2018-10-25 10:00:17 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*/
|
2020-04-04 20:30:09 +08:00
|
|
|
|
|
2019-06-28 17:37:11 +08:00
|
|
|
|
#include "AACRtp.h"
|
2020-05-11 23:25:12 +08:00
|
|
|
|
#define AAC_MAX_FRAME_SIZE (2 * 1024)
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit{
|
|
|
|
|
|
2018-10-21 21:45:44 +08:00
|
|
|
|
AACRtpEncoder::AACRtpEncoder(uint32_t ui32Ssrc,
|
|
|
|
|
uint32_t ui32MtuSize,
|
|
|
|
|
uint32_t ui32SampleRate,
|
2020-05-25 13:51:00 +08:00
|
|
|
|
uint8_t ui8PayloadType,
|
2018-10-21 21:45:44 +08:00
|
|
|
|
uint8_t ui8Interleaved) :
|
2018-10-23 11:38:56 +08:00
|
|
|
|
RtpInfo(ui32Ssrc,
|
|
|
|
|
ui32MtuSize,
|
|
|
|
|
ui32SampleRate,
|
2020-05-25 13:51:00 +08:00
|
|
|
|
ui8PayloadType,
|
2018-10-26 14:12:16 +08:00
|
|
|
|
ui8Interleaved){
|
2018-10-21 21:45:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 21:41:45 +08:00
|
|
|
|
void AACRtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
2019-05-28 17:14:36 +08:00
|
|
|
|
GET_CONFIG(uint32_t, cycleMS, Rtp::kCycleMS);
|
2020-01-14 10:04:24 +08:00
|
|
|
|
auto uiStamp = frame->dts();
|
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-31 09:36:12 +08:00
|
|
|
|
void AACRtpEncoder::makeAACRtp(const void *data, unsigned int len, bool mark, uint32_t uiStamp) {
|
2020-06-11 19:21:46 +08:00
|
|
|
|
RtpCodec::inputRtp(makeRtp(getTrackType(), data, len, mark, uiStamp), false);
|
2018-10-18 23:48:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
2019-01-24 12:21:29 +08:00
|
|
|
|
|
2020-06-11 19:21:46 +08:00
|
|
|
|
AACRtpDecoder::AACRtpDecoder(const Track::Ptr &track) {
|
2019-01-24 12:21:29 +08:00
|
|
|
|
auto aacTrack = dynamic_pointer_cast<AACTrack>(track);
|
2020-06-11 19:21:46 +08:00
|
|
|
|
if (!aacTrack || !aacTrack->ready()) {
|
2019-01-24 12:21:29 +08:00
|
|
|
|
WarnL << "该aac track无效!";
|
2020-06-11 19:21:46 +08:00
|
|
|
|
} else {
|
2019-01-24 12:21:29 +08:00
|
|
|
|
_aac_cfg = aacTrack->getAacCfg();
|
|
|
|
|
}
|
2020-06-11 19:21:46 +08:00
|
|
|
|
_frame = obtainFrame();
|
2019-01-24 12:21:29 +08:00
|
|
|
|
}
|
2020-06-11 19:21:46 +08:00
|
|
|
|
|
2018-10-26 14:12:16 +08:00
|
|
|
|
AACRtpDecoder::AACRtpDecoder() {
|
2020-06-11 19:21:46 +08:00
|
|
|
|
_frame = obtainFrame();
|
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();
|
2020-06-11 19:21:46 +08:00
|
|
|
|
frame->_prefix_size = 0;
|
|
|
|
|
frame->_buffer.clear();
|
2018-10-21 22:24:24 +08:00
|
|
|
|
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) {
|
2020-03-11 18:52:28 +08:00
|
|
|
|
//rtp数据开始部分
|
|
|
|
|
uint8_t *ptr = (uint8_t *) rtppack->data() + rtppack->offset;
|
|
|
|
|
//rtp数据末尾
|
|
|
|
|
const uint8_t *end = (uint8_t *) rtppack->data() + rtppack->size();
|
|
|
|
|
|
|
|
|
|
//首2字节表示Au-Header的个数,单位bit,所以除以16得到Au-Header个数
|
|
|
|
|
const uint16_t au_header_count = ((ptr[0] << 8) | ptr[1]) >> 4;
|
|
|
|
|
//忽略Au-Header区
|
|
|
|
|
ptr += 2 + au_header_count * 2;
|
|
|
|
|
|
|
|
|
|
while (ptr < end) {
|
2020-03-13 18:34:12 +08:00
|
|
|
|
auto size = (uint32_t) (end - ptr);
|
2020-06-11 19:21:46 +08:00
|
|
|
|
if (size > AAC_MAX_FRAME_SIZE) {
|
|
|
|
|
size = AAC_MAX_FRAME_SIZE;
|
2020-03-13 18:34:12 +08:00
|
|
|
|
}
|
2020-06-11 19:21:46 +08:00
|
|
|
|
if (_frame->size() + size > AAC_MAX_FRAME_SIZE) {
|
2020-03-11 18:52:28 +08:00
|
|
|
|
//数据太多了,先清空
|
|
|
|
|
flushData();
|
|
|
|
|
}
|
|
|
|
|
//追加aac数据
|
2020-06-11 19:21:46 +08:00
|
|
|
|
_frame->_buffer.append((char *) ptr, size);
|
|
|
|
|
_frame->_dts = rtppack->timeStamp;
|
2020-03-11 18:52:28 +08:00
|
|
|
|
ptr += size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rtppack->mark) {
|
|
|
|
|
//最后一个rtp分片
|
|
|
|
|
flushData();
|
|
|
|
|
}
|
2018-10-23 18:39:17 +08:00
|
|
|
|
return false;
|
2018-10-18 23:48:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-11 18:52:28 +08:00
|
|
|
|
void AACRtpDecoder::flushData() {
|
2020-06-11 19:21:46 +08:00
|
|
|
|
if (_frame->_buffer.empty()) {
|
2020-03-11 18:52:28 +08:00
|
|
|
|
//没有有效数据
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-11 23:25:12 +08:00
|
|
|
|
|
2020-06-11 19:21:46 +08:00
|
|
|
|
//插入adts头
|
|
|
|
|
char adts_header[32] = {0};
|
|
|
|
|
auto size = dumpAacConfig(_aac_cfg, _frame->_buffer.size(), (uint8_t *) adts_header, sizeof(adts_header));
|
|
|
|
|
if (size > 0) {
|
|
|
|
|
//插入adts头
|
|
|
|
|
_frame->_buffer.insert(0, adts_header, size);
|
|
|
|
|
_frame->_prefix_size = size;
|
|
|
|
|
}
|
|
|
|
|
RtpCodec::inputFrame(_frame);
|
|
|
|
|
_frame = obtainFrame();
|
2018-10-18 23:48:00 +08:00
|
|
|
|
}
|
2018-10-21 21:21:14 +08:00
|
|
|
|
|
2019-01-24 12:21:29 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
}//namespace mediakit
|
2018-10-21 21:45:44 +08:00
|
|
|
|
|
2018-10-21 22:24:24 +08:00
|
|
|
|
|
|
|
|
|
|