hls: Add a discontinuity marker on recover

Message ID 20180216150925.52135-1-lu_zero@gentoo.org
State Committed
Commit aeaa108bafa11db290f1ada0e22dbfbd655cf2ba
Headers show
Series
  • hls: Add a discontinuity marker on recover
Related show

Commit Message

Luca Barbato Feb. 16, 2018, 3:09 p.m.
It seems to improve the compatibility with the js demuxers.
---
 libavformat/hlsenc.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

Comments

Luca Barbato Feb. 19, 2018, 12:49 p.m. | #1
On 16/02/2018 16:09, Luca Barbato wrote:
> It seems to improve the compatibility with the js demuxers.
> ---
>   libavformat/hlsenc.c | 25 ++++++++++++++++++++-----
>   1 file changed, 20 insertions(+), 5 deletions(-)
> 

Ping.
Luca Barbato March 6, 2018, 7:50 p.m. | #2
On 19/02/2018 13:49, Luca Barbato wrote:
> On 16/02/2018 16:09, Luca Barbato wrote:
>> It seems to improve the compatibility with the js demuxers.
>> ---
>>   libavformat/hlsenc.c | 25 ++++++++++++++++++++-----
>>   1 file changed, 20 insertions(+), 5 deletions(-)
>>
> 
> Ping.
> 

Ping2.
Vittorio Giovara March 6, 2018, 7:57 p.m. | #3
On Tue, Mar 6, 2018 at 2:50 PM, Luca Barbato <lu_zero@gentoo.org> wrote:

> On 19/02/2018 13:49, Luca Barbato wrote:
>
>> On 16/02/2018 16:09, Luca Barbato wrote:
>>
>>> It seems to improve the compatibility with the js demuxers.
>>> ---
>>>   libavformat/hlsenc.c | 25 ++++++++++++++++++++-----
>>>   1 file changed, 20 insertions(+), 5 deletions(-)
>>>
>>>
>> Ping.
>>
>>
> Ping2.
>

apparently ok (if tested)
Luca Barbato March 6, 2018, 7:58 p.m. | #4
On 06/03/2018 20:57, Vittorio Giovara wrote:
> On Tue, Mar 6, 2018 at 2:50 PM, Luca Barbato <lu_zero@gentoo.org> wrote:
> 
>> On 19/02/2018 13:49, Luca Barbato wrote:
>>
>>> On 16/02/2018 16:09, Luca Barbato wrote:
>>>
>>>> It seems to improve the compatibility with the js demuxers.
>>>> ---
>>>>    libavformat/hlsenc.c | 25 ++++++++++++++++++++-----
>>>>    1 file changed, 20 insertions(+), 5 deletions(-)
>>>>
>>>>
>>> Ping.
>>>
>>>
>> Ping2.
>>
> 
> apparently ok (if tested)
> 

Tested for more than 3 weeks now :)

lu

Patch

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index c84dd82e7d..2fd3f19fcb 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -42,6 +42,7 @@ 
 typedef struct ListEntry {
     char  name[1024];
     int64_t duration;     // segment duration in AV_TIME_BASE units
+    int discont;
     struct ListEntry *next;
 } ListEntry;
 
@@ -76,6 +77,8 @@  typedef struct HLSContext {
     char *iv;              // Set by a private option.
     int iv_len;
 
+    int recovered;
+
     char *key_basename;
 
     AVDictionary *enc_opts;
@@ -214,7 +217,8 @@  static int hls_mux_init(AVFormatContext *s)
     return 0;
 }
 
-static int append_entry(HLSContext *hls, int64_t duration, const char *name)
+static int append_entry(HLSContext *hls, int64_t duration, const char *name,
+                        int discont)
 {
     ListEntry *en = av_malloc(sizeof(*en));
 
@@ -223,6 +227,7 @@  static int append_entry(HLSContext *hls, int64_t duration, const char *name)
 
     av_strlcpy(en->name, name, sizeof(en->name));
 
+    en->discont  = discont;
     en->duration = duration;
     en->next     = NULL;
 
@@ -289,6 +294,10 @@  static int hls_window(AVFormatContext *s, int last)
            sequence);
 
     for (en = hls->list; en; en = en->next) {
+        if (en->discont) {
+            avio_printf(out, "#EXT-X-DISCONTINUITY\n");
+        }
+
         if (hls->encrypt) {
             char *key_url;
 
@@ -383,7 +392,7 @@  static int hls_recover(AVFormatContext *s)
     char line[1024];
     AVIOContext *io;
     const char *ptr;
-    int ret, is_segment = 0;
+    int ret, is_segment = 0, is_discont = 0;
     int64_t duration = 0;
 
     ret = s->io_open(s, &io, s->filename, AVIO_FLAG_READ, NULL);
@@ -410,16 +419,21 @@  static int hls_recover(AVFormatContext *s)
         } else if (av_strstart(line, "#EXTINF:", &ptr)) {
             is_segment = 1;
             duration   = atof(ptr) * AV_TIME_BASE;
+        } else if (av_strstart(line, "#EXT-X-DISCONTINUITY", NULL)) {
+            is_discont = 1;
         } else if (av_strstart(line, "#", NULL)) {
             continue;
         } else if (line[0]) {
             if (is_segment) {
-                append_entry(hls, duration, av_basename(line));
+                append_entry(hls, duration, av_basename(line), is_discont);
                 is_segment = 0;
+                is_discont = 0;
             }
         }
     }
 
+    hls->recovered = 1;
+
     return 0;
 }
 
@@ -539,7 +553,8 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
         hls->duration = pts - hls->end_pts;
 
     if (can_split && pts - hls->start_pts >= end_pts) {
-        ret = append_entry(hls, hls->duration, av_basename(hls->avf->filename));
+        ret = append_entry(hls, hls->duration, av_basename(hls->avf->filename), hls->recovered);
+        hls->recovered = 0;
         if (ret)
             return ret;
 
@@ -574,7 +589,7 @@  static int hls_write_trailer(struct AVFormatContext *s)
     ff_format_io_close(s, &oc->pb);
     avformat_free_context(oc);
     av_free(hls->basename);
-    append_entry(hls, hls->duration, av_basename(hls->avf->filename));
+    append_entry(hls, hls->duration, av_basename(hls->avf->filename), 0);
     hls_window(s, 1);
 
     free_entries(hls);