From 9ccff69efd429c25a0d23b9bf286b9f6e875f8ed Mon Sep 17 00:00:00 2001 From: Yauhen Kharuzhy Date: Thu, 9 May 2019 12:57:30 +0300 Subject: [PATCH] Auto-reconnect to Pulseausio server at connection lost Reconnect to PA if connection was lost. Useful if pulseaudio server has been restarted by some reason. --- src/xvd_pulse.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/xvd_pulse.c b/src/xvd_pulse.c index 8722f37..2f3b971 100644 --- a/src/xvd_pulse.c +++ b/src/xvd_pulse.c @@ -97,7 +97,7 @@ static void xvd_update_source_callback (pa_context *c, int eol, void *userdata); -static gboolean xvd_connect_to_pulse (XvdInstance *i); +static gboolean xvd_connect_to_pulse (gpointer userdata); gboolean @@ -277,14 +277,15 @@ xvd_get_readable_volume (const pa_cvolume *vol) * This function does the context initialization. */ static gboolean -xvd_connect_to_pulse (XvdInstance *i) +xvd_connect_to_pulse (gpointer userdata) { + XvdInstance *i = userdata; pa_context_flags_t flags = PA_CONTEXT_NOFAIL; if (i->pulse_context) { - pa_context_unref (i->pulse_context); - i->pulse_context = NULL; + /* Stop reconnecting timer */ + return FALSE; } i->pulse_context = pa_context_new (pa_glib_mainloop_get_api (i->pa_main_loop), @@ -301,6 +302,12 @@ xvd_connect_to_pulse (XvdInstance *i) { g_warning ("xvd_connect_to_pulse: failed to connect context: %s", pa_strerror (pa_context_errno (i->pulse_context))); + if (pa_context_errno(i->pulse_context) == PA_ERR_INVALID) { + g_debug("pa errno = PA_ERR_INVALID"); + } else { + g_debug("Scheduling reconnect after 5 seconds"); + g_timeout_add_seconds(5, xvd_connect_to_pulse, i); + } return FALSE; } return TRUE; @@ -507,6 +514,9 @@ xvd_context_state_callback (pa_context *c, g_critical("xvd_context_state_callback: The connection failed or was disconnected, is PulseAudio Daemon running?"); i->sink_index = PA_INVALID_INDEX; i->source_index = PA_INVALID_INDEX; + pa_context_unref(i->pulse_context); + i->pulse_context = NULL; + g_timeout_add_seconds(5, xvd_connect_to_pulse, i); break; case PA_CONTEXT_READY: g_debug ("xvd_context_state_callback: The connection is established, the context is ready to execute operations"); -- 2.20.1