[FFmpeg-devel] [PATCH] added parameter to dash encoder for start available time

Jerome Berclaz jerome at percipient.ai
Thu Mar 18 03:17:27 EET 2021


---
 libavformat/dashenc.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 24d43c34ea..81855ca8d0 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -149,6 +149,7 @@ typedef struct DASHContext {
     int master_publish_rate;
     int nr_of_streams_to_flush;
     int nr_of_streams_flushed;
+    int64_t start_time_ms;
 } DASHContext;
 
 static struct codec_string {
@@ -725,13 +726,11 @@ static void write_time(AVIOContext *out, int64_t time)
     avio_printf(out, "%d.%dS", seconds, fractions / (AV_TIME_BASE / 10));
 }
 
-static void format_date_now(char *buf, int size)
+static void format_date(uint64_t epoch_ms, char *buf, int size)
 {
     struct tm *ptm, tmbuf;
-    int64_t time_us = av_gettime();
-    int64_t time_ms = time_us / 1000;
-    const time_t time_s = time_ms / 1000;
-    int millisec = time_ms - (time_s * 1000);
+    const time_t time_s = epoch_ms / 1000;
+    int millisec = epoch_ms - (time_s * 1000);
     ptm = gmtime_r(&time_s, &tmbuf);
     if (ptm) {
         int len;
@@ -744,6 +743,12 @@ static void format_date_now(char *buf, int size)
     }
 }
 
+static void format_date_now(char *buf, int size)
+{
+    int64_t time_us = av_gettime();
+    format_date( time_us / 1000, buf, size);
+}
+
 static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_index,
                                 int final)
 {
@@ -1712,10 +1717,16 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
     os->last_pts = pkt->pts;
 
     if (!c->availability_start_time[0]) {
-        int64_t start_time_us = av_gettime();
-        c->start_time_s = start_time_us / 1000000;
-        format_date_now(c->availability_start_time,
+        if (c->start_time_ms) {
+            c->start_time_s = c->start_time_ms / 1000;
+            format_date(c->start_time_ms, c->availability_start_time,
                         sizeof(c->availability_start_time));
+        } else {
+            int64_t start_time_us = av_gettime();
+            c->start_time_s = start_time_us / 1000000;
+            format_date_now(c->availability_start_time,
+                            sizeof(c->availability_start_time));
+        }
     }
 
     if (!os->availability_time_offset && pkt->duration) {
@@ -1922,6 +1933,7 @@ static const AVOption options[] = {
     { "ignore_io_errors", "Ignore IO errors during open and write. Useful for long-duration runs with network output", OFFSET(ignore_io_errors), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
     { "lhls", "Enable Low-latency HLS(Experimental). Adds #EXT-X-PREFETCH tag with current segment's URI", OFFSET(lhls), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
     { "master_m3u8_publish_rate", "Publish master playlist every after this many segment intervals", OFFSET(master_publish_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, UINT_MAX, E},
+    { "start_time_ms", "stream start time in epoch milliseconds", OFFSET(start_time_ms), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, UINT64_MAX, E, "ms"},
     { NULL },
 };
 
-- 
2.20.1



More information about the ffmpeg-devel mailing list