RTP

Name

RTP -- Real-Time Transport Protocol (RTP) Implementation.

Functions

rtcp_app *(*rtcp_app_callback) ()
void(*rtp_callback) ()
rtp_trtp_init ()
rtp_trtp_init_if ()
voidrtp_send_bye ()
voidrtp_done ()
intrtp_set_option ()
intrtp_get_option ()
intrtp_recv ()
intrtp_send_data ()
voidrtp_send_ctrl ()
voidrtp_update ()
uint32_trtp_my_ssrc ()
intrtp_add_csrc ()
intrtp_del_csrc ()
intrtp_set_sdes ()
const char *rtp_get_sdes ()
const rtcp_sr *rtp_get_sr ()
const rtcp_rr *rtp_get_rr ()
intrtp_set_encryption_key ()
intrtp_set_my_ssrc ()
char *rtp_get_addr ()
uint16_trtp_get_rx_port ()
uint16_trtp_get_tx_port ()
intrtp_get_ttl ()
uint8_t *rtp_get_userdata ()

Types and Values

#defineRTP_VERSION
#defineRTP_MAX_PACKET_LEN
 rtp_packet
#defineRTP_PACKET_HEADER_SIZE
 rtcp_sr
 rtcp_rr
enumrtcp_sdes_type
 rtcp_sdes_item
 rtcp_app
enumrtp_event_type
 rtp_event
enumrtp_option

Includes

#include <rtp.h>

Description

The Real-Time Transport Protocol (RTP) is a protocol for the transport of audio, video, and other real-time data across IP capable networks.

Functions

rtcp_app_callback ()

rtcp_app *
(*rtcp_app_callback) (struct rtp *session,
                      uint32_t rtp_ts,
                      int max_size);

This callback function crafts an RTCP APP packet to be sent with an RTCP RR.

Parameters

session

the session pointer (returned by rtp_init())

 

rtp_ts

the current time expressed in units of the media timestamp.

 

max_size

the max allowed size of an APP packet.

 

Returns

A fully-formed RTCP APP packet as an rtcp_app, or NULL (???) if no APP packet needs to be sent at this time.


rtp_callback ()

void
(*rtp_callback) (struct rtp *session,
                 rtp_event *e);

Handles RTP events in an application-specific way. See rtp_event for a description of the possible events and how rtp_callback() should handle each.

Parameters

session

The RTP Session.

 

e

The RTP Event information. See rtp_event.

 


rtp_init ()

rtp_t
rtp_init (const char *addr,
          uint16_t rx_port,
          uint16_t tx_port,
          int ttl,
          double rtcp_bw,
          rtp_callback callback,
          uint8_t *userdata);

Parameters

addr

IP destination of this session (unicast or multicast), as an ASCII string. May be a host name, which will be looked up, or may be an IPv4 dotted quad or IPv6 literal adddress.

 

rx_port

The port to which to bind the UDP socket

 

tx_port

The port to which to send UDP packets

 

ttl

The TTL with which to send multicasts

 

rtcp_bw

The total bandwidth (in units of bytes per second) that is allocated to RTCP.

 

callback

See section on rtp_callback.

 

userdata

Opaque data associated with the session. See rtp_get_userdata().

 

Returns

An opaque session identifier to be used in future calls to the RTP library functions, or NULL on failure.


rtp_init_if ()

rtp_t
rtp_init_if (const char *addr,
             char *iface,
             uint16_t rx_port,
             uint16_t tx_port,
             int ttl,
             double rtcp_bw,
             rtp_callback callback,
             uint8_t *userdata);

Creates and initializes an RTP session.

Parameters

addr

IP destination of this session (unicast or multicast), as an ASCII string. May be a host name, which will be looked up, or may be an IPv4 dotted quad or IPv6 literal adddress.

 

iface

If the destination of the session is multicast, the optional interface to bind to. May be NULL, in which case the default multicast interface as determined by the system will be used.

 

rx_port

The port to which to bind the UDP socket

 

tx_port

The port to which to send UDP packets

 

ttl

The TTL with which to send multicasts

 

rtcp_bw

The total bandwidth (in units of ___) that is allocated to RTCP.

 

callback

See section on rtp_callback.

 

userdata

Opaque data associated with the session. See rtp_get_userdata().

 

Returns

An opaque session identifier to be used in future calls to the RTP library functions, or NULL on failure.


rtp_send_bye ()

void
rtp_send_bye (struct rtp *session);

Sends a BYE message on the RTP session, indicating that this participant is leaving the session. The process of sending a BYE may take some time, and this function will block until it is complete. During this time, RTCP events are reported to the application via the callback function (data packets are silently discarded).

Parameters

session

The RTP session

 


rtp_done ()

void
rtp_done (struct rtp *session);

Free the state associated with the given RTP session. This function does not send any packets (e.g. an RTCP BYE) - an application which wishes to exit in a clean manner should call rtp_send_bye() first.

Parameters

session

the RTP session to finish

 


rtp_set_option ()

int
rtp_set_option (struct rtp *session,
                rtp_option optname,
                int optval);

Sets the value of a session option. See rtp_option for documentation on the options and their legal values.

Parameters

session

The RTP session.

 

optname

The option name, see rtp_option.

 

optval

The value to set.

 

Returns

TRUE on success, else FALSE.


rtp_get_option ()

int
rtp_get_option (struct rtp *session,
                rtp_option optname,
                int *optval);

Retrieves the value of a session option. See rtp_option for documentation on the options and their legal values.

Parameters

session

The RTP session.

 

optname

The option name, see rtp_option.

 

optval

The return value.

 

Returns

TRUE and the value of the option in optval on success, else FALSE.


rtp_recv ()

int
rtp_recv (struct rtp *session,
          struct timeval *timeout,
          uint32_t curr_rtp_ts);

Receive RTP packets and dispatch them.

Parameters

session

the session pointer (returned by rtp_init())

 

timeout

the amount of time that rtcp_recv() is allowed to block

 

curr_rtp_ts

the current time expressed in units of the media timestamp.

 

Returns

TRUE if data received, FALSE if the timeout occurred.


rtp_send_data ()

int
rtp_send_data (struct rtp *session,
               uint32_t rtp_ts,
               char pt,
               int m,
               int cc,
               uint32_t csrc[],
               char *data,
               int data_len,
               char *extn,
               uint16_t extn_len,
               uint16_t extn_type);

Send an RTP packet. Most media applications will only set the session , rtp_ts , pt , m , data , data_len arguments.

Mixers and translators typically set additional contributing sources arguments (cc , csrc ).

Extensions fields (extn , extn_len , extn_type ) are for including application specific information. When the widest amount of inter-operability is required these fields should be avoided as some applications discard packets with extensions they do not recognize.

Parameters

session

the session pointer (returned by rtp_init())

 

rtp_ts

The timestamp reflects the sampling instant of the first octet of the RTP data to be sent. The timestamp is expressed in media units.

 

pt

The payload type identifying the format of the data.

 

m

Marker bit, interpretation defined by media profile of payload.

 

cc

Number of contributing sources (excluding local participant)

 

csrc

Array of SSRC identifiers for contributing sources.

 

data

The RTP data to be sent.

 

data_len

The size data in bytes.

 

extn

Extension data (if present).

 

extn_len

size of extn in bytes.

 

extn_type

extension type indicator.

 

Returns

Number of bytes transmitted.


rtp_send_ctrl ()

void
rtp_send_ctrl (struct rtp *session,
               uint32_t rtp_ts,
               rtcp_app_callback appcallback);

Checks RTCP timer and sends RTCP data when nececessary. The interval between RTCP packets is randomized over an interval that depends on the session bandwidth, the number of participants, and whether the local participant is a sender. This function should be called at least once per second, and can be safely called more frequently.

Parameters

session

the session pointer (returned by rtp_init())

 

rtp_ts

the current time expressed in units of the media timestamp.

 

appcallback

a callback to create an APP RTCP packet, if needed.

 


rtp_update ()

void
rtp_update (struct rtp *session);

Trawls through the internal data structures and performs housekeeping. This function should be called at least once per second. It uses an internal timer to limit the number of passes through the data structures to once per second, it can be safely called more frequently.

Parameters

session

the session pointer (returned by rtp_init())

 


rtp_my_ssrc ()

uint32_t
rtp_my_ssrc (struct rtp *session);

Parameters

session

The RTP Session.

 

Returns

The SSRC we are currently using in this session. Note that our SSRC can change at any time (due to collisions) so applications must not store the value returned, but rather should call this function each time they need it.


rtp_add_csrc ()

int
rtp_add_csrc (struct rtp *session,
              uint32_t csrc);

Adds csrc to list of contributing sources used in SDES items. Used by mixers and transcoders.

Parameters

session

the session pointer (returned by rtp_init())

 

csrc

Constributing SSRC identifier

 

Returns

TRUE.


rtp_del_csrc ()

int
rtp_del_csrc (struct rtp *session,
              uint32_t csrc);

Removes csrc from list of contributing sources used in SDES items. Used by mixers and transcoders.

Parameters

session

the session pointer (returned by rtp_init())

 

csrc

Constributing SSRC identifier

 

Returns

TRUE on success, FALSE if csrc is not a valid source.


rtp_set_sdes ()

int
rtp_set_sdes (struct rtp *session,
              uint32_t ssrc,
              rtcp_sdes_type type,
              const char *value,
              int length);

Sets session description information associated with participant ssrc . Under normal circumstances applications always use the ssrc of the local participant, this SDES information is transmitted in receiver reports. Setting SDES information for other participants affects the local SDES entries, but are not transmitted onto the network.

Parameters

session

the session pointer (returned by rtp_init())

 

ssrc

the SSRC identifier of a participant

 

type

the SDES type represented by value

 

value

the SDES description

 

length

the length of the description

 

Returns

Returns TRUE if participant exists, FALSE otherwise.


rtp_get_sdes ()

const char *
rtp_get_sdes (struct rtp *session,
              uint32_t ssrc,
              rtcp_sdes_type type);

Recovers session description (SDES) information on participant identified with ssrc . The SDES information associated with a source is updated when receiver reports are received. There are several different types of SDES information, e.g. username, location, phone, email. These are enumerated by rtcp_sdes_type.

Parameters

session

the session pointer (returned by rtp_init())

 

ssrc

the SSRC identifier of a participant

 

type

the SDES information to retrieve

 

Returns

pointer to string containing SDES description if received, NULL otherwise.


rtp_get_sr ()

const rtcp_sr *
rtp_get_sr (struct rtp *session,
            uint32_t ssrc);

Retrieve the latest sender report made by sender with ssrc identifier.

Parameters

session

the session pointer (returned by rtp_init())

 

ssrc

identifier of source

 

Returns

A pointer to an rtcp_sr structure on success, NULL otherwise. The pointer must not be freed.


rtp_get_rr ()

const rtcp_rr *
rtp_get_rr (struct rtp *session,
            uint32_t reporter,
            uint32_t reportee);

Retrieve the latest receiver report on reportee made by reporter . Provides an indication of other receivers reception service.

Parameters

session

the session pointer (returned by rtp_init())

 

reporter

participant originating receiver report

 

reportee

participant included in receiver report

 

Returns

A pointer to a rtcp_rr structure on success, NULL otherwise. The pointer must not be freed.


rtp_set_encryption_key ()

int
rtp_set_encryption_key (struct rtp *session,
                        const char *passphrase);

Converts the user supplied key into a form suitable for use with RTP and install it as the active key. Passing in NULL as the passphrase disables encryption. The passphrase is converted into a DES key as specified in RFC1890, that is:

Note that versions of rat prior to 4.1.2 do not convert the passphrase to canonical form before taking the MD5 hash, and so will not be compatible for keys which are non-invarient under this step.

Determine from the user's encryption key which encryption mechanism we're using. Per the RTP RFC, if the key is of the form

string/key

then "string" is the name of the encryption algorithm, and "key" is the key to be used. If no / is present, then the algorithm is assumed to be (the appropriate variant of) DES.

Parameters

session

The RTP session.

 

passphrase

The user-provided "pass phrase" to map to an encryption key.

 

Returns

TRUE on success, FALSE on failure.


rtp_set_my_ssrc ()

int
rtp_set_my_ssrc (struct rtp *session,
                 uint32_t ssrc);

This function coerces the local SSRC identifer to be ssrc. For this function to succeed it must be called immediately after rtp_init or rtp_init_if. The intended purpose of this function is to co-ordinate SSRC's between layered sessions, it should not be used otherwise.

Parameters

session

the RTP session

 

ssrc

the SSRC to be used by the RTP session

 

Returns

TRUE on success, FALSE otherwise.


rtp_get_addr ()

char *
rtp_get_addr (struct rtp *session);

Parameters

session

The RTP Session.

 

Returns

The session's destination address, as set when creating the session with rtp_init() or rtp_init_if().


rtp_get_rx_port ()

uint16_t
rtp_get_rx_port (struct rtp *session);

Parameters

session

The RTP Session.

 

Returns

The UDP port to which this session is bound, as set when creating the session with rtp_init() or rtp_init_if().


rtp_get_tx_port ()

uint16_t
rtp_get_tx_port (struct rtp *session);

Parameters

session

The RTP Session.

 

Returns

The UDP port to which RTP packets are transmitted, as set when creating the session with rtp_init() or rtp_init_if().


rtp_get_ttl ()

int
rtp_get_ttl (struct rtp *session);

Parameters

session

The RTP Session.

 

Returns

The session's TTL, as set when creating the session with rtp_init() or rtp_init_if().


rtp_get_userdata ()

uint8_t *
rtp_get_userdata (struct rtp *session);

This function returns the userdata pointer that was passed to the rtp_init() or rtp_init_if() function when creating this session.

Parameters

session

The RTP session.

 

Returns

pointer to userdata.

Types and Values

RTP_VERSION

#define RTP_VERSION 2


RTP_MAX_PACKET_LEN

#define RTP_MAX_PACKET_LEN 1500


rtp_packet

typedef struct {
	/* The following are pointers to the data in the packet as    */
	/* it came off the wire. The packet it read in such that the  */
	/* header maps onto the latter part of this struct, and the   */
	/* fields in this first part of the struct point into it. The */
	/* entire packet can be freed by freeing this struct, without */
	/* having to free the csrc, data and extn blocks separately.  */
	/* WARNING: Don't change the size of the first portion of the */
	/* struct without changing RTP_PACKET_HEADER_SIZE to match.   */
	uint32_t *csrc;
	char		*data;
	int		 data_len;
	unsigned char *extn;
	uint16_t	 extn_len; /* Size of the extension in 32 bit words minus one */
	uint16_t	 extn_type; /* Extension type field in the RTP packet header   */
	/* The following map directly onto the RTP packet header...   */
#ifdef WORDS_BIGENDIAN
	unsigned short   v:2;		/* packet type                */
	unsigned short   p:1;		/* padding flag               */
	unsigned short   x:1;		/* header extension flag      */
	unsigned short   cc:4;		/* CSRC count                 */
	unsigned short   m:1;		/* marker bit                 */
	unsigned short   pt:7;		/* payload type               */
#else
	unsigned short   cc:4;		/* CSRC count                 */
	unsigned short   x:1;		/* header extension flag      */
	unsigned short   p:1;		/* padding flag               */
	unsigned short   v:2;		/* packet type                */
	unsigned short   pt:7;		/* payload type               */
	unsigned short   m:1;		/* marker bit                 */
#endif
	uint16_t          seq;		/* sequence number            */
	uint32_t          ts;		/* timestamp                  */
	uint32_t          ssrc;		/* synchronization source     */
	/* The csrc list, header extension and data follow, but can't */
	/* be represented in the struct.                              */
} rtp_packet;

The struct begins with pointers to the data in the packet as it came off the wire. The packet is read in such that the header maps onto the latter part of this struct, and the fields in this first part of the struct point into it. The entire packet can be freed by freeing this struct, without having to free the csrc, data and extn blocks separately. WARNING: Don't change the size of the first portion of the struct without changing RTP_PACKET_HEADER_SIZE to match.


RTP_PACKET_HEADER_SIZE

#define RTP_PACKET_HEADER_SIZE ((sizeof(char *) * 2) + sizeof(uint32_t *) + (2 * sizeof(int)))

This macro MUST resolve to the offset of the first packet field in the rtp_packet struct, including all padding. If you change rtp_packet, make sure to change this too.


rtcp_sr

typedef struct {
	uint32_t         ssrc;
	uint32_t         ntp_sec;
	uint32_t         ntp_frac;
	uint32_t         rtp_ts;
	uint32_t         sender_pcount;
	uint32_t         sender_bcount;
} rtcp_sr;


rtcp_rr

typedef struct {
	uint32_t ssrc;		/* The ssrc to which this RR pertains */
#ifdef WORDS_BIGENDIAN
	uint32_t fract_lost:8;
	uint32_t total_lost:24;
#else
	uint32_t total_lost:24;
	uint32_t fract_lost:8;
#endif	
	uint32_t last_seq;
	uint32_t jitter;
	uint32_t lsr;
	uint32_t dlsr;
} rtcp_rr;


enum rtcp_sdes_type

Possible values of Session Description (SDES) identifiers. SDES items are announced via rtp_callback (with RX_SDES events) when they are received. SDES items may be configured and queried with rtp_set_sdes and rtp_get_sdes. The RTP guidelines recommend keeping SDES items constant during an RTP session to avoid confusing end users.

Members

RTCP_SDES_END

Indicates the end of SDES item processing when SDES data is received. Has no meaning with rtp_set_sdes and rtp_get_sdes.

 

RTCP_SDES_CNAME

The canonical name associated with participant. It is algorithmically derived and should never be changed.

 

RTCP_SDES_NAME

The local participant's name, typically displayed in RTP session participant list. The name can take any form, and should remain constant during a session to avoid confusion.

 

RTCP_SDES_EMAIL

The local participant's email address (optional).

 

RTCP_SDES_PHONE

The local participant's telephone number (optional).

 

RTCP_SDES_LOC

The local participant's geographic location (optional).

 

RTCP_SDES_TOOL

The local participant's tool (optional).

 

RTCP_SDES_NOTE

Any additional information the local participant wishes to communicate about themselves (optional).

 

RTCP_SDES_PRIV

Private extension SDES item see RFC1889 for details.

 


rtcp_sdes_item

typedef struct {
	uint8_t		type;		/* type of SDES item              */
	uint8_t		length;		/* length of SDES item (in bytes) */
	char		data[1]; /* text, not zero-terminated      */
} rtcp_sdes_item;


rtcp_app

typedef struct {
#ifdef WORDS_BIGENDIAN
	unsigned short  version:2; /* RTP version            */
	unsigned short  p:1;		/* padding flag           */
	unsigned short  subtype:5; /* application dependent  */
#else
	unsigned short  subtype:5; /* application dependent  */
	unsigned short  p:1;		/* padding flag           */
	unsigned short  version:2; /* RTP version            */
#endif
	unsigned short  pt:8;		/* packet type            */
	uint16_t        length;		/* packet length          */
	uint32_t        ssrc;
	char            name[4];        /* four ASCII characters  */
	char            data[1];        /* variable length field  */
} rtcp_app;


enum rtp_event_type

The possible values for the type field in rtp_event. Each value represents an event in RTP or RTCP processing.

Members

RX_RTP

An RTP data packet was received. The ssrc field contains the RTP data packet's SSRC. The data field points to a rtp_packet containing the RTP data packet. The callback must free the rtp_packet when it's done with it using the xfree() function.

 

RX_SR

An RTCP SR packet was received. The ssrc field contains the SR packet's SSRC. The data field points to an rtcp_sr.

 

RX_RR

An RTCP RR packet was received. The ssrc field contains the RR packet's SSRC. The data field points to an rtcp_rr.

 

RX_SDES

An RTCP SDES packet was received. The ssrc field contains the SDES packet's SSRC. The data field points to an rtcp_sdes_item.

 

RX_BYE

An RTCP BYE packet was received. The ssrc field contains the BYE packet's SSRC.

 

SOURCE_CREATED

A new session participant was heard from. The ssrc field contains the new participant's SSRC.

 

SOURCE_DELETED

A source was deleted from the database. The ssrc field contains the ex-participant's SSRC.

 

RX_RR_EMPTY

An RTCP RR packet with no sources listed was received. The ssrc field contains the RR packet's SSRC.

 

RX_RTCP_START

Called at the start of parsing an RTCP packet. Neither the ssrc nor data fields are valid.

 

RX_RTCP_FINISH

Called at the end of parsing an RTCP packet. Neither the ssrc nor data fields are valid.

 

RR_TIMEOUT

An RR from the given receiver has not been refreshed recently enough. The ssrc field contains the RR packet's SSRC. The data field points to the expiring rtcp_rr.

 

RX_APP

An APP packet was received. The ssrc field contains the APP packet's SSRC. The data field points to an rtcp_app. The callback must free the app packet when it's done with it.

 


rtp_event

typedef struct {
	uint32_t	 ssrc;
	rtp_event_type	 type;
	void		*data;
	struct timeval *ts;
} rtp_event;

The event structure as passed to rtp_callback(). The callback type is carried in the type field; see rtp_event_type for a description of each callback. Unless otherwise noted, the ssrc field contains the SSRC of the participant triggering this callback. The data field points to any data contained in this callback; it must be cast to the appropriate type. The ts field contains the timestamp of the packet reception that caused this event.


enum rtp_option

The possible options for a session that affect which RTP packets are passed to the application and when internal state for sources is created.

Members

RTP_OPT_PROMISC

Pass RTP packets to application irrespective of whether a sender report has been received. Normally, participant state is created when a sender report is received and RTP packets that arrive prior to the sender report are discarded.

 

RTP_OPT_WEAK_VALIDATION

Create source state when RTP packets arrive if none exists. This option is set by default. Disabling the option means source state is only created when sender reports are received.

 

RTP_OPT_FILTER_MY_PACKETS

Filter out packets from local participant if they are received. With multicast sessions, packets may be looped back, causing senders packets to be received locally. This may be filtered out with this option.

 

RTP_OPT_REUSE_PACKET_BUFS

  

See Also