w3c / webcodecs

WebCodecs is a flexible web API for encoding and decoding audio and video.
https://w3c.github.io/webcodecs/
Other
990 stars 136 forks source link

Close and transfer (buffer stealing) #287

Open chcunningham opened 3 years ago

chcunningham commented 3 years ago

@padenot raised idea in editors call to frame.close() -> transfer cpu data immediately to ArrayBuffer. Tracking here to ponder it more.

dalecurtis commented 3 years ago

close() only closes that frame's handle on the frame. It may still be used elsewhere. Given the threading involved, I don't think there's any way to safely steal the buffer except in rare circumstances (RefPtr::HasOneRef() etc).

padenot commented 3 years ago

Yes, this is precisely the case that I mentioned, it can't work otherwise.

I expect this is in fact rather common: you decode, you get a frame, you read it back to apply some custom processing with wasm/js (this means a copy, for now). If this optimization isn't available, then the memory traffic is essentially doubled in the reasonably common case of a software decoder.

dalecurtis commented 3 years ago

I agree, it's a good idea if we can make it work in common scenarios. We may want a separate closeXXX() method or parameter which activates this though for clarity and to avoid always transferring to a GC'd type to avoid increased memory pressure.

That said, a HasOneRef() type solutions has some edges that need solving:

Probably more, but that's all I can think of at the moment.