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
      

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Nice sample. How can this be made for sending video?

    ReplyDelete
  3. First off thanks for this, it works great on Lollipop and older devices. However I am finding that on Android M devices it doesn't work. I've gone ahead and implemented the proper permissions request and I am getting all the proper permissions but no audio. No error in the output either though.

    ReplyDelete
  4. This was most helpful, thanks.

    How about the other way around? playing from VLC and receiving on an Android device??

    ReplyDelete