OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Software adjust volume of samples, allows each audio stream its own | 5 // Software adjust volume of samples, allows each audio stream its own |
6 // volume without impacting master volume for chrome and other applications. | 6 // volume without impacting master volume for chrome and other applications. |
7 | 7 |
8 // Implemented as templates to allow 8, 16 and 32 bit implementations. | 8 // Implemented as templates to allow 8, 16 and 32 bit implementations. |
9 // 8 bit is unsigned and biased by 128. | 9 // 8 bit is unsigned and biased by 128. |
10 | 10 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } else if (bytes_per_sample == 4) { | 101 } else if (bytes_per_sample == 4) { |
102 AdjustVolume<int32, int64, 0>(reinterpret_cast<int32*>(buf), | 102 AdjustVolume<int32, int64, 0>(reinterpret_cast<int32*>(buf), |
103 sample_count, | 103 sample_count, |
104 fixed_volume); | 104 fixed_volume); |
105 return true; | 105 return true; |
106 } | 106 } |
107 } | 107 } |
108 return false; | 108 return false; |
109 } | 109 } |
110 | 110 |
111 int GetAudioHardwareSampleRate() { | |
112 #if defined(OS_MACOSX) | |
113 // Hardware sample-rate on the Mac can be configured, so we must query. | |
114 return AUAudioOutputStream::HardwareSampleRate(); | |
115 #elif defined(OS_WIN) | |
116 if (!CoreAudioUtil::IsSupported()) { | |
117 // Fall back to Windows Wave implementation on Windows XP or lower | |
118 // and use 48kHz as default input sample rate. | |
119 return 48000; | |
120 } | |
121 | |
122 // TODO(crogers): tune this rate for best possible WebAudio performance. | |
123 // WebRTC works well at 48kHz and a buffer size of 480 samples will be used | |
124 // for this case. Note that exclusive mode is experimental. | |
125 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
126 if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio)) { | |
127 // This sample rate will be combined with a buffer size of 256 samples | |
128 // (see GetAudioHardwareBufferSize()), which corresponds to an output | |
129 // delay of ~5.33ms. | |
130 return 48000; | |
131 } | |
132 | |
133 // Hardware sample-rate on Windows can be configured, so we must query. | |
134 // TODO(henrika): improve possibility to specify an audio endpoint. | |
135 // Use the default device (same as for Wave) for now to be compatible. | |
136 return WASAPIAudioOutputStream::HardwareSampleRate(); | |
137 #elif defined(OS_ANDROID) | |
138 // TODO(leozwang): return native sampling rate on Android. | |
139 return 16000; | |
140 #else | |
141 return 48000; | |
142 #endif | |
143 } | |
144 | |
145 int GetAudioInputHardwareSampleRate(const std::string& device_id) { | |
146 // TODO(henrika): add support for device selection on all platforms. | |
147 // Only exists on Windows today. | |
148 #if defined(OS_MACOSX) | |
149 return AUAudioInputStream::HardwareSampleRate(); | |
150 #elif defined(OS_WIN) | |
151 if (!CoreAudioUtil::IsSupported()) { | |
152 return 48000; | |
153 } | |
154 return WASAPIAudioInputStream::HardwareSampleRate(device_id); | |
155 #elif defined(OS_ANDROID) | |
156 return 16000; | |
157 #else | |
158 // Hardware for Linux is nearly always 48KHz. | |
159 // TODO(crogers) : return correct value in rare non-48KHz cases. | |
160 return 48000; | |
161 #endif | |
162 } | |
163 | |
164 size_t GetAudioHardwareBufferSize() { | |
165 int user_buffer_size = GetUserBufferSize(); | |
166 if (user_buffer_size) | |
167 return user_buffer_size; | |
168 | |
169 // The sizes here were determined by experimentation and are roughly | |
170 // the lowest value (for low latency) that still allowed glitch-free | |
171 // audio under high loads. | |
172 // | |
173 // For Mac OS X and Windows the chromium audio backend uses a low-latency | |
174 // Core Audio API, so a low buffer size is possible. For Linux, further | |
175 // tuning may be needed. | |
176 #if defined(OS_MACOSX) | |
177 return 128; | |
178 #elif defined(OS_WIN) | |
179 // TODO(henrika): resolve conflict with GetUserBufferSize(). | |
180 // If the user tries to set a buffer size using GetUserBufferSize() it will | |
181 // most likely fail since only the native/perfect buffer size is allowed. | |
182 | |
183 // Buffer size to use when a proper size can't be determined from the system. | |
184 static const int kFallbackBufferSize = 2048; | |
185 | |
186 if (!CoreAudioUtil::IsSupported()) { | |
187 // Fall back to Windows Wave implementation on Windows XP or lower | |
188 // and assume 48kHz as default sample rate. | |
189 return kFallbackBufferSize; | |
190 } | |
191 | |
192 // TODO(crogers): tune this size to best possible WebAudio performance. | |
193 // WebRTC always uses 10ms for Windows and does not call this method. | |
194 // Note that exclusive mode is experimental. | |
195 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
196 if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio)) { | |
197 return 256; | |
198 } | |
199 | |
200 AudioParameters params; | |
201 HRESULT hr = CoreAudioUtil::GetPreferredAudioParameters(eRender, eConsole, | |
202 ¶ms); | |
203 return FAILED(hr) ? kFallbackBufferSize : params.frames_per_buffer(); | |
204 #elif defined(OS_LINUX) | |
205 return 512; | |
206 #else | |
207 return 2048; | |
208 #endif | |
209 } | |
210 | |
211 ChannelLayout GetAudioInputHardwareChannelLayout(const std::string& device_id) { | |
212 // TODO(henrika): add support for device selection on all platforms. | |
213 // Only exists on Windows today. | |
214 #if defined(OS_MACOSX) | |
215 return CHANNEL_LAYOUT_MONO; | |
216 #elif defined(OS_WIN) | |
217 if (!CoreAudioUtil::IsSupported()) { | |
218 // Fall back to Windows Wave implementation on Windows XP or lower and | |
219 // use stereo by default. | |
220 return CHANNEL_LAYOUT_STEREO; | |
221 } | |
222 return WASAPIAudioInputStream::HardwareChannelCount(device_id) == 1 ? | |
223 CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; | |
224 #else | |
225 return CHANNEL_LAYOUT_STEREO; | |
226 #endif | |
227 } | |
228 | |
229 // Computes a buffer size based on the given |sample_rate|. Must be used in | 111 // Computes a buffer size based on the given |sample_rate|. Must be used in |
230 // conjunction with AUDIO_PCM_LINEAR. | 112 // conjunction with AUDIO_PCM_LINEAR. |
231 size_t GetHighLatencyOutputBufferSize(int sample_rate) { | 113 size_t GetHighLatencyOutputBufferSize(int sample_rate) { |
232 int user_buffer_size = GetUserBufferSize(); | 114 int user_buffer_size = GetUserBufferSize(); |
233 if (user_buffer_size) | 115 if (user_buffer_size) |
234 return user_buffer_size; | 116 return user_buffer_size; |
235 | 117 |
236 // TODO(vrk/crogers): The buffer sizes that this function computes is probably | 118 // TODO(vrk/crogers): The buffer sizes that this function computes is probably |
237 // overly conservative. However, reducing the buffer size to 2048-8192 bytes | 119 // overly conservative. However, reducing the buffer size to 2048-8192 bytes |
238 // caused crbug.com/108396. This computation should be revisited while making | 120 // caused crbug.com/108396. This computation should be revisited while making |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 // out performance was degraded compared to XP. | 160 // out performance was degraded compared to XP. |
279 // - The regression was fixed in Windows 7 and most configurations will work | 161 // - The regression was fixed in Windows 7 and most configurations will work |
280 // with 2, but some (e.g., some Sound Blasters) still need 3. | 162 // with 2, but some (e.g., some Sound Blasters) still need 3. |
281 // - Some XP configurations (even multi-processor ones) also need 3. | 163 // - Some XP configurations (even multi-processor ones) also need 3. |
282 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; | 164 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; |
283 } | 165 } |
284 | 166 |
285 #endif | 167 #endif |
286 | 168 |
287 } // namespace media | 169 } // namespace media |
OLD | NEW |