FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/rtpenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 124 375 33.1%
Functions: 8 11 72.7%
Branches: 36 165 21.8%

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