-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathaudio-decoder.https.any.js
251 lines (235 loc) · 5.96 KB
/
audio-decoder.https.any.js
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
// META: global=window,dedicatedworker
// META: script=/webcodecs/utils.js
const detachedArrayBuffer = new ArrayBuffer(4);
var b = detachedArrayBuffer.transferToFixedLength();
const invalidConfigs = [
{
comment: 'Missing codec',
config: {
sampleRate: 48000,
numberOfChannels: 2,
},
},
{
comment: 'Empty codec',
config: {
codec: '',
sampleRate: 48000,
numberOfChannels: 2,
},
},
{
comment: 'Missing sampleRate',
config: {
codec: 'opus',
sampleRate: 48000,
},
},
{
comment: 'Missing numberOfChannels',
config: {
codec: 'opus',
sampleRate: 48000,
},
},
{
comment: 'Zero sampleRate',
config: {
codec: 'opus',
sampleRate: 0,
numberOfChannels: 2,
},
},
{
comment: 'Zero channels',
config: {
codec: 'opus',
sampleRate: 8000,
numberOfChannels: 0,
},
},
{
comment: 'Opus with >2 channels but no description',
config: {
codec: 'opus',
sampleRate: 48000,
numberOfChannels: 6,
}
},
{
comment: 'Valid configuration except detached description',
config: {
codec: 'opus',
sampleRate: 8000,
numberOfChannels: 1,
description: detachedArrayBuffer,
},
},
];
invalidConfigs.forEach(entry => {
promise_test(
t => {
return promise_rejects_js(
t, TypeError, AudioDecoder.isConfigSupported(entry.config));
},
'Test that AudioDecoder.isConfigSupported() rejects invalid config: ' +
entry.comment);
});
invalidConfigs.forEach(entry => {
async_test(
t => {
let codec = new AudioDecoder(getDefaultCodecInit(t));
assert_throws_js(TypeError, () => {
codec.configure(entry.config);
});
t.done();
},
'Test that AudioDecoder.configure() rejects invalid config: ' +
entry.comment);
});
const validButUnsupportedConfigs = [
{
comment: 'Unrecognized codec',
config: {
codec: 'bogus',
sampleRate: 48000,
numberOfChannels: 2,
},
},
{
comment: 'Video codec',
config: {
codec: 'vp8',
sampleRate: 48000,
numberOfChannels: 2,
},
},
{
comment: 'Ambiguous codec',
config: {
codec: 'vp9',
sampleRate: 48000,
numberOfChannels: 2,
},
},
{
comment: 'Codec with MIME type',
config: {
codec: 'audio/webm; codecs="opus"',
sampleRate: 48000,
numberOfChannels: 2,
},
},
{
comment: 'Possible future opus codec string',
config: {
codec: 'opus.123',
sampleRate: 48000,
numberOfChannels: 2,
}
},
{
comment: 'Possible future aac codec string',
config: {
codec: 'mp4a.FF.9',
sampleRate: 48000,
numberOfChannels: 2,
}
},
];
// Those configurations are supported, but attempting to configure an
// AudioDecoder will fail, because `description` is invalid for this particular
// codec
var supportedButErrorOnConfiguration = [
{
comment: 'Opus with more than two channels and without description',
config: {
codec: 'opus',
sampleRate: '48000',
numberOfChannels: 3,
},
},
{
comment: 'Opus with more than two channels and with a description that is too short',
config: {
codec: 'opus',
sampleRate: '48000',
numberOfChannels: 3,
description: new Uint8Array(9), // at least 10 bytes are required for multichannel
},
},
{
comment: 'vorbis requires a description',
config: {
codec: 'vorbis',
sampleRate: '48000',
numberOfChannels: 2
},
},
{
comment: 'flac requires a description',
config: {
codec: 'flac',
sampleRate: '48000',
numberOfChannels: 2
},
},
];
validButUnsupportedConfigs.forEach(entry => {
promise_test(
t => {
return AudioDecoder.isConfigSupported(entry.config).then(support => {
assert_false(support.supported);
});
},
'Test that AudioDecoder.isConfigSupported() doesn\'t support config: ' +
entry.comment);
});
var shouldError = validButUnsupportedConfigs.concat(supportedButErrorOnConfiguration);
shouldError.forEach(entry => {
promise_test(
t => {
let isErrorCallbackCalled = false;
let supported = AudioDecoder.isConfigSupported(entry.config);
assert_implements_optional(supported, entry.config.codec + ' unsupported');
let codec = new AudioDecoder({
output: t.unreached_func('unexpected output'),
error: t.step_func_done(e => {
isErrorCallbackCalled = true;
assert_true(e instanceof DOMException);
assert_equals(e.name, 'NotSupportedError');
assert_equals(codec.state, 'closed', 'state');
})
});
codec.configure(entry.config);
return codec.flush()
.then(t.unreached_func('flush succeeded unexpectedly'))
.catch(t.step_func(e => {
assert_true(isErrorCallbackCalled, "isErrorCallbackCalled");
assert_true(e instanceof DOMException);
assert_equals(e.name, 'NotSupportedError');
assert_equals(codec.state, 'closed', 'state');
}));
},
'Test that AudioDecoder.configure() doesn\'t support config: ' +
entry.comment);
});
function getFakeChunk() {
return new EncodedAudioChunk(
{type: 'key', timestamp: 0, data: Uint8Array.of(0)});
}
promise_test(t => {
// AudioDecoderInit lacks required fields.
assert_throws_js(TypeError, () => {
new AudioDecoder({});
});
// AudioDecoderInit has required fields.
let decoder = new AudioDecoder(getDefaultCodecInit(t));
assert_equals(decoder.state, 'unconfigured');
decoder.close();
return endAfterEventLoopTurn();
}, 'Test AudioDecoder construction');
promise_test(t => {
let decoder = new AudioDecoder(getDefaultCodecInit(t));
return testUnconfiguredCodec(t, decoder, getFakeChunk());
}, 'Verify unconfigured AudioDecoder operations');