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; ///< format to use for output segment files
38 char *list; ///< filename for the segment list file
39 int list_size; ///< number of entries for the segment list file
40 AVIOContext *list_pb; ///< list file put-byte context
41 float time; ///< segment duration
42 int wrap; ///< number after which the index wraps
43 int64_t recording_time;
47 static int segment_start(AVFormatContext *s)
49 SegmentContext *seg = s->priv_data;
50 AVFormatContext *oc = seg->avf;
54 seg->number %= seg->wrap;
56 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
57 s->filename, seg->number++) < 0) {
58 av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
59 return AVERROR(EINVAL);
62 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
63 &s->interrupt_callback, NULL)) < 0)
66 if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
67 oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
70 return AVERROR(ENOMEM);
72 if (oc->oformat->priv_class) {
73 *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
74 av_opt_set_defaults(oc->priv_data);
78 if ((err = oc->oformat->write_header(oc)) < 0) {
85 av_log(oc, AV_LOG_ERROR, "Failure occurred when starting segment '%s'\n",
88 av_freep(&oc->priv_data);
93 static int segment_end(AVFormatContext *s)
95 SegmentContext *seg = s->priv_data;
96 AVFormatContext *oc = seg->avf;
99 if (oc->oformat->write_trailer)
100 ret = oc->oformat->write_trailer(oc);
103 av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
107 if (seg->list_size && !(seg->number % seg->list_size)) {
108 avio_close(seg->list_pb);
109 if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
110 &s->interrupt_callback, NULL)) < 0)
113 avio_printf(seg->list_pb, "%s\n", oc->filename);
114 avio_flush(seg->list_pb);
119 if (oc->oformat->priv_class)
120 av_opt_free(oc->priv_data);
121 av_freep(&oc->priv_data);
126 static int seg_write_header(AVFormatContext *s)
128 SegmentContext *seg = s->priv_data;
133 seg->recording_time = seg->time * 1000000;
135 oc = avformat_alloc_context();
138 return AVERROR(ENOMEM);
141 if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
142 &s->interrupt_callback, NULL)) < 0)
145 for (i = 0; i< s->nb_streams; i++)
147 (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
149 if (seg->has_video > 1)
150 av_log(s, AV_LOG_WARNING,
151 "More than a single video stream present, "
152 "expect issues decoding it.\n");
154 oc->oformat = av_guess_format(seg->format, s->filename, NULL);
157 ret = AVERROR_MUXER_NOT_FOUND;
160 if (oc->oformat->flags & AVFMT_NOFILE) {
161 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
163 ret = AVERROR(EINVAL);
169 oc->streams = s->streams;
170 oc->nb_streams = s->nb_streams;
172 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
173 s->filename, seg->number++) < 0) {
174 ret = AVERROR(EINVAL);
178 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
179 &s->interrupt_callback, NULL)) < 0)
182 if ((ret = avformat_write_header(oc, NULL)) < 0) {
192 avformat_free_context(oc);
195 avio_close(seg->list_pb);
200 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
202 SegmentContext *seg = s->priv_data;
203 AVFormatContext *oc = seg->avf;
204 AVStream *st = oc->streams[pkt->stream_index];
205 int64_t end_pts = seg->recording_time * seg->number;
208 /* if the segment has video, start a new segment *only* with a key video frame */
209 if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
210 av_compare_ts(pkt->pts, st->time_base,
211 end_pts, AV_TIME_BASE_Q) >= 0 &&
212 pkt->flags & AV_PKT_FLAG_KEY) {
214 av_log(s, AV_LOG_DEBUG, "Next segment starts with packet stream:%d pts:%"PRId64" pts_time:%f\n",
215 pkt->stream_index, pkt->pts, pkt->pts * av_q2d(st->time_base));
217 if ((ret = segment_end(s)) < 0 || (ret = segment_start(s)) < 0)
221 ret = oc->oformat->write_packet(oc, pkt);
228 avio_close(seg->list_pb);
229 avformat_free_context(oc);
235 static int seg_write_trailer(struct AVFormatContext *s)
237 SegmentContext *seg = s->priv_data;
238 AVFormatContext *oc = seg->avf;
239 int ret = segment_end(s);
241 avio_close(seg->list_pb);
244 avformat_free_context(oc);
248 #define OFFSET(x) offsetof(SegmentContext, x)
249 #define E AV_OPT_FLAG_ENCODING_PARAM
250 static const AVOption options[] = {
251 { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
252 { "segment_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
253 { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
254 { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
255 { "segment_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
259 static const AVClass seg_class = {
260 .class_name = "segment muxer",
261 .item_name = av_default_item_name,
263 .version = LIBAVUTIL_VERSION_INT,
266 AVOutputFormat ff_segment_muxer = {
268 .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
269 .priv_data_size = sizeof(SegmentContext),
270 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
271 .write_header = seg_write_header,
272 .write_packet = seg_write_packet,
273 .write_trailer = seg_write_trailer,
274 .priv_class = &seg_class,
277 static const AVClass sseg_class = {
278 .class_name = "stream_segment muxer",
279 .item_name = av_default_item_name,
281 .version = LIBAVUTIL_VERSION_INT,
284 AVOutputFormat ff_stream_segment_muxer = {
285 .name = "stream_segment,ssegment",
286 .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
287 .priv_data_size = sizeof(SegmentContext),
288 .flags = AVFMT_NOFILE,
289 .write_header = seg_write_header,
290 .write_packet = seg_write_packet,
291 .write_trailer = seg_write_trailer,
292 .priv_class = &sseg_class,