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) {
83 return 1;
84 default:
85 return 0;
86 }
87 }
88
90 {
94
98 }
102
103 return -1;
104 }
105
107 /* Re-validate non-dynamic payload types */
110
112 } else {
113 /* private option takes priority */
115 }
116
125 /* Round the NTP time to whole milliseconds. */
128 // Pick a random sequence start number, but in the lower end of the
129 // available range, so that any wraparound doesn't happen immediately.
130 // (Immediate wraparound would be an issue for SRTP.)
134 } else
136 } else
137 s->
seq &= 0xffff;
// Use the given parameter, wrapped to the right interval
138
143 } else
148 }
152 }
154
157 } else {
159 }
166 break;
169 break;
172 if (n < 1)
173 n = 1;
175 break;
179 "Packetizing H261 is experimental and produces incorrect "
180 "packetization for cases where GOBs don't fit into packets "
181 "(even though most receivers may handle it just fine). "
182 "Please set -f_strict experimental in order to enable it.\n");
185 }
186 break;
188 /* check for H.264 MP4 syntax */
191 }
192 break;
194 /* Only check for the standardized hvcC version of extradata, keeping
195 * things simple and similar to the avcC/H264 case above, instead
196 * of trying to handle the pre-standardization versions (as in
197 * libavcodec/hevc.c). */
200 }
201 break;
205 break;
207 /* Due to a historical error, the clock rate for G722 in RTP is
208 * 8000, even if the sample rate is 16000. See RFC 3551. */
210 break;
215 }
216 /* The opus RTP RFC says that all opus streams should use 48000 Hz
217 * as clock rate, since all opus sample rates can be expressed in
218 * this clock rate, and sample rate changes on the fly are supported. */
220 break;
225 }
227 break;
232 n = 31;
233 else
234 n = 61;
235 /* max_header_toc_size + the largest AMR payload must fit */
239 }
243 }
244 break;
247 break;
248 default:
249 break;
250 }
251
252 return 0;
253
256 return ret;
257 }
258
259 /* send an rtcp sender report packet */
261 {
263 uint32_t rtp_ts;
264
266
269 s1->streams[0]->time_base) +
s->base_timestamp;
278
283 avio_wb16(
s1->pb, (7 + len + 3) / 4);
/* length in words - 1 */
284
290 for (len = (7 + len) % 4; len % 4; len++)
292 }
293
294 if (bye) {
299 }
300
302 }
303
304 /* send an rtp packet. sequence number is incremented, but the caller
305 must update the timestamp itself */
307 {
309
311
312 /* build the RTP header */
318
321
322 s->
seq = (s->
seq + 1) & 0xffff;
325 }
326
327 /* send an integer number of samples and compute time stamp and fill
328 the rtp send buffer before sending. */
330 const uint8_t *buf1,
int size,
int sample_size_bits)
331 {
333 int len, max_packet_size,
n;
334 /* Calculate the number of bytes to get samples aligned on a byte border */
335 int aligned_samples_size = sample_size_bits/
av_gcd(sample_size_bits, 8);
336
337 max_packet_size = (s->
max_payload_size / aligned_samples_size) * aligned_samples_size;
338 /* Not needed, but who knows. Don't check if samples aren't an even number of bytes. */
339 if ((sample_size_bits % 8) == 0 && ((8 *
size) % sample_size_bits) != 0)
341 n = 0;
342 while (size > 0) {
344 len =
FFMIN(max_packet_size, size);
345
346 /* copy data */
354 }
355 return 0;
356 }
357
360 {
363
365
366 /* test if we must flush because not enough space */
368 if ((len + size) > max_packet_size) {
369 if (len > 4) {
372 }
373 }
376 }
377
378 /* add the packet */
379 if (size > max_packet_size) {
380 /* big packet: fragment */
381 count = 0;
382 while (size > 0) {
383 len = max_packet_size - 4;
384 if (len > size)
386 /* build fragmented packet */
389 s->
buf[2] = count >> 8;
391 memcpy(s->
buf + 4, buf1, len);
396 }
397 } else {
399 /* no fragmentation possible */
404 }
405 memcpy(s->
buf_ptr, buf1, size);
407 }
408 }
409
412 {
414 int len, max_packet_size;
415
417
418 while (size > 0) {
419 len = max_packet_size;
420 if (len > size)
422
425
428 }
429 }
430
431 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
434 {
437
441 if (len > size)
447
452 }
453 }
454 }
455
457 {
463
464 while (frames > 0) {
470 }
471
475 }
476 memcpy(s->
buf_ptr, buf, frame_size);
477 frames--;
482
486 }
487 }
488 return 0;
489 }
490
492 {
495 int rtcp_bytes;
497
499
508 }
510
523 /* The actual sample size is half a byte per sample, but since the
524 * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
525 * the correct parameter for send_samples_bits is 8 bits per stream
526 * clock. */
534 break;
538 break;
542 else
544 break;
548 break;
551 break;
554 break;
557 break;
560 int mb_info_size = 0;
563 &mb_info_size);
565 break;
566 }
567 /* Fallthrough */
570 break;
573 break;
577 break;
580 break;
583 break;
586 break;
590 "Packet size %d too large for max RTP payload size %d\n",
593 }
594 /* Intentional fallthrough */
595 default:
596 /* better than nothing : send the codec raw data */
598 break;
599 }
600 return 0;
601 }
602
604 {
606
607 /* If the caller closes and recreates ->pb, this might actually
608 * be NULL here even if it was successfully allocated at the start. */
612
613 return 0;
614 }
615
625 .priv_class = &rtp_muxer_class,
627 };
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
void avio_wb64(AVIOContext *s, uint64_t val)
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
#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 block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
int strict_std_compliance
Allow non-standard and experimental extension.
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
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size)
Packetize H.263 frames into RTP packets according to RFC 4629.
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.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
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
int ff_rtp_get_payload_type(AVFormatContext *fmt, AVCodecContext *codec, int idx)
Return the payload type for a given stream used in the given format context.
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)
#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
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 a and b.
#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 2 timestamps each in its own timebases.
static void rtp_send_mpegts_raw(AVFormatContext *s1, const uint8_t *buf1, int size)
AVCodecContext * codec
Codec context associated with this stream.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
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
enum AVMediaType codec_type
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
int sample_rate
samples per second
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 numerator/denominator
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
Return audio frame duration.
void avio_wb16(AVIOContext *s, unsigned int val)
#define NTP_TO_RTP_FORMAT(x)
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 channels
number of audio channels
void * priv_data
Format private data.
static void write_header(FFV1Context *f)
static int rtp_write_trailer(AVFormatContext *s1)
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)
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
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.
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
#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.