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