ZLMediaKit/src/Extension/AACRtmp.cpp

135 lines
3.9 KiB
C++
Raw Normal View History

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 "AACRtmp.h"
2020-04-04 22:54:49 +08:00
#include "Rtmp/Rtmp.h"
2018-10-24 14:21:59 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit{
2018-10-24 14:21:59 +08:00
AACRtmpDecoder::AACRtmpDecoder() {
_adts = obtainFrame();
}
AACFrame::Ptr AACRtmpDecoder::obtainFrame() {
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
auto frame = ResourcePoolHelper<AACFrame>::obtainObj();
2020-05-11 23:25:12 +08:00
frame->_prefix_size = ADTS_HEADER_LEN;
//预留7个字节的空位以便后续覆盖
frame->_buffer.assign(ADTS_HEADER_LEN,(char)0);
2018-10-24 14:21:59 +08:00
return frame;
}
2020-04-04 22:54:49 +08:00
static string getAacCfg(const RtmpPacket &thiz) {
string ret;
if (thiz.getMediaType() != FLV_CODEC_AAC) {
return ret;
}
if (!thiz.isCfgFrame()) {
return ret;
}
if (thiz.strBuf.size() < 4) {
WarnL << "bad aac cfg!";
return ret;
}
ret = thiz.strBuf.substr(2, 2);
return ret;
}
2020-05-11 23:25:12 +08:00
bool AACRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt, bool) {
2018-10-24 14:21:59 +08:00
if (pkt->isCfgFrame()) {
2020-04-04 22:54:49 +08:00
_aac_cfg = getAacCfg(*pkt);
2018-10-24 14:21:59 +08:00
return false;
}
if (!_aac_cfg.empty()) {
onGetAAC(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2, pkt->timeStamp);
}
return false;
}
2020-05-11 23:25:12 +08:00
void AACRtmpDecoder::onGetAAC(const char* data, int len, uint32_t stamp) {
_adts->_dts = stamp;
//先追加数据
_adts->_buffer.append(data, len);
//覆盖adts头
dumpAacConfig(_aac_cfg, _adts->size(), (uint8_t *) _adts->data());
2018-10-24 14:21:59 +08:00
//写入环形缓存
RtmpCodec::inputFrame(_adts);
_adts = obtainFrame();
}
2020-05-11 23:25:12 +08:00
2018-10-24 14:21:59 +08:00
/////////////////////////////////////////////////////////////////////////////////////
AACRtmpEncoder::AACRtmpEncoder(const Track::Ptr &track) {
_track = dynamic_pointer_cast<AACTrack>(track);
}
void AACRtmpEncoder::makeConfigPacket() {
if (_track && _track->ready()) {
//从track中和获取aac配置信息
_aac_cfg = _track->getAacCfg();
}
if (!_aac_cfg.empty()) {
makeAudioConfigPkt();
}
}
2018-10-24 14:21:59 +08:00
void AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
if (_aac_cfg.empty()) {
if (frame->prefixSize() >= 7) {
//包含adts头,从adts头获取aac配置信息
2020-05-11 23:25:12 +08:00
_aac_cfg = makeAacConfig((uint8_t *) (frame->data()));
}
makeConfigPacket();
2018-10-24 14:21:59 +08:00
}
2018-10-24 14:44:36 +08:00
if(!_aac_cfg.empty()){
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
2018-10-24 17:50:09 +08:00
rtmpPkt->strBuf.clear();
2020-04-18 18:46:20 +08:00
//header
2018-10-24 14:44:36 +08:00
uint8_t is_config = false;
2020-04-18 22:13:11 +08:00
rtmpPkt->strBuf.push_back(_audio_flv_flags);
2018-10-24 14:44:36 +08:00
rtmpPkt->strBuf.push_back(!is_config);
2020-04-18 18:46:20 +08:00
//aac data
2018-10-24 14:44:36 +08:00
rtmpPkt->strBuf.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
rtmpPkt->bodySize = rtmpPkt->strBuf.size();
rtmpPkt->chunkId = CHUNK_AUDIO;
rtmpPkt->streamId = STREAM_MEDIA;
rtmpPkt->timeStamp = frame->dts();
2018-10-24 14:44:36 +08:00
rtmpPkt->typeId = MSG_AUDIO;
2018-10-26 18:40:31 +08:00
RtmpCodec::inputRtmp(rtmpPkt, false);
2018-10-24 14:44:36 +08:00
}
2018-10-24 14:21:59 +08:00
}
void AACRtmpEncoder::makeAudioConfigPkt() {
2020-04-18 22:13:11 +08:00
_audio_flv_flags = getAudioRtmpFlags(std::make_shared<AACTrack>(_aac_cfg));
2018-10-24 14:21:59 +08:00
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
2018-10-24 17:50:09 +08:00
rtmpPkt->strBuf.clear();
2020-04-18 18:46:20 +08:00
//header
2018-10-24 14:21:59 +08:00
uint8_t is_config = true;
2020-04-18 22:13:11 +08:00
rtmpPkt->strBuf.push_back(_audio_flv_flags);
2018-10-24 14:21:59 +08:00
rtmpPkt->strBuf.push_back(!is_config);
2020-04-18 18:46:20 +08:00
//aac config
2018-10-24 14:21:59 +08:00
rtmpPkt->strBuf.append(_aac_cfg);
rtmpPkt->bodySize = rtmpPkt->strBuf.size();
rtmpPkt->chunkId = CHUNK_AUDIO;
rtmpPkt->streamId = STREAM_MEDIA;
rtmpPkt->timeStamp = 0;
rtmpPkt->typeId = MSG_AUDIO;
2018-10-26 18:40:31 +08:00
RtmpCodec::inputRtmp(rtmpPkt, false);
2018-10-24 14:21:59 +08:00
}
2018-10-24 17:17:55 +08:00
}//namespace mediakit