1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

refine code, rename the sync call to common class.

This commit is contained in:
winlin 2015-05-23 07:57:45 +08:00
parent 0bb90145ba
commit 2f0ef87d6d
6 changed files with 43 additions and 41 deletions

View file

@ -31,65 +31,65 @@ using namespace std;
// the sleep interval for http async callback. // the sleep interval for http async callback.
#define SRS_AUTO_ASYNC_CALLBACL_SLEEP_US 300000 #define SRS_AUTO_ASYNC_CALLBACL_SLEEP_US 300000
ISrsDvrAsyncCall::ISrsDvrAsyncCall() ISrsAsyncCallTask::ISrsAsyncCallTask()
{ {
} }
ISrsDvrAsyncCall::~ISrsDvrAsyncCall() ISrsAsyncCallTask::~ISrsAsyncCallTask()
{ {
} }
SrsDvrAsyncCallThread::SrsDvrAsyncCallThread() SrsAsyncCallWorker::SrsAsyncCallWorker()
{ {
pthread = new SrsThread("async", this, SRS_AUTO_ASYNC_CALLBACL_SLEEP_US, true); pthread = new SrsThread("async", this, SRS_AUTO_ASYNC_CALLBACL_SLEEP_US, true);
} }
SrsDvrAsyncCallThread::~SrsDvrAsyncCallThread() SrsAsyncCallWorker::~SrsAsyncCallWorker()
{ {
stop(); stop();
srs_freep(pthread); srs_freep(pthread);
std::vector<ISrsDvrAsyncCall*>::iterator it; std::vector<ISrsAsyncCallTask*>::iterator it;
for (it = callbacks.begin(); it != callbacks.end(); ++it) { for (it = tasks.begin(); it != tasks.end(); ++it) {
ISrsDvrAsyncCall* call = *it; ISrsAsyncCallTask* task = *it;
srs_freep(call); srs_freep(task);
} }
callbacks.clear(); tasks.clear();
} }
int SrsDvrAsyncCallThread::call(ISrsDvrAsyncCall* c) int SrsAsyncCallWorker::execute(ISrsAsyncCallTask* t)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
callbacks.push_back(c); tasks.push_back(t);
return ret; return ret;
} }
int SrsDvrAsyncCallThread::start() int SrsAsyncCallWorker::start()
{ {
return pthread->start(); return pthread->start();
} }
void SrsDvrAsyncCallThread::stop() void SrsAsyncCallWorker::stop()
{ {
pthread->stop(); pthread->stop();
} }
int SrsDvrAsyncCallThread::cycle() int SrsAsyncCallWorker::cycle()
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
std::vector<ISrsDvrAsyncCall*> copies = callbacks; std::vector<ISrsAsyncCallTask*> copies = tasks;
callbacks.clear(); tasks.clear();
std::vector<ISrsDvrAsyncCall*>::iterator it; std::vector<ISrsAsyncCallTask*>::iterator it;
for (it = copies.begin(); it != copies.end(); ++it) { for (it = copies.begin(); it != copies.end(); ++it) {
ISrsDvrAsyncCall* call = *it; ISrsAsyncCallTask* task = *it;
if ((ret = call->call()) != ERROR_SUCCESS) { if ((ret = task->call()) != ERROR_SUCCESS) {
srs_warn("ignore async callback %s, ret=%d", call->to_string().c_str(), ret); srs_warn("ignore async callback %s, ret=%d", task->to_string().c_str(), ret);
} }
srs_freep(call); srs_freep(task);
} }
return ret; return ret;

View file

@ -42,11 +42,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* a video and pass it to the dvr again. * a video and pass it to the dvr again.
* futhurmore, the aync call never block the main worker thread. * futhurmore, the aync call never block the main worker thread.
*/ */
class ISrsDvrAsyncCall class ISrsAsyncCallTask
{ {
public: public:
ISrsDvrAsyncCall(); ISrsAsyncCallTask();
virtual ~ISrsDvrAsyncCall(); virtual ~ISrsAsyncCallTask();
public: public:
virtual int call() = 0; virtual int call() = 0;
virtual std::string to_string() = 0; virtual std::string to_string() = 0;
@ -54,17 +54,19 @@ public:
/** /**
* the async callback for dvr. * the async callback for dvr.
* when worker call with the task, the worker will do it in isolate thread.
* that is, the task is execute/call in async mode.
*/ */
class SrsDvrAsyncCallThread : public ISrsThreadHandler class SrsAsyncCallWorker : public ISrsThreadHandler
{ {
private: private:
SrsThread* pthread; SrsThread* pthread;
std::vector<ISrsDvrAsyncCall*> callbacks; std::vector<ISrsAsyncCallTask*> tasks;
public: public:
SrsDvrAsyncCallThread(); SrsAsyncCallWorker();
virtual ~SrsDvrAsyncCallThread(); virtual ~SrsAsyncCallWorker();
public: public:
virtual int call(ISrsDvrAsyncCall* c); virtual int execute(ISrsAsyncCallTask* t);
public: public:
virtual int start(); virtual int start();
virtual void stop(); virtual void stop();

View file

@ -548,7 +548,7 @@ SrsDvrPlan::SrsDvrPlan()
dvr_enabled = false; dvr_enabled = false;
segment = new SrsFlvSegment(this); segment = new SrsFlvSegment(this);
async = new SrsDvrAsyncCallThread(); async = new SrsAsyncCallWorker();
} }
SrsDvrPlan::~SrsDvrPlan() SrsDvrPlan::~SrsDvrPlan()
@ -629,7 +629,7 @@ int SrsDvrPlan::on_reap_segment()
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
if ((ret = async->call(new SrsDvrAsyncCallOnDvr(req, segment->get_path()))) != ERROR_SUCCESS) { if ((ret = async->execute(new SrsDvrAsyncCallOnDvr(req, segment->get_path()))) != ERROR_SUCCESS) {
return ret; return ret;
} }

View file

@ -178,7 +178,7 @@ public:
/** /**
* the dvr async call. * the dvr async call.
*/ */
class SrsDvrAsyncCallOnDvr : public ISrsDvrAsyncCall class SrsDvrAsyncCallOnDvr : public ISrsAsyncCallTask
{ {
private: private:
std::string path; std::string path;
@ -206,7 +206,7 @@ public:
SrsRequest* req; SrsRequest* req;
protected: protected:
SrsFlvSegment* segment; SrsFlvSegment* segment;
SrsDvrAsyncCallThread* async; SrsAsyncCallWorker* async;
bool dvr_enabled; bool dvr_enabled;
public: public:
SrsDvrPlan(); SrsDvrPlan();

View file

@ -286,7 +286,7 @@ SrsHlsMuxer::SrsHlsMuxer()
acodec = SrsCodecAudioReserved1; acodec = SrsCodecAudioReserved1;
should_write_cache = false; should_write_cache = false;
should_write_file = true; should_write_file = true;
async = new SrsDvrAsyncCallThread(); async = new SrsAsyncCallWorker();
context = new SrsTsContext(); context = new SrsTsContext();
} }
@ -669,7 +669,7 @@ int SrsHlsMuxer::segment_close(string log_desc)
segments.push_back(current); segments.push_back(current);
// use async to call the http hooks, for it will cause thread switch. // use async to call the http hooks, for it will cause thread switch.
if ((ret = async->call(new SrsDvrAsyncCallOnHls(req, if ((ret = async->execute(new SrsDvrAsyncCallOnHls(req,
current->full_path, current->uri, m3u8, m3u8_url, current->full_path, current->uri, m3u8, m3u8_url,
current->sequence_no, current->duration))) != ERROR_SUCCESS) current->sequence_no, current->duration))) != ERROR_SUCCESS)
{ {
@ -677,7 +677,7 @@ int SrsHlsMuxer::segment_close(string log_desc)
} }
// use async to call the http hooks, for it will cause thread switch. // use async to call the http hooks, for it will cause thread switch.
if ((ret = async->call(new SrsDvrAsyncCallOnHlsNotify(req, current->uri))) != ERROR_SUCCESS) { if ((ret = async->execute(new SrsDvrAsyncCallOnHlsNotify(req, current->uri))) != ERROR_SUCCESS) {
return ret; return ret;
} }

View file

@ -159,7 +159,7 @@ public:
/** /**
* the hls async call: on_hls * the hls async call: on_hls
*/ */
class SrsDvrAsyncCallOnHls : public ISrsDvrAsyncCall class SrsDvrAsyncCallOnHls : public ISrsAsyncCallTask
{ {
private: private:
std::string path; std::string path;
@ -180,7 +180,7 @@ public:
/** /**
* the hls async call: on_hls_notify * the hls async call: on_hls_notify
*/ */
class SrsDvrAsyncCallOnHlsNotify : public ISrsDvrAsyncCall class SrsDvrAsyncCallOnHlsNotify : public ISrsAsyncCallTask
{ {
private: private:
std::string ts_url; std::string ts_url;
@ -215,7 +215,7 @@ private:
double hls_aof_ratio; double hls_aof_ratio;
double hls_fragment; double hls_fragment;
double hls_window; double hls_window;
SrsDvrAsyncCallThread* async; SrsAsyncCallWorker* async;
private: private:
// whether use floor algorithm for timestamp. // whether use floor algorithm for timestamp.
bool hls_ts_floor; bool hls_ts_floor;