Android receive RTP/UDP audio stream from VLC/ffmpeg

2.8k views Asked by At

I was searching for a good answer for half a day, but I am a beginner at this stuff and I would appreciate any help.

What I would like to achieve is to stream audio (mp3 files) within ffmpeg or vlc and receive it on an Android device by udp/rtp.

This is what I was able to figure out myself sofar:

1) There is Android class AudioStream and RTPStream. What I don't know is how to use it. For example I create a stream via ffmpeg with: ffmpeg -re -i mymp3.mp3 -ar 8000 -acodec copy -f rtp rtp://192.168.0.100:5533, where 192.168.0.100 is the address of my Android device. Now I would like to receive it and play it.

I found something like this on Stack:

AudioStream audioStream;
AudioGroup audioGroup;
AudioCodec codec = AudioCodec.PCMU;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
AudioManager audio = (AudioManager)getSystemService(AUDIO_SERVICE);
audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioGroup = new AudioGroup();
audioGroup.setMode(AudioGroup.MODE_NORMAL);
InetAddress inetAddress;
try {
    inetAddress = InetAddress.getByName("163.11.62.208");
    audioStream = new AudioStream(inetAddress);
    audioStream.setMode(RtpStream.MODE_RECEIVE_ONLY);
    audioStream.setCodec(codec);
    InetAddress inetAddressRemote = InetAddress.getByName("163.11.169.206");
    audioStream.associate(inetAddressRemote, 5004);
    audioStream.join(audioGroup);
}

What is the first inetAddress 163.11.62.208 and what is the second one 163.11.169.206? Shoudln't I just give an address of a stream?

2) Can I submit only streams in PCMU format? Can I stream mp3 files?

3) Is it even possible?

1

There are 1 answers

0
Olga Khylkouskaya On

I've implemented Cisco Jabber integration with our server and Android and had similar set up.

audioStream = new AudioStream(inetAddress)

inetAddress(163.11.62.208) is the local network address of that android device. We get it using the following:

WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ip);
Log.w(TAG, "ipAddress=" + ipAddress);
inetAddress = InetAddress.getByName(ipAddress);

There is may be other ways, i'm not and Android developer.

audioStream.associate(inetAddressRemote, 5004)

inetAddressRemote(163.11.169.206) is the remote address of the server from which you'll be sending audio to Android.

5004 is the port to send audio to and from on both Android and server side. Now there is a catch - make sure the local port you send audio from the server to Android is also a 5004. For example test audio stream:

ffmpeg -re -f lavfi -i aevalsrc="sin(400*2*PI*t)" -map 0:0  -c:a pcm_mulaw -b:a 64k -ar 8000 -f rtp rtp://163.11.62.208:5004?localrtpport=5004