livekit-client 2.20.2 → 2.21.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 (105) hide show
  1. package/dist/livekit-client.e2ee.worker.js +1 -1
  2. package/dist/livekit-client.e2ee.worker.js.map +1 -1
  3. package/dist/livekit-client.e2ee.worker.mjs +4 -0
  4. package/dist/livekit-client.e2ee.worker.mjs.map +1 -1
  5. package/dist/livekit-client.esm.mjs +1993 -791
  6. package/dist/livekit-client.esm.mjs.map +1 -1
  7. package/dist/livekit-client.fm.worker.js +1 -1
  8. package/dist/livekit-client.fm.worker.js.map +1 -1
  9. package/dist/livekit-client.fm.worker.mjs +4 -0
  10. package/dist/livekit-client.fm.worker.mjs.map +1 -1
  11. package/dist/livekit-client.umd.js +1 -1
  12. package/dist/livekit-client.umd.js.map +1 -1
  13. package/dist/src/index.d.ts +1 -1
  14. package/dist/src/index.d.ts.map +1 -1
  15. package/dist/src/room/RTCEngine.d.ts +28 -64
  16. package/dist/src/room/RTCEngine.d.ts.map +1 -1
  17. package/dist/src/room/Room.d.ts +4 -0
  18. package/dist/src/room/Room.d.ts.map +1 -1
  19. package/dist/src/room/data-channel/DataChannelManager.d.ts +62 -0
  20. package/dist/src/room/data-channel/DataChannelManager.d.ts.map +1 -0
  21. package/dist/src/room/data-channel/FlowControlledDataChannel.d.ts +95 -0
  22. package/dist/src/room/data-channel/FlowControlledDataChannel.d.ts.map +1 -0
  23. package/dist/src/room/data-channel/LossyDataChannel.d.ts +40 -0
  24. package/dist/src/room/data-channel/LossyDataChannel.d.ts.map +1 -0
  25. package/dist/src/room/data-channel/ReliableDataChannel.d.ts +56 -0
  26. package/dist/src/room/data-channel/ReliableDataChannel.d.ts.map +1 -0
  27. package/dist/src/room/data-channel/types.d.ts +12 -0
  28. package/dist/src/room/data-channel/types.d.ts.map +1 -0
  29. package/dist/src/room/data-stream/compression.d.ts +49 -0
  30. package/dist/src/room/data-stream/compression.d.ts.map +1 -0
  31. package/dist/src/room/data-stream/constants.d.ts +18 -0
  32. package/dist/src/room/data-stream/constants.d.ts.map +1 -0
  33. package/dist/src/room/data-stream/incoming/IncomingDataStreamManager.d.ts +4 -0
  34. package/dist/src/room/data-stream/incoming/IncomingDataStreamManager.d.ts.map +1 -1
  35. package/dist/src/room/data-stream/incoming/StreamReader.d.ts.map +1 -1
  36. package/dist/src/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts +49 -2
  37. package/dist/src/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts.map +1 -1
  38. package/dist/src/room/data-stream/outgoing/header-utils.d.ts +18 -0
  39. package/dist/src/room/data-stream/outgoing/header-utils.d.ts.map +1 -0
  40. package/dist/src/room/errors.d.ts +3 -1
  41. package/dist/src/room/errors.d.ts.map +1 -1
  42. package/dist/src/room/participant/LocalParticipant.d.ts +9 -1
  43. package/dist/src/room/participant/LocalParticipant.d.ts.map +1 -1
  44. package/dist/src/room/participant/RemoteParticipant.d.ts +11 -2
  45. package/dist/src/room/participant/RemoteParticipant.d.ts.map +1 -1
  46. package/dist/src/room/rpc/client/RpcClientManager.d.ts.map +1 -1
  47. package/dist/src/room/rpc/server/RpcServerManager.d.ts.map +1 -1
  48. package/dist/src/room/types.d.ts +17 -0
  49. package/dist/src/room/types.d.ts.map +1 -1
  50. package/dist/src/room/utils.d.ts +13 -0
  51. package/dist/src/room/utils.d.ts.map +1 -1
  52. package/dist/src/version.d.ts +5 -1
  53. package/dist/src/version.d.ts.map +1 -1
  54. package/dist/ts4.2/index.d.ts +1 -1
  55. package/dist/ts4.2/room/RTCEngine.d.ts +28 -64
  56. package/dist/ts4.2/room/Room.d.ts +4 -0
  57. package/dist/ts4.2/room/data-channel/DataChannelManager.d.ts +62 -0
  58. package/dist/ts4.2/room/data-channel/FlowControlledDataChannel.d.ts +95 -0
  59. package/dist/ts4.2/room/data-channel/LossyDataChannel.d.ts +41 -0
  60. package/dist/ts4.2/room/data-channel/ReliableDataChannel.d.ts +57 -0
  61. package/dist/ts4.2/room/data-channel/types.d.ts +12 -0
  62. package/dist/ts4.2/room/data-stream/compression.d.ts +49 -0
  63. package/dist/ts4.2/room/data-stream/constants.d.ts +18 -0
  64. package/dist/ts4.2/room/data-stream/incoming/IncomingDataStreamManager.d.ts +4 -0
  65. package/dist/ts4.2/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts +49 -2
  66. package/dist/ts4.2/room/data-stream/outgoing/header-utils.d.ts +18 -0
  67. package/dist/ts4.2/room/errors.d.ts +3 -1
  68. package/dist/ts4.2/room/participant/LocalParticipant.d.ts +9 -1
  69. package/dist/ts4.2/room/participant/RemoteParticipant.d.ts +11 -1
  70. package/dist/ts4.2/room/types.d.ts +17 -0
  71. package/dist/ts4.2/room/utils.d.ts +13 -0
  72. package/dist/ts4.2/version.d.ts +5 -1
  73. package/package.json +2 -2
  74. package/src/index.ts +2 -0
  75. package/src/room/RTCEngine.test.ts +58 -99
  76. package/src/room/RTCEngine.ts +92 -534
  77. package/src/room/Room.test.ts +4 -1
  78. package/src/room/Room.ts +40 -8
  79. package/src/room/data-channel/DataChannelManager.test.ts +152 -0
  80. package/src/room/data-channel/DataChannelManager.ts +237 -0
  81. package/src/room/data-channel/FlowControlledDataChannel.test.ts +154 -0
  82. package/src/room/data-channel/FlowControlledDataChannel.ts +216 -0
  83. package/src/room/data-channel/LossyDataChannel.test.ts +118 -0
  84. package/src/room/data-channel/LossyDataChannel.ts +125 -0
  85. package/src/room/data-channel/ReliableDataChannel.test.ts +158 -0
  86. package/src/room/data-channel/ReliableDataChannel.ts +153 -0
  87. package/src/room/data-channel/types.ts +30 -0
  88. package/src/room/data-stream/compression.ts +117 -0
  89. package/src/room/data-stream/constants.ts +18 -0
  90. package/src/room/data-stream/incoming/IncomingDataStreamManager.test.ts +1601 -0
  91. package/src/room/data-stream/incoming/IncomingDataStreamManager.ts +438 -86
  92. package/src/room/data-stream/incoming/StreamReader.ts +8 -2
  93. package/src/room/data-stream/outgoing/OutgoingDataStreamManager.test.ts +1292 -0
  94. package/src/room/data-stream/outgoing/OutgoingDataStreamManager.ts +489 -116
  95. package/src/room/data-stream/outgoing/header-utils.ts +87 -0
  96. package/src/room/errors.ts +6 -0
  97. package/src/room/participant/LocalParticipant.ts +13 -0
  98. package/src/room/participant/RemoteParticipant.ts +19 -5
  99. package/src/room/rpc/client/RpcClientManager.test.ts +20 -27
  100. package/src/room/rpc/client/RpcClientManager.ts +1 -4
  101. package/src/room/rpc/server/RpcServerManager.test.ts +24 -23
  102. package/src/room/rpc/server/RpcServerManager.ts +1 -3
  103. package/src/room/types.ts +18 -0
  104. package/src/room/utils.ts +67 -0
  105. package/src/version.ts +5 -1
@@ -268,7 +268,11 @@ declare class Room extends Room_base {
268
268
  */
269
269
  private updateSubscriptions;
270
270
  private getRemoteParticipantBySid;
271
+ /** The client capabilities this SDK advertises to other participants in its `ClientInfo`. */
272
+ private getClientInfoCapabilities;
271
273
  private getRemoteParticipantClientProtocol;
274
+ private getRemoteParticipantCapabilities;
275
+ private getAllRemoteParticipantIdentities;
272
276
  private registerRpcDataStreamHandler;
273
277
  private registerConnectionReconcile;
274
278
  private clearConnectionReconcile;
@@ -0,0 +1,62 @@
1
+ import type { PCTransportManager } from '../PCTransportManager';
2
+ import { FlowControlledDataChannel } from './FlowControlledDataChannel';
3
+ import { LossyDataChannel } from './LossyDataChannel';
4
+ import { ReliableDataChannel } from './ReliableDataChannel';
5
+ import { DataChannelKind } from './types';
6
+ export interface DataChannelManagerOptions {
7
+ /** Whether the owning engine has been closed — a closed engine rejects headroom waiters. */
8
+ isEngineClosed: () => boolean;
9
+ /**
10
+ * Whether a reconnect attempt is underway: reliable sends defer to the resume replay and lossy
11
+ * sends are skipped while this is true.
12
+ */
13
+ isReconnecting: () => boolean;
14
+ onDataMessage: (message: MessageEvent) => void;
15
+ onDataTrackMessage: (message: MessageEvent) => void;
16
+ onDataError: (event: Event) => void;
17
+ onChannelClose: (kind: DataChannelKind) => void;
18
+ /** A channel's buffer crossed its low-water mark (debounced). Drives DCBufferStatusChanged. */
19
+ onBufferStatusChanged: (kind: DataChannelKind, isLow: boolean) => void;
20
+ }
21
+ /**
22
+ * Owns the engine's data channels: the three flow-controlled publisher channel wrappers (which
23
+ * live for the engine's lifetime and have RTCDataChannel handles attached/detached as peer
24
+ * connections come and go) plus the subscriber-side receive handles adopted by label.
25
+ *
26
+ * Handle turnover goes through {@link FlowControlledDataChannel.attach}/`detach`, which reject
27
+ * parked headroom waiters as a built-in — there is no separate invalidation step to forget.
28
+ */
29
+ export declare class DataChannelManager {
30
+ readonly reliable: ReliableDataChannel;
31
+ readonly lossy: LossyDataChannel;
32
+ readonly dataTrack: LossyDataChannel;
33
+ private reliableSub?;
34
+ private lossySub?;
35
+ private dataTrackSub?;
36
+ private opts;
37
+ constructor(opts: DataChannelManagerOptions);
38
+ /** The flow-control wrapper for `kind`. */
39
+ channelFor(kind: DataChannelKind): FlowControlledDataChannel;
40
+ /** The raw RTCDataChannel handle for `kind`, publisher side by default. */
41
+ getHandle(kind: DataChannelKind, subscriber?: boolean): RTCDataChannel | undefined;
42
+ get hasPublisherChannels(): boolean;
43
+ /**
44
+ * Creates the three publisher data channels on the given transport, wires their handlers, and
45
+ * attaches them to the wrappers — attaching rejects any waiters still parked on replaced
46
+ * channel objects.
47
+ */
48
+ createPublisherChannels(pcManager: PCTransportManager): void;
49
+ /**
50
+ * Adopts a subscriber-side data channel by label, wiring the matching receive handler.
51
+ * Returns false for labels this manager doesn't own.
52
+ */
53
+ adoptSubscriberChannel(channel: RTCDataChannel): boolean;
54
+ /**
55
+ * Tears down all channels for a peer-connection cleanup: rejects parked waiters (detach — the
56
+ * spec allows `pc.close()` to transition channels to 'closed' without firing events, so waiting
57
+ * for browser close events is not an option), strips handlers, closes the handles, and resets
58
+ * the reliable session state.
59
+ */
60
+ teardown(): void;
61
+ }
62
+ //# sourceMappingURL=DataChannelManager.d.ts.map
@@ -0,0 +1,95 @@
1
+ import type { DataChannelKind } from './types';
2
+ export interface FlowControlledDataChannelOptions {
3
+ kind: DataChannelKind;
4
+ /** Buffer level (bytes) at which blocked senders resume; armed as `bufferedAmountLowThreshold`. */
5
+ lowWaterMark: number;
6
+ /** Buffer level (bytes) above which senders block until the buffer drains to the low mark. */
7
+ highWaterMark: number;
8
+ /** Whether the owning engine has been closed — a closed engine rejects waiters immediately. */
9
+ isEngineClosed: () => boolean;
10
+ /**
11
+ * Notified when the buffer crosses the low-water mark in either direction (debounced: fires only
12
+ * on an actual change). Drives the engine's public DCBufferStatusChanged event.
13
+ */
14
+ onBufferStatusChanged?: (isLow: boolean) => void;
15
+ }
16
+ /**
17
+ * Two-watermark flow control for one data channel kind.
18
+ *
19
+ * Owns the per-kind headroom gate: senders proceed freely while the buffer is at or below the
20
+ * high-water mark and otherwise block — serialized FIFO through a mutex — until the browser's
21
+ * `bufferedamountlow` event (armed at the low-water mark) signals the buffer has drained. The
22
+ * serialization prevents woken senders from all refilling at once and overflowing the SCTP send
23
+ * buffer (see livekit/client-sdk-js#1995).
24
+ *
25
+ * Waiters are parked on the channel object captured at wait entry. If that object stops being
26
+ * current — replaced or torn down — its events may never fire again, so the owner must call
27
+ * {@link invalidateWaiters}, which aborts parked waiters (releasing the gate); the next waiter
28
+ * gets a fresh controller.
29
+ */
30
+ export declare class FlowControlledDataChannel {
31
+ readonly kind: DataChannelKind;
32
+ readonly lowWaterMark: number;
33
+ readonly highWaterMark: number;
34
+ protected isEngineClosed: () => boolean;
35
+ private onBufferStatusChanged?;
36
+ /** Last emitted low-water status; starts true (an empty buffer is below the mark). */
37
+ private bufferStatusLow;
38
+ private handle?;
39
+ private headroomLock;
40
+ /** Cancels parked headroom waiters when the handle is replaced or torn down. */
41
+ private waiterAbortController;
42
+ constructor(opts: FlowControlledDataChannelOptions);
43
+ /** The currently attached RTCDataChannel handle, if any. */
44
+ get channelHandle(): RTCDataChannel | undefined;
45
+ /**
46
+ * Attaches the channel handle this wrapper controls. Replacing an existing handle rejects
47
+ * parked waiters — their events would never fire again on the abandoned object — and installs a
48
+ * fresh controller, so queued senders re-check against the new channel. Wrappers outlive their
49
+ * handles: this is the one place handle turnover happens, which is what makes stranding a
50
+ * waiter structurally impossible.
51
+ */
52
+ attach(dc: RTCDataChannel): void;
53
+ /** Detaches the handle on teardown, rejecting parked waiters. */
54
+ detach(reason?: string): void;
55
+ protected getChannel(): RTCDataChannel | undefined;
56
+ /**
57
+ * Whether the send buffer has room to accept more data (the send gate). Senders proceed while
58
+ * this is true and block once it goes false. Callers resolve the handle (and decide what an
59
+ * absent one means) before asking.
60
+ */
61
+ isBelowHighWaterMark(dc: RTCDataChannel): boolean;
62
+ /**
63
+ * Whether the send buffer has drained to its low-water mark. Drives the engine's public
64
+ * DCBufferStatusChanged event.
65
+ */
66
+ isBelowLowWaterMark(dc: RTCDataChannel): boolean;
67
+ /**
68
+ * Acquires the headroom lock, resolving with the unlock function. Batch senders (the resume
69
+ * replay) hold it across all of their sends so no other sender can interleave, calling
70
+ * {@link waitForHeadroomWithoutLock} per message to respect flow control within the batch.
71
+ */
72
+ lockHeadroom(): Promise<() => void>;
73
+ /**
74
+ * Resolves once the caller may send on this channel: immediately while the send buffer is at or
75
+ * below its high-water mark, otherwise once the buffer has drained to the low-water mark (the
76
+ * `bufferedamountlow` event). Callers are serialized through the headroom lock so that, when
77
+ * the buffer drains, they refill it one at a time (up to the high-water mark) rather than all
78
+ * sending at once and overflowing the SCTP send buffer (see livekit/client-sdk-js#1995). The
79
+ * closed/buffer checks run inside the lock so queued callers proceed in FIFO order.
80
+ */
81
+ waitForHeadroomWithLock(): Promise<void>;
82
+ /** Core wait of {@link waitForHeadroomWithLock}. The caller must hold the headroom lock. */
83
+ waitForHeadroomWithoutLock(): Promise<void>;
84
+ /** Rejects all parked headroom waiters; the next waiter gets a fresh controller. */
85
+ invalidateWaiters(reason: string): void;
86
+ /**
87
+ * Recomputes whether the buffer has drained to the low-water mark and, if that changed since the
88
+ * last check, notifies the status listener. Two independent triggers land here: a send (which
89
+ * raises the buffer) and the `bufferedamountlow` drain event (which lowers it) — the latter has
90
+ * no send to hang the work off, which is why this is a shared entry point rather than a tail of
91
+ * `send`.
92
+ */
93
+ refreshBufferStatus(): void;
94
+ }
95
+ //# sourceMappingURL=FlowControlledDataChannel.d.ts.map
@@ -0,0 +1,41 @@
1
+ import type { NonSharedUint8Array } from '../../type-polyfills/non-shared-typed-arrays';
2
+ import type { FlowControlledDataChannelOptions } from './FlowControlledDataChannel';
3
+ import { FlowControlledDataChannel } from './FlowControlledDataChannel';
4
+ export interface LossyDataChannelOptions extends FlowControlledDataChannelOptions {
5
+ /**
6
+ * What to do with a send while the buffer is full: `drop` discards it to keep latency bounded
7
+ * (the classic lossy channel), `wait` backpressures the producer until there is headroom (the
8
+ * data-track channel, whose producer decides what to skip at frame granularity).
9
+ */
10
+ bufferFullBehavior: 'drop' | 'wait';
11
+ /** Sends are silently discarded while this is true (a reconnect attempt is underway). */
12
+ shouldSkipSends: () => boolean;
13
+ }
14
+ /**
15
+ * A lossy channel: flow control plus a per-instance full-buffer policy.
16
+ *
17
+ * Each instance owns its own byterate stat, drop counter, and (when tuning is started) the
18
+ * dynamic `bufferedAmountLowThreshold` adjustment that keeps the drop gate at roughly 100ms of
19
+ * buffered latency. Keeping these per instance is what prevents one channel's traffic from
20
+ * steering another channel's policy.
21
+ */
22
+ export declare class LossyDataChannel extends FlowControlledDataChannel {
23
+ private bufferFullBehavior;
24
+ private shouldSkipSends;
25
+ private statCurrentBytes;
26
+ private statByterate;
27
+ private statInterval;
28
+ private dropCount;
29
+ constructor(opts: LossyDataChannelOptions);
30
+ /** Sends prepared bytes with this channel's full-buffer policy (drop or wait). */
31
+ send(msg: NonSharedUint8Array): Promise<void>;
32
+ /**
33
+ * Starts the once-per-second adjustment of the channel's `bufferedAmountLowThreshold` to the
34
+ * observed byterate, keeping the drop gate at roughly 100ms of buffered latency (clamped to
35
+ * the watermarks). Restarts cleanly if already running.
36
+ */
37
+ startThresholdTuning(): void;
38
+ /** Stops the threshold tuning and resets the stats and drop counter. */
39
+ stopThresholdTuning(): void;
40
+ }
41
+ //# sourceMappingURL=LossyDataChannel.d.ts.map
@@ -0,0 +1,57 @@
1
+ import type { NonSharedUint8Array } from '../../type-polyfills/non-shared-typed-arrays';
2
+ import type { FlowControlledDataChannelOptions } from './FlowControlledDataChannel';
3
+ import { FlowControlledDataChannel } from './FlowControlledDataChannel';
4
+ export interface ReliableDataChannelOptions extends FlowControlledDataChannelOptions {
5
+ /**
6
+ * Whether sends should currently be deferred to the resume replay instead of hitting the wire
7
+ * (i.e. a reconnect attempt is underway). Read at send time so the reliable channel matches the
8
+ * engine's reconnect state without owning it.
9
+ */
10
+ isDeferringSends: () => boolean;
11
+ }
12
+ /**
13
+ * The reliable channel: flow control plus delivery-across-resume semantics.
14
+ *
15
+ * Every packet gets a monotonic sequence (stamped into the protobuf by the caller before
16
+ * serialization, via {@link nextSequence}) and is retained in a replay buffer until the channel's
17
+ * `bufferedAmount` confirms it has been handed to the transport. Sends that land in a reconnect
18
+ * window — or whose headroom wait is torn down transiently — are queued unsent and resolve;
19
+ * {@link replay} delivers them (plus any unacked packets) after a resume. Only an engine close
20
+ * rejects, because no replay is coming after that.
21
+ */
22
+ export declare class ReliableDataChannel extends FlowControlledDataChannel {
23
+ private messageBuffer;
24
+ private sequence;
25
+ private isDeferringSends;
26
+ constructor(opts: ReliableDataChannelOptions);
27
+ /**
28
+ * Claims the next packet sequence. The caller stamps it into the packet before serialization,
29
+ * then passes it back to {@link send} so the replay buffer stays keyed by wire sequence.
30
+ */
31
+ nextSequence(): number;
32
+ /**
33
+ * Sends prepared bytes with reliable semantics. Resolves once the packet has either been handed
34
+ * to the channel or queued for the resume replay; throws only when the engine is closed.
35
+ */
36
+ send(msg: NonSharedUint8Array, sequence: number): Promise<void>;
37
+ /**
38
+ * Replays the buffered backlog after a resume: drops everything the server acked
39
+ * (`lastMessageSeq`), then re-sends the rest in order. The headroom lock is held across the
40
+ * whole replay — releasing it between messages would let a concurrent send (whose newer
41
+ * sequence was already assigned before it queued on the lock) hit the wire mid-replay, and
42
+ * receivers would then discard the remaining lower-sequence resent messages as duplicates.
43
+ */
44
+ replay(lastMessageSeq: number): Promise<void>;
45
+ /**
46
+ * Before recomputing status, trim packets the transport has now delivered — a send or a drain
47
+ * may have acked buffered packets, and the replay buffer is keyed off the channel's buffered
48
+ * bytes.
49
+ */
50
+ refreshBufferStatus(): void;
51
+ /**
52
+ * Drops all replay state and restarts sequencing. Only valid on a full reconnect, where the
53
+ * session (and the receivers' sequence tracking) starts over.
54
+ */
55
+ reset(): void;
56
+ }
57
+ //# sourceMappingURL=ReliableDataChannel.d.ts.map
@@ -0,0 +1,12 @@
1
+ export declare enum DataChannelKind {
2
+ RELIABLE = 0,
3
+ LOSSY = 1,
4
+ DATA_TRACK_LOSSY = 2
5
+ }
6
+ export declare const reliableDataChannelWaterMarkLow: number;
7
+ export declare const reliableDataChannelWaterMarkHigh: number;
8
+ export declare const lossyDataChannelWaterMarkLow: number;
9
+ export declare const lossyDataChannelWaterMarkHigh: number;
10
+ export declare function dataChannelLowWaterMark(kind: DataChannelKind): number;
11
+ export declare function dataChannelHighWaterMark(kind: DataChannelKind): number;
12
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,49 @@
1
+ /*
2
+ * Compression helpers for data streams. The buffered deflate-raw variant ({@link deflateRawCompress})
3
+ * is for the inline (single-packet) case where the payload is small and bounded;
4
+ * {@link deflateRawTransform} / {@link inflateRawTransform} serve the chunked (multi-packet)
5
+ * `sendText`/`sendBytes`/`sendFile` paths, streaming the bytes through without buffering the whole
6
+ * payload.
7
+ *
8
+ * These operate on bytes (not strings) so a single set of helpers serves both text and byte streams;
9
+ * the `TextEncoder`/`TextDecoder` boundary lives at the manager/reader edges.
10
+ *
11
+ * Both streaming variants are exposed as `ReadableWritablePair`s so they drop straight into a
12
+ * `pipeThrough` chain. Each needs one localized cast to bridge a DOM lib-type mismatch: the platform
13
+ * `CompressionStream`/`DecompressionStream` type their `writable` as `WritableStream<BufferSource>`
14
+ * (a wider element type than `Uint8Array`), and `WritableStream<W>` is covariant in `W`, so neither
15
+ * is structurally a `ReadableWritablePair<Uint8Array, Uint8Array>` without help.
16
+ *
17
+ * @internal
18
+ */
19
+ import type { NonSharedUint8Array } from '../../type-polyfills/non-shared-typed-arrays';
20
+ /**
21
+ * A `deflate-raw` compression transform (inverse of {@link inflateRawTransform}): pipe a byte stream
22
+ * through it to get the compressed bytes without buffering the whole payload. Used for the chunked
23
+ * `sendText`/`sendBytes`/`sendFile` paths, where the full payload is known up front but is streamed
24
+ * (e.g. from `file.stream()`) rather than held in memory.
25
+ */
26
+ export declare function deflateRawTransform(): ReadableWritablePair<NonSharedUint8Array, NonSharedUint8Array>;
27
+ /**
28
+ * A `deflate-raw` decompression transform (inverse of {@link deflateRawTransform}): pipe a
29
+ * stream of compressed bytes through it to get the decompressed bytes. Inflate emits output greedily,
30
+ * so as long as the sender flushed at write boundaries each write's content is produced as soon as
31
+ * its compressed bytes arrive.
32
+ */
33
+ export declare function inflateRawTransform(): ReadableWritablePair<NonSharedUint8Array, NonSharedUint8Array>;
34
+ /** deflate-raw compresses a byte array in full. Use for inline payloads; prefer the streaming
35
+ * path for the chunked case. */
36
+ export declare function deflateRawCompress(data: NonSharedUint8Array): Promise<NonSharedUint8Array>;
37
+ /**
38
+ * Decompresses a raw-deflate byte array in full (inverse of {@link deflateRawCompress}).
39
+ * `maxByteLength` bounds the decompressed output (decompression-bomb guard); exceeding it rejects
40
+ * with a `PayloadTooLarge` error.
41
+ */
42
+ export declare function deflateRawDecompress(data: NonSharedUint8Array, maxByteLength?: number): Promise<NonSharedUint8Array>;
43
+ /**
44
+ * Drains a byte stream, concatenating all of its chunks into a single array. When
45
+ * `maxByteLength` is given, drops the stream and throws `PayloadTooLarge` as soon as the
46
+ * accumulated output exceeds it.
47
+ */
48
+ export declare function collect(stream: ReadableStream<NonSharedUint8Array>, maxByteLength?: number): Promise<NonSharedUint8Array>;
49
+ //# sourceMappingURL=compression.d.ts.map
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Maximum size of a single data-stream chunk in bytes, and the budget used to decide whether a
3
+ * payload can be sent inline as a single header packet. Kept below the ~16k data-channel MTU to
4
+ * leave headroom for protocol framing and E2EE overhead.
5
+ *
6
+ * @internal
7
+ */
8
+ export declare const STREAM_CHUNK_SIZE_BYTES = 15000;
9
+ /**
10
+ * Default cap on the number of decompressed bytes a single incoming compressed data stream may
11
+ * produce (5 GB). A tiny compressed payload can inflate to an arbitrarily large output
12
+ * (decompression bomb), so the decompressor's output is bounded rather than trusting the wire
13
+ * size; streams exceeding the cap error with `PayloadTooLarge`.
14
+ *
15
+ * @internal
16
+ */
17
+ export declare const DEFAULT_MAX_PAYLOAD_BYTE_LENGTH = 5000000000;
18
+ //# sourceMappingURL=constants.d.ts.map
@@ -3,6 +3,10 @@ import { Encryption_Type } from '@livekit/protocol';
3
3
  import type { ByteStreamHandler, TextStreamHandler } from './StreamReader';
4
4
  export default class IncomingDataStreamManager {
5
5
  private log;
6
+ /** Max number of decompressed bytes an incoming compressed stream may produce before it is
7
+ * errored (decompression-bomb guard). */
8
+ private maxPayloadByteLength;
9
+ constructor(maxPayloadByteLength?: number);
6
10
  private byteStreamControllers;
7
11
  private textStreamControllers;
8
12
  private byteStreamHandlers;
@@ -1,6 +1,7 @@
1
+ import { ClientInfo_Capability } from '@livekit/protocol';
1
2
  import type { StructuredLogger } from '../../../logger';
2
3
  import type RTCEngine from '../../RTCEngine';
3
- import type { SendFileOptions, SendTextOptions, StreamBytesOptions, StreamTextOptions, TextStreamInfo } from '../../types';
4
+ import type { ByteStreamInfo, SendBytesOptions, SendFileOptions, SendTextOptions, StreamBytesOptions, StreamTextOptions, TextStreamInfo } from '../../types';
4
5
  import { ByteStreamWriter, TextStreamWriter } from './StreamWriter';
5
6
  /**
6
7
  * Manages sending custom user data via data channels.
@@ -9,10 +10,49 @@ import { ByteStreamWriter, TextStreamWriter } from './StreamWriter';
9
10
  export default class OutgoingDataStreamManager {
10
11
  protected engine: RTCEngine;
11
12
  protected log: StructuredLogger;
12
- constructor(engine: RTCEngine, log: StructuredLogger);
13
+ /** Returns the advertised client protocol of a remote participant, used to decide whether a
14
+ * recipient can receive single-packet (inline) data streams. */
15
+ protected getRemoteParticipantClientProtocol: (identity: string) => number;
16
+ /** Returns the client capabilities a remote participant advertises, used to decide whether a
17
+ * recipient can decompress a deflate-raw compressed stream. */
18
+ protected getRemoteParticipantCapabilities: (identity: string) => Array<ClientInfo_Capability>;
19
+ /** Returns the identities of every remote participant currently in the room, used to decide
20
+ * whether a broadcast (no explicit destinations) can be sent inline. */
21
+ protected getAllRemoteParticipantIdentities: () => Array<string>;
22
+ constructor(engine: RTCEngine, log: StructuredLogger, getRemoteParticipantClientProtocol: (identity: string) => number, getRemoteParticipantCapabilities: (identity: string) => Array<ClientInfo_Capability>, getAllRemoteParticipantIdentities: () => Array<string>);
13
23
  setupEngine(engine: RTCEngine): void;
14
24
  /** {@inheritDoc LocalParticipant.sendText} */
15
25
  sendText(text: string, options?: SendTextOptions): Promise<TextStreamInfo>;
26
+ /**
27
+ * Sends a complete in-memory byte payload. Mirrors {@link sendText}'s semantics: when every
28
+ * recipient supports data streams v2 the payload rides inline in a single header packet
29
+ * (optionally deflate-raw compressed), otherwise it is sent as a (optionally compressed)
30
+ * chunked byte stream. Unlike {@link sendFile}, the whole payload is already in memory, so the
31
+ * inline single-packet fast path applies.
32
+ */
33
+ sendBytes(bytes: Uint8Array, options?: SendBytesOptions): Promise<ByteStreamInfo>;
34
+ /**
35
+ * Returns true only if every recipient is known to support data streams v2 (single-packet inline
36
+ * streams and compression). For a targeted send this checks the named destination identities; for
37
+ * a broadcast (no explicit destinations) it checks every remote participant currently in the room.
38
+ * An empty room (nobody to receive) is considered eligible.
39
+ */
40
+ private allRecipientsSupportV2;
41
+ /**
42
+ * Returns true only if every recipient advertises the deflate-raw compression capability (so it
43
+ * can decompress a compressed stream). Resolved the same way as {@link allRecipientsSupportV2}:
44
+ * named destinations, or every remote participant for a broadcast; an empty room is eligible.
45
+ */
46
+ private allRecipientsSupportCompression;
47
+ /**
48
+ * Shared chunked-stream send for `sendText`/`sendFile`: sends the prebuilt header packet, then
49
+ * forwards `source` (optionally deflate-raw compressed) as `streamChunk` packets re-chunked to
50
+ * the MTU budget with contiguous indices, then sends the trailer. The source is consumed
51
+ * incrementally, so a `file.stream()` is never buffered in full. The platform compressor can't
52
+ * flush mid-stream, so compression is only used when the whole payload is available as a stream
53
+ * up front (not for incremental writers like `streamText`/`streamBytes`).
54
+ */
55
+ private sendChunkedByteStream;
16
56
  /**
17
57
  * @internal
18
58
  */
@@ -20,6 +60,13 @@ export default class OutgoingDataStreamManager {
20
60
  sendFile(file: File, options?: SendFileOptions): Promise<{
21
61
  id: string;
22
62
  }>;
63
+ /**
64
+ * Streams a file as a chunked byte stream, compressed (deflate-raw) when the runtime supports it
65
+ * and every recipient is on data streams v2. The file is piped `file.stream()` →
66
+ * (`CompressionStream`) → chunk packets via {@link sendChunkedByteStream}, so it is never fully
67
+ * buffered in memory — unlike {@link sendBytes}, there is no inline single-packet fast path for
68
+ * files (the compressed size can't be known up front without buffering the whole file).
69
+ */
23
70
  private _sendFile;
24
71
  streamBytes(options?: StreamBytesOptions): Promise<ByteStreamWriter>;
25
72
  }
@@ -0,0 +1,18 @@
1
+ import { DataPacket, DataStream_CompressionType, DataStream_Header } from '@livekit/protocol';
2
+ import type { ByteStreamInfo, StreamTextOptions, TextStreamInfo } from '../../types';
3
+ /** The data-streams-v2 wire signals carried directly on the header: the compression flag and the
4
+ * inline single-packet payload. Both used to live in reserved header attributes; they are now
5
+ * first-class protobuf fields on `DataStream.Header`. */
6
+ export interface StreamHeaderV2Fields {
7
+ /** Compression applied to the inline/chunked payload. Defaults to `NONE` when omitted. */
8
+ compression?: DataStream_CompressionType;
9
+ /** The full payload smuggled into the header for single-packet (inline) sends. */
10
+ inlineContent?: Uint8Array;
11
+ }
12
+ /** Builds the `DataStream_Header` for a text stream from its info and stream options. */
13
+ export declare function buildTextStreamHeader(info: TextStreamInfo, options?: Pick<StreamTextOptions, 'version' | 'replyToStreamId' | 'type'>, v2?: StreamHeaderV2Fields): DataStream_Header;
14
+ /** Builds the `DataStream_Header` for a byte stream from its info. */
15
+ export declare function buildByteStreamHeader(info: ByteStreamInfo, v2?: StreamHeaderV2Fields): DataStream_Header;
16
+ /** Wraps a `DataStream_Header` in a `DataPacket` ready to be sent over a data channel. */
17
+ export declare function createStreamHeaderPacket(header: DataStream_Header, destinationIdentities?: Array<string>): DataPacket;
18
+ //# sourceMappingURL=header-utils.d.ts.map
@@ -135,7 +135,9 @@ export declare enum DataStreamErrorReason {
135
135
  LengthExceeded = 3,
136
136
  Incomplete = 4,
137
137
  HandlerAlreadyRegistered = 7,
138
- EncryptionTypeMismatch = 8
138
+ EncryptionTypeMismatch = 8,
139
+ HeaderTooLarge = 9,
140
+ PayloadTooLarge = 10
139
141
  }
140
142
  export declare class DataStreamError extends LivekitReasonedError<DataStreamErrorReason> {
141
143
  readonly name = "DataStreamError";
@@ -14,7 +14,7 @@ import LocalTrack from '../track/LocalTrack';
14
14
  import LocalTrackPublication from '../track/LocalTrackPublication';
15
15
  import { Track } from '../track/Track';
16
16
  import type { AudioCaptureOptions, BackupVideoCodec, CreateLocalTracksOptions, ScreenShareCaptureOptions, TrackPublishOptions, VideoCaptureOptions } from '../track/options';
17
- import type { ChatMessage, DataPublishOptions, SendFileOptions, SendTextOptions, StreamBytesOptions, StreamTextOptions, TextStreamInfo } from '../types';
17
+ import type { ByteStreamInfo, ChatMessage, DataPublishOptions, SendBytesOptions, SendFileOptions, SendTextOptions, StreamBytesOptions, StreamTextOptions, TextStreamInfo } from '../types';
18
18
  import Participant from './Participant';
19
19
  import type { ParticipantTrackPermission } from './ParticipantTrackPermission';
20
20
  import type RemoteParticipant from './RemoteParticipant';
@@ -208,6 +208,14 @@ export default class LocalParticipant extends Participant {
208
208
  sendFile(file: File, options?: SendFileOptions): Promise<{
209
209
  id: string;
210
210
  }>;
211
+ /**
212
+ * Sends the given bytes to participants in the room via the data channel.
213
+ * For files, consider using {@link sendFile}; for longer/incremental payloads, {@link streamBytes}.
214
+ *
215
+ * @param bytes The byte payload
216
+ * @param options.topic Topic identifier used to route the stream to appropriate handlers.
217
+ */
218
+ sendBytes(bytes: Uint8Array, options?: SendBytesOptions): Promise<ByteStreamInfo>;
211
219
  /**
212
220
  * Stream bytes incrementally to participants in the room via the data channel.
213
221
  * For sending files, consider using {@link sendFile} instead.
@@ -1,4 +1,5 @@
1
1
  import type { ParticipantInfo } from '@livekit/protocol';
2
+ import { ClientInfo_Capability } from '@livekit/protocol';
2
3
  import type { SignalClient } from '../../api/SignalClient';
3
4
  import { DeferrableMap } from '../../utils/deferrable-map';
4
5
  import RemoteDataTrack from '../data-track/RemoteDataTrack';
@@ -26,6 +27,15 @@ export default class RemoteParticipant extends Participant {
26
27
  * @internal
27
28
  **/
28
29
  clientProtocol: number;
30
+ /** The client capabilities the remote participant advertises (e.g. deflate-raw compression
31
+ * support). Used to decide which peer-to-peer features can be used when sending to them.
32
+ *
33
+ * Differs from clientProtocol in that these are truely optional "additions" which can be used
34
+ * or not depending on client specific attributes rather than protocol level invariants.
35
+ *
36
+ * @internal
37
+ **/
38
+ capabilities: Array<ClientInfo_Capability>;
29
39
  private volumeMap;
30
40
  private audioOutput?;
31
41
  /** @internal */
@@ -35,7 +45,7 @@ export default class RemoteParticipant extends Participant {
35
45
  remoteParticipant: string;
36
46
  };
37
47
  /** @internal */
38
- constructor(signalClient: SignalClient, sid: string, identity?: string, name?: string, metadata?: string, attributes?: Record<string, string>, loggerOptions?: LoggerOptions, kind?: ParticipantKind, remoteDataTracks?: Array<RemoteDataTrack>, clientProtocol?: number);
48
+ constructor(signalClient: SignalClient, sid: string, identity?: string, name?: string, metadata?: string, attributes?: Record<string, string>, loggerOptions?: LoggerOptions, kind?: ParticipantKind, remoteDataTracks?: Array<RemoteDataTrack>, clientProtocol?: number, capabilities?: Array<ClientInfo_Capability>);
39
49
  protected addTrackPublication(publication: RemoteTrackPublication): void;
40
50
  getTrackPublication(source: Track.Source): RemoteTrackPublication | undefined;
41
51
  getTrackPublicationByName(name: string): RemoteTrackPublication | undefined;
@@ -18,6 +18,20 @@ export interface SendTextOptions {
18
18
  attachments?: Array<File>;
19
19
  onProgress?: (progress: number) => void;
20
20
  attributes?: Record<string, string>;
21
+ /** Whether to compress the payload (deflate-raw). Defaults to true. Compression is only applied
22
+ * when every recipient supports data streams v2 and the runtime can compress. */
23
+ compress?: boolean;
24
+ }
25
+ export interface SendBytesOptions {
26
+ topic?: string;
27
+ destinationIdentities?: Array<string>;
28
+ attributes?: Record<string, string>;
29
+ onProgress?: (progress: number) => void;
30
+ /** Whether to compress the payload (deflate-raw). Defaults to true. Compression is only applied
31
+ * when every recipient supports data streams v2 and the runtime can compress. */
32
+ compress?: boolean;
33
+ name?: string;
34
+ mimeType?: string;
21
35
  }
22
36
  export interface StreamTextOptions {
23
37
  topic?: string;
@@ -42,6 +56,9 @@ export type StreamBytesOptions = {
42
56
  export type SendFileOptions = Pick<StreamBytesOptions, 'topic' | 'mimeType' | 'destinationIdentities'> & {
43
57
  onProgress?: (progress: number) => void;
44
58
  encryptionType?: Encryption_Type.NONE;
59
+ /** Whether to compress the payload (deflate-raw). Defaults to true. Compression is only applied
60
+ * when every recipient supports data streams v2 and the runtime can compress. */
61
+ compress?: boolean;
45
62
  };
46
63
  export type DataPublishOptions = {
47
64
  /**
@@ -146,6 +146,19 @@ export declare function isRemoteVideoTrack(track: Track | undefined): track is R
146
146
  export declare function isLocalParticipant(p: Participant): p is LocalParticipant;
147
147
  export declare function isRemoteParticipant(p: Participant): p is RemoteParticipant;
148
148
  export declare function splitUtf8(s: string, n: number): NonSharedUint8Array[];
149
+ /** Wraps a byte array in a `ReadableStream` that yields it as a single chunk and then closes. */
150
+ export declare function readableFromBytes(bytes: NonSharedUint8Array): ReadableStream<NonSharedUint8Array>;
151
+ /**
152
+ * Re-chunks a byte stream into pieces of exactly `chunkSize` bytes (the final piece may be
153
+ * smaller), coalescing or splitting the source's pieces as needed. Memory use is bounded to roughly
154
+ * `chunkSize` plus one source read, so it never buffers the whole stream — used to pack
155
+ * `CompressionStream`/`file.stream()` output into MTU-sized data-stream chunks.
156
+ */
157
+ export declare function readBytesInChunks(source: ReadableStream<NonSharedUint8Array>, chunkSize: number): AsyncGenerator<NonSharedUint8Array>;
158
+ /** Encodes a byte array as a base64 string (suitable for embedding binary data in a string field). */
159
+ export declare function encodeBase64(bytes: Uint8Array): string;
160
+ /** Decodes a base64 string (as produced by {@link encodeBase64}) back into a byte array. */
161
+ export declare function decodeBase64(base64: string): Uint8Array;
149
162
  export declare function extractMaxAgeFromRequestHeaders(headers: Headers): number | undefined;
150
163
  export declare function isCompressionStreamSupported(): boolean;
151
164
  export declare function isPublisherOfferWithJoinSupported(): boolean;
@@ -5,7 +5,11 @@ export declare const CLIENT_PROTOCOL_DEFAULT = 0;
5
5
  /** Replaces RPC v1 protocol with a v2 data streams based one to support unlimited request /
6
6
  * response payload length. */
7
7
  export declare const CLIENT_PROTOCOL_DATA_STREAM_RPC = 1;
8
+ /** "Data streams v2": the client knows how to receive a single-packet data stream (a stream whose
9
+ * entire payload is smuggled into the header packet, with no chunk/trailer packets). Senders only
10
+ * use the single-packet optimization when the recipient advertises at least this protocol. */
11
+ export declare const CLIENT_PROTOCOL_DATA_STREAM_V2 = 2;
8
12
  /** The client protocol version indicates what level of support that the client has for
9
13
  * client <-> client api interactions. */
10
- export declare const clientProtocol = 1;
14
+ export declare const clientProtocol = 2;
11
15
  //# sourceMappingURL=version.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "livekit-client",
3
- "version": "2.20.2",
3
+ "version": "2.21.0",
4
4
  "description": "JavaScript/TypeScript client SDK for LiveKit",
5
5
  "main": "./dist/livekit-client.umd.js",
6
6
  "unpkg": "./dist/livekit-client.umd.js",
@@ -50,7 +50,7 @@
50
50
  "license": "Apache-2.0",
51
51
  "dependencies": {
52
52
  "@livekit/mutex": "1.1.1",
53
- "@livekit/protocol": "1.46.6",
53
+ "@livekit/protocol": "1.50.4",
54
54
  "events": "^3.3.0",
55
55
  "jose": "^6.1.0",
56
56
  "loglevel": "^1.9.2",
package/src/index.ts CHANGED
@@ -104,6 +104,8 @@ export type {
104
104
  TranscriptionSegment,
105
105
  ChatMessage,
106
106
  SendTextOptions,
107
+ SendBytesOptions,
108
+ ByteStreamInfo,
107
109
  } from './room/types';
108
110
  export * from './version';
109
111
  export {