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
3 // found in the LICENSE file.
5 #include "media/base/audio_hardware_config.h"
9 #include "base/logging.h"
10 #include "build/build_config.h"
13 using media::AudioParameters
;
17 #if defined(OS_LINUX) || defined(OS_MACOSX)
18 #define HIGH_LATENCY_AUDIO_SUPPORT 1
21 #if defined(HIGH_LATENCY_AUDIO_SUPPORT)
22 // Taken from "Bit Twiddling Hacks"
23 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
24 static uint32_t RoundUpToPowerOfTwo(uint32_t v
) {
36 AudioHardwareConfig::AudioHardwareConfig(
37 const AudioParameters
& input_params
,
38 const AudioParameters
& output_params
)
39 : input_params_(input_params
),
40 output_params_(output_params
) {}
42 AudioHardwareConfig::~AudioHardwareConfig() {}
44 int AudioHardwareConfig::GetOutputBufferSize() const {
45 AutoLock
auto_lock(config_lock_
);
46 return output_params_
.frames_per_buffer();
49 int AudioHardwareConfig::GetOutputSampleRate() const {
50 AutoLock
auto_lock(config_lock_
);
51 return output_params_
.sample_rate();
54 ChannelLayout
AudioHardwareConfig::GetOutputChannelLayout() const {
55 AutoLock
auto_lock(config_lock_
);
56 return output_params_
.channel_layout();
59 int AudioHardwareConfig::GetOutputChannels() const {
60 AutoLock
auto_lock(config_lock_
);
61 return output_params_
.channels();
64 int AudioHardwareConfig::GetInputSampleRate() const {
65 AutoLock
auto_lock(config_lock_
);
66 return input_params_
.sample_rate();
69 ChannelLayout
AudioHardwareConfig::GetInputChannelLayout() const {
70 AutoLock
auto_lock(config_lock_
);
71 return input_params_
.channel_layout();
74 int AudioHardwareConfig::GetInputChannels() const {
75 AutoLock
auto_lock(config_lock_
);
76 return input_params_
.channels();
79 media::AudioParameters
80 AudioHardwareConfig::GetInputConfig() const {
81 AutoLock
auto_lock(config_lock_
);
85 media::AudioParameters
86 AudioHardwareConfig::GetOutputConfig() const {
87 AutoLock
auto_lock(config_lock_
);
88 return output_params_
;
91 void AudioHardwareConfig::UpdateInputConfig(
92 const AudioParameters
& input_params
) {
93 AutoLock
auto_lock(config_lock_
);
94 input_params_
= input_params
;
97 void AudioHardwareConfig::UpdateOutputConfig(
98 const AudioParameters
& output_params
) {
99 AutoLock
auto_lock(config_lock_
);
100 output_params_
= output_params
;
103 int AudioHardwareConfig::GetHighLatencyBufferSize() const {
104 AutoLock
auto_lock(config_lock_
);
105 #if defined(HIGH_LATENCY_AUDIO_SUPPORT)
106 // Empirically, use the nearest higher power of two buffer size corresponding
107 // to 20 ms worth of samples. For a given sample rate, this works out to:
117 // 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
119 // dependent but will generally be sample_rate() / 100.
120 const int high_latency_buffer_size
=
121 RoundUpToPowerOfTwo(2 * output_params_
.sample_rate() / 100);
122 return std::max(output_params_
.frames_per_buffer(), high_latency_buffer_size
);
124 return output_params_
.frames_per_buffer();