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)
45 FailingMuxerPacketData *data = av_malloc(sizeof(*data));
47 return AVERROR(ENOMEM);
49 memcpy(data, pkt_data, sizeof(FailingMuxerPacketData));
50 ret = av_packet_from_data(pkt, (uint8_t*) data, sizeof(*data));
52 pkt->pts = pkt->dts = pts;
58 static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc)
63 ret = avformat_alloc_output_context2(oc, NULL, "fifo", "-");
65 fprintf(stderr, "Failed to create format context: %s\n",
70 s = avformat_new_stream(*oc, NULL);
72 fprintf(stderr, "Failed to create stream: %s\n",
74 ret = AVERROR(ENOMEM);
80 static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts,
81 const FailingMuxerPacketData *pkt_data)
89 ret = avformat_write_header(oc, opts);
91 fprintf(stderr, "Unexpected write_header failure: %s\n",
96 for (i = 0; i < 15; i++ ) {
97 ret = prepare_packet(&pkt, pkt_data, i);
99 fprintf(stderr, "Failed to prepare test packet: %s\n",
101 goto write_trailer_and_fail;
103 ret = av_write_frame(oc, &pkt);
104 av_packet_unref(&pkt);
106 fprintf(stderr, "Unexpected write_frame error: %s\n",
108 goto write_trailer_and_fail;
112 ret = av_write_frame(oc, NULL);
114 fprintf(stderr, "Unexpected write_frame error during flushing: %s\n",
116 goto write_trailer_and_fail;
119 ret = av_write_trailer(oc);
121 fprintf(stderr, "Unexpected write_trailer error during flushing: %s\n",
127 write_trailer_and_fail:
128 av_write_trailer(oc);
133 static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
134 const FailingMuxerPacketData *data)
137 int64_t write_pkt_start, write_pkt_end, duration;
140 av_init_packet(&pkt);
142 ret = avformat_write_header(oc, opts);
144 fprintf(stderr, "Unexpected write_header failure: %s\n",
149 write_pkt_start = av_gettime_relative();
150 for (i = 0; i < 6; i++ ) {
151 ret = prepare_packet(&pkt, data, i);
153 fprintf(stderr, "Failed to prepare test packet: %s\n",
157 ret = av_write_frame(oc, &pkt);
158 av_packet_unref(&pkt);
163 write_pkt_end = av_gettime_relative();
164 duration = write_pkt_end - write_pkt_start;
165 if (duration > (SLEEPTIME_50_MS*6)/2) {
166 fprintf(stderr, "Writing packets to fifo muxer took too much time while testing"
167 "buffer overflow with drop_pkts_on_overflow was on.\n");
173 fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
177 ret = av_write_trailer(oc);
179 fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
183 av_write_trailer(oc);
187 typedef struct TestCase {
188 int (*test_func)(AVFormatContext *, AVDictionary **,const FailingMuxerPacketData *pkt_data);
189 const char *test_name;
192 uint8_t print_summary_on_deinit;
193 int write_header_ret;
194 int write_trailer_ret;
196 FailingMuxerPacketData pkt_data;
200 #define BUFFER_SIZE 64
202 static int run_test(const TestCase *test)
204 AVDictionary *opts = NULL;
205 AVFormatContext *oc = NULL;
206 char buffer[BUFFER_SIZE];
209 ret = initialize_fifo_tst_muxer_chain(&oc);
211 fprintf(stderr, "Muxer initialization failed: %s\n", av_err2str(ret));
216 ret = av_dict_parse_string(&opts, test->options, "=", ":", 0);
218 fprintf(stderr, "Failed to parse options: %s\n", av_err2str(ret));
223 snprintf(buffer, BUFFER_SIZE,
224 "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
225 (int)test->print_summary_on_deinit, test->write_header_ret,
226 test->write_trailer_ret);
227 ret = av_dict_set(&opts, "format_opts", buffer, 0);
228 ret1 = av_dict_set(&opts, "fifo_format", "fifo_test", 0);
229 if (ret < 0 || ret1 < 0) {
230 fprintf(stderr, "Failed to set options for test muxer: %s\n",
235 ret = test->test_func(oc, &opts, &test->pkt_data);
238 printf("%s: %s\n", test->test_name, ret < 0 ? "fail" : "ok");
239 avformat_free_context(oc);
245 const TestCase tests[] = {
246 /* Simple test in packet-non-dropping mode, we expect to get on the output
247 * exactly what was on input */
248 {fifo_basic_test, "nonfail test", NULL,1, 0, 0, {0, 0, 0}},
250 /* Each write_packet will fail 3 times before operation is successful. If recovery
251 * Since recovery is on, fifo muxer should not return any errors. */
252 {fifo_basic_test, "recovery test", "attempt_recovery=1:recovery_wait_time=0",
253 0, 0, 0, {AVERROR(ETIMEDOUT), 3, 0}},
255 /* By setting low queue_size and sending packets with longer processing time,
256 * this test will cause queue to overflow, since drop_pkts_on_overflow is off
257 * by default, all packets should be processed and fifo should block on full
259 {fifo_basic_test, "overflow without packet dropping","queue_size=3",
260 1, 0, 0, {0, 0, SLEEPTIME_10_MS}},
262 /* The test as the upper one, except that drop_on_overflow is turned on. In this case
263 * fifo should not block when the queue is full and slow down producer, so the test
264 * measures time producer spends on write_packet calls which should be significantly
265 * less than number_of_pkts * 50 MS.
267 {fifo_overflow_drop_test, "overflow with packet dropping", "queue_size=3:drop_pkts_on_overflow=1",
268 0, 0, 0, {0, 0, SLEEPTIME_50_MS}},
273 int main(int argc, char *argv[])
275 int i, ret, ret_all = 0;
277 for (i = 0; tests[i].test_func; i++) {
278 ret = run_test(&tests[i]);
279 if (!ret_all && ret < 0)