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"
28 #include "libavformat/network.h"
30 #define MAX_TST_PACKETS 128
31 #define SLEEPTIME_50_MS 50000
32 #define SLEEPTIME_10_MS 10000
34 /* Implementation of mock muxer to simulate real muxer failures */
36 /* This is structure of data sent in packets to
38 typedef struct FailingMuxerPacketData {
39 int ret; /* return value of write_packet call*/
40 int recover_after; /* set ret to zero after this number of recovery attempts */
41 unsigned sleep_time; /* sleep for this long in write_packet to simulate long I/O operation */
42 } FailingMuxerPacketData;
45 typedef struct FailingMuxerContext {
48 int write_trailer_ret;
49 /* If non-zero, summary of processed packets will be printed in deinit */
50 int print_deinit_summary;
53 int pts_written[MAX_TST_PACKETS];
55 } FailingMuxerContext;
57 static int failing_write_header(AVFormatContext *avf)
59 FailingMuxerContext *ctx = avf->priv_data;
60 return ctx->write_header_ret;
63 static int failing_write_packet(AVFormatContext *avf, AVPacket *pkt)
65 FailingMuxerContext *ctx = avf->priv_data;
70 FailingMuxerPacketData *data = (FailingMuxerPacketData*) pkt->data;
72 if (!data->recover_after) {
75 data->recover_after--;
80 if (data->sleep_time) {
82 while (slept < data->sleep_time) {
83 if (ff_check_interrupt(&avf->interrupt_callback))
85 av_usleep(SLEEPTIME_10_MS);
86 slept += SLEEPTIME_10_MS;
91 ctx->pts_written[ctx->pts_written_nr++] = pkt->pts;
98 static int failing_write_trailer(AVFormatContext *avf)
100 FailingMuxerContext *ctx = avf->priv_data;
101 return ctx->write_trailer_ret;
104 static void failing_deinit(AVFormatContext *avf)
107 FailingMuxerContext *ctx = avf->priv_data;
109 if (!ctx->print_deinit_summary)
112 printf("flush count: %d\n", ctx->flush_count);
113 printf("pts seen nr: %d\n", ctx->pts_written_nr);
114 printf("pts seen: ");
115 for (i = 0; i < ctx->pts_written_nr; ++i ) {
116 printf(i ? ",%d" : "%d", ctx->pts_written[i]);
120 #define OFFSET(x) offsetof(FailingMuxerContext, x)
121 static const AVOption options[] = {
122 {"write_header_ret", "write_header() return value", OFFSET(write_header_ret),
123 AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
124 {"write_trailer_ret", "write_trailer() return value", OFFSET(write_trailer_ret),
125 AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
126 {"print_deinit_summary", "print summary when deinitializing muxer", OFFSET(print_deinit_summary),
127 AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
131 static const AVClass failing_muxer_class = {
132 .class_name = "Failing test muxer",
133 .item_name = av_default_item_name,
135 .version = LIBAVUTIL_VERSION_INT,
138 AVOutputFormat tst_failing_muxer = {
140 .long_name = NULL_IF_CONFIG_SMALL("Failing test muxer"),
141 .priv_data_size = sizeof(FailingMuxerContext),
142 .write_header = failing_write_header,
143 .write_packet = failing_write_packet,
144 .write_trailer = failing_write_trailer,
145 .deinit = failing_deinit,
146 .priv_class = &failing_muxer_class,
147 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
150 static int prepare_packet(AVPacket *pkt,const FailingMuxerPacketData *pkt_data, int64_t pts)
153 FailingMuxerPacketData *data = av_malloc(sizeof(*data));
155 return AVERROR(ENOMEM);
157 memcpy(data, pkt_data, sizeof(FailingMuxerPacketData));
158 ret = av_packet_from_data(pkt, (uint8_t*) data, sizeof(*data));
160 pkt->pts = pkt->dts = pts;
166 static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc)
171 ret = avformat_alloc_output_context2(oc, NULL, "fifo", "-");
173 fprintf(stderr, "Failed to create format context: %s\n",
178 s = avformat_new_stream(*oc, NULL);
180 fprintf(stderr, "Failed to create stream: %s\n",
182 ret = AVERROR(ENOMEM);
188 static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts,
189 const FailingMuxerPacketData *pkt_data)
194 av_init_packet(&pkt);
197 ret = avformat_write_header(oc, opts);
199 fprintf(stderr, "Unexpected write_header failure: %s\n",
204 for (i = 0; i < 15; i++ ) {
205 ret = prepare_packet(&pkt, pkt_data, i);
207 fprintf(stderr, "Failed to prepare test packet: %s\n",
209 goto write_trailer_and_fail;
211 ret = av_write_frame(oc, &pkt);
212 av_packet_unref(&pkt);
214 fprintf(stderr, "Unexpected write_frame error: %s\n",
216 goto write_trailer_and_fail;
220 ret = av_write_frame(oc, NULL);
222 fprintf(stderr, "Unexpected write_frame error during flushing: %s\n",
224 goto write_trailer_and_fail;
227 ret = av_write_trailer(oc);
229 fprintf(stderr, "Unexpected write_trailer error during flushing: %s\n",
235 write_trailer_and_fail:
236 av_write_trailer(oc);
241 static int fifo_write_header_err_tst(AVFormatContext *oc, AVDictionary **opts,
242 const FailingMuxerPacketData *pkt_data)
247 av_init_packet(&pkt);
249 ret = avformat_write_header(oc, opts);
251 fprintf(stderr, "Unexpected write_header failure: %s\n",
256 for (i = 0; i < MAX_TST_PACKETS; i++ ) {
257 ret = prepare_packet(&pkt, pkt_data, i);
259 fprintf(stderr, "Failed to prepare test packet: %s\n",
261 goto write_trailer_and_fail;
263 ret = av_write_frame(oc, &pkt);
264 av_packet_unref(&pkt);
271 fprintf(stderr, "write_packet not failed when supposed to.\n");
273 } else if (ret != -1) {
274 fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
278 ret = av_write_trailer(oc);
280 fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
283 write_trailer_and_fail:
284 av_write_trailer(oc);
289 static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
290 const FailingMuxerPacketData *data)
293 int64_t write_pkt_start, write_pkt_end, duration;
296 av_init_packet(&pkt);
298 ret = avformat_write_header(oc, opts);
300 fprintf(stderr, "Unexpected write_header failure: %s\n",
305 write_pkt_start = av_gettime_relative();
306 for (i = 0; i < 6; i++ ) {
307 ret = prepare_packet(&pkt, data, i);
309 fprintf(stderr, "Failed to prepare test packet: %s\n",
313 ret = av_write_frame(oc, &pkt);
314 av_packet_unref(&pkt);
319 write_pkt_end = av_gettime_relative();
320 duration = write_pkt_end - write_pkt_start;
321 if (duration > (SLEEPTIME_50_MS*6)/2) {
322 fprintf(stderr, "Writing packets to fifo muxer took too much time while testing"
323 "buffer overflow with drop_pkts_on_overflow was on.\n");
329 fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
333 ret = av_write_trailer(oc);
335 fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
339 av_write_trailer(oc);
343 typedef struct TestCase {
344 int (*test_func)(AVFormatContext *, AVDictionary **,const FailingMuxerPacketData *pkt_data);
345 const char *test_name;
348 uint8_t print_summary_on_deinit;
349 int write_header_ret;
350 int write_trailer_ret;
352 FailingMuxerPacketData pkt_data;
356 #define BUFFER_SIZE 64
358 static int run_test(const TestCase *test)
360 AVDictionary *opts = NULL;
361 AVFormatContext *oc = NULL;
362 char buffer[BUFFER_SIZE];
365 ret = initialize_fifo_tst_muxer_chain(&oc);
367 fprintf(stderr, "Muxer initialization failed: %s\n", av_err2str(ret));
372 ret = av_dict_parse_string(&opts, test->options, "=", ":", 0);
374 fprintf(stderr, "Failed to parse options: %s\n", av_err2str(ret));
379 snprintf(buffer, BUFFER_SIZE,
380 "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
381 (int)test->print_summary_on_deinit, test->write_header_ret,
382 test->write_trailer_ret);
383 ret = av_dict_set(&opts, "format_opts", buffer, 0);
384 ret1 = av_dict_set(&opts, "fifo_format", "fail", 0);
385 if (ret < 0 || ret1 < 0) {
386 fprintf(stderr, "Failed to set options for test muxer: %s\n",
391 ret = test->test_func(oc, &opts, &test->pkt_data);
394 printf("%s: %s\n", test->test_name, ret < 0 ? "fail" : "ok");
395 avformat_free_context(oc);
401 const TestCase tests[] = {
402 /* Simple test in packet-non-dropping mode, we expect to get on the output
403 * exactly what was on input */
404 {fifo_basic_test, "nonfail test", NULL,1, 0, 0, {0, 0, 0}},
406 /* Test that we receive delayed write_header error from one of the write_packet
408 {fifo_write_header_err_tst, "write header error test", NULL, 0, -1, 0, {0, 0, 0}},
410 /* Each write_packet will fail 3 times before operation is successful. If recovery
411 * Since recovery is on, fifo muxer should not return any errors. */
412 {fifo_basic_test, "recovery test", "attempt_recovery=1:recovery_wait_time=0",
413 0, 0, 0, {AVERROR(ETIMEDOUT), 3, 0}},
415 /* By setting low queue_size and sending packets with longer processing time,
416 * this test will cause queue to overflow, since drop_pkts_on_overflow is off
417 * by default, all packets should be processed and fifo should block on full
419 {fifo_basic_test, "overflow without packet dropping","queue_size=3",
420 1, 0, 0, {0, 0, SLEEPTIME_10_MS}},
422 /* The test as the upper one, except that drop_on_overflow is turned on. In this case
423 * fifo should not block when the queue is full and slow down producer, so the test
424 * measures time producer spends on write_packet calls which should be significantly
425 * less than number_of_pkts * 50 MS.
427 {fifo_overflow_drop_test, "overflow with packet dropping", "queue_size=3:drop_pkts_on_overflow=1",
428 0, 0, 0, {0, 0, SLEEPTIME_50_MS}},
433 int main(int argc, char *argv[])
435 int i, ret, ret_all = 0;
438 av_register_output_format(&tst_failing_muxer);
440 for (i = 0; tests[i].test_func; i++) {
441 ret = run_test(&tests[i]);
442 if (!ret_all && ret < 0)