livekit-client 2.5.10 → 2.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. package/README.md +54 -0
  2. package/dist/livekit-client.e2ee.worker.js.map +1 -1
  3. package/dist/livekit-client.e2ee.worker.mjs.map +1 -1
  4. package/dist/livekit-client.esm.mjs +431 -45
  5. package/dist/livekit-client.esm.mjs.map +1 -1
  6. package/dist/livekit-client.umd.js +1 -1
  7. package/dist/livekit-client.umd.js.map +1 -1
  8. package/dist/src/api/SignalClient.d.ts.map +1 -1
  9. package/dist/src/index.d.ts +1 -0
  10. package/dist/src/index.d.ts.map +1 -1
  11. package/dist/src/room/PCTransport.d.ts +2 -0
  12. package/dist/src/room/PCTransport.d.ts.map +1 -1
  13. package/dist/src/room/PCTransportManager.d.ts.map +1 -1
  14. package/dist/src/room/RTCEngine.d.ts.map +1 -1
  15. package/dist/src/room/RegionUrlProvider.d.ts.map +1 -1
  16. package/dist/src/room/Room.d.ts.map +1 -1
  17. package/dist/src/room/errors.d.ts +2 -2
  18. package/dist/src/room/errors.d.ts.map +1 -1
  19. package/dist/src/room/participant/LocalParticipant.d.ts +56 -0
  20. package/dist/src/room/participant/LocalParticipant.d.ts.map +1 -1
  21. package/dist/src/room/rpc.d.ts +96 -0
  22. package/dist/src/room/rpc.d.ts.map +1 -0
  23. package/dist/src/room/track/RemoteAudioTrack.d.ts +1 -1
  24. package/dist/src/room/track/RemoteAudioTrack.d.ts.map +1 -1
  25. package/dist/src/room/track/RemoteVideoTrack.d.ts +2 -1
  26. package/dist/src/room/track/RemoteVideoTrack.d.ts.map +1 -1
  27. package/dist/src/room/track/utils.d.ts +2 -2
  28. package/dist/src/room/track/utils.d.ts.map +1 -1
  29. package/dist/ts4.2/src/index.d.ts +2 -0
  30. package/dist/ts4.2/src/room/PCTransport.d.ts +2 -0
  31. package/dist/ts4.2/src/room/errors.d.ts +2 -2
  32. package/dist/ts4.2/src/room/participant/LocalParticipant.d.ts +56 -0
  33. package/dist/ts4.2/src/room/rpc.d.ts +96 -0
  34. package/dist/ts4.2/src/room/track/RemoteAudioTrack.d.ts +1 -1
  35. package/dist/ts4.2/src/room/track/RemoteVideoTrack.d.ts +2 -1
  36. package/dist/ts4.2/src/room/track/utils.d.ts +2 -2
  37. package/package.json +2 -2
  38. package/src/api/SignalClient.ts +19 -3
  39. package/src/index.ts +2 -0
  40. package/src/room/PCTransport.ts +42 -29
  41. package/src/room/PCTransportManager.ts +6 -1
  42. package/src/room/RTCEngine.ts +13 -3
  43. package/src/room/RegionUrlProvider.ts +3 -1
  44. package/src/room/Room.ts +9 -3
  45. package/src/room/errors.ts +2 -2
  46. package/src/room/participant/LocalParticipant.test.ts +304 -0
  47. package/src/room/participant/LocalParticipant.ts +340 -1
  48. package/src/room/rpc.ts +172 -0
  49. package/src/room/track/RemoteAudioTrack.ts +1 -1
  50. package/src/room/track/RemoteVideoTrack.ts +1 -1
  51. package/src/room/track/utils.ts +1 -6
@@ -0,0 +1,172 @@
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ import { RpcError as RpcError_Proto } from '@livekit/protocol';
5
+
6
+ /** Parameters for initiating an RPC call */
7
+ export interface PerformRpcParams {
8
+ /** The `identity` of the destination participant */
9
+ destinationIdentity: string;
10
+ /** The method name to call */
11
+ method: string;
12
+ /** The method payload */
13
+ payload: string;
14
+ /** Timeout for receiving a response after initial connection (milliseconds). Default: 10000 */
15
+ responseTimeout?: number;
16
+ }
17
+
18
+ /**
19
+ * Data passed to method handler for incoming RPC invocations
20
+ */
21
+ export interface RpcInvocationData {
22
+ /**
23
+ * The unique request ID. Will match at both sides of the call, useful for debugging or logging.
24
+ */
25
+ requestId: string;
26
+
27
+ /**
28
+ * The unique participant identity of the caller.
29
+ */
30
+ callerIdentity: string;
31
+
32
+ /**
33
+ * The payload of the request. User-definable format, typically JSON.
34
+ */
35
+ payload: string;
36
+
37
+ /**
38
+ * The maximum time the caller will wait for a response.
39
+ */
40
+ responseTimeout: number;
41
+ }
42
+
43
+ /**
44
+ * Specialized error handling for RPC methods.
45
+ *
46
+ * Instances of this type, when thrown in a method handler, will have their `message`
47
+ * serialized and sent across the wire. The sender will receive an equivalent error on the other side.
48
+ *
49
+ * Built-in types are included but developers may use any string, with a max length of 256 bytes.
50
+ */
51
+
52
+ export class RpcError extends Error {
53
+ static MAX_MESSAGE_BYTES = 256;
54
+
55
+ static MAX_DATA_BYTES = 15360; // 15 KB
56
+
57
+ code: number;
58
+
59
+ data?: string;
60
+
61
+ /**
62
+ * Creates an error object with the given code and message, plus an optional data payload.
63
+ *
64
+ * If thrown in an RPC method handler, the error will be sent back to the caller.
65
+ *
66
+ * Error codes 1001-1999 are reserved for built-in errors (see RpcError.ErrorCode for their meanings).
67
+ */
68
+ constructor(code: number, message: string, data?: string) {
69
+ super(message);
70
+ this.code = code;
71
+ this.message = truncateBytes(message, RpcError.MAX_MESSAGE_BYTES);
72
+ this.data = data ? truncateBytes(data, RpcError.MAX_DATA_BYTES) : undefined;
73
+ }
74
+
75
+ /**
76
+ * @internal
77
+ */
78
+ static fromProto(proto: RpcError_Proto) {
79
+ return new RpcError(proto.code, proto.message, proto.data);
80
+ }
81
+
82
+ /**
83
+ * @internal
84
+ */
85
+ toProto() {
86
+ return new RpcError_Proto({
87
+ code: this.code as number,
88
+ message: this.message,
89
+ data: this.data,
90
+ });
91
+ }
92
+
93
+ static ErrorCode = {
94
+ APPLICATION_ERROR: 1500,
95
+ CONNECTION_TIMEOUT: 1501,
96
+ RESPONSE_TIMEOUT: 1502,
97
+ RECIPIENT_DISCONNECTED: 1503,
98
+ RESPONSE_PAYLOAD_TOO_LARGE: 1504,
99
+ SEND_FAILED: 1505,
100
+
101
+ UNSUPPORTED_METHOD: 1400,
102
+ RECIPIENT_NOT_FOUND: 1401,
103
+ REQUEST_PAYLOAD_TOO_LARGE: 1402,
104
+ UNSUPPORTED_SERVER: 1403,
105
+ UNSUPPORTED_VERSION: 1404,
106
+ } as const;
107
+
108
+ /**
109
+ * @internal
110
+ */
111
+ static ErrorMessage: Record<keyof typeof RpcError.ErrorCode, string> = {
112
+ APPLICATION_ERROR: 'Application error in method handler',
113
+ CONNECTION_TIMEOUT: 'Connection timeout',
114
+ RESPONSE_TIMEOUT: 'Response timeout',
115
+ RECIPIENT_DISCONNECTED: 'Recipient disconnected',
116
+ RESPONSE_PAYLOAD_TOO_LARGE: 'Response payload too large',
117
+ SEND_FAILED: 'Failed to send',
118
+
119
+ UNSUPPORTED_METHOD: 'Method not supported at destination',
120
+ RECIPIENT_NOT_FOUND: 'Recipient not found',
121
+ REQUEST_PAYLOAD_TOO_LARGE: 'Request payload too large',
122
+ UNSUPPORTED_SERVER: 'RPC not supported by server',
123
+ UNSUPPORTED_VERSION: 'Unsupported RPC version',
124
+ } as const;
125
+
126
+ /**
127
+ * Creates an error object from the code, with an auto-populated message.
128
+ *
129
+ * @internal
130
+ */
131
+ static builtIn(key: keyof typeof RpcError.ErrorCode, data?: string): RpcError {
132
+ return new RpcError(RpcError.ErrorCode[key], RpcError.ErrorMessage[key], data);
133
+ }
134
+ }
135
+
136
+ /*
137
+ * Maximum payload size for RPC requests and responses. If a payload exceeds this size,
138
+ * the RPC call will fail with a REQUEST_PAYLOAD_TOO_LARGE(1402) or RESPONSE_PAYLOAD_TOO_LARGE(1504) error.
139
+ */
140
+ export const MAX_PAYLOAD_BYTES = 15360; // 15 KB
141
+
142
+ /**
143
+ * @internal
144
+ */
145
+ export function byteLength(str: string): number {
146
+ const encoder = new TextEncoder();
147
+ return encoder.encode(str).length;
148
+ }
149
+
150
+ /**
151
+ * @internal
152
+ */
153
+ export function truncateBytes(str: string, maxBytes: number): string {
154
+ if (byteLength(str) <= maxBytes) {
155
+ return str;
156
+ }
157
+
158
+ let low = 0;
159
+ let high = str.length;
160
+ const encoder = new TextEncoder();
161
+
162
+ while (low < high) {
163
+ const mid = Math.floor((low + high + 1) / 2);
164
+ if (encoder.encode(str.slice(0, mid)).length <= maxBytes) {
165
+ low = mid;
166
+ } else {
167
+ high = mid - 1;
168
+ }
169
+ }
170
+
171
+ return str.slice(0, low);
172
+ }
@@ -233,7 +233,7 @@ export default class RemoteAudioTrack extends RemoteTrack<Track.Kind.Audio> {
233
233
  this.prevStats = stats;
234
234
  };
235
235
 
236
- protected async getReceiverStats(): Promise<AudioReceiverStats | undefined> {
236
+ async getReceiverStats(): Promise<AudioReceiverStats | undefined> {
237
237
  if (!this.receiver || !this.receiver.getStats) {
238
238
  return;
239
239
  }
@@ -163,7 +163,7 @@ export default class RemoteVideoTrack extends RemoteTrack<Track.Kind.Video> {
163
163
  this.prevStats = stats;
164
164
  };
165
165
 
166
- private async getReceiverStats(): Promise<VideoReceiverStats | undefined> {
166
+ async getReceiverStats(): Promise<VideoReceiverStats | undefined> {
167
167
  if (!this.receiver || !this.receiver.getStats) {
168
168
  return;
169
169
  }
@@ -9,7 +9,6 @@ import {
9
9
  type ScreenShareCaptureOptions,
10
10
  type VideoCaptureOptions,
11
11
  type VideoCodec,
12
- videoCodecs,
13
12
  } from './options';
14
13
  import type { AudioTrack } from './types';
15
14
 
@@ -188,11 +187,7 @@ export function screenCaptureToDisplayMediaStreamOptions(
188
187
  }
189
188
 
190
189
  export function mimeTypeToVideoCodecString(mimeType: string) {
191
- const codec = mimeType.split('/')[1].toLowerCase() as VideoCodec;
192
- if (!videoCodecs.includes(codec)) {
193
- throw Error(`Video codec not supported: ${codec}`);
194
- }
195
- return codec;
190
+ return mimeType.split('/')[1].toLowerCase() as VideoCodec;
196
191
  }
197
192
 
198
193
  export function getTrackPublicationInfo<T extends TrackPublication>(