segmind 0.6.0 → 0.7.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 +76 -3
- package/dist/chunk-B7VAGG2U.cjs +342 -0
- package/dist/chunk-B7VAGG2U.cjs.map +1 -0
- package/dist/{chunk-USGY6G4C.js → chunk-YSAOJ3YL.js} +157 -1
- package/dist/chunk-YSAOJ3YL.js.map +1 -0
- package/dist/{errors-CcHEuSmR.d.cts → errors-DsVQfts3.d.cts} +114 -9
- package/dist/{errors-CcHEuSmR.d.ts → errors-DsVQfts3.d.ts} +114 -9
- package/dist/index.cjs +114 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +167 -8
- package/dist/index.d.ts +167 -8
- package/dist/index.js +92 -1
- package/dist/index.js.map +1 -1
- package/dist/realtime/browser/index.cjs +632 -7
- package/dist/realtime/browser/index.cjs.map +1 -1
- package/dist/realtime/browser/index.d.cts +359 -4
- package/dist/realtime/browser/index.d.ts +359 -4
- package/dist/realtime/browser/index.js +627 -2
- package/dist/realtime/browser/index.js.map +1 -1
- package/dist/realtime/index.cjs +41 -141
- package/dist/realtime/index.cjs.map +1 -1
- package/dist/realtime/index.d.cts +12 -5
- package/dist/realtime/index.d.ts +12 -5
- package/dist/realtime/index.js +24 -124
- package/dist/realtime/index.js.map +1 -1
- package/dist/{vad-BcBkqh2p.d.ts → vad-dy0g4f3R.d.ts} +3 -1
- package/dist/{vad-B-BeifvT.d.cts → vad-xeCHdSHo.d.cts} +3 -1
- package/package.json +6 -6
- package/dist/chunk-HWZJ36WC.cjs +0 -186
- package/dist/chunk-HWZJ36WC.cjs.map +0 -1
- package/dist/chunk-USGY6G4C.js.map +0 -1
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2;
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4;
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunkB7VAGG2Ucjs = require('../../chunk-B7VAGG2U.cjs');
|
|
6
8
|
|
|
7
9
|
|
|
8
10
|
var _chunk74UKKBOOcjs = require('../../chunk-74UKKBOO.cjs');
|
|
@@ -34,7 +36,7 @@ registerProcessor('segmind-capture', SegmindCapture);
|
|
|
34
36
|
var MicrophoneCapture = (_class = class {
|
|
35
37
|
constructor(options = {}) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);_class.prototype.__init6.call(this);
|
|
36
38
|
this.options = options;
|
|
37
|
-
this.sampleRate = _nullishCoalesce(options.sampleRate, () => (
|
|
39
|
+
this.sampleRate = _nullishCoalesce(options.sampleRate, () => ( _chunkB7VAGG2Ucjs.SESSION_SAMPLE_RATE));
|
|
38
40
|
this.frameSamples = Math.round(this.sampleRate * (_nullishCoalesce(options.frameMs, () => ( 100))) / 1e3);
|
|
39
41
|
this.context = _nullishCoalesce(options.audioContext, () => ( null));
|
|
40
42
|
this.ownsContext = !options.audioContext;
|
|
@@ -161,7 +163,7 @@ registerProcessor('segmind-playback', SegmindPlayback);
|
|
|
161
163
|
var AudioPlayback = (_class2 = class {
|
|
162
164
|
constructor(options = {}) {;_class2.prototype.__init7.call(this);_class2.prototype.__init8.call(this);_class2.prototype.__init9.call(this);_class2.prototype.__init10.call(this);_class2.prototype.__init11.call(this);
|
|
163
165
|
this.options = options;
|
|
164
|
-
this.sampleRate = _nullishCoalesce(options.sampleRate, () => (
|
|
166
|
+
this.sampleRate = _nullishCoalesce(options.sampleRate, () => ( _chunkB7VAGG2Ucjs.SESSION_SAMPLE_RATE));
|
|
165
167
|
this.context = _nullishCoalesce(options.audioContext, () => ( null));
|
|
166
168
|
this.ownsContext = !options.audioContext;
|
|
167
169
|
}
|
|
@@ -211,7 +213,7 @@ var AudioPlayback = (_class2 = class {
|
|
|
211
213
|
/** Queue one chunk. Accepts the `audio.delta` base64 payload or raw Int16 samples. */
|
|
212
214
|
enqueue(audio) {
|
|
213
215
|
if (!this.node) throw new Error("playback not started");
|
|
214
|
-
const pcm = typeof audio === "string" ?
|
|
216
|
+
const pcm = typeof audio === "string" ? _chunkB7VAGG2Ucjs.decodePcm.call(void 0, audio) : audio;
|
|
215
217
|
this.queuedSamples += pcm.length;
|
|
216
218
|
const copy = pcm.buffer.slice(pcm.byteOffset, pcm.byteOffset + pcm.byteLength);
|
|
217
219
|
this.node.port.postMessage(copy, [copy]);
|
|
@@ -255,7 +257,7 @@ async function startVoice(session, options = {}) {
|
|
|
255
257
|
try {
|
|
256
258
|
await playback.start();
|
|
257
259
|
await capture.start();
|
|
258
|
-
bargeIn =
|
|
260
|
+
bargeIn = _chunkB7VAGG2Ucjs.attachBargeIn.call(void 0, {
|
|
259
261
|
session,
|
|
260
262
|
capture,
|
|
261
263
|
playback,
|
|
@@ -300,8 +302,631 @@ async function startVoice(session, options = {}) {
|
|
|
300
302
|
};
|
|
301
303
|
}
|
|
302
304
|
|
|
305
|
+
// src/realtime/browser/video/control-channel.ts
|
|
306
|
+
var MAX_QUEUED_MESSAGES = 64;
|
|
307
|
+
var ControlChannel = (_class3 = class {
|
|
308
|
+
constructor(channel, options = {}) {;_class3.prototype.__init12.call(this);_class3.prototype.__init13.call(this);_class3.prototype.__init14.call(this);_class3.prototype.__init15.call(this);
|
|
309
|
+
this.channel = channel;
|
|
310
|
+
this.options = options;
|
|
311
|
+
channel.binaryType = "arraybuffer";
|
|
312
|
+
channel.onopen = () => this.handleOpen();
|
|
313
|
+
channel.onclose = () => this.handleDeath("the control channel closed");
|
|
314
|
+
channel.onerror = () => this.handleDeath("the control channel failed");
|
|
315
|
+
channel.onmessage = (event) => this.handleMessage(event.data);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
__init12() {this.pending = []}
|
|
320
|
+
__init13() {this.closing = false}
|
|
321
|
+
__init14() {this.dead = false}
|
|
322
|
+
__init15() {this.decoder = typeof TextDecoder !== "undefined" ? new TextDecoder() : null}
|
|
323
|
+
get readyState() {
|
|
324
|
+
return this.channel.readyState;
|
|
325
|
+
}
|
|
326
|
+
/** Messages waiting for the channel to open. */
|
|
327
|
+
get queued() {
|
|
328
|
+
return this.pending.length;
|
|
329
|
+
}
|
|
330
|
+
/** Sends now when open, queues while connecting, drops otherwise. Returns false when dropped. */
|
|
331
|
+
send(message) {
|
|
332
|
+
if (this.closing || this.dead) return false;
|
|
333
|
+
const payload = JSON.stringify(message);
|
|
334
|
+
const state = this.channel.readyState;
|
|
335
|
+
if (state === "open") return this.rawSend(payload);
|
|
336
|
+
if (state !== "connecting") return false;
|
|
337
|
+
const limit = _nullishCoalesce(this.options.maxQueued, () => ( MAX_QUEUED_MESSAGES));
|
|
338
|
+
if (this.pending.length >= limit) this.pending.shift();
|
|
339
|
+
this.pending.push(payload);
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
/** Sends `last` if the channel is open, then closes it. Never reports a death. */
|
|
343
|
+
close(last) {
|
|
344
|
+
if (this.closing) return;
|
|
345
|
+
if (last && this.channel.readyState === "open" && !this.dead) {
|
|
346
|
+
this.rawSend(JSON.stringify(last));
|
|
347
|
+
}
|
|
348
|
+
this.closing = true;
|
|
349
|
+
this.pending.length = 0;
|
|
350
|
+
try {
|
|
351
|
+
this.channel.close();
|
|
352
|
+
} catch (e) {
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
handleOpen() {
|
|
356
|
+
if (this.closing || this.dead) return;
|
|
357
|
+
const queued = this.pending.splice(0);
|
|
358
|
+
if (this.options.first) queued.unshift(JSON.stringify(this.options.first));
|
|
359
|
+
for (const payload of queued) {
|
|
360
|
+
if (!this.rawSend(payload)) break;
|
|
361
|
+
}
|
|
362
|
+
_optionalChain([this, 'access', _22 => _22.options, 'access', _23 => _23.onOpen, 'optionalCall', _24 => _24()]);
|
|
363
|
+
}
|
|
364
|
+
handleDeath(message) {
|
|
365
|
+
if (this.closing || this.dead) return;
|
|
366
|
+
this.dead = true;
|
|
367
|
+
this.pending.length = 0;
|
|
368
|
+
_optionalChain([this, 'access', _25 => _25.options, 'access', _26 => _26.onDied, 'optionalCall', _27 => _27(message)]);
|
|
369
|
+
}
|
|
370
|
+
handleMessage(data) {
|
|
371
|
+
let text = null;
|
|
372
|
+
if (typeof data === "string") text = data;
|
|
373
|
+
else if (this.decoder && (data instanceof ArrayBuffer || ArrayBuffer.isView(data))) {
|
|
374
|
+
text = this.decoder.decode(data);
|
|
375
|
+
}
|
|
376
|
+
if (text === null) return;
|
|
377
|
+
let parsed;
|
|
378
|
+
try {
|
|
379
|
+
parsed = JSON.parse(text);
|
|
380
|
+
} catch (e2) {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
384
|
+
_optionalChain([this, 'access', _28 => _28.options, 'access', _29 => _29.onMessage, 'optionalCall', _30 => _30(parsed)]);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
rawSend(payload) {
|
|
388
|
+
try {
|
|
389
|
+
this.channel.send(payload);
|
|
390
|
+
return true;
|
|
391
|
+
} catch (e3) {
|
|
392
|
+
return false;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}, _class3);
|
|
396
|
+
|
|
397
|
+
// src/realtime/browser/video/ice.ts
|
|
398
|
+
var ICE_TIMEOUT_MS = 12e3;
|
|
399
|
+
var ICE_QUIET_PERIOD_MS = 1250;
|
|
400
|
+
function countTurnServers(servers = []) {
|
|
401
|
+
return servers.filter((server) => {
|
|
402
|
+
const urls = Array.isArray(server.urls) ? server.urls : [server.urls];
|
|
403
|
+
return urls.some((url) => /^turns?:/i.test(url));
|
|
404
|
+
}).length;
|
|
405
|
+
}
|
|
406
|
+
function candidateType(candidate) {
|
|
407
|
+
const match = candidate.match(/(?:^|\s)typ\s+(host|srflx|relay)(?:\s|$)/);
|
|
408
|
+
return _nullishCoalesce(_optionalChain([match, 'optionalAccess', _31 => _31[1]]), () => ( null));
|
|
409
|
+
}
|
|
410
|
+
function gatherIceCandidates(pc, options = {}) {
|
|
411
|
+
const {
|
|
412
|
+
iceServers = [],
|
|
413
|
+
timeoutMs = ICE_TIMEOUT_MS,
|
|
414
|
+
quietPeriodMs = ICE_QUIET_PERIOD_MS,
|
|
415
|
+
setTimeout: schedule = setTimeout,
|
|
416
|
+
clearTimeout: cancel = clearTimeout
|
|
417
|
+
} = options;
|
|
418
|
+
const counts = { host: 0, srflx: 0, relay: 0 };
|
|
419
|
+
for (const line of (_nullishCoalesce(_optionalChain([pc, 'access', _32 => _32.localDescription, 'optionalAccess', _33 => _33.sdp]), () => ( ""))).split(/\r?\n/)) {
|
|
420
|
+
if (!line.startsWith("a=candidate:")) continue;
|
|
421
|
+
const type = candidateType(line.slice(2));
|
|
422
|
+
if (type) counts[type] += 1;
|
|
423
|
+
}
|
|
424
|
+
if (pc.iceGatheringState === "complete") {
|
|
425
|
+
return Promise.resolve({ ...counts, state: "complete" });
|
|
426
|
+
}
|
|
427
|
+
const requireRelay = countTurnServers(iceServers) > 0;
|
|
428
|
+
const sufficient = () => requireRelay ? counts.relay > 0 : counts.srflx > 0;
|
|
429
|
+
return new Promise((resolve) => {
|
|
430
|
+
let settled = false;
|
|
431
|
+
let quiet = null;
|
|
432
|
+
const finish = (state) => {
|
|
433
|
+
if (settled) return;
|
|
434
|
+
settled = true;
|
|
435
|
+
cancel(hardBound);
|
|
436
|
+
if (quiet !== null) cancel(quiet);
|
|
437
|
+
pc.removeEventListener("icegatheringstatechange", onState);
|
|
438
|
+
pc.removeEventListener("icecandidate", onCandidate);
|
|
439
|
+
resolve({ ...counts, state });
|
|
440
|
+
};
|
|
441
|
+
const onState = () => {
|
|
442
|
+
if (pc.iceGatheringState === "complete") finish("complete");
|
|
443
|
+
};
|
|
444
|
+
const onCandidate = (event) => {
|
|
445
|
+
const type = event.candidate ? candidateType(event.candidate.candidate) : null;
|
|
446
|
+
if (type) counts[type] += 1;
|
|
447
|
+
if (quiet !== null) cancel(quiet);
|
|
448
|
+
quiet = sufficient() ? schedule(() => finish("sufficient"), quietPeriodMs) : null;
|
|
449
|
+
};
|
|
450
|
+
const hardBound = schedule(() => finish("timeout"), timeoutMs);
|
|
451
|
+
pc.addEventListener("icegatheringstatechange", onState);
|
|
452
|
+
pc.addEventListener("icecandidate", onCandidate);
|
|
453
|
+
if (sufficient()) quiet = schedule(() => finish("sufficient"), quietPeriodMs);
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// src/realtime/browser/video/peer.ts
|
|
458
|
+
function defaultPeerConnection(configuration) {
|
|
459
|
+
const Ctor = globalThis.RTCPeerConnection;
|
|
460
|
+
if (!Ctor) throw new Error("no RTCPeerConnection in this runtime: pass peerConnectionFactory");
|
|
461
|
+
return new Ctor(configuration);
|
|
462
|
+
}
|
|
463
|
+
function defaultGetUserMedia(constraints) {
|
|
464
|
+
const devices = _optionalChain([globalThis, 'access', _34 => _34.navigator, 'optionalAccess', _35 => _35.mediaDevices]);
|
|
465
|
+
if (!_optionalChain([devices, 'optionalAccess', _36 => _36.getUserMedia])) {
|
|
466
|
+
return Promise.reject(new Error("no camera access in this runtime: pass getUserMedia"));
|
|
467
|
+
}
|
|
468
|
+
return devices.getUserMedia(constraints);
|
|
469
|
+
}
|
|
470
|
+
function defaultMediaStream(tracks) {
|
|
471
|
+
const Ctor = globalThis.MediaStream;
|
|
472
|
+
return Ctor ? new Ctor(tracks) : null;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// src/realtime/browser/video/video-session.ts
|
|
476
|
+
var REPORTED_CHANNEL_EVENTS = /* @__PURE__ */ new Set([
|
|
477
|
+
"configured",
|
|
478
|
+
"prompt_applied",
|
|
479
|
+
"prompt_rejected",
|
|
480
|
+
"deadline_missed",
|
|
481
|
+
"stream_exhausted",
|
|
482
|
+
"error"
|
|
483
|
+
]);
|
|
484
|
+
var DEFAULT_RESOLUTION = "768p";
|
|
485
|
+
var DEFAULT_ASPECT_RATIO = "16:9";
|
|
486
|
+
var RESTART_GRACE_MS = 8e3;
|
|
487
|
+
var DEFAULT_ICE_SERVERS = [{ urls: "stun:stun.cloudflare.com:3478" }];
|
|
488
|
+
function attachVideo(session, options) {
|
|
489
|
+
return new VideoSession(session, options);
|
|
490
|
+
}
|
|
491
|
+
var VideoSession = (_class4 = class {
|
|
492
|
+
constructor(session, options = {}) {;_class4.prototype.__init16.call(this);_class4.prototype.__init17.call(this);_class4.prototype.__init18.call(this);_class4.prototype.__init19.call(this);_class4.prototype.__init20.call(this);_class4.prototype.__init21.call(this);_class4.prototype.__init22.call(this);_class4.prototype.__init23.call(this);_class4.prototype.__init24.call(this);_class4.prototype.__init25.call(this);_class4.prototype.__init26.call(this);_class4.prototype.__init27.call(this);_class4.prototype.__init28.call(this);_class4.prototype.__init29.call(this);_class4.prototype.__init30.call(this);_class4.prototype.__init31.call(this);_class4.prototype.__init32.call(this);_class4.prototype.__init33.call(this);_class4.prototype.__init34.call(this);_class4.prototype.__init35.call(this);
|
|
493
|
+
this.session = session;
|
|
494
|
+
this.options = options;
|
|
495
|
+
if (session.state === "closed") throw new Error("the session has already closed");
|
|
496
|
+
this.unsubscribe.push(session.onEvent((event) => this.handleEvent(event)));
|
|
497
|
+
this.unsubscribe.push(
|
|
498
|
+
session.onState((state) => {
|
|
499
|
+
if (state === "closed") this.teardown();
|
|
500
|
+
})
|
|
501
|
+
);
|
|
502
|
+
if (session.created) this.handleCreated(session.created);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
__init16() {this._phase = "waiting"}
|
|
507
|
+
__init17() {this._control = null}
|
|
508
|
+
__init18() {this._localStream = null}
|
|
509
|
+
__init19() {this._remoteStream = null}
|
|
510
|
+
__init20() {this.pc = null}
|
|
511
|
+
__init21() {this.channel = null}
|
|
512
|
+
__init22() {this.listeners = /* @__PURE__ */ new Set()}
|
|
513
|
+
__init23() {this.unsubscribe = []}
|
|
514
|
+
__init24() {this.ownsLocalStream = false}
|
|
515
|
+
/** The upstream's answer is applied; remote candidates go straight in. */
|
|
516
|
+
__init25() {this.remoteReady = false}
|
|
517
|
+
__init26() {this.remoteCandidates = []}
|
|
518
|
+
/** Local candidates wait until the offer they belong to has been sent. */
|
|
519
|
+
__init27() {this.offerSent = false}
|
|
520
|
+
__init28() {this.localCandidates = []}
|
|
521
|
+
__init29() {this.sentVersion = 1}
|
|
522
|
+
/** Data-channel prompts made before the channel exists; they go out right after `configure`. */
|
|
523
|
+
__init30() {this.earlyPrompts = []}
|
|
524
|
+
/** The opening prompt has gone out on a `session` stream (or there is none). */
|
|
525
|
+
__init31() {this.openingSent = false}
|
|
526
|
+
__init32() {this.stopSent = false}
|
|
527
|
+
/** `end()` was called: the session is closing, so nothing new is opened. */
|
|
528
|
+
__init33() {this.ending = false}
|
|
529
|
+
__init34() {this.failed = false}
|
|
530
|
+
__init35() {this.restartTimer = null}
|
|
531
|
+
get phase() {
|
|
532
|
+
return this._phase;
|
|
533
|
+
}
|
|
534
|
+
/** `session` or `datachannel` once the session is live; null before, or for a non-video session. */
|
|
535
|
+
get control() {
|
|
536
|
+
return this._control;
|
|
537
|
+
}
|
|
538
|
+
/** The `prompt_version` of the latest prompt on a data channel (`configure` is 1). */
|
|
539
|
+
get promptVersion() {
|
|
540
|
+
return this.sentVersion;
|
|
541
|
+
}
|
|
542
|
+
/** Read through a getter so TypeScript does not narrow it across an await. */
|
|
543
|
+
get closed() {
|
|
544
|
+
return this._phase === "closed";
|
|
545
|
+
}
|
|
546
|
+
get localStream() {
|
|
547
|
+
return this._localStream;
|
|
548
|
+
}
|
|
549
|
+
get remoteStream() {
|
|
550
|
+
return this._remoteStream;
|
|
551
|
+
}
|
|
552
|
+
onEvent(listener) {
|
|
553
|
+
this.listeners.add(listener);
|
|
554
|
+
return () => this.listeners.delete(listener);
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Steer the generation. On a `datachannel` session it goes on the runner's
|
|
558
|
+
* channel with the next `prompt_version` (queued until the channel opens).
|
|
559
|
+
* False when it could not be sent.
|
|
560
|
+
*/
|
|
561
|
+
prompt(text, options = {}) {
|
|
562
|
+
const value = text.trim();
|
|
563
|
+
if (!value || this.closed) return false;
|
|
564
|
+
if (this._control === "datachannel") {
|
|
565
|
+
const message = { type: "prompt", prompt_version: this.sentVersion + 1, prompt: value };
|
|
566
|
+
if (!this.channel) {
|
|
567
|
+
const limit = _nullishCoalesce(this.options.maxQueuedMessages, () => ( MAX_QUEUED_MESSAGES));
|
|
568
|
+
if (this.earlyPrompts.length >= limit) this.earlyPrompts.shift();
|
|
569
|
+
this.earlyPrompts.push(message);
|
|
570
|
+
this.sentVersion += 1;
|
|
571
|
+
return true;
|
|
572
|
+
}
|
|
573
|
+
const sent = this.channel.send(message);
|
|
574
|
+
if (sent) this.sentVersion += 1;
|
|
575
|
+
return sent;
|
|
576
|
+
}
|
|
577
|
+
if (this._control !== "session") return false;
|
|
578
|
+
this.sendOpeningPrompt();
|
|
579
|
+
return this.sendPrompt(value, options);
|
|
580
|
+
}
|
|
581
|
+
sendOpeningPrompt() {
|
|
582
|
+
if (this.openingSent) return;
|
|
583
|
+
this.openingSent = true;
|
|
584
|
+
const opening = _optionalChain([this, 'access', _37 => _37.options, 'access', _38 => _38.prompt, 'optionalAccess', _39 => _39.trim, 'call', _40 => _40()]);
|
|
585
|
+
if (opening) this.sendPrompt(opening);
|
|
586
|
+
}
|
|
587
|
+
sendPrompt(value, options = {}) {
|
|
588
|
+
const enhance = _nullishCoalesce(options.enhance, () => ( this.options.enhance));
|
|
589
|
+
return this.session.send({
|
|
590
|
+
type: "video.prompt",
|
|
591
|
+
text: value,
|
|
592
|
+
...enhance === void 0 ? {} : { enhance }
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* A reference image (base64, no `data:` prefix) with an optional prompt, or
|
|
597
|
+
* null to clear it. Only a `session` stream takes one.
|
|
598
|
+
*/
|
|
599
|
+
setImage(image, text, options = {}) {
|
|
600
|
+
if (this._control !== "session" || this.closed) return false;
|
|
601
|
+
const enhance = _nullishCoalesce(options.enhance, () => ( this.options.enhance));
|
|
602
|
+
return this.session.send({
|
|
603
|
+
type: "video.image",
|
|
604
|
+
image,
|
|
605
|
+
..._optionalChain([text, 'optionalAccess', _41 => _41.trim, 'call', _42 => _42()]) ? { text: text.trim() } : {},
|
|
606
|
+
...enhance === void 0 ? {} : { enhance }
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* End the session: the runner's `stop` first (a `datachannel` session stops
|
|
611
|
+
* generating now, not when its lease lapses), then `session.close()`. The
|
|
612
|
+
* camera is released at once; the peer stays up until the session closes,
|
|
613
|
+
* so `stop` has a transport to leave on.
|
|
614
|
+
*/
|
|
615
|
+
end() {
|
|
616
|
+
this.ending = true;
|
|
617
|
+
this.sendStop();
|
|
618
|
+
this.releaseCamera();
|
|
619
|
+
return this.session.close();
|
|
620
|
+
}
|
|
621
|
+
/** Stop listening and release the peer and the camera without closing the session. */
|
|
622
|
+
detach() {
|
|
623
|
+
this.teardown();
|
|
624
|
+
}
|
|
625
|
+
// --- server events ------------------------------------------------------------
|
|
626
|
+
handleEvent(event) {
|
|
627
|
+
switch (event.type) {
|
|
628
|
+
case "session.created":
|
|
629
|
+
this.handleCreated(event);
|
|
630
|
+
break;
|
|
631
|
+
case "webrtc.config":
|
|
632
|
+
if (this._control === "datachannel" && !this.pc && !this.ending) void this.startReceiveOnly(event);
|
|
633
|
+
break;
|
|
634
|
+
case "webrtc.answer":
|
|
635
|
+
void this.applyAnswer(event.sdp, event.ice_servers);
|
|
636
|
+
break;
|
|
637
|
+
case "webrtc.candidate":
|
|
638
|
+
if (this._control === "session" && event.candidate) void this.addRemote(event.candidate);
|
|
639
|
+
break;
|
|
640
|
+
case "webrtc.restart":
|
|
641
|
+
if (this._control === "session") void this.restart(event.ice_servers);
|
|
642
|
+
break;
|
|
643
|
+
case "video.started":
|
|
644
|
+
this.emit({ type: "started" });
|
|
645
|
+
break;
|
|
646
|
+
case "video.progress":
|
|
647
|
+
this.emit({ type: "progress", seconds: event.seconds });
|
|
648
|
+
break;
|
|
649
|
+
case "video.prompt_ack":
|
|
650
|
+
this.emit({
|
|
651
|
+
type: "prompt_ack",
|
|
652
|
+
kind: event.kind,
|
|
653
|
+
applied: event.applied,
|
|
654
|
+
...event.reason ? { reason: event.reason } : {}
|
|
655
|
+
});
|
|
656
|
+
break;
|
|
657
|
+
default:
|
|
658
|
+
break;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
handleCreated(created) {
|
|
662
|
+
if (this._control || this.closed) return;
|
|
663
|
+
this._control = _chunkB7VAGG2Ucjs.videoControl.call(void 0, created);
|
|
664
|
+
if (this._control === "session") void this.startCamera(created);
|
|
665
|
+
}
|
|
666
|
+
// --- session control: the camera up, the edited stream down ------------------
|
|
667
|
+
async startCamera(created) {
|
|
668
|
+
this.setPhase("negotiating");
|
|
669
|
+
let stream;
|
|
670
|
+
try {
|
|
671
|
+
stream = await _asyncNullishCoalesce(this.options.localStream, async () => ( await this.openCamera(created)));
|
|
672
|
+
} catch (error) {
|
|
673
|
+
this.fail(`camera: ${errorMessage(error)}`);
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
if (this.closed || this.ending) {
|
|
677
|
+
if (!this.options.localStream) stopTracks(stream);
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
this.ownsLocalStream = !this.options.localStream;
|
|
681
|
+
this._localStream = stream;
|
|
682
|
+
this.emit({ type: "local_stream", stream });
|
|
683
|
+
try {
|
|
684
|
+
const pc = this.createPeer({ iceServers: [..._nullishCoalesce(this.options.iceServers, () => ( DEFAULT_ICE_SERVERS))] });
|
|
685
|
+
pc.addEventListener("icecandidate", (event) => {
|
|
686
|
+
this.sendLocalCandidate(event.candidate ? event.candidate.toJSON() : null);
|
|
687
|
+
});
|
|
688
|
+
for (const track of stream.getTracks()) pc.addTrack(track, stream);
|
|
689
|
+
await this.offer(pc);
|
|
690
|
+
} catch (error) {
|
|
691
|
+
this.fail(`negotiation: ${errorMessage(error)}`);
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
if (this.closed) return;
|
|
695
|
+
this.sendOpeningPrompt();
|
|
696
|
+
}
|
|
697
|
+
async openCamera(created) {
|
|
698
|
+
const getUserMedia = _nullishCoalesce(this.options.getUserMedia, () => ( defaultGetUserMedia));
|
|
699
|
+
const video = _chunkB7VAGG2Ucjs.webrtcVideo.call(void 0, created);
|
|
700
|
+
const constraints = {};
|
|
701
|
+
if (_optionalChain([video, 'optionalAccess', _43 => _43.width])) constraints.width = { ideal: video.width };
|
|
702
|
+
if (_optionalChain([video, 'optionalAccess', _44 => _44.height])) constraints.height = { ideal: video.height };
|
|
703
|
+
if (_optionalChain([video, 'optionalAccess', _45 => _45.fps])) constraints.frameRate = { ideal: video.fps };
|
|
704
|
+
const audioSpec = _optionalChain([created, 'access', _46 => _46.media, 'optionalAccess', _47 => _47.audio]);
|
|
705
|
+
const wantAudio = _nullishCoalesce(this.options.audio, () => ( (_optionalChain([audioSpec, 'optionalAccess', _48 => _48.transport]) === "webrtc" && (audioSpec.direction === "in" || audioSpec.direction === "duplex"))));
|
|
706
|
+
const videoConstraint = Object.keys(constraints).length ? constraints : true;
|
|
707
|
+
try {
|
|
708
|
+
return await getUserMedia({ video: videoConstraint, audio: wantAudio });
|
|
709
|
+
} catch (error) {
|
|
710
|
+
if (!wantAudio) throw error;
|
|
711
|
+
return getUserMedia({ video: videoConstraint, audio: false });
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
async offer(pc, iceRestart = false) {
|
|
715
|
+
const offer = await pc.createOffer(iceRestart ? { iceRestart: true } : void 0);
|
|
716
|
+
await pc.setLocalDescription(offer);
|
|
717
|
+
if (this.closed || pc !== this.pc) return;
|
|
718
|
+
const sdp = _nullishCoalesce(_optionalChain([pc, 'access', _49 => _49.localDescription, 'optionalAccess', _50 => _50.sdp]), () => ( offer.sdp));
|
|
719
|
+
if (!sdp) throw new Error("the browser produced no SDP offer");
|
|
720
|
+
this.session.send({ type: "webrtc.offer", sdp });
|
|
721
|
+
this.offerSent = true;
|
|
722
|
+
for (const candidate of this.localCandidates.splice(0)) {
|
|
723
|
+
this.session.send({ type: "webrtc.candidate", candidate });
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
sendLocalCandidate(candidate) {
|
|
727
|
+
if (this.closed) return;
|
|
728
|
+
if (!this.offerSent) {
|
|
729
|
+
this.localCandidates.push(candidate);
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
this.session.send({ type: "webrtc.candidate", candidate });
|
|
733
|
+
}
|
|
734
|
+
async addRemote(candidate) {
|
|
735
|
+
if (!this.pc || !this.remoteReady) {
|
|
736
|
+
this.remoteCandidates.push(candidate);
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
try {
|
|
740
|
+
await this.pc.addIceCandidate(candidate);
|
|
741
|
+
} catch (e4) {
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
async restart(iceServers) {
|
|
745
|
+
const pc = this.pc;
|
|
746
|
+
if (!pc || this.closed) return;
|
|
747
|
+
this.clearRestartTimer();
|
|
748
|
+
try {
|
|
749
|
+
pc.setConfiguration({ ...pc.getConfiguration(), iceServers });
|
|
750
|
+
this.remoteReady = false;
|
|
751
|
+
this.offerSent = false;
|
|
752
|
+
await this.offer(pc, true);
|
|
753
|
+
} catch (error) {
|
|
754
|
+
this.fail(`ICE restart: ${errorMessage(error)}`);
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
// --- datachannel control: receive-only, one complete offer -------------------
|
|
758
|
+
async startReceiveOnly(config) {
|
|
759
|
+
this.setPhase("negotiating");
|
|
760
|
+
try {
|
|
761
|
+
const pc = this.createPeer({ iceServers: config.ice_servers });
|
|
762
|
+
pc.addTransceiver("video", { direction: "recvonly" });
|
|
763
|
+
pc.addTransceiver("audio", { direction: "recvonly" });
|
|
764
|
+
this.channel = new ControlChannel(pc.createDataChannel("control"), {
|
|
765
|
+
first: {
|
|
766
|
+
type: "configure",
|
|
767
|
+
protocol_version: 1,
|
|
768
|
+
prompt_version: 1,
|
|
769
|
+
prompt: _nullishCoalesce(_optionalChain([this, 'access', _51 => _51.options, 'access', _52 => _52.prompt, 'optionalAccess', _53 => _53.trim, 'call', _54 => _54()]), () => ( "")),
|
|
770
|
+
resolution: _nullishCoalesce(_nullishCoalesce(this.options.resolution, () => ( this.session.params.resolution)), () => ( DEFAULT_RESOLUTION)),
|
|
771
|
+
aspect_ratio: _nullishCoalesce(_nullishCoalesce(this.options.aspectRatio, () => ( this.session.params.aspect_ratio)), () => ( DEFAULT_ASPECT_RATIO))
|
|
772
|
+
},
|
|
773
|
+
maxQueued: _nullishCoalesce(this.options.maxQueuedMessages, () => ( MAX_QUEUED_MESSAGES)),
|
|
774
|
+
onMessage: (message) => this.handleChannelMessage(message),
|
|
775
|
+
onDied: (message) => this.fail(message)
|
|
776
|
+
});
|
|
777
|
+
for (const message of this.earlyPrompts.splice(0)) this.channel.send(message);
|
|
778
|
+
const offer = await pc.createOffer();
|
|
779
|
+
const gathering = gatherIceCandidates(pc, {
|
|
780
|
+
iceServers: config.ice_servers,
|
|
781
|
+
timeoutMs: this.options.iceTimeoutMs,
|
|
782
|
+
quietPeriodMs: this.options.iceQuietPeriodMs,
|
|
783
|
+
setTimeout: this.options.setTimeout,
|
|
784
|
+
clearTimeout: this.options.clearTimeout
|
|
785
|
+
});
|
|
786
|
+
const [gathered] = await Promise.all([gathering, pc.setLocalDescription(offer)]);
|
|
787
|
+
if (this.closed || pc !== this.pc) return;
|
|
788
|
+
if (gathered.relay === 0 && gathered.srflx === 0 && gathered.host === 0) {
|
|
789
|
+
throw new Error("ICE gathering produced no candidate");
|
|
790
|
+
}
|
|
791
|
+
const sdp = _optionalChain([pc, 'access', _55 => _55.localDescription, 'optionalAccess', _56 => _56.sdp]);
|
|
792
|
+
if (!sdp) throw new Error("the browser produced no SDP offer");
|
|
793
|
+
this.session.send({ type: "webrtc.offer", sdp });
|
|
794
|
+
this.offerSent = true;
|
|
795
|
+
} catch (error) {
|
|
796
|
+
this.fail(`negotiation: ${errorMessage(error)}`);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
handleChannelMessage(message) {
|
|
800
|
+
this.emit({ type: "channel", event: message });
|
|
801
|
+
if (typeof message.type === "string" && REPORTED_CHANNEL_EVENTS.has(message.type)) {
|
|
802
|
+
this.session.send({ type: "video.report", event: message });
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
sendStop() {
|
|
806
|
+
if (this.stopSent || !this.channel) return;
|
|
807
|
+
this.stopSent = true;
|
|
808
|
+
this.channel.close({ type: "stop" });
|
|
809
|
+
}
|
|
810
|
+
// --- both -------------------------------------------------------------------
|
|
811
|
+
createPeer(configuration) {
|
|
812
|
+
const pc = (_nullishCoalesce(this.options.peerConnectionFactory, () => ( defaultPeerConnection)))(configuration);
|
|
813
|
+
this.pc = pc;
|
|
814
|
+
pc.addEventListener("track", (event) => this.handleTrack(event));
|
|
815
|
+
pc.addEventListener("connectionstatechange", () => this.handleConnectionState(pc));
|
|
816
|
+
return pc;
|
|
817
|
+
}
|
|
818
|
+
async applyAnswer(sdp, iceServers) {
|
|
819
|
+
const pc = this.pc;
|
|
820
|
+
if (!pc || this.closed) return;
|
|
821
|
+
try {
|
|
822
|
+
if (_optionalChain([iceServers, 'optionalAccess', _57 => _57.length])) pc.setConfiguration({ ...pc.getConfiguration(), iceServers });
|
|
823
|
+
await pc.setRemoteDescription({ type: "answer", sdp });
|
|
824
|
+
} catch (error) {
|
|
825
|
+
this.fail(`answer: ${errorMessage(error)}`);
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
this.remoteReady = true;
|
|
829
|
+
for (const candidate of this.remoteCandidates.splice(0)) await this.addRemote(candidate);
|
|
830
|
+
}
|
|
831
|
+
handleTrack(event) {
|
|
832
|
+
if (this.closed) return;
|
|
833
|
+
const current = this._remoteStream;
|
|
834
|
+
if (current) {
|
|
835
|
+
if (!current.getTracks().includes(event.track)) current.addTrack(event.track);
|
|
836
|
+
return;
|
|
837
|
+
}
|
|
838
|
+
const stream = _nullishCoalesce(event.streams[0], () => ( (_nullishCoalesce(this.options.createMediaStream, () => ( defaultMediaStream)))([event.track])));
|
|
839
|
+
if (!stream) return;
|
|
840
|
+
this._remoteStream = stream;
|
|
841
|
+
this.emit({ type: "remote_stream", stream });
|
|
842
|
+
}
|
|
843
|
+
handleConnectionState(pc) {
|
|
844
|
+
if (pc !== this.pc || this.closed) return;
|
|
845
|
+
const state = pc.connectionState;
|
|
846
|
+
this.emit({ type: "connection", state });
|
|
847
|
+
if (state === "connected") {
|
|
848
|
+
this.clearRestartTimer();
|
|
849
|
+
this.setPhase("connected");
|
|
850
|
+
} else if (state === "failed") {
|
|
851
|
+
if (this._control === "session") {
|
|
852
|
+
const schedule = _nullishCoalesce(this.options.setTimeout, () => ( setTimeout));
|
|
853
|
+
this.clearRestartTimer();
|
|
854
|
+
this.restartTimer = schedule(() => {
|
|
855
|
+
this.restartTimer = null;
|
|
856
|
+
if (_optionalChain([this, 'access', _58 => _58.pc, 'optionalAccess', _59 => _59.connectionState]) === "failed") {
|
|
857
|
+
this.fail("the video connection failed");
|
|
858
|
+
}
|
|
859
|
+
}, RESTART_GRACE_MS);
|
|
860
|
+
} else {
|
|
861
|
+
this.fail("the video connection failed");
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
clearRestartTimer() {
|
|
866
|
+
if (this.restartTimer !== null) {
|
|
867
|
+
(_nullishCoalesce(this.options.clearTimeout, () => ( clearTimeout)))(this.restartTimer);
|
|
868
|
+
this.restartTimer = null;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
/** Report the failure and end the session: an uncontrollable stream should not keep billing. */
|
|
872
|
+
fail(message) {
|
|
873
|
+
if (this.failed || this.closed) return;
|
|
874
|
+
this.failed = true;
|
|
875
|
+
this.emit({ type: "failed", message });
|
|
876
|
+
void this.end();
|
|
877
|
+
}
|
|
878
|
+
teardown() {
|
|
879
|
+
if (this.closed) return;
|
|
880
|
+
this.sendStop();
|
|
881
|
+
this.clearRestartTimer();
|
|
882
|
+
this.setPhase("closed");
|
|
883
|
+
for (const off of this.unsubscribe.splice(0)) off();
|
|
884
|
+
this.remoteCandidates.length = 0;
|
|
885
|
+
this.localCandidates.length = 0;
|
|
886
|
+
const pc = this.pc;
|
|
887
|
+
this.pc = null;
|
|
888
|
+
try {
|
|
889
|
+
_optionalChain([pc, 'optionalAccess', _60 => _60.close, 'call', _61 => _61()]);
|
|
890
|
+
} catch (e5) {
|
|
891
|
+
}
|
|
892
|
+
this.earlyPrompts.length = 0;
|
|
893
|
+
this.releaseCamera();
|
|
894
|
+
}
|
|
895
|
+
/** Stop the camera this negotiator opened (a stream passed in stays the caller's). */
|
|
896
|
+
releaseCamera() {
|
|
897
|
+
if (this.ownsLocalStream && this._localStream) stopTracks(this._localStream);
|
|
898
|
+
this.ownsLocalStream = false;
|
|
899
|
+
}
|
|
900
|
+
setPhase(phase) {
|
|
901
|
+
if (this._phase === phase || this.closed) return;
|
|
902
|
+
this._phase = phase;
|
|
903
|
+
this.emit({ type: "phase", phase });
|
|
904
|
+
}
|
|
905
|
+
emit(event) {
|
|
906
|
+
for (const listener of this.listeners) listener(event);
|
|
907
|
+
}
|
|
908
|
+
}, _class4);
|
|
909
|
+
function stopTracks(stream) {
|
|
910
|
+
for (const track of stream.getTracks()) track.stop();
|
|
911
|
+
}
|
|
912
|
+
function errorMessage(error) {
|
|
913
|
+
return error instanceof Error ? error.message : String(error);
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
303
928
|
|
|
304
929
|
|
|
305
930
|
|
|
306
|
-
exports.AudioPlayback = AudioPlayback; exports.MicrophoneCapture = MicrophoneCapture; exports.startVoice = startVoice;
|
|
931
|
+
exports.AudioPlayback = AudioPlayback; exports.ControlChannel = ControlChannel; exports.DEFAULT_ICE_SERVERS = DEFAULT_ICE_SERVERS; exports.ICE_QUIET_PERIOD_MS = ICE_QUIET_PERIOD_MS; exports.ICE_TIMEOUT_MS = ICE_TIMEOUT_MS; exports.MAX_QUEUED_MESSAGES = MAX_QUEUED_MESSAGES; exports.MicrophoneCapture = MicrophoneCapture; exports.REPORTED_CHANNEL_EVENTS = REPORTED_CHANNEL_EVENTS; exports.RESTART_GRACE_MS = RESTART_GRACE_MS; exports.VideoSession = VideoSession; exports.attachVideo = attachVideo; exports.candidateType = candidateType; exports.countTurnServers = countTurnServers; exports.gatherIceCandidates = gatherIceCandidates; exports.startVoice = startVoice;
|
|
307
932
|
//# sourceMappingURL=index.cjs.map
|