use*_*068 2 node.js socket.io webrtc
我在下面的脚本这是一个混合应用程序的一部分创建的,有时它正常工作,并且可以接收/发送音频/视频通话,但有时onaddstream还是ontrack 不甚至从发送方调用,但SPD数据包正在通过socket发送的,我都试过了,(onaddstream or ontrack)但都没有成功:
从这里发送报价pc:
sendOffer() {
let that = this;
that.call_status = 'connecting';
let call_type;
if (that.call_type == 'audio')
call_type = { video: false, audio: true };
else
call_type = { video: true, audio: true };
that.pc = new RTCPeerConnection(that.peerConnectionConfig);
that.haveGum = navigator.mediaDevices.getUserMedia(call_type)
.then(stream => {
that.pc.addStream(that.from_video.nativeElement.srcObject = stream);
that.from_video.nativeElement.style.display = 'block';
}).then(() => that.pc.createOffer())
.then(d => that.pc.setLocalDescription(d))
.catch(log => { alert(log) });
that.pc.oniceconnectionstatechange = function (e) {
that.call_status = that.pc.iceConnectionState;
if (that.pc.iceConnectionState == 'disconnected') {
console.log('Disconnected');
}
}
that.pc.onaddstream = e => {
that.to_video.nativeElement.srcObject = e.stream;
};
that.pc.onicecandidate = e => {
if (e.candidate) {
return;
}
that.offerSent = true;
that.socket.emit('sdp-offer', {
from: that.user,
sdp: that.pc.localDescription.sdp,
call_type: call_type
});
};
that.socket.on('sdp-offer-reply', (sdp: any) => {
that.pc.setRemoteDescription(new RTCSessionDescription(({ type: "answer", sdp: sdp.sdp }))).catch(log => console.log(log));
});
that.socket.on('call-closed', (sdp: any) => {
that.closeConnection();
});
}
Run Code Online (Sandbox Code Playgroud)
pc2当接受答案时,在其他设备上:
answerCall() {
let that = this;
let call_type;
if (this.call_type == 'audio')
call_type = { video: false, audio: true };
else
call_type = { video: true, audio: true };
that.pc2 = new RTCPeerConnection(this.peerConnectionConfig);
that.haveGum = navigator.mediaDevices.getUserMedia(call_type)
.then(stream => {
that.pc2.addStream(this.from_video.nativeElement.srcObject = stream);
});
that.pc2.oniceconnectionstatechange = function (e) {
console.log(that.pc2.iceConnectionState);
}
that.pc2.onaddstream = e => {
that.to_video.nativeElement.srcObject = e.stream;
that.to_video.nativeElement.style.display = 'block';
};
if (that.pc2.signalingState != "stable") {
that.call_status = that.pc2.signalingState;
alert("not stable");
return;
}
that.pc2.setRemoteDescription(new RTCSessionDescription(({ type: "offer", sdp: this.sdp.sdp })))
.then(() => that.pc2.createAnswer())
.then(d => {
that.sendSdpAnswer = d; that.pc2.setLocalDescription(d);
this.call_connected = true;
})
.catch(log => console.log(log));
that.pc2.onicecandidate = e => {
if (e.candidate) {
console.log("not e.candidate");
return;
}
that.socket.emit('offeraccepted', {
from: that.user,
sdp: that.sendSdpAnswer.sdp
});
};
that.socket.on('call-closed', (sdp: any) => {
that.closeConnection();
that.call_status = "Hung Up";
});
}
Run Code Online (Sandbox Code Playgroud)
这是我调用的最后一个函数,用于在呼叫结束时关闭双方的对等连接:
closeConnection() {
if (typeof this.pc !== "undefined" && this.pc.signalingState != "closed") {
this.pc.close();
}
if (typeof this.pc2 !== "undefined" && this.pc2.signalingState != "closed") {
this.pc2.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我使用 webrtc latest-adapter.js 和 socket.io 作为信号服务器。首先,我EMIT事件sdp-offer上pc发送SDP包和pc2我收到sdp-offer-incoming的节点服务器,比pc2EMITofferaccepted与事件附加SDP数据,在pc1我收到的SDP数据包,并显示所行接收视频/两个PC的音频,因为它应该,但有时发件人流,但接收器总是有两个视频。
我在创建报价时必须通过约束:
this.pc.createOffer({
offerToReceiveAudio: 1,
offerToReceiveVideo: 1
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
978 次 |
| 最近记录: |