mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-01 07:22:23 +08:00
整理文件 规范命名
This commit is contained in:
@@ -1,229 +0,0 @@
|
||||
//
|
||||
// Created by xzl on 2018/8/30.
|
||||
//
|
||||
|
||||
#include "Util/File.h"
|
||||
#include "FlvMuxer.h"
|
||||
#include "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
|
||||
@@ -1,58 +0,0 @@
|
||||
//
|
||||
// Created by xzl on 2018/8/30.
|
||||
//
|
||||
|
||||
#ifndef ZLMEDIAKIT_FLVRECORDER_H
|
||||
#define ZLMEDIAKIT_FLVRECORDER_H
|
||||
|
||||
#include "Rtmp.h"
|
||||
#include "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
|
||||
@@ -34,7 +34,7 @@
|
||||
#include <unordered_map>
|
||||
#include "amf.h"
|
||||
#include "Rtmp.h"
|
||||
#include "RtmpParser.h"
|
||||
#include "RtmpMuxer/RtmpDemuxer.h"
|
||||
#include "Common/config.h"
|
||||
#include "Common/MediaSource.h"
|
||||
#include "Util/util.h"
|
||||
@@ -79,7 +79,7 @@ public:
|
||||
virtual void onGetMetaData(const AMFValue &metadata) {
|
||||
lock_guard<recursive_mutex> lock(_mtxMap);
|
||||
_metadata = metadata;
|
||||
RtmpParser parser(metadata);
|
||||
RtmpDemuxer parser(metadata);
|
||||
_iCfgFrameSize = parser.getTracks().size();
|
||||
if(ready()){
|
||||
MediaSource::regist();
|
||||
|
||||
@@ -1,234 +0,0 @@
|
||||
/*
|
||||
* 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 "RtmpParser.h"
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
RtmpParser::RtmpParser(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);
|
||||
}
|
||||
|
||||
RtmpParser::~RtmpParser() {
|
||||
}
|
||||
|
||||
bool RtmpParser::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 RtmpParser::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 RtmpParser::_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 RtmpParser::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 RtmpParser::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 RtmpParser::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 RtmpParser::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 */
|
||||
@@ -1,113 +0,0 @@
|
||||
/*
|
||||
* 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 "amf.h"
|
||||
#include "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 RtmpParser : public PlayerBase{
|
||||
public:
|
||||
typedef std::shared_ptr<RtmpParser> Ptr;
|
||||
RtmpParser(const AMFValue &val);
|
||||
virtual ~RtmpParser();
|
||||
|
||||
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_ */
|
||||
@@ -31,15 +31,15 @@
|
||||
#include <functional>
|
||||
#include "Common/config.h"
|
||||
#include "RtmpPlayer.h"
|
||||
#include "RtmpParser.h"
|
||||
#include "RtmpMediaSource.h"
|
||||
#include "RtmpMuxer/RtmpDemuxer.h"
|
||||
#include "Poller/Timer.h"
|
||||
#include "Util/TimeTicker.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class RtmpPlayerImp: public PlayerImp<RtmpPlayer,RtmpParser> {
|
||||
class RtmpPlayerImp: public PlayerImp<RtmpPlayer,RtmpDemuxer> {
|
||||
public:
|
||||
typedef std::shared_ptr<RtmpPlayerImp> Ptr;
|
||||
RtmpPlayerImp(){};
|
||||
@@ -65,7 +65,7 @@ private:
|
||||
_pRtmpMediaSrc->onGetMetaData(val);
|
||||
}
|
||||
try {
|
||||
_parser.reset(new RtmpParser(val));
|
||||
_parser.reset(new RtmpDemuxer(val));
|
||||
//todo(xzl) 修复此处
|
||||
// _parser->setOnVideoCB(_onGetVideoCB);
|
||||
// _parser->setOnAudioCB(_onGetAudioCB);
|
||||
|
||||
@@ -34,11 +34,10 @@
|
||||
#include <unordered_map>
|
||||
#include "amf.h"
|
||||
#include "Rtmp.h"
|
||||
#include "RtmpParser.h"
|
||||
#include "RtmpMediaSource.h"
|
||||
#include "RtpCodec/RtpMakerH264.h"
|
||||
#include "RtpCodec/RtpMakerAAC.h"
|
||||
#include "Rtsp/RtpParser.h"
|
||||
#include "RtspMuxer/RtpMakerH264.h"
|
||||
#include "RtspMuxer/RtpMakerAAC.h"
|
||||
#include "RtmpMuxer/RtmpDemuxer.h"
|
||||
#include "Rtsp/RtspMediaSource.h"
|
||||
#include "Util/util.h"
|
||||
#include "Util/logger.h"
|
||||
@@ -61,7 +60,7 @@ public:
|
||||
|
||||
void onGetMetaData(const AMFValue &_metadata) override {
|
||||
try {
|
||||
_pParser.reset(new RtmpParser(_metadata));
|
||||
_pParser.reset(new RtmpDemuxer(_metadata));
|
||||
_pRecorder.reset(new MediaRecorder(getVhost(),getApp(),getId(),_pParser,_bEnableHls,_bEnableMp4));
|
||||
//todo(xzl) 修复此处
|
||||
|
||||
@@ -84,7 +83,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
RtmpParser::Ptr _pParser;
|
||||
RtmpDemuxer::Ptr _pParser;
|
||||
RtspMediaSource::Ptr _pRtspSrc;
|
||||
RtpMaker_AAC::Ptr _pRtpMaker_aac;
|
||||
RtpMaker_H264::Ptr _pRtpMaker_h264;
|
||||
|
||||
Reference in New Issue
Block a user