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_2_1.h" 20 21 namespace android { 22 namespace bluetooth { 23 namespace audio { 24 25 class BluetoothAudioSessionControl_2_1 { 26 using SessionType_2_1 = 27 ::android::hardware::bluetooth::audio::V2_1::SessionType; 28 using AudioConfiguration_2_1 = 29 ::android::hardware::bluetooth::audio::V2_1::AudioConfiguration; 30 31 public: 32 // The control API helps to check if session is ready or not 33 // @return: true if the Bluetooth stack has started th specified session IsSessionReady(const SessionType_2_1 & session_type)34 static bool IsSessionReady(const SessionType_2_1& session_type) { 35 std::shared_ptr<BluetoothAudioSession_2_1> session_ptr = 36 BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type); 37 if (session_ptr != nullptr) { 38 return session_ptr->GetAudioSession()->IsSessionReady(); 39 } 40 return false; 41 } 42 43 // The control API helps the bluetooth_audio module to register 44 // PortStatusCallbacks 45 // @return: cookie - the assigned number to this bluetooth_audio output RegisterControlResultCback(const SessionType_2_1 & session_type,const PortStatusCallbacks & cbacks)46 static uint16_t RegisterControlResultCback( 47 const SessionType_2_1& session_type, const PortStatusCallbacks& cbacks) { 48 std::shared_ptr<BluetoothAudioSession_2_1> session_ptr = 49 BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type); 50 if (session_ptr != nullptr) { 51 return session_ptr->GetAudioSession()->RegisterStatusCback(cbacks); 52 } 53 return kObserversCookieUndefined; 54 } 55 56 // The control API helps the bluetooth_audio module to unregister 57 // PortStatusCallbacks 58 // @param: cookie - indicates which bluetooth_audio output is UnregisterControlResultCback(const SessionType_2_1 & session_type,uint16_t cookie)59 static void UnregisterControlResultCback(const SessionType_2_1& session_type, 60 uint16_t cookie) { 61 std::shared_ptr<BluetoothAudioSession_2_1> session_ptr = 62 BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type); 63 if (session_ptr != nullptr) { 64 session_ptr->GetAudioSession()->UnregisterStatusCback(cookie); 65 } 66 } 67 68 // The control API for the bluetooth_audio module to get current 69 // AudioConfiguration GetAudioConfig(const SessionType_2_1 & session_type)70 static const AudioConfiguration_2_1 GetAudioConfig( 71 const SessionType_2_1& session_type) { 72 std::shared_ptr<BluetoothAudioSession_2_1> session_ptr = 73 BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type); 74 if (session_ptr != nullptr) { 75 return session_ptr->GetAudioConfig(); 76 } else if (session_type == 77 SessionType_2_1::A2DP_HARDWARE_OFFLOAD_DATAPATH) { 78 return BluetoothAudioSession_2_1::kInvalidOffloadAudioConfiguration; 79 } else { 80 return BluetoothAudioSession_2_1::kInvalidSoftwareAudioConfiguration; 81 } 82 } 83 84 // Those control APIs for the bluetooth_audio module to start / suspend / stop 85 // stream, to check position, and to update metadata. StartStream(const SessionType_2_1 & session_type)86 static bool StartStream(const SessionType_2_1& session_type) { 87 std::shared_ptr<BluetoothAudioSession_2_1> session_ptr = 88 BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type); 89 if (session_ptr != nullptr) { 90 return session_ptr->GetAudioSession()->StartStream(); 91 } 92 return false; 93 } 94 SuspendStream(const SessionType_2_1 & session_type)95 static bool SuspendStream(const SessionType_2_1& session_type) { 96 std::shared_ptr<BluetoothAudioSession_2_1> session_ptr = 97 BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type); 98 if (session_ptr != nullptr) { 99 return session_ptr->GetAudioSession()->SuspendStream(); 100 } 101 return false; 102 } 103 StopStream(const SessionType_2_1 & session_type)104 static void StopStream(const SessionType_2_1& session_type) { 105 std::shared_ptr<BluetoothAudioSession_2_1> session_ptr = 106 BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type); 107 if (session_ptr != nullptr) { 108 session_ptr->GetAudioSession()->StopStream(); 109 } 110 } 111 GetPresentationPosition(const SessionType_2_1 & session_type,uint64_t * remote_delay_report_ns,uint64_t * total_bytes_readed,timespec * data_position)112 static bool GetPresentationPosition(const SessionType_2_1& session_type, 113 uint64_t* remote_delay_report_ns, 114 uint64_t* total_bytes_readed, 115 timespec* data_position) { 116 std::shared_ptr<BluetoothAudioSession_2_1> session_ptr = 117 BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type); 118 if (session_ptr != nullptr) { 119 return session_ptr->GetAudioSession()->GetPresentationPosition( 120 remote_delay_report_ns, total_bytes_readed, data_position); 121 } 122 return false; 123 } 124 UpdateTracksMetadata(const SessionType_2_1 & session_type,const struct source_metadata * source_metadata)125 static void UpdateTracksMetadata( 126 const SessionType_2_1& session_type, 127 const struct source_metadata* source_metadata) { 128 std::shared_ptr<BluetoothAudioSession_2_1> session_ptr = 129 BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type); 130 if (session_ptr != nullptr) { 131 session_ptr->GetAudioSession()->UpdateTracksMetadata(source_metadata); 132 } 133 } 134 135 // The control API writes stream to FMQ OutWritePcmData(const SessionType_2_1 & session_type,const void * buffer,size_t bytes)136 static size_t OutWritePcmData(const SessionType_2_1& session_type, 137 const void* buffer, size_t bytes) { 138 std::shared_ptr<BluetoothAudioSession_2_1> session_ptr = 139 BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type); 140 if (session_ptr != nullptr) { 141 return session_ptr->GetAudioSession()->OutWritePcmData(buffer, bytes); 142 } 143 return 0; 144 } 145 146 // The control API reads stream from FMQ InReadPcmData(const SessionType_2_1 & session_type,void * buffer,size_t bytes)147 static size_t InReadPcmData(const SessionType_2_1& session_type, void* buffer, 148 size_t bytes) { 149 std::shared_ptr<BluetoothAudioSession_2_1> session_ptr = 150 BluetoothAudioSessionInstance_2_1::GetSessionInstance(session_type); 151 if (session_ptr != nullptr) { 152 return session_ptr->GetAudioSession()->InReadPcmData(buffer, bytes); 153 } 154 return 0; 155 } 156 }; 157 158 } // namespace audio 159 } // namespace bluetooth 160 } // namespace android 161