FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/tests/fifo_muxer.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 113 165 68.5%
Functions: 10 10 100.0%
Branches: 47 70 67.1%

Line Branch Exec Source
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>
23 #include "libavutil/opt.h"
24 #include "libavutil/time.h"
25 #include "libavformat/avformat.h"
26 #include "libavformat/mux.h"
27 #include "libavformat/network.h"
28 #include "libavformat/url.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 */
39 #define FIFO_TEST
40 #include "libavformat/fifo.c"
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 */
48 typedef struct FailingMuxerPacketData {
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 */
52 } FailingMuxerPacketData;
53
54 typedef struct FifoTestMuxerContext {
55 AVClass *class;
56 int write_header_ret;
57 int write_trailer_ret;
58 /* If non-zero, summary of processed packets will be printed in deinit */
59 int print_deinit_summary;
60
61 int flush_count;
62 int pts_written[MAX_TST_PACKETS];
63 int pts_written_nr;
64 } FifoTestMuxerContext;
65
66 49 static int fifo_test_header(AVFormatContext *avf)
67 {
68 49 FifoTestMuxerContext *ctx = avf->priv_data;
69 49 return ctx->write_header_ret;
70 }
71
72 93 static int fifo_test_packet(AVFormatContext *avf, AVPacket *pkt)
73 {
74 93 FifoTestMuxerContext *ctx = avf->priv_data;
75 93 int ret = 0;
76
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 90 times.
93 if (!pkt) {
77 3 ctx->flush_count++;
78 } else {
79 90 FailingMuxerPacketData *data = (FailingMuxerPacketData*) pkt->data;
80
81
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 45 times.
90 if (!data->recover_after) {
82 45 data->ret = 0;
83 } else {
84 45 data->recover_after--;
85 }
86
87 90 ret = data->ret;
88
89
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 75 times.
90 if (data->sleep_time) {
90 15 int64_t slept = 0;
91
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 while (slept < data->sleep_time) {
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (ff_check_interrupt(&avf->interrupt_callback))
93 return AVERROR_EXIT;
94 15 av_usleep(SLEEPTIME_10_MS);
95 15 slept += SLEEPTIME_10_MS;
96 }
97 }
98
99
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 45 times.
90 if (!ret) {
100 45 ctx->pts_written[ctx->pts_written_nr++] = pkt->pts;
101 45 av_packet_unref(pkt);
102 }
103 }
104 93 return ret;
105 }
106
107 49 static int fifo_test_trailer(AVFormatContext *avf)
108 {
109 49 FifoTestMuxerContext *ctx = avf->priv_data;
110 49 return ctx->write_trailer_ret;
111 }
112
113 49 static void failing_deinit(AVFormatContext *avf)
114 {
115 int i;
116 49 FifoTestMuxerContext *ctx = avf->priv_data;
117
118
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 2 times.
49 if (!ctx->print_deinit_summary)
119 47 return;
120
121 2 printf("flush count: %d\n", ctx->flush_count);
122 2 printf("pts seen nr: %d\n", ctx->pts_written_nr);
123 2 printf("pts seen: ");
124
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 2 times.
32 for (i = 0; i < ctx->pts_written_nr; ++i ) {
125
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 2 times.
30 printf(i ? ",%d" : "%d", ctx->pts_written[i]);
126 }
127 2 printf("\n");
128 }
129
130 #define OFF(x) offsetof(FifoTestMuxerContext, x)
131 static const AVOption fifo_test_options[] = {
132 {"write_header_ret", "write_header() return value", OFF(write_header_ret),
133 AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
134 {"write_trailer_ret", "write_trailer() return value", OFF(write_trailer_ret),
135 AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
136 {"print_deinit_summary", "print summary when deinitializing muxer", OFF(print_deinit_summary),
137 AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
138 {NULL}
139 };
140
141 static const AVClass failing_muxer_class = {
142 .class_name = "Fifo test muxer",
143 .item_name = av_default_item_name,
144 .option = fifo_test_options,
145 .version = LIBAVUTIL_VERSION_INT,
146 };
147
148 const FFOutputFormat ff_fifo_test_muxer = {
149 .p.name = "fifo_test",
150 .p.long_name = NULL_IF_CONFIG_SMALL("Fifo test muxer"),
151 .priv_data_size = sizeof(FifoTestMuxerContext),
152 .write_header = fifo_test_header,
153 .write_packet = fifo_test_packet,
154 .write_trailer = fifo_test_trailer,
155 .deinit = failing_deinit,
156 .p.priv_class = &failing_muxer_class,
157 #if FF_API_ALLOW_FLUSH
158 .p.flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
159 #else
160 .p.flags = AVFMT_NOFILE,
161 #endif
162 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
163 };
164
165
166 51 static int prepare_packet(AVPacket *pkt, const FailingMuxerPacketData *pkt_data, int64_t pts)
167 {
168 51 int ret = av_new_packet(pkt, sizeof(*pkt_data));
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (ret < 0)
170 return ret;
171 51 memcpy(pkt->data, pkt_data, sizeof(*pkt_data));
172
173 51 pkt->pts = pkt->dts = pts;
174 51 pkt->duration = 1;
175
176 51 return 0;
177 }
178
179 4 static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc, AVPacket **pkt)
180 {
181 4 int ret = 0;
182 AVStream *s;
183
184 4 ret = avformat_alloc_output_context2(oc, NULL, "fifo", "-");
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret) {
186 fprintf(stderr, "Failed to create format context: %s\n",
187 av_err2str(ret));
188 return EXIT_FAILURE;
189 }
190
191 4 s = avformat_new_stream(*oc, NULL);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!s) {
193 fprintf(stderr, "Failed to create stream: %s\n",
194 av_err2str(ret));
195 return AVERROR(ENOMEM);
196 }
197
198 4 *pkt = av_packet_alloc();
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!*pkt)
200 return AVERROR(ENOMEM);
201
202 4 return 0;
203 }
204
205 3 static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts,
206 AVPacket *pkt, const FailingMuxerPacketData *pkt_data)
207 {
208 3 int ret = 0, i;
209
210 3 ret = avformat_write_header(oc, opts);
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret) {
212 fprintf(stderr, "Unexpected write_header failure: %s\n",
213 av_err2str(ret));
214 goto fail;
215 }
216
217
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 3 times.
48 for (i = 0; i < 15; i++ ) {
218 45 ret = prepare_packet(pkt, pkt_data, i);
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (ret < 0) {
220 fprintf(stderr, "Failed to prepare test packet: %s\n",
221 av_err2str(ret));
222 goto write_trailer_and_fail;
223 }
224 45 ret = av_write_frame(oc, pkt);
225 45 av_packet_unref(pkt);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (ret < 0) {
227 fprintf(stderr, "Unexpected write_frame error: %s\n",
228 av_err2str(ret));
229 goto write_trailer_and_fail;
230 }
231 }
232
233 3 ret = av_write_frame(oc, NULL);
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
235 fprintf(stderr, "Unexpected write_frame error during flushing: %s\n",
236 av_err2str(ret));
237 goto write_trailer_and_fail;
238 }
239
240 3 ret = av_write_trailer(oc);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
242 fprintf(stderr, "Unexpected write_trailer error during flushing: %s\n",
243 av_err2str(ret));
244 goto fail;
245 }
246
247 3 return ret;
248 write_trailer_and_fail:
249 av_write_trailer(oc);
250 fail:
251 return ret;
252 }
253
254 1 static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
255 AVPacket *pkt, const FailingMuxerPacketData *data)
256 {
257 1 int ret = 0, i;
258 int64_t write_pkt_start, write_pkt_end, duration;
259
260 1 ret = avformat_write_header(oc, opts);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret) {
262 fprintf(stderr, "Unexpected write_header failure: %s\n",
263 av_err2str(ret));
264 return ret;
265 }
266
267 1 write_pkt_start = av_gettime_relative();
268
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 for (i = 0; i < 6; i++ ) {
269 6 ret = prepare_packet(pkt, data, i);
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
271 fprintf(stderr, "Failed to prepare test packet: %s\n",
272 av_err2str(ret));
273 goto fail;
274 }
275 6 ret = av_write_frame(oc, pkt);
276 6 av_packet_unref(pkt);
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
278 break;
279 }
280 }
281
282 1 write_pkt_end = av_gettime_relative();
283 1 duration = write_pkt_end - write_pkt_start;
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (duration > (SLEEPTIME_50_MS*6)/2) {
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");
287 ret = AVERROR_BUG;
288 goto fail;
289 }
290
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret) {
292 fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
293 goto fail;
294 }
295
296 1 ret = av_write_trailer(oc);
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
298 fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
299
300 1 return ret;
301 fail:
302 av_write_trailer(oc);
303 return ret;
304 }
305
306 typedef struct TestCase {
307 int (*test_func)(AVFormatContext *, AVDictionary **,
308 AVPacket *, const FailingMuxerPacketData *pkt_data);
309 const char *test_name;
310 const char *options;
311
312 uint8_t print_summary_on_deinit;
313 int write_header_ret;
314 int write_trailer_ret;
315
316 FailingMuxerPacketData pkt_data;
317 } TestCase;
318
319
320 #define BUFFER_SIZE 64
321
322 4 static int run_test(const TestCase *test)
323 {
324 4 AVDictionary *opts = NULL;
325 4 AVFormatContext *oc = NULL;
326 4 AVPacket *pkt = NULL;
327 char buffer[BUFFER_SIZE];
328 int ret, ret1;
329
330 4 ret = initialize_fifo_tst_muxer_chain(&oc, &pkt);
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0) {
332 fprintf(stderr, "Muxer initialization failed: %s\n", av_err2str(ret));
333 goto end;
334 }
335
336
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (test->options) {
337 3 ret = av_dict_parse_string(&opts, test->options, "=", ":", 0);
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
339 fprintf(stderr, "Failed to parse options: %s\n", av_err2str(ret));
340 goto end;
341 }
342 }
343
344 4 snprintf(buffer, BUFFER_SIZE,
345 "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
346 4 (int)test->print_summary_on_deinit, test->write_header_ret,
347 4 test->write_trailer_ret);
348 4 ret = av_dict_set(&opts, "format_opts", buffer, 0);
349 4 ret1 = av_dict_set(&opts, "fifo_format", "fifo_test", 0);
350
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (ret < 0 || ret1 < 0) {
351 fprintf(stderr, "Failed to set options for test muxer: %s\n",
352 av_err2str(ret));
353 goto end;
354 }
355
356 4 ret = test->test_func(oc, &opts, pkt, &test->pkt_data);
357
358 4 end:
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 printf("%s: %s\n", test->test_name, ret < 0 ? "fail" : "ok");
360 4 avformat_free_context(oc);
361 4 av_packet_free(&pkt);
362 4 av_dict_free(&opts);
363 4 return ret;
364 }
365
366
367 const TestCase tests[] = {
368 /* Simple test in packet-non-dropping mode, we expect to get on the output
369 * exactly what was on input */
370 {fifo_basic_test, "nonfail test", NULL,1, 0, 0, {0, 0, 0}},
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. */
381 {fifo_basic_test, "overflow without packet dropping","queue_size=3",
382 1, 0, 0, {0, 0, SLEEPTIME_10_MS}},
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 */
389 {fifo_overflow_drop_test, "overflow with packet dropping", "queue_size=3:drop_pkts_on_overflow=1",
390 0, 0, 0, {0, 0, SLEEPTIME_50_MS}},
391
392 {NULL}
393 };
394
395 1 int main(int argc, char *argv[])
396 {
397 1 int i, ret, ret_all = 0;
398
399
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (i = 0; tests[i].test_func; i++) {
400 4 ret = run_test(&tests[i]);
401
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (!ret_all && ret < 0)
402 ret_all = ret;
403 }
404
405 1 return ret;
406 }
407