1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
// META: global=window,dedicatedworker
// META: script=/webcodecs/utils.js
// META: variant=?adts_aac
// META: variant=?mp4_aac
// META: variant=?mp3
// META: variant=?opus
// META: variant=?pcm_alaw
// META: variant=?pcm_mulaw
const ADTS_AAC_DATA = {
src: 'sfx.adts',
config: {
codec: 'mp4a.40.2',
sampleRate: 48000,
numberOfChannels: 1,
},
chunks: [
{offset: 0, size: 248}, {offset: 248, size: 280}, {offset: 528, size: 258},
{offset: 786, size: 125}, {offset: 911, size: 230},
{offset: 1141, size: 148}, {offset: 1289, size: 224},
{offset: 1513, size: 166}, {offset: 1679, size: 216},
{offset: 1895, size: 183}
],
duration: 24000
};
const MP3_DATA = {
src: 'sfx.mp3',
config: {
codec: 'mp3',
sampleRate: 48000,
numberOfChannels: 1,
},
chunks: [
{offset: 333, size: 288}, {offset: 621, size: 288},
{offset: 909, size: 288}, {offset: 1197, size: 288},
{offset: 1485, size: 288}, {offset: 1773, size: 288},
{offset: 2061, size: 288}, {offset: 2349, size: 288},
{offset: 2637, size: 288}, {offset: 2925, size: 288}
],
duration: 24000
};
const MP4_AAC_DATA = {
src: 'sfx-aac.mp4',
config: {
codec: 'mp4a.40.2',
sampleRate: 48000,
numberOfChannels: 1,
description: {offset: 2552, size: 5},
},
chunks: [
{offset: 44, size: 241},
{offset: 285, size: 273},
{offset: 558, size: 251},
{offset: 809, size: 118},
{offset: 927, size: 223},
{offset: 1150, size: 141},
{offset: 1291, size: 217},
{offset: 1508, size: 159},
{offset: 1667, size: 209},
{offset: 1876, size: 176},
],
duration: 21333
};
const OPUS_DATA = {
src: 'sfx-opus.ogg',
config: {
codec: 'opus',
sampleRate: 48000,
numberOfChannels: 1,
description: {offset: 28, size: 19},
},
chunks: [
{offset: 185, size: 450}, {offset: 635, size: 268},
{offset: 903, size: 285}, {offset: 1188, size: 296},
{offset: 1484, size: 287}, {offset: 1771, size: 308},
{offset: 2079, size: 289}, {offset: 2368, size: 286},
{offset: 2654, size: 296}, {offset: 2950, size: 294}
],
duration: 20000
};
const PCM_ALAW_DATA = {
src: 'sfx-alaw.wav',
config: {
codec: 'alaw',
sampleRate: 48000,
numberOfChannels: 1,
},
// Any arbitrary grouping should work.
chunks: [
{offset: 0, size: 2048}, {offset: 2048, size: 2048},
{offset: 4096, size: 2048}, {offset: 6144, size: 2048},
{offset: 8192, size: 2048}, {offset: 10240, size: 92}
],
duration: 35555
};
const PCM_MULAW_DATA = {
src: 'sfx-mulaw.wav',
config: {
codec: 'ulaw',
sampleRate: 48000,
numberOfChannels: 1,
},
// Any arbitrary grouping should work.
chunks: [
{offset: 0, size: 2048}, {offset: 2048, size: 2048},
{offset: 4096, size: 2048}, {offset: 6144, size: 2048},
{offset: 8192, size: 2048}, {offset: 10240, size: 92}
],
duration: 35555
};
// Allows mutating `callbacks` after constructing the AudioDecoder, wraps calls
// in t.step().
function createAudioDecoder(t, callbacks) {
return new AudioDecoder({
output(frame) {
if (callbacks && callbacks.output) {
t.step(() => callbacks.output(frame));
} else {
t.unreached_func('unexpected output()');
}
},
error(e) {
if (callbacks && callbacks.error) {
t.step(() => callbacks.error(e));
} else {
t.unreached_func('unexpected error()');
}
}
});
}
// Create a view of an ArrayBuffer.
function view(buffer, {offset, size}) {
return new Uint8Array(buffer, offset, size);
}
let CONFIG = null;
let CHUNK_DATA = null;
let CHUNKS = null;
promise_setup(async () => {
const data = {
'?adts_aac': ADTS_AAC_DATA,
'?mp3': MP3_DATA,
'?mp4_aac': MP4_AAC_DATA,
'?opus': OPUS_DATA,
'?pcm_alaw': PCM_ALAW_DATA,
'?pcm_mulaw': PCM_MULAW_DATA,
}[location.search];
// Don't run any tests if the codec is not supported.
assert_equals("function", typeof AudioDecoder.isConfigSupported);
let supported = false;
try {
const support = await AudioDecoder.isConfigSupported({
codec: data.config.codec,
sampleRate: data.config.sampleRate,
numberOfChannels: data.config.numberOfChannels
});
supported = support.supported;
} catch (e) {
}
assert_implements_optional(supported, data.config.codec + ' unsupported');
// Fetch the media data and prepare buffers.
const response = await fetch(data.src);
const buf = await response.arrayBuffer();
CONFIG = {...data.config};
if (data.config.description) {
CONFIG.description = view(buf, data.config.description);
}
CHUNK_DATA = data.chunks.map((chunk, i) => view(buf, chunk));
CHUNKS = CHUNK_DATA.map((encodedData, i) => new EncodedAudioChunk({
type: 'key',
timestamp: i * data.duration,
duration: data.duration,
data: encodedData
}));
});
promise_test(t => {
return AudioDecoder.isConfigSupported(CONFIG);
}, 'Test isConfigSupported()');
promise_test(t => {
// Define a valid config that includes a hypothetical 'futureConfigFeature',
// which is not yet recognized by the User Agent.
const validConfig = {
...CONFIG,
futureConfigFeature: 'foo',
};
// The UA will evaluate validConfig as being "valid", ignoring the
// `futureConfigFeature` it doesn't recognize.
return AudioDecoder.isConfigSupported(validConfig).then((decoderSupport) => {
// AudioDecoderSupport must contain the following properites.
assert_true(decoderSupport.hasOwnProperty('supported'));
assert_true(decoderSupport.hasOwnProperty('config'));
// AudioDecoderSupport.config must not contain unrecognized properties.
assert_false(decoderSupport.config.hasOwnProperty('futureConfigFeature'));
// AudioDecoderSupport.config must contiain the recognized properties.
assert_equals(decoderSupport.config.codec, validConfig.codec);
assert_equals(decoderSupport.config.sampleRate, validConfig.sampleRate);
assert_equals(
decoderSupport.config.numberOfChannels, validConfig.numberOfChannels);
if (validConfig.description) {
// The description must be copied.
assert_false(
decoderSupport.config.description === validConfig.description,
'description is unique');
assert_array_equals(
new Uint8Array(decoderSupport.config.description, 0),
new Uint8Array(validConfig.description, 0), 'description');
} else {
assert_false(
decoderSupport.config.hasOwnProperty('description'), 'description');
}
});
}, 'Test that AudioDecoder.isConfigSupported() returns a parsed configuration');
promise_test(async t => {
const decoder = createAudioDecoder(t);
decoder.configure(CONFIG);
assert_equals(decoder.state, 'configured', 'state');
}, 'Test configure()');
promise_test(t => {
const decoder = createAudioDecoder(t);
return testClosedCodec(t, decoder, CONFIG, CHUNKS[0]);
}, 'Verify closed AudioDecoder operations');
promise_test(async t => {
const callbacks = {};
const decoder = createAudioDecoder(t, callbacks);
let outputs = 0;
callbacks.output = frame => {
outputs++;
frame.close();
};
decoder.configure(CONFIG);
CHUNKS.forEach(chunk => {
decoder.decode(chunk);
});
await decoder.flush();
assert_equals(outputs, CHUNKS.length, 'outputs');
}, 'Test decoding');
promise_test(async t => {
const callbacks = {};
const decoder = createAudioDecoder(t, callbacks);
let outputs = 0;
callbacks.output = frame => {
outputs++;
frame.close();
};
decoder.configure(CONFIG);
decoder.decode(new EncodedAudioChunk(
{type: 'key', timestamp: -42, data: CHUNK_DATA[0]}));
await decoder.flush();
assert_equals(outputs, 1, 'outputs');
}, 'Test decoding a with negative timestamp');
promise_test(async t => {
const callbacks = {};
const decoder = createAudioDecoder(t, callbacks);
let outputs = 0;
callbacks.output = frame => {
outputs++;
frame.close();
};
decoder.configure(CONFIG);
decoder.decode(CHUNKS[0]);
await decoder.flush();
assert_equals(outputs, 1, 'outputs');
decoder.decode(CHUNKS[0]);
await decoder.flush();
assert_equals(outputs, 2, 'outputs');
}, 'Test decoding after flush');
promise_test(async t => {
const callbacks = {};
const decoder = createAudioDecoder(t, callbacks);
decoder.configure(CONFIG);
decoder.decode(CHUNKS[0]);
decoder.decode(CHUNKS[1]);
const flushDone = decoder.flush();
// Wait for the first output, then reset.
let outputs = 0;
await new Promise(resolve => {
callbacks.output = frame => {
outputs++;
assert_equals(outputs, 1, 'outputs');
decoder.reset();
frame.close();
resolve();
};
});
// Flush should have been synchronously rejected.
await promise_rejects_dom(t, 'AbortError', flushDone);
assert_equals(outputs, 1, 'outputs');
}, 'Test reset during flush');
promise_test(async t => {
const callbacks = {};
const decoder = createAudioDecoder(t, callbacks);
// No decodes yet.
assert_equals(decoder.decodeQueueSize, 0);
decoder.configure(CONFIG);
// Still no decodes.
assert_equals(decoder.decodeQueueSize, 0);
let lastDequeueSize = Infinity;
decoder.ondequeue = () => {
assert_greater_than(lastDequeueSize, 0, "Dequeue event after queue empty");
assert_greater_than(lastDequeueSize, decoder.decodeQueueSize,
"Dequeue event without decreased queue size");
lastDequeueSize = decoder.decodeQueueSize;
};
for (let chunk of CHUNKS)
decoder.decode(chunk);
assert_greater_than_equal(decoder.decodeQueueSize, 0);
assert_less_than_equal(decoder.decodeQueueSize, CHUNKS.length);
await decoder.flush();
// We can guarantee that all decodes are processed after a flush.
assert_equals(decoder.decodeQueueSize, 0);
// Last dequeue event should fire when the queue is empty.
assert_equals(lastDequeueSize, 0);
// Reset this to Infinity to track the decline of queue size for this next
// batch of decodes.
lastDequeueSize = Infinity;
for (let chunk of CHUNKS)
decoder.decode(chunk);
assert_greater_than_equal(decoder.decodeQueueSize, 0);
decoder.reset();
assert_equals(decoder.decodeQueueSize, 0);
}, 'AudioDecoder decodeQueueSize test');
|