Commit dbd91c44 authored by Rafaël Carré's avatar Rafaël Carré
Browse files

* #6629 : use helper to call alsa functions and display error msgs

parent 51325d42
...@@ -198,16 +198,26 @@ AlsaLayer::stopStream (void) ...@@ -198,16 +198,26 @@ AlsaLayer::stopStream (void)
///////////////// ALSA PRIVATE FUNCTIONS //////////////////////////////////////////////// ///////////////// ALSA PRIVATE FUNCTIONS ////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////
/*
* GCC extension : statement expression
*
* ALSA_CALL(function_call, error_string) will:
* call the function
* display an error if the function failed
* return the function return value
*/
#define ALSA_CALL(call, error) ({ \
int err = call; \
if (err < 0) \
_error("ALSA: "error": %s", snd_strerror(err)); \
err; \
})
void AlsaLayer::stopCaptureStream (void) void AlsaLayer::stopCaptureStream (void)
{ {
if (captureHandle_) { if (captureHandle_ && ALSA_CALL(snd_pcm_drop (captureHandle_), "couldn't stop capture") >= 0) {
int err = snd_pcm_drop (captureHandle_); is_capture_running_ = false;
if (err < 0) is_capture_prepared_ = false;
_error("ALSA: couldn't stop capture: %s", snd_strerror (err));
else {
is_capture_running_ = false;
is_capture_prepared_ = false;
}
} }
} }
...@@ -216,39 +226,24 @@ void AlsaLayer::closeCaptureStream (void) ...@@ -216,39 +226,24 @@ void AlsaLayer::closeCaptureStream (void)
if (is_capture_prepared_ and is_capture_running_) if (is_capture_prepared_ and is_capture_running_)
stopCaptureStream (); stopCaptureStream ();
if (is_capture_open_) { if (is_capture_open_ && ALSA_CALL(snd_pcm_close (captureHandle_), "Couldn't close capture") >= 0)
int err = snd_pcm_close (captureHandle_); is_capture_open_ = false;
if (err < 0)
_error("ALSA: Couldn't close capture: %s", snd_strerror (err));
else
is_capture_open_ = false;
}
} }
void AlsaLayer::startCaptureStream (void) void AlsaLayer::startCaptureStream (void)
{ {
if (captureHandle_ and not is_capture_running_) { if (captureHandle_ and not is_capture_running_)
int err = snd_pcm_start (captureHandle_); if (ALSA_CALL(snd_pcm_start (captureHandle_), "Couldn't start capture") >= 0)
if (err < 0)
_error("ALSA: Couldn't start capture: %s", snd_strerror (err));
else
is_capture_running_ = true; is_capture_running_ = true;
}
} }
void AlsaLayer::stopPlaybackStream (void) void AlsaLayer::stopPlaybackStream (void)
{ {
if (ringtoneHandle_ and is_playback_running_) { if (ringtoneHandle_ and is_playback_running_)
int err = snd_pcm_drop (ringtoneHandle_); ALSA_CALL(snd_pcm_drop(ringtoneHandle_), "Couldn't stop ringtone");
if (err < 0)
_error("ALSA: Couldn't stop ringtone: %s", snd_strerror (err));
}
if (playbackHandle_ and is_playback_running_) { if (playbackHandle_ and is_playback_running_) {
int err = snd_pcm_drop (playbackHandle_); if (ALSA_CALL(snd_pcm_drop(playbackHandle_), "Couldn't stop playback") >= 0) {
if (err < 0)
_error("ALSA: Couldn't stop playback: %s", snd_strerror (err));
else {
is_playback_running_ = false; is_playback_running_ = false;
is_playback_prepared_ = false; is_playback_prepared_ = false;
} }
...@@ -262,17 +257,10 @@ void AlsaLayer::closePlaybackStream (void) ...@@ -262,17 +257,10 @@ void AlsaLayer::closePlaybackStream (void)
stopPlaybackStream(); stopPlaybackStream();
if (is_playback_open_) { if (is_playback_open_) {
int err; if (ringtoneHandle_)
if (ringtoneHandle_) { ALSA_CALL(snd_pcm_close(ringtoneHandle_), "Couldn't stop ringtone");
err = snd_pcm_close (ringtoneHandle_);
if (err < 0)
_error("ALSA: Couldn't stop ringtone: %s", snd_strerror (err));
}
err = snd_pcm_close (playbackHandle_); if (ALSA_CALL(snd_pcm_close(playbackHandle_), "Coulnd't close playback") >= 0)
if (err < 0)
_error("ALSA: Coulnd't close playback: %s", snd_strerror (err));
else
is_playback_open_ = false; is_playback_open_ = false;
} }
...@@ -280,45 +268,30 @@ void AlsaLayer::closePlaybackStream (void) ...@@ -280,45 +268,30 @@ void AlsaLayer::closePlaybackStream (void)
void AlsaLayer::startPlaybackStream (void) void AlsaLayer::startPlaybackStream (void)
{ {
if (playbackHandle_ and not is_playback_running_) { if (playbackHandle_ and not is_playback_running_)
int err = snd_pcm_start (playbackHandle_); if (ALSA_CALL(snd_pcm_start(playbackHandle_), "Couldn't start playback") >= 0)
if (err < 0)
_error("ALSA: Couldn't start playback: %s", snd_strerror (err));
else
is_playback_running_ = true; is_playback_running_ = true;
}
} }
void AlsaLayer::prepareCaptureStream (void) void AlsaLayer::prepareCaptureStream (void)
{ {
if (is_capture_open_ and not is_capture_prepared_) { if (is_capture_open_ and not is_capture_prepared_)
int err = snd_pcm_prepare (captureHandle_); if (ALSA_CALL(snd_pcm_prepare(captureHandle_), "Couldn't prepare capture") >= 0)
if (err < 0)
_error("ALSA: Couldn't prepare capture: %s", snd_strerror (err));
else
is_capture_prepared_ = true; is_capture_prepared_ = true;
}
} }
void AlsaLayer::preparePlaybackStream (void) void AlsaLayer::preparePlaybackStream (void)
{ {
if (is_playback_open_ and not is_playback_prepared_) { if (is_playback_open_ and not is_playback_prepared_)
int err = snd_pcm_prepare (playbackHandle_); if (ALSA_CALL(snd_pcm_prepare(playbackHandle_), "Couldn't prepare playback") >= 0)
if (err < 0)
_error("ALSA: Couldn't prepare playback: %s", snd_strerror (err));
else
is_playback_prepared_ = true; is_playback_prepared_ = true;
}
} }
bool AlsaLayer::alsa_set_params (snd_pcm_t *pcm_handle) bool AlsaLayer::alsa_set_params (snd_pcm_t *pcm_handle)
{ {
#define TRY(call, error) do { \ #define TRY(call, error) do { \
int err = call; \ if (ALSA_CALL(call, error) < 0) \
if (err < 0) { \
_error("ALSA: Cannot set "error": %s", snd_strerror(err)); \
return false; \ return false; \
} \
} while(0) } while(0)
snd_pcm_hw_params_t *hwparams; snd_pcm_hw_params_t *hwparams;
...@@ -338,7 +311,9 @@ bool AlsaLayer::alsa_set_params (snd_pcm_t *pcm_handle) ...@@ -338,7 +311,9 @@ bool AlsaLayer::alsa_set_params (snd_pcm_t *pcm_handle)
TRY(snd_pcm_hw_params (HW), "hwparams"); TRY(snd_pcm_hw_params (HW), "hwparams");
#undef HW #undef HW
_debug ("ALSA: Using sampling rate %dHz", audioSampleRate_); _debug ("ALSA: %s using sampling rate %dHz",
(snd_pcm_stream(pcm_handle) == SND_PCM_STREAM_PLAYBACK) ? "playback" : "capture",
audioSampleRate_);
snd_pcm_sw_params_t *swparams = NULL; snd_pcm_sw_params_t *swparams = NULL;
snd_pcm_sw_params_alloca(&swparams); snd_pcm_sw_params_alloca(&swparams);
...@@ -373,18 +348,14 @@ AlsaLayer::write (void* buffer, int length, snd_pcm_t * handle) ...@@ -373,18 +348,14 @@ AlsaLayer::write (void* buffer, int length, snd_pcm_t * handle)
snd_pcm_status_t* status; snd_pcm_status_t* status;
snd_pcm_status_alloca (&status); snd_pcm_status_alloca (&status);
err = snd_pcm_status (handle, status); if (ALSA_CALL(snd_pcm_status (handle, status), "Cannot get playback handle status") >= 0)
if (err < 0) if (snd_pcm_status_get_state (status) == SND_PCM_STATE_XRUN) {
_error("ALSA: Cannot get playback handle status (%s)" , snd_strerror (err)); stopPlaybackStream();
else if (snd_pcm_status_get_state (status) == SND_PCM_STATE_XRUN) { preparePlaybackStream();
stopPlaybackStream(); startPlaybackStream();
preparePlaybackStream(); }
startPlaybackStream();
} ALSA_CALL(snd_pcm_writei (handle, buffer , frames), "XRUN handling failed");
err = snd_pcm_writei (handle, buffer , frames);
if (err < 0)
_error("ALSA: XRUN handling failed : %s", snd_strerror(err));
break; break;
} }
...@@ -410,7 +381,6 @@ AlsaLayer::read (void* buffer, int toCopy) ...@@ -410,7 +381,6 @@ AlsaLayer::read (void* buffer, int toCopy)
return snd_pcm_frames_to_bytes(captureHandle_, frames); return snd_pcm_frames_to_bytes(captureHandle_, frames);
switch (err) { switch (err) {
case -EPIPE: case -EPIPE:
case -ESTRPIPE: case -ESTRPIPE:
case -EIO: case -EIO:
...@@ -418,14 +388,12 @@ AlsaLayer::read (void* buffer, int toCopy) ...@@ -418,14 +388,12 @@ AlsaLayer::read (void* buffer, int toCopy)
snd_pcm_status_t* status; snd_pcm_status_t* status;
snd_pcm_status_alloca (&status); snd_pcm_status_alloca (&status);
err = snd_pcm_status(captureHandle_, status); if (ALSA_CALL(snd_pcm_status(captureHandle_, status), "Get status failed") >= 0)
if (err < 0) if (snd_pcm_status_get_state (status) == SND_PCM_STATE_XRUN) {
_error("ALSA: Get status failed: %s", snd_strerror(err)); stopCaptureStream ();
else if (snd_pcm_status_get_state (status) == SND_PCM_STATE_XRUN) { prepareCaptureStream ();
stopCaptureStream (); startCaptureStream ();
prepareCaptureStream (); }
startCaptureStream ();
}
_error("ALSA: XRUN capture ignored (%s)", snd_strerror (err)); _error("ALSA: XRUN capture ignored (%s)", snd_strerror (err));
break; break;
...@@ -436,9 +404,6 @@ AlsaLayer::read (void* buffer, int toCopy) ...@@ -436,9 +404,6 @@ AlsaLayer::read (void* buffer, int toCopy)
prepareCaptureStream (); prepareCaptureStream ();
startCaptureStream (); startCaptureStream ();
break; break;
default:
break;
} }
return 0; return 0;
...@@ -473,13 +438,12 @@ AlsaLayer::getSoundCardsInfo (int stream) ...@@ -473,13 +438,12 @@ AlsaLayer::getSoundCardsInfo (int stream)
if (snd_card_next (&numCard) < 0 || numCard < 0) if (snd_card_next (&numCard) < 0 || numCard < 0)
return cards_id; return cards_id;
while (numCard >= 0) { do {
std::stringstream ss; std::stringstream ss;
ss << numCard; ss << numCard;
std::string name= "hw:"; std::string name = "hw:" + ss.str();
name.append (ss.str());
if (snd_ctl_open (&handle, name.c_str(), 0) == 0) { if (snd_ctl_open (&handle, name.c_str(), 0) != 0) {
if (snd_ctl_card_info (handle, info) == 0) { if (snd_ctl_card_info (handle, info) == 0) {
snd_pcm_info_set_device (pcminfo , 0); snd_pcm_info_set_device (pcminfo , 0);
snd_pcm_info_set_stream (pcminfo, (stream == SFL_PCM_CAPTURE) ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK); snd_pcm_info_set_stream (pcminfo, (stream == SFL_PCM_CAPTURE) ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK);
...@@ -502,10 +466,8 @@ AlsaLayer::getSoundCardsInfo (int stream) ...@@ -502,10 +466,8 @@ AlsaLayer::getSoundCardsInfo (int stream)
snd_ctl_close (handle); snd_ctl_close (handle);
} }
} while (snd_card_next (&numCard) >= 0 && numCard >= 0);
if (snd_card_next (&numCard) < 0)
break;
}
return cards_id; return cards_id;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment