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 /* This is structure of data sent in packets to
36 typedef struct FailingMuxerPacketData {
37 int ret; /* return value of write_packet call*/
38 int recover_after; /* set ret to zero after this number of recovery attempts */
39 unsigned sleep_time; /* sleep for this long in write_packet to simulate long I/O operation */
40 } FailingMuxerPacketData;
42 static int prepare_packet(AVPacket *pkt, const FailingMuxerPacketData *pkt_data, int64_t pts)
44 int ret = av_new_packet(pkt, sizeof(*pkt_data));
47 memcpy(pkt->data, pkt_data, sizeof(*pkt_data));
49 pkt->pts = pkt->dts = pts;
55 static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc, AVPacket **pkt)
60 ret = avformat_alloc_output_context2(oc, NULL, "fifo", "-");
62 fprintf(stderr, "Failed to create format context: %s\n",
67 s = avformat_new_stream(*oc, NULL);
69 fprintf(stderr, "Failed to create stream: %s\n",
71 return AVERROR(ENOMEM);
74 *pkt = av_packet_alloc();
76 return AVERROR(ENOMEM);
81 static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts,
82 AVPacket *pkt, const FailingMuxerPacketData *pkt_data)
86 ret = avformat_write_header(oc, opts);
88 fprintf(stderr, "Unexpected write_header failure: %s\n",
93 for (i = 0; i < 15; i++ ) {
94 ret = prepare_packet(pkt, pkt_data, i);
96 fprintf(stderr, "Failed to prepare test packet: %s\n",
98 goto write_trailer_and_fail;
100 ret = av_write_frame(oc, pkt);
101 av_packet_unref(pkt);
103 fprintf(stderr, "Unexpected write_frame error: %s\n",
105 goto write_trailer_and_fail;
109 ret = av_write_frame(oc, NULL);
111 fprintf(stderr, "Unexpected write_frame error during flushing: %s\n",
113 goto write_trailer_and_fail;
116 ret = av_write_trailer(oc);
118 fprintf(stderr, "Unexpected write_trailer error during flushing: %s\n",
124 write_trailer_and_fail:
125 av_write_trailer(oc);
130 static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
131 AVPacket *pkt, const FailingMuxerPacketData *data)
134 int64_t write_pkt_start, write_pkt_end, duration;
136 ret = avformat_write_header(oc, opts);
138 fprintf(stderr, "Unexpected write_header failure: %s\n",
143 write_pkt_start = av_gettime_relative();
144 for (i = 0; i < 6; i++ ) {
145 ret = prepare_packet(pkt, data, i);
147 fprintf(stderr, "Failed to prepare test packet: %s\n",
151 ret = av_write_frame(oc, pkt);
152 av_packet_unref(pkt);
158 write_pkt_end = av_gettime_relative();
159 duration = write_pkt_end - write_pkt_start;
160 if (duration > (SLEEPTIME_50_MS*6)/2) {
161 fprintf(stderr, "Writing packets to fifo muxer took too much time while testing"
162 "buffer overflow with drop_pkts_on_overflow was on.\n");
168 fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
172 ret = av_write_trailer(oc);
174 fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
178 av_write_trailer(oc);
182 typedef struct TestCase {
183 int (*test_func)(AVFormatContext *, AVDictionary **,
184 AVPacket *, const FailingMuxerPacketData *pkt_data);
185 const char *test_name;
188 uint8_t print_summary_on_deinit;
189 int write_header_ret;
190 int write_trailer_ret;
192 FailingMuxerPacketData pkt_data;
196 #define BUFFER_SIZE 64
198 static int run_test(const TestCase *test)
200 AVDictionary *opts = NULL;
201 AVFormatContext *oc = NULL;
202 AVPacket *pkt = NULL;
203 char buffer[BUFFER_SIZE];
206 ret = initialize_fifo_tst_muxer_chain(&oc, &pkt);
208 fprintf(stderr, "Muxer initialization failed: %s\n", av_err2str(ret));
213 ret = av_dict_parse_string(&opts, test->options, "=", ":", 0);
215 fprintf(stderr, "Failed to parse options: %s\n", av_err2str(ret));
220 snprintf(buffer, BUFFER_SIZE,
221 "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
222 (int)test->print_summary_on_deinit, test->write_header_ret,
223 test->write_trailer_ret);
224 ret = av_dict_set(&opts, "format_opts", buffer, 0);
225 ret1 = av_dict_set(&opts, "fifo_format", "fifo_test", 0);
226 if (ret < 0 || ret1 < 0) {
227 fprintf(stderr, "Failed to set options for test muxer: %s\n",
232 ret = test->test_func(oc, &opts, pkt, &test->pkt_data);
235 printf("%s: %s\n", test->test_name, ret < 0 ? "fail" : "ok");
236 avformat_free_context(oc);
237 av_packet_free(&pkt);
243 const TestCase tests[] = {
244 /* Simple test in packet-non-dropping mode, we expect to get on the output
245 * exactly what was on input */
246 {fifo_basic_test, "nonfail test", NULL,1, 0, 0, {0, 0, 0}},
248 /* Each write_packet will fail 3 times before operation is successful. If recovery
249 * Since recovery is on, fifo muxer should not return any errors. */
250 {fifo_basic_test, "recovery test", "attempt_recovery=1:recovery_wait_time=0",
251 0, 0, 0, {AVERROR(ETIMEDOUT), 3, 0}},
253 /* By setting low queue_size and sending packets with longer processing time,
254 * this test will cause queue to overflow, since drop_pkts_on_overflow is off
255 * by default, all packets should be processed and fifo should block on full
257 {fifo_basic_test, "overflow without packet dropping","queue_size=3",
258 1, 0, 0, {0, 0, SLEEPTIME_10_MS}},
260 /* The test as the upper one, except that drop_on_overflow is turned on. In this case
261 * fifo should not block when the queue is full and slow down producer, so the test
262 * measures time producer spends on write_packet calls which should be significantly
263 * less than number_of_pkts * 50 MS.
265 {fifo_overflow_drop_test, "overflow with packet dropping", "queue_size=3:drop_pkts_on_overflow=1",
266 0, 0, 0, {0, 0, SLEEPTIME_50_MS}},
271 int main(int argc, char *argv[])
273 int i, ret, ret_all = 0;
275 for (i = 0; tests[i].test_func; i++) {
276 ret = run_test(&tests[i]);
277 if (!ret_all && ret < 0)