2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
#include "MP4Reader.h"
|
2017-05-02 17:15:12 +08:00
|
|
|
|
#include "Common/config.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Util/mini.h"
|
2019-12-04 10:45:38 +08:00
|
|
|
|
#include "Util/File.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "Http/HttpSession.h"
|
2019-06-28 17:25:53 +08:00
|
|
|
|
#include "Extension/AAC.h"
|
|
|
|
|
#include "Extension/H264.h"
|
2019-09-27 18:05:42 +08:00
|
|
|
|
#include "Thread/WorkThreadPool.h"
|
2019-06-28 17:25:53 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2017-05-02 17:07:02 +08:00
|
|
|
|
#ifdef ENABLE_MP4V2
|
2019-12-04 10:45:38 +08:00
|
|
|
|
MP4Reader::MP4Reader(const string &strVhost,const string &strApp, const string &strId,const string &filePath ) {
|
2019-09-27 18:05:42 +08:00
|
|
|
|
_poller = WorkThreadPool::Instance().getPoller();
|
2019-04-03 11:49:58 +08:00
|
|
|
|
auto strFileName = filePath;
|
|
|
|
|
if(strFileName.empty()){
|
2019-05-28 17:14:36 +08:00
|
|
|
|
GET_CONFIG(string,recordPath,Record::kFilePath);
|
|
|
|
|
GET_CONFIG(bool,enableVhost,General::kEnableVhost);
|
|
|
|
|
if(enableVhost){
|
2019-10-18 09:51:20 +08:00
|
|
|
|
strFileName = strVhost + "/" + strApp + "/" + strId;
|
2019-05-28 17:14:36 +08:00
|
|
|
|
}else{
|
2019-10-18 09:51:20 +08:00
|
|
|
|
strFileName = strApp + "/" + strId;
|
2019-05-28 17:14:36 +08:00
|
|
|
|
}
|
2019-10-18 10:02:42 +08:00
|
|
|
|
strFileName = File::absolutePath(strFileName,recordPath);
|
2019-04-03 11:49:58 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_hMP4File = MP4Read(strFileName.data());
|
|
|
|
|
if(_hMP4File == MP4_INVALID_FILE_HANDLE){
|
2017-08-09 18:39:30 +08:00
|
|
|
|
throw runtime_error(StrPrinter << "打开MP4文件失败:" << strFileName << endl);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_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){
|
2017-04-01 16:35:56 +08:00
|
|
|
|
delete [] ptr;
|
|
|
|
|
});
|
|
|
|
|
uint8_t **seqheader;
|
|
|
|
|
uint8_t **pictheader;
|
|
|
|
|
uint32_t *pictheadersize;
|
|
|
|
|
uint32_t *seqheadersize;
|
|
|
|
|
uint32_t ix;
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if(MP4GetTrackH264SeqPictHeaders(_hMP4File, _video_trId, &seqheader, &seqheadersize, &pictheader, &pictheadersize)){
|
2017-04-01 16:35:56 +08:00
|
|
|
|
for (ix = 0; seqheadersize[ix] != 0; ix++) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strSps.assign((char *)(seqheader[ix]), seqheadersize[ix]);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
float framerate;
|
2018-10-24 15:43:52 +08:00
|
|
|
|
getAVCInfo(_strSps, (int &)_video_width, (int &)_video_height, framerate);
|
|
|
|
|
_video_framerate = framerate;
|
|
|
|
|
_strSps = string("\x0\x0\x0\x1",4) + _strSps;
|
2018-05-22 17:22:31 +08:00
|
|
|
|
MP4Free(seqheader[ix]);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-05-22 17:22:31 +08:00
|
|
|
|
MP4Free(seqheader);
|
|
|
|
|
MP4Free(seqheadersize);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
for (ix = 0; pictheadersize[ix] != 0; ix++) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strPps.assign("\x0\x0\x0\x1",4);
|
|
|
|
|
_strPps.append((char *)(pictheader[ix]), pictheadersize[ix]);
|
2018-05-22 17:22:31 +08:00
|
|
|
|
MP4Free(pictheader[ix]);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-05-22 17:22:31 +08:00
|
|
|
|
MP4Free(pictheader);
|
|
|
|
|
MP4Free(pictheadersize);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_video_ms = 1000.0 * _video_duration / _video_timescale;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
/*InfoL << "\r\n"
|
2018-10-24 15:43:52 +08:00
|
|
|
|
<< _video_ms << "\r\n"
|
|
|
|
|
<< _video_num_samples << "\r\n"
|
|
|
|
|
<< _video_framerate << "\r\n"
|
|
|
|
|
<< _video_width << "\r\n"
|
|
|
|
|
<< _video_height << "\r\n";*/
|
2017-04-01 16:35:56 +08:00
|
|
|
|
} else {
|
|
|
|
|
//如果不是h264,则忽略
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_video_trId = MP4_INVALID_TRACK_ID;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_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);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
uint8_t *ppConfig;
|
|
|
|
|
uint32_t pConfigSize;
|
2018-10-24 15:43:52 +08:00
|
|
|
|
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);
|
2018-05-22 17:22:31 +08:00
|
|
|
|
MP4Free(ppConfig);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_audio_ms = 1000.0 * _audio_duration / _audio_sample_rate;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
/*InfoL << "\r\n"
|
2018-10-24 15:43:52 +08:00
|
|
|
|
<< _audio_ms << "\r\n"
|
|
|
|
|
<< _audio_num_samples << "\r\n"
|
|
|
|
|
<< _audio_num_channels << "\r\n"
|
|
|
|
|
<< _audio_sample_rate << "\r\n";*/
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}else{
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_audio_trId = MP4_INVALID_TRACK_ID;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if(_audio_trId == MP4_INVALID_TRACK_ID && _video_trId == MP4_INVALID_TRACK_ID){
|
|
|
|
|
MP4Close(_hMP4File);
|
|
|
|
|
_hMP4File = MP4_INVALID_FILE_HANDLE;
|
2017-08-09 18:39:30 +08:00
|
|
|
|
throw runtime_error(StrPrinter << "该MP4文件音视频格式不支持:" << strFileName << endl);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iDuration = MAX(_video_ms,_audio_ms);
|
2019-07-22 11:27:17 +08:00
|
|
|
|
_mediaMuxer.reset(new MultiMediaSourceMuxer(strVhost, strApp, strId, _iDuration / 1000.0, true, true, false, false));
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_audio_trId != MP4_INVALID_TRACK_ID) {
|
2018-10-26 16:09:48 +08:00
|
|
|
|
AACTrack::Ptr track = std::make_shared<AACTrack>(_strAacCfg);
|
2018-10-29 11:12:37 +08:00
|
|
|
|
_mediaMuxer->addTrack(track);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_video_trId != MP4_INVALID_TRACK_ID) {
|
2018-10-26 16:09:48 +08:00
|
|
|
|
H264Track::Ptr track = std::make_shared<H264Track>(_strSps,_strPps);
|
2018-10-29 11:12:37 +08:00
|
|
|
|
_mediaMuxer->addTrack(track);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
MP4Reader::~MP4Reader() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_hMP4File != MP4_INVALID_FILE_HANDLE) {
|
|
|
|
|
MP4Close(_hMP4File);
|
|
|
|
|
_hMP4File = MP4_INVALID_FILE_HANDLE;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
void MP4Reader::startReadMP4() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
auto strongSelf = shared_from_this();
|
2019-05-28 17:14:36 +08:00
|
|
|
|
GET_CONFIG(uint32_t,sampleMS,Record::kSampleMS);
|
2018-02-09 11:42:55 +08:00
|
|
|
|
|
2019-01-30 18:11:00 +08:00
|
|
|
|
_timer = std::make_shared<Timer>(sampleMS / 1000.0f,[strongSelf](){
|
2018-11-14 14:27:06 +08:00
|
|
|
|
return strongSelf->readSample(0,false);
|
2019-07-11 14:51:40 +08:00
|
|
|
|
}, _poller);
|
2019-01-30 18:11:00 +08:00
|
|
|
|
|
2018-11-14 14:27:06 +08:00
|
|
|
|
//先读sampleMS毫秒的数据用于产生MediaSouce
|
|
|
|
|
readSample(sampleMS, false);
|
2018-10-29 11:12:37 +08:00
|
|
|
|
_mediaMuxer->setListener(strongSelf);
|
2018-02-02 18:06:08 +08:00
|
|
|
|
}
|
2019-12-04 10:45:38 +08:00
|
|
|
|
bool MP4Reader::seekTo(MediaSource &sender,uint32_t ui32Stamp){
|
2018-02-02 18:06:08 +08:00
|
|
|
|
seek(ui32Stamp);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-12-04 10:45:38 +08:00
|
|
|
|
bool MP4Reader::close(MediaSource &sender,bool force){
|
2019-05-30 12:14:20 +08:00
|
|
|
|
if(!_mediaMuxer || (!force && _mediaMuxer->readerCount() != 0)){
|
2019-05-27 18:39:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-01-30 18:11:00 +08:00
|
|
|
|
_timer.reset();
|
2019-05-29 18:08:50 +08:00
|
|
|
|
WarnL << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
|
2018-02-06 10:56:58 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
void MP4Reader::onNoneReader(MediaSource &sender) {
|
2019-05-31 15:40:55 +08:00
|
|
|
|
if(!_mediaMuxer || _mediaMuxer->readerCount() != 0){
|
2019-05-27 18:39:43 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
MediaSourceEvent::onNoneReader(sender);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
bool MP4Reader::readSample(int iTimeInc,bool justSeekSyncFrame) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
TimeTicker();
|
2018-10-24 15:43:52 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(_mtx);
|
2018-11-14 14:27:06 +08:00
|
|
|
|
auto bFlag0 = readVideoSample(iTimeInc,justSeekSyncFrame);//数据没读完
|
|
|
|
|
auto bFlag1 = readAudioSample(iTimeInc,justSeekSyncFrame);//数据没读完
|
2018-10-29 11:12:37 +08:00
|
|
|
|
auto bFlag2 = _mediaMuxer->readerCount() > 0;//读取者大于0
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if((bFlag0 || bFlag1) && bFlag2){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_alive.resetTime();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-09-24 20:48:30 +08:00
|
|
|
|
//重头开始循环读取
|
|
|
|
|
GET_CONFIG(bool,fileRepeat,Record::kFileRepeat);
|
|
|
|
|
if (fileRepeat && !bFlag0 && !bFlag1) {
|
|
|
|
|
seek(0);
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//DebugL << "alive ...";
|
|
|
|
|
//3秒延时关闭
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return _alive.elapsedTime() < 3 * 1000;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-12-04 10:45:38 +08:00
|
|
|
|
inline bool MP4Reader::readVideoSample(int iTimeInc,bool justSeekSyncFrame) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_video_trId != MP4_INVALID_TRACK_ID) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
auto iNextSample = getVideoSampleId(iTimeInc);
|
2018-10-24 15:43:52 +08:00
|
|
|
|
MP4SampleId iIdx = _video_current;
|
2018-11-14 14:27:06 +08:00
|
|
|
|
for (; iIdx < iNextSample; iIdx++) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
uint8_t *pBytes = _pcVideoSample.get();
|
|
|
|
|
uint32_t numBytes = _video_sample_max_size;
|
2019-12-06 17:23:49 +08:00
|
|
|
|
MP4Duration pRenderingOffset;
|
|
|
|
|
if(MP4ReadSample(_hMP4File, _video_trId, iIdx + 1, &pBytes, &numBytes,NULL,NULL,&pRenderingOffset,&_bSyncSample)){
|
2018-11-14 14:27:06 +08:00
|
|
|
|
if (!justSeekSyncFrame) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
uint32_t iOffset = 0;
|
|
|
|
|
while (iOffset < numBytes) {
|
|
|
|
|
uint32_t iFrameLen;
|
|
|
|
|
memcpy(&iFrameLen,pBytes + iOffset,4);
|
|
|
|
|
iFrameLen = ntohl(iFrameLen);
|
|
|
|
|
if(iFrameLen + iOffset + 4> numBytes){
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
memcpy(pBytes + iOffset, "\x0\x0\x0\x1", 4);
|
2019-12-06 17:23:49 +08:00
|
|
|
|
uint32_t dts = (double) _video_ms * iIdx / _video_num_samples;
|
|
|
|
|
writeH264(pBytes + iOffset, iFrameLen + 4, dts, dts + pRenderingOffset / 90);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
iOffset += (iFrameLen + 4);
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
}else if(_bSyncSample){
|
2017-04-01 16:35:56 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}else{
|
2018-04-09 11:33:19 +08:00
|
|
|
|
ErrorL << "读取视频失败:" << iIdx + 1;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_video_current = iIdx;
|
|
|
|
|
return _video_current < _video_num_samples;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
inline bool MP4Reader::readAudioSample(int iTimeInc,bool justSeekSyncFrame) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_audio_trId != MP4_INVALID_TRACK_ID) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
auto iNextSample = getAudioSampleId(iTimeInc);
|
2018-10-24 15:43:52 +08:00
|
|
|
|
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)){
|
2018-11-14 14:27:06 +08:00
|
|
|
|
if (!justSeekSyncFrame) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_adts.aac_frame_length = 7 + numBytes;
|
|
|
|
|
writeAdtsHeader(_adts, _adts.buffer);
|
|
|
|
|
writeAAC(_adts.buffer, _adts.aac_frame_length, (double) _audio_ms * i / _audio_num_samples);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}else{
|
2018-04-09 11:33:19 +08:00
|
|
|
|
ErrorL << "读取音频失败:" << i+ 1;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_audio_current = iNextSample;
|
|
|
|
|
return _audio_current < _audio_num_samples;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
inline void MP4Reader::writeH264(uint8_t *pucData,int iLen,uint32_t dts,uint32_t pts) {
|
2019-07-24 18:02:55 +08:00
|
|
|
|
_mediaMuxer->inputFrame(std::make_shared<H264FrameNoCacheAble>((char*)pucData,iLen,dts,pts));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
inline void MP4Reader::writeAAC(uint8_t *pucData,int iLen,uint32_t uiStamp) {
|
2019-07-24 18:02:55 +08:00
|
|
|
|
_mediaMuxer->inputFrame(std::make_shared<AACFrameNoCacheAble>((char*)pucData,iLen,uiStamp));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
inline MP4SampleId MP4Reader::getVideoSampleId(int iTimeInc ) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
MP4SampleId video_current = (double)_video_num_samples * (_iSeekTime + _ticker.elapsedTime() + iTimeInc) / _video_ms;
|
|
|
|
|
video_current = MAX(0,MIN(_video_num_samples, video_current));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return video_current;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
inline MP4SampleId MP4Reader::getAudioSampleId(int iTimeInc) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
MP4SampleId audio_current = (double)_audio_num_samples * (_iSeekTime + _ticker.elapsedTime() + iTimeInc) / _audio_ms ;
|
|
|
|
|
audio_current = MAX(0,MIN(_audio_num_samples,audio_current));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return audio_current;
|
|
|
|
|
}
|
2019-12-04 10:45:38 +08:00
|
|
|
|
inline void MP4Reader::setSeekTime(uint32_t iSeekTime){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iSeekTime = MAX(0, MIN(iSeekTime,_iDuration));
|
|
|
|
|
_ticker.resetTime();
|
|
|
|
|
if (_audio_trId != MP4_INVALID_TRACK_ID) {
|
|
|
|
|
_audio_current = getAudioSampleId();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_video_trId != MP4_INVALID_TRACK_ID) {
|
|
|
|
|
_video_current = getVideoSampleId();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
inline uint32_t MP4Reader::getVideoCurrentTime(){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
return (double)_video_current * _video_ms /_video_num_samples;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-12-04 10:45:38 +08:00
|
|
|
|
void MP4Reader::seek(uint32_t iSeekTime,bool bReStart){
|
2018-10-24 15:43:52 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(_mtx);
|
|
|
|
|
if(iSeekTime == 0 || _video_trId == MP4_INVALID_TRACK_ID){
|
2017-04-01 16:35:56 +08:00
|
|
|
|
setSeekTime(iSeekTime);
|
|
|
|
|
}else{
|
|
|
|
|
setSeekTime(iSeekTime - 5000);
|
|
|
|
|
//在之后的10秒查找关键帧
|
2018-11-14 14:27:06 +08:00
|
|
|
|
readVideoSample(10000, true);
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_bSyncSample) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//找到关键帧
|
2018-10-24 15:43:52 +08:00
|
|
|
|
auto iIdr = _video_current;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
setSeekTime(getVideoCurrentTime());
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_video_current = iIdr;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}else{
|
|
|
|
|
//未找到关键帧
|
|
|
|
|
setSeekTime(iSeekTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-29 11:12:37 +08:00
|
|
|
|
_mediaMuxer->setTimeStamp(_iSeekTime);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
if(bReStart){
|
2019-01-30 18:11:00 +08:00
|
|
|
|
_timer.reset();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
startReadMP4();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-02 17:07:02 +08:00
|
|
|
|
#endif //ENABLE_MP4V2
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
MediaSource::Ptr MP4Reader::onMakeMediaSource(const string &strSchema,
|
2019-04-03 11:49:58 +08:00
|
|
|
|
const string &strVhost,
|
|
|
|
|
const string &strApp,
|
|
|
|
|
const string &strId,
|
|
|
|
|
const string &filePath,
|
|
|
|
|
bool checkApp ){
|
2017-05-02 17:07:02 +08:00
|
|
|
|
#ifdef ENABLE_MP4V2
|
2019-05-28 17:14:36 +08:00
|
|
|
|
GET_CONFIG(string,appName,Record::kAppName);
|
2019-04-03 11:49:58 +08:00
|
|
|
|
if (checkApp && strApp != appName) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
try {
|
2019-12-04 10:45:38 +08:00
|
|
|
|
MP4Reader::Ptr pReader(new MP4Reader(strVhost,strApp, strId,filePath));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
pReader->startReadMP4();
|
2018-02-02 18:06:08 +08:00
|
|
|
|
return MediaSource::find(strSchema,strVhost,strApp, strId, false);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
WarnL << ex.what();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
return nullptr;
|
2017-05-02 17:07:02 +08:00
|
|
|
|
#endif //ENABLE_MP4V2
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|