1 /* 2 * Copyright 2021 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 Opus 19 // 20 21 #ifndef A2DP_VENDOR_OPUS_H 22 #define A2DP_VENDOR_OPUS_H 23 24 #include "a2dp_codec_api.h" 25 #include "a2dp_vendor_opus_constants.h" 26 #include "avdt_api.h" 27 28 class A2dpCodecConfigOpusBase : public A2dpCodecConfig { 29 protected: A2dpCodecConfigOpusBase(btav_a2dp_codec_index_t codec_index,const std::string & name,btav_a2dp_codec_priority_t codec_priority,bool is_source)30 A2dpCodecConfigOpusBase(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, A2DP_CODEC_ID_OPUS, 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 [[maybe_unused]] bool is_source_; // True if local is Source 43 }; 44 45 class A2dpCodecConfigOpusSource : public A2dpCodecConfigOpusBase { 46 public: 47 A2dpCodecConfigOpusSource(btav_a2dp_codec_priority_t codec_priority); 48 virtual ~A2dpCodecConfigOpusSource(); 49 50 bool init() override; 51 uint64_t encoderIntervalMs() const; 52 53 private: 54 bool useRtpHeaderMarkerBit() const override; 55 bool updateEncoderUserConfig( 56 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 57 bool* p_restart_input, bool* p_restart_output, bool* p_config_updated); 58 void debug_codec_dump(int fd) override; 59 }; 60 61 class A2dpCodecConfigOpusSink : public A2dpCodecConfigOpusBase { 62 public: 63 A2dpCodecConfigOpusSink(btav_a2dp_codec_priority_t codec_priority); 64 virtual ~A2dpCodecConfigOpusSink(); 65 66 bool init() override; 67 uint64_t encoderIntervalMs() const; 68 69 private: 70 bool useRtpHeaderMarkerBit() const override; 71 bool updateEncoderUserConfig( 72 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 73 bool* p_restart_input, bool* p_restart_output, bool* p_config_updated); 74 }; 75 76 // Checks whether the codec capabilities contain a valid A2DP Opus Source 77 // codec. 78 // NOTE: only codecs that are implemented are considered valid. 79 // Returns true if |p_codec_info| contains information about a valid Opus 80 // codec, otherwise false. 81 bool A2DP_IsVendorSourceCodecValidOpus(const uint8_t* p_codec_info); 82 83 // Checks whether the codec capabilities contain a valid A2DP Opus Sink 84 // codec. 85 // NOTE: only codecs that are implemented are considered valid. 86 // Returns true if |p_codec_info| contains information about a valid Opus 87 // codec, otherwise false. 88 bool A2DP_IsVendorSinkCodecValidOpus(const uint8_t* p_codec_info); 89 90 // Checks whether the codec capabilities contain a valid peer A2DP Opus Sink 91 // codec. 92 // NOTE: only codecs that are implemented are considered valid. 93 // Returns true if |p_codec_info| contains information about a valid Opus 94 // codec, otherwise false. 95 bool A2DP_IsVendorPeerSinkCodecValidOpus(const uint8_t* p_codec_info); 96 97 // Checks whether the codec capabilities contain a valid peer A2DP Opus Source 98 // codec. 99 // NOTE: only codecs that are implemented are considered valid. 100 // Returns true if |p_codec_info| contains information about a valid Opus 101 // codec, otherwise false. 102 bool A2DP_IsVendorPeerSourceCodecValidOpus(const uint8_t* p_codec_info); 103 104 // Checks whether A2DP Opus Sink codec is supported. 105 // |p_codec_info| contains information about the codec capabilities. 106 // Returns true if the A2DP Opus Sink codec is supported, otherwise false. 107 bool A2DP_IsVendorSinkCodecSupportedOpus(const uint8_t* p_codec_info); 108 109 // Checks whether an A2DP Opus Source codec for a peer Source device is 110 // supported. 111 // |p_codec_info| contains information about the codec capabilities of the 112 // peer device. 113 // Returns true if the A2DP Opus Source codec for a peer Source device is 114 // supported, otherwise false. 115 bool A2DP_IsPeerSourceCodecSupportedOpus(const uint8_t* p_codec_info); 116 117 // Checks whether the A2DP data packets should contain RTP header. 118 // |content_protection_enabled| is true if Content Protection is 119 // enabled. |p_codec_info| contains information about the codec capabilities. 120 // Returns true if the A2DP data packets should contain RTP header, otherwise 121 // false. 122 bool A2DP_VendorUsesRtpHeaderOpus(bool content_protection_enabled, 123 const uint8_t* p_codec_info); 124 125 // Gets the A2DP Opus codec name for a given |p_codec_info|. 126 const char* A2DP_VendorCodecNameOpus(const uint8_t* p_codec_info); 127 128 // Checks whether two A2DP Opus codecs |p_codec_info_a| and |p_codec_info_b| 129 // have the same type. 130 // Returns true if the two codecs have the same type, otherwise false. 131 bool A2DP_VendorCodecTypeEqualsOpus(const uint8_t* p_codec_info_a, 132 const uint8_t* p_codec_info_b); 133 134 // Checks whether two A2DP Opus codecs |p_codec_info_a| and |p_codec_info_b| 135 // are exactly the same. 136 // Returns true if the two codecs are exactly the same, otherwise false. 137 // If the codec type is not Opus, the return value is false. 138 bool A2DP_VendorCodecEqualsOpus(const uint8_t* p_codec_info_a, 139 const uint8_t* p_codec_info_b); 140 141 // Gets the track sample rate value for the A2DP Opus codec. 142 // |p_codec_info| is a pointer to the Opus codec_info to decode. 143 // Returns the track sample rate on success, or -1 if |p_codec_info| 144 // contains invalid codec information. 145 int A2DP_VendorGetTrackSampleRateOpus(const uint8_t* p_codec_info); 146 147 // Gets the track bits per sample value for the A2DP Opus codec. 148 // |p_codec_info| is a pointer to the Opus codec_info to decode. 149 // Returns the track bits per sample on success, or -1 if |p_codec_info| 150 // contains invalid codec information. 151 int A2DP_VendorGetTrackBitsPerSampleOpus(const uint8_t* p_codec_info); 152 153 // Gets the track bitrate value for the A2DP Opus codec. 154 // |p_codec_info| is a pointer to the Opus codec_info to decode. 155 // Returns the track sample rate on success, or -1 if |p_codec_info| 156 // contains invalid codec information. 157 int A2DP_VendorGetBitRateOpus(const uint8_t* p_codec_info); 158 159 // Gets the channel count for the A2DP Opus codec. 160 // |p_codec_info| is a pointer to the Opus codec_info to decode. 161 // Returns the channel count on success, or -1 if |p_codec_info| 162 // contains invalid codec information. 163 int A2DP_VendorGetTrackChannelCountOpus(const uint8_t* p_codec_info); 164 165 // Gets the channel type for the A2DP Opus codec. 166 // 1 for mono, or 3 for dual channel/stereo. 167 // |p_codec_info| is a pointer to the Opus codec_info to decode. 168 // Returns the channel count on success, or -1 if |p_codec_info| 169 // contains invalid codec information. 170 int A2DP_VendorGetSinkTrackChannelTypeOpus(const uint8_t* p_codec_info); 171 172 // Gets the channel mode code for the A2DP Opus codec. 173 // The actual value is codec-specific - see |A2DP_OPUS_CHANNEL_MODE_*|. 174 // |p_codec_info| is a pointer to the Opus codec_info to decode. 175 // Returns the channel mode code on success, or -1 if |p_codec_info| 176 // contains invalid codec information. 177 int A2DP_VendorGetChannelModeCodeOpus(const uint8_t* p_codec_info); 178 179 // Gets the framesize value (in ms) for the A2DP Opus codec. 180 // |p_codec_info| is a pointer to the Opus codec_info to decode. 181 // Returns the framesize on success, or -1 if |p_codec_info| 182 // contains invalid codec information. 183 int A2DP_VendorGetFrameSizeOpus(const uint8_t* p_codec_info); 184 185 // Gets the A2DP Opus audio data timestamp from an audio packet. 186 // |p_codec_info| contains the codec information. 187 // |p_data| contains the audio data. 188 // The timestamp is stored in |p_timestamp|. 189 // Returns true on success, otherwise false. 190 bool A2DP_VendorGetPacketTimestampOpus(const uint8_t* p_codec_info, 191 const uint8_t* p_data, 192 uint32_t* p_timestamp); 193 194 // Builds A2DP Opus codec header for audio data. 195 // |p_codec_info| contains the codec information. 196 // |p_buf| contains the audio data. 197 // |frames_per_packet| is the number of frames in this packet. 198 // Returns true on success, otherwise false. 199 bool A2DP_VendorBuildCodecHeaderOpus(const uint8_t* p_codec_info, BT_HDR* p_buf, 200 uint16_t frames_per_packet); 201 202 // Decodes A2DP Opus codec info into a human readable string. 203 // |p_codec_info| is a pointer to the Opus codec_info to decode. 204 // Returns a string describing the codec information. 205 std::string A2DP_VendorCodecInfoStringOpus(const uint8_t* p_codec_info); 206 207 // Gets the A2DP Opus encoder interface that can be used to encode and prepare 208 // A2DP packets for transmission - see |tA2DP_ENCODER_INTERFACE|. 209 // |p_codec_info| contains the codec information. 210 // Returns the A2DP Opus encoder interface if the |p_codec_info| is valid and 211 // supported, otherwise NULL. 212 const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceOpus( 213 const uint8_t* p_codec_info); 214 215 // Gets the current A2DP Opus decoder interface that can be used to decode 216 // received A2DP packets - see |tA2DP_DECODER_INTERFACE|. 217 // |p_codec_info| contains the codec information. 218 // Returns the A2DP Opus decoder interface if the |p_codec_info| is valid and 219 // supported, otherwise NULL. 220 const tA2DP_DECODER_INTERFACE* A2DP_VendorGetDecoderInterfaceOpus( 221 const uint8_t* p_codec_info); 222 223 // Adjusts the A2DP Opus codec, based on local support and Bluetooth 224 // specification. 225 // |p_codec_info| contains the codec information to adjust. 226 // Returns true if |p_codec_info| is valid and supported, otherwise false. 227 bool A2DP_VendorAdjustCodecOpus(uint8_t* p_codec_info); 228 229 // Gets the A2DP Opus Source codec index for a given |p_codec_info|. 230 // Returns the corresponding |btav_a2dp_codec_index_t| on success, 231 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|. 232 btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexOpus( 233 const uint8_t* p_codec_info); 234 235 // Gets the A2DP Opus Sink codec index for a given |p_codec_info|. 236 // Returns the corresponding |btav_a2dp_codec_index_t| on success, 237 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|. 238 btav_a2dp_codec_index_t A2DP_VendorSinkCodecIndexOpus( 239 const uint8_t* p_codec_info); 240 241 // Gets the A2DP Opus Source codec name. 242 const char* A2DP_VendorCodecIndexStrOpus(void); 243 244 // Gets the A2DP Opus Sink codec name. 245 const char* A2DP_VendorCodecIndexStrOpusSink(void); 246 247 // Initializes A2DP Opus Source codec information into |AvdtpSepConfig| 248 // configuration entry pointed by |p_cfg|. 249 bool A2DP_VendorInitCodecConfigOpus(AvdtpSepConfig* p_cfg); 250 251 // Initializes A2DP Opus Sink codec information into |AvdtpSepConfig| 252 // configuration entry pointed by |p_cfg|. 253 bool A2DP_VendorInitCodecConfigOpusSink(AvdtpSepConfig* p_cfg); 254 255 #endif // A2DP_VENDOR_OPUS_H 256