LCOV - code coverage report
Current view: top level - libavformat - rtpdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 54 516 10.5 %
Date: 2017-12-18 06:23:41 Functions: 2 28 7.1 %

          Line data    Source code
       1             : /*
       2             :  * RTP input 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 "libavutil/mathematics.h"
      23             : #include "libavutil/avstring.h"
      24             : #include "libavutil/intreadwrite.h"
      25             : #include "libavutil/time.h"
      26             : 
      27             : #include "avformat.h"
      28             : #include "network.h"
      29             : #include "srtp.h"
      30             : #include "url.h"
      31             : #include "rtpdec.h"
      32             : #include "rtpdec_formats.h"
      33             : 
      34             : #define MIN_FEEDBACK_INTERVAL 200000 /* 200 ms in us */
      35             : 
      36             : static RTPDynamicProtocolHandler l24_dynamic_handler = {
      37             :     .enc_name   = "L24",
      38             :     .codec_type = AVMEDIA_TYPE_AUDIO,
      39             :     .codec_id   = AV_CODEC_ID_PCM_S24BE,
      40             : };
      41             : 
      42             : static RTPDynamicProtocolHandler gsm_dynamic_handler = {
      43             :     .enc_name   = "GSM",
      44             :     .codec_type = AVMEDIA_TYPE_AUDIO,
      45             :     .codec_id   = AV_CODEC_ID_GSM,
      46             : };
      47             : 
      48             : static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = {
      49             :     .enc_name   = "X-MP3-draft-00",
      50             :     .codec_type = AVMEDIA_TYPE_AUDIO,
      51             :     .codec_id   = AV_CODEC_ID_MP3ADU,
      52             : };
      53             : 
      54             : static RTPDynamicProtocolHandler speex_dynamic_handler = {
      55             :     .enc_name   = "speex",
      56             :     .codec_type = AVMEDIA_TYPE_AUDIO,
      57             :     .codec_id   = AV_CODEC_ID_SPEEX,
      58             : };
      59             : 
      60             : static RTPDynamicProtocolHandler opus_dynamic_handler = {
      61             :     .enc_name   = "opus",
      62             :     .codec_type = AVMEDIA_TYPE_AUDIO,
      63             :     .codec_id   = AV_CODEC_ID_OPUS,
      64             : };
      65             : 
      66             : static RTPDynamicProtocolHandler t140_dynamic_handler = { /* RFC 4103 */
      67             :     .enc_name   = "t140",
      68             :     .codec_type = AVMEDIA_TYPE_SUBTITLE,
      69             :     .codec_id   = AV_CODEC_ID_TEXT,
      70             : };
      71             : 
      72             : static RTPDynamicProtocolHandler *rtp_first_dynamic_payload_handler = NULL;
      73             : 
      74      290888 : void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
      75             : {
      76      290888 :     handler->next = rtp_first_dynamic_payload_handler;
      77      290888 :     rtp_first_dynamic_payload_handler = handler;
      78      290888 : }
      79             : 
      80        5594 : void ff_register_rtp_dynamic_payload_handlers(void)
      81             : {
      82        5594 :     ff_register_dynamic_payload_handler(&ff_ac3_dynamic_handler);
      83        5594 :     ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
      84        5594 :     ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
      85        5594 :     ff_register_dynamic_payload_handler(&ff_dv_dynamic_handler);
      86        5594 :     ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
      87        5594 :     ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
      88        5594 :     ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
      89        5594 :     ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
      90        5594 :     ff_register_dynamic_payload_handler(&ff_g726le_16_dynamic_handler);
      91        5594 :     ff_register_dynamic_payload_handler(&ff_g726le_24_dynamic_handler);
      92        5594 :     ff_register_dynamic_payload_handler(&ff_g726le_32_dynamic_handler);
      93        5594 :     ff_register_dynamic_payload_handler(&ff_g726le_40_dynamic_handler);
      94        5594 :     ff_register_dynamic_payload_handler(&ff_h261_dynamic_handler);
      95        5594 :     ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
      96        5594 :     ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
      97        5594 :     ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
      98        5594 :     ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
      99        5594 :     ff_register_dynamic_payload_handler(&ff_hevc_dynamic_handler);
     100        5594 :     ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler);
     101        5594 :     ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler);
     102        5594 :     ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
     103        5594 :     ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
     104        5594 :     ff_register_dynamic_payload_handler(&ff_mpeg_audio_dynamic_handler);
     105        5594 :     ff_register_dynamic_payload_handler(&ff_mpeg_audio_robust_dynamic_handler);
     106        5594 :     ff_register_dynamic_payload_handler(&ff_mpeg_video_dynamic_handler);
     107        5594 :     ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
     108        5594 :     ff_register_dynamic_payload_handler(&ff_mpegts_dynamic_handler);
     109        5594 :     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
     110        5594 :     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
     111        5594 :     ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
     112        5594 :     ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
     113        5594 :     ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
     114        5594 :     ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
     115        5594 :     ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
     116        5594 :     ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
     117        5594 :     ff_register_dynamic_payload_handler(&ff_rfc4175_rtp_handler);
     118        5594 :     ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
     119        5594 :     ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
     120        5594 :     ff_register_dynamic_payload_handler(&ff_vc2hq_dynamic_handler);
     121        5594 :     ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
     122        5594 :     ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
     123        5594 :     ff_register_dynamic_payload_handler(&ff_vp9_dynamic_handler);
     124        5594 :     ff_register_dynamic_payload_handler(&gsm_dynamic_handler);
     125        5594 :     ff_register_dynamic_payload_handler(&l24_dynamic_handler);
     126        5594 :     ff_register_dynamic_payload_handler(&opus_dynamic_handler);
     127        5594 :     ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
     128        5594 :     ff_register_dynamic_payload_handler(&speex_dynamic_handler);
     129        5594 :     ff_register_dynamic_payload_handler(&t140_dynamic_handler);
     130        5594 : }
     131             : 
     132           0 : RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
     133             :                                                        enum AVMediaType codec_type)
     134             : {
     135             :     RTPDynamicProtocolHandler *handler;
     136           0 :     for (handler = rtp_first_dynamic_payload_handler;
     137           0 :          handler; handler = handler->next)
     138           0 :         if (handler->enc_name &&
     139           0 :             !av_strcasecmp(name, handler->enc_name) &&
     140           0 :             codec_type == handler->codec_type)
     141           0 :             return handler;
     142           0 :     return NULL;
     143             : }
     144             : 
     145           0 : RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
     146             :                                                      enum AVMediaType codec_type)
     147             : {
     148             :     RTPDynamicProtocolHandler *handler;
     149           0 :     for (handler = rtp_first_dynamic_payload_handler;
     150           0 :          handler; handler = handler->next)
     151           0 :         if (handler->static_payload_id && handler->static_payload_id == id &&
     152           0 :             codec_type == handler->codec_type)
     153           0 :             return handler;
     154           0 :     return NULL;
     155             : }
     156             : 
     157           0 : static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
     158             :                              int len)
     159             : {
     160             :     int payload_len;
     161           0 :     while (len >= 4) {
     162           0 :         payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
     163             : 
     164           0 :         switch (buf[1]) {
     165           0 :         case RTCP_SR:
     166           0 :             if (payload_len < 20) {
     167           0 :                 av_log(s->ic, AV_LOG_ERROR, "Invalid RTCP SR packet length\n");
     168           0 :                 return AVERROR_INVALIDDATA;
     169             :             }
     170             : 
     171           0 :             s->last_rtcp_reception_time = av_gettime_relative();
     172           0 :             s->last_rtcp_ntp_time  = AV_RB64(buf + 8);
     173           0 :             s->last_rtcp_timestamp = AV_RB32(buf + 16);
     174           0 :             if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
     175           0 :                 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
     176           0 :                 if (!s->base_timestamp)
     177           0 :                     s->base_timestamp = s->last_rtcp_timestamp;
     178           0 :                 s->rtcp_ts_offset = (int32_t)(s->last_rtcp_timestamp - s->base_timestamp);
     179             :             }
     180             : 
     181           0 :             break;
     182           0 :         case RTCP_BYE:
     183           0 :             return -RTCP_BYE;
     184             :         }
     185             : 
     186           0 :         buf += payload_len;
     187           0 :         len -= payload_len;
     188             :     }
     189           0 :     return -1;
     190             : }
     191             : 
     192             : #define RTP_SEQ_MOD (1 << 16)
     193             : 
     194           0 : static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
     195             : {
     196           0 :     memset(s, 0, sizeof(RTPStatistics));
     197           0 :     s->max_seq   = base_sequence;
     198           0 :     s->probation = 1;
     199           0 : }
     200             : 
     201             : /*
     202             :  * Called whenever there is a large jump in sequence numbers,
     203             :  * or when they get out of probation...
     204             :  */
     205           0 : static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
     206             : {
     207           0 :     s->max_seq        = seq;
     208           0 :     s->cycles         = 0;
     209           0 :     s->base_seq       = seq - 1;
     210           0 :     s->bad_seq        = RTP_SEQ_MOD + 1;
     211           0 :     s->received       = 0;
     212           0 :     s->expected_prior = 0;
     213           0 :     s->received_prior = 0;
     214           0 :     s->jitter         = 0;
     215           0 :     s->transit        = 0;
     216           0 : }
     217             : 
     218             : /* Returns 1 if we should handle this packet. */
     219           0 : static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
     220             : {
     221           0 :     uint16_t udelta = seq - s->max_seq;
     222           0 :     const int MAX_DROPOUT    = 3000;
     223           0 :     const int MAX_MISORDER   = 100;
     224           0 :     const int MIN_SEQUENTIAL = 2;
     225             : 
     226             :     /* source not valid until MIN_SEQUENTIAL packets with sequence
     227             :      * seq. numbers have been received */
     228           0 :     if (s->probation) {
     229           0 :         if (seq == s->max_seq + 1) {
     230           0 :             s->probation--;
     231           0 :             s->max_seq = seq;
     232           0 :             if (s->probation == 0) {
     233           0 :                 rtp_init_sequence(s, seq);
     234           0 :                 s->received++;
     235           0 :                 return 1;
     236             :             }
     237             :         } else {
     238           0 :             s->probation = MIN_SEQUENTIAL - 1;
     239           0 :             s->max_seq   = seq;
     240             :         }
     241           0 :     } else if (udelta < MAX_DROPOUT) {
     242             :         // in order, with permissible gap
     243           0 :         if (seq < s->max_seq) {
     244             :             // sequence number wrapped; count another 64k cycles
     245           0 :             s->cycles += RTP_SEQ_MOD;
     246             :         }
     247           0 :         s->max_seq = seq;
     248           0 :     } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
     249             :         // sequence made a large jump...
     250           0 :         if (seq == s->bad_seq) {
     251             :             /* two sequential packets -- assume that the other side
     252             :              * restarted without telling us; just resync. */
     253           0 :             rtp_init_sequence(s, seq);
     254             :         } else {
     255           0 :             s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
     256           0 :             return 0;
     257             :         }
     258             :     } else {
     259             :         // duplicate or reordered packet...
     260             :     }
     261           0 :     s->received++;
     262           0 :     return 1;
     263             : }
     264             : 
     265           0 : static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp,
     266             :                                uint32_t arrival_timestamp)
     267             : {
     268             :     // Most of this is pretty straight from RFC 3550 appendix A.8
     269           0 :     uint32_t transit = arrival_timestamp - sent_timestamp;
     270           0 :     uint32_t prev_transit = s->transit;
     271           0 :     int32_t d = transit - prev_transit;
     272             :     // Doing the FFABS() call directly on the "transit - prev_transit"
     273             :     // expression doesn't work, since it's an unsigned expression. Doing the
     274             :     // transit calculation in unsigned is desired though, since it most
     275             :     // probably will need to wrap around.
     276           0 :     d = FFABS(d);
     277           0 :     s->transit = transit;
     278           0 :     if (!prev_transit)
     279           0 :         return;
     280           0 :     s->jitter += d - (int32_t) ((s->jitter + 8) >> 4);
     281             : }
     282             : 
     283           0 : int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
     284             :                                   AVIOContext *avio, int count)
     285             : {
     286             :     AVIOContext *pb;
     287             :     uint8_t *buf;
     288             :     int len;
     289             :     int rtcp_bytes;
     290           0 :     RTPStatistics *stats = &s->statistics;
     291             :     uint32_t lost;
     292             :     uint32_t extended_max;
     293             :     uint32_t expected_interval;
     294             :     uint32_t received_interval;
     295             :     int32_t  lost_interval;
     296             :     uint32_t expected;
     297             :     uint32_t fraction;
     298             : 
     299           0 :     if ((!fd && !avio) || (count < 1))
     300           0 :         return -1;
     301             : 
     302             :     /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
     303             :     /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
     304           0 :     s->octet_count += count;
     305           0 :     rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
     306             :         RTCP_TX_RATIO_DEN;
     307           0 :     rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
     308           0 :     if (rtcp_bytes < 28)
     309           0 :         return -1;
     310           0 :     s->last_octet_count = s->octet_count;
     311             : 
     312           0 :     if (!fd)
     313           0 :         pb = avio;
     314           0 :     else if (avio_open_dyn_buf(&pb) < 0)
     315           0 :         return -1;
     316             : 
     317             :     // Receiver Report
     318           0 :     avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
     319           0 :     avio_w8(pb, RTCP_RR);
     320           0 :     avio_wb16(pb, 7); /* length in words - 1 */
     321             :     // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
     322           0 :     avio_wb32(pb, s->ssrc + 1);
     323           0 :     avio_wb32(pb, s->ssrc); // server SSRC
     324             :     // some placeholders we should really fill...
     325             :     // RFC 1889/p64
     326           0 :     extended_max          = stats->cycles + stats->max_seq;
     327           0 :     expected              = extended_max - stats->base_seq;
     328           0 :     lost                  = expected - stats->received;
     329           0 :     lost                  = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
     330           0 :     expected_interval     = expected - stats->expected_prior;
     331           0 :     stats->expected_prior = expected;
     332           0 :     received_interval     = stats->received - stats->received_prior;
     333           0 :     stats->received_prior = stats->received;
     334           0 :     lost_interval         = expected_interval - received_interval;
     335           0 :     if (expected_interval == 0 || lost_interval <= 0)
     336           0 :         fraction = 0;
     337             :     else
     338           0 :         fraction = (lost_interval << 8) / expected_interval;
     339             : 
     340           0 :     fraction = (fraction << 24) | lost;
     341             : 
     342           0 :     avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
     343           0 :     avio_wb32(pb, extended_max); /* max sequence received */
     344           0 :     avio_wb32(pb, stats->jitter >> 4); /* jitter */
     345             : 
     346           0 :     if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) {
     347           0 :         avio_wb32(pb, 0); /* last SR timestamp */
     348           0 :         avio_wb32(pb, 0); /* delay since last SR */
     349             :     } else {
     350           0 :         uint32_t middle_32_bits   = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
     351           0 :         uint32_t delay_since_last = av_rescale(av_gettime_relative() - s->last_rtcp_reception_time,
     352             :                                                65536, AV_TIME_BASE);
     353             : 
     354           0 :         avio_wb32(pb, middle_32_bits); /* last SR timestamp */
     355           0 :         avio_wb32(pb, delay_since_last); /* delay since last SR */
     356             :     }
     357             : 
     358             :     // CNAME
     359           0 :     avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
     360           0 :     avio_w8(pb, RTCP_SDES);
     361           0 :     len = strlen(s->hostname);
     362           0 :     avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */
     363           0 :     avio_wb32(pb, s->ssrc + 1);
     364           0 :     avio_w8(pb, 0x01);
     365           0 :     avio_w8(pb, len);
     366           0 :     avio_write(pb, s->hostname, len);
     367           0 :     avio_w8(pb, 0); /* END */
     368             :     // padding
     369           0 :     for (len = (7 + len) % 4; len % 4; len++)
     370           0 :         avio_w8(pb, 0);
     371             : 
     372           0 :     avio_flush(pb);
     373           0 :     if (!fd)
     374           0 :         return 0;
     375           0 :     len = avio_close_dyn_buf(pb, &buf);
     376           0 :     if ((len > 0) && buf) {
     377             :         int av_unused result;
     378           0 :         av_log(s->ic, AV_LOG_TRACE, "sending %d bytes of RR\n", len);
     379           0 :         result = ffurl_write(fd, buf, len);
     380           0 :         av_log(s->ic, AV_LOG_TRACE, "result from ffurl_write: %d\n", result);
     381           0 :         av_free(buf);
     382             :     }
     383           0 :     return 0;
     384             : }
     385             : 
     386           0 : void ff_rtp_send_punch_packets(URLContext *rtp_handle)
     387             : {
     388             :     AVIOContext *pb;
     389             :     uint8_t *buf;
     390             :     int len;
     391             : 
     392             :     /* Send a small RTP packet */
     393           0 :     if (avio_open_dyn_buf(&pb) < 0)
     394           0 :         return;
     395             : 
     396           0 :     avio_w8(pb, (RTP_VERSION << 6));
     397           0 :     avio_w8(pb, 0); /* Payload type */
     398           0 :     avio_wb16(pb, 0); /* Seq */
     399           0 :     avio_wb32(pb, 0); /* Timestamp */
     400           0 :     avio_wb32(pb, 0); /* SSRC */
     401             : 
     402           0 :     avio_flush(pb);
     403           0 :     len = avio_close_dyn_buf(pb, &buf);
     404           0 :     if ((len > 0) && buf)
     405           0 :         ffurl_write(rtp_handle, buf, len);
     406           0 :     av_free(buf);
     407             : 
     408             :     /* Send a minimal RTCP RR */
     409           0 :     if (avio_open_dyn_buf(&pb) < 0)
     410           0 :         return;
     411             : 
     412           0 :     avio_w8(pb, (RTP_VERSION << 6));
     413           0 :     avio_w8(pb, RTCP_RR); /* receiver report */
     414           0 :     avio_wb16(pb, 1); /* length in words - 1 */
     415           0 :     avio_wb32(pb, 0); /* our own SSRC */
     416             : 
     417           0 :     avio_flush(pb);
     418           0 :     len = avio_close_dyn_buf(pb, &buf);
     419           0 :     if ((len > 0) && buf)
     420           0 :         ffurl_write(rtp_handle, buf, len);
     421           0 :     av_free(buf);
     422             : }
     423             : 
     424           0 : static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,
     425             :                                 uint16_t *missing_mask)
     426             : {
     427             :     int i;
     428           0 :     uint16_t next_seq = s->seq + 1;
     429           0 :     RTPPacket *pkt = s->queue;
     430             : 
     431           0 :     if (!pkt || pkt->seq == next_seq)
     432           0 :         return 0;
     433             : 
     434           0 :     *missing_mask = 0;
     435           0 :     for (i = 1; i <= 16; i++) {
     436           0 :         uint16_t missing_seq = next_seq + i;
     437           0 :         while (pkt) {
     438           0 :             int16_t diff = pkt->seq - missing_seq;
     439           0 :             if (diff >= 0)
     440           0 :                 break;
     441           0 :             pkt = pkt->next;
     442             :         }
     443           0 :         if (!pkt)
     444           0 :             break;
     445           0 :         if (pkt->seq == missing_seq)
     446           0 :             continue;
     447           0 :         *missing_mask |= 1 << (i - 1);
     448             :     }
     449             : 
     450           0 :     *first_missing = next_seq;
     451           0 :     return 1;
     452             : }
     453             : 
     454           0 : int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd,
     455             :                               AVIOContext *avio)
     456             : {
     457             :     int len, need_keyframe, missing_packets;
     458             :     AVIOContext *pb;
     459             :     uint8_t *buf;
     460             :     int64_t now;
     461           0 :     uint16_t first_missing = 0, missing_mask = 0;
     462             : 
     463           0 :     if (!fd && !avio)
     464           0 :         return -1;
     465             : 
     466           0 :     need_keyframe = s->handler && s->handler->need_keyframe &&
     467           0 :                     s->handler->need_keyframe(s->dynamic_protocol_context);
     468           0 :     missing_packets = find_missing_packets(s, &first_missing, &missing_mask);
     469             : 
     470           0 :     if (!need_keyframe && !missing_packets)
     471           0 :         return 0;
     472             : 
     473             :     /* Send new feedback if enough time has elapsed since the last
     474             :      * feedback packet. */
     475             : 
     476           0 :     now = av_gettime_relative();
     477           0 :     if (s->last_feedback_time &&
     478           0 :         (now - s->last_feedback_time) < MIN_FEEDBACK_INTERVAL)
     479           0 :         return 0;
     480           0 :     s->last_feedback_time = now;
     481             : 
     482           0 :     if (!fd)
     483           0 :         pb = avio;
     484           0 :     else if (avio_open_dyn_buf(&pb) < 0)
     485           0 :         return -1;
     486             : 
     487           0 :     if (need_keyframe) {
     488           0 :         avio_w8(pb, (RTP_VERSION << 6) | 1); /* PLI */
     489           0 :         avio_w8(pb, RTCP_PSFB);
     490           0 :         avio_wb16(pb, 2); /* length in words - 1 */
     491             :         // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
     492           0 :         avio_wb32(pb, s->ssrc + 1);
     493           0 :         avio_wb32(pb, s->ssrc); // server SSRC
     494             :     }
     495             : 
     496           0 :     if (missing_packets) {
     497           0 :         avio_w8(pb, (RTP_VERSION << 6) | 1); /* NACK */
     498           0 :         avio_w8(pb, RTCP_RTPFB);
     499           0 :         avio_wb16(pb, 3); /* length in words - 1 */
     500           0 :         avio_wb32(pb, s->ssrc + 1);
     501           0 :         avio_wb32(pb, s->ssrc); // server SSRC
     502             : 
     503           0 :         avio_wb16(pb, first_missing);
     504           0 :         avio_wb16(pb, missing_mask);
     505             :     }
     506             : 
     507           0 :     avio_flush(pb);
     508           0 :     if (!fd)
     509           0 :         return 0;
     510           0 :     len = avio_close_dyn_buf(pb, &buf);
     511           0 :     if (len > 0 && buf) {
     512           0 :         ffurl_write(fd, buf, len);
     513           0 :         av_free(buf);
     514             :     }
     515           0 :     return 0;
     516             : }
     517             : 
     518             : /**
     519             :  * open a new RTP parse context for stream 'st'. 'st' can be NULL for
     520             :  * MPEG-2 TS streams.
     521             :  */
     522           0 : RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st,
     523             :                                    int payload_type, int queue_size)
     524             : {
     525             :     RTPDemuxContext *s;
     526             : 
     527           0 :     s = av_mallocz(sizeof(RTPDemuxContext));
     528           0 :     if (!s)
     529           0 :         return NULL;
     530           0 :     s->payload_type        = payload_type;
     531           0 :     s->last_rtcp_ntp_time  = AV_NOPTS_VALUE;
     532           0 :     s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
     533           0 :     s->ic                  = s1;
     534           0 :     s->st                  = st;
     535           0 :     s->queue_size          = queue_size;
     536             : 
     537           0 :     av_log(s->ic, AV_LOG_VERBOSE, "setting jitter buffer size to %d\n",
     538             :            s->queue_size);
     539             : 
     540           0 :     rtp_init_statistics(&s->statistics, 0);
     541           0 :     if (st) {
     542           0 :         switch (st->codecpar->codec_id) {
     543           0 :         case AV_CODEC_ID_ADPCM_G722:
     544             :             /* According to RFC 3551, the stream clock rate is 8000
     545             :              * even if the sample rate is 16000. */
     546           0 :             if (st->codecpar->sample_rate == 8000)
     547           0 :                 st->codecpar->sample_rate = 16000;
     548           0 :             break;
     549           0 :         default:
     550           0 :             break;
     551             :         }
     552           0 :     }
     553             :     // needed to send back RTCP RR in RTSP sessions
     554           0 :     gethostname(s->hostname, sizeof(s->hostname));
     555           0 :     return s;
     556             : }
     557             : 
     558           0 : void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
     559             :                                        RTPDynamicProtocolHandler *handler)
     560             : {
     561           0 :     s->dynamic_protocol_context = ctx;
     562           0 :     s->handler                  = handler;
     563           0 : }
     564             : 
     565           0 : void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite,
     566             :                              const char *params)
     567             : {
     568           0 :     if (!ff_srtp_set_crypto(&s->srtp, suite, params))
     569           0 :         s->srtp_enabled = 1;
     570           0 : }
     571             : 
     572             : /**
     573             :  * This was the second switch in rtp_parse packet.
     574             :  * Normalizes time, if required, sets stream_index, etc.
     575             :  */
     576           0 : static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
     577             : {
     578           0 :     if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
     579           0 :         return; /* Timestamp already set by depacketizer */
     580           0 :     if (timestamp == RTP_NOTS_VALUE)
     581           0 :         return;
     582             : 
     583           0 :     if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
     584             :         int64_t addend;
     585             :         int delta_timestamp;
     586             : 
     587             :         /* compute pts from timestamp with received ntp_time */
     588           0 :         delta_timestamp = timestamp - s->last_rtcp_timestamp;
     589             :         /* convert to the PTS timebase */
     590           0 :         addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time,
     591           0 :                             s->st->time_base.den,
     592           0 :                             (uint64_t) s->st->time_base.num << 32);
     593           0 :         pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
     594             :                    delta_timestamp;
     595           0 :         return;
     596             :     }
     597             : 
     598           0 :     if (!s->base_timestamp)
     599           0 :         s->base_timestamp = timestamp;
     600             :     /* assume that the difference is INT32_MIN < x < INT32_MAX,
     601             :      * but allow the first timestamp to exceed INT32_MAX */
     602           0 :     if (!s->timestamp)
     603           0 :         s->unwrapped_timestamp += timestamp;
     604             :     else
     605           0 :         s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
     606           0 :     s->timestamp = timestamp;
     607           0 :     pkt->pts     = s->unwrapped_timestamp + s->range_start_offset -
     608           0 :                    s->base_timestamp;
     609             : }
     610             : 
     611           0 : static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
     612             :                                      const uint8_t *buf, int len)
     613             : {
     614             :     unsigned int ssrc;
     615           0 :     int payload_type, seq, flags = 0;
     616             :     int ext, csrc;
     617             :     AVStream *st;
     618             :     uint32_t timestamp;
     619           0 :     int rv = 0;
     620             : 
     621           0 :     csrc         = buf[0] & 0x0f;
     622           0 :     ext          = buf[0] & 0x10;
     623           0 :     payload_type = buf[1] & 0x7f;
     624           0 :     if (buf[1] & 0x80)
     625           0 :         flags |= RTP_FLAG_MARKER;
     626           0 :     seq       = AV_RB16(buf + 2);
     627           0 :     timestamp = AV_RB32(buf + 4);
     628           0 :     ssrc      = AV_RB32(buf + 8);
     629             :     /* store the ssrc in the RTPDemuxContext */
     630           0 :     s->ssrc = ssrc;
     631             : 
     632             :     /* NOTE: we can handle only one payload type */
     633           0 :     if (s->payload_type != payload_type)
     634           0 :         return -1;
     635             : 
     636           0 :     st = s->st;
     637             :     // only do something with this if all the rtp checks pass...
     638           0 :     if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) {
     639           0 :         av_log(s->ic, AV_LOG_ERROR,
     640             :                "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
     641           0 :                payload_type, seq, ((s->seq + 1) & 0xffff));
     642           0 :         return -1;
     643             :     }
     644             : 
     645           0 :     if (buf[0] & 0x20) {
     646           0 :         int padding = buf[len - 1];
     647           0 :         if (len >= 12 + padding)
     648           0 :             len -= padding;
     649             :     }
     650             : 
     651           0 :     s->seq = seq;
     652           0 :     len   -= 12;
     653           0 :     buf   += 12;
     654             : 
     655           0 :     len   -= 4 * csrc;
     656           0 :     buf   += 4 * csrc;
     657           0 :     if (len < 0)
     658           0 :         return AVERROR_INVALIDDATA;
     659             : 
     660             :     /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
     661           0 :     if (ext) {
     662           0 :         if (len < 4)
     663           0 :             return -1;
     664             :         /* calculate the header extension length (stored as number
     665             :          * of 32-bit words) */
     666           0 :         ext = (AV_RB16(buf + 2) + 1) << 2;
     667             : 
     668           0 :         if (len < ext)
     669           0 :             return -1;
     670             :         // skip past RTP header extension
     671           0 :         len -= ext;
     672           0 :         buf += ext;
     673             :     }
     674             : 
     675           0 :     if (s->handler && s->handler->parse_packet) {
     676           0 :         rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
     677             :                                       s->st, pkt, &timestamp, buf, len, seq,
     678             :                                       flags);
     679           0 :     } else if (st) {
     680           0 :         if ((rv = av_new_packet(pkt, len)) < 0)
     681           0 :             return rv;
     682           0 :         memcpy(pkt->data, buf, len);
     683           0 :         pkt->stream_index = st->index;
     684             :     } else {
     685           0 :         return AVERROR(EINVAL);
     686             :     }
     687             : 
     688             :     // now perform timestamp things....
     689           0 :     finalize_packet(s, pkt, timestamp);
     690             : 
     691           0 :     return rv;
     692             : }
     693             : 
     694           0 : void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
     695             : {
     696           0 :     while (s->queue) {
     697           0 :         RTPPacket *next = s->queue->next;
     698           0 :         av_freep(&s->queue->buf);
     699           0 :         av_freep(&s->queue);
     700           0 :         s->queue = next;
     701             :     }
     702           0 :     s->seq       = 0;
     703           0 :     s->queue_len = 0;
     704           0 :     s->prev_ret  = 0;
     705           0 : }
     706             : 
     707           0 : static int enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
     708             : {
     709           0 :     uint16_t seq   = AV_RB16(buf + 2);
     710           0 :     RTPPacket **cur = &s->queue, *packet;
     711             : 
     712             :     /* Find the correct place in the queue to insert the packet */
     713           0 :     while (*cur) {
     714           0 :         int16_t diff = seq - (*cur)->seq;
     715           0 :         if (diff < 0)
     716           0 :             break;
     717           0 :         cur = &(*cur)->next;
     718             :     }
     719             : 
     720           0 :     packet = av_mallocz(sizeof(*packet));
     721           0 :     if (!packet)
     722           0 :         return AVERROR(ENOMEM);
     723           0 :     packet->recvtime = av_gettime_relative();
     724           0 :     packet->seq      = seq;
     725           0 :     packet->len      = len;
     726           0 :     packet->buf      = buf;
     727           0 :     packet->next     = *cur;
     728           0 :     *cur = packet;
     729           0 :     s->queue_len++;
     730             : 
     731           0 :     return 0;
     732             : }
     733             : 
     734           0 : static int has_next_packet(RTPDemuxContext *s)
     735             : {
     736           0 :     return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
     737             : }
     738             : 
     739           0 : int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
     740             : {
     741           0 :     return s->queue ? s->queue->recvtime : 0;
     742             : }
     743             : 
     744           0 : static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
     745             : {
     746             :     int rv;
     747             :     RTPPacket *next;
     748             : 
     749           0 :     if (s->queue_len <= 0)
     750           0 :         return -1;
     751             : 
     752           0 :     if (!has_next_packet(s))
     753           0 :         av_log(s->ic, AV_LOG_WARNING,
     754           0 :                "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
     755             : 
     756             :     /* Parse the first packet in the queue, and dequeue it */
     757           0 :     rv   = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
     758           0 :     next = s->queue->next;
     759           0 :     av_freep(&s->queue->buf);
     760           0 :     av_freep(&s->queue);
     761           0 :     s->queue = next;
     762           0 :     s->queue_len--;
     763           0 :     return rv;
     764             : }
     765             : 
     766           0 : static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
     767             :                                 uint8_t **bufptr, int len)
     768             : {
     769           0 :     uint8_t *buf = bufptr ? *bufptr : NULL;
     770           0 :     int flags = 0;
     771             :     uint32_t timestamp;
     772           0 :     int rv = 0;
     773             : 
     774           0 :     if (!buf) {
     775             :         /* If parsing of the previous packet actually returned 0 or an error,
     776             :          * there's nothing more to be parsed from that packet, but we may have
     777             :          * indicated that we can return the next enqueued packet. */
     778           0 :         if (s->prev_ret <= 0)
     779           0 :             return rtp_parse_queued_packet(s, pkt);
     780             :         /* return the next packets, if any */
     781           0 :         if (s->handler && s->handler->parse_packet) {
     782             :             /* timestamp should be overwritten by parse_packet, if not,
     783             :              * the packet is left with pts == AV_NOPTS_VALUE */
     784           0 :             timestamp = RTP_NOTS_VALUE;
     785           0 :             rv        = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
     786             :                                                  s->st, pkt, &timestamp, NULL, 0, 0,
     787             :                                                  flags);
     788           0 :             finalize_packet(s, pkt, timestamp);
     789           0 :             return rv;
     790             :         }
     791             :     }
     792             : 
     793           0 :     if (len < 12)
     794           0 :         return -1;
     795             : 
     796           0 :     if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
     797           0 :         return -1;
     798           0 :     if (RTP_PT_IS_RTCP(buf[1])) {
     799           0 :         return rtcp_parse_packet(s, buf, len);
     800             :     }
     801             : 
     802           0 :     if (s->st) {
     803           0 :         int64_t received = av_gettime_relative();
     804           0 :         uint32_t arrival_ts = av_rescale_q(received, AV_TIME_BASE_Q,
     805           0 :                                            s->st->time_base);
     806           0 :         timestamp = AV_RB32(buf + 4);
     807             :         // Calculate the jitter immediately, before queueing the packet
     808             :         // into the reordering queue.
     809           0 :         rtcp_update_jitter(&s->statistics, timestamp, arrival_ts);
     810             :     }
     811             : 
     812           0 :     if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
     813             :         /* First packet, or no reordering */
     814           0 :         return rtp_parse_packet_internal(s, pkt, buf, len);
     815             :     } else {
     816           0 :         uint16_t seq = AV_RB16(buf + 2);
     817           0 :         int16_t diff = seq - s->seq;
     818           0 :         if (diff < 0) {
     819             :             /* Packet older than the previously emitted one, drop */
     820           0 :             av_log(s->ic, AV_LOG_WARNING,
     821             :                    "RTP: dropping old packet received too late\n");
     822           0 :             return -1;
     823           0 :         } else if (diff <= 1) {
     824             :             /* Correct packet */
     825           0 :             rv = rtp_parse_packet_internal(s, pkt, buf, len);
     826           0 :             return rv;
     827             :         } else {
     828             :             /* Still missing some packet, enqueue this one. */
     829           0 :             rv = enqueue_packet(s, buf, len);
     830           0 :             if (rv < 0)
     831           0 :                 return rv;
     832           0 :             *bufptr = NULL;
     833             :             /* Return the first enqueued packet if the queue is full,
     834             :              * even if we're missing something */
     835           0 :             if (s->queue_len >= s->queue_size) {
     836           0 :                 av_log(s->ic, AV_LOG_WARNING, "jitter buffer full\n");
     837           0 :                 return rtp_parse_queued_packet(s, pkt);
     838             :             }
     839           0 :             return -1;
     840             :         }
     841             :     }
     842             : }
     843             : 
     844             : /**
     845             :  * Parse an RTP or RTCP packet directly sent as a buffer.
     846             :  * @param s RTP parse context.
     847             :  * @param pkt returned packet
     848             :  * @param bufptr pointer to the input buffer or NULL to read the next packets
     849             :  * @param len buffer len
     850             :  * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
     851             :  * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
     852             :  */
     853           0 : int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
     854             :                         uint8_t **bufptr, int len)
     855             : {
     856             :     int rv;
     857           0 :     if (s->srtp_enabled && bufptr && ff_srtp_decrypt(&s->srtp, *bufptr, &len) < 0)
     858           0 :         return -1;
     859           0 :     rv = rtp_parse_one_packet(s, pkt, bufptr, len);
     860           0 :     s->prev_ret = rv;
     861           0 :     while (rv < 0 && has_next_packet(s))
     862           0 :         rv = rtp_parse_queued_packet(s, pkt);
     863           0 :     return rv ? rv : has_next_packet(s);
     864             : }
     865             : 
     866           0 : void ff_rtp_parse_close(RTPDemuxContext *s)
     867             : {
     868           0 :     ff_rtp_reset_packet_queue(s);
     869           0 :     ff_srtp_free(&s->srtp);
     870           0 :     av_free(s);
     871           0 : }
     872             : 
     873           0 : int ff_parse_fmtp(AVFormatContext *s,
     874             :                   AVStream *stream, PayloadContext *data, const char *p,
     875             :                   int (*parse_fmtp)(AVFormatContext *s,
     876             :                                     AVStream *stream,
     877             :                                     PayloadContext *data,
     878             :                                     const char *attr, const char *value))
     879             : {
     880             :     char attr[256];
     881             :     char *value;
     882             :     int res;
     883           0 :     int value_size = strlen(p) + 1;
     884             : 
     885           0 :     if (!(value = av_malloc(value_size))) {
     886           0 :         av_log(s, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n");
     887           0 :         return AVERROR(ENOMEM);
     888             :     }
     889             : 
     890             :     // remove protocol identifier
     891           0 :     while (*p && *p == ' ')
     892           0 :         p++;                     // strip spaces
     893           0 :     while (*p && *p != ' ')
     894           0 :         p++;                     // eat protocol identifier
     895           0 :     while (*p && *p == ' ')
     896           0 :         p++;                     // strip trailing spaces
     897             : 
     898           0 :     while (ff_rtsp_next_attr_and_value(&p,
     899             :                                        attr, sizeof(attr),
     900             :                                        value, value_size)) {
     901           0 :         res = parse_fmtp(s, stream, data, attr, value);
     902           0 :         if (res < 0 && res != AVERROR_PATCHWELCOME) {
     903           0 :             av_free(value);
     904           0 :             return res;
     905             :         }
     906             :     }
     907           0 :     av_free(value);
     908           0 :     return 0;
     909             : }
     910             : 
     911           0 : int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
     912             : {
     913             :     int ret;
     914           0 :     av_init_packet(pkt);
     915             : 
     916           0 :     pkt->size         = avio_close_dyn_buf(*dyn_buf, &pkt->data);
     917           0 :     pkt->stream_index = stream_idx;
     918           0 :     *dyn_buf = NULL;
     919           0 :     if ((ret = av_packet_from_data(pkt, pkt->data, pkt->size)) < 0) {
     920           0 :         av_freep(&pkt->data);
     921           0 :         return ret;
     922             :     }
     923           0 :     return pkt->size;
     924             : }

Generated by: LCOV version 1.13