FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fifo_muxer.c
Go to the documentation of this file.
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 */
43 
44 
45 typedef struct FailingMuxerContext {
46  AVClass *class;
49  /* If non-zero, summary of processed packets will be printed in deinit */
51 
56 
58 {
60  return ctx->write_header_ret;
61 }
62 
64 {
66  int ret = 0;
67  if (!pkt) {
68  ctx->flush_count++;
69  } else {
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) {
84  return AVERROR_EXIT;
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 
99 {
101  return ctx->write_trailer_ret;
102 }
103 
105 {
106  int i;
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 
139  .name = "fail",
140  .long_name = NULL_IF_CONFIG_SMALL("Failing test muxer"),
141  .priv_data_size = sizeof(FailingMuxerContext),
145  .deinit = failing_deinit,
146  .priv_class = &failing_muxer_class,
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 
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 
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 
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 
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 {
345  const char *test_name;
346  const char *options;
347 
351 
353 } TestCase;
354 
355 
356 #define BUFFER_SIZE 64
357 
358 static int run_test(const TestCase *test)
359 {
361  AVFormatContext *oc = NULL;
362  char buffer[BUFFER_SIZE];
363  int ret, ret1;
364 
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");
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 }
static int run_test(const TestCase *test)
Definition: fifo_muxer.c:358
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:672
#define OFFSET(x)
Definition: fifo_muxer.c:120
#define NULL
Definition: coverity.c:32
static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc)
Definition: fifo_muxer.c:166
const char * s
Definition: avisynth_c.h:768
#define SLEEPTIME_50_MS
Definition: fifo_muxer.c:31
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1605
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:932
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int main(int argc, char *argv[])
Definition: fifo_muxer.c:433
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
static AVPacket pkt
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:495
AVOutputFormat tst_failing_muxer
Definition: fifo_muxer.c:138
Format I/O context.
Definition: avformat.h:1349
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:72
int pts_written[MAX_TST_PACKETS]
Definition: fifo_muxer.c:53
uint8_t
#define av_malloc(s)
AVOptions.
FailingMuxerPacketData pkt_data
Definition: fifo_muxer.c:352
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1675
const TestCase tests[]
Definition: fifo_muxer.c:401
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
Initialize a reference-counted packet from av_malloc()ed data.
Definition: avpacket.c:152
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4231
int64_t duration
Definition: movenc.c:63
uint8_t * data
Definition: avcodec.h:1657
static int flags
Definition: log.c:57
static int failing_write_trailer(AVFormatContext *avf)
Definition: fifo_muxer.c:98
const char * test_name
Definition: fifo_muxer.c:345
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:276
static int fifo_write_header_err_tst(AVFormatContext *oc, AVDictionary **opts, const FailingMuxerPacketData *pkt_data)
Definition: fifo_muxer.c:241
const char * options
Definition: fifo_muxer.c:346
static const AVOption options[]
Definition: fifo_muxer.c:121
int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:148
static const AVClass failing_muxer_class
Definition: fifo_muxer.c:131
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
int write_trailer_ret
Definition: fifo_muxer.c:350
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
simple assert() macros that are a bit more flexible than ISO C assert().
#define fail()
Definition: checkasm.h:89
AVDictionary * opts
Definition: movenc.c:50
static int prepare_packet(AVPacket *pkt, const FailingMuxerPacketData *pkt_data, int64_t pts)
Definition: fifo_muxer.c:150
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.
Definition: mux.c:519
#define BUFFER_SIZE
Definition: fifo_muxer.c:356
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const char * name
Definition: avformat.h:524
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts, const FailingMuxerPacketData *pkt_data)
Definition: fifo_muxer.c:188
static int failing_write_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: fifo_muxer.c:63
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
Stream structure.
Definition: avformat.h:889
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.
Definition: dict.c:180
void av_register_output_format(AVOutputFormat *format)
Definition: format.c:73
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition: avio.c:662
static void test(const char *pattern, const char *host)
Definition: noproxy.c:23
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:589
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
Describe the class of an AVClass context structure.
Definition: log.h:67
#define snprintf
Definition: snprintf.h:34
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4166
static int64_t pts
Global timestamp for the audio frames.
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
Main libavformat public API header.
int
if(ret< 0)
Definition: vf_mcdeint.c:282
uint8_t print_summary_on_deinit
Definition: fifo_muxer.c:348
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:478
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
#define SLEEPTIME_10_MS
Definition: fifo_muxer.c:32
void * priv_data
Format private data.
Definition: avformat.h:1377
#define MAX_TST_PACKETS
Definition: fifo_muxer.c:30
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1656
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1307
static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts, const FailingMuxerPacketData *data)
Definition: fifo_muxer.c:289
unbuffered private I/O API
static int failing_write_header(AVFormatContext *avf)
Definition: fifo_muxer.c:57
This structure stores compressed data.
Definition: avcodec.h:1634
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:385
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1650
GLuint buffer
Definition: opengl_enc.c:102
int(* test_func)(AVFormatContext *, AVDictionary **, const FailingMuxerPacketData *pkt_data)
Definition: fifo_muxer.c:344
static void failing_deinit(AVFormatContext *avf)
Definition: fifo_muxer.c:104
int write_header_ret
Definition: fifo_muxer.c:349