2 * Apple HTTP Live Streaming segmenter
3 * Copyright (c) 2012, Luca Barbato
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/mathematics.h"
25 #include "libavutil/parseutils.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/log.h"
33 typedef struct ListEntry {
36 struct ListEntry *next;
39 typedef struct HLSContext {
40 const AVClass *class; // Class for private options.
42 AVOutputFormat *oformat;
44 float time; // Set by a private option.
45 int size; // Set by a private option.
46 int wrap; // Set by a private option.
47 int64_t recording_time;
57 static int hls_mux_init(AVFormatContext *s)
59 HLSContext *hls = s->priv_data;
63 hls->avf = oc = avformat_alloc_context();
65 return AVERROR(ENOMEM);
67 oc->oformat = hls->oformat;
68 oc->interrupt_callback = s->interrupt_callback;
70 for (i = 0; i < s->nb_streams; i++) {
72 if (!(st = avformat_new_stream(oc, NULL)))
73 return AVERROR(ENOMEM);
74 avcodec_copy_context(st->codec, s->streams[i]->codec);
75 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
81 static int append_entry(HLSContext *hls, uint64_t duration)
83 ListEntry *en = av_malloc(sizeof(*en));
86 return AVERROR(ENOMEM);
88 av_get_frame_filename(en->name, sizeof(en->name), hls->basename,
91 en->duration = duration;
97 hls->end_list->next = en;
101 if (hls->number >= hls->size) {
103 hls->list = en->next;
110 static void free_entries(HLSContext *hls)
112 ListEntry *p = hls->list, *en;
121 static int hls_window(AVFormatContext *s, int last)
123 HLSContext *hls = s->priv_data;
127 if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
128 &s->interrupt_callback, NULL)) < 0)
131 avio_printf(hls->pb, "#EXTM3U\n");
132 avio_printf(hls->pb, "#EXT-X-VERSION:3\n");
133 avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", (int)hls->time);
134 avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
135 FFMAX(0, hls->number - hls->size));
137 for (en = hls->list; en; en = en->next) {
138 avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration);
139 avio_printf(hls->pb, "%s\n", en->name);
143 avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
146 avio_closep(&hls->pb);
150 static int hls_start(AVFormatContext *s)
152 HLSContext *c = s->priv_data;
153 AVFormatContext *oc = c->avf;
157 c->number %= c->wrap;
159 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
160 c->basename, c->number++) < 0)
161 return AVERROR(EINVAL);
163 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
164 &s->interrupt_callback, NULL)) < 0)
167 if (oc->oformat->priv_class && oc->priv_data)
168 av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
173 static int hls_write_header(AVFormatContext *s)
175 HLSContext *hls = s->priv_data;
178 const char *pattern = "%d.ts";
179 int basename_size = strlen(s->filename) + strlen(pattern);
183 hls->recording_time = hls->time * 1000000;
184 hls->start_pts = AV_NOPTS_VALUE;
186 for (i = 0; i < s->nb_streams; i++)
188 s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO;
190 if (hls->has_video > 1)
191 av_log(s, AV_LOG_WARNING,
192 "More than a single video stream present, "
193 "expect issues decoding it.\n");
195 hls->oformat = av_guess_format("mpegts", NULL, NULL);
198 ret = AVERROR_MUXER_NOT_FOUND;
202 hls->basename = av_malloc(basename_size);
204 if (!hls->basename) {
205 ret = AVERROR(ENOMEM);
209 strcpy(hls->basename, s->filename);
211 p = strrchr(hls->basename, '.');
216 av_strlcat(hls->basename, "%d.ts", basename_size);
218 if ((ret = hls_mux_init(s)) < 0)
221 if ((ret = hls_start(s)) < 0)
224 if ((ret = avformat_write_header(hls->avf, NULL)) < 0)
230 av_free(hls->basename);
232 avformat_free_context(hls->avf);
237 static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
239 HLSContext *hls = s->priv_data;
240 AVFormatContext *oc = hls->avf;
241 AVStream *st = s->streams[pkt->stream_index];
242 int64_t end_pts = hls->recording_time * hls->number;
245 if (hls->start_pts == AV_NOPTS_VALUE) {
246 hls->start_pts = pkt->pts;
247 hls->end_pts = pkt->pts;
249 end_pts += hls->start_pts;
251 if ((hls->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
252 av_compare_ts(pkt->pts, st->time_base, end_pts, AV_TIME_BASE_Q) >= 0 &&
253 pkt->flags & AV_PKT_FLAG_KEY) {
255 append_entry(hls, av_rescale(pkt->pts - hls->end_pts,
258 hls->end_pts = pkt->pts;
260 av_write_frame(oc, NULL); /* Flush any buffered data */
270 if ((ret = hls_window(s, 0)) < 0)
274 ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
279 static int hls_write_trailer(struct AVFormatContext *s)
281 HLSContext *hls = s->priv_data;
282 AVFormatContext *oc = hls->avf;
284 av_write_trailer(oc);
285 avio_closep(&oc->pb);
286 avformat_free_context(oc);
287 av_free(hls->basename);
295 #define OFFSET(x) offsetof(HLSContext, x)
296 #define E AV_OPT_FLAG_ENCODING_PARAM
297 static const AVOption options[] = {
298 { "start_number", "first number in the sequence", OFFSET(number), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
299 {"hls_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
300 {"hls_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
301 {"hls_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
305 static const AVClass hls_class = {
306 .class_name = "hls muxer",
307 .item_name = av_default_item_name,
309 .version = LIBAVUTIL_VERSION_INT,
313 AVOutputFormat ff_hls_muxer = {
315 .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
316 .extensions = "m3u8",
317 .priv_data_size = sizeof(HLSContext),
318 .audio_codec = AV_CODEC_ID_MP2,
319 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
320 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
321 .write_header = hls_write_header,
322 .write_packet = hls_write_packet,
323 .write_trailer = hls_write_trailer,
324 .priv_class = &hls_class,