From b176cea7269db9d7f5023c561d0fd63b9b99ad84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Sat, 25 Nov 2023 22:06:29 +0100 Subject: [PATCH] Fix lost audio track on safari MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Müller --- src/utils/webrtc/simplewebrtc/peer.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils/webrtc/simplewebrtc/peer.js b/src/utils/webrtc/simplewebrtc/peer.js index 7c2f50a2f813..c99fa1667b36 100644 --- a/src/utils/webrtc/simplewebrtc/peer.js +++ b/src/utils/webrtc/simplewebrtc/peer.js @@ -3,6 +3,7 @@ import util from 'util' import adapter from 'webrtc-adapter' import webrtcSupport from 'webrtcsupport' import WildEmitter from 'wildemitter' +import UAParser from 'ua-parser-js' /** * @param {object} stream the stream object. @@ -42,6 +43,10 @@ function Peer(options) { this._processPendingReplaceTracksPromise = null this._initialStreamSetup = false this.sid = options.sid || Date.now().toString() + + const browserName = (new UAParser()).getBrowser().name + this.isSafari = browserName === 'Safari' || browserName === 'Mobile Safari' + this.pc = new RTCPeerConnection(this.parent.config.peerConnectionConfig) this.pc.addEventListener('icecandidate', this.onIceCandidate.bind(this)) this.pc.addEventListener('endofcandidates', function(event) { @@ -831,7 +836,10 @@ Peer.prototype._replaceTrack = async function(newTrack, oldTrack, stream) { return } - if (sender.track && newTrack && !newTrack.enabled) { + // When replacing with a null track on Safari the reference to the track is + // lost (setting trackDisabled is not enough). Therefore we keep the track + // intact and don't replace it with a null track. + if (sender.track && newTrack && !newTrack.enabled && !this.isSafari) { // Replace with a null track to stop the sender. newTrack = null } @@ -871,6 +879,8 @@ Peer.prototype.handleSentTrackEnabledChanged = function(track, stream) { this.handleSentTrackReplacedBound(track, track, stream) } else if (!track.enabled && sender) { this.handleSentTrackReplacedBound(track, track, stream) + } else if (!sender && !stoppedSender) { + console.error('No sender found to handle localTrackEnabledChanged', track, stream) } }