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