FFmpeg  4.4.9
rtpenc.c
Go to the documentation of this file.
1 /*
2  * RTP output 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 "avformat.h"
23 #include "mpegts.h"
24 #include "internal.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/random_seed.h"
27 #include "libavutil/opt.h"
28 
29 #include "rtpenc.h"
30 
31 static const AVOption options[] = {
33  { "payload_type", "Specify RTP payload type", offsetof(RTPMuxContext, payload_type), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, 127, AV_OPT_FLAG_ENCODING_PARAM },
34  { "ssrc", "Stream identifier", offsetof(RTPMuxContext, ssrc), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
35  { "cname", "CNAME to include in RTCP SR packets", offsetof(RTPMuxContext, cname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
36  { "seq", "Starting sequence number", offsetof(RTPMuxContext, seq), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, AV_OPT_FLAG_ENCODING_PARAM },
37  { NULL },
38 };
39 
40 static const AVClass rtp_muxer_class = {
41  .class_name = "RTP muxer",
42  .item_name = av_default_item_name,
43  .option = options,
44  .version = LIBAVUTIL_VERSION_INT,
45 };
46 
47 #define RTCP_SR_SIZE 28
48 
49 static int is_supported(enum AVCodecID id)
50 {
51  switch(id) {
52  case AV_CODEC_ID_DIRAC:
53  case AV_CODEC_ID_H261:
54  case AV_CODEC_ID_H263:
55  case AV_CODEC_ID_H263P:
56  case AV_CODEC_ID_H264:
57  case AV_CODEC_ID_HEVC:
60  case AV_CODEC_ID_MPEG4:
61  case AV_CODEC_ID_AAC:
62  case AV_CODEC_ID_MP2:
63  case AV_CODEC_ID_MP3:
66  case AV_CODEC_ID_PCM_S8:
72  case AV_CODEC_ID_PCM_U8:
74  case AV_CODEC_ID_AMR_NB:
75  case AV_CODEC_ID_AMR_WB:
76  case AV_CODEC_ID_VORBIS:
77  case AV_CODEC_ID_THEORA:
78  case AV_CODEC_ID_VP8:
79  case AV_CODEC_ID_VP9:
83  case AV_CODEC_ID_ILBC:
84  case AV_CODEC_ID_MJPEG:
85  case AV_CODEC_ID_SPEEX:
86  case AV_CODEC_ID_OPUS:
87  return 1;
88  default:
89  return 0;
90  }
91 }
92 
94 {
95  RTPMuxContext *s = s1->priv_data;
96  int n, ret = AVERROR(EINVAL);
97  int payload_header_size = 0;
98  AVStream *st;
99 
100  if (s1->nb_streams != 1) {
101  av_log(s1, AV_LOG_ERROR, "Only one stream supported in the RTP muxer\n");
102  return AVERROR(EINVAL);
103  }
104  st = s1->streams[0];
105  if (!is_supported(st->codecpar->codec_id)) {
106  av_log(s1, AV_LOG_ERROR, "Unsupported codec %s\n", avcodec_get_name(st->codecpar->codec_id));
107 
108  return -1;
109  }
110 
111  if (s->payload_type < 0) {
112  /* Re-validate non-dynamic payload types */
113  if (st->id < RTP_PT_PRIVATE)
114  st->id = ff_rtp_get_payload_type(s1, st->codecpar, -1);
115 
116  s->payload_type = st->id;
117  } else {
118  /* private option takes priority */
119  st->id = s->payload_type;
120  }
121 
122  s->base_timestamp = av_get_random_seed();
123  s->timestamp = s->base_timestamp;
124  s->cur_timestamp = 0;
125  if (!s->ssrc)
126  s->ssrc = av_get_random_seed();
127  s->first_packet = 1;
128  s->first_rtcp_ntp_time = ff_ntp_time();
129  if (s1->start_time_realtime != 0 && s1->start_time_realtime != AV_NOPTS_VALUE)
130  /* Round the NTP time to whole milliseconds. */
131  s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
133  // Pick a random sequence start number, but in the lower end of the
134  // available range, so that any wraparound doesn't happen immediately.
135  // (Immediate wraparound would be an issue for SRTP.)
136  if (s->seq < 0) {
137  if (s1->flags & AVFMT_FLAG_BITEXACT) {
138  s->seq = 0;
139  } else
140  s->seq = av_get_random_seed() & 0x0fff;
141  } else
142  s->seq &= 0xffff; // Use the given parameter, wrapped to the right interval
143 
144  if (s1->packet_size) {
145  if (s1->pb->max_packet_size)
146  s1->packet_size = FFMIN(s1->packet_size,
147  s1->pb->max_packet_size);
148  } else
149  s1->packet_size = s1->pb->max_packet_size;
150  if (s1->packet_size <= 12) {
151  av_log(s1, AV_LOG_ERROR, "Max packet size %u too low\n", s1->packet_size);
152  return AVERROR(EIO);
153  }
154  s->buf = av_malloc(s1->packet_size);
155  if (!s->buf) {
156  return AVERROR(ENOMEM);
157  }
158  s->max_payload_size = s1->packet_size - 12;
159 
160  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
161  avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate);
162  } else {
163  avpriv_set_pts_info(st, 32, 1, 90000);
164  }
165  s->buf_ptr = s->buf;
166  switch(st->codecpar->codec_id) {
167  case AV_CODEC_ID_MP2:
168  case AV_CODEC_ID_MP3:
169  s->buf_ptr = s->buf + 4;
170  payload_header_size = 4;
171  avpriv_set_pts_info(st, 32, 1, 90000);
172  break;
175  payload_header_size = 4;
176  break;
177  case AV_CODEC_ID_MPEG2TS:
178  if (s->max_payload_size < TS_PACKET_SIZE) {
180  "RTP payload size %u too small for MPEG-TS "
181  "(minimum %d bytes required)\n",
182  s->max_payload_size, TS_PACKET_SIZE);
183  ret = AVERROR(EINVAL);
184  goto fail;
185  }
186 
187  n = s->max_payload_size / TS_PACKET_SIZE;
188  s->max_payload_size = n * TS_PACKET_SIZE;
189  break;
190  case AV_CODEC_ID_DIRAC:
191  if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
193  "Packetizing VC-2 is experimental and does not use all values "
194  "of the specification "
195  "(even though most receivers may handle it just fine). "
196  "Please set -strict experimental in order to enable it.\n");
197  ret = AVERROR_EXPERIMENTAL;
198  goto fail;
199  }
200  payload_header_size = 20;
201  break;
202  case AV_CODEC_ID_H263:
203  if (s->flags & FF_RTP_FLAG_RFC2190) {
204  payload_header_size = 8;
205  break;
206  }
207  // fall-through
208  case AV_CODEC_ID_H263P:
209  payload_header_size = 2;
210  break;
211  case AV_CODEC_ID_H261:
212  if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
214  "Packetizing H.261 is experimental and produces incorrect "
215  "packetization for cases where GOBs don't fit into packets "
216  "(even though most receivers may handle it just fine). "
217  "Please set -f_strict experimental in order to enable it.\n");
218  ret = AVERROR_EXPERIMENTAL;
219  goto fail;
220  }
221  payload_header_size = 4;
222  break;
223  case AV_CODEC_ID_H264:
224  /* check for H.264 MP4 syntax */
225  if (st->codecpar->extradata_size > 4 && st->codecpar->extradata[0] == 1) {
226  s->nal_length_size = (st->codecpar->extradata[4] & 0x03) + 1;
227  }
228  payload_header_size = 2;
229  break;
230  case AV_CODEC_ID_HEVC:
231  /* Only check for the standardized hvcC version of extradata, keeping
232  * things simple and similar to the avcC/H.264 case above, instead
233  * of trying to handle the pre-standardization versions (as in
234  * libavcodec/hevc.c). */
235  if (st->codecpar->extradata_size > 21 && st->codecpar->extradata[0] == 1) {
236  s->nal_length_size = (st->codecpar->extradata[21] & 0x03) + 1;
237  }
238  payload_header_size = 3;
239  break;
240  case AV_CODEC_ID_VP8:
241  payload_header_size = 4;
242  break;
243  case AV_CODEC_ID_VP9:
244  if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
246  "Packetizing VP9 is experimental and its specification is "
247  "still in draft state. "
248  "Please set -strict experimental in order to enable it.\n");
249  ret = AVERROR_EXPERIMENTAL;
250  goto fail;
251  }
252  payload_header_size = 1;
253  break;
254  case AV_CODEC_ID_VORBIS:
255  case AV_CODEC_ID_THEORA:
256  s->max_frames_per_packet = 15;
257  break;
259  /* Due to a historical error, the clock rate for G722 in RTP is
260  * 8000, even if the sample rate is 16000. See RFC 3551. */
261  avpriv_set_pts_info(st, 32, 1, 8000);
262  break;
263  case AV_CODEC_ID_OPUS:
264  if (st->codecpar->channels > 2) {
265  av_log(s1, AV_LOG_ERROR, "Multistream opus not supported in RTP\n");
266  goto fail;
267  }
268  /* The opus RTP RFC says that all opus streams should use 48000 Hz
269  * as clock rate, since all opus sample rates can be expressed in
270  * this clock rate, and sample rate changes on the fly are supported. */
271  avpriv_set_pts_info(st, 32, 1, 48000);
272  break;
273  case AV_CODEC_ID_ILBC:
274  if (st->codecpar->block_align != 38 && st->codecpar->block_align != 50) {
275  av_log(s1, AV_LOG_ERROR, "Incorrect iLBC block size specified\n");
276  goto fail;
277  }
278  s->max_frames_per_packet = s->max_payload_size / st->codecpar->block_align;
279  if (!s->max_frames_per_packet) {
280  av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for iLBC\n");
281  goto fail;
282  }
283  break;
284  case AV_CODEC_ID_AMR_NB:
285  case AV_CODEC_ID_AMR_WB:
286  s->max_frames_per_packet = 50;
288  n = 31;
289  else
290  n = 61;
291  /* max_header_toc_size + the largest AMR payload must fit */
292  if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
293  av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
294  goto fail;
295  }
296  if (st->codecpar->channels != 1) {
297  av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
298  goto fail;
299  }
300  break;
301  case AV_CODEC_ID_AAC:
302  s->max_frames_per_packet = 50;
303  if (!(s->flags & FF_RTP_FLAG_MP4A_LATM))
304  payload_header_size = 4;
305  break;
306  default:
307  break;
308  }
309 
310  if (s->max_payload_size <= payload_header_size) {
311  av_log(s1, AV_LOG_ERROR, "Max packet size %u too low for a %d byte payload header\n",
312  s1->packet_size, payload_header_size);
313  goto fail;
314  }
315 
316  return 0;
317 
318 fail:
319  av_freep(&s->buf);
320  return ret;
321 }
322 
323 /* send an rtcp sender report packet */
324 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time, int bye)
325 {
326  RTPMuxContext *s = s1->priv_data;
327  uint32_t rtp_ts;
328 
329  av_log(s1, AV_LOG_TRACE, "RTCP: %02x %"PRIx64" %"PRIx32"\n", s->payload_type, ntp_time, s->timestamp);
330 
331  s->last_rtcp_ntp_time = ntp_time;
332  rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
333  s1->streams[0]->time_base) + s->base_timestamp;
334  avio_w8(s1->pb, RTP_VERSION << 6);
335  avio_w8(s1->pb, RTCP_SR);
336  avio_wb16(s1->pb, 6); /* length in words - 1 */
337  avio_wb32(s1->pb, s->ssrc);
338  avio_wb32(s1->pb, ntp_time / 1000000);
339  avio_wb32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
340  avio_wb32(s1->pb, rtp_ts);
341  avio_wb32(s1->pb, s->packet_count);
342  avio_wb32(s1->pb, s->octet_count);
343 
344  if (s->cname) {
345  int len = FFMIN(strlen(s->cname), 255);
346  avio_w8(s1->pb, (RTP_VERSION << 6) + 1);
347  avio_w8(s1->pb, RTCP_SDES);
348  avio_wb16(s1->pb, (7 + len + 3) / 4); /* length in words - 1 */
349 
350  avio_wb32(s1->pb, s->ssrc);
351  avio_w8(s1->pb, 0x01); /* CNAME */
352  avio_w8(s1->pb, len);
353  avio_write(s1->pb, s->cname, len);
354  avio_w8(s1->pb, 0); /* END */
355  for (len = (7 + len) % 4; len % 4; len++)
356  avio_w8(s1->pb, 0);
357  }
358 
359  if (bye) {
360  avio_w8(s1->pb, (RTP_VERSION << 6) | 1);
361  avio_w8(s1->pb, RTCP_BYE);
362  avio_wb16(s1->pb, 1); /* length in words - 1 */
363  avio_wb32(s1->pb, s->ssrc);
364  }
365 
366  avio_flush(s1->pb);
367 }
368 
369 /* send an rtp packet. sequence number is incremented, but the caller
370  must update the timestamp itself */
371 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
372 {
373  RTPMuxContext *s = s1->priv_data;
374 
375  av_log(s1, AV_LOG_TRACE, "rtp_send_data size=%d\n", len);
376 
377  /* build the RTP header */
378  avio_w8(s1->pb, RTP_VERSION << 6);
379  avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
380  avio_wb16(s1->pb, s->seq);
381  avio_wb32(s1->pb, s->timestamp);
382  avio_wb32(s1->pb, s->ssrc);
383 
384  avio_write(s1->pb, buf1, len);
385  avio_flush(s1->pb);
386 
387  s->seq = (s->seq + 1) & 0xffff;
388  if (!s->seq)
389  s->seq_high++;
390  s->octet_count += len;
391  s->packet_count++;
392 }
393 
394 /* send an integer number of samples and compute time stamp and fill
395  the rtp send buffer before sending. */
397  const uint8_t *buf1, int size, int bits_per_sample)
398 {
399  RTPMuxContext *s = s1->priv_data;
400  int nb_channels = s1->streams[0]->codecpar->channels;
401  int len, max_packet_size, n;
402 
403  if (bits_per_sample <= 0 || nb_channels <= 0 ||
404  nb_channels > INT_MAX / bits_per_sample) {
405  av_log(s1, AV_LOG_ERROR, "Invalid sample size %d or channel count %d\n",
406  bits_per_sample, nb_channels);
407  return AVERROR(EINVAL);
408  }
409  int sample_size_bits = bits_per_sample * nb_channels;
410  /* Calculate the number of bytes to get samples aligned on a byte border */
411  int aligned_samples_size = sample_size_bits/av_gcd(sample_size_bits, 8);
412 
413  max_packet_size = (s->max_payload_size / aligned_samples_size) * aligned_samples_size;
414  if (!max_packet_size) {
415  av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for %d byte samples\n",
416  aligned_samples_size);
417  return AVERROR(EINVAL);
418  }
419  /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
420  if ((sample_size_bits % 8) == 0 && ((8 * size) % sample_size_bits) != 0)
421  return AVERROR(EINVAL);
422  n = 0;
423  while (size > 0) {
424  s->buf_ptr = s->buf;
425  len = FFMIN(max_packet_size, size);
426 
427  /* copy data */
428  memcpy(s->buf_ptr, buf1, len);
429  s->buf_ptr += len;
430  buf1 += len;
431  size -= len;
432  s->timestamp = s->cur_timestamp + n * 8 / sample_size_bits;
433  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
434  n += (s->buf_ptr - s->buf);
435  }
436  return 0;
437 }
438 
440  const uint8_t *buf1, int size)
441 {
442  RTPMuxContext *s = s1->priv_data;
443  int len, count, max_packet_size;
444 
445  max_packet_size = s->max_payload_size;
446 
447  /* test if we must flush because not enough space */
448  len = (s->buf_ptr - s->buf);
449  if ((len + size) > max_packet_size) {
450  if (len > 4) {
451  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
452  s->buf_ptr = s->buf + 4;
453  }
454  }
455  if (s->buf_ptr == s->buf + 4) {
456  s->timestamp = s->cur_timestamp;
457  }
458 
459  /* add the packet */
460  if (size > max_packet_size - 4) {
461  /* big packet: fragment */
462  count = 0;
463  while (size > 0) {
464  len = max_packet_size - 4;
465  if (len > size)
466  len = size;
467  /* build fragmented packet */
468  s->buf[0] = 0;
469  s->buf[1] = 0;
470  s->buf[2] = count >> 8;
471  s->buf[3] = count;
472  memcpy(s->buf + 4, buf1, len);
473  ff_rtp_send_data(s1, s->buf, len + 4, 0);
474  size -= len;
475  buf1 += len;
476  count += len;
477  }
478  } else {
479  if (s->buf_ptr == s->buf + 4) {
480  /* no fragmentation possible */
481  s->buf[0] = 0;
482  s->buf[1] = 0;
483  s->buf[2] = 0;
484  s->buf[3] = 0;
485  }
486  memcpy(s->buf_ptr, buf1, size);
487  s->buf_ptr += size;
488  }
489 }
490 
492  const uint8_t *buf1, int size)
493 {
494  RTPMuxContext *s = s1->priv_data;
495  int len, max_packet_size;
496 
497  max_packet_size = s->max_payload_size;
498 
499  while (size > 0) {
500  len = max_packet_size;
501  if (len > size)
502  len = size;
503 
504  s->timestamp = s->cur_timestamp;
505  ff_rtp_send_data(s1, buf1, len, (len == size));
506 
507  buf1 += len;
508  size -= len;
509  }
510 }
511 
512 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
514  const uint8_t *buf1, int size)
515 {
516  RTPMuxContext *s = s1->priv_data;
517  int len, out_len;
518 
519  s->timestamp = s->cur_timestamp;
520  while (size >= TS_PACKET_SIZE) {
521  len = s->max_payload_size - (s->buf_ptr - s->buf);
522  if (len > size)
523  len = size;
524  memcpy(s->buf_ptr, buf1, len);
525  buf1 += len;
526  size -= len;
527  s->buf_ptr += len;
528 
529  out_len = s->buf_ptr - s->buf;
530  if (out_len >= s->max_payload_size) {
531  ff_rtp_send_data(s1, s->buf, out_len, 0);
532  s->buf_ptr = s->buf;
533  }
534  }
535 }
536 
537 static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
538 {
539  RTPMuxContext *s = s1->priv_data;
540  AVStream *st = s1->streams[0];
541  int frame_duration = av_get_audio_frame_duration2(st->codecpar, 0);
542  int frame_size = st->codecpar->block_align;
543  int frames = size / frame_size;
544 
545  while (frames > 0) {
546  if (s->num_frames > 0 &&
547  av_compare_ts(s->cur_timestamp - s->timestamp, st->time_base,
548  s1->max_delay, AV_TIME_BASE_Q) >= 0) {
549  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
550  s->num_frames = 0;
551  }
552 
553  if (!s->num_frames) {
554  s->buf_ptr = s->buf;
555  s->timestamp = s->cur_timestamp;
556  }
557  memcpy(s->buf_ptr, buf, frame_size);
558  frames--;
559  s->num_frames++;
560  s->buf_ptr += frame_size;
561  buf += frame_size;
562  s->cur_timestamp += frame_duration;
563 
564  if (s->num_frames == s->max_frames_per_packet) {
565  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
566  s->num_frames = 0;
567  }
568  }
569  return 0;
570 }
571 
573 {
574  RTPMuxContext *s = s1->priv_data;
575  AVStream *st = s1->streams[0];
576  int rtcp_bytes;
577  int size= pkt->size;
578 
579  av_log(s1, AV_LOG_TRACE, "%d: write len=%d\n", pkt->stream_index, size);
580 
581  rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
583  if ((s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
584  (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) &&
585  !(s->flags & FF_RTP_FLAG_SKIP_RTCP)) {
586  rtcp_send_sr(s1, ff_ntp_time(), 0);
587  s->last_octet_count = s->octet_count;
588  s->first_packet = 0;
589  }
590  s->cur_timestamp = s->base_timestamp + pkt->pts;
591 
592  switch(st->codecpar->codec_id) {
595  case AV_CODEC_ID_PCM_U8:
596  case AV_CODEC_ID_PCM_S8:
597  return rtp_send_samples(s1, pkt->data, size, 8);
602  return rtp_send_samples(s1, pkt->data, size, 16);
604  return rtp_send_samples(s1, pkt->data, size, 24);
606  /* The actual sample size is half a byte per sample, but since the
607  * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
608  * the correct parameter for send_samples_bits is 8 bits per stream
609  * clock. */
610  return rtp_send_samples(s1, pkt->data, size, 8);
614  case AV_CODEC_ID_MP2:
615  case AV_CODEC_ID_MP3:
617  break;
621  break;
622  case AV_CODEC_ID_AAC: {
623  const uint8_t *aac_frame = pkt->data;
624 
625  /* skip ADTS header, if present */
626  if (!st->codecpar->extradata_size) {
627  if (size < 7) {
628  av_log(s1, AV_LOG_ERROR, "AAC packet too small for ADTS header\n");
629  break;
630  }
631  aac_frame += 7;
632  size -= 7;
633  }
634  if (s->flags & FF_RTP_FLAG_MP4A_LATM)
635  ff_rtp_send_latm(s1, aac_frame, size);
636  else
637  ff_rtp_send_aac(s1, aac_frame, size);
638  break;
639  }
640  case AV_CODEC_ID_AMR_NB:
641  case AV_CODEC_ID_AMR_WB:
643  break;
644  case AV_CODEC_ID_MPEG2TS:
646  break;
647  case AV_CODEC_ID_DIRAC:
649  break;
650  case AV_CODEC_ID_H264:
652  break;
653  case AV_CODEC_ID_H261:
655  break;
656  case AV_CODEC_ID_H263:
657  if (s->flags & FF_RTP_FLAG_RFC2190) {
658  buffer_size_t mb_info_size;
659  const uint8_t *mb_info =
661  &mb_info_size);
662  ff_rtp_send_h263_rfc2190(s1, pkt->data, size, mb_info, mb_info_size);
663  break;
664  }
665  /* Fallthrough */
666  case AV_CODEC_ID_H263P:
668  break;
669  case AV_CODEC_ID_HEVC:
671  break;
672  case AV_CODEC_ID_VORBIS:
673  case AV_CODEC_ID_THEORA:
675  break;
676  case AV_CODEC_ID_VP8:
678  break;
679  case AV_CODEC_ID_VP9:
681  break;
682  case AV_CODEC_ID_ILBC:
684  break;
685  case AV_CODEC_ID_MJPEG:
687  break;
688  case AV_CODEC_ID_OPUS:
689  if (size > s->max_payload_size) {
691  "Packet size %d too large for max RTP payload size %d\n",
692  size, s->max_payload_size);
693  return AVERROR(EINVAL);
694  }
695  /* Intentional fallthrough */
696  default:
697  /* better than nothing : send the codec raw data */
699  break;
700  }
701  return 0;
702 }
703 
705 {
706  RTPMuxContext *s = s1->priv_data;
707 
708  /* If the caller closes and recreates ->pb, this might actually
709  * be NULL here even if it was successfully allocated at the start. */
710  if (s1->pb && (s->flags & FF_RTP_FLAG_SEND_BYE))
711  rtcp_send_sr(s1, ff_ntp_time(), 1);
712  av_freep(&s->buf);
713 
714  return 0;
715 }
716 
718  .name = "rtp",
719  .long_name = NULL_IF_CONFIG_SMALL("RTP output"),
720  .priv_data_size = sizeof(RTPMuxContext),
721  .audio_codec = AV_CODEC_ID_PCM_MULAW,
722  .video_codec = AV_CODEC_ID_MPEG4,
726  .priv_class = &rtp_muxer_class,
728 };
uint8_t
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:1610
Main libavformat public API header.
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1380
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:472
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:208
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:390
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:468
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:230
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:252
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t *size)
Definition: avpacket.c:368
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
int nb_channels
#define fail()
Definition: checkasm.h:133
@ AV_FIELD_PROGRESSIVE
Definition: codec_par.h:38
#define FFMIN(a, b)
Definition: common.h:105
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:740
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:348
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:497
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
@ AV_CODEC_ID_DIRAC
Definition: codec_id.h:165
@ AV_CODEC_ID_MPEG2TS
FAKE codec to indicate a raw MPEG-2 TS stream (only used by libavformat)
Definition: codec_id.h:569
@ AV_CODEC_ID_PCM_S24BE
Definition: codec_id.h:326
@ AV_CODEC_ID_VORBIS
Definition: codec_id.h:429
@ AV_CODEC_ID_H261
Definition: codec_id.h:52
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:318
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:313
@ AV_CODEC_ID_ADPCM_G722
Definition: codec_id.h:381
@ AV_CODEC_ID_H264
Definition: codec_id.h:76
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:314
@ AV_CODEC_ID_ADPCM_G726LE
Definition: codec_id.h:389
@ AV_CODEC_ID_PCM_S8
Definition: codec_id.h:317
@ AV_CODEC_ID_VP8
Definition: codec_id.h:189
@ AV_CODEC_ID_PCM_ALAW
Definition: codec_id.h:320
@ AV_CODEC_ID_AMR_NB
Definition: codec_id.h:406
@ AV_CODEC_ID_THEORA
Definition: codec_id.h:79
@ AV_CODEC_ID_PCM_U16BE
Definition: codec_id.h:316
@ AV_CODEC_ID_MP2
Definition: codec_id.h:424
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:223
@ AV_CODEC_ID_ADPCM_G726
Definition: codec_id.h:364
@ AV_CODEC_ID_AAC
Definition: codec_id.h:426
@ AV_CODEC_ID_H263
Definition: codec_id.h:53
@ AV_CODEC_ID_AMR_WB
Definition: codec_id.h:407
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:61
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:56
@ AV_CODEC_ID_SPEEX
Definition: codec_id.h:459
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:483
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:425
@ AV_CODEC_ID_VP9
Definition: codec_id.h:217
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:50
@ AV_CODEC_ID_H263P
Definition: codec_id.h:68
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:484
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:51
@ AV_CODEC_ID_PCM_U16LE
Definition: codec_id.h:315
@ AV_CODEC_ID_PCM_MULAW
Definition: codec_id.h:319
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
This function is the same as av_get_audio_frame_duration(), except it works with AVCodecParameters in...
Definition: utils.c:891
@ AV_PKT_DATA_H263_MB_INFO
An AV_PKT_DATA_H263_MB_INFO side data packet contains a number of structures with info about macroblo...
Definition: packet.h:93
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it.
Definition: error.h:72
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
Definition: mathematics.c:147
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition: mathematics.c:37
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
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
uint64_t ff_ntp_time(void)
Get the current time since NTP epoch in microseconds.
Definition: utils.c:4708
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4944
#define NTP_OFFSET_US
Definition: internal.h:400
common internal API header
int buffer_size_t
Definition: internal.h:306
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define TS_PACKET_SIZE
Definition: mpegts.h:29
int frame_size
Definition: mxfenc.c:2206
AVOptions.
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:278
#define s1
Definition: regdef.h:38
int ff_rtp_get_payload_type(AVFormatContext *fmt, AVCodecParameters *par, int idx)
Return the payload type for a given stream used in the given format context.
Definition: rtp.c:90
#define RTP_VERSION
Definition: rtp.h:78
@ RTCP_SR
Definition: rtp.h:97
@ RTCP_SDES
Definition: rtp.h:99
@ RTCP_BYE
Definition: rtp.h:100
#define RTP_PT_PRIVATE
Definition: rtp.h:77
#define RTCP_TX_RATIO_NUM
Definition: rtp.h:82
#define RTCP_TX_RATIO_DEN
Definition: rtp.h:83
static int is_supported(enum AVCodecID id)
Definition: rtpenc.c:49
static void rtp_send_mpegaudio(AVFormatContext *s1, const uint8_t *buf1, int size)
Definition: rtpenc.c:439
static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time, int bye)
Definition: rtpenc.c:324
static int rtp_send_samples(AVFormatContext *s1, const uint8_t *buf1, int size, int bits_per_sample)
Definition: rtpenc.c:396
static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: rtpenc.c:572
static const AVOption options[]
Definition: rtpenc.c:31
AVOutputFormat ff_rtp_muxer
Definition: rtpenc.c:717
static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
Definition: rtpenc.c:537
static int rtp_write_header(AVFormatContext *s1)
Definition: rtpenc.c:93
static const AVClass rtp_muxer_class
Definition: rtpenc.c:40
static void rtp_send_mpegts_raw(AVFormatContext *s1, const uint8_t *buf1, int size)
Definition: rtpenc.c:513
static int rtp_write_trailer(AVFormatContext *s1)
Definition: rtpenc.c:704
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
Definition: rtpenc.c:371
#define RTCP_SR_SIZE
Definition: rtpenc.c:47
static void rtp_send_raw(AVFormatContext *s1, const uint8_t *buf1, int size)
Definition: rtpenc.c:491
void ff_rtp_send_h263_rfc2190(AVFormatContext *s1, const uint8_t *buf1, int size, const uint8_t *mb_info, int mb_info_size)
void ff_rtp_send_h264_hevc(AVFormatContext *s1, const uint8_t *buf1, int size)
void ff_rtp_send_vp9(AVFormatContext *s1, const uint8_t *buff, int size)
Definition: rtpenc_vp9.c:26
void ff_rtp_send_vp8(AVFormatContext *s1, const uint8_t *buff, int size)
Definition: rtpenc_vp8.c:26
#define FF_RTP_FLAG_OPTS(ctx, fieldname)
Definition: rtpenc.h:75
void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size)
Packetize H.263 frames into RTP packets according to RFC 4629.
Definition: rtpenc_h263.c:43
void ff_rtp_send_jpeg(AVFormatContext *s1, const uint8_t *buff, int size)
Definition: rtpenc_jpeg.c:28
void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size)
Definition: rtpenc_aac.c:27
void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size)
Definition: rtpenc_mpv.c:29
#define FF_RTP_FLAG_MP4A_LATM
Definition: rtpenc.h:69
#define FF_RTP_FLAG_SEND_BYE
Definition: rtpenc.h:73
#define FF_RTP_FLAG_RFC2190
Definition: rtpenc.h:70
void ff_rtp_send_amr(AVFormatContext *s1, const uint8_t *buff, int size)
Packetize AMR frames into RTP packets according to RFC 3267, in octet-aligned mode.
Definition: rtpenc_amr.c:30
void ff_rtp_send_h261(AVFormatContext *s1, const uint8_t *buf1, int size)
Definition: rtpenc_h261.c:39
void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size)
Packetize Xiph frames into RTP according to RFC 5215 (Vorbis) and the Theora RFC draft.
Definition: rtpenc_xiph.c:33
#define FF_RTP_FLAG_SKIP_RTCP
Definition: rtpenc.h:71
void ff_rtp_send_vc2hq(AVFormatContext *s1, const uint8_t *buf, int size, int interlaced)
Definition: rtpenc_vc2hq.c:123
void ff_rtp_send_latm(AVFormatContext *s1, const uint8_t *buff, int size)
Definition: rtpenc_latm.c:25
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:141
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int channels
Audio only.
Definition: codec_par.h:166
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int block_align
Audio only.
Definition: codec_par.h:177
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
AVOption.
Definition: opt.h:248
const char * name
Definition: avformat.h:491
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int size
Definition: packet.h:370
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
uint8_t * data
Definition: packet.h:369
Rational number (pair of numerator and denominator).
Definition: rational.h:58
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int id
Format-specific stream ID.
Definition: avformat.h:880
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:902
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
int frames
Definition: movenc.c:66
int size
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
int len