summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/modules/webaudio/audio_worklet_global_scope.h
blob: f6dd38283d7a12203834fb73b96e476924925e7f (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
// 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_WORKLET_GLOBAL_SCOPE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_WORKLET_GLOBAL_SCOPE_H_

#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_audio_param_descriptor.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/workers/worklet_global_scope.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/audio/audio_array.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/casting.h"

namespace blink {

class AudioWorkletProcessor;
class AudioWorkletProcessorDefinition;
class CrossThreadAudioWorkletProcessorInfo;
class ExceptionState;
class MessagePortChannel;
class SerializedScriptValue;
class V8BlinkAudioWorkletProcessorConstructor;
struct GlobalScopeCreationParams;


// The storage for the construction of AudioWorkletProcessor, contains the
// processor name and MessageChannelPort object.
class MODULES_EXPORT ProcessorCreationParams final {
  USING_FAST_MALLOC(ProcessorCreationParams);

 public:
  ProcessorCreationParams(const String& name,
                          MessagePortChannel message_port_channel)
      : name_(name), message_port_channel_(message_port_channel) {}

  ~ProcessorCreationParams() = default;

  const String& Name() const { return name_; }
  MessagePortChannel PortChannel() {  return message_port_channel_; }

 private:
  const String name_;
  MessagePortChannel message_port_channel_;
};


// This is constructed and destroyed on a worker thread, and all methods also
// must be called on the worker thread.
class MODULES_EXPORT AudioWorkletGlobalScope final : public WorkletGlobalScope {
  DEFINE_WRAPPERTYPEINFO();

 public:
  AudioWorkletGlobalScope(std::unique_ptr<GlobalScopeCreationParams>,
                          WorkerThread*);
  ~AudioWorkletGlobalScope() override;

  bool IsAudioWorkletGlobalScope() const final { return true; }
  void Dispose() final;
  bool IsClosing() const final { return is_closing_; }

  void registerProcessor(
      const String& name,
      V8BlinkAudioWorkletProcessorConstructor* processor_ctor,
      ExceptionState&);

  // Creates an instance of AudioWorkletProcessor from a registered name.
  // This is invoked by AudioWorkletMessagingProxy upon the construction of
  // AudioWorkletNode.
  //
  // This function may return nullptr when a new V8 object cannot be constructed
  // for some reason.
  AudioWorkletProcessor* CreateProcessor(
      const String& name,
      MessagePortChannel,
      scoped_refptr<SerializedScriptValue> node_options);

  AudioWorkletProcessorDefinition* FindDefinition(const String& name);

  unsigned NumberOfRegisteredDefinitions();

  std::unique_ptr<Vector<CrossThreadAudioWorkletProcessorInfo>>
      WorkletProcessorInfoListForSynchronization();

  // Gets |processor_creation_params_| for the processor construction. If there
  // is no on-going processor construction, this MUST return nullptr.
  ProcessorCreationParams* GetProcessorCreationParams();

  void SetCurrentFrame(size_t current_frame);
  void SetSampleRate(float sample_rate);

  // IDL
  uint64_t currentFrame() const { return current_frame_; }
  double currentTime() const;
  float sampleRate() const { return sample_rate_; }

  void Trace(Visitor*) const override;

  // Returns the token that uniquely identifies this worklet.
  const AudioWorkletToken& GetAudioWorkletToken() const { return token_; }
  WorkletToken GetWorkletToken() const final { return token_; }
  ExecutionContextToken GetExecutionContextToken() const final {
    return token_;
  }

 private:
  bool is_closing_ = false;

  typedef HeapHashMap<String, Member<AudioWorkletProcessorDefinition>>
      ProcessorDefinitionMap;
  typedef HeapVector<Member<AudioWorkletProcessor>> ProcessorInstances;

  ProcessorDefinitionMap processor_definition_map_;
  ProcessorInstances processor_instances_;

  // Gets set when the processor construction is invoked, and cleared out after
  // the construction. See the comment in |CreateProcessor()| method for the
  // detail.
  std::unique_ptr<ProcessorCreationParams> processor_creation_params_;

  size_t current_frame_ = 0;
  float sample_rate_ = 0.0;

  // Default initialized to generate a distinct token for this worklet.
  const AudioWorkletToken token_;
};

template <>
struct DowncastTraits<AudioWorkletGlobalScope> {
  static bool AllowFrom(const ExecutionContext& context) {
    return context.IsAudioWorkletGlobalScope();
  }
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_WORKLET_GLOBAL_SCOPE_H_