Music Hub ..
A session-wide music playback service
hashed_keyed_player_store.h
Go to the documentation of this file.
1/*
2 * Copyright © 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#ifndef CORE_UBUNTU_MEDIA_HASHED_KEYED_PLAYER_STORE_H_
20#define CORE_UBUNTU_MEDIA_HASHED_KEYED_PLAYER_STORE_H_
21
23
24#include <mutex>
25#include <unordered_map>
26
27namespace core
28{
29namespace ubuntu
30{
31namespace media
32{
33// Implements KeyedPlayerStore using a std::unordered_map.
35{
36public:
38 // We keep track of the "current" player, that is, the one
39 // that has been created most recently, or has been explicitly foregrounded, or has been enabled for
40 // background playback. We provide a getable/observable access to that designated instance.
41 const core::Property<std::shared_ptr<media::Player>>& current_player() const override;
42
43 // We keep track of all known player sessions here and render them accessible via
44 // the key. All of these functions are thread-safe but not reentrant.
45 // Returns true iff a player is known for the given key.
46 bool has_player_for_key(const Player::PlayerKey& key) const override;
47
48 // Returns the player for the given key or throws std::out_of_range if no player is known
49 // for the given key.
50 // Throws std::out_of_range if no player is known for the key.
51 std::shared_ptr<Player> player_for_key(const Player::PlayerKey& key) const override;
52
53 // Enumerates all known players and invokes the given enumerator for each
54 // (key, player) pair.
55 void enumerate_players(const PlayerEnumerator& enumerator) const override;
56
57 // Adds the given player with the given key.
58 void add_player_for_key(const Player::PlayerKey& key, const std::shared_ptr<Player>& player) override;
59
60 // Removes the player for the given key, and unsets it if it is the current one.
61 void remove_player_for_key(const Player::PlayerKey& key) override;
62
63 // Returns the number of players in the store.
64 size_t number_of_players() const;
65
66 // Makes the player known under the given key current.
67 // Throws std::out_of_range if no player is known for the key.
68 void set_current_player_for_key(const Player::PlayerKey& key) override;
69
70private:
71 core::Property<std::shared_ptr<Player>> prop_current_player;
72 mutable std::recursive_mutex guard;
73 std::unordered_map<Player::PlayerKey, std::shared_ptr<Player>> map;
74};
75}
76}
77}
78
79#endif // CORE_UBUNTU_MEDIA_KEYED_PLAYER_STORE_H_
void enumerate_players(const PlayerEnumerator &enumerator) const override
void remove_player_for_key(const Player::PlayerKey &key) override
std::shared_ptr< Player > player_for_key(const Player::PlayerKey &key) const override
bool has_player_for_key(const Player::PlayerKey &key) const override
void add_player_for_key(const Player::PlayerKey &key, const std::shared_ptr< Player > &player) override
const core::Property< std::shared_ptr< media::Player > > & current_player() const override
void set_current_player_for_key(const Player::PlayerKey &key) override
std::function< void(const Player::PlayerKey &, const std::shared_ptr< Player > &) > PlayerEnumerator
Definition: player.h:34