From 7d5ad4239974cad5843fb605488a8c833716e56c Mon Sep 17 00:00:00 2001 From: "xians@chromium.org" Date: Thu, 15 Sep 2011 03:59:35 +0000 Subject: There is a complain from Valgrind about invalid memory access in snd_device_name_hint(-1, ..) // -1 means all cards. It looks like the problem is actually because we use -1 to loop through all the soundcards. And this patch will loop through the soundcard manually, which should resolve the Valgrind problem. Bug=96207 Test=media_unittests with Valgrind Review URL: http://codereview.chromium.org/7888011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@101240 0039d316-1c4b-4281-b951-d872f2087c98 --- media/audio/linux/alsa_wrapper.cc | 4 ++++ media/audio/linux/alsa_wrapper.h | 1 + media/audio/linux/audio_manager_linux.cc | 32 ++++++++++++++++++-------------- 3 files changed, 23 insertions(+), 14 deletions(-) (limited to 'media/audio') diff --git a/media/audio/linux/alsa_wrapper.cc b/media/audio/linux/alsa_wrapper.cc index 563bccb..69b703e 100644 --- a/media/audio/linux/alsa_wrapper.cc +++ b/media/audio/linux/alsa_wrapper.cc @@ -29,6 +29,10 @@ int AlsaWrapper::DeviceNameFreeHint(void** hints) { return snd_device_name_free_hint(hints); } +int AlsaWrapper::CardNext(int* rcard) { + return snd_card_next(rcard); +} + int AlsaWrapper::PcmClose(snd_pcm_t* handle) { return snd_pcm_close(handle); } diff --git a/media/audio/linux/alsa_wrapper.h b/media/audio/linux/alsa_wrapper.h index efe93d0..85cf68a 100644 --- a/media/audio/linux/alsa_wrapper.h +++ b/media/audio/linux/alsa_wrapper.h @@ -19,6 +19,7 @@ class MEDIA_EXPORT AlsaWrapper { virtual int DeviceNameHint(int card, const char* iface, void*** hints); virtual char* DeviceNameGetHint(const void* hint, const char* id); virtual int DeviceNameFreeHint(void** hints); + virtual int CardNext(int* rcard); virtual int PcmOpen(snd_pcm_t** handle, const char* name, snd_pcm_stream_t stream, int mode); diff --git a/media/audio/linux/audio_manager_linux.cc b/media/audio/linux/audio_manager_linux.cc index caab797..930c6b2 100644 --- a/media/audio/linux/audio_manager_linux.cc +++ b/media/audio/linux/audio_manager_linux.cc @@ -63,25 +63,29 @@ bool AudioManagerLinux::HasAudioInputDevices() { } // Constants specified by the ALSA API for device hints. - static const int kGetAllDevices = -1; static const char kPcmInterfaceName[] = "pcm"; bool has_device = false; void** hints = NULL; - - // Use the same approach to find the devices as in - // AlsaPcmOutputStream::FindDeviceForChannels - // Get Alsa device hints. - int error = wrapper_->DeviceNameHint(kGetAllDevices, - kPcmInterfaceName, - &hints); - if (error == 0) { - has_device = HasAnyValidAudioInputDevice(hints); - } else { - LOG(ERROR) << "Unable to get device hints: " << wrapper_->StrError(error); + int card = -1; + + // Loop through the sound cards to get Alsa device hints. + // Don't use snd_device_name_hint(-1,..) since there is a access violation + // inside this ALSA API with libasound.so.2.0.0. + while (!wrapper_->CardNext(&card) && (card >= 0) && !has_device) { + int error = wrapper_->DeviceNameHint(card, + kPcmInterfaceName, + &hints); + if (error == 0) { + has_device = HasAnyValidAudioInputDevice(hints); + + // Destroy the hints now that we're done with it. + wrapper_->DeviceNameFreeHint(hints); + hints = NULL; + } else { + LOG(ERROR) << "Unable to get device hints: " << wrapper_->StrError(error); + } } - // Destroy the hint now that we're done with it. - wrapper_->DeviceNameFreeHint(hints); return has_device; } -- cgit v1.1