This commit is contained in:
yumoqing 2024-10-11 15:53:30 +08:00
parent 1bd3db21d3
commit 595e2c126f

View File

@ -325,6 +325,7 @@ bricks.RTCP2PConnect = class {
async connection_statechange(peer, event){
var pc = this.peers[peer.id].pc;
var role = this.peers[peer.id].role;
console.log(`${peer.id} state changed. new state=${pc.connectionState}`);
if (pc.connectionState == 'disconnected'){
var w = new bricks.Error({
@ -336,18 +337,15 @@ bricks.RTCP2PConnect = class {
return;
}
if (pc.connectionState == 'connected'){
if (this.opts.data_connect){
if (this.opts.data_connect && role == 'requester'){
var dc = pc.createDataChannel();
if (this.requester && this.opts.data_connect){
await this.createDataChannel(peer);
}
await this.dc_created(peer, dc);
}
}
}
async createDataChannel(peer){
var pc = this.peers[peer.id].pc;
dc = pc.createDataChannel('data');
async dc_created(peer, dc){
console.log('dc_created.....');
this.peers[peer.id].dc = dc;
dc.onmessage = this.datachannel_message.bind(this, peer);
dc.onopen = this.datachannel_open(this, peer);
@ -355,18 +353,21 @@ bricks.RTCP2PConnect = class {
}
async datachannel_message(peer, msg){
console.log('datachannel_message():', peer. msg);
var dc = this.peers[peer.id].dc;
if (this.opts.on_dc_messaage){
await this.opts.on_dc_message(dc, msg);
}
}
async datachannel_open(peer){
console.log('datachannel_open():', arguments, peer);
var dc = this.peers[peer.id].dc
if (this.opts.on_dc_open){
await this.opts.on_dc_open(dc);
}
}
async datachannel_close(peer){
console.log('datachannel_close():', peer);
var dc = this.peers[peer.id].dc
if (this.opts.on_dc_close){
await this.opts.on_dc_close(dc);
@ -394,10 +395,7 @@ bricks.RTCP2PConnect = class {
pc.onconnectionstatechange = this.connection_statechange.bind(this, peer);
pc.ondatachanel = event => {
var dc = event.channel;
this.peers[peer.id].dc = dc;
dc.onmessage = this.data_message_recv.bind(this, peer);
dc.onopen = this.datachannel_opened(this, peer);
dc.onclose = this.datachannl_closed(this, peer);
await this.dc_created(peer, dc);
}
pc.ontrack = event => {
remoteVideo.set_stream(event.streams[0]);
@ -409,7 +407,7 @@ bricks.RTCP2PConnect = class {
var mediaOptions = this.opts.media_options;
this.local_stream = await navigator.mediaDevices.getUserMedia(mediaOptions);
this.localVideo = new bricks.VideoBox();
this.localVideo.set_stream(local_stream);
this.localVideo.set_stream(this.local_stream);
} catch (error) {
console.error('获取本地媒体流失败:', error);
}