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; determined arbitrarily. |
Raymond Toy
2016/12/22 16:59:59
That's not really true (arbitrarily). It was an e
hongchan
2016/12/22 17:19:49
Acknowledged.
hongchan
2016/12/22 18:59:45
Added comments as well. Done.
| |
44 const size_t fifoSize = 8192; | 43 const size_t kFIFOSize = 8192; |
45 | 44 |
46 // Factory method: Chromium-implementation | |
47 std::unique_ptr<AudioDestination> AudioDestination::create( | 45 std::unique_ptr<AudioDestination> AudioDestination::create( |
48 AudioIOCallback& callback, | 46 AudioIOCallback& callback, |
49 const String& inputDeviceId, | |
50 unsigned numberOfInputChannels, | |
51 unsigned numberOfOutputChannels, | 47 unsigned numberOfOutputChannels, |
52 float sampleRate, | 48 float sampleRate, |
53 PassRefPtr<SecurityOrigin> securityOrigin) { | 49 PassRefPtr<SecurityOrigin> securityOrigin) { |
54 return WTF::wrapUnique(new AudioDestination( | 50 return WTF::wrapUnique(new AudioDestination( |
55 callback, inputDeviceId, numberOfInputChannels, numberOfOutputChannels, | 51 callback, numberOfOutputChannels, sampleRate, std::move(securityOrigin))); |
56 sampleRate, std::move(securityOrigin))); | |
57 } | 52 } |
58 | 53 |
59 AudioDestination::AudioDestination(AudioIOCallback& callback, | 54 AudioDestination::AudioDestination(AudioIOCallback& callback, |
60 const String& inputDeviceId, | |
61 unsigned numberOfInputChannels, | |
62 unsigned numberOfOutputChannels, | 55 unsigned numberOfOutputChannels, |
63 float sampleRate, | 56 float sampleRate, |
64 PassRefPtr<SecurityOrigin> securityOrigin) | 57 PassRefPtr<SecurityOrigin> securityOrigin) |
65 : m_callback(callback), | 58 : m_numberOfOutputChannels(numberOfOutputChannels), |
66 m_numberOfOutputChannels(numberOfOutputChannels), | 59 m_sampleRate(sampleRate), |
67 m_renderBus(AudioBus::create(numberOfOutputChannels, | 60 m_isPlaying(false), |
61 m_callback(callback), | |
62 m_outputBus(AudioBus::create(numberOfOutputChannels, | |
68 AudioUtilities::kRenderQuantumFrames, | 63 AudioUtilities::kRenderQuantumFrames, |
69 false)), | 64 false)), |
70 m_sampleRate(sampleRate), | 65 m_framesElapsed(0) { |
71 m_isPlaying(false), | 66 // Calculate the optimum buffer size first. |
72 m_framesElapsed(0), | 67 bool isBufferSizeValid = calculateBufferSize(); |
73 m_outputPosition() { | 68 DCHECK(isBufferSizeValid); |
Raymond Toy
2016/12/22 18:38:39
What happened to this initializer?
hongchan
2016/12/22 18:59:45
This is a non-pointer member variable. So we don't
| |
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 | 69 |
83 // Use the optimal buffer size recommended by the audio backend. | 70 if (isBufferSizeValid) { |
84 size_t recommendedHardwareBufferSize = | 71 // Create WebAudioDevice. blink::WebAudioDevice is designed to support the |
85 Platform::current()->audioHardwareBufferSize(); | 72 // local input (e.g. loopback from OS audio system), but Chromium's media |
86 m_callbackBufferSize = recommendedHardwareBufferSize; | 73 // renderer does not support it currently. Thus, we use zero for the number |
74 // of input channels. | |
75 const unsigned numberOfInputChannels = 0; | |
76 const String inputDeviceId; | |
77 m_webAudioDevice = WTF::wrapUnique(Platform::current()->createAudioDevice( | |
78 m_callbackBufferSize, numberOfInputChannels, numberOfOutputChannels, | |
79 sampleRate, this, inputDeviceId, std::move(securityOrigin))); | |
Raymond Toy
2016/12/22 18:38:39
Why define these variables that are only used once
hongchan
2016/12/22 18:59:45
Sure. I'll do that.
| |
80 DCHECK(m_webAudioDevice); | |
87 | 81 |
88 #if OS(ANDROID) | 82 // Create a FIFO. |
89 // The optimum low-latency hardware buffer size is usually too small on | 83 m_fifo = WTF::wrapUnique( |
90 // Android for WebAudio to render without glitching. So, if it is small, use | 84 new AudioPullFIFO(*this, numberOfOutputChannels, kFIFOSize, |
91 // a larger size. If it was already large, use the requested size. | 85 AudioUtilities::kRenderQuantumFrames)); |
92 // | 86 } else { |
93 // Since WebAudio renders in 128-frame blocks, the small buffer sizes (144 | 87 NOTREACHED(); |
Raymond Toy
2016/12/22 18:38:39
Is this really necessary? You already have a DCHEC
hongchan
2016/12/22 18:59:45
I can remove it, however I am not sure what we sho
| |
94 // for a Galaxy Nexus), cause significant processing jitter. Sometimes | 88 } |
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 // Quick exit if the requested size is too large. | |
214 DCHECK_LE(m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames, | |
215 kFIFOSize); | |
216 if (m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames > kFIFOSize) | |
217 return false; | |
218 | |
219 return true; | |
210 } | 220 } |
211 | 221 |
212 } // namespace blink | 222 } // namespace blink |
OLD | NEW |