3

I'm trying to implement some WebRTC calls on my android devices. I have two emulators and one physical device.

My signalling server is located on the public network.

When I connect android emulator and physical device, this is the only string i have

IceServer.builder("stun:stun.l.google.com:19302").createIceServer(),

and it provides the connection.

But when I try to connect two android emulators I have to use TURN server that I've got from metered.ca.

So my last ice candidates are:

specific.a96e842f731140c29bacaed182a2da05!0a2333eaae7a4631919cbda03caf160f   {'action': 'ice', 'message': {'id': 'video', 'label': 0, 'candidate': 'candidate:123310808 1 udp 41754367 216.39.253.22 55142 typ relay raddr 77.234.203.142 rport 61291 generation 0 ufrag WPAt network-id 3 network-cost 900'}}
specific.a96e842f731140c29bacaed182a2da05!0a2333eaae7a4631919cbda03caf160f   {'action': 'ice', 'message': {'id': 'video', 'label': 0, 'candidate': 'candidate:123310808 1 udp 41885439 216.39.253.22 36761 typ relay raddr 77.234.203.142 rport 64965 generation 0 ufrag WPAt network-id 5 network-cost 10'}}
specific.a96e842f731140c29bacaed182a2da05!42e1ee0a15a842239405b5e77f760ab3   {'action': 'ice', 'message': {'id': 'video', 'label': 0, 'candidate': 'candidate:123310808 1 udp 41885439 216.39.253.22 63086 typ relay raddr 77.234.203.142 rport 53284 generation 0 ufrag UyvI network-id 3 network-cost 900'}}

As you can see, they both find RELAY and connected to it.

Why is it so?

nutella_eater
  • 3,393
  • 3
  • 27
  • 46

1 Answers1

4

The Android emulator runs behind a NAT device as documented here: https://developer.android.com/studio/run/emulator-networking

If each device has its own NAT and that NAT does not support NAT hairpin (see e.g. here then they won't be able to connnect each other.

Going via the external TURN server avoids this. You could run a TURN server such as coturn on your internal network to avoid this.

Philipp Hancke
  • 15,855
  • 2
  • 23
  • 31
  • Thanks for you answer! I know about emulators having NAT, but STUN server should dial with this NAT. I guess it does not work because of this hairpin thing. Could you elaborate it a little bit more? – nutella_eater May 09 '23 at 15:56
  • I guess the main question is how can I setup two emulators to work properly with only stun server. Can I use iptables on emulators? Or should I use redir? How can I access this internal router inside android emulator? – nutella_eater May 09 '23 at 16:48
  • STUN servers solve *some* NATs. iptables, which I believe is still what is used, is a NAT type that can't be traversed using only STUN. If you can get an iptables dump from the system(s) hosting the emulators that might show whether this can be tweaked there. Most production systems (or their testing) don't bother though since one needs TURN at some point anyway. – Philipp Hancke May 09 '23 at 19:12