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 | |
Raymond Toy
2016/12/22 21:46:07
crbug.com/670747
| |
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. | |
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 bool isBufferSizeValid = calculateBufferSize(); |
73 m_outputPosition() { | 73 DCHECK(isBufferSizeValid); |
74 // Histogram for audioHardwareBufferSize | |
75 DEFINE_STATIC_LOCAL(SparseHistogram, hardwareBufferSizeHistogram, | |
76 ("WebAudio.AudioDestination.HardwareBufferSize")); | |
77 // Histogram for the actual callback size used. Typically, this is the same | |
78 // as audioHardwareBufferSize, but can be adjusted depending on some | |
79 // heuristics below. | |
80 DEFINE_STATIC_LOCAL(SparseHistogram, callbackBufferSizeHistogram, | |
81 ("WebAudio.AudioDestination.CallbackBufferSize")); | |
82 | 74 |
83 // Use the optimal buffer size recommended by the audio backend. | 75 if (isBufferSizeValid) { |
84 size_t recommendedHardwareBufferSize = | 76 // Create WebAudioDevice. blink::WebAudioDevice is designed to support the |
85 Platform::current()->audioHardwareBufferSize(); | 77 // local input (e.g. loopback from OS audio system), but Chromium's media |
86 m_callbackBufferSize = recommendedHardwareBufferSize; | 78 // renderer does not support it currently. Thus, we use zero for the number |
79 // of input channels. | |
80 m_webAudioDevice = WTF::wrapUnique(Platform::current()->createAudioDevice( | |
81 m_callbackBufferSize, 0, numberOfOutputChannels, sampleRate, this, | |
82 String(), std::move(securityOrigin))); | |
83 DCHECK(m_webAudioDevice); | |
87 | 84 |
88 #if OS(ANDROID) | 85 // Create a FIFO. |
89 // The optimum low-latency hardware buffer size is usually too small on | 86 m_fifo = WTF::wrapUnique( |
90 // Android for WebAudio to render without glitching. So, if it is small, use | 87 new AudioPullFIFO(*this, numberOfOutputChannels, kFIFOSize, |
91 // a larger size. If it was already large, use the requested size. | 88 AudioUtilities::kRenderQuantumFrames)); |
92 // | 89 } |
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 } | 90 } |
132 | 91 |
133 AudioDestination::~AudioDestination() { | 92 AudioDestination::~AudioDestination() { |
134 stop(); | 93 stop(); |
135 } | 94 } |
136 | 95 |
137 void AudioDestination::start() { | 96 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, | 97 size_t numberOfFrames, |
161 double delay, | 98 double delay, |
162 double delayTimestamp, | 99 double delayTimestamp, |
163 size_t priorFramesSkipped) { | 100 size_t priorFramesSkipped) { |
164 bool isNumberOfChannelsGood = audioData.size() == m_numberOfOutputChannels; | 101 DCHECK_EQ(destinationData.size(), m_numberOfOutputChannels); |
165 if (!isNumberOfChannelsGood) { | 102 if (destinationData.size() != m_numberOfOutputChannels) |
166 ASSERT_NOT_REACHED(); | |
167 return; | 103 return; |
168 } | |
169 | 104 |
170 bool isBufferSizeGood = numberOfFrames == m_callbackBufferSize; | 105 DCHECK_EQ(numberOfFrames, m_callbackBufferSize); |
171 if (!isBufferSizeGood) { | 106 if (numberOfFrames != m_callbackBufferSize) |
172 ASSERT_NOT_REACHED(); | |
173 return; | 107 return; |
174 } | |
175 | 108 |
176 m_framesElapsed -= std::min(m_framesElapsed, priorFramesSkipped); | 109 m_framesElapsed -= std::min(m_framesElapsed, priorFramesSkipped); |
177 double outputPosition = | 110 double outputPosition = |
178 m_framesElapsed / static_cast<double>(m_sampleRate) - delay; | 111 m_framesElapsed / static_cast<double>(m_sampleRate) - delay; |
179 m_outputPosition.position = outputPosition; | 112 m_outputPosition.position = outputPosition; |
180 m_outputPosition.timestamp = delayTimestamp; | 113 m_outputPosition.timestamp = delayTimestamp; |
181 m_outputPositionReceivedTimestamp = base::TimeTicks::Now(); | 114 m_outputPositionReceivedTimestamp = base::TimeTicks::Now(); |
182 | 115 |
116 // Associate the destination data array with the output bus then fill the | |
117 // FIFO. | |
183 for (unsigned i = 0; i < m_numberOfOutputChannels; ++i) | 118 for (unsigned i = 0; i < m_numberOfOutputChannels; ++i) |
184 m_renderBus->setChannelMemory(i, audioData[i], numberOfFrames); | 119 m_outputBus->setChannelMemory(i, destinationData[i], numberOfFrames); |
185 | 120 m_fifo->consume(m_outputBus.get(), numberOfFrames); |
186 m_fifo->consume(m_renderBus.get(), numberOfFrames); | |
187 | 121 |
188 m_framesElapsed += numberOfFrames; | 122 m_framesElapsed += numberOfFrames; |
189 } | 123 } |
190 | 124 |
191 void AudioDestination::provideInput(AudioBus* bus, size_t framesToProcess) { | 125 void AudioDestination::provideInput(AudioBus* outputBus, |
126 size_t framesToProcess) { | |
192 AudioIOPosition outputPosition = m_outputPosition; | 127 AudioIOPosition outputPosition = m_outputPosition; |
193 | 128 |
194 // If platfrom buffer is more than two times longer than |framesToProcess| | 129 // 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 | 130 // 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. | 131 // using the elapsed time from the moment it was initially obtained. |
197 if (m_callbackBufferSize > framesToProcess * 2) { | 132 if (m_callbackBufferSize > framesToProcess * 2) { |
198 double delta = (base::TimeTicks::Now() - m_outputPositionReceivedTimestamp) | 133 double delta = (base::TimeTicks::Now() - m_outputPositionReceivedTimestamp) |
199 .InSecondsF(); | 134 .InSecondsF(); |
200 outputPosition.position += delta; | 135 outputPosition.position += delta; |
201 outputPosition.timestamp += delta; | 136 outputPosition.timestamp += delta; |
202 } | 137 } |
203 | 138 |
204 // Some implementations give only rough estimation of |delay| so | 139 // Some implementations give only rough estimation of |delay| so |
205 // we might have negative estimation |outputPosition| value. | 140 // we might have negative estimation |outputPosition| value. |
206 if (outputPosition.position < 0.0) | 141 if (outputPosition.position < 0.0) |
207 outputPosition.position = 0.0; | 142 outputPosition.position = 0.0; |
208 | 143 |
209 m_callback.render(nullptr, bus, framesToProcess, outputPosition); | 144 // To fill the FIFO, start the render call chain of the destination node. |
145 m_callback.render(nullptr, outputBus, framesToProcess, outputPosition); | |
146 } | |
147 | |
148 void AudioDestination::start() { | |
149 if (m_webAudioDevice && !m_isPlaying) { | |
150 m_webAudioDevice->start(); | |
151 m_isPlaying = true; | |
152 } | |
153 } | |
154 | |
155 void AudioDestination::stop() { | |
156 if (m_webAudioDevice && m_isPlaying) { | |
157 m_webAudioDevice->stop(); | |
158 m_isPlaying = false; | |
159 } | |
160 } | |
161 | |
162 size_t AudioDestination::hardwareBufferSize() { | |
163 return Platform::current()->audioHardwareBufferSize(); | |
164 } | |
165 | |
166 float AudioDestination::hardwareSampleRate() { | |
167 return static_cast<float>(Platform::current()->audioHardwareSampleRate()); | |
168 } | |
169 | |
170 unsigned long AudioDestination::maxChannelCount() { | |
171 return static_cast<unsigned long>( | |
172 Platform::current()->audioHardwareOutputChannels()); | |
173 } | |
174 | |
175 bool AudioDestination::calculateBufferSize() { | |
176 // Use the optimal buffer size recommended by the audio backend. | |
177 size_t recommendedHardwareBufferSize = hardwareBufferSize(); | |
178 m_callbackBufferSize = recommendedHardwareBufferSize; | |
179 | |
180 #if OS(ANDROID) | |
181 // The optimum low-latency hardware buffer size is usually too small on | |
182 // Android for WebAudio to render without glitching. So, if it is small, use a | |
183 // larger size. If it was already large, use the requested size. | |
184 // | |
185 // Since WebAudio renders in 128-frame blocks, the small buffer sizes (144 for | |
186 // a Galaxy Nexus), cause significant processing jitter. Sometimes multiple | |
187 // blocks will processed, but other times will not be since the FIFO can | |
188 // satisfy the request. By using a larger callbackBufferSize, we smooth out | |
189 // the jitter. | |
190 const size_t kSmallBufferSize = 1024; | |
191 const size_t kDefaultCallbackBufferSize = 2048; | |
192 | |
193 if (m_callbackBufferSize <= kSmallBufferSize) | |
194 m_callbackBufferSize = kDefaultCallbackBufferSize; | |
195 | |
196 LOG(INFO) << "audioHardwareBufferSize = " << recommendedHardwareBufferSize; | |
197 LOG(INFO) << "callbackBufferSize = " << m_callbackBufferSize; | |
198 #endif | |
199 | |
200 // Histogram for audioHardwareBufferSize | |
201 DEFINE_STATIC_LOCAL(SparseHistogram, hardwareBufferSizeHistogram, | |
202 ("WebAudio.AudioDestination.HardwareBufferSize")); | |
203 | |
204 // Histogram for the actual callback size used. Typically, this is the same | |
205 // as audioHardwareBufferSize, but can be adjusted depending on some | |
206 // heuristics below. | |
207 DEFINE_STATIC_LOCAL(SparseHistogram, callbackBufferSizeHistogram, | |
208 ("WebAudio.AudioDestination.CallbackBufferSize")); | |
209 | |
210 // Record the sizes if we successfully created an output device. | |
211 hardwareBufferSizeHistogram.sample(recommendedHardwareBufferSize); | |
212 callbackBufferSizeHistogram.sample(m_callbackBufferSize); | |
213 | |
214 // Quick exit if the requested size is too large. | |
215 DCHECK_LE(m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames, | |
216 kFIFOSize); | |
217 if (m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames > kFIFOSize) | |
218 return false; | |
219 | |
220 return true; | |
Raymond Toy
2016/12/22 21:46:06
Instead of lines 217-220, maybe just do
return m_
hongchan
2017/01/03 18:50:26
Done.
| |
210 } | 221 } |
211 | 222 |
212 } // namespace blink | 223 } // namespace blink |
OLD | NEW |