livekit-client 0.17.3 → 0.17.6-rc1

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 (155) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +26 -20
  3. package/dist/api/SignalClient.d.ts +1 -0
  4. package/dist/connect.d.ts +2 -0
  5. package/dist/index.d.ts +2 -2
  6. package/dist/livekit-client.esm.js +17344 -0
  7. package/dist/livekit-client.esm.js.map +1 -0
  8. package/dist/livekit-client.umd.js +17388 -0
  9. package/dist/livekit-client.umd.js.map +1 -0
  10. package/dist/logger.d.ts +22 -11
  11. package/dist/options.d.ts +4 -2
  12. package/dist/proto/google/protobuf/timestamp.d.ts +12 -2
  13. package/dist/proto/livekit_models.d.ts +524 -17
  14. package/dist/proto/livekit_rtc.d.ts +3449 -31
  15. package/dist/room/DeviceManager.d.ts +1 -1
  16. package/dist/room/RTCEngine.d.ts +1 -1
  17. package/dist/room/Room.d.ts +2 -2
  18. package/dist/room/events.d.ts +1 -1
  19. package/dist/room/participant/LocalParticipant.d.ts +9 -5
  20. package/dist/room/participant/RemoteParticipant.d.ts +9 -0
  21. package/dist/room/track/RemoteAudioTrack.d.ts +11 -0
  22. package/dist/room/track/options.d.ts +1 -1
  23. package/dist/test/mocks.d.ts +11 -0
  24. package/dist/version.d.ts +1 -1
  25. package/package.json +41 -16
  26. package/.eslintrc.js +0 -17
  27. package/.gitmodules +0 -3
  28. package/dist/api/RequestQueue.js +0 -61
  29. package/dist/api/RequestQueue.js.map +0 -1
  30. package/dist/api/SignalClient.js +0 -428
  31. package/dist/api/SignalClient.js.map +0 -1
  32. package/dist/connect.js +0 -130
  33. package/dist/connect.js.map +0 -1
  34. package/dist/index.js +0 -71
  35. package/dist/index.js.map +0 -1
  36. package/dist/logger.js +0 -24
  37. package/dist/logger.js.map +0 -1
  38. package/dist/options.js +0 -3
  39. package/dist/options.js.map +0 -1
  40. package/dist/proto/google/protobuf/timestamp.js +0 -93
  41. package/dist/proto/google/protobuf/timestamp.js.map +0 -1
  42. package/dist/proto/livekit_models.js +0 -2688
  43. package/dist/proto/livekit_models.js.map +0 -1
  44. package/dist/proto/livekit_rtc.js +0 -2995
  45. package/dist/proto/livekit_rtc.js.map +0 -1
  46. package/dist/room/DeviceManager.js +0 -62
  47. package/dist/room/DeviceManager.js.map +0 -1
  48. package/dist/room/PCTransport.js +0 -91
  49. package/dist/room/PCTransport.js.map +0 -1
  50. package/dist/room/RTCEngine.js +0 -562
  51. package/dist/room/RTCEngine.js.map +0 -1
  52. package/dist/room/Room.js +0 -759
  53. package/dist/room/Room.js.map +0 -1
  54. package/dist/room/errors.js +0 -68
  55. package/dist/room/errors.js.map +0 -1
  56. package/dist/room/events.js +0 -385
  57. package/dist/room/events.js.map +0 -1
  58. package/dist/room/participant/LocalParticipant.js +0 -647
  59. package/dist/room/participant/LocalParticipant.js.map +0 -1
  60. package/dist/room/participant/Participant.js +0 -189
  61. package/dist/room/participant/Participant.js.map +0 -1
  62. package/dist/room/participant/ParticipantTrackPermission.js +0 -16
  63. package/dist/room/participant/ParticipantTrackPermission.js.map +0 -1
  64. package/dist/room/participant/RemoteParticipant.js +0 -194
  65. package/dist/room/participant/RemoteParticipant.js.map +0 -1
  66. package/dist/room/participant/publishUtils.js +0 -189
  67. package/dist/room/participant/publishUtils.js.map +0 -1
  68. package/dist/room/participant/publishUtils.test.d.ts +0 -1
  69. package/dist/room/participant/publishUtils.test.js +0 -118
  70. package/dist/room/participant/publishUtils.test.js.map +0 -1
  71. package/dist/room/stats.js +0 -26
  72. package/dist/room/stats.js.map +0 -1
  73. package/dist/room/track/LocalAudioTrack.js +0 -153
  74. package/dist/room/track/LocalAudioTrack.js.map +0 -1
  75. package/dist/room/track/LocalTrack.js +0 -158
  76. package/dist/room/track/LocalTrack.js.map +0 -1
  77. package/dist/room/track/LocalTrackPublication.js +0 -64
  78. package/dist/room/track/LocalTrackPublication.js.map +0 -1
  79. package/dist/room/track/LocalVideoTrack.js +0 -297
  80. package/dist/room/track/LocalVideoTrack.js.map +0 -1
  81. package/dist/room/track/LocalVideoTrack.test.d.ts +0 -1
  82. package/dist/room/track/LocalVideoTrack.test.js +0 -68
  83. package/dist/room/track/LocalVideoTrack.test.js.map +0 -1
  84. package/dist/room/track/RemoteAudioTrack.js +0 -64
  85. package/dist/room/track/RemoteAudioTrack.js.map +0 -1
  86. package/dist/room/track/RemoteTrack.js +0 -49
  87. package/dist/room/track/RemoteTrack.js.map +0 -1
  88. package/dist/room/track/RemoteTrackPublication.js +0 -178
  89. package/dist/room/track/RemoteTrackPublication.js.map +0 -1
  90. package/dist/room/track/RemoteVideoTrack.js +0 -201
  91. package/dist/room/track/RemoteVideoTrack.js.map +0 -1
  92. package/dist/room/track/Track.js +0 -276
  93. package/dist/room/track/Track.js.map +0 -1
  94. package/dist/room/track/TrackPublication.js +0 -92
  95. package/dist/room/track/TrackPublication.js.map +0 -1
  96. package/dist/room/track/create.js +0 -131
  97. package/dist/room/track/create.js.map +0 -1
  98. package/dist/room/track/defaults.js +0 -21
  99. package/dist/room/track/defaults.js.map +0 -1
  100. package/dist/room/track/options.js +0 -100
  101. package/dist/room/track/options.js.map +0 -1
  102. package/dist/room/track/types.js +0 -3
  103. package/dist/room/track/types.js.map +0 -1
  104. package/dist/room/track/utils.js +0 -113
  105. package/dist/room/track/utils.js.map +0 -1
  106. package/dist/room/track/utils.test.d.ts +0 -1
  107. package/dist/room/track/utils.test.js +0 -85
  108. package/dist/room/track/utils.test.js.map +0 -1
  109. package/dist/room/utils.js +0 -79
  110. package/dist/room/utils.js.map +0 -1
  111. package/dist/version.js +0 -6
  112. package/dist/version.js.map +0 -1
  113. package/jest.config.js +0 -6
  114. package/src/api/RequestQueue.ts +0 -53
  115. package/src/api/SignalClient.ts +0 -499
  116. package/src/connect.ts +0 -100
  117. package/src/index.ts +0 -47
  118. package/src/logger.ts +0 -22
  119. package/src/options.ts +0 -149
  120. package/src/proto/google/protobuf/timestamp.ts +0 -222
  121. package/src/proto/livekit_models.ts +0 -3019
  122. package/src/proto/livekit_rtc.ts +0 -3677
  123. package/src/room/DeviceManager.ts +0 -57
  124. package/src/room/PCTransport.ts +0 -86
  125. package/src/room/RTCEngine.ts +0 -652
  126. package/src/room/Room.ts +0 -943
  127. package/src/room/errors.ts +0 -65
  128. package/src/room/events.ts +0 -424
  129. package/src/room/participant/LocalParticipant.ts +0 -734
  130. package/src/room/participant/Participant.ts +0 -269
  131. package/src/room/participant/ParticipantTrackPermission.ts +0 -32
  132. package/src/room/participant/RemoteParticipant.ts +0 -243
  133. package/src/room/participant/publishUtils.test.ts +0 -145
  134. package/src/room/participant/publishUtils.ts +0 -225
  135. package/src/room/stats.ts +0 -130
  136. package/src/room/track/LocalAudioTrack.ts +0 -137
  137. package/src/room/track/LocalTrack.ts +0 -161
  138. package/src/room/track/LocalTrackPublication.ts +0 -66
  139. package/src/room/track/LocalVideoTrack.test.ts +0 -70
  140. package/src/room/track/LocalVideoTrack.ts +0 -293
  141. package/src/room/track/RemoteAudioTrack.ts +0 -58
  142. package/src/room/track/RemoteTrack.ts +0 -62
  143. package/src/room/track/RemoteTrackPublication.ts +0 -198
  144. package/src/room/track/RemoteVideoTrack.ts +0 -235
  145. package/src/room/track/Track.ts +0 -337
  146. package/src/room/track/TrackPublication.ts +0 -120
  147. package/src/room/track/create.ts +0 -121
  148. package/src/room/track/defaults.ts +0 -23
  149. package/src/room/track/options.ts +0 -281
  150. package/src/room/track/types.ts +0 -20
  151. package/src/room/track/utils.test.ts +0 -93
  152. package/src/room/track/utils.ts +0 -115
  153. package/src/room/utils.ts +0 -70
  154. package/src/version.ts +0 -2
  155. package/tsconfig.eslint.json +0 -11
@@ -1,734 +0,0 @@
1
- import log from '../../logger';
2
- import { RoomOptions } from '../../options';
3
- import {
4
- DataPacket, DataPacket_Kind, ParticipantPermission,
5
- } from '../../proto/livekit_models';
6
- import {
7
- AddTrackRequest, DataChannelInfo,
8
- SubscribedQualityUpdate, TrackPublishedResponse, TrackUnpublishedResponse,
9
- } from '../../proto/livekit_rtc';
10
- import {
11
- TrackInvalidError,
12
- UnexpectedConnectionState,
13
- } from '../errors';
14
- import { ParticipantEvent, TrackEvent } from '../events';
15
- import RTCEngine from '../RTCEngine';
16
- import LocalAudioTrack from '../track/LocalAudioTrack';
17
- import LocalTrack from '../track/LocalTrack';
18
- import LocalTrackPublication from '../track/LocalTrackPublication';
19
- import LocalVideoTrack, { videoLayersFromEncodings } from '../track/LocalVideoTrack';
20
- import {
21
- CreateLocalTracksOptions,
22
- ScreenShareCaptureOptions,
23
- ScreenSharePresets,
24
- TrackPublishOptions, VideoCodec,
25
- } from '../track/options';
26
- import { Track } from '../track/Track';
27
- import { constraintsForOptions, mergeDefaultOptions } from '../track/utils';
28
- import { isFireFox } from '../utils';
29
- import Participant from './Participant';
30
- import { ParticipantTrackPermission, trackPermissionToProto } from './ParticipantTrackPermission';
31
- import { computeVideoEncodings, mediaTrackToLocalTrack } from './publishUtils';
32
- import RemoteParticipant from './RemoteParticipant';
33
-
34
- export default class LocalParticipant extends Participant {
35
- audioTracks: Map<string, LocalTrackPublication>;
36
-
37
- videoTracks: Map<string, LocalTrackPublication>;
38
-
39
- /** map of track sid => all published tracks */
40
- tracks: Map<string, LocalTrackPublication>;
41
-
42
- private pendingPublishing = new Set<Track.Source>();
43
-
44
- private cameraError: Error | undefined;
45
-
46
- private microphoneError: Error | undefined;
47
-
48
- private engine: RTCEngine;
49
-
50
- // keep a pointer to room options
51
- private roomOptions?: RoomOptions;
52
-
53
- /** @internal */
54
- constructor(sid: string, identity: string, engine: RTCEngine, options: RoomOptions) {
55
- super(sid, identity);
56
- this.audioTracks = new Map();
57
- this.videoTracks = new Map();
58
- this.tracks = new Map();
59
- this.engine = engine;
60
- this.roomOptions = options;
61
-
62
- this.engine.client.onRemoteMuteChanged = (trackSid: string, muted: boolean) => {
63
- const pub = this.tracks.get(trackSid);
64
- if (!pub || !pub.track) {
65
- return;
66
- }
67
- if (muted) {
68
- pub.mute();
69
- } else {
70
- pub.unmute();
71
- }
72
- };
73
-
74
- this.engine.client.onSubscribedQualityUpdate = this.handleSubscribedQualityUpdate;
75
-
76
- this.engine.client.onLocalTrackUnpublished = this.handleLocalTrackUnpublished;
77
- }
78
-
79
- get lastCameraError(): Error | undefined {
80
- return this.cameraError;
81
- }
82
-
83
- get lastMicrophoneError(): Error | undefined {
84
- return this.microphoneError;
85
- }
86
-
87
- getTrack(source: Track.Source): LocalTrackPublication | undefined {
88
- const track = super.getTrack(source);
89
- if (track) {
90
- return track as LocalTrackPublication;
91
- }
92
- }
93
-
94
- getTrackByName(name: string): LocalTrackPublication | undefined {
95
- const track = super.getTrackByName(name);
96
- if (track) {
97
- return track as LocalTrackPublication;
98
- }
99
- }
100
-
101
- /**
102
- * Enable or disable a participant's camera track.
103
- *
104
- * If a track has already published, it'll mute or unmute the track.
105
- */
106
- setCameraEnabled(enabled: boolean): Promise<void> {
107
- return this.setTrackEnabled(Track.Source.Camera, enabled);
108
- }
109
-
110
- /**
111
- * Enable or disable a participant's microphone track.
112
- *
113
- * If a track has already published, it'll mute or unmute the track.
114
- */
115
- setMicrophoneEnabled(enabled: boolean): Promise<void> {
116
- return this.setTrackEnabled(Track.Source.Microphone, enabled);
117
- }
118
-
119
- /**
120
- * Start or stop sharing a participant's screen
121
- */
122
- setScreenShareEnabled(enabled: boolean): Promise<void> {
123
- return this.setTrackEnabled(Track.Source.ScreenShare, enabled);
124
- }
125
-
126
- /** @internal */
127
- setPermissions(permissions: ParticipantPermission): boolean {
128
- const prevPermissions = this.permissions;
129
- const changed = super.setPermissions(permissions);
130
- if (changed && prevPermissions) {
131
- this.emit(ParticipantEvent.ParticipantPermissionsChanged, prevPermissions);
132
- }
133
- return changed;
134
- }
135
-
136
- /**
137
- * Enable or disable publishing for a track by source. This serves as a simple
138
- * way to manage the common tracks (camera, mic, or screen share)
139
- */
140
- private async setTrackEnabled(source: Track.Source, enabled: boolean): Promise<void> {
141
- log.debug('setTrackEnabled', source, enabled);
142
- const track = this.getTrack(source);
143
- if (enabled) {
144
- if (track) {
145
- await track.unmute();
146
- } else {
147
- let localTrack: LocalTrack | undefined;
148
- if (this.pendingPublishing.has(source)) {
149
- log.info('skipping duplicate published source', source);
150
- // no-op it's already been requested
151
- return;
152
- }
153
- this.pendingPublishing.add(source);
154
- try {
155
- switch (source) {
156
- case Track.Source.Camera:
157
- [localTrack] = await this.createTracks({
158
- video: true,
159
- });
160
- break;
161
- case Track.Source.Microphone:
162
- [localTrack] = await this.createTracks({
163
- audio: true,
164
- });
165
- break;
166
- case Track.Source.ScreenShare:
167
- [localTrack] = await this.createScreenTracks({ audio: false });
168
- break;
169
- default:
170
- throw new TrackInvalidError(source);
171
- }
172
-
173
- await this.publishTrack(localTrack);
174
- } catch (e) {
175
- if (e instanceof Error && !(e instanceof TrackInvalidError)) {
176
- this.emit(ParticipantEvent.MediaDevicesError, e);
177
- }
178
- throw e;
179
- } finally {
180
- this.pendingPublishing.delete(source);
181
- }
182
- }
183
- } else if (track && track.track) {
184
- // screenshare cannot be muted, unpublish instead
185
- if (source === Track.Source.ScreenShare) {
186
- this.unpublishTrack(track.track);
187
- } else {
188
- await track.mute();
189
- }
190
- }
191
- }
192
-
193
- /**
194
- * Publish both camera and microphone at the same time. This is useful for
195
- * displaying a single Permission Dialog box to the end user.
196
- */
197
- async enableCameraAndMicrophone() {
198
- if (this.pendingPublishing.has(Track.Source.Camera)
199
- || this.pendingPublishing.has(Track.Source.Microphone)) {
200
- // no-op it's already been requested
201
- return;
202
- }
203
-
204
- this.pendingPublishing.add(Track.Source.Camera);
205
- this.pendingPublishing.add(Track.Source.Microphone);
206
- try {
207
- const tracks: LocalTrack[] = await this.createTracks({
208
- audio: true,
209
- video: true,
210
- });
211
-
212
- await Promise.all(tracks.map((track) => this.publishTrack(track)));
213
- } finally {
214
- this.pendingPublishing.delete(Track.Source.Camera);
215
- this.pendingPublishing.delete(Track.Source.Microphone);
216
- }
217
- }
218
-
219
- /**
220
- * Create local camera and/or microphone tracks
221
- * @param options
222
- * @returns
223
- */
224
- async createTracks(
225
- options?: CreateLocalTracksOptions,
226
- ): Promise<LocalTrack[]> {
227
- const opts = mergeDefaultOptions(
228
- options,
229
- this.roomOptions?.audioCaptureDefaults,
230
- this.roomOptions?.videoCaptureDefaults,
231
- );
232
-
233
- const constraints = constraintsForOptions(opts);
234
- let stream: MediaStream | undefined;
235
- try {
236
- stream = await navigator.mediaDevices.getUserMedia(
237
- constraints,
238
- );
239
- } catch (err) {
240
- if (err instanceof Error) {
241
- if (constraints.audio) {
242
- this.microphoneError = err;
243
- }
244
- if (constraints.video) {
245
- this.cameraError = err;
246
- }
247
- }
248
-
249
- throw err;
250
- }
251
-
252
- if (constraints.audio) {
253
- this.microphoneError = undefined;
254
- }
255
- if (constraints.video) {
256
- this.cameraError = undefined;
257
- }
258
-
259
- return stream.getTracks().map((mediaStreamTrack) => {
260
- const isAudio = mediaStreamTrack.kind === 'audio';
261
- let trackOptions = isAudio ? options!.audio : options!.video;
262
- if (typeof trackOptions === 'boolean' || !trackOptions) {
263
- trackOptions = {};
264
- }
265
- let trackConstraints: MediaTrackConstraints | undefined;
266
- const conOrBool = isAudio ? constraints.audio : constraints.video;
267
- if (typeof conOrBool !== 'boolean') {
268
- trackConstraints = conOrBool;
269
- }
270
- const track = mediaTrackToLocalTrack(mediaStreamTrack, trackConstraints);
271
- if (track.kind === Track.Kind.Video) {
272
- track.source = Track.Source.Camera;
273
- } else if (track.kind === Track.Kind.Audio) {
274
- track.source = Track.Source.Microphone;
275
- }
276
- track.mediaStream = stream;
277
- return track;
278
- });
279
- }
280
-
281
- /**
282
- * Creates a screen capture tracks with getDisplayMedia().
283
- * A LocalVideoTrack is always created and returned.
284
- * If { audio: true }, and the browser supports audio capture, a LocalAudioTrack is also created.
285
- */
286
- async createScreenTracks(
287
- options?: ScreenShareCaptureOptions,
288
- ): Promise<Array<LocalTrack>> {
289
- if (options === undefined) {
290
- options = {};
291
- }
292
- if (options.resolution === undefined) {
293
- options.resolution = ScreenSharePresets.h1080fps15.resolution;
294
- }
295
-
296
- let videoConstraints: MediaTrackConstraints | boolean = true;
297
- if (options.resolution) {
298
- videoConstraints = {
299
- width: options.resolution.width,
300
- height: options.resolution.height,
301
- frameRate: options.resolution.frameRate,
302
- };
303
- }
304
- // typescript definition is missing getDisplayMedia: https://github.com/microsoft/TypeScript/issues/33232
305
- // @ts-ignore
306
- const stream: MediaStream = await navigator.mediaDevices.getDisplayMedia({
307
- audio: options.audio ?? false,
308
- video: videoConstraints,
309
- });
310
-
311
- const tracks = stream.getVideoTracks();
312
- if (tracks.length === 0) {
313
- throw new TrackInvalidError('no video track found');
314
- }
315
- const screenVideo = new LocalVideoTrack(tracks[0]);
316
- screenVideo.source = Track.Source.ScreenShare;
317
- const localTracks: Array<LocalTrack> = [screenVideo];
318
- if (stream.getAudioTracks().length > 0) {
319
- const screenAudio = new LocalAudioTrack(stream.getAudioTracks()[0]);
320
- screenAudio.source = Track.Source.ScreenShareAudio;
321
- localTracks.push(screenAudio);
322
- }
323
- return localTracks;
324
- }
325
-
326
- /**
327
- * Publish a new track to the room
328
- * @param track
329
- * @param options
330
- */
331
- async publishTrack(
332
- track: LocalTrack | MediaStreamTrack,
333
- options?: TrackPublishOptions,
334
- ): Promise<LocalTrackPublication> {
335
- const opts: TrackPublishOptions = {
336
- ...this.roomOptions?.publishDefaults,
337
- ...options,
338
- };
339
-
340
- // convert raw media track into audio or video track
341
- if (track instanceof MediaStreamTrack) {
342
- switch (track.kind) {
343
- case 'audio':
344
- track = new LocalAudioTrack(track);
345
- break;
346
- case 'video':
347
- track = new LocalVideoTrack(track);
348
- break;
349
- default:
350
- throw new TrackInvalidError(
351
- `unsupported MediaStreamTrack kind ${track.kind}`,
352
- );
353
- }
354
- }
355
-
356
- // is it already published? if so skip
357
- let existingPublication: LocalTrackPublication | undefined;
358
- this.tracks.forEach((publication) => {
359
- if (!publication.track) {
360
- return;
361
- }
362
- if (publication.track === track) {
363
- existingPublication = <LocalTrackPublication>publication;
364
- }
365
- });
366
-
367
- if (existingPublication) return existingPublication;
368
-
369
- if (opts.source) {
370
- track.source = opts.source;
371
- }
372
- if (opts.stopMicTrackOnMute && track instanceof LocalAudioTrack) {
373
- track.stopOnMute = true;
374
- }
375
-
376
- if (track.source === Track.Source.ScreenShare && isFireFox()) {
377
- // Firefox does not work well with simulcasted screen share
378
- // we frequently get no data on layer 0 when enabled
379
- opts.simulcast = false;
380
- }
381
-
382
- // handle track actions
383
- track.on(TrackEvent.Muted, this.onTrackMuted);
384
- track.on(TrackEvent.Unmuted, this.onTrackUnmuted);
385
- track.on(TrackEvent.Ended, this.onTrackUnpublish);
386
-
387
- // create track publication from track
388
- const req = AddTrackRequest.fromPartial({
389
- // get local track id for use during publishing
390
- cid: track.mediaStreamTrack.id,
391
- name: options?.name,
392
- type: Track.kindToProto(track.kind),
393
- muted: track.isMuted,
394
- source: Track.sourceToProto(track.source),
395
- disableDtx: !(opts?.dtx ?? true),
396
- });
397
-
398
- // compute encodings and layers for video
399
- let encodings: RTCRtpEncodingParameters[] | undefined;
400
- if (track.kind === Track.Kind.Video) {
401
- // TODO: support react native, which doesn't expose getSettings
402
- const settings = track.mediaStreamTrack.getSettings();
403
- const width = settings.width ?? track.dimensions?.width;
404
- const height = settings.height ?? track.dimensions?.height;
405
- // width and height should be defined for video
406
- req.width = width ?? 0;
407
- req.height = height ?? 0;
408
- encodings = computeVideoEncodings(
409
- track.source === Track.Source.ScreenShare,
410
- width,
411
- height,
412
- opts,
413
- );
414
- req.layers = videoLayersFromEncodings(req.width, req.height, encodings);
415
- } else if (track.kind === Track.Kind.Audio && opts.audioBitrate) {
416
- encodings = [
417
- {
418
- maxBitrate: opts.audioBitrate,
419
- },
420
- ];
421
- }
422
-
423
- const ti = await this.engine.addTrack(req);
424
- const publication = new LocalTrackPublication(track.kind, ti, track);
425
- track.sid = ti.sid;
426
-
427
- if (!this.engine.publisher) {
428
- throw new UnexpectedConnectionState('publisher is closed');
429
- }
430
- log.debug(`publishing ${track.kind} with encodings`, encodings, ti);
431
- const transceiverInit: RTCRtpTransceiverInit = { direction: 'sendonly' };
432
- if (encodings) {
433
- transceiverInit.sendEncodings = encodings;
434
- }
435
- const transceiver = this.engine.publisher.pc.addTransceiver(
436
- track.mediaStreamTrack, transceiverInit,
437
- );
438
- this.engine.negotiate();
439
-
440
- // store RTPSender
441
- track.sender = transceiver.sender;
442
- if (track instanceof LocalVideoTrack) {
443
- track.startMonitor(this.engine.client);
444
- } else if (track instanceof LocalAudioTrack) {
445
- track.startMonitor();
446
- }
447
-
448
- if (opts.videoCodec) {
449
- this.setPreferredCodec(transceiver, track.kind, opts.videoCodec);
450
- }
451
- this.addTrackPublication(publication);
452
-
453
- // send event for publication
454
- this.emit(ParticipantEvent.LocalTrackPublished, publication);
455
- return publication;
456
- }
457
-
458
- unpublishTrack(
459
- track: LocalTrack | MediaStreamTrack,
460
- stopOnUnpublish?: boolean,
461
- ): LocalTrackPublication | null {
462
- // look through all published tracks to find the right ones
463
- const publication = this.getPublicationForTrack(track);
464
-
465
- log.debug('unpublishTrack', 'unpublishing track', track);
466
-
467
- if (!publication || !publication.track) {
468
- log.warn(
469
- 'unpublishTrack',
470
- 'track was not unpublished because no publication was found',
471
- track,
472
- );
473
- return null;
474
- }
475
-
476
- track = publication.track;
477
-
478
- track.off(TrackEvent.Muted, this.onTrackMuted);
479
- track.off(TrackEvent.Unmuted, this.onTrackUnmuted);
480
- track.off(TrackEvent.Ended, this.onTrackUnpublish);
481
-
482
- if (stopOnUnpublish === undefined) {
483
- stopOnUnpublish = this.roomOptions?.stopLocalTrackOnUnpublish ?? true;
484
- }
485
- if (stopOnUnpublish) {
486
- track.stop();
487
- }
488
-
489
- const { mediaStreamTrack } = track;
490
-
491
- if (this.engine.publisher) {
492
- const senders = this.engine.publisher.pc.getSenders();
493
- senders.forEach((sender) => {
494
- if (sender.track === mediaStreamTrack) {
495
- try {
496
- this.engine.publisher?.pc.removeTrack(sender);
497
- this.engine.negotiate();
498
- } catch (e) {
499
- log.warn('unpublishTrack', 'failed to remove track', e);
500
- }
501
- }
502
- });
503
- }
504
-
505
- // remove from our maps
506
- this.tracks.delete(publication.trackSid);
507
- switch (publication.kind) {
508
- case Track.Kind.Audio:
509
- this.audioTracks.delete(publication.trackSid);
510
- break;
511
- case Track.Kind.Video:
512
- this.videoTracks.delete(publication.trackSid);
513
- break;
514
- default:
515
- break;
516
- }
517
-
518
- publication.setTrack(undefined);
519
- this.emit(ParticipantEvent.LocalTrackUnpublished, publication);
520
-
521
- return publication;
522
- }
523
-
524
- unpublishTracks(
525
- tracks: LocalTrack[] | MediaStreamTrack[],
526
- ): LocalTrackPublication[] {
527
- const publications: LocalTrackPublication[] = [];
528
- tracks.forEach((track: LocalTrack | MediaStreamTrack) => {
529
- const pub = this.unpublishTrack(track);
530
- if (pub) {
531
- publications.push(pub);
532
- }
533
- });
534
- return publications;
535
- }
536
-
537
- /**
538
- * Publish a new data payload to the room. Data will be forwarded to each
539
- * participant in the room if the destination argument is empty
540
- *
541
- * @param data Uint8Array of the payload. To send string data, use TextEncoder.encode
542
- * @param kind whether to send this as reliable or lossy.
543
- * For data that you need delivery guarantee (such as chat messages), use Reliable.
544
- * For data that should arrive as quickly as possible, but you are ok with dropped
545
- * packets, use Lossy.
546
- * @param destination the participants who will receive the message
547
- */
548
- async publishData(data: Uint8Array, kind: DataPacket_Kind,
549
- destination?: RemoteParticipant[] | string[]) {
550
- const dest: string[] = [];
551
- if (destination !== undefined) {
552
- destination.forEach((val: any) => {
553
- if (val instanceof RemoteParticipant) {
554
- dest.push(val.sid);
555
- } else {
556
- dest.push(val);
557
- }
558
- });
559
- }
560
-
561
- const packet: DataPacket = {
562
- kind,
563
- user: {
564
- participantSid: this.sid,
565
- payload: data,
566
- destinationSids: dest,
567
- },
568
- };
569
-
570
- await this.engine.sendDataPacket(packet, kind);
571
- }
572
-
573
- /**
574
- * Control who can subscribe to LocalParticipant's published tracks.
575
- *
576
- * By default, all participants can subscribe. This allows fine-grained control over
577
- * who is able to subscribe at a participant and track level.
578
- *
579
- * Note: if access is given at a track-level (i.e. both [allParticipantsAllowed] and
580
- * [ParticipantTrackPermission.allTracksAllowed] are false), any newer published tracks
581
- * will not grant permissions to any participants and will require a subsequent
582
- * permissions update to allow subscription.
583
- *
584
- * @param allParticipantsAllowed Allows all participants to subscribe all tracks.
585
- * Takes precedence over [[participantTrackPermissions]] if set to true.
586
- * By default this is set to true.
587
- * @param participantTrackPermissions Full list of individual permissions per
588
- * participant/track. Any omitted participants will not receive any permissions.
589
- */
590
- setTrackSubscriptionPermissions(
591
- allParticipantsAllowed: boolean,
592
- participantTrackPermissions: ParticipantTrackPermission[] = [],
593
- ) {
594
- this.engine.client.sendUpdateSubscriptionPermissions(
595
- allParticipantsAllowed,
596
- participantTrackPermissions.map((p) => trackPermissionToProto(p)),
597
- );
598
- }
599
-
600
- /** @internal */
601
- private onTrackUnmuted = (track: LocalTrack) => {
602
- this.onTrackMuted(track, false);
603
- };
604
-
605
- // when the local track changes in mute status, we'll notify server as such
606
- /** @internal */
607
- private onTrackMuted = (
608
- track: LocalTrack,
609
- muted?: boolean,
610
- ) => {
611
- if (muted === undefined) {
612
- muted = true;
613
- }
614
-
615
- if (!track.sid) {
616
- log.error('could not update mute status for unpublished track', track);
617
- return;
618
- }
619
-
620
- this.engine.updateMuteStatus(track.sid, muted);
621
- };
622
-
623
- private handleSubscribedQualityUpdate = (update: SubscribedQualityUpdate) => {
624
- if (!this.roomOptions?.dynacast) {
625
- return;
626
- }
627
- const pub = this.videoTracks.get(update.trackSid);
628
- if (!pub) {
629
- log.warn('handleSubscribedQualityUpdate',
630
- 'received subscribed quality update for unknown track', update.trackSid);
631
- return;
632
- }
633
- pub.videoTrack?.setPublishingLayers(update.subscribedQualities);
634
- };
635
-
636
- private handleLocalTrackUnpublished = (unpublished: TrackUnpublishedResponse) => {
637
- const track = this.tracks.get(unpublished.trackSid);
638
- if (!track) {
639
- log.warn('handleLocalTrackUnpublished',
640
- 'received unpublished event for unknown track', unpublished.trackSid);
641
- return;
642
- }
643
- this.unpublishTrack(track.track!);
644
- };
645
-
646
- private onTrackUnpublish = (track: LocalTrack) => {
647
- this.unpublishTrack(track);
648
- };
649
-
650
- private getPublicationForTrack(
651
- track: LocalTrack | MediaStreamTrack,
652
- ): LocalTrackPublication | undefined {
653
- let publication: LocalTrackPublication | undefined;
654
- this.tracks.forEach((pub) => {
655
- const localTrack = pub.track;
656
- if (!localTrack) {
657
- return;
658
- }
659
-
660
- // this looks overly complicated due to this object tree
661
- if (track instanceof MediaStreamTrack) {
662
- if (
663
- localTrack instanceof LocalAudioTrack
664
- || localTrack instanceof LocalVideoTrack
665
- ) {
666
- if (localTrack.mediaStreamTrack === track) {
667
- publication = <LocalTrackPublication>pub;
668
- }
669
- }
670
- } else if (track === localTrack) {
671
- publication = <LocalTrackPublication>pub;
672
- }
673
- });
674
- return publication;
675
- }
676
-
677
- private setPreferredCodec(
678
- transceiver: RTCRtpTransceiver,
679
- kind: Track.Kind,
680
- videoCodec: VideoCodec,
681
- ) {
682
- if (!('getCapabilities' in RTCRtpSender)) {
683
- return;
684
- }
685
- const cap = RTCRtpSender.getCapabilities(kind);
686
- if (!cap) return;
687
- const selected = cap.codecs.find((c) => {
688
- const codec = c.mimeType.toLowerCase();
689
- const matchesVideoCodec = codec === `video/${videoCodec}`;
690
-
691
- // for h264 codecs that have sdpFmtpLine available, use only if the
692
- // profile-level-id is 42e01f for cross-browser compatibility
693
- if (videoCodec === 'h264' && c.sdpFmtpLine) {
694
- return matchesVideoCodec && c.sdpFmtpLine.includes('profile-level-id=42e01f');
695
- }
696
-
697
- return matchesVideoCodec || codec === 'audio/opus';
698
- });
699
- if (selected && 'setCodecPreferences' in transceiver) {
700
- // @ts-ignore
701
- transceiver.setCodecPreferences([selected]);
702
- }
703
- }
704
-
705
- /** @internal */
706
- publishedTracksInfo(): TrackPublishedResponse[] {
707
- const infos: TrackPublishedResponse[] = [];
708
- this.tracks.forEach((track: LocalTrackPublication) => {
709
- if (track.track !== undefined) {
710
- infos.push({
711
- cid: track.track.mediaStreamTrack.id,
712
- track: track.trackInfo,
713
- });
714
- }
715
- });
716
- return infos;
717
- }
718
-
719
- /** @internal */
720
- dataChannelsInfo(): DataChannelInfo[] {
721
- const infos: DataChannelInfo[] = [];
722
- const getInfo = (dc: RTCDataChannel | undefined) => {
723
- if (dc?.id !== undefined && dc.id !== null) {
724
- infos.push({
725
- label: dc.label,
726
- id: dc.id,
727
- });
728
- }
729
- };
730
- getInfo(this.engine.dataChannelForKind(DataPacket_Kind.LOSSY));
731
- getInfo(this.engine.dataChannelForKind(DataPacket_Kind.RELIABLE));
732
- return infos;
733
- }
734
- }