Add Haivision SRT protocol
[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 static const AVOption options[] = {
32 FF_RTP_FLAG_OPTS(RTPMuxContext, flags),
33 { "payload_type", "Specify RTP payload type", offsetof(RTPMuxContext, payload_type), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, 127, AV_OPT_FLAG_ENCODING_PARAM },
34 { "ssrc", "Stream identifier", offsetof(RTPMuxContext, ssrc), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
35 { "cname", "CNAME to include in RTCP SR packets", offsetof(RTPMuxContext, cname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
36 { "seq", "Starting sequence number", offsetof(RTPMuxContext, seq), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, 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_H261:
53 case AV_CODEC_ID_H263:
54 case AV_CODEC_ID_H263P:
55 case AV_CODEC_ID_H264:
56 case AV_CODEC_ID_HEVC:
57 case AV_CODEC_ID_MPEG1VIDEO:
58 case AV_CODEC_ID_MPEG2VIDEO:
59 case AV_CODEC_ID_MPEG4:
60 case AV_CODEC_ID_AAC:
61 case AV_CODEC_ID_MP2:
62 case AV_CODEC_ID_MP3:
63 case AV_CODEC_ID_PCM_ALAW:
64 case AV_CODEC_ID_PCM_MULAW:
65 case AV_CODEC_ID_PCM_S8:
66 case AV_CODEC_ID_PCM_S16BE:
67 case AV_CODEC_ID_PCM_S16LE:
68 case AV_CODEC_ID_PCM_U16BE:
69 case AV_CODEC_ID_PCM_U16LE:
70 case AV_CODEC_ID_PCM_U8:
71 case AV_CODEC_ID_MPEG2TS:
72 case AV_CODEC_ID_AMR_NB:
73 case AV_CODEC_ID_AMR_WB:
74 case AV_CODEC_ID_VORBIS:
75 case AV_CODEC_ID_THEORA:
76 case AV_CODEC_ID_VP8:
77 case AV_CODEC_ID_ADPCM_G722:
78 case AV_CODEC_ID_ADPCM_G726:
79 case AV_CODEC_ID_ILBC:
80 case AV_CODEC_ID_MJPEG:
81 case AV_CODEC_ID_SPEEX:
82 case AV_CODEC_ID_OPUS:
83 return 1;
84 default:
85 return 0;
86 }
87 }
88
89 static int rtp_write_header(AVFormatContext *s1)
90 {
91 RTPMuxContext *s = s1->priv_data;
92 int n, ret = AVERROR(EINVAL);
93 AVStream *st;
94
95 if (s1->nb_streams != 1) {
96 av_log(s1, AV_LOG_ERROR, "Only one stream supported in the RTP muxer\n");
97 return AVERROR(EINVAL);
98 }
99 st = s1->streams[0];
100 if (!is_supported(st->codecpar->codec_id)) {
101 av_log(s1, AV_LOG_ERROR, "Unsupported codec %x\n", st->codecpar->codec_id);
102
103 return -1;
104 }
105
106 if (s->payload_type < 0) {
107 /* Re-validate non-dynamic payload types */
108 if (st->id < RTP_PT_PRIVATE)
109 st->id = ff_rtp_get_payload_type(s1, st->codecpar, -1);
110
111 s->payload_type = st->id;
112 } else {
113 /* private option takes priority */
114 st->id = s->payload_type;
115 }
116
117 s->base_timestamp = av_get_random_seed();
118 s->timestamp = s->base_timestamp;
119 s->cur_timestamp = 0;
120 if (!s->ssrc)
121 s->ssrc = av_get_random_seed();
122 s->first_packet = 1;
123 s->first_rtcp_ntp_time = ff_ntp_time();
124 if (s1->start_time_realtime)
125 /* Round the NTP time to whole milliseconds. */
126 s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
127 NTP_OFFSET_US;
128 // Pick a random sequence start number, but in the lower end of the
129 // available range, so that any wraparound doesn't happen immediately.
130 // (Immediate wraparound would be an issue for SRTP.)
131 if (s->seq < 0)
132 s->seq = av_get_random_seed() & 0x0fff;
133 else
134 s->seq &= 0xffff; // Use the given parameter, wrapped to the right interval
135
136 if (s1->packet_size) {
137 if (s1->pb->max_packet_size)
138 s1->packet_size = FFMIN(s1->packet_size,
139 s1->pb->max_packet_size);
140 } else
141 s1->packet_size = s1->pb->max_packet_size;
142 if (s1->packet_size <= 12) {
143 av_log(s1, AV_LOG_ERROR, "Max packet size %u too low\n", s1->packet_size);
144 return AVERROR(EIO);
145 }
146 s->buf = av_malloc(s1->packet_size);
147 if (!s->buf) {
148 return AVERROR(ENOMEM);
149 }
150 s->max_payload_size = s1->packet_size - 12;
151
152 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
153 avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate);
154 } else {
155 avpriv_set_pts_info(st, 32, 1, 90000);
156 }
157 s->buf_ptr = s->buf;
158 switch(st->codecpar->codec_id) {
159 case AV_CODEC_ID_MP2:
160 case AV_CODEC_ID_MP3:
161 s->buf_ptr = s->buf + 4;
162 avpriv_set_pts_info(st, 32, 1, 90000);
163 break;
164 case AV_CODEC_ID_MPEG1VIDEO:
165 case AV_CODEC_ID_MPEG2VIDEO:
166 break;
167 case AV_CODEC_ID_MPEG2TS:
168 n = s->max_payload_size / TS_PACKET_SIZE;
169 if (n < 1)
170 n = 1;
171 s->max_payload_size = n * TS_PACKET_SIZE;
172 break;
173 case AV_CODEC_ID_H261:
174 if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
175 av_log(s, AV_LOG_ERROR,
176 "Packetizing H.261 is experimental and produces incorrect "
177 "packetization for cases where GOBs don't fit into packets "
178 "(even though most receivers may handle it just fine). "
179 "Please set -f_strict experimental in order to enable it.\n");
180 ret = AVERROR_EXPERIMENTAL;
181 goto fail;
182 }
183 break;
184 case AV_CODEC_ID_H264:
185 /* check for H.264 MP4 syntax */
186 if (st->codecpar->extradata_size > 4 && st->codecpar->extradata[0] == 1) {
187 s->nal_length_size = (st->codecpar->extradata[4] & 0x03) + 1;
188 }
189 break;
190 case AV_CODEC_ID_HEVC:
191 /* Only check for the standardized hvcC version of extradata, keeping
192 * things simple and similar to the avcC/H.264 case above, instead
193 * of trying to handle the pre-standardization versions (as in
194 * libavcodec/hevc.c). */
195 if (st->codecpar->extradata_size > 21 && st->codecpar->extradata[0] == 1) {
196 s->nal_length_size = (st->codecpar->extradata[21] & 0x03) + 1;
197 }
198 break;
199 case AV_CODEC_ID_VORBIS:
200 case AV_CODEC_ID_THEORA:
201 s->max_frames_per_packet = 15;
202 break;
203 case AV_CODEC_ID_ADPCM_G722:
204 /* Due to a historical error, the clock rate for G722 in RTP is
205 * 8000, even if the sample rate is 16000. See RFC 3551. */
206 avpriv_set_pts_info(st, 32, 1, 8000);
207 break;
208 case AV_CODEC_ID_OPUS:
209 if (st->codecpar->channels > 2) {
210 av_log(s1, AV_LOG_ERROR, "Multistream opus not supported in RTP\n");
211 goto fail;
212 }
213 /* The opus RTP RFC says that all opus streams should use 48000 Hz
214 * as clock rate, since all opus sample rates can be expressed in
215 * this clock rate, and sample rate changes on the fly are supported. */
216 avpriv_set_pts_info(st, 32, 1, 48000);
217 break;
218 case AV_CODEC_ID_ILBC:
219 if (st->codecpar->block_align != 38 && st->codecpar->block_align != 50) {
220 av_log(s1, AV_LOG_ERROR, "Incorrect iLBC block size specified\n");
221 goto fail;
222 }
223 s->max_frames_per_packet = s->max_payload_size / st->codecpar->block_align;
224 break;
225 case AV_CODEC_ID_AMR_NB:
226 case AV_CODEC_ID_AMR_WB:
227 s->max_frames_per_packet = 50;
228 if (st->codecpar->codec_id == AV_CODEC_ID_AMR_NB)
229 n = 31;
230 else
231 n = 61;
232 /* max_header_toc_size + the largest AMR payload must fit */
233 if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
234 av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
235 goto fail;
236 }
237 if (st->codecpar->channels != 1) {
238 av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
239 goto fail;
240 }
241 break;
242 case AV_CODEC_ID_AAC:
243 s->max_frames_per_packet = 50;
244 break;
245 default:
246 break;
247 }
248
249 return 0;
250
251 fail:
252 av_freep(&s->buf);
253 return ret;
254 }
255
256 /* send an rtcp sender report packet */
257 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time, int bye)
258 {
259 RTPMuxContext *s = s1->priv_data;
260 uint32_t rtp_ts;
261
262 av_log(s1, AV_LOG_TRACE, "RTCP: %02x %"PRIx64" %"PRIx32"\n",
263 s->payload_type, ntp_time, s->timestamp);
264
265 s->last_rtcp_ntp_time = ntp_time;
266 rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
267 s1->streams[0]->time_base) + s->base_timestamp;
268 avio_w8(s1->pb, RTP_VERSION << 6);
269 avio_w8(s1->pb, RTCP_SR);
270 avio_wb16(s1->pb, 6); /* length in words - 1 */
271 avio_wb32(s1->pb, s->ssrc);
272 avio_wb64(s1->pb, NTP_TO_RTP_FORMAT(ntp_time));
273 avio_wb32(s1->pb, rtp_ts);
274 avio_wb32(s1->pb, s->packet_count);
275 avio_wb32(s1->pb, s->octet_count);
276
277 if (s->cname) {
278 int len = FFMIN(strlen(s->cname), 255);
279 avio_w8(s1->pb, (RTP_VERSION << 6) + 1);
280 avio_w8(s1->pb, RTCP_SDES);
281 avio_wb16(s1->pb, (7 + len + 3) / 4); /* length in words - 1 */
282
283 avio_wb32(s1->pb, s->ssrc);
284 avio_w8(s1->pb, 0x01); /* CNAME */
285 avio_w8(s1->pb, len);
286 avio_write(s1->pb, s->cname, len);
287 avio_w8(s1->pb, 0); /* END */
288 for (len = (7 + len) % 4; len % 4; len++)
289 avio_w8(s1->pb, 0);
290 }
291
292 if (bye) {
293 avio_w8(s1->pb, (RTP_VERSION << 6) | 1);
294 avio_w8(s1->pb, RTCP_BYE);
295 avio_wb16(s1->pb, 1); /* length in words - 1 */
296 avio_wb32(s1->pb, s->ssrc);
297 }
298
299 avio_flush(s1->pb);
300 }
301
302 /* send an rtp packet. sequence number is incremented, but the caller
303 must update the timestamp itself */
304 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
305 {
306 RTPMuxContext *s = s1->priv_data;
307
308 av_log(s1, AV_LOG_TRACE, "rtp_send_data size=%d\n", len);
309
310 /* build the RTP header */
311 avio_w8(s1->pb, RTP_VERSION << 6);
312 avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
313 avio_wb16(s1->pb, s->seq);
314 avio_wb32(s1->pb, s->timestamp);
315 avio_wb32(s1->pb, s->ssrc);
316
317 avio_write(s1->pb, buf1, len);
318 avio_flush(s1->pb);
319
320 s->seq = (s->seq + 1) & 0xffff;
321 s->octet_count += len;
322 s->packet_count++;
323 }
324
325 /* send an integer number of samples and compute time stamp and fill
326 the rtp send buffer before sending. */
327 static int rtp_send_samples(AVFormatContext *s1,
328 const uint8_t *buf1, int size, int sample_size_bits)
329 {
330 RTPMuxContext *s = s1->priv_data;
331 int len, max_packet_size, n;
332 /* Calculate the number of bytes to get samples aligned on a byte border */
333 int aligned_samples_size = sample_size_bits/av_gcd(sample_size_bits, 8);
334
335 max_packet_size = (s->max_payload_size / aligned_samples_size) * aligned_samples_size;
336 /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
337 if ((sample_size_bits % 8) == 0 && ((8 * size) % sample_size_bits) != 0)
338 return AVERROR(EINVAL);
339 n = 0;
340 while (size > 0) {
341 s->buf_ptr = s->buf;
342 len = FFMIN(max_packet_size, size);
343
344 /* copy data */
345 memcpy(s->buf_ptr, buf1, len);
346 s->buf_ptr += len;
347 buf1 += len;
348 size -= len;
349 s->timestamp = s->cur_timestamp + n * 8 / sample_size_bits;
350 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
351 n += (s->buf_ptr - s->buf);
352 }
353 return 0;
354 }
355
356 static void rtp_send_mpegaudio(AVFormatContext *s1,
357 const uint8_t *buf1, int size)
358 {
359 RTPMuxContext *s = s1->priv_data;
360 int len, count, max_packet_size;
361
362 max_packet_size = s->max_payload_size;
363
364 /* test if we must flush because not enough space */
365 len = (s->buf_ptr - s->buf);
366 if ((len + size) > max_packet_size) {
367 if (len > 4) {
368 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
369 s->buf_ptr = s->buf + 4;
370 }
371 }
372 if (s->buf_ptr == s->buf + 4) {
373 s->timestamp = s->cur_timestamp;
374 }
375
376 /* add the packet */
377 if (size > max_packet_size) {
378 /* big packet: fragment */
379 count = 0;
380 while (size > 0) {
381 len = max_packet_size - 4;
382 if (len > size)
383 len = size;
384 /* build fragmented packet */
385 s->buf[0] = 0;
386 s->buf[1] = 0;
387 s->buf[2] = count >> 8;
388 s->buf[3] = count;
389 memcpy(s->buf + 4, buf1, len);
390 ff_rtp_send_data(s1, s->buf, len + 4, 0);
391 size -= len;
392 buf1 += len;
393 count += len;
394 }
395 } else {
396 if (s->buf_ptr == s->buf + 4) {
397 /* no fragmentation possible */
398 s->buf[0] = 0;
399 s->buf[1] = 0;
400 s->buf[2] = 0;
401 s->buf[3] = 0;
402 }
403 memcpy(s->buf_ptr, buf1, size);
404 s->buf_ptr += size;
405 }
406 }
407
408 static void rtp_send_raw(AVFormatContext *s1,
409 const uint8_t *buf1, int size)
410 {
411 RTPMuxContext *s = s1->priv_data;
412 int len, max_packet_size;
413
414 max_packet_size = s->max_payload_size;
415
416 while (size > 0) {
417 len = max_packet_size;
418 if (len > size)
419 len = size;
420
421 s->timestamp = s->cur_timestamp;
422 ff_rtp_send_data(s1, buf1, len, (len == size));
423
424 buf1 += len;
425 size -= len;
426 }
427 }
428
429 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
430 static void rtp_send_mpegts_raw(AVFormatContext *s1,
431 const uint8_t *buf1, int size)
432 {
433 RTPMuxContext *s = s1->priv_data;
434 int len, out_len;
435
436 s->timestamp = s->cur_timestamp;
437 while (size >= TS_PACKET_SIZE) {
438 len = s->max_payload_size - (s->buf_ptr - s->buf);
439 if (len > size)
440 len = size;
441 memcpy(s->buf_ptr, buf1, len);
442 buf1 += len;
443 size -= len;
444 s->buf_ptr += len;
445
446 out_len = s->buf_ptr - s->buf;
447 if (out_len >= s->max_payload_size) {
448 ff_rtp_send_data(s1, s->buf, out_len, 0);
449 s->buf_ptr = s->buf;
450 }
451 }
452 }
453
454 static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
455 {
456 RTPMuxContext *s = s1->priv_data;
457 AVStream *st = s1->streams[0];
458 int frame_duration = av_get_audio_frame_duration2(st->codecpar, 0);
459 int frame_size = st->codecpar->block_align;
460 int frames = size / frame_size;
461
462 while (frames > 0) {
463 if (s->num_frames > 0 &&
464 av_compare_ts(s->cur_timestamp - s->timestamp, st->time_base,
465 s1->max_delay, AV_TIME_BASE_Q) >= 0) {
466 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
467 s->num_frames = 0;
468 }
469
470 if (!s->num_frames) {
471 s->buf_ptr = s->buf;
472 s->timestamp = s->cur_timestamp;
473 }
474 memcpy(s->buf_ptr, buf, frame_size);
475 frames--;
476 s->num_frames++;
477 s->buf_ptr += frame_size;
478 buf += frame_size;
479 s->cur_timestamp += frame_duration;
480
481 if (s->num_frames == s->max_frames_per_packet) {
482 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
483 s->num_frames = 0;
484 }
485 }
486 return 0;
487 }
488
489 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
490 {
491 RTPMuxContext *s = s1->priv_data;
492 AVStream *st = s1->streams[0];
493 int rtcp_bytes;
494 int size= pkt->size;
495
496 av_log(s1, AV_LOG_TRACE, "%d: write len=%d\n", pkt->stream_index, size);
497
498 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
499 RTCP_TX_RATIO_DEN;
500 if ((s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
501 (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) &&
502 !(s->flags & FF_RTP_FLAG_SKIP_RTCP)) {
503 rtcp_send_sr(s1, ff_ntp_time(), 0);
504 s->last_octet_count = s->octet_count;
505 s->first_packet = 0;
506 }
507 s->cur_timestamp = s->base_timestamp + pkt->pts;
508
509 switch(st->codecpar->codec_id) {
510 case AV_CODEC_ID_PCM_MULAW:
511 case AV_CODEC_ID_PCM_ALAW:
512 case AV_CODEC_ID_PCM_U8:
513 case AV_CODEC_ID_PCM_S8:
514 return rtp_send_samples(s1, pkt->data, size, 8 * st->codecpar->channels);
515 case AV_CODEC_ID_PCM_U16BE:
516 case AV_CODEC_ID_PCM_U16LE:
517 case AV_CODEC_ID_PCM_S16BE:
518 case AV_CODEC_ID_PCM_S16LE:
519 return rtp_send_samples(s1, pkt->data, size, 16 * st->codecpar->channels);
520 case AV_CODEC_ID_ADPCM_G722:
521 /* The actual sample size is half a byte per sample, but since the
522 * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
523 * the correct parameter for send_samples_bits is 8 bits per stream
524 * clock. */
525 return rtp_send_samples(s1, pkt->data, size, 8 * st->codecpar->channels);
526 case AV_CODEC_ID_ADPCM_G726:
527 return rtp_send_samples(s1, pkt->data, size,
528 st->codecpar->bits_per_coded_sample * st->codecpar->channels);
529 case AV_CODEC_ID_MP2:
530 case AV_CODEC_ID_MP3:
531 rtp_send_mpegaudio(s1, pkt->data, size);
532 break;
533 case AV_CODEC_ID_MPEG1VIDEO:
534 case AV_CODEC_ID_MPEG2VIDEO:
535 ff_rtp_send_mpegvideo(s1, pkt->data, size);
536 break;
537 case AV_CODEC_ID_AAC:
538 if (s->flags & FF_RTP_FLAG_MP4A_LATM)
539 ff_rtp_send_latm(s1, pkt->data, size);
540 else
541 ff_rtp_send_aac(s1, pkt->data, size);
542 break;
543 case AV_CODEC_ID_AMR_NB:
544 case AV_CODEC_ID_AMR_WB:
545 ff_rtp_send_amr(s1, pkt->data, size);
546 break;
547 case AV_CODEC_ID_MPEG2TS:
548 rtp_send_mpegts_raw(s1, pkt->data, size);
549 break;
550 case AV_CODEC_ID_H264:
551 ff_rtp_send_h264_hevc(s1, pkt->data, size);
552 break;
553 case AV_CODEC_ID_H261:
554 ff_rtp_send_h261(s1, pkt->data, size);
555 break;
556 case AV_CODEC_ID_H263:
557 if (s->flags & FF_RTP_FLAG_RFC2190) {
558 int mb_info_size = 0;
559 const uint8_t *mb_info =
560 av_packet_get_side_data(pkt, AV_PKT_DATA_H263_MB_INFO,
561 &mb_info_size);
562 ff_rtp_send_h263_rfc2190(s1, pkt->data, size, mb_info, mb_info_size);
563 break;
564 }
565 /* Fallthrough */
566 case AV_CODEC_ID_H263P:
567 ff_rtp_send_h263(s1, pkt->data, size);
568 break;
569 case AV_CODEC_ID_HEVC:
570 ff_rtp_send_h264_hevc(s1, pkt->data, size);
571 break;
572 case AV_CODEC_ID_VORBIS:
573 case AV_CODEC_ID_THEORA:
574 ff_rtp_send_xiph(s1, pkt->data, size);
575 break;
576 case AV_CODEC_ID_VP8:
577 ff_rtp_send_vp8(s1, pkt->data, size);
578 break;
579 case AV_CODEC_ID_ILBC:
580 rtp_send_ilbc(s1, pkt->data, size);
581 break;
582 case AV_CODEC_ID_MJPEG:
583 ff_rtp_send_jpeg(s1, pkt->data, size);
584 break;
585 case AV_CODEC_ID_OPUS:
586 if (size > s->max_payload_size) {
587 av_log(s1, AV_LOG_ERROR,
588 "Packet size %d too large for max RTP payload size %d\n",
589 size, s->max_payload_size);
590 return AVERROR(EINVAL);
591 }
592 /* Intentional fallthrough */
593 default:
594 /* better than nothing : send the codec raw data */
595 rtp_send_raw(s1, pkt->data, size);
596 break;
597 }
598 return 0;
599 }
600
601 static int rtp_write_trailer(AVFormatContext *s1)
602 {
603 RTPMuxContext *s = s1->priv_data;
604
605 /* If the caller closes and recreates ->pb, this might actually
606 * be NULL here even if it was successfully allocated at the start. */
607 if (s1->pb && (s->flags & FF_RTP_FLAG_SEND_BYE))
608 rtcp_send_sr(s1, ff_ntp_time(), 1);
609 av_freep(&s->buf);
610
611 return 0;
612 }
613
614 AVOutputFormat ff_rtp_muxer = {
615 .name = "rtp",
616 .long_name = NULL_IF_CONFIG_SMALL("RTP output"),
617 .priv_data_size = sizeof(RTPMuxContext),
618 .audio_codec = AV_CODEC_ID_PCM_MULAW,
619 .video_codec = AV_CODEC_ID_MPEG4,
620 .write_header = rtp_write_header,
621 .write_packet = rtp_write_packet,
622 .write_trailer = rtp_write_trailer,
623 .priv_class = &rtp_muxer_class,
624 .flags = AVFMT_TS_NONSTRICT,
625 };