2 * FIFO test pseudo-muxer
3 * Copyright (c) 2016 Jan Sebechlebsky
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/opt.h"
25 #include "libavutil/time.h"
26 #include "libavutil/avassert.h"
31 /* Implementation of mock muxer to simulate real muxer failures */
33 #define MAX_TST_PACKETS 128
34 #define SLEEPTIME_50_MS 50000
35 #define SLEEPTIME_10_MS 10000
37 /* Implementation of mock muxer to simulate real muxer failures */
39 /* This is structure of data sent in packets to
41 typedef struct FailingMuxerPacketData {
42 int ret; /* return value of write_packet call*/
43 int recover_after; /* set ret to zero after this number of recovery attempts */
44 unsigned sleep_time; /* sleep for this long in write_packet to simulate long I/O operation */
45 } FailingMuxerPacketData;
48 typedef struct FailingMuxerContext {
51 int write_trailer_ret;
52 /* If non-zero, summary of processed packets will be printed in deinit */
53 int print_deinit_summary;
56 int pts_written[MAX_TST_PACKETS];
58 } FailingMuxerContext;
60 static int failing_write_header(AVFormatContext *avf)
62 FailingMuxerContext *ctx = avf->priv_data;
63 return ctx->write_header_ret;
66 static int failing_write_packet(AVFormatContext *avf, AVPacket *pkt)
68 FailingMuxerContext *ctx = avf->priv_data;
73 FailingMuxerPacketData *data = (FailingMuxerPacketData*) pkt->data;
75 if (!data->recover_after) {
78 data->recover_after--;
83 if (data->sleep_time) {
85 while (slept < data->sleep_time) {
86 if (ff_check_interrupt(&avf->interrupt_callback))
88 av_usleep(SLEEPTIME_10_MS);
89 slept += SLEEPTIME_10_MS;
94 ctx->pts_written[ctx->pts_written_nr++] = pkt->pts;
101 static int failing_write_trailer(AVFormatContext *avf)
103 FailingMuxerContext *ctx = avf->priv_data;
104 return ctx->write_trailer_ret;
107 static void failing_deinit(AVFormatContext *avf)
110 FailingMuxerContext *ctx = avf->priv_data;
112 if (!ctx->print_deinit_summary)
115 printf("flush count: %d\n", ctx->flush_count);
116 printf("pts seen nr: %d\n", ctx->pts_written_nr);
117 printf("pts seen: ");
118 for (i = 0; i < ctx->pts_written_nr; ++i ) {
119 printf(i ? ",%d" : "%d", ctx->pts_written[i]);
123 #define OFFSET(x) offsetof(FailingMuxerContext, x)
124 static const AVOption options[] = {
125 {"write_header_ret", "write_header() return value", OFFSET(write_header_ret),
126 AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
127 {"write_trailer_ret", "write_trailer() return value", OFFSET(write_trailer_ret),
128 AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
129 {"print_deinit_summary", "print summary when deinitializing muxer", OFFSET(print_deinit_summary),
130 AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
134 static const AVClass failing_muxer_class = {
135 .class_name = "Fifo test muxer",
136 .item_name = av_default_item_name,
138 .version = LIBAVUTIL_VERSION_INT,
141 AVOutputFormat ff_fifo_test_muxer = {
143 .long_name = NULL_IF_CONFIG_SMALL("Fifo test muxer"),
144 .priv_data_size = sizeof(FailingMuxerContext),
145 .write_header = failing_write_header,
146 .write_packet = failing_write_packet,
147 .write_trailer = failing_write_trailer,
148 .deinit = failing_deinit,
149 .priv_class = &failing_muxer_class,
150 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,