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
           

            

72 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
  20. 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
  21. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.android development course fees in chennai | android app development training in chennai

    ReplyDelete
  22. 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
  23. Hi, Thanks for sharing article posting. For more information please visit again. RPA Training in Chennai | Blue Prism Training in Chennai

    ReplyDelete
  24. myTectra Placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance

    ReplyDelete
  25. 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
  26. This comment has been removed by the author.

    ReplyDelete
  27. 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
  28. Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you once agian

    court marriage in delhi ncr
    court marriage in delhi
    court marriage in noida
    court marriage in ghaziabad
    court marriage in gurgaon
    court marriage in faridabad
    court marriage in greater noida
    name change online
    court marriage in chandigarh
    court marriage in bangalore

    ReplyDelete
  29. 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
  30. Disneyslot - Game Play -Slot Machine - Tembakikan - Agen Playtech - Joker123 - Kingkong - Casino Online

    Hanya Dengan Min Dp 10,000- dan Wd 50,000-, Anda Berkesempatan Meraih Keberuntungan/Kemenangan Di Disneyslot. Memudahkan Transaksi Melalui Bank BCA - BNI - MANDIRI - BRI - DANAMON - PULSA TELKOMSEL/XL dan OVO Payment.

    :: Hot Promo News ::
    • Big Bonus Deposit 50%
    • Next Bonus Deposit 20%
    • Bonus Cashback 5% Setiap Senin

    Kontak Kami :
    Whatsapp : +62813 9701 4667

    Link Alternatif Disneyslot :
    • http://156.67.217.134/disneyslot/

    Yuk Jangan Tunggu Lagi Daftar Sekarang Juga Dan Nikmati Kemenangan/Keberuntungan Bersama Disneyslot!!Dapatkan Bonus Tertinggi SlotGame Hanya Di Disneyslot.com

    #slotgame #agenjoker123 #bandarjudionline #agenkongkong #agenplaytech #situsgameslot #websitejudislot #agentembakikanonline #slotplaystar #agencasinoonline #agengpslot #bandarcasino #slotdisney #rajaslotgame #dewaslot #situsjoker #jackpot #promo #bonus #slot #machine #superbonus #sagaming #ebetcasino #asiagaming #allbet #sagaming #evocasino #baccarat #dragontiger #sicbo #tembakikan

    ReplyDelete
  31. 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
  32. 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
  33. Hi there I am so delighted I found your weblog, I really found you by accident,
    while I was researching on Bing for something else, Regardless I am here now and would just QUALITY UNDETECTED ORIGINAL FAKE DRIVING LICENCE like to say thank you for a fantastic post and a all round entertaining blog (I also love the theme/design), I don’t have time to go through it all at the minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the excellent job.

    ReplyDelete
  34. NDUW is a labor registration portal created by the Labor Department, Government of india. The registration of the unorganized workers (working class) of the state takes place on this portal.

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

    QuickBooks Customer Service

    ReplyDelete
  36. jan adhar card very usefull in rajsthan govt. All Process in Download See Now

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

    ReplyDelete
  38. 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