Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Stop audio monitors when all audio tracks end
Until now the audio monitors were stopped when the source stream from
which the audio monitor stream was cloned was removed. Now the audio
monitors are stopped as soon as all* the audio tracks in the audio
monitor stream ends, which will make possible to set an audio monitor
again on a previously used stream from which all the audio tracks, but
not the video tracks, were removed.

*As internally the audio monitor uses a MediaStreamAudioSourceNode
actually only the audio track with the lower id will be used. But for
simplicity, and given that only a single audio track is expected anyway,
all the audio tracks in the audio monitor stream are checked.

Signed-off-by: Daniel Calviño Sánchez <[email protected]>
  • Loading branch information
danxuliu authored and nickvergessen committed Aug 21, 2020
commit 22cbf56bc87072768b6d0fe632fab56d0b5c492b
17 changes: 16 additions & 1 deletion src/utils/webrtc/simplewebrtc/localmedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ function isAllTracksEnded(stream) {
return isAllTracksEnded
}

function isAllAudioTracksEnded(stream) {
let isAllAudioTracksEnded = true
stream.getAudioTracks().forEach(function(t) {
isAllAudioTracksEnded = t.readyState === 'ended' && isAllAudioTracksEnded
})
return isAllAudioTracksEnded
}

function LocalMedia(opts) {
WildEmitter.call(this)

Expand Down Expand Up @@ -335,7 +343,6 @@ LocalMedia.prototype._removeStream = function(stream) {
let idx = this.localStreams.indexOf(stream)
if (idx > -1) {
this.localStreams.splice(idx, 1)
this._stopAudioMonitor(this._audioMonitorStreams[idx])
this._audioMonitorStreams.splice(idx, 1)
this.emit('localStreamStopped', stream)
} else {
Expand All @@ -353,6 +360,14 @@ LocalMedia.prototype._setupAudioMonitor = function(stream, harkOptions) {
const self = this
let timeout

stream.getAudioTracks().forEach(function(track) {
track.addEventListener('ended', function() {
if (isAllAudioTracksEnded(stream)) {
self._stopAudioMonitor(stream)
}
})
})

audio.on('speaking', function() {
self._speaking = true

Expand Down