Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / base / audio_hardware_config.cc
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.
4
5 #include "media/base/audio_hardware_config.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "build/build_config.h"
11
12 using base::AutoLock;
13 using media::AudioParameters;
14
15 namespace media {
16
17 #if defined(OS_LINUX) || defined(OS_MACOSX)
18 #define HIGH_LATENCY_AUDIO_SUPPORT 1
19 #endif
20
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) {
25   v--;
26   v |= v >> 1;
27   v |= v >> 2;
28   v |= v >> 4;
29   v |= v >> 8;
30   v |= v >> 16;
31   v++;
32   return v;
33 }
34 #endif
35
36 AudioHardwareConfig::AudioHardwareConfig(
37     const AudioParameters& input_params,
38     const AudioParameters& output_params)
39     : input_params_(input_params),
40       output_params_(output_params) {}
41
42 AudioHardwareConfig::~AudioHardwareConfig() {}
43
44 int AudioHardwareConfig::GetOutputBufferSize() const {
45   AutoLock auto_lock(config_lock_);
46   return output_params_.frames_per_buffer();
47 }
48
49 int AudioHardwareConfig::GetOutputSampleRate() const {
50   AutoLock auto_lock(config_lock_);
51   return output_params_.sample_rate();
52 }
53
54 ChannelLayout AudioHardwareConfig::GetOutputChannelLayout() const {
55   AutoLock auto_lock(config_lock_);
56   return output_params_.channel_layout();
57 }
58
59 int AudioHardwareConfig::GetOutputChannels() const {
60   AutoLock auto_lock(config_lock_);
61   return output_params_.channels();
62 }
63
64 int AudioHardwareConfig::GetInputSampleRate() const {
65   AutoLock auto_lock(config_lock_);
66   return input_params_.sample_rate();
67 }
68
69 ChannelLayout AudioHardwareConfig::GetInputChannelLayout() const {
70   AutoLock auto_lock(config_lock_);
71   return input_params_.channel_layout();
72 }
73
74 int AudioHardwareConfig::GetInputChannels() const {
75   AutoLock auto_lock(config_lock_);
76   return input_params_.channels();
77 }
78
79 media::AudioParameters
80 AudioHardwareConfig::GetInputConfig() const {
81   AutoLock auto_lock(config_lock_);
82   return input_params_;
83 }
84
85 media::AudioParameters
86 AudioHardwareConfig::GetOutputConfig() const {
87   AutoLock auto_lock(config_lock_);
88   return output_params_;
89 }
90
91 void AudioHardwareConfig::UpdateInputConfig(
92     const AudioParameters& input_params) {
93   AutoLock auto_lock(config_lock_);
94   input_params_ = input_params;
95 }
96
97 void AudioHardwareConfig::UpdateOutputConfig(
98     const AudioParameters& output_params) {
99   AutoLock auto_lock(config_lock_);
100   output_params_ = output_params;
101 }
102
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:
108   //
109   //     <= 3200   : 64
110   //     <= 6400   : 128
111   //     <= 12800  : 256
112   //     <= 25600  : 512
113   //     <= 51200  : 1024
114   //     <= 102400 : 2048
115   //     <= 204800 : 4096
116   //
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);
123 #else
124   return output_params_.frames_per_buffer();
125 #endif
126 }
127
128 }  // namespace media