[whatwg] When to stop <video> elements from playing

Maciej Stachowiak wrote:
> 
> On Oct 18, 2007, at 3:28 PM, Robert O'Callahan wrote:
> 
>> Suppose a script creates a <video> element, adds it to the document, 
>> starts it playing, then removes the element from the document and 
>> drops all references to it. When should the element stop playing?
>> -- when the element leaves the document?-- when all JS references to 
>> the element have been dropped (and garbage collection runs)?
>> -- when the user leaves the page?
>> -- when the end of the stream is reached? (which could be "never")
> 
> I think it would be ok to do a one-time stop of a playing <video> and 
> <audio> element at the time it is removed from the document, but still 
> allow at least <audio> (and maybe <video> too for consistency, though it 
> seems gratuitous) to play play if is outside the document already when 
> it starts. In any case, like <img>, I think loading should be able to 
> proceed when outside the document or set to display: none.
> 
> Stopping at remove time ensures that sounds and videos end at a 
> deterministic time when parts of the document are replaced, say with 
> innerHTML. Without this, if you replace a piece of the document that may 
> contain <video> or <audio> elements you will have to search for them 
> first and make sure to stop them. However, if the element can still 
> start to play when outside the document, that addresses the use case for 
> programmatic audio for things like game sound effects.

Yeah, it makes a lot of sense to me to stop playing when a node is moved 
out of the document. I'd even say that it should never autostart based 
on its DOM attributes unless it is in the document. In other words, the 
only way you could get it to play when outside the document would be 
through an explicit call to play().

This does leave the problem of what happens if the element is garbage 
collected while it's playing sound. It'd be nice to hear what experience 
opera has here.

I would be a little hesitant to say that elements that play are never 
garbage collected. That would make it easy for the page author to 
accidentally leak <video>s without sound and muted-but-playing 
<audio>/<video>s for the duration of the page. This sucks up both CPU 
cycles and memory.

One alternative would be to simply garbage collect them, but put a 
warning in the JS console whenever a playing element is garbage 
collected, aiding debugging.

/ Jonas

Received on Friday, 19 October 2007 15:19:48 UTC