Music Hub  ..
A session-wide music playback service
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
service_implementation.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  * Jim Hodapp <jim.hodapp@canonical.com>
18  */
19 
20 #include "service_implementation.h"
21 
22 #include "player_configuration.h"
23 #include "player_implementation.h"
24 
25 #include <map>
26 
27 namespace media = core::ubuntu::media;
28 
29 using namespace std;
30 
32 {
33  typedef map<media::Player::PlayerKey, std::shared_ptr<media::Player>> player_map_t;
34 
36  : key_(0)
37  {
38  }
39 
40  void track_player(const std::shared_ptr<media::Player>& player)
41  {
42  player_map.insert(
43  std::pair<media::Player::PlayerKey,
44  std::shared_ptr<media::Player>>(key_, player));
45 
46  ++key_;
47  }
48 
50  {
51  return key_;
52  }
53 
55  {
56  auto player_it = player_map.find(key);
57  if (player_it != player_map.end())
58  {
59  auto &current_player = (*player_it).second;
60  for (auto& player_pair : player_map)
61  {
62  // Only pause a Player if all of the following criteria are met:
63  // 1) currently playing
64  // 2) not the same player as the one passed in my key
65  // 3) new Player has an audio stream role set to multimedia
66  // 4) has an audio stream role set to multimedia
67  if (player_pair.second->playback_status() == Player::playing
68  && player_pair.first != key
69  && current_player->audio_stream_role() == media::Player::multimedia
70  && player_pair.second->audio_stream_role() == media::Player::multimedia)
71  {
72  cout << "Pausing Player with key: " << player_pair.first << endl;
73  player_pair.second->pause();
74  }
75  }
76  }
77  else
78  cerr << "Could not find Player by key: " << key << endl;
79  }
80 
81  // Used for Player instance management
82  player_map_t player_map;
84 
85 };
86 
88 {
89  cout << __PRETTY_FUNCTION__ << endl;
90 }
91 
93 {
94 }
95 
96 std::shared_ptr<media::Player> media::ServiceImplementation::create_session(
97  const media::Player::Configuration& conf)
98 {
99  std::shared_ptr<media::Player> player = std::make_shared<media::PlayerImplementation>(
100  conf.object_path, shared_from_this(), d->key());
101  d->track_player(player);
102  return player;
103 }
104 
106 {
107  d->pause_other_sessions(key);
108 }
void track_player(const std::shared_ptr< media::Player > &player)
void pause_other_sessions(media::Player::PlayerKey key)
STL namespace.
void pause_other_sessions(Player::PlayerKey key)
std::shared_ptr< Player > create_session(const Player::Configuration &)
media::Player::PlayerKey key() const
map< media::Player::PlayerKey, std::shared_ptr< media::Player > > player_map_t