2 * Apple HTTP Live Streaming segmenter
3 * Copyright (c) 2012, Luca Barbato
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/mathematics.h"
26 #include "libavutil/parseutils.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/log.h"
34 typedef struct ListEntry {
37 struct ListEntry *next;
40 typedef struct HLSContext {
41 const AVClass *class; // Class for private options.
44 int64_t start_sequence;
45 AVOutputFormat *oformat;
47 float time; // Set by a private option.
48 int size; // Set by a private option.
49 int wrap; // Set by a private option.
50 int64_t recording_time;
54 int64_t duration; // last segment duration computed so far, in seconds
63 static int hls_mux_init(AVFormatContext *s)
65 HLSContext *hls = s->priv_data;
69 hls->avf = oc = avformat_alloc_context();
71 return AVERROR(ENOMEM);
73 oc->oformat = hls->oformat;
74 oc->interrupt_callback = s->interrupt_callback;
76 for (i = 0; i < s->nb_streams; i++) {
78 if (!(st = avformat_new_stream(oc, NULL)))
79 return AVERROR(ENOMEM);
80 avcodec_copy_context(st->codec, s->streams[i]->codec);
81 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
87 static int append_entry(HLSContext *hls, uint64_t duration)
89 ListEntry *en = av_malloc(sizeof(*en));
92 return AVERROR(ENOMEM);
94 av_strlcpy(en->name, av_basename(hls->avf->filename), sizeof(en->name));
96 en->duration = duration;
102 hls->end_list->next = en;
106 if (hls->nb_entries >= hls->size) {
108 hls->list = en->next;
118 static void free_entries(HLSContext *hls)
120 ListEntry *p = hls->list, *en;
129 static int hls_window(AVFormatContext *s, int last)
131 HLSContext *hls = s->priv_data;
133 int target_duration = 0;
135 int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->size);
137 if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
138 &s->interrupt_callback, NULL)) < 0)
141 for (en = hls->list; en; en = en->next) {
142 if (target_duration < en->duration)
143 target_duration = en->duration;
146 avio_printf(hls->pb, "#EXTM3U\n");
147 avio_printf(hls->pb, "#EXT-X-VERSION:3\n");
148 avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", target_duration);
149 avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
151 av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
154 for (en = hls->list; en; en = en->next) {
155 avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration);
157 avio_printf(hls->pb, "%s", hls->baseurl);
158 avio_printf(hls->pb, "%s\n", en->name);
162 avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
165 avio_closep(&hls->pb);
169 static int hls_start(AVFormatContext *s)
171 HLSContext *c = s->priv_data;
172 AVFormatContext *oc = c->avf;
175 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
176 c->basename, c->wrap ? c->sequence % c->wrap : c->sequence) < 0)
177 return AVERROR(EINVAL);
180 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
181 &s->interrupt_callback, NULL)) < 0)
184 if (oc->oformat->priv_class && oc->priv_data)
185 av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
190 static int hls_write_header(AVFormatContext *s)
192 HLSContext *hls = s->priv_data;
195 const char *pattern = "%d.ts";
196 int basename_size = strlen(s->filename) + strlen(pattern) + 1;
198 hls->sequence = hls->start_sequence;
199 hls->recording_time = hls->time * AV_TIME_BASE;
200 hls->start_pts = AV_NOPTS_VALUE;
202 for (i = 0; i < s->nb_streams; i++)
204 s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO;
206 if (hls->has_video > 1)
207 av_log(s, AV_LOG_WARNING,
208 "More than a single video stream present, "
209 "expect issues decoding it.\n");
211 hls->oformat = av_guess_format("mpegts", NULL, NULL);
214 ret = AVERROR_MUXER_NOT_FOUND;
218 hls->basename = av_malloc(basename_size);
220 if (!hls->basename) {
221 ret = AVERROR(ENOMEM);
225 strcpy(hls->basename, s->filename);
227 p = strrchr(hls->basename, '.');
232 av_strlcat(hls->basename, pattern, basename_size);
234 if ((ret = hls_mux_init(s)) < 0)
237 if ((ret = hls_start(s)) < 0)
240 if ((ret = avformat_write_header(hls->avf, NULL)) < 0)
246 av_free(hls->basename);
248 avformat_free_context(hls->avf);
253 static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
255 HLSContext *hls = s->priv_data;
256 AVFormatContext *oc = hls->avf;
257 AVStream *st = s->streams[pkt->stream_index];
258 int64_t end_pts = hls->recording_time * hls->number;
259 int ret, can_split = 1;
261 if (hls->start_pts == AV_NOPTS_VALUE) {
262 hls->start_pts = pkt->pts;
263 hls->end_pts = pkt->pts;
266 if (hls->has_video) {
267 can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
268 pkt->flags & AV_PKT_FLAG_KEY;
270 if (pkt->pts == AV_NOPTS_VALUE)
273 hls->duration = av_rescale(pkt->pts - hls->end_pts,
274 st->time_base.num, st->time_base.den);
276 if (can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
277 end_pts, AV_TIME_BASE_Q) >= 0) {
278 ret = append_entry(hls, hls->duration);
282 hls->end_pts = pkt->pts;
285 av_write_frame(oc, NULL); /* Flush any buffered data */
295 if ((ret = hls_window(s, 0)) < 0)
299 ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
304 static int hls_write_trailer(struct AVFormatContext *s)
306 HLSContext *hls = s->priv_data;
307 AVFormatContext *oc = hls->avf;
309 av_write_trailer(oc);
310 avio_closep(&oc->pb);
311 avformat_free_context(oc);
312 av_free(hls->basename);
313 append_entry(hls, hls->duration);
321 #define OFFSET(x) offsetof(HLSContext, x)
322 #define E AV_OPT_FLAG_ENCODING_PARAM
323 static const AVOption options[] = {
324 {"start_number", "first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
325 {"hls_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
326 {"hls_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
327 {"hls_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
328 {"hls_base_url", "url to prepend to each playlist entry", OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
332 static const AVClass hls_class = {
333 .class_name = "hls muxer",
334 .item_name = av_default_item_name,
336 .version = LIBAVUTIL_VERSION_INT,
340 AVOutputFormat ff_hls_muxer = {
342 .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
343 .extensions = "m3u8",
344 .priv_data_size = sizeof(HLSContext),
345 .audio_codec = AV_CODEC_ID_AAC,
346 .video_codec = AV_CODEC_ID_H264,
347 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
348 .write_header = hls_write_header,
349 .write_packet = hls_write_packet,
350 .write_trailer = hls_write_trailer,
351 .priv_class = &hls_class,