3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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/time.h"
25 #include "libavcodec/get_bits.h"
31 #include "rtpdec_formats.h"
33 #define MIN_FEEDBACK_INTERVAL 200000 /* 200 ms in us */
35 static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = {
36 .enc_name = "X-MP3-draft-00",
37 .codec_type = AVMEDIA_TYPE_AUDIO,
38 .codec_id = AV_CODEC_ID_MP3ADU,
41 static RTPDynamicProtocolHandler speex_dynamic_handler = {
43 .codec_type = AVMEDIA_TYPE_AUDIO,
44 .codec_id = AV_CODEC_ID_SPEEX,
47 static RTPDynamicProtocolHandler opus_dynamic_handler = {
49 .codec_type = AVMEDIA_TYPE_AUDIO,
50 .codec_id = AV_CODEC_ID_OPUS,
53 static RTPDynamicProtocolHandler t140_dynamic_handler = { /* RFC 4103 */
55 .codec_type = AVMEDIA_TYPE_DATA,
56 .codec_id = AV_CODEC_ID_TEXT,
59 static RTPDynamicProtocolHandler *rtp_first_dynamic_payload_handler = NULL;
61 void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
63 handler->next = rtp_first_dynamic_payload_handler;
64 rtp_first_dynamic_payload_handler = handler;
67 void ff_register_rtp_dynamic_payload_handlers(void)
69 ff_register_dynamic_payload_handler(&ff_ac3_dynamic_handler);
70 ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
71 ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
72 ff_register_dynamic_payload_handler(&ff_dv_dynamic_handler);
73 ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
74 ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
75 ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
76 ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
77 ff_register_dynamic_payload_handler(&ff_h261_dynamic_handler);
78 ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
79 ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
80 ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
81 ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
82 ff_register_dynamic_payload_handler(&ff_hevc_dynamic_handler);
83 ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler);
84 ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler);
85 ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
86 ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
87 ff_register_dynamic_payload_handler(&ff_mpeg_audio_dynamic_handler);
88 ff_register_dynamic_payload_handler(&ff_mpeg_audio_robust_dynamic_handler);
89 ff_register_dynamic_payload_handler(&ff_mpeg_video_dynamic_handler);
90 ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
91 ff_register_dynamic_payload_handler(&ff_mpegts_dynamic_handler);
92 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
93 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
94 ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
95 ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
96 ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
97 ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
98 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
99 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
100 ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
101 ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
102 ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
103 ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
104 ff_register_dynamic_payload_handler(&ff_vp9_dynamic_handler);
105 ff_register_dynamic_payload_handler(&opus_dynamic_handler);
106 ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
107 ff_register_dynamic_payload_handler(&speex_dynamic_handler);
108 ff_register_dynamic_payload_handler(&t140_dynamic_handler);
111 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
112 enum AVMediaType codec_type)
114 RTPDynamicProtocolHandler *handler;
115 for (handler = rtp_first_dynamic_payload_handler;
116 handler; handler = handler->next)
117 if (handler->enc_name &&
118 !av_strcasecmp(name, handler->enc_name) &&
119 codec_type == handler->codec_type)
124 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
125 enum AVMediaType codec_type)
127 RTPDynamicProtocolHandler *handler;
128 for (handler = rtp_first_dynamic_payload_handler;
129 handler; handler = handler->next)
130 if (handler->static_payload_id && handler->static_payload_id == id &&
131 codec_type == handler->codec_type)
136 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
141 payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
145 if (payload_len < 20) {
146 av_log(s->ic, AV_LOG_ERROR, "Invalid RTCP SR packet length\n");
147 return AVERROR_INVALIDDATA;
150 s->last_rtcp_reception_time = av_gettime_relative();
151 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
152 s->last_rtcp_timestamp = AV_RB32(buf + 16);
153 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
154 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
155 if (!s->base_timestamp)
156 s->base_timestamp = s->last_rtcp_timestamp;
157 s->rtcp_ts_offset = (int32_t)(s->last_rtcp_timestamp - s->base_timestamp);
171 #define RTP_SEQ_MOD (1 << 16)
173 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
175 memset(s, 0, sizeof(RTPStatistics));
176 s->max_seq = base_sequence;
181 * Called whenever there is a large jump in sequence numbers,
182 * or when they get out of probation...
184 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
188 s->base_seq = seq - 1;
189 s->bad_seq = RTP_SEQ_MOD + 1;
191 s->expected_prior = 0;
192 s->received_prior = 0;
197 /* Returns 1 if we should handle this packet. */
198 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
200 uint16_t udelta = seq - s->max_seq;
201 const int MAX_DROPOUT = 3000;
202 const int MAX_MISORDER = 100;
203 const int MIN_SEQUENTIAL = 2;
205 /* source not valid until MIN_SEQUENTIAL packets with sequence
206 * seq. numbers have been received */
208 if (seq == s->max_seq + 1) {
211 if (s->probation == 0) {
212 rtp_init_sequence(s, seq);
217 s->probation = MIN_SEQUENTIAL - 1;
220 } else if (udelta < MAX_DROPOUT) {
221 // in order, with permissible gap
222 if (seq < s->max_seq) {
223 // sequence number wrapped; count another 64k cycles
224 s->cycles += RTP_SEQ_MOD;
227 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
228 // sequence made a large jump...
229 if (seq == s->bad_seq) {
230 /* two sequential packets -- assume that the other side
231 * restarted without telling us; just resync. */
232 rtp_init_sequence(s, seq);
234 s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
238 // duplicate or reordered packet...
244 static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp,
245 uint32_t arrival_timestamp)
247 // Most of this is pretty straight from RFC 3550 appendix A.8
248 uint32_t transit = arrival_timestamp - sent_timestamp;
249 uint32_t prev_transit = s->transit;
250 int32_t d = transit - prev_transit;
251 // Doing the FFABS() call directly on the "transit - prev_transit"
252 // expression doesn't work, since it's an unsigned expression. Doing the
253 // transit calculation in unsigned is desired though, since it most
254 // probably will need to wrap around.
256 s->transit = transit;
259 s->jitter += d - (int32_t) ((s->jitter + 8) >> 4);
262 int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
263 AVIOContext *avio, int count)
269 RTPStatistics *stats = &s->statistics;
271 uint32_t extended_max;
272 uint32_t expected_interval;
273 uint32_t received_interval;
274 int32_t lost_interval;
278 if ((!fd && !avio) || (count < 1))
281 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
282 /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
283 s->octet_count += count;
284 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
286 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
289 s->last_octet_count = s->octet_count;
293 else if (avio_open_dyn_buf(&pb) < 0)
297 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
298 avio_w8(pb, RTCP_RR);
299 avio_wb16(pb, 7); /* length in words - 1 */
300 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
301 avio_wb32(pb, s->ssrc + 1);
302 avio_wb32(pb, s->ssrc); // server SSRC
303 // some placeholders we should really fill...
305 extended_max = stats->cycles + stats->max_seq;
306 expected = extended_max - stats->base_seq;
307 lost = expected - stats->received;
308 lost = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
309 expected_interval = expected - stats->expected_prior;
310 stats->expected_prior = expected;
311 received_interval = stats->received - stats->received_prior;
312 stats->received_prior = stats->received;
313 lost_interval = expected_interval - received_interval;
314 if (expected_interval == 0 || lost_interval <= 0)
317 fraction = (lost_interval << 8) / expected_interval;
319 fraction = (fraction << 24) | lost;
321 avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
322 avio_wb32(pb, extended_max); /* max sequence received */
323 avio_wb32(pb, stats->jitter >> 4); /* jitter */
325 if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) {
326 avio_wb32(pb, 0); /* last SR timestamp */
327 avio_wb32(pb, 0); /* delay since last SR */
329 uint32_t middle_32_bits = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
330 uint32_t delay_since_last = av_rescale(av_gettime_relative() - s->last_rtcp_reception_time,
331 65536, AV_TIME_BASE);
333 avio_wb32(pb, middle_32_bits); /* last SR timestamp */
334 avio_wb32(pb, delay_since_last); /* delay since last SR */
338 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
339 avio_w8(pb, RTCP_SDES);
340 len = strlen(s->hostname);
341 avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */
342 avio_wb32(pb, s->ssrc + 1);
345 avio_write(pb, s->hostname, len);
346 avio_w8(pb, 0); /* END */
348 for (len = (7 + len) % 4; len % 4; len++)
354 len = avio_close_dyn_buf(pb, &buf);
355 if ((len > 0) && buf) {
356 int av_unused result;
357 av_log(s->ic, AV_LOG_TRACE, "sending %d bytes of RR\n", len);
358 result = ffurl_write(fd, buf, len);
359 av_log(s->ic, AV_LOG_TRACE, "result from ffurl_write: %d\n", result);
365 void ff_rtp_send_punch_packets(URLContext *rtp_handle)
371 /* Send a small RTP packet */
372 if (avio_open_dyn_buf(&pb) < 0)
375 avio_w8(pb, (RTP_VERSION << 6));
376 avio_w8(pb, 0); /* Payload type */
377 avio_wb16(pb, 0); /* Seq */
378 avio_wb32(pb, 0); /* Timestamp */
379 avio_wb32(pb, 0); /* SSRC */
382 len = avio_close_dyn_buf(pb, &buf);
383 if ((len > 0) && buf)
384 ffurl_write(rtp_handle, buf, len);
387 /* Send a minimal RTCP RR */
388 if (avio_open_dyn_buf(&pb) < 0)
391 avio_w8(pb, (RTP_VERSION << 6));
392 avio_w8(pb, RTCP_RR); /* receiver report */
393 avio_wb16(pb, 1); /* length in words - 1 */
394 avio_wb32(pb, 0); /* our own SSRC */
397 len = avio_close_dyn_buf(pb, &buf);
398 if ((len > 0) && buf)
399 ffurl_write(rtp_handle, buf, len);
403 static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,
404 uint16_t *missing_mask)
407 uint16_t next_seq = s->seq + 1;
408 RTPPacket *pkt = s->queue;
410 if (!pkt || pkt->seq == next_seq)
414 for (i = 1; i <= 16; i++) {
415 uint16_t missing_seq = next_seq + i;
417 int16_t diff = pkt->seq - missing_seq;
424 if (pkt->seq == missing_seq)
426 *missing_mask |= 1 << (i - 1);
429 *first_missing = next_seq;
433 int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd,
436 int len, need_keyframe, missing_packets;
440 uint16_t first_missing = 0, missing_mask = 0;
445 need_keyframe = s->handler && s->handler->need_keyframe &&
446 s->handler->need_keyframe(s->dynamic_protocol_context);
447 missing_packets = find_missing_packets(s, &first_missing, &missing_mask);
449 if (!need_keyframe && !missing_packets)
452 /* Send new feedback if enough time has elapsed since the last
453 * feedback packet. */
455 now = av_gettime_relative();
456 if (s->last_feedback_time &&
457 (now - s->last_feedback_time) < MIN_FEEDBACK_INTERVAL)
459 s->last_feedback_time = now;
463 else if (avio_open_dyn_buf(&pb) < 0)
467 avio_w8(pb, (RTP_VERSION << 6) | 1); /* PLI */
468 avio_w8(pb, RTCP_PSFB);
469 avio_wb16(pb, 2); /* length in words - 1 */
470 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
471 avio_wb32(pb, s->ssrc + 1);
472 avio_wb32(pb, s->ssrc); // server SSRC
475 if (missing_packets) {
476 avio_w8(pb, (RTP_VERSION << 6) | 1); /* NACK */
477 avio_w8(pb, RTCP_RTPFB);
478 avio_wb16(pb, 3); /* length in words - 1 */
479 avio_wb32(pb, s->ssrc + 1);
480 avio_wb32(pb, s->ssrc); // server SSRC
482 avio_wb16(pb, first_missing);
483 avio_wb16(pb, missing_mask);
489 len = avio_close_dyn_buf(pb, &buf);
490 if (len > 0 && buf) {
491 ffurl_write(fd, buf, len);
498 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
501 RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st,
502 int payload_type, int queue_size)
506 s = av_mallocz(sizeof(RTPDemuxContext));
509 s->payload_type = payload_type;
510 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
511 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
514 s->queue_size = queue_size;
516 av_log(s->ic, AV_LOG_VERBOSE, "setting jitter buffer size to %d\n",
519 rtp_init_statistics(&s->statistics, 0);
521 switch (st->codecpar->codec_id) {
522 case AV_CODEC_ID_ADPCM_G722:
523 /* According to RFC 3551, the stream clock rate is 8000
524 * even if the sample rate is 16000. */
525 if (st->codecpar->sample_rate == 8000)
526 st->codecpar->sample_rate = 16000;
532 // needed to send back RTCP RR in RTSP sessions
533 gethostname(s->hostname, sizeof(s->hostname));
537 void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
538 RTPDynamicProtocolHandler *handler)
540 s->dynamic_protocol_context = ctx;
541 s->handler = handler;
544 void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite,
547 if (!ff_srtp_set_crypto(&s->srtp, suite, params))
552 * This was the second switch in rtp_parse packet.
553 * Normalizes time, if required, sets stream_index, etc.
555 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
557 if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
558 return; /* Timestamp already set by depacketizer */
559 if (timestamp == RTP_NOTS_VALUE)
562 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
566 /* compute pts from timestamp with received ntp_time */
567 delta_timestamp = timestamp - s->last_rtcp_timestamp;
568 /* convert to the PTS timebase */
569 addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time,
570 s->st->time_base.den,
571 (uint64_t) s->st->time_base.num << 32);
572 pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
577 if (!s->base_timestamp)
578 s->base_timestamp = timestamp;
579 /* assume that the difference is INT32_MIN < x < INT32_MAX,
580 * but allow the first timestamp to exceed INT32_MAX */
582 s->unwrapped_timestamp += timestamp;
584 s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
585 s->timestamp = timestamp;
586 pkt->pts = s->unwrapped_timestamp + s->range_start_offset -
590 static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
591 const uint8_t *buf, int len)
594 int payload_type, seq, flags = 0;
600 csrc = buf[0] & 0x0f;
602 payload_type = buf[1] & 0x7f;
604 flags |= RTP_FLAG_MARKER;
605 seq = AV_RB16(buf + 2);
606 timestamp = AV_RB32(buf + 4);
607 ssrc = AV_RB32(buf + 8);
608 /* store the ssrc in the RTPDemuxContext */
611 /* NOTE: we can handle only one payload type */
612 if (s->payload_type != payload_type)
616 // only do something with this if all the rtp checks pass...
617 if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) {
618 av_log(s->ic, AV_LOG_ERROR,
619 "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
620 payload_type, seq, ((s->seq + 1) & 0xffff));
625 int padding = buf[len - 1];
626 if (len >= 12 + padding)
637 return AVERROR_INVALIDDATA;
639 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
643 /* calculate the header extension length (stored as number
644 * of 32-bit words) */
645 ext = (AV_RB16(buf + 2) + 1) << 2;
649 // skip past RTP header extension
654 if (s->handler && s->handler->parse_packet) {
655 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
656 s->st, pkt, ×tamp, buf, len, seq,
659 if ((rv = av_new_packet(pkt, len)) < 0)
661 memcpy(pkt->data, buf, len);
662 pkt->stream_index = st->index;
664 return AVERROR(EINVAL);
667 // now perform timestamp things....
668 finalize_packet(s, pkt, timestamp);
673 void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
676 RTPPacket *next = s->queue->next;
677 av_free(s->queue->buf);
686 static int enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
688 uint16_t seq = AV_RB16(buf + 2);
689 RTPPacket **cur = &s->queue, *packet;
691 /* Find the correct place in the queue to insert the packet */
693 int16_t diff = seq - (*cur)->seq;
699 packet = av_mallocz(sizeof(*packet));
701 return AVERROR(ENOMEM);
702 packet->recvtime = av_gettime_relative();
713 static int has_next_packet(RTPDemuxContext *s)
715 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
718 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
720 return s->queue ? s->queue->recvtime : 0;
723 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
728 if (s->queue_len <= 0)
731 if (!has_next_packet(s))
732 av_log(s->ic, AV_LOG_WARNING,
733 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
735 /* Parse the first packet in the queue, and dequeue it */
736 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
737 next = s->queue->next;
738 av_free(s->queue->buf);
745 static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
746 uint8_t **bufptr, int len)
748 uint8_t *buf = bufptr ? *bufptr : NULL;
754 /* If parsing of the previous packet actually returned 0 or an error,
755 * there's nothing more to be parsed from that packet, but we may have
756 * indicated that we can return the next enqueued packet. */
757 if (s->prev_ret <= 0)
758 return rtp_parse_queued_packet(s, pkt);
759 /* return the next packets, if any */
760 if (s->handler && s->handler->parse_packet) {
761 /* timestamp should be overwritten by parse_packet, if not,
762 * the packet is left with pts == AV_NOPTS_VALUE */
763 timestamp = RTP_NOTS_VALUE;
764 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
765 s->st, pkt, ×tamp, NULL, 0, 0,
767 finalize_packet(s, pkt, timestamp);
775 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
777 if (RTP_PT_IS_RTCP(buf[1])) {
778 return rtcp_parse_packet(s, buf, len);
782 int64_t received = av_gettime_relative();
783 uint32_t arrival_ts = av_rescale_q(received, AV_TIME_BASE_Q,
785 timestamp = AV_RB32(buf + 4);
786 // Calculate the jitter immediately, before queueing the packet
787 // into the reordering queue.
788 rtcp_update_jitter(&s->statistics, timestamp, arrival_ts);
791 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
792 /* First packet, or no reordering */
793 return rtp_parse_packet_internal(s, pkt, buf, len);
795 uint16_t seq = AV_RB16(buf + 2);
796 int16_t diff = seq - s->seq;
798 /* Packet older than the previously emitted one, drop */
799 av_log(s->ic, AV_LOG_WARNING,
800 "RTP: dropping old packet received too late\n");
802 } else if (diff <= 1) {
804 rv = rtp_parse_packet_internal(s, pkt, buf, len);
807 /* Still missing some packet, enqueue this one. */
808 rv = enqueue_packet(s, buf, len);
812 /* Return the first enqueued packet if the queue is full,
813 * even if we're missing something */
814 if (s->queue_len >= s->queue_size) {
815 av_log(s->ic, AV_LOG_WARNING, "jitter buffer full\n");
816 return rtp_parse_queued_packet(s, pkt);
824 * Parse an RTP or RTCP packet directly sent as a buffer.
825 * @param s RTP parse context.
826 * @param pkt returned packet
827 * @param bufptr pointer to the input buffer or NULL to read the next packets
828 * @param len buffer len
829 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
830 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
832 int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
833 uint8_t **bufptr, int len)
836 if (s->srtp_enabled && bufptr && ff_srtp_decrypt(&s->srtp, *bufptr, &len) < 0)
838 rv = rtp_parse_one_packet(s, pkt, bufptr, len);
840 while (rv == AVERROR(EAGAIN) && has_next_packet(s))
841 rv = rtp_parse_queued_packet(s, pkt);
842 return rv ? rv : has_next_packet(s);
845 void ff_rtp_parse_close(RTPDemuxContext *s)
847 ff_rtp_reset_packet_queue(s);
848 ff_srtp_free(&s->srtp);
852 int ff_parse_fmtp(AVFormatContext *s,
853 AVStream *stream, PayloadContext *data, const char *p,
854 int (*parse_fmtp)(AVFormatContext *s,
856 PayloadContext *data,
857 const char *attr, const char *value))
862 int value_size = strlen(p) + 1;
864 if (!(value = av_malloc(value_size))) {
865 av_log(s, AV_LOG_ERROR, "Failed to allocate data for FMTP.");
866 return AVERROR(ENOMEM);
869 // remove protocol identifier
870 while (*p && *p == ' ')
872 while (*p && *p != ' ')
873 p++; // eat protocol identifier
874 while (*p && *p == ' ')
875 p++; // strip trailing spaces
877 while (ff_rtsp_next_attr_and_value(&p,
879 value, value_size)) {
880 res = parse_fmtp(s, stream, data, attr, value);
881 if (res < 0 && res != AVERROR_PATCHWELCOME) {
890 int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
895 pkt->size = avio_close_dyn_buf(*dyn_buf, &pkt->data);
896 pkt->stream_index = stream_idx;
898 if ((ret = av_packet_from_data(pkt, pkt->data, pkt->size)) < 0) {
899 av_freep(&pkt->data);