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