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
26
28
36
37 #define MIN_FEEDBACK_INTERVAL 200000 /* 200 ms in us */
38
43 };
44
49 };
50
55 };
56
61 };
62
67 };
68
73 };
74
79
81 /* rtp */
130 /* rdt */
136 };
137
138 /**
139 * Iterate over all registered rtp dynamic protocol handlers.
140 *
141 * @param opaque a pointer where libavformat will store the iteration state.
142 * Must point to NULL to start the iteration.
143 *
144 * @return the next registered rtp dynamic protocol handler
145 * or NULL when the iteration is finished
146 */
148 {
149 uintptr_t
i = (uintptr_t)*opaque;
151
153 *opaque = (
void*)(
i + 1);
154
156 }
157
160 {
168 }
170 }
171
174 {
178 if (
handler->static_payload_id &&
handler->static_payload_id ==
id &&
181 }
183 }
184
187 {
188 int payload_len;
191
192 switch (buf[1]) {
194 if (payload_len < 20) {
197 }
198
200 s->last_rtcp_ntp_time =
AV_RB64(buf + 8);
201 s->last_rtcp_timestamp =
AV_RB32(buf + 16);
203 s->first_rtcp_ntp_time =
s->last_rtcp_ntp_time;
204 if (!
s->base_timestamp)
205 s->base_timestamp =
s->last_rtcp_timestamp;
206 s->rtcp_ts_offset = (
int32_t)(
s->last_rtcp_timestamp -
s->base_timestamp);
207 }
208
209 break;
212 }
213
214 buf += payload_len;
216 }
217 return -1;
218 }
219
220 #define RTP_SEQ_MOD (1 << 16)
221
223 {
225 s->max_seq = base_sequence;
227 }
228
229 /*
230 * Called whenever there is a large jump in sequence numbers,
231 * or when they get out of probation...
232 */
234 {
237 s->base_seq = seq - 1;
240 s->expected_prior = 0;
241 s->received_prior = 0;
244 }
245
246 /* Returns 1 if we should handle this packet. */
248 {
249 uint16_t udelta = seq -
s->max_seq;
250 const int MAX_DROPOUT = 3000;
251 const int MAX_MISORDER = 100;
252 const int MIN_SEQUENTIAL = 2;
253
254 /* source not valid until MIN_SEQUENTIAL packets with sequence
255 * seq. numbers have been received */
257 if (seq ==
s->max_seq + 1) {
260 if (
s->probation == 0) {
263 return 1;
264 }
265 } else {
266 s->probation = MIN_SEQUENTIAL - 1;
268 }
269 } else if (udelta < MAX_DROPOUT) {
270 // in order, with permissible gap
271 if (seq < s->max_seq) {
272 // sequence number wrapped; count another 64k cycles
274 }
277 // sequence made a large jump...
278 if (seq ==
s->bad_seq) {
279 /* two sequential packets -- assume that the other side
280 * restarted without telling us; just resync. */
282 } else {
284 return 0;
285 }
286 } else {
287 // duplicate or reordered packet...
288 }
290 return 1;
291 }
292
294 uint32_t arrival_timestamp)
295 {
296 // Most of this is pretty straight from RFC 3550 appendix A.8
297 uint32_t transit = arrival_timestamp - sent_timestamp;
298 uint32_t prev_transit =
s->transit;
300 // Doing the FFABS() call directly on the "transit - prev_transit"
301 // expression doesn't work, since it's an unsigned expression. Doing the
302 // transit calculation in unsigned is desired though, since it most
303 // probably will need to wrap around.
305 s->transit = transit;
306 if (!prev_transit)
307 return;
308 s->jitter +=
d - (
int32_t) ((
s->jitter + 8) >> 4);
309 }
310
313 {
315 uint8_t *buf;
317 int rtcp_bytes;
319 uint32_t lost;
320 uint32_t extended_max;
321 uint32_t expected_interval;
322 uint32_t received_interval;
324 uint32_t expected;
325 uint32_t fraction;
326
327 if ((!fd && !avio) || (count < 1))
328 return -1;
329
330 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
331 /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
332 s->octet_count += count;
335 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
336 if (rtcp_bytes < 28)
337 return -1;
338 s->last_octet_count =
s->octet_count;
339
340 if (!fd)
341 pb = avio;
343 return -1;
344
345 // Receiver Report
348 avio_wb16(pb, 7);
/* length in words - 1 */
349 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
352 // some placeholders we should really fill...
353 // RFC 1889/p64
354 extended_max =
stats->cycles +
stats->max_seq;
355 expected = extended_max -
stats->base_seq;
356 lost = expected -
stats->received;
357 lost =
FFMIN(lost, 0xffffff);
// clamp it since it's only 24 bits...
358 expected_interval = expected -
stats->expected_prior;
359 stats->expected_prior = expected;
360 received_interval =
stats->received -
stats->received_prior;
362 lost_interval = expected_interval - received_interval;
363 if (expected_interval == 0 || lost_interval <= 0)
364 fraction = 0;
365 else
366 fraction = (lost_interval << 8) / expected_interval;
367
368 fraction = (fraction << 24) | lost;
369
370 avio_wb32(pb, fraction);
/* 8 bits of fraction, 24 bits of total packets lost */
371 avio_wb32(pb, extended_max);
/* max sequence received */
373
375 avio_wb32(pb, 0);
/* last SR timestamp */
376 avio_wb32(pb, 0);
/* delay since last SR */
377 } else {
378 uint32_t middle_32_bits =
s->last_rtcp_ntp_time >> 16;
// this is valid, right? do we need to handle 64 bit values special?
381
382 avio_wb32(pb, middle_32_bits);
/* last SR timestamp */
383 avio_wb32(pb, delay_since_last);
/* delay since last SR */
384 }
385
386 // CNAME
389 len = strlen(
s->hostname);
390 avio_wb16(pb, (7 +
len + 3) / 4);
/* length in words - 1 */
396 // padding
399
401 if (!fd)
402 return 0;
404 if ((
len > 0) && buf) {
410 }
411 return 0;
412 }
413
415 {
417
418 /* Send a small RTP packet */
419
421 bytestream_put_byte(&ptr, 0); /* Payload type */
422 bytestream_put_be16(&ptr, 0); /* Seq */
423 bytestream_put_be32(&ptr, 0); /* Timestamp */
424 bytestream_put_be32(&ptr, 0); /* SSRC */
425
427
428 /* Send a minimal RTCP RR */
429 ptr = buf;
431 bytestream_put_byte(&ptr,
RTCP_RR);
/* receiver report */
432 bytestream_put_be16(&ptr, 1); /* length in words - 1 */
433 bytestream_put_be32(&ptr, 0); /* our own SSRC */
434
436 }
437
439 uint16_t *missing_mask)
440 {
442 uint16_t next_seq =
s->seq + 1;
444
445 if (!
pkt ||
pkt->seq == next_seq)
446 return 0;
447
448 *missing_mask = 0;
449 for (
i = 1;
i <= 16;
i++) {
450 uint16_t missing_seq = next_seq +
i;
452 int16_t
diff =
pkt->seq - missing_seq;
454 break;
456 }
458 break;
459 if (
pkt->seq == missing_seq)
460 continue;
461 *missing_mask |= 1 << (
i - 1);
462 }
463
464 *first_missing = next_seq;
465 return 1;
466 }
467
470 {
471 int len, need_keyframe, missing_packets;
473 uint8_t *buf;
474 int64_t now;
475 uint16_t first_missing = 0, missing_mask = 0;
476
477 if (!fd && !avio)
478 return -1;
479
480 need_keyframe =
s->handler &&
s->handler->need_keyframe &&
481 s->handler->need_keyframe(
s->dynamic_protocol_context);
483
484 if (!need_keyframe && !missing_packets)
485 return 0;
486
487 /* Send new feedback if enough time has elapsed since the last
488 * feedback packet. */
489
491 if (
s->last_feedback_time &&
493 return 0;
494 s->last_feedback_time = now;
495
496 if (!fd)
497 pb = avio;
499 return -1;
500
501 if (need_keyframe) {
504 avio_wb16(pb, 2);
/* length in words - 1 */
505 // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
508 }
509
510 if (missing_packets) {
513 avio_wb16(pb, 3);
/* length in words - 1 */
516
519 }
520
522 if (!fd)
523 return 0;
525 if (
len > 0 && buf) {
528 }
529 return 0;
530 }
531
533 {
534 uint8_t *bs;
536
537 /* This function writes an extradata with a channel mapping family of 0.
538 * This mapping family only supports mono and stereo layouts. And RFC7587
539 * specifies that the number of channels in the SDP must be 2.
540 */
543 }
544
548
550
551 /* Opus magic */
553 /* Version */
554 bytestream_put_byte (&bs, 0x1);
555 /* Channel count */
556 bytestream_put_byte (&bs, codecpar->
channels);
557 /* Pre skip */
558 bytestream_put_le16 (&bs, 0);
559 /* Input sample rate */
560 bytestream_put_le32 (&bs, 48000);
561 /* Output gain */
562 bytestream_put_le16 (&bs, 0x0);
563 /* Mapping family */
564 bytestream_put_byte (&bs, 0x0);
565
566 return 0;
567 }
568
569 /**
570 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
571 * MPEG-2 TS streams.
572 */
574 int payload_type, int queue_size)
575 {
578
582 s->payload_type = payload_type;
587 s->queue_size = queue_size;
588
591
593 if (st) {
596 /* According to RFC 3551, the stream clock rate is 8000
597 * even if the sample rate is 16000. */
600 break;
605 "Error creating opus extradata: %s\n",
609 }
610 break;
611 default:
612 break;
613 }
614 }
615 // needed to send back RTCP RR in RTSP sessions
616 gethostname(
s->hostname,
sizeof(
s->hostname));
618 }
619
622 {
623 s->dynamic_protocol_context =
ctx;
625 }
626
628 const char *params)
629 {
632 }
633
635 int64_t rtcp_time, delta_timestamp, delta_time;
636
640 if (!prft)
642
644 delta_timestamp = (int64_t)timestamp - (int64_t)
s->last_rtcp_timestamp;
646
647 prft->
wallclock = rtcp_time + delta_time;
649 return 0;
650 }
651
652 /**
653 * This was the second switch in rtp_parse packet.
654 * Normalizes time, if required, sets stream_index, etc.
655 */
657 {
659 return; /* Timestamp already set by depacketizer */
661 return;
662
666 }
667 }
668
670 int64_t addend;
671 int delta_timestamp;
672
673 /* compute pts from timestamp with received ntp_time */
674 delta_timestamp = timestamp -
s->last_rtcp_timestamp;
675 /* convert to the PTS timebase */
676 addend =
av_rescale(
s->last_rtcp_ntp_time -
s->first_rtcp_ntp_time,
677 s->st->time_base.den,
678 (uint64_t)
s->st->time_base.num << 32);
679 pkt->
pts =
s->range_start_offset +
s->rtcp_ts_offset + addend +
680 delta_timestamp;
681 return;
682 }
683
684 if (!
s->base_timestamp)
685 s->base_timestamp = timestamp;
686 /* assume that the difference is INT32_MIN < x < INT32_MAX,
687 * but allow the first timestamp to exceed INT32_MAX */
689 s->unwrapped_timestamp += timestamp;
690 else
691 s->unwrapped_timestamp += (
int32_t)(timestamp -
s->timestamp);
692 s->timestamp = timestamp;
693 pkt->
pts =
s->unwrapped_timestamp +
s->range_start_offset -
695 }
696
698 const uint8_t *buf,
int len)
699 {
700 unsigned int ssrc;
701 int payload_type, seq,
flags = 0;
702 int ext, csrc;
704 uint32_t timestamp;
705 int rv = 0;
706
707 csrc = buf[0] & 0x0f;
708 ext = buf[0] & 0x10;
709 payload_type = buf[1] & 0x7f;
710 if (buf[1] & 0x80)
715 /* store the ssrc in the RTPDemuxContext */
717
718 /* NOTE: we can handle only one payload type */
719 if (
s->payload_type != payload_type)
720 return -1;
721
723 // only do something with this if all the rtp checks pass...
726 "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
727 payload_type, seq, ((
s->seq + 1) & 0xffff));
728 return -1;
729 }
730
731 if (buf[0] & 0x20) {
732 int padding = buf[
len - 1];
733 if (
len >= 12 + padding)
735 }
736
739 buf += 12;
740
742 buf += 4 * csrc;
745
746 /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
747 if (ext) {
749 return -1;
750 /* calculate the header extension length (stored as number
751 * of 32-bit words) */
752 ext = (
AV_RB16(buf + 2) + 1) << 2;
753
755 return -1;
756 // skip past RTP header extension
758 buf += ext;
759 }
760
761 if (
s->handler &&
s->handler->parse_packet) {
762 rv =
s->handler->parse_packet(
s->ic,
s->dynamic_protocol_context,
763 s->st,
pkt, ×tamp, buf,
len, seq,
765 } else if (st) {
767 return rv;
770 } else {
772 }
773
774 // now perform timestamp things....
776
777 return rv;
778 }
779
781 {
787 }
791 }
792
794 {
795 uint16_t seq =
AV_RB16(buf + 2);
797
798 /* Find the correct place in the queue to insert the packet */
799 while (*cur) {
800 int16_t
diff = seq - (*cur)->seq;
802 break;
804 }
805
807 if (!packet)
810 packet->seq = seq;
812 packet->buf = buf;
813 packet->next = *cur;
814 *cur = packet;
816
817 return 0;
818 }
819
821 {
822 return s->queue &&
s->queue->seq == (uint16_t) (
s->seq + 1);
823 }
824
826 {
827 return s->queue ?
s->queue->recvtime : 0;
828 }
829
831 {
832 int rv;
834
835 if (
s->queue_len <= 0)
836 return -1;
837
840 "RTP: missed %d packets\n",
s->queue->seq -
s->seq - 1);
841
842 /* Parse the first packet in the queue, and dequeue it */
844 next =
s->queue->next;
849 return rv;
850 }
851
853 uint8_t **bufptr,
int len)
854 {
855 uint8_t *buf = bufptr ? *bufptr :
NULL;
857 uint32_t timestamp;
858 int rv = 0;
859
860 if (!buf) {
861 /* If parsing of the previous packet actually returned 0 or an error,
862 * there's nothing more to be parsed from that packet, but we may have
863 * indicated that we can return the next enqueued packet. */
864 if (
s->prev_ret <= 0)
866 /* return the next packets, if any */
867 if (
s->handler &&
s->handler->parse_packet) {
868 /* timestamp should be overwritten by parse_packet, if not,
869 * the packet is left with pts == AV_NOPTS_VALUE */
871 rv =
s->handler->parse_packet(
s->ic,
s->dynamic_protocol_context,
875 return rv;
876 }
877 }
878
880 return -1;
881
883 return -1;
886 }
887
893 // Calculate the jitter immediately, before queueing the packet
894 // into the reordering queue.
896 }
897
898 if ((
s->seq == 0 && !
s->queue) ||
s->queue_size <= 1) {
899 /* First packet, or no reordering */
901 } else {
902 uint16_t seq =
AV_RB16(buf + 2);
903 int16_t
diff = seq -
s->seq;
905 /* Packet older than the previously emitted one, drop */
907 "RTP: dropping old packet received too late\n");
908 return -1;
909 }
else if (
diff <= 1) {
910 /* Correct packet */
912 return rv;
913 } else {
914 /* Still missing some packet, enqueue this one. */
916 if (rv < 0)
917 return rv;
919 /* Return the first enqueued packet if the queue is full,
920 * even if we're missing something */
921 if (
s->queue_len >=
s->queue_size) {
924 }
925 return -1;
926 }
927 }
928 }
929
930 /**
931 * Parse an RTP or RTCP packet directly sent as a buffer.
932 * @param s RTP parse context.
933 * @param pkt returned packet
934 * @param bufptr pointer to the input buffer or NULL to read the next packets
935 * @param len buffer len
936 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
937 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
938 */
940 uint8_t **bufptr,
int len)
941 {
942 int rv;
944 return -1;
950 }
951
953 {
957 }
958
964 const char *attr,
const char *
value))
965 {
966 char attr[256];
968 int res;
969 int value_size = strlen(p) + 1;
970
974 }
975
976 // remove protocol identifier
977 while (*p && *p == ' ')
978 p++; // strip spaces
979 while (*p && *p != ' ')
980 p++; // eat protocol identifier
981 while (*p && *p == ' ')
982 p++; // strip trailing spaces
983
985 attr, sizeof(attr),
986 value, value_size)) {
990 return res;
991 }
992 }
994 return 0;
995 }
996
998 {
1001
1008 }
1010 }