This commit is contained in:
yumoqing 2024-10-11 15:02:24 +08:00
parent d1937661ed
commit 4f753548c7

View File

@ -208,17 +208,20 @@ bricks.RTCP2PConnect = class {
return this.signal_handlers[typ]; return this.signal_handlers[typ];
} }
async p2pconnect(peer){ async p2pconnect(peer, role){
await this.getLocalStream(); await this.getLocalStream();
var p = this.peers[peer.id]; var p = this.peers[peer.id];
if (!p){ if (!p){
await this.createPeerConnection(peer); await this.createPeerConnection(peer, role);
} else { } else {
aconsole.log(peer, 'connect exists', this); aconsole.log(peer, 'connect exists', this);
} }
console.log('p2pconnect() called, this=', this, 'peer=', peer); console.log('p2pconnect() called, this=', this, 'peer=', peer);
if (role=='requester'){
await this.send_offer(peer); await this.send_offer(peer);
} }
}
async h_sessioncreated(data){ async h_sessioncreated(data){
if (this.opts.peer_info){ if (this.opts.peer_info){
var d = { var d = {
@ -236,13 +239,13 @@ bricks.RTCP2PConnect = class {
msgto:data.msgfrom msgto:data.msgfrom
}; };
this.signaling_send(d); this.signaling_send(d);
await this.p2pconnect(data.msgfrom); await this.p2pconnect(data.msgfrom, 'responser');
return; return;
} }
this.dispatch('callrequest', data.msgfrom); this.dispatch('callrequest', data.msgfrom);
} }
async h_callaccepted(data){ async h_callaccepted(data){
await this.p2pconnect(data.msgfrom); await this.p2pconnect(data.msgfrom, 'requester');
} }
async h_offer(data){ async h_offer(data){
console.log('h_offer(), this=', this, 'peer=', data.msgfrom); console.log('h_offer(), this=', this, 'peer=', data.msgfrom);
@ -256,6 +259,10 @@ bricks.RTCP2PConnect = class {
answer:pc.localDescription, answer:pc.localDescription,
msgto:data.msgfrom msgto:data.msgfrom
}); });
if (this.peers[data.msgfrom.id].role == 'responser'){
await this.send_offer(peer);
}
} }
async h_answer(data){ async h_answer(data){
var desc = new RTCSessionDescription(data.answer); var desc = new RTCSessionDescription(data.answer);
@ -365,7 +372,7 @@ bricks.RTCP2PConnect = class {
await this.opts.on_dc_close(dc); await this.opts.on_dc_close(dc);
} }
} }
async createPeerConnection(peer){ async createPeerConnection(peer, role){
const configuration = { const configuration = {
iceServers:this.opts.ice_servers iceServers:this.opts.ice_servers
} }
@ -378,6 +385,7 @@ bricks.RTCP2PConnect = class {
} }
this.peers[peer.id] = bricks.extend({}, peer); this.peers[peer.id] = bricks.extend({}, peer);
this.peers[peer.id].pc = pc; this.peers[peer.id].pc = pc;
this.peers[peer.id].role = role;
var remoteVideo = new bricks.VideoBox(); var remoteVideo = new bricks.VideoBox();
this.peers[peer.id].video = remoteVideo; this.peers[peer.id].video = remoteVideo;
pc.onicecandidate = this.send_candidate.bind(this, peer); pc.onicecandidate = this.send_candidate.bind(this, peer);