3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/mathematics.h"
26 #include "libavutil/random_seed.h"
27 #include "libavutil/opt.h"
33 static const AVOption options[] = {
34 FF_RTP_FLAG_OPTS(RTPMuxContext, flags),
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 },
40 static const AVClass rtp_muxer_class = {
41 .class_name = "RTP muxer",
42 .item_name = av_default_item_name,
44 .version = LIBAVUTIL_VERSION_INT,
47 #define RTCP_SR_SIZE 28
49 static int is_supported(enum AVCodecID id)
52 case AV_CODEC_ID_H263:
53 case AV_CODEC_ID_H263P:
54 case AV_CODEC_ID_H264:
55 case AV_CODEC_ID_MPEG1VIDEO:
56 case AV_CODEC_ID_MPEG2VIDEO:
57 case AV_CODEC_ID_MPEG4:
61 case AV_CODEC_ID_PCM_ALAW:
62 case AV_CODEC_ID_PCM_MULAW:
63 case AV_CODEC_ID_PCM_S8:
64 case AV_CODEC_ID_PCM_S16BE:
65 case AV_CODEC_ID_PCM_S16LE:
66 case AV_CODEC_ID_PCM_U16BE:
67 case AV_CODEC_ID_PCM_U16LE:
68 case AV_CODEC_ID_PCM_U8:
69 case AV_CODEC_ID_MPEG2TS:
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:
75 case AV_CODEC_ID_ADPCM_G722:
76 case AV_CODEC_ID_ADPCM_G726:
77 case AV_CODEC_ID_ILBC:
78 case AV_CODEC_ID_MJPEG:
79 case AV_CODEC_ID_SPEEX:
86 static int rtp_write_header(AVFormatContext *s1)
88 RTPMuxContext *s = s1->priv_data;
92 if (s1->nb_streams != 1) {
93 av_log(s1, AV_LOG_ERROR, "Only one stream supported in the RTP muxer\n");
94 return AVERROR(EINVAL);
97 if (!is_supported(st->codec->codec_id)) {
98 av_log(s1, AV_LOG_ERROR, "Unsupported codec %x\n", st->codec->codec_id);
103 if (s->payload_type < 0)
104 s->payload_type = ff_rtp_get_payload_type(s1, st->codec);
105 s->base_timestamp = av_get_random_seed();
106 s->timestamp = s->base_timestamp;
107 s->cur_timestamp = 0;
109 s->ssrc = av_get_random_seed();
111 s->first_rtcp_ntp_time = ff_ntp_time();
112 if (s1->start_time_realtime)
113 /* Round the NTP time to whole milliseconds. */
114 s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
117 if (s1->packet_size) {
118 if (s1->pb->max_packet_size)
119 s1->packet_size = FFMIN(s1->packet_size,
120 s1->pb->max_packet_size);
122 s1->packet_size = s1->pb->max_packet_size;
123 if (s1->packet_size <= 12) {
124 av_log(s1, AV_LOG_ERROR, "Max packet size %d too low\n", s1->packet_size);
127 s->buf = av_malloc(s1->packet_size);
128 if (s->buf == NULL) {
129 return AVERROR(ENOMEM);
131 s->max_payload_size = s1->packet_size - 12;
133 s->max_frames_per_packet = 0;
134 if (s1->max_delay > 0) {
135 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
136 int frame_size = av_get_audio_frame_duration(st->codec, 0);
138 frame_size = st->codec->frame_size;
139 if (frame_size == 0) {
140 av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
142 s->max_frames_per_packet =
143 av_rescale_q_rnd(s1->max_delay,
145 (AVRational){ frame_size, st->codec->sample_rate },
149 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
150 /* FIXME: We should round down here... */
151 s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
155 avpriv_set_pts_info(st, 32, 1, 90000);
156 switch(st->codec->codec_id) {
157 case AV_CODEC_ID_MP2:
158 case AV_CODEC_ID_MP3:
159 s->buf_ptr = s->buf + 4;
161 case AV_CODEC_ID_MPEG1VIDEO:
162 case AV_CODEC_ID_MPEG2VIDEO:
164 case AV_CODEC_ID_MPEG2TS:
165 n = s->max_payload_size / TS_PACKET_SIZE;
168 s->max_payload_size = n * TS_PACKET_SIZE;
171 case AV_CODEC_ID_H264:
172 /* check for H.264 MP4 syntax */
173 if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
174 s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
177 case AV_CODEC_ID_VORBIS:
178 case AV_CODEC_ID_THEORA:
179 if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
180 s->max_frames_per_packet = av_clip(s->max_frames_per_packet, 1, 15);
181 s->max_payload_size -= 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
184 case AV_CODEC_ID_VP8:
185 av_log(s1, AV_LOG_ERROR, "RTP VP8 payload implementation is "
186 "incompatible with the latest spec drafts.\n");
188 case AV_CODEC_ID_ADPCM_G722:
189 /* Due to a historical error, the clock rate for G722 in RTP is
190 * 8000, even if the sample rate is 16000. See RFC 3551. */
191 avpriv_set_pts_info(st, 32, 1, 8000);
193 case AV_CODEC_ID_ILBC:
194 if (st->codec->block_align != 38 && st->codec->block_align != 50) {
195 av_log(s1, AV_LOG_ERROR, "Incorrect iLBC block size specified\n");
198 if (!s->max_frames_per_packet)
199 s->max_frames_per_packet = 1;
200 s->max_frames_per_packet = FFMIN(s->max_frames_per_packet,
201 s->max_payload_size / st->codec->block_align);
203 case AV_CODEC_ID_AMR_NB:
204 case AV_CODEC_ID_AMR_WB:
205 if (!s->max_frames_per_packet)
206 s->max_frames_per_packet = 12;
207 if (st->codec->codec_id == AV_CODEC_ID_AMR_NB)
211 /* max_header_toc_size + the largest AMR payload must fit */
212 if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
213 av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
216 if (st->codec->channels != 1) {
217 av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
220 case AV_CODEC_ID_AAC:
224 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
225 avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
235 return AVERROR(EINVAL);
238 /* send an rtcp sender report packet */
239 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
241 RTPMuxContext *s = s1->priv_data;
244 av_dlog(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
246 s->last_rtcp_ntp_time = ntp_time;
247 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
248 s1->streams[0]->time_base) + s->base_timestamp;
249 avio_w8(s1->pb, (RTP_VERSION << 6));
250 avio_w8(s1->pb, RTCP_SR);
251 avio_wb16(s1->pb, 6); /* length in words - 1 */
252 avio_wb32(s1->pb, s->ssrc);
253 avio_wb32(s1->pb, ntp_time / 1000000);
254 avio_wb32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
255 avio_wb32(s1->pb, rtp_ts);
256 avio_wb32(s1->pb, s->packet_count);
257 avio_wb32(s1->pb, s->octet_count);
261 /* send an rtp packet. sequence number is incremented, but the caller
262 must update the timestamp itself */
263 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
265 RTPMuxContext *s = s1->priv_data;
267 av_dlog(s1, "rtp_send_data size=%d\n", len);
269 /* build the RTP header */
270 avio_w8(s1->pb, (RTP_VERSION << 6));
271 avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
272 avio_wb16(s1->pb, s->seq);
273 avio_wb32(s1->pb, s->timestamp);
274 avio_wb32(s1->pb, s->ssrc);
276 avio_write(s1->pb, buf1, len);
280 s->octet_count += len;
284 /* send an integer number of samples and compute time stamp and fill
285 the rtp send buffer before sending. */
286 static int rtp_send_samples(AVFormatContext *s1,
287 const uint8_t *buf1, int size, int sample_size_bits)
289 RTPMuxContext *s = s1->priv_data;
290 int len, max_packet_size, n;
291 /* Calculate the number of bytes to get samples aligned on a byte border */
292 int aligned_samples_size = sample_size_bits/av_gcd(sample_size_bits, 8);
294 max_packet_size = (s->max_payload_size / aligned_samples_size) * aligned_samples_size;
295 /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
296 if ((sample_size_bits % 8) == 0 && ((8 * size) % sample_size_bits) != 0)
297 return AVERROR(EINVAL);
301 len = FFMIN(max_packet_size, size);
304 memcpy(s->buf_ptr, buf1, len);
308 s->timestamp = s->cur_timestamp + n * 8 / sample_size_bits;
309 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
310 n += (s->buf_ptr - s->buf);
315 static void rtp_send_mpegaudio(AVFormatContext *s1,
316 const uint8_t *buf1, int size)
318 RTPMuxContext *s = s1->priv_data;
319 int len, count, max_packet_size;
321 max_packet_size = s->max_payload_size;
323 /* test if we must flush because not enough space */
324 len = (s->buf_ptr - s->buf);
325 if ((len + size) > max_packet_size) {
327 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
328 s->buf_ptr = s->buf + 4;
331 if (s->buf_ptr == s->buf + 4) {
332 s->timestamp = s->cur_timestamp;
336 if (size > max_packet_size) {
337 /* big packet: fragment */
340 len = max_packet_size - 4;
343 /* build fragmented packet */
346 s->buf[2] = count >> 8;
348 memcpy(s->buf + 4, buf1, len);
349 ff_rtp_send_data(s1, s->buf, len + 4, 0);
355 if (s->buf_ptr == s->buf + 4) {
356 /* no fragmentation possible */
362 memcpy(s->buf_ptr, buf1, size);
367 static void rtp_send_raw(AVFormatContext *s1,
368 const uint8_t *buf1, int size)
370 RTPMuxContext *s = s1->priv_data;
371 int len, max_packet_size;
373 max_packet_size = s->max_payload_size;
376 len = max_packet_size;
380 s->timestamp = s->cur_timestamp;
381 ff_rtp_send_data(s1, buf1, len, (len == size));
388 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
389 static void rtp_send_mpegts_raw(AVFormatContext *s1,
390 const uint8_t *buf1, int size)
392 RTPMuxContext *s = s1->priv_data;
395 while (size >= TS_PACKET_SIZE) {
396 len = s->max_payload_size - (s->buf_ptr - s->buf);
399 memcpy(s->buf_ptr, buf1, len);
404 out_len = s->buf_ptr - s->buf;
405 if (out_len >= s->max_payload_size) {
406 ff_rtp_send_data(s1, s->buf, out_len, 0);
412 static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
414 RTPMuxContext *s = s1->priv_data;
415 AVStream *st = s1->streams[0];
416 int frame_duration = av_get_audio_frame_duration(st->codec, 0);
417 int frame_size = st->codec->block_align;
418 int frames = size / frame_size;
421 int n = FFMIN(s->max_frames_per_packet - s->num_frames, frames);
423 if (!s->num_frames) {
425 s->timestamp = s->cur_timestamp;
427 memcpy(s->buf_ptr, buf, n * frame_size);
430 s->buf_ptr += n * frame_size;
431 buf += n * frame_size;
432 s->cur_timestamp += n * frame_duration;
434 if (s->num_frames == s->max_frames_per_packet) {
435 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
442 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
444 RTPMuxContext *s = s1->priv_data;
445 AVStream *st = s1->streams[0];
449 av_dlog(s1, "%d: write len=%d\n", pkt->stream_index, size);
451 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
453 if ((s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
454 (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) &&
455 !(s->flags & FF_RTP_FLAG_SKIP_RTCP)) {
456 rtcp_send_sr(s1, ff_ntp_time());
457 s->last_octet_count = s->octet_count;
460 s->cur_timestamp = s->base_timestamp + pkt->pts;
462 switch(st->codec->codec_id) {
463 case AV_CODEC_ID_PCM_MULAW:
464 case AV_CODEC_ID_PCM_ALAW:
465 case AV_CODEC_ID_PCM_U8:
466 case AV_CODEC_ID_PCM_S8:
467 return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
468 case AV_CODEC_ID_PCM_U16BE:
469 case AV_CODEC_ID_PCM_U16LE:
470 case AV_CODEC_ID_PCM_S16BE:
471 case AV_CODEC_ID_PCM_S16LE:
472 return rtp_send_samples(s1, pkt->data, size, 16 * st->codec->channels);
473 case AV_CODEC_ID_ADPCM_G722:
474 /* The actual sample size is half a byte per sample, but since the
475 * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
476 * the correct parameter for send_samples_bits is 8 bits per stream
478 return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
479 case AV_CODEC_ID_ADPCM_G726:
480 return rtp_send_samples(s1, pkt->data, size,
481 st->codec->bits_per_coded_sample * st->codec->channels);
482 case AV_CODEC_ID_MP2:
483 case AV_CODEC_ID_MP3:
484 rtp_send_mpegaudio(s1, pkt->data, size);
486 case AV_CODEC_ID_MPEG1VIDEO:
487 case AV_CODEC_ID_MPEG2VIDEO:
488 ff_rtp_send_mpegvideo(s1, pkt->data, size);
490 case AV_CODEC_ID_AAC:
491 if (s->flags & FF_RTP_FLAG_MP4A_LATM)
492 ff_rtp_send_latm(s1, pkt->data, size);
494 ff_rtp_send_aac(s1, pkt->data, size);
496 case AV_CODEC_ID_AMR_NB:
497 case AV_CODEC_ID_AMR_WB:
498 ff_rtp_send_amr(s1, pkt->data, size);
500 case AV_CODEC_ID_MPEG2TS:
501 rtp_send_mpegts_raw(s1, pkt->data, size);
503 case AV_CODEC_ID_H264:
504 ff_rtp_send_h264(s1, pkt->data, size);
506 case AV_CODEC_ID_H263:
507 if (s->flags & FF_RTP_FLAG_RFC2190) {
508 int mb_info_size = 0;
509 const uint8_t *mb_info =
510 av_packet_get_side_data(pkt, AV_PKT_DATA_H263_MB_INFO,
512 ff_rtp_send_h263_rfc2190(s1, pkt->data, size, mb_info, mb_info_size);
516 case AV_CODEC_ID_H263P:
517 ff_rtp_send_h263(s1, pkt->data, size);
519 case AV_CODEC_ID_VORBIS:
520 case AV_CODEC_ID_THEORA:
521 ff_rtp_send_xiph(s1, pkt->data, size);
523 case AV_CODEC_ID_VP8:
524 ff_rtp_send_vp8(s1, pkt->data, size);
526 case AV_CODEC_ID_ILBC:
527 rtp_send_ilbc(s1, pkt->data, size);
529 case AV_CODEC_ID_MJPEG:
530 ff_rtp_send_jpeg(s1, pkt->data, size);
533 /* better than nothing : send the codec raw data */
534 rtp_send_raw(s1, pkt->data, size);
540 static int rtp_write_trailer(AVFormatContext *s1)
542 RTPMuxContext *s = s1->priv_data;
549 AVOutputFormat ff_rtp_muxer = {
551 .long_name = NULL_IF_CONFIG_SMALL("RTP output"),
552 .priv_data_size = sizeof(RTPMuxContext),
553 .audio_codec = AV_CODEC_ID_PCM_MULAW,
554 .video_codec = AV_CODEC_ID_MPEG4,
555 .write_header = rtp_write_header,
556 .write_packet = rtp_write_packet,
557 .write_trailer = rtp_write_trailer,
558 .priv_class = &rtp_muxer_class,