]> git.sesse.net Git - ffmpeg/blob - libavformat/tests/fifo_muxer.c
Merge commit 'f65285aba0df7d46298abe0c945dfee05cbc6028'
[ffmpeg] / libavformat / tests / fifo_muxer.c
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 "libavutil/avassert.h"
26 #include "libavformat/avformat.h"
27 #include "libavformat/url.h"
28 #include "libavformat/network.h"
29
30 #define MAX_TST_PACKETS 128
31 #define SLEEPTIME_50_MS 50000
32 #define SLEEPTIME_10_MS 10000
33
34 /* Implementation of mock muxer to simulate real muxer failures */
35
36 /* This is structure of data sent in packets to
37  * failing muxer */
38 typedef struct FailingMuxerPacketData {
39     int ret;             /* return value of write_packet call*/
40     int recover_after;   /* set ret to zero after this number of recovery attempts */
41     unsigned sleep_time; /* sleep for this long in write_packet to simulate long I/O operation */
42 } FailingMuxerPacketData;
43
44
45 typedef struct FailingMuxerContext {
46     AVClass *class;
47     int write_header_ret;
48     int write_trailer_ret;
49     /* If non-zero, summary of processed packets will be printed in deinit */
50     int print_deinit_summary;
51
52     int flush_count;
53     int pts_written[MAX_TST_PACKETS];
54     int pts_written_nr;
55 } FailingMuxerContext;
56
57 static int failing_write_header(AVFormatContext *avf)
58 {
59     FailingMuxerContext *ctx = avf->priv_data;
60     return ctx->write_header_ret;
61 }
62
63 static int failing_write_packet(AVFormatContext *avf, AVPacket *pkt)
64 {
65     FailingMuxerContext *ctx = avf->priv_data;
66     int ret = 0;
67     if (!pkt) {
68         ctx->flush_count++;
69     } else {
70         FailingMuxerPacketData *data = (FailingMuxerPacketData*) pkt->data;
71
72         if (!data->recover_after) {
73             data->ret = 0;
74         } else {
75             data->recover_after--;
76         }
77
78         ret = data->ret;
79
80         if (data->sleep_time) {
81             int64_t slept = 0;
82             while (slept < data->sleep_time) {
83                 if (ff_check_interrupt(&avf->interrupt_callback))
84                     return AVERROR_EXIT;
85                 av_usleep(SLEEPTIME_10_MS);
86                 slept += SLEEPTIME_10_MS;
87             }
88         }
89
90         if (!ret) {
91             ctx->pts_written[ctx->pts_written_nr++] = pkt->pts;
92             av_packet_unref(pkt);
93         }
94     }
95     return ret;
96 }
97
98 static int failing_write_trailer(AVFormatContext *avf)
99 {
100     FailingMuxerContext *ctx = avf->priv_data;
101     return ctx->write_trailer_ret;
102 }
103
104 static void failing_deinit(AVFormatContext *avf)
105 {
106     int i;
107     FailingMuxerContext *ctx = avf->priv_data;
108
109     if (!ctx->print_deinit_summary)
110         return;
111
112     printf("flush count: %d\n", ctx->flush_count);
113     printf("pts seen nr: %d\n", ctx->pts_written_nr);
114     printf("pts seen: ");
115     for (i = 0; i < ctx->pts_written_nr; ++i ) {
116         printf(i ? ",%d" : "%d", ctx->pts_written[i]);
117     }
118     printf("\n");
119 }
120 #define OFFSET(x) offsetof(FailingMuxerContext, x)
121 static const AVOption options[] = {
122         {"write_header_ret", "write_header() return value", OFFSET(write_header_ret),
123          AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
124         {"write_trailer_ret", "write_trailer() return value", OFFSET(write_trailer_ret),
125          AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
126         {"print_deinit_summary", "print summary when deinitializing muxer", OFFSET(print_deinit_summary),
127          AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
128         {NULL}
129     };
130
131 static const AVClass failing_muxer_class = {
132     .class_name = "Failing test muxer",
133     .item_name  = av_default_item_name,
134     .option     = options,
135     .version    = LIBAVUTIL_VERSION_INT,
136 };
137
138 AVOutputFormat tst_failing_muxer = {
139     .name           = "fail",
140     .long_name      = NULL_IF_CONFIG_SMALL("Failing test muxer"),
141     .priv_data_size = sizeof(FailingMuxerContext),
142     .write_header   = failing_write_header,
143     .write_packet   = failing_write_packet,
144     .write_trailer  = failing_write_trailer,
145     .deinit         = failing_deinit,
146     .priv_class     = &failing_muxer_class,
147     .flags          = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
148 };
149
150 static int prepare_packet(AVPacket *pkt,const FailingMuxerPacketData *pkt_data, int64_t pts)
151 {
152     int ret;
153     FailingMuxerPacketData *data = av_malloc(sizeof(*data));
154     if (!data) {
155         return AVERROR(ENOMEM);
156     }
157     memcpy(data, pkt_data, sizeof(FailingMuxerPacketData));
158     ret = av_packet_from_data(pkt, (uint8_t*) data, sizeof(*data));
159
160     pkt->pts = pkt->dts = pts;
161     pkt->duration = 1;
162
163     return ret;
164 }
165
166 static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc)
167 {
168     int ret = 0;
169     AVStream *s;
170
171     ret = avformat_alloc_output_context2(oc, NULL, "fifo", "-");
172     if (ret) {
173         fprintf(stderr, "Failed to create format context: %s\n",
174                 av_err2str(ret));
175         return EXIT_FAILURE;
176     }
177
178     s = avformat_new_stream(*oc, NULL);
179     if (!s) {
180         fprintf(stderr, "Failed to create stream: %s\n",
181                 av_err2str(ret));
182         ret = AVERROR(ENOMEM);
183     }
184
185     return ret;
186 }
187
188 static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts,
189                              const FailingMuxerPacketData *pkt_data)
190 {
191     int ret = 0, i;
192     AVPacket pkt;
193
194     av_init_packet(&pkt);
195
196
197     ret = avformat_write_header(oc, opts);
198     if (ret) {
199         fprintf(stderr, "Unexpected write_header failure: %s\n",
200                 av_err2str(ret));
201         goto fail;
202     }
203
204     for (i = 0; i < 15; i++ ) {
205         ret = prepare_packet(&pkt, pkt_data, i);
206         if (ret < 0) {
207             fprintf(stderr, "Failed to prepare test packet: %s\n",
208                     av_err2str(ret));
209             goto write_trailer_and_fail;
210         }
211         ret = av_write_frame(oc, &pkt);
212         av_packet_unref(&pkt);
213         if (ret < 0) {
214             fprintf(stderr, "Unexpected write_frame error: %s\n",
215                     av_err2str(ret));
216             goto write_trailer_and_fail;
217         }
218     }
219
220     ret = av_write_frame(oc, NULL);
221     if (ret < 0) {
222         fprintf(stderr, "Unexpected write_frame error during flushing: %s\n",
223                 av_err2str(ret));
224         goto write_trailer_and_fail;
225     }
226
227     ret = av_write_trailer(oc);
228     if (ret < 0) {
229         fprintf(stderr, "Unexpected write_trailer error during flushing: %s\n",
230                 av_err2str(ret));
231         goto fail;
232     }
233
234     return ret;
235 write_trailer_and_fail:
236     av_write_trailer(oc);
237 fail:
238     return ret;
239 }
240
241 static int fifo_write_header_err_tst(AVFormatContext *oc, AVDictionary **opts,
242                                      const FailingMuxerPacketData *pkt_data)
243 {
244     int ret = 0, i;
245     AVPacket pkt;
246
247     av_init_packet(&pkt);
248
249     ret = avformat_write_header(oc, opts);
250     if (ret) {
251         fprintf(stderr, "Unexpected write_header failure: %s\n",
252                 av_err2str(ret));
253         goto fail;
254     }
255
256     for (i = 0; i < MAX_TST_PACKETS; i++ ) {
257         ret = prepare_packet(&pkt, pkt_data, i);
258         if (ret < 0) {
259             fprintf(stderr, "Failed to prepare test packet: %s\n",
260                     av_err2str(ret));
261             goto write_trailer_and_fail;
262         }
263         ret = av_write_frame(oc, &pkt);
264         av_packet_unref(&pkt);
265         if (ret < 0) {
266             break;
267         }
268     }
269
270     if (!ret) {
271         fprintf(stderr, "write_packet not failed when supposed to.\n");
272         goto fail;
273     } else if (ret != -1) {
274         fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
275         goto fail;
276     }
277
278     ret = av_write_trailer(oc);
279     if (ret < 0)
280         fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
281
282     return ret;
283 write_trailer_and_fail:
284     av_write_trailer(oc);
285 fail:
286     return ret;
287 }
288
289 static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
290                                    const FailingMuxerPacketData *data)
291 {
292     int ret = 0, i;
293     int64_t write_pkt_start, write_pkt_end, duration;
294     AVPacket pkt;
295
296     av_init_packet(&pkt);
297
298     ret = avformat_write_header(oc, opts);
299     if (ret) {
300         fprintf(stderr, "Unexpected write_header failure: %s\n",
301                 av_err2str(ret));
302         return ret;
303     }
304
305     write_pkt_start = av_gettime_relative();
306     for (i = 0; i < 6; i++ ) {
307         ret = prepare_packet(&pkt, data, i);
308         if (ret < 0) {
309             fprintf(stderr, "Failed to prepare test packet: %s\n",
310                     av_err2str(ret));
311             goto fail;
312         }
313         ret = av_write_frame(oc, &pkt);
314         av_packet_unref(&pkt);
315         if (ret < 0) {
316             break;
317         }
318     }
319     write_pkt_end = av_gettime_relative();
320     duration = write_pkt_end - write_pkt_start;
321     if (duration > (SLEEPTIME_50_MS*6)/2) {
322         fprintf(stderr, "Writing packets to fifo muxer took too much time while testing"
323                         "buffer overflow with drop_pkts_on_overflow was on.\n");
324         ret = AVERROR_BUG;
325         goto fail;
326     }
327
328     if (ret) {
329         fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
330         goto fail;
331     }
332
333     ret = av_write_trailer(oc);
334     if (ret < 0)
335         fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
336
337     return ret;
338 fail:
339     av_write_trailer(oc);
340     return ret;
341 }
342
343 typedef struct TestCase {
344     int (*test_func)(AVFormatContext *, AVDictionary **,const FailingMuxerPacketData *pkt_data);
345     const char *test_name;
346     const char *options;
347
348     uint8_t print_summary_on_deinit;
349     int write_header_ret;
350     int write_trailer_ret;
351
352     FailingMuxerPacketData pkt_data;
353 } TestCase;
354
355
356 #define BUFFER_SIZE 64
357
358 static int run_test(const TestCase *test)
359 {
360     AVDictionary *opts = NULL;
361     AVFormatContext *oc = NULL;
362     char buffer[BUFFER_SIZE];
363     int ret, ret1;
364
365     ret = initialize_fifo_tst_muxer_chain(&oc);
366     if (ret < 0) {
367         fprintf(stderr, "Muxer initialization failed: %s\n", av_err2str(ret));
368         goto end;
369     }
370
371     if (test->options) {
372         ret = av_dict_parse_string(&opts, test->options, "=", ":", 0);
373         if (ret < 0) {
374             fprintf(stderr, "Failed to parse options: %s\n", av_err2str(ret));
375             goto end;
376         }
377     }
378
379     snprintf(buffer, BUFFER_SIZE,
380              "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
381              (int)test->print_summary_on_deinit, test->write_header_ret,
382              test->write_trailer_ret);
383     ret = av_dict_set(&opts, "format_opts", buffer, 0);
384     ret1 = av_dict_set(&opts, "fifo_format", "fail", 0);
385     if (ret < 0 || ret1 < 0) {
386         fprintf(stderr, "Failed to set options for test muxer: %s\n",
387                 av_err2str(ret));
388         goto end;
389     }
390
391     ret = test->test_func(oc, &opts, &test->pkt_data);
392
393 end:
394     printf("%s: %s\n", test->test_name, ret < 0 ? "fail" : "ok");
395     avformat_free_context(oc);
396     av_dict_free(&opts);
397     return ret;
398 }
399
400
401 const TestCase tests[] = {
402         /* Simple test in packet-non-dropping mode, we expect to get on the output
403          * exactly what was on input */
404         {fifo_basic_test, "nonfail test", NULL,1, 0, 0, {0, 0, 0}},
405
406         /* Test that we receive delayed write_header error from one of the write_packet
407          * calls. */
408         {fifo_write_header_err_tst, "write header error test", NULL, 0, -1, 0, {0, 0, 0}},
409
410         /* Each write_packet will fail 3 times before operation is successful. If recovery
411          * Since recovery is on, fifo muxer should not return any errors. */
412         {fifo_basic_test, "recovery test", "attempt_recovery=1:recovery_wait_time=0",
413          0, 0, 0, {AVERROR(ETIMEDOUT), 3, 0}},
414
415         /* By setting low queue_size and sending packets with longer processing time,
416          * this test will cause queue to overflow, since drop_pkts_on_overflow is off
417          * by default, all packets should be processed and fifo should block on full
418          * queue. */
419         {fifo_basic_test, "overflow without packet dropping","queue_size=3",
420          1, 0, 0, {0, 0, SLEEPTIME_10_MS}},
421
422         /* The test as the upper one, except that drop_on_overflow is turned on. In this case
423          * fifo should not block when the queue is full and slow down producer, so the test
424          * measures time producer spends on write_packet calls which should be significantly
425          * less than number_of_pkts * 50 MS.
426          */
427         {fifo_overflow_drop_test, "overflow with packet dropping", "queue_size=3:drop_pkts_on_overflow=1",
428          0, 0, 0, {0, 0, SLEEPTIME_50_MS}},
429
430         {NULL}
431 };
432
433 int main(int argc, char *argv[])
434 {
435     int i, ret, ret_all = 0;
436
437     av_register_all();
438     av_register_output_format(&tst_failing_muxer);
439
440     for (i = 0; tests[i].test_func; i++) {
441         ret = run_test(&tests[i]);
442         if (!ret_all && ret < 0)
443             ret_all = ret;
444     }
445
446     return ret;
447 }