1 /*
2 * RTP input format
3 * Copyright (c) 2002 Fabrice Bellard
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 */
21
27
29
37
38 #define MIN_FEEDBACK_INTERVAL 200000 /* 200 ms in us */
39
44 };
45
50 };
51
56 };
57
62 };
63
68 };
69
74
76 /* rtp */
126 /* rdt */
132 };
133
134 /**
135 * Iterate over all registered rtp dynamic protocol handlers.
136 *
137 * @param opaque a pointer where libavformat will store the iteration state.
138 * Must point to NULL to start the iteration.
139 *
140 * @return the next registered rtp dynamic protocol handler
141 * or NULL when the iteration is finished
142 */
144 {
145 uintptr_t
i = (uintptr_t)*opaque;
147
149 *opaque = (
void*)(
i + 1);
150
152 }
153
156 {
164 }
166 }
167
170 {
174 if (
handler->static_payload_id &&
handler->static_payload_id ==
id &&
177 }
179 }
180
183 {
184 int payload_len;
187
188 switch (buf[1]) {
190 if (payload_len < 28) {
193 }
194
196 s->last_sr.ntp_timestamp =
AV_RB64(buf + 8);
197 s->last_sr.rtp_timestamp =
AV_RB32(buf + 16);
198 s->last_sr.sender_nb_packets =
AV_RB32(buf + 20);
199 s->last_sr.sender_nb_bytes =
AV_RB32(buf + 24);
200
203
205 s->first_rtcp_ntp_time =
s->last_sr.ntp_timestamp;
206 if (!
s->base_timestamp)
207 s->base_timestamp =
s->last_sr.rtp_timestamp;
208 s->rtcp_ts_offset = (
int32_t)(
s->last_sr.rtp_timestamp -
s->base_timestamp);
209 }
210
211 break;
214 }
215
216 buf += payload_len;
218 }
219 return -1;
220 }
221
222 #define RTP_SEQ_MOD (1 << 16)
223
225 {
227 s->max_seq = base_sequence;
229 }
230
231 /*
232 * Called whenever there is a large jump in sequence numbers,
233 * or when they get out of probation...
234 */
236 {
239 s->base_seq = seq - 1;
242 s->expected_prior = 0;
243 s->received_prior = 0;
246 }
247
248 /* Returns 1 if we should handle this packet. */
250 {
251 uint16_t udelta = seq -
s->max_seq;
252 const int MAX_DROPOUT = 3000;
253 const int MAX_MISORDER = 100;
254 const int MIN_SEQUENTIAL = 2;
255
256 /* source not valid until MIN_SEQUENTIAL packets with sequence
257 * seq. numbers have been received */
259 if (seq ==
s->max_seq + 1) {
262 if (
s->probation == 0) {
265 return 1;
266 }
267 } else {
268 s->probation = MIN_SEQUENTIAL - 1;
270 }
271 } else if (udelta < MAX_DROPOUT) {
272 // in order, with permissible gap
273 if (seq < s->max_seq) {
274 // sequence number wrapped; count another 64k cycles
276 }
279 // sequence made a large jump...
280 if (seq ==
s->bad_seq) {
281 /* two sequential packets -- assume that the other side
282 * restarted without telling us; just resync. */
284 } else {
286 return 0;
287 }
288 } else {
289 // duplicate or reordered packet...
290 }
292 return 1;
293 }
294
296 uint32_t arrival_timestamp)
297 {
298 // Most of this is pretty straight from RFC 3550 appendix A.8
299 uint32_t transit = arrival_timestamp - sent_timestamp;
300 uint32_t prev_transit =
s->transit;
301 int32_t d = transit - prev_transit;
302 // Doing the FFABS() call directly on the "transit - prev_transit"
303 // expression doesn't work, since it's an unsigned expression. Doing the
304 // transit calculation in unsigned is desired though, since it most
305 // probably will need to wrap around.
307 s->transit = transit;
308 if (!prev_transit)
309 return;
310 s->jitter += d - (
int32_t) ((
s->jitter + 8) >> 4);
311 }
312
315 {
317 uint8_t *buf;
319 int rtcp_bytes;
321 uint32_t lost;
322 uint32_t extended_max;
323 uint32_t expected_interval;
324 uint32_t received_interval;
326 uint32_t expected;
327 uint32_t fraction;
328
329 if ((!fd && !avio) || (count < 1))
330 return -1;
331
332 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
333 /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
334 s->octet_count += count;
337 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
338 if (rtcp_bytes < 28)
339 return -1;
340 s->last_octet_count =
s->octet_count;
341
342 if (!fd)
343 pb = avio;
345 return -1;
346
347 // Receiver Report
350 avio_wb16(pb, 7);
/* length in words - 1 */
351 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
354 // some placeholders we should really fill...
355 // RFC 1889/p64
356 extended_max =
stats->cycles +
stats->max_seq;
357 expected = extended_max -
stats->base_seq;
358 lost = expected -
stats->received;
359 lost =
FFMIN(lost, 0xffffff);
// clamp it since it's only 24 bits...
360 expected_interval = expected -
stats->expected_prior;
361 stats->expected_prior = expected;
362 received_interval =
stats->received -
stats->received_prior;
364 lost_interval = expected_interval - received_interval;
365 if (expected_interval == 0 || lost_interval <= 0)
366 fraction = 0;
367 else
368 fraction = (lost_interval << 8) / expected_interval;
369
370 fraction = (fraction << 24) | lost;
371
372 avio_wb32(pb, fraction);
/* 8 bits of fraction, 24 bits of total packets lost */
373 avio_wb32(pb, extended_max);
/* max sequence received */
375
377 avio_wb32(pb, 0);
/* last SR timestamp */
378 avio_wb32(pb, 0);
/* delay since last SR */
379 } else {
380 uint32_t middle_32_bits =
s->last_sr.ntp_timestamp >> 16;
// this is valid, right? do we need to handle 64 bit values special?
383
384 avio_wb32(pb, middle_32_bits);
/* last SR timestamp */
385 avio_wb32(pb, delay_since_last);
/* delay since last SR */
386 }
387
388 // CNAME
391 len = strlen(
s->hostname);
392 avio_wb16(pb, (7 +
len + 3) / 4);
/* length in words - 1 */
398 // padding
401
403 if (!fd)
404 return 0;
406 if ((
len > 0) && buf) {
412 }
413 return 0;
414 }
415
417 {
419
420 /* Send a small RTP packet */
421
423 bytestream_put_byte(&ptr, 0); /* Payload type */
424 bytestream_put_be16(&ptr, 0); /* Seq */
425 bytestream_put_be32(&ptr, 0); /* Timestamp */
426 bytestream_put_be32(&ptr, 0); /* SSRC */
427
429
430 /* Send a minimal RTCP RR */
431 ptr = buf;
433 bytestream_put_byte(&ptr,
RTCP_RR);
/* receiver report */
434 bytestream_put_be16(&ptr, 1); /* length in words - 1 */
435 bytestream_put_be32(&ptr, 0); /* our own SSRC */
436
438 }
439
441 uint16_t *missing_mask)
442 {
444 uint16_t next_seq =
s->seq + 1;
446
447 if (!
pkt ||
pkt->seq == next_seq)
448 return 0;
449
450 *missing_mask = 0;
451 for (
i = 1;
i <= 16;
i++) {
452 uint16_t missing_seq = next_seq +
i;
454 int16_t
diff =
pkt->seq - missing_seq;
456 break;
458 }
460 break;
461 if (
pkt->seq == missing_seq)
462 continue;
463 *missing_mask |= 1 << (
i - 1);
464 }
465
466 *first_missing = next_seq;
467 return 1;
468 }
469
472 {
473 int len, need_keyframe, missing_packets;
475 uint8_t *buf;
477 uint16_t first_missing = 0, missing_mask = 0;
478
479 if (!fd && !avio)
480 return -1;
481
482 need_keyframe =
s->handler &&
s->handler->need_keyframe &&
483 s->handler->need_keyframe(
s->dynamic_protocol_context);
485
486 if (!need_keyframe && !missing_packets)
487 return 0;
488
489 /* Send new feedback if enough time has elapsed since the last
490 * feedback packet. */
491
493 if (
s->last_feedback_time &&
495 return 0;
496 s->last_feedback_time = now;
497
498 if (!fd)
499 pb = avio;
501 return -1;
502
503 if (need_keyframe) {
506 avio_wb16(pb, 2);
/* length in words - 1 */
507 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
510 }
511
512 if (missing_packets) {
515 avio_wb16(pb, 3);
/* length in words - 1 */
518
521 }
522
524 if (!fd)
525 return 0;
527 if (
len > 0 && buf) {
530 }
531 return 0;
532 }
533
534 /**
535 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
536 * MPEG-2 TS streams.
537 */
539 int payload_type, int queue_size)
540 {
542
546 s->payload_type = payload_type;
551 s->queue_size = queue_size;
552
555
557 if (st) {
560 /* According to RFC 3551, the stream clock rate is 8000
561 * even if the sample rate is 16000. */
564 break;
570 break;
571 }
572 default:
573 break;
574 }
575 }
576 // needed to send back RTCP RR in RTSP sessions
577 gethostname(
s->hostname,
sizeof(
s->hostname));
579 }
580
583 {
584 s->dynamic_protocol_context =
ctx;
586 }
587
589 const char *params)
590 {
593 }
594
598
602 if (!prft)
604
606 /* Cast to int32_t to handle timestamp wraparound correctly */
607 delta_timestamp = (
int32_t)(timestamp -
s->last_sr.rtp_timestamp);
609
610 prft->
wallclock = rtcp_time + delta_time;
612 return 0;
613 }
614
619 if (!sr)
621
624 return 0;
625 }
626
627 /**
628 * This was the second switch in rtp_parse packet.
629 * Normalizes time, if required, sets stream_index, etc.
630 */
632 {
637 }
638
640 return; /* Timestamp already set by depacketizer */
642 return;
643
647 }
648 }
649
653
654 /* compute pts from timestamp with received ntp_time */
655 /* Cast to int32_t to handle timestamp wraparound correctly */
656 delta_timestamp = (
int32_t)(timestamp -
s->last_sr.rtp_timestamp);
657 /* convert to the PTS timebase */
658 addend =
av_rescale(
s->last_sr.ntp_timestamp -
s->first_rtcp_ntp_time,
659 s->st->time_base.den,
660 (uint64_t)
s->st->time_base.num << 32);
661 pkt->
pts =
s->range_start_offset +
s->rtcp_ts_offset + addend +
662 delta_timestamp;
663 return;
664 }
665
666 if (!
s->base_timestamp)
667 s->base_timestamp = timestamp;
668 /* assume that the difference is INT32_MIN < x < INT32_MAX,
669 * but allow the first timestamp to exceed INT32_MAX */
671 s->unwrapped_timestamp += timestamp;
672 else
673 s->unwrapped_timestamp += (
int32_t)(timestamp -
s->timestamp);
674 s->timestamp = timestamp;
675 pkt->
pts =
s->unwrapped_timestamp +
s->range_start_offset -
677 }
678
680 const uint8_t *buf,
int len)
681 {
682 unsigned int ssrc;
683 int payload_type, seq,
flags = 0;
684 int ext, csrc;
686 uint32_t timestamp;
687 int rv = 0;
688
689 csrc = buf[0] & 0x0f;
690 ext = buf[0] & 0x10;
691 payload_type = buf[1] & 0x7f;
692 if (buf[1] & 0x80)
697 /* store the ssrc in the RTPDemuxContext */
699
700 /* NOTE: we can handle only one payload type */
701 if (
s->payload_type != payload_type)
702 return -1;
703
705 // only do something with this if all the rtp checks pass...
708 "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
709 payload_type, seq, ((
s->seq + 1) & 0xffff));
710 return -1;
711 }
712
713 if (buf[0] & 0x20) {
714 int padding = buf[
len - 1];
715 if (
len >= 12 + padding)
717 }
718
721 buf += 12;
722
724 buf += 4 * csrc;
727
728 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
729 if (ext) {
731 return -1;
732 /* calculate the header extension length (stored as number
733 * of 32-bit words) */
734 ext = (
AV_RB16(buf + 2) + 1) << 2;
735
737 return -1;
738 // skip past RTP header extension
740 buf += ext;
741 }
742
743 if (
s->handler &&
s->handler->parse_packet) {
744 rv =
s->handler->parse_packet(
s->ic,
s->dynamic_protocol_context,
745 s->st,
pkt, ×tamp, buf,
len, seq,
747 } else if (st) {
749 return rv;
752 } else {
754 }
755
756 // now perform timestamp things....
758
759 return rv;
760 }
761
763 {
769 }
773 }
774
776 {
777 uint16_t seq =
AV_RB16(buf + 2);
779
780 /* Find the correct place in the queue to insert the packet */
781 while (*cur) {
782 int16_t
diff = seq - (*cur)->seq;
784 break;
786 }
787
789 if (!packet)
792 packet->seq = seq;
794 packet->buf = buf;
795 packet->next = *cur;
796 *cur = packet;
798
799 return 0;
800 }
801
803 {
804 return s->queue &&
s->queue->seq == (uint16_t) (
s->seq + 1);
805 }
806
808 {
809 return s->queue ?
s->queue->recvtime : 0;
810 }
811
813 {
814 int rv;
816
817 if (
s->queue_len <= 0)
818 return -1;
819
821 int pkt_missed =
s->queue->seq -
s->seq - 1;
822
823 if (pkt_missed < 0)
824 pkt_missed += UINT16_MAX;
826 "RTP: missed %d packets\n", pkt_missed);
827 }
828
829 /* Parse the first packet in the queue, and dequeue it */
831 next =
s->queue->next;
836 return rv;
837 }
838
840 uint8_t **bufptr,
int len)
841 {
842 uint8_t *buf = bufptr ? *bufptr :
NULL;
844 uint32_t timestamp;
845 int rv = 0;
846
847 if (!buf) {
848 /* If parsing of the previous packet actually returned 0 or an error,
849 * there's nothing more to be parsed from that packet, but we may have
850 * indicated that we can return the next enqueued packet. */
851 if (
s->prev_ret <= 0)
853 /* return the next packets, if any */
854 if (
s->handler &&
s->handler->parse_packet) {
855 /* timestamp should be overwritten by parse_packet, if not,
856 * the packet is left with pts == AV_NOPTS_VALUE */
858 rv =
s->handler->parse_packet(
s->ic,
s->dynamic_protocol_context,
862 return rv;
863 }
864 }
865
867 return -1;
868
870 return -1;
873 }
874
880 // Calculate the jitter immediately, before queueing the packet
881 // into the reordering queue.
883 }
884
885 if ((
s->seq == 0 && !
s->queue) ||
s->queue_size <= 1) {
886 /* First packet, or no reordering */
888 } else {
889 uint16_t seq =
AV_RB16(buf + 2);
890 int16_t
diff = seq -
s->seq;
892 /* Packet older than the previously emitted one, drop */
894 "RTP: dropping old packet received too late\n");
895 return -1;
896 }
else if (
diff <= 1) {
897 /* Correct packet */
899 return rv;
900 } else {
901 /* Still missing some packet, enqueue this one. */
903 if (rv < 0)
904 return rv;
906 /* Return the first enqueued packet if the queue is full,
907 * even if we're missing something */
908 if (
s->queue_len >=
s->queue_size) {
911 }
912 return -1;
913 }
914 }
915 }
916
917 /**
918 * Parse an RTP or RTCP packet directly sent as a buffer.
919 * @param s RTP parse context.
920 * @param pkt returned packet
921 * @param bufptr pointer to the input buffer or NULL to read the next packets
922 * @param len buffer len
923 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
924 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
925 */
927 uint8_t **bufptr,
int len)
928 {
929 int rv;
931 return -1;
937 }
938
940 {
944 }
945
951 const char *attr,
const char *
value))
952 {
953 char attr[256];
955 int res;
956 int value_size = strlen(
p) + 1;
957
961 }
962
963 // remove protocol identifier
964 while (*
p && *
p ==
' ')
966 while (*
p && *
p !=
' ')
967 p++;
// eat protocol identifier
968 while (*
p && *
p ==
' ')
969 p++;
// strip trailing spaces
970
972 attr, sizeof(attr),
973 value, value_size)) {
977 return res;
978 }
979 }
981 return 0;
982 }
983
985 {
988
995 }
997 }