3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/mathematics.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/time.h"
32 #include "rtpdec_formats.h"
34 #define MIN_FEEDBACK_INTERVAL 200000 /* 200 ms in us */
36 static RTPDynamicProtocolHandler gsm_dynamic_handler = {
38 .codec_type = AVMEDIA_TYPE_AUDIO,
39 .codec_id = AV_CODEC_ID_GSM,
42 static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = {
43 .enc_name = "X-MP3-draft-00",
44 .codec_type = AVMEDIA_TYPE_AUDIO,
45 .codec_id = AV_CODEC_ID_MP3ADU,
48 static RTPDynamicProtocolHandler speex_dynamic_handler = {
50 .codec_type = AVMEDIA_TYPE_AUDIO,
51 .codec_id = AV_CODEC_ID_SPEEX,
54 static RTPDynamicProtocolHandler opus_dynamic_handler = {
56 .codec_type = AVMEDIA_TYPE_AUDIO,
57 .codec_id = AV_CODEC_ID_OPUS,
60 static RTPDynamicProtocolHandler t140_dynamic_handler = { /* RFC 4103 */
62 .codec_type = AVMEDIA_TYPE_SUBTITLE,
63 .codec_id = AV_CODEC_ID_TEXT,
66 static RTPDynamicProtocolHandler *rtp_first_dynamic_payload_handler = NULL;
68 void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
70 handler->next = rtp_first_dynamic_payload_handler;
71 rtp_first_dynamic_payload_handler = handler;
74 void ff_register_rtp_dynamic_payload_handlers(void)
76 ff_register_dynamic_payload_handler(&ff_ac3_dynamic_handler);
77 ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
78 ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
79 ff_register_dynamic_payload_handler(&ff_dv_dynamic_handler);
80 ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
81 ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
82 ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
83 ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
84 ff_register_dynamic_payload_handler(&ff_h261_dynamic_handler);
85 ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
86 ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
87 ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
88 ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
89 ff_register_dynamic_payload_handler(&ff_hevc_dynamic_handler);
90 ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler);
91 ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler);
92 ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
93 ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
94 ff_register_dynamic_payload_handler(&ff_mpeg_audio_dynamic_handler);
95 ff_register_dynamic_payload_handler(&ff_mpeg_audio_robust_dynamic_handler);
96 ff_register_dynamic_payload_handler(&ff_mpeg_video_dynamic_handler);
97 ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
98 ff_register_dynamic_payload_handler(&ff_mpegts_dynamic_handler);
99 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
100 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
101 ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
102 ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
103 ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
104 ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
105 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
106 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
107 ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
108 ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
109 ff_register_dynamic_payload_handler(&ff_vc2hq_dynamic_handler);
110 ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
111 ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
112 ff_register_dynamic_payload_handler(&ff_vp9_dynamic_handler);
113 ff_register_dynamic_payload_handler(&gsm_dynamic_handler);
114 ff_register_dynamic_payload_handler(&opus_dynamic_handler);
115 ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
116 ff_register_dynamic_payload_handler(&speex_dynamic_handler);
117 ff_register_dynamic_payload_handler(&t140_dynamic_handler);
120 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
121 enum AVMediaType codec_type)
123 RTPDynamicProtocolHandler *handler;
124 for (handler = rtp_first_dynamic_payload_handler;
125 handler; handler = handler->next)
126 if (handler->enc_name &&
127 !av_strcasecmp(name, handler->enc_name) &&
128 codec_type == handler->codec_type)
133 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
134 enum AVMediaType codec_type)
136 RTPDynamicProtocolHandler *handler;
137 for (handler = rtp_first_dynamic_payload_handler;
138 handler; handler = handler->next)
139 if (handler->static_payload_id && handler->static_payload_id == id &&
140 codec_type == handler->codec_type)
145 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
150 payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
154 if (payload_len < 20) {
155 av_log(s->ic, AV_LOG_ERROR, "Invalid RTCP SR packet length\n");
156 return AVERROR_INVALIDDATA;
159 s->last_rtcp_reception_time = av_gettime_relative();
160 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
161 s->last_rtcp_timestamp = AV_RB32(buf + 16);
162 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
163 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
164 if (!s->base_timestamp)
165 s->base_timestamp = s->last_rtcp_timestamp;
166 s->rtcp_ts_offset = (int32_t)(s->last_rtcp_timestamp - s->base_timestamp);
180 #define RTP_SEQ_MOD (1 << 16)
182 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
184 memset(s, 0, sizeof(RTPStatistics));
185 s->max_seq = base_sequence;
190 * Called whenever there is a large jump in sequence numbers,
191 * or when they get out of probation...
193 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
197 s->base_seq = seq - 1;
198 s->bad_seq = RTP_SEQ_MOD + 1;
200 s->expected_prior = 0;
201 s->received_prior = 0;
206 /* Returns 1 if we should handle this packet. */
207 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
209 uint16_t udelta = seq - s->max_seq;
210 const int MAX_DROPOUT = 3000;
211 const int MAX_MISORDER = 100;
212 const int MIN_SEQUENTIAL = 2;
214 /* source not valid until MIN_SEQUENTIAL packets with sequence
215 * seq. numbers have been received */
217 if (seq == s->max_seq + 1) {
220 if (s->probation == 0) {
221 rtp_init_sequence(s, seq);
226 s->probation = MIN_SEQUENTIAL - 1;
229 } else if (udelta < MAX_DROPOUT) {
230 // in order, with permissible gap
231 if (seq < s->max_seq) {
232 // sequence number wrapped; count another 64k cycles
233 s->cycles += RTP_SEQ_MOD;
236 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
237 // sequence made a large jump...
238 if (seq == s->bad_seq) {
239 /* two sequential packets -- assume that the other side
240 * restarted without telling us; just resync. */
241 rtp_init_sequence(s, seq);
243 s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
247 // duplicate or reordered packet...
253 static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp,
254 uint32_t arrival_timestamp)
256 // Most of this is pretty straight from RFC 3550 appendix A.8
257 uint32_t transit = arrival_timestamp - sent_timestamp;
258 uint32_t prev_transit = s->transit;
259 int32_t d = transit - prev_transit;
260 // Doing the FFABS() call directly on the "transit - prev_transit"
261 // expression doesn't work, since it's an unsigned expression. Doing the
262 // transit calculation in unsigned is desired though, since it most
263 // probably will need to wrap around.
265 s->transit = transit;
268 s->jitter += d - (int32_t) ((s->jitter + 8) >> 4);
271 int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
272 AVIOContext *avio, int count)
278 RTPStatistics *stats = &s->statistics;
280 uint32_t extended_max;
281 uint32_t expected_interval;
282 uint32_t received_interval;
283 int32_t lost_interval;
287 if ((!fd && !avio) || (count < 1))
290 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
291 /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
292 s->octet_count += count;
293 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
295 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
298 s->last_octet_count = s->octet_count;
302 else if (avio_open_dyn_buf(&pb) < 0)
306 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
307 avio_w8(pb, RTCP_RR);
308 avio_wb16(pb, 7); /* length in words - 1 */
309 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
310 avio_wb32(pb, s->ssrc + 1);
311 avio_wb32(pb, s->ssrc); // server SSRC
312 // some placeholders we should really fill...
314 extended_max = stats->cycles + stats->max_seq;
315 expected = extended_max - stats->base_seq;
316 lost = expected - stats->received;
317 lost = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
318 expected_interval = expected - stats->expected_prior;
319 stats->expected_prior = expected;
320 received_interval = stats->received - stats->received_prior;
321 stats->received_prior = stats->received;
322 lost_interval = expected_interval - received_interval;
323 if (expected_interval == 0 || lost_interval <= 0)
326 fraction = (lost_interval << 8) / expected_interval;
328 fraction = (fraction << 24) | lost;
330 avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
331 avio_wb32(pb, extended_max); /* max sequence received */
332 avio_wb32(pb, stats->jitter >> 4); /* jitter */
334 if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) {
335 avio_wb32(pb, 0); /* last SR timestamp */
336 avio_wb32(pb, 0); /* delay since last SR */
338 uint32_t middle_32_bits = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
339 uint32_t delay_since_last = av_rescale(av_gettime_relative() - s->last_rtcp_reception_time,
340 65536, AV_TIME_BASE);
342 avio_wb32(pb, middle_32_bits); /* last SR timestamp */
343 avio_wb32(pb, delay_since_last); /* delay since last SR */
347 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
348 avio_w8(pb, RTCP_SDES);
349 len = strlen(s->hostname);
350 avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */
351 avio_wb32(pb, s->ssrc + 1);
354 avio_write(pb, s->hostname, len);
355 avio_w8(pb, 0); /* END */
357 for (len = (7 + len) % 4; len % 4; len++)
363 len = avio_close_dyn_buf(pb, &buf);
364 if ((len > 0) && buf) {
365 int av_unused result;
366 av_log(s->ic, AV_LOG_TRACE, "sending %d bytes of RR\n", len);
367 result = ffurl_write(fd, buf, len);
368 av_log(s->ic, AV_LOG_TRACE, "result from ffurl_write: %d\n", result);
374 void ff_rtp_send_punch_packets(URLContext *rtp_handle)
380 /* Send a small RTP packet */
381 if (avio_open_dyn_buf(&pb) < 0)
384 avio_w8(pb, (RTP_VERSION << 6));
385 avio_w8(pb, 0); /* Payload type */
386 avio_wb16(pb, 0); /* Seq */
387 avio_wb32(pb, 0); /* Timestamp */
388 avio_wb32(pb, 0); /* SSRC */
391 len = avio_close_dyn_buf(pb, &buf);
392 if ((len > 0) && buf)
393 ffurl_write(rtp_handle, buf, len);
396 /* Send a minimal RTCP RR */
397 if (avio_open_dyn_buf(&pb) < 0)
400 avio_w8(pb, (RTP_VERSION << 6));
401 avio_w8(pb, RTCP_RR); /* receiver report */
402 avio_wb16(pb, 1); /* length in words - 1 */
403 avio_wb32(pb, 0); /* our own SSRC */
406 len = avio_close_dyn_buf(pb, &buf);
407 if ((len > 0) && buf)
408 ffurl_write(rtp_handle, buf, len);
412 static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,
413 uint16_t *missing_mask)
416 uint16_t next_seq = s->seq + 1;
417 RTPPacket *pkt = s->queue;
419 if (!pkt || pkt->seq == next_seq)
423 for (i = 1; i <= 16; i++) {
424 uint16_t missing_seq = next_seq + i;
426 int16_t diff = pkt->seq - missing_seq;
433 if (pkt->seq == missing_seq)
435 *missing_mask |= 1 << (i - 1);
438 *first_missing = next_seq;
442 int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd,
445 int len, need_keyframe, missing_packets;
449 uint16_t first_missing = 0, missing_mask = 0;
454 need_keyframe = s->handler && s->handler->need_keyframe &&
455 s->handler->need_keyframe(s->dynamic_protocol_context);
456 missing_packets = find_missing_packets(s, &first_missing, &missing_mask);
458 if (!need_keyframe && !missing_packets)
461 /* Send new feedback if enough time has elapsed since the last
462 * feedback packet. */
464 now = av_gettime_relative();
465 if (s->last_feedback_time &&
466 (now - s->last_feedback_time) < MIN_FEEDBACK_INTERVAL)
468 s->last_feedback_time = now;
472 else if (avio_open_dyn_buf(&pb) < 0)
476 avio_w8(pb, (RTP_VERSION << 6) | 1); /* PLI */
477 avio_w8(pb, RTCP_PSFB);
478 avio_wb16(pb, 2); /* length in words - 1 */
479 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
480 avio_wb32(pb, s->ssrc + 1);
481 avio_wb32(pb, s->ssrc); // server SSRC
484 if (missing_packets) {
485 avio_w8(pb, (RTP_VERSION << 6) | 1); /* NACK */
486 avio_w8(pb, RTCP_RTPFB);
487 avio_wb16(pb, 3); /* length in words - 1 */
488 avio_wb32(pb, s->ssrc + 1);
489 avio_wb32(pb, s->ssrc); // server SSRC
491 avio_wb16(pb, first_missing);
492 avio_wb16(pb, missing_mask);
498 len = avio_close_dyn_buf(pb, &buf);
499 if (len > 0 && buf) {
500 ffurl_write(fd, buf, len);
507 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
510 RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st,
511 int payload_type, int queue_size)
515 s = av_mallocz(sizeof(RTPDemuxContext));
518 s->payload_type = payload_type;
519 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
520 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
523 s->queue_size = queue_size;
525 av_log(s->ic, AV_LOG_VERBOSE, "setting jitter buffer size to %d\n",
528 rtp_init_statistics(&s->statistics, 0);
530 switch (st->codecpar->codec_id) {
531 case AV_CODEC_ID_ADPCM_G722:
532 /* According to RFC 3551, the stream clock rate is 8000
533 * even if the sample rate is 16000. */
534 if (st->codecpar->sample_rate == 8000)
535 st->codecpar->sample_rate = 16000;
541 // needed to send back RTCP RR in RTSP sessions
542 gethostname(s->hostname, sizeof(s->hostname));
546 void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
547 RTPDynamicProtocolHandler *handler)
549 s->dynamic_protocol_context = ctx;
550 s->handler = handler;
553 void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite,
556 if (!ff_srtp_set_crypto(&s->srtp, suite, params))
561 * This was the second switch in rtp_parse packet.
562 * Normalizes time, if required, sets stream_index, etc.
564 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
566 if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
567 return; /* Timestamp already set by depacketizer */
568 if (timestamp == RTP_NOTS_VALUE)
571 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
575 /* compute pts from timestamp with received ntp_time */
576 delta_timestamp = timestamp - s->last_rtcp_timestamp;
577 /* convert to the PTS timebase */
578 addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time,
579 s->st->time_base.den,
580 (uint64_t) s->st->time_base.num << 32);
581 pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
586 if (!s->base_timestamp)
587 s->base_timestamp = timestamp;
588 /* assume that the difference is INT32_MIN < x < INT32_MAX,
589 * but allow the first timestamp to exceed INT32_MAX */
591 s->unwrapped_timestamp += timestamp;
593 s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
594 s->timestamp = timestamp;
595 pkt->pts = s->unwrapped_timestamp + s->range_start_offset -
599 static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
600 const uint8_t *buf, int len)
603 int payload_type, seq, flags = 0;
609 csrc = buf[0] & 0x0f;
611 payload_type = buf[1] & 0x7f;
613 flags |= RTP_FLAG_MARKER;
614 seq = AV_RB16(buf + 2);
615 timestamp = AV_RB32(buf + 4);
616 ssrc = AV_RB32(buf + 8);
617 /* store the ssrc in the RTPDemuxContext */
620 /* NOTE: we can handle only one payload type */
621 if (s->payload_type != payload_type)
625 // only do something with this if all the rtp checks pass...
626 if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) {
627 av_log(s->ic, AV_LOG_ERROR,
628 "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
629 payload_type, seq, ((s->seq + 1) & 0xffff));
634 int padding = buf[len - 1];
635 if (len >= 12 + padding)
646 return AVERROR_INVALIDDATA;
648 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
652 /* calculate the header extension length (stored as number
653 * of 32-bit words) */
654 ext = (AV_RB16(buf + 2) + 1) << 2;
658 // skip past RTP header extension
663 if (s->handler && s->handler->parse_packet) {
664 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
665 s->st, pkt, ×tamp, buf, len, seq,
668 if ((rv = av_new_packet(pkt, len)) < 0)
670 memcpy(pkt->data, buf, len);
671 pkt->stream_index = st->index;
673 return AVERROR(EINVAL);
676 // now perform timestamp things....
677 finalize_packet(s, pkt, timestamp);
682 void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
685 RTPPacket *next = s->queue->next;
686 av_freep(&s->queue->buf);
695 static int enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
697 uint16_t seq = AV_RB16(buf + 2);
698 RTPPacket **cur = &s->queue, *packet;
700 /* Find the correct place in the queue to insert the packet */
702 int16_t diff = seq - (*cur)->seq;
708 packet = av_mallocz(sizeof(*packet));
710 return AVERROR(ENOMEM);
711 packet->recvtime = av_gettime_relative();
722 static int has_next_packet(RTPDemuxContext *s)
724 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
727 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
729 return s->queue ? s->queue->recvtime : 0;
732 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
737 if (s->queue_len <= 0)
740 if (!has_next_packet(s))
741 av_log(s->ic, AV_LOG_WARNING,
742 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
744 /* Parse the first packet in the queue, and dequeue it */
745 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
746 next = s->queue->next;
747 av_freep(&s->queue->buf);
754 static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
755 uint8_t **bufptr, int len)
757 uint8_t *buf = bufptr ? *bufptr : NULL;
763 /* If parsing of the previous packet actually returned 0 or an error,
764 * there's nothing more to be parsed from that packet, but we may have
765 * indicated that we can return the next enqueued packet. */
766 if (s->prev_ret <= 0)
767 return rtp_parse_queued_packet(s, pkt);
768 /* return the next packets, if any */
769 if (s->handler && s->handler->parse_packet) {
770 /* timestamp should be overwritten by parse_packet, if not,
771 * the packet is left with pts == AV_NOPTS_VALUE */
772 timestamp = RTP_NOTS_VALUE;
773 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
774 s->st, pkt, ×tamp, NULL, 0, 0,
776 finalize_packet(s, pkt, timestamp);
784 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
786 if (RTP_PT_IS_RTCP(buf[1])) {
787 return rtcp_parse_packet(s, buf, len);
791 int64_t received = av_gettime_relative();
792 uint32_t arrival_ts = av_rescale_q(received, AV_TIME_BASE_Q,
794 timestamp = AV_RB32(buf + 4);
795 // Calculate the jitter immediately, before queueing the packet
796 // into the reordering queue.
797 rtcp_update_jitter(&s->statistics, timestamp, arrival_ts);
800 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
801 /* First packet, or no reordering */
802 return rtp_parse_packet_internal(s, pkt, buf, len);
804 uint16_t seq = AV_RB16(buf + 2);
805 int16_t diff = seq - s->seq;
807 /* Packet older than the previously emitted one, drop */
808 av_log(s->ic, AV_LOG_WARNING,
809 "RTP: dropping old packet received too late\n");
811 } else if (diff <= 1) {
813 rv = rtp_parse_packet_internal(s, pkt, buf, len);
816 /* Still missing some packet, enqueue this one. */
817 rv = enqueue_packet(s, buf, len);
821 /* Return the first enqueued packet if the queue is full,
822 * even if we're missing something */
823 if (s->queue_len >= s->queue_size) {
824 av_log(s->ic, AV_LOG_WARNING, "jitter buffer full\n");
825 return rtp_parse_queued_packet(s, pkt);
833 * Parse an RTP or RTCP packet directly sent as a buffer.
834 * @param s RTP parse context.
835 * @param pkt returned packet
836 * @param bufptr pointer to the input buffer or NULL to read the next packets
837 * @param len buffer len
838 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
839 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
841 int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
842 uint8_t **bufptr, int len)
845 if (s->srtp_enabled && bufptr && ff_srtp_decrypt(&s->srtp, *bufptr, &len) < 0)
847 rv = rtp_parse_one_packet(s, pkt, bufptr, len);
849 while (rv < 0 && has_next_packet(s))
850 rv = rtp_parse_queued_packet(s, pkt);
851 return rv ? rv : has_next_packet(s);
854 void ff_rtp_parse_close(RTPDemuxContext *s)
856 ff_rtp_reset_packet_queue(s);
857 ff_srtp_free(&s->srtp);
861 int ff_parse_fmtp(AVFormatContext *s,
862 AVStream *stream, PayloadContext *data, const char *p,
863 int (*parse_fmtp)(AVFormatContext *s,
865 PayloadContext *data,
866 const char *attr, const char *value))
871 int value_size = strlen(p) + 1;
873 if (!(value = av_malloc(value_size))) {
874 av_log(s, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n");
875 return AVERROR(ENOMEM);
878 // remove protocol identifier
879 while (*p && *p == ' ')
881 while (*p && *p != ' ')
882 p++; // eat protocol identifier
883 while (*p && *p == ' ')
884 p++; // strip trailing spaces
886 while (ff_rtsp_next_attr_and_value(&p,
888 value, value_size)) {
889 res = parse_fmtp(s, stream, data, attr, value);
890 if (res < 0 && res != AVERROR_PATCHWELCOME) {
899 int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
904 pkt->size = avio_close_dyn_buf(*dyn_buf, &pkt->data);
905 pkt->stream_index = stream_idx;
907 if ((ret = av_packet_from_data(pkt, pkt->data, pkt->size)) < 0) {
908 av_freep(&pkt->data);