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 l24_dynamic_handler = {
38 .codec_type = AVMEDIA_TYPE_AUDIO,
39 .codec_id = AV_CODEC_ID_PCM_S24BE,
42 static RTPDynamicProtocolHandler gsm_dynamic_handler = {
44 .codec_type = AVMEDIA_TYPE_AUDIO,
45 .codec_id = AV_CODEC_ID_GSM,
48 static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = {
49 .enc_name = "X-MP3-draft-00",
50 .codec_type = AVMEDIA_TYPE_AUDIO,
51 .codec_id = AV_CODEC_ID_MP3ADU,
54 static RTPDynamicProtocolHandler speex_dynamic_handler = {
56 .codec_type = AVMEDIA_TYPE_AUDIO,
57 .codec_id = AV_CODEC_ID_SPEEX,
60 static RTPDynamicProtocolHandler opus_dynamic_handler = {
62 .codec_type = AVMEDIA_TYPE_AUDIO,
63 .codec_id = AV_CODEC_ID_OPUS,
66 static RTPDynamicProtocolHandler t140_dynamic_handler = { /* RFC 4103 */
68 .codec_type = AVMEDIA_TYPE_SUBTITLE,
69 .codec_id = AV_CODEC_ID_TEXT,
72 extern RTPDynamicProtocolHandler ff_rdt_video_handler;
73 extern RTPDynamicProtocolHandler ff_rdt_audio_handler;
74 extern RTPDynamicProtocolHandler ff_rdt_live_video_handler;
75 extern RTPDynamicProtocolHandler ff_rdt_live_audio_handler;
77 static const RTPDynamicProtocolHandler *rtp_dynamic_protocol_handler_list[] = {
79 &ff_ac3_dynamic_handler,
80 &ff_amr_nb_dynamic_handler,
81 &ff_amr_wb_dynamic_handler,
82 &ff_dv_dynamic_handler,
83 &ff_g726_16_dynamic_handler,
84 &ff_g726_24_dynamic_handler,
85 &ff_g726_32_dynamic_handler,
86 &ff_g726_40_dynamic_handler,
87 &ff_g726le_16_dynamic_handler,
88 &ff_g726le_24_dynamic_handler,
89 &ff_g726le_32_dynamic_handler,
90 &ff_g726le_40_dynamic_handler,
91 &ff_h261_dynamic_handler,
92 &ff_h263_1998_dynamic_handler,
93 &ff_h263_2000_dynamic_handler,
94 &ff_h263_rfc2190_dynamic_handler,
95 &ff_h264_dynamic_handler,
96 &ff_hevc_dynamic_handler,
97 &ff_ilbc_dynamic_handler,
98 &ff_jpeg_dynamic_handler,
99 &ff_mp4a_latm_dynamic_handler,
100 &ff_mp4v_es_dynamic_handler,
101 &ff_mpeg_audio_dynamic_handler,
102 &ff_mpeg_audio_robust_dynamic_handler,
103 &ff_mpeg_video_dynamic_handler,
104 &ff_mpeg4_generic_dynamic_handler,
105 &ff_mpegts_dynamic_handler,
106 &ff_ms_rtp_asf_pfa_handler,
107 &ff_ms_rtp_asf_pfv_handler,
108 &ff_qcelp_dynamic_handler,
109 &ff_qdm2_dynamic_handler,
110 &ff_qt_rtp_aud_handler,
111 &ff_qt_rtp_vid_handler,
112 &ff_quicktime_rtp_aud_handler,
113 &ff_quicktime_rtp_vid_handler,
114 &ff_rfc4175_rtp_handler,
115 &ff_svq3_dynamic_handler,
116 &ff_theora_dynamic_handler,
117 &ff_vc2hq_dynamic_handler,
118 &ff_vorbis_dynamic_handler,
119 &ff_vp8_dynamic_handler,
120 &ff_vp9_dynamic_handler,
121 &gsm_dynamic_handler,
122 &l24_dynamic_handler,
123 &opus_dynamic_handler,
124 &realmedia_mp3_dynamic_handler,
125 &speex_dynamic_handler,
126 &t140_dynamic_handler,
128 &ff_rdt_video_handler,
129 &ff_rdt_audio_handler,
130 &ff_rdt_live_video_handler,
131 &ff_rdt_live_audio_handler,
135 const RTPDynamicProtocolHandler *ff_rtp_handler_iterate(void **opaque)
137 uintptr_t i = (uintptr_t)*opaque;
138 const RTPDynamicProtocolHandler *r = rtp_dynamic_protocol_handler_list[i];
141 *opaque = (void*)(i + 1);
146 const RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
147 enum AVMediaType codec_type)
150 const RTPDynamicProtocolHandler *handler;
151 while (handler = ff_rtp_handler_iterate(&i)) {
152 if (handler->enc_name &&
153 !av_strcasecmp(name, handler->enc_name) &&
154 codec_type == handler->codec_type)
160 const RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
161 enum AVMediaType codec_type)
164 const RTPDynamicProtocolHandler *handler;
165 while (handler = ff_rtp_handler_iterate(&i)) {
166 if (handler->static_payload_id && handler->static_payload_id == id &&
167 codec_type == handler->codec_type)
173 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
178 payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
182 if (payload_len < 20) {
183 av_log(s->ic, AV_LOG_ERROR, "Invalid RTCP SR packet length\n");
184 return AVERROR_INVALIDDATA;
187 s->last_rtcp_reception_time = av_gettime_relative();
188 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
189 s->last_rtcp_timestamp = AV_RB32(buf + 16);
190 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
191 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
192 if (!s->base_timestamp)
193 s->base_timestamp = s->last_rtcp_timestamp;
194 s->rtcp_ts_offset = (int32_t)(s->last_rtcp_timestamp - s->base_timestamp);
208 #define RTP_SEQ_MOD (1 << 16)
210 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
212 memset(s, 0, sizeof(RTPStatistics));
213 s->max_seq = base_sequence;
218 * Called whenever there is a large jump in sequence numbers,
219 * or when they get out of probation...
221 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
225 s->base_seq = seq - 1;
226 s->bad_seq = RTP_SEQ_MOD + 1;
228 s->expected_prior = 0;
229 s->received_prior = 0;
234 /* Returns 1 if we should handle this packet. */
235 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
237 uint16_t udelta = seq - s->max_seq;
238 const int MAX_DROPOUT = 3000;
239 const int MAX_MISORDER = 100;
240 const int MIN_SEQUENTIAL = 2;
242 /* source not valid until MIN_SEQUENTIAL packets with sequence
243 * seq. numbers have been received */
245 if (seq == s->max_seq + 1) {
248 if (s->probation == 0) {
249 rtp_init_sequence(s, seq);
254 s->probation = MIN_SEQUENTIAL - 1;
257 } else if (udelta < MAX_DROPOUT) {
258 // in order, with permissible gap
259 if (seq < s->max_seq) {
260 // sequence number wrapped; count another 64k cycles
261 s->cycles += RTP_SEQ_MOD;
264 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
265 // sequence made a large jump...
266 if (seq == s->bad_seq) {
267 /* two sequential packets -- assume that the other side
268 * restarted without telling us; just resync. */
269 rtp_init_sequence(s, seq);
271 s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
275 // duplicate or reordered packet...
281 static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp,
282 uint32_t arrival_timestamp)
284 // Most of this is pretty straight from RFC 3550 appendix A.8
285 uint32_t transit = arrival_timestamp - sent_timestamp;
286 uint32_t prev_transit = s->transit;
287 int32_t d = transit - prev_transit;
288 // Doing the FFABS() call directly on the "transit - prev_transit"
289 // expression doesn't work, since it's an unsigned expression. Doing the
290 // transit calculation in unsigned is desired though, since it most
291 // probably will need to wrap around.
293 s->transit = transit;
296 s->jitter += d - (int32_t) ((s->jitter + 8) >> 4);
299 int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
300 AVIOContext *avio, int count)
306 RTPStatistics *stats = &s->statistics;
308 uint32_t extended_max;
309 uint32_t expected_interval;
310 uint32_t received_interval;
311 int32_t lost_interval;
315 if ((!fd && !avio) || (count < 1))
318 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
319 /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
320 s->octet_count += count;
321 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
323 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
326 s->last_octet_count = s->octet_count;
330 else if (avio_open_dyn_buf(&pb) < 0)
334 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
335 avio_w8(pb, RTCP_RR);
336 avio_wb16(pb, 7); /* length in words - 1 */
337 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
338 avio_wb32(pb, s->ssrc + 1);
339 avio_wb32(pb, s->ssrc); // server SSRC
340 // some placeholders we should really fill...
342 extended_max = stats->cycles + stats->max_seq;
343 expected = extended_max - stats->base_seq;
344 lost = expected - stats->received;
345 lost = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
346 expected_interval = expected - stats->expected_prior;
347 stats->expected_prior = expected;
348 received_interval = stats->received - stats->received_prior;
349 stats->received_prior = stats->received;
350 lost_interval = expected_interval - received_interval;
351 if (expected_interval == 0 || lost_interval <= 0)
354 fraction = (lost_interval << 8) / expected_interval;
356 fraction = (fraction << 24) | lost;
358 avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
359 avio_wb32(pb, extended_max); /* max sequence received */
360 avio_wb32(pb, stats->jitter >> 4); /* jitter */
362 if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) {
363 avio_wb32(pb, 0); /* last SR timestamp */
364 avio_wb32(pb, 0); /* delay since last SR */
366 uint32_t middle_32_bits = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
367 uint32_t delay_since_last = av_rescale(av_gettime_relative() - s->last_rtcp_reception_time,
368 65536, AV_TIME_BASE);
370 avio_wb32(pb, middle_32_bits); /* last SR timestamp */
371 avio_wb32(pb, delay_since_last); /* delay since last SR */
375 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
376 avio_w8(pb, RTCP_SDES);
377 len = strlen(s->hostname);
378 avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */
379 avio_wb32(pb, s->ssrc + 1);
382 avio_write(pb, s->hostname, len);
383 avio_w8(pb, 0); /* END */
385 for (len = (7 + len) % 4; len % 4; len++)
391 len = avio_close_dyn_buf(pb, &buf);
392 if ((len > 0) && buf) {
393 int av_unused result;
394 av_log(s->ic, AV_LOG_TRACE, "sending %d bytes of RR\n", len);
395 result = ffurl_write(fd, buf, len);
396 av_log(s->ic, AV_LOG_TRACE, "result from ffurl_write: %d\n", result);
402 void ff_rtp_send_punch_packets(URLContext *rtp_handle)
408 /* Send a small RTP packet */
409 if (avio_open_dyn_buf(&pb) < 0)
412 avio_w8(pb, (RTP_VERSION << 6));
413 avio_w8(pb, 0); /* Payload type */
414 avio_wb16(pb, 0); /* Seq */
415 avio_wb32(pb, 0); /* Timestamp */
416 avio_wb32(pb, 0); /* SSRC */
419 len = avio_close_dyn_buf(pb, &buf);
420 if ((len > 0) && buf)
421 ffurl_write(rtp_handle, buf, len);
424 /* Send a minimal RTCP RR */
425 if (avio_open_dyn_buf(&pb) < 0)
428 avio_w8(pb, (RTP_VERSION << 6));
429 avio_w8(pb, RTCP_RR); /* receiver report */
430 avio_wb16(pb, 1); /* length in words - 1 */
431 avio_wb32(pb, 0); /* our own SSRC */
434 len = avio_close_dyn_buf(pb, &buf);
435 if ((len > 0) && buf)
436 ffurl_write(rtp_handle, buf, len);
440 static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,
441 uint16_t *missing_mask)
444 uint16_t next_seq = s->seq + 1;
445 RTPPacket *pkt = s->queue;
447 if (!pkt || pkt->seq == next_seq)
451 for (i = 1; i <= 16; i++) {
452 uint16_t missing_seq = next_seq + i;
454 int16_t diff = pkt->seq - missing_seq;
461 if (pkt->seq == missing_seq)
463 *missing_mask |= 1 << (i - 1);
466 *first_missing = next_seq;
470 int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd,
473 int len, need_keyframe, missing_packets;
477 uint16_t first_missing = 0, missing_mask = 0;
482 need_keyframe = s->handler && s->handler->need_keyframe &&
483 s->handler->need_keyframe(s->dynamic_protocol_context);
484 missing_packets = find_missing_packets(s, &first_missing, &missing_mask);
486 if (!need_keyframe && !missing_packets)
489 /* Send new feedback if enough time has elapsed since the last
490 * feedback packet. */
492 now = av_gettime_relative();
493 if (s->last_feedback_time &&
494 (now - s->last_feedback_time) < MIN_FEEDBACK_INTERVAL)
496 s->last_feedback_time = now;
500 else if (avio_open_dyn_buf(&pb) < 0)
504 avio_w8(pb, (RTP_VERSION << 6) | 1); /* PLI */
505 avio_w8(pb, RTCP_PSFB);
506 avio_wb16(pb, 2); /* length in words - 1 */
507 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
508 avio_wb32(pb, s->ssrc + 1);
509 avio_wb32(pb, s->ssrc); // server SSRC
512 if (missing_packets) {
513 avio_w8(pb, (RTP_VERSION << 6) | 1); /* NACK */
514 avio_w8(pb, RTCP_RTPFB);
515 avio_wb16(pb, 3); /* length in words - 1 */
516 avio_wb32(pb, s->ssrc + 1);
517 avio_wb32(pb, s->ssrc); // server SSRC
519 avio_wb16(pb, first_missing);
520 avio_wb16(pb, missing_mask);
526 len = avio_close_dyn_buf(pb, &buf);
527 if (len > 0 && buf) {
528 ffurl_write(fd, buf, len);
535 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
538 RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st,
539 int payload_type, int queue_size)
543 s = av_mallocz(sizeof(RTPDemuxContext));
546 s->payload_type = payload_type;
547 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
548 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
551 s->queue_size = queue_size;
553 av_log(s->ic, AV_LOG_VERBOSE, "setting jitter buffer size to %d\n",
556 rtp_init_statistics(&s->statistics, 0);
558 switch (st->codecpar->codec_id) {
559 case AV_CODEC_ID_ADPCM_G722:
560 /* According to RFC 3551, the stream clock rate is 8000
561 * even if the sample rate is 16000. */
562 if (st->codecpar->sample_rate == 8000)
563 st->codecpar->sample_rate = 16000;
569 // needed to send back RTCP RR in RTSP sessions
570 gethostname(s->hostname, sizeof(s->hostname));
574 void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
575 const RTPDynamicProtocolHandler *handler)
577 s->dynamic_protocol_context = ctx;
578 s->handler = handler;
581 void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite,
584 if (!ff_srtp_set_crypto(&s->srtp, suite, params))
589 * This was the second switch in rtp_parse packet.
590 * Normalizes time, if required, sets stream_index, etc.
592 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
594 if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
595 return; /* Timestamp already set by depacketizer */
596 if (timestamp == RTP_NOTS_VALUE)
599 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
603 /* compute pts from timestamp with received ntp_time */
604 delta_timestamp = timestamp - s->last_rtcp_timestamp;
605 /* convert to the PTS timebase */
606 addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time,
607 s->st->time_base.den,
608 (uint64_t) s->st->time_base.num << 32);
609 pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
614 if (!s->base_timestamp)
615 s->base_timestamp = timestamp;
616 /* assume that the difference is INT32_MIN < x < INT32_MAX,
617 * but allow the first timestamp to exceed INT32_MAX */
619 s->unwrapped_timestamp += timestamp;
621 s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
622 s->timestamp = timestamp;
623 pkt->pts = s->unwrapped_timestamp + s->range_start_offset -
627 static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
628 const uint8_t *buf, int len)
631 int payload_type, seq, flags = 0;
637 csrc = buf[0] & 0x0f;
639 payload_type = buf[1] & 0x7f;
641 flags |= RTP_FLAG_MARKER;
642 seq = AV_RB16(buf + 2);
643 timestamp = AV_RB32(buf + 4);
644 ssrc = AV_RB32(buf + 8);
645 /* store the ssrc in the RTPDemuxContext */
648 /* NOTE: we can handle only one payload type */
649 if (s->payload_type != payload_type)
653 // only do something with this if all the rtp checks pass...
654 if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) {
655 av_log(s->ic, AV_LOG_ERROR,
656 "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
657 payload_type, seq, ((s->seq + 1) & 0xffff));
662 int padding = buf[len - 1];
663 if (len >= 12 + padding)
674 return AVERROR_INVALIDDATA;
676 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
680 /* calculate the header extension length (stored as number
681 * of 32-bit words) */
682 ext = (AV_RB16(buf + 2) + 1) << 2;
686 // skip past RTP header extension
691 if (s->handler && s->handler->parse_packet) {
692 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
693 s->st, pkt, ×tamp, buf, len, seq,
696 if ((rv = av_new_packet(pkt, len)) < 0)
698 memcpy(pkt->data, buf, len);
699 pkt->stream_index = st->index;
701 return AVERROR(EINVAL);
704 // now perform timestamp things....
705 finalize_packet(s, pkt, timestamp);
710 void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
713 RTPPacket *next = s->queue->next;
714 av_freep(&s->queue->buf);
723 static int enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
725 uint16_t seq = AV_RB16(buf + 2);
726 RTPPacket **cur = &s->queue, *packet;
728 /* Find the correct place in the queue to insert the packet */
730 int16_t diff = seq - (*cur)->seq;
736 packet = av_mallocz(sizeof(*packet));
738 return AVERROR(ENOMEM);
739 packet->recvtime = av_gettime_relative();
750 static int has_next_packet(RTPDemuxContext *s)
752 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
755 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
757 return s->queue ? s->queue->recvtime : 0;
760 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
765 if (s->queue_len <= 0)
768 if (!has_next_packet(s))
769 av_log(s->ic, AV_LOG_WARNING,
770 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
772 /* Parse the first packet in the queue, and dequeue it */
773 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
774 next = s->queue->next;
775 av_freep(&s->queue->buf);
782 static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
783 uint8_t **bufptr, int len)
785 uint8_t *buf = bufptr ? *bufptr : NULL;
791 /* If parsing of the previous packet actually returned 0 or an error,
792 * there's nothing more to be parsed from that packet, but we may have
793 * indicated that we can return the next enqueued packet. */
794 if (s->prev_ret <= 0)
795 return rtp_parse_queued_packet(s, pkt);
796 /* return the next packets, if any */
797 if (s->handler && s->handler->parse_packet) {
798 /* timestamp should be overwritten by parse_packet, if not,
799 * the packet is left with pts == AV_NOPTS_VALUE */
800 timestamp = RTP_NOTS_VALUE;
801 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
802 s->st, pkt, ×tamp, NULL, 0, 0,
804 finalize_packet(s, pkt, timestamp);
812 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
814 if (RTP_PT_IS_RTCP(buf[1])) {
815 return rtcp_parse_packet(s, buf, len);
819 int64_t received = av_gettime_relative();
820 uint32_t arrival_ts = av_rescale_q(received, AV_TIME_BASE_Q,
822 timestamp = AV_RB32(buf + 4);
823 // Calculate the jitter immediately, before queueing the packet
824 // into the reordering queue.
825 rtcp_update_jitter(&s->statistics, timestamp, arrival_ts);
828 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
829 /* First packet, or no reordering */
830 return rtp_parse_packet_internal(s, pkt, buf, len);
832 uint16_t seq = AV_RB16(buf + 2);
833 int16_t diff = seq - s->seq;
835 /* Packet older than the previously emitted one, drop */
836 av_log(s->ic, AV_LOG_WARNING,
837 "RTP: dropping old packet received too late\n");
839 } else if (diff <= 1) {
841 rv = rtp_parse_packet_internal(s, pkt, buf, len);
844 /* Still missing some packet, enqueue this one. */
845 rv = enqueue_packet(s, buf, len);
849 /* Return the first enqueued packet if the queue is full,
850 * even if we're missing something */
851 if (s->queue_len >= s->queue_size) {
852 av_log(s->ic, AV_LOG_WARNING, "jitter buffer full\n");
853 return rtp_parse_queued_packet(s, pkt);
861 * Parse an RTP or RTCP packet directly sent as a buffer.
862 * @param s RTP parse context.
863 * @param pkt returned packet
864 * @param bufptr pointer to the input buffer or NULL to read the next packets
865 * @param len buffer len
866 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
867 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
869 int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
870 uint8_t **bufptr, int len)
873 if (s->srtp_enabled && bufptr && ff_srtp_decrypt(&s->srtp, *bufptr, &len) < 0)
875 rv = rtp_parse_one_packet(s, pkt, bufptr, len);
877 while (rv < 0 && has_next_packet(s))
878 rv = rtp_parse_queued_packet(s, pkt);
879 return rv ? rv : has_next_packet(s);
882 void ff_rtp_parse_close(RTPDemuxContext *s)
884 ff_rtp_reset_packet_queue(s);
885 ff_srtp_free(&s->srtp);
889 int ff_parse_fmtp(AVFormatContext *s,
890 AVStream *stream, PayloadContext *data, const char *p,
891 int (*parse_fmtp)(AVFormatContext *s,
893 PayloadContext *data,
894 const char *attr, const char *value))
899 int value_size = strlen(p) + 1;
901 if (!(value = av_malloc(value_size))) {
902 av_log(s, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n");
903 return AVERROR(ENOMEM);
906 // remove protocol identifier
907 while (*p && *p == ' ')
909 while (*p && *p != ' ')
910 p++; // eat protocol identifier
911 while (*p && *p == ' ')
912 p++; // strip trailing spaces
914 while (ff_rtsp_next_attr_and_value(&p,
916 value, value_size)) {
917 res = parse_fmtp(s, stream, data, attr, value);
918 if (res < 0 && res != AVERROR_PATCHWELCOME) {
927 int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
932 pkt->size = avio_close_dyn_buf(*dyn_buf, &pkt->data);
933 pkt->stream_index = stream_idx;
935 if ((ret = av_packet_from_data(pkt, pkt->data, pkt->size)) < 0) {
936 av_freep(&pkt->data);