LCOV - code coverage report
Current view: top level - libavformat - rtpenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 124 370 33.5 %
Date: 2017-12-16 21:16:39 Functions: 8 11 72.7 %

          Line data    Source code
       1             : /*
       2             :  * RTP output format
       3             :  * Copyright (c) 2002 Fabrice Bellard
       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 "avformat.h"
      23             : #include "mpegts.h"
      24             : #include "internal.h"
      25             : #include "libavutil/mathematics.h"
      26             : #include "libavutil/random_seed.h"
      27             : #include "libavutil/opt.h"
      28             : 
      29             : #include "rtpenc.h"
      30             : 
      31             : static const AVOption options[] = {
      32             :     FF_RTP_FLAG_OPTS(RTPMuxContext, flags),
      33             :     { "payload_type", "Specify RTP payload type", offsetof(RTPMuxContext, payload_type), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, 127, AV_OPT_FLAG_ENCODING_PARAM },
      34             :     { "ssrc", "Stream identifier", offsetof(RTPMuxContext, ssrc), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
      35             :     { "cname", "CNAME to include in RTCP SR packets", offsetof(RTPMuxContext, cname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
      36             :     { "seq", "Starting sequence number", offsetof(RTPMuxContext, seq), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, AV_OPT_FLAG_ENCODING_PARAM },
      37             :     { NULL },
      38             : };
      39             : 
      40             : static const AVClass rtp_muxer_class = {
      41             :     .class_name = "RTP muxer",
      42             :     .item_name  = av_default_item_name,
      43             :     .option     = options,
      44             :     .version    = LIBAVUTIL_VERSION_INT,
      45             : };
      46             : 
      47             : #define RTCP_SR_SIZE 28
      48             : 
      49           2 : static int is_supported(enum AVCodecID id)
      50             : {
      51           2 :     switch(id) {
      52           2 :     case AV_CODEC_ID_DIRAC:
      53             :     case AV_CODEC_ID_H261:
      54             :     case AV_CODEC_ID_H263:
      55             :     case AV_CODEC_ID_H263P:
      56             :     case AV_CODEC_ID_H264:
      57             :     case AV_CODEC_ID_HEVC:
      58             :     case AV_CODEC_ID_MPEG1VIDEO:
      59             :     case AV_CODEC_ID_MPEG2VIDEO:
      60             :     case AV_CODEC_ID_MPEG4:
      61             :     case AV_CODEC_ID_AAC:
      62             :     case AV_CODEC_ID_MP2:
      63             :     case AV_CODEC_ID_MP3:
      64             :     case AV_CODEC_ID_PCM_ALAW:
      65             :     case AV_CODEC_ID_PCM_MULAW:
      66             :     case AV_CODEC_ID_PCM_S8:
      67             :     case AV_CODEC_ID_PCM_S16BE:
      68             :     case AV_CODEC_ID_PCM_S16LE:
      69             :     case AV_CODEC_ID_PCM_S24BE:
      70             :     case AV_CODEC_ID_PCM_U16BE:
      71             :     case AV_CODEC_ID_PCM_U16LE:
      72             :     case AV_CODEC_ID_PCM_U8:
      73             :     case AV_CODEC_ID_MPEG2TS:
      74             :     case AV_CODEC_ID_AMR_NB:
      75             :     case AV_CODEC_ID_AMR_WB:
      76             :     case AV_CODEC_ID_VORBIS:
      77             :     case AV_CODEC_ID_THEORA:
      78             :     case AV_CODEC_ID_VP8:
      79             :     case AV_CODEC_ID_VP9:
      80             :     case AV_CODEC_ID_ADPCM_G722:
      81             :     case AV_CODEC_ID_ADPCM_G726:
      82             :     case AV_CODEC_ID_ADPCM_G726LE:
      83             :     case AV_CODEC_ID_ILBC:
      84             :     case AV_CODEC_ID_MJPEG:
      85             :     case AV_CODEC_ID_SPEEX:
      86             :     case AV_CODEC_ID_OPUS:
      87           2 :         return 1;
      88           0 :     default:
      89           0 :         return 0;
      90             :     }
      91             : }
      92             : 
      93           2 : static int rtp_write_header(AVFormatContext *s1)
      94             : {
      95           2 :     RTPMuxContext *s = s1->priv_data;
      96           2 :     int n, ret = AVERROR(EINVAL);
      97             :     AVStream *st;
      98             : 
      99           2 :     if (s1->nb_streams != 1) {
     100           0 :         av_log(s1, AV_LOG_ERROR, "Only one stream supported in the RTP muxer\n");
     101           0 :         return AVERROR(EINVAL);
     102             :     }
     103           2 :     st = s1->streams[0];
     104           2 :     if (!is_supported(st->codecpar->codec_id)) {
     105           0 :         av_log(s1, AV_LOG_ERROR, "Unsupported codec %s\n", avcodec_get_name(st->codecpar->codec_id));
     106             : 
     107           0 :         return -1;
     108             :     }
     109             : 
     110           2 :     if (s->payload_type < 0) {
     111             :         /* Re-validate non-dynamic payload types */
     112           2 :         if (st->id < RTP_PT_PRIVATE)
     113           0 :             st->id = ff_rtp_get_payload_type(s1, st->codecpar, -1);
     114             : 
     115           2 :         s->payload_type = st->id;
     116             :     } else {
     117             :         /* private option takes priority */
     118           0 :         st->id = s->payload_type;
     119             :     }
     120             : 
     121           2 :     s->base_timestamp = av_get_random_seed();
     122           2 :     s->timestamp = s->base_timestamp;
     123           2 :     s->cur_timestamp = 0;
     124           2 :     if (!s->ssrc)
     125           2 :         s->ssrc = av_get_random_seed();
     126           2 :     s->first_packet = 1;
     127           2 :     s->first_rtcp_ntp_time = ff_ntp_time();
     128           2 :     if (s1->start_time_realtime != 0  &&  s1->start_time_realtime != AV_NOPTS_VALUE)
     129             :         /* Round the NTP time to whole milliseconds. */
     130           0 :         s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
     131             :                                  NTP_OFFSET_US;
     132             :     // Pick a random sequence start number, but in the lower end of the
     133             :     // available range, so that any wraparound doesn't happen immediately.
     134             :     // (Immediate wraparound would be an issue for SRTP.)
     135           2 :     if (s->seq < 0) {
     136           2 :         if (s1->flags & AVFMT_FLAG_BITEXACT) {
     137           2 :             s->seq = 0;
     138             :         } else
     139           0 :             s->seq = av_get_random_seed() & 0x0fff;
     140             :     } else
     141           0 :         s->seq &= 0xffff; // Use the given parameter, wrapped to the right interval
     142             : 
     143           2 :     if (s1->packet_size) {
     144           0 :         if (s1->pb->max_packet_size)
     145           0 :             s1->packet_size = FFMIN(s1->packet_size,
     146             :                                     s1->pb->max_packet_size);
     147             :     } else
     148           2 :         s1->packet_size = s1->pb->max_packet_size;
     149           2 :     if (s1->packet_size <= 12) {
     150           0 :         av_log(s1, AV_LOG_ERROR, "Max packet size %u too low\n", s1->packet_size);
     151           0 :         return AVERROR(EIO);
     152             :     }
     153           2 :     s->buf = av_malloc(s1->packet_size);
     154           2 :     if (!s->buf) {
     155           0 :         return AVERROR(ENOMEM);
     156             :     }
     157           2 :     s->max_payload_size = s1->packet_size - 12;
     158             : 
     159           2 :     if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
     160           1 :         avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate);
     161             :     } else {
     162           1 :         avpriv_set_pts_info(st, 32, 1, 90000);
     163             :     }
     164           2 :     s->buf_ptr = s->buf;
     165           2 :     switch(st->codecpar->codec_id) {
     166           0 :     case AV_CODEC_ID_MP2:
     167             :     case AV_CODEC_ID_MP3:
     168           0 :         s->buf_ptr = s->buf + 4;
     169           0 :         avpriv_set_pts_info(st, 32, 1, 90000);
     170           0 :         break;
     171           0 :     case AV_CODEC_ID_MPEG1VIDEO:
     172             :     case AV_CODEC_ID_MPEG2VIDEO:
     173           0 :         break;
     174           0 :     case AV_CODEC_ID_MPEG2TS:
     175           0 :         n = s->max_payload_size / TS_PACKET_SIZE;
     176           0 :         if (n < 1)
     177           0 :             n = 1;
     178           0 :         s->max_payload_size = n * TS_PACKET_SIZE;
     179           0 :         break;
     180           0 :     case AV_CODEC_ID_DIRAC:
     181           0 :         if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
     182           0 :             av_log(s, AV_LOG_ERROR,
     183             :                    "Packetizing VC-2 is experimental and does not use all values "
     184             :                    "of the specification "
     185             :                    "(even though most receivers may handle it just fine). "
     186             :                    "Please set -strict experimental in order to enable it.\n");
     187           0 :             ret = AVERROR_EXPERIMENTAL;
     188           0 :             goto fail;
     189             :         }
     190           0 :         break;
     191           0 :     case AV_CODEC_ID_H261:
     192           0 :         if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
     193           0 :             av_log(s, AV_LOG_ERROR,
     194             :                    "Packetizing H.261 is experimental and produces incorrect "
     195             :                    "packetization for cases where GOBs don't fit into packets "
     196             :                    "(even though most receivers may handle it just fine). "
     197             :                    "Please set -f_strict experimental in order to enable it.\n");
     198           0 :             ret = AVERROR_EXPERIMENTAL;
     199           0 :             goto fail;
     200             :         }
     201           0 :         break;
     202           0 :     case AV_CODEC_ID_H264:
     203             :         /* check for H.264 MP4 syntax */
     204           0 :         if (st->codecpar->extradata_size > 4 && st->codecpar->extradata[0] == 1) {
     205           0 :             s->nal_length_size = (st->codecpar->extradata[4] & 0x03) + 1;
     206             :         }
     207           0 :         break;
     208           0 :     case AV_CODEC_ID_HEVC:
     209             :         /* Only check for the standardized hvcC version of extradata, keeping
     210             :          * things simple and similar to the avcC/H.264 case above, instead
     211             :          * of trying to handle the pre-standardization versions (as in
     212             :          * libavcodec/hevc.c). */
     213           0 :         if (st->codecpar->extradata_size > 21 && st->codecpar->extradata[0] == 1) {
     214           0 :             s->nal_length_size = (st->codecpar->extradata[21] & 0x03) + 1;
     215             :         }
     216           0 :         break;
     217           0 :     case AV_CODEC_ID_VP9:
     218           0 :         if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
     219           0 :             av_log(s, AV_LOG_ERROR,
     220             :                    "Packetizing VP9 is experimental and its specification is "
     221             :                    "still in draft state. "
     222             :                    "Please set -strict experimental in order to enable it.\n");
     223           0 :             ret = AVERROR_EXPERIMENTAL;
     224           0 :             goto fail;
     225             :         }
     226           0 :         break;
     227           0 :     case AV_CODEC_ID_VORBIS:
     228             :     case AV_CODEC_ID_THEORA:
     229           0 :         s->max_frames_per_packet = 15;
     230           0 :         break;
     231           0 :     case AV_CODEC_ID_ADPCM_G722:
     232             :         /* Due to a historical error, the clock rate for G722 in RTP is
     233             :          * 8000, even if the sample rate is 16000. See RFC 3551. */
     234           0 :         avpriv_set_pts_info(st, 32, 1, 8000);
     235           0 :         break;
     236           0 :     case AV_CODEC_ID_OPUS:
     237           0 :         if (st->codecpar->channels > 2) {
     238           0 :             av_log(s1, AV_LOG_ERROR, "Multistream opus not supported in RTP\n");
     239           0 :             goto fail;
     240             :         }
     241             :         /* The opus RTP RFC says that all opus streams should use 48000 Hz
     242             :          * as clock rate, since all opus sample rates can be expressed in
     243             :          * this clock rate, and sample rate changes on the fly are supported. */
     244           0 :         avpriv_set_pts_info(st, 32, 1, 48000);
     245           0 :         break;
     246           0 :     case AV_CODEC_ID_ILBC:
     247           0 :         if (st->codecpar->block_align != 38 && st->codecpar->block_align != 50) {
     248           0 :             av_log(s1, AV_LOG_ERROR, "Incorrect iLBC block size specified\n");
     249           0 :             goto fail;
     250             :         }
     251           0 :         s->max_frames_per_packet = s->max_payload_size / st->codecpar->block_align;
     252           0 :         break;
     253           0 :     case AV_CODEC_ID_AMR_NB:
     254             :     case AV_CODEC_ID_AMR_WB:
     255           0 :         s->max_frames_per_packet = 50;
     256           0 :         if (st->codecpar->codec_id == AV_CODEC_ID_AMR_NB)
     257           0 :             n = 31;
     258             :         else
     259           0 :             n = 61;
     260             :         /* max_header_toc_size + the largest AMR payload must fit */
     261           0 :         if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
     262           0 :             av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
     263           0 :             goto fail;
     264             :         }
     265           0 :         if (st->codecpar->channels != 1) {
     266           0 :             av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
     267           0 :             goto fail;
     268             :         }
     269           0 :         break;
     270           0 :     case AV_CODEC_ID_AAC:
     271           0 :         s->max_frames_per_packet = 50;
     272           0 :         break;
     273           2 :     default:
     274           2 :         break;
     275             :     }
     276             : 
     277           2 :     return 0;
     278             : 
     279           0 : fail:
     280           0 :     av_freep(&s->buf);
     281           0 :     return ret;
     282             : }
     283             : 
     284             : /* send an rtcp sender report packet */
     285           2 : static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time, int bye)
     286             : {
     287           2 :     RTPMuxContext *s = s1->priv_data;
     288             :     uint32_t rtp_ts;
     289             : 
     290           2 :     av_log(s1, AV_LOG_TRACE, "RTCP: %02x %"PRIx64" %"PRIx32"\n", s->payload_type, ntp_time, s->timestamp);
     291             : 
     292           2 :     s->last_rtcp_ntp_time = ntp_time;
     293           4 :     rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
     294           4 :                           s1->streams[0]->time_base) + s->base_timestamp;
     295           2 :     avio_w8(s1->pb, RTP_VERSION << 6);
     296           2 :     avio_w8(s1->pb, RTCP_SR);
     297           2 :     avio_wb16(s1->pb, 6); /* length in words - 1 */
     298           2 :     avio_wb32(s1->pb, s->ssrc);
     299           2 :     avio_wb32(s1->pb, ntp_time / 1000000);
     300           2 :     avio_wb32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
     301           2 :     avio_wb32(s1->pb, rtp_ts);
     302           2 :     avio_wb32(s1->pb, s->packet_count);
     303           2 :     avio_wb32(s1->pb, s->octet_count);
     304             : 
     305           2 :     if (s->cname) {
     306           0 :         int len = FFMIN(strlen(s->cname), 255);
     307           0 :         avio_w8(s1->pb, (RTP_VERSION << 6) + 1);
     308           0 :         avio_w8(s1->pb, RTCP_SDES);
     309           0 :         avio_wb16(s1->pb, (7 + len + 3) / 4); /* length in words - 1 */
     310             : 
     311           0 :         avio_wb32(s1->pb, s->ssrc);
     312           0 :         avio_w8(s1->pb, 0x01); /* CNAME */
     313           0 :         avio_w8(s1->pb, len);
     314           0 :         avio_write(s1->pb, s->cname, len);
     315           0 :         avio_w8(s1->pb, 0); /* END */
     316           0 :         for (len = (7 + len) % 4; len % 4; len++)
     317           0 :             avio_w8(s1->pb, 0);
     318             :     }
     319             : 
     320           2 :     if (bye) {
     321           0 :         avio_w8(s1->pb, (RTP_VERSION << 6) | 1);
     322           0 :         avio_w8(s1->pb, RTCP_BYE);
     323           0 :         avio_wb16(s1->pb, 1); /* length in words - 1 */
     324           0 :         avio_wb32(s1->pb, s->ssrc);
     325             :     }
     326             : 
     327           2 :     avio_flush(s1->pb);
     328           2 : }
     329             : 
     330             : /* send an rtp packet. sequence number is incremented, but the caller
     331             :    must update the timestamp itself */
     332         274 : void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
     333             : {
     334         274 :     RTPMuxContext *s = s1->priv_data;
     335             : 
     336         274 :     av_log(s1, AV_LOG_TRACE, "rtp_send_data size=%d\n", len);
     337             : 
     338             :     /* build the RTP header */
     339         274 :     avio_w8(s1->pb, RTP_VERSION << 6);
     340         274 :     avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
     341         274 :     avio_wb16(s1->pb, s->seq);
     342         274 :     avio_wb32(s1->pb, s->timestamp);
     343         274 :     avio_wb32(s1->pb, s->ssrc);
     344             : 
     345         274 :     avio_write(s1->pb, buf1, len);
     346         274 :     avio_flush(s1->pb);
     347             : 
     348         274 :     s->seq = (s->seq + 1) & 0xffff;
     349         274 :     s->octet_count += len;
     350         274 :     s->packet_count++;
     351         274 : }
     352             : 
     353             : /* send an integer number of samples and compute time stamp and fill
     354             :    the rtp send buffer before sending. */
     355          44 : static int rtp_send_samples(AVFormatContext *s1,
     356             :                             const uint8_t *buf1, int size, int sample_size_bits)
     357             : {
     358          44 :     RTPMuxContext *s = s1->priv_data;
     359             :     int len, max_packet_size, n;
     360             :     /* Calculate the number of bytes to get samples aligned on a byte border */
     361          44 :     int aligned_samples_size = sample_size_bits/av_gcd(sample_size_bits, 8);
     362             : 
     363          44 :     max_packet_size = (s->max_payload_size / aligned_samples_size) * aligned_samples_size;
     364             :     /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
     365          44 :     if ((sample_size_bits % 8) == 0 && ((8 * size) % sample_size_bits) != 0)
     366           0 :         return AVERROR(EINVAL);
     367          44 :     n = 0;
     368         132 :     while (size > 0) {
     369          44 :         s->buf_ptr = s->buf;
     370          44 :         len = FFMIN(max_packet_size, size);
     371             : 
     372             :         /* copy data */
     373          44 :         memcpy(s->buf_ptr, buf1, len);
     374          44 :         s->buf_ptr += len;
     375          44 :         buf1 += len;
     376          44 :         size -= len;
     377          44 :         s->timestamp = s->cur_timestamp + n * 8 / sample_size_bits;
     378          44 :         ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
     379          44 :         n += (s->buf_ptr - s->buf);
     380             :     }
     381          44 :     return 0;
     382             : }
     383             : 
     384           0 : static void rtp_send_mpegaudio(AVFormatContext *s1,
     385             :                                const uint8_t *buf1, int size)
     386             : {
     387           0 :     RTPMuxContext *s = s1->priv_data;
     388             :     int len, count, max_packet_size;
     389             : 
     390           0 :     max_packet_size = s->max_payload_size;
     391             : 
     392             :     /* test if we must flush because not enough space */
     393           0 :     len = (s->buf_ptr - s->buf);
     394           0 :     if ((len + size) > max_packet_size) {
     395           0 :         if (len > 4) {
     396           0 :             ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
     397           0 :             s->buf_ptr = s->buf + 4;
     398             :         }
     399             :     }
     400           0 :     if (s->buf_ptr == s->buf + 4) {
     401           0 :         s->timestamp = s->cur_timestamp;
     402             :     }
     403             : 
     404             :     /* add the packet */
     405           0 :     if (size > max_packet_size) {
     406             :         /* big packet: fragment */
     407           0 :         count = 0;
     408           0 :         while (size > 0) {
     409           0 :             len = max_packet_size - 4;
     410           0 :             if (len > size)
     411           0 :                 len = size;
     412             :             /* build fragmented packet */
     413           0 :             s->buf[0] = 0;
     414           0 :             s->buf[1] = 0;
     415           0 :             s->buf[2] = count >> 8;
     416           0 :             s->buf[3] = count;
     417           0 :             memcpy(s->buf + 4, buf1, len);
     418           0 :             ff_rtp_send_data(s1, s->buf, len + 4, 0);
     419           0 :             size -= len;
     420           0 :             buf1 += len;
     421           0 :             count += len;
     422             :         }
     423             :     } else {
     424           0 :         if (s->buf_ptr == s->buf + 4) {
     425             :             /* no fragmentation possible */
     426           0 :             s->buf[0] = 0;
     427           0 :             s->buf[1] = 0;
     428           0 :             s->buf[2] = 0;
     429           0 :             s->buf[3] = 0;
     430             :         }
     431           0 :         memcpy(s->buf_ptr, buf1, size);
     432           0 :         s->buf_ptr += size;
     433             :     }
     434           0 : }
     435             : 
     436          25 : static void rtp_send_raw(AVFormatContext *s1,
     437             :                          const uint8_t *buf1, int size)
     438             : {
     439          25 :     RTPMuxContext *s = s1->priv_data;
     440             :     int len, max_packet_size;
     441             : 
     442          25 :     max_packet_size = s->max_payload_size;
     443             : 
     444         280 :     while (size > 0) {
     445         230 :         len = max_packet_size;
     446         230 :         if (len > size)
     447          25 :             len = size;
     448             : 
     449         230 :         s->timestamp = s->cur_timestamp;
     450         230 :         ff_rtp_send_data(s1, buf1, len, (len == size));
     451             : 
     452         230 :         buf1 += len;
     453         230 :         size -= len;
     454             :     }
     455          25 : }
     456             : 
     457             : /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
     458           0 : static void rtp_send_mpegts_raw(AVFormatContext *s1,
     459             :                                 const uint8_t *buf1, int size)
     460             : {
     461           0 :     RTPMuxContext *s = s1->priv_data;
     462             :     int len, out_len;
     463             : 
     464           0 :     s->timestamp = s->cur_timestamp;
     465           0 :     while (size >= TS_PACKET_SIZE) {
     466           0 :         len = s->max_payload_size - (s->buf_ptr - s->buf);
     467           0 :         if (len > size)
     468           0 :             len = size;
     469           0 :         memcpy(s->buf_ptr, buf1, len);
     470           0 :         buf1 += len;
     471           0 :         size -= len;
     472           0 :         s->buf_ptr += len;
     473             : 
     474           0 :         out_len = s->buf_ptr - s->buf;
     475           0 :         if (out_len >= s->max_payload_size) {
     476           0 :             ff_rtp_send_data(s1, s->buf, out_len, 0);
     477           0 :             s->buf_ptr = s->buf;
     478             :         }
     479             :     }
     480           0 : }
     481             : 
     482           0 : static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
     483             : {
     484           0 :     RTPMuxContext *s = s1->priv_data;
     485           0 :     AVStream *st = s1->streams[0];
     486           0 :     int frame_duration = av_get_audio_frame_duration2(st->codecpar, 0);
     487           0 :     int frame_size = st->codecpar->block_align;
     488           0 :     int frames = size / frame_size;
     489             : 
     490           0 :     while (frames > 0) {
     491           0 :         if (s->num_frames > 0 &&
     492           0 :             av_compare_ts(s->cur_timestamp - s->timestamp, st->time_base,
     493           0 :                           s1->max_delay, AV_TIME_BASE_Q) >= 0) {
     494           0 :             ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
     495           0 :             s->num_frames = 0;
     496             :         }
     497             : 
     498           0 :         if (!s->num_frames) {
     499           0 :             s->buf_ptr = s->buf;
     500           0 :             s->timestamp = s->cur_timestamp;
     501             :         }
     502           0 :         memcpy(s->buf_ptr, buf, frame_size);
     503           0 :         frames--;
     504           0 :         s->num_frames++;
     505           0 :         s->buf_ptr       += frame_size;
     506           0 :         buf              += frame_size;
     507           0 :         s->cur_timestamp += frame_duration;
     508             : 
     509           0 :         if (s->num_frames == s->max_frames_per_packet) {
     510           0 :             ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
     511           0 :             s->num_frames = 0;
     512             :         }
     513             :     }
     514           0 :     return 0;
     515             : }
     516             : 
     517          69 : static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
     518             : {
     519          69 :     RTPMuxContext *s = s1->priv_data;
     520          69 :     AVStream *st = s1->streams[0];
     521             :     int rtcp_bytes;
     522          69 :     int size= pkt->size;
     523             : 
     524          69 :     av_log(s1, AV_LOG_TRACE, "%d: write len=%d\n", pkt->stream_index, size);
     525             : 
     526          69 :     rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
     527             :         RTCP_TX_RATIO_DEN;
     528         131 :     if ((s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
     529          64 :                             (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) &&
     530           2 :         !(s->flags & FF_RTP_FLAG_SKIP_RTCP)) {
     531           2 :         rtcp_send_sr(s1, ff_ntp_time(), 0);
     532           2 :         s->last_octet_count = s->octet_count;
     533           2 :         s->first_packet = 0;
     534             :     }
     535          69 :     s->cur_timestamp = s->base_timestamp + pkt->pts;
     536             : 
     537          69 :     switch(st->codecpar->codec_id) {
     538          44 :     case AV_CODEC_ID_PCM_MULAW:
     539             :     case AV_CODEC_ID_PCM_ALAW:
     540             :     case AV_CODEC_ID_PCM_U8:
     541             :     case AV_CODEC_ID_PCM_S8:
     542          44 :         return rtp_send_samples(s1, pkt->data, size, 8 * st->codecpar->channels);
     543           0 :     case AV_CODEC_ID_PCM_U16BE:
     544             :     case AV_CODEC_ID_PCM_U16LE:
     545             :     case AV_CODEC_ID_PCM_S16BE:
     546             :     case AV_CODEC_ID_PCM_S16LE:
     547           0 :         return rtp_send_samples(s1, pkt->data, size, 16 * st->codecpar->channels);
     548           0 :     case AV_CODEC_ID_PCM_S24BE:
     549           0 :         return rtp_send_samples(s1, pkt->data, size, 24 * st->codecpar->channels);
     550           0 :     case AV_CODEC_ID_ADPCM_G722:
     551             :         /* The actual sample size is half a byte per sample, but since the
     552             :          * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
     553             :          * the correct parameter for send_samples_bits is 8 bits per stream
     554             :          * clock. */
     555           0 :         return rtp_send_samples(s1, pkt->data, size, 8 * st->codecpar->channels);
     556           0 :     case AV_CODEC_ID_ADPCM_G726:
     557             :     case AV_CODEC_ID_ADPCM_G726LE:
     558           0 :         return rtp_send_samples(s1, pkt->data, size,
     559           0 :                                 st->codecpar->bits_per_coded_sample * st->codecpar->channels);
     560           0 :     case AV_CODEC_ID_MP2:
     561             :     case AV_CODEC_ID_MP3:
     562           0 :         rtp_send_mpegaudio(s1, pkt->data, size);
     563           0 :         break;
     564           0 :     case AV_CODEC_ID_MPEG1VIDEO:
     565             :     case AV_CODEC_ID_MPEG2VIDEO:
     566           0 :         ff_rtp_send_mpegvideo(s1, pkt->data, size);
     567           0 :         break;
     568           0 :     case AV_CODEC_ID_AAC:
     569           0 :         if (s->flags & FF_RTP_FLAG_MP4A_LATM)
     570           0 :             ff_rtp_send_latm(s1, pkt->data, size);
     571             :         else
     572           0 :             ff_rtp_send_aac(s1, pkt->data, size);
     573           0 :         break;
     574           0 :     case AV_CODEC_ID_AMR_NB:
     575             :     case AV_CODEC_ID_AMR_WB:
     576           0 :         ff_rtp_send_amr(s1, pkt->data, size);
     577           0 :         break;
     578           0 :     case AV_CODEC_ID_MPEG2TS:
     579           0 :         rtp_send_mpegts_raw(s1, pkt->data, size);
     580           0 :         break;
     581           0 :     case AV_CODEC_ID_DIRAC:
     582           0 :         ff_rtp_send_vc2hq(s1, pkt->data, size, st->codecpar->field_order != AV_FIELD_PROGRESSIVE ? 1 : 0);
     583           0 :         break;
     584           0 :     case AV_CODEC_ID_H264:
     585           0 :         ff_rtp_send_h264_hevc(s1, pkt->data, size);
     586           0 :         break;
     587           0 :     case AV_CODEC_ID_H261:
     588           0 :         ff_rtp_send_h261(s1, pkt->data, size);
     589           0 :         break;
     590           0 :     case AV_CODEC_ID_H263:
     591           0 :         if (s->flags & FF_RTP_FLAG_RFC2190) {
     592           0 :             int mb_info_size = 0;
     593           0 :             const uint8_t *mb_info =
     594             :                 av_packet_get_side_data(pkt, AV_PKT_DATA_H263_MB_INFO,
     595             :                                         &mb_info_size);
     596           0 :             ff_rtp_send_h263_rfc2190(s1, pkt->data, size, mb_info, mb_info_size);
     597           0 :             break;
     598             :         }
     599             :         /* Fallthrough */
     600             :     case AV_CODEC_ID_H263P:
     601           0 :         ff_rtp_send_h263(s1, pkt->data, size);
     602           0 :         break;
     603           0 :     case AV_CODEC_ID_HEVC:
     604           0 :         ff_rtp_send_h264_hevc(s1, pkt->data, size);
     605           0 :         break;
     606           0 :     case AV_CODEC_ID_VORBIS:
     607             :     case AV_CODEC_ID_THEORA:
     608           0 :         ff_rtp_send_xiph(s1, pkt->data, size);
     609           0 :         break;
     610           0 :     case AV_CODEC_ID_VP8:
     611           0 :         ff_rtp_send_vp8(s1, pkt->data, size);
     612           0 :         break;
     613           0 :     case AV_CODEC_ID_VP9:
     614           0 :         ff_rtp_send_vp9(s1, pkt->data, size);
     615           0 :         break;
     616           0 :     case AV_CODEC_ID_ILBC:
     617           0 :         rtp_send_ilbc(s1, pkt->data, size);
     618           0 :         break;
     619           0 :     case AV_CODEC_ID_MJPEG:
     620           0 :         ff_rtp_send_jpeg(s1, pkt->data, size);
     621           0 :         break;
     622           0 :     case AV_CODEC_ID_OPUS:
     623           0 :         if (size > s->max_payload_size) {
     624           0 :             av_log(s1, AV_LOG_ERROR,
     625             :                    "Packet size %d too large for max RTP payload size %d\n",
     626             :                    size, s->max_payload_size);
     627           0 :             return AVERROR(EINVAL);
     628             :         }
     629             :         /* Intentional fallthrough */
     630             :     default:
     631             :         /* better than nothing : send the codec raw data */
     632          25 :         rtp_send_raw(s1, pkt->data, size);
     633          25 :         break;
     634             :     }
     635          25 :     return 0;
     636             : }
     637             : 
     638           2 : static int rtp_write_trailer(AVFormatContext *s1)
     639             : {
     640           2 :     RTPMuxContext *s = s1->priv_data;
     641             : 
     642             :     /* If the caller closes and recreates ->pb, this might actually
     643             :      * be NULL here even if it was successfully allocated at the start. */
     644           2 :     if (s1->pb && (s->flags & FF_RTP_FLAG_SEND_BYE))
     645           0 :         rtcp_send_sr(s1, ff_ntp_time(), 1);
     646           2 :     av_freep(&s->buf);
     647             : 
     648           2 :     return 0;
     649             : }
     650             : 
     651             : AVOutputFormat ff_rtp_muxer = {
     652             :     .name              = "rtp",
     653             :     .long_name         = NULL_IF_CONFIG_SMALL("RTP output"),
     654             :     .priv_data_size    = sizeof(RTPMuxContext),
     655             :     .audio_codec       = AV_CODEC_ID_PCM_MULAW,
     656             :     .video_codec       = AV_CODEC_ID_MPEG4,
     657             :     .write_header      = rtp_write_header,
     658             :     .write_packet      = rtp_write_packet,
     659             :     .write_trailer     = rtp_write_trailer,
     660             :     .priv_class        = &rtp_muxer_class,
     661             :     .flags             = AVFMT_TS_NONSTRICT,
     662             : };

Generated by: LCOV version 1.13