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