static int parse_playlist(HLSContext *c, const char *url,

                          struct variant *var, AVIOContext *in)

{

    int ret = 0, is_segment = 0, is_variant = 0, bandwidth = 0;

    int64_t duration = 0;

    enum KeyType key_type = KEY_NONE;

    uint8_t iv[16] = "";

    int has_iv = 0;

    char key[MAX_URL_SIZE] = "";

    char line[1024];

    const char *ptr;

    int close_in = 0;



    if (!in) {

        AVDictionary *opts = NULL;

        close_in = 1;

        /* Some HLS servers don't like being sent the range header */

        av_dict_set(&opts, "seekable", "0", 0);



        // broker prior HTTP options that should be consistent across requests

        av_dict_set(&opts, "user-agent", c->user_agent, 0);

        av_dict_set(&opts, "cookies", c->cookies, 0);



        ret = avio_open2(&in, url, AVIO_FLAG_READ,

                         c->interrupt_callback, &opts);

        av_dict_free(&opts);

        if (ret < 0)

            return ret;

    }



    read_chomp_line(in, line, sizeof(line));

    if (strcmp(line, "#EXTM3U")) {

        ret = AVERROR_INVALIDDATA;

        goto fail;

    }



    if (var) {

        free_segment_list(var);

        var->finished = 0;

    }

    while (!url_feof(in)) {

        read_chomp_line(in, line, sizeof(line));

        if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {

            struct variant_info info = {{0}};

            is_variant = 1;

            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,

                               &info);

            bandwidth = atoi(info.bandwidth);

        } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) {

            struct key_info info = {{0}};

            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_key_args,

                               &info);

            key_type = KEY_NONE;

            has_iv = 0;

            if (!strcmp(info.method, "AES-128"))

                key_type = KEY_AES_128;

            if (!strncmp(info.iv, "0x", 2) || !strncmp(info.iv, "0X", 2)) {

                ff_hex_to_data(iv, info.iv + 2);

                has_iv = 1;

            }

            av_strlcpy(key, info.uri, sizeof(key));

        } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {

            if (!var) {

                var = new_variant(c, 0, url, NULL);

                if (!var) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            var->target_duration = atoi(ptr) * AV_TIME_BASE;

        } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {

            if (!var) {

                var = new_variant(c, 0, url, NULL);

                if (!var) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            var->start_seq_no = atoi(ptr);

        } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {

            if (var)

                var->finished = 1;

        } else if (av_strstart(line, "#EXTINF:", &ptr)) {

            is_segment = 1;

            duration   = atof(ptr) * AV_TIME_BASE;

        } else if (av_strstart(line, "#", NULL)) {

            continue;

        } else if (line[0]) {

            if (is_variant) {

                if (!new_variant(c, bandwidth, line, url)) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

                is_variant = 0;

                bandwidth  = 0;

            }

            if (is_segment) {

                struct segment *seg;

                if (!var) {

                    var = new_variant(c, 0, url, NULL);

                    if (!var) {

                        ret = AVERROR(ENOMEM);

                        goto fail;

                    }

                }

                seg = av_malloc(sizeof(struct segment));

                if (!seg) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

                seg->duration = duration;

                seg->key_type = key_type;

                if (has_iv) {

                    memcpy(seg->iv, iv, sizeof(iv));

                } else {

                    int seq = var->start_seq_no + var->n_segments;

                    memset(seg->iv, 0, sizeof(seg->iv));

                    AV_WB32(seg->iv + 12, seq);

                }

                ff_make_absolute_url(seg->key, sizeof(seg->key), url, key);

                ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);

                dynarray_add(&var->segments, &var->n_segments, seg);

                is_segment = 0;

            }

        }

    }

    if (var)

        var->last_load_time = av_gettime();



fail:

    if (close_in)

        avio_close(in);

    return ret;

}

static int parse_playlist(HLSContext *c, const char *url,

                          struct variant *var, AVIOContext *in)

{

    int ret = 0, is_segment = 0, is_variant = 0, bandwidth = 0;

    int64_t duration = 0;

    enum KeyType key_type = KEY_NONE;

    uint8_t iv[16] = "";

    int has_iv = 0;

    char key[MAX_URL_SIZE] = "";

    char line[1024];

    const char *ptr;

    int close_in = 0;



    if (!in) {

        AVDictionary *opts = NULL;

        close_in = 1;

        

        av_dict_set(&opts, "seekable", "0", 0);



        

        av_dict_set(&opts, "user-agent", c->user_agent, 0);

        av_dict_set(&opts, "cookies", c->cookies, 0);



        ret = avio_open2(&in, url, AVIO_FLAG_READ,

                         c->interrupt_callback, &opts);

        av_dict_free(&opts);

        if (ret < 0)

            return ret;

    }



    read_chomp_line(in, line, sizeof(line));

    if (strcmp(line, "#EXTM3U")) {

        ret = AVERROR_INVALIDDATA;

        goto fail;

    }



    if (var) {

        free_segment_list(var);

        var->finished = 0;

    }

    while (!url_feof(in)) {

        read_chomp_line(in, line, sizeof(line));

        if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {

            struct variant_info info = {{0}};

            is_variant = 1;

            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,

                               &info);

            bandwidth = atoi(info.bandwidth);

        } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) {

            struct key_info info = {{0}};

            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_key_args,

                               &info);

            key_type = KEY_NONE;

            has_iv = 0;

            if (!strcmp(info.method, "AES-128"))

                key_type = KEY_AES_128;

            if (!strncmp(info.iv, "0x", 2) || !strncmp(info.iv, "0X", 2)) {

                ff_hex_to_data(iv, info.iv + 2);

                has_iv = 1;

            }

            av_strlcpy(key, info.uri, sizeof(key));

        } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {

            if (!var) {

                var = new_variant(c, 0, url, NULL);

                if (!var) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            var->target_duration = atoi(ptr) * AV_TIME_BASE;

        } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {

            if (!var) {

                var = new_variant(c, 0, url, NULL);

                if (!var) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            var->start_seq_no = atoi(ptr);

        } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {

            if (var)

                var->finished = 1;

        } else if (av_strstart(line, "#EXTINF:", &ptr)) {

            is_segment = 1;

            duration   = atof(ptr) * AV_TIME_BASE;

        } else if (av_strstart(line, "#", NULL)) {

            continue;

        } else if (line[0]) {

            if (is_variant) {

                if (!new_variant(c, bandwidth, line, url)) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

                is_variant = 0;

                bandwidth  = 0;

            }

            if (is_segment) {

                struct segment *seg;

                if (!var) {

                    var = new_variant(c, 0, url, NULL);

                    if (!var) {

                        ret = AVERROR(ENOMEM);

                        goto fail;

                    }

                }

                seg = av_malloc(sizeof(struct segment));

                if (!seg) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

                seg->duration = duration;

                seg->key_type = key_type;

                if (has_iv) {

                    memcpy(seg->iv, iv, sizeof(iv));

                } else {

                    int seq = var->start_seq_no + var->n_segments;

                    memset(seg->iv, 0, sizeof(seg->iv));

                    AV_WB32(seg->iv + 12, seq);

                }

                ff_make_absolute_url(seg->key, sizeof(seg->key), url, key);

                ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);

                dynarray_add(&var->segments, &var->n_segments, seg);

                is_segment = 0;

            }

        }

    }

    if (var)

        var->last_load_time = av_gettime();



fail:

    if (close_in)

        avio_close(in);

    return ret;

}

    char line[1024];
static int FUNC_0(HLSContext *VAR_0, const char *VAR_1,

                          struct variant *VAR_2, AVIOContext *VAR_3)

{

    int VAR_4 = 0, VAR_5 = 0, VAR_6 = 0, VAR_7 = 0;

    int64_t duration = 0;

    enum KeyType VAR_8 = KEY_NONE;

    uint8_t iv[16] = "";

    int VAR_9 = 0;

    char VAR_10[MAX_URL_SIZE] = "";

    char VAR_11[1024];

    const char *VAR_12;

    int VAR_13 = 0;



    if (!VAR_3) {

        AVDictionary *opts = NULL;

        VAR_13 = 1;

        

        av_dict_set(&opts, "seekable", "0", 0);



        

        av_dict_set(&opts, "user-agent", VAR_0->user_agent, 0);

        av_dict_set(&opts, "cookies", VAR_0->cookies, 0);



        VAR_4 = avio_open2(&VAR_3, VAR_1, AVIO_FLAG_READ,

                         VAR_0->interrupt_callback, &opts);

        av_dict_free(&opts);

        if (VAR_4 < 0)

            return VAR_4;

    }



    read_chomp_line(VAR_3, VAR_11, sizeof(VAR_11));

    if (strcmp(VAR_11, "#EXTM3U")) {

        VAR_4 = AVERROR_INVALIDDATA;

        goto fail;

    }



    if (VAR_2) {

        free_segment_list(VAR_2);

        VAR_2->finished = 0;

    }

    while (!url_feof(VAR_3)) {

        read_chomp_line(VAR_3, VAR_11, sizeof(VAR_11));

        if (av_strstart(VAR_11, "#EXT-X-STREAM-INF:", &VAR_12)) {

            struct variant_info VAR_15 = {{0}};

            VAR_6 = 1;

            ff_parse_key_value(VAR_12, (ff_parse_key_val_cb) handle_variant_args,

                               &VAR_15);

            VAR_7 = atoi(VAR_15.VAR_7);

        } else if (av_strstart(VAR_11, "#EXT-X-KEY:", &VAR_12)) {

            struct key_info VAR_15 = {{0}};

            ff_parse_key_value(VAR_12, (ff_parse_key_val_cb) handle_key_args,

                               &VAR_15);

            VAR_8 = KEY_NONE;

            VAR_9 = 0;

            if (!strcmp(VAR_15.method, "AES-128"))

                VAR_8 = KEY_AES_128;

            if (!strncmp(VAR_15.iv, "0x", 2) || !strncmp(VAR_15.iv, "0X", 2)) {

                ff_hex_to_data(iv, VAR_15.iv + 2);

                VAR_9 = 1;

            }

            av_strlcpy(VAR_10, VAR_15.uri, sizeof(VAR_10));

        } else if (av_strstart(VAR_11, "#EXT-X-TARGETDURATION:", &VAR_12)) {

            if (!VAR_2) {

                VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL);

                if (!VAR_2) {

                    VAR_4 = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            VAR_2->target_duration = atoi(VAR_12) * AV_TIME_BASE;

        } else if (av_strstart(VAR_11, "#EXT-X-MEDIA-SEQUENCE:", &VAR_12)) {

            if (!VAR_2) {

                VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL);

                if (!VAR_2) {

                    VAR_4 = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            VAR_2->start_seq_no = atoi(VAR_12);

        } else if (av_strstart(VAR_11, "#EXT-X-ENDLIST", &VAR_12)) {

            if (VAR_2)

                VAR_2->finished = 1;

        } else if (av_strstart(VAR_11, "#EXTINF:", &VAR_12)) {

            VAR_5 = 1;

            duration   = atof(VAR_12) * AV_TIME_BASE;

        } else if (av_strstart(VAR_11, "#", NULL)) {

            continue;

        } else if (VAR_11[0]) {

            if (VAR_6) {

                if (!new_variant(VAR_0, VAR_7, VAR_11, VAR_1)) {

                    VAR_4 = AVERROR(ENOMEM);

                    goto fail;

                }

                VAR_6 = 0;

                VAR_7  = 0;

            }

            if (VAR_5) {

                struct segment *VAR_15;

                if (!VAR_2) {

                    VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL);

                    if (!VAR_2) {

                        VAR_4 = AVERROR(ENOMEM);

                        goto fail;

                    }

                }

                VAR_15 = av_malloc(sizeof(struct segment));

                if (!VAR_15) {

                    VAR_4 = AVERROR(ENOMEM);

                    goto fail;

                }

                VAR_15->duration = duration;

                VAR_15->VAR_8 = VAR_8;

                if (VAR_9) {

                    memcpy(VAR_15->iv, iv, sizeof(iv));

                } else {

                    int VAR_16 = VAR_2->start_seq_no + VAR_2->n_segments;

                    memset(VAR_15->iv, 0, sizeof(VAR_15->iv));

                    AV_WB32(VAR_15->iv + 12, VAR_16);

                }

                ff_make_absolute_url(VAR_15->VAR_10, sizeof(VAR_15->VAR_10), VAR_1, VAR_10);

                ff_make_absolute_url(VAR_15->VAR_1, sizeof(VAR_15->VAR_1), VAR_1, VAR_11);

                dynarray_add(&VAR_2->segments, &VAR_2->n_segments, VAR_15);

                VAR_5 = 0;

            }

        }

    }

    if (VAR_2)

        VAR_2->last_load_time = av_gettime();



fail:

    if (VAR_13)

        avio_close(VAR_3);

    return VAR_4;

}

static int FUNC_0(HLSContext *VAR_0, const char *VAR_1,
struct variant *VAR_2, AVIOContext *VAR_3)
{ int VAR_4 = 0, VAR_5 = 0, VAR_6 = 0, VAR_7 = 0; int64_t duration = 0; enum KeyType VAR_8 = KEY_NONE; uint8_t iv[16] = ""; int VAR_9 = 0; char VAR_10[MAX_URL_SIZE] = ""; char VAR_11[1024]; const char *VAR_12; int VAR_13 = 0; if (!VAR_3) { AVDictionary *opts = NULL; VAR_13 = 1; av_dict_set(&opts, "seekable", "0", 0); av_dict_set(&opts, "user-agent", VAR_0->user_agent, 0); av_dict_set(&opts, "cookies", VAR_0->cookies, 0); VAR_4 = avio_open2(&VAR_3, VAR_1, AVIO_FLAG_READ,
VAR_0->interrupt_callback, &opts); av_dict_free(&opts); if (VAR_4 < 0)
return VAR_4; } read_chomp_line(VAR_3, VAR_11, sizeof(VAR_11)); if (strcmp(VAR_11, "#EXTM3U")) { VAR_4 = AVERROR_INVALIDDATA; goto fail; } if (VAR_2) { free_segment_list(VAR_2); VAR_2->finished = 0; } while (!url_feof(VAR_3)) { read_chomp_line(VAR_3, VAR_11, sizeof(VAR_11)); if (av_strstart(VAR_11, "#EXT-X-STREAM-INF:", &VAR_12)) { struct variant_info VAR_15 = {{0}}; VAR_6 = 1; ff_parse_key_value(VAR_12, (ff_parse_key_val_cb) handle_variant_args,
&VAR_15); VAR_7 = atoi(VAR_15.VAR_7); } else if (av_strstart(VAR_11, "#EXT-X-KEY:", &VAR_12)) { struct key_info VAR_15 = {{0}}; ff_parse_key_value(VAR_12, (ff_parse_key_val_cb) handle_key_args,
&VAR_15); VAR_8 = KEY_NONE; VAR_9 = 0; if (!strcmp(VAR_15.method, "AES-128"))
VAR_8 = KEY_AES_128; if (!strncmp(VAR_15.iv, "0x", 2) || !strncmp(VAR_15.iv, "0X", 2)) { ff_hex_to_data(iv, VAR_15.iv + 2); VAR_9 = 1; } av_strlcpy(VAR_10, VAR_15.uri, sizeof(VAR_10)); } else if (av_strstart(VAR_11, "#EXT-X-TARGETDURATION:", &VAR_12)) { if (!VAR_2) { VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL); if (!VAR_2) { VAR_4 = AVERROR(ENOMEM); goto fail; } } VAR_2->target_duration = atoi(VAR_12) * AV_TIME_BASE; } else if (av_strstart(VAR_11, "#EXT-X-MEDIA-SEQUENCE:", &VAR_12)) { if (!VAR_2) { VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL); if (!VAR_2) { VAR_4 = AVERROR(ENOMEM); goto fail; } } VAR_2->start_seq_no = atoi(VAR_12); } else if (av_strstart(VAR_11, "#EXT-X-ENDLIST", &VAR_12)) { if (VAR_2)
VAR_2->finished = 1; } else if (av_strstart(VAR_11, "#EXTINF:", &VAR_12)) { VAR_5 = 1; duration   = atof(VAR_12) * AV_TIME_BASE; } else if (av_strstart(VAR_11, "#", NULL)) { continue; } else if (VAR_11[0]) { if (VAR_6) { if (!new_variant(VAR_0, VAR_7, VAR_11, VAR_1)) { VAR_4 = AVERROR(ENOMEM); goto fail; } VAR_6 = 0; VAR_7  = 0; } if (VAR_5) { struct segment *VAR_15; if (!VAR_2) { VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL); if (!VAR_2) { VAR_4 = AVERROR(ENOMEM); goto fail; } } VAR_15 = av_malloc(sizeof(struct segment)); if (!VAR_15) { VAR_4 = AVERROR(ENOMEM); goto fail; } VAR_15->duration = duration; VAR_15->VAR_8 = VAR_8; if (VAR_9) { memcpy(VAR_15->iv, iv, sizeof(iv)); } else { int VAR_16 = VAR_2->start_seq_no + VAR_2->n_segments; memset(VAR_15->iv, 0, sizeof(VAR_15->iv)); AV_WB32(VAR_15->iv + 12, VAR_16); } ff_make_absolute_url(VAR_15->VAR_10, sizeof(VAR_15->VAR_10), VAR_1, VAR_10); ff_make_absolute_url(VAR_15->VAR_1, sizeof(VAR_15->VAR_1), VAR_1, VAR_11); dynarray_add(&VAR_2->segments, &VAR_2->n_segments, VAR_15); VAR_5 = 0; } } } if (VAR_2)
VAR_2->last_load_time = av_gettime(); fail:
if (VAR_13)
avio_close(VAR_3); return VAR_4; }