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
22 #include "libavutil/opt.h"
23 #include "libavutil/time.h"
24 #include "libavutil/thread.h"
25 #include "libavutil/threadmessage.h"
29 #define FIFO_DEFAULT_QUEUE_SIZE 60
30 #define FIFO_DEFAULT_MAX_RECOVERY_ATTEMPTS 0
31 #define FIFO_DEFAULT_RECOVERY_WAIT_TIME_USEC 5000000 // 5 seconds
33 typedef struct FifoContext {
38 char *format_options_str;
39 AVDictionary *format_options;
42 AVThreadMessageQueue *queue;
44 pthread_t writer_thread;
46 /* Return value of last write_trailer_call */
47 int write_trailer_ret;
49 /* Time to wait before next recovery attempt
50 * This can refer to the time in processed stream,
52 int64_t recovery_wait_time;
54 /* Maximal number of unsuccessful successive recovery attempts */
55 int max_recovery_attempts;
57 /* Whether to attempt recovery from failure */
60 /* If >0 stream time will be used when waiting
61 * for the recovery attempt instead of real time */
62 int recovery_wait_streamtime;
64 /* If >0 recovery will be attempted regardless of error code
65 * (except AVERROR_EXIT, so exit request is never ignored) */
66 int recover_any_error;
68 /* Whether to drop packets in case the queue is full. */
69 int drop_pkts_on_overflow;
71 /* Whether to wait for keyframe when recovering
72 * from failure or queue overflow */
73 int restart_with_keyframe;
75 pthread_mutex_t overflow_flag_lock;
76 int overflow_flag_lock_initialized;
77 /* Value > 0 signals queue overflow */
78 volatile uint8_t overflow_flag;
82 typedef struct FifoThreadContext {
85 /* Timestamp of last failure.
86 * This is either pts in case stream time is used,
87 * or microseconds as returned by av_getttime_relative() */
88 int64_t last_recovery_ts;
90 /* Number of current recovery process
91 * Value > 0 means we are in recovery process */
94 /* If > 0 all frames will be dropped until keyframe is received */
95 uint8_t drop_until_keyframe;
97 /* Value > 0 means that the previous write_header call was successful
98 * so finalization by calling write_trailer and ff_io_close must be done
99 * before exiting / reinitialization of underlying muxer */
100 uint8_t header_written;
103 typedef enum FifoMessageType {
109 typedef struct FifoMessage {
110 FifoMessageType type;
114 static int fifo_thread_write_header(FifoThreadContext *ctx)
116 AVFormatContext *avf = ctx->avf;
117 FifoContext *fifo = avf->priv_data;
118 AVFormatContext *avf2 = fifo->avf;
119 AVDictionary *format_options = NULL;
122 ret = av_dict_copy(&format_options, fifo->format_options, 0);
126 ret = ff_format_output_open(avf2, avf->filename, &format_options);
128 av_log(avf, AV_LOG_ERROR, "Error opening %s: %s\n", avf->filename,
133 for (i = 0;i < avf2->nb_streams; i++)
134 avf2->streams[i]->cur_dts = 0;
136 ret = avformat_write_header(avf2, &format_options);
138 ctx->header_written = 1;
140 // Check for options unrecognized by underlying muxer
141 if (format_options) {
142 AVDictionaryEntry *entry = NULL;
143 while ((entry = av_dict_get(format_options, "", entry, AV_DICT_IGNORE_SUFFIX)))
144 av_log(avf2, AV_LOG_ERROR, "Unknown option '%s'\n", entry->key);
145 ret = AVERROR(EINVAL);
149 av_dict_free(&format_options);
153 static int fifo_thread_flush_output(FifoThreadContext *ctx)
155 AVFormatContext *avf = ctx->avf;
156 FifoContext *fifo = avf->priv_data;
157 AVFormatContext *avf2 = fifo->avf;
159 return av_write_frame(avf2, NULL);
162 static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt)
164 AVFormatContext *avf = ctx->avf;
165 FifoContext *fifo = avf->priv_data;
166 AVFormatContext *avf2 = fifo->avf;
167 AVRational src_tb, dst_tb;
170 if (ctx->drop_until_keyframe) {
171 if (pkt->flags & AV_PKT_FLAG_KEY) {
172 ctx->drop_until_keyframe = 0;
173 av_log(avf, AV_LOG_VERBOSE, "Keyframe received, recovering...\n");
175 av_log(avf, AV_LOG_VERBOSE, "Dropping non-keyframe packet\n");
176 av_packet_unref(pkt);
181 s_idx = pkt->stream_index;
182 src_tb = avf->streams[s_idx]->time_base;
183 dst_tb = avf2->streams[s_idx]->time_base;
184 av_packet_rescale_ts(pkt, src_tb, dst_tb);
186 ret = av_write_frame(avf2, pkt);
188 av_packet_unref(pkt);
192 static int fifo_thread_write_trailer(FifoThreadContext *ctx)
194 AVFormatContext *avf = ctx->avf;
195 FifoContext *fifo = avf->priv_data;
196 AVFormatContext *avf2 = fifo->avf;
199 if (!ctx->header_written)
202 ret = av_write_trailer(avf2);
203 ff_format_io_close(avf2, &avf2->pb);
208 static int fifo_thread_dispatch_message(FifoThreadContext *ctx, FifoMessage *msg)
212 if (!ctx->header_written) {
213 ret = fifo_thread_write_header(ctx);
219 case FIFO_WRITE_HEADER:
221 case FIFO_WRITE_PACKET:
222 return fifo_thread_write_packet(ctx, &msg->pkt);
223 case FIFO_FLUSH_OUTPUT:
224 return fifo_thread_flush_output(ctx);
227 return AVERROR(EINVAL);
230 static int is_recoverable(const FifoContext *fifo, int err_no) {
231 if (!fifo->attempt_recovery)
234 if (fifo->recover_any_error)
235 return err_no != AVERROR_EXIT;
238 case AVERROR(EINVAL):
239 case AVERROR(ENOSYS):
242 case AVERROR_PATCHWELCOME:
249 static void free_message(void *msg)
251 FifoMessage *fifo_msg = msg;
253 if (fifo_msg->type == FIFO_WRITE_PACKET)
254 av_packet_unref(&fifo_msg->pkt);
257 static int fifo_thread_process_recovery_failure(FifoThreadContext *ctx, AVPacket *pkt,
260 AVFormatContext *avf = ctx->avf;
261 FifoContext *fifo = avf->priv_data;
264 av_log(avf, AV_LOG_INFO, "Recovery failed: %s\n",
267 if (fifo->recovery_wait_streamtime) {
268 if (pkt->pts == AV_NOPTS_VALUE)
269 av_log(avf, AV_LOG_WARNING, "Packet does not contain presentation"
270 " timestamp, recovery will be attempted immediately");
271 ctx->last_recovery_ts = pkt->pts;
273 ctx->last_recovery_ts = av_gettime_relative();
276 if (fifo->max_recovery_attempts &&
277 ctx->recovery_nr >= fifo->max_recovery_attempts) {
278 av_log(avf, AV_LOG_ERROR,
279 "Maximal number of %d recovery attempts reached.\n",
280 fifo->max_recovery_attempts);
283 ret = AVERROR(EAGAIN);
289 static int fifo_thread_attempt_recovery(FifoThreadContext *ctx, FifoMessage *msg, int err_no)
291 AVFormatContext *avf = ctx->avf;
292 FifoContext *fifo = avf->priv_data;
293 AVPacket *pkt = &msg->pkt;
294 int64_t time_since_recovery;
297 if (!is_recoverable(fifo, err_no)) {
302 if (ctx->header_written) {
303 fifo->write_trailer_ret = fifo_thread_write_trailer(ctx);
304 ctx->header_written = 0;
307 if (!ctx->recovery_nr) {
308 ctx->last_recovery_ts = fifo->recovery_wait_streamtime ?
311 if (fifo->recovery_wait_streamtime) {
312 if (ctx->last_recovery_ts == AV_NOPTS_VALUE) {
313 AVRational tb = avf->streams[pkt->stream_index]->time_base;
314 time_since_recovery = av_rescale_q(pkt->pts - ctx->last_recovery_ts,
317 /* Enforce recovery immediately */
318 time_since_recovery = fifo->recovery_wait_time;
321 time_since_recovery = av_gettime_relative() - ctx->last_recovery_ts;
324 if (time_since_recovery < fifo->recovery_wait_time)
325 return AVERROR(EAGAIN);
330 if (fifo->max_recovery_attempts) {
331 av_log(avf, AV_LOG_VERBOSE, "Recovery attempt #%d/%d\n",
332 ctx->recovery_nr, fifo->max_recovery_attempts);
334 av_log(avf, AV_LOG_VERBOSE, "Recovery attempt #%d\n",
338 if (fifo->restart_with_keyframe && fifo->drop_pkts_on_overflow)
339 ctx->drop_until_keyframe = 1;
341 ret = fifo_thread_dispatch_message(ctx, msg);
343 if (is_recoverable(fifo, ret)) {
344 return fifo_thread_process_recovery_failure(ctx, pkt, ret);
349 av_log(avf, AV_LOG_INFO, "Recovery successful\n");
350 ctx->recovery_nr = 0;
360 static int fifo_thread_recover(FifoThreadContext *ctx, FifoMessage *msg, int err_no)
362 AVFormatContext *avf = ctx->avf;
363 FifoContext *fifo = avf->priv_data;
367 if (!fifo->recovery_wait_streamtime && ctx->recovery_nr > 0) {
368 int64_t time_since_recovery = av_gettime_relative() - ctx->last_recovery_ts;
369 int64_t time_to_wait = FFMAX(0, fifo->recovery_wait_time - time_since_recovery);
371 av_usleep(FFMIN(10000, time_to_wait));
374 ret = fifo_thread_attempt_recovery(ctx, msg, err_no);
375 } while (ret == AVERROR(EAGAIN) && !fifo->drop_pkts_on_overflow);
377 if (ret == AVERROR(EAGAIN) && fifo->drop_pkts_on_overflow) {
378 if (msg->type == FIFO_WRITE_PACKET)
379 av_packet_unref(&msg->pkt);
386 static void *fifo_consumer_thread(void *data)
388 AVFormatContext *avf = data;
389 FifoContext *fifo = avf->priv_data;
390 AVThreadMessageQueue *queue = fifo->queue;
391 FifoMessage msg = {FIFO_WRITE_HEADER, {0}};
394 FifoThreadContext fifo_thread_ctx;
395 memset(&fifo_thread_ctx, 0, sizeof(FifoThreadContext));
396 fifo_thread_ctx.avf = avf;
399 uint8_t just_flushed = 0;
401 if (!fifo_thread_ctx.recovery_nr)
402 ret = fifo_thread_dispatch_message(&fifo_thread_ctx, &msg);
404 if (ret < 0 || fifo_thread_ctx.recovery_nr > 0) {
405 int rec_ret = fifo_thread_recover(&fifo_thread_ctx, &msg, ret);
407 av_thread_message_queue_set_err_send(queue, rec_ret);
412 /* If the queue is full at the moment when fifo_write_packet
413 * attempts to insert new message (packet) to the queue,
414 * it sets the fifo->overflow_flag to 1 and drops packet.
415 * Here in consumer thread, the flag is checked and if it is
416 * set, the queue is flushed and flag cleared. */
417 pthread_mutex_lock(&fifo->overflow_flag_lock);
418 if (fifo->overflow_flag) {
419 av_thread_message_flush(queue);
420 if (fifo->restart_with_keyframe)
421 fifo_thread_ctx.drop_until_keyframe = 1;
422 fifo->overflow_flag = 0;
425 pthread_mutex_unlock(&fifo->overflow_flag_lock);
428 av_log(avf, AV_LOG_INFO, "FIFO queue flushed\n");
430 ret = av_thread_message_queue_recv(queue, &msg, 0);
432 av_thread_message_queue_set_err_send(queue, ret);
437 fifo->write_trailer_ret = fifo_thread_write_trailer(&fifo_thread_ctx);
442 static int fifo_mux_init(AVFormatContext *avf, AVOutputFormat *oformat)
444 FifoContext *fifo = avf->priv_data;
445 AVFormatContext *avf2;
448 ret = avformat_alloc_output_context2(&avf2, oformat, NULL, NULL);
454 avf2->interrupt_callback = avf->interrupt_callback;
455 avf2->max_delay = avf->max_delay;
456 ret = av_dict_copy(&avf2->metadata, avf->metadata, 0);
459 avf2->opaque = avf->opaque;
460 avf2->io_close = avf->io_close;
461 avf2->io_open = avf->io_open;
462 avf2->flags = avf->flags;
464 for (i = 0; i < avf->nb_streams; ++i) {
465 AVStream *st = avformat_new_stream(avf2, NULL);
467 return AVERROR(ENOMEM);
469 ret = ff_stream_encode_params_copy(st, avf->streams[i]);
477 static int fifo_init(AVFormatContext *avf)
479 FifoContext *fifo = avf->priv_data;
480 AVOutputFormat *oformat;
483 if (fifo->recovery_wait_streamtime && !fifo->drop_pkts_on_overflow) {
484 av_log(avf, AV_LOG_ERROR, "recovery_wait_streamtime can be turned on"
485 " only when drop_pkts_on_overflow is also turned on\n");
486 return AVERROR(EINVAL);
489 if (fifo->format_options_str) {
490 ret = av_dict_parse_string(&fifo->format_options, fifo->format_options_str,
493 av_log(avf, AV_LOG_ERROR, "Could not parse format options list '%s'\n",
494 fifo->format_options_str);
499 oformat = av_guess_format(fifo->format, avf->filename, NULL);
501 ret = AVERROR_MUXER_NOT_FOUND;
505 ret = fifo_mux_init(avf, oformat);
509 ret = av_thread_message_queue_alloc(&fifo->queue, (unsigned) fifo->queue_size,
510 sizeof(FifoMessage));
514 av_thread_message_queue_set_free_func(fifo->queue, free_message);
516 ret = pthread_mutex_init(&fifo->overflow_flag_lock, NULL);
519 fifo->overflow_flag_lock_initialized = 1;
524 static int fifo_write_header(AVFormatContext *avf)
526 FifoContext * fifo = avf->priv_data;
529 ret = pthread_create(&fifo->writer_thread, NULL, fifo_consumer_thread, avf);
531 av_log(avf, AV_LOG_ERROR, "Failed to start thread: %s\n",
532 av_err2str(AVERROR(ret)));
539 static int fifo_write_packet(AVFormatContext *avf, AVPacket *pkt)
541 FifoContext *fifo = avf->priv_data;
542 FifoMessage msg = {.type = pkt ? FIFO_WRITE_PACKET : FIFO_FLUSH_OUTPUT};
546 av_init_packet(&msg.pkt);
547 ret = av_packet_ref(&msg.pkt,pkt);
552 ret = av_thread_message_queue_send(fifo->queue, &msg,
553 fifo->drop_pkts_on_overflow ?
554 AV_THREAD_MESSAGE_NONBLOCK : 0);
555 if (ret == AVERROR(EAGAIN)) {
556 uint8_t overflow_set = 0;
558 /* Queue is full, set fifo->overflow_flag to 1
559 * to let consumer thread know the queue should
561 pthread_mutex_lock(&fifo->overflow_flag_lock);
562 if (!fifo->overflow_flag)
563 fifo->overflow_flag = overflow_set = 1;
564 pthread_mutex_unlock(&fifo->overflow_flag_lock);
567 av_log(avf, AV_LOG_WARNING, "FIFO queue full\n");
570 } else if (ret < 0) {
577 av_packet_unref(&msg.pkt);
581 static int fifo_write_trailer(AVFormatContext *avf)
583 FifoContext *fifo= avf->priv_data;
586 av_thread_message_queue_set_err_recv(fifo->queue, AVERROR_EOF);
588 ret = pthread_join(fifo->writer_thread, NULL);
590 av_log(avf, AV_LOG_ERROR, "pthread join error: %s\n",
591 av_err2str(AVERROR(ret)));
595 ret = fifo->write_trailer_ret;
599 static void fifo_deinit(AVFormatContext *avf)
601 FifoContext *fifo = avf->priv_data;
603 av_dict_free(&fifo->format_options);
604 avformat_free_context(fifo->avf);
605 av_thread_message_queue_free(&fifo->queue);
606 if (fifo->overflow_flag_lock_initialized)
607 pthread_mutex_destroy(&fifo->overflow_flag_lock);
610 #define OFFSET(x) offsetof(FifoContext, x)
611 static const AVOption options[] = {
612 {"fifo_format", "Target muxer", OFFSET(format),
613 AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM},
615 {"queue_size", "Size of fifo queue", OFFSET(queue_size),
616 AV_OPT_TYPE_INT, {.i64 = FIFO_DEFAULT_QUEUE_SIZE}, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
618 {"format_opts", "Options to be passed to underlying muxer", OFFSET(format_options_str),
619 AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM},
621 {"drop_pkts_on_overflow", "Drop packets on fifo queue overflow not to block encoder", OFFSET(drop_pkts_on_overflow),
622 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
624 {"restart_with_keyframe", "Wait for keyframe when restarting output", OFFSET(restart_with_keyframe),
625 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
627 {"attempt_recovery", "Attempt recovery in case of failure", OFFSET(attempt_recovery),
628 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
630 {"max_recovery_attempts", "Maximal number of recovery attempts", OFFSET(max_recovery_attempts),
631 AV_OPT_TYPE_INT, {.i64 = FIFO_DEFAULT_MAX_RECOVERY_ATTEMPTS}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
633 {"recovery_wait_time", "Waiting time between recovery attempts", OFFSET(recovery_wait_time),
634 AV_OPT_TYPE_DURATION, {.i64 = FIFO_DEFAULT_RECOVERY_WAIT_TIME_USEC}, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM},
636 {"recovery_wait_streamtime", "Use stream time instead of real time while waiting for recovery",
637 OFFSET(recovery_wait_streamtime), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
639 {"recover_any_error", "Attempt recovery regardless of type of the error", OFFSET(recover_any_error),
640 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
645 static const AVClass fifo_muxer_class = {
646 .class_name = "Fifo muxer",
647 .item_name = av_default_item_name,
649 .version = LIBAVUTIL_VERSION_INT,
652 AVOutputFormat ff_fifo_muxer = {
654 .long_name = NULL_IF_CONFIG_SMALL("FIFO queue pseudo-muxer"),
655 .priv_data_size = sizeof(FifoContext),
657 .write_header = fifo_write_header,
658 .write_packet = fifo_write_packet,
659 .write_trailer = fifo_write_trailer,
660 .deinit = fifo_deinit,
661 .priv_class = &fifo_muxer_class,
662 .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,