diff options
author | Xavi Garcia Mena <xavi.garcia.mena@canonical.com> | 2015-10-02 16:00:31 +0200 |
---|---|---|
committer | Xavi Garcia Mena <xavi.garcia.mena@canonical.com> | 2015-10-02 16:00:31 +0200 |
commit | 0e8f4f68f88e16d06c092d7677d552bf0f614206 (patch) | |
tree | 67c6c1636171ee4fac0c0ab1d1a5b5f14cc24cb9 | |
parent | 471f1fef52392a9bb9455124929b81a716f5566e (diff) | |
parent | 480d998efb8bcd94401ff2d7c40a1fb9b81d8db1 (diff) | |
download | ayatana-indicator-sound-0e8f4f68f88e16d06c092d7677d552bf0f614206.tar.gz ayatana-indicator-sound-0e8f4f68f88e16d06c092d7677d552bf0f614206.tar.bz2 ayatana-indicator-sound-0e8f4f68f88e16d06c092d7677d552bf0f614206.zip |
merged lp:~charlesk/indicator-sound/lp-1481913-integrate-settings-and-clamp-volume
-rw-r--r-- | data/com.canonical.indicator.sound.gschema.xml | 68 | ||||
-rw-r--r-- | debian/changelog | 11 | ||||
-rw-r--r-- | src/service.vala | 210 | ||||
-rw-r--r-- | src/volume-control-pulse.vala | 179 | ||||
-rw-r--r-- | src/volume-control.vala | 14 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/notifications-test.cc | 16 | ||||
-rw-r--r-- | tests/volume-control-mock.vala | 6 |
8 files changed, 349 insertions, 157 deletions
diff --git a/data/com.canonical.indicator.sound.gschema.xml b/data/com.canonical.indicator.sound.gschema.xml index ff4816e..b2ee856 100644 --- a/data/com.canonical.indicator.sound.gschema.xml +++ b/data/com.canonical.indicator.sound.gschema.xml @@ -42,41 +42,65 @@ <!-- VOLUME --> - <key name="high-volume-warning-enabled" type="b"> + <key name="warning-volume-enabled" type="b"> <default>true</default> - <summary>Whether or not to show the a volume warning.</summary> + <summary>Whether or not to show high volume warnings.</summary> <description> - Whether or not to show the a volume warning when the volume exceeds some level while headphones are plugged in. + If enabled on a device, a confirmation dialog will be presented to the user if + (a) something is plugged into the headphone jack (ie, no warnings for speaker volumes) AND + (b) media is playing (ie, no warnings for ringtone volumes) AND + (c) the user attempts to set the volume higher than warning-volume-decibels AND + (d) the user has not indicated approval in the last warning-volume-approval-ttl seconds. </description> </key> - <key name="high-volume-acknowledgment-ttl" type="i"> - <default>1200</default> - <summary>How often, in hours, a user's high volume confirmation should be remembered.</summary> + <key name="warning-volume-confirmation-ttl" type="i"> + <default>72000</default> + <summary>How many seconds a user's warning-volume approval should be remembered.</summary> <description> - After a user confirms that they want to listen at a higher volume, subsequent volume - changes do not need to re-trigger a warning until this interval has passed. - For example, EU standard EN 60950-1/Al2 cites "The acknowledgement does not need to - be repeated more than once every 20 h of cumulative listening time." + How long to remember a user's approval of the confirmation dialog discussed in the + description of 'warning-volume-enabled'. + + The default value (72,000 seconds) corresponds to the 20 hours suggested by + EU standard EN 60950-1/Al2: “The acknowledgement does not need to be repeated + more than once every 20 h of cumulative listening time.” </description> </key> - <key name="high-volume-level" type="d"> - <default>0.75</default> - <summary>Volume level that triggers a high volume warning. [0.0..1.0]</summary><!-- FIXME: decibels would be better --> + <key name="warning-volume-decibels" type="d"> + <default>-8.0</default> + <summary>Volume level that triggers a high volume warning.</summary> <description> - When high volume warnings are enabled, a warning will be shown when - the volume level is raised past this level. + Volume level that triggers a high volume warning. + See warning-volume-enabled for details. </description> </key> -<!-- FIXME: not used yet, needs to be worked into the service.max_volume property wrt allow_amplified_volume . - Also, decibels would be better here - <key name="maximum-volume" type="d"> - <default>1.0</default> - <summary>Maximum volume level, [0.0..1.0]</summary> + + <key name="normal-volume-decibels" type="d"> + <default>0</default> + <summary>Normal output volume.</summary> <description> - Maximum volume level, [0.0..1.0] + The maximum volume when com.ubuntu.sound's 'allow-amplified-volume' setting is disabled. + + To conform with EU standard EN 60950-1/Al2, this value should never be + set to a dB that causes headphone sound output to exceed 100 dBA, nor + electric output to exceed 150 mV. + + The default value (0) corresponds to PulseAudio's PA_VOLUME_NORM. + </description> + </key> + + <key name="amplified-volume-decibels" type="d"> + <default>11</default> + <summary>Output volume to use when com.ubuntu.sound's 'allow-amplified-volume' setting is enabled.</summary> + <description> + The maximum volume when com.ubuntu.sound's 'allow-amplified-volume' setting is enabled. + + To conform with EU standard EN 60950-1/Al2, this value should never be + set to a dB that causes headphone sound output to exceed 100 dBA, nor + electric output to exceed 150 mV. + + The default value (11) corresponds to PulseAudio's PA_VOLUME_UI_MAX. </description> </key> ---> </schema> </schemalist> diff --git a/debian/changelog b/debian/changelog index 33d445d..d89524b 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,14 @@ +indicator-sound (12.10.2+15.10.20150807.6-0ubuntu1) vivid; urgency=medium + + [ CI Train Bot ] + * New rebuild forced. + + [ Charles Kerr ] + * Revised UI volume warnings to comply with EU requirements. (LP: + #1481913) + + -- CI Train Bot <ci-train-bot@canonical.com> Fri, 07 Aug 2015 22:35:58 +0000 + indicator-sound (12.10.2+15.10.20150605-0ubuntu1) vivid; urgency=medium * diff --git a/src/service.vala b/src/service.vala index 73a331a..f17d799 100644 --- a/src/service.vala +++ b/src/service.vala @@ -20,6 +20,13 @@ public class IndicatorSound.Service: Object { DBusConnection bus; + /** + * A copy of volume_control.volume made before just warn_notification + * is shown. Since the volume is clamped during the warning, we cache + * the previous volume to use iff the user hits "OK". + */ + VolumeControl.Volume _pre_warn_volume = null; + public Service (MediaPlayerList playerlist, VolumeControl volume, AccountsServiceUser? accounts) { try { bus = Bus.get_sync(GLib.BusType.SESSION); @@ -33,21 +40,13 @@ public class IndicatorSound.Service: Object { warn_notification.set_hint ("x-canonical-non-shaped-icon", "true"); warn_notification.set_hint ("x-canonical-snap-decisions", "true"); warn_notification.set_hint ("x-canonical-private-affirmative-tint", "true"); - warn_notification.add_action ("ok", _("OK"), (n, a) => { - this.loudness_approved_timestamp = GLib.get_monotonic_time (); - }); - warn_notification.add_action ("cancel", _("Cancel"), (n, a) => { - /* user rejected loud volume; re-clamp to just below the warning level */ - set_clamped_volume (settings.get_double("high-volume-level") * 0.9, VolumeControl.VolumeReasons.USER_KEYPRESS); - }); - + warn_notification.closed.connect((n) => { n.clear_actions(); }); BusWatcher.watch_namespace (GLib.BusType.SESSION, "org.freedesktop.Notifications", () => { debug("Notifications name appeared"); notify_server_caps_checked = false; }, () => { debug("Notifications name vanshed"); notify_server_caps_checked = false; }); this.settings = new Settings ("com.canonical.indicator.sound"); - this.sharedsettings = new Settings ("com.ubuntu.sound"); this.settings.bind ("visible", this, "visible", SettingsBindFlags.GET); this.notify["visible"].connect ( () => this.update_root_icon () ); @@ -96,8 +95,6 @@ public class IndicatorSound.Service: Object { this.sync_preferred_players (); }); - 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) => { @@ -121,7 +118,8 @@ public class IndicatorSound.Service: Object { } private void close_notification(Notify.Notification? n) { - if ((n != null) && (n.id != 0)) { + return_if_fail (n != null); + if (n.id != 0) { try { n.close(); } catch (GLib.Error e) { @@ -131,12 +129,11 @@ public class IndicatorSound.Service: Object { } private void show_notification(Notify.Notification? n) { - if (n != null) { - try { - n.show (); - } catch (GLib.Error e) { - warning ("Unable to show notification: %s", e.message); - } + return_if_fail (n != null); + try { + n.show (); + } catch (GLib.Error e) { + warning ("Unable to show notification: %s", e.message); } } @@ -145,6 +142,8 @@ public class IndicatorSound.Service: Object { clear_acts_player(); + stop_clamp_to_high_timeout(); + if (this.player_action_update_id > 0) { Source.remove (this.player_action_update_id); this.player_action_update_id = 0; @@ -174,28 +173,6 @@ public class IndicatorSound.Service: Object { public bool visible { get; set; } - public bool allow_amplified_volume { - get { - return this.max_volume > 1.0; - } - - set { - if (this.allow_amplified_volume == value) - return; - - if (value) { - /* from pulse/volume.h: #define PA_VOLUME_UI_MAX (pa_sw_volume_from_dB(+11.0)) */ - this.max_volume = (double)PulseAudio.Volume.sw_from_dB(11.0) / PulseAudio.Volume.NORM; - } - else { - this.max_volume = 1.0; - } - - /* 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.volume.volume / this.max_volume); - } - } - const ActionEntry[] action_entries = { { "root", null, null, "@a{sv} {}", null }, { "scroll", activate_scroll_action, "i", null, null }, @@ -207,7 +184,6 @@ public class IndicatorSound.Service: Object { SimpleActionGroup actions; HashTable<string, SoundMenu> menus; Settings settings; - Settings sharedsettings; VolumeControl volume_control; MediaPlayerList players; uint player_action_update_id; @@ -219,24 +195,12 @@ public class IndicatorSound.Service: Object { private Notify.Notification info_notification; private Notify.Notification warn_notification; - /* Maximum volume as a scaling factor between the volume action's state and the value in - * this.volume_control. See create_volume_action(). - */ - double max_volume = 1.0; - const double volume_step_percentage = 0.06; - void set_clamped_volume (double unclamped, VolumeControl.VolumeReasons reason) { - var vol = new VolumeControl.Volume(); - vol.volume = unclamped.clamp (0.0, this.max_volume); - vol.reason = reason; - this.volume_control.volume = vol; - } - - void activate_scroll_action (SimpleAction action, Variant? param) { + private void activate_scroll_action (SimpleAction action, Variant? param) { int delta = param.get_int32(); /* positive for up, negative for down */ - double v = this.volume_control.volume.volume + volume_step_percentage * delta; - set_clamped_volume (v, VolumeControl.VolumeReasons.USER_KEYPRESS); + double v = volume_control.volume.volume + volume_step_percentage * delta; + volume_control.set_volume_clamp (v, VolumeControl.VolumeReasons.USER_KEYPRESS); } void activate_desktop_settings (SimpleAction action, Variant? param) { @@ -278,7 +242,7 @@ public class IndicatorSound.Service: Object { 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"; + icon = "audio-volume-muted-panel"; else if (volume <= 0.0) icon = "audio-volume-low-zero-panel"; else if (volume <= 0.3) @@ -312,17 +276,8 @@ public class IndicatorSound.Service: Object { private bool notify_server_supports_actions = false; private bool notify_server_supports_sync = false; private bool block_info_notifications = false; - private int64 loudness_approved_timestamp = 0; - - private bool user_recently_approved_loudness() { - int64 ttl_sec = this.settings.get_int("high-volume-acknowledgment-ttl"); - int64 ttl_usec = ttl_sec * 1000000; - int64 now = GLib.get_monotonic_time(); - return (this.loudness_approved_timestamp != 0) - && (this.loudness_approved_timestamp + ttl_usec >= now); - } - void update_notification () { + private void update_notification () { if (!notify_server_caps_checked) { List<string> caps = Notify.get_server_caps (); @@ -334,11 +289,28 @@ public class IndicatorSound.Service: Object { var loud = volume_control.high_volume; var warn = loud && this.notify_server_supports_actions - && this.settings.get_boolean("high-volume-warning-enabled") - && !this.user_recently_approved_loudness(); + && !this.volume_control.high_volume_approved; if (warn) { close_notification(info_notification); + if (_pre_warn_volume == null) { + _pre_warn_volume = new VolumeControl.Volume(); + _pre_warn_volume.volume = volume_control.volume.volume; + _pre_warn_volume.reason = volume_control.volume.reason; + } + warn_notification.clear_actions(); + warn_notification.add_action ("ok", _("OK"), (n, a) => { + stop_clamp_to_high_timeout(); + volume_control.approve_high_volume (); + if (_pre_warn_volume != null) { + var tmp = _pre_warn_volume; + _pre_warn_volume = null; + volume_control.volume = tmp; + } + }); + warn_notification.add_action ("cancel", _("Cancel"), (n, a) => { + _pre_warn_volume = null; + }); show_notification(warn_notification); } else { close_notification(warn_notification); @@ -346,23 +318,26 @@ public class IndicatorSound.Service: Object { if (notify_server_supports_sync && !block_info_notifications) { /* Determine Label */ - string volume_label = ""; - if (loud) { - volume_label = _("High volume can damage your hearing."); - } + unowned string volume_label = loud + ? _("High volume can damage your hearing.") + : ""; /* Choose an icon */ - string icon = ""; - if (loud) - icon = "audio-volume-high"; - else if (volume_control.volume.volume <= 0.0) - icon = "audio-volume-muted"; - else if (volume_control.volume.volume <= 0.3) - icon = "audio-volume-low"; - else if (volume_control.volume.volume <= 0.7) - icon = "audio-volume-medium"; - else + unowned string icon; + if (loud) { icon = "audio-volume-high"; + } else { + var volume = volume_control.volume.volume; + + 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"; + } /* Reset the notification */ var n = this.info_notification; @@ -371,7 +346,7 @@ public class IndicatorSound.Service: Object { n.set_hint ("x-canonical-non-shaped-icon", "true"); n.set_hint ("x-canonical-private-synchronous", "true"); n.set_hint ("x-canonical-value-bar-tint", loud ? "true" : "false"); - n.set_hint ("value", (int32)Math.round(volume_control.volume.volume / this.max_volume * 100.0)); + n.set_hint ("value", (int32)Math.round(get_volume_percent() * 100.0)); show_notification(n); } } @@ -453,34 +428,39 @@ public class IndicatorSound.Service: Object { return mute_action; } - SimpleAction volume_action; - 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 - * every time allow-amplified-volume is changed. Convert between the - * two here, so that we always pass the full range into - * volume_control.set_volume(). - */ + /* return the current volume in the range of [0.0, 1.0] */ + private double get_volume_percent() { + return volume_control.volume.volume / this.volume_control.max_volume; + } - double volume = this.volume_control.volume.volume / this.max_volume; + /* volume control's range can vary depending on its max_volume property, + * but the action always needs to be in [0.0, 1.0]... */ + private Variant create_volume_action_state() { + return new Variant.double (get_volume_percent()); + } - volume_action = new SimpleAction.stateful ("volume", VariantType.INT32, new Variant.double (volume)); + private void update_volume_action_state() { + volume_action.set_state(create_volume_action_state()); + } + + private SimpleAction volume_action; + private Action create_volume_action () { + volume_action = new SimpleAction.stateful ("volume", VariantType.INT32, create_volume_action_state()); volume_action.change_state.connect ( (action, val) => { - double v = val.get_double () * this.max_volume; - set_clamped_volume (v, VolumeControl.VolumeReasons.USER_KEYPRESS); + double v = val.get_double () * this.volume_control.max_volume; + volume_control.set_volume_clamp (v, VolumeControl.VolumeReasons.USER_KEYPRESS); }); /* 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.volume.volume + volume_step_percentage * delta; - set_clamped_volume (v, VolumeControl.VolumeReasons.USER_KEYPRESS); + volume_action.activate.connect ((a,p) => activate_scroll_action(a,p)); + + this.volume_control.notify["max-volume"].connect(() => { + update_volume_action_state(); }); this.volume_control.notify["volume"].connect (() => { - /* Normalize volume, because the volume action's state is [0.0, 1.0], see create_volume_action() */ - volume_action.set_state (new Variant.double (this.volume_control.volume.volume / this.max_volume)); + update_volume_action_state(); this.update_root_icon (); @@ -488,6 +468,9 @@ public class IndicatorSound.Service: Object { if (reason == VolumeControl.VolumeReasons.USER_KEYPRESS || reason == VolumeControl.VolumeReasons.DEVICE_OUTPUT_CHANGE) this.update_notification (); + + if ((warn_notification.id != 0) && (_pre_warn_volume != null)) + clamp_to_high_soon(); }); this.volume_control.bind_property ("ready", volume_action, "enabled", BindingFlags.SYNC_CREATE); @@ -641,4 +624,27 @@ public class IndicatorSound.Service: Object { this.update_preferred_players (); } + + /** VOLUME CLAMPING **/ + + private uint _clamp_to_high_timeout = 0; + + private void stop_clamp_to_high_timeout() { + if (_clamp_to_high_timeout != 0) { + Source.remove(_clamp_to_high_timeout); + _clamp_to_high_timeout = 0; + } + } + + private void clamp_to_high_soon() { + const uint interval_msec = 200; + if (_clamp_to_high_timeout == 0) + _clamp_to_high_timeout = Timeout.add(interval_msec, clamp_to_high_idle); + } + + private bool clamp_to_high_idle() { + _clamp_to_high_timeout = 0; + volume_control.clamp_to_high_volume(); + return false; // Source.REMOVE; + } } diff --git a/src/volume-control-pulse.vala b/src/volume-control-pulse.vala index d3e93c5..87af258 100644 --- a/src/volume-control-pulse.vala +++ b/src/volume-control-pulse.vala @@ -28,8 +28,8 @@ extern unowned PulseAudio.CVolume? vol_set (PulseAudio.CVolume? cv, uint channel [DBus (name="com.canonical.UnityGreeter.List")] interface GreeterListInterface : Object { - public abstract async string get_active_entry () throws IOError; - public signal void entry_selected (string entry_name); + public abstract async string get_active_entry () throws IOError; + public signal void entry_selected (string entry_name); } public class VolumeControlPulse : VolumeControl @@ -45,6 +45,7 @@ public class VolumeControlPulse : VolumeControl private VolumeControl.Volume _volume = new VolumeControl.Volume(); private double _mic_volume = 0.0; private Settings _settings = new Settings ("com.canonical.indicator.sound"); + private Settings _shared_settings = new Settings ("com.ubuntu.sound"); /* Used by the pulseaudio stream restore extension */ private DBusConnection _pconn; @@ -93,20 +94,6 @@ public class VolumeControlPulse : VolumeControl /** true when a microphone is active **/ public override bool active_mic { get; private set; default = false; } - /** true when high volume warnings should be shown */ - public override bool high_volume { - get { - if (!_active_port_headphone) { - return false; - } - if (stream != "multimedia") { - return false; - } - var high_volume_level = this._settings.get_double("high-volume-level"); - return this._volume.volume > high_volume_level; - } - } - public VolumeControlPulse () { _volume.volume = 0.0; @@ -118,19 +105,34 @@ public class VolumeControlPulse : VolumeControl _mute_cancellable = new Cancellable (); _volume_cancellable = new Cancellable (); + init_all_properties(); + setup_accountsservice.begin (); this.reconnect_to_pulse (); } + private void init_all_properties() + { + init_max_volume(); + init_high_volume(); + init_high_volume_approved(); + } + ~VolumeControlPulse () { + stop_all_timers(); + } + + private void stop_all_timers() + { if (_reconnect_timer != 0) { Source.remove (_reconnect_timer); _reconnect_timer = 0; } stop_local_volume_timer(); stop_account_service_volume_timer(); + stop_high_volume_approved_timer(); } /* PulseAudio logic*/ @@ -220,7 +222,7 @@ public class VolumeControlPulse : VolumeControl vol.volume = volume_to_double (i.volume.max ()); vol.reason = VolumeControl.VolumeReasons.PULSE_CHANGE; this.volume = vol; - } + } } private void source_info_cb (Context c, SourceInfo? i, int eol) @@ -627,7 +629,6 @@ public class VolumeControlPulse : VolumeControl var volume_changed = (value.volume != _volume.volume); debug("Setting volume to %f for profile %d because %d", value.volume, _active_sink_input, value.reason); - var old_high_volume = this.high_volume; _volume = value; /* Make sure we're connected to Pulse and pulse didn't give us the change */ @@ -639,15 +640,153 @@ public class VolumeControlPulse : VolumeControl else context.get_server_info (server_info_cb_for_set_volume); - if (this.high_volume != old_high_volume) - this.notify_property("high-volume"); if (volume.reason != VolumeControl.VolumeReasons.ACCOUNTS_SERVICE_SET && volume_changed) { start_local_volume_timer(); } + + update_high_volume(); + } + } + + /** MAX VOLUME PROPERTY **/ + + private void init_max_volume() { + _settings.changed["normal-volume-decibels"].connect(() => update_max_volume()); + _settings.changed["amplified-volume-decibels"].connect(() => update_max_volume()); + _shared_settings.changed["allow-amplified-volume"].connect(() => update_max_volume()); + update_max_volume(); + } + private void update_max_volume () { + var new_max_volume = calculate_max_volume(); + if (max_volume != new_max_volume) { + debug("changing max_volume from %f to %f", this.max_volume, new_max_volume); + max_volume = calculate_max_volume(); + } + } + private double calculate_max_volume () { + unowned string decibel_key = _shared_settings.get_boolean("allow-amplified-volume") + ? "amplified-volume-decibels" + : "normal-volume-decibels"; + var volume_dB = _settings.get_double(decibel_key); + var volume_sw = PulseAudio.Volume.sw_from_dB (volume_dB); + return volume_to_double (volume_sw); + } + + /** HIGH VOLUME PROPERTY **/ + + private bool _warning_volume_enabled; + private double _warning_volume_norms; /* 1.0 == PA_VOLUME_NORM */ + private bool _high_volume = false; + public override bool high_volume { + get { return this._high_volume; } + private set { this._high_volume = value; } + } + private void init_high_volume() { + _settings.changed["warning-volume-enabled"].connect(() => update_high_volume_cache()); + _settings.changed["warning-volume-decibels"].connect(() => update_high_volume_cache()); + update_high_volume_cache(); + } + private void update_high_volume_cache() { + var volume_dB = _settings.get_double ("warning-volume-decibels"); + var volume_sw = PulseAudio.Volume.sw_from_dB (volume_dB); + var volume_norms = volume_to_double (volume_sw); + _warning_volume_norms = volume_norms; + _warning_volume_enabled = _settings.get_boolean("warning-volume-enabled"); + debug("updating high volume cache... enabled %d dB %f sw %lu norm %f", (int)_warning_volume_enabled, volume_dB, volume_sw, volume_norms); + update_high_volume(); + } + private void update_high_volume() { + var new_high_volume = calculate_high_volume(); + if (high_volume != new_high_volume) { + debug("changing high_volume from %d to %d", (int)high_volume, (int)new_high_volume); + high_volume = new_high_volume; + } + } + private bool calculate_high_volume() { + return calculate_high_volume_from_volume(_volume.volume); + } + private bool calculate_high_volume_from_volume(double volume) { + return _active_port_headphone + && _warning_volume_enabled + && volume >= _warning_volume_norms + && (stream == "multimedia"); + } + + public override void clamp_to_high_volume() { + if (_high_volume && (_volume.volume > _warning_volume_norms)) { + var vol = new VolumeControl.Volume(); + vol.volume = _volume.volume.clamp(0, _warning_volume_norms); + vol.reason = _volume.reason; + debug("Clamping from %f down to %f", _volume.volume, vol.volume); + volume = vol; + } + } + + /** HIGH VOLUME APPROVED PROPERTY **/ + + private bool _high_volume_approved = false; + private uint _high_volume_approved_timer = 0; + private int64 _high_volume_approved_at = 0; + private int64 _high_volume_approved_ttl_usec = 0; + public override bool high_volume_approved { + get { return this._high_volume_approved; } + private set { this._high_volume_approved = value; } + } + private void init_high_volume_approved() { + _settings.changed["warning-volume-confirmation-ttl"].connect(() => update_high_volume_approved_cache()); + update_high_volume_approved_cache(); + } + private void update_high_volume_approved_cache() { + _high_volume_approved_ttl_usec = _settings.get_int("warning-volume-confirmation-ttl"); + _high_volume_approved_ttl_usec *= 1000000; + + update_high_volume_approved(); + update_high_volume_approved_timer(); + } + private void update_high_volume_approved_timer() { + stop_high_volume_approved_timer(); + if (_high_volume_approved_at != 0) { + int64 expires_at = _high_volume_approved_at + _high_volume_approved_ttl_usec; + int64 now = GLib.get_monotonic_time(); + if (expires_at > now) { + var seconds_left = 1 + ((expires_at - now) / 1000000); + _high_volume_approved_timer = Timeout.add_seconds((uint)seconds_left, on_high_volume_approved_timer); + } } } + private void stop_high_volume_approved_timer() { + if (_high_volume_approved_timer != 0) { + Source.remove (_high_volume_approved_timer); + _high_volume_approved_timer = 0; + } + } + private bool on_high_volume_approved_timer() { + _high_volume_approved_timer = 0; + update_high_volume_approved(); + return false; /* Source.REMOVE */ + } + private void update_high_volume_approved() { + var new_high_volume_approved = calculate_high_volume_approved(); + if (high_volume_approved != new_high_volume_approved) { + debug("changing high_volume_approved from %d to %d", (int)high_volume_approved, (int)new_high_volume_approved); + high_volume_approved = new_high_volume_approved; + } + } + private bool calculate_high_volume_approved() { + int64 now = GLib.get_monotonic_time(); + return (_high_volume_approved_at != 0) + && (_high_volume_approved_at + _high_volume_approved_ttl_usec >= now); + } + public override void approve_high_volume() { + _high_volume_approved_at = GLib.get_monotonic_time(); + update_high_volume_approved(); + update_high_volume_approved_timer(); + } + + + /** MIC VOLUME PROPERTY */ public override double mic_volume { get { diff --git a/src/volume-control.vala b/src/volume-control.vala index 3f1c799..6efac35 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -36,12 +36,24 @@ public abstract class VolumeControl : Object public virtual string stream { get { return ""; } } public virtual bool ready { get { return false; } set { } } public virtual bool active_mic { get { return false; } set { } } - public virtual bool high_volume { get { return false; } } + public virtual bool high_volume { get { return false; } protected set { } } public virtual bool mute { get { return false; } } public virtual bool is_playing { get { return false; } } private Volume _volume; public virtual Volume volume { get { return _volume; } set { } } public virtual double mic_volume { get { return 0.0; } set { } } + public virtual double max_volume { get { return 1.0; } protected set { } } + + public virtual bool high_volume_approved { get { return false; } protected set { } } + public virtual void approve_high_volume() { } + public virtual void clamp_to_high_volume() { } public abstract void set_mute (bool mute); + + public void set_volume_clamp (double unclamped, VolumeControl.VolumeReasons reason) { + var v = new VolumeControl.Volume(); + v.volume = unclamped.clamp (0.0, this.max_volume); + v.reason = reason; + this.volume = v; + } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3c2e76f..2bbd8c5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -195,7 +195,6 @@ add_test(sound-menu-test sound-menu-test) # Notification Test ########################### -#[[ include_directories(${CMAKE_SOURCE_DIR}/src) add_executable (notifications-test notifications-test.cc) target_link_libraries ( @@ -209,7 +208,6 @@ target_link_libraries ( ) add_test(notifications-test notifications-test) -]] ########################### # Accounts Service User diff --git a/tests/notifications-test.cc b/tests/notifications-test.cc index 1f7e178..8d5617d 100644 --- a/tests/notifications-test.cc +++ b/tests/notifications-test.cc @@ -350,29 +350,29 @@ TEST_F(NotificationsTest, HighVolume) { /* Set high volume with volume change */ notifications->clearNotifications(); - volume_control_mock_set_mock_high_volume(VOLUME_CONTROL_MOCK(volumeControl.get()), TRUE); + volume_control_mock_set_high_volume(VOLUME_CONTROL_MOCK(volumeControl.get()), true); setMockVolume(volumeControl, 0.90); loop(50); notev = notifications->getNotifications(); ASSERT_LT(0, notev.size()); /* This passes with one or two since it would just be an update to the first if a second was sent */ EXPECT_EQ("Volume", notev[0].summary); - EXPECT_EQ("High volume", notev[0].body); + EXPECT_EQ("High volume can damage your hearing.", notev[0].body); EXPECT_GVARIANT_EQ("@s 'true'", notev[0].hints["x-canonical-value-bar-tint"]); /* Move it back */ - volume_control_mock_set_mock_high_volume(VOLUME_CONTROL_MOCK(volumeControl.get()), FALSE); + volume_control_mock_set_high_volume(VOLUME_CONTROL_MOCK(volumeControl.get()), false); setMockVolume(volumeControl, 0.50); loop(50); /* Set high volume without level change */ /* NOTE: This can happen if headphones are plugged in */ notifications->clearNotifications(); - volume_control_mock_set_mock_high_volume(VOLUME_CONTROL_MOCK(volumeControl.get()), TRUE); + volume_control_mock_set_high_volume(VOLUME_CONTROL_MOCK(volumeControl.get()), TRUE); loop(50); notev = notifications->getNotifications(); ASSERT_EQ(1, notev.size()); EXPECT_EQ("Volume", notev[0].summary); - EXPECT_EQ("High volume", notev[0].body); + EXPECT_EQ("High volume can damage your hearing.", notev[0].body); EXPECT_GVARIANT_EQ("@s 'true'", notev[0].hints["x-canonical-value-bar-tint"]); } @@ -406,7 +406,7 @@ TEST_F(NotificationsTest, MenuHide) { EXPECT_EQ(1, notev.size()); } -TEST_F(NotificationsTest, ExtendendVolumeNotification) { +TEST_F(NotificationsTest, DISABLED_ExtendendVolumeNotification) { auto volumeControl = volumeControlMock(); auto soundService = standardService(volumeControl, playerListMock()); @@ -424,7 +424,7 @@ TEST_F(NotificationsTest, ExtendendVolumeNotification) { /* Allow an amplified volume */ notifications->clearNotifications(); - indicator_sound_service_set_allow_amplified_volume(soundService.get(), TRUE); + //indicator_sound_service_set_allow_amplified_volume(soundService.get(), TRUE); loop(50); notev = notifications->getNotifications(); ASSERT_EQ(1, notev.size()); @@ -440,7 +440,7 @@ TEST_F(NotificationsTest, ExtendendVolumeNotification) { /* Put back */ notifications->clearNotifications(); - indicator_sound_service_set_allow_amplified_volume(soundService.get(), FALSE); + //indicator_sound_service_set_allow_amplified_volume(soundService.get(), FALSE); loop(50); notev = notifications->getNotifications(); ASSERT_EQ(1, notev.size()); diff --git a/tests/volume-control-mock.vala b/tests/volume-control-mock.vala index 4c96518..dc11fba 100644 --- a/tests/volume-control-mock.vala +++ b/tests/volume-control-mock.vala @@ -20,12 +20,14 @@ public class VolumeControlMock : VolumeControl { + private bool _high_volume = false; + public override bool high_volume { get { return _high_volume; } protected set { _high_volume = value; } } + public void set_high_volume(bool b) { high_volume = b; } + public string mock_stream { get; set; default = "multimedia"; } public override string stream { get { return mock_stream; } } public override bool ready { get; set; } public override bool active_mic { get; set; } - public bool mock_high_volume { get; set; } - public override bool high_volume { get { return mock_high_volume; } } public bool mock_mute { get; set; } public override bool mute { get { return mock_mute; } } public bool mock_is_playing { get; set; } |