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 #define MAX_TST_PACKETS 128
31 #define SLEEPTIME_50_MS 50000
32 #define SLEEPTIME_10_MS 10000
33
34 /* This is structure of data sent in packets to
35 * failing muxer */
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 */
41
43 {
44 int ret;
46 if (!data) {
48 }
51
54
55 return ret;
56 }
57
59 {
60 int ret = 0;
62
64 if (ret) {
65 fprintf(stderr, "Failed to create format context: %s\n",
67 return EXIT_FAILURE;
68 }
69
71 if (!s) {
72 fprintf(stderr, "Failed to create stream: %s\n",
75 }
76
77 return ret;
78 }
79
82 {
83 int ret = 0, i;
85
87
88
90 if (ret) {
91 fprintf(stderr, "Unexpected write_header failure: %s\n",
94 }
95
96 for (i = 0; i < 15; i++ ) {
98 if (ret < 0) {
99 fprintf(stderr, "Failed to prepare test packet: %s\n",
101 goto write_trailer_and_fail;
102 }
105 if (ret < 0) {
106 fprintf(stderr, "Unexpected write_frame error: %s\n",
108 goto write_trailer_and_fail;
109 }
110 }
111
113 if (ret < 0) {
114 fprintf(stderr, "Unexpected write_frame error during flushing: %s\n",
116 goto write_trailer_and_fail;
117 }
118
120 if (ret < 0) {
121 fprintf(stderr, "Unexpected write_trailer error during flushing: %s\n",
124 }
125
126 return ret;
127 write_trailer_and_fail:
130 return ret;
131 }
132
135 {
136 int ret = 0, i;
137 int64_t write_pkt_start, write_pkt_end,
duration;
139
141
143 if (ret) {
144 fprintf(stderr, "Unexpected write_header failure: %s\n",
146 return ret;
147 }
148
150 for (i = 0; i < 6; i++ ) {
152 if (ret < 0) {
153 fprintf(stderr, "Failed to prepare test packet: %s\n",
156 }
159 if (ret < 0) {
160 break;
161 }
162 }
164 duration = write_pkt_end - write_pkt_start;
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");
170 }
171
172 if (ret) {
173 fprintf(stderr,
"Unexpected write_packet error: %s\n",
av_err2str(ret));
175 }
176
178 if (ret < 0)
179 fprintf(stderr,
"Unexpected write_trailer error: %s\n",
av_err2str(ret));
180
181 return ret;
184 return ret;
185 }
186
191
195
198
199
200 #define BUFFER_SIZE 64
201
203 {
207 int ret, ret1;
208
210 if (ret < 0) {
211 fprintf(stderr,
"Muxer initialization failed: %s\n",
av_err2str(ret));
213 }
214
217 if (ret < 0) {
218 fprintf(stderr,
"Failed to parse options: %s\n",
av_err2str(ret));
220 }
221 }
222
224 "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
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",
233 }
234
236
238 printf(
"%s: %s\n", test->
test_name, ret < 0 ?
"fail" :
"ok");
241 return ret;
242 }
243
244
246 /* Simple test in packet-non-dropping mode, we expect to get on the output
247 * exactly what was on input */
249
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}},
254
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
258 * queue. */
261
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.
266 */
269
271 };
272
273 int main(
int argc,
char *argv[])
274 {
275 int i, ret, ret_all = 0;
276
279 if (!ret_all && ret < 0)
280 ret_all = ret;
281 }
282
283 return ret;
284 }
static int run_test(const TestCase *test)
static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc)
ptrdiff_t const GLvoid * data
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
int main(int argc, char *argv[])
FailingMuxerPacketData pkt_data
static av_cold int end(AVCodecContext *avctx)
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
Initialize a reference-counted packet from av_malloc()ed data.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
simple assert() macros that are a bit more flexible than ISO C assert().
static int prepare_packet(AVPacket *pkt, const FailingMuxerPacketData *pkt_data, int64_t pts)
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts, const FailingMuxerPacketData *pkt_data)
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
static void test(const char *pattern, const char *host)
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
uint8_t print_summary_on_deinit
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts, const FailingMuxerPacketData *data)
unbuffered private I/O API
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int(* test_func)(AVFormatContext *, AVDictionary **, const FailingMuxerPacketData *pkt_data)