OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/audio/android/audio_manager_android.h" | 5 #include "media/audio/android/audio_manager_android.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "jni/AudioManagerAndroid_jni.h" | 8 #include "jni/AudioManagerAndroid_jni.h" |
9 #include "media/audio/android/opensles_input.h" | 9 #include "media/audio/android/opensles_input.h" |
10 #include "media/audio/android/opensles_output.h" | 10 #include "media/audio/android/opensles_output.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 | 49 |
50 void AudioManagerAndroid::GetAudioInputDeviceNames( | 50 void AudioManagerAndroid::GetAudioInputDeviceNames( |
51 media::AudioDeviceNames* device_names) { | 51 media::AudioDeviceNames* device_names) { |
52 DCHECK(device_names->empty()); | 52 DCHECK(device_names->empty()); |
53 device_names->push_front( | 53 device_names->push_front( |
54 media::AudioDeviceName(kDefaultDeviceName, kDefaultDeviceId)); | 54 media::AudioDeviceName(kDefaultDeviceName, kDefaultDeviceId)); |
55 } | 55 } |
56 | 56 |
57 AudioParameters AudioManagerAndroid::GetInputStreamParameters( | 57 AudioParameters AudioManagerAndroid::GetInputStreamParameters( |
58 const std::string& device_id) { | 58 const std::string& device_id) { |
59 // TODO(leozwang): Android defines the minimal buffer size requirment | |
60 // we should use it. | |
61 static const int kDefaultBufferSize = 1024; | 59 static const int kDefaultBufferSize = 1024; |
62 // TODO(xians): query the native channel layout for the specific device. | 60 int size = GetMinInputFrameSize(GetNativeOutputSampleRate(), 2); |
no longer working on chromium
2013/05/20 09:59:26
nit, size -> buffer_size
leozwang1
2013/05/20 17:41:05
Done.
| |
63 return AudioParameters( | 61 return AudioParameters( |
64 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, | 62 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, |
65 GetNativeOutputSampleRate(), 16, kDefaultBufferSize); | 63 GetNativeOutputSampleRate(), 16, size <= 0 ? kDefaultBufferSize : size); |
66 } | 64 } |
67 | 65 |
68 AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream( | 66 AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream( |
69 const AudioParameters& params) { | 67 const AudioParameters& params) { |
70 AudioOutputStream* stream = | 68 AudioOutputStream* stream = |
71 AudioManagerBase::MakeAudioOutputStream(params); | 69 AudioManagerBase::MakeAudioOutputStream(params); |
72 if (stream && output_stream_count() == 1) | 70 if (stream && output_stream_count() == 1) |
73 RegisterHeadsetReceiver(); | 71 RegisterHeadsetReceiver(); |
74 return stream; | 72 return stream; |
75 } | 73 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 110 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
113 return new OpenSLESInputStream(this, params); | 111 return new OpenSLESInputStream(this, params); |
114 } | 112 } |
115 | 113 |
116 AudioInputStream* AudioManagerAndroid::MakeLowLatencyInputStream( | 114 AudioInputStream* AudioManagerAndroid::MakeLowLatencyInputStream( |
117 const AudioParameters& params, const std::string& device_id) { | 115 const AudioParameters& params, const std::string& device_id) { |
118 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); | 116 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
119 return new OpenSLESInputStream(this, params); | 117 return new OpenSLESInputStream(this, params); |
120 } | 118 } |
121 | 119 |
120 int AudioManagerAndroid::GetAudioOptimalOutputFrameSize(int sample_rate, | |
no longer working on chromium
2013/05/20 09:59:26
nit, s/GetAudioOptimalOutputFrameSize/GetOptimalOu
leozwang1
2013/05/20 17:41:05
Done.
| |
121 int channels) { | |
122 const int kTargetFrameSize = 2048; | |
no longer working on chromium
2013/05/20 09:59:26
kTargetFrameSize is the same as kDefaultBufferSize
leozwang1
2013/05/20 17:41:05
Done.
| |
123 if (IsAudioLowLatencySupported()) { | |
124 int frame_size = GetAudioLowLatencyFrameSize(); | |
125 // Return the optimal size as a multiple of the low latency frame | |
126 // size that is close to the target frame size. | |
127 return ((kTargetFrameSize + frame_size / 2) / frame_size) * frame_size; | |
no longer working on chromium
2013/05/20 09:59:26
I have to admit that I am not familiar with PROPER
leozwang1
2013/05/20 17:41:05
In order to achieve to "low latency", the buffer s
no longer working on chromium
2013/05/21 21:52:32
I understand that it needs to be a multiple of Get
Raymond Toy (Google)
2013/05/21 22:00:27
The low latency frame size varies quite a bit depe
| |
128 } else | |
no longer working on chromium
2013/05/20 09:59:26
nit, add { }
leozwang1
2013/05/20 17:41:05
Done.
| |
129 return std::max(kTargetFrameSize, | |
130 GetMinOutputFrameSize(sample_rate, channels)); | |
131 } | |
132 | |
122 AudioParameters AudioManagerAndroid::GetPreferredOutputStreamParameters( | 133 AudioParameters AudioManagerAndroid::GetPreferredOutputStreamParameters( |
123 const AudioParameters& input_params) { | 134 const AudioParameters& input_params) { |
124 // TODO(leozwang): Android defines the minimal buffer size requirment | |
125 // we should use it. | |
126 static const int kDefaultBufferSize = 2048; | 135 static const int kDefaultBufferSize = 2048; |
127 | |
128 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; | 136 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; |
129 int sample_rate = GetNativeOutputSampleRate(); | 137 int sample_rate = GetNativeOutputSampleRate(); |
130 int buffer_size = kDefaultBufferSize; | 138 int buffer_size = GetAudioOptimalOutputFrameSize(sample_rate, 2); |
131 int bits_per_sample = 16; | 139 int bits_per_sample = 16; |
132 int input_channels = 0; | 140 int input_channels = 0; |
133 if (input_params.IsValid()) { | 141 if (input_params.IsValid()) { |
134 // Use the client's input parameters if they are valid. | 142 // Use the client's input parameters if they are valid. |
135 sample_rate = input_params.sample_rate(); | 143 sample_rate = input_params.sample_rate(); |
136 bits_per_sample = input_params.bits_per_sample(); | 144 bits_per_sample = input_params.bits_per_sample(); |
137 channel_layout = input_params.channel_layout(); | 145 channel_layout = input_params.channel_layout(); |
138 input_channels = input_params.input_channels(); | 146 input_channels = input_params.input_channels(); |
139 | 147 buffer_size = buffer_size <= 0 ? kDefaultBufferSize : buffer_size; |
no longer working on chromium
2013/05/20 09:59:26
how buffer_size can be <= 0?
leozwang1
2013/05/20 17:41:05
good catch, it was latency code, removed it.
| |
140 buffer_size = std::min(buffer_size, input_params.frames_per_buffer()); | |
141 } | 148 } |
142 | 149 |
143 int user_buffer_size = GetUserBufferSize(); | 150 int user_buffer_size = GetUserBufferSize(); |
144 if (user_buffer_size) | 151 if (user_buffer_size) |
145 buffer_size = user_buffer_size; | 152 buffer_size = user_buffer_size; |
146 | 153 |
147 return AudioParameters( | 154 return AudioParameters( |
148 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels, | 155 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels, |
149 sample_rate, bits_per_sample, buffer_size); | 156 sample_rate, bits_per_sample, buffer_size); |
150 } | 157 } |
(...skipping 14 matching lines...) Expand all Loading... | |
165 base::android::AttachCurrentThread(), | 172 base::android::AttachCurrentThread(), |
166 j_audio_manager_.obj()); | 173 j_audio_manager_.obj()); |
167 } | 174 } |
168 | 175 |
169 void AudioManagerAndroid::UnregisterHeadsetReceiver() { | 176 void AudioManagerAndroid::UnregisterHeadsetReceiver() { |
170 Java_AudioManagerAndroid_unregisterHeadsetReceiver( | 177 Java_AudioManagerAndroid_unregisterHeadsetReceiver( |
171 base::android::AttachCurrentThread(), | 178 base::android::AttachCurrentThread(), |
172 j_audio_manager_.obj()); | 179 j_audio_manager_.obj()); |
173 } | 180 } |
174 | 181 |
175 int AudioManagerAndroid::GetNativeOutputSampleRate() { | 182 int AudioManagerAndroid::GetNativeOutputSampleRate() { |
no longer working on chromium
2013/05/20 09:59:26
Does GetNativeOutputSampleRate() get the native sa
leozwang1
2013/05/20 17:41:05
Then we could also cache all return value from Get
| |
176 return Java_AudioManagerAndroid_getNativeOutputSampleRate( | 183 return Java_AudioManagerAndroid_getNativeOutputSampleRate( |
177 base::android::AttachCurrentThread(), | 184 base::android::AttachCurrentThread(), |
178 j_audio_manager_.obj()); | 185 j_audio_manager_.obj()); |
179 } | 186 } |
180 | 187 |
188 int AudioManagerAndroid::GetMinInputFrameSize(int rate, int channels) { | |
no longer working on chromium
2013/05/20 09:59:26
could you please add comments here, to explain why
leozwang1
2013/05/20 17:41:05
Done.
| |
189 return Java_AudioManagerAndroid_getMinInputBufSize( | |
190 base::android::AttachCurrentThread(), rate, channels) / 2 / channels; | |
no longer working on chromium
2013/05/20 09:59:26
nit, use sizeof(int16) instead of 2
leozwang1
2013/05/20 17:41:05
Done.
| |
191 } | |
192 | |
193 int AudioManagerAndroid::GetMinOutputFrameSize(int rate, int channels) { | |
194 // Get buffer size in frames, assuming 16-bit pcm samples. | |
195 return Java_AudioManagerAndroid_getMinOutputBufSize( | |
196 base::android::AttachCurrentThread(), rate, channels) / 2 / channels; | |
197 } | |
198 | |
199 bool AudioManagerAndroid::IsAudioLowLatencySupported() { | |
no longer working on chromium
2013/05/20 09:59:26
I don't think you need a separate IsAudioLowLatenc
leozwang1
2013/05/20 17:41:05
Currently it's called once, but it's just wrapper
| |
200 return Java_AudioManagerAndroid_isAudioLowLatencySupported( | |
201 base::android::AttachCurrentThread(), | |
202 j_audio_manager_.obj()); | |
203 } | |
204 | |
205 int AudioManagerAndroid::GetAudioLowLatencyFrameSize() { | |
206 return Java_AudioManagerAndroid_getAudioLowLatencyFrameSize( | |
207 base::android::AttachCurrentThread(), | |
208 j_audio_manager_.obj()); | |
209 } | |
210 | |
181 } // namespace media | 211 } // namespace media |
OLD | NEW |