FFmpeg: libavformat/rtpenc.c Source File

FFmpeg
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[] = {
32  FF_RTP_FLAG_OPTS(RTPMuxContext, flags),
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:
56  case AV_CODEC_ID_HEVC:
57  case AV_CODEC_ID_MPEG1VIDEO:
58  case AV_CODEC_ID_MPEG2VIDEO:
59  case AV_CODEC_ID_MPEG4:
60  case AV_CODEC_ID_AAC:
61  case AV_CODEC_ID_MP2:
62  case AV_CODEC_ID_MP3:
63  case AV_CODEC_ID_PCM_ALAW:
64  case AV_CODEC_ID_PCM_MULAW:
65  case AV_CODEC_ID_PCM_S8:
66  case AV_CODEC_ID_PCM_S16BE:
67  case AV_CODEC_ID_PCM_S16LE:
68  case AV_CODEC_ID_PCM_U16BE:
69  case AV_CODEC_ID_PCM_U16LE:
70  case AV_CODEC_ID_PCM_U8:
71  case AV_CODEC_ID_MPEG2TS:
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:
77  case AV_CODEC_ID_ADPCM_G722:
78  case AV_CODEC_ID_ADPCM_G726:
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 
89  static int rtp_write_header(AVFormatContext *s1)
90 {
91  RTPMuxContext *s = s1->priv_data;
92  int n, ret = AVERROR(EINVAL);
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 
117  s->base_timestamp = av_get_random_seed();
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;
123  s->first_rtcp_ntp_time = ff_ntp_time();
124  if (s1->start_time_realtime != 0 && s1->start_time_realtime != AV_NOPTS_VALUE)
125  /* Round the NTP time to whole milliseconds. */
126  s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
127  NTP_OFFSET_US;
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 (s1->flags & AVFMT_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) {
151  return AVERROR(ENOMEM);
152  }
153  s->max_payload_size = s1->packet_size - 12;
154 
155  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
156  avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
157  } else {
158  avpriv_set_pts_info(st, 32, 1, 90000);
159  }
160  s->buf_ptr = s->buf;
161  switch(st->codec->codec_id) {
162  case AV_CODEC_ID_MP2:
163  case AV_CODEC_ID_MP3:
164  s->buf_ptr = s->buf + 4;
165  avpriv_set_pts_info(st, 32, 1, 90000);
166  break;
167  case AV_CODEC_ID_MPEG1VIDEO:
168  case AV_CODEC_ID_MPEG2VIDEO:
169  break;
170  case AV_CODEC_ID_MPEG2TS:
171  n = s->max_payload_size / TS_PACKET_SIZE;
172  if (n < 1)
173  n = 1;
174  s->max_payload_size = n * TS_PACKET_SIZE;
175  break;
176  case AV_CODEC_ID_H261:
177  if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
178  av_log(s, AV_LOG_ERROR,
179  "Packetizing H261 is experimental and produces incorrect "
180  "packetization for cases where GOBs don't fit into packets "
181  "(even though most receivers may handle it just fine). "
182  "Please set -f_strict experimental in order to enable it.\n");
183  ret = AVERROR_EXPERIMENTAL;
184  goto fail;
185  }
186  break;
187  case AV_CODEC_ID_H264:
188  /* check for H.264 MP4 syntax */
189  if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
190  s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
191  }
192  break;
193  case AV_CODEC_ID_HEVC:
194  /* Only check for the standardized hvcC version of extradata, keeping
195  * things simple and similar to the avcC/H264 case above, instead
196  * of trying to handle the pre-standardization versions (as in
197  * libavcodec/hevc.c). */
198  if (st->codec->extradata_size > 21 && st->codec->extradata[0] == 1) {
199  s->nal_length_size = (st->codec->extradata[21] & 0x03) + 1;
200  }
201  break;
202  case AV_CODEC_ID_VORBIS:
203  case AV_CODEC_ID_THEORA:
204  s->max_frames_per_packet = 15;
205  break;
206  case AV_CODEC_ID_ADPCM_G722:
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  s->max_frames_per_packet = s->max_payload_size / st->codec->block_align;
227  break;
228  case AV_CODEC_ID_AMR_NB:
229  case AV_CODEC_ID_AMR_WB:
230  s->max_frames_per_packet = 50;
231  if (st->codec->codec_id == AV_CODEC_ID_AMR_NB)
232  n = 31;
233  else
234  n = 61;
235  /* max_header_toc_size + the largest AMR payload must fit */
236  if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
237  av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
238  goto fail;
239  }
240  if (st->codec->channels != 1) {
241  av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
242  goto fail;
243  }
244  break;
245  case AV_CODEC_ID_AAC:
246  s->max_frames_per_packet = 50;
247  break;
248  default:
249  break;
250  }
251 
252  return 0;
253 
254 fail:
255  av_freep(&s->buf);
256  return ret;
257 }
258 
259 /* send an rtcp sender report packet */
260  static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time, int bye)
261 {
262  RTPMuxContext *s = s1->priv_data;
263  uint32_t rtp_ts;
264 
265  av_log(s1, AV_LOG_TRACE, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
266 
267  s->last_rtcp_ntp_time = ntp_time;
268  rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
269  s1->streams[0]->time_base) + s->base_timestamp;
270  avio_w8(s1->pb, RTP_VERSION << 6);
271  avio_w8(s1->pb, RTCP_SR);
272  avio_wb16(s1->pb, 6); /* length in words - 1 */
273  avio_wb32(s1->pb, s->ssrc);
274  avio_wb64(s1->pb, NTP_TO_RTP_FORMAT(ntp_time));
275  avio_wb32(s1->pb, rtp_ts);
276  avio_wb32(s1->pb, s->packet_count);
277  avio_wb32(s1->pb, s->octet_count);
278 
279  if (s->cname) {
280  int len = FFMIN(strlen(s->cname), 255);
281  avio_w8(s1->pb, (RTP_VERSION << 6) + 1);
282  avio_w8(s1->pb, RTCP_SDES);
283  avio_wb16(s1->pb, (7 + len + 3) / 4); /* length in words - 1 */
284 
285  avio_wb32(s1->pb, s->ssrc);
286  avio_w8(s1->pb, 0x01); /* CNAME */
287  avio_w8(s1->pb, len);
288  avio_write(s1->pb, s->cname, len);
289  avio_w8(s1->pb, 0); /* END */
290  for (len = (7 + len) % 4; len % 4; len++)
291  avio_w8(s1->pb, 0);
292  }
293 
294  if (bye) {
295  avio_w8(s1->pb, (RTP_VERSION << 6) | 1);
296  avio_w8(s1->pb, RTCP_BYE);
297  avio_wb16(s1->pb, 1); /* length in words - 1 */
298  avio_wb32(s1->pb, s->ssrc);
299  }
300 
301  avio_flush(s1->pb);
302 }
303 
304 /* send an rtp packet. sequence number is incremented, but the caller
305  must update the timestamp itself */
306  void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
307 {
308  RTPMuxContext *s = s1->priv_data;
309 
310  av_log(s1, AV_LOG_TRACE, "rtp_send_data size=%d\n", len);
311 
312  /* build the RTP header */
313  avio_w8(s1->pb, RTP_VERSION << 6);
314  avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
315  avio_wb16(s1->pb, s->seq);
316  avio_wb32(s1->pb, s->timestamp);
317  avio_wb32(s1->pb, s->ssrc);
318 
319  avio_write(s1->pb, buf1, len);
320  avio_flush(s1->pb);
321 
322  s->seq = (s->seq + 1) & 0xffff;
323  s->octet_count += len;
324  s->packet_count++;
325 }
326 
327 /* send an integer number of samples and compute time stamp and fill
328  the rtp send buffer before sending. */
329  static int rtp_send_samples(AVFormatContext *s1,
330  const uint8_t *buf1, int size, int sample_size_bits)
331 {
332  RTPMuxContext *s = s1->priv_data;
333  int len, max_packet_size, n;
334  /* Calculate the number of bytes to get samples aligned on a byte border */
335  int aligned_samples_size = sample_size_bits/av_gcd(sample_size_bits, 8);
336 
337  max_packet_size = (s->max_payload_size / aligned_samples_size) * aligned_samples_size;
338  /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
339  if ((sample_size_bits % 8) == 0 && ((8 * size) % sample_size_bits) != 0)
340  return AVERROR(EINVAL);
341  n = 0;
342  while (size > 0) {
343  s->buf_ptr = s->buf;
344  len = FFMIN(max_packet_size, size);
345 
346  /* copy data */
347  memcpy(s->buf_ptr, buf1, len);
348  s->buf_ptr += len;
349  buf1 += len;
350  size -= len;
351  s->timestamp = s->cur_timestamp + n * 8 / sample_size_bits;
352  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
353  n += (s->buf_ptr - s->buf);
354  }
355  return 0;
356 }
357 
358  static void rtp_send_mpegaudio(AVFormatContext *s1,
359  const uint8_t *buf1, int size)
360 {
361  RTPMuxContext *s = s1->priv_data;
362  int len, count, max_packet_size;
363 
364  max_packet_size = s->max_payload_size;
365 
366  /* test if we must flush because not enough space */
367  len = (s->buf_ptr - s->buf);
368  if ((len + size) > max_packet_size) {
369  if (len > 4) {
370  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
371  s->buf_ptr = s->buf + 4;
372  }
373  }
374  if (s->buf_ptr == s->buf + 4) {
375  s->timestamp = s->cur_timestamp;
376  }
377 
378  /* add the packet */
379  if (size > max_packet_size) {
380  /* big packet: fragment */
381  count = 0;
382  while (size > 0) {
383  len = max_packet_size - 4;
384  if (len > size)
385  len = size;
386  /* build fragmented packet */
387  s->buf[0] = 0;
388  s->buf[1] = 0;
389  s->buf[2] = count >> 8;
390  s->buf[3] = count;
391  memcpy(s->buf + 4, buf1, len);
392  ff_rtp_send_data(s1, s->buf, len + 4, 0);
393  size -= len;
394  buf1 += len;
395  count += len;
396  }
397  } else {
398  if (s->buf_ptr == s->buf + 4) {
399  /* no fragmentation possible */
400  s->buf[0] = 0;
401  s->buf[1] = 0;
402  s->buf[2] = 0;
403  s->buf[3] = 0;
404  }
405  memcpy(s->buf_ptr, buf1, size);
406  s->buf_ptr += size;
407  }
408 }
409 
410  static void rtp_send_raw(AVFormatContext *s1,
411  const uint8_t *buf1, int size)
412 {
413  RTPMuxContext *s = s1->priv_data;
414  int len, max_packet_size;
415 
416  max_packet_size = s->max_payload_size;
417 
418  while (size > 0) {
419  len = max_packet_size;
420  if (len > size)
421  len = size;
422 
423  s->timestamp = s->cur_timestamp;
424  ff_rtp_send_data(s1, buf1, len, (len == size));
425 
426  buf1 += len;
427  size -= len;
428  }
429 }
430 
431 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
432  static void rtp_send_mpegts_raw(AVFormatContext *s1,
433  const uint8_t *buf1, int size)
434 {
435  RTPMuxContext *s = s1->priv_data;
436  int len, out_len;
437 
438  s->timestamp = s->cur_timestamp;
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  if (s->num_frames > 0 &&
466  av_compare_ts(s->cur_timestamp - s->timestamp, st->time_base,
467  s1->max_delay, AV_TIME_BASE_Q) >= 0) {
468  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
469  s->num_frames = 0;
470  }
471 
472  if (!s->num_frames) {
473  s->buf_ptr = s->buf;
474  s->timestamp = s->cur_timestamp;
475  }
476  memcpy(s->buf_ptr, buf, frame_size);
477  frames--;
478  s->num_frames++;
479  s->buf_ptr += frame_size;
480  buf += frame_size;
481  s->cur_timestamp += frame_duration;
482 
483  if (s->num_frames == s->max_frames_per_packet) {
484  ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
485  s->num_frames = 0;
486  }
487  }
488  return 0;
489 }
490 
491  static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
492 {
493  RTPMuxContext *s = s1->priv_data;
494  AVStream *st = s1->streams[0];
495  int rtcp_bytes;
496  int size= pkt->size;
497 
498  av_log(s1, AV_LOG_TRACE, "%d: write len=%d\n", pkt->stream_index, size);
499 
500  rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
501  RTCP_TX_RATIO_DEN;
502  if ((s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
503  (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) &&
504  !(s->flags & FF_RTP_FLAG_SKIP_RTCP)) {
505  rtcp_send_sr(s1, ff_ntp_time(), 0);
506  s->last_octet_count = s->octet_count;
507  s->first_packet = 0;
508  }
509  s->cur_timestamp = s->base_timestamp + pkt->pts;
510 
511  switch(st->codec->codec_id) {
512  case AV_CODEC_ID_PCM_MULAW:
513  case AV_CODEC_ID_PCM_ALAW:
514  case AV_CODEC_ID_PCM_U8:
515  case AV_CODEC_ID_PCM_S8:
516  return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
517  case AV_CODEC_ID_PCM_U16BE:
518  case AV_CODEC_ID_PCM_U16LE:
519  case AV_CODEC_ID_PCM_S16BE:
520  case AV_CODEC_ID_PCM_S16LE:
521  return rtp_send_samples(s1, pkt->data, size, 16 * st->codec->channels);
522  case AV_CODEC_ID_ADPCM_G722:
523  /* The actual sample size is half a byte per sample, but since the
524  * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
525  * the correct parameter for send_samples_bits is 8 bits per stream
526  * clock. */
527  return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
528  case AV_CODEC_ID_ADPCM_G726:
529  return rtp_send_samples(s1, pkt->data, size,
530  st->codec->bits_per_coded_sample * st->codec->channels);
531  case AV_CODEC_ID_MP2:
532  case AV_CODEC_ID_MP3:
533  rtp_send_mpegaudio(s1, pkt->data, size);
534  break;
535  case AV_CODEC_ID_MPEG1VIDEO:
536  case AV_CODEC_ID_MPEG2VIDEO:
537  ff_rtp_send_mpegvideo(s1, pkt->data, size);
538  break;
539  case AV_CODEC_ID_AAC:
540  if (s->flags & FF_RTP_FLAG_MP4A_LATM)
541  ff_rtp_send_latm(s1, pkt->data, size);
542  else
543  ff_rtp_send_aac(s1, pkt->data, size);
544  break;
545  case AV_CODEC_ID_AMR_NB:
546  case AV_CODEC_ID_AMR_WB:
547  ff_rtp_send_amr(s1, pkt->data, size);
548  break;
549  case AV_CODEC_ID_MPEG2TS:
550  rtp_send_mpegts_raw(s1, pkt->data, size);
551  break;
552  case AV_CODEC_ID_H264:
553  ff_rtp_send_h264_hevc(s1, pkt->data, size);
554  break;
555  case AV_CODEC_ID_H261:
556  ff_rtp_send_h261(s1, pkt->data, size);
557  break;
558  case AV_CODEC_ID_H263:
559  if (s->flags & FF_RTP_FLAG_RFC2190) {
560  int mb_info_size = 0;
561  const uint8_t *mb_info =
562  av_packet_get_side_data(pkt, AV_PKT_DATA_H263_MB_INFO,
563  &mb_info_size);
564  ff_rtp_send_h263_rfc2190(s1, pkt->data, size, mb_info, mb_info_size);
565  break;
566  }
567  /* Fallthrough */
568  case AV_CODEC_ID_H263P:
569  ff_rtp_send_h263(s1, pkt->data, size);
570  break;
571  case AV_CODEC_ID_HEVC:
572  ff_rtp_send_h264_hevc(s1, pkt->data, size);
573  break;
574  case AV_CODEC_ID_VORBIS:
575  case AV_CODEC_ID_THEORA:
576  ff_rtp_send_xiph(s1, pkt->data, size);
577  break;
578  case AV_CODEC_ID_VP8:
579  ff_rtp_send_vp8(s1, pkt->data, size);
580  break;
581  case AV_CODEC_ID_ILBC:
582  rtp_send_ilbc(s1, pkt->data, size);
583  break;
584  case AV_CODEC_ID_MJPEG:
585  ff_rtp_send_jpeg(s1, pkt->data, size);
586  break;
587  case AV_CODEC_ID_OPUS:
588  if (size > s->max_payload_size) {
589  av_log(s1, AV_LOG_ERROR,
590  "Packet size %d too large for max RTP payload size %d\n",
591  size, s->max_payload_size);
592  return AVERROR(EINVAL);
593  }
594  /* Intentional fallthrough */
595  default:
596  /* better than nothing : send the codec raw data */
597  rtp_send_raw(s1, pkt->data, size);
598  break;
599  }
600  return 0;
601 }
602 
603  static int rtp_write_trailer(AVFormatContext *s1)
604 {
605  RTPMuxContext *s = s1->priv_data;
606 
607  /* If the caller closes and recreates ->pb, this might actually
608  * be NULL here even if it was successfully allocated at the start. */
609  if (s1->pb && (s->flags & FF_RTP_FLAG_SEND_BYE))
610  rtcp_send_sr(s1, ff_ntp_time(), 1);
611  av_freep(&s->buf);
612 
613  return 0;
614 }
615 
616  AVOutputFormat ff_rtp_muxer = {
617  .name = "rtp",
618  .long_name = NULL_IF_CONFIG_SMALL("RTP output"),
619  .priv_data_size = sizeof(RTPMuxContext),
620  .audio_codec = AV_CODEC_ID_PCM_MULAW,
621  .video_codec = AV_CODEC_ID_MPEG4,
622  .write_header = rtp_write_header,
623  .write_packet = rtp_write_packet,
624  .write_trailer = rtp_write_trailer,
625  .priv_class = &rtp_muxer_class,
626  .flags = AVFMT_TS_NONSTRICT,
627 };
FF_COMPLIANCE_EXPERIMENTAL
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2746
AVFormatContext::packet_size
unsigned int packet_size
Definition: avformat.h:1418
avio_wb64
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:418
NULL
#define NULL
Definition: coverity.c:32
s
const char * s
Definition: avisynth_c.h:631
AVFormatContext::start_time_realtime
int64_t start_time_realtime
Start time of the stream in real world time, in microseconds since the Unix epoch (00:00 1st January ...
Definition: avformat.h:1541
AVOption
AVOption.
Definition: opt.h:245
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
avpriv_set_pts_info
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:4149
RTPMuxContext::payload_type
int payload_type
Definition: rtpenc.h:31
rtp_send_samples
static int rtp_send_samples(AVFormatContext *s1, const uint8_t *buf1, int size, int sample_size_bits)
Definition: rtpenc.c:329
NTP_OFFSET_US
#define NTP_OFFSET_US
Definition: internal.h:187
rtp_write_packet
static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: rtpenc.c:491
RTP_VERSION
#define RTP_VERSION
Definition: rtp.h:78
RTPMuxContext::last_rtcp_ntp_time
int64_t last_rtcp_ntp_time
Definition: rtpenc.h:42
AVPacket::size
int size
Definition: avcodec.h:1468
RTCP_TX_RATIO_NUM
#define RTCP_TX_RATIO_NUM
Definition: rtp.h:82
RTPMuxContext::last_octet_count
unsigned int last_octet_count
Definition: rtpenc.h:46
options
static const AVOption options[]
Definition: rtpenc.c:31
ff_rtp_send_amr
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
RTPMuxContext::max_payload_size
int max_payload_size
Definition: rtpenc.h:38
frames
int frames
Definition: movenc-test.c:65
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
RTCP_TX_RATIO_DEN
#define RTCP_TX_RATIO_DEN
Definition: rtp.h:83
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: avcodec.h:1289
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2324
AVFormatContext::strict_std_compliance
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1596
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:495
RTPMuxContext::nal_length_size
int nal_length_size
Number of bytes used for H.264 NAL length, if the MP4 syntax is used (1, 2 or 4)
Definition: rtpenc.h:58
FF_RTP_FLAG_MP4A_LATM
#define FF_RTP_FLAG_MP4A_LATM
Definition: rtpenc.h:68
AVFormatContext
Format I/O context.
Definition: avformat.h:1314
RTP_PT_PRIVATE
#define RTP_PT_PRIVATE
Definition: rtp.h:77
AVClass::class_name
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
uint8_t
uint8_t
Definition: audio_convert.c:194
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
opt.h
AVOptions.
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
rtp_muxer_class
static const AVClass rtp_muxer_class
Definition: rtpenc.c:40
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:884
RTPMuxContext::max_frames_per_packet
int max_frames_per_packet
Definition: rtpenc.h:52
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
ff_rtp_send_h263
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
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
RTPMuxContext::octet_count
unsigned int octet_count
Definition: rtpenc.h:45
TS_PACKET_SIZE
#define TS_PACKET_SIZE
Definition: mpegts.h:29
rtp_write_header
static int rtp_write_header(AVFormatContext *s1)
Definition: rtpenc.c:89
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1425
AVPacket::data
uint8_t * data
Definition: avcodec.h:1467
RTCP_SDES
Definition: rtp.h:99
RTPMuxContext::buf
uint8_t * buf
Definition: rtpenc.h:49
size
ptrdiff_t size
Definition: opengl_enc.c:101
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2917
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:182
AVFMT_FLAG_BITEXACT
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1442
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:275
m
unsigned m
Definition: audioconvert.c:187
FF_RTP_FLAG_RFC2190
#define FF_RTP_FLAG_RFC2190
Definition: rtpenc.h:69
ff_ntp_time
uint64_t ff_ntp_time(void)
Get the current time since NTP epoch in microseconds.
Definition: utils.c:3969
ff_rtp_get_payload_type
int ff_rtp_get_payload_type(AVFormatContext *fmt, AVCodecContext *codec, int idx)
Return the payload type for a given stream used in the given format context.
Definition: rtp.c:90
av_rescale_q
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
AVIOContext::max_packet_size
int max_packet_size
Definition: avio.h:188
RTPMuxContext::ssrc
uint32_t ssrc
Definition: rtpenc.h:32
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:101
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
ff_rtp_send_aac
void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size)
Definition: rtpenc_aac.c:27
av_default_item_name
av_default_item_name
Definition: libopenh264enc.c:68
ff_rtp_send_jpeg
void ff_rtp_send_jpeg(AVFormatContext *s1, const uint8_t *buff, int size)
Definition: rtpenc_jpeg.c:28
AVERROR
#define AVERROR(e)
Definition: error.h:43
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:420
ff_rtp_send_latm
void ff_rtp_send_latm(AVFormatContext *s1, const uint8_t *buff, int size)
Definition: rtpenc_latm.c:25
ff_rtp_send_vp8
void ff_rtp_send_vp8(AVFormatContext *s1, const uint8_t *buff, int size)
Definition: rtpenc_vp8.c:26
av_gcd
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of a and b.
Definition: mathematics.c:37
count
GLsizei count
Definition: opengl_enc.c:109
FF_RTP_FLAG_SKIP_RTCP
#define FF_RTP_FLAG_SKIP_RTCP
Definition: rtpenc.h:70
fail
#define fail()
Definition: checkasm.h:80
av_compare_ts
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:147
rtp_send_mpegts_raw
static void rtp_send_mpegts_raw(AVFormatContext *s1, const uint8_t *buf1, int size)
Definition: rtpenc.c:432
AVStream::codec
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1370
ff_rtp_send_xiph
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
avio_flush
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:202
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
ff_rtp_send_data
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
Definition: rtpenc.c:306
ff_rtp_send_h261
void ff_rtp_send_h261(AVFormatContext *s1, const uint8_t *buf1, int size)
Definition: rtpenc_h261.c:39
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
ff_rtp_send_h264_hevc
void ff_rtp_send_h264_hevc(AVFormatContext *s1, const uint8_t *buf1, int size)
Definition: rtpenc_h264_hevc.c:180
AVOutputFormat::name
const char * name
Definition: avformat.h:523
rtp_send_ilbc
static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
Definition: rtpenc.c:456
n
int n
Definition: avisynth_c.h:547
RTPMuxContext::seq
int seq
Definition: rtpenc.h:34
ff_rtp_send_mpegvideo
void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size)
Definition: rtpenc_mpv.c:29
rtp_send_mpegaudio
static void rtp_send_mpegaudio(AVFormatContext *s1, const uint8_t *buf1, int size)
Definition: rtpenc.c:358
AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:106
AVERROR_EXPERIMENTAL
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it...
Definition: error.h:72
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:2648
AVStream
Stream structure.
Definition: avformat.h:877
RTPMuxContext::first_rtcp_ntp_time
int64_t first_rtcp_ntp_time
Definition: rtpenc.h:43
RTPMuxContext::cur_timestamp
uint32_t cur_timestamp
Definition: rtpenc.h:37
ff_rtp_muxer
AVOutputFormat ff_rtp_muxer
Definition: rtpenc.c:616
frame_size
int frame_size
Definition: mxfenc.c:1821
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:1540
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:1549
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:252
RTCP_BYE
Definition: rtp.h:100
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:2287
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
FF_RTP_FLAG_OPTS
#define FF_RTP_FLAG_OPTS(ctx, fieldname)
Definition: rtpenc.h:74
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:160
is_supported
static int is_supported(enum AVCodecID id)
Definition: rtpenc.c:49
RTPMuxContext::first_packet
int first_packet
Definition: rtpenc.h:47
buf
void * buf
Definition: avisynth_c.h:553
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:1648
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
AVRational
rational number numerator/denominator
Definition: rational.h:43
RTPMuxContext::flags
int flags
Definition: rtpenc.h:61
s1
#define s1
Definition: regdef.h:38
RTPMuxContext::num_frames
int num_frames
Definition: rtpenc.h:39
RTPMuxContext::base_timestamp
uint32_t base_timestamp
Definition: rtpenc.h:36
av_get_audio_frame_duration
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
Return audio frame duration.
Definition: utils.c:3024
RTPMuxContext::buf_ptr
uint8_t * buf_ptr
Definition: rtpenc.h:50
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:430
flags
static int flags
Definition: cpu.c:47
NTP_TO_RTP_FORMAT
#define NTP_TO_RTP_FORMAT(x)
Definition: rtp.h:113
avformat.h
Main libavformat public API header.
rtp_send_raw
static void rtp_send_raw(AVFormatContext *s1, const uint8_t *buf1, int size)
Definition: rtpenc.c:410
rtcp_send_sr
static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time, int bye)
Definition: rtpenc.c:260
RTCP_SR_SIZE
#define RTCP_SR_SIZE
Definition: rtpenc.c:47
RTCP_SR
Definition: rtp.h:97
RTPMuxContext::packet_count
unsigned int packet_count
Definition: rtpenc.h:44
AV_CODEC_ID_MPEG2TS
FAKE codec to indicate a raw MPEG-2 TS stream (only used by libavformat)
Definition: avcodec.h:548
RTPMuxContext::timestamp
uint32_t timestamp
Definition: rtpenc.h:35
len
int len
Definition: vorbis_enc_data.h:452
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:2288
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1342
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:497
rtp_write_trailer
static int rtp_write_trailer(AVFormatContext *s1)
Definition: rtpenc.c:603
ff_rtp_send_h263_rfc2190
void ff_rtp_send_h263_rfc2190(AVFormatContext *s1, const uint8_t *buf1, int size, const uint8_t *mb_info, int mb_info_size)
Definition: rtpenc_h263_rfc2190.c:101
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:332
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_packet_get_side_data
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:320
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:109
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1469
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:919
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1444
write_packet
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
FF_RTP_FLAG_SEND_BYE
#define FF_RTP_FLAG_SEND_BYE
Definition: rtpenc.h:72
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1460
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240

Generated on Mon Feb 15 2016 15:20:47 for FFmpeg by   doxygen 1.8.6

AltStyle によって変換されたページ (->オリジナル) /