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