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;
122  if (s1->start_time_realtime)
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 (st->codec->flags & CODEC_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  s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
172  }
173  }
174 
175  avpriv_set_pts_info(st, 32, 1, 90000);
176  switch(st->codec->codec_id) {
177  case AV_CODEC_ID_MP2:
178  case AV_CODEC_ID_MP3:
179  s->buf_ptr = s->buf + 4;
180  break;
183  break;
184  case AV_CODEC_ID_MPEG2TS:
185  n = s->max_payload_size / TS_PACKET_SIZE;
186  if (n < 1)
187  n = 1;
188  s->max_payload_size = n * TS_PACKET_SIZE;
189  s->buf_ptr = s->buf;
190  break;
191  case AV_CODEC_ID_H264:
192  /* check for H.264 MP4 syntax */
193  if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
194  s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
195  }
196  break;
197  case AV_CODEC_ID_VORBIS:
198  case AV_CODEC_ID_THEORA:
199  if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
200  s->max_frames_per_packet = av_clip(s->max_frames_per_packet, 1, 15);
201  s->max_payload_size -= 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
202  s->num_frames = 0;
203  goto defaultcase;
205  /* Due to a historical error, the clock rate for G722 in RTP is
206  * 8000, even if the sample rate is 16000. See RFC 3551. */
207  avpriv_set_pts_info(st, 32, 1, 8000);
208  break;
209  case AV_CODEC_ID_OPUS:
210  if (st->codec->channels > 2) {
211  av_log(s1, AV_LOG_ERROR, "Multistream opus not supported in RTP\n");
212  goto fail;
213  }
214  /* The opus RTP RFC says that all opus streams should use 48000 Hz
215  * as clock rate, since all opus sample rates can be expressed in
216  * this clock rate, and sample rate changes on the fly are supported. */
217  avpriv_set_pts_info(st, 32, 1, 48000);
218  break;
219  case AV_CODEC_ID_ILBC:
220  if (st->codec->block_align != 38 && st->codec->block_align != 50) {
221  av_log(s1, AV_LOG_ERROR, "Incorrect iLBC block size specified\n");
222  goto fail;
223  }
224  if (!s->max_frames_per_packet)
225  s->max_frames_per_packet = 1;
226  s->max_frames_per_packet = FFMIN(s->max_frames_per_packet,
227  s->max_payload_size / st->codec->block_align);
228  goto defaultcase;
229  case AV_CODEC_ID_AMR_NB:
230  case AV_CODEC_ID_AMR_WB:
231  if (!s->max_frames_per_packet)
232  s->max_frames_per_packet = 12;
233  if (st->codec->codec_id == AV_CODEC_ID_AMR_NB)
234  n = 31;
235  else
236  n = 61;
237  /* max_header_toc_size + the largest AMR payload must fit */
238  if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
239  av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
240  goto fail;
241  }
242  if (st->codec->channels != 1) {
243  av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
244  goto fail;
245  }
246  case AV_CODEC_ID_AAC:
247  s->num_frames = 0;
248  default:
249 defaultcase:
250  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
251  avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
252  }
253  s->buf_ptr = s->buf;
254  break;
255  }
256 
257  return 0;
258 
259 fail:
260  av_freep(&s->buf);
261  return AVERROR(EINVAL);
262 }
263 
264 /* send an rtcp sender report packet */
265 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
266 {
267  RTPMuxContext *s = s1->priv_data;
268  uint32_t rtp_ts;
269 
270  av_dlog(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
271 
272  s->last_rtcp_ntp_time = ntp_time;
273  rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
274  s1->streams[0]->time_base) + s->base_timestamp;
275  avio_w8(s1->pb, (RTP_VERSION << 6));
276  avio_w8(s1->pb, RTCP_SR);
277  avio_wb16(s1->pb, 6); /* length in words - 1 */
278  avio_wb32(s1->pb, s->ssrc);
279  avio_wb32(s1->pb, ntp_time / 1000000);
280  avio_wb32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
281  avio_wb32(s1->pb, rtp_ts);
282  avio_wb32(s1->pb, s->packet_count);
283  avio_wb32(s1->pb, s->octet_count);
284 
285  if (s->cname) {
286  int len = FFMIN(strlen(s->cname), 255);
287  avio_w8(s1->pb, (RTP_VERSION << 6) + 1);
288  avio_w8(s1->pb, RTCP_SDES);
289  avio_wb16(s1->pb, (7 + len + 3) / 4); /* length in words - 1 */
290 
291  avio_wb32(s1->pb, s->ssrc);
292  avio_w8(s1->pb, 0x01); /* CNAME */
293  avio_w8(s1->pb, len);
294  avio_write(s1->pb, s->cname, len);
295  avio_w8(s1->pb, 0); /* END */
296  for (len = (7 + len) % 4; len % 4; len++)
297  avio_w8(s1->pb, 0);
298  }
299 
300  avio_flush(s1->pb);
301 }
302 
303 /* send an rtp packet. sequence number is incremented, but the caller
304  must update the timestamp itself */
305 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
306 {
307  RTPMuxContext *s = s1->priv_data;
308 
309  av_dlog(s1, "rtp_send_data size=%d\n", len);
310 
311  /* build the RTP header */
312  avio_w8(s1->pb, (RTP_VERSION << 6));
313  avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
314  avio_wb16(s1->pb, s->seq);
315  avio_wb32(s1->pb, s->timestamp);
316  avio_wb32(s1->pb, s->ssrc);
317 
318  avio_write(s1->pb, buf1, len);
319  avio_flush(s1->pb);
320 
321  s->seq = (s->seq + 1) & 0xffff;
322  s->octet_count += len;
323  s->packet_count++;
324 }
325 
326 /* send an integer number of samples and compute time stamp and fill
327  the rtp send buffer before sending. */
329  const uint8_t *buf1, int size, int sample_size_bits)
330 {
331  RTPMuxContext *s = s1->priv_data;
332  int len, max_packet_size, n;
333  /* Calculate the number of bytes to get samples aligned on a byte border */
334  int aligned_samples_size = sample_size_bits/av_gcd(sample_size_bits, 8);
335 
336  max_packet_size = (s->max_payload_size / aligned_samples_size) * aligned_samples_size;
337  /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
338  if ((sample_size_bits % 8) == 0 && ((8 * size) % sample_size_bits) != 0)
339  return AVERROR(EINVAL);
340  n = 0;
341  while (size > 0) {
342  s->buf_ptr = s->buf;
343  len = FFMIN(max_packet_size, size);
344 
345  /* copy data */
346  memcpy(s->buf_ptr, buf1, len);
347  s->buf_ptr += len;
348  buf1 += len;
349  size -= len;
350  s->timestamp = s->cur_timestamp + n * 8 / sample_size_bits;
351  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
352  n += (s->buf_ptr - s->buf);
353  }
354  return 0;
355 }
356 
358  const uint8_t *buf1, int size)
359 {
360  RTPMuxContext *s = s1->priv_data;
361  int len, count, max_packet_size;
362 
363  max_packet_size = s->max_payload_size;
364 
365  /* test if we must flush because not enough space */
366  len = (s->buf_ptr - s->buf);
367  if ((len + size) > max_packet_size) {
368  if (len > 4) {
369  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
370  s->buf_ptr = s->buf + 4;
371  }
372  }
373  if (s->buf_ptr == s->buf + 4) {
374  s->timestamp = s->cur_timestamp;
375  }
376 
377  /* add the packet */
378  if (size > max_packet_size) {
379  /* big packet: fragment */
380  count = 0;
381  while (size > 0) {
382  len = max_packet_size - 4;
383  if (len > size)
384  len = size;
385  /* build fragmented packet */
386  s->buf[0] = 0;
387  s->buf[1] = 0;
388  s->buf[2] = count >> 8;
389  s->buf[3] = count;
390  memcpy(s->buf + 4, buf1, len);
391  ff_rtp_send_data(s1, s->buf, len + 4, 0);
392  size -= len;
393  buf1 += len;
394  count += len;
395  }
396  } else {
397  if (s->buf_ptr == s->buf + 4) {
398  /* no fragmentation possible */
399  s->buf[0] = 0;
400  s->buf[1] = 0;
401  s->buf[2] = 0;
402  s->buf[3] = 0;
403  }
404  memcpy(s->buf_ptr, buf1, size);
405  s->buf_ptr += size;
406  }
407 }
408 
410  const uint8_t *buf1, int size)
411 {
412  RTPMuxContext *s = s1->priv_data;
413  int len, max_packet_size;
414 
415  max_packet_size = s->max_payload_size;
416 
417  while (size > 0) {
418  len = max_packet_size;
419  if (len > size)
420  len = size;
421 
422  s->timestamp = s->cur_timestamp;
423  ff_rtp_send_data(s1, buf1, len, (len == size));
424 
425  buf1 += len;
426  size -= len;
427  }
428 }
429 
430 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
432  const uint8_t *buf1, int size)
433 {
434  RTPMuxContext *s = s1->priv_data;
435  int len, out_len;
436 
437  while (size >= TS_PACKET_SIZE) {
438  len = s->max_payload_size - (s->buf_ptr - s->buf);
439  if (len > size)
440  len = size;
441  memcpy(s->buf_ptr, buf1, len);
442  buf1 += len;
443  size -= len;
444  s->buf_ptr += len;
445 
446  out_len = s->buf_ptr - s->buf;
447  if (out_len >= s->max_payload_size) {
448  ff_rtp_send_data(s1, s->buf, out_len, 0);
449  s->buf_ptr = s->buf;
450  }
451  }
452 }
453 
454 static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
455 {
456  RTPMuxContext *s = s1->priv_data;
457  AVStream *st = s1->streams[0];
458  int frame_duration = av_get_audio_frame_duration(st->codec, 0);
459  int frame_size = st->codec->block_align;
460  int frames = size / frame_size;
461 
462  while (frames > 0) {
463  int n = FFMIN(s->max_frames_per_packet - s->num_frames, frames);
464 
465  if (!s->num_frames) {
466  s->buf_ptr = s->buf;
467  s->timestamp = s->cur_timestamp;
468  }
469  memcpy(s->buf_ptr, buf, n * frame_size);
470  frames -= n;
471  s->num_frames += n;
472  s->buf_ptr += n * frame_size;
473  buf += n * frame_size;
474  s->cur_timestamp += n * frame_duration;
475 
476  if (s->num_frames == s->max_frames_per_packet) {
477  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
478  s->num_frames = 0;
479  }
480  }
481  return 0;
482 }
483 
485 {
486  RTPMuxContext *s = s1->priv_data;
487  AVStream *st = s1->streams[0];
488  int rtcp_bytes;
489  int size= pkt->size;
490 
491  av_dlog(s1, "%d: write len=%d\n", pkt->stream_index, size);
492 
493  rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
495  if ((s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
496  (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) &&
497  !(s->flags & FF_RTP_FLAG_SKIP_RTCP)) {
498  rtcp_send_sr(s1, ff_ntp_time());
500  s->first_packet = 0;
501  }
502  s->cur_timestamp = s->base_timestamp + pkt->pts;
503 
504  switch(st->codec->codec_id) {
507  case AV_CODEC_ID_PCM_U8:
508  case AV_CODEC_ID_PCM_S8:
509  return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
514  return rtp_send_samples(s1, pkt->data, size, 16 * st->codec->channels);
516  /* The actual sample size is half a byte per sample, but since the
517  * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
518  * the correct parameter for send_samples_bits is 8 bits per stream
519  * clock. */
520  return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
522  return rtp_send_samples(s1, pkt->data, size,
524  case AV_CODEC_ID_MP2:
525  case AV_CODEC_ID_MP3:
526  rtp_send_mpegaudio(s1, pkt->data, size);
527  break;
530  ff_rtp_send_mpegvideo(s1, pkt->data, size);
531  break;
532  case AV_CODEC_ID_AAC:
533  if (s->flags & FF_RTP_FLAG_MP4A_LATM)
534  ff_rtp_send_latm(s1, pkt->data, size);
535  else
536  ff_rtp_send_aac(s1, pkt->data, size);
537  break;
538  case AV_CODEC_ID_AMR_NB:
539  case AV_CODEC_ID_AMR_WB:
540  ff_rtp_send_amr(s1, pkt->data, size);
541  break;
542  case AV_CODEC_ID_MPEG2TS:
543  rtp_send_mpegts_raw(s1, pkt->data, size);
544  break;
545  case AV_CODEC_ID_H264:
546  ff_rtp_send_h264(s1, pkt->data, size);
547  break;
548  case AV_CODEC_ID_H263:
549  if (s->flags & FF_RTP_FLAG_RFC2190) {
550  int mb_info_size = 0;
551  const uint8_t *mb_info =
553  &mb_info_size);
554  ff_rtp_send_h263_rfc2190(s1, pkt->data, size, mb_info, mb_info_size);
555  break;
556  }
557  /* Fallthrough */
558  case AV_CODEC_ID_H263P:
559  ff_rtp_send_h263(s1, pkt->data, size);
560  break;
561  case AV_CODEC_ID_VORBIS:
562  case AV_CODEC_ID_THEORA:
563  ff_rtp_send_xiph(s1, pkt->data, size);
564  break;
565  case AV_CODEC_ID_VP8:
566  ff_rtp_send_vp8(s1, pkt->data, size);
567  break;
568  case AV_CODEC_ID_ILBC:
569  rtp_send_ilbc(s1, pkt->data, size);
570  break;
571  case AV_CODEC_ID_MJPEG:
572  ff_rtp_send_jpeg(s1, pkt->data, size);
573  break;
574  case AV_CODEC_ID_OPUS:
575  if (size > s->max_payload_size) {
576  av_log(s1, AV_LOG_ERROR,
577  "Packet size %d too large for max RTP payload size %d\n",
578  size, s->max_payload_size);
579  return AVERROR(EINVAL);
580  }
581  /* Intentional fallthrough */
582  default:
583  /* better than nothing : send the codec raw data */
584  rtp_send_raw(s1, pkt->data, size);
585  break;
586  }
587  return 0;
588 }
589 
591 {
592  RTPMuxContext *s = s1->priv_data;
593 
594  av_freep(&s->buf);
595 
596  return 0;
597 }
598 
600  .name = "rtp",
601  .long_name = NULL_IF_CONFIG_SMALL("RTP output"),
602  .priv_data_size = sizeof(RTPMuxContext),
603  .audio_codec = AV_CODEC_ID_PCM_MULAW,
604  .video_codec = AV_CODEC_ID_MPEG4,
608  .priv_class = &rtp_muxer_class,
609 };