speechd-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] audio/pulse - Attempt to connect to pulseaudio server when d


From: Luke Yelavich
Subject: [PATCH 1/2] audio/pulse - Attempt to connect to pulseaudio server when determining audio output
Date: Fri, 4 Sep 2009 14:10:27 +1000

From: Luke Yelavich <address@hidden>
To: address@hidden

When attempting to make use of PulseAudio for the audio output, make sure
an attempt is made to connect to the server, so that if the server is not
available, an alternative audio output method can be used.
---
 src/audio/pulse.c |   56 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/src/audio/pulse.c b/src/audio/pulse.c
index df19e73..1e12c6f 100644
--- a/src/audio/pulse.c
+++ b/src/audio/pulse.c
@@ -1216,7 +1216,7 @@ int
 pulse_open(AudioID *id, void **pars) 
 {
   ENTER(__FUNCTION__);
-  int ret;
+  int ret, err = 0, state;
 
   if (id == NULL){
     ERR("Can't open PulseAudio sound output, invalid AudioID structure.\n","");
@@ -1269,9 +1269,61 @@ pulse_open(AudioID *id, void **pars)
   id->pulse_pre_buffering = (int) pars[3];
   id->pulse_min_request = (int) pars[4];
 
+  /* Check if the server is running */
+  id->pulse_mainloop = pa_mainloop_new();
+  if (id->pulse_mainloop == NULL)
+    ERR("Failed to allocate main loop","");
+    ret = PULSE_ERROR;
+    goto fail;
+
+  id->pulse_context = pa_context_new(pa_mainloop_get_api(id->pulse_mainloop), 
"speech-dispatcher");
+  if (id->pulse_context == NULL)
+    ERR("Failed to allocate context","");
+    ret = PULSE_ERROR;
+    goto fail;
+
+  err = pa_context_connect(id->pulse_context, id->pulse_server, 0, NULL);
+  if (err < 0)
+    ERR("Failed to connect to server: %s", 
pa_strerror(pa_context_errno(id->pulse_context)));
+    ret = PULSE_ERROR;
+    goto fail;
+
+  do {
+    err = pa_mainloop_prepare(id->pulse_mainloop, -1);
+    if (err < 0)
+      ret = PULSE_ERROR;
+      goto fail;
+
+    err = pa_mainloop_poll(id->pulse_mainloop);
+    if (err < 0)
+      ret = PULSE_ERROR;
+      goto fail;
+
+    err = pa_mainloop_dispatch(id->pulse_mainloop);
+    if (err < 0)
+      ret = PULSE_ERROR;
+      goto fail;
+
+    state = pa_context_get_state(id->pulse_context);
+  } while (state < PA_CONTEXT_AUTHORIZING);
+
+  if (state > PA_CONTEXT_READY)
+    ret = PULSE_ERROR;
+    goto fail;
+
   SHOW("PulseAudio sound output opened\n","");
 
-  return PULSE_OK;
+  ret = PULSE_OK;
+
+  fail:
+    ERR("Failed to connect to pulse server.");
+    if (id->pulse_context != NULL)
+      pa_context_unref(id->pulse_context);
+
+    if (id->pulse_mainloop != NULL)
+      pa_mainloop_free(id->pulse_mainloop);
+
+    return ret;
 }
 
 int
-- 
1.6.3.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]