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