static int dash_flush(AVFormatContext *s, int final, int stream)

{

    DASHContext *c = s->priv_data;

    int i, ret = 0;



    const char *proto = avio_find_protocol_name(s->filename);

    int use_rename = proto && !strcmp(proto, "file");



    int cur_flush_segment_index = 0;

    if (stream >= 0)

        cur_flush_segment_index = c->streams[stream].segment_index;



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

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

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

        char filename[1024] = "", full_path[1024], temp_path[1024];

        int range_length, index_length = 0;



        if (!os->packets_written)

            continue;



        // Flush the single stream that got a keyframe right now.

        // Flush all audio streams as well, in sync with video keyframes,

        // but not the other video streams.

        if (stream >= 0 && i != stream) {

            if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)

                continue;

            // Make sure we don't flush audio streams multiple times, when

            // all video streams are flushed one at a time.

            if (c->has_video && os->segment_index > cur_flush_segment_index)

                continue;

        }



        if (!os->init_range_length) {

            flush_init_segment(s, os);

        }



        if (!c->single_file) {

            ff_dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, i, os->segment_index, os->bit_rate, os->start_pts);

            snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, filename);

            snprintf(temp_path, sizeof(temp_path), use_rename ? "%s.tmp" : "%s", full_path);

            ret = s->io_open(s, &os->out, temp_path, AVIO_FLAG_WRITE, NULL);

            if (ret < 0)

                break;

            if (!strcmp(os->format_name, "mp4"))

                write_styp(os->ctx->pb);

        } else {

            snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, os->initfile);

        }



        ret = flush_dynbuf(os, &range_length);

        if (ret < 0)

            break;

        os->packets_written = 0;



        if (c->single_file) {

            find_index_range(s, full_path, os->pos, &index_length);

        } else {

            ff_format_io_close(s, &os->out);



            if (use_rename) {

                ret = avpriv_io_move(temp_path, full_path);

                if (ret < 0)

                    break;

            }

        }



        if (!os->bit_rate) {

            // calculate average bitrate of first segment

            int64_t bitrate = (int64_t) range_length * 8 * AV_TIME_BASE / av_rescale_q(os->max_pts - os->start_pts,

                                                                                       st->time_base,

                                                                                       AV_TIME_BASE_Q);

            if (bitrate >= 0) {

                os->bit_rate = bitrate;

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

                     " bandwidth=\"%d\"", os->bit_rate);

            }

        }

        add_segment(os, filename, os->start_pts, os->max_pts - os->start_pts, os->pos, range_length, index_length);

        av_log(s, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %s\n", i, os->segment_index, full_path);



        os->pos += range_length;

    }



    if (c->window_size || (final && c->remove_at_exit)) {

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

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

            int j;

            int remove = os->nb_segments - c->window_size - c->extra_window_size;

            if (final && c->remove_at_exit)

                remove = os->nb_segments;

            if (remove > 0) {

                for (j = 0; j < remove; j++) {

                    char filename[1024];

                    snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->segments[j]->file);

                    unlink(filename);

                    av_free(os->segments[j]);

                }

                os->nb_segments -= remove;

                memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments));

            }

        }

    }



    if (ret >= 0)

        ret = write_manifest(s, final);

    return ret;

}

static int dash_flush(AVFormatContext *s, int final, int stream)

{

    DASHContext *c = s->priv_data;

    int i, ret = 0;



    const char *proto = avio_find_protocol_name(s->filename);

    int use_rename = proto && !strcmp(proto, "file");



    int cur_flush_segment_index = 0;

    if (stream >= 0)

        cur_flush_segment_index = c->streams[stream].segment_index;



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

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

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

        char filename[1024] = "", full_path[1024], temp_path[1024];

        int range_length, index_length = 0;



        if (!os->packets_written)

            continue;



        

        

        

        if (stream >= 0 && i != stream) {

            if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)

                continue;

            

            

            if (c->has_video && os->segment_index > cur_flush_segment_index)

                continue;

        }



        if (!os->init_range_length) {

            flush_init_segment(s, os);

        }



        if (!c->single_file) {

            ff_dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, i, os->segment_index, os->bit_rate, os->start_pts);

            snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, filename);

            snprintf(temp_path, sizeof(temp_path), use_rename ? "%s.tmp" : "%s", full_path);

            ret = s->io_open(s, &os->out, temp_path, AVIO_FLAG_WRITE, NULL);

            if (ret < 0)

                break;

            if (!strcmp(os->format_name, "mp4"))

                write_styp(os->ctx->pb);

        } else {

            snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, os->initfile);

        }



        ret = flush_dynbuf(os, &range_length);

        if (ret < 0)

            break;

        os->packets_written = 0;



        if (c->single_file) {

            find_index_range(s, full_path, os->pos, &index_length);

        } else {

            ff_format_io_close(s, &os->out);



            if (use_rename) {

                ret = avpriv_io_move(temp_path, full_path);

                if (ret < 0)

                    break;

            }

        }



        if (!os->bit_rate) {

            

            int64_t bitrate = (int64_t) range_length * 8 * AV_TIME_BASE / av_rescale_q(os->max_pts - os->start_pts,

                                                                                       st->time_base,

                                                                                       AV_TIME_BASE_Q);

            if (bitrate >= 0) {

                os->bit_rate = bitrate;

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

                     " bandwidth=\"%d\"", os->bit_rate);

            }

        }

        add_segment(os, filename, os->start_pts, os->max_pts - os->start_pts, os->pos, range_length, index_length);

        av_log(s, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %s\n", i, os->segment_index, full_path);



        os->pos += range_length;

    }



    if (c->window_size || (final && c->remove_at_exit)) {

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

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

            int j;

            int remove = os->nb_segments - c->window_size - c->extra_window_size;

            if (final && c->remove_at_exit)

                remove = os->nb_segments;

            if (remove > 0) {

                for (j = 0; j < remove; j++) {

                    char filename[1024];

                    snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->segments[j]->file);

                    unlink(filename);

                    av_free(os->segments[j]);

                }

                os->nb_segments -= remove;

                memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments));

            }

        }

    }



    if (ret >= 0)

        ret = write_manifest(s, final);

    return ret;

}

static int FUNC_0(AVFormatContext *VAR_0, int VAR_1, int VAR_2)

{

    DASHContext *c = VAR_0->priv_data;

    int VAR_3, VAR_4 = 0;



    const char *VAR_5 = avio_find_protocol_name(VAR_0->filename);

    int VAR_6 = VAR_5 && !strcmp(VAR_5, "file");



    int VAR_7 = 0;

    if (VAR_2 >= 0)

        VAR_7 = c->streams[VAR_2].segment_index;



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

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

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

        char filename[1024] = "", full_path[1024], temp_path[1024];

        int range_length, index_length = 0;



        if (!os->packets_written)

            continue;



        

        

        

        if (VAR_2 >= 0 && VAR_3 != VAR_2) {

            if (VAR_0->streams[VAR_3]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)

                continue;

            

            

            if (c->has_video && os->segment_index > VAR_7)

                continue;

        }



        if (!os->init_range_length) {

            flush_init_segment(VAR_0, os);

        }



        if (!c->single_file) {

            ff_dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, VAR_3, os->segment_index, os->bit_rate, os->start_pts);

            snprintf(full_path, sizeof(full_path), "%VAR_0%VAR_0", c->dirname, filename);

            snprintf(temp_path, sizeof(temp_path), VAR_6 ? "%VAR_0.tmp" : "%VAR_0", full_path);

            VAR_4 = VAR_0->io_open(VAR_0, &os->out, temp_path, AVIO_FLAG_WRITE, NULL);

            if (VAR_4 < 0)

                break;

            if (!strcmp(os->format_name, "mp4"))

                write_styp(os->ctx->pb);

        } else {

            snprintf(full_path, sizeof(full_path), "%VAR_0%VAR_0", c->dirname, os->initfile);

        }



        VAR_4 = flush_dynbuf(os, &range_length);

        if (VAR_4 < 0)

            break;

        os->packets_written = 0;



        if (c->single_file) {

            find_index_range(VAR_0, full_path, os->pos, &index_length);

        } else {

            ff_format_io_close(VAR_0, &os->out);



            if (VAR_6) {

                VAR_4 = avpriv_io_move(temp_path, full_path);

                if (VAR_4 < 0)

                    break;

            }

        }



        if (!os->bit_rate) {

            

            int64_t bitrate = (int64_t) range_length * 8 * AV_TIME_BASE / av_rescale_q(os->max_pts - os->start_pts,

                                                                                       st->time_base,

                                                                                       AV_TIME_BASE_Q);

            if (bitrate >= 0) {

                os->bit_rate = bitrate;

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

                     " bandwidth=\"%d\"", os->bit_rate);

            }

        }

        add_segment(os, filename, os->start_pts, os->max_pts - os->start_pts, os->pos, range_length, index_length);

        av_log(VAR_0, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %VAR_0\n", VAR_3, os->segment_index, full_path);



        os->pos += range_length;

    }



    if (c->window_size || (VAR_1 && c->remove_at_exit)) {

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

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

            int j;

            int remove = os->nb_segments - c->window_size - c->extra_window_size;

            if (VAR_1 && c->remove_at_exit)

                remove = os->nb_segments;

            if (remove > 0) {

                for (j = 0; j < remove; j++) {

                    char filename[1024];

                    snprintf(filename, sizeof(filename), "%VAR_0%VAR_0", c->dirname, os->segments[j]->file);

                    unlink(filename);

                    av_free(os->segments[j]);

                }

                os->nb_segments -= remove;

                memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments));

            }

        }

    }



    if (VAR_4 >= 0)

        VAR_4 = write_manifest(VAR_0, VAR_1);

    return VAR_4;

}

static int FUNC_0(AVFormatContext *VAR_0, int VAR_1, int VAR_2)
{ DASHContext *c = VAR_0->priv_data; int VAR_3, VAR_4 = 0; const char *VAR_5 = avio_find_protocol_name(VAR_0->filename); int VAR_6 = VAR_5 && !strcmp(VAR_5, "file"); int VAR_7 = 0; if (VAR_2 >= 0)
VAR_7 = c->streams[VAR_2].segment_index; for (VAR_3 = 0; VAR_3 < VAR_0->nb_streams; VAR_3++) { OutputStream *os = &c->streams[VAR_3]; AVStream *st = VAR_0->streams[VAR_3]; char filename[1024] = "", full_path[1024], temp_path[1024]; int range_length, index_length = 0; if (!os->packets_written)
continue; if (VAR_2 >= 0 && VAR_3 != VAR_2) { if (VAR_0->streams[VAR_3]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
continue; if (c->has_video && os->segment_index > VAR_7)
continue; } if (!os->init_range_length) { flush_init_segment(VAR_0, os); } if (!c->single_file) { ff_dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, VAR_3, os->segment_index, os->bit_rate, os->start_pts); snprintf(full_path, sizeof(full_path), "%VAR_0%VAR_0", c->dirname, filename); snprintf(temp_path, sizeof(temp_path), VAR_6 ? "%VAR_0.tmp" : "%VAR_0", full_path); VAR_4 = VAR_0->io_open(VAR_0, &os->out, temp_path, AVIO_FLAG_WRITE, NULL); if (VAR_4 < 0)
break; if (!strcmp(os->format_name, "mp4"))
write_styp(os->ctx->pb); } else { snprintf(full_path, sizeof(full_path), "%VAR_0%VAR_0", c->dirname, os->initfile); } VAR_4 = flush_dynbuf(os, &range_length); if (VAR_4 < 0)
break; os->packets_written = 0; if (c->single_file) { find_index_range(VAR_0, full_path, os->pos, &index_length); } else { ff_format_io_close(VAR_0, &os->out); if (VAR_6) { VAR_4 = avpriv_io_move(temp_path, full_path); if (VAR_4 < 0)
break; } } if (!os->bit_rate) { int64_t bitrate = (int64_t) range_length * 8 * AV_TIME_BASE / av_rescale_q(os->max_pts - os->start_pts,
st->time_base,
AV_TIME_BASE_Q); if (bitrate >= 0) { os->bit_rate = bitrate; snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
" bandwidth=\"%d\"", os->bit_rate); } } add_segment(os, filename, os->start_pts, os->max_pts - os->start_pts, os->pos, range_length, index_length); av_log(VAR_0, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %VAR_0\n", VAR_3, os->segment_index, full_path); os->pos += range_length; } if (c->window_size || (VAR_1 && c->remove_at_exit)) { for (VAR_3 = 0; VAR_3 < VAR_0->nb_streams; VAR_3++) { OutputStream *os = &c->streams[VAR_3]; int j; int remove = os->nb_segments - c->window_size - c->extra_window_size; if (VAR_1 && c->remove_at_exit)
remove = os->nb_segments; if (remove > 0) { for (j = 0; j < remove; j++) { char filename[1024]; snprintf(filename, sizeof(filename), "%VAR_0%VAR_0", c->dirname, os->segments[j]->file); unlink(filename); av_free(os->segments[j]); } os->nb_segments -= remove; memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments)); } } } if (VAR_4 >= 0)
VAR_4 = write_manifest(VAR_0, VAR_1); return VAR_4; }