通过减少线程切换提高服务器性能

This commit is contained in:
xiongziliang
2019-03-21 22:28:12 +08:00
parent 6238f5aa58
commit b78c14a2a3
9 changed files with 33 additions and 65 deletions

View File

@@ -38,7 +38,7 @@ FlvMuxer::FlvMuxer() {
FlvMuxer::~FlvMuxer() {
}
void FlvMuxer::start(const RtmpMediaSource::Ptr &media) {
void FlvMuxer::start(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media) {
if(!media){
throw std::runtime_error("RtmpMediaSource 无效");
}
@@ -46,7 +46,7 @@ void FlvMuxer::start(const RtmpMediaSource::Ptr &media) {
onWriteFlvHeader(media);
std::weak_ptr<FlvMuxer> weakSelf = getSharedPtr();
_ring_reader = media->getRing()->attach();
_ring_reader = media->getRing()->attach(poller);
_ring_reader->setDetachCB([weakSelf](){
auto strongSelf = weakSelf.lock();
if(!strongSelf){
@@ -189,11 +189,11 @@ void FlvMuxer::stop() {
}
///////////////////////////////////////////////////////FlvRecorder/////////////////////////////////////////////////////
void FlvRecorder::startRecord(const string &vhost, const string &app, const string &stream,const string &file_path) {
startRecord(dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA,vhost,app,stream,false)),file_path);
void FlvRecorder::startRecord(const EventPoller::Ptr &poller,const string &vhost, const string &app, const string &stream,const string &file_path) {
startRecord(poller,dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA,vhost,app,stream,false)),file_path);
}
void FlvRecorder::startRecord(const RtmpMediaSource::Ptr &media, const string &file_path) {
void FlvRecorder::startRecord(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media, const string &file_path) {
stop();
lock_guard<recursive_mutex> lck(_file_mtx);
//开辟文件写缓存
@@ -215,7 +215,7 @@ void FlvRecorder::startRecord(const RtmpMediaSource::Ptr &media, const string &f
//设置文件写缓存
setvbuf( _file.get(), fileBuf.get(),_IOFBF, FILE_BUF_SIZE);
start(media);
start(poller,media);
}
void FlvRecorder::onWrite(const Buffer::Ptr &data) {

View File

@@ -41,7 +41,7 @@ public:
virtual ~FlvMuxer();
void stop();
protected:
void start(const RtmpMediaSource::Ptr &media);
void start(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media);
virtual void onWrite(const Buffer::Ptr &data) = 0;
virtual void onWrite(const char *data,int len) = 0;
virtual void onDetach() = 0;
@@ -55,6 +55,7 @@ private:
RtmpMediaSource::RingType::RingReader::Ptr _ring_reader;
uint32_t _aui32FirstStamp[2] = {0};
uint32_t _previousTagSize = 0;
};
class FlvRecorder : public FlvMuxer , public std::enable_shared_from_this<FlvRecorder>{
@@ -62,8 +63,8 @@ public:
typedef std::shared_ptr<FlvRecorder> Ptr;
FlvRecorder();
virtual ~FlvRecorder();
void startRecord(const string &vhost,const string &app,const string &stream,const string &file_path);
void startRecord(const RtmpMediaSource::Ptr &media,const string &file_path);
void startRecord(const EventPoller::Ptr &poller,const string &vhost,const string &app,const string &stream,const string &file_path);
void startRecord(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media,const string &file_path);
private:
virtual void onWrite(const Buffer::Ptr &data) override ;
virtual void onWrite(const char *data,int len) override;