Skip to content

Commit

Permalink
Fix lost audio track on safari
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Müller <marcel-mueller@gmx.de>
  • Loading branch information
SystemKeeper committed Nov 25, 2023
1 parent ae82dc0 commit b176cea
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/utils/webrtc/simplewebrtc/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit b176cea

Please sign in to comment.