Music Hub  ..
A session-wide music playback service
service_stub.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2013-2014 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as 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  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 
19 #include "service_stub.h"
20 #include "service_traits.h"
21 
22 #include "player_stub.h"
23 #include "the_session_bus.h"
24 
25 #include "mpris/service.h"
26 
27 namespace dbus = core::dbus;
28 namespace media = core::ubuntu::media;
29 
31 {
32  dbus::Object::Ptr object;
33 };
34 
36  : core::dbus::Stub<media::Service>(the_session_bus()),
37  d(new Private{
38  access_service()->object_for_path(
39  dbus::types::ObjectPath(
40  dbus::traits::Service<media::Service>::object_path()))})
41 {
42  auto bus = the_session_bus();
43  worker = std::move(std::thread([bus]()
44  {
45  bus->run();
46  }));
47 }
48 
50 {
51  auto bus = the_session_bus();
52  bus->stop();
53 
54  if (worker.joinable())
55  worker.join();
56 }
57 
58 std::shared_ptr<media::Player> media::ServiceStub::create_session(const media::Player::Configuration&)
59 {
60  auto op = d->object->invoke_method_synchronously<mpris::Service::CreateSession,
61  std::tuple<dbus::types::ObjectPath, std::string>>();
62 
63  if (op.is_error())
64  throw std::runtime_error("Problem creating session: " + op.error());
65 
66  return std::shared_ptr<media::Player>(new media::PlayerStub
67  {
68  shared_from_this(),
69  access_service(),
70  access_service()->object_for_path(std::get<0>(op.value())),
71  std::get<1>(op.value())
72  });
73 }
74 
75 void media::ServiceStub::detach_session(const std::string& uuid, const media::Player::Configuration&)
76 {
77  auto op = d->object->invoke_method_synchronously<mpris::Service::DetachSession,
78  void>(uuid);
79 
80  if (op.is_error())
81  throw std::runtime_error("Problem detaching session: " + op.error());
82 }
83 
84 std::shared_ptr<media::Player> media::ServiceStub::reattach_session(const std::string& uuid, const media::Player::Configuration&)
85 {
86  auto op = d->object->invoke_method_synchronously<mpris::Service::ReattachSession,
87  dbus::types::ObjectPath>(uuid);
88 
89  if (op.is_error())
90  throw std::runtime_error("Problem reattaching session: " + op.error());
91 
92  return std::shared_ptr<media::Player>(new media::PlayerStub
93  {
94  shared_from_this(),
95  access_service(),
96  access_service()->object_for_path(op.value()),
97  uuid
98  });
99 }
100 
101 void media::ServiceStub::destroy_session(const std::string& uuid, const media::Player::Configuration&)
102 {
103  auto op = d->object->invoke_method_synchronously<mpris::Service::DestroySession,
104  void>(uuid);
105 
106  if (op.is_error())
107  throw std::runtime_error("Problem destroying session: " + op.error());
108 }
109 
110 std::shared_ptr<media::Player> media::ServiceStub::create_fixed_session(const std::string& name, const media::Player::Configuration&)
111 {
112  auto op = d->object->invoke_method_synchronously<mpris::Service::CreateFixedSession,
113  dbus::types::ObjectPath>(name);
114 
115  if (op.is_error())
116  throw std::runtime_error("Problem creating session: " + op.error());
117 
118  return std::shared_ptr<media::Player>(new media::PlayerStub
119  {
120  shared_from_this(),
121  access_service(),
122  access_service()->object_for_path(op.value())
123  });
124 }
125 
126 std::shared_ptr<media::Player> media::ServiceStub::resume_session(media::Player::PlayerKey key)
127 {
128  auto op = d->object->invoke_method_synchronously<mpris::Service::ResumeSession,
129  dbus::types::ObjectPath>(key);
130 
131  if (op.is_error())
132  throw std::runtime_error("Problem resuming session: " + op.error());
133 
134  return std::shared_ptr<media::Player>(new media::PlayerStub
135  {
136  shared_from_this(),
137  access_service(),
138  access_service()->object_for_path(op.value())
139  });
140 }
141 
143 {
144  auto op = d->object->invoke_method_synchronously<mpris::Service::SetCurrentPlayer,
145  void>(key);
146 
147  if (op.is_error())
148  throw std::runtime_error("Problem setting current player: " + op.error());
149 }
150 
152 {
153  auto op = d->object->invoke_method_synchronously<mpris::Service::PauseOtherSessions,
154  void>(key);
155 
156  if (op.is_error())
157  throw std::runtime_error("Problem pausing other sessions: " + op.error());
158 }
std::shared_ptr< Player > create_session(const Player::Configuration &)
Definition: player.h:33
std::shared_ptr< Player > reattach_session(const std::string &uuid, const Player::Configuration &)
void pause_other_sessions(Player::PlayerKey key)
void set_current_player(Player::PlayerKey key)
void detach_session(const std::string &uuid, const Player::Configuration &)
void destroy_session(const std::string &uuid, const Player::Configuration &)
core::dbus::Bus::Ptr the_session_bus()
std::shared_ptr< Player > create_fixed_session(const std::string &name, const Player::Configuration &)
dbus::Object::Ptr object
std::shared_ptr< Player > resume_session(Player::PlayerKey key)