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"
33 #define RTCP_SR_SIZE 28
35 static int is_supported(enum CodecID id)
41 case CODEC_ID_MPEG1VIDEO:
42 case CODEC_ID_MPEG2VIDEO:
47 case CODEC_ID_PCM_ALAW:
48 case CODEC_ID_PCM_MULAW:
50 case CODEC_ID_PCM_S16BE:
51 case CODEC_ID_PCM_S16LE:
52 case CODEC_ID_PCM_U16BE:
53 case CODEC_ID_PCM_U16LE:
55 case CODEC_ID_MPEG2TS:
64 static int rtp_write_header(AVFormatContext *s1)
66 RTPMuxContext *s = s1->priv_data;
67 int max_packet_size, n;
70 if (s1->nb_streams != 1)
73 if (!is_supported(st->codec->codec_id)) {
74 av_log(s1, AV_LOG_ERROR, "Unsupported codec %x\n", st->codec->codec_id);
79 s->payload_type = ff_rtp_get_payload_type(st->codec);
80 if (s->payload_type < 0)
81 s->payload_type = RTP_PT_PRIVATE + (st->codec->codec_type == AVMEDIA_TYPE_AUDIO);
83 s->base_timestamp = av_get_random_seed();
84 s->timestamp = s->base_timestamp;
86 s->ssrc = av_get_random_seed();
88 s->first_rtcp_ntp_time = ff_ntp_time();
89 if (s1->start_time_realtime)
90 /* Round the NTP time to whole milliseconds. */
91 s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
94 max_packet_size = url_fget_max_packet_size(s1->pb);
95 if (max_packet_size <= 12)
97 s->buf = av_malloc(max_packet_size);
99 return AVERROR(ENOMEM);
101 s->max_payload_size = max_packet_size - 12;
103 s->max_frames_per_packet = 0;
105 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
106 if (st->codec->frame_size == 0) {
107 av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
109 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);
112 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
113 /* FIXME: We should round down here... */
114 s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
118 av_set_pts_info(st, 32, 1, 90000);
119 switch(st->codec->codec_id) {
122 s->buf_ptr = s->buf + 4;
124 case CODEC_ID_MPEG1VIDEO:
125 case CODEC_ID_MPEG2VIDEO:
127 case CODEC_ID_MPEG2TS:
128 n = s->max_payload_size / TS_PACKET_SIZE;
131 s->max_payload_size = n * TS_PACKET_SIZE;
135 /* check for H.264 MP4 syntax */
136 if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
137 s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
140 case CODEC_ID_AMR_NB:
141 case CODEC_ID_AMR_WB:
142 if (!s->max_frames_per_packet)
143 s->max_frames_per_packet = 12;
144 if (st->codec->codec_id == CODEC_ID_AMR_NB)
148 /* max_header_toc_size + the largest AMR payload must fit */
149 if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
150 av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
153 if (st->codec->channels != 1) {
154 av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
160 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
161 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
170 /* send an rtcp sender report packet */
171 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
173 RTPMuxContext *s = s1->priv_data;
176 dprintf(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
178 s->last_rtcp_ntp_time = ntp_time;
179 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
180 s1->streams[0]->time_base) + s->base_timestamp;
181 put_byte(s1->pb, (RTP_VERSION << 6));
182 put_byte(s1->pb, 200);
183 put_be16(s1->pb, 6); /* length in words - 1 */
184 put_be32(s1->pb, s->ssrc);
185 put_be32(s1->pb, ntp_time / 1000000);
186 put_be32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
187 put_be32(s1->pb, rtp_ts);
188 put_be32(s1->pb, s->packet_count);
189 put_be32(s1->pb, s->octet_count);
190 put_flush_packet(s1->pb);
193 /* send an rtp packet. sequence number is incremented, but the caller
194 must update the timestamp itself */
195 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
197 RTPMuxContext *s = s1->priv_data;
199 dprintf(s1, "rtp_send_data size=%d\n", len);
201 /* build the RTP header */
202 put_byte(s1->pb, (RTP_VERSION << 6));
203 put_byte(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
204 put_be16(s1->pb, s->seq);
205 put_be32(s1->pb, s->timestamp);
206 put_be32(s1->pb, s->ssrc);
208 put_buffer(s1->pb, buf1, len);
209 put_flush_packet(s1->pb);
212 s->octet_count += len;
216 /* send an integer number of samples and compute time stamp and fill
217 the rtp send buffer before sending. */
218 static void rtp_send_samples(AVFormatContext *s1,
219 const uint8_t *buf1, int size, int sample_size)
221 RTPMuxContext *s = s1->priv_data;
222 int len, max_packet_size, n;
224 max_packet_size = (s->max_payload_size / sample_size) * sample_size;
225 /* not needed, but who nows */
226 if ((size % sample_size) != 0)
231 len = FFMIN(max_packet_size, size);
234 memcpy(s->buf_ptr, buf1, len);
238 s->timestamp = s->cur_timestamp + n / sample_size;
239 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
240 n += (s->buf_ptr - s->buf);
244 static void rtp_send_mpegaudio(AVFormatContext *s1,
245 const uint8_t *buf1, int size)
247 RTPMuxContext *s = s1->priv_data;
248 int len, count, max_packet_size;
250 max_packet_size = s->max_payload_size;
252 /* test if we must flush because not enough space */
253 len = (s->buf_ptr - s->buf);
254 if ((len + size) > max_packet_size) {
256 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
257 s->buf_ptr = s->buf + 4;
260 if (s->buf_ptr == s->buf + 4) {
261 s->timestamp = s->cur_timestamp;
265 if (size > max_packet_size) {
266 /* big packet: fragment */
269 len = max_packet_size - 4;
272 /* build fragmented packet */
275 s->buf[2] = count >> 8;
277 memcpy(s->buf + 4, buf1, len);
278 ff_rtp_send_data(s1, s->buf, len + 4, 0);
284 if (s->buf_ptr == s->buf + 4) {
285 /* no fragmentation possible */
291 memcpy(s->buf_ptr, buf1, size);
296 static void rtp_send_raw(AVFormatContext *s1,
297 const uint8_t *buf1, int size)
299 RTPMuxContext *s = s1->priv_data;
300 int len, max_packet_size;
302 max_packet_size = s->max_payload_size;
305 len = max_packet_size;
309 s->timestamp = s->cur_timestamp;
310 ff_rtp_send_data(s1, buf1, len, (len == size));
317 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
318 static void rtp_send_mpegts_raw(AVFormatContext *s1,
319 const uint8_t *buf1, int size)
321 RTPMuxContext *s = s1->priv_data;
324 while (size >= TS_PACKET_SIZE) {
325 len = s->max_payload_size - (s->buf_ptr - s->buf);
328 memcpy(s->buf_ptr, buf1, len);
333 out_len = s->buf_ptr - s->buf;
334 if (out_len >= s->max_payload_size) {
335 ff_rtp_send_data(s1, s->buf, out_len, 0);
341 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
343 RTPMuxContext *s = s1->priv_data;
344 AVStream *st = s1->streams[0];
348 dprintf(s1, "%d: write len=%d\n", pkt->stream_index, size);
350 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
352 if (s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
353 (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
354 rtcp_send_sr(s1, ff_ntp_time());
355 s->last_octet_count = s->octet_count;
358 s->cur_timestamp = s->base_timestamp + pkt->pts;
360 switch(st->codec->codec_id) {
361 case CODEC_ID_PCM_MULAW:
362 case CODEC_ID_PCM_ALAW:
363 case CODEC_ID_PCM_U8:
364 case CODEC_ID_PCM_S8:
365 rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
367 case CODEC_ID_PCM_U16BE:
368 case CODEC_ID_PCM_U16LE:
369 case CODEC_ID_PCM_S16BE:
370 case CODEC_ID_PCM_S16LE:
371 rtp_send_samples(s1, pkt->data, size, 2 * st->codec->channels);
375 rtp_send_mpegaudio(s1, pkt->data, size);
377 case CODEC_ID_MPEG1VIDEO:
378 case CODEC_ID_MPEG2VIDEO:
379 ff_rtp_send_mpegvideo(s1, pkt->data, size);
382 ff_rtp_send_aac(s1, pkt->data, size);
384 case CODEC_ID_AMR_NB:
385 case CODEC_ID_AMR_WB:
386 ff_rtp_send_amr(s1, pkt->data, size);
388 case CODEC_ID_MPEG2TS:
389 rtp_send_mpegts_raw(s1, pkt->data, size);
392 ff_rtp_send_h264(s1, pkt->data, size);
396 ff_rtp_send_h263(s1, pkt->data, size);
399 /* better than nothing : send the codec raw data */
400 rtp_send_raw(s1, pkt->data, size);
406 static int rtp_write_trailer(AVFormatContext *s1)
408 RTPMuxContext *s = s1->priv_data;
415 AVOutputFormat rtp_muxer = {
417 NULL_IF_CONFIG_SMALL("RTP output format"),
420 sizeof(RTPMuxContext),