A B <!--创建本地媒体流-->
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
//绑定本地媒体流到video标签用于输出
$('.localVideo').attr('src', URL.createObjectURL(stream));
localStream = stream;
})
A --oncall-->B
B --accept-->A
A <!--创建peerConnection-->
pc = new RTCPeerConnection(iceServer);
pc.onicecandidate = function(event){
//发送ice_candidate
}
pc.onaddstream = function (event) {
//设置远端stream
}
pc.addStream(localVideo);
A <!--创建offer-->
pc.createOffer.then(function (offer) {
return pc.setLocalDescription(offer); //触发onicecandidate发送ice
}).then(function () {
<!--发送offer事件和pc.localDescription-->
}
A --offer-->B
B <!--创建answer-->
pc.setRemoteDescription(data.sdp).then(); //触发onaddstrean将远端视频流放入video
pc.createAnswer().then(function (answer) {
return pc.setLocalDescription(answer); //触发onicecandidate发送ice
}).then(function () {
<!--发送answer事件和pc.localDescription-->
})
B --answer-->A
A <!--收到answer-->
pc.setRemoteDescription(data.sdp).then();
创建多个peerConnection,放入对象或数组集合
var pc = new RTCPeerConnection();
var state = pc.iceConnectionState;
iceGatheringState(检测本地ice状态)有3个状态:
iceConnectionState(获取远端ice状态)有7个状态:
检测到iceConnectionState == 'failed’时,重置peerConnection,创建一个offer发送给对方