mpegts: Share the cleanup code between the demuxer and lavf-internal parser functions
[libav.git] / libavformat / rtpdec.c
CommitLineData
8eb793c4
LA
1/*
2 * RTP input format
406792e7 3 * Copyright (c) 2002 Fabrice Bellard
8eb793c4 4 *
2912e87a 5 * This file is part of Libav.
8eb793c4 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
8eb793c4
LA
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
8eb793c4
LA
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
8eb793c4
LA
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
245976da 21
0ebcdf5c 22#include "libavutil/mathematics.h"
bb3244de 23#include "libavutil/avstring.h"
c4ef6a3e 24#include "libavutil/time.h"
9106a698 25#include "libavcodec/get_bits.h"
8eb793c4
LA
26#include "avformat.h"
27#include "mpegts.h"
8eb793c4 28#include "network.h"
424da308 29#include "srtp.h"
5d471b73 30#include "url.h"
302879cb 31#include "rtpdec.h"
965a3ddb 32#include "rtpdec_formats.h"
8eb793c4 33
86d9181c
MS
34#define MIN_FEEDBACK_INTERVAL 200000 /* 200 ms in us */
35
69673138 36static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = {
5d471b73
MS
37 .enc_name = "X-MP3-draft-00",
38 .codec_type = AVMEDIA_TYPE_AUDIO,
39 .codec_id = AV_CODEC_ID_MP3ADU,
2eeefe20
MS
40};
41
b6bf1490 42static RTPDynamicProtocolHandler speex_dynamic_handler = {
5d471b73
MS
43 .enc_name = "speex",
44 .codec_type = AVMEDIA_TYPE_AUDIO,
45 .codec_id = AV_CODEC_ID_SPEEX,
b6bf1490
DS
46};
47
c136a813 48static RTPDynamicProtocolHandler opus_dynamic_handler = {
5d471b73
MS
49 .enc_name = "opus",
50 .codec_type = AVMEDIA_TYPE_AUDIO,
51 .codec_id = AV_CODEC_ID_OPUS,
c136a813
MS
52};
53
0d85663a 54static RTPDynamicProtocolHandler *rtp_first_dynamic_payload_handler = NULL;
8eb793c4 55
0369d2b0 56void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
8eb793c4 57{
0d85663a
MS
58 handler->next = rtp_first_dynamic_payload_handler;
59 rtp_first_dynamic_payload_handler = handler;
8eb793c4
LA
60}
61
62void av_register_rtp_dynamic_payload_handlers(void)
63{
9b3788ef
JA
64 ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
65 ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
556aa7a1
RB
66 ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
67 ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
45aa9080
RB
68 ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
69 ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
08bddfcd 70 ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
0369d2b0 71 ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
89c39605 72 ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler);
3c198154 73 ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler);
e6327fba 74 ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
887af2aa 75 ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
a59096e4 76 ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
4449df6b 77 ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
1ddc176e 78 ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
51291e60 79 ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
35014efc 80 ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
69673138 81 ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
b6bf1490 82 ff_register_dynamic_payload_handler(&speex_dynamic_handler);
c136a813 83 ff_register_dynamic_payload_handler(&opus_dynamic_handler);
e9fce261
RB
84
85 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
86 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
3ece3e4c
MS
87
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);
06d7325a
MS
92
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);
8eb793c4
LA
97}
98
1e515c42 99RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
5d471b73 100 enum AVMediaType codec_type)
1e515c42
MS
101{
102 RTPDynamicProtocolHandler *handler;
0d85663a 103 for (handler = rtp_first_dynamic_payload_handler;
1e515c42 104 handler; handler = handler->next)
bb3244de 105 if (!av_strcasecmp(name, handler->enc_name) &&
1e515c42
MS
106 codec_type == handler->codec_type)
107 return handler;
108 return NULL;
109}
110
111RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
5d471b73 112 enum AVMediaType codec_type)
1e515c42
MS
113{
114 RTPDynamicProtocolHandler *handler;
0d85663a 115 for (handler = rtp_first_dynamic_payload_handler;
1e515c42
MS
116 handler; handler = handler->next)
117 if (handler->static_payload_id && handler->static_payload_id == id &&
118 codec_type == handler->codec_type)
119 return handler;
120 return NULL;
121}
122
5d471b73
MS
123static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
124 int len)
8eb793c4 125{
ff328c02 126 int payload_len;
07b77fe3
JB
127 while (len >= 4) {
128 payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
129
ff328c02
JA
130 switch (buf[1]) {
131 case RTCP_SR:
07b77fe3 132 if (payload_len < 20) {
5d471b73
MS
133 av_log(NULL, AV_LOG_ERROR,
134 "Invalid length for RTCP SR packet\n");
ff328c02
JA
135 return AVERROR_INVALIDDATA;
136 }
ff328c02 137
22c436c8 138 s->last_rtcp_reception_time = av_gettime();
5d471b73 139 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
682d28a9 140 s->last_rtcp_timestamp = AV_RB32(buf + 16);
3a1cdcc7
MS
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;
146 }
ff328c02 147
ff328c02 148 break;
b20359f5
JA
149 case RTCP_BYE:
150 return -RTCP_BYE;
ff328c02 151 }
07b77fe3
JB
152
153 buf += payload_len;
154 len -= payload_len;
ff328c02 155 }
b20359f5 156 return -1;
8eb793c4
LA
157}
158
5d471b73 159#define RTP_SEQ_MOD (1 << 16)
8eb793c4 160
48f01398 161static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
8eb793c4
LA
162{
163 memset(s, 0, sizeof(RTPStatistics));
48f01398
MS
164 s->max_seq = base_sequence;
165 s->probation = 1;
8eb793c4
LA
166}
167
48f01398 168/*
5d471b73
MS
169 * Called whenever there is a large jump in sequence numbers,
170 * or when they get out of probation...
171 */
8eb793c4
LA
172static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
173{
48f01398
MS
174 s->max_seq = seq;
175 s->cycles = 0;
176 s->base_seq = seq - 1;
177 s->bad_seq = RTP_SEQ_MOD + 1;
178 s->received = 0;
179 s->expected_prior = 0;
180 s->received_prior = 0;
181 s->jitter = 0;
182 s->transit = 0;
8eb793c4
LA
183}
184
5d471b73 185/* Returns 1 if we should handle this packet. */
8eb793c4
LA
186static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
187{
48f01398
MS
188 uint16_t udelta = seq - s->max_seq;
189 const int MAX_DROPOUT = 3000;
190 const int MAX_MISORDER = 100;
8eb793c4
LA
191 const int MIN_SEQUENTIAL = 2;
192
5d471b73
MS
193 /* source not valid until MIN_SEQUENTIAL packets with sequence
194 * seq. numbers have been received */
48f01398
MS
195 if (s->probation) {
196 if (seq == s->max_seq + 1) {
8eb793c4 197 s->probation--;
48f01398
MS
198 s->max_seq = seq;
199 if (s->probation == 0) {
8eb793c4
LA
200 rtp_init_sequence(s, seq);
201 s->received++;
202 return 1;
203 }
204 } else {
48f01398 205 s->probation = MIN_SEQUENTIAL - 1;
5d471b73 206 s->max_seq = seq;
8eb793c4
LA
207 }
208 } else if (udelta < MAX_DROPOUT) {
209 // in order, with permissible gap
48f01398
MS
210 if (seq < s->max_seq) {
211 // sequence number wrapped; count another 64k cycles
8eb793c4
LA
212 s->cycles += RTP_SEQ_MOD;
213 }
48f01398 214 s->max_seq = seq;
8eb793c4
LA
215 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
216 // sequence made a large jump...
48f01398 217 if (seq == s->bad_seq) {
5d471b73
MS
218 /* two sequential packets -- assume that the other side
219 * restarted without telling us; just resync. */
8eb793c4
LA
220 rtp_init_sequence(s, seq);
221 } else {
48f01398 222 s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
8eb793c4
LA
223 return 0;
224 }
225 } else {
226 // duplicate or reordered packet...
227 }
228 s->received++;
229 return 1;
230}
231
e568db40
MS
232static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp,
233 uint32_t arrival_timestamp)
234{
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.
243 d = FFABS(d);
244 s->transit = transit;
245 if (!prev_transit)
246 return;
247 s->jitter += d - (int32_t) ((s->jitter + 8) >> 4);
248}
249
e96406ed
MS
250int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
251 AVIOContext *avio, int count)
8eb793c4 252{
ae628ec1 253 AVIOContext *pb;
8eb793c4
LA
254 uint8_t *buf;
255 int len;
256 int rtcp_bytes;
48f01398 257 RTPStatistics *stats = &s->statistics;
8eb793c4
LA
258 uint32_t lost;
259 uint32_t extended_max;
260 uint32_t expected_interval;
261 uint32_t received_interval;
30b50f79 262 int32_t lost_interval;
8eb793c4
LA
263 uint32_t expected;
264 uint32_t fraction;
8eb793c4 265
e96406ed 266 if ((!fd && !avio) || (count < 1))
8eb793c4
LA
267 return -1;
268
269 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
5d471b73 270 /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
8eb793c4
LA
271 s->octet_count += count;
272 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
273 RTCP_TX_RATIO_DEN;
274 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
275 if (rtcp_bytes < 28)
276 return -1;
277 s->last_octet_count = s->octet_count;
278
e96406ed
MS
279 if (!fd)
280 pb = avio;
281 else if (avio_open_dyn_buf(&pb) < 0)
8eb793c4
LA
282 return -1;
283
284 // Receiver Report
77eb5504
AK
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 */
952139a3 288 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
77eb5504
AK
289 avio_wb32(pb, s->ssrc + 1);
290 avio_wb32(pb, s->ssrc); // server SSRC
8eb793c4
LA
291 // some placeholders we should really fill...
292 // RFC 1889/p64
5d471b73 293 extended_max = stats->cycles + stats->max_seq;
abae27ed 294 expected = extended_max - stats->base_seq;
5d471b73
MS
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;
48f01398 298 stats->expected_prior = expected;
5d471b73 299 received_interval = stats->received - stats->received_prior;
48f01398 300 stats->received_prior = stats->received;
5d471b73 301 lost_interval = expected_interval - received_interval;
48f01398
MS
302 if (expected_interval == 0 || lost_interval <= 0)
303 fraction = 0;
304 else
305 fraction = (lost_interval << 8) / expected_interval;
306
307 fraction = (fraction << 24) | lost;
8eb793c4 308
77eb5504
AK
309 avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
310 avio_wb32(pb, extended_max); /* max sequence received */
48f01398 311 avio_wb32(pb, stats->jitter >> 4); /* jitter */
8eb793c4 312
48f01398 313 if (s->last_rtcp_ntp_time == AV_NOPTS_VALUE) {
77eb5504
AK
314 avio_wb32(pb, 0); /* last SR timestamp */
315 avio_wb32(pb, 0); /* delay since last SR */
8eb793c4 316 } else {
5d471b73 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?
22c436c8
MS
318 uint32_t delay_since_last = av_rescale(av_gettime() - s->last_rtcp_reception_time,
319 65536, AV_TIME_BASE);
8eb793c4 320
77eb5504
AK
321 avio_wb32(pb, middle_32_bits); /* last SR timestamp */
322 avio_wb32(pb, delay_since_last); /* delay since last SR */
8eb793c4
LA
323 }
324
325 // CNAME
77eb5504
AK
326 avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
327 avio_w8(pb, RTCP_SDES);
8eb793c4 328 len = strlen(s->hostname);
ed790932 329 avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */
ad7beb2c 330 avio_wb32(pb, s->ssrc + 1);
77eb5504
AK
331 avio_w8(pb, 0x01);
332 avio_w8(pb, len);
333 avio_write(pb, s->hostname, len);
ed790932 334 avio_w8(pb, 0); /* END */
8eb793c4 335 // padding
ed790932 336 for (len = (7 + len) % 4; len % 4; len++)
77eb5504 337 avio_w8(pb, 0);
8eb793c4 338
b7f2fdde 339 avio_flush(pb);
e96406ed
MS
340 if (!fd)
341 return 0;
6dc7d80d 342 len = avio_close_dyn_buf(pb, &buf);
8eb793c4 343 if ((len > 0) && buf) {
5e1166b3 344 int av_unused result;
dfd2a005 345 av_dlog(s->ic, "sending %d bytes of RR\n", len);
3f95f0dd 346 result = ffurl_write(fd, buf, len);
925e908b 347 av_dlog(s->ic, "result from ffurl_write: %d\n", result);
8eb793c4
LA
348 av_free(buf);
349 }
350 return 0;
351}
352
5d471b73 353void ff_rtp_send_punch_packets(URLContext *rtp_handle)
9c8fa20d 354{
ae628ec1 355 AVIOContext *pb;
9c8fa20d
MS
356 uint8_t *buf;
357 int len;
358
359 /* Send a small RTP packet */
b92c5452 360 if (avio_open_dyn_buf(&pb) < 0)
9c8fa20d
MS
361 return;
362
77eb5504
AK
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 */
9c8fa20d 368
b7f2fdde 369 avio_flush(pb);
6dc7d80d 370 len = avio_close_dyn_buf(pb, &buf);
9c8fa20d 371 if ((len > 0) && buf)
925e908b 372 ffurl_write(rtp_handle, buf, len);
9c8fa20d
MS
373 av_free(buf);
374
375 /* Send a minimal RTCP RR */
b92c5452 376 if (avio_open_dyn_buf(&pb) < 0)
9c8fa20d
MS
377 return;
378
77eb5504
AK
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 */
9c8fa20d 383
b7f2fdde 384 avio_flush(pb);
6dc7d80d 385 len = avio_close_dyn_buf(pb, &buf);
9c8fa20d 386 if ((len > 0) && buf)
925e908b 387 ffurl_write(rtp_handle, buf, len);
9c8fa20d
MS
388 av_free(buf);
389}
390
86d9181c
MS
391static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,
392 uint16_t *missing_mask)
393{
394 int i;
395 uint16_t next_seq = s->seq + 1;
396 RTPPacket *pkt = s->queue;
397
398 if (!pkt || pkt->seq == next_seq)
399 return 0;
400
401 *missing_mask = 0;
402 for (i = 1; i <= 16; i++) {
403 uint16_t missing_seq = next_seq + i;
404 while (pkt) {
405 int16_t diff = pkt->seq - missing_seq;
406 if (diff >= 0)
407 break;
408 pkt = pkt->next;
409 }
410 if (!pkt)
411 break;
412 if (pkt->seq == missing_seq)
413 continue;
414 *missing_mask |= 1 << (i - 1);
415 }
416
417 *first_missing = next_seq;
418 return 1;
419}
420
421int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd,
422 AVIOContext *avio)
423{
424 int len, need_keyframe, missing_packets;
425 AVIOContext *pb;
426 uint8_t *buf;
427 int64_t now;
428 uint16_t first_missing, missing_mask;
429
430 if (!fd && !avio)
431 return -1;
432
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);
436
437 if (!need_keyframe && !missing_packets)
438 return 0;
439
440 /* Send new feedback if enough time has elapsed since the last
441 * feedback packet. */
442
443 now = av_gettime();
444 if (s->last_feedback_time &&
445 (now - s->last_feedback_time) < MIN_FEEDBACK_INTERVAL)
446 return 0;
447 s->last_feedback_time = now;
448
449 if (!fd)
450 pb = avio;
451 else if (avio_open_dyn_buf(&pb) < 0)
452 return -1;
453
454 if (need_keyframe) {
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
461 }
462
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
469
470 avio_wb16(pb, first_missing);
471 avio_wb16(pb, missing_mask);
472 }
473
474 avio_flush(pb);
475 if (!fd)
476 return 0;
477 len = avio_close_dyn_buf(pb, &buf);
478 if (len > 0 && buf) {
479 ffurl_write(fd, buf, len);
480 av_free(buf);
481 }
482 return 0;
483}
484
8eb793c4
LA
485/**
486 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
5d471b73 487 * MPEG2-TS streams to indicate that they should be demuxed inside the
36ef5369 488 * rtp demux (otherwise AV_CODEC_ID_MPEG2TS packets are returned)
8eb793c4 489 */
5d471b73 490RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st,
3f95f0dd 491 int payload_type, int queue_size)
8eb793c4
LA
492{
493 RTPDemuxContext *s;
494
495 s = av_mallocz(sizeof(RTPDemuxContext));
496 if (!s)
497 return NULL;
5d471b73
MS
498 s->payload_type = payload_type;
499 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
2cab6b48 500 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
5d471b73
MS
501 s->ic = s1;
502 s->st = st;
503 s->queue_size = queue_size;
f6804c3e 504 rtp_init_statistics(&s->statistics, 0);
8eb793c4 505 if (!strcmp(ff_rtp_enc_name(payload_type), "MP2T")) {
9125806e 506 s->ts = ff_mpegts_parse_open(s->ic);
8eb793c4
LA
507 if (s->ts == NULL) {
508 av_free(s);
509 return NULL;
510 }
45600148 511 } else if (st) {
5d471b73 512 switch (st->codec->codec_id) {
36ef5369
AK
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:
8eb793c4
LA
520 st->need_parsing = AVSTREAM_PARSE_FULL;
521 break;
36ef5369 522 case AV_CODEC_ID_VORBIS:
5602a464
JR
523 st->need_parsing = AVSTREAM_PARSE_HEADERS;
524 break;
36ef5369 525 case AV_CODEC_ID_ADPCM_G722:
0048a2a8
MS
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;
530 break;
8eb793c4
LA
531 default:
532 break;
533 }
534 }
535 // needed to send back RTCP RR in RTSP sessions
8eb793c4
LA
536 gethostname(s->hostname, sizeof(s->hostname));
537 return s;
538}
539
48f01398
MS
540void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
541 RTPDynamicProtocolHandler *handler)
99a1d191
RB
542{
543 s->dynamic_protocol_context = ctx;
42805eda 544 s->handler = handler;
99a1d191
RB
545}
546
424da308
MS
547void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite,
548 const char *params)
549{
550 if (!ff_srtp_set_crypto(&s->srtp, suite, params))
551 s->srtp_enabled = 1;
552}
553
8eb793c4 554/**
5d471b73
MS
555 * This was the second switch in rtp_parse packet.
556 * Normalizes time, if required, sets stream_index, etc.
8eb793c4
LA
557 */
558static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
559{
79d482b1
MS
560 if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
561 return; /* Timestamp already set by depacketizer */
b8a1b880
JB
562 if (timestamp == RTP_NOTS_VALUE)
563 return;
564
525c5b08 565 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
fba7815d
LA
566 int64_t addend;
567 int delta_timestamp;
568
569 /* compute pts from timestamp with received ntp_time */
570 delta_timestamp = timestamp - s->last_rtcp_timestamp;
571 /* convert to the PTS timebase */
5d471b73
MS
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);
3a1cdcc7
MS
575 pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
576 delta_timestamp;
577 return;
fba7815d 578 }
b8a1b880 579
3a1cdcc7
MS
580 if (!s->base_timestamp)
581 s->base_timestamp = timestamp;
5d471b73
MS
582 /* assume that the difference is INT32_MIN < x < INT32_MAX,
583 * but allow the first timestamp to exceed INT32_MAX */
12348ca2
JB
584 if (!s->timestamp)
585 s->unwrapped_timestamp += timestamp;
586 else
587 s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
588 s->timestamp = timestamp;
5d471b73
MS
589 pkt->pts = s->unwrapped_timestamp + s->range_start_offset -
590 s->base_timestamp;
8eb793c4
LA
591}
592
02607418
MS
593static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
594 const uint8_t *buf, int len)
8eb793c4
LA
595{
596 unsigned int ssrc, h;
f841a0fc 597 int payload_type, seq, ret, flags = 0;
9446b4bb 598 int ext;
8eb793c4
LA
599 AVStream *st;
600 uint32_t timestamp;
5d471b73 601 int rv = 0;
8eb793c4 602
5d471b73 603 ext = buf[0] & 0x10;
8eb793c4 604 payload_type = buf[1] & 0x7f;
144ae29d
RB
605 if (buf[1] & 0x80)
606 flags |= RTP_FLAG_MARKER;
5d471b73 607 seq = AV_RB16(buf + 2);
8eb793c4 608 timestamp = AV_RB32(buf + 4);
5d471b73 609 ssrc = AV_RB32(buf + 8);
8eb793c4
LA
610 /* store the ssrc in the RTPDemuxContext */
611 s->ssrc = ssrc;
612
613 /* NOTE: we can handle only one payload type */
614 if (s->payload_type != payload_type)
615 return -1;
616
617 st = s->st;
618 // only do something with this if all the rtp checks pass...
5d471b73
MS
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",
8eb793c4
LA
622 payload_type, seq, ((s->seq + 1) & 0xffff));
623 return -1;
624 }
625
4838cdab
MS
626 if (buf[0] & 0x20) {
627 int padding = buf[len - 1];
628 if (len >= 12 + padding)
629 len -= padding;
630 }
631
8eb793c4 632 s->seq = seq;
5d471b73
MS
633 len -= 12;
634 buf += 12;
8eb793c4 635
9446b4bb
RS
636 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
637 if (ext) {
638 if (len < 4)
639 return -1;
640 /* calculate the header extension length (stored as number
641 * of 32-bit words) */
642 ext = (AV_RB16(buf + 2) + 1) << 2;
643
644 if (len < ext)
645 return -1;
646 // skip past RTP header extension
647 len -= ext;
648 buf += ext;
649 }
650
8eb793c4 651 if (!st) {
5d471b73 652 /* specific MPEG2-TS demux support */
9125806e 653 ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len);
946df059
MS
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 */
4ffff367 657 if (ret < 0)
946df059 658 return AVERROR(EAGAIN);
8eb793c4 659 if (ret < len) {
81ef5192 660 s->read_buf_size = FFMIN(len - ret, sizeof(s->buf));
8eb793c4
LA
661 memcpy(s->buf, buf + ret, s->read_buf_size);
662 s->read_buf_index = 0;
663 return 1;
664 }
f3e71942 665 return 0;
42805eda
MS
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, &timestamp, buf, len, seq,
669 flags);
8eb793c4 670 } else {
5d471b73
MS
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) {
36ef5369
AK
674 case AV_CODEC_ID_MP2:
675 case AV_CODEC_ID_MP3:
5d471b73 676 /* better than nothing: skip MPEG audio RTP header */
8eb793c4
LA
677 if (len <= 4)
678 return -1;
5d471b73 679 h = AV_RB32(buf);
8eb793c4
LA
680 len -= 4;
681 buf += 4;
682 av_new_packet(pkt, len);
683 memcpy(pkt->data, buf, len);
684 break;
36ef5369
AK
685 case AV_CODEC_ID_MPEG1VIDEO:
686 case AV_CODEC_ID_MPEG2VIDEO:
5d471b73 687 /* better than nothing: skip MPEG video RTP header */
8eb793c4
LA
688 if (len <= 4)
689 return -1;
5d471b73 690 h = AV_RB32(buf);
8eb793c4
LA
691 buf += 4;
692 len -= 4;
693 if (h & (1 << 26)) {
5d471b73 694 /* MPEG-2 */
8eb793c4
LA
695 if (len <= 4)
696 return -1;
697 buf += 4;
698 len -= 4;
699 }
700 av_new_packet(pkt, len);
701 memcpy(pkt->data, buf, len);
702 break;
8eb793c4 703 default:
f739b36d
RB
704 av_new_packet(pkt, len);
705 memcpy(pkt->data, buf, len);
8eb793c4
LA
706 break;
707 }
eafb17d1
RB
708
709 pkt->stream_index = st->index;
f3e71942 710 }
8eb793c4 711
95f03cf3
RB
712 // now perform timestamp things....
713 finalize_packet(s, pkt, timestamp);
f3e71942 714
8eb793c4
LA
715 return rv;
716}
717
58ee0991
MS
718void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
719{
720 while (s->queue) {
721 RTPPacket *next = s->queue->next;
722 av_free(s->queue->buf);
723 av_free(s->queue);
724 s->queue = next;
725 }
726 s->seq = 0;
727 s->queue_len = 0;
728 s->prev_ret = 0;
729}
730
731static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
732{
5d471b73 733 uint16_t seq = AV_RB16(buf + 2);
d0fe217e 734 RTPPacket **cur = &s->queue, *packet;
58ee0991
MS
735
736 /* Find the correct place in the queue to insert the packet */
d0fe217e
MS
737 while (*cur) {
738 int16_t diff = seq - (*cur)->seq;
58ee0991
MS
739 if (diff < 0)
740 break;
d0fe217e 741 cur = &(*cur)->next;
58ee0991
MS
742 }
743
744 packet = av_mallocz(sizeof(*packet));
745 if (!packet)
746 return;
747 packet->recvtime = av_gettime();
5d471b73
MS
748 packet->seq = seq;
749 packet->len = len;
750 packet->buf = buf;
d0fe217e
MS
751 packet->next = *cur;
752 *cur = packet;
58ee0991
MS
753 s->queue_len++;
754}
755
756static int has_next_packet(RTPDemuxContext *s)
757{
ddcf8411 758 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
58ee0991
MS
759}
760
761int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
762{
763 return s->queue ? s->queue->recvtime : 0;
764}
765
766static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
767{
768 int rv;
769 RTPPacket *next;
770
771 if (s->queue_len <= 0)
772 return -1;
773
774 if (!has_next_packet(s))
775 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
776 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
777
778 /* Parse the first packet in the queue, and dequeue it */
5d471b73 779 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
58ee0991
MS
780 next = s->queue->next;
781 av_free(s->queue->buf);
782 av_free(s->queue);
783 s->queue = next;
784 s->queue_len--;
4ffff367 785 return rv;
58ee0991
MS
786}
787
4ffff367 788static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
48f01398 789 uint8_t **bufptr, int len)
02607418 790{
5d471b73 791 uint8_t *buf = bufptr ? *bufptr : NULL;
02607418
MS
792 int ret, flags = 0;
793 uint32_t timestamp;
5d471b73 794 int rv = 0;
02607418
MS
795
796 if (!buf) {
f6e138b4
MS
797 /* If parsing of the previous packet actually returned 0 or an error,
798 * there's nothing more to be parsed from that packet, but we may have
58ee0991 799 * indicated that we can return the next enqueued packet. */
f6e138b4 800 if (s->prev_ret <= 0)
58ee0991 801 return rtp_parse_queued_packet(s, pkt);
02607418 802 /* return the next packets, if any */
42805eda 803 if (s->st && s->handler && s->handler->parse_packet) {
02607418
MS
804 /* timestamp should be overwritten by parse_packet, if not,
805 * the packet is left with pts == AV_NOPTS_VALUE */
806 timestamp = RTP_NOTS_VALUE;
42805eda
MS
807 rv = s->handler->parse_packet(s->ic, s->dynamic_protocol_context,
808 s->st, pkt, &timestamp, NULL, 0, 0,
809 flags);
02607418 810 finalize_packet(s, pkt, timestamp);
4ffff367 811 return rv;
02607418
MS
812 } else {
813 // TODO: Move to a dynamic packet handler (like above)
4ffff367 814 if (s->read_buf_index >= s->read_buf_size)
91ec7aea 815 return AVERROR(EAGAIN);
02607418 816 ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
5d471b73 817 s->read_buf_size - s->read_buf_index);
4ffff367 818 if (ret < 0)
946df059 819 return AVERROR(EAGAIN);
02607418
MS
820 s->read_buf_index += ret;
821 if (s->read_buf_index < s->read_buf_size)
822 return 1;
4ffff367
MS
823 else
824 return 0;
02607418
MS
825 }
826 }
827
828 if (len < 12)
829 return -1;
830
831 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
832 return -1;
298a587f 833 if (RTP_PT_IS_RTCP(buf[1])) {
02607418
MS
834 return rtcp_parse_packet(s, buf, len);
835 }
836
e568db40
MS
837 if (s->st) {
838 int64_t received = av_gettime();
839 uint32_t arrival_ts = av_rescale_q(received, AV_TIME_BASE_Q,
840 s->st->time_base);
841 timestamp = AV_RB32(buf + 4);
842 // Calculate the jitter immediately, before queueing the packet
843 // into the reordering queue.
844 rtcp_update_jitter(&s->statistics, timestamp, arrival_ts);
845 }
846
65cdee9c 847 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
58ee0991
MS
848 /* First packet, or no reordering */
849 return rtp_parse_packet_internal(s, pkt, buf, len);
850 } else {
851 uint16_t seq = AV_RB16(buf + 2);
852 int16_t diff = seq - s->seq;
853 if (diff < 0) {
854 /* Packet older than the previously emitted one, drop */
855 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
856 "RTP: dropping old packet received too late\n");
857 return -1;
858 } else if (diff <= 1) {
859 /* Correct packet */
860 rv = rtp_parse_packet_internal(s, pkt, buf, len);
4ffff367 861 return rv;
58ee0991
MS
862 } else {
863 /* Still missing some packet, enqueue this one. */
864 enqueue_packet(s, buf, len);
865 *bufptr = NULL;
866 /* Return the first enqueued packet if the queue is full,
867 * even if we're missing something */
868 if (s->queue_len >= s->queue_size)
869 return rtp_parse_queued_packet(s, pkt);
870 return -1;
871 }
872 }
02607418
MS
873}
874
4ffff367
MS
875/**
876 * Parse an RTP or RTCP packet directly sent as a buffer.
877 * @param s RTP parse context.
878 * @param pkt returned packet
879 * @param bufptr pointer to the input buffer or NULL to read the next packets
880 * @param len buffer len
881 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
882 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
883 */
bfc6db44
MS
884int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
885 uint8_t **bufptr, int len)
4ffff367 886{
424da308
MS
887 int rv;
888 if (s->srtp_enabled && bufptr && ff_srtp_decrypt(&s->srtp, *bufptr, &len) < 0)
889 return -1;
890 rv = rtp_parse_one_packet(s, pkt, bufptr, len);
4ffff367 891 s->prev_ret = rv;
d678a6fd
MS
892 while (rv == AVERROR(EAGAIN) && has_next_packet(s))
893 rv = rtp_parse_queued_packet(s, pkt);
4ffff367
MS
894 return rv ? rv : has_next_packet(s);
895}
896
bfc6db44 897void ff_rtp_parse_close(RTPDemuxContext *s)
8eb793c4 898{
58ee0991 899 ff_rtp_reset_packet_queue(s);
8eb793c4 900 if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
9125806e 901 ff_mpegts_parse_close(s->ts);
8eb793c4 902 }
424da308 903 ff_srtp_free(&s->srtp);
8eb793c4
LA
904 av_free(s);
905}
016bc031
JA
906
907int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p,
908 int (*parse_fmtp)(AVStream *stream,
909 PayloadContext *data,
910 char *attr, char *value))
911{
912 char attr[256];
824535e3 913 char *value;
016bc031 914 int res;
824535e3
JA
915 int value_size = strlen(p) + 1;
916
917 if (!(value = av_malloc(value_size))) {
c3e15f7b 918 av_log(NULL, AV_LOG_ERROR, "Failed to allocate data for FMTP.");
824535e3
JA
919 return AVERROR(ENOMEM);
920 }
016bc031
JA
921
922 // remove protocol identifier
5d471b73
MS
923 while (*p && *p == ' ')
924 p++; // strip spaces
925 while (*p && *p != ' ')
926 p++; // eat protocol identifier
927 while (*p && *p == ' ')
928 p++; // strip trailing spaces
016bc031
JA
929
930 while (ff_rtsp_next_attr_and_value(&p,
931 attr, sizeof(attr),
824535e3 932 value, value_size)) {
016bc031 933 res = parse_fmtp(stream, data, attr, value);
824535e3
JA
934 if (res < 0 && res != AVERROR_PATCHWELCOME) {
935 av_free(value);
016bc031 936 return res;
824535e3 937 }
016bc031 938 }
824535e3 939 av_free(value);
016bc031
JA
940 return 0;
941}
179a5c37
AK
942
943int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
944{
945 av_init_packet(pkt);
946
5d471b73 947 pkt->size = avio_close_dyn_buf(*dyn_buf, &pkt->data);
179a5c37
AK
948 pkt->stream_index = stream_idx;
949 pkt->destruct = av_destruct_packet;
5d471b73 950 *dyn_buf = NULL;
179a5c37
AK
951 return pkt->size;
952}