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 = ff_random_get_seed();
84
s->timestamp = s->base_timestamp;
86
s->ssrc = ff_random_get_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;
134
case CODEC_ID_AMR_NB:
135
case CODEC_ID_AMR_WB:
136
if (!s->max_frames_per_packet)
137
s->max_frames_per_packet = 12;
138
if (st->codec->codec_id == CODEC_ID_AMR_NB)
142
/* max_header_toc_size + the largest AMR payload must fit */
143
if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
144
av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
147
if (st->codec->channels != 1) {
148
av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
154
if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
155
av_set_pts_info(st, 32, 1, st->codec->sample_rate);
164
/* send an rtcp sender report packet */
165
static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
167
RTPMuxContext *s = s1->priv_data;
170
dprintf(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
172
s->last_rtcp_ntp_time = ntp_time;
173
rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
174
s1->streams[0]->time_base) + s->base_timestamp;
175
put_byte(s1->pb, (RTP_VERSION << 6));
176
put_byte(s1->pb, 200);
177
put_be16(s1->pb, 6); /* length in words - 1 */
178
put_be32(s1->pb, s->ssrc);
179
put_be32(s1->pb, ntp_time / 1000000);
180
put_be32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
181
put_be32(s1->pb, rtp_ts);
182
put_be32(s1->pb, s->packet_count);
183
put_be32(s1->pb, s->octet_count);
184
put_flush_packet(s1->pb);
187
/* send an rtp packet. sequence number is incremented, but the caller
188
must update the timestamp itself */
189
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
191
RTPMuxContext *s = s1->priv_data;
193
dprintf(s1, "rtp_send_data size=%d\n", len);
195
/* build the RTP header */
196
put_byte(s1->pb, (RTP_VERSION << 6));
197
put_byte(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
198
put_be16(s1->pb, s->seq);
199
put_be32(s1->pb, s->timestamp);
200
put_be32(s1->pb, s->ssrc);
202
put_buffer(s1->pb, buf1, len);
203
put_flush_packet(s1->pb);
206
s->octet_count += len;
210
/* send an integer number of samples and compute time stamp and fill
211
the rtp send buffer before sending. */
212
static void rtp_send_samples(AVFormatContext *s1,
213
const uint8_t *buf1, int size, int sample_size)
215
RTPMuxContext *s = s1->priv_data;
216
int len, max_packet_size, n;
218
max_packet_size = (s->max_payload_size / sample_size) * sample_size;
219
/* not needed, but who nows */
220
if ((size % sample_size) != 0)
225
len = FFMIN(max_packet_size, size);
228
memcpy(s->buf_ptr, buf1, len);
232
s->timestamp = s->cur_timestamp + n / sample_size;
233
ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
234
n += (s->buf_ptr - s->buf);
238
static void rtp_send_mpegaudio(AVFormatContext *s1,
239
const uint8_t *buf1, int size)
241
RTPMuxContext *s = s1->priv_data;
242
int len, count, max_packet_size;
244
max_packet_size = s->max_payload_size;
246
/* test if we must flush because not enough space */
247
len = (s->buf_ptr - s->buf);
248
if ((len + size) > max_packet_size) {
250
ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
251
s->buf_ptr = s->buf + 4;
254
if (s->buf_ptr == s->buf + 4) {
255
s->timestamp = s->cur_timestamp;
259
if (size > max_packet_size) {
260
/* big packet: fragment */
263
len = max_packet_size - 4;
266
/* build fragmented packet */
269
s->buf[2] = count >> 8;
271
memcpy(s->buf + 4, buf1, len);
272
ff_rtp_send_data(s1, s->buf, len + 4, 0);
278
if (s->buf_ptr == s->buf + 4) {
279
/* no fragmentation possible */
285
memcpy(s->buf_ptr, buf1, size);
290
static void rtp_send_raw(AVFormatContext *s1,
291
const uint8_t *buf1, int size)
293
RTPMuxContext *s = s1->priv_data;
294
int len, max_packet_size;
296
max_packet_size = s->max_payload_size;
299
len = max_packet_size;
303
s->timestamp = s->cur_timestamp;
304
ff_rtp_send_data(s1, buf1, len, (len == size));
311
/* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
312
static void rtp_send_mpegts_raw(AVFormatContext *s1,
313
const uint8_t *buf1, int size)
315
RTPMuxContext *s = s1->priv_data;
318
while (size >= TS_PACKET_SIZE) {
319
len = s->max_payload_size - (s->buf_ptr - s->buf);
322
memcpy(s->buf_ptr, buf1, len);
327
out_len = s->buf_ptr - s->buf;
328
if (out_len >= s->max_payload_size) {
329
ff_rtp_send_data(s1, s->buf, out_len, 0);
335
static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
337
RTPMuxContext *s = s1->priv_data;
338
AVStream *st = s1->streams[0];
342
dprintf(s1, "%d: write len=%d\n", pkt->stream_index, size);
344
rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
346
if (s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
347
(ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
348
rtcp_send_sr(s1, ff_ntp_time());
349
s->last_octet_count = s->octet_count;
352
s->cur_timestamp = s->base_timestamp + pkt->pts;
354
switch(st->codec->codec_id) {
355
case CODEC_ID_PCM_MULAW:
356
case CODEC_ID_PCM_ALAW:
357
case CODEC_ID_PCM_U8:
358
case CODEC_ID_PCM_S8:
359
rtp_send_samples(s1, pkt->data, size, 1 * st->codec->channels);
361
case CODEC_ID_PCM_U16BE:
362
case CODEC_ID_PCM_U16LE:
363
case CODEC_ID_PCM_S16BE:
364
case CODEC_ID_PCM_S16LE:
365
rtp_send_samples(s1, pkt->data, size, 2 * st->codec->channels);
369
rtp_send_mpegaudio(s1, pkt->data, size);
371
case CODEC_ID_MPEG1VIDEO:
372
case CODEC_ID_MPEG2VIDEO:
373
ff_rtp_send_mpegvideo(s1, pkt->data, size);
376
ff_rtp_send_aac(s1, pkt->data, size);
378
case CODEC_ID_AMR_NB:
379
case CODEC_ID_AMR_WB:
380
ff_rtp_send_amr(s1, pkt->data, size);
382
case CODEC_ID_MPEG2TS:
383
rtp_send_mpegts_raw(s1, pkt->data, size);
386
ff_rtp_send_h264(s1, pkt->data, size);
390
ff_rtp_send_h263(s1, pkt->data, size);
393
/* better than nothing : send the codec raw data */
394
rtp_send_raw(s1, pkt->data, size);
400
static int rtp_write_trailer(AVFormatContext *s1)
402
RTPMuxContext *s = s1->priv_data;
409
AVOutputFormat rtp_muxer = {
411
NULL_IF_CONFIG_SMALL("RTP output format"),
414
sizeof(RTPMuxContext),