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/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 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)), | |
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 int framesToPull = numberOfFrames - m_fifo->framesAvailable(); | |
120 DCHECK_LE(static_cast<size_t>(m_fifo->framesAvailable()), numberOfFrames); | |
o1ka
2017/01/24 11:09:02
This does not guarantee that |framesToPull| fits i
hongchan
2017/01/26 22:33:18
Changing all ints to size_t. That will solve this
| |
121 | |
122 while (framesToPull > 0) { | |
123 // If platform buffer is more than two times longer than |framesToProcess| | |
124 // we do not want output position to get stuck so we promote it | |
125 // using the elapsed time from the moment it was initially obtained. | |
126 if (m_callbackBufferSize > AudioUtilities::kRenderQuantumFrames * 2) { | |
127 double delta = | |
128 (base::TimeTicks::Now() - m_outputPositionReceivedTimestamp) | |
129 .InSecondsF(); | |
130 m_outputPosition.position += delta; | |
131 m_outputPosition.timestamp += delta; | |
132 } | |
133 | |
134 // Some implementations give only rough estimation of |delay| so | |
135 // we might have negative estimation |outputPosition| value. | |
136 if (m_outputPosition.position < 0.0) | |
137 m_outputPosition.position = 0.0; | |
138 | |
139 m_callback.render(nullptr, m_renderBus.get(), | |
140 AudioUtilities::kRenderQuantumFrames, m_outputPosition); | |
141 m_fifo->push(m_renderBus.get()); | |
142 | |
143 framesToPull -= AudioUtilities::kRenderQuantumFrames; | |
144 } | |
145 | |
146 m_fifo->pull(m_outputBus.get(), numberOfFrames); | |
120 | 147 |
121 m_framesElapsed += numberOfFrames; | 148 m_framesElapsed += numberOfFrames; |
122 } | 149 } |
123 | 150 |
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() { | 151 void AudioDestination::start() { |
148 if (m_webAudioDevice && !m_isPlaying) { | 152 if (m_webAudioDevice && !m_isPlaying) { |
149 m_webAudioDevice->start(); | 153 m_webAudioDevice->start(); |
150 m_isPlaying = true; | 154 m_isPlaying = true; |
151 } | 155 } |
152 } | 156 } |
153 | 157 |
154 void AudioDestination::stop() { | 158 void AudioDestination::stop() { |
155 if (m_webAudioDevice && m_isPlaying) { | 159 if (m_webAudioDevice && m_isPlaying) { |
156 m_webAudioDevice->stop(); | 160 m_webAudioDevice->stop(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 callbackBufferSizeHistogram.sample(m_callbackBufferSize); | 215 callbackBufferSizeHistogram.sample(m_callbackBufferSize); |
212 | 216 |
213 // Check if the requested buffer size is too large. | 217 // Check if the requested buffer size is too large. |
214 bool isBufferSizeValid = | 218 bool isBufferSizeValid = |
215 m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames <= kFIFOSize; | 219 m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames <= kFIFOSize; |
216 DCHECK(isBufferSizeValid); | 220 DCHECK(isBufferSizeValid); |
217 return isBufferSizeValid; | 221 return isBufferSizeValid; |
218 } | 222 } |
219 | 223 |
220 } // namespace blink | 224 } // namespace blink |
OLD | NEW |