static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)

{

    HLSContext *hls = s->priv_data;

    AVFormatContext *oc = NULL;

    AVStream *st = s->streams[pkt->stream_index];

    int64_t end_pts = 0;

    int is_ref_pkt = 1;

    int ret = 0, can_split = 1, i, j;

    int stream_index = 0;

    int range_length = 0;

    uint8_t *buffer = NULL;

    VariantStream *vs = NULL;



    for (i = 0; i < hls->nb_varstreams; i++) {

        vs = &hls->var_streams[i];

        for (j = 0; j < vs->nb_streams; j++) {

            if (vs->streams[j] == st) {

                if( st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ) {

                    oc = vs->vtt_avf;

                    stream_index = 0;

                } else {

                    oc = vs->avf;

                    stream_index = j;

                }

                break;

            }

        }



        if (oc)

            break;

    }



    if (!oc) {

        av_log(s, AV_LOG_ERROR, "Unable to find mapping variant stream\n");

        return AVERROR(ENOMEM);

    }



    end_pts = hls->recording_time * vs->number;



    if (vs->sequence - vs->nb_entries > hls->start_sequence && hls->init_time > 0) {

        /* reset end_pts, hls->recording_time at end of the init hls list */

        int init_list_dur = hls->init_time * vs->nb_entries * AV_TIME_BASE;

        int after_init_list_dur = (vs->sequence - vs->nb_entries ) * hls->time * AV_TIME_BASE;

        hls->recording_time = hls->time * AV_TIME_BASE;

        end_pts = init_list_dur + after_init_list_dur ;

    }



    if (vs->start_pts == AV_NOPTS_VALUE) {

        vs->start_pts = pkt->pts;

        vs->end_pts   = pkt->pts;

    }



    if (vs->has_video) {

        can_split = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&

                    ((pkt->flags & AV_PKT_FLAG_KEY) || (hls->flags & HLS_SPLIT_BY_TIME));

        is_ref_pkt = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO;

    }

    if (pkt->pts == AV_NOPTS_VALUE)

        is_ref_pkt = can_split = 0;



    if (is_ref_pkt) {

        if (vs->new_start) {

            vs->new_start = 0;

            vs->duration = (double)(pkt->pts - vs->end_pts)

                                       * st->time_base.num / st->time_base.den;

            vs->dpp = (double)(pkt->duration) * st->time_base.num / st->time_base.den;

        } else {

            if (pkt->duration) {

                vs->duration += (double)(pkt->duration) * st->time_base.num / st->time_base.den;

            } else {

                av_log(s, AV_LOG_WARNING, "pkt->duration = 0, maybe the hls segment duration will not precise\n");

                vs->duration = (double)(pkt->pts - vs->end_pts) * st->time_base.num / st->time_base.den;

            }

        }



    }



    if (vs->packets_written && can_split && av_compare_ts(pkt->pts - vs->start_pts, st->time_base,

                                   end_pts, AV_TIME_BASE_Q) >= 0) {

        int64_t new_start_pos;

        char *old_filename = av_strdup(vs->avf->filename);

        int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);



        if (!old_filename) {

            return AVERROR(ENOMEM);

        }



        av_write_frame(vs->avf, NULL); /* Flush any buffered data */



        new_start_pos = avio_tell(vs->avf->pb);

        vs->size = new_start_pos - vs->start_pos;



        if (!byterange_mode) {

            if (hls->segment_type == SEGMENT_TYPE_FMP4 && !vs->init_range_length) {

                avio_flush(oc->pb);

                range_length = avio_close_dyn_buf(oc->pb, &buffer);

                avio_write(vs->out, buffer, range_length);

                vs->init_range_length = range_length;

                avio_open_dyn_buf(&oc->pb);

                vs->packets_written = 0;

                ff_format_io_close(s, &vs->out);

            } else {

                ff_format_io_close(s, &oc->pb);

            }

            if (vs->vtt_avf) {

                ff_format_io_close(s, &vs->vtt_avf->pb);

            }

        }

        if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0]) {

            if (!(hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size <= 0))

                if ((vs->avf->oformat->priv_class && vs->avf->priv_data) && hls->segment_type != SEGMENT_TYPE_FMP4)

                    av_opt_set(vs->avf->priv_data, "mpegts_flags", "resend_headers", 0);

            hls_rename_temp_file(s, oc);

        }



        if (vs->fmp4_init_mode) {

            vs->number--;

        }



        if (!vs->fmp4_init_mode || byterange_mode)

            ret = hls_append_segment(s, hls, vs, vs->duration, vs->start_pos, vs->size);

        vs->start_pos = new_start_pos;

        if (ret < 0) {

            av_free(old_filename);

            return ret;

        }



        vs->end_pts = pkt->pts;

        vs->duration = 0;



        vs->fmp4_init_mode = 0;

        if (hls->flags & HLS_SINGLE_FILE) {

            vs->number++;

        } else if (hls->max_seg_size > 0) {

            if (vs->start_pos >= hls->max_seg_size) {

                vs->sequence++;

                sls_flag_file_rename(hls, vs, old_filename);

                ret = hls_start(s, vs);

                vs->start_pos = 0;

                /* When split segment by byte, the duration is short than hls_time,

                 * so it is not enough one segment duration as hls_time, */

                vs->number--;

            }

            vs->number++;

        } else {

            sls_flag_file_rename(hls, vs, old_filename);

            ret = hls_start(s, vs);

        }

        av_free(old_filename);



        if (ret < 0) {

            return ret;

        }



        if (!vs->fmp4_init_mode || byterange_mode)

            if ((ret = hls_window(s, 0, vs)) < 0) {

                return ret;

            }

    }



    vs->packets_written++;

    ret = ff_write_chained(oc, stream_index, pkt, s, 0);



    return ret;

}

static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)

{

    HLSContext *hls = s->priv_data;

    AVFormatContext *oc = NULL;

    AVStream *st = s->streams[pkt->stream_index];

    int64_t end_pts = 0;

    int is_ref_pkt = 1;

    int ret = 0, can_split = 1, i, j;

    int stream_index = 0;

    int range_length = 0;

    uint8_t *buffer = NULL;

    VariantStream *vs = NULL;



    for (i = 0; i < hls->nb_varstreams; i++) {

        vs = &hls->var_streams[i];

        for (j = 0; j < vs->nb_streams; j++) {

            if (vs->streams[j] == st) {

                if( st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ) {

                    oc = vs->vtt_avf;

                    stream_index = 0;

                } else {

                    oc = vs->avf;

                    stream_index = j;

                }

                break;

            }

        }



        if (oc)

            break;

    }



    if (!oc) {

        av_log(s, AV_LOG_ERROR, "Unable to find mapping variant stream\n");

        return AVERROR(ENOMEM);

    }



    end_pts = hls->recording_time * vs->number;



    if (vs->sequence - vs->nb_entries > hls->start_sequence && hls->init_time > 0) {

        

        int init_list_dur = hls->init_time * vs->nb_entries * AV_TIME_BASE;

        int after_init_list_dur = (vs->sequence - vs->nb_entries ) * hls->time * AV_TIME_BASE;

        hls->recording_time = hls->time * AV_TIME_BASE;

        end_pts = init_list_dur + after_init_list_dur ;

    }



    if (vs->start_pts == AV_NOPTS_VALUE) {

        vs->start_pts = pkt->pts;

        vs->end_pts   = pkt->pts;

    }



    if (vs->has_video) {

        can_split = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&

                    ((pkt->flags & AV_PKT_FLAG_KEY) || (hls->flags & HLS_SPLIT_BY_TIME));

        is_ref_pkt = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO;

    }

    if (pkt->pts == AV_NOPTS_VALUE)

        is_ref_pkt = can_split = 0;



    if (is_ref_pkt) {

        if (vs->new_start) {

            vs->new_start = 0;

            vs->duration = (double)(pkt->pts - vs->end_pts)

                                       * st->time_base.num / st->time_base.den;

            vs->dpp = (double)(pkt->duration) * st->time_base.num / st->time_base.den;

        } else {

            if (pkt->duration) {

                vs->duration += (double)(pkt->duration) * st->time_base.num / st->time_base.den;

            } else {

                av_log(s, AV_LOG_WARNING, "pkt->duration = 0, maybe the hls segment duration will not precise\n");

                vs->duration = (double)(pkt->pts - vs->end_pts) * st->time_base.num / st->time_base.den;

            }

        }



    }



    if (vs->packets_written && can_split && av_compare_ts(pkt->pts - vs->start_pts, st->time_base,

                                   end_pts, AV_TIME_BASE_Q) >= 0) {

        int64_t new_start_pos;

        char *old_filename = av_strdup(vs->avf->filename);

        int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);



        if (!old_filename) {

            return AVERROR(ENOMEM);

        }



        av_write_frame(vs->avf, NULL); 



        new_start_pos = avio_tell(vs->avf->pb);

        vs->size = new_start_pos - vs->start_pos;



        if (!byterange_mode) {

            if (hls->segment_type == SEGMENT_TYPE_FMP4 && !vs->init_range_length) {

                avio_flush(oc->pb);

                range_length = avio_close_dyn_buf(oc->pb, &buffer);

                avio_write(vs->out, buffer, range_length);

                vs->init_range_length = range_length;

                avio_open_dyn_buf(&oc->pb);

                vs->packets_written = 0;

                ff_format_io_close(s, &vs->out);

            } else {

                ff_format_io_close(s, &oc->pb);

            }

            if (vs->vtt_avf) {

                ff_format_io_close(s, &vs->vtt_avf->pb);

            }

        }

        if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0]) {

            if (!(hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size <= 0))

                if ((vs->avf->oformat->priv_class && vs->avf->priv_data) && hls->segment_type != SEGMENT_TYPE_FMP4)

                    av_opt_set(vs->avf->priv_data, "mpegts_flags", "resend_headers", 0);

            hls_rename_temp_file(s, oc);

        }



        if (vs->fmp4_init_mode) {

            vs->number--;

        }



        if (!vs->fmp4_init_mode || byterange_mode)

            ret = hls_append_segment(s, hls, vs, vs->duration, vs->start_pos, vs->size);

        vs->start_pos = new_start_pos;

        if (ret < 0) {

            av_free(old_filename);

            return ret;

        }



        vs->end_pts = pkt->pts;

        vs->duration = 0;



        vs->fmp4_init_mode = 0;

        if (hls->flags & HLS_SINGLE_FILE) {

            vs->number++;

        } else if (hls->max_seg_size > 0) {

            if (vs->start_pos >= hls->max_seg_size) {

                vs->sequence++;

                sls_flag_file_rename(hls, vs, old_filename);

                ret = hls_start(s, vs);

                vs->start_pos = 0;

                



                vs->number--;

            }

            vs->number++;

        } else {

            sls_flag_file_rename(hls, vs, old_filename);

            ret = hls_start(s, vs);

        }

        av_free(old_filename);



        if (ret < 0) {

            return ret;

        }



        if (!vs->fmp4_init_mode || byterange_mode)

            if ((ret = hls_window(s, 0, vs)) < 0) {

                return ret;

            }

    }



    vs->packets_written++;

    ret = ff_write_chained(oc, stream_index, pkt, s, 0);



    return ret;

}

        vs->end_pts   = pkt->pts;
static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)

{

    HLSContext *hls = VAR_0->priv_data;

    AVFormatContext *oc = NULL;

    AVStream *st = VAR_0->streams[VAR_1->VAR_7];

    int64_t end_pts = 0;

    int VAR_2 = 1;

    int VAR_3 = 0, VAR_4 = 1, VAR_5, VAR_6;

    int VAR_7 = 0;

    int VAR_8 = 0;

    uint8_t *buffer = NULL;

    VariantStream *vs = NULL;



    for (VAR_5 = 0; VAR_5 < hls->nb_varstreams; VAR_5++) {

        vs = &hls->var_streams[VAR_5];

        for (VAR_6 = 0; VAR_6 < vs->nb_streams; VAR_6++) {

            if (vs->streams[VAR_6] == st) {

                if( st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ) {

                    oc = vs->vtt_avf;

                    VAR_7 = 0;

                } else {

                    oc = vs->avf;

                    VAR_7 = VAR_6;

                }

                break;

            }

        }



        if (oc)

            break;

    }



    if (!oc) {

        av_log(VAR_0, AV_LOG_ERROR, "Unable to find mapping variant stream\n");

        return AVERROR(ENOMEM);

    }



    end_pts = hls->recording_time * vs->number;



    if (vs->sequence - vs->nb_entries > hls->start_sequence && hls->init_time > 0) {

        

        int VAR_9 = hls->init_time * vs->nb_entries * AV_TIME_BASE;

        int VAR_10 = (vs->sequence - vs->nb_entries ) * hls->time * AV_TIME_BASE;

        hls->recording_time = hls->time * AV_TIME_BASE;

        end_pts = VAR_9 + VAR_10 ;

    }



    if (vs->start_pts == AV_NOPTS_VALUE) {

        vs->start_pts = VAR_1->pts;

        vs->end_pts   = VAR_1->pts;

    }



    if (vs->has_video) {

        VAR_4 = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&

                    ((VAR_1->flags & AV_PKT_FLAG_KEY) || (hls->flags & HLS_SPLIT_BY_TIME));

        VAR_2 = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO;

    }

    if (VAR_1->pts == AV_NOPTS_VALUE)

        VAR_2 = VAR_4 = 0;



    if (VAR_2) {

        if (vs->new_start) {

            vs->new_start = 0;

            vs->duration = (double)(VAR_1->pts - vs->end_pts)

                                       * st->time_base.num / st->time_base.den;

            vs->dpp = (double)(VAR_1->duration) * st->time_base.num / st->time_base.den;

        } else {

            if (VAR_1->duration) {

                vs->duration += (double)(VAR_1->duration) * st->time_base.num / st->time_base.den;

            } else {

                av_log(VAR_0, AV_LOG_WARNING, "VAR_1->duration = 0, maybe the hls segment duration will not precise\n");

                vs->duration = (double)(VAR_1->pts - vs->end_pts) * st->time_base.num / st->time_base.den;

            }

        }



    }



    if (vs->packets_written && VAR_4 && av_compare_ts(VAR_1->pts - vs->start_pts, st->time_base,

                                   end_pts, AV_TIME_BASE_Q) >= 0) {

        int64_t new_start_pos;

        char *VAR_11 = av_strdup(vs->avf->filename);

        int VAR_12 = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);



        if (!VAR_11) {

            return AVERROR(ENOMEM);

        }



        av_write_frame(vs->avf, NULL); 



        new_start_pos = avio_tell(vs->avf->pb);

        vs->size = new_start_pos - vs->start_pos;



        if (!VAR_12) {

            if (hls->segment_type == SEGMENT_TYPE_FMP4 && !vs->init_range_length) {

                avio_flush(oc->pb);

                VAR_8 = avio_close_dyn_buf(oc->pb, &buffer);

                avio_write(vs->out, buffer, VAR_8);

                vs->init_range_length = VAR_8;

                avio_open_dyn_buf(&oc->pb);

                vs->packets_written = 0;

                ff_format_io_close(VAR_0, &vs->out);

            } else {

                ff_format_io_close(VAR_0, &oc->pb);

            }

            if (vs->vtt_avf) {

                ff_format_io_close(VAR_0, &vs->vtt_avf->pb);

            }

        }

        if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0]) {

            if (!(hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size <= 0))

                if ((vs->avf->oformat->priv_class && vs->avf->priv_data) && hls->segment_type != SEGMENT_TYPE_FMP4)

                    av_opt_set(vs->avf->priv_data, "mpegts_flags", "resend_headers", 0);

            hls_rename_temp_file(VAR_0, oc);

        }



        if (vs->fmp4_init_mode) {

            vs->number--;

        }



        if (!vs->fmp4_init_mode || VAR_12)

            VAR_3 = hls_append_segment(VAR_0, hls, vs, vs->duration, vs->start_pos, vs->size);

        vs->start_pos = new_start_pos;

        if (VAR_3 < 0) {

            av_free(VAR_11);

            return VAR_3;

        }



        vs->end_pts = VAR_1->pts;

        vs->duration = 0;



        vs->fmp4_init_mode = 0;

        if (hls->flags & HLS_SINGLE_FILE) {

            vs->number++;

        } else if (hls->max_seg_size > 0) {

            if (vs->start_pos >= hls->max_seg_size) {

                vs->sequence++;

                sls_flag_file_rename(hls, vs, VAR_11);

                VAR_3 = hls_start(VAR_0, vs);

                vs->start_pos = 0;

                



                vs->number--;

            }

            vs->number++;

        } else {

            sls_flag_file_rename(hls, vs, VAR_11);

            VAR_3 = hls_start(VAR_0, vs);

        }

        av_free(VAR_11);



        if (VAR_3 < 0) {

            return VAR_3;

        }



        if (!vs->fmp4_init_mode || VAR_12)

            if ((VAR_3 = hls_window(VAR_0, 0, vs)) < 0) {

                return VAR_3;

            }

    }



    vs->packets_written++;

    VAR_3 = ff_write_chained(oc, VAR_7, VAR_1, VAR_0, 0);



    return VAR_3;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)
{ HLSContext *hls = VAR_0->priv_data; AVFormatContext *oc = NULL; AVStream *st = VAR_0->streams[VAR_1->VAR_7]; int64_t end_pts = 0; int VAR_2 = 1; int VAR_3 = 0, VAR_4 = 1, VAR_5, VAR_6; int VAR_7 = 0; int VAR_8 = 0; uint8_t *buffer = NULL; VariantStream *vs = NULL; for (VAR_5 = 0; VAR_5 < hls->nb_varstreams; VAR_5++) { vs = &hls->var_streams[VAR_5]; for (VAR_6 = 0; VAR_6 < vs->nb_streams; VAR_6++) { if (vs->streams[VAR_6] == st) { if( st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ) { oc = vs->vtt_avf; VAR_7 = 0; } else { oc = vs->avf; VAR_7 = VAR_6; } break; } } if (oc)
break; } if (!oc) { av_log(VAR_0, AV_LOG_ERROR, "Unable to find mapping variant stream\n"); return AVERROR(ENOMEM); } end_pts = hls->recording_time * vs->number; if (vs->sequence - vs->nb_entries > hls->start_sequence && hls->init_time > 0) { int VAR_9 = hls->init_time * vs->nb_entries * AV_TIME_BASE; int VAR_10 = (vs->sequence - vs->nb_entries ) * hls->time * AV_TIME_BASE; hls->recording_time = hls->time * AV_TIME_BASE; end_pts = VAR_9 + VAR_10 ; } if (vs->start_pts == AV_NOPTS_VALUE) { vs->start_pts = VAR_1->pts; vs->end_pts   = VAR_1->pts; } if (vs->has_video) { VAR_4 = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
((VAR_1->flags & AV_PKT_FLAG_KEY) || (hls->flags & HLS_SPLIT_BY_TIME)); VAR_2 = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO; } if (VAR_1->pts == AV_NOPTS_VALUE)
VAR_2 = VAR_4 = 0; if (VAR_2) { if (vs->new_start) { vs->new_start = 0; vs->duration = (double)(VAR_1->pts - vs->end_pts)
* st->time_base.num / st->time_base.den; vs->dpp = (double)(VAR_1->duration) * st->time_base.num / st->time_base.den; } else { if (VAR_1->duration) { vs->duration += (double)(VAR_1->duration) * st->time_base.num / st->time_base.den; } else { av_log(VAR_0, AV_LOG_WARNING, "VAR_1->duration = 0, maybe the hls segment duration will not precise\n"); vs->duration = (double)(VAR_1->pts - vs->end_pts) * st->time_base.num / st->time_base.den; } } } if (vs->packets_written && VAR_4 && av_compare_ts(VAR_1->pts - vs->start_pts, st->time_base,
end_pts, AV_TIME_BASE_Q) >= 0) { int64_t new_start_pos; char *VAR_11 = av_strdup(vs->avf->filename); int VAR_12 = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0); if (!VAR_11) { return AVERROR(ENOMEM); } av_write_frame(vs->avf, NULL); new_start_pos = avio_tell(vs->avf->pb); vs->size = new_start_pos - vs->start_pos; if (!VAR_12) { if (hls->segment_type == SEGMENT_TYPE_FMP4 && !vs->init_range_length) { avio_flush(oc->pb); VAR_8 = avio_close_dyn_buf(oc->pb, &buffer); avio_write(vs->out, buffer, VAR_8); vs->init_range_length = VAR_8; avio_open_dyn_buf(&oc->pb); vs->packets_written = 0; ff_format_io_close(VAR_0, &vs->out); } else { ff_format_io_close(VAR_0, &oc->pb); } if (vs->vtt_avf) { ff_format_io_close(VAR_0, &vs->vtt_avf->pb); } } if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0]) { if (!(hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size <= 0))
if ((vs->avf->oformat->priv_class && vs->avf->priv_data) && hls->segment_type != SEGMENT_TYPE_FMP4)
av_opt_set(vs->avf->priv_data, "mpegts_flags", "resend_headers", 0); hls_rename_temp_file(VAR_0, oc); } if (vs->fmp4_init_mode) { vs->number--; } if (!vs->fmp4_init_mode || VAR_12)
VAR_3 = hls_append_segment(VAR_0, hls, vs, vs->duration, vs->start_pos, vs->size); vs->start_pos = new_start_pos; if (VAR_3 < 0) { av_free(VAR_11); return VAR_3; } vs->end_pts = VAR_1->pts; vs->duration = 0; vs->fmp4_init_mode = 0; if (hls->flags & HLS_SINGLE_FILE) { vs->number++; } else if (hls->max_seg_size > 0) { if (vs->start_pos >= hls->max_seg_size) { vs->sequence++; sls_flag_file_rename(hls, vs, VAR_11); VAR_3 = hls_start(VAR_0, vs); vs->start_pos = 0; vs->number--; } vs->number++; } else { sls_flag_file_rename(hls, vs, VAR_11); VAR_3 = hls_start(VAR_0, vs); } av_free(VAR_11); if (VAR_3 < 0) { return VAR_3; } if (!vs->fmp4_init_mode || VAR_12)
if ((VAR_3 = hls_window(VAR_0, 0, vs)) < 0) { return VAR_3; } } vs->packets_written++; VAR_3 = ff_write_chained(oc, VAR_7, VAR_1, VAR_0, 0); return VAR_3; }