@livekit/rtc-node 0.13.2 → 0.13.4

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 (114) hide show
  1. package/dist/data_streams/index.cjs +25 -0
  2. package/dist/data_streams/index.cjs.map +1 -0
  3. package/dist/data_streams/index.d.cts +11 -0
  4. package/dist/data_streams/index.d.ts +11 -0
  5. package/dist/data_streams/index.d.ts.map +1 -0
  6. package/dist/data_streams/index.js +3 -0
  7. package/dist/data_streams/index.js.map +1 -0
  8. package/dist/data_streams/stream_reader.cjs +145 -0
  9. package/dist/data_streams/stream_reader.cjs.map +1 -0
  10. package/dist/data_streams/stream_reader.d.cts +10 -0
  11. package/dist/data_streams/stream_reader.d.ts +10 -0
  12. package/dist/data_streams/stream_reader.d.ts.map +1 -0
  13. package/dist/data_streams/stream_reader.js +120 -0
  14. package/dist/data_streams/stream_reader.js.map +1 -0
  15. package/dist/data_streams/stream_writer.cjs +51 -0
  16. package/dist/data_streams/stream_writer.cjs.map +1 -0
  17. package/dist/data_streams/stream_writer.d.cts +26 -0
  18. package/dist/data_streams/stream_writer.d.ts +26 -0
  19. package/dist/data_streams/stream_writer.d.ts.map +1 -0
  20. package/dist/data_streams/stream_writer.js +26 -0
  21. package/dist/data_streams/stream_writer.js.map +1 -0
  22. package/dist/data_streams/types.cjs +17 -0
  23. package/dist/data_streams/types.cjs.map +1 -0
  24. package/dist/data_streams/types.d.cts +10 -0
  25. package/dist/data_streams/types.d.ts +10 -0
  26. package/dist/data_streams/types.d.ts.map +1 -0
  27. package/dist/data_streams/types.js +1 -0
  28. package/dist/data_streams/types.js.map +1 -0
  29. package/dist/index.cjs +7 -4
  30. package/dist/index.cjs.map +1 -1
  31. package/dist/index.d.cts +4 -0
  32. package/dist/index.d.ts +4 -0
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +1 -0
  35. package/dist/index.js.map +1 -1
  36. package/dist/log.cjs +42 -0
  37. package/dist/log.cjs.map +1 -0
  38. package/dist/log.d.cts +7 -0
  39. package/dist/log.d.ts +7 -0
  40. package/dist/log.d.ts.map +1 -0
  41. package/dist/log.js +18 -0
  42. package/dist/log.js.map +1 -0
  43. package/dist/napi/native.d.cjs.map +1 -1
  44. package/dist/napi/native.d.ts +5 -5
  45. package/dist/native.d.ts +5 -5
  46. package/dist/participant.cjs +260 -16
  47. package/dist/participant.cjs.map +1 -1
  48. package/dist/participant.d.cts +40 -6
  49. package/dist/participant.d.ts +40 -6
  50. package/dist/participant.d.ts.map +1 -1
  51. package/dist/participant.js +263 -5
  52. package/dist/participant.js.map +1 -1
  53. package/dist/proto/ffi_pb.cjs +14 -3
  54. package/dist/proto/ffi_pb.cjs.map +1 -1
  55. package/dist/proto/ffi_pb.d.cts +81 -7
  56. package/dist/proto/ffi_pb.d.ts +81 -7
  57. package/dist/proto/ffi_pb.d.ts.map +1 -1
  58. package/dist/proto/ffi_pb.js +15 -4
  59. package/dist/proto/ffi_pb.js.map +1 -1
  60. package/dist/proto/room_pb.cjs +488 -30
  61. package/dist/proto/room_pb.cjs.map +1 -1
  62. package/dist/proto/room_pb.d.cts +412 -50
  63. package/dist/proto/room_pb.d.ts +412 -50
  64. package/dist/proto/room_pb.d.ts.map +1 -1
  65. package/dist/proto/room_pb.js +471 -29
  66. package/dist/proto/room_pb.js.map +1 -1
  67. package/dist/proto/video_frame_pb.cjs.map +1 -1
  68. package/dist/proto/video_frame_pb.d.cts +3 -0
  69. package/dist/proto/video_frame_pb.d.ts +3 -0
  70. package/dist/proto/video_frame_pb.d.ts.map +1 -1
  71. package/dist/proto/video_frame_pb.js.map +1 -1
  72. package/dist/room.cjs +202 -45
  73. package/dist/room.cjs.map +1 -1
  74. package/dist/room.d.cts +21 -3
  75. package/dist/room.d.ts +21 -3
  76. package/dist/room.d.ts.map +1 -1
  77. package/dist/room.js +203 -45
  78. package/dist/room.js.map +1 -1
  79. package/dist/types-BWsKf9LP.d.cts +93 -0
  80. package/dist/types-Ca3IVRvo.d.ts +93 -0
  81. package/dist/utils.cjs +51 -0
  82. package/dist/utils.cjs.map +1 -0
  83. package/dist/utils.d.cts +7 -0
  84. package/dist/utils.d.ts +7 -0
  85. package/dist/utils.d.ts.map +1 -0
  86. package/dist/utils.js +25 -0
  87. package/dist/utils.js.map +1 -0
  88. package/dist/version.cjs +1 -1
  89. package/dist/version.cjs.map +1 -1
  90. package/dist/version.d.cts +1 -1
  91. package/dist/version.d.ts +1 -1
  92. package/dist/version.js +1 -1
  93. package/dist/version.js.map +1 -1
  94. package/dist/video_frame.cjs +2 -0
  95. package/dist/video_frame.cjs.map +1 -1
  96. package/dist/video_frame.d.ts.map +1 -1
  97. package/dist/video_frame.js +2 -0
  98. package/dist/video_frame.js.map +1 -1
  99. package/package.json +9 -7
  100. package/src/data_streams/index.ts +7 -0
  101. package/src/data_streams/stream_reader.ts +166 -0
  102. package/src/data_streams/stream_writer.ts +36 -0
  103. package/src/data_streams/types.ts +61 -0
  104. package/src/index.ts +1 -0
  105. package/src/log.ts +25 -0
  106. package/src/napi/native.d.ts +5 -5
  107. package/src/participant.ts +351 -29
  108. package/src/proto/ffi_pb.ts +86 -1
  109. package/src/proto/room_pb.ts +788 -68
  110. package/src/proto/video_frame_pb.ts +3 -0
  111. package/src/room.ts +251 -68
  112. package/src/utils.ts +33 -0
  113. package/src/version.ts +1 -1
  114. package/src/video_frame.ts +2 -0
@@ -0,0 +1,166 @@
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ import type { ReadableStream } from 'node:stream/web';
5
+ import { log } from '../log.js';
6
+ import type { DataStream_Chunk } from '../proto/room_pb.js';
7
+ import { bigIntToNumber } from '../utils.js';
8
+ import type { BaseStreamInfo, ByteStreamInfo, TextStreamChunk, TextStreamInfo } from './types.js';
9
+
10
+ abstract class BaseStreamReader<T extends BaseStreamInfo> {
11
+ protected reader: ReadableStream<DataStream_Chunk>;
12
+
13
+ protected totalByteSize?: number;
14
+
15
+ protected _info: T;
16
+
17
+ protected bytesReceived: number;
18
+
19
+ get info() {
20
+ return this._info;
21
+ }
22
+
23
+ constructor(info: T, stream: ReadableStream<DataStream_Chunk>, totalByteSize?: number) {
24
+ this.reader = stream;
25
+ this.totalByteSize = totalByteSize;
26
+ this._info = info;
27
+ this.bytesReceived = 0;
28
+ }
29
+
30
+ protected abstract handleChunkReceived(chunk: DataStream_Chunk): void;
31
+
32
+ onProgress?: (progress: number | undefined) => void;
33
+
34
+ abstract readAll(): Promise<string | Array<Uint8Array>>;
35
+ }
36
+
37
+ /**
38
+ * A class to read chunks from a ReadableStream and provide them in a structured format.
39
+ */
40
+ export class ByteStreamReader extends BaseStreamReader<ByteStreamInfo> {
41
+ protected handleChunkReceived(chunk: DataStream_Chunk) {
42
+ this.bytesReceived += chunk.content!.byteLength;
43
+ const currentProgress = this.totalByteSize
44
+ ? this.bytesReceived / this.totalByteSize
45
+ : undefined;
46
+ this.onProgress?.(currentProgress);
47
+ }
48
+
49
+ [Symbol.asyncIterator]() {
50
+ const reader = this.reader.getReader();
51
+
52
+ return {
53
+ next: async (): Promise<IteratorResult<Uint8Array>> => {
54
+ try {
55
+ const { done, value } = await reader.read();
56
+ if (done) {
57
+ return { done: true, value: undefined as any };
58
+ } else {
59
+ this.handleChunkReceived(value);
60
+ return { done: false, value: value.content! };
61
+ }
62
+ } catch (error) {
63
+ log.error('error processing stream update', error);
64
+ return { done: true, value: undefined };
65
+ }
66
+ },
67
+
68
+ return(): IteratorResult<Uint8Array> {
69
+ reader.releaseLock();
70
+ return { done: true, value: undefined };
71
+ },
72
+ };
73
+ }
74
+
75
+ async readAll(): Promise<Array<Uint8Array>> {
76
+ const chunks: Set<Uint8Array> = new Set();
77
+ for await (const chunk of this) {
78
+ chunks.add(chunk);
79
+ }
80
+ return Array.from(chunks);
81
+ }
82
+ }
83
+
84
+ /**
85
+ * A class to read chunks from a ReadableStream and provide them in a structured format.
86
+ */
87
+ export class TextStreamReader extends BaseStreamReader<TextStreamInfo> {
88
+ private receivedChunks: Map<number, DataStream_Chunk>;
89
+
90
+ /**
91
+ * A TextStreamReader instance can be used as an AsyncIterator that returns the entire string
92
+ * that has been received up to the current point in time.
93
+ */
94
+ constructor(
95
+ info: TextStreamInfo,
96
+ stream: ReadableStream<DataStream_Chunk>,
97
+ totalChunkCount?: number,
98
+ ) {
99
+ super(info, stream, totalChunkCount);
100
+ this.receivedChunks = new Map();
101
+ }
102
+
103
+ protected handleChunkReceived(chunk: DataStream_Chunk) {
104
+ const index = bigIntToNumber(chunk.chunkIndex!);
105
+ const previousChunkAtIndex = this.receivedChunks.get(index!);
106
+ if (previousChunkAtIndex && previousChunkAtIndex.version! > chunk.version!) {
107
+ // we have a newer version already, dropping the old one
108
+ return;
109
+ }
110
+ this.receivedChunks.set(index, chunk);
111
+ const currentProgress = this.totalByteSize
112
+ ? this.receivedChunks.size / this.totalByteSize
113
+ : undefined;
114
+ this.onProgress?.(currentProgress);
115
+ }
116
+
117
+ /**
118
+ * Async iterator implementation to allow usage of `for await...of` syntax.
119
+ * Yields structured chunks from the stream.
120
+ *
121
+ */
122
+ [Symbol.asyncIterator]() {
123
+ const reader = this.reader.getReader();
124
+ const decoder = new TextDecoder();
125
+
126
+ return {
127
+ next: async (): Promise<IteratorResult<TextStreamChunk>> => {
128
+ try {
129
+ const { done, value } = await reader.read();
130
+ if (done) {
131
+ return { done: true, value: undefined };
132
+ } else {
133
+ this.handleChunkReceived(value);
134
+ return {
135
+ done: false,
136
+ value: {
137
+ index: bigIntToNumber(value.chunkIndex)!,
138
+ current: decoder.decode(value.content!),
139
+ collected: Array.from(this.receivedChunks.values())
140
+ .sort((a, b) => bigIntToNumber(a.chunkIndex!) - bigIntToNumber(b.chunkIndex!))
141
+ .map((chunk) => decoder.decode(chunk.content!))
142
+ .join(''),
143
+ },
144
+ };
145
+ }
146
+ } catch (error) {
147
+ log.error('error processing stream update', error);
148
+ return { done: true, value: undefined };
149
+ }
150
+ },
151
+
152
+ return(): IteratorResult<TextStreamChunk> {
153
+ reader.releaseLock();
154
+ return { done: true, value: undefined };
155
+ },
156
+ };
157
+ }
158
+
159
+ async readAll(): Promise<string> {
160
+ let latestString: string = '';
161
+ for await (const { collected } of this) {
162
+ latestString = collected;
163
+ }
164
+ return latestString;
165
+ }
166
+ }
@@ -0,0 +1,36 @@
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ import type { WritableStream } from 'node:stream/web';
5
+ import type { BaseStreamInfo, ByteStreamInfo, TextStreamInfo } from './types.js';
6
+
7
+ class BaseStreamWriter<T, InfoType extends BaseStreamInfo> {
8
+ protected writableStream: WritableStream<[T, number?]>;
9
+
10
+ protected defaultWriter: WritableStreamDefaultWriter<[T, number?]>;
11
+
12
+ protected onClose?: () => void;
13
+
14
+ readonly info: InfoType;
15
+
16
+ constructor(writableStream: WritableStream<[T, number?]>, info: InfoType, onClose?: () => void) {
17
+ this.writableStream = writableStream;
18
+ this.defaultWriter = writableStream.getWriter();
19
+ this.onClose = onClose;
20
+ this.info = info;
21
+ }
22
+
23
+ write(chunk: T, chunkIndex?: number): Promise<void> {
24
+ return this.defaultWriter.write([chunk, chunkIndex]);
25
+ }
26
+
27
+ async close() {
28
+ await this.defaultWriter.close();
29
+ this.defaultWriter.releaseLock();
30
+ this.onClose?.();
31
+ }
32
+ }
33
+
34
+ export class TextStreamWriter extends BaseStreamWriter<string, TextStreamInfo> {}
35
+
36
+ export class ByteStreamWriter extends BaseStreamWriter<Uint8Array, ByteStreamInfo> {}
@@ -0,0 +1,61 @@
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ import type { DataStream_Chunk, DataStream_Header } from '../proto/room_pb.js';
5
+ import type { ByteStreamReader, TextStreamReader } from './stream_reader.js';
6
+
7
+ export interface StreamController<T extends DataStream_Chunk> {
8
+ header: DataStream_Header;
9
+ controller: ReadableStreamDefaultController<T>;
10
+ startTime: number;
11
+ endTime?: number;
12
+ }
13
+
14
+ export interface BaseStreamInfo {
15
+ streamId: string;
16
+ mimeType: string;
17
+ topic: string;
18
+ timestamp: number;
19
+ /** total size in bytes for finite streams and undefined for streams of unknown size */
20
+ totalSize?: number;
21
+ attributes?: Record<string, string>;
22
+ }
23
+
24
+ export type ByteStreamInfo = BaseStreamInfo & {
25
+ name: string;
26
+ };
27
+
28
+ export type TextStreamInfo = BaseStreamInfo;
29
+
30
+ export type TextStreamChunk = {
31
+ index: number;
32
+ current: string;
33
+ collected: string;
34
+ };
35
+
36
+ export interface DataStreamOptions {
37
+ topic?: string;
38
+ destinationIdentities?: Array<string>;
39
+ attributes?: Record<string, string>;
40
+ mimeType?: string;
41
+ }
42
+
43
+ export interface TextStreamOptions extends DataStreamOptions {
44
+ // replyToMessageId?: string;
45
+ attachments?: [];
46
+ }
47
+
48
+ export interface ByteStreamOptions extends DataStreamOptions {
49
+ name?: string;
50
+ onProgress?: (progress: number) => void;
51
+ }
52
+
53
+ export type ByteStreamHandler = (
54
+ reader: ByteStreamReader,
55
+ participantInfo: { identity: string },
56
+ ) => void;
57
+
58
+ export type TextStreamHandler = (
59
+ reader: TextStreamReader,
60
+ participantInfo: { identity: string },
61
+ ) => void;
package/src/index.ts CHANGED
@@ -30,6 +30,7 @@ export {
30
30
  export type { Transcription, TranscriptionSegment } from './transcription.js';
31
31
  export { E2EEManager, KeyProvider, FrameCryptor } from './e2ee.js';
32
32
  export type { E2EEOptions, KeyProviderOptions } from './e2ee.js';
33
+ export * from './data_streams/index.js';
33
34
  export {
34
35
  ConnectionQuality,
35
36
  IceServer,
package/src/log.ts ADDED
@@ -0,0 +1,25 @@
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ import type { LevelWithSilent, LoggerOptions } from 'pino';
5
+ import { pino } from 'pino';
6
+
7
+ const isProduction = process.env.NODE_ENV === 'production';
8
+
9
+ const defaultOptions: LoggerOptions = { name: 'lk-rtc' };
10
+
11
+ const devOptions: LoggerOptions = {
12
+ ...defaultOptions,
13
+ transport: {
14
+ target: 'pino-pretty',
15
+ options: {
16
+ colorize: true,
17
+ },
18
+ },
19
+ };
20
+
21
+ const log = pino(isProduction ? defaultOptions : devOptions);
22
+ log.level = isProduction ? 'info' : 'debug';
23
+
24
+ export type LogLevel = LevelWithSilent;
25
+ export { log };
@@ -3,15 +3,15 @@
3
3
 
4
4
  /* auto-generated by NAPI-RS */
5
5
 
6
- export function livekitInitialize(
6
+ export declare function livekitInitialize(
7
7
  callback: (data: Uint8Array) => void,
8
8
  captureLogs: boolean,
9
9
  sdkVersion: string,
10
10
  ): void;
11
- export function livekitFfiRequest(data: Uint8Array): Uint8Array;
12
- export function livekitRetrievePtr(handle: Uint8Array): bigint;
13
- export function livekitCopyBuffer(ptr: bigint, len: number): Uint8Array;
14
- export function livekitDispose(): Promise<void>;
11
+ export declare function livekitFfiRequest(data: Uint8Array): Uint8Array;
12
+ export declare function livekitRetrievePtr(handle: Uint8Array): bigint;
13
+ export declare function livekitCopyBuffer(ptr: bigint, len: number): Uint8Array;
14
+ export declare function livekitDispose(): Promise<void>;
15
15
  export declare class FfiHandle {
16
16
  constructor(handle: bigint);
17
17
  dispose(): void;