blob: c7761c5e76f0c4138feaa415d4bcdc8f79200517 [file] [log] [blame]
Josh Wu6ab53e72021-12-29 23:53:33 -08001/*
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 "BTAudioProviderA2dpHW"
18
19#include "A2dpOffloadAudioProvider.h"
20
21#include <BluetoothAudioCodecs.h>
22#include <BluetoothAudioSessionReport.h>
23#include <android-base/logging.h>
24
Antoine SOULIER4e34d052023-09-29 19:10:07 +000025#include "A2dpOffloadCodecAac.h"
Antoine SOULIER4e34d052023-09-29 19:10:07 +000026#include "A2dpOffloadCodecSbc.h"
27
Josh Wu6ab53e72021-12-29 23:53:33 -080028namespace aidl {
29namespace android {
30namespace hardware {
31namespace bluetooth {
32namespace audio {
33
Antoine SOULIERbabe71d2024-01-12 23:20:58 +000034A2dpOffloadEncodingAudioProvider::A2dpOffloadEncodingAudioProvider(
35 const A2dpOffloadCodecFactory& codec_factory)
36 : A2dpOffloadAudioProvider(codec_factory) {
Josh Wu6ab53e72021-12-29 23:53:33 -080037 session_type_ = SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH;
38}
39
Antoine SOULIERbabe71d2024-01-12 23:20:58 +000040A2dpOffloadDecodingAudioProvider::A2dpOffloadDecodingAudioProvider(
41 const A2dpOffloadCodecFactory& codec_factory)
42 : A2dpOffloadAudioProvider(codec_factory) {
Alice Kuoadcceec2022-03-28 13:28:43 +080043 session_type_ = SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH;
44}
45
Antoine SOULIERbabe71d2024-01-12 23:20:58 +000046A2dpOffloadAudioProvider::A2dpOffloadAudioProvider(
47 const A2dpOffloadCodecFactory& codec_factory)
48 : codec_factory_(codec_factory) {}
Alice Kuoadcceec2022-03-28 13:28:43 +080049
Josh Wu6ab53e72021-12-29 23:53:33 -080050bool A2dpOffloadAudioProvider::isValid(const SessionType& session_type) {
51 return (session_type == session_type_);
52}
53
54ndk::ScopedAStatus A2dpOffloadAudioProvider::startSession(
55 const std::shared_ptr<IBluetoothAudioPort>& host_if,
Chen Chenc92270e2022-02-14 18:29:52 -080056 const AudioConfiguration& audio_config,
Cheney Ni6ecbc762022-03-03 00:12:48 +080057 const std::vector<LatencyMode>& latency_modes, DataMQDesc* _aidl_return) {
Antoine SOULIER4e34d052023-09-29 19:10:07 +000058 if (audio_config.getTag() == AudioConfiguration::Tag::a2dp) {
59 auto a2dp_config = audio_config.get<AudioConfiguration::Tag::a2dp>();
60 A2dpStatus a2dp_status = A2dpStatus::NOT_SUPPORTED_CODEC_TYPE;
61
Antoine SOULIERbabe71d2024-01-12 23:20:58 +000062 auto codec = codec_factory_.GetCodec(a2dp_config.codecId);
63 if (!codec) {
64 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
65 << " - CodecId=" << a2dp_config.codecId.toString()
66 << " is not found";
67 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
68 }
Antoine SOULIER4e34d052023-09-29 19:10:07 +000069
Antoine SOULIERbabe71d2024-01-12 23:20:58 +000070 if (codec->info.id == CodecId(CodecId::A2dp::SBC)) {
71 SbcParameters sbc_parameters;
72
73 auto codec_sbc =
74 std::static_pointer_cast<const A2dpOffloadCodecSbc>(codec);
75 a2dp_status = codec_sbc->ParseConfiguration(a2dp_config.configuration,
76 &sbc_parameters);
77
78 } else if (codec->info.id == CodecId(CodecId::A2dp::AAC)) {
Antoine SOULIER4e34d052023-09-29 19:10:07 +000079 AacParameters aac_parameters;
Antoine SOULIERbabe71d2024-01-12 23:20:58 +000080
81 auto codec_aac =
82 std::static_pointer_cast<const A2dpOffloadCodecAac>(codec);
83 a2dp_status = codec_aac->ParseConfiguration(a2dp_config.configuration,
84 &aac_parameters);
Antoine SOULIER4e34d052023-09-29 19:10:07 +000085 }
86 if (a2dp_status != A2dpStatus::OK) {
87 LOG(WARNING) << __func__ << " - Invalid Audio Configuration="
88 << audio_config.toString();
89 *_aidl_return = DataMQDesc();
90 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
91 }
92 } else if (audio_config.getTag() == AudioConfiguration::Tag::a2dpConfig) {
93 if (!BluetoothAudioCodecs::IsOffloadCodecConfigurationValid(
94 session_type_,
95 audio_config.get<AudioConfiguration::a2dpConfig>())) {
96 LOG(WARNING) << __func__ << " - Invalid Audio Configuration="
97 << audio_config.toString();
98 *_aidl_return = DataMQDesc();
99 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
100 }
101 } else {
Josh Wu6ab53e72021-12-29 23:53:33 -0800102 LOG(WARNING) << __func__ << " - Invalid Audio Configuration="
103 << audio_config.toString();
104 *_aidl_return = DataMQDesc();
105 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
106 }
Antoine SOULIER4e34d052023-09-29 19:10:07 +0000107
Chen Chenc92270e2022-02-14 18:29:52 -0800108 return BluetoothAudioProvider::startSession(
109 host_if, audio_config, latency_modes, _aidl_return);
Josh Wu6ab53e72021-12-29 23:53:33 -0800110}
111
112ndk::ScopedAStatus A2dpOffloadAudioProvider::onSessionReady(
113 DataMQDesc* _aidl_return) {
114 *_aidl_return = DataMQDesc();
Cheney Ni6ecbc762022-03-03 00:12:48 +0800115 BluetoothAudioSessionReport::OnSessionStarted(
116 session_type_, stack_iface_, nullptr, *audio_config_, latency_modes_);
Josh Wu6ab53e72021-12-29 23:53:33 -0800117 return ndk::ScopedAStatus::ok();
118}
119
Antoine SOULIER4e34d052023-09-29 19:10:07 +0000120ndk::ScopedAStatus A2dpOffloadAudioProvider::parseA2dpConfiguration(
121 const CodecId& codec_id, const std::vector<uint8_t>& configuration,
122 CodecParameters* codec_parameters, A2dpStatus* _aidl_return) {
Antoine SOULIERbabe71d2024-01-12 23:20:58 +0000123 auto codec = codec_factory_.GetCodec(codec_id);
Antoine SOULIER4e34d052023-09-29 19:10:07 +0000124 if (!codec) {
125 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
126 << " - CodecId=" << codec_id.toString() << " is not found";
127 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
128 }
129
130 *_aidl_return = codec->ParseConfiguration(configuration, codec_parameters);
131
132 return ndk::ScopedAStatus::ok();
133}
134
135ndk::ScopedAStatus A2dpOffloadAudioProvider::getA2dpConfiguration(
136 const std::vector<A2dpRemoteCapabilities>& remote_a2dp_capabilities,
137 const A2dpConfigurationHint& hint,
138 std::optional<audio::A2dpConfiguration>* _aidl_return) {
139 *_aidl_return = std::nullopt;
140 A2dpConfiguration avdtp_configuration;
141
Antoine SOULIERbabe71d2024-01-12 23:20:58 +0000142 if (codec_factory_.GetConfiguration(remote_a2dp_capabilities, hint,
143 &avdtp_configuration))
Antoine SOULIER4e34d052023-09-29 19:10:07 +0000144 *_aidl_return =
145 std::make_optional<A2dpConfiguration>(std::move(avdtp_configuration));
146
147 return ndk::ScopedAStatus::ok();
148}
149
Josh Wu6ab53e72021-12-29 23:53:33 -0800150} // namespace audio
151} // namespace bluetooth
152} // namespace hardware
153} // namespace android
Cheney Ni6ecbc762022-03-03 00:12:48 +0800154} // namespace aidl