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