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,8 @@ |
| static const int kAudioModeNormal = 0x00000000; |
| static const int kAudioModeInCommunication = 0x00000003; |
| +static const int kDefaultBufferSize = 2048; |
| + |
| AudioManager* CreateAudioManager() { |
| return new AudioManagerAndroid(); |
| } |
| @@ -56,13 +58,12 @@ |
| 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; |
|
Raymond Toy (Google)
2013/05/21 16:30:38
This is kind of confusing since it has the same na
leozwang1
2013/05/21 17:03:31
Done.
|
| - // 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 ? kDefaultBufferSize : buffer_size); |
| } |
| AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream( |
| @@ -119,15 +120,25 @@ |
| return new OpenSLESInputStream(this, params); |
| } |
| +int AudioManagerAndroid::GetOptimalOutputFrameSize(int sample_rate, |
| + int channels) { |
| + const int kDefaultBufferSize = 2048; |
|
Raymond Toy (Google)
2013/05/21 16:30:38
Shouldn't this be removed now?
leozwang1
2013/05/21 17:03:31
Done.
leozwang1
2013/05/21 17:03:31
Done.
|
| + 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 ((kDefaultBufferSize + frame_size / 2) / frame_size) * frame_size; |
| + } else { |
| + return std::max(kDefaultBufferSize, |
| + GetMinOutputFrameSize(sample_rate, channels)); |
| + } |
| +} |
| + |
| 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); |
| 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 |