oRTP  0.16.1
rtpsession.h
Go to the documentation of this file.
1  /*
2  The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack.
3  Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
30 #ifndef RTPSESSION_H
31 #define RTPSESSION_H
32 
33 
34 #include <ortp/port.h>
35 #include <ortp/rtp.h>
36 #include <ortp/payloadtype.h>
37 #include <ortp/sessionset.h>
38 #include <ortp/rtcp.h>
39 #include <ortp/str_utils.h>
40 #include <ortp/rtpsignaltable.h>
41 #include <ortp/event.h>
42 
43 
44 
45 typedef enum {
46  RTP_SESSION_RECVONLY,
47  RTP_SESSION_SENDONLY,
48  RTP_SESSION_SENDRECV
49 } RtpSessionMode;
50 
51 
54 typedef struct _JBParameters{
55  int min_size;
56  int nom_size;
57  int max_size;
58  bool_t adaptive;
59  bool_t pad[3];
61 } JBParameters;
62 
63 typedef struct _JitterControl
64 {
65  int count;
66  int jitt_comp; /* the user jitt_comp in miliseconds*/
67  int jitt_comp_ts; /* the jitt_comp converted in rtp time (same unit as timestamp) */
68  int adapt_jitt_comp_ts;
69  int64_t slide;
70  int64_t prev_slide;
71  float jitter;
72  int olddiff;
73  float inter_jitter; /* interarrival jitter as defined in the RFC */
74  int corrective_step;
75  int corrective_slide;
76  bool_t adaptive;
77  bool_t enabled;
79 
80 typedef struct _WaitPoint
81 {
82  ortp_mutex_t lock;
83  ortp_cond_t cond;
84  uint32_t time;
85  bool_t wakeup;
86 } WaitPoint;
87 
88 typedef struct _RtpTransport
89 {
90  void *data;
91  ortp_socket_t (*t_getsocket)(struct _RtpTransport *t);
92  int (*t_sendto)(struct _RtpTransport *t, mblk_t *msg , int flags, const struct sockaddr *to, socklen_t tolen);
93  int (*t_recvfrom)(struct _RtpTransport *t, mblk_t *msg, int flags, struct sockaddr *from, socklen_t *fromlen);
94  struct _RtpSession *session;//<back pointer to the owning session, set by oRTP
95 } RtpTransport;
96 
97 
98 
99 typedef struct _RtpStream
100 {
101  ortp_socket_t socket;
102  struct _RtpTransport *tr;
103  int sockfamily;
104  int max_rq_size;
105  int time_jump;
106  uint32_t ts_jump;
107  queue_t rq;
108  queue_t tev_rq;
109  mblk_t *cached_mp;
110  int loc_port;
111 #ifdef ORTP_INET6
112  struct sockaddr_storage rem_addr;
113 #else
114  struct sockaddr_in rem_addr;
115 #endif
116  int rem_addrlen;
117  JitterControl jittctl;
118  uint32_t snd_time_offset;/*the scheduler time when the application send its first timestamp*/
119  uint32_t snd_ts_offset; /* the first application timestamp sent by the application */
120  uint32_t snd_rand_offset; /* a random number added to the user offset to make the stream timestamp*/
121  uint32_t snd_last_ts; /* the last stream timestamp sended */
122  uint32_t rcv_time_offset; /*the scheduler time when the application ask for its first timestamp*/
123  uint32_t rcv_ts_offset; /* the first stream timestamp */
124  uint32_t rcv_query_ts_offset; /* the first user timestamp asked by the application */
125  uint32_t rcv_last_ts; /* the last stream timestamp got by the application */
126  uint32_t rcv_last_app_ts; /* the last application timestamp asked by the application */
127  uint32_t rcv_last_ret_ts; /* the timestamp of the last sample returned (only for continuous audio)*/
128  uint32_t hwrcv_extseq; /* last received on socket extended sequence number */
129  uint32_t hwrcv_seq_at_last_SR;
130  uint32_t hwrcv_since_last_SR;
131  uint32_t last_rcv_SR_ts; /* NTP timestamp (middle 32 bits) of last received SR */
132  struct timeval last_rcv_SR_time; /* time at which last SR was received */
133  uint16_t snd_seq; /* send sequence number */
134  uint32_t last_rtcp_report_snt_r; /* the time of the last rtcp report sent, in recv timestamp unit */
135  uint32_t last_rtcp_report_snt_s; /* the time of the last rtcp report sent, in send timestamp unit */
136  uint32_t rtcp_report_snt_interval; /* the interval in timestamp unit between rtcp report sent */
137  uint32_t last_rtcp_packet_count; /*the sender's octet count in the last sent RTCP SR*/
138  uint32_t sent_payload_bytes; /*used for RTCP sender reports*/
139  unsigned int sent_bytes; /* used for bandwidth estimation */
140  struct timeval send_bw_start; /* used for bandwidth estimation */
141  unsigned int recv_bytes; /* used for bandwidth estimation */
142  struct timeval recv_bw_start; /* used for bandwidth estimation */
143  rtp_stats_t stats;
144  int recv_errno;
145  int send_errno;
146  int snd_socket_size;
147  int rcv_socket_size;
148 }RtpStream;
149 
150 typedef struct _RtcpStream
151 {
152  ortp_socket_t socket;
153  int sockfamily;
154  struct _RtpTransport *tr;
155 
156  mblk_t *cached_mp;
157 #ifdef ORTP_INET6
158  struct sockaddr_storage rem_addr;
159 #else
160  struct sockaddr_in rem_addr;
161 #endif
162  int rem_addrlen;
163  bool_t enabled; /*tells whether we can send RTCP packets */
164 } RtcpStream;
165 
166 typedef struct _RtpSession RtpSession;
167 
168 
178 {
179  RtpSession *next; /* next RtpSession, when the session are enqueued by the scheduler */
180  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*/
181  struct {
182  RtpProfile *profile;
183  int pt;
184  unsigned int ssrc;
185  WaitPoint wp;
186  int telephone_events_pt; /* the payload type used for telephony events */
187  } snd,rcv;
188  unsigned int inc_ssrc_candidate;
189  int inc_same_ssrc_count;
190  int hw_recv_pt; /* recv payload type before jitter buffer */
191  int recv_buf_size;
192  RtpSignalTable on_ssrc_changed;
193  RtpSignalTable on_payload_type_changed;
194  RtpSignalTable on_telephone_event_packet;
195  RtpSignalTable on_telephone_event;
196  RtpSignalTable on_timestamp_jump;
197  RtpSignalTable on_network_error;
198  RtpSignalTable on_rtcp_bye;
199  struct _OList *signal_tables;
200  struct _OList *eventqs;
201  msgb_allocator_t allocator;
202  RtpStream rtp;
203  RtcpStream rtcp;
204  RtpSessionMode mode;
205  struct _RtpScheduler *sched;
206  uint32_t flags;
207  int dscp;
208  int multicast_ttl;
209  int multicast_loopback;
210  void * user_data;
211  /* FIXME: Should be a table for all session participants. */
212  struct timeval last_recv_time; /* Time of receiving the RTP/RTCP packet. */
213  mblk_t *pending;
214  /* telephony events extension */
215  mblk_t *current_tev; /* the pending telephony events */
216  mblk_t *sd;
217  queue_t contributing_sources;
218  bool_t symmetric_rtp;
219  bool_t permissive; /*use the permissive algorithm*/
220  bool_t use_connect; /* use connect() on the socket */
221  bool_t ssrc_set;
222 };
223 
224 
225 
226 
227 #ifdef __cplusplus
228 extern "C"
229 {
230 #endif
231 
232 /* public API */
233 RtpSession *rtp_session_new(int mode);
234 void rtp_session_set_scheduling_mode(RtpSession *session, int yesno);
235 void rtp_session_set_blocking_mode(RtpSession *session, int yesno);
236 void rtp_session_set_profile(RtpSession *session, RtpProfile *profile);
237 void rtp_session_set_send_profile(RtpSession *session,RtpProfile *profile);
238 void rtp_session_set_recv_profile(RtpSession *session,RtpProfile *profile);
239 RtpProfile *rtp_session_get_profile(RtpSession *session);
240 RtpProfile *rtp_session_get_send_profile(RtpSession *session);
241 RtpProfile *rtp_session_get_recv_profile(RtpSession *session);
242 int rtp_session_signal_connect(RtpSession *session,const char *signal_name, RtpCallback cb, unsigned long user_data);
243 int rtp_session_signal_disconnect_by_callback(RtpSession *session,const char *signal_name, RtpCallback cb);
244 void rtp_session_set_ssrc(RtpSession *session, uint32_t ssrc);
245 void rtp_session_set_seq_number(RtpSession *session, uint16_t seq);
246 uint16_t rtp_session_get_seq_number(RtpSession *session);
247 
248 void rtp_session_enable_jitter_buffer(RtpSession *session , bool_t enabled);
249 bool_t rtp_session_jitter_buffer_enabled(const RtpSession *session);
250 void rtp_session_set_jitter_buffer_params(RtpSession *session, const JBParameters *par);
251 void rtp_session_get_jitter_buffer_params(RtpSession *session, JBParameters *par);
252 
253 /*deprecated jitter control functions*/
254 void rtp_session_set_jitter_compensation(RtpSession *session, int milisec);
255 void rtp_session_enable_adaptive_jitter_compensation(RtpSession *session, bool_t val);
256 bool_t rtp_session_adaptive_jitter_compensation_enabled(RtpSession *session);
257 
258 void rtp_session_set_time_jump_limit(RtpSession *session, int miliseconds);
259 int rtp_session_set_local_addr(RtpSession *session,const char *addr, int port);
260 int rtp_session_get_local_port(const RtpSession *session);
261 
262 int
263 rtp_session_set_remote_addr_full (RtpSession * session, const char * addr, int rtp_port, int rtcp_port);
264 /*same as previous function, old name:*/
265 int rtp_session_set_remote_addr_and_port (RtpSession * session, const char * addr, int rtp_port, int rtcp_port);
266 int rtp_session_set_remote_addr(RtpSession *session,const char *addr, int port);
267 /* alternatively to the set_remote_addr() and set_local_addr(), an application can give
268 a valid socket (potentially connect()ed )to be used by the RtpSession */
269 void rtp_session_set_sockets(RtpSession *session, int rtpfd, int rtcpfd);
270 void rtp_session_set_transports(RtpSession *session, RtpTransport *rtptr, RtpTransport *rtcptr);
271 
272 /*those methods are provided for people who wants to send non-RTP messages using the RTP/RTCP sockets */
273 ortp_socket_t rtp_session_get_rtp_socket(const RtpSession *session);
274 ortp_socket_t rtp_session_get_rtcp_socket(const RtpSession *session);
275 
276 
277 /* QOS / DSCP */
278 int rtp_session_set_dscp(RtpSession *session, int dscp);
279 int rtp_session_get_dscp(const RtpSession *session);
280 
281 
282 /* Multicast methods */
283 int rtp_session_set_multicast_ttl(RtpSession *session, int ttl);
284 int rtp_session_get_multicast_ttl(RtpSession *session);
285 
286 int rtp_session_set_multicast_loopback(RtpSession *session, int yesno);
287 int rtp_session_get_multicast_loopback(RtpSession *session);
288 
289 
290 
291 int rtp_session_set_send_payload_type(RtpSession *session, int paytype);
292 int rtp_session_get_send_payload_type(const RtpSession *session);
293 
294 int rtp_session_get_recv_payload_type(const RtpSession *session);
295 int rtp_session_set_recv_payload_type(RtpSession *session, int pt);
296 
297 int rtp_session_set_payload_type(RtpSession *session, int pt);
298 
299 void rtp_session_set_symmetric_rtp (RtpSession * session, bool_t yesno);
300 
301 void rtp_session_set_connected_mode(RtpSession *session, bool_t yesno);
302 
303 void rtp_session_enable_rtcp(RtpSession *session, bool_t yesno);
304 
305 /*low level recv and send functions */
306 mblk_t * rtp_session_recvm_with_ts (RtpSession * session, uint32_t user_ts);
307 mblk_t * rtp_session_create_packet(RtpSession *session,int header_size, const uint8_t *payload, int payload_size);
308 mblk_t * rtp_session_create_packet_with_data(RtpSession *session, uint8_t *payload, int payload_size, void (*freefn)(void*));
309 mblk_t * rtp_session_create_packet_in_place(RtpSession *session,uint8_t *buffer, int size, void (*freefn)(void*) );
310 int rtp_session_sendm_with_ts (RtpSession * session, mblk_t *mp, uint32_t userts);
311 /* high level recv and send functions */
312 int rtp_session_recv_with_ts(RtpSession *session, uint8_t *buffer, int len, uint32_t ts, int *have_more);
313 int rtp_session_send_with_ts(RtpSession *session, const uint8_t *buffer, int len, uint32_t userts);
314 
315 /* event API*/
316 void rtp_session_register_event_queue(RtpSession *session, OrtpEvQueue *q);
317 void rtp_session_unregister_event_queue(RtpSession *session, OrtpEvQueue *q);
318 
319 
320 /* IP bandwidth usage estimation functions, returning bits/s*/
321 float rtp_session_compute_send_bandwidth(RtpSession *session);
322 float rtp_session_compute_recv_bandwidth(RtpSession *session);
323 
324 void rtp_session_send_rtcp_APP(RtpSession *session, uint8_t subtype, const char *name, const uint8_t *data, int datalen);
325 
326 uint32_t rtp_session_get_current_send_ts(RtpSession *session);
327 uint32_t rtp_session_get_current_recv_ts(RtpSession *session);
328 void rtp_session_flush_sockets(RtpSession *session);
329 void rtp_session_release_sockets(RtpSession *session);
330 void rtp_session_resync(RtpSession *session);
331 void rtp_session_reset(RtpSession *session);
332 void rtp_session_destroy(RtpSession *session);
333 
334 const rtp_stats_t * rtp_session_get_stats(const RtpSession *session);
335 void rtp_session_reset_stats(RtpSession *session);
336 
337 void rtp_session_set_data(RtpSession *session, void *data);
338 void *rtp_session_get_data(const RtpSession *session);
339 
340 void rtp_session_set_recv_buf_size(RtpSession *session, int bufsize);
341 void rtp_session_set_rtp_socket_send_buffer_size(RtpSession * session, unsigned int size);
342 void rtp_session_set_rtp_socket_recv_buffer_size(RtpSession * session, unsigned int size);
343 
344 /* in use with the scheduler to convert a timestamp in scheduler time unit (ms) */
345 uint32_t rtp_session_ts_to_time(RtpSession *session,uint32_t timestamp);
346 uint32_t rtp_session_time_to_ts(RtpSession *session, int millisecs);
347 /* this function aims at simulating senders with "imprecise" clocks, resulting in
348 rtp packets sent with timestamp uncorrelated with the system clock .
349 This is only availlable to sessions working with the oRTP scheduler */
350 void rtp_session_make_time_distorsion(RtpSession *session, int milisec);
351 
352 /*RTCP functions */
353 void rtp_session_set_source_description(RtpSession *session, const char *cname,
354  const char *name, const char *email, const char *phone,
355  const char *loc, const char *tool, const char *note);
356 void rtp_session_add_contributing_source(RtpSession *session, uint32_t csrc,
357  const char *cname, const char *name, const char *email, const char *phone,
358  const char *loc, const char *tool, const char *note);
359 void rtp_session_remove_contributing_sources(RtpSession *session, uint32_t csrc);
360 mblk_t* rtp_session_create_rtcp_sdes_packet(RtpSession *session);
361 
362 void rtp_session_get_last_recv_time(RtpSession *session, struct timeval *tv);
363 int rtp_session_bye(RtpSession *session, const char *reason);
364 
365 int rtp_session_get_last_send_error_code(RtpSession *session);
366 void rtp_session_clear_send_error_code(RtpSession *session);
367 int rtp_session_get_last_recv_error_code(RtpSession *session);
368 void rtp_session_clear_recv_error_code(RtpSession *session);
369 
370 /*private */
371 void rtp_session_init(RtpSession *session, int mode);
372 #define rtp_session_set_flag(session,flag) (session)->flags|=(flag)
373 #define rtp_session_unset_flag(session,flag) (session)->flags&=~(flag)
374 void rtp_session_uninit(RtpSession *session);
375 
376 #ifdef __cplusplus
377 }
378 #endif
379 
380 #endif