Skip to content

Commit

Permalink
Bug 1497757 [wpt PR 13445] - Allow posting a SharedArrayBuffer to Aud…
Browse files Browse the repository at this point in the history
…ioWorklet, a=testonly

Automatic update from web-platform-testsAllow posting a SharedArrayBuffer to AudioWorklet

The HTML spec only allows passing SharedArrayBuffers to an agent in the
same agent cluster. Worklets currently do not belong to any agent
cluster, so a SharedArrayBuffer can not be shared with a worklet.

However, it is intended that this is possible; see, for example,
w3c/css-houdini-drafts#380 and the
AudioWorklet article here:
https://developers.google.com/web/updates/2018/06/audio-worklet-design-pattern#webaudio_powerhouse_audio_worklet_and_sharedarraybuffer

This change funnels the agent cluster ID through when creating a
ThreadedWorklet, so a SharedArrayBuffer can be shared as long as the
creator of the Worklet's thread is in the same agent cluster. It is not
clear that this is the behavior that will be specified, however.

Bug: chromium:892067
Change-Id: If1a2187ae38da41f2389538c07e7b04921c6128f
Reviewed-on: https://chromium-review.googlesource.com/c/1262932
Commit-Queue: Ben Smith <binji@chromium.org>
Reviewed-by: Hongchan Choi <hongchan@chromium.org>
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/master@{#598619}

--

wpt-commits: 2c89bbecfab9a69190906abd7610c3bc62303dd4
wpt-pr: 13445
  • Loading branch information
binji authored and moz-wptsync-bot committed Oct 14, 2018
1 parent c543493 commit 574c79d
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
@@ -0,0 +1,85 @@
<!DOCTYPE html>
<html>
<head>
<title>
Test passing SharedArrayBuffer to an AudioWorklet
</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/webaudio/resources/audit.js"></script>
</head>
<body>
<script id="layout-test-code">
let audit = Audit.createTaskRunner();

let context = new AudioContext();

let filePath = 'processors/sharedarraybuffer-processor.js';

if (window.SharedArrayBuffer) {
audit.define(
'Test postMessage from AudioWorkletProcessor to AudioWorkletNode',
(task, should) => {
let workletNode =
new AudioWorkletNode(context, 'sharedarraybuffer-processor');

// After it is created, the worklet will send a new
// SharedArrayBuffer to the main thread.
//
// The worklet will then wait to receive a message from the main
// thread.
//
// When it receives the message, it will check whether it is a
// SharedArrayBuffer, and send this information back to the main
// thread.

workletNode.port.onmessage = (event) => {
let data = event.data;
switch (data.state) {
case 'created':
should(
data.sab instanceof SharedArrayBuffer,
'event.data.sab from worklet is an instance of SharedArrayBuffer')
.beTrue();

// Send a SharedArrayBuffer back to the worklet.
let sab = new SharedArrayBuffer(8);
workletNode.port.postMessage(sab);
break;

case 'received message':
should(data.isSab, 'event.data from main thread is an instance of SharedArrayBuffer')
.beTrue();
task.done();
break;

default:
should(false,
`Got unexpected message from worklet: ${data.state}`)
.beTrue();
task.done();
break;
}
};

workletNode.port.onmessageerror = (event) => {
should(false, 'Got messageerror from worklet').beTrue();
task.done();
};
});
} else {
// NOTE(binji): SharedArrayBuffer is only enabled where we have site
// isolation.
audit.define('Skipping test because SharedArrayBuffer is not defined',
(task, should) => {
task.done();
});
}

context.audioWorklet.addModule(filePath).then(() => {
audit.run();
});
</script>
</body>
</html>

@@ -0,0 +1,35 @@
/**
* @class SharedArrayBufferProcessor
* @extends AudioWorkletProcessor
*
* This processor class demonstrates passing SharedArrayBuffers to and from
* workers.
*/
class SharedArrayBufferProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.port.onmessage = this.handleMessage.bind(this);
this.port.onmessageerror = this.handleMessageError.bind(this);
let sab = new SharedArrayBuffer(8);
this.port.postMessage({state: 'created', sab});
}

handleMessage(event) {
this.port.postMessage({
state: 'received message',
isSab: event.data instanceof SharedArrayBuffer
});
}

handleMessageError(event) {
this.port.postMessage({
state: 'received messageerror'
});
}

process() {
return true;
}
}

registerProcessor('sharedarraybuffer-processor', SharedArrayBufferProcessor);

0 comments on commit 574c79d

Please sign in to comment.