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 10 matching lines...) Expand all Loading... |
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
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 <memory> | |
32 #include "platform/Histogram.h" | 31 #include "platform/Histogram.h" |
| 32 #include "platform/audio/AudioPullFIFO.h" |
33 #include "platform/audio/AudioUtilities.h" | 33 #include "platform/audio/AudioUtilities.h" |
34 #include "platform/audio/PushPullFIFO.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" |
| 38 #include <memory> |
39 | 39 |
40 namespace blink { | 40 namespace blink { |
41 | 41 |
42 // FIFO Size. | 42 // FIFO Size. |
43 // | 43 // |
44 // TODO(hongchan): This was estimated based on the largest callback buffer size | 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 | 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 | 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 | 47 // 8000 or so. We might need to make this larger. See: crbug.com/670747 |
48 const size_t kFIFOSize = 8192; | 48 const size_t kFIFOSize = 8192; |
(...skipping 11 matching lines...) Expand all Loading... |
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)), | |
72 m_framesElapsed(0) { | 70 m_framesElapsed(0) { |
73 // Calculate the optimum buffer size first. | 71 // Calculate the optimum buffer size first. |
74 if (calculateBufferSize()) { | 72 if (calculateBufferSize()) { |
75 // Create WebAudioDevice. blink::WebAudioDevice is designed to support the | 73 // Create WebAudioDevice. blink::WebAudioDevice is designed to support the |
76 // local input (e.g. loopback from OS audio system), but Chromium's media | 74 // local input (e.g. loopback from OS audio system), but Chromium's media |
77 // renderer does not support it currently. Thus, we use zero for the number | 75 // renderer does not support it currently. Thus, we use zero for the number |
78 // of input channels. | 76 // of input channels. |
79 m_webAudioDevice = WTF::wrapUnique(Platform::current()->createAudioDevice( | 77 m_webAudioDevice = WTF::wrapUnique(Platform::current()->createAudioDevice( |
80 m_callbackBufferSize, 0, numberOfOutputChannels, sampleRate, this, | 78 m_callbackBufferSize, 0, numberOfOutputChannels, sampleRate, this, |
81 String(), std::move(securityOrigin))); | 79 String(), std::move(securityOrigin))); |
82 DCHECK(m_webAudioDevice); | 80 DCHECK(m_webAudioDevice); |
83 | 81 |
84 // Create a FIFO. | 82 // Create a FIFO. |
85 m_fifo = | 83 m_fifo = WTF::wrapUnique( |
86 WTF::wrapUnique(new PushPullFIFO(numberOfOutputChannels, kFIFOSize)); | 84 new AudioPullFIFO(*this, numberOfOutputChannels, kFIFOSize, |
| 85 AudioUtilities::kRenderQuantumFrames)); |
87 } else { | 86 } else { |
88 NOTREACHED(); | 87 NOTREACHED(); |
89 } | 88 } |
90 } | 89 } |
91 | 90 |
92 AudioDestination::~AudioDestination() { | 91 AudioDestination::~AudioDestination() { |
93 stop(); | 92 stop(); |
94 } | 93 } |
95 | 94 |
96 void AudioDestination::render(const WebVector<float*>& destinationData, | 95 void AudioDestination::render(const WebVector<float*>& destinationData, |
97 size_t numberOfFrames, | 96 size_t numberOfFrames, |
98 double delay, | 97 double delay, |
99 double delayTimestamp, | 98 double delayTimestamp, |
100 size_t priorFramesSkipped) { | 99 size_t priorFramesSkipped) { |
101 CHECK_EQ(destinationData.size(), m_numberOfOutputChannels); | 100 DCHECK_EQ(destinationData.size(), m_numberOfOutputChannels); |
102 CHECK_EQ(numberOfFrames, m_callbackBufferSize); | 101 if (destinationData.size() != m_numberOfOutputChannels) |
| 102 return; |
| 103 |
| 104 DCHECK_EQ(numberOfFrames, m_callbackBufferSize); |
| 105 if (numberOfFrames != m_callbackBufferSize) |
| 106 return; |
103 | 107 |
104 m_framesElapsed -= std::min(m_framesElapsed, priorFramesSkipped); | 108 m_framesElapsed -= std::min(m_framesElapsed, priorFramesSkipped); |
105 double outputPosition = | 109 double outputPosition = |
106 m_framesElapsed / static_cast<double>(m_sampleRate) - delay; | 110 m_framesElapsed / static_cast<double>(m_sampleRate) - delay; |
107 m_outputPosition.position = outputPosition; | 111 m_outputPosition.position = outputPosition; |
108 m_outputPosition.timestamp = delayTimestamp; | 112 m_outputPosition.timestamp = delayTimestamp; |
109 m_outputPositionReceivedTimestamp = base::TimeTicks::Now(); | 113 m_outputPositionReceivedTimestamp = base::TimeTicks::Now(); |
110 | 114 |
111 // Associate the destination data array with the output bus then fill the | 115 // Associate the destination data array with the output bus then fill the |
112 // FIFO. | 116 // FIFO. |
113 for (unsigned i = 0; i < m_numberOfOutputChannels; ++i) | 117 for (unsigned i = 0; i < m_numberOfOutputChannels; ++i) |
114 m_outputBus->setChannelMemory(i, destinationData[i], numberOfFrames); | 118 m_outputBus->setChannelMemory(i, destinationData[i], numberOfFrames); |
115 | 119 m_fifo->consume(m_outputBus.get(), numberOfFrames); |
116 // Number of frames to render via WebAudio graph. |framesToRender > 0| means | |
117 // the frames in FIFO is not enough to fulfill the requested frames from the | |
118 // audio device. | |
119 size_t framesToRender = numberOfFrames > m_fifo->framesAvailable() | |
120 ? numberOfFrames - m_fifo->framesAvailable() | |
121 : 0; | |
122 | |
123 for (size_t pushedFrames = 0; pushedFrames < framesToRender; | |
124 pushedFrames += AudioUtilities::kRenderQuantumFrames) { | |
125 // If platform buffer is more than two times longer than |framesToProcess| | |
126 // we do not want output position to get stuck so we promote it | |
127 // using the elapsed time from the moment it was initially obtained. | |
128 if (m_callbackBufferSize > AudioUtilities::kRenderQuantumFrames * 2) { | |
129 double delta = | |
130 (base::TimeTicks::Now() - m_outputPositionReceivedTimestamp) | |
131 .InSecondsF(); | |
132 m_outputPosition.position += delta; | |
133 m_outputPosition.timestamp += delta; | |
134 } | |
135 | |
136 // Some implementations give only rough estimation of |delay| so | |
137 // we might have negative estimation |outputPosition| value. | |
138 if (m_outputPosition.position < 0.0) | |
139 m_outputPosition.position = 0.0; | |
140 | |
141 // Process WebAudio graph and push the rendered output to FIFO. | |
142 m_callback.render(nullptr, m_renderBus.get(), | |
143 AudioUtilities::kRenderQuantumFrames, m_outputPosition); | |
144 m_fifo->push(m_renderBus.get()); | |
145 } | |
146 | |
147 m_fifo->pull(m_outputBus.get(), numberOfFrames); | |
148 | 120 |
149 m_framesElapsed += numberOfFrames; | 121 m_framesElapsed += numberOfFrames; |
150 } | 122 } |
151 | 123 |
| 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 |
152 void AudioDestination::start() { | 147 void AudioDestination::start() { |
153 if (m_webAudioDevice && !m_isPlaying) { | 148 if (m_webAudioDevice && !m_isPlaying) { |
154 m_webAudioDevice->start(); | 149 m_webAudioDevice->start(); |
155 m_isPlaying = true; | 150 m_isPlaying = true; |
156 } | 151 } |
157 } | 152 } |
158 | 153 |
159 void AudioDestination::stop() { | 154 void AudioDestination::stop() { |
160 if (m_webAudioDevice && m_isPlaying) { | 155 if (m_webAudioDevice && m_isPlaying) { |
161 m_webAudioDevice->stop(); | 156 m_webAudioDevice->stop(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 callbackBufferSizeHistogram.sample(m_callbackBufferSize); | 211 callbackBufferSizeHistogram.sample(m_callbackBufferSize); |
217 | 212 |
218 // Check if the requested buffer size is too large. | 213 // Check if the requested buffer size is too large. |
219 bool isBufferSizeValid = | 214 bool isBufferSizeValid = |
220 m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames <= kFIFOSize; | 215 m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames <= kFIFOSize; |
221 DCHECK(isBufferSizeValid); | 216 DCHECK(isBufferSizeValid); |
222 return isBufferSizeValid; | 217 return isBufferSizeValid; |
223 } | 218 } |
224 | 219 |
225 } // namespace blink | 220 } // namespace blink |
OLD | NEW |