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, const std::string& name, 31 btav_a2dp_codec_priority_t codec_priority, bool is_source) 32 : A2dpCodecConfig(codec_index, bluetooth::a2dp::CodecId::OPUS, name, codec_priority), 33 is_source_(is_source) {} 34 tA2DP_STATUS setCodecConfig(const uint8_t* p_peer_codec_info, bool is_capability, 35 uint8_t* p_result_codec_config) override; 36 bool setPeerCodecCapabilities(const uint8_t* p_peer_codec_capabilities) override; 37 38 private: 39 [[maybe_unused]] bool is_source_; // True if local is Source 40 }; 41 42 class A2dpCodecConfigOpusSource : public A2dpCodecConfigOpusBase { 43 public: 44 A2dpCodecConfigOpusSource(btav_a2dp_codec_priority_t codec_priority); 45 virtual ~A2dpCodecConfigOpusSource(); 46 47 bool init() override; 48 uint64_t encoderIntervalMs() const; 49 50 private: 51 bool useRtpHeaderMarkerBit() const override; 52 bool updateEncoderUserConfig(const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 53 bool* p_restart_input, bool* p_restart_output, 54 bool* p_config_updated); 55 void debug_codec_dump(int fd) override; 56 }; 57 58 class A2dpCodecConfigOpusSink : public A2dpCodecConfigOpusBase { 59 public: 60 A2dpCodecConfigOpusSink(btav_a2dp_codec_priority_t codec_priority); 61 virtual ~A2dpCodecConfigOpusSink(); 62 63 bool init() override; 64 uint64_t encoderIntervalMs() const; 65 66 private: 67 bool useRtpHeaderMarkerBit() const override; 68 bool updateEncoderUserConfig(const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 69 bool* p_restart_input, bool* p_restart_output, 70 bool* p_config_updated); 71 }; 72 73 // Checks whether the codec capabilities contain a valid A2DP Opus 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 Opus 77 // codec, otherwise false. 78 bool A2DP_IsCodecValidOpus(const uint8_t* p_codec_info); 79 80 // Checks whether A2DP Opus Sink codec is supported. 81 // |p_codec_info| contains information about the codec capabilities. 82 // Returns true if the A2DP Opus Sink codec is supported, otherwise false. 83 tA2DP_STATUS A2DP_IsVendorSinkCodecSupportedOpus(const uint8_t* p_codec_info); 84 85 // Checks whether the A2DP data packets should contain RTP header. 86 // |content_protection_enabled| is true if Content Protection is 87 // enabled. |p_codec_info| contains information about the codec capabilities. 88 // Returns true if the A2DP data packets should contain RTP header, otherwise 89 // false. 90 bool A2DP_VendorUsesRtpHeaderOpus(bool content_protection_enabled, const uint8_t* p_codec_info); 91 92 // Gets the A2DP Opus codec name for a given |p_codec_info|. 93 const char* A2DP_VendorCodecNameOpus(const uint8_t* p_codec_info); 94 95 // Checks whether two A2DP Opus codecs |p_codec_info_a| and |p_codec_info_b| 96 // have the same type. 97 // Returns true if the two codecs have the same type, otherwise false. 98 bool A2DP_VendorCodecTypeEqualsOpus(const uint8_t* p_codec_info_a, const uint8_t* p_codec_info_b); 99 100 // Checks whether two A2DP Opus codecs |p_codec_info_a| and |p_codec_info_b| 101 // are exactly the same. 102 // Returns true if the two codecs are exactly the same, otherwise false. 103 // If the codec type is not Opus, the return value is false. 104 bool A2DP_VendorCodecEqualsOpus(const uint8_t* p_codec_info_a, const uint8_t* p_codec_info_b); 105 106 // Gets the track sample rate value for the A2DP Opus codec. 107 // |p_codec_info| is a pointer to the Opus codec_info to decode. 108 // Returns the track sample rate on success, or -1 if |p_codec_info| 109 // contains invalid codec information. 110 int A2DP_VendorGetTrackSampleRateOpus(const uint8_t* p_codec_info); 111 112 // Gets the track bits per sample value for the A2DP Opus codec. 113 // |p_codec_info| is a pointer to the Opus codec_info to decode. 114 // Returns the track bits per sample on success, or -1 if |p_codec_info| 115 // contains invalid codec information. 116 int A2DP_VendorGetTrackBitsPerSampleOpus(const uint8_t* p_codec_info); 117 118 // Gets the track bitrate value for the A2DP Opus codec. 119 // |p_codec_info| is a pointer to the Opus codec_info to decode. 120 // Returns the track sample rate on success, or -1 if |p_codec_info| 121 // contains invalid codec information. 122 int A2DP_VendorGetBitRateOpus(const uint8_t* p_codec_info); 123 124 // Gets the channel count for the A2DP Opus codec. 125 // |p_codec_info| is a pointer to the Opus codec_info to decode. 126 // Returns the channel count on success, or -1 if |p_codec_info| 127 // contains invalid codec information. 128 int A2DP_VendorGetTrackChannelCountOpus(const uint8_t* p_codec_info); 129 130 // Gets the channel type for the A2DP Opus codec. 131 // 1 for mono, or 3 for dual channel/stereo. 132 // |p_codec_info| is a pointer to the Opus codec_info to decode. 133 // Returns the channel count on success, or -1 if |p_codec_info| 134 // contains invalid codec information. 135 int A2DP_VendorGetSinkTrackChannelTypeOpus(const uint8_t* p_codec_info); 136 137 // Gets the channel mode code for the A2DP Opus codec. 138 // The actual value is codec-specific - see |A2DP_OPUS_CHANNEL_MODE_*|. 139 // |p_codec_info| is a pointer to the Opus 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_VendorGetChannelModeCodeOpus(const uint8_t* p_codec_info); 143 144 // Gets the framesize value (in ms) for the A2DP Opus codec. 145 // |p_codec_info| is a pointer to the Opus codec_info to decode. 146 // Returns the framesize on success, or -1 if |p_codec_info| 147 // contains invalid codec information. 148 int A2DP_VendorGetFrameSizeOpus(const uint8_t* p_codec_info); 149 150 // Gets the A2DP Opus audio data timestamp from an audio packet. 151 // |p_codec_info| contains the codec information. 152 // |p_data| contains the audio data. 153 // The timestamp is stored in |p_timestamp|. 154 // Returns true on success, otherwise false. 155 bool A2DP_VendorGetPacketTimestampOpus(const uint8_t* p_codec_info, const uint8_t* p_data, 156 uint32_t* p_timestamp); 157 158 // Builds A2DP Opus codec header for audio data. 159 // |p_codec_info| contains the codec information. 160 // |p_buf| contains the audio data. 161 // |frames_per_packet| is the number of frames in this packet. 162 // Returns true on success, otherwise false. 163 bool A2DP_VendorBuildCodecHeaderOpus(const uint8_t* p_codec_info, BT_HDR* p_buf, 164 uint16_t frames_per_packet); 165 166 // Decodes A2DP Opus codec info into a human readable string. 167 // |p_codec_info| is a pointer to the Opus codec_info to decode. 168 // Returns a string describing the codec information. 169 std::string A2DP_VendorCodecInfoStringOpus(const uint8_t* p_codec_info); 170 171 // Gets the A2DP Opus encoder interface that can be used to encode and prepare 172 // A2DP packets for transmission - see |tA2DP_ENCODER_INTERFACE|. 173 // |p_codec_info| contains the codec information. 174 // Returns the A2DP Opus encoder interface if the |p_codec_info| is valid and 175 // supported, otherwise NULL. 176 const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceOpus(const uint8_t* p_codec_info); 177 178 // Gets the current A2DP Opus decoder interface that can be used to decode 179 // received A2DP packets - see |tA2DP_DECODER_INTERFACE|. 180 // |p_codec_info| contains the codec information. 181 // Returns the A2DP Opus decoder interface if the |p_codec_info| is valid and 182 // supported, otherwise NULL. 183 const tA2DP_DECODER_INTERFACE* A2DP_VendorGetDecoderInterfaceOpus(const uint8_t* p_codec_info); 184 185 // Adjusts the A2DP Opus codec, based on local support and Bluetooth 186 // specification. 187 // |p_codec_info| contains the codec information to adjust. 188 // Returns true if |p_codec_info| is valid and supported, otherwise false. 189 bool A2DP_VendorAdjustCodecOpus(uint8_t* p_codec_info); 190 191 // Gets the A2DP Opus Source codec index for a given |p_codec_info|. 192 // Returns the corresponding |btav_a2dp_codec_index_t| on success, 193 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|. 194 btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexOpus(const uint8_t* p_codec_info); 195 196 // Gets the A2DP Opus Sink codec index for a given |p_codec_info|. 197 // Returns the corresponding |btav_a2dp_codec_index_t| on success, 198 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|. 199 btav_a2dp_codec_index_t A2DP_VendorSinkCodecIndexOpus(const uint8_t* p_codec_info); 200 201 // Gets the A2DP Opus Source codec name. 202 const char* A2DP_VendorCodecIndexStrOpus(void); 203 204 // Gets the A2DP Opus Sink codec name. 205 const char* A2DP_VendorCodecIndexStrOpusSink(void); 206 207 // Initializes A2DP Opus Source codec information into |AvdtpSepConfig| 208 // configuration entry pointed by |p_cfg|. 209 bool A2DP_VendorInitCodecConfigOpus(AvdtpSepConfig* p_cfg); 210 211 // Initializes A2DP Opus Sink codec information into |AvdtpSepConfig| 212 // configuration entry pointed by |p_cfg|. 213 bool A2DP_VendorInitCodecConfigOpusSink(AvdtpSepConfig* p_cfg); 214 215 #endif // A2DP_VENDOR_OPUS_H 216