This commit is contained in:
yumoqing 2024-10-12 11:35:27 +08:00
parent 97da5deefe
commit 0db3f732a7

View File

@ -208,11 +208,11 @@ bricks.RTCP2PConnect = class {
return this.signal_handlers[typ]; return this.signal_handlers[typ];
} }
async p2pconnect(peer, role){ async p2pconnect(peer){
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, role); await this.createPeerConnection(peer);
} else { } else {
aconsole.log(peer, 'connect exists', this); aconsole.log(peer, 'connect exists', this);
} }
@ -241,7 +241,7 @@ bricks.RTCP2PConnect = class {
} }
} }
async h_callaccepted(data){ async h_callaccepted(data){
await this.send_offer(data.msgfrom); await this.send_offer(data.msgfrom, true);
} }
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);
@ -255,7 +255,7 @@ bricks.RTCP2PConnect = class {
answer:pc.localDescription, answer:pc.localDescription,
msgto:data.msgfrom msgto:data.msgfrom
}); });
if (this.peers[data.msgfrom.id].role == 'responser'){ if (this.peers[data.msgfrom.id].role != 'requester'){
await this.send_offer(data.msgfrom); await this.send_offer(data.msgfrom);
} }
@ -274,9 +274,14 @@ bricks.RTCP2PConnect = class {
var pc = this.peers[data.msgfrom.id].pc; var pc = this.peers[data.msgfrom.id].pc;
pc.close(); pc.close();
} }
async send_offer(peer){ async send_offer(peer, initial){
console.log('send_offer(), peers=', this.peers, 'peer=', peer); console.log('send_offer(), peers=', this.peers, 'peer=', peer);
var pc = this.peers[peer.id].pc; var pc = this.peers[peer.id].pc;
if (initial){
this.peers[peer.id].role = 'requester';
} else {
this.peers[peer.id].role = 'responser';
}
var offer = await pc.createOffer(); var offer = await pc.createOffer();
await pc.setLocalDescription(offer); await pc.setLocalDescription(offer);
var d = { var d = {
@ -321,7 +326,6 @@ bricks.RTCP2PConnect = class {
async connection_statechange(peer, event){ async connection_statechange(peer, event){
var pc = this.peers[peer.id].pc; var pc = this.peers[peer.id].pc;
var role = this.peers[peer.id].role;
console.log(`${peer.id} state changed. new state=${pc.connectionState}`); console.log(`${peer.id} state changed. new state=${pc.connectionState}`);
console.log('state=', pc.connectionState, typeof(pc.connectionState)); console.log('state=', pc.connectionState, typeof(pc.connectionState));
if (pc.connectionState == 'disconnected'){ if (pc.connectionState == 'disconnected'){
@ -332,8 +336,7 @@ bricks.RTCP2PConnect = class {
} }
if (pc.connectionState == 'connected'){ if (pc.connectionState == 'connected'){
console.log('state is connected, data_connect=', console.log('state is connected, data_connect=',
this.opts.data_connect, this.opts.data_connect);
'role=', role);
} }
} }
@ -371,7 +374,7 @@ bricks.RTCP2PConnect = class {
await this.opts.on_dc_close(dc); await this.opts.on_dc_close(dc);
} }
} }
async createPeerConnection(peer, role){ async createPeerConnection(peer){
const configuration = { const configuration = {
iceServers:this.opts.ice_servers iceServers:this.opts.ice_servers
} }
@ -385,7 +388,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; this.peers[peer.id].role = '';
var remoteVideo = new bricks.VideoBox(); var remoteVideo = new bricks.VideoBox();
this.peers[peer.id].video = remoteVideo; this.peers[peer.id].video = remoteVideo;
if (role == 'requester'){ if (role == 'requester'){
@ -405,6 +408,20 @@ bricks.RTCP2PConnect = class {
remoteVideo.set_stream(event.streams[0]); remoteVideo.set_stream(event.streams[0]);
} }
} }
async changeLocalVideoStream(peer, new_stream){
var pc = this.peers[peer.id].pc,
const senders = pc.getSenders();
const oldVideoTrackSender = senders.find(sender => sender.track && sender.track.kind === 'video');
if (oldVideoTrackSender) {
pc.removeTrack(oldVideoTrackSender);
}
new_stream.getTracks().forEach(track => {
if (track.kind === 'video') {
pc.addTrack(track, newStream);
}
});
await this.send_offer(peer, true);
}
async getLocalStream() { async getLocalStream() {
if (this.opts.media_options){ if (this.opts.media_options){
try { try {
@ -415,7 +432,33 @@ bricks.RTCP2PConnect = class {
} catch (error) { } catch (error) {
console.error('获取本地媒体流失败:', error); console.error('获取本地媒体流失败:', error);
} }
try {
this.local_screen = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: false });
} catch (error) {
console.error('获取本地Screen失败', error);
} }
} }
} }
peer_close(peer){
var pc = this.peers[peer.id].pc;
var video = this.peers[peer.id].video;
pc.getReceivers().forEach(receiver => {
if (receiver.track){
receiver.track.stop();
}
});
pc.getSenders().forEach(receiver => {
if (receiver.track){
receiver.track.stop();
}
});
video.srcObject.getTracks().forEach(track => track.stop());
var dc = this.peers[peer.id].dc;
if (dc){
dc.close();
}
pc.close();
self.peers = {k:v for k,v in self.peers if k!=peer.id};
}
});