更新:我更新了示例以遵循最新的规范,使用maplikegetStats
。
以下方法跟在the specification之后,目前只能在Firefox中使用,因为Chrome目前不正确执行getStats()
。希望adapter.js polyfill的一个版本即将推出,这也将使这项工作在Chrome中成功实现。
当您运行在Firefox this fiddle,你会看到:
checking
connected
Does not use TURN
这是因为,例如同时提供了STUN和TURN服务器。但是,当我修改使用TURN的配置只能用iceTransportPolicy: "relay"
,我看到:
checking
connected
Uses TURN server: 10.252.73.50
请注意,我用的是转服务器是后面的VPN,所以它不会为你工作,但随时修改摆弄你自己的服务器(只是不要保存它,除非你想让信息公开!)
虽然我没有测试多个回合服务器,但你可以看到显示的IP地址与回合服务器已配置,因此应该可以使用此方法来确定使用哪台服务器。
// Turn server is on Mozilla's VPN.
var cfg = { iceTransportPolicy: "all", // set to "relay" to force TURN.
iceServers: [{ urls: "stun:stun.l.google.com:19302" },
{ urls: "turn:10.252.73.50",
username:"webrtc", credential:"firefox" }] };
var pc1 = new RTCPeerConnection(cfg), pc2 = new RTCPeerConnection(cfg);
pc1.onicecandidate = e => pc2.addIceCandidate(e.candidate);
pc2.onicecandidate = e => pc1.addIceCandidate(e.candidate);
pc2.oniceconnectionstatechange =() => log(pc2.iceConnectionState);
pc2.onaddstream = e => v2.srcObject = e.stream;
var findSelected = stats =>
[...stats.values()].find(s => s.type == "candidate-pair" && s.selected);
var start =() => navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => pc1.addStream(v1.srcObject = stream))
.then(() => pc1.createOffer()).then(d => pc1.setLocalDescription(d))
.then(() => pc2.setRemoteDescription(pc1.localDescription))
.then(() => pc2.createAnswer()).then(d => pc2.setLocalDescription(d))
.then(() => pc1.setRemoteDescription(pc2.localDescription))
.then(() => waitUntil(() => pc1.getStats().then(s => findSelected(s))))
.then(() => pc1.getStats())
.then(stats => {
var candidate = stats.get(findSelected(stats).localCandidateId);
if (candidate.candidateType == "relayed") {
log("Uses TURN server: " + candidate.ipAddress);
} else {
log("Does not use TURN (uses " + candidate.candidateType + ").");
}
})
.catch(log);
var waitUntil = f => Promise.resolve(f())
.then(done => done || wait(200).then(() => waitUntil(f)));
var wait = ms => new Promise(resolve => setTimeout(resolve, ms));
var log = msg => div.innerHTML += msg +"<br>";
var failed = e => log(e +", line "+ e.lineNumber);
<video id="v1" width="108" height="81" autoplay></video>
<video id="v2" width="108" height="81" autoplay></video><br>
<button onclick="start()">Start!</button><br><div id="div"></div>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
来源
2015-08-25 21:33:02
jib
https://github.com/webrtc/apprtc/pull/99告诉您如何找出使用(UDP,TCP,TLS)TURN服务器的类型 - 它不不适用于Firefox,但这主要是apprtc示例稍微落后的问题。 –