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 "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,
77                                bool queue) = 0;
78   virtual void SendFolderUpdate(bool available_players, bool addressed_players,
79                                 bool uids_changed) = 0;
80   virtual void SendActiveDeviceChanged(const RawAddress& address) = 0;
81 
82   virtual void SendPlayerSettingsChanged(
83       std::vector<PlayerAttribute> attributes, std::vector<uint8_t> values) = 0;
84   virtual ~MediaCallbacks() = default;
85 };
86 
87 // The classes below are used by the JNI and are loaded dynamically with the
88 // Bluetooth library. All classes must be pure virtual otherwise a compiler
89 // error occurs when trying to link the function implementation.
90 
91 // MediaInterface defines the class that the AVRCP Service uses in order
92 // communicate with the media layer. The media layer will define its own
93 // implementation of this object and register it with the service using
94 // Avrcp::ServiceInterface::Init(). At this point the AVRCP Service will
95 // call RegisterUpdateCallbacks() to provide an handle to use to send
96 // notifications about changes in the Media Interface.
97 //
98 // NOTES: The current implementation has the native service handle all the
99 // thread switching. It will call the interface functions on the btif/jni
100 // thread and the callback will post its results to the bta thread.
101 // In the future the interface the JNI registered with the
102 // service should post all its tasks to the JNI thread itself so that the native
103 // service isn't aware of the thread the interface functions need to be called
104 // on. It can then supply callbacks that post results to the correct thread
105 // allowing the threading model to be totally encapsulated and allow correct
106 // behavior in case the threading model changes on either side.
107 class MediaInterface {
108  public:
109   virtual void SendKeyEvent(uint8_t key, KeyState state) = 0;
110 
111   using SongInfoCallback = base::Callback<void(SongInfo)>;
112   virtual void GetSongInfo(SongInfoCallback info_cb) = 0;
113 
114   using PlayStatusCallback = base::Callback<void(PlayStatus)>;
115   virtual void GetPlayStatus(PlayStatusCallback status_cb) = 0;
116 
117   // Contains the current queue and the media ID of the currently playing item
118   // in the queue
119   using NowPlayingCallback =
120       base::Callback<void(std::string, std::vector<SongInfo>)>;
121   virtual void GetNowPlayingList(NowPlayingCallback now_playing_cb) = 0;
122 
123   // TODO (apanicke): Use a map with the ID as the key instead of vector
124   // in follow up cleanup patches. This allows simplification of the
125   // MediaPlayerInfo object
126   using MediaListCallback =
127       base::Callback<void(uint16_t curr_player, std::vector<MediaPlayerInfo>)>;
128   virtual void GetMediaPlayerList(MediaListCallback list_cb) = 0;
129 
130   using FolderItemsCallback = base::Callback<void(std::vector<ListItem>)>;
131   virtual void GetFolderItems(uint16_t player_id, std::string media_id,
132                               FolderItemsCallback folder_cb) = 0;
133 
134   using SetBrowsedPlayerCallback = base::Callback<void(
135       bool success, std::string root_id, uint32_t num_items)>;
136   virtual void SetBrowsedPlayer(uint16_t player_id,
137                                 SetBrowsedPlayerCallback browse_cb) = 0;
138 
139   virtual void PlayItem(uint16_t player_id, bool now_playing,
140                         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,
166                                VolumeChangedCb cb) = 0;
167 
168   // Indicate that a device has been disconnected from AVRCP. Will unregister
169   // any callbacks if absolute volume is supported.
170   virtual void DeviceDisconnected(const RawAddress& bdaddr) = 0;
171 
172   virtual void SetVolume(int8_t volume) = 0;
173 
174   virtual ~VolumeInterface() = default;
175 };
176 
177 class PlayerSettingsInterface {
178  public:
179   using ListPlayerSettingsCallback =
180       base::Callback<void(std::vector<PlayerAttribute> attributes)>;
181   virtual void ListPlayerSettings(ListPlayerSettingsCallback cb) = 0;
182 
183   using ListPlayerSettingValuesCallback = base::Callback<void(
184       PlayerAttribute setting, std::vector<uint8_t> values)>;
185   virtual void ListPlayerSettingValues(PlayerAttribute setting,
186                                        ListPlayerSettingValuesCallback cb) = 0;
187 
188   using GetCurrentPlayerSettingValueCallback = base::Callback<void(
189       std::vector<PlayerAttribute> attributes, std::vector<uint8_t> values)>;
190   virtual void GetCurrentPlayerSettingValue(
191       std::vector<PlayerAttribute> attributes,
192       GetCurrentPlayerSettingValueCallback cb) = 0;
193 
194   using SetPlayerSettingValueCallback = base::Callback<void(bool success)>;
195   virtual void SetPlayerSettings(std::vector<PlayerAttribute> attributes,
196                                  std::vector<uint8_t> values,
197                                  SetPlayerSettingValueCallback cb) = 0;
198 
199   virtual ~PlayerSettingsInterface() = default;
200 };
201 
202 class ServiceInterface {
203  public:
204   // mediaInterface can not be null. If volumeInterface is null then Absolute
205   // Volume is disabled.
206   virtual void Init(MediaInterface* mediaInterface,
207                     VolumeInterface* volumeInterface,
208                     PlayerSettingsInterface* player_settings_interface) = 0;
209   virtual void RegisterBipServer(int psm) = 0;
210   virtual void UnregisterBipServer() = 0;
211   virtual bool ConnectDevice(const RawAddress& bdaddr) = 0;
212   virtual bool DisconnectDevice(const RawAddress& bdaddr) = 0;
213   virtual void SetBipClientStatus(const RawAddress& bdaddr, bool connected) = 0;
214   virtual bool Cleanup() = 0;
215 
216  protected:
217   virtual ~ServiceInterface() = default;
218 };
219 
220 }  // namespace avrcp
221 }  // namespace bluetooth
222