From 4cb96bda20ff862cac7624b0e477259c5f35b800 Mon Sep 17 00:00:00 2001 From: chundonglinlin Date: Wed, 17 May 2023 10:38:02 +0800 Subject: [PATCH] Refine rtmp publish timestamp algorithm. (#44) --- src/app/htl_app_rtmp_publish.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/app/htl_app_rtmp_publish.cpp b/src/app/htl_app_rtmp_publish.cpp index 5d03ecf..5b3baa2 100644 --- a/src/app/htl_app_rtmp_publish.cpp +++ b/src/app/htl_app_rtmp_publish.cpp @@ -36,6 +36,18 @@ using namespace std; #include #include +#include +int64_t srs_system_time_us() +{ + timeval now; + + if (gettimeofday(&now, NULL) < 0) { + return -1; + } + + return ((int64_t)now.tv_sec) * 1000 * 1000 + (int64_t)now.tv_usec; +} + StRtmpPublishClient::StRtmpPublishClient(){ stream_id = 0; srs = NULL; @@ -153,6 +165,7 @@ int StRtmpPublishClient::PublishAV(srs_flv_t flv, return ret; } + int64_t start_us = srs_system_time_us(); // open flv and publish to server. u_int32_t re = 0; while(true){ @@ -222,10 +235,10 @@ int StRtmpPublishClient::PublishAV(srs_flv_t flv, if (re <= 0) { re = timestamp; } - - if (timestamp - re > 300) { - st_usleep((timestamp - re) * 1000); - re = timestamp; + + int64_t now_us = srs_system_time_us(); + if ((timestamp - re) * 1000 - (now_us - start_us) > 300000) { + st_usleep((timestamp - re) * 1000 - (now_us - start_us)); } }