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 #pragma once
18 
19 #include <unordered_map>
20 #include <vector>
21 
22 #include "a2dp_constants.h"
23 #include "audio_aidl_interfaces.h"
24 #include "include/hardware/bt_av.h"
25 
26 namespace bluetooth::audio::aidl::a2dp {
27 
28 using ::aidl::android::hardware::bluetooth::audio::CodecId;
29 using ::aidl::android::hardware::bluetooth::audio::CodecInfo;
30 
31 /***
32  * Record the provider info returned by the HAL implementer.
33  ***/
34 class ProviderInfo {
35 public:
36   /***
37    * Reads the provider information from the HAL.
38    * May return nullptr if the HAL does not implement
39    * getProviderInfo, or if the feature flag for codec
40    * extensibility is disabled.
41    ***/
42   static std::unique_ptr<ProviderInfo> GetProviderInfo(bool supports_a2dp_hw_offload_v2);
43 
44   ProviderInfo(std::vector<CodecInfo> source_codecs, std::vector<CodecInfo> sink_codecs);
45   ~ProviderInfo() = default;
46 
47   /***
48    * Returns the codec with the selected index if supported
49    * by the provider.
50    ***/
51   std::optional<CodecInfo const*> GetCodec(btav_a2dp_codec_index_t codec_index) const;
52 
53   /***
54    * Find the source codec index by codec capabilities.
55    ***/
56   std::optional<btav_a2dp_codec_index_t> SourceCodecIndex(CodecId const& codec_id) const;
57   std::optional<btav_a2dp_codec_index_t> SourceCodecIndex(uint32_t vendor_id,
58                                                           uint16_t codec_id) const;
59   std::optional<btav_a2dp_codec_index_t> SourceCodecIndex(uint8_t const* codec_info) const;
60 
61   /***
62    * Find the sink codec index by codec capabilities.
63    ***/
64   std::optional<btav_a2dp_codec_index_t> SinkCodecIndex(CodecId const& codec_id) const;
65   std::optional<btav_a2dp_codec_index_t> SinkCodecIndex(uint32_t vendor_id,
66                                                         uint16_t codec_id) const;
67   std::optional<btav_a2dp_codec_index_t> SinkCodecIndex(uint8_t const* codec_info) const;
68 
69   /***
70    * Return the name of the codec with the assigned
71    * input index.
72    ***/
73   std::optional<const char*> CodecIndexStr(btav_a2dp_codec_index_t codec_index) const;
74 
75   /***
76    * Return true if the codec is supported by the
77    * provider.
78    ***/
79   bool SupportsCodec(btav_a2dp_codec_index_t codec_index) const;
80 
81   /***
82    * Helper to convert CodecId and byte[] configuration to
83    * the Media Codec Capabilities format.
84    * Returns true if the capabilities were successfully converted.
85    ***/
86   static bool BuildCodecCapabilities(CodecId const& codec_id,
87                                      std::vector<uint8_t> const& capabilities, uint8_t* codec_info);
88 
89   /***
90    * Return the A2DP capabilities for the selected codec.
91    * Returns true if the codec is supported, false otherwise.
92    ***/
93   bool CodecCapabilities(btav_a2dp_codec_index_t codec_index, bluetooth::a2dp::CodecId* codec_id,
94                          uint8_t* codec_info, btav_a2dp_codec_config_t* codec_config) const;
95 
96   const std::vector<CodecInfo> source_codecs;
97   const std::vector<CodecInfo> sink_codecs;
98 
99 private:
100   std::unordered_map<btav_a2dp_codec_index_t, CodecInfo const*> assigned_codec_indexes;
101 };
102 
103 }  // namespace bluetooth::audio::aidl::a2dp
104