oRTP 0.16.5
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/sessionset.h>
00038 #include <ortp/rtcp.h>
00039 #include <ortp/str_utils.h>
00040 #include <ortp/rtpsignaltable.h>
00041 #include <ortp/event.h>
00042 
00043 
00044 
00045 typedef enum {
00046         RTP_SESSION_RECVONLY,
00047         RTP_SESSION_SENDONLY,
00048         RTP_SESSION_SENDRECV
00049 } RtpSessionMode;
00050 
00051 
00054 typedef struct _JBParameters{
00055         int min_size; 
00056         int nom_size; 
00057         int max_size; 
00058         bool_t adaptive;
00059         bool_t pad[3];
00060         int max_packets; 
00061 } JBParameters;
00062 
00063 typedef struct _JitterControl
00064 {
00065         int count;
00066         int jitt_comp;   /* the user jitt_comp in miliseconds*/
00067         int jitt_comp_ts; /* the jitt_comp converted in rtp time (same unit as timestamp) */
00068         int adapt_jitt_comp_ts;
00069         int64_t slide;
00070         int64_t prev_slide;
00071         float jitter;
00072         int olddiff;
00073         float inter_jitter;     /* interarrival jitter as defined in the RFC */
00074         int corrective_step;
00075         int corrective_slide;
00076         bool_t adaptive;
00077         bool_t enabled;
00078 } JitterControl;
00079 
00080 typedef struct _WaitPoint
00081 {
00082         ortp_mutex_t lock;
00083         ortp_cond_t  cond;
00084         uint32_t time;
00085         bool_t wakeup;
00086 } WaitPoint;
00087 
00088 typedef struct _RtpTransport
00089 {
00090         void *data;
00091         ortp_socket_t (*t_getsocket)(struct _RtpTransport *t);
00092         int  (*t_sendto)(struct _RtpTransport *t, mblk_t *msg , int flags, const struct sockaddr *to, socklen_t tolen);
00093         int  (*t_recvfrom)(struct _RtpTransport *t, mblk_t *msg, int flags, struct sockaddr *from, socklen_t *fromlen);
00094         struct _RtpSession *session;//<back pointer to the owning session, set by oRTP
00095 }  RtpTransport;
00096 
00097 
00098         
00099 typedef struct _RtpStream
00100 {
00101         ortp_socket_t socket;
00102         struct _RtpTransport *tr; 
00103         int sockfamily;
00104         int max_rq_size;
00105         int time_jump;
00106         uint32_t ts_jump;
00107         queue_t rq;
00108         queue_t tev_rq;
00109         mblk_t *cached_mp;
00110         int loc_port;
00111 #ifdef ORTP_INET6
00112         struct sockaddr_storage rem_addr;
00113 #else
00114         struct sockaddr_in rem_addr;
00115 #endif
00116         int rem_addrlen;
00117         void *QoSHandle;
00118         unsigned long QoSFlowID;
00119         JitterControl jittctl;
00120         uint32_t snd_time_offset;/*the scheduler time when the application send its first timestamp*/   
00121         uint32_t snd_ts_offset; /* the first application timestamp sent by the application */
00122         uint32_t snd_rand_offset;       /* a random number added to the user offset to make the stream timestamp*/
00123         uint32_t snd_last_ts;   /* the last stream timestamp sended */
00124         uint32_t rcv_time_offset; /*the scheduler time when the application ask for its first timestamp*/
00125         uint32_t rcv_ts_offset;  /* the first stream timestamp */
00126         uint32_t rcv_query_ts_offset;   /* the first user timestamp asked by the application */
00127         uint32_t rcv_last_ts;   /* the last stream timestamp got by the application */
00128         uint32_t rcv_last_app_ts; /* the last application timestamp asked by the application */ 
00129         uint32_t rcv_last_ret_ts; /* the timestamp of the last sample returned (only for continuous audio)*/
00130         uint32_t hwrcv_extseq; /* last received on socket extended sequence number */
00131         uint32_t hwrcv_seq_at_last_SR;
00132         uint32_t hwrcv_since_last_SR;
00133         uint32_t last_rcv_SR_ts;     /* NTP timestamp (middle 32 bits) of last received SR */
00134         struct timeval last_rcv_SR_time;   /* time at which last SR was received  */
00135         uint16_t snd_seq; /* send sequence number */
00136         uint32_t last_rtcp_report_snt_r;        /* the time of the last rtcp report sent, in recv timestamp unit */
00137         uint32_t last_rtcp_report_snt_s;        /* the time of the last rtcp report sent, in send timestamp unit */
00138         uint32_t rtcp_report_snt_interval; /* the interval in timestamp unit between rtcp report sent */
00139         uint32_t last_rtcp_packet_count; /*the sender's octet count in the last sent RTCP SR*/
00140         uint32_t sent_payload_bytes; /*used for RTCP sender reports*/
00141         unsigned int sent_bytes; /* used for bandwidth estimation */
00142         struct timeval send_bw_start; /* used for bandwidth estimation */
00143         unsigned int recv_bytes; /* used for bandwidth estimation */
00144         struct timeval recv_bw_start; /* used for bandwidth estimation */
00145         rtp_stats_t stats;
00146         int recv_errno;
00147         int send_errno;
00148         int snd_socket_size;
00149         int rcv_socket_size;
00150         int ssrc_changed_thres;
00151 }RtpStream;
00152 
00153 typedef struct _RtcpStream
00154 {
00155         ortp_socket_t socket;
00156         int sockfamily;
00157         struct _RtpTransport *tr; 
00158         mblk_t *cached_mp;
00159 #ifdef ORTP_INET6
00160         struct sockaddr_storage rem_addr;
00161 #else
00162         struct sockaddr_in rem_addr;
00163 #endif
00164         int rem_addrlen;
00165         bool_t enabled; /*tells whether we can send RTCP packets */
00166 } RtcpStream;
00167 
00168 typedef struct _RtpSession RtpSession;
00169 
00170 
00179 struct _RtpSession
00180 {
00181         RtpSession *next;       /* next RtpSession, when the session are enqueued by the scheduler */
00182         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*/
00183         struct {
00184           RtpProfile *profile;
00185           int pt;
00186           unsigned int ssrc;
00187           WaitPoint wp;
00188           int telephone_events_pt;      /* the payload type used for telephony events */
00189         } snd,rcv;
00190         unsigned int inc_ssrc_candidate;
00191         int inc_same_ssrc_count;
00192         int hw_recv_pt; /* recv payload type before jitter buffer */
00193         int recv_buf_size;
00194         RtpSignalTable on_ssrc_changed;
00195         RtpSignalTable on_payload_type_changed;
00196         RtpSignalTable on_telephone_event_packet;
00197         RtpSignalTable on_telephone_event;
00198         RtpSignalTable on_timestamp_jump;
00199         RtpSignalTable on_network_error;
00200         RtpSignalTable on_rtcp_bye;
00201         struct _OList *signal_tables;
00202         struct _OList *eventqs;
00203         msgb_allocator_t allocator;
00204         RtpStream rtp;
00205         RtcpStream rtcp;
00206         RtpSessionMode mode;
00207         struct _RtpScheduler *sched;
00208         uint32_t flags;
00209         int dscp;
00210         int multicast_ttl;
00211         int multicast_loopback;
00212         void * user_data;
00213         /* FIXME: Should be a table for all session participants. */
00214         struct timeval last_recv_time; /* Time of receiving the RTP/RTCP packet. */
00215         mblk_t *pending;
00216         /* telephony events extension */
00217         mblk_t *current_tev;            /* the pending telephony events */
00218         mblk_t *sd;
00219         queue_t contributing_sources;
00220         bool_t symmetric_rtp;
00221         bool_t permissive; /*use the permissive algorithm*/
00222         bool_t use_connect; /* use connect() on the socket */
00223         bool_t ssrc_set;
00224 };
00225         
00226 
00227 
00228 
00229 #ifdef __cplusplus
00230 extern "C"
00231 {
00232 #endif
00233 
00234 /* public API */
00235 RtpSession *rtp_session_new(int mode);
00236 void rtp_session_set_scheduling_mode(RtpSession *session, int yesno);
00237 void rtp_session_set_blocking_mode(RtpSession *session, int yesno);
00238 void rtp_session_set_profile(RtpSession *session, RtpProfile *profile);
00239 void rtp_session_set_send_profile(RtpSession *session,RtpProfile *profile);
00240 void rtp_session_set_recv_profile(RtpSession *session,RtpProfile *profile);
00241 RtpProfile *rtp_session_get_profile(RtpSession *session);
00242 RtpProfile *rtp_session_get_send_profile(RtpSession *session);
00243 RtpProfile *rtp_session_get_recv_profile(RtpSession *session);
00244 int rtp_session_signal_connect(RtpSession *session,const char *signal_name, RtpCallback cb, unsigned long user_data);
00245 int rtp_session_signal_disconnect_by_callback(RtpSession *session,const char *signal_name, RtpCallback cb);
00246 void rtp_session_set_ssrc(RtpSession *session, uint32_t ssrc);
00247 void rtp_session_set_seq_number(RtpSession *session, uint16_t seq);
00248 uint16_t rtp_session_get_seq_number(RtpSession *session);
00249 
00250 void rtp_session_enable_jitter_buffer(RtpSession *session , bool_t enabled);
00251 bool_t rtp_session_jitter_buffer_enabled(const RtpSession *session);
00252 void rtp_session_set_jitter_buffer_params(RtpSession *session, const JBParameters *par);
00253 void rtp_session_get_jitter_buffer_params(RtpSession *session, JBParameters *par);
00254 
00255 /*deprecated jitter control functions*/
00256 void rtp_session_set_jitter_compensation(RtpSession *session, int milisec);
00257 void rtp_session_enable_adaptive_jitter_compensation(RtpSession *session, bool_t val);
00258 bool_t rtp_session_adaptive_jitter_compensation_enabled(RtpSession *session);
00259 
00260 void rtp_session_set_time_jump_limit(RtpSession *session, int miliseconds);
00261 int rtp_session_set_local_addr(RtpSession *session,const char *addr, int port);
00262 int rtp_session_get_local_port(const RtpSession *session);
00263 
00264 int
00265 rtp_session_set_remote_addr_full (RtpSession * session, const char * addr, int rtp_port, int rtcp_port);
00266 /*same as previous function, old name:*/
00267 int rtp_session_set_remote_addr_and_port (RtpSession * session, const char * addr, int rtp_port, int rtcp_port);
00268 int rtp_session_set_remote_addr(RtpSession *session,const char *addr, int port);
00269 /* alternatively to the set_remote_addr() and set_local_addr(), an application can give
00270 a valid socket (potentially connect()ed )to be used by the RtpSession */
00271 void rtp_session_set_sockets(RtpSession *session, int rtpfd, int rtcpfd);
00272 void rtp_session_set_transports(RtpSession *session, RtpTransport *rtptr, RtpTransport *rtcptr);
00273 
00274 /*those methods are provided for people who wants to send non-RTP messages using the RTP/RTCP sockets */
00275 ortp_socket_t rtp_session_get_rtp_socket(const RtpSession *session);
00276 ortp_socket_t rtp_session_get_rtcp_socket(const RtpSession *session);
00277 
00278 
00279 /* QOS / DSCP */
00280 int rtp_session_set_dscp(RtpSession *session, int dscp);
00281 int rtp_session_get_dscp(const RtpSession *session);
00282 
00283 
00284 /* Multicast methods */
00285 int rtp_session_set_multicast_ttl(RtpSession *session, int ttl);
00286 int rtp_session_get_multicast_ttl(RtpSession *session);
00287 
00288 int rtp_session_set_multicast_loopback(RtpSession *session, int yesno);
00289 int rtp_session_get_multicast_loopback(RtpSession *session);
00290 
00291 
00292 
00293 int rtp_session_set_send_payload_type(RtpSession *session, int paytype);
00294 int rtp_session_get_send_payload_type(const RtpSession *session);
00295 
00296 int rtp_session_get_recv_payload_type(const RtpSession *session);
00297 int rtp_session_set_recv_payload_type(RtpSession *session, int pt);
00298 
00299 int rtp_session_set_payload_type(RtpSession *session, int pt);
00300 
00301 void rtp_session_set_symmetric_rtp (RtpSession * session, bool_t yesno);
00302 
00303 void rtp_session_set_connected_mode(RtpSession *session, bool_t yesno);
00304 
00305 void rtp_session_enable_rtcp(RtpSession *session, bool_t yesno);
00306 
00307 void rtp_session_set_ssrc_changed_threshold(RtpSession *session, int numpackets);
00308 
00309 /*low level recv and send functions */
00310 mblk_t * rtp_session_recvm_with_ts (RtpSession * session, uint32_t user_ts);
00311 mblk_t * rtp_session_create_packet(RtpSession *session,int header_size, const uint8_t *payload, int payload_size);
00312 mblk_t * rtp_session_create_packet_with_data(RtpSession *session, uint8_t *payload, int payload_size, void (*freefn)(void*));
00313 mblk_t * rtp_session_create_packet_in_place(RtpSession *session,uint8_t *buffer, int size, void (*freefn)(void*) );
00314 int rtp_session_sendm_with_ts (RtpSession * session, mblk_t *mp, uint32_t userts);
00315 /* high level recv and send functions */
00316 int rtp_session_recv_with_ts(RtpSession *session, uint8_t *buffer, int len, uint32_t ts, int *have_more);
00317 int rtp_session_send_with_ts(RtpSession *session, const uint8_t *buffer, int len, uint32_t userts);
00318 
00319 /* event API*/
00320 void rtp_session_register_event_queue(RtpSession *session, OrtpEvQueue *q);
00321 void rtp_session_unregister_event_queue(RtpSession *session, OrtpEvQueue *q);
00322 
00323 
00324 /* IP bandwidth usage estimation functions, returning bits/s*/
00325 float rtp_session_compute_send_bandwidth(RtpSession *session);
00326 float rtp_session_compute_recv_bandwidth(RtpSession *session);
00327 
00328 void rtp_session_send_rtcp_APP(RtpSession *session, uint8_t subtype, const char *name, const uint8_t *data, int datalen);
00329 
00330 uint32_t rtp_session_get_current_send_ts(RtpSession *session);
00331 uint32_t rtp_session_get_current_recv_ts(RtpSession *session);
00332 void rtp_session_flush_sockets(RtpSession *session);
00333 void rtp_session_release_sockets(RtpSession *session);
00334 void rtp_session_resync(RtpSession *session);
00335 void rtp_session_reset(RtpSession *session);
00336 void rtp_session_destroy(RtpSession *session);
00337 
00338 const rtp_stats_t * rtp_session_get_stats(const RtpSession *session);
00339 void rtp_session_reset_stats(RtpSession *session);
00340 
00341 void rtp_session_set_data(RtpSession *session, void *data);
00342 void *rtp_session_get_data(const RtpSession *session);
00343 
00344 void rtp_session_set_recv_buf_size(RtpSession *session, int bufsize);
00345 void rtp_session_set_rtp_socket_send_buffer_size(RtpSession * session, unsigned int size);
00346 void rtp_session_set_rtp_socket_recv_buffer_size(RtpSession * session, unsigned int size);
00347 
00348 /* in use with the scheduler to convert a timestamp in scheduler time unit (ms) */
00349 uint32_t rtp_session_ts_to_time(RtpSession *session,uint32_t timestamp);
00350 uint32_t rtp_session_time_to_ts(RtpSession *session, int millisecs);
00351 /* this function aims at simulating senders with "imprecise" clocks, resulting in 
00352 rtp packets sent with timestamp uncorrelated with the system clock .
00353 This is only availlable to sessions working with the oRTP scheduler */
00354 void rtp_session_make_time_distorsion(RtpSession *session, int milisec);
00355 
00356 /*RTCP functions */
00357 void rtp_session_set_source_description(RtpSession *session, const char *cname,
00358         const char *name, const char *email, const char *phone, 
00359     const char *loc, const char *tool, const char *note);
00360 void rtp_session_add_contributing_source(RtpSession *session, uint32_t csrc, 
00361     const char *cname, const char *name, const char *email, const char *phone, 
00362     const char *loc, const char *tool, const char *note);
00363 void rtp_session_remove_contributing_sources(RtpSession *session, uint32_t csrc);
00364 mblk_t* rtp_session_create_rtcp_sdes_packet(RtpSession *session);
00365 
00366 void rtp_session_get_last_recv_time(RtpSession *session, struct timeval *tv);
00367 int rtp_session_bye(RtpSession *session, const char *reason);
00368 
00369 int rtp_session_get_last_send_error_code(RtpSession *session);
00370 void rtp_session_clear_send_error_code(RtpSession *session);
00371 int rtp_session_get_last_recv_error_code(RtpSession *session);
00372 void rtp_session_clear_recv_error_code(RtpSession *session);
00373 
00374 /*private */
00375 void rtp_session_init(RtpSession *session, int mode);
00376 #define rtp_session_set_flag(session,flag) (session)->flags|=(flag)
00377 #define rtp_session_unset_flag(session,flag) (session)->flags&=~(flag)
00378 void rtp_session_uninit(RtpSession *session);
00379 
00380 #ifdef __cplusplus
00381 }
00382 #endif
00383 
00384 #endif