FFmpeg
fifo_test.c
Go to the documentation of this file.
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 "mux.h"
29 #include "url.h"
30 
31 /* Implementation of mock muxer to simulate real muxer failures */
32 
33 #define MAX_TST_PACKETS 128
34 #define SLEEPTIME_50_MS 50000
35 #define SLEEPTIME_10_MS 10000
36 
37 /* Implementation of mock muxer to simulate real muxer failures */
38 
39 /* This is structure of data sent in packets to
40  * failing muxer */
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 */
46 
47 
48 typedef struct FailingMuxerContext {
49  AVClass *class;
52  /* If non-zero, summary of processed packets will be printed in deinit */
54 
59 
61 {
63  return ctx->write_header_ret;
64 }
65 
67 {
69  int ret = 0;
70  if (!pkt) {
71  ctx->flush_count++;
72  } else {
74 
75  if (!data->recover_after) {
76  data->ret = 0;
77  } else {
78  data->recover_after--;
79  }
80 
81  ret = data->ret;
82 
83  if (data->sleep_time) {
84  int64_t slept = 0;
85  while (slept < data->sleep_time) {
87  return AVERROR_EXIT;
89  slept += SLEEPTIME_10_MS;
90  }
91  }
92 
93  if (!ret) {
94  ctx->pts_written[ctx->pts_written_nr++] = pkt->pts;
96  }
97  }
98  return ret;
99 }
100 
102 {
104  return ctx->write_trailer_ret;
105 }
106 
108 {
109  int i;
111 
112  if (!ctx->print_deinit_summary)
113  return;
114 
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]);
120  }
121  printf("\n");
122 }
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},
131  {NULL}
132  };
133 
134 static const AVClass failing_muxer_class = {
135  .class_name = "Fifo test muxer",
136  .item_name = av_default_item_name,
137  .option = options,
138  .version = LIBAVUTIL_VERSION_INT,
139 };
140 
142  .p.name = "fifo_test",
143  .p.long_name = NULL_IF_CONFIG_SMALL("Fifo test muxer"),
144  .priv_data_size = sizeof(FailingMuxerContext),
149  .p.priv_class = &failing_muxer_class,
151  .p.flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
152 #else
153  .p.flags = AVFMT_NOFILE,
154 #endif
155  .flags_internal = FF_FMT_ALLOW_FLUSH,
156 };
157 
FailingMuxerContext::print_deinit_summary
int print_deinit_summary
Definition: fifo_test.c:53
FailingMuxerContext::write_trailer_ret
int write_trailer_ret
Definition: fifo_test.c:51
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:427
AVOutputFormat::name
const char * name
Definition: avformat.h:511
opt.h
deinit
static void deinit(AVFormatContext *s)
Definition: chromaprint.c:50
FailingMuxerContext::pts_written
int pts_written[MAX_TST_PACKETS]
Definition: fifo_test.c:56
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVOption
AVOption.
Definition: opt.h:294
data
const char data[16]
Definition: mxf.c:148
FailingMuxerPacketData
Definition: fifo_test.c:41
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:36
options
static const AVOption options[]
Definition: fifo_test.c:124
AVFormatContext::interrupt_callback
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1629
FF_FMT_ALLOW_FLUSH
#define FF_FMT_ALLOW_FLUSH
Definition: mux.h:30
ff_check_interrupt
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition: avio.c:667
pkt
AVPacket * pkt
Definition: movenc.c:59
ff_fifo_test_muxer
const FFOutputFormat ff_fifo_test_muxer
Definition: fifo_test.c:141
FailingMuxerContext
Definition: fifo_test.c:48
failing_write_trailer
static int failing_write_trailer(AVFormatContext *avf)
Definition: fifo_test.c:101
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_usleep
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
if
if(ret)
Definition: filter_design.txt:179
AVFormatContext
Format I/O context.
Definition: avformat.h:1363
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:101
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
FFOutputFormat
Definition: mux.h:32
time.h
FailingMuxerPacketData::sleep_time
unsigned sleep_time
Definition: fifo_test.c:44
OFFSET
#define OFFSET(x)
Definition: fifo_test.c:123
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition: opt.h:251
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:469
printf
printf("static const uint8_t my_array[100] = {\n")
SLEEPTIME_10_MS
#define SLEEPTIME_10_MS
Definition: fifo_test.c:35
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
failing_muxer_class
static const AVClass failing_muxer_class
Definition: fifo_test.c:134
failing_write_packet
static int failing_write_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: fifo_test.c:66
MAX_TST_PACKETS
#define MAX_TST_PACKETS
Definition: fifo_test.c:33
else
else
Definition: snow.txt:125
url.h
FF_API_ALLOW_FLUSH
#define FF_API_ALLOW_FLUSH
Definition: version_major.h:50
write_packet
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition: ffmpeg_mux.c:209
ret
ret
Definition: filter_design.txt:187
FailingMuxerPacketData::ret
int ret
Definition: fifo_test.c:42
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
avformat.h
FailingMuxerContext::write_header_ret
int write_header_ret
Definition: fifo_test.c:50
FailingMuxerPacketData::recover_after
int recover_after
Definition: fifo_test.c:43
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
failing_write_header
static int failing_write_header(AVFormatContext *avf)
Definition: fifo_test.c:60
FailingMuxerContext::pts_written_nr
int pts_written_nr
Definition: fifo_test.c:57
AVERROR_EXIT
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:58
FailingMuxerContext::flush_count
int flush_count
Definition: fifo_test.c:55
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
failing_deinit
static void failing_deinit(AVFormatContext *avf)
Definition: fifo_test.c:107
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1391
mux.h