mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-21 16:32:22 +08:00
完善线程安全设计
This commit is contained in:
@@ -20,6 +20,8 @@ using namespace toolkit;
|
||||
namespace mediakit {
|
||||
|
||||
MP4Reader::MP4Reader(const string &vhost, const string &app, const string &stream_id, const string &file_path) {
|
||||
//读写文件建议放在后台线程
|
||||
_poller = WorkThreadPool::Instance().getPoller();
|
||||
_file_path = file_path;
|
||||
if (_file_path.empty()) {
|
||||
GET_CONFIG(string, recordPath, Record::kFilePath);
|
||||
@@ -105,7 +107,7 @@ void MP4Reader::stopReadMP4() {
|
||||
_timer = nullptr;
|
||||
}
|
||||
|
||||
void MP4Reader::startReadMP4(const EventPoller::Ptr &poller_in, uint64_t sample_ms, bool ref_self, bool file_repeat) {
|
||||
void MP4Reader::startReadMP4(uint64_t sample_ms, bool ref_self, bool file_repeat) {
|
||||
GET_CONFIG(uint32_t, sampleMS, Record::kSampleMS);
|
||||
auto strong_self = shared_from_this();
|
||||
if (_muxer) {
|
||||
@@ -114,8 +116,6 @@ void MP4Reader::startReadMP4(const EventPoller::Ptr &poller_in, uint64_t sample_
|
||||
while (!_muxer->isAllTrackReady() && readNextSample()) {}
|
||||
}
|
||||
|
||||
//未指定线程,那么使用后台线程(读写文件采用后台线程)
|
||||
auto poller = poller_in ? poller_in : WorkThreadPool::Instance().getPoller();
|
||||
auto timer_sec = (sample_ms ? sample_ms : sampleMS) / 1000.0f;
|
||||
|
||||
//启动定时器
|
||||
@@ -123,7 +123,7 @@ void MP4Reader::startReadMP4(const EventPoller::Ptr &poller_in, uint64_t sample_
|
||||
_timer = std::make_shared<Timer>(timer_sec, [strong_self]() {
|
||||
lock_guard<recursive_mutex> lck(strong_self->_mtx);
|
||||
return strong_self->readSample();
|
||||
}, poller);
|
||||
}, _poller);
|
||||
} else {
|
||||
weak_ptr<MP4Reader> weak_self = strong_self;
|
||||
_timer = std::make_shared<Timer>(timer_sec, [weak_self]() {
|
||||
@@ -133,7 +133,7 @@ void MP4Reader::startReadMP4(const EventPoller::Ptr &poller_in, uint64_t sample_
|
||||
}
|
||||
lock_guard<recursive_mutex> lck(strong_self->_mtx);
|
||||
return strong_self->readSample();
|
||||
}, poller);
|
||||
}, _poller);
|
||||
}
|
||||
|
||||
_file_repeat = file_repeat;
|
||||
@@ -251,5 +251,9 @@ string MP4Reader::getOriginUrl(MediaSource &sender) const {
|
||||
return _file_path;
|
||||
}
|
||||
|
||||
toolkit::EventPoller::Ptr MP4Reader::getOwnerPoller(MediaSource &sender) {
|
||||
return _poller;
|
||||
}
|
||||
|
||||
} /* namespace mediakit */
|
||||
#endif //ENABLE_MP4
|
||||
@@ -33,12 +33,11 @@ public:
|
||||
|
||||
/**
|
||||
* 开始解复用MP4文件
|
||||
* @param poller 解复用mp4定时器所绑定线程,置空则随机采用一条后台线程
|
||||
* @param sample_ms 每次读取文件数据量,单位毫秒,置0时采用配置文件配置
|
||||
* @param ref_self 是否让定时器引用此对象本身,如果无其他对象引用本身,在不循环读文件时,读取文件结束后本对象将自动销毁
|
||||
* @param file_repeat 是否循环读取文件,如果配置文件设置为循环读文件,此参数无效
|
||||
*/
|
||||
void startReadMP4(const toolkit::EventPoller::Ptr &poller = nullptr, uint64_t sample_ms = 0, bool ref_self = true, bool file_repeat = false);
|
||||
void startReadMP4(uint64_t sample_ms = 0, bool ref_self = true, bool file_repeat = false);
|
||||
|
||||
/**
|
||||
* 停止解复用MP4定时器
|
||||
@@ -60,6 +59,7 @@ private:
|
||||
int totalReaderCount(MediaSource &sender) override;
|
||||
MediaOriginType getOriginType(MediaSource &sender) const override;
|
||||
std::string getOriginUrl(MediaSource &sender) const override;
|
||||
toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override;
|
||||
|
||||
bool readSample();
|
||||
bool readNextSample();
|
||||
@@ -80,6 +80,7 @@ private:
|
||||
toolkit::Timer::Ptr _timer;
|
||||
MP4Demuxer::Ptr _demuxer;
|
||||
MultiMediaSourceMuxer::Ptr _muxer;
|
||||
toolkit::EventPoller::Ptr _poller;
|
||||
};
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
Reference in New Issue
Block a user