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

Refine SrsHourGlass in time unit.

This commit is contained in:
winlin 2019-04-18 08:11:16 +08:00
parent f5a1f9b774
commit 522cbf1df8
4 changed files with 29 additions and 25 deletions

View file

@ -52,13 +52,15 @@ void SrsFragment::append(int64_t dts)
dts = 0; dts = 0;
} }
srs_utime_t dts_in_tbn = dts * SRS_UTIME_MILLISECONDS;
if (start_dts == -1) { if (start_dts == -1) {
start_dts = dts * SRS_UTIME_MILLISECONDS; start_dts = dts_in_tbn;
} }
// TODO: FIXME: Use cumulus dts. // TODO: FIXME: Use cumulus dts.
start_dts = srs_min(start_dts, dts * SRS_UTIME_MILLISECONDS); start_dts = srs_min(start_dts, dts_in_tbn);
dur = dts - start_dts; dur = dts_in_tbn - start_dts;
} }
srs_utime_t SrsFragment::duration() srs_utime_t SrsFragment::duration()

View file

@ -37,10 +37,10 @@ ISrsHourGlass::~ISrsHourGlass()
{ {
} }
SrsHourGlass::SrsHourGlass(ISrsHourGlass* h, int resolution_ms) SrsHourGlass::SrsHourGlass(ISrsHourGlass* h, srs_utime_t resolution)
{ {
handler = h; handler = h;
resolution = resolution_ms; _resolution = resolution;
total_elapse = 0; 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; srs_error_t err = srs_success;
if (resolution > 0 && (interval % resolution) != 0) { if (_resolution > 0 && (interval % _resolution) != 0) {
return srs_error_new(ERROR_SYSTEM_HOURGLASS_RESOLUTION, "hourglass interval=%d invalid, resolution=%d", interval, resolution); return srs_error_new(ERROR_SYSTEM_HOURGLASS_RESOLUTION,
"invalid interval=%dms, resolution=%dms", srsu2msi(interval), srsu2msi(_resolution));
} }
ticks[type] = interval; ticks[type] = interval;
@ -65,10 +66,10 @@ srs_error_t SrsHourGlass::cycle()
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
map<int, int>::iterator it; map<int, srs_utime_t>::iterator it;
for (it = ticks.begin(); it != ticks.end(); ++it) { for (it = ticks.begin(); it != ticks.end(); ++it) {
int type = it->first; int type = it->first;
int interval = it->second; srs_utime_t interval = it->second;
if (interval == 0 || (total_elapse % interval) == 0) { if (interval == 0 || (total_elapse % interval) == 0) {
if ((err = handler->notify(type, interval, total_elapse)) != srs_success) { if ((err = handler->notify(type, interval, total_elapse)) != srs_success) {
@ -77,8 +78,9 @@ srs_error_t SrsHourGlass::cycle()
} }
} }
total_elapse += resolution; // TODO: FIXME: Maybe we should use wallclock.
srs_usleep(resolution * 1000); total_elapse += _resolution;
srs_usleep(_resolution);
return err; return err;
} }

View file

@ -40,7 +40,7 @@ public:
/** /**
* notify the handler, the type and tick. * 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. * this is used for server and bocar server and other manager.
* *
* Usage: * Usage:
* SrsHourGlass* hg = new SrsHourGlass(handler, 1000); * SrsHourGlass* hg = new SrsHourGlass(handler, 1 * SRS_UTIME_MILLISECONDS);
* hg->tick(1, 3000); * hg->tick(1, 3 * SRS_UTIME_MILLISECONDS);
* hg->tick(2, 5000); * hg->tick(2, 5 * SRS_UTIME_MILLISECONDS);
* hg->tick(3, 7000); * hg->tick(3, 7 * SRS_UTIME_MILLISECONDS);
* // create a thread to cycle, which will call handerl when ticked. * // create a thread to cycle, which will call handerl when ticked.
* while (true) { * while (true) {
* hg->cycle(); * hg->cycle();
@ -73,21 +73,21 @@ class SrsHourGlass
{ {
private: private:
ISrsHourGlass* handler; ISrsHourGlass* handler;
int resolution; srs_utime_t _resolution;
// key: the type of tick. // key: the type of tick.
// value: the interval of tick. // value: the interval of tick.
std::map<int, int> ticks; std::map<int, srs_utime_t> ticks;
// the total elapsed time, // the total elapsed time,
// for each cycle, we increase it with a resolution. // for each cycle, we increase it with a resolution.
int64_t total_elapse; srs_utime_t total_elapse;
public: public:
SrsHourGlass(ISrsHourGlass* h, int resolution_ms); SrsHourGlass(ISrsHourGlass* h, srs_utime_t resolution);
virtual ~SrsHourGlass(); virtual ~SrsHourGlass();
public: public:
// add a pair of tick(type, interval). // add a pair of tick(type, interval).
// @param type the type of tick. // @param type the type of tick.
// @param interval the interval in ms of tick. // @param interval the interval in srs_utime_t of tick.
virtual srs_error_t tick(int type, int interval); virtual srs_error_t tick(int type, srs_utime_t interval);
public: public:
// cycle the hourglass, which will sleep resolution every time. // cycle the hourglass, which will sleep resolution every time.
// and call handler when ticked. // and call handler when ticked.

View file

@ -45,7 +45,7 @@ using namespace std;
#define SRS_HTTP_READ_BUFFER 4096 #define SRS_HTTP_READ_BUFFER 4096
#define SRS_HTTP_BODY_BUFFER (32 * 1024) #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) #define SRS_HLS_NOTIFY_TIMEOUT (10 * SRS_UTIME_SECONDS)
SrsHttpHooks::SrsHttpHooks() SrsHttpHooks::SrsHttpHooks()