OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2011, Google Inc. All rights reserved. | 2 * Copyright (C) 2011, Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 10 matching lines...) Expand all Loading... | |
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
23 */ | 23 */ |
24 | 24 |
25 #include "config.h" | 25 #include "config.h" |
26 #if ENABLE(WEB_AUDIO) | 26 #if ENABLE(WEB_AUDIO) |
27 #include "modules/webaudio/OfflineAudioDestinationNode.h" | 27 #include "modules/webaudio/OfflineAudioDestinationNode.h" |
28 | 28 |
29 #include "core/dom/CrossThreadTask.h" | 29 #include "core/dom/CrossThreadTask.h" |
30 #include "modules/webaudio/AbstractAudioContext.h" | 30 #include "modules/webaudio/AbstractAudioContext.h" |
31 #include "modules/webaudio/AudioNodeInput.h" | |
32 #include "modules/webaudio/AudioNodeOutput.h" | |
33 #include "modules/webaudio/OfflineAudioContext.h" | |
31 #include "platform/Task.h" | 34 #include "platform/Task.h" |
32 #include "platform/audio/AudioBus.h" | 35 #include "platform/audio/AudioBus.h" |
36 #include "platform/audio/DenormalDisabler.h" | |
33 #include "platform/audio/HRTFDatabaseLoader.h" | 37 #include "platform/audio/HRTFDatabaseLoader.h" |
34 #include "public/platform/Platform.h" | 38 #include "public/platform/Platform.h" |
35 #include <algorithm> | 39 #include <algorithm> |
36 | 40 |
37 namespace blink { | 41 namespace blink { |
38 | 42 |
39 const size_t renderQuantumSize = 128; | 43 const size_t OfflineAudioDestinationHandler::renderQuantumSize = 128; |
40 | 44 |
41 OfflineAudioDestinationHandler::OfflineAudioDestinationHandler(AudioNode& node, AudioBuffer* renderTarget) | 45 OfflineAudioDestinationHandler::OfflineAudioDestinationHandler(AudioNode& node, AudioBuffer* renderTarget) |
42 : AudioDestinationHandler(node, renderTarget->sampleRate()) | 46 : AudioDestinationHandler(node, renderTarget->sampleRate()) |
43 , m_renderTarget(renderTarget) | 47 , m_renderTarget(renderTarget) |
44 , m_startedRendering(false) | 48 , m_renderThread(adoptPtr(Platform::current()->createThread("offline audio r enderer"))) |
49 , m_framesProcessed(0) | |
50 , m_framesToProcess(0) | |
51 , m_isRenderingStarted(false) | |
52 , m_shouldSuspend(false) | |
45 { | 53 { |
46 m_renderBus = AudioBus::create(renderTarget->numberOfChannels(), renderQuant umSize); | 54 m_renderBus = AudioBus::create(renderTarget->numberOfChannels(), renderQuant umSize); |
55 m_framesToProcess = m_renderTarget->length(); | |
47 } | 56 } |
48 | 57 |
49 PassRefPtr<OfflineAudioDestinationHandler> OfflineAudioDestinationHandler::creat e(AudioNode& node, AudioBuffer* renderTarget) | 58 PassRefPtr<OfflineAudioDestinationHandler> OfflineAudioDestinationHandler::creat e(AudioNode& node, AudioBuffer* renderTarget) |
50 { | 59 { |
51 return adoptRef(new OfflineAudioDestinationHandler(node, renderTarget)); | 60 return adoptRef(new OfflineAudioDestinationHandler(node, renderTarget)); |
52 } | 61 } |
53 | 62 |
54 OfflineAudioDestinationHandler::~OfflineAudioDestinationHandler() | 63 OfflineAudioDestinationHandler::~OfflineAudioDestinationHandler() |
55 { | 64 { |
56 ASSERT(!isInitialized()); | 65 ASSERT(!isInitialized()); |
(...skipping 17 matching lines...) Expand all Loading... | |
74 { | 83 { |
75 if (!isInitialized()) | 84 if (!isInitialized()) |
76 return; | 85 return; |
77 | 86 |
78 if (m_renderThread) | 87 if (m_renderThread) |
79 m_renderThread.clear(); | 88 m_renderThread.clear(); |
80 | 89 |
81 AudioHandler::uninitialize(); | 90 AudioHandler::uninitialize(); |
82 } | 91 } |
83 | 92 |
93 OfflineAudioContext* OfflineAudioDestinationHandler::context() const | |
94 { | |
95 return static_cast<OfflineAudioContext*>(m_context); | |
96 } | |
97 | |
84 void OfflineAudioDestinationHandler::startRendering() | 98 void OfflineAudioDestinationHandler::startRendering() |
85 { | 99 { |
86 ASSERT(isMainThread()); | 100 ASSERT(isMainThread()); |
101 ASSERT(m_renderThread); | |
87 ASSERT(m_renderTarget); | 102 ASSERT(m_renderTarget); |
103 | |
88 if (!m_renderTarget) | 104 if (!m_renderTarget) |
89 return; | 105 return; |
90 | 106 |
91 if (!m_startedRendering) { | 107 // Rendering was not started. Starting now. |
92 m_startedRendering = true; | 108 if (!m_isRenderingStarted) { |
93 m_renderThread = adoptPtr(Platform::current()->createThread("Offline Aud io Renderer")); | 109 m_isRenderingStarted = true; |
94 m_renderThread->taskRunner()->postTask(BLINK_FROM_HERE, new Task(threadS afeBind(&OfflineAudioDestinationHandler::offlineRender, PassRefPtr<OfflineAudioD estinationHandler>(this)))); | 110 m_renderThread->taskRunner()->postTask(BLINK_FROM_HERE, |
111 new Task(threadSafeBind(&OfflineAudioDestinationHandler::startOfflin eRendering, this))); | |
112 return; | |
95 } | 113 } |
114 | |
115 // Rendering is already started, which implicitly means we resume the | |
116 // rendering by calling |doOfflineRendering| on the render thread. | |
117 m_renderThread->taskRunner()->postTask(BLINK_FROM_HERE, | |
118 threadSafeBind(&OfflineAudioDestinationHandler::doOfflineRendering, this )); | |
96 } | 119 } |
97 | 120 |
98 void OfflineAudioDestinationHandler::stopRendering() | 121 void OfflineAudioDestinationHandler::stopRendering() |
99 { | 122 { |
123 // offline audio rendering CANNOT BE stopped by JavaScript. | |
100 ASSERT_NOT_REACHED(); | 124 ASSERT_NOT_REACHED(); |
101 } | 125 } |
102 | 126 |
103 void OfflineAudioDestinationHandler::offlineRender() | 127 WebThread* OfflineAudioDestinationHandler::offlineRenderThread() |
104 { | 128 { |
105 offlineRenderInternal(); | 129 ASSERT(m_renderThread); |
106 context()->handlePostRenderTasks(); | 130 |
131 return m_renderThread.get(); | |
107 } | 132 } |
108 | 133 |
109 void OfflineAudioDestinationHandler::offlineRenderInternal() | 134 // size_t OfflineAudioDestinationHandler::renderQuantumFrames() const |
135 // { | |
136 // return renderQuantumSize; | |
137 // } | |
Raymond Toy
2015/10/21 23:14:18
You forgot to remove this. :-)
hongchan
2015/10/22 18:23:49
Oops. Sorry.
| |
138 | |
139 void OfflineAudioDestinationHandler::startOfflineRendering() | |
110 { | 140 { |
111 ASSERT(!isMainThread()); | 141 ASSERT(!isMainThread()); |
142 | |
112 ASSERT(m_renderBus); | 143 ASSERT(m_renderBus); |
113 if (!m_renderBus) | 144 if (!m_renderBus) |
114 return; | 145 return; |
115 | 146 |
116 bool isAudioContextInitialized = context()->isDestinationInitialized(); | 147 bool isAudioContextInitialized = context()->isDestinationInitialized(); |
117 ASSERT(isAudioContextInitialized); | 148 ASSERT(isAudioContextInitialized); |
118 if (!isAudioContextInitialized) | 149 if (!isAudioContextInitialized) |
119 return; | 150 return; |
120 | 151 |
121 bool channelsMatch = m_renderBus->numberOfChannels() == m_renderTarget->numb erOfChannels(); | 152 bool channelsMatch = m_renderBus->numberOfChannels() == m_renderTarget->numb erOfChannels(); |
122 ASSERT(channelsMatch); | 153 ASSERT(channelsMatch); |
123 if (!channelsMatch) | 154 if (!channelsMatch) |
124 return; | 155 return; |
125 | 156 |
126 bool isRenderBusAllocated = m_renderBus->length() >= renderQuantumSize; | 157 bool isRenderBusAllocated = m_renderBus->length() >= renderQuantumSize; |
127 ASSERT(isRenderBusAllocated); | 158 ASSERT(isRenderBusAllocated); |
128 if (!isRenderBusAllocated) | 159 if (!isRenderBusAllocated) |
129 return; | 160 return; |
130 | 161 |
131 // Break up the render target into smaller "render quantize" sized pieces. | 162 // Start rendering. |
132 // Render until we're finished. | 163 doOfflineRendering(); |
133 size_t framesToProcess = m_renderTarget->length(); | 164 } |
165 | |
166 void OfflineAudioDestinationHandler::doOfflineRendering() | |
167 { | |
168 ASSERT(!isMainThread()); | |
169 | |
134 unsigned numberOfChannels = m_renderTarget->numberOfChannels(); | 170 unsigned numberOfChannels = m_renderTarget->numberOfChannels(); |
135 | 171 |
136 unsigned n = 0; | 172 // Reset the suspend flag. |
137 while (framesToProcess > 0) { | 173 m_shouldSuspend = false; |
138 // Render one render quantum. | |
139 render(0, m_renderBus.get(), renderQuantumSize); | |
140 | 174 |
141 size_t framesAvailableToCopy = std::min(framesToProcess, renderQuantumSi ze); | 175 // If there is more to process and there is no suspension at the moment, |
176 // do continue to render quanta. Then calling OfflineAudioContext.resume() w ill pick up | |
177 // the render loop again from where it was suspended. | |
178 while (m_framesToProcess > 0 && !m_shouldSuspend) { | |
179 | |
180 // Suspend the rendering and update m_shouldSuspend if a scheduled | |
181 // suspend found at the current sample frame. Otherwise render one | |
182 // quantum and return false. | |
183 m_shouldSuspend = checkSuspendsAndRender(0, m_renderBus.get(), renderQua ntumSize); | |
184 | |
185 if (m_shouldSuspend) { | |
186 suspendOfflineRendering(); | |
187 return; | |
Raymond Toy
2015/10/21 23:14:18
Would it make more sense if checkSuspendsAndRender
hongchan
2015/10/22 18:23:49
Works fine with me. It makes more sense if we chan
| |
188 } | |
189 | |
190 size_t framesAvailableToCopy = std::min(m_framesToProcess, renderQuantum Size); | |
142 | 191 |
143 for (unsigned channelIndex = 0; channelIndex < numberOfChannels; ++chann elIndex) { | 192 for (unsigned channelIndex = 0; channelIndex < numberOfChannels; ++chann elIndex) { |
144 const float* source = m_renderBus->channel(channelIndex)->data(); | 193 const float* source = m_renderBus->channel(channelIndex)->data(); |
145 float* destination = m_renderTarget->getChannelData(channelIndex)->d ata(); | 194 float* destination = m_renderTarget->getChannelData(channelIndex)->d ata(); |
146 memcpy(destination + n, source, sizeof(float) * framesAvailableToCop y); | 195 memcpy(destination + m_framesProcessed, source, sizeof(float) * fram esAvailableToCopy); |
147 } | 196 } |
148 | 197 |
149 n += framesAvailableToCopy; | 198 m_framesProcessed += framesAvailableToCopy; |
150 framesToProcess -= framesAvailableToCopy; | 199 |
200 ASSERT(m_framesToProcess >= framesAvailableToCopy); | |
201 m_framesToProcess -= framesAvailableToCopy; | |
151 } | 202 } |
152 | 203 |
153 // Our work is done. Let the AbstractAudioContext know. | 204 // Finish up the rendering loop if there is no more to process. |
154 if (context()->executionContext()) | 205 if (!m_framesToProcess) |
155 context()->executionContext()->postTask(BLINK_FROM_HERE, createCrossThre adTask(&OfflineAudioDestinationHandler::notifyComplete, PassRefPtr<OfflineAudioD estinationHandler>(this))); | 206 finishOfflineRendering(); |
207 } | |
208 | |
209 void OfflineAudioDestinationHandler::suspendOfflineRendering() | |
210 { | |
211 ASSERT(!isMainThread()); | |
212 | |
213 // The actual rendering has been suspended. Notify the context. | |
214 if (context()->executionContext()) { | |
215 context()->executionContext()->postTask(BLINK_FROM_HERE, | |
216 createCrossThreadTask(&OfflineAudioDestinationHandler::notifySuspend , this)); | |
217 } | |
218 } | |
219 | |
220 void OfflineAudioDestinationHandler::finishOfflineRendering() | |
221 { | |
222 ASSERT(!isMainThread()); | |
223 | |
224 // The actual rendering has been completed. Notify the context. | |
225 if (context()->executionContext()) { | |
226 context()->executionContext()->postTask(BLINK_FROM_HERE, | |
227 createCrossThreadTask(&OfflineAudioDestinationHandler::notifyComplet e, this)); | |
228 } | |
229 } | |
230 | |
231 void OfflineAudioDestinationHandler::notifySuspend() | |
232 { | |
233 if (context()) | |
234 context()->resolveSuspendOnMainThread(context()->currentSampleFrame()); | |
156 } | 235 } |
157 | 236 |
158 void OfflineAudioDestinationHandler::notifyComplete() | 237 void OfflineAudioDestinationHandler::notifyComplete() |
159 { | 238 { |
160 // The AbstractAudioContext might be gone. | 239 // The OfflineAudioContext might be gone. |
161 if (context()) | 240 if (context()) |
162 context()->fireCompletionEvent(); | 241 context()->fireCompletionEvent(); |
163 } | 242 } |
164 | 243 |
244 bool OfflineAudioDestinationHandler::checkSuspendsAndRender(AudioBus* sourceBus, AudioBus* destinationBus, size_t numberOfFrames) | |
245 { | |
246 // We don't want denormals slowing down any of the audio processing | |
247 // since they can very seriously hurt performance. | |
248 // This will take care of all AudioNodes because they all process within thi s scope. | |
249 DenormalDisabler denormalDisabler; | |
250 | |
251 context()->deferredTaskHandler().setAudioThread(currentThread()); | |
252 | |
253 if (!context()->isDestinationInitialized()) { | |
254 destinationBus->zero(); | |
255 return false; | |
256 } | |
257 | |
258 // Take care pre-render tasks at the beginning of each render quantum. Then | |
259 // it will stop the rendering loop if the context needs to be suspended | |
260 // at the beginning of the next render quantum. | |
261 if (context()->handlePreOfflineRenderTasks()) | |
262 return true; | |
263 | |
264 // Prepare the local audio input provider for this render quantum. | |
265 if (sourceBus) | |
266 m_localAudioInputProvider.set(sourceBus); | |
267 | |
268 ASSERT(numberOfInputs() >= 1); | |
269 if (numberOfInputs() < 1) { | |
270 destinationBus->zero(); | |
271 return false; | |
272 } | |
273 // This will cause the node(s) connected to us to process, which in turn wil l pull on their input(s), | |
274 // all the way backwards through the rendering graph. | |
275 AudioBus* renderedBus = input(0).pull(destinationBus, numberOfFrames); | |
276 | |
277 if (!renderedBus) { | |
278 destinationBus->zero(); | |
279 } else if (renderedBus != destinationBus) { | |
280 // in-place processing was not possible - so copy | |
281 destinationBus->copyFrom(*renderedBus); | |
282 } | |
283 | |
284 // Process nodes which need a little extra help because they are not connect ed to anything, but still need to process. | |
285 context()->deferredTaskHandler().processAutomaticPullNodes(numberOfFrames); | |
286 | |
287 // Let the context take care of any business at the end of each render quant um. | |
288 context()->handlePostOfflineRenderTasks(); | |
289 | |
290 // Advance current sample-frame. | |
291 size_t newSampleFrame = m_currentSampleFrame + numberOfFrames; | |
292 releaseStore(&m_currentSampleFrame, newSampleFrame); | |
293 | |
294 return false; | |
295 } | |
296 | |
165 // ---------------------------------------------------------------- | 297 // ---------------------------------------------------------------- |
166 | 298 |
167 OfflineAudioDestinationNode::OfflineAudioDestinationNode(AbstractAudioContext& c ontext, AudioBuffer* renderTarget) | 299 OfflineAudioDestinationNode::OfflineAudioDestinationNode(AbstractAudioContext& c ontext, AudioBuffer* renderTarget) |
168 : AudioDestinationNode(context) | 300 : AudioDestinationNode(context) |
169 { | 301 { |
170 setHandler(OfflineAudioDestinationHandler::create(*this, renderTarget)); | 302 setHandler(OfflineAudioDestinationHandler::create(*this, renderTarget)); |
171 } | 303 } |
172 | 304 |
173 OfflineAudioDestinationNode* OfflineAudioDestinationNode::create(AbstractAudioCo ntext* context, AudioBuffer* renderTarget) | 305 OfflineAudioDestinationNode* OfflineAudioDestinationNode::create(AbstractAudioCo ntext* context, AudioBuffer* renderTarget) |
174 { | 306 { |
175 return new OfflineAudioDestinationNode(*context, renderTarget); | 307 return new OfflineAudioDestinationNode(*context, renderTarget); |
176 } | 308 } |
177 | 309 |
178 } // namespace blink | 310 } // namespace blink |
179 | 311 |
180 #endif // ENABLE(WEB_AUDIO) | 312 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |