Browse code

libavformat/hls: add an option to start from a given segment in a live stream

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Rodger Combs authored on 2015/03/29 09:27:35
Showing 1 changed files
... ...
@@ -165,6 +165,7 @@ struct variant {
165 165
 };
166 166
 
167 167
 typedef struct HLSContext {
168
+    AVClass *class;
168 169
     int n_variants;
169 170
     struct variant **variants;
170 171
     int n_playlists;
... ...
@@ -173,6 +174,7 @@ typedef struct HLSContext {
173 173
     struct rendition **renditions;
174 174
 
175 175
     int cur_seq_no;
176
+    int live_start_index;
176 177
     int first_packet;
177 178
     int64_t first_timestamp;
178 179
     int64_t cur_timestamp;
... ...
@@ -1237,10 +1239,12 @@ static int select_cur_seq_no(HLSContext *c, struct playlist *pls)
1237 1237
              * require us to download a segment to inspect its timestamps. */
1238 1238
             return c->cur_seq_no;
1239 1239
 
1240
-        /* If this is a live stream with more than 3 segments, start at the
1241
-         * third last segment. */
1242
-        if (pls->n_segments > 3)
1243
-            return pls->start_seq_no + pls->n_segments - 3;
1240
+        /* If this is a live stream, start live_start_index segments from the
1241
+         * start or end */
1242
+        if (c->live_start_index < 0)
1243
+            return pls->start_seq_no + FFMAX(pls->n_segments + c->live_start_index, 0);
1244
+        else
1245
+            return pls->start_seq_no + FFMIN(c->live_start_index, pls->n_segments - 1);
1244 1246
     }
1245 1247
 
1246 1248
     /* Otherwise just start on the first segment. */
... ...
@@ -1714,9 +1718,25 @@ static int hls_probe(AVProbeData *p)
1714 1714
     return 0;
1715 1715
 }
1716 1716
 
1717
+#define OFFSET(x) offsetof(HLSContext, x)
1718
+#define FLAGS AV_OPT_FLAG_DECODING_PARAM
1719
+static const AVOption hls_options[] = {
1720
+    {"live_start_index", "segment index to start live streams at (negative values are from the end)",
1721
+        OFFSET(live_start_index), FF_OPT_TYPE_INT, {.i64 = -3}, INT_MIN, INT_MAX, FLAGS},
1722
+    {NULL}
1723
+};
1724
+
1725
+static const AVClass hls_class = {
1726
+    .class_name = "hls,applehttp",
1727
+    .item_name  = av_default_item_name,
1728
+    .option     = hls_options,
1729
+    .version    = LIBAVUTIL_VERSION_INT,
1730
+};
1731
+
1717 1732
 AVInputFormat ff_hls_demuxer = {
1718 1733
     .name           = "hls,applehttp",
1719 1734
     .long_name      = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
1735
+    .priv_class     = &hls_class,
1720 1736
     .priv_data_size = sizeof(HLSContext),
1721 1737
     .read_probe     = hls_probe,
1722 1738
     .read_header    = hls_read_header,