Cheney Ni | 47a8309 | 2018-11-17 01:42:19 +0800 | [diff] [blame] | 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 <mutex> |
| 20 | #include <unordered_map> |
| 21 | |
| 22 | #include <android/hardware/bluetooth/audio/2.0/IBluetoothAudioPort.h> |
| 23 | #include <fmq/MessageQueue.h> |
| 24 | #include <hardware/audio.h> |
| 25 | #include <hidl/MQDescriptor.h> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace bluetooth { |
| 29 | namespace audio { |
| 30 | |
| 31 | using ::android::sp; |
| 32 | using ::android::hardware::kSynchronizedReadWrite; |
| 33 | using ::android::hardware::MessageQueue; |
| 34 | using ::android::hardware::bluetooth::audio::V2_0::AudioConfiguration; |
| 35 | using ::android::hardware::bluetooth::audio::V2_0::BitsPerSample; |
| 36 | using ::android::hardware::bluetooth::audio::V2_0::ChannelMode; |
| 37 | using ::android::hardware::bluetooth::audio::V2_0::CodecConfiguration; |
| 38 | using ::android::hardware::bluetooth::audio::V2_0::IBluetoothAudioPort; |
| 39 | using ::android::hardware::bluetooth::audio::V2_0::PcmParameters; |
| 40 | using ::android::hardware::bluetooth::audio::V2_0::SampleRate; |
| 41 | using ::android::hardware::bluetooth::audio::V2_0::SessionType; |
| 42 | |
| 43 | using BluetoothAudioStatus = |
| 44 | ::android::hardware::bluetooth::audio::V2_0::Status; |
| 45 | |
| 46 | using DataMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>; |
| 47 | |
| 48 | static constexpr uint16_t kObserversCookieSize = 0x0010; // 0x0000 ~ 0x000f |
| 49 | constexpr uint16_t kObserversCookieUndefined = |
| 50 | (static_cast<uint16_t>(SessionType::UNKNOWN) << 8 & 0xff00); |
| 51 | inline SessionType ObserversCookieGetSessionType(uint16_t cookie) { |
| 52 | return static_cast<SessionType>(cookie >> 8 & 0x00ff); |
| 53 | } |
| 54 | inline uint16_t ObserversCookieGetInitValue(SessionType session_type) { |
| 55 | return (static_cast<uint16_t>(session_type) << 8 & 0xff00); |
| 56 | } |
| 57 | inline uint16_t ObserversCookieGetUpperBound(SessionType session_type) { |
| 58 | return (static_cast<uint16_t>(session_type) << 8 & 0xff00) + |
| 59 | kObserversCookieSize; |
| 60 | } |
| 61 | |
| 62 | // This presents the callbacks of started / suspended and session changed, |
| 63 | // and the bluetooth_audio module uses to receive the status notification |
| 64 | struct PortStatusCallbacks { |
| 65 | // control_result_cb_ - when the Bluetooth stack reports results of |
| 66 | // streamStarted or streamSuspended, the BluetoothAudioProvider will invoke |
| 67 | // this callback to report to the bluetooth_audio module. |
| 68 | // @param: cookie - indicates which bluetooth_audio output should handle |
| 69 | // @param: start_resp - this report is for startStream or not |
| 70 | // @param: status - the result of startStream |
| 71 | std::function<void(uint16_t cookie, bool start_resp, |
| 72 | const BluetoothAudioStatus& status)> |
| 73 | control_result_cb_; |
| 74 | // session_changed_cb_ - when the Bluetooth stack start / end session, the |
| 75 | // BluetoothAudioProvider will invoke this callback to notify to the |
| 76 | // bluetooth_audio module. |
| 77 | // @param: cookie - indicates which bluetooth_audio output should handle |
| 78 | std::function<void(uint16_t cookie)> session_changed_cb_; |
| 79 | }; |
| 80 | |
| 81 | class BluetoothAudioSession { |
| 82 | private: |
| 83 | // using recursive_mutex to allow hwbinder to re-enter agian. |
| 84 | std::recursive_mutex mutex_; |
| 85 | SessionType session_type_; |
| 86 | |
| 87 | // audio control path to use for both software and offloading |
| 88 | sp<IBluetoothAudioPort> stack_iface_; |
| 89 | // audio data path (FMQ) for software encoding |
| 90 | std::unique_ptr<DataMQ> mDataMQ; |
| 91 | // audio data configuration for both software and offloading |
| 92 | AudioConfiguration audio_config_; |
| 93 | |
| 94 | static AudioConfiguration invalidSoftwareAudioConfiguration; |
| 95 | static AudioConfiguration invalidOffloadAudioConfiguration; |
| 96 | |
| 97 | // saving those registered bluetooth_audio's callbacks |
| 98 | std::unordered_map<uint16_t, std::shared_ptr<struct PortStatusCallbacks>> |
| 99 | observers_; |
| 100 | |
| 101 | bool UpdateDataPath(const DataMQ::Descriptor* dataMQ); |
| 102 | bool UpdateAudioConfig(const AudioConfiguration& audio_config); |
| 103 | // invoking the registered session_changed_cb_ |
| 104 | void ReportSessionStatus(); |
| 105 | |
| 106 | public: |
| 107 | BluetoothAudioSession(const SessionType& session_type); |
| 108 | |
| 109 | // The function helps to check if this session is ready or not |
| 110 | // @return: true if the Bluetooth stack has started the specified session |
| 111 | bool IsSessionReady(); |
| 112 | |
| 113 | // The report function is used to report that the Bluetooth stack has started |
| 114 | // this session without any failure, and will invoke session_changed_cb_ to |
| 115 | // notify those registered bluetooth_audio outputs |
| 116 | void OnSessionStarted(const sp<IBluetoothAudioPort> stack_iface, |
| 117 | const DataMQ::Descriptor* dataMQ, |
| 118 | const AudioConfiguration& audio_config); |
| 119 | |
| 120 | // The report function is used to report that the Bluetooth stack has ended |
| 121 | // the session, and will invoke session_changed_cb_ to notify registered |
| 122 | // bluetooth_audio outputs |
| 123 | void OnSessionEnded(); |
| 124 | |
| 125 | // The report function is used to report that the Bluetooth stack has notified |
| 126 | // the result of startStream or suspendStream, and will invoke |
| 127 | // control_result_cb_ to notify registered bluetooth_audio outputs |
| 128 | void ReportControlStatus(bool start_resp, const BluetoothAudioStatus& status); |
| 129 | |
| 130 | // The control function helps the bluetooth_audio module to register |
| 131 | // PortStatusCallbacks |
| 132 | // @return: cookie - the assigned number to this bluetooth_audio output |
| 133 | uint16_t RegisterStatusCback(const PortStatusCallbacks& cbacks); |
| 134 | |
| 135 | // The control function helps the bluetooth_audio module to unregister |
| 136 | // PortStatusCallbacks |
| 137 | // @param: cookie - indicates which bluetooth_audio output is |
| 138 | void UnregisterStatusCback(uint16_t cookie); |
| 139 | |
| 140 | // The control function is for the bluetooth_audio module to get the current |
| 141 | // AudioConfiguration |
| 142 | const AudioConfiguration& GetAudioConfig(); |
| 143 | |
| 144 | // Those control functions are for the bluetooth_audio module to start, |
| 145 | // suspend, stop stream, to check position, and to update metadata. |
| 146 | bool StartStream(); |
| 147 | bool SuspendStream(); |
| 148 | void StopStream(); |
| 149 | bool GetPresentationPosition(uint64_t* remote_delay_report_ns, |
| 150 | uint64_t* total_bytes_readed, |
| 151 | timespec* data_position); |
| 152 | void UpdateTracksMetadata(const struct source_metadata* source_metadata); |
| 153 | |
| 154 | // The control function writes stream to FMQ |
| 155 | size_t OutWritePcmData(const void* buffer, size_t bytes); |
| 156 | |
| 157 | static constexpr PcmParameters kInvalidPcmParameters = { |
| 158 | .sampleRate = SampleRate::RATE_UNKNOWN, |
Nick Desaulniers | 0fd95b2 | 2019-10-09 11:28:47 -0700 | [diff] [blame] | 159 | .channelMode = ChannelMode::UNKNOWN, |
Cheney Ni | 47a8309 | 2018-11-17 01:42:19 +0800 | [diff] [blame] | 160 | .bitsPerSample = BitsPerSample::BITS_UNKNOWN, |
Nick Desaulniers | 0fd95b2 | 2019-10-09 11:28:47 -0700 | [diff] [blame] | 161 | }; |
Cheney Ni | 47a8309 | 2018-11-17 01:42:19 +0800 | [diff] [blame] | 162 | // can't be constexpr because of non-literal type |
| 163 | static const CodecConfiguration kInvalidCodecConfiguration; |
| 164 | |
| 165 | static constexpr AudioConfiguration& kInvalidSoftwareAudioConfiguration = |
| 166 | invalidSoftwareAudioConfiguration; |
| 167 | static constexpr AudioConfiguration& kInvalidOffloadAudioConfiguration = |
| 168 | invalidOffloadAudioConfiguration; |
| 169 | }; |
| 170 | |
| 171 | class BluetoothAudioSessionInstance { |
| 172 | public: |
| 173 | // The API is to fetch the specified session of A2DP / Hearing Aid |
| 174 | static std::shared_ptr<BluetoothAudioSession> GetSessionInstance( |
| 175 | const SessionType& session_type); |
| 176 | |
| 177 | private: |
| 178 | static std::unique_ptr<BluetoothAudioSessionInstance> instance_ptr; |
| 179 | std::mutex mutex_; |
| 180 | std::unordered_map<SessionType, std::shared_ptr<BluetoothAudioSession>> |
| 181 | sessions_map_; |
| 182 | }; |
| 183 | |
| 184 | } // namespace audio |
| 185 | } // namespace bluetooth |
| 186 | } // namespace android |