trtc-electron-sdk 10.3.402 → 10.3.403

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ import { TRTCVideoFillMode } from "../trtc_define";
2
+ import { TFrameData } from './types';
3
+ export default interface IRenderer {
4
+ drawFrame(frameData: TFrameData): void;
5
+ setContentMode(mode: TRTCVideoFillMode): void;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,25 @@
1
+ import { TRTCVideoFillMode, TRTCVideoPixelFormat } from "../../trtc_define";
2
+ import IRenderer from "../IRenderer";
3
+ import { TFrameData, TCanvasImageSourceStyleOptions } from "../types";
4
+ declare class VideoRenderer implements IRenderer {
5
+ private pixelFormat;
6
+ private pixelFormatStr;
7
+ private viewContainer;
8
+ private contentMode;
9
+ private video;
10
+ private viewWrapper;
11
+ private track;
12
+ private writer;
13
+ private cacheCanvasOptions;
14
+ constructor(pixelFormat: TRTCVideoPixelFormat, view: HTMLElement);
15
+ setContentMode(mode?: TRTCVideoFillMode): void;
16
+ private bind;
17
+ private unbind;
18
+ drawFrame(frameData: TFrameData): Promise<void>;
19
+ _drawFrameH264(frameData: TFrameData): void;
20
+ _drawFrameNonH264(frameData: TFrameData): Promise<void>;
21
+ _getPixelFormat(): string;
22
+ _updateVideoStyle(options: TCanvasImageSourceStyleOptions): void;
23
+ destroy(): void;
24
+ }
25
+ export default VideoRenderer;
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const lodash_isequal_1 = __importDefault(require("lodash.isequal"));
16
+ const trtc_define_1 = require("../../trtc_define");
17
+ const util_1 = require("../util");
18
+ class VideoRenderer {
19
+ constructor(pixelFormat, view) {
20
+ this.pixelFormat = pixelFormat;
21
+ this.pixelFormatStr = this._getPixelFormat();
22
+ this.viewContainer = view;
23
+ this.contentMode = trtc_define_1.TRTCVideoFillMode.TRTCVideoFillMode_Fit;
24
+ const viewWrapper = document.createElement('div');
25
+ viewWrapper.className = 'trtc-video';
26
+ Object.assign(viewWrapper.style, {
27
+ width: '100%',
28
+ height: '100%',
29
+ display: 'flex',
30
+ justifyContent: 'center',
31
+ alignItems: 'center'
32
+ });
33
+ this.viewWrapper = viewWrapper;
34
+ this.track = new window.MediaStreamTrackGenerator({ kind: 'video' });
35
+ this.writer = this.track.writable.getWriter();
36
+ this.video = document.createElement('video');
37
+ this.video.setAttribute('style', 'display: block;');
38
+ this.video.setAttribute('autoplay', 'autoplay');
39
+ this.video.muted = true;
40
+ this.video.srcObject = new MediaStream([this.track]);
41
+ this.viewWrapper.appendChild(this.video);
42
+ this.cacheCanvasOptions = {};
43
+ this.bind(view);
44
+ }
45
+ setContentMode(mode = trtc_define_1.TRTCVideoFillMode.TRTCVideoFillMode_Fit) {
46
+ this.contentMode = mode;
47
+ }
48
+ bind(viewContainer) {
49
+ this.viewContainer = viewContainer;
50
+ if (this.viewWrapper) {
51
+ this.viewContainer.appendChild(this.viewWrapper);
52
+ }
53
+ }
54
+ unbind() {
55
+ this.viewWrapper && this.viewContainer && this.viewContainer.removeChild(this.viewWrapper);
56
+ this.viewContainer = null;
57
+ }
58
+ drawFrame(frameData) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ switch (this.pixelFormat) {
61
+ case trtc_define_1.TRTCVideoPixelFormat.TRTCVideoPixelFormat_I420:
62
+ case trtc_define_1.TRTCVideoPixelFormat.TRTCVideoPixelFormat_RGBA32:
63
+ case trtc_define_1.TRTCVideoPixelFormat.TRTCVideoPixelFormat_BGRA32:
64
+ yield this._drawFrameNonH264(frameData);
65
+ break;
66
+ case trtc_define_1.TRTCVideoPixelFormat.TRTCVideoPixelFormat_H264:
67
+ yield this._drawFrameH264(frameData);
68
+ break;
69
+ default:
70
+ console.warn('trtc:VideoRenderer.drawFrame error. Not support video format:', this.pixelFormat);
71
+ }
72
+ });
73
+ }
74
+ _drawFrameH264(frameData) {
75
+ // To-do
76
+ console.warn('trtc:H264 renderer not supported right now');
77
+ }
78
+ _drawFrameNonH264(frameData) {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ try {
81
+ const { data, width, height, timestamp, rotation, isNeedRotate } = frameData;
82
+ if (width <= 0 || height <= 0) {
83
+ throw new Error(`wrong video parameter width:${width} or height:${height}`);
84
+ }
85
+ if (this.viewWrapper && this.writer) {
86
+ this._updateVideoStyle({
87
+ contentWidth: width,
88
+ contentHeight: height,
89
+ containerWidth: this.viewWrapper.clientWidth,
90
+ containerHeight: this.viewWrapper.clientHeight,
91
+ contentMode: this.contentMode,
92
+ rotation,
93
+ isNeedRotate,
94
+ isNeedMirror: false,
95
+ });
96
+ const vf = new window.VideoFrame(frameData.data, {
97
+ format: this.pixelFormatStr,
98
+ codedWidth: frameData.width,
99
+ codedHeight: frameData.height,
100
+ timestamp: frameData.timestamp,
101
+ });
102
+ yield this.writer.write(vf);
103
+ }
104
+ else {
105
+ throw new Error('renderer container element is null');
106
+ }
107
+ }
108
+ catch (err) {
109
+ console.error('trtc:VideoRenderer._drawFrameH264 error:', err);
110
+ }
111
+ });
112
+ }
113
+ _getPixelFormat() {
114
+ switch (this.pixelFormat) {
115
+ case trtc_define_1.TRTCVideoPixelFormat.TRTCVideoPixelFormat_I420:
116
+ return 'I420';
117
+ case trtc_define_1.TRTCVideoPixelFormat.TRTCVideoPixelFormat_RGBA32:
118
+ return 'RGBA';
119
+ case trtc_define_1.TRTCVideoPixelFormat.TRTCVideoPixelFormat_BGRA32:
120
+ return 'BGRA';
121
+ default:
122
+ return '';
123
+ }
124
+ }
125
+ _updateVideoStyle(options) {
126
+ if (this.video) {
127
+ if ((0, lodash_isequal_1.default)(this.cacheCanvasOptions, options)) {
128
+ return;
129
+ }
130
+ this.cacheCanvasOptions = Object.assign({}, options);
131
+ const style = (0, util_1.calcVideoStyle)(options);
132
+ Object.keys(style).forEach(key => {
133
+ // @ts-ignore
134
+ this.video.style[key] = style[key];
135
+ });
136
+ }
137
+ else {
138
+ throw new Error('video element not exist');
139
+ }
140
+ }
141
+ destroy() {
142
+ this.unbind();
143
+ this.viewWrapper = null;
144
+ this.video = null;
145
+ this.cacheCanvasOptions = null;
146
+ }
147
+ }
148
+ exports.default = VideoRenderer;
@@ -0,0 +1,3 @@
1
+ import { TRTCVideoPixelFormat } from '../trtc_define';
2
+ import IRenderer from './IRenderer';
3
+ export declare function createRenderer(pixelFormat: TRTCVideoPixelFormat, view: HTMLElement): IRenderer;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createRenderer = void 0;
7
+ const Canvas3DRenderer_1 = __importDefault(require("./Canvas3DRenderer"));
8
+ const Canvas2DRenderer_1 = __importDefault(require("./Canvas2DRenderer"));
9
+ const VideoRenderer_1 = __importDefault(require("./VideoRenderer"));
10
+ const util_1 = require("./util");
11
+ const isWebGLSupported = (0, util_1.isSupportWebGL)();
12
+ function createRenderer(pixelFormat, view) {
13
+ let renderer = null;
14
+ if (isWebGLSupported) {
15
+ try {
16
+ renderer = new Canvas3DRenderer_1.default(pixelFormat, view);
17
+ console.log('trtc: created WebGL render');
18
+ }
19
+ catch (err) {
20
+ console.log('trtc: create WebGL renderer error.', err.message);
21
+ renderer = null;
22
+ }
23
+ }
24
+ if (!renderer) {
25
+ // @ts-ignore
26
+ if (window.VideoFrame && window.VideoDecoder) {
27
+ // Electron 15+ 版本支持 WebCodec API
28
+ renderer = new VideoRenderer_1.default(pixelFormat, view);
29
+ console.log('trtc: created Video render');
30
+ }
31
+ else {
32
+ renderer = new Canvas2DRenderer_1.default(pixelFormat, view);
33
+ console.log('trtc: created Canvas 2D render');
34
+ }
35
+ }
36
+ return renderer;
37
+ }
38
+ exports.createRenderer = createRenderer;
@@ -0,0 +1,19 @@
1
+ import { TRTCVideoFillMode } from "../trtc_define";
2
+ export declare type TFrameData = {
3
+ data: Uint8Array | ArrayBuffer;
4
+ width: number;
5
+ height: number;
6
+ timestamp: number;
7
+ rotation: number;
8
+ isNeedRotate: boolean;
9
+ };
10
+ export declare type TCanvasImageSourceStyleOptions = {
11
+ contentWidth: number;
12
+ contentHeight: number;
13
+ containerWidth: number;
14
+ containerHeight: number;
15
+ contentMode: TRTCVideoFillMode;
16
+ isNeedMirror: boolean;
17
+ isNeedRotate: boolean;
18
+ rotation: number;
19
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,52 @@
1
+ import { TRTCVideoStreamType } from "../trtc_define";
2
+ /**
3
+ * 配置对象, 可用来打开 debug 模式
4
+ *
5
+ * @example
6
+ * // 使用示例代码:
7
+ * const TRTCCloud = require('trtc-electron-sdk');
8
+ * this.rtcCloud = new TRTCCloud();
9
+ * this.rtcCloud.getConfigObject().setDebugMode(true);
10
+ *
11
+ */
12
+ export declare class Config {
13
+ private debugMode;
14
+ constructor();
15
+ setDebugMode(enable: boolean): void;
16
+ getDebugMode(): boolean;
17
+ }
18
+ export declare function isNeedKeepWidthAndHeight(rotation: number, isNeedRotate: boolean): boolean;
19
+ export declare function calcScaleRate(vertical: boolean, contentMode: number, contentWidth: number, contentHeight: number, containerWidth: number, containerHeight: number): number;
20
+ export declare function calcCanvasStyle(options?: {
21
+ contentWidth: number;
22
+ contentHeight: number;
23
+ rotation: number;
24
+ isNeedMirror: boolean;
25
+ contentMode: number;
26
+ containerWidth: number;
27
+ containerHeight: number;
28
+ isNeedRotate: boolean;
29
+ }): {
30
+ [x: string]: any;
31
+ };
32
+ export declare function calcVideoStyle(options?: {
33
+ contentWidth: number;
34
+ contentHeight: number;
35
+ rotation: number;
36
+ isNeedMirror: boolean;
37
+ contentMode: number;
38
+ containerWidth: number;
39
+ containerHeight: number;
40
+ isNeedRotate: boolean;
41
+ }): {
42
+ [x: string]: any;
43
+ };
44
+ export declare const config: Config;
45
+ export declare function allocBuffer(length: number, userId?: string, streamType?: TRTCVideoStreamType): Uint8Array | null;
46
+ export declare function transferBGRA2RGBA(data: Uint8Array | ArrayBuffer, width: number, height: number): Uint8Array | ArrayBuffer;
47
+ /**
48
+ * 判断是否支持webGL
49
+ * @returns {boolean}
50
+ * @private
51
+ */
52
+ export declare function isSupportWebGL(): boolean;
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isSupportWebGL = exports.transferBGRA2RGBA = exports.allocBuffer = exports.config = exports.calcVideoStyle = exports.calcCanvasStyle = exports.calcScaleRate = exports.isNeedKeepWidthAndHeight = exports.Config = void 0;
4
+ /**
5
+ * 配置对象, 可用来打开 debug 模式
6
+ *
7
+ * @example
8
+ * // 使用示例代码:
9
+ * const TRTCCloud = require('trtc-electron-sdk');
10
+ * this.rtcCloud = new TRTCCloud();
11
+ * this.rtcCloud.getConfigObject().setDebugMode(true);
12
+ *
13
+ */
14
+ class Config {
15
+ constructor() {
16
+ this.debugMode = false;
17
+ }
18
+ setDebugMode(enable) {
19
+ this.debugMode = enable;
20
+ }
21
+ getDebugMode() {
22
+ return this.debugMode;
23
+ }
24
+ }
25
+ exports.Config = Config;
26
+ function isNeedKeepWidthAndHeight(rotation, isNeedRotate) {
27
+ // 需保持宽高不做变化
28
+ return rotation === 0 || rotation === 180 || !isNeedRotate;
29
+ }
30
+ exports.isNeedKeepWidthAndHeight = isNeedKeepWidthAndHeight;
31
+ function calcScaleRate(vertical = false, contentMode = 0, contentWidth, contentHeight, containerWidth, containerHeight) {
32
+ const containerRatio = containerWidth / containerHeight;
33
+ const contentRatio = contentWidth / contentHeight;
34
+ if (isNaN(containerRatio) || isNaN(contentRatio)) {
35
+ return 1;
36
+ }
37
+ if (!contentMode) {
38
+ if (vertical) {
39
+ return containerRatio > contentRatio
40
+ ? containerHeight / contentHeight : containerWidth / contentWidth;
41
+ }
42
+ else {
43
+ return containerRatio < contentRatio
44
+ ? containerHeight / contentHeight : containerWidth / contentWidth;
45
+ }
46
+ }
47
+ else {
48
+ if (vertical) {
49
+ return containerRatio < contentRatio
50
+ ? containerHeight / contentHeight : containerWidth / contentWidth;
51
+ }
52
+ else {
53
+ return containerRatio > contentRatio
54
+ ? containerHeight / contentHeight : containerWidth / contentWidth;
55
+ }
56
+ }
57
+ }
58
+ exports.calcScaleRate = calcScaleRate;
59
+ function calcCanvasStyle(options = {
60
+ contentWidth: 0,
61
+ contentHeight: 0,
62
+ rotation: 0,
63
+ isNeedMirror: false,
64
+ contentMode: 0,
65
+ containerWidth: 0,
66
+ containerHeight: 0,
67
+ isNeedRotate: true
68
+ }) {
69
+ if ([0, 90, 180, 270].indexOf(options.rotation) === -1) {
70
+ throw new Error('Invalid value for rotation. Only support 0, 90, 180, 270');
71
+ }
72
+ const style = {
73
+ width: 0,
74
+ height: 0,
75
+ transform: ''
76
+ };
77
+ const isKeepWidthHeight = isNeedKeepWidthAndHeight(options.rotation, options.isNeedRotate);
78
+ if (isKeepWidthHeight) {
79
+ style.width = options.contentWidth;
80
+ style.height = options.contentHeight;
81
+ }
82
+ else {
83
+ style.height = options.contentWidth;
84
+ style.width = options.contentHeight;
85
+ }
86
+ const scale = calcScaleRate(!isKeepWidthHeight, options.contentMode, style.width, style.height, options.containerWidth, options.containerHeight);
87
+ style.transform = `scale(${scale})`;
88
+ if (options.isNeedMirror) {
89
+ style.transform = `${style.transform} rotateY(180deg)`;
90
+ }
91
+ return style;
92
+ }
93
+ exports.calcCanvasStyle = calcCanvasStyle;
94
+ function calcVideoStyle(options = {
95
+ contentWidth: 0,
96
+ contentHeight: 0,
97
+ rotation: 0,
98
+ isNeedMirror: false,
99
+ contentMode: 0,
100
+ containerWidth: 0,
101
+ containerHeight: 0,
102
+ isNeedRotate: true
103
+ }) {
104
+ if ([0, 90, 180, 270].indexOf(options.rotation) === -1) {
105
+ throw new Error('Invalid value for rotation. Only support 0, 90, 180, 270');
106
+ }
107
+ let width = 0;
108
+ let height = 0;
109
+ const isKeepWidthHeight = isNeedKeepWidthAndHeight(options.rotation, options.isNeedRotate);
110
+ if (isKeepWidthHeight) {
111
+ width = options.contentWidth;
112
+ height = options.contentHeight;
113
+ }
114
+ else {
115
+ height = options.contentWidth;
116
+ width = options.contentHeight;
117
+ }
118
+ const scale = calcScaleRate(!isKeepWidthHeight, options.contentMode, width, height, options.containerWidth, options.containerHeight);
119
+ const style = {
120
+ width: `${width * scale}px`,
121
+ height: `${height * scale}px`,
122
+ };
123
+ if (options.isNeedMirror) {
124
+ style.transform = 'rotateY(180deg)';
125
+ }
126
+ return style;
127
+ }
128
+ exports.calcVideoStyle = calcVideoStyle;
129
+ exports.config = new Config();
130
+ function allocBuffer(length, userId, streamType) {
131
+ console.debug('trtc:alloc buffer:', length, userId, streamType);
132
+ if (length >= 1) {
133
+ return Buffer.alloc(length);
134
+ }
135
+ return null;
136
+ }
137
+ exports.allocBuffer = allocBuffer;
138
+ function transferBGRA2RGBA(data, width, height) {
139
+ const bgraData = new Uint8ClampedArray((data.buffer || data).slice(0));
140
+ const rgbaData = new Uint8ClampedArray((data.buffer || data).slice(0));
141
+ // 将 BGRA8888 转成 RGBA8888
142
+ for (let i = 0; i < width * height; i++) {
143
+ rgbaData[4 * i] = bgraData[4 * i + 2];
144
+ rgbaData[4 * i + 1] = bgraData[4 * i + 1];
145
+ rgbaData[4 * i + 2] = bgraData[4 * i];
146
+ rgbaData[4 * i + 3] = bgraData[4 * i + 3];
147
+ }
148
+ return rgbaData;
149
+ }
150
+ exports.transferBGRA2RGBA = transferBGRA2RGBA;
151
+ /**
152
+ * 判断是否支持webGL
153
+ * @returns {boolean}
154
+ * @private
155
+ */
156
+ function isSupportWebGL() {
157
+ const canvas = document.createElement('canvas');
158
+ let gl;
159
+ canvas.width = 1;
160
+ canvas.height = 1;
161
+ const options = {
162
+ alpha: false,
163
+ depth: false,
164
+ stencil: false,
165
+ antialias: false,
166
+ preferLowPowerToHighPerformance: true
167
+ };
168
+ try {
169
+ gl = canvas.getContext('webgl', options) || canvas.getContext('experimental-webgl', options);
170
+ }
171
+ catch (e) {
172
+ return false;
173
+ }
174
+ if (gl) {
175
+ return true;
176
+ }
177
+ else {
178
+ return false;
179
+ }
180
+ }
181
+ exports.isSupportWebGL = isSupportWebGL;
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Represents metadata about a YUV frame format.
3
+ * @typedef {Object} YUVFormat
4
+ * @property {number} width - width of encoded frame in luma pixels
5
+ * @property {number} height - height of encoded frame in luma pixels
6
+ * @property {number} chromaWidth - width of encoded frame in chroma pixels
7
+ * @property {number} chromaHeight - height of encoded frame in chroma pixels
8
+ * @property {number} cropLeft - upper-left X coordinate of visible crop region, in luma pixels
9
+ * @property {number} cropTop - upper-left Y coordinate of visible crop region, in luma pixels
10
+ * @property {number} cropWidth - width of visible crop region, in luma pixels
11
+ * @property {number} cropHeight - height of visible crop region, in luma pixels
12
+ * @property {number} displayWidth - final display width of visible region, in luma pixels
13
+ * @property {number} displayHeight - final display height of visible region, in luma pixels
14
+ */
15
+ export interface YUVFormat {
16
+ width: number;
17
+ height: number;
18
+ chromaWidth: number;
19
+ chromaHeight: number;
20
+ cropLeft?: number;
21
+ cropTop?: number;
22
+ cropWidth?: number;
23
+ cropHeight?: number;
24
+ displayWidth?: number;
25
+ displayHeight?: number;
26
+ }
27
+ /**
28
+ * Represents underlying image data for a single luma or chroma plane.
29
+ * Cannot be interpreted without the format data from a frame buffer.
30
+ * @typedef {Object} YUVPlane
31
+ * @property {Uint8Array} bytes - typed array containing image data bytes
32
+ * @property {number} stride - byte distance between rows in data
33
+ */
34
+ export interface YUVPlane {
35
+ bytes: Uint8Array;
36
+ stride: number;
37
+ }
38
+ /**
39
+ * Represents a YUV image frame buffer, with enough format information
40
+ * to interpret the data usefully. Buffer objects use generic objects
41
+ * under the hood and can be transferred between worker threads using
42
+ * the structured clone algorithm.
43
+ *
44
+ * @typedef {Object} YUVFrame
45
+ * @property {YUVFormat} format
46
+ * @property {YUVPlane} y
47
+ * @property {YUVPlane} u
48
+ * @property {YUVPlane} v
49
+ */
50
+ export interface YUVFrame {
51
+ format: YUVFormat;
52
+ y: YUVPlane;
53
+ u: YUVPlane;
54
+ v: YUVPlane;
55
+ }
56
+ /**
57
+ * Represents metadata about a YUV frame format.
58
+ * @typedef {Object} YUVFormat
59
+ * @property {number} width - width of encoded frame in luma pixels
60
+ * @property {number} height - height of encoded frame in luma pixels
61
+ * @property {number} chromaWidth - width of encoded frame in chroma pixels
62
+ * @property {number} chromaHeight - height of encoded frame in chroma pixels
63
+ * @property {number} cropLeft - upper-left X coordinate of visible crop region, in luma pixels
64
+ * @property {number} cropTop - upper-left Y coordinate of visible crop region, in luma pixels
65
+ * @property {number} cropWidth - width of visible crop region, in luma pixels
66
+ * @property {number} cropHeight - height of visible crop region, in luma pixels
67
+ * @property {number} displayWidth - final display width of visible region, in luma pixels
68
+ * @property {number} displayHeight - final display height of visible region, in luma pixels
69
+ */
70
+ /**
71
+ * Represents underlying image data for a single luma or chroma plane.
72
+ * Cannot be interpreted without the format data from a frame buffer.
73
+ * @typedef {Object} YUVPlane
74
+ * @property {Uint8Array} bytes - typed array containing image data bytes
75
+ * @property {number} stride - byte distance between rows in data
76
+ */
77
+ /**
78
+ * Represents a YUV image frame buffer, with enough format information
79
+ * to interpret the data usefully. Buffer objects use generic objects
80
+ * under the hood and can be transferred between worker threads using
81
+ * the structured clone algorithm.
82
+ *
83
+ * @typedef {Object} YUVFrame
84
+ * @property {YUVFormat} format
85
+ * @property {YUVPlane} y
86
+ * @property {YUVPlane} u
87
+ * @property {YUVPlane} v
88
+ */
89
+ /**
90
+ * Holder namespace for utility functions and constants related to
91
+ * YUV frame and plane buffers.
92
+ *
93
+ * @namespace
94
+ */
95
+ declare var YUVBuffer: {
96
+ /**
97
+ * Validate a plane dimension
98
+ * @param {number} dim - vertical or horizontal dimension
99
+ * @throws exception on zero, negative, or non-integer value
100
+ */
101
+ validateDimension: (dim: number) => void;
102
+ /**
103
+ * Validate a plane offset
104
+ * @param {number} dim - vertical or horizontal dimension
105
+ * @throws exception on negative or non-integer value
106
+ */
107
+ validateOffset: (dim: number) => void;
108
+ /**
109
+ * Validate and fill out a YUVFormat object structure.
110
+ *
111
+ * At least width and height fields are required; other fields will be
112
+ * derived if left missing or empty:
113
+ * - chromaWidth and chromaHeight will be copied from width and height as for a 4:4:4 layout
114
+ * - cropLeft and cropTop will be 0
115
+ * - cropWidth and cropHeight will be set to whatever of the frame is visible after cropTop and cropLeft are applied
116
+ * - displayWidth and displayHeight will be set to cropWidth and cropHeight.
117
+ *
118
+ * @param {YUVFormat} fields - input fields, must include width and height.
119
+ * @returns {YUVFormat} - validated structure, with all derivable fields filled out.
120
+ * @throws exception on invalid fields or missing width/height
121
+ */
122
+ format: (fields: YUVFormat) => YUVFormat;
123
+ /**
124
+ * Pick a suitable stride for a custom-allocated thingy
125
+ * @param {number} width - width in bytes
126
+ * @returns {number} - new width in bytes at least as large
127
+ * @throws exception on invalid input width
128
+ */
129
+ suitableStride: (width: number) => number;
130
+ /**
131
+ * Allocate or extract a YUVPlane object from given dimensions/source.
132
+ * @param {number} width - width in pixels
133
+ * @param {number} height - height in pixels
134
+ * @param {Uint8Array} source - input byte array; optional (will create empty buffer if missing)
135
+ * @param {number} stride - row length in bytes; optional (will create a default if missing)
136
+ * @param {number} offset - offset into source array to extract; optional (will start at 0 if missing)
137
+ * @returns {YUVPlane} - freshly allocated planar buffer
138
+ */
139
+ allocPlane: (width: number, height: number, source?: Uint8Array, stride?: number, offset?: number) => YUVPlane;
140
+ /**
141
+ * Allocate a new YUVPlane object big enough for a luma plane in the given format
142
+ * @param {YUVFormat} format - target frame format
143
+ * @param {Uint8Array} source - input byte array; optional (will create empty buffer if missing)
144
+ * @param {number} stride - row length in bytes; optional (will create a default if missing)
145
+ * @param {number} offset - offset into source array to extract; optional (will start at 0 if missing)
146
+ * @returns {YUVPlane} - freshly allocated planar buffer
147
+ */
148
+ lumaPlane: (format: YUVFormat, source?: Uint8Array, stride?: number, offset?: number) => YUVPlane;
149
+ /**
150
+ * Allocate a new YUVPlane object big enough for a chroma plane in the given format,
151
+ * optionally copying data from an existing buffer.
152
+ *
153
+ * @param {YUVFormat} format - target frame format
154
+ * @param {Uint8Array} source - input byte array; optional (will create empty buffer if missing)
155
+ * @param {number} stride - row length in bytes; optional (will create a default if missing)
156
+ * @param {number} offset - offset into source array to extract; optional (will start at 0 if missing)
157
+ * @returns {YUVPlane} - freshly allocated planar buffer
158
+ */
159
+ chromaPlane: (format: YUVFormat, source?: Uint8Array, stride?: number, offset?: number) => YUVPlane;
160
+ /**
161
+ * Allocate a new YUVFrame object big enough for the given format
162
+ * @param {YUVFormat} format - target frame format
163
+ * @param {YUVPlane} y - optional Y plane; if missing, fresh one will be allocated
164
+ * @param {YUVPlane} u - optional U plane; if missing, fresh one will be allocated
165
+ * @param {YUVPlane} v - optional V plane; if missing, fresh one will be allocated
166
+ * @returns {YUVFrame} - freshly allocated frame buffer
167
+ */
168
+ frame: (format: YUVFormat, y: YUVPlane, u: YUVPlane, v: YUVPlane) => YUVFrame;
169
+ /**
170
+ * Duplicate a plane using new buffer memory.
171
+ * @param {YUVPlane} plane - input plane to copy
172
+ * @returns {YUVPlane} - freshly allocated and filled planar buffer
173
+ */
174
+ copyPlane: (plane: YUVPlane) => YUVPlane;
175
+ /**
176
+ * Duplicate a frame using new buffer memory.
177
+ * @param {YUVFrame} frame - input frame to copyFrame
178
+ * @returns {YUVFrame} - freshly allocated and filled frame buffer
179
+ */
180
+ copyFrame: (frame: YUVFrame) => YUVFrame;
181
+ /**
182
+ * List the backing buffers for the frame's planes for transfer between
183
+ * threads via Worker.postMessage.
184
+ * @param {YUVFrame} frame - input frame
185
+ * @returns {Array} - list of transferable objects
186
+ */
187
+ transferables: (frame: YUVFrame) => (ArrayBuffer | SharedArrayBuffer)[];
188
+ };
189
+ export default YUVBuffer;