@livekit/rtc-node 0.1.0 → 0.2.0

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.
Files changed (51) hide show
  1. package/dist/audio_stream.d.ts +4 -1
  2. package/dist/audio_stream.d.ts.map +1 -1
  3. package/dist/audio_stream.js +1 -1
  4. package/dist/audio_stream.js.map +1 -1
  5. package/dist/index.d.ts +4 -4
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +2 -2
  8. package/dist/index.js.map +1 -1
  9. package/dist/proto/audio_frame_pb.d.ts +0 -46
  10. package/dist/proto/audio_frame_pb.d.ts.map +1 -1
  11. package/dist/proto/audio_frame_pb.js +0 -70
  12. package/dist/proto/audio_frame_pb.js.map +1 -1
  13. package/dist/proto/ffi_pb.d.ts +31 -44
  14. package/dist/proto/ffi_pb.d.ts.map +1 -1
  15. package/dist/proto/ffi_pb.js +35 -10
  16. package/dist/proto/ffi_pb.js.map +1 -1
  17. package/dist/proto/room_pb.d.ts +5 -1
  18. package/dist/proto/room_pb.d.ts.map +1 -1
  19. package/dist/proto/room_pb.js +5 -0
  20. package/dist/proto/room_pb.js.map +1 -1
  21. package/dist/proto/video_frame_pb.d.ts +124 -390
  22. package/dist/proto/video_frame_pb.d.ts.map +1 -1
  23. package/dist/proto/video_frame_pb.js +149 -486
  24. package/dist/proto/video_frame_pb.js.map +1 -1
  25. package/dist/video_frame.d.ts +8 -103
  26. package/dist/video_frame.d.ts.map +1 -1
  27. package/dist/video_frame.js +157 -323
  28. package/dist/video_frame.js.map +1 -1
  29. package/dist/video_source.d.ts +2 -2
  30. package/dist/video_source.d.ts.map +1 -1
  31. package/dist/video_source.js +5 -10
  32. package/dist/video_source.js.map +1 -1
  33. package/dist/video_stream.d.ts +7 -2
  34. package/dist/video_stream.d.ts.map +1 -1
  35. package/dist/video_stream.js +5 -5
  36. package/dist/video_stream.js.map +1 -1
  37. package/package.json +6 -6
  38. package/src/audio_stream.ts +6 -2
  39. package/src/index.ts +3 -16
  40. package/src/proto/audio_frame_pb.ts +1 -91
  41. package/src/proto/e2ee_pb.ts +1 -1
  42. package/src/proto/ffi_pb.ts +55 -53
  43. package/src/proto/handle_pb.ts +1 -1
  44. package/src/proto/participant_pb.ts +1 -1
  45. package/src/proto/room_pb.ts +7 -1
  46. package/src/proto/stats_pb.ts +1 -1
  47. package/src/proto/track_pb.ts +1 -1
  48. package/src/proto/video_frame_pb.ts +186 -641
  49. package/src/video_frame.ts +181 -578
  50. package/src/video_source.ts +5 -9
  51. package/src/video_stream.ts +13 -6
@@ -2,623 +2,226 @@
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
 
5
- import { FfiClient, FfiHandle } from './ffi_client.js';
5
+ import { FfiClient, FfiHandle, FfiRequest } from './ffi_client.js';
6
6
  import {
7
- BiplanarYuvBufferInfo,
8
- OwnedVideoFrameBuffer,
9
- PlanarYuvBufferInfo,
10
- ToI420Request,
11
- ToI420Response,
12
- VideoFormatType,
13
- VideoFrameBufferInfo,
14
- VideoFrameBufferType,
15
- VideoRotation,
7
+ OwnedVideoBuffer,
8
+ VideoBufferInfo,
9
+ VideoBufferInfo_ComponentInfo,
10
+ VideoBufferType,
11
+ VideoConvertResponse,
16
12
  } from './proto/video_frame_pb.js';
17
13
 
18
14
  export class VideoFrame {
19
- timestampUs: number;
20
- rotation: VideoRotation;
21
- buffer: VideoFrameBuffer;
22
-
23
- constructor(timestampUs: number, rotation: VideoRotation, buffer: VideoFrameBuffer) {
24
- this.timestampUs = timestampUs;
25
- this.rotation = rotation;
26
- this.buffer = buffer;
27
- }
28
- }
29
-
30
- export abstract class VideoFrameBuffer {
31
15
  data: Uint8Array;
32
16
  width: number;
33
17
  height: number;
34
- bufferType: VideoFrameBufferType;
35
-
36
- constructor(data: Uint8Array, width: number, height: number, bufferType: VideoFrameBufferType) {
37
- this.data = data;
38
- this.width = width;
39
- this.height = height;
40
- this.bufferType = bufferType;
41
- }
42
-
43
- /** @internal */
44
- abstract protoInfo(): VideoFrameBufferInfo;
45
-
46
- /** @internal */
47
- static fromOwnedInfo(owned: OwnedVideoFrameBuffer): VideoFrameBuffer {
48
- let info = owned.info;
49
- switch (info.bufferType) {
50
- case VideoFrameBufferType.NATIVE:
51
- return NativeBuffer.fromOwnedInfo(owned);
52
- case VideoFrameBufferType.I420:
53
- return I420Buffer.fromOwnedInfo(owned);
54
- case VideoFrameBufferType.I420A:
55
- return I420ABuffer.fromOwnedInfo(owned);
56
- case VideoFrameBufferType.I422:
57
- return I422Buffer.fromOwnedInfo(owned);
58
- case VideoFrameBufferType.I444:
59
- return I444Buffer.fromOwnedInfo(owned);
60
- case VideoFrameBufferType.I010:
61
- return I010Buffer.fromOwnedInfo(owned);
62
- case VideoFrameBufferType.NV12:
63
- return NV12Buffer.fromOwnedInfo(owned);
64
- }
65
- }
66
- }
67
-
68
- export class NativeBuffer extends VideoFrameBuffer {
69
- private info: VideoFrameBufferInfo;
70
-
71
- constructor(ownedInfo: OwnedVideoFrameBuffer) {
72
- super(new Uint8Array(), ownedInfo.info.width, ownedInfo.info.height, ownedInfo.info.bufferType);
73
- this.info = ownedInfo.info;
74
- }
75
-
76
- /** @internal */
77
- protoInfo(): VideoFrameBufferInfo {
78
- return this.info;
79
- }
80
- }
81
-
82
- export abstract class PlanarYuvBuffer extends VideoFrameBuffer {
83
- strideY: number;
84
- strideU: number;
85
- strideV: number;
86
- chromaWidth: number;
87
- chromaHeight: number;
18
+ type: VideoBufferType;
19
+ dataPtr: bigint;
88
20
 
89
21
  constructor(
90
22
  data: Uint8Array,
91
23
  width: number,
92
24
  height: number,
93
- bufferType: VideoFrameBufferType,
94
- strideY: number,
95
- strideU: number,
96
- strideV: number,
97
- chromaWidth: number,
98
- chromaHeight: number,
25
+ type: VideoBufferType,
26
+ dataPtr: bigint,
99
27
  ) {
100
- super(data, width, height, bufferType);
101
- this.strideY = strideY;
102
- this.strideU = strideU;
103
- this.strideV = strideV;
104
- this.chromaWidth = chromaWidth;
105
- this.chromaHeight = chromaHeight;
28
+ this.data = data;
29
+ this.width = width;
30
+ this.height = height;
31
+ this.type = type;
32
+ this.dataPtr = dataPtr;
106
33
  }
107
34
 
108
35
  /** @internal */
109
- protoInfo(): VideoFrameBufferInfo {
110
- let info = new VideoFrameBufferInfo({
36
+ protoInfo(): VideoBufferInfo {
37
+ const info = new VideoBufferInfo({
111
38
  width: this.width,
112
39
  height: this.height,
113
- bufferType: this.bufferType,
114
- buffer: {
115
- case: 'yuv',
116
- value: new PlanarYuvBufferInfo({
117
- strideY: this.strideY,
118
- strideU: this.strideU,
119
- strideV: this.strideV,
120
- chromaWidth: this.chromaWidth,
121
- chromaHeight: this.chromaHeight,
122
- }),
123
- },
40
+ type: this.type,
41
+ dataPtr: this.dataPtr,
124
42
  });
125
- return info;
126
- }
127
- }
128
-
129
- export abstract class PlanarYuv8Buffer extends PlanarYuvBuffer {
130
- constructor(
131
- data: Uint8Array,
132
- width: number,
133
- height: number,
134
- bufferType: VideoFrameBufferType,
135
- strideY: number,
136
- strideU: number,
137
- strideV: number,
138
- chromaWidth: number,
139
- chromaHeight: number,
140
- ) {
141
- super(data, width, height, bufferType, strideY, strideU, strideV, chromaWidth, chromaHeight);
142
- }
143
-
144
- /** @internal */
145
- protoInfo(): VideoFrameBufferInfo {
146
- let info = super.protoInfo();
147
- let yuv = info.buffer.value as PlanarYuvBufferInfo;
148
- yuv.dataYPtr = FfiClient.instance.retrievePtr(this.dataY());
149
- yuv.dataUPtr = FfiClient.instance.retrievePtr(this.dataU());
150
- yuv.dataVPtr = FfiClient.instance.retrievePtr(this.dataV());
151
- return info;
152
- }
153
-
154
- dataY(): Uint8Array {
155
- return this.data.subarray(0, this.strideY * this.height);
156
- }
157
-
158
- dataU(): Uint8Array {
159
- return this.data.subarray(
160
- this.strideY * this.height,
161
- this.strideY * this.height + this.strideU * this.chromaHeight,
162
- );
163
- }
164
-
165
- dataV(): Uint8Array {
166
- return this.data.subarray(
167
- this.strideY * this.height + this.strideU * this.chromaHeight,
168
- this.strideY * this.height +
169
- this.strideU * this.chromaHeight +
170
- this.strideV * this.chromaHeight,
171
- );
172
- }
173
- }
174
43
 
175
- export abstract class PlanarYuv16Buffer extends PlanarYuvBuffer {
176
- constructor(
177
- data: Uint16Array,
178
- width: number,
179
- height: number,
180
- bufferType: VideoFrameBufferType,
181
- strideY: number,
182
- strideU: number,
183
- strideV: number,
184
- chromaWidth: number,
185
- chromaHeight: number,
186
- ) {
187
- super(
188
- new Uint8Array(data),
189
- width,
190
- height,
191
- bufferType,
192
- strideY,
193
- strideU,
194
- strideV,
195
- chromaWidth,
196
- chromaHeight,
197
- );
198
- }
199
-
200
- /** @internal */
201
- protoInfo(): VideoFrameBufferInfo {
202
- let info = super.protoInfo();
203
- let yuv = info.buffer.value as PlanarYuvBufferInfo;
204
- yuv.dataYPtr = FfiClient.instance.retrievePtr(new Uint8Array(this.dataY()));
205
- yuv.dataUPtr = FfiClient.instance.retrievePtr(new Uint8Array(this.dataU()));
206
- yuv.dataVPtr = FfiClient.instance.retrievePtr(new Uint8Array(this.dataV()));
207
- return info;
208
- }
209
-
210
- dataY(): Uint16Array {
211
- return new Uint16Array(this.data.subarray(0, this.strideY * this.height));
212
- }
213
-
214
- dataU(): Uint16Array {
215
- return new Uint16Array(
216
- this.data.subarray(
217
- this.strideY * this.height,
218
- this.strideY * this.height + this.strideU * this.chromaHeight,
219
- ),
220
- );
221
- }
222
-
223
- dataV(): Uint16Array {
224
- return new Uint16Array(
225
- this.data.subarray(
226
- this.strideY * this.height + this.strideU * this.chromaHeight,
227
- this.strideY * this.height +
228
- this.strideU * this.chromaHeight +
229
- this.strideV * this.chromaHeight,
230
- ),
231
- );
232
- }
233
- }
234
-
235
- export abstract class BiplanarYuv8Buffer extends VideoFrameBuffer {
236
- strideY: number;
237
- strideUV: number;
238
- chromaWidth: number;
239
- chromaHeight: number;
240
-
241
- constructor(
242
- data: Uint8Array,
243
- width: number,
244
- height: number,
245
- bufferType: VideoFrameBufferType,
246
- strideY: number,
247
- strideUV: number,
248
- chromaWidth: number,
249
- chromaHeight: number,
250
- ) {
251
- super(data, width, height, bufferType);
252
- this.strideY = strideY;
253
- this.strideUV = strideUV;
254
- this.chromaWidth = chromaWidth;
255
- this.chromaHeight = chromaHeight;
256
- }
44
+ switch (this.type) {
45
+ case VideoBufferType.ARGB:
46
+ case VideoBufferType.RGBA:
47
+ case VideoBufferType.ABGR:
48
+ case VideoBufferType.BGRA:
49
+ info.stride = this.width * 4;
50
+ case VideoBufferType.RGB24:
51
+ info.stride = this.width * 3;
52
+ }
257
53
 
258
- /** @internal */
259
- protoInfo(): VideoFrameBufferInfo {
260
- let info = new VideoFrameBufferInfo({
261
- width: this.width,
262
- height: this.height,
263
- bufferType: this.bufferType,
264
- buffer: {
265
- case: 'biYuv',
266
- value: new BiplanarYuvBufferInfo({
267
- strideY: this.strideY,
268
- strideUv: this.strideUV,
269
- chromaWidth: this.chromaWidth,
270
- chromaHeight: this.chromaHeight,
271
- }),
272
- },
273
- });
54
+ info.components.push(...getPlaneInfos(this.dataPtr, this.type, this.width, this.height));
274
55
 
275
56
  return info;
276
57
  }
277
58
 
278
- dataY(): Uint8Array {
279
- return this.data.subarray(0, this.strideY * this.height);
280
- }
281
-
282
- dataUV(): Uint8Array {
283
- return this.data.subarray(
284
- this.strideY * this.height,
285
- this.strideY * this.height + this.strideUV * this.chromaHeight,
286
- );
287
- }
288
- }
289
-
290
- export class I420Buffer extends PlanarYuv8Buffer {
291
- constructor(
292
- data: Uint8Array,
293
- width: number,
294
- height: number,
295
- strideY: number,
296
- strideU: number,
297
- strideV: number,
298
- ) {
299
- let chromaWidth = ~~((width + 1) / 2);
300
- let chromaHeight = ~~((height + 1) / 2);
301
- super(
302
- data,
303
- width,
304
- height,
305
- VideoFrameBufferType.I420,
306
- strideY,
307
- strideU,
308
- strideV,
309
- chromaWidth,
310
- chromaHeight,
311
- );
312
- }
313
-
314
59
  /** @internal */
315
- static fromOwnedInfo(owned: OwnedVideoFrameBuffer): I420Buffer {
60
+ static fromOwnedInfo(owned: OwnedVideoBuffer): VideoFrame {
316
61
  let info = owned.info;
317
- let yuv = info.buffer.value as PlanarYuvBufferInfo;
318
- let strideY = yuv.strideY;
319
- let strideU = yuv.strideU;
320
- let strideV = yuv.strideV;
321
- let nbytes = I420Buffer.calcDataSize(info.height, strideY, strideU, strideV);
322
- let data = FfiClient.instance.copyBuffer(yuv.dataYPtr, nbytes);
323
- new FfiHandle(owned.handle.id).dispose();
324
- return new I420Buffer(data, info.width, info.height, strideY, strideU, strideV);
325
- }
326
-
327
- static calcDataSize(height: number, strideY: number, strideU: number, strideV: number): number {
328
- let chromaHeight = ~~((height + 1) / 2);
329
- return strideY * height + strideU * chromaHeight + strideV * chromaHeight;
330
- }
331
-
332
- static create(width: number, height: number): I420Buffer {
333
- let strideY = width;
334
- let strideU = ~~((width + 1) / 2);
335
- let strideV = ~~((width + 1) / 2);
336
- let dataSize = I420Buffer.calcDataSize(height, strideY, strideU, strideV);
337
- let data = new Uint8Array(dataSize);
338
- return new I420Buffer(data, width, height, strideY, strideU, strideV);
339
- }
340
- }
341
-
342
- export class I420ABuffer extends PlanarYuv8Buffer {
343
- strideA: number;
344
-
345
- constructor(
346
- data: Uint8Array,
347
- width: number,
348
- height: number,
349
- strideY: number,
350
- strideU: number,
351
- strideV: number,
352
- strideA: number,
353
- ) {
354
- let chromaWidth = ~~((width + 1) / 2);
355
- let chromaHeight = ~~((height + 1) / 2);
356
- super(
357
- data,
358
- width,
359
- height,
360
- VideoFrameBufferType.I420A,
361
- strideY,
362
- strideU,
363
- strideV,
364
- chromaWidth,
365
- chromaHeight,
366
- );
367
- this.strideA = strideA;
368
- }
369
-
370
- /** @internal */
371
- static fromOwnedInfo(owned: OwnedVideoFrameBuffer): I420ABuffer {
372
- let info = owned.info;
373
- let yuv = info.buffer.value as PlanarYuvBufferInfo;
374
- let strideY = yuv.strideY;
375
- let strideU = yuv.strideU;
376
- let strideV = yuv.strideV;
377
- let strideA = yuv.strideA;
378
- let nbytes = I420ABuffer.calcDataSize(info.height, strideY, strideU, strideV, strideA);
379
- let data = FfiClient.instance.copyBuffer(yuv.dataYPtr, nbytes);
380
- new FfiHandle(owned.handle.id).dispose();
381
- return new I420ABuffer(data, info.width, info.height, strideY, strideU, strideV, strideA);
382
- }
383
-
384
- static calcDataSize(
385
- height: number,
386
- strideY: number,
387
- strideU: number,
388
- strideV: number,
389
- strideA: number,
390
- ): number {
391
- let chromaHeight = ~~((height + 1) / 2);
392
- return strideY * height + strideU * chromaHeight + strideV * chromaHeight + strideA * height;
393
- }
394
-
395
- dataA(): Uint8Array {
396
- return this.data.subarray(
397
- this.strideY * this.height +
398
- this.strideU * this.chromaHeight +
399
- this.strideV * this.chromaHeight,
400
- this.strideY * this.height +
401
- this.strideU * this.chromaHeight +
402
- this.strideV * this.chromaHeight +
403
- this.strideA * this.height,
404
- );
405
- }
406
- }
407
-
408
- export class I422Buffer extends PlanarYuv8Buffer {
409
- constructor(
410
- data: Uint8Array,
411
- width: number,
412
- height: number,
413
- strideY: number,
414
- strideU: number,
415
- strideV: number,
416
- ) {
417
- let chromaWidth = ~~((width + 1) / 2);
418
- let chromaHeight = height;
419
- super(
420
- data,
421
- width,
422
- height,
423
- VideoFrameBufferType.I422,
424
- strideY,
425
- strideU,
426
- strideV,
427
- chromaWidth,
428
- chromaHeight,
429
- );
430
- }
431
-
432
- /** @internal */
433
- static fromOwnedInfo(owned: OwnedVideoFrameBuffer): I422Buffer {
434
- let info = owned.info;
435
- let yuv = info.buffer.value as PlanarYuvBufferInfo;
436
- let strideY = yuv.strideY;
437
- let strideU = yuv.strideU;
438
- let strideV = yuv.strideV;
439
- let nbytes = I422Buffer.calcDataSize(info.height, strideY, strideU, strideV);
440
- let data = FfiClient.instance.copyBuffer(yuv.dataYPtr, nbytes);
441
- new FfiHandle(owned.handle.id).dispose();
442
- return new I422Buffer(data, info.width, info.height, strideY, strideU, strideV);
443
- }
444
-
445
- static calcDataSize(height: number, strideY: number, strideU: number, strideV: number): number {
446
- return strideY * height + strideU * height + strideV * height;
447
- }
448
- }
449
-
450
- export class I444Buffer extends PlanarYuv8Buffer {
451
- constructor(
452
- data: Uint8Array,
453
- width: number,
454
- height: number,
455
- strideY: number,
456
- strideU: number,
457
- strideV: number,
458
- ) {
459
- let chromaWidth = width;
460
- let chromaHeight = height;
461
- super(
462
- data,
463
- width,
464
- height,
465
- VideoFrameBufferType.I444,
466
- strideY,
467
- strideU,
468
- strideV,
469
- chromaWidth,
470
- chromaHeight,
471
- );
472
- }
473
-
474
- /** @internal */
475
- static fromOwnedInfo(owned: OwnedVideoFrameBuffer): I444Buffer {
476
- let info = owned.info;
477
- let yuv = info.buffer.value as PlanarYuvBufferInfo;
478
- let strideY = yuv.strideY;
479
- let strideU = yuv.strideU;
480
- let strideV = yuv.strideV;
481
- let nbytes = I444Buffer.calcDataSize(info.height, strideY, strideU, strideV);
482
- let data = FfiClient.instance.copyBuffer(yuv.dataYPtr, nbytes);
483
- new FfiHandle(owned.handle.id).dispose();
484
- return new I444Buffer(data, info.width, info.height, strideY, strideU, strideV);
485
- }
486
-
487
- static calcDataSize(height: number, strideY: number, strideU: number, strideV: number): number {
488
- return strideY * height + strideU * height + strideV * height;
489
- }
490
- }
491
-
492
- export class I010Buffer extends PlanarYuv16Buffer {
493
- constructor(
494
- data: Uint16Array,
495
- width: number,
496
- height: number,
497
- strideY: number,
498
- strideU: number,
499
- strideV: number,
500
- ) {
501
- let chromaWidth = ~~((width + 1) / 2);
502
- let chromaHeight = ~~((height + 1) / 2);
503
- super(
504
- data,
505
- width,
506
- height,
507
- VideoFrameBufferType.I010,
508
- strideY,
509
- strideU,
510
- strideV,
511
- chromaWidth,
512
- chromaHeight,
513
- );
514
- }
515
-
516
- /** @internal */
517
- static fromOwnedInfo(owned: OwnedVideoFrameBuffer): I010Buffer {
518
- let info = owned.info;
519
- let yuv = info.buffer.value as PlanarYuvBufferInfo;
520
- let strideY = yuv.strideY;
521
- let strideU = yuv.strideU;
522
- let strideV = yuv.strideV;
523
- let nbytes = I010Buffer.calcDataSize(info.height, strideY, strideU, strideV);
524
- let data = FfiClient.instance.copyBuffer(yuv.dataYPtr, nbytes);
525
- new FfiHandle(owned.handle.id).dispose();
526
- return new I010Buffer(
527
- new Uint16Array(data),
62
+ return new VideoFrame(
63
+ FfiClient.instance.copyBuffer(
64
+ info.dataPtr,
65
+ getPlaneLength(info.type, info.width, info.height),
66
+ ),
528
67
  info.width,
529
68
  info.height,
530
- strideY,
531
- strideU,
532
- strideV,
533
- );
534
- }
535
-
536
- static calcDataSize(height: number, strideY: number, strideU: number, strideV: number): number {
537
- let chromaHeight = ~~((height + 1) / 2);
538
- return (strideY * height + strideU * chromaHeight + strideV * chromaHeight) * 2;
539
- }
540
- }
541
-
542
- export class NV12Buffer extends BiplanarYuv8Buffer {
543
- constructor(data: Uint8Array, width: number, height: number, strideY: number, strideUV: number) {
544
- let chromaWidth = ~~((width + 1) / 2);
545
- let chromaHeight = ~~((height + 1) / 2);
546
- super(
547
- data,
548
- width,
549
- height,
550
- VideoFrameBufferType.NV12,
551
- strideY,
552
- strideUV,
553
- chromaWidth,
554
- chromaHeight,
69
+ info.type,
70
+ info.dataPtr,
555
71
  );
556
72
  }
557
73
 
558
- /** @internal */
559
- static fromOwnedInfo(owned: OwnedVideoFrameBuffer): NV12Buffer {
560
- let info = owned.info;
561
- let yuv = info.buffer.value as BiplanarYuvBufferInfo;
562
- let strideY = yuv.strideY;
563
- let strideUV = yuv.strideUv;
564
- let nbytes = NV12Buffer.calcDataSize(info.height, strideY, strideUV);
565
- let data = FfiClient.instance.copyBuffer(yuv.dataYPtr, nbytes);
566
- new FfiHandle(owned.handle.id).dispose();
567
- return new NV12Buffer(data, info.width, info.height, strideY, strideUV);
568
- }
74
+ getPlane(planeNth: number): Uint8Array | void {
75
+ const planeInfos = getPlaneInfos(this.dataPtr, this.type, this.width, this.height)
76
+ if (planeNth >= planeInfos.length) return;
569
77
 
570
- static calcDataSize(height: number, strideY: number, strideUV: number): number {
571
- let chromaHeight = ~~((height + 1) / 2);
572
- return strideY * height + strideUV * chromaHeight;
78
+ const planeInfo = planeInfos[planeNth];
79
+ return FfiClient.instance.copyBuffer(planeInfo.dataPtr, planeInfo.size);
573
80
  }
574
- }
575
-
576
- export class ArgbFrame {
577
- data: Uint8Array;
578
- format: VideoFormatType;
579
- width: number;
580
- height: number;
581
- stride: number;
582
81
 
583
- constructor(
584
- data: Uint8Array,
585
- format: VideoFormatType,
586
- width: number,
587
- height: number,
588
- stride: number = width * 4,
589
- ) {
590
- this.data = data;
591
- this.format = format;
592
- this.width = width;
593
- this.height = height;
594
- this.stride = stride;
595
- }
596
-
597
- static create(format: VideoFormatType, width: number, height: number): ArgbFrame {
598
- let data = new Uint8Array(width * height * 4);
599
- return new ArgbFrame(data, format, width, height);
600
- }
601
-
602
- toI420(): I420Buffer {
603
- let req = new ToI420Request({
604
- from: {
605
- case: 'argb',
82
+ convert(dstType: VideoBufferType, flipY = false): VideoFrame {
83
+ const req = new FfiRequest({
84
+ message: {
85
+ case: 'videoConvert',
606
86
  value: {
607
- format: this.format,
608
- width: this.width,
609
- height: this.height,
610
- stride: this.stride,
611
- ptr: FfiClient.instance.retrievePtr(this.data),
87
+ flipY,
88
+ dstType,
89
+ buffer: this.protoInfo(),
612
90
  },
613
91
  },
614
92
  });
93
+ const resp = FfiClient.instance.request<VideoConvertResponse>(req)
94
+ if (resp.error) {
95
+ throw resp.error
96
+ }
615
97
 
616
- let resp = FfiClient.instance.request<ToI420Response>({
617
- message: {
618
- case: 'toI420',
619
- value: req,
620
- },
621
- });
622
- return I420Buffer.fromOwnedInfo(resp.buffer);
98
+ return VideoFrame.fromOwnedInfo(resp.buffer)
623
99
  }
624
100
  }
101
+
102
+ const getPlaneLength = (type: VideoBufferType, width: number, height: number): number => {
103
+ const chromaWidth = Math.trunc((width + 1) / 2);
104
+ const chromaHeight = Math.trunc((height + 1) / 2);
105
+ switch (type) {
106
+ case VideoBufferType.ARGB:
107
+ case VideoBufferType.RGBA:
108
+ case VideoBufferType.ABGR:
109
+ case VideoBufferType.BGRA:
110
+ return width * height * 4;
111
+ case VideoBufferType.RGB24:
112
+ case VideoBufferType.I444:
113
+ return width * height * 3;
114
+ case VideoBufferType.I420:
115
+ return width * height + chromaWidth * chromaHeight * 2;
116
+ case VideoBufferType.I420A:
117
+ return width * height * 2 + chromaWidth * chromaWidth * 2;
118
+ case VideoBufferType.I422:
119
+ return width * height + chromaWidth * height * 2;
120
+ case VideoBufferType.I010:
121
+ return width * height * 2 + chromaWidth * chromaHeight * 4;
122
+ case VideoBufferType.NV12:
123
+ return width * height + chromaWidth * chromaWidth * 2;
124
+ }
125
+ };
126
+
127
+ const getPlaneInfos = (
128
+ dataPtr: bigint,
129
+ type: VideoBufferType,
130
+ width: number,
131
+ height: number,
132
+ ): VideoBufferInfo_ComponentInfo[] => {
133
+ const chromaWidth = Math.trunc((width + 1) / 2);
134
+ const chromaHeight = Math.trunc((height + 1) / 2);
135
+ switch (type) {
136
+ case VideoBufferType.I420: {
137
+ const y = new VideoBufferInfo_ComponentInfo({ dataPtr, stride: width, size: width * height });
138
+ const u = new VideoBufferInfo_ComponentInfo({
139
+ dataPtr: y.dataPtr + BigInt(y.size),
140
+ stride: chromaWidth,
141
+ size: chromaWidth * chromaHeight,
142
+ });
143
+ const v = new VideoBufferInfo_ComponentInfo({
144
+ dataPtr: u.dataPtr + BigInt(u.size),
145
+ stride: chromaWidth,
146
+ size: chromaWidth * chromaHeight,
147
+ });
148
+ return [y, u, v];
149
+ }
150
+ case VideoBufferType.I420A: {
151
+ const y = new VideoBufferInfo_ComponentInfo({ dataPtr, stride: width, size: width * height });
152
+ const u = new VideoBufferInfo_ComponentInfo({
153
+ dataPtr: y.dataPtr + BigInt(y.size),
154
+ stride: chromaWidth,
155
+ size: chromaWidth * chromaHeight,
156
+ });
157
+ const v = new VideoBufferInfo_ComponentInfo({
158
+ dataPtr: u.dataPtr + BigInt(u.size),
159
+ stride: chromaWidth,
160
+ size: chromaWidth * chromaHeight,
161
+ });
162
+ const a = new VideoBufferInfo_ComponentInfo({
163
+ dataPtr: v.dataPtr + BigInt(v.size),
164
+ stride: width,
165
+ size: width * height,
166
+ });
167
+ return [y, u, v, a];
168
+ }
169
+ case VideoBufferType.I422: {
170
+ const y = new VideoBufferInfo_ComponentInfo({ dataPtr, stride: width, size: width * height });
171
+ const u = new VideoBufferInfo_ComponentInfo({
172
+ dataPtr: y.dataPtr + BigInt(y.size),
173
+ stride: chromaWidth,
174
+ size: chromaWidth * height,
175
+ });
176
+ const v = new VideoBufferInfo_ComponentInfo({
177
+ dataPtr: u.dataPtr + BigInt(u.size),
178
+ stride: chromaWidth,
179
+ size: chromaWidth * height,
180
+ });
181
+ return [y, u, v];
182
+ }
183
+ case VideoBufferType.I444: {
184
+ const y = new VideoBufferInfo_ComponentInfo({ dataPtr, stride: width, size: width * height });
185
+ const u = new VideoBufferInfo_ComponentInfo({
186
+ dataPtr: y.dataPtr + BigInt(y.size),
187
+ stride: width,
188
+ size: width * height,
189
+ });
190
+ const v = new VideoBufferInfo_ComponentInfo({
191
+ dataPtr: u.dataPtr + BigInt(u.size),
192
+ stride: width,
193
+ size: width * height,
194
+ });
195
+ return [y, u, v];
196
+ }
197
+ case VideoBufferType.I010: {
198
+ const y = new VideoBufferInfo_ComponentInfo({
199
+ dataPtr,
200
+ stride: width * 2,
201
+ size: width * height * 2,
202
+ });
203
+ const u = new VideoBufferInfo_ComponentInfo({
204
+ dataPtr: y.dataPtr + BigInt(y.size),
205
+ stride: chromaWidth * 2,
206
+ size: chromaWidth * chromaHeight * 2,
207
+ });
208
+ const v = new VideoBufferInfo_ComponentInfo({
209
+ dataPtr: u.dataPtr + BigInt(u.size),
210
+ stride: chromaWidth * 2,
211
+ size: chromaWidth * chromaHeight * 2,
212
+ });
213
+ return [y, u, v];
214
+ }
215
+ case VideoBufferType.NV12: {
216
+ const y = new VideoBufferInfo_ComponentInfo({ dataPtr, stride: width, size: width * height });
217
+ const uv = new VideoBufferInfo_ComponentInfo({
218
+ dataPtr: y.dataPtr + BigInt(y.size),
219
+ stride: chromaWidth * 2,
220
+ size: chromaWidth * chromaHeight * 2,
221
+ });
222
+ return [y, uv];
223
+ }
224
+ default:
225
+ return [];
226
+ }
227
+ };