rtp: set the payload type as stream id
[libav.git] / libavformat / rtpenc.c
1 /*
2 * RTP output format
3 * Copyright (c) 2002 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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.
11 *
12 * Libav 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "avformat.h"
23 #include "mpegts.h"
24 #include "internal.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/random_seed.h"
27 #include "libavutil/opt.h"
28
29 #include "rtpenc.h"
30
31 //#define DEBUG
32
33 static const AVOption options[] = {
34 FF_RTP_FLAG_OPTS(RTPMuxContext, flags),
35 { "payload_type", "Specify RTP payload type", offsetof(RTPMuxContext, payload_type), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, 127, AV_OPT_FLAG_ENCODING_PARAM },
36 { "ssrc", "Stream identifier", offsetof(RTPMuxContext, ssrc), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
37 { NULL },
38 };
39
40 static const AVClass rtp_muxer_class = {
41 .class_name = "RTP muxer",
42 .item_name = av_default_item_name,
43 .option = options,
44 .version = LIBAVUTIL_VERSION_INT,
45 };
46
47 #define RTCP_SR_SIZE 28
48
49 static int is_supported(enum AVCodecID id)
50 {
51 switch(id) {
52 case AV_CODEC_ID_H263:
53 case AV_CODEC_ID_H263P:
54 case AV_CODEC_ID_H264:
55 case AV_CODEC_ID_MPEG1VIDEO:
56 case AV_CODEC_ID_MPEG2VIDEO:
57 case AV_CODEC_ID_MPEG4:
58 case AV_CODEC_ID_AAC:
59 case AV_CODEC_ID_MP2:
60 case AV_CODEC_ID_MP3:
61 case AV_CODEC_ID_PCM_ALAW:
62 case AV_CODEC_ID_PCM_MULAW:
63 case AV_CODEC_ID_PCM_S8:
64 case AV_CODEC_ID_PCM_S16BE:
65 case AV_CODEC_ID_PCM_S16LE:
66 case AV_CODEC_ID_PCM_U16BE:
67 case AV_CODEC_ID_PCM_U16LE:
68 case AV_CODEC_ID_PCM_U8:
69 case AV_CODEC_ID_MPEG2TS:
70 case AV_CODEC_ID_AMR_NB:
71 case AV_CODEC_ID_AMR_WB:
72 case AV_CODEC_ID_VORBIS:
73 case AV_CODEC_ID_THEORA:
74 case AV_CODEC_ID_VP8:
75 case AV_CODEC_ID_ADPCM_G722:
76 case AV_CODEC_ID_ADPCM_G726:
77 case AV_CODEC_ID_ILBC:
78 case AV_CODEC_ID_MJPEG:
79 case AV_CODEC_ID_SPEEX:
80 case AV_CODEC_ID_OPUS:
81 return 1;
82 default:
83 return 0;
84 }
85 }
86
87 static int rtp_write_header(AVFormatContext *s1)
88 {
89 RTPMuxContext *s = s1->priv_data;
90 int n;
91 AVStream *st;
92
93 if (s1->nb_streams != 1) {
94 av_log(s1, AV_LOG_ERROR, "Only one stream supported in the RTP muxer\n");
95 return AVERROR(EINVAL);
96 }
97 st = s1->streams[0];
98 if (!is_supported(st->codec->codec_id)) {
99 av_log(s1, AV_LOG_ERROR, "Unsupported codec %x\n", st->codec->codec_id);
100
101 return -1;
102 }
103
104 if (s->payload_type < 0) {
105 /* Re-validate non-dynamic payload types */
106 if (st->id < RTP_PT_PRIVATE)
107 st->id = ff_rtp_get_payload_type(s1, st->codec, -1);
108
109 s->payload_type = st->id;
110 } else {
111 /* private option takes priority */
112 st->id = s->payload_type;
113 }
114
115 s->base_timestamp = av_get_random_seed();
116 s->timestamp = s->base_timestamp;
117 s->cur_timestamp = 0;
118 if (!s->ssrc)
119 s->ssrc = av_get_random_seed();
120 s->first_packet = 1;
121 s->first_rtcp_ntp_time = ff_ntp_time();
122 if (s1->start_time_realtime)
123 /* Round the NTP time to whole milliseconds. */
124 s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
125 NTP_OFFSET_US;
126
127 if (s1->packet_size) {
128 if (s1->pb->max_packet_size)
129 s1->packet_size = FFMIN(s1->packet_size,
130 s1->pb->max_packet_size);
131 } else
132 s1->packet_size = s1->pb->max_packet_size;
133 if (s1->packet_size <= 12) {
134 av_log(s1, AV_LOG_ERROR, "Max packet size %d too low\n", s1->packet_size);
135 return AVERROR(EIO);
136 }
137 s->buf = av_malloc(s1->packet_size);
138 if (s->buf == NULL) {
139 return AVERROR(ENOMEM);
140 }
141 s->max_payload_size = s1->packet_size - 12;
142
143 s->max_frames_per_packet = 0;
144 if (s1->max_delay > 0) {
145 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
146 int frame_size = av_get_audio_frame_duration(st->codec, 0);
147 if (!frame_size)
148 frame_size = st->codec->frame_size;
149 if (frame_size == 0) {
150 av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
151 } else {
152 s->max_frames_per_packet =
153 av_rescale_q_rnd(s1->max_delay,
154 AV_TIME_BASE_Q,
155 (AVRational){ frame_size, st->codec->sample_rate },
156 AV_ROUND_DOWN);
157 }
158 }
159 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
160 /* FIXME: We should round down here... */
161 s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
162 }
163 }
164
165 avpriv_set_pts_info(st, 32, 1, 90000);
166 switch(st->codec->codec_id) {
167 case AV_CODEC_ID_MP2:
168 case AV_CODEC_ID_MP3:
169 s->buf_ptr = s->buf + 4;
170 break;
171 case AV_CODEC_ID_MPEG1VIDEO:
172 case AV_CODEC_ID_MPEG2VIDEO:
173 break;
174 case AV_CODEC_ID_MPEG2TS:
175 n = s->max_payload_size / TS_PACKET_SIZE;
176 if (n < 1)
177 n = 1;
178 s->max_payload_size = n * TS_PACKET_SIZE;
179 s->buf_ptr = s->buf;
180 break;
181 case AV_CODEC_ID_H264:
182 /* check for H.264 MP4 syntax */
183 if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
184 s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
185 }
186 break;
187 case AV_CODEC_ID_VORBIS:
188 case AV_CODEC_ID_THEORA:
189 if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
190 s->max_frames_per_packet = av_clip(s->max_frames_per_packet, 1, 15);
191 s->max_payload_size -= 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
192 s->num_frames = 0;
193 goto defaultcase;
194 case AV_CODEC_ID_ADPCM_G722:
195 /* Due to a historical error, the clock rate for G722 in RTP is
196 * 8000, even if the sample rate is 16000. See RFC 3551. */
197 avpriv_set_pts_info(st, 32, 1, 8000);
198 break;
199 case AV_CODEC_ID_OPUS:
200 if (st->codec->channels > 2) {
201 av_log(s1, AV_LOG_ERROR, "Multistream opus not supported in RTP\n");
202 goto fail;
203 }
204 /* The opus RTP RFC says that all opus streams should use 48000 Hz
205 * as clock rate, since all opus sample rates can be expressed in
206 * this clock rate, and sample rate changes on the fly are supported. */
207 avpriv_set_pts_info(st, 32, 1, 48000);
208 break;
209 case AV_CODEC_ID_ILBC:
210 if (st->codec->block_align != 38 && st->codec->block_align != 50) {
211 av_log(s1, AV_LOG_ERROR, "Incorrect iLBC block size specified\n");
212 goto fail;
213 }
214 if (!s->max_frames_per_packet)
215 s->max_frames_per_packet = 1;
216 s->max_frames_per_packet = FFMIN(s->max_frames_per_packet,
217 s->max_payload_size / st->codec->block_align);
218 goto defaultcase;
219 case AV_CODEC_ID_AMR_NB:
220 case AV_CODEC_ID_AMR_WB:
221 if (!s->max_frames_per_packet)
222 s->max_frames_per_packet = 12;
223 if (st->codec->codec_id == AV_CODEC_ID_AMR_NB)
224 n = 31;
225 else
226 n = 61;
227 /* max_header_toc_size + the largest AMR payload must fit */
228 if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
229 av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
230 goto fail;
231 }
232 if (st->codec->channels != 1) {
233 av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
234 goto fail;
235 }
236 case AV_CODEC_ID_AAC:
237 s->num_frames = 0;
238 default:
239 defaultcase:
240 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
241 avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
242 }
243 s->buf_ptr = s->buf;
244 break;
245 }
246
247 return 0;
248
249 fail:
250 av_freep(&s->buf);
251 return AVERROR(EINVAL);
252 }
253
254 /* send an rtcp sender report packet */
255 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
256 {
257 RTPMuxContext *s = s1->priv_data;
258 uint32_t rtp_ts;
259
260 av_dlog(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
261
262 s->last_rtcp_ntp_time = ntp_time;
263 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
264 s1->streams[0]->time_base) + s->base_timestamp;
265 avio_w8(s1->pb, (RTP_VERSION << 6));
266 avio_w8(s1->pb, RTCP_SR);
267 avio_wb16(s1->pb, 6); /* length in words - 1 */
268 avio_wb32(s1->pb, s->ssrc);
269 avio_wb32(s1->pb, ntp_time / 1000000);
270 avio_wb32(s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
271 avio_wb32(s1->pb, rtp_ts);
272 avio_wb32(s1->pb, s->packet_count);
273 avio_wb32(s1->pb, s->octet_count);
274 avio_flush(s1->pb);
275 }
276
277 /* send an rtp packet. sequence number is incremented, but the caller
278 must update the timestamp itself */
279 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
280 {
281 RTPMuxContext *s = s1->priv_data;
282
283 av_dlog(s1, "rtp_send_data size=%d\n", len);
284
285 /* build the RTP header */
286 avio_w8(s1->pb, (RTP_VERSION << 6));
287 avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
288 avio_wb16(s1->pb, s->seq);
289 avio_wb32(s1->pb, s->timestamp);
290 avio_wb32(s1->pb, s->ssrc);
291
292 avio_write(s1->pb, buf1, len);
293 avio_flush(s1->pb);
294
295 s->seq++;
296 s->octet_count += len;
297 s->packet_count++;
298 }
299
300 /* send an integer number of samples and compute time stamp and fill
301 the rtp send buffer before sending. */
302 static int rtp_send_samples(AVFormatContext *s1,
303 const uint8_t *buf1, int size, int sample_size_bits)
304 {
305 RTPMuxContext *s = s1->priv_data;
306 int len, max_packet_size, n;
307 /* Calculate the number of bytes to get samples aligned on a byte border */
308 int aligned_samples_size = sample_size_bits/av_gcd(sample_size_bits, 8);
309
310 max_packet_size = (s->max_payload_size / aligned_samples_size) * aligned_samples_size;
311 /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
312 if ((sample_size_bits % 8) == 0 && ((8 * size) % sample_size_bits) != 0)
313 return AVERROR(EINVAL);
314 n = 0;
315 while (size > 0) {
316 s->buf_ptr = s->buf;
317 len = FFMIN(max_packet_size, size);
318
319 /* copy data */
320 memcpy(s->buf_ptr, buf1, len);
321 s->buf_ptr += len;
322 buf1 += len;
323 size -= len;
324 s->timestamp = s->cur_timestamp + n * 8 / sample_size_bits;
325 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
326 n += (s->buf_ptr - s->buf);
327 }
328 return 0;
329 }
330
331 static void rtp_send_mpegaudio(AVFormatContext *s1,
332 const uint8_t *buf1, int size)
333 {
334 RTPMuxContext *s = s1->priv_data;
335 int len, count, max_packet_size;
336
337 max_packet_size = s->max_payload_size;
338
339 /* test if we must flush because not enough space */
340 len = (s->buf_ptr - s->buf);
341 if ((len + size) > max_packet_size) {
342 if (len > 4) {
343 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
344 s->buf_ptr = s->buf + 4;
345 }
346 }
347 if (s->buf_ptr == s->buf + 4) {
348 s->timestamp = s->cur_timestamp;
349 }
350
351 /* add the packet */
352 if (size > max_packet_size) {
353 /* big packet: fragment */
354 count = 0;
355 while (size > 0) {
356 len = max_packet_size - 4;
357 if (len > size)
358 len = size;
359 /* build fragmented packet */
360 s->buf[0] = 0;
361 s->buf[1] = 0;
362 s->buf[2] = count >> 8;
363 s->buf[3] = count;
364 memcpy(s->buf + 4, buf1, len);
365 ff_rtp_send_data(s1, s->buf, len + 4, 0);
366 size -= len;
367 buf1 += len;
368 count += len;
369 }
370 } else {
371 if (s->buf_ptr == s->buf + 4) {
372 /* no fragmentation possible */
373 s->buf[0] = 0;
374 s->buf[1] = 0;
375 s->buf[2] = 0;
376 s->buf[3] = 0;
377 }
378 memcpy(s->buf_ptr, buf1, size);
379 s->buf_ptr += size;
380 }
381 }
382
383 static void rtp_send_raw(AVFormatContext *s1,
384 const uint8_t *buf1, int size)
385 {
386 RTPMuxContext *s = s1->priv_data;
387 int len, max_packet_size;
388
389 max_packet_size = s->max_payload_size;
390
391 while (size > 0) {
392 len = max_packet_size;
393 if (len > size)
394 len = size;
395
396 s->timestamp = s->cur_timestamp;
397 ff_rtp_send_data(s1, buf1, len, (len == size));
398
399 buf1 += len;
400 size -= len;
401 }
402 }
403
404 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
405 static void rtp_send_mpegts_raw(AVFormatContext *s1,
406 const uint8_t *buf1, int size)
407 {
408 RTPMuxContext *s = s1->priv_data;
409 int len, out_len;
410
411 while (size >= TS_PACKET_SIZE) {
412 len = s->max_payload_size - (s->buf_ptr - s->buf);
413 if (len > size)
414 len = size;
415 memcpy(s->buf_ptr, buf1, len);
416 buf1 += len;
417 size -= len;
418 s->buf_ptr += len;
419
420 out_len = s->buf_ptr - s->buf;
421 if (out_len >= s->max_payload_size) {
422 ff_rtp_send_data(s1, s->buf, out_len, 0);
423 s->buf_ptr = s->buf;
424 }
425 }
426 }
427
428 static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
429 {
430 RTPMuxContext *s = s1->priv_data;
431 AVStream *st = s1->streams[0];
432 int frame_duration = av_get_audio_frame_duration(st->codec, 0);
433 int frame_size = st->codec->block_align;
434 int frames = size / frame_size;
435
436 while (frames > 0) {
437 int n = FFMIN(s->max_frames_per_packet - s->num_frames, frames);
438
439 if (!s->num_frames) {
440 s->buf_ptr = s->buf;
441 s->timestamp = s->cur_timestamp;
442 }
443 memcpy(s->buf_ptr, buf, n * frame_size);
444 frames -= n;
445 s->num_frames += n;
446 s->buf_ptr += n * frame_size;
447 buf += n * frame_size;
448 s->cur_timestamp += n * frame_duration;
449
450 if (s->num_frames == s->max_frames_per_packet) {
451 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
452 s->num_frames = 0;
453 }
454 }
455 return 0;
456 }
457
458 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
459 {
460 RTPMuxContext *s = s1->priv_data;
461 AVStream *st = s1->streams[0];
462 int rtcp_bytes;
463 int size= pkt->size;
464
465 av_dlog(s1, "%d: write len=%d\n", pkt->stream_index, size);
466
467 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
468 RTCP_TX_RATIO_DEN;
469 if ((s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
470 (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) &&
471 !(s->flags & FF_RTP_FLAG_SKIP_RTCP)) {
472 rtcp_send_sr(s1, ff_ntp_time());
473 s->last_octet_count = s->octet_count;
474 s->first_packet = 0;
475 }
476 s->cur_timestamp = s->base_timestamp + pkt->pts;
477
478 switch(st->codec->codec_id) {
479 case AV_CODEC_ID_PCM_MULAW:
480 case AV_CODEC_ID_PCM_ALAW:
481 case AV_CODEC_ID_PCM_U8:
482 case AV_CODEC_ID_PCM_S8:
483 return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
484 case AV_CODEC_ID_PCM_U16BE:
485 case AV_CODEC_ID_PCM_U16LE:
486 case AV_CODEC_ID_PCM_S16BE:
487 case AV_CODEC_ID_PCM_S16LE:
488 return rtp_send_samples(s1, pkt->data, size, 16 * st->codec->channels);
489 case AV_CODEC_ID_ADPCM_G722:
490 /* The actual sample size is half a byte per sample, but since the
491 * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
492 * the correct parameter for send_samples_bits is 8 bits per stream
493 * clock. */
494 return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
495 case AV_CODEC_ID_ADPCM_G726:
496 return rtp_send_samples(s1, pkt->data, size,
497 st->codec->bits_per_coded_sample * st->codec->channels);
498 case AV_CODEC_ID_MP2:
499 case AV_CODEC_ID_MP3:
500 rtp_send_mpegaudio(s1, pkt->data, size);
501 break;
502 case AV_CODEC_ID_MPEG1VIDEO:
503 case AV_CODEC_ID_MPEG2VIDEO:
504 ff_rtp_send_mpegvideo(s1, pkt->data, size);
505 break;
506 case AV_CODEC_ID_AAC:
507 if (s->flags & FF_RTP_FLAG_MP4A_LATM)
508 ff_rtp_send_latm(s1, pkt->data, size);
509 else
510 ff_rtp_send_aac(s1, pkt->data, size);
511 break;
512 case AV_CODEC_ID_AMR_NB:
513 case AV_CODEC_ID_AMR_WB:
514 ff_rtp_send_amr(s1, pkt->data, size);
515 break;
516 case AV_CODEC_ID_MPEG2TS:
517 rtp_send_mpegts_raw(s1, pkt->data, size);
518 break;
519 case AV_CODEC_ID_H264:
520 ff_rtp_send_h264(s1, pkt->data, size);
521 break;
522 case AV_CODEC_ID_H263:
523 if (s->flags & FF_RTP_FLAG_RFC2190) {
524 int mb_info_size = 0;
525 const uint8_t *mb_info =
526 av_packet_get_side_data(pkt, AV_PKT_DATA_H263_MB_INFO,
527 &mb_info_size);
528 ff_rtp_send_h263_rfc2190(s1, pkt->data, size, mb_info, mb_info_size);
529 break;
530 }
531 /* Fallthrough */
532 case AV_CODEC_ID_H263P:
533 ff_rtp_send_h263(s1, pkt->data, size);
534 break;
535 case AV_CODEC_ID_VORBIS:
536 case AV_CODEC_ID_THEORA:
537 ff_rtp_send_xiph(s1, pkt->data, size);
538 break;
539 case AV_CODEC_ID_VP8:
540 ff_rtp_send_vp8(s1, pkt->data, size);
541 break;
542 case AV_CODEC_ID_ILBC:
543 rtp_send_ilbc(s1, pkt->data, size);
544 break;
545 case AV_CODEC_ID_MJPEG:
546 ff_rtp_send_jpeg(s1, pkt->data, size);
547 break;
548 case AV_CODEC_ID_OPUS:
549 if (size > s->max_payload_size) {
550 av_log(s1, AV_LOG_ERROR,
551 "Packet size %d too large for max RTP payload size %d\n",
552 size, s->max_payload_size);
553 return AVERROR(EINVAL);
554 }
555 /* Intentional fallthrough */
556 default:
557 /* better than nothing : send the codec raw data */
558 rtp_send_raw(s1, pkt->data, size);
559 break;
560 }
561 return 0;
562 }
563
564 static int rtp_write_trailer(AVFormatContext *s1)
565 {
566 RTPMuxContext *s = s1->priv_data;
567
568 av_freep(&s->buf);
569
570 return 0;
571 }
572
573 AVOutputFormat ff_rtp_muxer = {
574 .name = "rtp",
575 .long_name = NULL_IF_CONFIG_SMALL("RTP output"),
576 .priv_data_size = sizeof(RTPMuxContext),
577 .audio_codec = AV_CODEC_ID_PCM_MULAW,
578 .video_codec = AV_CODEC_ID_MPEG4,
579 .write_header = rtp_write_header,
580 .write_packet = rtp_write_packet,
581 .write_trailer = rtp_write_trailer,
582 .priv_class = &rtp_muxer_class,
583 };