+ if ((ret = ff_write_chained(os->ctx, 0, pkt, s, 0)) < 0)
+ return ret;
+
+ if (!os->init_range_length)
+ flush_init_segment(s, os);
+
+ //open the output context when the first frame of a segment is ready
+ if (!c->single_file && os->packets_written == 1) {
+ AVDictionary *opts = NULL;
+ const char *proto = avio_find_protocol_name(s->url);
+ int use_rename = proto && !strcmp(proto, "file");
+ os->filename[0] = os->full_path[0] = os->temp_path[0] = '\0';
+ ff_dash_fill_tmpl_params(os->filename, sizeof(os->filename),
+ c->media_seg_name, pkt->stream_index,
+ os->segment_index, os->bit_rate, os->start_pts);
+ snprintf(os->full_path, sizeof(os->full_path), "%s%s", c->dirname,
+ os->filename);
+ snprintf(os->temp_path, sizeof(os->temp_path),
+ use_rename ? "%s.tmp" : "%s", os->full_path);
+ set_http_options(&opts, c);
+ ret = dashenc_io_open(s, &os->out, os->temp_path, &opts);
+ if (ret < 0)
+ return ret;
+ av_dict_free(&opts);
+ }
+
+ //write out the data immediately in streaming mode
+ if (c->streaming && !strcmp(os->format_name, "mp4")) {
+ int len = 0;
+ uint8_t *buf = NULL;
+ if (!os->written_len)
+ write_styp(os->ctx->pb);
+ avio_flush(os->ctx->pb);
+ len = avio_get_dyn_buf (os->ctx->pb, &buf);
+ avio_write(os->out, buf + os->written_len, len - os->written_len);
+ os->written_len = len;
+ avio_flush(os->out);
+ }
+
+ return ret;