From 86719275d89bb2c48d1cc9451f6add1918ee0f75 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Mon, 30 Sep 2013 14:13:32 +0200 Subject: Reconnect when pulseaudio terminates (or crashes) --- src/volume-control.vala | 65 ++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 25 deletions(-) (limited to 'src/volume-control.vala') diff --git a/src/volume-control.vala b/src/volume-control.vala index 9475f53..18c407f 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -46,21 +46,7 @@ public class VolumeControl : Object if (loop == null) loop = new PulseAudio.GLibMainLoop (); - var props = new Proplist (); - props.sets (Proplist.PROP_APPLICATION_NAME, "Ubuntu Audio Settings"); - props.sets (Proplist.PROP_APPLICATION_ID, "com.canonical.settings.sound"); - props.sets (Proplist.PROP_APPLICATION_ICON_NAME, "multimedia-volume-control"); - props.sets (Proplist.PROP_APPLICATION_VERSION, "0.1"); - - context = new PulseAudio.Context (loop.get_api(), null, props); - - context.set_state_callback (context_state_callback); - - if (context.connect(null, Context.Flags.NOFAIL, null) < 0) - { - warning( "pa_context_connect() failed: %s\n", PulseAudio.strerror(context.errno())); - return; - } + this.reconnect_to_pulse (); } /* PulseAudio logic*/ @@ -153,18 +139,47 @@ public class VolumeControl : Object private void context_state_callback (Context c) { - if (c.get_state () == Context.State.READY) - { - c.subscribe (PulseAudio.Context.SubscriptionMask.SINK | - PulseAudio.Context.SubscriptionMask.SOURCE | - PulseAudio.Context.SubscriptionMask.SOURCE_OUTPUT); - c.set_subscribe_callback (context_events_cb); - update_sink (); - update_source (); - this.ready = true; + switch (c.get_state ()) { + case Context.State.READY: + c.subscribe (PulseAudio.Context.SubscriptionMask.SINK | + PulseAudio.Context.SubscriptionMask.SOURCE | + PulseAudio.Context.SubscriptionMask.SOURCE_OUTPUT); + c.set_subscribe_callback (context_events_cb); + update_sink (); + update_source (); + this.ready = true; + break; + + case Context.State.FAILED: + case Context.State.TERMINATED: + this.reconnect_to_pulse (); + break; + + default: + this.ready = false; + break; } - else + } + + void reconnect_to_pulse () + { + if (this.ready) { + this.context.disconnect (); + this.context = null; this.ready = false; + } + + var props = new Proplist (); + props.sets (Proplist.PROP_APPLICATION_NAME, "Ubuntu Audio Settings"); + props.sets (Proplist.PROP_APPLICATION_ID, "com.canonical.settings.sound"); + props.sets (Proplist.PROP_APPLICATION_ICON_NAME, "multimedia-volume-control"); + props.sets (Proplist.PROP_APPLICATION_VERSION, "0.1"); + + this.context = new PulseAudio.Context (loop.get_api(), null, props); + this.context.set_state_callback (context_state_callback); + + if (context.connect(null, Context.Flags.NOFAIL, null) < 0) + warning( "pa_context_connect() failed: %s\n", PulseAudio.strerror(context.errno())); } /* Mute operations */ -- cgit v1.2.3