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
29
31
39 };
40
46 };
47
48 #define RTCP_SR_SIZE 28
49
51 {
52 switch(id) {
90 return 1;
91 default:
92 return 0;
93 }
94 }
95
97 {
101
102 if (
s1->nb_streams != 1) {
105 }
109
110 return -1;
111 }
112
113 if (
s->payload_type < 0) {
114 /* Re-validate non-dynamic payload types */
117
118 s->payload_type = st->
id;
119 } else {
120 /* private option takes priority */
121 st->
id =
s->payload_type;
122 }
123
125 s->timestamp =
s->base_timestamp;
126 s->cur_timestamp = 0;
132 /* Round the NTP time to whole milliseconds. */
133 s->first_rtcp_ntp_time = (
s1->start_time_realtime / 1000) * 1000 +
135 // Pick a random sequence start number, but in the lower end of the
136 // available range, so that any wraparound doesn't happen immediately.
137 // (Immediate wraparound would be an issue for SRTP.)
141 } else
143 } else
144 s->seq &= 0xffff;
// Use the given parameter, wrapped to the right interval
145
146 if (
s1->packet_size) {
147 if (
s1->pb->max_packet_size)
149 s1->pb->max_packet_size);
150 } else
151 s1->packet_size =
s1->pb->max_packet_size;
152 if (
s1->packet_size <= 12) {
155 }
159 }
160 s->max_payload_size =
s1->packet_size - 12;
161
164 } else {
166 }
171 s->buf_ptr =
s->buf + 4;
173 break;
176 break;
179 if (n < 1)
180 n = 1;
182 break;
186 "Packetizing VC-2 is experimental and does not use all values "
187 "of the specification "
188 "(even though most receivers may handle it just fine). "
189 "Please set -strict experimental in order to enable it.\n");
192 }
193 break;
197 "Packetizing H.261 is experimental and produces incorrect "
198 "packetization for cases where GOBs don't fit into packets "
199 "(even though most receivers may handle it just fine). "
200 "Please set -f_strict experimental in order to enable it.\n");
203 }
204 break;
206 /* check for H.264 MP4 syntax */
209 }
210 break;
212 /* Only check for the standardized hvcC version of extradata, keeping
213 * things simple and similar to the avcC/H.264 case above, instead
214 * of trying to handle the pre-standardization versions (as in
215 * libavcodec/hevc.c). */
218 }
219 break;
223 "Packetizing VP9 is experimental and its specification is "
224 "still in draft state. "
225 "Please set -strict experimental in order to enable it.\n");
228 }
229 break;
232 s->max_frames_per_packet = 15;
233 break;
235 /* Due to a historical error, the clock rate for G722 in RTP is
236 * 8000, even if the sample rate is 16000. See RFC 3551. */
238 break;
243 }
244 /* The opus RTP RFC says that all opus streams should use 48000 Hz
245 * as clock rate, since all opus sample rates can be expressed in
246 * this clock rate, and sample rate changes on the fly are supported. */
248 break;
253 }
255 break;
258 s->max_frames_per_packet = 50;
260 n = 31;
261 else
262 n = 61;
263 /* max_header_toc_size + the largest AMR payload must fit */
264 if (1 +
s->max_frames_per_packet + n >
s->max_payload_size) {
267 }
271 }
272 break;
274 s->max_frames_per_packet = 50;
275 break;
276 default:
277 break;
278 }
279
280 return 0;
281
285 }
286
287 /* send an rtcp sender report packet */
289 {
291 uint32_t rtp_ts;
292
294
295 s->last_rtcp_ntp_time = ntp_time;
297 s1->streams[0]->time_base) +
s->base_timestamp;
303 avio_wb32(
s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
307
313
321 }
322
323 if (bye) {
328 }
329
331 }
332
333 /* send an rtp packet. sequence number is incremented, but the caller
334 must update the timestamp itself */
336 {
338
340
341 /* build the RTP header */
343 avio_w8(
s1->pb, (
s->payload_type & 0x7f) | ((m & 0x01) << 7));
347
350
351 s->seq = (
s->seq + 1) & 0xffff;
352 s->octet_count +=
len;
354 }
355
356 /* send an integer number of samples and compute time stamp and fill
357 the rtp send buffer before sending. */
359 const uint8_t *buf1,
int size,
int sample_size_bits)
360 {
362 int len, max_packet_size, n;
363 /* Calculate the number of bytes to get samples aligned on a byte border */
364 int aligned_samples_size = sample_size_bits/
av_gcd(sample_size_bits, 8);
365
366 max_packet_size = (
s->max_payload_size / aligned_samples_size) * aligned_samples_size;
367 /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
368 if ((sample_size_bits % 8) == 0 && ((8 *
size) % sample_size_bits) != 0)
370 n = 0;
374
375 /* copy data */
376 memcpy(
s->buf_ptr, buf1,
len);
380 s->timestamp =
s->cur_timestamp + n * 8 / sample_size_bits;
382 n += (
s->buf_ptr -
s->buf);
383 }
384 return 0;
385 }
386
388 const uint8_t *buf1,
int size)
389 {
391 int len, count, max_packet_size;
392
393 max_packet_size =
s->max_payload_size;
394
395 /* test if we must flush because not enough space */
396 len = (
s->buf_ptr -
s->buf);
397 if ((
len +
size) > max_packet_size) {
400 s->buf_ptr =
s->buf + 4;
401 }
402 }
403 if (
s->buf_ptr ==
s->buf + 4) {
404 s->timestamp =
s->cur_timestamp;
405 }
406
407 /* add the packet */
408 if (
size > max_packet_size) {
409 /* big packet: fragment */
410 count = 0;
412 len = max_packet_size - 4;
415 /* build fragmented packet */
418 s->buf[2] = count >> 8;
420 memcpy(
s->buf + 4, buf1,
len);
425 }
426 } else {
427 if (
s->buf_ptr ==
s->buf + 4) {
428 /* no fragmentation possible */
433 }
434 memcpy(
s->buf_ptr, buf1,
size);
436 }
437 }
438
440 const uint8_t *buf1,
int size)
441 {
443 int len, max_packet_size;
444
445 max_packet_size =
s->max_payload_size;
446
448 len = max_packet_size;
451
452 s->timestamp =
s->cur_timestamp;
454
457 }
458 }
459
460 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
462 const uint8_t *buf1,
int size)
463 {
466
467 s->timestamp =
s->cur_timestamp;
469 len =
s->max_payload_size - (
s->buf_ptr -
s->buf);
472 memcpy(
s->buf_ptr, buf1,
len);
476
477 out_len =
s->buf_ptr -
s->buf;
478 if (out_len >=
s->max_payload_size) {
481 }
482 }
483 }
484
486 {
492
494 if (
s->num_frames > 0 &&
499 }
500
501 if (!
s->num_frames) {
503 s->timestamp =
s->cur_timestamp;
504 }
510 s->cur_timestamp += frame_duration;
511
512 if (
s->num_frames ==
s->max_frames_per_packet) {
515 }
516 }
517 return 0;
518 }
519
521 {
524 int rtcp_bytes;
526
528
535 s->last_octet_count =
s->octet_count;
537 }
538 s->cur_timestamp =
s->base_timestamp +
pkt->
pts;
539
554 /* The actual sample size is half a byte per sample, but since the
555 * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
556 * the correct parameter for send_samples_bits is 8 bits per stream
557 * clock. */
566 break;
570 break;
574 else
576 break;
580 break;
583 break;
586 break;
589 break;
592 break;
595 size_t mb_info_size;
598 &mb_info_size);
600 break;
601 }
602 /* Fallthrough */
605 break;
608 break;
612 break;
615 break;
618 break;
621 break;
624 break;
628
632 break;
633 }
635 if (
size >
s->max_payload_size) {
637 "Packet size %d too large for max RTP payload size %d\n",
638 size,
s->max_payload_size);
640 }
641 /* Intentional fallthrough */
642 default:
643 /* better than nothing : send the codec raw data */
645 break;
646 }
647 return 0;
648 }
649
651 {
653
654 /* If the caller closes and recreates ->pb, this might actually
655 * be NULL here even if it was successfully allocated at the start. */
659
660 return 0;
661 }
662
674 };