What are your media track handling tips?

In preparing my latest track handling blog post I ran into so many gotchas and caveats to consider (especially in Safari), that I'm fully expecting to evolve my approach (and likely the post) as time goes on. Do you have any tips for juggling media tracks on DOM elements? A few I'd point out are:
* Be sure to set media DOM element `srcObject` to `null` on disposal to avoid leaky WebMediaPlayers
* Avoid re-setting tracks more than necessary (eg when they haven't actually changed)
* Account for `"NotAllowedError"` blocking autoplay, and present a button to the user that allows them to click a button to play media manually if they encounter this
* Prepare a big cup of coffee and a bowl for your tears when testing your implementation in Safari
Comments
-
Legitimate question: does
srcObject
not auto update if you switch around tracks on theMediaStream
you assign tosrcObject
? Other than the cleanup you mention (which is super smart and true), I generally set-and-forgetsrcObject
and do track manipulation on theMediaStream
. The trade-off there is I'm also often wrangling a collection ofMediaStream
objects that are available on the global namespace.2 -
@karl I'm not 100% sure what behavior you include in "auto update" past updating the added/removed tracks on the stream, but that is the recommendation we went with in the post: just switching around tracks and not resetting `srcObject` each time, aside from setting it to
null
for teardown. Which I think is also what you're describing, or am I confused (I am on my first coffee :D)0 -
@Lazer yes, that's it :) so someplace you'd have, in pseudo-code,
videoEl.srcObject = myMediaStream;
just the once. And you'd then just add, replace, or remove tracks onmyMediaStream
, meaning there is no need to do any subsequent reassignment tosrcObject
to the same media stream.0