This commit is contained in:
yumoqing 2024-08-23 11:53:23 +08:00
parent 3af4d43157
commit 224afacc2c

View File

@ -152,7 +152,7 @@ bricks.RTCClient = class extends bricks.VBox {
async send_offer(){ async send_offer(){
var offer = await this.pc.createOffer(); var offer = await this.pc.createOffer();
await this.pc.setLocalDescription(offer); await this.pc.setLocalDescription(offer);
console.log('offer =', offer, this.pc.localDescription); console.log('########send_offer =', offer, this.pc.localDescription);
this.socket.send(JSON.stringify({ this.socket.send(JSON.stringify({
type:'offer', type:'offer',
offer:this.pc.localDescription, offer:this.pc.localDescription,
@ -190,6 +190,7 @@ bricks.RTCClient = class extends bricks.VBox {
})); }));
} }
async call_close(active){ async call_close(active){
this.role = null;
this.pc.onicecandidate = null; this.pc.onicecandidate = null;
this.pc.ontrack = null; this.pc.ontrack = null;
this.pc.close(); this.pc.close();
@ -241,16 +242,19 @@ bricks.RTCClient = class extends bricks.VBox {
await this.pc.setRemoteDescription(offer); await this.pc.setRemoteDescription(offer);
var answer = await this.pc.createAnswer(); var answer = await this.pc.createAnswer();
await this.pc.setLocalDescription(answer); await this.pc.setLocalDescription(answer);
console.log('answer=', answer, 'desc=', this.pc.localDescription); console.log('###### send_answer=', answer, 'desc=', this.pc.localDescription);
this.socket.send(JSON.stringify({ this.socket.send(JSON.stringify({
type:'answer', type:'answer',
answer:this.pc.localDescription, answer:this.pc.localDescription,
to:this.peer_info to:this.peer_info
})); }));
console.log('offer branch exe'); console.log('offer branch exe');
if (this.role == 'responser'){
this.send_offer();
}
break; break;
case 'answer': case 'answer':
console.log('answer: d.from=', d.from, 'peer_info=', this.peer_info); console.log('#### receive_answer: d.from=', d.from, 'peer_info=', this.peer_info);
if (d.from.id == this.peer_info.id){ if (d.from.id == this.peer_info.id){
var desc = new RTCSessionDescription(d.answer); var desc = new RTCSessionDescription(d.answer);
await this.pc.setRemoteDescription(desc); await this.pc.setRemoteDescription(desc);
@ -321,18 +325,14 @@ bricks.RTCClient = class extends bricks.VBox {
this.pc.ontrack = event => { this.pc.ontrack = event => {
this.remoteVideo.set_stream(event.streams[0]); this.remoteVideo.set_stream(event.streams[0]);
}; };
if (this.role == 'requester'){
await this.send_offer(); await this.send_offer();
}
console.log('createPeerConnection() finished'); console.log('createPeerConnection() finished');
} }
async ice_statechange(event){ async ice_statechange(event){
console.log(`oniceconnectionstatechange, pc.iceConnectionState is ${this.pc.iceConnectionState}.`); console.log(`oniceconnectionstatechange, pc.iceConnectionState is ${this.pc.iceConnectionState}.`);
if (this.pc.iceConnectionState === 'disconnected'){
if (this.role == 'requester'){
await this.send_offer();
}
}
} }
async connection_statechange(event){ async connection_statechange(event){
console.log(`onconnectionstatechange, pc.connectionState is ${this.pc.connectionState}.`); console.log(`onconnectionstatechange, pc.connectionState is ${this.pc.connectionState}.`);