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