FFmpeg
Loading...
Searching...
No Matches
udp.c File Reference

UDP protocol. More...

#include "avformat.h"
#include "libavutil/avassert.h"
#include "libavutil/mem.h"
#include "libavutil/parseutils.h"
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/opt.h"
#include "libavutil/log.h"
#include "libavutil/time.h"
#include "internal.h"
#include "network.h"
#include "os_support.h"
#include "url.h"
#include "ip.h"

Go to the source code of this file.

Data Structures

struct  UDPQueuedPacketHeader
 
struct  UDPContext
 

Macros

#define _DEFAULT_SOURCE
 
#define _BSD_SOURCE   /* Needed for using struct ip_mreq with recent glibc */
 
#define UDPLITE_SEND_CSCOV   10
 
#define UDPLITE_RECV_CSCOV   11
 
#define IPPROTO_UDPLITE   136
 
#define IPV6_ADD_MEMBERSHIP   IPV6_JOIN_GROUP
 
#define IPV6_DROP_MEMBERSHIP   IPV6_LEAVE_GROUP
 
#define UDP_TX_BUF_SIZE   32768
 
#define UDP_RX_BUF_SIZE   393216
 
#define UDP_MAX_PKT_SIZE   65536
 
#define UDP_HEADER_SIZE   8
 
#define OFFSET(x)
 
#define D   AV_OPT_FLAG_DECODING_PARAM
 
#define E   AV_OPT_FLAG_ENCODING_PARAM
 

Functions

static int udp_set_multicast_ttl (int sockfd, int mcastTTL, struct sockaddr *addr, void *logctx)
 
static int udp_join_multicast_group (int sockfd, struct sockaddr *addr, struct sockaddr *local_addr, void *logctx)
 
static int udp_leave_multicast_group (int sockfd, struct sockaddr *addr, struct sockaddr *local_addr, void *logctx)
 
static int udp_set_multicast_sources (URLContext *h, int sockfd, struct sockaddr *addr, int addr_len, struct sockaddr_storage *local_addr, struct sockaddr_storage *sources, int nb_sources, int include)
 
static int udp_set_url (URLContext *h, struct sockaddr_storage *addr, const char *hostname, int port)
 
static int udp_socket_create (URLContext *h, struct sockaddr_storage *addr, socklen_t *addr_len, const char *localaddr)
 
static int udp_port (struct sockaddr_storage *addr, int addr_len)
 
int ff_udp_set_remote_url (URLContext *h, const char *uri)
 If no filename is given to av_open_input_file because you want to get the local port first, then you must call this function to set the remote server address.
 
int ff_udp_set_remote_addr (URLContext *h, const struct sockaddr *dest_addr, socklen_t dest_addr_len, int do_connect)
 This function is identical to ff_udp_set_remote_url, except that it takes a sockaddr directly.
 
int ff_udp_get_local_port (URLContext *h)
 Return the local port used by the UDP connection.
 
void ff_udp_get_last_recv_addr (URLContext *h, struct sockaddr_storage *addr, socklen_t *addr_len)
 
static int udp_get_file_handle (URLContext *h)
 Return the udp file handle for select() usage to wait for several RTP streams at the same time.
 
static int udp_open (URLContext *h, const char *uri, int flags)
 
static int udplite_open (URLContext *h, const char *uri, int flags)
 
static int udp_read (URLContext *h, uint8_t *buf, int size)
 
static int udp_write (URLContext *h, const uint8_t *buf, int size)
 
static int udp_close (URLContext *h)
 

Variables

static const AVOption options []
 
static const AVClass udp_class
 
static const AVClass udplite_context_class
 
const URLProtocol ff_udp_protocol
 
const URLProtocol ff_udplite_protocol
 

Detailed Description

UDP protocol.

Definition in file udp.c.

Macro Definition Documentation

◆ _DEFAULT_SOURCE

#define _DEFAULT_SOURCE

Definition at line 27 of file udp.c.

◆ _BSD_SOURCE

#define _BSD_SOURCE   /* Needed for using struct ip_mreq with recent glibc */

Definition at line 28 of file udp.c.

◆ UDPLITE_SEND_CSCOV

#define UDPLITE_SEND_CSCOV   10

Definition at line 55 of file udp.c.

Referenced by udp_open().

◆ UDPLITE_RECV_CSCOV

#define UDPLITE_RECV_CSCOV   11

Definition at line 56 of file udp.c.

Referenced by udp_open().

◆ IPPROTO_UDPLITE

#define IPPROTO_UDPLITE   136

Definition at line 60 of file udp.c.

Referenced by udp_open(), and udp_socket_create().

◆ IPV6_ADD_MEMBERSHIP

#define IPV6_ADD_MEMBERSHIP   IPV6_JOIN_GROUP

Definition at line 73 of file udp.c.

Referenced by udp_join_multicast_group().

◆ IPV6_DROP_MEMBERSHIP

#define IPV6_DROP_MEMBERSHIP   IPV6_LEAVE_GROUP

Definition at line 74 of file udp.c.

Referenced by udp_leave_multicast_group().

◆ UDP_TX_BUF_SIZE

#define UDP_TX_BUF_SIZE   32768

Definition at line 77 of file udp.c.

Referenced by udp_open().

◆ UDP_RX_BUF_SIZE

#define UDP_RX_BUF_SIZE   393216

Definition at line 78 of file udp.c.

Referenced by udp_open().

◆ UDP_MAX_PKT_SIZE

#define UDP_MAX_PKT_SIZE   65536

Definition at line 79 of file udp.c.

Referenced by udp_open().

◆ UDP_HEADER_SIZE

#define UDP_HEADER_SIZE   8

Definition at line 80 of file udp.c.

Referenced by udplite_open().

◆ OFFSET

#define OFFSET ( x)
Value:
offsetof(UDPContext, x)

Definition at line 131 of file udp.c.

◆ D

Definition at line 132 of file udp.c.

◆ E

Definition at line 133 of file udp.c.

Function Documentation

◆ udp_set_multicast_ttl()

static int udp_set_multicast_ttl ( int sockfd,
int mcastTTL,
struct sockaddr * addr,
void * logctx )
static

Definition at line 171 of file udp.c.

Referenced by udp_open().

◆ udp_join_multicast_group()

static int udp_join_multicast_group ( int sockfd,
struct sockaddr * addr,
struct sockaddr * local_addr,
void * logctx )
static

Definition at line 212 of file udp.c.

Referenced by udp_open().

◆ udp_leave_multicast_group()

static int udp_leave_multicast_group ( int sockfd,
struct sockaddr * addr,
struct sockaddr * local_addr,
void * logctx )
static

Definition at line 246 of file udp.c.

Referenced by udp_close().

◆ udp_set_multicast_sources()

static int udp_set_multicast_sources ( URLContext * h,
int sockfd,
struct sockaddr * addr,
int addr_len,
struct sockaddr_storage * local_addr,
struct sockaddr_storage * sources,
int nb_sources,
int include )
static

Definition at line 280 of file udp.c.

Referenced by udp_open().

◆ udp_set_url()

static int udp_set_url ( URLContext * h,
struct sockaddr_storage * addr,
const char * hostname,
int port )
static

Definition at line 347 of file udp.c.

Referenced by ff_udp_set_remote_url().

◆ udp_socket_create()

static int udp_socket_create ( URLContext * h,
struct sockaddr_storage * addr,
socklen_t * addr_len,
const char * localaddr )
static

Definition at line 363 of file udp.c.

Referenced by udp_open().

◆ udp_port()

static int udp_port ( struct sockaddr_storage * addr,
int addr_len )
static

Definition at line 405 of file udp.c.

Referenced by udp_open().

◆ ff_udp_set_remote_url()

int ff_udp_set_remote_url ( URLContext * h,
const char * uri )

If no filename is given to av_open_input_file because you want to get the local port first, then you must call this function to set the remote server address.

url syntax: udp://host:port[?option=val...] option: 'ttl=n' : set the ttl value (for multicast only) 'localport=n' : set the local port 'pkt_size=n' : set max packet size 'reuse=1' : enable reusing the socket 'overrun_nonfatal=1': survive in case of circular buffer overrun

Parameters
hmedia file context
uriof the remote server
Returns
zero if no error.

Definition at line 435 of file udp.c.

Referenced by ff_rtp_set_remote_url(), and udp_open().

◆ ff_udp_set_remote_addr()

int ff_udp_set_remote_addr ( URLContext * h,
const struct sockaddr * dest_addr,
socklen_t dest_addr_len,
int do_connect )

This function is identical to ff_udp_set_remote_url, except that it takes a sockaddr directly.

Definition at line 472 of file udp.c.

Referenced by gnutls_url_pull(), mbedtls_recv(), tls_handshake_loop(), and url_bio_bread().

◆ ff_udp_get_local_port()

int ff_udp_get_local_port ( URLContext * h)

Return the local port used by the UDP connection.

Parameters
hmedia file context
Returns
the local port number

Definition at line 504 of file udp.c.

Referenced by ff_rtp_get_local_rtp_port(), and rtp_open().

◆ ff_udp_get_last_recv_addr()

void ff_udp_get_last_recv_addr ( URLContext * h,
struct sockaddr_storage * addr,
socklen_t * addr_len )

Definition at line 510 of file udp.c.

Referenced by gnutls_url_pull(), mbedtls_recv(), tls_handshake_loop(), and url_bio_bread().

◆ udp_get_file_handle()

static int udp_get_file_handle ( URLContext * h)
static

Return the udp file handle for select() usage to wait for several RTP streams at the same time.

Parameters
hmedia file context

Definition at line 522 of file udp.c.

◆ udp_open()

static int udp_open ( URLContext * h,
const char * uri,
int flags )
static

Definition at line 693 of file udp.c.

Referenced by udplite_open().

◆ udplite_open()

static int udplite_open ( URLContext * h,
const char * uri,
int flags )
static

Definition at line 967 of file udp.c.

◆ udp_read()

static int udp_read ( URLContext * h,
uint8_t * buf,
int size )
static

Definition at line 977 of file udp.c.

◆ udp_write()

static int udp_write ( URLContext * h,
const uint8_t * buf,
int size )
static

Definition at line 1044 of file udp.c.

◆ udp_close()

static int udp_close ( URLContext * h)
static

Definition at line 1094 of file udp.c.

Variable Documentation

◆ options

const AVOption options[]
static
Initial value:
= {
{ "buffer_size", "System data size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
{ "bitrate", "Bits to send per second", OFFSET(bitrate), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, .flags = E },
{ "burst_bits", "Max length of bursts in bits (when using bitrate)", OFFSET(burst_bits), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, .flags = E },
{ "localport", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, D|E },
{ "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
{ "localaddr", "Local address", OFFSET(localaddr), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
{ "udplite_coverage", "choose UDPLite head size which should be validated by checksum", OFFSET(udplite_coverage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E },
{ "pkt_size", "Maximum UDP packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = 1472 }, -1, INT_MAX, .flags = D|E },
{ "reuse", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D|E },
{ "reuse_socket", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, .flags = D|E },
{ "broadcast", "explicitly allow or disallow broadcast destination", OFFSET(is_broadcast), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
{ "ttl", "Time to live (multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 255, E },
{ "dscp", "DSCP class for outgoing packets", OFFSET(dscp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 63, E },
{ "connect", "set if connect() should be called on socket", OFFSET(is_connected), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
{ "fifo_size", "set the UDP circular buffer size (in 188-byte packets)", OFFSET(circular_buffer_size), AV_OPT_TYPE_INT, {.i64 = HAVE_PTHREAD_CANCEL ? 7*4096 : 0}, 0, INT_MAX, D },
{ "overrun_nonfatal", "survive in case of UDP receiving circular buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, D },
{ "timeout", "set raise error timeout, in microseconds (only in read mode)",OFFSET(timeout), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D },
{ "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
{ "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
{ NULL }
}
#define E
Definition avdct.c:34
#define D
Definition avdct.c:35
#define NULL
Definition coverity.c:32
static int16_t block[64]
Definition dct.c:125
#define OFFSET(x)
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
int64_t bitrate
Definition av1_levels.c:47

Definition at line 134 of file udp.c.

◆ udp_class

const AVClass udp_class
static
Initial value:
= {
.class_name = "udp",
.item_name = av_default_item_name,
.option = options,
}
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
#define LIBAVUTIL_VERSION_INT
Definition version.h:85

Definition at line 157 of file udp.c.

◆ udplite_context_class

const AVClass udplite_context_class
static
Initial value:
= {
.class_name = "udplite",
.item_name = av_default_item_name,
.option = options,
}

Definition at line 164 of file udp.c.

◆ ff_udp_protocol

const URLProtocol ff_udp_protocol
Initial value:
= {
.name = "udp",
.url_open = udp_open,
.url_read = udp_read,
.url_write = udp_write,
.url_close = udp_close,
.url_get_file_handle = udp_get_file_handle,
.priv_data_size = sizeof(UDPContext),
.priv_data_class = &udp_class,
}
#define flags(name, subs,...)
Definition cbs_h264.c:74
static int udp_close(URLContext *h)
Definition udp.c:1094
static int udp_open(URLContext *h, const char *uri, int flags)
Definition udp.c:693
static const AVClass udp_class
Definition udp.c:157
static int udp_get_file_handle(URLContext *h)
Return the udp file handle for select() usage to wait for several RTP streams at the same time.
Definition udp.c:522
static int udp_write(URLContext *h, const uint8_t *buf, int size)
Definition udp.c:1044
static int udp_read(URLContext *h, uint8_t *buf, int size)
Definition udp.c:977
#define URL_PROTOCOL_FLAG_NETWORK
Definition url.h:33

Definition at line 1140 of file udp.c.

◆ ff_udplite_protocol

const URLProtocol ff_udplite_protocol
Initial value:
= {
.name = "udplite",
.url_open = udplite_open,
.url_read = udp_read,
.url_write = udp_write,
.url_close = udp_close,
.url_get_file_handle = udp_get_file_handle,
.priv_data_size = sizeof(UDPContext),
.priv_data_class = &udplite_context_class,
}
static int udplite_open(URLContext *h, const char *uri, int flags)
Definition udp.c:967
static const AVClass udplite_context_class
Definition udp.c:164

Definition at line 1152 of file udp.c.