Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 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> |
Chen Chen | a4c4c61 | 2022-02-07 18:01:05 -0800 | [diff] [blame] | 23 | #include <aidl/android/hardware/bluetooth/audio/LatencyMode.h> |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 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> |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 30 | #include <vector> |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 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); |
| 55 | inline SessionType ObserversCookieGetSessionType(uint16_t cookie) { |
| 56 | return static_cast<SessionType>(cookie >> 8 & 0x00ff); |
| 57 | } |
| 58 | inline uint16_t ObserversCookieGetInitValue(SessionType session_type) { |
| 59 | return (static_cast<uint16_t>(session_type) << 8 & 0xff00); |
| 60 | } |
| 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_; |
Chen Chen | 81f38e5 | 2022-02-09 13:27:35 -0800 | [diff] [blame] | 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_; |
Alice Kuo | 851ef34 | 2022-08-25 02:45:02 +0800 | [diff] [blame^] | 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_; |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 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, |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 131 | const AudioConfiguration& audio_config, |
| 132 | const std::vector<LatencyMode>& latency_modes); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 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 | ***/ |
Josh Wu | 75462aa | 2022-01-21 21:51:21 -0800 | [diff] [blame] | 166 | const AudioConfiguration GetAudioConfig(); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 167 | |
| 168 | /*** |
Alice Kuo | 851ef34 | 2022-08-25 02:45:02 +0800 | [diff] [blame^] | 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 | /*** |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 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 | /*** |
Chen Chen | 81f38e5 | 2022-02-09 13:27:35 -0800 | [diff] [blame] | 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 | /*** |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 190 | * Those control functions are for the bluetooth_audio module to start, |
| 191 | * suspend, stop stream, to check position, and to update metadata. |
| 192 | ***/ |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 193 | bool StartStream(bool low_latency); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 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); |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 199 | |
| 200 | std::vector<LatencyMode> GetSupportedLatencyModes(); |
| 201 | void SetLatencyMode(const LatencyMode& latency_mode); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 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 | |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 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_; |
Alice Kuo | 851ef34 | 2022-08-25 02:45:02 +0800 | [diff] [blame^] | 222 | std::unique_ptr<AudioConfiguration> leaudio_connection_map_; |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 223 | std::vector<LatencyMode> latency_modes_; |
| 224 | bool low_latency_allowed_ = true; |
Alice Kuo | 851ef34 | 2022-08-25 02:45:02 +0800 | [diff] [blame^] | 225 | // saving those steaming state based on the session_type |
| 226 | bool is_streaming_ = false; |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 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 |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 259 | } // namespace aidl |