From 9b7c0802a9d85501f861bad22f122336c1615fdc Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 10 Apr 2019 08:32:46 +0800 Subject: [PATCH] Refine srs_update_system_time in time unit --- trunk/src/app/srs_app_bandwidth.cpp | 14 +++++++------- trunk/src/app/srs_app_config.cpp | 6 +++--- trunk/src/app/srs_app_config.hpp | 2 +- trunk/src/app/srs_app_dash.cpp | 2 +- trunk/src/app/srs_app_dvr.cpp | 8 ++------ trunk/src/app/srs_app_dvr.hpp | 4 ++-- trunk/src/app/srs_app_hls.cpp | 2 +- trunk/src/app/srs_app_http_hooks.cpp | 4 ++-- trunk/src/app/srs_app_recv_thread.cpp | 2 +- trunk/src/app/srs_app_server.cpp | 4 ++-- trunk/src/kernel/srs_kernel_utility.cpp | 10 +++++----- trunk/src/kernel/srs_kernel_utility.hpp | 4 +++- trunk/src/libs/srs_lib_bandwidth.cpp | 10 +++++----- trunk/src/libs/srs_librtmp.cpp | 2 +- trunk/src/main/srs_main_ingest_hls.cpp | 13 +++++++------ trunk/src/utest/srs_utest_config.cpp | 18 ++++++++++++++++++ trunk/src/utest/srs_utest_kernel.cpp | 6 +++--- 17 files changed, 64 insertions(+), 47 deletions(-) diff --git a/trunk/src/app/srs_app_bandwidth.cpp b/trunk/src/app/srs_app_bandwidth.cpp index 5bc17e587..8fb851df8 100644 --- a/trunk/src/app/srs_app_bandwidth.cpp +++ b/trunk/src/app/srs_app_bandwidth.cpp @@ -147,7 +147,7 @@ srs_error_t SrsBandwidth::bandwidth_check(SrsRtmpServer* rtmp, ISrsProtocolStati static srs_utime_t last_check_time = 0; srs_utime_t interval = _srs_config->get_bw_check_interval(_req->vhost); - srs_utime_t time_now = srs_update_system_time_ms() * SRS_UTIME_MILLISECONDS; + srs_utime_t time_now = srs_update_system_time(); // reject the connection in the interval window. if (last_check_time > 0 && time_now - last_check_time < interval) { _rtmp->response_connect_reject(_req, "bandcheck rejected"); @@ -184,7 +184,7 @@ srs_error_t SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) _rtmp->set_recv_timeout(publish_sample.duration_ms * 2); // start test. - srs_update_system_time_ms(); + srs_update_system_time(); int64_t start_time = srs_get_system_time_ms(); // sample play @@ -210,7 +210,7 @@ srs_error_t SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) } // stop test. - srs_update_system_time_ms(); + srs_update_system_time(); int64_t end_time = srs_get_system_time_ms(); srs_trace("bandwidth ok. duartion=%dms(%d+%d), play=%dkbps, publish=%dkbps", @@ -261,7 +261,7 @@ srs_error_t SrsBandwidth::play_checking(SrsBandwidthSample* sample, SrsKbpsLimit memset(random_data, 'A', size); int data_count = 1; - srs_update_system_time_ms(); + srs_update_system_time(); int64_t starttime = srs_get_system_time_ms(); while ((srs_get_system_time_ms() - starttime) < sample->duration_ms) { srs_usleep(sample->interval_ms); @@ -284,7 +284,7 @@ srs_error_t SrsBandwidth::play_checking(SrsBandwidthSample* sample, SrsKbpsLimit limit->send_limit(); } - srs_update_system_time_ms(); + srs_update_system_time(); sample->calc_kbps((int)_rtmp->get_send_bytes(), (int)(srs_get_system_time_ms() - starttime)); return err; @@ -344,7 +344,7 @@ srs_error_t SrsBandwidth::publish_checking(SrsBandwidthSample* sample, SrsKbpsLi srs_error_t err = srs_success; // recv publish msgs until @duration_ms ms - srs_update_system_time_ms(); + srs_update_system_time(); int64_t starttime = srs_get_system_time_ms(); while ((srs_get_system_time_ms() - starttime) < sample->duration_ms) { SrsCommonMessage* msg = NULL; @@ -363,7 +363,7 @@ srs_error_t SrsBandwidth::publish_checking(SrsBandwidthSample* sample, SrsKbpsLi limit->recv_limit(); } - srs_update_system_time_ms(); + srs_update_system_time(); sample->calc_kbps((int)_rtmp->get_recv_bytes(), (int)(srs_get_system_time_ms() - starttime)); return err; diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index d1886df59..9bb15ae7c 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -6524,9 +6524,9 @@ string SrsConfig::get_dvr_plan(string vhost) return conf->arg0(); } -int SrsConfig::get_dvr_duration(string vhost) +srs_utime_t SrsConfig::get_dvr_duration(string vhost) { - static int DEFAULT = 30; + static srs_utime_t DEFAULT = 30 * SRS_UTIME_SECONDS; SrsConfDirective* conf = get_dvr(vhost); if (!conf) { @@ -6538,7 +6538,7 @@ int SrsConfig::get_dvr_duration(string vhost) return DEFAULT; } - return ::atoi(conf->arg0().c_str()); + return (srs_utime_t)(::atoi(conf->arg0().c_str()) * SRS_UTIME_SECONDS); } bool SrsConfig::get_dvr_wait_keyframe(string vhost) diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 6cb1833b8..35ca1dc62 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -1313,7 +1313,7 @@ public: /** * get the duration of dvr flv. */ - virtual int get_dvr_duration(std::string vhost); + virtual srs_utime_t get_dvr_duration(std::string vhost); /** * whether wait keyframe to reap segment. */ diff --git a/trunk/src/app/srs_app_dash.cpp b/trunk/src/app/srs_app_dash.cpp index 4dbcad533..3e74134c3 100644 --- a/trunk/src/app/srs_app_dash.cpp +++ b/trunk/src/app/srs_app_dash.cpp @@ -265,7 +265,7 @@ srs_error_t SrsMpdWriter::get_fragment(bool video, std::string& home, std::strin home = fragment_home; - sn = srs_update_system_time_ms() * SRS_UTIME_MILLISECONDS / fragment; + sn = srs_update_system_time() / fragment; basetime = sn * srsu2ms(fragment); if (video) { diff --git a/trunk/src/app/srs_app_dvr.cpp b/trunk/src/app/srs_app_dvr.cpp index 1cdd07b10..4db4ff9eb 100644 --- a/trunk/src/app/srs_app_dvr.cpp +++ b/trunk/src/app/srs_app_dvr.cpp @@ -740,7 +740,7 @@ void SrsDvrSessionPlan::on_unpublish() SrsDvrSegmentPlan::SrsDvrSegmentPlan() { - cduration = -1; + cduration = 0; wait_keyframe = false; } @@ -759,8 +759,6 @@ srs_error_t SrsDvrSegmentPlan::initialize(SrsOriginHub* h, SrsDvrSegmenter* s, S wait_keyframe = _srs_config->get_dvr_wait_keyframe(req->vhost); cduration = _srs_config->get_dvr_duration(req->vhost); - // to ms - cduration *= 1000; return srs_success; } @@ -833,7 +831,7 @@ srs_error_t SrsDvrSegmentPlan::update_duration(SrsSharedPtrMessage* msg) // ignore if duration ok. SrsFragment* fragment = segment->current(); - if (cduration <= 0 || fragment->duration() < cduration) { + if (cduration <= 0 || fragment->duration() < int64_t(srsu2ms(cduration))) { return err; } @@ -881,8 +879,6 @@ srs_error_t SrsDvrSegmentPlan::on_reload_vhost_dvr(string vhost) wait_keyframe = _srs_config->get_dvr_wait_keyframe(req->vhost); cduration = _srs_config->get_dvr_duration(req->vhost); - // to ms - cduration *= 1000; return err; } diff --git a/trunk/src/app/srs_app_dvr.hpp b/trunk/src/app/srs_app_dvr.hpp index bcfc67d0a..453f52e86 100644 --- a/trunk/src/app/srs_app_dvr.hpp +++ b/trunk/src/app/srs_app_dvr.hpp @@ -227,8 +227,8 @@ public: class SrsDvrSegmentPlan : public SrsDvrPlan { private: - // in config, in ms - int cduration; + // in config, in srs_utime_t + srs_utime_t cduration; bool wait_keyframe; public: SrsDvrSegmentPlan(); diff --git a/trunk/src/app/srs_app_hls.cpp b/trunk/src/app/srs_app_hls.cpp index 8f6451c69..0b9058a52 100644 --- a/trunk/src/app/srs_app_hls.cpp +++ b/trunk/src/app/srs_app_hls.cpp @@ -390,7 +390,7 @@ srs_error_t SrsHlsMuxer::segment_open() ts_file = srs_path_build_stream(ts_file, req->vhost, req->app, req->stream); if (hls_ts_floor) { // accept the floor ts for the first piece. - int64_t current_floor_ts = (int64_t)(srs_update_system_time_ms() / (1000 * hls_fragment)); + int64_t current_floor_ts = (int64_t)(srsu2ms(srs_update_system_time()) / (1000 * hls_fragment)); if (!accept_floor_ts) { accept_floor_ts = current_floor_ts - 1; } else { diff --git a/trunk/src/app/srs_app_http_hooks.cpp b/trunk/src/app/srs_app_http_hooks.cpp index 5d9a5e8ed..d58be2829 100644 --- a/trunk/src/app/srs_app_http_hooks.cpp +++ b/trunk/src/app/srs_app_http_hooks.cpp @@ -363,7 +363,7 @@ srs_error_t SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* re url = srs_string_replace(url, "[ts_url]", ts_url); url = srs_string_replace(url, "[param]", req->param); - int64_t starttime = srs_update_system_time_ms(); + int64_t starttime = srsu2ms(srs_update_system_time()); SrsHttpUri uri; if ((err = uri.initialize(url)) != srs_success) { @@ -405,7 +405,7 @@ srs_error_t SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* re nb_read += nb_bytes; } - int spenttime = (int)(srs_update_system_time_ms() - starttime); + int spenttime = (int)(srsu2ms(srs_update_system_time()) - starttime); srs_trace("http hook on_hls_notify success. client_id=%d, url=%s, code=%d, spent=%dms, read=%dB, err=%s", client_id, url.c_str(), msg->status_code(), spenttime, nb_read, srs_error_desc(err).c_str()); diff --git a/trunk/src/app/srs_app_recv_thread.cpp b/trunk/src/app/srs_app_recv_thread.cpp index af331f269..b2753ff70 100644 --- a/trunk/src/app/srs_app_recv_thread.cpp +++ b/trunk/src/app/srs_app_recv_thread.cpp @@ -381,7 +381,7 @@ srs_error_t SrsPublishRecvThread::consume(SrsCommonMessage* msg) // log to show the time of recv thread. srs_verbose("recv thread now=%" PRId64 "us, got msg time=%" PRId64 "ms, size=%d", - srs_update_system_time_ms(), msg->header.timestamp, msg->size); + srs_update_system_time(), msg->header.timestamp, msg->size); // the rtmp connection will handle this message err = _conn->handle_publish_message(_source, msg); diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index f3990e0bb..f16fed09b 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -542,7 +542,7 @@ srs_error_t SrsServer::initialize(ISrsServerCycle* ch) srs_error_t err = srs_success; // ensure the time is ok. - srs_update_system_time_ms(); + srs_update_system_time(); // for the main objects(server, config, log, context), // never subscribe handler in constructor, @@ -971,7 +971,7 @@ srs_error_t SrsServer::do_cycle() // update the cache time if ((i % SRS_SYS_TIME_RESOLUTION_MS_TIMES) == 0) { srs_info("update current time cache."); - srs_update_system_time_ms(); + srs_update_system_time(); } if ((i % SRS_SYS_RUSAGE_RESOLUTION_TIMES) == 0) { diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp index c0dc2bd07..b00a1740a 100644 --- a/trunk/src/kernel/srs_kernel_utility.cpp +++ b/trunk/src/kernel/srs_kernel_utility.cpp @@ -107,7 +107,7 @@ int64_t _srs_system_time_startup_time = 0; int64_t srs_get_system_time_ms() { if (_srs_system_time_us_cache <= 0) { - srs_update_system_time_ms(); + srs_update_system_time(); } return _srs_system_time_us_cache / 1000; @@ -116,13 +116,13 @@ int64_t srs_get_system_time_ms() int64_t srs_get_system_startup_time_ms() { if (_srs_system_time_startup_time <= 0) { - srs_update_system_time_ms(); + srs_update_system_time(); } return _srs_system_time_startup_time / 1000; } -int64_t srs_update_system_time_ms() +srs_utime_t srs_update_system_time() { timeval now; @@ -143,7 +143,7 @@ int64_t srs_update_system_time_ms() // so we use relative time. if (_srs_system_time_us_cache <= 0) { _srs_system_time_startup_time = _srs_system_time_us_cache = now_us; - return _srs_system_time_us_cache / 1000; + return _srs_system_time_us_cache; } // use relative time. @@ -158,7 +158,7 @@ int64_t srs_update_system_time_ms() _srs_system_time_us_cache = now_us; srs_info("clock updated, startup=%" PRId64 "us, now=%" PRId64 "us", _srs_system_time_startup_time, _srs_system_time_us_cache); - return _srs_system_time_us_cache / 1000; + return _srs_system_time_us_cache; } string srs_dns_resolve(string host, int& family) diff --git a/trunk/src/kernel/srs_kernel_utility.hpp b/trunk/src/kernel/srs_kernel_utility.hpp index 13c264bd9..36e67cbb8 100644 --- a/trunk/src/kernel/srs_kernel_utility.hpp +++ b/trunk/src/kernel/srs_kernel_utility.hpp @@ -29,6 +29,8 @@ #include #include +#include + class SrsBuffer; class SrsBitBuffer; @@ -44,7 +46,7 @@ extern srs_error_t srs_avc_nalu_read_bit(SrsBitBuffer* stream, int8_t& v); extern int64_t srs_get_system_time_ms(); extern int64_t srs_get_system_startup_time_ms(); // the deamon st-thread will update it. -extern int64_t srs_update_system_time_ms(); +extern srs_utime_t srs_update_system_time(); // the any address for listener, // it's "0.0.0.0" for ipv4, and "::" for ipv6. diff --git a/trunk/src/libs/srs_lib_bandwidth.cpp b/trunk/src/libs/srs_lib_bandwidth.cpp index 69b0de289..e24672ddf 100644 --- a/trunk/src/libs/srs_lib_bandwidth.cpp +++ b/trunk/src/libs/srs_lib_bandwidth.cpp @@ -137,7 +137,7 @@ int SrsBandwidthClient::bandwidth_check( ) { int ret = ERROR_SUCCESS; - srs_update_system_time_ms(); + srs_update_system_time(); *start_time = srs_get_system_time_ms(); // play @@ -193,7 +193,7 @@ int SrsBandwidthClient::bandwidth_check( } } - srs_update_system_time_ms(); + srs_update_system_time(); *end_time = srs_get_system_time_ms(); return ret; @@ -313,7 +313,7 @@ int SrsBandwidthClient::publish_checking(int duration_ms, int play_kbps) } int data_count = 1; - srs_update_system_time_ms(); + srs_update_system_time(); int64_t starttime = srs_get_system_time_ms(); while ((srs_get_system_time_ms() - starttime) < duration_ms) { // TODO: FIXME: use shared ptr message. @@ -336,12 +336,12 @@ int SrsBandwidthClient::publish_checking(int duration_ms, int play_kbps) } // use the play kbps to control the publish - srs_update_system_time_ms(); + srs_update_system_time(); int elaps = (int)(srs_get_system_time_ms() - starttime); if (elaps > 0) { int current_kbps = (int)(_rtmp->get_send_bytes() * 8 / elaps); while (current_kbps > play_kbps) { - srs_update_system_time_ms(); + srs_update_system_time(); elaps = (int)(srs_get_system_time_ms() - starttime); current_kbps = (int)(_rtmp->get_send_bytes() * 8 / elaps); usleep(100 * 1000); // TODO: FIXME: magic number. diff --git a/trunk/src/libs/srs_librtmp.cpp b/trunk/src/libs/srs_librtmp.cpp index 2bfbbcea0..a07088838 100644 --- a/trunk/src/libs/srs_librtmp.cpp +++ b/trunk/src/libs/srs_librtmp.cpp @@ -2212,7 +2212,7 @@ void srs_amf0_strict_array_append(srs_amf0_t amf0, srs_amf0_t value) int64_t srs_utils_time_ms() { - return srs_update_system_time_ms(); + return srs_update_system_time(); } int64_t srs_utils_send_bytes(srs_rtmp_t rtmp) diff --git a/trunk/src/main/srs_main_ingest_hls.cpp b/trunk/src/main/srs_main_ingest_hls.cpp index 11f3acace..77a7a0036 100644 --- a/trunk/src/main/srs_main_ingest_hls.cpp +++ b/trunk/src/main/srs_main_ingest_hls.cpp @@ -46,6 +46,7 @@ using namespace std; #include #include #include +#include // pre-declare srs_error_t proxy_hls2rtmp(std::string hls, std::string rtmp); @@ -154,7 +155,7 @@ private: private: SrsHttpUri* in_hls; std::vector pieces; - int64_t next_connect_time; + srs_utime_t next_connect_time; private: SrsTsContext* context; public: @@ -213,10 +214,10 @@ int SrsIngestHlsInput::connect() { int ret = ERROR_SUCCESS; - int64_t now = srs_update_system_time_ms(); + srs_utime_t now = srs_update_system_time(); if (now < next_connect_time) { - srs_trace("input hls wait for %dms", next_connect_time - now); - srs_usleep((next_connect_time - now) * SRS_UTIME_MILLISECONDS); + srs_trace("input hls wait for %dms", srsu2msi(next_connect_time - now)); + srs_usleep(next_connect_time - now); } // set all ts to dirty. @@ -559,7 +560,7 @@ int SrsIngestHlsInput::fetch_all_ts(bool fresh_m3u8) // only wait for a duration of last piece. if (i == (int)pieces.size() - 1) { - next_connect_time = srs_update_system_time_ms() + (int)tp->duration * SRS_UTIME_MILLISECONDS; + next_connect_time = srs_update_system_time() + tp->duration * SRS_UTIME_SECONDS; } } @@ -696,7 +697,7 @@ SrsIngestHlsOutput::SrsIngestHlsOutput(SrsHttpUri* rtmp) { out_rtmp = rtmp; disconnected = false; - raw_aac_dts = srs_update_system_time_ms(); + raw_aac_dts = srsu2ms(srs_update_system_time()); req = NULL; sdk = NULL; diff --git a/trunk/src/utest/srs_utest_config.cpp b/trunk/src/utest/srs_utest_config.cpp index adb092a0c..811e9d0b6 100644 --- a/trunk/src/utest/srs_utest_config.cpp +++ b/trunk/src/utest/srs_utest_config.cpp @@ -29,6 +29,8 @@ using namespace std; #include #include #include +#include +#include MockSrsConfigBuffer::MockSrsConfigBuffer(string buf) { @@ -1856,5 +1858,21 @@ VOID TEST(ConfigUnitTest, CheckDefaultValues) EXPECT_EQ(100 * SRS_UTIME_MILLISECONDS, conf.get_publish_1stpkt_timeout("v")); EXPECT_EQ(100 * SRS_UTIME_MILLISECONDS, conf.get_publish_normal_timeout("v")); } + + if (true) { + EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF)); + EXPECT_EQ(30 * SRS_UTIME_SECONDS, conf.get_dvr_duration("")); + + EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF"vhost v{dvr{dvr_duration 10;}}")); + EXPECT_EQ(10 * SRS_UTIME_SECONDS, conf.get_dvr_duration("v")); + } + + if (true) { + srs_utime_t t0 = srs_update_system_time(); + srs_usleep(10 * SRS_UTIME_MILLISECONDS); + srs_utime_t t1 = srs_update_system_time(); + + EXPECT_TRUE(t1 - t0 >= 10 * SRS_UTIME_MILLISECONDS); + } } diff --git a/trunk/src/utest/srs_utest_kernel.cpp b/trunk/src/utest/srs_utest_kernel.cpp index b68d15b51..dea42914b 100644 --- a/trunk/src/utest/srs_utest_kernel.cpp +++ b/trunk/src/utest/srs_utest_kernel.cpp @@ -1489,7 +1489,7 @@ VOID TEST(KernelUtilityTest, UtilityTime) EXPECT_EQ(time, time1); usleep(1000); - srs_update_system_time_ms(); + srs_update_system_time(); time1 = srs_get_system_time_ms(); EXPECT_TRUE(time1 > time); } @@ -1506,7 +1506,7 @@ VOID TEST(KernelUtilityTest, UtilityStartupTime) EXPECT_EQ(time, time1); usleep(1000); - srs_update_system_time_ms(); + srs_update_system_time(); time1 = srs_get_system_startup_time_ms(); EXPECT_EQ(time, time1); } @@ -3185,7 +3185,7 @@ VOID TEST(KernelUtilityTest, CoverTimeUtilityAll) EXPECT_TRUE(srs_get_system_startup_time_ms() > 0); _srs_system_time_us_cache -= 300*1000 * 1000 + 1; - EXPECT_TRUE(srs_update_system_time_ms() > 0); + EXPECT_TRUE(srs_update_system_time() > 0); if (true) { string host;