简体   繁体   中英

How to stream WebRTC audio through a NAT?

I have a WebRTC multi-party app that works on both localhost and on an ngrok.io localhost tunnel. However, when I try and test it with my friend, who is connected through a router on their end, I am able to see an offer/answer exchange as well as an ICE candidate exchange, but no sound gets streamed through.

After first having this problem, I did some research and learned that you need a TURN server to get through a router's NAT. I'm using a public TURN server that I've confirmed works in https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/

var configuration = { 
    "iceServers": [{ "url": "stun:stun2.1.google.com:19302" }], 
     url: 'turn:192.158.29.39:3478?transport=udp', 
     credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=', 
     username: '28224511:1379330808' 
}; 

yourConn = new webkitRTCPeerConnection(configuration); 

yourConn2 = new webkitRTCPeerConnection(configuration); 

yourConn3 = new webkitRTCPeerConnection(configuration);

The sound packets should be routed through this TURN server and through my friend's NAT, but we still can't stream to each other.

Your turn server credentials are taken from https://www.html5rocks.com/en/tutorials/webrtc/infrastructure/ and have expired in 2013. If you used https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/ it should have told you this doesn't work -- i'd be rather surprised if it gave you relay candidates.

Run your own server.

You should change configuration:

var configuration = { 
  "iceServers": [
    { "url": "stun:stun2.1.google.com:19302" },
    {
      "url": "turn:192.158.29.39:3478?transport=udp", 
      "credential": "yourpassword", 
      "username": "yourusename" 
    }
  ], 

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM