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
Dispatch "addtrack" and "removetrack" events in MediaStream shim
Neither Firefox nor Chromium dispatch "addtrack" and "removetrack"
events when tracks are added and removed to and from a MediaStream.
However, this is part of the specification, so it is expected that all
browsers implement that eventually. Due to that the shim checks whether
the events were dispatched or not in order to do it only when needed.

Signed-off-by: Daniel Calviño Sánchez <[email protected]>
  • Loading branch information
danxuliu authored and nickvergessen committed Aug 21, 2020
commit c8375c71db2751c331ce43348c404a3c85b3d6e0
34 changes: 34 additions & 0 deletions src/utils/webrtc/shims/MediaStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,40 @@
*/

if (window.MediaStream) {
const originalMediaStreamAddTrack = window.MediaStream.prototype.addTrack
window.MediaStream.prototype.addTrack = function(track) {
let addTrackEventDispatched = false
const testAddTrackEvent = () => {
addTrackEventDispatched = true
}
this.addEventListener('addtrack', testAddTrackEvent)

originalMediaStreamAddTrack.apply(this, arguments)

this.removeEventListener('addtrack', testAddTrackEvent)

if (!addTrackEventDispatched) {
this.dispatchEvent(new MediaStreamTrackEvent('addtrack', { track: track }))
}
}

const originalMediaStreamRemoveTrack = window.MediaStream.prototype.removeTrack
window.MediaStream.prototype.removeTrack = function(track) {
let removeTrackEventDispatched = false
const testRemoveTrackEvent = () => {
removeTrackEventDispatched = true
}
this.addEventListener('removetrack', testRemoveTrackEvent)

originalMediaStreamRemoveTrack.apply(this, arguments)

this.removeEventListener('removetrack', testRemoveTrackEvent)

if (!removeTrackEventDispatched) {
this.dispatchEvent(new MediaStreamTrackEvent('removetrack', { track: track }))
}
}

// Event implementations do not support advanced parameters like "options"
// or "useCapture".
const originalMediaStreamDispatchEvent = window.MediaStream.prototype.dispatchEvent
Expand Down