Commit 46be8a38 by xiongziliang

重写MP4点播

parent f03365ef
media-server @ 9f12aee3
Subproject commit 737b8d852eeb1a36bc77854f327fbbef7cfb81be
Subproject commit 9f12aee385993d14b18a1d773be5eedbbe4fa2c2
......@@ -40,7 +40,7 @@ set(ENABLE_MYSQL false)
set(ENABLE_MP4V2 true)
set(ENABLE_FAAC false)
set(ENABLE_X264 false)
set(ENABLE_MP4RECORD true)
set(ENABLE_MP4 true)
#添加两个静态库
if(ENABLE_HLS)
......@@ -52,9 +52,9 @@ else()
endif()
if(ENABLE_MP4RECORD)
message(STATUS "ENABLE_MP4RECORD defined")
add_definitions(-DENABLE_MP4RECORD)
if(ENABLE_MP4)
message(STATUS "ENABLE_MP4 defined")
add_definitions(-DENABLE_MP4)
list(APPEND LINK_LIB_LIST mov flv)
endif()
......@@ -87,7 +87,7 @@ if(ENABLE_HLS)
endif(WIN32)
endif()
if(ENABLE_MP4RECORD)
if(ENABLE_MP4)
aux_source_directory(${MediaServer_Root}/libmov/include src_mov)
aux_source_directory(${MediaServer_Root}/libmov/source src_mov)
include_directories(${MediaServer_Root}/libmov/include)
......
......@@ -43,10 +43,9 @@ INCLUDE_DIRECTORIES(${MediaKit_Root})
set(ENABLE_HLS true)
set(ENABLE_OPENSSL true)
set(ENABLE_MYSQL false)
set(ENABLE_MP4V2 true)
set(ENABLE_FAAC false)
set(ENABLE_X264 false)
set(ENABLE_MP4RECORD true)
set(ENABLE_MP4 true)
set(ENABLE_RTPPROXY true)
set(LINK_LIB_LIST zlmediakit zltoolkit)
......@@ -70,15 +69,6 @@ if (MYSQL_FOUND AND ENABLE_MYSQL)
list(APPEND LINK_LIB_LIST ${MYSQL_LIBRARIES})
endif ()
#查找MP4V2是否安装
find_package(MP4V2 QUIET)
if (MP4V2_FOUND AND ENABLE_MP4V2)
include_directories(${MP4V2_INCLUDE_DIR})
list(APPEND LINK_LIB_LIST ${MP4V2_LIBRARY})
add_definitions(-DENABLE_MP4V2)
message(STATUS "found library:${MP4V2_LIBRARY},ENABLE_MP4V2 defined")
endif ()
#查找x264是否安装
find_package(X264 QUIET)
if (X264_FOUND AND ENABLE_X264)
......@@ -127,9 +117,9 @@ if(ENABLE_HLS)
endif()
#添加mov、flv库用于MP4录制
if(ENABLE_MP4RECORD)
message(STATUS "ENABLE_MP4RECORD defined")
add_definitions(-DENABLE_MP4RECORD)
if(ENABLE_MP4)
message(STATUS "ENABLE_MP4 defined")
add_definitions(-DENABLE_MP4)
aux_source_directory(${MediaServer_Root}/libmov/include src_mov)
aux_source_directory(${MediaServer_Root}/libmov/source src_mov)
......
......@@ -362,7 +362,7 @@ void installWebHook(){
do_http_hook(hook_stream_not_found,body, nullptr);
});
#ifdef ENABLE_MP4RECORD
#ifdef ENABLE_MP4
//录制mp4文件成功后广播
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastRecordMP4,[](BroadcastRecordMP4Args){
if(!hook_enable || hook_record_mp4.empty()){
......@@ -382,7 +382,7 @@ void installWebHook(){
//执行hook
do_http_hook(hook_record_mp4,body, nullptr);
});
#endif //ENABLE_MP4RECORD
#endif //ENABLE_MP4
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastShellLogin,[](BroadcastShellLoginArgs){
if(!hook_enable || hook_shell_login.empty() || sender.get_peer_ip() == "127.0.0.1"){
......
......@@ -152,12 +152,12 @@ void DevChannel::inputAAC(const char *pcDataWithoutAdts,int iDataLen, uint32_t u
uiStamp = (uint32_t)_aTicker[1].elapsedTime();
}
if(pcAdtsHeader + 7 == pcDataWithoutAdts){
inputFrame(std::make_shared<AACFrameNoCacheAble>((char *)pcDataWithoutAdts - 7,iDataLen + 7,uiStamp,7));
inputFrame(std::make_shared<AACFrameNoCacheAble>((char *)pcDataWithoutAdts - 7,iDataLen + 7,uiStamp,0,7));
} else {
char *dataWithAdts = new char[iDataLen + 7];
memcpy(dataWithAdts,pcAdtsHeader,7);
memcpy(dataWithAdts + 7 , pcDataWithoutAdts , iDataLen);
inputFrame(std::make_shared<AACFrameNoCacheAble>(dataWithAdts,iDataLen + 7,uiStamp,7));
inputFrame(std::make_shared<AACFrameNoCacheAble>(dataWithAdts,iDataLen + 7,uiStamp,0,7));
delete [] dataWithAdts;
}
}
......
......@@ -276,7 +276,7 @@ MediaSource::Ptr MediaSource::find(const string &schema, const string &vhost_tmp
if(!ret && bMake){
//未查找媒体源,则创建一个
ret = MP4Reader::onMakeMediaSource(schema, vhost,app,id);
ret = onMakeMediaSource(schema, vhost,app,id);
}
return ret;
}
......
......@@ -112,7 +112,7 @@ class AACFrameNoCacheAble : public FrameNoCacheAble {
public:
typedef std::shared_ptr<AACFrameNoCacheAble> Ptr;
AACFrameNoCacheAble(char *ptr,uint32_t size,uint32_t dts,int prefixeSize = 7){
AACFrameNoCacheAble(char *ptr,uint32_t size,uint32_t dts,uint32_t pts = 0,int prefixeSize = 7){
_ptr = ptr;
_size = size;
_dts = dts;
......
......@@ -268,7 +268,7 @@ public:
*/
void inputFrame(const Frame::Ptr &frame) override{
int type = H265_TYPE(*((uint8_t *)frame->data() + frame->prefixSize()));
if(type == H265Frame::NAL_VPS){
if(frame->configFrame()){
bool first_frame = true;
splitH264(frame->data() + frame->prefixSize(),
frame->size() - frame->prefixSize(),
......
......@@ -215,7 +215,7 @@ public:
auto iAudioIndex = frame->dts() / MUTE_ADTS_DATA_MS;
if(_iAudioIndex != iAudioIndex){
_iAudioIndex = iAudioIndex;
auto aacFrame = std::make_shared<AACFrameCacheAble>((char *)MUTE_ADTS_DATA, MUTE_ADTS_DATA_LEN, _iAudioIndex * MUTE_ADTS_DATA_MS);
auto aacFrame = std::make_shared<AACFrameCacheAble>((char *)MUTE_ADTS_DATA, MUTE_ADTS_DATA_LEN, _iAudioIndex * MUTE_ADTS_DATA_MS, 0);
FrameDispatcher::inputFrame(aacFrame);
}
}
......
......@@ -24,53 +24,35 @@
* SOFTWARE.
*/
#ifdef ENABLE_MP4RECORD
#ifdef ENABLE_MP4
#include "MP4Muxer.h"
#include "Util/File.h"
#include "Common/config.h"
namespace mediakit{
#if defined(_WIN32) || defined(_WIN64)
#define fseek64 _fseeki64
#define ftell64 _ftelli64
#else
#define fseek64 fseek
#define ftell64 ftell
#endif
MP4Muxer::MP4Muxer(const char *file) {
_file_name = file;
openMP4();
}
void MP4MuxerBase::init(int flags) {
static struct mov_buffer_t s_io = {
[](void* ctx, void* data, uint64_t bytes) {
MP4MuxerBase *thiz = (MP4MuxerBase *)ctx;
return thiz->onRead(data,bytes);
},
[](void* ctx, const void* data, uint64_t bytes){
MP4MuxerBase *thiz = (MP4MuxerBase *)ctx;
return thiz->onWrite(data,bytes);
},
[](void* ctx, uint64_t offset) {
MP4MuxerBase *thiz = (MP4MuxerBase *)ctx;
return thiz->onSeek(offset);
},
[](void* ctx){
MP4MuxerBase *thiz = (MP4MuxerBase *)ctx;
return thiz->onTell();
}
};
_mov_writter.reset(mov_writer_create(&s_io,this,flags),[](mov_writer_t *ptr){
if(ptr){
mov_writer_destroy(ptr);
}
});
MP4Muxer::~MP4Muxer() {
closeMP4();
}
void MP4Muxer::openMP4(){
closeMP4();
openFile(_file_name.data(), "wb+");
_mov_writter = createWriter();
}
void MP4Muxer::closeMP4(){
_mov_writter = nullptr;
closeFile();
}
///////////////////////////////////
void MP4Muxer::resetTracks() {
_codec_to_trackid.clear();
_started = false;
_have_video = false;
openMP4();
}
void MP4Muxer::inputFrame(const Frame::Ptr &frame) {
......@@ -263,70 +245,5 @@ void MP4Muxer::addTrack(const Track::Ptr &track) {
}
}
MP4MuxerFile::MP4MuxerFile(const char *file){
_file_name = file;
openFile(file);
}
void MP4MuxerFile::openFile(const char *file) {
//创建文件
auto fp = File::createfile_file(file,"wb+");
if(!fp){
throw std::runtime_error(string("打开文件失败:") + file);
}
GET_CONFIG(uint32_t,mp4BufSize,Record::kFileBufSize);
//新建文件io缓存
std::shared_ptr<char> file_buf(new char[mp4BufSize],[](char *ptr){
if(ptr){
delete [] ptr;
}
});
if(file_buf){
//设置文件io缓存
setvbuf(fp, file_buf.get(), _IOFBF, mp4BufSize);
}
//创建智能指针
_file.reset(fp,[file_buf](FILE *fp) {
fclose(fp);
});
GET_CONFIG(bool, mp4FastStart, Record::kFastStart);
init(mp4FastStart ? MOV_FLAG_FASTSTART : 0);
}
MP4MuxerFile::~MP4MuxerFile() {
_mov_writter = nullptr;
}
int MP4MuxerFile::onRead(void *data, uint64_t bytes) {
if (bytes == fread(data, 1, bytes, _file.get())){
return 0;
}
return 0 != ferror(_file.get()) ? ferror(_file.get()) : -1 /*EOF*/;
}
int MP4MuxerFile::onWrite(const void *data, uint64_t bytes) {
return bytes == fwrite(data, 1, bytes, _file.get()) ? 0 : ferror(_file.get());
}
int MP4MuxerFile::onSeek(uint64_t offset) {
return fseek64(_file.get(), offset, SEEK_SET);
}
uint64_t MP4MuxerFile::onTell() {
return ftell64(_file.get());
}
void MP4MuxerFile::resetTracks(){
MP4Muxer::resetTracks();
openFile(_file_name.data());
}
}//namespace mediakit
#endif//#ifdef ENABLE_MP4RECORD
#endif//#ifdef ENABLE_MP4
......@@ -27,40 +27,21 @@
#ifndef ZLMEDIAKIT_MP4MUXER_H
#define ZLMEDIAKIT_MP4MUXER_H
#ifdef ENABLE_MP4RECORD
#ifdef ENABLE_MP4
#include "Common/MediaSink.h"
#include "mov-writer.h"
#include "mpeg4-hevc.h"
#include "mpeg4-avc.h"
#include "mpeg4-aac.h"
#include "mov-buffer.h"
#include "mov-format.h"
#include "Extension/AAC.h"
#include "Extension/H264.h"
#include "Extension/H265.h"
#include "Common/Stamp.h"
#include "MP4.h"
namespace mediakit{
class MP4MuxerBase{
class MP4Muxer : public MediaSinkInterface, public MP4File{
public:
MP4MuxerBase() = default;
virtual ~MP4MuxerBase() = default;
protected:
virtual int onRead(void* data, uint64_t bytes) = 0;
virtual int onWrite(const void* data, uint64_t bytes) = 0;
virtual int onSeek( uint64_t offset) = 0;
virtual uint64_t onTell() = 0;
void init(int flags);
protected:
std::shared_ptr<mov_writer_t> _mov_writter;
};
class MP4Muxer : public MediaSinkInterface , public MP4MuxerBase{
public:
MP4Muxer() = default;
~MP4Muxer() override = default;
MP4Muxer(const char *file);
~MP4Muxer() override;
/**
* 添加已经ready状态的track
......@@ -75,6 +56,11 @@ public:
* 重置所有track
*/
void resetTracks() override ;
private:
void openMP4();
void closeMP4();
private:
struct track_info{
int track_id = -1;
......@@ -84,28 +70,10 @@ private:
List<Frame::Ptr> _frameCached;
bool _started = false;
bool _have_video = false;
};
class MP4MuxerFile : public MP4Muxer {
public:
typedef std::shared_ptr<MP4MuxerFile> Ptr;
MP4MuxerFile(const char *file);
~MP4MuxerFile();
void resetTracks() override ;
protected:
int onRead(void* data, uint64_t bytes) override;
int onWrite(const void* data, uint64_t bytes) override;
int onSeek( uint64_t offset) override;
uint64_t onTell() override ;
void openFile(const char *file);
private:
std::shared_ptr<FILE> _file;
MP4File::Writer _mov_writter;
string _file_name;
};
}//namespace mediakit
#endif//#ifdef ENABLE_MP4RECORD
#endif//#ifdef ENABLE_MP4
#endif //ZLMEDIAKIT_MP4MUXER_H
......@@ -26,21 +26,16 @@
#ifndef SRC_MEDIAFILE_MEDIAREADER_H_
#define SRC_MEDIAFILE_MEDIAREADER_H_
#ifdef ENABLE_MP4
#include "MP4Demuxer.h"
#include "Common/MultiMediaSourceMuxer.h"
#include "Extension/AAC.h"
#ifdef ENABLE_MP4V2
#include <mp4v2/mp4v2.h>
#endif //ENABLE_MP4V2
using namespace toolkit;
namespace mediakit {
class MP4Reader : public std::enable_shared_from_this<MP4Reader> ,public MediaSourceEvent{
public:
typedef std::shared_ptr<MP4Reader> Ptr;
virtual ~MP4Reader();
virtual ~MP4Reader() = default;
/**
* 流化一个mp4文件,使之转换成RtspMediaSource和RtmpMediaSource
......@@ -50,82 +45,54 @@ public:
* @param filePath 文件路径,如果为空则根据配置文件和上面参数自动生成,否则使用指定的文件
*/
MP4Reader(const string &strVhost,const string &strApp, const string &strId,const string &filePath = "");
/**
* 开始流化MP4文件,需要指出的是,MP4Reader对象一经过调用startReadMP4方法,它的强引用会自持有,
* 意思是在文件流化结束之前或中断之前,MP4Reader对象是不会被销毁的(不管有没有被外部对象持有)
*/
void startReadMP4();
/**
* 自动生成MP4Reader对象然后查找相关的MediaSource对象
* @param strSchema 协议名
* @param strVhost 虚拟主机
* @param strApp 应用名
* @param strId 流id
* @param filePath 文件路径,如果为空则根据配置文件和上面参数自动生成,否则使用指定的文件
* @param checkApp 是否检查app,防止服务器上文件被乱访问
* @return MediaSource
*/
static MediaSource::Ptr onMakeMediaSource(const string &strSchema,
const string &strVhost,
const string &strApp,
const string &strId,
const string &filePath = "",
bool checkApp = true);
private:
//MediaSourceEvent override
bool seekTo(MediaSource &sender,uint32_t ui32Stamp) override;
bool close(MediaSource &sender,bool force) override;
int totalReaderCount(MediaSource &sender) override;
#ifdef ENABLE_MP4V2
void seek(uint32_t iSeekTime,bool bReStart = true);
inline void setSeekTime(uint32_t iSeekTime);
inline uint32_t getVideoCurrentTime();
inline MP4SampleId getVideoSampleId(int iTimeInc = 0);
inline MP4SampleId getAudioSampleId(int iTimeInc = 0);
bool readSample(int iTimeInc, bool justSeekSyncFrame);
inline bool readVideoSample(int iTimeInc,bool justSeekSyncFrame);
inline bool readAudioSample(int iTimeInc,bool justSeekSyncFrame);
inline void writeH264(uint8_t *pucData,int iLen,uint32_t dts,uint32_t pts);
inline void writeAAC(uint8_t *pucData,int iLen,uint32_t uiStamp);
private:
MP4FileHandle _hMP4File = MP4_INVALID_FILE_HANDLE;
MP4TrackId _video_trId = MP4_INVALID_TRACK_ID;
uint32_t _video_ms = 0;
uint32_t _video_num_samples = 0;
uint32_t _video_sample_max_size = 0;
uint32_t _video_width = 0;
uint32_t _video_height = 0;
uint32_t _video_framerate = 0;
string _strPps;
string _strSps;
bool _bSyncSample = false;
MP4TrackId _audio_trId = MP4_INVALID_TRACK_ID;
uint32_t _audio_ms = 0;
uint32_t _audio_num_samples = 0;
uint32_t _audio_sample_max_size = 0;
uint32_t _audio_sample_rate = 0;
uint32_t _audio_num_channels = 0;
string _strAacCfg;
AACFrame _adts;
int _iDuration = 0;
bool readSample();
uint32_t nextStampForStop();
void setNextStampForStop(uint32_t ui32Stamp);
bool seekTo(uint32_t ui32Stamp);
private:
recursive_mutex _mtx;
MultiMediaSourceMuxer::Ptr _mediaMuxer;
MP4SampleId _video_current = 0;
MP4SampleId _audio_current = 0;
std::shared_ptr<uint8_t> _pcVideoSample;
int _iSeekTime = 0 ;
Ticker _ticker;
uint32_t _seek_to;
Ticker _seek_ticker;
Ticker _alive;
recursive_mutex _mtx;
Timer::Ptr _timer;
EventPoller::Ptr _poller;
#endif //ENABLE_MP4V2
MP4Demuxer::Ptr _demuxer;
bool _have_video = false;
};
} /* namespace mediakit */
#endif //ENABLE_MP4
namespace mediakit {
/**
* 自动生成MP4Reader对象然后查找相关的MediaSource对象
* @param strSchema 协议名
* @param strVhost 虚拟主机
* @param strApp 应用名
* @param strId 流id
* @param filePath 文件路径,如果为空则根据配置文件和上面参数自动生成,否则使用指定的文件
* @param checkApp 是否检查app,防止服务器上文件被乱访问
* @return MediaSource
*/
MediaSource::Ptr onMakeMediaSource(const string &strSchema,
const string &strVhost,
const string &strApp,
const string &strId,
const string &filePath = "",
bool checkApp = true);
} /* namespace mediakit */
#endif /* SRC_MEDIAFILE_MEDIAREADER_H_ */
......@@ -24,7 +24,7 @@
* SOFTWARE.
*/
#ifdef ENABLE_MP4RECORD
#ifdef ENABLE_MP4
#include <ctime>
#include <sys/stat.h>
#include "Common/config.h"
......@@ -69,7 +69,7 @@ void MP4Recorder::createFile() {
+ strTime + ".mp4";
try {
_muxer = std::make_shared<MP4MuxerFile>(strFileTmp.data());
_muxer = std::make_shared<MP4Muxer>(strFileTmp.data());
for(auto &track :_tracks){
//添加track
_muxer->addTrack(track);
......@@ -91,7 +91,7 @@ void MP4Recorder::asyncClose() {
//获取文件录制时间,放在关闭mp4之前是为了忽略关闭mp4执行时间
const_cast<MP4Info&>(info).ui64TimeLen = ::time(NULL) - info.ui64StartedTime;
//关闭mp4非常耗时,所以要放在后台线程执行
const_cast<MP4MuxerFile::Ptr &>(muxer).reset();
const_cast<MP4Muxer::Ptr &>(muxer).reset();
//临时文件名改成正式文件名,防止mp4未完成时被访问
rename(strFileTmp.data(),strFile.data());
//获取文件大小
......@@ -145,4 +145,4 @@ void MP4Recorder::resetTracks() {
} /* namespace mediakit */
#endif //ENABLE_MP4RECORD
#endif //ENABLE_MP4
......@@ -54,7 +54,7 @@ public:
string strVhost;//vhost
};
#ifdef ENABLE_MP4RECORD
#ifdef ENABLE_MP4
class MP4Recorder : public MediaSinkInterface{
public:
typedef std::shared_ptr<MP4Recorder> Ptr;
......@@ -90,11 +90,11 @@ private:
Ticker _createFileTicker;
MP4Info _info;
bool _haveVideo = false;
MP4MuxerFile::Ptr _muxer;
MP4Muxer::Ptr _muxer;
list<Track::Ptr> _tracks;
};
#endif ///ENABLE_MP4RECORD
#endif ///ENABLE_MP4
} /* namespace mediakit */
......
......@@ -364,7 +364,7 @@ std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const st
}
case Recorder::type_mp4: {
#if defined(ENABLE_MP4RECORD)
#if defined(ENABLE_MP4)
return std::make_shared<MP4Recorder>(path, vhost, app, stream_id);
#endif
return nullptr;
......
......@@ -282,7 +282,7 @@ void RtpProcess::onDecode(int stream,int codecid,int flags,int64_t pts,int64_t d
WarnL << "audio track change to AAC from codecid:" << getCodecName(_codecid_audio);
return;
}
_muxer->inputFrame(std::make_shared<AACFrameNoCacheAble>((char *) data, bytes, dts, 7));
_muxer->inputFrame(std::make_shared<AACFrameNoCacheAble>((char *) data, bytes, dts, 0, 7));
break;
}
default:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论