summaryrefslogtreecommitdiffstats
path: root/chromium/media/base/audio_hardware_config.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/media/base/audio_hardware_config.cc')
-rw-r--r--chromium/media/base/audio_hardware_config.cc56
1 files changed, 54 insertions, 2 deletions
diff --git a/chromium/media/base/audio_hardware_config.cc b/chromium/media/base/audio_hardware_config.cc
index d72fce7b4e2..d00e03f6b08 100644
--- a/chromium/media/base/audio_hardware_config.cc
+++ b/chromium/media/base/audio_hardware_config.cc
@@ -4,17 +4,37 @@
#include "media/base/audio_hardware_config.h"
+#include <algorithm>
+#include <cmath>
+
+#include "base/logging.h"
+#include "build/build_config.h"
+
using base::AutoLock;
using media::AudioParameters;
namespace media {
+#if !defined(OS_WIN)
+// Taken from "Bit Twiddling Hacks"
+// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
+static uint32_t RoundUpToPowerOfTwo(uint32_t v) {
+ v--;
+ v |= v >> 1;
+ v |= v >> 2;
+ v |= v >> 4;
+ v |= v >> 8;
+ v |= v >> 16;
+ v++;
+ return v;
+}
+#endif
+
AudioHardwareConfig::AudioHardwareConfig(
const AudioParameters& input_params,
const AudioParameters& output_params)
: input_params_(input_params),
- output_params_(output_params) {
-}
+ output_params_(output_params) {}
AudioHardwareConfig::~AudioHardwareConfig() {}
@@ -77,4 +97,36 @@ void AudioHardwareConfig::UpdateOutputConfig(
output_params_ = output_params;
}
+int AudioHardwareConfig::GetHighLatencyBufferSize() const {
+ AutoLock auto_lock(config_lock_);
+
+ // Empirically, we consider 20ms of samples to be high latency.
+ const double twenty_ms_size = 2.0 * output_params_.sample_rate() / 100;
+
+#if defined(OS_WIN)
+ // Windows doesn't use power of two buffer sizes, so we should always round up
+ // to the nearest multiple of the output buffer size.
+ const int high_latency_buffer_size =
+ std::ceil(twenty_ms_size / output_params_.frames_per_buffer()) *
+ output_params_.frames_per_buffer();
+#else
+ // On other platforms use the nearest higher power of two buffer size. For a
+ // given sample rate, this works out to:
+ //
+ // <= 3200 : 64
+ // <= 6400 : 128
+ // <= 12800 : 256
+ // <= 25600 : 512
+ // <= 51200 : 1024
+ // <= 102400 : 2048
+ // <= 204800 : 4096
+ //
+ // On Linux, the minimum hardware buffer size is 512, so the lower calculated
+ // values are unused. OSX may have a value as low as 128.
+ const int high_latency_buffer_size = RoundUpToPowerOfTwo(twenty_ms_size);
+#endif // defined(OS_WIN)
+
+ return std::max(output_params_.frames_per_buffer(), high_latency_buffer_size);
+}
+
} // namespace media