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