@reactor-team/js-sdk 3.3.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -248,6 +248,63 @@ var Emitter = class {
248
248
  }
249
249
  };
250
250
 
251
+ // src/internal/encoded-frames.ts
252
+ var CODECS = /* @__PURE__ */ new Set(["h264", "vp8", "vp9", "av1"]);
253
+ function isEncodedFrame(frame) {
254
+ return typeof frame === "object" && frame !== null && "codec" in frame;
255
+ }
256
+ function isChunk(data) {
257
+ return !(data instanceof Uint8Array) && !(data instanceof ArrayBuffer) && typeof data.copyTo === "function";
258
+ }
259
+ function toEncodedBytes(frame, operation, trackName) {
260
+ const refuse = (why) => new InvalidStateError(`${operation}('${trackName}'): ${why}`, { operation });
261
+ if (typeof frame !== "object" || frame === null) {
262
+ throw refuse("expected an EncodedFrame, { codec, data, width, height }");
263
+ }
264
+ const { codec, data, width, height, metadata, captureTimeUs, keyframe } = frame;
265
+ if (!CODECS.has(codec)) {
266
+ throw refuse(`unknown codec ${JSON.stringify(codec)}; expected "h264", "vp8", "vp9" or "av1"`);
267
+ }
268
+ if (keyframe != null && typeof keyframe !== "boolean") {
269
+ throw refuse("keyframe must be a boolean");
270
+ }
271
+ let bytes;
272
+ if (data instanceof Uint8Array) {
273
+ bytes = data;
274
+ } else if (data instanceof ArrayBuffer) {
275
+ bytes = new Uint8Array(data);
276
+ } else if (data && isChunk(data)) {
277
+ bytes = new Uint8Array(data.byteLength);
278
+ data.copyTo(bytes);
279
+ } else {
280
+ throw refuse("data must be a Uint8Array, an ArrayBuffer or an EncodedVideoChunk");
281
+ }
282
+ if (bytes.byteLength === 0) {
283
+ throw refuse("an encoded frame needs data");
284
+ }
285
+ if (!isDimension(width) || !isDimension(height)) {
286
+ throw refuse(`width and height are the decoded frame's, got ${String(width)}x${String(height)}`);
287
+ }
288
+ if (metadata != null && !(metadata instanceof Uint8Array)) {
289
+ throw refuse("metadata must be a Uint8Array");
290
+ }
291
+ if (captureTimeUs != null && !Number.isFinite(captureTimeUs)) {
292
+ throw refuse("captureTimeUs must be a finite number");
293
+ }
294
+ return {
295
+ codec,
296
+ data: bytes,
297
+ width,
298
+ height,
299
+ metadata: metadata ?? null,
300
+ captureTimeUs: captureTimeUs ?? null,
301
+ keyframe: keyframe ?? (isChunk(data) && data.type === "key")
302
+ };
303
+ }
304
+ function isDimension(value) {
305
+ return typeof value === "number" && Number.isInteger(value) && value > 0 && value <= 4294967295;
306
+ }
307
+
251
308
  // src/file-ref.ts
252
309
  var FileRef = class {
253
310
  constructor(uploadId, name, mimeType, size) {
@@ -458,6 +515,574 @@ function toPublicClip(clip) {
458
515
  };
459
516
  }
460
517
 
518
+ // src/internal/step-media.ts
519
+ function holdStepMedia(media) {
520
+ if (typeof media !== "object" || media === null || Array.isArray(media)) {
521
+ throw new InvalidStateError("step(): media must map track names to arrays of frames", {
522
+ operation: "step"
523
+ });
524
+ }
525
+ const held = [];
526
+ try {
527
+ for (const [trackName, frames] of Object.entries(media)) {
528
+ if (!Array.isArray(frames)) {
529
+ throw new InvalidStateError(`step(): media['${trackName}'] must be an array of frames`, {
530
+ operation: "step"
531
+ });
532
+ }
533
+ for (const frame of frames) {
534
+ held.push(holdStepFrame(trackName, frame));
535
+ }
536
+ }
537
+ } catch (cause) {
538
+ for (const frame of held) {
539
+ frame.release();
540
+ }
541
+ throw cause;
542
+ }
543
+ return held;
544
+ }
545
+ function holdStepFrame(track, frame) {
546
+ if (isEncodedFrame(frame)) {
547
+ const operation = "pushEncodedFrame";
548
+ return { track, kind: "encoded", operation, encoded: toEncodedBytes(frame, operation, track), release: () => {
549
+ } };
550
+ }
551
+ if (typeof frame !== "object" || frame === null) {
552
+ throw new InvalidStateError(`pushFrame('${track}'): expected a frame`, { operation: "pushFrame" });
553
+ }
554
+ const held = holdFrame(frame);
555
+ return { track, kind: "raw", operation: "pushFrame", frame: held.frame, release: held.release };
556
+ }
557
+ async function readStepFrame(frame) {
558
+ const { track } = frame;
559
+ if (frame.kind === "encoded") {
560
+ return { track, kind: "encoded", operation: frame.operation, encoded: frame.encoded };
561
+ }
562
+ if (isAudioFrame(frame.frame)) {
563
+ return { track, kind: "audio", operation: frame.operation, audio: frame.frame };
564
+ }
565
+ return { track, kind: "video", operation: frame.operation, pixels: await toPixels(frame.frame) };
566
+ }
567
+ function checkStepFrame(client, frame) {
568
+ if (frame.kind === "encoded") {
569
+ const { codec, data, width, height, captureTimeUs } = frame.encoded;
570
+ client.checkEncodedVideoFrame(frame.track, codec, data.byteLength, width, height, captureTimeUs);
571
+ } else if (frame.kind === "video") {
572
+ const { data, width, height, format } = frame.pixels;
573
+ client.checkVideoFrame(frame.track, data.byteLength, width, height, format);
574
+ } else {
575
+ client.checkAudioFrame(frame.track, frame.audio.samples, frame.audio.channels ?? 1);
576
+ }
577
+ }
578
+
579
+ // src/internal/bt601.ts
580
+ var BT601_LIMITED = {
581
+ matrix: "smpte170m",
582
+ primaries: "smpte170m",
583
+ transfer: "smpte170m",
584
+ fullRange: false
585
+ };
586
+ function clamp(value) {
587
+ return value < 0 ? 0 : value > 255 ? 255 : value;
588
+ }
589
+ function rgbToI420(pixels) {
590
+ const { width, height, data } = pixels;
591
+ const step = pixels.format === "rgb" ? 3 : 4;
592
+ const [ri, bi] = pixels.format === "bgra" ? [2, 0] : [0, 2];
593
+ const codedWidth = width + (width & 1);
594
+ const codedHeight = height + (height & 1);
595
+ const chromaWidth = codedWidth >> 1;
596
+ const chromaHeight = codedHeight >> 1;
597
+ const ySize = codedWidth * codedHeight;
598
+ const out = new Uint8Array(ySize + 2 * chromaWidth * chromaHeight);
599
+ const uOffset = ySize;
600
+ const vOffset = ySize + chromaWidth * chromaHeight;
601
+ const at = (x, y) => (Math.min(y, height - 1) * width + Math.min(x, width - 1)) * step;
602
+ for (let y = 0; y < codedHeight; y++) {
603
+ for (let x = 0; x < codedWidth; x++) {
604
+ const i = at(x, y);
605
+ const r = data[i + ri];
606
+ const g = data[i + 1];
607
+ const b = data[i + bi];
608
+ out[y * codedWidth + x] = 66 * r + 129 * g + 25 * b + 4224 >> 8 & 255;
609
+ }
610
+ }
611
+ for (let cy = 0; cy < chromaHeight; cy++) {
612
+ for (let cx = 0; cx < chromaWidth; cx++) {
613
+ let r = 0;
614
+ let g = 0;
615
+ let b = 0;
616
+ for (const [dx, dy] of [
617
+ [0, 0],
618
+ [1, 0],
619
+ [0, 1],
620
+ [1, 1]
621
+ ]) {
622
+ const i = at(2 * cx + dx, 2 * cy + dy);
623
+ r += data[i + ri];
624
+ g += data[i + 1];
625
+ b += data[i + bi];
626
+ }
627
+ r = r + 2 >> 2;
628
+ g = g + 2 >> 2;
629
+ b = b + 2 >> 2;
630
+ out[uOffset + cy * chromaWidth + cx] = 112 * b - 74 * g - 38 * r + 32896 >> 8;
631
+ out[vOffset + cy * chromaWidth + cx] = 112 * r - 94 * g - 18 * b + 32896 >> 8;
632
+ }
633
+ }
634
+ return { data: out, width, height, codedWidth, codedHeight };
635
+ }
636
+ function yuvToRgb(data, { format, width, height, planes }) {
637
+ const [yPlane, uPlane, vPlane] = planes;
638
+ if (yPlane === void 0 || uPlane === void 0 || format === "I420" && vPlane === void 0) {
639
+ throw new Error(`a ${format} picture needs ${format === "I420" ? 3 : 2} planes, got ${planes.length}`);
640
+ }
641
+ const rgb = new Uint8Array(width * height * 3);
642
+ for (let y = 0; y < height; y++) {
643
+ const cy = y >> 1;
644
+ for (let x = 0; x < width; x++) {
645
+ const cx = x >> 1;
646
+ const luma = data[yPlane.offset + y * yPlane.stride + x];
647
+ let u;
648
+ let v;
649
+ if (format === "NV12") {
650
+ u = data[uPlane.offset + cy * uPlane.stride + 2 * cx];
651
+ v = data[uPlane.offset + cy * uPlane.stride + 2 * cx + 1];
652
+ } else {
653
+ u = data[uPlane.offset + cy * uPlane.stride + cx];
654
+ v = data[vPlane.offset + cy * vPlane.stride + cx];
655
+ }
656
+ const c = 1.164 * (luma - 16);
657
+ const d = u - 128;
658
+ const e = v - 128;
659
+ const j = (y * width + x) * 3;
660
+ rgb[j] = clamp(Math.round(c + 1.596 * e));
661
+ rgb[j + 1] = clamp(Math.round(c - 0.391 * d - 0.813 * e));
662
+ rgb[j + 2] = clamp(Math.round(c + 2.018 * d));
663
+ }
664
+ }
665
+ return rgb;
666
+ }
667
+
668
+ // src/internal/step-codecs.ts
669
+ var STEP_CODECS = ["h264", "vp9", "av1", "vp8"];
670
+ var DECODE_TIMEOUT_MS = 1e4;
671
+ function codecString(codec) {
672
+ switch (codec) {
673
+ case "h264":
674
+ return "avc1.42e033";
675
+ case "vp8":
676
+ return "vp8";
677
+ case "vp9":
678
+ return "vp09.00.51.08";
679
+ case "av1":
680
+ return "av01.0.13M.08";
681
+ }
682
+ }
683
+ function encoderConfig(codec, width, height) {
684
+ return {
685
+ codec: codecString(codec),
686
+ width,
687
+ height,
688
+ // Rate-controlled, as a real-time encoder is: enough for a still image
689
+ // to come back close, small enough to keep a step's message small.
690
+ bitrate: Math.min(2e7, Math.max(3e5, width * height * 4)),
691
+ framerate: 30,
692
+ latencyMode: "realtime",
693
+ ...codec === "h264" ? { avc: { format: "annexb" } } : {}
694
+ };
695
+ }
696
+ function decoderConfig(codec) {
697
+ return { codec: codecString(codec), optimizeForLatency: true };
698
+ }
699
+ async function probeStepCodecs(api = globalThis) {
700
+ const supported = async (probe) => {
701
+ const results = await Promise.all(STEP_CODECS.map((codec) => probe(codec).catch(() => false)));
702
+ return STEP_CODECS.filter((_, i) => results[i]);
703
+ };
704
+ const { VideoEncoder: Encoder, VideoDecoder: Decoder, VideoFrame: Frame, EncodedVideoChunk: Chunk } = api;
705
+ const [encode, decode] = await Promise.all([
706
+ Encoder && Frame ? supported(async (codec) => (await Encoder.isConfigSupported(encoderConfig(codec, 1280, 720))).supported === true) : [],
707
+ Decoder && Chunk ? supported(async (codec) => (await Decoder.isConfigSupported(decoderConfig(codec))).supported === true) : []
708
+ ]);
709
+ return { encode, decode };
710
+ }
711
+ var TrackEncoder = class {
712
+ constructor(api, codec, width, height) {
713
+ this.api = api;
714
+ this.codec = codec;
715
+ this.width = width;
716
+ this.height = height;
717
+ this.encoder = new api.VideoEncoder({
718
+ output: (chunk) => {
719
+ const data = new Uint8Array(chunk.byteLength);
720
+ chunk.copyTo(data);
721
+ this.outputs.push({ data, keyframe: chunk.type === "key" });
722
+ },
723
+ error: (error) => {
724
+ this.failure = error instanceof Error ? error : new Error(String(error));
725
+ }
726
+ });
727
+ try {
728
+ this.encoder.configure(encoderConfig(codec, width, height));
729
+ } catch (cause) {
730
+ this.close();
731
+ throw cause;
732
+ }
733
+ }
734
+ api;
735
+ codec;
736
+ width;
737
+ height;
738
+ encoder;
739
+ outputs = [];
740
+ failure;
741
+ started = false;
742
+ timestampUs = 0;
743
+ /** Encode `pixels` as the stream's next frame, a keyframe when `keyframe`
744
+ * or when it is the stream's first, and hand back what the encoder made
745
+ * of it: every frame is flushed out before the next goes in. */
746
+ async encode(pixels, keyframe) {
747
+ const i420 = rgbToI420(pixels);
748
+ const frame = new this.api.VideoFrame(i420.data, {
749
+ format: "I420",
750
+ codedWidth: i420.codedWidth,
751
+ codedHeight: i420.codedHeight,
752
+ visibleRect: { x: 0, y: 0, width: i420.width, height: i420.height },
753
+ colorSpace: BT601_LIMITED,
754
+ timestamp: this.timestampUs
755
+ });
756
+ this.timestampUs += 33333;
757
+ try {
758
+ this.encoder.encode(frame, { keyFrame: keyframe || !this.started });
759
+ } finally {
760
+ frame.close();
761
+ }
762
+ this.started = true;
763
+ await this.encoder.flush();
764
+ if (this.failure !== void 0) {
765
+ throw this.failure;
766
+ }
767
+ return this.outputs.splice(0);
768
+ }
769
+ close() {
770
+ if (this.encoder.state !== "closed") {
771
+ this.encoder.close();
772
+ }
773
+ }
774
+ };
775
+ var InputEncoders = class {
776
+ constructor(api = globalThis) {
777
+ this.api = api;
778
+ }
779
+ api;
780
+ encoders = /* @__PURE__ */ new Map();
781
+ /** Whether this browser can encode at all. */
782
+ get available() {
783
+ return this.api.VideoEncoder !== void 0 && this.api.VideoFrame !== void 0;
784
+ }
785
+ /** Encode `pixels` as `track`'s next frame in `codec`. A track whose codec
786
+ * or size changed starts a new stream, at a keyframe. On a failure the
787
+ * track's encoder is dropped, so its next frame is a keyframe, and the
788
+ * error is thrown for the caller to send the frame raw instead. */
789
+ async encode(track, codec, pixels, keyframe) {
790
+ const { VideoEncoder: Encoder, VideoFrame: Frame } = this.api;
791
+ if (!Encoder || !Frame) {
792
+ throw new Error("this browser has no WebCodecs VideoEncoder");
793
+ }
794
+ let encoder = this.encoders.get(track);
795
+ if (encoder && (encoder.codec !== codec || encoder.width !== pixels.width || encoder.height !== pixels.height)) {
796
+ encoder.close();
797
+ encoder = void 0;
798
+ }
799
+ try {
800
+ if (!encoder) {
801
+ encoder = new TrackEncoder({ VideoEncoder: Encoder, VideoFrame: Frame }, codec, pixels.width, pixels.height);
802
+ this.encoders.set(track, encoder);
803
+ }
804
+ return await encoder.encode(pixels, keyframe);
805
+ } catch (cause) {
806
+ this.drop(track);
807
+ throw cause;
808
+ }
809
+ }
810
+ /** Drop `track`'s encoder: its next frame starts a new stream. */
811
+ drop(track) {
812
+ this.encoders.get(track)?.close();
813
+ this.encoders.delete(track);
814
+ }
815
+ reset() {
816
+ for (const track of [...this.encoders.keys()]) {
817
+ this.drop(track);
818
+ }
819
+ }
820
+ };
821
+ var TrackDecoder = class {
822
+ constructor(api, codec) {
823
+ this.codec = codec;
824
+ this.Chunk = api.EncodedVideoChunk;
825
+ this.decoder = new api.VideoDecoder({
826
+ output: (frame) => this.deliver(frame),
827
+ error: (error) => this.fail(error)
828
+ });
829
+ try {
830
+ this.decoder.configure(decoderConfig(codec));
831
+ } catch (cause) {
832
+ this.close();
833
+ throw cause;
834
+ }
835
+ }
836
+ codec;
837
+ decoder;
838
+ Chunk;
839
+ /** Resolvers of the frames handed in and not yet handed back, by the
840
+ * timestamp each chunk was given, in the order they were handed in. */
841
+ pending = /* @__PURE__ */ new Map();
842
+ /** The next chunk's timestamp: a count, so each output names the one chunk
843
+ * it was made of, whatever capture time the frame carried. */
844
+ nextTimestamp = 0;
845
+ failed = false;
846
+ /** Hand `frame` to the chunk it was made of, by its timestamp. A step's
847
+ * stream is decoded in order, with no frame reordered, so a chunk handed
848
+ * in before that one with no picture yet made none (a frame that is not
849
+ * shown): it resolves with none rather than wait for the timeout, and no
850
+ * picture is ever matched to a chunk it was not made of. */
851
+ deliver(frame) {
852
+ const entry = this.pending.get(frame.timestamp);
853
+ if (!entry) {
854
+ frame.close();
855
+ return;
856
+ }
857
+ for (const [timestamp, earlier] of this.pending) {
858
+ if (timestamp === frame.timestamp) {
859
+ break;
860
+ }
861
+ this.pending.delete(timestamp);
862
+ earlier.resolve(void 0);
863
+ }
864
+ this.pending.delete(frame.timestamp);
865
+ entry.resolve(frame);
866
+ }
867
+ /** Hand a chunk of `data` to the decoder now, and resolve with the frame it
868
+ * makes of it, or `undefined` for one it shows none of. Called
869
+ * synchronously for every frame of a result, so the decoder takes them in
870
+ * the order results arrive. */
871
+ decode(type, data) {
872
+ const timestamp = this.nextTimestamp++;
873
+ const decoded = new Promise((resolve, reject) => {
874
+ this.pending.set(timestamp, { resolve, reject });
875
+ });
876
+ try {
877
+ this.decoder.decode(new this.Chunk({ type, timestamp, data }));
878
+ } catch (cause) {
879
+ this.fail(cause);
880
+ }
881
+ return withTimeout(decoded, DECODE_TIMEOUT_MS, () => {
882
+ this.fail(new Error(`the ${this.codec} decoder handed back no frame in ${DECODE_TIMEOUT_MS} ms`));
883
+ });
884
+ }
885
+ /** The decoder lost its state, or is given up: every frame still in it
886
+ * fails, so no step waits on it. */
887
+ fail(cause) {
888
+ this.failed = true;
889
+ const pending = [...this.pending.values()];
890
+ this.pending.clear();
891
+ for (const next of pending) {
892
+ next.reject(cause);
893
+ }
894
+ this.close();
895
+ }
896
+ close() {
897
+ if (this.decoder.state !== "closed") {
898
+ this.decoder.close();
899
+ }
900
+ }
901
+ };
902
+ function withTimeout(promise, ms, onTimeout) {
903
+ let timer;
904
+ const timeout = new Promise((_, reject) => {
905
+ timer = setTimeout(() => {
906
+ onTimeout();
907
+ reject(new Error(`timed out after ${ms} ms`));
908
+ }, ms);
909
+ });
910
+ return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
911
+ }
912
+ var OutputDecoders = class {
913
+ constructor(api = globalThis) {
914
+ this.api = api;
915
+ }
916
+ api;
917
+ decoders = /* @__PURE__ */ new Map();
918
+ awaitingKeyframe = /* @__PURE__ */ new Set();
919
+ lost = [];
920
+ /** Whether a track lost its decoder and waits for a keyframe. */
921
+ needsKeyframe() {
922
+ return this.awaitingKeyframe.size > 0;
923
+ }
924
+ /** The tracks that lost their stream since the last call, each once per
925
+ * loss however many frames it then discards: the caller restarts each
926
+ * one's stream. */
927
+ takeLostTracks() {
928
+ const lost = this.lost;
929
+ this.lost = [];
930
+ return lost;
931
+ }
932
+ /** A step gave up after its request went out: its result, if it still
933
+ * comes, is never fed here, so every stream lost frames. Drop the
934
+ * decoders, and report each of their tracks lost. */
935
+ abandon() {
936
+ for (const [track, decoder] of this.decoders) {
937
+ decoder.fail(new DecodeError(`step(): track '${track}' lost its stream: a step gave up mid-stream`, { operation: "step" }));
938
+ this.lose(track, false);
939
+ }
940
+ this.decoders.clear();
941
+ }
942
+ /** Make `track` wait for a keyframe, and report it lost when it was not
943
+ * waiting already, or when `again` says this is a new loss. */
944
+ lose(track, again) {
945
+ if (!this.awaitingKeyframe.has(track) || again) {
946
+ this.lost.push(track);
947
+ }
948
+ this.awaitingKeyframe.add(track);
949
+ }
950
+ /** `result` with every encoded frame decoded to RGB `(height, width, 3)`
951
+ * `uint8`, as a raw frame comes. Call it as the result arrives: each
952
+ * encoded frame goes into its track's decoder before this returns, so
953
+ * results that arrive one after another are decoded in that order.
954
+ *
955
+ * Every frame of every track is fed to its decoder even after one fails,
956
+ * so the other streams keep up; the first failure rejects, with a
957
+ * `DecodeError`, rather than resolving a partial step. */
958
+ decodeResult(result) {
959
+ if (result.tracks.every((track) => track.frames.every((frame) => !isEncoded(frame)))) {
960
+ return Promise.resolve(result);
961
+ }
962
+ let firstError;
963
+ const tracks = result.tracks.map((track) => {
964
+ const frames = track.frames.map(
965
+ (frame) => this.decodeFrame(track.track, frame).catch((cause) => {
966
+ firstError ??= cause;
967
+ return void 0;
968
+ })
969
+ );
970
+ return Promise.all(frames).then((decoded) => ({
971
+ ...track,
972
+ frames: decoded.filter((frame) => frame !== void 0)
973
+ }));
974
+ });
975
+ return Promise.all(tracks).then((decoded) => {
976
+ if (firstError !== void 0) {
977
+ throw toDecodeError(firstError);
978
+ }
979
+ return { ...result, tracks: decoded };
980
+ });
981
+ }
982
+ /** Decode one frame of `track`; a raw one passes as it is, and one its
983
+ * decoder shows no picture of is `undefined`. Synchronous up to the
984
+ * decoder taking the frame. */
985
+ decodeFrame(track, frame) {
986
+ if (!isEncoded(frame)) {
987
+ return Promise.resolve(frame);
988
+ }
989
+ const codec = frame.encoding;
990
+ const { VideoDecoder: Decoder, EncodedVideoChunk: Chunk } = this.api;
991
+ if (!Decoder || !Chunk) {
992
+ return Promise.reject(new DecodeError(`step(): '${track}' sent ${codec} frames and this browser has no WebCodecs VideoDecoder`, { operation: "step" }));
993
+ }
994
+ let decoder = this.decoders.get(track);
995
+ if (decoder && (decoder.failed || decoder.codec !== codec)) {
996
+ decoder.fail(new DecodeError(`step(): track '${track}' changed codec to ${codec}`, { operation: "step" }));
997
+ this.decoders.delete(track);
998
+ decoder = void 0;
999
+ }
1000
+ if (!decoder) {
1001
+ if (!frame.keyframe) {
1002
+ this.lose(track, false);
1003
+ return Promise.reject(
1004
+ new DecodeError(
1005
+ `step(): track '${track}' has no decoder state for a ${codec} frame that is not a keyframe; waiting for a keyframe`,
1006
+ { operation: "step" }
1007
+ )
1008
+ );
1009
+ }
1010
+ try {
1011
+ decoder = new TrackDecoder({ VideoDecoder: Decoder, EncodedVideoChunk: Chunk }, codec);
1012
+ } catch (cause) {
1013
+ return Promise.reject(toDecodeError(cause));
1014
+ }
1015
+ this.decoders.set(track, decoder);
1016
+ }
1017
+ const live = decoder;
1018
+ return live.decode(frame.keyframe ? "key" : "delta", frame.data).then(
1019
+ async (picture) => {
1020
+ if (frame.keyframe) {
1021
+ this.awaitingKeyframe.delete(track);
1022
+ }
1023
+ return picture === void 0 ? void 0 : { ...frame, ...await rgbOf(picture) };
1024
+ },
1025
+ (cause) => {
1026
+ this.lose(track, frame.keyframe === true);
1027
+ if (this.decoders.get(track) === live) {
1028
+ this.decoders.delete(track);
1029
+ }
1030
+ throw cause;
1031
+ }
1032
+ );
1033
+ }
1034
+ reset() {
1035
+ for (const [track, decoder] of this.decoders) {
1036
+ decoder.fail(new DecodeError(`step(): track '${track}' lost its stream: its connection closed`, { operation: "step" }));
1037
+ }
1038
+ this.decoders.clear();
1039
+ this.awaitingKeyframe.clear();
1040
+ this.lost = [];
1041
+ }
1042
+ };
1043
+ function isEncoded(frame) {
1044
+ return frame.encoding !== "raw" && frame.encoding !== "unspecified";
1045
+ }
1046
+ function toDecodeError(cause) {
1047
+ if (cause instanceof DecodeError) {
1048
+ return cause;
1049
+ }
1050
+ const message = cause instanceof Error ? cause.message : JSON.stringify(cause);
1051
+ return new DecodeError(`step(): ${message}`, { operation: "step", cause });
1052
+ }
1053
+ async function rgbOf(picture) {
1054
+ let pixels;
1055
+ try {
1056
+ const yuv = await yuvOf(picture);
1057
+ if (yuv !== void 0) {
1058
+ return yuv;
1059
+ }
1060
+ pixels = await toPixels(picture);
1061
+ } finally {
1062
+ picture.close();
1063
+ }
1064
+ const { width, height } = pixels;
1065
+ const rgb = new Uint8Array(width * height * 3);
1066
+ const [r, b] = pixels.format === "bgra" ? [2, 0] : [0, 2];
1067
+ for (let i = 0, j = 0; j < rgb.length; i += 4, j += 3) {
1068
+ rgb[j] = pixels.data[i + r];
1069
+ rgb[j + 1] = pixels.data[i + 1];
1070
+ rgb[j + 2] = pixels.data[i + b];
1071
+ }
1072
+ return { dtype: "uint8", shape: [height, width, 3], data: rgb };
1073
+ }
1074
+ async function yuvOf(picture) {
1075
+ const format = picture.format === "I420A" ? "I420" : picture.format;
1076
+ if (format !== "I420" && format !== "NV12") {
1077
+ return void 0;
1078
+ }
1079
+ const width = picture.visibleRect?.width ?? picture.codedWidth;
1080
+ const height = picture.visibleRect?.height ?? picture.codedHeight;
1081
+ const data = new Uint8Array(picture.allocationSize());
1082
+ const planes = await picture.copyTo(data);
1083
+ return { dtype: "uint8", shape: [height, width, 3], data: yuvToRgb(data, { format, width, height, planes }) };
1084
+ }
1085
+
461
1086
  // src/internal/stats.ts
462
1087
  var LOCAL_STATS_INTERVAL_MS = 2e3;
463
1088
  var RUNTIME_REPORT_INTERVAL_MS = 5e3;
@@ -785,7 +1410,7 @@ function describeCause(cause) {
785
1410
  // package.json
786
1411
  var package_default = {
787
1412
  name: "@reactor-team/js-sdk",
788
- version: "3.3.0",
1413
+ version: "3.5.0",
789
1414
  description: "Connect your JavaScript/TypeScript app to a live Reactor model in the browser.",
790
1415
  license: "Apache-2.0",
791
1416
  repository: {
@@ -1307,10 +1932,22 @@ var Reactor = class {
1307
1932
  * is finalized on "ready" — see `handleStatusChanged()`. */
1308
1933
  connectStartTime;
1309
1934
  waitingStartTime;
1310
- /** The tail of every `pushFrame()` so far, settled either way — pushes
1311
- * reach the binding in call order however long each one's pixels take to
1312
- * read, and `step()` waits for the ones called before it. */
1935
+ /** The tail of every `pushFrame()`/`pushEncodedFrame()` so far, settled
1936
+ * either way — pushes reach the binding in call order however long each
1937
+ * one's pixels take to read, and `step()` waits for the ones called
1938
+ * before it. */
1313
1939
  pendingPushes = Promise.resolve();
1940
+ /** Stepped: every `setCodecPreference()` so far, keyed by track (`null`
1941
+ * for the session's list), replayed into a client created later. */
1942
+ codecPreferences = /* @__PURE__ */ new Map();
1943
+ /** Stepped: what this browser's WebCodecs can encode and decode, probed
1944
+ * once, on the first stepped connect. */
1945
+ localStepCodecs;
1946
+ /** Stepped: the input tracks' encoders and the output tracks' decoders of
1947
+ * the current connection, dropped as it ends so the next one's streams
1948
+ * start at keyframes. */
1949
+ inputEncoders = new InputEncoders();
1950
+ outputDecoders = new OutputDecoders();
1314
1951
  emitter = new Emitter();
1315
1952
  /** Serializes connect()/reconnect()/disconnect() (and the free() inside
1316
1953
  * disconnect()/[Symbol.dispose]) against each other. Calling into the
@@ -1418,6 +2055,7 @@ var Reactor = class {
1418
2055
  await this.queue.push(
1419
2056
  () => this.withWriterAccess(async () => {
1420
2057
  const client = await this.getOrCreateClient();
2058
+ await this.offerStepCodecs(client);
1421
2059
  await client.connect(options);
1422
2060
  }),
1423
2061
  "connect"
@@ -1472,6 +2110,7 @@ var Reactor = class {
1472
2110
  await this.queue.push(
1473
2111
  () => this.withWriterAccess(async () => {
1474
2112
  const client = await this.getOrCreateClient();
2113
+ await this.offerStepCodecs(client);
1475
2114
  await client.reconnect(options);
1476
2115
  }),
1477
2116
  "reconnect"
@@ -1650,7 +2289,8 @@ var Reactor = class {
1650
2289
  }, "publishTrack");
1651
2290
  return {
1652
2291
  name,
1653
- pushFrame: (frame, options) => this.pushTo(name, publication, frame, options)
2292
+ pushFrame: (frame, options) => this.pushTo(name, publication, frame, options),
2293
+ pushEncodedFrame: (frame) => this.pushEncodedTo(name, publication, frame)
1654
2294
  };
1655
2295
  } catch (cause) {
1656
2296
  throw this.captureError(cause);
@@ -1746,10 +2386,10 @@ var Reactor = class {
1746
2386
  * Runs one step of a `generation: "stepped"` session and resolves with
1747
2387
  * what it produced, per output track.
1748
2388
  *
1749
- * The request carries `state`, `commands` and every frame pushed since the
1750
- * previous step. The runtime applies them in that order — each `state` key
1751
- * as the model's `set_<key>` command, then `commands`, then the frames —
1752
- * and then generates. They are separate calls, not a transaction: one that
2389
+ * The request carries `state` and `commands`; the frames pushed before it
2390
+ * have already reached the runtime (see `pushFrame()`). The runtime applies
2391
+ * them in that order — each `state` key as the model's `set_<key>` command,
2392
+ * then `commands` — and then generates. They are separate calls, not a transaction: one that
1753
2393
  * fails does not undo the ones before it. All of it is optional:
1754
2394
  * `step()` with nothing set and nothing pushed runs on the model's current
1755
2395
  * state, which commands sent outside a step (`sendCommand()`) also change.
@@ -1760,7 +2400,9 @@ var Reactor = class {
1760
2400
  * A step that ran but that the model reported as failed still resolves,
1761
2401
  * with `result.error` set. `result.files` lists the files the model kept
1762
2402
  * with the step (name, content type, size); their bytes are served over
1763
- * HTTP from the session's step results, not carried here.
2403
+ * HTTP from the session's step results, not carried here. `result.data`
2404
+ * holds what the model returned besides its media, as opaque bytes (a dict
2405
+ * arrives encoded as JSON); it is absent when the model returned none.
1764
2406
  *
1765
2407
  * Rejects with a `StepRefusedError` when the runtime or the model turns
1766
2408
  * the step down (the runtime checks a whole request first, so nothing it
@@ -1774,10 +2416,34 @@ var Reactor = class {
1774
2416
  * message, `MessageTooLargeError` when the request itself is too large
1775
2417
  * (nothing was sent), `InvalidStateError` outside a ready stepped
1776
2418
  * session, or whatever else failed the step (`DisconnectedError`,
1777
- * `RequestTimeoutError`, ...). The frames pushed for a step belong to it:
2419
+ * `RequestTimeoutError`, ...). The frames buffered for a step belong to it:
1778
2420
  * when its request cannot be sent, they are dropped with it rather than
1779
2421
  * kept for the next step.
1780
2422
  *
2423
+ * `media` gives the step frames of its own, by input track name, pushed
2424
+ * after the ones already pushed to that track: what `pushFrame()` takes,
2425
+ * or an `EncodedFrame` — H.264 the runtime decodes before the step runs,
2426
+ * so the model reads RGB frames as for a raw push. They are pushed only
2427
+ * once `state` and `commands` are well formed and the session is ready, so
2428
+ * a step refused for those leaves the frames buffered as they were. They go
2429
+ * all or nothing: a bad track or frame anywhere in `media` rejects the step
2430
+ * before any of it is pushed or sent, leaving the buffer as it was. A
2431
+ * session that leaves 'ready' once the media is in has the step refused and
2432
+ * keeps the frames for the next one, as pushed frames are.
2433
+ * A track's encoded frames go in decode order, starting at a keyframe; a
2434
+ * step whose buffered frames do not decode rejects with a
2435
+ * `StepRefusedError` (see `pushEncodedFrame()` for frames already sent).
2436
+ *
2437
+ * Video step frames travel encoded both ways when this browser and the
2438
+ * runtime share a codec (H.264 first by default; see
2439
+ * `setCodecPreference()`), on WebCodecs: the frames pushed on an input
2440
+ * track are encoded, and an output track's frames are decoded, so a result
2441
+ * still holds RGB frames whose `encoding` names the codec they travelled
2442
+ * in. Each track is one stream for the connection. When one of its
2443
+ * decoders lost its stream, the step rejects with a `DecodeError`, the SDK
2444
+ * pauses and resumes that track so the runtime starts it again at a
2445
+ * keyframe, and the track's frames fail until that keyframe arrives.
2446
+ *
1781
2447
  * ```ts
1782
2448
  * const camera = await reactor.publishTrack('camera');
1783
2449
  * await camera.pushFrame(imageData);
@@ -1791,24 +2457,53 @@ var Reactor = class {
1791
2457
  this.assertNotDisposed();
1792
2458
  try {
1793
2459
  const state = rewriteNestedFileRefs(options.state ?? null);
1794
- const commands = (options.commands ?? []).map(
1795
- (command) => command.data ? { ...command, data: rewriteNestedFileRefs(command.data) } : command
1796
- );
1797
- await this.pendingPushes;
1798
- return await this.withReaderAccess(async () => {
1799
- const client = await this.getOrCreateClient();
1800
- return client.step(state, commands);
2460
+ const commands = rewriteNestedFileRefs(options.commands ?? []);
2461
+ const previous = this.pendingPushes;
2462
+ const before = options.media !== void 0 ? this.pushStepMedia(options.media, state, commands) : previous;
2463
+ let taken;
2464
+ const bufferTaken = new Promise((resolve) => {
2465
+ taken = resolve;
1801
2466
  });
2467
+ const stepping = before.then(
2468
+ () => (
2469
+ // Excluded from connect()/disconnect()/reconnect() like sendCommand():
2470
+ // a step can take minutes, and must neither be freed out from under
2471
+ // nor block unrelated reads.
2472
+ this.withReaderAccess(async () => {
2473
+ const client = await this.getOrCreateClient();
2474
+ try {
2475
+ const decoders = this.outputDecoders;
2476
+ return client.step(state, commands).then(
2477
+ (result) => decoders.decodeResult(result).finally(() => this.restartLostTracks(client, decoders)),
2478
+ (cause) => {
2479
+ decoders.abandon();
2480
+ this.restartLostTracks(client, decoders);
2481
+ throw cause;
2482
+ }
2483
+ );
2484
+ } finally {
2485
+ taken();
2486
+ }
2487
+ })
2488
+ )
2489
+ );
2490
+ stepping.catch(() => previous.then(taken));
2491
+ this.pendingPushes = bufferTaken;
2492
+ return await stepping;
1802
2493
  } catch (cause) {
1803
2494
  throw this.captureError(cause);
1804
2495
  }
1805
2496
  }
1806
2497
  /**
1807
- * Buffers one input frame for the next `step()` on the sendonly track
1808
- * `trackName`. Nothing is sent until that step, which carries every frame
1809
- * pushed since the one before it, per track in push order. The
1810
- * `PublishedTrack` that `publishTrack()` resolves with does the same
1811
- * through `track.pushFrame(frame)`.
2498
+ * Pushes one input frame on the sendonly track `trackName` of a stepped
2499
+ * session. Once the session is ready, against a runtime that takes pushed
2500
+ * frames, the frame is sent to the runtime at once over the data channel,
2501
+ * ahead of any `step()` after it; one the channel cannot take is dropped
2502
+ * and raised as an error event. A frame pushed before the session is ready,
2503
+ * or to an older runtime, is buffered and sent with the next push or
2504
+ * `step()`, per track in push order. The `PublishedTrack` that
2505
+ * `publishTrack()` resolves with does the same through
2506
+ * `track.pushFrame(frame)`.
1812
2507
  *
1813
2508
  * Video: a `VideoFrame` (cloned when called, so the caller may close it as
1814
2509
  * soon as this returns; it stays the caller's), an
@@ -1819,9 +2514,15 @@ var Reactor = class {
1819
2514
  * Audio: `{ samples, sampleRate, channels? }`, interleaved `Int16Array`
1820
2515
  * PCM at the rate the model's track declares.
1821
2516
  *
2517
+ * A video frame travels encoded when the connection negotiated a codec for
2518
+ * the track (see `setCodecPreference()`): it is encoded here, with
2519
+ * WebCodecs, as the next frame of the track's stream, and the model still
2520
+ * reads RGB. A track's first frame on a connection is a keyframe, and so
2521
+ * is the next one after the runtime could not decode its frames.
2522
+ *
1822
2523
  * Pushes keep their call order, and a `step()` waits for every push called
1823
2524
  * before it. A refused push rejects: await it, or `.catch()` it in a frame
1824
- * loop, or every bad frame is an unhandled rejection. What waits for
2525
+ * loop, or every bad frame is an unhandled rejection. What is buffered for
1825
2526
  * the next step is at most one message's worth of frames: pushing faster
1826
2527
  * than steps run drops the oldest frames, across tracks. Rejects with
1827
2528
  * an `InvalidStateError` outside a stepped session, for a track not
@@ -1857,29 +2558,16 @@ var Reactor = class {
1857
2558
  held.release();
1858
2559
  }
1859
2560
  await this.withReaderAccess(async () => {
1860
- if (epoch !== this.connectionEpoch) {
1861
- throw new InvalidStateError(
1862
- `pushFrame('${trackName}'): the connection it was pushed to ended while its frame was read`,
1863
- { operation: "pushFrame" }
1864
- );
1865
- }
1866
- const current = this.publishedInputs.get(trackName);
1867
- if (current === void 0 || publication !== void 0 && publication !== current) {
1868
- throw new InvalidStateError(
1869
- publication === void 0 ? `pushFrame('${trackName}') needs publishTrack('${trackName}') first.` : `pushFrame('${trackName}'): this track was unpublished, or its connection ended.`,
1870
- { operation: "pushFrame" }
1871
- );
1872
- }
2561
+ this.checkPushTarget("pushFrame", trackName, publication, epoch);
1873
2562
  const client = await this.getOrCreateClient();
1874
2563
  if (pixels) {
1875
- client.pushVideoFrame(
2564
+ await this.pushPixels(
2565
+ client,
1876
2566
  trackName,
1877
- pixels.data,
1878
- pixels.width,
1879
- pixels.height,
1880
- pixels.format,
2567
+ pixels,
1881
2568
  options.metadata ?? null,
1882
- options.captureTimeUs ?? null
2569
+ options.captureTimeUs ?? null,
2570
+ () => this.checkPushTarget("pushFrame", trackName, publication, epoch)
1883
2571
  );
1884
2572
  } else if (isAudioFrame(frame)) {
1885
2573
  client.pushAudioFrame(trackName, frame.samples, frame.sampleRate, frame.channels ?? 1);
@@ -1893,8 +2581,273 @@ var Reactor = class {
1893
2581
  throw this.captureError(cause);
1894
2582
  }
1895
2583
  }
1896
- /** Drops every frame buffered for the next step, pushes called before it
1897
- * included: it waits its turn behind them, as a `step()` does. */
2584
+ /**
2585
+ * Pushes one encoded video frame, sent or buffered as `pushFrame()` does,
2586
+ * on the sendonly video track `trackName`, its bytes as they are: one
2587
+ * H.264 access unit (Annex B), one VP8 or VP9 frame, or one AV1 temporal
2588
+ * unit, with the `width` and `height` it decodes to. The `PublishedTrack`
2589
+ * that `publishTrack()` resolves with does the same through
2590
+ * `track.pushEncodedFrame(frame)`.
2591
+ *
2592
+ * The runtime decodes a track's encoded frames as one stream, as they
2593
+ * arrive, and the model reads the RGB frames as if they had been pushed
2594
+ * raw. So push them in decode order, starting at a keyframe (`keyframe`, or
2595
+ * a chunk's `type`). A pushed frame that does not decode raises an
2596
+ * `input_decode_failed` error event naming the track, whose next frame
2597
+ * must then be a keyframe; a step whose buffered frames do not decode
2598
+ * rejects with a `StepRefusedError`, as does a runtime that predates
2599
+ * encoded frames. Encoded and raw pushes keep their call order, and a
2600
+ * `step()` waits for every push called before it.
2601
+ *
2602
+ * ```ts
2603
+ * const encoder = new VideoEncoder({
2604
+ * output: (chunk) => void camera.pushEncodedFrame({ codec: 'h264', data: chunk, width, height }),
2605
+ * error: console.error,
2606
+ * });
2607
+ * encoder.configure({ codec: 'avc1.42001f', width, height, avc: { format: 'annexb' } });
2608
+ * ```
2609
+ *
2610
+ * Rejects with an `InvalidStateError` outside a stepped session, for a
2611
+ * track not published on this connection, for an audio or recvonly track,
2612
+ * for empty data, a zero dimension or an unknown codec.
2613
+ */
2614
+ async pushEncodedFrame(trackName, frame) {
2615
+ return this.pushEncodedTo(trackName, void 0, frame);
2616
+ }
2617
+ /** `pushEncodedFrame()`, for the track's current publication or, from a
2618
+ * `PublishedTrack`, only for the `publication` it came from. */
2619
+ async pushEncodedTo(trackName, publication, frame) {
2620
+ const operation = "pushEncodedFrame";
2621
+ this.assertNotDisposed();
2622
+ const epoch = this.connectionEpoch;
2623
+ let encoded;
2624
+ try {
2625
+ encoded = toEncodedBytes(frame, operation, trackName);
2626
+ } catch (cause) {
2627
+ throw this.captureError(cause);
2628
+ }
2629
+ const push = this.pendingPushes.then(
2630
+ () => this.withReaderAccess(async () => {
2631
+ if (!this.isStepped()) {
2632
+ throw new InvalidStateError(
2633
+ `${operation}('${trackName}') needs a session created with generation: "stepped"; a streamed session sends media from a MediaStreamTrack given to publishTrack().`,
2634
+ { operation }
2635
+ );
2636
+ }
2637
+ this.checkPushTarget(operation, trackName, publication, epoch);
2638
+ const client = await this.getOrCreateClient();
2639
+ const declared = client.tracks().find((track) => track.name === trackName);
2640
+ if (declared && (declared.kind !== "video" || declared.direction !== "sendonly")) {
2641
+ throw new InvalidStateError(
2642
+ `${operation}('${trackName}'): '${trackName}' is a ${declared.direction} ${declared.kind} track; only a sendonly video track takes encoded frames.`,
2643
+ { operation }
2644
+ );
2645
+ }
2646
+ client.pushEncodedVideoFrame(
2647
+ trackName,
2648
+ encoded.codec,
2649
+ encoded.data,
2650
+ encoded.width,
2651
+ encoded.height,
2652
+ encoded.metadata,
2653
+ encoded.captureTimeUs,
2654
+ encoded.keyframe
2655
+ );
2656
+ })
2657
+ );
2658
+ this.pendingPushes = push.catch(() => void 0);
2659
+ try {
2660
+ await push;
2661
+ } catch (cause) {
2662
+ throw this.captureError(cause);
2663
+ }
2664
+ }
2665
+ /** Refuses a push whose connection ended since it was called, or whose
2666
+ * track is not (or no longer) published on it. */
2667
+ checkPushTarget(operation, trackName, publication, epoch) {
2668
+ if (epoch !== this.connectionEpoch) {
2669
+ throw new InvalidStateError(
2670
+ `${operation}('${trackName}'): the connection it was pushed to ended while its frame was read`,
2671
+ { operation }
2672
+ );
2673
+ }
2674
+ const current = this.publishedInputs.get(trackName);
2675
+ if (current === void 0 || publication !== void 0 && publication !== current) {
2676
+ throw new InvalidStateError(
2677
+ publication === void 0 ? `${operation}('${trackName}') needs publishTrack('${trackName}') first.` : `${operation}('${trackName}'): this track was unpublished, or its connection ended.`,
2678
+ { operation }
2679
+ );
2680
+ }
2681
+ }
2682
+ /** Hands `pixels` to the binding for `track`: encoded first, on the
2683
+ * track's encoder, when the connection negotiated a codec for the track's
2684
+ * step frames and this browser can encode; raw otherwise, and when the
2685
+ * encoder fails (its next frame is then a keyframe). Checked by the
2686
+ * binding before anything is encoded, so a bad frame is refused either
2687
+ * way. `stillTarget` throws when the push's connection or publication is
2688
+ * gone: asked again once the encode is awaited, since a reconnect can
2689
+ * start meanwhile, and neither the encoded frame nor its raw fallback may
2690
+ * then reach the next session. */
2691
+ async pushPixels(client, track, pixels, metadata, captureTimeUs, stillTarget) {
2692
+ const { data, width, height, format } = pixels;
2693
+ const encoding = this.inputEncoders.available ? client.stepFrameEncodings()[track] : void 0;
2694
+ if (encoding !== void 0 && encoding !== "raw") {
2695
+ client.checkVideoFrame(track, data.byteLength, width, height, format);
2696
+ const keyframe = client.takeInputKeyframeRequest(track);
2697
+ let encoded;
2698
+ try {
2699
+ encoded = await this.inputEncoders.encode(track, encoding, pixels, keyframe);
2700
+ } catch (cause) {
2701
+ console.warn(`[Reactor] pushFrame('${track}'): ${encoding} encoding failed, sending the frame raw:`, cause);
2702
+ }
2703
+ stillTarget();
2704
+ if (encoded !== void 0) {
2705
+ for (const frame of encoded) {
2706
+ client.pushEncodedVideoFrame(track, encoding, frame.data, width, height, metadata, captureTimeUs, frame.keyframe);
2707
+ }
2708
+ return;
2709
+ }
2710
+ }
2711
+ client.pushVideoFrame(track, data, width, height, format, metadata, captureTimeUs);
2712
+ }
2713
+ /** Gives the binding the codecs this browser can carry step frames in,
2714
+ * probed once, so the next offer can negotiate them. A streamed session
2715
+ * carries no step frames. */
2716
+ /** Restart each output track whose decoder lost its stream: a pause and
2717
+ * a resume of that track, so the runtime starts it again at a keyframe. A
2718
+ * restart that cannot be sent is left to the next connection, whose
2719
+ * streams start at keyframes anyway. */
2720
+ restartLostTracks(client, decoders) {
2721
+ for (const track of decoders.takeLostTracks()) {
2722
+ try {
2723
+ client.restartTrackStream(track);
2724
+ } catch (cause) {
2725
+ console.warn(`step(): could not restart track '${track}':`, cause);
2726
+ }
2727
+ }
2728
+ }
2729
+ async offerStepCodecs(client) {
2730
+ if (!this.isStepped()) {
2731
+ return;
2732
+ }
2733
+ this.localStepCodecs ??= probeStepCodecs();
2734
+ const { encode, decode } = await this.localStepCodecs;
2735
+ client.setLocalStepCodecs(encode, decode);
2736
+ }
2737
+ /** Pushes `step()`'s `media`, track by track and in order, each frame as
2738
+ * `pushFrame()` or `pushEncodedFrame()` would. All or nothing: the binding
2739
+ * checks the step's `state` and `commands`, then every frame, before the
2740
+ * first is pushed, so a refused step, or a bad track or frame anywhere,
2741
+ * leaves the buffer as it was. */
2742
+ async pushStepMedia(media, state, commands) {
2743
+ const epoch = this.connectionEpoch;
2744
+ const held = holdStepMedia(media);
2745
+ const push = this.pendingPushes.then(async () => {
2746
+ const ready = [];
2747
+ try {
2748
+ for (const frame of held) {
2749
+ ready.push(await readStepFrame(frame));
2750
+ }
2751
+ } finally {
2752
+ for (const frame of held) {
2753
+ frame.release();
2754
+ }
2755
+ }
2756
+ await this.withReaderAccess(async () => {
2757
+ const client = await this.getOrCreateClient();
2758
+ client.checkStep(state, commands);
2759
+ for (const frame of ready) {
2760
+ this.checkPushTarget(frame.operation, frame.track, void 0, epoch);
2761
+ checkStepFrame(client, frame);
2762
+ }
2763
+ for (const frame of ready) {
2764
+ if (frame.kind === "encoded") {
2765
+ client.pushEncodedVideoFrame(
2766
+ frame.track,
2767
+ frame.encoded.codec,
2768
+ frame.encoded.data,
2769
+ frame.encoded.width,
2770
+ frame.encoded.height,
2771
+ frame.encoded.metadata,
2772
+ frame.encoded.captureTimeUs,
2773
+ frame.encoded.keyframe
2774
+ );
2775
+ } else if (frame.kind === "video") {
2776
+ await this.pushPixels(
2777
+ client,
2778
+ frame.track,
2779
+ frame.pixels,
2780
+ null,
2781
+ null,
2782
+ () => this.checkPushTarget(frame.operation, frame.track, void 0, epoch)
2783
+ );
2784
+ } else {
2785
+ const { samples, sampleRate, channels } = frame.audio;
2786
+ client.pushAudioFrame(frame.track, samples, sampleRate, channels ?? 1);
2787
+ }
2788
+ }
2789
+ });
2790
+ });
2791
+ this.pendingPushes = push.catch(() => void 0);
2792
+ await push;
2793
+ }
2794
+ /**
2795
+ * Chooses the codecs a stepped session carries its video step frames in,
2796
+ * most preferred first, from `"h264"`, `"vp9"`, `"av1"` and `"vp8"`.
2797
+ * `null` goes back to the default, H.264 first. An empty list for the
2798
+ * session turns encoding off, so every frame travels raw:
2799
+ *
2800
+ * ```ts
2801
+ * reactor.setCodecPreference(['vp9', 'h264']); // VP9 when both ends have it
2802
+ * reactor.setCodecPreference([]); // raw frames
2803
+ * reactor.setCodecPreference(['av1'], { track: 'main_video' });
2804
+ * ```
2805
+ *
2806
+ * With `track`, the list applies to that track only, in place of the
2807
+ * session's; an empty one there goes back to the session's, since one
2808
+ * track cannot turn encoding off alone. The list is an order, not a
2809
+ * limit: the codecs it leaves out follow it, so a runtime without the
2810
+ * preferred ones takes another. Of those, the ones this browser's
2811
+ * WebCodecs can encode (input tracks) or decode (output tracks) are
2812
+ * offered, and the runtime takes per track the first it shares; a track
2813
+ * they share no codec for stays raw.
2814
+ *
2815
+ * Whatever the codec, `step()` results and the frames `pushFrame()` takes
2816
+ * stay RGB: the encoding is between this client and the runtime. Callable
2817
+ * before `connect()` or during a session; it takes effect on the next
2818
+ * connection or reconnect, and the live connection keeps what it
2819
+ * negotiated (see `getStepFrameEncodings()`). A streamed session ignores
2820
+ * it. Throws an `InvalidStateError` on a name that is not a codec.
2821
+ */
2822
+ setCodecPreference(codecs, options = {}) {
2823
+ this.assertNotDisposed();
2824
+ const operation = "setCodecPreference";
2825
+ const track = options.track ?? null;
2826
+ if (codecs !== null && (!Array.isArray(codecs) || codecs.some((codec) => !STEP_CODECS.includes(codec)))) {
2827
+ throw this.captureError(
2828
+ new InvalidStateError(
2829
+ `${operation}: expected null or an array of "h264", "vp9", "av1" and "vp8", got ${JSON.stringify(codecs)}`,
2830
+ { operation }
2831
+ )
2832
+ );
2833
+ }
2834
+ if (track !== null && typeof track !== "string") {
2835
+ throw this.captureError(new InvalidStateError(`${operation}: track must be a string`, { operation }));
2836
+ }
2837
+ const list = codecs === null ? null : [...codecs];
2838
+ this.codecPreferences.set(track, list);
2839
+ this.client?.setCodecPreference(list, track);
2840
+ }
2841
+ /** How each track's step frames travel on this connection, as its offer
2842
+ * and answer negotiated them: `"raw"`, or the codec of an encoded video
2843
+ * track. Empty outside a connected stepped session; a track left out is
2844
+ * raw. */
2845
+ getStepFrameEncodings() {
2846
+ return this.client?.stepFrameEncodings() ?? {};
2847
+ }
2848
+ /** Drops every frame still buffered for the next step, pushes called before
2849
+ * it included: it waits its turn behind them, as a `step()` does. Frames
2850
+ * already sent to the runtime are not recalled. */
1898
2851
  async clearStepInputs() {
1899
2852
  this.assertNotDisposed();
1900
2853
  const clear = this.pendingPushes.then(
@@ -2085,6 +3038,9 @@ var Reactor = class {
2085
3038
  throw new Error("Reactor was disposed while connecting.");
2086
3039
  }
2087
3040
  const client = new WasmReactorClient(this.clientOptions, this.jwt);
3041
+ for (const [track, codecs] of this.codecPreferences) {
3042
+ client.setCodecPreference(codecs, track);
3043
+ }
2088
3044
  client.onStatusChanged((status) => {
2089
3045
  this.emitter.emit("statusChanged", status);
2090
3046
  this.handleStatusChanged(client, status);
@@ -2173,6 +3129,11 @@ var Reactor = class {
2173
3129
  * → "ready" status sequence (see `ReactorStatus`), and starts/stops stats
2174
3130
  * polling around the "ready" window. */
2175
3131
  handleStatusChanged(client, status) {
3132
+ if (status === "connecting" || status === "disconnected") {
3133
+ this.resetStepStreams();
3134
+ this.connectionEpoch += 1;
3135
+ this.publishedInputs.clear();
3136
+ }
2176
3137
  switch (status) {
2177
3138
  case "connecting":
2178
3139
  this.connectStartTime = performance.now();
@@ -2264,6 +3225,7 @@ var Reactor = class {
2264
3225
  * leave `getCapabilities()` returning the previous session's stale
2265
3226
  * tracks/commands until a new `capabilitiesReceived` lands. */
2266
3227
  resetConnectionState() {
3228
+ this.resetStepStreams();
2267
3229
  this.connectionEpoch += 1;
2268
3230
  this.publishedInputs.clear();
2269
3231
  this.stopStatsPolling();
@@ -2272,6 +3234,12 @@ var Reactor = class {
2272
3234
  this.waitingStartTime = void 0;
2273
3235
  this.capabilities = void 0;
2274
3236
  }
3237
+ /** Drops every step frame encoder and decoder: each track's stream lived
3238
+ * with the connection that ended, and the next one's start at keyframes. */
3239
+ resetStepStreams() {
3240
+ this.inputEncoders.reset();
3241
+ this.outputDecoders.reset();
3242
+ }
2275
3243
  /** Wraps `cause` and records it as the most recent failure, for a call
2276
3244
  * that's about to throw rather than emit — see `getLastError()`'s doc
2277
3245
  * comment. */