xref: /aosp_15_r20/hardware/interfaces/bluetooth/audio/utils/aidl_session/BluetoothAudioSessionControl.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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    ***/
IsSessionReady(const SessionType & session_type)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    ***/
RegisterControlResultCback(const SessionType & session_type,const PortStatusCallbacks & cbacks)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    ***/
UnregisterControlResultCback(const SessionType & session_type,uint16_t cookie)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    ***/
GetAudioConfig(const SessionType & session_type)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();
82     }
83     switch (session_type) {
84       case SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
85       case SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH:
86         return AudioConfiguration(CodecConfiguration{});
87       case SessionType::HFP_HARDWARE_OFFLOAD_DATAPATH:
88         return AudioConfiguration(HfpConfiguration{});
89       case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
90       case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH:
91         return AudioConfiguration(LeAudioConfiguration{});
92       case SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
93         return AudioConfiguration(LeAudioBroadcastConfiguration{});
94       default:
95         return AudioConfiguration(PcmConfiguration{});
96     }
97   }
98 
99   /***
100    * Those control APIs for the bluetooth_audio module to start / suspend /
101   stop
102    * stream, to check position, and to update metadata.
103   ***/
104   static bool StartStream(const SessionType& session_type,
105                           bool low_latency = false) {
106     std::shared_ptr<BluetoothAudioSession> session_ptr =
107         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
108     if (session_ptr != nullptr) {
109       return session_ptr->StartStream(low_latency);
110     }
111     return false;
112   }
113 
SuspendStream(const SessionType & session_type)114   static bool SuspendStream(const SessionType& session_type) {
115     std::shared_ptr<BluetoothAudioSession> session_ptr =
116         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
117     if (session_ptr != nullptr) {
118       return session_ptr->SuspendStream();
119     }
120     return false;
121   }
122 
StopStream(const SessionType & session_type)123   static void StopStream(const SessionType& session_type) {
124     std::shared_ptr<BluetoothAudioSession> session_ptr =
125         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
126     if (session_ptr != nullptr) {
127       session_ptr->StopStream();
128     }
129   }
130 
GetPresentationPosition(const SessionType & session_type,PresentationPosition & presentation_position)131   static bool GetPresentationPosition(
132       const SessionType& session_type,
133       PresentationPosition& presentation_position) {
134     std::shared_ptr<BluetoothAudioSession> session_ptr =
135         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
136     if (session_ptr != nullptr) {
137       return session_ptr->GetPresentationPosition(presentation_position);
138     }
139     return false;
140   }
141 
UpdateSourceMetadata(const SessionType & session_type,const struct source_metadata & source_metadata)142   static void UpdateSourceMetadata(
143       const SessionType& session_type,
144       const struct source_metadata& source_metadata) {
145     std::shared_ptr<BluetoothAudioSession> session_ptr =
146         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
147     if (session_ptr != nullptr) {
148       session_ptr->UpdateSourceMetadata(source_metadata);
149     }
150   }
151 
UpdateSinkMetadata(const SessionType & session_type,const struct sink_metadata & sink_metadata)152   static void UpdateSinkMetadata(const SessionType& session_type,
153                                  const struct sink_metadata& sink_metadata) {
154     std::shared_ptr<BluetoothAudioSession> session_ptr =
155         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
156     if (session_ptr != nullptr) {
157       session_ptr->UpdateSinkMetadata(sink_metadata);
158     }
159   }
160 
UpdateSourceMetadata(const SessionType & session_type,const SourceMetadata & source_metadata)161   static bool UpdateSourceMetadata(const SessionType& session_type,
162                                    const SourceMetadata& source_metadata) {
163     std::shared_ptr<BluetoothAudioSession> session_ptr =
164         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
165     if (session_ptr != nullptr) {
166       return session_ptr->UpdateSourceMetadata(source_metadata);
167     }
168     return false;
169   }
170 
UpdateSinkMetadata(const SessionType & session_type,const SinkMetadata & sink_metadata)171   static bool UpdateSinkMetadata(const SessionType& session_type,
172                                  const SinkMetadata& sink_metadata) {
173     std::shared_ptr<BluetoothAudioSession> session_ptr =
174         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
175     if (session_ptr != nullptr) {
176       return session_ptr->UpdateSinkMetadata(sink_metadata);
177     }
178     return false;
179   }
180 
GetSupportedLatencyModes(const SessionType & session_type)181   static std::vector<LatencyMode> GetSupportedLatencyModes(
182       const SessionType& session_type) {
183     std::shared_ptr<BluetoothAudioSession> session_ptr =
184         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
185     if (session_ptr != nullptr) {
186       return session_ptr->GetSupportedLatencyModes();
187     }
188     return std::vector<LatencyMode>();
189   }
190 
SetLatencyMode(const SessionType & session_type,const LatencyMode & latency_mode)191   static void SetLatencyMode(const SessionType& session_type,
192                              const LatencyMode& latency_mode) {
193     std::shared_ptr<BluetoothAudioSession> session_ptr =
194         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
195     if (session_ptr != nullptr) {
196       session_ptr->SetLatencyMode(latency_mode);
197     }
198   }
199 
200   /***
201    * The control API writes stream to FMQ
202    ***/
OutWritePcmData(const SessionType & session_type,const void * buffer,size_t bytes)203   static size_t OutWritePcmData(const SessionType& session_type,
204                                 const void* buffer, size_t bytes) {
205     std::shared_ptr<BluetoothAudioSession> session_ptr =
206         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
207     if (session_ptr != nullptr) {
208       return session_ptr->OutWritePcmData(buffer, bytes);
209     }
210     return 0;
211   }
212 
213   /***
214    * The control API reads stream from FMQ
215    ***/
InReadPcmData(const SessionType & session_type,void * buffer,size_t bytes)216   static size_t InReadPcmData(const SessionType& session_type, void* buffer,
217                               size_t bytes) {
218     std::shared_ptr<BluetoothAudioSession> session_ptr =
219         BluetoothAudioSessionInstance::GetSessionInstance(session_type);
220     if (session_ptr != nullptr) {
221       return session_ptr->InReadPcmData(buffer, bytes);
222     }
223     return 0;
224   }
225 };
226 
227 }  // namespace audio
228 }  // namespace bluetooth
229 }  // namespace hardware
230 }  // namespace android
231 }  // namespace aidl
232