1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * Marcel Barbulescu <marcelbarbulescu@gmail.com>
28  * Seven Du <dujinfang@gmail.com>
29  *
30  * switch_rtp.c -- RTP
31  *
32  */
33 #include <switch.h>
34 #ifndef _MSC_VER
35 #include <switch_private.h>
36 #endif
37 #include <switch_stun.h>
38 #include <apr_network_io.h>
39 #undef PACKAGE_NAME
40 #undef PACKAGE_STRING
41 #undef PACKAGE_TARNAME
42 #undef PACKAGE_VERSION
43 #undef PACKAGE_BUGREPORT
44 #undef VERSION
45 #undef PACKAGE
46 #undef inline
47 #include <srtp.h>
48 #include <srtp_priv.h>
49 #include <switch_ssl.h>
50 #include <switch_jitterbuffer.h>
51 
52 //#define DEBUG_TS_ROLLOVER
53 //#define TS_ROLLOVER_START 4294951295
54 
55 //#define DEBUG_2833
56 //#define RTP_DEBUG_WRITE_DELTA
57 //#define DEBUG_MISSED_SEQ
58 //#define DEBUG_EXTRA
59 //#define DEBUG_RTCP
60 #define DEBUG_ESTIMATORS_
61 
62 
63 #define JITTER_LEAD_FRAMES 10
64 #define READ_INC(rtp_session) switch_mutex_lock(rtp_session->read_mutex); rtp_session->reading++
65 #define READ_DEC(rtp_session)  switch_mutex_unlock(rtp_session->read_mutex); rtp_session->reading--
66 #define WRITE_INC(rtp_session)  switch_mutex_lock(rtp_session->write_mutex); rtp_session->writing++
67 #define WRITE_DEC(rtp_session) switch_mutex_unlock(rtp_session->write_mutex); rtp_session->writing--
68 
69 #define RTP_STUN_FREQ 1000000
70 #define rtp_header_len 12
71 #define RTP_START_PORT 16384
72 #define RTP_END_PORT 32768
73 #define MASTER_KEY_LEN   30
74 #define RTP_MAGIC_NUMBER 42
75 #define WARN_SRTP_ERRS 10
76 #define MAX_SRTP_ERRS 100
77 #define NTP_TIME_OFFSET 2208988800UL
78 #define ZRTP_MAGIC_COOKIE 0x5a525450
79 static const switch_payload_t INVALID_PT = 255;
80 
81 #define DTMF_SANITY (rtp_session->one_second * 30)
82 
83 #define rtp_session_name(_rtp_session) _rtp_session->session ? switch_core_session_get_name(_rtp_session->session) : "-"
84 
85 #define STUN_USERNAME_MAX_SIZE 513 /* From RFC5389:  "It MUST contain a UTF-8 [RFC3629] encoded sequence of less than 513 bytes" */
86 #define SDP_UFRAG_MAX_SIZE 256 	/* From draft-ietf-mmusic-ice-sip-sdp-24: "the ice-ufrag attribute MUST NOT be longer than 32
87 								 * characters when sending, but an implementation MUST accept up to 256
88 								 * characters when receiving." */
89 
90 static switch_port_t START_PORT = RTP_START_PORT;
91 static switch_port_t END_PORT = RTP_END_PORT;
92 static switch_mutex_t *port_lock = NULL;
93 static switch_size_t do_flush(switch_rtp_t *rtp_session, int force, switch_size_t bytes_in);
94 
95 typedef srtp_hdr_t rtp_hdr_t;
96 
97 #ifdef ENABLE_ZRTP
98 #include "zrtp.h"
99 static zrtp_global_t *zrtp_global;
100 #ifndef WIN32
101 static zrtp_zid_t zid = { "FreeSWITCH01" };
102 #else
103 static zrtp_zid_t zid = { "FreeSWITCH0" };
104 #endif
105 static int zrtp_on = 0;
106 #define ZRTP_MITM_TRIES 100
107 #endif
108 
109 #ifdef _MSC_VER
110 #pragma pack(4)
111 #endif
112 
113 #ifdef _MSC_VER
114 #pragma pack()
115 #define ENABLE_SRTP
116 #endif
117 
118 #ifdef HAVE_OPENSSL
119 #include <openssl/ssl.h>
120 #include <openssl/err.h>
121 #include <openssl/rand.h>
122 #endif
123 
124 static switch_hash_t *alloc_hash = NULL;
125 
126 typedef struct {
127 	srtp_hdr_t header;
128 	char body[SWITCH_RTP_MAX_BUF_LEN+4+sizeof(char *)];
129 	switch_rtp_hdr_ext_t *ext;
130 	char *ebody;
131 } rtp_msg_t;
132 
133 #define RTP_BODY(_s) (char *) (_s->recv_msg.ebody ? _s->recv_msg.ebody : _s->recv_msg.body)
134 
135 typedef struct {
136 	uint32_t ssrc;
137 	uint8_t seq;
138 	uint8_t r1;
139 	uint8_t r2;
140 	uint8_t r3;
141 } rtcp_fir_t;
142 
143 #ifdef _MSC_VER
144 #pragma pack(push, r1, 1)
145 #endif
146 
147 typedef struct switch_rtcp_sdes_unit_s {
148 	unsigned char type;
149 	unsigned char length;
150 	char value[];
151 } switch_rtcp_sdes_unit_t;
152 
153 typedef struct {
154 	uint32_t ssrc;
155 	uint8_t parts[4];
156 } rtcp_tmmbx_t;
157 
158 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
159 
160 typedef struct {
161 	unsigned version:2;
162 	unsigned p:1;
163 	unsigned fmt:5;
164 	unsigned pt:8;
165 	unsigned length:16;
166 	uint32_t send_ssrc;
167 	uint32_t recv_ssrc;
168 } switch_rtcp_ext_hdr_t;
169 
170 #else /*  BIG_ENDIAN */
171 
172 typedef struct {
173 	unsigned fmt:5;
174 	unsigned p:1;
175 	unsigned version:2;
176 	unsigned pt:8;
177 	unsigned length:16;
178 	uint32_t send_ssrc;
179 	uint32_t recv_ssrc;
180 } switch_rtcp_ext_hdr_t;
181 
182 #endif
183 
184 #ifdef _MSC_VER
185 #pragma pack(pop, r1)
186 #endif
187 
188 #define KALMAN_SYSTEM_MODELS 3 /*loss, jitter, rtt*/
189 #define EST_LOSS 0
190 #define EST_JITTER 1
191 #define EST_RTT 2
192 
193 typedef struct {
194 	switch_rtcp_ext_hdr_t header;
195 	char body[SWITCH_RTCP_MAX_BUF_LEN];
196 } rtcp_ext_msg_t;
197 
198 typedef struct {
199 	switch_rtcp_hdr_t header;
200 	char body[SWITCH_RTCP_MAX_BUF_LEN];
201 } rtcp_msg_t;
202 
203 
204 typedef enum {
205 	VAD_FIRE_TALK = (1 << 0),
206 	VAD_FIRE_NOT_TALK = (1 << 1)
207 } vad_talk_mask_t;
208 
209 struct switch_rtp_vad_data {
210 	switch_core_session_t *session;
211 	switch_codec_t vad_codec;
212 	switch_codec_t *read_codec;
213 	uint32_t bg_level;
214 	uint32_t bg_count;
215 	uint32_t bg_len;
216 	uint32_t diff_level;
217 	uint8_t hangunder;
218 	uint8_t hangunder_hits;
219 	uint8_t hangover;
220 	uint8_t hangover_hits;
221 	uint8_t cng_freq;
222 	uint8_t cng_count;
223 	switch_vad_flag_t flags;
224 	uint32_t ts;
225 	uint8_t start;
226 	uint8_t start_count;
227 	uint8_t scan_freq;
228 	time_t next_scan;
229 	switch_time_t start_talking;
230 	switch_time_t stop_talking;
231 	switch_time_t total_talk_time;
232 	int fire_events;
233 };
234 
235 struct switch_rtp_rfc2833_data {
236 	switch_queue_t *dtmf_queue;
237 	char out_digit;
238 	unsigned char out_digit_packet[4];
239 	unsigned int out_digit_sofar;
240 	unsigned int out_digit_sub_sofar;
241 	unsigned int out_digit_dur;
242 	uint16_t in_digit_seq;
243 	uint32_t in_digit_ts;
244 	uint32_t last_in_digit_ts;
245 	uint32_t in_digit_sanity;
246 	uint32_t in_interleaved;
247 	uint32_t timestamp_dtmf;
248 	uint16_t last_duration;
249 	uint32_t flip;
250 	char first_digit;
251 	char last_digit;
252 	switch_queue_t *dtmf_inqueue;
253 	switch_mutex_t *dtmf_mutex;
254 	uint8_t in_digit_queued;
255 };
256 
257 typedef struct {
258 	char *ice_user;
259 	char *user_ice;
260 	char *luser_ice;
261 	char *pass;
262 	char *rpass;
263 	switch_sockaddr_t *addr;
264 	uint32_t funny_stun;
265 	switch_time_t next_run;
266 	switch_core_media_ice_type_t type;
267 	ice_t *ice_params;
268 	ice_proto_t proto;
269 	uint8_t sending;
270 	uint8_t ready;
271 	uint8_t rready;
272 	int missed_count;
273 	char last_sent_id[13];
274 	switch_time_t last_ok;
275 } switch_rtp_ice_t;
276 
277 struct switch_rtp;
278 
279 #define MAX_DTLS_MTU 4096
280 
281 typedef struct switch_dtls_s {
282 	/* DTLS */
283 	SSL_CTX *ssl_ctx;
284 	SSL *ssl;
285 	BIO *read_bio;
286 	BIO *write_bio;
287 	BIO *filter_bio;
288 	dtls_fingerprint_t *local_fp;
289 	dtls_fingerprint_t *remote_fp;
290 	dtls_state_t state;
291 	dtls_state_t last_state;
292 	uint8_t new_state;
293 	dtls_type_t type;
294 	switch_size_t bytes;
295 	void *data;
296 	switch_socket_t *sock_output;
297 	switch_sockaddr_t *remote_addr;
298 	char *rsa;
299 	char *pvt;
300 	char *ca;
301 	char *pem;
302 	struct switch_rtp *rtp_session;
303 	int mtu;
304 } switch_dtls_t;
305 
306 typedef int (*dtls_state_handler_t)(switch_rtp_t *, switch_dtls_t *);
307 
308 
309 static int dtls_state_handshake(switch_rtp_t *rtp_session, switch_dtls_t *dtls);
310 static int dtls_state_ready(switch_rtp_t *rtp_session, switch_dtls_t *dtls);
311 static int dtls_state_setup(switch_rtp_t *rtp_session, switch_dtls_t *dtls);
312 static int dtls_state_fail(switch_rtp_t *rtp_session, switch_dtls_t *dtls);
313 
314 dtls_state_handler_t dtls_states[DS_INVALID] = {NULL, dtls_state_handshake, dtls_state_setup, dtls_state_ready, dtls_state_fail};
315 
316 typedef struct ts_normalize_s {
317 	uint32_t last_ssrc;
318 	uint32_t last_frame;
319 	uint32_t ts;
320 	uint32_t delta;
321 	uint32_t delta_ttl;
322 	int last_external;
323 } ts_normalize_t;
324 
325 struct switch_rtp {
326 	/*
327 	 * Two sockets are needed because we might be transcoding protocol families
328 	 * (e.g. receive over IPv4 and send over IPv6). In case the protocol
329 	 * families are equal, sock_input == sock_output and only one socket is
330 	 * used.
331 	 */
332 	switch_socket_t *sock_input, *sock_output, *rtcp_sock_input, *rtcp_sock_output;
333 	switch_pollfd_t *read_pollfd, *rtcp_read_pollfd;
334 	switch_pollfd_t *jb_pollfd;
335 
336 	switch_sockaddr_t *local_addr, *rtcp_local_addr;
337 	rtp_msg_t send_msg;
338 	rtcp_msg_t rtcp_send_msg;
339 	switch_rtcp_frame_t rtcp_frame;
340 
341 	uint8_t send_rr;
342 	uint8_t fir_seq;
343 	uint16_t fir_count;
344 	uint16_t pli_count;
345 	uint32_t cur_tmmbr;
346 	uint32_t tmmbr;
347 	uint32_t tmmbn;
348 
349 	ts_normalize_t ts_norm;
350 	switch_sockaddr_t *remote_addr, *rtcp_remote_addr;
351 	rtp_msg_t recv_msg;
352 	rtcp_msg_t rtcp_recv_msg;
353 	rtcp_msg_t *rtcp_recv_msg_p;
354 
355 	uint32_t autoadj_window;
356 	uint32_t autoadj_threshold;
357 	uint32_t autoadj_tally;
358 
359 	uint32_t rtcp_autoadj_window;
360 	uint32_t rtcp_autoadj_threshold;
361 	uint32_t rtcp_autoadj_tally;
362 
363 	srtp_ctx_t *send_ctx[2];
364 	srtp_ctx_t *recv_ctx[2];
365 
366 	srtp_policy_t send_policy[2];
367 	srtp_policy_t recv_policy[2];
368 
369 	uint32_t srtp_errs[2];
370 	uint32_t srctp_errs[2];
371 
372 
373 	int srtp_idx_rtp;
374 	int srtp_idx_rtcp;
375 
376 	switch_dtls_t *dtls;
377 	switch_dtls_t *rtcp_dtls;
378 
379 	rtp_hdr_t last_rtp_hdr;
380 
381 	uint16_t seq;
382 	uint32_t ssrc;
383 	uint32_t remote_ssrc;
384 	uint32_t last_jb_read_ssrc;
385 	int8_t sending_dtmf;
386 	uint8_t need_mark;
387 	switch_payload_t payload;
388 	switch_rtp_invalid_handler_t invalid_handler;
389 	void *private_data;
390 	uint32_t ts;
391 	//uint32_t last_clock_ts;
392 	uint32_t last_write_ts;
393 	uint32_t last_read_ts;
394 	uint32_t last_cng_ts;
395 	uint32_t last_write_samplecount;
396 	uint32_t delay_samples;
397 	uint32_t next_write_samplecount;
398 	uint32_t max_next_write_samplecount;
399 	uint32_t queue_delay;
400 	switch_time_t last_write_timestamp;
401 	uint32_t flags[SWITCH_RTP_FLAG_INVALID];
402 	switch_memory_pool_t *pool;
403 	switch_sockaddr_t *from_addr, *rtp_from_addr, *rtcp_from_addr, *bundle_internal_addr, *bundle_external_addr;
404 	char *rx_host;
405 	switch_port_t rx_port;
406 	switch_rtp_ice_t ice;
407 	switch_rtp_ice_t rtcp_ice;
408 	char *timer_name;
409 	char *local_host_str;
410 	char *remote_host_str;
411 	char *eff_remote_host_str;
412 	switch_time_t first_stun;
413 	switch_time_t last_stun;
414 	uint32_t wrong_addrs;
415 	uint32_t samples_per_interval;
416 	uint32_t samples_per_second;
417 	uint32_t conf_samples_per_interval;
418 	switch_time_t rtcp_last_sent;
419 	uint32_t rsamples_per_interval;
420 	uint32_t ms_per_packet;
421 	uint32_t one_second;
422 	uint32_t consecutive_flaws;
423 	uint32_t jitter_lead;
424 	double old_mean;
425 	switch_time_t next_stat_check_time;
426 	switch_port_t local_port;
427 	switch_port_t remote_port;
428 	switch_port_t eff_remote_port;
429 	switch_port_t remote_rtcp_port;
430 
431 	struct switch_rtp_vad_data vad_data;
432 	struct switch_rtp_rfc2833_data dtmf_data;
433 	switch_payload_t te;
434 	switch_payload_t recv_te;
435 	switch_payload_t cng_pt;
436 	switch_mutex_t *flag_mutex;
437 	switch_mutex_t *read_mutex;
438 	switch_mutex_t *write_mutex;
439 	switch_mutex_t *ice_mutex;
440 	switch_timer_t timer;
441 	switch_timer_t write_timer;
442 	uint8_t ready;
443 	uint8_t cn;
444 	switch_jb_t *jb;
445 	switch_jb_t *vb;
446 	switch_jb_t *vbw;
447 	uint32_t max_missed_packets;
448 	uint32_t missed_count;
449 	switch_time_t last_media;
450 	uint32_t media_timeout;
451 	rtp_msg_t write_msg;
452 	switch_rtp_crypto_key_t *crypto_keys[SWITCH_RTP_CRYPTO_MAX];
453 	int reading;
454 	int writing;
455 	char *stun_ip;
456 	switch_port_t stun_port;
457 	int from_auto;
458 	uint32_t cng_count;
459 	switch_rtp_bug_flag_t rtp_bugs;
460 	switch_rtp_stats_t stats;
461 	switch_rtcp_video_stats_t rtcp_vstats;
462 	uint32_t clean_stream;
463 	uint32_t bad_stream;
464 	uint32_t recovering_stream;
465 
466 	uint32_t hot_hits;
467 	uint32_t sync_packets;
468 	int rtcp_interval;
469 	int rtcp_sent_packets;
470 	switch_bool_t rtcp_fresh_frame;
471 
472 	switch_time_t send_time;
473 	switch_byte_t auto_adj_used;
474 	switch_byte_t rtcp_auto_adj_used;
475 	uint8_t pause_jb;
476 	uint16_t last_seq;
477 	uint16_t last_write_seq;
478 	uint8_t video_delta_mode;
479 	switch_time_t last_read_time;
480 	switch_size_t last_flush_packet_count;
481 	uint32_t interdigit_delay;
482 	switch_core_session_t *session;
483 	payload_map_t **pmaps;
484 	payload_map_t *pmap_tail;
485 	kalman_estimator_t *estimators[KALMAN_SYSTEM_MODELS];
486 	cusum_kalman_detector_t *detectors[KALMAN_SYSTEM_MODELS];
487 	int ice_adj;
488 	uint8_t has_rtp;
489 	uint8_t has_rtcp;
490 	uint8_t has_ice;
491 	uint8_t punts;
492 	uint8_t clean;
493 	uint32_t last_max_vb_frames;
494 	int skip_timer;
495 	uint32_t prev_nacks_inflight;
496 #ifdef ENABLE_ZRTP
497 	zrtp_session_t *zrtp_session;
498 	zrtp_profile_t *zrtp_profile;
499 	zrtp_stream_t *zrtp_stream;
500 	int zrtp_mitm_tries;
501 	int zinit;
502 #endif
503 
504 };
505 
506 struct switch_rtcp_report_block {
507 	uint32_t ssrc; /* The SSRC identifier of the source to which the information in this reception report block pertains. */
508 	unsigned int fraction :8; /* The fraction of RTP data packets from source SSRC_n lost since the previous SR or RR packet was sent */
509 	int lost :24; /* The total number of RTP data packets from source SSRC_n that have been lost since the beginning of reception */
510 	uint32_t highest_sequence_number_received;
511 	uint32_t jitter; /* An estimate of the statistical variance of the RTP data packet interarrival time, measured in timestamp units and expressed as an unsigned integer. */
512 	uint32_t lsr; /* The middle 32 bits out of 64 in the NTP timestamp */
513 	uint32_t dlsr; /* The delay, expressed in units of 1/65536 seconds, between receiving the last SR packet from source SSRC_n and sending this reception report block */
514 };
515 
516 struct switch_rtcp_sr_head {
517 	uint32_t ssrc;
518 	uint32_t ntp_msw;
519 	uint32_t ntp_lsw;
520 	uint32_t ts;
521 	uint32_t pc;
522 	uint32_t oc;
523 };
524 
525 struct switch_rtcp_sender_info {
526 	uint32_t ntp_msw;
527 	uint32_t ntp_lsw;
528 	uint32_t ts;
529 	uint32_t pc;
530 	uint32_t oc;
531 };
532 
533 struct switch_rtcp_sender_report {
534 	uint32_t ssrc;
535 	struct switch_rtcp_sender_info sender_info;
536 	struct switch_rtcp_report_block report_block;
537 };
538 
539 struct switch_rtcp_receiver_report {
540 	uint32_t ssrc;
541 	struct switch_rtcp_report_block report_block;
542 };
543 
544 typedef enum {
545 	RESULT_CONTINUE,
546 	RESULT_GOTO_END,
547 	RESULT_GOTO_RECVFROM,
548 	RESULT_GOTO_TIMERCHECK
549 } handle_rfc2833_result_t;
550 
551 static void do_2833(switch_rtp_t *rtp_session);
552 
553 
554 #define rtp_type(rtp_session) rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ?  "text" : (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")
555 
556 
switch_rtp_change_ice_dest(switch_rtp_t * rtp_session,switch_rtp_ice_t * ice,const char * host,switch_port_t port)557 static void switch_rtp_change_ice_dest(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, const char *host, switch_port_t port)
558 {
559 	int is_rtcp = ice == &rtp_session->rtcp_ice;
560 	const char *err = "";
561 
562 	ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr = switch_core_strdup(rtp_session->pool, host);
563 	ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port = port;
564 	ice->missed_count = 0;
565 
566 	if (is_rtcp) {
567 		ice->addr = rtp_session->rtcp_remote_addr;
568 	} else {
569 		switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err);
570 
571 		if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
572 			ice->addr = rtp_session->remote_addr;
573 		}
574 	}
575 
576 }
577 
578 
579 
handle_rfc2833(switch_rtp_t * rtp_session,switch_size_t bytes,int * do_cng)580 static handle_rfc2833_result_t handle_rfc2833(switch_rtp_t *rtp_session, switch_size_t bytes, int *do_cng)
581 {
582 
583 	if (rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]) {
584 		rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]++;
585 
586 		if (rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] > DTMF_SANITY) {
587 			rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = 0;
588 		} else {
589 			rtp_session->stats.inbound.last_processed_seq = 0;
590 		}
591 	}
592 
593 
594 #ifdef DEBUG_2833
595 	if (rtp_session->dtmf_data.in_digit_sanity && !(rtp_session->dtmf_data.in_digit_sanity % 100)) {
596 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "sanity %d %ld\n", rtp_session->dtmf_data.in_digit_sanity, bytes);
597 	}
598 #endif
599 
600 	if (rtp_session->dtmf_data.in_digit_sanity && !--rtp_session->dtmf_data.in_digit_sanity) {
601 
602 		rtp_session->dtmf_data.last_digit = 0;
603 		rtp_session->dtmf_data.in_digit_ts = 0;
604 		rtp_session->dtmf_data.in_digit_queued = 0;
605 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Failed DTMF sanity check.\n");
606 	}
607 
608 	if (!bytes) return RESULT_CONTINUE;
609 
610 
611 	/* RFC2833 ... like all RFC RE: VoIP, guaranteed to drive you to insanity!
612 	   We know the real rules here, but if we enforce them, it's an interop nightmare so,
613 	   we put up with as much as we can so we don't have to deal with being punished for
614 	   doing it right. Nice guys finish last!
615 	*/
616 
617 	if (bytes > rtp_header_len && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] &&
618 		rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
619 		switch_size_t len = bytes - rtp_header_len;
620 		unsigned char *packet = (unsigned char *) RTP_BODY(rtp_session);
621 		int end;
622 		uint16_t duration;
623 		char key;
624 		uint16_t in_digit_seq;
625 		uint32_t ts;
626 
627 		rtp_session->stats.inbound.last_processed_seq = 0;
628 
629 		if (!(packet[0] || packet[1] || packet[2] || packet[3]) && len >= 8) {
630 			packet += 4;
631 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "DTMF payload offset by 4 bytes.\n");
632 		}
633 
634 		if (!(packet[0] || packet[1] || packet[2] || packet[3]) && rtp_session->dtmf_data.in_digit_ts) {
635 			switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
636 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Failed DTMF payload check.\n");
637 			rtp_session->dtmf_data.last_digit = 0;
638 			rtp_session->dtmf_data.in_digit_ts = 0;
639 			rtp_session->dtmf_data.in_digit_sanity = 0;
640 			rtp_session->dtmf_data.in_digit_queued = 0;
641 		}
642 
643 		end = packet[1] & 0x80 ? 1 : 0;
644 		duration = (packet[2] << 8) + packet[3];
645 		key = switch_rfc2833_to_char(packet[0]);
646 		in_digit_seq = ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
647 		ts = htonl(rtp_session->last_rtp_hdr.ts);
648 
649 		if (rtp_session->flags[SWITCH_RTP_FLAG_PASS_RFC2833]) {
650 
651 			if (end) {
652 				rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = DTMF_SANITY - 3;
653 			} else if (!rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]) {
654 				rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = 1;
655 			}
656 
657 			return RESULT_CONTINUE;
658 		}
659 
660 		if (in_digit_seq < rtp_session->dtmf_data.in_digit_seq) {
661 			if (rtp_session->dtmf_data.in_digit_seq - in_digit_seq > 100) {
662 				rtp_session->dtmf_data.in_digit_seq = 0;
663 			}
664 		}
665 #ifdef DEBUG_2833
666 		if (!(packet[0] || packet[1] || packet[2] || packet[3]) && len >= 8) {
667 			len -= 4;
668 		}
669 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "packet[%d]: %02x %02x %02x %02x\n", (int) len, (unsigned char) packet[0], (unsigned char) packet[1], (unsigned char) packet[2], (unsigned char) packet[3]);
670 #endif
671 
672 		if (in_digit_seq > rtp_session->dtmf_data.in_digit_seq) {
673 
674 			rtp_session->dtmf_data.in_digit_seq = in_digit_seq;
675 #ifdef DEBUG_2833
676 
677 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "read: %c %u %u %u %u %d %d %s\n",
678 							  key, in_digit_seq, rtp_session->dtmf_data.in_digit_seq,
679 				   ts, duration, rtp_session->last_rtp_hdr.m, end, end && !rtp_session->dtmf_data.in_digit_ts ? "ignored" : "");
680 #endif
681 
682 
683 			if (rtp_session->dtmf_data.in_digit_ts && rtp_session->dtmf_data.in_digit_ts != ts) {
684 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "TS changed from last packet, resetting....\n");
685 				rtp_session->dtmf_data.last_digit = 0;
686 				rtp_session->dtmf_data.in_digit_ts = 0;
687 				rtp_session->dtmf_data.in_digit_sanity = 0;
688 				rtp_session->dtmf_data.in_digit_queued = 0;
689 			}
690 
691 
692 			if (!rtp_session->dtmf_data.in_digit_queued && rtp_session->dtmf_data.in_digit_ts) {
693 				if ((rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION)) {
694 					switch_dtmf_t dtmf = { key, switch_core_min_dtmf_duration(0), 0, SWITCH_DTMF_RTP };
695 #ifdef DEBUG_2833
696 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Early Queuing digit %c:%d\n", dtmf.digit, dtmf.duration / 8);
697 #endif
698 					switch_rtp_queue_rfc2833_in(rtp_session, &dtmf);
699 					rtp_session->dtmf_data.in_digit_queued = 1;
700 				}
701 
702 				if (rtp_session->jb && (rtp_session->rtp_bugs & RTP_BUG_FLUSH_JB_ON_DTMF)) {
703 					switch_jb_reset(rtp_session->jb);
704 				}
705 
706 			}
707 
708 			/* only set sanity if we do NOT ignore the packet */
709 			if (rtp_session->dtmf_data.in_digit_ts) {
710 				rtp_session->dtmf_data.in_digit_sanity = 2000;
711 			}
712 
713 			if (rtp_session->dtmf_data.last_duration > duration &&
714 				rtp_session->dtmf_data.last_duration > 0xFC17 && ts == rtp_session->dtmf_data.in_digit_ts) {
715 				rtp_session->dtmf_data.flip++;
716 			}
717 
718 			if (end) {
719 				if (!rtp_session->dtmf_data.in_digit_ts && rtp_session->dtmf_data.last_in_digit_ts != ts) {
720 #ifdef DEBUG_2833
721 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "start with end packet %d\n", ts);
722 #endif
723 					rtp_session->dtmf_data.last_in_digit_ts = ts;
724 					rtp_session->dtmf_data.in_digit_ts = ts;
725 					rtp_session->dtmf_data.first_digit = key;
726 					rtp_session->dtmf_data.in_digit_sanity = 2000;
727 				}
728 				if (rtp_session->dtmf_data.in_digit_ts) {
729 					switch_dtmf_t dtmf = { key, duration, 0, SWITCH_DTMF_RTP };
730 
731 					if (ts > rtp_session->dtmf_data.in_digit_ts) {
732 						dtmf.duration += (ts - rtp_session->dtmf_data.in_digit_ts);
733 					}
734 					if (rtp_session->dtmf_data.flip) {
735 						dtmf.duration += rtp_session->dtmf_data.flip * 0xFFFF;
736 						rtp_session->dtmf_data.flip = 0;
737 #ifdef DEBUG_2833
738 						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "you're welcome!\n");
739 #endif
740 					}
741 #ifdef DEBUG_2833
742 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "done digit=%c ts=%u start_ts=%u dur=%u ddur=%u\n",
743 						   dtmf.digit, ts, rtp_session->dtmf_data.in_digit_ts, duration, dtmf.duration);
744 #endif
745 
746 					if (!(rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION) && !rtp_session->dtmf_data.in_digit_queued) {
747 #ifdef DEBUG_2833
748 						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Queuing digit %c:%d\n", dtmf.digit, dtmf.duration / 8);
749 #endif
750 						switch_rtp_queue_rfc2833_in(rtp_session, &dtmf);
751 					}
752 
753 					rtp_session->dtmf_data.last_digit = rtp_session->dtmf_data.first_digit;
754 
755 					rtp_session->dtmf_data.in_digit_ts = 0;
756 					rtp_session->dtmf_data.in_digit_sanity = 0;
757 					rtp_session->dtmf_data.in_digit_queued = 0;
758 					*do_cng = 1;
759 				} else {
760 					if (!switch_rtp_ready(rtp_session)) {
761 						return RESULT_GOTO_END;
762 					}
763 					switch_cond_next();
764 					return RESULT_GOTO_RECVFROM;
765 				}
766 
767 			} else if (!rtp_session->dtmf_data.in_digit_ts) {
768 #ifdef DEBUG_2833
769 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "start %d [%c]\n", ts, key);
770 #endif
771 				rtp_session->dtmf_data.in_digit_ts = ts;
772 				rtp_session->dtmf_data.last_in_digit_ts = ts;
773 				rtp_session->dtmf_data.first_digit = key;
774 				rtp_session->dtmf_data.in_digit_sanity = 2000;
775 			}
776 
777 			rtp_session->dtmf_data.last_duration = duration;
778 		} else {
779 #ifdef DEBUG_2833
780 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "drop: %c %u %u %u %u %d %d\n",
781 				   key, in_digit_seq, rtp_session->dtmf_data.in_digit_seq, ts, duration, rtp_session->last_rtp_hdr.m, end);
782 #endif
783 			switch_cond_next();
784 			return RESULT_GOTO_RECVFROM;
785 		}
786 	}
787 
788 	if (rtp_session->dtmf_data.in_digit_ts) {
789 		if (!switch_rtp_ready(rtp_session)) {
790 			return RESULT_GOTO_END;
791 		}
792 
793 		if (!rtp_session->dtmf_data.in_interleaved && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te) {
794 			/* Drat, they are sending audio still as well as DTMF ok fine..... *sigh* */
795 			rtp_session->dtmf_data.in_interleaved = 1;
796 		}
797 
798 		if (rtp_session->dtmf_data.in_interleaved || (rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION)) {
799 			if (rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
800 				return RESULT_GOTO_RECVFROM;
801 			}
802 		} else {
803 			*do_cng = 1;
804 			return RESULT_GOTO_TIMERCHECK;
805 		}
806 	}
807 
808 	return RESULT_CONTINUE;
809 }
810 
811 static int rtp_write_ready(switch_rtp_t *rtp_session, uint32_t bytes, int line);
812 static int global_init = 0;
813 static int rtp_common_write(switch_rtp_t *rtp_session,
814 							rtp_msg_t *send_msg, void *data, uint32_t datalen, switch_payload_t payload, uint32_t timestamp, switch_frame_flag_t *flags);
815 
816 
ice_out(switch_rtp_t * rtp_session,switch_rtp_ice_t * ice)817 static switch_status_t ice_out(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice)
818 {
819 	uint8_t buf[256] = { 0 };
820 	switch_stun_packet_t *packet;
821 	unsigned int elapsed;
822 	switch_size_t bytes;
823 	switch_status_t status = SWITCH_STATUS_SUCCESS;
824 	//switch_sockaddr_t *remote_addr = rtp_session->remote_addr;
825 	switch_socket_t *sock_output = rtp_session->sock_output;
826 	switch_time_t now = switch_micro_time_now();
827 
828 	if (ice->type & ICE_LITE) {
829 		// no connectivity checks for ICE-Lite
830 		return SWITCH_STATUS_BREAK;
831 	}
832 
833 	if (ice->next_run && ice->next_run > now) {
834 		return SWITCH_STATUS_BREAK;
835 	}
836 
837 	ice->next_run = now + RTP_STUN_FREQ;
838 
839 	if (ice == &rtp_session->rtcp_ice && rtp_session->rtcp_sock_output) {
840 		sock_output = rtp_session->rtcp_sock_output;
841 	}
842 
843 	if (!sock_output) {
844 		return SWITCH_STATUS_FALSE;
845 	}
846 
847 	switch_assert(rtp_session != NULL);
848 	switch_assert(ice->ice_user != NULL);
849 
850 	READ_INC(rtp_session);
851 
852 	if (rtp_session->last_stun) {
853 		elapsed = (unsigned int) ((switch_micro_time_now() - rtp_session->last_stun) / 1000);
854 
855 		if (elapsed > 30000) {
856 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "No %s stun for a long time!\n", rtp_type(rtp_session));
857 			rtp_session->last_stun = switch_micro_time_now();
858 			//status = SWITCH_STATUS_GENERR;
859 			//goto end;
860 		}
861 	}
862 
863 	packet = switch_stun_packet_build_header(SWITCH_STUN_BINDING_REQUEST, NULL, buf);
864 	switch_stun_packet_attribute_add_username(packet, ice->ice_user, (uint16_t)strlen(ice->ice_user));
865 
866 	memcpy(ice->last_sent_id, packet->header.id, 12);
867 
868 	//if (ice->pass && ice->type == ICE_GOOGLE_JINGLE) {
869 	//	switch_stun_packet_attribute_add_password(packet, ice->pass, (uint16_t)strlen(ice->pass));
870 	//}
871 
872 	if ((ice->type & ICE_VANILLA)) {
873 		char sw[128] = "";
874 
875 		switch_stun_packet_attribute_add_priority(packet, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].priority);
876 
877 		switch_snprintf(sw, sizeof(sw), "FreeSWITCH (%s)", switch_version_revision_human());
878 		switch_stun_packet_attribute_add_software(packet, sw, (uint16_t)strlen(sw));
879 
880 		if ((ice->type & ICE_CONTROLLED)) {
881 			switch_stun_packet_attribute_add_controlled(packet);
882 		} else {
883 			switch_stun_packet_attribute_add_controlling(packet);
884 			switch_stun_packet_attribute_add_use_candidate(packet);
885 		}
886 
887 		switch_stun_packet_attribute_add_integrity(packet, ice->rpass);
888 		switch_stun_packet_attribute_add_fingerprint(packet);
889 	}
890 
891 
892 	bytes = switch_stun_packet_length(packet);
893 
894 #ifdef DEBUG_EXTRA
895 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "%s send %s stun\n", rtp_session_name(rtp_session), rtp_type(rtp_session));
896 #endif
897 	switch_socket_sendto(sock_output, ice->addr, 0, (void *) packet, &bytes);
898 
899 	ice->sending = 3;
900 
901 	// end:
902 	READ_DEC(rtp_session);
903 
904 	return status;
905 }
906 
icecmp(const char * them,switch_rtp_ice_t * ice)907 int icecmp(const char *them, switch_rtp_ice_t *ice)
908 {
909 	if (strchr(them, ':')) {
910 		return strcmp(them, ice->user_ice);
911 	}
912 
913 	return strcmp(them, ice->luser_ice);
914 }
915 
handle_ice(switch_rtp_t * rtp_session,switch_rtp_ice_t * ice,void * data,switch_size_t len)916 static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
917 {
918 	switch_stun_packet_t *packet;
919 	switch_stun_packet_attribute_t *attr;
920 	void *end_buf;
921 	char username[STUN_USERNAME_MAX_SIZE] = { 0 };
922 	unsigned char buf[1500] = { 0 };
923 	switch_size_t cpylen = len;
924 	int xlen = 0;
925 	int ok = 1;
926 	uint32_t *pri = NULL;
927 	int is_rtcp = ice == &rtp_session->rtcp_ice;
928 	uint32_t elapsed;
929 	switch_time_t ref_point;
930 
931 	//if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
932 	//	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s CALL\n", rtp_type(rtp_session));
933 	//}
934 
935 	if (!switch_rtp_ready(rtp_session) || zstr(ice->user_ice) || zstr(ice->ice_user)) {
936 		return;
937 	}
938 
939 	READ_INC(rtp_session);
940 	WRITE_INC(rtp_session);
941 
942 	switch_mutex_lock(rtp_session->ice_mutex);
943 
944 	if (!switch_rtp_ready(rtp_session)) {
945 		goto end;
946 	}
947 
948 	if (cpylen > sizeof(buf)) {
949 		cpylen = sizeof(buf);
950 	}
951 
952 
953 	memcpy(buf, data, cpylen);
954 	packet = switch_stun_packet_parse(buf, (uint32_t)cpylen);
955 	if (!packet) {
956 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Invalid STUN/ICE packet received %ld bytes\n", (long)cpylen);
957 		goto end;
958 
959 	}
960 
961 	rtp_session->last_stun = switch_micro_time_now();
962 
963 	if (!rtp_session->first_stun) {
964 		rtp_session->first_stun = rtp_session->last_stun;
965 	}
966 
967 	if (ice->last_ok) {
968 		ref_point = ice->last_ok;
969 	} else {
970 		ref_point = rtp_session->first_stun;
971 	}
972 
973 	elapsed = (unsigned int) ((switch_micro_time_now() - ref_point) / 1000);
974 
975 
976 	end_buf = buf + ((sizeof(buf) > packet->header.length) ? packet->header.length : sizeof(buf));
977 
978 	switch_stun_packet_first_attribute(packet, attr);
979 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "%s STUN PACKET TYPE: %s\n",
980 					  rtp_type(rtp_session), switch_stun_value_to_name(SWITCH_STUN_TYPE_PACKET_TYPE, packet->header.type));
981 	do {
982 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|---: %s STUN ATTR %d %x %s\n", rtp_type(rtp_session), attr->type, attr->type,
983 						  switch_stun_value_to_name(SWITCH_STUN_TYPE_ATTRIBUTE, attr->type));
984 
985 		switch (attr->type) {
986 		case SWITCH_STUN_ATTR_USE_CAND:
987 			{
988 				ice->rready = 1;
989 			}
990 			break;
991 		case SWITCH_STUN_ATTR_ERROR_CODE:
992 			{
993 				switch_stun_error_code_t *err = (switch_stun_error_code_t *) attr->value;
994 				uint32_t code = (err->code * 100) + err->number;
995 
996 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s got %s stun binding response %u\n",
997 								  rtp_session_name(rtp_session),
998 								  rtp_type(rtp_session),
999 								  code
1000 								  );
1001 
1002 				if ((ice->type & ICE_VANILLA) && code == 487) {
1003 					if ((ice->type & ICE_CONTROLLED)) {
1004 						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s STUN Changing role to CONTROLLING\n", rtp_type(rtp_session));
1005 						ice->type &= ~ICE_CONTROLLED;
1006 					} else {
1007 						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s STUN Changing role to CONTROLLED\n", rtp_type(rtp_session));
1008 						ice->type |= ICE_CONTROLLED;
1009 					}
1010 					packet->header.type = SWITCH_STUN_BINDING_RESPONSE;
1011 				}
1012 
1013 			}
1014 			break;
1015 		case SWITCH_STUN_ATTR_MAPPED_ADDRESS:
1016 			{
1017 				char ip[50];
1018 				uint16_t port;
1019 				switch_stun_packet_attribute_get_mapped_address(attr, ip, sizeof(ip), &port);
1020 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s:%d\n", ip, port);
1021 			}
1022 			break;
1023 		case SWITCH_STUN_ATTR_XOR_MAPPED_ADDRESS:
1024 			{
1025 				char ip[50];
1026 				uint16_t port;
1027 				switch_stun_packet_attribute_get_xor_mapped_address(attr, &packet->header, ip, sizeof(ip), &port);
1028 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s:%d\n", ip, port);
1029 			}
1030 			break;
1031 		case SWITCH_STUN_ATTR_USERNAME:
1032 			{
1033 				switch_stun_packet_attribute_get_username(attr, username, sizeof(username));
1034 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s\n", username);
1035 			}
1036 			break;
1037 
1038 		case SWITCH_STUN_ATTR_PRIORITY:
1039 			{
1040 				uint32_t priority = 0;
1041 				pri = (uint32_t *) attr->value;
1042 				priority = ntohl(*pri);
1043 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %u\n", priority);
1044 				ok = priority == ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].priority;
1045 			}
1046 			break;
1047 		}
1048 
1049 		if (!switch_stun_packet_next_attribute(attr, end_buf)) {
1050 			break;
1051 		}
1052 
1053 		xlen += 4 + switch_stun_attribute_padded_length(attr);
1054 	} while (xlen <= packet->header.length);
1055 
1056 	if ((ice->type & ICE_GOOGLE_JINGLE) && ok) {
1057 		ok = !strcmp(ice->user_ice, username);
1058 	}
1059 
1060 	if (packet->header.type != SWITCH_STUN_BINDING_REQUEST && packet->header.type != SWITCH_STUN_BINDING_RESPONSE) {
1061 		goto end;
1062 	}
1063 
1064 	if ((ice->type & ICE_VANILLA)) {
1065 		if (!ok) ok = !memcmp(packet->header.id, ice->last_sent_id, 12);
1066 
1067 		if (packet->header.type == SWITCH_STUN_BINDING_RESPONSE) {
1068 			ok = 1;
1069 			if (!ice->rready) {
1070 				if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
1071 					rtp_session->ice.rready = 1;
1072 					rtp_session->rtcp_ice.rready = 1;
1073 				} else {
1074 					ice->rready = 1;
1075 				}
1076 
1077 				if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
1078 					switch_core_session_video_reinit(rtp_session->session);
1079 				}
1080 			}
1081 		}
1082 
1083 		if (!ok && ice == &rtp_session->ice && rtp_session->rtcp_ice.ice_params && pri &&
1084 			*pri == rtp_session->rtcp_ice.ice_params->cands[rtp_session->rtcp_ice.ice_params->chosen[1]][1].priority) {
1085 			ice = &rtp_session->rtcp_ice;
1086 			ok = 1;
1087 		}
1088 
1089 		if (!zstr(username)) {
1090 			if (!icecmp(username, ice)) {
1091 				ok = 1;
1092 			} else if(!zstr(rtp_session->rtcp_ice.user_ice) && !icecmp(username, &rtp_session->rtcp_ice)) {
1093 				ice = &rtp_session->rtcp_ice;
1094 				ok = 1;
1095 			}
1096 		}
1097 
1098 		if (ok) {
1099 			ice->missed_count = 0;
1100 		} else {
1101 			switch_rtp_ice_t *icep[2] = { &rtp_session->ice, &rtp_session->rtcp_ice };
1102 			switch_port_t port = 0;
1103 			char *host = NULL;
1104 
1105 			if (elapsed > 20000 && pri) {
1106 				int i, j;
1107 				uint32_t old;
1108 				//const char *tx_host;
1109 				const char *old_host, *err = NULL;
1110 				//char bufa[50];
1111 				char bufb[50];
1112 				char adj_port[6];
1113 				switch_channel_t *channel = NULL;
1114 
1115 
1116 				ice->missed_count++;
1117 				//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "missed %d\n", ice->missed_count);
1118 
1119 
1120 				if (rtp_session->session) {
1121 					channel = switch_core_session_get_channel(rtp_session->session);
1122 				}
1123 
1124 				//ice->ice_params->cands[ice->ice_params->chosen][ice->proto].priority;
1125 				for (j = 0; j < 2; j++) {
1126 					if (!icep[j] || !icep[j]->ice_params) {
1127 						continue;
1128 					}
1129 					for (i = 0; i < icep[j]->ice_params->cand_idx[icep[j]->proto]; i++) {
1130 						if (icep[j]->ice_params &&  icep[j]->ice_params->cands[i][icep[j]->proto].priority == *pri) {
1131 							if (j == IPR_RTP) {
1132 								icep[j]->ice_params->chosen[j] = i;
1133 								switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Change candidate index to %d\n", i);
1134 							}
1135 
1136 							ice = icep[j];
1137 							ok = 1;
1138 
1139 							if (j != IPR_RTP) {
1140 								break;
1141 							}
1142 
1143 							old = rtp_session->remote_port;
1144 
1145 							//tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr);
1146 							old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
1147 
1148 							host = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr;
1149 							port = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port;
1150 
1151 							if (!host || !port) {
1152 								switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error setting remote host!\n");
1153 								switch_mutex_unlock(rtp_session->ice_mutex);
1154 								return;
1155 							}
1156 
1157 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO,
1158 											  "%s ICE Auto Changing port from %s:%u to %s:%u\n", rtp_type(rtp_session), old_host, old, host, port);
1159 
1160 
1161 							if (channel) {
1162 								switch_channel_set_variable(channel, "remote_media_ip_reported", switch_channel_get_variable(channel, "remote_media_ip"));
1163 								switch_channel_set_variable(channel, "remote_media_ip", host);
1164 								switch_channel_set_variable(channel, "rtp_auto_adjust_ip", host);
1165 								switch_snprintf(adj_port, sizeof(adj_port), "%u", port);
1166 								switch_channel_set_variable(channel, "remote_media_port_reported", switch_channel_get_variable(channel, "remote_media_port"));
1167 								switch_channel_set_variable(channel, "remote_media_port", adj_port);
1168 								switch_channel_set_variable(channel, "rtp_auto_adjust_port", adj_port);
1169 								switch_channel_set_variable(channel, "rtp_auto_candidate_adjust", "true");
1170 							}
1171 							rtp_session->auto_adj_used = 1;
1172 
1173 
1174 							switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err);
1175 							if (switch_sockaddr_info_get(&ice->addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS ||
1176 								!ice->addr) {
1177 								switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error setting remote host!\n");
1178 								switch_mutex_unlock(rtp_session->ice_mutex);
1179 								return;
1180 							}
1181 
1182 							if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
1183 								switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
1184 							} else {
1185 								switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
1186 							}
1187 
1188 						}
1189 					}
1190 				}
1191 			}
1192 		}
1193 	}
1194 
1195 	if (ice->missed_count > 5 && !(ice->type & ICE_GOOGLE_JINGLE)) {
1196 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "missed too many: %d, looking for new ICE dest.\n",
1197 						  ice->missed_count);
1198 		ice->rready = 0;
1199 		ok = 1;
1200 	}
1201 
1202 
1203 	//if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] || 1) {
1204 	//	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s %d\n", rtp_type(rtp_session), ok);
1205 	//}
1206 
1207 	if (ok) {
1208 		const char *host = NULL, *host2 = NULL;
1209 		switch_port_t port = 0, port2 = 0;
1210 		char buf[80] = "";
1211 		char buf2[80] = "";
1212 
1213 		if (packet->header.type == SWITCH_STUN_BINDING_REQUEST) {
1214 			uint8_t stunbuf[512];
1215 			switch_stun_packet_t *rpacket;
1216 			const char *remote_ip;
1217 			switch_size_t bytes;
1218 			char ipbuf[50];
1219 			switch_sockaddr_t *from_addr = rtp_session->from_addr;
1220 			switch_socket_t *sock_output = rtp_session->sock_output;
1221 			uint8_t do_adj = 0;
1222 			switch_time_t now = switch_micro_time_now();
1223 			int cmp = 0;
1224 			int cur_idx = -1;//, is_relay = 0;
1225 			int i;
1226 
1227 			if (is_rtcp) {
1228 				from_addr = rtp_session->rtcp_from_addr;
1229 				sock_output = rtp_session->rtcp_sock_output;
1230 			}
1231 
1232 			if (!ice->ready) {
1233 				ice->ready = 1;
1234 			}
1235 
1236 			memset(stunbuf, 0, sizeof(stunbuf));
1237 			rpacket = switch_stun_packet_build_header(SWITCH_STUN_BINDING_RESPONSE, packet->header.id, stunbuf);
1238 
1239 			if ((ice->type & ICE_GOOGLE_JINGLE)) {
1240 				switch_stun_packet_attribute_add_username(rpacket, username, (uint16_t)strlen(username));
1241 			}
1242 
1243 			remote_ip = switch_get_addr(ipbuf, sizeof(ipbuf), from_addr);
1244 
1245 			switch_stun_packet_attribute_add_xor_binded_address(rpacket, (char *) remote_ip, switch_sockaddr_get_port(from_addr), from_addr->family);
1246 
1247 			if ((ice->type & ICE_VANILLA)) {
1248 				switch_stun_packet_attribute_add_integrity(rpacket, ice->pass);
1249 				switch_stun_packet_attribute_add_fingerprint(rpacket);
1250 			}
1251 
1252 			bytes = switch_stun_packet_length(rpacket);
1253 
1254 			host = switch_get_addr(buf, sizeof(buf), from_addr);
1255 			port = switch_sockaddr_get_port(from_addr);
1256 			host2 = switch_get_addr(buf2, sizeof(buf2), ice->addr);
1257 			port2 = switch_sockaddr_get_port(ice->addr);
1258 			cmp = switch_cmp_addr(from_addr, ice->addr);
1259 
1260 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG4,
1261 							  "STUN from %s:%d %s\n", host, port, cmp ? "EXPECTED" : "IGNORED");
1262 
1263 			if (cmp) {
1264 				ice->last_ok = now;
1265 				rtp_session->wrong_addrs = 0;
1266 			} else {
1267 				if (((rtp_session->dtls && rtp_session->dtls->state != DS_READY) || !ice->ready || !ice->rready) &&
1268 					rtp_session->wrong_addrs > 2 && rtp_session->ice_adj == 0) {
1269 					do_adj++;
1270 					rtp_session->ice_adj = 1;
1271 					rtp_session->wrong_addrs = 0;
1272 				} else if (rtp_session->wrong_addrs > 10 || elapsed >= 10000) {
1273 					do_adj++;
1274 				}
1275 
1276 				if (!do_adj) {
1277 					rtp_session->wrong_addrs++;
1278 				}
1279 
1280 				for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) {
1281 					if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, host)) {
1282 						cur_idx = i;
1283 						//if (!strcasecmp(ice->ice_params->cands[i][ice->proto].cand_type, "relay")) {
1284 						//	is_relay = 1;
1285 						//}
1286 					}
1287 				}
1288 
1289 
1290 				if (ice->ice_params && ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type &&
1291 					!strcasecmp(ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type, "relay")) {
1292 					do_adj++;
1293 				}
1294 			}
1295 
1296 			if ((ice->type & ICE_VANILLA) && ice->ice_params && do_adj) {
1297 				ice->missed_count = 0;
1298 				ice->rready = 1;
1299 
1300 				if (cur_idx > -1) {
1301 					ice->ice_params->chosen[ice->proto] = cur_idx;
1302 				}
1303 
1304 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_NOTICE,
1305 								  "Auto Changing %s stun/%s/dtls port from %s:%u to %s:%u idx:%d\n", rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp",
1306 								  host2, port2,
1307 								  host, port, cur_idx);
1308 
1309 				switch_rtp_change_ice_dest(rtp_session, ice, host, port);
1310 				ice->last_ok = now;
1311 				rtp_session->wrong_addrs = 0;
1312 			}
1313 			//if (cmp) {
1314 			switch_socket_sendto(sock_output, from_addr, 0, (void *) rpacket, &bytes);
1315 			//}
1316 		}
1317 	} else if (packet->header.type == SWITCH_STUN_BINDING_ERROR_RESPONSE) {
1318 
1319 		if (rtp_session->session) {
1320 			switch_core_session_message_t msg = { 0 };
1321 			msg.from = __FILE__;
1322 			msg.numeric_arg = packet->header.type;
1323 			msg.pointer_arg = packet;
1324 			msg.message_id = SWITCH_MESSAGE_INDICATE_STUN_ERROR;
1325 			switch_core_session_receive_message(rtp_session->session, &msg);
1326 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,
1327 							  "STUN/ICE binding error received on %s channel\n", rtp_type(rtp_session));
1328 		}
1329 
1330 	}
1331 
1332 
1333 
1334 
1335  end:
1336 	switch_mutex_unlock(rtp_session->ice_mutex);
1337 	WRITE_DEC(rtp_session);
1338 	READ_DEC(rtp_session);
1339 }
1340 
1341 #ifdef ENABLE_ZRTP
SWITCH_STANDARD_SCHED_FUNC(zrtp_cache_save_callback)1342 SWITCH_STANDARD_SCHED_FUNC(zrtp_cache_save_callback)
1343 {
1344 	zrtp_status_t status = zrtp_status_ok;
1345 
1346 	status = zrtp_def_cache_store(zrtp_global);
1347 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Saving ZRTP cache: %s\n", zrtp_status_ok == status ? "OK" : "FAIL");
1348 	task->runtime = switch_epoch_time_now(NULL) + 900;
1349 }
1350 
zrtp_send_rtp_callback(const zrtp_stream_t * stream,char * rtp_packet,unsigned int rtp_packet_length)1351 static int zrtp_send_rtp_callback(const zrtp_stream_t *stream, char *rtp_packet, unsigned int rtp_packet_length)
1352 {
1353 	switch_rtp_t *rtp_session = zrtp_stream_get_userdata(stream);
1354 	switch_size_t len = rtp_packet_length;
1355 	zrtp_status_t status = zrtp_status_ok;
1356 
1357 	if (!rtp_session->sock_output) {
1358 		return status;
1359 	}
1360 
1361 	switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, rtp_packet, &len);
1362 	return status;
1363 }
1364 
zrtp_event_callback(zrtp_stream_t * stream,unsigned event)1365 static void zrtp_event_callback(zrtp_stream_t *stream, unsigned event)
1366 {
1367 	switch_rtp_t *rtp_session = zrtp_stream_get_userdata(stream);
1368 	zrtp_session_info_t zrtp_session_info;
1369 
1370 	switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
1371 	switch_event_t *fsevent = NULL;
1372 	const char *type;
1373 
1374 	type = rtp_type(rtp_session);
1375 
1376 	switch (event) {
1377 	case ZRTP_EVENT_IS_SECURE:
1378 		{
1379 			rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_SEND] = 1;
1380 			rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_RECV] = 1;
1381 			if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
1382 				rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 1;
1383 				rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 1;
1384 			}
1385 			if (zrtp_status_ok == zrtp_session_get(stream->session, &zrtp_session_info)) {
1386 				if (zrtp_session_info.sas_is_ready) {
1387 
1388 					switch_channel_set_variable_name_printf(channel, "true", "zrtp_secure_media_confirmed_%s", type);
1389 					switch_channel_set_variable_name_printf(channel, stream->session->sas1.buffer, "zrtp_sas1_string_%s", type);
1390 					switch_channel_set_variable_name_printf(channel, stream->session->sas2.buffer, "zrtp_sas2_string", type);
1391 					zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, (uint8_t)1);
1392 				}
1393 			}
1394 
1395 			if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
1396 
1397 
1398 				if (rtp_session->session) {
1399 					switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
1400 					switch_rtp_t *video_rtp_session = switch_channel_get_private(channel, "__zrtp_video_rtp_session");
1401 
1402 					if (!video_rtp_session) {
1403 						video_rtp_session = switch_channel_get_private_partner(channel, "__zrtp_video_rtp_session");
1404 					}
1405 
1406 					if (video_rtp_session) {
1407 						if (zrtp_status_ok != zrtp_stream_attach(stream->session, &video_rtp_session->zrtp_stream)) {
1408 							abort();
1409 						}
1410 						zrtp_stream_set_userdata(video_rtp_session->zrtp_stream, video_rtp_session);
1411 						if (switch_true(switch_channel_get_variable(channel, "zrtp_enrollment"))) {
1412 							zrtp_stream_registration_start(video_rtp_session->zrtp_stream, video_rtp_session->ssrc);
1413 						} else {
1414 							zrtp_stream_start(video_rtp_session->zrtp_stream, video_rtp_session->ssrc);
1415 						}
1416 					}
1417 				}
1418 			}
1419 
1420 			if (switch_event_create(&fsevent, SWITCH_EVENT_CALL_SECURE) == SWITCH_STATUS_SUCCESS) {
1421 				switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_media_type", "%s", type);
1422 				switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "zrtp:%s:%s", stream->session->sas1.buffer,
1423 										stream->session->sas2.buffer);
1424 				switch_event_add_header_string(fsevent, SWITCH_STACK_BOTTOM, "caller-unique-id", switch_channel_get_uuid(channel));
1425 				switch_event_fire(&fsevent);
1426 			}
1427 		}
1428 		break;
1429 #if 0
1430 	case ZRTP_EVENT_NO_ZRTP_QUICK:
1431 		{
1432 			if (stream != NULL) {
1433 				zrtp_stream_stop(stream);
1434 			}
1435 		}
1436 		break;
1437 #endif
1438 	case ZRTP_EVENT_IS_CLIENT_ENROLLMENT:
1439 		{
1440 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Enrolled complete!\n");
1441 			switch_channel_set_variable_name_printf(channel, "true", "zrtp_enroll_complete_%s", type);
1442 		}
1443 		break;
1444 
1445 	case ZRTP_EVENT_USER_ALREADY_ENROLLED:
1446 		{
1447 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "User already enrolled!\n");
1448 			switch_channel_set_variable_name_printf(channel, "true", "zrtp_already_enrolled_%s", type);
1449 		}
1450 		break;
1451 
1452 	case ZRTP_EVENT_NEW_USER_ENROLLED:
1453 		{
1454 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "New user enrolled!\n");
1455 			switch_channel_set_variable_name_printf(channel, "true", "zrtp_new_user_enrolled_%s", type);
1456 		}
1457 		break;
1458 
1459 	case ZRTP_EVENT_USER_UNENROLLED:
1460 		{
1461 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "User unenrolled!\n");
1462 			switch_channel_set_variable_name_printf(channel, "true", "zrtp_user_unenrolled_%s", type);
1463 		}
1464 		break;
1465 
1466 	case ZRTP_EVENT_IS_PENDINGCLEAR:
1467 		{
1468 			switch_channel_set_variable_name_printf(channel, "false", "zrtp_secure_media_confirmed_%s", type);
1469 			rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_SEND] = 0;
1470 			rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_RECV] = 0;
1471 			rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
1472 			rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
1473 			rtp_session->zrtp_mitm_tries = 0;
1474 		}
1475 		break;
1476 
1477 	case ZRTP_EVENT_NO_ZRTP:
1478 		{
1479 			switch_channel_set_variable_name_printf(channel, "false", "zrtp_secure_media_confirmed_%s", type);
1480 		}
1481 		break;
1482 
1483 	default:
1484 		break;
1485 	}
1486 }
1487 
zrtp_logger(int level,const char * data,int len,int offset)1488 static void zrtp_logger(int level, const char *data, int len, int offset)
1489 {
1490 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s", data);
1491 }
1492 #endif
1493 
switch_rtp_init(switch_memory_pool_t * pool)1494 SWITCH_DECLARE(void) switch_rtp_init(switch_memory_pool_t *pool)
1495 {
1496 #ifdef ENABLE_ZRTP
1497 	const char *zid_string = switch_core_get_variable_pdup("switch_serial", pool);
1498 	const char *zrtp_enabled = switch_core_get_variable_pdup("zrtp_enabled", pool);
1499 	zrtp_config_t zrtp_config;
1500 	char zrtp_cache_path[256] = "";
1501 	zrtp_on = zrtp_enabled ? switch_true(zrtp_enabled) : 0;
1502 #endif
1503 	if (global_init) {
1504 		return;
1505 	}
1506 	switch_core_hash_init(&alloc_hash);
1507 #ifdef ENABLE_ZRTP
1508 	if (zrtp_on) {
1509 		uint32_t cache_len;
1510 		zrtp_config_defaults(&zrtp_config);
1511 		strcpy(zrtp_config.client_id, "FreeSWITCH");
1512 		zrtp_config.is_mitm = 1;
1513 		zrtp_config.lic_mode = ZRTP_LICENSE_MODE_ACTIVE;
1514 		switch_snprintf(zrtp_cache_path, sizeof(zrtp_cache_path), "%s%szrtp.dat", SWITCH_GLOBAL_dirs.db_dir, SWITCH_PATH_SEPARATOR);
1515 		cache_len=(uint32_t)strlen(zrtp_cache_path);
1516 		ZSTR_SET_EMPTY(zrtp_config.def_cache_path);
1517 		zrtp_config.def_cache_path.length = cache_len > zrtp_config.def_cache_path.max_length ? zrtp_config.def_cache_path.max_length : (uint16_t)cache_len;
1518 		strncpy(zrtp_config.def_cache_path.buffer, zrtp_cache_path, zrtp_config.def_cache_path.max_length);
1519 		zrtp_config.cb.event_cb.on_zrtp_protocol_event = (void (*)(zrtp_stream_t*,zrtp_protocol_event_t))zrtp_event_callback;
1520 		zrtp_config.cb.misc_cb.on_send_packet = zrtp_send_rtp_callback;
1521 		zrtp_config.cb.event_cb.on_zrtp_security_event = (void (*)(zrtp_stream_t*,zrtp_security_event_t))zrtp_event_callback;
1522 		zrtp_log_set_log_engine((zrtp_log_engine *) zrtp_logger);
1523 		zrtp_log_set_level(4);
1524 		if (zrtp_status_ok == zrtp_init(&zrtp_config, &zrtp_global)) {
1525 			memcpy(zid, zid_string, 12);
1526 			switch_scheduler_add_task(switch_epoch_time_now(NULL) + 900, zrtp_cache_save_callback, "zrtp_cache_save", "core", 0, NULL,
1527 									  SSHF_NONE | SSHF_NO_DEL);
1528 		} else {
1529 			switch_core_set_variable("zrtp_enabled", NULL);
1530 			zrtp_on = 0;
1531 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "ZRTP init failed!\n");
1532 		}
1533 	}
1534 #endif
1535 #ifdef ENABLE_SRTP
1536 	srtp_init();
1537 #endif
1538 	switch_mutex_init(&port_lock, SWITCH_MUTEX_NESTED, pool);
1539 	global_init = 1;
1540 }
1541 
get_next_write_ts(switch_rtp_t * rtp_session,uint32_t timestamp)1542 static uint8_t get_next_write_ts(switch_rtp_t *rtp_session, uint32_t timestamp)
1543 {
1544 	uint8_t m = 0, changed = 0;
1545 
1546 	if (!(rtp_session->rtp_bugs & RTP_BUG_SEND_LINEAR_TIMESTAMPS)) {
1547 		if (timestamp) {
1548 			rtp_session->ts = (uint32_t) timestamp;
1549 			changed++;
1550 		} else if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) {
1551 			switch_core_timer_next(&rtp_session->write_timer);
1552 			rtp_session->ts = rtp_session->write_timer.samplecount;
1553 			changed++;
1554 		}
1555 	}
1556 
1557 	if (!changed) {
1558 		rtp_session->ts = rtp_session->last_write_ts + rtp_session->samples_per_interval;
1559 	} else {
1560 		/* Send marker bit if timestamp is lower/same as before (resetted/new timer) */
1561 		if (abs((int32_t)(rtp_session->ts - rtp_session->last_write_ts)) > rtp_session->samples_per_interval
1562 			&& !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
1563 			m++;
1564 		}
1565 	}
1566 
1567 	return m;
1568 }
1569 
do_mos(switch_rtp_t * rtp_session)1570 static void do_mos(switch_rtp_t *rtp_session) {
1571 	int R;
1572 
1573 	if ((switch_size_t)rtp_session->stats.inbound.recved < rtp_session->stats.inbound.flaws) {
1574 		rtp_session->stats.inbound.flaws = 0;
1575 	}
1576 
1577 	if (rtp_session->stats.inbound.recved > 0 &&
1578 		rtp_session->stats.inbound.flaws && (rtp_session->stats.inbound.last_flaw != rtp_session->stats.inbound.flaws)) {
1579 
1580 		if (rtp_session->consecutive_flaws++) {
1581 			int penalty = rtp_session->consecutive_flaws;
1582 
1583 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s %s %d consecutive flaws, adding %d flaw penalty\n",
1584 							  rtp_session_name(rtp_session), rtp_type(rtp_session),
1585 							  rtp_session->consecutive_flaws, penalty);
1586 			rtp_session->bad_stream++;
1587 			rtp_session->stats.inbound.flaws += penalty;
1588 			rtp_session->stats.inbound.last_flaw = rtp_session->stats.inbound.flaws;
1589 
1590 			if (rtp_session->stats.inbound.error_log) {
1591 				rtp_session->stats.inbound.error_log->flaws += penalty;
1592 				rtp_session->stats.inbound.error_log->consecutive_flaws++;
1593 			}
1594 		}
1595 	} else {
1596 		rtp_session->consecutive_flaws = 0;
1597 	}
1598 
1599 	R = (int)((double)((double)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws) / (double)rtp_session->stats.inbound.recved) * 100.0);
1600 
1601 	if (R < 0 || R > 100) R = 100;
1602 
1603 	rtp_session->stats.inbound.R = R;
1604 	rtp_session->stats.inbound.mos = 1 + (0.035) * R + (.000007) * R * (R-60) * (100-R);
1605 
1606 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "%s %s stat %0.2f %ld/%d flaws: %ld mos: %0.2f v: %0.2f %0.2f/%0.2f\n",
1607 					  rtp_session_name(rtp_session),
1608 					  rtp_type(rtp_session),
1609 					  rtp_session->stats.inbound.R,
1610 					  (long int)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws), rtp_session->stats.inbound.recved,
1611 					  (long int)rtp_session->stats.inbound.flaws,
1612 					  rtp_session->stats.inbound.mos,
1613 					  rtp_session->stats.inbound.variance,
1614 					  rtp_session->stats.inbound.min_variance,
1615 					  rtp_session->stats.inbound.max_variance
1616 					  );
1617 
1618 }
1619 
burstr_calculate(int loss[],int received,double * burstr,double * lossr)1620 void burstr_calculate ( int loss[], int received, double *burstr, double *lossr )
1621 {
1622 	int lost = 0;
1623 	int bursts = 0;
1624 	int i;
1625 
1626 	for ( i = 0; i < LOST_BURST_ANALYZE; i++ ) {
1627 		lost += i * loss[i];
1628 		bursts += loss[i];
1629 	}
1630 	if (received > 0 && bursts > 0) {
1631 		*burstr = (double)((double)lost / (double)bursts) / (double)(1.0 / ( 1.0 - (double)lost / (double)received ));
1632 		if (*burstr < 0) {
1633 			*burstr = - *burstr;
1634 		}
1635 	} else {
1636 		*burstr = 0;
1637 	}
1638 	if (received > 0) {
1639 		*lossr = (double)((double)lost / (double)received);
1640 	} else {
1641 		*lossr = 0;
1642 	}
1643 }
1644 
reset_jitter_seq(switch_rtp_t * rtp_session)1645 static void reset_jitter_seq(switch_rtp_t *rtp_session)
1646 {
1647 	rtp_session->stats.inbound.last_proc_time = 0;
1648 	rtp_session->stats.inbound.last_processed_seq = 0;
1649 	rtp_session->jitter_lead = 0;
1650 	rtp_session->consecutive_flaws = 0;
1651 	rtp_session->stats.inbound.last_flaw = 0;
1652 }
1653 
check_jitter(switch_rtp_t * rtp_session)1654 static void check_jitter(switch_rtp_t *rtp_session)
1655 {
1656 	switch_time_t current_time;
1657 	int64_t diff_time = 0, cur_diff = 0;
1658 	int seq;
1659 
1660 	current_time = switch_micro_time_now() / 1000;
1661 
1662 	if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] || rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] || rtp_session->dtmf_data.in_digit_ts) {
1663 		reset_jitter_seq(rtp_session);
1664 		return;
1665 	}
1666 
1667 	if (++rtp_session->jitter_lead < JITTER_LEAD_FRAMES || !rtp_session->stats.inbound.last_proc_time) {
1668 		rtp_session->stats.inbound.last_proc_time = current_time;
1669 		return;
1670 	}
1671 
1672 	diff_time = (current_time - rtp_session->stats.inbound.last_proc_time);
1673 	seq = (int)(uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
1674 
1675 	/* Burst and Packet Loss */
1676 	rtp_session->stats.inbound.recved++;
1677 
1678 	if (rtp_session->stats.inbound.last_processed_seq > 0 && seq > (int)(rtp_session->stats.inbound.last_processed_seq + 1)) {
1679 		int lost = (seq - rtp_session->stats.inbound.last_processed_seq - 1);
1680 
1681 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s Got: %s seq %d but expected: %d lost: %d\n",
1682 						  rtp_session_name(rtp_session),
1683 						  rtp_type(rtp_session),
1684 						  seq,
1685 						  (rtp_session->stats.inbound.last_processed_seq + 1), lost);
1686 		rtp_session->stats.inbound.last_loss++;
1687 
1688 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
1689 			switch_core_session_request_video_refresh(rtp_session->session);
1690 		}
1691 
1692 		if (rtp_session->stats.inbound.last_loss > 0 && rtp_session->stats.inbound.last_loss < LOST_BURST_CAPTURE) {
1693 			rtp_session->stats.inbound.loss[rtp_session->stats.inbound.last_loss] += lost;
1694 		}
1695 
1696 		rtp_session->bad_stream++;
1697 		rtp_session->stats.inbound.flaws += lost;
1698 
1699 		if (rtp_session->stats.inbound.error_log) {
1700 			rtp_session->stats.inbound.error_log->flaws += lost;
1701 		}
1702 
1703 	} else {
1704 		rtp_session->stats.inbound.last_loss = 0;
1705 	}
1706 
1707 	rtp_session->stats.inbound.last_processed_seq = seq;
1708 
1709 	/* Burst and Packet Loss */
1710 
1711 	if (current_time > rtp_session->next_stat_check_time) {
1712 		rtp_session->next_stat_check_time = current_time + 5000;
1713 		burstr_calculate(rtp_session->stats.inbound.loss, rtp_session->stats.inbound.recved,
1714 						 &(rtp_session->stats.inbound.burstrate), &(rtp_session->stats.inbound.lossrate));
1715 		do_mos(rtp_session);
1716 	} else {
1717 		do_mos(rtp_session);
1718 	}
1719 
1720 	if (rtp_session->stats.inbound.last_loss || rtp_session->bad_stream) {
1721 		if (rtp_session->session && (!rtp_session->stats.inbound.error_log || rtp_session->stats.inbound.error_log->stop)) {
1722 			struct error_period *error = switch_core_session_alloc(rtp_session->session, sizeof(*error));
1723 			error->start = switch_micro_time_now();
1724 			error->next = rtp_session->stats.inbound.error_log;
1725 			rtp_session->stats.inbound.error_log = error;
1726 		}
1727 
1728 		if (!rtp_session->stats.inbound.last_loss) {
1729 			if (++rtp_session->recovering_stream > (rtp_session->one_second * 3)) {
1730 				if (rtp_session->session && rtp_session->stats.inbound.error_log) {
1731 					rtp_session->stats.inbound.error_log->stop = switch_micro_time_now();
1732 				}
1733 
1734 				rtp_session->bad_stream = 0;
1735 			}
1736 		} else {
1737 			rtp_session->recovering_stream = 0;
1738 			rtp_session->bad_stream++;
1739 		}
1740 	} else {
1741 		rtp_session->recovering_stream = 0;
1742 		rtp_session->clean_stream++;
1743 	}
1744 
1745 
1746 	if ( diff_time < 0 ) {
1747 		diff_time = -diff_time;
1748 	}
1749 
1750 	rtp_session->stats.inbound.jitter_n++;
1751 	rtp_session->stats.inbound.jitter_add += diff_time;
1752 
1753 	if (rtp_session->stats.inbound.mean_interval) {
1754 		cur_diff = (int64_t)(diff_time - rtp_session->stats.inbound.mean_interval);
1755 	} else {
1756 		cur_diff = 0;
1757 	}
1758 
1759 	rtp_session->stats.inbound.jitter_addsq += (cur_diff * cur_diff);
1760 	rtp_session->stats.inbound.last_proc_time = current_time;
1761 
1762 	if (rtp_session->stats.inbound.jitter_n > 0) {
1763 		double ipdv;
1764 
1765 		rtp_session->stats.inbound.mean_interval = (double)rtp_session->stats.inbound.jitter_add / (double)rtp_session->stats.inbound.jitter_n;
1766 
1767 		if (!rtp_session->old_mean) {
1768 			rtp_session->old_mean = rtp_session->stats.inbound.mean_interval;
1769 		}
1770 
1771 		rtp_session->stats.inbound.variance = (double)rtp_session->stats.inbound.jitter_addsq / (double)rtp_session->stats.inbound.jitter_n;
1772 
1773 		//printf("CHECK %d +%ld +%ld %f %f\n", rtp_session->write_timer.samplecount, diff_time, (diff_time * diff_time), rtp_session->stats.inbound.mean_interval, rtp_session->stats.inbound.variance);
1774 
1775 		ipdv = rtp_session->old_mean - rtp_session->stats.inbound.mean_interval;
1776 
1777 		if ( ipdv > IPDV_THRESHOLD ) { /* It shows Increasing Delays */
1778 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG3, "Calculated Instantaneous Packet Delay Variation: %s packet %lf\n",
1779 							  rtp_type(rtp_session), ipdv);
1780 		}
1781 
1782 		if ( rtp_session->stats.inbound.variance < rtp_session->stats.inbound.min_variance || rtp_session->stats.inbound.min_variance == 0 ) {
1783 			rtp_session->stats.inbound.min_variance = rtp_session->stats.inbound.variance;
1784 		}
1785 
1786 		if ( rtp_session->stats.inbound.variance > rtp_session->stats.inbound.max_variance ) {
1787 			rtp_session->stats.inbound.max_variance = rtp_session->stats.inbound.variance;
1788 		}
1789 
1790 		rtp_session->old_mean = rtp_session->stats.inbound.mean_interval;
1791 	}
1792 }
1793 
rtcp_generate_sender_info(switch_rtp_t * rtp_session,struct switch_rtcp_sender_info * sr)1794 static void rtcp_generate_sender_info(switch_rtp_t *rtp_session, struct switch_rtcp_sender_info *sr){
1795 	switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1796 	switch_time_t now;
1797 	uint32_t sec, ntp_sec, ntp_usec;
1798 	switch_time_exp_t now_hr;
1799 	now = switch_micro_time_now();
1800 	sec = (uint32_t)(now/1000000);        /* convert to seconds     */
1801 	ntp_sec = sec+NTP_TIME_OFFSET;  /* convert to NTP seconds */
1802 	sr->ntp_msw = htonl(ntp_sec);   /* store result in "most significant word" */
1803 	ntp_usec = (uint32_t)(now - (sec*1000000)); /* remove seconds to keep only the microseconds */
1804 	sr->ntp_lsw = htonl((u_long)(ntp_usec*(double)(((uint64_t)1)<<32)*1.0e-6)); /* convert microseconds to fraction of 32bits and store result in "least significatn word" */
1805 
1806 	sr->ts = htonl(rtp_session->last_write_ts);
1807 	sr->pc = htonl(rtp_session->stats.outbound.packet_count);
1808 	sr->oc = htonl(rtp_session->stats.outbound.raw_bytes - rtp_session->stats.outbound.packet_count * sizeof(srtp_hdr_t));
1809 
1810 	switch_time_exp_gmt(&now_hr,now);
1811 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG10,"Sending an RTCP packet[%04d-%02d-%02d %02d:%02d:%02d.%d] lsr[%u] msw[%u] lsw[%u] stats_ssrc[%u]\n",
1812 			1900 + now_hr.tm_year,  now_hr.tm_mday, now_hr.tm_mon, now_hr.tm_hour, now_hr.tm_min, now_hr.tm_sec, now_hr.tm_usec,
1813 			(ntohl(sr->ntp_lsw)&0xffff0000)>>16 | (ntohl(sr->ntp_msw)&0x0000ffff)<<16,
1814 			ntohl(sr->ntp_msw),ntohl(sr->ntp_lsw), rtp_session->stats.rtcp.ssrc
1815 			);
1816 }
1817 
calc_local_lsr_now()1818 static inline uint32_t calc_local_lsr_now()
1819 {
1820 	switch_time_t now;
1821 	uint32_t ntp_sec, ntp_usec, lsr_now, sec;
1822 	now = switch_micro_time_now();
1823 	sec = (uint32_t)(now/1000000);        /* convert to seconds     */
1824 	ntp_sec = sec+NTP_TIME_OFFSET;  /* convert to NTP seconds */
1825 	ntp_usec = (uint32_t)(now - ((switch_time_t) sec*1000000)); /* remove seconds to keep only the microseconds */
1826 
1827 	lsr_now = (uint32_t)(ntp_usec*0.065536) | (ntp_sec&0x0000ffff)<<16; /* 0.065536 is used for convertion from useconds to fraction of 65536 (x65536/1000000) */
1828 
1829 	return lsr_now;
1830 }
1831 
1832 //#define DEBUG_RTCP
1833 /* extra param is for duplicates (received NACKed packets) */
rtcp_generate_report_block(switch_rtp_t * rtp_session,struct switch_rtcp_report_block * rtcp_report_block,int16_t extra_expected)1834 static void rtcp_generate_report_block(switch_rtp_t *rtp_session, struct switch_rtcp_report_block *rtcp_report_block,
1835 		int16_t extra_expected)
1836 {
1837 #ifdef DEBUG_RTCP
1838 	switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1839 #endif
1840 	switch_rtcp_numbers_t * stats=&rtp_session->stats.rtcp;
1841 	uint32_t expected_pkt, dlsr = 0;
1842 	int32_t pkt_lost;
1843 
1844 	/* Packet loss */
1845 	if (stats->rtcp_rtp_count == 0) {
1846 		expected_pkt = stats->high_ext_seq_recv - stats->base_seq + 1;
1847 	} else {
1848 		expected_pkt = stats->high_ext_seq_recv - stats->last_rpt_ext_seq + extra_expected;
1849 	}
1850 
1851 	pkt_lost = expected_pkt - stats->period_pkt_count;
1852 	if (pkt_lost < 0) pkt_lost = 0;
1853 
1854 	stats->cum_lost=stats->cum_lost+pkt_lost;
1855 	if (expected_pkt > 0 && pkt_lost > 0) {
1856 		rtcp_report_block->fraction = (pkt_lost == expected_pkt ? 255 : (uint8_t) (pkt_lost * 256 / expected_pkt));             /* if X packets were expected and X was lost, we want 0xff to be reported, not 0 */
1857 	} else {
1858 		rtcp_report_block->fraction = 0;
1859 	}
1860 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
1861 	rtcp_report_block->lost = stats->cum_lost;
1862 #else
1863 	/* Reversing byte order for 24bits */
1864 	rtcp_report_block->lost = htonl(stats->cum_lost) >> 8;
1865 #endif
1866 
1867 #ifdef DEBUG_RTCP
1868 			if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO])
1869 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "rtcp_generate_sr: stats_ssrc[%u]\nreceived[%d]\nexpected[%d]\ncum[%d]\nlost[%d|%d/256]pkt\nlast_seq[%d]\ncyc[%d]\nlast_rpt_seq[%d]\ncyc[%d]\nssrc[%d]\n",
1870 						rtp_session->remote_ssrc, stats->period_pkt_count, expected_pkt,
1871 						stats->cum_lost, pkt_lost, rtcp_report_block->fraction, stats->high_ext_seq_recv&0x0000ffff,
1872 						stats->cycle, stats->last_rpt_ext_seq&0x0000ffff, stats->last_rpt_cycle, rtp_session->stats.rtcp.peer_ssrc
1873 						);
1874 #endif
1875 	rtcp_report_block->highest_sequence_number_received = htonl(stats->high_ext_seq_recv);
1876 
1877 	/* Jitter */
1878 	rtcp_report_block->jitter = htonl((uint32_t)stats->inter_jitter);
1879 
1880 	/* Delay since Last Sender Report (DLSR) : 32bits, 1/65536 seconds */
1881 	if (stats->last_recv_lsr_local) {
1882 		uint32_t lsr_now = calc_local_lsr_now();
1883 		/* check lsr_now: what we just read from clock may be in the past (race cond), don't send huge dlsr due to uint wrap around */
1884 		if (lsr_now > stats->last_recv_lsr_local) {
1885 			dlsr = lsr_now - stats->last_recv_lsr_local;
1886 		}
1887 	}
1888 	rtcp_report_block->lsr = stats->last_recv_lsr_peer;
1889 	rtcp_report_block->dlsr = htonl(dlsr);
1890 	rtcp_report_block->ssrc = htonl(rtp_session->stats.rtcp.peer_ssrc);
1891 	stats->rtcp_rtp_count++;
1892 }
1893 
rtcp_stats_init(switch_rtp_t * rtp_session)1894 static void rtcp_stats_init(switch_rtp_t *rtp_session)
1895 {
1896 	switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
1897 	srtp_hdr_t * hdr = &rtp_session->last_rtp_hdr;
1898 	switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1899 	stats->ssrc = ntohl(hdr->ssrc);
1900 	stats->last_rpt_ts = rtp_session->write_timer.samplecount;
1901 	stats->init = 1;
1902 	stats->last_rpt_ext_seq = 0;
1903 	stats->last_rpt_cycle = 0;
1904 	stats->last_pkt_tsdiff = 0;
1905 	stats->inter_jitter = 0;
1906 	stats->cycle = 0;
1907 	stats->high_ext_seq_recv = ntohs((uint16_t)hdr->seq);
1908 	stats->base_seq = ntohs((uint16_t)hdr->seq);
1909 	stats->bad_seq = (1<<16) + 1; /* Make sure we wont missmatch 2 consecutive packets, so seq == bad_seq is false */
1910 	stats->cum_lost = 0;
1911 	stats->period_pkt_count = 0;
1912 	stats->sent_pkt_count = 0;
1913 	stats->pkt_count = 0;
1914 	stats->rtcp_rtp_count = 0;
1915 
1916 	if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
1917 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats_init: %s rtcp disabled\n", rtp_type(rtp_session));
1918 	} else if (!rtp_session->rtcp_sock_output) {
1919 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "rtcp_stats_init: %s no rtcp socket\n", rtp_type(rtp_session));
1920 	} else if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
1921 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats_init: %s rtcp passthru\n", rtp_type(rtp_session));
1922 	} else {
1923 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats_init: %s ssrc[%u] base_seq[%u]\n", rtp_type(rtp_session), stats->ssrc, stats->base_seq);
1924 	}
1925 
1926 	if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && (switch_core_media_codec_get_cap(rtp_session->session,
1927 															SWITCH_MEDIA_TYPE_AUDIO, SWITCH_CODEC_FLAG_HAS_ADJ_BITRATE))) {
1928 			kalman_estimator_t *estimators[KALMAN_SYSTEM_MODELS];
1929 			cusum_kalman_detector_t *detectors[KALMAN_SYSTEM_MODELS];
1930 
1931 			rtp_session->flags[SWITCH_RTP_FLAG_ADJ_BITRATE_CAP] = 1;
1932 			rtp_session->flags[SWITCH_RTP_FLAG_ESTIMATORS] = 1;
1933 
1934 			rtp_session->estimators[EST_LOSS] = switch_core_alloc(rtp_session->pool, sizeof(*estimators[0]));
1935 			switch_kalman_init(rtp_session->estimators[EST_LOSS],0.1,0.1);
1936 			rtp_session->estimators[EST_RTT] = switch_core_alloc(rtp_session->pool, sizeof(*estimators[0]));
1937 			switch_kalman_init(rtp_session->estimators[EST_RTT],0.03,1);
1938 			rtp_session->detectors[EST_RTT] = switch_core_alloc(rtp_session->pool, sizeof(*detectors[0]));
1939 			switch_kalman_cusum_init(rtp_session->detectors[EST_RTT],0.005,0.5);
1940 			rtp_session->detectors[EST_LOSS] = switch_core_alloc(rtp_session->pool, sizeof(*detectors[0]));
1941 			switch_kalman_cusum_init(rtp_session->detectors[EST_LOSS],0.005,0.5);
1942 	}
1943 }
1944 
rtcp_stats(switch_rtp_t * rtp_session)1945 static int rtcp_stats(switch_rtp_t *rtp_session)
1946 {
1947 	switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1948 	srtp_hdr_t * hdr = &rtp_session->last_rtp_hdr;
1949 	switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
1950 	uint32_t packet_spacing_diff = 0, pkt_tsdiff, pkt_extended_seq;
1951 	uint16_t pkt_seq, seq_diff, max_seq;
1952 	const int MAX_DROPOUT = 3000;
1953 	const int MAX_MISORDER = 100;
1954 	const int RTP_SEQ_MOD = (1<<16);
1955 
1956 	if(!rtp_session->rtcp_sock_output || !rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] || rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] || !rtp_session->rtcp_interval)
1957 		return 0; /* do not process RTCP in current state */
1958 
1959 	pkt_seq = (uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
1960 
1961 	/* Detect sequence number cycle change */
1962 	max_seq = stats->high_ext_seq_recv&0x0000ffff;
1963 	seq_diff = pkt_seq - max_seq;
1964 
1965 	if (seq_diff < MAX_DROPOUT) {  /* in order, with permissible gap */
1966 		if (pkt_seq < max_seq) {
1967 			stats->cycle++;
1968 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats:[cycle change] pkt_seq[%d] cycle[%d] max_seq[%d] stats_ssrc[%u] local_ts[%u]\n",
1969 					pkt_seq, stats->cycle, max_seq, stats->ssrc, rtp_session->timer.samplecount);
1970 		}
1971 		pkt_extended_seq = stats->cycle << 16 | pkt_seq; /* getting the extended packet extended sequence ID */
1972 		if (pkt_extended_seq > stats->high_ext_seq_recv) {
1973 			stats->high_ext_seq_recv = pkt_extended_seq;
1974 		}
1975 	}
1976 	else if (seq_diff <= (RTP_SEQ_MOD - MAX_MISORDER)) {   /* the sequence number made a very large jump */
1977 		if (pkt_seq == stats->bad_seq) {
1978 			rtcp_stats_init(rtp_session);
1979 		} else {
1980 			stats->bad_seq = (pkt_seq + 1) & (RTP_SEQ_MOD-1);
1981 		}
1982 		return 0; /* no stats, packet is out of sync and will be accounted as lost */
1983 	} else {
1984 		/* duplicate or reordered packet */
1985 	}
1986 
1987 	/* Verify that we are on the same stream source (we do not support multiple sources) */
1988 	if (ntohl(hdr->ssrc) != stats->ssrc || !stats->init) {
1989 		rtcp_stats_init(rtp_session);
1990 	}
1991 
1992 	stats->period_pkt_count++;
1993 	stats->pkt_count++;
1994 #ifdef DEBUG_RTCP
1995 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG10, "rtcp_stats: period_pkt_count[%d]last_seq[%d]cycle[%d]stats_ssrc[%u]local_ts[%u]\n",
1996 			stats->period_pkt_count, pkt_seq, stats->cycle, stats->ssrc, rtp_session->write_timer.samplecount);
1997 #endif
1998 	/* Interarrival jitter calculation */
1999 	pkt_tsdiff = abs((int32_t)(rtp_session->timer.samplecount - ntohl(hdr->ts)));  /* relative transit times for this packet */
2000 	if (stats->pkt_count < 2) { /* Can not compute Jitter with only one packet */
2001 		stats->last_pkt_tsdiff = pkt_tsdiff;
2002 	} else {
2003 		/* Jitter : difference of relative transit times for the two packets */
2004 		packet_spacing_diff = abs((int32_t)(pkt_tsdiff - stats->last_pkt_tsdiff));
2005 		stats->last_pkt_tsdiff = pkt_tsdiff;
2006 		/* Interarrival jitter estimation, "J(i) = J(i-1) + ( |D(i-1,i)| - J(i-1) )/16" */
2007 		stats->inter_jitter = (stats->inter_jitter + (((double)packet_spacing_diff - stats->inter_jitter) /16.));
2008 	}
2009 
2010 #ifdef DEBUG_RTCP
2011 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG10, "rtcp_stats: pkt_ts[%d]local_ts[%d]diff[%d]pkt_spacing[%d]inter_jitter[%f]seq[%d]stats_ssrc[%d]",
2012 			ntohl(hdr->ts), rtp_session->timer.samplecount, pkt_tsdiff, packet_spacing_diff, stats->inter_jitter, ntohs(hdr->seq), stats->ssrc);
2013 #endif
2014 	return 1;
2015 }
2016 
calc_bw_exp(uint32_t bps,uint8_t bits,rtcp_tmmbx_t * tmmbx)2017 static void calc_bw_exp(uint32_t bps, uint8_t bits, rtcp_tmmbx_t *tmmbx)
2018 {
2019 	uint32_t mantissa_max, i = 0;
2020 	uint8_t exp = 0;
2021 	uint32_t mantissa = 0;
2022 	uint16_t overhead = 60;
2023 
2024 	switch_assert(bits<=32);
2025 
2026 	mantissa_max = (1 << bits) - 1;
2027 
2028 	for (i = 0; i < 32; ++i) {
2029 		if (bps <= (mantissa_max << i)) {
2030 			exp = i;
2031 			break;
2032 		}
2033 	}
2034 
2035 	mantissa = (bps >> exp);
2036 
2037 	tmmbx->parts[0] = (uint8_t) ((exp << 2) + ((mantissa >> 15) & 0x03));
2038 	tmmbx->parts[1] = (uint8_t) (mantissa >> 7);
2039 	tmmbx->parts[2] = (uint8_t) ((mantissa >> 1) + ((overhead >> 8) & 0x01));
2040 	tmmbx->parts[3] = (uint8_t) (overhead);
2041 }
2042 
using_ice(switch_rtp_t * rtp_session)2043 static int using_ice(switch_rtp_t *rtp_session)
2044 {
2045 	if (rtp_session->ice.ice_user || rtp_session->rtcp_ice.ice_user) {
2046 		return 1;
2047 	}
2048 
2049 	return 0;
2050 }
2051 
2052 #define MAX_NACK 10
check_rtcp_and_ice(switch_rtp_t * rtp_session)2053 static int check_rtcp_and_ice(switch_rtp_t *rtp_session)
2054 {
2055 	int ret = 0;
2056 	int rtcp_ok = 0, rtcp_cyclic = 0, rtcp_fb = 0, force_send_rr = 0;
2057 	switch_time_t now = switch_micro_time_now();
2058 	int rate = 0, nack_ttl = 0, nack_dup = 0;
2059 	uint32_t cur_nack[MAX_NACK] = { 0 };
2060 	uint16_t seq = 0;
2061 
2062 	if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
2063 		rtp_session->flags[SWITCH_RTP_FLAG_AUTO_CNG] &&
2064 		rtp_session->send_msg.header.ts &&
2065 		rtp_session->cng_pt != INVALID_PT &&
2066 		(rtp_session->write_timer.samplecount - rtp_session->last_write_samplecount >= rtp_session->samples_per_interval * 60)) {
2067 		uint8_t data[10] = { 0 };
2068 		switch_frame_flag_t frame_flags = SFF_NONE;
2069 		data[0] = 65;
2070 		rtp_session->cn++;
2071 
2072 		get_next_write_ts(rtp_session, 0);
2073 		rtp_session->send_msg.header.ts = htonl(rtp_session->ts);
2074 
2075 		switch_rtp_write_manual(rtp_session, (void *) data, 2, 0, rtp_session->cng_pt, ntohl(rtp_session->send_msg.header.ts), &frame_flags);
2076 
2077 		if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) {
2078 			rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount;
2079 		}
2080 	}
2081 
2082 	rate = rtp_session->rtcp_interval;
2083 
2084 	if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->vb) {
2085 		int n;
2086 		for (n = 0; n < MAX_NACK; n++) {
2087 			uint32_t nack = switch_jb_pop_nack(rtp_session->vb);
2088 
2089 			if (!nack) break;
2090 
2091 			seq = ntohs(nack & 0xFFFF);
2092 
2093 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s Got NACK [%u][0x%x] for seq %u\n",
2094 					switch_core_session_get_name(rtp_session->session), nack, nack, seq);
2095 
2096 			cur_nack[nack_ttl++] = nack;
2097 		}
2098 		if (nack_ttl) {
2099 			rtcp_ok = 1;
2100 			rtcp_fb = 1;
2101 		}
2102 	}
2103 
2104 
2105 
2106 	if (rtp_session->rtcp_sent_packets < 4) {
2107 		rate = 4000;
2108 	} else  {
2109 		if (rtp_session->pli_count || rtp_session->fir_count || rtp_session->tmmbr || rtp_session->tmmbn) {
2110 			//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "MARK BW/FIR ETC %d %d\n", rtp_session->pli_count, rtp_session->fir_count);
2111 			rtcp_ok = 1;
2112 			rtcp_fb = 1;
2113 		}
2114 	}
2115 
2116 	if (rtp_session->send_rr) {
2117 		rtp_session->send_rr = 0;
2118 		rtcp_ok = 1;
2119 		force_send_rr = 1;
2120 	}
2121 
2122 	//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "TIME CHECK %d > %d\n", (int)((now - rtp_session->rtcp_last_sent) / 1000), rate);
2123 
2124 	if (!rtcp_ok && (!rtp_session->rtcp_last_sent || (int)((now - rtp_session->rtcp_last_sent) / 1000) > rate)) {
2125 		//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "TIME UP\n");
2126 		rtcp_cyclic = 1;
2127 		rtcp_ok = 1;
2128 	}
2129 
2130 	if (rtcp_ok && using_ice(rtp_session)) {
2131 		if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2132 			if (!rtp_session->ice.rready) {
2133 				rtcp_ok = 0;
2134 			}
2135 		} else {
2136 			if (!rtp_session->rtcp_ice.rready) {
2137 				rtcp_ok = 0;
2138 			}
2139 		}
2140 	}
2141 
2142 	//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "WTF %d %d %d %d\n", rate, rtp_session->rtcp_sent_packets, rtcp_ok, nack_ttl);
2143 
2144 	if (rtp_session->rtcp_sock_output && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] && rtcp_ok) {
2145 		switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
2146 		struct switch_rtcp_receiver_report *rr;
2147 		struct switch_rtcp_sender_report *sr;
2148 		struct switch_rtcp_report_block *rtcp_report_block = NULL;
2149 		switch_size_t rtcp_bytes = sizeof(struct switch_rtcp_hdr_s)+sizeof(uint32_t); /* add size of the packet header and the ssrc */
2150 		switch_rtcp_hdr_t *sdes;
2151 		uint8_t *p;
2152 		switch_size_t sdes_bytes = sizeof(struct switch_rtcp_hdr_s);
2153 		uint32_t *ssrc;
2154 		switch_rtcp_sdes_unit_t *unit;
2155 		switch_bool_t is_only_receiver = FALSE;
2156 
2157 		if (!rtcp_fb) {
2158 			rtp_session->rtcp_last_sent = now;
2159 			rtp_session->rtcp_sent_packets++;
2160 		}
2161 
2162 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
2163 				rtp_session->vb && rtcp_cyclic) {
2164 				nack_dup = rtp_session->prev_nacks_inflight;
2165 				rtp_session->prev_nacks_inflight = 0;
2166 		}
2167 
2168 		rtp_session->rtcp_send_msg.header.version = 2;
2169 		rtp_session->rtcp_send_msg.header.p = 0;
2170 
2171 		if ((switch_core_session_media_flow(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO) == SWITCH_MEDIA_FLOW_RECVONLY) ||
2172 				switch_core_session_media_flow(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO) == SWITCH_MEDIA_FLOW_RECVONLY) {
2173 			is_only_receiver = TRUE;
2174 		}
2175 		if (!rtp_session->stats.rtcp.sent_pkt_count || is_only_receiver || force_send_rr) {
2176 			rtp_session->rtcp_send_msg.header.type = _RTCP_PT_RR; /* Receiver report */
2177 			rr=(struct switch_rtcp_receiver_report*) rtp_session->rtcp_send_msg.body;
2178 			rr->ssrc = htonl(rtp_session->ssrc);
2179 			rtcp_report_block = &rr->report_block;
2180 			rtcp_bytes += sizeof(struct switch_rtcp_report_block);
2181 			rtcp_generate_report_block(rtp_session, rtcp_report_block, nack_dup);
2182 			rtp_session->rtcp_send_msg.header.count = 1; /* reception report block count */
2183 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP RR (ssrc=%u)\n", rtp_session->ssrc);
2184 		} else {
2185 			struct switch_rtcp_sender_info *rtcp_sender_info;
2186 			rtp_session->rtcp_send_msg.header.type = _RTCP_PT_SR; /* Sender report */
2187 			sr = (struct switch_rtcp_sender_report*) rtp_session->rtcp_send_msg.body;
2188 			sr->ssrc = htonl(rtp_session->ssrc);
2189 			rtcp_sender_info = &sr->sender_info;
2190 			rtcp_generate_sender_info(rtp_session, rtcp_sender_info);
2191 			rtcp_bytes += sizeof(struct switch_rtcp_sender_info);
2192 			if (!rtcp_cyclic && rtcp_fb) {
2193 				 /* rtcp-fb only, don't send receive report block */
2194 				rtp_session->rtcp_send_msg.header.count = 0;
2195 			} else {
2196 				rtcp_report_block = &sr->report_block;
2197 				rtcp_bytes += sizeof(struct switch_rtcp_report_block);
2198 				rtcp_generate_report_block(rtp_session, rtcp_report_block, nack_dup);
2199 				rtp_session->rtcp_send_msg.header.count = 1; /* reception report block count */
2200 				stats->sent_pkt_count = 0;
2201 			}
2202 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP SR (ssrc=%u)\n", rtp_session->ssrc);
2203 		}
2204 
2205 		rtp_session->rtcp_send_msg.header.length = htons((uint16_t)(rtcp_bytes / 4) - 1);
2206 
2207 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
2208 			if (rtp_session->pli_count) {
2209 				switch_rtcp_ext_hdr_t *ext_hdr;
2210 
2211 				p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2212 				ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2213 
2214 				ext_hdr->version = 2;
2215 				ext_hdr->p = 0;
2216 				ext_hdr->fmt = _RTCP_PSFB_PLI;
2217 				ext_hdr->pt = _RTCP_PT_PSFB;
2218 
2219 				ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2220 				ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc);
2221 				rtp_session->rtcp_vstats.video_in.pli_count++;
2222 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP PLI %u %u [%u]\n",
2223 								  rtp_session->ssrc, rtp_session->remote_ssrc, rtp_session->rtcp_vstats.video_in.pli_count);
2224 
2225 				ext_hdr->length = htons((uint8_t)(sizeof(switch_rtcp_ext_hdr_t) / 4) - 1);
2226 				rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t);
2227 				rtp_session->pli_count = 0;
2228 			}
2229 
2230 			if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && nack_ttl > 0) {
2231 				int n = 0;
2232 
2233 				rtp_session->rtcp_vstats.video_in.nack_count++;
2234 				for (n = 0; n < nack_ttl; n++) {
2235 					switch_rtcp_ext_hdr_t *ext_hdr;
2236 					uint32_t *nack;
2237 					p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2238 					ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2239 
2240 					ext_hdr->version = 2;
2241 					ext_hdr->p = 0;
2242 					ext_hdr->fmt = _RTCP_RTPFB_NACK;
2243 					ext_hdr->pt = _RTCP_PT_RTPFB;
2244 					ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2245 					ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc);
2246 					ext_hdr->length = htons(3);
2247 					p += sizeof(switch_rtcp_ext_hdr_t);
2248 					nack = (uint32_t *) p;
2249 					*nack = cur_nack[n];
2250 
2251 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP NACK %u [%d]\n",
2252 									  ntohs(*nack & 0xFFFF), rtp_session->rtcp_vstats.video_in.nack_count);
2253 
2254 					rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(cur_nack[n]);
2255 					cur_nack[n] = 0;
2256 				}
2257 				rtp_session->prev_nacks_inflight = n;
2258 			}
2259 
2260 			if (rtp_session->fir_count) {
2261 				switch_rtcp_ext_hdr_t *ext_hdr;
2262 				rtcp_fir_t *fir;
2263 
2264 				if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_OLD_FIR)) {
2265 					p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2266 					ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2267 
2268 					ext_hdr->version = 2;
2269 					ext_hdr->pt = _RTCP_PT_FIR;
2270 					rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t);
2271 				}
2272 
2273 
2274 				p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2275 				ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2276 
2277 				p += sizeof(switch_rtcp_ext_hdr_t);
2278 				fir = (rtcp_fir_t *) p;
2279 
2280 				ext_hdr->version = 2;
2281 				ext_hdr->p = 0;
2282 				ext_hdr->fmt = _RTCP_PSFB_FIR;
2283 				ext_hdr->pt = _RTCP_PT_PSFB;
2284 
2285 				ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2286 				ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc);
2287 
2288 				fir->ssrc = htonl(rtp_session->remote_ssrc);
2289 				fir->seq = rtp_session->fir_seq;
2290 				fir->r1 = fir->r2 = fir->r3 = 0;
2291 
2292 				rtp_session->rtcp_vstats.video_in.fir_count++;
2293 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP FIR SEQ %d [%u]\n", rtp_session->fir_seq, rtp_session->rtcp_vstats.video_in.fir_count);
2294 
2295 				rtp_session->fir_seq++;
2296 
2297 				ext_hdr->length = htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t)) / 4) - 1);
2298 				rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t);
2299 				rtp_session->fir_count = 0;
2300 			}
2301 
2302 			//if (!rtp_session->tmmbr && rtp_session->cur_tmmbr) {
2303 			//	rtp_session->tmmbr = rtp_session->cur_tmmbr;
2304 			//}
2305 
2306 			while (rtp_session->tmmbr || rtp_session->tmmbn) {
2307 				switch_rtcp_ext_hdr_t *ext_hdr;
2308 				rtcp_tmmbx_t *tmmbx;
2309 				uint32_t bps = 0;
2310 				p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2311 				ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2312 
2313 				p += sizeof(switch_rtcp_ext_hdr_t);
2314 				tmmbx = (rtcp_tmmbx_t *) p;
2315 
2316 				ext_hdr->version = 2;
2317 				ext_hdr->p = 0;
2318 				ext_hdr->pt = _RTCP_PT_RTPFB;
2319 				ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2320 				ext_hdr->recv_ssrc = 0;
2321 
2322 				if (rtp_session->tmmbr) {
2323 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP TMMBR %u\n", rtp_session->tmmbr);
2324 					ext_hdr->fmt = _RTCP_RTPFB_TMMBR;
2325 					bps = rtp_session->tmmbr;
2326 					rtp_session->tmmbr = 0;
2327 				} else {
2328 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP TMMBN %u\n", rtp_session->tmmbr);
2329 					ext_hdr->fmt = _RTCP_RTPFB_TMMBN;
2330 					bps = rtp_session->tmmbn;
2331 					rtp_session->tmmbn = 0;
2332 				}
2333 
2334 				tmmbx->ssrc = htonl(rtp_session->remote_ssrc);
2335 				calc_bw_exp(bps, 17, tmmbx);
2336 
2337 				ext_hdr->length = htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_tmmbx_t)) / 4) - 1);
2338 				rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_tmmbx_t);
2339 			}
2340 
2341 		}
2342 
2343 		//SDES + CNAME
2344 		p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2345 		sdes = (switch_rtcp_hdr_t *) p;
2346 		sdes->version = 2;
2347 		sdes->type = _RTCP_PT_SDES;
2348 		sdes->count = 1;
2349 		sdes->p = 0;
2350 		p = (uint8_t *) (sdes) + sdes_bytes;
2351 		ssrc = (uint32_t *) p;
2352 		*ssrc = htonl(rtp_session->ssrc);
2353 		sdes_bytes += sizeof(uint32_t);
2354 
2355 
2356 		p = (uint8_t *) (sdes) + sdes_bytes;
2357 		unit = (switch_rtcp_sdes_unit_t *) p;
2358 		unit->type = _RTCP_SDES_CNAME;
2359 		snprintf((char *)unit->value, 80, "%x", rtp_session->ssrc);
2360 		unit->length = strlen((char *)unit->value);
2361 		sdes_bytes += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2362 
2363 
2364 		p += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2365 		unit = (switch_rtcp_sdes_unit_t *) p;
2366 		unit->type = _RTCP_SDES_NOTE;
2367 		snprintf((char *)unit->value, 80, "FreeSWITCH.org -- Come to ClueCon.com");
2368 		unit->length = strlen((char *)unit->value);
2369 		sdes_bytes += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2370 
2371 		sdes_bytes ++;//END
2372 
2373 		sdes_bytes += 4 - (sdes_bytes % 4);
2374 
2375 		sdes->length = htons((uint16_t)(sdes_bytes / 4) - 1);
2376 		rtcp_bytes += sdes_bytes;
2377 
2378 		/* Prepare next report */
2379 		if (rtp_session->rtcp_send_msg.header.count) {
2380 			stats->last_rpt_cycle = stats->cycle;
2381 			stats->last_rpt_ext_seq = stats->high_ext_seq_recv;
2382 			stats->last_rpt_ts = rtp_session->write_timer.samplecount;
2383 			stats->period_pkt_count = 0;
2384 		}
2385 
2386 
2387 
2388 #ifdef ENABLE_SRTP
2389 		if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
2390 			int stat = 0;
2391 			int sbytes = (int) rtcp_bytes;
2392 
2393 			if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) {
2394 				stat = srtp_protect_rtcp(rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_send_msg.header, &sbytes);
2395 			} else {
2396 				stat = srtp_protect_rtcp_mki(rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_send_msg.header, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX);
2397 			}
2398 
2399 			if (stat) {
2400 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
2401 				goto end;
2402 			} else {
2403 				rtcp_bytes = sbytes;
2404 			}
2405 
2406 		}
2407 #endif
2408 
2409 #ifdef ENABLE_ZRTP
2410 		/* ZRTP Send */
2411 		if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
2412 			unsigned int sbytes = (int) rtcp_bytes;
2413 			zrtp_status_t stat = zrtp_status_fail;
2414 
2415 			stat = zrtp_process_rtcp(rtp_session->zrtp_stream, (void *) &rtp_session->rtcp_send_msg, &sbytes);
2416 
2417 			switch (stat) {
2418 			case zrtp_status_ok:
2419 				break;
2420 			case zrtp_status_drop:
2421 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
2422 				ret = (int)rtcp_bytes;
2423 				goto end;
2424 				break;
2425 			case zrtp_status_fail:
2426 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
2427 				break;
2428 			default:
2429 				break;
2430 			}
2431 
2432 			rtcp_bytes = sbytes;
2433 		}
2434 #endif
2435 		//#define DEBUG_EXTRA
2436 #ifdef DEBUG_EXTRA
2437 		{
2438 			const char *old_host;
2439 			char bufb[50];
2440 			old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr);
2441 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "%s SEND %s RTCP %s:%d %ld\n",
2442 							  rtp_session_name(rtp_session),
2443 							  rtp_type(rtp_session),
2444 							  old_host,
2445 							  switch_sockaddr_get_port(rtp_session->rtcp_remote_addr),
2446 							  rtcp_bytes);
2447 		}
2448 #endif
2449 		if (switch_socket_sendto(rtp_session->rtcp_sock_output, rtp_session->rtcp_remote_addr, 0, (void *)&rtp_session->rtcp_send_msg, &rtcp_bytes ) != SWITCH_STATUS_SUCCESS) {
2450 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,"RTCP packet not written\n");
2451 		} else {
2452 			rtp_session->stats.inbound.period_packet_count = 0;
2453 		}
2454 	}
2455 
2456 	if (rtp_session->ice.ice_user) {
2457 		if (ice_out(rtp_session, &rtp_session->ice) == SWITCH_STATUS_GENERR) {
2458 			ret = -1;
2459 			goto end;
2460 		}
2461 	}
2462 
2463 	if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2464 		if (rtp_session->rtcp_ice.ice_user) {
2465 			if (ice_out(rtp_session, &rtp_session->rtcp_ice) == SWITCH_STATUS_GENERR) {
2466 				ret = -1;
2467 				goto end;
2468 			}
2469 		}
2470 	}
2471 
2472  end:
2473 
2474 	return ret;
2475 }
2476 
switch_rtp_ping(switch_rtp_t * rtp_session)2477 SWITCH_DECLARE(void) switch_rtp_ping(switch_rtp_t *rtp_session)
2478 {
2479 	check_rtcp_and_ice(rtp_session);
2480 }
2481 
switch_rtp_get_random(void * buf,uint32_t len)2482 SWITCH_DECLARE(void) switch_rtp_get_random(void *buf, uint32_t len)
2483 {
2484 #ifdef HAVE_OPENSSL
2485 	RAND_bytes(buf, len);
2486 #else
2487 	switch_stun_random_string(buf, len, NULL);
2488 #endif
2489 }
2490 
2491 
switch_rtp_shutdown(void)2492 SWITCH_DECLARE(void) switch_rtp_shutdown(void)
2493 {
2494 	switch_core_port_allocator_t *alloc = NULL;
2495 	switch_hash_index_t *hi;
2496 	const void *var;
2497 	void *val;
2498 
2499 	if (!global_init) {
2500 		return;
2501 	}
2502 
2503 	switch_mutex_lock(port_lock);
2504 
2505 	for (hi = switch_core_hash_first(alloc_hash); hi; hi = switch_core_hash_next(&hi)) {
2506 		switch_core_hash_this(hi, &var, NULL, &val);
2507 		if ((alloc = (switch_core_port_allocator_t *) val)) {
2508 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Destroy port allocator for %s\n", (char *) var);
2509 			switch_core_port_allocator_destroy(&alloc);
2510 		}
2511 	}
2512 
2513 	switch_core_hash_destroy(&alloc_hash);
2514 	switch_mutex_unlock(port_lock);
2515 
2516 #ifdef ENABLE_ZRTP
2517 	if (zrtp_on) {
2518 		zrtp_status_t status = zrtp_status_ok;
2519 
2520 		status = zrtp_def_cache_store(zrtp_global);
2521 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Saving ZRTP cache: %s\n", zrtp_status_ok == status ? "OK" : "FAIL");
2522 		zrtp_down(zrtp_global);
2523 	}
2524 #endif
2525 #ifdef ENABLE_SRTP
2526 	srtp_crypto_kernel_shutdown();
2527 #endif
2528 
2529 }
2530 
switch_rtp_set_start_port(switch_port_t port)2531 SWITCH_DECLARE(switch_port_t) switch_rtp_set_start_port(switch_port_t port)
2532 {
2533 	if (port) {
2534 		if (port_lock) {
2535 			switch_mutex_lock(port_lock);
2536 		}
2537 		START_PORT = port;
2538 		if (port_lock) {
2539 			switch_mutex_unlock(port_lock);
2540 		}
2541 	}
2542 	return START_PORT;
2543 }
2544 
switch_rtp_set_end_port(switch_port_t port)2545 SWITCH_DECLARE(switch_port_t) switch_rtp_set_end_port(switch_port_t port)
2546 {
2547 	if (port) {
2548 		if (port_lock) {
2549 			switch_mutex_lock(port_lock);
2550 		}
2551 		END_PORT = port;
2552 		if (port_lock) {
2553 			switch_mutex_unlock(port_lock);
2554 		}
2555 	}
2556 	return END_PORT;
2557 }
2558 
switch_rtp_release_port(const char * ip,switch_port_t port)2559 SWITCH_DECLARE(void) switch_rtp_release_port(const char *ip, switch_port_t port)
2560 {
2561 	switch_core_port_allocator_t *alloc = NULL;
2562 
2563 	if (!ip || !port) {
2564 		return;
2565 	}
2566 
2567 	switch_mutex_lock(port_lock);
2568 	if ((alloc = switch_core_hash_find(alloc_hash, ip))) {
2569 		switch_core_port_allocator_free_port(alloc, port);
2570 	}
2571 	switch_mutex_unlock(port_lock);
2572 
2573 }
2574 
switch_rtp_request_port(const char * ip)2575 SWITCH_DECLARE(switch_port_t) switch_rtp_request_port(const char *ip)
2576 {
2577 	switch_port_t port = 0;
2578 	switch_core_port_allocator_t *alloc = NULL;
2579 
2580 	switch_mutex_lock(port_lock);
2581 	alloc = switch_core_hash_find(alloc_hash, ip);
2582 	if (!alloc) {
2583 		if (switch_core_port_allocator_new(ip, START_PORT, END_PORT, SPF_EVEN, &alloc) != SWITCH_STATUS_SUCCESS) {
2584 			abort();
2585 		}
2586 
2587 		switch_core_hash_insert(alloc_hash, ip, alloc);
2588 	}
2589 
2590 	if (switch_core_port_allocator_request_port(alloc, &port) != SWITCH_STATUS_SUCCESS) {
2591 		port = 0;
2592 	}
2593 
2594 	switch_mutex_unlock(port_lock);
2595 	return port;
2596 }
2597 
switch_rtp_set_payload_map(switch_rtp_t * rtp_session,payload_map_t ** pmap)2598 SWITCH_DECLARE(switch_status_t) switch_rtp_set_payload_map(switch_rtp_t *rtp_session, payload_map_t **pmap)
2599 {
2600 
2601 	if (rtp_session) {
2602 		switch_mutex_lock(rtp_session->flag_mutex);
2603 		rtp_session->pmaps = pmap;
2604 		switch_mutex_unlock(rtp_session->flag_mutex);
2605 		return SWITCH_STATUS_SUCCESS;
2606 	}
2607 
2608 	return SWITCH_STATUS_FALSE;
2609 }
2610 
switch_rtp_intentional_bugs(switch_rtp_t * rtp_session,switch_rtp_bug_flag_t bugs)2611 SWITCH_DECLARE(void) switch_rtp_intentional_bugs(switch_rtp_t *rtp_session, switch_rtp_bug_flag_t bugs)
2612 {
2613 	rtp_session->rtp_bugs = bugs;
2614 
2615 	if ((rtp_session->rtp_bugs & RTP_BUG_START_SEQ_AT_ZERO)) {
2616 		rtp_session->seq = 0;
2617 	}
2618 
2619 }
2620 
2621 
enable_remote_rtcp_socket(switch_rtp_t * rtp_session,const char ** err)2622 static switch_status_t enable_remote_rtcp_socket(switch_rtp_t *rtp_session, const char **err) {
2623 
2624 	switch_status_t status = SWITCH_STATUS_SUCCESS;
2625 
2626 	if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2627 
2628 		if (switch_sockaddr_info_get(&rtp_session->rtcp_remote_addr, rtp_session->eff_remote_host_str, SWITCH_UNSPEC,
2629 									 rtp_session->remote_rtcp_port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !rtp_session->rtcp_remote_addr) {
2630 			*err = "RTCP Remote Address Error!";
2631 			return SWITCH_STATUS_FALSE;
2632 		} else {
2633 			const char *host;
2634 			char bufa[50];
2635 
2636 			host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_remote_addr);
2637 
2638 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,
2639 							  "Setting RTCP remote addr to %s:%d %d\n", host, rtp_session->remote_rtcp_port, rtp_session->rtcp_remote_addr->family);
2640 		}
2641 
2642 		if (rtp_session->rtcp_sock_input && switch_sockaddr_get_family(rtp_session->rtcp_remote_addr) ==
2643 			switch_sockaddr_get_family(rtp_session->rtcp_local_addr)) {
2644 			rtp_session->rtcp_sock_output = rtp_session->rtcp_sock_input;
2645 		} else {
2646 
2647 			if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) {
2648 				switch_socket_close(rtp_session->rtcp_sock_output);
2649 			}
2650 
2651 			if ((status = switch_socket_create(&rtp_session->rtcp_sock_output,
2652 											   switch_sockaddr_get_family(rtp_session->rtcp_remote_addr),
2653 											   SOCK_DGRAM, 0, rtp_session->pool)) != SWITCH_STATUS_SUCCESS) {
2654 				*err = "RTCP Socket Error!";
2655 			}
2656 		}
2657 
2658 	} else {
2659 		*err = "RTCP NOT ACTIVE!";
2660 	}
2661 
2662 	return status;
2663 
2664 }
2665 
enable_local_rtcp_socket(switch_rtp_t * rtp_session,const char ** err)2666 static switch_status_t enable_local_rtcp_socket(switch_rtp_t *rtp_session, const char **err) {
2667 
2668 	const char *host = rtp_session->local_host_str;
2669 	switch_port_t port = rtp_session->local_port;
2670 	switch_socket_t *rtcp_new_sock = NULL, *rtcp_old_sock = NULL;
2671 	switch_status_t status = SWITCH_STATUS_SUCCESS;
2672 	char bufa[50];
2673 
2674 	if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2675 		if (switch_sockaddr_info_get(&rtp_session->rtcp_local_addr, host, SWITCH_UNSPEC, port+1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2676 			*err = "RTCP Local Address Error!";
2677 			goto done;
2678 		}
2679 
2680 		if (switch_socket_create(&rtcp_new_sock, switch_sockaddr_get_family(rtp_session->rtcp_local_addr), SOCK_DGRAM, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2681 			*err = "RTCP Socket Error!";
2682 			goto done;
2683 		}
2684 
2685 		if (switch_socket_opt_set(rtcp_new_sock, SWITCH_SO_REUSEADDR, 1) != SWITCH_STATUS_SUCCESS) {
2686 			*err = "RTCP Socket Error!";
2687 			goto done;
2688 		}
2689 
2690 		if (switch_socket_bind(rtcp_new_sock, rtp_session->rtcp_local_addr) != SWITCH_STATUS_SUCCESS) {
2691 			*err = "RTCP Bind Error!";
2692 			goto done;
2693 		}
2694 
2695 		if (switch_sockaddr_info_get(&rtp_session->rtcp_from_addr, switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr),
2696 											 SWITCH_UNSPEC, switch_sockaddr_get_port(rtp_session->from_addr) + 1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2697 			*err = "RTCP From Address Error!";
2698 			goto done;
2699 		}
2700 
2701 		rtcp_old_sock = rtp_session->rtcp_sock_input;
2702 		rtp_session->rtcp_sock_input = rtcp_new_sock;
2703 		rtcp_new_sock = NULL;
2704 
2705 		switch_socket_create_pollset(&rtp_session->rtcp_read_pollfd, rtp_session->rtcp_sock_input, SWITCH_POLLIN | SWITCH_POLLERR, rtp_session->pool);
2706 
2707  done:
2708 
2709 		if (*err) {
2710 
2711 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating rtcp [%s]\n", *err);
2712 			status = SWITCH_STATUS_FALSE;
2713 		}
2714 
2715 		if (rtcp_new_sock) {
2716 			switch_socket_close(rtcp_new_sock);
2717 		}
2718 
2719 		if (rtcp_old_sock) {
2720 			switch_socket_close(rtcp_old_sock);
2721 		}
2722 	} else {
2723 		status = SWITCH_STATUS_FALSE;
2724 	}
2725 
2726 	return status;
2727 }
2728 
switch_rtp_set_local_address(switch_rtp_t * rtp_session,const char * host,switch_port_t port,const char ** err)2729 SWITCH_DECLARE(switch_status_t) switch_rtp_set_local_address(switch_rtp_t *rtp_session, const char *host, switch_port_t port, const char **err)
2730 {
2731 	switch_socket_t *new_sock = NULL, *old_sock = NULL;
2732 	switch_status_t status = SWITCH_STATUS_FALSE;
2733 	int j = 0;
2734 #ifndef WIN32
2735 	char o[5] = "TEST", i[5] = "";
2736 	switch_size_t len, ilen = 0;
2737 	int x;
2738 #endif
2739 
2740 	if (rtp_session->ready != 1) {
2741 		if (!switch_rtp_ready(rtp_session)) {
2742 			return SWITCH_STATUS_FALSE;
2743 		}
2744 
2745 		READ_INC(rtp_session);
2746 		WRITE_INC(rtp_session);
2747 
2748 		if (!switch_rtp_ready(rtp_session)) {
2749 			goto done;
2750 		}
2751 	}
2752 
2753 
2754 	*err = NULL;
2755 
2756 	if (zstr(host) || !port) {
2757 		*err = "Address Error";
2758 		goto done;
2759 	}
2760 
2761 
2762 	rtp_session->local_host_str = switch_core_strdup(rtp_session->pool, host);
2763 	rtp_session->local_port = port;
2764 
2765 
2766 	if (switch_sockaddr_info_get(&rtp_session->local_addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2767 		*err = "Local Address Error!";
2768 		goto done;
2769 	}
2770 
2771 
2772 	if (rtp_session->sock_input) {
2773 		switch_rtp_kill_socket(rtp_session);
2774 	}
2775 
2776 	if (switch_socket_create(&new_sock, switch_sockaddr_get_family(rtp_session->local_addr), SOCK_DGRAM, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2777 		*err = "Socket Error!";
2778 		goto done;
2779 	}
2780 
2781 	if (switch_socket_opt_set(new_sock, SWITCH_SO_REUSEADDR, 1) != SWITCH_STATUS_SUCCESS) {
2782 		*err = "Socket Error!";
2783 		goto done;
2784 	}
2785 
2786 	//if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
2787 	//	switch_socket_opt_set(new_sock, SWITCH_SO_RCVBUF, 1572864);
2788 	//	switch_socket_opt_set(new_sock, SWITCH_SO_SNDBUF, 1572864);
2789 	//}
2790 
2791 	if (switch_socket_bind(new_sock, rtp_session->local_addr) != SWITCH_STATUS_SUCCESS) {
2792 		char *em = switch_core_sprintf(rtp_session->pool, "Bind Error! %s:%d", host, port);
2793 		*err = em;
2794 		goto done;
2795 	}
2796 
2797 
2798 	if ((j = atoi(host)) && j > 223 && j < 240) { /* mcast */
2799 		if (switch_mcast_interface(new_sock, rtp_session->local_addr) != SWITCH_STATUS_SUCCESS) {
2800 			*err = "Multicast Socket interface Error";
2801 			goto done;
2802 		}
2803 
2804 		if (switch_mcast_join(new_sock, rtp_session->local_addr, NULL, NULL) != SWITCH_STATUS_SUCCESS) {
2805 			*err = "Multicast Error";
2806 			goto done;
2807 		}
2808 
2809 		if (rtp_session->session) {
2810 			switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
2811 			const char *var;
2812 
2813 			if ((var = switch_channel_get_variable(channel, "multicast_ttl"))) {
2814 				int ttl = atoi(var);
2815 
2816 				if (ttl > 0 && ttl < 256) {
2817 					if (switch_mcast_hops(new_sock, (uint8_t) ttl) != SWITCH_STATUS_SUCCESS) {
2818 						*err = "Mutlicast TTL set failed";
2819 						goto done;
2820 					}
2821 
2822 				}
2823 			}
2824 
2825 		}
2826 
2827 	}
2828 
2829 
2830 
2831 #ifndef WIN32
2832 	len = sizeof(i);
2833 	switch_socket_opt_set(new_sock, SWITCH_SO_NONBLOCK, TRUE);
2834 
2835 	switch_socket_sendto(new_sock, rtp_session->local_addr, 0, (void *) o, &len);
2836 
2837 	x = 0;
2838 	while (!ilen) {
2839 		switch_status_t status;
2840 		ilen = len;
2841 		status = switch_socket_recvfrom(rtp_session->from_addr, new_sock, 0, (void *) i, &ilen);
2842 
2843 		if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) {
2844 			break;
2845 		}
2846 
2847 		if (++x > 1000) {
2848 			break;
2849 		}
2850 		switch_cond_next();
2851 	}
2852 	switch_socket_opt_set(new_sock, SWITCH_SO_NONBLOCK, FALSE);
2853 
2854 #endif
2855 
2856 	old_sock = rtp_session->sock_input;
2857 	rtp_session->sock_input = new_sock;
2858 	new_sock = NULL;
2859 
2860 	if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] || rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] || rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
2861 		switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK, TRUE);
2862 		switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
2863 	}
2864 
2865 	switch_socket_create_pollset(&rtp_session->read_pollfd, rtp_session->sock_input, SWITCH_POLLIN | SWITCH_POLLERR, rtp_session->pool);
2866 
2867 	if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2868 		if ((status = enable_local_rtcp_socket(rtp_session, err)) == SWITCH_STATUS_SUCCESS) {
2869 			*err = "Success";
2870 		}
2871 	} else {
2872 		status = SWITCH_STATUS_SUCCESS;
2873 		*err = "Success";
2874 	}
2875 
2876 	switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_IO);
2877 
2878  done:
2879 
2880 	if (new_sock) {
2881 		switch_socket_close(new_sock);
2882 	}
2883 
2884 	if (old_sock) {
2885 		switch_socket_close(old_sock);
2886 	}
2887 
2888 
2889 	if (rtp_session->ready != 1) {
2890 		WRITE_DEC(rtp_session);
2891 		READ_DEC(rtp_session);
2892 	}
2893 
2894 	return status;
2895 }
2896 
switch_rtp_set_media_timeout(switch_rtp_t * rtp_session,uint32_t ms)2897 SWITCH_DECLARE(void) switch_rtp_set_media_timeout(switch_rtp_t *rtp_session, uint32_t ms)
2898 {
2899 	if (!switch_rtp_ready(rtp_session) || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
2900 		return;
2901 	}
2902 
2903 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1,
2904 					  "%s MEDIA TIMEOUT %s set to %u", switch_core_session_get_name(rtp_session->session), rtp_type(rtp_session), ms);
2905 	rtp_session->media_timeout = ms;
2906 	switch_rtp_reset_media_timer(rtp_session);
2907 }
2908 
switch_rtp_set_max_missed_packets(switch_rtp_t * rtp_session,uint32_t max)2909 SWITCH_DECLARE(void) switch_rtp_set_max_missed_packets(switch_rtp_t *rtp_session, uint32_t max)
2910 {
2911 	if (!switch_rtp_ready(rtp_session) || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
2912 		return;
2913 	}
2914 
2915 	if (rtp_session->missed_count >= max) {
2916 
2917 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING,
2918 						  "new max missed packets(%d->%d) greater than current missed packets(%d). RTP will timeout.\n",
2919 						  rtp_session->missed_count, max, rtp_session->missed_count);
2920 	}
2921 
2922 	rtp_session->max_missed_packets = max;
2923 }
2924 
switch_rtp_reset_jb(switch_rtp_t * rtp_session)2925 SWITCH_DECLARE(void) switch_rtp_reset_jb(switch_rtp_t *rtp_session)
2926 {
2927 	if (switch_rtp_ready(rtp_session)) {
2928 		if (rtp_session->jb) {
2929 			switch_jb_reset(rtp_session->jb);
2930 		}
2931 	}
2932 }
2933 
switch_rtp_reset_vb(switch_rtp_t * rtp_session)2934 SWITCH_DECLARE(void) switch_rtp_reset_vb(switch_rtp_t *rtp_session)
2935 {
2936 
2937 	if (rtp_session->vb) {
2938 		switch_jb_reset(rtp_session->vb);
2939 	}
2940 
2941 	if (rtp_session->vbw) {
2942 		switch_jb_reset(rtp_session->vbw);
2943 	}
2944 }
2945 
switch_rtp_reset(switch_rtp_t * rtp_session)2946 SWITCH_DECLARE(void) switch_rtp_reset(switch_rtp_t *rtp_session)
2947 {
2948 	if (!rtp_session) {
2949 		return;
2950 	}
2951 
2952 	rtp_session->seq = (uint16_t) rand();
2953 	rtp_session->ts = 0;
2954 	memset(&rtp_session->ts_norm, 0, sizeof(rtp_session->ts_norm));
2955 
2956 	rtp_session->last_stun = rtp_session->first_stun = 0;
2957 	rtp_session->wrong_addrs = 0;
2958 	rtp_session->rtcp_sent_packets = 0;
2959 	rtp_session->rtcp_last_sent = 0;
2960 	rtp_session->ice_adj = 0;
2961 
2962 	//switch_rtp_del_dtls(rtp_session, DTLS_TYPE_RTP|DTLS_TYPE_RTCP);
2963 	switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_PAUSE);
2964 	switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_MUTE);
2965 	rtcp_stats_init(rtp_session);
2966 
2967 	if (rtp_session->ice.ready) {
2968 		switch_rtp_reset_vb(rtp_session);
2969 		rtp_session->ice.ready = rtp_session->ice.rready = 0;
2970 	}
2971 
2972 }
2973 
switch_rtp_reset_media_timer(switch_rtp_t * rtp_session)2974 SWITCH_DECLARE(void) switch_rtp_reset_media_timer(switch_rtp_t *rtp_session)
2975 {
2976 	rtp_session->missed_count = 0;
2977 	rtp_session->last_media = switch_micro_time_now();
2978 }
2979 
switch_rtp_get_remote_host(switch_rtp_t * rtp_session)2980 SWITCH_DECLARE(char *) switch_rtp_get_remote_host(switch_rtp_t *rtp_session)
2981 {
2982 	return zstr(rtp_session->remote_host_str) ? "0.0.0.0" : rtp_session->remote_host_str;
2983 }
2984 
switch_rtp_get_remote_port(switch_rtp_t * rtp_session)2985 SWITCH_DECLARE(switch_port_t) switch_rtp_get_remote_port(switch_rtp_t *rtp_session)
2986 {
2987 	return rtp_session->remote_port;
2988 }
2989 
ping_socket(switch_rtp_t * rtp_session)2990 static void ping_socket(switch_rtp_t *rtp_session)
2991 {
2992 	uint32_t o = UINT_MAX;
2993 	switch_size_t len = sizeof(o);
2994 	switch_socket_sendto(rtp_session->sock_input, rtp_session->local_addr, 0, (void *) &o, &len);
2995 
2996 	if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && rtp_session->rtcp_sock_input) {
2997 		switch_socket_sendto(rtp_session->rtcp_sock_input, rtp_session->rtcp_local_addr, 0, (void *) &o, &len);
2998 	}
2999 }
3000 
switch_rtp_udptl_mode(switch_rtp_t * rtp_session)3001 SWITCH_DECLARE(switch_status_t) switch_rtp_udptl_mode(switch_rtp_t *rtp_session)
3002 {
3003 	switch_socket_t *sock;
3004 
3005 	if (!switch_rtp_ready(rtp_session)) {
3006 		return SWITCH_STATUS_FALSE;
3007 	}
3008 
3009 	if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_PROXY_MEDIA)) {
3010 		ping_socket(rtp_session);
3011 	}
3012 
3013 	READ_INC(rtp_session);
3014 	WRITE_INC(rtp_session);
3015 
3016 	if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] || rtp_session->timer.timer_interface) {
3017 		switch_core_timer_destroy(&rtp_session->timer);
3018 		memset(&rtp_session->timer, 0, sizeof(rtp_session->timer));
3019 		switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER);
3020 	}
3021 
3022 	rtp_session->missed_count = 0;
3023 	rtp_session->max_missed_packets = 0;
3024 
3025 	rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] = 0;
3026 
3027 	if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
3028 		rtp_session->rtcp_sock_input = NULL;
3029 		rtp_session->rtcp_sock_output = NULL;
3030 	} else {
3031 		if (rtp_session->rtcp_sock_input && rtp_session->rtcp_sock_input != rtp_session->sock_input) {
3032 			ping_socket(rtp_session);
3033 			switch_socket_shutdown(rtp_session->rtcp_sock_input, SWITCH_SHUTDOWN_READWRITE);
3034 		}
3035 
3036 		if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input &&
3037 			rtp_session->rtcp_sock_output != rtp_session->sock_input) {
3038 			switch_socket_shutdown(rtp_session->rtcp_sock_output, SWITCH_SHUTDOWN_READWRITE);
3039 		}
3040 
3041 		if ((sock = rtp_session->rtcp_sock_input)) {
3042 			rtp_session->rtcp_sock_input = NULL;
3043 			switch_socket_close(sock);
3044 
3045 			if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != sock) {
3046 				sock = rtp_session->rtcp_sock_output;
3047 				rtp_session->rtcp_sock_output = NULL;
3048 				switch_socket_close(sock);
3049 			}
3050 		}
3051 	}
3052 
3053 	switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_UDPTL);
3054 	switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_PROXY_MEDIA);
3055 	switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK, FALSE);
3056 	switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
3057 
3058 	WRITE_DEC(rtp_session);
3059 	READ_DEC(rtp_session);
3060 
3061 	switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_STICKY_FLUSH);
3062 	switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
3063 
3064 	switch_rtp_break(rtp_session);
3065 
3066 	return SWITCH_STATUS_SUCCESS;
3067 
3068 }
3069 
3070 
switch_rtp_set_remote_address(switch_rtp_t * rtp_session,const char * host,switch_port_t port,switch_port_t remote_rtcp_port,switch_bool_t change_adv_addr,const char ** err)3071 SWITCH_DECLARE(switch_status_t) switch_rtp_set_remote_address(switch_rtp_t *rtp_session, const char *host, switch_port_t port, switch_port_t remote_rtcp_port,
3072 															  switch_bool_t change_adv_addr, const char **err)
3073 {
3074 	switch_sockaddr_t *remote_addr;
3075 	switch_status_t status = SWITCH_STATUS_SUCCESS;
3076 	*err = "Success";
3077 
3078 	if (switch_sockaddr_info_get(&remote_addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !remote_addr) {
3079 		*err = "Remote Address Error!";
3080 		return SWITCH_STATUS_FALSE;
3081 	}
3082 
3083 
3084 	switch_mutex_lock(rtp_session->write_mutex);
3085 
3086 	rtp_session->remote_addr = remote_addr;
3087 
3088 	if (change_adv_addr) {
3089 		rtp_session->remote_host_str = switch_core_strdup(rtp_session->pool, host);
3090 		rtp_session->remote_port = port;
3091 	}
3092 
3093 	rtp_session->eff_remote_host_str = switch_core_strdup(rtp_session->pool, host);
3094 	rtp_session->eff_remote_port = port;
3095 
3096 	if (rtp_session->sock_input && switch_sockaddr_get_family(rtp_session->remote_addr) == switch_sockaddr_get_family(rtp_session->local_addr)) {
3097 		rtp_session->sock_output = rtp_session->sock_input;
3098 	} else {
3099 		if (rtp_session->sock_output && rtp_session->sock_output != rtp_session->sock_input) {
3100 			switch_socket_close(rtp_session->sock_output);
3101 		}
3102 		if ((status = switch_socket_create(&rtp_session->sock_output,
3103 										   switch_sockaddr_get_family(rtp_session->remote_addr),
3104 										   SOCK_DGRAM, 0, rtp_session->pool)) != SWITCH_STATUS_SUCCESS) {
3105 
3106 			*err = "Socket Error!";
3107 		}
3108 	}
3109 
3110 	if (rtp_session->dtls) {
3111 		rtp_session->dtls->sock_output = rtp_session->sock_output;
3112 
3113 		if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
3114 			switch_sockaddr_info_get(&rtp_session->dtls->remote_addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool);
3115 		}
3116 	}
3117 
3118 
3119 	if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
3120 		if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
3121 			rtp_session->rtcp_remote_addr = rtp_session->remote_addr;
3122 			rtp_session->rtcp_sock_output = rtp_session->sock_output;
3123 		}/* else {
3124 			if (remote_rtcp_port) {
3125 				rtp_session->remote_rtcp_port = remote_rtcp_port;
3126 			} else {
3127 				rtp_session->remote_rtcp_port = rtp_session->eff_remote_port + 1;
3128 			}
3129 			status = enable_remote_rtcp_socket(rtp_session, err);
3130 
3131 			if (rtp_session->rtcp_dtls) {
3132 				//switch_sockaddr_info_get(&rtp_session->rtcp_dtls->remote_addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool);
3133 				rtp_session->rtcp_dtls->remote_addr = rtp_session->rtcp_remote_addr;
3134 				rtp_session->rtcp_dtls->sock_output = rtp_session->rtcp_sock_output;
3135 			}
3136 			}*/
3137 	}
3138 
3139 	switch_mutex_unlock(rtp_session->write_mutex);
3140 
3141 	return status;
3142 }
3143 
3144 
3145 static const char *dtls_state_names_t[] = {"OFF", "HANDSHAKE", "SETUP", "READY", "FAIL", "INVALID"};
dtls_state_names(dtls_state_t s)3146 static const char *dtls_state_names(dtls_state_t s)
3147 {
3148 	if (s > DS_INVALID) {
3149 		s = DS_INVALID;
3150 	}
3151 
3152 	return dtls_state_names_t[s];
3153 }
3154 
3155 #define dtls_set_state(_dtls, _state) switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n", rtp_type(rtp_session), dtls_state_names(_dtls->state), dtls_state_names(_state)); _dtls->new_state = 1; _dtls->last_state = _dtls->state; _dtls->state = _state
3156 
3157 #define cr_keylen 16
3158 #define cr_saltlen 14
3159 #define cr_kslen 30
3160 
dtls_state_setup(switch_rtp_t * rtp_session,switch_dtls_t * dtls)3161 static int dtls_state_setup(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
3162 {
3163 	X509 *cert;
3164 	switch_secure_settings_t	ssec;	/* Used just to wrap over params in a call to switch_rtp_add_crypto_key. */
3165 	int r = 0;
3166 
3167 	uint8_t raw_key_data[cr_kslen * 2];
3168 	unsigned char local_key_buf[cr_kslen];
3169 	unsigned char remote_key_buf[cr_kslen];
3170 
3171 	memset(&ssec, 0, sizeof(ssec));
3172 	memset(&raw_key_data, 0, cr_kslen * 2 * sizeof(uint8_t));
3173 	memset(&local_key_buf, 0, cr_kslen * sizeof(unsigned char));
3174 	memset(&remote_key_buf, 0, cr_kslen * sizeof(unsigned char));
3175 
3176 	if ((dtls->type & DTLS_TYPE_SERVER)) {
3177 		r = 1;
3178 	} else if ((cert = SSL_get_peer_certificate(dtls->ssl))) {
3179 		switch_core_cert_extract_fingerprint(cert, dtls->remote_fp);
3180 		r = switch_core_cert_verify(dtls->remote_fp);
3181 		X509_free(cert);
3182 	}
3183 
3184 	if (!r) {
3185 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s Fingerprint Verification Failed!\n", rtp_type(rtp_session));
3186 		dtls_set_state(dtls, DS_FAIL);
3187 		return -1;
3188 	} else {
3189 		unsigned char *local_key, *remote_key, *local_salt, *remote_salt;
3190 
3191 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "%s Fingerprint Verified.\n", rtp_type(rtp_session));
3192 
3193 #ifdef HAVE_OPENSSL_DTLS_SRTP
3194 		if (!SSL_export_keying_material(dtls->ssl, raw_key_data, sizeof(raw_key_data), "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) {
3195 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s Key material export failure\n", rtp_type(rtp_session));
3196 			dtls_set_state(dtls, DS_FAIL);
3197 			return -1;
3198 		}
3199 #else
3200 		return -1;
3201 #endif
3202 
3203 		if ((dtls->type & DTLS_TYPE_CLIENT)) {
3204 			local_key = raw_key_data;
3205 			remote_key = local_key + cr_keylen;
3206 			local_salt = remote_key + cr_keylen;
3207 			remote_salt = local_salt + cr_saltlen;
3208 
3209 		} else {
3210 			remote_key = raw_key_data;
3211 			local_key = remote_key + cr_keylen;
3212 			remote_salt = local_key + cr_keylen;
3213 			local_salt = remote_salt + cr_saltlen;
3214 		}
3215 
3216 		memcpy(ssec.local_raw_key, local_key, cr_keylen);
3217 		memcpy(ssec.local_raw_key + cr_keylen, local_salt, cr_saltlen);
3218 
3219 		memcpy(ssec.remote_raw_key, remote_key, cr_keylen);
3220 		memcpy(ssec.remote_raw_key + cr_keylen, remote_salt, cr_saltlen);
3221 
3222 		ssec.crypto_type = AES_CM_128_HMAC_SHA1_80;
3223 
3224 		if (dtls == rtp_session->rtcp_dtls && rtp_session->rtcp_dtls != rtp_session->dtls) {
3225 			switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_SEND_RTCP, 0, &ssec);
3226 			switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_RECV_RTCP, 0, &ssec);
3227 		} else {
3228 			switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_SEND, 0, &ssec);
3229 			switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_RECV, 0, &ssec);
3230 		}
3231 	}
3232 
3233 	dtls_set_state(dtls, DS_READY);
3234 
3235 	return 0;
3236 }
3237 
dtls_state_ready(switch_rtp_t * rtp_session,switch_dtls_t * dtls)3238 static int dtls_state_ready(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
3239 {
3240 
3241 	if (dtls->new_state) {
3242 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
3243 			switch_core_session_t *other_session;
3244 
3245 			if (rtp_session->session && switch_core_session_get_partner(rtp_session->session, &other_session) == SWITCH_STATUS_SUCCESS) {
3246 				switch_core_session_request_video_refresh(other_session);
3247 				switch_core_session_rwunlock(other_session);
3248 			}
3249 		}
3250 		dtls->new_state = 0;
3251 	}
3252 	return 0;
3253 }
3254 
dtls_state_fail(switch_rtp_t * rtp_session,switch_dtls_t * dtls)3255 static int dtls_state_fail(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
3256 {
3257 	if (rtp_session->session) {
3258 		switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
3259 		switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
3260 	}
3261 
3262 	return -1;
3263 }
3264 
3265 
dtls_state_handshake(switch_rtp_t * rtp_session,switch_dtls_t * dtls)3266 static int dtls_state_handshake(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
3267 {
3268 	int ret;
3269 
3270 	if ((ret = SSL_do_handshake(dtls->ssl)) != 1){
3271 		switch((ret = SSL_get_error(dtls->ssl, ret))){
3272 		case SSL_ERROR_WANT_READ:
3273 		case SSL_ERROR_WANT_WRITE:
3274 		case SSL_ERROR_NONE:
3275 			break;
3276 		default:
3277 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s Handshake failure %d. This may happen when you use legacy DTLS v1.0 (legacyDTLS channel var is set) but endpoint requires DTLS v1.2.\n", rtp_type(rtp_session), ret);
3278 			dtls_set_state(dtls, DS_FAIL);
3279 			return -1;
3280 		}
3281 	}
3282 
3283 	if (SSL_is_init_finished(dtls->ssl)) {
3284 		dtls_set_state(dtls, DS_SETUP);
3285 	}
3286 
3287 	return 0;
3288 }
3289 
free_dtls(switch_dtls_t ** dtlsp)3290 static void free_dtls(switch_dtls_t **dtlsp)
3291 {
3292 	switch_dtls_t *dtls;
3293 
3294 	if (!dtlsp) {
3295 		return;
3296 	}
3297 
3298 	dtls = *dtlsp;
3299 	*dtlsp = NULL;
3300 
3301 	if (dtls->ssl) {
3302 		SSL_free(dtls->ssl);
3303 	}
3304 
3305 	if (dtls->ssl_ctx) {
3306 		SSL_CTX_free(dtls->ssl_ctx);
3307 	}
3308 }
3309 
do_dtls(switch_rtp_t * rtp_session,switch_dtls_t * dtls)3310 static int do_dtls(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
3311 {
3312 	int r = 0, ret = 0, len;
3313 	switch_size_t bytes;
3314 	unsigned char buf[MAX_DTLS_MTU] = "";
3315 	int ready = rtp_session->ice.ice_user ? (rtp_session->ice.rready && rtp_session->ice.ready) : 1;
3316 	int pending;
3317 
3318 	if (!dtls->bytes && !ready) {
3319 		return 0;
3320 	}
3321 
3322 	if (dtls->bytes > 0 && dtls->data) {
3323 		ret = BIO_write(dtls->read_bio, dtls->data, (int)dtls->bytes);
3324 		if (ret <= 0) {
3325 			ret = SSL_get_error(dtls->ssl, ret);
3326 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet decode err: SSL err %d\n", rtp_type(rtp_session), ret);
3327 		} else if (ret != (int)dtls->bytes) {
3328 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet decode err: read %d bytes instead of %d\n", rtp_type(rtp_session), ret, (int)dtls->bytes);
3329 		}
3330 	}
3331 
3332 	if (dtls_states[dtls->state]) {
3333 		r = dtls_states[dtls->state](rtp_session, dtls);
3334 	}
3335 
3336 	while ((pending = BIO_ctrl_pending(dtls->filter_bio)) > 0) {
3337 		switch_assert(pending <= sizeof(buf));
3338 
3339 		len = BIO_read(dtls->write_bio, buf, pending);
3340 		if (len > 0) {
3341 			bytes = len;
3342 			ret = switch_socket_sendto(dtls->sock_output, dtls->remote_addr, 0, (void *)buf, &bytes);
3343 
3344 			if (ret != SWITCH_STATUS_SUCCESS) {
3345 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet not written to socket: %d\n", rtp_type(rtp_session), ret);
3346 			} else if (bytes != len) {
3347 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet write err: written %d bytes instead of %d\n", rtp_type(rtp_session), (int)bytes, len);
3348 			}
3349 		} else {
3350 			ret = SSL_get_error(dtls->ssl, len);
3351 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet encode err: SSL err %d\n", rtp_type(rtp_session), ret);
3352 		}
3353 	}
3354 
3355 	return r;
3356 }
3357 
3358 #if VERIFY
cb_verify_peer(int preverify_ok,X509_STORE_CTX * ctx)3359 static int cb_verify_peer(int preverify_ok, X509_STORE_CTX *ctx)
3360 {
3361 	SSL *ssl = NULL;
3362 	switch_dtls_t *dtls;
3363 	X509 *cert;
3364 	int r = 0;
3365 
3366 	ssl = X509_STORE_CTX_get_app_data(ctx);
3367 	dtls = (switch_dtls_t *) SSL_get_app_data(ssl);
3368 
3369 	if (!(ssl && dtls)) {
3370 		return 0;
3371 	}
3372 
3373 	if ((cert = SSL_get_peer_certificate(dtls->ssl))) {
3374 		switch_core_cert_extract_fingerprint(cert, dtls->remote_fp);
3375 
3376 		r = switch_core_cert_verify(dtls->remote_fp);
3377 
3378 		X509_free(cert);
3379 	} else {
3380 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(dtls->rtp_session->session), SWITCH_LOG_ERROR, "%s CERT ERR!\n", rtp_type(dtls->rtp_session));
3381 	}
3382 
3383 	return r;
3384 }
3385 #endif
3386 
3387 
3388 ////////////
3389 
3390 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3391 static BIO_METHOD dtls_bio_filter_methods;
3392 #else
3393 static BIO_METHOD *dtls_bio_filter_methods;
3394 #endif
3395 
BIO_dtls_filter(void)3396 BIO_METHOD *BIO_dtls_filter(void) {
3397 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3398 	return(&dtls_bio_filter_methods);
3399 #else
3400 	return(dtls_bio_filter_methods);
3401 #endif
3402 }
3403 
3404 typedef struct packet_list_s {
3405 	//void *packet;
3406 	int size;
3407 	struct packet_list_s *next;
3408 } packet_list_t;
3409 
3410 /* Helper struct to keep the filter state */
3411 typedef struct dtls_bio_filter {
3412 	packet_list_t *packets;
3413 	packet_list_t *unused;
3414 	packet_list_t *tail;
3415 	switch_mutex_t *mutex;
3416 	switch_memory_pool_t *pool;
3417 	long mtu;
3418 } dtls_bio_filter;
3419 
3420 
dtls_bio_filter_new(BIO * bio)3421 static int dtls_bio_filter_new(BIO *bio) {
3422 	/* Create a filter state struct */
3423 	dtls_bio_filter *filter;
3424 	switch_memory_pool_t *pool;
3425 
3426 	switch_core_new_memory_pool(&pool);
3427 	filter = switch_core_alloc(pool, sizeof(*filter));
3428 	filter->pool = pool;
3429 
3430 	filter->packets = NULL;
3431 	switch_mutex_init(&filter->mutex, SWITCH_MUTEX_NESTED, filter->pool);
3432 
3433 	/* Set the BIO as initialized */
3434 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3435 	bio->init = 1;
3436 	bio->ptr = filter;
3437 	bio->flags = 0;
3438 #else
3439 	BIO_set_init(bio, 1);
3440 	BIO_set_data(bio, filter);
3441 	BIO_clear_flags(bio, ~0);
3442 #endif
3443 
3444 	return 1;
3445 }
3446 
dtls_bio_filter_free(BIO * bio)3447 static int dtls_bio_filter_free(BIO *bio) {
3448 	dtls_bio_filter *filter;
3449 
3450 	if (bio == NULL) {
3451 		return 0;
3452 	}
3453 
3454 	/* Get rid of the filter state */
3455 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3456 	filter = (dtls_bio_filter *)bio->ptr;
3457 #else
3458 	filter = (dtls_bio_filter *)BIO_get_data(bio);
3459 #endif
3460 
3461 	if (filter != NULL) {
3462 		switch_memory_pool_t *pool = filter->pool;
3463 		switch_core_destroy_memory_pool(&pool);
3464 		pool = NULL;
3465 		filter = NULL;
3466 	}
3467 
3468 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3469 	bio->ptr = NULL;
3470 	bio->init = 0;
3471 	bio->flags = 0;
3472 #else
3473 	BIO_set_init(bio, 0);
3474 	BIO_set_data(bio, NULL);
3475 	BIO_clear_flags(bio, ~0);
3476 #endif
3477 	return 1;
3478 }
3479 
dtls_bio_filter_write(BIO * bio,const char * in,int inl)3480 static int dtls_bio_filter_write(BIO *bio, const char *in, int inl) {
3481 	long ret;
3482 	dtls_bio_filter *filter;
3483 
3484 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "dtls_bio_filter_write: %p, %d\n", (void *)in, inl);
3485 	/* Forward data to the write BIO */
3486 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3487 	ret = BIO_write(bio->next_bio, in, inl);
3488 #else
3489 	ret = BIO_write(BIO_next(bio), in, inl);
3490 #endif
3491 
3492 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "  -- %ld\n", ret);
3493 
3494 	/* Keep track of the packet, as we'll advertize them one by one after a pending check */
3495 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3496 	filter = (dtls_bio_filter *)bio->ptr;
3497 #else
3498 	filter = (dtls_bio_filter *)BIO_get_data(bio);
3499 #endif
3500 
3501 	if (filter != NULL) {
3502 		packet_list_t *node;
3503 
3504 		switch_mutex_lock(filter->mutex);
3505 		if (filter->unused) {
3506 			node = filter->unused;
3507 			node->next = NULL;
3508 			filter->unused = filter->unused->next;
3509 		} else {
3510 			node = switch_core_alloc(filter->pool, sizeof(*node));
3511 		}
3512 
3513 		node->size = ret;
3514 
3515 		if (filter->tail) {
3516 			filter->tail->next = node;
3517 		} else {
3518 			filter->packets = node;
3519 		}
3520 
3521 		filter->tail = node;
3522 
3523 		switch_mutex_unlock(filter->mutex);
3524 		//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "New list length: %d\n", g_list_length(filter->packets));
3525 	}
3526 	return ret;
3527 }
3528 
dtls_bio_filter_ctrl(BIO * bio,int cmd,long num,void * ptr)3529 static long dtls_bio_filter_ctrl(BIO *bio, int cmd, long num, void *ptr) {
3530 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3531 	dtls_bio_filter *filter = (dtls_bio_filter *)bio->ptr;
3532 #else
3533 	dtls_bio_filter *filter = (dtls_bio_filter *)BIO_get_data(bio);
3534 #endif
3535 
3536 	switch(cmd) {
3537 	case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
3538 		return 1200;
3539 	case BIO_CTRL_DGRAM_SET_MTU:
3540 		filter->mtu = num;
3541 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Setting MTU: %ld\n", filter->mtu);
3542 		return num;
3543 	case BIO_CTRL_FLUSH:
3544 		return 1;
3545 	case BIO_CTRL_DGRAM_QUERY_MTU:
3546 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Advertizing MTU: %ld\n", filter->mtu);
3547 		return filter->mtu;
3548 	case BIO_CTRL_WPENDING:
3549 		return 0L;
3550 	case BIO_CTRL_PENDING: {
3551 		int pending = 0;
3552 		packet_list_t *top;
3553 
3554 		if (filter == NULL) {
3555 			return 0;
3556 		}
3557 
3558 		switch_mutex_lock(filter->mutex);
3559 		if ((top = filter->packets)) {
3560 			filter->packets = filter->packets->next;
3561 
3562 			if (top == filter->tail || !filter->packets) {
3563 				filter->tail = NULL;
3564 			}
3565 
3566 			pending = top->size;
3567 			top->next = filter->unused;
3568 			filter->unused = top;
3569 		}
3570 		switch_mutex_unlock(filter->mutex);
3571 
3572 		return pending;
3573 	}
3574 	default:
3575 		//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dtls_bio_filter_ctrl: %d\n", cmd);
3576 		break;
3577 	}
3578 	return 0;
3579 }
3580 
3581 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3582 static BIO_METHOD dtls_bio_filter_methods = {
3583 	BIO_TYPE_FILTER,
3584 	"DTLS filter",
3585 	dtls_bio_filter_write,
3586 	NULL,
3587 	NULL,
3588 	NULL,
3589 	dtls_bio_filter_ctrl,
3590 	dtls_bio_filter_new,
3591 	dtls_bio_filter_free,
3592 	NULL
3593 };
3594 #else
3595 static BIO_METHOD *dtls_bio_filter_methods = NULL;
3596 #endif
3597 
3598 ///////////
3599 
3600 
3601 
switch_rtp_has_dtls(void)3602 SWITCH_DECLARE(int) switch_rtp_has_dtls(void) {
3603 #ifdef HAVE_OPENSSL_DTLS_SRTP
3604 	return 1;
3605 #else
3606 	return 0;
3607 #endif
3608 }
3609 
switch_rtp_dtls_state(switch_rtp_t * rtp_session,dtls_type_t type)3610 SWITCH_DECLARE(dtls_state_t) switch_rtp_dtls_state(switch_rtp_t *rtp_session, dtls_type_t type)
3611 {
3612 	dtls_state_t s = DS_OFF;
3613 
3614 	switch_mutex_lock(rtp_session->ice_mutex);
3615 
3616 	if (!rtp_session || (!rtp_session->dtls && !rtp_session->rtcp_dtls)) {
3617 		s = DS_OFF;
3618 		goto done;
3619 	}
3620 
3621 	if ((type == DTLS_TYPE_RTP) && rtp_session->dtls) {
3622 		s = rtp_session->dtls->state;
3623 		goto done;
3624 	}
3625 
3626 	if ((type == DTLS_TYPE_RTCP) && rtp_session->rtcp_dtls) {
3627 		s = rtp_session->rtcp_dtls->state;
3628 	}
3629 
3630  done:
3631 
3632 	switch_mutex_unlock(rtp_session->ice_mutex);
3633 
3634 	return s;
3635 }
3636 
switch_rtp_del_dtls(switch_rtp_t * rtp_session,dtls_type_t type)3637 SWITCH_DECLARE(switch_status_t) switch_rtp_del_dtls(switch_rtp_t *rtp_session, dtls_type_t type)
3638 {
3639 	switch_status_t status = SWITCH_STATUS_SUCCESS;
3640 
3641 	switch_mutex_lock(rtp_session->ice_mutex);
3642 
3643 	if (!rtp_session || (!rtp_session->dtls && !rtp_session->rtcp_dtls)) {
3644 		switch_goto_status(SWITCH_STATUS_FALSE, done);
3645 	}
3646 
3647 	if ((type & DTLS_TYPE_RTP)) {
3648 		if (rtp_session->dtls && rtp_session->dtls == rtp_session->rtcp_dtls) {
3649 			rtp_session->rtcp_dtls = NULL;
3650 		}
3651 
3652 		if (rtp_session->dtls) {
3653 			free_dtls(&rtp_session->dtls);
3654 		}
3655 
3656 		if (rtp_session->jb) {
3657 			switch_jb_reset(rtp_session->jb);
3658 		}
3659 
3660 		if (rtp_session->vb) {
3661 			switch_jb_reset(rtp_session->vb);
3662 		}
3663 
3664 		if (rtp_session->vbw) {
3665 			switch_jb_reset(rtp_session->vbw);
3666 		}
3667 
3668 	}
3669 
3670 	if ((type & DTLS_TYPE_RTCP) && rtp_session->rtcp_dtls) {
3671 		free_dtls(&rtp_session->rtcp_dtls);
3672 	}
3673 
3674 
3675 #ifdef ENABLE_SRTP
3676 	if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
3677 		int x;
3678 
3679 		rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0;
3680 
3681 		for(x = 0; x < 2; x++) {
3682 			if (rtp_session->send_ctx[x]) {
3683 				srtp_dealloc(rtp_session->send_ctx[x]);
3684 				rtp_session->send_ctx[x] = NULL;
3685 			}
3686 		}
3687 	}
3688 
3689 	if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV]) {
3690 		int x;
3691 
3692 		rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
3693 
3694 		for (x = 0; x < 2; x++) {
3695 			if (rtp_session->recv_ctx[x]) {
3696 				srtp_dealloc(rtp_session->recv_ctx[x]);
3697 				rtp_session->recv_ctx[x] = NULL;
3698 			}
3699 		}
3700 	}
3701 #endif
3702 
3703  done:
3704 
3705 	switch_mutex_unlock(rtp_session->ice_mutex);
3706 
3707 	return status;
3708 }
3709 
switch_rtp_add_dtls(switch_rtp_t * rtp_session,dtls_fingerprint_t * local_fp,dtls_fingerprint_t * remote_fp,dtls_type_t type,uint8_t want_DTLSv1_2)3710 SWITCH_DECLARE(switch_status_t) switch_rtp_add_dtls(switch_rtp_t *rtp_session, dtls_fingerprint_t *local_fp, dtls_fingerprint_t *remote_fp, dtls_type_t type, uint8_t want_DTLSv1_2)
3711 {
3712 	switch_dtls_t *dtls;
3713 	const char *var;
3714 	int ret;
3715 	const char *kind = "";
3716 	BIO *bio;
3717 	DH *dh;
3718 	switch_status_t status = SWITCH_STATUS_SUCCESS;
3719 #ifndef OPENSSL_NO_EC
3720 	EC_KEY* ecdh;
3721 #endif
3722 
3723 #ifndef HAVE_OPENSSL_DTLS_SRTP
3724 	return SWITCH_STATUS_FALSE;
3725 #endif
3726 
3727 	if (!switch_rtp_ready(rtp_session)) {
3728 		return SWITCH_STATUS_FALSE;
3729 	}
3730 
3731 	switch_mutex_lock(rtp_session->ice_mutex);
3732 
3733 	if (!((type & DTLS_TYPE_RTP) || (type & DTLS_TYPE_RTCP)) || !((type & DTLS_TYPE_CLIENT) || (type & DTLS_TYPE_SERVER))) {
3734 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "INVALID TYPE!\n");
3735 	}
3736 
3737 	switch_rtp_del_dtls(rtp_session, type);
3738 
3739 	if ((type & DTLS_TYPE_RTP) && (type & DTLS_TYPE_RTCP)) {
3740 		kind = "RTP/RTCP";
3741 	} else if ((type & DTLS_TYPE_RTP)) {
3742 		kind = "RTP";
3743 	} else {
3744 		kind = "RTCP";
3745 	}
3746 
3747 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO,
3748 					  "Activate %s %s DTLS %s\n", kind, rtp_type(rtp_session), (type & DTLS_TYPE_SERVER) ? "server" : "client");
3749 
3750 	if (((type & DTLS_TYPE_RTP) && rtp_session->dtls) || ((type & DTLS_TYPE_RTCP) && rtp_session->rtcp_dtls)) {
3751 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "DTLS ALREADY INIT\n");
3752 		switch_goto_status(SWITCH_STATUS_FALSE, done);
3753 	}
3754 
3755 	dtls = switch_core_alloc(rtp_session->pool, sizeof(*dtls));
3756 
3757 	dtls->pem = switch_core_sprintf(rtp_session->pool, "%s%s%s.pem", SWITCH_GLOBAL_dirs.certs_dir, SWITCH_PATH_SEPARATOR, DTLS_SRTP_FNAME);
3758 
3759 	if (switch_file_exists(dtls->pem, rtp_session->pool) == SWITCH_STATUS_SUCCESS) {
3760 		dtls->pvt = dtls->rsa = dtls->pem;
3761 	} else {
3762 		dtls->pvt = switch_core_sprintf(rtp_session->pool, "%s%s%s.key", SWITCH_GLOBAL_dirs.certs_dir, SWITCH_PATH_SEPARATOR, DTLS_SRTP_FNAME);
3763 		dtls->rsa = switch_core_sprintf(rtp_session->pool, "%s%s%s.crt", SWITCH_GLOBAL_dirs.certs_dir, SWITCH_PATH_SEPARATOR, DTLS_SRTP_FNAME);
3764 	}
3765 
3766 	dtls->ca = switch_core_sprintf(rtp_session->pool, "%s%sca-bundle.crt", SWITCH_GLOBAL_dirs.certs_dir, SWITCH_PATH_SEPARATOR);
3767 
3768 #if OPENSSL_VERSION_NUMBER >= 0x10100000
3769 	dtls->ssl_ctx = SSL_CTX_new((type & DTLS_TYPE_SERVER) ? DTLS_server_method() : DTLS_client_method());
3770 #else
3771     #ifdef HAVE_OPENSSL_DTLSv1_2_method
3772 	        dtls->ssl_ctx = SSL_CTX_new((type & DTLS_TYPE_SERVER) ? (want_DTLSv1_2 ? DTLSv1_2_server_method() : DTLSv1_server_method()) : (want_DTLSv1_2 ? DTLSv1_2_client_method() : DTLSv1_client_method()));
3773     #else
3774             dtls->ssl_ctx = SSL_CTX_new((type & DTLS_TYPE_SERVER) ? DTLSv1_server_method() : DTLSv1_client_method());
3775     #endif // HAVE_OPENSSL_DTLSv1_2_method
3776 #endif
3777 	switch_assert(dtls->ssl_ctx);
3778 
3779 	bio = BIO_new_file(dtls->pem, "r");
3780 	dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3781 	BIO_free(bio);
3782 	if (dh) {
3783 		SSL_CTX_set_tmp_dh(dtls->ssl_ctx, dh);
3784 		DH_free(dh);
3785 	}
3786 
3787 	SSL_CTX_set_mode(dtls->ssl_ctx, SSL_MODE_AUTO_RETRY);
3788 
3789 	//SSL_CTX_set_verify(dtls->ssl_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
3790 	SSL_CTX_set_verify(dtls->ssl_ctx, SSL_VERIFY_NONE, NULL);
3791 
3792 	//SSL_CTX_set_cipher_list(dtls->ssl_ctx, "ECDH:!RC4:!SSLv3:RSA_WITH_AES_128_CBC_SHA");
3793 	//SSL_CTX_set_cipher_list(dtls->ssl_ctx, "ECDHE-RSA-AES256-GCM-SHA384");
3794 	SSL_CTX_set_cipher_list(dtls->ssl_ctx, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
3795 	//SSL_CTX_set_cipher_list(dtls->ssl_ctx, "SUITEB128");
3796 	SSL_CTX_set_read_ahead(dtls->ssl_ctx, 1);
3797 #ifdef HAVE_OPENSSL_DTLS_SRTP
3798 	//SSL_CTX_set_tlsext_use_srtp(dtls->ssl_ctx, "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32");
3799 	SSL_CTX_set_tlsext_use_srtp(dtls->ssl_ctx, "SRTP_AES128_CM_SHA1_80");
3800 #endif
3801 
3802 	dtls->type = type;
3803 	dtls->read_bio = BIO_new(BIO_s_mem());
3804 	switch_assert(dtls->read_bio);
3805 
3806 	dtls->write_bio = BIO_new(BIO_s_mem());
3807 	switch_assert(dtls->write_bio);
3808 
3809 	BIO_set_mem_eof_return(dtls->read_bio, -1);
3810 	BIO_set_mem_eof_return(dtls->write_bio, -1);
3811 
3812 	if ((ret=SSL_CTX_use_certificate_file(dtls->ssl_ctx, dtls->rsa, SSL_FILETYPE_PEM)) != 1) {
3813 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS cert err [%d]\n", rtp_type(rtp_session), SSL_get_error(dtls->ssl, ret));
3814 		switch_goto_status(SWITCH_STATUS_FALSE, done);
3815 	}
3816 
3817 	if ((ret=SSL_CTX_use_PrivateKey_file(dtls->ssl_ctx, dtls->pvt, SSL_FILETYPE_PEM)) != 1) {
3818 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS key err [%d]\n", rtp_type(rtp_session), SSL_get_error(dtls->ssl, ret));
3819 		switch_goto_status(SWITCH_STATUS_FALSE, done);
3820 	}
3821 
3822 	if (SSL_CTX_check_private_key(dtls->ssl_ctx) == 0) {
3823 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS check key failed\n", rtp_type(rtp_session));
3824 		switch_goto_status(SWITCH_STATUS_FALSE, done);
3825 	}
3826 
3827 	if (!zstr(dtls->ca) && switch_file_exists(dtls->ca, rtp_session->pool) == SWITCH_STATUS_SUCCESS
3828 		&& (ret = SSL_CTX_load_verify_locations(dtls->ssl_ctx, dtls->ca, NULL)) != 1) {
3829 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS check chain cert failed [%d]\n",
3830 						  rtp_type(rtp_session) ,
3831 						  SSL_get_error(dtls->ssl, ret));
3832 		switch_goto_status(SWITCH_STATUS_FALSE, done);
3833 	}
3834 
3835 	dtls->ssl = SSL_new(dtls->ssl_ctx);
3836 
3837 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3838 	dtls->filter_bio = BIO_new(BIO_dtls_filter());
3839 #else
3840 	dtls_bio_filter_methods = BIO_meth_new(BIO_TYPE_FILTER | BIO_get_new_index(), "DTLS filter");
3841 	BIO_meth_set_write(dtls_bio_filter_methods, dtls_bio_filter_write);
3842 	BIO_meth_set_ctrl(dtls_bio_filter_methods, dtls_bio_filter_ctrl);
3843 	BIO_meth_set_create(dtls_bio_filter_methods, dtls_bio_filter_new);
3844 	BIO_meth_set_destroy(dtls_bio_filter_methods, dtls_bio_filter_free);
3845 	dtls->filter_bio = BIO_new(dtls_bio_filter_methods);
3846 #endif
3847 
3848 	switch_assert(dtls->filter_bio);
3849 
3850 	BIO_push(dtls->filter_bio, dtls->write_bio);
3851 
3852 	SSL_set_bio(dtls->ssl, dtls->read_bio, dtls->filter_bio);
3853 
3854 	SSL_set_mode(dtls->ssl, SSL_MODE_AUTO_RETRY);
3855 	SSL_set_read_ahead(dtls->ssl, 1);
3856 
3857 
3858 	//SSL_set_verify(dtls->ssl, (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), cb_verify_peer);
3859 
3860 #ifndef OPENSSL_NO_EC
3861 	ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
3862 	if (!ecdh) {
3863 		switch_goto_status(SWITCH_STATUS_FALSE, done);
3864 	}
3865 	SSL_set_options(dtls->ssl, SSL_OP_SINGLE_ECDH_USE);
3866 	SSL_set_tmp_ecdh(dtls->ssl, ecdh);
3867 	EC_KEY_free(ecdh);
3868 #endif
3869 
3870 	SSL_set_verify(dtls->ssl, SSL_VERIFY_NONE, NULL);
3871 	SSL_set_app_data(dtls->ssl, dtls);
3872 
3873 	dtls->local_fp = local_fp;
3874 	dtls->remote_fp = remote_fp;
3875 	dtls->rtp_session = rtp_session;
3876 	dtls->mtu = 1200;
3877 
3878 	if (rtp_session->session) {
3879 		switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
3880 		if ((var = switch_channel_get_variable(channel, "rtp_dtls_mtu"))) {
3881 			int mtu = atoi(var);
3882 
3883 			if (mtu > 0 && mtu < MAX_DTLS_MTU) {
3884 				dtls->mtu = mtu;
3885 			}
3886 
3887 		}
3888 	}
3889 
3890 	BIO_ctrl(dtls->filter_bio, BIO_CTRL_DGRAM_SET_MTU, dtls->mtu, NULL);
3891 
3892 	switch_core_cert_expand_fingerprint(remote_fp, remote_fp->str);
3893 
3894 	if ((type & DTLS_TYPE_RTP)) {
3895 		rtp_session->dtls = dtls;
3896 		dtls->sock_output = rtp_session->sock_output;
3897 		dtls->remote_addr = rtp_session->remote_addr;
3898 	}
3899 
3900 	if ((type & DTLS_TYPE_RTCP)) {
3901 		rtp_session->rtcp_dtls = dtls;
3902 		if (!(type & DTLS_TYPE_RTP)) {
3903 			dtls->sock_output = rtp_session->rtcp_sock_output;
3904 			dtls->remote_addr = rtp_session->rtcp_remote_addr;
3905 		}
3906 	}
3907 
3908 	if ((type & DTLS_TYPE_SERVER)) {
3909 		SSL_set_accept_state(dtls->ssl);
3910 	} else {
3911 		SSL_set_connect_state(dtls->ssl);
3912 	}
3913 
3914 	dtls_set_state(dtls, DS_HANDSHAKE);
3915 
3916 	rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK] = 1;
3917 	switch_rtp_break(rtp_session);
3918 
3919  done:
3920 
3921 	switch_mutex_unlock(rtp_session->ice_mutex);
3922 
3923 	return status;
3924 
3925 }
3926 
switch_rtp_add_crypto_key(switch_rtp_t * rtp_session,switch_rtp_crypto_direction_t direction,uint32_t index,switch_secure_settings_t * ssec)3927 SWITCH_DECLARE(switch_status_t) switch_rtp_add_crypto_key(switch_rtp_t *rtp_session, switch_rtp_crypto_direction_t direction, uint32_t index, switch_secure_settings_t *ssec)
3928 {
3929 #ifndef ENABLE_SRTP
3930 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "SRTP NOT SUPPORTED IN THIS BUILD!\n");
3931 	return SWITCH_STATUS_FALSE;
3932 #else
3933 
3934 	switch_rtp_crypto_key_t *crypto_key;
3935 	srtp_policy_t *policy;
3936 	srtp_err_status_t stat;
3937 	switch_status_t status = SWITCH_STATUS_SUCCESS;
3938 
3939 	switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
3940 	switch_event_t *fsevent = NULL;
3941 	int idx = 0;
3942 	const char *var;
3943 	unsigned char b64_key[512] = "";
3944 
3945 	unsigned char	*keysalt = NULL;
3946 	switch_size_t	keysalt_len = 0;
3947 
3948 	switch_crypto_key_material_t	*key_material = NULL;
3949 	unsigned long					*key_material_n = NULL;
3950 	srtp_master_key_t		**mkis = NULL;
3951 	srtp_master_key_t		*mki = NULL;
3952 	int mki_idx = 0;
3953 
3954 	keysalt_len = switch_core_media_crypto_keysalt_len(ssec->crypto_type);
3955 
3956 	if (direction >= SWITCH_RTP_CRYPTO_MAX || keysalt_len > SWITCH_RTP_MAX_CRYPTO_LEN) {
3957 		return SWITCH_STATUS_FALSE;
3958 	}
3959 
3960 	if (direction == SWITCH_RTP_CRYPTO_RECV_RTCP) {
3961 		direction = SWITCH_RTP_CRYPTO_RECV;
3962 		rtp_session->srtp_idx_rtcp = idx = 1;
3963 	} else if (direction == SWITCH_RTP_CRYPTO_SEND_RTCP) {
3964 		direction = SWITCH_RTP_CRYPTO_SEND;
3965 		rtp_session->srtp_idx_rtcp = idx = 1;
3966 	}
3967 
3968 	if (direction == SWITCH_RTP_CRYPTO_RECV) {
3969 		policy = &rtp_session->recv_policy[idx];
3970 		keysalt = ssec->remote_raw_key;
3971 		key_material = ssec->remote_key_material_next;
3972 		key_material_n = &ssec->remote_key_material_n;
3973 	} else {
3974 		policy = &rtp_session->send_policy[idx];
3975 		keysalt = ssec->local_raw_key;
3976 		key_material = ssec->local_key_material_next;
3977 		key_material_n = &ssec->local_key_material_n;
3978 	}
3979 
3980 	switch_b64_encode(keysalt, keysalt_len, b64_key, sizeof(b64_key));
3981 
3982 	if (switch_true(switch_core_get_variable("rtp_retain_crypto_keys"))) {
3983 		switch(direction) {
3984 			case SWITCH_RTP_CRYPTO_SEND:
3985 				switch_channel_set_variable(channel, "srtp_local_crypto_key", (const char *)b64_key);
3986 				break;
3987 			case SWITCH_RTP_CRYPTO_RECV:
3988 				switch_channel_set_variable(channel, "srtp_remote_crypto_key", (const char *)b64_key);
3989 				break;
3990 			case SWITCH_RTP_CRYPTO_SEND_RTCP:
3991 				switch_channel_set_variable(channel, "srtcp_local_crypto_key", (const char *)b64_key);
3992 				break;
3993 			case SWITCH_RTP_CRYPTO_RECV_RTCP:
3994 				switch_channel_set_variable(channel, "srtcp_remote_crypto_key", (const char *)b64_key);
3995 				break;
3996 			default:
3997 				break;
3998 		}
3999 
4000 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
4001 			switch(direction) {
4002 			case SWITCH_RTP_CRYPTO_SEND:
4003 				switch_channel_set_variable(channel, "srtp_local_video_crypto_key", (const char *)b64_key);
4004 				break;
4005 			case SWITCH_RTP_CRYPTO_RECV:
4006 				switch_channel_set_variable(channel, "srtp_remote_video_crypto_key", (const char *)b64_key);
4007 				break;
4008 			case SWITCH_RTP_CRYPTO_SEND_RTCP:
4009 				switch_channel_set_variable(channel, "srtcp_local_video_crypto_key", (const char *)b64_key);
4010 				break;
4011 			case SWITCH_RTP_CRYPTO_RECV_RTCP:
4012 				switch_channel_set_variable(channel, "srtcp_remote_video_crypto_key", (const char *)b64_key);
4013 				break;
4014 			default:
4015 				break;
4016 			}
4017 
4018 		} else {
4019 			switch(direction) {
4020 			case SWITCH_RTP_CRYPTO_SEND:
4021 				switch_channel_set_variable(channel, "srtp_local_audio_crypto_key", (const char *)b64_key);
4022 				break;
4023 			case SWITCH_RTP_CRYPTO_RECV:
4024 				switch_channel_set_variable(channel, "srtp_remote_audio_crypto_key", (const char *)b64_key);
4025 				break;
4026 			case SWITCH_RTP_CRYPTO_SEND_RTCP:
4027 				switch_channel_set_variable(channel, "srtcp_local_audio_crypto_key", (const char *)b64_key);
4028 				break;
4029 			case SWITCH_RTP_CRYPTO_RECV_RTCP:
4030 				switch_channel_set_variable(channel, "srtcp_remote_audio_crypto_key", (const char *)b64_key);
4031 				break;
4032 			default:
4033 				break;
4034 			}
4035 		}
4036 	}
4037 
4038 	crypto_key = switch_core_alloc(rtp_session->pool, sizeof(*crypto_key));
4039 
4040 	crypto_key->type = ssec->crypto_type;
4041 	crypto_key->index = index;
4042 	memcpy(crypto_key->keysalt, keysalt, keysalt_len);
4043 	crypto_key->next = rtp_session->crypto_keys[direction];
4044 	rtp_session->crypto_keys[direction] = crypto_key;
4045 
4046 	memset(policy, 0, sizeof(*policy));
4047 
4048 	/* many devices can't handle gaps in SRTP streams */
4049 	if (!((var = switch_channel_get_variable(channel, "srtp_allow_idle_gaps"))
4050 		  && switch_true(var))
4051 		&& (!(var = switch_channel_get_variable(channel, "send_silence_when_idle"))
4052 			|| !(atoi(var)))) {
4053 		switch_channel_set_variable(channel, "send_silence_when_idle", "-1");
4054 	}
4055 
4056 	switch (crypto_key->type) {
4057 	case AES_CM_128_HMAC_SHA1_80:
4058 		srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtp);
4059 		srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtcp);
4060 
4061 		if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
4062 			switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_80");
4063 		}
4064 		break;
4065 	case AES_CM_128_HMAC_SHA1_32:
4066 		srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtp);
4067 		srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtcp);
4068 
4069 
4070 		if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
4071 			switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_32");
4072 		}
4073 		break;
4074 
4075 	case AEAD_AES_256_GCM_8:
4076 		srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy->rtp);
4077 		srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy->rtcp);
4078 
4079 		if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
4080 			switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_256_GCM_8");
4081 		}
4082 		break;
4083 
4084 	case AEAD_AES_128_GCM_8:
4085 		srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy->rtp);
4086 		srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy->rtcp);
4087 
4088 		if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
4089 			switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_128_GCM_8");
4090 		}
4091 		break;
4092 
4093 	case AES_CM_256_HMAC_SHA1_80:
4094 		srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtp);
4095 		srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtcp);
4096 		if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
4097 			switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_256_HMAC_SHA1_80");
4098 		}
4099 		break;
4100 	case AES_CM_256_HMAC_SHA1_32:
4101 		srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy->rtp);
4102 		srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy->rtcp);
4103 		if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
4104 			switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_256_HMAC_SHA1_32");
4105 		}
4106 		break;
4107 	case AES_CM_128_NULL_AUTH:
4108 		srtp_crypto_policy_set_aes_cm_128_null_auth(&policy->rtp);
4109 		srtp_crypto_policy_set_aes_cm_128_null_auth(&policy->rtcp);
4110 
4111 		if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
4112 			switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_NULL_AUTH");
4113 		}
4114 		break;
4115 	default:
4116 		break;
4117 	}
4118 
4119 	/* Setup the policy with MKI if they are used. Number of key materials must be positive to use MKI. */
4120 	if (key_material && (*key_material_n > 0)) {
4121 
4122 		if (direction == SWITCH_RTP_CRYPTO_RECV) {
4123 			rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI] = 1;	/* tell the rest of the environment MKI is used */
4124 		} else {
4125 			rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI] = 1;	/* tell the rest of the environment MKI is used */
4126 		}
4127 
4128 		/* key must be NULL for libsrtp to work correctly with MKI. */
4129 		policy->key = NULL;
4130 
4131 		/* Allocate array for MKIs. */
4132 		mkis = switch_core_alloc(rtp_session->pool, *key_material_n * sizeof(srtp_master_key_t*));
4133 		if (!mkis) {
4134 			return SWITCH_STATUS_FALSE;
4135 		}
4136 
4137 		/* Build array of MKIs. */
4138 		mki_idx = 0;
4139 
4140 		while (key_material && (mki_idx < *key_material_n)) {
4141 
4142 			if (key_material->mki_size < 1) {
4143 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "MKI bad key size at MKI %d\n", mki_idx);
4144 				return SWITCH_STATUS_FALSE;
4145 			}
4146 
4147 			mki = switch_core_alloc(rtp_session->pool, sizeof(srtp_master_key_t));
4148 			if (!mki) {
4149 				return SWITCH_STATUS_FALSE;
4150 			}
4151 
4152 			/* Setup MKI. */
4153 			mki->mki_id = switch_core_alloc(rtp_session->pool, sizeof(key_material->mki_size));
4154 			if (!mki->mki_id) {
4155 				return SWITCH_STATUS_FALSE;
4156 			}
4157 
4158 			mki->key = switch_core_alloc(rtp_session->pool, keysalt_len);
4159 			if (!mki->key) {
4160 				return SWITCH_STATUS_FALSE;
4161 			}
4162 
4163 			memcpy(mki->mki_id, &key_material->mki_id, key_material->mki_size);
4164 			mki->mki_size = key_material->mki_size;
4165 			memcpy(mki->key, key_material->raw_key, keysalt_len);
4166 
4167 			mkis[mki_idx] = mki;
4168 
4169 			key_material = key_material->next;
4170 			++mki_idx;
4171 		}
4172 
4173 		/* And pass the array of MKIs to libsrtp. */
4174 		policy->keys = mkis;
4175 		policy->num_master_keys = mki_idx;
4176 
4177 	} else {
4178 		policy->key = (uint8_t *) crypto_key->keysalt;
4179 	}
4180 
4181 	policy->next = NULL;
4182 
4183 	policy->window_size = 1024;
4184 	policy->allow_repeat_tx = 1;
4185 
4186 	//policy->rtp.sec_serv = sec_serv_conf_and_auth;
4187 	//policy->rtcp.sec_serv = sec_serv_conf_and_auth;
4188 
4189 	switch (direction) {
4190 	case SWITCH_RTP_CRYPTO_RECV:
4191 		policy->ssrc.type = ssrc_any_inbound;
4192 
4193 		if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && idx == 0 && rtp_session->recv_ctx[idx]) {
4194 			rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] = 1;
4195 		} else {
4196 			if ((stat = srtp_create(&rtp_session->recv_ctx[idx], policy)) || !rtp_session->recv_ctx[idx]) {
4197 				status = SWITCH_STATUS_FALSE;
4198 			}
4199 
4200 			if (status == SWITCH_STATUS_SUCCESS) {
4201 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Activating %s Secure %s RECV%s\n",
4202 								  rtp_type(rtp_session), idx ? "RTCP" : "RTP", rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI] ? " (with MKI)" : "");
4203 				rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 1;
4204 			} else {
4205 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating srtp [%d]\n", stat);
4206 				return status;
4207 			}
4208 		}
4209 		break;
4210 	case SWITCH_RTP_CRYPTO_SEND:
4211 		policy->ssrc.type = ssrc_any_outbound;
4212 		//policy->ssrc.type = ssrc_specific;
4213 		//policy->ssrc.value = rtp_session->ssrc;
4214 
4215 		if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] && idx == 0 && rtp_session->send_ctx[idx]) {
4216 			rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET] = 1;
4217 		} else {
4218 			if ((stat = srtp_create(&rtp_session->send_ctx[idx], policy)) || !rtp_session->send_ctx[idx]) {
4219 				status = SWITCH_STATUS_FALSE;
4220 			}
4221 
4222 			if (status == SWITCH_STATUS_SUCCESS) {
4223 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Activating %s Secure %s SEND%s\n",
4224 								  rtp_type(rtp_session), idx ? "RTCP" : "RTP", rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI] ? " (with MKI)" : "");
4225 				rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 1;
4226 			} else {
4227 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating SRTP [%d]\n", stat);
4228 				return status;
4229 			}
4230 		}
4231 
4232 		break;
4233 	default:
4234 		abort();
4235 		break;
4236 	}
4237 
4238 	if (switch_event_create(&fsevent, SWITCH_EVENT_CALL_SECURE) == SWITCH_STATUS_SUCCESS) {
4239 		if (rtp_session->dtls) {
4240 			switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "srtp:dtls:AES_CM_128_HMAC_SHA1_80");
4241 			switch_channel_set_variable(channel, "rtp_has_crypto", "srtp:dtls:AES_CM_128_HMAC_SHA1_80");
4242 		} else {
4243 			switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "srtp:sdes:%s", switch_channel_get_variable(channel, "rtp_has_crypto"));
4244 		}
4245 		switch_event_add_header_string(fsevent, SWITCH_STACK_BOTTOM, "caller-unique-id", switch_channel_get_uuid(channel));
4246 		switch_event_fire(&fsevent);
4247 	}
4248 
4249 
4250 	return SWITCH_STATUS_SUCCESS;
4251 #endif
4252 }
4253 
switch_rtp_set_interval(switch_rtp_t * rtp_session,uint32_t ms_per_packet,uint32_t samples_per_interval)4254 SWITCH_DECLARE(switch_status_t) switch_rtp_set_interval(switch_rtp_t *rtp_session, uint32_t ms_per_packet, uint32_t samples_per_interval)
4255 {
4256 	rtp_session->ms_per_packet = ms_per_packet;
4257 	rtp_session->samples_per_interval = rtp_session->conf_samples_per_interval = samples_per_interval;
4258 	rtp_session->missed_count = 0;
4259 	rtp_session->samples_per_second =
4260 		(uint32_t) ((double) (1000.0f / (double) (rtp_session->ms_per_packet / 1000)) * (double) rtp_session->samples_per_interval);
4261 
4262 	rtp_session->one_second = (rtp_session->samples_per_second / rtp_session->samples_per_interval);
4263 
4264 	return SWITCH_STATUS_SUCCESS;
4265 }
4266 
switch_rtp_change_interval(switch_rtp_t * rtp_session,uint32_t ms_per_packet,uint32_t samples_per_interval)4267 SWITCH_DECLARE(switch_status_t) switch_rtp_change_interval(switch_rtp_t *rtp_session, uint32_t ms_per_packet, uint32_t samples_per_interval)
4268 {
4269 	switch_status_t status = SWITCH_STATUS_SUCCESS;
4270 	int change_timer = 0;
4271 
4272 	if (rtp_session->ms_per_packet != ms_per_packet || rtp_session->samples_per_interval != samples_per_interval) {
4273 		change_timer = 1;
4274 	}
4275 
4276 	switch_rtp_set_interval(rtp_session, ms_per_packet, samples_per_interval);
4277 
4278 	if (change_timer && rtp_session->timer_name) {
4279 		READ_INC(rtp_session);
4280 		WRITE_INC(rtp_session);
4281 
4282 		if (rtp_session->timer.timer_interface) {
4283 			switch_core_timer_destroy(&rtp_session->timer);
4284 		}
4285 
4286 		if (rtp_session->write_timer.timer_interface) {
4287 			switch_core_timer_destroy(&rtp_session->write_timer);
4288 		}
4289 
4290 		if ((status = switch_core_timer_init(&rtp_session->timer,
4291 											 rtp_session->timer_name, ms_per_packet / 1000,
4292 											 samples_per_interval, rtp_session->pool)) == SWITCH_STATUS_SUCCESS) {
4293 
4294 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,
4295 							  "RE-Starting timer [%s] %d bytes per %dms\n", rtp_session->timer_name, samples_per_interval, ms_per_packet / 1000);
4296 			switch_core_timer_init(&rtp_session->write_timer, rtp_session->timer_name, (ms_per_packet / 1000), samples_per_interval, rtp_session->pool);
4297 		} else {
4298 
4299 			memset(&rtp_session->timer, 0, sizeof(rtp_session->timer));
4300 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR,
4301 							  "Problem RE-Starting timer [%s] %d bytes per %dms\n", rtp_session->timer_name, samples_per_interval, ms_per_packet / 1000);
4302 		}
4303 
4304 		WRITE_DEC(rtp_session);
4305 		READ_DEC(rtp_session);
4306 	}
4307 
4308 	return status;
4309 }
4310 
switch_rtp_set_ssrc(switch_rtp_t * rtp_session,uint32_t ssrc)4311 SWITCH_DECLARE(switch_status_t) switch_rtp_set_ssrc(switch_rtp_t *rtp_session, uint32_t ssrc)
4312 {
4313 	rtp_session->ssrc = ssrc;
4314 	rtp_session->send_msg.header.ssrc = htonl(rtp_session->ssrc);
4315 
4316 	return SWITCH_STATUS_SUCCESS;
4317 }
4318 
switch_rtp_set_remote_ssrc(switch_rtp_t * rtp_session,uint32_t ssrc)4319 SWITCH_DECLARE(switch_status_t) switch_rtp_set_remote_ssrc(switch_rtp_t *rtp_session, uint32_t ssrc)
4320 {
4321 	rtp_session->remote_ssrc = ssrc;
4322 	rtp_session->flags[SWITCH_RTP_FLAG_DETECT_SSRC] = 0;
4323 	return SWITCH_STATUS_SUCCESS;
4324 }
4325 
switch_rtp_create(switch_rtp_t ** new_rtp_session,switch_payload_t payload,uint32_t samples_per_interval,uint32_t ms_per_packet,switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID],char * timer_name,const char ** err,switch_memory_pool_t * pool)4326 SWITCH_DECLARE(switch_status_t) switch_rtp_create(switch_rtp_t **new_rtp_session,
4327 												  switch_payload_t payload,
4328 												  uint32_t samples_per_interval,
4329 												  uint32_t ms_per_packet,
4330 												  switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID], char *timer_name, const char **err, switch_memory_pool_t *pool)
4331 {
4332 	switch_rtp_t *rtp_session = NULL;
4333 	switch_core_session_t *session = switch_core_memory_pool_get_data(pool, "__session");
4334 	switch_channel_t *channel = NULL;
4335 
4336 	if (session) channel = switch_core_session_get_channel(session);
4337 
4338 	*new_rtp_session = NULL;
4339 
4340 	if (samples_per_interval > SWITCH_RTP_MAX_BUF_LEN) {
4341 		*err = "Packet Size Too Large!";
4342 		return SWITCH_STATUS_FALSE;
4343 	}
4344 
4345 	if (!(rtp_session = switch_core_alloc(pool, sizeof(*rtp_session)))) {
4346 		*err = "Memory Error!";
4347 		return SWITCH_STATUS_MEMERR;
4348 	}
4349 
4350 	rtp_session->pool = pool;
4351 	rtp_session->te = INVALID_PT;
4352 	rtp_session->recv_te = INVALID_PT;
4353 	rtp_session->cng_pt = INVALID_PT;
4354 	rtp_session->session = session;
4355 
4356 	switch_mutex_init(&rtp_session->flag_mutex, SWITCH_MUTEX_NESTED, pool);
4357 	switch_mutex_init(&rtp_session->read_mutex, SWITCH_MUTEX_NESTED, pool);
4358 	switch_mutex_init(&rtp_session->write_mutex, SWITCH_MUTEX_NESTED, pool);
4359 	switch_mutex_init(&rtp_session->ice_mutex, SWITCH_MUTEX_NESTED, pool);
4360 	switch_mutex_init(&rtp_session->dtmf_data.dtmf_mutex, SWITCH_MUTEX_NESTED, pool);
4361 	switch_queue_create(&rtp_session->dtmf_data.dtmf_queue, 100, rtp_session->pool);
4362 	switch_queue_create(&rtp_session->dtmf_data.dtmf_inqueue, 100, rtp_session->pool);
4363 
4364 	switch_rtp_set_flags(rtp_session, flags);
4365 
4366 	/* for from address on recvfrom calls */
4367 	switch_sockaddr_create(&rtp_session->from_addr, pool);
4368 	switch_sockaddr_create(&rtp_session->rtp_from_addr, pool);
4369 
4370 	if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
4371 		switch_sockaddr_create(&rtp_session->rtcp_from_addr, pool);
4372 	}
4373 	rtp_session->seq = (uint16_t) rand();
4374 	rtp_session->ssrc = (uint32_t) ((intptr_t) rtp_session + (uint32_t) switch_epoch_time_now(NULL));
4375 #ifdef DEBUG_TS_ROLLOVER
4376 	rtp_session->last_write_ts = TS_ROLLOVER_START;
4377 #endif
4378 	rtp_session->stats.inbound.R = 100.0;
4379 	rtp_session->stats.inbound.mos = 4.5;
4380 	rtp_session->send_msg.header.ssrc = htonl(rtp_session->ssrc);
4381 	rtp_session->send_msg.header.ts = 0;
4382 	rtp_session->send_msg.header.m = 0;
4383 	rtp_session->send_msg.header.pt = (switch_payload_t) htonl(payload);
4384 	rtp_session->send_msg.header.version = 2;
4385 	rtp_session->send_msg.header.p = 0;
4386 	rtp_session->send_msg.header.x = 0;
4387 	rtp_session->send_msg.header.cc = 0;
4388 
4389 	rtp_session->recv_msg.header.ssrc = 0;
4390 	rtp_session->recv_msg.header.ts = 0;
4391 	rtp_session->recv_msg.header.seq = 0;
4392 	rtp_session->recv_msg.header.m = 0;
4393 	rtp_session->recv_msg.header.pt = (switch_payload_t) htonl(payload);
4394 	rtp_session->recv_msg.header.version = 2;
4395 	rtp_session->recv_msg.header.p = 0;
4396 	rtp_session->recv_msg.header.x = 0;
4397 	rtp_session->recv_msg.header.cc = 0;
4398 
4399 	rtp_session->payload = payload;
4400 	rtp_session->rtcp_last_sent = switch_micro_time_now();
4401 
4402 	switch_rtp_set_interval(rtp_session, ms_per_packet, samples_per_interval);
4403 	rtp_session->conf_samples_per_interval = samples_per_interval;
4404 
4405 	if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && zstr(timer_name)) {
4406 		timer_name = "soft";
4407 	}
4408 
4409 	if (!zstr(timer_name) && !strcasecmp(timer_name, "none")) {
4410 		timer_name = NULL;
4411 	}
4412 
4413 	if (!zstr(timer_name)) {
4414 		rtp_session->timer_name = switch_core_strdup(pool, timer_name);
4415 		switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER);
4416 		switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
4417 
4418 		if (switch_core_timer_init(&rtp_session->timer, timer_name, ms_per_packet / 1000, samples_per_interval, pool) == SWITCH_STATUS_SUCCESS) {
4419 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,
4420 							  "Starting timer [%s] %d bytes per %dms\n", timer_name, samples_per_interval, ms_per_packet / 1000);
4421 			switch_core_timer_init(&rtp_session->write_timer, timer_name, (ms_per_packet / 1000), samples_per_interval, pool);
4422 #ifdef DEBUG_TS_ROLLOVER
4423 			rtp_session->timer.tick = TS_ROLLOVER_START / samples_per_interval;
4424 #endif
4425 		} else {
4426 			memset(&rtp_session->timer, 0, sizeof(rtp_session->timer));
4427 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR,
4428 							  "Error Starting timer [%s] %d bytes per %dms, async RTP disabled\n", timer_name, samples_per_interval, ms_per_packet / 1000);
4429 			switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER);
4430 		}
4431 	} else {
4432 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
4433 			if (switch_core_timer_init(&rtp_session->timer, "soft", 1, 90, pool) == SWITCH_STATUS_SUCCESS) {
4434 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Starting video timer.\n");
4435 			}
4436 
4437 			//switch_jb_create(&rtp_session->vb, 3, 10, rtp_session->pool);
4438 			//switch_jb_debug_level(rtp_session->vb, 10);
4439 
4440 		} else {
4441 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Not using a timer\n");
4442 		}
4443 
4444 		switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER);
4445 		switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
4446 	}
4447 
4448 
4449 	if (channel) {
4450 		switch_channel_set_private(channel, "__rtcp_audio_rtp_session", rtp_session);
4451 	}
4452 
4453 #ifdef ENABLE_ZRTP
4454 	if (zrtp_on && session && channel && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
4455 		switch_rtp_t *master_rtp_session = NULL;
4456 
4457 		int initiator = 0;
4458 		const char *zrtp_enabled = switch_channel_get_variable(channel, "zrtp_secure_media");
4459 		int srtp_enabled = switch_channel_test_flag(channel, CF_SECURE);
4460 
4461 		if (srtp_enabled && switch_true(zrtp_enabled)) {
4462 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING,
4463 							  "You can not have ZRTP and SRTP enabled simultaneously, ZRTP will be disabled for this call!\n");
4464 			switch_channel_set_variable(channel, "zrtp_secure_media", NULL);
4465 			zrtp_enabled = NULL;
4466 		}
4467 
4468 
4469 		if (switch_true(zrtp_enabled)) {
4470 			if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
4471 				switch_channel_set_private(channel, "__zrtp_video_rtp_session", rtp_session);
4472 				master_rtp_session = switch_channel_get_private(channel, "__zrtp_audio_rtp_session");
4473 			} else {
4474 				switch_channel_set_private(channel, "__zrtp_audio_rtp_session", rtp_session);
4475 				master_rtp_session = rtp_session;
4476 			}
4477 
4478 
4479 			if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
4480 				initiator = 1;
4481 			}
4482 
4483 			if (rtp_session == master_rtp_session) {
4484 				rtp_session->zrtp_profile = switch_core_alloc(rtp_session->pool, sizeof(*rtp_session->zrtp_profile));
4485 				zrtp_profile_defaults(rtp_session->zrtp_profile, zrtp_global);
4486 
4487 				rtp_session->zrtp_profile->allowclear = 0;
4488 				rtp_session->zrtp_profile->disclose_bit = 0;
4489 				rtp_session->zrtp_profile->cache_ttl = (uint32_t) -1;
4490 
4491 				if (zrtp_status_ok != zrtp_session_init(zrtp_global, rtp_session->zrtp_profile, zid, initiator, &rtp_session->zrtp_session)) {
4492 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error! zRTP INIT Failed\n");
4493 					zrtp_session_down(rtp_session->zrtp_session);
4494 					rtp_session->zrtp_session = NULL;
4495 					goto end;
4496 				}
4497 
4498 				zrtp_session_set_userdata(rtp_session->zrtp_session, session);
4499 
4500 
4501 				if (zrtp_status_ok != zrtp_stream_attach(master_rtp_session->zrtp_session, &rtp_session->zrtp_stream)) {
4502 					abort();
4503 				}
4504 
4505 				zrtp_stream_set_userdata(rtp_session->zrtp_stream, rtp_session);
4506 
4507 				if (switch_true(switch_channel_get_variable(channel, "zrtp_enrollment"))) {
4508 					zrtp_stream_registration_start(rtp_session->zrtp_stream, rtp_session->ssrc);
4509 				} else {
4510 					zrtp_stream_start(rtp_session->zrtp_stream, rtp_session->ssrc);
4511 				}
4512 			}
4513 
4514 		}
4515 	}
4516 
4517  end:
4518 
4519 #endif
4520 
4521 	/* Jitter */
4522 	rtp_session->stats.inbound.last_proc_time = switch_micro_time_now() / 1000;
4523 	rtp_session->stats.inbound.jitter_n = 0;
4524 	rtp_session->stats.inbound.jitter_add = 0;
4525 	rtp_session->stats.inbound.jitter_addsq = 0;
4526 	rtp_session->stats.inbound.min_variance = 0;
4527 	rtp_session->stats.inbound.max_variance = 0;
4528 
4529 	/* Burst and Packet Loss */
4530 	rtp_session->stats.inbound.lossrate = 0;
4531 	rtp_session->stats.inbound.burstrate = 0;
4532 	memset(rtp_session->stats.inbound.loss, 0, sizeof(rtp_session->stats.inbound.loss));
4533 	rtp_session->stats.inbound.last_loss = 0;
4534 	rtp_session->stats.inbound.last_processed_seq = -1;
4535 
4536 	rtp_session->ready = 1;
4537 	*new_rtp_session = rtp_session;
4538 
4539 	return SWITCH_STATUS_SUCCESS;
4540 }
4541 
switch_rtp_new(const char * rx_host,switch_port_t rx_port,const char * tx_host,switch_port_t tx_port,switch_payload_t payload,uint32_t samples_per_interval,uint32_t ms_per_packet,switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID],char * timer_name,const char ** err,switch_memory_pool_t * pool,switch_port_t bundle_internal_port,switch_port_t bundle_external_port)4542 SWITCH_DECLARE(switch_rtp_t *) switch_rtp_new(const char *rx_host,
4543 											  switch_port_t rx_port,
4544 											  const char *tx_host,
4545 											  switch_port_t tx_port,
4546 											  switch_payload_t payload,
4547 											  uint32_t samples_per_interval,
4548 											  uint32_t ms_per_packet,
4549 											  switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID], char *timer_name, const char **err, switch_memory_pool_t *pool,
4550                                               switch_port_t bundle_internal_port,
4551                                               switch_port_t bundle_external_port)
4552 {
4553 	switch_rtp_t *rtp_session = NULL;
4554 
4555 	if (zstr(rx_host)) {
4556 		*err = "Missing local host";
4557 		goto end;
4558 	}
4559 
4560 	if (!rx_port) {
4561 		*err = "Missing local port";
4562 		goto end;
4563 	}
4564 
4565 	if (zstr(tx_host)) {
4566 		*err = "Missing remote host";
4567 		goto end;
4568 	}
4569 
4570 	if (!tx_port) {
4571 		*err = "Missing remote port";
4572 		goto end;
4573 	}
4574 
4575 	if (switch_rtp_create(&rtp_session, payload, samples_per_interval, ms_per_packet, flags, timer_name, err, pool) != SWITCH_STATUS_SUCCESS) {
4576 		goto end;
4577 	}
4578 
4579 	switch_mutex_lock(rtp_session->flag_mutex);
4580 
4581 	if (switch_rtp_set_local_address(rtp_session, rx_host, rx_port, err) != SWITCH_STATUS_SUCCESS) {
4582 		switch_mutex_unlock(rtp_session->flag_mutex);
4583 		rtp_session = NULL;
4584 		goto end;
4585 	}
4586 
4587 	if (switch_rtp_set_remote_address(rtp_session, tx_host, tx_port, 0, SWITCH_TRUE, err) != SWITCH_STATUS_SUCCESS) {
4588 		switch_mutex_unlock(rtp_session->flag_mutex);
4589 		rtp_session = NULL;
4590 		goto end;
4591 	}
4592 
4593  end:
4594 
4595 	if (rtp_session) {
4596 		switch_mutex_unlock(rtp_session->flag_mutex);
4597 		rtp_session->ready = 2;
4598 		rtp_session->rx_host = switch_core_strdup(rtp_session->pool, rx_host);
4599 		rtp_session->rx_port = rx_port;
4600 		switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
4601 		switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_DETECT_SSRC);
4602 	} else {
4603 		switch_rtp_release_port(rx_host, rx_port);
4604 	}
4605 
4606 	return rtp_session;
4607 }
4608 
switch_rtp_set_telephony_event(switch_rtp_t * rtp_session,switch_payload_t te)4609 SWITCH_DECLARE(void) switch_rtp_set_telephony_event(switch_rtp_t *rtp_session, switch_payload_t te)
4610 {
4611 	if (te > 95) {
4612 		rtp_session->te = te;
4613 	}
4614 }
4615 
4616 
switch_rtp_set_telephony_recv_event(switch_rtp_t * rtp_session,switch_payload_t te)4617 SWITCH_DECLARE(void) switch_rtp_set_telephony_recv_event(switch_rtp_t *rtp_session, switch_payload_t te)
4618 {
4619 	if (te > 95) {
4620 		rtp_session->recv_te = te;
4621 	}
4622 }
4623 
4624 
switch_rtp_set_cng_pt(switch_rtp_t * rtp_session,switch_payload_t pt)4625 SWITCH_DECLARE(void) switch_rtp_set_cng_pt(switch_rtp_t *rtp_session, switch_payload_t pt)
4626 {
4627 	rtp_session->cng_pt = pt;
4628 	rtp_session->flags[SWITCH_RTP_FLAG_AUTO_CNG] = 1;
4629 }
4630 
switch_rtp_get_media_timer(switch_rtp_t * rtp_session)4631 SWITCH_DECLARE(switch_timer_t *) switch_rtp_get_media_timer(switch_rtp_t *rtp_session)
4632 {
4633 
4634 	if (rtp_session && rtp_session->timer.timer_interface) {
4635 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
4636 			switch_core_timer_sync(&rtp_session->timer);
4637 		}
4638 		return &rtp_session->timer;
4639 	}
4640 
4641 	return NULL;
4642 }
4643 
4644 
switch_rtp_get_jitter_buffer(switch_rtp_t * rtp_session)4645 SWITCH_DECLARE(switch_jb_t *) switch_rtp_get_jitter_buffer(switch_rtp_t *rtp_session)
4646 {
4647 	if (!switch_rtp_ready(rtp_session)) {
4648 		return NULL;
4649 	}
4650 
4651 	return rtp_session->jb ? rtp_session->jb : rtp_session->vb;
4652 }
4653 
switch_rtp_pause_jitter_buffer(switch_rtp_t * rtp_session,switch_bool_t pause)4654 SWITCH_DECLARE(switch_status_t) switch_rtp_pause_jitter_buffer(switch_rtp_t *rtp_session, switch_bool_t pause)
4655 {
4656 	int new_val;
4657 
4658 	if (rtp_session->pause_jb && !pause) {
4659 		if (rtp_session->jb) {
4660 			switch_jb_reset(rtp_session->jb);
4661 		}
4662 
4663 		if (rtp_session->vb) {
4664 			switch_jb_reset(rtp_session->vb);
4665 		}
4666 	}
4667 
4668 	new_val = pause ? 1 : -1;
4669 
4670 	if (rtp_session->pause_jb + new_val > -1) {
4671 		rtp_session->pause_jb += new_val;
4672 	}
4673 
4674 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1,
4675 					  "Jitterbuffer %s is %s\n", rtp_type(rtp_session), rtp_session->pause_jb ? "paused" : "enabled");
4676 
4677 	return SWITCH_STATUS_SUCCESS;
4678 }
4679 
switch_rtp_deactivate_jitter_buffer(switch_rtp_t * rtp_session)4680 SWITCH_DECLARE(switch_status_t) switch_rtp_deactivate_jitter_buffer(switch_rtp_t *rtp_session)
4681 {
4682 
4683 	if (!switch_rtp_ready(rtp_session)) {
4684 		return SWITCH_STATUS_FALSE;
4685 	}
4686 
4687 	rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB]++;
4688 
4689 	return SWITCH_STATUS_SUCCESS;
4690 }
4691 
switch_rtp_get_video_buffer_size(switch_rtp_t * rtp_session,uint32_t * min_frame_len,uint32_t * max_frame_len,uint32_t * cur_frame_len,uint32_t * highest_frame_len)4692 SWITCH_DECLARE(switch_status_t) switch_rtp_get_video_buffer_size(switch_rtp_t *rtp_session, uint32_t *min_frame_len, uint32_t *max_frame_len, uint32_t *cur_frame_len, uint32_t *highest_frame_len)
4693 {
4694 
4695 	if (rtp_session->vb) {
4696 		return switch_jb_get_frames(rtp_session->vb, min_frame_len, max_frame_len, cur_frame_len, highest_frame_len);
4697 	}
4698 
4699 	return SWITCH_STATUS_FALSE;
4700 }
4701 
switch_rtp_set_video_buffer_size(switch_rtp_t * rtp_session,uint32_t frames,uint32_t max_frames)4702 SWITCH_DECLARE(switch_status_t) switch_rtp_set_video_buffer_size(switch_rtp_t *rtp_session, uint32_t frames, uint32_t max_frames)
4703 {
4704 	if (!switch_rtp_ready(rtp_session)) {
4705 		return SWITCH_STATUS_FALSE;
4706 	}
4707 
4708 	if (!max_frames) {
4709 		max_frames = rtp_session->last_max_vb_frames;
4710 	}
4711 
4712 	if (!max_frames || frames >= max_frames) {
4713 		max_frames = frames * 10;
4714 	}
4715 
4716 	rtp_session->last_max_vb_frames = max_frames;
4717 
4718 	if (!rtp_session->vb) {
4719 		switch_jb_create(&rtp_session->vb, rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? SJB_TEXT : SJB_VIDEO, frames, max_frames, rtp_session->pool);
4720 		switch_jb_set_session(rtp_session->vb, rtp_session->session);
4721 	} else {
4722 		switch_jb_set_frames(rtp_session->vb, frames, max_frames);
4723 	}
4724 
4725 	//switch_rtp_flush(rtp_session);
4726 	switch_core_session_request_video_refresh(rtp_session->session);
4727 
4728 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Setting video buffer %u Frames.\n", frames);
4729 
4730 	return SWITCH_STATUS_SUCCESS;
4731 }
4732 
switch_rtp_debug_jitter_buffer(switch_rtp_t * rtp_session,const char * name)4733 SWITCH_DECLARE(switch_status_t) switch_rtp_debug_jitter_buffer(switch_rtp_t *rtp_session, const char *name)
4734 {
4735 	int x = 0;
4736 
4737 	if (!switch_rtp_ready(rtp_session)) {
4738 		return SWITCH_STATUS_FALSE;
4739 	}
4740 
4741 	if (name) x = atoi(name);
4742 	if (x < 0) x = 0;
4743 
4744 	if (rtp_session->jb) {
4745 		switch_jb_debug_level(rtp_session->jb, x);
4746 	} else if (rtp_session->vb) {
4747 		switch_jb_debug_level(rtp_session->vb, x);
4748 	}
4749 
4750 	return SWITCH_STATUS_SUCCESS;
4751 }
4752 
switch_rtp_activate_jitter_buffer(switch_rtp_t * rtp_session,uint32_t queue_frames,uint32_t max_queue_frames,uint32_t samples_per_packet,uint32_t samples_per_second)4753 SWITCH_DECLARE(switch_status_t) switch_rtp_activate_jitter_buffer(switch_rtp_t *rtp_session,
4754 																  uint32_t queue_frames,
4755 																  uint32_t max_queue_frames,
4756 																  uint32_t samples_per_packet,
4757 																  uint32_t samples_per_second)
4758 {
4759 	switch_status_t status = SWITCH_STATUS_FALSE;
4760 
4761 	if (!switch_rtp_ready(rtp_session)) {
4762 		return SWITCH_STATUS_FALSE;
4763 	}
4764 
4765 	if (queue_frames < 1) {
4766 		queue_frames = 3;
4767 	}
4768 
4769 	if (max_queue_frames < queue_frames) {
4770 		max_queue_frames = queue_frames * 3;
4771 	}
4772 
4773 
4774 
4775 	if (rtp_session->jb) {
4776 		status = switch_jb_set_frames(rtp_session->jb, queue_frames, max_queue_frames);
4777 	} else {
4778 		READ_INC(rtp_session);
4779 		status = switch_jb_create(&rtp_session->jb, SJB_AUDIO, queue_frames, max_queue_frames, rtp_session->pool);
4780 		switch_jb_set_session(rtp_session->jb, rtp_session->session);
4781 		if (switch_true(switch_channel_get_variable_dup(switch_core_session_get_channel(rtp_session->session), "jb_use_timestamps", SWITCH_FALSE, -1))) {
4782 			switch_jb_ts_mode(rtp_session->jb, samples_per_packet, samples_per_second);
4783 		}
4784 		//switch_jb_debug_level(rtp_session->jb, 10);
4785 		READ_DEC(rtp_session);
4786 	}
4787 
4788 
4789 
4790 	return status;
4791 }
4792 
switch_rtp_activate_rtcp(switch_rtp_t * rtp_session,int send_rate,switch_port_t remote_port,switch_bool_t mux)4793 SWITCH_DECLARE(switch_status_t) switch_rtp_activate_rtcp(switch_rtp_t *rtp_session, int send_rate, switch_port_t remote_port, switch_bool_t mux)
4794 {
4795 	const char *err = NULL;
4796 
4797 	if (!rtp_session->ms_per_packet) {
4798 		return SWITCH_STATUS_FALSE;
4799 	}
4800 
4801 	rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] = 1;
4802 
4803 	if (!(rtp_session->remote_rtcp_port = remote_port)) {
4804 		rtp_session->remote_rtcp_port = rtp_session->remote_port + 1;
4805 	}
4806 
4807 	if (mux) {
4808 		rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]++;
4809 	}
4810 
4811 
4812 	if (send_rate == -1) {
4813 
4814 		rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] = 1;
4815 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "RTCP passthru enabled. Remote Port: %d\n", rtp_session->remote_rtcp_port);
4816 	} else {
4817 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "RTCP send rate is: %d and packet rate is: %d Remote Port: %d\n", 						  send_rate, rtp_session->ms_per_packet, rtp_session->remote_rtcp_port);
4818 
4819 		rtp_session->rtcp_interval = send_rate;
4820 	}
4821 
4822 	if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
4823 
4824 		if (switch_sockaddr_info_get(&rtp_session->rtcp_remote_addr, rtp_session->eff_remote_host_str, SWITCH_UNSPEC,
4825 									 rtp_session->remote_rtcp_port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !rtp_session->rtcp_remote_addr) {
4826 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "RTCP MUX Remote Address Error!");
4827 			return SWITCH_STATUS_FALSE;
4828 		}
4829 
4830 		rtp_session->rtcp_local_addr = rtp_session->local_addr;
4831 		rtp_session->rtcp_from_addr = rtp_session->from_addr;
4832 		rtp_session->rtcp_sock_input = rtp_session->sock_input;
4833 		rtp_session->rtcp_sock_output = rtp_session->sock_output;
4834 
4835 		rtp_session->rtcp_recv_msg_p = (rtcp_msg_t *) &rtp_session->recv_msg;
4836 
4837 		return SWITCH_STATUS_SUCCESS;
4838 
4839 		//return enable_remote_rtcp_socket(rtp_session, &err);
4840 	} else {
4841 		rtp_session->rtcp_recv_msg_p = (rtcp_msg_t *) &rtp_session->rtcp_recv_msg;
4842 	}
4843 
4844 	return enable_local_rtcp_socket(rtp_session, &err) || enable_remote_rtcp_socket(rtp_session, &err);
4845 
4846 }
4847 
switch_rtp_activate_ice(switch_rtp_t * rtp_session,char * login,char * rlogin,const char * password,const char * rpassword,ice_proto_t proto,switch_core_media_ice_type_t type,ice_t * ice_params)4848 SWITCH_DECLARE(switch_status_t) switch_rtp_activate_ice(switch_rtp_t *rtp_session, char *login, char *rlogin,
4849 														const char *password, const char *rpassword, ice_proto_t proto,
4850 														switch_core_media_ice_type_t type, ice_t *ice_params)
4851 {
4852 	char ice_user[STUN_USERNAME_MAX_SIZE];
4853 	char user_ice[STUN_USERNAME_MAX_SIZE];
4854 	char luser_ice[SDP_UFRAG_MAX_SIZE];
4855 	switch_rtp_ice_t *ice;
4856 	char *host = NULL;
4857 	switch_port_t port = 0;
4858 	char bufc[50];
4859 
4860 
4861 	switch_mutex_lock(rtp_session->ice_mutex);
4862 
4863 	if (proto == IPR_RTP) {
4864 		ice = &rtp_session->ice;
4865 		rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] = 0;
4866 		rtp_session->flags[SWITCH_RTP_FLAG_MUTE] = 0;
4867 	} else {
4868 		ice = &rtp_session->rtcp_ice;
4869 	}
4870 
4871 	ice->proto = proto;
4872 
4873 	if ((type & ICE_VANILLA)) {
4874 		switch_snprintf(ice_user, sizeof(ice_user), "%s:%s", login, rlogin);
4875 		switch_snprintf(user_ice, sizeof(user_ice), "%s:%s", rlogin, login);
4876 		switch_snprintf(luser_ice, sizeof(user_ice), "%s%s", rlogin, login);
4877 		ice->ready = ice->rready = 0;
4878 	} else {
4879 		switch_snprintf(ice_user, sizeof(ice_user), "%s%s", login, rlogin);
4880 		switch_snprintf(user_ice, sizeof(user_ice), "%s%s", rlogin, login);
4881 		ice->ready = ice->rready = 1;
4882 	}
4883 
4884 	ice->ice_user = switch_core_strdup(rtp_session->pool, ice_user);
4885 	ice->user_ice = switch_core_strdup(rtp_session->pool, user_ice);
4886 	ice->luser_ice = switch_core_strdup(rtp_session->pool, luser_ice);
4887 	ice->type = type;
4888 	ice->ice_params = ice_params;
4889 	ice->pass = "";
4890 	ice->rpass = "";
4891 	ice->next_run = switch_micro_time_now();
4892 
4893 	if (password) {
4894 		ice->pass = switch_core_strdup(rtp_session->pool, password);
4895 	}
4896 
4897 	if (rpassword) {
4898 		ice->rpass = switch_core_strdup(rtp_session->pool, rpassword);
4899 	}
4900 
4901 	if ((ice->type & ICE_VANILLA) && ice->ice_params) {
4902 		host = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr;
4903 		port = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port;
4904 
4905 		if (!host || !port || switch_sockaddr_info_get(&ice->addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !ice->addr) {
4906 			switch_mutex_unlock(rtp_session->ice_mutex);
4907 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error setting remote host!\n");
4908 			return SWITCH_STATUS_FALSE;
4909 		}
4910 	} else {
4911 		if (proto == IPR_RTP) {
4912 			ice->addr = rtp_session->remote_addr;
4913 		} else {
4914 			ice->addr = rtp_session->rtcp_remote_addr;
4915 		}
4916 
4917 		host = (char *)switch_get_addr(bufc, sizeof(bufc), ice->addr);
4918 		port = switch_sockaddr_get_port(ice->addr);
4919 	}
4920 
4921 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_NOTICE, "Activating %s %s ICE: %s %s:%d\n",
4922 					  proto == IPR_RTP ? "RTP" : "RTCP", rtp_type(rtp_session), ice_user, host, port);
4923 
4924 
4925 	rtp_session->rtp_bugs |= RTP_BUG_ACCEPT_ANY_PACKETS;
4926 
4927 
4928 	if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
4929 		rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK] = 1;
4930 		switch_rtp_break(rtp_session);
4931 	}
4932 
4933 	switch_mutex_unlock(rtp_session->ice_mutex);
4934 
4935 	return SWITCH_STATUS_SUCCESS;
4936 }
4937 
4938 
switch_rtp_flush(switch_rtp_t * rtp_session)4939 SWITCH_DECLARE(void) switch_rtp_flush(switch_rtp_t *rtp_session)
4940 {
4941 	if (!switch_rtp_ready(rtp_session)) {
4942 		return;
4943 	}
4944 
4945 	switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
4946 }
4947 
switch_rtp_req_bitrate(switch_rtp_t * rtp_session,uint32_t bps)4948 SWITCH_DECLARE(switch_status_t) switch_rtp_req_bitrate(switch_rtp_t *rtp_session, uint32_t bps)
4949 {
4950 	if (!rtp_write_ready(rtp_session, 0, __LINE__) || rtp_session->tmmbr) {
4951 		return SWITCH_STATUS_FALSE;
4952 	}
4953 
4954 	rtp_session->tmmbr = bps;
4955 
4956 	return SWITCH_STATUS_SUCCESS;
4957 }
4958 
switch_rtp_ack_bitrate(switch_rtp_t * rtp_session,uint32_t bps)4959 SWITCH_DECLARE(switch_status_t) switch_rtp_ack_bitrate(switch_rtp_t *rtp_session, uint32_t bps)
4960 {
4961 	if (!rtp_write_ready(rtp_session, 0, __LINE__) || rtp_session->tmmbn) {
4962 		return SWITCH_STATUS_FALSE;
4963 	}
4964 
4965 	rtp_session->tmmbn = bps;
4966 
4967 	return SWITCH_STATUS_SUCCESS;
4968 }
4969 
switch_rtp_video_refresh(switch_rtp_t * rtp_session)4970 SWITCH_DECLARE(void) switch_rtp_video_refresh(switch_rtp_t *rtp_session)
4971 {
4972 
4973 	if (!rtp_write_ready(rtp_session, 0, __LINE__)) {
4974 		return;
4975 	}
4976 
4977 	if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (rtp_session->ice.ice_user || rtp_session->flags[SWITCH_RTP_FLAG_FIR] || rtp_session->flags[SWITCH_RTP_FLAG_OLD_FIR])) {
4978 		rtp_session->fir_count = 1;
4979 	}
4980 }
4981 
switch_rtp_video_loss(switch_rtp_t * rtp_session)4982 SWITCH_DECLARE(void) switch_rtp_video_loss(switch_rtp_t *rtp_session)
4983 {
4984 
4985 	if (!rtp_write_ready(rtp_session, 0, __LINE__)) {
4986 		return;
4987 	}
4988 
4989 	if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (rtp_session->ice.ice_user || rtp_session->flags[SWITCH_RTP_FLAG_PLI])) {
4990 		rtp_session->pli_count = 1;
4991 	}
4992 }
4993 
switch_rtp_break(switch_rtp_t * rtp_session)4994 SWITCH_DECLARE(void) switch_rtp_break(switch_rtp_t *rtp_session)
4995 {
4996 	if (!switch_rtp_ready(rtp_session)) {
4997 		return;
4998 	}
4999 
5000 	if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5001 		int ret = 1;
5002 
5003 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK]) {
5004 			rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK] = 0;
5005 			ret = 0;
5006 		} else if (rtp_session->session) {
5007 			switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
5008 			if (switch_channel_test_flag(channel, CF_VIDEO_BREAK)) {
5009 				switch_channel_clear_flag(channel, CF_VIDEO_BREAK);
5010 				ret = 0;
5011 			}
5012 		}
5013 
5014 		if (ret) return;
5015 
5016 		switch_rtp_video_refresh(rtp_session);
5017 	}
5018 
5019 	switch_mutex_lock(rtp_session->flag_mutex);
5020 	rtp_session->flags[SWITCH_RTP_FLAG_BREAK] = 1;
5021 
5022 	if (rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK]) {
5023 		switch_mutex_unlock(rtp_session->flag_mutex);
5024 		return;
5025 	}
5026 
5027 	if (rtp_session->sock_input) {
5028 		ping_socket(rtp_session);
5029 	}
5030 
5031 	switch_mutex_unlock(rtp_session->flag_mutex);
5032 }
5033 
switch_rtp_kill_socket(switch_rtp_t * rtp_session)5034 SWITCH_DECLARE(void) switch_rtp_kill_socket(switch_rtp_t *rtp_session)
5035 {
5036 	switch_assert(rtp_session != NULL);
5037 	switch_mutex_lock(rtp_session->flag_mutex);
5038 	if (rtp_session->flags[SWITCH_RTP_FLAG_IO]) {
5039 		rtp_session->flags[SWITCH_RTP_FLAG_IO] = 0;
5040 		if (rtp_session->sock_input) {
5041 			ping_socket(rtp_session);
5042 			switch_socket_shutdown(rtp_session->sock_input, SWITCH_SHUTDOWN_READWRITE);
5043 		}
5044 		if (rtp_session->sock_output && rtp_session->sock_output != rtp_session->sock_input) {
5045 			switch_socket_shutdown(rtp_session->sock_output, SWITCH_SHUTDOWN_READWRITE);
5046 		}
5047 
5048 		if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
5049 			if (rtp_session->rtcp_sock_input) {
5050 				ping_socket(rtp_session);
5051 				switch_socket_shutdown(rtp_session->rtcp_sock_input, SWITCH_SHUTDOWN_READWRITE);
5052 			}
5053 			if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) {
5054 				switch_socket_shutdown(rtp_session->rtcp_sock_output, SWITCH_SHUTDOWN_READWRITE);
5055 			}
5056 		}
5057 	}
5058 	switch_mutex_unlock(rtp_session->flag_mutex);
5059 }
5060 
switch_rtp_ready(switch_rtp_t * rtp_session)5061 SWITCH_DECLARE(uint8_t) switch_rtp_ready(switch_rtp_t *rtp_session)
5062 {
5063 	uint8_t ret;
5064 
5065 	if (!rtp_session || !rtp_session->flag_mutex || rtp_session->flags[SWITCH_RTP_FLAG_SHUTDOWN]) {
5066 		return 0;
5067 	}
5068 
5069 	switch_mutex_lock(rtp_session->flag_mutex);
5070 	ret = (rtp_session->flags[SWITCH_RTP_FLAG_IO] && rtp_session->sock_input && rtp_session->sock_output && rtp_session->remote_addr
5071 		   && rtp_session->ready == 2) ? 1 : 0;
5072 	switch_mutex_unlock(rtp_session->flag_mutex);
5073 
5074 	return ret;
5075 }
5076 
switch_rtp_sync_stats(switch_rtp_t * rtp_session)5077 SWITCH_DECLARE(switch_status_t) switch_rtp_sync_stats(switch_rtp_t *rtp_session)
5078 {
5079 	if (!rtp_session) {
5080 		return SWITCH_STATUS_FALSE;
5081 	}
5082 
5083 	if (rtp_session->flags[SWITCH_RTP_FLAG_VAD]) {
5084 		switch_channel_t *channel = switch_core_session_get_channel(rtp_session->vad_data.session);
5085 
5086 		switch_channel_set_variable_printf(channel, "vad_total_talk_time_ms", "%u", (uint32_t)rtp_session->vad_data.total_talk_time / 1000);
5087 		switch_channel_set_variable_printf(channel, "vad_total_talk_time_sec", "%u", (uint32_t)rtp_session->vad_data.total_talk_time / 1000000);
5088 	}
5089 
5090 	do_mos(rtp_session);
5091 
5092 	if (rtp_session->stats.inbound.error_log && !rtp_session->stats.inbound.error_log->stop) {
5093 		rtp_session->stats.inbound.error_log->stop = switch_micro_time_now();
5094 	}
5095 
5096 	return SWITCH_STATUS_SUCCESS;
5097 }
5098 
5099 
switch_rtp_destroy(switch_rtp_t ** rtp_session)5100 SWITCH_DECLARE(void) switch_rtp_destroy(switch_rtp_t **rtp_session)
5101 {
5102 	void *pop;
5103 	switch_socket_t *sock;
5104 #ifdef ENABLE_SRTP
5105 	int x;
5106 #endif
5107 
5108 	if (!rtp_session || !*rtp_session || !(*rtp_session)->ready) {
5109 		return;
5110 	}
5111 
5112 	if ((*rtp_session)->vb) {
5113 		/* retrieve counter for ALL received NACKed packets */
5114 		uint32_t nack_jb_ok = switch_jb_get_nack_success((*rtp_session)->vb);
5115 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG((*rtp_session)->session), SWITCH_LOG_DEBUG,
5116 				"NACK: Added to JB: [%u]\n", nack_jb_ok);
5117 	}
5118 
5119 	(*rtp_session)->flags[SWITCH_RTP_FLAG_SHUTDOWN] = 1;
5120 
5121 	READ_INC((*rtp_session));
5122 	WRITE_INC((*rtp_session));
5123 
5124 	(*rtp_session)->ready = 0;
5125 
5126 	WRITE_DEC((*rtp_session));
5127 	READ_DEC((*rtp_session));
5128 
5129 	if ((*rtp_session)->flags[SWITCH_RTP_FLAG_VAD]) {
5130 		switch_rtp_disable_vad(*rtp_session);
5131 	}
5132 
5133 	switch_mutex_lock((*rtp_session)->flag_mutex);
5134 
5135 	switch_rtp_kill_socket(*rtp_session);
5136 
5137 	while (switch_queue_trypop((*rtp_session)->dtmf_data.dtmf_inqueue, &pop) == SWITCH_STATUS_SUCCESS) {
5138 		switch_safe_free(pop);
5139 	}
5140 
5141 	while (switch_queue_trypop((*rtp_session)->dtmf_data.dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
5142 		switch_safe_free(pop);
5143 	}
5144 
5145 	if ((*rtp_session)->jb) {
5146 		switch_jb_destroy(&(*rtp_session)->jb);
5147 	}
5148 
5149 	if ((*rtp_session)->vb) {
5150 		switch_jb_destroy(&(*rtp_session)->vb);
5151 	}
5152 
5153 	if ((*rtp_session)->vbw) {
5154 		switch_jb_destroy(&(*rtp_session)->vbw);
5155 	}
5156 
5157 	if ((*rtp_session)->dtls && (*rtp_session)->dtls == (*rtp_session)->rtcp_dtls) {
5158 		(*rtp_session)->rtcp_dtls = NULL;
5159 	}
5160 
5161 	if ((*rtp_session)->dtls) {
5162 		free_dtls(&(*rtp_session)->dtls);
5163 	}
5164 
5165 	if ((*rtp_session)->rtcp_dtls) {
5166 		free_dtls(&(*rtp_session)->rtcp_dtls);
5167 	}
5168 
5169 
5170 	sock = (*rtp_session)->sock_input;
5171 	(*rtp_session)->sock_input = NULL;
5172 	switch_socket_close(sock);
5173 
5174 	if ((*rtp_session)->sock_output != sock) {
5175 		sock = (*rtp_session)->sock_output;
5176 		(*rtp_session)->sock_output = NULL;
5177 		switch_socket_close(sock);
5178 	}
5179 
5180 	if ((sock = (*rtp_session)->rtcp_sock_input)) {
5181 		(*rtp_session)->rtcp_sock_input = NULL;
5182 		switch_socket_close(sock);
5183 
5184 		if ((*rtp_session)->rtcp_sock_output && (*rtp_session)->rtcp_sock_output != sock) {
5185 			if ((sock = (*rtp_session)->rtcp_sock_output)) {
5186 				(*rtp_session)->rtcp_sock_output = NULL;
5187 				switch_socket_close(sock);
5188 			}
5189 		}
5190 	}
5191 
5192 #ifdef ENABLE_SRTP
5193 	if ((*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
5194 		for(x = 0; x < 2; x++) {
5195 			if ((*rtp_session)->send_ctx[x]) {
5196 				srtp_dealloc((*rtp_session)->send_ctx[x]);
5197 				(*rtp_session)->send_ctx[x] = NULL;
5198 			}
5199 		}
5200 		(*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0;
5201 	}
5202 
5203 	if ((*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_RECV]) {
5204 		for (x = 0; x < 2; x++) {
5205 			if ((*rtp_session)->recv_ctx[x]) {
5206 				srtp_dealloc((*rtp_session)->recv_ctx[x]);
5207 				(*rtp_session)->recv_ctx[x] = NULL;
5208 			}
5209 		}
5210 		(*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
5211 	}
5212 #endif
5213 
5214 #ifdef ENABLE_ZRTP
5215 	/* ZRTP */
5216 	if (zrtp_on && !(*rtp_session)->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
5217 
5218 		if ((*rtp_session)->zrtp_stream != NULL) {
5219 			zrtp_stream_stop((*rtp_session)->zrtp_stream);
5220 		}
5221 
5222 		if ((*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_SEND]) {
5223 			(*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_SEND] = 0;
5224 		}
5225 
5226 		if ((*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_RECV]) {
5227 			(*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_RECV] = 0;
5228 		}
5229 
5230 		if ((*rtp_session)->zrtp_session) {
5231 			zrtp_session_down((*rtp_session)->zrtp_session);
5232 			(*rtp_session)->zrtp_session = NULL;
5233 		}
5234 	}
5235 #endif
5236 	if ((*rtp_session)->timer.timer_interface) {
5237 		switch_core_timer_destroy(&(*rtp_session)->timer);
5238 	}
5239 
5240 	if ((*rtp_session)->write_timer.timer_interface) {
5241 		switch_core_timer_destroy(&(*rtp_session)->write_timer);
5242 	}
5243 
5244 	switch_rtp_release_port((*rtp_session)->rx_host, (*rtp_session)->rx_port);
5245 	switch_mutex_unlock((*rtp_session)->flag_mutex);
5246 
5247 	return;
5248 }
5249 
switch_rtp_set_interdigit_delay(switch_rtp_t * rtp_session,uint32_t delay)5250 SWITCH_DECLARE(void) switch_rtp_set_interdigit_delay(switch_rtp_t *rtp_session, uint32_t delay)
5251 {
5252 	rtp_session->interdigit_delay = delay;
5253 }
5254 
switch_rtp_get_rtp_socket(switch_rtp_t * rtp_session)5255 SWITCH_DECLARE(switch_socket_t *) switch_rtp_get_rtp_socket(switch_rtp_t *rtp_session)
5256 {
5257 	return rtp_session->sock_input;
5258 }
5259 
switch_rtp_get_default_samples_per_interval(switch_rtp_t * rtp_session)5260 SWITCH_DECLARE(uint32_t) switch_rtp_get_default_samples_per_interval(switch_rtp_t *rtp_session)
5261 {
5262 	return rtp_session->samples_per_interval;
5263 }
5264 
switch_rtp_set_default_payload(switch_rtp_t * rtp_session,switch_payload_t payload)5265 SWITCH_DECLARE(void) switch_rtp_set_default_payload(switch_rtp_t *rtp_session, switch_payload_t payload)
5266 {
5267 	rtp_session->payload = payload;
5268 }
5269 
switch_rtp_get_default_payload(switch_rtp_t * rtp_session)5270 SWITCH_DECLARE(uint32_t) switch_rtp_get_default_payload(switch_rtp_t *rtp_session)
5271 {
5272 	return rtp_session->payload;
5273 }
5274 
switch_rtp_set_invalid_handler(switch_rtp_t * rtp_session,switch_rtp_invalid_handler_t on_invalid)5275 SWITCH_DECLARE(void) switch_rtp_set_invalid_handler(switch_rtp_t *rtp_session, switch_rtp_invalid_handler_t on_invalid)
5276 {
5277 	rtp_session->invalid_handler = on_invalid;
5278 }
5279 
switch_rtp_set_flags(switch_rtp_t * rtp_session,switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID])5280 SWITCH_DECLARE(void) switch_rtp_set_flags(switch_rtp_t *rtp_session, switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID])
5281 {
5282 	int i;
5283 
5284 	for(i = 0; i < SWITCH_RTP_FLAG_INVALID; i++) {
5285 		if (flags[i]) {
5286 			switch_rtp_set_flag(rtp_session, i);
5287 		}
5288 	}
5289 }
5290 
switch_rtp_clear_flags(switch_rtp_t * rtp_session,switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID])5291 SWITCH_DECLARE(void) switch_rtp_clear_flags(switch_rtp_t *rtp_session, switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID])
5292 {
5293 	int i;
5294 
5295 	for(i = 0; i < SWITCH_RTP_FLAG_INVALID; i++) {
5296 		if (flags[i]) {
5297 			switch_rtp_clear_flag(rtp_session, i);
5298 		}
5299 	}
5300 }
5301 
switch_rtp_set_flag(switch_rtp_t * rtp_session,switch_rtp_flag_t flag)5302 SWITCH_DECLARE(void) switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
5303 {
5304 	int old_flag = rtp_session->flags[flag];
5305 
5306 	switch_mutex_lock(rtp_session->flag_mutex);
5307 	rtp_session->flags[flag] = 1;
5308 	switch_mutex_unlock(rtp_session->flag_mutex);
5309 
5310 	if (flag == SWITCH_RTP_FLAG_PASSTHRU) {
5311 		if (!old_flag) {
5312 			switch_rtp_pause_jitter_buffer(rtp_session, SWITCH_TRUE);
5313 		}
5314 	} else if (flag == SWITCH_RTP_FLAG_DTMF_ON) {
5315 		rtp_session->stats.inbound.last_processed_seq = 0;
5316 	} else if (flag == SWITCH_RTP_FLAG_FLUSH) {
5317 		reset_jitter_seq(rtp_session);
5318 	} else if (flag == SWITCH_RTP_FLAG_AUTOADJ) {
5319 		rtp_session->autoadj_window = 20;
5320 		rtp_session->autoadj_threshold = 10;
5321 		rtp_session->autoadj_tally = 0;
5322 
5323 		switch_mutex_lock(rtp_session->flag_mutex);
5324 		rtp_session->flags[SWITCH_RTP_FLAG_RTCP_AUTOADJ] = 1;
5325 		switch_mutex_unlock(rtp_session->flag_mutex);
5326 
5327 		rtp_session->rtcp_autoadj_window = 20;
5328 		rtp_session->rtcp_autoadj_threshold = 1;
5329 		rtp_session->rtcp_autoadj_tally = 0;
5330 
5331 		if (rtp_session->session) {
5332 			switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
5333 			const char *x = switch_channel_get_variable(channel, "rtp_auto_adjust_threshold");
5334 			if (x && *x) {
5335 				int xn = atoi(x);
5336 				if (xn > 0 && xn <= 65535) {
5337 					rtp_session->autoadj_window = xn*2;
5338 					rtp_session->autoadj_threshold = xn;
5339 				}
5340 			}
5341 		}
5342 
5343 
5344 		rtp_flush_read_buffer(rtp_session, SWITCH_RTP_FLUSH_ONCE);
5345 
5346 
5347 		if (rtp_session->jb) {
5348 			switch_jb_reset(rtp_session->jb);
5349 		}
5350 	} else if (flag == SWITCH_RTP_FLAG_NOBLOCK && rtp_session->sock_input) {
5351 		switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK, TRUE);
5352 	}
5353 
5354 }
5355 
switch_rtp_test_flag(switch_rtp_t * rtp_session,switch_rtp_flag_t flags)5356 SWITCH_DECLARE(uint32_t) switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
5357 {
5358 	return (uint32_t) rtp_session->flags[flags];
5359 }
5360 
switch_rtp_clear_flag(switch_rtp_t * rtp_session,switch_rtp_flag_t flag)5361 SWITCH_DECLARE(void) switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
5362 {
5363 	int old_flag = rtp_session->flags[flag];
5364 
5365 	switch_mutex_lock(rtp_session->flag_mutex);
5366 	rtp_session->flags[flag] = 0;
5367 	switch_mutex_unlock(rtp_session->flag_mutex);
5368 
5369 	if (flag == SWITCH_RTP_FLAG_PASSTHRU) {
5370 		if (old_flag) {
5371 			switch_rtp_pause_jitter_buffer(rtp_session, SWITCH_FALSE);
5372 		}
5373 	} else if (flag == SWITCH_RTP_FLAG_DTMF_ON) {
5374 		rtp_session->stats.inbound.last_processed_seq = 0;
5375 	} else if (flag == SWITCH_RTP_FLAG_PAUSE) {
5376 		reset_jitter_seq(rtp_session);
5377 	} else if (flag == SWITCH_RTP_FLAG_NOBLOCK && rtp_session->sock_input) {
5378 		switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK, FALSE);
5379 	}
5380 }
5381 
set_dtmf_delay(switch_rtp_t * rtp_session,uint32_t ms,uint32_t max_ms)5382 static void set_dtmf_delay(switch_rtp_t *rtp_session, uint32_t ms, uint32_t max_ms)
5383 {
5384 	int upsamp, max_upsamp;
5385 
5386 
5387 	if (!max_ms) max_ms = ms;
5388 
5389 	upsamp = ms * (rtp_session->samples_per_second / 1000);
5390 	max_upsamp = max_ms * (rtp_session->samples_per_second / 1000);
5391 
5392 	rtp_session->sending_dtmf = 0;
5393 	rtp_session->queue_delay = upsamp;
5394 
5395 	if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
5396 		rtp_session->max_next_write_samplecount = rtp_session->timer.samplecount + max_upsamp;
5397 		rtp_session->next_write_samplecount = rtp_session->timer.samplecount + upsamp;
5398 		rtp_session->last_write_ts += upsamp;
5399 	}
5400 
5401 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Queue digit delay of %dms\n", ms);
5402 }
5403 
do_2833(switch_rtp_t * rtp_session)5404 static void do_2833(switch_rtp_t *rtp_session)
5405 {
5406 	switch_frame_flag_t flags = 0;
5407 	uint32_t samples = rtp_session->samples_per_interval;
5408 
5409 	if (rtp_session->dtmf_data.out_digit_dur > 0) {
5410 		int x, loops = 1;
5411 
5412 		if (!rtp_session->last_write_ts) {
5413 			if (rtp_session->timer.timer_interface) {
5414 				//switch_core_timer_sync(&rtp_session->write_timer);
5415 				rtp_session->last_write_ts = rtp_session->write_timer.samplecount;
5416 			} else {
5417 				rtp_session->last_write_ts = rtp_session->samples_per_interval;
5418 			}
5419 		}
5420 
5421 		rtp_session->dtmf_data.out_digit_sofar += samples;
5422 		rtp_session->dtmf_data.out_digit_sub_sofar += samples;
5423 
5424 		if (rtp_session->dtmf_data.out_digit_sub_sofar > 0xFFFF) {
5425 			rtp_session->dtmf_data.out_digit_sub_sofar = samples;
5426 			rtp_session->dtmf_data.timestamp_dtmf += 0xFFFF;
5427 		}
5428 
5429 		if (rtp_session->dtmf_data.out_digit_sofar >= rtp_session->dtmf_data.out_digit_dur) {
5430 			rtp_session->dtmf_data.out_digit_packet[1] |= 0x80;
5431 			loops = 3;
5432 		}
5433 
5434 		rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8);
5435 		rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar;
5436 
5437 		for (x = 0; x < loops; x++) {
5438 			switch_size_t wrote = switch_rtp_write_manual(rtp_session,
5439 														  rtp_session->dtmf_data.out_digit_packet, 4, 0,
5440 														  rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags);
5441 
5442 			rtp_session->stats.outbound.raw_bytes += wrote;
5443 			rtp_session->stats.outbound.dtmf_packet_count++;
5444 
5445 			if (loops == 1) {
5446 				rtp_session->last_write_ts += samples;
5447 
5448 				if (rtp_session->rtp_bugs & RTP_BUG_SONUS_SEND_INVALID_TIMESTAMP_2833) {
5449 					rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts;
5450 				}
5451 			}
5452 
5453 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Send %s packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u\n",
5454 							  loops == 1 ? "middle" : "end", rtp_session->dtmf_data.out_digit,
5455 							  rtp_session->dtmf_data.timestamp_dtmf,
5456 							  rtp_session->dtmf_data.out_digit_sofar,
5457 							  rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts);
5458 		}
5459 
5460 		if (loops != 1) {
5461 			rtp_session->sending_dtmf = 0;
5462 			rtp_session->need_mark = 1;
5463 
5464 			if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
5465 				//switch_core_timer_sync(&rtp_session->write_timer);
5466 				rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount;
5467 			}
5468 
5469 			rtp_session->dtmf_data.out_digit_dur = 0;
5470 
5471 			if (rtp_session->interdigit_delay) {
5472 				set_dtmf_delay(rtp_session, rtp_session->interdigit_delay, rtp_session->interdigit_delay * 10);
5473 			}
5474 
5475 			return;
5476 		}
5477 	}
5478 
5479 	if (!rtp_session->dtmf_data.out_digit_dur && rtp_session->dtmf_data.dtmf_queue && switch_queue_size(rtp_session->dtmf_data.dtmf_queue)) {
5480 		void *pop;
5481 
5482 		if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
5483 			//switch_core_timer_sync(&rtp_session->write_timer);
5484 			if (rtp_session->timer.samplecount < rtp_session->next_write_samplecount) {
5485 				return;
5486 			}
5487 
5488 			if (rtp_session->timer.samplecount >= rtp_session->max_next_write_samplecount) {
5489 				rtp_session->queue_delay = 0;
5490 			}
5491 
5492 		} else if (rtp_session->queue_delay) {
5493 			if (rtp_session->delay_samples >= rtp_session->samples_per_interval) {
5494 				rtp_session->delay_samples -= rtp_session->samples_per_interval;
5495 			} else {
5496 				rtp_session->delay_samples = 0;
5497 			}
5498 
5499 			if (!rtp_session->delay_samples) {
5500 				rtp_session->queue_delay = 0;
5501 			}
5502 		}
5503 
5504 		if (rtp_session->queue_delay) {
5505 			return;
5506 		}
5507 
5508 
5509 		if (!rtp_session->sending_dtmf) {
5510 			rtp_session->sending_dtmf = 1;
5511 		}
5512 
5513 		if (switch_queue_trypop(rtp_session->dtmf_data.dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
5514 			switch_dtmf_t *rdigit = pop;
5515 			switch_size_t wrote;
5516 
5517 			if (rdigit->digit == 'w') {
5518 				set_dtmf_delay(rtp_session, 500, 0);
5519 				free(rdigit);
5520 				return;
5521 			}
5522 
5523 			if (rdigit->digit == 'W') {
5524 				set_dtmf_delay(rtp_session, 1000, 0);
5525 				free(rdigit);
5526 				return;
5527 			}
5528 
5529 			memset(rtp_session->dtmf_data.out_digit_packet, 0, 4);
5530 			rtp_session->dtmf_data.out_digit_sofar = samples;
5531 			rtp_session->dtmf_data.out_digit_sub_sofar = samples;
5532 			rtp_session->dtmf_data.out_digit_dur = rdigit->duration;
5533 			rtp_session->dtmf_data.out_digit = rdigit->digit;
5534 			rtp_session->dtmf_data.out_digit_packet[0] = (unsigned char) switch_char_to_rfc2833(rdigit->digit);
5535 			rtp_session->dtmf_data.out_digit_packet[1] = 13;
5536 			rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8);
5537 			rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar;
5538 
5539 
5540 			rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts + samples;
5541 			rtp_session->last_write_ts = rtp_session->dtmf_data.timestamp_dtmf;
5542 			rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
5543 
5544 			wrote = switch_rtp_write_manual(rtp_session,
5545 											rtp_session->dtmf_data.out_digit_packet,
5546 											4,
5547 											rtp_session->rtp_bugs & RTP_BUG_CISCO_SKIP_MARK_BIT_2833 ? 0 : 1,
5548 											rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags);
5549 
5550 
5551 			rtp_session->stats.outbound.raw_bytes += wrote;
5552 			rtp_session->stats.outbound.dtmf_packet_count++;
5553 
5554 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Send start packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u\n",
5555 							  rtp_session->dtmf_data.out_digit,
5556 							  rtp_session->dtmf_data.timestamp_dtmf,
5557 							  rtp_session->dtmf_data.out_digit_sofar,
5558 							  rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts);
5559 
5560 			free(rdigit);
5561 		}
5562 	}
5563 }
5564 
rtp_flush_read_buffer(switch_rtp_t * rtp_session,switch_rtp_flush_t flush)5565 SWITCH_DECLARE(void) rtp_flush_read_buffer(switch_rtp_t *rtp_session, switch_rtp_flush_t flush)
5566 {
5567 
5568 	if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] ||
5569 		rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
5570 		return;
5571 	}
5572 
5573 
5574 	if (switch_rtp_ready(rtp_session)) {
5575 		rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
5576 		rtp_session->flags[SWITCH_RTP_FLAG_FLUSH] = 1;
5577 		reset_jitter_seq(rtp_session);
5578 
5579 		switch (flush) {
5580 		case SWITCH_RTP_FLUSH_STICK:
5581 			switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_STICKY_FLUSH);
5582 			break;
5583 		case SWITCH_RTP_FLUSH_UNSTICK:
5584 			switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_STICKY_FLUSH);
5585 			break;
5586 		default:
5587 				break;
5588 		}
5589 	}
5590 }
5591 
jb_valid(switch_rtp_t * rtp_session)5592 static int jb_valid(switch_rtp_t *rtp_session)
5593 {
5594 	if (rtp_session->ice.ice_user) {
5595 		if (!rtp_session->ice.ready && rtp_session->ice.rready) {
5596 			return 0;
5597 		}
5598 	}
5599 
5600 	if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) {
5601 		return 0;
5602 	}
5603 
5604 	return 1;
5605 }
5606 
5607 
do_flush(switch_rtp_t * rtp_session,int force,switch_size_t bytes_in)5608 static switch_size_t do_flush(switch_rtp_t *rtp_session, int force, switch_size_t bytes_in)
5609 {
5610 	int was_blocking = 0;
5611 	switch_size_t bytes;
5612 	uint32_t flushed = 0;
5613 	switch_size_t bytes_out = 0;
5614 
5615 	if (!switch_rtp_ready(rtp_session)) {
5616 		return 0;
5617 	}
5618 
5619 	reset_jitter_seq(rtp_session);
5620 
5621 	if (!force) {
5622 		if ((rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) ||
5623 			rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] ||
5624 			rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] ||
5625 			rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]
5626 			) {
5627 			return bytes_in;
5628 		}
5629 	}
5630 
5631 	READ_INC(rtp_session);
5632 
5633 	if (switch_rtp_ready(rtp_session) ) {
5634 
5635 		if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
5636 			//switch_jb_reset(rtp_session->jb);
5637 			bytes_out = bytes_in;
5638 			goto end;
5639 		}
5640 
5641 		if (rtp_session->vbw) {
5642 			switch_jb_reset(rtp_session->vbw);
5643 		}
5644 
5645 		if (rtp_session->vb) {
5646 			//switch_jb_reset(rtp_session->vb);
5647 			bytes_out = bytes_in;
5648 			goto end;
5649 		}
5650 
5651 		if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) {
5652 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session),
5653 							  SWITCH_LOG_CONSOLE, "%s FLUSH\n",
5654 							  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName"
5655 							  );
5656 		}
5657 
5658 		if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK]) {
5659 			was_blocking = 1;
5660 			switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
5661 			switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK, TRUE);
5662 		}
5663 
5664 		// before processing/flushing packets, if current packet is rfc2833, handle it (else it would be lost)
5665 		if (bytes_in > rtp_header_len && rtp_session->last_rtp_hdr.version == 2 && rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
5666 		    int do_cng = 0;
5667 #ifdef DEBUG_2833
5668 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "*** Handling current RTP packet before flushing. seq=%u ***\n", ntohs(rtp_session->last_rtp_hdr.seq));
5669 #endif
5670 		    handle_rfc2833(rtp_session, bytes_in, &do_cng);
5671 		}
5672 
5673 		do {
5674 			if (switch_rtp_ready(rtp_session)) {
5675 				bytes = sizeof(rtp_msg_t);
5676 				switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock_input, 0, (void *) &rtp_session->recv_msg, &bytes);
5677 
5678 				if (bytes) {
5679 					int do_cng = 0;
5680 
5681 					if (rtp_session->media_timeout) {
5682 						rtp_session->last_media = switch_micro_time_now();
5683 					}
5684 
5685 					/* Make sure to handle RFC2833 packets, even if we're flushing the packets */
5686 					if (bytes > rtp_header_len && rtp_session->recv_msg.header.version == 2 && rtp_session->recv_msg.header.pt == rtp_session->recv_te) {
5687 						rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
5688 						handle_rfc2833(rtp_session, bytes, &do_cng);
5689 #ifdef DEBUG_2833
5690 						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "*** RTP packet handled in flush loop %d ***\n", do_cng);
5691 #endif
5692 					}
5693 
5694 					flushed++;
5695 
5696 					rtp_session->stats.inbound.raw_bytes += bytes;
5697 					rtp_session->stats.inbound.flush_packet_count++;
5698 					rtp_session->stats.inbound.packet_count++;
5699 				}
5700 			} else {
5701 				break;
5702 			}
5703 		} while (bytes > 0);
5704 
5705 #ifdef DEBUG_2833
5706         if (flushed) {
5707             switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "*** do_flush: total flushed packets: %ld ***\n",(long)flushed);
5708         }
5709 #endif
5710 
5711 
5712 		if (was_blocking && switch_rtp_ready(rtp_session)) {
5713 			switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
5714 			switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK, FALSE);
5715 		}
5716 
5717 
5718 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->session) {
5719 			//int type = 1; // sum flags: 1 encoder; 2; decoder
5720 			//switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_IO_READ, SCC_VIDEO_RESET, SCCT_INT, (void *)&type, NULL, NULL);
5721 			switch_core_session_request_video_refresh(rtp_session->session);
5722 		}
5723 	}
5724 
5725  end:
5726 
5727 	READ_DEC(rtp_session);
5728 
5729 	return bytes_out;
5730 }
5731 
check_recv_payload(switch_rtp_t * rtp_session)5732 static int check_recv_payload(switch_rtp_t *rtp_session)
5733 {
5734 	int ok = 1;
5735 
5736 	if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PAYLOAD) && rtp_session->pmaps && *rtp_session->pmaps) {
5737 		payload_map_t *pmap;
5738 		ok = 0;
5739 
5740 		switch_mutex_lock(rtp_session->flag_mutex);
5741 
5742 		for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
5743 			if (!pmap->negotiated) {
5744 				continue;
5745 			}
5746 
5747 			if (rtp_session->last_rtp_hdr.pt == pmap->pt) {
5748 				ok = 1;
5749 			}
5750 		}
5751 		switch_mutex_unlock(rtp_session->flag_mutex);
5752 	}
5753 
5754 	return ok;
5755 }
5756 
get_recv_payload(switch_rtp_t * rtp_session)5757 static int get_recv_payload(switch_rtp_t *rtp_session)
5758 {
5759 	int r = -1;
5760 
5761 	if (rtp_session->pmaps && *rtp_session->pmaps) {
5762 		payload_map_t *pmap;
5763 
5764 		switch_mutex_lock(rtp_session->flag_mutex);
5765 
5766 		for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
5767 			if (pmap->negotiated) {
5768 				r = pmap->pt;
5769 				break;
5770 			}
5771 		}
5772 		switch_mutex_unlock(rtp_session->flag_mutex);
5773 	}
5774 
5775 	return r;
5776 }
5777 
5778 #define return_cng_frame() do_cng = 1; goto timer_check
5779 
read_rtp_packet(switch_rtp_t * rtp_session,switch_size_t * bytes,switch_frame_flag_t * flags,payload_map_t ** pmapP,switch_status_t poll_status,switch_bool_t return_jb_packet)5780 static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags,
5781 									   payload_map_t **pmapP, switch_status_t poll_status, switch_bool_t return_jb_packet)
5782 {
5783 	switch_status_t status = SWITCH_STATUS_FALSE;
5784 	uint32_t ts = 0;
5785 	unsigned char *b = NULL;
5786 	int sync = 0;
5787 	switch_time_t now;
5788 	switch_size_t xcheck_jitter = 0;
5789 	int tries = 0;
5790 	int block = 0;
5791 
5792 	switch_assert(bytes);
5793  more:
5794 
5795 	tries++;
5796 
5797 	if (tries > 20) {
5798 		if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
5799 			switch_jb_reset(rtp_session->jb);
5800 		}
5801 		rtp_session->punts++;
5802 		rtp_session->clean = 0;
5803 		*bytes = 0;
5804 		return SWITCH_STATUS_BREAK;
5805 	}
5806 
5807 	if (block) {
5808 		int to = 20000;
5809 		int fdr = 0;
5810 
5811 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5812 			to = 100000;
5813 		} else {
5814 			if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
5815 				to = rtp_session->timer.interval * 1000;
5816 			}
5817 		}
5818 
5819 		poll_status = switch_poll(rtp_session->read_pollfd, 1, &fdr, to);
5820 
5821 		if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
5822 			switch_core_timer_sync(&rtp_session->timer);
5823 		}
5824 
5825 		if (rtp_session->session) {
5826 			switch_ivr_parse_all_messages(rtp_session->session);
5827 		}
5828 
5829 		block = 0;
5830 	}
5831 
5832 	*bytes = sizeof(rtp_msg_t);
5833 	sync = 0;
5834 
5835 	rtp_session->has_rtp = 0;
5836 	rtp_session->has_ice = 0;
5837 	rtp_session->has_rtcp = 0;
5838 	if (rtp_session->dtls) {
5839 		rtp_session->dtls->bytes = 0;
5840 		rtp_session->dtls->data = NULL;
5841 	}
5842 	memset(&rtp_session->last_rtp_hdr, 0, sizeof(rtp_session->last_rtp_hdr));
5843 
5844 	if (poll_status == SWITCH_STATUS_SUCCESS) {
5845 		status = switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock_input, 0, (void *) &rtp_session->recv_msg, bytes);
5846 	} else {
5847 		*bytes = 0;
5848 	}
5849 
5850 	if (*bytes) {
5851 		b = (unsigned char *) &rtp_session->recv_msg;
5852 
5853 		/* version 2 probably rtp, zrtp cookie present means zrtp */
5854 		rtp_session->has_rtp = (rtp_session->recv_msg.header.version == 2 || ntohl(*(int *)(b+4)) == ZRTP_MAGIC_COOKIE);
5855 
5856 		if (rtp_session->media_timeout) {
5857 			rtp_session->last_media = switch_micro_time_now();
5858 		}
5859 
5860 		if ((*b >= 20) && (*b <= 64)) {
5861 			if (rtp_session->dtls) {
5862 				rtp_session->dtls->bytes = *bytes;
5863 				rtp_session->dtls->data = (void *) &rtp_session->recv_msg;
5864 			}
5865 			rtp_session->has_ice = 0;
5866 			rtp_session->has_rtp = 0;
5867 			rtp_session->has_rtcp = 0;
5868 		} else if (*b == 0 || *b == 1) {
5869 			rtp_session->has_ice = 1;
5870 			rtp_session->has_rtp = 0;
5871 			rtp_session->has_rtcp = 0;
5872 		} else {
5873 			if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
5874 				switch(rtp_session->recv_msg.header.pt) {
5875 				case 64:        //  192 Full INTRA-frame request.
5876 				case 72:        //  200 Sender report.
5877 				case 73:        //  201 Receiver report.
5878 				case 74:        //  202 Source description.
5879 				case 75:        //  203 Goodbye.
5880 				case 76:        //  204 Application-defined.
5881 				case 77:        //  205 Transport layer FB message.
5882 				case 78:        //  206 Payload-specific FB message.
5883 				case 79:        //  207 Extended report.
5884 					rtp_session->has_rtcp = 1;
5885 					rtp_session->has_rtp = 0;
5886 					rtp_session->has_ice = 0;
5887 					break;
5888 				default:
5889 					if (rtp_session->rtcp_recv_msg_p->header.version == 2 &&
5890 						rtp_session->rtcp_recv_msg_p->header.type > 199 && rtp_session->rtcp_recv_msg_p->header.type < 208) {
5891 						rtp_session->has_rtcp = 1;
5892 						rtp_session->has_rtp = 0;
5893 						rtp_session->has_ice = 0;
5894 					}
5895 					break;
5896 				}
5897 			}
5898 		}
5899 
5900 		if (rtp_session->has_rtp || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
5901 			rtp_session->missed_count = 0;
5902 			switch_cp_addr(rtp_session->rtp_from_addr, rtp_session->from_addr);
5903 		}
5904 
5905 		if (rtp_session->has_rtp) {
5906 			rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
5907 
5908 
5909 			if (bytes && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
5910 				rtp_session->last_rtp_hdr.pt != 13 &&
5911 				rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
5912 				rtp_session->last_rtp_hdr.pt != rtp_session->cng_pt) {
5913 				int accept_packet = 1;
5914 
5915 
5916 				if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PAYLOAD) &&
5917 					!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PACKETS) && rtp_session->pmaps && *rtp_session->pmaps) {
5918 					payload_map_t *pmap;
5919 					accept_packet = 0;
5920 
5921 					switch_mutex_lock(rtp_session->flag_mutex);
5922 					for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
5923 
5924 						if (ntohl(*(int *)(b+4)) == ZRTP_MAGIC_COOKIE) {
5925 							accept_packet = 1;
5926 							break;
5927 						}
5928 
5929 						if (!pmap->negotiated) {
5930 							continue;
5931 						}
5932 
5933 						if (rtp_session->last_rtp_hdr.pt == pmap->pt) {
5934 							accept_packet = 1;
5935 							if (pmapP) {
5936 								*pmapP = pmap;
5937 							}
5938 							break;
5939 						}
5940 					}
5941 					switch_mutex_unlock(rtp_session->flag_mutex);
5942 				}
5943 
5944 				if (!accept_packet) {
5945 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1,
5946 									  "Invalid Packet SEQ: %d TS: %d PT:%d ignored\n",
5947 									  ntohs(rtp_session->recv_msg.header.seq), ntohl(rtp_session->last_rtp_hdr.ts), rtp_session->last_rtp_hdr.pt);
5948 					*bytes = 0;
5949 				}
5950 			}
5951 
5952 			if (rtp_session->flags[SWITCH_RTP_FLAG_DETECT_SSRC]) {
5953 				//if (rtp_session->remote_ssrc != rtp_session->stats.rtcp.peer_ssrc && rtp_session->stats.rtcp.peer_ssrc) {
5954 				//	rtp_session->remote_ssrc = rtp_session->stats.rtcp.peer_ssrc;
5955 				//}
5956 
5957 				if (rtp_session->remote_ssrc != rtp_session->last_rtp_hdr.ssrc && rtp_session->last_rtp_hdr.ssrc) {
5958 					rtp_session->remote_ssrc = ntohl(rtp_session->last_rtp_hdr.ssrc);
5959 				}
5960 			}
5961 		}
5962 	}
5963 
5964 	if (!rtp_session->vb && (!rtp_session->jb || rtp_session->pause_jb || !jb_valid(rtp_session))) {
5965 		if (*bytes > rtp_header_len && (rtp_session->has_rtp && check_recv_payload(rtp_session))) {
5966 			xcheck_jitter = *bytes;
5967 			check_jitter(rtp_session);
5968 		}
5969 	}
5970 
5971 	if (check_rtcp_and_ice(rtp_session) == -1) {
5972 		//if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5973 		//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF CHECK FAIL\n");
5974 		//}
5975 		return SWITCH_STATUS_GENERR;
5976 	}
5977 
5978 	if (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
5979 		goto udptl;
5980 	}
5981 
5982 
5983 	if (*bytes) {
5984 		*flags &= ~SFF_PROXY_PACKET;
5985 
5986 		//if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5987 		//	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF BYTES %ld b=%d\n", *bytes, *b);
5988 		//}
5989 
5990 
5991 		if (rtp_session->has_ice) {
5992 			if (rtp_session->ice.ice_user) {
5993 				handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, *bytes);
5994 			}
5995 			*bytes = 0;
5996 			sync = 1;
5997 		}
5998 	}
5999 
6000 	switch_mutex_lock(rtp_session->ice_mutex);
6001 
6002 	if (rtp_session->dtls) {
6003 
6004 		if (rtp_session->rtcp_dtls && rtp_session->rtcp_dtls != rtp_session->dtls) {
6005 			rtp_session->rtcp_dtls->bytes = 0;
6006 			rtp_session->rtcp_dtls->data = NULL;
6007 			do_dtls(rtp_session, rtp_session->rtcp_dtls);
6008 		}
6009 
6010 		do_dtls(rtp_session, rtp_session->dtls);
6011 
6012 		if (rtp_session->dtls && rtp_session->dtls->bytes) {
6013 			*bytes = 0;
6014 			sync = 1;
6015 		}
6016 	}
6017 
6018 
6019 
6020 	if (status == SWITCH_STATUS_SUCCESS && *bytes) {
6021 		if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
6022 			*flags &= ~SFF_RTCP;
6023 			if (rtp_session->has_rtcp) {
6024 				*flags |= SFF_RTCP;
6025 
6026 #ifdef ENABLE_SRTP
6027 				if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV]) {
6028 					int sbytes = (int) *bytes;
6029 					srtp_err_status_t stat = 0;
6030 
6031 					if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) {
6032 						stat = srtp_unprotect_rtcp(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes);
6033 					} else {
6034 						stat = srtp_unprotect_rtcp_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes, 1);
6035 					}
6036 
6037 					if (stat) {
6038 						//++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++;
6039 						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "RTCP UNPROTECT ERR\n");
6040 					} else {
6041 						//rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
6042 					}
6043 
6044 					*bytes = sbytes;
6045 				}
6046 #endif
6047 				switch_mutex_unlock(rtp_session->ice_mutex);
6048 				return SWITCH_STATUS_SUCCESS;
6049 			}
6050 		}
6051 	}
6052 
6053 	switch_mutex_unlock(rtp_session->ice_mutex);
6054 
6055 	if ((*bytes && (!rtp_write_ready(rtp_session, *bytes, __LINE__) || !rtp_session->has_rtp || rtp_session->has_rtcp)) || sync) {
6056 		rtp_session->hot_hits = 0;
6057 		block = 1;
6058 		*bytes = 0;
6059 		goto more;
6060 	}
6061 
6062 	if (*bytes && rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) {
6063 		const char *tx_host;
6064 		const char *old_host;
6065 		const char *my_host;
6066 
6067 		char bufa[50], bufb[50], bufc[50];
6068 
6069 
6070 		tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
6071 		old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
6072 		my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
6073 
6074 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session), SWITCH_LOG_CONSOLE,
6075 						  "R %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
6076 						  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "No-Name",
6077 						  (long) *bytes,
6078 						  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
6079 						  old_host, rtp_session->remote_port,
6080 						  tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr),
6081 						  rtp_session->last_rtp_hdr.pt, ntohl(rtp_session->last_rtp_hdr.ts), ntohs(rtp_session->last_rtp_hdr.seq),
6082 						  rtp_session->last_rtp_hdr.m);
6083 
6084 	}
6085 
6086 #ifdef RTP_READ_PLOSS
6087 	{
6088 		int r = (rand() % 10000) + 1;
6089 		if (r <= 200) {
6090 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ALERT,
6091 							  "Simulate dropped packet ......... ts: %u seq: %u\n", ntohl(rtp_session->last_rtp_hdr.ts), ntohs(rtp_session->last_rtp_hdr.seq));
6092 			*bytes = 0;
6093 		}
6094 	}
6095 #endif
6096 
6097 
6098 
6099  udptl:
6100 
6101 	ts = 0;
6102 	rtp_session->recv_msg.ebody = NULL;
6103 	now = switch_micro_time_now();
6104 
6105 	if (*bytes) {
6106 		uint16_t seq = ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
6107 		ts = ntohl(rtp_session->last_rtp_hdr.ts);
6108 
6109 #ifdef DEBUG_MISSED_SEQ
6110 		if (rtp_session->last_seq && rtp_session->last_seq+1 != seq) {
6111 			//2012-11-28 18:33:11.799070 [ERR] switch_rtp.c:2883 Missed -65536 RTP frames from sequence [65536] to [-1] (missed). Time since last read [20021]
6112 			switch_size_t flushed_packets_diff = rtp_session->stats.inbound.flush_packet_count - rtp_session->last_flush_packet_count;
6113 			switch_size_t num_missed = (switch_size_t)seq - (rtp_session->last_seq+1);
6114 
6115 			if (num_missed == 1) { /* We missed one packet */
6116 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missed one RTP frame with sequence [%d]%s. Time since last read [%ld]\n",
6117 								  rtp_session->last_seq+1, (flushed_packets_diff == 1) ? " (flushed by FS)" : " (missed)",
6118 								  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
6119 			} else { /* We missed multiple packets */
6120 				if (flushed_packets_diff == 0) {
6121 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
6122 									  "Missed %ld RTP frames from sequence [%d] to [%d] (missed). Time since last read [%ld]\n",
6123 									  num_missed, rtp_session->last_seq+1, seq-1,
6124 									  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
6125 				} else if (flushed_packets_diff == num_missed) {
6126 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
6127 									  "Missed %ld RTP frames from sequence [%d] to [%d] (flushed by FS). Time since last read [%ld]\n",
6128 									  num_missed, rtp_session->last_seq+1, seq-1,
6129 									  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
6130 				} else if (num_missed > flushed_packets_diff) {
6131 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
6132 									  "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS, %ld packets missed)."
6133 									  " Time since last read [%ld]\n",
6134 									  num_missed, rtp_session->last_seq+1, seq-1,
6135 									  flushed_packets_diff, num_missed-flushed_packets_diff,
6136 									  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
6137 				} else {
6138 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
6139 									  "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS). Time since last read [%ld]\n",
6140 									  num_missed, rtp_session->last_seq+1, seq-1,
6141 									  flushed_packets_diff, rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
6142 				}
6143 			}
6144 
6145 		}
6146 #endif
6147 		rtp_session->last_seq = seq;
6148 
6149 
6150 		rtp_session->last_flush_packet_count = rtp_session->stats.inbound.flush_packet_count;
6151 
6152 
6153 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && now - rtp_session->last_read_time > 5000000) {
6154 			switch_rtp_video_refresh(rtp_session);
6155 		}
6156 
6157 		rtp_session->last_read_time = now;
6158 	}
6159 
6160 	if (*bytes && rtp_session->has_rtp && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]){
6161 		rtcp_stats(rtp_session);
6162 	}
6163 
6164 
6165 	if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
6166 		*bytes && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
6167 		ts && !rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session) && ts == rtp_session->last_cng_ts) {
6168 		/* we already sent this frame..... */
6169 		*bytes = 0;
6170 		return SWITCH_STATUS_SUCCESS;
6171 	}
6172 
6173 	if (*bytes) {
6174 		if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
6175 #ifdef ENABLE_ZRTP
6176 			/* ZRTP Recv */
6177 			if (zrtp_on) {
6178 
6179 				unsigned int sbytes = (int) *bytes;
6180 				zrtp_status_t stat = 0;
6181 
6182 				stat = zrtp_process_srtp(rtp_session->zrtp_stream, (void *) &rtp_session->recv_msg, &sbytes);
6183 
6184 				switch (stat) {
6185 				case zrtp_status_ok:
6186 					*bytes = sbytes;
6187 					break;
6188 				case zrtp_status_drop:
6189 					/* switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */
6190 					*bytes = 0;
6191 					return SWITCH_STATUS_SUCCESS;
6192 				case zrtp_status_fail:
6193 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
6194 					return SWITCH_STATUS_FALSE;
6195 				default:
6196 					break;
6197 				}
6198 			}
6199 #endif
6200 
6201 #ifdef ENABLE_SRTP
6202 			switch_mutex_lock(rtp_session->ice_mutex);
6203 			if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->has_rtp &&
6204 				(check_recv_payload(rtp_session) ||
6205 				 rtp_session->last_rtp_hdr.pt == rtp_session->recv_te ||
6206 				 rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt)) {
6207 				//if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->has_rtp)) {
6208 				int sbytes = (int) *bytes;
6209 				srtp_err_status_t stat = 0;
6210 
6211 				if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
6212 					switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV_RESET);
6213 					srtp_dealloc(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]);
6214 					rtp_session->recv_ctx[rtp_session->srtp_idx_rtp] = NULL;
6215 					if ((stat = srtp_create(&rtp_session->recv_ctx[rtp_session->srtp_idx_rtp],
6216 											&rtp_session->recv_policy[rtp_session->srtp_idx_rtp])) || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
6217 
6218 						rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
6219 						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP RECV\n");
6220 						rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
6221 						switch_mutex_unlock(rtp_session->ice_mutex);
6222 						return SWITCH_STATUS_FALSE;
6223 					} else {
6224 
6225 						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "RE-Activating Secure RTP RECV\n");
6226 						rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
6227 					}
6228 				}
6229 
6230 				if (!(*flags & SFF_PLC) && rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
6231 					if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) {
6232 						stat = srtp_unprotect(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes);
6233 					} else {
6234 						stat = srtp_unprotect_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes, 1);
6235 					}
6236 
6237 					if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && stat == srtp_err_status_replay_fail) {
6238 						/* false alarm nack */
6239 						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "REPLAY ERR, FALSE NACK\n");
6240 						sbytes = 0;
6241 						*bytes = 0;
6242 						if (rtp_session->stats.rtcp.pkt_count) {
6243 							rtp_session->stats.rtcp.period_pkt_count--;
6244 							rtp_session->stats.rtcp.pkt_count--;
6245 						}
6246 						switch_mutex_unlock(rtp_session->ice_mutex);
6247 						goto more;
6248 					}
6249 				}
6250 
6251 				if (stat && rtp_session->recv_msg.header.pt != rtp_session->recv_te && rtp_session->recv_msg.header.pt != rtp_session->cng_pt) {
6252 					int errs = ++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp];
6253 					if (stat != 10) {
6254 						char *msg;
6255 						if (stat == srtp_err_status_replay_fail) msg="replay check failed";
6256 						else if (stat == srtp_err_status_auth_fail) msg="auth check failed";
6257 						else msg="";
6258 						if (errs >= MAX_SRTP_ERRS) {
6259 							switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
6260 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING,
6261 											  "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
6262 											  rtp_type(rtp_session), stat, msg, (long)*bytes, errs);
6263 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING,
6264 											  "Ending call due to SRTP error\n");
6265 							switch_channel_hangup(channel, SWITCH_CAUSE_SRTP_READ_ERROR);
6266 						} else if (errs >= WARN_SRTP_ERRS && !(errs % WARN_SRTP_ERRS)) {
6267 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING,
6268 											  "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
6269 											  rtp_type(rtp_session), stat, msg, (long)*bytes, errs);
6270 						}
6271 					}
6272 					sbytes = 0;
6273 				} else {
6274 					rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
6275 				}
6276 
6277 				*bytes = sbytes;
6278 			}
6279 			switch_mutex_unlock(rtp_session->ice_mutex);
6280 #endif
6281 		}
6282 
6283 
6284 		if (rtp_session->has_rtp) {
6285 			if (rtp_session->recv_msg.header.cc > 0) { /* Contributing Source Identifiers (4 bytes = sizeof CSRC header)*/
6286 				rtp_session->recv_msg.ebody = RTP_BODY(rtp_session) + (rtp_session->recv_msg.header.cc * 4);
6287 			}
6288 
6289 			/* recalculate body length in case rtp extension used */
6290 			if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
6291 				rtp_session->recv_msg.header.x) { /* header extensions */
6292 				uint16_t length;
6293 
6294 				rtp_session->recv_msg.ext = (switch_rtp_hdr_ext_t *) RTP_BODY(rtp_session);
6295 				length = ntohs((uint16_t)rtp_session->recv_msg.ext->length);
6296 
6297 				if (length < SWITCH_RTP_MAX_BUF_LEN_WORDS) {
6298 					rtp_session->recv_msg.ebody = (char *)rtp_session->recv_msg.ext + (length * 4) + 4;
6299 					if (*bytes > (length * 4 + 4)) {
6300 						*bytes -= (length * 4 + 4);
6301 					} else {
6302 						*bytes = 0;
6303 					}
6304 				}
6305 			}
6306 
6307 
6308 #ifdef DEBUG_CHROME
6309 
6310 			if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->has_rtp) {
6311 
6312 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
6313 								  "VIDEO: seq: %d ts: %u len: %ld %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x mark: %d\n",
6314 								  ntohs(rtp_session->last_rtp_hdr.seq), ntohl(rtp_session->last_rtp_hdr.ts), *bytes,
6315 								  *((uint8_t *)RTP_BODY(rtp_session)), *((uint8_t *)RTP_BODY(rtp_session) + 1),
6316 								  *((uint8_t *)RTP_BODY(rtp_session) + 2), *((uint8_t *)RTP_BODY(rtp_session) + 3),
6317 								  *((uint8_t *)RTP_BODY(rtp_session) + 4), *((uint8_t *)RTP_BODY(rtp_session) + 5),
6318 								  *((uint8_t *)RTP_BODY(rtp_session) + 6), *((uint8_t *)RTP_BODY(rtp_session) + 7),
6319 								  *((uint8_t *)RTP_BODY(rtp_session) + 8), *((uint8_t *)RTP_BODY(rtp_session) + 9),
6320 								  *((uint8_t *)RTP_BODY(rtp_session) + 10), rtp_session->last_rtp_hdr.m);
6321 
6322 			}
6323 #endif
6324 
6325 
6326 
6327 		}
6328 
6329 
6330 		rtp_session->stats.inbound.raw_bytes += *bytes;
6331 
6332 		if (rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
6333 			rtp_session->stats.inbound.dtmf_packet_count++;
6334 		} else if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) {
6335 			rtp_session->stats.inbound.cng_packet_count++;
6336 		} else {
6337 			rtp_session->stats.inbound.media_packet_count++;
6338 			rtp_session->stats.inbound.media_bytes += *bytes;
6339 		}
6340 
6341 		rtp_session->stats.inbound.packet_count++;
6342 	}
6343 
6344 	if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
6345 		((rtp_session->recv_te && rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) ||
6346 		 (*bytes < rtp_header_len && *bytes > 0 && !(rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL])))) {
6347 		return SWITCH_STATUS_BREAK;
6348 	}
6349 
6350 	if (ts) {
6351 		rtp_session->last_read_ts = ts;
6352 	}
6353 
6354 	if (rtp_session->flags[SWITCH_RTP_FLAG_BYTESWAP] && check_recv_payload(rtp_session)) {
6355 		switch_swap_linear((int16_t *)RTP_BODY(rtp_session), (int) *bytes - rtp_header_len);
6356 	}
6357 
6358 	if (rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB]) {
6359 		rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB] = 0;
6360 
6361 		if (rtp_session->jb) {
6362 			switch_jb_destroy(&rtp_session->jb);
6363 		}
6364 
6365 		if (rtp_session->vb) {
6366 			switch_jb_destroy(&rtp_session->vb);
6367 		}
6368 
6369 		if (rtp_session->vbw) {
6370 			switch_jb_destroy(&rtp_session->vbw);
6371 		}
6372 
6373 	}
6374 
6375 	if (rtp_session->has_rtp && *bytes) {
6376 		uint32_t read_ssrc = ntohl(rtp_session->last_rtp_hdr.ssrc);
6377 
6378 		if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
6379 			return SWITCH_STATUS_SUCCESS;
6380 		}
6381 
6382 		if (rtp_session->vb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
6383 			status = switch_jb_put_packet(rtp_session->vb, (switch_rtp_packet_t *) &rtp_session->recv_msg, *bytes);
6384 
6385 			if (status == SWITCH_STATUS_TOO_LATE) {
6386 				goto more;
6387 			}
6388 
6389 			status = SWITCH_STATUS_FALSE;
6390 			*bytes = 0;
6391 
6392 			if (!return_jb_packet) {
6393 				return status;
6394 			}
6395 		}
6396 
6397 		if (rtp_session->jb && jb_valid(rtp_session)) {
6398 			if (rtp_session->last_jb_read_ssrc && rtp_session->last_jb_read_ssrc != read_ssrc) {
6399 				switch_jb_reset(rtp_session->jb);
6400 			}
6401 
6402 			rtp_session->last_jb_read_ssrc = read_ssrc;
6403 		}
6404 
6405 		if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
6406 
6407 			if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
6408 				switch_core_timer_sync(&rtp_session->timer);
6409 				reset_jitter_seq(rtp_session);
6410 			}
6411 
6412 			status = switch_jb_put_packet(rtp_session->jb, (switch_rtp_packet_t *) &rtp_session->recv_msg, *bytes);
6413 			if (status == SWITCH_STATUS_TOO_LATE) {
6414 				goto more;
6415 			}
6416 
6417 
6418 			status = SWITCH_STATUS_FALSE;
6419 			*bytes = 0;
6420 
6421 			if (!return_jb_packet) {
6422 				return status;
6423 			}
6424 		} else {
6425 			if (rtp_session->last_rtp_hdr.m && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
6426 				!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !(rtp_session->rtp_bugs & RTP_BUG_IGNORE_MARK_BIT)) {
6427 				switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
6428 			} else if (rtp_session->last_jb_read_ssrc && rtp_session->last_jb_read_ssrc != read_ssrc) {
6429 				switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
6430 			}
6431 		}
6432 	}
6433 
6434 	if (!*bytes || rtp_session->has_rtp) {
6435 
6436 		if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
6437 			switch_status_t jstatus = switch_jb_get_packet(rtp_session->jb, (switch_rtp_packet_t *) &rtp_session->recv_msg, bytes);
6438 
6439 			status = jstatus;
6440 
6441 			switch(jstatus) {
6442 			case SWITCH_STATUS_MORE_DATA:
6443 				if (rtp_session->punts < 4) {
6444 					block = 1;
6445 					goto more;
6446 				}
6447 				*bytes = 0;
6448 				break;
6449 			case SWITCH_STATUS_NOTFOUND:
6450 				{
6451 					int pt = get_recv_payload(rtp_session);
6452 					(*flags) |= SFF_PLC;
6453 					status = SWITCH_STATUS_SUCCESS;
6454 					*bytes = switch_jb_get_last_read_len(rtp_session->jb);
6455 					rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
6456 					rtp_session->last_rtp_hdr.pt = pt;
6457 				}
6458 				break;
6459 			case SWITCH_STATUS_BREAK:
6460 				break;
6461 			case SWITCH_STATUS_SUCCESS:
6462 			case SWITCH_STATUS_TIMEOUT:
6463 			default:
6464 				{
6465 					if (status == SWITCH_STATUS_TIMEOUT) {
6466 						rtp_session->skip_timer = 1;
6467 					}
6468 					rtp_session->stats.inbound.jb_packet_count++;
6469 					status = SWITCH_STATUS_SUCCESS;
6470 					rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
6471 					if (++rtp_session->clean > 200) {
6472 						rtp_session->punts = 0;
6473 					}
6474 					if (!xcheck_jitter) {
6475 						check_jitter(rtp_session);
6476 					}
6477 				}
6478 				break;
6479 			}
6480 		}
6481 
6482 		if (rtp_session->vb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
6483 			switch_status_t vstatus = switch_jb_get_packet(rtp_session->vb, (switch_rtp_packet_t *) &rtp_session->recv_msg, bytes);
6484 			status = vstatus;
6485 
6486 			switch(vstatus) {
6487 			case SWITCH_STATUS_RESTART:
6488 				switch_core_session_request_video_refresh(rtp_session->session);
6489 				status = SWITCH_STATUS_BREAK;
6490 				break;
6491 			case SWITCH_STATUS_MORE_DATA:
6492 				status = SWITCH_STATUS_BREAK;
6493 				break;
6494 			case SWITCH_STATUS_BREAK:
6495 			default:
6496 				break;
6497 			}
6498 
6499 			if (vstatus == SWITCH_STATUS_NOTFOUND && rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) {
6500 				int pt = get_recv_payload(rtp_session);
6501 				(*flags) |= SFF_PLC;
6502 				status = SWITCH_STATUS_SUCCESS;
6503 				*bytes = switch_jb_get_last_read_len(rtp_session->vb);
6504 				rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
6505 				if (pt > -1) {
6506 					rtp_session->last_rtp_hdr.pt = pt;
6507 				}
6508 			}
6509 
6510 			if (vstatus == SWITCH_STATUS_SUCCESS) {
6511 				rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
6512 
6513 				if (!xcheck_jitter) {
6514 					check_jitter(rtp_session);
6515 				}
6516 			}
6517 		}
6518 	}
6519 
6520 	return status;
6521 }
6522 
handle_nack(switch_rtp_t * rtp_session,uint32_t nack)6523 static void handle_nack(switch_rtp_t *rtp_session, uint32_t nack)
6524 {
6525 	switch_size_t bytes = 0;
6526 	rtp_msg_t send_msg[1] = {{{0}}};
6527 	uint16_t seq = (uint16_t) (nack & 0xFFFF);
6528 	uint16_t blp = (uint16_t) (nack >> 16);
6529 	int i;
6530 	const char *tx_host = NULL;
6531 	const char *old_host = NULL;
6532 	const char *my_host = NULL;
6533 	char bufa[50], bufb[50], bufc[50];
6534 
6535 	if (!(rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->vbw)) {
6536 		return;  /* not enabled */
6537 	}
6538 
6539 	if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
6540 		tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_from_addr);
6541 		old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
6542 		my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
6543 	}
6544 
6545 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got NACK [%u][0x%x] for seq %u\n",
6546 					  switch_core_session_get_name(rtp_session->session), nack, nack, ntohs(seq));
6547 
6548 	if (switch_jb_get_packet_by_seq(rtp_session->vbw, seq, (switch_rtp_packet_t *) send_msg, &bytes) == SWITCH_STATUS_SUCCESS) {
6549 
6550 		if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
6551 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session), SWITCH_LOG_CONSOLE,
6552 							  "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
6553 							  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
6554 							  (long) bytes,
6555 							  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
6556 							  old_host, rtp_session->remote_port,
6557 							  tx_host, switch_sockaddr_get_port(rtp_session->rtcp_from_addr),
6558 							  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
6559 
6560 		}
6561 		//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG2, "RE----SEND %u\n", ntohs(send_msg->header.seq));
6562 		switch_rtp_write_raw(rtp_session, (void *) send_msg, &bytes, SWITCH_FALSE);
6563 	} else {
6564 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Cannot send NACK for seq %u\n", ntohs(seq));
6565 	}
6566 
6567 	blp = ntohs(blp);
6568 	for (i = 0; i < 16; i++) {
6569 		if (blp & (1 << i)) {
6570 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Also Got NACK for seq %u\n",
6571 							  switch_core_session_get_name(rtp_session->session), ntohs(seq) + i + 1);
6572 			/* If they are missing more than one, may as well gen a key frame for good measure */
6573 			//switch_core_media_gen_key_frame(rtp_session->session);
6574 			if (switch_jb_get_packet_by_seq(rtp_session->vbw, htons(ntohs(seq) + i + 1), (switch_rtp_packet_t *) &send_msg, &bytes) == SWITCH_STATUS_SUCCESS) {
6575 				if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
6576 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session), SWITCH_LOG_CONSOLE,
6577 									  "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
6578 									  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
6579 									  (long) bytes,
6580 									  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
6581 									  old_host, rtp_session->remote_port,
6582 									  tx_host, switch_sockaddr_get_port(rtp_session->rtcp_from_addr),
6583 									  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
6584 
6585 				}
6586 				//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "RE----SEND %u\n", ntohs(send_msg->header.seq));
6587 
6588 				switch_rtp_write_raw(rtp_session, (void *) &send_msg, &bytes, SWITCH_FALSE);
6589 			} else {
6590 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Cannot send NACK for seq %u\n", ntohs(seq) + i);
6591 			}
6592 		}
6593 	}
6594 }
6595 
process_rtcp_report(switch_rtp_t * rtp_session,rtcp_msg_t * msg,switch_size_t bytes)6596 static switch_status_t process_rtcp_report(switch_rtp_t *rtp_session, rtcp_msg_t *msg, switch_size_t bytes)
6597 {
6598 	switch_status_t status = SWITCH_STATUS_FALSE;
6599 
6600 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,
6601 					  "RTCP packet bytes %" SWITCH_SIZE_T_FMT " type %d pad %d\n",
6602 					  bytes, msg->header.type, msg->header.p);
6603 
6604 	if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (msg->header.type == _RTCP_PT_RTPFB || msg->header.type == _RTCP_PT_PSFB || msg->header.type < 200)) {
6605 		rtcp_ext_msg_t *extp = (rtcp_ext_msg_t *) msg;
6606 
6607 		if (extp->header.fmt != 15) { // <---- REMOVE WHEN BRIA STOPS SENDING UNSOLICITED REMB
6608 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s PICKED UP %s XRTCP type: %d fmt: %d\n",
6609 							  switch_core_session_get_name(rtp_session->session), rtp_type(rtp_session), msg->header.type, extp->header.fmt);
6610 		}
6611 
6612 		if (msg->header.type == _RTCP_PT_FIR ||
6613 			(msg->header.type == _RTCP_PT_PSFB && (extp->header.fmt == _RTCP_PSFB_FIR || extp->header.fmt == _RTCP_PSFB_PLI))) {
6614 #if 0
6615 			if (msg->header.type == _RTCP_PT_FIR) {
6616 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Ancient FIR Received. Hello from 1996!\n");
6617 
6618 				if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_OLD_FIR)) {
6619 					switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_OLD_FIR);
6620 					switch_core_session_request_video_refresh(rtp_session->session);
6621 				}
6622 			}
6623 #endif
6624 
6625 			if (switch_core_session_media_flow(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO) == SWITCH_MEDIA_FLOW_RECVONLY) {
6626 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Ignoring FIR/PLI from a sendonly stream.\n",
6627 								  switch_core_session_get_name(rtp_session->session));
6628 			} else {
6629 				switch_core_media_gen_key_frame(rtp_session->session);
6630 				switch_core_session_request_video_refresh(rtp_session->session);
6631 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got FIR/PLI\n",
6632 								  switch_core_session_get_name(rtp_session->session));
6633 				switch_channel_set_flag(switch_core_session_get_channel(rtp_session->session), CF_VIDEO_REFRESH_REQ);
6634 			}
6635 		}
6636 
6637 		if (msg->header.type == _RTCP_PT_RTPFB && extp->header.fmt == _RTCP_RTPFB_NACK) {
6638 			uint32_t *nack = (uint32_t *) extp->body;
6639 			int i;
6640 
6641 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got NACK count %d\n",
6642 							  switch_core_session_get_name(rtp_session->session), ntohs(extp->header.length) - 2);
6643 
6644 
6645 			for (i = 0; i < ntohs(extp->header.length) - 2; i++) {
6646 				handle_nack(rtp_session, nack[i]);
6647 			}
6648 
6649 			//switch_core_media_gen_key_frame(rtp_session->session);
6650 		}
6651 
6652 	} else {
6653 		struct switch_rtcp_report_block *report;
6654 
6655 		if (msg->header.type == _RTCP_PT_SR || msg->header.type == _RTCP_PT_RR) {
6656 			int i;
6657 #ifdef DEBUG_RTCP
6658 			switch_time_t now = switch_micro_time_now();
6659 #endif
6660 			uint32_t lsr_now;
6661 			uint32_t lsr;
6662 			uint32_t packet_ssrc;
6663 			double rtt_now = 0;
6664 			uint8_t rtt_valid = 0;
6665 			int rtt_increase = 0, packet_loss_increase=0;
6666 
6667 			//if (msg->header.type == _RTCP_PT_SR && rtp_session->ice.ice_user) {
6668 			//	rtp_session->send_rr = 1;
6669 			//}
6670 
6671 			lsr_now = calc_local_lsr_now();
6672 
6673 			if (msg->header.type == _RTCP_PT_SR) { /* Sender report */
6674 				struct switch_rtcp_sender_report* sr = (struct switch_rtcp_sender_report*)msg->body;
6675 
6676 				rtp_session->stats.rtcp.packet_count = ntohl(sr->sender_info.pc);
6677 				rtp_session->stats.rtcp.octet_count = ntohl(sr->sender_info.oc);
6678 				packet_ssrc = sr->ssrc;
6679 				/* Extracting LSR from NTP timestamp and save it */
6680 				lsr = (ntohl(sr->sender_info.ntp_lsw)&0xffff0000)>>16 | (ntohl(sr->sender_info.ntp_msw)&0x0000ffff)<<16; /* The middle 32 bits out of 64 in the NTP timestamp */
6681 				rtp_session->stats.rtcp.last_recv_lsr_peer = htonl(lsr);  /* Save it include it in the next SR */
6682 				rtp_session->stats.rtcp.last_recv_lsr_local = lsr_now;    /* Save it to calculate DLSR when generating next SR */
6683 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Received a SR with %d report blocks, " \
6684 								  "length in words = %d, " \
6685 								  "SSRC = 0x%X, " \
6686 								  "NTP MSW = %u, " \
6687 								  "NTP LSW = %u, " \
6688 								  "RTP timestamp = %u, " \
6689 								  "Sender Packet Count = %u, " \
6690 								  "Sender Octet Count = %u\n",
6691 								  msg->header.count,
6692 								  ntohs((uint16_t)msg->header.length),
6693 								  ntohl(sr->ssrc),
6694 								  ntohl(sr->sender_info.ntp_msw),
6695 								  ntohl(sr->sender_info.ntp_lsw),
6696 								  ntohl(sr->sender_info.ts),
6697 								  ntohl(sr->sender_info.pc),
6698 								  ntohl(sr->sender_info.oc));
6699 
6700 
6701 				rtp_session->rtcp_frame.ssrc = ntohl(sr->ssrc);
6702 				rtp_session->rtcp_frame.packet_type = (uint16_t)rtp_session->rtcp_recv_msg_p->header.type;
6703 				rtp_session->rtcp_frame.ntp_msw = ntohl(sr->sender_info.ntp_msw);
6704 				rtp_session->rtcp_frame.ntp_lsw = ntohl(sr->sender_info.ntp_lsw);
6705 				rtp_session->rtcp_frame.timestamp = ntohl(sr->sender_info.ts);
6706 				rtp_session->rtcp_frame.packet_count =  ntohl(sr->sender_info.pc);
6707 				rtp_session->rtcp_frame.octect_count = ntohl(sr->sender_info.oc);
6708 
6709 				report = &sr->report_block;
6710 			} else { /* Receiver report */
6711 				struct switch_rtcp_receiver_report* rr = (struct switch_rtcp_receiver_report*)msg->body;
6712 				packet_ssrc = rr->ssrc;
6713 				//memset(&rtp_session->rtcp_frame, 0, sizeof(rtp_session->rtcp_frame));
6714 				report = &rr->report_block;
6715 
6716 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Received a RR with %d report blocks, " \
6717 								  "length in words = %d, " \
6718 								  "SSRC = 0x%X, ",
6719 								  msg->header.count,
6720 								  ntohs((uint16_t)msg->header.length),
6721 								  ntohl(rr->ssrc));
6722 
6723 			}
6724 
6725 
6726 			for (i = 0; i < (int)msg->header.count && i < MAX_REPORT_BLOCKS ; i++) {
6727 				uint32_t old_avg = rtp_session->rtcp_frame.reports[i].loss_avg;
6728 				uint8_t percent_fraction = (uint8_t)((uint16_t/* prevent overflow when '* 100' */)(uint8_t)report->fraction * 100 / 255);
6729 				if (!rtp_session->rtcp_frame.reports[i].loss_avg) {
6730 					rtp_session->rtcp_frame.reports[i].loss_avg = percent_fraction;
6731 				} else {
6732 					rtp_session->rtcp_frame.reports[i].loss_avg = (uint32_t)(((float)rtp_session->rtcp_frame.reports[i].loss_avg * .7) +
6733 																			 ((float)percent_fraction * .3));
6734 				}
6735 
6736 				rtp_session->rtcp_frame.reports[i].ssrc = ntohl(report->ssrc);
6737 				rtp_session->rtcp_frame.reports[i].fraction = (uint8_t)report->fraction;
6738 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
6739 				rtp_session->rtcp_frame.reports[i].lost = report->lost; // signed 24bit will extended signess to int32_t automatically
6740 #else
6741 				rtp_session->rtcp_frame.reports[i].lost = ntohl(report->lost)>>8; // signed 24bit casted to uint32_t need >>8 after ntohl()...
6742 				rtp_session->rtcp_frame.reports[i].lost = rtp_session->rtcp_frame.reports[i].lost | ((rtp_session->rtcp_frame.reports[i].lost & 0x00800000) ? 0xff000000 : 0x00000000); // ...and signess compensation
6743 #endif
6744 				rtp_session->rtcp_frame.reports[i].highest_sequence_number_received = ntohl(report->highest_sequence_number_received);
6745 				rtp_session->rtcp_frame.reports[i].jitter = ntohl(report->jitter);
6746 				rtp_session->rtcp_frame.reports[i].lsr = ntohl(report->lsr);
6747 				rtp_session->rtcp_frame.reports[i].dlsr = ntohl(report->dlsr);
6748 
6749 				if (rtp_session->rtcp_frame.reports[i].lsr && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
6750 
6751 					/* Calculating RTT = A - DLSR - LSR */
6752 					rtt_now = ((double)(((int64_t)lsr_now) - rtp_session->rtcp_frame.reports[i].dlsr - rtp_session->rtcp_frame.reports[i].lsr))/65536;
6753 
6754 					/* Only account RTT if it didn't overflow. */
6755 					if (lsr_now > rtp_session->rtcp_frame.reports[i].dlsr + rtp_session->rtcp_frame.reports[i].lsr) {
6756 #ifdef DEBUG_RTCP
6757 						switch_time_exp_t now_hr;
6758 						switch_time_exp_gmt(&now_hr,now);
6759 						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,
6760 								"Receiving an RTCP packet\n[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[0x%x]\n"
6761 								"RTT[%f] = A[%u] - DLSR[%u] - LSR[%u]\n",
6762 								1900 + now_hr.tm_year, now_hr.tm_mday, now_hr.tm_mon, now_hr.tm_hour, now_hr.tm_min, now_hr.tm_sec, now_hr.tm_usec,
6763 								rtp_session->rtcp_frame.reports[i].ssrc, rtt_now,
6764 								lsr_now, rtp_session->rtcp_frame.reports[i].dlsr, rtp_session->rtcp_frame.reports[i].lsr);
6765 #endif
6766 						rtt_valid = 1;
6767 						if (!rtp_session->rtcp_frame.reports[i].rtt_avg) {
6768 							rtp_session->rtcp_frame.reports[i].rtt_avg = rtt_now;
6769 						} else {
6770 							rtp_session->rtcp_frame.reports[i].rtt_avg = (double)((rtp_session->rtcp_frame.reports[i].rtt_avg * .7) + (rtt_now * .3 ));
6771 						}
6772 					} else {
6773 #ifdef DEBUG_RTCP
6774 						switch_time_exp_t now_hr;
6775 						switch_time_exp_gmt(&now_hr,now);
6776 						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING,
6777 								"Receiving RTCP packet\n[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[0x%x]\n"
6778 								"Ignoring erroneous RTT[%f] = A[%u] - DLSR[%u] - LSR[%u]\n",
6779 								1900 + now_hr.tm_year, now_hr.tm_mday, now_hr.tm_mon, now_hr.tm_hour, now_hr.tm_min, now_hr.tm_sec, now_hr.tm_usec,
6780 								rtp_session->rtcp_frame.reports[i].ssrc, rtt_now,
6781 								lsr_now, rtp_session->rtcp_frame.reports[i].dlsr, rtp_session->rtcp_frame.reports[i].lsr);
6782 #endif
6783 						rtt_valid = 0;
6784 						rtt_now = 0;
6785 					}
6786 
6787 
6788 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "RTT average %f\n",
6789 							rtp_session->rtcp_frame.reports[i].rtt_avg);
6790 				}
6791 
6792 				if (rtp_session->flags[SWITCH_RTP_FLAG_ADJ_BITRATE_CAP] && rtp_session->flags[SWITCH_RTP_FLAG_ESTIMATORS] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
6793 
6794 					/* SWITCH_RTP_FLAG_ADJ_BITRATE_CAP : Can the codec change its bitrate on the fly per API command ? */
6795 #ifdef DEBUG_ESTIMATORS_
6796 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "Current packet loss: [%d %%] Current RTT: [%f ms]\n", percent_fraction, rtt_now);
6797 #endif
6798 
6799 					if (rtt_valid) {
6800 
6801 						switch_kalman_estimate(rtp_session->estimators[EST_RTT], rtt_now, EST_RTT);
6802 
6803 						if (switch_kalman_cusum_detect_change(rtp_session->detectors[EST_RTT], rtt_now, rtp_session->estimators[EST_RTT]->val_estimate_last)) {
6804 							/* sudden change in the mean value of RTT */
6805 #ifdef DEBUG_ESTIMATORS_
6806 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of RTT !\n");
6807 #endif
6808 							rtt_increase = 1;
6809 						}
6810 					}
6811 
6812 					switch_kalman_estimate(rtp_session->estimators[EST_LOSS], percent_fraction, EST_LOSS);
6813 
6814 					if (switch_kalman_cusum_detect_change(rtp_session->detectors[EST_LOSS], percent_fraction, rtp_session->estimators[EST_LOSS]->val_estimate_last)){
6815 						/* sudden change in the mean value of packet loss */
6816 #ifdef DEBUG_ESTIMATORS_
6817 						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of packet loss!\n");
6818 #endif
6819 						packet_loss_increase = 1;
6820 					}
6821 #ifdef DEBUG_ESTIMATORS_
6822 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "ESTIMATORS: Packet loss will be: [%f] RTT will be: [%f ms]\n",
6823 									  rtp_session->estimators[EST_LOSS]->val_estimate_last, rtp_session->estimators[EST_RTT]->val_estimate_last);
6824 #endif
6825 
6826 					if (rtp_session->rtcp_frame.reports[i].loss_avg != old_avg) {
6827 						/*getting bad*/
6828 						if (switch_kalman_is_slow_link(rtp_session->estimators[EST_LOSS],
6829 													   rtp_session->estimators[EST_RTT])) {
6830 							/* going to minimum bitrate */
6831 #ifdef DEBUG_ESTIMATORS_
6832 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "Slow link conditions: Loss average: [%d %%], Previous loss: [%d %%]. \
6833 																	Going to minimum bitrate!",rtp_session->rtcp_frame.reports[i].loss_avg, old_avg);
6834 #endif
6835 							switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO,
6836 															SWITCH_IO_WRITE, SCC_AUDIO_ADJUST_BITRATE, SCCT_STRING, "minimum", SCCT_NONE, NULL, NULL, NULL);
6837 							/* if after going to minimum bitrate we still have packet loss then we increase ptime. TODO */
6838 
6839 						} else if (packet_loss_increase && (rtp_session->estimators[EST_LOSS]->val_estimate_last >= 5)) {
6840 							/* sudden change in the mean value of packet loss percentage */
6841 							switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO,
6842 															SWITCH_IO_WRITE, SCC_AUDIO_ADJUST_BITRATE,
6843 															SCCT_STRING, "decrease",
6844 															SCCT_NONE, NULL, NULL, NULL);
6845 #ifdef DEBUG_ESTIMATORS_
6846 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of packet loss percentage !\n");
6847 #endif
6848 							switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO,
6849 															SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT,
6850 															(void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
6851 															SCCT_NONE, NULL, NULL, NULL);
6852 
6853 						} else if (rtt_valid && !rtt_increase && rtp_session->estimators[EST_LOSS]->val_estimate_last >= rtp_session->rtcp_frame.reports[i].loss_avg ) {
6854 							/* lossy because of congestion (queues full somewhere -> some packets are dropped , but RTT is good ), packet loss with many small gaps */
6855 #ifdef DEBUG_ESTIMATORS_
6856 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "packet loss, but RTT is not bad\n");
6857 #endif
6858 							switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO,
6859 															SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT,
6860 															(void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
6861 															SCCT_NONE, NULL, NULL, NULL);
6862 
6863 						} else if ((rtp_session->estimators[EST_LOSS]->val_estimate_last < 1) && packet_loss_increase) {
6864 #ifdef DEBUG_ESTIMATORS_
6865 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "small packet loss average\n");
6866 #endif
6867 							/*small loss_avg*/
6868 							switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO,
6869 															SWITCH_IO_WRITE, SCC_AUDIO_ADJUST_BITRATE,
6870 															SCCT_STRING, "default",
6871 															SCCT_NONE, NULL, NULL, NULL);
6872 
6873 							switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO,
6874 															SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT,
6875 															(void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
6876 															SCCT_NONE, NULL, NULL, NULL);
6877 
6878 						} else if ((rtp_session->estimators[EST_LOSS]->val_estimate_last < 5) &&
6879 								   (rtp_session->rtcp_frame.reports[i].rtt_avg < rtp_session->estimators[EST_RTT]->val_estimate_last))  {
6880 
6881 							/* estimate that packet loss will decrease, we can increase the bitrate */
6882 							switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO,
6883 															SWITCH_IO_WRITE, SCC_AUDIO_ADJUST_BITRATE,
6884 															SCCT_STRING, "increase",
6885 															SCCT_NONE, NULL, NULL, NULL);
6886 
6887 							switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO,
6888 															SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT,
6889 															(void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
6890 															SCCT_NONE, NULL, NULL, NULL);
6891 
6892 						} else {
6893 							/* *do nothing about bitrate, just pass the packet loss to the codec */
6894 #ifdef DEBUG_ESTIMATORS_
6895 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"do nothing about bitrate, just pass the packet loss to the codec\n");
6896 #endif
6897 							switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO,
6898 															SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT,
6899 															(void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
6900 															SCCT_NONE, NULL, NULL, NULL);
6901 						}
6902 					}
6903 				} else {
6904 					if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->rtcp_frame.reports[i].loss_avg != old_avg) {
6905 						switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO,
6906 														SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT,
6907 														(void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
6908 														SCCT_NONE, NULL, NULL, NULL);
6909 					}
6910 				}
6911 
6912 				report++;
6913 			}
6914 			rtp_session->rtcp_frame.report_count = (uint16_t)i;
6915 
6916 
6917 
6918 
6919 
6920 			rtp_session->rtcp_fresh_frame = 1;
6921 			rtp_session->stats.rtcp.peer_ssrc = ntohl(packet_ssrc);
6922 		}
6923 	}
6924 
6925 	if (msg->header.type > 194 && msg->header.type < 255) {
6926 		status = SWITCH_STATUS_SUCCESS;
6927 	}
6928 
6929 	return status;
6930 }
6931 
6932 
process_rtcp_packet(switch_rtp_t * rtp_session,switch_size_t * bytes)6933 static switch_status_t process_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes)
6934 {
6935 	switch_size_t len;
6936 	switch_size_t remain = *bytes;
6937 	switch_status_t status = SWITCH_STATUS_FALSE;
6938 	rtcp_msg_t *msg = rtp_session->rtcp_recv_msg_p;
6939 
6940 	if (remain < sizeof(switch_rtcp_ext_hdr_t) || remain > sizeof(rtcp_msg_t)) {
6941 		return status;
6942 	}
6943 	if (msg->header.version != 2) {
6944 		if (msg->header.version == 0) {
6945 			if (rtp_session->ice.ice_user) {
6946 				handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) msg, *bytes);
6947 			}
6948 			return SWITCH_STATUS_SUCCESS;
6949 		} else {
6950 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session),
6951 							  SWITCH_LOG_WARNING, "Received an unsupported RTCP packet version %d\n", msg->header.version);
6952 			return SWITCH_STATUS_FALSE;
6953 		}
6954 	}
6955 
6956 	do {
6957 		len = ((switch_size_t)ntohs(msg->header.length) * 4) + 4;
6958 
6959 		if (msg->header.version != 2 || !(msg->header.type > 191 && msg->header.type < 210)) {
6960 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING,
6961 							  "INVALID RTCP PACKET TYPE %d VER %d LEN %" SWITCH_SIZE_T_FMT "\n", msg->header.type,
6962 							  msg->header.version, len);
6963 			status = SWITCH_STATUS_BREAK;
6964 			break;
6965 		}
6966 
6967 		//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT,
6968 		//"WTF BYTES %ld REMAIN %ld PACKET TYPE %d LEN %ld\n", *bytes, remain, msg->header.type, len);
6969 
6970 		if (len > remain) {
6971 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING,
6972 							  "RTCP INVALID LENGTH %" SWITCH_SIZE_T_FMT "\n", len);
6973 			len = remain;
6974 		}
6975 
6976 		status = process_rtcp_report(rtp_session, msg, len);
6977 
6978 		if (remain > len) {
6979 			unsigned char *p = (unsigned char *) msg;
6980 			p += len;
6981 			msg = (rtcp_msg_t *) p;
6982 		}
6983 
6984 		remain -= len;
6985 
6986 	} while (remain >= 4);
6987 
6988 	return status;
6989 }
6990 
read_rtcp_packet(switch_rtp_t * rtp_session,switch_size_t * bytes,switch_frame_flag_t * flags)6991 static switch_status_t read_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags)
6992 {
6993 	switch_status_t status = SWITCH_STATUS_FALSE;
6994 
6995 	if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
6996 		return SWITCH_STATUS_FALSE;
6997 	}
6998 
6999 	switch_assert(bytes);
7000 
7001 	*bytes = sizeof(rtcp_msg_t);
7002 
7003 	if ((status = switch_socket_recvfrom(rtp_session->rtcp_from_addr, rtp_session->rtcp_sock_input, 0, (void *) rtp_session->rtcp_recv_msg_p, bytes))
7004 		!= SWITCH_STATUS_SUCCESS) {
7005 		*bytes = 0;
7006 	}
7007 
7008 	switch_mutex_lock(rtp_session->ice_mutex);
7009 	if (rtp_session->rtcp_dtls) {
7010 		char *b = (char *) rtp_session->rtcp_recv_msg_p;
7011 
7012 		if (*b == 0 || *b == 1) {
7013 			if (rtp_session->rtcp_ice.ice_user) {
7014 				handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) rtp_session->rtcp_recv_msg_p, *bytes);
7015 			}
7016 			*bytes = 0;
7017 		}
7018 
7019 		if (*bytes && (*b >= 20) && (*b <= 64)) {
7020 			rtp_session->rtcp_dtls->bytes = *bytes;
7021 			rtp_session->rtcp_dtls->data = (void *) rtp_session->rtcp_recv_msg_p;
7022 		} else {
7023 			rtp_session->rtcp_dtls->bytes = 0;
7024 			rtp_session->rtcp_dtls->data = NULL;
7025 		}
7026 
7027 		do_dtls(rtp_session, rtp_session->rtcp_dtls);
7028 
7029 
7030 		if (rtp_session->rtcp_dtls->bytes) {
7031 			*bytes = 0;
7032 		}
7033 	}
7034 	switch_mutex_unlock(rtp_session->ice_mutex);
7035 
7036 
7037 
7038 #ifdef ENABLE_SRTP
7039 	if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->rtcp_recv_msg_p->header.version == 2) {
7040 		//if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->rtcp_recv_msg_p->header.version == 2)) {
7041 		int sbytes = (int) *bytes;
7042 		srtp_err_status_t stat = 0;
7043 
7044 
7045 		if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) {
7046 			stat = srtp_unprotect_rtcp(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes);
7047 		} else {
7048 			stat = srtp_unprotect_rtcp_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes, 1);
7049 		}
7050 
7051 		if (stat) {
7052 			//++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++;
7053 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "RTCP UNPROTECT ERR\n");
7054 		} else {
7055 			//rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
7056 		}
7057 
7058 		*bytes = sbytes;
7059 
7060 	}
7061 #endif
7062 
7063 
7064 #ifdef ENABLE_ZRTP
7065 	if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && rtp_session->rtcp_recv_msg_p->header.version == 2) {
7066 		/* ZRTP Recv */
7067 		if (bytes) {
7068 			unsigned int sbytes = (int) *bytes;
7069 			zrtp_status_t stat = 0;
7070 
7071 			stat = zrtp_process_srtcp(rtp_session->zrtp_stream, (void *) rtp_session->rtcp_recv_msg_p, &sbytes);
7072 
7073 			switch (stat) {
7074 			case zrtp_status_ok:
7075 				*bytes = sbytes;
7076 				break;
7077 			case zrtp_status_drop:
7078 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
7079 				*bytes = 0;
7080 				break;
7081 			case zrtp_status_fail:
7082 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
7083 				*bytes = 0;
7084 				break;
7085 			default:
7086 				break;
7087 			}
7088 		}
7089 	}
7090 #endif
7091 
7092 	/* RTCP Auto ADJ */
7093 	if (*bytes && rtp_session->flags[SWITCH_RTP_FLAG_RTCP_AUTOADJ] &&  switch_sockaddr_get_port(rtp_session->rtcp_from_addr)) {
7094 			if (!switch_cmp_addr(rtp_session->rtcp_from_addr, rtp_session->rtcp_remote_addr)) {
7095 				if (++rtp_session->rtcp_autoadj_tally >= rtp_session->rtcp_autoadj_threshold) {
7096 					const char *err;
7097 					uint32_t old = rtp_session->remote_rtcp_port;
7098 					const char *tx_host;
7099 					const char *old_host;
7100 					char bufa[50], bufb[50];
7101 
7102 					tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_from_addr);
7103 					old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr);
7104 
7105 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO,
7106 									  "Auto Changing %s RTCP port from %s:%u to %s:%u\n", rtp_type(rtp_session), old_host, old, tx_host,
7107 									  switch_sockaddr_get_port(rtp_session->rtcp_from_addr));
7108 
7109 
7110 					rtp_session->eff_remote_host_str = switch_core_strdup(rtp_session->pool, tx_host);
7111 					rtp_session->remote_rtcp_port = switch_sockaddr_get_port(rtp_session->rtcp_from_addr);
7112 					status = enable_remote_rtcp_socket(rtp_session, &err);
7113 					rtp_session->rtcp_auto_adj_used = 1;
7114 
7115 					if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7116 						switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ);
7117 					} else {
7118 						switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ);
7119 					}
7120 				}
7121 			} else {
7122 
7123 				if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7124 					switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ);
7125 				} else {
7126 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session),
7127 									  SWITCH_LOG_DEBUG, "Correct %s RTCP ip/port confirmed.\n", rtp_type(rtp_session));
7128 					switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ);
7129 				}
7130 				rtp_session->rtcp_auto_adj_used = 0;
7131 
7132 			}
7133 	}
7134 
7135 	if (*bytes) {
7136 		return process_rtcp_packet(rtp_session, bytes);
7137 	}
7138 
7139 	return status;
7140 }
7141 
check_timeout(switch_rtp_t * rtp_session)7142 static void check_timeout(switch_rtp_t *rtp_session)
7143 {
7144 
7145 	switch_time_t now = switch_micro_time_now();
7146 	uint32_t elapsed = 0;
7147 
7148 	if (now >= rtp_session->last_media) {
7149 		elapsed = (now - rtp_session->last_media) / 1000;
7150 	}
7151 
7152 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10,
7153 					  "%s MEDIA TIMEOUT %s %d/%d", switch_core_session_get_name(rtp_session->session), rtp_type(rtp_session),
7154 					  elapsed, rtp_session->media_timeout);
7155 
7156 	if (elapsed > rtp_session->media_timeout) {
7157 
7158 		if (rtp_session->session) {
7159 			switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
7160 
7161 			switch_channel_execute_on(channel, "execute_on_media_timeout");
7162 			switch_channel_hangup(channel, SWITCH_CAUSE_MEDIA_TIMEOUT);
7163 		}
7164 	}
7165 }
7166 
rtp_common_read(switch_rtp_t * rtp_session,switch_payload_t * payload_type,payload_map_t ** pmapP,switch_frame_flag_t * flags,switch_io_flag_t io_flags)7167 static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_type,
7168 						   payload_map_t **pmapP, switch_frame_flag_t *flags, switch_io_flag_t io_flags)
7169 {
7170 
7171 	switch_channel_t *channel = NULL;
7172 	switch_size_t bytes = 0;
7173 	switch_size_t rtcp_bytes = 0;
7174 	switch_status_t status = SWITCH_STATUS_SUCCESS, poll_status = SWITCH_STATUS_SUCCESS;
7175 	switch_status_t rtcp_status = SWITCH_STATUS_SUCCESS, rtcp_poll_status = SWITCH_STATUS_SUCCESS;
7176 	int check = 0;
7177 	int ret = -1;
7178 	int sleep_mss = 1000;
7179 	int poll_sec = 5;
7180 	int poll_loop = 0;
7181 	int fdr = 0;
7182 	int rtcp_fdr = 0;
7183 	int hot_socket = 0;
7184 	int read_loops = 0;
7185 	int slept = 0;
7186 	switch_bool_t got_jb = SWITCH_FALSE;
7187 
7188 	if (!switch_rtp_ready(rtp_session)) {
7189 		return -1;
7190 	}
7191 
7192 	if (rtp_session->session) {
7193 		channel = switch_core_session_get_channel(rtp_session->session);
7194 	}
7195 
7196 	if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
7197 		sleep_mss = rtp_session->timer.interval * 1000;
7198 	}
7199 
7200 	READ_INC(rtp_session);
7201 
7202 
7203 
7204 	while (switch_rtp_ready(rtp_session)) {
7205 		int do_cng = 0;
7206 		int read_pretriggered = 0;
7207 		int has_rtcp = 0;
7208 		int got_rtp_poll = 0;
7209 
7210 		bytes = 0;
7211 
7212 		if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
7213 			!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] &&
7214 			!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
7215 			!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
7216 			rtp_session->read_pollfd) {
7217 
7218 			if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
7219 				while (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
7220 					status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, SWITCH_STATUS_SUCCESS, SWITCH_FALSE);
7221 
7222 					if (status == SWITCH_STATUS_GENERR) {
7223 						ret = -1;
7224 						goto end;
7225 					}
7226 
7227 					if ((*flags & SFF_RTCP)) {
7228 						*flags &= ~SFF_RTCP;
7229 						has_rtcp = 1;
7230 						read_pretriggered = 0;
7231 						goto rtcp;
7232 					}
7233 
7234 					if (status == SWITCH_STATUS_BREAK) {
7235 						read_pretriggered = 1;
7236 						break;
7237 					}
7238 				}
7239 
7240 			} else if ((rtp_session->flags[SWITCH_RTP_FLAG_AUTOFLUSH] || rtp_session->flags[SWITCH_RTP_FLAG_STICKY_FLUSH])) {
7241 
7242 				if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
7243 					status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, SWITCH_STATUS_SUCCESS, SWITCH_FALSE);
7244 					if (status == SWITCH_STATUS_GENERR) {
7245 						ret = -1;
7246 						goto end;
7247 					}
7248 					if ((*flags & SFF_RTCP)) {
7249 						*flags &= ~SFF_RTCP;
7250 						has_rtcp = 1;
7251 						read_pretriggered = 0;
7252 						goto rtcp;
7253 					}
7254 
7255 					/* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Initial (%i) %d\n", status, bytes); */
7256 					if (status != SWITCH_STATUS_FALSE) {
7257 						read_pretriggered = 1;
7258 					}
7259 
7260 					if (bytes) {
7261 						if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
7262 							rtp_session->hot_hits++;//+= rtp_session->samples_per_interval;
7263 
7264 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s Hot Hit %d\n",
7265 											  rtp_session_name(rtp_session),
7266 											  rtp_session->hot_hits);
7267 						} else {
7268 							rtp_session->hot_hits = 0;
7269 						}
7270 					}
7271 
7272 					if (rtp_session->hot_hits > 1 && !rtp_session->sync_packets) {// >= (rtp_session->samples_per_second * 30)) {
7273 						hot_socket = 1;
7274 					}
7275 				} else {
7276 					rtp_session->hot_hits = 0;
7277 				}
7278 			}
7279 
7280 			if (rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) {
7281 				///NOOP
7282 			} else if (hot_socket && (rtp_session->hot_hits % 10) != 0) {
7283 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s timer while HOT\n", rtp_session_name(rtp_session));
7284 				switch_core_timer_next(&rtp_session->timer);
7285 			} else if (hot_socket) {
7286 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s skip timer once\n", rtp_session_name(rtp_session));
7287 				rtp_session->sync_packets++;
7288 				switch_core_timer_sync(&rtp_session->timer);
7289 				reset_jitter_seq(rtp_session);
7290 			} else {
7291 
7292 				if (rtp_session->sync_packets) {
7293 
7294 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10,
7295 									  "%s Auto-Flush catching up %d packets (%d)ms.\n",
7296 									  rtp_session_name(rtp_session),
7297 									  rtp_session->sync_packets, (rtp_session->ms_per_packet * rtp_session->sync_packets) / 1000);
7298 					if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) {
7299 						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "%s syncing %d %s packet(s)\n",
7300 										 rtp_session_name(rtp_session),
7301 										  rtp_session->sync_packets, rtp_type(rtp_session));
7302 
7303 						rtp_session->bad_stream++;
7304 						rtp_session->stats.inbound.flaws += rtp_session->sync_packets;
7305 
7306 						if (rtp_session->stats.inbound.error_log) {
7307 							rtp_session->stats.inbound.error_log->flaws += rtp_session->sync_packets;
7308 						}
7309 					}
7310 
7311 					switch_core_timer_sync(&rtp_session->timer);
7312 					reset_jitter_seq(rtp_session);
7313 					rtp_session->hot_hits = 0;
7314 				} else {
7315 					if (slept) {
7316 						switch_cond_next();
7317 					} else {
7318 						if (rtp_session->skip_timer) {
7319 							rtp_session->skip_timer = 0;
7320 							switch_cond_next();
7321 						} else {
7322 							switch_core_timer_next(&rtp_session->timer);
7323 						}
7324 						slept++;
7325 					}
7326 
7327 				}
7328 
7329 				rtp_session->sync_packets = 0;
7330 			}
7331 		}
7332 
7333 		rtp_session->stats.read_count++;
7334 
7335 	recvfrom:
7336 
7337 		if (!read_pretriggered) {
7338 			bytes = 0;
7339 		}
7340 		read_loops++;
7341 		//poll_loop = 0;
7342 
7343 		if (!switch_rtp_ready(rtp_session)) {
7344 			break;
7345 		}
7346 
7347 		if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->read_pollfd) {
7348 			int pt = poll_sec * 1000000;
7349 
7350 			do_2833(rtp_session);
7351 
7352 			if (rtp_session->dtmf_data.out_digit_dur > 0 || rtp_session->dtmf_data.in_digit_sanity || rtp_session->sending_dtmf ||
7353 				switch_queue_size(rtp_session->dtmf_data.dtmf_queue) || switch_queue_size(rtp_session->dtmf_data.dtmf_inqueue)) {
7354 				pt = 20000;
7355 			}
7356 
7357 			if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
7358 				pt = 100000;
7359 			}
7360 
7361 			if (rtp_session->vb && !rtp_session->pause_jb) {
7362 				if (switch_jb_poll(rtp_session->vb)) {
7363 					pt = 1000;
7364 				}
7365 			}
7366 
7367 			if ((io_flags & SWITCH_IO_FLAG_NOBLOCK)) {
7368 				pt = 0;
7369 			}
7370 
7371 			poll_status = switch_poll(rtp_session->read_pollfd, 1, &fdr, pt);
7372 
7373 			if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && poll_status != SWITCH_STATUS_SUCCESS && rtp_session->media_timeout && rtp_session->last_media) {
7374 				check_timeout(rtp_session);
7375 			}
7376 
7377 			if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->dtmf_data.out_digit_dur > 0) {
7378 				return_cng_frame();
7379 			}
7380 
7381 			if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->flags[SWITCH_RTP_FLAG_BREAK]) {
7382 				switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_BREAK);
7383 				bytes = 0;
7384 				reset_jitter_seq(rtp_session);
7385 				return_cng_frame();
7386 			}
7387 
7388 		}
7389 
7390 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
7391 			got_jb = (rtp_session->vb && !rtp_session->pause_jb && switch_jb_poll(rtp_session->vb));
7392 		} else {
7393 			got_jb = SWITCH_TRUE;
7394 		}
7395 
7396 		if (poll_status == SWITCH_STATUS_SUCCESS || got_jb) {
7397 
7398 			got_rtp_poll = 1;
7399 
7400 			if (read_pretriggered) {
7401 				read_pretriggered = 0;
7402 			} else {
7403 
7404 
7405 				status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, poll_status, got_jb);
7406 
7407 				if (status == SWITCH_STATUS_GENERR) {
7408 					ret = -1;
7409 					goto end;
7410 				}
7411 
7412 				if (rtp_session->max_missed_packets && read_loops == 1 && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
7413 					!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
7414 					if (bytes && status == SWITCH_STATUS_SUCCESS) {
7415 						rtp_session->missed_count = 0;
7416 					} else {
7417 						if (rtp_session->media_timeout && rtp_session->last_media) {
7418 							check_timeout(rtp_session);
7419 						} else {
7420 							if (++rtp_session->missed_count >= rtp_session->max_missed_packets) {
7421 								ret = -2;
7422 								goto end;
7423 							}
7424 						}
7425 					}
7426 				}
7427 
7428 				if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
7429 					//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "Read bytes (%i) %ld\n", status, bytes);
7430 
7431 					if (bytes == 0) {
7432 						if (check_rtcp_and_ice(rtp_session) == -1) {
7433 							ret = -1;
7434 							goto end;
7435 						}
7436 						// This is dumb
7437 						//switch_rtp_video_refresh(rtp_session);
7438 						goto  rtcp;
7439 					}
7440 				}
7441 
7442 				if ((*flags & SFF_PROXY_PACKET)) {
7443 					ret = (int) bytes;
7444 					goto end;
7445 				}
7446 
7447 				if ((*flags & SFF_RTCP)) {
7448 					*flags &= ~SFF_RTCP;
7449 					has_rtcp = 1;
7450 					goto rtcp;
7451 				}
7452 
7453 
7454 			}
7455 			poll_loop = 0;
7456 		} else {
7457 
7458 			if (!switch_rtp_ready(rtp_session)) {
7459 				ret = -1;
7460 				goto end;
7461 			}
7462 
7463 			if (!SWITCH_STATUS_IS_BREAK(poll_status) && poll_status != SWITCH_STATUS_TIMEOUT) {
7464 				char tmp[128] = "";
7465 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Poll failed with error: %d [%s]\n",
7466 								  poll_status, switch_strerror_r(poll_status, tmp, sizeof(tmp)));
7467 				ret = -1;
7468 				goto end;
7469 			}
7470 
7471 			if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
7472 				rtp_session->missed_count += (poll_sec * 1000) / (rtp_session->ms_per_packet ? rtp_session->ms_per_packet / 1000 : 20);
7473 				bytes = 0;
7474 
7475 				if (rtp_session->media_timeout && rtp_session->last_media) {
7476 					check_timeout(rtp_session);
7477 				} else if (rtp_session->max_missed_packets) {
7478 					if (rtp_session->missed_count >= rtp_session->max_missed_packets) {
7479 						ret = -2;
7480 						goto end;
7481 					}
7482 				}
7483 			}
7484 
7485 
7486 			if (check_rtcp_and_ice(rtp_session) == -1) {
7487 				ret = -1;
7488 				goto end;
7489 			}
7490 
7491 
7492 			if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) &&
7493 				(rtp_session->dtmf_data.out_digit_dur == 0) && !rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
7494 				return_cng_frame();
7495 			}
7496 		}
7497 
7498 	rtcp:
7499 
7500 		if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
7501 			rtcp_poll_status = SWITCH_STATUS_FALSE;
7502 
7503 			if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX] && has_rtcp) {
7504 				if (rtp_session->rtcp_recv_msg_p->header.version == 2) { //rtcp muxed
7505 					rtp_session->rtcp_from_addr = rtp_session->from_addr;
7506 					rtcp_status = rtcp_poll_status = SWITCH_STATUS_SUCCESS;
7507 					rtcp_bytes = bytes;
7508 				}
7509 
7510 				has_rtcp = 0;
7511 
7512 			} else if (rtp_session->rtcp_read_pollfd) {
7513 				rtcp_poll_status = switch_poll(rtp_session->rtcp_read_pollfd, 1, &rtcp_fdr, 0);
7514 			}
7515 
7516 			if (rtcp_poll_status == SWITCH_STATUS_SUCCESS) {
7517 
7518 				if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
7519 					rtcp_status = read_rtcp_packet(rtp_session, &rtcp_bytes, flags);
7520 				}
7521 
7522 				if (rtcp_status == SWITCH_STATUS_SUCCESS) {
7523 					switch_rtp_reset_media_timer(rtp_session);
7524 
7525 					if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
7526 						switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
7527 						const char *uuid = switch_channel_get_partner_uuid(channel);
7528 
7529 						if (uuid) {
7530 							switch_core_session_t *other_session;
7531 							switch_rtp_t *other_rtp_session = NULL;
7532 
7533 							if ((other_session = switch_core_session_locate(uuid))) {
7534 								switch_channel_t *other_channel = switch_core_session_get_channel(other_session);
7535 								if ((other_rtp_session = switch_channel_get_private(other_channel, "__rtcp_audio_rtp_session")) &&
7536 									other_rtp_session->rtcp_sock_output &&
7537 									switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_ENABLE_RTCP)) {
7538 									other_rtp_session->rtcp_send_msg = rtp_session->rtcp_recv_msg;
7539 
7540 #ifdef ENABLE_SRTP
7541 									if (switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_SECURE_SEND)) {
7542 										int stat = 0;
7543 										int sbytes = (int) rtcp_bytes;
7544 
7545 										if (!other_rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) {
7546 											stat = srtp_protect_rtcp(other_rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &other_rtp_session->rtcp_send_msg.header, &sbytes);
7547 										} else {
7548 											stat = srtp_protect_rtcp_mki(other_rtp_session->send_ctx[other_rtp_session->srtp_idx_rtcp], &other_rtp_session->rtcp_send_msg.header, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX);
7549 										}
7550 
7551 										if (stat) {
7552 											switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
7553 										}
7554 										rtcp_bytes = sbytes;
7555 
7556 									}
7557 #endif
7558 
7559 #ifdef ENABLE_ZRTP
7560 									/* ZRTP Send */
7561 									if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
7562 										unsigned int sbytes = (unsigned int) bytes;
7563 										zrtp_status_t stat = zrtp_status_fail;
7564 
7565 										stat = zrtp_process_rtcp(other_rtp_session->zrtp_stream, (void *) &other_rtp_session->rtcp_send_msg, &sbytes);
7566 
7567 										switch (stat) {
7568 										case zrtp_status_ok:
7569 											break;
7570 										case zrtp_status_drop:
7571 											switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
7572 											ret = (int) bytes;
7573 											goto end;
7574 											break;
7575 										case zrtp_status_fail:
7576 											switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
7577 											break;
7578 										default:
7579 											break;
7580 										}
7581 
7582 										bytes = sbytes;
7583 									}
7584 #endif
7585 									if (switch_socket_sendto(other_rtp_session->rtcp_sock_output, other_rtp_session->rtcp_remote_addr, 0,
7586 															 (const char*)&other_rtp_session->rtcp_send_msg, &rtcp_bytes ) != SWITCH_STATUS_SUCCESS) {
7587 										switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,"RTCP packet not written\n");
7588 									}
7589 
7590 
7591 								}
7592 								switch_core_session_rwunlock(other_session);
7593 							}
7594 						}
7595 
7596 					}
7597 
7598 					if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
7599 						process_rtcp_packet(rtp_session, &rtcp_bytes);
7600 						ret = 1;
7601 
7602 						if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
7603 							switch_core_timer_sync(&rtp_session->timer);
7604 							reset_jitter_seq(rtp_session);
7605 						}
7606 						goto recvfrom;
7607 					}
7608 				}
7609 			}
7610 		}
7611 
7612 		if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) &&
7613 			(rtp_session->dtmf_data.out_digit_dur == 0) && !got_rtp_poll) {
7614 			return_cng_frame();
7615 		}
7616 
7617 		if (!bytes && (io_flags & SWITCH_IO_FLAG_NOBLOCK)) {
7618 			rtp_session->missed_count = 0;
7619 			ret = 0;
7620 			goto end;
7621 		}
7622 
7623 		check = !bytes;
7624 
7625 		if (rtp_session->flags[SWITCH_RTP_FLAG_FLUSH]) {
7626 			bytes = do_flush(rtp_session, SWITCH_FALSE, bytes);
7627 			switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
7628 		}
7629 
7630 		if ((!bytes && rtp_session->flags[SWITCH_RTP_FLAG_BREAK]) || (bytes && bytes == 4 && *((int *) &rtp_session->recv_msg) == UINT_MAX)) {
7631 			switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_BREAK);
7632 
7633 			if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] || !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] ||
7634 				rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] ||
7635 				(bytes && bytes < 5) || (!bytes && poll_loop)) {
7636 				bytes = 0;
7637 				reset_jitter_seq(rtp_session);
7638 				return_cng_frame();
7639 			}
7640 		}
7641 
7642 		if (bytes && bytes < 5) {
7643 			continue;
7644 		}
7645 
7646 		if (!bytes && poll_loop) {
7647 			goto recvfrom;
7648 		}
7649 
7650 		if (bytes && rtp_session->last_rtp_hdr.m && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
7651 			!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
7652 			!rtp_session->flags[SWITCH_RTP_FLAG_TEXT] &&
7653 			!(rtp_session->rtp_bugs & RTP_BUG_IGNORE_MARK_BIT)) {
7654 			rtp_flush_read_buffer(rtp_session, SWITCH_RTP_FLUSH_ONCE);
7655 		}
7656 
7657 		if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) {
7658 			*flags |= SFF_NOT_AUDIO;
7659 		} else {
7660 			*flags &= ~SFF_NOT_AUDIO; /* If this flag was already set, make sure to remove it when we get real audio */
7661 		}
7662 
7663 		/* ignore packets not meant for us unless the auto-adjust window is open (ice mode has its own alternatives to this) */
7664 		if (!using_ice(rtp_session) && bytes) {
7665 			if (rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ]) {
7666 				if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) {
7667 					goto recvfrom;
7668 
7669 				}
7670 			} else if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PACKETS) && !switch_cmp_addr(rtp_session->rtp_from_addr, rtp_session->remote_addr)) {
7671 				goto recvfrom;
7672 			}
7673 		}
7674 
7675 		if (bytes && rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ] && switch_sockaddr_get_port(rtp_session->rtp_from_addr)) {
7676 			if (!switch_cmp_addr(rtp_session->rtp_from_addr, rtp_session->remote_addr)) {
7677 				if (++rtp_session->autoadj_tally >= rtp_session->autoadj_threshold) {
7678 					const char *err;
7679 					uint32_t old = rtp_session->remote_port;
7680 					const char *tx_host;
7681 					const char *old_host;
7682 					char bufa[50], bufb[50];
7683 					char adj_port[6];
7684 
7685 					tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
7686 					old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
7687 
7688 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO,
7689 									  "Auto Changing %s port from %s:%u to %s:%u\n", rtp_type(rtp_session), old_host, old, tx_host,
7690 									  switch_sockaddr_get_port(rtp_session->rtp_from_addr));
7691 
7692 					if (channel) {
7693 						char varname[80] = "";
7694 
7695 						switch_snprintf(varname, sizeof(varname), "remote_%s_ip_reported", rtp_type(rtp_session));
7696 						switch_channel_set_variable(channel, varname, switch_channel_get_variable(channel, "remote_media_ip"));
7697 
7698 						switch_snprintf(varname, sizeof(varname), "remote_%s_ip", rtp_type(rtp_session));
7699 						switch_channel_set_variable(channel, varname, tx_host);
7700 
7701 						switch_snprintf(varname, sizeof(varname), "remote_%s_port_reported", rtp_type(rtp_session));
7702 						switch_snprintf(adj_port, sizeof(adj_port), "%u", switch_sockaddr_get_port(rtp_session->rtp_from_addr));
7703 						switch_channel_set_variable(channel, varname, switch_channel_get_variable(channel, "remote_media_port"));
7704 
7705 						switch_snprintf(varname, sizeof(varname), "remote_%s_port", rtp_type(rtp_session));
7706 						switch_channel_set_variable(channel, varname, adj_port);
7707 
7708 						switch_snprintf(varname, sizeof(varname), "rtp_auto_adjust_%s", rtp_type(rtp_session));
7709 						switch_channel_set_variable(channel, varname, "true");
7710 					}
7711 					rtp_session->auto_adj_used = 1;
7712 					switch_rtp_set_remote_address(rtp_session, tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr), 0, SWITCH_FALSE, &err);
7713 					if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7714 						switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
7715 						switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ);
7716 					} else {
7717 						switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
7718 					}
7719 					if (rtp_session->ice.ice_user) {
7720 						rtp_session->ice.addr = rtp_session->remote_addr;
7721 					}
7722 				}
7723 			} else {
7724 				if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7725 					switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
7726 					switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ);
7727 				} else {
7728 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Correct %s ip/port confirmed.\n", rtp_type(rtp_session));
7729 					switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
7730 				}
7731 				rtp_session->auto_adj_used = 0;
7732 			}
7733 		}
7734 
7735 		if (bytes && !(rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST) && rtp_session->autoadj_window) {
7736 			if (--rtp_session->autoadj_window == 0) {
7737 				switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
7738 			}
7739 		}
7740 
7741 		if (rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) {
7742 			if (!bytes) {
7743 				if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
7744 					switch_core_timer_next(&rtp_session->timer);
7745 				}
7746 				return_cng_frame();
7747 			} else {
7748 				*payload_type = rtp_session->last_rtp_hdr.pt;
7749 				ret = (int) bytes;
7750 				goto end;
7751 			}
7752 		}
7753 
7754 		if (bytes && (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL])) {
7755 			/* Fast PASS! */
7756 			*flags |= SFF_PROXY_PACKET;
7757 
7758 			if (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
7759 #if 0
7760 				if (rtp_session->has_rtp && check_recv_payload(rtp_session)) {
7761 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING,
7762 									  "Ignoring udptl packet of size of %ld bytes that looks strikingly like a RTP packet.\n", (long)bytes);
7763 					bytes = 0;
7764 					goto do_continue;
7765 				}
7766 #endif
7767 				*flags |= SFF_UDPTL_PACKET;
7768 			}
7769 
7770 			ret = (int) bytes;
7771 			goto end;
7772 		}
7773 
7774 		if (bytes) {
7775 			rtp_session->missed_count = 0;
7776 
7777 			if (bytes < rtp_header_len) {
7778 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Ignoring invalid RTP packet size of %ld bytes.\n", (long)bytes);
7779 				bytes = 0;
7780 				goto do_continue;
7781 			}
7782 
7783 			if (rtp_session->last_rtp_hdr.pt && (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13)) {
7784 				return_cng_frame();
7785 			}
7786 		}
7787 
7788 		if (check || bytes) {
7789 			do_2833(rtp_session);
7790 		}
7791 
7792 		if (bytes && rtp_session->recv_msg.header.version != 2) {
7793 			uint8_t *data = (uint8_t *) RTP_BODY(rtp_session);
7794 
7795 			//if (rtp_session->recv_msg.header.version == 0) {
7796 			//	if (rtp_session->ice.ice_user) {
7797 			//		handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, bytes);
7798 			//		goto recvfrom;
7799 			//	}
7800 			//}
7801 
7802 			if (rtp_session->invalid_handler) {
7803 				rtp_session->invalid_handler(rtp_session, rtp_session->sock_input, (void *) &rtp_session->recv_msg, bytes, rtp_session->rtp_from_addr);
7804 			}
7805 
7806 			memset(data, 0, 2);
7807 			data[0] = 65;
7808 
7809 			rtp_session->last_rtp_hdr.pt = rtp_session->cng_pt != INVALID_PT ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD;
7810 			*flags |= SFF_CNG;
7811 			*payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
7812 			ret = 2 + rtp_header_len;
7813 			goto end;
7814 		} else if (bytes) {
7815 			rtp_session->stats.inbound.period_packet_count++;
7816 		}
7817 
7818 
7819 		/* Handle incoming RFC2833 packets */
7820 		switch (handle_rfc2833(rtp_session, bytes, &do_cng)) {
7821 		case RESULT_GOTO_END:
7822 			goto end;
7823 		case RESULT_GOTO_RECVFROM:
7824 			goto recvfrom;
7825 		case RESULT_GOTO_TIMERCHECK:
7826 			goto timer_check;
7827 		case RESULT_CONTINUE:
7828 			status = SWITCH_STATUS_SUCCESS;
7829 			goto result_continue;
7830 		}
7831 
7832 	result_continue:
7833 	timer_check:
7834 
7835 		if (!rtp_session->media_timeout && rtp_session->flags[SWITCH_RTP_FLAG_MUTE]) {
7836 			do_cng++;
7837 		}
7838 
7839 		if (do_cng) {
7840 			uint8_t *data = (uint8_t *) RTP_BODY(rtp_session);
7841 
7842 			do_2833(rtp_session);
7843 
7844 			if (rtp_session->last_cng_ts == rtp_session->last_read_ts + rtp_session->samples_per_interval) {
7845 				rtp_session->last_cng_ts = 0;
7846 			} else {
7847 				rtp_session->last_cng_ts = rtp_session->last_read_ts + rtp_session->samples_per_interval;
7848 			}
7849 
7850 			memset(data, 0, 2);
7851 			data[0] = 65;
7852 			rtp_session->last_rtp_hdr.pt = rtp_session->cng_pt != INVALID_PT ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD;
7853 			*flags |= SFF_CNG;
7854 			*payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
7855 			ret = 2 + rtp_header_len;
7856 			rtp_session->stats.inbound.skip_packet_count++;
7857 			goto end;
7858 		}
7859 
7860 
7861 		if (check || (bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER])) {
7862 			if (!bytes && rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {	/* We're late! We're Late! */
7863 				if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] && status == SWITCH_STATUS_BREAK) {
7864 					switch_cond_next();
7865 					continue;
7866 				}
7867 
7868 
7869 
7870 				if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] && !rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] && !rtp_session->dtmf_data.in_digit_ts
7871 					&& rtp_session->cng_count > (rtp_session->one_second * 2) && rtp_session->jitter_lead > JITTER_LEAD_FRAMES) {
7872 
7873 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s %s timeout\n",
7874 									  rtp_session_name(rtp_session), rtp_type(rtp_session));
7875 
7876 					if (rtp_session->media_timeout && rtp_session->last_media) {
7877 						check_timeout(rtp_session);
7878 					}
7879 
7880 					if (rtp_session->stats.inbound.error_log) {
7881 						rtp_session->stats.inbound.error_log->flaws++;
7882 					}
7883 					rtp_session->stats.inbound.flaws++;
7884 					do_mos(rtp_session);
7885 				}
7886 
7887 				rtp_session->cng_count++;
7888 				return_cng_frame();
7889 			}
7890 		}
7891 
7892 		rtp_session->cng_count = 0;
7893 
7894 		if (status == SWITCH_STATUS_BREAK || bytes == 0) {
7895 			if (!(io_flags & SWITCH_IO_FLAG_SINGLE_READ) && rtp_session->flags[SWITCH_RTP_FLAG_DATAWAIT]) {
7896 				goto do_continue;
7897 			}
7898 			return_cng_frame();
7899 		}
7900 
7901 		if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->last_rtp_hdr.pt == 102) {
7902 			rtp_session->last_rtp_hdr.pt = 97;
7903 		}
7904 
7905 		break;
7906 
7907 	do_continue:
7908 
7909 		if (!bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
7910 
7911 			if (sleep_mss) {
7912 				switch_yield(sleep_mss);
7913 			}
7914 		}
7915 
7916 	}
7917 
7918 	if (switch_rtp_ready(rtp_session)) {
7919 		*payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
7920 
7921 		if (*payload_type == SWITCH_RTP_CNG_PAYLOAD) {
7922 			*flags |= SFF_CNG;
7923 		}
7924 
7925 		ret = (int) bytes;
7926 	} else {
7927 		ret = -1;
7928 	}
7929 
7930  end:
7931 
7932 	READ_DEC(rtp_session);
7933 
7934 	return ret;
7935 }
7936 
7937 
switch_rtp_check_auto_adj(switch_rtp_t * rtp_session)7938 SWITCH_DECLARE(switch_byte_t) switch_rtp_check_auto_adj(switch_rtp_t *rtp_session)
7939 {
7940 	return rtp_session->auto_adj_used;
7941 }
7942 
switch_rtp_has_dtmf(switch_rtp_t * rtp_session)7943 SWITCH_DECLARE(switch_size_t) switch_rtp_has_dtmf(switch_rtp_t *rtp_session)
7944 {
7945 	switch_size_t has = 0;
7946 
7947 	if (switch_rtp_ready(rtp_session)) {
7948 		switch_mutex_lock(rtp_session->dtmf_data.dtmf_mutex);
7949 		has = switch_queue_size(rtp_session->dtmf_data.dtmf_inqueue);
7950 		switch_mutex_unlock(rtp_session->dtmf_data.dtmf_mutex);
7951 	}
7952 
7953 	return has;
7954 }
7955 
switch_rtp_dequeue_dtmf(switch_rtp_t * rtp_session,switch_dtmf_t * dtmf)7956 SWITCH_DECLARE(switch_size_t) switch_rtp_dequeue_dtmf(switch_rtp_t *rtp_session, switch_dtmf_t *dtmf)
7957 {
7958 	switch_size_t bytes = 0;
7959 	switch_dtmf_t *_dtmf = NULL;
7960 	void *pop;
7961 
7962 	if (!switch_rtp_ready(rtp_session)) {
7963 		return bytes;
7964 	}
7965 
7966 	switch_mutex_lock(rtp_session->dtmf_data.dtmf_mutex);
7967 	if (switch_queue_trypop(rtp_session->dtmf_data.dtmf_inqueue, &pop) == SWITCH_STATUS_SUCCESS) {
7968 
7969 		_dtmf = (switch_dtmf_t *)pop;
7970 		*dtmf = *_dtmf;
7971 		/* Only log DTMF buffer if sensitive_dtmf channel variable not set to true */
7972 		if (!(switch_channel_var_true(switch_core_session_get_channel(rtp_session->session), SWITCH_SENSITIVE_DTMF_VARIABLE))) {
7973 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,"RTP RECV DTMF %c:%d\n", dtmf->digit, dtmf->duration);
7974 		}
7975 		bytes++;
7976 		free(pop);
7977 	}
7978 	switch_mutex_unlock(rtp_session->dtmf_data.dtmf_mutex);
7979 
7980 	return bytes;
7981 }
7982 
switch_rtp_queue_rfc2833(switch_rtp_t * rtp_session,const switch_dtmf_t * dtmf)7983 SWITCH_DECLARE(switch_status_t) switch_rtp_queue_rfc2833(switch_rtp_t *rtp_session, const switch_dtmf_t *dtmf)
7984 {
7985 
7986 	switch_dtmf_t *rdigit;
7987 
7988 	if (!switch_rtp_ready(rtp_session)) {
7989 		return SWITCH_STATUS_FALSE;
7990 	}
7991 
7992 	if ((rdigit = malloc(sizeof(*rdigit))) != 0) {
7993 		*rdigit = *dtmf;
7994 		if (rdigit->duration < switch_core_min_dtmf_duration(0)) {
7995 			rdigit->duration = switch_core_min_dtmf_duration(0);
7996 		}
7997 
7998 		if ((switch_queue_trypush(rtp_session->dtmf_data.dtmf_queue, rdigit)) != SWITCH_STATUS_SUCCESS) {
7999 			free(rdigit);
8000 			return SWITCH_STATUS_FALSE;
8001 		}
8002 	} else {
8003 		abort();
8004 	}
8005 
8006 	return SWITCH_STATUS_SUCCESS;
8007 }
8008 
switch_rtp_queue_rfc2833_in(switch_rtp_t * rtp_session,const switch_dtmf_t * dtmf)8009 SWITCH_DECLARE(switch_status_t) switch_rtp_queue_rfc2833_in(switch_rtp_t *rtp_session, const switch_dtmf_t *dtmf)
8010 {
8011 	switch_dtmf_t *rdigit;
8012 
8013 	if (!switch_rtp_ready(rtp_session)) {
8014 		return SWITCH_STATUS_FALSE;
8015 	}
8016 
8017 	if ((rdigit = malloc(sizeof(*rdigit))) != 0) {
8018 		*rdigit = *dtmf;
8019 		if (rdigit->duration < switch_core_min_dtmf_duration(0)) {
8020 			rdigit->duration = switch_core_min_dtmf_duration(0);
8021 		}
8022 
8023 		if ((switch_queue_trypush(rtp_session->dtmf_data.dtmf_inqueue, rdigit)) != SWITCH_STATUS_SUCCESS) {
8024 			free(rdigit);
8025 			return SWITCH_STATUS_FALSE;
8026 		}
8027 	} else {
8028 		abort();
8029 	}
8030 
8031 	return SWITCH_STATUS_SUCCESS;
8032 }
8033 
switch_rtp_read(switch_rtp_t * rtp_session,void * data,uint32_t * datalen,switch_payload_t * payload_type,switch_frame_flag_t * flags,switch_io_flag_t io_flags)8034 SWITCH_DECLARE(switch_status_t) switch_rtp_read(switch_rtp_t *rtp_session, void *data, uint32_t *datalen,
8035 												switch_payload_t *payload_type, switch_frame_flag_t *flags, switch_io_flag_t io_flags)
8036 {
8037 	int bytes = 0;
8038 
8039 	if (!switch_rtp_ready(rtp_session)) {
8040 		return SWITCH_STATUS_FALSE;
8041 	}
8042 
8043 	bytes = rtp_common_read(rtp_session, payload_type, NULL, flags, io_flags);
8044 
8045 	if (bytes < 0) {
8046 		*datalen = 0;
8047 		return bytes == -2 ? SWITCH_STATUS_TIMEOUT : SWITCH_STATUS_GENERR;
8048 	} else if (bytes == 0) {
8049 		*datalen = 0;
8050 		return SWITCH_STATUS_BREAK;
8051 	} else {
8052 		if (bytes > rtp_header_len) {
8053 			bytes -= rtp_header_len;
8054 		}
8055 	}
8056 
8057 	*datalen = bytes;
8058 
8059 	memcpy(data, RTP_BODY(rtp_session), bytes);
8060 
8061 	return SWITCH_STATUS_SUCCESS;
8062 }
8063 
switch_rtcp_zerocopy_read_frame(switch_rtp_t * rtp_session,switch_rtcp_frame_t * frame)8064 SWITCH_DECLARE(switch_status_t) switch_rtcp_zerocopy_read_frame(switch_rtp_t *rtp_session, switch_rtcp_frame_t *frame)
8065 {
8066 
8067 	if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
8068 		return SWITCH_STATUS_FALSE;
8069 	}
8070 
8071 	/* A fresh frame has been found! */
8072 	if (rtp_session->rtcp_fresh_frame) {
8073 		/* turn the flag off! */
8074 		rtp_session->rtcp_fresh_frame = 0;
8075 
8076 		*frame = rtp_session->rtcp_frame;
8077 
8078 		return SWITCH_STATUS_SUCCESS;
8079 	}
8080 
8081 	return SWITCH_STATUS_TIMEOUT;
8082 }
8083 
switch_rtp_zerocopy_read_frame(switch_rtp_t * rtp_session,switch_frame_t * frame,switch_io_flag_t io_flags)8084 SWITCH_DECLARE(switch_status_t) switch_rtp_zerocopy_read_frame(switch_rtp_t *rtp_session, switch_frame_t *frame, switch_io_flag_t io_flags)
8085 {
8086 	int bytes = 0;
8087 
8088 	if (!switch_rtp_ready(rtp_session)) {
8089 		return SWITCH_STATUS_FALSE;
8090 	}
8091 
8092 	bytes = rtp_common_read(rtp_session, &frame->payload, &frame->pmap, &frame->flags, io_flags);
8093 
8094 	frame->data = RTP_BODY(rtp_session);
8095 
8096 	if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && (bytes < rtp_header_len || switch_test_flag(frame, SFF_CNG))) {
8097 		frame->packet = NULL;
8098 		frame->timestamp = 0;
8099 		frame->seq = 0;
8100 		frame->ssrc = 0;
8101 		frame->m = 0;
8102 	} else {
8103 
8104 		frame->packet = &rtp_session->recv_msg;
8105 		frame->packetlen = bytes;
8106 		frame->source = __FILE__;
8107 
8108 		switch_set_flag(frame, SFF_RAW_RTP);
8109 		switch_set_flag(frame, SFF_EXTERNAL);
8110 		if (frame->payload == rtp_session->recv_te) {
8111 			switch_set_flag(frame, SFF_RFC2833);
8112 		}
8113 		frame->timestamp = ntohl(rtp_session->last_rtp_hdr.ts);
8114 		frame->seq = (uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
8115 		frame->ssrc = ntohl(rtp_session->last_rtp_hdr.ssrc);
8116 		frame->m = rtp_session->last_rtp_hdr.m ? SWITCH_TRUE : SWITCH_FALSE;
8117 	}
8118 
8119 #ifdef ENABLE_ZRTP
8120 	if (zrtp_on && rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV]) {
8121 		zrtp_session_info_t zrtp_session_info;
8122 
8123 		if (rtp_session->zrtp_session && (zrtp_status_ok == zrtp_session_get(rtp_session->zrtp_session, &zrtp_session_info))) {
8124 			if (zrtp_session_info.sas_is_ready) {
8125 
8126 				switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
8127 
8128 				const char *uuid = switch_channel_get_partner_uuid(channel);
8129 				if (uuid) {
8130 					switch_core_session_t *other_session;
8131 
8132 					if ((other_session = switch_core_session_locate(uuid))) {
8133 						switch_channel_t *other_channel = switch_core_session_get_channel(other_session);
8134 						switch_rtp_t *other_rtp_session = switch_channel_get_private(other_channel, "__zrtp_audio_rtp_session");
8135 
8136 						if (other_rtp_session) {
8137 							if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND) {
8138 								switch_mutex_lock(other_rtp_session->read_mutex);
8139 								if (zrtp_status_ok == zrtp_session_get(other_rtp_session->zrtp_session, &zrtp_session_info)) {
8140 									if (rtp_session->zrtp_mitm_tries > ZRTP_MITM_TRIES) {
8141 										switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV);
8142 										switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND);
8143 										rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
8144 										rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
8145 									} else if (zrtp_status_ok == zrtp_resolve_mitm_call(other_rtp_session->zrtp_stream, rtp_session->zrtp_stream)) {
8146 										rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
8147 										rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
8148 										switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV);
8149 										switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND);
8150 										rtp_session->zrtp_mitm_tries++;
8151 									}
8152 								}
8153 								switch_mutex_unlock(other_rtp_session->read_mutex);
8154 							}
8155 						}
8156 
8157 						switch_core_session_rwunlock(other_session);
8158 					}
8159 				}
8160 			}
8161 		} else {
8162 			rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
8163 			rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
8164 		}
8165 	}
8166 #endif
8167 
8168 	if (bytes < 0) {
8169 		frame->datalen = 0;
8170 		return bytes == -2 ? SWITCH_STATUS_TIMEOUT : SWITCH_STATUS_GENERR;
8171 	} else if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
8172 		if (bytes < rtp_header_len) {
8173 			frame->datalen = 0;
8174 			return SWITCH_STATUS_BREAK;
8175 		} else {
8176 			bytes -= rtp_header_len;
8177 		}
8178 	}
8179 
8180 	frame->datalen = bytes;
8181 	return SWITCH_STATUS_SUCCESS;
8182 }
8183 
switch_rtp_zerocopy_read(switch_rtp_t * rtp_session,void ** data,uint32_t * datalen,switch_payload_t * payload_type,switch_frame_flag_t * flags,switch_io_flag_t io_flags)8184 SWITCH_DECLARE(switch_status_t) switch_rtp_zerocopy_read(switch_rtp_t *rtp_session,
8185 														 void **data, uint32_t *datalen, switch_payload_t *payload_type, switch_frame_flag_t *flags,
8186 														 switch_io_flag_t io_flags)
8187 {
8188 	int bytes = 0;
8189 
8190 	if (!switch_rtp_ready(rtp_session)) {
8191 		return SWITCH_STATUS_FALSE;
8192 	}
8193 
8194 	bytes = rtp_common_read(rtp_session, payload_type, NULL, flags, io_flags);
8195 	*data = RTP_BODY(rtp_session);
8196 
8197 	if (bytes < 0) {
8198 		*datalen = 0;
8199 		return SWITCH_STATUS_GENERR;
8200 	} else {
8201 		if (bytes > rtp_header_len) {
8202 			bytes -= rtp_header_len;
8203 		}
8204 	}
8205 
8206 	*datalen = bytes;
8207 	return SWITCH_STATUS_SUCCESS;
8208 }
8209 
rtp_write_ready(switch_rtp_t * rtp_session,uint32_t bytes,int line)8210 static int rtp_write_ready(switch_rtp_t *rtp_session, uint32_t bytes, int line)
8211 {
8212 	if (!rtp_session) return 0;
8213 
8214 	if (rtp_session->ice.ice_user && !(rtp_session->ice.rready || rtp_session->ice.ready)) {
8215 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "Skip sending %s packet %ld bytes (ice not ready @ line %d!)\n",
8216 						  rtp_type(rtp_session), (long)bytes, line);
8217 		return 0;
8218 	}
8219 
8220 	if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) {
8221 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "Skip sending %s packet %ld bytes (dtls not ready @ line %d!)\n",
8222 						  rtp_type(rtp_session), (long)bytes, line);
8223 		return 0;
8224 	}
8225 
8226 	return 1;
8227 }
8228 
8229 
rtp_common_write(switch_rtp_t * rtp_session,rtp_msg_t * send_msg,void * data,uint32_t datalen,switch_payload_t payload,uint32_t timestamp,switch_frame_flag_t * flags)8230 static int rtp_common_write(switch_rtp_t *rtp_session,
8231 							rtp_msg_t *send_msg, void *data, uint32_t datalen, switch_payload_t payload, uint32_t timestamp, switch_frame_flag_t *flags)
8232 {
8233 	switch_size_t bytes;
8234 	uint8_t send = 1;
8235 	uint32_t this_ts = 0;
8236 	int ret;
8237 	switch_time_t now;
8238 	uint8_t m = 0;
8239 
8240 	if (!switch_rtp_ready(rtp_session)) {
8241 		return -1;
8242 	}
8243 
8244 	if (!rtp_write_ready(rtp_session, datalen, __LINE__)) {
8245 		return 0;
8246 	}
8247 
8248 	WRITE_INC(rtp_session);
8249 
8250 	if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
8251 		//switch_core_timer_sync(&rtp_session->write_timer);
8252 	}
8253 
8254 	if (send_msg) {
8255 		bytes = datalen;
8256 
8257 		m = (uint8_t) send_msg->header.m;
8258 		rtp_session->ts = ntohl(send_msg->header.ts);
8259 
8260 		if (flags && *flags & SFF_RFC2833) {
8261 			if (rtp_session->te == INVALID_PT) {
8262 				ret = 0;
8263 				goto end;
8264 			}
8265 			send_msg->header.pt = rtp_session->te;
8266 		}
8267 		data = send_msg->body;
8268 		if (datalen > rtp_header_len) {
8269 			datalen -= rtp_header_len;
8270 		}
8271 	} else {
8272 		if (*flags & SFF_RFC2833) {
8273 			if (rtp_session->te == INVALID_PT) {
8274 				ret = 0;
8275 				goto end;
8276 			}
8277 			payload = rtp_session->te;
8278 		}
8279 
8280 		send_msg = &rtp_session->send_msg;
8281 		send_msg->header.pt = payload;
8282 
8283 		m = get_next_write_ts(rtp_session, timestamp);
8284 
8285 		rtp_session->send_msg.header.ts = htonl(rtp_session->ts);
8286 
8287 		memcpy(send_msg->body, data, datalen);
8288 		bytes = datalen + rtp_header_len;
8289 	}
8290 
8291 	if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
8292 
8293 		if ((rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
8294 			m = 0;
8295 		} else {
8296 			int delta = rtp_session->ts - rtp_session->last_write_ts;
8297 
8298 			if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
8299 				((!rtp_session->flags[SWITCH_RTP_FLAG_RESET] && (abs(delta) > rtp_session->samples_per_interval * 10))
8300 				|| rtp_session->ts == rtp_session->samples_per_interval)) {
8301 				m++;
8302 			}
8303 
8304 			if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
8305 				//switch_core_timer_sync(&rtp_session->write_timer);
8306 			}
8307 
8308 			if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
8309 				(rtp_session->write_timer.samplecount - rtp_session->last_write_samplecount) > rtp_session->samples_per_interval * 10) {
8310 				m++;
8311 			}
8312 
8313 			if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
8314 				((unsigned) ((switch_micro_time_now() - rtp_session->last_write_timestamp))) > (rtp_session->ms_per_packet * 10)) {
8315 				m++;
8316 			}
8317 
8318 			if (rtp_session->cn && payload != rtp_session->cng_pt) {
8319 				rtp_session->cn = 0;
8320 				m++;
8321 			}
8322 
8323 			if (rtp_session->need_mark && !rtp_session->sending_dtmf) {
8324 				m++;
8325 				rtp_session->need_mark = 0;
8326 			}
8327 		}
8328 
8329 		if (m) {
8330 			rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
8331 			rtp_session->ts = 0;
8332 		}
8333 
8334 		/* If the marker was set, and the timestamp seems to have started over - set a new SSRC, to indicate this is a new stream */
8335 		if (m && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND) && (rtp_session->rtp_bugs & RTP_BUG_CHANGE_SSRC_ON_MARKER) &&
8336 			(rtp_session->flags[SWITCH_RTP_FLAG_RESET] || (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->last_write_ts > 0))) {
8337 			switch_rtp_set_ssrc(rtp_session, (uint32_t) ((intptr_t) rtp_session + (uint32_t) switch_epoch_time_now(NULL)));
8338 		}
8339 
8340 		if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO) && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_UDPTL)) {
8341 			send_msg->header.m = (m && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) ? 1 : 0;
8342 		}
8343 	}
8344 
8345 	if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
8346 		int external = (*flags & SFF_EXTERNAL);
8347 		/* Normalize the timestamps to our own base by generating a made up starting point then adding the measured deltas to that base
8348 		   so if the timestamps and ssrc of the source change, it will not break the other end's jitter bufffer / decoder etc *cough* CHROME *cough*
8349 		 */
8350 
8351 		if (!rtp_session->ts_norm.ts) {
8352 			rtp_session->ts_norm.ts = (uint32_t) rand() % 1000000 + 1;
8353 		}
8354 
8355 		if (!rtp_session->ts_norm.last_ssrc || send_msg->header.ssrc != rtp_session->ts_norm.last_ssrc || rtp_session->ts_norm.last_external != external) {
8356 			switch_core_session_t *other_session;
8357 
8358 			switch_core_session_request_video_refresh(rtp_session->session);
8359 			switch_core_media_gen_key_frame(rtp_session->session);
8360 
8361 			if (switch_core_session_get_partner(rtp_session->session, &other_session) == SWITCH_STATUS_SUCCESS) {
8362 				switch_core_session_request_video_refresh(other_session);
8363 				switch_core_media_gen_key_frame(other_session);
8364 				switch_core_session_rwunlock(other_session);
8365 			}
8366 
8367 			if (rtp_session->ts_norm.last_ssrc) {
8368 				rtp_session->ts_norm.delta_ttl = 0;
8369 				rtp_session->ts_norm.ts++;
8370 			}
8371 
8372 			rtp_session->ts_norm.last_ssrc = send_msg->header.ssrc;
8373 			rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts);
8374 		}
8375 
8376 		rtp_session->ts_norm.last_external = external;
8377 
8378 		if (ntohl(send_msg->header.ts) != rtp_session->ts_norm.last_frame) {
8379 			int32_t delta = ntohl(send_msg->header.ts) - rtp_session->ts_norm.last_frame;
8380 
8381 			if (delta < 0 || delta > 90000) {
8382 				switch_core_media_gen_key_frame(rtp_session->session);
8383 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1,
8384 								  "Timestamp shift detected last: %d this: %d delta: %d stick with prev delta: %d\n",
8385 								  rtp_session->ts_norm.last_frame, ntohl(send_msg->header.ts), delta, rtp_session->ts_norm.delta);
8386 			} else {
8387 				rtp_session->ts_norm.delta = delta;
8388 			}
8389 
8390 			rtp_session->ts_norm.ts += rtp_session->ts_norm.delta;
8391 
8392 		}
8393 
8394 		rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts);
8395 		send_msg->header.ts = htonl(rtp_session->ts_norm.ts);
8396 		this_ts = rtp_session->ts_norm.ts;
8397 	}
8398 
8399 	send_msg->header.ssrc = htonl(rtp_session->ssrc);
8400 
8401 	if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->send_msg.header.pt == 97) {
8402 		rtp_session->last_rtp_hdr.pt = 102;
8403 	}
8404 
8405 	if (rtp_session->flags[SWITCH_RTP_FLAG_VAD] &&
8406 		rtp_session->last_rtp_hdr.pt == rtp_session->vad_data.read_codec->implementation->ianacode) {
8407 
8408 		int16_t decoded[SWITCH_RECOMMENDED_BUFFER_SIZE / sizeof(int16_t)] = { 0 };
8409 		uint32_t rate = 0;
8410 		uint32_t codec_flags = 0;
8411 		uint32_t len = sizeof(decoded);
8412 		time_t now = switch_epoch_time_now(NULL);
8413 		send = 0;
8414 
8415 		if (rtp_session->vad_data.scan_freq && rtp_session->vad_data.next_scan <= now) {
8416 			rtp_session->vad_data.bg_count = rtp_session->vad_data.bg_level = 0;
8417 			rtp_session->vad_data.next_scan = now + rtp_session->vad_data.scan_freq;
8418 		}
8419 
8420 		if (switch_core_codec_decode(&rtp_session->vad_data.vad_codec,
8421 									 rtp_session->vad_data.read_codec,
8422 									 data,
8423 									 datalen,
8424 									 rtp_session->vad_data.read_codec->implementation->actual_samples_per_second,
8425 									 decoded, &len, &rate, &codec_flags) == SWITCH_STATUS_SUCCESS) {
8426 
8427 			uint32_t energy = 0;
8428 			uint32_t x, y = 0, z = len / sizeof(int16_t);
8429 			uint32_t score = 0;
8430 			int divisor = 0;
8431 			if (z) {
8432 
8433 				if (!(divisor = rtp_session->vad_data.read_codec->implementation->actual_samples_per_second / 8000)) {
8434 					divisor = 1;
8435 				}
8436 
8437 				for (x = 0; x < z; x++) {
8438 					energy += abs(decoded[y]);
8439 					y += rtp_session->vad_data.read_codec->implementation->number_of_channels;
8440 				}
8441 
8442 				if (++rtp_session->vad_data.start_count < rtp_session->vad_data.start) {
8443 					send = 1;
8444 				} else {
8445 					score = (energy / (z / divisor));
8446 					if (score && (rtp_session->vad_data.bg_count < rtp_session->vad_data.bg_len)) {
8447 						rtp_session->vad_data.bg_level += score;
8448 						if (++rtp_session->vad_data.bg_count == rtp_session->vad_data.bg_len) {
8449 							rtp_session->vad_data.bg_level /= rtp_session->vad_data.bg_len;
8450 						}
8451 						send = 1;
8452 					} else {
8453 						if (score > rtp_session->vad_data.bg_level && !switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
8454 							uint32_t diff = score - rtp_session->vad_data.bg_level;
8455 
8456 							if (rtp_session->vad_data.hangover_hits) {
8457 								rtp_session->vad_data.hangover_hits--;
8458 							}
8459 
8460 							if (diff >= rtp_session->vad_data.diff_level || ++rtp_session->vad_data.hangunder_hits >= rtp_session->vad_data.hangunder) {
8461 
8462 								switch_set_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING);
8463 
8464 								rtp_session->vad_data.start_talking = switch_micro_time_now();
8465 
8466 								if (!(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
8467 									send_msg->header.m = 1;
8468 								}
8469 								rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
8470 								if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_EVENTS_TALK)) {
8471 
8472 									if ((rtp_session->vad_data.fire_events & VAD_FIRE_TALK)) {
8473 										switch_event_t *event;
8474 										if (switch_event_create(&event, SWITCH_EVENT_TALK) == SWITCH_STATUS_SUCCESS) {
8475 											switch_channel_event_set_data(switch_core_session_get_channel(rtp_session->vad_data.session), event);
8476 											switch_event_fire(&event);
8477 										}
8478 									}
8479 								}
8480 							}
8481 						} else {
8482 							if (rtp_session->vad_data.hangunder_hits) {
8483 								rtp_session->vad_data.hangunder_hits--;
8484 							}
8485 							if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
8486 								if (++rtp_session->vad_data.hangover_hits >= rtp_session->vad_data.hangover) {
8487 									rtp_session->vad_data.stop_talking = switch_micro_time_now();
8488 									rtp_session->vad_data.total_talk_time += (rtp_session->vad_data.stop_talking - rtp_session->vad_data.start_talking);
8489 
8490 									switch_clear_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING);
8491 
8492 									rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
8493 									if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_EVENTS_NOTALK)) {
8494 
8495 										if ((rtp_session->vad_data.fire_events & VAD_FIRE_NOT_TALK)) {
8496 											switch_event_t *event;
8497 											if (switch_event_create(&event, SWITCH_EVENT_NOTALK) == SWITCH_STATUS_SUCCESS) {
8498 												switch_channel_event_set_data(switch_core_session_get_channel(rtp_session->vad_data.session), event);
8499 												switch_event_fire(&event);
8500 											}
8501 										}
8502 									}
8503 								}
8504 							}
8505 						}
8506 					}
8507 				}
8508 
8509 				if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
8510 					send = 1;
8511 				}
8512 			}
8513 		} else {
8514 			ret = -1;
8515 			goto end;
8516 		}
8517 	}
8518 
8519 	if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
8520 		uint32_t ts_delta;
8521 
8522 		this_ts = ntohl(send_msg->header.ts);
8523 
8524 		ts_delta = abs((int32_t)(this_ts - rtp_session->last_write_ts));
8525 
8526 		if (ts_delta > rtp_session->samples_per_second * 2) {
8527 			rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
8528 		}
8529 #ifdef DEBUG_TS_ROLLOVER
8530 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "WRITE TS LAST:%u THIS:%u DELTA:%u\n", rtp_session->last_write_ts, this_ts, ts_delta);
8531 #endif
8532 		if ((!(flags && *flags & SFF_RFC2833) && ts_delta == 0) || !switch_rtp_ready(rtp_session) || rtp_session->sending_dtmf) {
8533 			send = 0;
8534 		}
8535 	}
8536 
8537 	if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) {
8538 		send = 0;
8539 	}
8540 
8541 	if (send) {
8542 		int delta = 1;
8543 
8544 		if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (*flags & SFF_EXTERNAL) &&
8545 			rtp_session->stats.outbound.packet_count && rtp_session->flags[SWITCH_RTP_FLAG_PASSTHRU]) {
8546 			int32_t x = rtp_session->last_write_seq;
8547 			int32_t y = ntohs(send_msg->header.seq);
8548 
8549 			if (!rtp_session->video_delta_mode) {
8550 				rtp_session->video_delta_mode = 1;
8551 			} else {
8552 				if (x > UINT16_MAX / 2 && y < UINT16_MAX / 2) {
8553 					x -= (int32_t)UINT16_MAX+1;
8554 				}
8555 
8556 				delta = y-x;
8557 			}
8558 
8559 			rtp_session->last_write_seq = y;
8560 		}
8561 
8562 		if (!rtp_session->flags[SWITCH_RTP_FLAG_PASSTHRU]) {
8563 			rtp_session->video_delta_mode = 0;
8564 		}
8565 
8566 		rtp_session->seq += delta;
8567 
8568 		send_msg->header.seq = htons(rtp_session->seq);
8569 
8570 		if (rtp_session->flags[SWITCH_RTP_FLAG_BYTESWAP] && send_msg->header.pt == rtp_session->payload) {
8571 			switch_swap_linear((int16_t *)send_msg->body, (int) datalen);
8572 		}
8573 
8574 #ifdef ENABLE_SRTP
8575 		switch_mutex_lock(rtp_session->ice_mutex);
8576 		if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
8577 			int sbytes = (int) bytes;
8578 			srtp_err_status_t stat;
8579 
8580 
8581 			if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET] || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) {
8582 
8583 				switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND_RESET);
8584 				srtp_dealloc(rtp_session->send_ctx[rtp_session->srtp_idx_rtp]);
8585 				rtp_session->send_ctx[rtp_session->srtp_idx_rtp] = NULL;
8586 				if ((stat = srtp_create(&rtp_session->send_ctx[rtp_session->srtp_idx_rtp],
8587 										&rtp_session->send_policy[rtp_session->srtp_idx_rtp])) || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) {
8588 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR,
8589 									  "Error! RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session));
8590 					rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0;
8591 					ret = -1;
8592 					switch_mutex_unlock(rtp_session->ice_mutex);
8593 					goto end;
8594 				} else {
8595 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO,
8596 									  "RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session));
8597 				}
8598 			}
8599 
8600 			if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) {
8601 				stat = srtp_protect(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &send_msg->header, &sbytes);
8602 			} else {
8603 				stat = srtp_protect_mki(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &send_msg->header, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX);
8604 			}
8605 
8606 			if (stat) {
8607 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR,
8608 								  "Error: %s SRTP protection failed with code %d\n", rtp_type(rtp_session), stat);
8609 			}
8610 
8611 			bytes = sbytes;
8612 		}
8613 		switch_mutex_unlock(rtp_session->ice_mutex);
8614 #endif
8615 #ifdef ENABLE_ZRTP
8616 		/* ZRTP Send */
8617 		if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
8618 			unsigned int sbytes = (int) bytes;
8619 			zrtp_status_t stat = zrtp_status_fail;
8620 
8621 
8622 			stat = zrtp_process_rtp(rtp_session->zrtp_stream, (void *) send_msg, &sbytes);
8623 
8624 			switch (stat) {
8625 			case zrtp_status_ok:
8626 				break;
8627 			case zrtp_status_drop:
8628 				/* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */
8629 				ret = (int) bytes;
8630 				goto end;
8631 				break;
8632 			case zrtp_status_fail:
8633 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
8634 				break;
8635 			default:
8636 				break;
8637 			}
8638 
8639 			bytes = sbytes;
8640 		}
8641 #endif
8642 
8643 		now = switch_micro_time_now();
8644 #ifdef RTP_DEBUG_WRITE_DELTA
8645 		{
8646 			int delta = (int) (now - rtp_session->send_time) / 1000;
8647 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "WRITE %d delta %d\n", (int) bytes, delta);
8648 		}
8649 #endif
8650 		rtp_session->send_time = now;
8651 
8652 		if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
8653 			const char *tx_host;
8654 			const char *old_host;
8655 			const char *my_host;
8656 
8657 			char bufa[50], bufb[50], bufc[50];
8658 
8659 
8660 			tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
8661 			old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
8662 			my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
8663 
8664 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session), SWITCH_LOG_CONSOLE,
8665 							  "W %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
8666 							  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
8667 							  (long) bytes,
8668 							  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
8669 							  old_host, rtp_session->remote_port,
8670 							  tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr),
8671 							  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
8672 
8673 		}
8674 
8675 		if (rtp_session->flags[SWITCH_RTP_FLAG_NACK]) {
8676 			switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
8677 
8678 			if (!rtp_session->vbw) {
8679 				int nack_size = 100;
8680 				const char *var;
8681 
8682 				if ((var = switch_channel_get_variable(channel, "rtp_nack_buffer_size"))) {
8683 					int tmp = atoi(var);
8684 
8685 					if (tmp > 0 && tmp < 500) {
8686 						nack_size = tmp;
8687 					}
8688 				}
8689 
8690 				switch_jb_create(&rtp_session->vbw, SJB_VIDEO, nack_size, nack_size, rtp_session->pool);
8691 
8692 				if (rtp_session->vbw) {
8693 					switch_jb_set_flag(rtp_session->vbw, SJB_QUEUE_ONLY);
8694 					//switch_jb_debug_level(rtp_session->vbw, 10);
8695 				}
8696 			}
8697 			switch_jb_put_packet(rtp_session->vbw, (switch_rtp_packet_t *)send_msg, bytes);
8698 		}
8699 
8700 #ifdef RTP_WRITE_PLOSS
8701 		{
8702 			int r = (rand() % 10000) + 1;
8703 
8704 			if (r <= 200) {
8705 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ALERT,
8706 								  "Simulate dropping packet ......... ts: %u seq: %u\n", ntohl(send_msg->header.ts), ntohs(send_msg->header.seq));
8707 			} else {
8708 				if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
8709 					rtp_session->seq--;
8710 					ret = -1;
8711 					goto end;
8712 				}
8713 			}
8714 		}
8715 #else
8716 		//if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
8717 		//
8718 		//	rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]++;
8719 		//
8720 		//	//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "SEND %u\n", ntohs(send_msg->header.seq));
8721 		//}
8722 		if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
8723 			rtp_session->seq -= delta;
8724 
8725 			ret = -1;
8726 			goto end;
8727 		}
8728 #endif
8729 		rtp_session->last_write_ts = this_ts;
8730 		rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
8731 
8732 		if (rtp_session->queue_delay) {
8733 			rtp_session->delay_samples = rtp_session->queue_delay;
8734 			rtp_session->queue_delay = 0;
8735 		}
8736 
8737 		rtp_session->stats.outbound.raw_bytes += bytes;
8738 		rtp_session->stats.outbound.packet_count++;
8739 
8740 		if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
8741 			rtp_session->stats.rtcp.sent_pkt_count++;
8742 		}
8743 
8744 		if (send_msg->header.pt == rtp_session->cng_pt) {
8745 			rtp_session->stats.outbound.cng_packet_count++;
8746 		} else {
8747 			rtp_session->stats.outbound.media_packet_count++;
8748 			rtp_session->stats.outbound.media_bytes += bytes;
8749 		}
8750 
8751 		if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
8752 			//switch_core_timer_sync(&rtp_session->write_timer);
8753 			rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount;
8754 		}
8755 
8756 		rtp_session->last_write_timestamp = switch_micro_time_now();
8757 	}
8758 
8759 	ret = (int) bytes;
8760 
8761  end:
8762 
8763 	WRITE_DEC(rtp_session);
8764 
8765 	return ret;
8766 }
8767 
switch_rtp_disable_vad(switch_rtp_t * rtp_session)8768 SWITCH_DECLARE(switch_status_t) switch_rtp_disable_vad(switch_rtp_t *rtp_session)
8769 {
8770 
8771 	if (!rtp_session) {
8772 		return SWITCH_STATUS_FALSE;
8773 	}
8774 
8775 	if (!rtp_session->flags[SWITCH_RTP_FLAG_VAD]) {
8776 		return SWITCH_STATUS_GENERR;
8777 	}
8778 	switch_core_codec_destroy(&rtp_session->vad_data.vad_codec);
8779 	switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_VAD);
8780 	return SWITCH_STATUS_SUCCESS;
8781 }
8782 
switch_rtp_enable_vad(switch_rtp_t * rtp_session,switch_core_session_t * session,switch_codec_t * codec,switch_vad_flag_t flags)8783 SWITCH_DECLARE(switch_status_t) switch_rtp_enable_vad(switch_rtp_t *rtp_session, switch_core_session_t *session, switch_codec_t *codec,
8784 													  switch_vad_flag_t flags)
8785 {
8786 	if (!switch_rtp_ready(rtp_session)) {
8787 		return SWITCH_STATUS_FALSE;
8788 	}
8789 
8790 	if (rtp_session->flags[SWITCH_RTP_FLAG_VAD]) {
8791 		return SWITCH_STATUS_GENERR;
8792 	}
8793 
8794 	memset(&rtp_session->vad_data, 0, sizeof(rtp_session->vad_data));
8795 
8796 	if (switch_true(switch_channel_get_variable(switch_core_session_get_channel(rtp_session->session), "fire_talk_events"))) {
8797 		rtp_session->vad_data.fire_events |= VAD_FIRE_TALK;
8798 	}
8799 
8800 	if (switch_true(switch_channel_get_variable(switch_core_session_get_channel(rtp_session->session), "fire_not_talk_events"))) {
8801 		rtp_session->vad_data.fire_events |= VAD_FIRE_NOT_TALK;
8802 	}
8803 
8804 
8805 	if (switch_core_codec_init(&rtp_session->vad_data.vad_codec,
8806 							   codec->implementation->iananame,
8807 							   codec->implementation->modname,
8808 							   NULL,
8809 							   codec->implementation->samples_per_second,
8810 							   codec->implementation->microseconds_per_packet / 1000,
8811 							   codec->implementation->number_of_channels,
8812 							   SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
8813 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Can't load codec?\n");
8814 		return SWITCH_STATUS_FALSE;
8815 	}
8816 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Activate VAD codec %s %dms\n", codec->implementation->iananame,
8817 					  codec->implementation->microseconds_per_packet / 1000);
8818 	rtp_session->vad_data.diff_level = 400;
8819 	rtp_session->vad_data.hangunder = 15;
8820 	rtp_session->vad_data.hangover = 40;
8821 	rtp_session->vad_data.bg_len = 5;
8822 	rtp_session->vad_data.bg_count = 5;
8823 	rtp_session->vad_data.bg_level = 300;
8824 	rtp_session->vad_data.read_codec = codec;
8825 	rtp_session->vad_data.session = session;
8826 	rtp_session->vad_data.flags = flags;
8827 	rtp_session->vad_data.cng_freq = 50;
8828 	rtp_session->vad_data.ts = 1;
8829 	rtp_session->vad_data.start = 0;
8830 	rtp_session->vad_data.next_scan = switch_epoch_time_now(NULL);
8831 	rtp_session->vad_data.scan_freq = 0;
8832 	if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
8833 		rtp_session->vad_data.start_talking = switch_micro_time_now();
8834 	}
8835 	switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_VAD);
8836 	switch_set_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_CNG);
8837 	return SWITCH_STATUS_SUCCESS;
8838 }
8839 
switch_rtp_write_frame(switch_rtp_t * rtp_session,switch_frame_t * frame)8840 SWITCH_DECLARE(int) switch_rtp_write_frame(switch_rtp_t *rtp_session, switch_frame_t *frame)
8841 {
8842 	uint8_t fwd = 0;
8843 	void *data = NULL;
8844 	uint32_t len, ts = 0;
8845 	switch_payload_t payload = 0;
8846 	rtp_msg_t *send_msg = NULL;
8847 	srtp_hdr_t local_header;
8848 	int r = 0;
8849 
8850 	if (!switch_rtp_ready(rtp_session) || !rtp_session->remote_addr) {
8851 		return -1;
8852 	}
8853 
8854 	if (!rtp_write_ready(rtp_session, frame->datalen, __LINE__)) {
8855 		return 0;
8856 	}
8857 
8858 	//if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
8859 	//	rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]++;
8860 	//	rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]++;
8861 	//}
8862 
8863 
8864 	if (switch_test_flag(frame, SFF_PROXY_PACKET) || switch_test_flag(frame, SFF_UDPTL_PACKET) ||
8865 		rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
8866 
8867 		//if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
8868 		switch_size_t bytes;
8869 		//char bufa[50];
8870 
8871 		/* Fast PASS! */
8872 		if (!switch_test_flag(frame, SFF_PROXY_PACKET) && !switch_test_flag(frame, SFF_UDPTL_PACKET)) {
8873 			return 0;
8874 		}
8875 		bytes = frame->packetlen;
8876 		//tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->remote_addr);
8877 
8878 		send_msg = frame->packet;
8879 
8880 		if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !switch_test_flag(frame, SFF_UDPTL_PACKET)) {
8881 
8882 			if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->payload > 0) {
8883 				send_msg->header.pt = rtp_session->payload;
8884 			}
8885 
8886 			send_msg->header.ssrc = htonl(rtp_session->ssrc);
8887 			send_msg->header.seq = htons(++rtp_session->seq);
8888 		}
8889 
8890 		if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
8891 			const char *tx_host;
8892 			const char *old_host;
8893 			const char *my_host;
8894 
8895 			char bufa[50], bufb[50], bufc[50];
8896 
8897 
8898 			tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
8899 			old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
8900 			my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
8901 
8902 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session), SWITCH_LOG_CONSOLE,
8903 							  "W %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
8904 							  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
8905 							  (long) bytes,
8906 							  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
8907 							  old_host, rtp_session->remote_port,
8908 							  tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr),
8909 							  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
8910 
8911 		}
8912 
8913 		if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, frame->packet, &bytes) != SWITCH_STATUS_SUCCESS) {
8914 			return -1;
8915 		}
8916 
8917 
8918 		rtp_session->stats.outbound.raw_bytes += bytes;
8919 		rtp_session->stats.outbound.media_bytes += bytes;
8920 		rtp_session->stats.outbound.media_packet_count++;
8921 		rtp_session->stats.outbound.packet_count++;
8922 		return (int) bytes;
8923 	}
8924 #ifdef ENABLE_ZRTP
8925 	if (zrtp_on && rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND]) {
8926 		zrtp_session_info_t zrtp_session_info;
8927 
8928 		if (zrtp_status_ok == zrtp_session_get(rtp_session->zrtp_session, &zrtp_session_info)) {
8929 			if (zrtp_session_info.sas_is_ready) {
8930 
8931 				switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
8932 
8933 				const char *uuid = switch_channel_get_partner_uuid(channel);
8934 				if (uuid) {
8935 					switch_core_session_t *other_session;
8936 
8937 					if ((other_session = switch_core_session_locate(uuid))) {
8938 						switch_channel_t *other_channel = switch_core_session_get_channel(other_session);
8939 						switch_rtp_t *other_rtp_session = switch_channel_get_private(other_channel, "__zrtp_audio_rtp_session");
8940 
8941 
8942 						if (other_rtp_session) {
8943 							if (zrtp_status_ok == zrtp_session_get(other_rtp_session->zrtp_session, &zrtp_session_info)) {
8944 								if (rtp_session->zrtp_mitm_tries > ZRTP_MITM_TRIES) {
8945 									rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
8946 									rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
8947 									switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV);
8948 									switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND);
8949 								} else if (zrtp_status_ok == zrtp_resolve_mitm_call(other_rtp_session->zrtp_stream, rtp_session->zrtp_stream)) {
8950 									rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
8951 									rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
8952 									switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV);
8953 									switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND);
8954 									rtp_session->zrtp_mitm_tries++;
8955 								}
8956 								rtp_session->zrtp_mitm_tries++;
8957 							}
8958 						}
8959 
8960 						switch_core_session_rwunlock(other_session);
8961 					}
8962 				}
8963 			}
8964 		}
8965 	}
8966 #endif
8967 
8968 	fwd = (rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] &&
8969 		   (switch_test_flag(frame, SFF_RAW_RTP) || switch_test_flag(frame, SFF_RAW_RTP_PARSE_FRAME))) ? 1 : 0;
8970 
8971 	if (!fwd && !rtp_session->sending_dtmf && !rtp_session->queue_delay && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
8972 		rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] && (rtp_session->rtp_bugs & RTP_BUG_GEN_ONE_GEN_ALL)) {
8973 
8974 		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Generating RTP locally but timestamp passthru is configured, disabling....\n");
8975 		rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] = 0;
8976 		rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
8977 	}
8978 
8979 	switch_assert(frame != NULL);
8980 
8981 	if (switch_test_flag(frame, SFF_CNG)) {
8982 		if (rtp_session->cng_pt != INVALID_PT) {
8983 			payload = rtp_session->cng_pt;
8984 		} else {
8985 			return (int) frame->packetlen;
8986 		}
8987 	} else {
8988 		payload = rtp_session->payload;
8989 #if 0
8990 		if (rtp_session->pmaps && *rtp_session->pmaps) {
8991 			payload_map_t *pmap;
8992 			for (pmap = *rtp_session->pmaps; pmap; pmap = pmap->next) {
8993 				if (pmap->current) {
8994 					payload = pmap->pt;
8995 				}
8996 			}
8997 		}
8998 #endif
8999 	}
9000 
9001 
9002 	if (switch_test_flag(frame, SFF_RTP_HEADER) || rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) {
9003 		switch_size_t wrote;
9004 
9005 		wrote = switch_rtp_write_manual(rtp_session, frame->data, frame->datalen,
9006 										frame->m, frame->payload, (uint32_t) (frame->timestamp), &frame->flags);
9007 
9008 		rtp_session->stats.outbound.raw_bytes += wrote;
9009 		rtp_session->stats.outbound.media_bytes += wrote;
9010 		rtp_session->stats.outbound.media_packet_count++;
9011 		rtp_session->stats.outbound.packet_count++;
9012 
9013 		return wrote;
9014 	}
9015 
9016 	if (frame->pmap && rtp_session->pmaps && *rtp_session->pmaps) {
9017 		payload_map_t *pmap;
9018 
9019 		switch_mutex_lock(rtp_session->flag_mutex);
9020 		for (pmap = *rtp_session->pmaps; pmap; pmap = pmap->next) {
9021 			if (pmap->negotiated && pmap->hash == frame->pmap->hash) {
9022 				payload = pmap->recv_pt;
9023 				break;
9024 			}
9025 		}
9026 		switch_mutex_unlock(rtp_session->flag_mutex);
9027 	}
9028 
9029 	if (fwd) {
9030 		send_msg = frame->packet;
9031 		local_header = send_msg->header;
9032 		len = frame->packetlen;
9033 		ts = 0;
9034 
9035 		send_msg->header.pt = payload;
9036 
9037 		if (switch_test_flag(frame, SFF_RAW_RTP_PARSE_FRAME)) {
9038 			send_msg->header.version = 2;
9039 			send_msg->header.m = frame->m;
9040 
9041 			send_msg->header.ts = htonl(frame->timestamp);
9042 			if (frame->ssrc) {
9043 				send_msg->header.ssrc = htonl(frame->ssrc);
9044 			} else {
9045 				send_msg->header.ssrc = htonl(rtp_session->ssrc);
9046 			}
9047 		}
9048 
9049 	} else {
9050 		data = frame->data;
9051 		len = frame->datalen;
9052 		ts = rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] ? (uint32_t) frame->timestamp : 0;
9053 	}
9054 
9055 	/*
9056 	  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
9057 	  send_msg->header.pt = rtp_session->payload;
9058 	  }
9059 	*/
9060 
9061 	r = rtp_common_write(rtp_session, send_msg, data, len, payload, ts, &frame->flags);
9062 
9063 	if (send_msg) {
9064 		send_msg->header = local_header;
9065 	}
9066 
9067 	return r;
9068 
9069 }
9070 
switch_rtp_get_stats(switch_rtp_t * rtp_session,switch_memory_pool_t * pool)9071 SWITCH_DECLARE(switch_rtp_stats_t *) switch_rtp_get_stats(switch_rtp_t *rtp_session, switch_memory_pool_t *pool)
9072 {
9073 	switch_rtp_stats_t *s;
9074 
9075 	if (!rtp_session) {
9076 		return NULL;
9077 	}
9078 
9079 	switch_mutex_lock(rtp_session->flag_mutex);
9080 	if (pool) {
9081 		s = switch_core_alloc(pool, sizeof(*s));
9082 		*s = rtp_session->stats;
9083 	} else {
9084 		s = &rtp_session->stats;
9085 	}
9086 
9087 	if (rtp_session->jb) {
9088 		switch_jb_get_frames(rtp_session->jb, NULL, NULL, NULL, (uint32_t *)&s->inbound.largest_jb_size);
9089 	}
9090 
9091 	do_mos(rtp_session);
9092 
9093 	switch_mutex_unlock(rtp_session->flag_mutex);
9094 
9095 	return s;
9096 }
9097 
switch_rtp_write_manual(switch_rtp_t * rtp_session,void * data,uint32_t datalen,uint8_t m,switch_payload_t payload,uint32_t ts,switch_frame_flag_t * flags)9098 SWITCH_DECLARE(int) switch_rtp_write_manual(switch_rtp_t *rtp_session,
9099 											void *data, uint32_t datalen, uint8_t m, switch_payload_t payload, uint32_t ts, switch_frame_flag_t *flags)
9100 {
9101 	switch_size_t bytes;
9102 	int ret = -1;
9103 
9104 	if (!switch_rtp_ready(rtp_session) || !rtp_session->remote_addr || datalen > SWITCH_RTP_MAX_BUF_LEN) {
9105 		return -1;
9106 	}
9107 
9108 	if (!rtp_write_ready(rtp_session, datalen, __LINE__)) {
9109 		return 0;
9110 	}
9111 
9112 	if (payload == INVALID_PT) {
9113 		return 0;
9114 	}
9115 
9116 	WRITE_INC(rtp_session);
9117 
9118 	rtp_session->write_msg = rtp_session->send_msg;
9119 	rtp_session->write_msg.header.seq = htons(++rtp_session->seq);
9120 	rtp_session->write_msg.header.ts = htonl(ts);
9121 	rtp_session->write_msg.header.pt = payload;
9122 	rtp_session->write_msg.header.m = m;
9123 	memcpy(rtp_session->write_msg.body, data, datalen);
9124 
9125 	bytes = rtp_header_len + datalen;
9126 
9127 	if (switch_rtp_write_raw(rtp_session, (void *) &rtp_session->write_msg, &bytes, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) {
9128 		rtp_session->seq--;
9129 		ret = -1;
9130 		goto end;
9131 	}
9132 
9133 	if (((*flags) & SFF_RTP_HEADER)) {
9134 		rtp_session->last_write_ts = ts;
9135 		rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
9136 	}
9137 
9138 	ret = (int) bytes;
9139 
9140  end:
9141 
9142 	WRITE_DEC(rtp_session);
9143 
9144 	return ret;
9145 }
9146 
9147 
9148 
switch_rtp_write_raw(switch_rtp_t * rtp_session,void * data,switch_size_t * bytes,switch_bool_t process_encryption)9149 SWITCH_DECLARE(switch_status_t) switch_rtp_write_raw(switch_rtp_t *rtp_session, void *data, switch_size_t *bytes, switch_bool_t process_encryption)
9150 {
9151 	switch_status_t status = SWITCH_STATUS_FALSE;
9152 
9153 	switch_assert(bytes);
9154 
9155 	if (!switch_rtp_ready(rtp_session) || !rtp_session->remote_addr || *bytes > SWITCH_RTP_MAX_BUF_LEN) {
9156 		return status;
9157 	}
9158 
9159 	if (!rtp_write_ready(rtp_session, *bytes, __LINE__)) {
9160 		return SWITCH_STATUS_NOT_INITALIZED;
9161 	}
9162 
9163 	WRITE_INC(rtp_session);
9164 
9165 	if (process_encryption) {
9166 #ifdef ENABLE_SRTP
9167 		if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
9168 
9169 			int sbytes = (int) *bytes;
9170 			srtp_err_status_t stat;
9171 
9172 			if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET]) {
9173 				switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND_RESET);
9174 				srtp_dealloc(rtp_session->send_ctx[rtp_session->srtp_idx_rtp]);
9175 				rtp_session->send_ctx[rtp_session->srtp_idx_rtp] = NULL;
9176 				if ((stat = srtp_create(&rtp_session->send_ctx[rtp_session->srtp_idx_rtp],
9177 										&rtp_session->send_policy[rtp_session->srtp_idx_rtp])) || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) {
9178 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP SEND\n");
9179 					rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0;
9180 					status = SWITCH_STATUS_FALSE;
9181 					goto end;
9182 				} else {
9183 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "RE-Activating Secure RTP SEND\n");
9184 				}
9185 			}
9186 
9187 			if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) {
9188 				stat = srtp_protect(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &rtp_session->write_msg.header, &sbytes);
9189 			} else {
9190 				stat = srtp_protect_mki(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &rtp_session->write_msg.header, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX);
9191 			}
9192 
9193 			if (stat) {
9194 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP protection failed with code %d\n", stat);
9195 			}
9196 			*bytes = sbytes;
9197 		}
9198 #endif
9199 #ifdef ENABLE_ZRTP
9200 		/* ZRTP Send */
9201 		if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
9202 			unsigned int sbytes = (int) *bytes;
9203 			zrtp_status_t stat = zrtp_status_fail;
9204 
9205 			stat = zrtp_process_rtp(rtp_session->zrtp_stream, (void *) &rtp_session->write_msg, &sbytes);
9206 
9207 			switch (stat) {
9208 			case zrtp_status_ok:
9209 				break;
9210 			case zrtp_status_drop:
9211 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
9212 				status = SWITCH_STATUS_SUCCESS;
9213 				goto end;
9214 				break;
9215 			case zrtp_status_fail:
9216 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
9217 				break;
9218 			default:
9219 				break;
9220 			}
9221 
9222 			*bytes = sbytes;
9223 		}
9224 #endif
9225 	}
9226 
9227 	status = switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, data, bytes);
9228 #if defined(ENABLE_SRTP) || defined(ENABLE_ZRTP)
9229  end:
9230 #endif
9231 
9232 	WRITE_DEC(rtp_session);
9233 
9234 	return status;
9235 }
9236 
switch_rtp_get_ssrc(switch_rtp_t * rtp_session)9237 SWITCH_DECLARE(uint32_t) switch_rtp_get_ssrc(switch_rtp_t *rtp_session)
9238 {
9239 	return rtp_session->ssrc;
9240 }
9241 
switch_rtp_set_private(switch_rtp_t * rtp_session,void * private_data)9242 SWITCH_DECLARE(void) switch_rtp_set_private(switch_rtp_t *rtp_session, void *private_data)
9243 {
9244 	rtp_session->private_data = private_data;
9245 }
9246 
switch_rtp_get_private(switch_rtp_t * rtp_session)9247 SWITCH_DECLARE(void *) switch_rtp_get_private(switch_rtp_t *rtp_session)
9248 {
9249 	return rtp_session->private_data;
9250 }
9251 
switch_rtp_get_core_session(switch_rtp_t * rtp_session)9252 SWITCH_DECLARE(switch_core_session_t*) switch_rtp_get_core_session(switch_rtp_t *rtp_session)
9253 {
9254 	return rtp_session->session;
9255 }
9256 
9257 /* For Emacs:
9258  * Local Variables:
9259  * mode:c
9260  * indent-tabs-mode:t
9261  * tab-width:4
9262  * c-basic-offset:4
9263  * End:
9264  * For VIM:
9265  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
9266  */
9267