更新:我已使用maplike 更新了示例以遵循最新规范getStats
。
以下方法遵循规范,目前仅适用于 Firefox,因为 Chrome 目前实现getStats()
不正确。希望adapter.js polyfill的一个版本很快就可以使用,它也可以在 Chrome 中使用。
当你在 Firefox 中运行这个小提琴时,你会看到:
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>