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
32 #define RTCP_SR_SIZE 28
34 static int is_supported(enum CodecID id)
40 case CODEC_ID_MPEG1VIDEO:
41 case CODEC_ID_MPEG2VIDEO:
46 case CODEC_ID_PCM_ALAW:
47 case CODEC_ID_PCM_MULAW:
49 case CODEC_ID_PCM_S16BE:
50 case CODEC_ID_PCM_S16LE:
51 case CODEC_ID_PCM_U16BE:
52 case CODEC_ID_PCM_U16LE:
54 case CODEC_ID_MPEG2TS:
63 static int rtp_write_header(AVFormatContext *s1)
65 RTPMuxContext *s = s1->priv_data;
66 int max_packet_size, n;
69 if (s1->nb_streams != 1)
72 if (!is_supported(st->codec->codec_id)) {
73 av_log(s1, AV_LOG_ERROR, "Unsupported codec %x\n", st->codec->codec_id);
78 s->payload_type = ff_rtp_get_payload_type(st->codec);
79 if (s->payload_type < 0)
80 s->payload_type = RTP_PT_PRIVATE + (st->codec->codec_type == CODEC_TYPE_AUDIO);
82 // following 2 FIXMEs could be set based on the current time, there is normally no info leak, as RTP will likely be transmitted immediately
83 s->base_timestamp = 0; /* FIXME: was random(), what should this be? */
84 s->timestamp = s->base_timestamp;
86 s->ssrc = 0; /* FIXME: was random(), what should this be? */
88 s->first_rtcp_ntp_time = ff_ntp_time();
90 max_packet_size = url_fget_max_packet_size(s1->pb);
91 if (max_packet_size <= 12)
93 s->buf = av_malloc(max_packet_size);
95 return AVERROR(ENOMEM);
97 s->max_payload_size = max_packet_size - 12;
99 s->max_frames_per_packet = 0;
101 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
102 if (st->codec->frame_size == 0) {
103 av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
105 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);
108 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
109 /* FIXME: We should round down here... */
110 s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
114 av_set_pts_info(st, 32, 1, 90000);
115 switch(st->codec->codec_id) {
118 s->buf_ptr = s->buf + 4;
120 case CODEC_ID_MPEG1VIDEO:
121 case CODEC_ID_MPEG2VIDEO:
123 case CODEC_ID_MPEG2TS:
124 n = s->max_payload_size / TS_PACKET_SIZE;
127 s->max_payload_size = n * TS_PACKET_SIZE;
130 case CODEC_ID_AMR_NB:
131 case CODEC_ID_AMR_WB:
132 if (!s->max_frames_per_packet)
133 s->max_frames_per_packet = 12;
134 if (st->codec->codec_id == CODEC_ID_AMR_NB)
138 /* max_header_toc_size + the largest AMR payload must fit */
139 if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
140 av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
143 if (st->codec->channels != 1) {
144 av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
150 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
151 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
160 /* send an rtcp sender report packet */
161 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
163 RTPMuxContext *s = s1->priv_data;
166 dprintf(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
168 s->last_rtcp_ntp_time = ntp_time;
169 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
170 s1->streams[0]->time_base) + s->base_timestamp;
171 put_byte(s1->pb, (RTP_VERSION << 6));
172 put_byte(s1->pb, 200);
173 put_be16(s1->pb, 6); /* length in words - 1 */
174 put_be32(s1->pb, s->ssrc);
175 put_be32(s1->pb, ntp_time / 1000000);
176 put_be32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
177 put_be32(s1->pb, rtp_ts);
178 put_be32(s1->pb, s->packet_count);
179 put_be32(s1->pb, s->octet_count);
180 put_flush_packet(s1->pb);
183 /* send an rtp packet. sequence number is incremented, but the caller
184 must update the timestamp itself */
185 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
187 RTPMuxContext *s = s1->priv_data;
189 dprintf(s1, "rtp_send_data size=%d\n", len);
191 /* build the RTP header */
192 put_byte(s1->pb, (RTP_VERSION << 6));
193 put_byte(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
194 put_be16(s1->pb, s->seq);
195 put_be32(s1->pb, s->timestamp);
196 put_be32(s1->pb, s->ssrc);
198 put_buffer(s1->pb, buf1, len);
199 put_flush_packet(s1->pb);
202 s->octet_count += len;
206 /* send an integer number of samples and compute time stamp and fill
207 the rtp send buffer before sending. */
208 static void rtp_send_samples(AVFormatContext *s1,
209 const uint8_t *buf1, int size, int sample_size)
211 RTPMuxContext *s = s1->priv_data;
212 int len, max_packet_size, n;
214 max_packet_size = (s->max_payload_size / sample_size) * sample_size;
215 /* not needed, but who nows */
216 if ((size % sample_size) != 0)
221 len = FFMIN(max_packet_size, size);
224 memcpy(s->buf_ptr, buf1, len);
228 s->timestamp = s->cur_timestamp + n / sample_size;
229 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
230 n += (s->buf_ptr - s->buf);
234 static void rtp_send_mpegaudio(AVFormatContext *s1,
235 const uint8_t *buf1, int size)
237 RTPMuxContext *s = s1->priv_data;
238 int len, count, max_packet_size;
240 max_packet_size = s->max_payload_size;
242 /* test if we must flush because not enough space */
243 len = (s->buf_ptr - s->buf);
244 if ((len + size) > max_packet_size) {
246 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
247 s->buf_ptr = s->buf + 4;
250 if (s->buf_ptr == s->buf + 4) {
251 s->timestamp = s->cur_timestamp;
255 if (size > max_packet_size) {
256 /* big packet: fragment */
259 len = max_packet_size - 4;
262 /* build fragmented packet */
265 s->buf[2] = count >> 8;
267 memcpy(s->buf + 4, buf1, len);
268 ff_rtp_send_data(s1, s->buf, len + 4, 0);
274 if (s->buf_ptr == s->buf + 4) {
275 /* no fragmentation possible */
281 memcpy(s->buf_ptr, buf1, size);
286 static void rtp_send_raw(AVFormatContext *s1,
287 const uint8_t *buf1, int size)
289 RTPMuxContext *s = s1->priv_data;
290 int len, max_packet_size;
292 max_packet_size = s->max_payload_size;
295 len = max_packet_size;
299 s->timestamp = s->cur_timestamp;
300 ff_rtp_send_data(s1, buf1, len, (len == size));
307 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
308 static void rtp_send_mpegts_raw(AVFormatContext *s1,
309 const uint8_t *buf1, int size)
311 RTPMuxContext *s = s1->priv_data;
314 while (size >= TS_PACKET_SIZE) {
315 len = s->max_payload_size - (s->buf_ptr - s->buf);
318 memcpy(s->buf_ptr, buf1, len);
323 out_len = s->buf_ptr - s->buf;
324 if (out_len >= s->max_payload_size) {
325 ff_rtp_send_data(s1, s->buf, out_len, 0);
331 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
333 RTPMuxContext *s = s1->priv_data;
334 AVStream *st = s1->streams[0];
338 dprintf(s1, "%d: write len=%d\n", pkt->stream_index, size);
340 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
342 if (s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
343 (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
344 rtcp_send_sr(s1, ff_ntp_time());
345 s->last_octet_count = s->octet_count;
348 s->cur_timestamp = s->base_timestamp + pkt->pts;
350 switch(st->codec->codec_id) {
351 case CODEC_ID_PCM_MULAW:
352 case CODEC_ID_PCM_ALAW:
353 case CODEC_ID_PCM_U8:
354 case CODEC_ID_PCM_S8:
355 rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
357 case CODEC_ID_PCM_U16BE:
358 case CODEC_ID_PCM_U16LE:
359 case CODEC_ID_PCM_S16BE:
360 case CODEC_ID_PCM_S16LE:
361 rtp_send_samples(s1, pkt->data, size, 2 * st->codec->channels);
365 rtp_send_mpegaudio(s1, pkt->data, size);
367 case CODEC_ID_MPEG1VIDEO:
368 case CODEC_ID_MPEG2VIDEO:
369 ff_rtp_send_mpegvideo(s1, pkt->data, size);
372 ff_rtp_send_aac(s1, pkt->data, size);
374 case CODEC_ID_AMR_NB:
375 case CODEC_ID_AMR_WB:
376 ff_rtp_send_amr(s1, pkt->data, size);
378 case CODEC_ID_MPEG2TS:
379 rtp_send_mpegts_raw(s1, pkt->data, size);
382 ff_rtp_send_h264(s1, pkt->data, size);
386 ff_rtp_send_h263(s1, pkt->data, size);
389 /* better than nothing : send the codec raw data */
390 rtp_send_raw(s1, pkt->data, size);
396 static int rtp_write_trailer(AVFormatContext *s1)
398 RTPMuxContext *s = s1->priv_data;
405 AVOutputFormat rtp_muxer = {
407 NULL_IF_CONFIG_SMALL("RTP output format"),
410 sizeof(RTPMuxContext),