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