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
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
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
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
2018-10-24 14:21:59 +08:00
|
|
|
|
|
2023-07-22 19:40:28 +08:00
|
|
|
|
static string getConfig(const RtmpPacket &thiz) {
|
2020-04-04 22:54:49 +08:00
|
|
|
|
string ret;
|
2023-07-22 18:54:59 +08:00
|
|
|
|
if ((RtmpAudioCodec)thiz.getRtmpCodecId() != RtmpAudioCodec::aac) {
|
2020-04-04 22:54:49 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
2020-08-30 10:48:34 +08:00
|
|
|
|
if (thiz.buffer.size() < 4) {
|
2022-01-08 16:21:00 +08:00
|
|
|
|
WarnL << "get aac config failed, rtmp packet is: " << hexdump(thiz.data(), thiz.size());
|
2020-04-04 22:54:49 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
2020-08-30 10:48:34 +08:00
|
|
|
|
ret = thiz.buffer.substr(2);
|
2020-04-04 22:54:49 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-06 18:22:04 +08:00
|
|
|
|
void AACRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
2023-07-22 19:40:28 +08:00
|
|
|
|
if (pkt->isConfigFrame()) {
|
|
|
|
|
_aac_cfg = getConfig(*pkt);
|
2022-01-08 16:21:00 +08:00
|
|
|
|
if (!_aac_cfg.empty()) {
|
|
|
|
|
onGetAAC(nullptr, 0, 0);
|
|
|
|
|
}
|
2020-09-06 18:22:04 +08:00
|
|
|
|
return;
|
2018-10-24 14:21:59 +08:00
|
|
|
|
}
|
2020-06-30 21:08:52 +08:00
|
|
|
|
|
2018-10-24 14:21:59 +08:00
|
|
|
|
if (!_aac_cfg.empty()) {
|
2020-08-30 10:48:34 +08:00
|
|
|
|
onGetAAC(pkt->buffer.data() + 2, pkt->buffer.size() - 2, pkt->time_stamp);
|
2018-10-24 14:21:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void AACRtmpDecoder::onGetAAC(const char* data, size_t len, uint32_t stamp) {
|
2021-02-05 11:51:16 +08:00
|
|
|
|
auto frame = FrameImp::create();
|
2020-08-01 10:22:12 +08:00
|
|
|
|
frame->_codec_id = CodecAAC;
|
|
|
|
|
|
2020-06-11 19:21:46 +08:00
|
|
|
|
//生成adts头
|
|
|
|
|
char adts_header[32] = {0};
|
|
|
|
|
auto size = dumpAacConfig(_aac_cfg, len, (uint8_t *) adts_header, sizeof(adts_header));
|
|
|
|
|
if (size > 0) {
|
|
|
|
|
frame->_buffer.assign(adts_header, size);
|
|
|
|
|
frame->_prefix_size = size;
|
|
|
|
|
} else {
|
|
|
|
|
frame->_buffer.clear();
|
|
|
|
|
frame->_prefix_size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-30 21:11:59 +08:00
|
|
|
|
if(len > 0){
|
2020-06-30 21:00:45 +08:00
|
|
|
|
//追加负载数据
|
|
|
|
|
frame->_buffer.append(data, len);
|
|
|
|
|
frame->_dts = stamp;
|
|
|
|
|
}
|
2018-10-24 14:21:59 +08:00
|
|
|
|
|
2020-06-30 21:11:59 +08:00
|
|
|
|
if(size > 0 || len > 0){
|
2020-06-30 21:00:45 +08:00
|
|
|
|
//有adts头或者实际aac负载
|
|
|
|
|
RtmpCodec::inputFrame(frame);
|
|
|
|
|
}
|
2018-10-24 14:21:59 +08:00
|
|
|
|
}
|
2020-05-11 23:25:12 +08:00
|
|
|
|
|
2018-10-24 14:21:59 +08:00
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2020-06-11 19:21:46 +08:00
|
|
|
|
AACRtmpEncoder::AACRtmpEncoder(const Track::Ptr &track) {
|
2018-10-25 14:16:40 +08:00
|
|
|
|
_track = dynamic_pointer_cast<AACTrack>(track);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 11:46:15 +08:00
|
|
|
|
void AACRtmpEncoder::makeConfigPacket() {
|
|
|
|
|
if (_track && _track->ready()) {
|
|
|
|
|
//从track中和获取aac配置信息
|
2023-07-22 19:40:28 +08:00
|
|
|
|
_aac_cfg = _track->getConfig();
|
2020-01-15 11:46:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_aac_cfg.empty()) {
|
|
|
|
|
makeAudioConfigPkt();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
2020-01-15 11:46:15 +08:00
|
|
|
|
if (_aac_cfg.empty()) {
|
2020-06-11 19:21:46 +08:00
|
|
|
|
if (frame->prefixSize()) {
|
2023-07-22 18:54:59 +08:00
|
|
|
|
// 包含adts头,从adts头获取aac配置信息
|
|
|
|
|
_aac_cfg = makeAacConfig((uint8_t *)(frame->data()), frame->prefixSize());
|
2018-10-25 14:16:40 +08:00
|
|
|
|
}
|
2020-01-15 11:46:15 +08:00
|
|
|
|
makeConfigPacket();
|
2018-10-24 14:21:59 +08:00
|
|
|
|
}
|
2018-10-24 14:44:36 +08:00
|
|
|
|
|
2023-07-22 18:54:59 +08:00
|
|
|
|
if (_aac_cfg.empty()) {
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2018-10-24 14:44:36 +08:00
|
|
|
|
}
|
2021-09-27 13:12:53 +08:00
|
|
|
|
|
2023-07-22 18:54:59 +08:00
|
|
|
|
auto pkt = RtmpPacket::create();
|
|
|
|
|
// header
|
|
|
|
|
pkt->buffer.push_back(_audio_flv_flags);
|
|
|
|
|
pkt->buffer.push_back((uint8_t)RtmpAACPacketType::aac_raw);
|
|
|
|
|
// aac data
|
|
|
|
|
pkt->buffer.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
|
|
|
|
|
pkt->body_size = pkt->buffer.size();
|
|
|
|
|
pkt->chunk_id = CHUNK_AUDIO;
|
|
|
|
|
pkt->stream_index = STREAM_MEDIA;
|
|
|
|
|
pkt->time_stamp = frame->dts();
|
|
|
|
|
pkt->type_id = MSG_AUDIO;
|
|
|
|
|
RtmpCodec::inputRtmp(pkt);
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return true;
|
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));
|
2023-07-22 18:54:59 +08:00
|
|
|
|
auto pkt = RtmpPacket::create();
|
|
|
|
|
// header
|
|
|
|
|
pkt->buffer.push_back(_audio_flv_flags);
|
|
|
|
|
pkt->buffer.push_back((uint8_t)RtmpAACPacketType::aac_config_header);
|
|
|
|
|
// aac config
|
|
|
|
|
pkt->buffer.append(_aac_cfg);
|
|
|
|
|
pkt->body_size = pkt->buffer.size();
|
|
|
|
|
pkt->chunk_id = CHUNK_AUDIO;
|
|
|
|
|
pkt->stream_index = STREAM_MEDIA;
|
|
|
|
|
pkt->time_stamp = 0;
|
|
|
|
|
pkt->type_id = MSG_AUDIO;
|
|
|
|
|
RtmpCodec::inputRtmp(pkt);
|
2018-10-24 14:21:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
}//namespace mediakit
|