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:
parent
f5a1f9b774
commit
522cbf1df8
4 changed files with 29 additions and 25 deletions
|
@ -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()
|
||||
|
|
|
@ -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<int, int>::iterator it;
|
||||
map<int, srs_utime_t>::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) {
|
||||
|
@ -77,8 +78,9 @@ 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;
|
||||
}
|
||||
|
|
|
@ -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<int, int> ticks;
|
||||
std::map<int, srs_utime_t> 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.
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue