@livekit/rtc-node 0.12.2 → 0.13.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 (217) hide show
  1. package/dist/audio_frame.cjs +2 -2
  2. package/dist/audio_frame.cjs.map +1 -1
  3. package/dist/audio_frame.d.cts +2 -3
  4. package/dist/audio_frame.d.ts +2 -3
  5. package/dist/audio_frame.d.ts.map +1 -1
  6. package/dist/audio_frame.js +3 -3
  7. package/dist/audio_frame.js.map +1 -1
  8. package/dist/audio_resampler.cjs +10 -7
  9. package/dist/audio_resampler.cjs.map +1 -1
  10. package/dist/audio_resampler.d.cts +2 -3
  11. package/dist/audio_resampler.d.ts +2 -3
  12. package/dist/audio_resampler.d.ts.map +1 -1
  13. package/dist/audio_resampler.js +13 -10
  14. package/dist/audio_resampler.js.map +1 -1
  15. package/dist/audio_source.cjs +6 -4
  16. package/dist/audio_source.cjs.map +1 -1
  17. package/dist/audio_source.d.cts +3 -3
  18. package/dist/audio_source.d.ts +3 -3
  19. package/dist/audio_source.d.ts.map +1 -1
  20. package/dist/audio_source.js +9 -7
  21. package/dist/audio_source.js.map +1 -1
  22. package/dist/audio_stream.cjs +1 -2
  23. package/dist/audio_stream.cjs.map +1 -1
  24. package/dist/audio_stream.d.cts +2 -3
  25. package/dist/audio_stream.d.ts +2 -3
  26. package/dist/audio_stream.d.ts.map +1 -1
  27. package/dist/audio_stream.js +2 -3
  28. package/dist/audio_stream.js.map +1 -1
  29. package/dist/e2ee.cjs +19 -20
  30. package/dist/e2ee.cjs.map +1 -1
  31. package/dist/e2ee.d.cts +0 -1
  32. package/dist/e2ee.d.ts +0 -1
  33. package/dist/e2ee.d.ts.map +1 -1
  34. package/dist/e2ee.js +29 -30
  35. package/dist/e2ee.js.map +1 -1
  36. package/dist/ffi_client.cjs +6 -5
  37. package/dist/ffi_client.cjs.map +1 -1
  38. package/dist/ffi_client.d.cts +10 -10
  39. package/dist/ffi_client.d.ts +10 -10
  40. package/dist/ffi_client.d.ts.map +1 -1
  41. package/dist/ffi_client.js +6 -10
  42. package/dist/ffi_client.js.map +1 -1
  43. package/dist/index.cjs +16 -49
  44. package/dist/index.cjs.map +1 -1
  45. package/dist/index.d.cts +6 -35
  46. package/dist/index.d.ts +6 -35
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +7 -46
  49. package/dist/index.js.map +1 -1
  50. package/dist/napi/native.d.cjs.map +1 -1
  51. package/dist/napi/native.d.ts +5 -5
  52. package/dist/native.d.ts +5 -5
  53. package/dist/participant.cjs +57 -34
  54. package/dist/participant.cjs.map +1 -1
  55. package/dist/participant.d.cts +10 -10
  56. package/dist/participant.d.ts +10 -10
  57. package/dist/participant.d.ts.map +1 -1
  58. package/dist/participant.js +75 -50
  59. package/dist/participant.js.map +1 -1
  60. package/dist/proto/audio_frame_pb.cjs +977 -125
  61. package/dist/proto/audio_frame_pb.cjs.map +1 -1
  62. package/dist/proto/audio_frame_pb.d.cts +502 -417
  63. package/dist/proto/audio_frame_pb.d.ts +502 -417
  64. package/dist/proto/audio_frame_pb.d.ts.map +1 -1
  65. package/dist/proto/audio_frame_pb.js +943 -85
  66. package/dist/proto/audio_frame_pb.js.map +1 -1
  67. package/dist/proto/e2ee_pb.cjs +700 -85
  68. package/dist/proto/e2ee_pb.cjs.map +1 -1
  69. package/dist/proto/e2ee_pb.d.cts +331 -263
  70. package/dist/proto/e2ee_pb.d.ts +331 -263
  71. package/dist/proto/e2ee_pb.d.ts.map +1 -1
  72. package/dist/proto/e2ee_pb.js +675 -57
  73. package/dist/proto/e2ee_pb.js.map +1 -1
  74. package/dist/proto/ffi_pb.cjs +369 -36
  75. package/dist/proto/ffi_pb.cjs.map +1 -1
  76. package/dist/proto/ffi_pb.d.cts +156 -109
  77. package/dist/proto/ffi_pb.d.ts +156 -109
  78. package/dist/proto/ffi_pb.d.ts.map +1 -1
  79. package/dist/proto/ffi_pb.js +364 -29
  80. package/dist/proto/ffi_pb.js.map +1 -1
  81. package/dist/proto/handle_pb.cjs +27 -7
  82. package/dist/proto/handle_pb.cjs.map +1 -1
  83. package/dist/proto/handle_pb.d.cts +13 -15
  84. package/dist/proto/handle_pb.d.ts +13 -15
  85. package/dist/proto/handle_pb.d.ts.map +1 -1
  86. package/dist/proto/handle_pb.js +26 -5
  87. package/dist/proto/handle_pb.js.map +1 -1
  88. package/dist/proto/participant_pb.cjs +112 -15
  89. package/dist/proto/participant_pb.cjs.map +1 -1
  90. package/dist/proto/participant_pb.d.cts +140 -52
  91. package/dist/proto/participant_pb.d.ts +140 -52
  92. package/dist/proto/participant_pb.d.ts.map +1 -1
  93. package/dist/proto/participant_pb.js +109 -11
  94. package/dist/proto/participant_pb.js.map +1 -1
  95. package/dist/proto/room_pb.cjs +2693 -302
  96. package/dist/proto/room_pb.cjs.map +1 -1
  97. package/dist/proto/room_pb.d.cts +1265 -862
  98. package/dist/proto/room_pb.d.ts +1265 -862
  99. package/dist/proto/room_pb.d.ts.map +1 -1
  100. package/dist/proto/room_pb.js +2603 -210
  101. package/dist/proto/room_pb.js.map +1 -1
  102. package/dist/proto/rpc_pb.cjs +302 -37
  103. package/dist/proto/rpc_pb.cjs.map +1 -1
  104. package/dist/proto/rpc_pb.d.cts +138 -108
  105. package/dist/proto/rpc_pb.d.ts +138 -108
  106. package/dist/proto/rpc_pb.d.ts.map +1 -1
  107. package/dist/proto/rpc_pb.js +291 -25
  108. package/dist/proto/rpc_pb.js.map +1 -1
  109. package/dist/proto/stats_pb.cjs +1253 -139
  110. package/dist/proto/stats_pb.cjs.map +1 -1
  111. package/dist/proto/stats_pb.d.cts +803 -692
  112. package/dist/proto/stats_pb.d.ts +803 -692
  113. package/dist/proto/stats_pb.d.ts.map +1 -1
  114. package/dist/proto/stats_pb.js +1216 -93
  115. package/dist/proto/stats_pb.js.map +1 -1
  116. package/dist/proto/track_pb.cjs +462 -64
  117. package/dist/proto/track_pb.cjs.map +1 -1
  118. package/dist/proto/track_pb.d.cts +250 -218
  119. package/dist/proto/track_pb.d.ts +250 -218
  120. package/dist/proto/track_pb.d.ts.map +1 -1
  121. package/dist/proto/track_pb.js +447 -45
  122. package/dist/proto/track_pb.js.map +1 -1
  123. package/dist/proto/track_publication_pb.cjs +130 -0
  124. package/dist/proto/track_publication_pb.cjs.map +1 -0
  125. package/dist/proto/track_publication_pb.d.cts +80 -0
  126. package/dist/proto/track_publication_pb.d.ts +80 -0
  127. package/dist/proto/track_publication_pb.d.ts.map +1 -0
  128. package/dist/proto/track_publication_pb.js +103 -0
  129. package/dist/proto/track_publication_pb.js.map +1 -0
  130. package/dist/proto/video_frame_pb.cjs +648 -88
  131. package/dist/proto/video_frame_pb.cjs.map +1 -1
  132. package/dist/proto/video_frame_pb.d.cts +371 -328
  133. package/dist/proto/video_frame_pb.d.ts +371 -328
  134. package/dist/proto/video_frame_pb.d.ts.map +1 -1
  135. package/dist/proto/video_frame_pb.js +626 -60
  136. package/dist/proto/video_frame_pb.js.map +1 -1
  137. package/dist/room.cjs +93 -125
  138. package/dist/room.cjs.map +1 -1
  139. package/dist/room.d.cts +6 -17
  140. package/dist/room.d.ts +6 -17
  141. package/dist/room.d.ts.map +1 -1
  142. package/dist/room.js +92 -122
  143. package/dist/room.js.map +1 -1
  144. package/dist/rpc.cjs.map +1 -1
  145. package/dist/rpc.d.cts +0 -1
  146. package/dist/rpc.d.ts +0 -1
  147. package/dist/rpc.js.map +1 -1
  148. package/dist/track.cjs +12 -8
  149. package/dist/track.cjs.map +1 -1
  150. package/dist/track.d.cts +8 -9
  151. package/dist/track.d.ts +8 -9
  152. package/dist/track.d.ts.map +1 -1
  153. package/dist/track.js +13 -9
  154. package/dist/track.js.map +1 -1
  155. package/dist/track_publication.cjs +21 -12
  156. package/dist/track_publication.cjs.map +1 -1
  157. package/dist/track_publication.d.cts +12 -13
  158. package/dist/track_publication.d.ts +12 -13
  159. package/dist/track_publication.d.ts.map +1 -1
  160. package/dist/track_publication.js +22 -13
  161. package/dist/track_publication.js.map +1 -1
  162. package/dist/version.cjs +1 -1
  163. package/dist/version.cjs.map +1 -1
  164. package/dist/version.d.cts +1 -1
  165. package/dist/version.d.ts +1 -1
  166. package/dist/version.js +1 -1
  167. package/dist/version.js.map +1 -1
  168. package/dist/video_frame.cjs +26 -45
  169. package/dist/video_frame.cjs.map +1 -1
  170. package/dist/video_frame.d.cts +2 -3
  171. package/dist/video_frame.d.ts +2 -3
  172. package/dist/video_frame.d.ts.map +1 -1
  173. package/dist/video_frame.js +29 -48
  174. package/dist/video_frame.js.map +1 -1
  175. package/dist/video_source.cjs +7 -4
  176. package/dist/video_source.cjs.map +1 -1
  177. package/dist/video_source.d.cts +4 -4
  178. package/dist/video_source.d.ts +4 -4
  179. package/dist/video_source.d.ts.map +1 -1
  180. package/dist/video_source.js +9 -6
  181. package/dist/video_source.js.map +1 -1
  182. package/dist/video_stream.cjs +16 -5
  183. package/dist/video_stream.cjs.map +1 -1
  184. package/dist/video_stream.d.cts +3 -4
  185. package/dist/video_stream.d.ts +3 -4
  186. package/dist/video_stream.d.ts.map +1 -1
  187. package/dist/video_stream.js +17 -6
  188. package/dist/video_stream.js.map +1 -1
  189. package/package.json +9 -9
  190. package/src/audio_frame.ts +10 -10
  191. package/src/audio_resampler.ts +15 -13
  192. package/src/audio_source.ts +11 -8
  193. package/src/audio_stream.ts +3 -4
  194. package/src/e2ee.ts +36 -37
  195. package/src/ffi_client.ts +8 -16
  196. package/src/index.ts +5 -68
  197. package/src/napi/native.d.ts +5 -5
  198. package/src/participant.ts +100 -72
  199. package/src/proto/audio_frame_pb.ts +1220 -496
  200. package/src/proto/e2ee_pb.ts +858 -337
  201. package/src/proto/ffi_pb.ts +442 -135
  202. package/src/proto/handle_pb.ts +33 -19
  203. package/src/proto/participant_pb.ts +225 -60
  204. package/src/proto/room_pb.ts +3149 -1013
  205. package/src/proto/rpc_pb.ts +358 -129
  206. package/src/proto/stats_pb.ts +1891 -877
  207. package/src/proto/track_pb.ts +587 -253
  208. package/src/proto/track_publication_pb.ts +180 -0
  209. package/src/proto/video_frame_pb.ts +861 -394
  210. package/src/room.ts +143 -188
  211. package/src/rpc.ts +1 -1
  212. package/src/track.ts +16 -17
  213. package/src/track_publication.ts +25 -26
  214. package/src/version.ts +1 -1
  215. package/src/video_frame.ts +48 -72
  216. package/src/video_source.ts +11 -8
  217. package/src/video_stream.ts +18 -8
package/src/room.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
- import { create } from '@bufbuild/protobuf';
5
4
  import type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';
6
5
  import EventEmitter from 'events';
7
6
  import type { E2EEOptions } from './e2ee.js';
@@ -11,20 +10,19 @@ import type { Participant } from './participant.js';
11
10
  import { LocalParticipant, RemoteParticipant } from './participant.js';
12
11
  import { EncryptionState } from './proto/e2ee_pb.js';
13
12
  import type { FfiEvent } from './proto/ffi_pb.js';
14
- import type { OwnedParticipant } from './proto/participant_pb.js';
15
- import type {
16
- ConnectCallback,
17
- ConnectResponse,
18
- ConnectionQuality,
19
- DataPacketKind,
20
- DisconnectReason,
21
- DisconnectResponse,
13
+ import type { DisconnectReason, OwnedParticipant } from './proto/participant_pb.js';
14
+ import {
15
+ type ConnectCallback,
16
+ type ConnectResponse,
17
+ type ConnectionQuality,
18
+ type DataPacketKind,
19
+ type DisconnectResponse,
22
20
  RoomOptions as FfiRoomOptions,
23
- IceServer,
24
- RoomInfo,
21
+ type IceServer,
22
+ type RoomInfo,
25
23
  } from './proto/room_pb.js';
26
24
  import {
27
- ConnectRequestSchema,
25
+ ConnectRequest,
28
26
  ConnectionState,
29
27
  ContinualGatheringPolicy,
30
28
  IceTransportType,
@@ -55,14 +53,14 @@ export interface RoomOptions {
55
53
  rtcConfig?: RtcConfiguration;
56
54
  }
57
55
 
58
- export const defaultRoomOptions = {
56
+ export const defaultRoomOptions = new FfiRoomOptions({
59
57
  autoSubscribe: true,
60
58
  dynacast: false,
61
59
  e2ee: undefined,
62
60
  rtcConfig: undefined,
63
61
  adaptiveStream: false,
64
62
  joinRetries: 1,
65
- } satisfies Omit<FfiRoomOptions, '$typeName'>;
63
+ });
66
64
 
67
65
  export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>) {
68
66
  private info?: RoomInfo;
@@ -90,32 +88,14 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
90
88
  return this.ffiHandle != undefined && this.connectionState != ConnectionState.CONN_DISCONNECTED;
91
89
  }
92
90
 
93
- async getSid(): Promise<string> {
94
- if (!this.isConnected) {
95
- return '';
96
- }
97
- if (this.info && this.info.sid !== '') {
98
- return this.info.sid;
99
- }
100
- return new Promise((resolve, reject) => {
101
- const handleRoomUpdate = (sid: string) => {
102
- if (sid !== '') {
103
- this.off(RoomEvent.RoomSidChanged, handleRoomUpdate);
104
- resolve(sid);
105
- }
106
- };
107
- this.on(RoomEvent.RoomSidChanged, handleRoomUpdate);
108
- this.once(RoomEvent.Disconnected, () => {
109
- this.off(RoomEvent.RoomSidChanged, handleRoomUpdate);
110
- reject('Room disconnected before room server id was available');
111
- });
112
- });
91
+ async getSid(): Promise<string | undefined> {
92
+ return this.info?.sid; // TODO update this to handle async room updates once rust protocol has been updated
113
93
  }
114
94
 
115
95
  async connect(url: string, token: string, opts?: RoomOptions) {
116
96
  const options = { ...defaultRoomOptions, ...opts };
117
97
 
118
- const req = create(ConnectRequestSchema, {
98
+ const req = new ConnectRequest({
119
99
  url: url,
120
100
  token: token,
121
101
  options,
@@ -132,35 +112,42 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
132
112
  return ev.message.case == 'connect' && ev.message.value.asyncId == res.asyncId;
133
113
  });
134
114
 
135
- if (cb.message.case !== 'result') {
136
- throw new ConnectError(cb.message.value ?? 'Unknown error');
137
- }
138
-
139
- const { room, localParticipant, participants } = cb.message.value;
115
+ switch (cb.message.case) {
116
+ case 'result':
117
+ this.ffiHandle = new FfiHandle(cb.message.value.room!.handle!.id!);
118
+ this.e2eeManager = new E2EEManager(this.ffiHandle.handle, {
119
+ keyProviderOptions: {
120
+ sharedKey: options.e2ee?.keyProviderOptions?.sharedKey,
121
+ ratchetSalt: options.e2ee!.keyProviderOptions!.ratchetSalt!,
122
+ ratchetWindowSize: options.e2ee!.keyProviderOptions!.ratchetWindowSize!,
123
+ failureTolerance: options.e2ee!.keyProviderOptions!.failureTolerance!,
124
+ },
125
+ encryptionType: options.e2ee!.encryptionType!,
126
+ });
140
127
 
141
- this.ffiHandle = new FfiHandle(room!.handle!.id);
142
- if (options.e2ee) {
143
- this.e2eeManager = new E2EEManager(this.ffiHandle.handle, options.e2ee);
144
- }
128
+ this.info = cb.message.value.room!.info;
129
+ this.connectionState = ConnectionState.CONN_CONNECTED;
130
+ this.localParticipant = new LocalParticipant(cb.message.value.localParticipant!);
145
131
 
146
- this.info = room!.info;
147
- this.connectionState = ConnectionState.CONN_CONNECTED;
148
- this.localParticipant = new LocalParticipant(localParticipant!);
132
+ for (const pt of cb.message.value.participants) {
133
+ const rp = this.createRemoteParticipant(pt.participant!);
149
134
 
150
- for (const pt of participants) {
151
- const rp = this.createRemoteParticipant(pt.participant!);
135
+ for (const pub of pt.publications) {
136
+ const publication = new RemoteTrackPublication(pub);
137
+ rp.trackPublications.set(publication.sid!, publication);
138
+ }
139
+ }
152
140
 
153
- for (const pub of pt.publications) {
154
- const publication = new RemoteTrackPublication(pub);
155
- rp.trackPublications.set(publication.sid, publication);
156
- }
141
+ FfiClient.instance.on(FfiClientEvent.FfiEvent, this.onFfiEvent);
142
+ break;
143
+ case 'error':
144
+ default:
145
+ throw new ConnectError(cb.message.value || '');
157
146
  }
158
-
159
- FfiClient.instance.on(FfiClientEvent.FfiEvent, this.onFfiEvent);
160
147
  }
161
148
 
162
149
  async disconnect() {
163
- if (!this.isConnected || !this.ffiHandle) {
150
+ if (!this.isConnected) {
164
151
  return;
165
152
  }
166
153
 
@@ -168,7 +155,7 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
168
155
  message: {
169
156
  case: 'disconnect',
170
157
  value: {
171
- roomHandle: this.ffiHandle.handle,
158
+ roomHandle: this.ffiHandle?.handle,
172
159
  },
173
160
  },
174
161
  });
@@ -178,27 +165,9 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
178
165
  }
179
166
 
180
167
  private onFfiEvent = (ffiEvent: FfiEvent) => {
181
- if (!this.localParticipant || !this.ffiHandle || !this.info) {
182
- throw TypeError('cannot handle ffi events before connectCallback');
183
- }
184
-
185
- if (ffiEvent.message.case == 'rpcMethodInvocation') {
186
- if (
187
- ffiEvent.message.value.localParticipantHandle == this.localParticipant.ffi_handle.handle
188
- ) {
189
- this.localParticipant.handleRpcMethodInvocation(
190
- ffiEvent.message.value.invocationId,
191
- ffiEvent.message.value.method,
192
- ffiEvent.message.value.requestId,
193
- ffiEvent.message.value.callerIdentity,
194
- ffiEvent.message.value.payload,
195
- ffiEvent.message.value.responseTimeoutMs,
196
- );
197
- }
198
- return;
199
- } else if (
168
+ if (
200
169
  ffiEvent.message.case != 'roomEvent' ||
201
- ffiEvent.message.value.roomHandle != this.ffiHandle.handle
170
+ ffiEvent.message.value.roomHandle != this.ffiHandle?.handle
202
171
  ) {
203
172
  return;
204
173
  }
@@ -206,136 +175,125 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
206
175
  const ev = ffiEvent.message.value.message;
207
176
  if (ev.case == 'participantConnected') {
208
177
  const participant = this.createRemoteParticipant(ev.value.info!);
209
- this.remoteParticipants.set(participant.identity, participant);
178
+ this.remoteParticipants.set(participant.identity!, participant);
210
179
  this.emit(RoomEvent.ParticipantConnected, participant);
211
180
  } else if (ev.case == 'participantDisconnected') {
212
- const participant = this.remoteParticipants.get(ev.value.participantIdentity);
213
- if (participant) {
214
- this.remoteParticipants.delete(participant.identity);
215
- this.emit(RoomEvent.ParticipantDisconnected, participant);
216
- }
181
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
182
+ this.remoteParticipants.delete(participant!.identity!);
183
+ this.emit(RoomEvent.ParticipantDisconnected, participant!);
217
184
  } else if (ev.case == 'localTrackPublished') {
218
- const publication = this.localParticipant.trackPublications.get(ev.value.trackSid);
219
- if (!publication) {
220
- throw new TypeError('local track publication not found');
221
- }
222
- this.emit(RoomEvent.LocalTrackPublished, publication, this.localParticipant);
185
+ const publication = this.localParticipant!.trackPublications.get(ev.value.trackSid!);
186
+ this.emit(RoomEvent.LocalTrackPublished, publication!, this.localParticipant!);
223
187
  } else if (ev.case == 'localTrackUnpublished') {
224
- const publication = this.localParticipant.trackPublications.get(ev.value.publicationSid);
225
- this.localParticipant.trackPublications.delete(ev.value.publicationSid);
226
- if (publication) {
227
- this.emit(RoomEvent.LocalTrackUnpublished, publication, this.localParticipant);
228
- }
188
+ const publication = this.localParticipant!.trackPublications.get(ev.value.publicationSid!);
189
+ this.localParticipant!.trackPublications.delete(ev.value.publicationSid!);
190
+ this.emit(RoomEvent.LocalTrackUnpublished, publication!, this.localParticipant!);
229
191
  } else if (ev.case == 'localTrackSubscribed') {
230
- const publication = this.localParticipant.trackPublications.get(ev.value.trackSid);
231
- if (!publication) {
232
- throw new TypeError('local track publication not found');
233
- }
234
- publication.resolveFirstSubscription();
235
- this.emit(RoomEvent.LocalTrackSubscribed, publication.track!);
192
+ const publication = this.localParticipant!.trackPublications.get(ev.value.trackSid!);
193
+ publication!.resolveFirstSubscription();
194
+ this.emit(RoomEvent.LocalTrackSubscribed, publication!.track!);
236
195
  } else if (ev.case == 'trackPublished') {
237
- const participant = this.requireRemoteParticipant(ev.value.participantIdentity);
196
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
238
197
  const publication = new RemoteTrackPublication(ev.value.publication!);
239
- participant.trackPublications.set(publication.sid, publication);
240
- this.emit(RoomEvent.TrackPublished, publication, participant);
198
+ participant!.trackPublications.set(publication.sid!, publication);
199
+ this.emit(RoomEvent.TrackPublished, publication, participant!);
241
200
  } else if (ev.case == 'trackUnpublished') {
242
- const participant = this.requireRemoteParticipant(ev.value.participantIdentity);
243
- const publication = participant.trackPublications.get(ev.value.publicationSid);
244
- participant.trackPublications.delete(ev.value.publicationSid);
245
- if (publication) {
246
- this.emit(RoomEvent.TrackUnpublished, publication, participant);
247
- }
201
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
202
+ const publication = participant!.trackPublications.get(ev.value.publicationSid!);
203
+ participant!.trackPublications.delete(ev.value.publicationSid!);
204
+ this.emit(RoomEvent.TrackUnpublished, publication!, participant!);
248
205
  } else if (ev.case == 'trackSubscribed') {
249
- const ownedTrack = ev.value.track!;
250
- const trackInfo = ownedTrack.info!;
251
- const { participant, publication } = this.requirePublicationOfRemoteParticipant(
252
- ev.value.participantIdentity,
253
- trackInfo.sid,
254
- );
255
- publication.subscribed = true;
256
- if (trackInfo.kind == TrackKind.KIND_VIDEO) {
257
- publication.track = new RemoteVideoTrack(ownedTrack);
258
- } else if (trackInfo.kind == TrackKind.KIND_AUDIO) {
259
- publication.track = new RemoteAudioTrack(ownedTrack);
206
+ const ownedTrack = ev.value.track;
207
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
208
+ const publication = participant!.trackPublications.get(ownedTrack!.info!.sid!);
209
+ publication!.subscribed = true;
210
+ if (ownedTrack!.info!.kind == TrackKind.KIND_VIDEO) {
211
+ publication!.track = new RemoteVideoTrack(ownedTrack!);
212
+ } else if (ownedTrack!.info!.kind == TrackKind.KIND_AUDIO) {
213
+ publication!.track = new RemoteAudioTrack(ownedTrack!);
260
214
  }
261
215
 
262
- this.emit(RoomEvent.TrackSubscribed, publication.track!, publication, participant);
216
+ this.emit(RoomEvent.TrackSubscribed, publication!.track!, publication!, participant!);
263
217
  } else if (ev.case == 'trackUnsubscribed') {
264
- const { participant, publication } = this.requirePublicationOfRemoteParticipant(
265
- ev.value.participantIdentity,
266
- ev.value.trackSid,
267
- );
268
- const track = publication.track!;
269
- publication.track = undefined;
270
- publication.subscribed = false;
271
- this.emit(RoomEvent.TrackUnsubscribed, track, publication, participant);
218
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
219
+ const publication = participant!.trackPublications.get(ev.value.trackSid!);
220
+ publication!.track = undefined;
221
+ publication!.subscribed = false;
222
+ this.emit(RoomEvent.TrackUnsubscribed, publication!.track!, publication!, participant!);
272
223
  } else if (ev.case == 'trackSubscriptionFailed') {
273
- const participant = this.requireRemoteParticipant(ev.value.participantIdentity);
274
- this.emit(RoomEvent.TrackSubscriptionFailed, ev.value.trackSid, participant, ev.value.error);
275
- } else if (ev.case == 'trackMuted') {
276
- const { participant, publication } = this.requirePublicationOfParticipant(
277
- ev.value.participantIdentity,
278
- ev.value.trackSid,
224
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
225
+ this.emit(
226
+ RoomEvent.TrackSubscriptionFailed,
227
+ ev.value.trackSid!,
228
+ participant!,
229
+ ev.value.error,
279
230
  );
280
- publication.info.muted = true;
281
- if (publication.track) {
282
- publication.track.info.muted = true;
231
+ } else if (ev.case == 'trackMuted') {
232
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
233
+ const publication = participant!.trackPublications.get(ev.value.trackSid!);
234
+ publication!.info!.muted = true;
235
+ if (publication!.track) {
236
+ publication!.track.info!.muted = true;
283
237
  }
284
- this.emit(RoomEvent.TrackMuted, publication, participant);
238
+ this.emit(RoomEvent.TrackMuted, publication!, participant!);
285
239
  } else if (ev.case == 'trackUnmuted') {
286
- const { participant, publication } = this.requirePublicationOfParticipant(
287
- ev.value.participantIdentity,
288
- ev.value.trackSid,
289
- );
290
- publication.info.muted = false;
291
- if (publication.track) {
292
- publication.track.info.muted = false;
240
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
241
+ const publication = participant!.trackPublications.get(ev.value.trackSid!);
242
+ publication!.info!.muted = false;
243
+ if (publication!.track) {
244
+ publication!.track.info!.muted = false;
293
245
  }
294
- this.emit(RoomEvent.TrackUnmuted, publication, participant);
246
+ this.emit(RoomEvent.TrackUnmuted, publication!, participant!);
295
247
  } else if (ev.case == 'activeSpeakersChanged') {
296
248
  const activeSpeakers = ev.value.participantIdentities.map((identity) =>
297
- this.requireParticipantByIdentity(identity),
249
+ this.retrieveParticipantByIdentity(identity),
250
+ );
251
+ this.emit(
252
+ RoomEvent.ActiveSpeakersChanged,
253
+ activeSpeakers.map((s) => s!),
298
254
  );
299
- this.emit(RoomEvent.ActiveSpeakersChanged, activeSpeakers);
300
255
  } else if (ev.case == 'roomMetadataChanged') {
301
- this.info.metadata = ev.value.metadata;
302
- this.emit(RoomEvent.RoomMetadataChanged, this.info.metadata);
256
+ this.info!.metadata = ev.value.metadata;
257
+ this.emit(RoomEvent.RoomMetadataChanged, this.info!.metadata!);
303
258
  } else if (ev.case == 'participantMetadataChanged') {
304
- const participant = this.requireParticipantByIdentity(ev.value.participantIdentity);
305
- participant.info.metadata = ev.value.metadata;
306
- this.emit(RoomEvent.ParticipantMetadataChanged, participant.metadata, participant);
259
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
260
+ participant!.info!.metadata = ev.value.metadata;
261
+ this.emit(RoomEvent.ParticipantMetadataChanged, participant!.metadata, participant!);
307
262
  } else if (ev.case == 'participantNameChanged') {
308
- const participant = this.requireParticipantByIdentity(ev.value.participantIdentity);
309
- participant.info.name = ev.value.name;
310
- this.emit(RoomEvent.ParticipantNameChanged, participant.name, participant);
263
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
264
+ participant!.info!.name = ev.value.name;
265
+ this.emit(RoomEvent.ParticipantNameChanged, participant!.name!, participant!);
311
266
  } else if (ev.case == 'participantAttributesChanged') {
312
- const participant = this.requireParticipantByIdentity(ev.value.participantIdentity);
313
- participant.info.attributes = ev.value.attributes.reduce(
314
- (acc, value) => {
315
- acc[value.key] = value.value;
316
- return acc;
267
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
268
+ participant!.info!.attributes = ev.value.attributes.reduce(
269
+ (obj, item) => {
270
+ obj[item.key!] = item.value!;
271
+ return obj;
317
272
  },
318
273
  {} as Record<string, string>,
319
274
  );
320
275
  if (Object.keys(ev.value.changedAttributes).length > 0) {
321
- const changedAttributes = ev.value.changedAttributes.reduce(
322
- (acc, value) => {
323
- acc[value.key] = value.value;
324
- return acc;
325
- },
326
- {} as Record<string, string>,
276
+ this.emit(
277
+ RoomEvent.ParticipantAttributesChanged,
278
+ ev.value.changedAttributes.reduce(
279
+ (obj, item) => {
280
+ obj[item.key!] = item.value!;
281
+ return obj;
282
+ },
283
+ {} as Record<string, string>,
284
+ ),
285
+ participant!,
327
286
  );
328
- this.emit(RoomEvent.ParticipantAttributesChanged, changedAttributes, participant);
329
287
  }
330
288
  } else if (ev.case == 'connectionQualityChanged') {
331
- const participant = this.requireParticipantByIdentity(ev.value.participantIdentity);
332
- this.emit(RoomEvent.ConnectionQualityChanged, ev.value.quality, participant);
289
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
290
+ this.emit(RoomEvent.ConnectionQualityChanged, ev.value.quality!, participant!);
333
291
  } else if (ev.case == 'chatMessage') {
334
- const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity);
292
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
335
293
  const { id, message: messageText, timestamp, editTimestamp, generated } = ev.value.message!;
336
294
  const message: ChatMessage = {
337
- id,
338
- message: messageText,
295
+ id: id!,
296
+ message: messageText!,
339
297
  timestamp: Number(timestamp),
340
298
  editTimestamp: Number(editTimestamp),
341
299
  generated,
@@ -343,13 +301,15 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
343
301
  this.emit(RoomEvent.ChatMessage, message, participant);
344
302
  } else if (ev.case == 'dataPacketReceived') {
345
303
  // Can be undefined if the data is sent from a Server SDK
346
- const participant = this.remoteParticipants.get(ev.value.participantIdentity);
304
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
347
305
  const dataPacket = ev.value.value;
348
306
  switch (dataPacket.case) {
349
307
  case 'user':
350
- const data = dataPacket.value.data!.data!;
351
- const buffer = FfiClient.instance.copyBuffer(data.dataPtr, Number(data.dataLen));
352
- new FfiHandle(dataPacket.value.data!.handle!.id).dispose();
308
+ const buffer = FfiClient.instance.copyBuffer(
309
+ dataPacket.value.data!.data!.dataPtr!,
310
+ Number(dataPacket.value.data!.data!.dataLen),
311
+ );
312
+ new FfiHandle(dataPacket.value.data!.handle!.id!).dispose();
353
313
  this.emit(
354
314
  RoomEvent.DataReceived,
355
315
  buffer,
@@ -360,7 +320,7 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
360
320
  break;
361
321
  case 'sipDtmf':
362
322
  const { code, digit } = dataPacket.value;
363
- this.emit(RoomEvent.DtmfReceived, code, digit, participant);
323
+ this.emit(RoomEvent.DtmfReceived, code!, digit!, participant!);
364
324
  break;
365
325
  default:
366
326
  break;
@@ -371,18 +331,16 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
371
331
  this.emit(RoomEvent.EncryptionError, new Error('internal server error'));
372
332
  }
373
333
  } else if (ev.case == 'connectionStateChanged') {
374
- this.connectionState = ev.value.state;
334
+ this.connectionState = ev.value.state!;
375
335
  this.emit(RoomEvent.ConnectionStateChanged, this.connectionState);
376
336
  /*} else if (ev.case == 'connected') {
377
337
  this.emit(RoomEvent.Connected);*/
378
338
  } else if (ev.case == 'disconnected') {
379
- this.emit(RoomEvent.Disconnected, ev.value.reason);
339
+ this.emit(RoomEvent.Disconnected, ev.value.reason!);
380
340
  } else if (ev.case == 'reconnecting') {
381
341
  this.emit(RoomEvent.Reconnecting);
382
342
  } else if (ev.case == 'reconnected') {
383
343
  this.emit(RoomEvent.Reconnected);
384
- } else if (ev.case == 'roomSidChanged') {
385
- this.emit(RoomEvent.RoomSidChanged, ev.value.sid);
386
344
  }
387
345
  };
388
346
 
@@ -431,12 +389,12 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
431
389
  }
432
390
 
433
391
  private createRemoteParticipant(ownedInfo: OwnedParticipant) {
434
- if (this.remoteParticipants.has(ownedInfo.info!.identity)) {
392
+ if (this.remoteParticipants.has(ownedInfo.info!.identity!)) {
435
393
  throw new Error('Participant already exists');
436
394
  }
437
395
 
438
396
  const participant = new RemoteParticipant(ownedInfo);
439
- this.remoteParticipants.set(ownedInfo.info!.identity, participant);
397
+ this.remoteParticipants.set(ownedInfo.info!.identity!, participant);
440
398
  return participant;
441
399
  }
442
400
  }
@@ -477,7 +435,6 @@ export type RoomCallbacks = {
477
435
  trackUnmuted: (publication: TrackPublication, participant: Participant) => void;
478
436
  activeSpeakersChanged: (speakers: Participant[]) => void;
479
437
  roomMetadataChanged: (metadata: string) => void;
480
- roomInfoUpdated: (info: RoomInfo) => void;
481
438
  participantMetadataChanged: (metadata: string | undefined, participant: Participant) => void;
482
439
  participantNameChanged: (name: string, participant: Participant) => void;
483
440
  participantAttributesChanged: (
@@ -492,14 +449,13 @@ export type RoomCallbacks = {
492
449
  topic?: string,
493
450
  ) => void;
494
451
  chatMessage: (message: ChatMessage, participant?: Participant) => void;
495
- dtmfReceived: (code: number, digit: string, participant?: RemoteParticipant) => void;
452
+ dtmfReceived: (code: number, digit: string, participant: RemoteParticipant) => void;
496
453
  encryptionError: (error: Error) => void;
497
454
  connectionStateChanged: (state: ConnectionState) => void;
498
455
  connected: () => void;
499
456
  disconnected: (reason: DisconnectReason) => void;
500
457
  reconnecting: () => void;
501
458
  reconnected: () => void;
502
- roomSidChanged: (sid: string) => void;
503
459
  };
504
460
 
505
461
  export enum RoomEvent {
@@ -517,7 +473,6 @@ export enum RoomEvent {
517
473
  TrackUnmuted = 'trackUnmuted',
518
474
  ActiveSpeakersChanged = 'activeSpeakersChanged',
519
475
  RoomMetadataChanged = 'roomMetadataChanged',
520
- RoomSidChanged = 'roomSidChanged',
521
476
  ParticipantMetadataChanged = 'participantMetadataChanged',
522
477
  ParticipantNameChanged = 'participantNameChanged',
523
478
  ParticipantAttributesChanged = 'participantAttributesChanged',
package/src/rpc.ts CHANGED
@@ -68,7 +68,7 @@ export class RpcError extends Error {
68
68
  }
69
69
 
70
70
  static fromProto(proto: RpcError_Proto) {
71
- return new RpcError(proto.code, proto.message, proto.data);
71
+ return new RpcError(proto.code!, proto.message!, proto.data);
72
72
  }
73
73
 
74
74
  toProto() {
package/src/track.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
- import { create } from '@bufbuild/protobuf';
5
4
  import type { AudioSource } from './audio_source.js';
6
5
  import { FfiClient, FfiHandle } from './ffi_client.js';
7
6
  import type {
@@ -12,39 +11,39 @@ import type {
12
11
  TrackInfo,
13
12
  TrackKind,
14
13
  } from './proto/track_pb.js';
15
- import { CreateAudioTrackRequestSchema, CreateVideoTrackRequestSchema } from './proto/track_pb.js';
14
+ import { CreateAudioTrackRequest, CreateVideoTrackRequest } from './proto/track_pb.js';
16
15
  import type { VideoSource } from './video_source.js';
17
16
 
18
17
  export abstract class Track {
19
18
  /** @internal */
20
- info: TrackInfo;
19
+ info?: TrackInfo;
21
20
 
22
21
  /** @internal */
23
22
  ffi_handle: FfiHandle;
24
23
 
25
24
  constructor(owned: OwnedTrack) {
26
- this.info = owned.info!;
27
- this.ffi_handle = new FfiHandle(owned.handle!.id);
25
+ this.info = owned.info;
26
+ this.ffi_handle = new FfiHandle(owned.handle!.id!);
28
27
  }
29
28
 
30
- get sid(): string {
31
- return this.info.sid;
29
+ get sid(): string | undefined {
30
+ return this.info?.sid;
32
31
  }
33
32
 
34
- get name(): string {
35
- return this.info.name;
33
+ get name(): string | undefined {
34
+ return this.info?.name;
36
35
  }
37
36
 
38
- get kind(): TrackKind {
39
- return this.info.kind;
37
+ get kind(): TrackKind | undefined {
38
+ return this.info?.kind;
40
39
  }
41
40
 
42
- get stream_state(): StreamState {
43
- return this.info.streamState;
41
+ get stream_state(): StreamState | undefined {
42
+ return this.info?.streamState;
44
43
  }
45
44
 
46
- get muted(): boolean {
47
- return this.info.muted;
45
+ get muted(): boolean | undefined {
46
+ return this.info?.muted;
48
47
  }
49
48
  }
50
49
 
@@ -54,7 +53,7 @@ export class LocalAudioTrack extends Track {
54
53
  }
55
54
 
56
55
  static createAudioTrack(name: string, source: AudioSource): LocalAudioTrack {
57
- const req = create(CreateAudioTrackRequestSchema, {
56
+ const req = new CreateAudioTrackRequest({
58
57
  name: name,
59
58
  sourceHandle: source.ffiHandle.handle,
60
59
  });
@@ -73,7 +72,7 @@ export class LocalVideoTrack extends Track {
73
72
  }
74
73
 
75
74
  static createVideoTrack(name: string, source: VideoSource): LocalVideoTrack {
76
- const req = create(CreateVideoTrackRequestSchema, {
75
+ const req = new CreateVideoTrackRequest({
77
76
  name: name,
78
77
  sourceHandle: source.ffiHandle.handle,
79
78
  });