OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "media/base/audio_hardware_config.h" | 5 #include "media/base/audio_hardware_config.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> |
8 | 9 |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
11 | 12 |
12 using base::AutoLock; | 13 using base::AutoLock; |
13 using media::AudioParameters; | 14 using media::AudioParameters; |
14 | 15 |
15 namespace media { | 16 namespace media { |
16 | 17 |
17 #if defined(OS_LINUX) || defined(OS_MACOSX) | 18 #if !defined(OS_WIN) |
18 #define HIGH_LATENCY_AUDIO_SUPPORT 1 | |
19 #endif | |
20 | |
21 #if defined(HIGH_LATENCY_AUDIO_SUPPORT) | |
22 // Taken from "Bit Twiddling Hacks" | 19 // Taken from "Bit Twiddling Hacks" |
23 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 | 20 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 |
24 static uint32_t RoundUpToPowerOfTwo(uint32_t v) { | 21 static uint32_t RoundUpToPowerOfTwo(uint32_t v) { |
25 v--; | 22 v--; |
26 v |= v >> 1; | 23 v |= v >> 1; |
27 v |= v >> 2; | 24 v |= v >> 2; |
28 v |= v >> 4; | 25 v |= v >> 4; |
29 v |= v >> 8; | 26 v |= v >> 8; |
30 v |= v >> 16; | 27 v |= v >> 16; |
31 v++; | 28 v++; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 } | 92 } |
96 | 93 |
97 void AudioHardwareConfig::UpdateOutputConfig( | 94 void AudioHardwareConfig::UpdateOutputConfig( |
98 const AudioParameters& output_params) { | 95 const AudioParameters& output_params) { |
99 AutoLock auto_lock(config_lock_); | 96 AutoLock auto_lock(config_lock_); |
100 output_params_ = output_params; | 97 output_params_ = output_params; |
101 } | 98 } |
102 | 99 |
103 int AudioHardwareConfig::GetHighLatencyBufferSize() const { | 100 int AudioHardwareConfig::GetHighLatencyBufferSize() const { |
104 AutoLock auto_lock(config_lock_); | 101 AutoLock auto_lock(config_lock_); |
105 #if defined(HIGH_LATENCY_AUDIO_SUPPORT) | 102 |
106 // Empirically, use the nearest higher power of two buffer size corresponding | 103 // Empirically, we consider 20ms of samples to be high latency. |
107 // to 20 ms worth of samples. For a given sample rate, this works out to: | 104 const double twenty_ms_size = 2.0 * output_params_.sample_rate() / 100; |
| 105 |
| 106 #if defined(OS_WIN) |
| 107 // Windows doesn't use power of two buffer sizes, so we should always round up |
| 108 // to the nearest multiple of the output buffer size. |
| 109 const int high_latency_buffer_size = |
| 110 std::ceil(twenty_ms_size / output_params_.frames_per_buffer()) * |
| 111 output_params_.frames_per_buffer(); |
| 112 #else |
| 113 // On other platforms use the nearest higher power of two buffer size. For a |
| 114 // given sample rate, this works out to: |
108 // | 115 // |
109 // <= 3200 : 64 | 116 // <= 3200 : 64 |
110 // <= 6400 : 128 | 117 // <= 6400 : 128 |
111 // <= 12800 : 256 | 118 // <= 12800 : 256 |
112 // <= 25600 : 512 | 119 // <= 25600 : 512 |
113 // <= 51200 : 1024 | 120 // <= 51200 : 1024 |
114 // <= 102400 : 2048 | 121 // <= 102400 : 2048 |
115 // <= 204800 : 4096 | 122 // <= 204800 : 4096 |
116 // | 123 // |
117 // On Linux, the minimum hardware buffer size is 512, so the lower calculated | 124 // On Linux, the minimum hardware buffer size is 512, so the lower calculated |
118 // values are unused. OSX may have a value as low as 128. Windows is device | 125 // values are unused. OSX may have a value as low as 128. |
119 // dependent but will generally be sample_rate() / 100. | 126 const int high_latency_buffer_size = RoundUpToPowerOfTwo(twenty_ms_size); |
120 const int high_latency_buffer_size = | 127 #endif // defined(OS_WIN) |
121 RoundUpToPowerOfTwo(2 * output_params_.sample_rate() / 100); | 128 |
122 return std::max(output_params_.frames_per_buffer(), high_latency_buffer_size); | 129 return std::max(output_params_.frames_per_buffer(), high_latency_buffer_size); |
123 #else | |
124 return output_params_.frames_per_buffer(); | |
125 #endif | |
126 } | 130 } |
127 | 131 |
128 } // namespace media | 132 } // namespace media |
OLD | NEW |