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 *rtp_first_dynamic_payload_handler = NULL;
55 void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
57 handler->next = rtp_first_dynamic_payload_handler;
58 rtp_first_dynamic_payload_handler = handler;
61 void ff_register_rtp_dynamic_payload_handlers(void)
63 ff_register_dynamic_payload_handler(&ff_ac3_dynamic_handler);
64 ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
65 ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
66 ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
67 ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
68 ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
69 ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
70 ff_register_dynamic_payload_handler(&ff_h261_dynamic_handler);
71 ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
72 ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
73 ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
74 ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
75 ff_register_dynamic_payload_handler(&ff_hevc_dynamic_handler);
76 ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler);
77 ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler);
78 ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
79 ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
80 ff_register_dynamic_payload_handler(&ff_mpeg_audio_dynamic_handler);
81 ff_register_dynamic_payload_handler(&ff_mpeg_video_dynamic_handler);
82 ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
83 ff_register_dynamic_payload_handler(&ff_mpegts_dynamic_handler);
84 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
85 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
86 ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
87 ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_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);
92 ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
93 ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
94 ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
95 ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
96 ff_register_dynamic_payload_handler(&opus_dynamic_handler);
97 ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
98 ff_register_dynamic_payload_handler(&speex_dynamic_handler);
101 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
102 enum AVMediaType codec_type)
104 RTPDynamicProtocolHandler *handler;
105 for (handler = rtp_first_dynamic_payload_handler;
106 handler; handler = handler->next)
107 if (!av_strcasecmp(name, handler->enc_name) &&
108 codec_type == handler->codec_type)
113 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
114 enum AVMediaType codec_type)
116 RTPDynamicProtocolHandler *handler;
117 for (handler = rtp_first_dynamic_payload_handler;
118 handler; handler = handler->next)
119 if (handler->static_payload_id && handler->static_payload_id == id &&
120 codec_type == handler->codec_type)
125 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
130 payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
134 if (payload_len < 20) {
135 av_log(NULL, AV_LOG_ERROR,
136 "Invalid length for RTCP SR packet\n");
137 return AVERROR_INVALIDDATA;
140 s->last_rtcp_reception_time = av_gettime_relative();
141 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
142 s->last_rtcp_timestamp = AV_RB32(buf + 16);
143 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
144 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
145 if (!s->base_timestamp)
146 s->base_timestamp = s->last_rtcp_timestamp;
147 s->rtcp_ts_offset = (int32_t)(s->last_rtcp_timestamp - s->base_timestamp);
161 #define RTP_SEQ_MOD (1 << 16)
163 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
165 memset(s, 0, sizeof(RTPStatistics));
166 s->max_seq = base_sequence;
171 * Called whenever there is a large jump in sequence numbers,
172 * or when they get out of probation...
174 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
178 s->base_seq = seq - 1;
179 s->bad_seq = RTP_SEQ_MOD + 1;
181 s->expected_prior = 0;
182 s->received_prior = 0;
187 /* Returns 1 if we should handle this packet. */
188 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
190 uint16_t udelta = seq - s->max_seq;
191 const int MAX_DROPOUT = 3000;
192 const int MAX_MISORDER = 100;
193 const int MIN_SEQUENTIAL = 2;
195 /* source not valid until MIN_SEQUENTIAL packets with sequence
196 * seq. numbers have been received */
198 if (seq == s->max_seq + 1) {
201 if (s->probation == 0) {
202 rtp_init_sequence(s, seq);
207 s->probation = MIN_SEQUENTIAL - 1;
210 } else if (udelta < MAX_DROPOUT) {
211 // in order, with permissible gap
212 if (seq < s->max_seq) {
213 // sequence number wrapped; count another 64k cycles
214 s->cycles += RTP_SEQ_MOD;
217 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
218 // sequence made a large jump...
219 if (seq == s->bad_seq) {
220 /* two sequential packets -- assume that the other side
221 * restarted without telling us; just resync. */
222 rtp_init_sequence(s, seq);
224 s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
228 // duplicate or reordered packet...
234 static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp,
235 uint32_t arrival_timestamp)
237 // Most of this is pretty straight from RFC 3550 appendix A.8
238 uint32_t transit = arrival_timestamp - sent_timestamp;
239 uint32_t prev_transit = s->transit;
240 int32_t d = transit - prev_transit;
241 // Doing the FFABS() call directly on the "transit - prev_transit"
242 // expression doesn't work, since it's an unsigned expression. Doing the
243 // transit calculation in unsigned is desired though, since it most
244 // probably will need to wrap around.
246 s->transit = transit;
249 s->jitter += d - (int32_t) ((s->jitter + 8) >> 4);
252 int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
253 AVIOContext *avio, int count)
259 RTPStatistics *stats = &s->statistics;
261 uint32_t extended_max;
262 uint32_t expected_interval;
263 uint32_t received_interval;
264 int32_t lost_interval;
268 if ((!fd && !avio) || (count < 1))
271 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
272 /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
273 s->octet_count += count;
274 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
276 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
279 s->last_octet_count = s->octet_count;
283 else if (avio_open_dyn_buf(&pb) < 0)
287 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
288 avio_w8(pb, RTCP_RR);
289 avio_wb16(pb, 7); /* length in words - 1 */
290 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
291 avio_wb32(pb, s->ssrc + 1);
292 avio_wb32(pb, s->ssrc); // server SSRC
293 // some placeholders we should really fill...
295 extended_max = stats->cycles + stats->max_seq;
296 expected = extended_max - stats->base_seq;
297 lost = expected - stats->received;
298 lost = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
299 expected_interval = expected - stats->expected_prior;
300 stats->expected_prior = expected;
301 received_interval = stats->received - stats->received_prior;
302 stats->received_prior = stats->received;
303 lost_interval = expected_interval - received_interval;
304 if (expected_interval == 0 || lost_interval <= 0)
307 fraction = (lost_interval << 8) / expected_interval;
309 fraction = (fraction << 24) | lost;
311 avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
312 avio_wb32(pb, extended_max); /* max sequence received */
313 avio_wb32(pb, stats->jitter >> 4); /* jitter */
315 if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) {
316 avio_wb32(pb, 0); /* last SR timestamp */
317 avio_wb32(pb, 0); /* delay since last SR */
319 uint32_t middle_32_bits = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
320 uint32_t delay_since_last = av_rescale(av_gettime_relative() - s->last_rtcp_reception_time,
321 65536, AV_TIME_BASE);
323 avio_wb32(pb, middle_32_bits); /* last SR timestamp */
324 avio_wb32(pb, delay_since_last); /* delay since last SR */
328 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
329 avio_w8(pb, RTCP_SDES);
330 len = strlen(s->hostname);
331 avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */
332 avio_wb32(pb, s->ssrc + 1);
335 avio_write(pb, s->hostname, len);
336 avio_w8(pb, 0); /* END */
338 for (len = (7 + len) % 4; len % 4; len++)
344 len = avio_close_dyn_buf(pb, &buf);
345 if ((len > 0) && buf) {
346 int av_unused result;
347 av_dlog(s->ic, "sending %d bytes of RR\n", len);
348 result = ffurl_write(fd, buf, len);
349 av_dlog(s->ic, "result from ffurl_write: %d\n", result);
355 void ff_rtp_send_punch_packets(URLContext *rtp_handle)
361 /* Send a small RTP packet */
362 if (avio_open_dyn_buf(&pb) < 0)
365 avio_w8(pb, (RTP_VERSION << 6));
366 avio_w8(pb, 0); /* Payload type */
367 avio_wb16(pb, 0); /* Seq */
368 avio_wb32(pb, 0); /* Timestamp */
369 avio_wb32(pb, 0); /* SSRC */
372 len = avio_close_dyn_buf(pb, &buf);
373 if ((len > 0) && buf)
374 ffurl_write(rtp_handle, buf, len);
377 /* Send a minimal RTCP RR */
378 if (avio_open_dyn_buf(&pb) < 0)
381 avio_w8(pb, (RTP_VERSION << 6));
382 avio_w8(pb, RTCP_RR); /* receiver report */
383 avio_wb16(pb, 1); /* length in words - 1 */
384 avio_wb32(pb, 0); /* our own SSRC */
387 len = avio_close_dyn_buf(pb, &buf);
388 if ((len > 0) && buf)
389 ffurl_write(rtp_handle, buf, len);
393 static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,
394 uint16_t *missing_mask)
397 uint16_t next_seq = s->seq + 1;
398 RTPPacket *pkt = s->queue;
400 if (!pkt || pkt->seq == next_seq)
404 for (i = 1; i <= 16; i++) {
405 uint16_t missing_seq = next_seq + i;
407 int16_t diff = pkt->seq - missing_seq;
414 if (pkt->seq == missing_seq)
416 *missing_mask |= 1 << (i - 1);
419 *first_missing = next_seq;
423 int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd,
426 int len, need_keyframe, missing_packets;
430 uint16_t first_missing = 0, missing_mask = 0;
435 need_keyframe = s->handler && s->handler->need_keyframe &&
436 s->handler->need_keyframe(s->dynamic_protocol_context);
437 missing_packets = find_missing_packets(s, &first_missing, &missing_mask);
439 if (!need_keyframe && !missing_packets)
442 /* Send new feedback if enough time has elapsed since the last
443 * feedback packet. */
445 now = av_gettime_relative();
446 if (s->last_feedback_time &&
447 (now - s->last_feedback_time) < MIN_FEEDBACK_INTERVAL)
449 s->last_feedback_time = now;
453 else if (avio_open_dyn_buf(&pb) < 0)
457 avio_w8(pb, (RTP_VERSION << 6) | 1); /* PLI */
458 avio_w8(pb, RTCP_PSFB);
459 avio_wb16(pb, 2); /* length in words - 1 */
460 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
461 avio_wb32(pb, s->ssrc + 1);
462 avio_wb32(pb, s->ssrc); // server SSRC
465 if (missing_packets) {
466 avio_w8(pb, (RTP_VERSION << 6) | 1); /* NACK */
467 avio_w8(pb, RTCP_RTPFB);
468 avio_wb16(pb, 3); /* length in words - 1 */
469 avio_wb32(pb, s->ssrc + 1);
470 avio_wb32(pb, s->ssrc); // server SSRC
472 avio_wb16(pb, first_missing);
473 avio_wb16(pb, missing_mask);
479 len = avio_close_dyn_buf(pb, &buf);
480 if (len > 0 && buf) {
481 ffurl_write(fd, buf, len);
488 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
491 RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st,
492 int payload_type, int queue_size)
496 s = av_mallocz(sizeof(RTPDemuxContext));
499 s->payload_type = payload_type;
500 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
501 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
504 s->queue_size = queue_size;
505 rtp_init_statistics(&s->statistics, 0);
507 switch (st->codec->codec_id) {
508 case AV_CODEC_ID_ADPCM_G722:
509 /* According to RFC 3551, the stream clock rate is 8000
510 * even if the sample rate is 16000. */
511 if (st->codec->sample_rate == 8000)
512 st->codec->sample_rate = 16000;
518 // needed to send back RTCP RR in RTSP sessions
519 gethostname(s->hostname, sizeof(s->hostname));
523 void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
524 RTPDynamicProtocolHandler *handler)
526 s->dynamic_protocol_context = ctx;
527 s->handler = handler;
530 void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite,
533 if (!ff_srtp_set_crypto(&s->srtp, suite, params))
538 * This was the second switch in rtp_parse packet.
539 * Normalizes time, if required, sets stream_index, etc.
541 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
543 if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
544 return; /* Timestamp already set by depacketizer */
545 if (timestamp == RTP_NOTS_VALUE)
548 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
552 /* compute pts from timestamp with received ntp_time */
553 delta_timestamp = timestamp - s->last_rtcp_timestamp;
554 /* convert to the PTS timebase */
555 addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time,
556 s->st->time_base.den,
557 (uint64_t) s->st->time_base.num << 32);
558 pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
563 if (!s->base_timestamp)
564 s->base_timestamp = timestamp;
565 /* assume that the difference is INT32_MIN < x < INT32_MAX,
566 * but allow the first timestamp to exceed INT32_MAX */
568 s->unwrapped_timestamp += timestamp;
570 s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
571 s->timestamp = timestamp;
572 pkt->pts = s->unwrapped_timestamp + s->range_start_offset -
576 static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
577 const uint8_t *buf, int len)
580 int payload_type, seq, flags = 0;
586 csrc = buf[0] & 0x0f;
588 payload_type = buf[1] & 0x7f;
590 flags |= RTP_FLAG_MARKER;
591 seq = AV_RB16(buf + 2);
592 timestamp = AV_RB32(buf + 4);
593 ssrc = AV_RB32(buf + 8);
594 /* store the ssrc in the RTPDemuxContext */
597 /* NOTE: we can handle only one payload type */
598 if (s->payload_type != payload_type)
602 // only do something with this if all the rtp checks pass...
603 if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) {
604 av_log(st ? st->codec : NULL, AV_LOG_ERROR,
605 "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
606 payload_type, seq, ((s->seq + 1) & 0xffff));
611 int padding = buf[len - 1];
612 if (len >= 12 + padding)
623 return AVERROR_INVALIDDATA;
625 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
629 /* calculate the header extension length (stored as number
630 * of 32-bit words) */
631 ext = (AV_RB16(buf + 2) + 1) << 2;
635 // skip past RTP header extension
640 if (s->handler && s->handler->parse_packet) {
641 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
642 s->st, pkt, ×tamp, buf, len, seq,
645 if ((rv = av_new_packet(pkt, len)) < 0)
647 memcpy(pkt->data, buf, len);
648 pkt->stream_index = st->index;
650 return AVERROR(EINVAL);
653 // now perform timestamp things....
654 finalize_packet(s, pkt, timestamp);
659 void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
662 RTPPacket *next = s->queue->next;
663 av_free(s->queue->buf);
672 static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
674 uint16_t seq = AV_RB16(buf + 2);
675 RTPPacket **cur = &s->queue, *packet;
677 /* Find the correct place in the queue to insert the packet */
679 int16_t diff = seq - (*cur)->seq;
685 packet = av_mallocz(sizeof(*packet));
688 packet->recvtime = av_gettime_relative();
697 static int has_next_packet(RTPDemuxContext *s)
699 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
702 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
704 return s->queue ? s->queue->recvtime : 0;
707 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
712 if (s->queue_len <= 0)
715 if (!has_next_packet(s))
716 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
717 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
719 /* Parse the first packet in the queue, and dequeue it */
720 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
721 next = s->queue->next;
722 av_free(s->queue->buf);
729 static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
730 uint8_t **bufptr, int len)
732 uint8_t *buf = bufptr ? *bufptr : NULL;
738 /* If parsing of the previous packet actually returned 0 or an error,
739 * there's nothing more to be parsed from that packet, but we may have
740 * indicated that we can return the next enqueued packet. */
741 if (s->prev_ret <= 0)
742 return rtp_parse_queued_packet(s, pkt);
743 /* return the next packets, if any */
744 if (s->handler && s->handler->parse_packet) {
745 /* timestamp should be overwritten by parse_packet, if not,
746 * the packet is left with pts == AV_NOPTS_VALUE */
747 timestamp = RTP_NOTS_VALUE;
748 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
749 s->st, pkt, ×tamp, NULL, 0, 0,
751 finalize_packet(s, pkt, timestamp);
759 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
761 if (RTP_PT_IS_RTCP(buf[1])) {
762 return rtcp_parse_packet(s, buf, len);
766 int64_t received = av_gettime_relative();
767 uint32_t arrival_ts = av_rescale_q(received, AV_TIME_BASE_Q,
769 timestamp = AV_RB32(buf + 4);
770 // Calculate the jitter immediately, before queueing the packet
771 // into the reordering queue.
772 rtcp_update_jitter(&s->statistics, timestamp, arrival_ts);
775 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
776 /* First packet, or no reordering */
777 return rtp_parse_packet_internal(s, pkt, buf, len);
779 uint16_t seq = AV_RB16(buf + 2);
780 int16_t diff = seq - s->seq;
782 /* Packet older than the previously emitted one, drop */
783 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
784 "RTP: dropping old packet received too late\n");
786 } else if (diff <= 1) {
788 rv = rtp_parse_packet_internal(s, pkt, buf, len);
791 /* Still missing some packet, enqueue this one. */
792 enqueue_packet(s, buf, len);
794 /* Return the first enqueued packet if the queue is full,
795 * even if we're missing something */
796 if (s->queue_len >= s->queue_size)
797 return rtp_parse_queued_packet(s, pkt);
804 * Parse an RTP or RTCP packet directly sent as a buffer.
805 * @param s RTP parse context.
806 * @param pkt returned packet
807 * @param bufptr pointer to the input buffer or NULL to read the next packets
808 * @param len buffer len
809 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
810 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
812 int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
813 uint8_t **bufptr, int len)
816 if (s->srtp_enabled && bufptr && ff_srtp_decrypt(&s->srtp, *bufptr, &len) < 0)
818 rv = rtp_parse_one_packet(s, pkt, bufptr, len);
820 while (rv == AVERROR(EAGAIN) && has_next_packet(s))
821 rv = rtp_parse_queued_packet(s, pkt);
822 return rv ? rv : has_next_packet(s);
825 void ff_rtp_parse_close(RTPDemuxContext *s)
827 ff_rtp_reset_packet_queue(s);
828 ff_srtp_free(&s->srtp);
832 int ff_parse_fmtp(AVFormatContext *s,
833 AVStream *stream, PayloadContext *data, const char *p,
834 int (*parse_fmtp)(AVFormatContext *s,
836 PayloadContext *data,
837 char *attr, char *value))
842 int value_size = strlen(p) + 1;
844 if (!(value = av_malloc(value_size))) {
845 av_log(NULL, AV_LOG_ERROR, "Failed to allocate data for FMTP.");
846 return AVERROR(ENOMEM);
849 // remove protocol identifier
850 while (*p && *p == ' ')
852 while (*p && *p != ' ')
853 p++; // eat protocol identifier
854 while (*p && *p == ' ')
855 p++; // strip trailing spaces
857 while (ff_rtsp_next_attr_and_value(&p,
859 value, value_size)) {
860 res = parse_fmtp(s, stream, data, attr, value);
861 if (res < 0 && res != AVERROR_PATCHWELCOME) {
870 int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
875 pkt->size = avio_close_dyn_buf(*dyn_buf, &pkt->data);
876 pkt->stream_index = stream_idx;
878 if ((ret = av_packet_from_data(pkt, pkt->data, pkt->size)) < 0) {
879 av_freep(&pkt->data);