【m】解决远端黑屏问题

This commit is contained in:
2026-05-16 18:16:57 +08:00
parent 87c4a56306
commit 53166b648f
2 changed files with 9 additions and 8 deletions

View File

@@ -75,10 +75,11 @@ export class RenderStreaming {
if (this._isHost) { if (this._isHost) {
// host端为该participant创建或复用peer // host端为该participant创建或复用peer
// host端始终使用polite=falseimpolite确保perfect negotiation中host的offer优先
let peer = this._peers.get(participantId); let peer = this._peers.get(participantId);
if (!peer || (peer.pc && peer.pc.iceConnectionState === 'disconnected')) { if (!peer || (peer.pc && peer.pc.iceConnectionState === 'disconnected')) {
if (peer) peer.close(); if (peer) peer.close();
peer = this._preparePeerConnection(this._connectionId, offer.polite, participantId); peer = this._preparePeerConnection(this._connectionId, false, participantId);
} }
const desc = new RTCSessionDescription({ sdp: offer.sdp, type: "offer" }); const desc = new RTCSessionDescription({ sdp: offer.sdp, type: "offer" });
try { try {
@@ -87,13 +88,13 @@ export class RenderStreaming {
Logger.warn(`Error on GotDescription for participant ${participantId}: ${error}`); Logger.warn(`Error on GotDescription for participant ${participantId}: ${error}`);
} }
} else { } else {
// participant端使用单一peer // participant端使用单一peer始终使用polite=true
if (this._peer && this._peer.pc && this._peer.pc.iceConnectionState === 'disconnected') { if (this._peer && this._peer.pc && this._peer.pc.iceConnectionState === 'disconnected') {
this._peer.close(); this._peer.close();
this._peer = null; this._peer = null;
} }
if (!this._peer) { if (!this._peer) {
this._preparePeerConnection(offer.connectionId, offer.polite, null); this._preparePeerConnection(offer.connectionId, true, null);
} }
const desc = new RTCSessionDescription({ sdp: offer.sdp, type: "offer" }); const desc = new RTCSessionDescription({ sdp: offer.sdp, type: "offer" });
try { try {
@@ -171,10 +172,9 @@ export class RenderStreaming {
const participantId = data.participantId; const participantId = data.participantId;
Logger.log(`Participant joined: ${participantId}`); Logger.log(`Participant joined: ${participantId}`);
// host端为新participant创建peer // host端不在此处创建peer等待participant的offer到达后在_onOffer中创建
if (this._isHost && !this._peers.has(participantId)) { // 这样避免host和participant同时发offer导致的glare冲突
this._preparePeerConnection(this._connectionId, false, participantId); // _onOffer会在收到participant的offer时自动创建peer如果不存在
}
this.onParticipantJoined(participantId); this.onParticipantJoined(participantId);
} }

View File

@@ -239,7 +239,8 @@ function onOffer(ws: WebSocket, message: any): void {
} }
} else { } else {
// participant发送offer给host携带该participant的participantId // participant发送offer给host携带该participant的participantId
newOffer.polite = true; // host端应为impolitepolite=false确保perfect negotiation中host优先
newOffer.polite = false;
group.host.send(JSON.stringify({ from: connectionId, to: "", type: "offer", data: newOffer, participantId: senderParticipantId })); group.host.send(JSON.stringify({ from: connectionId, to: "", type: "offer", data: newOffer, participantId: senderParticipantId }));
} }
} }