1 /*
2 * Copyright (C) 2022 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 #define LOG_TAG "BTAudioProviderStub"
18
19 #include "BluetoothAudioProvider.h"
20
21 #include <BluetoothAudioSessionReport.h>
22 #include <android-base/logging.h>
23
24 #include "A2dpOffloadCodecFactory.h"
25
26 namespace aidl {
27 namespace android {
28 namespace hardware {
29 namespace bluetooth {
30 namespace audio {
31
32 struct BluetoothAudioProviderContext {
33 SessionType session_type;
34 };
35
binderUnlinkedCallbackAidl(void * cookie)36 static void binderUnlinkedCallbackAidl(void* cookie) {
37 LOG(INFO) << __func__;
38 BluetoothAudioProviderContext* ctx =
39 static_cast<BluetoothAudioProviderContext*>(cookie);
40 delete ctx;
41 }
42
binderDiedCallbackAidl(void * cookie)43 static void binderDiedCallbackAidl(void* cookie) {
44 LOG(INFO) << __func__;
45 BluetoothAudioProviderContext* ctx =
46 static_cast<BluetoothAudioProviderContext*>(cookie);
47 CHECK_NE(ctx, nullptr);
48
49 BluetoothAudioSessionReport::OnSessionEnded(ctx->session_type);
50 }
51
BluetoothAudioProvider()52 BluetoothAudioProvider::BluetoothAudioProvider() {
53 death_recipient_ = ::ndk::ScopedAIBinder_DeathRecipient(
54 AIBinder_DeathRecipient_new(binderDiedCallbackAidl));
55 AIBinder_DeathRecipient_setOnUnlinked(death_recipient_.get(),
56 binderUnlinkedCallbackAidl);
57 }
58
startSession(const std::shared_ptr<IBluetoothAudioPort> & host_if,const AudioConfiguration & audio_config,const std::vector<LatencyMode> & latencyModes,DataMQDesc * _aidl_return)59 ndk::ScopedAStatus BluetoothAudioProvider::startSession(
60 const std::shared_ptr<IBluetoothAudioPort>& host_if,
61 const AudioConfiguration& audio_config,
62 const std::vector<LatencyMode>& latencyModes,
63 DataMQDesc* _aidl_return) {
64 if (host_if == nullptr) {
65 *_aidl_return = DataMQDesc();
66 LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_)
67 << " Illegal argument";
68 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
69 }
70
71 latency_modes_ = latencyModes;
72 audio_config_ = std::make_unique<AudioConfiguration>(audio_config);
73 stack_iface_ = host_if;
74 BluetoothAudioProviderContext* cookie =
75 new BluetoothAudioProviderContext{session_type_};
76
77 AIBinder_linkToDeath(stack_iface_->asBinder().get(), death_recipient_.get(),
78 cookie);
79
80 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_);
81 return onSessionReady(_aidl_return);
82 }
83
endSession()84 ndk::ScopedAStatus BluetoothAudioProvider::endSession() {
85 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_);
86
87 if (stack_iface_ != nullptr) {
88 BluetoothAudioSessionReport::OnSessionEnded(session_type_);
89
90 AIBinder_unlinkToDeath(stack_iface_->asBinder().get(),
91 death_recipient_.get(), this);
92 } else {
93 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
94 << " has NO session";
95 }
96
97 stack_iface_ = nullptr;
98 audio_config_ = nullptr;
99
100 return ndk::ScopedAStatus::ok();
101 }
102
streamStarted(BluetoothAudioStatus status)103 ndk::ScopedAStatus BluetoothAudioProvider::streamStarted(
104 BluetoothAudioStatus status) {
105 if (stack_iface_ != nullptr) {
106 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
107 << ", status=" << toString(status);
108 BluetoothAudioSessionReport::ReportControlStatus(session_type_, true,
109 status);
110 } else {
111 LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_)
112 << ", status=" << toString(status) << " has NO session";
113 }
114
115 return ndk::ScopedAStatus::ok();
116 }
117
streamSuspended(BluetoothAudioStatus status)118 ndk::ScopedAStatus BluetoothAudioProvider::streamSuspended(
119 BluetoothAudioStatus status) {
120 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
121 << ", status=" << toString(status);
122
123 if (stack_iface_ != nullptr) {
124 BluetoothAudioSessionReport::ReportControlStatus(session_type_, false,
125 status);
126 } else {
127 LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_)
128 << ", status=" << toString(status) << " has NO session";
129 }
130 return ndk::ScopedAStatus::ok();
131 }
132
updateAudioConfiguration(const AudioConfiguration & audio_config)133 ndk::ScopedAStatus BluetoothAudioProvider::updateAudioConfiguration(
134 const AudioConfiguration& audio_config) {
135 if (stack_iface_ == nullptr || audio_config_ == nullptr) {
136 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
137 << " has NO session";
138 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
139 }
140
141 if (audio_config.getTag() != audio_config_->getTag()) {
142 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
143 << " audio config type is not match";
144 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
145 }
146
147 audio_config_ = std::make_unique<AudioConfiguration>(audio_config);
148 BluetoothAudioSessionReport::ReportAudioConfigChanged(session_type_,
149 *audio_config_);
150 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
151 << " | audio_config=" << audio_config.toString();
152 return ndk::ScopedAStatus::ok();
153 }
154
setLowLatencyModeAllowed(bool allowed)155 ndk::ScopedAStatus BluetoothAudioProvider::setLowLatencyModeAllowed(
156 bool allowed) {
157 if (stack_iface_ == nullptr) {
158 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
159 << " has NO session";
160 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
161 }
162 LOG(INFO) << __func__ << " - allowed " << allowed;
163 BluetoothAudioSessionReport::ReportLowLatencyModeAllowedChanged(
164 session_type_, allowed);
165 return ndk::ScopedAStatus::ok();
166 }
167
parseA2dpConfiguration(const CodecId & codec_id,const std::vector<uint8_t> & configuration,CodecParameters * codec_parameters,A2dpStatus * _aidl_return)168 ndk::ScopedAStatus BluetoothAudioProvider::parseA2dpConfiguration(
169 [[maybe_unused]] const CodecId& codec_id,
170 [[maybe_unused]] const std::vector<uint8_t>& configuration,
171 [[maybe_unused]] CodecParameters* codec_parameters,
172 [[maybe_unused]] A2dpStatus* _aidl_return) {
173 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
174 << " is illegal";
175 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
176 }
177
getA2dpConfiguration(const std::vector<A2dpRemoteCapabilities> & remote_a2dp_capabilities,const A2dpConfigurationHint & hint,std::optional<audio::A2dpConfiguration> * _aidl_return)178 ndk::ScopedAStatus BluetoothAudioProvider::getA2dpConfiguration(
179 [[maybe_unused]] const std::vector<A2dpRemoteCapabilities>&
180 remote_a2dp_capabilities,
181 [[maybe_unused]] const A2dpConfigurationHint& hint,
182 [[maybe_unused]] std::optional<audio::A2dpConfiguration>* _aidl_return) {
183 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
184 << " is illegal";
185
186 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
187 }
188
setCodecPriority(const::aidl::android::hardware::bluetooth::audio::CodecId & in_codecId,int32_t in_priority)189 ndk::ScopedAStatus BluetoothAudioProvider::setCodecPriority(
190 const ::aidl::android::hardware::bluetooth::audio::CodecId& in_codecId,
191 int32_t in_priority) {
192 /* TODO: Implement */
193 (void)in_codecId;
194 (void)in_priority;
195 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
196 };
197
getLeAudioAseConfiguration(const std::optional<std::vector<std::optional<::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::LeAudioDeviceCapabilities>>> & in_remoteSinkAudioCapabilities,const std::optional<std::vector<std::optional<::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::LeAudioDeviceCapabilities>>> & in_remoteSourceAudioCapabilities,const std::vector<::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::LeAudioConfigurationRequirement> & in_requirements,std::vector<::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::LeAudioAseConfigurationSetting> * _aidl_return)198 ndk::ScopedAStatus BluetoothAudioProvider::getLeAudioAseConfiguration(
199 const std::optional<std::vector<std::optional<
200 ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
201 LeAudioDeviceCapabilities>>>& in_remoteSinkAudioCapabilities,
202 const std::optional<std::vector<std::optional<
203 ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
204 LeAudioDeviceCapabilities>>>& in_remoteSourceAudioCapabilities,
205 const std::vector<
206 ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
207 LeAudioConfigurationRequirement>& in_requirements,
208 std::vector<::aidl::android::hardware::bluetooth::audio::
209 IBluetoothAudioProvider::LeAudioAseConfigurationSetting>*
210 _aidl_return) {
211 /* TODO: Implement */
212 (void)in_remoteSinkAudioCapabilities;
213 (void)in_remoteSourceAudioCapabilities;
214 (void)in_requirements;
215 (void)_aidl_return;
216 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
217 };
218
getLeAudioAseQosConfiguration(const::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::LeAudioAseQosConfigurationRequirement & in_qosRequirement,::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::LeAudioAseQosConfigurationPair * _aidl_return)219 ndk::ScopedAStatus BluetoothAudioProvider::getLeAudioAseQosConfiguration(
220 const ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
221 LeAudioAseQosConfigurationRequirement& in_qosRequirement,
222 ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
223 LeAudioAseQosConfigurationPair* _aidl_return) {
224 /* TODO: Implement */
225 (void)in_qosRequirement;
226 (void)_aidl_return;
227 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
228 };
229
getLeAudioAseDatapathConfiguration(const std::optional<::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::StreamConfig> & in_sinkConfig,const std::optional<::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::StreamConfig> & in_sourceConfig,::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::LeAudioDataPathConfigurationPair * _aidl_return)230 ndk::ScopedAStatus BluetoothAudioProvider::getLeAudioAseDatapathConfiguration(
231 const std::optional<::aidl::android::hardware::bluetooth::audio::
232 IBluetoothAudioProvider::StreamConfig>&
233 in_sinkConfig,
234 const std::optional<::aidl::android::hardware::bluetooth::audio::
235 IBluetoothAudioProvider::StreamConfig>&
236 in_sourceConfig,
237 ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
238 LeAudioDataPathConfigurationPair* _aidl_return) {
239 /* TODO: Implement */
240 (void)in_sinkConfig;
241 (void)in_sourceConfig;
242 (void)_aidl_return;
243 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
244 }
245
onSinkAseMetadataChanged(::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::AseState in_state,int32_t cigId,int32_t cisId,const std::optional<std::vector<std::optional<::aidl::android::hardware::bluetooth::audio::MetadataLtv>>> & in_metadata)246 ndk::ScopedAStatus BluetoothAudioProvider::onSinkAseMetadataChanged(
247 ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
248 AseState in_state,
249 int32_t cigId, int32_t cisId,
250 const std::optional<std::vector<std::optional<
251 ::aidl::android::hardware::bluetooth::audio::MetadataLtv>>>&
252 in_metadata) {
253 /* TODO: Implement */
254 (void)in_state;
255 (void)cigId;
256 (void)cisId;
257 (void)in_metadata;
258 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
259 };
260
onSourceAseMetadataChanged(::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::AseState in_state,int32_t cigId,int32_t cisId,const std::optional<std::vector<std::optional<::aidl::android::hardware::bluetooth::audio::MetadataLtv>>> & in_metadata)261 ndk::ScopedAStatus BluetoothAudioProvider::onSourceAseMetadataChanged(
262 ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
263 AseState in_state,
264 int32_t cigId, int32_t cisId,
265 const std::optional<std::vector<std::optional<
266 ::aidl::android::hardware::bluetooth::audio::MetadataLtv>>>&
267 in_metadata) {
268 /* TODO: Implement */
269 (void)in_state;
270 (void)cigId;
271 (void)cisId;
272 (void)in_metadata;
273 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
274 };
275
getLeAudioBroadcastConfiguration(const std::optional<std::vector<std::optional<::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::LeAudioDeviceCapabilities>>> & in_remoteSinkAudioCapabilities,const::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::LeAudioBroadcastConfigurationRequirement & in_requirement,::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::LeAudioBroadcastConfigurationSetting * _aidl_return)276 ndk::ScopedAStatus BluetoothAudioProvider::getLeAudioBroadcastConfiguration(
277 const std::optional<std::vector<std::optional<
278 ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
279 LeAudioDeviceCapabilities>>>& in_remoteSinkAudioCapabilities,
280 const ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
281 LeAudioBroadcastConfigurationRequirement& in_requirement,
282 ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
283 LeAudioBroadcastConfigurationSetting* _aidl_return) {
284 /* TODO: Implement */
285 (void)in_remoteSinkAudioCapabilities;
286 (void)in_requirement;
287 (void)_aidl_return;
288 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
289 };
290
291 ndk::ScopedAStatus
getLeAudioBroadcastDatapathConfiguration(const::aidl::android::hardware::bluetooth::audio::AudioContext & in_context,const std::vector<::aidl::android::hardware::bluetooth::audio::LeAudioBroadcastConfiguration::BroadcastStreamMap> & in_streamMap,::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::LeAudioDataPathConfiguration * _aidl_return)292 BluetoothAudioProvider::getLeAudioBroadcastDatapathConfiguration(
293 const ::aidl::android::hardware::bluetooth::audio::AudioContext& in_context,
294 const std::vector<::aidl::android::hardware::bluetooth::audio::
295 LeAudioBroadcastConfiguration::BroadcastStreamMap>&
296 in_streamMap,
297 ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
298 LeAudioDataPathConfiguration* _aidl_return) {
299 /* TODO: Implement */
300 (void)in_context;
301 (void)in_streamMap;
302 (void)_aidl_return;
303 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
304 }
305
306 } // namespace audio
307 } // namespace bluetooth
308 } // namespace hardware
309 } // namespace android
310 } // namespace aidl
311