3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/random_seed.h"
31 #define RTCP_SR_SIZE 28
33 static int is_supported(enum CodecID id)
39 case CODEC_ID_MPEG1VIDEO:
40 case CODEC_ID_MPEG2VIDEO:
45 case CODEC_ID_PCM_ALAW:
46 case CODEC_ID_PCM_MULAW:
48 case CODEC_ID_PCM_S16BE:
49 case CODEC_ID_PCM_S16LE:
50 case CODEC_ID_PCM_U16BE:
51 case CODEC_ID_PCM_U16LE:
53 case CODEC_ID_MPEG2TS:
59 case CODEC_ID_ADPCM_G722:
66 static int rtp_write_header(AVFormatContext *s1)
68 RTPMuxContext *s = s1->priv_data;
69 int max_packet_size, n;
72 if (s1->nb_streams != 1)
75 if (!is_supported(st->codec->codec_id)) {
76 av_log(s1, AV_LOG_ERROR, "Unsupported codec %x\n", st->codec->codec_id);
81 s->payload_type = ff_rtp_get_payload_type(st->codec);
82 if (s->payload_type < 0)
83 s->payload_type = RTP_PT_PRIVATE + (st->codec->codec_type == AVMEDIA_TYPE_AUDIO);
85 s->base_timestamp = av_get_random_seed();
86 s->timestamp = s->base_timestamp;
88 s->ssrc = av_get_random_seed();
90 s->first_rtcp_ntp_time = ff_ntp_time();
91 if (s1->start_time_realtime)
92 /* Round the NTP time to whole milliseconds. */
93 s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
96 max_packet_size = url_fget_max_packet_size(s1->pb);
97 if (max_packet_size <= 12)
99 s->buf = av_malloc(max_packet_size);
100 if (s->buf == NULL) {
101 return AVERROR(ENOMEM);
103 s->max_payload_size = max_packet_size - 12;
105 s->max_frames_per_packet = 0;
107 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
108 if (st->codec->frame_size == 0) {
109 av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
111 s->max_frames_per_packet = av_rescale_rnd(s1->max_delay, st->codec->sample_rate, AV_TIME_BASE * st->codec->frame_size, AV_ROUND_DOWN);
114 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
115 /* FIXME: We should round down here... */
116 s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
120 av_set_pts_info(st, 32, 1, 90000);
121 switch(st->codec->codec_id) {
124 s->buf_ptr = s->buf + 4;
126 case CODEC_ID_MPEG1VIDEO:
127 case CODEC_ID_MPEG2VIDEO:
129 case CODEC_ID_MPEG2TS:
130 n = s->max_payload_size / TS_PACKET_SIZE;
133 s->max_payload_size = n * TS_PACKET_SIZE;
137 /* check for H.264 MP4 syntax */
138 if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
139 s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
142 case CODEC_ID_VORBIS:
143 case CODEC_ID_THEORA:
144 if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
145 s->max_frames_per_packet = av_clip(s->max_frames_per_packet, 1, 15);
146 s->max_payload_size -= 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
150 av_log(s1, AV_LOG_WARNING, "RTP VP8 payload is still experimental\n");
152 case CODEC_ID_ADPCM_G722:
153 /* Due to a historical error, the clock rate for G722 in RTP is
154 * 8000, even if the sample rate is 16000. See RFC 3551. */
155 av_set_pts_info(st, 32, 1, 8000);
157 case CODEC_ID_AMR_NB:
158 case CODEC_ID_AMR_WB:
159 if (!s->max_frames_per_packet)
160 s->max_frames_per_packet = 12;
161 if (st->codec->codec_id == CODEC_ID_AMR_NB)
165 /* max_header_toc_size + the largest AMR payload must fit */
166 if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
167 av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
170 if (st->codec->channels != 1) {
171 av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
178 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
179 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
188 /* send an rtcp sender report packet */
189 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
191 RTPMuxContext *s = s1->priv_data;
194 dprintf(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
196 s->last_rtcp_ntp_time = ntp_time;
197 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
198 s1->streams[0]->time_base) + s->base_timestamp;
199 put_byte(s1->pb, (RTP_VERSION << 6));
200 put_byte(s1->pb, RTCP_SR);
201 put_be16(s1->pb, 6); /* length in words - 1 */
202 put_be32(s1->pb, s->ssrc);
203 put_be32(s1->pb, ntp_time / 1000000);
204 put_be32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
205 put_be32(s1->pb, rtp_ts);
206 put_be32(s1->pb, s->packet_count);
207 put_be32(s1->pb, s->octet_count);
208 put_flush_packet(s1->pb);
211 /* send an rtp packet. sequence number is incremented, but the caller
212 must update the timestamp itself */
213 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
215 RTPMuxContext *s = s1->priv_data;
217 dprintf(s1, "rtp_send_data size=%d\n", len);
219 /* build the RTP header */
220 put_byte(s1->pb, (RTP_VERSION << 6));
221 put_byte(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
222 put_be16(s1->pb, s->seq);
223 put_be32(s1->pb, s->timestamp);
224 put_be32(s1->pb, s->ssrc);
226 put_buffer(s1->pb, buf1, len);
227 put_flush_packet(s1->pb);
230 s->octet_count += len;
234 /* send an integer number of samples and compute time stamp and fill
235 the rtp send buffer before sending. */
236 static void rtp_send_samples(AVFormatContext *s1,
237 const uint8_t *buf1, int size, int sample_size)
239 RTPMuxContext *s = s1->priv_data;
240 int len, max_packet_size, n;
242 max_packet_size = (s->max_payload_size / sample_size) * sample_size;
243 /* not needed, but who nows */
244 if ((size % sample_size) != 0)
249 len = FFMIN(max_packet_size, size);
252 memcpy(s->buf_ptr, buf1, len);
256 s->timestamp = s->cur_timestamp + n / sample_size;
257 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
258 n += (s->buf_ptr - s->buf);
262 static void rtp_send_mpegaudio(AVFormatContext *s1,
263 const uint8_t *buf1, int size)
265 RTPMuxContext *s = s1->priv_data;
266 int len, count, max_packet_size;
268 max_packet_size = s->max_payload_size;
270 /* test if we must flush because not enough space */
271 len = (s->buf_ptr - s->buf);
272 if ((len + size) > max_packet_size) {
274 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
275 s->buf_ptr = s->buf + 4;
278 if (s->buf_ptr == s->buf + 4) {
279 s->timestamp = s->cur_timestamp;
283 if (size > max_packet_size) {
284 /* big packet: fragment */
287 len = max_packet_size - 4;
290 /* build fragmented packet */
293 s->buf[2] = count >> 8;
295 memcpy(s->buf + 4, buf1, len);
296 ff_rtp_send_data(s1, s->buf, len + 4, 0);
302 if (s->buf_ptr == s->buf + 4) {
303 /* no fragmentation possible */
309 memcpy(s->buf_ptr, buf1, size);
314 static void rtp_send_raw(AVFormatContext *s1,
315 const uint8_t *buf1, int size)
317 RTPMuxContext *s = s1->priv_data;
318 int len, max_packet_size;
320 max_packet_size = s->max_payload_size;
323 len = max_packet_size;
327 s->timestamp = s->cur_timestamp;
328 ff_rtp_send_data(s1, buf1, len, (len == size));
335 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
336 static void rtp_send_mpegts_raw(AVFormatContext *s1,
337 const uint8_t *buf1, int size)
339 RTPMuxContext *s = s1->priv_data;
342 while (size >= TS_PACKET_SIZE) {
343 len = s->max_payload_size - (s->buf_ptr - s->buf);
346 memcpy(s->buf_ptr, buf1, len);
351 out_len = s->buf_ptr - s->buf;
352 if (out_len >= s->max_payload_size) {
353 ff_rtp_send_data(s1, s->buf, out_len, 0);
359 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
361 RTPMuxContext *s = s1->priv_data;
362 AVStream *st = s1->streams[0];
366 dprintf(s1, "%d: write len=%d\n", pkt->stream_index, size);
368 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
370 if (s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
371 (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
372 rtcp_send_sr(s1, ff_ntp_time());
373 s->last_octet_count = s->octet_count;
376 s->cur_timestamp = s->base_timestamp + pkt->pts;
378 switch(st->codec->codec_id) {
379 case CODEC_ID_PCM_MULAW:
380 case CODEC_ID_PCM_ALAW:
381 case CODEC_ID_PCM_U8:
382 case CODEC_ID_PCM_S8:
383 rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
385 case CODEC_ID_PCM_U16BE:
386 case CODEC_ID_PCM_U16LE:
387 case CODEC_ID_PCM_S16BE:
388 case CODEC_ID_PCM_S16LE:
389 rtp_send_samples(s1, pkt->data, size, 2 * st->codec->channels);
391 case CODEC_ID_ADPCM_G722:
392 /* The actual sample size is half a byte per sample, but since the
393 * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
394 * the correct parameter for send_samples is 1 byte per stream clock. */
395 rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
399 rtp_send_mpegaudio(s1, pkt->data, size);
401 case CODEC_ID_MPEG1VIDEO:
402 case CODEC_ID_MPEG2VIDEO:
403 ff_rtp_send_mpegvideo(s1, pkt->data, size);
406 ff_rtp_send_aac(s1, pkt->data, size);
408 case CODEC_ID_AMR_NB:
409 case CODEC_ID_AMR_WB:
410 ff_rtp_send_amr(s1, pkt->data, size);
412 case CODEC_ID_MPEG2TS:
413 rtp_send_mpegts_raw(s1, pkt->data, size);
416 ff_rtp_send_h264(s1, pkt->data, size);
420 ff_rtp_send_h263(s1, pkt->data, size);
422 case CODEC_ID_VORBIS:
423 case CODEC_ID_THEORA:
424 ff_rtp_send_xiph(s1, pkt->data, size);
427 ff_rtp_send_vp8(s1, pkt->data, size);
430 /* better than nothing : send the codec raw data */
431 rtp_send_raw(s1, pkt->data, size);
437 static int rtp_write_trailer(AVFormatContext *s1)
439 RTPMuxContext *s = s1->priv_data;
446 AVOutputFormat rtp_muxer = {
448 NULL_IF_CONFIG_SMALL("RTP output format"),
451 sizeof(RTPMuxContext),