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 };
75
80
82 /* rtp */
131 /* rdt */
137 };
138
139 /**
140 * Iterate over all registered rtp dynamic protocol handlers.
141 *
142 * @param opaque a pointer where libavformat will store the iteration state.
143 * Must point to NULL to start the iteration.
144 *
145 * @return the next registered rtp dynamic protocol handler
146 * or NULL when the iteration is finished
147 */
149 {
150 uintptr_t
i = (uintptr_t)*opaque;
152
154 *opaque = (
void*)(
i + 1);
155
157 }
158
161 {
169 }
171 }
172
175 {
179 if (
handler->static_payload_id &&
handler->static_payload_id ==
id &&
182 }
184 }
185
188 {
189 int payload_len;
192
193 switch (buf[1]) {
195 if (payload_len < 20) {
198 }
199
201 s->last_rtcp_ntp_time =
AV_RB64(buf + 8);
202 s->last_rtcp_timestamp =
AV_RB32(buf + 16);
204 s->first_rtcp_ntp_time =
s->last_rtcp_ntp_time;
205 if (!
s->base_timestamp)
206 s->base_timestamp =
s->last_rtcp_timestamp;
207 s->rtcp_ts_offset = (
int32_t)(
s->last_rtcp_timestamp -
s->base_timestamp);
208 }
209
210 break;
213 }
214
215 buf += payload_len;
217 }
218 return -1;
219 }
220
221 #define RTP_SEQ_MOD (1 << 16)
222
224 {
226 s->max_seq = base_sequence;
228 }
229
230 /*
231 * Called whenever there is a large jump in sequence numbers,
232 * or when they get out of probation...
233 */
235 {
238 s->base_seq = seq - 1;
241 s->expected_prior = 0;
242 s->received_prior = 0;
245 }
246
247 /* Returns 1 if we should handle this packet. */
249 {
250 uint16_t udelta = seq -
s->max_seq;
251 const int MAX_DROPOUT = 3000;
252 const int MAX_MISORDER = 100;
253 const int MIN_SEQUENTIAL = 2;
254
255 /* source not valid until MIN_SEQUENTIAL packets with sequence
256 * seq. numbers have been received */
258 if (seq ==
s->max_seq + 1) {
261 if (
s->probation == 0) {
264 return 1;
265 }
266 } else {
267 s->probation = MIN_SEQUENTIAL - 1;
269 }
270 } else if (udelta < MAX_DROPOUT) {
271 // in order, with permissible gap
272 if (seq < s->max_seq) {
273 // sequence number wrapped; count another 64k cycles
275 }
278 // sequence made a large jump...
279 if (seq ==
s->bad_seq) {
280 /* two sequential packets -- assume that the other side
281 * restarted without telling us; just resync. */
283 } else {
285 return 0;
286 }
287 } else {
288 // duplicate or reordered packet...
289 }
291 return 1;
292 }
293
295 uint32_t arrival_timestamp)
296 {
297 // Most of this is pretty straight from RFC 3550 appendix A.8
298 uint32_t transit = arrival_timestamp - sent_timestamp;
299 uint32_t prev_transit =
s->transit;
300 int32_t d = transit - prev_transit;
301 // Doing the FFABS() call directly on the "transit - prev_transit"
302 // expression doesn't work, since it's an unsigned expression. Doing the
303 // transit calculation in unsigned is desired though, since it most
304 // probably will need to wrap around.
306 s->transit = transit;
307 if (!prev_transit)
308 return;
309 s->jitter += d - (
int32_t) ((
s->jitter + 8) >> 4);
310 }
311
314 {
316 uint8_t *buf;
318 int rtcp_bytes;
320 uint32_t lost;
321 uint32_t extended_max;
322 uint32_t expected_interval;
323 uint32_t received_interval;
325 uint32_t expected;
326 uint32_t fraction;
327
328 if ((!fd && !avio) || (count < 1))
329 return -1;
330
331 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
332 /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
333 s->octet_count += count;
336 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
337 if (rtcp_bytes < 28)
338 return -1;
339 s->last_octet_count =
s->octet_count;
340
341 if (!fd)
342 pb = avio;
344 return -1;
345
346 // Receiver Report
349 avio_wb16(pb, 7);
/* length in words - 1 */
350 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
353 // some placeholders we should really fill...
354 // RFC 1889/p64
355 extended_max =
stats->cycles +
stats->max_seq;
356 expected = extended_max -
stats->base_seq;
357 lost = expected -
stats->received;
358 lost =
FFMIN(lost, 0xffffff);
// clamp it since it's only 24 bits...
359 expected_interval = expected -
stats->expected_prior;
360 stats->expected_prior = expected;
361 received_interval =
stats->received -
stats->received_prior;
363 lost_interval = expected_interval - received_interval;
364 if (expected_interval == 0 || lost_interval <= 0)
365 fraction = 0;
366 else
367 fraction = (lost_interval << 8) / expected_interval;
368
369 fraction = (fraction << 24) | lost;
370
371 avio_wb32(pb, fraction);
/* 8 bits of fraction, 24 bits of total packets lost */
372 avio_wb32(pb, extended_max);
/* max sequence received */
374
376 avio_wb32(pb, 0);
/* last SR timestamp */
377 avio_wb32(pb, 0);
/* delay since last SR */
378 } else {
379 uint32_t middle_32_bits =
s->last_rtcp_ntp_time >> 16;
// this is valid, right? do we need to handle 64 bit values special?
382
383 avio_wb32(pb, middle_32_bits);
/* last SR timestamp */
384 avio_wb32(pb, delay_since_last);
/* delay since last SR */
385 }
386
387 // CNAME
390 len = strlen(
s->hostname);
391 avio_wb16(pb, (7 +
len + 3) / 4);
/* length in words - 1 */
397 // padding
400
402 if (!fd)
403 return 0;
405 if ((
len > 0) && buf) {
411 }
412 return 0;
413 }
414
416 {
418
419 /* Send a small RTP packet */
420
422 bytestream_put_byte(&ptr, 0); /* Payload type */
423 bytestream_put_be16(&ptr, 0); /* Seq */
424 bytestream_put_be32(&ptr, 0); /* Timestamp */
425 bytestream_put_be32(&ptr, 0); /* SSRC */
426
428
429 /* Send a minimal RTCP RR */
430 ptr = buf;
432 bytestream_put_byte(&ptr,
RTCP_RR);
/* receiver report */
433 bytestream_put_be16(&ptr, 1); /* length in words - 1 */
434 bytestream_put_be32(&ptr, 0); /* our own SSRC */
435
437 }
438
440 uint16_t *missing_mask)
441 {
443 uint16_t next_seq =
s->seq + 1;
445
446 if (!
pkt ||
pkt->seq == next_seq)
447 return 0;
448
449 *missing_mask = 0;
450 for (
i = 1;
i <= 16;
i++) {
451 uint16_t missing_seq = next_seq +
i;
453 int16_t
diff =
pkt->seq - missing_seq;
455 break;
457 }
459 break;
460 if (
pkt->seq == missing_seq)
461 continue;
462 *missing_mask |= 1 << (
i - 1);
463 }
464
465 *first_missing = next_seq;
466 return 1;
467 }
468
471 {
472 int len, need_keyframe, missing_packets;
474 uint8_t *buf;
476 uint16_t first_missing = 0, missing_mask = 0;
477
478 if (!fd && !avio)
479 return -1;
480
481 need_keyframe =
s->handler &&
s->handler->need_keyframe &&
482 s->handler->need_keyframe(
s->dynamic_protocol_context);
484
485 if (!need_keyframe && !missing_packets)
486 return 0;
487
488 /* Send new feedback if enough time has elapsed since the last
489 * feedback packet. */
490
492 if (
s->last_feedback_time &&
494 return 0;
495 s->last_feedback_time = now;
496
497 if (!fd)
498 pb = avio;
500 return -1;
501
502 if (need_keyframe) {
505 avio_wb16(pb, 2);
/* length in words - 1 */
506 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
509 }
510
511 if (missing_packets) {
514 avio_wb16(pb, 3);
/* length in words - 1 */
517
520 }
521
523 if (!fd)
524 return 0;
526 if (
len > 0 && buf) {
529 }
530 return 0;
531 }
532
534 {
535 uint8_t *bs;
537
538 /* This function writes an extradata with a channel mapping family of 0.
539 * This mapping family only supports mono and stereo layouts. And RFC7587
540 * specifies that the number of channels in the SDP must be 2.
541 */
544 }
545
549
551
552 /* Opus magic */
554 /* Version */
555 bytestream_put_byte (&bs, 0x1);
556 /* Channel count */
558 /* Pre skip */
559 bytestream_put_le16 (&bs, 0);
560 /* Input sample rate */
561 bytestream_put_le32 (&bs, 48000);
562 /* Output gain */
563 bytestream_put_le16 (&bs, 0x0);
564 /* Mapping family */
565 bytestream_put_byte (&bs, 0x0);
566
567 return 0;
568 }
569
570 /**
571 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
572 * MPEG-2 TS streams.
573 */
575 int payload_type, int queue_size)
576 {
579
583 s->payload_type = payload_type;
588 s->queue_size = queue_size;
589
592
594 if (st) {
597 /* According to RFC 3551, the stream clock rate is 8000
598 * even if the sample rate is 16000. */
601 break;
606 "Error creating opus extradata: %s\n",
610 }
611 break;
612 default:
613 break;
614 }
615 }
616 // needed to send back RTCP RR in RTSP sessions
617 gethostname(
s->hostname,
sizeof(
s->hostname));
619 }
620
623 {
624 s->dynamic_protocol_context =
ctx;
626 }
627
629 const char *params)
630 {
633 }
634
636 int64_t rtcp_time, delta_timestamp, delta_time;
637
641 if (!prft)
643
645 delta_timestamp = (
int64_t)timestamp - (
int64_t)
s->last_rtcp_timestamp;
647
648 prft->
wallclock = rtcp_time + delta_time;
650 return 0;
651 }
652
653 /**
654 * This was the second switch in rtp_parse packet.
655 * Normalizes time, if required, sets stream_index, etc.
656 */
658 {
660 return; /* Timestamp already set by depacketizer */
662 return;
663
667 }
668 }
669
672 int delta_timestamp;
673
674 /* compute pts from timestamp with received ntp_time */
675 delta_timestamp = timestamp -
s->last_rtcp_timestamp;
676 /* convert to the PTS timebase */
677 addend =
av_rescale(
s->last_rtcp_ntp_time -
s->first_rtcp_ntp_time,
678 s->st->time_base.den,
679 (uint64_t)
s->st->time_base.num << 32);
680 pkt->
pts =
s->range_start_offset +
s->rtcp_ts_offset + addend +
681 delta_timestamp;
682 return;
683 }
684
685 if (!
s->base_timestamp)
686 s->base_timestamp = timestamp;
687 /* assume that the difference is INT32_MIN < x < INT32_MAX,
688 * but allow the first timestamp to exceed INT32_MAX */
690 s->unwrapped_timestamp += timestamp;
691 else
692 s->unwrapped_timestamp += (
int32_t)(timestamp -
s->timestamp);
693 s->timestamp = timestamp;
694 pkt->
pts =
s->unwrapped_timestamp +
s->range_start_offset -
696 }
697
699 const uint8_t *buf,
int len)
700 {
701 unsigned int ssrc;
702 int payload_type, seq,
flags = 0;
703 int ext, csrc;
705 uint32_t timestamp;
706 int rv = 0;
707
708 csrc = buf[0] & 0x0f;
709 ext = buf[0] & 0x10;
710 payload_type = buf[1] & 0x7f;
711 if (buf[1] & 0x80)
716 /* store the ssrc in the RTPDemuxContext */
718
719 /* NOTE: we can handle only one payload type */
720 if (
s->payload_type != payload_type)
721 return -1;
722
724 // only do something with this if all the rtp checks pass...
727 "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
728 payload_type, seq, ((
s->seq + 1) & 0xffff));
729 return -1;
730 }
731
732 if (buf[0] & 0x20) {
733 int padding = buf[
len - 1];
734 if (
len >= 12 + padding)
736 }
737
740 buf += 12;
741
743 buf += 4 * csrc;
746
747 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
748 if (ext) {
750 return -1;
751 /* calculate the header extension length (stored as number
752 * of 32-bit words) */
753 ext = (
AV_RB16(buf + 2) + 1) << 2;
754
756 return -1;
757 // skip past RTP header extension
759 buf += ext;
760 }
761
762 if (
s->handler &&
s->handler->parse_packet) {
763 rv =
s->handler->parse_packet(
s->ic,
s->dynamic_protocol_context,
764 s->st,
pkt, ×tamp, buf,
len, seq,
766 } else if (st) {
768 return rv;
771 } else {
773 }
774
775 // now perform timestamp things....
777
778 return rv;
779 }
780
782 {
788 }
792 }
793
795 {
796 uint16_t seq =
AV_RB16(buf + 2);
798
799 /* Find the correct place in the queue to insert the packet */
800 while (*cur) {
801 int16_t
diff = seq - (*cur)->seq;
803 break;
805 }
806
808 if (!packet)
811 packet->seq = seq;
813 packet->buf = buf;
814 packet->next = *cur;
815 *cur = packet;
817
818 return 0;
819 }
820
822 {
823 return s->queue &&
s->queue->seq == (uint16_t) (
s->seq + 1);
824 }
825
827 {
828 return s->queue ?
s->queue->recvtime : 0;
829 }
830
832 {
833 int rv;
835
836 if (
s->queue_len <= 0)
837 return -1;
838
840 int pkt_missed =
s->queue->seq -
s->seq - 1;
841
842 if (pkt_missed < 0)
843 pkt_missed += UINT16_MAX;
845 "RTP: missed %d packets\n", pkt_missed);
846 }
847
848 /* Parse the first packet in the queue, and dequeue it */
850 next =
s->queue->next;
855 return rv;
856 }
857
859 uint8_t **bufptr,
int len)
860 {
861 uint8_t *buf = bufptr ? *bufptr :
NULL;
863 uint32_t timestamp;
864 int rv = 0;
865
866 if (!buf) {
867 /* If parsing of the previous packet actually returned 0 or an error,
868 * there's nothing more to be parsed from that packet, but we may have
869 * indicated that we can return the next enqueued packet. */
870 if (
s->prev_ret <= 0)
872 /* return the next packets, if any */
873 if (
s->handler &&
s->handler->parse_packet) {
874 /* timestamp should be overwritten by parse_packet, if not,
875 * the packet is left with pts == AV_NOPTS_VALUE */
877 rv =
s->handler->parse_packet(
s->ic,
s->dynamic_protocol_context,
881 return rv;
882 }
883 }
884
886 return -1;
887
889 return -1;
892 }
893
899 // Calculate the jitter immediately, before queueing the packet
900 // into the reordering queue.
902 }
903
904 if ((
s->seq == 0 && !
s->queue) ||
s->queue_size <= 1) {
905 /* First packet, or no reordering */
907 } else {
908 uint16_t seq =
AV_RB16(buf + 2);
909 int16_t
diff = seq -
s->seq;
911 /* Packet older than the previously emitted one, drop */
913 "RTP: dropping old packet received too late\n");
914 return -1;
915 }
else if (
diff <= 1) {
916 /* Correct packet */
918 return rv;
919 } else {
920 /* Still missing some packet, enqueue this one. */
922 if (rv < 0)
923 return rv;
925 /* Return the first enqueued packet if the queue is full,
926 * even if we're missing something */
927 if (
s->queue_len >=
s->queue_size) {
930 }
931 return -1;
932 }
933 }
934 }
935
936 /**
937 * Parse an RTP or RTCP packet directly sent as a buffer.
938 * @param s RTP parse context.
939 * @param pkt returned packet
940 * @param bufptr pointer to the input buffer or NULL to read the next packets
941 * @param len buffer len
942 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
943 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
944 */
946 uint8_t **bufptr,
int len)
947 {
948 int rv;
950 return -1;
956 }
957
959 {
963 }
964
970 const char *attr,
const char *
value))
971 {
972 char attr[256];
974 int res;
975 int value_size = strlen(p) + 1;
976
980 }
981
982 // remove protocol identifier
983 while (*p && *p == ' ')
984 p++; // strip spaces
985 while (*p && *p != ' ')
986 p++; // eat protocol identifier
987 while (*p && *p == ' ')
988 p++; // strip trailing spaces
989
991 attr, sizeof(attr),
992 value, value_size)) {
996 return res;
997 }
998 }
1000 return 0;
1001 }
1002
1004 {
1007
1014 }
1016 }