From 572ece93bed4e3063dbbc8354a61dd90534f2e9c Mon Sep 17 00:00:00 2001
From: igorshevach <igorshevach@gmail.com>
Date: Mon, 6 Nov 2023 13:31:42 +0200
Subject: [PATCH] - add hiccup simulation to file streamer

---
 transcoder/config_v.json         |  4 +++-
 transcoder/debug/file_streamer.c | 25 ++++++++++++++++++++++---
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/transcoder/config_v.json b/transcoder/config_v.json
index b0450bab..bde615a1 100644
--- a/transcoder/config_v.json
+++ b/transcoder/config_v.json
@@ -5,7 +5,9 @@
         "activeStream": 0,
         "xduration": 9000000,
         "randomDataPercentage": 0,
-        "jumpOffsetSec": -60
+        "jumpOffsetSec": -60,
+        "hiccupIntervalSec": 0,
+        "hiccupDurationSec": 0
     },
     "throttler": {
         "maxDataRate": 1.5,
diff --git a/transcoder/debug/file_streamer.c b/transcoder/debug/file_streamer.c
index 31223ae8..1e0671af 100644
--- a/transcoder/debug/file_streamer.c
+++ b/transcoder/debug/file_streamer.c
@@ -49,9 +49,14 @@ void* thread_stream_from_file(void *vargp)
     char channelId[KMP_MAX_CHANNEL_ID];
     json_get_string(GetConfig(),"input.channelId","1_abcdefgh",channelId,sizeof(channelId));
 
-    int jumpOffsetSec=0;
+    int64_t jumpOffsetSec=0;
     json_get_int64(GetConfig(),"input.jumpoffsetsec",0,&jumpOffsetSec);
 
+    int64_t hiccupDurationSec, hiccupIntervalSec;
+    json_get_int64(GetConfig(),"input.hiccupDurationSec",0,&hiccupDurationSec);
+
+    json_get_int64(GetConfig(),"input.hiccupIntervalSec",0,&hiccupIntervalSec);
+
     AVPacket packet;
     av_init_packet(&packet);
 
@@ -86,8 +91,10 @@ void* thread_stream_from_file(void *vargp)
     LOGGER("SENDER",AV_LOG_INFO,"Realtime = %s",realTime ? "true" : "false");
     srand((int)time(NULL));
     uint64_t lastDts=0;
-    int64_t start_time=av_gettime_relative();
-
+    int64_t start_time=av_gettime_relative(),
+            hiccup_duration =  hiccupDurationSec * 1000 * 1000,
+            hiccup_interval = hiccupIntervalSec * 1000 * 1000,
+            next_hiccup = start_time + hiccup_interval;
 
     samples_stats_t stats;
     sample_stats_init(&stats,standard_timebase);
@@ -144,6 +151,18 @@ void* thread_stream_from_file(void *vargp)
 
             int64_t timePassed=av_rescale_q(packet.dts,standard_timebase,AV_TIME_BASE_Q) + start_time,
                     clockPassed = av_gettime_relative();
+
+           if(clockPassed >= next_hiccup && clockPassed < next_hiccup + hiccup_duration) {
+               next_hiccup += hiccup_duration;
+               LOGGER("SENDER",AV_LOG_INFO,"hiccup! [ %ld - %ld ]",
+                ts2str((clockPassed - start_time) * 90,true),
+                ts2str((next_hiccup  - start_time) * 90,true));
+
+               av_usleep(next_hiccup - clockPassed);
+
+               next_hiccup = av_gettime_relative() + hiccup_interval;
+           }
+
             LOGGER("SENDER",AV_LOG_DEBUG,"XXXX clockPassed=%ld timePassed=%ld", clockPassed - start_time,timePassed - start_time);
             while (clockPassed < timePassed) {