Allow transferring ArrayBuffer into VideoFrame, AudioData, EncodedVideoChunk, EncodedAudioChunk, ImageDecoder constructors
Currently when creating any of the VideoFrame
, AudioData
, EncodedVideoChunk
, EncodedAudioChunk
, ImageDecoder
objects
from corresponding *Init
objects constructors make a copy of ArrayBuffer
contents into an internal memory buffer.
In many cases the ArrayBuffer
is never used again for anything else after a WebCodecs object is created.
It means that this copy can be avoided by transfering (moving) ArrayBuffer
contents into the WebCodecs objects.
Remember, memcpy is murder.
Let's add a transfer
list to WebCodecs *Init
objects, that would allow developers to signal when they don't need
the ArrayBuffer
after the object is created. If the transfer
list contains the data ArrayBuffer
, the ArrayBuffer
is detached and
its contents are used in the WebCodecs objects without a copy. This is consistent with the approach taken by structuredClone and postMessage
dictionary VideoFrameBufferInit {
.......
sequence<ArrayBuffer> transfer = [];
};
dictionary ImageDecoderInit {
.....
sequence<ArrayBuffer> transfer = [];
};
dictionary EncodedVideoChunkInit {
.....
sequence<ArrayBuffer> transfer = [];
};
dictionary AudioDataInit {
.....
sequence<ArrayBuffer> transfer = [];
}
dictionary EncodedAudioChunkInit {
.....
sequence<ArrayBuffer> transfer = [];
}
let prototype_frame = new VideoFrame(canvas, { timestamp: 0 });
let size = prototype_frame.allocationSize();
let buf = new ArrayBuffer(size);
let layout = await prototype_frame.copyTo(buf);
let init = {
format: prototype_frame.format,
timestamp: prototype_frame.timestamp,
codedWidth: prototype_frame.codedWidth,
codedHeight: prototype_frame.codedHeight,
layout: layout,
transfer: [buf]
};
// |buf| is transfered here and becomes detached
let new_frame = new VideoFrame(buf, init);
- WebCodecs github issue: w3c/webcodecs#104
- Prototype CL for VideoFrame: https://chromium-review.googlesource.com/c/chromium/src/+/4529012
- w3c Media Working Group discussion: https://www.w3.org/2023/05/30-mediawg-minutes.html#t01