@livekit/rtc-node 0.0.2 → 0.0.3

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