@convai/web-sdk 2.1.0 → 2.2.0-beta.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.
- package/README.md +2 -0
- package/dist/core/BlendshapeQueue.d.ts +8 -0
- package/dist/core/BlendshapeQueue.js +11 -0
- package/dist/core/ConvaiClient.d.ts +36 -0
- package/dist/core/ConvaiClient.js +106 -1
- package/dist/core/MessageHandler.d.ts +6 -0
- package/dist/core/MessageHandler.js +13 -2
- package/dist/core/SSESession.d.ts +2 -0
- package/dist/core/SSESession.js +3 -0
- package/dist/core/VideoManager.d.ts +97 -2
- package/dist/core/VideoManager.js +434 -64
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +1 -0
- package/dist/core/types.d.ts +105 -0
- package/dist/core/videoSourceState.d.ts +25 -1
- package/dist/core/videoSourceState.js +55 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +1 -0
- package/dist/utils/inlineMarkdown.js +24 -13
- package/dist/utils/markdownBlocks.d.ts +57 -0
- package/dist/utils/markdownBlocks.js +254 -0
- package/dist/vanilla/index.d.ts +2 -0
- package/dist/vanilla/index.js +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { Track } from "livekit-client";
|
|
2
2
|
import { logger } from "../utils/logger.js";
|
|
3
3
|
import { EventEmitter } from "./EventEmitter.js";
|
|
4
|
-
import { isWebcamVisionSource, shouldKeepExistingVisionSource, } from "./videoSourceState.js";
|
|
4
|
+
import { isWebcamVisionSource, shouldKeepExistingVisionSource, visionPublicationName, visionSourceLabel, } from "./videoSourceState.js";
|
|
5
5
|
/**
|
|
6
6
|
* Manages visual source controls. LiveKit publishes real video tracks;
|
|
7
7
|
* WebSocket samples active sources into vision-frame messages.
|
|
8
|
+
*
|
|
9
|
+
* One source is live at a time: starting another replaces it. Sources with
|
|
10
|
+
* different labels stay live together only while `multiSourceVision` is
|
|
11
|
+
* `"active"`, which takes the `visionSourceMode: "multi"` option and a server
|
|
12
|
+
* that keeps a buffer per source. A server without that files every track of
|
|
13
|
+
* a participant under one label, so coexistence there mislabels the frames.
|
|
8
14
|
*/
|
|
9
15
|
export class VideoManager extends EventEmitter {
|
|
10
16
|
constructor(room) {
|
|
@@ -14,7 +20,19 @@ export class VideoManager extends EventEmitter {
|
|
|
14
20
|
this.listeners = [];
|
|
15
21
|
this._isVideoEnabled = false;
|
|
16
22
|
this._isVideoHidden = false;
|
|
17
|
-
|
|
23
|
+
/** Vision sources, oldest first. At most one unless sources coexist. */
|
|
24
|
+
this._visionSources = [];
|
|
25
|
+
this._multiSourceVision = "off";
|
|
26
|
+
/** Publishes held until the server says whether sources may coexist. */
|
|
27
|
+
this.multiSourceDecisionWaiters = [];
|
|
28
|
+
/** Whether this session's multi-source question has been sent. */
|
|
29
|
+
this.multiSourceProbeSent = false;
|
|
30
|
+
/** Tracks being moved to their coexistence publication; their unpublish is not a stop. */
|
|
31
|
+
this.republishingTracks = new WeakSet();
|
|
32
|
+
/** Settles when sources published while pending have been moved for coexistence. */
|
|
33
|
+
this.coexistenceAdoption = Promise.resolve();
|
|
34
|
+
/** Settles when the source start ahead of the next one has finished. */
|
|
35
|
+
this.sourceStartQueue = Promise.resolve();
|
|
18
36
|
this.pendingVisionSourceTracks = new WeakMap();
|
|
19
37
|
this.webSocketVisionMessageSender = null;
|
|
20
38
|
this.webSocketVisionMode = "off";
|
|
@@ -37,6 +55,79 @@ export class VideoManager extends EventEmitter {
|
|
|
37
55
|
get visionSourceState() {
|
|
38
56
|
return this.makeVisionSourceState();
|
|
39
57
|
}
|
|
58
|
+
get visionSources() {
|
|
59
|
+
const live = this._visionSources.filter((source) => source.track.readyState === "live");
|
|
60
|
+
return this.sourcesCoexist ? live : live.slice(-1);
|
|
61
|
+
}
|
|
62
|
+
get multiSourceVision() {
|
|
63
|
+
return this._multiSourceVision;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Set by ConvaiClient: `"pending"` when a session that asked for several
|
|
67
|
+
* sources starts, then the server's answer, and `"off"` when it ends.
|
|
68
|
+
*
|
|
69
|
+
* A session can end with its room already gone (LiveKit dropped it). The
|
|
70
|
+
* room took the publications with it, so its sources are released here as
|
|
71
|
+
* `reset()` releases them on `disconnect()`; none may outlive the session
|
|
72
|
+
* that let them coexist.
|
|
73
|
+
* @internal
|
|
74
|
+
*/
|
|
75
|
+
setMultiSourceVision(state) {
|
|
76
|
+
if (this._multiSourceVision === state)
|
|
77
|
+
return;
|
|
78
|
+
const previous = this._multiSourceVision;
|
|
79
|
+
this._multiSourceVision = state;
|
|
80
|
+
const roomLive = !!this.room && this.room.state !== "disconnected";
|
|
81
|
+
if (state === "pending") {
|
|
82
|
+
this.multiSourceProbeSent = false;
|
|
83
|
+
}
|
|
84
|
+
else if (state === "active") {
|
|
85
|
+
// Sources published while the answer was pending went out as the
|
|
86
|
+
// default publishes them. Move them to their coexistence publications
|
|
87
|
+
// before any held start goes ahead beside them.
|
|
88
|
+
const adoption = this.adoptCoexistencePublications().finally(() => {
|
|
89
|
+
if (this._multiSourceVision !== "pending")
|
|
90
|
+
this.settleMultiSourceDecision();
|
|
91
|
+
});
|
|
92
|
+
this.coexistenceAdoption = adoption.catch(() => { });
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
this.settleMultiSourceDecision();
|
|
96
|
+
}
|
|
97
|
+
if (previous === "active" && roomLive) {
|
|
98
|
+
// Sources no longer coexist but are still being sent: keep the one that
|
|
99
|
+
// started last, as the getters already report, and stop the rest.
|
|
100
|
+
void this.collapseToLastSource();
|
|
101
|
+
}
|
|
102
|
+
if (state === "off" && !roomLive) {
|
|
103
|
+
this.releaseVisionSources();
|
|
104
|
+
this._isVideoEnabled = false;
|
|
105
|
+
this._isVideoHidden = false;
|
|
106
|
+
}
|
|
107
|
+
this.emitVideoStateChange(`multi_source_vision_${state}`);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Set by ConvaiClient when it sends the multi-source question: a source
|
|
111
|
+
* waiting for the answer now waits `MULTI_SOURCE_DECISION_TIMEOUT_MS` at
|
|
112
|
+
* most, counted from here.
|
|
113
|
+
* @internal
|
|
114
|
+
*/
|
|
115
|
+
noteMultiSourceProbeSent() {
|
|
116
|
+
if (this._multiSourceVision !== "pending" || this.multiSourceProbeSent)
|
|
117
|
+
return;
|
|
118
|
+
this.multiSourceProbeSent = true;
|
|
119
|
+
for (const waiter of this.multiSourceDecisionWaiters) {
|
|
120
|
+
waiter.arm(VideoManager.MULTI_SOURCE_DECISION_TIMEOUT_MS);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** The source that started last. */
|
|
124
|
+
get _activeVisionSource() {
|
|
125
|
+
return this._visionSources[this._visionSources.length - 1] ?? null;
|
|
126
|
+
}
|
|
127
|
+
/** Sources with different labels stay live together. */
|
|
128
|
+
get sourcesCoexist() {
|
|
129
|
+
return this._multiSourceVision === "active";
|
|
130
|
+
}
|
|
40
131
|
/**
|
|
41
132
|
* Set the LiveKit room instance
|
|
42
133
|
*/
|
|
@@ -58,13 +149,7 @@ export class VideoManager extends EventEmitter {
|
|
|
58
149
|
setWebSocketVisionDisabled() {
|
|
59
150
|
this.webSocketVisionMessageSender = null;
|
|
60
151
|
this.webSocketVisionMode = "disabled";
|
|
61
|
-
|
|
62
|
-
activeSource?.cleanup?.();
|
|
63
|
-
if (activeSource?.stopTrackOnUnpublish &&
|
|
64
|
-
activeSource.track.readyState !== "ended") {
|
|
65
|
-
activeSource.track.stop();
|
|
66
|
-
}
|
|
67
|
-
this._activeVisionSource = null;
|
|
152
|
+
this.releaseVisionSources();
|
|
68
153
|
this._isVideoEnabled = false;
|
|
69
154
|
}
|
|
70
155
|
/**
|
|
@@ -89,7 +174,10 @@ export class VideoManager extends EventEmitter {
|
|
|
89
174
|
const handleTrackPublished = (publication) => {
|
|
90
175
|
if (this.isWebcamPublication(publication)) {
|
|
91
176
|
this._isVideoEnabled = true;
|
|
92
|
-
|
|
177
|
+
const known = this.sourcesCoexist
|
|
178
|
+
? this._visionSources.some((source) => isWebcamVisionSource(source.source))
|
|
179
|
+
: this._activeVisionSource !== null;
|
|
180
|
+
if (!known) {
|
|
93
181
|
const handle = this.createPublicationHandle(publication, "webcam", "webcam", false);
|
|
94
182
|
if (handle) {
|
|
95
183
|
this.setActiveVisionSource(handle);
|
|
@@ -99,7 +187,16 @@ export class VideoManager extends EventEmitter {
|
|
|
99
187
|
}
|
|
100
188
|
};
|
|
101
189
|
const handleTrackUnpublished = (publication) => {
|
|
102
|
-
|
|
190
|
+
// Moved to its coexistence publication, not stopped.
|
|
191
|
+
const moving = this.mediaTrackFromPublication(publication);
|
|
192
|
+
if (moving && this.republishingTracks.has(moving))
|
|
193
|
+
return;
|
|
194
|
+
// A session that asked for several sources publishes canvas and custom
|
|
195
|
+
// tracks with no camera source, so follow those by the handle that owns
|
|
196
|
+
// them.
|
|
197
|
+
const tracked = this._multiSourceVision !== "off" &&
|
|
198
|
+
this.visionSourceForPublication(publication) !== null;
|
|
199
|
+
if (this.isCameraLikeSource(publication?.source) || tracked) {
|
|
103
200
|
const isWebcam = this.isWebcamPublication(publication);
|
|
104
201
|
this.clearActiveVisionSourceForPublication(publication, isWebcam ? "track_unpublished" : "vision_source_unpublished");
|
|
105
202
|
this.updateState();
|
|
@@ -139,11 +236,17 @@ export class VideoManager extends EventEmitter {
|
|
|
139
236
|
if (!this.room || this.room.state === "disconnected") {
|
|
140
237
|
this._isVideoEnabled = false;
|
|
141
238
|
this._isVideoHidden = false;
|
|
142
|
-
this.
|
|
143
|
-
this.
|
|
239
|
+
const sources = this._visionSources;
|
|
240
|
+
this._visionSources = [];
|
|
241
|
+
for (const source of sources)
|
|
242
|
+
source.cleanup?.();
|
|
144
243
|
return;
|
|
145
244
|
}
|
|
146
|
-
|
|
245
|
+
// With coexisting sources the camera answers for the webcam alone; a
|
|
246
|
+
// canvas that is live says nothing about it.
|
|
247
|
+
let activeSource = this.sourcesCoexist
|
|
248
|
+
? (this.visionSources.find((source) => isWebcamVisionSource(source.source)) ?? null)
|
|
249
|
+
: this.getLiveVisionSource();
|
|
147
250
|
const localParticipant = this.room.localParticipant;
|
|
148
251
|
if (!activeSource && localParticipant.isCameraEnabled) {
|
|
149
252
|
const handle = this.createPublicationHandle(localParticipant.getTrackPublication(Track.Source.Camera), "webcam", "webcam", false);
|
|
@@ -187,6 +290,8 @@ export class VideoManager extends EventEmitter {
|
|
|
187
290
|
if (!this.room) {
|
|
188
291
|
throw new Error("Room not initialized");
|
|
189
292
|
}
|
|
293
|
+
const queued = this.queueSourceStart();
|
|
294
|
+
const release = queued ? await queued : null;
|
|
190
295
|
try {
|
|
191
296
|
await this.replaceActiveVisionSource({ source: "webcam" });
|
|
192
297
|
const publication = await this.room.localParticipant.setCameraEnabled(true);
|
|
@@ -201,6 +306,9 @@ export class VideoManager extends EventEmitter {
|
|
|
201
306
|
catch (error) {
|
|
202
307
|
throw error;
|
|
203
308
|
}
|
|
309
|
+
finally {
|
|
310
|
+
release?.();
|
|
311
|
+
}
|
|
204
312
|
}
|
|
205
313
|
/**
|
|
206
314
|
* Disable video
|
|
@@ -221,14 +329,23 @@ export class VideoManager extends EventEmitter {
|
|
|
221
329
|
try {
|
|
222
330
|
await this.room.localParticipant.setCameraEnabled(false);
|
|
223
331
|
this._isVideoEnabled = false;
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
332
|
+
if (this.sourcesCoexist) {
|
|
333
|
+
// Only the webcam stops; the other sources stay live.
|
|
334
|
+
for (const source of [...this._visionSources]) {
|
|
335
|
+
if (isWebcamVisionSource(source.source)) {
|
|
336
|
+
this.forgetVisionSource(source.track)?.cleanup?.();
|
|
337
|
+
}
|
|
229
338
|
}
|
|
230
|
-
|
|
231
|
-
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
const activeSource = this._activeVisionSource;
|
|
342
|
+
if (activeSource) {
|
|
343
|
+
if (isWebcamVisionSource(activeSource.source)) {
|
|
344
|
+
this.forgetVisionSource(activeSource.track)?.cleanup?.();
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
await this.unpublishVisionSource(activeSource);
|
|
348
|
+
}
|
|
232
349
|
}
|
|
233
350
|
}
|
|
234
351
|
this.emitVideoStateChange("webcam_disabled", { isVideoEnabled: false });
|
|
@@ -290,6 +407,8 @@ export class VideoManager extends EventEmitter {
|
|
|
290
407
|
if (!this.room) {
|
|
291
408
|
throw new Error("Room not initialized");
|
|
292
409
|
}
|
|
410
|
+
const queued = this.queueSourceStart();
|
|
411
|
+
const release = queued ? await queued : null;
|
|
293
412
|
try {
|
|
294
413
|
await this.replaceActiveVisionSource({ source: "webcam" });
|
|
295
414
|
const publication = await this.room.localParticipant.setCameraEnabled(true, {
|
|
@@ -309,6 +428,9 @@ export class VideoManager extends EventEmitter {
|
|
|
309
428
|
logger.error("Failed to set video device:", error);
|
|
310
429
|
throw error;
|
|
311
430
|
}
|
|
431
|
+
finally {
|
|
432
|
+
release?.();
|
|
433
|
+
}
|
|
312
434
|
}
|
|
313
435
|
/**
|
|
314
436
|
* Get available video devices
|
|
@@ -368,26 +490,39 @@ export class VideoManager extends EventEmitter {
|
|
|
368
490
|
}
|
|
369
491
|
}
|
|
370
492
|
async publishVideoTrack(track, options = {}) {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
493
|
+
const queued = this.queueSourceStart();
|
|
494
|
+
const release = queued ? await queued : null;
|
|
495
|
+
try {
|
|
496
|
+
if (this.isWebSocketVisionTransport()) {
|
|
497
|
+
this.assertWebSocketVisionEnabled();
|
|
498
|
+
return await this.publishWebSocketVideoTrack(track, options);
|
|
499
|
+
}
|
|
500
|
+
return await this.publishLiveKitVideoTrack(track, options);
|
|
501
|
+
}
|
|
502
|
+
finally {
|
|
503
|
+
release?.();
|
|
374
504
|
}
|
|
505
|
+
}
|
|
506
|
+
async publishLiveKitVideoTrack(track, options) {
|
|
375
507
|
if (!this.room) {
|
|
376
508
|
throw new Error("Room not initialized");
|
|
377
509
|
}
|
|
378
510
|
const source = options.source ?? "custom";
|
|
379
511
|
const sourceName = options.name ?? source;
|
|
380
|
-
const publicationName = sourceName.toLowerCase().includes(source.toLowerCase())
|
|
381
|
-
? sourceName
|
|
382
|
-
: `${source}:${sourceName}`;
|
|
383
512
|
const stopTrackOnUnpublish = options.stopTrackOnUnpublish ?? false;
|
|
384
513
|
await this.replaceActiveVisionSource({ source, track });
|
|
514
|
+
// After the await: the server's multi-source answer may have arrived.
|
|
515
|
+
// Until it says "active" a source is published as the default publishes
|
|
516
|
+
// it, so a server without multi-source vision receives exactly what it
|
|
517
|
+
// always has; adoptCoexistencePublications() moves it if the answer is yes.
|
|
518
|
+
const liveKitSource = this.toLiveKitSource(source);
|
|
519
|
+
const publicationName = visionPublicationName(source, sourceName, liveKitSource, this.sourcesCoexist);
|
|
385
520
|
this.pendingVisionSourceTracks.set(track, source);
|
|
386
521
|
let publication;
|
|
387
522
|
try {
|
|
388
523
|
publication = await this.room.localParticipant.publishTrack(track, {
|
|
389
524
|
name: publicationName,
|
|
390
|
-
source:
|
|
525
|
+
source: liveKitSource,
|
|
391
526
|
});
|
|
392
527
|
}
|
|
393
528
|
catch (error) {
|
|
@@ -405,7 +540,10 @@ export class VideoManager extends EventEmitter {
|
|
|
405
540
|
};
|
|
406
541
|
this.addTrackEndedListener(handle);
|
|
407
542
|
this.setActiveVisionSource(handle);
|
|
408
|
-
this
|
|
543
|
+
// The answer arrived while this publish was in flight.
|
|
544
|
+
if (this.sourcesCoexist)
|
|
545
|
+
await this.moveToCoexistencePublication(handle);
|
|
546
|
+
this._isVideoEnabled = this._visionSources.some((live) => isWebcamVisionSource(live.source));
|
|
409
547
|
this.emitVideoStateChange("vision_source_published", {
|
|
410
548
|
isVideoEnabled: this._isVideoEnabled,
|
|
411
549
|
});
|
|
@@ -422,7 +560,7 @@ export class VideoManager extends EventEmitter {
|
|
|
422
560
|
handle.track.stop();
|
|
423
561
|
}
|
|
424
562
|
if (wasActive) {
|
|
425
|
-
this.
|
|
563
|
+
this.forgetVisionSource(handle.track);
|
|
426
564
|
if (isWebcamVisionSource(handle.source)) {
|
|
427
565
|
this._isVideoEnabled = false;
|
|
428
566
|
}
|
|
@@ -440,10 +578,7 @@ export class VideoManager extends EventEmitter {
|
|
|
440
578
|
if (handle.stopTrackOnUnpublish && handle.track.readyState !== "ended") {
|
|
441
579
|
handle.track.stop();
|
|
442
580
|
}
|
|
443
|
-
|
|
444
|
-
this._activeVisionSource.cleanup?.();
|
|
445
|
-
this._activeVisionSource = null;
|
|
446
|
-
}
|
|
581
|
+
this.forgetVisionSource(handle.track)?.cleanup?.();
|
|
447
582
|
this.updateState();
|
|
448
583
|
this.emitVideoStateChange("vision_source_unpublished", {
|
|
449
584
|
isVideoEnabled: this._isVideoEnabled,
|
|
@@ -457,7 +592,7 @@ export class VideoManager extends EventEmitter {
|
|
|
457
592
|
const activeSource = this._activeVisionSource;
|
|
458
593
|
if (activeSource?.track === track) {
|
|
459
594
|
activeSource.cleanup?.();
|
|
460
|
-
this.
|
|
595
|
+
this.forgetVisionSource(track);
|
|
461
596
|
}
|
|
462
597
|
else {
|
|
463
598
|
await this.replaceActiveVisionSource({ source, track });
|
|
@@ -550,9 +685,9 @@ export class VideoManager extends EventEmitter {
|
|
|
550
685
|
return this._activeVisionSource;
|
|
551
686
|
if ("track" in source)
|
|
552
687
|
return source;
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
688
|
+
const known = this._visionSources.find((live) => live.track === source);
|
|
689
|
+
if (known)
|
|
690
|
+
return known;
|
|
556
691
|
return {
|
|
557
692
|
track: source,
|
|
558
693
|
sourceName: source.label || "custom",
|
|
@@ -599,12 +734,25 @@ export class VideoManager extends EventEmitter {
|
|
|
599
734
|
}
|
|
600
735
|
if (source === "unknown")
|
|
601
736
|
return Track.Source.Unknown;
|
|
737
|
+
// Where sources coexist the camera source is the webcam's alone.
|
|
738
|
+
// LiveKit's camera helpers act on the first camera publication they find,
|
|
739
|
+
// so beside a canvas published as a camera, setCameraEnabled(true) would
|
|
740
|
+
// unmute the canvas instead of starting the webcam. Core labels these
|
|
741
|
+
// tracks by name. Only once the server has said yes: a server without
|
|
742
|
+
// multi-source vision may sample camera tracks alone.
|
|
743
|
+
if (this.sourcesCoexist && !isWebcamVisionSource(source)) {
|
|
744
|
+
return Track.Source.Unknown;
|
|
745
|
+
}
|
|
602
746
|
// P1 backend sampling listens on the video channel. Keep webcam/canvas/custom
|
|
603
747
|
// as camera-compatible LiveKit video tracks while preserving semantic labels
|
|
604
748
|
// in SDK state and track names.
|
|
605
749
|
return Track.Source.Camera;
|
|
606
750
|
}
|
|
607
751
|
getLiveVisionSource() {
|
|
752
|
+
if (this.sourcesCoexist) {
|
|
753
|
+
const live = this.visionSources;
|
|
754
|
+
return live[live.length - 1] ?? null;
|
|
755
|
+
}
|
|
608
756
|
const source = this._activeVisionSource;
|
|
609
757
|
if (!source)
|
|
610
758
|
return null;
|
|
@@ -633,25 +781,237 @@ export class VideoManager extends EventEmitter {
|
|
|
633
781
|
activeVisionSource: this.activeVisionSource,
|
|
634
782
|
hasActiveVisionSource: this.hasActiveVisionSource,
|
|
635
783
|
visionSourceState: this.makeVisionSourceState(reason),
|
|
784
|
+
visionSources: this.visionSources,
|
|
785
|
+
multiSourceVision: this._multiSourceVision,
|
|
636
786
|
...extra,
|
|
637
787
|
});
|
|
638
788
|
}
|
|
639
789
|
setActiveVisionSource(handle) {
|
|
640
|
-
|
|
641
|
-
|
|
790
|
+
// One source per label when sources coexist, one source otherwise. A
|
|
791
|
+
// handle for the same track is superseded without a cleanup.
|
|
792
|
+
const label = visionSourceLabel(handle.source);
|
|
793
|
+
const kept = [];
|
|
794
|
+
for (const source of this._visionSources) {
|
|
795
|
+
if (source.track === handle.track)
|
|
796
|
+
continue;
|
|
797
|
+
if (this.sourcesCoexist &&
|
|
798
|
+
visionSourceLabel(source.source) !== label &&
|
|
799
|
+
source.track.readyState === "live") {
|
|
800
|
+
kept.push(source);
|
|
801
|
+
continue;
|
|
802
|
+
}
|
|
803
|
+
source.cleanup?.();
|
|
804
|
+
}
|
|
805
|
+
this._visionSources = [...kept, handle];
|
|
806
|
+
}
|
|
807
|
+
/** Remove the source that owns `track`, without cleaning it up. */
|
|
808
|
+
forgetVisionSource(track) {
|
|
809
|
+
const source = this._visionSources.find((live) => live.track === track);
|
|
810
|
+
if (!source)
|
|
811
|
+
return null;
|
|
812
|
+
this._visionSources = this._visionSources.filter((live) => live !== source);
|
|
813
|
+
return source;
|
|
814
|
+
}
|
|
815
|
+
/** Drop every source without unpublishing: the session that carried them is gone. */
|
|
816
|
+
releaseVisionSources() {
|
|
817
|
+
const sources = this._visionSources;
|
|
818
|
+
this._visionSources = [];
|
|
819
|
+
for (const source of sources) {
|
|
820
|
+
source.cleanup?.();
|
|
821
|
+
if (source.stopTrackOnUnpublish && source.track.readyState !== "ended") {
|
|
822
|
+
source.track.stop();
|
|
823
|
+
}
|
|
642
824
|
}
|
|
643
|
-
this._activeVisionSource = handle;
|
|
644
825
|
}
|
|
826
|
+
/**
|
|
827
|
+
* Make room for the next source. One source at a time: whatever is live is
|
|
828
|
+
* unpublished. Coexisting sources: only one with the same label is, since
|
|
829
|
+
* Core keeps one buffer per label.
|
|
830
|
+
*/
|
|
645
831
|
async replaceActiveVisionSource(next) {
|
|
646
|
-
|
|
647
|
-
|
|
832
|
+
// A start that begins after "active" was set does not wait for the
|
|
833
|
+
// answer, but must still wait for pending-era sources to leave the camera
|
|
834
|
+
// source: setCameraEnabled() would otherwise act on a canvas.
|
|
835
|
+
await this.coexistenceAdoption;
|
|
836
|
+
const label = visionSourceLabel(next.source);
|
|
837
|
+
const differsByLabel = (current) => current.track !== next.track &&
|
|
838
|
+
visionSourceLabel(current.source) !== label;
|
|
839
|
+
// The answer decides whether a source with another label is replaced, so
|
|
840
|
+
// wait for it rather than replace on a guess. Bounded.
|
|
841
|
+
if (this._multiSourceVision === "pending" &&
|
|
842
|
+
this._visionSources.some(differsByLabel)) {
|
|
843
|
+
await this.waitForMultiSourceDecision();
|
|
844
|
+
}
|
|
845
|
+
for (const current of [...this._visionSources]) {
|
|
846
|
+
if (next.track && current.track === next.track)
|
|
847
|
+
continue;
|
|
848
|
+
if (this.sourcesCoexist && differsByLabel(current))
|
|
849
|
+
continue;
|
|
850
|
+
if (shouldKeepExistingVisionSource(current.source, next.source, next.track !== undefined)) {
|
|
851
|
+
continue;
|
|
852
|
+
}
|
|
853
|
+
await current.unpublish();
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Take a turn at starting a source; the caller runs when the start ahead of
|
|
858
|
+
* it has finished, and calls the returned function when its own has.
|
|
859
|
+
*
|
|
860
|
+
* Making room for a source and publishing it is one step. Two starts that
|
|
861
|
+
* interleave both find nothing to replace or to wait for, and both tracks
|
|
862
|
+
* go out: the two-tracks-to-an-unconfirmed-server case the multi-source
|
|
863
|
+
* state exists to rule out. So starts take turns in a session that asked for
|
|
864
|
+
* several sources. Elsewhere this returns null and they run as they always
|
|
865
|
+
* have. Stopping a source never waits.
|
|
866
|
+
*/
|
|
867
|
+
queueSourceStart() {
|
|
868
|
+
if (this._multiSourceVision === "off")
|
|
869
|
+
return null;
|
|
870
|
+
const ahead = this.sourceStartQueue;
|
|
871
|
+
let finished;
|
|
872
|
+
this.sourceStartQueue = new Promise((resolve) => (finished = resolve));
|
|
873
|
+
return ahead.then(() => finished);
|
|
874
|
+
}
|
|
875
|
+
waitForMultiSourceDecision() {
|
|
876
|
+
if (this._multiSourceVision !== "pending")
|
|
877
|
+
return Promise.resolve();
|
|
878
|
+
return new Promise((resolve) => {
|
|
879
|
+
let timer;
|
|
880
|
+
const waiter = {
|
|
881
|
+
settle: () => {
|
|
882
|
+
clearTimeout(timer);
|
|
883
|
+
this.multiSourceDecisionWaiters = this.multiSourceDecisionWaiters.filter((other) => other !== waiter);
|
|
884
|
+
resolve();
|
|
885
|
+
},
|
|
886
|
+
arm: (ms) => {
|
|
887
|
+
clearTimeout(timer);
|
|
888
|
+
timer = setTimeout(() => {
|
|
889
|
+
logger.warn("The server has not said whether it supports multi-source vision; " +
|
|
890
|
+
"this source replaces the one that is live.");
|
|
891
|
+
waiter.settle();
|
|
892
|
+
}, ms);
|
|
893
|
+
},
|
|
894
|
+
};
|
|
895
|
+
waiter.arm(this.multiSourceProbeSent
|
|
896
|
+
? VideoManager.MULTI_SOURCE_DECISION_TIMEOUT_MS
|
|
897
|
+
: VideoManager.MULTI_SOURCE_READY_BACKSTOP_MS);
|
|
898
|
+
this.multiSourceDecisionWaiters.push(waiter);
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
settleMultiSourceDecision() {
|
|
902
|
+
for (const waiter of [...this.multiSourceDecisionWaiters])
|
|
903
|
+
waiter.settle();
|
|
904
|
+
}
|
|
905
|
+
/**
|
|
906
|
+
* Coexistence confirmed: move each source published while the answer was
|
|
907
|
+
* pending to the publication Core files under its own label.
|
|
908
|
+
*/
|
|
909
|
+
async adoptCoexistencePublications() {
|
|
910
|
+
for (const handle of [...this._visionSources]) {
|
|
911
|
+
if (!this.sourcesCoexist)
|
|
912
|
+
return;
|
|
913
|
+
await this.moveToCoexistencePublication(handle);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Republish a live LiveKit source under its coexistence LiveKit source and
|
|
918
|
+
* name, when it is not already. The same MediaStreamTrack and handle carry
|
|
919
|
+
* on; only the publication changes. A source that cannot be republished is
|
|
920
|
+
* dropped, as a source unpublished outside videoControls is.
|
|
921
|
+
*/
|
|
922
|
+
async moveToCoexistencePublication(handle) {
|
|
923
|
+
if (!this.room || handle.transport !== "livekit")
|
|
924
|
+
return;
|
|
925
|
+
if (isWebcamVisionSource(handle.source))
|
|
648
926
|
return;
|
|
649
|
-
if (
|
|
927
|
+
if (handle.track.readyState !== "live")
|
|
650
928
|
return;
|
|
651
|
-
|
|
929
|
+
// One move per track at a time: a second would clear the first's guard.
|
|
930
|
+
if (this.republishingTracks.has(handle.track))
|
|
931
|
+
return;
|
|
932
|
+
const publication = handle.publication;
|
|
933
|
+
const liveKitSource = this.toLiveKitSource(handle.source);
|
|
934
|
+
const name = visionPublicationName(handle.source, handle.sourceName, liveKitSource, true);
|
|
935
|
+
if (publication?.source === liveKitSource && publication?.trackName === name) {
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
const localParticipant = this.room.localParticipant;
|
|
939
|
+
this.republishingTracks.add(handle.track);
|
|
940
|
+
// Still the same session, still coexisting, still a live source we own.
|
|
941
|
+
const stillWanted = () => this.sourcesCoexist &&
|
|
942
|
+
this.room?.localParticipant === localParticipant &&
|
|
943
|
+
this._visionSources.includes(handle) &&
|
|
944
|
+
handle.track.readyState === "live";
|
|
945
|
+
try {
|
|
946
|
+
await localParticipant.unpublishTrack(handle.track, false);
|
|
947
|
+
if (!stillWanted()) {
|
|
948
|
+
// Stopped, replaced or no longer coexisting while it was unpublished:
|
|
949
|
+
// forget it rather than publish a track nothing tracks.
|
|
950
|
+
this.dropMovedSource(handle);
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
this.pendingVisionSourceTracks.set(handle.track, handle.source);
|
|
954
|
+
const publication = await localParticipant.publishTrack(handle.track, {
|
|
955
|
+
name,
|
|
956
|
+
source: liveKitSource,
|
|
957
|
+
});
|
|
958
|
+
if (!stillWanted()) {
|
|
959
|
+
await localParticipant.unpublishTrack(handle.track, false);
|
|
960
|
+
this.dropMovedSource(handle);
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
handle.publication = publication;
|
|
964
|
+
}
|
|
965
|
+
catch (error) {
|
|
966
|
+
logger.warn("Could not move a vision source to its multi-source publication:", error);
|
|
967
|
+
this.forgetVisionSource(handle.track)?.cleanup?.();
|
|
968
|
+
// Read again: the track may have ended during the awaits above.
|
|
969
|
+
const state = handle.track.readyState;
|
|
970
|
+
if (handle.stopTrackOnUnpublish && state !== "ended") {
|
|
971
|
+
handle.track.stop();
|
|
972
|
+
}
|
|
973
|
+
this.updateState();
|
|
974
|
+
this.emitVideoStateChange("vision_source_unpublished", {
|
|
975
|
+
isVideoEnabled: this._isVideoEnabled,
|
|
976
|
+
});
|
|
977
|
+
}
|
|
978
|
+
finally {
|
|
979
|
+
this.republishingTracks.delete(handle.track);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
/** A source whose move was abandoned: it is no longer published, so forget it. */
|
|
983
|
+
dropMovedSource(handle) {
|
|
984
|
+
if (!this.forgetVisionSource(handle.track))
|
|
652
985
|
return;
|
|
986
|
+
handle.cleanup?.();
|
|
987
|
+
if (handle.stopTrackOnUnpublish &&
|
|
988
|
+
handle.track.readyState !== "ended") {
|
|
989
|
+
handle.track.stop();
|
|
990
|
+
}
|
|
991
|
+
this.updateState();
|
|
992
|
+
this.emitVideoStateChange("vision_source_unpublished", {
|
|
993
|
+
isVideoEnabled: this._isVideoEnabled,
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
/** Sources stopped coexisting with the room still live: keep only the newest. */
|
|
997
|
+
async collapseToLastSource() {
|
|
998
|
+
const live = this._visionSources.filter((source) => source.track.readyState === "live");
|
|
999
|
+
for (const source of live.slice(0, -1)) {
|
|
1000
|
+
try {
|
|
1001
|
+
if (isWebcamVisionSource(source.source) && source.transport === "livekit") {
|
|
1002
|
+
// Not handle.unpublish(): outside coexistence that is disableVideo(),
|
|
1003
|
+
// which also stops whatever source is newest.
|
|
1004
|
+
await this.room?.localParticipant.setCameraEnabled(false);
|
|
1005
|
+
this.forgetVisionSource(source.track)?.cleanup?.();
|
|
1006
|
+
}
|
|
1007
|
+
else {
|
|
1008
|
+
await this.unpublishVisionSource(source);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
catch (error) {
|
|
1012
|
+
logger.warn("Could not stop a vision source that no longer coexists:", error);
|
|
1013
|
+
}
|
|
653
1014
|
}
|
|
654
|
-
await current.unpublish();
|
|
655
1015
|
}
|
|
656
1016
|
createPublicationHandle(publication, source, sourceName, stopTrackOnUnpublish) {
|
|
657
1017
|
const track = this.mediaTrackFromPublication(publication);
|
|
@@ -686,10 +1046,9 @@ export class VideoManager extends EventEmitter {
|
|
|
686
1046
|
};
|
|
687
1047
|
}
|
|
688
1048
|
clearEndedVisionSource(handle) {
|
|
689
|
-
if (this.
|
|
1049
|
+
if (!this.forgetVisionSource(handle.track))
|
|
690
1050
|
return;
|
|
691
1051
|
handle.cleanup?.();
|
|
692
|
-
this._activeVisionSource = null;
|
|
693
1052
|
if (handle.transport === "websocket") {
|
|
694
1053
|
this.sendWebSocketVisionSourceState(false, handle.source);
|
|
695
1054
|
if (isWebcamVisionSource(handle.source)) {
|
|
@@ -702,17 +1061,17 @@ export class VideoManager extends EventEmitter {
|
|
|
702
1061
|
this.emitVideoStateChange("track_ended");
|
|
703
1062
|
}
|
|
704
1063
|
clearActiveVisionSourceForPublication(publication, reason) {
|
|
705
|
-
const current = this.
|
|
1064
|
+
const current = this.visionSourceForPublication(publication);
|
|
706
1065
|
if (!current)
|
|
707
1066
|
return;
|
|
708
|
-
if (current.publication !== publication &&
|
|
709
|
-
current.track !== this.mediaTrackFromPublication(publication)) {
|
|
710
|
-
return;
|
|
711
|
-
}
|
|
712
1067
|
current.cleanup?.();
|
|
713
|
-
this.
|
|
1068
|
+
this.forgetVisionSource(current.track);
|
|
714
1069
|
this.emitVideoStateChange(reason);
|
|
715
1070
|
}
|
|
1071
|
+
visionSourceForPublication(publication) {
|
|
1072
|
+
const track = this.mediaTrackFromPublication(publication);
|
|
1073
|
+
return (this._visionSources.find((source) => source.publication === publication || source.track === track) ?? null);
|
|
1074
|
+
}
|
|
716
1075
|
mediaTrackFromPublication(publication) {
|
|
717
1076
|
const track = publication?.track;
|
|
718
1077
|
const mediaTrack = track?.mediaStreamTrack ?? track;
|
|
@@ -725,9 +1084,9 @@ export class VideoManager extends EventEmitter {
|
|
|
725
1084
|
const track = this.mediaTrackFromPublication(publication);
|
|
726
1085
|
if (!track)
|
|
727
1086
|
return undefined;
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
1087
|
+
const known = this._visionSources.find((source) => source.track === track);
|
|
1088
|
+
if (known)
|
|
1089
|
+
return known.source;
|
|
731
1090
|
return this.pendingVisionSourceTracks.get(track);
|
|
732
1091
|
}
|
|
733
1092
|
isWebcamPublication(publication) {
|
|
@@ -748,19 +1107,30 @@ export class VideoManager extends EventEmitter {
|
|
|
748
1107
|
this._isVideoHidden = false;
|
|
749
1108
|
this.webSocketVisionMessageSender = null;
|
|
750
1109
|
this.webSocketVisionMode = "off";
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
activeSource.track.stop();
|
|
756
|
-
}
|
|
757
|
-
this._activeVisionSource = null;
|
|
1110
|
+
this.releaseVisionSources();
|
|
1111
|
+
this._multiSourceVision = "off";
|
|
1112
|
+
this.multiSourceProbeSent = false;
|
|
1113
|
+
this.settleMultiSourceDecision();
|
|
758
1114
|
this.emit("videoStateChange", {
|
|
759
1115
|
isVideoEnabled: false,
|
|
760
1116
|
isVideoHidden: false,
|
|
761
1117
|
activeVisionSource: null,
|
|
762
1118
|
hasActiveVisionSource: false,
|
|
763
1119
|
visionSourceState: this.makeVisionSourceState("reset"),
|
|
1120
|
+
visionSources: [],
|
|
1121
|
+
multiSourceVision: "off",
|
|
764
1122
|
});
|
|
765
1123
|
}
|
|
766
1124
|
}
|
|
1125
|
+
/**
|
|
1126
|
+
* How long a second source waits for the server's multi-source answer,
|
|
1127
|
+
* counted from when the question is sent (`noteMultiSourceProbeSent`).
|
|
1128
|
+
*/
|
|
1129
|
+
VideoManager.MULTI_SOURCE_DECISION_TIMEOUT_MS = 10000;
|
|
1130
|
+
/**
|
|
1131
|
+
* The wait before the question is sent. The client asks only once the
|
|
1132
|
+
* character can receive it, which a cold start can delay well past the
|
|
1133
|
+
* decision timeout; a source must not replace another on a guess in the
|
|
1134
|
+
* meantime. Matches the client's bot-ready timeout.
|
|
1135
|
+
*/
|
|
1136
|
+
VideoManager.MULTI_SOURCE_READY_BACKSTOP_MS = 45000;
|