1 /*
2  * The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) implementation with additional features.
3  * Copyright (C) 2017 Belledonne Communications SARL
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program 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
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 /**
21  * \file rtpsession.h
22  * \brief The RtpSession api
23  *
24  * The RtpSession objects represent a RTP session: once it is configured with
25  * local and remote network addresses and a payload type is given, it let you send
26  * and recv a media stream.
27 **/
28 
29 
30 #ifndef RTPSESSION_H
31 #define RTPSESSION_H
32 
33 
34 #include <bctoolbox/list.h>
35 
36 #include <ortp/port.h>
37 #include <ortp/rtp.h>
38 #include <ortp/payloadtype.h>
39 #include <ortp/rtpprofile.h>
40 #include <ortp/sessionset.h>
41 #include <ortp/rtcp.h>
42 #include <ortp/str_utils.h>
43 #include <ortp/utils.h>
44 #include <ortp/rtpsignaltable.h>
45 #include <ortp/event.h>
46 
47 #define ORTP_AVPF_FEATURE_NONE 0
48 #define ORTP_AVPF_FEATURE_TMMBR (1 << 0)
49 #define ORTP_AVPF_FEATURE_GENERIC_NACK (1 << 1)
50 
51 
52 typedef enum {
53 	RTP_SESSION_RECVONLY,
54 	RTP_SESSION_SENDONLY,
55 	RTP_SESSION_SENDRECV
56 } RtpSessionMode;
57 
58 
59 typedef enum _OrtpJitterBufferAlgorithm {
60 	OrtpJitterBufferBasic,
61 	OrtpJitterBufferRecursiveLeastSquare,
62 } OrtpJitterBufferAlgorithm;
63 
64 /*! Jitter buffer parameters
65 */
66 typedef struct _JBParameters{
67 	int min_size; /*(adaptive=TRUE only) maximum dynamic delay to be added to incoming packets (ms) */
68 	int nom_size; /*(adaptive=TRUE only) initial dynamic delay to be added to incoming packets (ms) */
69 	int max_size; /*(adaptive=TRUE only) minimum dynamic delay to be added to incoming packets (ms) */
70 	bool_t adaptive; /*either a dynamic buffer should be used or not to compensate bursts */
71 	bool_t enabled; /*whether jitter buffer is enabled*/
72 	bool_t pad[2]; /*(dev only) alignment pad: insert your bool_t here*/
73 	int max_packets; /**max number of packets allowed to be queued in the jitter buffer */
74 	OrtpJitterBufferAlgorithm buffer_algorithm;
75 	int refresh_ms; /* (adaptive=TRUE only) dynamic buffer size update frequency (ms) */
76 	int ramp_threshold; /*(adaptive=TRUE, algo=RLS only) Percentage in [0;100] threshold between current jitter and previous jitter to enable smooth ramp*/
77 	int ramp_step_ms; /*(adaptive=TRUE, algo=RLS only) In smooth ramp, how much we should reduce jitter size on each step*/
78 	int ramp_refresh_ms; /*(adaptive=TRUE, algo=RLS only) In smooth ramp, frequency of step*/
79 } JBParameters;
80 
81 typedef struct _JitterControl
82 {
83 	JBParameters params;
84 	unsigned int count; /* number of packets handled in jitter_control_new_packet. Used internally only. */
85 	int jitt_comp_ts; /* the nominal jitter buffer size converted in rtp time (same unit as timestamp) */
86 	int adapt_jitt_comp_ts;
87 	int32_t clock_offset_ts; /*offset difference between local and distant clock, in timestamp units*/
88 	int32_t prev_clock_offset_ts;
89 	int32_t olddiff;
90 	float jitter;
91 	float inter_jitter;	/* interarrival jitter as defined in the RFC */
92 	float jitter_buffer_mean_size; /*effective size (fullness) of jitter buffer*/
93 	int corrective_step;
94 	int corrective_slide;
95 	uint64_t cum_jitter_buffer_size; /*in timestamp units*/
96 	unsigned int cum_jitter_buffer_count; /*used for computation of jitter buffer size*/
97 	int clock_rate;
98 	uint32_t adapt_refresh_prev_ts; /*last time we refreshed the buffer*/
99 	OrtpExtremum max_ts_deviation; /*maximum difference between packet and expected timestamps */
100 	OrtpKalmanRLS kalman_rls;
101 	double capped_clock_ratio;
102 	uint32_t last_log_ts;
103 	uint32_t local_ts_start;
104 	uint32_t remote_ts_start;
105 } JitterControl;
106 
107 typedef struct _WaitPoint
108 {
109 	ortp_mutex_t lock;
110 	ortp_cond_t  cond;
111 	uint32_t time;
112 	bool_t wakeup;
113 } WaitPoint;
114 
115 typedef struct _RtpTransportModifier
116 {
117 	void *data;
118 	struct _RtpSession *session;//<back pointer to the owning session, set by oRTP
119 	struct _RtpTransport *transport;//<back point to the owning transport, set by oRTP
120 	int  (*t_process_on_send)(struct _RtpTransportModifier *t, mblk_t *msg);
121 	int  (*t_process_on_receive)(struct _RtpTransportModifier *t, mblk_t *msg);
122 	void  (*t_process_on_schedule)(struct _RtpTransportModifier *t); /*invoked each time rtp_session_recvm is called even is no message are available*/
123 	/**
124 	 * Mandatory callback responsible of freeing the #_RtpTransportModifier AND the pointer.
125 	 * @param[in] transport #_RtpTransportModifier object to free.
126 	 */
127 	void  (*t_destroy)(struct _RtpTransportModifier *transport);
128 } RtpTransportModifier;
129 
130 typedef struct _RtpTransport
131 {
132 	void *data;
133 	struct _RtpSession *session;//<back pointer to the owning session, set by oRTP
134 	ortp_socket_t (*t_getsocket)(struct _RtpTransport *t);
135 	int  (*t_sendto)(struct _RtpTransport *t, mblk_t *msg , int flags, const struct sockaddr *to, socklen_t tolen);
136 	int  (*t_recvfrom)(struct _RtpTransport *t, mblk_t *msg, int flags, struct sockaddr *from, socklen_t *fromlen);
137 	void  (*t_close)(struct _RtpTransport *transport);
138 	/**
139 	 * Mandatory callback responsible of freeing the #_RtpTransport object AND the pointer.
140 	 * @param[in] transport #_RtpTransport object to free.
141 	 */
142 	void  (*t_destroy)(struct _RtpTransport *transport);
143 }  RtpTransport;
144 
145 typedef enum _OrtpNetworkSimulatorMode{
146 	OrtpNetworkSimulatorInvalid=-1,
147 	OrtpNetworkSimulatorInbound,/**<simulation is applied when receiving packets*/
148 	OrtpNetworkSimulatorOutbound, /**<simulation is applied to sent packets*/
149 	OrtpNetworkSimulatorOutboundControlled /**<simulation is applied to sent packets according to sent timestamps
150 				set in the timestamps field of mblk_t, which is defined only with -DORTP_TIMESTAMP */
151 }OrtpNetworkSimulatorMode;
152 
153 /**
154  * Structure describing the network simulator parameters
155 **/
156 typedef struct _OrtpNetworkSimulatorParams{
157 	int enabled; /**<Whether simulation is enabled or off.*/
158 	float max_bandwidth; /**<IP bandwidth, in bit/s.
159 						This limitation is applied after loss are simulated, so incoming bandwidth
160 						is NOT socket bandwidth, but after-loss-simulation bandwidth e.g with 50% loss, the bandwidth will be 50% reduced*/
161 	int max_buffer_size; /**<Max number of bit buffered before being discarded*/
162 	float loss_rate; /**<Percentage of lost packets*/
163 	uint32_t latency; /**<Packet transmission delay, in ms*/
164 	float consecutive_loss_probability;/**< a probability of having a subsequent loss after a loss occurred, in a 0-1 range. Useful to simulate burst of lost packets*/
165 	float jitter_burst_density; /**<density of gap/bursts events. A value of 1 means one gap/burst per second approximately*/
166 	float jitter_strength; /**<percentage of max_bandwidth artificially consumed during bursts events*/
167 	bool_t rtp_only; /**True for only RTP packet loss, False for both RTP and RTCP */
168 	bool_t pad[3];
169 	OrtpNetworkSimulatorMode mode; /**<whether simulation is applied to inbound or outbound stream.*/
170 }OrtpNetworkSimulatorParams;
171 
172 typedef struct _OrtpNetworkSimulatorCtx{
173 	OrtpNetworkSimulatorParams params;
174 	int bit_budget;
175 	int qsize;
176 	queue_t q;/*queue used for simulating bandwidth limit*/
177 	queue_t latency_q;
178 	queue_t send_q; /*used only for OrtpNetworkSimulatorOutbound direction*/
179 	struct timeval last_check;
180 	uint64_t last_jitter_event;
181 	int consecutive_drops;
182 	int drops_to_ignore;
183 	int drop_by_congestion;
184 	int drop_by_loss;
185 	int total_count; /*total number of packets gone through the simulator*/
186 	ortp_mutex_t mutex;
187 	ortp_thread_t thread;
188 	bool_t in_jitter_event;
189 	bool_t thread_started;
190 }OrtpNetworkSimulatorCtx;
191 
192 typedef struct OrtpRtcpSendAlgorithm {
193 	uint64_t tn; /* Time of the next scheduled RTCP RR transmission in milliseconds. */
194 	uint64_t tp; /* Time of the last scheduled RTCP RR transmission in milliseconds. */
195 	uint64_t t_rr_last; /* Time of the last regular RTCP packet sent in milliseconds. */
196 	uint32_t T_rr; /* Interval for the scheduling of the next regular RTCP packet. */
197 	uint32_t T_max_fb_delay; /* Interval within which a feeback message is considered to be useful to the sender. */
198 	uint32_t T_rr_interval; /* Minimal interval to be used between regular RTCP packets. */
199 	uint32_t T_rr_current_interval;
200 	uint32_t Tmin; /* Minimal interval between RTCP packets. */
201 	float avg_rtcp_size;
202 	mblk_t *fb_packets;
203 	bool_t initialized; /* Whether the RTCP send algorithm is fully initialized. */
204 	bool_t initial;
205 	bool_t allow_early;
206 	bool_t tmmbr_scheduled;
207 	bool_t tmmbn_scheduled;
208 } OrtpRtcpSendAlgorithm;
209 
210 typedef struct OrtpRtcpFbConfiguration {
211 	bool_t generic_nack_enabled;
212 	bool_t tmmbr_enabled;
213 } OrtpRtcpFbConfiguration;
214 
215 #define ORTP_RTCP_XR_UNAVAILABLE_PARAMETER 127
216 
217 typedef enum {
218 	OrtpRtcpXrNoPlc,
219 	OrtpRtcpXrSilencePlc,
220 	OrtpRtcpXrEnhancedPlc
221 } OrtpRtcpXrPlcStatus;
222 
223 typedef OrtpRtcpXrPlcStatus (*OrtpRtcpXrPlcCallback)(void *userdata);
224 typedef int (*OrtpRtcpXrSignalLevelCallback)(void *userdata);
225 typedef int (*OrtpRtcpXrNoiseLevelCallback)(void *userdata);
226 typedef float (*OrtpRtcpXrAverageQualityIndicatorCallback)(void *userdata);
227 
228 typedef struct OrtpRtcpXrMediaCallbacks {
229 	OrtpRtcpXrPlcCallback plc;
230 	OrtpRtcpXrSignalLevelCallback signal_level;
231 	OrtpRtcpXrNoiseLevelCallback noise_level;
232 	OrtpRtcpXrAverageQualityIndicatorCallback average_qi;
233 	OrtpRtcpXrAverageQualityIndicatorCallback average_lq_qi;
234 	void *userdata;
235 } OrtpRtcpXrMediaCallbacks;
236 
237 typedef enum {
238 	OrtpRtcpXrRcvrRttNone,
239 	OrtpRtcpXrRcvrRttAll,
240 	OrtpRtcpXrRcvrRttSender
241 } OrtpRtcpXrRcvrRttMode;
242 
243 typedef enum {
244 	OrtpRtcpXrStatSummaryLoss = (1 << 7),
245 	OrtpRtcpXrStatSummaryDup = (1 << 6),
246 	OrtpRtcpXrStatSummaryJitt = (1 << 5),
247 	OrtpRtcpXrStatSummaryTTL = (1 << 3),
248 	OrtpRtcpXrStatSummaryHL = (1 << 4)
249 } OrtpRtcpXrStatSummaryFlag;
250 
251 typedef struct OrtpRtcpXrConfiguration {
252 	bool_t enabled;
253 	bool_t stat_summary_enabled;
254 	bool_t voip_metrics_enabled;
255 	bool_t pad;
256 	OrtpRtcpXrRcvrRttMode rcvr_rtt_mode;
257 	int rcvr_rtt_max_size;
258 	OrtpRtcpXrStatSummaryFlag stat_summary_flags;
259 } OrtpRtcpXrConfiguration;
260 
261 typedef struct OrtpRtcpXrStats {
262 	uint32_t last_rcvr_rtt_ts;	/* NTP timestamp (middle 32 bits) of last received XR rcvr-rtt */
263 	struct timeval last_rcvr_rtt_time;	/* Time at which last XR rcvr-rtt was received  */
264 	uint16_t rcv_seq_at_last_stat_summary;	/* Received sequence number at last XR stat-summary sent */
265 	uint32_t rcv_since_last_stat_summary;	/* The number of packets received since last XR stat-summary was sent */
266 	uint32_t dup_since_last_stat_summary;	/* The number of duplicate packets received since last XR stat-summary was sent */
267 	uint32_t min_jitter_since_last_stat_summary;	/* The minimum value of jitter since last XR stat-summary was sent */
268 	uint32_t max_jitter_since_last_stat_summary;	/* The maximum value of jitter since last XR stat-summary was sent */
269 	double olds_jitter_since_last_stat_summary;
270 	double oldm_jitter_since_last_stat_summary;
271 	double news_jitter_since_last_stat_summary;
272 	double newm_jitter_since_last_stat_summary;
273 	int64_t last_jitter_diff_since_last_stat_summary;
274 	double olds_ttl_or_hl_since_last_stat_summary;
275 	double oldm_ttl_or_hl_since_last_stat_summary;
276 	double news_ttl_or_hl_since_last_stat_summary;
277 	double newm_ttl_or_hl_since_last_stat_summary;
278 	uint8_t min_ttl_or_hl_since_last_stat_summary;	/* The minimum value of TTL/HL since last XR stat-summary was sent */
279 	uint8_t max_ttl_or_hl_since_last_stat_summary;	/* The maximum value of TTL/HL since last XR stat-summary was sent */
280 	uint32_t first_rcv_seq;
281 	uint32_t last_rcv_seq;
282 	uint32_t rcv_count;
283 	uint32_t discarded_count;
284 } OrtpRtcpXrStats;
285 
286 typedef struct OrtpRtcpTmmbrInfo {
287 	mblk_t *sent;
288 	mblk_t *received;
289 } OrtpRtcpTmmbrInfo;
290 
291 typedef struct _OrtpAddress{
292 	struct sockaddr_storage addr;
293 	socklen_t len;
294 }OrtpAddress;
295 
296 typedef struct _OrtpStream {
297 	ortp_socket_t socket;
298 	int sockfamily;
299 	int loc_port;
300 	socklen_t rem_addrlen;
301 	struct sockaddr_storage rem_addr;
302 	socklen_t loc_addrlen;
303 	struct sockaddr_storage loc_addr;
304 	struct _RtpTransport *tr;
305 	OrtpBwEstimator recv_bw_estimator;
306 	struct timeval send_bw_start; /* used for bandwidth estimation */
307 	struct timeval recv_bw_start; /* used for bandwidth estimation */
308 	unsigned int sent_bytes; /* used for bandwidth estimation */
309 	unsigned int recv_bytes; /* used for bandwidth estimation */
310 	float upload_bw;
311 	float download_bw;
312 	float average_upload_bw;
313 	float average_download_bw;
314 	bctbx_list_t *aux_destinations; /*list of OrtpAddress */
315 	msgb_allocator_t allocator;
316 } OrtpStream;
317 
318 typedef struct _RtpStream
319 {
320 	OrtpStream gs;
321 	int time_jump;
322 	uint32_t ts_jump;
323 	queue_t rq;
324 	queue_t tev_rq;
325 	void *QoSHandle;
326 	unsigned long QoSFlowID;
327 	JitterControl jittctl;
328 	uint32_t snd_time_offset;/*the scheduler time when the application send its first timestamp*/
329 	uint32_t snd_ts_offset;	/* the first application timestamp sent by the application */
330 	uint32_t snd_rand_offset;	/* a random number added to the user offset to make the stream timestamp*/
331 	uint32_t snd_last_ts;	/* the last stream timestamp sended */
332 	uint32_t rcv_time_offset; /*the scheduler time when the application ask for its first timestamp*/
333 	uint32_t rcv_ts_offset;  /* the first stream timestamp */
334 	uint32_t rcv_query_ts_offset;	/* the first user timestamp asked by the application */
335 	uint32_t rcv_last_ts;	/* the last stream timestamp got by the application */
336 	uint16_t rcv_last_seq;	/* the last stream sequence number got by the application*/
337 	uint16_t pad;
338 	uint32_t rcv_last_app_ts; /* the last application timestamp asked by the application */
339 	uint32_t rcv_last_ret_ts; /* the timestamp of the last sample returned (only for continuous audio)*/
340 	uint32_t hwrcv_extseq; /* last received on socket extended sequence number */
341 	uint32_t hwrcv_seq_at_last_SR;
342 	uint32_t hwrcv_since_last_SR;
343 	uint32_t last_rcv_SR_ts;     /* NTP timestamp (middle 32 bits) of last received SR */
344 	struct timeval last_rcv_SR_time;   /* time at which last SR was received  */
345 	uint16_t snd_seq; /* send sequence number */
346 	uint32_t last_rtcp_packet_count; /*the sender's octet count in the last sent RTCP SR*/
347 	uint32_t sent_payload_bytes; /*used for RTCP sender reports*/
348 	int recv_errno;
349 	int send_errno;
350 	int snd_socket_size;
351 	int rcv_socket_size;
352 	int ssrc_changed_thres;
353 	jitter_stats_t jitter_stats;
354 	struct _OrtpCongestionDetector *congdetect;
355 	struct _OrtpVideoBandwidthEstimator *video_bw_estimator;
356 }RtpStream;
357 
358 typedef struct _RtcpStream
359 {
360 	OrtpStream gs;
361 	OrtpRtcpSendAlgorithm send_algo;
362 	OrtpRtcpXrConfiguration xr_conf;
363 	OrtpRtcpXrMediaCallbacks xr_media_callbacks;
364 	OrtpRtcpTmmbrInfo tmmbr_info;
365 	bool_t enabled; /*tells whether we can send RTCP packets */
366 	bool_t rtcp_xr_dlrr_to_send;
367 	uint8_t rtcp_fb_fir_seq_nr;	/* The FIR command sequence number */
368 	uint32_t last_rtcp_fb_pli_snt;
369 } RtcpStream;
370 
371 typedef struct _RtpSession RtpSession;
372 
373 
374 /**
375  * An object representing a bi-directional RTP session.
376  * It holds sockets, jitter buffer, various counters (timestamp, sequence numbers...)
377  * Applications SHOULD NOT try to read things within the RtpSession object but use
378  * instead its public API (the rtp_session_* methods) where RtpSession is used as a
379  * pointer.
380  * rtp_session_new() allocates and initialize a RtpSession.
381 **/
382 struct _RtpSession
383 {
384 	RtpSession *next;	/* next RtpSession, when the session are enqueued by the scheduler */
385 	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*/
386 	struct {
387 		RtpProfile *profile;
388 		int pt;
389 		unsigned int ssrc;
390 		WaitPoint wp;
391 	} snd,rcv;
392 	unsigned int inc_ssrc_candidate;
393 	int inc_same_ssrc_count;
394 	int hw_recv_pt; /* recv payload type before jitter buffer */
395 	int recv_buf_size;
396 	int target_upload_bandwidth; /* Target upload bandwidth at network layer (with IP and UDP headers) in bits/s */
397 	RtpSignalTable on_ssrc_changed;
398 	RtpSignalTable on_payload_type_changed;
399 	RtpSignalTable on_telephone_event_packet;
400 	RtpSignalTable on_telephone_event;
401 	RtpSignalTable on_timestamp_jump;
402 	RtpSignalTable on_network_error;
403 	RtpSignalTable on_rtcp_bye;
404 	bctbx_list_t *signal_tables;
405 	bctbx_list_t *eventqs;
406 	RtpStream rtp;
407 	RtcpStream rtcp;
408 	OrtpRtcpXrStats rtcp_xr_stats;
409 	RtpSessionMode mode;
410 	struct _RtpScheduler *sched;
411 	uint32_t flags;
412 	int dscp;
413 	int multicast_ttl;
414 	int multicast_loopback;
415 	float duplication_ratio; /* Number of times a packet should be duplicated */
416 	float duplication_left ; /* Remainder of the duplication ratio, internal use */
417 	void * user_data;
418 	/* FIXME: Should be a table for all session participants. */
419 	struct timeval last_recv_time; /* Time of receiving the RTP/RTCP packet. */
420 	mblk_t *pending;
421 	/* telephony events extension */
422 	int tev_send_pt; /*telephone event to be used for sending*/
423 	mblk_t *current_tev;		/* the pending telephony events */
424 	mblk_t *minimal_sdes;
425 	mblk_t *full_sdes;
426 	queue_t contributing_sources;
427 	int lost_packets_test_vector;
428 	unsigned int interarrival_jitter_test_vector;
429 	unsigned int delay_test_vector;
430 	float rtt;/*last round trip delay calculated*/
431 	int cum_loss;
432 	OrtpNetworkSimulatorCtx *net_sim_ctx;
433 	RtpSession *spliced_session; /*a RtpSession that will retransmit everything received on this session*/
434 	rtp_stats_t stats;
435 	bctbx_list_t *recv_addr_map;
436 	uint32_t send_ts_offset; /*additional offset to add when sending packets */
437 	bool_t symmetric_rtp;
438 	bool_t permissive; /*use the permissive algorithm*/
439 	bool_t use_connect; /* use connect() on the socket */
440 	bool_t ssrc_set;
441 
442 	bool_t reuseaddr; /*setsockopt SO_REUSEADDR */
443 	bool_t rtcp_mux;
444 	unsigned char avpf_features; /**< A bitmask of ORTP_AVPF_FEATURE_* macros. */
445 	bool_t use_pktinfo;
446 
447 	bool_t is_spliced;
448 	bool_t congestion_detector_enabled;
449 	bool_t video_bandwidth_estimator_enabled;
450 };
451 
452 /**
453  * Structure describing the video bandwidth estimator parameters
454 **/
455 typedef struct _OrtpVideoBandwidthEstimatorParams {
456 	int enabled; /**<Whether estimator is enabled or off.*/
457 	unsigned int packet_count_min; /** minimum number of packets with the same sent timestamp to be processed continuously before being used */
458 	unsigned int packets_size_max; /** number of packets needed to compute the available video bandwidth */
459 	unsigned int trust_percentage; /** percentage for which the chosen bandwidth value in all available will be inferior */
460 } OrtpVideoBandwidthEstimatorParams;
461 
462 
463 
464 
465 #ifdef __cplusplus
466 extern "C"
467 {
468 #endif
469 
470 ORTP_PUBLIC const char *ortp_network_simulator_mode_to_string(OrtpNetworkSimulatorMode mode);
471 ORTP_PUBLIC OrtpNetworkSimulatorMode ortp_network_simulator_mode_from_string(const char *str);
472 
473 
474 /* public API */
475 ORTP_PUBLIC RtpSession *rtp_session_new(int mode);
476 ORTP_PUBLIC void rtp_session_set_scheduling_mode(RtpSession *session, int yesno);
477 ORTP_PUBLIC void rtp_session_set_blocking_mode(RtpSession *session, int yesno);
478 ORTP_PUBLIC void rtp_session_set_profile(RtpSession *session, RtpProfile *profile);
479 ORTP_PUBLIC void rtp_session_set_send_profile(RtpSession *session,RtpProfile *profile);
480 ORTP_PUBLIC void rtp_session_set_recv_profile(RtpSession *session,RtpProfile *profile);
481 ORTP_PUBLIC RtpProfile *rtp_session_get_profile(RtpSession *session);
482 ORTP_PUBLIC RtpProfile *rtp_session_get_send_profile(RtpSession *session);
483 ORTP_PUBLIC RtpProfile *rtp_session_get_recv_profile(RtpSession *session);
484 ORTP_PUBLIC int rtp_session_signal_connect(RtpSession *session,const char *signal_name, RtpCallback cb, void *user_data);
485 ORTP_PUBLIC int rtp_session_signal_disconnect_by_callback(RtpSession *session,const char *signal_name, RtpCallback cb);
486 ORTP_PUBLIC void rtp_session_set_ssrc(RtpSession *session, uint32_t ssrc);
487 ORTP_PUBLIC uint32_t rtp_session_get_send_ssrc(RtpSession* session);
488 ORTP_PUBLIC uint32_t rtp_session_get_recv_ssrc(RtpSession *session);
489 ORTP_PUBLIC void rtp_session_set_seq_number(RtpSession *session, uint16_t seq);
490 ORTP_PUBLIC uint16_t rtp_session_get_seq_number(RtpSession *session);
491 ORTP_PUBLIC uint32_t rtp_session_get_rcv_ext_seq_number(RtpSession *session);
492 ORTP_PUBLIC int rtp_session_get_cum_loss(RtpSession *session);
493 ORTP_PUBLIC void rtp_session_set_duplication_ratio(RtpSession *session, float ratio);
494 
495 ORTP_PUBLIC void rtp_session_enable_jitter_buffer(RtpSession *session , bool_t enabled);
496 ORTP_PUBLIC bool_t rtp_session_jitter_buffer_enabled(const RtpSession *session);
497 ORTP_PUBLIC void rtp_session_set_jitter_buffer_params(RtpSession *session, const JBParameters *par);
498 ORTP_PUBLIC void rtp_session_get_jitter_buffer_params(RtpSession *session, JBParameters *par);
499 
500 /**
501  * Set an additional timestamps offset for outgoing stream..
502  * @param s		a rtp session freshly created.
503  * @param offset		a timestamp offset value
504  *
505 **/
506 ORTP_PUBLIC void rtp_session_set_send_ts_offset(RtpSession *s, uint32_t offset);
507 ORTP_PUBLIC uint32_t rtp_session_get_send_ts_offset(RtpSession *s);
508 
509 
510 /*deprecated jitter control functions*/
511 ORTP_PUBLIC void rtp_session_set_jitter_compensation(RtpSession *session, int milisec);
512 ORTP_PUBLIC void rtp_session_enable_adaptive_jitter_compensation(RtpSession *session, bool_t val);
513 ORTP_PUBLIC bool_t rtp_session_adaptive_jitter_compensation_enabled(RtpSession *session);
514 
515 ORTP_PUBLIC void rtp_session_set_time_jump_limit(RtpSession *session, int miliseconds);
516 ORTP_PUBLIC int rtp_session_join_multicast_group(RtpSession *session, const char *ip);
517 ORTP_PUBLIC int rtp_session_set_local_addr(RtpSession *session,const char *addr, int rtp_port, int rtcp_port);
518 ORTP_PUBLIC int rtp_session_get_local_port(const RtpSession *session);
519 ORTP_PUBLIC int rtp_session_get_local_rtcp_port(const RtpSession *session);
520 
521 ORTP_PUBLIC int
522 rtp_session_set_remote_addr_full (RtpSession * session, const char * rtp_addr, int rtp_port, const char * rtcp_addr, int rtcp_port);
523 /*same as previous function, old name:*/
524 ORTP_PUBLIC int rtp_session_set_remote_addr_and_port (RtpSession * session, const char * addr, int rtp_port, int rtcp_port);
525 ORTP_PUBLIC int rtp_session_set_remote_addr(RtpSession *session,const char *addr, int port);
526 ORTP_PUBLIC int rtp_session_add_aux_remote_addr_full(RtpSession * session, const char * rtp_addr, int rtp_port, const char * rtcp_addr, int rtcp_port);
527 ORTP_PUBLIC void rtp_session_clear_aux_remote_addr(RtpSession * session);
528 /* alternatively to the set_remote_addr() and set_local_addr(), an application can give
529 a valid socket (potentially connect()ed )to be used by the RtpSession */
530 ORTP_PUBLIC void rtp_session_set_sockets(RtpSession *session, int rtpfd, int rtcpfd);
531 
532 ORTP_PUBLIC void rtp_session_get_transports(const RtpSession *session, RtpTransport **rtptr, RtpTransport **rtcptr);
533 /*those methods are provided for people who wants to send non-RTP messages using the RTP/RTCP sockets */
534 ORTP_PUBLIC ortp_socket_t rtp_session_get_rtp_socket(const RtpSession *session);
535 ORTP_PUBLIC ortp_socket_t rtp_session_get_rtcp_socket(const RtpSession *session);
536 ORTP_PUBLIC void rtp_session_refresh_sockets(RtpSession *session);
537 
538 
539 /* QOS / DSCP */
540 ORTP_PUBLIC int rtp_session_set_dscp(RtpSession *session, int dscp);
541 ORTP_PUBLIC int rtp_session_get_dscp(const RtpSession *session);
542 
543 
544 /* Packet info */
545 ORTP_PUBLIC int rtp_session_set_pktinfo(RtpSession *session, int activate);
546 
547 /* Multicast methods */
548 ORTP_PUBLIC int rtp_session_set_multicast_ttl(RtpSession *session, int ttl);
549 ORTP_PUBLIC int rtp_session_get_multicast_ttl(RtpSession *session);
550 
551 ORTP_PUBLIC int rtp_session_set_multicast_loopback(RtpSession *session, int yesno);
552 ORTP_PUBLIC int rtp_session_get_multicast_loopback(RtpSession *session);
553 
554 
555 
556 ORTP_PUBLIC int rtp_session_set_send_payload_type(RtpSession *session, int paytype);
557 ORTP_PUBLIC int rtp_session_get_send_payload_type(const RtpSession *session);
558 
559 ORTP_PUBLIC int rtp_session_get_recv_payload_type(const RtpSession *session);
560 ORTP_PUBLIC int rtp_session_set_recv_payload_type(RtpSession *session, int pt);
561 
562 ORTP_PUBLIC int rtp_session_set_send_telephone_event_payload_type(RtpSession *session, int paytype);
563 
564 ORTP_PUBLIC int rtp_session_set_payload_type(RtpSession *session, int pt);
565 
566 ORTP_PUBLIC void rtp_session_set_symmetric_rtp (RtpSession * session, bool_t yesno);
567 
568 ORTP_PUBLIC bool_t rtp_session_get_symmetric_rtp (const RtpSession * session);
569 
570 ORTP_PUBLIC void rtp_session_enable_rtcp_mux(RtpSession *session, bool_t yesno);
571 
572 ORTP_PUBLIC bool_t rtp_session_rtcp_mux_enabled(RtpSession *session);
573 
574 ORTP_PUBLIC void rtp_session_set_connected_mode(RtpSession *session, bool_t yesno);
575 
576 ORTP_PUBLIC void rtp_session_enable_rtcp(RtpSession *session, bool_t yesno);
577 /*
578  * rtcp status
579  * @return TRUE if rtcp is enabled for this session
580  */
581 ORTP_PUBLIC bool_t rtp_session_rtcp_enabled(const RtpSession *session);
582 
583 ORTP_PUBLIC void rtp_session_set_rtcp_report_interval(RtpSession *session, int value_ms);
584 
585 /**
586  * Define the bandwidth available for RTCP streams based on the upload bandwidth
587  * targeted by the application (in bits/s). RTCP streams would not take more than
588  * a few percents of the limit bandwidth (around 5%).
589  *
590  * @param session a rtp session
591  * @param target_bandwidth bandwidth limit in bits/s
592  */
593 ORTP_PUBLIC void rtp_session_set_target_upload_bandwidth(RtpSession *session, int target_bandwidth);
594 ORTP_PUBLIC int rtp_session_get_target_upload_bandwidth(RtpSession *session);
595 
596 ORTP_PUBLIC void rtp_session_configure_rtcp_xr(RtpSession *session, const OrtpRtcpXrConfiguration *config);
597 ORTP_PUBLIC void rtp_session_set_rtcp_xr_media_callbacks(RtpSession *session, const OrtpRtcpXrMediaCallbacks *cbs);
598 
599 ORTP_PUBLIC void rtp_session_set_ssrc_changed_threshold(RtpSession *session, int numpackets);
600 
601 /*low level recv and send functions */
602 ORTP_PUBLIC mblk_t * rtp_session_recvm_with_ts (RtpSession * session, uint32_t user_ts);
603 ORTP_PUBLIC mblk_t * rtp_session_create_packet(RtpSession *session, size_t header_size, const uint8_t *payload, size_t payload_size);
604 ORTP_PUBLIC mblk_t * rtp_session_create_packet_raw(const uint8_t *packet, size_t packet_size);
605 ORTP_PUBLIC mblk_t * rtp_session_create_packet_with_data(RtpSession *session, uint8_t *payload, size_t payload_size, void (*freefn)(void*));
606 ORTP_PUBLIC mblk_t * rtp_session_create_packet_in_place(RtpSession *session,uint8_t *buffer, size_t size, void (*freefn)(void*) );
607 ORTP_PUBLIC int rtp_session_sendm_with_ts (RtpSession * session, mblk_t *mp, uint32_t userts);
608 ORTP_PUBLIC int rtp_session_sendto(RtpSession *session, bool_t is_rtp, mblk_t *m, int flags, const struct sockaddr *destaddr, socklen_t destlen);
609 ORTP_PUBLIC int rtp_session_recvfrom(RtpSession *session, bool_t is_rtp, mblk_t *m, int flags, struct sockaddr *from, socklen_t *fromlen);
610 /* high level recv and send functions */
611 ORTP_PUBLIC int rtp_session_recv_with_ts(RtpSession *session, uint8_t *buffer, int len, uint32_t ts, int *have_more);
612 ORTP_PUBLIC int rtp_session_send_with_ts(RtpSession *session, const uint8_t *buffer, int len, uint32_t userts);
613 
614 /* event API*/
615 ORTP_PUBLIC void rtp_session_register_event_queue(RtpSession *session, OrtpEvQueue *q);
616 ORTP_PUBLIC void rtp_session_unregister_event_queue(RtpSession *session, OrtpEvQueue *q);
617 
618 
619 /* IP bandwidth usage estimation functions, returning bits/s*/
620 ORTP_PUBLIC float rtp_session_compute_send_bandwidth(RtpSession *session);
621 ORTP_PUBLIC float rtp_session_compute_recv_bandwidth(RtpSession *session);
622 ORTP_PUBLIC float rtp_session_get_send_bandwidth(RtpSession *session);
623 ORTP_PUBLIC float rtp_session_get_recv_bandwidth(RtpSession *session);
624 ORTP_PUBLIC float rtp_session_get_rtp_send_bandwidth(RtpSession *session);
625 ORTP_PUBLIC float rtp_session_get_rtp_recv_bandwidth(RtpSession *session);
626 ORTP_PUBLIC float rtp_session_get_rtcp_send_bandwidth(RtpSession *session);
627 ORTP_PUBLIC float rtp_session_get_rtcp_recv_bandwidth(RtpSession *session);
628 
629 ORTP_PUBLIC float rtp_session_get_send_bandwidth_smooth(RtpSession *session);
630 ORTP_PUBLIC float rtp_session_get_recv_bandwidth_smooth(RtpSession *session);
631 
632 ORTP_PUBLIC void rtp_session_send_rtcp_APP(RtpSession *session, uint8_t subtype, const char *name, const uint8_t *data, int datalen);
633 /**
634  *	Send the rtcp datagram \a packet to the destination set by rtp_session_set_remote_addr()
635  *  The packet (\a packet) is freed once it is sent.
636  *
637  * @param session a rtp session.
638  * @param m a rtcp packet presented as a mblk_t.
639  * @return the number of bytes sent over the network.
640  **/
641 
642 ORTP_PUBLIC	int rtp_session_rtcp_sendm_raw(RtpSession * session, mblk_t * m);
643 
644 
645 ORTP_PUBLIC uint32_t rtp_session_get_current_send_ts(RtpSession *session);
646 ORTP_PUBLIC uint32_t rtp_session_get_current_recv_ts(RtpSession *session);
647 ORTP_PUBLIC void rtp_session_flush_sockets(RtpSession *session);
648 ORTP_PUBLIC void rtp_session_release_sockets(RtpSession *session);
649 ORTP_PUBLIC void rtp_session_resync(RtpSession *session);
650 ORTP_PUBLIC void rtp_session_reset(RtpSession *session);
651 ORTP_PUBLIC void rtp_session_destroy(RtpSession *session);
652 
653 ORTP_PUBLIC const rtp_stats_t * rtp_session_get_stats(const RtpSession *session);
654 ORTP_PUBLIC const jitter_stats_t * rtp_session_get_jitter_stats( const RtpSession *session );
655 ORTP_PUBLIC void rtp_session_reset_stats(RtpSession *session);
656 
657 ORTP_PUBLIC void rtp_session_set_data(RtpSession *session, void *data);
658 ORTP_PUBLIC void *rtp_session_get_data(const RtpSession *session);
659 
660 ORTP_PUBLIC void rtp_session_set_recv_buf_size(RtpSession *session, int bufsize);
661 ORTP_PUBLIC void rtp_session_set_rtp_socket_send_buffer_size(RtpSession * session, unsigned int size);
662 ORTP_PUBLIC void rtp_session_set_rtp_socket_recv_buffer_size(RtpSession * session, unsigned int size);
663 
664 /* in use with the scheduler to convert a timestamp in scheduler time unit (ms) */
665 ORTP_PUBLIC uint32_t rtp_session_ts_to_time(RtpSession *session,uint32_t timestamp);
666 ORTP_PUBLIC uint32_t rtp_session_time_to_ts(RtpSession *session, int millisecs);
667 /* this function aims at simulating senders with "imprecise" clocks, resulting in
668 rtp packets sent with timestamp uncorrelated with the system clock .
669 This is only availlable to sessions working with the oRTP scheduler */
670 ORTP_PUBLIC void rtp_session_make_time_distorsion(RtpSession *session, int milisec);
671 
672 /*RTCP functions */
673 ORTP_PUBLIC void rtp_session_set_source_description(RtpSession *session, const char *cname,
674 	const char *name, const char *email, const char *phone,
675     const char *loc, const char *tool, const char *note);
676 ORTP_PUBLIC void rtp_session_add_contributing_source(RtpSession *session, uint32_t csrc,
677     const char *cname, const char *name, const char *email, const char *phone,
678     const char *loc, const char *tool, const char *note);
679 /* DEPRECATED: Use rtp_session_remove_contributing_source instead of rtp_session_remove_contributing_sources */
680 #define rtp_session_remove_contributing_sources rtp_session_remove_contributing_source
681 ORTP_PUBLIC void rtp_session_remove_contributing_source(RtpSession *session, uint32_t csrc);
682 ORTP_PUBLIC mblk_t* rtp_session_create_rtcp_sdes_packet(RtpSession *session, bool_t full);
683 
684 ORTP_PUBLIC void rtp_session_get_last_recv_time(RtpSession *session, struct timeval *tv);
685 ORTP_PUBLIC int rtp_session_bye(RtpSession *session, const char *reason);
686 
687 ORTP_PUBLIC int rtp_session_get_last_send_error_code(RtpSession *session);
688 ORTP_PUBLIC void rtp_session_clear_send_error_code(RtpSession *session);
689 ORTP_PUBLIC int rtp_session_get_last_recv_error_code(RtpSession *session);
690 ORTP_PUBLIC void rtp_session_clear_recv_error_code(RtpSession *session);
691 
692 
693 ORTP_PUBLIC float rtp_session_get_round_trip_propagation(RtpSession *session);
694 
695 
696 ORTP_PUBLIC void rtp_session_enable_network_simulation(RtpSession *session, const OrtpNetworkSimulatorParams *params);
697 ORTP_PUBLIC void rtp_session_enable_congestion_detection(RtpSession *session, bool_t enabled);
698 ORTP_PUBLIC void rtp_session_enable_video_bandwidth_estimator(RtpSession *session, const OrtpVideoBandwidthEstimatorParams *params);
699 
700 ORTP_PUBLIC void rtp_session_rtcp_set_lost_packet_value( RtpSession *session, const int value );
701 ORTP_PUBLIC void rtp_session_rtcp_set_jitter_value(RtpSession *session, const unsigned int value );
702 ORTP_PUBLIC void rtp_session_rtcp_set_delay_value(RtpSession *session, const unsigned int value );
703 ORTP_PUBLIC mblk_t * rtp_session_pick_with_cseq (RtpSession * session, const uint16_t sequence_number);
704 
705 
706 ORTP_PUBLIC void rtp_session_send_rtcp_xr_rcvr_rtt(RtpSession *session);
707 ORTP_PUBLIC void rtp_session_send_rtcp_xr_dlrr(RtpSession *session);
708 ORTP_PUBLIC void rtp_session_send_rtcp_xr_stat_summary(RtpSession *session);
709 ORTP_PUBLIC void rtp_session_send_rtcp_xr_voip_metrics(RtpSession *session);
710 
711 
712 ORTP_PUBLIC bool_t rtp_session_avpf_enabled(RtpSession *session);
713 ORTP_PUBLIC bool_t rtp_session_avpf_payload_type_feature_enabled(RtpSession *session, unsigned char feature);
714 ORTP_PUBLIC bool_t rtp_session_avpf_feature_enabled(RtpSession *session, unsigned char feature);
715 ORTP_PUBLIC void rtp_session_enable_avpf_feature(RtpSession *session, unsigned char feature, bool_t enable);
716 ORTP_PUBLIC uint16_t rtp_session_get_avpf_rr_interval(RtpSession *session);
717 ORTP_PUBLIC bool_t rtp_session_rtcp_psfb_scheduled(RtpSession *session, rtcp_psfb_type_t type);
718 ORTP_PUBLIC bool_t rtp_session_rtcp_rtpfb_scheduled(RtpSession *session, rtcp_rtpfb_type_t type);
719 ORTP_PUBLIC void rtp_session_send_rtcp_fb_generic_nack(RtpSession *session, uint16_t pid, uint16_t blp);
720 ORTP_PUBLIC void rtp_session_send_rtcp_fb_pli(RtpSession *session);
721 ORTP_PUBLIC void rtp_session_send_rtcp_fb_fir(RtpSession *session);
722 ORTP_PUBLIC void rtp_session_send_rtcp_fb_sli(RtpSession *session, uint16_t first, uint16_t number, uint8_t picture_id);
723 ORTP_PUBLIC void rtp_session_send_rtcp_fb_rpsi(RtpSession *session, uint8_t *bit_string, uint16_t bit_string_len);
724 ORTP_PUBLIC void rtp_session_send_rtcp_fb_tmmbr(RtpSession *session, uint64_t mxtbr);
725 ORTP_PUBLIC void rtp_session_send_rtcp_fb_tmmbn(RtpSession *session, uint32_t ssrc);
726 
727 
728 /*private */
729 ORTP_PUBLIC void rtp_session_init(RtpSession *session, int mode);
730 #define rtp_session_set_flag(session,flag) (session)->flags|=(flag)
731 #define rtp_session_unset_flag(session,flag) (session)->flags&=~(flag)
732 ORTP_PUBLIC void rtp_session_uninit(RtpSession *session);
733 ORTP_PUBLIC void rtp_session_dispatch_event(RtpSession *session, OrtpEvent *ev);
734 
735 
736 ORTP_PUBLIC void rtp_session_set_reuseaddr(RtpSession *session, bool_t yes);
737 
738 ORTP_PUBLIC int meta_rtp_transport_modifier_inject_packet_to_send(RtpTransport *t, RtpTransportModifier *tpm, mblk_t *msg, int flags);
739 ORTP_PUBLIC int meta_rtp_transport_modifier_inject_packet_to_send_to(RtpTransport *t, RtpTransportModifier *tpm, mblk_t *msg, int flags, const struct sockaddr *to, socklen_t tolen);
740 ORTP_PUBLIC int meta_rtp_transport_modifier_inject_packet_to_recv(RtpTransport *t, RtpTransportModifier *tpm, mblk_t *msg, int flags);
741 
742 /**
743  * get endpoint if any
744  * @param[in] transport RtpTransport object.
745  * @return #_RtpTransport
746  *
747  * */
748 ORTP_PUBLIC RtpTransport* meta_rtp_transport_get_endpoint(const RtpTransport *transport);
749 /**
750  * set endpoint
751  * @param[in] transport RtpTransport object.
752  * @param[in] endpoint RtpEndpoint.
753  *
754  * */
755 ORTP_PUBLIC void meta_rtp_transport_set_endpoint(RtpTransport *transport,RtpTransport *endpoint);
756 
757 ORTP_PUBLIC void meta_rtp_transport_destroy(RtpTransport *tp);
758 ORTP_PUBLIC void meta_rtp_transport_append_modifier(RtpTransport *tp,RtpTransportModifier *tpm);
759 
760 ORTP_PUBLIC int rtp_session_splice(RtpSession *session, RtpSession *to_session);
761 ORTP_PUBLIC int rtp_session_unsplice(RtpSession *session, RtpSession *to_session);
762 
763 ORTP_PUBLIC bool_t ortp_stream_is_ipv6(OrtpStream *os);
764 
765 #ifdef __cplusplus
766 }
767 #endif
768 
769 #endif
770