File: audio_worklet_processor.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (160 lines) | stat: -rw-r--r-- 6,719 bytes parent folder | download | duplicates (5)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_WORKLET_PROCESSOR_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_WORKLET_PROCESSOR_H_

#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/modules/webaudio/audio_worklet_processor_error_state.h"
#include "third_party/blink/renderer/platform/audio/audio_array.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/bindings/trace_wrapper_v8_reference.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_vector.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "v8/include/v8.h"

namespace blink {

class AudioBus;
class AudioWorkletGlobalScope;
class AudioWorkletProcessorDefinition;
class MessagePort;
class ExecutionContext;
class V8BlinkAudioWorkletProcessCallback;

// AudioWorkletProcessor class represents the active instance created from
// AudioWorkletProcessorDefinition. AudioWorkletNodeHandler invokes `.process()`
// method in this object upon graph rendering.
//
// This is constructed and destroyed on a worker thread, and all methods also
// must be called on the worker thread.
class MODULES_EXPORT AudioWorkletProcessor : public ScriptWrappable {
  DEFINE_WRAPPERTYPEINFO();

 public:
  // This static factory should be called after an instance of
  // AudioWorkletNode gets created by user-supplied JS code in the main
  // thread. This factory must not be called by user in
  // AudioWorkletGlobalScope.
  static AudioWorkletProcessor* Create(ExecutionContext*, ExceptionState&);

  AudioWorkletProcessor(AudioWorkletGlobalScope*,
                        const String& name,
                        MessagePort*);
  ~AudioWorkletProcessor() override;

  // `AudioWorkletHandler` invokes this method to process audio.
  bool Process(
      const Vector<scoped_refptr<AudioBus>>& inputs,
      Vector<scoped_refptr<AudioBus>>& outputs,
      const HashMap<String, std::unique_ptr<AudioFloatArray>>& param_value_map);

  const String& Name() const { return name_; }

  void SetErrorState(AudioWorkletProcessorErrorState);
  AudioWorkletProcessorErrorState GetErrorState() const;
  bool hasErrorOccurred() const;

  // IDL
  MessagePort* port() const;

  void Trace(Visitor*) const override;

 private:
  using BackingArrayBuffers =
    HeapVector<HeapVector<TraceWrapperV8Reference<v8::ArrayBuffer>>>;

  // An AudioPort is an array of one or more AudioBus objects, which is
  // represented by:
  // Vector<scoped_refptr<AudioBus>> or TraceWrapperV8Reference<v8::Array>.
  // An AudioBus can contain one or more AudioChannels, that are represented
  // with a Float32Array (V8) or an AudioFloatArray (Web Audio).

  // Returns true if the topology of two AudioPorts match. The first AudioPort
  // is given from AudioWorkletHandler (Blink) and the second AudioPort is from
  // AudioWorkletProcessor (V8).
  static bool PortTopologyMatches(
      v8::Isolate*, v8::Local<v8::Context>,
      const Vector<scoped_refptr<AudioBus>>& audio_port_1,
      const TraceWrapperV8Reference<v8::Array>& audio_port_2);

  // Freezes an AudioPort. After this operation the AudioPort will be locked and
  // cannot be altered. Returns false only if any V8 operation throws an
  // exception.
  static bool FreezeAudioPort(v8::Isolate*, v8::Local<v8::Context>,
                              v8::Local<v8::Array>& audio_port_array);

  // Clones the topology of `audio_port_1` and builds a new AudioPort to
  // `audio_port_2`. This call makes memory allocation and it should be avoided
  // in the hot audio stack as much as possible. If `array_buffers` is a valid
  // pointer, fill in `array_buffers` with new backing ArrayBuffers of
  // `audio_port_2`. Returns false only if any v8 operation throws an
  // exception.
  static bool ClonePortTopology(
      v8::Isolate*, v8::Local<v8::Context>,
      const Vector<scoped_refptr<AudioBus>>& audio_port_1,
      TraceWrapperV8Reference<v8::Array>& audio_port_2,
      BackingArrayBuffers& array_buffers);

  // Copies an AudioPort to a BackingArrayBuffers. The size of two args must
  // be identical.
  static void CopyPortToArrayBuffers(
      v8::Isolate*,
      const Vector<scoped_refptr<AudioBus>>& audio_port,
      BackingArrayBuffers& array_buffers);

  // Copies a BackingArrayBuffers to an AudioPort. The size of two args must
  // be identical.
  static void CopyArrayBuffersToPort(
      v8::Isolate*,
      const BackingArrayBuffers& array_buffers,
      Vector<scoped_refptr<AudioBus>>& audio_port);

  // Fills a given BackingArrayBuffers with zeros.
  static void ZeroArrayBuffers(v8::Isolate*,
                               const BackingArrayBuffers& array_buffers);

  // Returns true if the structure of `param_value_map` matches `params` object
  // and the underlying ArrayBuffers are not transferred.
  static bool ParamValueMapMatchesToParamsObject(
      v8::Isolate*, v8::Local<v8::Context>,
      const HashMap<String, std::unique_ptr<AudioFloatArray>>& param_value_map,
      const TraceWrapperV8Reference<v8::Object>& params);

  // Clones the structure of `param_value_map` to a given v8::Object, which
  // is an associated array of Float32Arrays.
  static bool CloneParamValueMapToObject(
      v8::Isolate*, v8::Local<v8::Context>,
      const HashMap<String, std::unique_ptr<AudioFloatArray>>& param_value_map,
      TraceWrapperV8Reference<v8::Object>& params);

  // Copies the content of float arrays from `param_value_map` to `params`
  // v8::Object.
  static bool CopyParamValueMapToObject(
      v8::Isolate*, v8::Local<v8::Context>,
      const HashMap<String, std::unique_ptr<AudioFloatArray>>& param_value_map,
      TraceWrapperV8Reference<v8::Object>& params);

  Member<AudioWorkletGlobalScope> global_scope_;
  Member<MessagePort> processor_port_;
  Member<V8BlinkAudioWorkletProcessCallback> cached_process_callback_;

  const String name_;

  TraceWrapperV8Reference<v8::Array> inputs_;
  TraceWrapperV8Reference<v8::Array> outputs_;
  TraceWrapperV8Reference<v8::Object> params_;

  BackingArrayBuffers input_array_buffers_;
  BackingArrayBuffers output_array_buffers_;

  AudioWorkletProcessorErrorState error_state_ =
      AudioWorkletProcessorErrorState::kNoError;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_WORKLET_PROCESSOR_H_