1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <base/functional/callback_forward.h>
20 
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 #include "avrcp_common.h"
26 #include "types/raw_address.h"
27 
28 namespace bluetooth {
29 namespace avrcp {
30 
31 struct SongInfo {
32   std::string media_id;  // This gets converted to a UID in the native service
33   std::set<AttributeEntry> attributes;
34 };
35 
36 enum PlayState : uint8_t {
37   STOPPED = 0x00,
38   PLAYING,
39   PAUSED,
40   FWD_SEEK,
41   REV_SEEK,
42   ERROR = 0xFF,
43 };
44 
45 struct PlayStatus {
46   uint32_t position;
47   uint32_t duration;
48   PlayState state;
49 };
50 
51 struct MediaPlayerInfo {
52   uint16_t id;
53   std::string name;
54   bool browsing_supported;
55 };
56 
57 struct FolderInfo {
58   std::string media_id;
59   bool is_playable;
60   std::string name;
61 };
62 
63 // TODO (apanicke): Convert this to a union
64 struct ListItem {
65   enum : uint8_t {
66     FOLDER,
67     SONG,
68   } type;
69 
70   FolderInfo folder;
71   SongInfo song;
72 };
73 
74 class MediaCallbacks {
75 public:
76   virtual void SendMediaUpdate(bool track_changed, bool play_state, bool queue) = 0;
77   virtual void SendFolderUpdate(bool available_players, bool addressed_players,
78                                 bool uids_changed) = 0;
79   virtual void SendPlayerSettingsChanged(std::vector<PlayerAttribute> attributes,
80                                          std::vector<uint8_t> values) = 0;
81   virtual ~MediaCallbacks() = default;
82 };
83 
84 // The classes below are used by the JNI and are loaded dynamically with the
85 // Bluetooth library. All classes must be pure virtual otherwise a compiler
86 // error occurs when trying to link the function implementation.
87 
88 // MediaInterface defines the class that the AVRCP Service uses in order
89 // communicate with the media layer. The media layer will define its own
90 // implementation of this object and register it with the service using
91 // Avrcp::ServiceInterface::Init(). At this point the AVRCP Service will
92 // call RegisterUpdateCallbacks() to provide an handle to use to send
93 // notifications about changes in the Media Interface.
94 //
95 // NOTES: The current implementation has the native service handle all the
96 // thread switching. It will call the interface functions on the btif/jni
97 // thread and the callback will post its results to the bta thread.
98 // In the future the interface the JNI registered with the
99 // service should post all its tasks to the JNI thread itself so that the native
100 // service isn't aware of the thread the interface functions need to be called
101 // on. It can then supply callbacks that post results to the correct thread
102 // allowing the threading model to be totally encapsulated and allow correct
103 // behavior in case the threading model changes on either side.
104 class MediaInterface {
105 public:
106   virtual void SendKeyEvent(uint8_t key, KeyState state) = 0;
107 
108   using SongInfoCallback = base::Callback<void(SongInfo)>;
109   virtual void GetSongInfo(SongInfoCallback info_cb) = 0;
110 
111   using PlayStatusCallback = base::Callback<void(PlayStatus)>;
112   virtual void GetPlayStatus(PlayStatusCallback status_cb) = 0;
113 
114   // Contains the current queue and the media ID of the currently playing item
115   // in the queue
116   using NowPlayingCallback = base::Callback<void(std::string, std::vector<SongInfo>)>;
117   virtual void GetNowPlayingList(NowPlayingCallback now_playing_cb) = 0;
118 
119   // TODO (apanicke): Use a map with the ID as the key instead of vector
120   // in follow up cleanup patches. This allows simplification of the
121   // MediaPlayerInfo object
122   using MediaListCallback =
123           base::Callback<void(uint16_t curr_player, std::vector<MediaPlayerInfo>)>;
124   virtual void GetMediaPlayerList(MediaListCallback list_cb) = 0;
125 
126   using FolderItemsCallback = base::Callback<void(std::vector<ListItem>)>;
127   virtual void GetFolderItems(uint16_t player_id, std::string media_id,
128                               FolderItemsCallback folder_cb) = 0;
129 
130   using GetAddressedPlayerCallback = base::Callback<void(uint16_t)>;
131   virtual void GetAddressedPlayer(GetAddressedPlayerCallback addressed_player) = 0;
132 
133   using SetBrowsedPlayerCallback =
134           base::Callback<void(bool success, std::string root_id, uint32_t num_items)>;
135   virtual void SetBrowsedPlayer(uint16_t player_id, SetBrowsedPlayerCallback browse_cb) = 0;
136 
137   using SetAddressedPlayerCallback = base::Callback<void(uint16_t)>;
138   virtual void SetAddressedPlayer(uint16_t player_id, SetAddressedPlayerCallback new_player) = 0;
139 
140   virtual void PlayItem(uint16_t player_id, bool now_playing, std::string media_id) = 0;
141 
142   virtual void SetActiveDevice(const RawAddress& address) = 0;
143 
144   virtual void RegisterUpdateCallback(MediaCallbacks* callback) = 0;
145 
146   virtual void UnregisterUpdateCallback(MediaCallbacks* callback) = 0;
147 
148   MediaInterface() = default;
149   virtual ~MediaInterface() = default;
150 };
151 
152 class VolumeInterface {
153 public:
154   // TODO (apanicke): Investigate the best value type for volume. Right now it
155   // is a value from 0-127 because thats what AVRCP uses.
156   using VolumeChangedCb = base::Callback<void(int8_t volume)>;
157 
158   // Indicate that a device has been connected that does not support absolute
159   // volume.
160   virtual void DeviceConnected(const RawAddress& bdaddr) = 0;
161 
162   // Indicate that a device has been connected that does support absolute
163   // volume. The callback will be immediately called with the current volume
164   // which will be sent to the device.
165   virtual void DeviceConnected(const RawAddress& bdaddr, VolumeChangedCb cb) = 0;
166 
167   // Indicate that a device has been disconnected from AVRCP. Will unregister
168   // any callbacks if absolute volume is supported.
169   virtual void DeviceDisconnected(const RawAddress& bdaddr) = 0;
170 
171   virtual void SetVolume(int8_t volume) = 0;
172 
173   virtual ~VolumeInterface() = default;
174 };
175 
176 class PlayerSettingsInterface {
177 public:
178   using ListPlayerSettingsCallback = base::Callback<void(std::vector<PlayerAttribute> attributes)>;
179   virtual void ListPlayerSettings(ListPlayerSettingsCallback cb) = 0;
180 
181   using ListPlayerSettingValuesCallback =
182           base::Callback<void(PlayerAttribute setting, std::vector<uint8_t> values)>;
183   virtual void ListPlayerSettingValues(PlayerAttribute setting,
184                                        ListPlayerSettingValuesCallback cb) = 0;
185 
186   using GetCurrentPlayerSettingValueCallback = base::Callback<void(
187           std::vector<PlayerAttribute> attributes, std::vector<uint8_t> values)>;
188   virtual void GetCurrentPlayerSettingValue(std::vector<PlayerAttribute> attributes,
189                                             GetCurrentPlayerSettingValueCallback cb) = 0;
190 
191   using SetPlayerSettingValueCallback = base::Callback<void(bool success)>;
192   virtual void SetPlayerSettings(std::vector<PlayerAttribute> attributes,
193                                  std::vector<uint8_t> values, SetPlayerSettingValueCallback cb) = 0;
194 
195   virtual ~PlayerSettingsInterface() = default;
196 };
197 
198 class ServiceInterface {
199 public:
200   // mediaInterface can not be null. If volumeInterface is null then Absolute
201   // Volume is disabled.
202   virtual void Init(MediaInterface* mediaInterface, VolumeInterface* volumeInterface,
203                     PlayerSettingsInterface* player_settings_interface) = 0;
204   virtual void RegisterBipServer(int psm) = 0;
205   virtual void UnregisterBipServer() = 0;
206   virtual bool ConnectDevice(const RawAddress& bdaddr) = 0;
207   virtual bool DisconnectDevice(const RawAddress& bdaddr) = 0;
208   virtual void SetBipClientStatus(const RawAddress& bdaddr, bool connected) = 0;
209   virtual bool Cleanup() = 0;
210 
211 protected:
212   virtual ~ServiceInterface() = default;
213 };
214 
215 }  // namespace avrcp
216 }  // namespace bluetooth
217