From 4cbc4560512f31947cbf0011af9d7f5566787b43 Mon Sep 17 00:00:00 2001
From: Julian Rother <julianr@fsmpi.rwth-aachen.de>
Date: Mon, 27 Nov 2017 16:07:58 +0100
Subject: [PATCH] transcode: Ping api at least every 30 seconds

---
 transcode.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/transcode.c b/transcode.c
index 175f913..090adcb 100644
--- a/transcode.c
+++ b/transcode.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <unistd.h>
+#include <time.h>
 #include <libavutil/opt.h>
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
@@ -244,6 +245,22 @@ void connect_pads(AVFilterInOut **ins, AVFilterInOut **outs)
 	}
 }
 
+int checktime(time_t min)
+{
+	static struct timespec prev = {};
+	time_t delta;
+	struct timespec t;
+	clock_gettime(CLOCK_MONOTONIC, &t);
+	if (t.tv_sec < prev.tv_sec)
+		delta = min;
+	else
+		delta = t.tv_sec - prev.tv_sec;
+	if (delta < min)
+		return 1;
+	memcpy(&prev, &t, sizeof(prev));
+	return 0;
+}
+
 int main(int argc, char *argv[])
 {
 	int err, i, progress, _progress;
@@ -327,7 +344,7 @@ int main(int argc, char *argv[])
 	{
 		i = pkt.stream_index;
 		_progress = av_rescale_q(pkt.pts, demux->streams[i]->time_base, AV_TIME_BASE_Q)*100/demux->duration;
-		if (_progress > progress)
+		if (_progress > progress || !checktime(30))
 		{
 			ping_job(jobid, "running", "{\"progress\": %i, \"log\": \"%s\"}", _progress,
 					jescape(get_avlogbuf()));
-- 
GitLab