From 3901ca87d9918e9002681b55a6984d3d0d0eaea5 Mon Sep 17 00:00:00 2001
From: Bela Bodecs <bodecsb@vivanet.hu>
Date: Fri, 6 Jan 2017 14:52:08 +0100
Subject: [PATCH] avformat/hlsenc: start_number new options

start_number option starts the playlist sequence number
(#EXT-X-MEDIA-SEQUENCE) from the specified number. Unless hls_flags
single_file is set, it also specifies starting sequence numbers of
segment and subtitle filenames. Sometimes it is usefull to have unique
starting numbers at each run, but currently it is only achiveable by
setting this parameter manually.
This patch enables to set start_number parameter automatically for
practically unique numbers. If start_number is set to -1, then
the start number will be the seconds since epoch (1970-01-01 00:00:00).
If set to -2, then the start number will be based on the current
date/time value as YYYYmmddHHMMSS. e.g. 20161231235659.

Signed-off-by: Bela Bodecs <bodecsb@vivanet.hu>
---
 doc/muxers.texi      |  7 +++++--
 libavformat/hlsenc.c | 22 ++++++++++++++++++++--
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index 351cd8c..ae877c9 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -417,8 +417,11 @@ files, and limits the maximum number of segment files written to disk
 to @var{wrap}.
 
 @item start_number @var{number}
-Start the playlist sequence number from @var{number}. Default value is
-0.
+Start the playlist sequence number (@code{#EXT-X-MEDIA-SEQUENCE}) from the specified @var{number}.
+Unless @code{hls_flags single_file} is set, it also specifies starting sequence numbers of segment and subtitle filenames.
+Default value is 0.
+If set to -1, then the start number will be the seconds since epoch (1970-01-01 00:00:00).
+If set to -2, then the start number will be based on the current date/time as YYYYmmddHHMMSS. e.g. 20161231235659.
 
 @item hls_allow_cache @var{allowcache}
 Explicitly set whether the client MAY (1) or MUST NOT (0) cache media segments.
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index eeb450a..de37be5 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -39,6 +39,9 @@
 #include "internal.h"
 #include "os_support.h"
 
+#define HLS_START_SEQUNCE_AS_SECONDS_SINCE_EPOCH -1
+#define HLS_START_SEQUNCE_AS_FORMATTED_STRFTIME -2
+
 #define KEYSIZE 16
 #define LINE_BUFFER_SIZE 1024
 
@@ -586,7 +589,7 @@ static int parse_playlist(AVFormatContext *s, const char *url)
     while (!avio_feof(in)) {
         read_chomp_line(in, line, sizeof(line));
         if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
-            hls->sequence = atoi(ptr);
+            hls->sequence = strtoll(ptr, NULL, 10);
         } else if (av_strstart(line, "#EXT-X-DISCONTINUITY", &ptr)) {
             is_segment = 1;
             hls->discontinuity = 1;
@@ -971,6 +974,21 @@ static int hls_write_header(AVFormatContext *s)
     int basename_size;
     int vtt_basename_size;
 
+    if (hls->start_sequence == HLS_START_SEQUNCE_AS_SECONDS_SINCE_EPOCH || hls->start_sequence == HLS_START_SEQUNCE_AS_FORMATTED_STRFTIME) {
+        time_t t = time(NULL); // we will need it in either case
+        if (hls->start_sequence == HLS_START_SEQUNCE_AS_SECONDS_SINCE_EPOCH)
+            hls->start_sequence = (int64_t)t;
+        else if (hls->start_sequence == HLS_START_SEQUNCE_AS_FORMATTED_STRFTIME) {
+            char b[21];
+            struct tm *p, tmbuf;
+            if (!(p = localtime_r(&t, &tmbuf)))
+                return AVERROR(ENOMEM);
+            if (!strftime(b, sizeof(b), "%Y%m%d%H%M%S", p))
+                return AVERROR(ENOMEM);
+            hls->start_sequence = strtoll(b, NULL, 10);
+        }
+    }
+
     hls->sequence       = hls->start_sequence;
     hls->recording_time = (hls->init_time ? hls->init_time : hls->time) * AV_TIME_BASE;
     hls->start_pts      = AV_NOPTS_VALUE;
@@ -1318,7 +1336,7 @@ static int hls_write_trailer(struct AVFormatContext *s)
 #define OFFSET(x) offsetof(HLSContext, x)
 #define E AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
-    {"start_number",  "set first number in the sequence",        OFFSET(start_sequence),AV_OPT_TYPE_INT64,  {.i64 = 0},     0, INT64_MAX, E},
+    {"start_number",  "set first number in the sequence",        OFFSET(start_sequence),AV_OPT_TYPE_INT64,  {.i64 = 0},     -2, INT64_MAX, E},
     {"hls_time",      "set segment length in seconds",           OFFSET(time),    AV_OPT_TYPE_FLOAT,  {.dbl = 2},     0, FLT_MAX, E},
     {"hls_init_time", "set segment length in seconds at init list",           OFFSET(init_time),    AV_OPT_TYPE_FLOAT,  {.dbl = 0},     0, FLT_MAX, E},
     {"hls_list_size", "set maximum number of playlist entries",  OFFSET(max_nb_segments),    AV_OPT_TYPE_INT,    {.i64 = 5},     0, INT_MAX, E},
-- 
2.5.3.windows.1

