LCOV - code coverage report
Current view: top level - src/libavformat - fifo.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 228 285 80.0 %
Date: 2017-01-24 04:42:20 Functions: 17 17 100.0 %

          Line data    Source code
       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 "libavutil/opt.h"
      23             : #include "libavutil/time.h"
      24             : #include "libavutil/thread.h"
      25             : #include "libavutil/threadmessage.h"
      26             : #include "avformat.h"
      27             : #include "internal.h"
      28             : 
      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
      32             : 
      33             : typedef struct FifoContext {
      34             :     const AVClass *class;
      35             :     AVFormatContext *avf;
      36             : 
      37             :     char *format;
      38             :     char *format_options_str;
      39             :     AVDictionary *format_options;
      40             : 
      41             :     int queue_size;
      42             :     AVThreadMessageQueue *queue;
      43             : 
      44             :     pthread_t writer_thread;
      45             : 
      46             :     /* Return value of last write_trailer_call */
      47             :     int write_trailer_ret;
      48             : 
      49             :     /* Time to wait before next recovery attempt
      50             :      * This can refer to the time in processed stream,
      51             :      * or real time. */
      52             :     int64_t recovery_wait_time;
      53             : 
      54             :     /* Maximal number of unsuccessful successive recovery attempts */
      55             :     int max_recovery_attempts;
      56             : 
      57             :     /* Whether to attempt recovery from failure */
      58             :     int attempt_recovery;
      59             : 
      60             :     /* If >0 stream time will be used when waiting
      61             :      * for the recovery attempt instead of real time */
      62             :     int recovery_wait_streamtime;
      63             : 
      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;
      67             : 
      68             :     /* Whether to drop packets in case the queue is full. */
      69             :     int drop_pkts_on_overflow;
      70             : 
      71             :     /* Whether to wait for keyframe when recovering
      72             :      * from failure or queue overflow */
      73             :     int restart_with_keyframe;
      74             : 
      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;
      79             : 
      80             : } FifoContext;
      81             : 
      82             : typedef struct FifoThreadContext {
      83             :     AVFormatContext *avf;
      84             : 
      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;
      89             : 
      90             :     /* Number of current recovery process
      91             :      * Value > 0 means we are in recovery process */
      92             :     int recovery_nr;
      93             : 
      94             :     /* If > 0 all frames will be dropped until keyframe is received */
      95             :     uint8_t drop_until_keyframe;
      96             : 
      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;
     101             : } FifoThreadContext;
     102             : 
     103             : typedef enum FifoMessageType {
     104             :     FIFO_WRITE_HEADER,
     105             :     FIFO_WRITE_PACKET,
     106             :     FIFO_FLUSH_OUTPUT
     107             : } FifoMessageType;
     108             : 
     109             : typedef struct FifoMessage {
     110             :     FifoMessageType type;
     111             :     AVPacket pkt;
     112             : } FifoMessage;
     113             : 
     114          52 : static int fifo_thread_write_header(FifoThreadContext *ctx)
     115             : {
     116          52 :     AVFormatContext *avf = ctx->avf;
     117          52 :     FifoContext *fifo = avf->priv_data;
     118          52 :     AVFormatContext *avf2 = fifo->avf;
     119          52 :     AVDictionary *format_options = NULL;
     120             :     int ret, i;
     121             : 
     122          52 :     ret = av_dict_copy(&format_options, fifo->format_options, 0);
     123          52 :     if (ret < 0)
     124           0 :         return ret;
     125             : 
     126          52 :     ret = ff_format_output_open(avf2, avf->filename, &format_options);
     127          52 :     if (ret < 0) {
     128           0 :         av_log(avf, AV_LOG_ERROR, "Error opening %s: %s\n", avf->filename,
     129           0 :                av_err2str(ret));
     130           0 :         goto end;
     131             :     }
     132             : 
     133         105 :     for (i = 0;i < avf2->nb_streams; i++)
     134          53 :         avf2->streams[i]->cur_dts = 0;
     135             : 
     136          52 :     ret = avformat_write_header(avf2, &format_options);
     137          52 :     if (!ret)
     138          51 :         ctx->header_written = 1;
     139             : 
     140             :     // Check for options unrecognized by underlying muxer
     141          52 :     if (format_options) {
     142           0 :         AVDictionaryEntry *entry = NULL;
     143           0 :         while ((entry = av_dict_get(format_options, "", entry, AV_DICT_IGNORE_SUFFIX)))
     144           0 :             av_log(avf2, AV_LOG_ERROR, "Unknown option '%s'\n", entry->key);
     145           0 :         ret = AVERROR(EINVAL);
     146             :     }
     147             : 
     148             : end:
     149          52 :     av_dict_free(&format_options);
     150          52 :     return ret;
     151             : }
     152             : 
     153           3 : static int fifo_thread_flush_output(FifoThreadContext *ctx)
     154             : {
     155           3 :     AVFormatContext *avf = ctx->avf;
     156           3 :     FifoContext *fifo = avf->priv_data;
     157           3 :     AVFormatContext *avf2 = fifo->avf;
     158             : 
     159           3 :     return av_write_frame(avf2, NULL);
     160             : }
     161             : 
     162         217 : static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt)
     163             : {
     164         217 :     AVFormatContext *avf = ctx->avf;
     165         217 :     FifoContext *fifo = avf->priv_data;
     166         217 :     AVFormatContext *avf2 = fifo->avf;
     167             :     AVRational src_tb, dst_tb;
     168             :     int ret, s_idx;
     169             : 
     170         217 :     if (ctx->drop_until_keyframe) {
     171           0 :         if (pkt->flags & AV_PKT_FLAG_KEY) {
     172           0 :             ctx->drop_until_keyframe = 0;
     173           0 :             av_log(avf, AV_LOG_VERBOSE, "Keyframe received, recovering...\n");
     174             :         } else {
     175           0 :             av_log(avf, AV_LOG_VERBOSE, "Dropping non-keyframe packet\n");
     176           0 :             av_packet_unref(pkt);
     177           0 :             return 0;
     178             :         }
     179             :     }
     180             : 
     181         217 :     s_idx = pkt->stream_index;
     182         217 :     src_tb = avf->streams[s_idx]->time_base;
     183         217 :     dst_tb = avf2->streams[s_idx]->time_base;
     184         217 :     av_packet_rescale_ts(pkt, src_tb, dst_tb);
     185             : 
     186         217 :     ret = av_write_frame(avf2, pkt);
     187         217 :     if (ret >= 0)
     188         172 :         av_packet_unref(pkt);
     189         217 :     return ret;
     190             : }
     191             : 
     192          52 : static int fifo_thread_write_trailer(FifoThreadContext *ctx)
     193             : {
     194          52 :     AVFormatContext *avf = ctx->avf;
     195          52 :     FifoContext *fifo = avf->priv_data;
     196          52 :     AVFormatContext *avf2 = fifo->avf;
     197             :     int ret;
     198             : 
     199          52 :     if (!ctx->header_written)
     200           1 :         return 0;
     201             : 
     202          51 :     ret = av_write_trailer(avf2);
     203          51 :     ff_format_io_close(avf2, &avf2->pb);
     204             : 
     205          51 :     return ret;
     206             : }
     207             : 
     208         227 : static int fifo_thread_dispatch_message(FifoThreadContext *ctx, FifoMessage *msg)
     209             : {
     210             :     int ret;
     211             : 
     212         227 :     if (!ctx->header_written) {
     213          52 :         ret = fifo_thread_write_header(ctx);
     214          52 :         if (ret < 0)
     215           1 :             return ret;
     216             :     }
     217             : 
     218         226 :     switch(msg->type) {
     219             :     case FIFO_WRITE_HEADER:
     220           6 :         return ret;
     221             :     case FIFO_WRITE_PACKET:
     222         217 :         return fifo_thread_write_packet(ctx, &msg->pkt);
     223             :     case FIFO_FLUSH_OUTPUT:
     224           3 :         return fifo_thread_flush_output(ctx);
     225             :     }
     226             : 
     227           0 :     return AVERROR(EINVAL);
     228             : }
     229             : 
     230          76 : static int is_recoverable(const FifoContext *fifo, int err_no) {
     231          76 :     if (!fifo->attempt_recovery)
     232           1 :         return 0;
     233             : 
     234          75 :     if (fifo->recover_any_error)
     235           0 :         return err_no != AVERROR_EXIT;
     236             : 
     237          75 :     switch (err_no) {
     238             :     case AVERROR(EINVAL):
     239             :     case AVERROR(ENOSYS):
     240             :     case AVERROR_EOF:
     241             :     case AVERROR_EXIT:
     242             :     case AVERROR_PATCHWELCOME:
     243           0 :         return 0;
     244             :     default:
     245          75 :         return 1;
     246             :     }
     247             : }
     248             : 
     249          64 : static void free_message(void *msg)
     250             : {
     251          64 :     FifoMessage *fifo_msg = msg;
     252             : 
     253          64 :     if (fifo_msg->type == FIFO_WRITE_PACKET)
     254          63 :         av_packet_unref(&fifo_msg->pkt);
     255          64 : }
     256             : 
     257          30 : static int fifo_thread_process_recovery_failure(FifoThreadContext *ctx, AVPacket *pkt,
     258             :                                                 int err_no)
     259             : {
     260          30 :     AVFormatContext *avf = ctx->avf;
     261          30 :     FifoContext *fifo = avf->priv_data;
     262             :     int ret;
     263             : 
     264          30 :     av_log(avf, AV_LOG_INFO, "Recovery failed: %s\n",
     265          30 :            av_err2str(err_no));
     266             : 
     267          30 :     if (fifo->recovery_wait_streamtime) {
     268           0 :         if (pkt->pts == AV_NOPTS_VALUE)
     269           0 :             av_log(avf, AV_LOG_WARNING, "Packet does not contain presentation"
     270             :                    " timestamp, recovery will be attempted immediately");
     271           0 :         ctx->last_recovery_ts = pkt->pts;
     272             :     } else {
     273          30 :         ctx->last_recovery_ts = av_gettime_relative();
     274             :     }
     275             : 
     276          30 :     if (fifo->max_recovery_attempts &&
     277           0 :         ctx->recovery_nr >= fifo->max_recovery_attempts) {
     278           0 :         av_log(avf, AV_LOG_ERROR,
     279             :                "Maximal number of %d recovery attempts reached.\n",
     280             :                fifo->max_recovery_attempts);
     281           0 :         ret = err_no;
     282             :     } else {
     283          30 :         ret = AVERROR(EAGAIN);
     284             :     }
     285             : 
     286          30 :     return ret;
     287             : }
     288             : 
     289          46 : static int fifo_thread_attempt_recovery(FifoThreadContext *ctx, FifoMessage *msg, int err_no)
     290             : {
     291          46 :     AVFormatContext *avf = ctx->avf;
     292          46 :     FifoContext *fifo = avf->priv_data;
     293          46 :     AVPacket *pkt = &msg->pkt;
     294             :     int64_t time_since_recovery;
     295             :     int ret;
     296             : 
     297          46 :     if (!is_recoverable(fifo, err_no)) {
     298           1 :         ret = err_no;
     299           1 :         goto fail;
     300             :     }
     301             : 
     302          45 :     if (ctx->header_written) {
     303          45 :         fifo->write_trailer_ret = fifo_thread_write_trailer(ctx);
     304          45 :         ctx->header_written = 0;
     305             :     }
     306             : 
     307          45 :     if (!ctx->recovery_nr) {
     308          30 :         ctx->last_recovery_ts = fifo->recovery_wait_streamtime ?
     309          15 :                                 AV_NOPTS_VALUE : 0;
     310             :     } else {
     311          30 :         if (fifo->recovery_wait_streamtime) {
     312           0 :             if (ctx->last_recovery_ts == AV_NOPTS_VALUE) {
     313           0 :                 AVRational tb = avf->streams[pkt->stream_index]->time_base;
     314           0 :                 time_since_recovery = av_rescale_q(pkt->pts - ctx->last_recovery_ts,
     315           0 :                                                    tb, AV_TIME_BASE_Q);
     316             :             } else {
     317             :                 /* Enforce recovery immediately */
     318           0 :                 time_since_recovery = fifo->recovery_wait_time;
     319             :             }
     320             :         } else {
     321          30 :             time_since_recovery = av_gettime_relative() - ctx->last_recovery_ts;
     322             :         }
     323             : 
     324          30 :         if (time_since_recovery < fifo->recovery_wait_time)
     325           0 :             return AVERROR(EAGAIN);
     326             :     }
     327             : 
     328          45 :     ctx->recovery_nr++;
     329             : 
     330          45 :     if (fifo->max_recovery_attempts) {
     331           0 :         av_log(avf, AV_LOG_VERBOSE, "Recovery attempt #%d/%d\n",
     332             :                ctx->recovery_nr, fifo->max_recovery_attempts);
     333             :     } else {
     334          45 :         av_log(avf, AV_LOG_VERBOSE, "Recovery attempt #%d\n",
     335             :                ctx->recovery_nr);
     336             :     }
     337             : 
     338          45 :     if (fifo->restart_with_keyframe && fifo->drop_pkts_on_overflow)
     339           0 :         ctx->drop_until_keyframe = 1;
     340             : 
     341          45 :     ret = fifo_thread_dispatch_message(ctx, msg);
     342          45 :     if (ret < 0) {
     343          30 :         if (is_recoverable(fifo, ret)) {
     344          30 :             return fifo_thread_process_recovery_failure(ctx, pkt, ret);
     345             :         } else {
     346           0 :             goto fail;
     347             :         }
     348             :     } else {
     349          15 :         av_log(avf, AV_LOG_INFO, "Recovery successful\n");
     350          15 :         ctx->recovery_nr = 0;
     351             :     }
     352             : 
     353          15 :     return 0;
     354             : 
     355             : fail:
     356           1 :     free_message(msg);
     357           1 :     return ret;
     358             : }
     359             : 
     360          16 : static int fifo_thread_recover(FifoThreadContext *ctx, FifoMessage *msg, int err_no)
     361             : {
     362          16 :     AVFormatContext *avf = ctx->avf;
     363          16 :     FifoContext *fifo = avf->priv_data;
     364             :     int ret;
     365             : 
     366             :     do {
     367          46 :         if (!fifo->recovery_wait_streamtime && ctx->recovery_nr > 0) {
     368          30 :             int64_t time_since_recovery = av_gettime_relative() - ctx->last_recovery_ts;
     369          30 :             int64_t time_to_wait = FFMAX(0, fifo->recovery_wait_time - time_since_recovery);
     370          30 :             if (time_to_wait)
     371           0 :                 av_usleep(FFMIN(10000, time_to_wait));
     372             :         }
     373             : 
     374          46 :         ret = fifo_thread_attempt_recovery(ctx, msg, err_no);
     375          46 :     } while (ret == AVERROR(EAGAIN) && !fifo->drop_pkts_on_overflow);
     376             : 
     377          16 :     if (ret == AVERROR(EAGAIN) && fifo->drop_pkts_on_overflow) {
     378           0 :         if (msg->type == FIFO_WRITE_PACKET)
     379           0 :             av_packet_unref(&msg->pkt);
     380           0 :         ret = 0;
     381             :     }
     382             : 
     383          16 :     return ret;
     384             : }
     385             : 
     386           7 : static void *fifo_consumer_thread(void *data)
     387             : {
     388           7 :     AVFormatContext *avf = data;
     389           7 :     FifoContext *fifo = avf->priv_data;
     390           7 :     AVThreadMessageQueue *queue = fifo->queue;
     391           7 :     FifoMessage msg = {FIFO_WRITE_HEADER, {0}};
     392             :     int ret;
     393             : 
     394             :     FifoThreadContext fifo_thread_ctx;
     395           7 :     memset(&fifo_thread_ctx, 0, sizeof(FifoThreadContext));
     396           7 :     fifo_thread_ctx.avf = avf;
     397             : 
     398         175 :     while (1) {
     399         182 :         uint8_t just_flushed = 0;
     400             : 
     401         182 :         if (!fifo_thread_ctx.recovery_nr)
     402         182 :             ret = fifo_thread_dispatch_message(&fifo_thread_ctx, &msg);
     403             : 
     404         182 :         if (ret < 0 || fifo_thread_ctx.recovery_nr > 0) {
     405          16 :             int rec_ret = fifo_thread_recover(&fifo_thread_ctx, &msg, ret);
     406          16 :             if (rec_ret < 0) {
     407           1 :                 av_thread_message_queue_set_err_send(queue, rec_ret);
     408           1 :                 break;
     409             :             }
     410             :         }
     411             : 
     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         181 :         pthread_mutex_lock(&fifo->overflow_flag_lock);
     418         181 :         if (fifo->overflow_flag) {
     419           1 :             av_thread_message_flush(queue);
     420           1 :             if (fifo->restart_with_keyframe)
     421           0 :                 fifo_thread_ctx.drop_until_keyframe = 1;
     422           1 :             fifo->overflow_flag = 0;
     423           1 :             just_flushed = 1;
     424             :         }
     425         181 :         pthread_mutex_unlock(&fifo->overflow_flag_lock);
     426             : 
     427         181 :         if (just_flushed)
     428           1 :             av_log(avf, AV_LOG_INFO, "FIFO queue flushed\n");
     429             : 
     430         181 :         ret = av_thread_message_queue_recv(queue, &msg, 0);
     431         181 :         if (ret < 0) {
     432           6 :             av_thread_message_queue_set_err_send(queue, ret);
     433           6 :             break;
     434             :         }
     435             :     }
     436             : 
     437           7 :     fifo->write_trailer_ret = fifo_thread_write_trailer(&fifo_thread_ctx);
     438             : 
     439           7 :     return NULL;
     440             : }
     441             : 
     442           7 : static int fifo_mux_init(AVFormatContext *avf, AVOutputFormat *oformat)
     443             : {
     444           7 :     FifoContext *fifo = avf->priv_data;
     445             :     AVFormatContext *avf2;
     446           7 :     int ret = 0, i;
     447             : 
     448           7 :     ret = avformat_alloc_output_context2(&avf2, oformat, NULL, NULL);
     449           7 :     if (ret < 0)
     450           0 :         return ret;
     451             : 
     452           7 :     fifo->avf = avf2;
     453             : 
     454           7 :     avf2->interrupt_callback = avf->interrupt_callback;
     455           7 :     avf2->max_delay = avf->max_delay;
     456           7 :     ret = av_dict_copy(&avf2->metadata, avf->metadata, 0);
     457           7 :     if (ret < 0)
     458           0 :         return ret;
     459           7 :     avf2->opaque = avf->opaque;
     460           7 :     avf2->io_close = avf->io_close;
     461           7 :     avf2->io_open = avf->io_open;
     462           7 :     avf2->flags = avf->flags;
     463             : 
     464          15 :     for (i = 0; i < avf->nb_streams; ++i) {
     465           8 :         AVStream *st = avformat_new_stream(avf2, NULL);
     466           8 :         if (!st)
     467           0 :             return AVERROR(ENOMEM);
     468             : 
     469           8 :         ret = ff_stream_encode_params_copy(st, avf->streams[i]);
     470           8 :         if (ret < 0)
     471           0 :             return ret;
     472             :     }
     473             : 
     474           7 :     return 0;
     475             : }
     476             : 
     477           7 : static int fifo_init(AVFormatContext *avf)
     478             : {
     479           7 :     FifoContext *fifo = avf->priv_data;
     480             :     AVOutputFormat *oformat;
     481           7 :     int ret = 0;
     482             : 
     483           7 :     if (fifo->recovery_wait_streamtime && !fifo->drop_pkts_on_overflow) {
     484           0 :         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           0 :         return AVERROR(EINVAL);
     487             :     }
     488             : 
     489           7 :     if (fifo->format_options_str) {
     490           5 :         ret = av_dict_parse_string(&fifo->format_options, fifo->format_options_str,
     491             :                                    "=", ":", 0);
     492           5 :         if (ret < 0) {
     493           0 :             av_log(avf, AV_LOG_ERROR, "Could not parse format options list '%s'\n",
     494             :                    fifo->format_options_str);
     495           0 :             return ret;
     496             :         }
     497             :     }
     498             : 
     499           7 :     oformat = av_guess_format(fifo->format, avf->filename, NULL);
     500           7 :     if (!oformat) {
     501           0 :         ret = AVERROR_MUXER_NOT_FOUND;
     502           0 :         return ret;
     503             :     }
     504             : 
     505           7 :     ret = fifo_mux_init(avf, oformat);
     506           7 :     if (ret < 0)
     507           0 :         return ret;
     508             : 
     509           7 :     ret = av_thread_message_queue_alloc(&fifo->queue, (unsigned) fifo->queue_size,
     510             :                                         sizeof(FifoMessage));
     511           7 :     if (ret < 0)
     512           0 :         return ret;
     513             : 
     514           7 :     av_thread_message_queue_set_free_func(fifo->queue, free_message);
     515             : 
     516           7 :     ret = pthread_mutex_init(&fifo->overflow_flag_lock, NULL);
     517           7 :     if (ret < 0)
     518           0 :         return AVERROR(ret);
     519           7 :     fifo->overflow_flag_lock_initialized = 1;
     520             : 
     521           7 :     return 0;
     522             : }
     523             : 
     524           7 : static int fifo_write_header(AVFormatContext *avf)
     525             : {
     526           7 :     FifoContext * fifo = avf->priv_data;
     527             :     int ret;
     528             : 
     529           7 :     ret = pthread_create(&fifo->writer_thread, NULL, fifo_consumer_thread, avf);
     530           7 :     if (ret) {
     531           0 :         av_log(avf, AV_LOG_ERROR, "Failed to start thread: %s\n",
     532           0 :                av_err2str(AVERROR(ret)));
     533           0 :         ret = AVERROR(ret);
     534             :     }
     535             : 
     536           7 :     return ret;
     537             : }
     538             : 
     539         242 : static int fifo_write_packet(AVFormatContext *avf, AVPacket *pkt)
     540             : {
     541         242 :     FifoContext *fifo = avf->priv_data;
     542         242 :     FifoMessage msg = {.type = pkt ? FIFO_WRITE_PACKET : FIFO_FLUSH_OUTPUT};
     543             :     int ret;
     544             : 
     545         242 :     if (pkt) {
     546         239 :         av_init_packet(&msg.pkt);
     547         239 :         ret = av_packet_ref(&msg.pkt,pkt);
     548         239 :         if (ret < 0)
     549           0 :             return ret;
     550             :     }
     551             : 
     552         242 :     ret = av_thread_message_queue_send(fifo->queue, &msg,
     553         242 :                                        fifo->drop_pkts_on_overflow ?
     554             :                                        AV_THREAD_MESSAGE_NONBLOCK : 0);
     555         242 :     if (ret == AVERROR(EAGAIN)) {
     556           3 :         uint8_t overflow_set = 0;
     557             : 
     558             :         /* Queue is full, set fifo->overflow_flag to 1
     559             :          * to let consumer thread know the queue should
     560             :          * be flushed. */
     561           3 :         pthread_mutex_lock(&fifo->overflow_flag_lock);
     562           3 :         if (!fifo->overflow_flag)
     563           1 :             fifo->overflow_flag = overflow_set = 1;
     564           3 :         pthread_mutex_unlock(&fifo->overflow_flag_lock);
     565             : 
     566           3 :         if (overflow_set)
     567           1 :             av_log(avf, AV_LOG_WARNING, "FIFO queue full\n");
     568           3 :         ret = 0;
     569           3 :         goto fail;
     570         239 :     } else if (ret < 0) {
     571           1 :         goto fail;
     572             :     }
     573             : 
     574         238 :     return ret;
     575             : fail:
     576           4 :     if (pkt)
     577           4 :         av_packet_unref(&msg.pkt);
     578           4 :     return ret;
     579             : }
     580             : 
     581           7 : static int fifo_write_trailer(AVFormatContext *avf)
     582             : {
     583           7 :     FifoContext *fifo= avf->priv_data;
     584             :     int ret;
     585             : 
     586           7 :     av_thread_message_queue_set_err_recv(fifo->queue, AVERROR_EOF);
     587             : 
     588           7 :     ret = pthread_join(fifo->writer_thread, NULL);
     589           7 :     if (ret < 0) {
     590           0 :         av_log(avf, AV_LOG_ERROR, "pthread join error: %s\n",
     591           0 :                av_err2str(AVERROR(ret)));
     592           0 :         return AVERROR(ret);
     593             :     }
     594             : 
     595           7 :     ret = fifo->write_trailer_ret;
     596           7 :     return ret;
     597             : }
     598             : 
     599           7 : static void fifo_deinit(AVFormatContext *avf)
     600             : {
     601           7 :     FifoContext *fifo = avf->priv_data;
     602             : 
     603           7 :     av_dict_free(&fifo->format_options);
     604           7 :     avformat_free_context(fifo->avf);
     605           7 :     av_thread_message_queue_free(&fifo->queue);
     606           7 :     if (fifo->overflow_flag_lock_initialized)
     607           7 :         pthread_mutex_destroy(&fifo->overflow_flag_lock);
     608           7 : }
     609             : 
     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},
     614             : 
     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},
     617             : 
     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},
     620             : 
     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},
     623             : 
     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},
     626             : 
     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},
     629             : 
     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},
     632             : 
     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},
     635             : 
     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},
     638             : 
     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},
     641             : 
     642             :         {NULL},
     643             : };
     644             : 
     645             : static const AVClass fifo_muxer_class = {
     646             :     .class_name = "Fifo muxer",
     647             :     .item_name  = av_default_item_name,
     648             :     .option     = options,
     649             :     .version    = LIBAVUTIL_VERSION_INT,
     650             : };
     651             : 
     652             : AVOutputFormat ff_fifo_muxer = {
     653             :     .name           = "fifo",
     654             :     .long_name      = NULL_IF_CONFIG_SMALL("FIFO queue pseudo-muxer"),
     655             :     .priv_data_size = sizeof(FifoContext),
     656             :     .init           = fifo_init,
     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,
     663             : };

Generated by: LCOV version 1.12