mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-13 19:51:49 +08:00
初步提交2.0版本,支持虚拟主机
This commit is contained in:
@@ -38,10 +38,6 @@ HLSMaker::HLSMaker(const string& strM3u8File, const string& strHttpUrl,
|
||||
if (ui32BufSize < 16 * 1024) {
|
||||
ui32BufSize = 16 * 1024;
|
||||
}
|
||||
|
||||
if(ui32Duration < 0){
|
||||
ui32Duration = 0;
|
||||
}
|
||||
if(ui32Num < 1){
|
||||
ui32Num = 1;
|
||||
}
|
||||
|
||||
@@ -36,9 +36,9 @@ namespace ZL {
|
||||
namespace MediaFile {
|
||||
|
||||
#ifdef ENABLE_MP4V2
|
||||
MediaReader::MediaReader(const string &strApp, const string &strId) {
|
||||
MediaReader::MediaReader(const string &strVhost,const string &strApp, const string &strId) {
|
||||
static string recordPath = mINI::Instance()[Config::Record::kFilePath];
|
||||
auto strFileName = recordPath + "/" + strApp + "/" + strId;
|
||||
auto strFileName = recordPath + "/" + strVhost + "/" + strApp + "/" + strId;
|
||||
|
||||
m_hMP4File = MP4Read(strFileName.data());
|
||||
if(m_hMP4File == MP4_INVALID_FILE_HANDLE){
|
||||
@@ -129,7 +129,7 @@ MediaReader::MediaReader(const string &strApp, const string &strId) {
|
||||
}
|
||||
|
||||
m_iDuration = MAX(m_video_ms,m_audio_ms);
|
||||
m_pChn.reset(new DevChannel(strApp.data(),strId.data(),m_iDuration/1000.0,false));
|
||||
m_pChn.reset(new DevChannel(strVhost.data(),strApp.data(),strId.data(),m_iDuration/1000.0,false));
|
||||
if (m_audio_trId != MP4_INVALID_TRACK_ID) {
|
||||
AudioInfo info;
|
||||
info.iChannel = m_audio_num_channels;
|
||||
@@ -169,26 +169,17 @@ MediaReader::~MediaReader() {
|
||||
void MediaReader::startReadMP4() {
|
||||
auto strongSelf = shared_from_this();
|
||||
static uint32_t sampleMS = mINI::Instance()[Config::Record::kSampleMS].as<uint32_t>();
|
||||
|
||||
AsyncTaskThread::Instance().DoTaskDelay(reinterpret_cast<uint64_t>(this), sampleMS, [strongSelf](){
|
||||
return strongSelf->readSample();
|
||||
});
|
||||
weak_ptr<MediaReader> weakSelf = strongSelf;
|
||||
m_pChn->setOnSeek([weakSelf](uint32_t ui32Stamp){
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(!strongSelf){
|
||||
return false;
|
||||
}
|
||||
strongSelf->seek(ui32Stamp);
|
||||
return true;
|
||||
});
|
||||
m_pChn->setOnStamp([weakSelf]() -> uint32_t {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(!strongSelf) {
|
||||
return 0;
|
||||
}
|
||||
return strongSelf-> m_iSeekTime + strongSelf->m_ticker.elapsedTime();
|
||||
});
|
||||
m_pChn->setListener(strongSelf);
|
||||
}
|
||||
bool MediaReader::seekTo(uint32_t ui32Stamp){
|
||||
seek(ui32Stamp);
|
||||
return true;
|
||||
}
|
||||
uint32_t MediaReader::getStamp() {
|
||||
return m_iSeekTime + m_ticker.elapsedTime();
|
||||
}
|
||||
|
||||
bool MediaReader::readSample(int iTimeInc) {
|
||||
@@ -322,36 +313,18 @@ void MediaReader::seek(int iSeekTime,bool bReStart){
|
||||
|
||||
#endif //ENABLE_MP4V2
|
||||
|
||||
RtspMediaSource::Ptr MediaReader::onMakeRtsp(const string &strApp, const string &strId) {
|
||||
#ifdef ENABLE_MP4V2
|
||||
static string appName = mINI::Instance()[Config::Record::kAppName];
|
||||
if (strApp != appName) {
|
||||
return nullptr;
|
||||
}
|
||||
try{
|
||||
MediaReader::Ptr pReader(new MediaReader(strApp,strId));
|
||||
pReader->startReadMP4();
|
||||
return RtspMediaSource::find(strApp, strId, false);
|
||||
}catch (std::exception &ex) {
|
||||
WarnL << ex.what();
|
||||
return nullptr;
|
||||
}
|
||||
#else
|
||||
return nullptr;
|
||||
#endif //ENABLE_MP4V2
|
||||
|
||||
}
|
||||
|
||||
RtmpMediaSource::Ptr MediaReader::onMakeRtmp(const string &strApp, const string &strId) {
|
||||
MediaSource::Ptr MediaReader::onMakeMediaSource(const string &strSchema,const string &strVhost,const string &strApp, const string &strId){
|
||||
#ifdef ENABLE_MP4V2
|
||||
static string appName = mINI::Instance()[Config::Record::kAppName];
|
||||
if (strApp != appName) {
|
||||
return nullptr;
|
||||
}
|
||||
try {
|
||||
MediaReader::Ptr pReader(new MediaReader(strApp, strId));
|
||||
MediaReader::Ptr pReader(new MediaReader(strVhost,strApp, strId));
|
||||
pReader->startReadMP4();
|
||||
return RtmpMediaSource::find(strApp, strId, false);
|
||||
return MediaSource::find(strSchema,strVhost,strApp, strId, false);
|
||||
} catch (std::exception &ex) {
|
||||
WarnL << ex.what();
|
||||
return nullptr;
|
||||
@@ -359,7 +332,6 @@ RtmpMediaSource::Ptr MediaReader::onMakeRtmp(const string &strApp, const string
|
||||
#else
|
||||
return nullptr;
|
||||
#endif //ENABLE_MP4V2
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -42,13 +42,15 @@ using namespace ZL::Rtmp;
|
||||
namespace ZL {
|
||||
namespace MediaFile {
|
||||
|
||||
class MediaReader : public std::enable_shared_from_this<MediaReader>{
|
||||
class MediaReader : public std::enable_shared_from_this<MediaReader> ,public MediaSourceEvent{
|
||||
public:
|
||||
typedef std::shared_ptr<MediaReader> Ptr;
|
||||
MediaReader(const string &strApp, const string &strId);
|
||||
MediaReader(const string &strVhost,const string &strApp, const string &strId);
|
||||
virtual ~MediaReader();
|
||||
static RtspMediaSource::Ptr onMakeRtsp(const string &strApp, const string &strId);
|
||||
static RtmpMediaSource::Ptr onMakeRtmp(const string &strApp, const string &strId);
|
||||
static MediaSource::Ptr onMakeMediaSource(const string &strSchema,const string &strVhost,const string &strApp, const string &strId);
|
||||
public:
|
||||
bool seekTo(uint32_t ui32Stamp) override;
|
||||
uint32_t getStamp() override;
|
||||
private:
|
||||
|
||||
#ifdef ENABLE_MP4V2
|
||||
@@ -95,6 +97,8 @@ private:
|
||||
inline bool readAudioSample(int iTimeInc = 0);
|
||||
inline void writeH264(uint8_t *pucData,int iLen,uint32_t uiStamp);
|
||||
inline void writeAAC(uint8_t *pucData,int iLen,uint32_t uiStamp);
|
||||
|
||||
|
||||
#endif //ENABLE_MP4V2
|
||||
};
|
||||
|
||||
|
||||
@@ -37,24 +37,39 @@ using namespace ZL::Network;
|
||||
namespace ZL {
|
||||
namespace MediaFile {
|
||||
|
||||
MediaRecorder::MediaRecorder(const string &strApp,const string &strId,const std::shared_ptr<PlayerBase> &pPlayer) {
|
||||
#ifdef ENABLE_HLS
|
||||
static string hlsPrefix = mINI::Instance()[Config::Http::kHttpPrefix];
|
||||
MediaRecorder::MediaRecorder(const string &strVhost ,const string &strApp,const string &strId,const std::shared_ptr<PlayerBase> &pPlayer) {
|
||||
static string hlsPrefix = mINI::Instance()[Config::Hls::kHttpPrefix];
|
||||
static string hlsPrefixDefaultVhost = mINI::Instance()[Config::Hls::kHttpPrefixDefaultVhost];
|
||||
static string hlsPath = mINI::Instance()[Config::Hls::kFilePath];
|
||||
static uint32_t hlsBufSize = mINI::Instance()[Config::Hls::kFileBufSize].as<uint32_t>();
|
||||
static uint32_t hlsDuration = mINI::Instance()[Config::Hls::kSegmentDuration].as<uint32_t>();
|
||||
static uint32_t hlsNum = mINI::Instance()[Config::Hls::kSegmentNum].as<uint32_t>();
|
||||
|
||||
m_hlsMaker.reset(new HLSMaker(hlsPath + "/" + strApp + "/" + strId + "/hls.m3u8",
|
||||
hlsPrefix + "/" + strApp + "/" + strId + "/",
|
||||
hlsBufSize,hlsDuration,hlsNum));
|
||||
#endif //ENABLE_HLS
|
||||
|
||||
string hlsPrefixVhost = hlsPrefix;
|
||||
do{
|
||||
//生成hls http前缀
|
||||
if (strVhost.empty() || strVhost == DEFAULT_VHOST) {
|
||||
hlsPrefixVhost = hlsPrefixDefaultVhost;
|
||||
break;
|
||||
}
|
||||
auto pos_start = hlsPrefixVhost.find("${");
|
||||
auto pos_end = hlsPrefixVhost.find("}");
|
||||
if (pos_start != string::npos && pos_end != string::npos && pos_end - pos_start - 2 > 0 ) {
|
||||
auto key = hlsPrefixVhost.substr(pos_start + 2, pos_end - pos_start - 2);
|
||||
trim(key);
|
||||
if (key == VHOST_KEY) {
|
||||
hlsPrefixVhost.replace(pos_start, pos_end - pos_start + 1, strVhost);
|
||||
}
|
||||
}
|
||||
}while(0);
|
||||
m_hlsMaker.reset(new HLSMaker(hlsPath + "/" + strVhost + "/" + strApp + "/" + strId + "/hls.m3u8",
|
||||
hlsPrefixVhost + "/" + strApp + "/" + strId + "/",
|
||||
hlsBufSize,hlsDuration,hlsNum));
|
||||
#ifdef ENABLE_MP4V2
|
||||
static string recordPath = mINI::Instance()[Config::Record::kFilePath];
|
||||
static string recordAppName = mINI::Instance()[Config::Record::kAppName];
|
||||
m_mp4Maker.reset(new Mp4Maker(recordPath + "/" + recordAppName + "/" + strApp + "/" + strId + "/",
|
||||
strApp,strId,pPlayer));
|
||||
m_mp4Maker.reset(new Mp4Maker(recordPath + "/" + strVhost + "/" + recordAppName + "/" + strApp + "/" + strId + "/",
|
||||
strVhost,strApp,strId,pPlayer));
|
||||
#endif //ENABLE_MP4V2
|
||||
}
|
||||
|
||||
@@ -62,20 +77,14 @@ MediaRecorder::~MediaRecorder() {
|
||||
}
|
||||
|
||||
void MediaRecorder::inputH264(void* pData, uint32_t ui32Length, uint32_t ui32TimeStamp, int iType) {
|
||||
#ifdef ENABLE_HLS
|
||||
m_hlsMaker->inputH264(pData, ui32Length, ui32TimeStamp * 90, iType);
|
||||
#endif //ENABLE_HLS
|
||||
|
||||
#ifdef ENABLE_MP4V2
|
||||
m_mp4Maker->inputH264(pData, ui32Length, ui32TimeStamp, iType);
|
||||
#endif //ENABLE_MP4V2
|
||||
}
|
||||
|
||||
void MediaRecorder::inputAAC(void* pData, uint32_t ui32Length, uint32_t ui32TimeStamp) {
|
||||
#ifdef ENABLE_HLS
|
||||
m_hlsMaker->inputAAC(pData, ui32Length, ui32TimeStamp * 90);
|
||||
#endif //ENABLE_HLS
|
||||
|
||||
#ifdef ENABLE_MP4V2
|
||||
m_mp4Maker->inputAAC(pData, ui32Length, ui32TimeStamp);
|
||||
#endif //ENABLE_MP4V2
|
||||
|
||||
@@ -34,9 +34,7 @@
|
||||
#include "Mp4Maker.h"
|
||||
#endif //ENABLE_MP4V2
|
||||
|
||||
#ifdef ENABLE_HLS
|
||||
#include "HLSMaker.h"
|
||||
#endif //ENABLE_HLS
|
||||
|
||||
using namespace std;
|
||||
using namespace ZL::Player;
|
||||
@@ -48,7 +46,7 @@ namespace MediaFile {
|
||||
class MediaRecorder {
|
||||
public:
|
||||
typedef std::shared_ptr<MediaRecorder> Ptr;
|
||||
MediaRecorder(const string &strApp,const string &strId,const std::shared_ptr<PlayerBase> &pPlayer);
|
||||
MediaRecorder(const string &strVhost,const string &strApp,const string &strId,const std::shared_ptr<PlayerBase> &pPlayer);
|
||||
virtual ~MediaRecorder();
|
||||
|
||||
void inputH264( void *pData,
|
||||
@@ -60,11 +58,7 @@ public:
|
||||
uint32_t ui32Length,
|
||||
uint32_t ui32TimeStamp);
|
||||
private:
|
||||
|
||||
#ifdef ENABLE_HLS
|
||||
std::shared_ptr<HLSMaker> m_hlsMaker;
|
||||
#endif //ENABLE_HLS
|
||||
|
||||
#ifdef ENABLE_MP4V2
|
||||
std::shared_ptr<Mp4Maker> m_mp4Maker;
|
||||
#endif //ENABLE_MP4V2
|
||||
|
||||
@@ -59,7 +59,11 @@ string timeStr(const char *fmt) {
|
||||
#endif
|
||||
}
|
||||
|
||||
Mp4Maker::Mp4Maker(const string& strPath,const string &strApp,const string &strStreamId, const PlayerBase::Ptr &pPlayer) {
|
||||
Mp4Maker::Mp4Maker(const string& strPath,
|
||||
const string &strVhost,
|
||||
const string &strApp,
|
||||
const string &strStreamId,
|
||||
const PlayerBase::Ptr &pPlayer) {
|
||||
DebugL << strPath;
|
||||
m_pPlayer = pPlayer;
|
||||
m_strPath = strPath;
|
||||
@@ -67,6 +71,7 @@ Mp4Maker::Mp4Maker(const string& strPath,const string &strApp,const string &strS
|
||||
/////record 业务逻辑//////
|
||||
m_info.strAppName = strApp;
|
||||
m_info.strStreamId = strStreamId;
|
||||
m_info.strVhost = strVhost;
|
||||
m_info.strFolder = strPath;
|
||||
//----record 业务逻辑----//
|
||||
}
|
||||
@@ -144,7 +149,6 @@ void Mp4Maker::createFile() {
|
||||
}
|
||||
closeFile();
|
||||
|
||||
|
||||
auto strDate = timeStr("%Y-%m-%d");
|
||||
auto strTime = timeStr("%H-%M-%S");
|
||||
auto strFileTmp = m_strPath + strDate + "/." + strTime + ".mp4";
|
||||
@@ -155,7 +159,8 @@ void Mp4Maker::createFile() {
|
||||
m_info.strFileName = strTime + ".mp4";
|
||||
m_info.strFilePath = strFile;
|
||||
static string appName = mINI::Instance()[Config::Record::kAppName];
|
||||
m_info.strUrl = appName + "/"
|
||||
m_info.strUrl = m_info.strVhost + "/"
|
||||
+ appName + "/"
|
||||
+ m_info.strAppName + "/"
|
||||
+ m_info.strStreamId + "/"
|
||||
+ strDate + "/"
|
||||
|
||||
@@ -57,11 +57,16 @@ public:
|
||||
string strUrl;//播放路径
|
||||
string strAppName;//应用名称
|
||||
string strStreamId;//流ID
|
||||
string strVhost;//vhost
|
||||
};
|
||||
class Mp4Maker {
|
||||
public:
|
||||
typedef std::shared_ptr<Mp4Maker> Ptr;
|
||||
Mp4Maker(const string &strPath,const string &strApp,const string &strStreamId, const PlayerBase::Ptr &pPlayer);
|
||||
Mp4Maker(const string &strPath,
|
||||
const string &strVhost ,
|
||||
const string &strApp,
|
||||
const string &strStreamId,
|
||||
const PlayerBase::Ptr &pPlayer);
|
||||
virtual ~Mp4Maker();
|
||||
//时间戳:参考频率1000
|
||||
void inputH264(void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp, int iType);
|
||||
|
||||
Reference in New Issue
Block a user