This commit is contained in:
yumoqing 2024-08-21 11:36:18 +08:00
parent cf2513960d
commit 8cfa1e1cd7

View File

@ -35,7 +35,7 @@ bricks.RTCClient = class extends bricks.VBox {
this.videobox.add_widget(this.localVideo);
this.videobox.add_widget(this.remoteVideo);
this.add_widget(this.videobox);
this.peerConnection;
this.pc;
this.onlineList= [];
this.socket = new WebSocket(this.signaling_url);
this.socket.onmessage = this.signaling_message_handle.bind(this);
@ -139,12 +139,12 @@ bricks.RTCClient = class extends bricks.VBox {
this.socket.send(JSON.stringify(d));
}
async send_offer(){
var offer = await this.peerConnection.createOffer();
this.peerConnection.setLocalDescription(offer);
console.log('offer =', offer, this.peerConnection.localDescription);
var offer = await this.pc.createOffer();
this.pc.setLocalDescription(offer);
console.log('offer =', offer, this.pc.localDescription);
this.socket.send(JSON.stringify({
type:'offer',
offer:this.peerConnection.localDescription,
offer:this.pc.localDescription,
to:this.peer_info
}));
}
@ -177,9 +177,9 @@ bricks.RTCClient = class extends bricks.VBox {
}));
}
async call_close(active){
this.peerConnection.onicecandidate = null;
this.peerConnection.ontrack = null;
this.peerConnection.close();
this.pc.onicecandidate = null;
this.pc.ontrack = null;
this.pc.close();
if (! active){
this.socket.send(JSON.stringify({
type:'disconnect',
@ -210,11 +210,13 @@ bricks.RTCClient = class extends bricks.VBox {
break;
case 'offer':
var offer = new RTCSessionDescription(d.offer);
var answer = this.peerConnection.setRemoteDescription(offer);
this.peerConnection.setLocalDescription(answer);
this.pc.setRemoteDescription(offer);
var answer = await this.pc.createAnswer();
this.pc.setLocalDescription(answer);
console.log('answer=', answer, 'desc=', this.pc.localDescription);
this.socket.send(JSON.stringify({
type:'answer',
answer:answer,
answer:this.pc.localDescription,
to:this.peer_info
}));
console.log('offer branch exe');
@ -222,16 +224,16 @@ bricks.RTCClient = class extends bricks.VBox {
case 'answer':
if (d.from == self.peer_info){
var desc = new RTCSessionDescription(d.answer);
this.peerConnection.setRemoteDescription(desc);
this.pc.setRemoteDescription(desc);
}
console.log('answer branch exe');
break;
case 'iceCandidate':
if (d.from == self.peer_info){
var candidate = new RTCIceCandidate(d.candidate);
this.peerConnection.addIceCandidate(candidate);
this.pc.addIceCandidate(candidate);
console.log('iceCandidate branch exe');
}
console.log('iceCandidate branch exe');
break;
case 'callAccepted':
await this.createPeerConnection(this.peer_info);
@ -263,16 +265,16 @@ bricks.RTCClient = class extends bricks.VBox {
{ urls: 'stun:stunserver2024.stunprotocol.org:3478' }
]
};
this.peerConnection = new RTCPeerConnection(configuration);
this.pc = new RTCPeerConnection(configuration);
// 处理本地流添加到 PeerConnection
this.localVideo
.get_stream()
.getTracks()
.forEach(track => {
this.peerConnection.addTrack(track, this.localVideo.stream);
this.pc.addTrack(track, this.localVideo.stream);
});
if (! this.peerConnection){
console.log('peerConnection is null');
if (! this.pc){
console.log('pc is null');
return;
}
if (this.role == 'requester'){
@ -280,10 +282,10 @@ bricks.RTCClient = class extends bricks.VBox {
}
// 处理 ICE 候选
this.peerConnection.onicecandidate = this.send_candidate.bind(this, to_info);
this.pc.onicecandidate = this.send_candidate.bind(this, to_info);
// 处理远程流添加
this.peerConnection.ontrack = event => {
this.pc.ontrack = event => {
this.remoteVideo.set_stream(event.streams[0]);
};
}