|
12 | 12 | #include "third_party/blink/renderer/bindings/core/v8/idl_types.h"
|
13 | 13 | #include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h"
|
14 | 14 | #include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
|
15 |
| -#include "third_party/blink/renderer/bindings/core/v8/to_v8_for_core.h" |
16 |
| -#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h" |
17 | 15 | #include "third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.h"
|
18 |
| -#include "third_party/blink/renderer/bindings/modules/v8/v8_audio_param_descriptor.h" |
19 | 16 | #include "third_party/blink/renderer/bindings/modules/v8/v8_audio_worklet_processor.h"
|
20 | 17 | #include "third_party/blink/renderer/bindings/modules/v8/v8_blink_audio_worklet_process_callback.h"
|
21 | 18 | #include "third_party/blink/renderer/bindings/modules/v8/v8_blink_audio_worklet_processor_constructor.h"
|
22 |
| -#include "third_party/blink/renderer/core/messaging/message_port.h" |
23 |
| -#include "third_party/blink/renderer/core/typed_arrays/dom_typed_array.h" |
24 |
| -#include "third_party/blink/renderer/core/workers/global_scope_creation_params.h" |
25 | 19 | #include "third_party/blink/renderer/core/workers/worker_thread.h"
|
26 |
| -#include "third_party/blink/renderer/modules/webaudio/audio_buffer.h" |
27 |
| -#include "third_party/blink/renderer/modules/webaudio/audio_worklet_processor.h" |
28 | 20 | #include "third_party/blink/renderer/modules/webaudio/audio_worklet_processor_definition.h"
|
29 |
| -#include "third_party/blink/renderer/modules/webaudio/audio_worklet_processor_error_state.h" |
30 | 21 | #include "third_party/blink/renderer/modules/webaudio/cross_thread_audio_worklet_processor_info.h"
|
31 |
| -#include "third_party/blink/renderer/platform/audio/audio_bus.h" |
32 |
| -#include "third_party/blink/renderer/platform/audio/audio_utilities.h" |
33 | 22 | #include "third_party/blink/renderer/platform/bindings/callback_method_retriever.h"
|
34 | 23 |
|
35 | 24 | namespace blink {
|
@@ -217,180 +206,6 @@ AudioWorkletProcessor* AudioWorkletGlobalScope::CreateProcessor(
|
217 | 206 | return processor;
|
218 | 207 | }
|
219 | 208 |
|
220 |
| -bool AudioWorkletGlobalScope::Process( |
221 |
| - AudioWorkletProcessor* processor, |
222 |
| - Vector<scoped_refptr<AudioBus>>* input_buses, |
223 |
| - Vector<AudioBus*>* output_buses, |
224 |
| - HashMap<String, std::unique_ptr<AudioFloatArray>>* param_value_map) { |
225 |
| - CHECK_GE(input_buses->size(), 0u); |
226 |
| - CHECK_GE(output_buses->size(), 0u); |
227 |
| - |
228 |
| - ScriptState* script_state = ScriptController()->GetScriptState(); |
229 |
| - ScriptState::Scope scope(script_state); |
230 |
| - |
231 |
| - v8::Isolate* isolate = script_state->GetIsolate(); |
232 |
| - v8::Local<v8::Context> current_context = script_state->GetContext(); |
233 |
| - AudioWorkletProcessorDefinition* definition = |
234 |
| - FindDefinition(processor->Name()); |
235 |
| - DCHECK(definition); |
236 |
| - |
237 |
| - v8::TryCatch try_catch(isolate); |
238 |
| - try_catch.SetVerbose(true); |
239 |
| - |
240 |
| - // Prepare arguments of JS callback (inputs, outputs and param_values) with |
241 |
| - // directly using V8 API because the overhead of |
242 |
| - // ToV8(HeapVector<HeapVector<DOMFloat32Array>>) is not negligible and there |
243 |
| - // is no need to externalize the array buffers. |
244 |
| - |
245 |
| - // 1st arg of JS callback: inputs |
246 |
| - v8::Local<v8::Array> inputs = v8::Array::New(isolate, input_buses->size()); |
247 |
| - uint32_t input_bus_index = 0; |
248 |
| - for (auto input_bus : *input_buses) { |
249 |
| - // If |input_bus| is null, it means the input port is not connected, and |
250 |
| - // the corresponding array for that input port should have zero channel. |
251 |
| - unsigned number_of_channels = input_bus ? input_bus->NumberOfChannels() : 0; |
252 |
| - size_t bus_length = input_bus ? input_bus->length() : 0; |
253 |
| - |
254 |
| - v8::Local<v8::Array> channels = v8::Array::New(isolate, number_of_channels); |
255 |
| - bool success; |
256 |
| - if (!inputs |
257 |
| - ->CreateDataProperty(current_context, input_bus_index++, channels) |
258 |
| - .To(&success)) { |
259 |
| - return false; |
260 |
| - } |
261 |
| - for (uint32_t channel_index = 0; channel_index < number_of_channels; |
262 |
| - ++channel_index) { |
263 |
| - v8::Local<v8::ArrayBuffer> array_buffer = |
264 |
| - v8::ArrayBuffer::New(isolate, bus_length * sizeof(float)); |
265 |
| - v8::Local<v8::Float32Array> float32_array = |
266 |
| - v8::Float32Array::New(array_buffer, 0, bus_length); |
267 |
| - if (!channels |
268 |
| - ->CreateDataProperty(current_context, channel_index, |
269 |
| - float32_array) |
270 |
| - .To(&success)) { |
271 |
| - return false; |
272 |
| - } |
273 |
| - const v8::ArrayBuffer::Contents& contents = array_buffer->GetContents(); |
274 |
| - if (input_bus) { |
275 |
| - memcpy(contents.Data(), input_bus->Channel(channel_index)->Data(), |
276 |
| - bus_length * sizeof(float)); |
277 |
| - } |
278 |
| - } |
279 |
| - } |
280 |
| - |
281 |
| - // 2nd arg of JS callback: outputs |
282 |
| - v8::Local<v8::Array> outputs = v8::Array::New(isolate, output_buses->size()); |
283 |
| - uint32_t output_bus_counter = 0; |
284 |
| - |
285 |
| - // |output_array_buffers| stores underlying array buffers so that we can copy |
286 |
| - // them back to |output_buses|. |
287 |
| - Vector<Vector<v8::Local<v8::ArrayBuffer>>> output_array_buffers; |
288 |
| - output_array_buffers.ReserveInitialCapacity(output_buses->size()); |
289 |
| - |
290 |
| - for (auto* const output_bus : *output_buses) { |
291 |
| - output_array_buffers.UncheckedAppend(Vector<v8::Local<v8::ArrayBuffer>>()); |
292 |
| - output_array_buffers.back().ReserveInitialCapacity( |
293 |
| - output_bus->NumberOfChannels()); |
294 |
| - v8::Local<v8::Array> channels = |
295 |
| - v8::Array::New(isolate, output_bus->NumberOfChannels()); |
296 |
| - bool success; |
297 |
| - if (!outputs |
298 |
| - ->CreateDataProperty(current_context, output_bus_counter++, |
299 |
| - channels) |
300 |
| - .To(&success)) { |
301 |
| - return false; |
302 |
| - } |
303 |
| - for (uint32_t channel_index = 0; |
304 |
| - channel_index < output_bus->NumberOfChannels(); ++channel_index) { |
305 |
| - v8::Local<v8::ArrayBuffer> array_buffer = |
306 |
| - v8::ArrayBuffer::New(isolate, output_bus->length() * sizeof(float)); |
307 |
| - v8::Local<v8::Float32Array> float32_array = |
308 |
| - v8::Float32Array::New(array_buffer, 0, output_bus->length()); |
309 |
| - if (!channels |
310 |
| - ->CreateDataProperty(current_context, channel_index, |
311 |
| - float32_array) |
312 |
| - .To(&success)) { |
313 |
| - return false; |
314 |
| - } |
315 |
| - output_array_buffers.back().UncheckedAppend(array_buffer); |
316 |
| - } |
317 |
| - } |
318 |
| - |
319 |
| - // 3rd arg of JS callback: param_values |
320 |
| - v8::Local<v8::Object> param_values = v8::Object::New(isolate); |
321 |
| - for (const auto& param : *param_value_map) { |
322 |
| - const String& param_name = param.key; |
323 |
| - const AudioFloatArray* param_array = param.value.get(); |
324 |
| - |
325 |
| - // If the AudioParam is constant, then the param array should have length 1. |
326 |
| - // Manually check to see if the parameter is truly constant. |
327 |
| - unsigned array_size = 1; |
328 |
| - |
329 |
| - for (unsigned k = 1; k < param_array->size(); ++k) { |
330 |
| - if (param_array->Data()[k] != param_array->Data()[0]) { |
331 |
| - array_size = param_array->size(); |
332 |
| - break; |
333 |
| - } |
334 |
| - } |
335 |
| - |
336 |
| - v8::Local<v8::ArrayBuffer> array_buffer = |
337 |
| - v8::ArrayBuffer::New(isolate, array_size * sizeof(float)); |
338 |
| - v8::Local<v8::Float32Array> float32_array = |
339 |
| - v8::Float32Array::New(array_buffer, 0, array_size); |
340 |
| - bool success; |
341 |
| - if (!param_values |
342 |
| - ->CreateDataProperty(current_context, |
343 |
| - V8String(isolate, param_name.IsolatedCopy()), |
344 |
| - float32_array) |
345 |
| - .To(&success)) { |
346 |
| - return false; |
347 |
| - } |
348 |
| - const v8::ArrayBuffer::Contents& contents = array_buffer->GetContents(); |
349 |
| - memcpy(contents.Data(), param_array->Data(), array_size * sizeof(float)); |
350 |
| - } |
351 |
| - |
352 |
| - // Perform JS function process() in AudioWorkletProcessor instance. The actual |
353 |
| - // V8 operation happens here to make the AudioWorkletProcessor class a thin |
354 |
| - // wrapper of v8::Object instance. |
355 |
| - ScriptValue result; |
356 |
| - if (!definition->ProcessFunction() |
357 |
| - ->Invoke(processor, ScriptValue(isolate, inputs), |
358 |
| - ScriptValue(isolate, outputs), |
359 |
| - ScriptValue(isolate, param_values)) |
360 |
| - .To(&result)) { |
361 |
| - // process() method call failed for some reason or an exception was thrown |
362 |
| - // by the user supplied code. Disable the processor to exclude it from the |
363 |
| - // subsequent rendering task. |
364 |
| - processor->SetErrorState(AudioWorkletProcessorErrorState::kProcessError); |
365 |
| - return false; |
366 |
| - } |
367 |
| - |
368 |
| - // Copy |sequence<sequence<Float32Array>>| back to the original |
369 |
| - // |Vector<AudioBus*>|. While iterating, we also check if the size of backing |
370 |
| - // array buffer is changed. When the size does not match, silence the buffer. |
371 |
| - for (uint32_t output_bus_index = 0; output_bus_index < output_buses->size(); |
372 |
| - ++output_bus_index) { |
373 |
| - AudioBus* output_bus = (*output_buses)[output_bus_index]; |
374 |
| - for (uint32_t channel_index = 0; |
375 |
| - channel_index < output_bus->NumberOfChannels(); ++channel_index) { |
376 |
| - const v8::ArrayBuffer::Contents& contents = |
377 |
| - output_array_buffers[output_bus_index][channel_index]->GetContents(); |
378 |
| - const size_t size = output_bus->length() * sizeof(float); |
379 |
| - if (contents.ByteLength() == size) { |
380 |
| - memcpy(output_bus->Channel(channel_index)->MutableData(), |
381 |
| - contents.Data(), size); |
382 |
| - } else { |
383 |
| - memset(output_bus->Channel(channel_index)->MutableData(), 0, size); |
384 |
| - } |
385 |
| - } |
386 |
| - } |
387 |
| - |
388 |
| - // Return the value from the user-supplied |process()| function. It is |
389 |
| - // used to maintain the lifetime of the node and the processor. |
390 |
| - DCHECK(!try_catch.HasCaught()); |
391 |
| - return result.V8Value()->IsTrue(); |
392 |
| -} |
393 |
| - |
394 | 209 | AudioWorkletProcessorDefinition* AudioWorkletGlobalScope::FindDefinition(
|
395 | 210 | const String& name) {
|
396 | 211 | return processor_definition_map_.at(name);
|
|
0 commit comments