]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpenc.c
librtmp: Allow changing the socket send buffer size
[ffmpeg] / libavformat / rtpenc.c
1 /*
2  * RTP output format
3  * Copyright (c) 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
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->codec->codec_id)) {
101         av_log(s1, AV_LOG_ERROR, "Unsupported codec %s\n", avcodec_get_name(st->codec->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->codec, -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 != 0  &&  s1->start_time_realtime != AV_NOPTS_VALUE)
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         if (s1->flags & AVFMT_FLAG_BITEXACT) {
133             s->seq = 0;
134         } else
135             s->seq = av_get_random_seed() & 0x0fff;
136     } else
137         s->seq &= 0xffff; // Use the given parameter, wrapped to the right interval
138
139     if (s1->packet_size) {
140         if (s1->pb->max_packet_size)
141             s1->packet_size = FFMIN(s1->packet_size,
142                                     s1->pb->max_packet_size);
143     } else
144         s1->packet_size = s1->pb->max_packet_size;
145     if (s1->packet_size <= 12) {
146         av_log(s1, AV_LOG_ERROR, "Max packet size %d too low\n", s1->packet_size);
147         return AVERROR(EIO);
148     }
149     s->buf = av_malloc(s1->packet_size);
150     if (!s->buf) {
151         return AVERROR(ENOMEM);
152     }
153     s->max_payload_size = s1->packet_size - 12;
154
155     s->max_frames_per_packet = 0;
156     if (s1->max_delay > 0) {
157         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
158             int frame_size = av_get_audio_frame_duration(st->codec, 0);
159             if (!frame_size)
160                 frame_size = st->codec->frame_size;
161             if (frame_size == 0) {
162                 av_log(s1, AV_LOG_ERROR, "Cannot respect max delay: frame size = 0\n");
163             } else {
164                 s->max_frames_per_packet =
165                         av_rescale_q_rnd(s1->max_delay,
166                                          AV_TIME_BASE_Q,
167                                          (AVRational){ frame_size, st->codec->sample_rate },
168                                          AV_ROUND_DOWN);
169             }
170         }
171         if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
172             /* FIXME: We should round down here... */
173             if (st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0) {
174                 s->max_frames_per_packet = av_rescale_q(s1->max_delay,
175                                                         (AVRational){1, 1000000},
176                                                         av_inv_q(st->avg_frame_rate));
177             } else
178                 s->max_frames_per_packet = 1;
179         }
180     }
181
182     avpriv_set_pts_info(st, 32, 1, 90000);
183     switch(st->codec->codec_id) {
184     case AV_CODEC_ID_MP2:
185     case AV_CODEC_ID_MP3:
186         s->buf_ptr = s->buf + 4;
187         break;
188     case AV_CODEC_ID_MPEG1VIDEO:
189     case AV_CODEC_ID_MPEG2VIDEO:
190         break;
191     case AV_CODEC_ID_MPEG2TS:
192         n = s->max_payload_size / TS_PACKET_SIZE;
193         if (n < 1)
194             n = 1;
195         s->max_payload_size = n * TS_PACKET_SIZE;
196         s->buf_ptr = s->buf;
197         break;
198     case AV_CODEC_ID_H261:
199         if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
200             av_log(s, AV_LOG_ERROR,
201                    "Packetizing H261 is experimental and produces incorrect "
202                    "packetization for cases where GOBs don't fit into packets "
203                    "(even though most receivers may handle it just fine). "
204                    "Please set -f_strict experimental in order to enable it.\n");
205             ret = AVERROR_EXPERIMENTAL;
206             goto fail;
207         }
208         break;
209     case AV_CODEC_ID_H264:
210         /* check for H.264 MP4 syntax */
211         if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) {
212             s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
213         }
214         break;
215     case AV_CODEC_ID_HEVC:
216         /* Only check for the standardized hvcC version of extradata, keeping
217          * things simple and similar to the avcC/H264 case above, instead
218          * of trying to handle the pre-standardization versions (as in
219          * libavcodec/hevc.c). */
220         if (st->codec->extradata_size > 21 && st->codec->extradata[0] == 1) {
221             s->nal_length_size = (st->codec->extradata[21] & 0x03) + 1;
222         }
223         break;
224     case AV_CODEC_ID_VORBIS:
225     case AV_CODEC_ID_THEORA:
226         if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
227         s->max_frames_per_packet = av_clip(s->max_frames_per_packet, 1, 15);
228         s->max_payload_size -= 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
229         s->num_frames = 0;
230         goto defaultcase;
231     case AV_CODEC_ID_ADPCM_G722:
232         /* Due to a historical error, the clock rate for G722 in RTP is
233          * 8000, even if the sample rate is 16000. See RFC 3551. */
234         avpriv_set_pts_info(st, 32, 1, 8000);
235         break;
236     case AV_CODEC_ID_OPUS:
237         if (st->codec->channels > 2) {
238             av_log(s1, AV_LOG_ERROR, "Multistream opus not supported in RTP\n");
239             goto fail;
240         }
241         /* The opus RTP RFC says that all opus streams should use 48000 Hz
242          * as clock rate, since all opus sample rates can be expressed in
243          * this clock rate, and sample rate changes on the fly are supported. */
244         avpriv_set_pts_info(st, 32, 1, 48000);
245         break;
246     case AV_CODEC_ID_ILBC:
247         if (st->codec->block_align != 38 && st->codec->block_align != 50) {
248             av_log(s1, AV_LOG_ERROR, "Incorrect iLBC block size specified\n");
249             goto fail;
250         }
251         if (!s->max_frames_per_packet)
252             s->max_frames_per_packet = 1;
253         s->max_frames_per_packet = FFMIN(s->max_frames_per_packet,
254                                          s->max_payload_size / st->codec->block_align);
255         goto defaultcase;
256     case AV_CODEC_ID_AMR_NB:
257     case AV_CODEC_ID_AMR_WB:
258         if (!s->max_frames_per_packet)
259             s->max_frames_per_packet = 12;
260         if (st->codec->codec_id == AV_CODEC_ID_AMR_NB)
261             n = 31;
262         else
263             n = 61;
264         /* max_header_toc_size + the largest AMR payload must fit */
265         if (1 + s->max_frames_per_packet + n > s->max_payload_size) {
266             av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
267             goto fail;
268         }
269         if (st->codec->channels != 1) {
270             av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
271             goto fail;
272         }
273         s->num_frames = 0;
274         goto defaultcase;
275     case AV_CODEC_ID_AAC:
276         s->num_frames = 0;
277         goto defaultcase;
278     default:
279 defaultcase:
280         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
281             avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
282         }
283         s->buf_ptr = s->buf;
284         break;
285     }
286
287     return 0;
288
289 fail:
290     av_freep(&s->buf);
291     return ret;
292 }
293
294 /* send an rtcp sender report packet */
295 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time, int bye)
296 {
297     RTPMuxContext *s = s1->priv_data;
298     uint32_t rtp_ts;
299
300     av_dlog(s1, "RTCP: %02x %"PRIx64" %x\n", s->payload_type, ntp_time, s->timestamp);
301
302     s->last_rtcp_ntp_time = ntp_time;
303     rtp_ts = av_rescale_q(ntp_time - s->first_rtcp_ntp_time, (AVRational){1, 1000000},
304                           s1->streams[0]->time_base) + s->base_timestamp;
305     avio_w8(s1->pb, RTP_VERSION << 6);
306     avio_w8(s1->pb, RTCP_SR);
307     avio_wb16(s1->pb, 6); /* length in words - 1 */
308     avio_wb32(s1->pb, s->ssrc);
309     avio_wb64(s1->pb, NTP_TO_RTP_FORMAT(ntp_time));
310     avio_wb32(s1->pb, rtp_ts);
311     avio_wb32(s1->pb, s->packet_count);
312     avio_wb32(s1->pb, s->octet_count);
313
314     if (s->cname) {
315         int len = FFMIN(strlen(s->cname), 255);
316         avio_w8(s1->pb, (RTP_VERSION << 6) + 1);
317         avio_w8(s1->pb, RTCP_SDES);
318         avio_wb16(s1->pb, (7 + len + 3) / 4); /* length in words - 1 */
319
320         avio_wb32(s1->pb, s->ssrc);
321         avio_w8(s1->pb, 0x01); /* CNAME */
322         avio_w8(s1->pb, len);
323         avio_write(s1->pb, s->cname, len);
324         avio_w8(s1->pb, 0); /* END */
325         for (len = (7 + len) % 4; len % 4; len++)
326             avio_w8(s1->pb, 0);
327     }
328
329     if (bye) {
330         avio_w8(s1->pb, (RTP_VERSION << 6) | 1);
331         avio_w8(s1->pb, RTCP_BYE);
332         avio_wb16(s1->pb, 1); /* length in words - 1 */
333         avio_wb32(s1->pb, s->ssrc);
334     }
335
336     avio_flush(s1->pb);
337 }
338
339 /* send an rtp packet. sequence number is incremented, but the caller
340    must update the timestamp itself */
341 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
342 {
343     RTPMuxContext *s = s1->priv_data;
344
345     av_dlog(s1, "rtp_send_data size=%d\n", len);
346
347     /* build the RTP header */
348     avio_w8(s1->pb, RTP_VERSION << 6);
349     avio_w8(s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
350     avio_wb16(s1->pb, s->seq);
351     avio_wb32(s1->pb, s->timestamp);
352     avio_wb32(s1->pb, s->ssrc);
353
354     avio_write(s1->pb, buf1, len);
355     avio_flush(s1->pb);
356
357     s->seq = (s->seq + 1) & 0xffff;
358     s->octet_count += len;
359     s->packet_count++;
360 }
361
362 /* send an integer number of samples and compute time stamp and fill
363    the rtp send buffer before sending. */
364 static int rtp_send_samples(AVFormatContext *s1,
365                             const uint8_t *buf1, int size, int sample_size_bits)
366 {
367     RTPMuxContext *s = s1->priv_data;
368     int len, max_packet_size, n;
369     /* Calculate the number of bytes to get samples aligned on a byte border */
370     int aligned_samples_size = sample_size_bits/av_gcd(sample_size_bits, 8);
371
372     max_packet_size = (s->max_payload_size / aligned_samples_size) * aligned_samples_size;
373     /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
374     if ((sample_size_bits % 8) == 0 && ((8 * size) % sample_size_bits) != 0)
375         return AVERROR(EINVAL);
376     n = 0;
377     while (size > 0) {
378         s->buf_ptr = s->buf;
379         len = FFMIN(max_packet_size, size);
380
381         /* copy data */
382         memcpy(s->buf_ptr, buf1, len);
383         s->buf_ptr += len;
384         buf1 += len;
385         size -= len;
386         s->timestamp = s->cur_timestamp + n * 8 / sample_size_bits;
387         ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
388         n += (s->buf_ptr - s->buf);
389     }
390     return 0;
391 }
392
393 static void rtp_send_mpegaudio(AVFormatContext *s1,
394                                const uint8_t *buf1, int size)
395 {
396     RTPMuxContext *s = s1->priv_data;
397     int len, count, max_packet_size;
398
399     max_packet_size = s->max_payload_size;
400
401     /* test if we must flush because not enough space */
402     len = (s->buf_ptr - s->buf);
403     if ((len + size) > max_packet_size) {
404         if (len > 4) {
405             ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
406             s->buf_ptr = s->buf + 4;
407         }
408     }
409     if (s->buf_ptr == s->buf + 4) {
410         s->timestamp = s->cur_timestamp;
411     }
412
413     /* add the packet */
414     if (size > max_packet_size) {
415         /* big packet: fragment */
416         count = 0;
417         while (size > 0) {
418             len = max_packet_size - 4;
419             if (len > size)
420                 len = size;
421             /* build fragmented packet */
422             s->buf[0] = 0;
423             s->buf[1] = 0;
424             s->buf[2] = count >> 8;
425             s->buf[3] = count;
426             memcpy(s->buf + 4, buf1, len);
427             ff_rtp_send_data(s1, s->buf, len + 4, 0);
428             size -= len;
429             buf1 += len;
430             count += len;
431         }
432     } else {
433         if (s->buf_ptr == s->buf + 4) {
434             /* no fragmentation possible */
435             s->buf[0] = 0;
436             s->buf[1] = 0;
437             s->buf[2] = 0;
438             s->buf[3] = 0;
439         }
440         memcpy(s->buf_ptr, buf1, size);
441         s->buf_ptr += size;
442     }
443 }
444
445 static void rtp_send_raw(AVFormatContext *s1,
446                          const uint8_t *buf1, int size)
447 {
448     RTPMuxContext *s = s1->priv_data;
449     int len, max_packet_size;
450
451     max_packet_size = s->max_payload_size;
452
453     while (size > 0) {
454         len = max_packet_size;
455         if (len > size)
456             len = size;
457
458         s->timestamp = s->cur_timestamp;
459         ff_rtp_send_data(s1, buf1, len, (len == size));
460
461         buf1 += len;
462         size -= len;
463     }
464 }
465
466 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
467 static void rtp_send_mpegts_raw(AVFormatContext *s1,
468                                 const uint8_t *buf1, int size)
469 {
470     RTPMuxContext *s = s1->priv_data;
471     int len, out_len;
472
473     s->timestamp = s->cur_timestamp;
474     while (size >= TS_PACKET_SIZE) {
475         len = s->max_payload_size - (s->buf_ptr - s->buf);
476         if (len > size)
477             len = size;
478         memcpy(s->buf_ptr, buf1, len);
479         buf1 += len;
480         size -= len;
481         s->buf_ptr += len;
482
483         out_len = s->buf_ptr - s->buf;
484         if (out_len >= s->max_payload_size) {
485             ff_rtp_send_data(s1, s->buf, out_len, 0);
486             s->buf_ptr = s->buf;
487         }
488     }
489 }
490
491 static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
492 {
493     RTPMuxContext *s = s1->priv_data;
494     AVStream *st = s1->streams[0];
495     int frame_duration = av_get_audio_frame_duration(st->codec, 0);
496     int frame_size = st->codec->block_align;
497     int frames = size / frame_size;
498
499     while (frames > 0) {
500         int n = FFMIN(s->max_frames_per_packet - s->num_frames, frames);
501
502         if (!s->num_frames) {
503             s->buf_ptr = s->buf;
504             s->timestamp = s->cur_timestamp;
505         }
506         memcpy(s->buf_ptr, buf, n * frame_size);
507         frames           -= n;
508         s->num_frames    += n;
509         s->buf_ptr       += n * frame_size;
510         buf              += n * frame_size;
511         s->cur_timestamp += n * frame_duration;
512
513         if (s->num_frames == s->max_frames_per_packet) {
514             ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 1);
515             s->num_frames = 0;
516         }
517     }
518     return 0;
519 }
520
521 static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
522 {
523     RTPMuxContext *s = s1->priv_data;
524     AVStream *st = s1->streams[0];
525     int rtcp_bytes;
526     int size= pkt->size;
527
528     av_dlog(s1, "%d: write len=%d\n", pkt->stream_index, size);
529
530     rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
531         RTCP_TX_RATIO_DEN;
532     if ((s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
533                             (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) &&
534         !(s->flags & FF_RTP_FLAG_SKIP_RTCP)) {
535         rtcp_send_sr(s1, ff_ntp_time(), 0);
536         s->last_octet_count = s->octet_count;
537         s->first_packet = 0;
538     }
539     s->cur_timestamp = s->base_timestamp + pkt->pts;
540
541     switch(st->codec->codec_id) {
542     case AV_CODEC_ID_PCM_MULAW:
543     case AV_CODEC_ID_PCM_ALAW:
544     case AV_CODEC_ID_PCM_U8:
545     case AV_CODEC_ID_PCM_S8:
546         return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
547     case AV_CODEC_ID_PCM_U16BE:
548     case AV_CODEC_ID_PCM_U16LE:
549     case AV_CODEC_ID_PCM_S16BE:
550     case AV_CODEC_ID_PCM_S16LE:
551         return rtp_send_samples(s1, pkt->data, size, 16 * st->codec->channels);
552     case AV_CODEC_ID_ADPCM_G722:
553         /* The actual sample size is half a byte per sample, but since the
554          * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
555          * the correct parameter for send_samples_bits is 8 bits per stream
556          * clock. */
557         return rtp_send_samples(s1, pkt->data, size, 8 * st->codec->channels);
558     case AV_CODEC_ID_ADPCM_G726:
559         return rtp_send_samples(s1, pkt->data, size,
560                                 st->codec->bits_per_coded_sample * st->codec->channels);
561     case AV_CODEC_ID_MP2:
562     case AV_CODEC_ID_MP3:
563         rtp_send_mpegaudio(s1, pkt->data, size);
564         break;
565     case AV_CODEC_ID_MPEG1VIDEO:
566     case AV_CODEC_ID_MPEG2VIDEO:
567         ff_rtp_send_mpegvideo(s1, pkt->data, size);
568         break;
569     case AV_CODEC_ID_AAC:
570         if (s->flags & FF_RTP_FLAG_MP4A_LATM)
571             ff_rtp_send_latm(s1, pkt->data, size);
572         else
573             ff_rtp_send_aac(s1, pkt->data, size);
574         break;
575     case AV_CODEC_ID_AMR_NB:
576     case AV_CODEC_ID_AMR_WB:
577         ff_rtp_send_amr(s1, pkt->data, size);
578         break;
579     case AV_CODEC_ID_MPEG2TS:
580         rtp_send_mpegts_raw(s1, pkt->data, size);
581         break;
582     case AV_CODEC_ID_H264:
583         ff_rtp_send_h264(s1, pkt->data, size);
584         break;
585     case AV_CODEC_ID_H261:
586         ff_rtp_send_h261(s1, pkt->data, size);
587         break;
588     case AV_CODEC_ID_H263:
589         if (s->flags & FF_RTP_FLAG_RFC2190) {
590             int mb_info_size = 0;
591             const uint8_t *mb_info =
592                 av_packet_get_side_data(pkt, AV_PKT_DATA_H263_MB_INFO,
593                                         &mb_info_size);
594             if (!mb_info) {
595                 av_log(s1, AV_LOG_ERROR, "failed to allocate side data\n");
596                 return AVERROR(ENOMEM);
597             }
598             ff_rtp_send_h263_rfc2190(s1, pkt->data, size, mb_info, mb_info_size);
599             break;
600         }
601         /* Fallthrough */
602     case AV_CODEC_ID_H263P:
603         ff_rtp_send_h263(s1, pkt->data, size);
604         break;
605     case AV_CODEC_ID_HEVC:
606         ff_rtp_send_hevc(s1, pkt->data, size);
607         break;
608     case AV_CODEC_ID_VORBIS:
609     case AV_CODEC_ID_THEORA:
610         ff_rtp_send_xiph(s1, pkt->data, size);
611         break;
612     case AV_CODEC_ID_VP8:
613         ff_rtp_send_vp8(s1, pkt->data, size);
614         break;
615     case AV_CODEC_ID_ILBC:
616         rtp_send_ilbc(s1, pkt->data, size);
617         break;
618     case AV_CODEC_ID_MJPEG:
619         ff_rtp_send_jpeg(s1, pkt->data, size);
620         break;
621     case AV_CODEC_ID_OPUS:
622         if (size > s->max_payload_size) {
623             av_log(s1, AV_LOG_ERROR,
624                    "Packet size %d too large for max RTP payload size %d\n",
625                    size, s->max_payload_size);
626             return AVERROR(EINVAL);
627         }
628         /* Intentional fallthrough */
629     default:
630         /* better than nothing : send the codec raw data */
631         rtp_send_raw(s1, pkt->data, size);
632         break;
633     }
634     return 0;
635 }
636
637 static int rtp_write_trailer(AVFormatContext *s1)
638 {
639     RTPMuxContext *s = s1->priv_data;
640
641     /* If the caller closes and recreates ->pb, this might actually
642      * be NULL here even if it was successfully allocated at the start. */
643     if (s1->pb && (s->flags & FF_RTP_FLAG_SEND_BYE))
644         rtcp_send_sr(s1, ff_ntp_time(), 1);
645     av_freep(&s->buf);
646
647     return 0;
648 }
649
650 AVOutputFormat ff_rtp_muxer = {
651     .name              = "rtp",
652     .long_name         = NULL_IF_CONFIG_SMALL("RTP output"),
653     .priv_data_size    = sizeof(RTPMuxContext),
654     .audio_codec       = AV_CODEC_ID_PCM_MULAW,
655     .video_codec       = AV_CODEC_ID_MPEG4,
656     .write_header      = rtp_write_header,
657     .write_packet      = rtp_write_packet,
658     .write_trailer     = rtp_write_trailer,
659     .priv_class        = &rtp_muxer_class,
660     .flags             = AVFMT_TS_NONSTRICT,
661 };