Tuesday, August 21, 2012

[android-developers] Using the new RTP libraries to stream with NAT in the way

If you've used the new RTP libs you know that when you create an audio stream it magically decides what port it plans to use. Which works great when your RTP is coming directly to your publicly exposed IP address, but since almost none of us have this luxury and almost everyone is behind NAT in my application currently I have to punch a hole through the NAT router by sending a packet from my local RTP port to the RTP server on it's sending audio port. I can get this to work and the audio will stream in without the new rtp libs picking up the audio, but how do I accomplish this trick with the new libs since they require a bind to the local ip address before it will give me the intended recipient port and you can't double bind to a port/ip address. Thanks!

My setup looks like this:

inRtpStream = new AudioStream(InetAddress.getLocalHost());
inRtpStream.setMode(RtpStream.MODE_NORMAL);
inRtpStream.setCodec(AudioCodec.PCMU);

and then my punch hole code looks like this:


public static void punchHole(final int pPort, final String pRemoteHost) {

(new Thread() {

public void run() {

try {

String host = lServer;

int port = pPort;
byte[] message = "Java Source and Support".getBytes();

// Get the internet address of the specified host
InetAddress address = InetAddress.getByName(pRemoteHost);
Log.i("punchHole", "Punching a hole from port " + getAudio_port() + " to port " + port + " on address " + address.getHostAddress());

// Initialize a datagram packet with data and address
DatagramPacket packet = new DatagramPacket(message, message.length, address, port); 

// Create a datagram socket, send the packet through it,
// close it.
DatagramSocket dsocket = new DatagramSocket(getAudio_port());
dsocket.send(packet);
dsocket.close();
dsocket = null;
packet = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}

If I call punchHole(remote_audio_port, remote_media_address); after the audiostream is set then it errors with:

java.net.BindException: bind failed: EADDRINUSE (Address already in use)

Shaun

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment