Merge lp:~jhodapp/media-hub/critical-power into lp:media-hub

Proposed by Jim Hodapp
Status: Merged
Approved by: Ricardo Mendoza
Approved revision: no longer in the source branch.
Merged at revision: 59
Proposed branch: lp:~jhodapp/media-hub/critical-power
Merge into: lp:media-hub
Prerequisite: lp:~jhodapp/media-hub/audio-stream-role
Diff against target: 192 lines (+155/-2)
2 files modified
src/core/media/indicator_power_service.h (+74/-0)
src/core/media/service_implementation.cpp (+81/-2)
To merge this branch: bzr merge lp:~jhodapp/media-hub/critical-power
Reviewer Review Type Date Requested Status
Ricardo Mendoza (community) Approve
PS Jenkins bot continuous-integration Approve
Florian Boucault (community) Approve
Review via email: mp+233427@code.launchpad.net

Commit message

When power hits the low or very low levels, pause all Players with role of multimedia. When the warning notification is cleared from the screen, resume playback.

Description of the change

When power hits the low or very low levels, pause all Players with role of multimedia. When the warning notification is cleared from the screen, resume playback.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Florian Boucault (fboucault) wrote :

Code looks alright I think.

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ricardo Mendoza (ricmm) :
review: Approve
lp:~jhodapp/media-hub/critical-power updated
59. By Jim Hodapp

When power hits the low or very low levels, pause all Players with role of multimedia. When the warning notification is cleared from the screen, resume playback.
Approved by: Florian Boucault, PS Jenkins bot, Ricardo Mendoza

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'src/core/media/indicator_power_service.h'
--- src/core/media/indicator_power_service.h 1970-01-01 00:00:00 +0000
+++ src/core/media/indicator_power_service.h 2014-09-05 18:59:40 +0000
@@ -0,0 +1,74 @@
1/*
2 * Copyright (C) 2014 Canonical Ltd
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Jim Hodapp <jim.hodapp@canonical.com>
17 */
18
19#include <core/dbus/dbus.h>
20#include <core/dbus/fixture.h>
21#include <core/dbus/object.h>
22#include <core/dbus/property.h>
23#include <core/dbus/service.h>
24#include <core/dbus/interfaces/properties.h>
25#include <core/dbus/types/stl/tuple.h>
26#include <core/dbus/types/stl/vector.h>
27
28#include <core/dbus/asio/executor.h>
29
30#include <string>
31
32namespace core
33{
34
35struct IndicatorPower
36{
37 static std::string& name()
38 {
39 static std::string s = "com.canonical.indicator.power.Battery";
40 return s;
41 }
42
43 struct PowerLevel
44 {
45 static std::string name()
46 {
47 static std::string s = "PowerLevel";
48 return s;
49 }
50
51 typedef IndicatorPower Interface;
52 // Possible values: "ok", "low", "very_low", "critical"
53 typedef std::string ValueType;
54 static const bool readable = true;
55 static const bool writable = false;
56 };
57
58 struct IsWarning
59 {
60 static std::string name()
61 {
62 static std::string s = "IsWarning";
63 return s;
64 }
65
66 typedef IndicatorPower Interface;
67 typedef bool ValueType;
68 static const bool readable = true;
69 static const bool writable = false;
70 };
71
72}; // IndicatorPower
73
74}
075
=== modified file 'src/core/media/service_implementation.cpp'
--- src/core/media/service_implementation.cpp 2014-08-29 18:06:11 +0000
+++ src/core/media/service_implementation.cpp 2014-09-05 18:59:40 +0000
@@ -19,10 +19,14 @@
1919
20#include "service_implementation.h"20#include "service_implementation.h"
2121
22#include "indicator_power_service.h"
22#include "player_configuration.h"23#include "player_configuration.h"
23#include "player_implementation.h"24#include "player_implementation.h"
2425
25#include <map>26#include <map>
27#include <memory>
28#include <stdint.h>
29#include <thread>
2630
27namespace media = core::ubuntu::media;31namespace media = core::ubuntu::media;
2832
@@ -33,8 +37,45 @@
33 typedef map<media::Player::PlayerKey, std::shared_ptr<media::Player>> player_map_t;37 typedef map<media::Player::PlayerKey, std::shared_ptr<media::Player>> player_map_t;
3438
35 Private()39 Private()
36 : key_(0)40 : key_(0),
37 {41 resume_key(UINT32_MAX)
42 {
43 bus = std::shared_ptr<dbus::Bus>(new dbus::Bus(core::dbus::WellKnownBus::session));
44 bus->install_executor(dbus::asio::make_executor(bus));
45 worker = std::move(std::thread([this]()
46 {
47 bus->run();
48 }));
49
50 // Connect the property change signal that will allow media-hub to take appropriate action
51 // when the battery level reaches critical
52 auto stub_service = dbus::Service::use_service(bus, "com.canonical.indicator.power");
53 indicator_power_session = stub_service->object_for_path(dbus::types::ObjectPath("/com/canonical/indicator/power/Battery"));
54 power_level = indicator_power_session->get_property<core::IndicatorPower::PowerLevel>();
55 power_level->changed().connect([this](const core::IndicatorPower::PowerLevel::ValueType &level)
56 {
57 // When the battery level hits 10% or 5%, pause all multimedia sessions.
58 // Playback will resume when the user clears the presented notification.
59 if (level == "low" || level == "very_low")
60 pause_all_multimedia_sessions();
61 });
62
63 is_warning = indicator_power_session->get_property<core::IndicatorPower::IsWarning>();
64 is_warning->changed().connect([this](const core::IndicatorPower::IsWarning::ValueType &notifying)
65 {
66 // If the low battery level notification is no longer being displayed,
67 // resume what the user was previously playing
68 if (!notifying)
69 resume_multimedia_session();
70 });
71 }
72
73 ~Private()
74 {
75 bus->stop();
76
77 if (worker.joinable())
78 worker.join();
38 }79 }
3980
40 void track_player(const std::shared_ptr<media::Player>& player)81 void track_player(const std::shared_ptr<media::Player>& player)
@@ -78,9 +119,47 @@
78 cerr << "Could not find Player by key: " << key << endl;119 cerr << "Could not find Player by key: " << key << endl;
79 }120 }
80121
122 // Pauses all multimedia audio stream role type Players
123 void pause_all_multimedia_sessions()
124 {
125 for (auto& player_pair : player_map)
126 {
127 if (player_pair.second->playback_status() == Player::playing
128 && player_pair.second->audio_stream_role() == media::Player::multimedia)
129 {
130 resume_key = player_pair.first;
131 cout << "Will resume playback of Player with key: " << resume_key << endl;
132 player_pair.second->pause();
133 }
134 }
135 }
136
137 void resume_multimedia_session()
138 {
139 auto player_it = player_map.find(resume_key);
140 if (player_it != player_map.end())
141 {
142 auto &player = (*player_it).second;
143 if (player->playback_status() == Player::paused)
144 {
145 cout << "Resuming playback of Player with key: " << resume_key << endl;
146 player->play();
147 resume_key = UINT32_MAX;
148 }
149 }
150 }
151
81 // Used for Player instance management152 // Used for Player instance management
82 player_map_t player_map;153 player_map_t player_map;
83 media::Player::PlayerKey key_;154 media::Player::PlayerKey key_;
155 // This holds the key of the multimedia role Player instance that was paused
156 // when the battery level reached 10% or 5%
157 media::Player::PlayerKey resume_key;
158 std::thread worker;
159 dbus::Bus::Ptr bus;
160 std::shared_ptr<dbus::Object> indicator_power_session;
161 std::shared_ptr<core::dbus::Property<core::IndicatorPower::PowerLevel>> power_level;
162 std::shared_ptr<core::dbus::Property<core::IndicatorPower::IsWarning>> is_warning;
84163
85};164};
86165

Subscribers

People subscribed via source and target branches