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