Fix screenshare not visible until viewer reloads
When a screenshare was started while both peers were already connected, the viewer never received the video stream. This worked after a reload because the peer_joined handler proactively sends an offer to the new peer. The root cause: startSharing() relied on a request_stream round-trip (viewer sends request_stream, sharer responds with offer). Since handleSignal is async but never awaited by ws.onmessage, this round-trip could silently fail when promises rejected or messages interleaved during the exchange. Fix by having the sharer proactively send offers to all connected peers in startSharing(), matching the existing peer_joined behavior. Also add a fallback in ontrack for when event.streams is empty. https://claude.ai/code/session_01ALSwS4S8EHiP81i2KMsb9Y
This commit is contained in:
parent
6193fe4cf1
commit
4c223b47ed
1 changed files with 20 additions and 4 deletions
|
|
@ -714,7 +714,15 @@
|
||||||
console.log(`[${peerId.slice(0,8)}] Received track:`, event.track.kind);
|
console.log(`[${peerId.slice(0,8)}] Received track:`, event.track.kind);
|
||||||
const peer = state.peers.get(peerId);
|
const peer = state.peers.get(peerId);
|
||||||
if (peer) {
|
if (peer) {
|
||||||
peer.stream = event.streams[0];
|
// Use the associated stream, or build one from the track
|
||||||
|
// (event.streams can be empty in some WebRTC edge cases)
|
||||||
|
if (event.streams[0]) {
|
||||||
|
peer.stream = event.streams[0];
|
||||||
|
} else if (!peer.stream) {
|
||||||
|
peer.stream = new MediaStream([event.track]);
|
||||||
|
} else {
|
||||||
|
peer.stream.addTrack(event.track);
|
||||||
|
}
|
||||||
updateUI();
|
updateUI();
|
||||||
|
|
||||||
// Auto-select if this is the first/only stream
|
// Auto-select if this is the first/only stream
|
||||||
|
|
@ -913,13 +921,21 @@
|
||||||
stopSharing();
|
stopSharing();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Notify server - other peers will send request_stream in response
|
// Notify server
|
||||||
send({
|
send({
|
||||||
type: 'started_sharing',
|
type: 'started_sharing',
|
||||||
peer_id: state.peerId,
|
peer_id: state.peerId,
|
||||||
has_audio: stream.getAudioTracks().length > 0,
|
has_audio: stream.getAudioTracks().length > 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Proactively send offers to all connected peers so they
|
||||||
|
// receive the stream immediately. Without this, we rely on
|
||||||
|
// a request_stream round-trip that can silently fail because
|
||||||
|
// handleSignal is async but never awaited by ws.onmessage.
|
||||||
|
for (const [peerId] of state.peers) {
|
||||||
|
sendOfferTo(peerId);
|
||||||
|
}
|
||||||
|
|
||||||
updateUI();
|
updateUI();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error starting screen share:', err);
|
console.error('Error starting screen share:', err);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue