统一成员变量命名风格

This commit is contained in:
xiongziliang
2018-10-24 15:43:52 +08:00
parent 97567ec36d
commit 39baaebc55
74 changed files with 2048 additions and 2048 deletions

View File

@@ -43,35 +43,35 @@ HLSMaker::HLSMaker(const string& strM3u8File,
if(ui32Num < 1){
ui32Num = 1;
}
m_ui32BufSize = ui32BufSize;
m_ui64TsCnt = 0;
m_strM3u8File = strM3u8File;
m_ui32NumSegments = ui32Num;
m_ui32SegmentDuration = ui32Duration;
m_ui32LastStamp = 0;
_ui32BufSize = ui32BufSize;
_ui64TsCnt = 0;
_strM3u8File = strM3u8File;
_ui32NumSegments = ui32Num;
_ui32SegmentDuration = ui32Duration;
_ui32LastStamp = 0;
m_strOutputPrefix = strM3u8File.substr(0, strM3u8File.rfind('.'));
m_strFileName = m_strOutputPrefix.substr(m_strOutputPrefix.rfind('/') + 1);
m_ts.init(m_strOutputPrefix + "-0.ts", m_ui32BufSize);
_strOutputPrefix = strM3u8File.substr(0, strM3u8File.rfind('.'));
_strFileName = _strOutputPrefix.substr(_strOutputPrefix.rfind('/') + 1);
_ts.init(_strOutputPrefix + "-0.ts", _ui32BufSize);
}
HLSMaker::~HLSMaker() {
m_ts.clear();
string strDir = m_strOutputPrefix.substr(0,m_strOutputPrefix.rfind('/'));
_ts.clear();
string strDir = _strOutputPrefix.substr(0,_strOutputPrefix.rfind('/'));
File::delete_file(strDir.data());
}
bool HLSMaker::write_index_file(int iFirstSegment, unsigned int uiLastSegment, int iEnd) {
char acWriteBuf[1024];
std::shared_ptr<FILE> pM3u8File(File::createfile_file(m_strM3u8File.data(), "w"),[](FILE *fp){
std::shared_ptr<FILE> pM3u8File(File::createfile_file(_strM3u8File.data(), "w"),[](FILE *fp){
if(fp){
fflush(fp);
fclose(fp);
}
});
if (!pM3u8File) {
WarnL << "Could not open temporary m3u8 index file (" << m_strM3u8File << "), no index file will be created";
WarnL << "Could not open temporary m3u8 index file (" << _strM3u8File << "), no index file will be created";
return false;
}
if (iFirstSegment < 0) {
@@ -80,13 +80,13 @@ bool HLSMaker::write_index_file(int iFirstSegment, unsigned int uiLastSegment, i
//最少1秒
int maxSegmentDuration = 0;
for (auto dur : m_iDurations) {
for (auto dur : _iDurations) {
dur /=1000;
if(dur > maxSegmentDuration){
maxSegmentDuration = dur;
}
}
if (m_ui32NumSegments) {
if (_ui32NumSegments) {
snprintf(acWriteBuf,
sizeof(acWriteBuf),
"#EXTM3U\r\n"
@@ -114,8 +114,8 @@ bool HLSMaker::write_index_file(int iFirstSegment, unsigned int uiLastSegment, i
snprintf(acWriteBuf,
sizeof(acWriteBuf),
"#EXTINF:%.3f,\r\n%s-%u.ts\r\n",
m_iDurations[i-iFirstSegment]/1000.0,
m_strFileName.c_str(),
_iDurations[i-iFirstSegment]/1000.0,
_strFileName.c_str(),
i);
if (fwrite(acWriteBuf, strlen(acWriteBuf), 1, pM3u8File.get()) != 1) {
WarnL << "Could not write to m3u8 index file, will not continue writing to index file";
@@ -134,36 +134,36 @@ bool HLSMaker::write_index_file(int iFirstSegment, unsigned int uiLastSegment, i
}
void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp, int type) {
if(m_ui32LastStamp == 0){
m_ui32LastStamp = timeStamp;
if(_ui32LastStamp == 0){
_ui32LastStamp = timeStamp;
}
int stampInc = timeStamp - m_ui32LastStamp;
int stampInc = timeStamp - _ui32LastStamp;
switch (type) {
case 7: //SPS
if (stampInc >= m_ui32SegmentDuration * 1000) {
m_ui32LastStamp = timeStamp;
if (stampInc >= _ui32SegmentDuration * 1000) {
_ui32LastStamp = timeStamp;
//关闭文件
m_ts.clear();
auto strTmpFileName = StrPrinter << m_strOutputPrefix << '-' << (++m_ui64TsCnt) << ".ts" << endl;
if (!m_ts.init(strTmpFileName, m_ui32BufSize)) {
_ts.clear();
auto strTmpFileName = StrPrinter << _strOutputPrefix << '-' << (++_ui64TsCnt) << ".ts" << endl;
if (!_ts.init(strTmpFileName, _ui32BufSize)) {
//创建文件失败
return;
}
//记录切片时间
m_iDurations.push_back(stampInc);
_iDurations.push_back(stampInc);
if(removets()){
//删除老的时间戳
m_iDurations.pop_front();
_iDurations.pop_front();
}
write_index_file(m_ui64TsCnt - m_ui32NumSegments, m_ui64TsCnt, 0);
write_index_file(_ui64TsCnt - _ui32NumSegments, _ui64TsCnt, 0);
}
case 1: //P
//insert aud frame before p and SPS frame
m_ts.inputH264("\x0\x0\x0\x1\x9\xf0", 6, timeStamp * 90);
_ts.inputH264("\x0\x0\x0\x1\x9\xf0", 6, timeStamp * 90);
case 5: //IDR
case 8: //PPS
m_ts.inputH264((char *) data, length, timeStamp * 90);
_ts.inputH264((char *) data, length, timeStamp * 90);
break;
default:
break;
@@ -171,15 +171,15 @@ void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp, int ty
}
void HLSMaker::inputAAC(void *data, uint32_t length, uint32_t timeStamp) {
m_ts.inputAAC((char *) data, length, timeStamp * 90);
_ts.inputAAC((char *) data, length, timeStamp * 90);
}
bool HLSMaker::removets() {
if (m_ui64TsCnt < m_ui32NumSegments + 2) {
if (_ui64TsCnt < _ui32NumSegments + 2) {
return false;
}
File::delete_file((StrPrinter << m_strOutputPrefix << "-"
<< m_ui64TsCnt - m_ui32NumSegments - 2
File::delete_file((StrPrinter << _strOutputPrefix << "-"
<< _ui64TsCnt - _ui32NumSegments - 2
<< ".ts" << endl).data());
return true;
}

View File

@@ -60,16 +60,16 @@ public:
uint32_t ui32Length,
uint32_t ui32TimeStamp);
private:
TSMaker m_ts;
string m_strM3u8File;
string m_strFileName;
string m_strOutputPrefix;
uint32_t m_ui32SegmentDuration;
uint32_t m_ui32NumSegments;
uint64_t m_ui64TsCnt;
uint32_t m_ui32BufSize;
uint32_t m_ui32LastStamp;
std::deque<int> m_iDurations;
TSMaker _ts;
string _strM3u8File;
string _strFileName;
string _strOutputPrefix;
uint32_t _ui32SegmentDuration;
uint32_t _ui32NumSegments;
uint64_t _ui64TsCnt;
uint32_t _ui32BufSize;
uint32_t _ui32LastStamp;
std::deque<int> _iDurations;
bool write_index_file(int iFirstSegment, unsigned int uiLastSegment, int iEnd);
bool removets();

View File

@@ -41,21 +41,21 @@ MediaReader::MediaReader(const string &strVhost,const string &strApp, const stri
auto strFileName = recordPath + "/" + strVhost + "/" + strApp + "/" + strId;
m_hMP4File = MP4Read(strFileName.data());
if(m_hMP4File == MP4_INVALID_FILE_HANDLE){
_hMP4File = MP4Read(strFileName.data());
if(_hMP4File == MP4_INVALID_FILE_HANDLE){
throw runtime_error(StrPrinter << "打开MP4文件失败:" << strFileName << endl);
}
m_video_trId = MP4FindTrackId(m_hMP4File, 0, MP4_VIDEO_TRACK_TYPE, 0);
if(m_video_trId != MP4_INVALID_TRACK_ID){
if(strcmp(MP4GetTrackMediaDataName(m_hMP4File, m_video_trId),"avc1") ==0){
auto m_video_timescale = MP4GetTrackTimeScale(m_hMP4File, m_video_trId);
auto m_video_duration = MP4GetTrackDuration(m_hMP4File, m_video_trId);
m_video_num_samples = MP4GetTrackNumberOfSamples(m_hMP4File, m_video_trId);
m_video_sample_max_size = MP4GetTrackMaxSampleSize(m_hMP4File, m_video_trId);
m_video_width = MP4GetTrackVideoWidth(m_hMP4File, m_video_trId);
m_video_height = MP4GetTrackVideoHeight(m_hMP4File, m_video_trId);
m_video_framerate = MP4GetTrackVideoFrameRate(m_hMP4File, m_video_trId);
m_pcVideoSample = std::shared_ptr<uint8_t> (new uint8_t[m_video_sample_max_size],[](uint8_t *ptr){
_video_trId = MP4FindTrackId(_hMP4File, 0, MP4_VIDEO_TRACK_TYPE, 0);
if(_video_trId != MP4_INVALID_TRACK_ID){
if(strcmp(MP4GetTrackMediaDataName(_hMP4File, _video_trId),"avc1") ==0){
auto _video_timescale = MP4GetTrackTimeScale(_hMP4File, _video_trId);
auto _video_duration = MP4GetTrackDuration(_hMP4File, _video_trId);
_video_num_samples = MP4GetTrackNumberOfSamples(_hMP4File, _video_trId);
_video_sample_max_size = MP4GetTrackMaxSampleSize(_hMP4File, _video_trId);
_video_width = MP4GetTrackVideoWidth(_hMP4File, _video_trId);
_video_height = MP4GetTrackVideoHeight(_hMP4File, _video_trId);
_video_framerate = MP4GetTrackVideoFrameRate(_hMP4File, _video_trId);
_pcVideoSample = std::shared_ptr<uint8_t> (new uint8_t[_video_sample_max_size],[](uint8_t *ptr){
delete [] ptr;
});
uint8_t **seqheader;
@@ -63,106 +63,106 @@ MediaReader::MediaReader(const string &strVhost,const string &strApp, const stri
uint32_t *pictheadersize;
uint32_t *seqheadersize;
uint32_t ix;
if(MP4GetTrackH264SeqPictHeaders(m_hMP4File, m_video_trId, &seqheader, &seqheadersize, &pictheader, &pictheadersize)){
if(MP4GetTrackH264SeqPictHeaders(_hMP4File, _video_trId, &seqheader, &seqheadersize, &pictheader, &pictheadersize)){
for (ix = 0; seqheadersize[ix] != 0; ix++) {
m_strSps.assign((char *)(seqheader[ix]), seqheadersize[ix]);
_strSps.assign((char *)(seqheader[ix]), seqheadersize[ix]);
float framerate;
getAVCInfo(m_strSps, (int &)m_video_width, (int &)m_video_height, framerate);
m_video_framerate = framerate;
m_strSps = string("\x0\x0\x0\x1",4) + m_strSps;
getAVCInfo(_strSps, (int &)_video_width, (int &)_video_height, framerate);
_video_framerate = framerate;
_strSps = string("\x0\x0\x0\x1",4) + _strSps;
MP4Free(seqheader[ix]);
}
MP4Free(seqheader);
MP4Free(seqheadersize);
for (ix = 0; pictheadersize[ix] != 0; ix++) {
m_strPps.assign("\x0\x0\x0\x1",4);
m_strPps.append((char *)(pictheader[ix]), pictheadersize[ix]);
_strPps.assign("\x0\x0\x0\x1",4);
_strPps.append((char *)(pictheader[ix]), pictheadersize[ix]);
MP4Free(pictheader[ix]);
}
MP4Free(pictheader);
MP4Free(pictheadersize);
}
m_video_ms = 1000.0 * m_video_duration / m_video_timescale;
_video_ms = 1000.0 * _video_duration / _video_timescale;
/*InfoL << "\r\n"
<< m_video_ms << "\r\n"
<< m_video_num_samples << "\r\n"
<< m_video_framerate << "\r\n"
<< m_video_width << "\r\n"
<< m_video_height << "\r\n";*/
<< _video_ms << "\r\n"
<< _video_num_samples << "\r\n"
<< _video_framerate << "\r\n"
<< _video_width << "\r\n"
<< _video_height << "\r\n";*/
} else {
//如果不是h264则忽略
m_video_trId = MP4_INVALID_TRACK_ID;
_video_trId = MP4_INVALID_TRACK_ID;
}
}
m_audio_trId = MP4FindTrackId(m_hMP4File, 0, MP4_AUDIO_TRACK_TYPE, 0);
if (m_audio_trId != MP4_INVALID_TRACK_ID) {
if (strcmp(MP4GetTrackMediaDataName(m_hMP4File, m_audio_trId), "mp4a") == 0) {
m_audio_sample_rate = MP4GetTrackTimeScale(m_hMP4File, m_audio_trId);
auto m_audio_duration = MP4GetTrackDuration(m_hMP4File, m_audio_trId);
m_audio_num_samples = MP4GetTrackNumberOfSamples(m_hMP4File,m_audio_trId);
m_audio_num_channels = MP4GetTrackAudioChannels(m_hMP4File, m_audio_trId);
m_audio_sample_max_size = MP4GetTrackMaxSampleSize(m_hMP4File,m_audio_trId);
_audio_trId = MP4FindTrackId(_hMP4File, 0, MP4_AUDIO_TRACK_TYPE, 0);
if (_audio_trId != MP4_INVALID_TRACK_ID) {
if (strcmp(MP4GetTrackMediaDataName(_hMP4File, _audio_trId), "mp4a") == 0) {
_audio_sample_rate = MP4GetTrackTimeScale(_hMP4File, _audio_trId);
auto _audio_duration = MP4GetTrackDuration(_hMP4File, _audio_trId);
_audio_num_samples = MP4GetTrackNumberOfSamples(_hMP4File,_audio_trId);
_audio_num_channels = MP4GetTrackAudioChannels(_hMP4File, _audio_trId);
_audio_sample_max_size = MP4GetTrackMaxSampleSize(_hMP4File,_audio_trId);
uint8_t *ppConfig;
uint32_t pConfigSize;
if(MP4GetTrackESConfiguration(m_hMP4File,m_audio_trId,&ppConfig,&pConfigSize)){
m_strAacCfg.assign((char *)ppConfig, pConfigSize);
makeAdtsHeader(m_strAacCfg, m_adts);
writeAdtsHeader(m_adts,m_adts.buffer);
getAACInfo(m_adts, (int &)m_audio_sample_rate, (int &)m_audio_num_channels);
if(MP4GetTrackESConfiguration(_hMP4File,_audio_trId,&ppConfig,&pConfigSize)){
_strAacCfg.assign((char *)ppConfig, pConfigSize);
makeAdtsHeader(_strAacCfg, _adts);
writeAdtsHeader(_adts,_adts.buffer);
getAACInfo(_adts, (int &)_audio_sample_rate, (int &)_audio_num_channels);
MP4Free(ppConfig);
}
m_audio_ms = 1000.0 * m_audio_duration / m_audio_sample_rate;
_audio_ms = 1000.0 * _audio_duration / _audio_sample_rate;
/*InfoL << "\r\n"
<< m_audio_ms << "\r\n"
<< m_audio_num_samples << "\r\n"
<< m_audio_num_channels << "\r\n"
<< m_audio_sample_rate << "\r\n";*/
<< _audio_ms << "\r\n"
<< _audio_num_samples << "\r\n"
<< _audio_num_channels << "\r\n"
<< _audio_sample_rate << "\r\n";*/
}else{
m_audio_trId = MP4_INVALID_TRACK_ID;
_audio_trId = MP4_INVALID_TRACK_ID;
}
}
if(m_audio_trId == MP4_INVALID_TRACK_ID && m_video_trId == MP4_INVALID_TRACK_ID){
MP4Close(m_hMP4File);
m_hMP4File = MP4_INVALID_FILE_HANDLE;
if(_audio_trId == MP4_INVALID_TRACK_ID && _video_trId == MP4_INVALID_TRACK_ID){
MP4Close(_hMP4File);
_hMP4File = MP4_INVALID_FILE_HANDLE;
throw runtime_error(StrPrinter << "该MP4文件音视频格式不支持:" << strFileName << endl);
}
m_iDuration = MAX(m_video_ms,m_audio_ms);
m_pChn.reset(new DevChannel(strVhost.data(),strApp.data(),strId.data(),m_iDuration/1000.0,false, false));
if (m_audio_trId != MP4_INVALID_TRACK_ID) {
_iDuration = MAX(_video_ms,_audio_ms);
_pChn.reset(new DevChannel(strVhost.data(),strApp.data(),strId.data(),_iDuration/1000.0,false, false));
if (_audio_trId != MP4_INVALID_TRACK_ID) {
AudioInfo info;
info.iChannel = m_audio_num_channels;
info.iChannel = _audio_num_channels;
info.iSampleBit = 16;
info.iSampleRate = m_audio_sample_rate;
m_pChn->initAudio(info);
info.iSampleRate = _audio_sample_rate;
_pChn->initAudio(info);
}
if (m_video_trId != MP4_INVALID_TRACK_ID) {
if (_video_trId != MP4_INVALID_TRACK_ID) {
VideoInfo info;
info.iFrameRate = m_video_framerate;
info.iWidth = m_video_width;
info.iHeight = m_video_height;
m_pChn->initVideo(info);
info.iFrameRate = _video_framerate;
info.iWidth = _video_width;
info.iHeight = _video_height;
_pChn->initVideo(info);
}
if (m_audio_trId != MP4_INVALID_TRACK_ID) {
m_pChn->inputAAC((char *)m_adts.buffer, 7, 0);
if (_audio_trId != MP4_INVALID_TRACK_ID) {
_pChn->inputAAC((char *)_adts.buffer, 7, 0);
}
if (m_video_trId != MP4_INVALID_TRACK_ID) {
//m_pChn->initVideo(info);
m_pChn->inputH264((char *) m_strSps.data(), m_strSps.size(), 0);
m_pChn->inputH264((char *) m_strPps.data(), m_strPps.size(), 0);
if (_video_trId != MP4_INVALID_TRACK_ID) {
//_pChn->initVideo(info);
_pChn->inputH264((char *) _strSps.data(), _strSps.size(), 0);
_pChn->inputH264((char *) _strPps.data(), _strPps.size(), 0);
}
}
MediaReader::~MediaReader() {
if (m_hMP4File != MP4_INVALID_FILE_HANDLE) {
MP4Close(m_hMP4File);
m_hMP4File = MP4_INVALID_FILE_HANDLE;
if (_hMP4File != MP4_INVALID_FILE_HANDLE) {
MP4Close(_hMP4File);
_hMP4File = MP4_INVALID_FILE_HANDLE;
}
}
@@ -174,14 +174,14 @@ void MediaReader::startReadMP4() {
AsyncTaskThread::Instance().DoTaskDelay(reinterpret_cast<uint64_t>(this), sampleMS, [strongSelf](){
return strongSelf->readSample();
});
m_pChn->setListener(strongSelf);
_pChn->setListener(strongSelf);
}
bool MediaReader::seekTo(uint32_t ui32Stamp){
seek(ui32Stamp);
return true;
}
uint32_t MediaReader::getStamp() {
return m_iSeekTime + m_ticker.elapsedTime();
return _iSeekTime + _ticker.elapsedTime();
}
bool MediaReader::shutDown(){
AsyncTaskThread::Instance().CancelTask(reinterpret_cast<uint64_t>(this));
@@ -190,25 +190,25 @@ bool MediaReader::shutDown(){
bool MediaReader::readSample(int iTimeInc) {
TimeTicker();
lock_guard<recursive_mutex> lck(m_mtx);
lock_guard<recursive_mutex> lck(_mtx);
auto bFlag0 = readVideoSample(iTimeInc);//数据没读完
auto bFlag1 = readAudioSample(iTimeInc);//数据没读完
auto bFlag2 = m_pChn->readerCount() > 0;//读取者大于0
auto bFlag2 = _pChn->readerCount() > 0;//读取者大于0
if((bFlag0 || bFlag1) && bFlag2){
m_alive.resetTime();
_alive.resetTime();
}
//DebugL << "alive ...";
//3秒延时关闭
return m_alive.elapsedTime() < 3 * 1000;
return _alive.elapsedTime() < 3 * 1000;
}
inline bool MediaReader::readVideoSample(int iTimeInc) {
if (m_video_trId != MP4_INVALID_TRACK_ID) {
if (_video_trId != MP4_INVALID_TRACK_ID) {
auto iNextSample = getVideoSampleId(iTimeInc);
MP4SampleId iIdx = m_video_current;
for (iIdx = m_video_current; iIdx < iNextSample; iIdx++) {
uint8_t *pBytes = m_pcVideoSample.get();
uint32_t numBytes = m_video_sample_max_size;
if(MP4ReadSample(m_hMP4File, m_video_trId, iIdx + 1, &pBytes, &numBytes,NULL,NULL,NULL,&m_bSyncSample)){
MP4SampleId iIdx = _video_current;
for (iIdx = _video_current; iIdx < iNextSample; iIdx++) {
uint8_t *pBytes = _pcVideoSample.get();
uint32_t numBytes = _video_sample_max_size;
if(MP4ReadSample(_hMP4File, _video_trId, iIdx + 1, &pBytes, &numBytes,NULL,NULL,NULL,&_bSyncSample)){
if (!iTimeInc) {
uint32_t iOffset = 0;
while (iOffset < numBytes) {
@@ -219,97 +219,97 @@ inline bool MediaReader::readVideoSample(int iTimeInc) {
break;
}
memcpy(pBytes + iOffset, "\x0\x0\x0\x1", 4);
writeH264(pBytes + iOffset, iFrameLen + 4, (double) m_video_ms * iIdx / m_video_num_samples);
writeH264(pBytes + iOffset, iFrameLen + 4, (double) _video_ms * iIdx / _video_num_samples);
iOffset += (iFrameLen + 4);
}
}else if(m_bSyncSample){
}else if(_bSyncSample){
break;
}
}else{
ErrorL << "读取视频失败:" << iIdx + 1;
}
}
m_video_current = iIdx;
return m_video_current < m_video_num_samples;
_video_current = iIdx;
return _video_current < _video_num_samples;
}
return false;
}
inline bool MediaReader::readAudioSample(int iTimeInc) {
if (m_audio_trId != MP4_INVALID_TRACK_ID) {
if (_audio_trId != MP4_INVALID_TRACK_ID) {
auto iNextSample = getAudioSampleId(iTimeInc);
for (auto i = m_audio_current; i < iNextSample; i++) {
uint32_t numBytes = m_audio_sample_max_size;
uint8_t *pBytes = m_adts.buffer + 7;
if(MP4ReadSample(m_hMP4File, m_audio_trId, i + 1, &pBytes, &numBytes)){
for (auto i = _audio_current; i < iNextSample; i++) {
uint32_t numBytes = _audio_sample_max_size;
uint8_t *pBytes = _adts.buffer + 7;
if(MP4ReadSample(_hMP4File, _audio_trId, i + 1, &pBytes, &numBytes)){
if (!iTimeInc) {
m_adts.aac_frame_length = 7 + numBytes;
writeAdtsHeader(m_adts, m_adts.buffer);
writeAAC(m_adts.buffer, m_adts.aac_frame_length, (double) m_audio_ms * i / m_audio_num_samples);
_adts.aac_frame_length = 7 + numBytes;
writeAdtsHeader(_adts, _adts.buffer);
writeAAC(_adts.buffer, _adts.aac_frame_length, (double) _audio_ms * i / _audio_num_samples);
}
}else{
ErrorL << "读取音频失败:" << i+ 1;
}
}
m_audio_current = iNextSample;
return m_audio_current < m_audio_num_samples;
_audio_current = iNextSample;
return _audio_current < _audio_num_samples;
}
return false;
}
inline void MediaReader::writeH264(uint8_t *pucData,int iLen,uint32_t uiStamp) {
m_pChn->inputH264((char *)pucData, iLen, uiStamp);
_pChn->inputH264((char *)pucData, iLen, uiStamp);
}
inline void MediaReader::writeAAC(uint8_t *pucData,int iLen,uint32_t uiStamp) {
m_pChn->inputAAC((char *)pucData, iLen, uiStamp);
_pChn->inputAAC((char *)pucData, iLen, uiStamp);
}
inline MP4SampleId MediaReader::getVideoSampleId(int iTimeInc ) {
MP4SampleId video_current = (double)m_video_num_samples * (m_iSeekTime + m_ticker.elapsedTime() + iTimeInc) / m_video_ms;
video_current = MAX(0,MIN(m_video_num_samples, video_current));
MP4SampleId video_current = (double)_video_num_samples * (_iSeekTime + _ticker.elapsedTime() + iTimeInc) / _video_ms;
video_current = MAX(0,MIN(_video_num_samples, video_current));
return video_current;
}
inline MP4SampleId MediaReader::getAudioSampleId(int iTimeInc) {
MP4SampleId audio_current = (double)m_audio_num_samples * (m_iSeekTime + m_ticker.elapsedTime() + iTimeInc) / m_audio_ms ;
audio_current = MAX(0,MIN(m_audio_num_samples,audio_current));
MP4SampleId audio_current = (double)_audio_num_samples * (_iSeekTime + _ticker.elapsedTime() + iTimeInc) / _audio_ms ;
audio_current = MAX(0,MIN(_audio_num_samples,audio_current));
return audio_current;
}
inline void MediaReader::setSeekTime(int iSeekTime){
m_iSeekTime = MAX(0, MIN(iSeekTime,m_iDuration));
m_ticker.resetTime();
if (m_audio_trId != MP4_INVALID_TRACK_ID) {
m_audio_current = getAudioSampleId();
_iSeekTime = MAX(0, MIN(iSeekTime,_iDuration));
_ticker.resetTime();
if (_audio_trId != MP4_INVALID_TRACK_ID) {
_audio_current = getAudioSampleId();
}
if (m_video_trId != MP4_INVALID_TRACK_ID) {
m_video_current = getVideoSampleId();
if (_video_trId != MP4_INVALID_TRACK_ID) {
_video_current = getVideoSampleId();
}
}
inline uint32_t MediaReader::getVideoCurrentTime(){
return (double)m_video_current * m_video_ms /m_video_num_samples;
return (double)_video_current * _video_ms /_video_num_samples;
}
void MediaReader::seek(int iSeekTime,bool bReStart){
lock_guard<recursive_mutex> lck(m_mtx);
if(iSeekTime == 0 || m_video_trId == MP4_INVALID_TRACK_ID){
lock_guard<recursive_mutex> lck(_mtx);
if(iSeekTime == 0 || _video_trId == MP4_INVALID_TRACK_ID){
setSeekTime(iSeekTime);
}else{
setSeekTime(iSeekTime - 5000);
//在之后的10秒查找关键帧
readVideoSample(10000);
if (m_bSyncSample) {
if (_bSyncSample) {
//找到关键帧
auto iIdr = m_video_current;
auto iIdr = _video_current;
setSeekTime(getVideoCurrentTime());
m_video_current = iIdr;
_video_current = iIdr;
}else{
//未找到关键帧
setSeekTime(iSeekTime);
}
}
m_pChn->updateTimeStamp(m_iSeekTime);
_pChn->updateTimeStamp(_iSeekTime);
if(bReStart){
AsyncTaskThread::Instance().CancelTask(reinterpret_cast<uint64_t>(this));

View File

@@ -55,37 +55,37 @@ public:
private:
#ifdef ENABLE_MP4V2
MP4FileHandle m_hMP4File = MP4_INVALID_FILE_HANDLE;
MP4TrackId m_video_trId = MP4_INVALID_TRACK_ID;
uint32_t m_video_ms = 0;
uint32_t m_video_num_samples = 0;
uint32_t m_video_sample_max_size = 0;
uint32_t m_video_width = 0;
uint32_t m_video_height = 0;
uint32_t m_video_framerate = 0;
string m_strPps;
string m_strSps;
bool m_bSyncSample = false;
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 m_audio_trId = MP4_INVALID_TRACK_ID;
uint32_t m_audio_ms = 0;
uint32_t m_audio_num_samples = 0;
uint32_t m_audio_sample_max_size = 0;
uint32_t m_audio_sample_rate = 0;
uint32_t m_audio_num_channels = 0;
string m_strAacCfg;
AACFrame m_adts;
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 m_iDuration = 0;
DevChannel::Ptr m_pChn;
MP4SampleId m_video_current = 0;
MP4SampleId m_audio_current = 0;
std::shared_ptr<uint8_t> m_pcVideoSample;
int _iDuration = 0;
DevChannel::Ptr _pChn;
MP4SampleId _video_current = 0;
MP4SampleId _audio_current = 0;
std::shared_ptr<uint8_t> _pcVideoSample;
int m_iSeekTime = 0 ;
Ticker m_ticker;
Ticker m_alive;
recursive_mutex m_mtx;
int _iSeekTime = 0 ;
Ticker _ticker;
Ticker _alive;
recursive_mutex _mtx;
void seek(int iSeekTime,bool bReStart = true);
inline void setSeekTime(int iSeekTime);

View File

@@ -57,7 +57,7 @@ MediaRecorder::MediaRecorder(const string &strVhost_tmp,
if(enableHls) {
auto m3u8FilePath = hlsPath + "/" + strVhost + "/" + strApp + "/" + strId + "/hls.m3u8";
m_hlsMaker.reset(new HLSMaker(m3u8FilePath,hlsBufSize, hlsDuration, hlsNum));
_hlsMaker.reset(new HLSMaker(m3u8FilePath,hlsBufSize, hlsDuration, hlsNum));
}
#ifdef ENABLE_MP4V2
@@ -66,7 +66,7 @@ MediaRecorder::MediaRecorder(const string &strVhost_tmp,
if(enableMp4){
auto mp4FilePath = recordPath + "/" + strVhost + "/" + recordAppName + "/" + strApp + "/" + strId + "/";
m_mp4Maker.reset(new Mp4Maker(mp4FilePath,strVhost,strApp,strId,pPlayer));
_mp4Maker.reset(new Mp4Maker(mp4FilePath,strVhost,strApp,strId,pPlayer));
}
#endif //ENABLE_MP4V2
}
@@ -75,23 +75,23 @@ MediaRecorder::~MediaRecorder() {
}
void MediaRecorder::inputH264(void* pData, uint32_t ui32Length, uint32_t ui32TimeStamp, int iType) {
if(m_hlsMaker){
m_hlsMaker->inputH264(pData, ui32Length, ui32TimeStamp, iType);
if(_hlsMaker){
_hlsMaker->inputH264(pData, ui32Length, ui32TimeStamp, iType);
}
#ifdef ENABLE_MP4V2
if(m_mp4Maker){
m_mp4Maker->inputH264(pData, ui32Length, ui32TimeStamp, iType);
if(_mp4Maker){
_mp4Maker->inputH264(pData, ui32Length, ui32TimeStamp, iType);
}
#endif //ENABLE_MP4V2
}
void MediaRecorder::inputAAC(void* pData, uint32_t ui32Length, uint32_t ui32TimeStamp) {
if(m_hlsMaker){
m_hlsMaker->inputAAC(pData, ui32Length, ui32TimeStamp);
if(_hlsMaker){
_hlsMaker->inputAAC(pData, ui32Length, ui32TimeStamp);
}
#ifdef ENABLE_MP4V2
if(m_mp4Maker){
m_mp4Maker->inputAAC(pData, ui32Length, ui32TimeStamp);
if(_mp4Maker){
_mp4Maker->inputAAC(pData, ui32Length, ui32TimeStamp);
}
#endif //ENABLE_MP4V2
}

View File

@@ -50,8 +50,8 @@ public:
const string &strApp,
const string &strId,
const std::shared_ptr<PlayerBase> &pPlayer,
bool m_enableHls = true,
bool m_enableMp4 = false);
bool enableHls = true,
bool enableMp4 = false);
virtual ~MediaRecorder();
void inputH264( void *pData,
@@ -63,9 +63,9 @@ public:
uint32_t ui32Length,
uint32_t ui32TimeStamp);
private:
std::shared_ptr<HLSMaker> m_hlsMaker;
std::shared_ptr<HLSMaker> _hlsMaker;
#ifdef ENABLE_MP4V2
std::shared_ptr<Mp4Maker> m_mp4Maker;
std::shared_ptr<Mp4Maker> _mp4Maker;
#endif //ENABLE_MP4V2
};

View File

@@ -62,14 +62,14 @@ Mp4Maker::Mp4Maker(const string& strPath,
const string &strStreamId,
const PlayerBase::Ptr &pPlayer) {
DebugL << strPath;
m_pPlayer = pPlayer;
m_strPath = strPath;
_pPlayer = pPlayer;
_strPath = strPath;
/////record 业务逻辑//////
m_info.strAppName = strApp;
m_info.strStreamId = strStreamId;
m_info.strVhost = strVhost;
m_info.strFolder = strPath;
_info.strAppName = strApp;
_info.strStreamId = strStreamId;
_info.strVhost = strVhost;
_info.strFolder = strPath;
//----record 业务逻辑----//
}
Mp4Maker::~Mp4Maker() {
@@ -80,22 +80,22 @@ void Mp4Maker::inputH264(void *pData, uint32_t ui32Length, uint32_t ui32TimeStam
switch (iType) {
case 1: //P
case 5: { //IDR
if (m_strLastVideo.size()) {
int64_t iTimeInc = (int64_t)ui32TimeStamp - (int64_t)m_ui32LastVideoTime;
if (_strLastVideo.size()) {
int64_t iTimeInc = (int64_t)ui32TimeStamp - (int64_t)_ui32LastVideoTime;
iTimeInc = MAX(0,MIN(iTimeInc,500));
if(iTimeInc == 0 || iTimeInc == 500){
WarnL << "abnormal time stamp increment:" << ui32TimeStamp << " " << m_ui32LastVideoTime;
WarnL << "abnormal time stamp increment:" << ui32TimeStamp << " " << _ui32LastVideoTime;
}
_inputH264((char *) m_strLastVideo.data(), m_strLastVideo.size(), iTimeInc, m_iLastVideoType);
_inputH264((char *) _strLastVideo.data(), _strLastVideo.size(), iTimeInc, _iLastVideoType);
}
//m_strLastVideo.assign(("\x0\x0\x0\x2\x9\xf0"), 6);
//_strLastVideo.assign(("\x0\x0\x0\x2\x9\xf0"), 6);
uint32_t *p = (uint32_t *) pData;
*p = htonl(ui32Length - 4);
m_strLastVideo.assign((char *) pData, ui32Length);
_strLastVideo.assign((char *) pData, ui32Length);
memcpy(pData, "\x00\x00\x00\x01", 4);
m_ui32LastVideoTime = ui32TimeStamp;
m_iLastVideoType = iType;
_ui32LastVideoTime = ui32TimeStamp;
_iLastVideoType = iType;
}
break;
default:
@@ -103,28 +103,28 @@ void Mp4Maker::inputH264(void *pData, uint32_t ui32Length, uint32_t ui32TimeStam
}
}
void Mp4Maker::inputAAC(void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp){
if (m_strLastAudio.size()) {
int64_t iTimeInc = (int64_t)ui32TimeStamp - (int64_t)m_ui32LastAudioTime;
if (_strLastAudio.size()) {
int64_t iTimeInc = (int64_t)ui32TimeStamp - (int64_t)_ui32LastAudioTime;
iTimeInc = MAX(0,MIN(iTimeInc,500));
if(iTimeInc == 0 || iTimeInc == 500){
WarnL << "abnormal time stamp increment:" << ui32TimeStamp << " " << m_ui32LastAudioTime;
WarnL << "abnormal time stamp increment:" << ui32TimeStamp << " " << _ui32LastAudioTime;
}
_inputAAC((char *)m_strLastAudio.data(), m_strLastAudio.size(), iTimeInc);
_inputAAC((char *)_strLastAudio.data(), _strLastAudio.size(), iTimeInc);
}
m_strLastAudio.assign((char *)pData, ui32Length);
m_ui32LastAudioTime = ui32TimeStamp;
_strLastAudio.assign((char *)pData, ui32Length);
_ui32LastAudioTime = ui32TimeStamp;
}
void Mp4Maker::_inputH264(void* pData, uint32_t ui32Length, uint32_t ui32Duration, int iType) {
GET_CONFIG_AND_REGISTER(uint32_t,recordSec,Config::Record::kFileSecond);
if(iType == 5 && (m_hMp4 == MP4_INVALID_FILE_HANDLE || m_ticker.elapsedTime() > recordSec * 1000)){
if(iType == 5 && (_hMp4 == MP4_INVALID_FILE_HANDLE || _ticker.elapsedTime() > recordSec * 1000)){
//在I帧率处新建MP4文件
//如果文件未创建或者文件超过10分钟则创建新文件
createFile();
}
if (m_hVideo != MP4_INVALID_TRACK_ID) {
MP4WriteSample(m_hMp4, m_hVideo, (uint8_t *) pData, ui32Length,ui32Duration * 90,0,iType == 5);
if (_hVideo != MP4_INVALID_TRACK_ID) {
MP4WriteSample(_hMp4, _hVideo, (uint8_t *) pData, ui32Length,ui32Duration * 90,0,iType == 5);
}
}
@@ -134,39 +134,39 @@ void Mp4Maker::_inputAAC(void* pData, uint32_t ui32Length, uint32_t ui32Duration
//todo(xzl) 修复此处
//
// if (!m_pPlayer->containVideo() && (m_hMp4 == MP4_INVALID_FILE_HANDLE || m_ticker.elapsedTime() > recordSec * 1000)) {
// if (!_pPlayer->containVideo() && (_hMp4 == MP4_INVALID_FILE_HANDLE || _ticker.elapsedTime() > recordSec * 1000)) {
// //在I帧率处新建MP4文件
// //如果文件未创建或者文件超过10分钟则创建新文件
// createFile();
// }
// if (m_hAudio != MP4_INVALID_TRACK_ID) {
// auto duration = ui32Duration * m_pPlayer->getAudioSampleRate() /1000.0;
// MP4WriteSample(m_hMp4, m_hAudio, (uint8_t*)pData + 7, ui32Length - 7,duration,0,false);
// if (_hAudio != MP4_INVALID_TRACK_ID) {
// auto duration = ui32Duration * _pPlayer->getAudioSampleRate() /1000.0;
// MP4WriteSample(_hMp4, _hAudio, (uint8_t*)pData + 7, ui32Length - 7,duration,0,false);
// }
}
void Mp4Maker::createFile() {
if(!m_pPlayer->isInited()){
if(!_pPlayer->isInited()){
return;
}
closeFile();
auto strDate = timeStr("%Y-%m-%d");
auto strTime = timeStr("%H-%M-%S");
auto strFileTmp = m_strPath + strDate + "/." + strTime + ".mp4";
auto strFile = m_strPath + strDate + "/" + strTime + ".mp4";
auto strFileTmp = _strPath + strDate + "/." + strTime + ".mp4";
auto strFile = _strPath + strDate + "/" + strTime + ".mp4";
/////record 业务逻辑//////
m_info.ui64StartedTime = ::time(NULL);
m_info.strFileName = strTime + ".mp4";
m_info.strFilePath = strFile;
_info.ui64StartedTime = ::time(NULL);
_info.strFileName = strTime + ".mp4";
_info.strFilePath = strFile;
GET_CONFIG_AND_REGISTER(string,appName,Config::Record::kAppName);
m_info.strUrl = m_info.strVhost + "/"
_info.strUrl = _info.strVhost + "/"
+ appName + "/"
+ m_info.strAppName + "/"
+ m_info.strStreamId + "/"
+ _info.strAppName + "/"
+ _info.strStreamId + "/"
+ strDate + "/"
+ strTime + ".mp4";
//----record 业务逻辑----//
@@ -176,36 +176,36 @@ void Mp4Maker::createFile() {
#else
File::createfile_path(strFileTmp.data(), 0);
#endif
m_hMp4 = MP4Create(strFileTmp.data());
if (m_hMp4 == MP4_INVALID_FILE_HANDLE) {
_hMp4 = MP4Create(strFileTmp.data());
if (_hMp4 == MP4_INVALID_FILE_HANDLE) {
WarnL << "创建MP4文件失败:" << strFileTmp;
return;
}
//MP4SetTimeScale(m_hMp4, 90000);
m_strFileTmp = strFileTmp;
m_strFile = strFile;
m_ticker.resetTime();
//MP4SetTimeScale(_hMp4, 90000);
_strFileTmp = strFileTmp;
_strFile = strFile;
_ticker.resetTime();
//todo(xzl) 修复此处
// if(m_pPlayer->containVideo()){
// auto &sps = m_pPlayer->getSps();
// auto &pps = m_pPlayer->getPps();
// m_hVideo = MP4AddH264VideoTrack(m_hMp4, 90000, MP4_INVALID_DURATION,
// m_pPlayer->getVideoWidth(), m_pPlayer->getVideoHeight(),
// if(_pPlayer->containVideo()){
// auto &sps = _pPlayer->getSps();
// auto &pps = _pPlayer->getPps();
// _hVideo = MP4AddH264VideoTrack(_hMp4, 90000, MP4_INVALID_DURATION,
// _pPlayer->getVideoWidth(), _pPlayer->getVideoHeight(),
// sps[5], sps[6], sps[7], 3);
// if(m_hVideo !=MP4_INVALID_TRACK_ID){
// MP4AddH264SequenceParameterSet(m_hMp4, m_hVideo, (uint8_t *)sps.data() + 4, sps.size() - 4);
// MP4AddH264PictureParameterSet(m_hMp4, m_hVideo, (uint8_t *)pps.data() + 4, pps.size() - 4);
// if(_hVideo !=MP4_INVALID_TRACK_ID){
// MP4AddH264SequenceParameterSet(_hMp4, _hVideo, (uint8_t *)sps.data() + 4, sps.size() - 4);
// MP4AddH264PictureParameterSet(_hMp4, _hVideo, (uint8_t *)pps.data() + 4, pps.size() - 4);
// }else{
// WarnL << "添加视频通道失败:" << strFileTmp;
// }
// }
// if(m_pPlayer->containAudio()){
// m_hAudio = MP4AddAudioTrack(m_hMp4, m_pPlayer->getAudioSampleRate(), MP4_INVALID_DURATION, MP4_MPEG4_AUDIO_TYPE);
// if (m_hAudio != MP4_INVALID_TRACK_ID) {
// auto &cfg = m_pPlayer->getAudioCfg();
// MP4SetTrackESConfiguration(m_hMp4, m_hAudio,(uint8_t *)cfg.data(), cfg.size());
// if(_pPlayer->containAudio()){
// _hAudio = MP4AddAudioTrack(_hMp4, _pPlayer->getAudioSampleRate(), MP4_INVALID_DURATION, MP4_MPEG4_AUDIO_TYPE);
// if (_hAudio != MP4_INVALID_TRACK_ID) {
// auto &cfg = _pPlayer->getAudioCfg();
// MP4SetTrackESConfiguration(_hMp4, _hAudio,(uint8_t *)cfg.data(), cfg.size());
// }else{
// WarnL << "添加音频通道失败:" << strFileTmp;
// }
@@ -213,24 +213,24 @@ void Mp4Maker::createFile() {
}
void Mp4Maker::closeFile() {
if (m_hMp4 != MP4_INVALID_FILE_HANDLE) {
if (_hMp4 != MP4_INVALID_FILE_HANDLE) {
{
TimeTicker();
MP4Close(m_hMp4,MP4_CLOSE_DO_NOT_COMPUTE_BITRATE);
MP4Close(_hMp4,MP4_CLOSE_DO_NOT_COMPUTE_BITRATE);
}
rename(m_strFileTmp.data(),m_strFile.data());
m_hMp4 = MP4_INVALID_FILE_HANDLE;
m_hVideo = MP4_INVALID_TRACK_ID;
m_hAudio = MP4_INVALID_TRACK_ID;
rename(_strFileTmp.data(),_strFile.data());
_hMp4 = MP4_INVALID_FILE_HANDLE;
_hVideo = MP4_INVALID_TRACK_ID;
_hAudio = MP4_INVALID_TRACK_ID;
/////record 业务逻辑//////
m_info.ui64TimeLen = ::time(NULL) - m_info.ui64StartedTime;
_info.ui64TimeLen = ::time(NULL) - _info.ui64StartedTime;
//获取文件大小
struct stat fileData;
stat(m_strFile.data(), &fileData);
m_info.ui64FileSize = fileData.st_size;
stat(_strFile.data(), &fileData);
_info.ui64FileSize = fileData.st_size;
//----record 业务逻辑----//
NoticeCenter::Instance().emitEvent(Config::Broadcast::kBroadcastRecordMP4,m_info,*this);
NoticeCenter::Instance().emitEvent(Config::Broadcast::kBroadcastRecordMP4,_info,*this);
}
}

View File

@@ -73,29 +73,29 @@ public:
//时间戳参考频率1000
void inputAAC(void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp);
private:
MP4FileHandle m_hMp4 = MP4_INVALID_FILE_HANDLE;
MP4TrackId m_hVideo = MP4_INVALID_TRACK_ID;
MP4TrackId m_hAudio = MP4_INVALID_TRACK_ID;
PlayerBase::Ptr m_pPlayer;
string m_strPath;
string m_strFile;
string m_strFileTmp;
Ticker m_ticker;
SmoothTicker m_mediaTicker[2];
MP4FileHandle _hMp4 = MP4_INVALID_FILE_HANDLE;
MP4TrackId _hVideo = MP4_INVALID_TRACK_ID;
MP4TrackId _hAudio = MP4_INVALID_TRACK_ID;
PlayerBase::Ptr _pPlayer;
string _strPath;
string _strFile;
string _strFileTmp;
Ticker _ticker;
SmoothTicker _mediaTicker[2];
void createFile();
void closeFile();
void _inputH264(void *pData, uint32_t ui32Length, uint32_t ui64Duration, int iType);
void _inputAAC(void *pData, uint32_t ui32Length, uint32_t ui64Duration);
string m_strLastVideo;
string m_strLastAudio;
string _strLastVideo;
string _strLastAudio;
uint32_t m_ui32LastVideoTime = 0;
uint32_t m_ui32LastAudioTime = 0;
int m_iLastVideoType = 0;
uint32_t _ui32LastVideoTime = 0;
uint32_t _ui32LastAudioTime = 0;
int _iLastVideoType = 0;
Mp4Info m_info;
Mp4Info _info;
};
} /* namespace MediaFile */