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