-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow 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 <[email protected]> Reviewed-by: Hongchan Choi <[email protected]> Reviewed-by: Hiroki Nakagawa <[email protected]> Cr-Commit-Position: refs/heads/master@{#598619}
- Loading branch information
Showing
5 changed files
with
139 additions
and
6 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
...udio-api/the-audioworklet-interface/audioworklet-postmessage-sharedarraybuffer.https.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
|
35 changes: 35 additions & 0 deletions
35
...baudio/the-audio-api/the-audioworklet-interface/processors/sharedarraybuffer-processor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters