3 * Copyright (c) 2016 Jan Sebechlebsky
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg 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
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with FFmpeg; if not, write to the Free Software * Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/opt.h"
24 #include "libavutil/time.h"
25 #include "libavutil/avassert.h"
26 #include "libavformat/avformat.h"
27 #include "libavformat/url.h"
29 #define MAX_TST_PACKETS 128
30 #define SLEEPTIME_50_MS 50000
31 #define SLEEPTIME_10_MS 10000
33 /* Implementation of mock muxer to simulate real muxer failures */
35 /* This is structure of data sent in packets to
37 typedef struct FailingMuxerPacketData {
38 int ret; /* return value of write_packet call*/
39 int recover_after; /* set ret to zero after this number of recovery attempts */
40 unsigned sleep_time; /* sleep for this long in write_packet to simulate long I/O operation */
41 } FailingMuxerPacketData;
44 typedef struct FailingMuxerContext {
47 int write_trailer_ret;
48 /* If non-zero, summary of processed packets will be printed in deinit */
49 int print_deinit_summary;
52 int pts_written[MAX_TST_PACKETS];
54 } FailingMuxerContext;
56 static int failing_write_header(AVFormatContext *avf)
58 FailingMuxerContext *ctx = avf->priv_data;
59 return ctx->write_header_ret;
62 static int failing_write_packet(AVFormatContext *avf, AVPacket *pkt)
64 FailingMuxerContext *ctx = avf->priv_data;
69 FailingMuxerPacketData *data = (FailingMuxerPacketData*) pkt->data;
71 if (!data->recover_after) {
74 data->recover_after--;
79 if (data->sleep_time) {
81 while (slept < data->sleep_time) {
82 if (ff_check_interrupt(&avf->interrupt_callback))
84 av_usleep(SLEEPTIME_10_MS);
85 slept += SLEEPTIME_10_MS;
90 ctx->pts_written[ctx->pts_written_nr++] = pkt->pts;
97 static int failing_write_trailer(AVFormatContext *avf)
99 FailingMuxerContext *ctx = avf->priv_data;
100 return ctx->write_trailer_ret;
103 static void failing_deinit(AVFormatContext *avf)
106 FailingMuxerContext *ctx = avf->priv_data;
108 if (!ctx->print_deinit_summary)
111 printf("flush count: %d\n", ctx->flush_count);
112 printf("pts seen nr: %d\n", ctx->pts_written_nr);
113 printf("pts seen: ");
114 for (i = 0; i < ctx->pts_written_nr; ++i ) {
115 printf(i ? ",%d" : "%d", ctx->pts_written[i]);
119 #define OFFSET(x) offsetof(FailingMuxerContext, x)
120 static const AVOption options[] = {
121 {"write_header_ret", "write_header() return value", OFFSET(write_header_ret),
122 AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
123 {"write_trailer_ret", "write_trailer() return value", OFFSET(write_trailer_ret),
124 AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
125 {"print_deinit_summary", "print summary when deinitializing muxer", OFFSET(print_deinit_summary),
126 AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
130 static const AVClass failing_muxer_class = {
131 .class_name = "Failing test muxer",
132 .item_name = av_default_item_name,
134 .version = LIBAVUTIL_VERSION_INT,
137 AVOutputFormat tst_failing_muxer = {
139 .long_name = NULL_IF_CONFIG_SMALL("Failing test muxer"),
140 .priv_data_size = sizeof(FailingMuxerContext),
141 .write_header = failing_write_header,
142 .write_packet = failing_write_packet,
143 .write_trailer = failing_write_trailer,
144 .deinit = failing_deinit,
145 .priv_class = &failing_muxer_class,
146 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
149 static int prepare_packet(AVPacket *pkt,const FailingMuxerPacketData *pkt_data, int64_t pts)
152 FailingMuxerPacketData *data = av_malloc(sizeof(*data));
153 memcpy(data, pkt_data, sizeof(FailingMuxerPacketData));
154 ret = av_packet_from_data(pkt, (uint8_t*) data, sizeof(*data));
156 pkt->pts = pkt->dts = pts;
162 static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc)
167 ret = avformat_alloc_output_context2(oc, NULL, "fifo", "-");
169 fprintf(stderr, "Failed to create format context: %s\n",
174 s = avformat_new_stream(*oc, NULL);
176 fprintf(stderr, "Failed to create stream: %s\n",
178 ret = AVERROR(ENOMEM);
184 static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts,
185 const FailingMuxerPacketData *pkt_data)
190 av_init_packet(&pkt);
193 ret = avformat_write_header(oc, opts);
195 fprintf(stderr, "Unexpected write_header failure: %s\n",
200 for (i = 0; i < 15; i++ ) {
201 ret = prepare_packet(&pkt, pkt_data, i);
203 fprintf(stderr, "Failed to prepare test packet: %s\n",
205 goto write_trailer_and_fail;
207 ret = av_write_frame(oc, &pkt);
208 av_packet_unref(&pkt);
210 fprintf(stderr, "Unexpected write_frame error: %s\n",
212 goto write_trailer_and_fail;
216 ret = av_write_frame(oc, NULL);
218 fprintf(stderr, "Unexpected write_frame error during flushing: %s\n",
220 goto write_trailer_and_fail;
223 ret = av_write_trailer(oc);
225 fprintf(stderr, "Unexpected write_trailer error during flushing: %s\n",
231 write_trailer_and_fail:
232 av_write_trailer(oc);
237 static int fifo_write_header_err_tst(AVFormatContext *oc, AVDictionary **opts,
238 const FailingMuxerPacketData *pkt_data)
243 av_init_packet(&pkt);
245 ret = avformat_write_header(oc, opts);
247 fprintf(stderr, "Unexpected write_header failure: %s\n",
252 for (i = 0; i < MAX_TST_PACKETS; i++ ) {
253 ret = prepare_packet(&pkt, pkt_data, i);
255 fprintf(stderr, "Failed to prepare test packet: %s\n",
257 goto write_trailer_and_fail;
259 ret = av_write_frame(oc, &pkt);
260 av_packet_unref(&pkt);
267 fprintf(stderr, "write_packet not failed when supposed to.\n");
269 } else if (ret != -1) {
270 fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
274 ret = av_write_trailer(oc);
276 fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
279 write_trailer_and_fail:
280 av_write_trailer(oc);
285 static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
286 const FailingMuxerPacketData *data)
289 int64_t write_pkt_start, write_pkt_end, duration;
292 av_init_packet(&pkt);
294 ret = avformat_write_header(oc, opts);
296 fprintf(stderr, "Unexpected write_header failure: %s\n",
301 write_pkt_start = av_gettime_relative();
302 for (i = 0; i < 6; i++ ) {
303 ret = prepare_packet(&pkt, data, i);
305 fprintf(stderr, "Failed to prepare test packet: %s\n",
309 ret = av_write_frame(oc, &pkt);
310 av_packet_unref(&pkt);
315 write_pkt_end = av_gettime_relative();
316 duration = write_pkt_end - write_pkt_start;
317 if (duration > (SLEEPTIME_50_MS*6)/2) {
318 fprintf(stderr, "Writing packets to fifo muxer took too much time while testing"
319 "buffer overflow with drop_pkts_on_overflow was on.\n");
325 fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
329 ret = av_write_trailer(oc);
331 fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
335 av_write_trailer(oc);
339 typedef struct TestCase {
340 int (*test_func)(AVFormatContext *, AVDictionary **,const FailingMuxerPacketData *pkt_data);
341 const char *test_name;
344 uint8_t print_summary_on_deinit;
345 int write_header_ret;
346 int write_trailer_ret;
348 FailingMuxerPacketData pkt_data;
352 #define BUFFER_SIZE 64
354 static int run_test(const TestCase *test)
356 AVDictionary *opts = NULL;
357 AVFormatContext *oc = NULL;
358 char buffer[BUFFER_SIZE];
361 ret = initialize_fifo_tst_muxer_chain(&oc);
363 fprintf(stderr, "Muxer initialization failed: %s\n", av_err2str(ret));
368 ret = av_dict_parse_string(&opts, test->options, "=", ":", 0);
370 fprintf(stderr, "Failed to parse options: %s\n", av_err2str(ret));
375 snprintf(buffer, BUFFER_SIZE,
376 "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
377 (int)test->print_summary_on_deinit, test->write_header_ret,
378 test->write_trailer_ret);
379 ret = av_dict_set(&opts, "format_opts", buffer, 0);
380 ret1 = av_dict_set(&opts, "fifo_format", "fail", 0);
381 if (ret < 0 || ret1 < 0) {
382 fprintf(stderr, "Failed to set options for test muxer: %s\n",
387 ret = test->test_func(oc, &opts, &test->pkt_data);
390 printf("%s: %s\n", test->test_name, ret < 0 ? "fail" : "ok");
391 avformat_free_context(oc);
397 const TestCase tests[] = {
398 /* Simple test in packet-non-dropping mode, we expect to get on the output
399 * exactly what was on input */
400 {fifo_basic_test, "nonfail test", NULL,1, 0, 0, {0, 0, 0}},
402 /* Test that we receive delayed write_header error from one of the write_packet
404 {fifo_write_header_err_tst, "write header error test", NULL, 0, -1, 0, {0, 0, 0}},
406 /* Each write_packet will fail 3 times before operation is successful. If recovery
407 * Since recovery is on, fifo muxer should not return any errors. */
408 {fifo_basic_test, "recovery test", "attempt_recovery=1:recovery_wait_time=0",
409 0, 0, 0, {AVERROR(ETIMEDOUT), 3, 0}},
411 /* By setting low queue_size and sending packets with longer processing time,
412 * this test will cause queue to overflow, since drop_pkts_on_overflow is off
413 * by default, all packets should be processed and fifo should block on full
415 {fifo_basic_test, "overflow without packet dropping","queue_size=3",
416 1, 0, 0, {0, 0, SLEEPTIME_10_MS}},
418 /* The test as the upper one, except that drop_on_overflow is turned on. In this case
419 * fifo should not block when the queue is full and slow down producer, so the test
420 * measures time producer spends on write_packet calls which should be significantly
421 * less than number_of_pkts * 50 MS.
423 {fifo_overflow_drop_test, "overflow with packet dropping", "queue_size=3:drop_pkts_on_overflow=1",
424 0, 0, 0, {0, 0, SLEEPTIME_50_MS}},
429 int main(int argc, char *argv[])
431 int i, ret, ret_all = 0;
434 av_register_output_format(&tst_failing_muxer);
436 for (i = 0; tests[i].test_func; i++) {
437 ret = run_test(&tests[i]);
438 if (!ret_all && ret < 0)