2 * Copyright (c) 2015, Vignesh Venkatasubramanian
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * @file WebM Chunk Muxer
23 * The chunk muxer enables writing WebM Live chunks where there is a header
24 * chunk, followed by data chunks where each Cluster is written out as a Chunk.
32 #include "avio_internal.h"
35 #include "libavutil/avassert.h"
36 #include "libavutil/log.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/parseutils.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/time.h"
42 #include "libavutil/time_internal.h"
43 #include "libavutil/timestamp.h"
45 #define MAX_FILENAME_SIZE 1024
47 typedef struct WebMChunkContext {
49 int chunk_start_index;
50 char *header_filename;
54 uint64_t duration_written;
56 ff_const59 AVOutputFormat *oformat;
60 static int chunk_mux_init(AVFormatContext *s)
62 WebMChunkContext *wc = s->priv_data;
66 ret = avformat_alloc_output_context2(&wc->avf, wc->oformat, NULL, NULL);
71 oc->interrupt_callback = s->interrupt_callback;
72 oc->max_delay = s->max_delay;
73 av_dict_copy(&oc->metadata, s->metadata, 0);
75 *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
76 av_opt_set_defaults(oc->priv_data);
77 av_opt_set_int(oc->priv_data, "dash", 1, 0);
78 av_opt_set_int(oc->priv_data, "cluster_time_limit", wc->chunk_duration, 0);
79 av_opt_set_int(oc->priv_data, "live", 1, 0);
81 oc->streams = s->streams;
82 oc->nb_streams = s->nb_streams;
87 static int get_chunk_filename(AVFormatContext *s, int is_header, char filename[MAX_FILENAME_SIZE])
89 WebMChunkContext *wc = s->priv_data;
90 AVFormatContext *oc = wc->avf;
92 return AVERROR(EINVAL);
96 if (!wc->header_filename) {
97 av_log(oc, AV_LOG_ERROR, "No header filename provided\n");
98 return AVERROR(EINVAL);
100 len = av_strlcpy(filename, wc->header_filename, MAX_FILENAME_SIZE);
101 if (len >= MAX_FILENAME_SIZE) {
102 av_log(oc, AV_LOG_ERROR, "Header filename too long\n");
103 return AVERROR(EINVAL);
106 if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
107 s->url, wc->chunk_index - 1) < 0) {
108 av_log(oc, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->url);
109 return AVERROR(EINVAL);
115 static int webm_chunk_write_header(AVFormatContext *s)
117 WebMChunkContext *wc = s->priv_data;
118 AVFormatContext *oc = NULL;
121 AVDictionary *options = NULL;
122 char oc_filename[MAX_FILENAME_SIZE];
125 // DASH Streams can only have either one track per file.
126 if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
128 wc->chunk_index = wc->chunk_start_index;
129 wc->oformat = av_guess_format("webm", s->url, "video/webm");
131 return AVERROR_MUXER_NOT_FOUND;
132 wc->prev_pts = AV_NOPTS_VALUE;
134 ret = chunk_mux_init(s);
138 ret = get_chunk_filename(s, 1, oc_filename);
141 oc_url = av_strdup(oc_filename);
143 return AVERROR(ENOMEM);
144 ff_format_set_url(oc, oc_url);
146 av_dict_set(&options, "method", wc->http_method, 0);
147 ret = s->io_open(s, &oc->pb, oc->url, AVIO_FLAG_WRITE, &options);
148 av_dict_free(&options);
152 oc->pb->seekable = 0;
153 ret = oc->oformat->write_header(oc);
156 ff_format_io_close(s, &oc->pb);
157 for (i = 0; i < s->nb_streams; i++) {
158 // ms precision is the de-facto standard timescale for mkv files.
159 avpriv_set_pts_info(s->streams[i], 64, 1, 1000);
164 static int chunk_start(AVFormatContext *s)
166 WebMChunkContext *wc = s->priv_data;
167 AVFormatContext *oc = wc->avf;
170 ret = avio_open_dyn_buf(&oc->pb);
177 static int chunk_end(AVFormatContext *s, int flush)
179 WebMChunkContext *wc = s->priv_data;
180 AVFormatContext *oc = wc->avf;
185 char filename[MAX_FILENAME_SIZE];
186 AVDictionary *options = NULL;
192 // Flush the cluster in WebM muxer.
193 oc->oformat->write_packet(oc, NULL);
194 buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
196 ret = get_chunk_filename(s, 0, filename);
200 av_dict_set(&options, "method", wc->http_method, 0);
201 ret = s->io_open(s, &pb, filename, AVIO_FLAG_WRITE, &options);
204 avio_write(pb, buffer, buffer_size);
205 ff_format_io_close(s, &pb);
207 av_dict_free(&options);
209 return (ret < 0) ? ret : 0;
212 static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
214 WebMChunkContext *wc = s->priv_data;
215 AVFormatContext *oc = wc->avf;
216 AVStream *st = s->streams[pkt->stream_index];
219 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
220 if (wc->prev_pts != AV_NOPTS_VALUE)
221 wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
223 (AVRational) {1, 1000});
224 wc->prev_pts = pkt->pts;
227 // For video, a new chunk is started only on key frames. For audio, a new
228 // chunk is started based on chunk_duration. Also, a new chunk is started
229 // unconditionally if there is no currently open chunk.
230 if (!oc->pb || (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
231 (pkt->flags & AV_PKT_FLAG_KEY)) ||
232 (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
233 wc->duration_written >= wc->chunk_duration)) {
234 wc->duration_written = 0;
235 if ((ret = chunk_end(s, 1)) < 0 || (ret = chunk_start(s)) < 0) {
240 ret = oc->oformat->write_packet(oc, pkt);
245 static int webm_chunk_write_trailer(AVFormatContext *s)
247 WebMChunkContext *wc = s->priv_data;
248 AVFormatContext *oc = wc->avf;
252 ret = chunk_start(s);
256 oc->oformat->write_trailer(oc);
257 ret = chunk_end(s, 0);
261 avformat_free_context(oc);
265 #define OFFSET(x) offsetof(WebMChunkContext, x)
266 static const AVOption options[] = {
267 { "chunk_start_index", "start index of the chunk", OFFSET(chunk_start_index), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
268 { "header", "filename of the header where the initialization data will be written", OFFSET(header_filename), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
269 { "audio_chunk_duration", "duration of each chunk in milliseconds", OFFSET(chunk_duration), AV_OPT_TYPE_INT, {.i64 = 5000}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
270 { "method", "set the HTTP method", OFFSET(http_method), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
274 #if CONFIG_WEBM_CHUNK_MUXER
275 static const AVClass webm_chunk_class = {
276 .class_name = "WebM Chunk Muxer",
277 .item_name = av_default_item_name,
279 .version = LIBAVUTIL_VERSION_INT,
282 AVOutputFormat ff_webm_chunk_muxer = {
283 .name = "webm_chunk",
284 .long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
285 .mime_type = "video/webm",
287 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
289 .priv_data_size = sizeof(WebMChunkContext),
290 .write_header = webm_chunk_write_header,
291 .write_packet = webm_chunk_write_packet,
292 .write_trailer = webm_chunk_write_trailer,
293 .priv_class = &webm_chunk_class,