Josh Wu | 20bac52 | 2021-12-29 23:52:39 -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 "BluetoothAudioSession.h" |
| 20 | |
| 21 | namespace aidl { |
| 22 | namespace android { |
| 23 | namespace hardware { |
| 24 | namespace bluetooth { |
| 25 | namespace audio { |
| 26 | |
| 27 | class BluetoothAudioSessionControl { |
| 28 | public: |
| 29 | /*** |
| 30 | * The control API helps to check if session is ready or not |
| 31 | * @return: true if the Bluetooth stack has started th specified session |
| 32 | ***/ |
| 33 | static bool IsSessionReady(const SessionType& session_type) { |
| 34 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 35 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 36 | if (session_ptr != nullptr) { |
| 37 | return session_ptr->IsSessionReady(); |
| 38 | } |
| 39 | |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | /*** |
| 44 | * The control API helps the bluetooth_audio module to register |
| 45 | * PortStatusCallbacks |
| 46 | * @return: cookie - the assigned number to this bluetooth_audio output |
| 47 | ***/ |
| 48 | static uint16_t RegisterControlResultCback( |
| 49 | const SessionType& session_type, const PortStatusCallbacks& cbacks) { |
| 50 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 51 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 52 | if (session_ptr != nullptr) { |
| 53 | return session_ptr->RegisterStatusCback(cbacks); |
| 54 | } |
| 55 | return kObserversCookieUndefined; |
| 56 | } |
| 57 | |
| 58 | /*** |
| 59 | * The control API helps the bluetooth_audio module to unregister |
| 60 | * PortStatusCallbacks |
| 61 | * @param: cookie - indicates which bluetooth_audio output is |
| 62 | ***/ |
| 63 | static void UnregisterControlResultCback(const SessionType& session_type, |
| 64 | uint16_t cookie) { |
| 65 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 66 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 67 | if (session_ptr != nullptr) { |
| 68 | session_ptr->UnregisterStatusCback(cookie); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /*** |
| 73 | * The control API for the bluetooth_audio module to get current |
| 74 | * AudioConfiguration |
| 75 | ***/ |
| 76 | static const AudioConfiguration GetAudioConfig( |
| 77 | const SessionType& session_type) { |
| 78 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 79 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 80 | if (session_ptr != nullptr) { |
| 81 | return session_ptr->GetAudioConfig(); |
Josh Wu | 75462aa | 2022-01-21 21:51:21 -0800 | [diff] [blame] | 82 | } |
| 83 | switch (session_type) { |
| 84 | case SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH: |
| 85 | return AudioConfiguration(CodecConfiguration{}); |
| 86 | case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH: |
| 87 | case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH: |
| 88 | return AudioConfiguration(LeAudioConfiguration{}); |
Alice Kuo | e80a576 | 2022-02-09 14:44:29 +0800 | [diff] [blame] | 89 | case SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH: |
| 90 | return AudioConfiguration(LeAudioBroadcastConfiguration{}); |
Josh Wu | 75462aa | 2022-01-21 21:51:21 -0800 | [diff] [blame] | 91 | default: |
| 92 | return AudioConfiguration(PcmConfiguration{}); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | /*** |
| 97 | * Those control APIs for the bluetooth_audio module to start / suspend / |
| 98 | stop |
| 99 | * stream, to check position, and to update metadata. |
| 100 | ***/ |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame^] | 101 | static bool StartStream(const SessionType& session_type, |
| 102 | bool low_latency = false) { |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 103 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 104 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 105 | if (session_ptr != nullptr) { |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame^] | 106 | return session_ptr->StartStream(low_latency); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | static bool SuspendStream(const SessionType& session_type) { |
| 112 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 113 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 114 | if (session_ptr != nullptr) { |
| 115 | return session_ptr->SuspendStream(); |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | static void StopStream(const SessionType& session_type) { |
| 121 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 122 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 123 | if (session_ptr != nullptr) { |
| 124 | session_ptr->StopStream(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static bool GetPresentationPosition( |
| 129 | const SessionType& session_type, |
| 130 | PresentationPosition& presentation_position) { |
| 131 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 132 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 133 | if (session_ptr != nullptr) { |
| 134 | return session_ptr->GetPresentationPosition(presentation_position); |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | static void UpdateSourceMetadata( |
| 140 | const SessionType& session_type, |
| 141 | const struct source_metadata& source_metadata) { |
| 142 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 143 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 144 | if (session_ptr != nullptr) { |
| 145 | session_ptr->UpdateSourceMetadata(source_metadata); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | static void UpdateSinkMetadata(const SessionType& session_type, |
| 150 | const struct sink_metadata& sink_metadata) { |
| 151 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 152 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 153 | if (session_ptr != nullptr) { |
| 154 | session_ptr->UpdateSinkMetadata(sink_metadata); |
| 155 | } |
| 156 | } |
| 157 | |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame^] | 158 | static std::vector<LatencyMode> GetSupportedLatencyModes( |
| 159 | const SessionType& session_type) { |
| 160 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 161 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 162 | if (session_ptr != nullptr) { |
| 163 | return session_ptr->GetSupportedLatencyModes(); |
| 164 | } |
| 165 | return std::vector<LatencyMode>(); |
| 166 | } |
| 167 | |
| 168 | static void SetLatencyMode(const SessionType& session_type, |
| 169 | const LatencyMode& latency_mode) { |
| 170 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 171 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 172 | if (session_ptr != nullptr) { |
| 173 | session_ptr->SetLatencyMode(latency_mode); |
| 174 | } |
| 175 | } |
| 176 | |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 177 | /*** |
| 178 | * The control API writes stream to FMQ |
| 179 | ***/ |
| 180 | static size_t OutWritePcmData(const SessionType& session_type, |
| 181 | const void* buffer, size_t bytes) { |
| 182 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 183 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 184 | if (session_ptr != nullptr) { |
| 185 | return session_ptr->OutWritePcmData(buffer, bytes); |
| 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /*** |
| 191 | * The control API reads stream from FMQ |
| 192 | ***/ |
| 193 | static size_t InReadPcmData(const SessionType& session_type, void* buffer, |
| 194 | size_t bytes) { |
| 195 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 196 | BluetoothAudioSessionInstance::GetSessionInstance(session_type); |
| 197 | if (session_ptr != nullptr) { |
| 198 | return session_ptr->InReadPcmData(buffer, bytes); |
| 199 | } |
| 200 | return 0; |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | } // namespace audio |
| 205 | } // namespace bluetooth |
| 206 | } // namespace hardware |
| 207 | } // namespace android |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame^] | 208 | } // namespace aidl |