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

Subscribers

People subscribed via source and target branches