livekit-client 0.15.3 → 0.15.4
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/dist/api/SignalClient.d.ts +3 -1
- package/dist/api/SignalClient.js +59 -25
- package/dist/api/SignalClient.js.map +1 -1
- package/dist/options.d.ts +5 -0
- package/dist/proto/livekit_models.d.ts +30 -0
- package/dist/proto/livekit_models.js +219 -1
- package/dist/proto/livekit_models.js.map +1 -1
- package/dist/room/RTCEngine.d.ts +2 -0
- package/dist/room/RTCEngine.js +45 -2
- package/dist/room/RTCEngine.js.map +1 -1
- package/dist/room/Room.js +4 -0
- package/dist/room/Room.js.map +1 -1
- package/dist/room/participant/LocalParticipant.js +2 -1
- package/dist/room/participant/LocalParticipant.js.map +1 -1
- package/dist/room/participant/publishUtils.js +1 -1
- package/dist/room/participant/publishUtils.js.map +1 -1
- package/dist/room/participant/publishUtils.test.js +9 -0
- package/dist/room/participant/publishUtils.test.js.map +1 -1
- package/dist/room/track/RemoteTrackPublication.d.ts +1 -0
- package/dist/room/track/RemoteTrackPublication.js +15 -7
- package/dist/room/track/RemoteTrackPublication.js.map +1 -1
- package/dist/room/track/create.js +5 -0
- package/dist/room/track/create.js.map +1 -1
- package/dist/room/utils.d.ts +2 -0
- package/dist/room/utils.js +32 -1
- package/dist/room/utils.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +4 -2
- package/src/api/SignalClient.ts +434 -0
- package/src/connect.ts +100 -0
- package/src/index.ts +47 -0
- package/src/logger.ts +22 -0
- package/src/options.ts +152 -0
- package/src/proto/livekit_models.ts +1863 -0
- package/src/proto/livekit_rtc.ts +3401 -0
- package/src/room/DeviceManager.ts +57 -0
- package/src/room/PCTransport.ts +86 -0
- package/src/room/RTCEngine.ts +484 -0
- package/src/room/Room.ts +785 -0
- package/src/room/errors.ts +65 -0
- package/src/room/events.ts +396 -0
- package/src/room/participant/LocalParticipant.ts +685 -0
- package/src/room/participant/Participant.ts +214 -0
- package/src/room/participant/ParticipantTrackPermission.ts +32 -0
- package/src/room/participant/RemoteParticipant.ts +238 -0
- package/src/room/participant/publishUtils.test.ts +105 -0
- package/src/room/participant/publishUtils.ts +180 -0
- package/src/room/stats.ts +130 -0
- package/src/room/track/LocalAudioTrack.ts +112 -0
- package/src/room/track/LocalTrack.ts +124 -0
- package/src/room/track/LocalTrackPublication.ts +63 -0
- package/src/room/track/LocalVideoTrack.test.ts +70 -0
- package/src/room/track/LocalVideoTrack.ts +416 -0
- package/src/room/track/RemoteAudioTrack.ts +58 -0
- package/src/room/track/RemoteTrack.ts +59 -0
- package/src/room/track/RemoteTrackPublication.ts +192 -0
- package/src/room/track/RemoteVideoTrack.ts +213 -0
- package/src/room/track/Track.ts +301 -0
- package/src/room/track/TrackPublication.ts +120 -0
- package/src/room/track/create.ts +120 -0
- package/src/room/track/defaults.ts +23 -0
- package/src/room/track/options.ts +229 -0
- package/src/room/track/types.ts +8 -0
- package/src/room/track/utils.test.ts +93 -0
- package/src/room/track/utils.ts +76 -0
- package/src/room/utils.ts +74 -0
- package/src/version.ts +2 -0
- package/.github/workflows/publish.yaml +0 -55
- package/.github/workflows/test.yaml +0 -36
- package/example/index.html +0 -247
- package/example/sample.ts +0 -632
- package/example/styles.css +0 -144
- package/example/webpack.config.js +0 -33
package/src/room/Room.ts
ADDED
@@ -0,0 +1,785 @@
|
|
1
|
+
import { EventEmitter } from 'events';
|
2
|
+
import { toProtoSessionDescription } from '../api/SignalClient';
|
3
|
+
import log from '../logger';
|
4
|
+
import { RoomConnectOptions, RoomOptions } from '../options';
|
5
|
+
import {
|
6
|
+
DataPacket_Kind, ParticipantInfo,
|
7
|
+
ParticipantInfo_State, Room as RoomModel, SpeakerInfo, UserPacket,
|
8
|
+
} from '../proto/livekit_models';
|
9
|
+
import {
|
10
|
+
ConnectionQualityUpdate, SimulateScenario, StreamStateUpdate, SubscriptionPermissionUpdate,
|
11
|
+
} from '../proto/livekit_rtc';
|
12
|
+
import DeviceManager from './DeviceManager';
|
13
|
+
import { ConnectionError, UnsupportedServer } from './errors';
|
14
|
+
import {
|
15
|
+
EngineEvent, ParticipantEvent, RoomEvent, TrackEvent,
|
16
|
+
} from './events';
|
17
|
+
import LocalParticipant from './participant/LocalParticipant';
|
18
|
+
import Participant, { ConnectionQuality } from './participant/Participant';
|
19
|
+
import RemoteParticipant from './participant/RemoteParticipant';
|
20
|
+
import RTCEngine, { maxICEConnectTimeout } from './RTCEngine';
|
21
|
+
import { audioDefaults, publishDefaults, videoDefaults } from './track/defaults';
|
22
|
+
import LocalTrackPublication from './track/LocalTrackPublication';
|
23
|
+
import RemoteTrackPublication from './track/RemoteTrackPublication';
|
24
|
+
import { Track } from './track/Track';
|
25
|
+
import { TrackPublication } from './track/TrackPublication';
|
26
|
+
import { RemoteTrack } from './track/types';
|
27
|
+
import { unpackStreamId } from './utils';
|
28
|
+
|
29
|
+
export enum RoomState {
|
30
|
+
Disconnected = 'disconnected',
|
31
|
+
Connected = 'connected',
|
32
|
+
Reconnecting = 'reconnecting',
|
33
|
+
}
|
34
|
+
|
35
|
+
/**
|
36
|
+
* In LiveKit, a room is the logical grouping for a list of participants.
|
37
|
+
* Participants in a room can publish tracks, and subscribe to others' tracks.
|
38
|
+
*
|
39
|
+
* a Room fires [[RoomEvent | RoomEvents]].
|
40
|
+
*
|
41
|
+
* @noInheritDoc
|
42
|
+
*/
|
43
|
+
class Room extends EventEmitter {
|
44
|
+
state: RoomState = RoomState.Disconnected;
|
45
|
+
|
46
|
+
/** map of sid: [[RemoteParticipant]] */
|
47
|
+
participants: Map<string, RemoteParticipant>;
|
48
|
+
|
49
|
+
/**
|
50
|
+
* list of participants that are actively speaking. when this changes
|
51
|
+
* a [[RoomEvent.ActiveSpeakersChanged]] event is fired
|
52
|
+
*/
|
53
|
+
activeSpeakers: Participant[] = [];
|
54
|
+
|
55
|
+
/** @internal */
|
56
|
+
engine!: RTCEngine;
|
57
|
+
|
58
|
+
// available after connected
|
59
|
+
/** server assigned unique room id */
|
60
|
+
sid: string = '';
|
61
|
+
|
62
|
+
/** user assigned name, derived from JWT token */
|
63
|
+
name: string = '';
|
64
|
+
|
65
|
+
/** the current participant */
|
66
|
+
localParticipant: LocalParticipant;
|
67
|
+
|
68
|
+
/** room metadata */
|
69
|
+
metadata: string | undefined = undefined;
|
70
|
+
|
71
|
+
/** options of room */
|
72
|
+
options: RoomOptions;
|
73
|
+
|
74
|
+
/** connect options of room */
|
75
|
+
private connOptions?: RoomConnectOptions;
|
76
|
+
|
77
|
+
private audioEnabled = true;
|
78
|
+
|
79
|
+
private audioContext?: AudioContext;
|
80
|
+
|
81
|
+
/**
|
82
|
+
* Creates a new Room, the primary construct for a LiveKit session.
|
83
|
+
* @param options
|
84
|
+
*/
|
85
|
+
constructor(options?: RoomOptions) {
|
86
|
+
super();
|
87
|
+
this.participants = new Map();
|
88
|
+
this.options = options || {};
|
89
|
+
|
90
|
+
this.options.audioCaptureDefaults = {
|
91
|
+
...audioDefaults,
|
92
|
+
...options?.audioCaptureDefaults,
|
93
|
+
};
|
94
|
+
this.options.videoCaptureDefaults = {
|
95
|
+
...videoDefaults,
|
96
|
+
...options?.videoCaptureDefaults,
|
97
|
+
};
|
98
|
+
this.options.publishDefaults = {
|
99
|
+
...publishDefaults,
|
100
|
+
...options?.publishDefaults,
|
101
|
+
};
|
102
|
+
|
103
|
+
this.createEngine();
|
104
|
+
|
105
|
+
this.localParticipant = new LocalParticipant(
|
106
|
+
'', '', this.engine, this.options,
|
107
|
+
);
|
108
|
+
}
|
109
|
+
|
110
|
+
private createEngine() {
|
111
|
+
if (this.engine) {
|
112
|
+
return;
|
113
|
+
}
|
114
|
+
|
115
|
+
this.engine = new RTCEngine();
|
116
|
+
this.engine.client.signalLatency = this.options.expSignalLatency;
|
117
|
+
|
118
|
+
this.engine.on(
|
119
|
+
EngineEvent.MediaTrackAdded,
|
120
|
+
(
|
121
|
+
mediaTrack: MediaStreamTrack,
|
122
|
+
stream: MediaStream,
|
123
|
+
receiver?: RTCRtpReceiver,
|
124
|
+
) => {
|
125
|
+
this.onTrackAdded(mediaTrack, stream, receiver);
|
126
|
+
},
|
127
|
+
);
|
128
|
+
|
129
|
+
this.engine.on(EngineEvent.Disconnected, () => {
|
130
|
+
this.handleDisconnect();
|
131
|
+
});
|
132
|
+
|
133
|
+
this.engine.client.onParticipantUpdate = this.handleParticipantUpdates;
|
134
|
+
this.engine.client.onRoomUpdate = this.handleRoomUpdate;
|
135
|
+
this.engine.client.onSpeakersChanged = this.handleSpeakersChanged;
|
136
|
+
this.engine.client.onStreamStateUpdate = this.handleStreamStateUpdate;
|
137
|
+
this.engine.client.onSubscriptionPermissionUpdate = this.handleSubscriptionPermissionUpdate;
|
138
|
+
this.engine.on(EngineEvent.ActiveSpeakersUpdate, this.handleActiveSpeakersUpdate);
|
139
|
+
this.engine.on(EngineEvent.DataPacketReceived, this.handleDataPacket);
|
140
|
+
|
141
|
+
this.engine.on(EngineEvent.Reconnecting, () => {
|
142
|
+
this.state = RoomState.Reconnecting;
|
143
|
+
this.emit(RoomEvent.Reconnecting);
|
144
|
+
});
|
145
|
+
|
146
|
+
this.engine.on(EngineEvent.Reconnected, () => {
|
147
|
+
this.state = RoomState.Connected;
|
148
|
+
this.emit(RoomEvent.Reconnected);
|
149
|
+
});
|
150
|
+
|
151
|
+
this.engine.on(EngineEvent.SignalConnected, () => {
|
152
|
+
if (this.state === RoomState.Reconnecting) {
|
153
|
+
this.sendSyncState();
|
154
|
+
}
|
155
|
+
});
|
156
|
+
|
157
|
+
this.engine.client.onConnectionQuality = this.handleConnectionQualityUpdate;
|
158
|
+
}
|
159
|
+
|
160
|
+
/**
|
161
|
+
* getLocalDevices abstracts navigator.mediaDevices.enumerateDevices.
|
162
|
+
* In particular, it handles Chrome's unique behavior of creating `default`
|
163
|
+
* devices. When encountered, it'll be removed from the list of devices.
|
164
|
+
* The actual default device will be placed at top.
|
165
|
+
* @param kind
|
166
|
+
* @returns a list of available local devices
|
167
|
+
*/
|
168
|
+
static getLocalDevices(kind: MediaDeviceKind): Promise<MediaDeviceInfo[]> {
|
169
|
+
return DeviceManager.getInstance().getDevices(kind);
|
170
|
+
}
|
171
|
+
|
172
|
+
connect = async (url: string, token: string, opts?: RoomConnectOptions) => {
|
173
|
+
// guard against calling connect
|
174
|
+
if (this.state !== RoomState.Disconnected) {
|
175
|
+
log.warn('already connected to room', this.name);
|
176
|
+
return;
|
177
|
+
}
|
178
|
+
|
179
|
+
// recreate engine if previously disconnected
|
180
|
+
this.createEngine();
|
181
|
+
|
182
|
+
this.acquireAudioContext();
|
183
|
+
|
184
|
+
if (opts?.rtcConfig) {
|
185
|
+
this.engine.rtcConfig = opts.rtcConfig;
|
186
|
+
}
|
187
|
+
|
188
|
+
this.connOptions = opts;
|
189
|
+
|
190
|
+
try {
|
191
|
+
const joinResponse = await this.engine.join(url, token, opts);
|
192
|
+
log.debug('connected to Livekit Server', joinResponse.serverVersion);
|
193
|
+
|
194
|
+
if (!joinResponse.serverVersion) {
|
195
|
+
throw new UnsupportedServer('unknown server version');
|
196
|
+
}
|
197
|
+
|
198
|
+
if (joinResponse.serverVersion === '0.15.1' && this.options.dynacast) {
|
199
|
+
log.debug('disabling dynacast due to server version');
|
200
|
+
// dynacast has a bug in 0.15.1, so we cannot use it then
|
201
|
+
this.options.dynacast = false;
|
202
|
+
}
|
203
|
+
|
204
|
+
this.state = RoomState.Connected;
|
205
|
+
const pi = joinResponse.participant!;
|
206
|
+
this.localParticipant = new LocalParticipant(
|
207
|
+
pi.sid,
|
208
|
+
pi.identity,
|
209
|
+
this.engine,
|
210
|
+
this.options,
|
211
|
+
);
|
212
|
+
|
213
|
+
this.localParticipant.updateInfo(pi);
|
214
|
+
// forward metadata changed for the local participant
|
215
|
+
this.localParticipant
|
216
|
+
.on(ParticipantEvent.MetadataChanged, (metadata: object) => {
|
217
|
+
this.emit(RoomEvent.MetadataChanged, metadata, this.localParticipant);
|
218
|
+
})
|
219
|
+
.on(ParticipantEvent.ParticipantMetadataChanged, (metadata: object) => {
|
220
|
+
this.emit(RoomEvent.ParticipantMetadataChanged, metadata, this.localParticipant);
|
221
|
+
})
|
222
|
+
.on(ParticipantEvent.TrackMuted, (pub: TrackPublication) => {
|
223
|
+
this.emit(RoomEvent.TrackMuted, pub, this.localParticipant);
|
224
|
+
})
|
225
|
+
.on(ParticipantEvent.TrackUnmuted, (pub: TrackPublication) => {
|
226
|
+
this.emit(RoomEvent.TrackUnmuted, pub, this.localParticipant);
|
227
|
+
})
|
228
|
+
.on(ParticipantEvent.LocalTrackPublished, (pub: LocalTrackPublication) => {
|
229
|
+
this.emit(RoomEvent.LocalTrackPublished, pub, this.localParticipant);
|
230
|
+
})
|
231
|
+
.on(ParticipantEvent.LocalTrackUnpublished, (pub: LocalTrackPublication) => {
|
232
|
+
this.emit(RoomEvent.LocalTrackUnpublished, pub, this.localParticipant);
|
233
|
+
})
|
234
|
+
.on(ParticipantEvent.ConnectionQualityChanged, (quality: ConnectionQuality) => {
|
235
|
+
this.emit(RoomEvent.ConnectionQualityChanged, quality, this.localParticipant);
|
236
|
+
})
|
237
|
+
.on(ParticipantEvent.MediaDevicesError, (e: Error) => {
|
238
|
+
this.emit(RoomEvent.MediaDevicesError, e);
|
239
|
+
});
|
240
|
+
|
241
|
+
// populate remote participants, these should not trigger new events
|
242
|
+
joinResponse.otherParticipants.forEach((info) => {
|
243
|
+
this.getOrCreateParticipant(info.sid, info);
|
244
|
+
});
|
245
|
+
|
246
|
+
this.name = joinResponse.room!.name;
|
247
|
+
this.sid = joinResponse.room!.sid;
|
248
|
+
this.metadata = joinResponse.room!.metadata;
|
249
|
+
} catch (err) {
|
250
|
+
this.engine.close();
|
251
|
+
throw err;
|
252
|
+
}
|
253
|
+
|
254
|
+
// don't return until ICE connected
|
255
|
+
return new Promise<Room>((resolve, reject) => {
|
256
|
+
const connectTimeout = setTimeout(() => {
|
257
|
+
// timeout
|
258
|
+
this.engine.close();
|
259
|
+
reject(new ConnectionError('could not connect after timeout'));
|
260
|
+
}, maxICEConnectTimeout);
|
261
|
+
|
262
|
+
this.engine.once(EngineEvent.Connected, () => {
|
263
|
+
clearTimeout(connectTimeout);
|
264
|
+
|
265
|
+
// also hook unload event
|
266
|
+
window.addEventListener('beforeunload', this.onBeforeUnload);
|
267
|
+
navigator.mediaDevices.addEventListener('devicechange', this.handleDeviceChange);
|
268
|
+
|
269
|
+
resolve(this);
|
270
|
+
});
|
271
|
+
});
|
272
|
+
};
|
273
|
+
|
274
|
+
/**
|
275
|
+
* disconnects the room, emits [[RoomEvent.Disconnected]]
|
276
|
+
*/
|
277
|
+
disconnect = (stopTracks = true) => {
|
278
|
+
// send leave
|
279
|
+
if (this.engine) {
|
280
|
+
this.engine.client.sendLeave();
|
281
|
+
this.engine.close();
|
282
|
+
}
|
283
|
+
this.handleDisconnect(stopTracks);
|
284
|
+
/* @ts-ignore */
|
285
|
+
this.engine = undefined;
|
286
|
+
};
|
287
|
+
|
288
|
+
/**
|
289
|
+
* retrieves a participant by identity
|
290
|
+
* @param identity
|
291
|
+
* @returns
|
292
|
+
*/
|
293
|
+
getParticipantByIdentity(identity: string): Participant | undefined {
|
294
|
+
for (const [, p] of this.participants) {
|
295
|
+
if (p.identity === identity) {
|
296
|
+
return p;
|
297
|
+
}
|
298
|
+
}
|
299
|
+
if (this.localParticipant.identity === identity) {
|
300
|
+
return this.localParticipant;
|
301
|
+
}
|
302
|
+
}
|
303
|
+
|
304
|
+
/**
|
305
|
+
* @internal for testing
|
306
|
+
*/
|
307
|
+
simulateScenario(scenario: string) {
|
308
|
+
let req: SimulateScenario | undefined;
|
309
|
+
switch (scenario) {
|
310
|
+
case 'speaker':
|
311
|
+
req = SimulateScenario.fromPartial({
|
312
|
+
speakerUpdate: 3,
|
313
|
+
});
|
314
|
+
break;
|
315
|
+
case 'node-failure':
|
316
|
+
req = SimulateScenario.fromPartial({
|
317
|
+
nodeFailure: true,
|
318
|
+
});
|
319
|
+
break;
|
320
|
+
case 'server-leave':
|
321
|
+
req = SimulateScenario.fromPartial({
|
322
|
+
serverLeave: true,
|
323
|
+
});
|
324
|
+
break;
|
325
|
+
case 'migration':
|
326
|
+
req = SimulateScenario.fromPartial({
|
327
|
+
migration: true,
|
328
|
+
});
|
329
|
+
break;
|
330
|
+
default:
|
331
|
+
}
|
332
|
+
if (req) {
|
333
|
+
this.engine.client.sendSimulateScenario(req);
|
334
|
+
}
|
335
|
+
}
|
336
|
+
|
337
|
+
private onBeforeUnload = () => {
|
338
|
+
this.disconnect();
|
339
|
+
};
|
340
|
+
|
341
|
+
/**
|
342
|
+
* Browsers have different policies regarding audio playback. Most requiring
|
343
|
+
* some form of user interaction (click/tap/etc).
|
344
|
+
* In those cases, audio will be silent until a click/tap triggering one of the following
|
345
|
+
* - `startAudio`
|
346
|
+
* - `getUserMedia`
|
347
|
+
*/
|
348
|
+
async startAudio() {
|
349
|
+
this.acquireAudioContext();
|
350
|
+
|
351
|
+
const elements: Array<HTMLMediaElement> = [];
|
352
|
+
this.participants.forEach((p) => {
|
353
|
+
p.audioTracks.forEach((t) => {
|
354
|
+
if (t.track) {
|
355
|
+
t.track.attachedElements.forEach((e) => {
|
356
|
+
elements.push(e);
|
357
|
+
});
|
358
|
+
}
|
359
|
+
});
|
360
|
+
});
|
361
|
+
|
362
|
+
try {
|
363
|
+
await Promise.all(elements.map((e) => e.play()));
|
364
|
+
this.handleAudioPlaybackStarted();
|
365
|
+
} catch (err) {
|
366
|
+
this.handleAudioPlaybackFailed(err);
|
367
|
+
throw err;
|
368
|
+
}
|
369
|
+
}
|
370
|
+
|
371
|
+
/**
|
372
|
+
* Returns true if audio playback is enabled
|
373
|
+
*/
|
374
|
+
get canPlaybackAudio(): boolean {
|
375
|
+
return this.audioEnabled;
|
376
|
+
}
|
377
|
+
|
378
|
+
/**
|
379
|
+
* Switches all active device used in this room to the given device.
|
380
|
+
*
|
381
|
+
* Note: setting AudioOutput is not supported on some browsers. See [setSinkId](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility)
|
382
|
+
*
|
383
|
+
* @param kind use `videoinput` for camera track,
|
384
|
+
* `audioinput` for microphone track,
|
385
|
+
* `audiooutput` to set speaker for all incoming audio tracks
|
386
|
+
* @param deviceId
|
387
|
+
*/
|
388
|
+
async switchActiveDevice(kind: MediaDeviceKind, deviceId: string) {
|
389
|
+
if (kind === 'audioinput') {
|
390
|
+
const tracks = Array
|
391
|
+
.from(this.localParticipant.audioTracks.values())
|
392
|
+
.filter((track) => track.source === Track.Source.Microphone);
|
393
|
+
await Promise.all(tracks.map((t) => t.audioTrack?.setDeviceId(deviceId)));
|
394
|
+
this.options.audioCaptureDefaults!.deviceId = deviceId;
|
395
|
+
} else if (kind === 'videoinput') {
|
396
|
+
const tracks = Array
|
397
|
+
.from(this.localParticipant.videoTracks.values())
|
398
|
+
.filter((track) => track.source === Track.Source.Camera);
|
399
|
+
await Promise.all(tracks.map((t) => t.videoTrack?.setDeviceId(deviceId)));
|
400
|
+
this.options.videoCaptureDefaults!.deviceId = deviceId;
|
401
|
+
} else if (kind === 'audiooutput') {
|
402
|
+
const elements: HTMLMediaElement[] = [];
|
403
|
+
this.participants.forEach((p) => {
|
404
|
+
p.audioTracks.forEach((t) => {
|
405
|
+
if (t.isSubscribed && t.track) {
|
406
|
+
t.track.attachedElements.forEach((e) => {
|
407
|
+
elements.push(e);
|
408
|
+
});
|
409
|
+
}
|
410
|
+
});
|
411
|
+
});
|
412
|
+
|
413
|
+
await Promise.all(elements.map(async (e) => {
|
414
|
+
if ('setSinkId' in e) {
|
415
|
+
/* @ts-ignore */
|
416
|
+
await e.setSinkId(deviceId);
|
417
|
+
}
|
418
|
+
}));
|
419
|
+
}
|
420
|
+
}
|
421
|
+
|
422
|
+
private onTrackAdded(
|
423
|
+
mediaTrack: MediaStreamTrack,
|
424
|
+
stream: MediaStream,
|
425
|
+
receiver?: RTCRtpReceiver,
|
426
|
+
) {
|
427
|
+
const parts = unpackStreamId(stream.id);
|
428
|
+
const participantId = parts[0];
|
429
|
+
let trackId = parts[1];
|
430
|
+
if (!trackId || trackId === '') trackId = mediaTrack.id;
|
431
|
+
|
432
|
+
const participant = this.getOrCreateParticipant(participantId);
|
433
|
+
participant.addSubscribedMediaTrack(
|
434
|
+
mediaTrack,
|
435
|
+
trackId,
|
436
|
+
stream,
|
437
|
+
receiver,
|
438
|
+
this.options.adaptiveStream,
|
439
|
+
);
|
440
|
+
}
|
441
|
+
|
442
|
+
private handleDisconnect(shouldStopTracks = true) {
|
443
|
+
if (this.state === RoomState.Disconnected) {
|
444
|
+
return;
|
445
|
+
}
|
446
|
+
this.participants.forEach((p) => {
|
447
|
+
p.tracks.forEach((pub) => {
|
448
|
+
p.unpublishTrack(pub.trackSid);
|
449
|
+
});
|
450
|
+
});
|
451
|
+
|
452
|
+
this.localParticipant.tracks.forEach((pub) => {
|
453
|
+
if (pub.track) {
|
454
|
+
this.localParticipant.unpublishTrack(pub.track);
|
455
|
+
}
|
456
|
+
if (shouldStopTracks) {
|
457
|
+
pub.track?.detach();
|
458
|
+
pub.track?.stop();
|
459
|
+
}
|
460
|
+
});
|
461
|
+
|
462
|
+
this.participants.clear();
|
463
|
+
this.activeSpeakers = [];
|
464
|
+
if (this.audioContext) {
|
465
|
+
this.audioContext.close();
|
466
|
+
this.audioContext = undefined;
|
467
|
+
}
|
468
|
+
window.removeEventListener('beforeunload', this.onBeforeUnload);
|
469
|
+
navigator.mediaDevices.removeEventListener('devicechange', this.handleDeviceChange);
|
470
|
+
this.state = RoomState.Disconnected;
|
471
|
+
this.emit(RoomEvent.Disconnected);
|
472
|
+
}
|
473
|
+
|
474
|
+
private handleParticipantUpdates = (participantInfos: ParticipantInfo[]) => {
|
475
|
+
// handle changes to participant state, and send events
|
476
|
+
participantInfos.forEach((info) => {
|
477
|
+
if (info.sid === this.localParticipant.sid) {
|
478
|
+
this.localParticipant.updateInfo(info);
|
479
|
+
return;
|
480
|
+
}
|
481
|
+
|
482
|
+
let remoteParticipant = this.participants.get(info.sid);
|
483
|
+
const isNewParticipant = !remoteParticipant;
|
484
|
+
|
485
|
+
// create participant if doesn't exist
|
486
|
+
remoteParticipant = this.getOrCreateParticipant(info.sid, info);
|
487
|
+
|
488
|
+
// when it's disconnected, send updates
|
489
|
+
if (info.state === ParticipantInfo_State.DISCONNECTED) {
|
490
|
+
this.handleParticipantDisconnected(info.sid, remoteParticipant);
|
491
|
+
} else if (isNewParticipant) {
|
492
|
+
// fire connected event
|
493
|
+
this.emit(RoomEvent.ParticipantConnected, remoteParticipant);
|
494
|
+
} else {
|
495
|
+
// just update, no events
|
496
|
+
remoteParticipant.updateInfo(info);
|
497
|
+
}
|
498
|
+
});
|
499
|
+
};
|
500
|
+
|
501
|
+
private handleParticipantDisconnected(
|
502
|
+
sid: string,
|
503
|
+
participant?: RemoteParticipant,
|
504
|
+
) {
|
505
|
+
// remove and send event
|
506
|
+
this.participants.delete(sid);
|
507
|
+
if (!participant) {
|
508
|
+
return;
|
509
|
+
}
|
510
|
+
|
511
|
+
participant.tracks.forEach((publication) => {
|
512
|
+
participant.unpublishTrack(publication.trackSid);
|
513
|
+
});
|
514
|
+
this.emit(RoomEvent.ParticipantDisconnected, participant);
|
515
|
+
}
|
516
|
+
|
517
|
+
// updates are sent only when there's a change to speaker ordering
|
518
|
+
private handleActiveSpeakersUpdate = (speakers: SpeakerInfo[]) => {
|
519
|
+
const activeSpeakers: Participant[] = [];
|
520
|
+
const seenSids: any = {};
|
521
|
+
speakers.forEach((speaker) => {
|
522
|
+
seenSids[speaker.sid] = true;
|
523
|
+
if (speaker.sid === this.localParticipant.sid) {
|
524
|
+
this.localParticipant.audioLevel = speaker.level;
|
525
|
+
this.localParticipant.setIsSpeaking(true);
|
526
|
+
activeSpeakers.push(this.localParticipant);
|
527
|
+
} else {
|
528
|
+
const p = this.participants.get(speaker.sid);
|
529
|
+
if (p) {
|
530
|
+
p.audioLevel = speaker.level;
|
531
|
+
p.setIsSpeaking(true);
|
532
|
+
activeSpeakers.push(p);
|
533
|
+
}
|
534
|
+
}
|
535
|
+
});
|
536
|
+
|
537
|
+
if (!seenSids[this.localParticipant.sid]) {
|
538
|
+
this.localParticipant.audioLevel = 0;
|
539
|
+
this.localParticipant.setIsSpeaking(false);
|
540
|
+
}
|
541
|
+
this.participants.forEach((p) => {
|
542
|
+
if (!seenSids[p.sid]) {
|
543
|
+
p.audioLevel = 0;
|
544
|
+
p.setIsSpeaking(false);
|
545
|
+
}
|
546
|
+
});
|
547
|
+
|
548
|
+
this.activeSpeakers = activeSpeakers;
|
549
|
+
this.emit(RoomEvent.ActiveSpeakersChanged, activeSpeakers);
|
550
|
+
};
|
551
|
+
|
552
|
+
// process list of changed speakers
|
553
|
+
private handleSpeakersChanged = (speakerUpdates: SpeakerInfo[]) => {
|
554
|
+
const lastSpeakers = new Map<string, Participant>();
|
555
|
+
this.activeSpeakers.forEach((p) => {
|
556
|
+
lastSpeakers.set(p.sid, p);
|
557
|
+
});
|
558
|
+
speakerUpdates.forEach((speaker) => {
|
559
|
+
let p: Participant | undefined = this.participants.get(speaker.sid);
|
560
|
+
if (speaker.sid === this.localParticipant.sid) {
|
561
|
+
p = this.localParticipant;
|
562
|
+
}
|
563
|
+
if (!p) {
|
564
|
+
return;
|
565
|
+
}
|
566
|
+
p.audioLevel = speaker.level;
|
567
|
+
p.setIsSpeaking(speaker.active);
|
568
|
+
|
569
|
+
if (speaker.active) {
|
570
|
+
lastSpeakers.set(speaker.sid, p);
|
571
|
+
} else {
|
572
|
+
lastSpeakers.delete(speaker.sid);
|
573
|
+
}
|
574
|
+
});
|
575
|
+
const activeSpeakers = Array.from(lastSpeakers.values());
|
576
|
+
activeSpeakers.sort((a, b) => b.audioLevel - a.audioLevel);
|
577
|
+
this.activeSpeakers = activeSpeakers;
|
578
|
+
this.emit(RoomEvent.ActiveSpeakersChanged, activeSpeakers);
|
579
|
+
};
|
580
|
+
|
581
|
+
private handleStreamStateUpdate = (streamStateUpdate: StreamStateUpdate) => {
|
582
|
+
streamStateUpdate.streamStates.forEach((streamState) => {
|
583
|
+
const participant = this.participants.get(streamState.participantSid);
|
584
|
+
if (!participant) {
|
585
|
+
return;
|
586
|
+
}
|
587
|
+
const pub = participant.getTrackPublication(streamState.trackSid);
|
588
|
+
if (!pub || !pub.track) {
|
589
|
+
return;
|
590
|
+
}
|
591
|
+
pub.track.streamState = Track.streamStateFromProto(streamState.state);
|
592
|
+
participant.emit(ParticipantEvent.TrackStreamStateChanged, pub, pub.track.streamState);
|
593
|
+
this.emit(ParticipantEvent.TrackStreamStateChanged, pub, pub.track.streamState, participant);
|
594
|
+
});
|
595
|
+
};
|
596
|
+
|
597
|
+
private handleSubscriptionPermissionUpdate = (update: SubscriptionPermissionUpdate) => {
|
598
|
+
const participant = this.participants.get(update.participantSid);
|
599
|
+
if (!participant) {
|
600
|
+
return;
|
601
|
+
}
|
602
|
+
const pub = participant.getTrackPublication(update.trackSid);
|
603
|
+
if (!pub) {
|
604
|
+
return;
|
605
|
+
}
|
606
|
+
|
607
|
+
pub._allowed = update.allowed;
|
608
|
+
participant.emit(ParticipantEvent.TrackSubscriptionPermissionChanged, pub,
|
609
|
+
pub.subscriptionStatus);
|
610
|
+
this.emit(ParticipantEvent.TrackSubscriptionPermissionChanged, pub,
|
611
|
+
pub.subscriptionStatus, participant);
|
612
|
+
};
|
613
|
+
|
614
|
+
private handleDataPacket = (
|
615
|
+
userPacket: UserPacket,
|
616
|
+
kind: DataPacket_Kind,
|
617
|
+
) => {
|
618
|
+
// find the participant
|
619
|
+
const participant = this.participants.get(userPacket.participantSid);
|
620
|
+
|
621
|
+
this.emit(RoomEvent.DataReceived, userPacket.payload, participant, kind);
|
622
|
+
|
623
|
+
// also emit on the participant
|
624
|
+
participant?.emit(ParticipantEvent.DataReceived, userPacket.payload, kind);
|
625
|
+
};
|
626
|
+
|
627
|
+
private handleAudioPlaybackStarted = () => {
|
628
|
+
if (this.canPlaybackAudio) {
|
629
|
+
return;
|
630
|
+
}
|
631
|
+
this.audioEnabled = true;
|
632
|
+
this.emit(RoomEvent.AudioPlaybackStatusChanged, true);
|
633
|
+
};
|
634
|
+
|
635
|
+
private handleAudioPlaybackFailed = (e: any) => {
|
636
|
+
log.warn('could not playback audio', e);
|
637
|
+
if (!this.canPlaybackAudio) {
|
638
|
+
return;
|
639
|
+
}
|
640
|
+
this.audioEnabled = false;
|
641
|
+
this.emit(RoomEvent.AudioPlaybackStatusChanged, false);
|
642
|
+
};
|
643
|
+
|
644
|
+
private handleDeviceChange = async () => {
|
645
|
+
this.emit(RoomEvent.MediaDevicesChanged);
|
646
|
+
};
|
647
|
+
|
648
|
+
private handleRoomUpdate = (r: RoomModel) => {
|
649
|
+
this.metadata = r.metadata;
|
650
|
+
this.emit(RoomEvent.RoomMetadataChanged, r.metadata);
|
651
|
+
};
|
652
|
+
|
653
|
+
private handleConnectionQualityUpdate = (update: ConnectionQualityUpdate) => {
|
654
|
+
update.updates.forEach((info) => {
|
655
|
+
if (info.participantSid === this.localParticipant.sid) {
|
656
|
+
this.localParticipant.setConnectionQuality(info.quality);
|
657
|
+
return;
|
658
|
+
}
|
659
|
+
const participant = this.participants.get(info.participantSid);
|
660
|
+
if (participant) {
|
661
|
+
participant.setConnectionQuality(info.quality);
|
662
|
+
}
|
663
|
+
});
|
664
|
+
};
|
665
|
+
|
666
|
+
private acquireAudioContext() {
|
667
|
+
if (this.audioContext) {
|
668
|
+
this.audioContext.close();
|
669
|
+
}
|
670
|
+
// by using an AudioContext, it reduces lag on audio elements
|
671
|
+
// https://stackoverflow.com/questions/9811429/html5-audio-tag-on-safari-has-a-delay/54119854#54119854
|
672
|
+
// @ts-ignore
|
673
|
+
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
674
|
+
if (AudioContext) {
|
675
|
+
this.audioContext = new AudioContext();
|
676
|
+
}
|
677
|
+
}
|
678
|
+
|
679
|
+
private getOrCreateParticipant(
|
680
|
+
id: string,
|
681
|
+
info?: ParticipantInfo,
|
682
|
+
): RemoteParticipant {
|
683
|
+
let participant = this.participants.get(id);
|
684
|
+
if (!participant) {
|
685
|
+
// it's possible for the RTC track to arrive before signaling data
|
686
|
+
// when this happens, we'll create the participant and make the track work
|
687
|
+
if (info) {
|
688
|
+
participant = RemoteParticipant.fromParticipantInfo(
|
689
|
+
this.engine.client,
|
690
|
+
info,
|
691
|
+
);
|
692
|
+
} else {
|
693
|
+
participant = new RemoteParticipant(this.engine.client, id, '');
|
694
|
+
}
|
695
|
+
this.participants.set(id, participant);
|
696
|
+
// also forward events
|
697
|
+
|
698
|
+
// trackPublished is only fired for tracks added after both local participant
|
699
|
+
// and remote participant joined the room
|
700
|
+
participant
|
701
|
+
.on(ParticipantEvent.TrackPublished, (trackPublication: RemoteTrackPublication) => {
|
702
|
+
this.emit(RoomEvent.TrackPublished, trackPublication, participant);
|
703
|
+
})
|
704
|
+
.on(ParticipantEvent.TrackSubscribed,
|
705
|
+
(track: RemoteTrack, publication: RemoteTrackPublication) => {
|
706
|
+
// monitor playback status
|
707
|
+
if (track.kind === Track.Kind.Audio) {
|
708
|
+
track.on(TrackEvent.AudioPlaybackStarted, this.handleAudioPlaybackStarted);
|
709
|
+
track.on(TrackEvent.AudioPlaybackFailed, this.handleAudioPlaybackFailed);
|
710
|
+
}
|
711
|
+
this.emit(RoomEvent.TrackSubscribed, track, publication, participant);
|
712
|
+
})
|
713
|
+
.on(ParticipantEvent.TrackUnpublished, (publication: RemoteTrackPublication) => {
|
714
|
+
this.emit(RoomEvent.TrackUnpublished, publication, participant);
|
715
|
+
})
|
716
|
+
.on(ParticipantEvent.TrackUnsubscribed,
|
717
|
+
(track: RemoteTrack, publication: RemoteTrackPublication) => {
|
718
|
+
this.emit(RoomEvent.TrackUnsubscribed, track, publication, participant);
|
719
|
+
})
|
720
|
+
.on(ParticipantEvent.TrackSubscriptionFailed, (sid: string) => {
|
721
|
+
this.emit(RoomEvent.TrackSubscriptionFailed, sid, participant);
|
722
|
+
})
|
723
|
+
.on(ParticipantEvent.TrackMuted, (pub: TrackPublication) => {
|
724
|
+
this.emit(RoomEvent.TrackMuted, pub, participant);
|
725
|
+
})
|
726
|
+
.on(ParticipantEvent.TrackUnmuted, (pub: TrackPublication) => {
|
727
|
+
this.emit(RoomEvent.TrackUnmuted, pub, participant);
|
728
|
+
})
|
729
|
+
.on(ParticipantEvent.MetadataChanged, (metadata: any) => {
|
730
|
+
this.emit(RoomEvent.MetadataChanged, metadata, participant);
|
731
|
+
})
|
732
|
+
.on(ParticipantEvent.ParticipantMetadataChanged, (metadata: any) => {
|
733
|
+
this.emit(RoomEvent.ParticipantMetadataChanged, metadata, participant);
|
734
|
+
})
|
735
|
+
.on(ParticipantEvent.ConnectionQualityChanged, (quality: ConnectionQuality) => {
|
736
|
+
this.emit(RoomEvent.ConnectionQualityChanged, quality, participant);
|
737
|
+
});
|
738
|
+
}
|
739
|
+
return participant;
|
740
|
+
}
|
741
|
+
|
742
|
+
private sendSyncState() {
|
743
|
+
if (this.engine.subscriber === undefined
|
744
|
+
|| this.engine.subscriber.pc.localDescription === null) {
|
745
|
+
return;
|
746
|
+
}
|
747
|
+
const previousSdp = this.engine.subscriber.pc.localDescription;
|
748
|
+
|
749
|
+
/* 1. autosubscribe on, so subscribed tracks = all tracks - unsub tracks,
|
750
|
+
in this case, we send unsub tracks, so server add all tracks to this
|
751
|
+
subscribe pc and unsub special tracks from it.
|
752
|
+
2. autosubscribe off, we send subscribed tracks.
|
753
|
+
*/
|
754
|
+
const sendUnsub = this.connOptions?.autoSubscribe || false;
|
755
|
+
const trackSids = new Array<string>();
|
756
|
+
this.participants.forEach((participant) => {
|
757
|
+
participant.tracks.forEach((track) => {
|
758
|
+
if (track.isSubscribed !== sendUnsub) {
|
759
|
+
trackSids.push(track.trackSid);
|
760
|
+
}
|
761
|
+
});
|
762
|
+
});
|
763
|
+
|
764
|
+
this.engine.client.sendSyncState({
|
765
|
+
answer: toProtoSessionDescription({
|
766
|
+
sdp: previousSdp.sdp,
|
767
|
+
type: previousSdp.type,
|
768
|
+
}),
|
769
|
+
subscription: {
|
770
|
+
trackSids,
|
771
|
+
subscribe: !sendUnsub,
|
772
|
+
participantTracks: [],
|
773
|
+
},
|
774
|
+
publishTracks: this.localParticipant.publishedTracksInfo(),
|
775
|
+
});
|
776
|
+
}
|
777
|
+
|
778
|
+
/** @internal */
|
779
|
+
emit(event: string | symbol, ...args: any[]): boolean {
|
780
|
+
log.debug('room event', event, ...args);
|
781
|
+
return super.emit(event, ...args);
|
782
|
+
}
|
783
|
+
}
|
784
|
+
|
785
|
+
export default Room;
|