LCOV - code coverage report
Current view: top level - libavformat/tests - fifo_muxer.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 113 164 68.9 %
Date: 2017-12-14 19:11:59 Functions: 10 10 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 <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 */
      42             : } FailingMuxerPacketData;
      43             : 
      44             : 
      45             : typedef struct FailingMuxerContext {
      46             :     AVClass *class;
      47             :     int write_header_ret;
      48             :     int write_trailer_ret;
      49             :     /* If non-zero, summary of processed packets will be printed in deinit */
      50             :     int print_deinit_summary;
      51             : 
      52             :     int flush_count;
      53             :     int pts_written[MAX_TST_PACKETS];
      54             :     int pts_written_nr;
      55             : } FailingMuxerContext;
      56             : 
      57          49 : static int failing_write_header(AVFormatContext *avf)
      58             : {
      59          49 :     FailingMuxerContext *ctx = avf->priv_data;
      60          49 :     return ctx->write_header_ret;
      61             : }
      62             : 
      63          93 : static int failing_write_packet(AVFormatContext *avf, AVPacket *pkt)
      64             : {
      65          93 :     FailingMuxerContext *ctx = avf->priv_data;
      66          93 :     int ret = 0;
      67          93 :     if (!pkt) {
      68           3 :         ctx->flush_count++;
      69             :     } else {
      70          90 :         FailingMuxerPacketData *data = (FailingMuxerPacketData*) pkt->data;
      71             : 
      72          90 :         if (!data->recover_after) {
      73          45 :             data->ret = 0;
      74             :         } else {
      75          45 :             data->recover_after--;
      76             :         }
      77             : 
      78          90 :         ret = data->ret;
      79             : 
      80          90 :         if (data->sleep_time) {
      81          15 :             int64_t slept = 0;
      82          45 :             while (slept < data->sleep_time) {
      83          15 :                 if (ff_check_interrupt(&avf->interrupt_callback))
      84           0 :                     return AVERROR_EXIT;
      85          15 :                 av_usleep(SLEEPTIME_10_MS);
      86          15 :                 slept += SLEEPTIME_10_MS;
      87             :             }
      88             :         }
      89             : 
      90          90 :         if (!ret) {
      91          45 :             ctx->pts_written[ctx->pts_written_nr++] = pkt->pts;
      92          45 :             av_packet_unref(pkt);
      93             :         }
      94             :     }
      95          93 :     return ret;
      96             : }
      97             : 
      98          49 : static int failing_write_trailer(AVFormatContext *avf)
      99             : {
     100          49 :     FailingMuxerContext *ctx = avf->priv_data;
     101          49 :     return ctx->write_trailer_ret;
     102             : }
     103             : 
     104          49 : static void failing_deinit(AVFormatContext *avf)
     105             : {
     106             :     int i;
     107          49 :     FailingMuxerContext *ctx = avf->priv_data;
     108             : 
     109          49 :     if (!ctx->print_deinit_summary)
     110          47 :         return;
     111             : 
     112           2 :     printf("flush count: %d\n", ctx->flush_count);
     113           2 :     printf("pts seen nr: %d\n", ctx->pts_written_nr);
     114           2 :     printf("pts seen: ");
     115          32 :     for (i = 0; i < ctx->pts_written_nr; ++i ) {
     116          30 :         printf(i ? ",%d" : "%d", ctx->pts_written[i]);
     117             :     }
     118           2 :     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             : 
     138             : AVOutputFormat tst_failing_muxer = {
     139             :     .name           = "fail",
     140             :     .long_name      = NULL_IF_CONFIG_SMALL("Failing test muxer"),
     141             :     .priv_data_size = sizeof(FailingMuxerContext),
     142             :     .write_header   = failing_write_header,
     143             :     .write_packet   = failing_write_packet,
     144             :     .write_trailer  = failing_write_trailer,
     145             :     .deinit         = failing_deinit,
     146             :     .priv_class     = &failing_muxer_class,
     147             :     .flags          = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
     148             : };
     149             : 
     150          51 : static int prepare_packet(AVPacket *pkt,const FailingMuxerPacketData *pkt_data, int64_t pts)
     151             : {
     152             :     int ret;
     153          51 :     FailingMuxerPacketData *data = av_malloc(sizeof(*data));
     154          51 :     if (!data) {
     155           0 :         return AVERROR(ENOMEM);
     156             :     }
     157          51 :     memcpy(data, pkt_data, sizeof(FailingMuxerPacketData));
     158          51 :     ret = av_packet_from_data(pkt, (uint8_t*) data, sizeof(*data));
     159             : 
     160          51 :     pkt->pts = pkt->dts = pts;
     161          51 :     pkt->duration = 1;
     162             : 
     163          51 :     return ret;
     164             : }
     165             : 
     166           4 : static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc)
     167             : {
     168           4 :     int ret = 0;
     169             :     AVStream *s;
     170             : 
     171           4 :     ret = avformat_alloc_output_context2(oc, NULL, "fifo", "-");
     172           4 :     if (ret) {
     173           0 :         fprintf(stderr, "Failed to create format context: %s\n",
     174           0 :                 av_err2str(ret));
     175           0 :         return EXIT_FAILURE;
     176             :     }
     177             : 
     178           4 :     s = avformat_new_stream(*oc, NULL);
     179           4 :     if (!s) {
     180           0 :         fprintf(stderr, "Failed to create stream: %s\n",
     181           0 :                 av_err2str(ret));
     182           0 :         ret = AVERROR(ENOMEM);
     183             :     }
     184             : 
     185           4 :     return ret;
     186             : }
     187             : 
     188           3 : static int fifo_basic_test(AVFormatContext *oc, AVDictionary **opts,
     189             :                              const FailingMuxerPacketData *pkt_data)
     190             : {
     191           3 :     int ret = 0, i;
     192             :     AVPacket pkt;
     193             : 
     194           3 :     av_init_packet(&pkt);
     195             : 
     196             : 
     197           3 :     ret = avformat_write_header(oc, opts);
     198           3 :     if (ret) {
     199           0 :         fprintf(stderr, "Unexpected write_header failure: %s\n",
     200           0 :                 av_err2str(ret));
     201           0 :         goto fail;
     202             :     }
     203             : 
     204          48 :     for (i = 0; i < 15; i++ ) {
     205          45 :         ret = prepare_packet(&pkt, pkt_data, i);
     206          45 :         if (ret < 0) {
     207           0 :             fprintf(stderr, "Failed to prepare test packet: %s\n",
     208           0 :                     av_err2str(ret));
     209           0 :             goto write_trailer_and_fail;
     210             :         }
     211          45 :         ret = av_write_frame(oc, &pkt);
     212          45 :         av_packet_unref(&pkt);
     213          45 :         if (ret < 0) {
     214           0 :             fprintf(stderr, "Unexpected write_frame error: %s\n",
     215           0 :                     av_err2str(ret));
     216           0 :             goto write_trailer_and_fail;
     217             :         }
     218             :     }
     219             : 
     220           3 :     ret = av_write_frame(oc, NULL);
     221           3 :     if (ret < 0) {
     222           0 :         fprintf(stderr, "Unexpected write_frame error during flushing: %s\n",
     223           0 :                 av_err2str(ret));
     224           0 :         goto write_trailer_and_fail;
     225             :     }
     226             : 
     227           3 :     ret = av_write_trailer(oc);
     228           3 :     if (ret < 0) {
     229           0 :         fprintf(stderr, "Unexpected write_trailer error during flushing: %s\n",
     230           0 :                 av_err2str(ret));
     231           0 :         goto fail;
     232             :     }
     233             : 
     234           3 :     return ret;
     235           0 : write_trailer_and_fail:
     236           0 :     av_write_trailer(oc);
     237           0 : fail:
     238           0 :     return ret;
     239             : }
     240             : 
     241           1 : static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
     242             :                                    const FailingMuxerPacketData *data)
     243             : {
     244           1 :     int ret = 0, i;
     245             :     int64_t write_pkt_start, write_pkt_end, duration;
     246             :     AVPacket pkt;
     247             : 
     248           1 :     av_init_packet(&pkt);
     249             : 
     250           1 :     ret = avformat_write_header(oc, opts);
     251           1 :     if (ret) {
     252           0 :         fprintf(stderr, "Unexpected write_header failure: %s\n",
     253           0 :                 av_err2str(ret));
     254           0 :         return ret;
     255             :     }
     256             : 
     257           1 :     write_pkt_start = av_gettime_relative();
     258           7 :     for (i = 0; i < 6; i++ ) {
     259           6 :         ret = prepare_packet(&pkt, data, i);
     260           6 :         if (ret < 0) {
     261           0 :             fprintf(stderr, "Failed to prepare test packet: %s\n",
     262           0 :                     av_err2str(ret));
     263           0 :             goto fail;
     264             :         }
     265           6 :         ret = av_write_frame(oc, &pkt);
     266           6 :         av_packet_unref(&pkt);
     267           6 :         if (ret < 0) {
     268           0 :             break;
     269             :         }
     270             :     }
     271           1 :     write_pkt_end = av_gettime_relative();
     272           1 :     duration = write_pkt_end - write_pkt_start;
     273           1 :     if (duration > (SLEEPTIME_50_MS*6)/2) {
     274           0 :         fprintf(stderr, "Writing packets to fifo muxer took too much time while testing"
     275             :                         "buffer overflow with drop_pkts_on_overflow was on.\n");
     276           0 :         ret = AVERROR_BUG;
     277           0 :         goto fail;
     278             :     }
     279             : 
     280           1 :     if (ret) {
     281           0 :         fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
     282           0 :         goto fail;
     283             :     }
     284             : 
     285           1 :     ret = av_write_trailer(oc);
     286           1 :     if (ret < 0)
     287           0 :         fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
     288             : 
     289           1 :     return ret;
     290           0 : fail:
     291           0 :     av_write_trailer(oc);
     292           0 :     return ret;
     293             : }
     294             : 
     295             : typedef struct TestCase {
     296             :     int (*test_func)(AVFormatContext *, AVDictionary **,const FailingMuxerPacketData *pkt_data);
     297             :     const char *test_name;
     298             :     const char *options;
     299             : 
     300             :     uint8_t print_summary_on_deinit;
     301             :     int write_header_ret;
     302             :     int write_trailer_ret;
     303             : 
     304             :     FailingMuxerPacketData pkt_data;
     305             : } TestCase;
     306             : 
     307             : 
     308             : #define BUFFER_SIZE 64
     309             : 
     310           4 : static int run_test(const TestCase *test)
     311             : {
     312           4 :     AVDictionary *opts = NULL;
     313           4 :     AVFormatContext *oc = NULL;
     314             :     char buffer[BUFFER_SIZE];
     315             :     int ret, ret1;
     316             : 
     317           4 :     ret = initialize_fifo_tst_muxer_chain(&oc);
     318           4 :     if (ret < 0) {
     319           0 :         fprintf(stderr, "Muxer initialization failed: %s\n", av_err2str(ret));
     320           0 :         goto end;
     321             :     }
     322             : 
     323           4 :     if (test->options) {
     324           3 :         ret = av_dict_parse_string(&opts, test->options, "=", ":", 0);
     325           3 :         if (ret < 0) {
     326           0 :             fprintf(stderr, "Failed to parse options: %s\n", av_err2str(ret));
     327           0 :             goto end;
     328             :         }
     329             :     }
     330             : 
     331           8 :     snprintf(buffer, BUFFER_SIZE,
     332             :              "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
     333           4 :              (int)test->print_summary_on_deinit, test->write_header_ret,
     334             :              test->write_trailer_ret);
     335           4 :     ret = av_dict_set(&opts, "format_opts", buffer, 0);
     336           4 :     ret1 = av_dict_set(&opts, "fifo_format", "fail", 0);
     337           4 :     if (ret < 0 || ret1 < 0) {
     338           0 :         fprintf(stderr, "Failed to set options for test muxer: %s\n",
     339           0 :                 av_err2str(ret));
     340           0 :         goto end;
     341             :     }
     342             : 
     343           4 :     ret = test->test_func(oc, &opts, &test->pkt_data);
     344             : 
     345           4 : end:
     346           4 :     printf("%s: %s\n", test->test_name, ret < 0 ? "fail" : "ok");
     347           4 :     avformat_free_context(oc);
     348           4 :     av_dict_free(&opts);
     349           4 :     return ret;
     350             : }
     351             : 
     352             : 
     353             : const TestCase tests[] = {
     354             :         /* Simple test in packet-non-dropping mode, we expect to get on the output
     355             :          * exactly what was on input */
     356             :         {fifo_basic_test, "nonfail test", NULL,1, 0, 0, {0, 0, 0}},
     357             : 
     358             :         /* Each write_packet will fail 3 times before operation is successful. If recovery
     359             :          * Since recovery is on, fifo muxer should not return any errors. */
     360             :         {fifo_basic_test, "recovery test", "attempt_recovery=1:recovery_wait_time=0",
     361             :          0, 0, 0, {AVERROR(ETIMEDOUT), 3, 0}},
     362             : 
     363             :         /* By setting low queue_size and sending packets with longer processing time,
     364             :          * this test will cause queue to overflow, since drop_pkts_on_overflow is off
     365             :          * by default, all packets should be processed and fifo should block on full
     366             :          * queue. */
     367             :         {fifo_basic_test, "overflow without packet dropping","queue_size=3",
     368             :          1, 0, 0, {0, 0, SLEEPTIME_10_MS}},
     369             : 
     370             :         /* The test as the upper one, except that drop_on_overflow is turned on. In this case
     371             :          * fifo should not block when the queue is full and slow down producer, so the test
     372             :          * measures time producer spends on write_packet calls which should be significantly
     373             :          * less than number_of_pkts * 50 MS.
     374             :          */
     375             :         {fifo_overflow_drop_test, "overflow with packet dropping", "queue_size=3:drop_pkts_on_overflow=1",
     376             :          0, 0, 0, {0, 0, SLEEPTIME_50_MS}},
     377             : 
     378             :         {NULL}
     379             : };
     380             : 
     381           1 : int main(int argc, char *argv[])
     382             : {
     383           1 :     int i, ret, ret_all = 0;
     384             : 
     385           1 :     av_register_all();
     386           1 :     av_register_output_format(&tst_failing_muxer);
     387             : 
     388           5 :     for (i = 0; tests[i].test_func; i++) {
     389           4 :         ret = run_test(&tests[i]);
     390           4 :         if (!ret_all && ret < 0)
     391           0 :             ret_all = ret;
     392             :     }
     393             : 
     394           1 :     return ret;
     395             : }

Generated by: LCOV version 1.13