oRTP  0.22.0
include/ortp/rtpsession.h
Go to the documentation of this file.
00001  /*
00002   The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack.
00003   Copyright (C) 2001  Simon MORLAT simon.morlat@linphone.org
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the Free Software
00017   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00030 #ifndef RTPSESSION_H
00031 #define RTPSESSION_H
00032 
00033 
00034 #include <ortp/port.h>
00035 #include <ortp/rtp.h>
00036 #include <ortp/payloadtype.h>
00037 #include <ortp/rtpprofile.h>
00038 #include <ortp/sessionset.h>
00039 #include <ortp/rtcp.h>
00040 #include <ortp/str_utils.h>
00041 #include <ortp/rtpsignaltable.h>
00042 #include <ortp/event.h>
00043 
00044 
00045 
00046 typedef enum {
00047         RTP_SESSION_RECVONLY,
00048         RTP_SESSION_SENDONLY,
00049         RTP_SESSION_SENDRECV
00050 } RtpSessionMode;
00051 
00052 
00055 typedef struct _JBParameters{
00056         int min_size; 
00057         int nom_size; 
00058         int max_size; 
00059         bool_t adaptive;
00060         bool_t pad[3];
00061         int max_packets; 
00062 } JBParameters;
00063 
00064 typedef struct _JitterControl
00065 {
00066         unsigned int count;
00067         int jitt_comp;   /* the user jitt_comp in miliseconds*/
00068         int jitt_comp_ts; /* the jitt_comp converted in rtp time (same unit as timestamp) */
00069         int adapt_jitt_comp_ts;
00070         int64_t slide;
00071         int64_t prev_slide;
00072         float jitter;
00073         int olddiff;
00074         float inter_jitter;     /* interarrival jitter as defined in the RFC */
00075         int corrective_step;
00076         int corrective_slide;
00077         uint64_t cum_jitter_buffer_size; /*in timestamp units*/
00078         unsigned int cum_jitter_buffer_count; /*used for computation of jitter buffer size*/
00079         int clock_rate;
00080         bool_t adaptive;
00081         bool_t enabled;
00082 } JitterControl;
00083 
00084 typedef struct _WaitPoint
00085 {
00086         ortp_mutex_t lock;
00087         ortp_cond_t  cond;
00088         uint32_t time;
00089         bool_t wakeup;
00090 } WaitPoint;
00091 
00092 typedef struct _RtpTransport
00093 {
00094         void *data;
00095         ortp_socket_t (*t_getsocket)(struct _RtpTransport *t);
00096         int  (*t_sendto)(struct _RtpTransport *t, mblk_t *msg , int flags, const struct sockaddr *to, socklen_t tolen);
00097         int  (*t_recvfrom)(struct _RtpTransport *t, mblk_t *msg, int flags, struct sockaddr *from, socklen_t *fromlen);
00098         struct _RtpSession *session;//<back pointer to the owning session, set by oRTP
00099         void  (*t_close)(struct _RtpTransport *transport, void *userData);
00100 }  RtpTransport;
00101 
00102 typedef struct _OrtpNetworkSimulatorParams{
00103         int enabled;
00104         float max_bandwidth; /*IP bandwidth, in bit/s*/
00105         float loss_rate;
00106 }OrtpNetworkSimulatorParams;
00107 
00108 typedef struct _OrtpNetworkSimulatorCtx{
00109         OrtpNetworkSimulatorParams params;
00110         int bit_budget;
00111         int qsize;
00112         queue_t q;
00113         struct timeval last_check;
00114 }OrtpNetworkSimulatorCtx;
00115 
00116 typedef struct _RtpStream
00117 {
00118         ortp_socket_t socket;
00119         struct _RtpTransport *tr; 
00120         int sockfamily;
00121         int max_rq_size;
00122         int time_jump;
00123         uint32_t ts_jump;
00124         queue_t rq;
00125         queue_t tev_rq;
00126         mblk_t *cached_mp;
00127         int loc_port;
00128 #ifdef ORTP_INET6
00129         struct sockaddr_storage rem_addr;
00130 #else
00131         struct sockaddr_in rem_addr;
00132 #endif
00133         int rem_addrlen;
00134         void *QoSHandle;
00135         unsigned long QoSFlowID;
00136         JitterControl jittctl;
00137         uint32_t snd_time_offset;/*the scheduler time when the application send its first timestamp*/   
00138         uint32_t snd_ts_offset; /* the first application timestamp sent by the application */
00139         uint32_t snd_rand_offset;       /* a random number added to the user offset to make the stream timestamp*/
00140         uint32_t snd_last_ts;   /* the last stream timestamp sended */
00141         uint32_t rcv_time_offset; /*the scheduler time when the application ask for its first timestamp*/
00142         uint32_t rcv_ts_offset;  /* the first stream timestamp */
00143         uint32_t rcv_query_ts_offset;   /* the first user timestamp asked by the application */
00144         uint32_t rcv_last_ts;   /* the last stream timestamp got by the application */
00145         uint32_t rcv_last_app_ts; /* the last application timestamp asked by the application */ 
00146         uint32_t rcv_last_ret_ts; /* the timestamp of the last sample returned (only for continuous audio)*/
00147         uint32_t hwrcv_extseq; /* last received on socket extended sequence number */
00148         uint32_t hwrcv_seq_at_last_SR;
00149         uint32_t hwrcv_since_last_SR;
00150         uint32_t last_rcv_SR_ts;     /* NTP timestamp (middle 32 bits) of last received SR */
00151         struct timeval last_rcv_SR_time;   /* time at which last SR was received  */
00152         uint16_t snd_seq; /* send sequence number */
00153         uint32_t last_rtcp_packet_count; /*the sender's octet count in the last sent RTCP SR*/
00154         uint32_t sent_payload_bytes; /*used for RTCP sender reports*/
00155         unsigned int sent_bytes; /* used for bandwidth estimation */
00156         struct timeval send_bw_start; /* used for bandwidth estimation */
00157         unsigned int recv_bytes; /* used for bandwidth estimation */
00158         struct timeval recv_bw_start; /* used for bandwidth estimation */
00159         rtp_stats_t stats;
00160         int recv_errno;
00161         int send_errno;
00162         int snd_socket_size;
00163         int rcv_socket_size;
00164         int ssrc_changed_thres;
00165         jitter_stats_t jitter_stats;
00166 }RtpStream;
00167 
00168 typedef struct _RtcpStream
00169 {
00170         ortp_socket_t socket;
00171         int sockfamily;
00172         struct _RtpTransport *tr; 
00173         mblk_t *cached_mp;
00174         int loc_port;
00175 #ifdef ORTP_INET6
00176         struct sockaddr_storage rem_addr;
00177 #else
00178         struct sockaddr_in rem_addr;
00179 #endif
00180         int rem_addrlen;
00181         int interval;
00182         uint32_t last_rtcp_report_snt_r;        /* the time of the last rtcp report sent, in recv timestamp unit */
00183         uint32_t last_rtcp_report_snt_s;        /* the time of the last rtcp report sent, in send timestamp unit */
00184         uint32_t rtcp_report_snt_interval_r; /* the interval in timestamp unit for receive path between rtcp report sent */
00185         uint32_t rtcp_report_snt_interval_s; /* the interval in timestamp unit for send path between rtcp report sent */
00186         bool_t enabled; /*tells whether we can send RTCP packets */
00187 } RtcpStream;
00188 
00189 typedef struct _RtpSession RtpSession;
00190 
00191 
00200 struct _RtpSession
00201 {
00202         RtpSession *next;       /* next RtpSession, when the session are enqueued by the scheduler */
00203         int mask_pos;   /* the position in the scheduler mask of RtpSession : do not move this field: it is part of the ABI since the session_set macros use it*/
00204         struct {
00205                 RtpProfile *profile;
00206                 int pt;
00207                 unsigned int ssrc;
00208                 WaitPoint wp;
00209                 int telephone_events_pt;        /* the payload type used for telephony events */
00210         } snd,rcv;
00211         unsigned int inc_ssrc_candidate;
00212         int inc_same_ssrc_count;
00213         int hw_recv_pt; /* recv payload type before jitter buffer */
00214         int recv_buf_size;
00215         RtpSignalTable on_ssrc_changed;
00216         RtpSignalTable on_payload_type_changed;
00217         RtpSignalTable on_telephone_event_packet;
00218         RtpSignalTable on_telephone_event;
00219         RtpSignalTable on_timestamp_jump;
00220         RtpSignalTable on_network_error;
00221         RtpSignalTable on_rtcp_bye;
00222         struct _OList *signal_tables;
00223         struct _OList *eventqs;
00224         msgb_allocator_t allocator;
00225         RtpStream rtp;
00226         RtcpStream rtcp;
00227         RtpSessionMode mode;
00228         struct _RtpScheduler *sched;
00229         uint32_t flags;
00230         int dscp;
00231         int multicast_ttl;
00232         int multicast_loopback;
00233         void * user_data;
00234         /* FIXME: Should be a table for all session participants. */
00235         struct timeval last_recv_time; /* Time of receiving the RTP/RTCP packet. */
00236         mblk_t *pending;
00237         /* telephony events extension */
00238         mblk_t *current_tev;            /* the pending telephony events */
00239         mblk_t *sd;
00240         queue_t contributing_sources;
00241         unsigned int lost_packets_test_vector;
00242         unsigned int interarrival_jitter_test_vector;
00243         unsigned int delay_test_vector;
00244         float rtt;/*last round trip delay calculated*/
00245         OrtpNetworkSimulatorCtx *net_sim_ctx;
00246         bool_t symmetric_rtp;
00247         bool_t permissive; /*use the permissive algorithm*/
00248         bool_t use_connect; /* use connect() on the socket */
00249         bool_t ssrc_set;
00250         bool_t reuseaddr; /*setsockopt SO_REUSEADDR */
00251 };
00252         
00253 
00254 
00255 
00256 #ifdef __cplusplus
00257 extern "C"
00258 {
00259 #endif
00260 
00261 /* public API */
00262 ORTP_PUBLIC RtpSession *rtp_session_new(int mode);
00263 ORTP_PUBLIC void rtp_session_set_scheduling_mode(RtpSession *session, int yesno);
00264 ORTP_PUBLIC void rtp_session_set_blocking_mode(RtpSession *session, int yesno);
00265 ORTP_PUBLIC void rtp_session_set_profile(RtpSession *session, RtpProfile *profile);
00266 ORTP_PUBLIC void rtp_session_set_send_profile(RtpSession *session,RtpProfile *profile);
00267 ORTP_PUBLIC void rtp_session_set_recv_profile(RtpSession *session,RtpProfile *profile);
00268 ORTP_PUBLIC RtpProfile *rtp_session_get_profile(RtpSession *session);
00269 ORTP_PUBLIC RtpProfile *rtp_session_get_send_profile(RtpSession *session);
00270 ORTP_PUBLIC RtpProfile *rtp_session_get_recv_profile(RtpSession *session);
00271 ORTP_PUBLIC int rtp_session_signal_connect(RtpSession *session,const char *signal_name, RtpCallback cb, unsigned long user_data);
00272 ORTP_PUBLIC int rtp_session_signal_disconnect_by_callback(RtpSession *session,const char *signal_name, RtpCallback cb);
00273 ORTP_PUBLIC void rtp_session_set_ssrc(RtpSession *session, uint32_t ssrc);
00274 ORTP_PUBLIC uint32_t rtp_session_get_send_ssrc(RtpSession* session);
00275 ORTP_PUBLIC uint32_t rtp_session_get_recv_ssrc(RtpSession *session);
00276 ORTP_PUBLIC void rtp_session_set_seq_number(RtpSession *session, uint16_t seq);
00277 ORTP_PUBLIC uint16_t rtp_session_get_seq_number(RtpSession *session);
00278 ORTP_PUBLIC uint32_t rtp_session_get_rcv_ext_seq_number(RtpSession *session);
00279 
00280 ORTP_PUBLIC void rtp_session_enable_jitter_buffer(RtpSession *session , bool_t enabled);
00281 ORTP_PUBLIC bool_t rtp_session_jitter_buffer_enabled(const RtpSession *session);
00282 ORTP_PUBLIC void rtp_session_set_jitter_buffer_params(RtpSession *session, const JBParameters *par);
00283 ORTP_PUBLIC void rtp_session_get_jitter_buffer_params(RtpSession *session, JBParameters *par);
00284 
00285 /*deprecated jitter control functions*/
00286 ORTP_PUBLIC void rtp_session_set_jitter_compensation(RtpSession *session, int milisec);
00287 ORTP_PUBLIC void rtp_session_enable_adaptive_jitter_compensation(RtpSession *session, bool_t val);
00288 ORTP_PUBLIC bool_t rtp_session_adaptive_jitter_compensation_enabled(RtpSession *session);
00289 
00290 ORTP_PUBLIC void rtp_session_set_time_jump_limit(RtpSession *session, int miliseconds);
00291 ORTP_PUBLIC int rtp_session_set_local_addr(RtpSession *session,const char *addr, int rtp_port, int rtcp_port);
00292 ORTP_PUBLIC int rtp_session_get_local_port(const RtpSession *session);
00293 
00294 ORTP_PUBLIC int
00295 rtp_session_set_remote_addr_full (RtpSession * session, const char * rtp_addr, int rtp_port, const char * rtcp_addr, int rtcp_port);
00296 /*same as previous function, old name:*/
00297 ORTP_PUBLIC int rtp_session_set_remote_addr_and_port (RtpSession * session, const char * addr, int rtp_port, int rtcp_port);
00298 ORTP_PUBLIC int rtp_session_set_remote_addr(RtpSession *session,const char *addr, int port);
00299 /* alternatively to the set_remote_addr() and set_local_addr(), an application can give
00300 a valid socket (potentially connect()ed )to be used by the RtpSession */
00301 ORTP_PUBLIC void rtp_session_set_sockets(RtpSession *session, int rtpfd, int rtcpfd);
00302 ORTP_PUBLIC void rtp_session_set_transports(RtpSession *session, RtpTransport *rtptr, RtpTransport *rtcptr);
00303 
00304 /*those methods are provided for people who wants to send non-RTP messages using the RTP/RTCP sockets */
00305 ORTP_PUBLIC ortp_socket_t rtp_session_get_rtp_socket(const RtpSession *session);
00306 ORTP_PUBLIC ortp_socket_t rtp_session_get_rtcp_socket(const RtpSession *session);
00307 
00308 
00309 /* QOS / DSCP */
00310 ORTP_PUBLIC int rtp_session_set_dscp(RtpSession *session, int dscp);
00311 ORTP_PUBLIC int rtp_session_get_dscp(const RtpSession *session);
00312 
00313 
00314 /* Packet info */
00315 ORTP_PUBLIC int rtp_session_set_pktinfo(RtpSession *session, int activate);
00316 
00317 /* Multicast methods */
00318 ORTP_PUBLIC int rtp_session_set_multicast_ttl(RtpSession *session, int ttl);
00319 ORTP_PUBLIC int rtp_session_get_multicast_ttl(RtpSession *session);
00320 
00321 ORTP_PUBLIC int rtp_session_set_multicast_loopback(RtpSession *session, int yesno);
00322 ORTP_PUBLIC int rtp_session_get_multicast_loopback(RtpSession *session);
00323 
00324 
00325 
00326 ORTP_PUBLIC int rtp_session_set_send_payload_type(RtpSession *session, int paytype);
00327 ORTP_PUBLIC int rtp_session_get_send_payload_type(const RtpSession *session);
00328 
00329 ORTP_PUBLIC int rtp_session_get_recv_payload_type(const RtpSession *session);
00330 ORTP_PUBLIC int rtp_session_set_recv_payload_type(RtpSession *session, int pt);
00331 
00332 ORTP_PUBLIC int rtp_session_set_payload_type(RtpSession *session, int pt);
00333 
00334 ORTP_PUBLIC void rtp_session_set_symmetric_rtp (RtpSession * session, bool_t yesno);
00335 
00336 ORTP_PUBLIC void rtp_session_set_connected_mode(RtpSession *session, bool_t yesno);
00337 
00338 ORTP_PUBLIC void rtp_session_enable_rtcp(RtpSession *session, bool_t yesno);
00339 
00340 ORTP_PUBLIC void rtp_session_set_rtcp_report_interval(RtpSession *session, int value_ms);
00341 
00342 ORTP_PUBLIC void rtp_session_set_ssrc_changed_threshold(RtpSession *session, int numpackets);
00343 
00344 /*low level recv and send functions */
00345 ORTP_PUBLIC mblk_t * rtp_session_recvm_with_ts (RtpSession * session, uint32_t user_ts);
00346 ORTP_PUBLIC mblk_t * rtp_session_create_packet(RtpSession *session,int header_size, const uint8_t *payload, int payload_size);
00347 ORTP_PUBLIC mblk_t * rtp_session_create_packet_with_data(RtpSession *session, uint8_t *payload, int payload_size, void (*freefn)(void*));
00348 ORTP_PUBLIC mblk_t * rtp_session_create_packet_in_place(RtpSession *session,uint8_t *buffer, int size, void (*freefn)(void*) );
00349 ORTP_PUBLIC int rtp_session_sendm_with_ts (RtpSession * session, mblk_t *mp, uint32_t userts);
00350 /* high level recv and send functions */
00351 ORTP_PUBLIC int rtp_session_recv_with_ts(RtpSession *session, uint8_t *buffer, int len, uint32_t ts, int *have_more);
00352 ORTP_PUBLIC int rtp_session_send_with_ts(RtpSession *session, const uint8_t *buffer, int len, uint32_t userts);
00353 
00354 /* event API*/
00355 ORTP_PUBLIC void rtp_session_register_event_queue(RtpSession *session, OrtpEvQueue *q);
00356 ORTP_PUBLIC void rtp_session_unregister_event_queue(RtpSession *session, OrtpEvQueue *q);
00357 
00358 
00359 /* IP bandwidth usage estimation functions, returning bits/s*/
00360 ORTP_PUBLIC float rtp_session_compute_send_bandwidth(RtpSession *session);
00361 ORTP_PUBLIC float rtp_session_compute_recv_bandwidth(RtpSession *session);
00362 
00363 ORTP_PUBLIC void rtp_session_send_rtcp_APP(RtpSession *session, uint8_t subtype, const char *name, const uint8_t *data, int datalen);
00364 
00365 ORTP_PUBLIC uint32_t rtp_session_get_current_send_ts(RtpSession *session);
00366 ORTP_PUBLIC uint32_t rtp_session_get_current_recv_ts(RtpSession *session);
00367 ORTP_PUBLIC void rtp_session_flush_sockets(RtpSession *session);
00368 ORTP_PUBLIC void rtp_session_release_sockets(RtpSession *session);
00369 ORTP_PUBLIC void rtp_session_resync(RtpSession *session);
00370 ORTP_PUBLIC void rtp_session_reset(RtpSession *session);
00371 ORTP_PUBLIC void rtp_session_destroy(RtpSession *session);
00372 
00373 ORTP_PUBLIC const rtp_stats_t * rtp_session_get_stats(const RtpSession *session);
00374 ORTP_PUBLIC const jitter_stats_t * rtp_session_get_jitter_stats( const RtpSession *session );
00375 ORTP_PUBLIC void rtp_session_reset_stats(RtpSession *session);
00376 
00377 ORTP_PUBLIC void rtp_session_set_data(RtpSession *session, void *data);
00378 ORTP_PUBLIC void *rtp_session_get_data(const RtpSession *session);
00379 
00380 ORTP_PUBLIC void rtp_session_set_recv_buf_size(RtpSession *session, int bufsize);
00381 ORTP_PUBLIC void rtp_session_set_rtp_socket_send_buffer_size(RtpSession * session, unsigned int size);
00382 ORTP_PUBLIC void rtp_session_set_rtp_socket_recv_buffer_size(RtpSession * session, unsigned int size);
00383 
00384 /* in use with the scheduler to convert a timestamp in scheduler time unit (ms) */
00385 ORTP_PUBLIC uint32_t rtp_session_ts_to_time(RtpSession *session,uint32_t timestamp);
00386 ORTP_PUBLIC uint32_t rtp_session_time_to_ts(RtpSession *session, int millisecs);
00387 /* this function aims at simulating senders with "imprecise" clocks, resulting in 
00388 rtp packets sent with timestamp uncorrelated with the system clock .
00389 This is only availlable to sessions working with the oRTP scheduler */
00390 ORTP_PUBLIC void rtp_session_make_time_distorsion(RtpSession *session, int milisec);
00391 
00392 /*RTCP functions */
00393 ORTP_PUBLIC void rtp_session_set_source_description(RtpSession *session, const char *cname,
00394         const char *name, const char *email, const char *phone, 
00395     const char *loc, const char *tool, const char *note);
00396 ORTP_PUBLIC void rtp_session_add_contributing_source(RtpSession *session, uint32_t csrc, 
00397     const char *cname, const char *name, const char *email, const char *phone, 
00398     const char *loc, const char *tool, const char *note);
00399 ORTP_PUBLIC void rtp_session_remove_contributing_sources(RtpSession *session, uint32_t csrc);
00400 ORTP_PUBLIC mblk_t* rtp_session_create_rtcp_sdes_packet(RtpSession *session);
00401 
00402 ORTP_PUBLIC void rtp_session_get_last_recv_time(RtpSession *session, struct timeval *tv);
00403 ORTP_PUBLIC int rtp_session_bye(RtpSession *session, const char *reason);
00404 
00405 ORTP_PUBLIC int rtp_session_get_last_send_error_code(RtpSession *session);
00406 ORTP_PUBLIC void rtp_session_clear_send_error_code(RtpSession *session);
00407 ORTP_PUBLIC int rtp_session_get_last_recv_error_code(RtpSession *session);
00408 ORTP_PUBLIC void rtp_session_clear_recv_error_code(RtpSession *session);
00409 
00410 
00411 ORTP_PUBLIC float rtp_session_get_round_trip_propagation(RtpSession *session);
00412 
00413 
00414 ORTP_PUBLIC void rtp_session_enable_network_simulation(RtpSession *session, const OrtpNetworkSimulatorParams *params);
00415 ORTP_PUBLIC void rtp_session_rtcp_set_lost_packet_value( RtpSession *session, const unsigned int value );
00416 ORTP_PUBLIC void rtp_session_rtcp_set_jitter_value(RtpSession *session, const unsigned int value );
00417 ORTP_PUBLIC void rtp_session_rtcp_set_delay_value(RtpSession *session, const unsigned int value );
00418 ORTP_PUBLIC mblk_t * rtp_session_pick_with_cseq (RtpSession * session, const uint16_t sequence_number);
00419 /*private */
00420 ORTP_PUBLIC void rtp_session_init(RtpSession *session, int mode);
00421 #define rtp_session_set_flag(session,flag) (session)->flags|=(flag)
00422 #define rtp_session_unset_flag(session,flag) (session)->flags&=~(flag)
00423 ORTP_PUBLIC void rtp_session_uninit(RtpSession *session);
00424 ORTP_PUBLIC void rtp_session_dispatch_event(RtpSession *session, OrtpEvent *ev);
00425 
00426 ORTP_PUBLIC void rtp_session_set_reuseaddr(RtpSession *session, bool_t yes);
00427 
00428 #ifdef __cplusplus
00429 }
00430 #endif
00431 
00432 #endif