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/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-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{
|
|
|
|
|
|
|
|
|
|
class AACFrame;
|
|
|
|
|
|
2020-05-11 23:25:12 +08:00
|
|
|
|
string makeAacConfig(const uint8_t *hex);
|
|
|
|
|
void dumpAacConfig(const string &config, int length, uint8_t *out);
|
|
|
|
|
void parseAacConfig(const string &config, int &samplerate, int &channels);
|
2018-10-30 14:59:42 +08:00
|
|
|
|
|
2019-08-30 11:17:27 +08:00
|
|
|
|
/**
|
2018-10-30 14:59:42 +08:00
|
|
|
|
* aac帧,包含adts头
|
|
|
|
|
*/
|
2020-05-11 23:25:12 +08:00
|
|
|
|
class AACFrame : public FrameImp {
|
2018-10-30 14:59:42 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<AACFrame> Ptr;
|
2020-05-11 23:25:12 +08:00
|
|
|
|
AACFrame(){
|
|
|
|
|
_codecid = CodecAAC;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
2020-05-11 22:33:10 +08:00
|
|
|
|
};
|
2018-10-30 14:59:42 +08:00
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
class AACFrameNoCacheAble : public FrameFromPtr {
|
2018-10-30 14:59:42 +08:00
|
|
|
|
public:
|
2019-07-24 18:02:55 +08:00
|
|
|
|
typedef std::shared_ptr<AACFrameNoCacheAble> Ptr;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
AACFrameNoCacheAble(char *ptr,uint32_t size,uint32_t dts,uint32_t pts = 0,int prefixeSize = 7){
|
2019-07-03 16:22:12 +08:00
|
|
|
|
_ptr = ptr;
|
|
|
|
|
_size = size;
|
|
|
|
|
_dts = dts;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
_prefix_size = prefixeSize;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodecId getCodecId() const override{
|
|
|
|
|
return CodecAAC;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool keyFrame() const override {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
|
|
|
|
bool configFrame() const override{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-05-11 22:33:10 +08:00
|
|
|
|
};
|
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两个字节的配置信息
|
|
|
|
|
*/
|
|
|
|
|
AACTrack(const string &aac_cfg){
|
2019-08-30 16:56:57 +08:00
|
|
|
|
if(aac_cfg.size() < 2){
|
|
|
|
|
throw std::invalid_argument("adts配置必须最少2个字节");
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
2019-08-30 16:56:57 +08:00
|
|
|
|
_cfg = aac_cfg.substr(0,2);
|
2018-10-30 16:12:32 +08:00
|
|
|
|
onReady();
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造aac类型的媒体
|
|
|
|
|
* @param adts_header adts头,7个字节
|
|
|
|
|
* @param adts_header_len adts头长度,不少于7个字节
|
|
|
|
|
*/
|
|
|
|
|
AACTrack(const char *adts_header,int adts_header_len = 7){
|
|
|
|
|
if(adts_header_len < 7){
|
|
|
|
|
throw std::invalid_argument("adts头必须不少于7个字节");
|
|
|
|
|
}
|
2020-05-11 23:25:12 +08:00
|
|
|
|
_cfg = makeAacConfig((uint8_t *) adts_header);
|
2018-10-30 16:12:32 +08:00
|
|
|
|
onReady();
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造aac类型的媒体
|
|
|
|
|
* @param aac_frame_with_adts 带adts头的aac帧
|
|
|
|
|
*/
|
|
|
|
|
AACTrack(const Frame::Ptr &aac_frame_with_adts){
|
|
|
|
|
if(aac_frame_with_adts->getCodecId() != CodecAAC || aac_frame_with_adts->prefixSize() < 7){
|
|
|
|
|
throw std::invalid_argument("必须输入带adts头的aac帧");
|
|
|
|
|
}
|
2020-05-11 23:25:12 +08:00
|
|
|
|
_cfg = makeAacConfig((uint8_t *) aac_frame_with_adts->data());
|
2018-10-30 16:12:32 +08:00
|
|
|
|
onReady();
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取aac两个字节的配置
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
/**
|
|
|
|
|
* 返回音频采样位数,一般为16或8
|
|
|
|
|
*/
|
|
|
|
|
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-04-26 15:19:32 +08:00
|
|
|
|
if (_cfg.empty()) {
|
2019-01-24 12:21:29 +08:00
|
|
|
|
//未获取到aac_cfg信息
|
2020-04-26 15:19:32 +08:00
|
|
|
|
if (frame->prefixSize() >= 7) {
|
2019-01-24 12:21:29 +08:00
|
|
|
|
//7个字节的adts头
|
2020-05-11 23:25:12 +08:00
|
|
|
|
_cfg = makeAacConfig((uint8_t *) (frame->data()));
|
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
|
|
|
|
}
|
|
|
|
|
AudioTrack::inputFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* 解析2个字节的aac配置
|
|
|
|
|
*/
|
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 音频采样率
|
|
|
|
|
* @param playload_type rtp playload type 默认98
|
|
|
|
|
* @param bitrate 比特率
|
|
|
|
|
*/
|
|
|
|
|
AACSdp(const string &aac_cfg,
|
|
|
|
|
int sample_rate,
|
2020-04-30 13:35:38 +08:00
|
|
|
|
int channels,
|
2018-10-30 17:58:10 +08:00
|
|
|
|
int playload_type = 98,
|
|
|
|
|
int bitrate = 128) : Sdp(sample_rate,playload_type){
|
|
|
|
|
_printer << "m=audio 0 RTP/AVP " << playload_type << "\r\n";
|
|
|
|
|
_printer << "b=AS:" << bitrate << "\r\n";
|
2020-04-30 13:35:38 +08:00
|
|
|
|
_printer << "a=rtpmap:" << playload_type << " MPEG4-GENERIC/" << sample_rate << "/" << channels << "\r\n";
|
2018-10-30 17:58:10 +08:00
|
|
|
|
|
|
|
|
|
char configStr[32] = {0};
|
|
|
|
|
snprintf(configStr, sizeof(configStr), "%02X%02X", (uint8_t)aac_cfg[0], (uint8_t)aac_cfg[1]);
|
|
|
|
|
_printer << "a=fmtp:" << playload_type << " streamtype=5;profile-level-id=1;mode=AAC-hbr;"
|
|
|
|
|
<< "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
|