FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/webm_chunk.c
Date: 2024-05-03 15:42:48
Exec Total Coverage
Lines: 0 147 0.0%
Functions: 0 8 0.0%
Branches: 0 74 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015, Vignesh Venkatasubramanian
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
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.
25 */
26
27 #include "avformat.h"
28 #include "avio.h"
29 #include "avio_internal.h"
30 #include "internal.h"
31 #include "mux.h"
32
33 #include "libavutil/log.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/mathematics.h"
37
38 #define MAX_FILENAME_SIZE 1024
39
40 typedef struct WebMChunkContext {
41 const AVClass *class;
42 char *header_filename;
43 int chunk_duration;
44 int chunk_index;
45 char *http_method;
46 uint64_t duration_written;
47 int64_t prev_pts;
48 AVFormatContext *avf;
49 int header_written;
50 } WebMChunkContext;
51
52 static int webm_chunk_init(AVFormatContext *s)
53 {
54 WebMChunkContext *wc = s->priv_data;
55 const AVOutputFormat *oformat;
56 AVFormatContext *oc;
57 AVStream *st, *ost = s->streams[0];
58 AVDictionary *dict = NULL;
59 int ret;
60
61 // DASH Streams can only have one track per file.
62 if (s->nb_streams != 1)
63 return AVERROR(EINVAL);
64
65 if (!wc->header_filename) {
66 av_log(s, AV_LOG_ERROR, "No header filename provided\n");
67 return AVERROR(EINVAL);
68 }
69
70 wc->prev_pts = AV_NOPTS_VALUE;
71
72 oformat = av_guess_format("webm", s->url, "video/webm");
73 if (!oformat)
74 return AVERROR_MUXER_NOT_FOUND;
75
76 ret = avformat_alloc_output_context2(&wc->avf, oformat, NULL, NULL);
77 if (ret < 0)
78 return ret;
79 oc = wc->avf;
80
81 ff_format_set_url(oc, wc->header_filename);
82 wc->header_filename = NULL;
83
84 oc->interrupt_callback = s->interrupt_callback;
85 oc->max_delay = s->max_delay;
86 oc->flags = s->flags & ~AVFMT_FLAG_FLUSH_PACKETS;
87 oc->strict_std_compliance = s->strict_std_compliance;
88 oc->avoid_negative_ts = s->avoid_negative_ts;
89
90 oc->flush_packets = 0;
91
92 if ((ret = av_dict_copy(&oc->metadata, s->metadata, 0)) < 0)
93 return ret;
94
95 st = ff_stream_clone(oc, ost);
96 if (!st)
97 return AVERROR(ENOMEM);
98
99 if (wc->http_method)
100 if ((ret = av_dict_set(&dict, "method", wc->http_method, 0)) < 0)
101 return ret;
102 ret = s->io_open(s, &oc->pb, oc->url, AVIO_FLAG_WRITE, &dict);
103 av_dict_free(&dict);
104 if (ret < 0)
105 return ret;
106 oc->pb->seekable = 0;
107
108 if ((ret = av_dict_set_int(&dict, "dash", 1, 0)) < 0 ||
109 (ret = av_dict_set_int(&dict, "cluster_time_limit",
110 wc->chunk_duration, 0)) < 0 ||
111 (ret = av_dict_set_int(&dict, "live", 1, 0)) < 0)
112 goto fail;
113
114 ret = avformat_init_output(oc, &dict);
115 fail:
116 av_dict_free(&dict);
117 if (ret < 0)
118 return ret;
119
120 // Copy the timing info back to the original stream
121 // so that the timestamps of the packets are directly usable
122 avpriv_set_pts_info(ost, st->pts_wrap_bits, st->time_base.num,
123 st->time_base.den);
124
125 // This ensures that the timestamps will already be properly shifted
126 // when the packets arrive here, so we don't need to shift again.
127 s->avoid_negative_ts = oc->avoid_negative_ts;
128 ffformatcontext(s)->avoid_negative_ts_use_pts =
129 ffformatcontext(oc)->avoid_negative_ts_use_pts;
130 oc->avoid_negative_ts = AVFMT_AVOID_NEG_TS_DISABLED;
131 ffformatcontext(oc)->avoid_negative_ts_status = AVOID_NEGATIVE_TS_DISABLED;
132
133 return 0;
134 }
135
136 static int get_chunk_filename(AVFormatContext *s, char filename[MAX_FILENAME_SIZE])
137 {
138 WebMChunkContext *wc = s->priv_data;
139 if (!filename) {
140 return AVERROR(EINVAL);
141 }
142 if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
143 s->url, wc->chunk_index - 1) < 0) {
144 av_log(s, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->url);
145 return AVERROR(EINVAL);
146 }
147 return 0;
148 }
149
150 static int webm_chunk_write_header(AVFormatContext *s)
151 {
152 WebMChunkContext *wc = s->priv_data;
153 AVFormatContext *oc = wc->avf;
154 AVStream *st = s->streams[0], *ost = oc->streams[0];
155 int ret;
156
157 ret = avformat_write_header(oc, NULL);
158 ff_format_io_close(s, &oc->pb);
159 ffstream(st)->lowest_ts_allowed = ffstream(ost)->lowest_ts_allowed;
160 ffstream(ost)->lowest_ts_allowed = 0;
161 wc->header_written = 1;
162 if (ret < 0)
163 return ret;
164 return 0;
165 }
166
167 static int chunk_start(AVFormatContext *s)
168 {
169 WebMChunkContext *wc = s->priv_data;
170 AVFormatContext *oc = wc->avf;
171 int ret;
172
173 ret = avio_open_dyn_buf(&oc->pb);
174 if (ret < 0)
175 return ret;
176 wc->chunk_index++;
177 return 0;
178 }
179
180 static int chunk_end(AVFormatContext *s, int flush)
181 {
182 WebMChunkContext *wc = s->priv_data;
183 AVFormatContext *oc = wc->avf;
184 int ret;
185 int buffer_size;
186 uint8_t *buffer;
187 AVIOContext *pb;
188 char filename[MAX_FILENAME_SIZE];
189 AVDictionary *options = NULL;
190
191 if (!oc->pb)
192 return 0;
193
194 if (flush)
195 // Flush the cluster in WebM muxer.
196 av_write_frame(oc, NULL);
197 buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
198 oc->pb = NULL;
199 ret = get_chunk_filename(s, filename);
200 if (ret < 0)
201 goto fail;
202 if (wc->http_method)
203 if ((ret = av_dict_set(&options, "method", wc->http_method, 0)) < 0)
204 goto fail;
205 ret = s->io_open(s, &pb, filename, AVIO_FLAG_WRITE, &options);
206 av_dict_free(&options);
207 if (ret < 0)
208 goto fail;
209 avio_write(pb, buffer, buffer_size);
210 ff_format_io_close(s, &pb);
211 fail:
212 av_free(buffer);
213 return (ret < 0) ? ret : 0;
214 }
215
216 static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
217 {
218 WebMChunkContext *wc = s->priv_data;
219 AVFormatContext *oc = wc->avf;
220 AVStream *st = s->streams[pkt->stream_index];
221 int ret;
222
223 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
224 if (wc->prev_pts != AV_NOPTS_VALUE)
225 wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
226 st->time_base,
227 (AVRational) {1, 1000});
228 wc->prev_pts = pkt->pts;
229 }
230
231 // For video, a new chunk is started only on key frames. For audio, a new
232 // chunk is started based on chunk_duration. Also, a new chunk is started
233 // unconditionally if there is no currently open chunk.
234 if (!oc->pb || (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
235 (pkt->flags & AV_PKT_FLAG_KEY)) ||
236 (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
237 wc->duration_written >= wc->chunk_duration)) {
238 wc->duration_written = 0;
239 if ((ret = chunk_end(s, 1)) < 0 || (ret = chunk_start(s)) < 0) {
240 return ret;
241 }
242 }
243
244 // We only have one stream, so use the non-interleaving av_write_frame.
245 return av_write_frame(oc, pkt);
246 }
247
248 static int webm_chunk_write_trailer(AVFormatContext *s)
249 {
250 WebMChunkContext *wc = s->priv_data;
251 AVFormatContext *oc = wc->avf;
252 int ret;
253
254 if (!oc->pb) {
255 ret = chunk_start(s);
256 if (ret < 0)
257 return ret;
258 }
259 ret = av_write_trailer(oc);
260 if (ret < 0)
261 return ret;
262 return chunk_end(s, 0);
263 }
264
265 static void webm_chunk_deinit(AVFormatContext *s)
266 {
267 WebMChunkContext *wc = s->priv_data;
268
269 if (!wc->avf)
270 return;
271
272 if (wc->header_written)
273 ffio_free_dyn_buf(&wc->avf->pb);
274 else
275 ff_format_io_close(s, &wc->avf->pb);
276 avformat_free_context(wc->avf);
277 wc->avf = NULL;
278 }
279
280 #define OFFSET(x) offsetof(WebMChunkContext, x)
281 static const AVOption options[] = {
282 { "chunk_start_index", "start index of the chunk", OFFSET(chunk_index), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
283 { "header", "filename of the header where the initialization data will be written", OFFSET(header_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
284 { "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 },
285 { "method", "set the HTTP method", OFFSET(http_method), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
286 { NULL },
287 };
288
289 static const AVClass webm_chunk_class = {
290 .class_name = "WebM Chunk Muxer",
291 .item_name = av_default_item_name,
292 .option = options,
293 .version = LIBAVUTIL_VERSION_INT,
294 };
295
296 const FFOutputFormat ff_webm_chunk_muxer = {
297 .p.name = "webm_chunk",
298 .p.long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
299 .p.mime_type = "video/webm",
300 .p.extensions = "chk",
301 .p.flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
302 AVFMT_TS_NONSTRICT,
303 .p.priv_class = &webm_chunk_class,
304 .priv_data_size = sizeof(WebMChunkContext),
305 .init = webm_chunk_init,
306 .write_header = webm_chunk_write_header,
307 .write_packet = webm_chunk_write_packet,
308 .write_trailer = webm_chunk_write_trailer,
309 .deinit = webm_chunk_deinit,
310 };
311