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