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

fix #109: fix the system jump time, adjust system startup time. 0.9.135

This commit is contained in:
winlin 2014-06-27 13:35:28 +08:00
parent 7de95c12e0
commit b357504b1d
6 changed files with 48 additions and 11 deletions

View file

@ -33,7 +33,13 @@ using namespace std;
#include <srs_kernel_log.hpp>
// this value must:
// equals to (SRS_SYS_CYCLE_INTERVAL*SRS_SYS_TIME_RESOLUTION_MS_TIMES)*1000
// @see SRS_SYS_TIME_RESOLUTION_MS_TIMES
#define SYS_TIME_RESOLUTION_US 300*1000
static int64_t _srs_system_time_us_cache = 0;
static int64_t _srs_system_time_startup_time = 0;
int64_t srs_get_system_time_ms()
{
@ -43,7 +49,14 @@ int64_t srs_get_system_time_ms()
return _srs_system_time_us_cache / 1000;
}
int64_t srs_get_system_startup_time_ms()
{
if (_srs_system_time_startup_time <= 0) {
srs_update_system_time_ms();
}
return _srs_system_time_startup_time / 1000;
}
void srs_update_system_time_ms()
{
timeval now;
@ -56,12 +69,32 @@ void srs_update_system_time_ms()
// @see: https://github.com/winlinvip/simple-rtmp-server/issues/35
// we must convert the tv_sec/tv_usec to int64_t.
int64_t now_us = ((int64_t)now.tv_sec) * 1000 * 1000 + (int64_t)now.tv_usec;
if (now_us < _srs_system_time_us_cache) {
srs_warn("system time negative, "
"history=%"PRId64"us, now=%"PRId64"", _srs_system_time_us_cache, now_us);
// for some ARM os, the starttime maybe invalid,
// for example, on the cubieboard2, the srs_startup_time is 1262304014640,
// while now is 1403842979210 in ms, diff is 141538964570 ms, 1638 days
// it's impossible, and maybe the problem of startup time is invalid.
// use date +%s to get system time is 1403844851.
// so we use relative time.
if (_srs_system_time_us_cache <= 0) {
_srs_system_time_us_cache = now_us;
_srs_system_time_startup_time = now_us;
return;
}
// use relative time.
int64_t diff = now_us - _srs_system_time_us_cache;
diff = srs_max(0, diff);
if (diff < 0 || diff > 1000 * SYS_TIME_RESOLUTION_US) {
srs_warn("system time jump, history=%"PRId64"us, now=%"PRId64"us, diff=%"PRId64"us",
_srs_system_time_us_cache, now_us, diff);
// @see: https://github.com/winlinvip/simple-rtmp-server/issues/109
_srs_system_time_startup_time += diff;
}
_srs_system_time_us_cache = now_us;
srs_info("system time updated, startup=%"PRId64"us, now=%"PRId64"us",
_srs_system_time_startup_time, _srs_system_time_us_cache);
}
string srs_dns_resolve(string host)