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