3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/mathematics.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/random_seed.h"
29 #include "libavutil/opt.h"
33 static const AVOption options
[] = {
34 FF_RTP_FLAG_OPTS(RTPMuxContext
, flags
),
35 { "payload_type", "Specify RTP payload type", offsetof(RTPMuxContext
, payload_type
), AV_OPT_TYPE_INT
, {.i64
= -1 }, -1, 127, AV_OPT_FLAG_ENCODING_PARAM
},
36 { "ssrc", "Stream identifier", offsetof(RTPMuxContext
, ssrc
), AV_OPT_TYPE_INT
, { .i64
= 0 }, INT_MIN
, INT_MAX
, AV_OPT_FLAG_ENCODING_PARAM
},
37 { "cname", "CNAME to include in RTCP SR packets", offsetof(RTPMuxContext
, cname
), AV_OPT_TYPE_STRING
, { .str
= NULL
}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM
},
38 { "seq", "Starting sequence number", offsetof(RTPMuxContext
, seq
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, 65535, AV_OPT_FLAG_ENCODING_PARAM
},
42 static const AVClass rtp_muxer_class
= {
43 .class_name
= "RTP muxer",
44 .item_name
= av_default_item_name
,
46 .version
= LIBAVUTIL_VERSION_INT
,
49 #define RTCP_SR_SIZE 28
51 static int is_supported(enum AVCodecID id
)
54 case AV_CODEC_ID_DIRAC
:
55 case AV_CODEC_ID_H261
:
56 case AV_CODEC_ID_H263
:
57 case AV_CODEC_ID_H263P
:
58 case AV_CODEC_ID_H264
:
59 case AV_CODEC_ID_HEVC
:
60 case AV_CODEC_ID_MPEG1VIDEO
:
61 case AV_CODEC_ID_MPEG2VIDEO
:
62 case AV_CODEC_ID_MPEG4
:
66 case AV_CODEC_ID_PCM_ALAW
:
67 case AV_CODEC_ID_PCM_MULAW
:
68 case AV_CODEC_ID_PCM_S8
:
69 case AV_CODEC_ID_PCM_S16BE
:
70 case AV_CODEC_ID_PCM_S16LE
:
71 case AV_CODEC_ID_PCM_S24BE
:
72 case AV_CODEC_ID_PCM_U16BE
:
73 case AV_CODEC_ID_PCM_U16LE
:
74 case AV_CODEC_ID_PCM_U8
:
75 case AV_CODEC_ID_MPEG2TS
:
76 case AV_CODEC_ID_AMR_NB
:
77 case AV_CODEC_ID_AMR_WB
:
78 case AV_CODEC_ID_VORBIS
:
79 case AV_CODEC_ID_THEORA
:
83 case AV_CODEC_ID_ADPCM_G722
:
84 case AV_CODEC_ID_ADPCM_G726
:
85 case AV_CODEC_ID_ADPCM_G726LE
:
86 case AV_CODEC_ID_ILBC
:
87 case AV_CODEC_ID_MJPEG
:
88 case AV_CODEC_ID_SPEEX
:
89 case AV_CODEC_ID_OPUS
:
90 case AV_CODEC_ID_RAWVIDEO
:
91 case AV_CODEC_ID_BITPACKED
:
92 case AV_CODEC_ID_G728
:
99 static int rtp_write_header(AVFormatContext
*s1
)
101 RTPMuxContext
*s
= s1
->priv_data
;
102 int n
, ret
= AVERROR(EINVAL
);
105 if (s1
->nb_streams
!= 1) {
106 av_log(s1
, AV_LOG_ERROR
, "Only one stream supported in the RTP muxer\n");
107 return AVERROR(EINVAL
);
110 if (!is_supported(st
->codecpar
->codec_id
)) {
111 av_log(s1
, AV_LOG_ERROR
, "Unsupported codec %s\n", avcodec_get_name(st
->codecpar
->codec_id
));
116 if (s
->payload_type
< 0) {
117 /* Re-validate non-dynamic payload types */
118 if (st
->id
< RTP_PT_PRIVATE
)
119 st
->id
= ff_rtp_get_payload_type(s1
, st
->codecpar
, -1);
121 s
->payload_type
= st
->id
;
123 /* private option takes priority */
124 st
->id
= s
->payload_type
;
127 s
->base_timestamp
= av_get_random_seed();
128 s
->timestamp
= s
->base_timestamp
;
129 s
->cur_timestamp
= 0;
131 s
->ssrc
= av_get_random_seed();
133 s
->first_rtcp_ntp_time
= ff_ntp_time();
134 if (s1
->start_time_realtime
!= 0 && s1
->start_time_realtime
!= AV_NOPTS_VALUE
)
135 /* Round the NTP time to whole milliseconds. */
136 s
->first_rtcp_ntp_time
= (s1
->start_time_realtime
/ 1000) * 1000 +
138 // Pick a random sequence start number, but in the lower end of the
139 // available range, so that any wraparound doesn't happen immediately.
140 // (Immediate wraparound would be an issue for SRTP.)
142 if (s1
->flags
& AVFMT_FLAG_BITEXACT
) {
145 s
->seq
= av_get_random_seed() & 0x0fff;
147 s
->seq
&= 0xffff; // Use the given parameter, wrapped to the right interval
149 if (s1
->packet_size
) {
150 if (s1
->pb
->max_packet_size
)
151 s1
->packet_size
= FFMIN(s1
->packet_size
,
152 s1
->pb
->max_packet_size
);
154 s1
->packet_size
= s1
->pb
->max_packet_size
;
155 if (s1
->packet_size
<= 12) {
156 av_log(s1
, AV_LOG_ERROR
, "Max packet size %u too low\n", s1
->packet_size
);
159 s
->buf
= av_malloc(s1
->packet_size
);
161 return AVERROR(ENOMEM
);
163 s
->max_payload_size
= s1
->packet_size
- 12;
165 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
166 avpriv_set_pts_info(st
, 32, 1, st
->codecpar
->sample_rate
);
168 avpriv_set_pts_info(st
, 32, 1, 90000);
171 switch(st
->codecpar
->codec_id
) {
172 case AV_CODEC_ID_MP2
:
173 case AV_CODEC_ID_MP3
:
174 s
->buf_ptr
= s
->buf
+ 4;
175 avpriv_set_pts_info(st
, 32, 1, 90000);
177 case AV_CODEC_ID_MPEG1VIDEO
:
178 case AV_CODEC_ID_MPEG2VIDEO
:
180 case AV_CODEC_ID_MPEG2TS
:
181 n
= s
->max_payload_size
/ TS_PACKET_SIZE
;
184 s
->max_payload_size
= n
* TS_PACKET_SIZE
;
186 case AV_CODEC_ID_DIRAC
:
187 if (s1
->strict_std_compliance
> FF_COMPLIANCE_EXPERIMENTAL
) {
188 av_log(s
, AV_LOG_ERROR
,
189 "Packetizing VC-2 is experimental and does not use all values "
190 "of the specification "
191 "(even though most receivers may handle it just fine). "
192 "Please set -strict experimental in order to enable it.\n");
193 ret
= AVERROR_EXPERIMENTAL
;
197 case AV_CODEC_ID_H261
:
198 if (s1
->strict_std_compliance
> FF_COMPLIANCE_EXPERIMENTAL
) {
199 av_log(s
, AV_LOG_ERROR
,
200 "Packetizing H.261 is experimental and produces incorrect "
201 "packetization for cases where GOBs don't fit into packets "
202 "(even though most receivers may handle it just fine). "
203 "Please set -f_strict experimental in order to enable it.\n");
204 ret
= AVERROR_EXPERIMENTAL
;
208 case AV_CODEC_ID_H264
:
209 /* check for H.264 MP4 syntax */
210 if (st
->codecpar
->extradata_size
> 4 && st
->codecpar
->extradata
[0] == 1) {
211 s
->nal_length_size
= (st
->codecpar
->extradata
[4] & 0x03) + 1;
214 case AV_CODEC_ID_HEVC
:
215 /* Only check for the standardized hvcC version of extradata, keeping
216 * things simple and similar to the avcC/H.264 case above, instead
217 * of trying to handle the pre-standardization versions (as in
218 * libavcodec/hevc.c). */
219 if (st
->codecpar
->extradata_size
> 21 && st
->codecpar
->extradata
[0] == 1) {
220 s
->nal_length_size
= (st
->codecpar
->extradata
[21] & 0x03) + 1;
223 case AV_CODEC_ID_MJPEG
:
224 case AV_CODEC_ID_BITPACKED
:
225 case AV_CODEC_ID_RAWVIDEO
:
226 if (st
->codecpar
->width
<= 0 || st
->codecpar
->height
<= 0) {
227 av_log(s1
, AV_LOG_ERROR
, "dimensions not set\n");
228 return AVERROR(EINVAL
);
231 case AV_CODEC_ID_VP9
:
232 if (s1
->strict_std_compliance
> FF_COMPLIANCE_EXPERIMENTAL
) {
233 av_log(s
, AV_LOG_ERROR
,
234 "Packetizing VP9 is experimental and its specification is "
235 "still in draft state. "
236 "Please set -strict experimental in order to enable it.\n");
237 ret
= AVERROR_EXPERIMENTAL
;
241 case AV_CODEC_ID_AV1
:
242 if (s1
->strict_std_compliance
> FF_COMPLIANCE_EXPERIMENTAL
) {
243 av_log(s
, AV_LOG_ERROR
,
244 "Packetizing AV1 is experimental and its specification is "
245 "still in draft state. "
246 "Please set -strict experimental in order to enable it.\n");
247 ret
= AVERROR_EXPERIMENTAL
;
251 case AV_CODEC_ID_VORBIS
:
252 case AV_CODEC_ID_THEORA
:
253 s
->max_frames_per_packet
= 15;
255 case AV_CODEC_ID_ADPCM_G722
:
256 /* Due to a historical error, the clock rate for G722 in RTP is
257 * 8000, even if the sample rate is 16000. See RFC 3551. */
258 avpriv_set_pts_info(st
, 32, 1, 8000);
260 case AV_CODEC_ID_OPUS
:
261 if (st
->codecpar
->ch_layout
.nb_channels
> 2) {
262 av_log(s1
, AV_LOG_ERROR
, "Multistream opus not supported in RTP\n");
265 /* The opus RTP RFC says that all opus streams should use 48000 Hz
266 * as clock rate, since all opus sample rates can be expressed in
267 * this clock rate, and sample rate changes on the fly are supported. */
268 avpriv_set_pts_info(st
, 32, 1, 48000);
270 case AV_CODEC_ID_ILBC
:
271 if (st
->codecpar
->block_align
!= 38 && st
->codecpar
->block_align
!= 50) {
272 av_log(s1
, AV_LOG_ERROR
, "Incorrect iLBC block size specified\n");
275 s
->max_frames_per_packet
= s
->max_payload_size
/ st
->codecpar
->block_align
;
277 case AV_CODEC_ID_AMR_NB
:
278 case AV_CODEC_ID_AMR_WB
:
279 s
->max_frames_per_packet
= 50;
280 if (st
->codecpar
->codec_id
== AV_CODEC_ID_AMR_NB
)
284 /* max_header_toc_size + the largest AMR payload must fit */
285 if (1 + s
->max_frames_per_packet
+ n
> s
->max_payload_size
) {
286 av_log(s1
, AV_LOG_ERROR
, "RTP max payload size too small for AMR\n");
289 if (st
->codecpar
->ch_layout
.nb_channels
!= 1) {
290 av_log(s1
, AV_LOG_ERROR
, "Only mono is supported\n");
294 case AV_CODEC_ID_AAC
:
295 s
->max_frames_per_packet
= 50;
308 /* send an rtcp sender report packet */
309 static void rtcp_send_sr(AVFormatContext
*s1
, int64_t ntp_time
, int bye
)
311 RTPMuxContext
*s
= s1
->priv_data
;
314 av_log(s1
, AV_LOG_TRACE
, "RTCP: %02x %"PRIx64
" %"PRIx32
"\n", s
->payload_type
, ntp_time
, s
->timestamp
);
316 s
->last_rtcp_ntp_time
= ntp_time
;
317 rtp_ts
= av_rescale_q(ntp_time
- s
->first_rtcp_ntp_time
, (AVRational
){1, 1000000},
318 s1
->streams
[0]->time_base
) + s
->base_timestamp
;
319 avio_w8(s1
->pb
, RTP_VERSION
<< 6);
320 avio_w8(s1
->pb
, RTCP_SR
);
321 avio_wb16(s1
->pb
, 6); /* length in words - 1 */
322 avio_wb32(s1
->pb
, s
->ssrc
);
323 avio_wb32(s1
->pb
, ntp_time
/ 1000000);
324 avio_wb32(s1
->pb
, ((ntp_time
% 1000000) << 32) / 1000000);
325 avio_wb32(s1
->pb
, rtp_ts
);
326 avio_wb32(s1
->pb
, s
->packet_count
);
327 avio_wb32(s1
->pb
, s
->octet_count
);
330 int len
= FFMIN(strlen(s
->cname
), 255);
331 avio_w8(s1
->pb
, (RTP_VERSION
<< 6) + 1);
332 avio_w8(s1
->pb
, RTCP_SDES
);
333 avio_wb16(s1
->pb
, (7 + len
+ 3) / 4); /* length in words - 1 */
335 avio_wb32(s1
->pb
, s
->ssrc
);
336 avio_w8(s1
->pb
, 0x01); /* CNAME */
337 avio_w8(s1
->pb
, len
);
338 avio_write(s1
->pb
, s
->cname
, len
);
339 avio_w8(s1
->pb
, 0); /* END */
340 for (len
= (7 + len
) % 4; len
% 4; len
++)
345 avio_w8(s1
->pb
, (RTP_VERSION
<< 6) | 1);
346 avio_w8(s1
->pb
, RTCP_BYE
);
347 avio_wb16(s1
->pb
, 1); /* length in words - 1 */
348 avio_wb32(s1
->pb
, s
->ssrc
);
354 /* send an rtp packet. sequence number is incremented, but the caller
355 must update the timestamp itself */
356 void ff_rtp_send_data(AVFormatContext
*s1
, const uint8_t *buf1
, int len
, int m
)
358 RTPMuxContext
*s
= s1
->priv_data
;
360 av_log(s1
, AV_LOG_TRACE
, "rtp_send_data size=%d\n", len
);
362 /* build the RTP header */
363 avio_w8(s1
->pb
, RTP_VERSION
<< 6);
364 avio_w8(s1
->pb
, (s
->payload_type
& 0x7f) | ((m
& 0x01) << 7));
365 avio_wb16(s1
->pb
, s
->seq
);
366 avio_wb32(s1
->pb
, s
->timestamp
);
367 avio_wb32(s1
->pb
, s
->ssrc
);
369 avio_write(s1
->pb
, buf1
, len
);
372 s
->seq
= (s
->seq
+ 1) & 0xffff;
373 s
->octet_count
+= len
;
377 /* send an integer number of samples and compute time stamp and fill
378 the rtp send buffer before sending. */
379 static int rtp_send_samples(AVFormatContext
*s1
,
380 const uint8_t *buf1
, int size
, int sample_size_bits
)
382 RTPMuxContext
*s
= s1
->priv_data
;
383 int len
, max_packet_size
, n
;
384 /* Calculate the number of bytes to get samples aligned on a byte border */
385 int aligned_samples_size
= sample_size_bits
/av_gcd(sample_size_bits
, 8);
387 max_packet_size
= (s
->max_payload_size
/ aligned_samples_size
) * aligned_samples_size
;
388 /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
389 if ((sample_size_bits
% 8) == 0 && ((8 * size
) % sample_size_bits
) != 0)
390 return AVERROR(EINVAL
);
394 len
= FFMIN(max_packet_size
, size
);
397 memcpy(s
->buf_ptr
, buf1
, len
);
401 s
->timestamp
= s
->cur_timestamp
+ n
* 8 / sample_size_bits
;
402 ff_rtp_send_data(s1
, s
->buf
, s
->buf_ptr
- s
->buf
, 0);
403 n
+= (s
->buf_ptr
- s
->buf
);
408 static void rtp_send_mpegaudio(AVFormatContext
*s1
,
409 const uint8_t *buf1
, int size
)
411 RTPMuxContext
*s
= s1
->priv_data
;
412 int len
, count
, max_packet_size
;
414 max_packet_size
= s
->max_payload_size
;
416 /* test if we must flush because not enough space */
417 len
= (s
->buf_ptr
- s
->buf
);
418 if ((len
+ size
) > max_packet_size
) {
420 ff_rtp_send_data(s1
, s
->buf
, s
->buf_ptr
- s
->buf
, 0);
421 s
->buf_ptr
= s
->buf
+ 4;
424 if (s
->buf_ptr
== s
->buf
+ 4) {
425 s
->timestamp
= s
->cur_timestamp
;
429 if (size
> max_packet_size
) {
430 /* big packet: fragment */
433 len
= max_packet_size
- 4;
436 /* build fragmented packet */
439 s
->buf
[2] = count
>> 8;
441 memcpy(s
->buf
+ 4, buf1
, len
);
442 ff_rtp_send_data(s1
, s
->buf
, len
+ 4, 0);
448 if (s
->buf_ptr
== s
->buf
+ 4) {
449 /* no fragmentation possible */
455 memcpy(s
->buf_ptr
, buf1
, size
);
460 static void rtp_send_raw(AVFormatContext
*s1
,
461 const uint8_t *buf1
, int size
)
463 RTPMuxContext
*s
= s1
->priv_data
;
464 int len
, max_packet_size
;
466 max_packet_size
= s
->max_payload_size
;
469 len
= max_packet_size
;
473 s
->timestamp
= s
->cur_timestamp
;
474 ff_rtp_send_data(s1
, buf1
, len
, (len
== size
));
481 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
482 static void rtp_send_mpegts_raw(AVFormatContext
*s1
,
483 const uint8_t *buf1
, int size
)
485 RTPMuxContext
*s
= s1
->priv_data
;
488 s
->timestamp
= s
->cur_timestamp
;
489 while (size
>= TS_PACKET_SIZE
) {
490 len
= s
->max_payload_size
- (s
->buf_ptr
- s
->buf
);
493 memcpy(s
->buf_ptr
, buf1
, len
);
498 out_len
= s
->buf_ptr
- s
->buf
;
499 if (out_len
>= s
->max_payload_size
) {
500 ff_rtp_send_data(s1
, s
->buf
, out_len
, 0);
506 static int rtp_send_ilbc(AVFormatContext
*s1
, const uint8_t *buf
, int size
)
508 RTPMuxContext
*s
= s1
->priv_data
;
509 AVStream
*st
= s1
->streams
[0];
510 int frame_duration
= av_get_audio_frame_duration2(st
->codecpar
, 0);
511 int frame_size
= st
->codecpar
->block_align
;
512 int frames
= size
/ frame_size
;
515 if (s
->num_frames
> 0 &&
516 av_compare_ts(s
->cur_timestamp
- s
->timestamp
, st
->time_base
,
517 s1
->max_delay
, AV_TIME_BASE_Q
) >= 0) {
518 ff_rtp_send_data(s1
, s
->buf
, s
->buf_ptr
- s
->buf
, 1);
522 if (!s
->num_frames
) {
524 s
->timestamp
= s
->cur_timestamp
;
526 memcpy(s
->buf_ptr
, buf
, frame_size
);
529 s
->buf_ptr
+= frame_size
;
531 s
->cur_timestamp
+= frame_duration
;
533 if (s
->num_frames
== s
->max_frames_per_packet
) {
534 ff_rtp_send_data(s1
, s
->buf
, s
->buf_ptr
- s
->buf
, 1);
541 static int rtp_write_packet(AVFormatContext
*s1
, AVPacket
*pkt
)
543 RTPMuxContext
*s
= s1
->priv_data
;
544 AVStream
*st
= s1
->streams
[0];
548 av_log(s1
, AV_LOG_TRACE
, "%d: write len=%d\n", pkt
->stream_index
, size
);
550 rtcp_bytes
= ((s
->octet_count
- s
->last_octet_count
) * RTCP_TX_RATIO_NUM
) /
552 if ((s
->first_packet
|| ((rtcp_bytes
>= RTCP_SR_SIZE
) &&
553 (ff_ntp_time() - s
->last_rtcp_ntp_time
> 5000000))) &&
554 !(s
->flags
& FF_RTP_FLAG_SKIP_RTCP
)) {
555 rtcp_send_sr(s1
, ff_ntp_time(), 0);
556 s
->last_octet_count
= s
->octet_count
;
559 s
->cur_timestamp
= s
->base_timestamp
+ pkt
->pts
;
561 switch(st
->codecpar
->codec_id
) {
562 case AV_CODEC_ID_PCM_MULAW
:
563 case AV_CODEC_ID_PCM_ALAW
:
564 case AV_CODEC_ID_PCM_U8
:
565 case AV_CODEC_ID_PCM_S8
:
566 return rtp_send_samples(s1
, pkt
->data
, size
, 8 * st
->codecpar
->ch_layout
.nb_channels
);
567 case AV_CODEC_ID_PCM_U16BE
:
568 case AV_CODEC_ID_PCM_U16LE
:
569 case AV_CODEC_ID_PCM_S16BE
:
570 case AV_CODEC_ID_PCM_S16LE
:
571 return rtp_send_samples(s1
, pkt
->data
, size
, 16 * st
->codecpar
->ch_layout
.nb_channels
);
572 case AV_CODEC_ID_PCM_S24BE
:
573 return rtp_send_samples(s1
, pkt
->data
, size
, 24 * st
->codecpar
->ch_layout
.nb_channels
);
574 case AV_CODEC_ID_ADPCM_G722
:
575 /* The actual sample size is half a byte per sample, but since the
576 * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
577 * the correct parameter for send_samples_bits is 8 bits per stream
579 return rtp_send_samples(s1
, pkt
->data
, size
, 8 * st
->codecpar
->ch_layout
.nb_channels
);
580 case AV_CODEC_ID_ADPCM_G726
:
581 case AV_CODEC_ID_ADPCM_G726LE
:
582 return rtp_send_samples(s1
, pkt
->data
, size
,
583 st
->codecpar
->bits_per_coded_sample
* st
->codecpar
->ch_layout
.nb_channels
);
584 case AV_CODEC_ID_MP2
:
585 case AV_CODEC_ID_MP3
:
586 rtp_send_mpegaudio(s1
, pkt
->data
, size
);
588 case AV_CODEC_ID_MPEG1VIDEO
:
589 case AV_CODEC_ID_MPEG2VIDEO
:
590 ff_rtp_send_mpegvideo(s1
, pkt
->data
, size
);
592 case AV_CODEC_ID_AAC
:
593 if (s
->flags
& FF_RTP_FLAG_MP4A_LATM
)
594 ff_rtp_send_latm(s1
, pkt
->data
, size
);
596 ff_rtp_send_aac(s1
, pkt
->data
, size
);
598 case AV_CODEC_ID_AMR_NB
:
599 case AV_CODEC_ID_AMR_WB
:
600 ff_rtp_send_amr(s1
, pkt
->data
, size
);
602 case AV_CODEC_ID_AV1
:
603 ff_rtp_send_av1(s1
, pkt
->data
, size
, (pkt
->flags
& AV_PKT_FLAG_KEY
) ? 1 : 0);
605 case AV_CODEC_ID_MPEG2TS
:
606 rtp_send_mpegts_raw(s1
, pkt
->data
, size
);
608 case AV_CODEC_ID_DIRAC
:
609 ff_rtp_send_vc2hq(s1
, pkt
->data
, size
, st
->codecpar
->field_order
!= AV_FIELD_PROGRESSIVE
? 1 : 0);
611 case AV_CODEC_ID_H264
:
612 ff_rtp_send_h264_hevc(s1
, pkt
->data
, size
);
614 case AV_CODEC_ID_H261
:
615 ff_rtp_send_h261(s1
, pkt
->data
, size
);
617 case AV_CODEC_ID_H263
:
618 if (s
->flags
& FF_RTP_FLAG_RFC2190
) {
620 const uint8_t *mb_info
=
621 av_packet_get_side_data(pkt
, AV_PKT_DATA_H263_MB_INFO
,
623 ff_rtp_send_h263_rfc2190(s1
, pkt
->data
, size
, mb_info
, mb_info_size
);
627 case AV_CODEC_ID_H263P
:
628 ff_rtp_send_h263(s1
, pkt
->data
, size
);
630 case AV_CODEC_ID_HEVC
:
631 ff_rtp_send_h264_hevc(s1
, pkt
->data
, size
);
633 case AV_CODEC_ID_VORBIS
:
634 case AV_CODEC_ID_THEORA
:
635 ff_rtp_send_xiph(s1
, pkt
->data
, size
);
637 case AV_CODEC_ID_VP8
:
638 ff_rtp_send_vp8(s1
, pkt
->data
, size
);
640 case AV_CODEC_ID_VP9
:
641 ff_rtp_send_vp9(s1
, pkt
->data
, size
);
643 case AV_CODEC_ID_ILBC
:
644 rtp_send_ilbc(s1
, pkt
->data
, size
);
646 case AV_CODEC_ID_MJPEG
:
647 ff_rtp_send_jpeg(s1
, pkt
->data
, size
);
649 case AV_CODEC_ID_BITPACKED
:
650 case AV_CODEC_ID_RAWVIDEO
: {
651 int interlaced
= st
->codecpar
->field_order
!= AV_FIELD_PROGRESSIVE
;
653 ff_rtp_send_raw_rfc4175(s1
, pkt
->data
, size
, interlaced
, 0);
655 ff_rtp_send_raw_rfc4175(s1
, pkt
->data
, size
, interlaced
, 1);
658 case AV_CODEC_ID_OPUS
:
659 if (size
> s
->max_payload_size
) {
660 av_log(s1
, AV_LOG_ERROR
,
661 "Packet size %d too large for max RTP payload size %d\n",
662 size
, s
->max_payload_size
);
663 return AVERROR(EINVAL
);
665 /* Intentional fallthrough */
667 /* better than nothing : send the codec raw data */
668 rtp_send_raw(s1
, pkt
->data
, size
);
674 static int rtp_write_trailer(AVFormatContext
*s1
)
676 RTPMuxContext
*s
= s1
->priv_data
;
678 /* If the caller closes and recreates ->pb, this might actually
679 * be NULL here even if it was successfully allocated at the start. */
680 if (s1
->pb
&& (s
->flags
& FF_RTP_FLAG_SEND_BYE
))
681 rtcp_send_sr(s1
, ff_ntp_time(), 1);
687 const FFOutputFormat ff_rtp_muxer
= {
689 .p
.long_name
= NULL_IF_CONFIG_SMALL("RTP output"),
690 .priv_data_size
= sizeof(RTPMuxContext
),
691 .p
.audio_codec
= AV_CODEC_ID_PCM_MULAW
,
692 .p
.video_codec
= AV_CODEC_ID_MPEG4
,
693 .write_header
= rtp_write_header
,
694 .write_packet
= rtp_write_packet
,
695 .write_trailer
= rtp_write_trailer
,
696 .p
.priv_class
= &rtp_muxer_class
,
697 .p
.flags
= AVFMT_NODIMENSIONS
| AVFMT_TS_NONSTRICT
,