| 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 13 matching lines...) Expand all Loading... |
| 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" | 32 #include "platform/audio/AudioFIFO.h" |
| 33 #include "platform/audio/AudioPullFIFO.h" | 33 #include "platform/audio/AudioPullFIFO.h" |
| 34 #include "platform/audio/AudioUtilities.h" |
| 34 #include "platform/weborigin/SecurityOrigin.h" | 35 #include "platform/weborigin/SecurityOrigin.h" |
| 35 #include "public/platform/Platform.h" | 36 #include "public/platform/Platform.h" |
| 36 #include "public/platform/WebSecurityOrigin.h" | 37 #include "public/platform/WebSecurityOrigin.h" |
| 37 #include "wtf/PtrUtil.h" | 38 #include "wtf/PtrUtil.h" |
| 38 #include <memory> | 39 #include <memory> |
| 39 | 40 |
| 40 namespace blink { | 41 namespace blink { |
| 41 | 42 |
| 42 // Buffer size at which the web audio engine will render. | |
| 43 const unsigned renderBufferSize = 128; | |
| 44 | |
| 45 // Size of the FIFO | 43 // Size of the FIFO |
| 46 const size_t fifoSize = 8192; | 44 const size_t fifoSize = 8192; |
| 47 | 45 |
| 48 // Factory method: Chromium-implementation | 46 // Factory method: Chromium-implementation |
| 49 std::unique_ptr<AudioDestination> AudioDestination::create( | 47 std::unique_ptr<AudioDestination> AudioDestination::create( |
| 50 AudioIOCallback& callback, | 48 AudioIOCallback& callback, |
| 51 const String& inputDeviceId, | 49 const String& inputDeviceId, |
| 52 unsigned numberOfInputChannels, | 50 unsigned numberOfInputChannels, |
| 53 unsigned numberOfOutputChannels, | 51 unsigned numberOfOutputChannels, |
| 54 float sampleRate, | 52 float sampleRate, |
| 55 PassRefPtr<SecurityOrigin> securityOrigin) { | 53 PassRefPtr<SecurityOrigin> securityOrigin) { |
| 56 return wrapUnique(new AudioDestination( | 54 return wrapUnique(new AudioDestination( |
| 57 callback, inputDeviceId, numberOfInputChannels, numberOfOutputChannels, | 55 callback, inputDeviceId, numberOfInputChannels, numberOfOutputChannels, |
| 58 sampleRate, std::move(securityOrigin))); | 56 sampleRate, std::move(securityOrigin))); |
| 59 } | 57 } |
| 60 | 58 |
| 61 AudioDestination::AudioDestination(AudioIOCallback& callback, | 59 AudioDestination::AudioDestination(AudioIOCallback& callback, |
| 62 const String& inputDeviceId, | 60 const String& inputDeviceId, |
| 63 unsigned numberOfInputChannels, | 61 unsigned numberOfInputChannels, |
| 64 unsigned numberOfOutputChannels, | 62 unsigned numberOfOutputChannels, |
| 65 float sampleRate, | 63 float sampleRate, |
| 66 PassRefPtr<SecurityOrigin> securityOrigin) | 64 PassRefPtr<SecurityOrigin> securityOrigin) |
| 67 : m_callback(callback), | 65 : m_callback(callback), |
| 68 m_numberOfOutputChannels(numberOfOutputChannels), | 66 m_numberOfOutputChannels(numberOfOutputChannels), |
| 69 m_inputBus(AudioBus::create(numberOfInputChannels, renderBufferSize)), | 67 m_inputBus(AudioBus::create(numberOfInputChannels, |
| 70 m_renderBus( | 68 AudioUtilities::kRenderQuantumFrames)), |
| 71 AudioBus::create(numberOfOutputChannels, renderBufferSize, false)), | 69 m_renderBus(AudioBus::create(numberOfOutputChannels, |
| 70 AudioUtilities::kRenderQuantumFrames, |
| 71 false)), |
| 72 m_sampleRate(sampleRate), | 72 m_sampleRate(sampleRate), |
| 73 m_isPlaying(false) { | 73 m_isPlaying(false) { |
| 74 // Histogram for audioHardwareBufferSize | 74 // Histogram for audioHardwareBufferSize |
| 75 DEFINE_STATIC_LOCAL(SparseHistogram, hardwareBufferSizeHistogram, | 75 DEFINE_STATIC_LOCAL(SparseHistogram, hardwareBufferSizeHistogram, |
| 76 ("WebAudio.AudioDestination.HardwareBufferSize")); | 76 ("WebAudio.AudioDestination.HardwareBufferSize")); |
| 77 // Histogram for the actual callback size used. Typically, this is the same | 77 // Histogram for the actual callback size used. Typically, this is the same |
| 78 // as audioHardwareBufferSize, but can be adjusted depending on some | 78 // as audioHardwareBufferSize, but can be adjusted depending on some |
| 79 // heuristics below. | 79 // heuristics below. |
| 80 DEFINE_STATIC_LOCAL(SparseHistogram, callbackBufferSizeHistogram, | 80 DEFINE_STATIC_LOCAL(SparseHistogram, callbackBufferSizeHistogram, |
| 81 ("WebAudio.AudioDestination.CallbackBufferSize")); | 81 ("WebAudio.AudioDestination.CallbackBufferSize")); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 96 // can satisfy the request. By using a larger callbackBufferSize, we smooth | 96 // can satisfy the request. By using a larger callbackBufferSize, we smooth |
| 97 // out the jitter. | 97 // out the jitter. |
| 98 const size_t kSmallBufferSize = 1024; | 98 const size_t kSmallBufferSize = 1024; |
| 99 const size_t kDefaultCallbackBufferSize = 2048; | 99 const size_t kDefaultCallbackBufferSize = 2048; |
| 100 | 100 |
| 101 if (m_callbackBufferSize <= kSmallBufferSize) | 101 if (m_callbackBufferSize <= kSmallBufferSize) |
| 102 m_callbackBufferSize = kDefaultCallbackBufferSize; | 102 m_callbackBufferSize = kDefaultCallbackBufferSize; |
| 103 #endif | 103 #endif |
| 104 | 104 |
| 105 // Quick exit if the requested size is too large. | 105 // Quick exit if the requested size is too large. |
| 106 ASSERT(m_callbackBufferSize + renderBufferSize <= fifoSize); | 106 DCHECK_LE(m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames, |
| 107 if (m_callbackBufferSize + renderBufferSize > fifoSize) | 107 fifoSize); |
| 108 if (m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames > fifoSize) |
| 108 return; | 109 return; |
| 109 | 110 |
| 110 m_audioDevice = wrapUnique(Platform::current()->createAudioDevice( | 111 m_audioDevice = wrapUnique(Platform::current()->createAudioDevice( |
| 111 m_callbackBufferSize, numberOfInputChannels, numberOfOutputChannels, | 112 m_callbackBufferSize, numberOfInputChannels, numberOfOutputChannels, |
| 112 sampleRate, this, inputDeviceId, std::move(securityOrigin))); | 113 sampleRate, this, inputDeviceId, std::move(securityOrigin))); |
| 113 ASSERT(m_audioDevice); | 114 ASSERT(m_audioDevice); |
| 114 | 115 |
| 115 // Record the sizes if we successfully created an output device. | 116 // Record the sizes if we successfully created an output device. |
| 116 hardwareBufferSizeHistogram.sample(recommendedHardwareBufferSize); | 117 hardwareBufferSizeHistogram.sample(recommendedHardwareBufferSize); |
| 117 callbackBufferSizeHistogram.sample(m_callbackBufferSize); | 118 callbackBufferSizeHistogram.sample(m_callbackBufferSize); |
| 118 | 119 |
| 119 // Create a FIFO to handle the possibility of the callback size | 120 // Create a FIFO to handle the possibility of the callback size |
| 120 // not being a multiple of the render size. If the FIFO already | 121 // not being a multiple of the render size. If the FIFO already |
| 121 // contains enough data, the data will be provided directly. | 122 // contains enough data, the data will be provided directly. |
| 122 // Otherwise, the FIFO will call the provider enough times to | 123 // Otherwise, the FIFO will call the provider enough times to |
| 123 // satisfy the request for data. | 124 // satisfy the request for data. |
| 124 m_fifo = wrapUnique(new AudioPullFIFO(*this, numberOfOutputChannels, fifoSize, | 125 m_fifo = wrapUnique(new AudioPullFIFO(*this, numberOfOutputChannels, fifoSize, |
| 125 renderBufferSize)); | 126 AudioUtilities::kRenderQuantumFrames)); |
| 126 | 127 |
| 127 // Input buffering. | 128 // Input buffering. |
| 128 m_inputFifo = wrapUnique(new AudioFIFO(numberOfInputChannels, fifoSize)); | 129 m_inputFifo = wrapUnique(new AudioFIFO(numberOfInputChannels, fifoSize)); |
| 129 | 130 |
| 130 // If the callback size does not match the render size, then we need to | 131 // If the callback size does not match the render size, then we need to |
| 131 // buffer some extra silence for the input. Otherwise, we can over-consume | 132 // buffer some extra silence for the input. Otherwise, we can over-consume |
| 132 // the input FIFO. | 133 // the input FIFO. |
| 133 if (m_callbackBufferSize != renderBufferSize) { | 134 if (m_callbackBufferSize != AudioUtilities::kRenderQuantumFrames) { |
| 134 // FIXME: handle multi-channel input and don't hard-code to stereo. | 135 // FIXME: handle multi-channel input and don't hard-code to stereo. |
| 135 RefPtr<AudioBus> silence = AudioBus::create(2, renderBufferSize); | 136 RefPtr<AudioBus> silence = |
| 137 AudioBus::create(2, AudioUtilities::kRenderQuantumFrames); |
| 136 m_inputFifo->push(silence.get()); | 138 m_inputFifo->push(silence.get()); |
| 137 } | 139 } |
| 138 } | 140 } |
| 139 | 141 |
| 140 AudioDestination::~AudioDestination() { | 142 AudioDestination::~AudioDestination() { |
| 141 stop(); | 143 stop(); |
| 142 } | 144 } |
| 143 | 145 |
| 144 void AudioDestination::start() { | 146 void AudioDestination::start() { |
| 145 if (!m_isPlaying && m_audioDevice) { | 147 if (!m_isPlaying && m_audioDevice) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 AudioBus* sourceBus = nullptr; | 199 AudioBus* sourceBus = nullptr; |
| 198 if (m_inputFifo->framesInFifo() >= framesToProcess) { | 200 if (m_inputFifo->framesInFifo() >= framesToProcess) { |
| 199 m_inputFifo->consume(m_inputBus.get(), framesToProcess); | 201 m_inputFifo->consume(m_inputBus.get(), framesToProcess); |
| 200 sourceBus = m_inputBus.get(); | 202 sourceBus = m_inputBus.get(); |
| 201 } | 203 } |
| 202 | 204 |
| 203 m_callback.render(sourceBus, bus, framesToProcess); | 205 m_callback.render(sourceBus, bus, framesToProcess); |
| 204 } | 206 } |
| 205 | 207 |
| 206 } // namespace blink | 208 } // namespace blink |
| OLD | NEW |