Index: media/audio/android/audio_manager_android.cc |
=================================================================== |
--- media/audio/android/audio_manager_android.cc (revision 200309) |
+++ media/audio/android/audio_manager_android.cc (working copy) |
@@ -56,14 +56,11 @@ |
AudioParameters AudioManagerAndroid::GetInputStreamParameters( |
const std::string& device_id) { |
- // TODO(xians): figure out the right input sample rate and buffer size to |
- // achieve the best audio performance for Android devices. |
- // TODO(xians): query the native channel layout for the specific device. |
- static const int kDefaultSampleRate = 16000; |
static const int kDefaultBufferSize = 1024; |
+ int size = GetMinInputBufferSize(GetNativeOutputSampleRate(), 2); |
return AudioParameters( |
AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, |
- kDefaultSampleRate, 16, kDefaultBufferSize); |
+ GetNativeOutputSampleRate(), 16, size <= 0 ? kDefaultBufferSize : size); |
} |
AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream( |
@@ -120,16 +117,24 @@ |
return new OpenSLESInputStream(this, params); |
} |
+int AudioManagerAndroid::GetAudioOptimalOutputFrameSize(int sample_rate, |
+ int channels) { |
+ if (IsAudioLowLatencySupported()) { |
+ const int kTargetFrameSize = 2048; |
+ 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 ((kTargetFrameSize + frame_size / 2) / frame_size) * frame_size; |
+ } else |
+ return GetMinOutputBufferSize(sample_rate, channels); |
Raymond Toy (Google)
2013/05/16 04:32:28
I wonder if we should return the max of GetMinOutp
leozwang2
2013/05/16 20:56:18
All devices I have tested have "LowLatency", this
|
+} |
+ |
AudioParameters AudioManagerAndroid::GetPreferredOutputStreamParameters( |
const AudioParameters& input_params) { |
- // TODO(xians): figure out the right output sample rate and buffer size to |
- // achieve the best audio performance for Android devices. |
- static const int kDefaultSampleRate = 44100; |
static const int kDefaultBufferSize = 2048; |
- |
ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; |
- int sample_rate = kDefaultSampleRate; |
- int buffer_size = kDefaultBufferSize; |
+ int sample_rate = GetNativeOutputSampleRate(); |
+ int buffer_size = GetAudioOptimalOutputFrameSize(sample_rate, 2); |
int bits_per_sample = 16; |
int input_channels = 0; |
if (input_params.IsValid()) { |
@@ -138,12 +143,7 @@ |
bits_per_sample = input_params.bits_per_sample(); |
channel_layout = input_params.channel_layout(); |
input_channels = input_params.input_channels(); |
- |
- // TODO(leozwang): Android defines the minimal buffer size requirment |
- // we should follow it. From Android 4.1, a new audio low latency api |
- // set was introduced and is under development, we want to take advantage |
- // of it. |
- buffer_size = std::min(buffer_size, input_params.frames_per_buffer()); |
+ buffer_size = buffer_size <= 0 ? kDefaultBufferSize : buffer_size; |
} |
int user_buffer_size = GetUserBufferSize(); |
@@ -178,4 +178,35 @@ |
j_audio_manager_.obj()); |
} |
+int AudioManagerAndroid::GetNativeOutputSampleRate() { |
+ return Java_AudioManagerAndroid_getNativeOutputSampleRate( |
+ base::android::AttachCurrentThread(), |
+ j_audio_manager_.obj()); |
+} |
+ |
+int AudioManagerAndroid::GetMinInputBufferSize(int rate, int channels) { |
+ return Java_AudioManagerAndroid_getMinInputBufSize( |
+ base::android::AttachCurrentThread(), |
+ rate, channels); |
+} |
+ |
+int AudioManagerAndroid::GetMinOutputBufferSize(int rate, int channels) { |
+ int min_buffer_size_bytes = Java_AudioManagerAndroid_getMinOutputBufSize( |
+ base::android::AttachCurrentThread(), rate, channels); |
+ // Get buffer size in frames, assuming 16-bit pcm samples. |
+ return min_buffer_size_bytes / channels / 2; |
+} |
+ |
+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 |