FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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_H263:
53  case AV_CODEC_ID_H263P:
54  case AV_CODEC_ID_H264:
57  case AV_CODEC_ID_MPEG4:
58  case AV_CODEC_ID_AAC:
59  case AV_CODEC_ID_MP2:
60  case AV_CODEC_ID_MP3:
63  case AV_CODEC_ID_PCM_S8:
68  case AV_CODEC_ID_PCM_U8:
70  case AV_CODEC_ID_AMR_NB:
71  case AV_CODEC_ID_AMR_WB:
72  case AV_CODEC_ID_VORBIS:
73  case AV_CODEC_ID_THEORA:
74  case AV_CODEC_ID_VP8:
77  case AV_CODEC_ID_ILBC:
78  case AV_CODEC_ID_MJPEG:
79  case AV_CODEC_ID_SPEEX:
80  case AV_CODEC_ID_OPUS:
81  return 1;
82  default:
83  return 0;
84  }
85 }
86 
88 {
89  RTPMuxContext *s = s1->priv_data;
90  int n;
91  AVStream *st;
92 
93  if (s1->nb_streams != 1) {
94  av_log(s1, AV_LOG_ERROR, "Only one stream supported in the RTP muxer\n");
95  return AVERROR(EINVAL);
96  }
97  st = s1->streams[0];
98  if (!is_supported(st->codec->codec_id)) {
99  av_log(s1, AV_LOG_ERROR, "Unsupported codec %s\n", avcodec_get_name(st->codec->codec_id));
100 
101  return -1;
102  }
103 
104  if (s->payload_type < 0) {
105  /* Re-validate non-dynamic payload types */
106  if (st->id < RTP_PT_PRIVATE)
107  st->id = ff_rtp_get_payload_type(s1, st->codec, -1);
108 
109  s->payload_type = st->id;
110  } else {
111  /* private option takes priority */
112  st->id = s->payload_type;
113  }
114 
116  s->timestamp = s->base_timestamp;
117  s->cur_timestamp = 0;
118  if (!s->ssrc)
119  s->ssrc = av_get_random_seed();
120  s->first_packet = 1;
123  /* Round the NTP time to whole milliseconds. */
124  s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
126  // Pick a random sequence start number, but in the lower end of the
127  // available range, so that any wraparound doesn't happen immediately.
128  // (Immediate wraparound would be an issue for SRTP.)
129  if (s->seq < 0) {
130  if (s1->flags & AVFMT_FLAG_BITEXACT) {
131  s->seq = 0;
132  } else
133  s->seq = av_get_random_seed() & 0x0fff;
134  } else
135  s->seq &= 0xffff; // Use the given parameter, wrapped to the right interval
136 
137  if (s1->packet_size) {
138  if (s1->pb->max_packet_size)
139  s1->packet_size = FFMIN(s1->packet_size,
140  s1->pb->max_packet_size);
141  } else
142  s1->packet_size = s1->pb->max_packet_size;
143  if (s1->packet_size <= 12) {
144  av_log(s1, AV_LOG_ERROR, "Max packet size %d too low\n", s1->packet_size);
145  return AVERROR(EIO);
146  }
147  s->buf = av_malloc(s1->packet_size);
148  if (s->buf == NULL) {
149  return AVERROR(ENOMEM);
150  }
151  s->max_payload_size = s1->packet_size - 12;
152 
153  s->max_frames_per_packet = 0;
154  if (s1->max_delay > 0) {
155  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
157  if (!frame_size)
158  frame_size = st->codec->frame_size;
159  if (frame_size == 0) {
160  av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
161  } else {
165  (AVRational){ frame_size, st->codec->sample_rate },
166  AV_ROUND_DOWN);
167  }
168  }
169  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
170  /* FIXME: We should round down here... */
171  if (st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0) {
173  (AVRational){1, 1000000},
174  av_inv_q(st->avg_frame_rate));
175  } else
176  s->max_frames_per_packet = 1;
177  }
178  }
179 
180  avpriv_set_pts_info(st, 32, 1, 90000);
181  switch(st->codec->codec_id) {
182  case AV_CODEC_ID_MP2:
183  case AV_CODEC_ID_MP3:
184  s->buf_ptr = s->buf + 4;
185  break;
188  break;
189  case AV_CODEC_ID_MPEG2TS:
190  n = s->max_payload_size / TS_PACKET_SIZE;
191  if (n < 1)
192  n = 1;
193  s->max_payload_size = n * TS_PACKET_SIZE;
194  s->buf_ptr = s->buf;
195  break;
196  case AV_CODEC_ID_H264:
197  /* check for H.264 MP4 syntax */
198  if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
199  s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
200  }
201  break;
202  case AV_CODEC_ID_VORBIS:
203  case AV_CODEC_ID_THEORA:
204  if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
205  s->max_frames_per_packet = av_clip(s->max_frames_per_packet, 1, 15);
206  s->max_payload_size -= 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
207  s->num_frames = 0;
208  goto defaultcase;
210  /* Due to a historical error, the clock rate for G722 in RTP is
211  * 8000, even if the sample rate is 16000. See RFC 3551. */
212  avpriv_set_pts_info(st, 32, 1, 8000);
213  break;
214  case AV_CODEC_ID_OPUS:
215  if (st->codec->channels > 2) {
216  av_log(s1, AV_LOG_ERROR, "Multistream opus not supported in RTP\n");
217  goto fail;
218  }
219  /* The opus RTP RFC says that all opus streams should use 48000 Hz
220  * as clock rate, since all opus sample rates can be expressed in
221  * this clock rate, and sample rate changes on the fly are supported. */
222  avpriv_set_pts_info(st, 32, 1, 48000);
223  break;
224  case AV_CODEC_ID_ILBC:
225  if (st->codec->block_align != 38 && st->codec->block_align != 50) {
226  av_log(s1, AV_LOG_ERROR, "Incorrect iLBC block size specified\n");
227  goto fail;
228  }
229  if (!s->max_frames_per_packet)
230  s->max_frames_per_packet = 1;
231  s->max_frames_per_packet = FFMIN(s->max_frames_per_packet,
232  s->max_payload_size / st->codec->block_align);
233  goto defaultcase;
234  case AV_CODEC_ID_AMR_NB:
235  case AV_CODEC_ID_AMR_WB:
236  if (!s->max_frames_per_packet)
237  s->max_frames_per_packet = 12;
238  if (st->codec->codec_id == AV_CODEC_ID_AMR_NB)
239  n = 31;
240  else
241  n = 61;
242  /* max_header_toc_size + the largest AMR payload must fit */
243  if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
244  av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
245  goto fail;
246  }
247  if (st->codec->channels != 1) {
248  av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
249  goto fail;
250  }
251  case AV_CODEC_ID_AAC:
252  s->num_frames = 0;
253  default:
254 defaultcase:
255  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
256  avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
257  }
258  s->buf_ptr = s->buf;
259  break;
260  }
261 
262  return 0;
263 
264 fail:
265  av_freep(&s->buf);
266  return AVERROR(EINVAL);
267 }
268 
269 /* send an rtcp sender report packet */
270 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time, int bye)
271 {
272  RTPMuxContext *s = s1->priv_data;
273  uint32_t rtp_ts;
274 
275  av_dlog(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
276 
277  s->last_rtcp_ntp_time = ntp_time;
278  rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
279  s1->streams[0]->time_base) + s->base_timestamp;
280  avio_w8(s1->pb, RTP_VERSION << 6);
281  avio_w8(s1->pb, RTCP_SR);
282  avio_wb16(s1->pb, 6); /* length in words - 1 */
283  avio_wb32(s1->pb, s->ssrc);
284  avio_wb64(s1->pb, NTP_TO_RTP_FORMAT(ntp_time));
285  avio_wb32(s1->pb, rtp_ts);
286  avio_wb32(s1->pb, s->packet_count);
287  avio_wb32(s1->pb, s->octet_count);
288 
289  if (s->cname) {
290  int len = FFMIN(strlen(s->cname), 255);
291  avio_w8(s1->pb, (RTP_VERSION << 6) + 1);
292  avio_w8(s1->pb, RTCP_SDES);
293  avio_wb16(s1->pb, (7 + len + 3) / 4); /* length in words - 1 */
294 
295  avio_wb32(s1->pb, s->ssrc);
296  avio_w8(s1->pb, 0x01); /* CNAME */
297  avio_w8(s1->pb, len);
298  avio_write(s1->pb, s->cname, len);
299  avio_w8(s1->pb, 0); /* END */
300  for (len = (7 + len) % 4; len % 4; len++)
301  avio_w8(s1->pb, 0);
302  }
303 
304  if (bye) {
305  avio_w8(s1->pb, (RTP_VERSION << 6) | 1);
306  avio_w8(s1->pb, RTCP_BYE);
307  avio_wb16(s1->pb, 1); /* length in words - 1 */
308  avio_wb32(s1->pb, s->ssrc);
309  }
310 
311  avio_flush(s1->pb);
312 }
313 
314 /* send an rtp packet. sequence number is incremented, but the caller
315  must update the timestamp itself */
316 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
317 {
318  RTPMuxContext *s = s1->priv_data;
319 
320  av_dlog(s1, "rtp_send_data size=%d\n", len);
321 
322  /* build the RTP header */
323  avio_w8(s1->pb, RTP_VERSION << 6);
324  avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
325  avio_wb16(s1->pb, s->seq);
326  avio_wb32(s1->pb, s->timestamp);
327  avio_wb32(s1->pb, s->ssrc);
328 
329  avio_write(s1->pb, buf1, len);
330  avio_flush(s1->pb);
331 
332  s->seq = (s->seq + 1) & 0xffff;
333  s->octet_count += len;
334  s->packet_count++;
335 }
336 
337 /* send an integer number of samples and compute time stamp and fill
338  the rtp send buffer before sending. */
340  const uint8_t *buf1, int size, int sample_size_bits)
341 {
342  RTPMuxContext *s = s1->priv_data;
343  int len, max_packet_size, n;
344  /* Calculate the number of bytes to get samples aligned on a byte border */
345  int aligned_samples_size = sample_size_bits/av_gcd(sample_size_bits, 8);
346 
347  max_packet_size = (s->max_payload_size / aligned_samples_size) * aligned_samples_size;
348  /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
349  if ((sample_size_bits % 8) == 0 && ((8 * size) % sample_size_bits) != 0)
350  return AVERROR(EINVAL);
351  n = 0;
352  while (size > 0) {
353  s->buf_ptr = s->buf;
354  len = FFMIN(max_packet_size, size);
355 
356  /* copy data */
357  memcpy(s->buf_ptr, buf1, len);
358  s->buf_ptr += len;
359  buf1 += len;
360  size -= len;
361  s->timestamp = s->cur_timestamp + n * 8 / sample_size_bits;
362  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
363  n += (s->buf_ptr - s->buf);
364  }
365  return 0;
366 }
367 
369  const uint8_t *buf1, int size)
370 {
371  RTPMuxContext *s = s1->priv_data;
372  int len, count, max_packet_size;
373 
374  max_packet_size = s->max_payload_size;
375 
376  /* test if we must flush because not enough space */
377  len = (s->buf_ptr - s->buf);
378  if ((len + size) > max_packet_size) {
379  if (len > 4) {
380  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
381  s->buf_ptr = s->buf + 4;
382  }
383  }
384  if (s->buf_ptr == s->buf + 4) {
385  s->timestamp = s->cur_timestamp;
386  }
387 
388  /* add the packet */
389  if (size > max_packet_size) {
390  /* big packet: fragment */
391  count = 0;
392  while (size > 0) {
393  len = max_packet_size - 4;
394  if (len > size)
395  len = size;
396  /* build fragmented packet */
397  s->buf[0] = 0;
398  s->buf[1] = 0;
399  s->buf[2] = count >> 8;
400  s->buf[3] = count;
401  memcpy(s->buf + 4, buf1, len);
402  ff_rtp_send_data(s1, s->buf, len + 4, 0);
403  size -= len;
404  buf1 += len;
405  count += len;
406  }
407  } else {
408  if (s->buf_ptr == s->buf + 4) {
409  /* no fragmentation possible */
410  s->buf[0] = 0;
411  s->buf[1] = 0;
412  s->buf[2] = 0;
413  s->buf[3] = 0;
414  }
415  memcpy(s->buf_ptr, buf1, size);
416  s->buf_ptr += size;
417  }
418 }
419 
421  const uint8_t *buf1, int size)
422 {
423  RTPMuxContext *s = s1->priv_data;
424  int len, max_packet_size;
425 
426  max_packet_size = s->max_payload_size;
427 
428  while (size > 0) {
429  len = max_packet_size;
430  if (len > size)
431  len = size;
432 
433  s->timestamp = s->cur_timestamp;
434  ff_rtp_send_data(s1, buf1, len, (len == size));
435 
436  buf1 += len;
437  size -= len;
438  }
439 }
440 
441 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
443  const uint8_t *buf1, int size)
444 {
445  RTPMuxContext *s = s1->priv_data;
446  int len, out_len;
447 
448  while (size >= TS_PACKET_SIZE) {
449  len = s->max_payload_size - (s->buf_ptr - s->buf);
450  if (len > size)
451  len = size;
452  memcpy(s->buf_ptr, buf1, len);
453  buf1 += len;
454  size -= len;
455  s->buf_ptr += len;
456 
457  out_len = s->buf_ptr - s->buf;
458  if (out_len >= s->max_payload_size) {
459  ff_rtp_send_data(s1, s->buf, out_len, 0);
460  s->buf_ptr = s->buf;
461  }
462  }
463 }
464 
465 static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
466 {
467  RTPMuxContext *s = s1->priv_data;
468  AVStream *st = s1->streams[0];
469  int frame_duration = av_get_audio_frame_duration(st->codec, 0);
470  int frame_size = st->codec->block_align;
471  int frames = size / frame_size;
472 
473  while (frames > 0) {
474  int n = FFMIN(s->max_frames_per_packet - s->num_frames, frames);
475 
476  if (!s->num_frames) {
477  s->buf_ptr = s->buf;
478  s->timestamp = s->cur_timestamp;
479  }
480  memcpy(s->buf_ptr, buf, n * frame_size);
481  frames -= n;
482  s->num_frames += n;
483  s->buf_ptr += n * frame_size;
484  buf += n * frame_size;
485  s->cur_timestamp += n * frame_duration;
486 
487  if (s->num_frames == s->max_frames_per_packet) {
488  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
489  s->num_frames = 0;
490  }
491  }
492  return 0;
493 }
494 
496 {
497  RTPMuxContext *s = s1->priv_data;
498  AVStream *st = s1->streams[0];
499  int rtcp_bytes;
500  int size= pkt->size;
501 
502  av_dlog(s1, "%d: write len=%d\n", pkt->stream_index, size);
503 
504  rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
506  if ((s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
507  (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) &&
508  !(s->flags & FF_RTP_FLAG_SKIP_RTCP)) {
509  rtcp_send_sr(s1, ff_ntp_time(), 0);
511  s->first_packet = 0;
512  }
513  s->cur_timestamp = s->base_timestamp + pkt->pts;
514 
515  switch(st->codec->codec_id) {
518  case AV_CODEC_ID_PCM_U8:
519  case AV_CODEC_ID_PCM_S8:
520  return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
525  return rtp_send_samples(s1, pkt->data, size, 16 * st->codec->channels);
527  /* The actual sample size is half a byte per sample, but since the
528  * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
529  * the correct parameter for send_samples_bits is 8 bits per stream
530  * clock. */
531  return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
533  return rtp_send_samples(s1, pkt->data, size,
535  case AV_CODEC_ID_MP2:
536  case AV_CODEC_ID_MP3:
537  rtp_send_mpegaudio(s1, pkt->data, size);
538  break;
541  ff_rtp_send_mpegvideo(s1, pkt->data, size);
542  break;
543  case AV_CODEC_ID_AAC:
544  if (s->flags & FF_RTP_FLAG_MP4A_LATM)
545  ff_rtp_send_latm(s1, pkt->data, size);
546  else
547  ff_rtp_send_aac(s1, pkt->data, size);
548  break;
549  case AV_CODEC_ID_AMR_NB:
550  case AV_CODEC_ID_AMR_WB:
551  ff_rtp_send_amr(s1, pkt->data, size);
552  break;
553  case AV_CODEC_ID_MPEG2TS:
554  rtp_send_mpegts_raw(s1, pkt->data, size);
555  break;
556  case AV_CODEC_ID_H264:
557  ff_rtp_send_h264(s1, pkt->data, size);
558  break;
559  case AV_CODEC_ID_H263:
560  if (s->flags & FF_RTP_FLAG_RFC2190) {
561  int mb_info_size = 0;
562  const uint8_t *mb_info =
564  &mb_info_size);
565  if (!mb_info) {
566  av_log(s1, AV_LOG_ERROR, "failed to allocate side data\n");
567  return AVERROR(ENOMEM);
568  }
569  ff_rtp_send_h263_rfc2190(s1, pkt->data, size, mb_info, mb_info_size);
570  break;
571  }
572  /* Fallthrough */
573  case AV_CODEC_ID_H263P:
574  ff_rtp_send_h263(s1, pkt->data, size);
575  break;
576  case AV_CODEC_ID_VORBIS:
577  case AV_CODEC_ID_THEORA:
578  ff_rtp_send_xiph(s1, pkt->data, size);
579  break;
580  case AV_CODEC_ID_VP8:
581  ff_rtp_send_vp8(s1, pkt->data, size);
582  break;
583  case AV_CODEC_ID_ILBC:
584  rtp_send_ilbc(s1, pkt->data, size);
585  break;
586  case AV_CODEC_ID_MJPEG:
587  ff_rtp_send_jpeg(s1, pkt->data, size);
588  break;
589  case AV_CODEC_ID_OPUS:
590  if (size > s->max_payload_size) {
591  av_log(s1, AV_LOG_ERROR,
592  "Packet size %d too large for max RTP payload size %d\n",
593  size, s->max_payload_size);
594  return AVERROR(EINVAL);
595  }
596  /* Intentional fallthrough */
597  default:
598  /* better than nothing : send the codec raw data */
599  rtp_send_raw(s1, pkt->data, size);
600  break;
601  }
602  return 0;
603 }
604 
606 {
607  RTPMuxContext *s = s1->priv_data;
608 
609  /* If the caller closes and recreates ->pb, this might actually
610  * be NULL here even if it was successfully allocated at the start. */
611  if (s1->pb && (s->flags & FF_RTP_FLAG_SEND_BYE))
612  rtcp_send_sr(s1, ff_ntp_time(), 1);
613  av_freep(&s->buf);
614 
615  return 0;
616 }
617 
619  .name = "rtp",
620  .long_name = NULL_IF_CONFIG_SMALL("RTP output"),
621  .priv_data_size = sizeof(RTPMuxContext),
622  .audio_codec = AV_CODEC_ID_PCM_MULAW,
623  .video_codec = AV_CODEC_ID_MPEG4,
627  .priv_class = &rtp_muxer_class,
628 };