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
28
30
38 };
39
45 };
46
47 #define RTCP_SR_SIZE 28
48
50 {
51 switch(id) {
89 return 1;
90 default:
91 return 0;
92 }
93 }
94
96 {
100
101 if (
s1->nb_streams != 1) {
104 }
108
109 return -1;
110 }
111
112 if (
s->payload_type < 0) {
113 /* Re-validate non-dynamic payload types */
116
117 s->payload_type = st->
id;
118 } else {
119 /* private option takes priority */
120 st->
id =
s->payload_type;
121 }
122
124 s->timestamp =
s->base_timestamp;
125 s->cur_timestamp = 0;
131 /* Round the NTP time to whole milliseconds. */
132 s->first_rtcp_ntp_time = (
s1->start_time_realtime / 1000) * 1000 +
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.)
140 } else
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)
148 s1->pb->max_packet_size);
149 } else
150 s1->packet_size =
s1->pb->max_packet_size;
151 if (
s1->packet_size <= 12) {
154 }
158 }
159 s->max_payload_size =
s1->packet_size - 12;
160
163 } else {
165 }
170 s->buf_ptr =
s->buf + 4;
172 break;
175 break;
178 if (n < 1)
179 n = 1;
181 break;
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");
191 }
192 break;
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");
202 }
203 break;
205 /* check for H.264 MP4 syntax */
208 }
209 break;
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). */
217 }
218 break;
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");
227 }
228 break;
231 s->max_frames_per_packet = 15;
232 break;
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. */
237 break;
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. */
247 break;
252 }
254 break;
257 s->max_frames_per_packet = 50;
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) {
266 }
270 }
271 break;
273 s->max_frames_per_packet = 50;
274 break;
275 default:
276 break;
277 }
278
279 return 0;
280
284 }
285
286 /* send an rtcp sender report packet */
288 {
290 uint32_t rtp_ts;
291
293
294 s->last_rtcp_ntp_time = ntp_time;
296 s1->streams[0]->time_base) +
s->base_timestamp;
302 avio_wb32(
s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
306
312
320 }
321
322 if (bye) {
327 }
328
330 }
331
332 /* send an rtp packet. sequence number is incremented, but the caller
333 must update the timestamp itself */
335 {
337
339
340 /* build the RTP header */
342 avio_w8(
s1->pb, (
s->payload_type & 0x7f) | ((m & 0x01) << 7));
346
349
350 s->seq = (
s->seq + 1) & 0xffff;
351 s->octet_count +=
len;
353 }
354
355 /* send an integer number of samples and compute time stamp and fill
356 the rtp send buffer before sending. */
358 const uint8_t *buf1,
int size,
int sample_size_bits)
359 {
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)
369 n = 0;
373
374 /* copy data */
375 memcpy(
s->buf_ptr, buf1,
len);
379 s->timestamp =
s->cur_timestamp + n * 8 / sample_size_bits;
381 n += (
s->buf_ptr -
s->buf);
382 }
383 return 0;
384 }
385
387 const uint8_t *buf1,
int size)
388 {
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) {
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;
411 len = max_packet_size - 4;
414 /* build fragmented packet */
417 s->buf[2] = count >> 8;
419 memcpy(
s->buf + 4, buf1,
len);
424 }
425 } else {
426 if (
s->buf_ptr ==
s->buf + 4) {
427 /* no fragmentation possible */
432 }
433 memcpy(
s->buf_ptr, buf1,
size);
435 }
436 }
437
439 const uint8_t *buf1,
int size)
440 {
442 int len, max_packet_size;
443
444 max_packet_size =
s->max_payload_size;
445
447 len = max_packet_size;
450
451 s->timestamp =
s->cur_timestamp;
453
456 }
457 }
458
459 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
461 const uint8_t *buf1,
int size)
462 {
465
466 s->timestamp =
s->cur_timestamp;
468 len =
s->max_payload_size - (
s->buf_ptr -
s->buf);
471 memcpy(
s->buf_ptr, buf1,
len);
475
476 out_len =
s->buf_ptr -
s->buf;
477 if (out_len >=
s->max_payload_size) {
480 }
481 }
482 }
483
485 {
491
493 if (
s->num_frames > 0 &&
498 }
499
500 if (!
s->num_frames) {
502 s->timestamp =
s->cur_timestamp;
503 }
509 s->cur_timestamp += frame_duration;
510
511 if (
s->num_frames ==
s->max_frames_per_packet) {
514 }
515 }
516 return 0;
517 }
518
520 {
523 int rtcp_bytes;
525
527
534 s->last_octet_count =
s->octet_count;
536 }
537 s->cur_timestamp =
s->base_timestamp +
pkt->
pts;
538
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. */
565 break;
569 break;
573 else
575 break;
579 break;
582 break;
585 break;
588 break;
591 break;
594 size_t mb_info_size;
597 &mb_info_size);
599 break;
600 }
601 /* Fallthrough */
604 break;
607 break;
611 break;
614 break;
617 break;
620 break;
623 break;
627
631 break;
632 }
634 if (
size >
s->max_payload_size) {
636 "Packet size %d too large for max RTP payload size %d\n",
637 size,
s->max_payload_size);
639 }
640 /* Intentional fallthrough */
641 default:
642 /* better than nothing : send the codec raw data */
644 break;
645 }
646 return 0;
647 }
648
650 {
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. */
658
659 return 0;
660 }
661
673 };