1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef A2DP_ENCODER_SBC_H 17 #define A2DP_ENCODER_SBC_H 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <memory> 22 #include <vector> 23 #include "a2dp_sbc_dynamic_lib_ctrl.h" 24 #include "../../include/a2dp_codec_config.h" 25 #include "../../include/a2dp_codec_wrapper.h" 26 #include "packet.h" 27 28 namespace OHOS { 29 namespace bluetooth { 30 /* A2DP sbc max mtu size.(byte) */ 31 constexpr int A2DP_SBC_MAX_PACKET_SIZE = 6144; 32 constexpr int MAX_2MBPS_SBC_PAYLOAD_MTU = 655; 33 constexpr int MAX_3MBPS_SBC_PAYLOAD_MTU = 997; 34 constexpr int MAX_PCM_FRAME_NUM_PER_TICK = 14; 35 /* AVDP media headsize. */ 36 constexpr int A2DP_SBC_PACKET_HEAD_SIZE = 23; 37 constexpr int A2DP_SBC_MEDIA_PAYLOAD_HEAD_SIZE = 1; 38 constexpr int A2DP_SBC_ENCODER_INTERVAL_MS = 20; 39 constexpr int A2DP_SBC_NON_EDR_MAX_BITRATE = 229; 40 constexpr int A2DP_SBC_DEFAULT_BITRATE = 328; 41 constexpr int A2DP_SBC_HQ_DUAL_BP_53_FRAME_SIZE = 224; 42 constexpr int A2DP_SBC_FRAGMENT_HEADER = 1; 43 /* sample rate for sbc encode param */ 44 constexpr int SBC_SAMPLE_RATE_16000 = 16000; 45 constexpr int SBC_SAMPLE_RATE_32000 = 32000; 46 constexpr int SBC_SAMPLE_RATE_44100 = 44100; 47 constexpr int SBC_SAMPLE_RATE_48000 = 48000; 48 /* sample freq for sbc encode param */ 49 constexpr int SBC_SAMPLE_BITS_16 = 16; 50 constexpr int SBC_SAMPLE_BITS_24 = 24; 51 constexpr int SBC_SAMPLE_BITS_32 = 32; 52 /* channel Mode for sbc encode param */ 53 constexpr int SBC_MONO = 0; 54 constexpr int SBC_DUAL = 1; 55 constexpr int SBC_STEREO = 2; 56 constexpr int SBC_JOINT_STEREO = 3; 57 /* channel count for sbc encode param */ 58 constexpr int SBC_SINGLE_CHANNEL_COUNT = 1; 59 constexpr int SBC_DOUBLE_CHANNEL_COUNT = 2; 60 /* sub-band for sbc encode param */ 61 constexpr int SBC_SUBBAND_4 = 4; 62 constexpr int SBC_SUBBAND_8 = 8; 63 /* block-length for sbc encode param */ 64 constexpr int SBC_BLOCKS_4 = 4; 65 constexpr int SBC_BLOCKS_8 = 8; 66 constexpr int SBC_BLOCKS_12 = 12; 67 constexpr int SBC_BLOCKS_16 = 16; 68 /* allocation-method for sbc encode param */ 69 constexpr int SBC_LOUDNESS = 0; 70 constexpr int SBC_SNR = 1; 71 constexpr int FRAME_TWO = 2; 72 constexpr int FRAME_THREE = 3; 73 74 struct A2dpSBCFeedingParams { 75 uint32_t sampleRate; // 16000, 32000, 44100 or 48000. 76 uint16_t bitsPerSample; // 8, 16, 24 or32. 77 uint8_t channelCount; // mono: 1, else 2. 78 }; 79 80 struct A2dpSbcFeedingState { 81 uint32_t aaFrameCounter; 82 int32_t aaFeedCounter; 83 int32_t aaFeedResidue; 84 uint32_t counter; 85 uint32_t bytesPerTick; // Pcm bytes read during each media task tick. 86 uint64_t lastFrameTimestampNs; // Last tick timestamp, values in ns. 87 }; 88 89 struct SBCEncoderParams { 90 uint32_t samplingFreq; // 16000, 32000, 44100 or 48000. 91 int16_t channelMode; // mono:0, dual:1, stereo:2, joint stereo:3. 92 int16_t subBands; // 4 or 8. 93 int16_t channels; // 1 or 2. 94 int16_t numOfBlocks; // 4, 8, 12 or 16. 95 int16_t allocationMethod; // loudness:0, snr:1. 96 int16_t bitPool; // bitPool 97 uint16_t bitRate; // exp: 328. 98 }; 99 100 struct A2dpSbcEncoderCb { 101 uint16_t mtuSize; 102 bool isPeerEdr; // Whether peer device supports EDR. 103 bool peerSupports3mbps; // Whether peer device supports 3mbps EDR. 104 uint16_t peerMtu; // A2DP peer mtu. 105 uint32_t timestamp; // A2DP frames timestamp. 106 SBCEncoderParams sbcEncoderParams; 107 A2dpSBCFeedingParams feedingParams; 108 A2dpSbcFeedingState feedingState; 109 uint8_t pcmBuffer[A2DP_SBC_MAX_PACKET_SIZE * FRAME_THREE]; 110 uint8_t pcmRemain[A2DP_SBC_MAX_PACKET_SIZE]; 111 uint16_t offsetPCM; 112 uint16_t sendDataSize; 113 }; 114 115 class A2dpSbcEncoder : public A2dpEncoder { 116 public: 117 A2dpSbcEncoder(const A2dpEncoderInitPeerParams *peerParams, A2dpCodecConfig *config); 118 ~A2dpSbcEncoder() override; 119 void ResetFeedingState(void) override; 120 void SendFrames(uint64_t timeStampUs) override; 121 void UpdateEncoderParam() override; 122 void GetRenderPosition(uint64_t &sendDataSize, uint32_t &timeStamp) override; 123 124 private: 125 sbc::IEncoderBase* sbcEncoder_ = nullptr; 126 std::unique_ptr<A2dpSBCDynamicLibCtrl> codecLib_ = nullptr; 127 CODECSbcLib *codecSbcEncoderLib_ = nullptr; 128 void updateParam(void); 129 bool A2dpSbcReadFeeding(uint32_t *bytesRead); 130 void A2dpSbcCalculateEncBitPool(uint16_t samplingFreq, uint16_t minBitPool, uint16_t maxBitPool); 131 void A2dpSbcEncodeFrames(void); 132 void CalculateSbcPCMRemain(uint16_t codecSize, uint32_t bytesNum, uint8_t *numOfFrame); 133 void UpdateMtuSize(void); 134 static uint16_t A2dpSbcGetSampleRate(const uint8_t *codecInfo); 135 uint16_t A2dpSbcGetBitsPerSample() const; 136 static uint8_t A2dpSbcGetTrackChannelCount(const uint8_t *codecInfo); 137 static int16_t A2dpSbcGetTrackChannelMode(const uint8_t *codecInfo); 138 static int16_t A2dpSbcGetNumberOfSubbands(const uint8_t *codecInfo); 139 static int16_t A2dpSbcGetNumberOfBlocks(const uint8_t *codecInfo); 140 static int16_t A2dpSbcGetAllocationMethodCode(const uint8_t *codecInfo); 141 void A2dpSbcUpdateEncodeFeedingParams(const uint8_t *codecInfo); 142 void A2dpSbcUpdateEncoderParams(SBCEncoderParams *encParams, const uint8_t *codecInfo) const; 143 void SetSBCParam(void); 144 void ConvertFreqParamToSBCParam(void); 145 void ConvertModeParamToSBCParam(void); 146 void ConvertSubbandsParamToSBCParam(void); 147 void ConvertBlockParamToSBCParam(void); 148 void ConvertAllocationParamToSBCParam(void); 149 void ConvertBitpoolParamToSBCParam(void); 150 void EnqueuePacket(Packet *pkt, 151 size_t frames, const uint32_t bytes, uint32_t timeStamp, const uint16_t frameSize) const; 152 void EnqueuePacketFragment(Packet *pkt, 153 size_t frames, const uint32_t bytes, uint32_t timeStamp, const uint16_t frameSize) const; 154 A2dpSbcEncoderCb a2dpSbcEncoderCb_ {}; 155 BT_DISALLOW_COPY_AND_ASSIGN(A2dpSbcEncoder); 156 }; 157 } // namespace bluetooth 158 } // namespace OHOS 159 160 #endif // A2DP_ENCODER_SBC_H 161