blob: 0782c824e1ab26e9f1faf8d68c49bec1b9645284 [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 /***
98 * Those control APIs for the bluetooth_audio module to start / suspend /
99 stop
100 * stream, to check position, and to update metadata.
101 ***/
Cheney Ni6ecbc762022-03-03 00:12:48 +0800102 static bool StartStream(const SessionType& session_type,
103 bool low_latency = false) {
Josh Wu20bac522021-12-29 23:52:39 -0800104 std::shared_ptr<BluetoothAudioSession> session_ptr =
105 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
106 if (session_ptr != nullptr) {
Cheney Ni6ecbc762022-03-03 00:12:48 +0800107 return session_ptr->StartStream(low_latency);
Josh Wu20bac522021-12-29 23:52:39 -0800108 }
109 return false;
110 }
111
112 static bool SuspendStream(const SessionType& session_type) {
113 std::shared_ptr<BluetoothAudioSession> session_ptr =
114 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
115 if (session_ptr != nullptr) {
116 return session_ptr->SuspendStream();
117 }
118 return false;
119 }
120
121 static void StopStream(const SessionType& session_type) {
122 std::shared_ptr<BluetoothAudioSession> session_ptr =
123 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
124 if (session_ptr != nullptr) {
125 session_ptr->StopStream();
126 }
127 }
128
129 static bool GetPresentationPosition(
130 const SessionType& session_type,
131 PresentationPosition& presentation_position) {
132 std::shared_ptr<BluetoothAudioSession> session_ptr =
133 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
134 if (session_ptr != nullptr) {
135 return session_ptr->GetPresentationPosition(presentation_position);
136 }
137 return false;
138 }
139
140 static void UpdateSourceMetadata(
141 const SessionType& session_type,
142 const struct source_metadata& source_metadata) {
143 std::shared_ptr<BluetoothAudioSession> session_ptr =
144 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
145 if (session_ptr != nullptr) {
146 session_ptr->UpdateSourceMetadata(source_metadata);
147 }
148 }
149
150 static void UpdateSinkMetadata(const SessionType& session_type,
151 const struct sink_metadata& sink_metadata) {
152 std::shared_ptr<BluetoothAudioSession> session_ptr =
153 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
154 if (session_ptr != nullptr) {
155 session_ptr->UpdateSinkMetadata(sink_metadata);
156 }
157 }
158
Cheney Ni6ecbc762022-03-03 00:12:48 +0800159 static std::vector<LatencyMode> GetSupportedLatencyModes(
160 const SessionType& session_type) {
161 std::shared_ptr<BluetoothAudioSession> session_ptr =
162 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
163 if (session_ptr != nullptr) {
164 return session_ptr->GetSupportedLatencyModes();
165 }
166 return std::vector<LatencyMode>();
167 }
168
169 static void SetLatencyMode(const SessionType& session_type,
170 const LatencyMode& latency_mode) {
171 std::shared_ptr<BluetoothAudioSession> session_ptr =
172 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
173 if (session_ptr != nullptr) {
174 session_ptr->SetLatencyMode(latency_mode);
175 }
176 }
177
Josh Wu20bac522021-12-29 23:52:39 -0800178 /***
179 * The control API writes stream to FMQ
180 ***/
181 static size_t OutWritePcmData(const SessionType& session_type,
182 const void* buffer, size_t bytes) {
183 std::shared_ptr<BluetoothAudioSession> session_ptr =
184 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
185 if (session_ptr != nullptr) {
186 return session_ptr->OutWritePcmData(buffer, bytes);
187 }
188 return 0;
189 }
190
191 /***
192 * The control API reads stream from FMQ
193 ***/
194 static size_t InReadPcmData(const SessionType& session_type, void* buffer,
195 size_t bytes) {
196 std::shared_ptr<BluetoothAudioSession> session_ptr =
197 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
198 if (session_ptr != nullptr) {
199 return session_ptr->InReadPcmData(buffer, bytes);
200 }
201 return 0;
202 }
203};
204
205} // namespace audio
206} // namespace bluetooth
207} // namespace hardware
208} // namespace android
Cheney Ni6ecbc762022-03-03 00:12:48 +0800209} // namespace aidl