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"
34 * - add RTCP statistics reporting (should be optional).
36 * - add support for H.263/MPEG-4 packetized output: IDEA: send a
37 * buffer to 'rtp_write_packet' contains all the packets for ONE
38 * frame. Each packet should have a four byte header containing
39 * the length in big-endian format (same trick as
40 * 'ffio_open_dyn_packet_buf').
43 static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = {
44 .enc_name = "X-MP3-draft-00",
45 .codec_type = AVMEDIA_TYPE_AUDIO,
46 .codec_id = AV_CODEC_ID_MP3ADU,
49 static RTPDynamicProtocolHandler speex_dynamic_handler = {
51 .codec_type = AVMEDIA_TYPE_AUDIO,
52 .codec_id = AV_CODEC_ID_SPEEX,
55 static RTPDynamicProtocolHandler opus_dynamic_handler = {
57 .codec_type = AVMEDIA_TYPE_AUDIO,
58 .codec_id = AV_CODEC_ID_OPUS,
61 /* statistics functions */
62 static RTPDynamicProtocolHandler *RTPFirstDynamicPayloadHandler = NULL;
64 void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
66 handler->next = RTPFirstDynamicPayloadHandler;
67 RTPFirstDynamicPayloadHandler = handler;
70 void av_register_rtp_dynamic_payload_handlers(void)
72 ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
73 ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
74 ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
75 ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
76 ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
77 ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
78 ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
79 ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
80 ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler);
81 ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler);
82 ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
83 ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
84 ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
85 ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
86 ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
87 ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
88 ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
89 ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
90 ff_register_dynamic_payload_handler(&speex_dynamic_handler);
91 ff_register_dynamic_payload_handler(&opus_dynamic_handler);
93 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
94 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_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);
101 ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
102 ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
103 ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
104 ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
107 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
108 enum AVMediaType codec_type)
110 RTPDynamicProtocolHandler *handler;
111 for (handler = RTPFirstDynamicPayloadHandler;
112 handler; handler = handler->next)
113 if (!av_strcasecmp(name, handler->enc_name) &&
114 codec_type == handler->codec_type)
119 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
120 enum AVMediaType codec_type)
122 RTPDynamicProtocolHandler *handler;
123 for (handler = RTPFirstDynamicPayloadHandler;
124 handler; handler = handler->next)
125 if (handler->static_payload_id && handler->static_payload_id == id &&
126 codec_type == handler->codec_type)
131 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
136 payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
140 if (payload_len < 20) {
141 av_log(NULL, AV_LOG_ERROR,
142 "Invalid length for RTCP SR packet\n");
143 return AVERROR_INVALIDDATA;
146 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
147 s->last_rtcp_timestamp = AV_RB32(buf + 16);
148 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
149 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
150 if (!s->base_timestamp)
151 s->base_timestamp = s->last_rtcp_timestamp;
152 s->rtcp_ts_offset = s->last_rtcp_timestamp - s->base_timestamp;
166 #define RTP_SEQ_MOD (1 << 16)
168 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
170 memset(s, 0, sizeof(RTPStatistics));
171 s->max_seq = base_sequence;
176 * Called whenever there is a large jump in sequence numbers,
177 * or when they get out of probation...
179 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
183 s->base_seq = seq - 1;
184 s->bad_seq = RTP_SEQ_MOD + 1;
186 s->expected_prior = 0;
187 s->received_prior = 0;
192 /* Returns 1 if we should handle this packet. */
193 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
195 uint16_t udelta = seq - s->max_seq;
196 const int MAX_DROPOUT = 3000;
197 const int MAX_MISORDER = 100;
198 const int MIN_SEQUENTIAL = 2;
200 /* source not valid until MIN_SEQUENTIAL packets with sequence
201 * seq. numbers have been received */
203 if (seq == s->max_seq + 1) {
206 if (s->probation == 0) {
207 rtp_init_sequence(s, seq);
212 s->probation = MIN_SEQUENTIAL - 1;
215 } else if (udelta < MAX_DROPOUT) {
216 // in order, with permissible gap
217 if (seq < s->max_seq) {
218 // sequence number wrapped; count another 64k cycles
219 s->cycles += RTP_SEQ_MOD;
222 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
223 // sequence made a large jump...
224 if (seq == s->bad_seq) {
225 /* two sequential packets -- assume that the other side
226 * restarted without telling us; just resync. */
227 rtp_init_sequence(s, seq);
229 s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
233 // duplicate or reordered packet...
239 int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, int count)
245 RTPStatistics *stats = &s->statistics;
247 uint32_t extended_max;
248 uint32_t expected_interval;
249 uint32_t received_interval;
250 uint32_t lost_interval;
253 uint64_t ntp_time = s->last_rtcp_ntp_time; // TODO: Get local ntp time?
255 if (!s->rtp_ctx || (count < 1))
258 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
259 /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
260 s->octet_count += count;
261 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
263 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
266 s->last_octet_count = s->octet_count;
268 if (avio_open_dyn_buf(&pb) < 0)
272 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
273 avio_w8(pb, RTCP_RR);
274 avio_wb16(pb, 7); /* length in words - 1 */
275 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
276 avio_wb32(pb, s->ssrc + 1);
277 avio_wb32(pb, s->ssrc); // server SSRC
278 // some placeholders we should really fill...
280 extended_max = stats->cycles + stats->max_seq;
281 expected = extended_max - stats->base_seq + 1;
282 lost = expected - stats->received;
283 lost = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
284 expected_interval = expected - stats->expected_prior;
285 stats->expected_prior = expected;
286 received_interval = stats->received - stats->received_prior;
287 stats->received_prior = stats->received;
288 lost_interval = expected_interval - received_interval;
289 if (expected_interval == 0 || lost_interval <= 0)
292 fraction = (lost_interval << 8) / expected_interval;
294 fraction = (fraction << 24) | lost;
296 avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
297 avio_wb32(pb, extended_max); /* max sequence received */
298 avio_wb32(pb, stats->jitter >> 4); /* jitter */
300 if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) {
301 avio_wb32(pb, 0); /* last SR timestamp */
302 avio_wb32(pb, 0); /* delay since last SR */
304 uint32_t middle_32_bits = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
305 uint32_t delay_since_last = ntp_time - s->last_rtcp_ntp_time;
307 avio_wb32(pb, middle_32_bits); /* last SR timestamp */
308 avio_wb32(pb, delay_since_last); /* delay since last SR */
312 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
313 avio_w8(pb, RTCP_SDES);
314 len = strlen(s->hostname);
315 avio_wb16(pb, (6 + len + 3) / 4); /* length in words - 1 */
316 avio_wb32(pb, s->ssrc + 1);
319 avio_write(pb, s->hostname, len);
321 for (len = (6 + len) % 4; len % 4; len++)
325 len = avio_close_dyn_buf(pb, &buf);
326 if ((len > 0) && buf) {
327 int av_unused result;
328 av_dlog(s->ic, "sending %d bytes of RR\n", len);
329 result = ffurl_write(s->rtp_ctx, buf, len);
330 av_dlog(s->ic, "result from ffurl_write: %d\n", result);
336 void ff_rtp_send_punch_packets(URLContext *rtp_handle)
342 /* Send a small RTP packet */
343 if (avio_open_dyn_buf(&pb) < 0)
346 avio_w8(pb, (RTP_VERSION << 6));
347 avio_w8(pb, 0); /* Payload type */
348 avio_wb16(pb, 0); /* Seq */
349 avio_wb32(pb, 0); /* Timestamp */
350 avio_wb32(pb, 0); /* SSRC */
353 len = avio_close_dyn_buf(pb, &buf);
354 if ((len > 0) && buf)
355 ffurl_write(rtp_handle, buf, len);
358 /* Send a minimal RTCP RR */
359 if (avio_open_dyn_buf(&pb) < 0)
362 avio_w8(pb, (RTP_VERSION << 6));
363 avio_w8(pb, RTCP_RR); /* receiver report */
364 avio_wb16(pb, 1); /* length in words - 1 */
365 avio_wb32(pb, 0); /* our own SSRC */
368 len = avio_close_dyn_buf(pb, &buf);
369 if ((len > 0) && buf)
370 ffurl_write(rtp_handle, buf, len);
375 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
376 * MPEG2-TS streams to indicate that they should be demuxed inside the
377 * rtp demux (otherwise AV_CODEC_ID_MPEG2TS packets are returned)
379 RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st,
380 URLContext *rtpc, int payload_type,
385 s = av_mallocz(sizeof(RTPDemuxContext));
388 s->payload_type = payload_type;
389 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
390 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
393 s->queue_size = queue_size;
394 rtp_init_statistics(&s->statistics, 0); // do we know the initial sequence from sdp?
395 if (!strcmp(ff_rtp_enc_name(payload_type), "MP2T")) {
396 s->ts = ff_mpegts_parse_open(s->ic);
402 switch (st->codec->codec_id) {
403 case AV_CODEC_ID_MPEG1VIDEO:
404 case AV_CODEC_ID_MPEG2VIDEO:
405 case AV_CODEC_ID_MP2:
406 case AV_CODEC_ID_MP3:
407 case AV_CODEC_ID_MPEG4:
408 case AV_CODEC_ID_H263:
409 case AV_CODEC_ID_H264:
410 st->need_parsing = AVSTREAM_PARSE_FULL;
412 case AV_CODEC_ID_VORBIS:
413 st->need_parsing = AVSTREAM_PARSE_HEADERS;
415 case AV_CODEC_ID_ADPCM_G722:
416 /* According to RFC 3551, the stream clock rate is 8000
417 * even if the sample rate is 16000. */
418 if (st->codec->sample_rate == 8000)
419 st->codec->sample_rate = 16000;
425 // needed to send back RTCP RR in RTSP sessions
427 gethostname(s->hostname, sizeof(s->hostname));
431 void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
432 RTPDynamicProtocolHandler *handler)
434 s->dynamic_protocol_context = ctx;
435 s->parse_packet = handler->parse_packet;
439 * This was the second switch in rtp_parse packet.
440 * Normalizes time, if required, sets stream_index, etc.
442 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
444 if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
445 return; /* Timestamp already set by depacketizer */
446 if (timestamp == RTP_NOTS_VALUE)
449 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
453 /* compute pts from timestamp with received ntp_time */
454 delta_timestamp = timestamp - s->last_rtcp_timestamp;
455 /* convert to the PTS timebase */
456 addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time,
457 s->st->time_base.den,
458 (uint64_t) s->st->time_base.num << 32);
459 pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
464 if (!s->base_timestamp)
465 s->base_timestamp = timestamp;
466 /* assume that the difference is INT32_MIN < x < INT32_MAX,
467 * but allow the first timestamp to exceed INT32_MAX */
469 s->unwrapped_timestamp += timestamp;
471 s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
472 s->timestamp = timestamp;
473 pkt->pts = s->unwrapped_timestamp + s->range_start_offset -
477 static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
478 const uint8_t *buf, int len)
480 unsigned int ssrc, h;
481 int payload_type, seq, ret, flags = 0;
488 payload_type = buf[1] & 0x7f;
490 flags |= RTP_FLAG_MARKER;
491 seq = AV_RB16(buf + 2);
492 timestamp = AV_RB32(buf + 4);
493 ssrc = AV_RB32(buf + 8);
494 /* store the ssrc in the RTPDemuxContext */
497 /* NOTE: we can handle only one payload type */
498 if (s->payload_type != payload_type)
502 // only do something with this if all the rtp checks pass...
503 if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) {
504 av_log(st ? st->codec : NULL, AV_LOG_ERROR,
505 "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
506 payload_type, seq, ((s->seq + 1) & 0xffff));
511 int padding = buf[len - 1];
512 if (len >= 12 + padding)
520 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
524 /* calculate the header extension length (stored as number
525 * of 32-bit words) */
526 ext = (AV_RB16(buf + 2) + 1) << 2;
530 // skip past RTP header extension
536 /* specific MPEG2-TS demux support */
537 ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len);
538 /* The only error that can be returned from ff_mpegts_parse_packet
539 * is "no more data to return from the provided buffer", so return
540 * AVERROR(EAGAIN) for all errors */
542 return AVERROR(EAGAIN);
544 s->read_buf_size = len - ret;
545 memcpy(s->buf, buf + ret, s->read_buf_size);
546 s->read_buf_index = 0;
550 } else if (s->parse_packet) {
551 rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
552 s->st, pkt, ×tamp, buf, len, flags);
554 /* At this point, the RTP header has been stripped;
555 * This is ASSUMING that there is only 1 CSRC, which isn't wise. */
556 switch (st->codec->codec_id) {
557 case AV_CODEC_ID_MP2:
558 case AV_CODEC_ID_MP3:
559 /* better than nothing: skip MPEG audio RTP header */
565 if (av_new_packet(pkt, len) < 0)
566 return AVERROR(ENOMEM);
567 memcpy(pkt->data, buf, len);
569 case AV_CODEC_ID_MPEG1VIDEO:
570 case AV_CODEC_ID_MPEG2VIDEO:
571 /* better than nothing: skip MPEG video RTP header */
584 if (av_new_packet(pkt, len) < 0)
585 return AVERROR(ENOMEM);
586 memcpy(pkt->data, buf, len);
589 if (av_new_packet(pkt, len) < 0)
590 return AVERROR(ENOMEM);
591 memcpy(pkt->data, buf, len);
595 pkt->stream_index = st->index;
598 // now perform timestamp things....
599 finalize_packet(s, pkt, timestamp);
604 void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
607 RTPPacket *next = s->queue->next;
608 av_free(s->queue->buf);
617 static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
619 uint16_t seq = AV_RB16(buf + 2);
620 RTPPacket *cur = s->queue, *prev = NULL, *packet;
622 /* Find the correct place in the queue to insert the packet */
624 int16_t diff = seq - cur->seq;
631 packet = av_mallocz(sizeof(*packet));
634 packet->recvtime = av_gettime();
646 static int has_next_packet(RTPDemuxContext *s)
648 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
651 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
653 return s->queue ? s->queue->recvtime : 0;
656 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
661 if (s->queue_len <= 0)
664 if (!has_next_packet(s))
665 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
666 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
668 /* Parse the first packet in the queue, and dequeue it */
669 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
670 next = s->queue->next;
671 av_free(s->queue->buf);
678 static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
679 uint8_t **bufptr, int len)
681 uint8_t *buf = bufptr ? *bufptr : NULL;
687 /* If parsing of the previous packet actually returned 0 or an error,
688 * there's nothing more to be parsed from that packet, but we may have
689 * indicated that we can return the next enqueued packet. */
690 if (s->prev_ret <= 0)
691 return rtp_parse_queued_packet(s, pkt);
692 /* return the next packets, if any */
693 if (s->st && s->parse_packet) {
694 /* timestamp should be overwritten by parse_packet, if not,
695 * the packet is left with pts == AV_NOPTS_VALUE */
696 timestamp = RTP_NOTS_VALUE;
697 rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
698 s->st, pkt, ×tamp, NULL, 0, flags);
699 finalize_packet(s, pkt, timestamp);
702 // TODO: Move to a dynamic packet handler (like above)
703 if (s->read_buf_index >= s->read_buf_size)
704 return AVERROR(EAGAIN);
705 ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
706 s->read_buf_size - s->read_buf_index);
708 return AVERROR(EAGAIN);
709 s->read_buf_index += ret;
710 if (s->read_buf_index < s->read_buf_size)
720 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
722 if (RTP_PT_IS_RTCP(buf[1])) {
723 return rtcp_parse_packet(s, buf, len);
726 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
727 /* First packet, or no reordering */
728 return rtp_parse_packet_internal(s, pkt, buf, len);
730 uint16_t seq = AV_RB16(buf + 2);
731 int16_t diff = seq - s->seq;
733 /* Packet older than the previously emitted one, drop */
734 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
735 "RTP: dropping old packet received too late\n");
737 } else if (diff <= 1) {
739 rv = rtp_parse_packet_internal(s, pkt, buf, len);
742 /* Still missing some packet, enqueue this one. */
743 enqueue_packet(s, buf, len);
745 /* Return the first enqueued packet if the queue is full,
746 * even if we're missing something */
747 if (s->queue_len >= s->queue_size)
748 return rtp_parse_queued_packet(s, pkt);
755 * Parse an RTP or RTCP packet directly sent as a buffer.
756 * @param s RTP parse context.
757 * @param pkt returned packet
758 * @param bufptr pointer to the input buffer or NULL to read the next packets
759 * @param len buffer len
760 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
761 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
763 int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
764 uint8_t **bufptr, int len)
766 int rv = rtp_parse_one_packet(s, pkt, bufptr, len);
768 while (rv == AVERROR(EAGAIN) && has_next_packet(s))
769 rv = rtp_parse_queued_packet(s, pkt);
770 return rv ? rv : has_next_packet(s);
773 void ff_rtp_parse_close(RTPDemuxContext *s)
775 ff_rtp_reset_packet_queue(s);
776 if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
777 ff_mpegts_parse_close(s->ts);
782 int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p,
783 int (*parse_fmtp)(AVStream *stream,
784 PayloadContext *data,
785 char *attr, char *value))
790 int value_size = strlen(p) + 1;
792 if (!(value = av_malloc(value_size))) {
793 av_log(NULL, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n");
794 return AVERROR(ENOMEM);
797 // remove protocol identifier
798 while (*p && *p == ' ')
800 while (*p && *p != ' ')
801 p++; // eat protocol identifier
802 while (*p && *p == ' ')
803 p++; // strip trailing spaces
805 while (ff_rtsp_next_attr_and_value(&p,
807 value, value_size)) {
808 res = parse_fmtp(stream, data, attr, value);
809 if (res < 0 && res != AVERROR_PATCHWELCOME) {
818 int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
822 pkt->size = avio_close_dyn_buf(*dyn_buf, &pkt->data);
823 pkt->stream_index = stream_idx;
824 pkt->destruct = av_destruct_packet;