LCOV - code coverage report
Current view: top level - src/libavformat/tests - fifo_muxer.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 130 196 66.3 %
Date: 2017-09-20 05:29:53 Functions: 11 11 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          50 : static int failing_write_header(AVFormatContext *avf)
      58             : {
      59          50 :     FailingMuxerContext *ctx = avf->priv_data;
      60          50 :     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          50 : static void failing_deinit(AVFormatContext *avf)
     105             : {
     106             :     int i;
     107          50 :     FailingMuxerContext *ctx = avf->priv_data;
     108             : 
     109          50 :     if (!ctx->print_deinit_summary)
     110          48 :         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         112 : static int prepare_packet(AVPacket *pkt,const FailingMuxerPacketData *pkt_data, int64_t pts)
     151             : {
     152             :     int ret;
     153         112 :     FailingMuxerPacketData *data = av_malloc(sizeof(*data));
     154         112 :     if (!data) {
     155           0 :         return AVERROR(ENOMEM);
     156             :     }
     157         112 :     memcpy(data, pkt_data, sizeof(FailingMuxerPacketData));
     158         112 :     ret = av_packet_from_data(pkt, (uint8_t*) data, sizeof(*data));
     159             : 
     160         112 :     pkt->pts = pkt->dts = pts;
     161         112 :     pkt->duration = 1;
     162             : 
     163         112 :     return ret;
     164             : }
     165             : 
     166           5 : static int initialize_fifo_tst_muxer_chain(AVFormatContext **oc)
     167             : {
     168           5 :     int ret = 0;
     169             :     AVStream *s;
     170             : 
     171           5 :     ret = avformat_alloc_output_context2(oc, NULL, "fifo", "-");
     172           5 :     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           5 :     s = avformat_new_stream(*oc, NULL);
     179           5 :     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           5 :     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_write_header_err_tst(AVFormatContext *oc, AVDictionary **opts,
     242             :                                      const FailingMuxerPacketData *pkt_data)
     243             : {
     244           1 :     int ret = 0, i;
     245             :     AVPacket pkt;
     246             : 
     247           1 :     av_init_packet(&pkt);
     248             : 
     249           1 :     ret = avformat_write_header(oc, opts);
     250           1 :     if (ret) {
     251           0 :         fprintf(stderr, "Unexpected write_header failure: %s\n",
     252           0 :                 av_err2str(ret));
     253           0 :         goto fail;
     254             :     }
     255             : 
     256          61 :     for (i = 0; i < MAX_TST_PACKETS; i++ ) {
     257          61 :         ret = prepare_packet(&pkt, pkt_data, i);
     258          61 :         if (ret < 0) {
     259           0 :             fprintf(stderr, "Failed to prepare test packet: %s\n",
     260           0 :                     av_err2str(ret));
     261           0 :             goto write_trailer_and_fail;
     262             :         }
     263          61 :         ret = av_write_frame(oc, &pkt);
     264          61 :         av_packet_unref(&pkt);
     265          61 :         if (ret < 0) {
     266           1 :             break;
     267             :         }
     268             :     }
     269             : 
     270           1 :     if (!ret) {
     271           0 :         fprintf(stderr, "write_packet not failed when supposed to.\n");
     272           0 :         goto fail;
     273           1 :     } else if (ret != -1) {
     274           0 :         fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
     275           0 :         goto fail;
     276             :     }
     277             : 
     278           1 :     ret = av_write_trailer(oc);
     279           1 :     if (ret < 0)
     280           0 :         fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
     281             : 
     282           1 :     return ret;
     283           0 : write_trailer_and_fail:
     284           0 :     av_write_trailer(oc);
     285           0 : fail:
     286           0 :     return ret;
     287             : }
     288             : 
     289           1 : static int fifo_overflow_drop_test(AVFormatContext *oc, AVDictionary **opts,
     290             :                                    const FailingMuxerPacketData *data)
     291             : {
     292           1 :     int ret = 0, i;
     293             :     int64_t write_pkt_start, write_pkt_end, duration;
     294             :     AVPacket pkt;
     295             : 
     296           1 :     av_init_packet(&pkt);
     297             : 
     298           1 :     ret = avformat_write_header(oc, opts);
     299           1 :     if (ret) {
     300           0 :         fprintf(stderr, "Unexpected write_header failure: %s\n",
     301           0 :                 av_err2str(ret));
     302           0 :         return ret;
     303             :     }
     304             : 
     305           1 :     write_pkt_start = av_gettime_relative();
     306           7 :     for (i = 0; i < 6; i++ ) {
     307           6 :         ret = prepare_packet(&pkt, data, i);
     308           6 :         if (ret < 0) {
     309           0 :             fprintf(stderr, "Failed to prepare test packet: %s\n",
     310           0 :                     av_err2str(ret));
     311           0 :             goto fail;
     312             :         }
     313           6 :         ret = av_write_frame(oc, &pkt);
     314           6 :         av_packet_unref(&pkt);
     315           6 :         if (ret < 0) {
     316           0 :             break;
     317             :         }
     318             :     }
     319           1 :     write_pkt_end = av_gettime_relative();
     320           1 :     duration = write_pkt_end - write_pkt_start;
     321           1 :     if (duration > (SLEEPTIME_50_MS*6)/2) {
     322           0 :         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           0 :         ret = AVERROR_BUG;
     325           0 :         goto fail;
     326             :     }
     327             : 
     328           1 :     if (ret) {
     329           0 :         fprintf(stderr, "Unexpected write_packet error: %s\n", av_err2str(ret));
     330           0 :         goto fail;
     331             :     }
     332             : 
     333           1 :     ret = av_write_trailer(oc);
     334           1 :     if (ret < 0)
     335           0 :         fprintf(stderr, "Unexpected write_trailer error: %s\n", av_err2str(ret));
     336             : 
     337           1 :     return ret;
     338           0 : fail:
     339           0 :     av_write_trailer(oc);
     340           0 :     return ret;
     341             : }
     342             : 
     343             : typedef struct TestCase {
     344             :     int (*test_func)(AVFormatContext *, AVDictionary **,const FailingMuxerPacketData *pkt_data);
     345             :     const char *test_name;
     346             :     const char *options;
     347             : 
     348             :     uint8_t print_summary_on_deinit;
     349             :     int write_header_ret;
     350             :     int write_trailer_ret;
     351             : 
     352             :     FailingMuxerPacketData pkt_data;
     353             : } TestCase;
     354             : 
     355             : 
     356             : #define BUFFER_SIZE 64
     357             : 
     358           5 : static int run_test(const TestCase *test)
     359             : {
     360           5 :     AVDictionary *opts = NULL;
     361           5 :     AVFormatContext *oc = NULL;
     362             :     char buffer[BUFFER_SIZE];
     363             :     int ret, ret1;
     364             : 
     365           5 :     ret = initialize_fifo_tst_muxer_chain(&oc);
     366           5 :     if (ret < 0) {
     367           0 :         fprintf(stderr, "Muxer initialization failed: %s\n", av_err2str(ret));
     368           0 :         goto end;
     369             :     }
     370             : 
     371           5 :     if (test->options) {
     372           3 :         ret = av_dict_parse_string(&opts, test->options, "=", ":", 0);
     373           3 :         if (ret < 0) {
     374           0 :             fprintf(stderr, "Failed to parse options: %s\n", av_err2str(ret));
     375           0 :             goto end;
     376             :         }
     377             :     }
     378             : 
     379          10 :     snprintf(buffer, BUFFER_SIZE,
     380             :              "print_deinit_summary=%d:write_header_ret=%d:write_trailer_ret=%d",
     381           5 :              (int)test->print_summary_on_deinit, test->write_header_ret,
     382             :              test->write_trailer_ret);
     383           5 :     ret = av_dict_set(&opts, "format_opts", buffer, 0);
     384           5 :     ret1 = av_dict_set(&opts, "fifo_format", "fail", 0);
     385           5 :     if (ret < 0 || ret1 < 0) {
     386           0 :         fprintf(stderr, "Failed to set options for test muxer: %s\n",
     387           0 :                 av_err2str(ret));
     388           0 :         goto end;
     389             :     }
     390             : 
     391           5 :     ret = test->test_func(oc, &opts, &test->pkt_data);
     392             : 
     393           5 : end:
     394           5 :     printf("%s: %s\n", test->test_name, ret < 0 ? "fail" : "ok");
     395           5 :     avformat_free_context(oc);
     396           5 :     av_dict_free(&opts);
     397           5 :     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           1 : int main(int argc, char *argv[])
     434             : {
     435           1 :     int i, ret, ret_all = 0;
     436             : 
     437           1 :     av_register_all();
     438           1 :     av_register_output_format(&tst_failing_muxer);
     439             : 
     440           6 :     for (i = 0; tests[i].test_func; i++) {
     441           5 :         ret = run_test(&tests[i]);
     442           5 :         if (!ret_all && ret < 0)
     443           0 :             ret_all = ret;
     444             :     }
     445             : 
     446           1 :     return ret;
     447             : }

Generated by: LCOV version 1.13