diff options
-rw-r--r-- | data/com.canonical.indicator.sound.gschema.xml | 9 | ||||
-rw-r--r-- | data/indicator-sound.desktop.in | 2 | ||||
-rw-r--r-- | data/indicator-sound.upstart.desktop.in | 2 | ||||
-rw-r--r-- | debian/changelog | 28 | ||||
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/accounts-service-user.vala | 4 | ||||
-rw-r--r-- | src/service.vala | 177 | ||||
-rw-r--r-- | src/sound-menu.vala | 1 | ||||
-rw-r--r-- | src/volume-control.vala | 146 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 26 | ||||
-rw-r--r-- | tests/pa-mock.cpp | 571 | ||||
-rw-r--r-- | tests/volume-control-test.cc | 83 |
12 files changed, 879 insertions, 172 deletions
diff --git a/data/com.canonical.indicator.sound.gschema.xml b/data/com.canonical.indicator.sound.gschema.xml index 102a1db..a346c0d 100644 --- a/data/com.canonical.indicator.sound.gschema.xml +++ b/data/com.canonical.indicator.sound.gschema.xml @@ -32,15 +32,6 @@ On start up volume should not be muted. </description> </key> - <key name="show-notify-osd-on-scroll" type="b"> - <default>true</default> - <summary>Initial setting for showing notify-osd notification on scroll volume-change</summary> - <description> - When using the mouse scroll-wheel over the indicator-sound icon, the volume changes. - Enabling this setting, every scroll volume-change a notify-osd bubble with the - updated volume value will be shown (if supported by your notification daemon). - </description> - </key> <key name="visible" type="b"> <default>true</default> <summary>Whether or not to show the sound indicator in the menu bar.</summary> diff --git a/data/indicator-sound.desktop.in b/data/indicator-sound.desktop.in index 61c049c..6d31a9a 100644 --- a/data/indicator-sound.desktop.in +++ b/data/indicator-sound.desktop.in @@ -2,7 +2,7 @@ Type=Application Name=Indicator Sound Exec=@CMAKE_INSTALL_FULL_LIBEXECDIR@/indicator-sound/indicator-sound-service -OnlyShowIn=Unity;XFCE;GNOME;Pantheon; +OnlyShowIn=Unity;GNOME;Pantheon; NoDisplay=true StartupNotify=false Terminal=false diff --git a/data/indicator-sound.upstart.desktop.in b/data/indicator-sound.upstart.desktop.in index 4c3ebc9..0380ab8 100644 --- a/data/indicator-sound.upstart.desktop.in +++ b/data/indicator-sound.upstart.desktop.in @@ -2,7 +2,7 @@ Type=Application Name=Indicator Sound Exec=@CMAKE_INSTALL_FULL_LIBEXECDIR@/indicator-sound/indicator-sound-service -OnlyShowIn=Unity;XFCE;Pantheon; +OnlyShowIn=Unity;Pantheon; NoDisplay=true StartupNotify=false Terminal=false diff --git a/debian/changelog b/debian/changelog index 93626ad..6482f8a 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,31 @@ +indicator-sound (12.10.2+15.04.20150128-0ubuntu1) vivid; urgency=low + + [ Ted Gould ] + * Remove XFCE from the OnlyShowIn list (LP: #1411959) + + -- Ubuntu daily release <ps-jenkins@lists.canonical.com> Wed, 28 Jan 2015 21:16:55 +0000 + +indicator-sound (12.10.2+15.04.20150120-0ubuntu1) vivid; urgency=low + + [ Ted Gould ] + * Set internal silent mode variable at startup (LP: #1391164) + + [ CI bot ] + * Set internal silent mode variable at startup (LP: #1391164) + + [ Nick Dedekind ] + * Update the panel icon when entering/exiting silent mode. (LP: + #1390067) + + [ Dmitry Shachnev ] + * Fix indicator-sound-service crash when active_port is null. (LP: + #1395455) + + [ Lars Uebernickel ] + * Set internal silent mode variable at startup (LP: #1391164) + + -- Ubuntu daily release <ps-jenkins@lists.canonical.com> Tue, 20 Jan 2015 22:03:19 +0000 + indicator-sound (12.10.2+15.04.20141105-0ubuntu1) vivid; urgency=medium [ Ted Gould ] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c18726c..b6f006a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -163,7 +163,6 @@ add_library( target_link_libraries( indicator-sound-service-lib - ${PULSEAUDIO_LIBRARIES} ${SOUNDSERVICE_LIBRARIES} ) @@ -187,6 +186,7 @@ set_target_properties( target_link_libraries( indicator-sound-service-bin indicator-sound-service-lib + ${PULSEAUDIO_LIBRARIES} ) ########################### diff --git a/src/accounts-service-user.vala b/src/accounts-service-user.vala index ec52730..e8db7c4 100644 --- a/src/accounts-service-user.vala +++ b/src/accounts-service-user.vala @@ -35,6 +35,7 @@ public class AccountsServiceUser : Object { return _silentMode; } set { + _silentMode = value; if (syssoundproxy != null) syssoundproxy.silent_mode = value; } @@ -211,7 +212,8 @@ public class AccountsServiceUser : Object { } }); - this.silentMode = this.syssoundproxy.silent_mode; + this._silentMode = this.syssoundproxy.silent_mode; + this.notify_property("silentMode"); } catch (Error e) { this.syssoundproxy = null; warning("Unable to get proxy to system sound settings: %s", e.message); diff --git a/src/service.vala b/src/service.vala index fd0c08d..fb56215 100644 --- a/src/service.vala +++ b/src/service.vala @@ -19,6 +19,13 @@ public class IndicatorSound.Service: Object { public Service (MediaPlayerList playerlist) { + sync_notification = new Notify.Notification(_("Volume"), "", "audio-volume-muted"); + this.notification_server_watch = GLib.Bus.watch_name(GLib.BusType.SESSION, + "org.freedesktop.Notifications", + GLib.BusNameWatcherFlags.NONE, + () => { check_sync_notification = false; }, + () => { check_sync_notification = false; }); + this.settings = new Settings ("com.canonical.indicator.sound"); this.sharedsettings = new Settings ("com.ubuntu.sound"); @@ -49,7 +56,7 @@ public class IndicatorSound.Service: Object { this.actions.add_action (this.create_mute_action ()); this.actions.add_action (this.create_volume_action ()); this.actions.add_action (this.create_mic_volume_action ()); - this.actions.add_action (this.create_high_volume_actions ()); + this.actions.add_action (this.create_high_volume_action ()); this.menus = new HashTable<string, SoundMenu> (str_hash, str_equal); this.menus.insert ("desktop_greeter", new SoundMenu (null, SoundMenu.DisplayFlags.SHOW_MUTE | SoundMenu.DisplayFlags.HIDE_PLAYERS | SoundMenu.DisplayFlags.GREETER_PLAYERS)); @@ -70,15 +77,19 @@ public class IndicatorSound.Service: Object { this.sync_preferred_players (); }); - if (settings.get_boolean ("show-notify-osd-on-scroll")) { - List<string> caps = Notify.get_server_caps (); - if (caps.find_custom ("x-canonical-private-synchronous", strcmp) != null) { - this.notification = new Notify.Notification ("indicator-sound", "", ""); - this.notification.set_hint ("x-canonical-private-synchronous", "indicator-sound"); - } - } - sharedsettings.bind ("allow-amplified-volume", this, "allow-amplified-volume", SettingsBindFlags.GET); + + /* Hide the notification when the menu is shown */ + var shown_action = actions.lookup_action ("indicator-shown") as SimpleAction; + shown_action.change_state.connect ((state) => { + if (state.get_boolean()) { + try { + sync_notification.close(); + } catch (Error e) { + warning("Unable to close synchronous volume notification: %s", e.message); + } + } + }); } ~Service() { @@ -86,6 +97,11 @@ public class IndicatorSound.Service: Object { Source.remove (this.sound_was_blocked_timeout_id); this.sound_was_blocked_timeout_id = 0; } + + if (this.notification_server_watch != 0) { + GLib.Bus.unwatch_name(this.notification_server_watch); + this.notification_server_watch = 0; + } } bool greeter_show_track () { @@ -143,7 +159,7 @@ public class IndicatorSound.Service: Object { } /* Normalize volume, because the volume action's state is [0.0, 1.0], see create_volume_action() */ - this.actions.change_action_state ("volume", this.volume_control.get_volume () / this.max_volume); + this.actions.change_action_state ("volume", this.volume_control.volume / this.max_volume); } } @@ -152,6 +168,7 @@ public class IndicatorSound.Service: Object { { "scroll", activate_scroll_action, "i", null, null }, { "desktop-settings", activate_desktop_settings, null, null, null }, { "phone-settings", activate_phone_settings, null, null, null }, + { "indicator-shown", null, null, "@b false", null }, }; MainLoop loop; @@ -164,10 +181,11 @@ public class IndicatorSound.Service: Object { uint player_action_update_id; bool mute_blocks_sound; uint sound_was_blocked_timeout_id; - Notify.Notification notification; bool syncing_preferred_players = false; AccountsServiceUser? accounts_service = null; bool export_to_accounts_service = false; + private Notify.Notification sync_notification; + private uint notification_server_watch; /* Maximum volume as a scaling factor between the volume action's state and the value in * this.volume_control. See create_volume_action(). @@ -179,31 +197,8 @@ public class IndicatorSound.Service: Object { void activate_scroll_action (SimpleAction action, Variant? param) { int delta = param.get_int32(); /* positive for up, negative for down */ - double v = this.volume_control.get_volume () + volume_step_percentage * delta; - this.volume_control.set_volume (v.clamp (0.0, this.max_volume)); - - /* TODO: Don't want to mess up the desktop today, but we should remove this - scrolling change and merge that into volume control's notification */ - if (this.notification != null) { - string icon; - if (v <= 0.0) - icon = "notification-audio-volume-off"; - else if (v <= 0.3) - icon = "notification-audio-volume-low"; - else if (v <= 0.7) - icon = "notification-audio-volume-medium"; - else - icon = "notification-audio-volume-high"; - - this.notification.update ("indicator-sound", "", icon); - this.notification.set_hint ("value", ((int32) (100 * v / this.max_volume)).clamp (-1, 101)); - try { - this.notification.show (); - } - catch (Error e) { - warning ("unable to show notification: %s", e.message); - } - } + double v = this.volume_control.volume + volume_step_percentage * delta; + this.volume_control.volume = v.clamp (0.0, this.max_volume); } void activate_desktop_settings (SimpleAction action, Variant? param) { @@ -238,10 +233,12 @@ public class IndicatorSound.Service: Object { } void update_root_icon () { - double volume = this.volume_control.get_volume (); + double volume = this.volume_control.volume; string icon; if (this.volume_control.mute) icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel"; + else if (this.accounts_service != null && this.accounts_service.silentMode) + icon = "audio-volume-muted-panel"; else if (volume <= 0.0) icon = "audio-volume-low-zero-panel"; else if (volume <= 0.3) @@ -254,6 +251,9 @@ public class IndicatorSound.Service: Object { string accessible_name; if (this.volume_control.mute) { accessible_name = _("Volume (muted)"); + } else if (this.accounts_service != null && this.accounts_service.silentMode) { + int volume_int = (int)(volume * 100); + accessible_name = "%s (%s %d%%)".printf (_("Volume"), _("silent"), volume_int); } else { int volume_int = (int)(volume * 100); accessible_name = "%s (%d%%)".printf (_("Volume"), volume_int); @@ -268,6 +268,62 @@ public class IndicatorSound.Service: Object { root_action.set_state (builder.end()); } + private bool check_sync_notification = false; + private bool support_sync_notification = false; + + void update_sync_notification () { + if (!check_sync_notification) { + List<string> caps = Notify.get_server_caps (); + if (caps.find_custom ("x-canonical-private-synchronous", strcmp) != null) { + support_sync_notification = true; + } + check_sync_notification = true; + } + + if (!support_sync_notification) + return; + + var shown_action = actions.lookup_action ("indicator-shown") as SimpleAction; + if (shown_action != null && shown_action.get_state().get_boolean()) + return; + + /* Determine Label */ + string volume_label = ""; + if (volume_control.high_volume) + volume_label = _("High volume"); + + /* Choose an icon */ + string icon = "audio-volume-muted"; + if (volume_control.volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume_control.volume <= 0.3) + icon = "audio-volume-low"; + else if (volume_control.volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + + /* Check tint */ + string tint = "false"; + if (volume_control.high_volume) + tint = "true"; + + /* Put it all into the notification */ + sync_notification.clear_hints (); + sync_notification.update (_("Volume"), volume_label, icon); + sync_notification.set_hint ("value", (int32)(volume_control.volume * 100.0)); + sync_notification.set_hint ("x-canonical-value-bar-tint", tint); + sync_notification.set_hint ("x-canonical-private-synchronous", "true"); + sync_notification.set_hint ("x-canonical-non-shaped-icon", "true"); + + /* Show it */ + try { + sync_notification.show (); + } catch (GLib.Error e) { + warning("Unable to send volume change notification: %s", e.message); + } + } + Action create_silent_mode_action () { bool silentNow = false; if (this.accounts_service != null) { @@ -342,15 +398,6 @@ public class IndicatorSound.Service: Object { return mute_action; } - void volume_changed (double volume) { - var volume_action = this.actions.lookup_action ("volume") as SimpleAction; - - /* Normalize volume, because the volume action's state is [0.0, 1.0], see create_volume_action() */ - volume_action.set_state (new Variant.double (volume / this.max_volume)); - - this.update_root_icon (); - } - Action create_volume_action () { /* The action's state is between be in [0.0, 1.0] instead of [0.0, * max_volume], so that we don't need to update the slider menu item @@ -359,23 +406,31 @@ public class IndicatorSound.Service: Object { * volume_control.set_volume(). */ - double volume = this.volume_control.get_volume () / this.max_volume; + double volume = this.volume_control.volume / this.max_volume; var volume_action = new SimpleAction.stateful ("volume", VariantType.INT32, new Variant.double (volume)); volume_action.change_state.connect ( (action, val) => { double v = val.get_double () * this.max_volume; - volume_control.set_volume (v.clamp (0.0, this.max_volume)); + volume_control.volume = v.clamp (0.0, this.max_volume); }); /* activating this action changes the volume by the amount given in the parameter */ volume_action.activate.connect ( (action, param) => { int delta = param.get_int32 (); - double v = volume_control.get_volume () + volume_step_percentage * delta; - volume_control.set_volume (v.clamp (0.0, this.max_volume)); + double v = volume_control.volume + volume_step_percentage * delta; + volume_control.volume = v.clamp (0.0, this.max_volume); }); - this.volume_control.volume_changed.connect (volume_changed); + this.volume_control.notify["volume"].connect (() => { + var vol_action = this.actions.lookup_action ("volume") as SimpleAction; + + /* Normalize volume, because the volume action's state is [0.0, 1.0], see create_volume_action() */ + vol_action.set_state (new Variant.double (this.volume_control.volume / this.max_volume)); + + this.update_root_icon (); + this.update_sync_notification (); + }); this.volume_control.bind_property ("ready", volume_action, "enabled", BindingFlags.SYNC_CREATE); @@ -383,14 +438,14 @@ public class IndicatorSound.Service: Object { } Action create_mic_volume_action () { - var volume_action = new SimpleAction.stateful ("mic-volume", null, new Variant.double (this.volume_control.get_mic_volume ())); + var volume_action = new SimpleAction.stateful ("mic-volume", null, new Variant.double (this.volume_control.mic_volume)); volume_action.change_state.connect ( (action, val) => { - volume_control.set_mic_volume (val.get_double ()); + volume_control.mic_volume = val.get_double (); }); - this.volume_control.mic_volume_changed.connect ( (volume) => { - volume_action.set_state (new Variant.double (volume)); + this.volume_control.notify["mic-volume"].connect ( () => { + volume_action.set_state (new Variant.double (this.volume_control.mic_volume)); }); this.volume_control.bind_property ("ready", volume_action, "enabled", BindingFlags.SYNC_CREATE); @@ -398,11 +453,13 @@ public class IndicatorSound.Service: Object { return volume_action; } - Action create_high_volume_actions () { + Action create_high_volume_action () { var high_volume_action = new SimpleAction.stateful("high-volume", null, new Variant.boolean (this.volume_control.high_volume)); - this.volume_control.notify["high-volume"].connect( () => - high_volume_action.set_state(new Variant.boolean (this.volume_control.high_volume))); + this.volume_control.notify["high-volume"].connect( () => { + high_volume_action.set_state(new Variant.boolean (this.volume_control.high_volume)); + update_sync_notification(); + }); return high_volume_action; } @@ -443,13 +500,13 @@ public class IndicatorSound.Service: Object { action.set_state (this.action_state_for_player (player)); action.set_enabled (player.can_raise); } - + SimpleAction? greeter_action = this.actions.lookup_action (player.id + ".greeter") as SimpleAction; if (greeter_action != null) { greeter_action.set_state (this.action_state_for_player (player, greeter_show_track())); greeter_action.set_enabled (player.can_raise); } - + /* If we're playing then put that data in accounts service */ if (player.is_running && export_to_accounts_service && accounts_service != null) { accounts_service.player = player; diff --git a/src/sound-menu.vala b/src/sound-menu.vala index 3881faf..96dd143 100644 --- a/src/sound-menu.vala +++ b/src/sound-menu.vala @@ -60,6 +60,7 @@ public class SoundMenu: Object root_item.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.root"); root_item.set_attribute ("x-canonical-scroll-action", "s", "indicator.scroll"); root_item.set_attribute ("x-canonical-secondary-action", "s", "indicator.mute"); + root_item.set_attribute ("submenu-action", "s", "indicator.indicator-shown"); root_item.set_submenu (this.menu); this.root = new Menu (); diff --git a/src/volume-control.vala b/src/volume-control.vala index 6f22dc5..c5e1883 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -68,12 +68,8 @@ public class VolumeControl : Object private uint _accountservice_volume_timer = 0; private bool _send_next_local_volume = false; private double _account_service_volume = 0.0; - private Notify.Notification _notification; private bool _active_port_headphone = false; - public signal void volume_changed (double v); - public signal void mic_volume_changed (double v); - /** true when connected to the pulse server */ public bool ready { get; set; } @@ -81,7 +77,11 @@ public class VolumeControl : Object public bool active_mic { get; private set; default = false; } /** true when high volume warnings should be shown */ - public bool high_volume { get; set; } + public bool high_volume { + get { + return this._volume > 0.75 && _active_port_headphone; + } + } public VolumeControl () { @@ -91,12 +91,6 @@ public class VolumeControl : Object _mute_cancellable = new Cancellable (); _volume_cancellable = new Cancellable (); - Notify.init ("Volume"); - _notification = new Notify.Notification(_("Volume"), "", "audio-volume-muted"); - _notification.set_hint ("value", 0); - _notification.set_hint ("x-canonical-private-synchronous", "true"); - _notification.set_hint ("x-canonical-non-shaped-icon", "true"); - setup_accountsservice.begin (); this.reconnect_to_pulse (); @@ -162,7 +156,7 @@ public class VolumeControl : Object private void sink_info_cb_for_props (Context c, SinkInfo? i, int eol) { - bool old_active_port_headphone = this._active_port_headphone; + bool old_high_volume = this.high_volume; if (i == null) return; @@ -185,9 +179,10 @@ public class VolumeControl : Object * checking for the port name. On touch (with the pulseaudio droid element) * the headset/headphone port is called 'output-headset' and 'output-headphone'. * On the desktop this is usually called 'analog-output-headphones' */ - if (i.active_port.name == "output-wired_headset" || + if (i.active_port != null && ( + i.active_port.name == "output-wired_headset" || i.active_port.name == "output-wired_headphone" || - i.active_port.name == "analog-output-headphones") { + i.active_port.name == "analog-output-headphones")) { _active_port_headphone = true; } else { _active_port_headphone = false; @@ -197,10 +192,12 @@ public class VolumeControl : Object _volume != volume_to_double (i.volume.max ())) { _volume = volume_to_double (i.volume.max ()); - volume_changed (_volume); + this.notify_property("volume"); start_local_volume_timer(); - } else if (this._active_port_headphone != old_active_port_headphone) { - volume_changed (_volume); + } + + if (this.high_volume != old_high_volume) { + this.notify_property("high-volume"); } } @@ -212,7 +209,7 @@ public class VolumeControl : Object if (_mic_volume != volume_to_double (i.volume.values[0])) { _mic_volume = volume_to_double (i.volume.values[0]); - mic_volume_changed (_mic_volume); + this.notify_property ("mic-volume"); } } @@ -268,7 +265,7 @@ public class VolumeControl : Object if (volume != cvolume) { /* Someone else changed the volume for this role, reflect on the indicator */ _volume = volume_to_double (volume); - volume_changed (_volume); + this.notify_property("volume"); start_local_volume_timer(); } } @@ -311,7 +308,7 @@ public class VolumeControl : Object iter.next ("(uu)", &type, &volume); _volume = volume_to_double (volume); - volume_changed (_volume); + this.notify_property("volume"); start_local_volume_timer(); } catch (GLib.Error e) { warning ("unable to get volume for active role %s (%s)", sink_input_objp, e.message); @@ -528,7 +525,7 @@ public class VolumeControl : Object private void set_volume_success_cb (Context c, int success) { if ((bool)success) - volume_changed (_volume); + this.notify_property("volume"); } private void sink_info_set_volume_cb (Context c, SinkInfo? i, int eol) @@ -574,7 +571,7 @@ public class VolumeControl : Object new Variant ("(ssv)", "org.PulseAudio.Ext.StreamRestore1.RestoreEntry", "Volume", volume), null, DBusCallFlags.NONE, -1); - volume_changed (_volume); + this.notify_property("volume"); } catch (GLib.Error e) { lock (_pa_volume_sig_count) { _pa_volume_sig_count--; @@ -589,83 +586,29 @@ public class VolumeControl : Object return false; if (_volume != volume) { + var old_high_volume = this.high_volume; + _volume = volume; if (_pulse_use_stream_restore) set_volume_active_role.begin (); else context.get_server_info (server_info_cb_for_set_volume); + + this.notify_property("volume"); + + if (this.high_volume != old_high_volume) + this.notify_property("high-volume"); + return true; } else { return false; } } - public void set_volume (double volume) - { - /* Using this to detect whether we're on the phone or not */ - if (_pulse_use_stream_restore) { - /* Watch for extreme */ - if (volume > 0.75 && _active_port_headphone) - high_volume = true; - else - high_volume = false; - - /* Determine Label */ - string volume_label = ""; - if (high_volume) - volume_label = _("High volume"); - - /* Choose an icon */ - string icon = "audio-volume-muted"; - if (volume <= 0.0) - icon = "audio-volume-muted"; - else if (volume <= 0.3) - icon = "audio-volume-low"; - else if (volume <= 0.7) - icon = "audio-volume-medium"; - else - icon = "audio-volume-high"; - - /* Choose a sound */ - string? sound = null; - if (!((_active_sink_input >= 0) && (_active_sink_input < _valid_roles.length) - && (_valid_roles[_active_sink_input] == "multimedia"))) - sound = "/usr/share/sounds/ubuntu/stereo/message.ogg"; - - /* Check tint */ - string tint = "false"; - if (high_volume) - tint = "true"; - - /* Put it all into the notification */ - _notification.clear_hints (); - _notification.update (_("Volume"), volume_label, icon); - _notification.set_hint ("value", (int32)(volume * 100.0)); - /* TODO: Removing sound until we can get all the roles cleaned up for - when to play it. We expect this to come back, but in another landing. - _notification.set_hint ("sound-file", sound); - */ - _notification.set_hint ("x-canonical-value-bar-tint", tint); - _notification.set_hint ("x-canonical-private-synchronous", "true"); - _notification.set_hint ("x-canonical-non-shaped-icon", "true"); - - /* Show it */ - try { - _notification.show (); - } catch (GLib.Error e) { - warning("Unable to send volume change notification: %s", e.message); - } - } - - if (set_volume_internal (volume)) { - start_local_volume_timer(); - } - } - void set_mic_volume_success_cb (Context c, int success) { if ((bool)success) - mic_volume_changed (_mic_volume); + this.notify_property ("mic-volume"); } void set_mic_volume_get_server_info_cb (PulseAudio.Context c, PulseAudio.ServerInfo? i) { @@ -676,23 +619,28 @@ public class VolumeControl : Object } } - public void set_mic_volume (double volume) - { - return_if_fail (context.get_state () == Context.State.READY); - - _mic_volume = volume; - - context.get_server_info (set_mic_volume_get_server_info_cb); + public double volume { + get { + return _volume; + } + set { + if (set_volume_internal (value)) { + start_local_volume_timer(); + } + } } - public double get_volume () - { - return _volume; - } + public double mic_volume { + get { + return _mic_volume; + } + set { + return_if_fail (context.get_state () == Context.State.READY); - public double get_mic_volume () - { - return _mic_volume; + _mic_volume = value; + + context.get_server_info (set_mic_volume_get_server_info_cb); + } } /* PulseAudio Dbus (Stream Restore) logic */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ae68c45..fb93aca 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -83,6 +83,16 @@ target_link_libraries( include_directories(${CMAKE_CURRENT_BINARY_DIR}) ########################### +# Pulse Mock +########################### + +add_library( + pulse-mock + SHARED + pa-mock.cpp +) + +########################### # Name Watch Test ########################### @@ -116,6 +126,22 @@ add_test(accounts-service-user-test-player ) ########################### +# Volume Control +########################### + +include_directories(${CMAKE_SOURCE_DIR}/src) +add_executable (volume-control-test volume-control-test.cc) +target_link_libraries ( + volume-control-test + indicator-sound-service-lib + pulse-mock + gtest + ${TEST_LIBRARIES} +) + +add_test(volume-control-test volume-control-test) + +########################### # Sound Menu ########################### diff --git a/tests/pa-mock.cpp b/tests/pa-mock.cpp new file mode 100644 index 0000000..5dd5c9b --- /dev/null +++ b/tests/pa-mock.cpp @@ -0,0 +1,571 @@ +/* + * Copyright © 2015 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Ted Gould <ted@canonical.com> + */ + +#include <atomic> +#include <functional> +#include <vector> + +#include <pulse/pulseaudio.h> +#include <pulse/glib-mainloop.h> +#include <gio/gio.h> +#include <math.h> + +#ifdef G_LOG_DOMAIN +#undef G_LOG_DOMAIN +#endif +#define G_LOG_DOMAIN "PA-Mock" + +/* Core class of the PA Mock state */ +class PAMockContext { +public: + /* Accounting */ + std::atomic<unsigned int> refcnt; + + /* State stuff */ + std::vector<std::function<void(void)>> stateCallbacks; + pa_context_state_t currentState; + pa_context_state_t futureState; + + /* Event stuff */ + std::vector<std::function<void(pa_subscription_event_type_t, uint32_t)>> eventCallbacks; + pa_subscription_mask_t eventMask; + + PAMockContext () + : refcnt(1) + , currentState(PA_CONTEXT_UNCONNECTED) + , futureState(PA_CONTEXT_UNCONNECTED) + , eventMask(PA_SUBSCRIPTION_MASK_NULL) + { + g_debug("Creating Context: %p", this); + } + +private: /* To ensure we're the only ones who can delete it */ + ~PAMockContext () { + g_debug("Destroying Context: %p", this); + } + +public: + /* Ref counting */ + void ref () { + refcnt++; + } + + void unref () { + refcnt--; + if (refcnt == 0) + delete this; + } + + /* C/C++ boundry */ + operator pa_context *() { + return reinterpret_cast<pa_context *>(this); + } + + /* State Stuff */ + void setState (pa_context_state_t state) + { + if (state == currentState) + return; + + currentState = state; + for (auto callback : stateCallbacks) { + callback(); + } + } + + void idleOnce (std::function<void(void)> idleFunc) { + auto allocated = new std::function<void(void)>(idleFunc); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + [](gpointer data) -> gboolean { + std::function<void(void)> * pidleFunc = reinterpret_cast<std::function<void(void)> *>(data); + (*pidleFunc)(); + return G_SOURCE_REMOVE; + }, + allocated, + [](gpointer data) -> void { + std::function<void(void)> * pidleFunc = reinterpret_cast<std::function<void(void)> *>(data); + delete pidleFunc; + }); + } + + void queueState (pa_context_state_t state) + { + idleOnce([this, state](){ + setState(state); + }); + } + + pa_context_state_t getState (void) + { + return currentState; + } + + void addStateCallback (std::function<void(void)> &callback) + { + stateCallbacks.push_back(callback); + } + + /* Event Stuff */ + void setMask (pa_subscription_mask_t mask) + { + eventMask = mask; + } + + void addEventCallback (std::function<void(pa_subscription_event_type_t, uint32_t)> &callback) + { + eventCallbacks.push_back(callback); + } +}; + +/* ******************************* + * context.h + * *******************************/ + +pa_context * +pa_context_new_with_proplist (pa_mainloop_api *mainloop, const char *name, pa_proplist *proplist) +{ + return *(new PAMockContext()); +} + +void +pa_context_unref (pa_context *c) { + reinterpret_cast<PAMockContext*>(c)->unref(); +} + +pa_context * +pa_context_ref (pa_context *c) { + reinterpret_cast<PAMockContext*>(c)->ref(); + return c; +} + +int +pa_context_connect (pa_context *c, const char *server, pa_context_flags_t flags, const pa_spawn_api *api) +{ + reinterpret_cast<PAMockContext*>(c)->queueState(PA_CONTEXT_READY); + return 0; +} + +void +pa_context_disconnect (pa_context *c) +{ + reinterpret_cast<PAMockContext*>(c)->queueState(PA_CONTEXT_UNCONNECTED); +} + +int +pa_context_errno (pa_context *c) +{ + return 0; +} + +void +pa_context_set_state_callback (pa_context *c, pa_context_notify_cb_t cb, void *userdata) +{ + std::function<void(void)> cppcb([c, cb, userdata]() { + cb(c, userdata); + }); + reinterpret_cast<PAMockContext*>(c)->addStateCallback(cppcb); +} + +pa_context_state_t +pa_context_get_state (pa_context *c) +{ + return reinterpret_cast<PAMockContext*>(c)->getState(); +} + +/* ******************************* + * introspect.h + * *******************************/ + +static pa_operation * +dummy_operation (void) +{ + GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +pa_operation* +pa_context_get_server_info (pa_context *c, pa_server_info_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, cb, userdata]() { + if (cb == nullptr) + return; + + pa_server_info server{ + .user_name = "user", + .host_name = "host", + .server_version = "1.2.3", + .server_name = "server", + .sample_spec = { + .format = PA_SAMPLE_U8, + .rate = 44100, + .channels = 1 + }, + .default_sink_name = "default-sink", + .default_source_name = "default-source", + .cookie = 1234, + .channel_map = { + .channels = 0 + } + }; + + cb(c, &server, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_get_sink_info_by_name (pa_context *c, const gchar * name, pa_sink_info_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, name, cb, userdata]() { + if (cb == nullptr) + return; + + pa_sink_port_info active_port = {0}; + active_port.name = "speaker"; + + pa_sink_info sink = {0}; + sink.name = "default-sink"; + sink.index = 0; + sink.description = "Default Sink"; + sink.channel_map.channels = 0; + sink.active_port = &active_port; + + cb(c, &sink, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_get_sink_info_list (pa_context *c, pa_sink_info_cb_t cb, void *userdata) +{ + /* Only have one today, so this is the same */ + return pa_context_get_sink_info_by_name(c, "default-sink", cb, userdata); +} + +pa_operation * +pa_context_get_sink_input_info (pa_context *c, uint32_t idx, pa_sink_input_info_cb_t cb, void * userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, idx, cb, userdata]() { + if (cb == nullptr) + return; + + pa_sink_input_info sink = { 0 }; + + cb(c, &sink, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_get_source_info_by_name (pa_context *c, const char * name, pa_source_info_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, name, cb, userdata]() { + if (cb == nullptr) + return; + + pa_source_info source = { + .name = "default-source" + }; + + cb(c, &source, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_get_source_output_info (pa_context *c, uint32_t idx, pa_source_output_info_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, idx, cb, userdata]() { + if (cb == nullptr) + return; + + pa_source_output_info source = {0}; + source.name = "default source"; + + cb(c, &source, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_set_sink_mute_by_index (pa_context *c, uint32_t idx, int mute, pa_context_success_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, idx, mute, cb, userdata]() { + if (cb != nullptr) + cb(c, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_set_sink_volume_by_index (pa_context *c, uint32_t idx, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, idx, cvol, cb, userdata]() { + if (cb != nullptr) + cb(c, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_set_source_volume_by_name (pa_context *c, const char * name, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, name, cvol, cb, userdata]() { + if (cb != nullptr) + cb(c, 1, userdata); + }); + + return dummy_operation(); +} + +/* ******************************* + * subscribe.h + * *******************************/ + +pa_operation * +pa_context_subscribe (pa_context * c, pa_subscription_mask_t mask, pa_context_success_cb_t callback, void * userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, mask, callback, userdata]() { + reinterpret_cast<PAMockContext*>(c)->setMask(mask); + if (callback != nullptr) + callback(c, 1, userdata); + }); + + return dummy_operation(); +} + +void +pa_context_set_subscribe_callback (pa_context * c, pa_context_subscribe_cb_t callback, void * userdata) +{ + std::function<void(pa_subscription_event_type_t, uint32_t)> cppcb([c, callback, userdata](pa_subscription_event_type_t event, uint32_t index) { + if (callback != nullptr) + callback(c, event, index, userdata); + }); + + reinterpret_cast<PAMockContext*>(c)->addEventCallback(cppcb); +} + +/* ******************************* + * glib-mainloop.h + * *******************************/ + +struct pa_glib_mainloop { + GMainContext * context; +}; + +struct pa_mainloop_api mock_mainloop = { 0 }; + +pa_mainloop_api * +pa_glib_mainloop_get_api (pa_glib_mainloop * g) +{ + return &mock_mainloop; +} + +pa_glib_mainloop * +pa_glib_mainloop_new (GMainContext * c) +{ + pa_glib_mainloop * loop = g_new0(pa_glib_mainloop, 1); + + if (c == nullptr) + loop->context = g_main_context_default(); + else + loop->context = c; + + g_main_context_ref(loop->context); + return loop; +} + +void +pa_glib_mainloop_free (pa_glib_mainloop * g) +{ + g_main_context_unref(g->context); + g_free(g); +} + +/* ******************************* + * operation.h + * *******************************/ + +void +pa_operation_unref (pa_operation * operation) +{ + g_return_if_fail(G_IS_OBJECT(operation)); + g_object_unref(G_OBJECT(operation)); +} + +/* ******************************* + * proplist.h + * *******************************/ + +pa_proplist * +pa_proplist_new (void) +{ + return (pa_proplist *)g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); +} + +void +pa_proplist_free (pa_proplist * p) +{ + g_return_if_fail(p != nullptr); + g_hash_table_destroy((GHashTable *)p); +} + +const char * +pa_proplist_gets (pa_proplist * p, const char * key) +{ + g_return_val_if_fail(p != nullptr, nullptr); + g_return_val_if_fail(key != nullptr, nullptr); + return (const char *)g_hash_table_lookup((GHashTable *)p, key); +} + +int +pa_proplist_sets (pa_proplist *p, const char * key, const char * value) +{ + g_return_val_if_fail(p != nullptr, -1); + g_return_val_if_fail(key != nullptr, -1); + g_return_val_if_fail(value != nullptr, -1); + + g_hash_table_insert((GHashTable *)p, g_strdup(key), g_strdup(value)); + return 0; +} + +/* ******************************* + * error.h + * *******************************/ + +const char * +pa_strerror (int error) +{ + return "This is error text"; +} + +/* ******************************* + * volume.h + * *******************************/ + +pa_volume_t +pa_sw_volume_from_dB (double f) +{ + double linear = pow(10.0, f / 20.0); + + pa_volume_t calculated = lround(cbrt(linear) * PA_VOLUME_NORM); + + if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) { + return PA_VOLUME_MAX; + } else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) { + return PA_VOLUME_MUTED; + } else { + return calculated; + } +} + +pa_cvolume * +pa_cvolume_init (pa_cvolume * cvol) +{ + g_return_val_if_fail(cvol != nullptr, nullptr); + + cvol->channels = 0; + + unsigned int i; + for (i = 0; i < PA_CHANNELS_MAX; i++) + cvol->values[i] = PA_VOLUME_INVALID; + + return cvol; +} + +pa_cvolume * +pa_cvolume_set (pa_cvolume * cvol, unsigned channels, pa_volume_t volume) +{ + g_return_val_if_fail(cvol != nullptr, nullptr); + g_warn_if_fail(channels > 0); + g_return_val_if_fail(channels <= PA_CHANNELS_MAX, nullptr); + + cvol->channels = channels; + + unsigned int i; + for (i = 0; i < channels; i++) { + if (G_UNLIKELY(volume > PA_VOLUME_MAX)) { + cvol->values[i] = PA_VOLUME_MAX; + } else if (G_UNLIKELY(volume < PA_VOLUME_MUTED)) { + cvol->values[i] = PA_VOLUME_MUTED; + } else { + cvol->values[i] = volume; + } + } + + return cvol; +} + +pa_volume_t +pa_cvolume_max (const pa_cvolume * cvol) +{ + g_return_val_if_fail(cvol != nullptr, PA_VOLUME_MUTED); + pa_volume_t max = PA_VOLUME_MUTED; + + unsigned int i; + for (i = 0; i < cvol->channels; i++) + max = MAX(max, cvol->values[i]); + + return max; +} + +pa_cvolume * +pa_cvolume_scale (pa_cvolume * cvol, pa_volume_t max) +{ + g_return_val_if_fail(cvol != nullptr, nullptr); + + pa_volume_t originalmax = pa_cvolume_max(cvol); + + if (originalmax <= PA_VOLUME_MUTED) + return pa_cvolume_set(cvol, cvol->channels, max); + + unsigned int i; + for (i = 0; i < cvol->channels; i++) { + pa_volume_t calculated = (cvol->values[i] * max) / originalmax; + + if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) { + cvol->values[i] = PA_VOLUME_MAX; + } else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) { + cvol->values[i] = PA_VOLUME_MUTED; + } else { + cvol->values[i] = calculated; + } + } + + return cvol; +} + diff --git a/tests/volume-control-test.cc b/tests/volume-control-test.cc new file mode 100644 index 0000000..9970241 --- /dev/null +++ b/tests/volume-control-test.cc @@ -0,0 +1,83 @@ +/* + * Copyright © 2014 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Ted Gould <ted@canonical.com> + */ + +#include <gtest/gtest.h> +#include <gio/gio.h> +#include <libdbustest/dbus-test.h> + +extern "C" { +#include "indicator-sound-service.h" +} + +class VolumeControlTest : public ::testing::Test +{ + + protected: + DbusTestService * service = NULL; + GDBusConnection * session = NULL; + + virtual void SetUp() { + service = dbus_test_service_new(NULL); + dbus_test_service_start_tasks(service); + + session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL); + ASSERT_NE(nullptr, session); + g_dbus_connection_set_exit_on_close(session, FALSE); + g_object_add_weak_pointer(G_OBJECT(session), (gpointer *)&session); + } + + virtual void TearDown() { + g_clear_object(&service); + + g_object_unref(session); + + unsigned int cleartry = 0; + while (session != NULL && cleartry < 100) { + loop(100); + cleartry++; + } + + ASSERT_EQ(nullptr, session); + } + + static gboolean timeout_cb (gpointer user_data) { + GMainLoop * loop = static_cast<GMainLoop *>(user_data); + g_main_loop_quit(loop); + return G_SOURCE_REMOVE; + } + + void loop (unsigned int ms) { + GMainLoop * loop = g_main_loop_new(NULL, FALSE); + g_timeout_add(ms, timeout_cb, loop); + g_main_loop_run(loop); + g_main_loop_unref(loop); + } +}; + +TEST_F(VolumeControlTest, BasicObject) { + VolumeControl * control = volume_control_new(); + + /* Setup the PA backend */ + loop(100); + + /* Ready */ + EXPECT_TRUE(volume_control_get_ready(control)); + + g_clear_object(&control); +} |