• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 <aidl/android/hardware/audio/common/SinkMetadata.h>
20 #include <aidl/android/hardware/audio/common/SourceMetadata.h>
21 #include <aidl/android/hardware/bluetooth/audio/IBluetoothAudioProvider.h>
22 #include <aidl/android/hardware/bluetooth/audio/IBluetoothAudioProviderFactory.h>
23 #include <aidl/android/hardware/bluetooth/audio/LatencyMode.h>
24 #include <aidl/android/hardware/bluetooth/audio/SessionType.h>
25 #include <fmq/AidlMessageQueue.h>
26 #include <hardware/audio.h>
27 
28 #include <mutex>
29 #include <unordered_map>
30 #include <vector>
31 
32 namespace aidl {
33 namespace android {
34 namespace hardware {
35 namespace bluetooth {
36 namespace audio {
37 
38 using ::aidl::android::hardware::common::fmq::MQDescriptor;
39 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
40 using ::android::AidlMessageQueue;
41 
42 using ::aidl::android::hardware::audio::common::SinkMetadata;
43 using ::aidl::android::hardware::audio::common::SourceMetadata;
44 
45 using MQDataType = int8_t;
46 using MQDataMode = SynchronizedReadWrite;
47 using DataMQ = AidlMessageQueue<MQDataType, MQDataMode>;
48 using DataMQDesc =
49     ::aidl::android::hardware::common::fmq::MQDescriptor<MQDataType,
50                                                          MQDataMode>;
51 
52 static constexpr uint16_t kObserversCookieSize = 0x0010;  // 0x0000 ~ 0x000f
53 static constexpr uint16_t kObserversCookieUndefined =
54     (static_cast<uint16_t>(SessionType::UNKNOWN) << 8 & 0xff00);
ObserversCookieGetSessionType(uint16_t cookie)55 inline SessionType ObserversCookieGetSessionType(uint16_t cookie) {
56   return static_cast<SessionType>(cookie >> 8 & 0x00ff);
57 }
ObserversCookieGetInitValue(SessionType session_type)58 inline uint16_t ObserversCookieGetInitValue(SessionType session_type) {
59   return (static_cast<uint16_t>(session_type) << 8 & 0xff00);
60 }
ObserversCookieGetUpperBound(SessionType session_type)61 inline uint16_t ObserversCookieGetUpperBound(SessionType session_type) {
62   return (static_cast<uint16_t>(session_type) << 8 & 0xff00) +
63          kObserversCookieSize;
64 }
65 
66 /***
67  * This presents the callbacks of started / suspended and session changed,
68  * and the bluetooth_audio module uses to receive the status notification
69  ***/
70 struct PortStatusCallbacks {
71   /***
72    * control_result_cb_ - when the Bluetooth stack reports results of
73    * streamStarted or streamSuspended, the BluetoothAudioProvider will invoke
74    * this callback to report to the bluetooth_audio module.
75    * @param: cookie - indicates which bluetooth_audio output should handle
76    * @param: start_resp - this report is for startStream or not
77    * @param: status - the result of startStream
78    ***/
79   std::function<void(uint16_t cookie, bool start_resp,
80                      BluetoothAudioStatus status)>
81       control_result_cb_;
82   /***
83    * session_changed_cb_ - when the Bluetooth stack start / end session, the
84    * BluetoothAudioProvider will invoke this callback to notify to the
85    * bluetooth_audio module.
86    * @param: cookie - indicates which bluetooth_audio output should handle
87    ***/
88   std::function<void(uint16_t cookie)> session_changed_cb_;
89   /***
90    * audio_configuration_changed_cb_ - when the Bluetooth stack change the audio
91    * configuration, the BluetoothAudioProvider will invoke this callback to
92    * notify to the bluetooth_audio module.
93    * @param: cookie - indicates which bluetooth_audio output should handle
94    ***/
95   std::function<void(uint16_t cookie)> audio_configuration_changed_cb_;
96   /***
97    * low_latency_mode_allowed_cb_ - when the Bluetooth stack low latency mode
98    * allowed or disallowed, the BluetoothAudioProvider will invoke
99    * this callback to report to the bluetooth_audio module.
100    * @param: cookie - indicates which bluetooth_audio output should handle
101    * @param: allowed - indicates if low latency mode is allowed
102    ***/
103   std::function<void(uint16_t cookie, bool allowed)>
104       low_latency_mode_allowed_cb_;
105   /***
106    * soft_audio_configuration_changed_cb_ - when the Bluetooth stack change
107    * the streamMap during the streaming, the BluetoothAudioProvider will invoke
108    * this callback to report to the bluetooth_audio module.
109    * @param: cookie - indicates which bluetooth_audio output should handle
110    ***/
111   std::function<void(uint16_t cookie)> soft_audio_configuration_changed_cb_;
112 };
113 
114 class BluetoothAudioSession {
115  public:
116   BluetoothAudioSession(const SessionType& session_type);
117 
118   /***
119    * The function helps to check if this session is ready or not
120    * @return: true if the Bluetooth stack has started the specified session
121    ***/
122   bool IsSessionReady();
123 
124   /***
125    * The report function is used to report that the Bluetooth stack has started
126    * this session without any failure, and will invoke session_changed_cb_ to
127    * notify those registered bluetooth_audio outputs
128    ***/
129   void OnSessionStarted(const std::shared_ptr<IBluetoothAudioPort> stack_iface,
130                         const DataMQDesc* mq_desc,
131                         const AudioConfiguration& audio_config,
132                         const std::vector<LatencyMode>& latency_modes);
133 
134   /***
135    * The report function is used to report that the Bluetooth stack has ended
136    * the session, and will invoke session_changed_cb_ to notify registered
137    * bluetooth_audio outputs
138    ***/
139   void OnSessionEnded();
140 
141   /***
142    * The report function is used to report that the Bluetooth stack has notified
143    * the result of startStream or suspendStream, and will invoke
144    * control_result_cb_ to notify registered bluetooth_audio outputs
145    ***/
146   void ReportControlStatus(bool start_resp, BluetoothAudioStatus status);
147 
148   /***
149    * The control function helps the bluetooth_audio module to register
150    * PortStatusCallbacks
151    * @return: cookie - the assigned number to this bluetooth_audio output
152    ***/
153   uint16_t RegisterStatusCback(const PortStatusCallbacks& cbacks);
154 
155   /***
156    * The control function helps the bluetooth_audio module to unregister
157    * PortStatusCallbacks
158    * @param: cookie - indicates which bluetooth_audio output is
159    ***/
160   void UnregisterStatusCback(uint16_t cookie);
161 
162   /***
163    * The control function is for the bluetooth_audio module to get the current
164    * AudioConfiguration
165    ***/
166   const AudioConfiguration GetAudioConfig();
167 
168   /***
169    * The control function is for the bluetooth_audio module to get the current
170    * LE audio connection map
171    ***/
172   const AudioConfiguration GetLeAudioConnectionMap();
173 
174   /***
175    * The report function is used to report that the Bluetooth stack has notified
176    * the audio configuration changed, and will invoke
177    * audio_configuration_changed_cb_ to notify registered bluetooth_audio
178    * outputs
179    ***/
180   void ReportAudioConfigChanged(const AudioConfiguration& audio_config);
181 
182   /***
183    * The report function is used to report that the Bluetooth stack has notified
184    * the low latency mode allowed changed, and will invoke
185    * low_latency_mode_allowed_changed_cb to notify registered bluetooth_audio
186    * outputs
187    ***/
188   void ReportLowLatencyModeAllowedChanged(bool allowed);
189   /***
190    * Those control functions are for the bluetooth_audio module to start,
191    * suspend, stop stream, to check position, and to update metadata.
192    ***/
193   bool StartStream(bool low_latency);
194   bool SuspendStream();
195   void StopStream();
196   bool GetPresentationPosition(PresentationPosition& presentation_position);
197   void UpdateSourceMetadata(const struct source_metadata& source_metadata);
198   void UpdateSinkMetadata(const struct sink_metadata& sink_metadata);
199 
200   std::vector<LatencyMode> GetSupportedLatencyModes();
201   void SetLatencyMode(const LatencyMode& latency_mode);
202 
203   // The control function writes stream to FMQ
204   size_t OutWritePcmData(const void* buffer, size_t bytes);
205   // The control function read stream from FMQ
206   size_t InReadPcmData(void* buffer, size_t bytes);
207 
208   // Return if IBluetoothAudioProviderFactory implementation existed
209   static bool IsAidlAvailable();
210 
211  private:
212   // using recursive_mutex to allow hwbinder to re-enter again.
213   std::recursive_mutex mutex_;
214   SessionType session_type_;
215 
216   // audio control path to use for both software and offloading
217   std::shared_ptr<IBluetoothAudioPort> stack_iface_;
218   // audio data path (FMQ) for software encoding
219   std::unique_ptr<DataMQ> data_mq_;
220   // audio data configuration for both software and offloading
221   std::unique_ptr<AudioConfiguration> audio_config_;
222   std::unique_ptr<AudioConfiguration> leaudio_connection_map_;
223   std::vector<LatencyMode> latency_modes_;
224   bool low_latency_allowed_ = true;
225   // saving those steaming state based on the session_type
226   bool is_streaming_ = false;
227 
228   // saving those registered bluetooth_audio's callbacks
229   std::unordered_map<uint16_t, std::shared_ptr<struct PortStatusCallbacks>>
230       observers_;
231 
232   bool UpdateDataPath(const DataMQDesc* mq_desc);
233   bool UpdateAudioConfig(const AudioConfiguration& audio_config);
234   // invoking the registered session_changed_cb_
235   void ReportSessionStatus();
236 
237   static inline std::atomic<bool> is_aidl_checked = false;
238   static inline std::atomic<bool> is_aidl_available = false;
239   static inline const std::string kDefaultAudioProviderFactoryInterface =
240       std::string() + IBluetoothAudioProviderFactory::descriptor + "/default";
241 };
242 
243 class BluetoothAudioSessionInstance {
244  public:
245   // The API is to fetch the specified session of A2DP / Hearing Aid
246   static std::shared_ptr<BluetoothAudioSession> GetSessionInstance(
247       const SessionType& session_type);
248 
249  private:
250   static std::mutex mutex_;
251   static std::unordered_map<SessionType, std::shared_ptr<BluetoothAudioSession>>
252       sessions_map_;
253 };
254 
255 }  // namespace audio
256 }  // namespace bluetooth
257 }  // namespace hardware
258 }  // namespace android
259 }  // namespace aidl
260