LCOV - code coverage report
Current view: top level - libavformat - rtpenc_mpegts.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 91 0.0 %
Date: 2017-12-11 04:34:20 Functions: 0 3 0.0 %

          Line data    Source code
       1             : /*
       2             :  * RTP/mpegts muxer
       3             :  * Copyright (c) 2011 Martin Storsjo
       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
       9             :  * License 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 GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/mathematics.h"
      23             : #include "avformat.h"
      24             : #include "avio_internal.h"
      25             : 
      26             : struct MuxChain {
      27             :     AVFormatContext *mpegts_ctx;
      28             :     AVFormatContext *rtp_ctx;
      29             : };
      30             : 
      31           0 : static int rtp_mpegts_write_close(AVFormatContext *s)
      32             : {
      33           0 :     struct MuxChain *chain = s->priv_data;
      34             : 
      35           0 :     if (chain->mpegts_ctx) {
      36           0 :         av_write_trailer(chain->mpegts_ctx);
      37           0 :         ffio_free_dyn_buf(&chain->mpegts_ctx->pb);
      38           0 :         avformat_free_context(chain->mpegts_ctx);
      39             :     }
      40           0 :     if (chain->rtp_ctx) {
      41           0 :         av_write_trailer(chain->rtp_ctx);
      42           0 :         avformat_free_context(chain->rtp_ctx);
      43             :     }
      44           0 :     return 0;
      45             : }
      46             : 
      47           0 : static int rtp_mpegts_write_header(AVFormatContext *s)
      48             : {
      49           0 :     struct MuxChain *chain = s->priv_data;
      50           0 :     AVFormatContext *mpegts_ctx = NULL, *rtp_ctx = NULL;
      51           0 :     AVOutputFormat *mpegts_format = av_guess_format("mpegts", NULL, NULL);
      52           0 :     AVOutputFormat *rtp_format    = av_guess_format("rtp", NULL, NULL);
      53           0 :     int i, ret = AVERROR(ENOMEM);
      54             :     AVStream *st;
      55             : 
      56           0 :     if (!mpegts_format || !rtp_format)
      57           0 :         return AVERROR(ENOSYS);
      58           0 :     mpegts_ctx = avformat_alloc_context();
      59           0 :     if (!mpegts_ctx)
      60           0 :         return AVERROR(ENOMEM);
      61           0 :     mpegts_ctx->oformat   = mpegts_format;
      62           0 :     mpegts_ctx->max_delay = s->max_delay;
      63           0 :     for (i = 0; i < s->nb_streams; i++) {
      64           0 :         AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
      65           0 :         if (!st)
      66           0 :             goto fail;
      67           0 :         st->time_base           = s->streams[i]->time_base;
      68           0 :         st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
      69           0 :         avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
      70             :     }
      71           0 :     if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
      72           0 :         goto fail;
      73           0 :     if ((ret = avformat_write_header(mpegts_ctx, NULL)) < 0)
      74           0 :         goto fail;
      75           0 :     for (i = 0; i < s->nb_streams; i++)
      76           0 :         s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
      77             : 
      78           0 :     chain->mpegts_ctx = mpegts_ctx;
      79           0 :     mpegts_ctx = NULL;
      80             : 
      81           0 :     rtp_ctx = avformat_alloc_context();
      82           0 :     if (!rtp_ctx) {
      83           0 :         ret = AVERROR(ENOMEM);
      84           0 :         goto fail;
      85             :     }
      86           0 :     rtp_ctx->oformat = rtp_format;
      87           0 :     st = avformat_new_stream(rtp_ctx, NULL);
      88           0 :     if (!st) {
      89           0 :         ret = AVERROR(ENOMEM);
      90           0 :         goto fail;
      91             :     }
      92           0 :     st->time_base.num   = 1;
      93           0 :     st->time_base.den   = 90000;
      94           0 :     st->codecpar->codec_id = AV_CODEC_ID_MPEG2TS;
      95           0 :     rtp_ctx->pb = s->pb;
      96           0 :     if ((ret = avformat_write_header(rtp_ctx, NULL)) < 0)
      97           0 :         goto fail;
      98           0 :     chain->rtp_ctx = rtp_ctx;
      99             : 
     100           0 :     return 0;
     101             : 
     102           0 : fail:
     103           0 :     if (mpegts_ctx) {
     104           0 :         ffio_free_dyn_buf(&mpegts_ctx->pb);
     105           0 :         avformat_free_context(mpegts_ctx);
     106             :     }
     107           0 :     if (rtp_ctx)
     108           0 :         avformat_free_context(rtp_ctx);
     109           0 :     rtp_mpegts_write_close(s);
     110           0 :     return ret;
     111             : }
     112             : 
     113           0 : static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
     114             : {
     115           0 :     struct MuxChain *chain = s->priv_data;
     116           0 :     int ret = 0, size;
     117             :     uint8_t *buf;
     118             :     AVPacket local_pkt;
     119             : 
     120           0 :     if (!chain->mpegts_ctx->pb) {
     121           0 :         if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
     122           0 :             return ret;
     123             :     }
     124           0 :     if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
     125           0 :         return ret;
     126           0 :     size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
     127           0 :     chain->mpegts_ctx->pb = NULL;
     128           0 :     if (size == 0) {
     129           0 :         av_free(buf);
     130           0 :         return 0;
     131             :     }
     132           0 :     av_init_packet(&local_pkt);
     133           0 :     local_pkt.data         = buf;
     134           0 :     local_pkt.size         = size;
     135           0 :     local_pkt.stream_index = 0;
     136           0 :     if (pkt->pts != AV_NOPTS_VALUE)
     137           0 :         local_pkt.pts = av_rescale_q(pkt->pts,
     138           0 :                                      s->streams[pkt->stream_index]->time_base,
     139           0 :                                      chain->rtp_ctx->streams[0]->time_base);
     140           0 :     if (pkt->dts != AV_NOPTS_VALUE)
     141           0 :         local_pkt.dts = av_rescale_q(pkt->dts,
     142           0 :                                      s->streams[pkt->stream_index]->time_base,
     143           0 :                                      chain->rtp_ctx->streams[0]->time_base);
     144           0 :     ret = av_write_frame(chain->rtp_ctx, &local_pkt);
     145           0 :     av_free(buf);
     146             : 
     147           0 :     return ret;
     148             : }
     149             : 
     150             : AVOutputFormat ff_rtp_mpegts_muxer = {
     151             :     .name              = "rtp_mpegts",
     152             :     .long_name         = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
     153             :     .priv_data_size    = sizeof(struct MuxChain),
     154             :     .audio_codec       = AV_CODEC_ID_AAC,
     155             :     .video_codec       = AV_CODEC_ID_MPEG4,
     156             :     .write_header      = rtp_mpegts_write_header,
     157             :     .write_packet      = rtp_mpegts_write_packet,
     158             :     .write_trailer     = rtp_mpegts_write_close,
     159             : };

Generated by: LCOV version 1.13