• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_local_capability_);
50   log::assert_that(result, "provider::codec_info unexpectdly failed");
51 }
52 
setCodecConfig(const uint8_t * p_peer_codec_info,bool,uint8_t * p_result_codec_config)53 tA2DP_STATUS A2dpCodecConfigExt::setCodecConfig(const uint8_t* p_peer_codec_info,
54                                                 bool /* is_capability */,
55                                                 uint8_t* p_result_codec_config) {
56   if (p_peer_codec_info == nullptr || p_result_codec_config == nullptr) {
57     return A2DP_FAIL;
58   }
59 
60   // Call get_a2dp_config to recompute best capabilities.
61   // This method need to update codec_config_, and ota_codec_config_
62   // using the local codec_user_config_, and input peer_codec_info.
63   using namespace bluetooth::audio::a2dp;
64   provider::a2dp_remote_capabilities capabilities = {
65           .seid = 0,  // the SEID does not matter here.
66           .capabilities = p_peer_codec_info,
67   };
68 
69   auto result = provider::get_a2dp_configuration(
70           RawAddress::kEmpty, std::vector<provider::a2dp_remote_capabilities>{capabilities},
71           codec_user_config_);
72   if (!result.has_value()) {
73     log::error("Failed to set a configuration for {}", name_);
74     return AVDTP_UNSUPPORTED_CONFIGURATION;
75   }
76 
77   // Use the local capabilities for the selectable capabilities:
78   // the provider AIDL HAL does not provide an interface to parse the
79   // peer capabilities and the selectable capabilities cannot be
80   // computed.
81   codec_selectable_capability_ = codec_local_capability_;
82   codec_config_ = result->codec_parameters;
83   vendor_specific_parameters_ = result->vendor_specific_parameters;
84   memcpy(ota_codec_config_, result->codec_config, sizeof(ota_codec_config_));
85   memcpy(p_result_codec_config, result->codec_config, sizeof(ota_codec_config_));
86   return A2DP_SUCCESS;
87 }
88 
setPeerCodecCapabilities(const uint8_t * p_peer_codec_capabilities)89 bool A2dpCodecConfigExt::setPeerCodecCapabilities(const uint8_t* p_peer_codec_capabilities) {
90   // Use the local capabilities for the selectable capabilities:
91   // the provider AIDL HAL does not provide an interface to parse the
92   // peer capabilities and the selectable capabilities cannot be
93   // computed.
94   codec_selectable_capability_ = codec_local_capability_;
95   memcpy(ota_codec_peer_capability_, p_peer_codec_capabilities, sizeof(ota_codec_peer_capability_));
96   return true;
97 }
98 
setCodecConfig(btav_a2dp_codec_config_t codec_parameters,uint8_t const codec_config[AVDT_CODEC_SIZE],std::vector<uint8_t> const & vendor_specific_parameters)99 void A2dpCodecConfigExt::setCodecConfig(btav_a2dp_codec_config_t codec_parameters,
100                                         uint8_t const codec_config[AVDT_CODEC_SIZE],
101                                         std::vector<uint8_t> const& vendor_specific_parameters) {
102   // Use the local capabilities for the selectable capabilities:
103   // the provider AIDL HAL does not provide an interface to parse the
104   // peer capabilities and the selectable capabilities cannot be
105   // computed.
106   codec_selectable_capability_ = codec_local_capability_;
107   codec_config_ = codec_parameters;
108   memcpy(ota_codec_config_, codec_config, sizeof(ota_codec_config_));
109   vendor_specific_parameters_ = vendor_specific_parameters;
110 }
111 
112 tA2DP_ENCODER_INTERFACE const a2dp_encoder_interface_ext = {
113         .encoder_init = [](const tA2DP_ENCODER_INIT_PEER_PARAMS*, A2dpCodecConfig*,
__anon9ac95cea0102() 114                            a2dp_source_read_callback_t, a2dp_source_enqueue_callback_t) {},
__anon9ac95cea0202() 115         .encoder_cleanup = []() {},
__anon9ac95cea0302() 116         .feeding_reset = []() {},
__anon9ac95cea0402() 117         .feeding_flush = []() {},
__anon9ac95cea0502() 118         .get_encoder_interval_ms = []() { return (uint64_t)20; },
__anon9ac95cea0602() 119         .get_effective_frame_size = []() { return 0; },
__anon9ac95cea0702() 120         .send_frames = [](uint64_t) {},
__anon9ac95cea0802() 121         .set_transmit_queue_length = [](size_t) {},
122 };
123 
A2DP_GetEncoderInterfaceExt(const uint8_t *)124 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceExt(const uint8_t*) {
125   return &a2dp_encoder_interface_ext;
126 }
127