完善线程安全设计

This commit is contained in:
xiongziliang
2022-08-27 10:17:06 +08:00
parent 8b0bd2d224
commit c2ab45f78d
15 changed files with 89 additions and 59 deletions

View File

@@ -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