FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/fifo_test.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 36 37 97.3%
Functions: 4 4 100.0%
Branches: 17 18 94.4%

Line Branch Exec Source
1 /*
2 * FIFO test 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
24 #include "libavutil/opt.h"
25 #include "libavutil/time.h"
26
27 #include "avformat.h"
28 #include "url.h"
29
30 /* Implementation of mock muxer to simulate real muxer failures */
31
32 #define MAX_TST_PACKETS 128
33 #define SLEEPTIME_50_MS 50000
34 #define SLEEPTIME_10_MS 10000
35
36 /* Implementation of mock muxer to simulate real muxer failures */
37
38 /* This is structure of data sent in packets to
39 * failing muxer */
40 typedef struct FailingMuxerPacketData {
41 int ret; /* return value of write_packet call*/
42 int recover_after; /* set ret to zero after this number of recovery attempts */
43 unsigned sleep_time; /* sleep for this long in write_packet to simulate long I/O operation */
44 } FailingMuxerPacketData;
45
46
47 typedef struct FailingMuxerContext {
48 AVClass *class;
49 int write_header_ret;
50 int write_trailer_ret;
51 /* If non-zero, summary of processed packets will be printed in deinit */
52 int print_deinit_summary;
53
54 int flush_count;
55 int pts_written[MAX_TST_PACKETS];
56 int pts_written_nr;
57 } FailingMuxerContext;
58
59 49 static int failing_write_header(AVFormatContext *avf)
60 {
61 49 FailingMuxerContext *ctx = avf->priv_data;
62 49 return ctx->write_header_ret;
63 }
64
65 93 static int failing_write_packet(AVFormatContext *avf, AVPacket *pkt)
66 {
67 93 FailingMuxerContext *ctx = avf->priv_data;
68 93 int ret = 0;
69
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 90 times.
93 if (!pkt) {
70 3 ctx->flush_count++;
71 } else {
72 90 FailingMuxerPacketData *data = (FailingMuxerPacketData*) pkt->data;
73
74
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 45 times.
90 if (!data->recover_after) {
75 45 data->ret = 0;
76 } else {
77 45 data->recover_after--;
78 }
79
80 90 ret = data->ret;
81
82
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 75 times.
90 if (data->sleep_time) {
83 15 int64_t slept = 0;
84
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 while (slept < data->sleep_time) {
85
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (ff_check_interrupt(&avf->interrupt_callback))
86 return AVERROR_EXIT;
87 15 av_usleep(SLEEPTIME_10_MS);
88 15 slept += SLEEPTIME_10_MS;
89 }
90 }
91
92
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 45 times.
90 if (!ret) {
93 45 ctx->pts_written[ctx->pts_written_nr++] = pkt->pts;
94 45 av_packet_unref(pkt);
95 }
96 }
97 93 return ret;
98 }
99
100 49 static int failing_write_trailer(AVFormatContext *avf)
101 {
102 49 FailingMuxerContext *ctx = avf->priv_data;
103 49 return ctx->write_trailer_ret;
104 }
105
106 49 static void failing_deinit(AVFormatContext *avf)
107 {
108 int i;
109 49 FailingMuxerContext *ctx = avf->priv_data;
110
111
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 2 times.
49 if (!ctx->print_deinit_summary)
112 47 return;
113
114 2 printf("flush count: %d\n", ctx->flush_count);
115 2 printf("pts seen nr: %d\n", ctx->pts_written_nr);
116 2 printf("pts seen: ");
117
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 2 times.
32 for (i = 0; i < ctx->pts_written_nr; ++i ) {
118
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 2 times.
30 printf(i ? ",%d" : "%d", ctx->pts_written[i]);
119 }
120 2 printf("\n");
121 }
122 #define OFFSET(x) offsetof(FailingMuxerContext, x)
123 static const AVOption options[] = {
124 {"write_header_ret", "write_header() return value", OFFSET(write_header_ret),
125 AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
126 {"write_trailer_ret", "write_trailer() return value", OFFSET(write_trailer_ret),
127 AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
128 {"print_deinit_summary", "print summary when deinitializing muxer", OFFSET(print_deinit_summary),
129 AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
130 {NULL}
131 };
132
133 static const AVClass failing_muxer_class = {
134 .class_name = "Fifo test muxer",
135 .item_name = av_default_item_name,
136 .option = options,
137 .version = LIBAVUTIL_VERSION_INT,
138 };
139
140 const AVOutputFormat ff_fifo_test_muxer = {
141 .name = "fifo_test",
142 .long_name = NULL_IF_CONFIG_SMALL("Fifo test muxer"),
143 .priv_data_size = sizeof(FailingMuxerContext),
144 .write_header = failing_write_header,
145 .write_packet = failing_write_packet,
146 .write_trailer = failing_write_trailer,
147 .deinit = failing_deinit,
148 .priv_class = &failing_muxer_class,
149 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
150 };
151
152