| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 | 14 |
| 15 using base::AutoLock; | 15 using base::AutoLock; |
| 16 using media::AudioParameters; | 16 using media::AudioParameters; |
| 17 | 17 |
| 18 namespace media { | 18 namespace media { |
| 19 | 19 |
| 20 #if !defined(OS_WIN) | |
| 21 // Taken from "Bit Twiddling Hacks" | |
| 22 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 | |
| 23 static uint32_t RoundUpToPowerOfTwo(uint32_t v) { | |
| 24 v--; | |
| 25 v |= v >> 1; | |
| 26 v |= v >> 2; | |
| 27 v |= v >> 4; | |
| 28 v |= v >> 8; | |
| 29 v |= v >> 16; | |
| 30 v++; | |
| 31 return v; | |
| 32 } | |
| 33 #endif | |
| 34 | |
| 35 AudioHardwareConfig::AudioHardwareConfig( | 20 AudioHardwareConfig::AudioHardwareConfig( |
| 36 const AudioParameters& input_params, | 21 const AudioParameters& input_params, |
| 37 const AudioParameters& output_params) | 22 const AudioParameters& output_params) |
| 38 : input_params_(input_params), | 23 : input_params_(input_params), |
| 39 output_params_(output_params) {} | 24 output_params_(output_params) {} |
| 40 | 25 |
| 41 AudioHardwareConfig::~AudioHardwareConfig() {} | 26 AudioHardwareConfig::~AudioHardwareConfig() {} |
| 42 | 27 |
| 43 int AudioHardwareConfig::GetOutputBufferSize() const { | 28 int AudioHardwareConfig::GetOutputBufferSize() const { |
| 44 AutoLock auto_lock(config_lock_); | 29 AutoLock auto_lock(config_lock_); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 AutoLock auto_lock(config_lock_); | 77 AutoLock auto_lock(config_lock_); |
| 93 input_params_ = input_params; | 78 input_params_ = input_params; |
| 94 } | 79 } |
| 95 | 80 |
| 96 void AudioHardwareConfig::UpdateOutputConfig( | 81 void AudioHardwareConfig::UpdateOutputConfig( |
| 97 const AudioParameters& output_params) { | 82 const AudioParameters& output_params) { |
| 98 AutoLock auto_lock(config_lock_); | 83 AutoLock auto_lock(config_lock_); |
| 99 output_params_ = output_params; | 84 output_params_ = output_params; |
| 100 } | 85 } |
| 101 | 86 |
| 102 // static | |
| 103 int AudioHardwareConfig::GetHighLatencyBufferSize(int sample_rate, | |
| 104 int buffer_size) { | |
| 105 // Empirically, we consider 20ms of samples to be high latency. | |
| 106 const double twenty_ms_size = 2.0 * sample_rate / 100; | |
| 107 | |
| 108 #if defined(OS_WIN) | |
| 109 buffer_size = std::max(buffer_size, 1); | |
| 110 | |
| 111 // Windows doesn't use power of two buffer sizes, so we should always round up | |
| 112 // to the nearest multiple of the output buffer size. | |
| 113 const int high_latency_buffer_size = | |
| 114 std::ceil(twenty_ms_size / buffer_size) * buffer_size; | |
| 115 #else | |
| 116 // On other platforms use the nearest higher power of two buffer size. For a | |
| 117 // given sample rate, this works out to: | |
| 118 // | |
| 119 // <= 3200 : 64 | |
| 120 // <= 6400 : 128 | |
| 121 // <= 12800 : 256 | |
| 122 // <= 25600 : 512 | |
| 123 // <= 51200 : 1024 | |
| 124 // <= 102400 : 2048 | |
| 125 // <= 204800 : 4096 | |
| 126 // | |
| 127 // On Linux, the minimum hardware buffer size is 512, so the lower calculated | |
| 128 // values are unused. OSX may have a value as low as 128. | |
| 129 const int high_latency_buffer_size = RoundUpToPowerOfTwo(twenty_ms_size); | |
| 130 #endif // defined(OS_WIN) | |
| 131 | |
| 132 return std::max(buffer_size, high_latency_buffer_size); | |
| 133 } | |
| 134 | |
| 135 int AudioHardwareConfig::GetHighLatencyBufferSize() const { | |
| 136 AutoLock auto_lock(config_lock_); | |
| 137 return GetHighLatencyBufferSize(output_params_.sample_rate(), | |
| 138 output_params_.frames_per_buffer()); | |
| 139 } | |
| 140 | |
| 141 } // namespace media | 87 } // namespace media |
| OLD | NEW |