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
|
/*
* Copyright (C) 2010, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#include "third_party/blink/renderer/modules/webaudio/script_processor_node.h"
#include <memory>
#include "base/compiler_specific.h"
#include "base/synchronization/waitable_event.h"
#include "base/trace_event/trace_event.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable_creation_key.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/modules/webaudio/audio_buffer.h"
#include "third_party/blink/renderer/modules/webaudio/audio_graph_tracer.h"
#include "third_party/blink/renderer/modules/webaudio/audio_node_input.h"
#include "third_party/blink/renderer/modules/webaudio/audio_node_output.h"
#include "third_party/blink/renderer/modules/webaudio/audio_processing_event.h"
#include "third_party/blink/renderer/modules/webaudio/base_audio_context.h"
#include "third_party/blink/renderer/modules/webaudio/realtime_audio_destination_node.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
namespace blink {
namespace {
bool IsAudioBufferDetached(AudioBuffer* buffer) {
bool is_buffer_detached = false;
for (unsigned channel = 0; channel < buffer->numberOfChannels(); ++channel) {
if (buffer->getChannelData(channel)->buffer()->IsDetached()) {
is_buffer_detached = true;
break;
}
}
return is_buffer_detached;
}
bool BufferTopologyMatches(AudioBuffer* buffer_1, AudioBuffer* buffer_2) {
return (buffer_1->numberOfChannels() == buffer_2->numberOfChannels()) &&
(buffer_1->length() == buffer_2->length()) &&
(buffer_1->sampleRate() == buffer_2->sampleRate());
}
uint32_t ChooseBufferSize(uint32_t callback_buffer_size) {
// Choose a buffer size based on the audio hardware buffer size. Arbitrarily
// make it a power of two that is 4 times greater than the hardware buffer
// size.
// TODO(crbug.com/855758): What is the best way to choose this?
uint32_t buffer_size =
1 << static_cast<uint32_t>(log2(4 * callback_buffer_size) + 0.5);
if (buffer_size < 256) {
return 256;
}
if (buffer_size > 16384) {
return 16384;
}
return buffer_size;
}
} // namespace
ScriptProcessorNode::ScriptProcessorNode(BaseAudioContext& context,
float sample_rate,
uint32_t buffer_size,
uint32_t number_of_input_channels,
uint32_t number_of_output_channels)
: AudioNode(context), ActiveScriptWrappable<ScriptProcessorNode>({}) {
// Regardless of the allowed buffer sizes, we still need to process at the
// granularity of the AudioNode.
if (buffer_size < context.GetDeferredTaskHandler().RenderQuantumFrames()) {
buffer_size = context.GetDeferredTaskHandler().RenderQuantumFrames();
}
// Create double buffers on both the input and output sides.
// These AudioBuffers will be directly accessed in the main thread by
// JavaScript.
for (uint32_t i = 0; i < 2; ++i) {
AudioBuffer* input_buffer =
number_of_input_channels ? AudioBuffer::Create(number_of_input_channels,
buffer_size, sample_rate)
: nullptr;
AudioBuffer* output_buffer =
number_of_output_channels
? AudioBuffer::Create(number_of_output_channels, buffer_size,
sample_rate)
: nullptr;
input_buffers_.push_back(input_buffer);
output_buffers_.push_back(output_buffer);
}
external_input_buffer_ = AudioBuffer::Create(
number_of_input_channels, buffer_size, sample_rate);
external_output_buffer_ = AudioBuffer::Create(
number_of_output_channels, buffer_size, sample_rate);
SetHandler(ScriptProcessorHandler::Create(
*this, sample_rate, buffer_size, number_of_input_channels,
number_of_output_channels, input_buffers_, output_buffers_));
}
ScriptProcessorNode* ScriptProcessorNode::Create(
BaseAudioContext& context,
ExceptionState& exception_state) {
DCHECK(IsMainThread());
// Default buffer size is 0 (let WebAudio choose) with 2 inputs and 2
// outputs.
return Create(context, 0, 2, 2, exception_state);
}
ScriptProcessorNode* ScriptProcessorNode::Create(
BaseAudioContext& context,
uint32_t requested_buffer_size,
ExceptionState& exception_state) {
DCHECK(IsMainThread());
// Default is 2 inputs and 2 outputs.
return Create(context, requested_buffer_size, 2, 2, exception_state);
}
ScriptProcessorNode* ScriptProcessorNode::Create(
BaseAudioContext& context,
uint32_t requested_buffer_size,
uint32_t number_of_input_channels,
ExceptionState& exception_state) {
DCHECK(IsMainThread());
// Default is 2 outputs.
return Create(context, requested_buffer_size, number_of_input_channels, 2,
exception_state);
}
ScriptProcessorNode* ScriptProcessorNode::Create(
BaseAudioContext& context,
uint32_t requested_buffer_size,
uint32_t number_of_input_channels,
uint32_t number_of_output_channels,
ExceptionState& exception_state) {
DCHECK(IsMainThread());
if (number_of_input_channels == 0 && number_of_output_channels == 0) {
exception_state.ThrowDOMException(
DOMExceptionCode::kIndexSizeError,
"number of input channels and output channels cannot both be zero.");
return nullptr;
}
if (number_of_input_channels > BaseAudioContext::MaxNumberOfChannels()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kIndexSizeError,
"number of input channels (" +
String::Number(number_of_input_channels) + ") exceeds maximum (" +
String::Number(BaseAudioContext::MaxNumberOfChannels()) + ").");
return nullptr;
}
if (number_of_output_channels > BaseAudioContext::MaxNumberOfChannels()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kIndexSizeError,
"number of output channels (" +
String::Number(number_of_output_channels) + ") exceeds maximum (" +
String::Number(BaseAudioContext::MaxNumberOfChannels()) + ").");
return nullptr;
}
// Sanitize user-supplied buffer size.
uint32_t buffer_size;
switch (requested_buffer_size) {
case 0:
// Choose an appropriate size. For an AudioContext that is not closed, we
// need to choose an appropriate size based on the callback buffer size.
if (context.HasRealtimeConstraint() && !context.IsContextCleared()) {
RealtimeAudioDestinationHandler& destination_handler =
static_cast<RealtimeAudioDestinationHandler&>(
context.destination()->GetAudioDestinationHandler());
buffer_size =
ChooseBufferSize(destination_handler.GetCallbackBufferSize());
} else {
// An OfflineAudioContext has no callback buffer size, so just use the
// minimum. If the realtime context is closed, we can't guarantee the
// we can get the callback size, so use this same default. (With the
// context closed, there's not much you can do with this node anyway.)
buffer_size = 256;
}
break;
case 256:
case 512:
case 1024:
case 2048:
case 4096:
case 8192:
case 16384:
buffer_size = requested_buffer_size;
break;
default:
exception_state.ThrowDOMException(
DOMExceptionCode::kIndexSizeError,
"buffer size (" + String::Number(requested_buffer_size) +
") must be 0 or a power of two between 256 and 16384.");
return nullptr;
}
ScriptProcessorNode* node = MakeGarbageCollected<ScriptProcessorNode>(
context, context.sampleRate(), buffer_size, number_of_input_channels,
number_of_output_channels);
if (!node) {
return nullptr;
}
return node;
}
uint32_t ScriptProcessorNode::bufferSize() const {
return static_cast<ScriptProcessorHandler&>(Handler()).BufferSize();
}
void ScriptProcessorNode::DispatchEvent(double playback_time,
uint32_t double_buffer_index) {
DCHECK(IsMainThread());
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("webaudio.audionode"),
"ScriptProcessorNode::DispatchEvent");
ScriptProcessorHandler& handler =
static_cast<ScriptProcessorHandler&>(Handler());
{
base::AutoLock locker(handler.GetBufferLock());
TRACE_EVENT1(
TRACE_DISABLED_BY_DEFAULT("webaudio.audionode"),
"ScriptProcessorNode::DispatchEvent (copy input under lock)",
"double_buffer_index", double_buffer_index);
AudioBuffer* backing_input_buffer =
input_buffers_.at(double_buffer_index).Get();
// The backing buffer can be `nullptr`, when the number of input channels
// is 0.
if (backing_input_buffer) {
// Also the author code might have transferred `external_input_buffer_` to
// other threads or replaced it with a different AudioBuffer object. Then
// re-create a new buffer instance.
if (IsAudioBufferDetached(external_input_buffer_) ||
!BufferTopologyMatches(backing_input_buffer,
external_input_buffer_)) {
TRACE_EVENT0(
TRACE_DISABLED_BY_DEFAULT("webaudio.audionode"),
"ScriptProcessorNode::DispatchEvent (create input AudioBuffer)");
external_input_buffer_ = AudioBuffer::Create(
backing_input_buffer->numberOfChannels(),
backing_input_buffer->length(),
backing_input_buffer->sampleRate());
}
for (unsigned channel = 0;
channel < backing_input_buffer->numberOfChannels(); ++channel) {
const float* source = static_cast<float*>(
backing_input_buffer->getChannelData(channel)->buffer()->Data());
float* destination = static_cast<float*>(
external_input_buffer_->getChannelData(channel)->buffer()->Data());
UNSAFE_TODO(memcpy(destination, source,
backing_input_buffer->length() * sizeof(float)));
}
}
}
external_output_buffer_->Zero();
AudioNode::DispatchEvent(*AudioProcessingEvent::Create(
external_input_buffer_, external_output_buffer_, playback_time));
{
base::AutoLock locker(handler.GetBufferLock());
TRACE_EVENT1(
TRACE_DISABLED_BY_DEFAULT("webaudio.audionode"),
"ScriptProcessorNode::DispatchEvent (copy output under lock)",
"double_buffer_index", double_buffer_index);
AudioBuffer* backing_output_buffer =
output_buffers_.at(double_buffer_index).Get();
if (backing_output_buffer) {
if (IsAudioBufferDetached(external_output_buffer_) ||
!BufferTopologyMatches(backing_output_buffer,
external_output_buffer_)) {
TRACE_EVENT0(
TRACE_DISABLED_BY_DEFAULT("webaudio.audionode"),
"ScriptProcessorNode::DispatchEvent (create output AudioBuffer)");
external_output_buffer_ = AudioBuffer::Create(
backing_output_buffer->numberOfChannels(),
backing_output_buffer->length(),
backing_output_buffer->sampleRate());
}
for (unsigned channel = 0;
channel < backing_output_buffer->numberOfChannels(); ++channel) {
const float* source = static_cast<float*>(
external_output_buffer_->getChannelData(channel)->buffer()->Data());
float* destination = static_cast<float*>(
backing_output_buffer->getChannelData(channel)->buffer()->Data());
UNSAFE_TODO(memcpy(destination, source,
backing_output_buffer->length() * sizeof(float)));
}
}
}
}
bool ScriptProcessorNode::HasPendingActivity() const {
// To prevent the node from leaking after the context is closed.
if (context()->IsContextCleared()) {
return false;
}
// If `.onaudioprocess` event handler is defined, the node should not be
// GCed even if it is out of scope.
if (HasEventListeners(event_type_names::kAudioprocess)) {
return true;
}
return false;
}
void ScriptProcessorNode::Trace(Visitor* visitor) const {
visitor->Trace(input_buffers_);
visitor->Trace(output_buffers_);
visitor->Trace(external_input_buffer_);
visitor->Trace(external_output_buffer_);
AudioNode::Trace(visitor);
}
void ScriptProcessorNode::ReportDidCreate() {
GraphTracer().DidCreateAudioNode(this);
}
void ScriptProcessorNode::ReportWillBeDestroyed() {
GraphTracer().WillDestroyAudioNode(this);
}
} // namespace blink
|