Skip to content

Commit 4cb96bd

Browse files
Refine rtmp publish timestamp algorithm. (#44)
1 parent 1330c5d commit 4cb96bd

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/app/htl_app_rtmp_publish.cpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ using namespace std;
3636
#include <htl_app_rtmp_publish.hpp>
3737
#include <srs_librtmp.h>
3838

39+
#include <sys/time.h>
40+
int64_t srs_system_time_us()
41+
{
42+
timeval now;
43+
44+
if (gettimeofday(&now, NULL) < 0) {
45+
return -1;
46+
}
47+
48+
return ((int64_t)now.tv_sec) * 1000 * 1000 + (int64_t)now.tv_usec;
49+
}
50+
3951
StRtmpPublishClient::StRtmpPublishClient(){
4052
stream_id = 0;
4153
srs = NULL;
@@ -153,6 +165,7 @@ int StRtmpPublishClient::PublishAV(srs_flv_t flv,
153165
return ret;
154166
}
155167

168+
int64_t start_us = srs_system_time_us();
156169
// open flv and publish to server.
157170
u_int32_t re = 0;
158171
while(true){
@@ -222,10 +235,10 @@ int StRtmpPublishClient::PublishAV(srs_flv_t flv,
222235
if (re <= 0) {
223236
re = timestamp;
224237
}
225-
226-
if (timestamp - re > 300) {
227-
st_usleep((timestamp - re) * 1000);
228-
re = timestamp;
238+
239+
int64_t now_us = srs_system_time_us();
240+
if ((timestamp - re) * 1000 - (now_us - start_us) > 300000) {
241+
st_usleep((timestamp - re) * 1000 - (now_us - start_us));
229242
}
230243
}
231244

0 commit comments

Comments
 (0)