• 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 <set>
20 #include <string>
21 #include <vector>
22 
23 #include <base/callback_forward.h>
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   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 =
117       base::Callback<void(std::string, std::vector<SongInfo>)>;
118   virtual void GetNowPlayingList(NowPlayingCallback now_playing_cb) = 0;
119 
120   // TODO (apanicke): Use a map with the ID as the key instead of vector
121   // in follow up cleanup patches. This allows simplification of the
122   // MediaPlayerInfo object
123   using MediaListCallback =
124       base::Callback<void(uint16_t curr_player, std::vector<MediaPlayerInfo>)>;
125   virtual void GetMediaPlayerList(MediaListCallback list_cb) = 0;
126 
127   using FolderItemsCallback = base::Callback<void(std::vector<ListItem>)>;
128   virtual void GetFolderItems(uint16_t player_id, std::string media_id,
129                               FolderItemsCallback folder_cb) = 0;
130 
131   using SetBrowsedPlayerCallback = base::Callback<void(
132       bool success, std::string root_id, uint32_t num_items)>;
133   virtual void SetBrowsedPlayer(uint16_t player_id,
134                                 SetBrowsedPlayerCallback browse_cb) = 0;
135 
136   virtual void PlayItem(uint16_t player_id, bool now_playing,
137                         std::string media_id) = 0;
138 
139   virtual void SetActiveDevice(const RawAddress& address) = 0;
140 
141   virtual void RegisterUpdateCallback(MediaCallbacks* callback) = 0;
142 
143   virtual void UnregisterUpdateCallback(MediaCallbacks* callback) = 0;
144 
145   MediaInterface() = default;
146   virtual ~MediaInterface() = default;
147 };
148 
149 class VolumeInterface {
150  public:
151   // TODO (apanicke): Investigate the best value type for volume. Right now it
152   // is a value from 0-127 because thats what AVRCP uses.
153   using VolumeChangedCb = base::Callback<void(int8_t volume)>;
154 
155   // Indicate that a device has been connected that does not support absolute
156   // volume.
157   virtual void DeviceConnected(const RawAddress& bdaddr) = 0;
158 
159   // Indicate that a device has been connected that does support absolute
160   // volume. The callback will be immediately called with the current volume
161   // which will be sent to the device.
162   virtual void DeviceConnected(const RawAddress& bdaddr,
163                                VolumeChangedCb cb) = 0;
164 
165   // Indicate that a device has been disconnected from AVRCP. Will unregister
166   // any callbacks if absolute volume is supported.
167   virtual void DeviceDisconnected(const RawAddress& bdaddr) = 0;
168 
169   virtual void SetVolume(int8_t volume) = 0;
170 
171   virtual ~VolumeInterface() = default;
172 };
173 
174 class ServiceInterface {
175  public:
176   // mediaInterface can not be null. If volumeInterface is null then Absolute
177   // Volume is disabled.
178   virtual void Init(MediaInterface* mediaInterface,
179                     VolumeInterface* volumeInterface) = 0;
180   virtual void RegisterBipServer(int psm) = 0;
181   virtual void UnregisterBipServer() = 0;
182   virtual bool ConnectDevice(const RawAddress& bdaddr) = 0;
183   virtual bool DisconnectDevice(const RawAddress& bdaddr) = 0;
184   virtual void SetBipClientStatus(const RawAddress& bdaddr, bool connected) = 0;
185   virtual bool Cleanup() = 0;
186 
187  protected:
188   virtual ~ServiceInterface() = default;
189 };
190 
191 }  // namespace avrcp
192 }  // namespace bluetooth
193