livekit-client 2.20.1 → 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 (156) 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 +37 -7
  4. package/dist/livekit-client.e2ee.worker.mjs.map +1 -1
  5. package/dist/livekit-client.esm.mjs +2279 -651
  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 +37 -7
  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/api/SignalClient.d.ts.map +1 -1
  14. package/dist/src/index.d.ts +1 -1
  15. package/dist/src/index.d.ts.map +1 -1
  16. package/dist/src/logger.d.ts +3 -1
  17. package/dist/src/logger.d.ts.map +1 -1
  18. package/dist/src/room/PCTransport.d.ts +2 -0
  19. package/dist/src/room/PCTransport.d.ts.map +1 -1
  20. package/dist/src/room/PCTransportManager.d.ts +1 -0
  21. package/dist/src/room/PCTransportManager.d.ts.map +1 -1
  22. package/dist/src/room/RTCEngine.d.ts +31 -26
  23. package/dist/src/room/RTCEngine.d.ts.map +1 -1
  24. package/dist/src/room/RegionUrlProvider.d.ts.map +1 -1
  25. package/dist/src/room/Room.d.ts +4 -0
  26. package/dist/src/room/Room.d.ts.map +1 -1
  27. package/dist/src/room/data-channel/DataChannelManager.d.ts +62 -0
  28. package/dist/src/room/data-channel/DataChannelManager.d.ts.map +1 -0
  29. package/dist/src/room/data-channel/FlowControlledDataChannel.d.ts +95 -0
  30. package/dist/src/room/data-channel/FlowControlledDataChannel.d.ts.map +1 -0
  31. package/dist/src/room/data-channel/LossyDataChannel.d.ts +40 -0
  32. package/dist/src/room/data-channel/LossyDataChannel.d.ts.map +1 -0
  33. package/dist/src/room/data-channel/ReliableDataChannel.d.ts +56 -0
  34. package/dist/src/room/data-channel/ReliableDataChannel.d.ts.map +1 -0
  35. package/dist/src/room/data-channel/types.d.ts +12 -0
  36. package/dist/src/room/data-channel/types.d.ts.map +1 -0
  37. package/dist/src/room/data-stream/compression.d.ts +49 -0
  38. package/dist/src/room/data-stream/compression.d.ts.map +1 -0
  39. package/dist/src/room/data-stream/constants.d.ts +18 -0
  40. package/dist/src/room/data-stream/constants.d.ts.map +1 -0
  41. package/dist/src/room/data-stream/incoming/IncomingDataStreamManager.d.ts +4 -0
  42. package/dist/src/room/data-stream/incoming/IncomingDataStreamManager.d.ts.map +1 -1
  43. package/dist/src/room/data-stream/incoming/StreamReader.d.ts.map +1 -1
  44. package/dist/src/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts +49 -2
  45. package/dist/src/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts.map +1 -1
  46. package/dist/src/room/data-stream/outgoing/header-utils.d.ts +18 -0
  47. package/dist/src/room/data-stream/outgoing/header-utils.d.ts.map +1 -0
  48. package/dist/src/room/data-track/incoming/IncomingDataTrackManager.d.ts +7 -0
  49. package/dist/src/room/data-track/incoming/IncomingDataTrackManager.d.ts.map +1 -1
  50. package/dist/src/room/data-track/outgoing/OutgoingDataTrackManager.d.ts.map +1 -1
  51. package/dist/src/room/errors.d.ts +3 -1
  52. package/dist/src/room/errors.d.ts.map +1 -1
  53. package/dist/src/room/participant/LocalParticipant.d.ts +9 -1
  54. package/dist/src/room/participant/LocalParticipant.d.ts.map +1 -1
  55. package/dist/src/room/participant/Participant.d.ts.map +1 -1
  56. package/dist/src/room/participant/RemoteParticipant.d.ts +11 -2
  57. package/dist/src/room/participant/RemoteParticipant.d.ts.map +1 -1
  58. package/dist/src/room/participant/publishUtils.d.ts +7 -0
  59. package/dist/src/room/participant/publishUtils.d.ts.map +1 -1
  60. package/dist/src/room/rpc/client/RpcClientManager.d.ts.map +1 -1
  61. package/dist/src/room/rpc/server/RpcServerManager.d.ts.map +1 -1
  62. package/dist/src/room/track/LocalAudioTrack.d.ts +1 -0
  63. package/dist/src/room/track/LocalAudioTrack.d.ts.map +1 -1
  64. package/dist/src/room/track/Track.d.ts.map +1 -1
  65. package/dist/src/room/track/options.d.ts +11 -1
  66. package/dist/src/room/track/options.d.ts.map +1 -1
  67. package/dist/src/room/types.d.ts +17 -0
  68. package/dist/src/room/types.d.ts.map +1 -1
  69. package/dist/src/room/utils.d.ts +13 -0
  70. package/dist/src/room/utils.d.ts.map +1 -1
  71. package/dist/src/test/signalToken.d.ts +26 -0
  72. package/dist/src/test/signalToken.d.ts.map +1 -0
  73. package/dist/src/utils/dataPacketBuffer.d.ts +18 -0
  74. package/dist/src/utils/dataPacketBuffer.d.ts.map +1 -1
  75. package/dist/src/version.d.ts +5 -1
  76. package/dist/src/version.d.ts.map +1 -1
  77. package/dist/ts4.2/index.d.ts +1 -1
  78. package/dist/ts4.2/logger.d.ts +3 -1
  79. package/dist/ts4.2/room/PCTransport.d.ts +2 -0
  80. package/dist/ts4.2/room/PCTransportManager.d.ts +1 -0
  81. package/dist/ts4.2/room/RTCEngine.d.ts +31 -26
  82. package/dist/ts4.2/room/Room.d.ts +4 -0
  83. package/dist/ts4.2/room/data-channel/DataChannelManager.d.ts +62 -0
  84. package/dist/ts4.2/room/data-channel/FlowControlledDataChannel.d.ts +95 -0
  85. package/dist/ts4.2/room/data-channel/LossyDataChannel.d.ts +41 -0
  86. package/dist/ts4.2/room/data-channel/ReliableDataChannel.d.ts +57 -0
  87. package/dist/ts4.2/room/data-channel/types.d.ts +12 -0
  88. package/dist/ts4.2/room/data-stream/compression.d.ts +49 -0
  89. package/dist/ts4.2/room/data-stream/constants.d.ts +18 -0
  90. package/dist/ts4.2/room/data-stream/incoming/IncomingDataStreamManager.d.ts +4 -0
  91. package/dist/ts4.2/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts +49 -2
  92. package/dist/ts4.2/room/data-stream/outgoing/header-utils.d.ts +18 -0
  93. package/dist/ts4.2/room/data-track/incoming/IncomingDataTrackManager.d.ts +7 -0
  94. package/dist/ts4.2/room/errors.d.ts +3 -1
  95. package/dist/ts4.2/room/participant/LocalParticipant.d.ts +9 -1
  96. package/dist/ts4.2/room/participant/RemoteParticipant.d.ts +11 -1
  97. package/dist/ts4.2/room/participant/publishUtils.d.ts +7 -0
  98. package/dist/ts4.2/room/track/LocalAudioTrack.d.ts +1 -0
  99. package/dist/ts4.2/room/track/options.d.ts +11 -1
  100. package/dist/ts4.2/room/types.d.ts +17 -0
  101. package/dist/ts4.2/room/utils.d.ts +13 -0
  102. package/dist/ts4.2/test/signalToken.d.ts +26 -0
  103. package/dist/ts4.2/utils/dataPacketBuffer.d.ts +18 -0
  104. package/dist/ts4.2/version.d.ts +5 -1
  105. package/package.json +8 -4
  106. package/src/api/SignalClient.e2e.test.ts +264 -0
  107. package/src/api/SignalClient.ts +34 -2
  108. package/src/index.ts +2 -0
  109. package/src/logger.ts +2 -0
  110. package/src/room/PCTransport.ts +66 -31
  111. package/src/room/PCTransportManager.ts +10 -6
  112. package/src/room/RTCEngine.test.ts +238 -6
  113. package/src/room/RTCEngine.ts +101 -338
  114. package/src/room/RegionUrlProvider.ts +4 -2
  115. package/src/room/Room.test.ts +4 -1
  116. package/src/room/Room.ts +47 -9
  117. package/src/room/data-channel/DataChannelManager.test.ts +152 -0
  118. package/src/room/data-channel/DataChannelManager.ts +237 -0
  119. package/src/room/data-channel/FlowControlledDataChannel.test.ts +154 -0
  120. package/src/room/data-channel/FlowControlledDataChannel.ts +216 -0
  121. package/src/room/data-channel/LossyDataChannel.test.ts +118 -0
  122. package/src/room/data-channel/LossyDataChannel.ts +125 -0
  123. package/src/room/data-channel/ReliableDataChannel.test.ts +158 -0
  124. package/src/room/data-channel/ReliableDataChannel.ts +153 -0
  125. package/src/room/data-channel/types.ts +30 -0
  126. package/src/room/data-stream/compression.ts +117 -0
  127. package/src/room/data-stream/constants.ts +18 -0
  128. package/src/room/data-stream/incoming/IncomingDataStreamManager.test.ts +1601 -0
  129. package/src/room/data-stream/incoming/IncomingDataStreamManager.ts +438 -86
  130. package/src/room/data-stream/incoming/StreamReader.ts +8 -2
  131. package/src/room/data-stream/outgoing/OutgoingDataStreamManager.test.ts +1292 -0
  132. package/src/room/data-stream/outgoing/OutgoingDataStreamManager.ts +489 -116
  133. package/src/room/data-stream/outgoing/header-utils.ts +87 -0
  134. package/src/room/data-track/incoming/IncomingDataTrackManager.test.ts +303 -0
  135. package/src/room/data-track/incoming/IncomingDataTrackManager.ts +70 -8
  136. package/src/room/data-track/outgoing/OutgoingDataTrackManager.ts +2 -0
  137. package/src/room/errors.ts +10 -4
  138. package/src/room/participant/LocalParticipant.ts +29 -6
  139. package/src/room/participant/Participant.ts +5 -0
  140. package/src/room/participant/RemoteParticipant.ts +19 -5
  141. package/src/room/participant/publishUtils.ts +14 -11
  142. package/src/room/rpc/client/RpcClientManager.test.ts +20 -27
  143. package/src/room/rpc/client/RpcClientManager.ts +1 -4
  144. package/src/room/rpc/server/RpcServerManager.test.ts +24 -23
  145. package/src/room/rpc/server/RpcServerManager.ts +1 -3
  146. package/src/room/track/LocalAudioTrack.ts +16 -0
  147. package/src/room/track/Track.ts +10 -6
  148. package/src/room/track/options.ts +11 -1
  149. package/src/room/types.ts +18 -0
  150. package/src/room/utils.ts +67 -0
  151. package/src/test/signalServerSetup.ts +151 -0
  152. package/src/test/signalToken.ts +59 -0
  153. package/src/test/vitest-context.d.ts +10 -0
  154. package/src/utils/dataPacketBuffer.test.ts +100 -0
  155. package/src/utils/dataPacketBuffer.ts +46 -1
  156. package/src/version.ts +5 -1
@@ -1,8 +1,9 @@
1
- import type {
2
- ParticipantInfo,
3
- SubscriptionError,
4
- UpdateSubscription,
5
- UpdateTrackSettings,
1
+ import {
2
+ ClientInfo_Capability,
3
+ type ParticipantInfo,
4
+ type SubscriptionError,
5
+ type UpdateSubscription,
6
+ type UpdateTrackSettings,
6
7
  } from '@livekit/protocol';
7
8
  import type { SignalClient } from '../../api/SignalClient';
8
9
  import { DeferrableMap } from '../../utils/deferrable-map';
@@ -47,6 +48,16 @@ export default class RemoteParticipant extends Participant {
47
48
  **/
48
49
  clientProtocol: number;
49
50
 
51
+ /** The client capabilities the remote participant advertises (e.g. deflate-raw compression
52
+ * support). Used to decide which peer-to-peer features can be used when sending to them.
53
+ *
54
+ * Differs from clientProtocol in that these are truely optional "additions" which can be used
55
+ * or not depending on client specific attributes rather than protocol level invariants.
56
+ *
57
+ * @internal
58
+ **/
59
+ capabilities: Array<ClientInfo_Capability>;
60
+
50
61
  private volumeMap: Map<Track.Source, number>;
51
62
 
52
63
  private audioOutput?: AudioOutputOptions;
@@ -72,6 +83,7 @@ export default class RemoteParticipant extends Participant {
72
83
  return new RemoteDataTrack(info, manager, { publisherIdentity: pi.identity });
73
84
  }),
74
85
  pi.clientProtocol,
86
+ pi.capabilities,
75
87
  );
76
88
  }
77
89
 
@@ -95,6 +107,7 @@ export default class RemoteParticipant extends Participant {
95
107
  kind: ParticipantKind = ParticipantKind.STANDARD,
96
108
  remoteDataTracks: Array<RemoteDataTrack> = [],
97
109
  clientProtocol: number = CLIENT_PROTOCOL_DEFAULT,
110
+ capabilities: Array<ClientInfo_Capability> = [],
98
111
  ) {
99
112
  super(sid, identity || '', name, metadata, attributes, loggerOptions, kind);
100
113
  this.signalClient = signalClient;
@@ -108,6 +121,7 @@ export default class RemoteParticipant extends Participant {
108
121
  );
109
122
  this.volumeMap = new Map();
110
123
  this.clientProtocol = clientProtocol;
124
+ this.capabilities = capabilities;
111
125
  }
112
126
 
113
127
  protected addTrackPublication(publication: RemoteTrackPublication) {
@@ -19,7 +19,6 @@ import {
19
19
  isSVCCodec,
20
20
  isSafariBased,
21
21
  isSafariSvcApi,
22
- unwrapConstraint,
23
22
  } from '../utils';
24
23
 
25
24
  /** @internal */
@@ -468,16 +467,20 @@ export class ScalabilityMode {
468
467
  }
469
468
  }
470
469
 
470
+ /**
471
+ * Returns the appropriate degradation preference for a video track based on its source.
472
+ *
473
+ * - Camera: 'maintain-framerate' (smoother video for real-time communication)
474
+ * - Screen share: 'maintain-resolution' (clarity is critical for reading text/UI)
475
+ * - Other/unknown: 'balanced'
476
+ */
471
477
  export function getDefaultDegradationPreference(track: LocalVideoTrack): RTCDegradationPreference {
472
- // a few of reasons we have different default paths:
473
- // 1. without this, Chrome seems to aggressively resize the SVC video stating `quality-limitation: bandwidth` even when BW isn't an issue
474
- // 2. since we are overriding contentHint to motion (to workaround L1T3 publishing), it overrides the default degradationPreference to `balanced`
475
- if (
476
- track.source === Track.Source.ScreenShare ||
477
- (track.constraints.height && unwrapConstraint(track.constraints.height) >= 1080)
478
- ) {
479
- return 'maintain-resolution';
480
- } else {
481
- return 'balanced';
478
+ switch (track.source) {
479
+ case Track.Source.Camera:
480
+ return 'maintain-framerate';
481
+ case Track.Source.ScreenShare:
482
+ return 'maintain-resolution';
483
+ default:
484
+ return 'balanced';
482
485
  }
483
486
  }
@@ -16,6 +16,8 @@ describe('RpcClientManager', () => {
16
16
  const outgoingDataStreamManager = new OutgoingDataStreamManager(
17
17
  {} as unknown as RTCEngine,
18
18
  log,
19
+ (_identity) => CLIENT_PROTOCOL_DEFAULT,
20
+ () => [],
19
21
  );
20
22
 
21
23
  rpcClientManager = new RpcClientManager(
@@ -132,19 +134,13 @@ describe('RpcClientManager', () => {
132
134
 
133
135
  describe('v2 -> v2', () => {
134
136
  let rpcClientManager: RpcClientManager;
135
- let mockStreamTextWriter: {
136
- write: ReturnType<typeof vi.fn>;
137
- close: ReturnType<typeof vi.fn>;
138
- };
137
+ let sendTextMock: ReturnType<typeof vi.fn>;
139
138
  let mockOutgoingDataStreamManager: OutgoingDataStreamManager;
140
139
 
141
140
  beforeEach(() => {
142
- mockStreamTextWriter = {
143
- write: vi.fn().mockResolvedValue(undefined),
144
- close: vi.fn().mockResolvedValue(undefined),
145
- };
141
+ sendTextMock = vi.fn().mockResolvedValue(undefined);
146
142
  mockOutgoingDataStreamManager = {
147
- streamText: vi.fn().mockResolvedValue(mockStreamTextWriter),
143
+ sendText: sendTextMock,
148
144
  } as unknown as OutgoingDataStreamManager;
149
145
 
150
146
  rpcClientManager = new RpcClientManager(
@@ -171,7 +167,8 @@ describe('RpcClientManager', () => {
171
167
  });
172
168
 
173
169
  // Verify the data stream was used with correct attributes
174
- expect(mockOutgoingDataStreamManager.streamText).toHaveBeenCalledWith(
170
+ expect(mockOutgoingDataStreamManager.sendText).toHaveBeenCalledWith(
171
+ 'request-payload',
175
172
  expect.objectContaining({
176
173
  topic: RPC_REQUEST_DATA_STREAM_TOPIC,
177
174
  destinationIdentities: ['destination-identity'],
@@ -182,8 +179,6 @@ describe('RpcClientManager', () => {
182
179
  }),
183
180
  }),
184
181
  );
185
- expect(mockStreamTextWriter.write).toHaveBeenCalledWith('request-payload');
186
- expect(mockStreamTextWriter.close).toHaveBeenCalled();
187
182
 
188
183
  // No packet should have been emitted
189
184
  expect(managerEvents.areThereBufferedEvents('sendDataPacket')).toBe(false);
@@ -213,7 +208,8 @@ describe('RpcClientManager', () => {
213
208
  });
214
209
 
215
210
  // Verify the data stream was used with correct attributes
216
- expect(mockOutgoingDataStreamManager.streamText).toHaveBeenCalledWith(
211
+ expect(mockOutgoingDataStreamManager.sendText).toHaveBeenCalledWith(
212
+ longPayload,
217
213
  expect.objectContaining({
218
214
  topic: RPC_REQUEST_DATA_STREAM_TOPIC,
219
215
  destinationIdentities: ['destination-identity'],
@@ -224,8 +220,6 @@ describe('RpcClientManager', () => {
224
220
  }),
225
221
  }),
226
222
  );
227
- expect(mockStreamTextWriter.write).toHaveBeenCalledWith(longPayload);
228
- expect(mockStreamTextWriter.close).toHaveBeenCalled();
229
223
 
230
224
  // No packet should have been emitted
231
225
  expect(managerEvents.areThereBufferedEvents('sendDataPacket')).toBe(false);
@@ -337,14 +331,14 @@ describe('RpcClientManager', () => {
337
331
  });
338
332
 
339
333
  it('should not drop ack and response that arrive before publish completes', async () => {
340
- // Hold the publish path open by blocking writer.close() until we explicitly resolve it.
341
- let resolveClose!: () => void;
342
- const closeBlocked = new Promise<void>((resolve) => {
343
- resolveClose = resolve;
334
+ // Hold the publish path open by blocking sendText() until we explicitly resolve it.
335
+ let resolveSend!: () => void;
336
+ const sendBlocked = new Promise<void>((resolve) => {
337
+ resolveSend = resolve;
344
338
  });
345
- mockStreamTextWriter.close = vi.fn().mockReturnValue(closeBlocked);
339
+ sendTextMock.mockReturnValue(sendBlocked);
346
340
 
347
- // Start performRpc but don't await its return yet. The synchronous prefix runs streamText.
341
+ // Start performRpc but don't await its return yet. The synchronous prefix runs sendText.
348
342
  const performRpcPromise = rpcClientManager.performRpc({
349
343
  destinationIdentity: 'destination-identity',
350
344
  method: 'test-method',
@@ -352,11 +346,10 @@ describe('RpcClientManager', () => {
352
346
  responseTimeout: 200,
353
347
  });
354
348
 
355
- // streamText was called synchronously; pull the request id out of the attributes.
356
- const streamTextCalls = (mockOutgoingDataStreamManager.streamText as ReturnType<typeof vi.fn>)
357
- .mock.calls;
358
- expect(streamTextCalls.length).toBe(1);
359
- const requestId = streamTextCalls[0][0].attributes[RpcRequestAttrs.RPC_REQUEST_ID];
349
+ // sendText was called synchronously; pull the request id out of the attributes.
350
+ const sendTextCalls = sendTextMock.mock.calls;
351
+ expect(sendTextCalls.length).toBe(1);
352
+ const requestId = sendTextCalls[0][1].attributes[RpcRequestAttrs.RPC_REQUEST_ID];
360
353
 
361
354
  // Deliver ack and response BEFORE close() unblocks - the publish has not yet returned.
362
355
  rpcClientManager.handleIncomingRpcAck(requestId);
@@ -367,7 +360,7 @@ describe('RpcClientManager', () => {
367
360
  );
368
361
 
369
362
  // Now allow the publish path to complete.
370
- resolveClose();
363
+ resolveSend();
371
364
 
372
365
  const [, completionPromise] = await performRpcPromise;
373
366
  await expect(completionPromise).resolves.toStrictEqual('response-payload');
@@ -145,7 +145,7 @@ export default class RpcClientManager extends (EventEmitter as new () => TypedEm
145
145
  ) {
146
146
  if (remoteClientProtocol >= CLIENT_PROTOCOL_DATA_STREAM_RPC) {
147
147
  // Send payload as a data stream - a "version 2" rpc request.
148
- const writer = await this.outgoingDataStreamManager.streamText({
148
+ await this.outgoingDataStreamManager.sendText(payload, {
149
149
  topic: RPC_REQUEST_DATA_STREAM_TOPIC,
150
150
  destinationIdentities: [destinationIdentity],
151
151
  attributes: {
@@ -155,9 +155,6 @@ export default class RpcClientManager extends (EventEmitter as new () => TypedEm
155
155
  [RpcRequestAttrs.RPC_REQUEST_VERSION]: `${RPC_VERSION_V2}`,
156
156
  },
157
157
  });
158
-
159
- await writer.write(payload);
160
- await writer.close();
161
158
  return;
162
159
  }
163
160
 
@@ -2,7 +2,11 @@ import { RpcRequest } from '@livekit/protocol';
2
2
  import { assert, beforeEach, describe, expect, it, vi } from 'vitest';
3
3
  import log from '../../../logger';
4
4
  import { subscribeToEvents } from '../../../utils/subscribeToEvents';
5
- import { CLIENT_PROTOCOL_DATA_STREAM_RPC, CLIENT_PROTOCOL_DEFAULT } from '../../../version';
5
+ import {
6
+ CLIENT_PROTOCOL_DATA_STREAM_RPC,
7
+ CLIENT_PROTOCOL_DATA_STREAM_V2,
8
+ CLIENT_PROTOCOL_DEFAULT,
9
+ } from '../../../version';
6
10
  import type RTCEngine from '../../RTCEngine';
7
11
  import OutgoingDataStreamManager from '../../data-stream/outgoing/OutgoingDataStreamManager';
8
12
  import { RPC_RESPONSE_DATA_STREAM_TOPIC, RpcError, RpcRequestAttrs } from '../utils';
@@ -17,6 +21,8 @@ describe('RpcServerManager', () => {
17
21
  const outgoingDataStreamManager = new OutgoingDataStreamManager(
18
22
  {} as unknown as RTCEngine,
19
23
  log,
24
+ (_identity) => CLIENT_PROTOCOL_DEFAULT,
25
+ () => [],
20
26
  );
21
27
 
22
28
  rpcServerManager = new RpcServerManager(
@@ -184,22 +190,17 @@ describe('RpcServerManager', () => {
184
190
  describe('v2 -> v2', () => {
185
191
  let rpcServerManager: RpcServerManager;
186
192
  let outgoingDataStreamManager: OutgoingDataStreamManager;
187
- let mockStreamTextWriter: {
188
- write: ReturnType<typeof vi.fn>;
189
- close: ReturnType<typeof vi.fn>;
190
- };
191
193
 
192
194
  beforeEach(() => {
193
- outgoingDataStreamManager = new OutgoingDataStreamManager({} as unknown as RTCEngine, log);
194
-
195
- mockStreamTextWriter = {
196
- write: vi.fn().mockResolvedValue(undefined),
197
- close: vi.fn().mockResolvedValue(undefined),
198
- };
199
- vi.spyOn(outgoingDataStreamManager, 'streamText').mockResolvedValue(
200
- mockStreamTextWriter as any,
195
+ outgoingDataStreamManager = new OutgoingDataStreamManager(
196
+ {} as unknown as RTCEngine,
197
+ log,
198
+ (_identity) => CLIENT_PROTOCOL_DATA_STREAM_V2,
199
+ () => [],
201
200
  );
202
201
 
202
+ vi.spyOn(outgoingDataStreamManager, 'sendText').mockResolvedValue(undefined as any);
203
+
203
204
  rpcServerManager = new RpcServerManager(
204
205
  log,
205
206
  outgoingDataStreamManager,
@@ -243,15 +244,14 @@ describe('RpcServerManager', () => {
243
244
 
244
245
  // The response should have been sent via data stream, not packet
245
246
  expect(managerEvents.areThereBufferedEvents('sendDataPacket')).toBe(false);
246
- expect(outgoingDataStreamManager.streamText).toHaveBeenCalledWith(
247
+ expect(outgoingDataStreamManager.sendText).toHaveBeenCalledWith(
248
+ 'response payload',
247
249
  expect.objectContaining({
248
250
  topic: RPC_RESPONSE_DATA_STREAM_TOPIC,
249
251
  destinationIdentities: ['caller-identity'],
250
252
  attributes: { [RpcRequestAttrs.RPC_REQUEST_ID]: requestId },
251
253
  }),
252
254
  );
253
- expect(mockStreamTextWriter.write).toHaveBeenCalledWith('response payload');
254
- expect(mockStreamTextWriter.close).toHaveBeenCalled();
255
255
  });
256
256
 
257
257
  it('should receive a large rpc request (> 15kb) and send a large response via data stream from a participant', async () => {
@@ -277,15 +277,14 @@ describe('RpcServerManager', () => {
277
277
 
278
278
  // The response should have been sent via data stream, not packet
279
279
  expect(managerEvents.areThereBufferedEvents('sendDataPacket')).toBe(false);
280
- expect(outgoingDataStreamManager.streamText).toHaveBeenCalledWith(
280
+ expect(outgoingDataStreamManager.sendText).toHaveBeenCalledWith(
281
+ new Array(20_000).fill('B').join(''),
281
282
  expect.objectContaining({
282
283
  topic: RPC_RESPONSE_DATA_STREAM_TOPIC,
283
284
  destinationIdentities: ['caller-identity'],
284
285
  attributes: { [RpcRequestAttrs.RPC_REQUEST_ID]: requestId },
285
286
  }),
286
287
  );
287
- expect(mockStreamTextWriter.write).toHaveBeenCalledWith(new Array(20_000).fill('B').join(''));
288
- expect(mockStreamTextWriter.close).toHaveBeenCalled();
289
288
  });
290
289
 
291
290
  it('should register an RPC method handler', async () => {
@@ -317,7 +316,7 @@ describe('RpcServerManager', () => {
317
316
 
318
317
  // Response goes via data stream, not packet
319
318
  expect(managerEvents.areThereBufferedEvents('sendDataPacket')).toBe(false);
320
- expect(outgoingDataStreamManager.streamText).toHaveBeenCalled();
319
+ expect(outgoingDataStreamManager.sendText).toHaveBeenCalled();
321
320
  });
322
321
 
323
322
  it('should catch and transform unhandled errors in the RPC method handler', async () => {
@@ -414,7 +413,7 @@ describe('RpcServerManager', () => {
414
413
  const errorResponse = errorEvent.packet.value.value.value.value;
415
414
  expect(errorResponse.code).toStrictEqual(RpcError.ErrorCode.UNSUPPORTED_METHOD);
416
415
 
417
- expect(outgoingDataStreamManager.streamText).not.toHaveBeenCalled();
416
+ expect(outgoingDataStreamManager.sendText).not.toHaveBeenCalled();
418
417
  expect(managerEvents.areThereBufferedEvents('sendDataPacket')).toBe(false);
419
418
  });
420
419
  });
@@ -424,8 +423,10 @@ describe('RpcServerManager', () => {
424
423
  const outgoingDataStreamManager = new OutgoingDataStreamManager(
425
424
  {} as unknown as RTCEngine,
426
425
  log,
426
+ (_identity) => CLIENT_PROTOCOL_DEFAULT,
427
+ () => [],
427
428
  );
428
- const streamTextSpy = vi.spyOn(outgoingDataStreamManager, 'streamText');
429
+ const sendTextSpy = vi.spyOn(outgoingDataStreamManager, 'sendText');
429
430
 
430
431
  const rpcServerManager = new RpcServerManager(
431
432
  log,
@@ -457,7 +458,7 @@ describe('RpcServerManager', () => {
457
458
  assert(ackEvent.packet.value.case === 'rpcAck');
458
459
 
459
460
  // Response should be a v1 RpcResponse packet, not a data stream
460
- expect(streamTextSpy).not.toHaveBeenCalled();
461
+ expect(sendTextSpy).not.toHaveBeenCalled();
461
462
  const responseEvent = await managerEvents.waitFor('sendDataPacket');
462
463
  assert(responseEvent.packet.value.case === 'rpcResponse');
463
464
  const rpcResponse = responseEvent.packet.value.value;
@@ -263,13 +263,11 @@ export default class RpcServerManager extends (EventEmitter as new () => TypedEm
263
263
 
264
264
  if (callerClientProtocol >= CLIENT_PROTOCOL_DATA_STREAM_RPC) {
265
265
  // Send response as a data stream
266
- const writer = await this.outgoingDataStreamManager.streamText({
266
+ await this.outgoingDataStreamManager.sendText(payload, {
267
267
  topic: RPC_RESPONSE_DATA_STREAM_TOPIC,
268
268
  destinationIdentities: [destinationIdentity],
269
269
  attributes: { [RpcRequestAttrs.RPC_REQUEST_ID]: requestId },
270
270
  });
271
- await writer.write(payload);
272
- await writer.close();
273
271
  return;
274
272
  }
275
273
 
@@ -103,6 +103,22 @@ export default class LocalAudioTrack extends LocalTrack<Track.Kind.Audio> {
103
103
  await this.restart(constraints);
104
104
  }
105
105
 
106
+ async applyConstraints(
107
+ constraints: Pick<
108
+ AudioCaptureOptions,
109
+ 'autoGainControl' | 'noiseSuppression' | 'echoCancellation' | 'voiceIsolation'
110
+ >,
111
+ ): Promise<void> {
112
+ const unlock = await this.trackChangeLock.lock();
113
+ try {
114
+ const res = await this._mediaStreamTrack.applyConstraints(constraints);
115
+ this._constraints = { ...this._constraints, ...constraints };
116
+ return res;
117
+ } finally {
118
+ unlock();
119
+ }
120
+ }
121
+
106
122
  protected async restart(
107
123
  constraints?: MediaTrackConstraints,
108
124
  isUnmuting?: boolean,
@@ -60,6 +60,9 @@ export abstract class Track<
60
60
 
61
61
  /** @internal */
62
62
  setStreamState(value: Track.StreamState) {
63
+ if (this._streamState !== value) {
64
+ this.log.debug(`stream state changed: ${this._streamState} -> ${value}`);
65
+ }
63
66
  this._streamState = value;
64
67
  }
65
68
 
@@ -90,8 +93,8 @@ export abstract class Track<
90
93
  loggerOptions: LoggerOptions = {},
91
94
  ) {
92
95
  super();
93
- this.log = getLogger(loggerOptions.loggerName ?? LoggerNames.Track);
94
96
  this.loggerContextCb = loggerOptions.loggerContextCb;
97
+ this.log = getLogger(loggerOptions.loggerName ?? LoggerNames.Track, () => this.logContext);
95
98
 
96
99
  this.setMaxListeners(100);
97
100
  this.kind = kind;
@@ -185,11 +188,11 @@ export abstract class Track<
185
188
  this.emit(hasAudio ? TrackEvent.AudioPlaybackFailed : TrackEvent.VideoPlaybackFailed, e);
186
189
  } else if (e.name === 'AbortError') {
187
190
  // commonly triggered by another `play` request, only log for debugging purposes
188
- log.debug(
191
+ this.log.debug(
189
192
  `${hasAudio ? 'audio' : 'video'} playback aborted, likely due to new play request`,
190
193
  );
191
194
  } else {
192
- log.warn(`could not playback ${hasAudio ? 'audio' : 'video'}`, e);
195
+ this.log.warn(`could not playback ${hasAudio ? 'audio' : 'video'}`, { error: e });
193
196
  }
194
197
  // If audio playback isn't allowed make sure we still play back the video
195
198
  if (
@@ -252,6 +255,7 @@ export abstract class Track<
252
255
  }
253
256
 
254
257
  stop() {
258
+ this.log.debug('stopping track');
255
259
  this.stopMonitor();
256
260
  this._mediaStreamTrack.stop();
257
261
  }
@@ -279,12 +283,12 @@ export abstract class Track<
279
283
 
280
284
  /** @internal */
281
285
  updateLoggerOptions(loggerOptions: LoggerOptions) {
282
- if (loggerOptions.loggerName) {
283
- this.log = getLogger(loggerOptions.loggerName);
284
- }
285
286
  if (loggerOptions.loggerContextCb) {
286
287
  this.loggerContextCb = loggerOptions.loggerContextCb;
287
288
  }
289
+ if (loggerOptions.loggerName) {
290
+ this.log = getLogger(loggerOptions.loggerName, () => this.logContext);
291
+ }
288
292
  }
289
293
 
290
294
  private recycleElement(element: HTMLMediaElement) {
@@ -84,7 +84,17 @@ export interface TrackPublishDefaults {
84
84
  scalabilityMode?: ScalabilityMode;
85
85
 
86
86
  /**
87
- * degradation preference
87
+ * Controls how the encoder trades off between resolution and framerate
88
+ * when bandwidth is constrained.
89
+ *
90
+ * - 'maintain-framerate': Prioritizes framerate, reduces resolution if needed
91
+ * - 'maintain-resolution': Prioritizes resolution, drops frames if needed
92
+ * - 'balanced': Balances between both
93
+ *
94
+ * If not set, the SDK uses defaults based on track source:
95
+ * - Camera: 'maintain-framerate' (smoother video for real-time communication)
96
+ * - Screen share: 'maintain-resolution' (clarity is critical for text/UI)
97
+ * - Other/unknown: 'balanced'
88
98
  */
89
99
  degradationPreference?: RTCDegradationPreference;
90
100
 
package/src/room/types.ts CHANGED
@@ -21,6 +21,21 @@ export interface SendTextOptions {
21
21
  attachments?: Array<File>;
22
22
  onProgress?: (progress: number) => void;
23
23
  attributes?: Record<string, string>;
24
+ /** Whether to compress the payload (deflate-raw). Defaults to true. Compression is only applied
25
+ * when every recipient supports data streams v2 and the runtime can compress. */
26
+ compress?: boolean;
27
+ }
28
+
29
+ export interface SendBytesOptions {
30
+ topic?: string;
31
+ destinationIdentities?: Array<string>;
32
+ attributes?: Record<string, string>;
33
+ onProgress?: (progress: number) => void;
34
+ /** Whether to compress the payload (deflate-raw). Defaults to true. Compression is only applied
35
+ * when every recipient supports data streams v2 and the runtime can compress. */
36
+ compress?: boolean;
37
+ name?: string;
38
+ mimeType?: string;
24
39
  }
25
40
 
26
41
  export interface StreamTextOptions {
@@ -51,6 +66,9 @@ export type SendFileOptions = Pick<
51
66
  > & {
52
67
  onProgress?: (progress: number) => void;
53
68
  encryptionType?: Encryption_Type.NONE;
69
+ /** Whether to compress the payload (deflate-raw). Defaults to true. Compression is only applied
70
+ * when every recipient supports data streams v2 and the runtime can compress. */
71
+ compress?: boolean;
54
72
  };
55
73
 
56
74
  export type DataPublishOptions = {
package/src/room/utils.ts CHANGED
@@ -781,6 +781,73 @@ export function splitUtf8(s: string, n: number): NonSharedUint8Array[] {
781
781
  return result;
782
782
  }
783
783
 
784
+ /** Wraps a byte array in a `ReadableStream` that yields it as a single chunk and then closes. */
785
+ export function readableFromBytes(bytes: NonSharedUint8Array): ReadableStream<NonSharedUint8Array> {
786
+ return new ReadableStream<NonSharedUint8Array>({
787
+ start(controller) {
788
+ controller.enqueue(bytes);
789
+ controller.close();
790
+ },
791
+ });
792
+ }
793
+
794
+ /**
795
+ * Re-chunks a byte stream into pieces of exactly `chunkSize` bytes (the final piece may be
796
+ * smaller), coalescing or splitting the source's pieces as needed. Memory use is bounded to roughly
797
+ * `chunkSize` plus one source read, so it never buffers the whole stream — used to pack
798
+ * `CompressionStream`/`file.stream()` output into MTU-sized data-stream chunks.
799
+ */
800
+ export async function* readBytesInChunks(
801
+ source: ReadableStream<NonSharedUint8Array>,
802
+ chunkSize: number,
803
+ ): AsyncGenerator<NonSharedUint8Array> {
804
+ const reader = source.getReader();
805
+ let buffer = new Uint8Array(0);
806
+ try {
807
+ while (true) {
808
+ const { done, value } = await reader.read();
809
+ if (done) {
810
+ break;
811
+ }
812
+ if (value.byteLength === 0) {
813
+ continue;
814
+ }
815
+ const merged = new Uint8Array(buffer.byteLength + value.byteLength);
816
+ merged.set(buffer);
817
+ merged.set(value, buffer.byteLength);
818
+ buffer = merged;
819
+ while (buffer.byteLength >= chunkSize) {
820
+ yield buffer.slice(0, chunkSize);
821
+ buffer = buffer.slice(chunkSize);
822
+ }
823
+ }
824
+ if (buffer.byteLength > 0) {
825
+ yield buffer;
826
+ }
827
+ } finally {
828
+ reader.releaseLock();
829
+ }
830
+ }
831
+
832
+ /** Encodes a byte array as a base64 string (suitable for embedding binary data in a string field). */
833
+ export function encodeBase64(bytes: Uint8Array): string {
834
+ let binary = '';
835
+ for (let i = 0; i < bytes.byteLength; i++) {
836
+ binary += String.fromCharCode(bytes[i]!);
837
+ }
838
+ return btoa(binary);
839
+ }
840
+
841
+ /** Decodes a base64 string (as produced by {@link encodeBase64}) back into a byte array. */
842
+ export function decodeBase64(base64: string): Uint8Array {
843
+ const binary = atob(base64);
844
+ const bytes = new Uint8Array(binary.length);
845
+ for (let i = 0; i < binary.length; i++) {
846
+ bytes[i] = binary.charCodeAt(i);
847
+ }
848
+ return bytes;
849
+ }
850
+
784
851
  export function extractMaxAgeFromRequestHeaders(headers: Headers): number | undefined {
785
852
  const cacheControl = headers.get('Cache-Control');
786
853
  if (cacheControl) {