FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rtpdec.c
Go to the documentation of this file.
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 
22 #include "libavutil/mathematics.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/time.h"
26 
27 #include "avformat.h"
28 #include "network.h"
29 #include "srtp.h"
30 #include "url.h"
31 #include "rtpdec.h"
32 #include "rtpdec_formats.h"
33 
34 #define MIN_FEEDBACK_INTERVAL 200000 /* 200 ms in us */
35 
37  .enc_name = "GSM",
38  .codec_type = AVMEDIA_TYPE_AUDIO,
39  .codec_id = AV_CODEC_ID_GSM,
40 };
41 
43  .enc_name = "X-MP3-draft-00",
44  .codec_type = AVMEDIA_TYPE_AUDIO,
45  .codec_id = AV_CODEC_ID_MP3ADU,
46 };
47 
49  .enc_name = "speex",
50  .codec_type = AVMEDIA_TYPE_AUDIO,
51  .codec_id = AV_CODEC_ID_SPEEX,
52 };
53 
55  .enc_name = "opus",
56  .codec_type = AVMEDIA_TYPE_AUDIO,
57  .codec_id = AV_CODEC_ID_OPUS,
58 };
59 
61  .enc_name = "t140",
62  .codec_type = AVMEDIA_TYPE_SUBTITLE,
63  .codec_id = AV_CODEC_ID_TEXT,
64 };
65 
67 
69 {
71  rtp_first_dynamic_payload_handler = handler;
72 }
73 
75 {
113  ff_register_dynamic_payload_handler(&gsm_dynamic_handler);
114  ff_register_dynamic_payload_handler(&opus_dynamic_handler);
115  ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
116  ff_register_dynamic_payload_handler(&speex_dynamic_handler);
117  ff_register_dynamic_payload_handler(&t140_dynamic_handler);
118 }
119 
121  enum AVMediaType codec_type)
122 {
124  for (handler = rtp_first_dynamic_payload_handler;
125  handler; handler = handler->next)
126  if (handler->enc_name &&
127  !av_strcasecmp(name, handler->enc_name) &&
128  codec_type == handler->codec_type)
129  return handler;
130  return NULL;
131 }
132 
134  enum AVMediaType codec_type)
135 {
137  for (handler = rtp_first_dynamic_payload_handler;
138  handler; handler = handler->next)
139  if (handler->static_payload_id && handler->static_payload_id == id &&
140  codec_type == handler->codec_type)
141  return handler;
142  return NULL;
143 }
144 
145 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf,
146  int len)
147 {
148  int payload_len;
149  while (len >= 4) {
150  payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
151 
152  switch (buf[1]) {
153  case RTCP_SR:
154  if (payload_len < 20) {
155  av_log(s->ic, AV_LOG_ERROR, "Invalid RTCP SR packet length\n");
156  return AVERROR_INVALIDDATA;
157  }
158 
160  s->last_rtcp_ntp_time = AV_RB64(buf + 8);
161  s->last_rtcp_timestamp = AV_RB32(buf + 16);
164  if (!s->base_timestamp)
167  }
168 
169  break;
170  case RTCP_BYE:
171  return -RTCP_BYE;
172  }
173 
174  buf += payload_len;
175  len -= payload_len;
176  }
177  return -1;
178 }
179 
180 #define RTP_SEQ_MOD (1 << 16)
181 
182 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
183 {
184  memset(s, 0, sizeof(RTPStatistics));
185  s->max_seq = base_sequence;
186  s->probation = 1;
187 }
188 
189 /*
190  * Called whenever there is a large jump in sequence numbers,
191  * or when they get out of probation...
192  */
193 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
194 {
195  s->max_seq = seq;
196  s->cycles = 0;
197  s->base_seq = seq - 1;
198  s->bad_seq = RTP_SEQ_MOD + 1;
199  s->received = 0;
200  s->expected_prior = 0;
201  s->received_prior = 0;
202  s->jitter = 0;
203  s->transit = 0;
204 }
205 
206 /* Returns 1 if we should handle this packet. */
207 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
208 {
209  uint16_t udelta = seq - s->max_seq;
210  const int MAX_DROPOUT = 3000;
211  const int MAX_MISORDER = 100;
212  const int MIN_SEQUENTIAL = 2;
213 
214  /* source not valid until MIN_SEQUENTIAL packets with sequence
215  * seq. numbers have been received */
216  if (s->probation) {
217  if (seq == s->max_seq + 1) {
218  s->probation--;
219  s->max_seq = seq;
220  if (s->probation == 0) {
221  rtp_init_sequence(s, seq);
222  s->received++;
223  return 1;
224  }
225  } else {
226  s->probation = MIN_SEQUENTIAL - 1;
227  s->max_seq = seq;
228  }
229  } else if (udelta < MAX_DROPOUT) {
230  // in order, with permissible gap
231  if (seq < s->max_seq) {
232  // sequence number wrapped; count another 64k cycles
233  s->cycles += RTP_SEQ_MOD;
234  }
235  s->max_seq = seq;
236  } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
237  // sequence made a large jump...
238  if (seq == s->bad_seq) {
239  /* two sequential packets -- assume that the other side
240  * restarted without telling us; just resync. */
241  rtp_init_sequence(s, seq);
242  } else {
243  s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1);
244  return 0;
245  }
246  } else {
247  // duplicate or reordered packet...
248  }
249  s->received++;
250  return 1;
251 }
252 
253 static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp,
254  uint32_t arrival_timestamp)
255 {
256  // Most of this is pretty straight from RFC 3550 appendix A.8
257  uint32_t transit = arrival_timestamp - sent_timestamp;
258  uint32_t prev_transit = s->transit;
259  int32_t d = transit - prev_transit;
260  // Doing the FFABS() call directly on the "transit - prev_transit"
261  // expression doesn't work, since it's an unsigned expression. Doing the
262  // transit calculation in unsigned is desired though, since it most
263  // probably will need to wrap around.
264  d = FFABS(d);
265  s->transit = transit;
266  if (!prev_transit)
267  return;
268  s->jitter += d - (int32_t) ((s->jitter + 8) >> 4);
269 }
270 
272  AVIOContext *avio, int count)
273 {
274  AVIOContext *pb;
275  uint8_t *buf;
276  int len;
277  int rtcp_bytes;
279  uint32_t lost;
280  uint32_t extended_max;
281  uint32_t expected_interval;
282  uint32_t received_interval;
283  int32_t lost_interval;
284  uint32_t expected;
285  uint32_t fraction;
286 
287  if ((!fd && !avio) || (count < 1))
288  return -1;
289 
290  /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
291  /* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */
292  s->octet_count += count;
293  rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
295  rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
296  if (rtcp_bytes < 28)
297  return -1;
299 
300  if (!fd)
301  pb = avio;
302  else if (avio_open_dyn_buf(&pb) < 0)
303  return -1;
304 
305  // Receiver Report
306  avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
307  avio_w8(pb, RTCP_RR);
308  avio_wb16(pb, 7); /* length in words - 1 */
309  // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
310  avio_wb32(pb, s->ssrc + 1);
311  avio_wb32(pb, s->ssrc); // server SSRC
312  // some placeholders we should really fill...
313  // RFC 1889/p64
314  extended_max = stats->cycles + stats->max_seq;
315  expected = extended_max - stats->base_seq;
316  lost = expected - stats->received;
317  lost = FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
318  expected_interval = expected - stats->expected_prior;
319  stats->expected_prior = expected;
320  received_interval = stats->received - stats->received_prior;
321  stats->received_prior = stats->received;
322  lost_interval = expected_interval - received_interval;
323  if (expected_interval == 0 || lost_interval <= 0)
324  fraction = 0;
325  else
326  fraction = (lost_interval << 8) / expected_interval;
327 
328  fraction = (fraction << 24) | lost;
329 
330  avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
331  avio_wb32(pb, extended_max); /* max sequence received */
332  avio_wb32(pb, stats->jitter >> 4); /* jitter */
333 
335  avio_wb32(pb, 0); /* last SR timestamp */
336  avio_wb32(pb, 0); /* delay since last SR */
337  } else {
338  uint32_t middle_32_bits = s->last_rtcp_ntp_time >> 16; // this is valid, right? do we need to handle 64 bit values special?
339  uint32_t delay_since_last = av_rescale(av_gettime_relative() - s->last_rtcp_reception_time,
340  65536, AV_TIME_BASE);
341 
342  avio_wb32(pb, middle_32_bits); /* last SR timestamp */
343  avio_wb32(pb, delay_since_last); /* delay since last SR */
344  }
345 
346  // CNAME
347  avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
348  avio_w8(pb, RTCP_SDES);
349  len = strlen(s->hostname);
350  avio_wb16(pb, (7 + len + 3) / 4); /* length in words - 1 */
351  avio_wb32(pb, s->ssrc + 1);
352  avio_w8(pb, 0x01);
353  avio_w8(pb, len);
354  avio_write(pb, s->hostname, len);
355  avio_w8(pb, 0); /* END */
356  // padding
357  for (len = (7 + len) % 4; len % 4; len++)
358  avio_w8(pb, 0);
359 
360  avio_flush(pb);
361  if (!fd)
362  return 0;
363  len = avio_close_dyn_buf(pb, &buf);
364  if ((len > 0) && buf) {
365  int av_unused result;
366  av_log(s->ic, AV_LOG_TRACE, "sending %d bytes of RR\n", len);
367  result = ffurl_write(fd, buf, len);
368  av_log(s->ic, AV_LOG_TRACE, "result from ffurl_write: %d\n", result);
369  av_free(buf);
370  }
371  return 0;
372 }
373 
375 {
376  AVIOContext *pb;
377  uint8_t *buf;
378  int len;
379 
380  /* Send a small RTP packet */
381  if (avio_open_dyn_buf(&pb) < 0)
382  return;
383 
384  avio_w8(pb, (RTP_VERSION << 6));
385  avio_w8(pb, 0); /* Payload type */
386  avio_wb16(pb, 0); /* Seq */
387  avio_wb32(pb, 0); /* Timestamp */
388  avio_wb32(pb, 0); /* SSRC */
389 
390  avio_flush(pb);
391  len = avio_close_dyn_buf(pb, &buf);
392  if ((len > 0) && buf)
393  ffurl_write(rtp_handle, buf, len);
394  av_free(buf);
395 
396  /* Send a minimal RTCP RR */
397  if (avio_open_dyn_buf(&pb) < 0)
398  return;
399 
400  avio_w8(pb, (RTP_VERSION << 6));
401  avio_w8(pb, RTCP_RR); /* receiver report */
402  avio_wb16(pb, 1); /* length in words - 1 */
403  avio_wb32(pb, 0); /* our own SSRC */
404 
405  avio_flush(pb);
406  len = avio_close_dyn_buf(pb, &buf);
407  if ((len > 0) && buf)
408  ffurl_write(rtp_handle, buf, len);
409  av_free(buf);
410 }
411 
412 static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing,
413  uint16_t *missing_mask)
414 {
415  int i;
416  uint16_t next_seq = s->seq + 1;
417  RTPPacket *pkt = s->queue;
418 
419  if (!pkt || pkt->seq == next_seq)
420  return 0;
421 
422  *missing_mask = 0;
423  for (i = 1; i <= 16; i++) {
424  uint16_t missing_seq = next_seq + i;
425  while (pkt) {
426  int16_t diff = pkt->seq - missing_seq;
427  if (diff >= 0)
428  break;
429  pkt = pkt->next;
430  }
431  if (!pkt)
432  break;
433  if (pkt->seq == missing_seq)
434  continue;
435  *missing_mask |= 1 << (i - 1);
436  }
437 
438  *first_missing = next_seq;
439  return 1;
440 }
441 
443  AVIOContext *avio)
444 {
445  int len, need_keyframe, missing_packets;
446  AVIOContext *pb;
447  uint8_t *buf;
448  int64_t now;
449  uint16_t first_missing = 0, missing_mask = 0;
450 
451  if (!fd && !avio)
452  return -1;
453 
454  need_keyframe = s->handler && s->handler->need_keyframe &&
456  missing_packets = find_missing_packets(s, &first_missing, &missing_mask);
457 
458  if (!need_keyframe && !missing_packets)
459  return 0;
460 
461  /* Send new feedback if enough time has elapsed since the last
462  * feedback packet. */
463 
464  now = av_gettime_relative();
465  if (s->last_feedback_time &&
467  return 0;
468  s->last_feedback_time = now;
469 
470  if (!fd)
471  pb = avio;
472  else if (avio_open_dyn_buf(&pb) < 0)
473  return -1;
474 
475  if (need_keyframe) {
476  avio_w8(pb, (RTP_VERSION << 6) | 1); /* PLI */
477  avio_w8(pb, RTCP_PSFB);
478  avio_wb16(pb, 2); /* length in words - 1 */
479  // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
480  avio_wb32(pb, s->ssrc + 1);
481  avio_wb32(pb, s->ssrc); // server SSRC
482  }
483 
484  if (missing_packets) {
485  avio_w8(pb, (RTP_VERSION << 6) | 1); /* NACK */
486  avio_w8(pb, RTCP_RTPFB);
487  avio_wb16(pb, 3); /* length in words - 1 */
488  avio_wb32(pb, s->ssrc + 1);
489  avio_wb32(pb, s->ssrc); // server SSRC
490 
491  avio_wb16(pb, first_missing);
492  avio_wb16(pb, missing_mask);
493  }
494 
495  avio_flush(pb);
496  if (!fd)
497  return 0;
498  len = avio_close_dyn_buf(pb, &buf);
499  if (len > 0 && buf) {
500  ffurl_write(fd, buf, len);
501  av_free(buf);
502  }
503  return 0;
504 }
505 
506 /**
507  * open a new RTP parse context for stream 'st'. 'st' can be NULL for
508  * MPEG-2 TS streams.
509  */
511  int payload_type, int queue_size)
512 {
514 
515  s = av_mallocz(sizeof(RTPDemuxContext));
516  if (!s)
517  return NULL;
518  s->payload_type = payload_type;
521  s->ic = s1;
522  s->st = st;
523  s->queue_size = queue_size;
524 
525  av_log(s->ic, AV_LOG_VERBOSE, "setting jitter buffer size to %d\n",
526  s->queue_size);
527 
529  if (st) {
530  switch (st->codecpar->codec_id) {
532  /* According to RFC 3551, the stream clock rate is 8000
533  * even if the sample rate is 16000. */
534  if (st->codecpar->sample_rate == 8000)
535  st->codecpar->sample_rate = 16000;
536  break;
537  default:
538  break;
539  }
540  }
541  // needed to send back RTCP RR in RTSP sessions
542  gethostname(s->hostname, sizeof(s->hostname));
543  return s;
544 }
545 
548 {
550  s->handler = handler;
551 }
552 
553 void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite,
554  const char *params)
555 {
556  if (!ff_srtp_set_crypto(&s->srtp, suite, params))
557  s->srtp_enabled = 1;
558 }
559 
560 /**
561  * This was the second switch in rtp_parse packet.
562  * Normalizes time, if required, sets stream_index, etc.
563  */
564 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
565 {
566  if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
567  return; /* Timestamp already set by depacketizer */
568  if (timestamp == RTP_NOTS_VALUE)
569  return;
570 
571  if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
572  int64_t addend;
573  int delta_timestamp;
574 
575  /* compute pts from timestamp with received ntp_time */
576  delta_timestamp = timestamp - s->last_rtcp_timestamp;
577  /* convert to the PTS timebase */
579  s->st->time_base.den,
580  (uint64_t) s->st->time_base.num << 32);
581  pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
582  delta_timestamp;
583  return;
584  }
585 
586  if (!s->base_timestamp)
587  s->base_timestamp = timestamp;
588  /* assume that the difference is INT32_MIN < x < INT32_MAX,
589  * but allow the first timestamp to exceed INT32_MAX */
590  if (!s->timestamp)
591  s->unwrapped_timestamp += timestamp;
592  else
593  s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
594  s->timestamp = timestamp;
596  s->base_timestamp;
597 }
598 
600  const uint8_t *buf, int len)
601 {
602  unsigned int ssrc;
603  int payload_type, seq, flags = 0;
604  int ext, csrc;
605  AVStream *st;
606  uint32_t timestamp;
607  int rv = 0;
608 
609  csrc = buf[0] & 0x0f;
610  ext = buf[0] & 0x10;
611  payload_type = buf[1] & 0x7f;
612  if (buf[1] & 0x80)
613  flags |= RTP_FLAG_MARKER;
614  seq = AV_RB16(buf + 2);
615  timestamp = AV_RB32(buf + 4);
616  ssrc = AV_RB32(buf + 8);
617  /* store the ssrc in the RTPDemuxContext */
618  s->ssrc = ssrc;
619 
620  /* NOTE: we can handle only one payload type */
621  if (s->payload_type != payload_type)
622  return -1;
623 
624  st = s->st;
625  // only do something with this if all the rtp checks pass...
626  if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) {
627  av_log(s->ic, AV_LOG_ERROR,
628  "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
629  payload_type, seq, ((s->seq + 1) & 0xffff));
630  return -1;
631  }
632 
633  if (buf[0] & 0x20) {
634  int padding = buf[len - 1];
635  if (len >= 12 + padding)
636  len -= padding;
637  }
638 
639  s->seq = seq;
640  len -= 12;
641  buf += 12;
642 
643  len -= 4 * csrc;
644  buf += 4 * csrc;
645  if (len < 0)
646  return AVERROR_INVALIDDATA;
647 
648  /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
649  if (ext) {
650  if (len < 4)
651  return -1;
652  /* calculate the header extension length (stored as number
653  * of 32-bit words) */
654  ext = (AV_RB16(buf + 2) + 1) << 2;
655 
656  if (len < ext)
657  return -1;
658  // skip past RTP header extension
659  len -= ext;
660  buf += ext;
661  }
662 
663  if (s->handler && s->handler->parse_packet) {
665  s->st, pkt, &timestamp, buf, len, seq,
666  flags);
667  } else if (st) {
668  if ((rv = av_new_packet(pkt, len)) < 0)
669  return rv;
670  memcpy(pkt->data, buf, len);
671  pkt->stream_index = st->index;
672  } else {
673  return AVERROR(EINVAL);
674  }
675 
676  // now perform timestamp things....
677  finalize_packet(s, pkt, timestamp);
678 
679  return rv;
680 }
681 
683 {
684  while (s->queue) {
685  RTPPacket *next = s->queue->next;
686  av_freep(&s->queue->buf);
687  av_freep(&s->queue);
688  s->queue = next;
689  }
690  s->seq = 0;
691  s->queue_len = 0;
692  s->prev_ret = 0;
693 }
694 
696 {
697  uint16_t seq = AV_RB16(buf + 2);
698  RTPPacket **cur = &s->queue, *packet;
699 
700  /* Find the correct place in the queue to insert the packet */
701  while (*cur) {
702  int16_t diff = seq - (*cur)->seq;
703  if (diff < 0)
704  break;
705  cur = &(*cur)->next;
706  }
707 
708  packet = av_mallocz(sizeof(*packet));
709  if (!packet)
710  return AVERROR(ENOMEM);
711  packet->recvtime = av_gettime_relative();
712  packet->seq = seq;
713  packet->len = len;
714  packet->buf = buf;
715  packet->next = *cur;
716  *cur = packet;
717  s->queue_len++;
718 
719  return 0;
720 }
721 
723 {
724  return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
725 }
726 
728 {
729  return s->queue ? s->queue->recvtime : 0;
730 }
731 
733 {
734  int rv;
735  RTPPacket *next;
736 
737  if (s->queue_len <= 0)
738  return -1;
739 
740  if (!has_next_packet(s))
742  "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
743 
744  /* Parse the first packet in the queue, and dequeue it */
745  rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
746  next = s->queue->next;
747  av_freep(&s->queue->buf);
748  av_freep(&s->queue);
749  s->queue = next;
750  s->queue_len--;
751  return rv;
752 }
753 
755  uint8_t **bufptr, int len)
756 {
757  uint8_t *buf = bufptr ? *bufptr : NULL;
758  int flags = 0;
759  uint32_t timestamp;
760  int rv = 0;
761 
762  if (!buf) {
763  /* If parsing of the previous packet actually returned 0 or an error,
764  * there's nothing more to be parsed from that packet, but we may have
765  * indicated that we can return the next enqueued packet. */
766  if (s->prev_ret <= 0)
767  return rtp_parse_queued_packet(s, pkt);
768  /* return the next packets, if any */
769  if (s->handler && s->handler->parse_packet) {
770  /* timestamp should be overwritten by parse_packet, if not,
771  * the packet is left with pts == AV_NOPTS_VALUE */
772  timestamp = RTP_NOTS_VALUE;
774  s->st, pkt, &timestamp, NULL, 0, 0,
775  flags);
776  finalize_packet(s, pkt, timestamp);
777  return rv;
778  }
779  }
780 
781  if (len < 12)
782  return -1;
783 
784  if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
785  return -1;
786  if (RTP_PT_IS_RTCP(buf[1])) {
787  return rtcp_parse_packet(s, buf, len);
788  }
789 
790  if (s->st) {
791  int64_t received = av_gettime_relative();
792  uint32_t arrival_ts = av_rescale_q(received, AV_TIME_BASE_Q,
793  s->st->time_base);
794  timestamp = AV_RB32(buf + 4);
795  // Calculate the jitter immediately, before queueing the packet
796  // into the reordering queue.
797  rtcp_update_jitter(&s->statistics, timestamp, arrival_ts);
798  }
799 
800  if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
801  /* First packet, or no reordering */
802  return rtp_parse_packet_internal(s, pkt, buf, len);
803  } else {
804  uint16_t seq = AV_RB16(buf + 2);
805  int16_t diff = seq - s->seq;
806  if (diff < 0) {
807  /* Packet older than the previously emitted one, drop */
809  "RTP: dropping old packet received too late\n");
810  return -1;
811  } else if (diff <= 1) {
812  /* Correct packet */
813  rv = rtp_parse_packet_internal(s, pkt, buf, len);
814  return rv;
815  } else {
816  /* Still missing some packet, enqueue this one. */
817  rv = enqueue_packet(s, buf, len);
818  if (rv < 0)
819  return rv;
820  *bufptr = NULL;
821  /* Return the first enqueued packet if the queue is full,
822  * even if we're missing something */
823  if (s->queue_len >= s->queue_size) {
824  av_log(s->ic, AV_LOG_WARNING, "jitter buffer full\n");
825  return rtp_parse_queued_packet(s, pkt);
826  }
827  return -1;
828  }
829  }
830 }
831 
832 /**
833  * Parse an RTP or RTCP packet directly sent as a buffer.
834  * @param s RTP parse context.
835  * @param pkt returned packet
836  * @param bufptr pointer to the input buffer or NULL to read the next packets
837  * @param len buffer len
838  * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
839  * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
840  */
842  uint8_t **bufptr, int len)
843 {
844  int rv;
845  if (s->srtp_enabled && bufptr && ff_srtp_decrypt(&s->srtp, *bufptr, &len) < 0)
846  return -1;
847  rv = rtp_parse_one_packet(s, pkt, bufptr, len);
848  s->prev_ret = rv;
849  while (rv < 0 && has_next_packet(s))
850  rv = rtp_parse_queued_packet(s, pkt);
851  return rv ? rv : has_next_packet(s);
852 }
853 
855 {
857  ff_srtp_free(&s->srtp);
858  av_free(s);
859 }
860 
862  AVStream *stream, PayloadContext *data, const char *p,
863  int (*parse_fmtp)(AVFormatContext *s,
864  AVStream *stream,
865  PayloadContext *data,
866  const char *attr, const char *value))
867 {
868  char attr[256];
869  char *value;
870  int res;
871  int value_size = strlen(p) + 1;
872 
873  if (!(value = av_malloc(value_size))) {
874  av_log(s, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n");
875  return AVERROR(ENOMEM);
876  }
877 
878  // remove protocol identifier
879  while (*p && *p == ' ')
880  p++; // strip spaces
881  while (*p && *p != ' ')
882  p++; // eat protocol identifier
883  while (*p && *p == ' ')
884  p++; // strip trailing spaces
885 
886  while (ff_rtsp_next_attr_and_value(&p,
887  attr, sizeof(attr),
888  value, value_size)) {
889  res = parse_fmtp(s, stream, data, attr, value);
890  if (res < 0 && res != AVERROR_PATCHWELCOME) {
891  av_free(value);
892  return res;
893  }
894  }
895  av_free(value);
896  return 0;
897 }
898 
899 int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
900 {
901  int ret;
902  av_init_packet(pkt);
903 
904  pkt->size = avio_close_dyn_buf(*dyn_buf, &pkt->data);
905  pkt->stream_index = stream_idx;
906  *dyn_buf = NULL;
907  if ((ret = av_packet_from_data(pkt, pkt->data, pkt->size)) < 0) {
908  av_freep(&pkt->data);
909  return ret;
910  }
911  return pkt->size;
912 }
int queue_size
The size of queue, or 0 if reordering is disabled.
Definition: rtpdec.h:174
#define NULL
Definition: coverity.c:32
void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite, const char *params)
Definition: rtpdec.c:553
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVFormatContext * ic
Definition: rtpdec.h:150
uint16_t seq
Definition: rtpdec.h:154
RTPDynamicProtocolHandler ff_quicktime_rtp_aud_handler
void ff_rtp_send_punch_packets(URLContext *rtp_handle)
Send a dummy packet on both port pairs to set up the connection state in potential NAT routers...
Definition: rtpdec.c:374
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1272
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp, uint32_t arrival_timestamp)
Definition: rtpdec.c:253
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:421
int payload_type
Definition: rtpdec.h:152
int64_t range_start_offset
Definition: rtpdec.h:159
int prev_ret
Fields for packet reordering.
Definition: rtpdec.h:171
RTP/JPEG specific private data.
Definition: rdt.c:83
int64_t last_feedback_time
Definition: rtpdec.h:188
unsigned int last_octet_count
Definition: rtpdec.h:187
static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt, const uint8_t *buf, int len)
Definition: rtpdec.c:599
RTPPacket * queue
A sorted queue of buffered packets not yet returned.
Definition: rtpdec.h:172
#define RTP_VERSION
Definition: rtp.h:78
static int parse_fmtp(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *attr, const char *value)
Definition: rtpdec_latm.c:131
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3939
RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler
Definition: rtpdec_latm.c:165
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:877
int size
Definition: avcodec.h:1589
RTPDynamicProtocolHandler ff_ms_rtp_asf_pfa_handler
#define RTCP_TX_RATIO_NUM
Definition: rtp.h:82
const RTPDynamicProtocolHandler * handler
Definition: rtpdec.h:191
enum AVMediaType codec_type
Definition: rtp.c:37
static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
Definition: rtpdec.c:145
RTPDynamicProtocolHandler ff_jpeg_dynamic_handler
Definition: rtpdec_jpeg.c:382
RTPDynamicProtocolHandler ff_h263_1998_dynamic_handler
Definition: rtpdec_h263.c:92
static AVPacket pkt
RTPDynamicProtocolHandler ff_amr_wb_dynamic_handler
Definition: rtpdec_amr.c:195
uint64_t last_rtcp_ntp_time
Definition: rtpdec.h:178
uint32_t cycles
shifted count of sequence number cycles
Definition: rtpdec.h:81
#define RTCP_TX_RATIO_DEN
Definition: rtp.h:83
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
static RTPDynamicProtocolHandler speex_dynamic_handler
Definition: rtpdec.c:48
RTPDynamicProtocolHandler ff_h263_2000_dynamic_handler
Definition: rtpdec_h263.c:100
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1260
static int find_missing_packets(RTPDemuxContext *s, uint16_t *first_missing, uint16_t *missing_mask)
Definition: rtpdec.c:412
enum AVMediaType codec_type
Definition: rtpdec.h:117
RTPDemuxContext * ff_rtp_parse_open(AVFormatContext *s1, AVStream *st, int payload_type, int queue_size)
open a new RTP parse context for stream 'st'.
Definition: rtpdec.c:510
PayloadContext * dynamic_protocol_context
Definition: rtpdec.h:192
Format I/O context.
Definition: avformat.h:1325
uint64_t first_rtcp_ntp_time
Definition: rtpdec.h:180
static RTPDynamicProtocolHandler gsm_dynamic_handler
Definition: rtpdec.c:36
uint32_t base_seq
base sequence number
Definition: rtpdec.h:82
void ff_srtp_free(struct SRTPContext *s)
Definition: srtp.c:31
uint8_t
#define av_malloc(s)
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
Definition: rtpdec.c:682
RTPDynamicProtocolHandler ff_ilbc_dynamic_handler
Definition: rtpdec_ilbc.c:69
int len
Definition: rtpdec.h:144
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
Initialize a reference-counted packet from av_malloc()ed data.
Definition: avpacket.c:151
static void handler(vbi_event *ev, void *user_data)
RTPDynamicProtocolHandler ff_dv_dynamic_handler
Definition: rtpdec_dv.c:134
int ff_rtsp_next_attr_and_value(const char **p, char *attr, int attr_size, char *value, int value_size)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
RTPDynamicProtocolHandler ff_theora_dynamic_handler
Definition: rtpdec_xiph.c:370
uint8_t * data
Definition: avcodec.h:1588
void ff_register_rtp_dynamic_payload_handlers(void)
Definition: rtpdec.c:74
Definition: rtp.h:99
static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
Definition: rtpdec.c:732
RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
char hostname[256]
Definition: rtpdec.h:162
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:204
uint32_t expected_prior
packets expected in last interval
Definition: rtpdec.h:86
#define av_log(a,...)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
int srtp_enabled
Definition: rtpdec.h:164
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint16_t seq
Definition: rtpdec.h:142
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:93
#define AVERROR(e)
Definition: error.h:43
Definition: rtp.h:103
int probation
sequence packets till source is valid
Definition: rtpdec.h:84
static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
Definition: rtpdec.c:193
GLenum GLint * params
Definition: opengl_enc.c:114
#define RTP_SEQ_MOD
Definition: rtpdec.c:180
RTPDynamicProtocolHandler ff_vorbis_dynamic_handler
Definition: rtpdec_xiph.c:380
RTPDynamicProtocolHandler ff_ms_rtp_asf_pfv_handler
RTPDynamicProtocolHandler ff_ac3_dynamic_handler
Definition: rtpdec_ac3.c:125
RTPDynamicProtocolHandler ff_svq3_dynamic_handler
Definition: rtpdec_svq3.c:113
GLsizei count
Definition: opengl_enc.c:109
DynamicPayloadPacketHandlerProc parse_packet
Parse handler for this dynamic packet.
Definition: rtpdec.h:135
int64_t rtcp_ts_offset
Definition: rtpdec.h:182
RTPDynamicProtocolHandler * ff_rtp_handler_find_by_id(int id, enum AVMediaType codec_type)
Definition: rtpdec.c:133
uint32_t timestamp
Definition: rtpdec.h:155
uint32_t transit
relative transit time for previous packet
Definition: rtpdec.h:88
uint32_t jitter
estimated jitter.
Definition: rtpdec.h:89
int queue_len
The number of packets in queue.
Definition: rtpdec.h:173
RTPDynamicProtocolHandler ff_qt_rtp_vid_handler
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1381
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:224
int ff_srtp_decrypt(struct SRTPContext *s, uint8_t *buf, int *lenptr)
Definition: srtp.c:126
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:246
#define FFMIN(a, b)
Definition: common.h:96
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
static RTPDynamicProtocolHandler * rtp_first_dynamic_payload_handler
Definition: rtpdec.c:66
Definition: rtp.h:98
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx, RTPDynamicProtocolHandler *handler)
Definition: rtpdec.c:546
int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd, AVIOContext *avio)
Definition: rtpdec.c:442
Stream structure.
Definition: avformat.h:876
uint32_t received
packets received
Definition: rtpdec.h:85
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
RTPDynamicProtocolHandler ff_g726_16_dynamic_handler
int64_t last_rtcp_reception_time
Definition: rtpdec.h:179
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:252
Definition: rtp.h:100
static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt, uint8_t **bufptr, int len)
Definition: rtpdec.c:754
RTPDynamicProtocolHandler ff_vc2hq_dynamic_handler
Definition: rtpdec_vc2hq.c:219
int64_t unwrapped_timestamp
Definition: rtpdec.h:158
uint32_t last_rtcp_timestamp
Definition: rtpdec.h:181
static int has_next_packet(RTPDemuxContext *s)
Definition: rtpdec.c:722
void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
Definition: rtpdec.c:68
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:182
RTPDynamicProtocolHandler ff_mp4v_es_dynamic_handler
Definition: rtpdec_mpeg4.c:317
unsigned int octet_count
Definition: rtpdec.h:186
RTPDynamicProtocolHandler ff_h261_dynamic_handler
Definition: rtpdec_h261.c:165
void * buf
Definition: avisynth_c.h:553
Definition: url.h:38
RTPStatistics statistics
Statistics for this stream (used by RTCP receiver reports)
Definition: rtpdec.h:168
uint32_t received_prior
packets received in last interval
Definition: rtpdec.h:87
RTPDynamicProtocolHandler ff_qdm2_dynamic_handler
Definition: rtpdec_qdm2.c:301
uint32_t bad_seq
last bad sequence number + 1
Definition: rtpdec.h:83
AVMediaType
Definition: avutil.h:191
int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
Definition: rtpdec.c:727
int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd, AVIOContext *avio, int count)
some rtp servers assume client is dead if they don't hear from them...
Definition: rtpdec.c:271
#define s1
Definition: regdef.h:38
int ff_parse_fmtp(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *p, int(*parse_fmtp)(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *attr, const char *value))
Definition: rtpdec.c:861
static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
This was the second switch in rtp_parse packet.
Definition: rtpdec.c:564
uint16_t max_seq
highest sequence number seen
Definition: rtpdec.h:80
RTPDynamicProtocolHandler ff_mpegts_dynamic_handler
Definition: rtpdec_mpegts.c:92
RTPDynamicProtocolHandler ff_g726_32_dynamic_handler
RTPDynamicProtocolHandler ff_qt_rtp_aud_handler
const char * enc_name
Definition: rtpdec.h:116
uint8_t * buf
Definition: rtpdec.h:143
#define RTP_PT_IS_RTCP(x)
Definition: rtp.h:110
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:452
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
static int flags
Definition: cpu.c:47
RTPDynamicProtocolHandler ff_mpeg_audio_dynamic_handler
Definition: rtpdec_mpeg12.c:51
int sample_rate
Audio only.
Definition: avcodec.h:4049
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:87
RTPDynamicProtocolHandler ff_mpeg_video_dynamic_handler
Definition: rtpdec_mpeg12.c:59
Main libavformat public API header.
struct RTPPacket * next
Definition: rtpdec.h:146
uint32_t ssrc
Definition: rtpdec.h:153
static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler
Definition: rtpdec.c:42
int64_t recvtime
Definition: rtpdec.h:145
RTPDynamicProtocolHandler * ff_rtp_handler_find_by_name(const char *name, enum AVMediaType codec_type)
Definition: rtpdec.c:120
RTPDynamicProtocolHandler ff_g726_40_dynamic_handler
raw UTF-8 text
Definition: avcodec.h:604
RTPDynamicProtocolHandler ff_qcelp_dynamic_handler
Definition: rtpdec_qcelp.c:212
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
int den
denominator
Definition: rational.h:45
RTPDynamicProtocolHandler ff_hevc_dynamic_handler
Definition: rtpdec_hevc.c:350
Definition: rtp.h:97
int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
Close the dynamic buffer and make a packet from it.
Definition: rtpdec.c:899
uint32_t base_timestamp
Definition: rtpdec.h:156
RTPDynamicProtocolHandler ff_g726_24_dynamic_handler
RTPDynamicProtocolHandler ff_h264_dynamic_handler
Definition: rtpdec_h264.c:411
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_free(p)
RTPDynamicProtocolHandler ff_vp9_dynamic_handler
Definition: rtpdec_vp9.c:333
as in Berlin toast format
Definition: avcodec.h:532
int len
int ff_srtp_set_crypto(struct SRTPContext *s, const char *suite, const char *params)
Definition: srtp.c:65
void ff_rtp_parse_close(RTPDemuxContext *s)
Definition: rtpdec.c:854
static RTPDynamicProtocolHandler t140_dynamic_handler
Definition: rtpdec.c:60
static void stats(const struct CachedBuf *in, int n_in, unsigned *_max, unsigned *_sum)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1587
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:354
#define RTP_NOTS_VALUE
Definition: rtpdec.h:40
static int enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
Definition: rtpdec.c:695
#define av_freep(p)
unbuffered private I/O API
static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
Definition: rtpdec.c:182
AVCodecParameters * codecpar
Definition: avformat.h:1228
int(* need_keyframe)(PayloadContext *context)
Definition: rtpdec.h:136
static RTPDynamicProtocolHandler opus_dynamic_handler
Definition: rtpdec.c:54
int stream_index
Definition: avcodec.h:1590
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:913
RTPDynamicProtocolHandler ff_quicktime_rtp_vid_handler
RTPDynamicProtocolHandler ff_mpeg4_generic_dynamic_handler
Definition: rtpdec_mpeg4.c:326
int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt, uint8_t **bufptr, int len)
Parse an RTP or RTCP packet directly sent as a buffer.
Definition: rtpdec.c:841
RTPDynamicProtocolHandler ff_amr_nb_dynamic_handler
Definition: rtpdec_amr.c:185
AVStream * st
Definition: rtpdec.h:151
This structure stores compressed data.
Definition: avcodec.h:1565
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
Definition: rtpdec.c:207
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1581
RTPDynamicProtocolHandler ff_vp8_dynamic_handler
Definition: rtpdec_vp8.c:279
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
#define MIN_FEEDBACK_INTERVAL
Definition: rtpdec.c:34
#define av_unused
Definition: attributes.h:126
struct SRTPContext srtp
Definition: rtpdec.h:165
const char * name
Definition: opengl_enc.c:103
struct RTPDynamicProtocolHandler * next
Definition: rtpdec.h:138