mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
MP4: mp4开始点播确保MediaSource注册后再返回
This commit is contained in:
parent
0b2e55429d
commit
0feac7c924
@ -9,14 +9,16 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef ENABLE_MP4
|
#ifdef ENABLE_MP4
|
||||||
|
|
||||||
#include "MP4Reader.h"
|
#include "MP4Reader.h"
|
||||||
#include "Common/config.h"
|
#include "Common/config.h"
|
||||||
#include "Thread/WorkThreadPool.h"
|
#include "Thread/WorkThreadPool.h"
|
||||||
|
|
||||||
using namespace toolkit;
|
using namespace toolkit;
|
||||||
|
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
MP4Reader::MP4Reader(const string &vhost, const string &app, const string &stream_id, const string &file_path) {
|
MP4Reader::MP4Reader(const string &vhost, const string &app, const string &stream_id, const string &file_path) {
|
||||||
_poller = WorkThreadPool::Instance().getPoller();
|
|
||||||
_file_path = file_path;
|
_file_path = file_path;
|
||||||
if (_file_path.empty()) {
|
if (_file_path.empty()) {
|
||||||
GET_CONFIG(string, recordPath, Record::kFilePath);
|
GET_CONFIG(string, recordPath, Record::kFilePath);
|
||||||
@ -37,12 +39,12 @@ MP4Reader::MP4Reader(const string &vhost, const string &app, const string &strea
|
|||||||
}
|
}
|
||||||
_muxer = std::make_shared<MultiMediaSourceMuxer>(vhost, app, stream_id, _demuxer->getDurationMS() / 1000.0f, true, true, false, false);
|
_muxer = std::make_shared<MultiMediaSourceMuxer>(vhost, app, stream_id, _demuxer->getDurationMS() / 1000.0f, true, true, false, false);
|
||||||
auto tracks = _demuxer->getTracks(false);
|
auto tracks = _demuxer->getTracks(false);
|
||||||
if(tracks.empty()){
|
if (tracks.empty()) {
|
||||||
throw std::runtime_error(StrPrinter << "该mp4文件没有有效的track:" << _file_path);
|
throw std::runtime_error(StrPrinter << "该mp4文件没有有效的track:" << _file_path);
|
||||||
}
|
}
|
||||||
for(auto &track : tracks){
|
for (auto &track : tracks) {
|
||||||
_muxer->addTrack(track);
|
_muxer->addTrack(track);
|
||||||
if(track->getTrackType() == TrackVideo){
|
if (track->getTrackType() == TrackVideo) {
|
||||||
_have_video = true;
|
_have_video = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,8 +72,8 @@ bool MP4Reader::readSample() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GET_CONFIG(bool, fileRepeat, Record::kFileRepeat);
|
GET_CONFIG(bool, file_repeat, Record::kFileRepeat);
|
||||||
if (eof && (fileRepeat || _file_repeat)) {
|
if (eof && (file_repeat || _file_repeat)) {
|
||||||
//需要从头开始看
|
//需要从头开始看
|
||||||
seekTo(0);
|
seekTo(0);
|
||||||
return true;
|
return true;
|
||||||
@ -80,37 +82,53 @@ bool MP4Reader::readSample() {
|
|||||||
return !eof;
|
return !eof;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
void MP4Reader::stopReadMP4() {
|
void MP4Reader::stopReadMP4() {
|
||||||
_timer = nullptr;
|
_timer = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MP4Reader::startReadMP4(const EventPoller::Ptr &poller, uint64_t sample_ms, bool ref_self, bool file_repeat) {
|
void MP4Reader::startReadMP4(const EventPoller::Ptr &poller_in, uint64_t sample_ms, bool ref_self, bool file_repeat) {
|
||||||
GET_CONFIG(uint32_t, sampleMS, Record::kSampleMS);
|
GET_CONFIG(uint32_t, sampleMS, Record::kSampleMS);
|
||||||
auto strongSelf = shared_from_this();
|
auto strong_self = shared_from_this();
|
||||||
if (_muxer) { _muxer->setMediaListener(strongSelf); }
|
if (_muxer) {
|
||||||
|
_muxer->setMediaListener(strong_self);
|
||||||
|
//一直读到所有track就绪为止
|
||||||
|
while (!_muxer->isAllTrackReady() && readNextSample()) {}
|
||||||
|
}
|
||||||
|
|
||||||
//先获取关键帧
|
//未指定线程,那么使用后台线程(读写文件采用后台线程)
|
||||||
seekTo(0);
|
auto poller = poller_in ? poller_in : WorkThreadPool::Instance().getPoller();
|
||||||
//读sampleMS毫秒的数据用于产生MediaSource
|
auto timer_sec = (sample_ms ? sample_ms : sampleMS) / 1000.0f;
|
||||||
setCurrentStamp(getCurrentStamp() + sampleMS);
|
|
||||||
readSample();
|
|
||||||
|
|
||||||
//启动定时器
|
//启动定时器
|
||||||
if (ref_self) {
|
if (ref_self) {
|
||||||
_timer = std::make_shared<Timer>((sample_ms ? sample_ms : sampleMS) / 1000.0f, [strongSelf]() {
|
_timer = std::make_shared<Timer>(timer_sec, [strong_self]() {
|
||||||
lock_guard<recursive_mutex> lck(strongSelf->_mtx);
|
lock_guard<recursive_mutex> lck(strong_self->_mtx);
|
||||||
return strongSelf->readSample();
|
return strong_self->readSample();
|
||||||
}, poller ? poller : _poller);
|
}, poller);
|
||||||
} else {
|
} else {
|
||||||
weak_ptr<MP4Reader> weak_self = strongSelf;
|
weak_ptr<MP4Reader> weak_self = strong_self;
|
||||||
_timer = std::make_shared<Timer>((sample_ms ? sample_ms : sampleMS) / 1000.0f, [weak_self]() {
|
_timer = std::make_shared<Timer>(timer_sec, [weak_self]() {
|
||||||
auto strongSelf = weak_self.lock();
|
auto strong_self = weak_self.lock();
|
||||||
if (!strongSelf) {
|
if (!strong_self) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
lock_guard<recursive_mutex> lck(strongSelf->_mtx);
|
lock_guard<recursive_mutex> lck(strong_self->_mtx);
|
||||||
return strongSelf->readSample();
|
return strong_self->readSample();
|
||||||
}, poller ? poller : _poller);
|
}, poller);
|
||||||
}
|
}
|
||||||
|
|
||||||
_file_repeat = file_repeat;
|
_file_repeat = file_repeat;
|
||||||
@ -121,10 +139,10 @@ const MP4Demuxer::Ptr &MP4Reader::getDemuxer() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint32_t MP4Reader::getCurrentStamp() {
|
uint32_t MP4Reader::getCurrentStamp() {
|
||||||
return (uint32_t)(_seek_to + !_paused * _speed * _seek_ticker.elapsedTime());
|
return (uint32_t) (_seek_to + !_paused * _speed * _seek_ticker.elapsedTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
void MP4Reader::setCurrentStamp(uint32_t new_stamp){
|
void MP4Reader::setCurrentStamp(uint32_t new_stamp) {
|
||||||
auto old_stamp = getCurrentStamp();
|
auto old_stamp = getCurrentStamp();
|
||||||
_seek_to = new_stamp;
|
_seek_to = new_stamp;
|
||||||
_last_dts = new_stamp;
|
_last_dts = new_stamp;
|
||||||
@ -168,22 +186,21 @@ bool MP4Reader::speed(MediaSource &sender, float speed) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MP4Reader::seekTo(uint32_t ui32Stamp) {
|
bool MP4Reader::seekTo(uint32_t stamp_seek) {
|
||||||
lock_guard<recursive_mutex> lck(_mtx);
|
lock_guard<recursive_mutex> lck(_mtx);
|
||||||
if (ui32Stamp > _demuxer->getDurationMS()) {
|
if (stamp_seek > _demuxer->getDurationMS()) {
|
||||||
//超过文件长度
|
//超过文件长度
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto stamp = _demuxer->seekTo(ui32Stamp);
|
auto stamp = _demuxer->seekTo(stamp_seek);
|
||||||
if(stamp == -1){
|
if (stamp == -1) {
|
||||||
//seek失败
|
//seek失败
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!_have_video){
|
if (!_have_video) {
|
||||||
//没有视频,不需要搜索关键帧
|
//没有视频,不需要搜索关键帧;设置当前时间戳
|
||||||
//设置当前时间戳
|
setCurrentStamp((uint32_t) stamp);
|
||||||
setCurrentStamp((uint32_t)stamp);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//搜索到下一帧关键帧
|
//搜索到下一帧关键帧
|
||||||
@ -191,11 +208,11 @@ bool MP4Reader::seekTo(uint32_t ui32Stamp) {
|
|||||||
bool eof = false;
|
bool eof = false;
|
||||||
while (!eof) {
|
while (!eof) {
|
||||||
auto frame = _demuxer->readFrame(keyFrame, eof);
|
auto frame = _demuxer->readFrame(keyFrame, eof);
|
||||||
if(!frame){
|
if (!frame) {
|
||||||
//文件读完了都未找到下一帧关键帧
|
//文件读完了都未找到下一帧关键帧
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(keyFrame || frame->keyFrame() || frame->configFrame()){
|
if (keyFrame || frame->keyFrame() || frame->configFrame()) {
|
||||||
//定位到key帧
|
//定位到key帧
|
||||||
if (_muxer) {
|
if (_muxer) {
|
||||||
_muxer->inputFrame(frame);
|
_muxer->inputFrame(frame);
|
||||||
@ -208,8 +225,8 @@ bool MP4Reader::seekTo(uint32_t ui32Stamp) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MP4Reader::close(MediaSource &sender,bool force){
|
bool MP4Reader::close(MediaSource &sender, bool force) {
|
||||||
if(!_muxer || (!force && _muxer->totalReaderCount())){
|
if (!_muxer || (!force && _muxer->totalReaderCount())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
_timer.reset();
|
_timer.reset();
|
||||||
|
@ -16,27 +16,37 @@
|
|||||||
using namespace toolkit;
|
using namespace toolkit;
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
class MP4Reader : public std::enable_shared_from_this<MP4Reader> ,public MediaSourceEvent{
|
class MP4Reader : public std::enable_shared_from_this<MP4Reader>, public MediaSourceEvent {
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<MP4Reader> Ptr;
|
using Ptr = std::shared_ptr<MP4Reader>;
|
||||||
virtual ~MP4Reader() = default;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流化一个mp4文件,使之转换成RtspMediaSource和RtmpMediaSource
|
* 点播一个mp4文件,使之转换成MediaSource流媒体
|
||||||
* @param vhost 虚拟主机
|
* @param vhost 虚拟主机
|
||||||
* @param app 应用名
|
* @param app 应用名
|
||||||
* @param stream_id 流id
|
* @param stream_id 流id,置空时,只解复用mp4,但是不生成MediaSource
|
||||||
* @param file_path 文件路径,如果为空则根据配置文件和上面参数自动生成,否则使用指定的文件
|
* @param file_path 文件路径,如果为空则根据配置文件和上面参数自动生成,否则使用指定的文件
|
||||||
*/
|
*/
|
||||||
MP4Reader(const string &vhost, const string &app, const string &stream_id, const string &file_path = "");
|
MP4Reader(const string &vhost, const string &app, const string &stream_id, const string &file_path = "");
|
||||||
|
~MP4Reader() override = default;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开始流化MP4文件,需要指出的是,MP4Reader对象一经过调用startReadMP4方法,它的强引用会自持有,
|
* 开始解复用MP4文件
|
||||||
* 意思是在文件流化结束之前或中断之前,MP4Reader对象是不会被销毁的(不管有没有被外部对象持有)
|
* @param poller 解复用mp4定时器所绑定线程,置空则随机采用一条后台线程
|
||||||
|
* @param sample_ms 每次读取文件数据量,单位毫秒,置0时采用配置文件配置
|
||||||
|
* @param ref_self 是否让定时器引用此对象本身,如果无其他对象引用本身,在不循环读文件时,读取文件结束后本对象将自动销毁
|
||||||
|
* @param file_repeat 是否循环读取文件,如果配置文件设置为循环读文件,此参数无效
|
||||||
*/
|
*/
|
||||||
void startReadMP4(const EventPoller::Ptr &poller = nullptr, uint64_t sample_ms = 0, bool ref_self = true, bool file_repeat = false);
|
void startReadMP4(const EventPoller::Ptr &poller = nullptr, uint64_t sample_ms = 0, bool ref_self = true, bool file_repeat = false);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止解复用MP4定时器
|
||||||
|
*/
|
||||||
void stopReadMP4();
|
void stopReadMP4();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取mp4解复用器
|
||||||
|
*/
|
||||||
const MP4Demuxer::Ptr& getDemuxer() const;
|
const MP4Demuxer::Ptr& getDemuxer() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -51,9 +61,10 @@ private:
|
|||||||
string getOriginUrl(MediaSource &sender) const override;
|
string getOriginUrl(MediaSource &sender) const override;
|
||||||
|
|
||||||
bool readSample();
|
bool readSample();
|
||||||
|
bool readNextSample();
|
||||||
uint32_t getCurrentStamp();
|
uint32_t getCurrentStamp();
|
||||||
void setCurrentStamp(uint32_t ui32Stamp);
|
void setCurrentStamp(uint32_t stamp);
|
||||||
bool seekTo(uint32_t ui32Stamp);
|
bool seekTo(uint32_t stamp_seek);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _file_repeat = false;
|
bool _file_repeat = false;
|
||||||
@ -61,12 +72,11 @@ private:
|
|||||||
bool _paused = false;
|
bool _paused = false;
|
||||||
float _speed = 1.0;
|
float _speed = 1.0;
|
||||||
uint32_t _last_dts = 0;
|
uint32_t _last_dts = 0;
|
||||||
uint32_t _seek_to;
|
uint32_t _seek_to = 0;
|
||||||
string _file_path;
|
string _file_path;
|
||||||
recursive_mutex _mtx;
|
recursive_mutex _mtx;
|
||||||
Ticker _seek_ticker;
|
Ticker _seek_ticker;
|
||||||
Timer::Ptr _timer;
|
Timer::Ptr _timer;
|
||||||
EventPoller::Ptr _poller;
|
|
||||||
MP4Demuxer::Ptr _demuxer;
|
MP4Demuxer::Ptr _demuxer;
|
||||||
MultiMediaSourceMuxer::Ptr _muxer;
|
MultiMediaSourceMuxer::Ptr _muxer;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user