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
|
|
|
|
*/
|
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
#ifdef ENABLE_MP4
|
2019-12-04 10:45:38 +08:00
|
|
|
|
#include "MP4Reader.h"
|
2017-05-02 17:15:12 +08:00
|
|
|
|
#include "Common/config.h"
|
2019-09-27 18:05:42 +08:00
|
|
|
|
#include "Thread/WorkThreadPool.h"
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
MP4Reader::MP4Reader(const string &strVhost,const string &strApp, const string &strId,const string &filePath ) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
_poller = WorkThreadPool::Instance().getPoller();
|
2019-04-03 11:49:58 +08:00
|
|
|
|
auto strFileName = filePath;
|
|
|
|
|
if(strFileName.empty()){
|
2020-03-20 11:51:24 +08:00
|
|
|
|
GET_CONFIG(string,recordPath,Record::kFilePath);
|
2019-05-28 17:14:36 +08:00
|
|
|
|
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
|
|
|
|
}
|
2020-03-20 11:51:24 +08:00
|
|
|
|
strFileName = File::absolutePath(strFileName,recordPath);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
_demuxer = std::make_shared<MP4Demuxer>(strFileName.data());
|
|
|
|
|
_mediaMuxer.reset(new MultiMediaSourceMuxer(strVhost, strApp, strId, _demuxer->getDurationMS() / 1000.0, true, true, false, false));
|
2020-04-03 21:39:44 +08:00
|
|
|
|
auto tracks = _demuxer->getTracks(false);
|
|
|
|
|
if(tracks.empty()){
|
|
|
|
|
throw std::runtime_error(StrPrinter << "该mp4文件没有有效的track:" << strFileName);
|
|
|
|
|
}
|
|
|
|
|
for(auto &track : tracks){
|
2020-04-03 20:45:58 +08:00
|
|
|
|
_mediaMuxer->addTrack(track);
|
|
|
|
|
if(track->getTrackType() == TrackVideo){
|
|
|
|
|
_have_video = true;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2019-04-03 11:49:58 +08:00
|
|
|
|
}
|
2020-04-03 20:45:58 +08:00
|
|
|
|
//添加完毕所有track,防止单track情况下最大等待3秒
|
|
|
|
|
_mediaMuxer->addTrackCompleted();
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
bool MP4Reader::readSample() {
|
2020-04-03 21:39:44 +08:00
|
|
|
|
bool keyFrame = false;
|
2020-04-03 22:04:13 +08:00
|
|
|
|
bool eof = false;
|
|
|
|
|
while (true) {
|
|
|
|
|
auto frame = _demuxer->readFrame(keyFrame);
|
2020-04-03 20:45:58 +08:00
|
|
|
|
if (!frame) {
|
2020-04-03 22:04:13 +08:00
|
|
|
|
eof = true;
|
2020-04-03 20:45:58 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
_mediaMuxer->inputFrame(frame);
|
2020-04-03 22:04:13 +08:00
|
|
|
|
if (frame->dts() > getCurrentStamp()) {
|
2020-04-03 20:45:58 +08:00
|
|
|
|
break;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 22:04:13 +08:00
|
|
|
|
GET_CONFIG(bool, fileRepeat, Record::kFileRepeat);
|
|
|
|
|
if (eof && fileRepeat) {
|
|
|
|
|
//需要从头开始看
|
2020-04-03 20:45:58 +08:00
|
|
|
|
seekTo(0);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2020-04-03 22:04:13 +08:00
|
|
|
|
|
|
|
|
|
return !eof;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
void MP4Reader::startReadMP4() {
|
2020-04-03 20:45:58 +08:00
|
|
|
|
GET_CONFIG(uint32_t, sampleMS, Record::kSampleMS);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
auto strongSelf = shared_from_this();
|
2020-04-03 20:45:58 +08:00
|
|
|
|
_mediaMuxer->setListener(strongSelf);
|
2018-02-09 11:42:55 +08:00
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
//先获取关键帧
|
|
|
|
|
seekTo(0);
|
|
|
|
|
//读sampleMS毫秒的数据用于产生MediaSource
|
2020-04-03 22:04:13 +08:00
|
|
|
|
setCurrentStamp(getCurrentStamp() + sampleMS);
|
2020-04-03 20:45:58 +08:00
|
|
|
|
readSample();
|
|
|
|
|
|
|
|
|
|
//启动定时器
|
|
|
|
|
_timer = std::make_shared<Timer>(sampleMS / 1000.0f, [strongSelf]() {
|
|
|
|
|
lock_guard<recursive_mutex> lck(strongSelf->_mtx);
|
|
|
|
|
return strongSelf->readSample();
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}, _poller);
|
2018-02-02 18:06:08 +08:00
|
|
|
|
}
|
2020-04-03 20:45:58 +08:00
|
|
|
|
|
2020-04-03 22:04:13 +08:00
|
|
|
|
uint32_t MP4Reader::getCurrentStamp() {
|
2020-04-03 20:45:58 +08:00
|
|
|
|
return _seek_to + _seek_ticker.elapsedTime();
|
2018-02-02 18:06:08 +08:00
|
|
|
|
}
|
2020-04-03 20:45:58 +08:00
|
|
|
|
|
2020-04-03 22:04:13 +08:00
|
|
|
|
void MP4Reader::setCurrentStamp(uint32_t ui32Stamp){
|
2020-04-03 20:45:58 +08:00
|
|
|
|
_seek_to = ui32Stamp;
|
|
|
|
|
_seek_ticker.resetTime();
|
2020-04-03 22:04:13 +08:00
|
|
|
|
_mediaMuxer->setTimeStamp(ui32Stamp);
|
2018-02-06 10:56:58 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
bool MP4Reader::seekTo(MediaSource &sender,uint32_t ui32Stamp){
|
|
|
|
|
return seekTo(ui32Stamp);
|
2019-12-28 16:48:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
bool MP4Reader::seekTo(uint32_t ui32Stamp){
|
2020-03-20 11:51:24 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(_mtx);
|
2020-04-03 20:45:58 +08:00
|
|
|
|
if (ui32Stamp > _demuxer->getDurationMS()) {
|
|
|
|
|
//超过文件长度
|
|
|
|
|
return false;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2020-04-03 20:45:58 +08:00
|
|
|
|
auto stamp = _demuxer->seekTo(ui32Stamp);
|
|
|
|
|
if(stamp == -1){
|
|
|
|
|
//seek失败
|
|
|
|
|
return false;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
if(!_have_video){
|
|
|
|
|
//没有视频,不需要搜索关键帧
|
2020-04-03 22:04:13 +08:00
|
|
|
|
//设置当前时间戳
|
|
|
|
|
setCurrentStamp(stamp);
|
2020-04-03 20:45:58 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
//搜索到下一帧关键帧
|
2020-04-03 21:39:44 +08:00
|
|
|
|
bool keyFrame = false;
|
2020-04-03 22:08:03 +08:00
|
|
|
|
while (true) {
|
2020-04-03 22:04:13 +08:00
|
|
|
|
auto frame = _demuxer->readFrame(keyFrame);
|
2020-04-03 20:45:58 +08:00
|
|
|
|
if(!frame){
|
2020-04-03 22:08:03 +08:00
|
|
|
|
//文件读完了都未找到下一帧关键帧
|
|
|
|
|
return false;
|
2020-04-03 20:45:58 +08:00
|
|
|
|
}
|
2020-04-03 21:39:44 +08:00
|
|
|
|
if(keyFrame || frame->keyFrame() || frame->configFrame()){
|
2020-04-03 20:45:58 +08:00
|
|
|
|
//定位到key帧
|
|
|
|
|
_mediaMuxer->inputFrame(frame);
|
2020-04-03 22:04:13 +08:00
|
|
|
|
//设置当前时间戳
|
|
|
|
|
setCurrentStamp(frame->dts());
|
2020-04-03 20:45:58 +08:00
|
|
|
|
return true;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
bool MP4Reader::close(MediaSource &sender,bool force){
|
|
|
|
|
if(!_mediaMuxer || (!force && _mediaMuxer->totalReaderCount())){
|
|
|
|
|
return false;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2020-04-03 20:45:58 +08:00
|
|
|
|
_timer.reset();
|
|
|
|
|
WarnL << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
|
|
|
|
|
return true;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
int MP4Reader::totalReaderCount(MediaSource &sender) {
|
|
|
|
|
return _mediaMuxer ? _mediaMuxer->totalReaderCount() : sender.readerCount();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
} /* namespace mediakit */
|
|
|
|
|
#endif //ENABLE_MP4
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
MediaSource::Ptr onMakeMediaSource(const string &strSchema,
|
|
|
|
|
const string &strVhost,
|
|
|
|
|
const string &strApp,
|
|
|
|
|
const string &strId,
|
|
|
|
|
const string &filePath,
|
|
|
|
|
bool checkApp) {
|
|
|
|
|
#ifdef ENABLE_MP4
|
|
|
|
|
GET_CONFIG(string, appName, Record::kAppName);
|
2019-04-03 11:49:58 +08:00
|
|
|
|
if (checkApp && strApp != appName) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
try {
|
2020-04-03 20:45:58 +08:00
|
|
|
|
MP4Reader::Ptr pReader(new MP4Reader(strVhost, strApp, strId, filePath));
|
2020-03-20 11:51:24 +08:00
|
|
|
|
pReader->startReadMP4();
|
2020-04-03 20:45:58 +08:00
|
|
|
|
return MediaSource::find(strSchema, strVhost, strApp, strId, false);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
|
WarnL << ex.what();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#else
|
2020-03-20 11:51:24 +08:00
|
|
|
|
return nullptr;
|
2020-04-03 20:45:58 +08:00
|
|
|
|
#endif //ENABLE_MP4
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2020-04-03 20:45:58 +08:00
|
|
|
|
}//namespace mediakit
|