blob: 52632220848e97cd4149f786dddec3fd4bc42b53 [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{});
Yuyang Huangaa70c112023-10-26 16:01:45 -070087 case SessionType::HFP_HARDWARE_OFFLOAD_DATAPATH:
88 return AudioConfiguration(HfpConfiguration{});
Josh Wu75462aa2022-01-21 21:51:21 -080089 case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
90 case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH:
91 return AudioConfiguration(LeAudioConfiguration{});
Alice Kuoe80a5762022-02-09 14:44:29 +080092 case SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
93 return AudioConfiguration(LeAudioBroadcastConfiguration{});
Josh Wu75462aa2022-01-21 21:51:21 -080094 default:
95 return AudioConfiguration(PcmConfiguration{});
Josh Wu20bac522021-12-29 23:52:39 -080096 }
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 ***/
Cheney Ni6ecbc762022-03-03 00:12:48 +0800104 static bool StartStream(const SessionType& session_type,
105 bool low_latency = false) {
Josh Wu20bac522021-12-29 23:52:39 -0800106 std::shared_ptr<BluetoothAudioSession> session_ptr =
107 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
108 if (session_ptr != nullptr) {
Cheney Ni6ecbc762022-03-03 00:12:48 +0800109 return session_ptr->StartStream(low_latency);
Josh Wu20bac522021-12-29 23:52:39 -0800110 }
111 return false;
112 }
113
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
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
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
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
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
Mikhail Naganovd5f0d132023-07-26 17:26:02 -0700161 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
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
Cheney Ni6ecbc762022-03-03 00:12:48 +0800181 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
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
Josh Wu20bac522021-12-29 23:52:39 -0800200 /***
201 * The control API writes stream to FMQ
202 ***/
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 ***/
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
Cheney Ni6ecbc762022-03-03 00:12:48 +0800231} // namespace aidl