基本完成重构

This commit is contained in:
xiongziliang 2018-10-29 09:54:35 +08:00
parent f899f3503f
commit 05b51585cb
18 changed files with 93 additions and 935 deletions

View File

@ -32,8 +32,6 @@
#include <functional>
#include "Util/util.h"
#include "Player/Player.h"
#include "RtspMuxer/RtpMakerAAC.h"
#include "RtspMuxer/RtpMakerH264.h"
#include "Rtsp/RtspToRtmpMediaSource.h"
#include "RtspMuxer/RtspSdp.h"
#include "Util/TimeTicker.h"

View File

@ -27,7 +27,7 @@
#ifndef SRC_MEDIAFILE_MEDIAREADER_H_
#define SRC_MEDIAFILE_MEDIAREADER_H_
#include "Device/Device.h"
#include "Common/Device.h"
#include "Rtsp/RtspMediaSource.h"
#include "Rtmp/RtmpMediaSource.h"

View File

@ -28,7 +28,7 @@
#define SRC_DEVICE_PLAYERPROXY_H_
#include <memory>
#include "Device.h"
#include "Common/Device.h"
#include "Player/MediaPlayer.h"
#include "Util/TimeTicker.h"
@ -58,7 +58,6 @@ public:
void play(const char* strUrl) override;
bool close() override;
private:
void initMedia();
void rePlay(const string &strUrl,int iFailedCnt);
void onPlaySuccess();
private:

View File

@ -1,161 +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 "Common/config.h"
#include "RtmpToRtspMediaSource.h"
#include "Util/util.h"
#include "Util/base64.h"
#include "Network/sockutil.h"
using namespace toolkit;
namespace mediakit {
RtmpToRtspMediaSource::RtmpToRtspMediaSource(const string &vhost,
const string &app,
const string &id,
bool bEnableHls,
bool bEnableMp4) :
RtmpMediaSource(vhost,app,id),_bEnableHls(bEnableHls),_bEnableMp4(bEnableMp4) {
}
RtmpToRtspMediaSource::~RtmpToRtspMediaSource() {}
void RtmpToRtspMediaSource::onGetH264(const H264Frame &frame) {
if(_pRecorder){
//todo(xzl) 修复此处
// _pRecorder->inputH264((char *) frame.data(), frame.size(), frame.timeStamp, frame.type);
}
if(_pRtpMaker_h264){
_pRtpMaker_h264->makeRtp(frame.data() + 4, frame.size() - 4, frame.timeStamp);
}
}
inline void RtmpToRtspMediaSource::onGetAAC(const AACFrame &frame) {
if(_pRecorder){
//todo(xzl) 修复此处
// _pRecorder->inputAAC((char *) frame.buffer, frame.aac_frame_length, frame.timeStamp);
}
if (_pRtpMaker_aac) {
_pRtpMaker_aac->makeRtp((char *) frame.buffer + 7, frame.aac_frame_length - 7, frame.timeStamp);
}
}
void RtmpToRtspMediaSource::makeSDP() {
string strSDP;
strSDP = "v=0\r\n";
strSDP += "o=- 1383190487994921 1 IN IP4 0.0.0.0\r\n";
strSDP += "s=RTSP Session, streamed by the ZL\r\n";
strSDP += "i=ZL Live Stream\r\n";
strSDP += "c=IN IP4 0.0.0.0\r\n";
strSDP += "t=0 0\r\n";
if(_pParser->getDuration() <= 0){
strSDP += "a=range:npt=0-\r\n";
}else{
strSDP += StrPrinter << "0-"<< _pParser->getDuration()<< "\r\n" << endl;
}
strSDP += "a=control:*\r\n";
//todo(xzl) 修复此处
// if (_pParser->containVideo()) {
// uint32_t ssrc0;
// memcpy(&ssrc0, makeRandStr(4, false).data(), 4);
// auto lam = [this](const RtpPacket::Ptr &pkt, bool bKeyPos) {
// _pRtspSrc->onWrite(pkt,bKeyPos);
// };
//
// GET_CONFIG_AND_REGISTER(uint32_t,videoMtu,Rtp::kVideoMtuSize);
// _pRtpMaker_h264.reset(new RtpMaker_H264(lam, ssrc0,videoMtu));
//
// char strTemp[100];
// int profile_level_id = 0;
// string strSPS =_pParser->getSps().substr(4);
// string strPPS =_pParser->getPps().substr(4);
// if (strSPS.length() >= 4) { // sanity check
// profile_level_id = (strSPS[1] << 16) | (strSPS[2] << 8) | strSPS[3]; // profile_idc|constraint_setN_flag|level_idc
// }
//
// //视频通道
// strSDP += StrPrinter << "m=video 0 RTP/AVP " << _pRtpMaker_h264->getPlayloadType()
// << "\r\n" << endl;
// strSDP += "b=AS:5100\r\n";
// strSDP += StrPrinter << "a=rtpmap:" << _pRtpMaker_h264->getPlayloadType()
// << " H264/" << _pRtpMaker_h264->getSampleRate() << "\r\n" << endl;
// strSDP += StrPrinter << "a=fmtp:" << _pRtpMaker_h264->getPlayloadType()
// << " packetization-mode=1;profile-level-id=" << endl;
//
// memset(strTemp, 0, 100);
// sprintf(strTemp, "%06X", profile_level_id);
// strSDP += strTemp;
// strSDP += ";sprop-parameter-sets=";
// memset(strTemp, 0, 100);
// av_base64_encode(strTemp, 100, (uint8_t *) strSPS.data(), strSPS.size());
// strSDP += strTemp;
// strSDP += ",";
// memset(strTemp, 0, 100);
// av_base64_encode(strTemp, 100, (uint8_t *) strPPS.data(), strPPS.size());
// strSDP += strTemp;
// strSDP += "\r\n";
// strSDP += StrPrinter << "a=control:trackID=" << _pRtpMaker_h264->getInterleaved() / 2
// << "\r\n" << endl;
// }
//
// if (_pParser->containAudio()) {
// uint32_t ssrc1;
// memcpy(&ssrc1, makeRandStr(8, false).data() + 4, 4);
// auto lam = [this](const RtpPacket::Ptr &pkt, bool bKeyPos) {
// _pRtspSrc->onWrite(pkt,bKeyPos);
// };
// GET_CONFIG_AND_REGISTER(uint32_t,audioMtu,Rtp::kAudioMtuSize);
// _pRtpMaker_aac.reset(new RtpMaker_AAC(lam, ssrc1, audioMtu,_pParser->getAudioSampleRate()));
//
// char configStr[32];
// const string & strAacCfg = _pParser->getAudioCfg();
// snprintf(configStr, sizeof(configStr), "%02X%02x", strAacCfg[0], strAacCfg[1]);
// strSDP += StrPrinter << "m=audio 0 RTP/AVP " << _pRtpMaker_aac->getPlayloadType()
// << "\r\n" << endl;
// strSDP += "b=AS:96\r\n";
// strSDP += StrPrinter << "a=rtpmap:" << _pRtpMaker_aac->getPlayloadType()
// << " MPEG4-GENERIC/" << _pRtpMaker_aac->getSampleRate() << "\r\n"
// << endl;
// strSDP += StrPrinter << "a=fmtp:" << _pRtpMaker_aac->getPlayloadType()
// << " streamtype=5;profile-level-id=1;mode=AAC-hbr;"
// << "sizelength=13;indexlength=3;indexdeltalength=3;config="
// << endl;
// strSDP.append(configStr, 4);
// strSDP += "\r\n";
// strSDP += StrPrinter << "a=control:trackID=" << _pRtpMaker_aac->getInterleaved() / 2
// << "\r\n" << endl;
// }
_pRtspSrc.reset(new RtspMediaSource(getVhost(),getApp(),getId()));
_pRtspSrc->setListener(_listener);
_pRtspSrc->onGetSDP(strSDP);
}
} /* namespace mediakit */

View File

@ -32,16 +32,14 @@
#include <memory>
#include <functional>
#include <unordered_map>
#include "Util/util.h"
#include "Util/logger.h"
#include "amf.h"
#include "Rtmp.h"
#include "RtmpMediaSource.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"
#include "MediaFile/MediaRecorder.h"
#include "RtspMuxer/RtspMediaSourceMuxer.h"
using namespace std;
using namespace toolkit;
@ -55,44 +53,39 @@ public:
const string &app,
const string &id,
bool bEnableHls = true,
bool bEnableMp4 = false);
virtual ~RtmpToRtspMediaSource();
bool bEnableMp4 = false):RtmpMediaSource(vhost, app, id){
_recorder = std::make_shared<MediaRecorder>(vhost, app, id, bEnableHls, bEnableMp4);
}
virtual ~RtmpToRtspMediaSource(){}
void onGetMetaData(const AMFValue &_metadata) override {
try {
_pParser.reset(new RtmpDemuxer(_metadata));
_pRecorder.reset(new MediaRecorder(getVhost(),getApp(),getId(),_bEnableHls,_bEnableMp4));
//todo(xzl) 修复此处
// _pParser->setOnAudioCB(std::bind(&RtmpToRtspMediaSource::onGetAAC, this, placeholders::_1));
// _pParser->setOnVideoCB(std::bind(&RtmpToRtspMediaSource::onGetH264, this, placeholders::_1));
} catch (exception &ex) {
WarnL << ex.what();
}
_rtmpDemuxer = std::make_shared<RtmpDemuxer>(_metadata);
RtmpMediaSource::onGetMetaData(_metadata);
}
void onWrite(const RtmpPacket::Ptr &pkt,bool key_pos) override {
if (_pParser) {
if (!_pRtspSrc && _pParser->isInited()) {
makeSDP();
if(_rtmpDemuxer){
_rtmpDemuxer->inputRtmp(pkt);
if(!_rtspMuxer && _rtmpDemuxer->isInited()){
_rtspMuxer = std::make_shared<RtspMediaSourceMuxer>(getVhost(),
getApp(),
getId(),
std::make_shared<TitleSdp>(
_rtmpDemuxer->getDuration()));
for (auto &track : _rtmpDemuxer->getTracks()){
_rtspMuxer->addTrack(track);
_recorder->addTrack(track);
track->addDelegate(_rtspMuxer);
track->addDelegate(_recorder);
}
}
_pParser->inputRtmp(pkt);
}
RtmpMediaSource::onWrite(pkt,key_pos);
}
private:
RtmpDemuxer::Ptr _pParser;
RtspMediaSource::Ptr _pRtspSrc;
RtpMaker_AAC::Ptr _pRtpMaker_aac;
RtpMaker_H264::Ptr _pRtpMaker_h264;
MediaRecorder::Ptr _pRecorder;
bool _bEnableHls;
bool _bEnableMp4;
void onGetH264(const H264Frame &frame);
void onGetAAC(const AACFrame &frame);
void makeSDP();
RtmpDemuxer::Ptr _rtmpDemuxer;
RtspMediaSourceMuxer::Ptr _rtspMuxer;
MediaRecorder::Ptr _recorder;
};
} /* namespace mediakit */

View File

@ -1,222 +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 <string.h>
#include "Common/config.h"
#include "Rtmp/Rtmp.h"
#include "RtspToRtmpMediaSource.h"
#include "Util/util.h"
#include "Network/sockutil.h"
using namespace toolkit;
namespace mediakit {
RtspToRtmpMediaSource::RtspToRtmpMediaSource(const string &vhost,
const string &app,
const string &id,
bool bEnableHls,
bool bEnableMp4) :
RtspMediaSource(vhost,app,id),_bEnableHls(bEnableHls),_bEnableMp4(bEnableMp4) {
}
RtspToRtmpMediaSource::~RtspToRtmpMediaSource() {
}
void RtspToRtmpMediaSource::makeVideoConfigPkt() {
int8_t flags = 7; //h.264
flags |= (FLV_KEY_FRAME << 4);
bool is_config = true;
RtmpPacket::Ptr rtmpPkt(new RtmpPacket);
//////////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
//todo(xzl) 修复此处
string _sps ;//= _pParser->getSps().substr(4);
string _pps ;//= _pParser->getPps().substr(4);
//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;
_pRtmpSrc->onWrite(rtmpPkt);
}
void RtspToRtmpMediaSource::onGetH264(const H264Frame& frame) {
if(_pRecorder){
//todo(xzl) 修复此处
// _pRecorder->inputH264((char *) frame.data(), frame.size(), frame.timeStamp, frame.type);
}
uint8_t nal_type = frame.data()[4] & 0x1F;
int8_t flags = 7; //h.264
bool is_config = false;
switch (nal_type) {
case 7:
case 8:
return;
case 5:
flags |= (FLV_KEY_FRAME << 4);
break;
default:
flags |= (FLV_INTER_FRAME << 4);
break;
}
RtmpPacket::Ptr rtmpPkt(new RtmpPacket);
rtmpPkt->strBuf.push_back(flags);
rtmpPkt->strBuf.push_back(!is_config);
rtmpPkt->strBuf.append("\x0\x0\x0", 3);
uint32_t size = frame.size() - 4;
size = htonl(size);
rtmpPkt->strBuf.append((char *) &size, 4);
rtmpPkt->strBuf.append(&frame.data()[4], frame.size() - 4);
rtmpPkt->bodySize = rtmpPkt->strBuf.size();
rtmpPkt->chunkId = CHUNK_VIDEO;
rtmpPkt->streamId = STREAM_MEDIA;
rtmpPkt->timeStamp = frame.timeStamp;
rtmpPkt->typeId = MSG_VIDEO;
_pRtmpSrc->onWrite(rtmpPkt);
}
void RtspToRtmpMediaSource::onGetAAC(const AACFrame& frame) {
if(_pRecorder){
//todo(xzl) 修复此处
// _pRecorder->inputAAC((char *) frame.buffer, frame.aac_frame_length, frame.timeStamp);
}
RtmpPacket::Ptr rtmpPkt(new RtmpPacket);
//////////header
uint8_t is_config = false;
rtmpPkt->strBuf.push_back(_ui8AudioFlags);
rtmpPkt->strBuf.push_back(!is_config);
rtmpPkt->strBuf.append((char *) frame.buffer + 7, frame.aac_frame_length - 7);
rtmpPkt->bodySize = rtmpPkt->strBuf.size();
rtmpPkt->chunkId = CHUNK_AUDIO;
rtmpPkt->streamId = STREAM_MEDIA;
rtmpPkt->timeStamp = frame.timeStamp;
rtmpPkt->typeId = MSG_AUDIO;
_pRtmpSrc->onWrite(rtmpPkt);
}
void RtspToRtmpMediaSource::makeAudioConfigPkt() {
//todo(xzl) 修复此处
#if 0
uint8_t flvStereoOrMono = (_pParser->getAudioChannel() > 1);
uint8_t flvSampleRate;
switch (_pParser->getAudioSampleRate()) {
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 = _pParser->getAudioSampleBit() == 16;
uint8_t flvAudioType = 10; //aac
_ui8AudioFlags = (flvAudioType << 4) | (flvSampleRate << 2) | (flvSampleBit << 1) | flvStereoOrMono;
RtmpPacket::Ptr rtmpPkt(new RtmpPacket);
//////////header
uint8_t is_config = true;
rtmpPkt->strBuf.push_back(_ui8AudioFlags);
rtmpPkt->strBuf.push_back(!is_config);
rtmpPkt->strBuf.append(_pParser->getAudioCfg());
rtmpPkt->bodySize = rtmpPkt->strBuf.size();
rtmpPkt->chunkId = CHUNK_AUDIO;
rtmpPkt->streamId = STREAM_MEDIA;
rtmpPkt->timeStamp = 0;
rtmpPkt->typeId = MSG_AUDIO;
_pRtmpSrc->onWrite(rtmpPkt);
#endif
}
void RtspToRtmpMediaSource::makeMetaData() {
_pRtmpSrc.reset(new RtmpMediaSource(getVhost(),getApp(),getId()));
_pRtmpSrc->setListener(_listener);
AMFValue metaData(AMF_OBJECT);
metaData.set("duration", _pParser->getDuration());
metaData.set("fileSize", 0);
//todo(xzl) 修复此处
#if 0
if (_pParser->containVideo()) {
metaData.set("width", _pParser->getVideoWidth());
metaData.set("height", _pParser->getVideoHeight());
metaData.set("videocodecid", "avc1"); //h.264
metaData.set("videodatarate", 5000);
metaData.set("framerate", _pParser->getVideoFps());
makeVideoConfigPkt();
}
if (_pParser->containAudio()) {
metaData.set("audiocodecid", "mp4a"); //aac
metaData.set("audiodatarate", 160);
metaData.set("audiosamplerate", _pParser->getAudioSampleRate());
metaData.set("audiosamplesize", _pParser->getAudioSampleBit());
metaData.set("audiochannels", _pParser->getAudioChannel());
metaData.set("stereo", _pParser->getAudioChannel() > 1);
makeAudioConfigPkt();
}
#endif
_pRtmpSrc->onGetMetaData(metaData);
}
} /* namespace mediakit */

View File

@ -1,42 +1,44 @@
/*
* 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.
*/
* 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_RTSP_RTSPTORTMPMEDIASOURCE_H_
#define SRC_RTSP_RTSPTORTMPMEDIASOURCE_H_
#include "RtspMediaSource.h"
#include "Rtmp/amf.h"
#include "RtspMediaSource.h"
#include "MediaFile/MediaRecorder.h"
#include "Rtmp/RtmpMediaSource.h"
#include "RtspMuxer/RtspDemuxer.h"
#include "MediaFile/MediaRecorder.h"
#include "RtmpMuxer/RtmpMediaSourceMuxer.h"
using namespace toolkit;
namespace mediakit {
class RtspToRtmpMediaSource: public RtspMediaSource {
class RtspToRtmpMediaSource : public RtspMediaSource {
public:
typedef std::shared_ptr<RtspToRtmpMediaSource> Ptr;
@ -44,49 +46,40 @@ public:
const string &app,
const string &id,
bool bEnableHls = true,
bool bEnableMp4 = true);
bool bEnableMp4 = true) : RtspMediaSource(vhost, app, id) {
_recorder = std::make_shared<MediaRecorder>(vhost, app, id, bEnableHls, bEnableMp4);
}
virtual ~RtspToRtmpMediaSource();
virtual ~RtspToRtmpMediaSource() {}
virtual void onGetSDP(const string& strSdp) override{
virtual void onGetSDP(const string &strSdp) override {
_rtspDemuxer = std::make_shared<RtspDemuxer>(strSdp);
RtspMediaSource::onGetSDP(strSdp);
try {
_pParser.reset(new RtspDemuxer(_sdpAttr));
_pRecorder.reset(new MediaRecorder(getVhost(),getApp(),getId(),_bEnableHls,_bEnableMp4));
//todo(xzl) 修复此处
// _pParser->setOnAudioCB( std::bind(&RtspToRtmpMediaSource::onGetAAC, this, placeholders::_1));
// _pParser->setOnVideoCB( std::bind(&RtspToRtmpMediaSource::onGetH264, this, placeholders::_1));
makeMetaData();
} catch (exception &ex) {
WarnL << ex.what();
}
}
virtual void onWrite(const RtpPacket::Ptr &pRtppkt, bool bKeyPos) override{
if (_pParser) {
bKeyPos = _pParser->inputRtp(pRtppkt);
}
RtspMediaSource::onWrite(pRtppkt, bKeyPos);
}
int readerCount(){
return getRing()->readerCount() + (_pRtmpSrc ? _pRtmpSrc->getRing()->readerCount() : 0);
virtual void onWrite(const RtpPacket::Ptr &rtp, bool bKeyPos) override {
if (_rtspDemuxer) {
bKeyPos = _rtspDemuxer->inputRtp(rtp);
if (!_rtmpMuxer && _rtspDemuxer->isInited()) {
_rtmpMuxer = std::make_shared<RtmpMediaSourceMuxer>(getVhost(),
getApp(),
getId(),
std::make_shared<TitleMete>(
_rtspDemuxer->getDuration()));
for (auto &track : _rtspDemuxer->getTracks()) {
_rtmpMuxer->addTrack(track);
_recorder->addTrack(track);
track->addDelegate(_rtmpMuxer);
track->addDelegate(_recorder);
}
}
}
RtspMediaSource::onWrite(rtp, bKeyPos);
}
protected:
void onGetH264(const H264Frame &frame);
void onGetAAC(const AACFrame &frame);
private:
void makeVideoConfigPkt();
void makeAudioConfigPkt();
void makeMetaData();
private:
RtspDemuxer::Ptr _pParser;
RtmpMediaSource::Ptr _pRtmpSrc;
uint8_t _ui8AudioFlags = 0;
MediaRecorder::Ptr _pRecorder;
bool _bEnableHls;
bool _bEnableMp4;
RtspDemuxer::Ptr _rtspDemuxer;
RtmpMediaSourceMuxer::Ptr _rtmpMuxer;
MediaRecorder::Ptr _recorder;
};
} /* namespace mediakit */

View File

@ -1,105 +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 RTP_RTPMAKER_H_
#define RTP_RTPMAKER_H_
#include "Rtsp/RtspMediaSource.h"
#include "Rtsp/Rtsp.h"
#include "Util/logger.h"
#include "Util/RingBuffer.h"
#include "Util/TimeTicker.h"
#include "Util/ResourcePool.h"
#include "Thread/ThreadPool.h"
using namespace toolkit;
namespace mediakit{
class RtpMaker {
public:
typedef function<void(const RtpPacket::Ptr &pPkt, bool bKeyPos)> onGetRTP;
typedef std::shared_ptr<RtpMaker> Ptr;
RtpMaker(const onGetRTP &cb, uint32_t ui32Ssrc, int iMtuSize,int iSampleRate,
uint8_t ui8PlayloadType, uint8_t ui8Interleaved) {
callBack = cb;
_ui32Ssrc = ui32Ssrc;
_ui32SampleRate = iSampleRate;
_iMtuSize = iMtuSize;
_ui8PlayloadType = ui8PlayloadType;
_ui8Interleaved = ui8Interleaved;
_pktPool.setSize(64);
}
virtual ~RtpMaker() {
}
virtual void makeRtp(const char *pcData, int iDataLen, uint32_t uiStamp)=0;
int getInterleaved() const {
return _ui8Interleaved;
}
int getPlayloadType() const {
return _ui8PlayloadType;
}
int getSampleRate() const {
return _ui32SampleRate;
}
uint32_t getSsrc() const {
return _ui32Ssrc;
}
uint16_t getSeqence() const {
return _ui16Sequence;
}
uint32_t getTimestamp() const {
return _ui32TimeStamp;
}
protected:
uint32_t _ui32Ssrc;
uint32_t _ui32SampleRate;
int _iMtuSize;
uint8_t _ui8PlayloadType;
uint8_t _ui8Interleaved;
uint16_t _ui16Sequence = 0;
uint32_t _ui32TimeStamp = 0;
virtual void onMakeRtp(const RtpPacket::Ptr &pkt, bool bKeyPos = true) {
callBack(pkt, bKeyPos);
}
inline RtpPacket::Ptr obtainPkt() {
return _pktPool.obtain();
}
private:
RtspMediaSource::PoolType _pktPool;
onGetRTP callBack;
};
} //namespace mediakit
#endif /* RTP_RTPMAKER_H_ */

View File

@ -1,99 +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 "Common/config.h"
#include "RtpMakerAAC.h"
#include "Util/mini.h"
#include "Network/sockutil.h"
using namespace toolkit;
namespace mediakit{
void RtpMaker_AAC::makeRtp(const char *pcData, int iLen, uint32_t uiStamp) {
GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Rtp::kCycleMS);
uiStamp %= cycleMS;
char *ptr = (char *) pcData;
int iSize = iLen;
while (iSize > 0 ) {
if (iSize <= _iMtuSize - 20) {
_aucSectionBuf[0] = 0;
_aucSectionBuf[1] = 16;
_aucSectionBuf[2] = iLen >> 5;
_aucSectionBuf[3] = (iLen & 0x1F) << 3;
memcpy(_aucSectionBuf + 4, ptr, iSize);
makeAACRtp(_aucSectionBuf, iSize + 4, true, uiStamp);
break;
}
_aucSectionBuf[0] = 0;
_aucSectionBuf[1] = 16;
_aucSectionBuf[2] = (iLen) >> 5;
_aucSectionBuf[3] = (iLen & 0x1F) << 3;
memcpy(_aucSectionBuf + 4, ptr, _iMtuSize - 20);
makeAACRtp(_aucSectionBuf, _iMtuSize - 16, false, uiStamp);
ptr += (_iMtuSize - 20);
iSize -= (_iMtuSize - 20);
}
}
inline void RtpMaker_AAC::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp) {
uint16_t u16RtpLen = uiLen + 12;
uint32_t ts = htonl((_ui32SampleRate / 1000) * uiStamp);
uint16_t sq = htons(_ui16Sequence);
uint32_t sc = htonl(_ui32Ssrc);
auto pRtppkt = obtainPkt();
auto &rtppkt = *(pRtppkt.get());
unsigned char *pucRtp = rtppkt.payload;
pucRtp[0] = '$';
pucRtp[1] = _ui8Interleaved;
pucRtp[2] = u16RtpLen >> 8;
pucRtp[3] = u16RtpLen & 0x00FF;
pucRtp[4] = 0x80;
pucRtp[5] = (bMark << 7) | _ui8PlayloadType;
memcpy(&pucRtp[6], &sq, 2);
memcpy(&pucRtp[8], &ts, 4);
//ssrc
memcpy(&pucRtp[12], &sc, 4);
//playload
memcpy(&pucRtp[16], pData, uiLen);
rtppkt.PT = _ui8PlayloadType;
rtppkt.interleaved = _ui8Interleaved;
rtppkt.mark = bMark;
rtppkt.length = uiLen + 16;
rtppkt.sequence = _ui16Sequence;
rtppkt.timeStamp = uiStamp;
rtppkt.ssrc = _ui32Ssrc;
rtppkt.type = TrackAudio;
rtppkt.offset = 16;
onMakeRtp(pRtppkt, false);
_ui16Sequence++;
_ui32TimeStamp = uiStamp;
}
}//namespace mediakit

View File

@ -1,60 +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 RTP_RTPMAKERAAC_H_
#define RTP_RTPMAKERAAC_H_
#include <memory>
#include "RtpMaker.h"
#include "Rtsp/RtspMediaSource.h"
#include "Util/logger.h"
#include "Util/RingBuffer.h"
#include "Util/ResourcePool.h"
using namespace std;
using namespace toolkit;
namespace mediakit{
class RtpMaker_AAC: public RtpMaker {
public:
typedef std::shared_ptr<RtpMaker_AAC> Ptr;
RtpMaker_AAC(const onGetRTP &cb,
uint32_t ui32Ssrc, int iMtuSize , int iSampleRate, uint8_t ui8PlayloadType = 97,
uint8_t ui8Interleaved = TrackAudio * 2) :
RtpMaker(cb, ui32Ssrc, iMtuSize,iSampleRate, ui8PlayloadType, ui8Interleaved) {
}
virtual ~RtpMaker_AAC() {
}
void makeRtp(const char *pcData, int iDataLen, uint32_t uiStamp) override;
private:
inline void makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
unsigned char _aucSectionBuf[1600];
};
}//namespace mediakit
#endif /* RTP_RTPMAKERAAC_H_ */

View File

@ -1,118 +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 "Common/config.h"
#include "RtpMakerH264.h"
#include "Util/mini.h"
#include "Network/sockutil.h"
using namespace toolkit;
namespace mediakit{
void RtpMaker_H264::makeRtp(const char* pcData, int iLen, uint32_t uiStamp) {
GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Rtp::kCycleMS);
uiStamp %= cycleMS;
int iSize = _iMtuSize - 2;
if (iLen > iSize) { //超过MTU
const unsigned char s_e_r_Start = 0x80;
const unsigned char s_e_r_Mid = 0x00;
const unsigned char s_e_r_End = 0x40;
//获取帧头数据1byte
unsigned char naluType = *((unsigned char *) pcData) & 0x1f; //获取NALU的5bit 帧类型
unsigned char nal_ref_idc = *((unsigned char *) pcData) & 0x60; //获取NALU的2bit 帧重要程度 00 可以丢 11不能丢
//nal_ref_idc = 0x60;
//组装FU-A帧头数据 2byte
unsigned char f_nri_type = nal_ref_idc + 28;//F为0 1bit,nri上面获取到2bit,28为FU-A分片类型5bit
unsigned char s_e_r_type = naluType;
bool bFirst = true;
bool mark = false;
int nOffset = 1;
while (!mark) {
if (iLen < nOffset + iSize) { //是否拆分结束
iSize = iLen - nOffset;
mark = true;
s_e_r_type = s_e_r_End + naluType;
} else {
if (bFirst == true) {
s_e_r_type = s_e_r_Start + naluType;
bFirst = false;
} else {
s_e_r_type = s_e_r_Mid + naluType;
}
}
memcpy(aucSectionBuf, &f_nri_type, 1);
memcpy(aucSectionBuf + 1, &s_e_r_type, 1);
memcpy(aucSectionBuf + 2, (unsigned char *) pcData + nOffset, iSize);
nOffset += iSize;
makeH264Rtp(aucSectionBuf, iSize + 2, mark, uiStamp);
}
} else {
makeH264Rtp(pcData, iLen, true, uiStamp);
}
}
inline void RtpMaker_H264::makeH264Rtp(const void* data, unsigned int len, bool mark, uint32_t uiStamp) {
uint16_t ui16RtpLen = len + 12;
uint32_t ts = htonl((_ui32SampleRate / 1000) * uiStamp);
uint16_t sq = htons(_ui16Sequence);
uint32_t sc = htonl(_ui32Ssrc);
auto pRtppkt = obtainPkt();
auto &rtppkt = *(pRtppkt.get());
unsigned char *pucRtp = rtppkt.payload;
pucRtp[0] = '$';
pucRtp[1] = _ui8Interleaved;
pucRtp[2] = ui16RtpLen >> 8;
pucRtp[3] = ui16RtpLen & 0x00FF;
pucRtp[4] = 0x80;
pucRtp[5] = (mark << 7) | _ui8PlayloadType;
memcpy(&pucRtp[6], &sq, 2);
memcpy(&pucRtp[8], &ts, 4);
//ssrc
memcpy(&pucRtp[12], &sc, 4);
//playload
memcpy(&pucRtp[16], data, len);
rtppkt.PT = _ui8PlayloadType;
rtppkt.interleaved = _ui8Interleaved;
rtppkt.mark = mark;
rtppkt.length = len + 16;
rtppkt.sequence = _ui16Sequence;
rtppkt.timeStamp = uiStamp;
rtppkt.ssrc = _ui32Ssrc;
rtppkt.type = TrackVideo;
rtppkt.offset = 16;
uint8_t type = ((uint8_t *) (data))[0] & 0x1F;
onMakeRtp(pRtppkt, type == 5);
_ui16Sequence++;
_ui32TimeStamp = uiStamp;
//InfoL<<timeStamp<<" "<<time<<" "<<sampleRate;
}
}//namespace mediakit

View File

@ -1,60 +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 RTP_RTPMAKERH264_H_
#define RTP_RTPMAKERH264_H_
#include <memory>
#include "RtpMaker.h"
#include "Rtsp/RtspMediaSource.h"
#include "Util/logger.h"
#include "Util/RingBuffer.h"
using namespace std;
using namespace toolkit;
namespace mediakit{
class RtpMaker_H264: public RtpMaker {
public:
typedef std::shared_ptr<RtpMaker_H264> Ptr;
RtpMaker_H264(const onGetRTP &cb, uint32_t ui32Ssrc,int iMtuSize = 1400,int iSampleRate = 90000,
uint8_t ui8PlayloadType = 96, uint8_t ui8Interleaved = TrackVideo * 2) :
RtpMaker(cb, ui32Ssrc, iMtuSize,iSampleRate, ui8PlayloadType, ui8Interleaved) {
}
virtual ~RtpMaker_H264() {
}
void makeRtp(const char *pcData, int iDataLen, uint32_t uiStamp) override;
private:
inline void makeH264Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
unsigned char aucSectionBuf[1600];
};
}//namespace mediakit
#endif /* RTP_RTPMAKERH264_H_ */

View File

@ -32,7 +32,7 @@
#include "Rtsp/UDPServer.h"
#include "Network/sockutil.h"
#include "Poller/EventPoller.h"
#include "Device/PlayerProxy.h"
#include "Player/PlayerProxy.h"
#include "Thread/WorkThreadPool.h"
using namespace std;

View File

@ -29,7 +29,7 @@
#include "Util/logger.h"
#include "Util/NoticeCenter.h"
#include "Poller/EventPoller.h"
#include "Device/PlayerProxy.h"
#include "Player/PlayerProxy.h"
#include "Rtmp/RtmpPusher.h"
#include "Common/config.h"

View File

@ -29,7 +29,7 @@
#include "Util/logger.h"
#include "Util/NoticeCenter.h"
#include "Poller/EventPoller.h"
#include "Device/PlayerProxy.h"
#include "Player/PlayerProxy.h"
#include "Rtmp/RtmpPusher.h"
#include "Common/config.h"
#include "MediaFile/MediaReader.h"

View File

@ -1,4 +1,4 @@
/*
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
@ -47,7 +47,7 @@
#include "Network/TcpServer.h"
#include "Poller/EventPoller.h"
#include "Thread/WorkThreadPool.h"
#include "Device/PlayerProxy.h"
#include "Player/PlayerProxy.h"
using namespace std;
using namespace toolkit;