+static void get_start_index_number(OutputStream *os, DASHContext *c,
+ int *start_index, int *start_number) {
+ *start_index = 0;
+ *start_number = 1;
+ if (c->window_size) {
+ *start_index = FFMAX(os->nb_segments - c->window_size, 0);
+ *start_number = FFMAX(os->segment_index - c->window_size, 1);
+ }
+}
+
+static void write_hls_media_playlist(OutputStream *os, AVFormatContext *s,
+ int representation_id, int final,
+ char *prefetch_url) {
+ DASHContext *c = s->priv_data;
+ int timescale = os->ctx->streams[0]->time_base.den;
+ char temp_filename_hls[1024];
+ char filename_hls[1024];
+ AVDictionary *http_opts = NULL;
+ int target_duration = 0;
+ int ret = 0;
+ const char *proto = avio_find_protocol_name(c->dirname);
+ int use_rename = proto && !strcmp(proto, "file");
+ int i, start_index, start_number;
+
+ get_start_index_number(os, c, &start_index, &start_number);
+
+ if (!c->hls_playlist || start_index >= os->nb_segments ||
+ os->segment_type != SEGMENT_TYPE_MP4)
+ return;
+
+ get_hls_playlist_name(filename_hls, sizeof(filename_hls),
+ c->dirname, representation_id);
+
+ snprintf(temp_filename_hls, sizeof(temp_filename_hls), use_rename ? "%s.tmp" : "%s", filename_hls);
+
+ set_http_options(&http_opts, c);
+ ret = dashenc_io_open(s, &c->m3u8_out, temp_filename_hls, &http_opts);
+ av_dict_free(&http_opts);
+ if (ret < 0) {
+ handle_io_open_error(s, ret, temp_filename_hls);
+ return;
+ }
+ for (i = start_index; i < os->nb_segments; i++) {
+ Segment *seg = os->segments[i];
+ double duration = (double) seg->duration / timescale;
+ if (target_duration <= duration)
+ target_duration = lrint(duration);
+ }
+
+ ff_hls_write_playlist_header(c->m3u8_out, 6, -1, target_duration,
+ start_number, PLAYLIST_TYPE_NONE);
+
+ ff_hls_write_init_file(c->m3u8_out, os->initfile, c->single_file,
+ os->init_range_length, os->init_start_pos);
+
+ for (i = start_index; i < os->nb_segments; i++) {
+ Segment *seg = os->segments[i];
+ ret = ff_hls_write_file_entry(c->m3u8_out, 0, c->single_file,
+ (double) seg->duration / timescale, 0,
+ seg->range_length, seg->start_pos, NULL,
+ c->single_file ? os->initfile : seg->file,
+ NULL);
+ if (ret < 0) {
+ av_log(os->ctx, AV_LOG_WARNING, "ff_hls_write_file_entry get error\n");
+ }
+ }
+
+ if (prefetch_url)
+ avio_printf(c->m3u8_out, "#EXT-X-PREFETCH:%s\n", prefetch_url);
+
+ if (final)
+ ff_hls_write_end_list(c->m3u8_out);
+
+ dashenc_io_close(s, &c->m3u8_out, temp_filename_hls);
+
+ if (use_rename)
+ if (avpriv_io_move(temp_filename_hls, filename_hls) < 0) {
+ av_log(os->ctx, AV_LOG_WARNING, "renaming file %s to %s failed\n\n", temp_filename_hls, filename_hls);
+ }
+}
+