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