Refine srs_update_system_time in time unit

pull/1651/head
winlin 6 years ago
parent 9b1222a79c
commit 9b7c0802a9

@ -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;

@ -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)

@ -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.
*/

@ -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) {

@ -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;
}

@ -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();

@ -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 {

@ -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());

@ -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);

@ -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) {

@ -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)

@ -29,6 +29,8 @@
#include <string>
#include <vector>
#include <srs_core_time.hpp>
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.

@ -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.

@ -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)

@ -46,6 +46,7 @@ using namespace std;
#include <srs_service_http_conn.hpp>
#include <srs_service_rtmp_conn.hpp>
#include <srs_service_utility.hpp>
#include <srs_core_time.hpp>
// pre-declare
srs_error_t proxy_hls2rtmp(std::string hls, std::string rtmp);
@ -154,7 +155,7 @@ private:
private:
SrsHttpUri* in_hls;
std::vector<SrsTsPiece*> 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;

@ -29,6 +29,8 @@ using namespace std;
#include <srs_kernel_error.hpp>
#include <srs_app_source.hpp>
#include <srs_core_performance.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_service_st.hpp>
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);
}
}

@ -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;

Loading…
Cancel
Save