static int hds_write_header(AVFormatContext *s)

{

    HDSContext *c = s->priv_data;

    int ret = 0, i;

    AVOutputFormat *oformat;



    mkdir(s->filename, 0777);



    oformat = av_guess_format("flv", NULL, NULL);

    if (!oformat) {

        ret = AVERROR_MUXER_NOT_FOUND;

        goto fail;

    }



    c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);

    if (!c->streams) {

        ret = AVERROR(ENOMEM);

        goto fail;

    }



    for (i = 0; i < s->nb_streams; i++) {

        OutputStream *os = &c->streams[c->nb_streams];

        AVFormatContext *ctx;

        AVStream *st = s->streams[i];



        if (!st->codec->bit_rate) {

            av_log(s, AV_LOG_ERROR, "No bit rate set for stream %d\n", i);

            ret = AVERROR(EINVAL);

            goto fail;

        }

        if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {

            if (os->has_video) {

                c->nb_streams++;

                os++;

            }

            os->has_video = 1;

        } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {

            if (os->has_audio) {

                c->nb_streams++;

                os++;

            }

            os->has_audio = 1;

        } else {

            av_log(s, AV_LOG_ERROR, "Unsupported stream type in stream %d\n", i);

            ret = AVERROR(EINVAL);

            goto fail;

        }

        os->bitrate += s->streams[i]->codec->bit_rate;



        if (!os->ctx) {

            os->first_stream = i;

            ctx = avformat_alloc_context();

            if (!ctx) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }

            os->ctx = ctx;

            ctx->oformat = oformat;

            ctx->interrupt_callback = s->interrupt_callback;



            ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf),

                                         AVIO_FLAG_WRITE, os,

                                         NULL, hds_write, NULL);

            if (!ctx->pb) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }

        } else {

            ctx = os->ctx;

        }

        s->streams[i]->id = c->nb_streams;



        if (!(st = avformat_new_stream(ctx, NULL))) {

            ret = AVERROR(ENOMEM);

            goto fail;

        }

        avcodec_copy_context(st->codec, s->streams[i]->codec);

        st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;

    }

    if (c->streams[c->nb_streams].ctx)

        c->nb_streams++;



    for (i = 0; i < c->nb_streams; i++) {

        OutputStream *os = &c->streams[i];

        int j;

        if ((ret = avformat_write_header(os->ctx, NULL)) < 0) {

             goto fail;

        }

        os->ctx_inited = 1;

        avio_flush(os->ctx->pb);

        for (j = 0; j < os->ctx->nb_streams; j++)

            s->streams[os->first_stream + j]->time_base = os->ctx->streams[j]->time_base;



        snprintf(os->temp_filename, sizeof(os->temp_filename),

                 "%s/stream%d_temp", s->filename, i);

        init_file(s, os, 0);



        if (!os->has_video && c->min_frag_duration <= 0) {

            av_log(s, AV_LOG_WARNING,

                   "No video stream in output stream %d and no min frag duration set\n", i);

            ret = AVERROR(EINVAL);

        }

        os->fragment_index = 1;

        write_abst(s, os, 0);

    }

    ret = write_manifest(s, 0);



fail:

    if (ret)

        hds_free(s);

    return ret;

}

static int hds_write_header(AVFormatContext *s)

{

    HDSContext *c = s->priv_data;

    int ret = 0, i;

    AVOutputFormat *oformat;



    mkdir(s->filename, 0777);



    oformat = av_guess_format("flv", NULL, NULL);

    if (!oformat) {

        ret = AVERROR_MUXER_NOT_FOUND;

        goto fail;

    }



    c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);

    if (!c->streams) {

        ret = AVERROR(ENOMEM);

        goto fail;

    }



    for (i = 0; i < s->nb_streams; i++) {

        OutputStream *os = &c->streams[c->nb_streams];

        AVFormatContext *ctx;

        AVStream *st = s->streams[i];



        if (!st->codec->bit_rate) {

            av_log(s, AV_LOG_ERROR, "No bit rate set for stream %d\n", i);

            ret = AVERROR(EINVAL);

            goto fail;

        }

        if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {

            if (os->has_video) {

                c->nb_streams++;

                os++;

            }

            os->has_video = 1;

        } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {

            if (os->has_audio) {

                c->nb_streams++;

                os++;

            }

            os->has_audio = 1;

        } else {

            av_log(s, AV_LOG_ERROR, "Unsupported stream type in stream %d\n", i);

            ret = AVERROR(EINVAL);

            goto fail;

        }

        os->bitrate += s->streams[i]->codec->bit_rate;



        if (!os->ctx) {

            os->first_stream = i;

            ctx = avformat_alloc_context();

            if (!ctx) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }

            os->ctx = ctx;

            ctx->oformat = oformat;

            ctx->interrupt_callback = s->interrupt_callback;



            ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf),

                                         AVIO_FLAG_WRITE, os,

                                         NULL, hds_write, NULL);

            if (!ctx->pb) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }

        } else {

            ctx = os->ctx;

        }

        s->streams[i]->id = c->nb_streams;



        if (!(st = avformat_new_stream(ctx, NULL))) {

            ret = AVERROR(ENOMEM);

            goto fail;

        }

        avcodec_copy_context(st->codec, s->streams[i]->codec);

        st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;

    }

    if (c->streams[c->nb_streams].ctx)

        c->nb_streams++;



    for (i = 0; i < c->nb_streams; i++) {

        OutputStream *os = &c->streams[i];

        int j;

        if ((ret = avformat_write_header(os->ctx, NULL)) < 0) {

             goto fail;

        }

        os->ctx_inited = 1;

        avio_flush(os->ctx->pb);

        for (j = 0; j < os->ctx->nb_streams; j++)

            s->streams[os->first_stream + j]->time_base = os->ctx->streams[j]->time_base;



        snprintf(os->temp_filename, sizeof(os->temp_filename),

                 "%s/stream%d_temp", s->filename, i);

        init_file(s, os, 0);



        if (!os->has_video && c->min_frag_duration <= 0) {

            av_log(s, AV_LOG_WARNING,

                   "No video stream in output stream %d and no min frag duration set\n", i);

            ret = AVERROR(EINVAL);

        }

        os->fragment_index = 1;

        write_abst(s, os, 0);

    }

    ret = write_manifest(s, 0);



fail:

    if (ret)

        hds_free(s);

    return ret;

}

static int FUNC_0(AVFormatContext *VAR_0)

{

    HDSContext *c = VAR_0->priv_data;

    int VAR_1 = 0, VAR_2;

    AVOutputFormat *oformat;



    mkdir(VAR_0->filename, 0777);



    oformat = av_guess_format("flv", NULL, NULL);

    if (!oformat) {

        VAR_1 = AVERROR_MUXER_NOT_FOUND;

        goto fail;

    }



    c->streams = av_mallocz(sizeof(*c->streams) * VAR_0->nb_streams);

    if (!c->streams) {

        VAR_1 = AVERROR(ENOMEM);

        goto fail;

    }



    for (VAR_2 = 0; VAR_2 < VAR_0->nb_streams; VAR_2++) {

        OutputStream *os = &c->streams[c->nb_streams];

        AVFormatContext *ctx;

        AVStream *st = VAR_0->streams[VAR_2];



        if (!st->codec->bit_rate) {

            av_log(VAR_0, AV_LOG_ERROR, "No bit rate set for stream %d\n", VAR_2);

            VAR_1 = AVERROR(EINVAL);

            goto fail;

        }

        if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {

            if (os->has_video) {

                c->nb_streams++;

                os++;

            }

            os->has_video = 1;

        } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {

            if (os->has_audio) {

                c->nb_streams++;

                os++;

            }

            os->has_audio = 1;

        } else {

            av_log(VAR_0, AV_LOG_ERROR, "Unsupported stream type in stream %d\n", VAR_2);

            VAR_1 = AVERROR(EINVAL);

            goto fail;

        }

        os->bitrate += VAR_0->streams[VAR_2]->codec->bit_rate;



        if (!os->ctx) {

            os->first_stream = VAR_2;

            ctx = avformat_alloc_context();

            if (!ctx) {

                VAR_1 = AVERROR(ENOMEM);

                goto fail;

            }

            os->ctx = ctx;

            ctx->oformat = oformat;

            ctx->interrupt_callback = VAR_0->interrupt_callback;



            ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf),

                                         AVIO_FLAG_WRITE, os,

                                         NULL, hds_write, NULL);

            if (!ctx->pb) {

                VAR_1 = AVERROR(ENOMEM);

                goto fail;

            }

        } else {

            ctx = os->ctx;

        }

        VAR_0->streams[VAR_2]->id = c->nb_streams;



        if (!(st = avformat_new_stream(ctx, NULL))) {

            VAR_1 = AVERROR(ENOMEM);

            goto fail;

        }

        avcodec_copy_context(st->codec, VAR_0->streams[VAR_2]->codec);

        st->sample_aspect_ratio = VAR_0->streams[VAR_2]->sample_aspect_ratio;

    }

    if (c->streams[c->nb_streams].ctx)

        c->nb_streams++;



    for (VAR_2 = 0; VAR_2 < c->nb_streams; VAR_2++) {

        OutputStream *os = &c->streams[VAR_2];

        int j;

        if ((VAR_1 = avformat_write_header(os->ctx, NULL)) < 0) {

             goto fail;

        }

        os->ctx_inited = 1;

        avio_flush(os->ctx->pb);

        for (j = 0; j < os->ctx->nb_streams; j++)

            VAR_0->streams[os->first_stream + j]->time_base = os->ctx->streams[j]->time_base;



        snprintf(os->temp_filename, sizeof(os->temp_filename),

                 "%VAR_0/stream%d_temp", VAR_0->filename, VAR_2);

        init_file(VAR_0, os, 0);



        if (!os->has_video && c->min_frag_duration <= 0) {

            av_log(VAR_0, AV_LOG_WARNING,

                   "No video stream in output stream %d and no min frag duration set\n", VAR_2);

            VAR_1 = AVERROR(EINVAL);

        }

        os->fragment_index = 1;

        write_abst(VAR_0, os, 0);

    }

    VAR_1 = write_manifest(VAR_0, 0);



fail:

    if (VAR_1)

        hds_free(VAR_0);

    return VAR_1;

}

static int FUNC_0(AVFormatContext *VAR_0)
{ HDSContext *c = VAR_0->priv_data; int VAR_1 = 0, VAR_2; AVOutputFormat *oformat; mkdir(VAR_0->filename, 0777); oformat = av_guess_format("flv", NULL, NULL); if (!oformat) { VAR_1 = AVERROR_MUXER_NOT_FOUND; goto fail; } c->streams = av_mallocz(sizeof(*c->streams) * VAR_0->nb_streams); if (!c->streams) { VAR_1 = AVERROR(ENOMEM); goto fail; } for (VAR_2 = 0; VAR_2 < VAR_0->nb_streams; VAR_2++) { OutputStream *os = &c->streams[c->nb_streams]; AVFormatContext *ctx; AVStream *st = VAR_0->streams[VAR_2]; if (!st->codec->bit_rate) { av_log(VAR_0, AV_LOG_ERROR, "No bit rate set for stream %d\n", VAR_2); VAR_1 = AVERROR(EINVAL); goto fail; } if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { if (os->has_video) { c->nb_streams++; os++; } os->has_video = 1; } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { if (os->has_audio) { c->nb_streams++; os++; } os->has_audio = 1; } else { av_log(VAR_0, AV_LOG_ERROR, "Unsupported stream type in stream %d\n", VAR_2); VAR_1 = AVERROR(EINVAL); goto fail; } os->bitrate += VAR_0->streams[VAR_2]->codec->bit_rate; if (!os->ctx) { os->first_stream = VAR_2; ctx = avformat_alloc_context(); if (!ctx) { VAR_1 = AVERROR(ENOMEM); goto fail; } os->ctx = ctx; ctx->oformat = oformat; ctx->interrupt_callback = VAR_0->interrupt_callback; ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf),
AVIO_FLAG_WRITE, os,
NULL, hds_write, NULL); if (!ctx->pb) { VAR_1 = AVERROR(ENOMEM); goto fail; } } else { ctx = os->ctx; } VAR_0->streams[VAR_2]->id = c->nb_streams; if (!(st = avformat_new_stream(ctx, NULL))) { VAR_1 = AVERROR(ENOMEM); goto fail; } avcodec_copy_context(st->codec, VAR_0->streams[VAR_2]->codec); st->sample_aspect_ratio = VAR_0->streams[VAR_2]->sample_aspect_ratio; } if (c->streams[c->nb_streams].ctx)
c->nb_streams++; for (VAR_2 = 0; VAR_2 < c->nb_streams; VAR_2++) { OutputStream *os = &c->streams[VAR_2]; int j; if ((VAR_1 = avformat_write_header(os->ctx, NULL)) < 0) { goto fail; } os->ctx_inited = 1; avio_flush(os->ctx->pb); for (j = 0; j < os->ctx->nb_streams; j++) VAR_0->streams[os->first_stream + j]->time_base = os->ctx->streams[j]->time_base; snprintf(os->temp_filename, sizeof(os->temp_filename),
"%VAR_0/stream%d_temp", VAR_0->filename, VAR_2); init_file(VAR_0, os, 0); if (!os->has_video && c->min_frag_duration <= 0) { av_log(VAR_0, AV_LOG_WARNING,
"No video stream in output stream %d and no min frag duration set\n", VAR_2); VAR_1 = AVERROR(EINVAL); } os->fragment_index = 1; write_abst(VAR_0, os, 0); } VAR_1 = write_manifest(VAR_0, 0); fail:
if (VAR_1)
hds_free(VAR_0); return VAR_1; }