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) {
87 return 1;
88 default:
89 return 0;
90 }
91 }
92
94 {
98
102 }
106
107 return -1;
108 }
109
111 /* Re-validate non-dynamic payload types */
114
116 } else {
117 /* private option takes priority */
119 }
120
129 /* Round the NTP time to whole milliseconds. */
132 // Pick a random sequence start number, but in the lower end of the
133 // available range, so that any wraparound doesn't happen immediately.
134 // (Immediate wraparound would be an issue for SRTP.)
138 } else
140 } else
141 s->
seq &= 0xffff;
// Use the given parameter, wrapped to the right interval
142
147 } else
152 }
156 }
158
161 } else {
163 }
170 break;
173 break;
176 if (n < 1)
177 n = 1;
179 break;
183 "Packetizing VC-2 is experimental and does not use all values "
184 "of the specification "
185 "(even though most receivers may handle it just fine). "
186 "Please set -strict experimental in order to enable it.\n");
189 }
190 break;
194 "Packetizing H.261 is experimental and produces incorrect "
195 "packetization for cases where GOBs don't fit into packets "
196 "(even though most receivers may handle it just fine). "
197 "Please set -f_strict experimental in order to enable it.\n");
200 }
201 break;
203 /* check for H.264 MP4 syntax */
206 }
207 break;
209 /* Only check for the standardized hvcC version of extradata, keeping
210 * things simple and similar to the avcC/H.264 case above, instead
211 * of trying to handle the pre-standardization versions (as in
212 * libavcodec/hevc.c). */
215 }
216 break;
220 "Packetizing VP9 is experimental and its specification is "
221 "still in draft state. "
222 "Please set -strict experimental in order to enable it.\n");
225 }
226 break;
230 break;
232 /* Due to a historical error, the clock rate for G722 in RTP is
233 * 8000, even if the sample rate is 16000. See RFC 3551. */
235 break;
240 }
241 /* The opus RTP RFC says that all opus streams should use 48000 Hz
242 * as clock rate, since all opus sample rates can be expressed in
243 * this clock rate, and sample rate changes on the fly are supported. */
245 break;
250 }
252 break;
257 n = 31;
258 else
259 n = 61;
260 /* max_header_toc_size + the largest AMR payload must fit */
264 }
268 }
269 break;
272 break;
273 default:
274 break;
275 }
276
277 return 0;
278
281 return ret;
282 }
283
284 /* send an rtcp sender report packet */
286 {
288 uint32_t rtp_ts;
289
291
294 s1->streams[0]->time_base) +
s->base_timestamp;
300 avio_wb32(
s1->pb, ((ntp_time % 1000000) << 32) / 1000000);
304
309 avio_wb16(
s1->pb, (7 + len + 3) / 4);
/* length in words - 1 */
310
316 for (len = (7 + len) % 4; len % 4; len++)
318 }
319
320 if (bye) {
325 }
326
328 }
329
330 /* send an rtp packet. sequence number is incremented, but the caller
331 must update the timestamp itself */
333 {
335
337
338 /* build the RTP header */
344
347
348 s->
seq = (s->
seq + 1) & 0xffff;
351 }
352
353 /* send an integer number of samples and compute time stamp and fill
354 the rtp send buffer before sending. */
356 const uint8_t *buf1,
int size,
int sample_size_bits)
357 {
359 int len, max_packet_size,
n;
360 /* Calculate the number of bytes to get samples aligned on a byte border */
361 int aligned_samples_size = sample_size_bits/
av_gcd(sample_size_bits, 8);
362
363 max_packet_size = (s->
max_payload_size / aligned_samples_size) * aligned_samples_size;
364 /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
365 if ((sample_size_bits % 8) == 0 && ((8 *
size) % sample_size_bits) != 0)
367 n = 0;
368 while (size > 0) {
370 len =
FFMIN(max_packet_size, size);
371
372 /* copy data */
380 }
381 return 0;
382 }
383
386 {
389
391
392 /* test if we must flush because not enough space */
394 if ((len + size) > max_packet_size) {
395 if (len > 4) {
398 }
399 }
402 }
403
404 /* add the packet */
405 if (size > max_packet_size) {
406 /* big packet: fragment */
407 count = 0;
408 while (size > 0) {
409 len = max_packet_size - 4;
410 if (len > size)
412 /* build fragmented packet */
415 s->
buf[2] = count >> 8;
417 memcpy(s->
buf + 4, buf1, len);
422 }
423 } else {
425 /* no fragmentation possible */
430 }
431 memcpy(s->
buf_ptr, buf1, size);
433 }
434 }
435
438 {
440 int len, max_packet_size;
441
443
444 while (size > 0) {
445 len = max_packet_size;
446 if (len > size)
448
451
454 }
455 }
456
457 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
460 {
463
467 if (len > size)
473
478 }
479 }
480 }
481
483 {
489
490 while (frames > 0) {
496 }
497
501 }
502 memcpy(s->
buf_ptr, buf, frame_size);
503 frames--;
508
512 }
513 }
514 return 0;
515 }
516
518 {
521 int rtcp_bytes;
523
525
534 }
536
551 /* The actual sample size is half a byte per sample, but since the
552 * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
553 * the correct parameter for send_samples_bits is 8 bits per stream
554 * clock. */
563 break;
567 break;
571 else
573 break;
577 break;
580 break;
583 break;
586 break;
589 break;
592 int mb_info_size = 0;
595 &mb_info_size);
597 break;
598 }
599 /* Fallthrough */
602 break;
605 break;
609 break;
612 break;
615 break;
618 break;
621 break;
625 "Packet size %d too large for max RTP payload size %d\n",
628 }
629 /* Intentional fallthrough */
630 default:
631 /* better than nothing : send the codec raw data */
633 break;
634 }
635 return 0;
636 }
637
639 {
641
642 /* If the caller closes and recreates ->pb, this might actually
643 * be NULL here even if it was successfully allocated at the start. */
647
648 return 0;
649 }
650
660 .priv_class = &rtp_muxer_class,
662 };
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
enum AVFieldOrder field_order
Video only.
int64_t start_time_realtime
Start time of the stream in real world time, in microseconds since the Unix epoch (00:00 1st January ...
#define LIBAVUTIL_VERSION_INT
static int rtp_send_samples(AVFormatContext *s1, const uint8_t *buf1, int size, int sample_size_bits)
static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
int64_t last_rtcp_ntp_time
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
const char * av_default_item_name(void *ptr)
Return the context name.
#define RTCP_TX_RATIO_NUM
unsigned int last_octet_count
static const AVOption options[]
void ff_rtp_send_amr(AVFormatContext *s1, const uint8_t *buff, int size)
Packetize AMR frames into RTP packets according to RFC 3267, in octet-aligned mode.
#define RTCP_TX_RATIO_DEN
An AV_PKT_DATA_H263_MB_INFO side data packet contains a number of structures with info about macroblo...
int strict_std_compliance
Allow non-standard and experimental extension.
void ff_rtp_send_vc2hq(AVFormatContext *s1, const uint8_t *buf, int size, int interlaced)
int nal_length_size
Number of bytes used for H.264 NAL length, if the MP4 syntax is used (1, 2 or 4)
#define FF_RTP_FLAG_MP4A_LATM
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
static const AVClass rtp_muxer_class
int id
Format-specific stream ID.
int max_frames_per_packet
void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size)
Packetize H.263 frames into RTP packets according to RFC 4629.
void ff_rtp_send_vp9(AVFormatContext *s1, const uint8_t *buff, int size)
AVStream ** streams
A list of all streams in the file.
static int rtp_write_header(AVFormatContext *s1)
int flags
Flags modifying the (de)muxer behaviour.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
#define FF_RTP_FLAG_RFC2190
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size)
void ff_rtp_send_jpeg(AVFormatContext *s1, const uint8_t *buff, int size)
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
preferred ID for decoding MPEG audio layer 1, 2 or 3
enum AVMediaType codec_type
General type of the encoded data.
void ff_rtp_send_latm(AVFormatContext *s1, const uint8_t *buff, int size)
void ff_rtp_send_vp8(AVFormatContext *s1, const uint8_t *buff, int size)
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
This function is the same as av_get_audio_frame_duration(), except it works with AVCodecParameters in...
#define FF_RTP_FLAG_SKIP_RTCP
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
static void rtp_send_mpegts_raw(AVFormatContext *s1, const uint8_t *buf1, int size)
int extradata_size
Size of the extradata content in bytes.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
int block_align
Audio only.
void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size)
Packetize Xiph frames into RTP according to RFC 5215 (Vorbis) and the Theora RFC draft.
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
void ff_rtp_send_h261(AVFormatContext *s1, const uint8_t *buf1, int size)
static int write_trailer(AVFormatContext *s1)
void ff_rtp_send_h264_hevc(AVFormatContext *s1, const uint8_t *buf1, int size)
static int rtp_send_ilbc(AVFormatContext *s1, const uint8_t *buf, int size)
void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size)
static void rtp_send_mpegaudio(AVFormatContext *s1, const uint8_t *buf1, int size)
preferred ID for MPEG-1/2 video decoding
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it...
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
int64_t first_rtcp_ntp_time
AVOutputFormat ff_rtp_muxer
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
AVIOContext * pb
I/O context.
#define FF_RTP_FLAG_OPTS(ctx, fieldname)
void avio_w8(AVIOContext *s, int b)
static int is_supported(enum AVCodecID id)
Describe the class of an AVClass context structure.
Rational number (pair of numerator and denominator).
void avio_wb16(AVIOContext *s, unsigned int val)
int sample_rate
Audio only.
static void rtp_send_raw(AVFormatContext *s1, const uint8_t *buf1, int size)
static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time, int bye)
unsigned int packet_count
FAKE codec to indicate a raw MPEG-2 TS stream (only used by libavformat)
int ff_rtp_get_payload_type(AVFormatContext *fmt, AVCodecParameters *par, int idx)
Return the payload type for a given stream used in the given format context.
void * priv_data
Format private data.
static void write_header(FFV1Context *f)
static int rtp_write_trailer(AVFormatContext *s1)
int bits_per_coded_sample
The number of bits per sample in the codedwords.
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
void ff_rtp_send_h263_rfc2190(AVFormatContext *s1, const uint8_t *buf1, int size, const uint8_t *mb_info, int mb_info_size)
void avio_wb32(AVIOContext *s, unsigned int val)
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
This structure stores compressed data.
#define FF_RTP_FLAG_SEND_BYE
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.