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