Skip to content

Commit

Permalink
Refine rtmp publish timestamp algorithm. (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
chundonglinlin committed May 17, 2023
1 parent 1330c5d commit 4cb96bd
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/app/htl_app_rtmp_publish.cpp
Expand Up @@ -36,6 +36,18 @@ using namespace std;
#include <htl_app_rtmp_publish.hpp>
#include <srs_librtmp.h>

#include <sys/time.h>
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;
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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));
}
}

Expand Down

0 comments on commit 4cb96bd

Please sign in to comment.