@livekit/rtc-node 0.0.7 → 0.2.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 (113) hide show
  1. package/build.rs +4 -0
  2. package/dist/audio_frame.d.ts +1 -1
  3. package/dist/audio_frame.d.ts.map +1 -1
  4. package/dist/audio_frame.js +10 -11
  5. package/dist/audio_frame.js.map +1 -1
  6. package/dist/audio_source.d.ts +3 -3
  7. package/dist/audio_source.d.ts.map +1 -1
  8. package/dist/audio_source.js +14 -15
  9. package/dist/audio_source.js.map +1 -1
  10. package/dist/audio_stream.d.ts +8 -5
  11. package/dist/audio_stream.d.ts.map +1 -1
  12. package/dist/audio_stream.js +18 -22
  13. package/dist/audio_stream.js.map +1 -1
  14. package/dist/e2ee.d.ts +1 -1
  15. package/dist/e2ee.d.ts.map +1 -1
  16. package/dist/e2ee.js +42 -45
  17. package/dist/e2ee.js.map +1 -1
  18. package/dist/ffi_client.d.ts +2 -2
  19. package/dist/ffi_client.d.ts.map +1 -1
  20. package/dist/ffi_client.js +17 -24
  21. package/dist/ffi_client.js.map +1 -1
  22. package/dist/index.d.ts +15 -15
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +18 -70
  25. package/dist/index.js.map +1 -1
  26. package/{src/native.js → dist/napi/native.cjs} +12 -0
  27. package/dist/napi/native.js +15 -0
  28. package/dist/participant.d.ts +24 -9
  29. package/dist/participant.d.ts.map +1 -1
  30. package/dist/participant.js +45 -45
  31. package/dist/participant.js.map +1 -1
  32. package/dist/proto/audio_frame_pb.d.ts +0 -46
  33. package/dist/proto/audio_frame_pb.d.ts.map +1 -1
  34. package/dist/proto/audio_frame_pb.js +138 -236
  35. package/dist/proto/audio_frame_pb.js.map +1 -1
  36. package/dist/proto/e2ee_pb.js +134 -162
  37. package/dist/proto/e2ee_pb.js.map +1 -1
  38. package/dist/proto/ffi_pb.d.ts +31 -44
  39. package/dist/proto/ffi_pb.d.ts.map +1 -1
  40. package/dist/proto/ffi_pb.js +140 -126
  41. package/dist/proto/ffi_pb.js.map +1 -1
  42. package/dist/proto/handle_pb.js +7 -11
  43. package/dist/proto/handle_pb.js.map +1 -1
  44. package/dist/proto/participant_pb.js +13 -18
  45. package/dist/proto/participant_pb.js.map +1 -1
  46. package/dist/proto/room_pb.d.ts +5 -1
  47. package/dist/proto/room_pb.d.ts.map +1 -1
  48. package/dist/proto/room_pb.js +393 -454
  49. package/dist/proto/room_pb.js.map +1 -1
  50. package/dist/proto/stats_pb.js +265 -303
  51. package/dist/proto/stats_pb.js.map +1 -1
  52. package/dist/proto/track_pb.js +87 -102
  53. package/dist/proto/track_pb.js.map +1 -1
  54. package/dist/proto/video_frame_pb.d.ts +124 -390
  55. package/dist/proto/video_frame_pb.d.ts.map +1 -1
  56. package/dist/proto/video_frame_pb.js +252 -620
  57. package/dist/proto/video_frame_pb.js.map +1 -1
  58. package/dist/room.d.ts +10 -9
  59. package/dist/room.d.ts.map +1 -1
  60. package/dist/room.js +74 -76
  61. package/dist/room.js.map +1 -1
  62. package/dist/track.d.ts +4 -4
  63. package/dist/track.d.ts.map +1 -1
  64. package/dist/track.js +15 -20
  65. package/dist/track.js.map +1 -1
  66. package/dist/track_publication.d.ts +4 -4
  67. package/dist/track_publication.d.ts.map +1 -1
  68. package/dist/track_publication.js +12 -15
  69. package/dist/track_publication.js.map +1 -1
  70. package/dist/video_frame.d.ts +8 -103
  71. package/dist/video_frame.d.ts.map +1 -1
  72. package/dist/video_frame.js +161 -341
  73. package/dist/video_frame.js.map +1 -1
  74. package/dist/video_source.d.ts +4 -4
  75. package/dist/video_source.d.ts.map +1 -1
  76. package/dist/video_source.js +16 -22
  77. package/dist/video_source.js.map +1 -1
  78. package/dist/video_stream.d.ts +10 -5
  79. package/dist/video_stream.d.ts.map +1 -1
  80. package/dist/video_stream.js +20 -24
  81. package/dist/video_stream.js.map +1 -1
  82. package/package.json +14 -14
  83. package/src/audio_frame.ts +6 -2
  84. package/src/audio_source.ts +8 -4
  85. package/src/audio_stream.ts +15 -7
  86. package/src/e2ee.ts +6 -2
  87. package/src/ffi_client.ts +6 -2
  88. package/src/index.ts +19 -28
  89. package/src/lib.rs +4 -0
  90. package/src/napi/native.cjs +281 -0
  91. package/src/napi/native.js +15 -0
  92. package/src/nodejs.rs +4 -0
  93. package/src/participant.ts +45 -22
  94. package/src/proto/audio_frame_pb.ts +1 -91
  95. package/src/proto/e2ee_pb.ts +1 -1
  96. package/src/proto/ffi_pb.ts +55 -53
  97. package/src/proto/handle_pb.ts +1 -1
  98. package/src/proto/participant_pb.ts +1 -1
  99. package/src/proto/room_pb.ts +7 -1
  100. package/src/proto/stats_pb.ts +1 -1
  101. package/src/proto/track_pb.ts +1 -1
  102. package/src/proto/video_frame_pb.ts +186 -641
  103. package/src/room.ts +56 -48
  104. package/src/track.ts +8 -4
  105. package/src/track_publication.ts +10 -6
  106. package/src/video_frame.ts +186 -579
  107. package/src/video_source.ts +12 -12
  108. package/src/video_stream.ts +21 -10
  109. package/dist/native.d.ts.map +0 -1
  110. package/dist/native.js +0 -297
  111. package/dist/native.js.map +0 -1
  112. /package/dist/{native.d.ts → napi/native.d.ts} +0 -0
  113. /package/src/{native.d.ts → napi/native.d.ts} +0 -0
package/src/room.ts CHANGED
@@ -1,8 +1,12 @@
1
- import { FfiClient, FfiClientEvent, FfiHandle } from './ffi_client';
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ import { FfiClient, FfiClientEvent, FfiHandle } from './ffi_client.js';
2
6
  import EventEmitter from 'events';
3
7
  import TypedEmitter from 'typed-emitter';
4
- import { FfiEvent } from './proto/ffi_pb';
5
- import { LocalParticipant, Participant, RemoteParticipant } from './participant';
8
+ import { FfiEvent } from './proto/ffi_pb.js';
9
+ import { LocalParticipant, Participant, RemoteParticipant } from './participant.js';
6
10
  import {
7
11
  ConnectCallback,
8
12
  ConnectRequest,
@@ -15,17 +19,17 @@ import {
15
19
  IceServer,
16
20
  IceTransportType,
17
21
  RoomInfo,
18
- } from './proto/room_pb';
19
- import { E2EEManager, E2EEOptions, defaultE2EEOptions } from './e2ee';
20
- import { OwnedParticipant } from './proto/participant_pb';
22
+ } from './proto/room_pb.js';
23
+ import { E2EEManager, E2EEOptions, defaultE2EEOptions } from './e2ee.js';
24
+ import { OwnedParticipant } from './proto/participant_pb.js';
21
25
  import {
22
26
  LocalTrackPublication,
23
27
  RemoteTrackPublication,
24
28
  TrackPublication,
25
- } from './track_publication';
26
- import { LocalTrack, RemoteAudioTrack, RemoteTrack, RemoteVideoTrack } from './track';
27
- import { TrackKind } from './proto/track_pb';
28
- import { EncryptionState } from './proto/e2ee_pb';
29
+ } from './track_publication.js';
30
+ import { LocalTrack, RemoteAudioTrack, RemoteTrack, RemoteVideoTrack } from './track.js';
31
+ import { TrackKind } from './proto/track_pb.js';
32
+ import { EncryptionState } from './proto/e2ee_pb.js';
29
33
 
30
34
  export interface RtcConfiguration {
31
35
  iceTransportType: IceTransportType;
@@ -58,19 +62,15 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
58
62
  private ffiHandle?: FfiHandle;
59
63
 
60
64
  e2eeManager: E2EEManager;
61
- connection_state: ConnectionState = ConnectionState.CONN_DISCONNECTED;
65
+ connectionState: ConnectionState = ConnectionState.CONN_DISCONNECTED;
62
66
 
63
- participants: Map<string, RemoteParticipant> = new Map();
67
+ remoteParticipants: Map<string, RemoteParticipant> = new Map();
64
68
  localParticipant?: LocalParticipant;
65
69
 
66
70
  constructor() {
67
71
  super();
68
72
  }
69
73
 
70
- get sid(): string {
71
- return this.info.sid;
72
- }
73
-
74
74
  get name(): string {
75
75
  return this.info.name;
76
76
  }
@@ -80,9 +80,11 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
80
80
  }
81
81
 
82
82
  get isConnected(): boolean {
83
- return (
84
- this.ffiHandle != undefined && this.connection_state != ConnectionState.CONN_DISCONNECTED
85
- );
83
+ return this.ffiHandle != undefined && this.connectionState != ConnectionState.CONN_DISCONNECTED;
84
+ }
85
+
86
+ async getSid(): Promise<string> {
87
+ return this.info.sid; // TODO update this to handle async room updates once rust protocol has been updated
86
88
  }
87
89
 
88
90
  async connect(url: string, token: string, opts?: RoomOptions) {
@@ -125,7 +127,7 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
125
127
  this.e2eeManager = new E2EEManager(this.ffiHandle.handle, options.e2ee);
126
128
 
127
129
  this.info = cb.room.info;
128
- this.connection_state = ConnectionState.CONN_CONNECTED;
130
+ this.connectionState = ConnectionState.CONN_CONNECTED;
129
131
  this.localParticipant = new LocalParticipant(cb.localParticipant);
130
132
 
131
133
  for (let pt of cb.participants) {
@@ -133,7 +135,7 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
133
135
 
134
136
  for (let pub of pt.publications) {
135
137
  let publication = new RemoteTrackPublication(pub);
136
- rp.tracks.set(publication.sid, publication);
138
+ rp.trackPublications.set(publication.sid, publication);
137
139
  }
138
140
  }
139
141
 
@@ -166,33 +168,33 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
166
168
  let ev = ffiEvent.message.value.message;
167
169
  if (ev.case == 'participantConnected') {
168
170
  let participant = this.createRemoteParticipant(ev.value.info);
169
- this.participants.set(participant.sid, participant);
171
+ this.remoteParticipants.set(participant.identity, participant);
170
172
  this.emit(RoomEvent.ParticipantConnected, participant);
171
173
  } else if (ev.case == 'participantDisconnected') {
172
- let participant = this.participants.get(ev.value.participantSid);
173
- this.participants.delete(ev.value.participantSid);
174
+ let participant = this.getRemoteParticipantBySid(ev.value.participantSid);
175
+ this.remoteParticipants.delete(participant.identity);
174
176
  this.emit(RoomEvent.ParticipantDisconnected, participant);
175
177
  } else if (ev.case == 'localTrackPublished') {
176
- let publication = this.localParticipant.tracks.get(ev.value.trackSid);
178
+ let publication = this.localParticipant.trackPublications.get(ev.value.trackSid);
177
179
  this.emit(RoomEvent.LocalTrackPublished, publication, publication.track);
178
180
  } else if (ev.case == 'localTrackUnpublished') {
179
- let publication = this.localParticipant.tracks.get(ev.value.publicationSid);
180
- this.localParticipant.tracks.delete(ev.value.publicationSid);
181
+ let publication = this.localParticipant.trackPublications.get(ev.value.publicationSid);
182
+ this.localParticipant.trackPublications.delete(ev.value.publicationSid);
181
183
  this.emit(RoomEvent.LocalTrackUnpublished, publication);
182
184
  } else if (ev.case == 'trackPublished') {
183
- let participant = this.participants.get(ev.value.participantSid);
185
+ let participant = this.getRemoteParticipantBySid(ev.value.participantSid);
184
186
  let publication = new RemoteTrackPublication(ev.value.publication);
185
- participant.tracks.set(publication.sid, publication);
187
+ participant.trackPublications.set(publication.sid, publication);
186
188
  this.emit(RoomEvent.TrackPublished, publication, participant);
187
189
  } else if (ev.case == 'trackUnpublished') {
188
- let participant = this.participants.get(ev.value.participantSid);
189
- let publication = participant.tracks.get(ev.value.publicationSid);
190
- participant.tracks.delete(ev.value.publicationSid);
190
+ let participant = this.getRemoteParticipantBySid(ev.value.participantSid);
191
+ let publication = participant.trackPublications.get(ev.value.publicationSid);
192
+ participant.trackPublications.delete(ev.value.publicationSid);
191
193
  this.emit(RoomEvent.TrackUnpublished, publication, participant);
192
194
  } else if (ev.case == 'trackSubscribed') {
193
195
  let ownedTrack = ev.value.track;
194
- let participant = this.participants.get(ev.value.participantSid);
195
- let publication = participant.tracks.get(ownedTrack.info.sid);
196
+ let participant = this.getRemoteParticipantBySid(ev.value.participantSid);
197
+ let publication = participant.trackPublications.get(ownedTrack.info.sid);
196
198
  publication.subscribed = true;
197
199
  if (ownedTrack.info.kind == TrackKind.KIND_VIDEO) {
198
200
  publication.track = new RemoteVideoTrack(ownedTrack);
@@ -202,17 +204,17 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
202
204
 
203
205
  this.emit(RoomEvent.TrackSubscribed, publication.track, publication, participant);
204
206
  } else if (ev.case == 'trackUnsubscribed') {
205
- let participant = this.participants.get(ev.value.participantSid);
206
- let publication = participant.tracks.get(ev.value.trackSid);
207
+ let participant = this.getRemoteParticipantBySid(ev.value.participantSid);
208
+ let publication = participant.trackPublications.get(ev.value.trackSid);
207
209
  publication.track = undefined;
208
210
  publication.subscribed = false;
209
211
  this.emit(RoomEvent.TrackUnsubscribed, publication.track, publication, participant);
210
212
  } else if (ev.case == 'trackSubscriptionFailed') {
211
- let participant = this.participants.get(ev.value.participantSid);
213
+ let participant = this.getRemoteParticipantBySid(ev.value.participantSid);
212
214
  this.emit(RoomEvent.TrackSubscriptionFailed, participant, ev.value.trackSid, ev.value.error);
213
215
  } else if (ev.case == 'trackMuted') {
214
216
  let participant = this.retrieveParticipant(ev.value.participantSid);
215
- let publication = participant.tracks.get(ev.value.trackSid);
217
+ let publication = participant.trackPublications.get(ev.value.trackSid);
216
218
  publication.info.muted = true;
217
219
  if (publication.track) {
218
220
  publication.track.info.muted = true;
@@ -220,14 +222,16 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
220
222
  this.emit(RoomEvent.TrackMuted, participant, publication);
221
223
  } else if (ev.case == 'trackUnmuted') {
222
224
  let participant = this.retrieveParticipant(ev.value.participantSid);
223
- let publication = participant.tracks.get(ev.value.trackSid);
225
+ let publication = participant.trackPublications.get(ev.value.trackSid);
224
226
  publication.info.muted = false;
225
227
  if (publication.track) {
226
228
  publication.track.info.muted = false;
227
229
  }
228
230
  this.emit(RoomEvent.TrackUnmuted, participant, publication);
229
231
  } else if (ev.case == 'activeSpeakersChanged') {
230
- let activeSpeakers = ev.value.participantSids.map((sid) => this.participants.get(sid));
232
+ let activeSpeakers = ev.value.participantSids.map((sid) =>
233
+ this.getRemoteParticipantBySid(sid),
234
+ );
231
235
  this.emit(RoomEvent.ActiveSpeakersChanged, activeSpeakers);
232
236
  } else if (ev.case == 'roomMetadataChanged') {
233
237
  let oldMetadata = this.info.metadata;
@@ -253,7 +257,7 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
253
257
  this.emit(RoomEvent.ConnectionQualityChanged, participant, ev.value.quality);
254
258
  } else if (ev.case == 'dataReceived') {
255
259
  // Can be undefined if the data is sent from a Server SDK
256
- let participant = this.participants.get(ev.value.participantSid);
260
+ let participant = this.getRemoteParticipantBySid(ev.value.participantSid);
257
261
  let info = ev.value.data;
258
262
  let buffer = FfiClient.instance.copyBuffer(info.data.dataPtr, Number(info.data.dataLen));
259
263
  new FfiHandle(info.handle.id).dispose();
@@ -262,8 +266,8 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
262
266
  let participant = this.retrieveParticipant(ev.value.participantSid);
263
267
  this.emit(RoomEvent.E2EEStateChanged, participant, ev.value.state);
264
268
  } else if (ev.case == 'connectionStateChanged') {
265
- this.connection_state = ev.value.state;
266
- this.emit(RoomEvent.ConenctionStateChanged, this.connection_state);
269
+ this.connectionState = ev.value.state;
270
+ this.emit(RoomEvent.ConenctionStateChanged, this.connectionState);
267
271
  /*} else if (ev.case == 'connected') {
268
272
  this.emit(RoomEvent.Connected);*/
269
273
  } else if (ev.case == 'disconnected') {
@@ -273,23 +277,27 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
273
277
  } else if (ev.case == 'reconnected') {
274
278
  this.emit(RoomEvent.Reconnected);
275
279
  }
280
+ };
281
+
282
+ getRemoteParticipantBySid(sid: string) {
283
+ return Array.from(this.remoteParticipants.values()).find((p) => p.sid === sid);
276
284
  }
277
285
 
278
286
  private retrieveParticipant(sid: string): Participant {
279
- if (this.localParticipant.sid == sid) {
287
+ if (this.localParticipant.sid === sid) {
280
288
  return this.localParticipant;
281
289
  } else {
282
- return this.participants.get(sid);
290
+ return this.getRemoteParticipantBySid(sid);
283
291
  }
284
292
  }
285
293
 
286
294
  private createRemoteParticipant(ownedInfo: OwnedParticipant) {
287
- if (this.participants.has(ownedInfo.info.sid)) {
295
+ if (this.remoteParticipants.has(ownedInfo.info.identity)) {
288
296
  throw new Error('Participant already exists');
289
297
  }
290
298
 
291
- let participant = new RemoteParticipant(ownedInfo);
292
- this.participants.set(ownedInfo.info.sid, participant);
299
+ const participant = new RemoteParticipant(ownedInfo);
300
+ this.remoteParticipants.set(ownedInfo.info.identity, participant);
293
301
  return participant;
294
302
  }
295
303
  }
package/src/track.ts CHANGED
@@ -1,5 +1,9 @@
1
- import { AudioSource } from './audio_source';
2
- import { FfiClient, FfiHandle, FfiRequest } from './ffi_client';
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ import { AudioSource } from './audio_source.js';
6
+ import { FfiClient, FfiHandle, FfiRequest } from './ffi_client.js';
3
7
  import {
4
8
  CreateAudioTrackRequest,
5
9
  CreateAudioTrackResponse,
@@ -9,8 +13,8 @@ import {
9
13
  StreamState,
10
14
  TrackInfo,
11
15
  TrackKind,
12
- } from './proto/track_pb';
13
- import { VideoSource } from './video_source';
16
+ } from './proto/track_pb.js';
17
+ import { VideoSource } from './video_source.js';
14
18
 
15
19
  export abstract class Track {
16
20
  /** @internal */
@@ -1,14 +1,18 @@
1
- import { FfiClient, FfiRequest } from './ffi_client';
2
- import { FfiHandle } from './native';
3
- import { EncryptionType } from './proto/e2ee_pb';
4
- import { SetSubscribedRequest, SetSubscribedResponse } from './proto/room_pb';
1
+ // SPDX-FileCopyrightText: 2024 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ import { FfiClient, FfiRequest } from './ffi_client.js';
6
+ import { FfiHandle } from './napi/native.js';
7
+ import { EncryptionType } from './proto/e2ee_pb.js';
8
+ import { SetSubscribedRequest, SetSubscribedResponse } from './proto/room_pb.js';
5
9
  import {
6
10
  OwnedTrackPublication,
7
11
  TrackKind,
8
12
  TrackPublicationInfo,
9
13
  TrackSource,
10
- } from './proto/track_pb';
11
- import { Track } from './track';
14
+ } from './proto/track_pb.js';
15
+ import { Track } from './track.js';
12
16
 
13
17
  export abstract class TrackPublication {
14
18
  /** @internal */