# RtpSession API

## Name

RtpSession API -- The RtpSession object provides control on a RTP session as defined in RFC 1889.

## Synopsis

    #include <ortp.h>

    struct      RtpSession;
    enum        RtpSessionMode;
    #define     RTP_CALLBACK_TABLE_MAX_ENTRIES
    RtpSession* rtp_session_new                 (gint mode);
    void        rtp_session_set_scheduling_mode (RtpSession *session,
                                                 gint yesno);
    void        rtp_session_set_blocking_mode   (RtpSession *session,
                                                 gint yesno);
    void        rtp_session_set_profile         (RtpSession *session,
                                                 RtpProfile *profile);
    int         rtp_session_set_local_addr      (RtpSession *session,
                                                 gchar *addr,
                                                 gint port);
    gint        rtp_session_set_remote_addr     (RtpSession *session,
                                                 struct sockaddr_in *dest);
    void        rtp_session_set_jitter_compensation
                                                (RtpSession *session,
                                                 int milisec);
    void        rtp_session_set_ssrc            (RtpSession *session,
                                                 guint32 ssrc);
    void        rtp_session_set_seq_number      (RtpSession *session,
                                                 guint16 seq);
    int         rtp_session_set_payload_type    (RtpSession *session,
                                                 int paytype);
    int         rtp_session_signal_connect      (RtpSession *session,
                                                 char *signal,
                                                 RtpCallback cb,
                                                 gpointer user_data);
    int         rtp_session_signal_disconnect_by_callback
                                                (RtpSession *session,
                                                 char *signal,
                                                 RtpCallback cb);
    gint        rtp_session_send_with_ts        (RtpSession *session,
                                                 gchar *buffer,
                                                 gint len,
                                                 guint32 userts);
    gint        rtp_session_recv_with_ts        (RtpSession *session,
                                                 gchar *buffer,
                                                 gint len,
                                                 guint32 time,
                                                 gint *have_more);
    gint        rtp_session_sendm_with_ts       (RtpSession *session,
                                                 mblk_t *mp,
                                                 guint32 userts);
    mblk_t*     rtp_session_recvm_with_ts       (RtpSession *session,
                                                 guint32 user_ts);
    mblk_t*     rtp_session_create_packet       (RtpSession *session,
                                                 gint header_size,
                                                 char *payload,
                                                 gint payload_size);
    guint32     rtp_session_get_current_send_ts (RtpSession *session);
    void        rtp_session_reset               (RtpSession *session);

## Description

The following api provides the application a way to define a RTP session, send or receives data through it, and to keep informed of the evolution of the RTP session through a simple callback mecanism (see rtp_session_signal_connect() for details).

## Details

### struct RtpSession

    struct RtpSession {

    	RtpSession *next;	/* next RtpSession, when the session are enqueued by the scheduler */
    	RtpProfile *profile;
    	GMutex *lock;
    	guint32 ssrc;
    	gint payload_type;
    	gint max_buf_size;
    #ifdef TARGET_IS_HPUXKERNEL
    	mblk_t *dest_mproto; 	/* a M_PROTO that contains the destination address for outgoing packets*/
    #endif
    	RtpSignalTable on_ssrc_changed;
    	RtpSignalTable on_payload_type_changed;
    	RtpSignalTable on_telephone_event_packet;
    	RtpSignalTable on_telephone_event;
    	RtpSignalTable on_timestamp_jump;
    	RtpStream rtp;
    	RtcpStream rtcp;
    	RtpSessionMode mode;
    #ifdef BUILD_SCHEDULER
    	struct _RtpScheduler *sched;
    #endif
    	guint32 flags;
    	rtp_stats_t stats;
    	gint mask_pos;	/* the position in the scheduler mask of RtpSession */
    	gpointer user_data;

    	/* telephony events extension */
    	gint telephone_events_pt;	/* the payload type used for telephony events */
    	mblk_t *current_tev;		/* the pending telephony events */

    	#ifdef RTCP
    	GList *contributing_sources;
    	#endif
    };

Users should not manipulate this structure directly.

### enum RtpSessionMode

    typedef enum {
    	RTP_SESSION_RECVONLY,
    	RTP_SESSION_SENDONLY,
    	RTP_SESSION_SENDRECV
    } RtpSessionMode;

### RTP_CALLBACK_TABLE_MAX_ENTRIES

    #define RTP_CALLBACK_TABLE_MAX_ENTRIES	5

### rtp_session_new ()

    RtpSession* rtp_session_new                 (gint mode);

Creates a new rtp session.

mode : | One of the RtpSessionMode flags.
Returns : | the newly created rtp session.

### rtp_session_set_scheduling_mode ()

    void        rtp_session_set_scheduling_mode (RtpSession *session,
                                                 gint yesno);

Sets the scheduling mode of the rtp session. If yesno is 1, the rtp session is in the scheduled mode: this means that packet input/output for that session is done by a thread that is in charge of getting and sending packet at regular time interval. This is very usefull for outgoing packets, that have to be sent at a time that matches their timestamp. If yesno is zero, then the session is not scheduled. All recv and send operation will occur when calling respectively rtp_session_recv_with_ts() and rtp_session_send_with_ts().

session : | a rtp session.
yesno : | a boolean to indicate the scheduling mode.

### rtp_session_set_blocking_mode ()

    void        rtp_session_set_blocking_mode   (RtpSession *session,
                                                 gint yesno);

This function defines the behaviour of the rtp_session_recv_with_ts() and rtp_session_send_with_ts() functions. If yesno is 1, rtp_session_recv_with_ts() will block until it is time for the packet to be received, according to the timestamp passed to the function. After this time, the function returns. For rtp_session_send_with_ts(), it will block until it is time for the packet to be sent. If yesno is 0, then the two functions will return immediately.

session : | a rtp session
yesno : | a boolean

### rtp_session_set_profile ()

    void        rtp_session_set_profile         (RtpSession *session,
                                                 RtpProfile *profile);

Set the RTP profile to be used for the session. By default, all session are created by rtp_session_new() are initialized with the AV profile, as defined in RFC 1890. The application can set any other profile instead using that function.

session : | a rtp session
profile : | a rtp profile

### rtp_session_set_local_addr ()

    int         rtp_session_set_local_addr      (RtpSession *session,
                                                 gchar *addr,
                                                 gint port);

Specify the local addr to be use to listen for rtp packets or to send rtp packet from. In case where the rtp session is send-only, then it is not required to call this function: when calling rtp_session_set_remote_addr(), if no local address has been set, then the default INADRR_ANY (0.0.0.0) IP address with a random port will be used. Calling rtp_sesession_set_local_addr() is mandatory when the session is send-only or duplex.

session : | a rtp session freshly created.
addr : | a local IP address in the xxx.xxx.xxx.xxx form.
port : | a local port.
Returns : | 0 on success.

### rtp_session_set_remote_addr ()

    gint        rtp_session_set_remote_addr     (RtpSession *session,
                                                 struct sockaddr_in *dest);

Sets the remote address of the rtp session, ie the destination address where rtp packet are sent. If the session is recv-only or duplex, it sets also the origin of incoming RTP packets. Rtp packets that don't come from addr:port are discarded.

session : | a rtp session freshly created.
dest : |
Returns : | 0 on success.

### rtp_session_set_jitter_compensation ()

    void        rtp_session_set_jitter_compensation
                                                (RtpSession *session,
                                                 int milisec);

Sets the time interval for which packet are buffered instead of being delivered to the application.

session : | a RtpSession
milisec : | the time interval in milisec to be jitter compensed.

### rtp_session_set_ssrc ()

    void        rtp_session_set_ssrc            (RtpSession *session,
                                                 guint32 ssrc);

Sets the SSRC of the session.

session : | a rtp session.
ssrc : | an unsigned 32bit integer representing the synchronisation source identifier (SSRC).

### rtp_session_set_seq_number ()

    void        rtp_session_set_seq_number      (RtpSession *session,
                                                 guint16 seq);

sets the initial sequence number of a sending session.

session : | a rtp session freshly created.
seq : |

### rtp_session_set_payload_type ()

    int         rtp_session_set_payload_type    (RtpSession *session,
                                                 int paytype);

Sets the payload type of the rtp session. It decides of the payload types written in the of the rtp header for the outgoing stream, if the session is SENDRECV or SENDONLY. For the incoming stream, it sets the waited payload type. If that value does not match at any time this waited value, then the application can be informed by registering for the "payload_type_changed" signal, so that it can make the necessary changes on the downstream decoder that deals with the payload of the packets.

session : | a rtp session
paytype : | the payload type
Returns : | 0 on success, -1 if the payload is not defined.

### rtp_session_signal_connect ()

    int         rtp_session_signal_connect      (RtpSession *session,
                                                 char *signal,
                                                 RtpCallback cb,
                                                 gpointer user_data);

This function provides the way for an application to be informed of various events that may occur during a rtp session. signal is a string identifying the event, and cb is a user supplied function in charge of processing it. The application can register several callbacks for the same signal, in the limit of RTP_CALLBACK_TABLE_MAX_ENTRIES. Here are name and meaning of supported signals types:

"ssrc_changed" : the SSRC of the incoming stream has changed.

"payload_type_changed" : the payload type of the incoming stream has changed.

"telephone-event_packet" : a telephone-event rtp packet (RFC1833) is received.

"telephone-event" : a telephone event has occured. This is a shortcut for "telephone-event_packet".

session : | a rtp session
signal : | the name of a signal
cb : | a RtpCallback
user_data : | a pointer to any data to be passed when invoking the callback.
Returns : | 0 on success, -EOPNOTSUPP if the signal does not exists, -1 if no more callbacks can be assigned to the signal type.

### rtp_session_signal_disconnect_by_callback ()

    int         rtp_session_signal_disconnect_by_callback
                                                (RtpSession *session,
                                                 char *signal,
                                                 RtpCallback cb);

Removes callback function cb to the list of callbacks for signal signal.

session : | a rtp session
signal : | a signal name
cb : | a callback function.
Returns : | 0 on success, -ENOENT if the callbacks was not found.

### rtp_session_send_with_ts ()

    gint        rtp_session_send_with_ts        (RtpSession *session,
                                                 gchar *buffer,
                                                 gint len,
                                                 guint32 userts);

Send a rtp datagram to the destination set by rtp_session_set_remote_addr() containing the data from buffer with timestamp userts. This is a high level function that uses rtp_session_create_packet() and rtp_session_sendm_with_ts() to send the data.

session : | a rtp session.
buffer : | a buffer containing the data to be sent in a rtp packet.
len : | the length of the data buffer, in bytes.
userts : | the timestamp of the data to be sent. Refer to the rfc to know what it is.
Returns : | the number of bytes sent over the network.

### rtp_session_recv_with_ts ()

    gint        rtp_session_recv_with_ts        (RtpSession *session,
                                                 gchar *buffer,
                                                 gint len,
                                                 guint32 time,
                                                 gint *have_more);

Tries to read the bytes of the incoming rtp stream related to timestamp time. In case where the user supplied buffer buffer is not large enough to get all the data related to timestamp time, then *( have_more) is set to 1 to indicate that the application should recall the function with the same timestamp to get more data.

When the rtp session is scheduled (see rtp_session_set_scheduling_mode() ), and the blocking mode is on (see rtp_session_set_blocking_mode() ), then the calling thread is suspended until the timestamp given as argument expires, whatever a received packet fits the query or not.

Important note: it is clear that the application cannot know the timestamp of the first packet of the incoming stream, because it can be random. The time timestamp given to the function is used relatively to first timestamp of the stream. In simple words, 0 is a good value to start calling this function.

This function internally calls rtp_session_recvm_with_ts() to get a rtp packet. The content of this packet is then copied into the user supplied buffer in an intelligent manner: the function takes care of the size of the supplied buffer and the timestamp given in argument. Using this function it is possible to read continous audio data (e.g. pcma,pcmu...) with for example a standart buffer of size of 160 with timestamp incrementing by 160 while the incoming stream has a different packet size.

session : | a rtp session.
buffer : | a user supplied buffer to write the data.
len : | the length in bytes of the user supplied buffer.
time : | the timestamp wanted.
have_more : | the address of an integer to indicate if more data is availlable for the given timestamp.

Returns :

if a packet was availlable with the corresponding timestamp supplied in argument then the number of bytes written in the user supplied buffer is returned. If no packets are availlable, either because the sender has not started to send the stream, or either because silence packet are not transmitted, or either because the packet was lost during network transport, then the function returns zero.

### rtp_session_sendm_with_ts ()

    gint        rtp_session_sendm_with_ts       (RtpSession *session,
                                                 mblk_t *mp,
                                                 guint32 userts);

Send the rtp datagram mp to the destination set by rtp_session_set_remote_addr() with timestamp timestamp. For audio data, the timestamp is the number of the first sample resulting of the data transmitted. See rfc1889 for details.

session : | a rtp session.
mp : | a rtp packet presented as a mblk_t.
userts : |
Returns : | the number of bytes sent over the network.

### rtp_session_recvm_with_ts ()

    mblk_t*     rtp_session_recvm_with_ts       (RtpSession *session,
                                                 guint32 user_ts);

Try to get a rtp packet presented as a mblk_t structure from the rtp session. The user_ts parameter is relative to the first timestamp of the incoming stream. In other words, the application does not have to know the first timestamp of the stream, it can simply call for the first time this function with user_ts=0, and then incrementing it as it want. The RtpSession takes care of synchronisation between the stream timestamp and the user timestamp given here.

session : | a rtp session.
user_ts : | a timestamp.
Returns : | a rtp packet presented as a mblk_t.

### rtp_session_create_packet ()

    mblk_t*     rtp_session_create_packet       (RtpSession *session,
                                                 gint header_size,
                                                 char *payload,
                                                 gint payload_size);

Allocates a new rtp packet. In the header, ssrc and payload_type according to the session's context. Timestamp and seq number are not set, there will be set when the packet is going to be sent with rtp_session_sendm_with_ts().

session : | a rtp session.
header_size : | the rtp header size. For standart size (without extensions), it is RTP_FIXED_HEADER_SIZE
payload : | data to be copied into the rtp packet.
payload_size : | size of data carried by the rtp packet.
Returns : | a rtp packet in a mblk_t (message block) structure.

### rtp_session_get_current_send_ts ()

    guint32     rtp_session_get_current_send_ts (RtpSession *session);

When the rtp session is scheduled and has started to send packets, this function computes the timestamp that matches to the present time. Using this function can be usefull when sending discontinuous streams. Some time can be elapsed between the end of a stream burst and the begin of a new stream burst, and the application may be not not aware of this elapsed time. In order to get a valid (current) timestamp to pass to #rtp_session_send_with_ts() or #rtp_session_sendm_with_ts(), the application may use rtp_session_get_current_send_ts().

session : | a rtp session.
Returns : | the actual send timestamp for the rtp session.

### rtp_session_reset ()

    void        rtp_session_reset               (RtpSession *session);

Reset the session: local and remote addresses are kept unchanged but the internal queue for ordering and buffering packets is flushed, the session is ready to be re-synchronised to another incoming stream.

session : | a rtp session.