From 6f1e24b00c21166b5f3a3e89e65efe7a0c1ee655 Mon Sep 17 00:00:00 2001 From: Nedko Arnaudov Date: Sun, 7 Apr 2013 00:44:39 +0300 Subject: [PATCH] Ignore device reservation failures Device reservation could fail because of bad system setup. Trying to open the device anyway seems to have no undesired impact. --- linux/alsa/JackAlsaDriver.cpp | 93 ++++++++++++++++++++--------------- linux/alsa/JackAlsaDriver.h | 13 ++++- 2 files changed, 65 insertions(+), 41 deletions(-) diff --git a/linux/alsa/JackAlsaDriver.cpp b/linux/alsa/JackAlsaDriver.cpp index bfed2f11..1e89f020 100644 --- a/linux/alsa/JackAlsaDriver.cpp +++ b/linux/alsa/JackAlsaDriver.cpp @@ -241,6 +241,57 @@ fail: return i; } +bool JackAlsaDriver::AcquireDevice(int device_no, char* reserved_device) +{ + assert(*reserved_device == '\0'); + snprintf(reserved_device, MAX_RESERVED_DEVICE_NAME_SIZE, "Audio%d", device_no); + + if (!JackServerGlobals::on_device_acquire(reserved_device)) { + *reserved_device = '\0'; + return false; + } + + return true; +} + +void JackAlsaDriver::AcquireDevices(const char* capture, const char* playback) +{ + if (JackServerGlobals::on_device_acquire == NULL) { + assert(JackServerGlobals::on_device_release == NULL); + return; + } + + int capture_card = card_to_num(capture); + int playback_card = card_to_num(playback); + + if (capture_card >= 0) + if (!AcquireDevice(capture_card, fReservedCaptureDevice)) + jack_error("Audio device %s cannot be acquired...", capture); + + if (playback_card >= 0 && playback_card != capture_card) + if (!AcquireDevice(playback_card, fReservedPlaybackDevice)) + jack_error("Audio device %s cannot be acquired...", playback); +} + +void JackAlsaDriver::ReleaseDevice(char* reserved_device) +{ + if (*reserved_device == '\0') return; + + JackServerGlobals::on_device_release(reserved_device); + *reserved_device = '\0'; +} + +void JackAlsaDriver::ReleaseDevices() +{ + if (JackServerGlobals::on_device_release == NULL) { + assert(JackServerGlobals::on_device_acquire == NULL); + return; + } + + ReleaseDevice(fReservedCaptureDevice); + ReleaseDevice(fReservedPlaybackDevice); +} + int JackAlsaDriver::Open(jack_nframes_t nframes, jack_nframes_t user_nperiods, jack_nframes_t samplerate, @@ -273,31 +324,7 @@ int JackAlsaDriver::Open(jack_nframes_t nframes, else if (strcmp(midi_driver_name, "raw") == 0) midi = alsa_rawmidi_new((jack_client_t*)this); - if (JackServerGlobals::on_device_acquire != NULL) { - int capture_card = card_to_num(capture_driver_name); - int playback_card = card_to_num(playback_driver_name); - char audio_name[32]; - - if (capture_card >= 0) { - snprintf(audio_name, sizeof(audio_name), "Audio%d", capture_card); - if (!JackServerGlobals::on_device_acquire(audio_name)) { - jack_error("Audio device %s cannot be acquired...", capture_driver_name); - return -1; - } - } - - if (playback_card >= 0 && playback_card != capture_card) { - snprintf(audio_name, sizeof(audio_name), "Audio%d", playback_card); - if (!JackServerGlobals::on_device_acquire(audio_name)) { - jack_error("Audio device %s cannot be acquired...", playback_driver_name); - if (capture_card >= 0) { - snprintf(audio_name, sizeof(audio_name), "Audio%d", capture_card); - JackServerGlobals::on_device_release(audio_name); - } - return -1; - } - } - } + AcquireDevices(capture_driver_name, playback_driver_name); fDriver = alsa_driver_new ((char*)"alsa_pcm", (char*)playback_driver_name, (char*)capture_driver_name, NULL, @@ -335,21 +362,7 @@ int JackAlsaDriver::Close() alsa_driver_delete((alsa_driver_t*)fDriver); - if (JackServerGlobals::on_device_release != NULL) - { - char audio_name[32]; - int capture_card = card_to_num(fCaptureDriverName); - if (capture_card >= 0) { - snprintf(audio_name, sizeof(audio_name), "Audio%d", capture_card); - JackServerGlobals::on_device_release(audio_name); - } - - int playback_card = card_to_num(fPlaybackDriverName); - if (playback_card >= 0 && playback_card != capture_card) { - snprintf(audio_name, sizeof(audio_name), "Audio%d", playback_card); - JackServerGlobals::on_device_release(audio_name); - } - } + ReleaseDevices(); return res; } diff --git a/linux/alsa/JackAlsaDriver.h b/linux/alsa/JackAlsaDriver.h index c62ff541..09a943ba 100644 --- a/linux/alsa/JackAlsaDriver.h +++ b/linux/alsa/JackAlsaDriver.h @@ -29,6 +29,8 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. namespace Jack { +#define MAX_RESERVED_DEVICE_NAME_SIZE 32 + /*! \brief The ALSA driver. */ @@ -39,14 +41,23 @@ class JackAlsaDriver : public JackAudioDriver private: jack_driver_t* fDriver; + char fReservedCaptureDevice[MAX_RESERVED_DEVICE_NAME_SIZE]; + char fReservedPlaybackDevice[MAX_RESERVED_DEVICE_NAME_SIZE]; + bool AcquireDevice(int device_no, char* reserved_device); + void ReleaseDevice(char* reserved_device); + void AcquireDevices(const char* capture, const char* playback); + void ReleaseDevices(); void UpdateLatencies(); public: JackAlsaDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table) : JackAudioDriver(name, alias, engine, table),fDriver(NULL) - {} + { + *fReservedCaptureDevice = '\0'; + *fReservedPlaybackDevice = '\0'; + } virtual ~JackAlsaDriver() {}