• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 current_path, uint32_t num_items)>;
135   virtual void SetBrowsedPlayer(uint16_t player_id, std::string current_path,
136                                 SetBrowsedPlayerCallback browse_cb) = 0;
137 
138   using SetAddressedPlayerCallback = base::Callback<void(uint16_t)>;
139   virtual void SetAddressedPlayer(uint16_t player_id, SetAddressedPlayerCallback new_player) = 0;
140 
141   virtual void PlayItem(uint16_t player_id, bool now_playing, std::string media_id) = 0;
142 
143   virtual void SetActiveDevice(const RawAddress& address) = 0;
144 
145   virtual void RegisterUpdateCallback(MediaCallbacks* callback) = 0;
146 
147   virtual void UnregisterUpdateCallback(MediaCallbacks* callback) = 0;
148 
149   MediaInterface() = default;
150   virtual ~MediaInterface() = default;
151 };
152 
153 class VolumeInterface {
154 public:
155   // TODO (apanicke): Investigate the best value type for volume. Right now it
156   // is a value from 0-127 because thats what AVRCP uses.
157   using VolumeChangedCb = base::Callback<void(int8_t volume)>;
158 
159   // Indicate that a device has been connected that does not support absolute
160   // volume.
161   virtual void DeviceConnected(const RawAddress& bdaddr) = 0;
162 
163   // Indicate that a device has been connected that does support absolute
164   // volume. The callback will be immediately called with the current volume
165   // which will be sent to the device.
166   virtual void DeviceConnected(const RawAddress& bdaddr, 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 = base::Callback<void(std::vector<PlayerAttribute> attributes)>;
180   virtual void ListPlayerSettings(ListPlayerSettingsCallback cb) = 0;
181 
182   using ListPlayerSettingValuesCallback =
183           base::Callback<void(PlayerAttribute setting, std::vector<uint8_t> values)>;
184   virtual void ListPlayerSettingValues(PlayerAttribute setting,
185                                        ListPlayerSettingValuesCallback cb) = 0;
186 
187   using GetCurrentPlayerSettingValueCallback = base::Callback<void(
188           std::vector<PlayerAttribute> attributes, std::vector<uint8_t> values)>;
189   virtual void GetCurrentPlayerSettingValue(std::vector<PlayerAttribute> attributes,
190                                             GetCurrentPlayerSettingValueCallback cb) = 0;
191 
192   using SetPlayerSettingValueCallback = base::Callback<void(bool success)>;
193   virtual void SetPlayerSettings(std::vector<PlayerAttribute> attributes,
194                                  std::vector<uint8_t> values, SetPlayerSettingValueCallback cb) = 0;
195 
196   virtual ~PlayerSettingsInterface() = default;
197 };
198 
199 class ServiceInterface {
200 public:
201   // mediaInterface can not be null. If volumeInterface is null then Absolute
202   // Volume is disabled.
203   virtual void Init(MediaInterface* mediaInterface, VolumeInterface* volumeInterface,
204                     PlayerSettingsInterface* player_settings_interface) = 0;
205   virtual void RegisterBipServer(int psm) = 0;
206   virtual void UnregisterBipServer() = 0;
207   virtual bool ConnectDevice(const RawAddress& bdaddr) = 0;
208   virtual bool DisconnectDevice(const RawAddress& bdaddr) = 0;
209   virtual void SetBipClientStatus(const RawAddress& bdaddr, bool connected) = 0;
210   virtual bool Cleanup() = 0;
211 
212 protected:
213   virtual ~ServiceInterface() = default;
214 };
215 
216 }  // namespace avrcp
217 }  // namespace bluetooth
218