1 /**
2 * Copyright 2023 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 "bluetooth-a2dp"
18
19 #include "a2dp_ext.h"
20
21 #include <bluetooth/log.h>
22
23 #include <cstddef>
24 #include <cstdint>
25 #include <cstring>
26 #include <vector>
27
28 #include "a2dp_codec_api.h"
29 #include "a2dp_constants.h"
30 #include "audio_hal_interface/a2dp_encoding.h"
31 #include "hardware/bt_av.h"
32
33 using namespace bluetooth;
34
codec_id(btav_a2dp_codec_index_t codec_index)35 static a2dp::CodecId codec_id(btav_a2dp_codec_index_t codec_index) {
36 bluetooth::a2dp::CodecId id;
37 auto result = ::bluetooth::audio::a2dp::provider::codec_info(codec_index, &id, nullptr, nullptr);
38 log::assert_that(result, "provider::codec_info unexpectdly failed");
39 return id;
40 }
41
A2dpCodecConfigExt(btav_a2dp_codec_index_t codec_index,bool is_source)42 A2dpCodecConfigExt::A2dpCodecConfigExt(btav_a2dp_codec_index_t codec_index, bool is_source)
43 : A2dpCodecConfig(codec_index, codec_id(codec_index),
44 bluetooth::audio::a2dp::provider::codec_index_str(codec_index).value(),
45 BTAV_A2DP_CODEC_PRIORITY_DEFAULT),
46 is_source_(is_source) {
47 // Load the local capabilities from the provider info.
48 auto result = ::bluetooth::audio::a2dp::provider::codec_info(
49 codec_index, nullptr, ota_codec_config_, &codec_capability_);
50 log::assert_that(result, "provider::codec_info unexpectdly failed");
51 codec_selectable_capability_ = codec_capability_;
52 }
53
setCodecConfig(const uint8_t * p_peer_codec_info,bool,uint8_t *)54 tA2DP_STATUS A2dpCodecConfigExt::setCodecConfig(const uint8_t* p_peer_codec_info,
55 bool /* is_capability */,
56 uint8_t* /* p_result_codec_config */) {
57 // Call get_a2dp_config to recompute best capabilities.
58 // This method need to update codec_capability_, codec_config_,
59 // and ota_codec_config_ using the local codec_user_config_, and input
60 // peer_codec_info.
61 using namespace bluetooth::audio::a2dp;
62 provider::a2dp_remote_capabilities capabilities = {
63 .seid = 0, // the SEID does not matter here.
64 .capabilities = p_peer_codec_info,
65 };
66
67 auto result = provider::get_a2dp_configuration(
68 RawAddress::kEmpty, std::vector<provider::a2dp_remote_capabilities>{capabilities},
69 codec_user_config_);
70 if (!result.has_value()) {
71 log::error("Failed to set a configuration for {}", name_);
72 return AVDTP_UNSUPPORTED_CONFIGURATION;
73 }
74
75 memcpy(ota_codec_config_, result->codec_config, sizeof(ota_codec_config_));
76 codec_config_ = result->codec_parameters;
77 codec_capability_ = result->codec_parameters;
78 vendor_specific_parameters_ = result->vendor_specific_parameters;
79 return A2DP_SUCCESS;
80 }
81
setPeerCodecCapabilities(const uint8_t *)82 bool A2dpCodecConfigExt::setPeerCodecCapabilities(const uint8_t* /* p_peer_codec_capabilities */) {
83 // setPeerCodecCapabilities updates the selectable
84 // capabilities in the codec config. It can be safely
85 // ignored as providing a superset of the selectable
86 // capabilities is safe.
87 return true;
88 }
89
90 tA2DP_ENCODER_INTERFACE const a2dp_encoder_interface_ext = {
91 .encoder_init = [](const tA2DP_ENCODER_INIT_PEER_PARAMS*, A2dpCodecConfig*,
__anonbfdcc26a0102() 92 a2dp_source_read_callback_t, a2dp_source_enqueue_callback_t) {},
__anonbfdcc26a0202() 93 .encoder_cleanup = []() {},
__anonbfdcc26a0302() 94 .feeding_reset = []() {},
__anonbfdcc26a0402() 95 .feeding_flush = []() {},
__anonbfdcc26a0502() 96 .get_encoder_interval_ms = []() { return (uint64_t)20; },
__anonbfdcc26a0602() 97 .get_effective_frame_size = []() { return 0; },
__anonbfdcc26a0702() 98 .send_frames = [](uint64_t) {},
__anonbfdcc26a0802() 99 .set_transmit_queue_length = [](size_t) {},
100 };
101
A2DP_GetEncoderInterfaceExt(const uint8_t *)102 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceExt(const uint8_t*) {
103 return &a2dp_encoder_interface_ext;
104 }
105