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