diff options
author | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-16 22:38:48 +0000 |
---|---|---|
committer | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-16 22:38:48 +0000 |
commit | 45dc74d1275e9732cb3204785a2c17a02e5692cd (patch) | |
tree | 19bedc1c44c853c2dc455cc741e838e8eda9b09b /media/audio | |
parent | a181c585ae286c28c96f85bb758becd4f8ef4c53 (diff) | |
download | chromium_src-45dc74d1275e9732cb3204785a2c17a02e5692cd.zip chromium_src-45dc74d1275e9732cb3204785a2c17a02e5692cd.tar.gz chromium_src-45dc74d1275e9732cb3204785a2c17a02e5692cd.tar.bz2 |
AudioController accepts hardware buffer size as parameter.
BUG=39885
TEST=media_unitests
AudioController to take hardware buffer size for opening the audio device.
Review URL: http://codereview.chromium.org/2822014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50046 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio')
-rw-r--r-- | media/audio/audio_controller.cc | 37 | ||||
-rw-r--r-- | media/audio/audio_controller.h | 5 | ||||
-rw-r--r-- | media/audio/audio_controller_unittest.cc | 31 |
3 files changed, 51 insertions, 22 deletions
diff --git a/media/audio/audio_controller.cc b/media/audio/audio_controller.cc index 8e9357b..b30f722 100644 --- a/media/audio/audio_controller.cc +++ b/media/audio/audio_controller.cc @@ -4,16 +4,10 @@ #include "media/audio/audio_controller.h" -// This constant governs the hardware audio buffer size, this value should be -// choosen carefully and is platform specific. -static const int kSamplesPerHardwarePacket = 8192; - -static const uint32 kMegabytes = 1024 * 1024; - // The following parameters limit the request buffer and packet size from the // renderer to avoid renderer from requesting too much memory. -static const uint32 kMaxDecodedPacketSize = 2 * kMegabytes; -static const uint32 kMaxBufferCapacity = 5 * kMegabytes; +static const int kMegabytes = 1024 * 1024; +static const int kMaxHardwareBufferSize = 2 * kMegabytes; static const int kMaxChannels = 32; static const int kMaxBitsPerSample = 64; static const int kMaxSampleRate = 192000; @@ -21,13 +15,17 @@ static const int kMaxSampleRate = 192000; // Return true if the parameters for creating an audio stream is valid. // Return false otherwise. static bool CheckParameters(int channels, int sample_rate, - int bits_per_sample) { + int bits_per_sample, int hardware_buffer_size) { if (channels <= 0 || channels > kMaxChannels) return false; if (sample_rate <= 0 || sample_rate > kMaxSampleRate) return false; if (bits_per_sample <= 0 || bits_per_sample > kMaxBitsPerSample) return false; + if (hardware_buffer_size <= 0 || + hardware_buffer_size > kMaxHardwareBufferSize) { + return false; + } return true; } @@ -54,9 +52,11 @@ scoped_refptr<AudioController> AudioController::Create( int channels, int sample_rate, int bits_per_sample, + int hardware_buffer_size, uint32 buffer_capacity) { - if (!CheckParameters(channels, sample_rate, bits_per_sample)) + if (!CheckParameters(channels, sample_rate, bits_per_sample, + hardware_buffer_size)) return NULL; // Starts the audio controller thread. @@ -69,7 +69,8 @@ scoped_refptr<AudioController> AudioController::Create( source->thread_.message_loop()->PostTask( FROM_HERE, NewRunnableMethod(source.get(), &AudioController::DoCreate, - format, channels, sample_rate, bits_per_sample)); + format, channels, sample_rate, bits_per_sample, + hardware_buffer_size)); return source; } @@ -80,11 +81,13 @@ scoped_refptr<AudioController> AudioController::CreateLowLatency( int channels, int sample_rate, int bits_per_sample, + int hardware_buffer_size, SyncReader* sync_reader) { DCHECK(sync_reader); - if (!CheckParameters(channels, sample_rate, bits_per_sample)) + if (!CheckParameters(channels, sample_rate, bits_per_sample, + hardware_buffer_size)) return NULL; // Starts the audio controller thread. @@ -97,7 +100,8 @@ scoped_refptr<AudioController> AudioController::CreateLowLatency( source->thread_.message_loop()->PostTask( FROM_HERE, NewRunnableMethod(source.get(), &AudioController::DoCreate, - format, channels, sample_rate, bits_per_sample)); + format, channels, sample_rate, bits_per_sample, + hardware_buffer_size)); return source; } @@ -145,7 +149,8 @@ void AudioController::EnqueueData(const uint8* data, uint32 size) { } void AudioController::DoCreate(AudioManager::Format format, int channels, - int sample_rate, int bits_per_sample) { + int sample_rate, int bits_per_sample, + int hardware_buffer_size) { // Create the stream in the first place. stream_ = AudioManager::GetAudioManager()->MakeAudioStream( format, channels, sample_rate, bits_per_sample); @@ -156,9 +161,7 @@ void AudioController::DoCreate(AudioManager::Format format, int channels, return; } - uint32 hardware_packet_size = kSamplesPerHardwarePacket * channels * - bits_per_sample / 8; - if (stream_ && !stream_->Open(hardware_packet_size)) { + if (stream_ && !stream_->Open(hardware_buffer_size)) { stream_->Close(); stream_ = NULL; diff --git a/media/audio/audio_controller.h b/media/audio/audio_controller.h index dae3d58..21d6d22 100644 --- a/media/audio/audio_controller.h +++ b/media/audio/audio_controller.h @@ -107,6 +107,7 @@ class AudioController : public base::RefCountedThreadSafe<AudioController>, int channels, // Number of channels. int sample_rate, // Sampling frequency/rate. int bits_per_sample, // Number of bits per sample. + int hardware_buffer_size, // Size of the hardware buffer. // Soft limit for buffer capacity in this controller. This parameter // is used only in regular latency mode. @@ -119,6 +120,7 @@ class AudioController : public base::RefCountedThreadSafe<AudioController>, int channels, // Number of channels. int sample_rate, // Sampling frequency/rate. int bits_per_sample, // Number of bits per sample. + int hardware_buffer_size, // Size of the hardware buffer. // External synchronous reader for audio controller. SyncReader* sync_reader); @@ -161,7 +163,8 @@ class AudioController : public base::RefCountedThreadSafe<AudioController>, // The following methods are executed on the audio controller thread. void DoCreate(AudioManager::Format format, int channels, - int sample_rate, int bits_per_sample); + int sample_rate, int bits_per_sample, + int hardware_buffer_size); void DoPlay(); void DoPause(); void DoFlush(); diff --git a/media/audio/audio_controller_unittest.cc b/media/audio/audio_controller_unittest.cc index 5c5a522..c2a30ae 100644 --- a/media/audio/audio_controller_unittest.cc +++ b/media/audio/audio_controller_unittest.cc @@ -15,6 +15,13 @@ using ::testing::Exactly; using ::testing::InvokeWithoutArgs; using ::testing::NotNull; +static const int kSampleRate = AudioManager::kAudioCDSampleRate; +static const int kBitsPerSample = 16; +static const int kChannels = 2; +static const int kHardwareBufferSize = kSampleRate * kBitsPerSample * + kChannels / 8; +static const int kBufferCapacity = 3 * kHardwareBufferSize; + namespace media { class MockAudioControllerEventHandler : public AudioController::EventHandler { @@ -86,8 +93,8 @@ TEST(AudioControllerTest, PlayAndClose) { .WillRepeatedly(SignalEvent(&event, &count, 10)); scoped_refptr<AudioController> controller = AudioController::Create( - &event_handler, AudioManager::AUDIO_PCM_LINEAR, 1, - AudioManager::kAudioCDSampleRate, 16, 4096); + &event_handler, AudioManager::AUDIO_PCM_LINEAR, kChannels, + kSampleRate, kBitsPerSample, kHardwareBufferSize, kBufferCapacity); ASSERT_TRUE(controller.get()); // Wait for OnCreated() to be called. @@ -135,8 +142,8 @@ TEST(AudioControllerTest, PlayPauseClose) { .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal)); scoped_refptr<AudioController> controller = AudioController::Create( - &event_handler, AudioManager::AUDIO_PCM_LINEAR, 1, - AudioManager::kAudioCDSampleRate, 16, 4096); + &event_handler, AudioManager::AUDIO_PCM_LINEAR, kChannels, + kSampleRate, kBitsPerSample, kHardwareBufferSize, kBufferCapacity); ASSERT_TRUE(controller.get()); // Wait for OnCreated() to be called. @@ -161,4 +168,20 @@ TEST(AudioControllerTest, PlayPauseClose) { controller = NULL; } +TEST(AudioControllerTest, HardwareBufferTooLarge) { + if (!HasAudioDevices() || IsRunningHeadless()) + return; + + // Create an audio device with a very large hardware buffer size. + MockAudioControllerEventHandler event_handler; + scoped_refptr<AudioController> controller = AudioController::Create( + &event_handler, AudioManager::AUDIO_PCM_LINEAR, kChannels, + kSampleRate, kBitsPerSample, kHardwareBufferSize * 1000, + kBufferCapacity); + + // Use assert because we don't stop the device and assume we can't + // create one. + ASSERT_FALSE(controller); +} + } // namespace media |