FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/tests/fifo_muxer.c
Date: 2025-04-25 22:50:00
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 .p.flags = AVFMT_NOFILE,
158 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
159 };
160
161
162 51 static int prepare_packet(AVPacket *pkt, const FailingMuxerPacketData *pkt_data, int64_t pts)
163 {
164 51 int ret = av_new_packet(pkt, sizeof(*pkt_data));
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (ret < 0)
166 return ret;
167 51 memcpy(pkt->data, pkt_data, sizeof(*pkt_data));
168
169 51 pkt->pts = pkt->dts = pts;
170 51 pkt->duration = 1;
171
172 51 return 0;
173 }
174
175 4 static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc, AVPacket **pkt)
176 {
177 4 int ret = 0;
178 AVStream *s;
179
180 4 ret = avformat_alloc_output_context2(oc, NULL, "fifo", "-");
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret) {
182 fprintf(stderr, "Failed to create format context: %s\n",
183 av_err2str(ret));
184 return EXIT_FAILURE;
185 }
186
187 4 s = avformat_new_stream(*oc, NULL);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!s) {
189 fprintf(stderr, "Failed to create stream: %s\n",
190 av_err2str(ret));
191 return AVERROR(ENOMEM);
192 }
193
194 4 *pkt = av_packet_alloc();
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!*pkt)
196 return AVERROR(ENOMEM);
197
198 4 return 0;
199 }
200
201 3 static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts,
202 AVPacket *pkt, const FailingMuxerPacketData *pkt_data)
203 {
204 3 int ret = 0, i;
205
206 3 ret = avformat_write_header(oc, opts);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret) {
208 fprintf(stderr, "Unexpected write_header failure: %s\n",
209 av_err2str(ret));
210 goto fail;
211 }
212
213
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 3 times.
48 for (i = 0; i < 15; i++ ) {
214 45 ret = prepare_packet(pkt, pkt_data, i);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (ret < 0) {
216 fprintf(stderr, "Failed to prepare test packet: %s\n",
217 av_err2str(ret));
218 goto write_trailer_and_fail;
219 }
220 45 ret = av_write_frame(oc, pkt);
221 45 av_packet_unref(pkt);
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (ret < 0) {
223 fprintf(stderr, "Unexpected write_frame error: %s\n",
224 av_err2str(ret));
225 goto write_trailer_and_fail;
226 }
227 }
228
229 3 ret = av_write_frame(oc, NULL);
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
231 fprintf(stderr, "Unexpected write_frame error during flushing: %s\n",
232 av_err2str(ret));
233 goto write_trailer_and_fail;
234 }
235
236 3 ret = av_write_trailer(oc);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
238 fprintf(stderr, "Unexpected write_trailer error during flushing: %s\n",
239 av_err2str(ret));
240 goto fail;
241 }
242
243 3 return ret;
244 write_trailer_and_fail:
245 av_write_trailer(oc);
246 fail:
247 return ret;
248 }
249
250 1 static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
251 AVPacket *pkt, const FailingMuxerPacketData *data)
252 {
253 1 int ret = 0, i;
254 int64_t write_pkt_start, write_pkt_end, duration;
255
256 1 ret = avformat_write_header(oc, opts);
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret) {
258 fprintf(stderr, "Unexpected write_header failure: %s\n",
259 av_err2str(ret));
260 return ret;
261 }
262
263 1 write_pkt_start = av_gettime_relative();
264
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 for (i = 0; i < 6; i++ ) {
265 6 ret = prepare_packet(pkt, data, i);
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
267 fprintf(stderr, "Failed to prepare test packet: %s\n",
268 av_err2str(ret));
269 goto fail;
270 }
271 6 ret = av_write_frame(oc, pkt);
272 6 av_packet_unref(pkt);
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
274 break;
275 }
276 }
277
278 1 write_pkt_end = av_gettime_relative();
279 1 duration = write_pkt_end - write_pkt_start;
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (duration > (SLEEPTIME_50_MS*6)/2) {
281 fprintf(stderr, "Writing packets to fifo muxer took too much time while testing"
282 "buffer overflow with drop_pkts_on_overflow was on.\n");
283 ret = AVERROR_BUG;
284 goto fail;
285 }
286
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret) {
288 fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
289 goto fail;
290 }
291
292 1 ret = av_write_trailer(oc);
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
294 fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
295
296 1 return ret;
297 fail:
298 av_write_trailer(oc);
299 return ret;
300 }
301
302 typedef struct TestCase {
303 int (*test_func)(AVFormatContext *, AVDictionary **,
304 AVPacket *, const FailingMuxerPacketData *pkt_data);
305 const char *test_name;
306 const char *options;
307
308 uint8_t print_summary_on_deinit;
309 int write_header_ret;
310 int write_trailer_ret;
311
312 FailingMuxerPacketData pkt_data;
313 } TestCase;
314
315
316 #define BUFFER_SIZE 64
317
318 4 static int run_test(const TestCase *test)
319 {
320 4 AVDictionary *opts = NULL;
321 4 AVFormatContext *oc = NULL;
322 4 AVPacket *pkt = NULL;
323 char buffer[BUFFER_SIZE];
324 int ret, ret1;
325
326 4 ret = initialize_fifo_tst_muxer_chain(&oc, &pkt);
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0) {
328 fprintf(stderr, "Muxer initialization failed: %s\n", av_err2str(ret));
329 goto end;
330 }
331
332
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (test->options) {
333 3 ret = av_dict_parse_string(&opts, test->options, "=", ":", 0);
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
335 fprintf(stderr, "Failed to parse options: %s\n", av_err2str(ret));
336 goto end;
337 }
338 }
339
340 4 snprintf(buffer, BUFFER_SIZE,
341 "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
342 4 (int)test->print_summary_on_deinit, test->write_header_ret,
343 4 test->write_trailer_ret);
344 4 ret = av_dict_set(&opts, "format_opts", buffer, 0);
345 4 ret1 = av_dict_set(&opts, "fifo_format", "fifo_test", 0);
346
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) {
347 fprintf(stderr, "Failed to set options for test muxer: %s\n",
348 av_err2str(ret));
349 goto end;
350 }
351
352 4 ret = test->test_func(oc, &opts, pkt, &test->pkt_data);
353
354 4 end:
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 printf("%s: %s\n", test->test_name, ret < 0 ? "fail" : "ok");
356 4 avformat_free_context(oc);
357 4 av_packet_free(&pkt);
358 4 av_dict_free(&opts);
359 4 return ret;
360 }
361
362
363 const TestCase tests[] = {
364 /* Simple test in packet-non-dropping mode, we expect to get on the output
365 * exactly what was on input */
366 {fifo_basic_test, "nonfail test", NULL,1, 0, 0, {0, 0, 0}},
367
368 /* Each write_packet will fail 3 times before operation is successful. If recovery
369 * Since recovery is on, fifo muxer should not return any errors. */
370 {fifo_basic_test, "recovery test", "attempt_recovery=1:recovery_wait_time=0",
371 0, 0, 0, {AVERROR(ETIMEDOUT), 3, 0}},
372
373 /* By setting low queue_size and sending packets with longer processing time,
374 * this test will cause queue to overflow, since drop_pkts_on_overflow is off
375 * by default, all packets should be processed and fifo should block on full
376 * queue. */
377 {fifo_basic_test, "overflow without packet dropping","queue_size=3",
378 1, 0, 0, {0, 0, SLEEPTIME_10_MS}},
379
380 /* The test as the upper one, except that drop_on_overflow is turned on. In this case
381 * fifo should not block when the queue is full and slow down producer, so the test
382 * measures time producer spends on write_packet calls which should be significantly
383 * less than number_of_pkts * 50 MS.
384 */
385 {fifo_overflow_drop_test, "overflow with packet dropping", "queue_size=3:drop_pkts_on_overflow=1",
386 0, 0, 0, {0, 0, SLEEPTIME_50_MS}},
387
388 {NULL}
389 };
390
391 1 int main(int argc, char *argv[])
392 {
393 1 int i, ret, ret_all = 0;
394
395
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (i = 0; tests[i].test_func; i++) {
396 4 ret = run_test(&tests[i]);
397
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)
398 ret_all = ret;
399 }
400
401 1 return ret;
402 }
403