From 925cb393973e6d4f5a8a990e9686bbc511a712a4 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Fri, 19 Apr 2024 23:37:47 +0700 Subject: [PATCH] desktop: using system sound in screenshare (#4035) Co-authored-by: Evgeny Poberezkin --- .../commonMain/resources/assets/www/call.js | 22 ++++++++++++++++--- packages/simplex-chat-webrtc/src/call.ts | 17 ++++++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/resources/assets/www/call.js b/apps/multiplatform/common/src/commonMain/resources/assets/www/call.js index 9e7d29189f..218fe0c660 100644 --- a/apps/multiplatform/common/src/commonMain/resources/assets/www/call.js +++ b/apps/multiplatform/common/src/commonMain/resources/assets/www/call.js @@ -486,8 +486,22 @@ const processCommand = (function () { } return; } - for (const t of call.localStream.getTracks()) - t.stop(); + if (!call.screenShareEnabled) { + for (const t of call.localStream.getTracks()) + t.stop(); + } + else { + // Don't stop audio track if switching to screenshare + for (const t of call.localStream.getVideoTracks()) + t.stop(); + // Replace new track from screenshare with old track from recording device + for (const t of localStream.getAudioTracks()) { + t.stop(); + localStream.removeTrack(t); + } + for (const t of call.localStream.getAudioTracks()) + localStream.addTrack(t); + } call.localCamera = camera; const audioTracks = localStream.getAudioTracks(); const videoTracks = localStream.getVideoTracks(); @@ -547,7 +561,9 @@ const processCommand = (function () { //}, //aspectRatio: 1.33, }, - audio: true, + audio: false, + // This works with Chrome, Edge, Opera, but not with Firefox and Safari + // systemAudio: "include" }; return navigator.mediaDevices.getDisplayMedia(constraints); } diff --git a/packages/simplex-chat-webrtc/src/call.ts b/packages/simplex-chat-webrtc/src/call.ts index 41241e22c2..e7788ac723 100644 --- a/packages/simplex-chat-webrtc/src/call.ts +++ b/packages/simplex-chat-webrtc/src/call.ts @@ -689,7 +689,18 @@ const processCommand = (function () { } return } - for (const t of call.localStream.getTracks()) t.stop() + if (!call.screenShareEnabled) { + for (const t of call.localStream.getTracks()) t.stop() + } else { + // Don't stop audio track if switching to screenshare + for (const t of call.localStream.getVideoTracks()) t.stop() + // Replace new track from screenshare with old track from recording device + for (const t of localStream.getAudioTracks()) { + t.stop() + localStream.removeTrack(t) + } + for (const t of call.localStream.getAudioTracks()) localStream.addTrack(t) + } call.localCamera = camera const audioTracks = localStream.getAudioTracks() @@ -755,7 +766,9 @@ const processCommand = (function () { //}, //aspectRatio: 1.33, }, - audio: true, + audio: false, + // This works with Chrome, Edge, Opera, but not with Firefox and Safari + // systemAudio: "include" } return navigator.mediaDevices.getDisplayMedia(constraints) }