mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-05 10:58:11 +08:00
整理文件 规范命名
This commit is contained in:
133
src/RtmpMuxer/AACRtmpCodec.cpp
Normal file
133
src/RtmpMuxer/AACRtmpCodec.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/24.
|
||||
//
|
||||
|
||||
#include <Player/Player.h>
|
||||
#include "AACRtmpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
AACRtmpDecoder::AACRtmpDecoder() {
|
||||
_adts = obtainFrame();
|
||||
}
|
||||
|
||||
AACFrame::Ptr AACRtmpDecoder::obtainFrame() {
|
||||
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
||||
auto frame = ResourcePoolHelper<AACFrame>::obtainObj();
|
||||
frame->aac_frame_length = 7;
|
||||
frame->iPrefixSize = 7;
|
||||
return frame;
|
||||
}
|
||||
|
||||
bool AACRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt, bool key_pos) {
|
||||
RtmpCodec::inputRtmp(pkt, false);
|
||||
|
||||
if (pkt->isCfgFrame()) {
|
||||
_aac_cfg = pkt->getAacCfg();
|
||||
return false;
|
||||
}
|
||||
if (!_aac_cfg.empty()) {
|
||||
onGetAAC(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2, pkt->timeStamp);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void AACRtmpDecoder::onGetAAC(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
|
||||
if(iLen + 7 > sizeof(_adts->buffer)){
|
||||
WarnL << "Illegal adts data, exceeding the length limit.";
|
||||
return;
|
||||
}
|
||||
//写adts结构头
|
||||
makeAdtsHeader(_aac_cfg,*_adts);
|
||||
|
||||
//拷贝aac负载
|
||||
memcpy(_adts->buffer + 7, pcData, iLen);
|
||||
_adts->aac_frame_length = 7 + iLen;
|
||||
_adts->timeStamp = ui32TimeStamp;
|
||||
|
||||
//adts结构头转成头7个字节
|
||||
writeAdtsHeader(*_adts, _adts->buffer);
|
||||
|
||||
//写入环形缓存
|
||||
RtmpCodec::inputFrame(_adts);
|
||||
_adts = obtainFrame();
|
||||
}
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
RtmpCodec::inputFrame(frame);
|
||||
if(frame->prefixSize() == 7 && _aac_cfg.empty()){
|
||||
//包含adts头
|
||||
_aac_cfg = makeAdtsConfig(reinterpret_cast<const uint8_t *>(frame->data()));
|
||||
makeAudioConfigPkt();
|
||||
}
|
||||
|
||||
if(!_aac_cfg.empty()){
|
||||
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
|
||||
rtmpPkt->strBuf.clear();
|
||||
|
||||
//////////header
|
||||
uint8_t is_config = false;
|
||||
rtmpPkt->strBuf.push_back(_ui8AudioFlags);
|
||||
rtmpPkt->strBuf.push_back(!is_config);
|
||||
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->stamp();
|
||||
rtmpPkt->typeId = MSG_AUDIO;
|
||||
inputRtmp(rtmpPkt, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void AACRtmpEncoder::makeAudioConfigPkt() {
|
||||
makeAdtsHeader(_aac_cfg,*_adts);
|
||||
int iSampleRate , iChannel , iSampleBit = 16;
|
||||
getAACInfo(*_adts,iSampleRate,iChannel);
|
||||
|
||||
uint8_t flvStereoOrMono = (iChannel > 1);
|
||||
uint8_t flvSampleRate;
|
||||
switch (iSampleRate) {
|
||||
case 48000:
|
||||
case 44100:
|
||||
flvSampleRate = 3;
|
||||
break;
|
||||
case 24000:
|
||||
case 22050:
|
||||
flvSampleRate = 2;
|
||||
break;
|
||||
case 12000:
|
||||
case 11025:
|
||||
flvSampleRate = 1;
|
||||
break;
|
||||
default:
|
||||
flvSampleRate = 0;
|
||||
break;
|
||||
}
|
||||
uint8_t flvSampleBit = iSampleBit == 16;
|
||||
uint8_t flvAudioType = 10; //aac
|
||||
|
||||
_ui8AudioFlags = (flvAudioType << 4) | (flvSampleRate << 2) | (flvSampleBit << 1) | flvStereoOrMono;
|
||||
|
||||
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
|
||||
rtmpPkt->strBuf.clear();
|
||||
|
||||
//////////header
|
||||
uint8_t is_config = true;
|
||||
rtmpPkt->strBuf.push_back(_ui8AudioFlags);
|
||||
rtmpPkt->strBuf.push_back(!is_config);
|
||||
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;
|
||||
inputRtmp(rtmpPkt, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
68
src/RtmpMuxer/AACRtmpCodec.h
Normal file
68
src/RtmpMuxer/AACRtmpCodec.h
Normal file
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/24.
|
||||
//
|
||||
|
||||
#ifndef ZLMEDIAKIT_AACRTMPCODEC_H
|
||||
#define ZLMEDIAKIT_AACRTMPCODEC_H
|
||||
|
||||
#include "RtmpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
/**
|
||||
* aac Rtmp转adts类
|
||||
*/
|
||||
class AACRtmpDecoder : public RtmpCodec , public ResourcePoolHelper<AACFrame> {
|
||||
public:
|
||||
typedef std::shared_ptr<AACRtmpDecoder> Ptr;
|
||||
|
||||
AACRtmpDecoder();
|
||||
~AACRtmpDecoder() {}
|
||||
|
||||
/**
|
||||
* 输入Rtmp并解码
|
||||
* @param Rtmp Rtmp数据包
|
||||
* @param key_pos 此参数内部强制转换为false,请忽略之
|
||||
*/
|
||||
bool inputRtmp(const RtmpPacket::Ptr &Rtmp, bool key_pos = false) override;
|
||||
|
||||
TrackType getTrackType() const override{
|
||||
return TrackAudio;
|
||||
}
|
||||
|
||||
CodecId getCodecId() const override{
|
||||
return CodecAAC;
|
||||
}
|
||||
|
||||
protected:
|
||||
void onGetAAC(const char* pcData, int iLen, uint32_t ui32TimeStamp);
|
||||
AACFrame::Ptr obtainFrame();
|
||||
protected:
|
||||
AACFrame::Ptr _adts;
|
||||
string _aac_cfg;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* aac adts转Rtmp类
|
||||
*/
|
||||
class AACRtmpEncoder : public AACRtmpDecoder , public ResourcePoolHelper<RtmpPacket> {
|
||||
public:
|
||||
typedef std::shared_ptr<AACRtmpEncoder> Ptr;
|
||||
|
||||
AACRtmpEncoder();
|
||||
~AACRtmpEncoder() {}
|
||||
|
||||
/**
|
||||
* 输入aac 数据,必须带dats头
|
||||
* @param frame 带dats头的aac数据
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override;
|
||||
|
||||
private:
|
||||
void makeAudioConfigPkt();
|
||||
uint8_t _ui8AudioFlags;
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
#endif //ZLMEDIAKIT_AACRTMPCODEC_H
|
||||
229
src/RtmpMuxer/FlvMuxer.cpp
Normal file
229
src/RtmpMuxer/FlvMuxer.cpp
Normal file
@@ -0,0 +1,229 @@
|
||||
//
|
||||
// Created by xzl on 2018/8/30.
|
||||
//
|
||||
|
||||
#include "FlvMuxer.h"
|
||||
#include "Util/File.h"
|
||||
#include "Rtmp/utils.h"
|
||||
|
||||
#define FILE_BUF_SIZE (64 * 1024)
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
|
||||
FlvMuxer::FlvMuxer() {
|
||||
}
|
||||
FlvMuxer::~FlvMuxer() {
|
||||
}
|
||||
|
||||
void FlvMuxer::start(const RtmpMediaSource::Ptr &media) {
|
||||
if(!media){
|
||||
throw std::runtime_error("RtmpMediaSource 无效");
|
||||
}
|
||||
|
||||
onWriteFlvHeader(media);
|
||||
|
||||
std::weak_ptr<FlvMuxer> weakSelf = getSharedPtr();
|
||||
_ring_reader = media->getRing()->attach();
|
||||
_ring_reader->setDetachCB([weakSelf](){
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(!strongSelf){
|
||||
return;
|
||||
}
|
||||
strongSelf->onDetach();
|
||||
});
|
||||
_ring_reader->setReadCB([weakSelf](const RtmpPacket::Ptr &pkt){
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(!strongSelf){
|
||||
return;
|
||||
}
|
||||
strongSelf->onWriteRtmp(pkt);
|
||||
});
|
||||
}
|
||||
|
||||
void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &mediaSrc) {
|
||||
_previousTagSize = 0;
|
||||
CLEAR_ARR(_aui32FirstStamp);
|
||||
|
||||
//发送flv文件头
|
||||
char flv_file_header[] = "FLV\x1\x5\x0\x0\x0\x9"; // have audio and have video
|
||||
bool is_have_audio = false,is_have_video = false;
|
||||
|
||||
mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt){
|
||||
if(pkt->typeId == MSG_VIDEO){
|
||||
is_have_video = true;
|
||||
}
|
||||
if(pkt->typeId == MSG_AUDIO){
|
||||
is_have_audio = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (is_have_audio && is_have_video) {
|
||||
flv_file_header[4] = 0x05;
|
||||
} else if (is_have_audio && !is_have_video) {
|
||||
flv_file_header[4] = 0x04;
|
||||
} else if (!is_have_audio && is_have_video) {
|
||||
flv_file_header[4] = 0x01;
|
||||
} else {
|
||||
flv_file_header[4] = 0x00;
|
||||
}
|
||||
|
||||
//flv header
|
||||
onWrite(flv_file_header, sizeof(flv_file_header) - 1);
|
||||
//metadata
|
||||
AMFEncoder invoke;
|
||||
invoke << "onMetaData" << mediaSrc->getMetaData();
|
||||
onWriteFlvTag(MSG_DATA, invoke.data(), 0);
|
||||
//config frame
|
||||
mediaSrc->getConfigFrame([&](const RtmpPacket::Ptr &pkt){
|
||||
onWriteRtmp(pkt);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if defined(_WIN32)
|
||||
#pragma pack(push, 1)
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
class RtmpTagHeader {
|
||||
public:
|
||||
uint8_t type = 0;
|
||||
uint8_t data_size[3] = {0};
|
||||
uint8_t timestamp[3] = {0};
|
||||
uint8_t timestamp_ex = 0;
|
||||
uint8_t streamid[3] = {0}; /* Always 0. */
|
||||
}PACKED;
|
||||
|
||||
#if defined(_WIN32)
|
||||
#pragma pack(pop)
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
class BufferRtmp : public Buffer{
|
||||
public:
|
||||
typedef std::shared_ptr<BufferRtmp> Ptr;
|
||||
BufferRtmp(const RtmpPacket::Ptr & pkt):_rtmp(pkt){}
|
||||
virtual ~BufferRtmp(){}
|
||||
|
||||
char *data() const override {
|
||||
return (char *)_rtmp->strBuf.data();
|
||||
}
|
||||
uint32_t size() const override {
|
||||
return _rtmp->strBuf.size();
|
||||
}
|
||||
private:
|
||||
RtmpPacket::Ptr _rtmp;
|
||||
};
|
||||
|
||||
|
||||
void FlvMuxer::onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t ui32TimeStamp) {
|
||||
auto size = htonl(_previousTagSize);
|
||||
onWrite((char *)&size,4);//onWrite PreviousTagSize
|
||||
RtmpTagHeader header;
|
||||
header.type = pkt->typeId;
|
||||
set_be24(header.data_size, pkt->strBuf.size());
|
||||
header.timestamp_ex = (uint8_t) ((ui32TimeStamp >> 24) & 0xff);
|
||||
set_be24(header.timestamp,ui32TimeStamp & 0xFFFFFF);
|
||||
onWrite((char *)&header, sizeof(header));//onWrite tag header
|
||||
onWrite(std::make_shared<BufferRtmp>(pkt));//onWrite tag data
|
||||
_previousTagSize += (pkt->strBuf.size() + sizeof(header));
|
||||
}
|
||||
|
||||
void FlvMuxer::onWriteFlvTag(uint8_t ui8Type, const std::string &strBuf, uint32_t ui32TimeStamp) {
|
||||
auto size = htonl(_previousTagSize);
|
||||
onWrite((char *)&size,4);//onWrite PreviousTagSize
|
||||
RtmpTagHeader header;
|
||||
header.type = ui8Type;
|
||||
set_be24(header.data_size, strBuf.size());
|
||||
header.timestamp_ex = (uint8_t) ((ui32TimeStamp >> 24) & 0xff);
|
||||
set_be24(header.timestamp,ui32TimeStamp & 0xFFFFFF);
|
||||
onWrite((char *)&header, sizeof(header));//onWrite tag header
|
||||
onWrite(std::make_shared<BufferString>(strBuf));//onWrite tag data
|
||||
_previousTagSize += (strBuf.size() + sizeof(header));
|
||||
}
|
||||
|
||||
void FlvMuxer::onWriteRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
auto modifiedStamp = pkt->timeStamp;
|
||||
auto &firstStamp = _aui32FirstStamp[pkt->typeId % 2];
|
||||
if(!firstStamp){
|
||||
firstStamp = modifiedStamp;
|
||||
}
|
||||
if(modifiedStamp >= firstStamp){
|
||||
//计算时间戳增量
|
||||
modifiedStamp -= firstStamp;
|
||||
}else{
|
||||
//发生回环,重新计算时间戳增量
|
||||
CLEAR_ARR(_aui32FirstStamp);
|
||||
modifiedStamp = 0;
|
||||
}
|
||||
onWriteFlvTag(pkt, modifiedStamp);
|
||||
}
|
||||
|
||||
void FlvMuxer::stop() {
|
||||
if(_ring_reader){
|
||||
_ring_reader.reset();
|
||||
onDetach();
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////FlvRecorder/////////////////////////////////////////////////////
|
||||
void FlvRecorder::startRecord(const string &vhost, const string &app, const string &stream,const string &file_path) {
|
||||
startRecord(dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA,vhost,app,stream,false)),file_path);
|
||||
}
|
||||
|
||||
void FlvRecorder::startRecord(const RtmpMediaSource::Ptr &media, const string &file_path) {
|
||||
stop();
|
||||
lock_guard<recursive_mutex> lck(_file_mtx);
|
||||
//开辟文件写缓存
|
||||
std::shared_ptr<char> fileBuf(new char[FILE_BUF_SIZE],[](char *ptr){
|
||||
if(ptr){
|
||||
delete [] ptr;
|
||||
}
|
||||
});
|
||||
//新建文件
|
||||
_file.reset(File::createfile_file(file_path.data(),"wb"),[fileBuf](FILE *fp){
|
||||
if(fp){
|
||||
fflush(fp);
|
||||
fclose(fp);
|
||||
}
|
||||
});
|
||||
if (!_file){
|
||||
throw std::runtime_error( StrPrinter << "打开文件失败:" << file_path);
|
||||
}
|
||||
|
||||
//设置文件写缓存
|
||||
setvbuf( _file.get(), fileBuf.get(),_IOFBF, FILE_BUF_SIZE);
|
||||
start(media);
|
||||
}
|
||||
|
||||
void FlvRecorder::onWrite(const Buffer::Ptr &data) {
|
||||
lock_guard<recursive_mutex> lck(_file_mtx);
|
||||
if(_file){
|
||||
fwrite(data->data(),data->size(),1,_file.get());
|
||||
}
|
||||
}
|
||||
|
||||
void FlvRecorder::onWrite(const char *data, int len) {
|
||||
lock_guard<recursive_mutex> lck(_file_mtx);
|
||||
if(_file){
|
||||
fwrite(data,len,1,_file.get());
|
||||
}
|
||||
}
|
||||
|
||||
void FlvRecorder::onDetach() {
|
||||
lock_guard<recursive_mutex> lck(_file_mtx);
|
||||
_file.reset();
|
||||
}
|
||||
|
||||
std::shared_ptr<FlvMuxer> FlvRecorder::getSharedPtr() {
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
FlvRecorder::FlvRecorder() {
|
||||
}
|
||||
|
||||
FlvRecorder::~FlvRecorder() {
|
||||
}
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
58
src/RtmpMuxer/FlvMuxer.h
Normal file
58
src/RtmpMuxer/FlvMuxer.h
Normal file
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// Created by xzl on 2018/8/30.
|
||||
//
|
||||
|
||||
#ifndef ZLMEDIAKIT_FLVRECORDER_H
|
||||
#define ZLMEDIAKIT_FLVRECORDER_H
|
||||
|
||||
#include "Rtmp/Rtmp.h"
|
||||
#include "Rtmp/RtmpMediaSource.h"
|
||||
#include "Network/Socket.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class FlvMuxer{
|
||||
public:
|
||||
typedef std::shared_ptr<FlvMuxer> Ptr;
|
||||
FlvMuxer();
|
||||
virtual ~FlvMuxer();
|
||||
void stop();
|
||||
protected:
|
||||
void start(const RtmpMediaSource::Ptr &media);
|
||||
virtual void onWrite(const Buffer::Ptr &data) = 0;
|
||||
virtual void onWrite(const char *data,int len) = 0;
|
||||
virtual void onDetach() = 0;
|
||||
virtual std::shared_ptr<FlvMuxer> getSharedPtr() = 0;
|
||||
private:
|
||||
void onWriteFlvHeader(const RtmpMediaSource::Ptr &media);
|
||||
void onWriteRtmp(const RtmpPacket::Ptr &pkt);
|
||||
void onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t ui32TimeStamp);
|
||||
void onWriteFlvTag(uint8_t ui8Type, const std::string &strBuf, uint32_t ui32TimeStamp);
|
||||
private:
|
||||
RtmpMediaSource::RingType::RingReader::Ptr _ring_reader;
|
||||
uint32_t _aui32FirstStamp[2] = {0};
|
||||
uint32_t _previousTagSize = 0;
|
||||
};
|
||||
|
||||
class FlvRecorder : public FlvMuxer , public std::enable_shared_from_this<FlvRecorder>{
|
||||
public:
|
||||
typedef std::shared_ptr<FlvRecorder> Ptr;
|
||||
FlvRecorder();
|
||||
virtual ~FlvRecorder();
|
||||
void startRecord(const string &vhost,const string &app,const string &stream,const string &file_path);
|
||||
void startRecord(const RtmpMediaSource::Ptr &media,const string &file_path);
|
||||
private:
|
||||
virtual void onWrite(const Buffer::Ptr &data) override ;
|
||||
virtual void onWrite(const char *data,int len) override;
|
||||
virtual void onDetach() override;
|
||||
virtual std::shared_ptr<FlvMuxer> getSharedPtr() override;
|
||||
private:
|
||||
std::shared_ptr<FILE> _file;
|
||||
recursive_mutex _file_mtx;
|
||||
};
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
#endif //ZLMEDIAKIT_FLVRECORDER_H
|
||||
198
src/RtmpMuxer/H264RtmpCodec.cpp
Normal file
198
src/RtmpMuxer/H264RtmpCodec.cpp
Normal file
@@ -0,0 +1,198 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/18.
|
||||
//
|
||||
|
||||
#include "H264RtmpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
H264RtmpDecoder::H264RtmpDecoder() {
|
||||
_h264frame = obtainFrame();
|
||||
}
|
||||
|
||||
H264Frame::Ptr H264RtmpDecoder::obtainFrame() {
|
||||
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
||||
auto frame = obtainObj();
|
||||
frame->buffer.clear();
|
||||
frame->iPrefixSize = 4;
|
||||
return frame;
|
||||
}
|
||||
|
||||
bool H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos) {
|
||||
key_pos = decodeRtmp(rtmp);
|
||||
RtmpCodec::inputRtmp(rtmp, key_pos);
|
||||
return key_pos;
|
||||
}
|
||||
|
||||
bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
if (pkt->isCfgFrame()) {
|
||||
//缓存sps pps,后续插入到I帧之前
|
||||
_sps = pkt->getH264SPS();
|
||||
_pps = pkt->getH264PPS();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_sps.size()) {
|
||||
uint32_t iTotalLen = pkt->strBuf.size();
|
||||
uint32_t iOffset = 5;
|
||||
while(iOffset + 4 < iTotalLen){
|
||||
uint32_t iFrameLen;
|
||||
memcpy(&iFrameLen, pkt->strBuf.data() + iOffset, 4);
|
||||
iFrameLen = ntohl(iFrameLen);
|
||||
iOffset += 4;
|
||||
if(iFrameLen + iOffset > iTotalLen){
|
||||
break;
|
||||
}
|
||||
onGetH264_l(pkt->strBuf.data() + iOffset, iFrameLen, pkt->timeStamp);
|
||||
iOffset += iFrameLen;
|
||||
}
|
||||
}
|
||||
return pkt->isVideoKeyFrame();
|
||||
}
|
||||
|
||||
|
||||
inline void H264RtmpDecoder::onGetH264_l(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
|
||||
switch (pcData[0] & 0x1F) {
|
||||
case 5: {
|
||||
//I frame
|
||||
onGetH264(_sps.data(), _sps.length(), ui32TimeStamp);
|
||||
onGetH264(_pps.data(), _pps.length(), ui32TimeStamp);
|
||||
}
|
||||
case 1: {
|
||||
//I or P or B frame
|
||||
onGetH264(pcData, iLen, ui32TimeStamp);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
//WarnL <<(int)(pcData[0] & 0x1F);
|
||||
break;
|
||||
}
|
||||
}
|
||||
inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
|
||||
_h264frame->type = pcData[0] & 0x1F;
|
||||
_h264frame->timeStamp = ui32TimeStamp;
|
||||
_h264frame->buffer.assign("\x0\x0\x0\x1", 4); //添加264头
|
||||
_h264frame->buffer.append(pcData, iLen);
|
||||
|
||||
//写入环形缓存
|
||||
RtmpCodec::inputFrame(_h264frame);
|
||||
_h264frame = obtainFrame();
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
H264RtmpEncoder::H264RtmpEncoder() {
|
||||
}
|
||||
|
||||
void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
RtmpCodec::inputFrame(frame);
|
||||
|
||||
auto pcData = frame->data() + frame->prefixSize();
|
||||
auto iLen = frame->size() - frame->prefixSize();
|
||||
auto type = ((uint8_t*)pcData)[0] & 0x1F;
|
||||
|
||||
switch (type){
|
||||
case 7:{
|
||||
//sps
|
||||
if(_sps.empty()){
|
||||
_sps = string(pcData,iLen);
|
||||
if(!_pps.empty()){
|
||||
makeVideoConfigPkt();
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 8:{
|
||||
//pps
|
||||
if(_pps.empty()){
|
||||
_pps = string(pcData,iLen);
|
||||
if(!_sps.empty()){
|
||||
makeVideoConfigPkt();
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
case 5:{
|
||||
//I or P or B frame
|
||||
int8_t flags = 7; //h.264
|
||||
bool is_config = false;
|
||||
bool keyFrame = frame->keyFrame();
|
||||
flags |= ((keyFrame ? FLV_KEY_FRAME : FLV_INTER_FRAME) << 4);
|
||||
|
||||
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
|
||||
rtmpPkt->strBuf.clear();
|
||||
|
||||
rtmpPkt->strBuf.push_back(flags);
|
||||
rtmpPkt->strBuf.push_back(!is_config);
|
||||
rtmpPkt->strBuf.append("\x0\x0\x0", 3);
|
||||
auto size = htonl(iLen);
|
||||
rtmpPkt->strBuf.append((char *) &size, 4);
|
||||
rtmpPkt->strBuf.append(pcData, iLen);
|
||||
|
||||
rtmpPkt->bodySize = rtmpPkt->strBuf.size();
|
||||
rtmpPkt->chunkId = CHUNK_VIDEO;
|
||||
rtmpPkt->streamId = STREAM_MEDIA;
|
||||
rtmpPkt->timeStamp = frame->stamp();
|
||||
rtmpPkt->typeId = MSG_VIDEO;
|
||||
RtmpCodec::inputRtmp(rtmpPkt,keyFrame);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void H264RtmpEncoder::makeVideoConfigPkt() {
|
||||
int8_t flags = 7; //h.264
|
||||
flags |= (FLV_KEY_FRAME << 4);
|
||||
bool is_config = true;
|
||||
|
||||
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
|
||||
rtmpPkt->strBuf.clear();
|
||||
|
||||
//////////header
|
||||
rtmpPkt->strBuf.push_back(flags);
|
||||
rtmpPkt->strBuf.push_back(!is_config);
|
||||
rtmpPkt->strBuf.append("\x0\x0\x0", 3);
|
||||
|
||||
////////////sps
|
||||
rtmpPkt->strBuf.push_back(1); // version
|
||||
|
||||
//DebugL<<hexdump(_sps.data(), _sps.size());
|
||||
rtmpPkt->strBuf.push_back(_sps[1]); // profile
|
||||
rtmpPkt->strBuf.push_back(_sps[2]); // compat
|
||||
rtmpPkt->strBuf.push_back(_sps[3]); // level
|
||||
rtmpPkt->strBuf.push_back(0xff); // 6 bits reserved + 2 bits nal size length - 1 (11)
|
||||
rtmpPkt->strBuf.push_back(0xe1); // 3 bits reserved + 5 bits number of sps (00001)
|
||||
uint16_t size = _sps.size();
|
||||
size = htons(size);
|
||||
rtmpPkt->strBuf.append((char *) &size, 2);
|
||||
rtmpPkt->strBuf.append(_sps);
|
||||
|
||||
/////////////pps
|
||||
rtmpPkt->strBuf.push_back(1); // version
|
||||
size = _pps.size();
|
||||
size = htons(size);
|
||||
rtmpPkt->strBuf.append((char *) &size, 2);
|
||||
rtmpPkt->strBuf.append(_pps);
|
||||
|
||||
rtmpPkt->bodySize = rtmpPkt->strBuf.size();
|
||||
rtmpPkt->chunkId = CHUNK_VIDEO;
|
||||
rtmpPkt->streamId = STREAM_MEDIA;
|
||||
rtmpPkt->timeStamp = 0;
|
||||
rtmpPkt->typeId = MSG_VIDEO;
|
||||
RtmpCodec::inputRtmp(rtmpPkt, true);
|
||||
}
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
69
src/RtmpMuxer/H264RtmpCodec.h
Normal file
69
src/RtmpMuxer/H264RtmpCodec.h
Normal file
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/24.
|
||||
//
|
||||
|
||||
#ifndef ZLMEDIAKIT_H264RTMPCODEC_H
|
||||
#define ZLMEDIAKIT_H264RTMPCODEC_H
|
||||
|
||||
#include "RtmpCodec.h"
|
||||
#include "Util/ResourcePool.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
/**
|
||||
* h264 Rtmp解码类
|
||||
*/
|
||||
class H264RtmpDecoder : public RtmpCodec ,public ResourcePoolHelper<H264Frame> {
|
||||
public:
|
||||
typedef std::shared_ptr<H264RtmpDecoder> Ptr;
|
||||
|
||||
H264RtmpDecoder();
|
||||
~H264RtmpDecoder() {}
|
||||
|
||||
/**
|
||||
* 输入264 Rtmp包
|
||||
* @param rtmp Rtmp包
|
||||
* @param key_pos 此参数忽略之
|
||||
*/
|
||||
bool inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos = true) override;
|
||||
|
||||
TrackType getTrackType() const override{
|
||||
return TrackVideo;
|
||||
}
|
||||
|
||||
CodecId getCodecId() const override{
|
||||
return CodecH264;
|
||||
}
|
||||
protected:
|
||||
bool decodeRtmp(const RtmpPacket::Ptr &Rtmp);
|
||||
void onGetH264_l(const char *pcData, int iLen, uint32_t ui32TimeStamp);
|
||||
void onGetH264(const char *pcData, int iLen, uint32_t ui32TimeStamp);
|
||||
H264Frame::Ptr obtainFrame();
|
||||
protected:
|
||||
H264Frame::Ptr _h264frame;
|
||||
string _sps;
|
||||
string _pps;
|
||||
};
|
||||
|
||||
/**
|
||||
* 264 Rtmp打包类
|
||||
*/
|
||||
class H264RtmpEncoder : public H264RtmpDecoder, public ResourcePoolHelper<RtmpPacket> {
|
||||
public:
|
||||
typedef std::shared_ptr<H264RtmpEncoder> Ptr;
|
||||
|
||||
H264RtmpEncoder();
|
||||
~H264RtmpEncoder() {}
|
||||
|
||||
/**
|
||||
* 输入264帧,需要指出的是,必须输入sps pps帧
|
||||
* @param frame 帧数据
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override;
|
||||
private:
|
||||
void makeVideoConfigPkt();
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
#endif //ZLMEDIAKIT_H264RTMPCODEC_H
|
||||
8
src/RtmpMuxer/RtmpCodec.cpp
Normal file
8
src/RtmpMuxer/RtmpCodec.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/24.
|
||||
//
|
||||
|
||||
#include "RtmpCodec.h"
|
||||
namespace mediakit{
|
||||
|
||||
}
|
||||
80
src/RtmpMuxer/RtmpCodec.h
Normal file
80
src/RtmpMuxer/RtmpCodec.h
Normal file
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// Created by xzl on 2018/10/24.
|
||||
//
|
||||
|
||||
#ifndef ZLMEDIAKIT_RTMPCODEC_H
|
||||
#define ZLMEDIAKIT_RTMPCODEC_H
|
||||
|
||||
#include "Rtmp/Rtmp.h"
|
||||
#include "Player/Frame.h"
|
||||
#include "Util/RingBuffer.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
class RtmpRingInterface {
|
||||
public:
|
||||
typedef RingBuffer<RtmpPacket::Ptr> RingType;
|
||||
typedef std::shared_ptr<RtmpRingInterface> Ptr;
|
||||
|
||||
RtmpRingInterface(){}
|
||||
virtual ~RtmpRingInterface(){}
|
||||
|
||||
/**
|
||||
* 获取rtmp环形缓存
|
||||
* @return
|
||||
*/
|
||||
virtual RingType::Ptr getRtmpRing() const = 0;
|
||||
|
||||
/**
|
||||
* 设置rtmp环形缓存
|
||||
* @param ring
|
||||
*/
|
||||
virtual void setRtmpRing(const RingType::Ptr &ring) = 0;
|
||||
|
||||
/**
|
||||
* 输入rtmp包
|
||||
* @param rtmp rtmp包
|
||||
* @param key_pos 是否为关键帧
|
||||
* @return 是否为关键帧
|
||||
*/
|
||||
virtual bool inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos) = 0;
|
||||
};
|
||||
|
||||
class RtmpRing : public RtmpRingInterface {
|
||||
public:
|
||||
typedef std::shared_ptr<RtmpRing> Ptr;
|
||||
|
||||
RtmpRing(){
|
||||
_rtmpRing = std::make_shared<RingType>();
|
||||
}
|
||||
virtual ~RtmpRing(){}
|
||||
|
||||
RingType::Ptr getRtmpRing() const override {
|
||||
return _rtmpRing;
|
||||
}
|
||||
|
||||
void setRtmpRing(const RingType::Ptr &ring) override {
|
||||
_rtmpRing = ring;
|
||||
}
|
||||
|
||||
bool inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos) override{
|
||||
_rtmpRing->write(rtmp,key_pos);
|
||||
return key_pos;
|
||||
}
|
||||
protected:
|
||||
RingType::Ptr _rtmpRing;
|
||||
};
|
||||
|
||||
|
||||
class RtmpCodec : public RtmpRing, public FrameRingInterfaceDelegate , public CodecInfo{
|
||||
public:
|
||||
typedef std::shared_ptr<RtmpCodec> Ptr;
|
||||
RtmpCodec(){}
|
||||
virtual ~RtmpCodec(){}
|
||||
};
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
#endif //ZLMEDIAKIT_RTMPCODEC_H
|
||||
234
src/RtmpMuxer/RtmpDemuxer.cpp
Normal file
234
src/RtmpMuxer/RtmpDemuxer.cpp
Normal file
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "RtmpDemuxer.h"
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
RtmpDemuxer::RtmpDemuxer(const AMFValue &val) {
|
||||
auto videoCodec = val["videocodecid"];
|
||||
auto audioCodec = val["audiocodecid"];
|
||||
|
||||
if (videoCodec.type() == AMF_STRING) {
|
||||
if (videoCodec.as_string() == "avc1") {
|
||||
//h264
|
||||
_iVideoCodecID = H264_CODEC_ID;
|
||||
} else {
|
||||
InfoL << "不支持RTMP视频格式:" << videoCodec.as_string();
|
||||
}
|
||||
}else if (videoCodec.type() != AMF_NULL){
|
||||
_iVideoCodecID = videoCodec.as_integer();
|
||||
if (_iVideoCodecID != H264_CODEC_ID) {
|
||||
InfoL << "不支持RTMP视频格式:" << videoCodec.as_integer();
|
||||
}
|
||||
}
|
||||
|
||||
if (audioCodec.type() == AMF_STRING) {
|
||||
if (audioCodec.as_string() == "mp4a") {
|
||||
//aac
|
||||
_iAudioCodecID = AAC_CODEC_ID;
|
||||
} else {
|
||||
InfoL << "不支持RTMP音频格式:" << audioCodec.as_string();
|
||||
}
|
||||
}else if (audioCodec.type() != AMF_NULL) {
|
||||
_iAudioCodecID = audioCodec.as_integer();
|
||||
if (_iAudioCodecID != AAC_CODEC_ID) {
|
||||
InfoL << "不支持RTMP音频格式:" << audioCodec.as_integer();
|
||||
}
|
||||
}
|
||||
onCheckMedia(val);
|
||||
}
|
||||
|
||||
RtmpDemuxer::~RtmpDemuxer() {
|
||||
}
|
||||
|
||||
bool RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
switch (pkt->typeId) {
|
||||
case MSG_VIDEO:{
|
||||
if(_iVideoCodecID == 0){
|
||||
//未初始化视频
|
||||
_iVideoCodecID = pkt->getMediaType();
|
||||
if(_iVideoCodecID != H264_CODEC_ID){
|
||||
InfoL << "不支持RTMP视频格式:" << _iVideoCodecID;
|
||||
}
|
||||
}
|
||||
if(_iVideoCodecID == H264_CODEC_ID){
|
||||
return inputVideo(pkt);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
case MSG_AUDIO: {
|
||||
if(_iAudioCodecID == 0){
|
||||
//未初始化音频
|
||||
_iAudioCodecID = pkt->getMediaType();
|
||||
if(_iAudioCodecID != AAC_CODEC_ID){
|
||||
InfoL << "不支持RTMP音频格式:" << _iAudioCodecID;
|
||||
}
|
||||
}
|
||||
if (_iAudioCodecID == AAC_CODEC_ID) {
|
||||
return inputAudio(pkt);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool RtmpDemuxer::inputVideo(const RtmpPacket::Ptr &pkt) {
|
||||
if (pkt->isCfgFrame()) {
|
||||
//WarnL << " got h264 cfg";
|
||||
if (_strSPS.size()) {
|
||||
return false;
|
||||
}
|
||||
_strSPS.assign("\x00\x00\x00\x01", 4);
|
||||
_strSPS.append(pkt->getH264SPS());
|
||||
|
||||
_strPPS.assign("\x00\x00\x00\x01", 4);
|
||||
_strPPS.append(pkt->getH264PPS());
|
||||
|
||||
getAVCInfo(pkt->getH264SPS(), _iVideoWidth, _iVideoHeight, _fVideoFps);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_strSPS.size()) {
|
||||
uint32_t iTotalLen = pkt->strBuf.size();
|
||||
uint32_t iOffset = 5;
|
||||
while(iOffset + 4 < iTotalLen){
|
||||
uint32_t iFrameLen;
|
||||
memcpy(&iFrameLen, pkt->strBuf.data() + iOffset, 4);
|
||||
iFrameLen = ntohl(iFrameLen);
|
||||
iOffset += 4;
|
||||
if(iFrameLen + iOffset > iTotalLen){
|
||||
break;
|
||||
}
|
||||
_onGetH264(pkt->strBuf.data() + iOffset, iFrameLen, pkt->timeStamp);
|
||||
iOffset += iFrameLen;
|
||||
}
|
||||
}
|
||||
return pkt->isVideoKeyFrame();
|
||||
}
|
||||
inline void RtmpDemuxer::_onGetH264(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
|
||||
switch (pcData[0] & 0x1F) {
|
||||
case 5: {
|
||||
onGetH264(_strSPS.data() + 4, _strSPS.length() - 4, ui32TimeStamp);
|
||||
onGetH264(_strPPS.data() + 4, _strPPS.length() - 4, ui32TimeStamp);
|
||||
}
|
||||
case 1: {
|
||||
onGetH264(pcData, iLen, ui32TimeStamp);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
//WarnL <<(int)(pcData[0] & 0x1F);
|
||||
break;
|
||||
}
|
||||
}
|
||||
inline void RtmpDemuxer::onGetH264(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
|
||||
_h264frame.type = pcData[0] & 0x1F;
|
||||
_h264frame.timeStamp = ui32TimeStamp;
|
||||
_h264frame.buffer.assign("\x0\x0\x0\x1", 4); //添加264头
|
||||
_h264frame.buffer.append(pcData, iLen);
|
||||
{
|
||||
lock_guard<recursive_mutex> lck(_mtxCB);
|
||||
if (onVideo) {
|
||||
onVideo(_h264frame);
|
||||
}
|
||||
}
|
||||
_h264frame.buffer.clear();
|
||||
}
|
||||
|
||||
inline bool RtmpDemuxer::inputAudio(const RtmpPacket::Ptr &pkt) {
|
||||
if (pkt->isCfgFrame()) {
|
||||
if (_strAudioCfg.size()) {
|
||||
return false;
|
||||
}
|
||||
_strAudioCfg = pkt->getAacCfg();
|
||||
_iSampleBit = pkt->getAudioSampleBit();
|
||||
makeAdtsHeader(_strAudioCfg,_adts);
|
||||
getAACInfo(_adts, _iSampleRate, _iChannel);
|
||||
return false;
|
||||
}
|
||||
if (_strAudioCfg.size()) {
|
||||
onGetAAC(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2, pkt->timeStamp);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
inline void RtmpDemuxer::onGetAAC(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
|
||||
if(iLen + 7 > sizeof(_adts.buffer)){
|
||||
WarnL << "Illegal adts data, exceeding the length limit.";
|
||||
return;
|
||||
}
|
||||
//添加adts头
|
||||
memcpy(_adts.buffer + 7, pcData, iLen);
|
||||
_adts.aac_frame_length = 7 + iLen;
|
||||
_adts.timeStamp = ui32TimeStamp;
|
||||
writeAdtsHeader(_adts, _adts.buffer);
|
||||
{
|
||||
lock_guard<recursive_mutex> lck(_mtxCB);
|
||||
if (onAudio) {
|
||||
onAudio(_adts);
|
||||
}
|
||||
}
|
||||
_adts.aac_frame_length = 7;
|
||||
|
||||
}
|
||||
inline void RtmpDemuxer::onCheckMedia(const AMFValue& obj) {
|
||||
obj.object_for_each([&](const string &key ,const AMFValue& val) {
|
||||
if(key == "duration") {
|
||||
_fDuration = val.as_number();
|
||||
return;
|
||||
}
|
||||
if(key == "width") {
|
||||
_iVideoWidth = val.as_number();
|
||||
return;
|
||||
}
|
||||
if(key == "height") {
|
||||
_iVideoHeight = val.as_number();
|
||||
return;
|
||||
}
|
||||
if(key == "framerate") {
|
||||
_fVideoFps = val.as_number();
|
||||
return;
|
||||
}
|
||||
if(key == "audiosamplerate") {
|
||||
_iSampleRate = val.as_number();
|
||||
return;
|
||||
}
|
||||
if(key == "audiosamplesize") {
|
||||
_iSampleBit = val.as_number();
|
||||
return;
|
||||
}
|
||||
if(key == "stereo") {
|
||||
_iChannel = val.as_boolean() ? 2 :1;
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
} /* namespace mediakit */
|
||||
113
src/RtmpMuxer/RtmpDemuxer.h
Normal file
113
src/RtmpMuxer/RtmpDemuxer.h
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRC_RTMP_RTMPPARSER_H_
|
||||
#define SRC_RTMP_RTMPPARSER_H_
|
||||
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include "Rtmp/amf.h"
|
||||
#include "Rtmp/Rtmp.h"
|
||||
#include "Player/Player.h"
|
||||
#include "Util/TimeTicker.h"
|
||||
#include "Player/PlayerBase.h"
|
||||
using namespace toolkit;
|
||||
|
||||
#define H264_CODEC_ID 7
|
||||
#define AAC_CODEC_ID 10
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class RtmpDemuxer : public PlayerBase{
|
||||
public:
|
||||
typedef std::shared_ptr<RtmpDemuxer> Ptr;
|
||||
RtmpDemuxer(const AMFValue &val);
|
||||
virtual ~RtmpDemuxer();
|
||||
|
||||
bool inputRtmp(const RtmpPacket::Ptr &pkt);
|
||||
|
||||
bool isInited() const override{
|
||||
if((_iAudioCodecID | _iVideoCodecID) == 0){
|
||||
//音视频codec_id都未获取到,说明还未初始化成功
|
||||
return false;
|
||||
}
|
||||
if((_iAudioCodecID & _iVideoCodecID) == 0 && _ticker.elapsedTime() < 300){
|
||||
//音视频codec_id有其一未获取到,且最少分析300ms才能断定没有音频或视频
|
||||
return false;
|
||||
}
|
||||
if (_iAudioCodecID && !_strAudioCfg.size()) {
|
||||
//如果音频是aac但是还未获取aac config ,则未初始化成功
|
||||
return false;
|
||||
}
|
||||
if (_iVideoCodecID && !_strSPS.size()) {
|
||||
//如果视频是h264但是还未获取sps ,则未初始化成功
|
||||
return false;
|
||||
}
|
||||
//初始化成功
|
||||
return true;
|
||||
}
|
||||
float getDuration() const override{
|
||||
return _fDuration;
|
||||
}
|
||||
private:
|
||||
inline void onCheckMedia(const AMFValue &obj);
|
||||
|
||||
//返回值:true 代表是i帧第一个rtp包
|
||||
inline bool inputVideo(const RtmpPacket::Ptr &pkt);
|
||||
inline bool inputAudio(const RtmpPacket::Ptr &pkt);
|
||||
inline void _onGetH264(const char *pcData, int iLen, uint32_t ui32TimeStamp);
|
||||
inline void onGetH264(const char *pcData, int iLen, uint32_t ui32TimeStamp);
|
||||
inline void onGetAAC(const char *pcData, int iLen, uint32_t ui32TimeStamp);
|
||||
//video
|
||||
H264Frame _h264frame;
|
||||
//aduio
|
||||
AACFrame _adts;
|
||||
|
||||
int _iSampleRate = 44100;
|
||||
int _iSampleBit = 16;
|
||||
int _iChannel = 1;
|
||||
|
||||
string _strSPS;
|
||||
string _strPPS;
|
||||
string _strAudioCfg;
|
||||
int _iVideoWidth = 0;
|
||||
int _iVideoHeight = 0;
|
||||
float _fVideoFps = 0;
|
||||
//音视频codec_id初始为0代表尚未获取到
|
||||
int _iAudioCodecID = 0;
|
||||
int _iVideoCodecID = 0;
|
||||
float _fDuration = 0;
|
||||
mutable Ticker _ticker;
|
||||
function<void(const H264Frame &frame)> onVideo;
|
||||
function<void(const AACFrame &frame)> onAudio;
|
||||
recursive_mutex _mtxCB;
|
||||
|
||||
|
||||
};
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
#endif /* SRC_RTMP_RTMPPARSER_H_ */
|
||||
Reference in New Issue
Block a user