Greetings,

If a C++ flowgraph containing an audio source (ALSA) is reconfigured
during runtime, the application will crash with:

audio_alsa_source[hw:1]: snd_pcm_hw_params failed: File descriptor in bad state
terminate called after throwing an instance of 'std::runtime_error'
  what():  check topology failed on audio_alsa_source(2) using
ninputs=0, noutputs=2
Aborted

Some time ago the same problem has been fixed for the audio_alsa_sink
and the attached patch contains the very same fix for the
audio_alsa_source.

Alex
From 5096a92b5d4c03c1b7514802d569b7a8f1cb71c4 Mon Sep 17 00:00:00 2001
From: Alexandru Csete <oz9...@gmail.com>
Date: Sun, 10 Jul 2011 23:31:27 +0200
Subject: [PATCH] Check pcm state before messing with hw_params (prevents
 crash when flowgraph is reconfigured at runtime).

---
 gr-audio/lib/alsa/audio_alsa_source.cc |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/gr-audio/lib/alsa/audio_alsa_source.cc b/gr-audio/lib/alsa/audio_alsa_source.cc
index 4f0042b..2f4506f 100644
--- a/gr-audio/lib/alsa/audio_alsa_source.cc
+++ b/gr-audio/lib/alsa/audio_alsa_source.cc
@@ -217,8 +217,14 @@ audio_alsa_source::check_topology (int ninputs, int noutputs)
   unsigned int nchan = noutputs;
   int err;
 
-  // FIXME check_topology may be called more than once.
+  // Check the state of the stream
   // Ensure that the pcm is in a state where we can still mess with the hw_params
+  snd_pcm_state_t state;
+  state=snd_pcm_state(d_pcm_handle);
+  if ( state== SND_PCM_STATE_RUNNING)
+    return true;  // If stream is running, don't change any parameters
+  else if(state == SND_PCM_STATE_XRUN )
+    snd_pcm_prepare ( d_pcm_handle ); // Prepare stream on underrun, and we can set parameters;
 
   bool special_case = nchan == 1 && d_special_case_stereo_to_mono;
   if (special_case)
-- 
1.7.6

_______________________________________________
Patch-gnuradio mailing list
Patch-gnuradio@gnu.org
https://lists.gnu.org/mailman/listinfo/patch-gnuradio

Reply via email to