更新:我更新了示例以遵循最新的規範,使用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示例稍微落後的問題。 –