Browse code

avformat/hlsenc: Added option to add EXT-X-INDEPENDENT-SEGMENTS tag

Karthick J authored on 2017/11/24 07:57:36
Showing 2 changed files
... ...
@@ -737,6 +737,10 @@ The file specified by @code{hls_key_info_file} will be checked periodically and
737 737
 detect updates to the encryption info. Be sure to replace this file atomically,
738 738
 including the file containing the AES encryption key.
739 739
 
740
+@item independent_segments
741
+Add the @code{#EXT-X-INDEPENDENT-SEGMENTS} to playlists that has video segments
742
+and when all the segments of that playlist are guaranteed to start with a Key frame.
743
+
740 744
 @item split_by_time
741 745
 Allow segments to start on frames other than keyframes. This improves
742 746
 behavior on some players when the time between keyframes is inconsistent,
... ...
@@ -88,6 +88,7 @@ typedef enum HLSFlags {
88 88
     HLS_SECOND_LEVEL_SEGMENT_SIZE = (1 << 10), // include segment size (bytes) in segment filenames when use_localtime  e.g.: %%014s
89 89
     HLS_TEMP_FILE = (1 << 11),
90 90
     HLS_PERIODIC_REKEY = (1 << 12),
91
+    HLS_INDEPENDENT_SEGMENTS = (1 << 13),
91 92
 } HLSFlags;
92 93
 
93 94
 typedef enum {
... ...
@@ -1190,6 +1191,10 @@ static int hls_window(AVFormatContext *s, int last, VariantStream *vs)
1190 1190
         sequence = 0;
1191 1191
     }
1192 1192
 
1193
+    if (hls->flags & HLS_INDEPENDENT_SEGMENTS) {
1194
+        hls->version = 6;
1195
+    }
1196
+
1193 1197
     if (hls->segment_type == SEGMENT_TYPE_FMP4) {
1194 1198
         hls->version = 7;
1195 1199
     }
... ...
@@ -1219,6 +1224,9 @@ static int hls_window(AVFormatContext *s, int last, VariantStream *vs)
1219 1219
         avio_printf(out, "#EXT-X-DISCONTINUITY\n");
1220 1220
         vs->discontinuity_set = 1;
1221 1221
     }
1222
+    if (vs->has_video && (hls->flags & HLS_INDEPENDENT_SEGMENTS)) {
1223
+        avio_printf(out, "#EXT-X-INDEPENDENT-SEGMENTS\n");
1224
+    }
1222 1225
     for (en = vs->segments; en; en = en->next) {
1223 1226
         if ((hls->encrypt || hls->key_info_file) && (!key_uri || strcmp(en->key_uri, key_uri) ||
1224 1227
                                     av_strcasecmp(en->iv_string, iv_string))) {
... ...
@@ -1731,6 +1739,14 @@ static int hls_write_header(AVFormatContext *s)
1731 1731
     vs->start_pts      = AV_NOPTS_VALUE;
1732 1732
     vs->current_segment_final_filename_fmt[0] = '\0';
1733 1733
 
1734
+    if (hls->flags & HLS_SPLIT_BY_TIME && hls->flags & HLS_INDEPENDENT_SEGMENTS) {
1735
+        // Independent segments cannot be guaranteed when splitting by time
1736
+        hls->flags &= ~HLS_INDEPENDENT_SEGMENTS;
1737
+        av_log(s, AV_LOG_WARNING,
1738
+               "'split_by_time' and 'independent_segments' cannot be enabled together. "
1739
+               "Disabling 'independent_segments' flag\n");
1740
+    }
1741
+
1734 1742
     if (hls->flags & HLS_PROGRAM_DATE_TIME) {
1735 1743
         time_t now0;
1736 1744
         time(&now0);
... ...
@@ -2322,6 +2338,7 @@ static const AVOption options[] = {
2322 2322
     {"second_level_segment_duration", "include segment duration in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_DURATION }, 0, UINT_MAX,   E, "flags"},
2323 2323
     {"second_level_segment_size", "include segment size in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_SIZE }, 0, UINT_MAX,   E, "flags"},
2324 2324
     {"periodic_rekey", "reload keyinfo file periodically for re-keying", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_PERIODIC_REKEY }, 0, UINT_MAX,   E, "flags"},
2325
+    {"independent_segments", "add EXT-X-INDEPENDENT-SEGMENTS, whenever applicable", 0, AV_OPT_TYPE_CONST, { .i64 = HLS_INDEPENDENT_SEGMENTS }, 0, UINT_MAX, E, "flags"},
2325 2326
     {"use_localtime", "set filename expansion with strftime at segment creation", OFFSET(use_localtime), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
2326 2327
     {"use_localtime_mkdir", "create last directory component in strftime-generated filename", OFFSET(use_localtime_mkdir), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
2327 2328
     {"hls_playlist_type", "set the HLS playlist type", OFFSET(pl_type), AV_OPT_TYPE_INT, {.i64 = PLAYLIST_TYPE_NONE }, 0, PLAYLIST_TYPE_NB-1, E, "pl_type" },