1 /* 2 * Copyright (C) 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 "a2dp_codec_api.h" 25 #include "a2dp_sbc_constants.h" 26 #include "avdt_api.h" 27 28 class A2dpCodecConfigSbc : public A2dpCodecConfig { 29 public: 30 A2dpCodecConfigSbc(btav_a2dp_codec_priority_t codec_priority); 31 virtual ~A2dpCodecConfigSbc(); 32 33 bool init() override; 34 period_ms_t encoderIntervalMs() const override; 35 bool setCodecConfig(const uint8_t* p_peer_codec_info, bool is_capability, 36 uint8_t* p_result_codec_config) override; 37 38 private: 39 bool useRtpHeaderMarkerBit() const override; 40 bool updateEncoderUserConfig( 41 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 42 bool* p_restart_input, bool* p_restart_output, 43 bool* p_config_updated) override; 44 void debug_codec_dump(int fd) override; 45 }; 46 47 class A2dpCodecConfigSbcSink : public A2dpCodecConfig { 48 public: 49 A2dpCodecConfigSbcSink(btav_a2dp_codec_priority_t codec_priority); 50 virtual ~A2dpCodecConfigSbcSink(); 51 52 bool init() override; 53 period_ms_t encoderIntervalMs() const override; 54 bool setCodecConfig(const uint8_t* p_peer_codec_info, bool is_capability, 55 uint8_t* p_result_codec_config) override; 56 57 private: 58 bool useRtpHeaderMarkerBit() const override; 59 bool updateEncoderUserConfig( 60 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 61 bool* p_restart_input, bool* p_restart_output, 62 bool* p_config_updated) override; 63 }; 64 65 // Checks whether the codec capabilities contain a valid A2DP SBC Source codec. 66 // NOTE: only codecs that are implemented are considered valid. 67 // Returns true if |p_codec_info| contains information about a valid SBC codec, 68 // otherwise false. 69 bool A2DP_IsSourceCodecValidSbc(const uint8_t* p_codec_info); 70 71 // Checks whether the codec capabilities contain a valid A2DP SBC Sink codec. 72 // NOTE: only codecs that are implemented are considered valid. 73 // Returns true if |p_codec_info| contains information about a valid SBC codec, 74 // otherwise false. 75 bool A2DP_IsSinkCodecValidSbc(const uint8_t* p_codec_info); 76 77 // Checks whether the codec capabilities contain a valid peer A2DP SBC Source 78 // codec. 79 // NOTE: only codecs that are implemented are considered valid. 80 // Returns true if |p_codec_info| contains information about a valid SBC codec, 81 // otherwise false. 82 bool A2DP_IsPeerSourceCodecValidSbc(const uint8_t* p_codec_info); 83 84 // Checks whether the codec capabilities contain a valid peer A2DP SBC Sink 85 // codec. 86 // NOTE: only codecs that are implemented are considered valid. 87 // Returns true if |p_codec_info| contains information about a valid SBC codec, 88 // otherwise false. 89 bool A2DP_IsPeerSinkCodecValidSbc(const uint8_t* p_codec_info); 90 91 // Checks whether A2DP SBC Sink codec is supported. 92 // |p_codec_info| contains information about the codec capabilities. 93 // Returns true if the A2DP SBC Sink codec is supported, otherwise false. 94 bool A2DP_IsSinkCodecSupportedSbc(const uint8_t* p_codec_info); 95 96 // Checks whether an A2DP SBC Source codec for a peer Source device is 97 // supported. 98 // |p_codec_info| contains information about the codec capabilities of the 99 // peer device. 100 // Returns true if the A2DP SBC Source codec for a peer Source device is 101 // supported, otherwise false. 102 bool A2DP_IsPeerSourceCodecSupportedSbc(const uint8_t* p_codec_info); 103 104 // Initialize state with the default A2DP SBC codec. 105 // The initialized state with the codec capabilities is stored in 106 // |p_codec_info|. 107 void A2DP_InitDefaultCodecSbc(uint8_t* p_codec_info); 108 109 // Builds A2DP preferred SBC Sink capability from SBC Source capability. 110 // |p_src_cap| is the Source capability to use. 111 // |p_pref_cfg| is the result Sink capability to store. 112 // Returns |A2DP_SUCCESS| on success, otherwise the corresponding A2DP error 113 // status code. 114 tA2DP_STATUS A2DP_BuildSrc2SinkConfigSbc(const uint8_t* p_src_cap, 115 uint8_t* p_pref_cfg); 116 117 // Gets the A2DP SBC codec name for a given |p_codec_info|. 118 const char* A2DP_CodecNameSbc(const uint8_t* p_codec_info); 119 120 // Checks whether two A2DP SBC codecs |p_codec_info_a| and |p_codec_info_b| 121 // have the same type. 122 // Returns true if the two codecs have the same type, otherwise false. 123 bool A2DP_CodecTypeEqualsSbc(const uint8_t* p_codec_info_a, 124 const uint8_t* p_codec_info_b); 125 126 // Checks whether two A2DP SBC codecs |p_codec_info_a| and |p_codec_info_b| 127 // are exactly the same. 128 // Returns true if the two codecs are exactly the same, otherwise false. 129 // If the codec type is not SBC, the return value is false. 130 bool A2DP_CodecEqualsSbc(const uint8_t* p_codec_info_a, 131 const uint8_t* p_codec_info_b); 132 133 // Gets the track sample rate value for the A2DP SBC codec. 134 // |p_codec_info| is a pointer to the SBC codec_info to decode. 135 // Returns the track sample rate on success, or -1 if |p_codec_info| 136 // contains invalid codec information. 137 int A2DP_GetTrackSampleRateSbc(const uint8_t* p_codec_info); 138 139 // Gets the bits per audio sample for the A2DP SBC codec. 140 // |p_codec_info| is a pointer to the SBC codec_info to decode. 141 // Returns the bits per audio sample on success, or -1 if |p_codec_info| 142 // contains invalid codec information. 143 int A2DP_GetTrackBitsPerSampleSbc(const uint8_t* p_codec_info); 144 145 // Gets the channel count for the A2DP SBC codec. 146 // |p_codec_info| is a pointer to the SBC codec_info to decode. 147 // Returns the channel count on success, or -1 if |p_codec_info| 148 // contains invalid codec information. 149 int A2DP_GetTrackChannelCountSbc(const uint8_t* p_codec_info); 150 151 // Gets the number of subbands for the A2DP SBC codec. 152 // |p_codec_info| is a pointer to the SBC codec_info to decode. 153 // Returns the number of subbands on success, or -1 if |p_codec_info| 154 // contains invalid codec information. 155 int A2DP_GetNumberOfSubbandsSbc(const uint8_t* p_codec_info); 156 157 // Gets the number of blocks for the A2DP SBC codec. 158 // |p_codec_info| is a pointer to the SBC codec_info to decode. 159 // Returns the number of blocks on success, or -1 if |p_codec_info| 160 // contains invalid codec information. 161 int A2DP_GetNumberOfBlocksSbc(const uint8_t* p_codec_info); 162 163 // Gets the allocation method code for the A2DP SBC codec. 164 // The actual value is codec-specific - see |A2DP_SBC_IE_ALLOC_MD_*|. 165 // |p_codec_info| is a pointer to the SBC codec_info to decode. 166 // Returns the allocation method code on success, or -1 if |p_codec_info| 167 // contains invalid codec information. 168 int A2DP_GetAllocationMethodCodeSbc(const uint8_t* p_codec_info); 169 170 // Gets the channel mode code for the A2DP SBC codec. 171 // The actual value is codec-specific - see |A2DP_SBC_IE_CH_MD_*|. 172 // |p_codec_info| is a pointer to the SBC codec_info to decode. 173 // Returns the channel mode code on success, or -1 if |p_codec_info| 174 // contains invalid codec information. 175 int A2DP_GetChannelModeCodeSbc(const uint8_t* p_codec_info); 176 177 // Gets the sampling frequency code for the A2DP SBC codec. 178 // The actual value is codec-specific - see |A2DP_SBC_IE_SAMP_FREQ_*|. 179 // |p_codec_info| is a pointer to the SBC codec_info to decode. 180 // Returns the sampling frequency code on success, or -1 if |p_codec_info| 181 // contains invalid codec information. 182 int A2DP_GetSamplingFrequencyCodeSbc(const uint8_t* p_codec_info); 183 184 // Gets the minimum bitpool for the A2DP SBC codec. 185 // The actual value is codec-specific - see |A2DP_SBC_IE_MIN_BITPOOL|. 186 // |p_codec_info| is a pointer to the SBC codec_info to decode. 187 // Returns the minimum bitpool on success, or -1 if |p_codec_info| 188 // contains invalid codec information. 189 int A2DP_GetMinBitpoolSbc(const uint8_t* p_codec_info); 190 191 // Gets the maximum bitpool for the A2DP SBC codec. 192 // The actual value is codec-specific - see |A2DP_SBC_IE_MAX_BITPOOL|. 193 // |p_codec_info| is a pointer to the SBC codec_info to decode. 194 // Returns the maximum bitpool on success, or -1 if |p_codec_info| 195 // contains invalid codec information. 196 int A2DP_GetMaxBitpoolSbc(const uint8_t* p_codec_info); 197 198 // Gets the channel type for the A2DP SBC Sink codec: 199 // 1 for mono, or 3 for dual/stereo/joint. 200 // |p_codec_info| is a pointer to the SBC codec_info to decode. 201 // Returns the channel type on success, or -1 if |p_codec_info| 202 // contains invalid codec information. 203 int A2DP_GetSinkTrackChannelTypeSbc(const uint8_t* p_codec_info); 204 205 // Computes the number of frames to process in a time window for the A2DP 206 // SBC sink codec. |time_interval_ms| is the time interval (in milliseconds). 207 // |p_codec_info| is a pointer to the codec_info to decode. 208 // Returns the number of frames to process on success, or -1 if |p_codec_info| 209 // contains invalid codec information. 210 int A2DP_GetSinkFramesCountToProcessSbc(uint64_t time_interval_ms, 211 const uint8_t* p_codec_info); 212 213 // Gets the A2DP SBC audio data timestamp from an audio packet. 214 // |p_codec_info| contains the codec information. 215 // |p_data| contains the audio data. 216 // The timestamp is stored in |p_timestamp|. 217 // Returns true on success, otherwise false. 218 bool A2DP_GetPacketTimestampSbc(const uint8_t* p_codec_info, 219 const uint8_t* p_data, uint32_t* p_timestamp); 220 221 // Builds A2DP SBC codec header for audio data. 222 // |p_codec_info| contains the codec information. 223 // |p_buf| contains the audio data. 224 // |frames_per_packet| is the number of frames in this packet. 225 // Returns true on success, otherwise false. 226 bool A2DP_BuildCodecHeaderSbc(const uint8_t* p_codec_info, BT_HDR* p_buf, 227 uint16_t frames_per_packet); 228 229 // Decodes and displays SBC codec info (for debugging). 230 // |p_codec_info| is a pointer to the SBC codec_info to decode and display. 231 void A2DP_DumpCodecInfoSbc(const uint8_t* p_codec_info); 232 233 // Gets the A2DP SBC encoder interface that can be used to encode and prepare 234 // A2DP packets for transmission - see |tA2DP_ENCODER_INTERFACE|. 235 // |p_codec_info| contains the codec information. 236 // Returns the A2DP SBC encoder interface if the |p_codec_info| is valid and 237 // supported, otherwise NULL. 238 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceSbc( 239 const uint8_t* p_codec_info); 240 241 // Adjusts the A2DP SBC codec, based on local support and Bluetooth 242 // specification. 243 // |p_codec_info| contains the codec information to adjust. 244 // Returns true if |p_codec_info| is valid and supported, otherwise false. 245 bool A2DP_AdjustCodecSbc(uint8_t* p_codec_info); 246 247 // Gets the A2DP SBC Source codec index for a given |p_codec_info|. 248 // Returns the corresponding |btav_a2dp_codec_index_t| on success, 249 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|. 250 btav_a2dp_codec_index_t A2DP_SourceCodecIndexSbc(const uint8_t* p_codec_info); 251 252 // Gets the A2DP SBC Source codec name. 253 const char* A2DP_CodecIndexStrSbc(void); 254 255 // Gets the A2DP SBC Sink codec name. 256 const char* A2DP_CodecIndexStrSbcSink(void); 257 258 // Initializes A2DP SBC Source codec information into |tAVDT_CFG| configuration 259 // entry pointed by |p_cfg|. 260 bool A2DP_InitCodecConfigSbc(tAVDT_CFG* p_cfg); 261 262 // Initializes A2DP SBC Sink codec information into |tAVDT_CFG| configuration 263 // entry pointed by |p_cfg|. 264 bool A2DP_InitCodecConfigSbcSink(tAVDT_CFG* p_cfg); 265 266 #endif // A2DP_SBC_H 267