@vindral/web-sdk 4.5.1 → 4.6.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/BddIq5-p.js ADDED
@@ -0,0 +1,154 @@
1
+ import { n as e, t } from "./BsfwXDui.js";
2
+ import { t as n } from "./DrWNJwZx.js";
3
+ import { t as r } from "./BKeRxR4O.js";
4
+ import { i, n as a, t as o } from "./CbyVl6wP.js";
5
+ //#region ../../libs/web-sdk/src/decoders/WebCodecsAudioDecoderContext.ts
6
+ var s, c = globalThis, l = (e) => {
7
+ var t, n;
8
+ if (!e) return {
9
+ description: e,
10
+ preSkip: 0
11
+ };
12
+ let r = e instanceof ArrayBuffer ? new Uint8Array(e) : new Uint8Array(e.buffer, e.byteOffset, e.byteLength);
13
+ if (!(r.byteLength >= 19 && new TextDecoder().decode(r.subarray(0, 8)) === "OpusHead")) return {
14
+ description: e,
15
+ preSkip: 0
16
+ };
17
+ let i = r.slice();
18
+ return i[10] = 0, i[11] = 0, {
19
+ description: i,
20
+ preSkip: ((t = r[10]) == null ? 0 : t) | ((n = r[11]) == null ? 0 : n) << 8
21
+ };
22
+ }, u = class {
23
+ constructor(s) {
24
+ var c = this;
25
+ e(this, "decodedSamples", []), e(this, "decoder", null), e(this, "logger", void 0), e(this, "initSegments", /* @__PURE__ */ new Map()), e(this, "previousSample", void 0), e(this, "previousCodec", void 0), e(this, "pendingSamples", new n(Infinity)), e(this, "priming", { remaining: 0 }), e(this, "sentCount", 0), e(this, "doneCount", 0), e(this, "errors", new n(10)), e(this, "resetDecoderState", () => {
26
+ this.pendingSamples.clear(), this.doneCount = this.sentCount, this.previousSample = void 0, this.previousCodec = void 0;
27
+ }), e(this, "dropUndecoded", (e) => {
28
+ for (; this.doneCount < e && this.pendingSamples.pop();) this.doneCount++;
29
+ }), e(this, "ensureDecoder", () => ((!this.decoder || this.decoder.state === "closed") && (this.decoder = this.createDecoder()), this.decoder)), e(this, "createDecoder", () => new AudioDecoder({
30
+ output: (e) => {
31
+ let t = this.pendingSamples.pop();
32
+ if (!t) {
33
+ e.close();
34
+ return;
35
+ }
36
+ this.doneCount++;
37
+ let { sample: n, decodeStartTime: i, priming: a } = t;
38
+ try {
39
+ let t = e.numberOfFrames, o = e.numberOfChannels, s = e.sampleRate, c = e.duration, l = Math.min(t, Math.round(a.remaining * s / 48e3));
40
+ if (a.remaining = Math.max(0, a.remaining - Math.round(l * 48e3 / s)), l === t && t > 0) return;
41
+ let u = new Float32Array((t - l) * o);
42
+ e.copyTo(u, {
43
+ planeIndex: 0,
44
+ format: "f32",
45
+ frameOffset: l
46
+ });
47
+ let d = l > 0 ? Math.round((t - l) * n.timescale / s) : typeof c == "number" && Number.isFinite(c) ? Math.max(0, Math.round(c * n.timescale / 1e6)) : n.duration, f = r(r({}, n), {}, {
48
+ timestamp: n.timestamp + Math.round(l * n.timescale / s),
49
+ duration: d,
50
+ type: "audio",
51
+ format: "f32",
52
+ data: u,
53
+ sampleRate: s,
54
+ channels: o,
55
+ statistics: {
56
+ decodeTime: Math.max(performance.now() - i, 0),
57
+ transportTimeFromWorker: 0,
58
+ transportTimeToWorker: 0,
59
+ samplesInBatch: 1
60
+ }
61
+ });
62
+ this.decodedSamples.push(f);
63
+ } finally {
64
+ e.close();
65
+ }
66
+ },
67
+ error: (e) => {
68
+ this.logger.error("Decoder error:", e.name, e.message), this.resetDecoderState(), this.errors.push(e);
69
+ }
70
+ })), e(this, "buildCodecString", o), e(this, "getDescription", (e, t) => {
71
+ let n = this.initSegments.get(t.renditionId);
72
+ if (n && n.data.byteLength > 0 && n.codec === e) return n.data;
73
+ if (e === "opus") return a(t.channels, t.sampleRate);
74
+ if (e === "aac") return i(t.channels, t.sampleRate);
75
+ }), e(this, "reset", () => {
76
+ this.decodedSamples = [], this.decoder && this.decoder.state !== "closed" && this.decoder.reset(), this.resetDecoderState();
77
+ }), e(this, "unload", () => {
78
+ this.reset(), this.decoder && this.decoder.state !== "closed" && this.decoder.close(), this.decoder = null;
79
+ }), e(this, "initSegment", (e) => {
80
+ this.initSegments.set(e.renditionId, e);
81
+ }), e(this, "enqueue", (e) => {
82
+ var t, n;
83
+ let r = this.ensureDecoder(), i = e.codec, a = ((t = this.previousSample) == null ? void 0 : t.channelId) !== e.channelId, o = ((n = this.previousSample) == null ? void 0 : n.renditionId) !== e.renditionId, s = this.previousCodec !== i;
84
+ if (!this.previousSample || a || o || s) {
85
+ if (this.previousSample && r.state === "configured") {
86
+ let e = this.sentCount;
87
+ r.flush().then(() => this.dropUndecoded(e), (e) => {
88
+ this.logger.warn("Failed to flush before reconfiguration:", e);
89
+ });
90
+ }
91
+ let t = this.buildCodecString(i), { description: n, preSkip: a } = i === "opus" ? l(this.getDescription(i, e)) : {
92
+ description: this.getDescription(i, e),
93
+ preSkip: 0
94
+ }, o = {
95
+ codec: t,
96
+ sampleRate: e.sampleRate,
97
+ numberOfChannels: e.channels,
98
+ description: n
99
+ };
100
+ try {
101
+ r.configure(o), this.previousCodec = i, this.priming = { remaining: a };
102
+ } catch (t) {
103
+ this.logger.error(`Failed to configure decoder for ${i} (rendition ${e.renditionId}), dropping sample`, t), this.errors.push(t instanceof Error ? t : /* @__PURE__ */ Error(`Failed to configure decoder for ${i}`)), r.state === "configured" && r.reset(), this.resetDecoderState(), this.ensureDecoder();
104
+ return;
105
+ }
106
+ }
107
+ if (e.timescale <= 0) {
108
+ this.logger.error(`Invalid timescale ${e.timescale} in sample, dropping sample`);
109
+ return;
110
+ }
111
+ this.previousSample = e;
112
+ let c = Math.floor(1e6 * e.timestamp / e.timescale), u = Math.floor(1e6 * e.duration / e.timescale), d;
113
+ try {
114
+ d = new EncodedAudioChunk({
115
+ type: "key",
116
+ timestamp: c,
117
+ duration: u > 0 ? u : void 0,
118
+ data: e.data
119
+ });
120
+ } catch (e) {
121
+ this.logger.error("Failed to create chunk:", e);
122
+ return;
123
+ }
124
+ this.pendingSamples.push({
125
+ sample: e,
126
+ decodeStartTime: performance.now(),
127
+ priming: this.priming
128
+ }), this.sentCount++;
129
+ try {
130
+ r.decode(d);
131
+ } catch (e) {
132
+ this.logger.error("Failed to decode chunk:", e), this.pendingSamples.popLast(), this.sentCount--, r.state === "closed" && (this.resetDecoderState(), this.ensureDecoder());
133
+ }
134
+ }), e(this, "flush", t(function* () {
135
+ if (c.decoder && c.decoder.state === "configured") try {
136
+ let e = c.sentCount;
137
+ yield c.decoder.flush(), c.dropUndecoded(e);
138
+ } catch (e) {
139
+ c.logger.error("Failed to flush decoder:", e), c.resetDecoderState();
140
+ }
141
+ })), e(this, "error", () => this.errors.pop()), e(this, "take", () => this.decodedSamples.shift()), this.logger = s, this.decoder = this.createDecoder();
142
+ }
143
+ };
144
+ s = u, e(u, "isSupported", () => c.AudioDecoder !== void 0), e(u, "create", function() {
145
+ var e = t(function* (e) {
146
+ if (!s.isSupported()) throw Error("WebCodecs AudioDecoder is not supported in this environment");
147
+ return new s(e);
148
+ });
149
+ return function(t) {
150
+ return e.apply(this, arguments);
151
+ };
152
+ }());
153
+ //#endregion
154
+ export { u as WebCodecsAudioDecoderContext };