RTCPeerConnection
RTCPeerConnection 인터페이스는 로컬 컴퓨터와 원격 피어 간의 WebRTC 연결을 담당하며 원격 피어에 연결하기 위한 메서드들을 제공하고, 연결을 유지하고 연결 상태를 모니터링하며 더 이상 연결이 필요하지 않을 경우 연결을 종료합니다.
addIceCandidate
RTCPeerConnection을 사용하는 웹이나 앱이 신규 ICE candidate를 signaling 채널을 통해 원격 유저로부터 수신하게되면, RTCPeerConnection.addIceCandidate()를 호출해서 브라우저의 ICE 에이전트에게 새로 수신한 candidate를 전달합니다. 이 메소드는 RTCPeerConnection의 원격 설명 (remote description)에 연결의 원격쪽 상태를 설명해주는 신규 원격 candidate를 추가합니다.
addIceCandidate() 호출시 candidate 매개변수가 존재하지 않거나 값이 null인 경우에, 추가된 ICE candidate는 "candidate 종료"를 알려줍니다. 지정한 객체의candidate 값이 존재하지 않거나, 빈 문자열 ("")인 경우에도 원격 candidate들이 모두 전달되었음을 알려줍니다.
"candidate 종료" 알림은 a-line 값의 end-of-candidates를 가진 candidate와 함께 원격 유저에 송신됩니다.
네고시에이션 중에 앱이 위의 방법처럼 ICE 에이전트에 전달할 다수의 candidate를 수신 받을 수 있고, 이는 가능한 연결 방법들의 리스트를 만들 수 있도록 도와줍니다. 자세한 내용은 WebRTC connectivity와 Signaling and video calling를 참고하십시오.
강제 TURN 서버 사용
iceTransportPolicy: "relay"
옵션을 사용하면 된다.
Troubleshooting
Both username and credential are required when the URL scheme is "turn" or "turns"
Failed to construct 'RTCPeerConnection': Both username and credential are required when the URL scheme is "turn" or "turns".
TURN서버에 사용자와 비밀번호를 추가하자.
Can't create RTCPeerConnections when the network is down
Firefox에서 RTCPeerConnection을 생성할 때, 네트워크가 offline 상태일 경우 위와 같은 에러를 뱉으면서, 더 이상 진행되지 않는다.
gecko 엔진의 RTCPeerConnection 생성자 구현을 확인해 보면 다음과 같다.
RTCPeerConnection.prototype = {
classDescription: "mozRTCPeerConnection",
classID: PC_CID,
contractID: PC_CONTRACT,
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
Ci.nsIDOMGlobalPropertyInitializer]),
init: function(win) { this._win = win; },
__init: function(rtcConfig) {
if (!rtcConfig.iceServers ||
!Services.prefs.getBoolPref("media.peerconnection.use_document_iceservers")) {
rtcConfig.iceServers =
JSON.parse(Services.prefs.getCharPref("media.peerconnection.default_iceservers"));
}
this._winID = this._win.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
this._mustValidateRTCConfiguration(rtcConfig,
"RTCPeerConnection constructor passed invalid RTCConfiguration");
if (_globalPCList._networkdown || !this._win.navigator.onLine) {
throw new this._win.DOMException(
"Can't create RTCPeerConnections when the network is down",
"InvalidStateError");
}
this.makeGetterSetterEH("onaddstream");
this.makeGetterSetterEH("onaddtrack");
this.makeGetterSetterEH("onicecandidate");
this.makeGetterSetterEH("onnegotiationneeded");
this.makeGetterSetterEH("onsignalingstatechange");
this.makeGetterSetterEH("onremovestream");
this.makeGetterSetterEH("ondatachannel");
this.makeGetterSetterEH("oniceconnectionstatechange");
this.makeGetterSetterEH("onidentityresult");
this.makeGetterSetterEH("onpeeridentity");
this.makeGetterSetterEH("onidpassertionerror");
this.makeGetterSetterEH("onidpvalidationerror");
this._pc = new this._win.PeerConnectionImpl();
this._taskChain = this._win.Promise.resolve();
this.__DOM_IMPL__._innerObject = this;
this._observer = new this._win.PeerConnectionObserver(this.__DOM_IMPL__);
// Add a reference to the PeerConnection to global list (before init).
_globalPCList.addPC(this);
this._impl.initialize(this._observer, this._win, rtcConfig,
Services.tm.currentThread);
this._initIdp();
_globalPCList.notifyLifecycleObservers(this, "initialized");
},
네트워크가 연결된 상태로 진행하거나, 강제로 네트워크 상태를 online 으로 고정해야 한다.
Firefox의 about:config
페이지에서 network.manage-offline-status
설정을 false
로 변경하면 된다.