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
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),
89 av_basename(hls->basename),
92 en->duration = duration;
98 hls->end_list->next = en;
102 if (hls->number >= hls->size) {
104 hls->list = en->next;
111 static void free_entries(HLSContext *hls)
113 ListEntry *p = hls->list, *en;
122 static int hls_window(AVFormatContext *s, int last)
124 HLSContext *hls = s->priv_data;
126 int target_duration = 0;
129 if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
130 &s->interrupt_callback, NULL)) < 0)
133 for (en = hls->list; en; en = en->next) {
134 if (target_duration < en->duration)
135 target_duration = en->duration;
138 avio_printf(hls->pb, "#EXTM3U\n");
139 avio_printf(hls->pb, "#EXT-X-VERSION:3\n");
140 avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", target_duration);
141 avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
142 FFMAX(0, hls->number - hls->size));
144 for (en = hls->list; en; en = en->next) {
145 avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration);
146 avio_printf(hls->pb, "%s\n", en->name);
150 avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
153 avio_closep(&hls->pb);
157 static int hls_start(AVFormatContext *s)
159 HLSContext *c = s->priv_data;
160 AVFormatContext *oc = c->avf;
164 c->number %= c->wrap;
166 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
167 c->basename, c->number++) < 0)
168 return AVERROR(EINVAL);
170 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
171 &s->interrupt_callback, NULL)) < 0)
174 if (oc->oformat->priv_class && oc->priv_data)
175 av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
180 static int hls_write_header(AVFormatContext *s)
182 HLSContext *hls = s->priv_data;
185 const char *pattern = "%d.ts";
186 int basename_size = strlen(s->filename) + strlen(pattern) + 1;
190 hls->recording_time = hls->time * 1000000;
191 hls->start_pts = AV_NOPTS_VALUE;
193 for (i = 0; i < s->nb_streams; i++)
195 s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO;
197 if (hls->has_video > 1)
198 av_log(s, AV_LOG_WARNING,
199 "More than a single video stream present, "
200 "expect issues decoding it.\n");
202 hls->oformat = av_guess_format("mpegts", NULL, NULL);
205 ret = AVERROR_MUXER_NOT_FOUND;
209 hls->basename = av_malloc(basename_size);
211 if (!hls->basename) {
212 ret = AVERROR(ENOMEM);
216 strcpy(hls->basename, s->filename);
218 p = strrchr(hls->basename, '.');
223 av_strlcat(hls->basename, pattern, basename_size);
225 if ((ret = hls_mux_init(s)) < 0)
228 if ((ret = hls_start(s)) < 0)
231 if ((ret = avformat_write_header(hls->avf, NULL)) < 0)
237 av_free(hls->basename);
239 avformat_free_context(hls->avf);
244 static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
246 HLSContext *hls = s->priv_data;
247 AVFormatContext *oc = hls->avf;
248 AVStream *st = s->streams[pkt->stream_index];
249 int64_t end_pts = hls->recording_time * hls->number;
252 if (hls->start_pts == AV_NOPTS_VALUE) {
253 hls->start_pts = pkt->pts;
254 hls->end_pts = pkt->pts;
256 end_pts += hls->start_pts;
258 if ((hls->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
259 av_compare_ts(pkt->pts, st->time_base, end_pts, AV_TIME_BASE_Q) >= 0 &&
260 pkt->flags & AV_PKT_FLAG_KEY) {
262 append_entry(hls, av_rescale(pkt->pts - hls->end_pts,
265 hls->end_pts = pkt->pts;
267 av_write_frame(oc, NULL); /* Flush any buffered data */
277 if ((ret = hls_window(s, 0)) < 0)
281 ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
286 static int hls_write_trailer(struct AVFormatContext *s)
288 HLSContext *hls = s->priv_data;
289 AVFormatContext *oc = hls->avf;
291 av_write_trailer(oc);
292 avio_closep(&oc->pb);
293 avformat_free_context(oc);
294 av_free(hls->basename);
302 #define OFFSET(x) offsetof(HLSContext, x)
303 #define E AV_OPT_FLAG_ENCODING_PARAM
304 static const AVOption options[] = {
305 { "start_number", "first number in the sequence", OFFSET(number), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
306 {"hls_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
307 {"hls_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
308 {"hls_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
312 static const AVClass hls_class = {
313 .class_name = "hls muxer",
314 .item_name = av_default_item_name,
316 .version = LIBAVUTIL_VERSION_INT,
320 AVOutputFormat ff_hls_muxer = {
322 .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
323 .extensions = "m3u8",
324 .priv_data_size = sizeof(HLSContext),
325 .audio_codec = AV_CODEC_ID_MP2,
326 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
327 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
328 .write_header = hls_write_header,
329 .write_packet = hls_write_packet,
330 .write_trailer = hls_write_trailer,
331 .priv_class = &hls_class,