整理和精简代码

This commit is contained in:
xiongziliang 2020-05-11 22:33:10 +08:00
parent 41f1df3acf
commit 625d7e30c0
23 changed files with 326 additions and 564 deletions

View File

@ -163,7 +163,9 @@ void DevChannel::inputG711(const char *data, int len, uint32_t dts){
if (dts == 0) {
dts = (uint32_t)_aTicker[1].elapsedTime();
}
inputFrame(std::make_shared<G711FrameNoCacheAble>(_audio->codecId, (char*)data, len, dts, 0));
auto frame = std::make_shared<G711FrameNoCacheAble>((char*)data, len, dts, 0);
frame->setCodec(_audio->codecId);
inputFrame(frame);
}
void DevChannel::initVideo(const VideoInfo &info) {

View File

@ -18,20 +18,12 @@ namespace mediakit{
class AACFrame;
unsigned const samplingFrequencyTable[16] = { 96000, 88200,
64000, 48000,
44100, 32000,
24000, 22050,
16000, 12000,
11025, 8000,
7350, 0, 0, 0 };
unsigned const samplingFrequencyTable[16] = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, 0, 0, 0 };
void makeAdtsHeader(const string &strAudioCfg,AACFrame &adts);
void writeAdtsHeader(const AACFrame &adts, uint8_t *pcAdts) ;
string makeAdtsConfig(const uint8_t *pcAdts);
void getAACInfo(const AACFrame &adts,int &iSampleRate,int &iChannel);
/**
* aac帧adts头
*/
@ -46,14 +38,10 @@ public:
return aac_frame_length;
}
uint32_t dts() const override {
return timeStamp;
return _dts;
}
uint32_t prefixSize() const override{
return iPrefixSize;
}
TrackType getTrackType() const override{
return TrackAudio;
return _prefix_size;
}
CodecId getCodecId() const override{
@ -88,11 +76,11 @@ public:
//表示说ADTS帧中有一个AAC数据块并不是说没有。(一个AAC原始帧包含一段时间内1024个采样及相关数据)
unsigned int no_raw_data_blocks_in_frame; //2 uimsfb
unsigned char buffer[2 * 1024 + 7];
uint32_t timeStamp;
uint32_t iPrefixSize = 7;
uint32_t _dts;
uint32_t _prefix_size = 7;
};
class AACFrameNoCacheAble : public FrameNoCacheAble {
class AACFrameNoCacheAble : public FrameFromPtr {
public:
typedef std::shared_ptr<AACFrameNoCacheAble> Ptr;
@ -100,11 +88,7 @@ public:
_ptr = ptr;
_size = size;
_dts = dts;
_prefixSize = prefixeSize;
}
TrackType getTrackType() const override{
return TrackAudio;
_prefix_size = prefixeSize;
}
CodecId getCodecId() const override{
@ -120,7 +104,6 @@ public:
}
};
/**
* aac音频通道
*/
@ -173,7 +156,6 @@ public:
/**
* aac两个字节的配置
* @return
*/
const string &getAacCfg() const{
return _cfg;
@ -181,7 +163,6 @@ public:
/**
*
* @return
*/
CodecId getCodecId() const override{
return CodecAAC;
@ -189,30 +170,27 @@ public:
/**
* aac_cfg前是无效的Track
* @return
*/
bool ready() override {
return !_cfg.empty();
}
/**
*
* @return
*/
int getAudioSampleRate() const override{
return _sampleRate;
}
/**
* 168
* @return
*/
int getAudioSampleBit() const override{
return _sampleBit;
}
/**
*
* @return
*/
int getAudioChannel() const override{
return _channel;
@ -247,6 +225,7 @@ private:
makeAdtsHeader(_cfg,aacFrame);
getAACInfo(aacFrame,_sampleRate,_channel);
}
Track::Ptr clone() override {
return std::make_shared<std::remove_reference<decltype(*this)>::type >(*this);
}
@ -260,15 +239,13 @@ private:
int _channel = 0;
};
/**
* aac类型SDP
*/
class AACSdp : public Sdp {
public:
/**
*
*
* @param aac_cfg aac两个字节的配置描述
* @param sample_rate
* @param playload_type rtp playload type 98
@ -288,16 +265,13 @@ public:
_printer << "a=fmtp:" << playload_type << " streamtype=5;profile-level-id=1;mode=AAC-hbr;"
<< "sizelength=13;indexlength=3;indexdeltalength=3;config="
<< configStr << "\r\n";
_printer << "a=control:trackID=" << getTrackType() << "\r\n";
_printer << "a=control:trackID=" << (int)TrackAudio << "\r\n";
}
string getSdp() const override {
return _printer;
}
TrackType getTrackType() const override {
return TrackAudio;
}
CodecId getCodecId() const override {
return CodecAAC;
}
@ -306,6 +280,4 @@ private:
};
}//namespace mediakit
#endif //ZLMEDIAKIT_AAC_H

View File

@ -21,7 +21,7 @@ AACFrame::Ptr AACRtmpDecoder::obtainFrame() {
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
auto frame = ResourcePoolHelper<AACFrame>::obtainObj();
frame->aac_frame_length = 7;
frame->iPrefixSize = 7;
frame->_prefix_size = 7;
return frame;
}
@ -63,7 +63,7 @@ void AACRtmpDecoder::onGetAAC(const char* pcData, int iLen, uint32_t ui32TimeSta
//拷贝aac负载
memcpy(_adts->buffer + 7, pcData, iLen);
_adts->aac_frame_length = 7 + iLen;
_adts->timeStamp = ui32TimeStamp;
_adts->_dts = ui32TimeStamp;
//adts结构头转成头7个字节
writeAdtsHeader(*_adts, _adts->buffer);

View File

@ -33,10 +33,6 @@ public:
*/
bool inputRtmp(const RtmpPacket::Ptr &Rtmp, bool key_pos = false) override;
TrackType getTrackType() const override{
return TrackAudio;
}
CodecId getCodecId() const override{
return CodecAAC;
}

View File

@ -78,7 +78,7 @@ AACFrame::Ptr AACRtpDecoder::obtainFrame() {
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
auto frame = ResourcePoolHelper<AACFrame>::obtainObj();
frame->aac_frame_length = ADTS_HEADER_LEN;
frame->iPrefixSize = ADTS_HEADER_LEN;
frame->_prefix_size = ADTS_HEADER_LEN;
if(frame->syncword == 0 && !_aac_cfg.empty()) {
makeAdtsHeader(_aac_cfg,*frame);
}
@ -109,7 +109,7 @@ bool AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
//追加aac数据
memcpy(_adts->buffer + _adts->aac_frame_length, ptr, size);
_adts->aac_frame_length += size;
_adts->timeStamp = rtppack->timeStamp;
_adts->_dts = rtppack->timeStamp;
ptr += size;
}

View File

@ -31,9 +31,6 @@ public:
*/
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = false) override;
TrackType getTrackType() const override{
return TrackAudio;
}
CodecId getCodecId() const override{
return CodecAAC;
}

View File

@ -15,6 +15,59 @@ using namespace toolkit;
namespace mediakit{
/**
*
*/
class FrameCacheAble : public FrameFromPtr {
public:
typedef std::shared_ptr<FrameCacheAble> Ptr;
FrameCacheAble(const Frame::Ptr &frame){
if(frame->cacheAble()){
_frame = frame;
_ptr = frame->data();
}else{
_buffer = std::make_shared<BufferRaw>();
_buffer->assign(frame->data(),frame->size());
_ptr = _buffer->data();
}
_size = frame->size();
_dts = frame->dts();
_pts = frame->pts();
_prefix_size = frame->prefixSize();
_codecid = frame->getCodecId();
_key = frame->keyFrame();
_config = frame->configFrame();
}
virtual ~FrameCacheAble() = default;
/**
*
*/
bool cacheAble() const override {
return true;
}
CodecId getCodecId() const override{
return _codecid;
}
bool keyFrame() const override{
return _key;
}
bool configFrame() const override{
return _config;
}
private:
Frame::Ptr _frame;
BufferRaw::Ptr _buffer;
CodecId _codecid;
bool _key;
bool _config;
};
Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){
if(frame->cacheAble()){
return frame;
@ -30,10 +83,21 @@ const char *CodecInfo::getCodecName() {
SWITCH_CASE(CodecAAC);
SWITCH_CASE(CodecG711A);
SWITCH_CASE(CodecG711U);
default:
return "unknown codec";
SWITCH_CASE(CodecOpus);
default : return "unknown codec";
}
}
TrackType CodecInfo::getTrackType(){
switch (getCodecId()){
case CodecH264:
case CodecH265: return TrackVideo;
case CodecAAC:
case CodecG711A:
case CodecG711U:
case CodecOpus: return TrackAudio;
default: return TrackInvalid;
}
}
}//namespace mediakit

View File

@ -28,6 +28,7 @@ typedef enum {
CodecAAC,
CodecG711A,
CodecG711U,
CodecOpus,
CodecMax = 0x7FFF
} CodecId;
@ -49,11 +50,6 @@ public:
CodecInfo(){}
virtual ~CodecInfo(){}
/**
*
*/
virtual TrackType getTrackType() const = 0;
/**
*
*/
@ -61,9 +57,13 @@ public:
/**
*
* @return
*/
const char *getCodecName();
/**
*
*/
TrackType getTrackType();
};
/**
@ -76,15 +76,11 @@ public:
/**
*
* @return
*/
virtual uint32_t dts() const = 0;
/**
*
* @return
*/
virtual uint32_t pts() const {
return dts();
@ -98,13 +94,11 @@ public:
/**
*
* @return
*/
virtual bool keyFrame() const = 0;
/**
* sps pps vps
* @return
*/
virtual bool configFrame() const = 0;
@ -115,14 +109,77 @@ public:
/**
* frame
* @return
*/
static Ptr getCacheAbleFrame(const Ptr &frame);
};
class FrameImp : public Frame {
public:
typedef std::shared_ptr<FrameImp> Ptr;
char *data() const override{
return (char *)_buffer.data();
}
uint32_t size() const override {
return _buffer.size();
}
uint32_t dts() const override {
return _dts;
}
uint32_t pts() const override{
return _pts ? _pts : _dts;
}
uint32_t prefixSize() const override{
return _prefix_size;
}
CodecId getCodecId() const override{
return _codecid;
}
bool keyFrame() const override {
return false;
}
bool configFrame() const override{
return false;
}
public:
CodecId _codecid = CodecInvalid;
string _buffer;
uint32_t _dts = 0;
uint32_t _pts = 0;
uint32_t _prefix_size = 0;
};
/**
* Frame类中可以有多个帧 0x 00 00 01
* ZLMediaKit会先把这种复合帧split成单个帧然后再处理
* Frame
*
*/
template<typename Parent>
class FrameInternal : public Parent{
public:
typedef std::shared_ptr<FrameInternal> Ptr;
FrameInternal(const Frame::Ptr &parent_frame, char *ptr, uint32_t size, int prefix_size)
: Parent(ptr, size, parent_frame->dts(), parent_frame->pts(), prefix_size) {
_parent_frame = parent_frame;
}
bool cacheAble() const override {
return _parent_frame->cacheAble();
}
private:
Frame::Ptr _parent_frame;
};
/**
*
* @tparam T
*/
template <typename T>
class ResourcePoolHelper{
@ -140,17 +197,16 @@ private:
};
/**
*
*
*/
class FrameWriterInterface {
public:
typedef std::shared_ptr<FrameWriterInterface> Ptr;
FrameWriterInterface(){}
virtual ~FrameWriterInterface(){}
/**
*
* @param frame
*/
virtual void inputFrame(const Frame::Ptr &frame) = 0;
};
@ -165,15 +221,15 @@ public:
/**
* inputFrame后触发onWriteFrame回调
* @param cb
*/
FrameWriterInterfaceHelper(const onWriteFrame& cb){
_writeCallback = cb;
}
virtual ~FrameWriterInterfaceHelper(){}
/**
*
* @param frame
*/
void inputFrame(const Frame::Ptr &frame) override {
_writeCallback(frame);
@ -182,7 +238,6 @@ private:
onWriteFrame _writeCallback;
};
/**
*
*/
@ -193,6 +248,9 @@ public:
FrameDispatcher(){}
virtual ~FrameDispatcher(){}
/**
*
*/
void addDelegate(const FrameWriterInterface::Ptr &delegate){
//_delegates_write可能多线程同时操作
lock_guard<mutex> lck(_mtx);
@ -200,6 +258,9 @@ public:
_need_update = true;
}
/**
*
*/
void delDelegate(void *ptr){
//_delegates_write可能多线程同时操作
lock_guard<mutex> lck(_mtx);
@ -208,8 +269,7 @@ public:
}
/**
*
* @param frame
*
*/
void inputFrame(const Frame::Ptr &frame) override{
if(_need_update){
@ -223,7 +283,13 @@ public:
for(auto &pr : _delegates_read){
pr.second->inputFrame(frame);
}
}
/**
*
*/
int size() const {
return _delegates_write.size();
}
private:
mutex _mtx;
@ -250,105 +316,23 @@ public:
}
uint32_t pts() const override{
if(_pts){
return _pts;
}
return dts();
return _pts ? _pts : dts();
}
uint32_t prefixSize() const override{
return _prefixSize;
return _prefix_size;
}
bool cacheAble() const override {
return false;
}
protected:
char *_ptr;
uint32_t _size;
uint32_t _dts;
uint32_t _pts = 0;
uint32_t _prefixSize;
uint32_t _prefix_size;
};
/**
* DevChannel类中有用到
* 使
* ZLMediaKit是同步对帧数据进行使用和处理的
*
* Frame::getCacheAbleFrame方法拷贝一个可缓存的帧
*/
class FrameNoCacheAble : public FrameFromPtr{
public:
typedef std::shared_ptr<FrameNoCacheAble> Ptr;
/**
*
* @return
*/
bool cacheAble() const override {
return false;
}
};
/**
*
* @see FrameNoCacheAble
*/
class FrameCacheAble : public FrameFromPtr {
public:
typedef std::shared_ptr<FrameCacheAble> Ptr;
FrameCacheAble(const Frame::Ptr &frame){
if(frame->cacheAble()){
_frame = frame;
_ptr = frame->data();
}else{
_buffer = std::make_shared<BufferRaw>();
_buffer->assign(frame->data(),frame->size());
_ptr = _buffer->data();
}
_size = frame->size();
_dts = frame->dts();
_pts = frame->pts();
_prefixSize = frame->prefixSize();
_trackType = frame->getTrackType();
_codec = frame->getCodecId();
_key = frame->keyFrame();
_config = frame->configFrame();
}
virtual ~FrameCacheAble() = default;
/**
*
* @return
*/
bool cacheAble() const override {
return true;
}
TrackType getTrackType() const override{
return _trackType;
}
CodecId getCodecId() const override{
return _codec;
}
bool keyFrame() const override{
return _key;
}
bool configFrame() const override{
return _config;
}
private:
Frame::Ptr _frame;
BufferRaw::Ptr _buffer;
TrackType _trackType;
CodecId _codec;
bool _key;
bool _config;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_FRAME_H

View File

@ -19,76 +19,28 @@ namespace mediakit{
/**
* G711帧
*/
class G711Frame : public Frame {
class G711Frame : public FrameImp {
public:
typedef std::shared_ptr<G711Frame> Ptr;
char *data() const override{
return (char *)buffer.data();
G711Frame(){
_codecid = CodecG711A;
}
uint32_t size() const override {
return buffer.size();
}
uint32_t dts() const override {
return timeStamp;
}
uint32_t prefixSize() const override{
return 0;
}
TrackType getTrackType() const override{
return TrackAudio;
}
CodecId getCodecId() const override{
return _codecId;
}
bool keyFrame() const override {
return false;
}
bool configFrame() const override{
return false;
}
public:
CodecId _codecId = CodecG711A;
string buffer;
uint32_t timeStamp;
};
class G711FrameNoCacheAble : public FrameNoCacheAble {
class G711FrameNoCacheAble : public FrameFromPtr {
public:
typedef std::shared_ptr<G711FrameNoCacheAble> Ptr;
//兼容通用接口
G711FrameNoCacheAble(char *ptr,uint32_t size,uint32_t dts, uint32_t pts = 0,int prefixeSize = 0){
G711FrameNoCacheAble(char *ptr,uint32_t size,uint32_t dts, uint32_t pts = 0,int prefix_size = 0){
_ptr = ptr;
_size = size;
_dts = dts;
_prefixSize = prefixeSize;
_prefix_size = prefix_size;
}
//兼容通用接口
void setCodec(CodecId codecId){
_codecId = codecId;
}
G711FrameNoCacheAble(CodecId codecId, char *ptr,uint32_t size,uint32_t dts,int prefixeSize = 0){
_codecId = codecId;
_ptr = ptr;
_size = size;
_dts = dts;
_prefixSize = prefixeSize;
}
TrackType getTrackType() const override{
return TrackAudio;
}
CodecId getCodecId() const override{
return _codecId;
}
@ -108,67 +60,18 @@ private:
/**
* G711音频通道
*/
class G711Track : public AudioTrack{
class G711Track : public AudioTrackImp{
public:
typedef std::shared_ptr<G711Track> Ptr;
/**
* G711A G711U
*/
G711Track(CodecId codecId,int sample_rate, int channels, int sample_bit){
_codecid = codecId;
_sample_rate = sample_rate;
_channels = channels;
_sample_bit = sample_bit;
}
/**
*
*/
CodecId getCodecId() const override{
return _codecid;
}
/**
*
*/
bool ready() override {
return true;
}
/**
*
*/
int getAudioSampleRate() const override{
return _sample_rate;
}
/**
* 168
*/
int getAudioSampleBit() const override{
return _sample_bit;
}
/**
*
*/
int getAudioChannel() const override{
return _channels;
}
G711Track(CodecId codecId,int sample_rate, int channels, int sample_bit) : AudioTrackImp(codecId,sample_rate,channels,sample_bit){}
private:
//克隆该Track
Track::Ptr clone() override {
return std::make_shared<std::remove_reference<decltype(*this)>::type >(*this);
}
//生成sdp
Sdp::Ptr getSdp() override ;
private:
CodecId _codecid;
int _sample_rate;
int _channels;
int _sample_bit;
};
/**
@ -190,27 +93,20 @@ public:
int bitrate = 128) : Sdp(sample_rate,playload_type), _codecId(codecId){
_printer << "m=audio 0 RTP/AVP " << playload_type << "\r\n";
_printer << "a=rtpmap:" << playload_type << (codecId == CodecG711A ? " PCMA/" : " PCMU/") << sample_rate << "/" << channels << "\r\n";
_printer << "a=control:trackID=" << getTrackType() << "\r\n";
_printer << "a=control:trackID=" << (int)TrackAudio << "\r\n";
}
string getSdp() const override {
return _printer;
}
TrackType getTrackType() const override {
return TrackAudio;
}
CodecId getCodecId() const override {
return _codecId;
}
private:
_StrPrinter _printer;
CodecId _codecId;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_AAC_H
#endif //ZLMEDIAKIT_G711_H

View File

@ -20,15 +20,15 @@ G711RtmpDecoder::G711RtmpDecoder(CodecId codecId) {
G711Frame::Ptr G711RtmpDecoder::obtainFrame() {
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
auto frame = ResourcePoolHelper<G711Frame>::obtainObj();
frame->buffer.clear();
frame->_codecId = _codecId;
frame->_buffer.clear();
frame->_codecid = _codecId;
return frame;
}
bool G711RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt, bool) {
//拷贝G711负载
_frame->buffer.assign(pkt->strBuf.data() + 1, pkt->strBuf.size() - 1);
_frame->timeStamp = pkt->timeStamp;
_frame->_buffer.assign(pkt->strBuf.data() + 1, pkt->strBuf.size() - 1);
_frame->_dts = pkt->timeStamp;
//写入环形缓存
RtmpCodec::inputFrame(_frame);
_frame = obtainFrame();

View File

@ -33,10 +33,6 @@ public:
*/
bool inputRtmp(const RtmpPacket::Ptr &Rtmp, bool key_pos = false) override;
TrackType getTrackType() const override{
return TrackAudio;
}
CodecId getCodecId() const override{
return _codecId;
}

View File

@ -20,9 +20,9 @@ G711RtpDecoder::G711RtpDecoder(const Track::Ptr &track){
G711Frame::Ptr G711RtpDecoder::obtainFrame() {
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
auto frame = ResourcePoolHelper<G711Frame>::obtainObj();
frame->buffer.clear();
frame->_codecId = _codecid;
frame->timeStamp = 0;
frame->_buffer.clear();
frame->_codecid = _codecid;
frame->_dts = 0;
return frame;
}
@ -32,17 +32,17 @@ bool G711RtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool) {
// 获取rtp数据
const char *rtp_packet_buf = rtppack->data() + rtppack->offset;
if (rtppack->timeStamp != _frame->timeStamp) {
if (rtppack->timeStamp != _frame->_dts) {
//时间戳变更,清空上一帧
onGetG711(_frame);
}
//追加数据
_frame->buffer.append(rtp_packet_buf, length);
_frame->_buffer.append(rtp_packet_buf, length);
//赋值时间戳
_frame->timeStamp = rtppack->timeStamp;
_frame->_dts = rtppack->timeStamp;
if (rtppack->mark || _frame->buffer.size() > 10 * 1024) {
if (rtppack->mark || _frame->_buffer.size() > 10 * 1024) {
//标记为mark时或者内存快溢出时我们认为这是该帧最后一个包
onGetG711(_frame);
}
@ -50,7 +50,7 @@ bool G711RtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool) {
}
void G711RtpDecoder::onGetG711(const G711Frame::Ptr &frame) {
if(!frame->buffer.empty()){
if(!frame->_buffer.empty()){
//写入环形缓存
RtpCodec::inputFrame(frame);
_frame = obtainFrame();
@ -96,6 +96,3 @@ void G711RtpEncoder::makeG711Rtp(const void *data, unsigned int len, bool mark,
}
}//namespace mediakit

View File

@ -31,10 +31,6 @@ public:
*/
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = false) override;
TrackType getTrackType() const override{
return TrackAudio;
}
CodecId getCodecId() const override{
return _codecid;
}

View File

@ -25,7 +25,7 @@ void splitH264(const char *ptr, int len, int prefix, const std::function<void(co
/**
* 264
*/
class H264Frame : public Frame {
class H264Frame : public FrameImp {
public:
typedef std::shared_ptr<H264Frame> Ptr;
@ -36,30 +36,8 @@ public:
NAL_SEI = 6,
} NalType;
char *data() const override{
return (char *)_buffer.data();
}
uint32_t size() const override {
return _buffer.size();
}
uint32_t dts() const override {
return _dts;
}
uint32_t pts() const override {
return _pts ? _pts : _dts;
}
uint32_t prefixSize() const override{
return _prefix_size;
}
TrackType getTrackType() const override{
return TrackVideo;
}
CodecId getCodecId() const override{
return CodecH264;
H264Frame(){
_codecid = CodecH264;
}
bool keyFrame() const override {
@ -69,39 +47,27 @@ public:
bool configFrame() const override{
switch(H264_TYPE(_buffer[_prefix_size]) ){
case H264Frame::NAL_SPS:
case H264Frame::NAL_PPS:
return true;
default:
return false;
case H264Frame::NAL_PPS:return true;
default:return false;
}
}
public:
uint32_t _dts = 0;
uint32_t _pts = 0;
uint32_t _prefix_size = 4;
string _buffer;
};
/**
* H264类
* Frame类
* DevChannel中有使用
*/
class H264FrameNoCacheAble : public FrameNoCacheAble {
class H264FrameNoCacheAble : public FrameFromPtr {
public:
typedef std::shared_ptr<H264FrameNoCacheAble> Ptr;
H264FrameNoCacheAble(char *ptr,uint32_t size,uint32_t dts , uint32_t pts ,int prefixeSize = 4){
H264FrameNoCacheAble(char *ptr,uint32_t size,uint32_t dts , uint32_t pts ,int prefix_size = 4){
_ptr = ptr;
_size = size;
_dts = dts;
_pts = pts;
_prefixSize = prefixeSize;
}
TrackType getTrackType() const override{
return TrackVideo;
_prefix_size = prefix_size;
}
CodecId getCodecId() const override{
@ -109,43 +75,18 @@ public:
}
bool keyFrame() const override {
return H264_TYPE(_ptr[_prefixSize]) == H264Frame::NAL_IDR;
return H264_TYPE(_ptr[_prefix_size]) == H264Frame::NAL_IDR;
}
bool configFrame() const override{
switch(H264_TYPE(_ptr[_prefixSize])){
switch(H264_TYPE(_ptr[_prefix_size])){
case H264Frame::NAL_SPS:
case H264Frame::NAL_PPS:
return true;
default:
return false;
case H264Frame::NAL_PPS:return true;
default:return false;
}
}
};
/**
* H264Frame类中可以有多个帧 0x 00 00 01
* ZLMediaKit会先把这种复合帧split成单个帧然后再处理
* H264FrameSubFrame
*
*/
template<typename Parent>
class FrameInternal : public Parent{
public:
typedef std::shared_ptr<FrameInternal> Ptr;
FrameInternal(const Frame::Ptr &parent_frame,
char *ptr,
uint32_t size,
int prefixeSize) : Parent(ptr,size,parent_frame->dts(),parent_frame->pts(),prefixeSize){
_parent_frame = parent_frame;
}
bool cacheAble() const override {
return _parent_frame->cacheAble();
}
private:
Frame::Ptr _parent_frame;
};
typedef FrameInternal<H264FrameNoCacheAble> H264FrameInternal;
/**
@ -334,13 +275,11 @@ private:
bool _last_frame_is_idr = false;
};
/**
* h264类型sdp
*/
class H264Sdp : public Sdp {
public:
/**
*
* @param sps 264 sps,0x00000001
@ -375,17 +314,13 @@ public:
memset(strTemp, 0, 100);
av_base64_encode(strTemp, 100, (uint8_t *) strPPS.data(), strPPS.size());
_printer << strTemp << "\r\n";
_printer << "a=control:trackID=" << getTrackType() << "\r\n";
_printer << "a=control:trackID=" << (int)TrackVideo << "\r\n";
}
string getSdp() const override {
return _printer;
}
TrackType getTrackType() const override {
return TrackVideo;
}
CodecId getCodecId() const override {
return CodecH264;
}
@ -393,8 +328,5 @@ private:
_StrPrinter _printer;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_H264_H

View File

@ -36,10 +36,6 @@ public:
*/
bool inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos = true) override;
TrackType getTrackType() const override{
return TrackVideo;
}
CodecId getCodecId() const override{
return CodecH264;
}

View File

@ -38,10 +38,6 @@ public:
*/
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = true) override;
TrackType getTrackType() const override{
return TrackVideo;
}
CodecId getCodecId() const override{
return CodecH264;
}

View File

@ -25,7 +25,7 @@ bool getHEVCInfo(const string &strVps, const string &strSps, int &iVideoWidth, i
/**
* 265
*/
class H265Frame : public Frame {
class H265Frame : public FrameImp {
public:
typedef std::shared_ptr<H265Frame> Ptr;
@ -60,32 +60,8 @@ public:
NAL_SEI_SUFFIX = 40,
} NaleType;
char *data() const override {
return (char *) _buffer.data();
}
uint32_t size() const override {
return _buffer.size();
}
uint32_t dts() const override {
return _dts;
}
uint32_t pts() const override {
return _pts ? _pts : _dts;
}
uint32_t prefixSize() const override {
return _prefix_size;
}
TrackType getTrackType() const override {
return TrackVideo;
}
CodecId getCodecId() const override {
return CodecH265;
H265Frame(){
_codecid = CodecH265;
}
bool keyFrame() const override {
@ -96,39 +72,26 @@ public:
switch(H265_TYPE(_buffer[_prefix_size])){
case H265Frame::NAL_VPS:
case H265Frame::NAL_SPS:
case H265Frame::NAL_PPS:
return true;
default:
return false;
case H265Frame::NAL_PPS : return true;
default : return false;
}
}
static bool isKeyFrame(int type) {
return type >= NAL_BLA_W_LP && type <= NAL_RSV_IRAP_VCL23;
}
public:
uint32_t _dts = 0;
uint32_t _pts = 0;
uint32_t _prefix_size = 4;
string _buffer;
};
class H265FrameNoCacheAble : public FrameNoCacheAble {
class H265FrameNoCacheAble : public FrameFromPtr {
public:
typedef std::shared_ptr<H265FrameNoCacheAble> Ptr;
H265FrameNoCacheAble(char *ptr, uint32_t size, uint32_t dts,uint32_t pts, int prefixeSize = 4) {
H265FrameNoCacheAble(char *ptr, uint32_t size, uint32_t dts,uint32_t pts, int prefix_size = 4) {
_ptr = ptr;
_size = size;
_dts = dts;
_pts = pts;
_prefixSize = prefixeSize;
}
TrackType getTrackType() const override {
return TrackVideo;
_prefix_size = prefix_size;
}
CodecId getCodecId() const override {
@ -136,17 +99,15 @@ public:
}
bool keyFrame() const override {
return H265Frame::isKeyFrame(H265_TYPE(((uint8_t *) _ptr)[_prefixSize]));
return H265Frame::isKeyFrame(H265_TYPE(((uint8_t *) _ptr)[_prefix_size]));
}
bool configFrame() const override{
switch(H265_TYPE(((uint8_t *) _ptr)[_prefixSize])){
switch(H265_TYPE(((uint8_t *) _ptr)[_prefix_size])){
case H265Frame::NAL_VPS:
case H265Frame::NAL_SPS:
case H265Frame::NAL_PPS:
return true;
default:
return false;
case H265Frame::NAL_PPS:return true;
default:return false;
}
}
};
@ -184,7 +145,6 @@ public:
/**
* 0x00 00 00 01vps
* @return
*/
const string &getVps() const {
return _vps;
@ -192,7 +152,6 @@ public:
/**
* 0x00 00 00 01sps
* @return
*/
const string &getSps() const {
return _sps;
@ -200,7 +159,6 @@ public:
/**
* 0x00 00 00 01pps
* @return
*/
const string &getPps() const {
return _pps;
@ -212,7 +170,6 @@ public:
/**
*
* @return
*/
int getVideoHeight() const override{
return _height ;
@ -220,7 +177,6 @@ public:
/**
*
* @return
*/
int getVideoWidth() const override{
return _width;
@ -228,7 +184,6 @@ public:
/**
* fps
* @return
*/
float getVideoFps() const override{
return _fps;
@ -238,7 +193,6 @@ public:
return !_vps.empty() && !_sps.empty() && !_pps.empty();
}
/**
* ,sps pps
* @param frame
@ -352,15 +306,13 @@ private:
bool _last_frame_is_idr = false;
};
/**
* h265类型sdp
*/
class H265Sdp : public Sdp {
public:
/**
*
*
* @param sps 265 sps,0x00000001
* @param pps 265 pps,0x00000001
* @param playload_type rtp playload type 96
@ -382,17 +334,13 @@ public:
_printer << encodeBase64(strSPS) << "; ";
_printer << "sprop-pps=";
_printer << encodeBase64(strPPS) << "\r\n";
_printer << "a=control:trackID=" << getTrackType() << "\r\n";
_printer << "a=control:trackID=" << (int)TrackVideo << "\r\n";
}
string getSdp() const override {
return _printer;
}
TrackType getTrackType() const override {
return TrackVideo;
}
CodecId getCodecId() const override {
return CodecH265;
}
@ -400,9 +348,5 @@ private:
_StrPrinter _printer;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_H265_H

View File

@ -36,10 +36,6 @@ public:
*/
bool inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos = true) override;
TrackType getTrackType() const override{
return TrackVideo;
}
CodecId getCodecId() const override{
return CodecH265;
}

View File

@ -39,10 +39,6 @@ public:
*/
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = true) override;
TrackType getTrackType() const override{
return TrackVideo;
}
CodecId getCodecId() const override{
return CodecH265;
}

View File

@ -65,8 +65,6 @@ class VideoTrack : public Track {
public:
typedef std::shared_ptr<VideoTrack> Ptr;
TrackType getTrackType() const override { return TrackVideo;};
/**
*
* @return
@ -93,8 +91,6 @@ class AudioTrack : public Track {
public:
typedef std::shared_ptr<AudioTrack> Ptr;
TrackType getTrackType() const override { return TrackAudio;};
/**
*
* @return
@ -114,6 +110,64 @@ public:
virtual int getAudioChannel() const {return 0;};
};
class AudioTrackImp : public AudioTrack{
public:
typedef std::shared_ptr<AudioTrackImp> Ptr;
/**
*
* @param codecId
* @param sample_rate (HZ)
* @param channels
* @param sample_bit 16
*/
AudioTrackImp(CodecId codecId,int sample_rate, int channels, int sample_bit){
_codecid = codecId;
_sample_rate = sample_rate;
_channels = channels;
_sample_bit = sample_bit;
}
/**
*
*/
CodecId getCodecId() const override{
return _codecid;
}
/**
*
*/
bool ready() override {
return true;
}
/**
*
*/
int getAudioSampleRate() const override{
return _sample_rate;
}
/**
* 168
*/
int getAudioSampleBit() const override{
return _sample_bit;
}
/**
*
*/
int getAudioChannel() const override{
return _channels;
}
private:
CodecId _codecid;
int _sample_rate;
int _channels;
int _sample_bit;
};
class TrackSource{
public:
@ -123,7 +177,6 @@ public:
/**
* Track
* @param trackReady Track
* @return
*/
virtual vector<Track::Ptr> getTracks(bool trackReady = true) const = 0;
@ -131,7 +184,6 @@ public:
* Track
* @param type track类型
* @param trackReady Track
* @return
*/
Track::Ptr getTrack(TrackType type , bool trackReady = true) const {
auto tracks = getTracks(trackReady);
@ -145,5 +197,4 @@ public:
};
}//namespace mediakit
#endif //ZLMEDIAKIT_TRACK_H

View File

@ -242,18 +242,6 @@ public:
}
}
/**
*
* @return
*/
TrackType getTrackType() const override {
return TrackTitle;
}
/**
* id
* @return
*/
CodecId getCodecId() const override{
return CodecInvalid;
}
@ -266,18 +254,6 @@ public:
VideoMeta(const VideoTrack::Ptr &video,int datarate = 5000);
virtual ~VideoMeta(){}
/**
*
* @return
*/
TrackType getTrackType() const override {
return TrackVideo;
}
/**
* id
* @return
*/
CodecId getCodecId() const override{
return _codecId;
}
@ -294,18 +270,6 @@ public:
virtual ~AudioMeta(){}
/**
*
* @return
*/
TrackType getTrackType() const override {
return TrackAudio;
}
/**
* id
* @return
*/
CodecId getCodecId() const override{
return _codecId;
}
@ -317,7 +281,4 @@ private:
uint8_t getAudioRtmpFlags(const Track::Ptr &track);
}//namespace mediakit
#endif
#endif//__rtmp_h

View File

@ -319,7 +319,9 @@ void RtpProcess::onDecode(int stream,int codecid,int flags,int64_t pts,int64_t d
WarnP(this) << "audio track change to G711 from codecid:" << getCodecName(_codecid_audio);
return;
}
_muxer->inputFrame(std::make_shared<G711FrameNoCacheAble>(codec, (char *) data, bytes, dts));
auto frame = std::make_shared<G711FrameNoCacheAble>((char *) data, bytes, dts);
frame->setCodec(codec);
_muxer->inputFrame(frame);
break;
}
default:

View File

@ -263,18 +263,7 @@ public:
string getSdp() const override {
return _printer;
}
/**
*
* @return
*/
TrackType getTrackType() const override {
return TrackTitle;
}
/**
* id
* @return
*/
CodecId getCodecId() const override{
return CodecInvalid;
}
@ -283,5 +272,4 @@ private:
};
} //namespace mediakit
#endif //RTSP_RTSP_H_