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 low complexity subband codec (SBC)
19 //
20 
21 #ifndef A2DP_SBC_H
22 #define A2DP_SBC_H
23 
24 #include <stddef.h>
25 #include <stdint.h>
26 
27 #include "a2dp_codec_api.h"
28 #include "a2dp_sbc_constants.h"
29 #include "avdt_api.h"
30 #include "internal_include/bt_target.h"
31 #include "stack/include/bt_hdr.h"
32 
33 class A2dpCodecConfigSbcBase : public A2dpCodecConfig {
34 protected:
A2dpCodecConfigSbcBase(btav_a2dp_codec_index_t codec_index,const std::string & name,btav_a2dp_codec_priority_t codec_priority,bool is_source)35   A2dpCodecConfigSbcBase(btav_a2dp_codec_index_t codec_index, const std::string& name,
36                          btav_a2dp_codec_priority_t codec_priority, bool is_source)
37       : A2dpCodecConfig(codec_index, bluetooth::a2dp::CodecId::SBC, name, codec_priority),
38         is_source_(is_source) {}
39   tA2DP_STATUS setCodecConfig(const uint8_t* p_peer_codec_info, bool is_capability,
40                               uint8_t* p_result_codec_config) override;
41   bool setPeerCodecCapabilities(const uint8_t* p_peer_codec_capabilities) override;
42 
43 private:
44   bool is_source_;  // True if local is Source
45 };
46 
47 class A2dpCodecConfigSbcSource : public A2dpCodecConfigSbcBase {
48 public:
49   A2dpCodecConfigSbcSource(btav_a2dp_codec_priority_t codec_priority);
50   virtual ~A2dpCodecConfigSbcSource();
51 
52   bool init() override;
53 
54 private:
55   bool useRtpHeaderMarkerBit() const override;
56   void debug_codec_dump(int fd) override;
57 };
58 
59 class A2dpCodecConfigSbcSink : public A2dpCodecConfigSbcBase {
60 public:
61   A2dpCodecConfigSbcSink(btav_a2dp_codec_priority_t codec_priority);
62   virtual ~A2dpCodecConfigSbcSink();
63 
64   bool init() override;
65 
66 private:
67   bool useRtpHeaderMarkerBit() const override;
68 };
69 
70 // Checks whether the codec capabilities contain a valid A2DP SBC Source codec.
71 // NOTE: only codecs that are implemented are considered valid.
72 // Returns true if |p_codec_info| contains information about a valid SBC codec,
73 // otherwise false.
74 bool A2DP_IsCodecValidSbc(const uint8_t* p_codec_info);
75 
76 // Checks whether A2DP SBC Sink codec is supported.
77 // |p_codec_info| contains information about the codec capabilities.
78 // Returns true if the A2DP SBC Sink codec is supported, otherwise false.
79 tA2DP_STATUS A2DP_IsSinkCodecSupportedSbc(const uint8_t* p_codec_info);
80 
81 // Initialize state with the default A2DP SBC codec.
82 // The initialized state with the codec capabilities is stored in
83 // |p_codec_info|.
84 void A2DP_InitDefaultCodecSbc(uint8_t* p_codec_info);
85 
86 // Gets the A2DP SBC codec name for a given |p_codec_info|.
87 const char* A2DP_CodecNameSbc(const uint8_t* p_codec_info);
88 
89 // Checks whether two A2DP SBC codecs |p_codec_info_a| and |p_codec_info_b|
90 // have the same type.
91 // Returns true if the two codecs have the same type, otherwise false.
92 bool A2DP_CodecTypeEqualsSbc(const uint8_t* p_codec_info_a, const uint8_t* p_codec_info_b);
93 
94 // Checks whether two A2DP SBC codecs |p_codec_info_a| and |p_codec_info_b|
95 // are exactly the same.
96 // Returns true if the two codecs are exactly the same, otherwise false.
97 // If the codec type is not SBC, the return value is false.
98 bool A2DP_CodecEqualsSbc(const uint8_t* p_codec_info_a, const uint8_t* p_codec_info_b);
99 
100 // Gets the track sample rate value for the A2DP SBC codec.
101 // |p_codec_info| is a pointer to the SBC codec_info to decode.
102 // Returns the track sample rate on success, or -1 if |p_codec_info|
103 // contains invalid codec information.
104 int A2DP_GetTrackSampleRateSbc(const uint8_t* p_codec_info);
105 
106 // Gets the track bits per sample value for the A2DP SBC codec.
107 // |p_codec_info| is a pointer to the SBC codec_info to decode.
108 // Returns the track bits per sample on success, or -1 if |p_codec_info|
109 // contains invalid codec information.
110 int A2DP_GetTrackBitsPerSampleSbc(const uint8_t* p_codec_info);
111 
112 // Gets the channel count for the A2DP SBC codec.
113 // |p_codec_info| is a pointer to the SBC codec_info to decode.
114 // Returns the channel count on success, or -1 if |p_codec_info|
115 // contains invalid codec information.
116 int A2DP_GetTrackChannelCountSbc(const uint8_t* p_codec_info);
117 
118 // Gets the number of subbands for the A2DP SBC codec.
119 // |p_codec_info| is a pointer to the SBC codec_info to decode.
120 // Returns the number of subbands on success, or -1 if |p_codec_info|
121 // contains invalid codec information.
122 int A2DP_GetNumberOfSubbandsSbc(const uint8_t* p_codec_info);
123 
124 // Gets the number of blocks for the A2DP SBC codec.
125 // |p_codec_info| is a pointer to the SBC codec_info to decode.
126 // Returns the number of blocks on success, or -1 if |p_codec_info|
127 // contains invalid codec information.
128 int A2DP_GetNumberOfBlocksSbc(const uint8_t* p_codec_info);
129 
130 // Gets the allocation method code for the A2DP SBC codec.
131 // The actual value is codec-specific - see |A2DP_SBC_IE_ALLOC_MD_*|.
132 // |p_codec_info| is a pointer to the SBC codec_info to decode.
133 // Returns the allocation method code on success, or -1 if |p_codec_info|
134 // contains invalid codec information.
135 int A2DP_GetAllocationMethodCodeSbc(const uint8_t* p_codec_info);
136 
137 // Gets the channel mode code for the A2DP SBC codec.
138 // The actual value is codec-specific - see |A2DP_SBC_IE_CH_MD_*|.
139 // |p_codec_info| is a pointer to the SBC codec_info to decode.
140 // Returns the channel mode code on success, or -1 if |p_codec_info|
141 // contains invalid codec information.
142 int A2DP_GetChannelModeCodeSbc(const uint8_t* p_codec_info);
143 
144 // Gets the sampling frequency code for the A2DP SBC codec.
145 // The actual value is codec-specific - see |A2DP_SBC_IE_SAMP_FREQ_*|.
146 // |p_codec_info| is a pointer to the SBC codec_info to decode.
147 // Returns the sampling frequency code on success, or -1 if |p_codec_info|
148 // contains invalid codec information.
149 int A2DP_GetSamplingFrequencyCodeSbc(const uint8_t* p_codec_info);
150 
151 // Gets the minimum bitpool for the A2DP SBC codec.
152 // The actual value is codec-specific - see |A2DP_SBC_IE_MIN_BITPOOL|.
153 // |p_codec_info| is a pointer to the SBC codec_info to decode.
154 // Returns the minimum bitpool on success, or -1 if |p_codec_info|
155 // contains invalid codec information.
156 int A2DP_GetMinBitpoolSbc(const uint8_t* p_codec_info);
157 
158 // Gets the maximum bitpool for the A2DP SBC codec.
159 // The actual value is codec-specific - see |A2DP_SBC_IE_MAX_BITPOOL|.
160 // |p_codec_info| is a pointer to the SBC codec_info to decode.
161 // Returns the maximum bitpool on success, or -1 if |p_codec_info|
162 // contains invalid codec information.
163 int A2DP_GetMaxBitpoolSbc(const uint8_t* p_codec_info);
164 
165 // Gets the channel type for the A2DP SBC Sink codec:
166 // 1 for mono, or 3 for dual/stereo/joint.
167 // |p_codec_info| is a pointer to the SBC codec_info to decode.
168 // Returns the channel type on success, or -1 if |p_codec_info|
169 // contains invalid codec information.
170 int A2DP_GetSinkTrackChannelTypeSbc(const uint8_t* p_codec_info);
171 
172 // Gets the A2DP SBC audio data timestamp from an audio packet.
173 // |p_codec_info| contains the codec information.
174 // |p_data| contains the audio data.
175 // The timestamp is stored in |p_timestamp|.
176 // Returns true on success, otherwise false.
177 bool A2DP_GetPacketTimestampSbc(const uint8_t* p_codec_info, const uint8_t* p_data,
178                                 uint32_t* p_timestamp);
179 
180 // Builds A2DP SBC codec header for audio data.
181 // |p_codec_info| contains the codec information.
182 // |p_buf| contains the audio data.
183 // |frames_per_packet| is the number of frames in this packet.
184 // Returns true on success, otherwise false.
185 bool A2DP_BuildCodecHeaderSbc(const uint8_t* p_codec_info, BT_HDR* p_buf,
186                               uint16_t frames_per_packet);
187 
188 // Decodes A2DP SBC codec info into a human readable string.
189 // |p_codec_info| is a pointer to the SBC codec_info to decode.
190 // Returns a string describing the codec information.
191 std::string A2DP_CodecInfoStringSbc(const uint8_t* p_codec_info);
192 
193 // Gets the A2DP SBC encoder interface that can be used to encode and prepare
194 // A2DP packets for transmission - see |tA2DP_ENCODER_INTERFACE|.
195 // |p_codec_info| contains the codec information.
196 // Returns the A2DP SBC encoder interface if the |p_codec_info| is valid and
197 // supported, otherwise NULL.
198 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceSbc(const uint8_t* p_codec_info);
199 
200 // Gets the A2DP SBC decoder interface that can be used to decode received A2DP
201 // packets - see |tA2DP_DECODER_INTERFACE|.
202 // |p_codec_info| contains the codec information.
203 // Returns the A2DP SBC decoder interface if the |p_codec_info| is valid and
204 // supported, otherwise NULL.
205 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterfaceSbc(const uint8_t* p_codec_info);
206 
207 // Adjusts the A2DP SBC codec, based on local support and Bluetooth
208 // specification.
209 // |p_codec_info| contains the codec information to adjust.
210 // Returns true if |p_codec_info| is valid and supported, otherwise false.
211 bool A2DP_AdjustCodecSbc(uint8_t* p_codec_info);
212 
213 // Gets the A2DP SBC Source codec index for a given |p_codec_info|.
214 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
215 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
216 btav_a2dp_codec_index_t A2DP_SourceCodecIndexSbc(const uint8_t* p_codec_info);
217 
218 // Gets the A2DP SBC Sink codec index for a given |p_codec_info|.
219 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
220 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
221 btav_a2dp_codec_index_t A2DP_SinkCodecIndexSbc(const uint8_t* p_codec_info);
222 
223 // Gets the A2DP SBC Source codec name.
224 const char* A2DP_CodecIndexStrSbc(void);
225 
226 // Gets the A2DP SBC Sink codec name.
227 const char* A2DP_CodecIndexStrSbcSink(void);
228 
229 // Initializes A2DP SBC Source codec information into |AvdtpSepConfig|
230 // configuration entry pointed by |p_cfg|.
231 bool A2DP_InitCodecConfigSbc(AvdtpSepConfig* p_cfg);
232 
233 // Initializes A2DP SBC Sink codec information into |AvdtpSepConfig|
234 // configuration entry pointed by |p_cfg|.
235 bool A2DP_InitCodecConfigSbcSink(AvdtpSepConfig* p_cfg);
236 
237 // Get SBC bitrate
238 // Returns |uint32_t| bitrate value in bits per second
239 uint32_t A2DP_GetBitrateSbc();
240 
241 #endif  // A2DP_SBC_H
242