3 * Copyright (c) 2011, 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
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"
34 const AVClass *class; /**< Class for private options. */
37 char *format; /**< Set by a private option. */
38 char *list; /**< Set by a private option. */
39 int list_size; /**< Set by a private option. */
40 float time; /**< Set by a private option. */
41 int wrap; /**< Set by a private option. */
43 int64_t recording_time;
48 static int segment_start(AVFormatContext *s)
50 SegmentContext *seg = s->priv_data;
51 AVFormatContext *oc = seg->avf;
55 seg->number %= seg->wrap;
57 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
58 s->filename, seg->number++) < 0)
59 return AVERROR(EINVAL);
61 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
62 &s->interrupt_callback, NULL)) < 0)
65 if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
66 oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
69 return AVERROR(ENOMEM);
71 if (oc->oformat->priv_class) {
72 *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
73 av_opt_set_defaults(oc->priv_data);
77 if ((err = oc->oformat->write_header(oc)) < 0) {
84 av_log(oc, AV_LOG_ERROR, "Failure occurred when starting segment '%s'\n",
87 av_freep(&oc->priv_data);
92 static int segment_end(AVFormatContext *s)
94 SegmentContext *seg = s->priv_data;
95 AVFormatContext *oc = seg->avf;
98 if (oc->oformat->write_trailer)
99 ret = oc->oformat->write_trailer(oc);
102 av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
106 if (oc->oformat->priv_class)
107 av_opt_free(oc->priv_data);
108 av_freep(&oc->priv_data);
113 static int seg_write_header(AVFormatContext *s)
115 SegmentContext *seg = s->priv_data;
120 seg->offset_time = 0;
121 seg->recording_time = seg->time * 1000000;
123 oc = avformat_alloc_context();
126 return AVERROR(ENOMEM);
129 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
130 &s->interrupt_callback, NULL)) < 0)
133 for (i = 0; i< s->nb_streams; i++)
135 (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
137 if (seg->has_video > 1)
138 av_log(s, AV_LOG_WARNING,
139 "More than a single video stream present, "
140 "expect issues decoding it.\n");
142 oc->oformat = av_guess_format(seg->format, s->filename, NULL);
145 ret = AVERROR_MUXER_NOT_FOUND;
148 if (oc->oformat->flags & AVFMT_NOFILE) {
149 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
151 ret = AVERROR(EINVAL);
157 oc->streams = s->streams;
158 oc->nb_streams = s->nb_streams;
160 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
161 s->filename, seg->number++) < 0) {
162 ret = AVERROR(EINVAL);
166 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
167 &s->interrupt_callback, NULL)) < 0)
170 if ((ret = avformat_write_header(oc, NULL)) < 0) {
176 avio_printf(seg->pb, "%s\n", oc->filename);
185 avformat_free_context(oc);
193 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
195 SegmentContext *seg = s->priv_data;
196 AVFormatContext *oc = seg->avf;
197 AVStream *st = oc->streams[pkt->stream_index];
198 int64_t end_pts = seg->recording_time * seg->number;
201 if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
202 av_compare_ts(pkt->pts, st->time_base,
203 end_pts, AV_TIME_BASE_Q) >= 0 &&
204 pkt->flags & AV_PKT_FLAG_KEY) {
206 av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
207 pkt->stream_index, pkt->pts);
209 ret = segment_end(s);
212 ret = segment_start(s);
218 avio_printf(seg->pb, "%s\n", oc->filename);
220 if (seg->list_size && !(seg->number % seg->list_size)) {
222 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
223 &s->interrupt_callback, NULL)) < 0)
229 ret = oc->oformat->write_packet(oc, pkt);
237 avformat_free_context(oc);
243 static int seg_write_trailer(struct AVFormatContext *s)
245 SegmentContext *seg = s->priv_data;
246 AVFormatContext *oc = seg->avf;
247 int ret = segment_end(s);
252 avformat_free_context(oc);
256 #define OFFSET(x) offsetof(SegmentContext, x)
257 #define E AV_OPT_FLAG_ENCODING_PARAM
258 static const AVOption options[] = {
259 { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
260 { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
261 { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
262 { "segment_list_size", "maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
263 { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
267 static const AVClass seg_class = {
268 .class_name = "segment muxer",
269 .item_name = av_default_item_name,
271 .version = LIBAVUTIL_VERSION_INT,
274 AVOutputFormat ff_segment_muxer = {
276 .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
277 .priv_data_size = sizeof(SegmentContext),
278 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
279 .write_header = seg_write_header,
280 .write_packet = seg_write_packet,
281 .write_trailer = seg_write_trailer,
282 .priv_class = &seg_class,
285 static const AVClass sseg_class = {
286 .class_name = "stream_segment muxer",
287 .item_name = av_default_item_name,
289 .version = LIBAVUTIL_VERSION_INT,
292 AVOutputFormat ff_stream_segment_muxer = {
293 .name = "stream_segment,ssegment",
294 .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
295 .priv_data_size = sizeof(SegmentContext),
296 .flags = AVFMT_NOFILE,
297 .write_header = seg_write_header,
298 .write_packet = seg_write_packet,
299 .write_trailer = seg_write_trailer,
300 .priv_class = &sseg_class,