summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/webaudio/audio_worklet_global_scope.cc
blob: 72e7eb137d9a92b91baaeffddd38240819f033dd (plain)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/webaudio/audio_worklet_global_scope.h"

#include <memory>
#include <utility>

#include "base/auto_reset.h"
#include "base/stl_util.h"
#include "third_party/blink/renderer/bindings/core/v8/idl_types.h"
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/to_v8_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_audio_param_descriptor.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_audio_worklet_processor.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_blink_audio_worklet_process_callback.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_blink_audio_worklet_processor_constructor.h"
#include "third_party/blink/renderer/core/messaging/message_port.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_typed_array.h"
#include "third_party/blink/renderer/core/workers/global_scope_creation_params.h"
#include "third_party/blink/renderer/core/workers/worker_thread.h"
#include "third_party/blink/renderer/modules/webaudio/audio_buffer.h"
#include "third_party/blink/renderer/modules/webaudio/audio_param_descriptor.h"
#include "third_party/blink/renderer/modules/webaudio/audio_worklet_processor.h"
#include "third_party/blink/renderer/modules/webaudio/audio_worklet_processor_definition.h"
#include "third_party/blink/renderer/modules/webaudio/audio_worklet_processor_error_state.h"
#include "third_party/blink/renderer/modules/webaudio/cross_thread_audio_worklet_processor_info.h"
#include "third_party/blink/renderer/platform/audio/audio_bus.h"
#include "third_party/blink/renderer/platform/audio/audio_utilities.h"
#include "third_party/blink/renderer/platform/bindings/callback_method_retriever.h"

namespace blink {

AudioWorkletGlobalScope::AudioWorkletGlobalScope(
    std::unique_ptr<GlobalScopeCreationParams> creation_params,
    WorkerThread* thread)
    : WorkletGlobalScope(std::move(creation_params),
                         thread->GetWorkerReportingProxy(),
                         thread) {}

AudioWorkletGlobalScope::~AudioWorkletGlobalScope() = default;

void AudioWorkletGlobalScope::Dispose() {
  DCHECK(IsContextThread());
  is_closing_ = true;
  WorkletGlobalScope::Dispose();
}

void AudioWorkletGlobalScope::registerProcessor(
    const String& name,
    V8BlinkAudioWorkletProcessorConstructor* processor_ctor,
    ExceptionState& exception_state) {
  DCHECK(IsContextThread());

  if (processor_definition_map_.Contains(name)) {
    exception_state.ThrowDOMException(
        DOMExceptionCode::kNotSupportedError,
        "A class with name:'" + name + "' is already registered.");
    return;
  }

  // TODO(hongchan): this is not stated in the spec, but seems necessary.
  // https://github.com/WebAudio/web-audio-api/issues/1172
  if (name.IsEmpty()) {
    exception_state.ThrowTypeError("The empty string is not a valid name.");
    return;
  }

  if (!processor_ctor->IsConstructor()) {
    exception_state.ThrowTypeError(
        "The provided callback is not a constructor.");
    return;
  }

  CallbackMethodRetriever retriever(processor_ctor);
  retriever.GetPrototypeObject(exception_state);
  if (exception_state.HadException())
    return;

  v8::Local<v8::Function> v8_process =
      retriever.GetMethodOrThrow("process", exception_state);
  if (exception_state.HadException())
    return;
  V8BlinkAudioWorkletProcessCallback* process =
      V8BlinkAudioWorkletProcessCallback::Create(v8_process);

  // constructor() and process() functions are successfully parsed from the
  // script code, thus create the definition. The rest of parsing process
  // (i.e. parameterDescriptors) is optional.
  AudioWorkletProcessorDefinition* definition =
      AudioWorkletProcessorDefinition::Create(name, processor_ctor, process);

  v8::Isolate* isolate = processor_ctor->GetIsolate();
  v8::Local<v8::Context> current_context = isolate->GetCurrentContext();

  v8::Local<v8::Value> v8_parameter_descriptors;
  {
    v8::TryCatch try_catch(isolate);
    if (!processor_ctor->CallbackObject()
             ->Get(current_context,
                   V8AtomicString(isolate, "parameterDescriptors"))
             .ToLocal(&v8_parameter_descriptors)) {
      exception_state.RethrowV8Exception(try_catch.Exception());
      return;
    }
  }

  if (!v8_parameter_descriptors->IsNullOrUndefined()) {
    // Optional 'parameterDescriptors' property is found.
    const HeapVector<Member<AudioParamDescriptor>>& audio_param_descriptors =
        NativeValueTraits<IDLSequence<AudioParamDescriptor>>::NativeValue(
            isolate, v8_parameter_descriptors, exception_state);
    if (exception_state.HadException())
      return;
    definition->SetAudioParamDescriptors(audio_param_descriptors);
  }

  processor_definition_map_.Set(name, definition);
}

AudioWorkletProcessor* AudioWorkletGlobalScope::CreateProcessor(
    const String& name,
    MessagePortChannel message_port_channel,
    scoped_refptr<SerializedScriptValue> node_options) {
  DCHECK(IsContextThread());

  // The registered definition is already checked by AudioWorkletNode
  // construction process, so the |definition| here must be valid.
  AudioWorkletProcessorDefinition* definition = FindDefinition(name);
  DCHECK(definition);

  ScriptState* script_state = ScriptController()->GetScriptState();
  ScriptState::Scope scope(script_state);

  // V8 object instance construction: this construction process is here to make
  // the AudioWorkletProcessor class a thin wrapper of v8::Object instance.
  v8::Isolate* isolate = script_state->GetIsolate();
  v8::TryCatch try_catch(isolate);
  try_catch.SetVerbose(true);  // Route errors/exceptions to the dev console.

  DCHECK(!processor_creation_params_);
  // There is no way to pass additional constructor arguments that are not
  // described in Web IDL, the static constructor will look up
  // |processor_creation_params_| in the global scope to perform the
  // construction properly.
  base::AutoReset<std::unique_ptr<ProcessorCreationParams>>
      processor_creation_extra_param(
          &processor_creation_params_,
          std::make_unique<ProcessorCreationParams>(
              name, std::move(message_port_channel)));

  ScriptValue options(isolate,
                      ToV8(node_options->Deserialize(isolate), script_state));

  ScriptValue instance;
  if (!definition->ConstructorFunction()->Construct(options).To(&instance)) {
    return nullptr;
  }

  // ToImplWithTypeCheck() may return nullptr when the type does not match.
  AudioWorkletProcessor* processor =
      V8AudioWorkletProcessor::ToImplWithTypeCheck(isolate, instance.V8Value());

  if (processor) {
    processor_instances_.push_back(processor);
  }

  return processor;
}

bool AudioWorkletGlobalScope::Process(
    AudioWorkletProcessor* processor,
    Vector<AudioBus*>* input_buses,
    Vector<AudioBus*>* output_buses,
    HashMap<String, std::unique_ptr<AudioFloatArray>>* param_value_map) {
  CHECK_GE(input_buses->size(), 0u);
  CHECK_GE(output_buses->size(), 0u);

  ScriptState* script_state = ScriptController()->GetScriptState();
  ScriptState::Scope scope(script_state);

  v8::Isolate* isolate = script_state->GetIsolate();
  v8::Local<v8::Context> current_context = script_state->GetContext();
  AudioWorkletProcessorDefinition* definition =
      FindDefinition(processor->Name());
  DCHECK(definition);

  v8::TryCatch try_catch(isolate);
  try_catch.SetVerbose(true);

  // Prepare arguments of JS callback (inputs, outputs and param_values) with
  // directly using V8 API because the overhead of
  // ToV8(HeapVector<HeapVector<DOMFloat32Array>>) is not negligible and there
  // is no need to externalize the array buffers.

  // 1st arg of JS callback: inputs
  v8::Local<v8::Array> inputs = v8::Array::New(isolate, input_buses->size());
  uint32_t input_bus_index = 0;
  for (auto* const input_bus : *input_buses) {
    // If |input_bus| is null, then the input is not connected, and
    // the array for that input should have one channel and a length
    // of 0.
    unsigned number_of_channels = input_bus ? input_bus->NumberOfChannels() : 1;
    size_t bus_length = input_bus ? input_bus->length() : 0;

    v8::Local<v8::Array> channels = v8::Array::New(isolate, number_of_channels);
    bool success;
    if (!inputs
             ->CreateDataProperty(current_context, input_bus_index++, channels)
             .To(&success)) {
      return false;
    }
    for (uint32_t channel_index = 0; channel_index < number_of_channels;
         ++channel_index) {
      v8::Local<v8::ArrayBuffer> array_buffer =
          v8::ArrayBuffer::New(isolate, bus_length * sizeof(float));
      v8::Local<v8::Float32Array> float32_array =
          v8::Float32Array::New(array_buffer, 0, bus_length);
      if (!channels
               ->CreateDataProperty(current_context, channel_index,
                                    float32_array)
               .To(&success)) {
        return false;
      }
      const v8::ArrayBuffer::Contents& contents = array_buffer->GetContents();
      if (input_bus) {
        memcpy(contents.Data(), input_bus->Channel(channel_index)->Data(),
               bus_length * sizeof(float));
      }
    }
  }

  // 2nd arg of JS callback: outputs
  v8::Local<v8::Array> outputs = v8::Array::New(isolate, output_buses->size());
  uint32_t output_bus_counter = 0;

  // |output_array_buffers| stores underlying array buffers so that we can copy
  // them back to |output_buses|.
  Vector<Vector<v8::Local<v8::ArrayBuffer>>> output_array_buffers;
  output_array_buffers.ReserveInitialCapacity(output_buses->size());

  for (auto* const output_bus : *output_buses) {
    output_array_buffers.UncheckedAppend(Vector<v8::Local<v8::ArrayBuffer>>());
    output_array_buffers.back().ReserveInitialCapacity(
        output_bus->NumberOfChannels());
    v8::Local<v8::Array> channels =
        v8::Array::New(isolate, output_bus->NumberOfChannels());
    bool success;
    if (!outputs
             ->CreateDataProperty(current_context, output_bus_counter++,
                                  channels)
             .To(&success)) {
      return false;
    }
    for (uint32_t channel_index = 0;
         channel_index < output_bus->NumberOfChannels(); ++channel_index) {
      v8::Local<v8::ArrayBuffer> array_buffer =
          v8::ArrayBuffer::New(isolate, output_bus->length() * sizeof(float));
      v8::Local<v8::Float32Array> float32_array =
          v8::Float32Array::New(array_buffer, 0, output_bus->length());
      if (!channels
               ->CreateDataProperty(current_context, channel_index,
                                    float32_array)
               .To(&success)) {
        return false;
      }
      output_array_buffers.back().UncheckedAppend(array_buffer);
    }
  }

  // 3rd arg of JS callback: param_values
  v8::Local<v8::Object> param_values = v8::Object::New(isolate);
  for (const auto& param : *param_value_map) {
    const String& param_name = param.key;
    const AudioFloatArray* param_array = param.value.get();

    // If the AudioParam is constant, then the param array should have length 1.
    // Manually check to see if the parameter is truly constant.
    unsigned array_size = 1;

    for (unsigned k = 1; k < param_array->size(); ++k) {
      if (param_array->Data()[k] != param_array->Data()[0]) {
        array_size = param_array->size();
        break;
      }
    }

    v8::Local<v8::ArrayBuffer> array_buffer =
        v8::ArrayBuffer::New(isolate, array_size * sizeof(float));
    v8::Local<v8::Float32Array> float32_array =
        v8::Float32Array::New(array_buffer, 0, array_size);
    bool success;
    if (!param_values
             ->CreateDataProperty(current_context,
                                  V8String(isolate, param_name.IsolatedCopy()),
                                  float32_array)
             .To(&success)) {
      return false;
    }
    const v8::ArrayBuffer::Contents& contents = array_buffer->GetContents();
    memcpy(contents.Data(), param_array->Data(), array_size * sizeof(float));
  }

  // Perform JS function process() in AudioWorkletProcessor instance. The actual
  // V8 operation happens here to make the AudioWorkletProcessor class a thin
  // wrapper of v8::Object instance.
  ScriptValue result;
  if (!definition->ProcessFunction()
           ->Invoke(processor, ScriptValue(isolate, inputs),
                    ScriptValue(isolate, outputs),
                    ScriptValue(isolate, param_values))
           .To(&result)) {
    // process() method call failed for some reason or an exception was thrown
    // by the user supplied code. Disable the processor to exclude it from the
    // subsequent rendering task.
    processor->SetErrorState(AudioWorkletProcessorErrorState::kProcessError);
    return false;
  }

  // Copy |sequence<sequence<Float32Array>>| back to the original
  // |Vector<AudioBus*>|. While iterating, we also check if the size of backing
  // array buffer is changed. When the size does not match, silence the buffer.
  for (uint32_t output_bus_index = 0; output_bus_index < output_buses->size();
       ++output_bus_index) {
    AudioBus* output_bus = (*output_buses)[output_bus_index];
    for (uint32_t channel_index = 0;
         channel_index < output_bus->NumberOfChannels(); ++channel_index) {
      const v8::ArrayBuffer::Contents& contents =
          output_array_buffers[output_bus_index][channel_index]->GetContents();
      const size_t size = output_bus->length() * sizeof(float);
      if (contents.ByteLength() == size) {
        memcpy(output_bus->Channel(channel_index)->MutableData(),
               contents.Data(), size);
      } else {
        memset(output_bus->Channel(channel_index)->MutableData(), 0, size);
      }
    }
  }

  // Return the value from the user-supplied |process()| function. It is
  // used to maintain the lifetime of the node and the processor.
  DCHECK(!try_catch.HasCaught());
  return result.V8Value()->IsTrue();
}

AudioWorkletProcessorDefinition* AudioWorkletGlobalScope::FindDefinition(
    const String& name) {
  return processor_definition_map_.at(name);
}

unsigned AudioWorkletGlobalScope::NumberOfRegisteredDefinitions() {
  return processor_definition_map_.size();
}

std::unique_ptr<Vector<CrossThreadAudioWorkletProcessorInfo>>
AudioWorkletGlobalScope::WorkletProcessorInfoListForSynchronization() {
  auto processor_info_list =
      std::make_unique<Vector<CrossThreadAudioWorkletProcessorInfo>>();
  for (auto definition_entry : processor_definition_map_) {
    if (!definition_entry.value->IsSynchronized()) {
      definition_entry.value->MarkAsSynchronized();
      processor_info_list->emplace_back(*definition_entry.value);
    }
  }
  return processor_info_list;
}

ProcessorCreationParams* AudioWorkletGlobalScope::GetProcessorCreationParams() {
  return processor_creation_params_.get();
}

void AudioWorkletGlobalScope::SetCurrentFrame(size_t current_frame) {
  current_frame_ = current_frame;
}

void AudioWorkletGlobalScope::SetSampleRate(float sample_rate) {
  sample_rate_ = sample_rate;
}

double AudioWorkletGlobalScope::currentTime() const {
  return sample_rate_ > 0.0
        ? current_frame_ / static_cast<double>(sample_rate_)
        : 0.0;
}

void AudioWorkletGlobalScope::Trace(blink::Visitor* visitor) {
  visitor->Trace(processor_definition_map_);
  visitor->Trace(processor_instances_);
  WorkletGlobalScope::Trace(visitor);
}

}  // namespace blink