@livekit/rtc-node 0.12.2 → 0.13.1

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 +5 -6
  32. package/dist/e2ee.d.ts +5 -6
  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 +60 -36
  54. package/dist/participant.cjs.map +1 -1
  55. package/dist/participant.d.cts +11 -11
  56. package/dist/participant.d.ts +11 -11
  57. package/dist/participant.d.ts.map +1 -1
  58. package/dist/participant.js +78 -52
  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 +85 -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 +85 -123
  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 +41 -42
  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 +102 -73
  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 +137 -189
  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,30 +1,28 @@
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';
8
- import { E2EEManager } from './e2ee.js';
7
+ import { E2EEManager, defaultE2EEOptions } from './e2ee.js';
9
8
  import { FfiClient, FfiClientEvent, FfiHandle } from './ffi_client.js';
10
9
  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,15 @@ 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 };
97
+ options.e2ee = { ...defaultE2EEOptions, ...options.e2ee };
117
98
 
118
- const req = create(ConnectRequestSchema, {
99
+ const req = new ConnectRequest({
119
100
  url: url,
120
101
  token: token,
121
102
  options,
@@ -132,35 +113,34 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
132
113
  return ev.message.case == 'connect' && ev.message.value.asyncId == res.asyncId;
133
114
  });
134
115
 
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;
116
+ switch (cb.message.case) {
117
+ case 'result':
118
+ this.ffiHandle = new FfiHandle(cb.message.value.room!.handle!.id!);
119
+ this.e2eeManager = new E2EEManager(this.ffiHandle.handle, options.e2ee);
140
120
 
141
- this.ffiHandle = new FfiHandle(room!.handle!.id);
142
- if (options.e2ee) {
143
- this.e2eeManager = new E2EEManager(this.ffiHandle.handle, options.e2ee);
144
- }
121
+ this.info = cb.message.value.room!.info;
122
+ this.connectionState = ConnectionState.CONN_CONNECTED;
123
+ this.localParticipant = new LocalParticipant(cb.message.value.localParticipant!);
145
124
 
146
- this.info = room!.info;
147
- this.connectionState = ConnectionState.CONN_CONNECTED;
148
- this.localParticipant = new LocalParticipant(localParticipant!);
125
+ for (const pt of cb.message.value.participants) {
126
+ const rp = this.createRemoteParticipant(pt.participant!);
149
127
 
150
- for (const pt of participants) {
151
- const rp = this.createRemoteParticipant(pt.participant!);
128
+ for (const pub of pt.publications) {
129
+ const publication = new RemoteTrackPublication(pub);
130
+ rp.trackPublications.set(publication.sid!, publication);
131
+ }
132
+ }
152
133
 
153
- for (const pub of pt.publications) {
154
- const publication = new RemoteTrackPublication(pub);
155
- rp.trackPublications.set(publication.sid, publication);
156
- }
134
+ FfiClient.instance.on(FfiClientEvent.FfiEvent, this.onFfiEvent);
135
+ break;
136
+ case 'error':
137
+ default:
138
+ throw new ConnectError(cb.message.value || '');
157
139
  }
158
-
159
- FfiClient.instance.on(FfiClientEvent.FfiEvent, this.onFfiEvent);
160
140
  }
161
141
 
162
142
  async disconnect() {
163
- if (!this.isConnected || !this.ffiHandle) {
143
+ if (!this.isConnected) {
164
144
  return;
165
145
  }
166
146
 
@@ -168,7 +148,7 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
168
148
  message: {
169
149
  case: 'disconnect',
170
150
  value: {
171
- roomHandle: this.ffiHandle.handle,
151
+ roomHandle: this.ffiHandle?.handle,
172
152
  },
173
153
  },
174
154
  });
@@ -178,27 +158,9 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
178
158
  }
179
159
 
180
160
  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 (
161
+ if (
200
162
  ffiEvent.message.case != 'roomEvent' ||
201
- ffiEvent.message.value.roomHandle != this.ffiHandle.handle
163
+ ffiEvent.message.value.roomHandle != this.ffiHandle?.handle
202
164
  ) {
203
165
  return;
204
166
  }
@@ -206,136 +168,125 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
206
168
  const ev = ffiEvent.message.value.message;
207
169
  if (ev.case == 'participantConnected') {
208
170
  const participant = this.createRemoteParticipant(ev.value.info!);
209
- this.remoteParticipants.set(participant.identity, participant);
171
+ this.remoteParticipants.set(participant.identity!, participant);
210
172
  this.emit(RoomEvent.ParticipantConnected, participant);
211
173
  } 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
- }
174
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
175
+ this.remoteParticipants.delete(participant!.identity!);
176
+ this.emit(RoomEvent.ParticipantDisconnected, participant!);
217
177
  } 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);
178
+ const publication = this.localParticipant!.trackPublications.get(ev.value.trackSid!);
179
+ this.emit(RoomEvent.LocalTrackPublished, publication!, this.localParticipant!);
223
180
  } 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
- }
181
+ const publication = this.localParticipant!.trackPublications.get(ev.value.publicationSid!);
182
+ this.localParticipant!.trackPublications.delete(ev.value.publicationSid!);
183
+ this.emit(RoomEvent.LocalTrackUnpublished, publication!, this.localParticipant!);
229
184
  } 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!);
185
+ const publication = this.localParticipant!.trackPublications.get(ev.value.trackSid!);
186
+ publication!.resolveFirstSubscription();
187
+ this.emit(RoomEvent.LocalTrackSubscribed, publication!.track!);
236
188
  } else if (ev.case == 'trackPublished') {
237
- const participant = this.requireRemoteParticipant(ev.value.participantIdentity);
189
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
238
190
  const publication = new RemoteTrackPublication(ev.value.publication!);
239
- participant.trackPublications.set(publication.sid, publication);
240
- this.emit(RoomEvent.TrackPublished, publication, participant);
191
+ participant!.trackPublications.set(publication.sid!, publication);
192
+ this.emit(RoomEvent.TrackPublished, publication, participant!);
241
193
  } 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
- }
194
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
195
+ const publication = participant!.trackPublications.get(ev.value.publicationSid!);
196
+ participant!.trackPublications.delete(ev.value.publicationSid!);
197
+ this.emit(RoomEvent.TrackUnpublished, publication!, participant!);
248
198
  } 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);
199
+ const ownedTrack = ev.value.track;
200
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
201
+ const publication = participant!.trackPublications.get(ownedTrack!.info!.sid!);
202
+ publication!.subscribed = true;
203
+ if (ownedTrack!.info!.kind == TrackKind.KIND_VIDEO) {
204
+ publication!.track = new RemoteVideoTrack(ownedTrack!);
205
+ } else if (ownedTrack!.info!.kind == TrackKind.KIND_AUDIO) {
206
+ publication!.track = new RemoteAudioTrack(ownedTrack!);
260
207
  }
261
208
 
262
- this.emit(RoomEvent.TrackSubscribed, publication.track!, publication, participant);
209
+ this.emit(RoomEvent.TrackSubscribed, publication!.track!, publication!, participant!);
263
210
  } 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);
211
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
212
+ const publication = participant!.trackPublications.get(ev.value.trackSid!);
213
+ publication!.track = undefined;
214
+ publication!.subscribed = false;
215
+ this.emit(RoomEvent.TrackUnsubscribed, publication!.track!, publication!, participant!);
272
216
  } 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,
217
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
218
+ this.emit(
219
+ RoomEvent.TrackSubscriptionFailed,
220
+ ev.value.trackSid!,
221
+ participant!,
222
+ ev.value.error,
279
223
  );
280
- publication.info.muted = true;
281
- if (publication.track) {
282
- publication.track.info.muted = true;
224
+ } else if (ev.case == 'trackMuted') {
225
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
226
+ const publication = participant!.trackPublications.get(ev.value.trackSid!);
227
+ publication!.info!.muted = true;
228
+ if (publication!.track) {
229
+ publication!.track.info!.muted = true;
283
230
  }
284
- this.emit(RoomEvent.TrackMuted, publication, participant);
231
+ this.emit(RoomEvent.TrackMuted, publication!, participant!);
285
232
  } 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;
233
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
234
+ const publication = participant!.trackPublications.get(ev.value.trackSid!);
235
+ publication!.info!.muted = false;
236
+ if (publication!.track) {
237
+ publication!.track.info!.muted = false;
293
238
  }
294
- this.emit(RoomEvent.TrackUnmuted, publication, participant);
239
+ this.emit(RoomEvent.TrackUnmuted, publication!, participant!);
295
240
  } else if (ev.case == 'activeSpeakersChanged') {
296
241
  const activeSpeakers = ev.value.participantIdentities.map((identity) =>
297
- this.requireParticipantByIdentity(identity),
242
+ this.retrieveParticipantByIdentity(identity),
243
+ );
244
+ this.emit(
245
+ RoomEvent.ActiveSpeakersChanged,
246
+ activeSpeakers.map((s) => s!),
298
247
  );
299
- this.emit(RoomEvent.ActiveSpeakersChanged, activeSpeakers);
300
248
  } else if (ev.case == 'roomMetadataChanged') {
301
- this.info.metadata = ev.value.metadata;
302
- this.emit(RoomEvent.RoomMetadataChanged, this.info.metadata);
249
+ this.info!.metadata = ev.value.metadata;
250
+ this.emit(RoomEvent.RoomMetadataChanged, this.info!.metadata!);
303
251
  } 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);
252
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
253
+ participant!.info!.metadata = ev.value.metadata;
254
+ this.emit(RoomEvent.ParticipantMetadataChanged, participant!.metadata, participant!);
307
255
  } 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);
256
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
257
+ participant!.info!.name = ev.value.name;
258
+ this.emit(RoomEvent.ParticipantNameChanged, participant!.name!, participant!);
311
259
  } 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;
260
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
261
+ participant!.info!.attributes = ev.value.attributes.reduce(
262
+ (obj, item) => {
263
+ obj[item.key!] = item.value!;
264
+ return obj;
317
265
  },
318
266
  {} as Record<string, string>,
319
267
  );
320
268
  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>,
269
+ this.emit(
270
+ RoomEvent.ParticipantAttributesChanged,
271
+ ev.value.changedAttributes.reduce(
272
+ (obj, item) => {
273
+ obj[item.key!] = item.value!;
274
+ return obj;
275
+ },
276
+ {} as Record<string, string>,
277
+ ),
278
+ participant!,
327
279
  );
328
- this.emit(RoomEvent.ParticipantAttributesChanged, changedAttributes, participant);
329
280
  }
330
281
  } else if (ev.case == 'connectionQualityChanged') {
331
- const participant = this.requireParticipantByIdentity(ev.value.participantIdentity);
332
- this.emit(RoomEvent.ConnectionQualityChanged, ev.value.quality, participant);
282
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
283
+ this.emit(RoomEvent.ConnectionQualityChanged, ev.value.quality!, participant!);
333
284
  } else if (ev.case == 'chatMessage') {
334
- const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity);
285
+ const participant = this.retrieveParticipantByIdentity(ev.value.participantIdentity!);
335
286
  const { id, message: messageText, timestamp, editTimestamp, generated } = ev.value.message!;
336
287
  const message: ChatMessage = {
337
- id,
338
- message: messageText,
288
+ id: id!,
289
+ message: messageText!,
339
290
  timestamp: Number(timestamp),
340
291
  editTimestamp: Number(editTimestamp),
341
292
  generated,
@@ -343,13 +294,15 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
343
294
  this.emit(RoomEvent.ChatMessage, message, participant);
344
295
  } else if (ev.case == 'dataPacketReceived') {
345
296
  // Can be undefined if the data is sent from a Server SDK
346
- const participant = this.remoteParticipants.get(ev.value.participantIdentity);
297
+ const participant = this.remoteParticipants.get(ev.value.participantIdentity!);
347
298
  const dataPacket = ev.value.value;
348
299
  switch (dataPacket.case) {
349
300
  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();
301
+ const buffer = FfiClient.instance.copyBuffer(
302
+ dataPacket.value.data!.data!.dataPtr!,
303
+ Number(dataPacket.value.data!.data!.dataLen),
304
+ );
305
+ new FfiHandle(dataPacket.value.data!.handle!.id!).dispose();
353
306
  this.emit(
354
307
  RoomEvent.DataReceived,
355
308
  buffer,
@@ -360,7 +313,7 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
360
313
  break;
361
314
  case 'sipDtmf':
362
315
  const { code, digit } = dataPacket.value;
363
- this.emit(RoomEvent.DtmfReceived, code, digit, participant);
316
+ this.emit(RoomEvent.DtmfReceived, code!, digit!, participant!);
364
317
  break;
365
318
  default:
366
319
  break;
@@ -371,18 +324,16 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
371
324
  this.emit(RoomEvent.EncryptionError, new Error('internal server error'));
372
325
  }
373
326
  } else if (ev.case == 'connectionStateChanged') {
374
- this.connectionState = ev.value.state;
327
+ this.connectionState = ev.value.state!;
375
328
  this.emit(RoomEvent.ConnectionStateChanged, this.connectionState);
376
329
  /*} else if (ev.case == 'connected') {
377
330
  this.emit(RoomEvent.Connected);*/
378
331
  } else if (ev.case == 'disconnected') {
379
- this.emit(RoomEvent.Disconnected, ev.value.reason);
332
+ this.emit(RoomEvent.Disconnected, ev.value.reason!);
380
333
  } else if (ev.case == 'reconnecting') {
381
334
  this.emit(RoomEvent.Reconnecting);
382
335
  } else if (ev.case == 'reconnected') {
383
336
  this.emit(RoomEvent.Reconnected);
384
- } else if (ev.case == 'roomSidChanged') {
385
- this.emit(RoomEvent.RoomSidChanged, ev.value.sid);
386
337
  }
387
338
  };
388
339
 
@@ -431,12 +382,12 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
431
382
  }
432
383
 
433
384
  private createRemoteParticipant(ownedInfo: OwnedParticipant) {
434
- if (this.remoteParticipants.has(ownedInfo.info!.identity)) {
385
+ if (this.remoteParticipants.has(ownedInfo.info!.identity!)) {
435
386
  throw new Error('Participant already exists');
436
387
  }
437
388
 
438
389
  const participant = new RemoteParticipant(ownedInfo);
439
- this.remoteParticipants.set(ownedInfo.info!.identity, participant);
390
+ this.remoteParticipants.set(ownedInfo.info!.identity!, participant);
440
391
  return participant;
441
392
  }
442
393
  }
@@ -477,7 +428,6 @@ export type RoomCallbacks = {
477
428
  trackUnmuted: (publication: TrackPublication, participant: Participant) => void;
478
429
  activeSpeakersChanged: (speakers: Participant[]) => void;
479
430
  roomMetadataChanged: (metadata: string) => void;
480
- roomInfoUpdated: (info: RoomInfo) => void;
481
431
  participantMetadataChanged: (metadata: string | undefined, participant: Participant) => void;
482
432
  participantNameChanged: (name: string, participant: Participant) => void;
483
433
  participantAttributesChanged: (
@@ -492,14 +442,13 @@ export type RoomCallbacks = {
492
442
  topic?: string,
493
443
  ) => void;
494
444
  chatMessage: (message: ChatMessage, participant?: Participant) => void;
495
- dtmfReceived: (code: number, digit: string, participant?: RemoteParticipant) => void;
445
+ dtmfReceived: (code: number, digit: string, participant: RemoteParticipant) => void;
496
446
  encryptionError: (error: Error) => void;
497
447
  connectionStateChanged: (state: ConnectionState) => void;
498
448
  connected: () => void;
499
449
  disconnected: (reason: DisconnectReason) => void;
500
450
  reconnecting: () => void;
501
451
  reconnected: () => void;
502
- roomSidChanged: (sid: string) => void;
503
452
  };
504
453
 
505
454
  export enum RoomEvent {
@@ -517,7 +466,6 @@ export enum RoomEvent {
517
466
  TrackUnmuted = 'trackUnmuted',
518
467
  ActiveSpeakersChanged = 'activeSpeakersChanged',
519
468
  RoomMetadataChanged = 'roomMetadataChanged',
520
- RoomSidChanged = 'roomSidChanged',
521
469
  ParticipantMetadataChanged = 'participantMetadataChanged',
522
470
  ParticipantNameChanged = 'participantNameChanged',
523
471
  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
  });