segment: Fix the failure paths
[libav.git] / libavformat / segment.c
CommitLineData
02e8f032
LB
1/*
2 * Generic segmenter
3 * Copyright (c) 2011, Luca Barbato
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
02e8f032
LB
22#include <float.h>
23
24#include "avformat.h"
25#include "internal.h"
26
27#include "libavutil/log.h"
28#include "libavutil/opt.h"
29#include "libavutil/avstring.h"
30#include "libavutil/parseutils.h"
31#include "libavutil/mathematics.h"
32
33typedef struct {
34 const AVClass *class; /**< Class for private options. */
35 int number;
eb447d51 36 AVOutputFormat *oformat;
02e8f032
LB
37 AVFormatContext *avf;
38 char *format; /**< Set by a private option. */
39 char *list; /**< Set by a private option. */
5c08ae4f 40 char *entry_prefix; /**< Set by a private option. */
26db9100 41 int list_type; /**< Set by a private option. */
02e8f032
LB
42 float time; /**< Set by a private option. */
43 int size; /**< Set by a private option. */
0c1759ac 44 int wrap; /**< Set by a private option. */
378a6315 45 int individual_header_trailer; /**< Set by a private option. */
c864c396 46 int write_header_trailer; /**< Set by a private option. */
02e8f032
LB
47 int64_t offset_time;
48 int64_t recording_time;
49 int has_video;
50 AVIOContext *pb;
51} SegmentContext;
52
26db9100
LB
53enum {
54 LIST_FLAT,
55 LIST_HLS
56};
57
eb447d51
MS
58static int segment_mux_init(AVFormatContext *s)
59{
60 SegmentContext *seg = s->priv_data;
61 AVFormatContext *oc;
62 int i;
63
64 seg->avf = oc = avformat_alloc_context();
65 if (!oc)
66 return AVERROR(ENOMEM);
67
68 oc->oformat = seg->oformat;
69 oc->interrupt_callback = s->interrupt_callback;
70
71 for (i = 0; i < s->nb_streams; i++) {
72 AVStream *st;
73 if (!(st = avformat_new_stream(oc, NULL)))
74 return AVERROR(ENOMEM);
75 avcodec_copy_context(st->codec, s->streams[i]->codec);
76 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
28816050 77 st->time_base = s->streams[i]->time_base;
eb447d51
MS
78 }
79
80 return 0;
81}
82
26db9100
LB
83static int segment_hls_window(AVFormatContext *s, int last)
84{
85 SegmentContext *seg = s->priv_data;
86 int i, ret = 0;
87 char buf[1024];
88
89 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
90 &s->interrupt_callback, NULL)) < 0)
91 goto fail;
92
93 avio_printf(seg->pb, "#EXTM3U\n");
94 avio_printf(seg->pb, "#EXT-X-VERSION:3\n");
95 avio_printf(seg->pb, "#EXT-X-TARGETDURATION:%d\n", (int)seg->time);
96 avio_printf(seg->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
97 FFMAX(0, seg->number - seg->size));
98
87a3ea31
LB
99 av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%d\n",
100 FFMAX(0, seg->number - seg->size));
101
26db9100
LB
102 for (i = FFMAX(0, seg->number - seg->size);
103 i < seg->number; i++) {
104 avio_printf(seg->pb, "#EXTINF:%d,\n", (int)seg->time);
4b1f5e50 105 if (seg->entry_prefix) {
5c08ae4f
EAB
106 avio_printf(seg->pb, "%s", seg->entry_prefix);
107 }
26db9100
LB
108 av_get_frame_filename(buf, sizeof(buf), s->filename, i);
109 avio_printf(seg->pb, "%s\n", buf);
110 }
111
112 if (last)
113 avio_printf(seg->pb, "#EXT-X-ENDLIST\n");
114fail:
115 avio_closep(&seg->pb);
116 return ret;
117}
118
378a6315 119static int segment_start(AVFormatContext *s, int write_header)
02e8f032
LB
120{
121 SegmentContext *c = s->priv_data;
122 AVFormatContext *oc = c->avf;
123 int err = 0;
124
378a6315
MS
125 if (write_header) {
126 avformat_free_context(oc);
127 c->avf = NULL;
128 if ((err = segment_mux_init(s)) < 0)
129 return err;
130 oc = c->avf;
131 }
eb447d51 132
0c1759ac
LB
133 if (c->wrap)
134 c->number %= c->wrap;
135
02e8f032
LB
136 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
137 s->filename, c->number++) < 0)
138 return AVERROR(EINVAL);
139
140 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
141 &s->interrupt_callback, NULL)) < 0)
142 return err;
143
f7b24043 144 if (oc->oformat->priv_class && oc->priv_data)
91c1567b 145 av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
f7b24043 146
378a6315
MS
147 if (write_header) {
148 if ((err = avformat_write_header(oc, NULL)) < 0)
149 return err;
150 }
02e8f032
LB
151
152 return 0;
02e8f032
LB
153}
154
378a6315 155static int segment_end(AVFormatContext *oc, int write_trailer)
02e8f032
LB
156{
157 int ret = 0;
158
91c1567b 159 av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
378a6315
MS
160 if (write_trailer)
161 av_write_trailer(oc);
02e8f032 162 avio_close(oc->pb);
02e8f032
LB
163
164 return ret;
165}
166
c864c396
MS
167static int open_null_ctx(AVIOContext **ctx)
168{
169 int buf_size = 32768;
170 uint8_t *buf = av_malloc(buf_size);
171 if (!buf)
172 return AVERROR(ENOMEM);
173 *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
174 if (!*ctx) {
175 av_free(buf);
176 return AVERROR(ENOMEM);
177 }
178 return 0;
179}
180
181static void close_null_ctx(AVIOContext *pb)
182{
183 av_free(pb->buffer);
184 av_free(pb);
185}
186
b3f04657
LB
187static void seg_free_context(SegmentContext *seg)
188{
189 avio_closep(&seg->pb);
190 avformat_free_context(seg->avf);
191 seg->avf = NULL;
192}
193
02e8f032
LB
194static int seg_write_header(AVFormatContext *s)
195{
196 SegmentContext *seg = s->priv_data;
eb447d51 197 AVFormatContext *oc = NULL;
02e8f032
LB
198 int ret, i;
199
200 seg->number = 0;
201 seg->offset_time = 0;
202 seg->recording_time = seg->time * 1000000;
c864c396
MS
203 if (!seg->write_header_trailer)
204 seg->individual_header_trailer = 0;
02e8f032 205
26db9100 206 if (seg->list && seg->list_type != LIST_HLS)
02e8f032
LB
207 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
208 &s->interrupt_callback, NULL)) < 0)
3b52e9da 209 goto fail;
02e8f032 210
aa9f128b 211 for (i = 0; i < s->nb_streams; i++)
02e8f032
LB
212 seg->has_video +=
213 (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
214
215 if (seg->has_video > 1)
216 av_log(s, AV_LOG_WARNING,
217 "More than a single video stream present, "
218 "expect issues decoding it.\n");
219
eb447d51 220 seg->oformat = av_guess_format(seg->format, s->filename, NULL);
02e8f032 221
eb447d51 222 if (!seg->oformat) {
02e8f032
LB
223 ret = AVERROR_MUXER_NOT_FOUND;
224 goto fail;
225 }
eb447d51 226 if (seg->oformat->flags & AVFMT_NOFILE) {
02e8f032 227 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
e8769b37 228 seg->oformat->name);
02e8f032
LB
229 ret = AVERROR(EINVAL);
230 goto fail;
231 }
232
eb447d51
MS
233 if ((ret = segment_mux_init(s)) < 0)
234 goto fail;
235 oc = seg->avf;
02e8f032
LB
236
237 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
238 s->filename, seg->number++) < 0) {
239 ret = AVERROR(EINVAL);
240 goto fail;
241 }
242
c864c396
MS
243 if (seg->write_header_trailer) {
244 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
245 &s->interrupt_callback, NULL)) < 0)
246 goto fail;
247 } else {
248 if ((ret = open_null_ctx(&oc->pb)) < 0)
249 goto fail;
250 }
02e8f032
LB
251
252 if ((ret = avformat_write_header(oc, NULL)) < 0) {
253 avio_close(oc->pb);
254 goto fail;
255 }
256
c864c396
MS
257 if (!seg->write_header_trailer) {
258 close_null_ctx(oc->pb);
259 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
260 &s->interrupt_callback, NULL)) < 0)
261 goto fail;
262 }
263
02e8f032 264 if (seg->list) {
26db9100
LB
265 if (seg->list_type == LIST_HLS) {
266 if ((ret = segment_hls_window(s, 0)) < 0)
267 goto fail;
268 } else {
269 avio_printf(seg->pb, "%s\n", oc->filename);
270 avio_flush(seg->pb);
271 }
02e8f032
LB
272 }
273
274fail:
b3f04657
LB
275 if (ret < 0)
276 seg_free_context(seg);
277
02e8f032
LB
278 return ret;
279}
280
281static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
282{
283 SegmentContext *seg = s->priv_data;
284 AVFormatContext *oc = seg->avf;
0edae4e6 285 AVStream *st = s->streams[pkt->stream_index];
02e8f032 286 int64_t end_pts = seg->recording_time * seg->number;
cf679b94 287 int ret, can_split = 1;
02e8f032 288
b3f04657
LB
289 if (!oc)
290 return AVERROR(EINVAL);
291
cf679b94
AK
292 if (seg->has_video) {
293 can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
294 pkt->flags & AV_PKT_FLAG_KEY;
295 }
02e8f032 296
cf679b94
AK
297 if (can_split && av_compare_ts(pkt->pts, st->time_base, end_pts,
298 AV_TIME_BASE_Q) >= 0) {
02e8f032
LB
299 av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
300 pkt->stream_index, pkt->pts);
301
378a6315 302 ret = segment_end(oc, seg->individual_header_trailer);
02e8f032
LB
303
304 if (!ret)
378a6315 305 ret = segment_start(s, seg->individual_header_trailer);
02e8f032
LB
306
307 if (ret)
308 goto fail;
309
eb447d51
MS
310 oc = seg->avf;
311
02e8f032 312 if (seg->list) {
26db9100
LB
313 if (seg->list_type == LIST_HLS) {
314 if ((ret = segment_hls_window(s, 0)) < 0)
02e8f032 315 goto fail;
26db9100
LB
316 } else {
317 avio_printf(seg->pb, "%s\n", oc->filename);
318 avio_flush(seg->pb);
319 if (seg->size && !(seg->number % seg->size)) {
320 avio_closep(&seg->pb);
321 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
322 &s->interrupt_callback, NULL)) < 0)
323 goto fail;
324 }
02e8f032
LB
325 }
326 }
327 }
328
0edae4e6 329 ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
02e8f032
LB
330
331fail:
b3f04657
LB
332 if (ret < 0)
333 seg_free_context(seg);
02e8f032
LB
334
335 return ret;
336}
337
338static int seg_write_trailer(struct AVFormatContext *s)
339{
340 SegmentContext *seg = s->priv_data;
341 AVFormatContext *oc = seg->avf;
b3f04657
LB
342 int ret = 0;
343
344 if (!oc)
345 goto fail;
346
c864c396 347 if (!seg->write_header_trailer) {
26db9100
LB
348 if ((ret = segment_end(oc, 0)) < 0)
349 goto fail;
c864c396 350 open_null_ctx(&oc->pb);
26db9100 351 ret = av_write_trailer(oc);
c864c396
MS
352 close_null_ctx(oc->pb);
353 } else {
354 ret = segment_end(oc, 1);
355 }
26db9100
LB
356
357 if (ret < 0)
358 goto fail;
359
360 if (seg->list && seg->list_type == LIST_HLS) {
361 if ((ret = segment_hls_window(s, 1) < 0))
362 goto fail;
363 }
364
365fail:
366 avio_close(seg->pb);
02e8f032
LB
367 avformat_free_context(oc);
368 return ret;
369}
370
371#define OFFSET(x) offsetof(SegmentContext, x)
372#define E AV_OPT_FLAG_ENCODING_PARAM
373static const AVOption options[] = {
374 { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
375 { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
376 { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
e6153f17 377 { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E },
26db9100
LB
378 { "segment_list_type", "segment list format", OFFSET(list_type), AV_OPT_TYPE_INT, {.i64 = LIST_FLAT}, 0, 2, E, "list_type" },
379 { "flat", "plain list (default)", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_FLAT}, 0, 0, E, "list_type" },
380 { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_HLS}, 0, 0, E, "list_type" },
e6153f17 381 { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
5c08ae4f 382 { "segment_list_entry_prefix", "base url prefix for segments", OFFSET(entry_prefix), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
378a6315 383 { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
c864c396 384 { "write_header_trailer", "write a header to the first segment and a trailer to the last one", OFFSET(write_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
02e8f032
LB
385 { NULL },
386};
387
388static const AVClass seg_class = {
389 .class_name = "segment muxer",
390 .item_name = av_default_item_name,
391 .option = options,
392 .version = LIBAVUTIL_VERSION_INT,
393};
394
395
396AVOutputFormat ff_segment_muxer = {
397 .name = "segment",
0177b7d2 398 .long_name = NULL_IF_CONFIG_SMALL("segment"),
02e8f032 399 .priv_data_size = sizeof(SegmentContext),
0826d851 400 .flags = AVFMT_NOFILE,
02e8f032
LB
401 .write_header = seg_write_header,
402 .write_packet = seg_write_packet,
403 .write_trailer = seg_write_trailer,
404 .priv_class = &seg_class,
405};