Android RTP Sample (Receiving via VLC player)

Simple demonstrate application for creating rtp audio stream using android.net.rtp package

   RTP Sender    : Android Application
   RTP Receiver : VLC Player

 Source :RtpSender (SVN)
 Zipped Source : RTPSender.zip

 Steps :   
       Create new android project with minimum sdk vesrion 12
       Open the MainActivity.java and make below code changes

    
package com.javaorigin.rtpsender;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.net.rtp.AudioCodec;
import android.net.rtp.AudioGroup;
import android.net.rtp.AudioStream;
import android.net.rtp.RtpStream;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  StrictMode.setThreadPolicy(policy);
  try {   
      AudioManager audio =  (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
      audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
      AudioGroup audioGroup = new AudioGroup();
      audioGroup.setMode(AudioGroup.MODE_NORMAL);        
      AudioStream audioStream = new AudioStream(InetAddress.getByAddress(getLocalIPAddress ()));
      audioStream.setCodec(AudioCodec.PCMU);
      audioStream.setMode(RtpStream.MODE_NORMAL);
                           //set receiver(vlc player) machine ip address(please update with your machine ip)
      audioStream.associate(InetAddress.getByAddress(new byte[] {(byte)192, (byte)168, (byte)1, (byte)19 }), 22222);
      audioStream.join(audioGroup);
     
   
  } catch (Exception e) {
   Log.e("----------------------", e.toString());
   e.printStackTrace();
  }
 }
public static byte[] getLocalIPAddress () {
    byte ip[]=null;
       try {
           for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
               NetworkInterface intf = en.nextElement();
               for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                   InetAddress inetAddress = enumIpAddr.nextElement();
                   if (!inetAddress.isLoopbackAddress()) {
                    ip= inetAddress.getAddress();
                   }
               }
           }
       } catch (SocketException ex) {
           Log.i("SocketException ", ex.toString());
       }
       return ip;
       
 }
 

}


please update the corresponding  remote ip address
Add  below permissions to AndroidManifest.xml
   
    
    <uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>

    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" >
    </uses-permission>

    <uses-permission android:name="android.permission.RECORD_AUDIO" >

    </uses-permission>

    
Run the above rtp sender application
Now start the vlc player and click Media-->Open Network Stream menu
Enter the following url  (please update with your machine ip) and click play button
  
         rtp://192.168.1.19:22222







Now you can hear the voice  and analysis the receiving data using following vlc options

     Open  Tools--->Codec Information
           

            

49 comments :

  1. Thanks for the code.
    I tried your RTPSender with only IP address change.
    Codec info shows "Bits per sample: 16" and increases dropped count on and on.
    I can't hear any sound on VLC.
    Do you know why?

    ReplyDelete
    Replies
    1. it might be codec issue on vlc , so please try some other codec like GSM,AMR,etc

      Delete
    2. I found the PCMA codec worked like a charm, however could not get the stream to work on Android Marshmallow (with permission checks).

      Delete
    3. Hey, I have the same problem I was just wondering if you found any solution to that problem?

      Delete
  2. thank you very muck , it help me a lot

    ReplyDelete
  3. Thanks for the code.I'm trying to do voice over in internet.I want you to be between android devices.How can I do it in a simple way?

    ReplyDelete
  4. I have tried your sample but VLC fails to connect. I am 99% sure I have all IP's and ports set up correctly. There is just very little information given via logcat or vlc as to why the connection fails. Are there any suggestions to help debug the connection failure?

    ReplyDelete
  5. I get:

    incompatible types
    found : java.lang.Object
    required: java.net.NetworkInterface

    What could be the issue here?

    ReplyDelete
    Replies
    1. You have to cast type

      NetworkInterface to type (NetworkInterface)
      and
      InetAddress to type (InetAddress)

      Delete
    2. This comment has been removed by the author.

      Delete
  6. In this line:
    NetworkInterface intf = en.nextElement();

    ReplyDelete
    Replies
    1. its the cast of the object

      NetworkInterface intf = (NetworkInterface)en.nextElement();

      Delete
  7. Thanks for the code! I am creating a similar application but the android device is receiving an RTP packets. The codec is PCMU, and the audio is very crackly and it repeats itself for a second sometimes. Any ideas as to why this is?

    ReplyDelete
    Replies
    1. The above code seems to have errors. Could you post your working code?

      Delete
  8. hi,

    if i wont to live streaming video to VLC, how to do it.

    thanks

    ReplyDelete
  9. really don't understand.
    cell phone ip 192.168.0.102
    VLC(pc side) 192.168.0.101
    rtp://192.168.0.102:22222 ==> VLC fail ??????

    ReplyDelete
    Replies
    1. The IP you are worried about is the PC's IP. The Android device streams to the PC, the PC (VLC) tunes in to itself.

      Delete
  10. Is it possible to stream video in the same way?

    ReplyDelete
    Replies
    1. I've been trying to figure this out myself. I need to send AND receive audio/video on Android.

      Delete
  11. This comment has been removed by the author.

    ReplyDelete
  12. public static byte[] getLocalIpAddress() {
    try {
    for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
    NetworkInterface intf = en.nextElement();
    for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
    InetAddress inetAddress = enumIpAddr.nextElement();
    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
    Log.e("ip",inetAddress.getHostAddress());
    Log.e("ip2",""+inetAddress.getAddress());

    return inetAddress.getAddress();
    }
    }
    }
    } catch (SocketException ex) {
    ex.printStackTrace();
    }

    ReplyDelete
  13. It worked, but I found the latency between my voice and the speaker in PC VLC, It is about 300ms, It is so bad. How can I decrease the latency?

    ReplyDelete
  14. how can we stream to multiple devices

    ReplyDelete
  15. i have tried using thread to make it work for multiple devices but i am getting alot of jitter.
    and how can we transfer the currently playing song. i mean i want the sound to be recieved by the mic.is there any possible solutions

    ReplyDelete
  16. Thanks for the sample code.
    I managed to create an application on Android that receives and transmits stream rtp, but I have a problem. In reception I can not set the local port but is chosen randomly from "rtpstream" (chosen automatically according to rfc 3550). Any idea how to overcome the problem by forcing the local port to listen?
    Thanks again for the help.

    ReplyDelete
  17. why is this working on the same ip network group only?....i tried it by changing the ip network group or by changing the network carrier it does not works.
    suppose my device ip address is '192.168.1.0'(my local wifi) and my receiver IP address is '192.168.3.1'(wifi at my friends house) then no voice can be heard !! is RTP streaming only for same network ip connected through a router ?? if not then please help me i am in serious trouble!! please do reply!!

    ReplyDelete
  18. Hello I tried to download the ZIP file but it seems to be broken... Where can I find it? thank you

    ReplyDelete
  19. It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving... very specific nice content.
    Best Android Training in Velachery | android development course fees in chennai

    ReplyDelete
  20. I tried above code in Android Studio 2.3.3 , but I got following error:

    Thread[1,tid=18486,Native,Thread*=0x7f91e96a00,peer=0x7485af88,"main"] recursive
    attempt to load library "/system/lib64/librtp_jni.so"
    java.net.SocketException: Invalid argument
    java.net.SocketException: Invalid argument
    at android.net.rtp.RtpStream.create(Native Method)
    at android.net.rtp.RtpStream.(RtpStream.java:71)
    at android.net.rtp.AudioStream.(AudioStream.java:59)
    at com.example.swanand.audiostreamdemo.MainActivity$1.onClick(MainActivity.java:91)

    Any solution for this?

    ReplyDelete
  21. Dimana anda tidak hanya tertuju pada 1 permainan saja. Anda pun akan bisa bermain semua permainan sambil mempelajari cara cara permianan tersebut. Sehingga anda akan bisa mendapatkan strategi terbaik dalam menaklukan kemenangan pada meja permainan.
    asikqq
    http://dewaqqq.club/
    http://sumoqq.today/
    interqq
    pionpoker
    bandar ceme terpercaya
    freebet tanpa deposit
    paito warna
    syair sgp

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. Hi, I have tried the code but we I am getting the following error
    E/AudioFlinger: Error when setting output stream volume: -38
    Can you please post the complete code...

    ReplyDelete
  24. So another possible meaning for "play movies on Apple computer" would be a movie you've downloaded off the Internet. There's a lot of different types of video files (video just means moving pictures, just like a movie) that are sent by email, or that can be downloaded off a web site. howtodoninja

    ReplyDelete
  25. Great to read article about berry update which i like reading about it.You have made an excellent effort in making this website which is appreciable. appvalley vip Sydney Brooke Simpson cotomovies streaming app

    ReplyDelete
  26. Hey! Amazing work. With full of knowledge.Our Team resolve any glitches occurring while utilizing the software. Looking for QuickBooks Phone Number Contact us 1-855-756-1077. Our experts will assist you to fulfill your accounting needs. The solutions are accurate and time-saving.

    ReplyDelete
  27. very nice blog blog which provide great information Find best software service for

    QuickBooks Customer Service

    ReplyDelete
  28. Welcome to CapturedCurrentNews – Latest & Breaking India News 2021
    Hello Friends My Name Anthony Morris.latest and breaking news drupepower.com

    ReplyDelete
  29. Thank you so much for sharing this amazing information about. I really appreciate your hard work for writing this article thanks once again

    for women skin tertinion cream

    ReplyDelete