2018-10-18 23:48:00 +08:00
|
|
|
//
|
|
|
|
// Created by xzl on 2018/10/18.
|
|
|
|
//
|
|
|
|
|
2018-10-21 21:21:14 +08:00
|
|
|
#include "AACRtpCodec.h"
|
2018-10-18 23:48:00 +08:00
|
|
|
|
2018-10-21 21:45:44 +08:00
|
|
|
AACRtpEncoder::AACRtpEncoder(uint32_t ui32Ssrc,
|
|
|
|
uint32_t ui32MtuSize,
|
|
|
|
uint32_t ui32SampleRate,
|
|
|
|
uint8_t ui8PlayloadType,
|
|
|
|
uint8_t ui8Interleaved) :
|
2018-10-23 11:38:56 +08:00
|
|
|
RtpInfo(ui32Ssrc,
|
|
|
|
ui32MtuSize,
|
|
|
|
ui32SampleRate,
|
|
|
|
ui8PlayloadType,
|
|
|
|
ui8Interleaved),
|
|
|
|
AACRtpDecoder(ui32SampleRate){
|
2018-10-21 21:45:44 +08:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:41:45 +08:00
|
|
|
void AACRtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
|
|
|
RtpCodec::inputFrame(frame);
|
2018-10-18 23:48:00 +08:00
|
|
|
|
2018-10-21 21:45:44 +08:00
|
|
|
GET_CONFIG_AND_REGISTER(uint32_t, cycleMS, Config::Rtp::kCycleMS);
|
2018-10-18 23:48:00 +08:00
|
|
|
auto uiStamp = frame->stamp();
|
2018-10-21 22:24:24 +08:00
|
|
|
auto pcData = frame->data() + frame->prefixSize();
|
|
|
|
auto iLen = frame->size() - frame->prefixSize();
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
|
|
uiStamp %= cycleMS;
|
|
|
|
char *ptr = (char *) pcData;
|
|
|
|
int iSize = iLen;
|
2018-10-21 21:45:44 +08:00
|
|
|
while (iSize > 0) {
|
2018-10-24 15:43:52 +08:00
|
|
|
if (iSize <= _ui32MtuSize - 20) {
|
|
|
|
_aucSectionBuf[0] = 0;
|
|
|
|
_aucSectionBuf[1] = 16;
|
|
|
|
_aucSectionBuf[2] = iLen >> 5;
|
|
|
|
_aucSectionBuf[3] = (iLen & 0x1F) << 3;
|
|
|
|
memcpy(_aucSectionBuf + 4, ptr, iSize);
|
|
|
|
makeAACRtp(_aucSectionBuf, iSize + 4, true, uiStamp);
|
2018-10-18 23:48:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
_aucSectionBuf[0] = 0;
|
|
|
|
_aucSectionBuf[1] = 16;
|
|
|
|
_aucSectionBuf[2] = (iLen) >> 5;
|
|
|
|
_aucSectionBuf[3] = (iLen & 0x1F) << 3;
|
|
|
|
memcpy(_aucSectionBuf + 4, ptr, _ui32MtuSize - 20);
|
|
|
|
makeAACRtp(_aucSectionBuf, _ui32MtuSize - 16, false, uiStamp);
|
|
|
|
ptr += (_ui32MtuSize - 20);
|
|
|
|
iSize -= (_ui32MtuSize - 20);
|
2018-10-18 23:48:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 21:45:44 +08:00
|
|
|
void AACRtpEncoder::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp) {
|
2018-10-18 23:48:00 +08:00
|
|
|
uint16_t u16RtpLen = uiLen + 12;
|
2018-10-24 15:43:52 +08:00
|
|
|
_ui32TimeStamp = (_ui32SampleRate / 1000) * uiStamp;
|
|
|
|
uint32_t ts = htonl(_ui32TimeStamp);
|
|
|
|
uint16_t sq = htons(_ui16Sequence);
|
|
|
|
uint32_t sc = htonl(_ui32Ssrc);
|
2018-10-24 12:01:40 +08:00
|
|
|
auto pRtppkt = ResourcePoolHelper<RtpPacket>::obtainObj();
|
|
|
|
auto &rtppkt = *pRtppkt;
|
2018-10-18 23:48:00 +08:00
|
|
|
unsigned char *pucRtp = rtppkt.payload;
|
|
|
|
pucRtp[0] = '$';
|
2018-10-24 15:43:52 +08:00
|
|
|
pucRtp[1] = _ui8Interleaved;
|
2018-10-18 23:48:00 +08:00
|
|
|
pucRtp[2] = u16RtpLen >> 8;
|
|
|
|
pucRtp[3] = u16RtpLen & 0x00FF;
|
|
|
|
pucRtp[4] = 0x80;
|
2018-10-24 15:43:52 +08:00
|
|
|
pucRtp[5] = (bMark << 7) | _ui8PlayloadType;
|
2018-10-18 23:48:00 +08:00
|
|
|
memcpy(&pucRtp[6], &sq, 2);
|
|
|
|
memcpy(&pucRtp[8], &ts, 4);
|
|
|
|
//ssrc
|
|
|
|
memcpy(&pucRtp[12], &sc, 4);
|
|
|
|
//playload
|
|
|
|
memcpy(&pucRtp[16], pData, uiLen);
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
rtppkt.PT = _ui8PlayloadType;
|
|
|
|
rtppkt.interleaved = _ui8Interleaved;
|
2018-10-18 23:48:00 +08:00
|
|
|
rtppkt.mark = bMark;
|
|
|
|
rtppkt.length = uiLen + 16;
|
2018-10-24 15:43:52 +08:00
|
|
|
rtppkt.sequence = _ui16Sequence;
|
|
|
|
rtppkt.timeStamp = _ui32TimeStamp;
|
|
|
|
rtppkt.ssrc = _ui32Ssrc;
|
2018-10-18 23:48:00 +08:00
|
|
|
rtppkt.type = TrackAudio;
|
|
|
|
rtppkt.offset = 16;
|
|
|
|
|
|
|
|
RtpCodec::inputRtp(pRtppkt, false);
|
2018-10-24 15:43:52 +08:00
|
|
|
_ui16Sequence++;
|
2018-10-18 23:48:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
2018-10-21 22:24:24 +08:00
|
|
|
AACRtpDecoder::AACRtpDecoder(uint32_t ui32SampleRate) {
|
2018-10-24 15:43:52 +08:00
|
|
|
_adts = obtainFrame();
|
|
|
|
_sampleRate = ui32SampleRate;
|
2018-10-21 22:24:24 +08:00
|
|
|
}
|
|
|
|
|
2018-10-23 11:09:21 +08:00
|
|
|
AACFrame::Ptr AACRtpDecoder::obtainFrame() {
|
2018-10-21 22:24:24 +08:00
|
|
|
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
2018-10-24 12:01:40 +08:00
|
|
|
auto frame = ResourcePoolHelper<AACFrame>::obtainObj();
|
2018-10-21 22:24:24 +08:00
|
|
|
frame->aac_frame_length = 7;
|
|
|
|
frame->iPrefixSize = 7;
|
|
|
|
return frame;
|
|
|
|
}
|
2018-10-18 23:48:00 +08:00
|
|
|
|
2018-10-23 18:39:17 +08:00
|
|
|
bool AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
|
2018-10-21 22:24:24 +08:00
|
|
|
RtpCodec::inputRtp(rtppack, false);
|
2018-10-18 23:48:00 +08:00
|
|
|
|
2018-10-21 21:21:14 +08:00
|
|
|
int length = rtppack->length - rtppack->offset;
|
2018-10-24 15:43:52 +08:00
|
|
|
if (_adts->aac_frame_length + length - 4 > sizeof(AACFrame::buffer)) {
|
|
|
|
_adts->aac_frame_length = 7;
|
2018-10-21 21:21:14 +08:00
|
|
|
WarnL << "aac负载数据太长";
|
2018-10-23 18:39:17 +08:00
|
|
|
return false;
|
2018-10-18 23:48:00 +08:00
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
memcpy(_adts->buffer + _adts->aac_frame_length, rtppack->payload + rtppack->offset + 4, length - 4);
|
|
|
|
_adts->aac_frame_length += (length - 4);
|
2018-10-21 21:21:14 +08:00
|
|
|
if (rtppack->mark == true) {
|
2018-10-24 15:43:52 +08:00
|
|
|
_adts->sequence = rtppack->sequence;
|
|
|
|
_adts->timeStamp = rtppack->timeStamp * (1000.0 / _sampleRate);
|
|
|
|
writeAdtsHeader(*_adts, _adts->buffer);
|
|
|
|
onGetAAC(_adts);
|
2018-10-18 23:48:00 +08:00
|
|
|
}
|
2018-10-23 18:39:17 +08:00
|
|
|
return false;
|
2018-10-18 23:48:00 +08:00
|
|
|
}
|
|
|
|
|
2018-10-24 14:21:59 +08:00
|
|
|
void AACRtpDecoder::onGetAAC(const AACFrame::Ptr &frame) {
|
2018-10-21 21:27:05 +08:00
|
|
|
//写入环形缓存
|
2018-10-23 21:41:45 +08:00
|
|
|
RtpCodec::inputFrame(frame);
|
2018-10-24 15:43:52 +08:00
|
|
|
_adts = obtainFrame();
|
2018-10-18 23:48:00 +08:00
|
|
|
}
|
2018-10-21 21:21:14 +08:00
|
|
|
|
2018-10-21 21:45:44 +08:00
|
|
|
|
2018-10-21 22:24:24 +08:00
|
|
|
|
|
|
|
|