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 16 matching lines...) Expand all Loading... | |
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/audio/AudioUtilities.h" |
35 #include "platform/weborigin/SecurityOrigin.h" | 35 #include "platform/weborigin/SecurityOrigin.h" |
36 #include "public/platform/Platform.h" | 36 #include "public/platform/Platform.h" |
37 #include "public/platform/WebAudioLatencyHint.h" | |
37 #include "public/platform/WebSecurityOrigin.h" | 38 #include "public/platform/WebSecurityOrigin.h" |
38 #include "wtf/PtrUtil.h" | 39 #include "wtf/PtrUtil.h" |
39 #include <memory> | 40 #include <memory> |
40 | 41 |
41 namespace blink { | 42 namespace blink { |
42 | 43 |
43 // Size of the FIFO | 44 // Size of the FIFO |
44 const size_t fifoSize = 8192; | 45 const size_t fifoSize = 8192; |
45 | 46 |
46 // Factory method: Chromium-implementation | 47 // Factory method: Chromium-implementation |
47 std::unique_ptr<AudioDestination> AudioDestination::create( | 48 std::unique_ptr<AudioDestination> AudioDestination::create( |
48 AudioIOCallback& callback, | 49 AudioIOCallback& callback, |
49 const String& inputDeviceId, | 50 const String& inputDeviceId, |
50 unsigned numberOfInputChannels, | 51 unsigned numberOfInputChannels, |
51 unsigned numberOfOutputChannels, | 52 unsigned numberOfOutputChannels, |
52 float sampleRate, | 53 const WebAudioLatencyHint& latencyHint, |
53 PassRefPtr<SecurityOrigin> securityOrigin) { | 54 PassRefPtr<SecurityOrigin> securityOrigin) { |
54 return wrapUnique(new AudioDestination( | 55 return wrapUnique(new AudioDestination( |
55 callback, inputDeviceId, numberOfInputChannels, numberOfOutputChannels, | 56 callback, inputDeviceId, numberOfInputChannels, numberOfOutputChannels, |
56 sampleRate, std::move(securityOrigin))); | 57 latencyHint, std::move(securityOrigin))); |
57 } | 58 } |
58 | 59 |
59 AudioDestination::AudioDestination(AudioIOCallback& callback, | 60 AudioDestination::AudioDestination(AudioIOCallback& callback, |
60 const String& inputDeviceId, | 61 const String& inputDeviceId, |
61 unsigned numberOfInputChannels, | 62 unsigned numberOfInputChannels, |
62 unsigned numberOfOutputChannels, | 63 unsigned numberOfOutputChannels, |
63 float sampleRate, | 64 const WebAudioLatencyHint& latencyHint, |
o1ka
2016/11/30 11:46:26
WebAudioLatencyHint class really makes the code lo
Raymond Toy
2016/11/30 21:50:27
I think this is the right approach because the lat
| |
64 PassRefPtr<SecurityOrigin> securityOrigin) | 65 PassRefPtr<SecurityOrigin> securityOrigin) |
65 : m_callback(callback), | 66 : m_callback(callback), |
66 m_numberOfOutputChannels(numberOfOutputChannels), | 67 m_numberOfOutputChannels(numberOfOutputChannels), |
67 m_inputBus(AudioBus::create(numberOfInputChannels, | 68 m_inputBus(AudioBus::create(numberOfInputChannels, |
68 AudioUtilities::kRenderQuantumFrames)), | 69 AudioUtilities::kRenderQuantumFrames)), |
69 m_renderBus(AudioBus::create(numberOfOutputChannels, | 70 m_renderBus(AudioBus::create(numberOfOutputChannels, |
70 AudioUtilities::kRenderQuantumFrames, | 71 AudioUtilities::kRenderQuantumFrames, |
71 false)), | 72 false)), |
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")); |
82 | 82 |
83 // Use the optimal buffer size recommended by the audio backend. | 83 m_audioDevice = wrapUnique(Platform::current()->createAudioDevice( |
84 size_t recommendedHardwareBufferSize = | 84 numberOfInputChannels, numberOfOutputChannels, latencyHint, this, |
85 Platform::current()->audioHardwareBufferSize(); | 85 inputDeviceId, std::move(securityOrigin))); |
86 m_callbackBufferSize = recommendedHardwareBufferSize; | 86 DCHECK(m_audioDevice); |
87 | 87 |
88 #if OS(ANDROID) | 88 m_callbackBufferSize = m_audioDevice->framesPerBuffer(); |
89 // The optimum low-latency hardware buffer size is usually too small on | |
90 // Android for WebAudio to render without glitching. So, if it is small, use | |
91 // a larger size. If it was already large, use the requested size. | |
92 // | |
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 #endif | |
104 | |
105 // Quick exit if the requested size is too large. | 89 // Quick exit if the requested size is too large. |
106 DCHECK_LE(m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames, | 90 DCHECK_LE(m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames, |
107 fifoSize); | 91 fifoSize); |
108 if (m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames > fifoSize) | 92 if (m_callbackBufferSize + AudioUtilities::kRenderQuantumFrames > fifoSize) |
109 return; | 93 return; |
110 | 94 |
111 m_audioDevice = wrapUnique(Platform::current()->createAudioDevice( | |
112 m_callbackBufferSize, numberOfInputChannels, numberOfOutputChannels, | |
113 sampleRate, this, inputDeviceId, std::move(securityOrigin))); | |
114 ASSERT(m_audioDevice); | |
115 | |
116 // Record the sizes if we successfully created an output device. | 95 // Record the sizes if we successfully created an output device. |
117 hardwareBufferSizeHistogram.sample(recommendedHardwareBufferSize); | 96 hardwareBufferSizeHistogram.sample(m_callbackBufferSize); |
o1ka
2016/11/30 11:46:26
rtoy@/hongchan@ what is your recommendation for th
Raymond Toy
2016/11/30 20:37:29
We do definitely want to keep these around. They'
Andrew MacPherson
2016/12/01 12:11:56
Should I move these to the WebAudioDeviceImpl then
Andrew MacPherson
2016/12/02 09:42:55
Maybe for now the easiest thing is to just use the
Raymond Toy
2016/12/02 16:57:45
If this preserves the current values, then I'm fin
| |
118 callbackBufferSizeHistogram.sample(m_callbackBufferSize); | 97 callbackBufferSizeHistogram.sample(m_callbackBufferSize); |
119 | 98 |
120 // Create a FIFO to handle the possibility of the callback size | 99 // Create a FIFO to handle the possibility of the callback size |
121 // not being a multiple of the render size. If the FIFO already | 100 // not being a multiple of the render size. If the FIFO already |
122 // contains enough data, the data will be provided directly. | 101 // contains enough data, the data will be provided directly. |
123 // Otherwise, the FIFO will call the provider enough times to | 102 // Otherwise, the FIFO will call the provider enough times to |
124 // satisfy the request for data. | 103 // satisfy the request for data. |
125 m_fifo = wrapUnique(new AudioPullFIFO(*this, numberOfOutputChannels, fifoSize, | 104 m_fifo = wrapUnique(new AudioPullFIFO(*this, numberOfOutputChannels, fifoSize, |
126 AudioUtilities::kRenderQuantumFrames)); | 105 AudioUtilities::kRenderQuantumFrames)); |
127 | 106 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 AudioBus* sourceBus = nullptr; | 178 AudioBus* sourceBus = nullptr; |
200 if (m_inputFifo->framesInFifo() >= framesToProcess) { | 179 if (m_inputFifo->framesInFifo() >= framesToProcess) { |
201 m_inputFifo->consume(m_inputBus.get(), framesToProcess); | 180 m_inputFifo->consume(m_inputBus.get(), framesToProcess); |
202 sourceBus = m_inputBus.get(); | 181 sourceBus = m_inputBus.get(); |
203 } | 182 } |
204 | 183 |
205 m_callback.render(sourceBus, bus, framesToProcess); | 184 m_callback.render(sourceBus, bus, framesToProcess); |
206 } | 185 } |
207 | 186 |
208 } // namespace blink | 187 } // namespace blink |
OLD | NEW |