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 |
11 // TODO(vrk): This file has been running pretty wild and free, and it's likely | 11 // TODO(vrk): This file has been running pretty wild and free, and it's likely |
12 // that a lot of the functions can be simplified and made more elegant. Revisit | 12 // that a lot of the functions can be simplified and made more elegant. Revisit |
13 // after other audio cleanup is done. (crbug.com/120319) | 13 // after other audio cleanup is done. (crbug.com/120319) |
14 | 14 |
15 #include "media/audio/audio_util.h" | 15 #include "media/audio/audio_util.h" |
16 | 16 |
17 #include <algorithm> | 17 #include <algorithm> |
18 #include <limits> | 18 #include <limits> |
19 | 19 |
20 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
21 #include "base/logging.h" | 21 #include "base/logging.h" |
22 #include "base/string_number_conversions.h" | |
22 #include "base/time.h" | 23 #include "base/time.h" |
23 #include "media/audio/audio_parameters.h" | 24 #include "media/audio/audio_parameters.h" |
24 #include "media/base/audio_bus.h" | 25 #include "media/base/audio_bus.h" |
25 | 26 |
26 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
27 #include "media/audio/mac/audio_low_latency_input_mac.h" | 28 #include "media/audio/mac/audio_low_latency_input_mac.h" |
28 #include "media/audio/mac/audio_low_latency_output_mac.h" | 29 #include "media/audio/mac/audio_low_latency_output_mac.h" |
29 #elif defined(OS_WIN) | 30 #elif defined(OS_WIN) |
30 #include "base/command_line.h" | 31 #include "base/command_line.h" |
31 #include "base/win/windows_version.h" | 32 #include "base/win/windows_version.h" |
32 #include "media/audio/audio_manager_base.h" | 33 #include "media/audio/audio_manager_base.h" |
33 #include "media/audio/win/audio_low_latency_input_win.h" | 34 #include "media/audio/win/audio_low_latency_input_win.h" |
34 #include "media/audio/win/audio_low_latency_output_win.h" | 35 #include "media/audio/win/audio_low_latency_output_win.h" |
35 #include "media/audio/win/audio_unified_win.h" | 36 #include "media/audio/win/audio_unified_win.h" |
36 #include "media/base/limits.h" | 37 #include "media/base/limits.h" |
37 #include "media/base/media_switches.h" | 38 #include "media/base/media_switches.h" |
38 #endif | 39 #endif |
39 | 40 |
40 namespace media { | 41 namespace media { |
41 | 42 |
43 // Returns user buffer size as specified on the command line or 0 if no buffer | |
44 // size has been specified. | |
45 static int GetUserBufferSize() { | |
46 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
47 int buffer_size = 0; | |
48 std::string buffer_size_str(cmd_line->GetSwitchValueASCII( | |
49 switches::kAudioBufferSize)); | |
50 if (!buffer_size_str.empty() && | |
scherkus (not reviewing)
2012/10/29 18:23:02
nit: base::StringToInt() will return 0 on empty st
DaleCurtis
2012/10/30 21:52:52
Done.
| |
51 base::StringToInt(buffer_size_str, &buffer_size) && buffer_size > 0) { | |
52 return buffer_size; | |
53 } | |
54 | |
55 return 0; | |
56 } | |
57 | |
42 // TODO(fbarchard): Convert to intrinsics for better efficiency. | 58 // TODO(fbarchard): Convert to intrinsics for better efficiency. |
43 template<class Fixed> | 59 template<class Fixed> |
44 static int ScaleChannel(int channel, int volume) { | 60 static int ScaleChannel(int channel, int volume) { |
45 return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16); | 61 return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16); |
46 } | 62 } |
47 | 63 |
48 template<class Format, class Fixed, int bias> | 64 template<class Format, class Fixed, int bias> |
49 static void AdjustVolume(Format* buf_out, | 65 static void AdjustVolume(Format* buf_out, |
50 int sample_count, | 66 int sample_count, |
51 int fixed_volume) { | 67 int fixed_volume) { |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 } | 234 } |
219 return WASAPIAudioInputStream::HardwareSampleRate(device_id); | 235 return WASAPIAudioInputStream::HardwareSampleRate(device_id); |
220 #elif defined(OS_ANDROID) | 236 #elif defined(OS_ANDROID) |
221 return 16000; | 237 return 16000; |
222 #else | 238 #else |
223 return 48000; | 239 return 48000; |
224 #endif | 240 #endif |
225 } | 241 } |
226 | 242 |
227 size_t GetAudioHardwareBufferSize() { | 243 size_t GetAudioHardwareBufferSize() { |
244 int user_buffer_size = GetUserBufferSize(); | |
245 if (user_buffer_size) | |
246 return user_buffer_size; | |
247 | |
228 // The sizes here were determined by experimentation and are roughly | 248 // The sizes here were determined by experimentation and are roughly |
229 // the lowest value (for low latency) that still allowed glitch-free | 249 // the lowest value (for low latency) that still allowed glitch-free |
230 // audio under high loads. | 250 // audio under high loads. |
231 // | 251 // |
232 // For Mac OS X and Windows the chromium audio backend uses a low-latency | 252 // For Mac OS X and Windows the chromium audio backend uses a low-latency |
233 // Core Audio API, so a low buffer size is possible. For Linux, further | 253 // Core Audio API, so a low buffer size is possible. For Linux, further |
234 // tuning may be needed. | 254 // tuning may be needed. |
235 #if defined(OS_MACOSX) | 255 #if defined(OS_MACOSX) |
236 return 128; | 256 return 128; |
237 #elif defined(OS_WIN) | 257 #elif defined(OS_WIN) |
238 // Buffer size to use when a proper size can't be determined from the system. | 258 // Buffer size to use when a proper size can't be determined from the system. |
239 static const int kFallbackBufferSize = 2048; | 259 static const int kFallbackBufferSize = 4096; |
scherkus (not reviewing)
2012/10/29 18:23:02
henrika: this is the buffer size change
henrika (OOO until Aug 14)
2012/10/29 18:50:33
Got it. Thanks.
| |
240 | 260 |
241 if (!IsWASAPISupported()) { | 261 if (!IsWASAPISupported()) { |
242 // Fall back to Windows Wave implementation on Windows XP or lower | 262 // Fall back to Windows Wave implementation on Windows XP or lower |
243 // and assume 48kHz as default sample rate. | 263 // and assume 48kHz as default sample rate. |
244 return kFallbackBufferSize; | 264 return kFallbackBufferSize; |
245 } | 265 } |
246 | 266 |
247 // TODO(crogers): tune this size to best possible WebAudio performance. | 267 // TODO(crogers): tune this size to best possible WebAudio performance. |
248 // WebRTC always uses 10ms for Windows and does not call this method. | 268 // WebRTC always uses 10ms for Windows and does not call this method. |
249 // Note that exclusive mode is experimental. | 269 // Note that exclusive mode is experimental. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 return WASAPIAudioInputStream::HardwareChannelCount(device_id) == 1 ? | 327 return WASAPIAudioInputStream::HardwareChannelCount(device_id) == 1 ? |
308 CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; | 328 CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; |
309 #else | 329 #else |
310 return CHANNEL_LAYOUT_STEREO; | 330 return CHANNEL_LAYOUT_STEREO; |
311 #endif | 331 #endif |
312 } | 332 } |
313 | 333 |
314 // Computes a buffer size based on the given |sample_rate|. Must be used in | 334 // Computes a buffer size based on the given |sample_rate|. Must be used in |
315 // conjunction with AUDIO_PCM_LINEAR. | 335 // conjunction with AUDIO_PCM_LINEAR. |
316 size_t GetHighLatencyOutputBufferSize(int sample_rate) { | 336 size_t GetHighLatencyOutputBufferSize(int sample_rate) { |
337 int user_buffer_size = GetUserBufferSize(); | |
338 if (user_buffer_size) | |
339 return user_buffer_size; | |
340 | |
317 // TODO(vrk/crogers): The buffer sizes that this function computes is probably | 341 // TODO(vrk/crogers): The buffer sizes that this function computes is probably |
318 // overly conservative. However, reducing the buffer size to 2048-8192 bytes | 342 // overly conservative. However, reducing the buffer size to 2048-8192 bytes |
319 // caused crbug.com/108396. This computation should be revisited while making | 343 // caused crbug.com/108396. This computation should be revisited while making |
320 // sure crbug.com/108396 doesn't happen again. | 344 // sure crbug.com/108396 doesn't happen again. |
321 | 345 |
322 // The minimum number of samples in a hardware packet. | 346 // The minimum number of samples in a hardware packet. |
323 // This value is selected so that we can handle down to 5khz sample rate. | 347 // This value is selected so that we can handle down to 5khz sample rate. |
324 static const size_t kMinSamplesPerHardwarePacket = 1024; | 348 static const size_t kMinSamplesPerHardwarePacket = 1024; |
325 | 349 |
326 // The maximum number of samples in a hardware packet. | 350 // The maximum number of samples in a hardware packet. |
(...skipping 30 matching lines...) Expand all Loading... | |
357 // out performance was degraded compared to XP. | 381 // out performance was degraded compared to XP. |
358 // - The regression was fixed in Windows 7 and most configurations will work | 382 // - The regression was fixed in Windows 7 and most configurations will work |
359 // with 2, but some (e.g., some Sound Blasters) still need 3. | 383 // with 2, but some (e.g., some Sound Blasters) still need 3. |
360 // - Some XP configurations (even multi-processor ones) also need 3. | 384 // - Some XP configurations (even multi-processor ones) also need 3. |
361 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; | 385 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; |
362 } | 386 } |
363 | 387 |
364 #endif | 388 #endif |
365 | 389 |
366 } // namespace media | 390 } // namespace media |
OLD | NEW |