Aggiornamento: Ho aggiornato l'esempio per seguire le ultime specifiche, con maplikegetStats
.
Il seguente approccio segue the specification e attualmente funziona solo in Firefox, poiché Chrome implementa getStats()
in modo errato al momento. Speriamo che presto sia disponibile una versione del polyfill adapter.js che farà funzionare anche in Chrome.
Quando si esegue this fiddle in Firefox, vedrete:
checking
connected
Does not use TURN
Questo perché l'esempio fornisce sia uno STUN e un server TURN. Ma quando modifico la configurazione da utilizzare girare solo con iceTransportPolicy: "relay"
, vedo:
checking
connected
Uses TURN server: 10.252.73.50
Nota che il server di turno che uso è dietro una VPN, quindi non funzionerà per voi, ma sentitevi liberi di modificare la giocare con il tuo server (basta non salvarlo a meno che tu non voglia che le informazioni diventino pubbliche!)
Mentre non ho provato con più di un server di turno, come puoi vedere l'indirizzo IP mostrato corrisponde alla svolta server configurato, quindi dovrebbe essere possibile dire quale server viene utilizzato utilizzando questo approccio.
// 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>
https://github.com/webrtc/apprtc/pull/99 mostra come capire il tipo di server TURN utilizzato (UDP, TCP, TLS) - doesn Lavoriamo con Firefox, ma questo è in gran parte un problema del campione apprtc leggermente indietro. –