Retry opening blocked alsa devices

This commit is contained in:
badaix 2020-08-17 23:16:45 +02:00
parent f2e2094254
commit 9e06e52193
2 changed files with 23 additions and 23 deletions

View file

@ -271,7 +271,7 @@ void AlsaPlayer::initAlsa()
/* Open the PCM device in playback mode */
if ((err = snd_pcm_open(&handle_, settings_.pcm_device.name.c_str(), SND_PCM_STREAM_PLAYBACK, 0)) < 0)
throw SnapException("Can't open " + settings_.pcm_device.name + ", error: " + snd_strerror(err));
throw SnapException("Can't open " + settings_.pcm_device.name + ", error: " + snd_strerror(err), err);
/* struct snd_pcm_playback_info_t pinfo;
if ( (pcm = snd_pcm_playback_info( pcm_handle, &pinfo )) < 0 )
@ -426,7 +426,18 @@ void AlsaPlayer::uninitMixer()
void AlsaPlayer::start()
{
try
{
initAlsa();
}
catch (const SnapException& e)
{
LOG(ERROR, LOG_TAG) << "Exception: " << e.what() << ", code: " << e.code() << "\n";
// Accept "Device or ressource busy", the worker loop will retry
if (e.code() != -EBUSY)
throw;
}
Player::start();
}

View file

@ -16,8 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef SNAP_EXCEPTION_H
#define SNAP_EXCEPTION_H
#ifndef SNAP_EXCEPTION_HPP
#define SNAP_EXCEPTION_HPP
#include <cstring> // std::strlen, std::strcpy
#include <exception>
@ -27,18 +27,24 @@
class SnapException : public std::exception
{
std::string text_;
int error_code_;
public:
SnapException(const char* text) : text_(text)
SnapException(const char* text, int error_code = 0) : text_(text), error_code_(error_code)
{
}
SnapException(const std::string& text) : SnapException(text.c_str())
SnapException(const std::string& text, int error_code = 0) : SnapException(text.c_str(), error_code)
{
}
~SnapException() throw() override = default;
int code() const noexcept
{
return error_code_;
}
const char* what() const noexcept override
{
return text_.c_str();
@ -46,21 +52,4 @@ public:
};
class AsyncSnapException : public SnapException
{
public:
AsyncSnapException(const char* text) : SnapException(text)
{
}
AsyncSnapException(const std::string& text) : SnapException(text)
{
}
~AsyncSnapException() throw() override = default;
};
#endif