OLD | NEW |
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 Loading... |
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/AudioFIFO.h" | |
33 #include "platform/audio/AudioPullFIFO.h" | 32 #include "platform/audio/AudioPullFIFO.h" |
34 #include "platform/audio/AudioUtilities.h" | 33 #include "platform/audio/AudioUtilities.h" |
35 #include "platform/weborigin/SecurityOrigin.h" | 34 #include "platform/weborigin/SecurityOrigin.h" |
36 #include "public/platform/Platform.h" | 35 #include "public/platform/Platform.h" |
37 #include "public/platform/WebSecurityOrigin.h" | 36 #include "public/platform/WebSecurityOrigin.h" |
38 #include "wtf/PtrUtil.h" | 37 #include "wtf/PtrUtil.h" |
39 #include <memory> | 38 #include <memory> |
40 | 39 |
41 namespace blink { | 40 namespace blink { |
42 | 41 |
43 // Size of the FIFO | 42 // FIFO Size. |
44 const size_t fifoSize = 8192; | 43 // |
| 44 // TODO(hongchan): This was estimated based on the largest callback buffer size |
| 45 // that we would ever need. The current UMA stats indicates that this is, in |
| 46 // fact, probably too small. There are Android devices out there with a size of |
| 47 // 8000 or so. We might need to make this larger. See: crbug.com/670747 |
| 48 const size_t kFIFOSize = 8192; |
45 | 49 |
46 // Factory method: Chromium-implementation | |
47 std::unique_ptr<AudioDestination> AudioDestination::create( | 50 std::unique_ptr<AudioDestination> AudioDestination::create( |
48 AudioIOCallback& callback, | 51 AudioIOCallback& callback, |
49 const String& inputDeviceId, | |
50 unsigned numberOfInputChannels, | |
51 unsigned numberOfOutputChannels, | 52 unsigned numberOfOutputChannels, |
52 float sampleRate, | 53 float sampleRate, |
53 PassRefPtr<SecurityOrigin> securityOrigin) { | 54 PassRefPtr<SecurityOrigin> securityOrigin) { |
54 return WTF::wrapUnique(new AudioDestination( | 55 return WTF::wrapUnique(new AudioDestination( |
55 callback, inputDeviceId, numberOfInputChannels, numberOfOutputChannels, | 56 callback, numberOfOutputChannels, sampleRate, std::move(securityOrigin))); |
56 sampleRate, std::move(securityOrigin))); | |
57 } | 57 } |
58 | 58 |
59 AudioDestination::AudioDestination(AudioIOCallback& callback, | 59 AudioDestination::AudioDestination(AudioIOCallback& callback, |
60 const String& inputDeviceId, | |
61 unsigned numberOfInputChannels, | |
62 unsigned numberOfOutputChannels, | 60 unsigned numberOfOutputChannels, |
63 float sampleRate, | 61 float sampleRate, |
64 PassRefPtr<SecurityOrigin> securityOrigin) | 62 PassRefPtr<SecurityOrigin> securityOrigin) |
65 : m_callback(callback), | 63 : m_numberOfOutputChannels(numberOfOutputChannels), |
66 m_numberOfOutputChannels(numberOfOutputChannels), | 64 m_sampleRate(sampleRate), |
67 m_renderBus(AudioBus::create(numberOfOutputChannels, | 65 m_isPlaying(false), |
| 66 m_callback(callback), |
| 67 m_outputBus(AudioBus::create(numberOfOutputChannels, |
68 AudioUtilities::kRenderQuantumFrames, | 68 AudioUtilities::kRenderQuantumFrames, |
69 false)), | 69 false)), |
70 m_sampleRate(sampleRate), | 70 m_framesElapsed(0) { |
71 m_isPlaying(false), | 71 // Calculate the optimum buffer size first. |
72 m_framesElapsed(0), | 72 if (calculateBufferSize()) { |
73 m_outputPosition() { | 73 // Create WebAudioDevice. blink::WebAudioDevice is designed to support the |
74 // Histogram for audioHardwareBufferSize | 74 // local input (e.g. loopback from OS audio system), but Chromium's media |
75 DEFINE_STATIC_LOCAL(SparseHistogram, hardwareBufferSizeHistogram, | 75 // renderer does not support it currently. Thus, we use zero for the number |
76 ("WebAudio.AudioDestination.HardwareBufferSize")); | 76 // of input channels. |
77 // Histogram for the actual callback size used. Typically, this is the same | 77 m_webAudioDevice = WTF::wrapUnique(Platform::current()->createAudioDevice( |
78 // as audioHardwareBufferSize, but can be adjusted depending on some | 78 m_callbackBufferSize, 0, numberOfOutputChannels, sampleRate, this, |
79 // heuristics below. | 79 String(), std::move(securityOrigin))); |
80 DEFINE_STATIC_LOCAL(SparseHistogram, callbackBufferSizeHistogram, | 80 DCHECK(m_webAudioDevice); |
81 ("WebAudio.AudioDestination.CallbackBufferSize")); | |
82 | 81 |
83 // Use the optimal buffer size recommended by the audio backend. | 82 // Create a FIFO. |
84 size_t recommendedHardwareBufferSize = | 83 m_fifo = WTF::wrapUnique( |
85 Platform::current()->audioHardwareBufferSize(); | 84 new AudioPullFIFO(*this, numberOfOutputChannels, kFIFOSize, |
86 m_callbackBufferSize = recommendedHardwareBufferSize; | 85 AudioUtilities::kRenderQuantumFrames)); |
87 | 86 } else { |
88 #if OS(ANDROID) | 87 NOTREACHED(); |
89 // The optimum low-latency hardware buffer size is usually too small on | 88 } |
90 // Android for WebAudio to render without glitching. So, if it is small, use | |
91 // a larger size. If it was already large, use the requested size. | |
92 // | |
93 // Since WebAudio renders in 128-frame blocks, the small buffer sizes (144 | |
94 // for a Galaxy Nexus), cause significant processing jitter. Sometimes | |
95 // multiple blocks will processed, but other times will not be since the FIFO | |
96 // can satisfy the request. By using a larger callbackBufferSize, we smooth | |
97 // out the jitter. | |
98 const size_t kSmallBufferSize = 1024; | |
99 const size_t kDefaultCallbackBufferSize = 2048; | |
100 | |
101 if (m_callbackBufferSize <= kSmallBufferSize) | |
102 m_callbackBufferSize = kDefaultCallbackBufferSize; | |
103 | |
104 LOG(INFO) << "audioHardwareBufferSize = " << recommendedHardwareBufferSize; | |
105 LOG(INFO) << "callbackBufferSize = " << m_callbackBufferSize; | |
106 #endif | |
107 | |
108 // Quick exit if the requested size is too large. | |
109 DCHECK_LE(m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames, | |
110 fifoSize); | |
111 if (m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames > fifoSize) | |
112 return; | |
113 | |
114 m_audioDevice = WTF::wrapUnique(Platform::current()->createAudioDevice( | |
115 m_callbackBufferSize, numberOfInputChannels, numberOfOutputChannels, | |
116 sampleRate, this, inputDeviceId, std::move(securityOrigin))); | |
117 ASSERT(m_audioDevice); | |
118 | |
119 // Record the sizes if we successfully created an output device. | |
120 hardwareBufferSizeHistogram.sample(recommendedHardwareBufferSize); | |
121 callbackBufferSizeHistogram.sample(m_callbackBufferSize); | |
122 | |
123 // Create a FIFO to handle the possibility of the callback size | |
124 // not being a multiple of the render size. If the FIFO already | |
125 // contains enough data, the data will be provided directly. | |
126 // Otherwise, the FIFO will call the provider enough times to | |
127 // satisfy the request for data. | |
128 m_fifo = | |
129 WTF::wrapUnique(new AudioPullFIFO(*this, numberOfOutputChannels, fifoSize, | |
130 AudioUtilities::kRenderQuantumFrames)); | |
131 } | 89 } |
132 | 90 |
133 AudioDestination::~AudioDestination() { | 91 AudioDestination::~AudioDestination() { |
134 stop(); | 92 stop(); |
135 } | 93 } |
136 | 94 |
137 void AudioDestination::start() { | 95 void AudioDestination::render(const WebVector<float*>& destinationData, |
138 if (!m_isPlaying && m_audioDevice) { | |
139 m_audioDevice->start(); | |
140 m_isPlaying = true; | |
141 } | |
142 } | |
143 | |
144 void AudioDestination::stop() { | |
145 if (m_isPlaying && m_audioDevice) { | |
146 m_audioDevice->stop(); | |
147 m_isPlaying = false; | |
148 } | |
149 } | |
150 | |
151 float AudioDestination::hardwareSampleRate() { | |
152 return static_cast<float>(Platform::current()->audioHardwareSampleRate()); | |
153 } | |
154 | |
155 unsigned long AudioDestination::maxChannelCount() { | |
156 return static_cast<float>(Platform::current()->audioHardwareOutputChannels()); | |
157 } | |
158 | |
159 void AudioDestination::render(const WebVector<float*>& audioData, | |
160 size_t numberOfFrames, | 96 size_t numberOfFrames, |
161 double delay, | 97 double delay, |
162 double delayTimestamp, | 98 double delayTimestamp, |
163 size_t priorFramesSkipped) { | 99 size_t priorFramesSkipped) { |
164 bool isNumberOfChannelsGood = audioData.size() == m_numberOfOutputChannels; | 100 DCHECK_EQ(destinationData.size(), m_numberOfOutputChannels); |
165 if (!isNumberOfChannelsGood) { | 101 if (destinationData.size() != m_numberOfOutputChannels) |
166 ASSERT_NOT_REACHED(); | |
167 return; | 102 return; |
168 } | |
169 | 103 |
170 bool isBufferSizeGood = numberOfFrames == m_callbackBufferSize; | 104 DCHECK_EQ(numberOfFrames, m_callbackBufferSize); |
171 if (!isBufferSizeGood) { | 105 if (numberOfFrames != m_callbackBufferSize) |
172 ASSERT_NOT_REACHED(); | |
173 return; | 106 return; |
174 } | |
175 | 107 |
176 m_framesElapsed -= std::min(m_framesElapsed, priorFramesSkipped); | 108 m_framesElapsed -= std::min(m_framesElapsed, priorFramesSkipped); |
177 double outputPosition = | 109 double outputPosition = |
178 m_framesElapsed / static_cast<double>(m_sampleRate) - delay; | 110 m_framesElapsed / static_cast<double>(m_sampleRate) - delay; |
179 m_outputPosition.position = outputPosition; | 111 m_outputPosition.position = outputPosition; |
180 m_outputPosition.timestamp = delayTimestamp; | 112 m_outputPosition.timestamp = delayTimestamp; |
181 m_outputPositionReceivedTimestamp = base::TimeTicks::Now(); | 113 m_outputPositionReceivedTimestamp = base::TimeTicks::Now(); |
182 | 114 |
| 115 // Associate the destination data array with the output bus then fill the |
| 116 // FIFO. |
183 for (unsigned i = 0; i < m_numberOfOutputChannels; ++i) | 117 for (unsigned i = 0; i < m_numberOfOutputChannels; ++i) |
184 m_renderBus->setChannelMemory(i, audioData[i], numberOfFrames); | 118 m_outputBus->setChannelMemory(i, destinationData[i], numberOfFrames); |
185 | 119 m_fifo->consume(m_outputBus.get(), numberOfFrames); |
186 m_fifo->consume(m_renderBus.get(), numberOfFrames); | |
187 | 120 |
188 m_framesElapsed += numberOfFrames; | 121 m_framesElapsed += numberOfFrames; |
189 } | 122 } |
190 | 123 |
191 void AudioDestination::provideInput(AudioBus* bus, size_t framesToProcess) { | 124 void AudioDestination::provideInput(AudioBus* outputBus, |
| 125 size_t framesToProcess) { |
192 AudioIOPosition outputPosition = m_outputPosition; | 126 AudioIOPosition outputPosition = m_outputPosition; |
193 | 127 |
194 // If platfrom buffer is more than two times longer than |framesToProcess| | 128 // If platform buffer is more than two times longer than |framesToProcess| |
195 // we do not want output position to get stuck so we promote it | 129 // we do not want output position to get stuck so we promote it |
196 // using the elapsed time from the moment it was initially obtained. | 130 // using the elapsed time from the moment it was initially obtained. |
197 if (m_callbackBufferSize > framesToProcess * 2) { | 131 if (m_callbackBufferSize > framesToProcess * 2) { |
198 double delta = (base::TimeTicks::Now() - m_outputPositionReceivedTimestamp) | 132 double delta = (base::TimeTicks::Now() - m_outputPositionReceivedTimestamp) |
199 .InSecondsF(); | 133 .InSecondsF(); |
200 outputPosition.position += delta; | 134 outputPosition.position += delta; |
201 outputPosition.timestamp += delta; | 135 outputPosition.timestamp += delta; |
202 } | 136 } |
203 | 137 |
204 // Some implementations give only rough estimation of |delay| so | 138 // Some implementations give only rough estimation of |delay| so |
205 // we might have negative estimation |outputPosition| value. | 139 // we might have negative estimation |outputPosition| value. |
206 if (outputPosition.position < 0.0) | 140 if (outputPosition.position < 0.0) |
207 outputPosition.position = 0.0; | 141 outputPosition.position = 0.0; |
208 | 142 |
209 m_callback.render(nullptr, bus, framesToProcess, outputPosition); | 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() { |
| 148 if (m_webAudioDevice && !m_isPlaying) { |
| 149 m_webAudioDevice->start(); |
| 150 m_isPlaying = true; |
| 151 } |
| 152 } |
| 153 |
| 154 void AudioDestination::stop() { |
| 155 if (m_webAudioDevice && m_isPlaying) { |
| 156 m_webAudioDevice->stop(); |
| 157 m_isPlaying = false; |
| 158 } |
| 159 } |
| 160 |
| 161 size_t AudioDestination::hardwareBufferSize() { |
| 162 return Platform::current()->audioHardwareBufferSize(); |
| 163 } |
| 164 |
| 165 float AudioDestination::hardwareSampleRate() { |
| 166 return static_cast<float>(Platform::current()->audioHardwareSampleRate()); |
| 167 } |
| 168 |
| 169 unsigned long AudioDestination::maxChannelCount() { |
| 170 return static_cast<unsigned long>( |
| 171 Platform::current()->audioHardwareOutputChannels()); |
| 172 } |
| 173 |
| 174 bool AudioDestination::calculateBufferSize() { |
| 175 // Use the optimal buffer size recommended by the audio backend. |
| 176 size_t recommendedHardwareBufferSize = hardwareBufferSize(); |
| 177 m_callbackBufferSize = recommendedHardwareBufferSize; |
| 178 |
| 179 #if OS(ANDROID) |
| 180 // The optimum low-latency hardware buffer size is usually too small on |
| 181 // Android for WebAudio to render without glitching. So, if it is small, use a |
| 182 // larger size. If it was already large, use the requested size. |
| 183 // |
| 184 // Since WebAudio renders in 128-frame blocks, the small buffer sizes (144 for |
| 185 // a Galaxy Nexus), cause significant processing jitter. Sometimes multiple |
| 186 // blocks will processed, but other times will not be since the FIFO can |
| 187 // satisfy the request. By using a larger callbackBufferSize, we smooth out |
| 188 // the jitter. |
| 189 const size_t kSmallBufferSize = 1024; |
| 190 const size_t kDefaultCallbackBufferSize = 2048; |
| 191 |
| 192 if (m_callbackBufferSize <= kSmallBufferSize) |
| 193 m_callbackBufferSize = kDefaultCallbackBufferSize; |
| 194 |
| 195 LOG(INFO) << "audioHardwareBufferSize = " << recommendedHardwareBufferSize; |
| 196 LOG(INFO) << "callbackBufferSize = " << m_callbackBufferSize; |
| 197 #endif |
| 198 |
| 199 // Histogram for audioHardwareBufferSize |
| 200 DEFINE_STATIC_LOCAL(SparseHistogram, hardwareBufferSizeHistogram, |
| 201 ("WebAudio.AudioDestination.HardwareBufferSize")); |
| 202 |
| 203 // Histogram for the actual callback size used. Typically, this is the same |
| 204 // as audioHardwareBufferSize, but can be adjusted depending on some |
| 205 // heuristics below. |
| 206 DEFINE_STATIC_LOCAL(SparseHistogram, callbackBufferSizeHistogram, |
| 207 ("WebAudio.AudioDestination.CallbackBufferSize")); |
| 208 |
| 209 // Record the sizes if we successfully created an output device. |
| 210 hardwareBufferSizeHistogram.sample(recommendedHardwareBufferSize); |
| 211 callbackBufferSizeHistogram.sample(m_callbackBufferSize); |
| 212 |
| 213 // Check if the requested buffer size is too large. |
| 214 bool isBufferSizeValid = |
| 215 m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames <= kFIFOSize; |
| 216 DCHECK(isBufferSizeValid); |
| 217 return isBufferSizeValid; |
210 } | 218 } |
211 | 219 |
212 } // namespace blink | 220 } // namespace blink |
OLD | NEW |