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/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 gsm_dynamic_handler = {
37 .codec_type = AVMEDIA_TYPE_AUDIO,
38 .codec_id = AV_CODEC_ID_GSM,
41 static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = {
42 .enc_name = "X-MP3-draft-00",
43 .codec_type = AVMEDIA_TYPE_AUDIO,
44 .codec_id = AV_CODEC_ID_MP3ADU,
47 static RTPDynamicProtocolHandler speex_dynamic_handler = {
49 .codec_type = AVMEDIA_TYPE_AUDIO,
50 .codec_id = AV_CODEC_ID_SPEEX,
53 static RTPDynamicProtocolHandler opus_dynamic_handler = {
55 .codec_type = AVMEDIA_TYPE_AUDIO,
56 .codec_id = AV_CODEC_ID_OPUS,
59 static RTPDynamicProtocolHandler t140_dynamic_handler = { /* RFC 4103 */
61 .codec_type = AVMEDIA_TYPE_SUBTITLE,
62 .codec_id = AV_CODEC_ID_TEXT,
65 static RTPDynamicProtocolHandler *rtp_first_dynamic_payload_handler = NULL;
67 void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
69 handler->next = rtp_first_dynamic_payload_handler;
70 rtp_first_dynamic_payload_handler = handler;
73 void ff_register_rtp_dynamic_payload_handlers(void)
75 ff_register_dynamic_payload_handler(&ff_ac3_dynamic_handler);
76 ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
77 ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
78 ff_register_dynamic_payload_handler(&ff_dv_dynamic_handler);
79 ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
80 ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
81 ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
82 ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
83 ff_register_dynamic_payload_handler(&ff_h261_dynamic_handler);
84 ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
85 ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
86 ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
87 ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
88 ff_register_dynamic_payload_handler(&ff_hevc_dynamic_handler);
89 ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler);
90 ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler);
91 ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
92 ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
93 ff_register_dynamic_payload_handler(&ff_mpeg_audio_dynamic_handler);
94 ff_register_dynamic_payload_handler(&ff_mpeg_audio_robust_dynamic_handler);
95 ff_register_dynamic_payload_handler(&ff_mpeg_video_dynamic_handler);
96 ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
97 ff_register_dynamic_payload_handler(&ff_mpegts_dynamic_handler);
98 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
99 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
100 ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
101 ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
102 ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
103 ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
104 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
105 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
106 ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
107 ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
108 ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
109 ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
110 ff_register_dynamic_payload_handler(&ff_vp9_dynamic_handler);
111 ff_register_dynamic_payload_handler(&gsm_dynamic_handler);
112 ff_register_dynamic_payload_handler(&opus_dynamic_handler);
113 ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
114 ff_register_dynamic_payload_handler(&speex_dynamic_handler);
115 ff_register_dynamic_payload_handler(&t140_dynamic_handler);
118 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
119 enum AVMediaType codec_type)
121 RTPDynamicProtocolHandler *handler;
122 for (handler = rtp_first_dynamic_payload_handler;
123 handler; handler = handler->next)
124 if (handler->enc_name &&
125 !av_strcasecmp(name, handler->enc_name) &&
126 codec_type == handler->codec_type)
131 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
132 enum AVMediaType codec_type)
134 RTPDynamicProtocolHandler *handler;
135 for (handler = rtp_first_dynamic_payload_handler;
136 handler; handler = handler->next)
137 if (handler->static_payload_id && handler->static_payload_id == id &&
138 codec_type == handler->codec_type)
143 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
148 payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
152 if (payload_len < 20) {
153 av_log(NULL, AV_LOG_ERROR,
154 "Invalid length for RTCP SR packet\n");
155 return AVERROR_INVALIDDATA;
158 s->last_rtcp_reception_time = av_gettime_relative();
159 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
160 s->last_rtcp_timestamp = AV_RB32(buf + 16);
161 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
162 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
163 if (!s->base_timestamp)
164 s->base_timestamp = s->last_rtcp_timestamp;
165 s->rtcp_ts_offset = (int32_t)(s->last_rtcp_timestamp - s->base_timestamp);
179 #define RTP_SEQ_MOD (1 << 16)
181 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
183 memset(s, 0, sizeof(RTPStatistics));
184 s->max_seq = base_sequence;
189 * Called whenever there is a large jump in sequence numbers,
190 * or when they get out of probation...
192 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
196 s->base_seq = seq - 1;
197 s->bad_seq = RTP_SEQ_MOD + 1;
199 s->expected_prior = 0;
200 s->received_prior = 0;
205 /* Returns 1 if we should handle this packet. */
206 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
208 uint16_t udelta = seq - s->max_seq;
209 const int MAX_DROPOUT = 3000;
210 const int MAX_MISORDER = 100;
211 const int MIN_SEQUENTIAL = 2;
213 /* source not valid until MIN_SEQUENTIAL packets with sequence
214 * seq. numbers have been received */
216 if (seq == s->max_seq + 1) {
219 if (s->probation == 0) {
220 rtp_init_sequence(s, seq);
225 s->probation = MIN_SEQUENTIAL - 1;
228 } else if (udelta < MAX_DROPOUT) {
229 // in order, with permissible gap
230 if (seq < s->max_seq) {
231 // sequence number wrapped; count another 64k cycles
232 s->cycles += RTP_SEQ_MOD;
235 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
236 // sequence made a large jump...
237 if (seq == s->bad_seq) {
238 /* two sequential packets -- assume that the other side
239 * restarted without telling us; just resync. */
240 rtp_init_sequence(s, seq);
242 s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
246 // duplicate or reordered packet...
252 static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp,
253 uint32_t arrival_timestamp)
255 // Most of this is pretty straight from RFC 3550 appendix A.8
256 uint32_t transit = arrival_timestamp - sent_timestamp;
257 uint32_t prev_transit = s->transit;
258 int32_t d = transit - prev_transit;
259 // Doing the FFABS() call directly on the "transit - prev_transit"
260 // expression doesn't work, since it's an unsigned expression. Doing the
261 // transit calculation in unsigned is desired though, since it most
262 // probably will need to wrap around.
264 s->transit = transit;
267 s->jitter += d - (int32_t) ((s->jitter + 8) >> 4);
270 int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
271 AVIOContext *avio, int count)
277 RTPStatistics *stats = &s->statistics;
279 uint32_t extended_max;
280 uint32_t expected_interval;
281 uint32_t received_interval;
282 int32_t lost_interval;
286 if ((!fd && !avio) || (count < 1))
289 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
290 /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
291 s->octet_count += count;
292 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
294 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
297 s->last_octet_count = s->octet_count;
301 else if (avio_open_dyn_buf(&pb) < 0)
305 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
306 avio_w8(pb, RTCP_RR);
307 avio_wb16(pb, 7); /* length in words - 1 */
308 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
309 avio_wb32(pb, s->ssrc + 1);
310 avio_wb32(pb, s->ssrc); // server SSRC
311 // some placeholders we should really fill...
313 extended_max = stats->cycles + stats->max_seq;
314 expected = extended_max - stats->base_seq;
315 lost = expected - stats->received;
316 lost = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
317 expected_interval = expected - stats->expected_prior;
318 stats->expected_prior = expected;
319 received_interval = stats->received - stats->received_prior;
320 stats->received_prior = stats->received;
321 lost_interval = expected_interval - received_interval;
322 if (expected_interval == 0 || lost_interval <= 0)
325 fraction = (lost_interval << 8) / expected_interval;
327 fraction = (fraction << 24) | lost;
329 avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
330 avio_wb32(pb, extended_max); /* max sequence received */
331 avio_wb32(pb, stats->jitter >> 4); /* jitter */
333 if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) {
334 avio_wb32(pb, 0); /* last SR timestamp */
335 avio_wb32(pb, 0); /* delay since last SR */
337 uint32_t middle_32_bits = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
338 uint32_t delay_since_last = av_rescale(av_gettime_relative() - s->last_rtcp_reception_time,
339 65536, AV_TIME_BASE);
341 avio_wb32(pb, middle_32_bits); /* last SR timestamp */
342 avio_wb32(pb, delay_since_last); /* delay since last SR */
346 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
347 avio_w8(pb, RTCP_SDES);
348 len = strlen(s->hostname);
349 avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */
350 avio_wb32(pb, s->ssrc + 1);
353 avio_write(pb, s->hostname, len);
354 avio_w8(pb, 0); /* END */
356 for (len = (7 + len) % 4; len % 4; len++)
362 len = avio_close_dyn_buf(pb, &buf);
363 if ((len > 0) && buf) {
364 int av_unused result;
365 av_log(s->ic, AV_LOG_TRACE, "sending %d bytes of RR\n", len);
366 result = ffurl_write(fd, buf, len);
367 av_log(s->ic, AV_LOG_TRACE, "result from ffurl_write: %d\n", result);
373 void ff_rtp_send_punch_packets(URLContext *rtp_handle)
379 /* Send a small RTP packet */
380 if (avio_open_dyn_buf(&pb) < 0)
383 avio_w8(pb, (RTP_VERSION << 6));
384 avio_w8(pb, 0); /* Payload type */
385 avio_wb16(pb, 0); /* Seq */
386 avio_wb32(pb, 0); /* Timestamp */
387 avio_wb32(pb, 0); /* SSRC */
390 len = avio_close_dyn_buf(pb, &buf);
391 if ((len > 0) && buf)
392 ffurl_write(rtp_handle, buf, len);
395 /* Send a minimal RTCP RR */
396 if (avio_open_dyn_buf(&pb) < 0)
399 avio_w8(pb, (RTP_VERSION << 6));
400 avio_w8(pb, RTCP_RR); /* receiver report */
401 avio_wb16(pb, 1); /* length in words - 1 */
402 avio_wb32(pb, 0); /* our own SSRC */
405 len = avio_close_dyn_buf(pb, &buf);
406 if ((len > 0) && buf)
407 ffurl_write(rtp_handle, buf, len);
411 static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,
412 uint16_t *missing_mask)
415 uint16_t next_seq = s->seq + 1;
416 RTPPacket *pkt = s->queue;
418 if (!pkt || pkt->seq == next_seq)
422 for (i = 1; i <= 16; i++) {
423 uint16_t missing_seq = next_seq + i;
425 int16_t diff = pkt->seq - missing_seq;
432 if (pkt->seq == missing_seq)
434 *missing_mask |= 1 << (i - 1);
437 *first_missing = next_seq;
441 int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd,
444 int len, need_keyframe, missing_packets;
448 uint16_t first_missing = 0, missing_mask = 0;
453 need_keyframe = s->handler && s->handler->need_keyframe &&
454 s->handler->need_keyframe(s->dynamic_protocol_context);
455 missing_packets = find_missing_packets(s, &first_missing, &missing_mask);
457 if (!need_keyframe && !missing_packets)
460 /* Send new feedback if enough time has elapsed since the last
461 * feedback packet. */
463 now = av_gettime_relative();
464 if (s->last_feedback_time &&
465 (now - s->last_feedback_time) < MIN_FEEDBACK_INTERVAL)
467 s->last_feedback_time = now;
471 else if (avio_open_dyn_buf(&pb) < 0)
475 avio_w8(pb, (RTP_VERSION << 6) | 1); /* PLI */
476 avio_w8(pb, RTCP_PSFB);
477 avio_wb16(pb, 2); /* length in words - 1 */
478 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
479 avio_wb32(pb, s->ssrc + 1);
480 avio_wb32(pb, s->ssrc); // server SSRC
483 if (missing_packets) {
484 avio_w8(pb, (RTP_VERSION << 6) | 1); /* NACK */
485 avio_w8(pb, RTCP_RTPFB);
486 avio_wb16(pb, 3); /* length in words - 1 */
487 avio_wb32(pb, s->ssrc + 1);
488 avio_wb32(pb, s->ssrc); // server SSRC
490 avio_wb16(pb, first_missing);
491 avio_wb16(pb, missing_mask);
497 len = avio_close_dyn_buf(pb, &buf);
498 if (len > 0 && buf) {
499 ffurl_write(fd, buf, len);
506 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
509 RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st,
510 int payload_type, int queue_size)
514 s = av_mallocz(sizeof(RTPDemuxContext));
517 s->payload_type = payload_type;
518 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
519 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
522 s->queue_size = queue_size;
523 rtp_init_statistics(&s->statistics, 0);
525 switch (st->codec->codec_id) {
526 case AV_CODEC_ID_ADPCM_G722:
527 /* According to RFC 3551, the stream clock rate is 8000
528 * even if the sample rate is 16000. */
529 if (st->codec->sample_rate == 8000)
530 st->codec->sample_rate = 16000;
536 // needed to send back RTCP RR in RTSP sessions
537 gethostname(s->hostname, sizeof(s->hostname));
541 void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
542 RTPDynamicProtocolHandler *handler)
544 s->dynamic_protocol_context = ctx;
545 s->handler = handler;
548 void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite,
551 if (!ff_srtp_set_crypto(&s->srtp, suite, params))
556 * This was the second switch in rtp_parse packet.
557 * Normalizes time, if required, sets stream_index, etc.
559 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
561 if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
562 return; /* Timestamp already set by depacketizer */
563 if (timestamp == RTP_NOTS_VALUE)
566 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
570 /* compute pts from timestamp with received ntp_time */
571 delta_timestamp = timestamp - s->last_rtcp_timestamp;
572 /* convert to the PTS timebase */
573 addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time,
574 s->st->time_base.den,
575 (uint64_t) s->st->time_base.num << 32);
576 pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
581 if (!s->base_timestamp)
582 s->base_timestamp = timestamp;
583 /* assume that the difference is INT32_MIN < x < INT32_MAX,
584 * but allow the first timestamp to exceed INT32_MAX */
586 s->unwrapped_timestamp += timestamp;
588 s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
589 s->timestamp = timestamp;
590 pkt->pts = s->unwrapped_timestamp + s->range_start_offset -
594 static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
595 const uint8_t *buf, int len)
598 int payload_type, seq, flags = 0;
604 csrc = buf[0] & 0x0f;
606 payload_type = buf[1] & 0x7f;
608 flags |= RTP_FLAG_MARKER;
609 seq = AV_RB16(buf + 2);
610 timestamp = AV_RB32(buf + 4);
611 ssrc = AV_RB32(buf + 8);
612 /* store the ssrc in the RTPDemuxContext */
615 /* NOTE: we can handle only one payload type */
616 if (s->payload_type != payload_type)
620 // only do something with this if all the rtp checks pass...
621 if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) {
622 av_log(st ? st->codec : NULL, AV_LOG_ERROR,
623 "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
624 payload_type, seq, ((s->seq + 1) & 0xffff));
629 int padding = buf[len - 1];
630 if (len >= 12 + padding)
641 return AVERROR_INVALIDDATA;
643 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
647 /* calculate the header extension length (stored as number
648 * of 32-bit words) */
649 ext = (AV_RB16(buf + 2) + 1) << 2;
653 // skip past RTP header extension
658 if (s->handler && s->handler->parse_packet) {
659 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
660 s->st, pkt, ×tamp, buf, len, seq,
663 if ((rv = av_new_packet(pkt, len)) < 0)
665 memcpy(pkt->data, buf, len);
666 pkt->stream_index = st->index;
668 return AVERROR(EINVAL);
671 // now perform timestamp things....
672 finalize_packet(s, pkt, timestamp);
677 void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
680 RTPPacket *next = s->queue->next;
681 av_freep(&s->queue->buf);
690 static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
692 uint16_t seq = AV_RB16(buf + 2);
693 RTPPacket **cur = &s->queue, *packet;
695 /* Find the correct place in the queue to insert the packet */
697 int16_t diff = seq - (*cur)->seq;
703 packet = av_mallocz(sizeof(*packet));
706 packet->recvtime = av_gettime_relative();
715 static int has_next_packet(RTPDemuxContext *s)
717 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
720 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
722 return s->queue ? s->queue->recvtime : 0;
725 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
730 if (s->queue_len <= 0)
733 if (!has_next_packet(s))
734 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
735 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
737 /* Parse the first packet in the queue, and dequeue it */
738 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
739 next = s->queue->next;
740 av_freep(&s->queue->buf);
747 static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
748 uint8_t **bufptr, int len)
750 uint8_t *buf = bufptr ? *bufptr : NULL;
756 /* If parsing of the previous packet actually returned 0 or an error,
757 * there's nothing more to be parsed from that packet, but we may have
758 * indicated that we can return the next enqueued packet. */
759 if (s->prev_ret <= 0)
760 return rtp_parse_queued_packet(s, pkt);
761 /* return the next packets, if any */
762 if (s->handler && s->handler->parse_packet) {
763 /* timestamp should be overwritten by parse_packet, if not,
764 * the packet is left with pts == AV_NOPTS_VALUE */
765 timestamp = RTP_NOTS_VALUE;
766 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
767 s->st, pkt, ×tamp, NULL, 0, 0,
769 finalize_packet(s, pkt, timestamp);
777 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
779 if (RTP_PT_IS_RTCP(buf[1])) {
780 return rtcp_parse_packet(s, buf, len);
784 int64_t received = av_gettime_relative();
785 uint32_t arrival_ts = av_rescale_q(received, AV_TIME_BASE_Q,
787 timestamp = AV_RB32(buf + 4);
788 // Calculate the jitter immediately, before queueing the packet
789 // into the reordering queue.
790 rtcp_update_jitter(&s->statistics, timestamp, arrival_ts);
793 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
794 /* First packet, or no reordering */
795 return rtp_parse_packet_internal(s, pkt, buf, len);
797 uint16_t seq = AV_RB16(buf + 2);
798 int16_t diff = seq - s->seq;
800 /* Packet older than the previously emitted one, drop */
801 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
802 "RTP: dropping old packet received too late\n");
804 } else if (diff <= 1) {
806 rv = rtp_parse_packet_internal(s, pkt, buf, len);
809 /* Still missing some packet, enqueue this one. */
810 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 return rtp_parse_queued_packet(s, pkt);
822 * Parse an RTP or RTCP packet directly sent as a buffer.
823 * @param s RTP parse context.
824 * @param pkt returned packet
825 * @param bufptr pointer to the input buffer or NULL to read the next packets
826 * @param len buffer len
827 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
828 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
830 int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
831 uint8_t **bufptr, int len)
834 if (s->srtp_enabled && bufptr && ff_srtp_decrypt(&s->srtp, *bufptr, &len) < 0)
836 rv = rtp_parse_one_packet(s, pkt, bufptr, len);
838 while (rv == AVERROR(EAGAIN) && has_next_packet(s))
839 rv = rtp_parse_queued_packet(s, pkt);
840 return rv ? rv : has_next_packet(s);
843 void ff_rtp_parse_close(RTPDemuxContext *s)
845 ff_rtp_reset_packet_queue(s);
846 ff_srtp_free(&s->srtp);
850 int ff_parse_fmtp(AVFormatContext *s,
851 AVStream *stream, PayloadContext *data, const char *p,
852 int (*parse_fmtp)(AVFormatContext *s,
854 PayloadContext *data,
855 const char *attr, const char *value))
860 int value_size = strlen(p) + 1;
862 if (!(value = av_malloc(value_size))) {
863 av_log(NULL, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n");
864 return AVERROR(ENOMEM);
867 // remove protocol identifier
868 while (*p && *p == ' ')
870 while (*p && *p != ' ')
871 p++; // eat protocol identifier
872 while (*p && *p == ' ')
873 p++; // strip trailing spaces
875 while (ff_rtsp_next_attr_and_value(&p,
877 value, value_size)) {
878 res = parse_fmtp(s, stream, data, attr, value);
879 if (res < 0 && res != AVERROR_PATCHWELCOME) {
888 int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
893 pkt->size = avio_close_dyn_buf(*dyn_buf, &pkt->data);
894 pkt->stream_index = stream_idx;
896 if ((ret = av_packet_from_data(pkt, pkt->data, pkt->size)) < 0) {
897 av_freep(&pkt->data);