1 /*
2  * Copyright 2016 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 //
18 // A2DP Codec API for LDAC
19 //
20 
21 #ifndef A2DP_VENDOR_LDAC_H
22 #define A2DP_VENDOR_LDAC_H
23 
24 #include <stddef.h>
25 #include <stdint.h>
26 
27 #include "a2dp_codec_api.h"
28 #include "a2dp_vendor_ldac_constants.h"
29 #include "avdt_api.h"
30 #include "internal_include/bt_target.h"
31 #include "stack/include/bt_hdr.h"
32 
33 class A2dpCodecConfigLdacBase : public A2dpCodecConfig {
34  protected:
A2dpCodecConfigLdacBase(btav_a2dp_codec_index_t codec_index,const std::string & name,btav_a2dp_codec_priority_t codec_priority,bool is_source)35   A2dpCodecConfigLdacBase(btav_a2dp_codec_index_t codec_index,
36                           const std::string& name,
37                           btav_a2dp_codec_priority_t codec_priority,
38                           bool is_source)
39       : A2dpCodecConfig(codec_index, A2DP_CODEC_ID_LDAC, name, codec_priority),
40         is_source_(is_source) {}
41   bool setCodecConfig(const uint8_t* p_peer_codec_info, bool is_capability,
42                       uint8_t* p_result_codec_config) override;
43   bool setPeerCodecCapabilities(
44       const uint8_t* p_peer_codec_capabilities) override;
45 
46  private:
47   [[maybe_unused]] bool is_source_;  // True if local is Source
48 };
49 
50 class A2dpCodecConfigLdacSource : public A2dpCodecConfigLdacBase {
51  public:
52   A2dpCodecConfigLdacSource(btav_a2dp_codec_priority_t codec_priority);
53   virtual ~A2dpCodecConfigLdacSource();
54 
55   bool init() override;
56 
57  private:
58   bool useRtpHeaderMarkerBit() const override;
59   void debug_codec_dump(int fd) override;
60 };
61 
62 class A2dpCodecConfigLdacSink : public A2dpCodecConfigLdacBase {
63  public:
64   A2dpCodecConfigLdacSink(btav_a2dp_codec_priority_t codec_priority);
65   virtual ~A2dpCodecConfigLdacSink();
66 
67   bool init() override;
68 
69  private:
70   bool useRtpHeaderMarkerBit() const override;
71 };
72 
73 // Checks whether the codec capabilities contain a valid A2DP LDAC Source
74 // codec.
75 // NOTE: only codecs that are implemented are considered valid.
76 // Returns true if |p_codec_info| contains information about a valid LDAC
77 // codec, otherwise false.
78 bool A2DP_IsVendorSourceCodecValidLdac(const uint8_t* p_codec_info);
79 
80 // Checks whether the codec capabilities contain a valid A2DP LDAC Sink
81 // codec.
82 // NOTE: only codecs that are implemented are considered valid.
83 // Returns true if |p_codec_info| contains information about a valid LDAC
84 // codec, otherwise false.
85 bool A2DP_IsVendorSinkCodecValidLdac(const uint8_t* p_codec_info);
86 
87 // Checks whether the codec capabilities contain a valid peer A2DP LDAC Sink
88 // codec.
89 // NOTE: only codecs that are implemented are considered valid.
90 // Returns true if |p_codec_info| contains information about a valid LDAC
91 // codec, otherwise false.
92 bool A2DP_IsVendorPeerSinkCodecValidLdac(const uint8_t* p_codec_info);
93 
94 // Checks whether the codec capabilities contain a valid peer A2DP LDAC Source
95 // codec.
96 // NOTE: only codecs that are implemented are considered valid.
97 // Returns true if |p_codec_info| contains information about a valid LDAC
98 // codec, otherwise false.
99 bool A2DP_IsVendorPeerSourceCodecValidLdac(const uint8_t* p_codec_info);
100 
101 // Checks whether A2DP LDAC Sink codec is supported.
102 // |p_codec_info| contains information about the codec capabilities.
103 // Returns true if the A2DP LDAC Sink codec is supported, otherwise false.
104 bool A2DP_IsVendorSinkCodecSupportedLdac(const uint8_t* p_codec_info);
105 
106 // Checks whether an A2DP LDAC Source codec for a peer Source device is
107 // supported.
108 // |p_codec_info| contains information about the codec capabilities of the
109 // peer device.
110 // Returns true if the A2DP LDAC Source codec for a peer Source device is
111 // supported, otherwise false.
112 bool A2DP_IsPeerSourceCodecSupportedLdac(const uint8_t* p_codec_info);
113 
114 // Checks whether the A2DP data packets should contain RTP header.
115 // |content_protection_enabled| is true if Content Protection is
116 // enabled. |p_codec_info| contains information about the codec capabilities.
117 // Returns true if the A2DP data packets should contain RTP header, otherwise
118 // false.
119 bool A2DP_VendorUsesRtpHeaderLdac(bool content_protection_enabled,
120                                   const uint8_t* p_codec_info);
121 
122 // Gets the A2DP LDAC codec name for a given |p_codec_info|.
123 const char* A2DP_VendorCodecNameLdac(const uint8_t* p_codec_info);
124 
125 // Checks whether two A2DP LDAC codecs |p_codec_info_a| and |p_codec_info_b|
126 // have the same type.
127 // Returns true if the two codecs have the same type, otherwise false.
128 bool A2DP_VendorCodecTypeEqualsLdac(const uint8_t* p_codec_info_a,
129                                     const uint8_t* p_codec_info_b);
130 
131 // Checks whether two A2DP LDAC codecs |p_codec_info_a| and |p_codec_info_b|
132 // are exactly the same.
133 // Returns true if the two codecs are exactly the same, otherwise false.
134 // If the codec type is not LDAC, the return value is false.
135 bool A2DP_VendorCodecEqualsLdac(const uint8_t* p_codec_info_a,
136                                 const uint8_t* p_codec_info_b);
137 
138 // Gets the track sample rate value for the A2DP LDAC codec.
139 // |p_codec_info| is a pointer to the LDAC codec_info to decode.
140 // Returns the track sample rate on success, or -1 if |p_codec_info|
141 // contains invalid codec information.
142 int A2DP_VendorGetTrackSampleRateLdac(const uint8_t* p_codec_info);
143 
144 // Gets the track bits per sample value for the A2DP LDAC codec.
145 // |p_codec_info| is a pointer to the LDAC codec_info to decode.
146 // Returns the track bits per sample on success, or -1 if |p_codec_info|
147 // contains invalid codec information.
148 int A2DP_VendorGetTrackBitsPerSampleLdac(const uint8_t* p_codec_info);
149 
150 // Gets the track bitrate value for the A2DP LDAC codec.
151 // |p_codec_info| is a pointer to the LDAC codec_info to decode.
152 // Returns the track sample rate on success, or -1 if |p_codec_info|
153 // contains invalid codec information.
154 int A2DP_VendorGetBitRateLdac(const uint8_t* p_codec_info);
155 
156 // Gets the channel count for the A2DP LDAC codec.
157 // |p_codec_info| is a pointer to the LDAC codec_info to decode.
158 // Returns the channel count on success, or -1 if |p_codec_info|
159 // contains invalid codec information.
160 int A2DP_VendorGetTrackChannelCountLdac(const uint8_t* p_codec_info);
161 
162 // Gets the channel type for the A2DP LDAC codec.
163 // 1 for mono, or 3 for dual channel/stereo.
164 // |p_codec_info| is a pointer to the LDAC codec_info to decode.
165 // Returns the channel count on success, or -1 if |p_codec_info|
166 // contains invalid codec information.
167 int A2DP_VendorGetSinkTrackChannelTypeLdac(const uint8_t* p_codec_info);
168 
169 // Gets the channel mode code for the A2DP LDAC codec.
170 // The actual value is codec-specific - see |A2DP_LDAC_CHANNEL_MODE_*|.
171 // |p_codec_info| is a pointer to the LDAC codec_info to decode.
172 // Returns the channel mode code on success, or -1 if |p_codec_info|
173 // contains invalid codec information.
174 int A2DP_VendorGetChannelModeCodeLdac(const uint8_t* p_codec_info);
175 
176 // Gets the A2DP LDAC audio data timestamp from an audio packet.
177 // |p_codec_info| contains the codec information.
178 // |p_data| contains the audio data.
179 // The timestamp is stored in |p_timestamp|.
180 // Returns true on success, otherwise false.
181 bool A2DP_VendorGetPacketTimestampLdac(const uint8_t* p_codec_info,
182                                        const uint8_t* p_data,
183                                        uint32_t* p_timestamp);
184 
185 // Builds A2DP LDAC codec header for audio data.
186 // |p_codec_info| contains the codec information.
187 // |p_buf| contains the audio data.
188 // |frames_per_packet| is the number of frames in this packet.
189 // Returns true on success, otherwise false.
190 bool A2DP_VendorBuildCodecHeaderLdac(const uint8_t* p_codec_info, BT_HDR* p_buf,
191                                      uint16_t frames_per_packet);
192 
193 // Decodes A2DP LDAC codec info into a human readable string.
194 // |p_codec_info| is a pointer to the LDAC codec_info to decode.
195 // Returns a string describing the codec information.
196 std::string A2DP_VendorCodecInfoStringLdac(const uint8_t* p_codec_info);
197 
198 // Gets the A2DP LDAC encoder interface that can be used to encode and prepare
199 // A2DP packets for transmission - see |tA2DP_ENCODER_INTERFACE|.
200 // |p_codec_info| contains the codec information.
201 // Returns the A2DP LDAC encoder interface if the |p_codec_info| is valid and
202 // supported, otherwise NULL.
203 const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceLdac(
204     const uint8_t* p_codec_info);
205 
206 // Gets the current A2DP LDAC decoder interface that can be used to decode
207 // received A2DP packets - see |tA2DP_DECODER_INTERFACE|.
208 // |p_codec_info| contains the codec information.
209 // Returns the A2DP LDAC decoder interface if the |p_codec_info| is valid and
210 // supported, otherwise NULL.
211 const tA2DP_DECODER_INTERFACE* A2DP_VendorGetDecoderInterfaceLdac(
212     const uint8_t* p_codec_info);
213 
214 // Adjusts the A2DP LDAC codec, based on local support and Bluetooth
215 // specification.
216 // |p_codec_info| contains the codec information to adjust.
217 // Returns true if |p_codec_info| is valid and supported, otherwise false.
218 bool A2DP_VendorAdjustCodecLdac(uint8_t* p_codec_info);
219 
220 // Gets the A2DP LDAC Source codec index for a given |p_codec_info|.
221 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
222 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
223 btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexLdac(
224     const uint8_t* p_codec_info);
225 
226 // Gets the A2DP LDAC Sink codec index for a given |p_codec_info|.
227 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
228 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
229 btav_a2dp_codec_index_t A2DP_VendorSinkCodecIndexLdac(
230     const uint8_t* p_codec_info);
231 
232 // Gets the A2DP LDAC Source codec name.
233 const char* A2DP_VendorCodecIndexStrLdac(void);
234 
235 // Gets the A2DP LDAC Sink codec name.
236 const char* A2DP_VendorCodecIndexStrLdacSink(void);
237 
238 // Initializes A2DP LDAC Source codec information into |AvdtpSepConfig|
239 // configuration entry pointed by |p_cfg|.
240 bool A2DP_VendorInitCodecConfigLdac(AvdtpSepConfig* p_cfg);
241 
242 // Initializes A2DP LDAC Sink codec information into |AvdtpSepConfig|
243 // configuration entry pointed by |p_cfg|.
244 bool A2DP_VendorInitCodecConfigLdacSink(AvdtpSepConfig* p_cfg);
245 
246 #endif  // A2DP_VENDOR_LDAC_H
247