From 522cbf1df8964486a095e3568076bed2e03b451c Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 18 Apr 2019 08:11:16 +0800 Subject: [PATCH] Refine SrsHourGlass in time unit. --- trunk/src/app/srs_app_fragment.cpp | 8 +++++--- trunk/src/app/srs_app_hourglass.cpp | 22 ++++++++++++---------- trunk/src/app/srs_app_hourglass.hpp | 22 +++++++++++----------- trunk/src/app/srs_app_http_hooks.cpp | 2 +- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/trunk/src/app/srs_app_fragment.cpp b/trunk/src/app/srs_app_fragment.cpp index efadb9022..bea0ae128 100644 --- a/trunk/src/app/srs_app_fragment.cpp +++ b/trunk/src/app/srs_app_fragment.cpp @@ -52,13 +52,15 @@ void SrsFragment::append(int64_t dts) dts = 0; } + srs_utime_t dts_in_tbn = dts * SRS_UTIME_MILLISECONDS; + if (start_dts == -1) { - start_dts = dts * SRS_UTIME_MILLISECONDS; + start_dts = dts_in_tbn; } // TODO: FIXME: Use cumulus dts. - start_dts = srs_min(start_dts, dts * SRS_UTIME_MILLISECONDS); - dur = dts - start_dts; + start_dts = srs_min(start_dts, dts_in_tbn); + dur = dts_in_tbn - start_dts; } srs_utime_t SrsFragment::duration() diff --git a/trunk/src/app/srs_app_hourglass.cpp b/trunk/src/app/srs_app_hourglass.cpp index 84e0e78e5..f95a9c01a 100644 --- a/trunk/src/app/srs_app_hourglass.cpp +++ b/trunk/src/app/srs_app_hourglass.cpp @@ -37,10 +37,10 @@ ISrsHourGlass::~ISrsHourGlass() { } -SrsHourGlass::SrsHourGlass(ISrsHourGlass* h, int resolution_ms) +SrsHourGlass::SrsHourGlass(ISrsHourGlass* h, srs_utime_t resolution) { handler = h; - resolution = resolution_ms; + _resolution = resolution; total_elapse = 0; } @@ -48,12 +48,13 @@ SrsHourGlass::~SrsHourGlass() { } -srs_error_t SrsHourGlass::tick(int type, int interval) +srs_error_t SrsHourGlass::tick(int type, srs_utime_t interval) { srs_error_t err = srs_success; - if (resolution > 0 && (interval % resolution) != 0) { - return srs_error_new(ERROR_SYSTEM_HOURGLASS_RESOLUTION, "hourglass interval=%d invalid, resolution=%d", interval, resolution); + if (_resolution > 0 && (interval % _resolution) != 0) { + return srs_error_new(ERROR_SYSTEM_HOURGLASS_RESOLUTION, + "invalid interval=%dms, resolution=%dms", srsu2msi(interval), srsu2msi(_resolution)); } ticks[type] = interval; @@ -65,10 +66,10 @@ srs_error_t SrsHourGlass::cycle() { srs_error_t err = srs_success; - map::iterator it; + map::iterator it; for (it = ticks.begin(); it != ticks.end(); ++it) { int type = it->first; - int interval = it->second; + srs_utime_t interval = it->second; if (interval == 0 || (total_elapse % interval) == 0) { if ((err = handler->notify(type, interval, total_elapse)) != srs_success) { @@ -76,9 +77,10 @@ srs_error_t SrsHourGlass::cycle() } } } - - total_elapse += resolution; - srs_usleep(resolution * 1000); + + // TODO: FIXME: Maybe we should use wallclock. + total_elapse += _resolution; + srs_usleep(_resolution); return err; } diff --git a/trunk/src/app/srs_app_hourglass.hpp b/trunk/src/app/srs_app_hourglass.hpp index 241d212cc..d5b18e799 100644 --- a/trunk/src/app/srs_app_hourglass.hpp +++ b/trunk/src/app/srs_app_hourglass.hpp @@ -40,7 +40,7 @@ public: /** * notify the handler, the type and tick. */ - virtual srs_error_t notify(int type, int interval, int64_t tick) = 0; + virtual srs_error_t notify(int type, srs_utime_t interval, srs_utime_t tick) = 0; }; /** @@ -60,10 +60,10 @@ public: * this is used for server and bocar server and other manager. * * Usage: - * SrsHourGlass* hg = new SrsHourGlass(handler, 1000); - * hg->tick(1, 3000); - * hg->tick(2, 5000); - * hg->tick(3, 7000); + * SrsHourGlass* hg = new SrsHourGlass(handler, 1 * SRS_UTIME_MILLISECONDS); + * hg->tick(1, 3 * SRS_UTIME_MILLISECONDS); + * hg->tick(2, 5 * SRS_UTIME_MILLISECONDS); + * hg->tick(3, 7 * SRS_UTIME_MILLISECONDS); * // create a thread to cycle, which will call handerl when ticked. * while (true) { * hg->cycle(); @@ -73,21 +73,21 @@ class SrsHourGlass { private: ISrsHourGlass* handler; - int resolution; + srs_utime_t _resolution; // key: the type of tick. // value: the interval of tick. - std::map ticks; + std::map ticks; // the total elapsed time, // for each cycle, we increase it with a resolution. - int64_t total_elapse; + srs_utime_t total_elapse; public: - SrsHourGlass(ISrsHourGlass* h, int resolution_ms); + SrsHourGlass(ISrsHourGlass* h, srs_utime_t resolution); virtual ~SrsHourGlass(); public: // add a pair of tick(type, interval). // @param type the type of tick. - // @param interval the interval in ms of tick. - virtual srs_error_t tick(int type, int interval); + // @param interval the interval in srs_utime_t of tick. + virtual srs_error_t tick(int type, srs_utime_t interval); public: // cycle the hourglass, which will sleep resolution every time. // and call handler when ticked. diff --git a/trunk/src/app/srs_app_http_hooks.cpp b/trunk/src/app/srs_app_http_hooks.cpp index fd95130ed..7437fe68c 100644 --- a/trunk/src/app/srs_app_http_hooks.cpp +++ b/trunk/src/app/srs_app_http_hooks.cpp @@ -45,7 +45,7 @@ using namespace std; #define SRS_HTTP_READ_BUFFER 4096 #define SRS_HTTP_BODY_BUFFER (32 * 1024) -// the timeout for hls notify, in ms. +// the timeout for hls notify, in srs_utime_t. #define SRS_HLS_NOTIFY_TIMEOUT (10 * SRS_UTIME_SECONDS) SrsHttpHooks::SrsHttpHooks()