livekit-client 0.16.5 → 0.16.6

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.
package/src/room/Room.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { EventEmitter } from 'events';
2
+ import type TypedEmitter from 'typed-emitter';
2
3
  import { toProtoSessionDescription } from '../api/SignalClient';
3
4
  import log from '../logger';
4
5
  import { RoomConnectOptions, RoomOptions } from '../options';
@@ -44,7 +45,7 @@ export enum RoomState {
44
45
  *
45
46
  * @noInheritDoc
46
47
  */
47
- class Room extends EventEmitter {
48
+ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>) {
48
49
  state: RoomState = RoomState.Disconnected;
49
50
 
50
51
  /** map of sid: [[RemoteParticipant]] */
@@ -145,10 +146,12 @@ class Room extends EventEmitter {
145
146
  .on(EngineEvent.Resuming, () => {
146
147
  this.state = RoomState.Reconnecting;
147
148
  this.emit(RoomEvent.Reconnecting);
149
+ this.emit(RoomEvent.StateChanged, this.state);
148
150
  })
149
151
  .on(EngineEvent.Resumed, () => {
150
152
  this.state = RoomState.Connected;
151
153
  this.emit(RoomEvent.Reconnected);
154
+ this.emit(RoomEvent.StateChanged, this.state);
152
155
  this.updateSubscriptions();
153
156
  })
154
157
  .on(EngineEvent.SignalResumed, () => {
@@ -205,6 +208,7 @@ class Room extends EventEmitter {
205
208
  }
206
209
 
207
210
  this.state = RoomState.Connected;
211
+ this.emit(RoomEvent.StateChanged, this.state);
208
212
  const pi = joinResponse.participant!;
209
213
  this.localParticipant = new LocalParticipant(
210
214
  pi.sid,
@@ -216,10 +220,10 @@ class Room extends EventEmitter {
216
220
  this.localParticipant.updateInfo(pi);
217
221
  // forward metadata changed for the local participant
218
222
  this.localParticipant
219
- .on(ParticipantEvent.MetadataChanged, (metadata: object) => {
223
+ .on(ParticipantEvent.MetadataChanged, (metadata: string | undefined) => {
220
224
  this.emit(RoomEvent.MetadataChanged, metadata, this.localParticipant);
221
225
  })
222
- .on(ParticipantEvent.ParticipantMetadataChanged, (metadata: object) => {
226
+ .on(ParticipantEvent.ParticipantMetadataChanged, (metadata: string | undefined) => {
223
227
  this.emit(RoomEvent.ParticipantMetadataChanged, metadata, this.localParticipant);
224
228
  })
225
229
  .on(ParticipantEvent.TrackMuted, (pub: TrackPublication) => {
@@ -445,6 +449,7 @@ class Room extends EventEmitter {
445
449
  private handleRestarting = () => {
446
450
  this.state = RoomState.Reconnecting;
447
451
  this.emit(RoomEvent.Reconnecting);
452
+ this.emit(RoomEvent.StateChanged, this.state);
448
453
 
449
454
  // also unwind existing participants & existing subscriptions
450
455
  for (const p of this.participants.values()) {
@@ -455,6 +460,7 @@ class Room extends EventEmitter {
455
460
  private handleRestarted = async (joinResponse: JoinResponse) => {
456
461
  this.state = RoomState.Connected;
457
462
  this.emit(RoomEvent.Reconnected);
463
+ this.emit(RoomEvent.StateChanged, this.state);
458
464
 
459
465
  // rehydrate participants
460
466
  if (joinResponse.participant) {
@@ -509,6 +515,7 @@ class Room extends EventEmitter {
509
515
  navigator.mediaDevices.removeEventListener('devicechange', this.handleDeviceChange);
510
516
  this.state = RoomState.Disconnected;
511
517
  this.emit(RoomEvent.Disconnected);
518
+ this.emit(RoomEvent.StateChanged, this.state);
512
519
  }
513
520
 
514
521
  private handleParticipantUpdates = (participantInfos: ParticipantInfo[]) => {
@@ -717,66 +724,72 @@ class Room extends EventEmitter {
717
724
  }
718
725
  }
719
726
 
727
+ private createParticipant(id: string, info?: ParticipantInfo): RemoteParticipant {
728
+ let participant: RemoteParticipant;
729
+ if (info) {
730
+ participant = RemoteParticipant.fromParticipantInfo(
731
+ this.engine.client,
732
+ info,
733
+ );
734
+ } else {
735
+ participant = new RemoteParticipant(this.engine.client, id, '');
736
+ }
737
+ return participant;
738
+ }
739
+
720
740
  private getOrCreateParticipant(
721
741
  id: string,
722
742
  info?: ParticipantInfo,
723
743
  ): RemoteParticipant {
724
- let participant = this.participants.get(id);
725
- if (!participant) {
726
- // it's possible for the RTC track to arrive before signaling data
727
- // when this happens, we'll create the participant and make the track work
728
- if (info) {
729
- participant = RemoteParticipant.fromParticipantInfo(
730
- this.engine.client,
731
- info,
732
- );
733
- } else {
734
- participant = new RemoteParticipant(this.engine.client, id, '');
735
- }
736
- this.participants.set(id, participant);
737
- // also forward events
738
-
739
- // trackPublished is only fired for tracks added after both local participant
740
- // and remote participant joined the room
741
- participant
742
- .on(ParticipantEvent.TrackPublished, (trackPublication: RemoteTrackPublication) => {
743
- this.emit(RoomEvent.TrackPublished, trackPublication, participant);
744
- })
745
- .on(ParticipantEvent.TrackSubscribed,
746
- (track: RemoteTrack, publication: RemoteTrackPublication) => {
747
- // monitor playback status
748
- if (track.kind === Track.Kind.Audio) {
749
- track.on(TrackEvent.AudioPlaybackStarted, this.handleAudioPlaybackStarted);
750
- track.on(TrackEvent.AudioPlaybackFailed, this.handleAudioPlaybackFailed);
751
- }
752
- this.emit(RoomEvent.TrackSubscribed, track, publication, participant);
753
- })
754
- .on(ParticipantEvent.TrackUnpublished, (publication: RemoteTrackPublication) => {
755
- this.emit(RoomEvent.TrackUnpublished, publication, participant);
756
- })
757
- .on(ParticipantEvent.TrackUnsubscribed,
758
- (track: RemoteTrack, publication: RemoteTrackPublication) => {
759
- this.emit(RoomEvent.TrackUnsubscribed, track, publication, participant);
760
- })
761
- .on(ParticipantEvent.TrackSubscriptionFailed, (sid: string) => {
762
- this.emit(RoomEvent.TrackSubscriptionFailed, sid, participant);
763
- })
764
- .on(ParticipantEvent.TrackMuted, (pub: TrackPublication) => {
765
- this.emit(RoomEvent.TrackMuted, pub, participant);
766
- })
767
- .on(ParticipantEvent.TrackUnmuted, (pub: TrackPublication) => {
768
- this.emit(RoomEvent.TrackUnmuted, pub, participant);
769
- })
770
- .on(ParticipantEvent.MetadataChanged, (metadata: any) => {
771
- this.emit(RoomEvent.MetadataChanged, metadata, participant);
744
+ if (this.participants.has(id)) {
745
+ return (this.participants.get(id) as RemoteParticipant);
746
+ }
747
+ // it's possible for the RTC track to arrive before signaling data
748
+ // when this happens, we'll create the participant and make the track work
749
+ const participant = this.createParticipant(id, info);
750
+ this.participants.set(id, participant);
751
+
752
+ // also forward events
753
+ // trackPublished is only fired for tracks added after both local participant
754
+ // and remote participant joined the room
755
+ participant
756
+ .on(ParticipantEvent.TrackPublished, (trackPublication: RemoteTrackPublication) => {
757
+ this.emit(RoomEvent.TrackPublished, trackPublication, participant);
758
+ })
759
+ .on(ParticipantEvent.TrackSubscribed,
760
+ (track: RemoteTrack, publication: RemoteTrackPublication) => {
761
+ // monitor playback status
762
+ if (track.kind === Track.Kind.Audio) {
763
+ track.on(TrackEvent.AudioPlaybackStarted, this.handleAudioPlaybackStarted);
764
+ track.on(TrackEvent.AudioPlaybackFailed, this.handleAudioPlaybackFailed);
765
+ }
766
+ this.emit(RoomEvent.TrackSubscribed, track, publication, participant);
772
767
  })
773
- .on(ParticipantEvent.ParticipantMetadataChanged, (metadata: any) => {
774
- this.emit(RoomEvent.ParticipantMetadataChanged, metadata, participant);
768
+ .on(ParticipantEvent.TrackUnpublished, (publication: RemoteTrackPublication) => {
769
+ this.emit(RoomEvent.TrackUnpublished, publication, participant);
770
+ })
771
+ .on(ParticipantEvent.TrackUnsubscribed,
772
+ (track: RemoteTrack, publication: RemoteTrackPublication) => {
773
+ this.emit(RoomEvent.TrackUnsubscribed, track, publication, participant);
775
774
  })
776
- .on(ParticipantEvent.ConnectionQualityChanged, (quality: ConnectionQuality) => {
777
- this.emit(RoomEvent.ConnectionQualityChanged, quality, participant);
778
- });
779
- }
775
+ .on(ParticipantEvent.TrackSubscriptionFailed, (sid: string) => {
776
+ this.emit(RoomEvent.TrackSubscriptionFailed, sid, participant);
777
+ })
778
+ .on(ParticipantEvent.TrackMuted, (pub: TrackPublication) => {
779
+ this.emit(RoomEvent.TrackMuted, pub, participant);
780
+ })
781
+ .on(ParticipantEvent.TrackUnmuted, (pub: TrackPublication) => {
782
+ this.emit(RoomEvent.TrackUnmuted, pub, participant);
783
+ })
784
+ .on(ParticipantEvent.MetadataChanged, (metadata: string | undefined) => {
785
+ this.emit(RoomEvent.MetadataChanged, metadata, participant);
786
+ })
787
+ .on(ParticipantEvent.ParticipantMetadataChanged, (metadata: string | undefined) => {
788
+ this.emit(RoomEvent.ParticipantMetadataChanged, metadata, participant);
789
+ })
790
+ .on(ParticipantEvent.ConnectionQualityChanged, (quality: ConnectionQuality) => {
791
+ this.emit(RoomEvent.ConnectionQualityChanged, quality, participant);
792
+ });
780
793
  return participant;
781
794
  }
782
795
 
@@ -830,11 +843,74 @@ class Room extends EventEmitter {
830
843
  }
831
844
  }
832
845
 
833
- /** @internal */
834
- emit(event: string | symbol, ...args: any[]): boolean {
846
+ // /** @internal */
847
+ emit<E extends keyof RoomEventCallbacks>(
848
+ event: E, ...args: Parameters<RoomEventCallbacks[E]>
849
+ ): boolean {
835
850
  log.debug('room event', event, ...args);
836
851
  return super.emit(event, ...args);
837
852
  }
838
853
  }
839
854
 
840
855
  export default Room;
856
+
857
+ export type RoomEventCallbacks = {
858
+ reconnecting: () => void,
859
+ reconnected: () => void,
860
+ disconnected: () => void,
861
+ stateChanged: (state: RoomState) => void,
862
+ mediaDevicesChanged: () => void,
863
+ participantConnected: (participant: RemoteParticipant) => void,
864
+ participantDisconnected: (participant: RemoteParticipant) => void,
865
+ trackPublished: (publication: RemoteTrackPublication, participant: RemoteParticipant) => void,
866
+ trackSubscribed: (
867
+ track: RemoteTrack,
868
+ publication: RemoteTrackPublication,
869
+ participant: RemoteParticipant
870
+ ) => void,
871
+ trackSubscriptionFailed: (trackSid: string, participant: RemoteParticipant) => void,
872
+ trackUnpublished: (publication: RemoteTrackPublication, participant: RemoteParticipant) => void,
873
+ trackUnsubscribed: (
874
+ track: RemoteTrack,
875
+ publication: RemoteTrackPublication,
876
+ participant: RemoteParticipant,
877
+ ) => void,
878
+ trackMuted: (publication: TrackPublication, participant: Participant) => void,
879
+ trackUnmuted: (publication: TrackPublication, participant: Participant) => void,
880
+ localTrackPublished: (publication: LocalTrackPublication, participant: LocalParticipant) => void,
881
+ localTrackUnpublished: (
882
+ publication: LocalTrackPublication,
883
+ participant: LocalParticipant
884
+ ) => void,
885
+ /**
886
+ * @deprecated use [[participantMetadataChanged]] instead
887
+ */
888
+ metadataChanged: (
889
+ metadata: string | undefined,
890
+ participant?: RemoteParticipant | LocalParticipant
891
+ ) => void,
892
+ participantMetadataChanged: (
893
+ metadata: string | undefined,
894
+ participant: RemoteParticipant | LocalParticipant
895
+ ) => void,
896
+ activeSpeakersChanged: (speakers: Array<Participant>) => void,
897
+ roomMetadataChanged: (metadata: string) => void,
898
+ dataReceived: (
899
+ payload: Uint8Array,
900
+ participant?: RemoteParticipant,
901
+ kind?: DataPacket_Kind
902
+ ) => void,
903
+ connectionQualityChanged: (quality: ConnectionQuality, participant: Participant) => void,
904
+ mediaDevicesError: (error: Error) => void,
905
+ trackStreamStateChanged: (
906
+ publication: RemoteTrackPublication,
907
+ streamState: Track.StreamState,
908
+ participant: RemoteParticipant,
909
+ ) => void,
910
+ trackSubscriptionPermissionChanged: (
911
+ publication: RemoteTrackPublication,
912
+ status: TrackPublication.SubscriptionStatus,
913
+ participant: RemoteParticipant,
914
+ ) => void,
915
+ audioPlaybackChanged: (playing: boolean) => void,
916
+ };
@@ -7,6 +7,7 @@
7
7
  * room.on(RoomEvent.TrackPublished, (track, publication, participant) => {})
8
8
  * ```
9
9
  */
10
+
10
11
  export enum RoomEvent {
11
12
  /**
12
13
  * When the connection to the server has been interrupted and it's attempting
@@ -25,6 +26,13 @@ export enum RoomEvent {
25
26
  */
26
27
  Disconnected = 'disconnected',
27
28
 
29
+ /**
30
+ * Whenever the connection state of the room changes
31
+ *
32
+ * args: ([[RoomState]])
33
+ */
34
+ StateChanged = 'stateChanged',
35
+
28
36
  /**
29
37
  * When input or output devices on the machine have changed.
30
38
  */
@@ -146,7 +154,7 @@ export enum RoomEvent {
146
154
  * args: (prevMetadata: string, [[Participant]])
147
155
  *
148
156
  */
149
- ParticipantMetadataChanged = 'participantMetaDataChanged',
157
+ ParticipantMetadataChanged = 'participantMetadataChanged',
150
158
 
151
159
  /**
152
160
  * Room metadata is a simple way for app-specific state to be pushed to
@@ -366,6 +374,7 @@ export enum ParticipantEvent {
366
374
 
367
375
  /** @internal */
368
376
  export enum EngineEvent {
377
+ TransportsCreated = 'transportsCreated',
369
378
  Connected = 'connected',
370
379
  Disconnected = 'disconnected',
371
380
  Resuming = 'resuming',
@@ -1,8 +1,12 @@
1
1
  import { EventEmitter } from 'events';
2
- import { ConnectionQuality as ProtoQuality, ParticipantInfo } from '../../proto/livekit_models';
2
+ import type TypedEmitter from 'typed-emitter';
3
+ import { ConnectionQuality as ProtoQuality, DataPacket_Kind, ParticipantInfo } from '../../proto/livekit_models';
3
4
  import { ParticipantEvent, TrackEvent } from '../events';
5
+ import LocalTrackPublication from '../track/LocalTrackPublication';
6
+ import RemoteTrackPublication from '../track/RemoteTrackPublication';
4
7
  import { Track } from '../track/Track';
5
8
  import { TrackPublication } from '../track/TrackPublication';
9
+ import { RemoteTrack } from '../track/types';
6
10
 
7
11
  export enum ConnectionQuality {
8
12
  Excellent = 'excellent',
@@ -24,7 +28,9 @@ function qualityFromProto(q: ProtoQuality): ConnectionQuality {
24
28
  }
25
29
  }
26
30
 
27
- export default class Participant extends EventEmitter {
31
+ export default class Participant extends (
32
+ EventEmitter as new () => TypedEmitter<ParticipantEventCallbacks>
33
+ ) {
28
34
  protected participantInfo?: ParticipantInfo;
29
35
 
30
36
  audioTracks: Map<string, TrackPublication>;
@@ -158,8 +164,8 @@ export default class Participant extends EventEmitter {
158
164
  this.metadata = md;
159
165
 
160
166
  if (changed) {
161
- this.emit(ParticipantEvent.MetadataChanged, prevMetadata, this);
162
- this.emit(ParticipantEvent.ParticipantMetadataChanged, prevMetadata, this);
167
+ this.emit(ParticipantEvent.MetadataChanged, prevMetadata);
168
+ this.emit(ParticipantEvent.ParticipantMetadataChanged, prevMetadata);
163
169
  }
164
170
  }
165
171
 
@@ -212,3 +218,32 @@ export default class Participant extends EventEmitter {
212
218
  }
213
219
  }
214
220
  }
221
+
222
+ export type ParticipantEventCallbacks = {
223
+ trackPublished: (publication: RemoteTrackPublication) => void,
224
+ trackSubscribed: (track: RemoteTrack, publication: RemoteTrackPublication) => void,
225
+ trackSubscriptionFailed: (trackSid: string) => void,
226
+ trackUnpublished: (publication: RemoteTrackPublication) => void,
227
+ trackUnsubscribed: (track: RemoteTrack, publication: RemoteTrackPublication) => void,
228
+ trackMuted: (publication: TrackPublication) => void,
229
+ trackUnmuted: (publication: TrackPublication) => void,
230
+ localTrackPublished: (publication: LocalTrackPublication) => void,
231
+ localTrackUnpublished: (publication: LocalTrackPublication) => void,
232
+ /**
233
+ * @deprecated use [[participantMetadataChanged]] instead
234
+ */
235
+ metadataChanged: (prevMetadata: string | undefined, participant?: any) => void,
236
+ participantMetadataChanged: (prevMetadata: string | undefined, participant?: any) => void,
237
+ dataReceived: (payload: Uint8Array, kind: DataPacket_Kind) => void,
238
+ isSpeakingChanged: (speaking: boolean) => void,
239
+ connectionQualityChanged: (connectionQuality: ConnectionQuality) => void,
240
+ trackStreamStateChanged: (
241
+ publication: RemoteTrackPublication,
242
+ streamState: Track.StreamState
243
+ ) => void,
244
+ trackSubscriptionPermissionChanged: (
245
+ publication: RemoteTrackPublication,
246
+ status: TrackPublication.SubscriptionStatus
247
+ ) => void,
248
+ mediaDevicesError: (error: Error) => void,
249
+ };
@@ -10,9 +10,8 @@ import RemoteAudioTrack from '../track/RemoteAudioTrack';
10
10
  import RemoteTrackPublication from '../track/RemoteTrackPublication';
11
11
  import RemoteVideoTrack from '../track/RemoteVideoTrack';
12
12
  import { Track } from '../track/Track';
13
- import { TrackPublication } from '../track/TrackPublication';
14
13
  import { RemoteTrack } from '../track/types';
15
- import Participant from './Participant';
14
+ import Participant, { ParticipantEventCallbacks } from './Participant';
16
15
 
17
16
  export default class RemoteParticipant extends Participant {
18
17
  audioTracks: Map<string, RemoteTrackPublication>;
@@ -42,7 +41,7 @@ export default class RemoteParticipant extends Participant {
42
41
  this.videoTracks = new Map();
43
42
  }
44
43
 
45
- protected addTrackPublication(publication: TrackPublication) {
44
+ protected addTrackPublication(publication: RemoteTrackPublication) {
46
45
  super.addTrackPublication(publication);
47
46
 
48
47
  // register action events
@@ -234,7 +233,10 @@ export default class RemoteParticipant extends Participant {
234
233
  }
235
234
 
236
235
  /** @internal */
237
- emit(event: string | symbol, ...args: any[]): boolean {
236
+ emit<E extends keyof ParticipantEventCallbacks>(
237
+ event: E,
238
+ ...args: Parameters<ParticipantEventCallbacks[E]>
239
+ ): boolean {
238
240
  log.trace('participant event', this.sid, event, ...args);
239
241
  return super.emit(event, ...args);
240
242
  }
@@ -195,11 +195,9 @@ export default class RemoteVideoTrack extends RemoteTrack {
195
195
  let maxWidth = 0;
196
196
  let maxHeight = 0;
197
197
  for (const info of this.elementInfos) {
198
- const currentElementWidth = info.element.clientWidth * (window.devicePixelRatio ?? 1);
199
- const currentElementHeight = info.element.clientHeight * (window.devicePixelRatio ?? 1);
200
- if (currentElementWidth + currentElementHeight > maxWidth + maxHeight) {
201
- maxWidth = currentElementWidth;
202
- maxHeight = currentElementHeight;
198
+ if (info.element.clientWidth + info.element.clientHeight > maxWidth + maxHeight) {
199
+ maxWidth = info.element.clientWidth;
200
+ maxHeight = info.element.clientHeight;
203
201
  }
204
202
  }
205
203
 
@@ -1,4 +1,5 @@
1
1
  import { EventEmitter } from 'events';
2
+ import type TypedEventEmitter from 'typed-emitter';
2
3
  import { TrackSource, TrackType } from '../../proto/livekit_models';
3
4
  import { StreamState as ProtoStreamState } from '../../proto/livekit_rtc';
4
5
  import { TrackEvent } from '../events';
@@ -8,7 +9,7 @@ import { isFireFox, isSafari } from '../utils';
8
9
  // Safari tracks which audio elements have been "blessed" by the user.
9
10
  const recycledElements: Array<HTMLAudioElement> = [];
10
11
 
11
- export class Track extends EventEmitter {
12
+ export class Track extends (EventEmitter as new () => TypedEventEmitter<TrackEventCallbacks>) {
12
13
  kind: Track.Kind;
13
14
 
14
15
  mediaStreamTrack: MediaStreamTrack;
@@ -307,3 +308,16 @@ export namespace Track {
307
308
  }
308
309
  }
309
310
  }
311
+
312
+ export type TrackEventCallbacks = {
313
+ message: () => void,
314
+ muted: (track?: any) => void,
315
+ unmuted: (track?: any) => void,
316
+ ended: (track?: any) => void,
317
+ updateSettings: () => void,
318
+ updateSubscription: () => void,
319
+ audioPlaybackStarted: () => void,
320
+ audioPlaybackFailed: (error: Error) => void,
321
+ visibilityChanged: (visible: boolean, track?: any) => void,
322
+ videoDimensionsChanged: (dimensions: Track.Dimensions, track?: any) => void,
323
+ };
@@ -1,7 +1,7 @@
1
- import LocalAudioTrack from './LocalAudioTrack';
2
- import LocalVideoTrack from './LocalVideoTrack';
3
- import RemoteAudioTrack from './RemoteAudioTrack';
4
- import RemoteVideoTrack from './RemoteVideoTrack';
1
+ import type LocalAudioTrack from './LocalAudioTrack';
2
+ import type LocalVideoTrack from './LocalVideoTrack';
3
+ import type RemoteAudioTrack from './RemoteAudioTrack';
4
+ import type RemoteVideoTrack from './RemoteVideoTrack';
5
5
 
6
6
  export type RemoteTrack = RemoteAudioTrack | RemoteVideoTrack;
7
7
  export type AudioTrack = RemoteAudioTrack | LocalAudioTrack;
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.16.5';
1
+ export const version = '0.16.6';
2
2
  export const protocolVersion = 6;