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
           

            

30 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. Yeah its a good article. According to you what we project managers do is communicating. And a lot of this communication is done during project meetings. It can sometimes feel like you are running from one meeting to another and that your time is often wasted. Meetings don’t start on time, the issues aren’t dealt with, there is no agenda, there is no focus, nobody assigns any follow ups or tasks and of course then they also don’t end on time. An efficient project manager is required for the good management of a project. I think a project manager should PMP certified. Looking forwards to apply what I learned in PMP classes in my company.

    ReplyDelete
  10. 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
  11. 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
  12. This comment has been removed by the author.

    ReplyDelete
  13. 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
  14. 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
  15. how can we stream to multiple devices

    ReplyDelete
  16. 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
  17. 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
  18. 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
  19. Hello I tried to download the ZIP file but it seems to be broken... Where can I find it? thank you

    ReplyDelete