Chromium Code Reviews| Index: media/audio/android/audio_manager_android.cc |
| =================================================================== |
| --- media/audio/android/audio_manager_android.cc (revision 200599) |
| +++ media/audio/android/audio_manager_android.cc (working copy) |
| @@ -22,6 +22,9 @@ |
| static const int kAudioModeNormal = 0x00000000; |
| static const int kAudioModeInCommunication = 0x00000003; |
| +static const int kDefaultInputBufferSize = 1024; |
| +static const int kDefaultOutputBufferSize = 2048; |
| + |
| AudioManager* CreateAudioManager() { |
| return new AudioManagerAndroid(); |
| } |
| @@ -56,13 +59,11 @@ |
| AudioParameters AudioManagerAndroid::GetInputStreamParameters( |
| const std::string& device_id) { |
| - // TODO(leozwang): Android defines the minimal buffer size requirment |
| - // we should use it. |
| - static const int kDefaultBufferSize = 1024; |
| - // TODO(xians): query the native channel layout for the specific device. |
| + int buffer_size = GetMinInputFrameSize(GetNativeOutputSampleRate(), 2); |
| return AudioParameters( |
| AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, |
| - GetNativeOutputSampleRate(), 16, kDefaultBufferSize); |
| + GetNativeOutputSampleRate(), 16, |
| + buffer_size <= 0 ? kDefaultInputBufferSize : buffer_size); |
| } |
| AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream( |
| @@ -119,15 +120,25 @@ |
| return new OpenSLESInputStream(this, params); |
| } |
| +int AudioManagerAndroid::GetOptimalOutputFrameSize(int sample_rate, |
| + int channels) { |
|
no longer working on chromium
2013/05/21 21:52:33
indentation.
|
| + if (IsAudioLowLatencySupported()) { |
| + int frame_size = GetAudioLowLatencyFrameSize(); |
| + // Return the optimal size as a multiple of the low latency frame |
| + // size that is close to the target frame size. |
| + return ((kDefaultOutputBufferSize + frame_size / 2) / frame_size) * |
| + frame_size; |
| + } else { |
| + return std::max(kDefaultOutputBufferSize, |
| + GetMinOutputFrameSize(sample_rate, channels)); |
|
no longer working on chromium
2013/05/21 21:52:33
why the output code is different from the way of i
leozwang1
2013/05/22 06:33:19
The return value of GetMinOutputFrameSize is small
|
| + } |
| +} |
| + |
| AudioParameters AudioManagerAndroid::GetPreferredOutputStreamParameters( |
| const AudioParameters& input_params) { |
| - // TODO(leozwang): Android defines the minimal buffer size requirment |
| - // we should use it. |
| - static const int kDefaultBufferSize = 2048; |
| - |
| ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; |
| int sample_rate = GetNativeOutputSampleRate(); |
| - int buffer_size = kDefaultBufferSize; |
| + int buffer_size = GetOptimalOutputFrameSize(sample_rate, 2); |
|
no longer working on chromium
2013/05/21 21:52:33
This is not correct. In case input_params.IsValid(
leozwang1
2013/05/22 06:33:19
I was puzzled by the original logic, from what you
no longer working on chromium
2013/05/22 21:00:00
The value we are setting here is frames_per_buffer
|
| int bits_per_sample = 16; |
| int input_channels = 0; |
| if (input_params.IsValid()) { |
| @@ -136,8 +147,6 @@ |
| bits_per_sample = input_params.bits_per_sample(); |
| channel_layout = input_params.channel_layout(); |
| input_channels = input_params.input_channels(); |
| - |
| - buffer_size = std::min(buffer_size, input_params.frames_per_buffer()); |
| } |
| int user_buffer_size = GetUserBufferSize(); |
| @@ -178,4 +187,31 @@ |
| j_audio_manager_.obj()); |
| } |
| +int AudioManagerAndroid::GetMinInputFrameSize(int rate, int channels) { |
| + // Java_AudioManagerAndroid_getMinInputBufSize return buffer size in bytes, |
| + // convert it to size in frame. |
| + return Java_AudioManagerAndroid_getMinInputBufSize( |
| + base::android::AttachCurrentThread(), rate, channels) / |
| + sizeof(int16) / channels; |
| +} |
| + |
| +int AudioManagerAndroid::GetMinOutputFrameSize(int rate, int channels) { |
| + // Get buffer size in frames, assuming 16-bit pcm samples. |
| + return Java_AudioManagerAndroid_getMinOutputBufSize( |
| + base::android::AttachCurrentThread(), rate, channels) / |
| + sizeof(int16) / channels; |
| +} |
| + |
| +bool AudioManagerAndroid::IsAudioLowLatencySupported() { |
| + return Java_AudioManagerAndroid_isAudioLowLatencySupported( |
| + base::android::AttachCurrentThread(), |
| + j_audio_manager_.obj()); |
| +} |
| + |
| +int AudioManagerAndroid::GetAudioLowLatencyFrameSize() { |
| + return Java_AudioManagerAndroid_getAudioLowLatencyFrameSize( |
| + base::android::AttachCurrentThread(), |
| + j_audio_manager_.obj()); |
| +} |
| + |
| } // namespace media |