diff --git a/daemon/src/audio/alsa/alsalayer.cpp b/daemon/src/audio/alsa/alsalayer.cpp index 414d45e084a62fde93f34603196301c454fd75c8..aacdf3983738304d51809cdeda430527d02f71d6 100644 --- a/daemon/src/audio/alsa/alsalayer.cpp +++ b/daemon/src/audio/alsa/alsalayer.cpp @@ -639,16 +639,11 @@ AlsaLayer::soundCardIndexExists(int card, DeviceType stream) } int -AlsaLayer::getAudioDeviceIndex(const std::string &description) const +AlsaLayer::getAudioDeviceIndex(const std::string &description, DeviceType type) const { - std::vector<HwIDPair> captureDevice(getAudioDeviceIndexMap(true)); - std::vector<HwIDPair> playbackDevice(getAudioDeviceIndexMap(false)); + std::vector<HwIDPair> devices = getAudioDeviceIndexMap(type == DeviceType::CAPTURE); - std::vector<HwIDPair> audioDeviceIndexMap; - audioDeviceIndexMap.insert(audioDeviceIndexMap.end(), captureDevice.begin(), captureDevice.end()); - audioDeviceIndexMap.insert(audioDeviceIndexMap.end(), playbackDevice.begin(), playbackDevice.end()); - - for (const auto & dev : audioDeviceIndexMap) + for (const auto & dev : devices) if (dev.second == description) return dev.first; diff --git a/daemon/src/audio/alsa/alsalayer.h b/daemon/src/audio/alsa/alsalayer.h index 7efadea9d20bf7471c9f6b782a87df4fc0c5aef7..3211872dde301b8705fcb4b18f3771f79f00f357 100644 --- a/daemon/src/audio/alsa/alsalayer.h +++ b/daemon/src/audio/alsa/alsalayer.h @@ -105,7 +105,7 @@ class AlsaLayer : public AudioLayer { * @param description The string description * @return int Its index */ - int getAudioDeviceIndex(const std::string &description) const; + int getAudioDeviceIndex(const std::string &description, DeviceType type) const; std::string getAudioDeviceName(int index, DeviceType type) const; void playback(int maxSamples); diff --git a/daemon/src/audio/audiolayer.cpp b/daemon/src/audio/audiolayer.cpp index 222f09f66db5371676593ecbddd1bcd397caeb43..9f3e97ed52cdb6332eee55160ff6e1fa6376366c 100644 --- a/daemon/src/audio/audiolayer.cpp +++ b/daemon/src/audio/audiolayer.cpp @@ -29,12 +29,13 @@ * as that of the covered work. */ -#include <ctime> #include "audiolayer.h" #include "audio/dcblocker.h" #include "logger.h" #include "manager.h" +#include <ctime> + AudioLayer::AudioLayer(const AudioPreference &pref) : isCaptureMuted_(pref.getCaptureMuted()) , isPlaybackMuted_(pref.getPlaybackMuted()) diff --git a/daemon/src/audio/audiolayer.h b/daemon/src/audio/audiolayer.h index cf5a286f01569404afbea635bc27c0527c9fe49f..c0fb3533741b52500906c56dd3a3f1a1b9b2c12f 100644 --- a/daemon/src/audio/audiolayer.h +++ b/daemon/src/audio/audiolayer.h @@ -34,14 +34,16 @@ #ifndef AUDIO_LAYER_H_ #define AUDIO_LAYER_H_ -#include <mutex> -#include <sys/time.h> -#include <vector> + #include "ringbuffer.h" #include "dcblocker.h" #include "resampler.h" #include "noncopyable.h" +#include <sys/time.h> +#include <mutex> +#include <vector> + /** * @file audiolayer.h * @brief Main sound class. Manages the data transfers between the application and the hardware. @@ -73,7 +75,7 @@ class AudioLayer { virtual std::vector<std::string> getCaptureDeviceList() const = 0; virtual std::vector<std::string> getPlaybackDeviceList() const = 0; - virtual int getAudioDeviceIndex(const std::string& name) const = 0; + virtual int getAudioDeviceIndex(const std::string& name, DeviceType type) const = 0; virtual std::string getAudioDeviceName(int index, DeviceType type) const = 0; virtual int getIndexCapture() const = 0; virtual int getIndexPlayback() const = 0; diff --git a/daemon/src/audio/jack/jacklayer.cpp b/daemon/src/audio/jack/jacklayer.cpp index a1895220e717dac88cc70725bbbdfb9abd658115..dc583809c92fba84575e72c2eae064b37adbd04c 100644 --- a/daemon/src/audio/jack/jacklayer.cpp +++ b/daemon/src/audio/jack/jacklayer.cpp @@ -399,7 +399,7 @@ JackLayer::getPlaybackDeviceList() const } int -JackLayer::getAudioDeviceIndex(const std::string& /*name*/) const { return 0; } +JackLayer::getAudioDeviceIndex(const std::string& /*name*/, DeviceType /*type*/) const { return 0; } std::string JackLayer::getAudioDeviceName(int /*index*/, DeviceType /*type*/) const { return ""; } diff --git a/daemon/src/audio/jack/jacklayer.h b/daemon/src/audio/jack/jacklayer.h index bb53039fd2895ab788a90db263454c4e41f96576..14c9492f35fb478ed42b76f90b78b569a43754c8 100644 --- a/daemon/src/audio/jack/jacklayer.h +++ b/daemon/src/audio/jack/jacklayer.h @@ -79,7 +79,7 @@ class JackLayer : public AudioLayer { std::vector<std::string> getCaptureDeviceList() const; std::vector<std::string> getPlaybackDeviceList() const; - int getAudioDeviceIndex(const std::string& name) const; + int getAudioDeviceIndex(const std::string& name, DeviceType type) const; std::string getAudioDeviceName(int index, DeviceType type) const; int getIndexCapture() const; int getIndexPlayback() const; diff --git a/daemon/src/audio/opensl/opensllayer.h b/daemon/src/audio/opensl/opensllayer.h index 0c8a0e6c453ceb42461cc3789e5d95583e259785..2330371b2d823f21623cfdecee52731b7dbd1b83 100644 --- a/daemon/src/audio/opensl/opensllayer.h +++ b/daemon/src/audio/opensl/opensllayer.h @@ -108,7 +108,7 @@ class OpenSLLayer : public AudioLayer { void stopAudioCapture(); - virtual int getAudioDeviceIndex(const std::string&) const { + virtual int getAudioDeviceIndex(const std::string&, DeviceType) const { return 0; } diff --git a/daemon/src/audio/pulseaudio/pulselayer.cpp b/daemon/src/audio/pulseaudio/pulselayer.cpp index 15854250d2756fa28e3067a15d59e6bcd1c0302a..0a52fe0f81b361105422fa209eef3df0a986e392 100644 --- a/daemon/src/audio/pulseaudio/pulselayer.cpp +++ b/daemon/src/audio/pulseaudio/pulselayer.cpp @@ -267,15 +267,32 @@ std::vector<std::string> PulseLayer::getPlaybackDeviceList() const return names; } -int PulseLayer::getAudioDeviceIndex(const std::string& name) const +int PulseLayer::getAudioDeviceIndex(const std::string& descr, DeviceType type) const { - int index = std::distance(sourceList_.begin(), std::find_if(sourceList_.begin(), sourceList_.end(), PaDeviceInfos::DescriptionComparator(name))); - - if (index == std::distance(sourceList_.begin(), sourceList_.end())) { - index = std::distance(sinkList_.begin(), std::find_if(sinkList_.begin(), sinkList_.end(), PaDeviceInfos::DescriptionComparator(name))); + switch (type) { + case DeviceType::PLAYBACK: + case DeviceType::RINGTONE: + return std::distance(sinkList_.begin(), std::find_if(sinkList_.begin(), sinkList_.end(), PaDeviceInfos::DescriptionComparator(descr))); + case DeviceType::CAPTURE: + return std::distance(sourceList_.begin(), std::find_if(sourceList_.begin(), sourceList_.end(), PaDeviceInfos::DescriptionComparator(descr))); + default: + ERROR("Unexpected device type"); + return 0; } +} - return index; +int PulseLayer::getAudioDeviceIndexByName(const std::string& name, DeviceType type) const +{ + switch (type) { + case DeviceType::PLAYBACK: + case DeviceType::RINGTONE: + return std::distance(sinkList_.begin(), std::find_if(sinkList_.begin(), sinkList_.end(), PaDeviceInfos::NameComparator(name))); + case DeviceType::CAPTURE: + return std::distance(sourceList_.begin(), std::find_if(sourceList_.begin(), sourceList_.end(), PaDeviceInfos::NameComparator(name))); + default: + ERROR("Unexpected device type"); + return 0; + } } const PaDeviceInfos* PulseLayer::getDeviceInfos(const std::vector<PaDeviceInfos>& list, const std::string& name) const @@ -760,15 +777,15 @@ void PulseLayer::updatePreference(AudioPreference &preference, int index, Device int PulseLayer::getIndexCapture() const { - return getAudioDeviceIndex(preference_.getPulseDeviceRecord()); + return getAudioDeviceIndexByName(preference_.getPulseDeviceRecord(), DeviceType::CAPTURE); } int PulseLayer::getIndexPlayback() const { - return getAudioDeviceIndex(preference_.getPulseDevicePlayback()); + return getAudioDeviceIndexByName(preference_.getPulseDevicePlayback(), DeviceType::PLAYBACK); } int PulseLayer::getIndexRingtone() const { - return getAudioDeviceIndex(preference_.getPulseDeviceRingtone()); + return getAudioDeviceIndexByName(preference_.getPulseDeviceRingtone(), DeviceType::RINGTONE); } diff --git a/daemon/src/audio/pulseaudio/pulselayer.h b/daemon/src/audio/pulseaudio/pulselayer.h index 31d943c5bdd49af51e9c3c2893dcffa8ce77343e..5533386506fab2769f62519654f7c8f4e69f20f3 100644 --- a/daemon/src/audio/pulseaudio/pulselayer.h +++ b/daemon/src/audio/pulseaudio/pulselayer.h @@ -117,18 +117,16 @@ class PulseLayer : public AudioLayer { void writeToSpeaker(); void ringtoneToSpeaker(); - void updateSinkList(); - void updateSourceList(); bool inSinkList(const std::string &deviceName); - bool inSourceList(const std::string &deviceName); virtual std::vector<std::string> getCaptureDeviceList() const; virtual std::vector<std::string> getPlaybackDeviceList() const; - int getAudioDeviceIndex(const std::string& name) const; + int getAudioDeviceIndex(const std::string& descr, DeviceType type) const; + int getAudioDeviceIndexByName(const std::string& name, DeviceType type) const; std::string getAudioDeviceName(int index, DeviceType type) const; diff --git a/daemon/src/client/android/configurationmanager.i b/daemon/src/client/android/configurationmanager.i index add701e5dd2739dd2eee0c233bfbf964b0c053e3..1616e3ef29891b06f58d469cabc1c3424950ff48 100644 --- a/daemon/src/client/android/configurationmanager.i +++ b/daemon/src/client/android/configurationmanager.i @@ -109,7 +109,8 @@ public: void setAudioRingtoneDevice(const int32_t& index); std::vector< std::string > getAudioInputDeviceList(); std::vector< std::string > getCurrentAudioDevicesIndex(); - int32_t getAudioDeviceIndex(const std::string& name); + int32_t getAudioInputDeviceIndex(const std::string& name); + int32_t getAudioOutputDeviceIndex(const std::string& name); std::string getCurrentAudioOutputPlugin(); bool getNoiseSuppressState(); void setNoiseSuppressState(const bool& state); diff --git a/daemon/src/client/configurationmanager.cpp b/daemon/src/client/configurationmanager.cpp index 1937577e66af839bcc00d203d0c234594c30398c..23fbe4df930d4f1d77bd15f103ef581669b41328 100644 --- a/daemon/src/client/configurationmanager.cpp +++ b/daemon/src/client/configurationmanager.cpp @@ -262,9 +262,14 @@ std::vector<std::string> ConfigurationManager::getCurrentAudioDevicesIndex() return Manager::instance().getCurrentAudioDevicesIndex(); } -int32_t ConfigurationManager::getAudioDeviceIndex(const std::string& name) +int32_t ConfigurationManager::getAudioInputDeviceIndex(const std::string& name) { - return Manager::instance().getAudioDeviceIndex(name); + return Manager::instance().getAudioInputDeviceIndex(name); +} + +int32_t ConfigurationManager::getAudioOutputDeviceIndex(const std::string& name) +{ + return Manager::instance().getAudioOutputDeviceIndex(name); } std::string ConfigurationManager::getCurrentAudioOutputPlugin() diff --git a/daemon/src/client/configurationmanager.h b/daemon/src/client/configurationmanager.h index e4a2e7041dc880681202385c3c2e917cd58d1a15..632f4481521f62497b5e700b29bf6cdbc3bb3e38 100644 --- a/daemon/src/client/configurationmanager.h +++ b/daemon/src/client/configurationmanager.h @@ -104,7 +104,8 @@ class ConfigurationManager void setAudioRingtoneDevice(const int32_t& index); std::vector< std::string > getAudioInputDeviceList(); std::vector< std::string > getCurrentAudioDevicesIndex(); - int32_t getAudioDeviceIndex(const std::string& name); + int32_t getAudioInputDeviceIndex(const std::string& name); + int32_t getAudioOutputDeviceIndex(const std::string& name); std::string getCurrentAudioOutputPlugin(); bool getNoiseSuppressState(); void setNoiseSuppressState(const bool& state); diff --git a/daemon/src/client/dbus/configurationmanager-introspec.xml b/daemon/src/client/dbus/configurationmanager-introspec.xml index 6ddd0b11535a42d8bb787b31d31e1cf71fb03c2a..2d265de05db0d551f77e3e9f4f435169b0a301c7 100644 --- a/daemon/src/client/dbus/configurationmanager-introspec.xml +++ b/daemon/src/client/dbus/configurationmanager-introspec.xml @@ -424,7 +424,14 @@ </arg> </method> - <method name="getAudioDeviceIndex" tp:name-for-bindings="getAudioDeviceIndex"> + <method name="getAudioInputDeviceIndex" tp:name-for-bindings="getAudioInputDeviceIndex"> + <arg type="s" name="name" direction="in"> + </arg> + <arg type="i" name="index" direction="out"> + </arg> + </method> + + <method name="getAudioOutputDeviceIndex" tp:name-for-bindings="getAudioOutputDeviceIndex"> <arg type="s" name="name" direction="in"> </arg> <arg type="i" name="index" direction="out"> diff --git a/daemon/src/managerimpl.cpp b/daemon/src/managerimpl.cpp index 34e00fe46e64f5a3fb632e4c47c1b37a71db1472..4d29a100f3d3912913663257d06a059ba6730b4f 100644 --- a/daemon/src/managerimpl.cpp +++ b/daemon/src/managerimpl.cpp @@ -2192,18 +2192,28 @@ std::string ManagerImpl::getAudioManager() const } -int ManagerImpl::getAudioDeviceIndex(const std::string &name) +int ManagerImpl::getAudioInputDeviceIndex(const std::string &name) { - int soundCardIndex = 0; + std::lock_guard<std::mutex> lock(audioLayerMutex_); + + if (audiodriver_ == nullptr) { + ERROR("Audio layer not initialized"); + return 0; + } + + return audiodriver_->getAudioDeviceIndex(name, DeviceType::CAPTURE); +} +int ManagerImpl::getAudioOutputDeviceIndex(const std::string &name) +{ std::lock_guard<std::mutex> lock(audioLayerMutex_); if (audiodriver_ == nullptr) { ERROR("Audio layer not initialized"); - return soundCardIndex; + return 0; } - return audiodriver_->getAudioDeviceIndex(name); + return audiodriver_->getAudioDeviceIndex(name, DeviceType::PLAYBACK); } std::string ManagerImpl::getCurrentAudioOutputPlugin() const diff --git a/daemon/src/managerimpl.h b/daemon/src/managerimpl.h index 2f4a76f7676232893c3db4570b83ade383353848..dae7950c78fa204140c46886d4f6777421f5e22a 100644 --- a/daemon/src/managerimpl.h +++ b/daemon/src/managerimpl.h @@ -548,7 +548,8 @@ class ManagerImpl { * @param name The string description of an audio device * @return int His index */ - int getAudioDeviceIndex(const std::string &name); + int getAudioInputDeviceIndex(const std::string &name); + int getAudioOutputDeviceIndex(const std::string &name); /** * Get current alsa plugin