2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
#ifdef ENABLE_MP4
|
2021-12-17 22:33:08 +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"
|
2019-09-27 18:05:42 +08:00
|
|
|
|
#include "Thread/WorkThreadPool.h"
|
2021-12-17 22:33:08 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2021-12-17 22:33:08 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2021-12-15 20:50:35 +08:00
|
|
|
|
MP4Reader::MP4Reader(const string &vhost, const string &app, const string &stream_id, const string &file_path) {
|
|
|
|
|
_file_path = file_path;
|
|
|
|
|
if (_file_path.empty()) {
|
|
|
|
|
GET_CONFIG(string, recordPath, Record::kFilePath);
|
|
|
|
|
GET_CONFIG(bool, enableVhost, General::kEnableVhost);
|
|
|
|
|
if (enableVhost) {
|
|
|
|
|
_file_path = vhost + "/" + app + "/" + stream_id;
|
|
|
|
|
} else {
|
|
|
|
|
_file_path = app + "/" + stream_id;
|
2019-05-28 17:14:36 +08:00
|
|
|
|
}
|
2021-12-15 20:50:35 +08:00
|
|
|
|
_file_path = File::absolutePath(_file_path, recordPath);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 19:45:04 +08:00
|
|
|
|
_demuxer = std::make_shared<MP4Demuxer>();
|
2020-09-27 11:32:49 +08:00
|
|
|
|
_demuxer->openMP4(_file_path);
|
2021-12-15 20:50:35 +08:00
|
|
|
|
|
|
|
|
|
if (stream_id.empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-12 13:19:21 +08:00
|
|
|
|
ProtocolOption option;
|
|
|
|
|
//读取mp4文件并流化时,不重复生成mp4/hls文件
|
|
|
|
|
option.enable_mp4 = false;
|
|
|
|
|
option.enable_hls = false;
|
|
|
|
|
_muxer = std::make_shared<MultiMediaSourceMuxer>(vhost, app, stream_id, _demuxer->getDurationMS() / 1000.0f, option);
|
2020-04-03 21:39:44 +08:00
|
|
|
|
auto tracks = _demuxer->getTracks(false);
|
2021-12-17 22:33:08 +08:00
|
|
|
|
if (tracks.empty()) {
|
2020-09-27 11:32:49 +08:00
|
|
|
|
throw std::runtime_error(StrPrinter << "该mp4文件没有有效的track:" << _file_path);
|
2020-04-03 21:39:44 +08:00
|
|
|
|
}
|
2021-12-17 22:33:08 +08:00
|
|
|
|
for (auto &track : tracks) {
|
2021-11-10 12:48:13 +08:00
|
|
|
|
_muxer->addTrack(track);
|
2021-12-17 22:33:08 +08:00
|
|
|
|
if (track->getTrackType() == TrackVideo) {
|
2020-04-03 20:45:58 +08:00
|
|
|
|
_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秒
|
2021-11-10 12:48:13 +08:00
|
|
|
|
_muxer->addTrackCompleted();
|
2020-04-03 20:45:58 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
bool MP4Reader::readSample() {
|
2021-09-29 00:04:36 +08:00
|
|
|
|
if (_paused) {
|
|
|
|
|
//确保暂停时,时间轴不走动
|
|
|
|
|
_seek_ticker.resetTime();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 21:39:44 +08:00
|
|
|
|
bool keyFrame = false;
|
2020-04-03 22:04:13 +08:00
|
|
|
|
bool eof = false;
|
2021-12-16 10:50:31 +08:00
|
|
|
|
while (!eof && _last_dts < getCurrentStamp()) {
|
2020-04-13 09:44:05 +08:00
|
|
|
|
auto frame = _demuxer->readFrame(keyFrame, eof);
|
2020-04-03 20:45:58 +08:00
|
|
|
|
if (!frame) {
|
2020-04-13 09:44:05 +08:00
|
|
|
|
continue;
|
2020-04-03 20:45:58 +08:00
|
|
|
|
}
|
2021-12-16 10:50:31 +08:00
|
|
|
|
_last_dts = frame->dts();
|
2021-12-15 20:50:35 +08:00
|
|
|
|
if (_muxer) {
|
|
|
|
|
_muxer->inputFrame(frame);
|
|
|
|
|
}
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 22:33:08 +08:00
|
|
|
|
GET_CONFIG(bool, file_repeat, Record::kFileRepeat);
|
|
|
|
|
if (eof && (file_repeat || _file_repeat)) {
|
2020-04-03 22:04:13 +08:00
|
|
|
|
//需要从头开始看
|
2020-04-03 20:45:58 +08:00
|
|
|
|
seekTo(0);
|
2020-04-04 16:08:31 +08:00
|
|
|
|
return true;
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 22:33:08 +08:00
|
|
|
|
bool MP4Reader::readNextSample() {
|
|
|
|
|
bool keyFrame = false;
|
|
|
|
|
bool eof = false;
|
|
|
|
|
auto frame = _demuxer->readFrame(keyFrame, eof);
|
|
|
|
|
if (!frame) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (_muxer) {
|
|
|
|
|
_muxer->inputFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
setCurrentStamp(frame->dts());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 20:50:35 +08:00
|
|
|
|
void MP4Reader::stopReadMP4() {
|
|
|
|
|
_timer = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 22:33:08 +08:00
|
|
|
|
void MP4Reader::startReadMP4(const EventPoller::Ptr &poller_in, uint64_t sample_ms, bool ref_self, bool file_repeat) {
|
2020-04-03 20:45:58 +08:00
|
|
|
|
GET_CONFIG(uint32_t, sampleMS, Record::kSampleMS);
|
2021-12-17 22:33:08 +08:00
|
|
|
|
auto strong_self = shared_from_this();
|
|
|
|
|
if (_muxer) {
|
|
|
|
|
_muxer->setMediaListener(strong_self);
|
|
|
|
|
//一直读到所有track就绪为止
|
|
|
|
|
while (!_muxer->isAllTrackReady() && readNextSample()) {}
|
|
|
|
|
}
|
2018-02-09 11:42:55 +08:00
|
|
|
|
|
2021-12-17 22:33:08 +08:00
|
|
|
|
//未指定线程,那么使用后台线程(读写文件采用后台线程)
|
|
|
|
|
auto poller = poller_in ? poller_in : WorkThreadPool::Instance().getPoller();
|
|
|
|
|
auto timer_sec = (sample_ms ? sample_ms : sampleMS) / 1000.0f;
|
2020-04-03 20:45:58 +08:00
|
|
|
|
|
|
|
|
|
//启动定时器
|
2021-12-15 20:50:35 +08:00
|
|
|
|
if (ref_self) {
|
2021-12-17 22:33:08 +08:00
|
|
|
|
_timer = std::make_shared<Timer>(timer_sec, [strong_self]() {
|
|
|
|
|
lock_guard<recursive_mutex> lck(strong_self->_mtx);
|
|
|
|
|
return strong_self->readSample();
|
|
|
|
|
}, poller);
|
2021-12-15 20:50:35 +08:00
|
|
|
|
} else {
|
2021-12-17 22:33:08 +08:00
|
|
|
|
weak_ptr<MP4Reader> weak_self = strong_self;
|
|
|
|
|
_timer = std::make_shared<Timer>(timer_sec, [weak_self]() {
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
2021-12-15 20:50:35 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-12-17 22:33:08 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(strong_self->_mtx);
|
|
|
|
|
return strong_self->readSample();
|
|
|
|
|
}, poller);
|
2021-12-15 20:50:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_file_repeat = file_repeat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MP4Demuxer::Ptr &MP4Reader::getDemuxer() const {
|
|
|
|
|
return _demuxer;
|
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() {
|
2021-12-17 22:33:08 +08:00
|
|
|
|
return (uint32_t) (_seek_to + !_paused * _speed * _seek_ticker.elapsedTime());
|
2018-02-02 18:06:08 +08:00
|
|
|
|
}
|
2020-04-03 20:45:58 +08:00
|
|
|
|
|
2021-12-17 22:33:08 +08:00
|
|
|
|
void MP4Reader::setCurrentStamp(uint32_t new_stamp) {
|
2021-09-29 00:04:36 +08:00
|
|
|
|
auto old_stamp = getCurrentStamp();
|
|
|
|
|
_seek_to = new_stamp;
|
2021-12-16 10:50:31 +08:00
|
|
|
|
_last_dts = new_stamp;
|
2020-04-03 20:45:58 +08:00
|
|
|
|
_seek_ticker.resetTime();
|
2021-12-15 20:50:35 +08:00
|
|
|
|
if (old_stamp != new_stamp && _muxer) {
|
2021-09-29 00:04:36 +08:00
|
|
|
|
//时间轴未拖动时不操作
|
2021-11-10 12:48:13 +08:00
|
|
|
|
_muxer->setTimeStamp(new_stamp);
|
2021-09-29 00:04:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MP4Reader::seekTo(MediaSource &sender, uint32_t stamp) {
|
|
|
|
|
//拖动进度条后应该恢复播放
|
|
|
|
|
pause(sender, false);
|
|
|
|
|
TraceL << getOriginUrl(sender) << ",stamp:" << stamp;
|
|
|
|
|
return seekTo(stamp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MP4Reader::pause(MediaSource &sender, bool pause) {
|
|
|
|
|
if (_paused == pause) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
//_seek_ticker重新计时,不管是暂停还是seek都不影响总的播放进度
|
|
|
|
|
setCurrentStamp(getCurrentStamp());
|
|
|
|
|
_paused = pause;
|
|
|
|
|
TraceL << getOriginUrl(sender) << ",pause:" << pause;
|
|
|
|
|
return true;
|
2018-02-06 10:56:58 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2021-09-29 00:04:36 +08:00
|
|
|
|
bool MP4Reader::speed(MediaSource &sender, float speed) {
|
|
|
|
|
if (speed < 0.1 && speed > 20) {
|
|
|
|
|
WarnL << "播放速度取值范围非法:" << speed;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
//设置播放速度后应该恢复播放
|
|
|
|
|
pause(sender, false);
|
|
|
|
|
if (_speed == speed) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
_speed = speed;
|
|
|
|
|
TraceL << getOriginUrl(sender) << ",speed:" << speed;
|
|
|
|
|
return true;
|
2019-12-28 16:48:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 22:33:08 +08:00
|
|
|
|
bool MP4Reader::seekTo(uint32_t stamp_seek) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(_mtx);
|
2021-12-17 22:33:08 +08:00
|
|
|
|
if (stamp_seek > _demuxer->getDurationMS()) {
|
2020-04-03 20:45:58 +08:00
|
|
|
|
//超过文件长度
|
|
|
|
|
return false;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-12-17 22:33:08 +08:00
|
|
|
|
auto stamp = _demuxer->seekTo(stamp_seek);
|
|
|
|
|
if (stamp == -1) {
|
2020-04-03 20:45:58 +08:00
|
|
|
|
//seek失败
|
|
|
|
|
return false;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2021-12-17 22:33:08 +08:00
|
|
|
|
if (!_have_video) {
|
|
|
|
|
//没有视频,不需要搜索关键帧;设置当前时间戳
|
|
|
|
|
setCurrentStamp((uint32_t) stamp);
|
2020-04-03 20:45:58 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
//搜索到下一帧关键帧
|
2020-04-03 21:39:44 +08:00
|
|
|
|
bool keyFrame = false;
|
2020-04-13 09:44:05 +08:00
|
|
|
|
bool eof = false;
|
|
|
|
|
while (!eof) {
|
|
|
|
|
auto frame = _demuxer->readFrame(keyFrame, eof);
|
2021-12-17 22:33:08 +08:00
|
|
|
|
if (!frame) {
|
2020-04-03 22:08:03 +08:00
|
|
|
|
//文件读完了都未找到下一帧关键帧
|
2020-04-13 09:44:05 +08:00
|
|
|
|
continue;
|
2020-04-03 20:45:58 +08:00
|
|
|
|
}
|
2021-12-17 22:33:08 +08:00
|
|
|
|
if (keyFrame || frame->keyFrame() || frame->configFrame()) {
|
2020-04-03 20:45:58 +08:00
|
|
|
|
//定位到key帧
|
2021-12-15 20:50:35 +08:00
|
|
|
|
if (_muxer) {
|
|
|
|
|
_muxer->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
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-13 09:44:05 +08:00
|
|
|
|
return false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 22:33:08 +08:00
|
|
|
|
bool MP4Reader::close(MediaSource &sender, bool force) {
|
|
|
|
|
if (!_muxer || (!force && _muxer->totalReaderCount())) {
|
2020-04-03 20:45:58 +08:00
|
|
|
|
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) {
|
2021-11-10 12:48:13 +08:00
|
|
|
|
return _muxer ? _muxer->totalReaderCount() : sender.readerCount();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 11:32:49 +08:00
|
|
|
|
MediaOriginType MP4Reader::getOriginType(MediaSource &sender) const {
|
|
|
|
|
return MediaOriginType::mp4_vod;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string MP4Reader::getOriginUrl(MediaSource &sender) const {
|
|
|
|
|
return _file_path;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 20:45:58 +08:00
|
|
|
|
} /* namespace mediakit */
|
2020-04-03 23:27:16 +08:00
|
|
|
|
#endif //ENABLE_MP4
|