blob: 451a31fb7ec962f9c33638f7bd29661bc5e92573 [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:
85 return AudioConfiguration(CodecConfiguration{});
86 case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
87 case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH:
88 return AudioConfiguration(LeAudioConfiguration{});
Alice Kuoe80a5762022-02-09 14:44:29 +080089 case SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
90 return AudioConfiguration(LeAudioBroadcastConfiguration{});
Josh Wu75462aa2022-01-21 21:51:21 -080091 default:
92 return AudioConfiguration(PcmConfiguration{});
Josh Wu20bac522021-12-29 23:52:39 -080093 }
94 }
95
96 /***
97 * Those control APIs for the bluetooth_audio module to start / suspend /
98 stop
99 * stream, to check position, and to update metadata.
100 ***/
101 static bool StartStream(const SessionType& session_type) {
102 std::shared_ptr<BluetoothAudioSession> session_ptr =
103 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
104 if (session_ptr != nullptr) {
105 return session_ptr->StartStream();
106 }
107 return false;
108 }
109
110 static bool SuspendStream(const SessionType& session_type) {
111 std::shared_ptr<BluetoothAudioSession> session_ptr =
112 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
113 if (session_ptr != nullptr) {
114 return session_ptr->SuspendStream();
115 }
116 return false;
117 }
118
119 static void StopStream(const SessionType& session_type) {
120 std::shared_ptr<BluetoothAudioSession> session_ptr =
121 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
122 if (session_ptr != nullptr) {
123 session_ptr->StopStream();
124 }
125 }
126
127 static bool GetPresentationPosition(
128 const SessionType& session_type,
129 PresentationPosition& presentation_position) {
130 std::shared_ptr<BluetoothAudioSession> session_ptr =
131 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
132 if (session_ptr != nullptr) {
133 return session_ptr->GetPresentationPosition(presentation_position);
134 }
135 return false;
136 }
137
138 static void UpdateSourceMetadata(
139 const SessionType& session_type,
140 const struct source_metadata& source_metadata) {
141 std::shared_ptr<BluetoothAudioSession> session_ptr =
142 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
143 if (session_ptr != nullptr) {
144 session_ptr->UpdateSourceMetadata(source_metadata);
145 }
146 }
147
148 static void UpdateSinkMetadata(const SessionType& session_type,
149 const struct sink_metadata& sink_metadata) {
150 std::shared_ptr<BluetoothAudioSession> session_ptr =
151 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
152 if (session_ptr != nullptr) {
153 session_ptr->UpdateSinkMetadata(sink_metadata);
154 }
155 }
156
157 /***
158 * The control API writes stream to FMQ
159 ***/
160 static size_t OutWritePcmData(const SessionType& session_type,
161 const void* buffer, size_t bytes) {
162 std::shared_ptr<BluetoothAudioSession> session_ptr =
163 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
164 if (session_ptr != nullptr) {
165 return session_ptr->OutWritePcmData(buffer, bytes);
166 }
167 return 0;
168 }
169
170 /***
171 * The control API reads stream from FMQ
172 ***/
173 static size_t InReadPcmData(const SessionType& session_type, void* buffer,
174 size_t bytes) {
175 std::shared_ptr<BluetoothAudioSession> session_ptr =
176 BluetoothAudioSessionInstance::GetSessionInstance(session_type);
177 if (session_ptr != nullptr) {
178 return session_ptr->InReadPcmData(buffer, bytes);
179 }
180 return 0;
181 }
182};
183
184} // namespace audio
185} // namespace bluetooth
186} // namespace hardware
187} // namespace android
188} // namespace aidl