rtpdec: Allow dynamic payload handlers to handle static payload types, too
[libav.git] / libavformat / rtpdec.c
CommitLineData
8eb793c4
LA
1/*
2 * RTP input format
406792e7 3 * Copyright (c) 2002 Fabrice Bellard
8eb793c4
LA
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
245976da 21
7246177d 22/* needed for gethostname() */
d0feff2a 23#define _XOPEN_SOURCE 600
7246177d 24
9106a698 25#include "libavcodec/get_bits.h"
8eb793c4
LA
26#include "avformat.h"
27#include "mpegts.h"
8eb793c4
LA
28
29#include <unistd.h>
30#include "network.h"
31
302879cb 32#include "rtpdec.h"
965a3ddb 33#include "rtpdec_formats.h"
8eb793c4
LA
34
35//#define DEBUG
36
37/* TODO: - add RTCP statistics reporting (should be optional).
38
39 - add support for h263/mpeg4 packetized output : IDEA: send a
40 buffer to 'rtp_write_packet' contains all the packets for ONE
41 frame. Each packet should have a four byte header containing
42 the length in big endian format (same trick as
43 'url_open_dyn_packet_buf')
44*/
45
46/* statistics functions */
47RTPDynamicProtocolHandler *RTPFirstDynamicPayloadHandler= NULL;
48
0369d2b0 49void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
8eb793c4
LA
50{
51 handler->next= RTPFirstDynamicPayloadHandler;
52 RTPFirstDynamicPayloadHandler= handler;
53}
54
55void av_register_rtp_dynamic_payload_handlers(void)
56{
9b3788ef
JA
57 ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
58 ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
556aa7a1
RB
59 ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
60 ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
45aa9080
RB
61 ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
62 ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
0369d2b0 63 ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
e6327fba 64 ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
887af2aa 65 ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
a59096e4 66 ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
4449df6b 67 ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
1ddc176e 68 ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
51291e60 69 ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
35014efc 70 ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
e9fce261
RB
71
72 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
73 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
3ece3e4c
MS
74
75 ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
76 ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
77 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
78 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
8eb793c4
LA
79}
80
81static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
82{
ff328c02
JA
83 int payload_len;
84 while (len >= 2) {
85 switch (buf[1]) {
86 case RTCP_SR:
87 if (len < 16) {
88 av_log(NULL, AV_LOG_ERROR, "Invalid length for RTCP SR packet\n");
89 return AVERROR_INVALIDDATA;
90 }
91 payload_len = (AV_RB16(buf + 2) + 1) * 4;
92
682d28a9
JA
93 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
94 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE)
95 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
96 s->last_rtcp_timestamp = AV_RB32(buf + 16);
ff328c02
JA
97
98 buf += payload_len;
99 len -= payload_len;
100 break;
b20359f5
JA
101 case RTCP_BYE:
102 return -RTCP_BYE;
ff328c02
JA
103 default:
104 return -1;
105 }
106 }
b20359f5 107 return -1;
8eb793c4
LA
108}
109
110#define RTP_SEQ_MOD (1<<16)
111
112/**
113* called on parse open packet
114*/
115static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence) // called on parse open packet.
116{
117 memset(s, 0, sizeof(RTPStatistics));
118 s->max_seq= base_sequence;
119 s->probation= 1;
120}
121
122/**
123* called whenever there is a large jump in sequence numbers, or when they get out of probation...
124*/
125static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
126{
127 s->max_seq= seq;
128 s->cycles= 0;
129 s->base_seq= seq -1;
130 s->bad_seq= RTP_SEQ_MOD + 1;
131 s->received= 0;
132 s->expected_prior= 0;
133 s->received_prior= 0;
134 s->jitter= 0;
135 s->transit= 0;
136}
137
138/**
139* returns 1 if we should handle this packet.
140*/
141static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
142{
143 uint16_t udelta= seq - s->max_seq;
144 const int MAX_DROPOUT= 3000;
145 const int MAX_MISORDER = 100;
146 const int MIN_SEQUENTIAL = 2;
147
148 /* source not valid until MIN_SEQUENTIAL packets with sequence seq. numbers have been received */
149 if(s->probation)
150 {
151 if(seq==s->max_seq + 1) {
152 s->probation--;
153 s->max_seq= seq;
154 if(s->probation==0) {
155 rtp_init_sequence(s, seq);
156 s->received++;
157 return 1;
158 }
159 } else {
160 s->probation= MIN_SEQUENTIAL - 1;
161 s->max_seq = seq;
162 }
163 } else if (udelta < MAX_DROPOUT) {
164 // in order, with permissible gap
165 if(seq < s->max_seq) {
166 //sequence number wrapped; count antother 64k cycles
167 s->cycles += RTP_SEQ_MOD;
168 }
169 s->max_seq= seq;
170 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
171 // sequence made a large jump...
172 if(seq==s->bad_seq) {
173 // two sequential packets-- assume that the other side restarted without telling us; just resync.
174 rtp_init_sequence(s, seq);
175 } else {
176 s->bad_seq= (seq + 1) & (RTP_SEQ_MOD-1);
177 return 0;
178 }
179 } else {
180 // duplicate or reordered packet...
181 }
182 s->received++;
183 return 1;
184}
185
186#if 0
187/**
188* This function is currently unused; without a valid local ntp time, I don't see how we could calculate the
189* difference between the arrival and sent timestamp. As a result, the jitter and transit statistics values
190* never change. I left this in in case someone else can see a way. (rdm)
191*/
192static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp, uint32_t arrival_timestamp)
193{
194 uint32_t transit= arrival_timestamp - sent_timestamp;
195 int d;
196 s->transit= transit;
197 d= FFABS(transit - s->transit);
198 s->jitter += d - ((s->jitter + 8)>>4);
199}
200#endif
201
202int rtp_check_and_send_back_rr(RTPDemuxContext *s, int count)
203{
204 ByteIOContext *pb;
205 uint8_t *buf;
206 int len;
207 int rtcp_bytes;
208 RTPStatistics *stats= &s->statistics;
209 uint32_t lost;
210 uint32_t extended_max;
211 uint32_t expected_interval;
212 uint32_t received_interval;
213 uint32_t lost_interval;
214 uint32_t expected;
215 uint32_t fraction;
216 uint64_t ntp_time= s->last_rtcp_ntp_time; // TODO: Get local ntp time?
217
218 if (!s->rtp_ctx || (count < 1))
219 return -1;
220
221 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
222 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
223 s->octet_count += count;
224 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
225 RTCP_TX_RATIO_DEN;
226 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
227 if (rtcp_bytes < 28)
228 return -1;
229 s->last_octet_count = s->octet_count;
230
231 if (url_open_dyn_buf(&pb) < 0)
232 return -1;
233
234 // Receiver Report
235 put_byte(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
7f3468d3 236 put_byte(pb, RTCP_RR);
8eb793c4 237 put_be16(pb, 7); /* length in words - 1 */
952139a3
LA
238 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
239 put_be32(pb, s->ssrc + 1);
240 put_be32(pb, s->ssrc); // server SSRC
8eb793c4
LA
241 // some placeholders we should really fill...
242 // RFC 1889/p64
243 extended_max= stats->cycles + stats->max_seq;
244 expected= extended_max - stats->base_seq + 1;
245 lost= expected - stats->received;
246 lost= FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
247 expected_interval= expected - stats->expected_prior;
248 stats->expected_prior= expected;
249 received_interval= stats->received - stats->received_prior;
250 stats->received_prior= stats->received;
251 lost_interval= expected_interval - received_interval;
252 if (expected_interval==0 || lost_interval<=0) fraction= 0;
253 else fraction = (lost_interval<<8)/expected_interval;
254
255 fraction= (fraction<<24) | lost;
256
257 put_be32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
258 put_be32(pb, extended_max); /* max sequence received */
259 put_be32(pb, stats->jitter>>4); /* jitter */
260
261 if(s->last_rtcp_ntp_time==AV_NOPTS_VALUE)
262 {
263 put_be32(pb, 0); /* last SR timestamp */
264 put_be32(pb, 0); /* delay since last SR */
265 } else {
266 uint32_t middle_32_bits= s->last_rtcp_ntp_time>>16; // this is valid, right? do we need to handle 64 bit values special?
267 uint32_t delay_since_last= ntp_time - s->last_rtcp_ntp_time;
268
269 put_be32(pb, middle_32_bits); /* last SR timestamp */
270 put_be32(pb, delay_since_last); /* delay since last SR */
271 }
272
273 // CNAME
274 put_byte(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
7f3468d3 275 put_byte(pb, RTCP_SDES);
8eb793c4
LA
276 len = strlen(s->hostname);
277 put_be16(pb, (6 + len + 3) / 4); /* length in words - 1 */
278 put_be32(pb, s->ssrc);
279 put_byte(pb, 0x01);
280 put_byte(pb, len);
281 put_buffer(pb, s->hostname, len);
282 // padding
283 for (len = (6 + len) % 4; len % 4; len++) {
284 put_byte(pb, 0);
285 }
286
287 put_flush_packet(pb);
288 len = url_close_dyn_buf(pb, &buf);
289 if ((len > 0) && buf) {
290 int result;
e8420626 291 dprintf(s->ic, "sending %d bytes of RR\n", len);
8eb793c4 292 result= url_write(s->rtp_ctx, buf, len);
e8420626 293 dprintf(s->ic, "result from url_write: %d\n", result);
8eb793c4
LA
294 av_free(buf);
295 }
296 return 0;
297}
298
9c8fa20d
MS
299void rtp_send_punch_packets(URLContext* rtp_handle)
300{
301 ByteIOContext *pb;
302 uint8_t *buf;
303 int len;
304
305 /* Send a small RTP packet */
306 if (url_open_dyn_buf(&pb) < 0)
307 return;
308
309 put_byte(pb, (RTP_VERSION << 6));
310 put_byte(pb, 0); /* Payload type */
311 put_be16(pb, 0); /* Seq */
312 put_be32(pb, 0); /* Timestamp */
313 put_be32(pb, 0); /* SSRC */
314
315 put_flush_packet(pb);
316 len = url_close_dyn_buf(pb, &buf);
317 if ((len > 0) && buf)
318 url_write(rtp_handle, buf, len);
319 av_free(buf);
320
321 /* Send a minimal RTCP RR */
322 if (url_open_dyn_buf(&pb) < 0)
323 return;
324
325 put_byte(pb, (RTP_VERSION << 6));
7f3468d3 326 put_byte(pb, RTCP_RR); /* receiver report */
9c8fa20d
MS
327 put_be16(pb, 1); /* length in words - 1 */
328 put_be32(pb, 0); /* our own SSRC */
329
330 put_flush_packet(pb);
331 len = url_close_dyn_buf(pb, &buf);
332 if ((len > 0) && buf)
333 url_write(rtp_handle, buf, len);
334 av_free(buf);
335}
336
337
8eb793c4
LA
338/**
339 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
340 * MPEG2TS streams to indicate that they should be demuxed inside the
341 * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned)
8eb793c4 342 */
58ee0991 343RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, URLContext *rtpc, int payload_type, int queue_size)
8eb793c4
LA
344{
345 RTPDemuxContext *s;
346
347 s = av_mallocz(sizeof(RTPDemuxContext));
348 if (!s)
349 return NULL;
350 s->payload_type = payload_type;
351 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
2cab6b48 352 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
8eb793c4
LA
353 s->ic = s1;
354 s->st = st;
58ee0991 355 s->queue_size = queue_size;
8eb793c4
LA
356 rtp_init_statistics(&s->statistics, 0); // do we know the initial sequence from sdp?
357 if (!strcmp(ff_rtp_enc_name(payload_type), "MP2T")) {
9125806e 358 s->ts = ff_mpegts_parse_open(s->ic);
8eb793c4
LA
359 if (s->ts == NULL) {
360 av_free(s);
361 return NULL;
362 }
363 } else {
26efefc5 364 av_set_pts_info(st, 32, 1, 90000);
8eb793c4
LA
365 switch(st->codec->codec_id) {
366 case CODEC_ID_MPEG1VIDEO:
367 case CODEC_ID_MPEG2VIDEO:
368 case CODEC_ID_MP2:
369 case CODEC_ID_MP3:
370 case CODEC_ID_MPEG4:
45aa9080 371 case CODEC_ID_H263:
8eb793c4
LA
372 case CODEC_ID_H264:
373 st->need_parsing = AVSTREAM_PARSE_FULL;
374 break;
0048a2a8
MS
375 case CODEC_ID_ADPCM_G722:
376 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
377 /* According to RFC 3551, the stream clock rate is 8000
378 * even if the sample rate is 16000. */
379 if (st->codec->sample_rate == 8000)
380 st->codec->sample_rate = 16000;
381 break;
8eb793c4 382 default:
72415b2a 383 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
d6b9e57a
LA
384 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
385 }
8eb793c4
LA
386 break;
387 }
388 }
389 // needed to send back RTCP RR in RTSP sessions
390 s->rtp_ctx = rtpc;
391 gethostname(s->hostname, sizeof(s->hostname));
392 return s;
393}
394
99a1d191
RB
395void
396rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
397 RTPDynamicProtocolHandler *handler)
398{
399 s->dynamic_protocol_context = ctx;
400 s->parse_packet = handler->parse_packet;
401}
402
8eb793c4
LA
403/**
404 * This was the second switch in rtp_parse packet. Normalizes time, if required, sets stream_index, etc.
405 */
406static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
407{
d74c6145 408 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && timestamp != RTP_NOTS_VALUE) {
fba7815d
LA
409 int64_t addend;
410 int delta_timestamp;
411
412 /* compute pts from timestamp with received ntp_time */
413 delta_timestamp = timestamp - s->last_rtcp_timestamp;
414 /* convert to the PTS timebase */
2cab6b48 415 addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time, s->st->time_base.den, (uint64_t)s->st->time_base.num << 32);
5948f822 416 pkt->pts = s->range_start_offset + addend + delta_timestamp;
fba7815d 417 }
8eb793c4
LA
418}
419
02607418
MS
420static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
421 const uint8_t *buf, int len)
8eb793c4
LA
422{
423 unsigned int ssrc, h;
f841a0fc 424 int payload_type, seq, ret, flags = 0;
9446b4bb 425 int ext;
8eb793c4
LA
426 AVStream *st;
427 uint32_t timestamp;
428 int rv= 0;
429
9446b4bb 430 ext = buf[0] & 0x10;
8eb793c4 431 payload_type = buf[1] & 0x7f;
144ae29d
RB
432 if (buf[1] & 0x80)
433 flags |= RTP_FLAG_MARKER;
8eb793c4
LA
434 seq = AV_RB16(buf + 2);
435 timestamp = AV_RB32(buf + 4);
436 ssrc = AV_RB32(buf + 8);
437 /* store the ssrc in the RTPDemuxContext */
438 s->ssrc = ssrc;
439
440 /* NOTE: we can handle only one payload type */
441 if (s->payload_type != payload_type)
442 return -1;
443
444 st = s->st;
445 // only do something with this if all the rtp checks pass...
446 if(!rtp_valid_packet_in_sequence(&s->statistics, seq))
447 {
448 av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
449 payload_type, seq, ((s->seq + 1) & 0xffff));
450 return -1;
451 }
452
453 s->seq = seq;
454 len -= 12;
455 buf += 12;
456
9446b4bb
RS
457 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
458 if (ext) {
459 if (len < 4)
460 return -1;
461 /* calculate the header extension length (stored as number
462 * of 32-bit words) */
463 ext = (AV_RB16(buf + 2) + 1) << 2;
464
465 if (len < ext)
466 return -1;
467 // skip past RTP header extension
468 len -= ext;
469 buf += ext;
470 }
471
8eb793c4
LA
472 if (!st) {
473 /* specific MPEG2TS demux support */
9125806e 474 ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len);
946df059
MS
475 /* The only error that can be returned from ff_mpegts_parse_packet
476 * is "no more data to return from the provided buffer", so return
477 * AVERROR(EAGAIN) for all errors */
4ffff367 478 if (ret < 0)
946df059 479 return AVERROR(EAGAIN);
8eb793c4
LA
480 if (ret < len) {
481 s->read_buf_size = len - ret;
482 memcpy(s->buf, buf + ret, s->read_buf_size);
483 s->read_buf_index = 0;
484 return 1;
485 }
f3e71942 486 return 0;
b4e3330c 487 } else if (s->parse_packet) {
1a45a9f4 488 rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
9b932b8a 489 s->st, pkt, &timestamp, buf, len, flags);
8eb793c4
LA
490 } else {
491 // at this point, the RTP header has been stripped; This is ASSUMING that there is only 1 CSRC, which in't wise.
492 switch(st->codec->codec_id) {
493 case CODEC_ID_MP2:
76faff6e 494 case CODEC_ID_MP3:
8eb793c4
LA
495 /* better than nothing: skip mpeg audio RTP header */
496 if (len <= 4)
497 return -1;
498 h = AV_RB32(buf);
499 len -= 4;
500 buf += 4;
501 av_new_packet(pkt, len);
502 memcpy(pkt->data, buf, len);
503 break;
504 case CODEC_ID_MPEG1VIDEO:
505 case CODEC_ID_MPEG2VIDEO:
506 /* better than nothing: skip mpeg video RTP header */
507 if (len <= 4)
508 return -1;
509 h = AV_RB32(buf);
510 buf += 4;
511 len -= 4;
512 if (h & (1 << 26)) {
513 /* mpeg2 */
514 if (len <= 4)
515 return -1;
516 buf += 4;
517 len -= 4;
518 }
519 av_new_packet(pkt, len);
520 memcpy(pkt->data, buf, len);
521 break;
8eb793c4 522 default:
f739b36d
RB
523 av_new_packet(pkt, len);
524 memcpy(pkt->data, buf, len);
8eb793c4
LA
525 break;
526 }
eafb17d1
RB
527
528 pkt->stream_index = st->index;
f3e71942 529 }
8eb793c4 530
95f03cf3
RB
531 // now perform timestamp things....
532 finalize_packet(s, pkt, timestamp);
f3e71942 533
8eb793c4
LA
534 return rv;
535}
536
58ee0991
MS
537void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
538{
539 while (s->queue) {
540 RTPPacket *next = s->queue->next;
541 av_free(s->queue->buf);
542 av_free(s->queue);
543 s->queue = next;
544 }
545 s->seq = 0;
546 s->queue_len = 0;
547 s->prev_ret = 0;
548}
549
550static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
551{
552 uint16_t seq = AV_RB16(buf + 2);
553 RTPPacket *cur = s->queue, *prev = NULL, *packet;
554
555 /* Find the correct place in the queue to insert the packet */
556 while (cur) {
557 int16_t diff = seq - cur->seq;
558 if (diff < 0)
559 break;
560 prev = cur;
561 cur = cur->next;
562 }
563
564 packet = av_mallocz(sizeof(*packet));
565 if (!packet)
566 return;
567 packet->recvtime = av_gettime();
568 packet->seq = seq;
569 packet->len = len;
570 packet->buf = buf;
571 packet->next = cur;
572 if (prev)
573 prev->next = packet;
574 else
575 s->queue = packet;
576 s->queue_len++;
577}
578
579static int has_next_packet(RTPDemuxContext *s)
580{
ddcf8411 581 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
58ee0991
MS
582}
583
584int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
585{
586 return s->queue ? s->queue->recvtime : 0;
587}
588
589static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
590{
591 int rv;
592 RTPPacket *next;
593
594 if (s->queue_len <= 0)
595 return -1;
596
597 if (!has_next_packet(s))
598 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
599 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
600
601 /* Parse the first packet in the queue, and dequeue it */
602 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
603 next = s->queue->next;
604 av_free(s->queue->buf);
605 av_free(s->queue);
606 s->queue = next;
607 s->queue_len--;
4ffff367 608 return rv;
58ee0991
MS
609}
610
4ffff367 611static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
02607418
MS
612 uint8_t **bufptr, int len)
613{
614 uint8_t* buf = bufptr ? *bufptr : NULL;
615 int ret, flags = 0;
616 uint32_t timestamp;
617 int rv= 0;
618
619 if (!buf) {
f6e138b4
MS
620 /* If parsing of the previous packet actually returned 0 or an error,
621 * there's nothing more to be parsed from that packet, but we may have
58ee0991 622 * indicated that we can return the next enqueued packet. */
f6e138b4 623 if (s->prev_ret <= 0)
58ee0991 624 return rtp_parse_queued_packet(s, pkt);
02607418
MS
625 /* return the next packets, if any */
626 if(s->st && s->parse_packet) {
627 /* timestamp should be overwritten by parse_packet, if not,
628 * the packet is left with pts == AV_NOPTS_VALUE */
629 timestamp = RTP_NOTS_VALUE;
630 rv= s->parse_packet(s->ic, s->dynamic_protocol_context,
631 s->st, pkt, &timestamp, NULL, 0, flags);
632 finalize_packet(s, pkt, timestamp);
4ffff367 633 return rv;
02607418
MS
634 } else {
635 // TODO: Move to a dynamic packet handler (like above)
4ffff367 636 if (s->read_buf_index >= s->read_buf_size)
91ec7aea 637 return AVERROR(EAGAIN);
02607418
MS
638 ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
639 s->read_buf_size - s->read_buf_index);
4ffff367 640 if (ret < 0)
946df059 641 return AVERROR(EAGAIN);
02607418
MS
642 s->read_buf_index += ret;
643 if (s->read_buf_index < s->read_buf_size)
644 return 1;
4ffff367
MS
645 else
646 return 0;
02607418
MS
647 }
648 }
649
650 if (len < 12)
651 return -1;
652
653 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
654 return -1;
655 if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
656 return rtcp_parse_packet(s, buf, len);
657 }
658
65cdee9c 659 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
58ee0991
MS
660 /* First packet, or no reordering */
661 return rtp_parse_packet_internal(s, pkt, buf, len);
662 } else {
663 uint16_t seq = AV_RB16(buf + 2);
664 int16_t diff = seq - s->seq;
665 if (diff < 0) {
666 /* Packet older than the previously emitted one, drop */
667 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
668 "RTP: dropping old packet received too late\n");
669 return -1;
670 } else if (diff <= 1) {
671 /* Correct packet */
672 rv = rtp_parse_packet_internal(s, pkt, buf, len);
4ffff367 673 return rv;
58ee0991
MS
674 } else {
675 /* Still missing some packet, enqueue this one. */
676 enqueue_packet(s, buf, len);
677 *bufptr = NULL;
678 /* Return the first enqueued packet if the queue is full,
679 * even if we're missing something */
680 if (s->queue_len >= s->queue_size)
681 return rtp_parse_queued_packet(s, pkt);
682 return -1;
683 }
684 }
02607418
MS
685}
686
4ffff367
MS
687/**
688 * Parse an RTP or RTCP packet directly sent as a buffer.
689 * @param s RTP parse context.
690 * @param pkt returned packet
691 * @param bufptr pointer to the input buffer or NULL to read the next packets
692 * @param len buffer len
693 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
694 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
695 */
696int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
697 uint8_t **bufptr, int len)
698{
699 int rv = rtp_parse_one_packet(s, pkt, bufptr, len);
700 s->prev_ret = rv;
d678a6fd
MS
701 while (rv == AVERROR(EAGAIN) && has_next_packet(s))
702 rv = rtp_parse_queued_packet(s, pkt);
4ffff367
MS
703 return rv ? rv : has_next_packet(s);
704}
705
8eb793c4
LA
706void rtp_parse_close(RTPDemuxContext *s)
707{
58ee0991 708 ff_rtp_reset_packet_queue(s);
8eb793c4 709 if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
9125806e 710 ff_mpegts_parse_close(s->ts);
8eb793c4
LA
711 }
712 av_free(s);
713}
016bc031
JA
714
715int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p,
716 int (*parse_fmtp)(AVStream *stream,
717 PayloadContext *data,
718 char *attr, char *value))
719{
720 char attr[256];
824535e3 721 char *value;
016bc031 722 int res;
824535e3
JA
723 int value_size = strlen(p) + 1;
724
725 if (!(value = av_malloc(value_size))) {
726 av_log(stream, AV_LOG_ERROR, "Failed to allocate data for FMTP.");
727 return AVERROR(ENOMEM);
728 }
016bc031
JA
729
730 // remove protocol identifier
731 while (*p && *p == ' ') p++; // strip spaces
732 while (*p && *p != ' ') p++; // eat protocol identifier
733 while (*p && *p == ' ') p++; // strip trailing spaces
734
735 while (ff_rtsp_next_attr_and_value(&p,
736 attr, sizeof(attr),
824535e3 737 value, value_size)) {
016bc031
JA
738
739 res = parse_fmtp(stream, data, attr, value);
824535e3
JA
740 if (res < 0 && res != AVERROR_PATCHWELCOME) {
741 av_free(value);
016bc031 742 return res;
824535e3 743 }
016bc031 744 }
824535e3 745 av_free(value);
016bc031
JA
746 return 0;
747}