1 /*
2  * Copyright (C) 2010, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23  * DAMAGE.
24  */
25 
26 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_DEFERRED_TASK_HANDLER_H_
27 #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_DEFERRED_TASK_HANDLER_H_
28 
29 #include <atomic>
30 #include "base/memory/scoped_refptr.h"
31 #include "base/memory/weak_ptr.h"
32 #include "third_party/blink/renderer/modules/modules_export.h"
33 #include "third_party/blink/renderer/platform/heap/handle.h"
34 #include "third_party/blink/renderer/platform/wtf/hash_set.h"
35 #include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"
36 #include "third_party/blink/renderer/platform/wtf/threading.h"
37 #include "third_party/blink/renderer/platform/wtf/threading_primitives.h"
38 #include "third_party/blink/renderer/platform/wtf/vector.h"
39 
40 namespace base {
41 class SingleThreadTaskRunner;
42 }
43 
44 namespace blink {
45 
46 class BaseAudioContext;
47 class OfflineAudioContext;
48 class AudioHandler;
49 class AudioNodeOutput;
50 class AudioSummingJunction;
51 
52 // DeferredTaskHandler manages the major part of pre- and post- rendering tasks,
53 // and provides a lock mechanism against the audio rendering graph. A
54 // DeferredTaskHandler object is created when an BaseAudioContext object is
55 // created.
56 //
57 // DeferredTaskHandler outlives the BaseAudioContext only if all of the
58 // following conditions match:
59 // - An audio rendering thread is running,
60 // - It is requested to stop,
61 // - The audio rendering thread calls requestToDeleteHandlersOnMainThread(),
62 // - It posts a task of deleteHandlersOnMainThread(), and
63 // - GC happens and it collects the BaseAudioContext before the task execution.
64 //
65 class MODULES_EXPORT DeferredTaskHandler final
66     : public ThreadSafeRefCounted<DeferredTaskHandler>,
67       public base::SupportsWeakPtr<DeferredTaskHandler> {
68  public:
69   static scoped_refptr<DeferredTaskHandler> Create(
70       scoped_refptr<base::SingleThreadTaskRunner> task_runner);
71   ~DeferredTaskHandler();
72 
73   void HandleDeferredTasks();
74   void ContextWillBeDestroyed();
75 
76   // BaseAudioContext can pull node(s) at the end of each render quantum even
77   // when they are not connected to any downstream nodes.  These two methods are
78   // called by the nodes who want to add/remove themselves into/from the
79   // automatic pull lists.
80   void AddAutomaticPullNode(scoped_refptr<AudioHandler>);
81   void RemoveAutomaticPullNode(AudioHandler*);
82 
83   // Called right before handlePostRenderTasks() to handle nodes which need to
84   // be pulled even when they are not connected to anything.
85   void ProcessAutomaticPullNodes(uint32_t frames_to_process);
86 
87   // Keep track of AudioNode's that have their channel count mode changed. We
88   // process the changes in the post rendering phase.
89   void AddChangedChannelCountMode(AudioHandler*);
90   void RemoveChangedChannelCountMode(AudioHandler*);
91 
92   // Keep track of AudioNode's that have their channel interpretation
93   // changed. We process the changes in the post rendering phase.
94   void AddChangedChannelInterpretation(AudioHandler*);
95   void RemoveChangedChannelInterpretation(AudioHandler*);
96 
97   // Only accessed when the graph lock is held.
98   void MarkSummingJunctionDirty(AudioSummingJunction*);
99   // Only accessed when the graph lock is held. Must be called on the main
100   // thread.
101   void RemoveMarkedSummingJunction(AudioSummingJunction*);
102 
103   void MarkAudioNodeOutputDirty(AudioNodeOutput*);
104   void RemoveMarkedAudioNodeOutput(AudioNodeOutput*);
105 
106   // Break connections between nodes.  This is done on the audio thread with the
107   // graph lock.
108   void BreakConnections();
109 
110   void AddRenderingOrphanHandler(scoped_refptr<AudioHandler>);
111   void RequestToDeleteHandlersOnMainThread();
112   void ClearHandlersToBeDeleted();
113 
114   // Clear the context from the rendering and deletable orphan handlers.
115   void ClearContextFromOrphanHandlers();
116 
AcceptsTailProcessing()117   bool AcceptsTailProcessing() const { return accepts_tail_processing_; }
StopAcceptingTailProcessing()118   void StopAcceptingTailProcessing() { accepts_tail_processing_ = false; }
119 
120   // If |node| requires tail processing, add it to the list of tail
121   // nodes so the tail is processed.
122   void AddTailProcessingHandler(scoped_refptr<AudioHandler>);
123 
124   // Remove |node| from the list of tail nodes (because the tail processing is
125   // complete).  Set |disable_outputs| to true if the outputs of the handler
126   // should also be disabled.  This should be true if the tail is done.  But if
127   // we're reconnected or re-enabled, then |disable_outputs| should be false.
128   void RemoveTailProcessingHandler(AudioHandler*, bool disable_outputs);
129 
130   // Remove all tail processing nodes.  Should be called only when the
131   // context is done.
132   void FinishTailProcessing();
133 
134   // For handlers that have finished processing their tail and require disabling
135   // the ouputs of the handler, we do that here.
136   void DisableOutputsForTailProcessing();
137 
138   //
139   // Thread Safety and Graph Locking:
140   //
141   void SetAudioThreadToCurrentThread();
142 
143   // It is okay to use a relaxed (no-barrier) load here. Because the data
144   // referenced by m_audioThread is not actually being used, thus we do not
145   // need a barrier between the load of m_audioThread and of that data.
IsAudioThread()146   bool IsAudioThread() const {
147     return CurrentThread() == audio_thread_.load(std::memory_order_relaxed);
148   }
149 
150   void lock();
151   bool TryLock();
152   void unlock();
153 
154   // This locks the audio render thread for OfflineAudioContext rendering.
155   // MUST NOT be used in the real-time audio context.
156   void OfflineLock();
157 
158   // In DCHECK builds, fails if this thread does not own the context's lock.
AssertGraphOwner()159   void AssertGraphOwner() const { context_graph_mutex_.AssertAcquired(); }
160 
161   class MODULES_EXPORT GraphAutoLocker {
162     STACK_ALLOCATED();
163 
164    public:
GraphAutoLocker(DeferredTaskHandler & handler)165     explicit GraphAutoLocker(DeferredTaskHandler& handler) : handler_(handler) {
166       handler_.lock();
167     }
168     explicit GraphAutoLocker(const BaseAudioContext*);
169 
~GraphAutoLocker()170     ~GraphAutoLocker() { handler_.unlock(); }
171 
172    private:
173     DeferredTaskHandler& handler_;
174   };
175 
176   // This is for locking offline render thread (which is considered as the
177   // audio thread) with unlocking on self-destruction at the end of the scope.
178   // Also note that it uses lock() rather than tryLock() because the timing
179   // MUST be accurate on offline rendering.
180   class MODULES_EXPORT OfflineGraphAutoLocker {
181     STACK_ALLOCATED();
182 
183    public:
184     explicit OfflineGraphAutoLocker(OfflineAudioContext*);
185 
~OfflineGraphAutoLocker()186     ~OfflineGraphAutoLocker() { handler_.unlock(); }
187 
188    private:
189     DeferredTaskHandler& handler_;
190   };
191 
GetActiveSourceHandlers()192   HashSet<scoped_refptr<AudioHandler>>* GetActiveSourceHandlers() {
193     return &active_source_handlers_;
194   }
195 
GetFinishedSourceHandlers()196   Vector<scoped_refptr<AudioHandler>>* GetFinishedSourceHandlers() {
197     return &finished_source_handlers_;
198   }
199 
200  private:
201   explicit DeferredTaskHandler(scoped_refptr<base::SingleThreadTaskRunner>);
202   void UpdateAutomaticPullNodes();
203   void UpdateChangedChannelCountMode();
204   void UpdateChangedChannelInterpretation();
205   void HandleDirtyAudioSummingJunctions();
206   void HandleDirtyAudioNodeOutputs();
207   void DeleteHandlersOnMainThread();
208 
209   // Check tail processing handlers and remove any handler if the tail
210   // has been processed.
211   void UpdateTailProcessingHandlers();
212 
213   // For the sake of thread safety, we maintain a seperate Vector of
214   // AudioHandlers for "automatic-pull nodes":
215   // |rendering_automatic_pull_handlers|. This storage will be copied from
216   // |automatic_pull_handlers| by |UpdateAutomaticPullNodes()| at the beginning
217   // or end of the render quantum.
218   HashSet<scoped_refptr<AudioHandler>> automatic_pull_handlers_;
219   Vector<scoped_refptr<AudioHandler>> rendering_automatic_pull_handlers_;
220 
221   // Keeps track if the |automatic_pull_handlers| storage is touched.
222   bool automatic_pull_handlers_need_updating_;
223 
224   // Collection of nodes where the channel count mode has changed. We want the
225   // channel count mode to change in the pre- or post-rendering phase so as
226   // not to disturb the running audio thread.
227   HashSet<AudioHandler*> deferred_count_mode_change_;
228 
229   HashSet<AudioHandler*> deferred_channel_interpretation_change_;
230 
231   // These two HashSet must be accessed only when the graph lock is held.
232   // These raw pointers are safe because their destructors unregister them.
233   HashSet<AudioSummingJunction*> dirty_summing_junctions_;
234   HashSet<AudioNodeOutput*> dirty_audio_node_outputs_;
235 
236   Vector<scoped_refptr<AudioHandler>> rendering_orphan_handlers_;
237   Vector<scoped_refptr<AudioHandler>> deletable_orphan_handlers_;
238 
239   // Nodes that are processing its tail.
240   Vector<scoped_refptr<AudioHandler>> tail_processing_handlers_;
241 
242   // Tail processing nodes that are now finished and want the output to be
243   // disabled.  This is updated in the audio thread (with the graph lock).  The
244   // main thread will disable the outputs.
245   Vector<scoped_refptr<AudioHandler>> finished_tail_processing_handlers_;
246 
247   // Once the associated context closes, new tail processing handlers are not
248   // accepted.
249   bool accepts_tail_processing_ = true;
250 
251   // When source nodes are started, we place the handlers here to keep track of
252   // these active sources.  We must call AudioHandler::makeConnection() when we
253   // add an AudioNode to this, and must call AudioHandler::breakConnection()
254   // when we remove an AudioNode from this.
255   //
256   // This can be accessed from either the main thread or the audio thread, so it
257   // must be protected by the graph lock.
258   HashSet<scoped_refptr<AudioHandler>> active_source_handlers_;
259 
260   // When source nodes are finished, the handler is placed here to make a note
261   // of it.  At a render quantum boundary, these are used to break the
262   // connection and elements here are removed from |active_source_handlers_|.
263   //
264   // This must be accessed only from the audio thread.
265   Vector<scoped_refptr<AudioHandler>> finished_source_handlers_;
266 
267   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
268 
269   // Graph locking.
270   RecursiveMutex context_graph_mutex_;
271 
272   // Protects |rendering_automatic_pull_handlers| when updating, processing, and
273   // clearing. (See crbug.com/1061018)
274   mutable Mutex automatic_pull_handlers_lock_;
275 
276   std::atomic<base::PlatformThreadId> audio_thread_;
277 };
278 
279 }  // namespace blink
280 
281 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_DEFERRED_TASK_HANDLER_H_
282