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