On Tue, May 30, 2017 at 4:18 AM, Luca Barbato <lu_z...@gentoo.org> wrote:
> Parse the playlist to recover the start sequence and previously
> generated segments and continue muxing from there.
>
> Mainly useful for near-seamless recovery in live scenarios.
> ---
>  libavformat/hlsenc.c | 84 
> ++++++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 78 insertions(+), 6 deletions(-)
>
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index b6dccf359e..cc8e95fa98 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -119,7 +119,7 @@ static int setup_encryption(AVFormatContext *s)
>      AVIOContext *out = NULL;
>      int len, ret;
>      uint8_t buf[16];
> -    uint8_t *k;
> +    uint8_t *k = NULL;
>
>      len = strlen(hls->basename) + 4 + 1;
>      hls->key_basename = av_mallocz(len);
> @@ -141,9 +141,22 @@ static int setup_encryption(AVFormatContext *s)
>              return ret;
>          k = hls->key;
>      } else {
> -        if ((ret = randomize(buf, sizeof(buf))) < 0) {
> -            av_log(s, AV_LOG_ERROR, "Cannot generate a strong random key\n");
> -            return ret;
> +        if (hls->start_sequence < 0) {
> +            ret = s->io_open(s, &out, hls->key_basename, AVIO_FLAG_READ, 
> NULL);
> +            if (ret < 0) {
> +                av_log(s, AV_LOG_WARNING,
> +                       "Cannot recover the key, generating a new one.\n");
> +            } else {
> +                avio_read(out, buf, 16);
> +                k = buf;
> +                avio_close(out);
> +            }
> +        }
> +        if (!k) {
> +            if ((ret = randomize(buf, sizeof(buf))) < 0) {
> +                av_log(s, AV_LOG_ERROR, "Cannot generate a strong random 
> key\n");
> +                return ret;
> +            }
>          }
>
>          if ((ret = dict_set_bin(&hls->enc_opts, "key", buf, sizeof(buf))) < 
> 0)
> @@ -356,12 +369,64 @@ fail:
>      return err;
>  }
>
> +static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
> +{
> +    int len = ff_get_line(s, buf, maxlen);
> +    while (len > 0 && av_isspace(buf[len - 1]))
> +        buf[--len] = '\0';
> +    return len;
> +}
> +
> +static int hls_recover(AVFormatContext *s)
> +{
> +    HLSContext *hls = s->priv_data;
> +    char line[1024];
> +    AVIOContext *io;
> +    const char *ptr;
> +    int ret, is_segment = 0;
> +    int64_t duration = 0;
> +
> +    ret = s->io_open(s, &io, s->filename, AVIO_FLAG_READ, NULL);
> +    if (ret < 0) {
> +        av_log(s, AV_LOG_WARNING,
> +               "Cannot recover the playlist, generating a new one.\n");
> +        hls->start_sequence = 0;
> +        hls->sequence = 0;
> +        return 0;
> +    }
> +
> +    read_chomp_line(io, line, sizeof(line));
> +    if (strcmp(line, "#EXTM3U")) {
> +        return AVERROR_INVALIDDATA;
> +    }

Maybe add a friendly error message?
I think it's ok, maybe no need to split the two patches either.
-- 
Vittorio
_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to