@fishjam-cloud/react-client 0.5.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 (44) hide show
  1. package/dist/Client.d.ts +210 -0
  2. package/dist/Client.d.ts.map +1 -0
  3. package/dist/Client.js +589 -0
  4. package/dist/DeviceManager.d.ts +49 -0
  5. package/dist/DeviceManager.d.ts.map +1 -0
  6. package/dist/DeviceManager.js +157 -0
  7. package/dist/ScreenShareManager.d.ts +57 -0
  8. package/dist/ScreenShareManager.d.ts.map +1 -0
  9. package/dist/ScreenShareManager.js +135 -0
  10. package/dist/constraints.d.ts +26 -0
  11. package/dist/constraints.d.ts.map +1 -0
  12. package/dist/constraints.js +47 -0
  13. package/dist/create.d.ts +11 -0
  14. package/dist/create.d.ts.map +1 -0
  15. package/dist/create.js +228 -0
  16. package/dist/index.d.ts +10 -0
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +4 -0
  19. package/dist/mediaInitializer.d.ts +11 -0
  20. package/dist/mediaInitializer.d.ts.map +1 -0
  21. package/dist/mediaInitializer.js +48 -0
  22. package/dist/state.types.d.ts +60 -0
  23. package/dist/state.types.d.ts.map +1 -0
  24. package/dist/state.types.js +1 -0
  25. package/dist/trackManager.d.ts +24 -0
  26. package/dist/trackManager.d.ts.map +1 -0
  27. package/dist/trackManager.js +99 -0
  28. package/dist/types.d.ts +213 -0
  29. package/dist/types.d.ts.map +1 -0
  30. package/dist/types.js +1 -0
  31. package/dist/useSetupMedia.d.ts +3 -0
  32. package/dist/useSetupMedia.d.ts.map +1 -0
  33. package/dist/useSetupMedia.js +262 -0
  34. package/dist/utils/errors.d.ts +7 -0
  35. package/dist/utils/errors.d.ts.map +1 -0
  36. package/dist/utils/errors.js +22 -0
  37. package/dist/utils/localStorage.d.ts +5 -0
  38. package/dist/utils/localStorage.d.ts.map +1 -0
  39. package/dist/utils/localStorage.js +21 -0
  40. package/dist/utils/media.d.ts +12 -0
  41. package/dist/utils/media.d.ts.map +1 -0
  42. package/dist/utils/media.js +64 -0
  43. package/package.json +65 -0
  44. package/readme.md +151 -0
package/dist/Client.js ADDED
@@ -0,0 +1,589 @@
1
+ import EventEmitter from "events";
2
+ import { FishjamClient } from "@fishjam-cloud/ts-client";
3
+ import { ScreenShareManager } from "./ScreenShareManager";
4
+ import { DeviceManager } from "./DeviceManager";
5
+ import { TrackManager } from "./trackManager";
6
+ import { getAvailableMedia, getCorrectedResult } from "./mediaInitializer";
7
+ export class Client extends EventEmitter {
8
+ tsClient;
9
+ screenShareManager;
10
+ local = null;
11
+ peers = {};
12
+ components = {};
13
+ peersTracks = {};
14
+ componentsTracks = {};
15
+ bandwidthEstimation = BigInt(0);
16
+ status = null;
17
+ media = null;
18
+ devices;
19
+ reconnectionStatus = "idle";
20
+ currentScreenShareTrackId = null;
21
+ videoDeviceManager;
22
+ audioDeviceManager;
23
+ videoTrackManager;
24
+ audioTrackManager;
25
+ constructor(config) {
26
+ super();
27
+ this.tsClient = new FishjamClient(config?.clientConfig);
28
+ this.videoDeviceManager = new DeviceManager("video", config?.deviceManagerDefaultConfig);
29
+ this.audioDeviceManager = new DeviceManager("audio", config?.deviceManagerDefaultConfig);
30
+ this.videoTrackManager = new TrackManager(this.tsClient, this.videoDeviceManager);
31
+ this.audioTrackManager = new TrackManager(this.tsClient, this.audioDeviceManager);
32
+ this.screenShareManager = new ScreenShareManager(config?.screenShareManagerDefaultConfig);
33
+ this.devices = {
34
+ camera: {
35
+ broadcast: null,
36
+ status: null,
37
+ stream: null,
38
+ track: null,
39
+ enabled: false,
40
+ mediaStatus: null,
41
+ deviceInfo: null,
42
+ error: null,
43
+ devices: null,
44
+ },
45
+ microphone: {
46
+ broadcast: null,
47
+ status: null,
48
+ stream: null,
49
+ track: null,
50
+ enabled: false,
51
+ mediaStatus: null,
52
+ deviceInfo: null,
53
+ error: null,
54
+ devices: null,
55
+ },
56
+ screenShare: {
57
+ cleanup: async () => { },
58
+ disableTrack: () => { },
59
+ enableTrack: () => { },
60
+ initialize: async () => { },
61
+ startStreaming: (_trackMetadata, _maxBandwidth) => Promise.reject(),
62
+ stopStreaming: () => Promise.reject(),
63
+ broadcast: null,
64
+ status: null,
65
+ stream: null,
66
+ track: null,
67
+ enabled: false,
68
+ mediaStatus: null,
69
+ error: null,
70
+ },
71
+ };
72
+ this.stateToSnapshot();
73
+ this.tsClient.on("socketOpen", (event) => {
74
+ this.status = "connected";
75
+ this.stateToSnapshot();
76
+ this.emit("socketOpen", event, this);
77
+ });
78
+ this.tsClient.on("socketError", (event) => {
79
+ this.stateToSnapshot();
80
+ this.emit("socketError", event, this);
81
+ });
82
+ this.tsClient.on("socketClose", (event) => {
83
+ this.stateToSnapshot();
84
+ this.emit("socketClose", event, this);
85
+ });
86
+ this.tsClient.on("authSuccess", () => {
87
+ this.status = "authenticated";
88
+ this.stateToSnapshot();
89
+ this.emit("authSuccess", this);
90
+ });
91
+ this.tsClient.on("authError", (reason) => {
92
+ this.stateToSnapshot();
93
+ this.status = "error";
94
+ this.emit("authError", reason, this);
95
+ });
96
+ this.tsClient.on("disconnected", () => {
97
+ this.status = null;
98
+ this.videoTrackManager.stopStreaming();
99
+ this.audioTrackManager.stopStreaming();
100
+ this.currentScreenShareTrackId = null;
101
+ this.stateToSnapshot();
102
+ this.emit("disconnected", this);
103
+ });
104
+ this.tsClient.on("joined", (peerId, peers, components) => {
105
+ this.status = "joined";
106
+ this.stateToSnapshot();
107
+ this.emit("joined", { peerId, peers, components }, this);
108
+ });
109
+ this.tsClient.on("joinError", (metadata) => {
110
+ this.status = "error";
111
+ this.stateToSnapshot();
112
+ this.emit("joinError", metadata, this);
113
+ });
114
+ this.tsClient.on("reconnectionStarted", () => {
115
+ this.reconnectionStatus = "reconnecting";
116
+ this.stateToSnapshot();
117
+ this.emit("reconnectionStarted", this);
118
+ });
119
+ this.tsClient.on("reconnected", () => {
120
+ this.reconnectionStatus = "idle";
121
+ this.stateToSnapshot();
122
+ this.emit("reconnected", this);
123
+ });
124
+ this.tsClient.on("reconnectionRetriesLimitReached", () => {
125
+ this.reconnectionStatus = "error";
126
+ this.stateToSnapshot();
127
+ this.emit("reconnectionRetriesLimitReached", this);
128
+ });
129
+ this.tsClient.on("connectionError", (metadata) => {
130
+ this.status = "error";
131
+ this.stateToSnapshot();
132
+ this.emit("connectionError", metadata, this);
133
+ });
134
+ this.tsClient.on("peerJoined", (peer) => {
135
+ this.stateToSnapshot();
136
+ this.emit("peerJoined", peer, this);
137
+ });
138
+ this.tsClient.on("peerUpdated", (peer) => {
139
+ this.stateToSnapshot();
140
+ this.emit("peerUpdated", peer, this);
141
+ });
142
+ this.tsClient.on("peerLeft", (peer) => {
143
+ this.stateToSnapshot();
144
+ this.emit("peerLeft", peer, this);
145
+ });
146
+ this.tsClient.on("componentAdded", (component) => {
147
+ this.stateToSnapshot();
148
+ this.emit("componentAdded", component, this);
149
+ });
150
+ this.tsClient.on("componentUpdated", (component) => {
151
+ this.stateToSnapshot();
152
+ this.emit("componentUpdated", component, this);
153
+ });
154
+ this.tsClient.on("componentRemoved", (component) => {
155
+ this.stateToSnapshot();
156
+ this.emit("componentRemoved", component, this);
157
+ });
158
+ this.tsClient.on("trackReady", (ctx) => {
159
+ this.stateToSnapshot();
160
+ this.emit("trackReady", ctx, this);
161
+ });
162
+ this.tsClient.on("trackAdded", (ctx) => {
163
+ this.stateToSnapshot();
164
+ this.emit("trackAdded", ctx, this);
165
+ ctx.on("encodingChanged", () => {
166
+ this.stateToSnapshot();
167
+ this.emit("encodingChanged", ctx, this);
168
+ });
169
+ ctx.on("voiceActivityChanged", () => {
170
+ this.stateToSnapshot();
171
+ this.emit("voiceActivityChanged", ctx, this);
172
+ });
173
+ });
174
+ this.tsClient.on("trackRemoved", (ctx) => {
175
+ this.stateToSnapshot();
176
+ this.emit("trackRemoved", ctx, this);
177
+ ctx.removeAllListeners();
178
+ });
179
+ this.tsClient.on("trackUpdated", (ctx) => {
180
+ this.stateToSnapshot();
181
+ this.emit("trackUpdated", ctx, this);
182
+ });
183
+ this.tsClient.on("bandwidthEstimationChanged", (estimation) => {
184
+ this.stateToSnapshot();
185
+ this.emit("bandwidthEstimationChanged", estimation, this);
186
+ });
187
+ this.videoDeviceManager.on("deviceDisabled", (state) => {
188
+ this.stateToSnapshot();
189
+ this.emit("deviceDisabled", { ...state, trackType: "video", mediaDeviceType: "userMedia" }, this);
190
+ });
191
+ this.videoDeviceManager.on("deviceEnabled", (state) => {
192
+ this.stateToSnapshot();
193
+ this.emit("deviceEnabled", { ...state, trackType: "video", mediaDeviceType: "userMedia" }, this);
194
+ });
195
+ this.videoDeviceManager.on("managerStarted", (state) => {
196
+ this.stateToSnapshot();
197
+ this.emit("managerStarted", { ...state, trackType: "video", mediaDeviceType: "userMedia" }, this);
198
+ });
199
+ this.videoDeviceManager.on("deviceStopped", (state) => {
200
+ this.stateToSnapshot();
201
+ this.emit("deviceStopped", { ...state, trackType: "video", mediaDeviceType: "userMedia" }, this);
202
+ });
203
+ this.videoDeviceManager.on("deviceReady", ({ stream }) => {
204
+ this.stateToSnapshot();
205
+ this.emit("deviceReady", { trackType: "video", stream, mediaDeviceType: "userMedia" }, this);
206
+ });
207
+ this.videoDeviceManager.on("devicesStarted", (event) => {
208
+ this.stateToSnapshot();
209
+ this.emit("devicesStarted", { ...event, trackType: "video", mediaDeviceType: "userMedia" }, this);
210
+ });
211
+ this.videoDeviceManager.on("devicesReady", (event) => {
212
+ this.stateToSnapshot();
213
+ this.emit("devicesReady", { ...event, trackType: "video", mediaDeviceType: "userMedia" }, this);
214
+ });
215
+ this.videoDeviceManager.on("error", (event) => {
216
+ this.stateToSnapshot();
217
+ this.emit("error", event, this);
218
+ });
219
+ this.audioDeviceManager.on("deviceDisabled", (state) => {
220
+ this.stateToSnapshot();
221
+ this.emit("deviceDisabled", { ...state, trackType: "audio", mediaDeviceType: "userMedia" }, this);
222
+ });
223
+ this.audioDeviceManager.on("deviceEnabled", (state) => {
224
+ this.stateToSnapshot();
225
+ this.emit("deviceEnabled", { ...state, trackType: "audio", mediaDeviceType: "userMedia" }, this);
226
+ });
227
+ this.audioDeviceManager.on("managerStarted", (state) => {
228
+ this.stateToSnapshot();
229
+ this.emit("managerStarted", { ...state, trackType: "audio", mediaDeviceType: "userMedia" }, this);
230
+ });
231
+ this.audioDeviceManager.on("deviceStopped", (state) => {
232
+ this.stateToSnapshot();
233
+ this.emit("deviceStopped", { ...state, trackType: "audio", mediaDeviceType: "userMedia" }, this);
234
+ });
235
+ this.audioDeviceManager.on("deviceReady", ({ stream }) => {
236
+ this.stateToSnapshot();
237
+ this.emit("deviceReady", { trackType: "audio", stream, mediaDeviceType: "userMedia" }, this);
238
+ });
239
+ this.audioDeviceManager.on("devicesStarted", (event) => {
240
+ this.stateToSnapshot();
241
+ this.emit("devicesStarted", { ...event, trackType: "audio", mediaDeviceType: "userMedia" }, this);
242
+ });
243
+ this.audioDeviceManager.on("devicesReady", (event) => {
244
+ this.stateToSnapshot();
245
+ this.emit("devicesReady", { ...event, trackType: "audio", mediaDeviceType: "userMedia" }, this);
246
+ });
247
+ this.audioDeviceManager.on("error", (event) => {
248
+ this.stateToSnapshot();
249
+ this.emit("error", event, this);
250
+ });
251
+ this.screenShareManager.on("deviceDisabled", (event) => {
252
+ this.stateToSnapshot();
253
+ this.emit("deviceDisabled", { trackType: event.type, mediaDeviceType: "displayMedia" }, this);
254
+ });
255
+ this.screenShareManager.on("deviceEnabled", (event) => {
256
+ this.stateToSnapshot();
257
+ this.emit("deviceEnabled", { trackType: event.type, mediaDeviceType: "displayMedia" }, this);
258
+ });
259
+ this.screenShareManager.on("deviceStopped", async (event) => {
260
+ this.stateToSnapshot();
261
+ this.emit("deviceStopped", { trackType: event.type, mediaDeviceType: "displayMedia" }, this);
262
+ });
263
+ this.screenShareManager.on("deviceReady", (event, state) => {
264
+ this.stateToSnapshot();
265
+ if (!state.videoMedia?.stream)
266
+ throw Error("Invalid screen share state");
267
+ this.emit("deviceReady", {
268
+ trackType: event.type,
269
+ stream: state.videoMedia.stream,
270
+ mediaDeviceType: "displayMedia",
271
+ }, this);
272
+ });
273
+ this.screenShareManager.on("error", (a) => {
274
+ this.stateToSnapshot();
275
+ this.emit("error", a, this);
276
+ });
277
+ this.tsClient?.on("targetTrackEncodingRequested", (event) => {
278
+ this.stateToSnapshot();
279
+ this.emit("targetTrackEncodingRequested", event, this);
280
+ });
281
+ this.tsClient?.on("localTrackAdded", (event) => {
282
+ this.stateToSnapshot();
283
+ this.emit("localTrackAdded", event, this);
284
+ });
285
+ this.tsClient?.on("localTrackRemoved", (event) => {
286
+ this.stateToSnapshot();
287
+ this.emit("localTrackRemoved", event, this);
288
+ });
289
+ this.tsClient?.on("localTrackReplaced", (event) => {
290
+ this.stateToSnapshot();
291
+ this.emit("localTrackReplaced", event, this);
292
+ });
293
+ this.tsClient?.on("localTrackMuted", (event) => {
294
+ this.stateToSnapshot();
295
+ this.emit("localTrackMuted", event, this);
296
+ });
297
+ this.tsClient?.on("localTrackUnmuted", (event) => {
298
+ this.stateToSnapshot();
299
+ this.emit("localTrackUnmuted", event, this);
300
+ });
301
+ this.tsClient?.on("localTrackBandwidthSet", (event) => {
302
+ this.stateToSnapshot();
303
+ this.emit("localTrackBandwidthSet", event, this);
304
+ });
305
+ this.tsClient?.on("localTrackEncodingBandwidthSet", (event) => {
306
+ this.stateToSnapshot();
307
+ this.emit("localTrackEncodingBandwidthSet", event, this);
308
+ });
309
+ this.tsClient?.on("localTrackEncodingEnabled", (event) => {
310
+ this.stateToSnapshot();
311
+ this.emit("localTrackEncodingEnabled", event, this);
312
+ });
313
+ this.tsClient?.on("localTrackEncodingDisabled", (event) => {
314
+ this.stateToSnapshot();
315
+ this.emit("localTrackEncodingDisabled", event, this);
316
+ });
317
+ this.tsClient?.on("localPeerMetadataChanged", (event) => {
318
+ this.stateToSnapshot();
319
+ this.emit("localPeerMetadataChanged", event, this);
320
+ });
321
+ this.tsClient?.on("localTrackMetadataChanged", (event) => {
322
+ this.stateToSnapshot();
323
+ this.emit("localTrackMetadataChanged", event, this);
324
+ });
325
+ this.tsClient?.on("disconnectRequested", (event) => {
326
+ this.stateToSnapshot();
327
+ this.emit("disconnectRequested", event, this);
328
+ });
329
+ }
330
+ setScreenManagerConfig(config) {
331
+ this.screenShareManager?.setConfig(config);
332
+ }
333
+ setDeviceManagerConfig(config) {
334
+ this.videoDeviceManager.setConfig(config.storage, config.trackConstraints);
335
+ this.audioDeviceManager.setConfig(config.storage, config.trackConstraints);
336
+ }
337
+ trackContextToTrack(track) {
338
+ return {
339
+ rawMetadata: track.rawMetadata,
340
+ metadata: track.metadata,
341
+ trackId: track.trackId,
342
+ stream: track.stream,
343
+ simulcastConfig: track.simulcastConfig || null,
344
+ encoding: track.encoding || null,
345
+ vadStatus: track.vadStatus,
346
+ track: track.track,
347
+ metadataParsingError: track.metadataParsingError,
348
+ };
349
+ }
350
+ connect(config) {
351
+ this.status = "connecting";
352
+ this.tsClient.connect(config);
353
+ }
354
+ disconnect() {
355
+ this.status = null;
356
+ this.tsClient.disconnect();
357
+ }
358
+ addTrack(track, trackMetadata, simulcastConfig = { enabled: false, activeEncodings: [], disabledEncodings: [] }, maxBandwidth = 0) {
359
+ return this.tsClient.addTrack(track, trackMetadata, simulcastConfig, maxBandwidth);
360
+ }
361
+ removeTrack(trackId) {
362
+ return this.tsClient.removeTrack(trackId);
363
+ }
364
+ replaceTrack(trackId, newTrack, newTrackMetadata) {
365
+ return this.tsClient.replaceTrack(trackId, newTrack, newTrackMetadata);
366
+ }
367
+ getStatistics(selector) {
368
+ return this.tsClient.getStatistics(selector);
369
+ }
370
+ getBandwidthEstimation() {
371
+ return this.tsClient.getBandwidthEstimation();
372
+ }
373
+ setTrackBandwidth(trackId, bandwidth) {
374
+ return this.tsClient.setTrackBandwidth(trackId, bandwidth);
375
+ }
376
+ setEncodingBandwidth(trackId, rid, bandwidth) {
377
+ return this.tsClient.setEncodingBandwidth(trackId, rid, bandwidth);
378
+ }
379
+ setTargetTrackEncoding(trackId, encoding) {
380
+ return this.tsClient.setTargetTrackEncoding(trackId, encoding);
381
+ }
382
+ enableTrackEncoding(trackId, encoding) {
383
+ return this.tsClient.enableTrackEncoding(trackId, encoding);
384
+ }
385
+ disableTrackEncoding(trackId, encoding) {
386
+ return this.tsClient.disableTrackEncoding(trackId, encoding);
387
+ }
388
+ updatePeerMetadata = (peerMetadata) => {
389
+ this.tsClient.updatePeerMetadata(peerMetadata);
390
+ };
391
+ updateTrackMetadata = (trackId, trackMetadata) => {
392
+ this.tsClient.updateTrackMetadata(trackId, trackMetadata);
393
+ };
394
+ isReconnecting = () => {
395
+ return this.tsClient.isReconnecting();
396
+ };
397
+ initializeDevices = async (config) => {
398
+ const constraints = {
399
+ video: this.videoDeviceManager.getConstraints(config?.videoTrackConstraints),
400
+ audio: this.audioDeviceManager.getConstraints(config?.audioTrackConstraints),
401
+ };
402
+ const previousDevices = {
403
+ video: this.videoDeviceManager.getLastDevice(),
404
+ audio: this.audioDeviceManager.getLastDevice(),
405
+ };
406
+ let [stream, deviceErrors] = await getAvailableMedia(constraints);
407
+ const devices = await navigator.mediaDevices.enumerateDevices();
408
+ const videoDevices = devices.filter(({ kind }) => kind === "videoinput");
409
+ const audioDevices = devices.filter(({ kind }) => kind === "audioinput");
410
+ if (stream) {
411
+ [stream, deviceErrors] = await getCorrectedResult(stream, deviceErrors, devices, constraints, previousDevices);
412
+ }
413
+ const state = {
414
+ video: this.videoDeviceManager.initialize(stream, stream?.getVideoTracks()?.[0] ?? null, videoDevices, !!constraints.video, deviceErrors.video),
415
+ audio: this.audioDeviceManager.initialize(stream, stream?.getAudioTracks()?.[0] ?? null, audioDevices, !!constraints.audio, deviceErrors.audio),
416
+ };
417
+ this.stateToSnapshot();
418
+ this.emit("managerInitialized", state, this);
419
+ };
420
+ startDevices = async (config) => {
421
+ this.videoDeviceManager.start(config?.videoDeviceId);
422
+ this.audioDeviceManager.start(config?.audioDeviceId);
423
+ };
424
+ // In most cases, the track is identified by its remote track ID.
425
+ // This ID comes from the ts-client `addTrack` method.
426
+ // However, we don't have that ID before the `addTrack` method returns it.
427
+ //
428
+ // The `addTrack` method emits the `localTrackAdded` event.
429
+ // This event will refresh the internal state of this object.
430
+ // However, in that event handler, we don't yet have the remote track ID.
431
+ // Therefore, for that brief moment, we will use the local track ID from the MediaStreamTrack object to identify the track.
432
+ getRemoteTrack = (remoteOrLocalTrackId) => {
433
+ if (!remoteOrLocalTrackId)
434
+ return null;
435
+ const tracks = this.tsClient?.getLocalPeer()?.tracks;
436
+ if (!tracks)
437
+ return null;
438
+ const trackByRemoteId = tracks?.get(remoteOrLocalTrackId);
439
+ if (trackByRemoteId)
440
+ return this.trackContextToTrack(trackByRemoteId);
441
+ const trackByLocalId = [...tracks.values()].find((track) => track.track?.id === remoteOrLocalTrackId);
442
+ return trackByLocalId ? this.trackContextToTrack(trackByLocalId) : null;
443
+ };
444
+ stateToSnapshot() {
445
+ const screenShareManager = this.screenShareManager.getSnapshot();
446
+ const deviceManagerSnapshot = {
447
+ audio: this?.audioDeviceManager?.deviceState,
448
+ video: this?.videoDeviceManager?.deviceState,
449
+ };
450
+ const localEndpoint = this.tsClient.getLocalPeer();
451
+ const localTracks = {};
452
+ (localEndpoint?.tracks || new Map()).forEach((track) => {
453
+ localTracks[track.trackId] = this.trackContextToTrack(track);
454
+ });
455
+ const broadcastedVideoTrack = this.videoTrackManager.getCurrentTrack();
456
+ const broadcastedAudioTrack = this.audioTrackManager.getCurrentTrack();
457
+ const screenShareVideoTrack = this.getRemoteTrack(this.currentScreenShareTrackId);
458
+ const devices = {
459
+ camera: {
460
+ broadcast: broadcastedVideoTrack ?? null,
461
+ status: deviceManagerSnapshot?.video?.devicesStatus || null,
462
+ stream: deviceManagerSnapshot?.video.media?.stream || null,
463
+ track: deviceManagerSnapshot?.video.media?.track || null,
464
+ enabled: deviceManagerSnapshot?.video.media?.enabled || false,
465
+ deviceInfo: deviceManagerSnapshot?.video.media?.deviceInfo || null,
466
+ mediaStatus: deviceManagerSnapshot?.video.mediaStatus || null,
467
+ error: deviceManagerSnapshot?.video?.error || null,
468
+ devices: deviceManagerSnapshot?.video?.devices || null,
469
+ },
470
+ microphone: {
471
+ broadcast: broadcastedAudioTrack ?? null,
472
+ status: deviceManagerSnapshot?.audio?.devicesStatus || null,
473
+ stream: deviceManagerSnapshot?.audio.media?.stream || null,
474
+ track: deviceManagerSnapshot?.audio.media?.track || null,
475
+ enabled: deviceManagerSnapshot?.audio.media?.enabled || false,
476
+ deviceInfo: deviceManagerSnapshot?.audio.media?.deviceInfo || null,
477
+ mediaStatus: deviceManagerSnapshot?.video.mediaStatus || null,
478
+ error: deviceManagerSnapshot?.audio?.error || null,
479
+ devices: deviceManagerSnapshot?.audio?.devices || null,
480
+ },
481
+ screenShare: {
482
+ cleanup: async () => {
483
+ this?.screenShareManager?.stop("video");
484
+ },
485
+ disableTrack: () => {
486
+ this.screenShareManager?.setEnable("video", false);
487
+ },
488
+ enableTrack: () => {
489
+ this.screenShareManager?.setEnable("video", true);
490
+ },
491
+ initialize: async (config) => {
492
+ await this.screenShareManager?.start(config);
493
+ },
494
+ startStreaming: async (trackMetadata, maxBandwidth) => {
495
+ const media = this.screenShareManager?.getSnapshot().videoMedia;
496
+ if (!media || !media.stream || !media.track)
497
+ throw Error("Device is unavailable");
498
+ if (this.currentScreenShareTrackId)
499
+ throw Error("Screen share track already added");
500
+ const track = this.getRemoteTrack(media.track.id);
501
+ if (track)
502
+ return track.trackId;
503
+ // see `getRemoteTrack()` explanation
504
+ this.currentScreenShareTrackId = media.track.id;
505
+ const trackId = await this.tsClient.addTrack(media.track, trackMetadata, undefined, maxBandwidth);
506
+ this.currentScreenShareTrackId = trackId;
507
+ return trackId;
508
+ },
509
+ stopStreaming: () => {
510
+ if (!this.currentScreenShareTrackId)
511
+ throw Error("There is no screen share track id");
512
+ const prevTrack = this.getRemoteTrack(this.currentScreenShareTrackId);
513
+ if (!prevTrack)
514
+ throw Error("There is no screen share video track");
515
+ this.currentScreenShareTrackId = null;
516
+ return this.tsClient.removeTrack(prevTrack.trackId);
517
+ },
518
+ broadcast: screenShareVideoTrack ?? null,
519
+ status: screenShareManager?.status || null,
520
+ mediaStatus: null,
521
+ stream: screenShareManager?.videoMedia?.stream || null,
522
+ track: screenShareManager?.videoMedia?.track || null,
523
+ enabled: screenShareManager?.videoMedia?.enabled || false,
524
+ error: screenShareManager?.error || null,
525
+ },
526
+ };
527
+ if (!this.tsClient["webrtc"]) {
528
+ this.media = deviceManagerSnapshot || null;
529
+ this.peersTracks = {};
530
+ this.componentsTracks = {};
531
+ this.devices = devices;
532
+ this.peers = {};
533
+ this.components = {};
534
+ this.local = null;
535
+ this.bandwidthEstimation = 0n;
536
+ return;
537
+ }
538
+ const peers = {};
539
+ const components = {};
540
+ const peersTracks = {};
541
+ const componentTracks = {};
542
+ Object.values(this.tsClient.getRemotePeers()).forEach((remotePeer) => {
543
+ const tracks = {};
544
+ remotePeer.tracks.forEach((track) => {
545
+ const mappedTrack = this.trackContextToTrack(track);
546
+ tracks[track.trackId] = mappedTrack;
547
+ peersTracks[track.trackId] = { ...mappedTrack, origin: remotePeer };
548
+ });
549
+ peers[remotePeer.id] = {
550
+ rawMetadata: remotePeer.rawMetadata,
551
+ metadata: remotePeer.metadata,
552
+ metadataParsingError: remotePeer.metadataParsingError,
553
+ id: remotePeer.id,
554
+ tracks,
555
+ };
556
+ });
557
+ Object.values(this.tsClient.getRemoteComponents()).forEach((remotePeer) => {
558
+ const tracks = {};
559
+ remotePeer.tracks.forEach((track) => {
560
+ const mappedTrack = this.trackContextToTrack(track);
561
+ tracks[track.trackId] = mappedTrack;
562
+ componentTracks[track.trackId] = { ...mappedTrack, origin: remotePeer };
563
+ });
564
+ components[remotePeer.id] = {
565
+ rawMetadata: remotePeer.rawMetadata,
566
+ metadata: remotePeer.metadata,
567
+ metadataParsingError: remotePeer.metadataParsingError,
568
+ id: remotePeer.id,
569
+ tracks,
570
+ };
571
+ });
572
+ this.peersTracks = peersTracks;
573
+ this.componentsTracks = componentTracks;
574
+ this.media = deviceManagerSnapshot || null;
575
+ this.local = localEndpoint
576
+ ? {
577
+ id: localEndpoint.id,
578
+ metadata: localEndpoint.metadata,
579
+ metadataParsingError: localEndpoint.metadataParsingError,
580
+ rawMetadata: localEndpoint.rawMetadata,
581
+ tracks: localTracks, // to record
582
+ }
583
+ : null;
584
+ this.peers = peers;
585
+ this.components = components;
586
+ this.bandwidthEstimation = this.tsClient.getBandwidthEstimation();
587
+ this.devices = devices;
588
+ }
589
+ }
@@ -0,0 +1,49 @@
1
+ import type { DeviceManagerConfig, DeviceState, GenericMediaManager, StorageConfig, DeviceError } from "./types";
2
+ import type TypedEmitter from "typed-emitter";
3
+ export type DeviceManagerEvents = {
4
+ managerStarted: (event: DeviceState & {
5
+ constraints: MediaTrackConstraints | undefined;
6
+ }, state: DeviceState) => void;
7
+ managerInitialized: (state: DeviceState) => void;
8
+ devicesStarted: (event: {
9
+ restarting: boolean;
10
+ constraints?: string | boolean;
11
+ }, state: DeviceState) => void;
12
+ deviceReady: (event: {
13
+ stream: MediaStream;
14
+ }, state: DeviceState) => void;
15
+ devicesReady: (event: DeviceState & {
16
+ restarted: boolean;
17
+ }, state: DeviceState) => void;
18
+ deviceStopped: (state: DeviceState) => void;
19
+ deviceEnabled: (state: DeviceState) => void;
20
+ deviceDisabled: (state: DeviceState) => void;
21
+ error: (event: any, state: DeviceState) => void;
22
+ };
23
+ export type DeviceManagerStatus = "uninitialized" | "initializing" | "initialized" | "error";
24
+ declare const DeviceManager_base: new () => TypedEmitter<DeviceManagerEvents>;
25
+ export declare class DeviceManager extends DeviceManager_base implements GenericMediaManager {
26
+ private constraints;
27
+ private storageConfig;
28
+ private status;
29
+ private deviceType;
30
+ deviceState: DeviceState;
31
+ constructor(deviceType: "audio" | "video", defaultConfig?: DeviceManagerConfig);
32
+ private createStorageConfig;
33
+ getStatus(): DeviceManagerStatus;
34
+ getConstraints(currentConstraints: boolean | MediaTrackConstraints | undefined): MediaTrackConstraints | undefined;
35
+ getMedia: () => import("./types").Media | null;
36
+ getTracks: () => MediaStreamTrack[];
37
+ initialize: (stream: MediaStream | null, track: MediaStreamTrack | null, devices: MediaDeviceInfo[], requestedMedia: boolean, error?: DeviceError | null) => DeviceState;
38
+ private setupOnEndedCallback;
39
+ private onTrackEnded;
40
+ getLastDevice(): MediaDeviceInfo | null;
41
+ private saveLastDevice;
42
+ start(deviceId?: string | boolean): Promise<void>;
43
+ stop(): Promise<void>;
44
+ disable(): void;
45
+ enable(): void;
46
+ setConfig(storage?: boolean | StorageConfig, constraints?: boolean | MediaTrackConstraints): void;
47
+ }
48
+ export {};
49
+ //# sourceMappingURL=DeviceManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeviceManager.d.ts","sourceRoot":"","sources":["../src/DeviceManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,WAAW,EAAE,mBAAmB,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAKjH,OAAO,KAAK,YAAY,MAAM,eAAe,CAAC;AAI9C,MAAM,MAAM,mBAAmB,GAAG;IAChC,cAAc,EAAE,CACd,KAAK,EAAE,WAAW,GAAG;QACnB,WAAW,EAAE,qBAAqB,GAAG,SAAS,CAAC;KAChD,EACD,KAAK,EAAE,WAAW,KACf,IAAI,CAAC;IACV,kBAAkB,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;IACjD,cAAc,EAAE,CAAC,KAAK,EAAE;QAAE,UAAU,EAAE,OAAO,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,EAAE,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;IAC7G,WAAW,EAAE,CAAC,KAAK,EAAE;QAAE,MAAM,EAAE,WAAW,CAAA;KAAE,EAAE,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;IAC1E,YAAY,EAAE,CAAC,KAAK,EAAE,WAAW,GAAG;QAAE,SAAS,EAAE,OAAO,CAAA;KAAE,EAAE,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;IACxF,aAAa,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;IAC5C,aAAa,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;IAC5C,cAAc,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;IAE7C,KAAK,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;CACjD,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG,eAAe,GAAG,cAAc,GAAG,aAAa,GAAG,OAAO,CAAC;4CAGxD,YAAY,CAAC,mBAAmB,CAAC;AADtE,qBAAa,aACX,SAAQ,kBACR,YAAW,mBAAmB;IAE9B,OAAO,CAAC,WAAW,CAAoC;IACvD,OAAO,CAAC,aAAa,CAAuB;IAE5C,OAAO,CAAC,MAAM,CAAwC;IACtD,OAAO,CAAC,UAAU,CAAoB;IAE/B,WAAW,EAAE,WAAW,CAM7B;gBAEU,UAAU,EAAE,OAAO,GAAG,OAAO,EAAE,aAAa,CAAC,EAAE,mBAAmB;IAS9E,OAAO,CAAC,mBAAmB;IAMpB,SAAS,IAAI,mBAAmB;IAIhC,cAAc,CACnB,kBAAkB,EAAE,OAAO,GAAG,qBAAqB,GAAG,SAAS,GAC9D,qBAAqB,GAAG,SAAS;IAO7B,QAAQ,uCAAgC;IAExC,SAAS,2BAKd;IAEK,UAAU,WACP,WAAW,GAAG,IAAI,SACnB,gBAAgB,GAAG,IAAI,WACrB,eAAe,EAAE,kBACV,OAAO,UAChB,WAAW,GAAG,IAAI,iBAWzB;IAEF,OAAO,CAAC,oBAAoB;IAS5B,OAAO,CAAC,YAAY,CAIlB;IAEK,aAAa,IAAI,eAAe,GAAG,IAAI;IAI9C,OAAO,CAAC,cAAc;IAMT,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO;IA4EjC,IAAI;IAOV,OAAO;IAWP,MAAM;IAWN,SAAS,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,aAAa,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,qBAAqB;CAIlG"}