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
31 #define RTCP_SR_SIZE 28
32 #define NTP_OFFSET 2208988800ULL
33 #define NTP_OFFSET_US (NTP_OFFSET * 1000000ULL)
35 static uint64_t ntp_time(void)
37 return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
40 static int is_supported(enum CodecID id)
46 case CODEC_ID_MPEG1VIDEO:
47 case CODEC_ID_MPEG2VIDEO:
52 case CODEC_ID_PCM_ALAW:
53 case CODEC_ID_PCM_MULAW:
55 case CODEC_ID_PCM_S16BE:
56 case CODEC_ID_PCM_S16LE:
57 case CODEC_ID_PCM_U16BE:
58 case CODEC_ID_PCM_U16LE:
60 case CODEC_ID_MPEG2TS:
69 static int rtp_write_header(AVFormatContext *s1)
71 RTPMuxContext *s = s1->priv_data;
72 int max_packet_size, n;
75 if (s1->nb_streams != 1)
78 if (!is_supported(st->codec->codec_id)) {
79 av_log(s1, AV_LOG_ERROR, "Unsupported codec %x\n", st->codec->codec_id);
84 s->payload_type = ff_rtp_get_payload_type(st->codec);
85 if (s->payload_type < 0)
86 s->payload_type = RTP_PT_PRIVATE + (st->codec->codec_type == CODEC_TYPE_AUDIO);
88 // following 2 FIXMEs could be set based on the current time, there is normally no info leak, as RTP will likely be transmitted immediately
89 s->base_timestamp = 0; /* FIXME: was random(), what should this be? */
90 s->timestamp = s->base_timestamp;
92 s->ssrc = 0; /* FIXME: was random(), what should this be? */
94 s->first_rtcp_ntp_time = ntp_time();
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 == CODEC_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 == CODEC_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;
136 case CODEC_ID_AMR_NB:
137 case CODEC_ID_AMR_WB:
138 if (!s->max_frames_per_packet)
139 s->max_frames_per_packet = 12;
140 if (st->codec->codec_id == CODEC_ID_AMR_NB)
144 /* max_header_toc_size + the largest AMR payload must fit */
145 if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
146 av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
149 if (st->codec->channels != 1) {
150 av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
156 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
157 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
166 /* send an rtcp sender report packet */
167 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
169 RTPMuxContext *s = s1->priv_data;
172 dprintf(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
174 s->last_rtcp_ntp_time = ntp_time;
175 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
176 s1->streams[0]->time_base) + s->base_timestamp;
177 put_byte(s1->pb, (RTP_VERSION << 6));
178 put_byte(s1->pb, 200);
179 put_be16(s1->pb, 6); /* length in words - 1 */
180 put_be32(s1->pb, s->ssrc);
181 put_be32(s1->pb, ntp_time / 1000000);
182 put_be32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
183 put_be32(s1->pb, rtp_ts);
184 put_be32(s1->pb, s->packet_count);
185 put_be32(s1->pb, s->octet_count);
186 put_flush_packet(s1->pb);
189 /* send an rtp packet. sequence number is incremented, but the caller
190 must update the timestamp itself */
191 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
193 RTPMuxContext *s = s1->priv_data;
195 dprintf(s1, "rtp_send_data size=%d\n", len);
197 /* build the RTP header */
198 put_byte(s1->pb, (RTP_VERSION << 6));
199 put_byte(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
200 put_be16(s1->pb, s->seq);
201 put_be32(s1->pb, s->timestamp);
202 put_be32(s1->pb, s->ssrc);
204 put_buffer(s1->pb, buf1, len);
205 put_flush_packet(s1->pb);
208 s->octet_count += len;
212 /* send an integer number of samples and compute time stamp and fill
213 the rtp send buffer before sending. */
214 static void rtp_send_samples(AVFormatContext *s1,
215 const uint8_t *buf1, int size, int sample_size)
217 RTPMuxContext *s = s1->priv_data;
218 int len, max_packet_size, n;
220 max_packet_size = (s->max_payload_size / sample_size) * sample_size;
221 /* not needed, but who nows */
222 if ((size % sample_size) != 0)
227 len = FFMIN(max_packet_size, size);
230 memcpy(s->buf_ptr, buf1, len);
234 s->timestamp = s->cur_timestamp + n / sample_size;
235 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
236 n += (s->buf_ptr - s->buf);
240 static void rtp_send_mpegaudio(AVFormatContext *s1,
241 const uint8_t *buf1, int size)
243 RTPMuxContext *s = s1->priv_data;
244 int len, count, max_packet_size;
246 max_packet_size = s->max_payload_size;
248 /* test if we must flush because not enough space */
249 len = (s->buf_ptr - s->buf);
250 if ((len + size) > max_packet_size) {
252 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
253 s->buf_ptr = s->buf + 4;
256 if (s->buf_ptr == s->buf + 4) {
257 s->timestamp = s->cur_timestamp;
261 if (size > max_packet_size) {
262 /* big packet: fragment */
265 len = max_packet_size - 4;
268 /* build fragmented packet */
271 s->buf[2] = count >> 8;
273 memcpy(s->buf + 4, buf1, len);
274 ff_rtp_send_data(s1, s->buf, len + 4, 0);
280 if (s->buf_ptr == s->buf + 4) {
281 /* no fragmentation possible */
287 memcpy(s->buf_ptr, buf1, size);
292 static void rtp_send_raw(AVFormatContext *s1,
293 const uint8_t *buf1, int size)
295 RTPMuxContext *s = s1->priv_data;
296 int len, max_packet_size;
298 max_packet_size = s->max_payload_size;
301 len = max_packet_size;
305 s->timestamp = s->cur_timestamp;
306 ff_rtp_send_data(s1, buf1, len, (len == size));
313 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
314 static void rtp_send_mpegts_raw(AVFormatContext *s1,
315 const uint8_t *buf1, int size)
317 RTPMuxContext *s = s1->priv_data;
320 while (size >= TS_PACKET_SIZE) {
321 len = s->max_payload_size - (s->buf_ptr - s->buf);
324 memcpy(s->buf_ptr, buf1, len);
329 out_len = s->buf_ptr - s->buf;
330 if (out_len >= s->max_payload_size) {
331 ff_rtp_send_data(s1, s->buf, out_len, 0);
337 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
339 RTPMuxContext *s = s1->priv_data;
340 AVStream *st = s1->streams[0];
344 dprintf(s1, "%d: write len=%d\n", pkt->stream_index, size);
346 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
348 if (s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
349 (ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
350 rtcp_send_sr(s1, ntp_time());
351 s->last_octet_count = s->octet_count;
354 s->cur_timestamp = s->base_timestamp + pkt->pts;
356 switch(st->codec->codec_id) {
357 case CODEC_ID_PCM_MULAW:
358 case CODEC_ID_PCM_ALAW:
359 case CODEC_ID_PCM_U8:
360 case CODEC_ID_PCM_S8:
361 rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
363 case CODEC_ID_PCM_U16BE:
364 case CODEC_ID_PCM_U16LE:
365 case CODEC_ID_PCM_S16BE:
366 case CODEC_ID_PCM_S16LE:
367 rtp_send_samples(s1, pkt->data, size, 2 * st->codec->channels);
371 rtp_send_mpegaudio(s1, pkt->data, size);
373 case CODEC_ID_MPEG1VIDEO:
374 case CODEC_ID_MPEG2VIDEO:
375 ff_rtp_send_mpegvideo(s1, pkt->data, size);
378 ff_rtp_send_aac(s1, pkt->data, size);
380 case CODEC_ID_AMR_NB:
381 case CODEC_ID_AMR_WB:
382 ff_rtp_send_amr(s1, pkt->data, size);
384 case CODEC_ID_MPEG2TS:
385 rtp_send_mpegts_raw(s1, pkt->data, size);
388 ff_rtp_send_h264(s1, pkt->data, size);
392 ff_rtp_send_h263(s1, pkt->data, size);
395 /* better than nothing : send the codec raw data */
396 rtp_send_raw(s1, pkt->data, size);
402 static int rtp_write_trailer(AVFormatContext *s1)
404 RTPMuxContext *s = s1->priv_data;
411 AVOutputFormat rtp_muxer = {
413 NULL_IF_CONFIG_SMALL("RTP output format"),
416 sizeof(RTPMuxContext),