@mediabunny/ac3 1.65.1 → 1.66.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 +1 -1
- package/dist/bundles/mediabunny-ac3.js +1114 -1066
- package/dist/bundles/mediabunny-ac3.min.js +1005 -1012
- package/dist/bundles/mediabunny-ac3.min.mjs +1005 -1012
- package/dist/bundles/mediabunny-ac3.mjs +1114 -1066
- package/dist/modules/build/ac3.js +0 -0
- package/dist/modules/src/codec.worker.js +68 -40
- package/dist/modules/src/decoder.d.ts.map +1 -1
- package/dist/modules/src/decoder.js +41 -13
- package/dist/modules/src/encoder.d.ts.map +1 -1
- package/dist/modules/src/encoder.js +51 -33
- package/dist/modules/src/shared.d.ts +10 -6
- package/dist/modules/src/shared.d.ts.map +1 -1
- package/dist/modules/src/worker-client.js +10 -3
- package/dist/modules/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/bridge.c +55 -12
- package/src/codec.worker.ts +80 -40
- package/src/decoder.ts +47 -14
- package/src/encoder.ts +75 -47
- package/src/shared.ts +11 -6
- package/src/worker-client.ts +9 -3
|
Binary file
|
|
@@ -15,14 +15,16 @@ let module;
|
|
|
15
15
|
let modulePromise = null;
|
|
16
16
|
let initDecoderFn;
|
|
17
17
|
let configureDecodePacket;
|
|
18
|
-
let
|
|
18
|
+
let sendDecodePacket;
|
|
19
|
+
let sendDecodeFlush;
|
|
20
|
+
let receiveDecodedFrame;
|
|
19
21
|
let getDecodedFormat;
|
|
20
22
|
let getDecodedPlanePtr;
|
|
21
23
|
let getDecodedChannels;
|
|
22
24
|
let getDecodedSampleRate;
|
|
23
25
|
let getDecodedSampleCount;
|
|
24
26
|
let getDecodedPts;
|
|
25
|
-
let
|
|
27
|
+
let resetDecoder;
|
|
26
28
|
let closeDecoderFn;
|
|
27
29
|
let initEncoderFn;
|
|
28
30
|
let getEncoderFrameSize;
|
|
@@ -45,14 +47,16 @@ const ensureModule = async () => {
|
|
|
45
47
|
modulePromise = null;
|
|
46
48
|
initDecoderFn = module.cwrap('init_decoder', 'number', ['number']);
|
|
47
49
|
configureDecodePacket = module.cwrap('configure_decode_packet', 'number', ['number', 'number']);
|
|
48
|
-
|
|
50
|
+
sendDecodePacket = module.cwrap('send_decode_packet', 'number', ['number', 'number']);
|
|
51
|
+
sendDecodeFlush = module.cwrap('send_decode_flush', 'number', ['number']);
|
|
52
|
+
receiveDecodedFrame = module.cwrap('receive_decoded_frame', 'number', ['number']);
|
|
49
53
|
getDecodedFormat = module.cwrap('get_decoded_format', 'number', ['number']);
|
|
50
54
|
getDecodedPlanePtr = module.cwrap('get_decoded_plane_ptr', 'number', ['number', 'number']);
|
|
51
55
|
getDecodedChannels = module.cwrap('get_decoded_channels', 'number', ['number']);
|
|
52
56
|
getDecodedSampleRate = module.cwrap('get_decoded_sample_rate', 'number', ['number']);
|
|
53
57
|
getDecodedSampleCount = module.cwrap('get_decoded_sample_count', 'number', ['number']);
|
|
54
58
|
getDecodedPts = module.cwrap('get_decoded_pts', 'number', ['number']);
|
|
55
|
-
|
|
59
|
+
resetDecoder = module.cwrap('reset_decoder', null, ['number']);
|
|
56
60
|
closeDecoderFn = module.cwrap('close_decoder', null, ['number']);
|
|
57
61
|
initEncoderFn = module.cwrap('init_encoder', 'number', ['number', 'number', 'number', 'number']);
|
|
58
62
|
getEncoderFrameSize = module.cwrap('get_encoder_frame_size', 'number', ['number']);
|
|
@@ -84,6 +88,7 @@ const AV_FORMAT_MAP = {
|
|
|
84
88
|
7: { format: 's32-planar', bytesPerSample: 4, planar: true },
|
|
85
89
|
8: { format: 'f32-planar', bytesPerSample: 4, planar: true },
|
|
86
90
|
};
|
|
91
|
+
const AV_NOPTS_VALUE = -(BigInt(2) ** BigInt(63));
|
|
87
92
|
const decode = (ctx, encodedData, timestamp) => {
|
|
88
93
|
const bytes = new Uint8Array(encodedData);
|
|
89
94
|
const dataPtr = configureDecodePacket(ctx, bytes.length);
|
|
@@ -91,35 +96,65 @@ const decode = (ctx, encodedData, timestamp) => {
|
|
|
91
96
|
throw new Error('Failed to configure decode packet.');
|
|
92
97
|
}
|
|
93
98
|
module.HEAPU8.set(bytes, dataPtr);
|
|
94
|
-
const ret =
|
|
99
|
+
const ret = sendDecodePacket(ctx, BigInt(timestamp));
|
|
95
100
|
if (ret < 0) {
|
|
96
101
|
throw new Error(`Decode failed with error code ${ret}.`);
|
|
97
102
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
103
|
+
return receiveDecodedChunks(ctx);
|
|
104
|
+
};
|
|
105
|
+
const flushDecoder = (ctx) => {
|
|
106
|
+
const ret = sendDecodeFlush(ctx);
|
|
107
|
+
if (ret < 0) {
|
|
108
|
+
throw new Error(`Decoder flush failed with error code ${ret}.`);
|
|
102
109
|
}
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
buffer.set(module.HEAPU8.subarray(ptr, ptr + planeSize), ch * planeSize);
|
|
110
|
+
const chunks = receiveDecodedChunks(ctx);
|
|
111
|
+
resetDecoder(ctx);
|
|
112
|
+
return chunks;
|
|
113
|
+
};
|
|
114
|
+
const receiveDecodedChunks = (ctx) => {
|
|
115
|
+
const chunks = [];
|
|
116
|
+
while (true) {
|
|
117
|
+
const ret = receiveDecodedFrame(ctx);
|
|
118
|
+
if (ret < 0) {
|
|
119
|
+
throw new Error(`Decode failed with error code ${ret}.`);
|
|
114
120
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
+
if (ret === 0) {
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
const avFormat = getDecodedFormat(ctx);
|
|
125
|
+
const info = AV_FORMAT_MAP[avFormat];
|
|
126
|
+
if (!info) {
|
|
127
|
+
throw new Error(`Unsupported AVSampleFormat: ${avFormat}`);
|
|
128
|
+
}
|
|
129
|
+
const channels = getDecodedChannels(ctx);
|
|
130
|
+
const sampleRate = getDecodedSampleRate(ctx);
|
|
131
|
+
const sampleCount = getDecodedSampleCount(ctx);
|
|
132
|
+
const pts = getDecodedPts(ctx);
|
|
133
|
+
let pcmData;
|
|
134
|
+
if (info.planar) {
|
|
135
|
+
const planeSize = sampleCount * info.bytesPerSample;
|
|
136
|
+
const buffer = new Uint8Array(planeSize * channels);
|
|
137
|
+
for (let ch = 0; ch < channels; ch++) {
|
|
138
|
+
const ptr = getDecodedPlanePtr(ctx, ch);
|
|
139
|
+
buffer.set(module.HEAPU8.subarray(ptr, ptr + planeSize), ch * planeSize);
|
|
140
|
+
}
|
|
141
|
+
pcmData = buffer.buffer;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
const totalSize = sampleCount * channels * info.bytesPerSample;
|
|
145
|
+
const ptr = getDecodedPlanePtr(ctx, 0);
|
|
146
|
+
pcmData = module.HEAPU8.slice(ptr, ptr + totalSize).buffer;
|
|
147
|
+
}
|
|
148
|
+
chunks.push({
|
|
149
|
+
pcmData,
|
|
150
|
+
format: info.format,
|
|
151
|
+
channels,
|
|
152
|
+
sampleRate,
|
|
153
|
+
sampleCount,
|
|
154
|
+
pts: pts === AV_NOPTS_VALUE ? null : Number(pts),
|
|
155
|
+
});
|
|
121
156
|
}
|
|
122
|
-
return
|
|
157
|
+
return chunks;
|
|
123
158
|
};
|
|
124
159
|
const initEncoder = async (codec, numberOfChannels, sampleRate, bitrate) => {
|
|
125
160
|
await ensureModule();
|
|
@@ -165,24 +200,17 @@ const onMessage = (data) => {
|
|
|
165
200
|
break;
|
|
166
201
|
case 'decode':
|
|
167
202
|
{
|
|
168
|
-
const
|
|
169
|
-
result = {
|
|
170
|
-
|
|
171
|
-
pcmData: decoded.pcmData,
|
|
172
|
-
format: decoded.format,
|
|
173
|
-
channels: decoded.channels,
|
|
174
|
-
sampleRate: decoded.sampleRate,
|
|
175
|
-
sampleCount: decoded.sampleCount,
|
|
176
|
-
pts: decoded.pts,
|
|
177
|
-
};
|
|
178
|
-
transferables.push(decoded.pcmData);
|
|
203
|
+
const chunks = decode(command.data.ctx, command.data.encodedData, command.data.timestamp);
|
|
204
|
+
result = { type: command.type, chunks };
|
|
205
|
+
transferables.push(...chunks.map(x => x.pcmData));
|
|
179
206
|
}
|
|
180
207
|
;
|
|
181
208
|
break;
|
|
182
209
|
case 'flush-decoder':
|
|
183
210
|
{
|
|
184
|
-
|
|
185
|
-
result = { type: command.type };
|
|
211
|
+
const chunks = flushDecoder(command.data.ctx);
|
|
212
|
+
result = { type: command.type, chunks };
|
|
213
|
+
transferables.push(...chunks.map(x => x.pcmData));
|
|
186
214
|
}
|
|
187
215
|
;
|
|
188
216
|
break;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decoder.d.ts","sourceRoot":"","sources":["../../../src/decoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;
|
|
1
|
+
{"version":3,"file":"decoder.d.ts","sourceRoot":"","sources":["../../../src/decoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AA6FH;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,YAO9B,CAAC"}
|
|
@@ -10,10 +10,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.registerAc3Decoder = void 0;
|
|
11
11
|
const mediabunny_1 = require("mediabunny");
|
|
12
12
|
const worker_client_1 = require("./worker-client");
|
|
13
|
+
const shared_1 = require("./shared");
|
|
13
14
|
class Ac3Decoder extends mediabunny_1.CustomAudioDecoder {
|
|
14
15
|
constructor() {
|
|
15
16
|
super(...arguments);
|
|
16
|
-
this.ctx =
|
|
17
|
+
this.ctx = null;
|
|
18
|
+
this.nextTimestamp = null;
|
|
19
|
+
this.inFlightDecode = null;
|
|
17
20
|
}
|
|
18
21
|
static supports(codec) {
|
|
19
22
|
return codec === 'ac3' || codec === 'eac3';
|
|
@@ -27,29 +30,54 @@ class Ac3Decoder extends mediabunny_1.CustomAudioDecoder {
|
|
|
27
30
|
this.ctx = result.ctx;
|
|
28
31
|
}
|
|
29
32
|
async decode(packet) {
|
|
33
|
+
(0, shared_1.assert)(this.ctx !== null);
|
|
30
34
|
const encodedData = packet.data.slice().buffer;
|
|
31
35
|
const timestamp = Math.round(packet.timestamp * this.config.sampleRate);
|
|
32
|
-
const
|
|
36
|
+
const decode = (0, worker_client_1.sendCommand)({
|
|
33
37
|
type: 'decode',
|
|
34
38
|
data: { ctx: this.ctx, encodedData, timestamp },
|
|
35
|
-
}, [encodedData]);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
timestamp: result.pts / result.sampleRate,
|
|
42
|
-
});
|
|
43
|
-
this.onSample(sample);
|
|
39
|
+
}, [encodedData]).then(result => this.emitSamples(result.chunks));
|
|
40
|
+
decode.catch(() => { }); // Surfaces through whoever awaits it next
|
|
41
|
+
// Keep one packet in flight, so the worker already has the next one by the time it's done with the current one
|
|
42
|
+
const previousDecode = this.inFlightDecode;
|
|
43
|
+
this.inFlightDecode = decode;
|
|
44
|
+
await previousDecode;
|
|
44
45
|
}
|
|
45
46
|
async flush() {
|
|
46
|
-
|
|
47
|
+
(0, shared_1.assert)(this.ctx !== null);
|
|
48
|
+
await this.inFlightDecode;
|
|
49
|
+
this.inFlightDecode = null;
|
|
50
|
+
const result = await (0, worker_client_1.sendCommand)({ type: 'flush-decoder', data: { ctx: this.ctx } });
|
|
51
|
+
this.emitSamples(result.chunks);
|
|
47
52
|
}
|
|
48
53
|
async close() {
|
|
49
54
|
// Close shouldn't throw
|
|
50
|
-
|
|
55
|
+
if (this.ctx !== null) {
|
|
56
|
+
(0, worker_client_1.sendCommand)({ type: 'close-decoder', data: { ctx: this.ctx } }).catch(() => { });
|
|
57
|
+
}
|
|
51
58
|
await (0, worker_client_1.unrefWorker)();
|
|
52
59
|
}
|
|
60
|
+
emitSamples(chunks) {
|
|
61
|
+
for (const chunk of chunks) {
|
|
62
|
+
let timestamp;
|
|
63
|
+
if (chunk.pts !== null) {
|
|
64
|
+
timestamp = chunk.pts / this.config.sampleRate;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
(0, shared_1.assert)(this.nextTimestamp !== null);
|
|
68
|
+
timestamp = this.nextTimestamp;
|
|
69
|
+
}
|
|
70
|
+
const sample = new mediabunny_1.AudioSample({
|
|
71
|
+
data: chunk.pcmData,
|
|
72
|
+
format: chunk.format,
|
|
73
|
+
numberOfChannels: chunk.channels,
|
|
74
|
+
sampleRate: chunk.sampleRate,
|
|
75
|
+
timestamp,
|
|
76
|
+
});
|
|
77
|
+
this.nextTimestamp = timestamp + sample.duration;
|
|
78
|
+
this.onSample(sample);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
53
81
|
}
|
|
54
82
|
let registered = false;
|
|
55
83
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../../../src/encoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;
|
|
1
|
+
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../../../src/encoder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AA4MH;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,YAO9B,CAAC"}
|
|
@@ -15,7 +15,7 @@ const ac3_misc_1 = require("../../../shared/ac3-misc");
|
|
|
15
15
|
class Ac3Encoder extends mediabunny_1.CustomAudioEncoder {
|
|
16
16
|
constructor() {
|
|
17
17
|
super(...arguments);
|
|
18
|
-
this.ctx =
|
|
18
|
+
this.ctx = null;
|
|
19
19
|
this.encoderFrameSize = 0;
|
|
20
20
|
this.sampleRate = 0;
|
|
21
21
|
this.numberOfChannels = 0;
|
|
@@ -24,16 +24,21 @@ class Ac3Encoder extends mediabunny_1.CustomAudioEncoder {
|
|
|
24
24
|
this.pendingBuffer = new Float32Array(2 ** 16);
|
|
25
25
|
this.pendingFrames = 0;
|
|
26
26
|
this.nextSampleTimestampInSamples = null;
|
|
27
|
+
this.encodedEndTimestampInSamples = 0;
|
|
28
|
+
this.inFlightEncode = null;
|
|
27
29
|
}
|
|
28
30
|
static supports(codec, config) {
|
|
29
|
-
|
|
30
|
-
? [...ac3_misc_1.AC3_SAMPLE_RATES, ...ac3_misc_1.EAC3_REDUCED_SAMPLE_RATES]
|
|
31
|
-
: ac3_misc_1.AC3_SAMPLE_RATES;
|
|
31
|
+
// FFmpeg's encoders top out at 5.1 and can't do E-AC-3's reduced sample rates
|
|
32
32
|
return (codec === 'ac3' || codec === 'eac3')
|
|
33
33
|
&& config.numberOfChannels >= 1
|
|
34
|
-
&& config.numberOfChannels <=
|
|
35
|
-
&&
|
|
36
|
-
&& config.bitrate !== undefined
|
|
34
|
+
&& config.numberOfChannels <= 6
|
|
35
|
+
&& ac3_misc_1.AC3_SAMPLE_RATES.includes(config.sampleRate)
|
|
36
|
+
&& config.bitrate !== undefined
|
|
37
|
+
// FFmpeg snaps AC-3 bitrates to the closest valid one, but rejects E-AC-3 bitrates outside of what
|
|
38
|
+
// six-block and single-block syncframes can hold
|
|
39
|
+
&& (codec === 'ac3'
|
|
40
|
+
|| (config.bitrate >= 16 * Math.ceil(config.sampleRate / 1536)
|
|
41
|
+
&& config.bitrate <= 16 * Math.floor(2048 * config.sampleRate / 256)));
|
|
37
42
|
}
|
|
38
43
|
async init() {
|
|
39
44
|
await (0, worker_client_1.refWorker)();
|
|
@@ -67,6 +72,7 @@ class Ac3Encoder extends mediabunny_1.CustomAudioEncoder {
|
|
|
67
72
|
async encode(audioSample) {
|
|
68
73
|
if (this.nextSampleTimestampInSamples === null) {
|
|
69
74
|
this.nextSampleTimestampInSamples = Math.round(audioSample.timestamp * this.sampleRate);
|
|
75
|
+
this.encodedEndTimestampInSamples = this.nextSampleTimestampInSamples;
|
|
70
76
|
}
|
|
71
77
|
const channels = this.numberOfChannels;
|
|
72
78
|
const incomingFrames = audioSample.numberOfFrames;
|
|
@@ -87,54 +93,66 @@ class Ac3Encoder extends mediabunny_1.CustomAudioEncoder {
|
|
|
87
93
|
}
|
|
88
94
|
this.pendingBuffer.set(incomingData, this.pendingFrames * channels);
|
|
89
95
|
this.pendingFrames += incomingFrames;
|
|
90
|
-
|
|
91
|
-
|
|
96
|
+
let encodedFrames = 0;
|
|
97
|
+
while (this.pendingFrames - encodedFrames >= this.encoderFrameSize) {
|
|
98
|
+
await this.encodeOneFrame(encodedFrames);
|
|
99
|
+
encodedFrames += this.encoderFrameSize;
|
|
92
100
|
}
|
|
101
|
+
this.pendingBuffer.copyWithin(0, encodedFrames * channels, this.pendingFrames * channels);
|
|
102
|
+
this.pendingFrames -= encodedFrames;
|
|
93
103
|
}
|
|
94
104
|
async flush() {
|
|
95
|
-
|
|
96
|
-
|
|
105
|
+
(0, shared_1.assert)(this.ctx !== null);
|
|
106
|
+
await this.inFlightEncode;
|
|
107
|
+
if (this.nextSampleTimestampInSamples !== null) {
|
|
97
108
|
const channels = this.numberOfChannels;
|
|
98
109
|
const frameSize = this.encoderFrameSize;
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
this.
|
|
102
|
-
|
|
103
|
-
|
|
110
|
+
const endTimestampInSamples = this.nextSampleTimestampInSamples + this.pendingFrames;
|
|
111
|
+
// Pad with silence until every sample has made it out
|
|
112
|
+
while (this.encodedEndTimestampInSamples < endTimestampInSamples) {
|
|
113
|
+
this.pendingBuffer.fill(0, this.pendingFrames * channels, frameSize * channels);
|
|
114
|
+
this.pendingFrames = 0;
|
|
115
|
+
await this.encodeOneFrame(0);
|
|
116
|
+
await this.inFlightEncode;
|
|
117
|
+
}
|
|
104
118
|
}
|
|
119
|
+
this.inFlightEncode = null;
|
|
105
120
|
await (0, worker_client_1.sendCommand)({ type: 'flush-encoder', data: { ctx: this.ctx } });
|
|
106
121
|
this.resetInternalState();
|
|
107
122
|
}
|
|
108
123
|
close() {
|
|
109
124
|
// Close shouldn't throw
|
|
110
|
-
|
|
125
|
+
if (this.ctx !== null) {
|
|
126
|
+
(0, worker_client_1.sendCommand)({ type: 'close-encoder', data: { ctx: this.ctx } }).catch(() => { });
|
|
127
|
+
}
|
|
111
128
|
void (0, worker_client_1.unrefWorker)();
|
|
112
129
|
}
|
|
113
|
-
async encodeOneFrame() {
|
|
130
|
+
async encodeOneFrame(startFrame) {
|
|
131
|
+
(0, shared_1.assert)(this.ctx !== null);
|
|
114
132
|
(0, shared_1.assert)(this.nextSampleTimestampInSamples !== null);
|
|
115
133
|
const channels = this.numberOfChannels;
|
|
116
|
-
const
|
|
117
|
-
const frameSamples = frameSize * channels;
|
|
118
|
-
const frameData = this.pendingBuffer.slice(0, frameSamples);
|
|
119
|
-
// Shift remaining using copyWithin
|
|
120
|
-
this.pendingFrames -= frameSize;
|
|
121
|
-
if (this.pendingFrames > 0) {
|
|
122
|
-
this.pendingBuffer.copyWithin(0, frameSamples, frameSamples + this.pendingFrames * channels);
|
|
123
|
-
}
|
|
134
|
+
const frameData = this.pendingBuffer.slice(startFrame * channels, (startFrame + this.encoderFrameSize) * channels);
|
|
124
135
|
const audioData = frameData.buffer;
|
|
125
|
-
const
|
|
136
|
+
const encode = (0, worker_client_1.sendCommand)({
|
|
126
137
|
type: 'encode',
|
|
127
138
|
data: {
|
|
128
139
|
ctx: this.ctx,
|
|
129
140
|
audioData,
|
|
130
141
|
timestamp: this.nextSampleTimestampInSamples,
|
|
131
142
|
},
|
|
132
|
-
}, [audioData])
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
143
|
+
}, [audioData]).then((result) => {
|
|
144
|
+
this.encodedEndTimestampInSamples = result.pts + result.duration;
|
|
145
|
+
// We always get exactly one packet because we encode the correct frame size
|
|
146
|
+
const packet = new mediabunny_1.EncodedPacket(new Uint8Array(result.encodedData), 'key', result.pts / this.sampleRate, result.duration / this.sampleRate);
|
|
147
|
+
this.onPacket(packet, this.chunkMetadata);
|
|
148
|
+
this.chunkMetadata = {};
|
|
149
|
+
});
|
|
150
|
+
encode.catch(() => { }); // Surfaces through whoever awaits it next
|
|
151
|
+
this.nextSampleTimestampInSamples += this.encoderFrameSize;
|
|
152
|
+
// Keep one frame in flight, so the worker already has the next frame by the time it's done with the current one
|
|
153
|
+
const previousEncode = this.inFlightEncode;
|
|
154
|
+
this.inFlightEncode = encode;
|
|
155
|
+
await previousEncode;
|
|
138
156
|
}
|
|
139
157
|
}
|
|
140
158
|
let registered = false;
|
|
@@ -5,6 +5,14 @@
|
|
|
5
5
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
6
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
7
|
*/
|
|
8
|
+
export type DecodedChunk = {
|
|
9
|
+
pcmData: ArrayBuffer;
|
|
10
|
+
format: AudioSampleFormat;
|
|
11
|
+
channels: number;
|
|
12
|
+
sampleRate: number;
|
|
13
|
+
sampleCount: number;
|
|
14
|
+
pts: number | null;
|
|
15
|
+
};
|
|
8
16
|
export type WorkerCommand = {
|
|
9
17
|
type: 'init-decoder';
|
|
10
18
|
data: {
|
|
@@ -59,14 +67,10 @@ export type WorkerResponseData = {
|
|
|
59
67
|
frameSize: number;
|
|
60
68
|
} | {
|
|
61
69
|
type: 'decode';
|
|
62
|
-
|
|
63
|
-
format: AudioSampleFormat;
|
|
64
|
-
channels: number;
|
|
65
|
-
sampleRate: number;
|
|
66
|
-
sampleCount: number;
|
|
67
|
-
pts: number;
|
|
70
|
+
chunks: DecodedChunk[];
|
|
68
71
|
} | {
|
|
69
72
|
type: 'flush-decoder';
|
|
73
|
+
chunks: DecodedChunk[];
|
|
70
74
|
} | {
|
|
71
75
|
type: 'close-decoder';
|
|
72
76
|
} | {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../../src/shared.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,MAAM,aA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AAE,eAAe,CAAC;IACtB,IAAI,EAAE;QACL,GAAG,EAAE,MAAM,CAAC;KACZ,CAAC;CACF,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAChC,IAAI,EAAE,cAAc,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;CAClB,GAAG;IACH,IAAI,EAAE,QAAQ,CAAC;IACf,MAAM,EAAE,YAAY,EAAE,CAAC;CACvB,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;IACtB,MAAM,EAAE,YAAY,EAAE,CAAC;CACvB,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;CACtB,GAAG;IACH,IAAI,EAAE,cAAc,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;CAClB,GAAG;IACH,IAAI,EAAE,QAAQ,CAAC;IACf,WAAW,EAAE,WAAW,CAAC;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;CACjB,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;CACtB,GAAG;IACH,IAAI,EAAE,eAAe,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC5B,EAAE,EAAE,MAAM,CAAC;CACX,GAAG,CAAC;IACJ,OAAO,EAAE,IAAI,CAAC;IACd,IAAI,EAAE,kBAAkB,CAAC;CACzB,GAAG;IACH,OAAO,EAAE,KAAK,CAAC;IACf,KAAK,EAAE,OAAO,CAAC;CACf,CAAC,CAAC;AAEH,wBAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC,CAI5C"}
|
|
@@ -36,7 +36,7 @@ const unrefWorker = async () => {
|
|
|
36
36
|
clearInterval(keepAliveInterval);
|
|
37
37
|
keepAliveInterval = null;
|
|
38
38
|
}
|
|
39
|
-
const worker = await workerPromise;
|
|
39
|
+
const worker = await workerPromise?.catch(() => null);
|
|
40
40
|
if (worker) {
|
|
41
41
|
if (workerError) {
|
|
42
42
|
// The failed worker was already terminated. With nobody using it anymore, the next ref can start fresh
|
|
@@ -78,8 +78,15 @@ const sendCommand = async (command, transferables) => {
|
|
|
78
78
|
exports.sendCommand = sendCommand;
|
|
79
79
|
const ensureWorker = () => {
|
|
80
80
|
return workerPromise ??= (async () => {
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
let worker;
|
|
82
|
+
try {
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
84
|
+
worker = (await (0, codec_worker_1.default)());
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
workerPromise = null; // So that the next use tries again
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
83
90
|
worker.unref?.(); // Start unreffed
|
|
84
91
|
const onMessage = (data) => {
|
|
85
92
|
const pending = pendingMessages.get(data.id);
|