1 /*
2 * FIFO pseudo-muxer
3 * Copyright (c) 2016 Jan Sebechlebsky
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <stdlib.h>
29
30 /*
31 * Include fifo.c directly to override libavformat/fifo.c and
32 * thereby prevent libavformat/fifo.o from being pulled in when linking.
33 * This relies on libavformat always being linked statically to its
34 * test tools (like this one).
35 * Due to FIFO_TEST, our fifo muxer will include special handling
36 * for tests, i.e. it allows to select the fifo_test muxer below
37 * even though it is not accessible via the API.
38 */
41
42 #define MAX_TST_PACKETS 128
43 #define SLEEPTIME_50_MS 50000
44 #define SLEEPTIME_10_MS 10000
45
46 /* This is structure of data sent in packets to
47 * failing muxer */
49 int ret;
/* return value of write_packet call*/
50 int recover_after;
/* set ret to zero after this number of recovery attempts */
51 unsigned sleep_time;
/* sleep for this long in write_packet to simulate long I/O operation */
53
58 /* If non-zero, summary of processed packets will be printed in deinit */
60
65
67 {
69 return ctx->write_header_ret;
70 }
71
73 {
78 } else {
80
81 if (!
data->recover_after) {
83 } else {
84 data->recover_after--;
85 }
86
88
89 if (
data->sleep_time) {
91 while (slept < data->sleep_time) {
96 }
97 }
98
102 }
103 }
105 }
106
108 {
110 return ctx->write_trailer_ret;
111 }
112
114 {
117
118 if (!
ctx->print_deinit_summary)
119 return;
120
121 printf(
"flush count: %d\n",
ctx->flush_count);
122 printf(
"pts seen nr: %d\n",
ctx->pts_written_nr);
124 for (
i = 0;
i <
ctx->pts_written_nr; ++
i ) {
126 }
128 }
129
130 #define OFF(x) offsetof(FifoTestMuxerContext, x)
132 {
"write_header_ret",
"write_header() return value",
OFF(write_header_ret),
134 {
"write_trailer_ret",
"write_trailer() return value",
OFF(write_trailer_ret),
136 {
"print_deinit_summary",
"print summary when deinitializing muxer",
OFF(print_deinit_summary),
139 };
140
146 };
147
149 .
p.
name =
"fifo_test",
161 #endif
163 };
164
165
167 {
171 memcpy(
pkt->
data, pkt_data,
sizeof(*pkt_data));
172
175
176 return 0;
177 }
178
180 {
183
186 fprintf(stderr, "Failed to create format context: %s\n",
188 return EXIT_FAILURE;
189 }
190
193 fprintf(stderr, "Failed to create stream: %s\n",
196 }
197
201
202 return 0;
203 }
204
207 {
209
212 fprintf(stderr, "Unexpected write_header failure: %s\n",
215 }
216
217 for (
i = 0;
i < 15;
i++ ) {
220 fprintf(stderr, "Failed to prepare test packet: %s\n",
222 goto write_trailer_and_fail;
223 }
227 fprintf(stderr, "Unexpected write_frame error: %s\n",
229 goto write_trailer_and_fail;
230 }
231 }
232
235 fprintf(stderr, "Unexpected write_frame error during flushing: %s\n",
237 goto write_trailer_and_fail;
238 }
239
242 fprintf(stderr, "Unexpected write_trailer error during flushing: %s\n",
245 }
246
248 write_trailer_and_fail:
252 }
253
256 {
259
262 fprintf(stderr, "Unexpected write_header failure: %s\n",
265 }
266
268 for (
i = 0;
i < 6;
i++ ) {
271 fprintf(stderr, "Failed to prepare test packet: %s\n",
274 }
278 break;
279 }
280 }
281
283 duration = write_pkt_end - write_pkt_start;
285 fprintf(stderr, "Writing packets to fifo muxer took too much time while testing"
286 "buffer overflow with drop_pkts_on_overflow was on.\n");
289 }
290
292 fprintf(stderr,
"Unexpected write_packet error: %s\n",
av_err2str(
ret));
294 }
295
298 fprintf(stderr,
"Unexpected write_trailer error: %s\n",
av_err2str(
ret));
299
304 }
305
311
315
318
319
320 #define BUFFER_SIZE 64
321
323 {
329
332 fprintf(stderr,
"Muxer initialization failed: %s\n",
av_err2str(
ret));
333 goto end;
334 }
335
339 fprintf(stderr,
"Failed to parse options: %s\n",
av_err2str(
ret));
340 goto end;
341 }
342 }
343
345 "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
346 (
int)
test->print_summary_on_deinit,
test->write_header_ret,
347 test->write_trailer_ret);
350 if (
ret < 0 || ret1 < 0) {
351 fprintf(stderr, "Failed to set options for test muxer: %s\n",
353 goto end;
354 }
355
357
358 end:
359 printf(
"%s: %s\n",
test->test_name,
ret < 0 ?
"fail" :
"ok");
364 }
365
366
368 /* Simple test in packet-non-dropping mode, we expect to get on the output
369 * exactly what was on input */
371
372 /* Each write_packet will fail 3 times before operation is successful. If recovery
373 * Since recovery is on, fifo muxer should not return any errors. */
374 {
fifo_basic_test,
"recovery test",
"attempt_recovery=1:recovery_wait_time=0",
375 0, 0, 0, {
AVERROR(ETIMEDOUT), 3, 0}},
376
377 /* By setting low queue_size and sending packets with longer processing time,
378 * this test will cause queue to overflow, since drop_pkts_on_overflow is off
379 * by default, all packets should be processed and fifo should block on full
380 * queue. */
383
384 /* The test as the upper one, except that drop_on_overflow is turned on. In this case
385 * fifo should not block when the queue is full and slow down producer, so the test
386 * measures time producer spends on write_packet calls which should be significantly
387 * less than number_of_pkts * 50 MS.
388 */
391
393 };
394
395 int main(
int argc,
char *argv[])
396 {
397 int i,
ret, ret_all = 0;
398
401 if (!ret_all &&
ret < 0)
403 }
404
406 }