Skip to content

Commit 1452210

Browse files
authored
Register engine events on localParticipant when updated (#480)
* Register engine events on localParticipant when updated * changeset * add comment * remove debug code * remove log * remove log * rename createEngine, make codepath easier to follow * set connection state as early as possible
1 parent 2b2640e commit 1452210

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

.changeset/few-dolls-reflect.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'livekit-client': patch
3+
---
4+
5+
Register engine events on localParticipant when updated

src/room/Room.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
140140
...options?.publishDefaults,
141141
};
142142

143-
this.createEngine();
143+
this.maybeCreateEngine();
144144

145145
this.localParticipant = new LocalParticipant('', '', this.engine, this.options);
146146
}
147147

148-
private createEngine() {
148+
private maybeCreateEngine() {
149149
if (this.engine) {
150150
return;
151151
}
@@ -190,7 +190,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
190190
.on(EngineEvent.Restarted, this.handleRestarted);
191191

192192
if (this.localParticipant) {
193-
this.localParticipant.engine = this.engine;
193+
this.localParticipant.setupEngine(this.engine);
194194
}
195195
}
196196

@@ -231,17 +231,22 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
231231
if (this.connectFuture) {
232232
return this.connectFuture.promise;
233233
}
234-
if (this.state === ConnectionState.Reconnecting) {
235-
log.info('Reconnection attempt replaced by new connection attempt');
236-
}
234+
235+
this.setAndEmitConnectionState(ConnectionState.Connecting);
236+
237237
const connectFn = async (resolve: () => void, reject: (reason: any) => void) => {
238-
this.setAndEmitConnectionState(ConnectionState.Connecting);
239238
if (!this.abortController || this.abortController.signal.aborted) {
240239
this.abortController = new AbortController();
241240
}
242241

243-
// recreate engine if previously disconnected
244-
this.recreateEngine();
242+
if (this.state === ConnectionState.Reconnecting) {
243+
log.info('Reconnection attempt replaced by new connection attempt');
244+
// make sure we close and recreate the existing engine in order to get rid of any potentially ongoing reconnection attempts
245+
this.recreateEngine();
246+
} else {
247+
// create engine if previously disconnected
248+
this.maybeCreateEngine();
249+
}
245250

246251
this.acquireAudioContext();
247252

@@ -599,8 +604,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
599604
// clear out existing remote participants, since they may have attached
600605
// the old engine
601606
this.participants.clear();
602-
603-
this.createEngine();
607+
this.maybeCreateEngine();
604608
}
605609

606610
private onTrackAdded(

src/room/participant/LocalParticipant.ts

+28-21
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,7 @@ export default class LocalParticipant extends Participant {
7676
this.tracks = new Map();
7777
this.engine = engine;
7878
this.roomOptions = options;
79-
80-
this.engine.client.onRemoteMuteChanged = (trackSid: string, muted: boolean) => {
81-
const pub = this.tracks.get(trackSid);
82-
if (!pub || !pub.track) {
83-
return;
84-
}
85-
if (muted) {
86-
pub.mute();
87-
} else {
88-
pub.unmute();
89-
}
90-
};
91-
92-
this.engine.client.onSubscribedQualityUpdate = this.handleSubscribedQualityUpdate;
93-
94-
this.engine.client.onLocalTrackUnpublished = this.handleLocalTrackUnpublished;
95-
96-
this.engine
97-
.on(EngineEvent.Connected, this.updateTrackSubscriptionPermissions)
98-
.on(EngineEvent.Restarted, this.updateTrackSubscriptionPermissions)
99-
.on(EngineEvent.Resumed, this.updateTrackSubscriptionPermissions);
79+
this.setupEngine(engine);
10080
}
10181

10282
get lastCameraError(): Error | undefined {
@@ -121,6 +101,33 @@ export default class LocalParticipant extends Participant {
121101
}
122102
}
123103

104+
/**
105+
* @internal
106+
*/
107+
setupEngine(engine: RTCEngine) {
108+
this.engine = engine;
109+
this.engine.client.onRemoteMuteChanged = (trackSid: string, muted: boolean) => {
110+
const pub = this.tracks.get(trackSid);
111+
if (!pub || !pub.track) {
112+
return;
113+
}
114+
if (muted) {
115+
pub.mute();
116+
} else {
117+
pub.unmute();
118+
}
119+
};
120+
121+
this.engine.client.onSubscribedQualityUpdate = this.handleSubscribedQualityUpdate;
122+
123+
this.engine.client.onLocalTrackUnpublished = this.handleLocalTrackUnpublished;
124+
125+
this.engine
126+
.on(EngineEvent.Connected, this.updateTrackSubscriptionPermissions)
127+
.on(EngineEvent.Restarted, this.updateTrackSubscriptionPermissions)
128+
.on(EngineEvent.Resumed, this.updateTrackSubscriptionPermissions);
129+
}
130+
124131
/**
125132
* Enable or disable a participant's camera track.
126133
*

0 commit comments

Comments
 (0)