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"
32 #include "rtpdec_formats.h"
34 #define MIN_FEEDBACK_INTERVAL 200000 /* 200 ms in us */
36 static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = {
37 .enc_name = "X-MP3-draft-00",
38 .codec_type = AVMEDIA_TYPE_AUDIO,
39 .codec_id = AV_CODEC_ID_MP3ADU,
42 static RTPDynamicProtocolHandler speex_dynamic_handler = {
44 .codec_type = AVMEDIA_TYPE_AUDIO,
45 .codec_id = AV_CODEC_ID_SPEEX,
48 static RTPDynamicProtocolHandler opus_dynamic_handler = {
50 .codec_type = AVMEDIA_TYPE_AUDIO,
51 .codec_id = AV_CODEC_ID_OPUS,
54 static RTPDynamicProtocolHandler *rtp_first_dynamic_payload_handler = NULL;
56 void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
58 handler->next = rtp_first_dynamic_payload_handler;
59 rtp_first_dynamic_payload_handler = handler;
62 void av_register_rtp_dynamic_payload_handlers(void)
64 ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
65 ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
66 ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
67 ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
68 ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
69 ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
70 ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
71 ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
72 ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler);
73 ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler);
74 ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
75 ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
76 ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
77 ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
78 ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
79 ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
80 ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
81 ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
82 ff_register_dynamic_payload_handler(&speex_dynamic_handler);
83 ff_register_dynamic_payload_handler(&opus_dynamic_handler);
85 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
86 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
88 ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
89 ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
90 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
91 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
93 ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
94 ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
95 ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
96 ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
99 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
100 enum AVMediaType codec_type)
102 RTPDynamicProtocolHandler *handler;
103 for (handler = rtp_first_dynamic_payload_handler;
104 handler; handler = handler->next)
105 if (!av_strcasecmp(name, handler->enc_name) &&
106 codec_type == handler->codec_type)
111 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
112 enum AVMediaType codec_type)
114 RTPDynamicProtocolHandler *handler;
115 for (handler = rtp_first_dynamic_payload_handler;
116 handler; handler = handler->next)
117 if (handler->static_payload_id && handler->static_payload_id == id &&
118 codec_type == handler->codec_type)
123 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
128 payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
132 if (payload_len < 20) {
133 av_log(NULL, AV_LOG_ERROR,
134 "Invalid length for RTCP SR packet\n");
135 return AVERROR_INVALIDDATA;
138 s->last_rtcp_reception_time = av_gettime();
139 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
140 s->last_rtcp_timestamp = AV_RB32(buf + 16);
141 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
142 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
143 if (!s->base_timestamp)
144 s->base_timestamp = s->last_rtcp_timestamp;
145 s->rtcp_ts_offset = s->last_rtcp_timestamp - s->base_timestamp;
159 #define RTP_SEQ_MOD (1 << 16)
161 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
163 memset(s, 0, sizeof(RTPStatistics));
164 s->max_seq = base_sequence;
169 * Called whenever there is a large jump in sequence numbers,
170 * or when they get out of probation...
172 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
176 s->base_seq = seq - 1;
177 s->bad_seq = RTP_SEQ_MOD + 1;
179 s->expected_prior = 0;
180 s->received_prior = 0;
185 /* Returns 1 if we should handle this packet. */
186 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
188 uint16_t udelta = seq - s->max_seq;
189 const int MAX_DROPOUT = 3000;
190 const int MAX_MISORDER = 100;
191 const int MIN_SEQUENTIAL = 2;
193 /* source not valid until MIN_SEQUENTIAL packets with sequence
194 * seq. numbers have been received */
196 if (seq == s->max_seq + 1) {
199 if (s->probation == 0) {
200 rtp_init_sequence(s, seq);
205 s->probation = MIN_SEQUENTIAL - 1;
208 } else if (udelta < MAX_DROPOUT) {
209 // in order, with permissible gap
210 if (seq < s->max_seq) {
211 // sequence number wrapped; count another 64k cycles
212 s->cycles += RTP_SEQ_MOD;
215 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
216 // sequence made a large jump...
217 if (seq == s->bad_seq) {
218 /* two sequential packets -- assume that the other side
219 * restarted without telling us; just resync. */
220 rtp_init_sequence(s, seq);
222 s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
226 // duplicate or reordered packet...
232 static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp,
233 uint32_t arrival_timestamp)
235 // Most of this is pretty straight from RFC 3550 appendix A.8
236 uint32_t transit = arrival_timestamp - sent_timestamp;
237 uint32_t prev_transit = s->transit;
238 int32_t d = transit - prev_transit;
239 // Doing the FFABS() call directly on the "transit - prev_transit"
240 // expression doesn't work, since it's an unsigned expression. Doing the
241 // transit calculation in unsigned is desired though, since it most
242 // probably will need to wrap around.
244 s->transit = transit;
247 s->jitter += d - (int32_t) ((s->jitter + 8) >> 4);
250 int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
251 AVIOContext *avio, int count)
257 RTPStatistics *stats = &s->statistics;
259 uint32_t extended_max;
260 uint32_t expected_interval;
261 uint32_t received_interval;
262 int32_t lost_interval;
266 if ((!fd && !avio) || (count < 1))
269 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
270 /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
271 s->octet_count += count;
272 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
274 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
277 s->last_octet_count = s->octet_count;
281 else if (avio_open_dyn_buf(&pb) < 0)
285 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
286 avio_w8(pb, RTCP_RR);
287 avio_wb16(pb, 7); /* length in words - 1 */
288 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
289 avio_wb32(pb, s->ssrc + 1);
290 avio_wb32(pb, s->ssrc); // server SSRC
291 // some placeholders we should really fill...
293 extended_max = stats->cycles + stats->max_seq;
294 expected = extended_max - stats->base_seq;
295 lost = expected - stats->received;
296 lost = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
297 expected_interval = expected - stats->expected_prior;
298 stats->expected_prior = expected;
299 received_interval = stats->received - stats->received_prior;
300 stats->received_prior = stats->received;
301 lost_interval = expected_interval - received_interval;
302 if (expected_interval == 0 || lost_interval <= 0)
305 fraction = (lost_interval << 8) / expected_interval;
307 fraction = (fraction << 24) | lost;
309 avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
310 avio_wb32(pb, extended_max); /* max sequence received */
311 avio_wb32(pb, stats->jitter >> 4); /* jitter */
313 if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) {
314 avio_wb32(pb, 0); /* last SR timestamp */
315 avio_wb32(pb, 0); /* delay since last SR */
317 uint32_t middle_32_bits = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
318 uint32_t delay_since_last = av_rescale(av_gettime() - s->last_rtcp_reception_time,
319 65536, AV_TIME_BASE);
321 avio_wb32(pb, middle_32_bits); /* last SR timestamp */
322 avio_wb32(pb, delay_since_last); /* delay since last SR */
326 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
327 avio_w8(pb, RTCP_SDES);
328 len = strlen(s->hostname);
329 avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */
330 avio_wb32(pb, s->ssrc + 1);
333 avio_write(pb, s->hostname, len);
334 avio_w8(pb, 0); /* END */
336 for (len = (7 + len) % 4; len % 4; len++)
342 len = avio_close_dyn_buf(pb, &buf);
343 if ((len > 0) && buf) {
344 int av_unused result;
345 av_dlog(s->ic, "sending %d bytes of RR\n", len);
346 result = ffurl_write(fd, buf, len);
347 av_dlog(s->ic, "result from ffurl_write: %d\n", result);
353 void ff_rtp_send_punch_packets(URLContext *rtp_handle)
359 /* Send a small RTP packet */
360 if (avio_open_dyn_buf(&pb) < 0)
363 avio_w8(pb, (RTP_VERSION << 6));
364 avio_w8(pb, 0); /* Payload type */
365 avio_wb16(pb, 0); /* Seq */
366 avio_wb32(pb, 0); /* Timestamp */
367 avio_wb32(pb, 0); /* SSRC */
370 len = avio_close_dyn_buf(pb, &buf);
371 if ((len > 0) && buf)
372 ffurl_write(rtp_handle, buf, len);
375 /* Send a minimal RTCP RR */
376 if (avio_open_dyn_buf(&pb) < 0)
379 avio_w8(pb, (RTP_VERSION << 6));
380 avio_w8(pb, RTCP_RR); /* receiver report */
381 avio_wb16(pb, 1); /* length in words - 1 */
382 avio_wb32(pb, 0); /* our own SSRC */
385 len = avio_close_dyn_buf(pb, &buf);
386 if ((len > 0) && buf)
387 ffurl_write(rtp_handle, buf, len);
391 static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,
392 uint16_t *missing_mask)
395 uint16_t next_seq = s->seq + 1;
396 RTPPacket *pkt = s->queue;
398 if (!pkt || pkt->seq == next_seq)
402 for (i = 1; i <= 16; i++) {
403 uint16_t missing_seq = next_seq + i;
405 int16_t diff = pkt->seq - missing_seq;
412 if (pkt->seq == missing_seq)
414 *missing_mask |= 1 << (i - 1);
417 *first_missing = next_seq;
421 int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd,
424 int len, need_keyframe, missing_packets;
428 uint16_t first_missing, missing_mask;
433 need_keyframe = s->handler && s->handler->need_keyframe &&
434 s->handler->need_keyframe(s->dynamic_protocol_context);
435 missing_packets = find_missing_packets(s, &first_missing, &missing_mask);
437 if (!need_keyframe && !missing_packets)
440 /* Send new feedback if enough time has elapsed since the last
441 * feedback packet. */
444 if (s->last_feedback_time &&
445 (now - s->last_feedback_time) < MIN_FEEDBACK_INTERVAL)
447 s->last_feedback_time = now;
451 else if (avio_open_dyn_buf(&pb) < 0)
455 avio_w8(pb, (RTP_VERSION << 6) | 1); /* PLI */
456 avio_w8(pb, RTCP_PSFB);
457 avio_wb16(pb, 2); /* length in words - 1 */
458 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
459 avio_wb32(pb, s->ssrc + 1);
460 avio_wb32(pb, s->ssrc); // server SSRC
463 if (missing_packets) {
464 avio_w8(pb, (RTP_VERSION << 6) | 1); /* NACK */
465 avio_w8(pb, RTCP_RTPFB);
466 avio_wb16(pb, 3); /* length in words - 1 */
467 avio_wb32(pb, s->ssrc + 1);
468 avio_wb32(pb, s->ssrc); // server SSRC
470 avio_wb16(pb, first_missing);
471 avio_wb16(pb, missing_mask);
477 len = avio_close_dyn_buf(pb, &buf);
478 if (len > 0 && buf) {
479 ffurl_write(fd, buf, len);
486 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
487 * MPEG2-TS streams to indicate that they should be demuxed inside the
488 * rtp demux (otherwise AV_CODEC_ID_MPEG2TS packets are returned)
490 RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st,
491 int payload_type, int queue_size)
495 s = av_mallocz(sizeof(RTPDemuxContext));
498 s->payload_type = payload_type;
499 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
500 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
503 s->queue_size = queue_size;
504 rtp_init_statistics(&s->statistics, 0);
505 if (!strcmp(ff_rtp_enc_name(payload_type), "MP2T")) {
506 s->ts = ff_mpegts_parse_open(s->ic);
512 switch (st->codec->codec_id) {
513 case AV_CODEC_ID_MPEG1VIDEO:
514 case AV_CODEC_ID_MPEG2VIDEO:
515 case AV_CODEC_ID_MP2:
516 case AV_CODEC_ID_MP3:
517 case AV_CODEC_ID_MPEG4:
518 case AV_CODEC_ID_H263:
519 case AV_CODEC_ID_H264:
520 st->need_parsing = AVSTREAM_PARSE_FULL;
522 case AV_CODEC_ID_VORBIS:
523 st->need_parsing = AVSTREAM_PARSE_HEADERS;
525 case AV_CODEC_ID_ADPCM_G722:
526 /* According to RFC 3551, the stream clock rate is 8000
527 * even if the sample rate is 16000. */
528 if (st->codec->sample_rate == 8000)
529 st->codec->sample_rate = 16000;
535 // needed to send back RTCP RR in RTSP sessions
536 gethostname(s->hostname, sizeof(s->hostname));
540 void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
541 RTPDynamicProtocolHandler *handler)
543 s->dynamic_protocol_context = ctx;
544 s->handler = handler;
547 void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite,
550 if (!ff_srtp_set_crypto(&s->srtp, suite, params))
555 * This was the second switch in rtp_parse packet.
556 * Normalizes time, if required, sets stream_index, etc.
558 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
560 if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
561 return; /* Timestamp already set by depacketizer */
562 if (timestamp == RTP_NOTS_VALUE)
565 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
569 /* compute pts from timestamp with received ntp_time */
570 delta_timestamp = timestamp - s->last_rtcp_timestamp;
571 /* convert to the PTS timebase */
572 addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time,
573 s->st->time_base.den,
574 (uint64_t) s->st->time_base.num << 32);
575 pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
580 if (!s->base_timestamp)
581 s->base_timestamp = timestamp;
582 /* assume that the difference is INT32_MIN < x < INT32_MAX,
583 * but allow the first timestamp to exceed INT32_MAX */
585 s->unwrapped_timestamp += timestamp;
587 s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
588 s->timestamp = timestamp;
589 pkt->pts = s->unwrapped_timestamp + s->range_start_offset -
593 static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
594 const uint8_t *buf, int len)
596 unsigned int ssrc, h;
597 int payload_type, seq, ret, flags = 0;
604 payload_type = buf[1] & 0x7f;
606 flags |= RTP_FLAG_MARKER;
607 seq = AV_RB16(buf + 2);
608 timestamp = AV_RB32(buf + 4);
609 ssrc = AV_RB32(buf + 8);
610 /* store the ssrc in the RTPDemuxContext */
613 /* NOTE: we can handle only one payload type */
614 if (s->payload_type != payload_type)
618 // only do something with this if all the rtp checks pass...
619 if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) {
620 av_log(st ? st->codec : NULL, AV_LOG_ERROR,
621 "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
622 payload_type, seq, ((s->seq + 1) & 0xffff));
627 int padding = buf[len - 1];
628 if (len >= 12 + padding)
636 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
640 /* calculate the header extension length (stored as number
641 * of 32-bit words) */
642 ext = (AV_RB16(buf + 2) + 1) << 2;
646 // skip past RTP header extension
652 /* specific MPEG2-TS demux support */
653 ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len);
654 /* The only error that can be returned from ff_mpegts_parse_packet
655 * is "no more data to return from the provided buffer", so return
656 * AVERROR(EAGAIN) for all errors */
658 return AVERROR(EAGAIN);
660 s->read_buf_size = FFMIN(len - ret, sizeof(s->buf));
661 memcpy(s->buf, buf + ret, s->read_buf_size);
662 s->read_buf_index = 0;
666 } else if (s->handler && s->handler->parse_packet) {
667 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
668 s->st, pkt, ×tamp, buf, len, seq,
671 /* At this point, the RTP header has been stripped;
672 * This is ASSUMING that there is only 1 CSRC, which isn't wise. */
673 switch (st->codec->codec_id) {
674 case AV_CODEC_ID_MP2:
675 case AV_CODEC_ID_MP3:
676 /* better than nothing: skip MPEG audio RTP header */
682 if (av_new_packet(pkt, len) < 0)
683 return AVERROR(ENOMEM);
684 memcpy(pkt->data, buf, len);
686 case AV_CODEC_ID_MPEG1VIDEO:
687 case AV_CODEC_ID_MPEG2VIDEO:
688 /* better than nothing: skip MPEG video RTP header */
701 if (av_new_packet(pkt, len) < 0)
702 return AVERROR(ENOMEM);
703 memcpy(pkt->data, buf, len);
706 if (av_new_packet(pkt, len) < 0)
707 return AVERROR(ENOMEM);
708 memcpy(pkt->data, buf, len);
712 pkt->stream_index = st->index;
715 // now perform timestamp things....
716 finalize_packet(s, pkt, timestamp);
721 void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
724 RTPPacket *next = s->queue->next;
725 av_free(s->queue->buf);
734 static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
736 uint16_t seq = AV_RB16(buf + 2);
737 RTPPacket **cur = &s->queue, *packet;
739 /* Find the correct place in the queue to insert the packet */
741 int16_t diff = seq - (*cur)->seq;
747 packet = av_mallocz(sizeof(*packet));
750 packet->recvtime = av_gettime();
759 static int has_next_packet(RTPDemuxContext *s)
761 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
764 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
766 return s->queue ? s->queue->recvtime : 0;
769 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
774 if (s->queue_len <= 0)
777 if (!has_next_packet(s))
778 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
779 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
781 /* Parse the first packet in the queue, and dequeue it */
782 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
783 next = s->queue->next;
784 av_free(s->queue->buf);
791 static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
792 uint8_t **bufptr, int len)
794 uint8_t *buf = bufptr ? *bufptr : NULL;
800 /* If parsing of the previous packet actually returned 0 or an error,
801 * there's nothing more to be parsed from that packet, but we may have
802 * indicated that we can return the next enqueued packet. */
803 if (s->prev_ret <= 0)
804 return rtp_parse_queued_packet(s, pkt);
805 /* return the next packets, if any */
806 if (s->st && s->handler && s->handler->parse_packet) {
807 /* timestamp should be overwritten by parse_packet, if not,
808 * the packet is left with pts == AV_NOPTS_VALUE */
809 timestamp = RTP_NOTS_VALUE;
810 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
811 s->st, pkt, ×tamp, NULL, 0, 0,
813 finalize_packet(s, pkt, timestamp);
816 // TODO: Move to a dynamic packet handler (like above)
817 if (s->read_buf_index >= s->read_buf_size)
818 return AVERROR(EAGAIN);
819 ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
820 s->read_buf_size - s->read_buf_index);
822 return AVERROR(EAGAIN);
823 s->read_buf_index += ret;
824 if (s->read_buf_index < s->read_buf_size)
834 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
836 if (RTP_PT_IS_RTCP(buf[1])) {
837 return rtcp_parse_packet(s, buf, len);
841 int64_t received = av_gettime();
842 uint32_t arrival_ts = av_rescale_q(received, AV_TIME_BASE_Q,
844 timestamp = AV_RB32(buf + 4);
845 // Calculate the jitter immediately, before queueing the packet
846 // into the reordering queue.
847 rtcp_update_jitter(&s->statistics, timestamp, arrival_ts);
850 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
851 /* First packet, or no reordering */
852 return rtp_parse_packet_internal(s, pkt, buf, len);
854 uint16_t seq = AV_RB16(buf + 2);
855 int16_t diff = seq - s->seq;
857 /* Packet older than the previously emitted one, drop */
858 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
859 "RTP: dropping old packet received too late\n");
861 } else if (diff <= 1) {
863 rv = rtp_parse_packet_internal(s, pkt, buf, len);
866 /* Still missing some packet, enqueue this one. */
867 enqueue_packet(s, buf, len);
869 /* Return the first enqueued packet if the queue is full,
870 * even if we're missing something */
871 if (s->queue_len >= s->queue_size)
872 return rtp_parse_queued_packet(s, pkt);
879 * Parse an RTP or RTCP packet directly sent as a buffer.
880 * @param s RTP parse context.
881 * @param pkt returned packet
882 * @param bufptr pointer to the input buffer or NULL to read the next packets
883 * @param len buffer len
884 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
885 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
887 int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
888 uint8_t **bufptr, int len)
891 if (s->srtp_enabled && bufptr && ff_srtp_decrypt(&s->srtp, *bufptr, &len) < 0)
893 rv = rtp_parse_one_packet(s, pkt, bufptr, len);
895 while (rv == AVERROR(EAGAIN) && has_next_packet(s))
896 rv = rtp_parse_queued_packet(s, pkt);
897 return rv ? rv : has_next_packet(s);
900 void ff_rtp_parse_close(RTPDemuxContext *s)
902 ff_rtp_reset_packet_queue(s);
903 if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
904 ff_mpegts_parse_close(s->ts);
906 ff_srtp_free(&s->srtp);
910 int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p,
911 int (*parse_fmtp)(AVStream *stream,
912 PayloadContext *data,
913 char *attr, char *value))
918 int value_size = strlen(p) + 1;
920 if (!(value = av_malloc(value_size))) {
921 av_log(NULL, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n");
922 return AVERROR(ENOMEM);
925 // remove protocol identifier
926 while (*p && *p == ' ')
928 while (*p && *p != ' ')
929 p++; // eat protocol identifier
930 while (*p && *p == ' ')
931 p++; // strip trailing spaces
933 while (ff_rtsp_next_attr_and_value(&p,
935 value, value_size)) {
936 res = parse_fmtp(stream, data, attr, value);
937 if (res < 0 && res != AVERROR_PATCHWELCOME) {
946 int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
950 pkt->size = avio_close_dyn_buf(*dyn_buf, &pkt->data);
951 pkt->stream_index = stream_idx;
952 pkt->destruct = av_destruct_packet;