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