ZLMediaKit/src/Extension/AAC.h

211 lines
5.4 KiB
C++
Raw Normal View History

2018-10-30 14:59:42 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2018-10-30 14:59:42 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2018-10-30 14:59:42 +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-30 14:59:42 +08:00
*/
#ifndef ZLMEDIAKIT_AAC_H
#define ZLMEDIAKIT_AAC_H
#include "Frame.h"
#include "Track.h"
2020-05-11 23:25:12 +08:00
#define ADTS_HEADER_LEN 7
2018-10-30 14:59:42 +08:00
namespace mediakit{
string makeAacConfig(const uint8_t *hex, size_t length);
int getAacFrameLength(const uint8_t *hex, size_t length);
int dumpAacConfig(const string &config, size_t length, uint8_t *out, size_t out_size);
2020-06-11 19:21:46 +08:00
bool parseAacConfig(const string &config, int &samplerate, int &channels);
2018-10-30 14:59:42 +08:00
/**
* aac音频通道
*/
class AACTrack : public AudioTrack{
public:
typedef std::shared_ptr<AACTrack> Ptr;
/**
* adts头信息
* inputFrame中获取adts头信息
*/
AACTrack(){}
/**
* aac类型的媒体
* @param aac_cfg aac配置信息
2018-10-30 14:59:42 +08:00
*/
AACTrack(const string &aac_cfg){
setAacCfg(aac_cfg);
}
/**
* aac
*/
void setAacCfg(const string &aac_cfg){
if (aac_cfg.size() < 2) {
2019-08-30 16:56:57 +08:00
throw std::invalid_argument("adts配置必须最少2个字节");
2018-10-30 14:59:42 +08:00
}
_cfg = aac_cfg;
2018-10-30 16:12:32 +08:00
onReady();
2018-10-30 14:59:42 +08:00
}
/**
* aac
2018-10-30 14:59:42 +08:00
*/
const string &getAacCfg() const{
return _cfg;
}
/**
*
*/
CodecId getCodecId() const override{
return CodecAAC;
}
/**
* aac_cfg前是无效的Track
*/
bool ready() override {
return !_cfg.empty();
}
/**
2020-05-11 22:33:10 +08:00
*
*/
2018-10-30 14:59:42 +08:00
int getAudioSampleRate() const override{
return _sampleRate;
}
2020-05-11 22:33:10 +08:00
2018-10-30 14:59:42 +08:00
/**
* 168
*/
int getAudioSampleBit() const override{
return _sampleBit;
}
2020-05-11 22:33:10 +08:00
2018-10-30 14:59:42 +08:00
/**
*
*/
int getAudioChannel() const override{
return _channel;
}
/**
2020-05-11 22:33:10 +08:00
* ,aac_cfg
* @param frame
*/
2018-10-30 14:59:42 +08:00
void inputFrame(const Frame::Ptr &frame) override{
2020-08-08 12:19:04 +08:00
if (frame->prefixSize()) {
//有adts头尝试分帧
auto ptr = frame->data();
auto end = frame->data() + frame->size();
while (ptr < end) {
auto frame_len = getAacFrameLength((uint8_t *) ptr, end - ptr);
if (frame_len < ADTS_HEADER_LEN) {
break;
}
auto sub_frame = std::make_shared<FrameInternal<FrameFromPtr> >(frame, (char *) ptr, frame_len, ADTS_HEADER_LEN);
ptr += frame_len;
sub_frame->setCodecId(CodecAAC);
inputFrame_l(sub_frame);
}
} else {
inputFrame_l(frame);
}
}
private:
void inputFrame_l(const Frame::Ptr &frame) {
2020-04-26 15:19:32 +08:00
if (_cfg.empty()) {
2019-01-24 12:21:29 +08:00
//未获取到aac_cfg信息
2020-06-11 19:21:46 +08:00
if (frame->prefixSize()) {
//根据7个字节的adts头生成aac config
2020-06-11 19:21:46 +08:00
_cfg = makeAacConfig((uint8_t *) (frame->data()), frame->prefixSize());
2019-01-24 12:21:29 +08:00
onReady();
2020-04-26 15:19:32 +08:00
} else {
2019-01-24 12:21:29 +08:00
WarnL << "无法获取adts头!";
}
2018-10-30 14:59:42 +08:00
}
if (frame->size() > frame->prefixSize()) {
//除adts头外有实际负载
AudioTrack::inputFrame(frame);
}
2018-10-30 14:59:42 +08:00
}
/**
* 2aac配置
*/
2018-10-30 16:12:32 +08:00
void onReady(){
2020-05-11 23:25:12 +08:00
if (_cfg.size() < 2) {
2019-12-11 09:29:10 +08:00
return;
}
2020-05-11 23:25:12 +08:00
parseAacConfig(_cfg, _sampleRate, _channel);
2018-10-30 14:59:42 +08:00
}
2020-05-11 22:33:10 +08:00
2018-10-30 14:59:42 +08:00
Track::Ptr clone() override {
return std::make_shared<std::remove_reference<decltype(*this)>::type >(*this);
}
2019-06-28 16:12:39 +08:00
//生成sdp
Sdp::Ptr getSdp() override ;
2018-10-30 14:59:42 +08:00
private:
string _cfg;
int _sampleRate = 0;
int _sampleBit = 16;
int _channel = 0;
};
2018-10-30 17:58:10 +08:00
/**
2020-05-11 22:33:10 +08:00
* aac类型SDP
*/
2018-10-30 17:58:10 +08:00
class AACSdp : public Sdp {
public:
/**
2020-05-11 22:33:10 +08:00
*
2018-10-30 17:58:10 +08:00
* @param aac_cfg aac两个字节的配置描述
* @param sample_rate
2020-05-25 13:51:00 +08:00
* @param payload_type rtp payload type 98
2018-10-30 17:58:10 +08:00
* @param bitrate
*/
AACSdp(const string &aac_cfg,
int sample_rate,
2020-04-30 13:35:38 +08:00
int channels,
2020-12-05 12:22:17 +08:00
int bitrate = 128,
int payload_type = 98) : Sdp(sample_rate,payload_type){
2020-05-25 13:51:00 +08:00
_printer << "m=audio 0 RTP/AVP " << payload_type << "\r\n";
2020-12-05 12:22:17 +08:00
if (bitrate) {
_printer << "b=AS:" << bitrate << "\r\n";
}
2020-05-25 13:51:00 +08:00
_printer << "a=rtpmap:" << payload_type << " MPEG4-GENERIC/" << sample_rate << "/" << channels << "\r\n";
2018-10-30 17:58:10 +08:00
string configStr;
char buf[4] = {0};
for(auto &ch : aac_cfg){
snprintf(buf, sizeof(buf), "%02X", (uint8_t)ch);
configStr.append(buf);
}
2020-05-25 13:51:00 +08:00
_printer << "a=fmtp:" << payload_type << " streamtype=5;profile-level-id=1;mode=AAC-hbr;"
2020-05-11 23:43:28 +08:00
<< "sizelength=13;indexlength=3;indexdeltalength=3;config=" << configStr << "\r\n";
2020-05-11 22:33:10 +08:00
_printer << "a=control:trackID=" << (int)TrackAudio << "\r\n";
2018-10-30 17:58:10 +08:00
}
string getSdp() const override {
return _printer;
}
CodecId getCodecId() const override {
return CodecAAC;
}
private:
_StrPrinter _printer;
};
2018-10-30 14:59:42 +08:00
}//namespace mediakit
2020-05-11 22:33:10 +08:00
#endif //ZLMEDIAKIT_AAC_H