From e49f2b424318aa8e830e7a1338e5e32ab82992f9 Mon Sep 17 00:00:00 2001 From: Eric Laurent Date: Tue, 12 Jun 2012 17:09:30 -0700 Subject: stagefright: fix AudioRecord callback buffer size Make sure that the maximum number of frames passed to AudioSource by the AudioRecord callback always fits within the maximum buffer size defined by kMaxBufferSize. Also make sure that the total AudioRecord buffer size is more than the minimum required. Change-Id: I26a1f998e0cf75ac88b02e67ec9d8db3c0cca193 --- media/libstagefright/AudioSource.cpp | 46 ++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 13 deletions(-) (limited to 'media') diff --git a/media/libstagefright/AudioSource.cpp b/media/libstagefright/AudioSource.cpp index 0f1d841..72d797e 100644 --- a/media/libstagefright/AudioSource.cpp +++ b/media/libstagefright/AudioSource.cpp @@ -56,19 +56,39 @@ AudioSource::AudioSource( ALOGV("sampleRate: %d, channelCount: %d", sampleRate, channelCount); CHECK(channelCount == 1 || channelCount == 2); - AudioRecord::record_flags flags = (AudioRecord::record_flags) - (AudioRecord::RECORD_AGC_ENABLE | - AudioRecord::RECORD_NS_ENABLE | - AudioRecord::RECORD_IIR_ENABLE); - mRecord = new AudioRecord( - inputSource, sampleRate, AUDIO_FORMAT_PCM_16_BIT, - audio_channel_in_mask_from_count(channelCount), - 4 * kMaxBufferSize / sizeof(int16_t), /* Enable ping-pong buffers */ - flags, - AudioRecordCallbackFunction, - this); - - mInitCheck = mRecord->initCheck(); + + int minFrameCount; + status_t status = AudioRecord::getMinFrameCount(&minFrameCount, + sampleRate, + AUDIO_FORMAT_PCM_16_BIT, + channelCount); + if (status == OK) { + // make sure that the AudioRecord callback never returns more than the maximum + // buffer size + int frameCount = kMaxBufferSize / sizeof(int16_t) / channelCount; + + // make sure that the AudioRecord total buffer size is large enough + int bufCount = 2; + while ((bufCount * frameCount) < minFrameCount) { + bufCount++; + } + + AudioRecord::record_flags flags = (AudioRecord::record_flags) + (AudioRecord::RECORD_AGC_ENABLE | + AudioRecord::RECORD_NS_ENABLE | + AudioRecord::RECORD_IIR_ENABLE); + mRecord = new AudioRecord( + inputSource, sampleRate, AUDIO_FORMAT_PCM_16_BIT, + audio_channel_in_mask_from_count(channelCount), + bufCount * frameCount, + flags, + AudioRecordCallbackFunction, + this, + frameCount); + mInitCheck = mRecord->initCheck(); + } else { + mInitCheck = status; + } } AudioSource::~AudioSource() { -- cgit v1.1