typedef struct HLSContext {
const AVClass *class; // Class for private options.
- int number;
+ unsigned number;
+ int64_t sequence;
AVOutputFormat *oformat;
AVFormatContext *avf;
float time; // Set by a private option.
int has_video;
int64_t start_pts;
int64_t end_pts;
+ int nb_entries;
ListEntry *list;
ListEntry *end_list;
char *basename;
if (!en)
return AVERROR(ENOMEM);
- av_get_frame_filename(en->name, sizeof(en->name), hls->basename,
- hls->number -1);
+ av_strlcpy(en->name, av_basename(hls->avf->filename), sizeof(en->name));
en->duration = duration;
en->next = NULL;
hls->end_list = en;
- if (hls->number >= hls->size) {
+ if (hls->nb_entries >= hls->size) {
en = hls->list;
hls->list = en->next;
av_free(en);
- }
+ } else
+ hls->nb_entries++;
+
+ hls->sequence++;
return 0;
}
{
HLSContext *hls = s->priv_data;
ListEntry *en;
+ int target_duration = 0;
int ret = 0;
if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
&s->interrupt_callback, NULL)) < 0)
goto fail;
+ for (en = hls->list; en; en = en->next) {
+ if (target_duration < en->duration)
+ target_duration = en->duration;
+ }
+
avio_printf(hls->pb, "#EXTM3U\n");
avio_printf(hls->pb, "#EXT-X-VERSION:3\n");
- avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", (int)hls->time);
- avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
- FFMAX(0, hls->number - hls->size));
+ avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", target_duration);
+ avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
+ FFMAX(0, hls->sequence - hls->size));
for (en = hls->list; en; en = en->next) {
avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration);
int ret, i;
char *p;
const char *pattern = "%d.ts";
- int basename_size = strlen(s->filename) + strlen(pattern);
+ int basename_size = strlen(s->filename) + strlen(pattern) + 1;
hls->number = 0;
- hls->recording_time = hls->time * 1000000;
+ hls->recording_time = hls->time * AV_TIME_BASE;
hls->start_pts = AV_NOPTS_VALUE;
for (i = 0; i < s->nb_streams; i++)
if (p)
*p = '\0';
- av_strlcat(hls->basename, "%d.ts", basename_size);
+ av_strlcat(hls->basename, pattern, basename_size);
if ((ret = hls_mux_init(s)) < 0)
goto fail;
hls->start_pts = pkt->pts;
hls->end_pts = pkt->pts;
}
- end_pts += hls->start_pts;
if ((hls->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
- av_compare_ts(pkt->pts, st->time_base, end_pts, AV_TIME_BASE_Q) >= 0 &&
+ av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
+ end_pts, AV_TIME_BASE_Q) >= 0 &&
pkt->flags & AV_PKT_FLAG_KEY) {
- append_entry(hls, av_rescale(pkt->pts - hls->end_pts,
- st->time_base.num,
- st->time_base.den));
+ ret = append_entry(hls, av_rescale(pkt->pts - hls->end_pts,
+ st->time_base.num,
+ st->time_base.den));
+ if (ret)
+ return ret;
+
hls->end_pts = pkt->pts;
av_write_frame(oc, NULL); /* Flush any buffered data */