-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathToCPUMemoryCopier.js
37 lines (32 loc) · 1.02 KB
/
ToCPUMemoryCopier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
/**
* Returns a transformer for a TransformStream that moves the underlying memory
* resources of the VideoFrame to CPU memory.
*/
function ToCPUMemoryCopier(config) {
// Worst case scenario is 4 bytes per pixel
const frameSize = config.width * config.height * 8;
const buffer = new Uint8Array(frameSize);
return {
/**
* Copy frame data to an ArrayBuffer and create a new VideoFrame based on
* that.
*/
async transform(frame, controller) {
// Copy frame bytes to the buffer
await frame.copyTo(buffer);
// Create new frame out of processed buffer
const processedFrame = new VideoFrame(buffer, {
format: frame.format,
codedWidth: frame.displayWidth,
codedHeight: frame.displayHeight,
timestamp: frame.timestamp,
duration: frame.duration
});
// Time to get rid of the incoming VideoFrame and to return the newly
// created one.
frame.close();
controller.enqueue(processedFrame);
}
};
}