Bug 1699519 [wpt PR 28057] - Add AudioFrame serialization, a=testonly

Automatic update from web-platform-tests
Add AudioFrame serialization (#28057)

This CL allows AudioFrames to be serialized and posted to a worker. The
model is the same as for VideoFrame serialization, with the difference
that we copy data, instead of passing a reference around.

There is a copy when serializing the data, and another copy each time
the data is deserialized. In practice, copying audio shouldn't be too
expensive, but adding a shareable backing to blink::AudioBuffer would
be nice.

Bug: 1168418
Change-Id: I9d1a272fddb9595ea0f5fd2b28b38b92a5acc241
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2757314
Commit-Queue: Thomas Guilbert <tguilbert@chromium.org>
Reviewed-by: Dan Sanders <sandersd@chromium.org>
Reviewed-by: Jeremy Roman <jbroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#863477}

Co-authored-by: Thomas Guilbert <tguilbert@chromium.org>
--

wpt-commits: c736e9090a166395ddad2f4afbd92ab304365a8a
wpt-pr: 28057
This commit is contained in:
Blink WPT Bot 2021-03-19 22:09:16 +00:00 коммит произвёл moz-wptsync-bot
Родитель 12e0507021
Коммит e19786df46
3 изменённых файлов: 96 добавлений и 24 удалений

Просмотреть файл

@ -1,29 +1,6 @@
// META: global=window
// META: script=/webcodecs/utils.js
function make_audio_frame(timestamp, channels, sampleRate, length) {
let buffer = new AudioBuffer({
length: length,
numberOfChannels: channels,
sampleRate: sampleRate
});
for (var channel = 0; channel < buffer.numberOfChannels; channel++) {
// This gives us the actual array that contains the data
var array = buffer.getChannelData(channel);
let hz = 100 + channel * 50; // sound frequency
for (var i = 0; i < array.length; i++) {
let t = (i / sampleRate) * hz * (Math.PI * 2);
array[i] = Math.sin(t);
}
}
return new AudioFrame({
timestamp: timestamp,
buffer: buffer
});
}
// Merge all audio buffers into a new big one with all the data.
function join_buffers(buffers) {
assert_greater_than_equal(buffers.length, 0);
@ -326,4 +303,4 @@ promise_test(async t => {
assert_not_equals(decoder_config, null);
assert_not_equals(decoder_config.description, null);
encoder.close();
}, "Emit decoder config and extra data.");
}, "Emit decoder config and extra data.");

Просмотреть файл

@ -0,0 +1,72 @@
// META: global=window
// META: script=/common/media.js
// META: script=/webcodecs/utils.js
var defaultInit = {
timestamp: 1234,
channels: 2,
sampleRate: 8000,
frames: 100,
}
function createDefaultAudioFrame() {
return make_audio_frame(defaultInit.timestamp,
defaultInit.channels,
defaultInit.sampleRate,
defaultInit.frames);
}
async_test(t => {
let localFrame = createDefaultAudioFrame();
let channel = new MessageChannel();
let localPort = channel.port1;
let externalPort = channel.port2;
externalPort.onmessage = t.step_func((e) => {
let externalFrame = e.data;
let buffer = externalFrame.buffer;
// We should have a valid deserialized buffer.
assert_true(buffer != undefined || buffer != null);
assert_equals(buffer.numberOfChannels,
localFrame.buffer.numberOfChannels, "numberOfChannels");
for (var channel = 0; channel < buffer.numberOfChannels; channel++) {
// This gives us the actual array that contains the data
var dest_array = buffer.getChannelData(channel);
var source_array = localFrame.buffer.getChannelData(channel);
for (var i = 0; i < dest_array.length; i+=10) {
assert_equals(dest_array[i], source_array[i],
"data (ch=" + channel + ", i=" + i + ")");
}
}
externalFrame.close();
externalPort.postMessage("Done");
})
localPort.onmessage = t.step_func_done((e) => {
assert_true(localFrame.buffer != null);
localFrame.close();
})
localPort.postMessage(localFrame);
}, 'Verify closing frames does not propagate accross contexts.');
async_test(t => {
let localFrame = createDefaultAudioFrame();
let channel = new MessageChannel();
let localPort = channel.port1;
localPort.onmessage = t.unreached_func();
localFrame.close();
assert_throws_dom("DataCloneError", () => {
localPort.postMessage(localFrame);
});
t.done();
}, 'Verify posting closed frames throws.');

Просмотреть файл

@ -1,3 +1,26 @@
function make_audio_frame(timestamp, channels, sampleRate, length) {
let buffer = new AudioBuffer({
length: length,
numberOfChannels: channels,
sampleRate: sampleRate
});
for (var channel = 0; channel < buffer.numberOfChannels; channel++) {
// This gives us the actual array that contains the data
var array = buffer.getChannelData(channel);
let hz = 100 + channel * 50; // sound frequency
for (var i = 0; i < array.length; i++) {
let t = (i / sampleRate) * hz * (Math.PI * 2);
array[i] = Math.sin(t);
}
}
return new AudioFrame({
timestamp: timestamp,
buffer: buffer
});
}
function makeOffscreenCanvas(width, height) {
let canvas = new OffscreenCanvas(width, height);
let ctx = canvas.getContext('2d');