]> git.sesse.net Git - ffmpeg/blob - libavformat/tests/fifo_muxer.c
Merge commit '7993ec19af394fdc58ec64165bc0b12619543a5d'
[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_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
242                                    const FailingMuxerPacketData *data)
243 {
244     int ret = 0, i;
245     int64_t write_pkt_start, write_pkt_end, duration;
246     AVPacket pkt;
247
248     av_init_packet(&pkt);
249
250     ret = avformat_write_header(oc, opts);
251     if (ret) {
252         fprintf(stderr, "Unexpected write_header failure: %s\n",
253                 av_err2str(ret));
254         return ret;
255     }
256
257     write_pkt_start = av_gettime_relative();
258     for (i = 0; i < 6; i++ ) {
259         ret = prepare_packet(&pkt, data, i);
260         if (ret < 0) {
261             fprintf(stderr, "Failed to prepare test packet: %s\n",
262                     av_err2str(ret));
263             goto fail;
264         }
265         ret = av_write_frame(oc, &pkt);
266         av_packet_unref(&pkt);
267         if (ret < 0) {
268             break;
269         }
270     }
271     write_pkt_end = av_gettime_relative();
272     duration = write_pkt_end - write_pkt_start;
273     if (duration > (SLEEPTIME_50_MS*6)/2) {
274         fprintf(stderr, "Writing packets to fifo muxer took too much time while testing"
275                         "buffer overflow with drop_pkts_on_overflow was on.\n");
276         ret = AVERROR_BUG;
277         goto fail;
278     }
279
280     if (ret) {
281         fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
282         goto fail;
283     }
284
285     ret = av_write_trailer(oc);
286     if (ret < 0)
287         fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
288
289     return ret;
290 fail:
291     av_write_trailer(oc);
292     return ret;
293 }
294
295 typedef struct TestCase {
296     int (*test_func)(AVFormatContext *, AVDictionary **,const FailingMuxerPacketData *pkt_data);
297     const char *test_name;
298     const char *options;
299
300     uint8_t print_summary_on_deinit;
301     int write_header_ret;
302     int write_trailer_ret;
303
304     FailingMuxerPacketData pkt_data;
305 } TestCase;
306
307
308 #define BUFFER_SIZE 64
309
310 static int run_test(const TestCase *test)
311 {
312     AVDictionary *opts = NULL;
313     AVFormatContext *oc = NULL;
314     char buffer[BUFFER_SIZE];
315     int ret, ret1;
316
317     ret = initialize_fifo_tst_muxer_chain(&oc);
318     if (ret < 0) {
319         fprintf(stderr, "Muxer initialization failed: %s\n", av_err2str(ret));
320         goto end;
321     }
322
323     if (test->options) {
324         ret = av_dict_parse_string(&opts, test->options, "=", ":", 0);
325         if (ret < 0) {
326             fprintf(stderr, "Failed to parse options: %s\n", av_err2str(ret));
327             goto end;
328         }
329     }
330
331     snprintf(buffer, BUFFER_SIZE,
332              "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
333              (int)test->print_summary_on_deinit, test->write_header_ret,
334              test->write_trailer_ret);
335     ret = av_dict_set(&opts, "format_opts", buffer, 0);
336     ret1 = av_dict_set(&opts, "fifo_format", "fail", 0);
337     if (ret < 0 || ret1 < 0) {
338         fprintf(stderr, "Failed to set options for test muxer: %s\n",
339                 av_err2str(ret));
340         goto end;
341     }
342
343     ret = test->test_func(oc, &opts, &test->pkt_data);
344
345 end:
346     printf("%s: %s\n", test->test_name, ret < 0 ? "fail" : "ok");
347     avformat_free_context(oc);
348     av_dict_free(&opts);
349     return ret;
350 }
351
352
353 const TestCase tests[] = {
354         /* Simple test in packet-non-dropping mode, we expect to get on the output
355          * exactly what was on input */
356         {fifo_basic_test, "nonfail test", NULL,1, 0, 0, {0, 0, 0}},
357
358         /* Each write_packet will fail 3 times before operation is successful. If recovery
359          * Since recovery is on, fifo muxer should not return any errors. */
360         {fifo_basic_test, "recovery test", "attempt_recovery=1:recovery_wait_time=0",
361          0, 0, 0, {AVERROR(ETIMEDOUT), 3, 0}},
362
363         /* By setting low queue_size and sending packets with longer processing time,
364          * this test will cause queue to overflow, since drop_pkts_on_overflow is off
365          * by default, all packets should be processed and fifo should block on full
366          * queue. */
367         {fifo_basic_test, "overflow without packet dropping","queue_size=3",
368          1, 0, 0, {0, 0, SLEEPTIME_10_MS}},
369
370         /* The test as the upper one, except that drop_on_overflow is turned on. In this case
371          * fifo should not block when the queue is full and slow down producer, so the test
372          * measures time producer spends on write_packet calls which should be significantly
373          * less than number_of_pkts * 50 MS.
374          */
375         {fifo_overflow_drop_test, "overflow with packet dropping", "queue_size=3:drop_pkts_on_overflow=1",
376          0, 0, 0, {0, 0, SLEEPTIME_50_MS}},
377
378         {NULL}
379 };
380
381 int main(int argc, char *argv[])
382 {
383     int i, ret, ret_all = 0;
384
385     av_register_all();
386     av_register_output_format(&tst_failing_muxer);
387
388     for (i = 0; tests[i].test_func; i++) {
389         ret = run_test(&tests[i]);
390         if (!ret_all && ret < 0)
391             ret_all = ret;
392     }
393
394     return ret;
395 }