2018-10-25 10:00:17 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* 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"
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit{
|
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool AACRtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
2023-12-09 16:23:51 +08:00
|
|
|
|
auto ptr = (char *)frame->data() + frame->prefixSize();
|
|
|
|
|
auto size = frame->size() - frame->prefixSize();
|
|
|
|
|
auto remain_size = size;
|
|
|
|
|
auto max_size = getRtpInfo().getMaxSize() - 4;
|
2021-01-31 19:19:24 +08:00
|
|
|
|
while (remain_size > 0) {
|
|
|
|
|
if (remain_size <= max_size) {
|
2023-12-09 16:23:51 +08:00
|
|
|
|
outputRtp(ptr, remain_size, size, true, frame->dts());
|
2018-10-18 23:48:00 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2023-12-09 16:23:51 +08:00
|
|
|
|
outputRtp(ptr, max_size, size, false, frame->dts());
|
2021-01-31 19:19:24 +08:00
|
|
|
|
ptr += max_size;
|
|
|
|
|
remain_size -= max_size;
|
2018-10-18 23:48:00 +08:00
|
|
|
|
}
|
2023-12-09 16:23:51 +08:00
|
|
|
|
return true;
|
2018-10-18 23:48:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
void AACRtpEncoder::outputRtp(const char *data, size_t len, size_t total_len, bool mark, uint64_t stamp) {
|
|
|
|
|
auto rtp = getRtpInfo().makeRtp(TrackAudio, nullptr, len + 4, mark, stamp);
|
|
|
|
|
auto payload = rtp->data() + RtpPacket::kRtpTcpHeaderSize + RtpPacket::kRtpHeaderSize;
|
|
|
|
|
payload[0] = 0;
|
|
|
|
|
payload[1] = 16;
|
|
|
|
|
payload[2] = ((total_len) >> 5) & 0xFF;
|
|
|
|
|
payload[3] = ((total_len & 0x1F) << 3) & 0xFF;
|
|
|
|
|
memcpy(payload + 4, data, len);
|
|
|
|
|
RtpCodec::inputRtp(std::move(rtp), false);
|
2018-10-18 23:48:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
2019-01-24 12:21:29 +08:00
|
|
|
|
|
2018-10-26 14:12:16 +08:00
|
|
|
|
AACRtpDecoder::AACRtpDecoder() {
|
2020-08-01 10:22:12 +08:00
|
|
|
|
obtainFrame();
|
2018-10-21 22:24:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 10:22:12 +08:00
|
|
|
|
void AACRtpDecoder::obtainFrame() {
|
2018-10-21 22:24:24 +08:00
|
|
|
|
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
2021-02-05 11:51:16 +08:00
|
|
|
|
_frame = FrameImp::create();
|
2020-08-01 10:22:12 +08:00
|
|
|
|
_frame->_codec_id = CodecAAC;
|
2018-10-21 22:24:24 +08:00
|
|
|
|
}
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
2021-01-31 19:19:24 +08:00
|
|
|
|
bool AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
|
2022-05-28 10:34:54 +08:00
|
|
|
|
auto payload_size = rtp->getPayloadSize();
|
|
|
|
|
if (payload_size <= 0) {
|
|
|
|
|
//无实际负载
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 19:19:24 +08:00
|
|
|
|
auto stamp = rtp->getStampMS();
|
2020-03-11 18:52:28 +08:00
|
|
|
|
//rtp数据开始部分
|
2021-01-31 19:19:24 +08:00
|
|
|
|
auto ptr = rtp->getPayload();
|
2020-03-11 18:52:28 +08:00
|
|
|
|
//rtp数据末尾
|
2022-05-28 10:34:54 +08:00
|
|
|
|
auto end = ptr + payload_size;
|
2020-03-11 18:52:28 +08:00
|
|
|
|
//首2字节表示Au-Header的个数,单位bit,所以除以16得到Au-Header个数
|
2021-01-31 19:19:24 +08:00
|
|
|
|
auto au_header_count = ((ptr[0] << 8) | ptr[1]) >> 4;
|
2022-08-10 10:44:34 +08:00
|
|
|
|
if (!au_header_count) {
|
|
|
|
|
//问题issue: https://github.com/ZLMediaKit/ZLMediaKit/issues/1869
|
|
|
|
|
WarnL << "invalid aac rtp au_header_count";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-07-24 20:03:35 +08:00
|
|
|
|
//记录au_header起始指针
|
2021-01-31 19:19:24 +08:00
|
|
|
|
auto au_header_ptr = ptr + 2;
|
2020-07-24 20:03:35 +08:00
|
|
|
|
ptr = au_header_ptr + au_header_count * 2;
|
|
|
|
|
|
|
|
|
|
if (end < ptr) {
|
|
|
|
|
//数据不够
|
|
|
|
|
return false;
|
2020-03-11 18:52:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 20:10:47 +08:00
|
|
|
|
if (!_last_dts) {
|
|
|
|
|
//记录第一个时间戳
|
2021-01-31 19:19:24 +08:00
|
|
|
|
_last_dts = stamp;
|
2020-07-24 20:10:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 20:03:35 +08:00
|
|
|
|
//每个audio unit时间戳增量
|
2021-01-31 19:19:24 +08:00
|
|
|
|
auto dts_inc = (stamp - _last_dts) / au_header_count;
|
2020-07-24 20:10:47 +08:00
|
|
|
|
if (dts_inc < 0 && dts_inc > 100) {
|
|
|
|
|
//时间戳增量异常,忽略
|
2020-07-24 20:03:35 +08:00
|
|
|
|
dts_inc = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < au_header_count; ++i) {
|
|
|
|
|
// 之后的2字节是AU_HEADER,其中高13位表示一帧AAC负载的字节长度,低3位无用
|
|
|
|
|
uint16_t size = ((au_header_ptr[0] << 8) | au_header_ptr[1]) >> 3;
|
|
|
|
|
if (ptr + size > end) {
|
|
|
|
|
//数据不够
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-07-24 20:10:47 +08:00
|
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
|
//设置aac数据
|
|
|
|
|
_frame->_buffer.assign((char *) ptr, size);
|
|
|
|
|
//设置当前audio unit时间戳
|
|
|
|
|
_frame->_dts = _last_dts + i * dts_inc;
|
|
|
|
|
ptr += size;
|
|
|
|
|
au_header_ptr += 2;
|
|
|
|
|
flushData();
|
|
|
|
|
}
|
2020-03-11 18:52:28 +08:00
|
|
|
|
}
|
2020-07-24 20:03:35 +08:00
|
|
|
|
//记录上次时间戳
|
2021-01-31 19:19:24 +08:00
|
|
|
|
_last_dts = stamp;
|
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() {
|
2021-10-27 13:26:53 +08:00
|
|
|
|
auto ptr = reinterpret_cast<const uint8_t *>(_frame->data());
|
|
|
|
|
if ((ptr[0] == 0xFF && (ptr[1] & 0xF0) == 0xF0) && _frame->size() > ADTS_HEADER_LEN) {
|
2023-12-09 16:23:51 +08:00
|
|
|
|
// adts头打入了rtp包,不符合规范,兼容EasyPusher的bug
|
2021-10-27 13:23:27 +08:00
|
|
|
|
_frame->_prefix_size = ADTS_HEADER_LEN;
|
2020-06-11 19:21:46 +08:00
|
|
|
|
}
|
|
|
|
|
RtpCodec::inputFrame(_frame);
|
2020-08-01 10:22:12 +08:00
|
|
|
|
obtainFrame();
|
2018-10-18 23:48:00 +08:00
|
|
|
|
}
|
2018-10-21 21:21:14 +08:00
|
|
|
|
|
2020-08-01 10:22:12 +08:00
|
|
|
|
}//namespace mediakit
|