rtp: set the payload type as stream id
[libav.git] / libavformat / rtpenc.c
CommitLineData
83a0d387
LA
1/*
2 * RTP output format
406792e7 3 * Copyright (c) 2002 Fabrice Bellard
83a0d387 4 *
2912e87a 5 * This file is part of Libav.
83a0d387 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
83a0d387
LA
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
83a0d387
LA
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
83a0d387
LA
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
245976da 21
83a0d387
LA
22#include "avformat.h"
23#include "mpegts.h"
594a9aeb 24#include "internal.h"
0ebcdf5c 25#include "libavutil/mathematics.h"
4c1202f7 26#include "libavutil/random_seed.h"
08321228 27#include "libavutil/opt.h"
83a0d387 28
302879cb 29#include "rtpenc.h"
83a0d387
LA
30
31//#define DEBUG
32
08321228 33static const AVOption options[] = {
5354a904 34 FF_RTP_FLAG_OPTS(RTPMuxContext, flags),
e6153f17
MS
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 },
08321228
JCR
37 { NULL },
38};
39
40static 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
83a0d387
LA
47#define RTCP_SR_SIZE 28
48
36ef5369 49static int is_supported(enum AVCodecID id)
0766c3ee
LA
50{
51 switch(id) {
36ef5369
AK
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:
cee1950b 78 case AV_CODEC_ID_MJPEG:
490ae95a 79 case AV_CODEC_ID_SPEEX:
c136a813 80 case AV_CODEC_ID_OPUS:
0766c3ee
LA
81 return 1;
82 default:
83 return 0;
84 }
85}
86
83a0d387
LA
87static int rtp_write_header(AVFormatContext *s1)
88{
302879cb 89 RTPMuxContext *s = s1->priv_data;
f5534620 90 int n;
83a0d387
LA
91 AVStream *st;
92
ada4e362
MS
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 }
83a0d387 97 st = s1->streams[0];
0766c3ee
LA
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 }
83a0d387 103
8034130e
LB
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
576fb48e 115 s->base_timestamp = av_get_random_seed();
83a0d387
LA
116 s->timestamp = s->base_timestamp;
117 s->cur_timestamp = 0;
2dcb21a9
MS
118 if (!s->ssrc)
119 s->ssrc = av_get_random_seed();
83a0d387 120 s->first_packet = 1;
594a9aeb 121 s->first_rtcp_ntp_time = ff_ntp_time();
b1d55e5e
MS
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;
83a0d387 126
316e724f 127 if (s1->packet_size) {
ba605cef 128 if (s1->pb->max_packet_size)
316e724f
MS
129 s1->packet_size = FFMIN(s1->packet_size,
130 s1->pb->max_packet_size);
ba605cef 131 } else
316e724f
MS
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);
83a0d387 135 return AVERROR(EIO);
ba83ac4c 136 }
316e724f 137 s->buf = av_malloc(s1->packet_size);
d3536678
LA
138 if (s->buf == NULL) {
139 return AVERROR(ENOMEM);
140 }
316e724f 141 s->max_payload_size = s1->packet_size - 12;
83a0d387
LA
142
143 s->max_frames_per_packet = 0;
4fa57d52 144 if (s1->max_delay > 0) {
72415b2a 145 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
14aecc50
JR
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) {
83a0d387
LA
150 av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
151 } else {
14aecc50
JR
152 s->max_frames_per_packet =
153 av_rescale_q_rnd(s1->max_delay,
154 AV_TIME_BASE_Q,
94f1b11a 155 (AVRational){ frame_size, st->codec->sample_rate },
14aecc50 156 AV_ROUND_DOWN);
83a0d387
LA
157 }
158 }
72415b2a 159 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
83a0d387 160 /* FIXME: We should round down here... */
a4696aa2 161 s->max_frames_per_packet = av_rescale_q(s1->max_delay, (AVRational){1, 1000000}, st->codec->time_base);
83a0d387
LA
162 }
163 }
164
c3f9ebf7 165 avpriv_set_pts_info(st, 32, 1, 90000);
83a0d387 166 switch(st->codec->codec_id) {
36ef5369
AK
167 case AV_CODEC_ID_MP2:
168 case AV_CODEC_ID_MP3:
83a0d387
LA
169 s->buf_ptr = s->buf + 4;
170 break;
36ef5369
AK
171 case AV_CODEC_ID_MPEG1VIDEO:
172 case AV_CODEC_ID_MPEG2VIDEO:
83a0d387 173 break;
36ef5369 174 case AV_CODEC_ID_MPEG2TS:
83a0d387
LA
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;
36ef5369 181 case AV_CODEC_ID_H264:
8b889b34 182 /* check for H.264 MP4 syntax */
8a2679ad 183 if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
8b889b34
LA
184 s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
185 }
186 break;
36ef5369
AK
187 case AV_CODEC_ID_VORBIS:
188 case AV_CODEC_ID_THEORA:
91af5601
JA
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;
36ef5369 194 case AV_CODEC_ID_ADPCM_G722:
0048a2a8
MS
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. */
c3f9ebf7 197 avpriv_set_pts_info(st, 32, 1, 8000);
0048a2a8 198 break;
c136a813
MS
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;
36ef5369 209 case AV_CODEC_ID_ILBC:
579fd87b
MS
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;
36ef5369
AK
219 case AV_CODEC_ID_AMR_NB:
220 case AV_CODEC_ID_AMR_WB:
08e696c0
MS
221 if (!s->max_frames_per_packet)
222 s->max_frames_per_packet = 12;
36ef5369 223 if (st->codec->codec_id == AV_CODEC_ID_AMR_NB)
08e696c0
MS
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");
e9ef88fb 230 goto fail;
08e696c0
MS
231 }
232 if (st->codec->channels != 1) {
233 av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
e9ef88fb 234 goto fail;
08e696c0 235 }
36ef5369 236 case AV_CODEC_ID_AAC:
21da81d7 237 s->num_frames = 0;
83a0d387 238 default:
91af5601 239defaultcase:
72415b2a 240 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
c3f9ebf7 241 avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
83a0d387
LA
242 }
243 s->buf_ptr = s->buf;
244 break;
245 }
246
247 return 0;
e9ef88fb
MS
248
249fail:
250 av_freep(&s->buf);
251 return AVERROR(EINVAL);
83a0d387
LA
252}
253
254/* send an rtcp sender report packet */
255static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
256{
302879cb 257 RTPMuxContext *s = s1->priv_data;
83a0d387
LA
258 uint32_t rtp_ts;
259
dfd2a005 260 av_dlog(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
83a0d387 261
83a0d387 262 s->last_rtcp_ntp_time = ntp_time;
a4696aa2 263 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
83a0d387 264 s1->streams[0]->time_base) + s->base_timestamp;
77eb5504
AK
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);
b7f2fdde 274 avio_flush(s1->pb);
83a0d387
LA
275}
276
277/* send an rtp packet. sequence number is incremented, but the caller
278 must update the timestamp itself */
279void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
280{
302879cb 281 RTPMuxContext *s = s1->priv_data;
83a0d387 282
dfd2a005 283 av_dlog(s1, "rtp_send_data size=%d\n", len);
83a0d387
LA
284
285 /* build the RTP header */
77eb5504
AK
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);
83a0d387 291
77eb5504 292 avio_write(s1->pb, buf1, len);
b7f2fdde 293 avio_flush(s1->pb);
83a0d387
LA
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. */
bfb82fcd
MS
302static int rtp_send_samples(AVFormatContext *s1,
303 const uint8_t *buf1, int size, int sample_size_bits)
83a0d387 304{
302879cb 305 RTPMuxContext *s = s1->priv_data;
83a0d387 306 int len, max_packet_size, n;
77e0c758
MS
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);
83a0d387 309
77e0c758
MS
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)
bfb82fcd 313 return AVERROR(EINVAL);
83a0d387
LA
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;
77e0c758 324 s->timestamp = s->cur_timestamp + n * 8 / sample_size_bits;
83a0d387
LA
325 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
326 n += (s->buf_ptr - s->buf);
327 }
bfb82fcd 328 return 0;
83a0d387
LA
329}
330
83a0d387
LA
331static void rtp_send_mpegaudio(AVFormatContext *s1,
332 const uint8_t *buf1, int size)
333{
302879cb 334 RTPMuxContext *s = s1->priv_data;
83a0d387
LA
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
383static void rtp_send_raw(AVFormatContext *s1,
384 const uint8_t *buf1, int size)
385{
302879cb 386 RTPMuxContext *s = s1->priv_data;
83a0d387
LA
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 */
405static void rtp_send_mpegts_raw(AVFormatContext *s1,
406 const uint8_t *buf1, int size)
407{
302879cb 408 RTPMuxContext *s = s1->priv_data;
83a0d387
LA
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
579fd87b
MS
428static 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
83a0d387
LA
458static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
459{
302879cb 460 RTPMuxContext *s = s1->priv_data;
83a0d387
LA
461 AVStream *st = s1->streams[0];
462 int rtcp_bytes;
463 int size= pkt->size;
83a0d387 464
dfd2a005 465 av_dlog(s1, "%d: write len=%d\n", pkt->stream_index, size);
83a0d387 466
83a0d387
LA
467 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
468 RTCP_TX_RATIO_DEN;
7337484e
MS
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)) {
594a9aeb 472 rtcp_send_sr(s1, ff_ntp_time());
83a0d387
LA
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) {
36ef5369
AK
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:
bfb82fcd 483 return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
36ef5369
AK
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:
bfb82fcd 488 return rtp_send_samples(s1, pkt->data, size, 16 * st->codec->channels);
36ef5369 489 case AV_CODEC_ID_ADPCM_G722:
0048a2a8
MS
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,
77e0c758
MS
492 * the correct parameter for send_samples_bits is 8 bits per stream
493 * clock. */
bfb82fcd 494 return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
36ef5369 495 case AV_CODEC_ID_ADPCM_G726:
bfb82fcd
MS
496 return rtp_send_samples(s1, pkt->data, size,
497 st->codec->bits_per_coded_sample * st->codec->channels);
36ef5369
AK
498 case AV_CODEC_ID_MP2:
499 case AV_CODEC_ID_MP3:
d3d1eae6 500 rtp_send_mpegaudio(s1, pkt->data, size);
83a0d387 501 break;
36ef5369
AK
502 case AV_CODEC_ID_MPEG1VIDEO:
503 case AV_CODEC_ID_MPEG2VIDEO:
d3d1eae6 504 ff_rtp_send_mpegvideo(s1, pkt->data, size);
83a0d387 505 break;
36ef5369 506 case AV_CODEC_ID_AAC:
08321228
JCR
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);
83a0d387 511 break;
36ef5369
AK
512 case AV_CODEC_ID_AMR_NB:
513 case AV_CODEC_ID_AMR_WB:
d3d1eae6 514 ff_rtp_send_amr(s1, pkt->data, size);
08e696c0 515 break;
36ef5369 516 case AV_CODEC_ID_MPEG2TS:
d3d1eae6 517 rtp_send_mpegts_raw(s1, pkt->data, size);
83a0d387 518 break;
36ef5369 519 case AV_CODEC_ID_H264:
d3d1eae6 520 ff_rtp_send_h264(s1, pkt->data, size);
f79bfe48 521 break;
36ef5369 522 case AV_CODEC_ID_H263:
c4584f3c 523 if (s->flags & FF_RTP_FLAG_RFC2190) {
984b914c
MS
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);
c4584f3c
MS
529 break;
530 }
531 /* Fallthrough */
36ef5369 532 case AV_CODEC_ID_H263P:
d3d1eae6 533 ff_rtp_send_h263(s1, pkt->data, size);
9edfaf3c 534 break;
36ef5369
AK
535 case AV_CODEC_ID_VORBIS:
536 case AV_CODEC_ID_THEORA:
91af5601
JA
537 ff_rtp_send_xiph(s1, pkt->data, size);
538 break;
36ef5369 539 case AV_CODEC_ID_VP8:
7b18d94c
JA
540 ff_rtp_send_vp8(s1, pkt->data, size);
541 break;
36ef5369 542 case AV_CODEC_ID_ILBC:
579fd87b
MS
543 rtp_send_ilbc(s1, pkt->data, size);
544 break;
cee1950b
SP
545 case AV_CODEC_ID_MJPEG:
546 ff_rtp_send_jpeg(s1, pkt->data, size);
547 break;
c136a813
MS
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 */
83a0d387
LA
556 default:
557 /* better than nothing : send the codec raw data */
d3d1eae6 558 rtp_send_raw(s1, pkt->data, size);
83a0d387
LA
559 break;
560 }
561 return 0;
562}
563
d3536678
LA
564static 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
c6610a21 573AVOutputFormat ff_rtp_muxer = {
dfc2c4d9 574 .name = "rtp",
6774247a 575 .long_name = NULL_IF_CONFIG_SMALL("RTP output"),
dfc2c4d9 576 .priv_data_size = sizeof(RTPMuxContext),
36ef5369
AK
577 .audio_codec = AV_CODEC_ID_PCM_MULAW,
578 .video_codec = AV_CODEC_ID_MPEG4,
dfc2c4d9
AK
579 .write_header = rtp_write_header,
580 .write_packet = rtp_write_packet,
581 .write_trailer = rtp_write_trailer,
20234a4b 582 .priv_class = &rtp_muxer_class,
83a0d387 583};