Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: third_party/WebKit/Source/platform/audio/AudioDestination.cpp

Issue 2549093009: Introduce PushPullFIFO class and remove other FIFOs (Closed)
Patch Set: Renaming and clarification (1) Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 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 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 11 matching lines...) Expand all
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "platform/audio/AudioDestination.h" 29 #include "platform/audio/AudioDestination.h"
30 30
31 #include "platform/Histogram.h" 31 #include "platform/Histogram.h"
32 #include "platform/audio/AudioPullFIFO.h"
33 #include "platform/audio/AudioUtilities.h" 32 #include "platform/audio/AudioUtilities.h"
33 #include "platform/audio/PushPullFIFO.h"
34 #include "platform/weborigin/SecurityOrigin.h" 34 #include "platform/weborigin/SecurityOrigin.h"
35 #include "public/platform/Platform.h" 35 #include "public/platform/Platform.h"
36 #include "public/platform/WebSecurityOrigin.h" 36 #include "public/platform/WebSecurityOrigin.h"
37 #include "wtf/PtrUtil.h" 37 #include "wtf/PtrUtil.h"
38 #include <memory> 38 #include <memory>
39 39
40 namespace blink { 40 namespace blink {
41 41
42 // FIFO Size. 42 // FIFO Size.
43 // 43 //
(...skipping 16 matching lines...) Expand all
60 unsigned numberOfOutputChannels, 60 unsigned numberOfOutputChannels,
61 float sampleRate, 61 float sampleRate,
62 PassRefPtr<SecurityOrigin> securityOrigin) 62 PassRefPtr<SecurityOrigin> securityOrigin)
63 : m_numberOfOutputChannels(numberOfOutputChannels), 63 : m_numberOfOutputChannels(numberOfOutputChannels),
64 m_sampleRate(sampleRate), 64 m_sampleRate(sampleRate),
65 m_isPlaying(false), 65 m_isPlaying(false),
66 m_callback(callback), 66 m_callback(callback),
67 m_outputBus(AudioBus::create(numberOfOutputChannels, 67 m_outputBus(AudioBus::create(numberOfOutputChannels,
68 AudioUtilities::kRenderQuantumFrames, 68 AudioUtilities::kRenderQuantumFrames,
69 false)), 69 false)),
70 m_renderBus(AudioBus::create(numberOfOutputChannels,
71 AudioUtilities::kRenderQuantumFrames)),
70 m_framesElapsed(0) { 72 m_framesElapsed(0) {
71 // Calculate the optimum buffer size first. 73 // Calculate the optimum buffer size first.
72 if (calculateBufferSize()) { 74 if (calculateBufferSize()) {
73 // Create WebAudioDevice. blink::WebAudioDevice is designed to support the 75 // Create WebAudioDevice. blink::WebAudioDevice is designed to support the
74 // local input (e.g. loopback from OS audio system), but Chromium's media 76 // local input (e.g. loopback from OS audio system), but Chromium's media
75 // renderer does not support it currently. Thus, we use zero for the number 77 // renderer does not support it currently. Thus, we use zero for the number
76 // of input channels. 78 // of input channels.
77 m_webAudioDevice = WTF::wrapUnique(Platform::current()->createAudioDevice( 79 m_webAudioDevice = WTF::wrapUnique(Platform::current()->createAudioDevice(
78 m_callbackBufferSize, 0, numberOfOutputChannels, sampleRate, this, 80 m_callbackBufferSize, 0, numberOfOutputChannels, sampleRate, this,
79 String(), std::move(securityOrigin))); 81 String(), std::move(securityOrigin)));
80 DCHECK(m_webAudioDevice); 82 DCHECK(m_webAudioDevice);
81 83
82 // Create a FIFO. 84 // Create a FIFO.
83 m_fifo = WTF::wrapUnique( 85 m_fifo =
84 new AudioPullFIFO(*this, numberOfOutputChannels, kFIFOSize, 86 WTF::wrapUnique(new PushPullFIFO(numberOfOutputChannels, kFIFOSize));
85 AudioUtilities::kRenderQuantumFrames));
86 } else { 87 } else {
87 NOTREACHED(); 88 NOTREACHED();
88 } 89 }
89 } 90 }
90 91
91 AudioDestination::~AudioDestination() { 92 AudioDestination::~AudioDestination() {
92 stop(); 93 stop();
93 } 94 }
94 95
95 void AudioDestination::render(const WebVector<float*>& destinationData, 96 void AudioDestination::render(const WebVector<float*>& destinationData,
96 size_t numberOfFrames, 97 size_t numberOfFrames,
97 double delay, 98 double delay,
98 double delayTimestamp, 99 double delayTimestamp,
99 size_t priorFramesSkipped) { 100 size_t priorFramesSkipped) {
100 DCHECK_EQ(destinationData.size(), m_numberOfOutputChannels); 101 DCHECK_EQ(destinationData.size(), m_numberOfOutputChannels);
101 if (destinationData.size() != m_numberOfOutputChannels)
102 return;
103
104 DCHECK_EQ(numberOfFrames, m_callbackBufferSize); 102 DCHECK_EQ(numberOfFrames, m_callbackBufferSize);
105 if (numberOfFrames != m_callbackBufferSize) 103 if (destinationData.size() != m_numberOfOutputChannels ||
104 numberOfFrames != m_callbackBufferSize)
106 return; 105 return;
107 106
108 m_framesElapsed -= std::min(m_framesElapsed, priorFramesSkipped); 107 m_framesElapsed -= std::min(m_framesElapsed, priorFramesSkipped);
109 double outputPosition = 108 double outputPosition =
110 m_framesElapsed / static_cast<double>(m_sampleRate) - delay; 109 m_framesElapsed / static_cast<double>(m_sampleRate) - delay;
111 m_outputPosition.position = outputPosition; 110 m_outputPosition.position = outputPosition;
112 m_outputPosition.timestamp = delayTimestamp; 111 m_outputPosition.timestamp = delayTimestamp;
113 m_outputPositionReceivedTimestamp = base::TimeTicks::Now(); 112 m_outputPositionReceivedTimestamp = base::TimeTicks::Now();
114 113
115 // Associate the destination data array with the output bus then fill the 114 // Associate the destination data array with the output bus then fill the
116 // FIFO. 115 // FIFO.
117 for (unsigned i = 0; i < m_numberOfOutputChannels; ++i) 116 for (unsigned i = 0; i < m_numberOfOutputChannels; ++i)
118 m_outputBus->setChannelMemory(i, destinationData[i], numberOfFrames); 117 m_outputBus->setChannelMemory(i, destinationData[i], numberOfFrames);
119 m_fifo->consume(m_outputBus.get(), numberOfFrames); 118
119 size_t framesToPull = numberOfFrames - m_fifo->framesInFIFO();
Raymond Toy 2017/01/09 22:46:02 Although this isn't caused by this CL, what happen
hongchan 2017/01/10 21:15:58 Yes, this is actually unsafe. framesToPull can be
120 while (framesToPull > 0) {
121 // If platform buffer is more than two times longer than |framesToProcess|
122 // we do not want output position to get stuck so we promote it
123 // using the elapsed time from the moment it was initially obtained.
124 if (m_callbackBufferSize > AudioUtilities::kRenderQuantumFrames * 2) {
125 double delta =
126 (base::TimeTicks::Now() - m_outputPositionReceivedTimestamp)
127 .InSecondsF();
128 m_outputPosition.position += delta;
129 m_outputPosition.timestamp += delta;
130 }
131
132 // Some implementations give only rough estimation of |delay| so
133 // we might have negative estimation |outputPosition| value.
134 if (m_outputPosition.position < 0.0)
135 m_outputPosition.position = 0.0;
136
137 m_callback.render(nullptr, m_renderBus.get(),
138 AudioUtilities::kRenderQuantumFrames, m_outputPosition);
139 m_fifo->push(m_renderBus.get());
140
141 framesToPull -= AudioUtilities::kRenderQuantumFrames;
142 }
143
144 m_fifo->pull(m_outputBus.get(), numberOfFrames);
120 145
121 m_framesElapsed += numberOfFrames; 146 m_framesElapsed += numberOfFrames;
122 } 147 }
123 148
124 void AudioDestination::provideInput(AudioBus* outputBus,
125 size_t framesToProcess) {
126 AudioIOPosition outputPosition = m_outputPosition;
127
128 // If platform buffer is more than two times longer than |framesToProcess|
129 // we do not want output position to get stuck so we promote it
130 // using the elapsed time from the moment it was initially obtained.
131 if (m_callbackBufferSize > framesToProcess * 2) {
132 double delta = (base::TimeTicks::Now() - m_outputPositionReceivedTimestamp)
133 .InSecondsF();
134 outputPosition.position += delta;
135 outputPosition.timestamp += delta;
136 }
137
138 // Some implementations give only rough estimation of |delay| so
139 // we might have negative estimation |outputPosition| value.
140 if (outputPosition.position < 0.0)
141 outputPosition.position = 0.0;
142
143 // To fill the FIFO, start the render call chain of the destination node.
144 m_callback.render(nullptr, outputBus, framesToProcess, outputPosition);
145 }
146
147 void AudioDestination::start() { 149 void AudioDestination::start() {
148 if (m_webAudioDevice && !m_isPlaying) { 150 if (m_webAudioDevice && !m_isPlaying) {
149 m_webAudioDevice->start(); 151 m_webAudioDevice->start();
150 m_isPlaying = true; 152 m_isPlaying = true;
151 } 153 }
152 } 154 }
153 155
154 void AudioDestination::stop() { 156 void AudioDestination::stop() {
155 if (m_webAudioDevice && m_isPlaying) { 157 if (m_webAudioDevice && m_isPlaying) {
156 m_webAudioDevice->stop(); 158 m_webAudioDevice->stop();
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 callbackBufferSizeHistogram.sample(m_callbackBufferSize); 213 callbackBufferSizeHistogram.sample(m_callbackBufferSize);
212 214
213 // Check if the requested buffer size is too large. 215 // Check if the requested buffer size is too large.
214 bool isBufferSizeValid = 216 bool isBufferSizeValid =
215 m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames <= kFIFOSize; 217 m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames <= kFIFOSize;
216 DCHECK(isBufferSizeValid); 218 DCHECK(isBufferSizeValid);
217 return isBufferSizeValid; 219 return isBufferSizeValid;
218 } 220 }
219 221
220 } // namespace blink 222 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698