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