oRTP  0.16.3
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  void *QoSHandle;
118  unsigned long QoSFlowID;
119  JitterControl jittctl;
120  uint32_t snd_time_offset;/*the scheduler time when the application send its first timestamp*/
121  uint32_t snd_ts_offset; /* the first application timestamp sent by the application */
122  uint32_t snd_rand_offset; /* a random number added to the user offset to make the stream timestamp*/
123  uint32_t snd_last_ts; /* the last stream timestamp sended */
124  uint32_t rcv_time_offset; /*the scheduler time when the application ask for its first timestamp*/
125  uint32_t rcv_ts_offset; /* the first stream timestamp */
126  uint32_t rcv_query_ts_offset; /* the first user timestamp asked by the application */
127  uint32_t rcv_last_ts; /* the last stream timestamp got by the application */
128  uint32_t rcv_last_app_ts; /* the last application timestamp asked by the application */
129  uint32_t rcv_last_ret_ts; /* the timestamp of the last sample returned (only for continuous audio)*/
130  uint32_t hwrcv_extseq; /* last received on socket extended sequence number */
131  uint32_t hwrcv_seq_at_last_SR;
132  uint32_t hwrcv_since_last_SR;
133  uint32_t last_rcv_SR_ts; /* NTP timestamp (middle 32 bits) of last received SR */
134  struct timeval last_rcv_SR_time; /* time at which last SR was received */
135  uint16_t snd_seq; /* send sequence number */
136  uint32_t last_rtcp_report_snt_r; /* the time of the last rtcp report sent, in recv timestamp unit */
137  uint32_t last_rtcp_report_snt_s; /* the time of the last rtcp report sent, in send timestamp unit */
138  uint32_t rtcp_report_snt_interval; /* the interval in timestamp unit between rtcp report sent */
139  uint32_t last_rtcp_packet_count; /*the sender's octet count in the last sent RTCP SR*/
140  uint32_t sent_payload_bytes; /*used for RTCP sender reports*/
141  unsigned int sent_bytes; /* used for bandwidth estimation */
142  struct timeval send_bw_start; /* used for bandwidth estimation */
143  unsigned int recv_bytes; /* used for bandwidth estimation */
144  struct timeval recv_bw_start; /* used for bandwidth estimation */
145  rtp_stats_t stats;
146  int recv_errno;
147  int send_errno;
148  int snd_socket_size;
149  int rcv_socket_size;
150  int ssrc_changed_thres;
151 }RtpStream;
152 
153 typedef struct _RtcpStream
154 {
155  ortp_socket_t socket;
156  int sockfamily;
157  struct _RtpTransport *tr;
158  mblk_t *cached_mp;
159 #ifdef ORTP_INET6
160  struct sockaddr_storage rem_addr;
161 #else
162  struct sockaddr_in rem_addr;
163 #endif
164  int rem_addrlen;
165  bool_t enabled; /*tells whether we can send RTCP packets */
166 } RtcpStream;
167 
168 typedef struct _RtpSession RtpSession;
169 
170 
180 {
181  RtpSession *next; /* next RtpSession, when the session are enqueued by the scheduler */
182  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*/
183  struct {
184  RtpProfile *profile;
185  int pt;
186  unsigned int ssrc;
187  WaitPoint wp;
188  int telephone_events_pt; /* the payload type used for telephony events */
189  } snd,rcv;
190  unsigned int inc_ssrc_candidate;
191  int inc_same_ssrc_count;
192  int hw_recv_pt; /* recv payload type before jitter buffer */
193  int recv_buf_size;
194  RtpSignalTable on_ssrc_changed;
195  RtpSignalTable on_payload_type_changed;
196  RtpSignalTable on_telephone_event_packet;
197  RtpSignalTable on_telephone_event;
198  RtpSignalTable on_timestamp_jump;
199  RtpSignalTable on_network_error;
200  RtpSignalTable on_rtcp_bye;
201  struct _OList *signal_tables;
202  struct _OList *eventqs;
203  msgb_allocator_t allocator;
204  RtpStream rtp;
205  RtcpStream rtcp;
206  RtpSessionMode mode;
207  struct _RtpScheduler *sched;
208  uint32_t flags;
209  int dscp;
210  int multicast_ttl;
211  int multicast_loopback;
212  void * user_data;
213  /* FIXME: Should be a table for all session participants. */
214  struct timeval last_recv_time; /* Time of receiving the RTP/RTCP packet. */
215  mblk_t *pending;
216  /* telephony events extension */
217  mblk_t *current_tev; /* the pending telephony events */
218  mblk_t *sd;
219  queue_t contributing_sources;
220  bool_t symmetric_rtp;
221  bool_t permissive; /*use the permissive algorithm*/
222  bool_t use_connect; /* use connect() on the socket */
223  bool_t ssrc_set;
224 };
225 
226 
227 
228 
229 #ifdef __cplusplus
230 extern "C"
231 {
232 #endif
233 
234 /* public API */
235 RtpSession *rtp_session_new(int mode);
236 void rtp_session_set_scheduling_mode(RtpSession *session, int yesno);
237 void rtp_session_set_blocking_mode(RtpSession *session, int yesno);
238 void rtp_session_set_profile(RtpSession *session, RtpProfile *profile);
239 void rtp_session_set_send_profile(RtpSession *session,RtpProfile *profile);
240 void rtp_session_set_recv_profile(RtpSession *session,RtpProfile *profile);
244 int rtp_session_signal_connect(RtpSession *session,const char *signal_name, RtpCallback cb, unsigned long user_data);
245 int rtp_session_signal_disconnect_by_callback(RtpSession *session,const char *signal_name, RtpCallback cb);
246 void rtp_session_set_ssrc(RtpSession *session, uint32_t ssrc);
247 void rtp_session_set_seq_number(RtpSession *session, uint16_t seq);
248 uint16_t rtp_session_get_seq_number(RtpSession *session);
249 
250 void rtp_session_enable_jitter_buffer(RtpSession *session , bool_t enabled);
251 bool_t rtp_session_jitter_buffer_enabled(const RtpSession *session);
252 void rtp_session_set_jitter_buffer_params(RtpSession *session, const JBParameters *par);
253 void rtp_session_get_jitter_buffer_params(RtpSession *session, JBParameters *par);
254 
255 /*deprecated jitter control functions*/
256 void rtp_session_set_jitter_compensation(RtpSession *session, int milisec);
257 void rtp_session_enable_adaptive_jitter_compensation(RtpSession *session, bool_t val);
258 bool_t rtp_session_adaptive_jitter_compensation_enabled(RtpSession *session);
259 
260 void rtp_session_set_time_jump_limit(RtpSession *session, int miliseconds);
261 int rtp_session_set_local_addr(RtpSession *session,const char *addr, int port);
262 int rtp_session_get_local_port(const RtpSession *session);
263 
264 int
265 rtp_session_set_remote_addr_full (RtpSession * session, const char * addr, int rtp_port, int rtcp_port);
266 /*same as previous function, old name:*/
267 int rtp_session_set_remote_addr_and_port (RtpSession * session, const char * addr, int rtp_port, int rtcp_port);
268 int rtp_session_set_remote_addr(RtpSession *session,const char *addr, int port);
269 /* alternatively to the set_remote_addr() and set_local_addr(), an application can give
270 a valid socket (potentially connect()ed )to be used by the RtpSession */
271 void rtp_session_set_sockets(RtpSession *session, int rtpfd, int rtcpfd);
272 void rtp_session_set_transports(RtpSession *session, RtpTransport *rtptr, RtpTransport *rtcptr);
273 
274 /*those methods are provided for people who wants to send non-RTP messages using the RTP/RTCP sockets */
275 ortp_socket_t rtp_session_get_rtp_socket(const RtpSession *session);
276 ortp_socket_t rtp_session_get_rtcp_socket(const RtpSession *session);
277 
278 
279 /* QOS / DSCP */
280 int rtp_session_set_dscp(RtpSession *session, int dscp);
281 int rtp_session_get_dscp(const RtpSession *session);
282 
283 
284 /* Multicast methods */
285 int rtp_session_set_multicast_ttl(RtpSession *session, int ttl);
287 
288 int rtp_session_set_multicast_loopback(RtpSession *session, int yesno);
290 
291 
292 
293 int rtp_session_set_send_payload_type(RtpSession *session, int paytype);
295 
297 int rtp_session_set_recv_payload_type(RtpSession *session, int pt);
298 
299 int rtp_session_set_payload_type(RtpSession *session, int pt);
300 
301 void rtp_session_set_symmetric_rtp (RtpSession * session, bool_t yesno);
302 
303 void rtp_session_set_connected_mode(RtpSession *session, bool_t yesno);
304 
305 void rtp_session_enable_rtcp(RtpSession *session, bool_t yesno);
306 
307 void rtp_session_set_ssrc_changed_threshold(RtpSession *session, int numpackets);
308 
309 /*low level recv and send functions */
310 mblk_t * rtp_session_recvm_with_ts (RtpSession * session, uint32_t user_ts);
311 mblk_t * rtp_session_create_packet(RtpSession *session,int header_size, const uint8_t *payload, int payload_size);
312 mblk_t * rtp_session_create_packet_with_data(RtpSession *session, uint8_t *payload, int payload_size, void (*freefn)(void*));
313 mblk_t * rtp_session_create_packet_in_place(RtpSession *session,uint8_t *buffer, int size, void (*freefn)(void*) );
314 int rtp_session_sendm_with_ts (RtpSession * session, mblk_t *mp, uint32_t userts);
315 /* high level recv and send functions */
316 int rtp_session_recv_with_ts(RtpSession *session, uint8_t *buffer, int len, uint32_t ts, int *have_more);
317 int rtp_session_send_with_ts(RtpSession *session, const uint8_t *buffer, int len, uint32_t userts);
318 
319 /* event API*/
321 void rtp_session_unregister_event_queue(RtpSession *session, OrtpEvQueue *q);
322 
323 
324 /* IP bandwidth usage estimation functions, returning bits/s*/
325 float rtp_session_compute_send_bandwidth(RtpSession *session);
326 float rtp_session_compute_recv_bandwidth(RtpSession *session);
327 
328 void rtp_session_send_rtcp_APP(RtpSession *session, uint8_t subtype, const char *name, const uint8_t *data, int datalen);
329 
334 void rtp_session_resync(RtpSession *session);
335 void rtp_session_reset(RtpSession *session);
336 void rtp_session_destroy(RtpSession *session);
337 
338 const rtp_stats_t * rtp_session_get_stats(const RtpSession *session);
339 void rtp_session_reset_stats(RtpSession *session);
340 
341 void rtp_session_set_data(RtpSession *session, void *data);
342 void *rtp_session_get_data(const RtpSession *session);
343 
344 void rtp_session_set_recv_buf_size(RtpSession *session, int bufsize);
345 void rtp_session_set_rtp_socket_send_buffer_size(RtpSession * session, unsigned int size);
346 void rtp_session_set_rtp_socket_recv_buffer_size(RtpSession * session, unsigned int size);
347 
348 /* in use with the scheduler to convert a timestamp in scheduler time unit (ms) */
349 uint32_t rtp_session_ts_to_time(RtpSession *session,uint32_t timestamp);
350 uint32_t rtp_session_time_to_ts(RtpSession *session, int millisecs);
351 /* this function aims at simulating senders with "imprecise" clocks, resulting in
352 rtp packets sent with timestamp uncorrelated with the system clock .
353 This is only availlable to sessions working with the oRTP scheduler */
354 void rtp_session_make_time_distorsion(RtpSession *session, int milisec);
355 
356 /*RTCP functions */
357 void rtp_session_set_source_description(RtpSession *session, const char *cname,
358  const char *name, const char *email, const char *phone,
359  const char *loc, const char *tool, const char *note);
360 void rtp_session_add_contributing_source(RtpSession *session, uint32_t csrc,
361  const char *cname, const char *name, const char *email, const char *phone,
362  const char *loc, const char *tool, const char *note);
363 void rtp_session_remove_contributing_sources(RtpSession *session, uint32_t csrc);
364 mblk_t* rtp_session_create_rtcp_sdes_packet(RtpSession *session);
365 
366 void rtp_session_get_last_recv_time(RtpSession *session, struct timeval *tv);
367 int rtp_session_bye(RtpSession *session, const char *reason);
368 
369 int rtp_session_get_last_send_error_code(RtpSession *session);
370 void rtp_session_clear_send_error_code(RtpSession *session);
371 int rtp_session_get_last_recv_error_code(RtpSession *session);
372 void rtp_session_clear_recv_error_code(RtpSession *session);
373 
374 /*private */
375 void rtp_session_init(RtpSession *session, int mode);
376 #define rtp_session_set_flag(session,flag) (session)->flags|=(flag)
377 #define rtp_session_unset_flag(session,flag) (session)->flags&=~(flag)
378 void rtp_session_uninit(RtpSession *session);
379 
380 #ifdef __cplusplus
381 }
382 #endif
383 
384 #endif
mblk_t * rtp_session_recvm_with_ts(RtpSession *session, uint32_t user_ts)
Definition: rtpsession.c:970
void rtp_session_release_sockets(RtpSession *session)
Definition: rtpsession.c:1273
void rtp_session_set_scheduling_mode(RtpSession *session, int yesno)
Definition: rtpsession.c:331
Definition: rtpsession.h:153
RtpProfile * rtp_session_get_send_profile(RtpSession *session)
Definition: rtpsession.c:456
void rtp_session_get_last_recv_time(RtpSession *session, struct timeval *tv)
Definition: rtpsession.c:1599
void rtp_session_set_seq_number(RtpSession *session, uint16_t seq)
Definition: rtpsession.c:577
void rtp_session_flush_sockets(RtpSession *session)
Definition: rtpsession_inet.c:783
void rtp_session_set_ssrc(RtpSession *session, uint32_t ssrc)
Definition: rtpsession.c:595
int max_packets
Definition: rtpsession.h:60
Using and creating standart and custom RTP profiles.
int rtp_session_set_send_payload_type(RtpSession *session, int paytype)
Definition: rtpsession.c:626
void rtp_session_destroy(RtpSession *session)
Definition: rtpsession.c:1545
Definition: rtpsession.h:179
Definition: str_utils.h:27
int rtp_session_get_multicast_loopback(RtpSession *session)
Definition: rtpsession_inet.c:434
void rtp_session_set_profile(RtpSession *session, RtpProfile *profile)
Definition: rtpsession.c:385
int rtp_session_get_local_port(const RtpSession *session)
Definition: rtpsession_inet.c:576
int rtp_session_send_with_ts(RtpSession *session, const uint8_t *buffer, int len, uint32_t userts)
Definition: rtpsession.c:909
int rtp_session_set_dscp(RtpSession *session, int dscp)
Definition: rtpsession_inet.c:449
int rtp_session_set_remote_addr(RtpSession *session, const char *addr, int port)
Definition: rtpsession_inet.c:610
Definition: rtpsession.h:99
uint32_t rtp_session_get_current_send_ts(RtpSession *session)
Definition: rtpsession.c:1209
void rtp_session_set_symmetric_rtp(RtpSession *session, bool_t yesno)
Definition: rtpsession.c:1474
Definition: rtpsession.h:80
int min_size
Definition: rtpsession.h:55
int rtp_session_get_multicast_ttl(RtpSession *session)
Definition: rtpsession_inet.c:358
const rtp_stats_t * rtp_session_get_stats(const RtpSession *session)
Definition: rtpsession.c:1438
Definition: rtpsignaltable.h:27
uint32_t rtp_session_get_current_recv_ts(RtpSession *session)
Definition: rtpsession.c:1235
Definition: str_utils.h:47
RtpProfile * rtp_session_get_recv_profile(RtpSession *session)
Definition: rtpsession.c:466
void rtp_session_set_blocking_mode(RtpSession *session, int yesno)
Definition: rtpsession.c:366
int rtp_session_get_send_payload_type(const RtpSession *session)
Definition: rtpsession.c:637
int rtp_session_signal_connect(RtpSession *session, const char *signal_name, RtpCallback cb, unsigned long user_data)
Definition: rtpsession.c:532
struct _JBParameters JBParameters
void rtp_session_register_event_queue(RtpSession *session, OrtpEvQueue *q)
Definition: rtpsession.c:1300
void rtp_session_set_jitter_compensation(RtpSession *session, int milisec)
Definition: jitterctl.c:141
int max_size
Definition: rtpsession.h:57
void rtp_session_set_ssrc_changed_threshold(RtpSession *session, int numpackets)
Definition: rtpsession.c:1387
Definition: event.h:70
int rtp_session_set_local_addr(RtpSession *session, const char *addr, int port)
Definition: rtpsession_inet.c:250
Definition: utils.h:32
int rtp_session_get_recv_payload_type(const RtpSession *session)
Definition: rtpsession.c:670
mblk_t * rtp_session_create_packet(RtpSession *session, int header_size, const uint8_t *payload, int payload_size)
Definition: rtpsession.c:715
void rtp_session_set_recv_profile(RtpSession *session, RtpProfile *profile)
Definition: rtpsession.c:432
Definition: rtpsession.h:88
Definition: rtp.h:63
void rtp_session_set_connected_mode(RtpSession *session, bool_t yesno)
Definition: rtpsession.c:1492
void rtp_session_set_send_profile(RtpSession *session, RtpProfile *profile)
Definition: rtpsession.c:414
void * rtp_session_get_data(const RtpSession *session)
Definition: rtpsession.c:1460
int rtp_session_set_multicast_ttl(RtpSession *session, int ttl)
Definition: rtpsession_inet.c:304
RtpSession * rtp_session_new(int mode)
Definition: rtpsession.c:304
void rtp_session_set_rtp_socket_send_buffer_size(RtpSession *session, unsigned int size)
Definition: rtpsession.c:486
mblk_t * rtp_session_create_packet_with_data(RtpSession *session, uint8_t *payload, int payload_size, void(*freefn)(void *))
Definition: rtpsession.c:749
RtpProfile * rtp_session_get_profile(RtpSession *session)
Definition: rtpsession.c:445
Definition: rtpsession.h:54
int nom_size
Definition: rtpsession.h:56
Definition: str_utils.h:119
int rtp_session_set_remote_addr_full(RtpSession *session, const char *addr, int rtp_port, int rtcp_port)
Definition: rtpsession_inet.c:629
int rtp_session_signal_disconnect_by_callback(RtpSession *session, const char *signal_name, RtpCallback cb)
Definition: rtpsession.c:556
void rtp_session_set_rtp_socket_recv_buffer_size(RtpSession *session, unsigned int size)
Definition: rtpsession.c:494
Definition: scheduler.h:28
int rtp_session_set_multicast_loopback(RtpSession *session, int yesno)
Definition: rtpsession_inet.c:374
int rtp_session_sendm_with_ts(RtpSession *session, mblk_t *mp, uint32_t userts)
Definition: rtpsession.c:889
Sending and receiving multiple streams together with only one thread.
int rtp_session_bye(RtpSession *session, const char *reason)
Definition: rtcp.c:380
void rtp_session_set_source_description(RtpSession *session, const char *cname, const char *name, const char *email, const char *phone, const char *loc, const char *tool, const char *note)
Definition: rtcp.c:82
void rtp_session_set_data(RtpSession *session, void *data)
Definition: rtpsession.c:1452
void rtp_session_set_recv_buf_size(RtpSession *session, int bufsize)
Definition: rtpsession.c:478
int rtp_session_set_recv_payload_type(RtpSession *session, int pt)
Definition: rtpsession.c:653
void rtp_session_enable_rtcp(RtpSession *session, bool_t yesno)
Definition: rtpsession.c:400
Definition: payloadtype.h:85
Definition: rtpsession.h:63
void rtp_session_set_time_jump_limit(RtpSession *session, int miliseconds)
Definition: rtpsession.c:1262
int rtp_session_recv_with_ts(RtpSession *session, uint8_t *buffer, int len, uint32_t ts, int *have_more)
Definition: rtpsession.c:1161
mblk_t * rtp_session_create_packet_in_place(RtpSession *session, uint8_t *buffer, int size, void(*freefn)(void *))
Definition: rtpsession.c:781
void rtp_session_resync(RtpSession *session)
Definition: rtpsession.c:1397
int rtp_session_get_dscp(const RtpSession *session)
Definition: rtpsession_inet.c:560
void rtp_session_reset(RtpSession *session)
Definition: rtpsession.c:1410
int rtp_session_set_payload_type(RtpSession *session, int pt)
Definition: rtpsession.c:683