@fishjam-cloud/ts-client 0.5.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 (52) hide show
  1. package/README.md +134 -0
  2. package/dist/src/FishjamClient.d.ts +523 -0
  3. package/dist/src/FishjamClient.js +666 -0
  4. package/dist/src/auth.d.ts +3 -0
  5. package/dist/src/auth.js +8 -0
  6. package/dist/src/index.d.ts +7 -0
  7. package/dist/src/index.js +3 -0
  8. package/dist/src/protos/fishjam/peer_notifications.d.ts +78 -0
  9. package/dist/src/protos/fishjam/peer_notifications.js +302 -0
  10. package/dist/src/protos/index.d.ts +1 -0
  11. package/dist/src/protos/index.js +1 -0
  12. package/dist/src/reconnection.d.ts +27 -0
  13. package/dist/src/reconnection.js +121 -0
  14. package/dist/src/webrtc/CommandsQueue.d.ts +23 -0
  15. package/dist/src/webrtc/CommandsQueue.js +102 -0
  16. package/dist/src/webrtc/ConnectionManager.d.ts +29 -0
  17. package/dist/src/webrtc/ConnectionManager.js +82 -0
  18. package/dist/src/webrtc/bitrate.d.ts +12 -0
  19. package/dist/src/webrtc/bitrate.js +12 -0
  20. package/dist/src/webrtc/deferred.d.ts +8 -0
  21. package/dist/src/webrtc/deferred.js +17 -0
  22. package/dist/src/webrtc/index.d.ts +3 -0
  23. package/dist/src/webrtc/index.js +1 -0
  24. package/dist/src/webrtc/internal.d.ts +26 -0
  25. package/dist/src/webrtc/internal.js +34 -0
  26. package/dist/src/webrtc/mediaEvent.d.ts +10 -0
  27. package/dist/src/webrtc/mediaEvent.js +16 -0
  28. package/dist/src/webrtc/tracks/Local.d.ts +51 -0
  29. package/dist/src/webrtc/tracks/Local.js +289 -0
  30. package/dist/src/webrtc/tracks/LocalTrack.d.ts +62 -0
  31. package/dist/src/webrtc/tracks/LocalTrack.js +235 -0
  32. package/dist/src/webrtc/tracks/LocalTrackManager.d.ts +38 -0
  33. package/dist/src/webrtc/tracks/LocalTrackManager.js +83 -0
  34. package/dist/src/webrtc/tracks/Remote.d.ts +38 -0
  35. package/dist/src/webrtc/tracks/Remote.js +200 -0
  36. package/dist/src/webrtc/tracks/RemoteTrack.d.ts +39 -0
  37. package/dist/src/webrtc/tracks/RemoteTrack.js +64 -0
  38. package/dist/src/webrtc/tracks/TrackCommon.d.ts +8 -0
  39. package/dist/src/webrtc/tracks/TrackCommon.js +1 -0
  40. package/dist/src/webrtc/tracks/bandwidth.d.ts +1 -0
  41. package/dist/src/webrtc/tracks/bandwidth.js +27 -0
  42. package/dist/src/webrtc/tracks/encodings.d.ts +1 -0
  43. package/dist/src/webrtc/tracks/encodings.js +7 -0
  44. package/dist/src/webrtc/tracks/transceivers.d.ts +2 -0
  45. package/dist/src/webrtc/tracks/transceivers.js +78 -0
  46. package/dist/src/webrtc/types.d.ts +306 -0
  47. package/dist/src/webrtc/types.js +2 -0
  48. package/dist/src/webrtc/voiceActivityDetection.d.ts +2 -0
  49. package/dist/src/webrtc/voiceActivityDetection.js +2 -0
  50. package/dist/src/webrtc/webRTCEndpoint.d.ts +299 -0
  51. package/dist/src/webrtc/webRTCEndpoint.js +730 -0
  52. package/package.json +82 -0
@@ -0,0 +1,730 @@
1
+ import { deserializeMediaEvent, generateCustomEvent, generateMediaEvent, serializeMediaEvent, } from './mediaEvent';
2
+ import { v4 as uuidv4 } from 'uuid';
3
+ import EventEmitter from 'events';
4
+ import { Deferred } from './deferred';
5
+ import { isEncoding } from './types';
6
+ import { LocalTrackManager } from './tracks/LocalTrackManager';
7
+ import { CommandsQueue } from './CommandsQueue';
8
+ import { Remote } from './tracks/Remote';
9
+ import { Local } from './tracks/Local';
10
+ import { ConnectionManager } from './ConnectionManager';
11
+ /**
12
+ * Main class that is responsible for connecting to the RTC Engine, sending and receiving media.
13
+ */
14
+ export class WebRTCEndpoint extends EventEmitter {
15
+ endpointMetadataParser;
16
+ trackMetadataParser;
17
+ localTrackManager;
18
+ remote;
19
+ local;
20
+ commandsQueue;
21
+ bandwidthEstimation = BigInt(0);
22
+ connectionManager;
23
+ clearConnectionCallbacks = null;
24
+ constructor(config) {
25
+ super();
26
+ this.endpointMetadataParser =
27
+ config?.endpointMetadataParser ?? ((x) => x);
28
+ this.trackMetadataParser =
29
+ config?.trackMetadataParser ?? ((x) => x);
30
+ const sendEvent = (mediaEvent) => this.sendMediaEvent(mediaEvent);
31
+ const emit = (events, ...args) => {
32
+ this.emit(events, ...args);
33
+ };
34
+ this.remote = new Remote(emit, sendEvent, this.endpointMetadataParser, this.trackMetadataParser);
35
+ this.local = new Local(emit, sendEvent, this.endpointMetadataParser, this.trackMetadataParser);
36
+ this.localTrackManager = new LocalTrackManager(this.local, sendEvent);
37
+ this.commandsQueue = new CommandsQueue(this.localTrackManager);
38
+ }
39
+ /**
40
+ * Tries to connect to the RTC Engine. If user is successfully connected then {@link WebRTCEndpointEvents.connected}
41
+ * will be emitted.
42
+ *
43
+ * @param metadata - Any information that other endpoints will receive in {@link WebRTCEndpointEvents.endpointAdded}
44
+ * after accepting this endpoint
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * let webrtc = new WebRTCEndpoint();
49
+ * webrtc.connect({displayName: "Bob"});
50
+ * ```
51
+ */
52
+ connect = (metadata) => {
53
+ this.local.setEndpointMetadata(metadata);
54
+ const mediaEvent = generateMediaEvent('connect', {
55
+ metadata: metadata,
56
+ });
57
+ this.sendMediaEvent(mediaEvent);
58
+ };
59
+ /**
60
+ * Feeds media event received from RTC Engine to {@link WebRTCEndpoint}.
61
+ * This function should be called whenever some media event from RTC Engine
62
+ * was received and can result in {@link WebRTCEndpoint} generating some other
63
+ * media events.
64
+ *
65
+ * @param mediaEvent - String data received over custom signalling layer.
66
+ *
67
+ * @example
68
+ * This example assumes phoenix channels as signalling layer.
69
+ * As phoenix channels require objects, RTC Engine encapsulates binary data into
70
+ * map with one field that is converted to object with one field on the TS side.
71
+ * ```ts
72
+ * webrtcChannel.on("mediaEvent", (event) => webrtc.receiveMediaEvent(event.data));
73
+ * ```
74
+ */
75
+ receiveMediaEvent = async (mediaEvent) => {
76
+ const deserializedMediaEvent = deserializeMediaEvent(mediaEvent);
77
+ switch (deserializedMediaEvent.type) {
78
+ case 'connected': {
79
+ this.local.setLocalEndpointId(deserializedMediaEvent.data.id);
80
+ const endpoints = deserializedMediaEvent.data
81
+ .otherEndpoints;
82
+ // todo implement track mapping (+ validate metadata)
83
+ // todo implement endpoint metadata mapping
84
+ endpoints.forEach((endpoint) => {
85
+ this.remote.addRemoteEndpoint(endpoint);
86
+ });
87
+ const remoteEndpoints = Object.values(this.remote.getRemoteEndpoints());
88
+ this.emit('connected', this.local.getEndpoint().id, remoteEndpoints);
89
+ break;
90
+ }
91
+ default:
92
+ if (this.getEndpointId() != null)
93
+ await this.handleMediaEvent(deserializedMediaEvent);
94
+ }
95
+ };
96
+ getEndpointId = () => this.local.getEndpoint().id;
97
+ onTrackReady = (event) => {
98
+ const stream = event.streams[0];
99
+ if (!stream)
100
+ throw new Error('Cannot find media stream');
101
+ const mid = event.transceiver.mid;
102
+ const remoteTrack = this.remote.getTrackByMid(mid);
103
+ remoteTrack.setReady(stream, event.track);
104
+ this.emit('trackReady', remoteTrack.trackContext);
105
+ };
106
+ /**
107
+ * Retrieves statistics related to the RTCPeerConnection.
108
+ * These statistics provide insights into the performance and status of the connection.
109
+ *
110
+ * @return {Promise<RTCStatsReport>}
111
+ *
112
+ * @external RTCPeerConnection#getStats()
113
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/getStats | MDN Web Docs: RTCPeerConnection.getStats()}
114
+ */
115
+ async getStatistics(selector) {
116
+ return ((await this.connectionManager?.getConnection().getStats(selector)) ??
117
+ new Map());
118
+ }
119
+ /**
120
+ * Returns a snapshot of currently received remote tracks.
121
+ *
122
+ * @example
123
+ * if (webRTCEndpoint.getRemoteTracks()[trackId]?.simulcastConfig?.enabled) {
124
+ * webRTCEndpoint.setTargetTrackEncoding(trackId, encoding);
125
+ * }
126
+ */
127
+ getRemoteTracks() {
128
+ return this.remote.getRemoteTrackContexts();
129
+ }
130
+ /**
131
+ * Returns a snapshot of currently received remote endpoints.
132
+ */
133
+ getRemoteEndpoints() {
134
+ return this.remote.getRemoteEndpoints();
135
+ }
136
+ getLocalEndpoint() {
137
+ return this.local.getEndpoint();
138
+ }
139
+ getBandwidthEstimation() {
140
+ return this.bandwidthEstimation;
141
+ }
142
+ handleMediaEvent = async (deserializedMediaEvent) => {
143
+ switch (deserializedMediaEvent.type) {
144
+ case 'offerData': {
145
+ await this.onOfferData(deserializedMediaEvent);
146
+ break;
147
+ }
148
+ case 'tracksAdded': {
149
+ this.localTrackManager.ongoingRenegotiation = true;
150
+ const data = deserializedMediaEvent.data;
151
+ if (this.getEndpointId() === data.endpointId)
152
+ return;
153
+ this.remote.addTracks(data.endpointId, data.tracks, data.trackIdToMetadata);
154
+ break;
155
+ }
156
+ case 'tracksRemoved': {
157
+ this.localTrackManager.ongoingRenegotiation = true;
158
+ const data = deserializedMediaEvent.data;
159
+ const endpointId = data.endpointId;
160
+ if (this.getEndpointId() === endpointId)
161
+ return;
162
+ this.remote.removeTracks(data.trackIds);
163
+ break;
164
+ }
165
+ case 'sdpAnswer':
166
+ await this.onSdpAnswer(deserializedMediaEvent.data);
167
+ this.localTrackManager.ongoingRenegotiation = false;
168
+ this.commandsQueue.processNextCommand();
169
+ break;
170
+ case 'candidate':
171
+ await this.onRemoteCandidate(deserializedMediaEvent.data);
172
+ break;
173
+ case 'endpointAdded':
174
+ const endpoint = deserializedMediaEvent.data;
175
+ if (endpoint.id === this.getEndpointId())
176
+ return;
177
+ this.remote.addRemoteEndpoint(endpoint);
178
+ break;
179
+ case 'endpointRemoved':
180
+ if (deserializedMediaEvent.data.id === this.local.getEndpoint().id) {
181
+ this.cleanUp();
182
+ this.emit('disconnected');
183
+ return;
184
+ }
185
+ if (this.getEndpointId() === deserializedMediaEvent.data.id)
186
+ return;
187
+ this.remote.removeRemoteEndpoint(deserializedMediaEvent.data.id);
188
+ break;
189
+ case 'endpointUpdated':
190
+ if (this.getEndpointId() === deserializedMediaEvent.data.id)
191
+ return;
192
+ this.remote.updateRemoteEndpoint(deserializedMediaEvent.data);
193
+ break;
194
+ case 'trackUpdated': {
195
+ if (this.getEndpointId() === deserializedMediaEvent.data.endpointId)
196
+ return;
197
+ this.remote.updateRemoteTrack(deserializedMediaEvent.data);
198
+ break;
199
+ }
200
+ case 'trackEncodingDisabled': {
201
+ if (this.getEndpointId() === deserializedMediaEvent.data.endpointId)
202
+ return;
203
+ this.remote.disableRemoteTrackEncoding(deserializedMediaEvent.data.trackId, deserializedMediaEvent.data.encoding);
204
+ break;
205
+ }
206
+ case 'trackEncodingEnabled': {
207
+ const data = deserializedMediaEvent.data;
208
+ if (this.getEndpointId() === data.endpointId)
209
+ return;
210
+ this.remote.enableRemoteTrackEncoding(data.trackId, data.encoding);
211
+ break;
212
+ }
213
+ case 'encodingSwitched': {
214
+ const data = deserializedMediaEvent.data;
215
+ this.remote.setRemoteTrackEncoding(data.trackId, data.encoding, data.reason);
216
+ break;
217
+ }
218
+ case 'custom':
219
+ await this.handleMediaEvent(deserializedMediaEvent.data);
220
+ break;
221
+ case 'error':
222
+ this.emit('signalingError', {
223
+ message: deserializedMediaEvent.data.message,
224
+ });
225
+ this.disconnect();
226
+ break;
227
+ case 'vadNotification': {
228
+ this.remote.setRemoteTrackVadStatus(deserializedMediaEvent.data.trackId, deserializedMediaEvent.data.status);
229
+ break;
230
+ }
231
+ case 'bandwidthEstimation': {
232
+ this.bandwidthEstimation = deserializedMediaEvent.data.estimation;
233
+ this.emit('bandwidthEstimationChanged', this.bandwidthEstimation);
234
+ break;
235
+ }
236
+ default:
237
+ console.warn('Received unknown media event: ', deserializedMediaEvent.type);
238
+ break;
239
+ }
240
+ };
241
+ onSdpAnswer = async (data) => {
242
+ this.remote.updateMLineIds(data.midToTrackId);
243
+ this.local.updateMLineIds(data.midToTrackId);
244
+ Object.values(data.midToTrackId)
245
+ .map((trackId) => {
246
+ if (!trackId)
247
+ throw new Error('TrackId is not defined');
248
+ if (typeof trackId !== 'string')
249
+ throw new Error('TrackId is not a string');
250
+ return trackId;
251
+ })
252
+ .map((trackId) => this.local.getTrackByMidOrNull(trackId))
253
+ .filter((localTrack) => localTrack !== null)
254
+ .forEach((localTrack) => {
255
+ const trackContext = localTrack.trackContext;
256
+ trackContext.negotiationStatus = 'done';
257
+ if (trackContext.pendingMetadataUpdate) {
258
+ const mediaEvent = generateMediaEvent('updateTrackMetadata', {
259
+ trackId: localTrack.id,
260
+ trackMetadata: trackContext.metadata,
261
+ });
262
+ this.sendMediaEvent(mediaEvent);
263
+ }
264
+ trackContext.pendingMetadataUpdate = false;
265
+ });
266
+ if (!this.connectionManager)
267
+ throw new Error(`There is no active RTCPeerConnection`);
268
+ // probably there is no need to reassign it on every onAnswer
269
+ this.connectionManager.setOnTrackReady((event) => {
270
+ this.onTrackReady(event);
271
+ });
272
+ try {
273
+ await this.connectionManager.setRemoteDescription(data);
274
+ await this.local.disableAllLocalTrackEncodings();
275
+ }
276
+ catch (err) {
277
+ console.error(err);
278
+ }
279
+ };
280
+ /**
281
+ * Adds track that will be sent to the RTC Engine.
282
+ * @param track - Audio or video track e.g. from your microphone or camera.
283
+ * @param trackMetadata - Any information about this track that other endpoints will
284
+ * receive in {@link WebRTCEndpointEvents.endpointAdded}. E.g. this can source of the track - whether it's
285
+ * screensharing, webcam or some other media device.
286
+ * @param simulcastConfig - Simulcast configuration. By default simulcast is disabled.
287
+ * For more information refer to {@link SimulcastConfig}.
288
+ * @param maxBandwidth - maximal bandwidth this track can use.
289
+ * Defaults to 0 which is unlimited.
290
+ * This option has no effect for simulcast and audio tracks.
291
+ * For simulcast tracks use `{@link WebRTCEndpoint.setTrackBandwidth}.
292
+ * @returns {string} Returns id of added track
293
+ * @example
294
+ * ```ts
295
+ * let localStream: MediaStream = new MediaStream();
296
+ * try {
297
+ * localAudioStream = await navigator.mediaDevices.getUserMedia(
298
+ * AUDIO_CONSTRAINTS
299
+ * );
300
+ * localAudioStream
301
+ * .getTracks()
302
+ * .forEach((track) => localStream.addTrack(track));
303
+ * } catch (error) {
304
+ * console.error("Couldn't get microphone permission:", error);
305
+ * }
306
+ *
307
+ * try {
308
+ * localVideoStream = await navigator.mediaDevices.getUserMedia(
309
+ * VIDEO_CONSTRAINTS
310
+ * );
311
+ * localVideoStream
312
+ * .getTracks()
313
+ * .forEach((track) => localStream.addTrack(track));
314
+ * } catch (error) {
315
+ * console.error("Couldn't get camera permission:", error);
316
+ * }
317
+ *
318
+ * localStream
319
+ * .getTracks()
320
+ * .forEach((track) => webrtc.addTrack(track, localStream));
321
+ * ```
322
+ */
323
+ async addTrack(track, trackMetadata, simulcastConfig = {
324
+ enabled: false,
325
+ activeEncodings: [],
326
+ disabledEncodings: [],
327
+ }, maxBandwidth = 0) {
328
+ const resolutionNotifier = new Deferred();
329
+ const trackId = this.getTrackId(uuidv4());
330
+ const stream = new MediaStream();
331
+ let metadata;
332
+ try {
333
+ const parsedMetadata = this.trackMetadataParser(trackMetadata);
334
+ metadata = parsedMetadata;
335
+ stream.addTrack(track);
336
+ this.commandsQueue.pushCommand({
337
+ handler: () => {
338
+ this.localTrackManager.addTrackHandler(trackId, track, stream, parsedMetadata, simulcastConfig, maxBandwidth);
339
+ },
340
+ parse: () => this.localTrackManager.parseAddTrack(track, simulcastConfig, maxBandwidth),
341
+ resolve: 'after-renegotiation',
342
+ resolutionNotifier,
343
+ });
344
+ }
345
+ catch (error) {
346
+ resolutionNotifier.reject(error);
347
+ }
348
+ await resolutionNotifier.promise;
349
+ this.emit('localTrackAdded', {
350
+ trackId,
351
+ track,
352
+ stream,
353
+ trackMetadata: metadata,
354
+ simulcastConfig,
355
+ maxBandwidth,
356
+ });
357
+ return trackId;
358
+ }
359
+ /**
360
+ * Replaces a track that is being sent to the RTC Engine.
361
+ * @param trackId - Audio or video track.
362
+ * @param {string} trackId - Id of audio or video track to replace.
363
+ * @param {MediaStreamTrack} newTrack
364
+ * @param {any} [newTrackMetadata] - Optional track metadata to apply to the new track. If no
365
+ * track metadata is passed, the old track metadata is retained.
366
+ * @returns {Promise<boolean>} success
367
+ * @example
368
+ * ```ts
369
+ * // setup camera
370
+ * let localStream: MediaStream = new MediaStream();
371
+ * try {
372
+ * localVideoStream = await navigator.mediaDevices.getUserMedia(
373
+ * VIDEO_CONSTRAINTS
374
+ * );
375
+ * localVideoStream
376
+ * .getTracks()
377
+ * .forEach((track) => localStream.addTrack(track));
378
+ * } catch (error) {
379
+ * console.error("Couldn't get camera permission:", error);
380
+ * }
381
+ * let oldTrackId;
382
+ * localStream
383
+ * .getTracks()
384
+ * .forEach((track) => trackId = webrtc.addTrack(track, localStream));
385
+ *
386
+ * // change camera
387
+ * const oldTrack = localStream.getVideoTracks()[0];
388
+ * let videoDeviceId = "abcd-1234";
389
+ * navigator.mediaDevices.getUserMedia({
390
+ * video: {
391
+ * ...(VIDEO_CONSTRAINTS as {}),
392
+ * deviceId: {
393
+ * exact: videoDeviceId,
394
+ * },
395
+ * }
396
+ * })
397
+ * .then((stream) => {
398
+ * let videoTrack = stream.getVideoTracks()[0];
399
+ * webrtc.replaceTrack(oldTrackId, videoTrack);
400
+ * })
401
+ * .catch((error) => {
402
+ * console.error('Error switching camera', error);
403
+ * })
404
+ * ```
405
+ */
406
+ async replaceTrack(trackId, newTrack, newTrackMetadata) {
407
+ const resolutionNotifier = new Deferred();
408
+ try {
409
+ const newMetadata = newTrackMetadata !== undefined
410
+ ? this.trackMetadataParser(newTrackMetadata)
411
+ : undefined;
412
+ this.commandsQueue.pushCommand({
413
+ handler: () => {
414
+ this.localTrackManager.replaceTrackHandler(this, trackId, newTrack, newMetadata);
415
+ },
416
+ resolutionNotifier,
417
+ resolve: 'immediately',
418
+ });
419
+ }
420
+ catch (error) {
421
+ resolutionNotifier.reject(error);
422
+ }
423
+ return resolutionNotifier.promise.then(() => {
424
+ this.emit('localTrackReplaced', {
425
+ trackId,
426
+ track: newTrack,
427
+ metadata: newTrackMetadata,
428
+ });
429
+ });
430
+ }
431
+ /**
432
+ * Updates maximum bandwidth for the track identified by trackId.
433
+ * This value directly translates to quality of the stream and, in case of video, to the amount of RTP packets being sent.
434
+ * In case trackId points at the simulcast track bandwidth is split between all of the variant streams proportionally to their resolution.
435
+ *
436
+ * @param {string} trackId
437
+ * @param {BandwidthLimit} bandwidth in kbps
438
+ * @returns {Promise<boolean>} success
439
+ */
440
+ setTrackBandwidth(trackId, bandwidth) {
441
+ if (!this.connectionManager)
442
+ throw new Error(`There is no active RTCPeerConnection`);
443
+ return this.local.setTrackBandwidth(trackId, bandwidth);
444
+ }
445
+ /**
446
+ * Updates maximum bandwidth for the given simulcast encoding of the given track.
447
+ *
448
+ * @param {string} trackId - id of the track
449
+ * @param {string} rid - rid of the encoding
450
+ * @param {BandwidthLimit} bandwidth - desired max bandwidth used by the encoding (in kbps)
451
+ * @returns
452
+ */
453
+ async setEncodingBandwidth(trackId, rid, bandwidth) {
454
+ if (!isEncoding(rid))
455
+ throw new Error(`Rid is invalid ${rid}`);
456
+ if (!this.connectionManager)
457
+ throw new Error(`There is no active RTCPeerConnection`);
458
+ return await this.local.setEncodingBandwidth(trackId, rid, bandwidth);
459
+ }
460
+ /**
461
+ * Removes a track from connection that was sent to the RTC Engine.
462
+ * @param {string} trackId - Id of audio or video track to remove.
463
+ * @example
464
+ * ```ts
465
+ * // setup camera
466
+ * let localStream: MediaStream = new MediaStream();
467
+ * try {
468
+ * localVideoStream = await navigator.mediaDevices.getUserMedia(
469
+ * VIDEO_CONSTRAINTS
470
+ * );
471
+ * localVideoStream
472
+ * .getTracks()
473
+ * .forEach((track) => localStream.addTrack(track));
474
+ * } catch (error) {
475
+ * console.error("Couldn't get camera permission:", error);
476
+ * }
477
+ *
478
+ * let trackId
479
+ * localStream
480
+ * .getTracks()
481
+ * .forEach((track) => trackId = webrtc.addTrack(track, localStream));
482
+ *
483
+ * // remove track
484
+ * webrtc.removeTrack(trackId)
485
+ * ```
486
+ */
487
+ async removeTrack(trackId) {
488
+ const resolutionNotifier = new Deferred();
489
+ this.commandsQueue.pushCommand({
490
+ handler: () => {
491
+ this.localTrackManager.removeTrackHandler(trackId);
492
+ },
493
+ resolutionNotifier,
494
+ resolve: 'after-renegotiation',
495
+ });
496
+ await resolutionNotifier.promise;
497
+ this.emit('localTrackRemoved', {
498
+ trackId,
499
+ });
500
+ }
501
+ /**
502
+ * Sets track variant that server should send to the client library.
503
+ *
504
+ * The variant will be sent whenever it is available.
505
+ * If chosen variant is temporarily unavailable, some other variant
506
+ * will be sent until the chosen variant becomes active again.
507
+ *
508
+ * @param {string} trackId - id of track
509
+ * @param {Encoding} variant - variant to receive
510
+ * @example
511
+ * ```ts
512
+ * webrtc.setTargetTrackEncoding(incomingTrackCtx.trackId, "l")
513
+ * ```
514
+ */
515
+ setTargetTrackEncoding(trackId, variant) {
516
+ this.remote.setTargetRemoteTrackEncoding(trackId, variant);
517
+ }
518
+ /**
519
+ * Enables track encoding so that it will be sent to the server.
520
+ * @param {string} trackId - id of track
521
+ * @param {Encoding} encoding - encoding that will be enabled
522
+ * @example
523
+ * ```ts
524
+ * const trackId = webrtc.addTrack(track, stream, {}, {enabled: true, activeEncodings: ["l", "m", "h"]});
525
+ * webrtc.disableTrackEncoding(trackId, "l");
526
+ * // wait some time
527
+ * webrtc.enableTrackEncoding(trackId, "l");
528
+ * ```
529
+ */
530
+ enableTrackEncoding = async (trackId, encoding) => {
531
+ await this.local.enableLocalTrackEncoding(trackId, encoding);
532
+ };
533
+ /**
534
+ * Disables track encoding so that it will be no longer sent to the server.
535
+ * @param {string} trackId - id of track
536
+ * @param {Encoding} encoding - encoding that will be disabled
537
+ * @example
538
+ * ```ts
539
+ * const trackId = webrtc.addTrack(track, stream, {}, {enabled: true, activeEncodings: ["l", "m", "h"]});
540
+ * webrtc.disableTrackEncoding(trackId, "l");
541
+ * ```
542
+ */
543
+ disableTrackEncoding = async (trackId, encoding) => {
544
+ await this.local.disableLocalTrackEncoding(trackId, encoding);
545
+ };
546
+ /**
547
+ * Updates the metadata for the current endpoint.
548
+ * @param metadata - Data about this endpoint that other endpoints will receive upon being added.
549
+ *
550
+ * If the metadata is different from what is already tracked in the room, the optional
551
+ * event `endpointUpdated` will be emitted for other endpoint in the room.
552
+ */
553
+ updateEndpointMetadata = (metadata) => {
554
+ this.local.updateEndpointMetadata(metadata);
555
+ };
556
+ /**
557
+ * Updates the metadata for a specific track.
558
+ * @param trackId - trackId (generated in addTrack) of audio or video track.
559
+ * @param trackMetadata - Data about this track that other endpoint will receive upon being added.
560
+ *
561
+ * If the metadata is different from what is already tracked in the room, the optional
562
+ * event `trackUpdated` will be emitted for other endpoints in the room.
563
+ */
564
+ updateTrackMetadata = (trackId, trackMetadata) => {
565
+ this.local.updateLocalTrackMetadata(trackId, trackMetadata);
566
+ };
567
+ /**
568
+ * Disconnects from the room. This function should be called when user disconnects from the room
569
+ * in a clean way e.g. by clicking a dedicated, custom button `disconnect`.
570
+ * As a result there will be generated one more media event that should be
571
+ * sent to the RTC Engine. Thanks to it each other endpoint will be notified
572
+ * that endpoint was removed in {@link WebRTCEndpointEvents.endpointRemoved},
573
+ */
574
+ disconnect = () => {
575
+ const mediaEvent = generateMediaEvent('disconnect');
576
+ this.sendMediaEvent(mediaEvent);
577
+ this.emit('disconnectRequested', {});
578
+ this.cleanUp();
579
+ };
580
+ /**
581
+ * Cleans up {@link WebRTCEndpoint} instance.
582
+ */
583
+ cleanUp = () => {
584
+ if (this.connectionManager) {
585
+ this.clearConnectionCallbacks?.();
586
+ this.connectionManager?.getConnection().close();
587
+ this.commandsQueue.cleanUp();
588
+ this.localTrackManager.cleanUp();
589
+ }
590
+ this.connectionManager = undefined;
591
+ };
592
+ getTrackId(uuid) {
593
+ return `${this.getEndpointId()}:${uuid}`;
594
+ }
595
+ // todo change to private
596
+ sendMediaEvent = (mediaEvent) => {
597
+ const serializedMediaEvent = serializeMediaEvent(mediaEvent);
598
+ this.emit('sendMediaEvent', serializedMediaEvent);
599
+ };
600
+ async createAndSendOffer() {
601
+ const connection = this.connectionManager;
602
+ if (!connection)
603
+ return;
604
+ try {
605
+ const offer = await connection.getConnection().createOffer();
606
+ if (!this.connectionManager) {
607
+ console.warn('RTCPeerConnection stopped or restarted');
608
+ return;
609
+ }
610
+ await connection.getConnection().setLocalDescription(offer);
611
+ if (!this.connectionManager) {
612
+ console.warn('RTCPeerConnection stopped or restarted');
613
+ return;
614
+ }
615
+ const mediaEvent = this.local.createSdpOfferEvent(offer);
616
+ this.sendMediaEvent(mediaEvent);
617
+ this.local.setLocalTrackStatusToOffered();
618
+ }
619
+ catch (error) {
620
+ console.error(error);
621
+ }
622
+ }
623
+ onOfferData = async (offerData) => {
624
+ const connection = this.connectionManager;
625
+ if (connection) {
626
+ connection.getConnection().restartIce();
627
+ }
628
+ else {
629
+ this.setConnection(offerData.data.integratedTurnServers);
630
+ const onIceCandidate = (event) => this.onLocalCandidate(event);
631
+ const onIceCandidateError = (event) => this.onIceCandidateError(event);
632
+ const onConnectionStateChange = (event) => this.onConnectionStateChange(event);
633
+ const onIceConnectionStateChange = (event) => this.onIceConnectionStateChange(event);
634
+ const connection = this.connectionManager;
635
+ if (!connection)
636
+ throw new Error(`There is no active RTCPeerConnection`);
637
+ this.clearConnectionCallbacks = () => {
638
+ connection
639
+ ?.getConnection()
640
+ ?.removeEventListener('icecandidate', onIceCandidate);
641
+ connection
642
+ ?.getConnection()
643
+ ?.removeEventListener('icecandidateerror', onIceCandidateError);
644
+ connection
645
+ ?.getConnection()
646
+ ?.removeEventListener('connectionstatechange', onConnectionStateChange);
647
+ connection
648
+ ?.getConnection()
649
+ ?.removeEventListener('iceconnectionstatechange', onIceConnectionStateChange);
650
+ };
651
+ connection
652
+ .getConnection()
653
+ .addEventListener('icecandidate', onIceCandidate);
654
+ connection
655
+ .getConnection()
656
+ .addEventListener('icecandidateerror', onIceCandidateError);
657
+ connection
658
+ .getConnection()
659
+ .addEventListener('connectionstatechange', onConnectionStateChange);
660
+ connection
661
+ .getConnection()
662
+ .addEventListener('iceconnectionstatechange', onIceConnectionStateChange);
663
+ this.commandsQueue.initConnection(connection);
664
+ this.local.addAllTracksToConnection();
665
+ connection.setTransceiversToReadOnly();
666
+ }
667
+ this.localTrackManager.updateSenders();
668
+ const tracks = new Map(Object.entries(offerData.data.tracksTypes));
669
+ this.connectionManager?.addTransceiversIfNeeded(tracks);
670
+ await this.createAndSendOffer();
671
+ };
672
+ setConnection = (turnServers) => {
673
+ this.connectionManager = new ConnectionManager(turnServers);
674
+ this.localTrackManager.updateConnection(this.connectionManager);
675
+ this.local.updateConnection(this.connectionManager);
676
+ };
677
+ onRemoteCandidate = async (candidate) => {
678
+ try {
679
+ const iceCandidate = new RTCIceCandidate(candidate);
680
+ if (!this.connectionManager) {
681
+ throw new Error('Received new remote candidate but RTCConnection is undefined');
682
+ }
683
+ await this.connectionManager.addIceCandidate(iceCandidate);
684
+ }
685
+ catch (error) {
686
+ console.error(error);
687
+ }
688
+ };
689
+ onLocalCandidate = (event) => {
690
+ if (event.candidate) {
691
+ const mediaEvent = generateCustomEvent({
692
+ type: 'candidate',
693
+ data: {
694
+ candidate: event.candidate.candidate,
695
+ sdpMLineIndex: event.candidate.sdpMLineIndex,
696
+ },
697
+ });
698
+ this.sendMediaEvent(mediaEvent);
699
+ }
700
+ };
701
+ onIceCandidateError = (event) => {
702
+ console.warn(event);
703
+ };
704
+ onConnectionStateChange = (event) => {
705
+ switch (this.localTrackManager.connection?.getConnection().connectionState) {
706
+ case 'failed':
707
+ this.emit('connectionError', {
708
+ message: 'RTCPeerConnection failed',
709
+ event,
710
+ });
711
+ break;
712
+ }
713
+ };
714
+ onIceConnectionStateChange = (event) => {
715
+ switch (this.localTrackManager.connection?.getConnection().iceConnectionState) {
716
+ case 'disconnected':
717
+ console.warn('ICE connection: disconnected');
718
+ // Requesting renegotiation on ICE connection state failed fixes RTCPeerConnection
719
+ // when the user changes their WiFi network.
720
+ this.sendMediaEvent(generateCustomEvent({ type: 'renegotiateTracks' }));
721
+ break;
722
+ case 'failed':
723
+ this.emit('connectionError', {
724
+ message: 'ICE connection failed',
725
+ event,
726
+ });
727
+ break;
728
+ }
729
+ };
730
+ }