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  dbus::types::ObjectPath>();
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()->object_for_path(op.value())
70  });
71 }
72 
73 std::shared_ptr<media::Player> media::ServiceStub::create_fixed_session(const std::string& name, const media::Player::Configuration&)
74 {
75  auto op = d->object->invoke_method_synchronously<mpris::Service::CreateFixedSession,
76  dbus::types::ObjectPath>(name);
77 
78  if (op.is_error())
79  throw std::runtime_error("Problem creating session: " + op.error());
80 
81  return std::shared_ptr<media::Player>(new media::PlayerStub
82  {
83  shared_from_this(),
84  access_service()->object_for_path(op.value())
85  });
86 }
87 
88 std::shared_ptr<media::Player> media::ServiceStub::resume_session(media::Player::PlayerKey key)
89 {
90  auto op = d->object->invoke_method_synchronously<mpris::Service::ResumeSession,
91  dbus::types::ObjectPath>(key);
92 
93  if (op.is_error())
94  throw std::runtime_error("Problem resuming session: " + op.error());
95 
96  return std::shared_ptr<media::Player>(new media::PlayerStub
97  {
98  shared_from_this(),
99  access_service()->object_for_path(op.value())
100  });
101 }
102 
104 {
105  std::cout << __PRETTY_FUNCTION__ << std::endl;
106  auto op = d->object->invoke_method_synchronously<mpris::Service::PauseOtherSessions,
107  void>(key);
108 
109  if (op.is_error())
110  throw std::runtime_error("Problem pausing other sessions: " + op.error());
111 }
std::shared_ptr< Player > create_session(const Player::Configuration &)
Definition: player.h:32
void pause_other_sessions(Player::PlayerKey key)
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)