blob: 6c60a3b5bf307666dd4ed348d3e30d785a6217b2 [file] [log] [blame]
Josh Wu20bac522021-12-29 23:52:39 -08001/*
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
21namespace aidl {
22namespace android {
23namespace hardware {
24namespace bluetooth {
25namespace audio {
26
27class 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 Wu75462aa2022-01-21 21:51:21 -080082 }
83 switch (session_type) {
84 case SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
Alice Kuoadcceec2022-03-28 13:28:43 +080085 case SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH:
Josh Wu75462aa2022-01-21 21:51:21 -080086 return AudioConfiguration(CodecConfiguration{});
87 case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
88 case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH:
89 return AudioConfiguration(LeAudioConfiguration{});
Alice Kuoe80a5762022-02-09 14:44:29 +080090 case SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
91 return AudioConfiguration(LeAudioBroadcastConfiguration{});
Josh Wu75462aa2022-01-21 21:51:21 -080092 default:
93 return AudioConfiguration(PcmConfiguration{});
Josh Wu20bac522021-12-29 23:52:39 -080094 }
95 }
96
97 /***
Alice Kuo851ef342022-08-25 02:45:02 +080098 * The control API for the bluetooth_audio module to get current
99 * LE audio connection map
100 ***/
101 static const AudioConfiguration GetLeAudioConnectionMap(
102 const SessionType& session_type) {
103 std::shared_ptr<BluetoothAudioSession> session_ptr =
104 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
105 if ((session_type ==
106 SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
107 session_type ==
108 SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) &&
109 session_ptr != nullptr) {
110 return session_ptr->GetLeAudioConnectionMap();
111 }
112
113 return AudioConfiguration(LeAudioConfiguration{});
114 }
115
116 /***
Josh Wu20bac522021-12-29 23:52:39 -0800117 * Those control APIs for the bluetooth_audio module to start / suspend /
118 stop
119 * stream, to check position, and to update metadata.
120 ***/
Cheney Ni6ecbc762022-03-03 00:12:48 +0800121 static bool StartStream(const SessionType& session_type,
122 bool low_latency = false) {
Josh Wu20bac522021-12-29 23:52:39 -0800123 std::shared_ptr<BluetoothAudioSession> session_ptr =
124 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
125 if (session_ptr != nullptr) {
Cheney Ni6ecbc762022-03-03 00:12:48 +0800126 return session_ptr->StartStream(low_latency);
Josh Wu20bac522021-12-29 23:52:39 -0800127 }
128 return false;
129 }
130
131 static bool SuspendStream(const SessionType& session_type) {
132 std::shared_ptr<BluetoothAudioSession> session_ptr =
133 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
134 if (session_ptr != nullptr) {
135 return session_ptr->SuspendStream();
136 }
137 return false;
138 }
139
140 static void StopStream(const SessionType& session_type) {
141 std::shared_ptr<BluetoothAudioSession> session_ptr =
142 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
143 if (session_ptr != nullptr) {
144 session_ptr->StopStream();
145 }
146 }
147
148 static bool GetPresentationPosition(
149 const SessionType& session_type,
150 PresentationPosition& presentation_position) {
151 std::shared_ptr<BluetoothAudioSession> session_ptr =
152 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
153 if (session_ptr != nullptr) {
154 return session_ptr->GetPresentationPosition(presentation_position);
155 }
156 return false;
157 }
158
159 static void UpdateSourceMetadata(
160 const SessionType& session_type,
161 const struct source_metadata& source_metadata) {
162 std::shared_ptr<BluetoothAudioSession> session_ptr =
163 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
164 if (session_ptr != nullptr) {
165 session_ptr->UpdateSourceMetadata(source_metadata);
166 }
167 }
168
169 static void UpdateSinkMetadata(const SessionType& session_type,
170 const struct sink_metadata& sink_metadata) {
171 std::shared_ptr<BluetoothAudioSession> session_ptr =
172 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
173 if (session_ptr != nullptr) {
174 session_ptr->UpdateSinkMetadata(sink_metadata);
175 }
176 }
177
Mikhail Naganovd5f0d132023-07-26 17:26:02 -0700178 static bool UpdateSourceMetadata(const SessionType& session_type,
179 const SourceMetadata& source_metadata) {
180 std::shared_ptr<BluetoothAudioSession> session_ptr =
181 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
182 if (session_ptr != nullptr) {
183 return session_ptr->UpdateSourceMetadata(source_metadata);
184 }
185 return false;
186 }
187
188 static bool UpdateSinkMetadata(const SessionType& session_type,
189 const SinkMetadata& sink_metadata) {
190 std::shared_ptr<BluetoothAudioSession> session_ptr =
191 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
192 if (session_ptr != nullptr) {
193 return session_ptr->UpdateSinkMetadata(sink_metadata);
194 }
195 return false;
196 }
197
Cheney Ni6ecbc762022-03-03 00:12:48 +0800198 static std::vector<LatencyMode> GetSupportedLatencyModes(
199 const SessionType& session_type) {
200 std::shared_ptr<BluetoothAudioSession> session_ptr =
201 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
202 if (session_ptr != nullptr) {
203 return session_ptr->GetSupportedLatencyModes();
204 }
205 return std::vector<LatencyMode>();
206 }
207
208 static void SetLatencyMode(const SessionType& session_type,
209 const LatencyMode& latency_mode) {
210 std::shared_ptr<BluetoothAudioSession> session_ptr =
211 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
212 if (session_ptr != nullptr) {
213 session_ptr->SetLatencyMode(latency_mode);
214 }
215 }
216
Josh Wu20bac522021-12-29 23:52:39 -0800217 /***
218 * The control API writes stream to FMQ
219 ***/
220 static size_t OutWritePcmData(const SessionType& session_type,
221 const void* buffer, size_t bytes) {
222 std::shared_ptr<BluetoothAudioSession> session_ptr =
223 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
224 if (session_ptr != nullptr) {
225 return session_ptr->OutWritePcmData(buffer, bytes);
226 }
227 return 0;
228 }
229
230 /***
231 * The control API reads stream from FMQ
232 ***/
233 static size_t InReadPcmData(const SessionType& session_type, void* buffer,
234 size_t bytes) {
235 std::shared_ptr<BluetoothAudioSession> session_ptr =
236 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
237 if (session_ptr != nullptr) {
238 return session_ptr->InReadPcmData(buffer, bytes);
239 }
240 return 0;
241 }
242};
243
244} // namespace audio
245} // namespace bluetooth
246} // namespace hardware
247} // namespace android
Cheney Ni6ecbc762022-03-03 00:12:48 +0800248} // namespace aidl