[pulseaudio-discuss] Behavior of pa_threaded_mainloop_wait ()

Colin Guthrie gmane at colin.guthr.ie
Wed Feb 10 00:40:19 PST 2010


'Twas brillig, and Tristin Celestin at 10/02/10 04:41 did gyre and gimble:
> I have a stream state change callback that fires a signal when the stream is PA_STREAM_READY, PA_STREAM_FAILED, or PA_STREAM_TERMINATED
> When trying to get a ready stream state in pulseaudio, I first wrote this. This hangs
> 
>     do {
> 	  stream_state = pa_stream_get_state (device.stream);
> 	  if (! PA_STREAM_IS_GOOD (stream_state))
> 	  {
> 	       error_number = pa_context_errno (device.context);
> 	       fprintf (stderr, "Could not acquire PulseAudio stream: %s\n", pa_strerror (error_number));
> 	       return;
> 	  }
> 	  else
> 	  {
> 	       fprintf (stderr, "PulseAudio stream state is %d.\n", stream_state);
> 	  }
> 	  pa_threaded_mainloop_wait (device.mainloop);
>      } while (stream_state != PA_STREAM_READY);
> 
> 
> Then I wrote this, based on the pa_simple code, and this doesn't hang
> 
>      stream_state = pa_stream_get_state (device.stream);
>      while (stream_state != PA_STREAM_READY)
>      {
> 	  stream_state = pa_stream_get_state (device.stream);
> 	  if (! PA_STREAM_IS_GOOD (stream_state))
> 	  {
> 	       error_number = pa_context_errno (device.context);
> 	       fprintf (stderr, "Could not acquire PulseAudio stream: %s\n", pa_strerror (error_number));
> 	       return;
> 	  }
> 	  else if (stream_state == PA_STREAM_READY)
> 	       break;
> 	  else
> 	       fprintf (stderr, "PulseAudio stream state is %d.\n", stream_state);
> 	  pa_threaded_mainloop_wait (device.mainloop);
>      }
> 
> Why do I have to explicitly check inside the loop to see if the stream is ready, and then break? Do I have to have called wait() while the callback executes?


With your example, the pa_threaded_mainloop_wait() will be called after
the connection state reaches PA_STREAM_READY. This means that you are
waiting for *something* to happen, but you've not really asked for
anything to be done... all it knows so far is "I need to connect". Once
connected you'll probably want to do various things with subscriptions
etc., but as things stand right now, there is nothing to wait for, so it
pretty much waits forever (a client can be killed in theory and it will
exit then, but the exit condition, while succeeding, will be invalid as
the thing that woke the pa_threaded_mainloop_wait() from it's slumber
will be a state change... so we wont be PA_STREAM_READY anymore...

HTHs

Col


-- 

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
  Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
  Mandriva Linux Contributor [http://www.mandriva.com/]
  PulseAudio Hacker [http://www.pulseaudio.org/]
  Trac Hacker [http://trac.edgewall.org/]




More information about the pulseaudio-discuss mailing list