Bug Summary

File:src/switch_rtp.c
Location:line 559, column 4
Description:Value stored to 'len' is never read

Annotated Source Code

1/*
2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3 * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4 *
5 * Version: MPL 1.1
6 *
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18 *
19 * The Initial Developer of the Original Code is
20 * Anthony Minessale II <anthm@freeswitch.org>
21 * Portions created by the Initial Developer are Copyright (C)
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Anthony Minessale II <anthm@freeswitch.org>
27 * Marcel Barbulescu <marcelbarbulescu@gmail.com>
28 * Seven Du <dujinfang@gmail.com>
29 *
30 * switch_rtp.c -- RTP
31 *
32 */
33//#define DEBUG_2833
34//#define RTP_DEBUG_WRITE_DELTA
35//#define DEBUG_MISSED_SEQ
36//#define DEBUG_EXTRA
37//#define DEBUG_RTCP
38
39#include <switch.h>
40#ifndef _MSC_VER
41#include <switch_private.h>
42#endif
43#include <switch_stun.h>
44#include <apr_network_io.h>
45#undef PACKAGE_NAME"srtp"
46#undef PACKAGE_STRING"srtp 1.4.2"
47#undef PACKAGE_TARNAME"srtp"
48#undef PACKAGE_VERSION"1.4.2"
49#undef PACKAGE_BUGREPORT"mcgrew@cisco.com"
50#undef VERSION"1.4.2"
51#undef PACKAGE"srtp"
52#undef inlineinline
53#include <datatypes.h>
54#include <srtp.h>
55#include <srtp_priv.h>
56#include <switch_ssl.h>
57
58#define FIR_COUNTDOWN50 50
59#define JITTER_LEAD_FRAMES10 10
60#define READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session->
reading++
switch_mutex_lock(rtp_session->read_mutex); rtp_session->reading++
61#define READ_DEC(rtp_session)switch_mutex_unlock(rtp_session->read_mutex); rtp_session->
reading--
switch_mutex_unlock(rtp_session->read_mutex); rtp_session->reading--
62#define WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session->
writing++
switch_mutex_lock(rtp_session->write_mutex); rtp_session->writing++
63#define WRITE_DEC(rtp_session)switch_mutex_unlock(rtp_session->write_mutex); rtp_session
->writing--
switch_mutex_unlock(rtp_session->write_mutex); rtp_session->writing--
64
65#define RTP_STUN_FREQ1000000 1000000
66#define rtp_header_len12 12
67#define RTP_START_PORT16384 16384
68#define RTP_END_PORT32768 32768
69#define MASTER_KEY_LEN30 30
70#define RTP_MAGIC_NUMBER42 42
71#define WARN_SRTP_ERRS10 10
72#define MAX_SRTP_ERRS100 100
73#define NTP_TIME_OFFSET2208988800UL 2208988800UL
74
75#define DTMF_SANITY(rtp_session->one_second * 30) (rtp_session->one_second * 30)
76
77#define rtp_session_name(_rtp_session)_rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(_rtp_session->session)) : "-"
_rtp_session->session ? switch_core_session_get_name(_rtp_session->session)switch_channel_get_name(switch_core_session_get_channel(_rtp_session
->session))
: "-"
78
79static switch_port_t START_PORT = RTP_START_PORT16384;
80static switch_port_t END_PORT = RTP_END_PORT32768;
81static switch_mutex_t *port_lock = NULL((void*)0);
82static void do_flush(switch_rtp_t *rtp_session, int force);
83
84typedef srtp_hdr_t rtp_hdr_t;
85
86#ifdef ENABLE_ZRTP
87#include "zrtp.h"
88static zrtp_global_t *zrtp_global;
89#ifndef WIN32
90static zrtp_zid_t zid = { "FreeSWITCH01" };
91#else
92static zrtp_zid_t zid = { "FreeSWITCH0" };
93#endif
94static int zrtp_on = 0;
95#define ZRTP_MITM_TRIES 100
96#endif
97
98#ifdef _MSC_VER
99#pragma pack(4)
100#endif
101
102#ifdef _MSC_VER
103#pragma pack()
104#define ENABLE_SRTP1
105#endif
106
107static switch_hash_t *alloc_hash = NULL((void*)0);
108
109typedef struct {
110 srtp_hdr_t header;
111 char body[SWITCH_RTP_MAX_BUF_LEN16384];
112 switch_rtp_hdr_ext_t *ext;
113 char *ebody;
114} rtp_msg_t;
115
116#define RTP_BODY(_s)(char *) (_s->recv_msg.ebody ? _s->recv_msg.ebody : _s->
recv_msg.body)
(char *) (_s->recv_msg.ebody ? _s->recv_msg.ebody : _s->recv_msg.body)
117
118typedef struct {
119 uint32_t ssrc;
120 uint8_t seq;
121 uint8_t r1;
122 uint8_t r2;
123 uint8_t r3;
124} rtcp_fir_t;
125
126#ifdef _MSC_VER
127#pragma pack(push, r1, 1)
128#endif
129
130#if SWITCH_BYTE_ORDER1234 == __BIG_ENDIAN4321
131typedef struct {
132 unsigned version:2;
133 unsigned p:1;
134 unsigned fmt:5;
135 unsigned pt:8;
136 unsigned length:16;
137 uint32_t send_ssrc;
138 uint32_t recv_ssrc;
139} switch_rtcp_ext_hdr_t;
140
141#else /* BIG_ENDIAN */
142
143typedef struct {
144 unsigned fmt:5;
145 unsigned p:1;
146 unsigned version:2;
147 unsigned pt:8;
148 unsigned length:16;
149 uint32_t send_ssrc;
150 uint32_t recv_ssrc;
151} switch_rtcp_ext_hdr_t;
152
153#endif
154
155#ifdef _MSC_VER
156#pragma pack(pop, r1)
157#endif
158
159
160typedef struct {
161 switch_rtcp_ext_hdr_t header;
162 char body[SWITCH_RTCP_MAX_BUF_LEN16384];
163} rtcp_ext_msg_t;
164
165typedef struct {
166 switch_rtcp_hdr_t header;
167 char body[SWITCH_RTCP_MAX_BUF_LEN16384];
168} rtcp_msg_t;
169
170
171typedef enum {
172 VAD_FIRE_TALK = (1 << 0),
173 VAD_FIRE_NOT_TALK = (1 << 1)
174} vad_talk_mask_t;
175
176struct switch_rtp_vad_data {
177 switch_core_session_t *session;
178 switch_codec_t vad_codec;
179 switch_codec_t *read_codec;
180 uint32_t bg_level;
181 uint32_t bg_count;
182 uint32_t bg_len;
183 uint32_t diff_level;
184 uint8_t hangunder;
185 uint8_t hangunder_hits;
186 uint8_t hangover;
187 uint8_t hangover_hits;
188 uint8_t cng_freq;
189 uint8_t cng_count;
190 switch_vad_flag_t flags;
191 uint32_t ts;
192 uint8_t start;
193 uint8_t start_count;
194 uint8_t scan_freq;
195 time_t next_scan;
196 int fire_events;
197};
198
199struct switch_rtp_rfc2833_data {
200 switch_queue_t *dtmf_queue;
201 char out_digit;
202 unsigned char out_digit_packet[4];
203 unsigned int out_digit_sofar;
204 unsigned int out_digit_sub_sofar;
205 unsigned int out_digit_dur;
206 uint16_t in_digit_seq;
207 uint32_t in_digit_ts;
208 uint32_t last_in_digit_ts;
209 uint32_t in_digit_sanity;
210 uint32_t in_interleaved;
211 uint32_t timestamp_dtmf;
212 uint16_t last_duration;
213 uint32_t flip;
214 char first_digit;
215 char last_digit;
216 switch_queue_t *dtmf_inqueue;
217 switch_mutex_t *dtmf_mutex;
218 uint8_t in_digit_queued;
219};
220
221typedef struct {
222 char *ice_user;
223 char *user_ice;
224 char *luser_ice;
225 char *pass;
226 char *rpass;
227 switch_sockaddr_t *addr;
228 uint32_t funny_stun;
229 switch_time_t next_run;
230 switch_core_media_ice_type_t type;
231 ice_t *ice_params;
232 ice_proto_t proto;
233 uint8_t sending;
234 uint8_t ready;
235 uint8_t rready;
236 int missed_count;
237 char last_sent_id[13];
238} switch_rtp_ice_t;
239
240struct switch_rtp;
241
242typedef struct switch_dtls_s {
243 /* DTLS */
244 SSL_CTX *ssl_ctx;
245 SSL *ssl;
246 BIO *read_bio;
247 BIO *write_bio;
248 dtls_fingerprint_t *local_fp;
249 dtls_fingerprint_t *remote_fp;
250 dtls_state_t state;
251 dtls_state_t last_state;
252 uint8_t new_state;
253 dtls_type_t type;
254 switch_size_t bytes;
255 void *data;
256 switch_socket_t *sock_output;
257 switch_sockaddr_t *remote_addr;
258 char *rsa;
259 char *pvt;
260 char *ca;
261 char *pem;
262 struct switch_rtp *rtp_session;
263} switch_dtls_t;
264
265typedef int (*dtls_state_handler_t)(switch_rtp_t *, switch_dtls_t *);
266
267
268static int dtls_state_handshake(switch_rtp_t *rtp_session, switch_dtls_t *dtls);
269static int dtls_state_ready(switch_rtp_t *rtp_session, switch_dtls_t *dtls);
270static int dtls_state_setup(switch_rtp_t *rtp_session, switch_dtls_t *dtls);
271static int dtls_state_fail(switch_rtp_t *rtp_session, switch_dtls_t *dtls);
272
273dtls_state_handler_t dtls_states[DS_INVALID] = {dtls_state_handshake, dtls_state_setup, dtls_state_ready, dtls_state_fail};
274
275typedef struct ts_normalize_s {
276 uint32_t last_ssrc;
277 uint32_t last_frame;
278 uint32_t ts;
279 uint32_t delta;
280 uint32_t delta_ct;
281 uint32_t delta_ttl;
282} ts_normalize_t;
283
284struct switch_rtp {
285 /*
286 * Two sockets are needed because we might be transcoding protocol families
287 * (e.g. receive over IPv4 and send over IPv6). In case the protocol
288 * families are equal, sock_input == sock_output and only one socket is
289 * used.
290 */
291 switch_socket_t *sock_input, *sock_output, *rtcp_sock_input, *rtcp_sock_output;
292 switch_pollfd_t *read_pollfd, *rtcp_read_pollfd;
293 switch_pollfd_t *jb_pollfd;
294
295 switch_sockaddr_t *local_addr, *rtcp_local_addr;
296 rtp_msg_t send_msg;
297 rtcp_msg_t rtcp_send_msg;
298 rtcp_ext_msg_t rtcp_ext_send_msg;
299 uint8_t fir_seq;
300 uint16_t fir_countdown;
301 ts_normalize_t ts_norm;
302 switch_sockaddr_t *remote_addr, *rtcp_remote_addr;
303 rtp_msg_t recv_msg;
304 rtcp_msg_t rtcp_recv_msg;
305 rtcp_msg_t *rtcp_recv_msg_p;
306
307 uint32_t autoadj_window;
308 uint32_t autoadj_threshold;
309 uint32_t autoadj_tally;
310
311 srtp_ctx_t *send_ctx[2];
312 srtp_ctx_t *recv_ctx[2];
313
314 srtp_policy_t send_policy[2];
315 srtp_policy_t recv_policy[2];
316
317 uint32_t srtp_errs[2];
318 uint32_t srctp_errs[2];
319
320
321 int srtp_idx_rtp;
322 int srtp_idx_rtcp;
323
324 switch_dtls_t *dtls;
325 switch_dtls_t *rtcp_dtls;
326
327 uint16_t seq;
328 uint32_t ssrc;
329 uint32_t remote_ssrc;
330 int8_t sending_dtmf;
331 uint8_t need_mark;
332 switch_payload_t payload;
333 switch_rtp_invalid_handler_t invalid_handler;
334 void *private_data;
335 uint32_t ts;
336 uint32_t last_write_ts;
337 uint32_t last_read_ts;
338 uint32_t last_cng_ts;
339 uint32_t last_write_samplecount;
340 uint32_t delay_samples;
341 uint32_t next_write_samplecount;
342 uint32_t max_next_write_samplecount;
343 uint32_t queue_delay;
344 switch_time_t last_write_timestamp;
345 uint32_t flags[SWITCH_RTP_FLAG_INVALID];
346 switch_memory_pool_t *pool;
347 switch_sockaddr_t *from_addr, *rtcp_from_addr;
348 char *rx_host;
349 switch_port_t rx_port;
350 switch_rtp_ice_t ice;
351 switch_rtp_ice_t rtcp_ice;
352 char *timer_name;
353 char *local_host_str;
354 char *remote_host_str;
355 char *eff_remote_host_str;
356 switch_time_t last_stun;
357 uint32_t samples_per_interval;
358 uint32_t samples_per_second;
359 uint32_t conf_samples_per_interval;
360 uint16_t rtcp_send_rate;
361 uint32_t rsamples_per_interval;
362 uint32_t ms_per_packet;
363 uint32_t one_second;
364 uint32_t consecutive_flaws;
365 uint32_t jitter_lead;
366 double old_mean;
367 switch_time_t next_stat_check_time;
368 switch_port_t local_port;
369 switch_port_t remote_port;
370 switch_port_t eff_remote_port;
371 switch_port_t remote_rtcp_port;
372
373 struct switch_rtp_vad_data vad_data;
374 struct switch_rtp_rfc2833_data dtmf_data;
375 switch_payload_t te;
376 switch_payload_t recv_te;
377 switch_payload_t cng_pt;
378 switch_mutex_t *flag_mutex;
379 switch_mutex_t *read_mutex;
380 switch_mutex_t *write_mutex;
381 switch_timer_t timer;
382 uint8_t ready;
383 uint8_t cn;
384 stfu_instance_t *jb;
385 uint32_t max_missed_packets;
386 uint32_t missed_count;
387 rtp_msg_t write_msg;
388 switch_rtp_crypto_key_t *crypto_keys[SWITCH_RTP_CRYPTO_MAX];
389 int reading;
390 int writing;
391 char *stun_ip;
392 switch_port_t stun_port;
393 int from_auto;
394 uint32_t cng_count;
395 switch_rtp_bug_flag_t rtp_bugs;
396 switch_rtp_stats_t stats;
397 uint32_t hot_hits;
398 uint32_t sync_packets;
399 int rtcp_interval;
400 switch_time_t next_rtcp_send;
401 switch_bool_t rtcp_fresh_frame;
402
403 switch_time_t send_time;
404 switch_byte_t auto_adj_used;
405 uint8_t pause_jb;
406 uint16_t last_seq;
407 switch_time_t last_read_time;
408 switch_size_t last_flush_packet_count;
409 uint32_t interdigit_delay;
410 switch_core_session_t *session;
411 payload_map_t **pmaps;
412 payload_map_t *pmap_tail;
413
414#ifdef ENABLE_ZRTP
415 zrtp_session_t *zrtp_session;
416 zrtp_profile_t *zrtp_profile;
417 zrtp_stream_t *zrtp_stream;
418 int zrtp_mitm_tries;
419 int zinit;
420#endif
421
422};
423
424struct switch_rtcp_report_block {
425 uint32_t ssrc; /* The SSRC identifier of the source to which the information in this reception report block pertains. */
426 unsigned int fraction :8; /* The fraction of RTP data packets from source SSRC_n lost since the previous SR or RR packet was sent */
427 int lost :24; /* The total number of RTP data packets from source SSRC_n that have been lost since the beginning of reception */
428 uint32_t highest_sequence_number_received;
429 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. */
430 uint32_t lsr; /* The middle 32 bits out of 64 in the NTP timestamp */
431 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 */
432};
433
434struct switch_rtcp_sr_head {
435 uint32_t ssrc;
436 uint32_t ntp_msw;
437 uint32_t ntp_lsw;
438 uint32_t ts;
439 uint32_t pc;
440 uint32_t oc;
441};
442
443struct switch_rtcp_sender_info {
444 uint32_t ntp_msw;
445 uint32_t ntp_lsw;
446 uint32_t ts;
447 uint32_t pc;
448 uint32_t oc;
449};
450
451struct switch_rtcp_sender_report {
452 uint32_t ssrc;
453 struct switch_rtcp_sender_info sender_info;
454 struct switch_rtcp_report_block report_block;
455};
456
457struct switch_rtcp_receiver_report {
458 uint32_t ssrc;
459 struct switch_rtcp_report_block report_block;
460};
461
462typedef enum {
463 RESULT_CONTINUE,
464 RESULT_GOTO_END,
465 RESULT_GOTO_RECVFROM,
466 RESULT_GOTO_TIMERCHECK
467} handle_rfc2833_result_t;
468
469static void do_2833(switch_rtp_t *rtp_session);
470
471
472#define rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio" rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"
473
474
475static void switch_rtp_change_ice_dest(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, const char *host, switch_port_t port)
476{
477 int is_rtcp = ice == &rtp_session->rtcp_ice;
478 const char *err = "";
479
480 ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr = switch_core_strdup(rtp_session->pool, host)switch_core_perform_strdup(rtp_session->pool, host, "src/switch_rtp.c"
, (const char *)__func__, 480)
;
481 ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port = port;
482 ice->missed_count = 0;
483
484 switch_sockaddr_info_get(&ice->addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool);
485
486 if (!is_rtcp || rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
487 switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err);
488 }
489
490 if (rtp_session->dtls) {
491
492 if (!is_rtcp || rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
493 switch_sockaddr_info_get(&rtp_session->dtls->remote_addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool);
494 }
495
496 if (is_rtcp && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
497
498 switch_sockaddr_info_get(&rtp_session->rtcp_remote_addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool);
499 if (rtp_session->rtcp_dtls) {
500 //switch_sockaddr_info_get(&rtp_session->rtcp_dtls->remote_addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool);
501 rtp_session->rtcp_dtls->remote_addr = rtp_session->rtcp_remote_addr;
502 rtp_session->rtcp_dtls->sock_output = rtp_session->rtcp_sock_output;
503 }
504
505 }
506 }
507
508}
509
510
511
512static handle_rfc2833_result_t handle_rfc2833(switch_rtp_t *rtp_session, switch_size_t bytes, int *do_cng)
513{
514
515 if (rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]) {
516 rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]++;
517
518 if (rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] > DTMF_SANITY(rtp_session->one_second * 30)) {
519 rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = 0;
520 } else {
521 rtp_session->stats.inbound.last_processed_seq = 0;
522 }
523 }
524
525
526#ifdef DEBUG_2833
527 if (rtp_session->dtmf_data.in_digit_sanity && !(rtp_session->dtmf_data.in_digit_sanity % 100)) {
528 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 528, ((void*)0)
, SWITCH_LOG_ERROR, "sanity %d %ld\n", rtp_session->dtmf_data.in_digit_sanity, bytes);
529 }
530#endif
531
532 if (rtp_session->dtmf_data.in_digit_sanity && !--rtp_session->dtmf_data.in_digit_sanity) {
533
534 rtp_session->dtmf_data.last_digit = 0;
535 rtp_session->dtmf_data.in_digit_ts = 0;
536 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 536, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Failed DTMF sanity check.\n");
537 }
538
539 /* RFC2833 ... like all RFC RE: VoIP, guaranteed to drive you to insanity!
540 We know the real rules here, but if we enforce them, it's an interop nightmare so,
541 we put up with as much as we can so we don't have to deal with being punished for
542 doing it right. Nice guys finish last!
543 */
544
545 if (bytes > rtp_header_len12 && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] &&
546 rtp_session->recv_te && rtp_session->recv_msg.header.pt == rtp_session->recv_te) {
547 switch_size_t len = bytes - rtp_header_len12;
548 unsigned char *packet = (unsigned char *) RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg
.ebody : rtp_session->recv_msg.body)
;
549 int end;
550 uint16_t duration;
551 char key;
552 uint16_t in_digit_seq;
553 uint32_t ts;
554
555 rtp_session->stats.inbound.last_processed_seq = 0;
556
557 if (!(packet[0] || packet[1] || packet[2] || packet[3]) && len >= 8) {
558 packet += 4;
559 len -= 4;
Value stored to 'len' is never read
560 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 560, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING, "DTMF payload offset by 4 bytes.\n");
561 }
562
563 if (!(packet[0] || packet[1] || packet[2] || packet[3]) && rtp_session->dtmf_data.in_digit_ts) {
564 switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
565 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 565, (const char*)(session)
, SWITCH_LOG_ERROR, "Failed DTMF payload check.\n");
566 rtp_session->dtmf_data.last_digit = 0;
567 rtp_session->dtmf_data.in_digit_ts = 0;
568 rtp_session->dtmf_data.in_digit_sanity = 0;
569 }
570
571 end = packet[1] & 0x80 ? 1 : 0;
572 duration = (packet[2] << 8) + packet[3];
573 key = switch_rfc2833_to_char(packet[0]);
574 in_digit_seq = ntohs((uint16_t) rtp_session->recv_msg.header.seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t) rtp_session->recv_msg.header.seq); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
575 ts = htonl(rtp_session->recv_msg.header.ts)(__extension__ ({ unsigned int __v, __x = (rtp_session->recv_msg
.header.ts); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
;
576
577 if (rtp_session->flags[SWITCH_RTP_FLAG_PASS_RFC2833]) {
578
579 if (end) {
580 rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = DTMF_SANITY(rtp_session->one_second * 30) - 3;
581 } else if (!rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]) {
582 rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = 1;
583 }
584
585 return RESULT_CONTINUE;
586 }
587
588 if (in_digit_seq < rtp_session->dtmf_data.in_digit_seq) {
589 if (rtp_session->dtmf_data.in_digit_seq - in_digit_seq > 100) {
590 rtp_session->dtmf_data.in_digit_seq = 0;
591 }
592 }
593#ifdef DEBUG_2833
594 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 594, ((void*)0)
, 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]);
595#endif
596
597 if (in_digit_seq > rtp_session->dtmf_data.in_digit_seq) {
598
599 rtp_session->dtmf_data.in_digit_seq = in_digit_seq;
600#ifdef DEBUG_2833
601
602 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 602, ((void*)0)
, SWITCH_LOG_ERROR, "read: %c %u %u %u %u %d %d %s\n",
603 key, in_digit_seq, rtp_session->dtmf_data.in_digit_seq,
604 ts, duration, rtp_session->recv_msg.header.m, end, end && !rtp_session->dtmf_data.in_digit_ts ? "ignored" : "");
605#endif
606
607 if (!rtp_session->dtmf_data.in_digit_queued && rtp_session->dtmf_data.in_digit_ts) {
608 if ((rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION)) {
609 switch_dtmf_t dtmf = { key, switch_core_min_dtmf_duration(0), 0, SWITCH_DTMF_RTP };
610#ifdef DEBUG_2833
611 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 611, ((void*)0)
, SWITCH_LOG_ERROR, "Early Queuing digit %c:%d\n", dtmf.digit, dtmf.duration / 8);
612#endif
613 switch_rtp_queue_rfc2833_in(rtp_session, &dtmf);
614 rtp_session->dtmf_data.in_digit_queued = 1;
615 }
616
617 if (rtp_session->jb && (rtp_session->rtp_bugs & RTP_BUG_FLUSH_JB_ON_DTMF)) {
618 stfu_n_reset(rtp_session->jb)_stfu_n_reset(rtp_session->jb, "src/switch_rtp.c", __FUNCTION__
, 618)
;
619 }
620
621 }
622
623 /* only set sanity if we do NOT ignore the packet */
624 if (rtp_session->dtmf_data.in_digit_ts) {
625 rtp_session->dtmf_data.in_digit_sanity = 2000;
626 }
627
628 if (rtp_session->dtmf_data.last_duration > duration &&
629 rtp_session->dtmf_data.last_duration > 0xFC17 && ts == rtp_session->dtmf_data.in_digit_ts) {
630 rtp_session->dtmf_data.flip++;
631 }
632
633 if (end) {
634 if (!rtp_session->dtmf_data.in_digit_ts && rtp_session->dtmf_data.last_in_digit_ts != ts) {
635#ifdef DEBUG_2833
636 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 636, ((void*)0)
, SWITCH_LOG_ERROR, "start with end packet %d\n", ts);
637#endif
638 rtp_session->dtmf_data.last_in_digit_ts = ts;
639 rtp_session->dtmf_data.in_digit_ts = ts;
640 rtp_session->dtmf_data.first_digit = key;
641 rtp_session->dtmf_data.in_digit_sanity = 2000;
642 }
643 if (rtp_session->dtmf_data.in_digit_ts) {
644 switch_dtmf_t dtmf = { key, duration, 0, SWITCH_DTMF_RTP };
645
646 if (ts > rtp_session->dtmf_data.in_digit_ts) {
647 dtmf.duration += (ts - rtp_session->dtmf_data.in_digit_ts);
648 }
649 if (rtp_session->dtmf_data.flip) {
650 dtmf.duration += rtp_session->dtmf_data.flip * 0xFFFF;
651 rtp_session->dtmf_data.flip = 0;
652#ifdef DEBUG_2833
653 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 653, ((void*)0)
, SWITCH_LOG_ERROR, "you're welcome!\n");
654#endif
655 }
656#ifdef DEBUG_2833
657 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 657, ((void*)0)
, SWITCH_LOG_ERROR, "done digit=%c ts=%u start_ts=%u dur=%u ddur=%u\n",
658 dtmf.digit, ts, rtp_session->dtmf_data.in_digit_ts, duration, dtmf.duration);
659#endif
660
661 if (!(rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION) && !rtp_session->dtmf_data.in_digit_queued) {
662#ifdef DEBUG_2833
663 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 663, ((void*)0)
, SWITCH_LOG_ERROR, "Queuing digit %c:%d\n", dtmf.digit, dtmf.duration / 8);
664#endif
665 switch_rtp_queue_rfc2833_in(rtp_session, &dtmf);
666 }
667
668 rtp_session->dtmf_data.last_digit = rtp_session->dtmf_data.first_digit;
669
670 rtp_session->dtmf_data.in_digit_ts = 0;
671 rtp_session->dtmf_data.in_digit_sanity = 0;
672 rtp_session->dtmf_data.in_digit_queued = 0;
673 *do_cng = 1;
674 } else {
675 if (!switch_rtp_ready(rtp_session)) {
676 return RESULT_GOTO_END;
677 }
678 switch_cond_next();
679 return RESULT_GOTO_RECVFROM;
680 }
681
682 } else if (!rtp_session->dtmf_data.in_digit_ts) {
683#ifdef DEBUG_2833
684 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 684, ((void*)0)
, SWITCH_LOG_ERROR, "start %d [%c]\n", ts, key);
685#endif
686 rtp_session->dtmf_data.in_digit_ts = ts;
687 rtp_session->dtmf_data.last_in_digit_ts = ts;
688 rtp_session->dtmf_data.first_digit = key;
689 rtp_session->dtmf_data.in_digit_sanity = 2000;
690 }
691
692 rtp_session->dtmf_data.last_duration = duration;
693 } else {
694#ifdef DEBUG_2833
695 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 695, ((void*)0)
, SWITCH_LOG_ERROR, "drop: %c %u %u %u %u %d %d\n",
696 key, in_digit_seq, rtp_session->dtmf_data.in_digit_seq, ts, duration, rtp_session->recv_msg.header.m, end);
697#endif
698 switch_cond_next();
699 return RESULT_GOTO_RECVFROM;
700 }
701 }
702
703 if (bytes && rtp_session->dtmf_data.in_digit_ts) {
704 if (!switch_rtp_ready(rtp_session)) {
705 return RESULT_GOTO_END;
706 }
707
708 if (!rtp_session->dtmf_data.in_interleaved && rtp_session->recv_msg.header.pt != rtp_session->recv_te) {
709 /* Drat, they are sending audio still as well as DTMF ok fine..... *sigh* */
710 rtp_session->dtmf_data.in_interleaved = 1;
711 }
712
713 if (rtp_session->dtmf_data.in_interleaved || (rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION)) {
714 if (rtp_session->recv_msg.header.pt == rtp_session->recv_te) {
715 return RESULT_GOTO_RECVFROM;
716 }
717 } else {
718 *do_cng = 1;
719 return RESULT_GOTO_TIMERCHECK;
720 }
721 }
722
723 return RESULT_CONTINUE;
724}
725
726static int global_init = 0;
727static int rtp_common_write(switch_rtp_t *rtp_session,
728 rtp_msg_t *send_msg, void *data, uint32_t datalen, switch_payload_t payload, uint32_t timestamp, switch_frame_flag_t *flags);
729
730
731static switch_status_t ice_out(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice)
732{
733 uint8_t buf[256] = { 0 };
734 switch_stun_packet_t *packet;
735 unsigned int elapsed;
736 switch_size_t bytes;
737 switch_status_t status = SWITCH_STATUS_SUCCESS;
738 //switch_sockaddr_t *remote_addr = rtp_session->remote_addr;
739 switch_socket_t *sock_output = rtp_session->sock_output;
740 switch_time_t now = switch_micro_time_now();
741
742 if (ice->next_run && ice->next_run > now) {
743 return SWITCH_STATUS_BREAK;
744 }
745
746 ice->next_run = now + RTP_STUN_FREQ1000000;
747
748 if (ice == &rtp_session->rtcp_ice && rtp_session->rtcp_sock_output) {
749 sock_output = rtp_session->rtcp_sock_output;
750 }
751
752 if (!sock_output) {
753 return SWITCH_STATUS_FALSE;
754 }
755
756 switch_assert(rtp_session != NULL)((rtp_session != ((void*)0)) ? (void) (0) : __assert_fail ("rtp_session != ((void*)0)"
, "src/switch_rtp.c", 756, __PRETTY_FUNCTION__))
;
757 switch_assert(ice->ice_user != NULL)((ice->ice_user != ((void*)0)) ? (void) (0) : __assert_fail
("ice->ice_user != ((void*)0)", "src/switch_rtp.c", 757, __PRETTY_FUNCTION__
))
;
758
759 READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session->
reading++
;
760
761 if (rtp_session->last_stun) {
762 elapsed = (unsigned int) ((switch_micro_time_now() - rtp_session->last_stun) / 1000);
763
764 if (elapsed > 30000) {
765 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 765, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "No %s stun for a long time!\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio");
766 rtp_session->last_stun = switch_micro_time_now();
767 //status = SWITCH_STATUS_GENERR;
768 //goto end;
769 }
770 }
771
772 packet = switch_stun_packet_build_header(SWITCH_STUN_BINDING_REQUEST, NULL((void*)0), buf);
773 switch_stun_packet_attribute_add_username(packet, ice->ice_user, (uint16_t)strlen(ice->ice_user));
774
775 memcpy(ice->last_sent_id, packet->header.id, 12);
776
777 //if (ice->pass && ice->type == ICE_GOOGLE_JINGLE) {
778 // switch_stun_packet_attribute_add_password(packet, ice->pass, (uint16_t)strlen(ice->pass));
779 //}
780
781 if ((ice->type & ICE_VANILLA)) {
782 char sw[128] = "";
783
784 switch_stun_packet_attribute_add_priority(packet, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].priority);
785
786 switch_snprintf(sw, sizeof(sw), "FreeSWITCH (%s)", switch_version_revision_human());
787 switch_stun_packet_attribute_add_software(packet, sw, (uint16_t)strlen(sw));
788
789 if ((ice->type & ICE_CONTROLLED)) {
790 switch_stun_packet_attribute_add_controlled(packet);
791 } else {
792 switch_stun_packet_attribute_add_controlling(packet);
793 switch_stun_packet_attribute_add_use_candidate(packet);
794 }
795
796 switch_stun_packet_attribute_add_integrity(packet, ice->rpass);
797 switch_stun_packet_attribute_add_fingerprint(packet);
798 }
799
800
801 bytes = switch_stun_packet_length(packet)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) (packet->header.length); if (__builtin_constant_p (__x
)) __v = ((unsigned short int) ((((__x) >> 8) & 0xff
) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; })) + (sizeof(switch_stun_packet_header_t
))
;
802
803#ifdef DEBUG_EXTRA
804 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 804, (const char*)(rtp_session->session)
, SWITCH_LOG_CRIT, "%s send %s stun\n", rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
, rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio");
805#endif
806 switch_socket_sendto(sock_output, ice->addr, 0, (void *) packet, &bytes);
807
808 ice->sending = 3;
809
810 // end:
811 READ_DEC(rtp_session)switch_mutex_unlock(rtp_session->read_mutex); rtp_session->
reading--
;
812
813 return status;
814}
815
816int icecmp(const char *them, switch_rtp_ice_t *ice)
817{
818 if (strchr(them, ':')(__extension__ (__builtin_constant_p (':') && !__builtin_constant_p
(them) && (':') == '\0' ? (char *) __rawmemchr (them
, ':') : __builtin_strchr (them, ':')))
) {
819 return strcmp(them, ice->user_ice)__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(them) && __builtin_constant_p (ice->user_ice) &&
(__s1_len = __builtin_strlen (them), __s2_len = __builtin_strlen
(ice->user_ice), (!((size_t)(const void *)((them) + 1) - (
size_t)(const void *)(them) == 1) || __s1_len >= 4) &&
(!((size_t)(const void *)((ice->user_ice) + 1) - (size_t)
(const void *)(ice->user_ice) == 1) || __s2_len >= 4)) ?
__builtin_strcmp (them, ice->user_ice) : (__builtin_constant_p
(them) && ((size_t)(const void *)((them) + 1) - (size_t
)(const void *)(them) == 1) && (__s1_len = __builtin_strlen
(them), __s1_len < 4) ? (__builtin_constant_p (ice->user_ice
) && ((size_t)(const void *)((ice->user_ice) + 1) -
(size_t)(const void *)(ice->user_ice) == 1) ? __builtin_strcmp
(them, ice->user_ice) : (__extension__ ({ const unsigned char
*__s2 = (const unsigned char *) (const char *) (ice->user_ice
); int __result = (((const unsigned char *) (const char *) (them
))[0] - __s2[0]); if (__s1_len > 0 && __result == 0
) { __result = (((const unsigned char *) (const char *) (them
))[1] - __s2[1]); if (__s1_len > 1 && __result == 0
) { __result = (((const unsigned char *) (const char *) (them
))[2] - __s2[2]); if (__s1_len > 2 && __result == 0
) __result = (((const unsigned char *) (const char *) (them))
[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p (ice
->user_ice) && ((size_t)(const void *)((ice->user_ice
) + 1) - (size_t)(const void *)(ice->user_ice) == 1) &&
(__s2_len = __builtin_strlen (ice->user_ice), __s2_len <
4) ? (__builtin_constant_p (them) && ((size_t)(const
void *)((them) + 1) - (size_t)(const void *)(them) == 1) ? __builtin_strcmp
(them, ice->user_ice) : (- (__extension__ ({ const unsigned
char *__s2 = (const unsigned char *) (const char *) (them); int
__result = (((const unsigned char *) (const char *) (ice->
user_ice))[0] - __s2[0]); if (__s2_len > 0 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
ice->user_ice))[1] - __s2[1]); if (__s2_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (ice->user_ice))[2] - __s2[2]); if (__s2_len >
2 && __result == 0) __result = (((const unsigned char
*) (const char *) (ice->user_ice))[3] - __s2[3]); } } __result
; })))) : __builtin_strcmp (them, ice->user_ice)))); })
;
820 }
821
822 return strcmp(them, ice->luser_ice)__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(them) && __builtin_constant_p (ice->luser_ice) &&
(__s1_len = __builtin_strlen (them), __s2_len = __builtin_strlen
(ice->luser_ice), (!((size_t)(const void *)((them) + 1) -
(size_t)(const void *)(them) == 1) || __s1_len >= 4) &&
(!((size_t)(const void *)((ice->luser_ice) + 1) - (size_t
)(const void *)(ice->luser_ice) == 1) || __s2_len >= 4)
) ? __builtin_strcmp (them, ice->luser_ice) : (__builtin_constant_p
(them) && ((size_t)(const void *)((them) + 1) - (size_t
)(const void *)(them) == 1) && (__s1_len = __builtin_strlen
(them), __s1_len < 4) ? (__builtin_constant_p (ice->luser_ice
) && ((size_t)(const void *)((ice->luser_ice) + 1)
- (size_t)(const void *)(ice->luser_ice) == 1) ? __builtin_strcmp
(them, ice->luser_ice) : (__extension__ ({ const unsigned
char *__s2 = (const unsigned char *) (const char *) (ice->
luser_ice); int __result = (((const unsigned char *) (const char
*) (them))[0] - __s2[0]); if (__s1_len > 0 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
them))[1] - __s2[1]); if (__s1_len > 1 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
them))[2] - __s2[2]); if (__s1_len > 2 && __result
== 0) __result = (((const unsigned char *) (const char *) (them
))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p (
ice->luser_ice) && ((size_t)(const void *)((ice->
luser_ice) + 1) - (size_t)(const void *)(ice->luser_ice) ==
1) && (__s2_len = __builtin_strlen (ice->luser_ice
), __s2_len < 4) ? (__builtin_constant_p (them) &&
((size_t)(const void *)((them) + 1) - (size_t)(const void *)
(them) == 1) ? __builtin_strcmp (them, ice->luser_ice) : (
- (__extension__ ({ const unsigned char *__s2 = (const unsigned
char *) (const char *) (them); int __result = (((const unsigned
char *) (const char *) (ice->luser_ice))[0] - __s2[0]); if
(__s2_len > 0 && __result == 0) { __result = (((const
unsigned char *) (const char *) (ice->luser_ice))[1] - __s2
[1]); if (__s2_len > 1 && __result == 0) { __result
= (((const unsigned char *) (const char *) (ice->luser_ice
))[2] - __s2[2]); if (__s2_len > 2 && __result == 0
) __result = (((const unsigned char *) (const char *) (ice->
luser_ice))[3] - __s2[3]); } } __result; })))) : __builtin_strcmp
(them, ice->luser_ice)))); })
;
823}
824
825static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
826{
827 switch_stun_packet_t *packet;
828 switch_stun_packet_attribute_t *attr;
829 void *end_buf;
830 char username[34] = { 0 };
831 unsigned char buf[512] = { 0 };
832 switch_size_t cpylen = len;
833 int xlen = 0;
834 int ok = 1;
835 uint32_t *pri = NULL((void*)0);
836 int is_rtcp = ice == &rtp_session->rtcp_ice;
837 uint32_t elapsed;
838
839
840 if (!switch_rtp_ready(rtp_session) || zstr(ice->user_ice)_zstr(ice->user_ice) || zstr(ice->ice_user)_zstr(ice->ice_user)) {
841 return;
842 }
843
844 READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session->
reading++
;
845 WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session->
writing++
;
846
847 if (!switch_rtp_ready(rtp_session)) {
848 goto end;
849 }
850
851 if (cpylen > sizeof(buf)) {
852 cpylen = sizeof(buf);
853 }
854
855
856 memcpy(buf, data, cpylen);
857 packet = switch_stun_packet_parse(buf, (uint32_t)cpylen);
858 if (!packet) {
859 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 859, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Invalid STUN/ICE packet received %ld bytes\n", (long)cpylen);
860 goto end;
861
862 }
863
864 if (!rtp_session->last_stun) {
865 elapsed = 0;
866 } else {
867 elapsed = (unsigned int) ((switch_micro_time_now() - rtp_session->last_stun) / 1000);
868 }
869
870 end_buf = buf + ((sizeof(buf) > packet->header.length) ? packet->header.length : sizeof(buf));
871
872 rtp_session->last_stun = switch_micro_time_now();
873
874 switch_stun_packet_first_attribute(packet, attr)attr = (switch_stun_packet_attribute_t *)(&packet->first_attribute
);
;
875 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 875, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "STUN PACKET TYPE: %s\n",
876 switch_stun_value_to_name(SWITCH_STUN_TYPE_PACKET_TYPE, packet->header.type));
877 do {
878 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 878, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "|---: STUN ATTR %s\n",
879 switch_stun_value_to_name(SWITCH_STUN_TYPE_ATTRIBUTE, attr->type));
880
881 switch (attr->type) {
882 case SWITCH_STUN_ATTR_USE_CAND:
883 {
884 ice->rready = 1;
885 }
886 break;
887 case SWITCH_STUN_ATTR_ERROR_CODE:
888 {
889 switch_stun_error_code_t *err = (switch_stun_error_code_t *) attr->value;
890 uint32_t code = (err->code * 100) + err->number;
891
892 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 892, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING, "%s got stun binding response %u %s\n",
893 rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
,
894 code,
895 err->reason
896 );
897
898 if ((ice->type & ICE_VANILLA) && code == 487) {
899 if ((ice->type & ICE_CONTROLLED)) {
900 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 900, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING, "Changing role to CONTROLLING\n");
901 ice->type &= ~ICE_CONTROLLED;
902 } else {
903 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 903, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING, "Changing role to CONTROLLED\n");
904 ice->type |= ICE_CONTROLLED;
905 }
906 packet->header.type = SWITCH_STUN_BINDING_RESPONSE;
907 }
908
909 }
910 break;
911 case SWITCH_STUN_ATTR_MAPPED_ADDRESS:
912 if (attr->type) {
913 char ip[16];
914 uint16_t port;
915 switch_stun_packet_attribute_get_mapped_address(attr, ip, &port);
916 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 916, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "|------: %s:%d\n", ip, port);
917 }
918 break;
919 case SWITCH_STUN_ATTR_USERNAME:
920 if (attr->type) {
921 switch_stun_packet_attribute_get_username(attr, username, sizeof(username));
922 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 922, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "|------: %s\n", username);
923 }
924 break;
925
926 case SWITCH_STUN_ATTR_PRIORITY:
927 {
928 uint32_t priority = 0;
929 pri = (uint32_t *) attr->value;
930 priority = ntohl(*pri)(__extension__ ({ unsigned int __v, __x = (*pri); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
931 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 931, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "|------: %u\n", priority);
932 ok = priority == ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].priority;
933 }
934 break;
935 }
936
937 if (!switch_stun_packet_next_attribute(attr, end_buf)(attr && (attr = (switch_stun_packet_attribute_t *) (
attr->value + (int16_t)((attr->length & 0x3) ? 0x4 +
(attr->length & ~0x3) : attr->length))) &&
((void *)attr < end_buf) && attr->type &&
(((switch_byte_t *)attr + (int16_t)((attr->length & 0x3
) ? 0x4 + (attr->length & ~0x3) : attr->length)) <
(switch_byte_t *)end_buf))
) {
938 break;
939 }
940
941 xlen += 4 + switch_stun_attribute_padded_length(attr)(int16_t)((attr->length & 0x3) ? 0x4 + (attr->length
& ~0x3) : attr->length)
;
942 } while (xlen <= packet->header.length);
943
944 if ((ice->type & ICE_GOOGLE_JINGLE) && ok) {
945 ok = !strcmp(ice->user_ice, username)__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(ice->user_ice) && __builtin_constant_p (username
) && (__s1_len = __builtin_strlen (ice->user_ice),
__s2_len = __builtin_strlen (username), (!((size_t)(const void
*)((ice->user_ice) + 1) - (size_t)(const void *)(ice->
user_ice) == 1) || __s1_len >= 4) && (!((size_t)(const
void *)((username) + 1) - (size_t)(const void *)(username) ==
1) || __s2_len >= 4)) ? __builtin_strcmp (ice->user_ice
, username) : (__builtin_constant_p (ice->user_ice) &&
((size_t)(const void *)((ice->user_ice) + 1) - (size_t)(const
void *)(ice->user_ice) == 1) && (__s1_len = __builtin_strlen
(ice->user_ice), __s1_len < 4) ? (__builtin_constant_p
(username) && ((size_t)(const void *)((username) + 1
) - (size_t)(const void *)(username) == 1) ? __builtin_strcmp
(ice->user_ice, username) : (__extension__ ({ const unsigned
char *__s2 = (const unsigned char *) (const char *) (username
); int __result = (((const unsigned char *) (const char *) (ice
->user_ice))[0] - __s2[0]); if (__s1_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (ice->user_ice))[1] - __s2[1]); if (__s1_len >
1 && __result == 0) { __result = (((const unsigned char
*) (const char *) (ice->user_ice))[2] - __s2[2]); if (__s1_len
> 2 && __result == 0) __result = (((const unsigned
char *) (const char *) (ice->user_ice))[3] - __s2[3]); } }
__result; }))) : (__builtin_constant_p (username) &&
((size_t)(const void *)((username) + 1) - (size_t)(const void
*)(username) == 1) && (__s2_len = __builtin_strlen (
username), __s2_len < 4) ? (__builtin_constant_p (ice->
user_ice) && ((size_t)(const void *)((ice->user_ice
) + 1) - (size_t)(const void *)(ice->user_ice) == 1) ? __builtin_strcmp
(ice->user_ice, username) : (- (__extension__ ({ const unsigned
char *__s2 = (const unsigned char *) (const char *) (ice->
user_ice); int __result = (((const unsigned char *) (const char
*) (username))[0] - __s2[0]); if (__s2_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (username))[1] - __s2[1]); if (__s2_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (username))[2] - __s2[2]); if (__s2_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) (username))[3] - __s2[3]); } } __result; })))) : __builtin_strcmp
(ice->user_ice, username)))); })
;
946 }
947
948 if (packet->header.type != SWITCH_STUN_BINDING_REQUEST && packet->header.type != SWITCH_STUN_BINDING_RESPONSE) {
949 goto end;
950 }
951
952 if ((ice->type & ICE_VANILLA)) {
953 char foo1[13] = "", foo2[13] = "";
954 if (!ok) ok = !memcmp(packet->header.id, ice->last_sent_id, 12);
955
956 if (packet->header.type == SWITCH_STUN_BINDING_RESPONSE) {
957 ok = 1;
958 if (!ice->rready) {
959 if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
960 rtp_session->ice.rready = 1;
961 rtp_session->rtcp_ice.rready = 1;
962 } else {
963 ice->rready = 1;
964 }
965
966 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
967 }
968 }
969
970 memcpy(foo1, packet->header.id, 12);
971 memcpy(foo2, ice->last_sent_id, 12);
972
973 if (!ok && ice == &rtp_session->ice && rtp_session->rtcp_ice.ice_params && pri &&
974 *pri == rtp_session->rtcp_ice.ice_params->cands[rtp_session->rtcp_ice.ice_params->chosen[1]][1].priority) {
975 ice = &rtp_session->rtcp_ice;
976 ok = 1;
977 }
978
979 if (!zstr(username)_zstr(username)) {
980 if (!icecmp(username, ice)) {
981 ok = 1;
982 } else if(!zstr(rtp_session->rtcp_ice.user_ice)_zstr(rtp_session->rtcp_ice.user_ice) && !icecmp(username, &rtp_session->rtcp_ice)) {
983 ice = &rtp_session->rtcp_ice;
984 ok = 1;
985 }
986 }
987
988 if (ok) {
989 ice->missed_count = 0;
990 } else {
991 switch_rtp_ice_t *icep[2] = { &rtp_session->ice, &rtp_session->rtcp_ice };
992 switch_port_t port = 0;
993 char *host = NULL((void*)0);
994
995 if (elapsed > 20000 && pri) {
996 int i, j;
997 uint32_t old;
998 //const char *tx_host;
999 const char *old_host, *err = NULL((void*)0);
1000 //char bufa[30];
1001 char bufb[30];
1002 char adj_port[6];
1003 switch_channel_t *channel = NULL((void*)0);
1004
1005
1006 ice->missed_count++;
1007 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1007, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING, "missed %d\n", ice->missed_count);
1008
1009
1010 if (rtp_session->session) {
1011 channel = switch_core_session_get_channel(rtp_session->session);
1012 }
1013
1014 //ice->ice_params->cands[ice->ice_params->chosen][ice->proto].priority;
1015 for (j = 0; j < 2; j++) {
1016 for (i = 0; i < icep[j]->ice_params->cand_idx; i++) {
1017 if (icep[j]->ice_params->cands[i][icep[j]->proto].priority == *pri) {
1018 if (j == IPR_RTP) {
1019 icep[j]->ice_params->chosen[j] = i;
1020 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1020, (const char*)(rtp_session->session)
, SWITCH_LOG_INFO, "Change candidate index to %d\n", i);
1021 }
1022
1023 ice = icep[j];
1024 ok = 1;
1025
1026 if (j != IPR_RTP) {
1027 break;
1028 }
1029
1030 old = rtp_session->remote_port;
1031
1032 //tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr);
1033 old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
1034
1035 host = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr;
1036 port = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port;
1037
1038 if (!host || !port) {
1039 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1039, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error setting remote host!\n");
1040 return;
1041 }
1042
1043 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1043, (const char*)(rtp_session->session)
, SWITCH_LOG_INFO,
1044 "ICE Auto Changing port from %s:%u to %s:%u\n", old_host, old, host, port);
1045
1046
1047 if (channel) {
1048 switch_channel_set_variable(channel, "remote_media_ip_reported", switch_channel_get_variable(channel, "remote_media_ip"))switch_channel_set_variable_var_check(channel, "remote_media_ip_reported"
, switch_channel_get_variable_dup(channel, "remote_media_ip",
SWITCH_TRUE, -1), SWITCH_TRUE)
;
1049 switch_channel_set_variable(channel, "remote_media_ip", host)switch_channel_set_variable_var_check(channel, "remote_media_ip"
, host, SWITCH_TRUE)
;
1050 switch_channel_set_variable(channel, "rtp_auto_adjust_ip", host)switch_channel_set_variable_var_check(channel, "rtp_auto_adjust_ip"
, host, SWITCH_TRUE)
;
1051 switch_snprintf(adj_port, sizeof(adj_port), "%u", port);
1052 switch_channel_set_variable(channel, "remote_media_port_reported", switch_channel_get_variable(channel, "remote_media_port"))switch_channel_set_variable_var_check(channel, "remote_media_port_reported"
, switch_channel_get_variable_dup(channel, "remote_media_port"
, SWITCH_TRUE, -1), SWITCH_TRUE)
;
1053 switch_channel_set_variable(channel, "remote_media_port", adj_port)switch_channel_set_variable_var_check(channel, "remote_media_port"
, adj_port, SWITCH_TRUE)
;
1054 switch_channel_set_variable(channel, "rtp_auto_adjust_port", adj_port)switch_channel_set_variable_var_check(channel, "rtp_auto_adjust_port"
, adj_port, SWITCH_TRUE)
;
1055 switch_channel_set_variable(channel, "rtp_auto_candidate_adjust", "true")switch_channel_set_variable_var_check(channel, "rtp_auto_candidate_adjust"
, "true", SWITCH_TRUE)
;
1056 }
1057 rtp_session->auto_adj_used = 1;
1058
1059
1060 switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err);
1061 if (switch_sockaddr_info_get(&ice->addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS ||
1062 !ice->addr) {
1063 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1063, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error setting remote host!\n");
1064 return;
1065 }
1066
1067 if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
1068 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
1069 } else {
1070 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
1071 }
1072
1073 }
1074 }
1075 }
1076 }
1077 }
1078 }
1079
1080 if (ice->missed_count > 5 && !(ice->type & ICE_GOOGLE_JINGLE)) {
1081 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1081, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING, "missed too many: %d, looking for new ICE dest.\n",
1082 ice->missed_count);
1083 ice->rready = 0;
1084 ok = 1;
1085 }
1086
1087 if (ok) {
1088 const char *host = NULL((void*)0), *host2 = NULL((void*)0);
1089 switch_port_t port = 0, port2 = 0;
1090 char buf[80] = "";
1091 char buf2[80] = "";
1092
1093 if (packet->header.type == SWITCH_STUN_BINDING_REQUEST) {
1094 uint8_t stunbuf[512];
1095 switch_stun_packet_t *rpacket;
1096 const char *remote_ip;
1097 switch_size_t bytes;
1098 char ipbuf[25];
1099 switch_sockaddr_t *from_addr = rtp_session->from_addr;
1100 switch_socket_t *sock_output = rtp_session->sock_output;
1101 uint8_t hosts_set = 0;
1102
1103 if (is_rtcp) {
1104 from_addr = rtp_session->rtcp_from_addr;
1105 sock_output = rtp_session->rtcp_sock_output;
1106 }
1107
1108 if (!ice->ready) {
1109 ice->ready = 1;
1110 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
1111 }
1112
1113 memset(stunbuf, 0, sizeof(stunbuf));
1114 rpacket = switch_stun_packet_build_header(SWITCH_STUN_BINDING_RESPONSE, packet->header.id, stunbuf);
1115
1116 if ((ice->type & ICE_GOOGLE_JINGLE)) {
1117 switch_stun_packet_attribute_add_username(rpacket, username, (uint16_t)strlen(username));
1118 }
1119
1120 remote_ip = switch_get_addr(ipbuf, sizeof(ipbuf), from_addr);
1121 switch_stun_packet_attribute_add_xor_binded_address(rpacket, (char *) remote_ip, switch_sockaddr_get_port(from_addr));
1122
1123 if (!switch_cmp_addr(from_addr, ice->addr)) {
1124 hosts_set++;
1125 host = switch_get_addr(buf, sizeof(buf), from_addr);
1126 port = switch_sockaddr_get_port(from_addr);
1127 host2 = switch_get_addr(buf2, sizeof(buf2), ice->addr);
1128 port2 = switch_sockaddr_get_port(ice->addr);
1129 }
1130
1131 if ((ice->type & ICE_VANILLA)) {
1132 switch_stun_packet_attribute_add_integrity(rpacket, ice->pass);
1133 switch_stun_packet_attribute_add_fingerprint(rpacket);
1134 }
1135
1136 if (hosts_set) {
1137 switch_sockaddr_info_get(&ice->addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool);
1138
1139 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1139, (const char*)(rtp_session->session)
, SWITCH_LOG_NOTICE,
1140 "Auto Changing stun/%s/dtls port from %s:%u to %s:%u\n", is_rtcp ? "rtcp" : "rtp",
1141 host2, port2,
1142 host, port);
1143
1144 switch_rtp_change_ice_dest(rtp_session, ice, host, port);
1145 }
1146
1147
1148 bytes = switch_stun_packet_length(rpacket)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) (rpacket->header.length); if (__builtin_constant_p (
__x)) __v = ((unsigned short int) ((((__x) >> 8) & 0xff
) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; })) + (sizeof(switch_stun_packet_header_t
))
;
1149
1150 if (!ice->rready && (ice->type & ICE_VANILLA) && ice->ice_params && hosts_set && !switch_cmp_addr(from_addr, ice->addr)) {
1151 int i = 0;
1152
1153 ice->missed_count = 0;
1154 ice->rready = 1;
1155
1156
1157
1158
1159 for (i = 0; i <= ice->ice_params->cand_idx; i++) {
1160 if (ice->ice_params->cands[i][ice->proto].con_port == port) {
1161 if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, host)__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(ice->ice_params->cands[i][ice->proto].con_addr) &&
__builtin_constant_p (host) && (__s1_len = __builtin_strlen
(ice->ice_params->cands[i][ice->proto].con_addr), __s2_len
= __builtin_strlen (host), (!((size_t)(const void *)((ice->
ice_params->cands[i][ice->proto].con_addr) + 1) - (size_t
)(const void *)(ice->ice_params->cands[i][ice->proto
].con_addr) == 1) || __s1_len >= 4) && (!((size_t)
(const void *)((host) + 1) - (size_t)(const void *)(host) == 1
) || __s2_len >= 4)) ? __builtin_strcmp (ice->ice_params
->cands[i][ice->proto].con_addr, host) : (__builtin_constant_p
(ice->ice_params->cands[i][ice->proto].con_addr) &&
((size_t)(const void *)((ice->ice_params->cands[i][ice
->proto].con_addr) + 1) - (size_t)(const void *)(ice->ice_params
->cands[i][ice->proto].con_addr) == 1) && (__s1_len
= __builtin_strlen (ice->ice_params->cands[i][ice->
proto].con_addr), __s1_len < 4) ? (__builtin_constant_p (host
) && ((size_t)(const void *)((host) + 1) - (size_t)(const
void *)(host) == 1) ? __builtin_strcmp (ice->ice_params->
cands[i][ice->proto].con_addr, host) : (__extension__ ({ const
unsigned char *__s2 = (const unsigned char *) (const char *)
(host); int __result = (((const unsigned char *) (const char
*) (ice->ice_params->cands[i][ice->proto].con_addr)
)[0] - __s2[0]); if (__s1_len > 0 && __result == 0
) { __result = (((const unsigned char *) (const char *) (ice->
ice_params->cands[i][ice->proto].con_addr))[1] - __s2[1
]); if (__s1_len > 1 && __result == 0) { __result =
(((const unsigned char *) (const char *) (ice->ice_params
->cands[i][ice->proto].con_addr))[2] - __s2[2]); if (__s1_len
> 2 && __result == 0) __result = (((const unsigned
char *) (const char *) (ice->ice_params->cands[i][ice->
proto].con_addr))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p
(host) && ((size_t)(const void *)((host) + 1) - (size_t
)(const void *)(host) == 1) && (__s2_len = __builtin_strlen
(host), __s2_len < 4) ? (__builtin_constant_p (ice->ice_params
->cands[i][ice->proto].con_addr) && ((size_t)(const
void *)((ice->ice_params->cands[i][ice->proto].con_addr
) + 1) - (size_t)(const void *)(ice->ice_params->cands[
i][ice->proto].con_addr) == 1) ? __builtin_strcmp (ice->
ice_params->cands[i][ice->proto].con_addr, host) : (- (
__extension__ ({ const unsigned char *__s2 = (const unsigned char
*) (const char *) (ice->ice_params->cands[i][ice->proto
].con_addr); int __result = (((const unsigned char *) (const char
*) (host))[0] - __s2[0]); if (__s2_len > 0 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
host))[1] - __s2[1]); if (__s2_len > 1 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
host))[2] - __s2[2]); if (__s2_len > 2 && __result
== 0) __result = (((const unsigned char *) (const char *) (host
))[3] - __s2[3]); } } __result; })))) : __builtin_strcmp (ice
->ice_params->cands[i][ice->proto].con_addr, host)))
); })
&&
1162 !strcmp(ice->ice_params->cands[i][ice->proto].cand_type, "relay")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(ice->ice_params->cands[i][ice->proto].cand_type) &&
__builtin_constant_p ("relay") && (__s1_len = __builtin_strlen
(ice->ice_params->cands[i][ice->proto].cand_type), __s2_len
= __builtin_strlen ("relay"), (!((size_t)(const void *)((ice
->ice_params->cands[i][ice->proto].cand_type) + 1) -
(size_t)(const void *)(ice->ice_params->cands[i][ice->
proto].cand_type) == 1) || __s1_len >= 4) && (!((size_t
)(const void *)(("relay") + 1) - (size_t)(const void *)("relay"
) == 1) || __s2_len >= 4)) ? __builtin_strcmp (ice->ice_params
->cands[i][ice->proto].cand_type, "relay") : (__builtin_constant_p
(ice->ice_params->cands[i][ice->proto].cand_type) &&
((size_t)(const void *)((ice->ice_params->cands[i][ice
->proto].cand_type) + 1) - (size_t)(const void *)(ice->
ice_params->cands[i][ice->proto].cand_type) == 1) &&
(__s1_len = __builtin_strlen (ice->ice_params->cands[i
][ice->proto].cand_type), __s1_len < 4) ? (__builtin_constant_p
("relay") && ((size_t)(const void *)(("relay") + 1) -
(size_t)(const void *)("relay") == 1) ? __builtin_strcmp (ice
->ice_params->cands[i][ice->proto].cand_type, "relay"
) : (__extension__ ({ const unsigned char *__s2 = (const unsigned
char *) (const char *) ("relay"); int __result = (((const unsigned
char *) (const char *) (ice->ice_params->cands[i][ice->
proto].cand_type))[0] - __s2[0]); if (__s1_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (ice->ice_params->cands[i][ice->proto].cand_type
))[1] - __s2[1]); if (__s1_len > 1 && __result == 0
) { __result = (((const unsigned char *) (const char *) (ice->
ice_params->cands[i][ice->proto].cand_type))[2] - __s2[
2]); if (__s1_len > 2 && __result == 0) __result =
(((const unsigned char *) (const char *) (ice->ice_params
->cands[i][ice->proto].cand_type))[3] - __s2[3]); } } __result
; }))) : (__builtin_constant_p ("relay") && ((size_t)
(const void *)(("relay") + 1) - (size_t)(const void *)("relay"
) == 1) && (__s2_len = __builtin_strlen ("relay"), __s2_len
< 4) ? (__builtin_constant_p (ice->ice_params->cands
[i][ice->proto].cand_type) && ((size_t)(const void
*)((ice->ice_params->cands[i][ice->proto].cand_type
) + 1) - (size_t)(const void *)(ice->ice_params->cands[
i][ice->proto].cand_type) == 1) ? __builtin_strcmp (ice->
ice_params->cands[i][ice->proto].cand_type, "relay") : (
- (__extension__ ({ const unsigned char *__s2 = (const unsigned
char *) (const char *) (ice->ice_params->cands[i][ice->
proto].cand_type); int __result = (((const unsigned char *) (
const char *) ("relay"))[0] - __s2[0]); if (__s2_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("relay"))[1] - __s2[1]); if (__s2_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("relay"))[2] - __s2[2]); if (__s2_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) ("relay"))[3] - __s2[3]); } } __result; })))) : __builtin_strcmp
(ice->ice_params->cands[i][ice->proto].cand_type, "relay"
)))); })
) {
1163
1164 if (rtp_session->last_stun && elapsed < 5000) {
1165 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1165, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING,
1166 "Skiping RELAY stun/%s/dtls port change from %s:%u to %s:%u\n", is_rtcp ? "rtcp" : "rtp",
1167 host2, port2,
1168 host, port);
1169
1170 goto end;
1171 }
1172
1173 break;
1174 }
1175 }
1176 }
1177
1178 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1178, (const char*)(rtp_session->session)
, SWITCH_LOG_NOTICE,
1179 "Auto Changing stun/%s/dtls port from %s:%u to %s:%u\n", is_rtcp ? "rtcp" : "rtp",
1180 host2, port2,
1181 host, port);
1182
1183 switch_rtp_change_ice_dest(rtp_session, ice, host, port);
1184 }
1185
1186 switch_socket_sendto(sock_output, from_addr, 0, (void *) rpacket, &bytes);
1187 }
1188 } else if (packet->header.type == SWITCH_STUN_BINDING_ERROR_RESPONSE) {
1189
1190 if (rtp_session->session) {
1191 switch_core_session_message_t msg = { 0 };
1192 msg.from = __FILE__"src/switch_rtp.c";
1193 msg.numeric_arg = packet->header.type;
1194 msg.pointer_arg = packet;
1195 msg.message_id = SWITCH_MESSAGE_INDICATE_STUN_ERROR;
1196 switch_core_session_receive_message(rtp_session->session, &msg)switch_core_session_perform_receive_message(rtp_session->session
, &msg, "src/switch_rtp.c", (const char *)__func__, 1196)
;
1197 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1197, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG,
1198 "STUN/ICE binding error received on %s channel\n", rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio");
1199 }
1200
1201 }
1202
1203
1204
1205
1206 end:
1207
1208 READ_DEC(rtp_session)switch_mutex_unlock(rtp_session->read_mutex); rtp_session->
reading--
;
1209 WRITE_DEC(rtp_session)switch_mutex_unlock(rtp_session->write_mutex); rtp_session
->writing--
;
1210}
1211
1212#ifdef ENABLE_ZRTP
1213SWITCH_STANDARD_SCHED_FUNC(zrtp_cache_save_callback)static void zrtp_cache_save_callback (switch_scheduler_task_t
*task)
1214{
1215 zrtp_status_t status = zrtp_status_ok;
1216
1217 status = zrtp_def_cache_store(zrtp_global);
1218 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 1218, ((void*)0)
, SWITCH_LOG_DEBUG, "Saving ZRTP cache: %s\n", zrtp_status_ok == status ? "OK" : "FAIL");
1219 task->runtime = switch_epoch_time_now(NULL((void*)0)) + 900;
1220}
1221
1222static int zrtp_send_rtp_callback(const zrtp_stream_t *stream, char *rtp_packet, unsigned int rtp_packet_length)
1223{
1224 switch_rtp_t *rtp_session = zrtp_stream_get_userdata(stream);
1225 switch_size_t len = rtp_packet_length;
1226 zrtp_status_t status = zrtp_status_ok;
1227
1228 switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, rtp_packet, &len);
1229 return status;
1230}
1231
1232static void zrtp_event_callback(zrtp_stream_t *stream, unsigned event)
1233{
1234 switch_rtp_t *rtp_session = zrtp_stream_get_userdata(stream);
1235 zrtp_session_info_t zrtp_session_info;
1236
1237 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
1238 switch_event_t *fsevent = NULL((void*)0);
1239 const char *type;
1240
1241 type = rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio";
1242
1243 switch (event) {
1244 case ZRTP_EVENT_IS_SECURE:
1245 {
1246 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_SEND] = 1;
1247 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_RECV] = 1;
1248 if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
1249 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 1;
1250 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 1;
1251 }
1252 if (zrtp_status_ok == zrtp_session_get(stream->session, &zrtp_session_info)) {
1253 if (zrtp_session_info.sas_is_ready) {
1254
1255 switch_channel_set_variable_name_printf(channel, "true", "zrtp_secure_media_confirmed_%s", type);
1256 switch_channel_set_variable_name_printf(channel, stream->session->sas1.buffer, "zrtp_sas1_string_%s", type);
1257 switch_channel_set_variable_name_printf(channel, stream->session->sas2.buffer, "zrtp_sas2_string", type);
1258 zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, (uint8_t)1);
1259 }
1260 }
1261
1262 if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
1263
1264
1265 if (rtp_session->session) {
1266 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
1267 switch_rtp_t *video_rtp_session = switch_channel_get_private(channel, "__zrtp_video_rtp_session");
1268
1269 if (!video_rtp_session) {
1270 video_rtp_session = switch_channel_get_private_partner(channel, "__zrtp_video_rtp_session");
1271 }
1272
1273 if (video_rtp_session) {
1274 if (zrtp_status_ok != zrtp_stream_attach(stream->session, &video_rtp_session->zrtp_stream)) {
1275 abort();
1276 }
1277 zrtp_stream_set_userdata(video_rtp_session->zrtp_stream, video_rtp_session);
1278 if (switch_true(switch_channel_get_variable(channel, "zrtp_enrollment")switch_channel_get_variable_dup(channel, "zrtp_enrollment", SWITCH_TRUE
, -1)
)) {
1279 zrtp_stream_registration_start(video_rtp_session->zrtp_stream, video_rtp_session->ssrc);
1280 } else {
1281 zrtp_stream_start(video_rtp_session->zrtp_stream, video_rtp_session->ssrc);
1282 }
1283 }
1284 }
1285 }
1286
1287 if (switch_event_create(&fsevent, SWITCH_EVENT_CALL_SECURE)switch_event_create_subclass_detailed("src/switch_rtp.c", (const
char * )(const char *)__func__, 1287, &fsevent, SWITCH_EVENT_CALL_SECURE
, ((void*)0))
== SWITCH_STATUS_SUCCESS) {
1288 switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_media_type", "%s", type);
1289 switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "zrtp:%s:%s", stream->session->sas1.buffer,
1290 stream->session->sas2.buffer);
1291 switch_event_add_header_string(fsevent, SWITCH_STACK_BOTTOM, "caller-unique-id", switch_channel_get_uuid(channel));
1292 switch_event_fire(&fsevent)switch_event_fire_detailed("src/switch_rtp.c", (const char * )
(const char *)__func__, 1292, &fsevent, ((void*)0))
;
1293 }
1294 }
1295 break;
1296#if 0
1297 case ZRTP_EVENT_NO_ZRTP_QUICK:
1298 {
1299 if (stream != NULL((void*)0)) {
1300 zrtp_stream_stop(stream);
1301 }
1302 }
1303 break;
1304#endif
1305 case ZRTP_EVENT_IS_CLIENT_ENROLLMENT:
1306 {
1307 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1307, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "Enrolled complete!\n");
1308 switch_channel_set_variable_name_printf(channel, "true", "zrtp_enroll_complete_%s", type);
1309 }
1310 break;
1311
1312 case ZRTP_EVENT_USER_ALREADY_ENROLLED:
1313 {
1314 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1314, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "User already enrolled!\n");
1315 switch_channel_set_variable_name_printf(channel, "true", "zrtp_already_enrolled_%s", type);
1316 }
1317 break;
1318
1319 case ZRTP_EVENT_NEW_USER_ENROLLED:
1320 {
1321 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1321, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "New user enrolled!\n");
1322 switch_channel_set_variable_name_printf(channel, "true", "zrtp_new_user_enrolled_%s", type);
1323 }
1324 break;
1325
1326 case ZRTP_EVENT_USER_UNENROLLED:
1327 {
1328 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1328, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "User unenrolled!\n");
1329 switch_channel_set_variable_name_printf(channel, "true", "zrtp_user_unenrolled_%s", type);
1330 }
1331 break;
1332
1333 case ZRTP_EVENT_IS_PENDINGCLEAR:
1334 {
1335 switch_channel_set_variable_name_printf(channel, "false", "zrtp_secure_media_confirmed_%s", type);
1336 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_SEND] = 0;
1337 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_RECV] = 0;
1338 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
1339 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
1340 rtp_session->zrtp_mitm_tries = 0;
1341 }
1342 break;
1343
1344 case ZRTP_EVENT_NO_ZRTP:
1345 {
1346 switch_channel_set_variable_name_printf(channel, "false", "zrtp_secure_media_confirmed_%s", type);
1347 }
1348 break;
1349
1350 default:
1351 break;
1352 }
1353}
1354
1355static void zrtp_logger(int level, const char *data, int len, int offset)
1356{
1357 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 1357, ((void*)0)
, SWITCH_LOG_DEBUG, "%s", data);
1358}
1359#endif
1360
1361SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_init(switch_memory_pool_t *pool)
1362{
1363#ifdef ENABLE_ZRTP
1364 const char *zid_string = switch_core_get_variable_pdup("switch_serial", pool);
1365 const char *zrtp_enabled = switch_core_get_variable_pdup("zrtp_enabled", pool);
1366 zrtp_config_t zrtp_config;
1367 char zrtp_cache_path[256] = "";
1368 zrtp_on = zrtp_enabled ? switch_true(zrtp_enabled) : 0;
1369#endif
1370 if (global_init) {
1371 return;
1372 }
1373 switch_core_hash_init(&alloc_hash)switch_core_hash_init_case(&alloc_hash, SWITCH_TRUE);
1374#ifdef ENABLE_ZRTP
1375 if (zrtp_on) {
1376 uint32_t cache_len;
1377 zrtp_config_defaults(&zrtp_config);
1378 strcpy(zrtp_config.client_id, "FreeSWITCH");
1379 zrtp_config.is_mitm = 1;
1380 zrtp_config.lic_mode = ZRTP_LICENSE_MODE_ACTIVE;
1381 switch_snprintf(zrtp_cache_path, sizeof(zrtp_cache_path), "%s%szrtp.dat", SWITCH_GLOBAL_dirs.db_dir, SWITCH_PATH_SEPARATOR"/");
1382 cache_len=(uint32_t)strlen(zrtp_cache_path);
1383 ZSTR_SET_EMPTY(zrtp_config.def_cache_path);
1384 zrtp_config.def_cache_path.length = cache_len > zrtp_config.def_cache_path.max_length ? zrtp_config.def_cache_path.max_length : (uint16_t)cache_len;
1385 strncpy(zrtp_config.def_cache_path.buffer, zrtp_cache_path, zrtp_config.def_cache_path.max_length)__builtin_strncpy (zrtp_config.def_cache_path.buffer, zrtp_cache_path
, zrtp_config.def_cache_path.max_length)
;
1386 zrtp_config.cb.event_cb.on_zrtp_protocol_event = (void (*)(zrtp_stream_t*,zrtp_protocol_event_t))zrtp_event_callback;
1387 zrtp_config.cb.misc_cb.on_send_packet = zrtp_send_rtp_callback;
1388 zrtp_config.cb.event_cb.on_zrtp_security_event = (void (*)(zrtp_stream_t*,zrtp_security_event_t))zrtp_event_callback;
1389 zrtp_log_set_log_engine((zrtp_log_engine *) zrtp_logger);
1390 zrtp_log_set_level(4);
1391 if (zrtp_status_ok == zrtp_init(&zrtp_config, &zrtp_global)) {
1392 memcpy(zid, zid_string, 12);
1393 switch_scheduler_add_task(switch_epoch_time_now(NULL((void*)0)) + 900, zrtp_cache_save_callback, "zrtp_cache_save", "core", 0, NULL((void*)0),
1394 SSHF_NONE | SSHF_NO_DEL);
1395 } else {
1396 switch_core_set_variable("zrtp_enabled", NULL((void*)0));
1397 zrtp_on = 0;
1398 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 1398, ((void*)0)
, SWITCH_LOG_CRIT, "ZRTP init failed!\n");
1399 }
1400 }
1401#endif
1402#ifdef ENABLE_SRTP1
1403 srtp_init();
1404#endif
1405 switch_mutex_init(&port_lock, SWITCH_MUTEX_NESTED0x1, pool);
1406 global_init = 1;
1407}
1408
1409static uint8_t get_next_write_ts(switch_rtp_t *rtp_session, uint32_t timestamp)
1410{
1411 uint8_t m = 0;
1412
1413 if (rtp_session->rtp_bugs & RTP_BUG_SEND_LINEAR_TIMESTAMPS) {
1414 rtp_session->ts += rtp_session->samples_per_interval;
1415 if (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->ts > 0) {
1416 rtp_session->ts = rtp_session->last_write_ts + rtp_session->samples_per_interval;
1417 }
1418 } else if (timestamp) {
1419 rtp_session->ts = (uint32_t) timestamp;
1420 /* Send marker bit if timestamp is lower/same as before (resetted/new timer) */
1421 if (rtp_session->ts <= rtp_session->last_write_ts && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
1422 m++;
1423 }
1424 } else if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) {
1425 rtp_session->ts = rtp_session->timer.samplecount;
1426
1427 if (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->ts > 0) {
1428 rtp_session->ts = rtp_session->last_write_ts + rtp_session->samples_per_interval;
1429 }
1430 } else {
1431 rtp_session->ts += rtp_session->samples_per_interval;
1432 if (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->ts > 0) {
1433 rtp_session->ts = rtp_session->last_write_ts + rtp_session->samples_per_interval;
1434 }
1435 }
1436
1437 return m;
1438}
1439
1440static void send_fir(switch_rtp_t *rtp_session)
1441{
1442
1443 if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->ice.ice_user) {
1444 return;
1445 }
1446
1447 if (rtp_session->remote_ssrc == 0) {
1448 rtp_session->remote_ssrc = rtp_session->stats.rtcp.peer_ssrc;
1449 }
1450
1451 if (rtp_session->remote_ssrc == 0) {
1452 rtp_session->remote_ssrc = ntohl(rtp_session->recv_msg.header.ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->recv_msg
.header.ssrc); if (__builtin_constant_p (__x)) __v = ((((__x)
& 0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
;
1453 }
1454
1455 if (rtp_session->remote_ssrc == 0) {
1456 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1456, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "Peer ssrc not known yet for FIR\n");
1457 return;
1458 }
1459
1460 if (rtp_session->rtcp_sock_output && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
1461 rtcp_fir_t *fir = (rtcp_fir_t *) rtp_session->rtcp_ext_send_msg.body;
1462 switch_size_t rtcp_bytes;
1463
1464 rtp_session->rtcp_ext_send_msg.header.version = 2;
1465 rtp_session->rtcp_ext_send_msg.header.p = 0;
1466 rtp_session->rtcp_ext_send_msg.header.fmt = 4;
1467 rtp_session->rtcp_ext_send_msg.header.pt = 206;
1468
1469 rtp_session->rtcp_ext_send_msg.header.send_ssrc = htonl(rtp_session->ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->ssrc
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
1470 rtp_session->rtcp_ext_send_msg.header.recv_ssrc = 0;//htonl(rtp_session->stats.rtcp.peer_ssrc);
1471
1472 //fir->ssrc = htonl(rtp_session->stats.rtcp.peer_ssrc);
1473 fir->ssrc = htonl(rtp_session->remote_ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->remote_ssrc
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
1474 fir->seq = ++rtp_session->fir_seq;
1475 fir->r1 = fir->r2 = fir->r3 = 0;
1476
1477 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1477, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "Sending RTCP FIR %d\n", rtp_session->fir_seq);
1478
1479 rtcp_bytes = sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t);
1480 rtp_session->rtcp_ext_send_msg.header.length = htons((u_short)(rtcp_bytes / 4) - 1)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((u_short)(rtcp_bytes / 4) - 1); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
1481
1482
1483#ifdef ENABLE_SRTP1
1484 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
1485 int sbytes = (int) rtcp_bytes;
1486 int stat = srtp_protect_rtcp(rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_ext_send_msg.header, &sbytes);
1487
1488 if (stat) {
1489 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1489, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
1490 goto end;
1491 } else {
1492 rtcp_bytes = sbytes;
1493 }
1494
1495 }
1496#endif
1497
1498#ifdef ENABLE_ZRTP
1499 /* ZRTP Send */
1500 if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
1501 unsigned int sbytes = (int) rtcp_bytes;
1502 zrtp_status_t stat = zrtp_status_fail;
1503
1504 stat = zrtp_process_rtcp(rtp_session->zrtp_stream, (void *) &rtp_session->rtcp_ext_send_msg, &sbytes);
1505
1506 switch (stat) {
1507 case zrtp_status_ok:
1508 break;
1509 case zrtp_status_drop:
1510 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 1510, ((void*)0)
, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
1511 goto end;
1512 break;
1513 case zrtp_status_fail:
1514 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 1514, ((void*)0)
, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
1515 break;
1516 default:
1517 break;
1518 }
1519
1520 rtcp_bytes = sbytes;
1521 }
1522#endif
1523
1524#ifdef DEBUG_EXTRA
1525 {
1526 const char *old_host;
1527 char bufb[30];
1528 old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr);
1529 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1529, (const char*)(rtp_session->session)
, SWITCH_LOG_CRIT, "%s SEND %s RTCP %s:%d %ld\n",
1530 rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
,
1531 rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio",
1532 old_host,
1533 switch_sockaddr_get_port(rtp_session->rtcp_remote_addr),
1534 rtcp_bytes);
1535 }
1536#endif
1537 if (switch_socket_sendto(rtp_session->rtcp_sock_output, rtp_session->rtcp_remote_addr, 0, (void *)&rtp_session->rtcp_ext_send_msg, &rtcp_bytes ) != SWITCH_STATUS_SUCCESS) {
1538 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1538, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG,"RTCP packet not written\n");
1539 } else {
1540 rtp_session->stats.inbound.period_packet_count = 0;
1541 }
1542 }
1543
1544#ifdef ENABLE_SRTP1
1545 end:
1546#endif
1547
1548 return;
1549}
1550
1551
1552
1553static void send_pli(switch_rtp_t *rtp_session)
1554{
1555
1556 if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->ice.ice_user) {
1557 return;
1558 }
1559
1560 if (rtp_session->rtcp_sock_output && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
1561 switch_size_t rtcp_bytes;
1562
1563 rtp_session->rtcp_ext_send_msg.header.version = 2;
1564 rtp_session->rtcp_ext_send_msg.header.p = 0;
1565 rtp_session->rtcp_ext_send_msg.header.fmt = 1;
1566 rtp_session->rtcp_ext_send_msg.header.pt = 206;
1567
1568 rtp_session->rtcp_ext_send_msg.header.send_ssrc = htonl(rtp_session->ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->ssrc
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
1569 rtp_session->rtcp_ext_send_msg.header.recv_ssrc = 0;
1570
1571 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1571, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "Sending RTCP PLI\n");
1572
1573 rtcp_bytes = sizeof(switch_rtcp_ext_hdr_t);
1574 rtp_session->rtcp_ext_send_msg.header.length = htons((u_short)(rtcp_bytes / 4) - 1)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((u_short)(rtcp_bytes / 4) - 1); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
1575
1576
1577#ifdef ENABLE_SRTP1
1578 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
1579 int sbytes = (int) rtcp_bytes;
1580 int stat = srtp_protect_rtcp(rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_ext_send_msg.header, &sbytes);
1581
1582 if (stat) {
1583 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1583, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
1584 goto end;
1585 } else {
1586 rtcp_bytes = sbytes;
1587 }
1588
1589 }
1590#endif
1591
1592#ifdef ENABLE_ZRTP
1593 /* ZRTP Send */
1594 if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
1595 unsigned int sbytes = (int) rtcp_bytes;
1596 zrtp_status_t stat = zrtp_status_fail;
1597
1598 stat = zrtp_process_rtcp(rtp_session->zrtp_stream, (void *) &rtp_session->rtcp_ext_send_msg, &sbytes);
1599
1600 switch (stat) {
1601 case zrtp_status_ok:
1602 break;
1603 case zrtp_status_drop:
1604 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 1604, ((void*)0)
, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
1605 goto end;
1606 break;
1607 case zrtp_status_fail:
1608 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 1608, ((void*)0)
, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
1609 break;
1610 default:
1611 break;
1612 }
1613
1614 rtcp_bytes = sbytes;
1615 }
1616#endif
1617
1618#ifdef DEBUG_EXTRA
1619 {
1620 const char *old_host;
1621 char bufb[30];
1622 old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr);
1623 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1623, (const char*)(rtp_session->session)
, SWITCH_LOG_CRIT, "%s SEND %s RTCP %s:%d %ld\n",
1624 rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
,
1625 rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio",
1626 old_host,
1627 switch_sockaddr_get_port(rtp_session->rtcp_remote_addr),
1628 rtcp_bytes);
1629 }
1630
1631#endif
1632 if (switch_socket_sendto(rtp_session->rtcp_sock_output, rtp_session->rtcp_remote_addr, 0, (void *)&rtp_session->rtcp_ext_send_msg, &rtcp_bytes ) != SWITCH_STATUS_SUCCESS) {
1633 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1633, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG,"RTCP packet not written\n");
1634 } else {
1635 rtp_session->stats.inbound.period_packet_count = 0;
1636 }
1637 }
1638
1639#ifdef ENABLE_SRTP1
1640 end:
1641#endif
1642 return;
1643}
1644
1645static void do_mos(switch_rtp_t *rtp_session, int force) {
1646
1647 if ((switch_size_t)rtp_session->stats.inbound.recved < rtp_session->stats.inbound.flaws) {
1648 rtp_session->stats.inbound.flaws = 0;
1649 }
1650
1651 if (rtp_session->stats.inbound.recved > 0 &&
1652 rtp_session->stats.inbound.flaws && (force || rtp_session->stats.inbound.last_flaw != rtp_session->stats.inbound.flaws)) {
1653 int R;
1654
1655 if (rtp_session->consecutive_flaws++) {
1656 int diff, penalty;
1657
1658 diff = (rtp_session->stats.inbound.flaws - rtp_session->stats.inbound.last_flaw);
1659
1660 if (diff < 1) diff = 1;
1661
1662 penalty = diff * 2;
1663
1664 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1664, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "%s %s %d consecutive flaws, adding %d flaw penalty\n",
1665 rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
, rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio",
1666 rtp_session->consecutive_flaws, penalty);
1667
1668 rtp_session->stats.inbound.flaws += penalty;
1669 }
1670
1671 R = (int)((double)((double)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws) / (double)rtp_session->stats.inbound.recved) * 100.0);
1672
1673 if (R < 0 || R > 100) R = 100;
1674
1675 rtp_session->stats.inbound.R = R;
1676 rtp_session->stats.inbound.mos = 1 + (0.035) * R + (.000007) * R * (R-60) * (100-R);
1677 rtp_session->stats.inbound.last_flaw = rtp_session->stats.inbound.flaws;
1678 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1678, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "%s %s stat %0.2f %ld/%d flaws: %ld mos: %0.2f v: %0.2f %0.2f/%0.2f\n",
1679 rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
,
1680 rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio",
1681 rtp_session->stats.inbound.R,
1682 (long int)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws), rtp_session->stats.inbound.recved,
1683 (long int)rtp_session->stats.inbound.flaws,
1684 rtp_session->stats.inbound.mos,
1685 rtp_session->stats.inbound.variance,
1686 rtp_session->stats.inbound.min_variance,
1687 rtp_session->stats.inbound.max_variance
1688 );
1689 } else {
1690 rtp_session->consecutive_flaws = 0;
1691 }
1692}
1693
1694void burstr_calculate ( int loss[], int received, double *burstr, double *lossr )
1695{
1696 int lost = 0;
1697 int bursts = 0;
1698 int i;
1699
1700 for ( i = 0; i < LOST_BURST_ANALYZE500; i++ ) {
1701 lost += i * loss[i];
1702 bursts += loss[i];
1703 }
1704 if (received > 0 && bursts > 0) {
1705 *burstr = (double)((double)lost / (double)bursts) / (double)(1.0 / ( 1.0 - (double)lost / (double)received ));
1706 if (*burstr < 0) {
1707 *burstr = - *burstr;
1708 }
1709 } else {
1710 *burstr = 0;
1711 }
1712 if (received > 0) {
1713 *lossr = (double)((double)lost / (double)received);
1714 } else {
1715 *lossr = 0;
1716 }
1717}
1718
1719static void reset_jitter_seq(switch_rtp_t *rtp_session)
1720{
1721 rtp_session->stats.inbound.last_proc_time = 0;
1722 rtp_session->stats.inbound.last_processed_seq = 0;
1723 rtp_session->jitter_lead = 0;
1724 rtp_session->consecutive_flaws = 0;
1725 rtp_session->stats.inbound.last_flaw = 0;
1726}
1727
1728static void check_jitter(switch_rtp_t *rtp_session)
1729{
1730 switch_time_t current_time;
1731 int64_t diff_time = 0, cur_diff = 0;
1732 int seq;
1733
1734 current_time = switch_micro_time_now() / 1000;
1735
1736 if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] || rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] || rtp_session->dtmf_data.in_digit_ts) {
1737 reset_jitter_seq(rtp_session);
1738 return;
1739 }
1740
1741 if (++rtp_session->jitter_lead < JITTER_LEAD_FRAMES10 || !rtp_session->stats.inbound.last_proc_time) {
1742 rtp_session->stats.inbound.last_proc_time = current_time;
1743 return;
1744 }
1745
1746 diff_time = (current_time - rtp_session->stats.inbound.last_proc_time);
1747 seq = (int)(uint16_t) ntohs((uint16_t) rtp_session->recv_msg.header.seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t) rtp_session->recv_msg.header.seq); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
1748
1749 /* Burst and Packet Loss */
1750 rtp_session->stats.inbound.recved++;
1751
1752 if (rtp_session->stats.inbound.last_processed_seq > 0 && seq > (int)(rtp_session->stats.inbound.last_processed_seq + 1)) {
1753 int lost = (seq - rtp_session->stats.inbound.last_processed_seq - 1);
1754
1755 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1755, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "%s Got: %s seq %d but expected: %d lost: %d\n",
1756 rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
,
1757 rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio",
1758 seq,
1759 (rtp_session->stats.inbound.last_processed_seq + 1), lost);
1760 rtp_session->stats.inbound.last_loss++;
1761
1762 if (rtp_session->stats.inbound.last_loss > 0 && rtp_session->stats.inbound.last_loss < LOST_BURST_CAPTURE1024) {
1763 rtp_session->stats.inbound.loss[rtp_session->stats.inbound.last_loss] += lost;
1764 }
1765
1766 rtp_session->stats.inbound.flaws += lost;
1767
1768 } else {
1769 rtp_session->stats.inbound.last_loss = 0;
1770 }
1771
1772 rtp_session->stats.inbound.last_processed_seq = seq;
1773
1774 /* Burst and Packet Loss */
1775
1776 if (current_time > rtp_session->next_stat_check_time) {
1777 rtp_session->next_stat_check_time = current_time + 5000;
1778 burstr_calculate(rtp_session->stats.inbound.loss, rtp_session->stats.inbound.recved,
1779 &(rtp_session->stats.inbound.burstrate), &(rtp_session->stats.inbound.lossrate));
1780 do_mos(rtp_session, SWITCH_TRUE);
1781 } else {
1782 do_mos(rtp_session, SWITCH_FALSE);
1783 }
1784
1785
1786 if ( diff_time < 0 ) {
1787 diff_time = -diff_time;
1788 }
1789
1790 rtp_session->stats.inbound.jitter_n++;
1791 rtp_session->stats.inbound.jitter_add += diff_time;
1792
1793 cur_diff = (int64_t)(diff_time - rtp_session->stats.inbound.mean_interval);
1794
1795 rtp_session->stats.inbound.jitter_addsq += (cur_diff * cur_diff);
1796 rtp_session->stats.inbound.last_proc_time = current_time;
1797
1798 if (rtp_session->stats.inbound.jitter_n > 0) {
1799 double ipdv;
1800
1801 rtp_session->stats.inbound.mean_interval = (double)rtp_session->stats.inbound.jitter_add / (double)rtp_session->stats.inbound.jitter_n;
1802
1803 if (!rtp_session->old_mean) {
1804 rtp_session->old_mean = rtp_session->stats.inbound.mean_interval;
1805 }
1806
1807 rtp_session->stats.inbound.variance = (double)rtp_session->stats.inbound.jitter_addsq / (double)rtp_session->stats.inbound.jitter_n;
1808
1809 //printf("CHECK %d +%ld +%ld %f %f\n", rtp_session->timer.samplecount, diff_time, (diff_time * diff_time), rtp_session->stats.inbound.mean_interval, rtp_session->stats.inbound.variance);
1810
1811 ipdv = rtp_session->old_mean - rtp_session->stats.inbound.mean_interval;
1812
1813 if ( ipdv > IPDV_THRESHOLD1.0 ) { /* It shows Increasing Delays */
1814 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 1814, ((void*)0)
, SWITCH_LOG_DEBUG1, "Calculated Instantaneous Packet Delay Variation: %s packet %lf\n",
1815 rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", ipdv);
1816 }
1817
1818 if ( rtp_session->stats.inbound.variance < rtp_session->stats.inbound.min_variance || rtp_session->stats.inbound.min_variance == 0 ) {
1819 rtp_session->stats.inbound.min_variance = rtp_session->stats.inbound.variance;
1820 }
1821
1822 if ( rtp_session->stats.inbound.variance > rtp_session->stats.inbound.max_variance ) {
1823 rtp_session->stats.inbound.max_variance = rtp_session->stats.inbound.variance;
1824 }
1825
1826 rtp_session->old_mean = rtp_session->stats.inbound.mean_interval;
1827 }
1828}
1829
1830static void rtcp_generate_sender_info(switch_rtp_t *rtp_session, struct switch_rtcp_sender_info *sr){
1831 switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1832 switch_time_t now;
1833 uint32_t sec, ntp_sec, ntp_usec;
1834 switch_time_exp_t now_hr;
1835 now = switch_time_now();
1836 sec = (uint32_t)(now/1000000); /* convert to seconds */
1837 ntp_sec = sec+NTP_TIME_OFFSET2208988800UL; /* convert to NTP seconds */
1838 sr->ntp_msw = htonl(ntp_sec)(__extension__ ({ unsigned int __v, __x = (ntp_sec); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
; /* store result in "most significant word" */
1839 ntp_usec = (uint32_t)(now - (sec*1000000)); /* remove seconds to keep only the microseconds */
1840 sr->ntp_lsw = htonl((u_long)(ntp_usec*(double)(((uint64_t)1)<<32)*1.0e-6))(__extension__ ({ unsigned int __v, __x = ((u_long)(ntp_usec*
(double)(((uint64_t)1)<<32)*1.0e-6)); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
; /* convert microseconds to fraction of 32bits and store result in "least significatn word" */
1841
1842 sr->ts = htonl(rtp_session->last_write_ts)(__extension__ ({ unsigned int __v, __x = (rtp_session->last_write_ts
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
1843 sr->pc = htonl(rtp_session->stats.outbound.packet_count)(__extension__ ({ unsigned int __v, __x = (rtp_session->stats
.outbound.packet_count); if (__builtin_constant_p (__x)) __v =
((((__x) & 0xff000000) >> 24) | (((__x) & 0x00ff0000
) >> 8) | (((__x) & 0x0000ff00) << 8) | (((__x
) & 0x000000ff) << 24)); else __asm__ ("bswap %0" :
"=r" (__v) : "0" (__x)); __v; }))
;
1844 sr->oc = htonl((rtp_session->stats.outbound.raw_bytes - rtp_session->stats.outbound.packet_count * sizeof(srtp_hdr_t)))(__extension__ ({ unsigned int __v, __x = ((rtp_session->stats
.outbound.raw_bytes - rtp_session->stats.outbound.packet_count
* sizeof(srtp_hdr_t))); if (__builtin_constant_p (__x)) __v =
((((__x) & 0xff000000) >> 24) | (((__x) & 0x00ff0000
) >> 8) | (((__x) & 0x0000ff00) << 8) | (((__x
) & 0x000000ff) << 24)); else __asm__ ("bswap %0" :
"=r" (__v) : "0" (__x)); __v; }))
;
1845
1846 switch_time_exp_gmt(&now_hr,now);
1847 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1847, (const char*)(session)
, SWITCH_LOG_DEBUG10,"Sending an RTCP packet[%04d-%02d-%02d %02d:%02d:%02d.%d] lsr[%u] msw[%u] lsw[%u] stats_ssrc[%u]\n",
1848 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,
1849 (ntohl(sr->ntp_lsw)(__extension__ ({ unsigned int __v, __x = (sr->ntp_lsw); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
&0xffff0000)>>16 | (ntohl(sr->ntp_msw)(__extension__ ({ unsigned int __v, __x = (sr->ntp_msw); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
&0x0000ffff)<<16,
1850 ntohl(sr->ntp_msw)(__extension__ ({ unsigned int __v, __x = (sr->ntp_msw); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
,ntohl(sr->ntp_lsw)(__extension__ ({ unsigned int __v, __x = (sr->ntp_lsw); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
, rtp_session->stats.rtcp.ssrc
1851 );
1852}
1853
1854
1855static void rtcp_generate_report_block(switch_rtp_t *rtp_session, struct switch_rtcp_report_block *rtcp_report_block){
1856#ifdef DEBUG_RTCP
1857 switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1858#endif
1859 switch_rtcp_numbers_t * stats=&rtp_session->stats.rtcp;
1860 switch_time_t now;
1861 uint32_t expected_pkt, dlsr;
1862 int32_t pkt_lost;
1863 uint32_t ntp_sec, ntp_usec, lsr_now, sec;
1864 now = switch_time_now();
1865 sec = (uint32_t)(now/1000000); /* convert to seconds */
1866 ntp_sec = sec+NTP_TIME_OFFSET2208988800UL; /* convert to NTP seconds */
1867 ntp_usec = (uint32_t)(now - (sec*1000000)); /* remove seconds to keep only the microseconds */
1868
1869 /* Packet loss */
1870 if (stats->rtcp_rtp_count == 0) {
1871 expected_pkt = stats->high_ext_seq_recv - stats->base_seq + 1;
1872 } else {
1873 expected_pkt = stats->high_ext_seq_recv - stats->last_rpt_ext_seq;
1874 }
1875
1876 pkt_lost = expected_pkt - stats->period_pkt_count;
1877 stats->cum_lost=stats->cum_lost+pkt_lost;
1878 if (expected_pkt > 0 && pkt_lost > 0) {
1879 rtcp_report_block->fraction = (uint8_t) (pkt_lost * 256 / expected_pkt);
1880 } else {
1881 rtcp_report_block->fraction = 0;
1882 }
1883#if SWITCH_BYTE_ORDER1234 != __BIG_ENDIAN4321
1884 /* Reversing byte order for 24bits */
1885 rtcp_report_block->lost = (((stats->cum_lost&0x0000FF)<<16) | ((stats->cum_lost&0x00FF00)) | ((stats->cum_lost&0xFF0000)>>16));
1886#endif
1887
1888#ifdef DEBUG_RTCP
1889 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1889, (const char*)(session)
, SWITCH_LOG_DEBUG, "rtcp_generate_sr: stats_ssrc[%u] received[%d] expected[%d] cum[%d]lost[%d|%d/256]pkt last_seq[%d]cyc[%d] last_rpt_seq[%d]cyc[%d] ssrc[%d]\n",
1890 ntohl(rtp_session->recv_msg.header.ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->recv_msg
.header.ssrc); if (__builtin_constant_p (__x)) __v = ((((__x)
& 0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
, stats->period_pkt_count, expected_pkt,
1891 stats->cum_lost, pkt_lost, rtcp_report_block->fraction, stats->high_ext_seq_recv&0x0000ffff,
1892 stats->cycle, stats->last_rpt_ext_seq&0x0000ffff, stats->last_rpt_cycle, rtp_session->stats.rtcp.peer_ssrc
1893 );
1894#endif
1895 rtcp_report_block->highest_sequence_number_received = htonl(stats->high_ext_seq_recv)(__extension__ ({ unsigned int __v, __x = (stats->high_ext_seq_recv
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
1896
1897 /* Jitter */
1898 rtcp_report_block->jitter = htonl((uint32_t)stats->inter_jitter)(__extension__ ({ unsigned int __v, __x = ((uint32_t)stats->
inter_jitter); if (__builtin_constant_p (__x)) __v = ((((__x)
& 0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
;
1899
1900 /* Delay since Last Sender Report (DLSR) : 32bits, 1/65536 seconds */
1901 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) */
1902
1903 if (stats->last_recv_lsr_local) {
1904 dlsr = lsr_now - stats->last_recv_lsr_local;
1905 } else {
1906 dlsr = 0;
1907 }
1908 rtcp_report_block->lsr = stats->last_recv_lsr_peer;
1909 rtcp_report_block->dlsr = htonl(dlsr)(__extension__ ({ unsigned int __v, __x = (dlsr); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
1910 rtcp_report_block->ssrc = htonl(rtp_session->stats.rtcp.peer_ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->stats
.rtcp.peer_ssrc); if (__builtin_constant_p (__x)) __v = ((((__x
) & 0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
;
1911 stats->rtcp_rtp_count++;
1912}
1913
1914static void rtcp_stats_init(switch_rtp_t *rtp_session)
1915{
1916 switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
1917 srtp_hdr_t * hdr = &rtp_session->recv_msg.header;
1918 switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1919 stats->ssrc = ntohl(hdr->ssrc)(__extension__ ({ unsigned int __v, __x = (hdr->ssrc); if (
__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000) >>
24) | (((__x) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00
) << 8) | (((__x) & 0x000000ff) << 24)); else
__asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
1920 stats->last_rpt_ts = rtp_session->timer.samplecount;
1921 stats->init = 1;
1922 stats->last_rpt_ext_seq = 0;
1923 stats->last_rpt_cycle = 0;
1924 stats->last_pkt_tsdiff = 0;
1925 stats->inter_jitter = 0;
1926 stats->cycle = 0;
1927 stats->high_ext_seq_recv = ntohs((uint16_t)hdr->seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t)hdr->seq); if (__builtin_constant_p (__x)
) __v = ((unsigned short int) ((((__x) >> 8) & 0xff
) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
1928 stats->base_seq = ntohs((uint16_t)hdr->seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t)hdr->seq); if (__builtin_constant_p (__x)
) __v = ((unsigned short int) ((((__x) >> 8) & 0xff
) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
1929 stats->bad_seq = (1<<16) + 1; /* Make sure we wont missmatch 2 consecutive packets, so seq == bad_seq is false */
1930 stats->cum_lost = 0;
1931 stats->period_pkt_count = 0;
1932 stats->pkt_count = 0;
1933 stats->rtcp_rtp_count = 0;
1934
1935 if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
1936 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1936, (const char*)(session)
, SWITCH_LOG_DEBUG, "rtcp_stats_init: rtcp disabled\n");
1937 } else if (!rtp_session->rtcp_sock_output) {
1938 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1938, (const char*)(session)
, SWITCH_LOG_WARNING, "rtcp_stats_init: no rtcp socket\n");
1939 } else if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
1940 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1940, (const char*)(session)
, SWITCH_LOG_DEBUG, "rtcp_stats_init: rtcp passthru\n");
1941 } else {
1942 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1942, (const char*)(session)
, SWITCH_LOG_DEBUG, "rtcp_stats_init: ssrc[%d] base_seq[%d]\n", stats->ssrc, stats->base_seq);
1943 }
1944}
1945
1946static int rtcp_stats(switch_rtp_t *rtp_session)
1947{
1948 switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1949 srtp_hdr_t * hdr = &rtp_session->recv_msg.header;
1950 switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
1951 uint32_t packet_spacing_diff, pkt_tsdiff, pkt_extended_seq;
1952 uint16_t pkt_seq, seq_diff, max_seq;
1953 const int MAX_DROPOUT = 3000;
1954 const int MAX_MISORDER = 100;
1955 const int RTP_SEQ_MOD = (1<<16);
1956
1957 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)
1958 return 0; /* do not process RTCP in current state */
1959
1960 pkt_seq = (uint16_t) ntohs((uint16_t) rtp_session->recv_msg.header.seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t) rtp_session->recv_msg.header.seq); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
1961
1962 /* Detect sequence number cycle change */
1963 max_seq = stats->high_ext_seq_recv&0x0000ffff;
1964 seq_diff = pkt_seq - max_seq;
1965
1966 if (seq_diff < MAX_DROPOUT) { /* in order, with permissible gap */
1967 if (pkt_seq < max_seq) {
1968 stats->cycle++;
1969 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1969, (const char*)(session)
, SWITCH_LOG_NOTICE, "rtcp_stats:[cycle change] pkt_seq[%d]cycle[%d] max_seq[%d] stats_ssrc[%u] local_ts[%u]\n",
1970 pkt_seq, stats->cycle, max_seq, stats->ssrc, rtp_session->timer.samplecount);
1971 }
1972 pkt_extended_seq = stats->cycle << 16 | pkt_seq; /* getting the extended packet extended sequence ID */
1973 if (pkt_extended_seq > stats->high_ext_seq_recv) {
1974 stats->high_ext_seq_recv = pkt_extended_seq;
1975 }
1976 }
1977 else if (seq_diff <= (RTP_SEQ_MOD - MAX_MISORDER)) { /* the sequence number made a very large jump */
1978 if (pkt_seq == stats->bad_seq) {
1979 rtcp_stats_init(rtp_session);
1980 } else {
1981 stats->bad_seq = (pkt_seq + 1) & (RTP_SEQ_MOD-1);
1982 }
1983 return 0; /* no stats, packet is out of sync and will be accounted as lost */
1984 } else {
1985 /* duplicate or reordered packet */
1986 }
1987
1988 /* Verify that we are on the same stream source (we do not support multiple sources) */
1989 if (ntohl(hdr->ssrc)(__extension__ ({ unsigned int __v, __x = (hdr->ssrc); if (
__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000) >>
24) | (((__x) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00
) << 8) | (((__x) & 0x000000ff) << 24)); else
__asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
!= stats->ssrc || !stats->init) {
1990 rtcp_stats_init(rtp_session);
1991 }
1992
1993 stats->period_pkt_count++;
1994 stats->pkt_count++;
1995#ifdef DEBUG_RTCP
1996 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 1996, (const char*)(session)
, SWITCH_LOG_DEBUG10, "rtcp_stats: period_pkt_count[%d]last_seq[%d]cycle[%d]stats_ssrc[%u]local_ts[%u]\n",
1997 stats->period_pkt_count, pkt_seq, stats->cycle, stats->ssrc, rtp_session->timer.samplecount);
1998#endif
1999 /* Interarrival jitter calculation */
2000 pkt_tsdiff = rtp_session->timer.samplecount - ntohl(hdr->ts)(__extension__ ({ unsigned int __v, __x = (hdr->ts); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
; /* relative transit times for this packet */
2001 if (stats->pkt_count < 2) { /* Can not compute Jitter with only one packet */
2002 stats->last_pkt_tsdiff = pkt_tsdiff;
2003 } else {
2004 packet_spacing_diff = pkt_tsdiff - stats->last_pkt_tsdiff; /* Jitter : difference of relative transit times for the two packets */
2005 stats->last_pkt_tsdiff = pkt_tsdiff;
2006 /* Interarrival jitter estimation, "J(i) = J(i-1) + ( |D(i-1,i)| - J(i-1) )/16" */
2007 stats->inter_jitter = (stats->inter_jitter + (((double)abs(packet_spacing_diff) - stats->inter_jitter) /16.));
2008 }
2009
2010#ifdef DEBUG_RTCP
2011 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2011, (const char*)(session)
, SWITCH_LOG_DEBUG10, "rtcp_stats: pkt_ts[%d]local_ts[%d]diff[%d]pkt_spacing[%d]inter_jitter[%f]seq[%d]stats_ssrc[%d]",
2012 ntohl(hdr->ts)(__extension__ ({ unsigned int __v, __x = (hdr->ts); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
, rtp_session->timer.samplecount, pkt_tsdiff, packet_spacing_diff, stats->inter_jitter, ntohs(hdr->seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) (hdr->seq); if (__builtin_constant_p (__x)) __v = ((
unsigned short int) ((((__x) >> 8) & 0xff) | (((__x
) & 0xff) << 8))); else __asm__ ("rorw $8, %w0" : "=r"
(__v) : "0" (__x) : "cc"); __v; }))
, stats->ssrc);
2013#endif
2014 return 1;
2015}
2016
2017
2018static int check_rtcp_and_ice(switch_rtp_t *rtp_session)
2019{
2020 int ret = 0;
2021 int rtcp_ok = 1;
2022 switch_time_t now = switch_micro_time_now();
2023
2024 if (rtp_session->fir_countdown) {
2025 //if (rtp_session->fir_countdown == FIR_COUNTDOWN) {
2026 // do_flush(rtp_session, SWITCH_TRUE);
2027 //}
2028
2029 if (rtp_session->fir_countdown == FIR_COUNTDOWN50 || (rtp_session->fir_countdown == FIR_COUNTDOWN50 / 2) || rtp_session->fir_countdown == 1) {
2030 if (rtp_session->flags[SWITCH_RTP_FLAG_PLI]) {
2031 send_pli(rtp_session);
2032 } else {
2033 send_fir(rtp_session);
2034 }
2035 }
2036
2037 rtp_session->fir_countdown--;
2038 }
2039
2040 if (rtp_session->flags[SWITCH_RTP_FLAG_AUTO_CNG] && rtp_session->send_msg.header.ts && rtp_session->cng_pt &&
2041 rtp_session->timer.samplecount >= (rtp_session->last_write_samplecount + (rtp_session->samples_per_interval * 60))) {
2042 uint8_t data[10] = { 0 };
2043 switch_frame_flag_t frame_flags = SFF_NONE;
2044 data[0] = 65;
2045 rtp_session->cn++;
2046
2047 get_next_write_ts(rtp_session, 0);
2048 rtp_session->send_msg.header.ts = htonl(rtp_session->ts)(__extension__ ({ unsigned int __v, __x = (rtp_session->ts
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
2049
2050 switch_rtp_write_manual(rtp_session, (void *) data, 2, 0, rtp_session->cng_pt, ntohl(rtp_session->send_msg.header.ts)(__extension__ ({ unsigned int __v, __x = (rtp_session->send_msg
.header.ts); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
, &frame_flags);
2051
2052 if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) {
2053 rtp_session->last_write_samplecount = rtp_session->timer.samplecount;
2054 }
2055 }
2056
2057 if (rtp_session->rtcp_interval && rtp_session->next_rtcp_send > now) {
2058 rtcp_ok = 0;
2059 } else {
2060 rtp_session->next_rtcp_send = now + (rtp_session->rtcp_interval * 1000);
2061 }
2062
2063 if (rtcp_ok && rtp_session->rtcp_ice.ice_user && !rtp_session->rtcp_ice.rready) {
2064 rtcp_ok = 0;
2065 }
2066
2067 if (rtp_session->rtcp_sock_output && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] &&
2068 !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] &&
2069 (rtp_session->timer.samplecount - rtp_session->stats.rtcp.last_rpt_ts >= rtp_session->samples_per_second * rtp_session->rtcp_send_rate) ) {
2070
2071 switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
2072 struct switch_rtcp_receiver_report *rr;
2073 struct switch_rtcp_sender_report *sr;
2074 struct switch_rtcp_report_block *rtcp_report_block;
2075
2076 switch_size_t rtcp_bytes = sizeof(struct switch_rtcp_hdr_s)+sizeof(uint32_t); /* add size of the packet header and the ssrc */
2077
2078 rtp_session->rtcp_send_msg.header.version = 2;
2079 rtp_session->rtcp_send_msg.header.p = 0;
2080 rtp_session->rtcp_send_msg.header.count = 1;
2081
2082 if (!rtp_session->stats.outbound.packet_count) {
2083 rtp_session->rtcp_send_msg.header.type = 201; /* Receiver report */
2084 rr=(struct switch_rtcp_receiver_report*) rtp_session->rtcp_send_msg.body;
2085 rr->ssrc = htonl(rtp_session->ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->ssrc
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
2086 rtcp_report_block = &rr->report_block;
2087 rtcp_bytes += sizeof(struct switch_rtcp_report_block);
2088 } else {
2089 struct switch_rtcp_sender_info *rtcp_sender_info;
2090 rtp_session->rtcp_send_msg.header.type = 200; /* Sender report */
2091 sr = (struct switch_rtcp_sender_report*) rtp_session->rtcp_send_msg.body;
2092 sr->ssrc = htonl(rtp_session->ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->ssrc
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
2093 rtcp_sender_info = &sr->sender_info;
2094 rtcp_generate_sender_info(rtp_session, rtcp_sender_info);
2095 rtcp_report_block = &sr->report_block;
2096 rtcp_bytes += sizeof(struct switch_rtcp_sender_info) + sizeof(struct switch_rtcp_report_block);
2097 }
2098 rtcp_generate_report_block(rtp_session, rtcp_report_block);
2099
2100 rtp_session->rtcp_send_msg.header.length = htons((uint16_t)(rtcp_bytes / 4) - 1)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t)(rtcp_bytes / 4) - 1); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
2101
2102 /* Prepare next report */
2103 stats->last_rpt_cycle = stats->cycle;
2104 stats->last_rpt_ext_seq = stats->high_ext_seq_recv;
2105 stats->last_rpt_ts = rtp_session->timer.samplecount;
2106 stats->period_pkt_count = 0;
2107 rtp_session->rtcp_send_msg.header.length = htons((u_short)(rtcp_bytes / 4) - 1)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((u_short)(rtcp_bytes / 4) - 1); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
2108
2109
2110#ifdef ENABLE_SRTP1
2111 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
2112 int sbytes = (int) rtcp_bytes;
2113 int stat = srtp_protect_rtcp(rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_send_msg.header, &sbytes);
2114
2115 if (stat) {
2116 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2116, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
2117 goto end;
2118 } else {
2119 rtcp_bytes = sbytes;
2120 }
2121
2122 }
2123#endif
2124
2125#ifdef ENABLE_ZRTP
2126 /* ZRTP Send */
2127 if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
2128 unsigned int sbytes = (int) rtcp_bytes;
2129 zrtp_status_t stat = zrtp_status_fail;
2130
2131 stat = zrtp_process_rtcp(rtp_session->zrtp_stream, (void *) &rtp_session->rtcp_send_msg, &sbytes);
2132
2133 switch (stat) {
2134 case zrtp_status_ok:
2135 break;
2136 case zrtp_status_drop:
2137 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 2137, ((void*)0)
, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
2138 ret = (int)rtcp_bytes;
2139 goto end;
2140 break;
2141 case zrtp_status_fail:
2142 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 2142, ((void*)0)
, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
2143 break;
2144 default:
2145 break;
2146 }
2147
2148 rtcp_bytes = sbytes;
2149 }
2150#endif
2151
2152#ifdef DEBUG_EXTRA
2153 {
2154 const char *old_host;
2155 char bufb[30];
2156 old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr);
2157 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2157, (const char*)(rtp_session->session)
, SWITCH_LOG_CRIT, "%s SEND %s RTCP %s:%d %ld\n",
2158 rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
,
2159 rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio",
2160 old_host,
2161 switch_sockaddr_get_port(rtp_session->rtcp_remote_addr),
2162 rtcp_bytes);
2163 }
2164#endif
2165 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) {
2166 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2166, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG,"RTCP packet not written\n");
2167 } else {
2168 rtp_session->stats.inbound.period_packet_count = 0;
2169 }
2170 }
2171
2172 if (rtp_session->ice.ice_user) {
2173 if (ice_out(rtp_session, &rtp_session->ice) == SWITCH_STATUS_GENERR) {
2174 ret = -1;
2175 goto end;
2176 }
2177 }
2178
2179 if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2180 if (rtp_session->rtcp_ice.ice_user) {
2181 if (ice_out(rtp_session, &rtp_session->rtcp_ice) == SWITCH_STATUS_GENERR) {
2182 ret = -1;
2183 goto end;
2184 }
2185 }
2186 }
2187
2188 end:
2189
2190 return ret;
2191}
2192
2193SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_ping(switch_rtp_t *rtp_session)
2194{
2195 check_rtcp_and_ice(rtp_session);
2196}
2197
2198SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_get_random(void *buf, uint32_t len)
2199{
2200#ifdef ENABLE_SRTP1
2201 crypto_get_random(buf, len);
2202#else
2203 switch_stun_random_string(buf, len, NULL((void*)0));
2204#endif
2205}
2206
2207
2208SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_shutdown(void)
2209{
2210 switch_core_port_allocator_t *alloc = NULL((void*)0);
2211 switch_hash_index_t *hi;
2212 const void *var;
2213 void *val;
2214
2215 if (!global_init) {
2216 return;
2217 }
2218
2219 switch_mutex_lock(port_lock);
2220
2221 for (hi = switch_core_hash_first(alloc_hash)switch_core_hash_first_iter(alloc_hash, ((void*)0)); hi; hi = switch_core_hash_next(&hi)) {
2222 switch_core_hash_this(hi, &var, NULL((void*)0), &val);
2223 if ((alloc = (switch_core_port_allocator_t *) val)) {
2224 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 2224, ((void*)0)
, SWITCH_LOG_DEBUG, "Destroy port allocator for %s\n", (char *) var);
2225 switch_core_port_allocator_destroy(&alloc);
2226 }
2227 }
2228
2229 switch_core_hash_destroy(&alloc_hash);
2230 switch_mutex_unlock(port_lock);
2231
2232#ifdef ENABLE_ZRTP
2233 if (zrtp_on) {
2234 zrtp_status_t status = zrtp_status_ok;
2235
2236 status = zrtp_def_cache_store(zrtp_global);
2237 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 2237, ((void*)0)
, SWITCH_LOG_DEBUG, "Saving ZRTP cache: %s\n", zrtp_status_ok == status ? "OK" : "FAIL");
2238 zrtp_down(zrtp_global);
2239 }
2240#endif
2241#ifdef ENABLE_SRTP1
2242 crypto_kernel_shutdown();
2243#endif
2244
2245}
2246
2247SWITCH_DECLARE(switch_port_t)__attribute__((visibility("default"))) switch_port_t switch_rtp_set_start_port(switch_port_t port)
2248{
2249 if (port) {
2250 if (port_lock) {
2251 switch_mutex_lock(port_lock);
2252 }
2253 START_PORT = port;
2254 if (port_lock) {
2255 switch_mutex_unlock(port_lock);
2256 }
2257 }
2258 return START_PORT;
2259}
2260
2261SWITCH_DECLARE(switch_port_t)__attribute__((visibility("default"))) switch_port_t switch_rtp_set_end_port(switch_port_t port)
2262{
2263 if (port) {
2264 if (port_lock) {
2265 switch_mutex_lock(port_lock);
2266 }
2267 END_PORT = port;
2268 if (port_lock) {
2269 switch_mutex_unlock(port_lock);
2270 }
2271 }
2272 return END_PORT;
2273}
2274
2275SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_release_port(const char *ip, switch_port_t port)
2276{
2277 switch_core_port_allocator_t *alloc = NULL((void*)0);
2278
2279 if (!ip || !port) {
2280 return;
2281 }
2282
2283 switch_mutex_lock(port_lock);
2284 if ((alloc = switch_core_hash_find(alloc_hash, ip))) {
2285 switch_core_port_allocator_free_port(alloc, port);
2286 }
2287 switch_mutex_unlock(port_lock);
2288
2289}
2290
2291SWITCH_DECLARE(switch_port_t)__attribute__((visibility("default"))) switch_port_t switch_rtp_request_port(const char *ip)
2292{
2293 switch_port_t port = 0;
2294 switch_core_port_allocator_t *alloc = NULL((void*)0);
2295
2296 switch_mutex_lock(port_lock);
2297 alloc = switch_core_hash_find(alloc_hash, ip);
2298 if (!alloc) {
2299 if (switch_core_port_allocator_new(ip, START_PORT, END_PORT, SPF_EVEN, &alloc) != SWITCH_STATUS_SUCCESS) {
2300 abort();
2301 }
2302
2303 switch_core_hash_insert(alloc_hash, ip, alloc)switch_core_hash_insert_destructor(alloc_hash, ip, alloc, ((void
*)0))
;
2304 }
2305
2306 if (switch_core_port_allocator_request_port(alloc, &port) != SWITCH_STATUS_SUCCESS) {
2307 port = 0;
2308 }
2309
2310 switch_mutex_unlock(port_lock);
2311 return port;
2312}
2313
2314SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_set_payload_map(switch_rtp_t *rtp_session, payload_map_t **pmap)
2315{
2316
2317 if (rtp_session) {
2318 switch_mutex_lock(rtp_session->flag_mutex);
2319 rtp_session->pmaps = pmap;
2320 switch_mutex_unlock(rtp_session->flag_mutex);
2321 return SWITCH_STATUS_SUCCESS;
2322 }
2323
2324 return SWITCH_STATUS_FALSE;
2325}
2326
2327SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_intentional_bugs(switch_rtp_t *rtp_session, switch_rtp_bug_flag_t bugs)
2328{
2329 rtp_session->rtp_bugs = bugs;
2330
2331 if ((rtp_session->rtp_bugs & RTP_BUG_START_SEQ_AT_ZERO)) {
2332 rtp_session->seq = 0;
2333 }
2334
2335}
2336
2337
2338static switch_status_t enable_remote_rtcp_socket(switch_rtp_t *rtp_session, const char **err) {
2339
2340 switch_status_t status = SWITCH_STATUS_SUCCESS;
2341
2342 if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2343
2344 if (switch_sockaddr_info_get(&rtp_session->rtcp_remote_addr, rtp_session->eff_remote_host_str, SWITCH_UNSPEC0,
2345 rtp_session->remote_rtcp_port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !rtp_session->rtcp_remote_addr) {
2346 *err = "RTCP Remote Address Error!";
2347 return SWITCH_STATUS_FALSE;
2348 } else {
2349 const char *host;
2350 char bufa[30];
2351
2352 host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_remote_addr);
2353
2354 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2354, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG,
2355 "Setting RTCP remote addr to %s:%d\n", host, rtp_session->remote_rtcp_port);
2356 }
2357
2358 if (rtp_session->rtcp_sock_input && switch_sockaddr_get_family(rtp_session->rtcp_remote_addr) ==
2359 switch_sockaddr_get_family(rtp_session->rtcp_local_addr)) {
2360 rtp_session->rtcp_sock_output = rtp_session->rtcp_sock_input;
2361 } else {
2362
2363 if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) {
2364 switch_socket_close(rtp_session->rtcp_sock_output);
2365 }
2366
2367 if ((status = switch_socket_create(&rtp_session->rtcp_sock_output,
2368 switch_sockaddr_get_family(rtp_session->rtcp_remote_addr),
2369 SOCK_DGRAMSOCK_DGRAM, 0, rtp_session->pool)) != SWITCH_STATUS_SUCCESS) {
2370 *err = "RTCP Socket Error!";
2371 }
2372 }
2373
2374 } else {
2375 *err = "RTCP NOT ACTIVE!";
2376 }
2377
2378 return status;
2379
2380}
2381
2382static switch_status_t enable_local_rtcp_socket(switch_rtp_t *rtp_session, const char **err) {
2383
2384 const char *host = rtp_session->local_host_str;
2385 switch_port_t port = rtp_session->local_port;
2386 switch_socket_t *rtcp_new_sock = NULL((void*)0), *rtcp_old_sock = NULL((void*)0);
2387 switch_status_t status = SWITCH_STATUS_SUCCESS;
2388 char bufa[30];
2389
2390 if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2391 if (switch_sockaddr_info_get(&rtp_session->rtcp_local_addr, host, SWITCH_UNSPEC0, port+1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2392 *err = "RTCP Local Address Error!";
2393 goto done;
2394 }
2395
2396 if (switch_socket_create(&rtcp_new_sock, switch_sockaddr_get_family(rtp_session->rtcp_local_addr), SOCK_DGRAMSOCK_DGRAM, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2397 *err = "RTCP Socket Error!";
2398 goto done;
2399 }
2400
2401 if (switch_socket_opt_set(rtcp_new_sock, SWITCH_SO_REUSEADDR16, 1) != SWITCH_STATUS_SUCCESS) {
2402 *err = "RTCP Socket Error!";
2403 goto done;
2404 }
2405
2406 if (switch_socket_bind(rtcp_new_sock, rtp_session->rtcp_local_addr) != SWITCH_STATUS_SUCCESS) {
2407 *err = "RTCP Bind Error!";
2408 goto done;
2409 }
2410
2411 if (switch_sockaddr_info_get(&rtp_session->rtcp_from_addr, switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr),
2412 SWITCH_UNSPEC0, switch_sockaddr_get_port(rtp_session->from_addr) + 1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2413 *err = "RTCP From Address Error!";
2414 goto done;
2415 }
2416
2417 rtcp_old_sock = rtp_session->rtcp_sock_input;
2418 rtp_session->rtcp_sock_input = rtcp_new_sock;
2419 rtcp_new_sock = NULL((void*)0);
2420
2421 switch_socket_create_pollset(&rtp_session->rtcp_read_pollfd, rtp_session->rtcp_sock_input, SWITCH_POLLIN0x001 | SWITCH_POLLERR0x010, rtp_session->pool);
2422
2423 done:
2424
2425 if (*err) {
2426
2427 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2427, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error allocating rtcp [%s]\n", *err);
2428 status = SWITCH_STATUS_FALSE;
2429 }
2430
2431 if (rtcp_new_sock) {
2432 switch_socket_close(rtcp_new_sock);
2433 }
2434
2435 if (rtcp_old_sock) {
2436 switch_socket_close(rtcp_old_sock);
2437 }
2438 } else {
2439 status = SWITCH_STATUS_FALSE;
2440 }
2441
2442 return status;
2443}
2444
2445SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_set_local_address(switch_rtp_t *rtp_session, const char *host, switch_port_t port, const char **err)
2446{
2447 switch_socket_t *new_sock = NULL((void*)0), *old_sock = NULL((void*)0);
2448 switch_status_t status = SWITCH_STATUS_FALSE;
2449 int j = 0;
2450#ifndef WIN32
2451 char o[5] = "TEST", i[5] = "";
2452 switch_size_t len, ilen = 0;
2453 int x;
2454#endif
2455
2456 if (rtp_session->ready != 1) {
2457 if (!switch_rtp_ready(rtp_session)) {
2458 return SWITCH_STATUS_FALSE;
2459 }
2460
2461 WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session->
writing++
;
2462 READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session->
reading++
;
2463
2464 if (!switch_rtp_ready(rtp_session)) {
2465 goto done;
2466 }
2467 }
2468
2469
2470 *err = NULL((void*)0);
2471
2472 if (zstr(host)_zstr(host) || !port) {
2473 *err = "Address Error";
2474 goto done;
2475 }
2476
2477
2478 rtp_session->local_host_str = switch_core_strdup(rtp_session->pool, host)switch_core_perform_strdup(rtp_session->pool, host, "src/switch_rtp.c"
, (const char *)__func__, 2478)
;
2479 rtp_session->local_port = port;
2480
2481
2482 if (switch_sockaddr_info_get(&rtp_session->local_addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2483 *err = "Local Address Error!";
2484 goto done;
2485 }
2486
2487
2488 if (rtp_session->sock_input) {
2489 switch_rtp_kill_socket(rtp_session);
2490 }
2491
2492 if (switch_socket_create(&new_sock, switch_sockaddr_get_family(rtp_session->local_addr), SOCK_DGRAMSOCK_DGRAM, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2493 *err = "Socket Error!";
2494 goto done;
2495 }
2496
2497 if (switch_socket_opt_set(new_sock, SWITCH_SO_REUSEADDR16, 1) != SWITCH_STATUS_SUCCESS) {
2498 *err = "Socket Error!";
2499 goto done;
2500 }
2501
2502 if (switch_socket_bind(new_sock, rtp_session->local_addr) != SWITCH_STATUS_SUCCESS) {
2503 char *em = switch_core_sprintf(rtp_session->pool, "Bind Error! %s:%d", host, port);
2504 *err = em;
2505 goto done;
2506 }
2507
2508
2509 if ((j = atoi(host)) && j > 223 && j < 240) { /* mcast */
2510 if (switch_mcast_interface(new_sock, rtp_session->local_addr) != SWITCH_STATUS_SUCCESS) {
2511 *err = "Multicast Socket interface Error";
2512 goto done;
2513 }
2514
2515 if (switch_mcast_join(new_sock, rtp_session->local_addr, NULL((void*)0), NULL((void*)0)) != SWITCH_STATUS_SUCCESS) {
2516 *err = "Multicast Error";
2517 goto done;
2518 }
2519
2520 if (rtp_session->session) {
2521 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
2522 const char *var;
2523
2524 if ((var = switch_channel_get_variable(channel, "multicast_ttl")switch_channel_get_variable_dup(channel, "multicast_ttl", SWITCH_TRUE
, -1)
)) {
2525 int ttl = atoi(var);
2526
2527 if (ttl > 0 && ttl < 256) {
2528 if (switch_mcast_hops(new_sock, (uint8_t) ttl) != SWITCH_STATUS_SUCCESS) {
2529 *err = "Mutlicast TTL set failed";
2530 goto done;
2531 }
2532
2533 }
2534 }
2535
2536 }
2537
2538 }
2539
2540
2541
2542#ifndef WIN32
2543 len = sizeof(i);
2544 switch_socket_opt_set(new_sock, SWITCH_SO_NONBLOCK8, TRUE(!0));
2545
2546 switch_socket_sendto(new_sock, rtp_session->local_addr, 0, (void *) o, &len);
2547
2548 x = 0;
2549 while (!ilen) {
2550 switch_status_t status;
2551 ilen = len;
2552 status = switch_socket_recvfrom(rtp_session->from_addr, new_sock, 0, (void *) i, &ilen);
2553
2554 if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) {
2555 break;
2556 }
2557
2558 if (++x > 1000) {
2559 break;
2560 }
2561 switch_cond_next();
2562 }
2563 switch_socket_opt_set(new_sock, SWITCH_SO_NONBLOCK8, FALSE0);
2564
2565#endif
2566
2567 old_sock = rtp_session->sock_input;
2568 rtp_session->sock_input = new_sock;
2569 new_sock = NULL((void*)0);
2570
2571 if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] || rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] || rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
2572 switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, TRUE(!0));
2573 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
2574 }
2575
2576 switch_socket_create_pollset(&rtp_session->read_pollfd, rtp_session->sock_input, SWITCH_POLLIN0x001 | SWITCH_POLLERR0x010, rtp_session->pool);
2577
2578 if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2579 if ((status = enable_local_rtcp_socket(rtp_session, err)) == SWITCH_STATUS_SUCCESS) {
2580 *err = "Success";
2581 }
2582 } else {
2583 status = SWITCH_STATUS_SUCCESS;
2584 *err = "Success";
2585 }
2586
2587 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_IO);
2588
2589 done:
2590
2591 if (new_sock) {
2592 switch_socket_close(new_sock);
2593 }
2594
2595 if (old_sock) {
2596 switch_socket_close(old_sock);
2597 }
2598
2599
2600 if (rtp_session->ready != 1) {
2601 WRITE_DEC(rtp_session)switch_mutex_unlock(rtp_session->write_mutex); rtp_session
->writing--
;
2602 READ_DEC(rtp_session)switch_mutex_unlock(rtp_session->read_mutex); rtp_session->
reading--
;
2603 }
2604
2605 return status;
2606}
2607
2608SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_max_missed_packets(switch_rtp_t *rtp_session, uint32_t max)
2609{
2610 if (rtp_session->missed_count >= max) {
2611
2612 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2612, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING,
2613 "new max missed packets(%d->%d) greater than current missed packets(%d). RTP will timeout.\n",
2614 rtp_session->missed_count, max, rtp_session->missed_count);
2615 }
2616
2617 rtp_session->max_missed_packets = max;
2618}
2619
2620SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_reset(switch_rtp_t *rtp_session)
2621{
2622 if (!rtp_session) {
2623 return;
2624 }
2625
2626 rtp_session->seq = (uint16_t) rand();
2627 rtp_session->ts = 0;
2628 memset(&rtp_session->ts_norm, 0, sizeof(rtp_session->ts_norm));
2629
2630}
2631
2632SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_reset_media_timer(switch_rtp_t *rtp_session)
2633{
2634 rtp_session->missed_count = 0;
2635}
2636
2637SWITCH_DECLARE(char *)__attribute__((visibility("default"))) char * switch_rtp_get_remote_host(switch_rtp_t *rtp_session)
2638{
2639 return zstr(rtp_session->remote_host_str)_zstr(rtp_session->remote_host_str) ? "0.0.0.0" : rtp_session->remote_host_str;
2640}
2641
2642SWITCH_DECLARE(switch_port_t)__attribute__((visibility("default"))) switch_port_t switch_rtp_get_remote_port(switch_rtp_t *rtp_session)
2643{
2644 return rtp_session->remote_port;
2645}
2646
2647static void ping_socket(switch_rtp_t *rtp_session)
2648{
2649 uint32_t o = UINT_MAX(2147483647 *2U +1U);
2650 switch_size_t len = sizeof(o);
2651 switch_socket_sendto(rtp_session->sock_input, rtp_session->local_addr, 0, (void *) &o, &len);
2652
2653 if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && rtp_session->rtcp_sock_input) {
2654 switch_socket_sendto(rtp_session->rtcp_sock_input, rtp_session->rtcp_local_addr, 0, (void *) &o, &len);
2655 }
2656}
2657
2658SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_udptl_mode(switch_rtp_t *rtp_session)
2659{
2660 switch_socket_t *sock;
2661
2662 if (!switch_rtp_ready(rtp_session)) {
2663 return SWITCH_STATUS_FALSE;
2664 }
2665
2666 if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_PROXY_MEDIA)) {
2667 ping_socket(rtp_session);
2668 }
2669
2670 READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session->
reading++
;
2671 WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session->
writing++
;
2672
2673 if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] || rtp_session->timer.timer_interface) {
2674 switch_core_timer_destroy(&rtp_session->timer);
2675 memset(&rtp_session->timer, 0, sizeof(rtp_session->timer));
2676 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER);
2677 }
2678
2679 rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] = 0;
2680
2681 if (rtp_session->rtcp_sock_input) {
2682 ping_socket(rtp_session);
2683 switch_socket_shutdown(rtp_session->rtcp_sock_input, SWITCH_SHUTDOWN_READWRITE);
2684 }
2685
2686 if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) {
2687 switch_socket_shutdown(rtp_session->rtcp_sock_output, SWITCH_SHUTDOWN_READWRITE);
2688 }
2689
2690 if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2691 rtp_session->rtcp_sock_input = NULL((void*)0);
2692 rtp_session->rtcp_sock_output = NULL((void*)0);
2693 } else {
2694 if ((sock = rtp_session->rtcp_sock_input)) {
2695 rtp_session->rtcp_sock_input = NULL((void*)0);
2696 switch_socket_close(sock);
2697
2698 if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != sock) {
2699 if ((sock = rtp_session->rtcp_sock_output)) {
2700 rtp_session->rtcp_sock_output = NULL((void*)0);
2701 switch_socket_close(sock);
2702 }
2703 }
2704 }
2705 }
2706
2707 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_UDPTL);
2708 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_PROXY_MEDIA);
2709 switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, FALSE0);
2710 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
2711
2712 WRITE_DEC(rtp_session)switch_mutex_unlock(rtp_session->write_mutex); rtp_session
->writing--
;
2713 READ_DEC(rtp_session)switch_mutex_unlock(rtp_session->read_mutex); rtp_session->
reading--
;
2714
2715 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_STICKY_FLUSH);
2716 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
2717
2718 switch_rtp_break(rtp_session);
2719
2720 return SWITCH_STATUS_SUCCESS;
2721
2722}
2723
2724
2725SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) 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,
2726 switch_bool_t change_adv_addr, const char **err)
2727{
2728 switch_sockaddr_t *remote_addr;
2729 switch_status_t status = SWITCH_STATUS_SUCCESS;
2730 *err = "Success";
2731
2732 if (switch_sockaddr_info_get(&remote_addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !remote_addr) {
2733 *err = "Remote Address Error!";
2734 return SWITCH_STATUS_FALSE;
2735 }
2736
2737
2738 switch_mutex_lock(rtp_session->write_mutex);
2739
2740 rtp_session->remote_addr = remote_addr;
2741
2742 if (change_adv_addr) {
2743 rtp_session->remote_host_str = switch_core_strdup(rtp_session->pool, host)switch_core_perform_strdup(rtp_session->pool, host, "src/switch_rtp.c"
, (const char *)__func__, 2743)
;
2744 rtp_session->remote_port = port;
2745 }
2746
2747 rtp_session->eff_remote_host_str = switch_core_strdup(rtp_session->pool, host)switch_core_perform_strdup(rtp_session->pool, host, "src/switch_rtp.c"
, (const char *)__func__, 2747)
;
2748 rtp_session->eff_remote_port = port;
2749
2750 if (rtp_session->sock_input && switch_sockaddr_get_family(rtp_session->remote_addr) == switch_sockaddr_get_family(rtp_session->local_addr)) {
2751 rtp_session->sock_output = rtp_session->sock_input;
2752 } else {
2753 if (rtp_session->sock_output && rtp_session->sock_output != rtp_session->sock_input) {
2754 switch_socket_close(rtp_session->sock_output);
2755 }
2756 if ((status = switch_socket_create(&rtp_session->sock_output,
2757 switch_sockaddr_get_family(rtp_session->remote_addr),
2758 SOCK_DGRAMSOCK_DGRAM, 0, rtp_session->pool)) != SWITCH_STATUS_SUCCESS) {
2759 *err = "Socket Error!";
2760 }
2761 }
2762
2763
2764 if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2765 if (remote_rtcp_port) {
2766 rtp_session->remote_rtcp_port = remote_rtcp_port;
2767 } else {
2768 rtp_session->remote_rtcp_port = rtp_session->eff_remote_port + 1;
2769 }
2770 status = enable_remote_rtcp_socket(rtp_session, err);
2771 }
2772
2773 if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2774 rtp_session->rtcp_remote_addr = rtp_session->remote_addr;
2775 }
2776
2777 switch_mutex_unlock(rtp_session->write_mutex);
2778
2779 return status;
2780}
2781
2782
2783static const char *dtls_state_names_t[] = {"HANDSHAKE", "SETUP", "READY", "FAIL", "INVALID"};
2784static const char *dtls_state_names(dtls_state_t s)
2785{
2786 if (s > DS_INVALID) {
2787 s = DS_INVALID;
2788 }
2789
2790 return dtls_state_names_t[s];
2791}
2792
2793
2794#define dtls_set_state(_dtls, _state)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c"
, (const char *)__func__, 2794, (const char*)(rtp_session->
session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n"
, rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"
, dtls_state_names(_dtls->state), dtls_state_names(_state)
); _dtls->new_state = 1; _dtls->last_state = _dtls->
state; _dtls->state = _state
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2794, (const char*)(rtp_session->session)
, SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", dtls_state_names(_dtls->state), dtls_state_names(_state)); _dtls->new_state = 1; _dtls->last_state = _dtls->state; _dtls->state = _state
2795
2796#define cr_keylen16 16
2797#define cr_saltlen14 14
2798#define cr_kslen30 30
2799
2800static int dtls_state_setup(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
2801{
2802 X509 *cert;
2803 int r = 0;
2804
2805 if ((dtls->type & DTLS_TYPE_SERVER)) {
2806 r = 1;
2807 } else if ((cert = SSL_get_peer_certificate(dtls->ssl))) {
2808 switch_core_cert_extract_fingerprint(cert, dtls->remote_fp);
2809 r = switch_core_cert_verify(dtls->remote_fp);
2810 X509_free(cert);
2811 }
2812
2813 if (!r) {
2814 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2814, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "%s Fingerprint Verification Failed!\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio");
2815 dtls_set_state(dtls, DS_FAIL)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c"
, (const char *)__func__, 2815, (const char*)(rtp_session->
session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n"
, rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"
, dtls_state_names(dtls->state), dtls_state_names(DS_FAIL)
); dtls->new_state = 1; dtls->last_state = dtls->state
; dtls->state = DS_FAIL
;
2816 return -1;
2817 } else {
2818 uint8_t raw_key_data[cr_kslen30*2] = { 0 };
2819 unsigned char *local_key, *remote_key, *local_salt, *remote_salt;
2820 unsigned char local_key_buf[cr_kslen30] = {0}, remote_key_buf[cr_kslen30] = {0};
2821
2822 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2822, (const char*)(rtp_session->session)
, SWITCH_LOG_INFO, "%s Fingerprint Verified.\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio");
2823
2824#ifdef HAVE_OPENSSL_DTLS_SRTP1
2825 if (!SSL_export_keying_material(dtls->ssl, raw_key_data, sizeof(raw_key_data), "EXTRACTOR-dtls_srtp", 19, NULL((void*)0), 0, 0)) {
2826 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2826, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "%s Key material export failure\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio");
2827 dtls_set_state(dtls, DS_FAIL)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c"
, (const char *)__func__, 2827, (const char*)(rtp_session->
session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n"
, rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"
, dtls_state_names(dtls->state), dtls_state_names(DS_FAIL)
); dtls->new_state = 1; dtls->last_state = dtls->state
; dtls->state = DS_FAIL
;
2828 return -1;
2829 }
2830#else
2831 return -1;
2832#endif
2833
2834 if ((dtls->type & DTLS_TYPE_CLIENT)) {
2835 local_key = raw_key_data;
2836 remote_key = local_key + cr_keylen16;
2837 local_salt = remote_key + cr_keylen16;
2838 remote_salt = local_salt + cr_saltlen14;
2839
2840 } else {
2841 remote_key = raw_key_data;
2842 local_key = remote_key + cr_keylen16;
2843 remote_salt = local_key + cr_keylen16;
2844 local_salt = remote_salt + cr_saltlen14;
2845 }
2846
2847 memcpy(local_key_buf, local_key, cr_keylen16);
2848 memcpy(local_key_buf + cr_keylen16, local_salt, cr_saltlen14);
2849
2850 memcpy(remote_key_buf, remote_key, cr_keylen16);
2851 memcpy(remote_key_buf + cr_keylen16, remote_salt, cr_saltlen14);
2852
2853 if (dtls == rtp_session->rtcp_dtls && rtp_session->rtcp_dtls != rtp_session->dtls) {
2854 switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_SEND_RTCP, 0, AES_CM_128_HMAC_SHA1_80, local_key_buf, cr_kslen30);
2855 switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_RECV_RTCP, 0, AES_CM_128_HMAC_SHA1_80, remote_key_buf, cr_kslen30);
2856 } else {
2857 switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_SEND, 0, AES_CM_128_HMAC_SHA1_80, local_key_buf, cr_kslen30);
2858 switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_RECV, 0, AES_CM_128_HMAC_SHA1_80, remote_key_buf, cr_kslen30);
2859 }
2860 }
2861
2862 dtls_set_state(dtls, DS_READY)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c"
, (const char *)__func__, 2862, (const char*)(rtp_session->
session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n"
, rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"
, dtls_state_names(dtls->state), dtls_state_names(DS_READY
)); dtls->new_state = 1; dtls->last_state = dtls->state
; dtls->state = DS_READY
;
2863
2864 return 0;
2865}
2866
2867static int dtls_state_ready(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
2868{
2869
2870 if (dtls->new_state) {
2871 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
2872 switch_core_session_t *other_session;
2873 rtp_session->fir_countdown = FIR_COUNTDOWN50;
2874
2875 if (rtp_session->session && switch_core_session_get_partner(rtp_session->session, &other_session)switch_core_session_perform_get_partner(rtp_session->session
, &other_session, "src/switch_rtp.c", (const char *)__func__
, 2875)
== SWITCH_STATUS_SUCCESS) {
2876 switch_core_session_refresh_video(other_session);
2877 switch_core_session_rwunlock(other_session);
2878 }
2879 }
2880 dtls->new_state = 0;
2881 }
2882 return 0;
2883}
2884
2885static int dtls_state_fail(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
2886{
2887 if (rtp_session->session) {
2888 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
2889 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER)switch_channel_perform_hangup(channel, "src/switch_rtp.c", (const
char *)__func__, 2889, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
)
;
2890 }
2891
2892 return -1;
2893}
2894
2895
2896static int dtls_state_handshake(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
2897{
2898 int ret;
2899
2900 if ((ret = SSL_do_handshake(dtls->ssl)) != 1){
2901 switch((ret = SSL_get_error(dtls->ssl, ret))){
2902 case SSL_ERROR_WANT_READ2:
2903 case SSL_ERROR_WANT_WRITE3:
2904 case SSL_ERROR_NONE0:
2905 break;
2906 default:
2907 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2907, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "%s Handshake failure %d\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", ret);
2908 dtls_set_state(dtls, DS_FAIL)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c"
, (const char *)__func__, 2908, (const char*)(rtp_session->
session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n"
, rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"
, dtls_state_names(dtls->state), dtls_state_names(DS_FAIL)
); dtls->new_state = 1; dtls->last_state = dtls->state
; dtls->state = DS_FAIL
;
2909 return -1;
2910 }
2911 }
2912
2913 if (SSL_is_init_finished(dtls->ssl)(SSL_state(dtls->ssl) == 0x03)) {
2914 dtls_set_state(dtls, DS_SETUP)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c"
, (const char *)__func__, 2914, (const char*)(rtp_session->
session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n"
, rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"
, dtls_state_names(dtls->state), dtls_state_names(DS_SETUP
)); dtls->new_state = 1; dtls->last_state = dtls->state
; dtls->state = DS_SETUP
;
2915 }
2916
2917 return 0;
2918}
2919
2920static void free_dtls(switch_dtls_t **dtlsp)
2921{
2922 switch_dtls_t *dtls;
2923
2924 if (!dtlsp) {
2925 return;
2926 }
2927
2928 dtls = *dtlsp;
2929 *dtlsp = NULL((void*)0);
2930
2931 if (dtls->ssl) {
2932 SSL_free(dtls->ssl);
2933 }
2934
2935 if (dtls->ssl_ctx) {
2936 SSL_CTX_free(dtls->ssl_ctx);
2937 }
2938}
2939
2940static int do_dtls(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
2941{
2942 int r = 0, ret = 0, len;
2943 switch_size_t bytes;
2944 unsigned char buf[4096] = "";
2945 int ready = rtp_session->ice.ice_user ? (rtp_session->ice.rready && rtp_session->ice.ready) : 1;
2946
2947
2948 if (!dtls->bytes && !ready) {
2949 return 0;
2950 }
2951
2952 if ((ret = BIO_write(dtls->read_bio, dtls->data, (int)dtls->bytes)) != (int)dtls->bytes && dtls->bytes > 0) {
2953 ret = SSL_get_error(dtls->ssl, ret);
2954 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2954, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "%s DTLS packet read err %d\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", ret);
2955 }
2956
2957 r = dtls_states[dtls->state](rtp_session, dtls);
2958
2959 if ((len = BIO_read(dtls->write_bio, buf, sizeof(buf))) > 0) {
2960 bytes = len;
2961
2962 if (switch_socket_sendto(dtls->sock_output, dtls->remote_addr, 0, (void *)buf, &bytes ) != SWITCH_STATUS_SUCCESS) {
2963 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2963, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "%s DTLS packet not written\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio");
2964 }
2965 }
2966
2967
2968
2969 return r;
2970}
2971
2972#if VERIFY
2973static int cb_verify_peer(int preverify_ok, X509_STORE_CTX *ctx)
2974{
2975 SSL *ssl = NULL((void*)0);
2976 switch_dtls_t *dtls;
2977 X509 *cert;
2978 int r = 0;
2979
2980 ssl = X509_STORE_CTX_get_app_data(ctx)X509_STORE_CTX_get_ex_data(ctx,0);
2981 dtls = (switch_dtls_t *) SSL_get_app_data(ssl)(SSL_get_ex_data(ssl,0));
2982
2983 if (!(ssl && dtls)) {
2984 return 0;
2985 }
2986
2987 if ((cert = SSL_get_peer_certificate(dtls->ssl))) {
2988 switch_core_cert_extract_fingerprint(cert, dtls->remote_fp);
2989
2990 r = switch_core_cert_verify(dtls->remote_fp);
2991
2992 X509_free(cert);
2993 } else {
2994 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(dtls->rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 2994, (const char*)(dtls->rtp_session->session
)
, SWITCH_LOG_ERROR, "%s CERT ERR!\n", rtp_type(dtls->rtp_session)dtls->rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video"
: "audio"
);
2995 }
2996
2997 return r;
2998}
2999#endif
3000
3001SWITCH_DECLARE(int)__attribute__((visibility("default"))) int switch_rtp_has_dtls(void) {
3002#ifdef HAVE_OPENSSL_DTLS_SRTP1
3003 return 1;
3004#else
3005 return 0;
3006#endif
3007}
3008
3009SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_del_dtls(switch_rtp_t *rtp_session, dtls_type_t type)
3010{
3011
3012 if (!rtp_session->dtls && !rtp_session->rtcp_dtls) {
3013 return SWITCH_STATUS_FALSE;
3014 }
3015
3016 if ((type & DTLS_TYPE_RTP)) {
3017 if (rtp_session->dtls && rtp_session->dtls == rtp_session->rtcp_dtls) {
3018 rtp_session->rtcp_dtls = NULL((void*)0);
3019 }
3020
3021 if (rtp_session->dtls) {
3022 free_dtls(&rtp_session->dtls);
3023 }
3024
3025 if (rtp_session->jb) {
3026 stfu_n_reset(rtp_session->jb)_stfu_n_reset(rtp_session->jb, "src/switch_rtp.c", __FUNCTION__
, 3026)
;
3027 }
3028
3029 }
3030
3031 if ((type & DTLS_TYPE_RTCP) && rtp_session->rtcp_dtls) {
3032 free_dtls(&rtp_session->rtcp_dtls);
3033 }
3034
3035
3036#ifdef ENABLE_SRTP1
3037 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
3038 int x;
3039 for(x = 0; x < 2; x++) {
3040 if (rtp_session->send_ctx[x]) {
3041 srtp_dealloc(rtp_session->send_ctx[x]);
3042 rtp_session->send_ctx[x] = NULL((void*)0);
3043 }
3044 }
3045 rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0;
3046 }
3047
3048 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV]) {
3049 int x;
3050 for (x = 0; x < 2; x++) {
3051 if (rtp_session->recv_ctx[x]) {
3052 srtp_dealloc(rtp_session->recv_ctx[x]);
3053 rtp_session->recv_ctx[x] = NULL((void*)0);
3054 }
3055 }
3056 rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
3057 }
3058#endif
3059
3060 return SWITCH_STATUS_SUCCESS;
3061}
3062
3063SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) 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)
3064{
3065 switch_dtls_t *dtls;
3066 int ret;
3067 const char *kind = "";
3068
3069#ifndef HAVE_OPENSSL_DTLS_SRTP1
3070 return SWITCH_STATUS_FALSE;
3071#endif
3072
3073 if (!switch_rtp_ready(rtp_session)) {
3074 return SWITCH_STATUS_FALSE;
3075 }
3076
3077 if (!((type & DTLS_TYPE_RTP) || (type & DTLS_TYPE_RTCP)) || !((type & DTLS_TYPE_CLIENT) || (type & DTLS_TYPE_SERVER))) {
3078 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3078, (const char*)(rtp_session->session)
, SWITCH_LOG_CRIT, "INVALID TYPE!\n");
3079 }
3080
3081 switch_rtp_del_dtls(rtp_session, type);
3082
3083 if ((type & DTLS_TYPE_RTP) && (type & DTLS_TYPE_RTCP)) {
3084 kind = "RTP/RTCP";
3085 } else if ((type & DTLS_TYPE_RTP)) {
3086 kind = "RTP";
3087 } else {
3088 kind = "RTCP";
3089 }
3090
3091 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3091, (const char*)(rtp_session->session)
, SWITCH_LOG_INFO,
3092 "Activate %s %s DTLS %s\n", kind, rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", (type & DTLS_TYPE_SERVER) ? "server" : "client");
3093
3094 if (((type & DTLS_TYPE_RTP) && rtp_session->dtls) || ((type & DTLS_TYPE_RTCP) && rtp_session->rtcp_dtls)) {
3095 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3095, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING, "DTLS ALREADY INIT\n");
3096 return SWITCH_STATUS_FALSE;
3097 }
3098
3099 dtls = switch_core_alloc(rtp_session->pool, sizeof(*dtls))switch_core_perform_alloc(rtp_session->pool, sizeof(*dtls)
, "src/switch_rtp.c", (const char *)__func__, 3099)
;
3100
3101 dtls->pem = switch_core_sprintf(rtp_session->pool, "%s%s%s.pem", SWITCH_GLOBAL_dirs.certs_dir, SWITCH_PATH_SEPARATOR"/", DTLS_SRTP_FNAME"dtls-srtp");
3102
3103 if (switch_file_exists(dtls->pem, rtp_session->pool) == SWITCH_STATUS_SUCCESS) {
3104 dtls->pvt = dtls->rsa = dtls->pem;
3105 } else {
3106 dtls->pvt = switch_core_sprintf(rtp_session->pool, "%s%s%s.key", SWITCH_GLOBAL_dirs.certs_dir, SWITCH_PATH_SEPARATOR"/", DTLS_SRTP_FNAME"dtls-srtp");
3107 dtls->rsa = switch_core_sprintf(rtp_session->pool, "%s%s%s.crt", SWITCH_GLOBAL_dirs.certs_dir, SWITCH_PATH_SEPARATOR"/", DTLS_SRTP_FNAME"dtls-srtp");
3108 }
3109
3110 dtls->ca = switch_core_sprintf(rtp_session->pool, "%s%sca-bundle.crt", SWITCH_GLOBAL_dirs.certs_dir, SWITCH_PATH_SEPARATOR"/");
3111
3112 dtls->ssl_ctx = SSL_CTX_new(DTLSv1_method());
3113 switch_assert(dtls->ssl_ctx)((dtls->ssl_ctx) ? (void) (0) : __assert_fail ("dtls->ssl_ctx"
, "src/switch_rtp.c", 3113, __PRETTY_FUNCTION__))
;
3114
3115 SSL_CTX_set_mode(dtls->ssl_ctx, SSL_MODE_AUTO_RETRY)SSL_CTX_ctrl((dtls->ssl_ctx),33,(0x00000004L),((void*)0));
3116
3117 //SSL_CTX_set_verify(dtls->ssl_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
3118 SSL_CTX_set_verify(dtls->ssl_ctx, SSL_VERIFY_NONE0x00, NULL((void*)0));
3119
3120 SSL_CTX_set_cipher_list(dtls->ssl_ctx, "ALL");
3121
3122#ifdef HAVE_OPENSSL_DTLS_SRTP1
3123 //SSL_CTX_set_tlsext_use_srtp(dtls->ssl_ctx, "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32");
3124 SSL_CTX_set_tlsext_use_srtp(dtls->ssl_ctx, "SRTP_AES128_CM_SHA1_80");
3125#endif
3126
3127 dtls->type = type;
3128 dtls->read_bio = BIO_new(BIO_s_mem());
3129 switch_assert(dtls->read_bio)((dtls->read_bio) ? (void) (0) : __assert_fail ("dtls->read_bio"
, "src/switch_rtp.c", 3129, __PRETTY_FUNCTION__))
;
3130
3131 dtls->write_bio = BIO_new(BIO_s_mem());
3132 switch_assert(dtls->write_bio)((dtls->write_bio) ? (void) (0) : __assert_fail ("dtls->write_bio"
, "src/switch_rtp.c", 3132, __PRETTY_FUNCTION__))
;
3133
3134 BIO_set_mem_eof_return(dtls->read_bio, -1)BIO_ctrl(dtls->read_bio,130,-1,((void*)0));
3135 BIO_set_mem_eof_return(dtls->write_bio, -1)BIO_ctrl(dtls->write_bio,130,-1,((void*)0));
3136
3137 if ((ret=SSL_CTX_use_certificate_file(dtls->ssl_ctx, dtls->rsa, SSL_FILETYPE_PEM1)) != 1) {
3138 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3138, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "%s DTLS cert err [%d]\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", SSL_get_error(dtls->ssl, ret));
3139 return SWITCH_STATUS_FALSE;
3140 }
3141
3142 if ((ret=SSL_CTX_use_PrivateKey_file(dtls->ssl_ctx, dtls->pvt, SSL_FILETYPE_PEM1)) != 1) {
3143 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3143, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "%s DTLS key err [%d]\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", SSL_get_error(dtls->ssl, ret));
3144 return SWITCH_STATUS_FALSE;
3145 }
3146
3147 if (SSL_CTX_check_private_key(dtls->ssl_ctx) == 0) {
3148 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3148, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "%s DTLS check key failed\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio");
3149 return SWITCH_STATUS_FALSE;
3150 }
3151
3152 if (!zstr(dtls->ca)_zstr(dtls->ca) && switch_file_exists(dtls->ca, rtp_session->pool) == SWITCH_STATUS_SUCCESS
3153 && (ret = SSL_CTX_load_verify_locations(dtls->ssl_ctx, dtls->ca, NULL((void*)0))) != 1) {
3154 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3154, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "%s DTLS check chain cert failed [%d]\n",
3155 rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio" ,
3156 SSL_get_error(dtls->ssl, ret));
3157 return SWITCH_STATUS_FALSE;
3158 }
3159
3160 dtls->ssl = SSL_new(dtls->ssl_ctx);
3161
3162 SSL_set_bio(dtls->ssl, dtls->read_bio, dtls->write_bio);
3163 SSL_set_mode(dtls->ssl, SSL_MODE_AUTO_RETRY)SSL_ctrl((dtls->ssl),33,(0x00000004L),((void*)0));
3164 SSL_set_read_ahead(dtls->ssl, 1);
3165 //SSL_set_verify(dtls->ssl, (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), cb_verify_peer);
3166 SSL_set_verify(dtls->ssl, SSL_VERIFY_NONE0x00, NULL((void*)0));
3167 SSL_set_app_data(dtls->ssl, dtls)(SSL_set_ex_data(dtls->ssl,0,(char *)dtls));
3168
3169 BIO_ctrl(dtls->read_bio, BIO_CTRL_DGRAM_SET_MTU42, 1400, NULL((void*)0));
3170 BIO_ctrl(dtls->write_bio, BIO_CTRL_DGRAM_SET_MTU42, 1400, NULL((void*)0));
3171 SSL_set_mtu(dtls->ssl, 1400)SSL_ctrl((dtls->ssl),17,(1400),((void*)0));
3172 BIO_ctrl(dtls->write_bio, BIO_C_SET_BUFF_SIZE117, 1400, NULL((void*)0));
3173 BIO_ctrl(dtls->read_bio, BIO_C_SET_BUFF_SIZE117, 1400, NULL((void*)0));
3174
3175
3176
3177 dtls->local_fp = local_fp;
3178 dtls->remote_fp = remote_fp;
3179 dtls->rtp_session = rtp_session;
3180
3181 switch_core_cert_expand_fingerprint(remote_fp, remote_fp->str);
3182
3183 if ((type & DTLS_TYPE_RTP)) {
3184 rtp_session->dtls = dtls;
3185 dtls->sock_output = rtp_session->sock_output;
3186 dtls->remote_addr = rtp_session->remote_addr;
3187 }
3188
3189 if ((type & DTLS_TYPE_RTCP)) {
3190 rtp_session->rtcp_dtls = dtls;
3191 if (!(type & DTLS_TYPE_RTP)) {
3192 dtls->sock_output = rtp_session->rtcp_sock_output;
3193 dtls->remote_addr = rtp_session->rtcp_remote_addr;
3194 }
3195 }
3196
3197 if ((type & DTLS_TYPE_SERVER)) {
3198 SSL_set_accept_state(dtls->ssl);
3199 } else {
3200 SSL_set_connect_state(dtls->ssl);
3201 }
3202
3203 rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK] = 1;
3204 switch_rtp_break(rtp_session);
3205
3206 return SWITCH_STATUS_SUCCESS;
3207
3208}
3209
3210
3211SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_add_crypto_key(switch_rtp_t *rtp_session,
3212 switch_rtp_crypto_direction_t direction,
3213 uint32_t index, switch_rtp_crypto_key_type_t type, unsigned char *key, switch_size_t keylen)
3214{
3215#ifndef ENABLE_SRTP1
3216 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3216, (const char*)(rtp_session->session)
, SWITCH_LOG_CRIT, "SRTP NOT SUPPORTED IN THIS BUILD!\n");
3217 return SWITCH_STATUS_FALSE;
3218#else
3219 switch_rtp_crypto_key_t *crypto_key;
3220 srtp_policy_t *policy;
3221 err_status_t stat;
3222 switch_status_t status = SWITCH_STATUS_SUCCESS;
3223
3224 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
3225 switch_event_t *fsevent = NULL((void*)0);
3226 int idx = 0;
3227 const char *var;
3228
3229 if (direction >= SWITCH_RTP_CRYPTO_MAX || keylen > SWITCH_RTP_MAX_CRYPTO_LEN64) {
3230 return SWITCH_STATUS_FALSE;
3231 }
3232
3233 crypto_key = switch_core_alloc(rtp_session->pool, sizeof(*crypto_key))switch_core_perform_alloc(rtp_session->pool, sizeof(*crypto_key
), "src/switch_rtp.c", (const char *)__func__, 3233)
;
3234
3235 if (direction == SWITCH_RTP_CRYPTO_RECV_RTCP) {
3236 direction = SWITCH_RTP_CRYPTO_RECV;
3237 rtp_session->srtp_idx_rtcp = idx = 1;
3238 } else if (direction == SWITCH_RTP_CRYPTO_SEND_RTCP) {
3239 direction = SWITCH_RTP_CRYPTO_SEND;
3240 rtp_session->srtp_idx_rtcp = idx = 1;
3241 }
3242
3243 if (direction == SWITCH_RTP_CRYPTO_RECV) {
3244 policy = &rtp_session->recv_policy[idx];
3245 } else {
3246 policy = &rtp_session->send_policy[idx];
3247 }
3248
3249 crypto_key->type = type;
3250 crypto_key->index = index;
3251 memcpy(crypto_key->key, key, keylen);
3252 crypto_key->next = rtp_session->crypto_keys[direction];
3253 rtp_session->crypto_keys[direction] = crypto_key;
3254
3255 memset(policy, 0, sizeof(*policy));
3256
3257 /* many devices can't handle gaps in SRTP streams */
3258 if (!((var = switch_channel_get_variable(channel, "srtp_allow_idle_gaps")switch_channel_get_variable_dup(channel, "srtp_allow_idle_gaps"
, SWITCH_TRUE, -1)
)
3259 && switch_true(var))
3260 && (!(var = switch_channel_get_variable(channel, "send_silence_when_idle")switch_channel_get_variable_dup(channel, "send_silence_when_idle"
, SWITCH_TRUE, -1)
)
3261 || !(atoi(var)))) {
3262 switch_channel_set_variable(channel, "send_silence_when_idle", "-1")switch_channel_set_variable_var_check(channel, "send_silence_when_idle"
, "-1", SWITCH_TRUE)
;
3263 }
3264
3265 switch (crypto_key->type) {
3266 case AES_CM_128_HMAC_SHA1_80:
3267 crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtp)crypto_policy_set_rtp_default(&policy->rtp);
3268 crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtcp)crypto_policy_set_rtp_default(&policy->rtcp);
3269
3270 if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
3271 switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_80")switch_channel_set_variable_var_check(channel, "rtp_has_crypto"
, "AES_CM_128_HMAC_SHA1_80", SWITCH_TRUE)
;
3272 }
3273 break;
3274 case AES_CM_128_HMAC_SHA1_32:
3275 crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtp);
3276 crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtcp)crypto_policy_set_rtp_default(&policy->rtcp);
3277
3278
3279 if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
3280 switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_32")switch_channel_set_variable_var_check(channel, "rtp_has_crypto"
, "AES_CM_128_HMAC_SHA1_32", SWITCH_TRUE)
;
3281 }
3282 break;
3283
3284 case AEAD_AES_256_GCM_8:
3285 crypto_policy_set_aes_gcm_256_8_auth(&policy->rtp);
3286 crypto_policy_set_aes_gcm_256_8_auth(&policy->rtcp);
3287
3288 if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
3289 switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_256_GCM_8")switch_channel_set_variable_var_check(channel, "rtp_has_crypto"
, "AEAD_AES_256_GCM_8", SWITCH_TRUE)
;
3290 }
3291 break;
3292
3293 case AEAD_AES_128_GCM_8:
3294 crypto_policy_set_aes_gcm_128_8_auth(&policy->rtp);
3295 crypto_policy_set_aes_gcm_128_8_auth(&policy->rtcp);
3296
3297 if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
3298 switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_128_GCM_8")switch_channel_set_variable_var_check(channel, "rtp_has_crypto"
, "AEAD_AES_128_GCM_8", SWITCH_TRUE)
;
3299 }
3300 break;
3301
3302 case AES_CM_256_HMAC_SHA1_80:
3303 crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtp);
3304 crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtcp);
3305 if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
3306 switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_256_HMAC_SHA1_80")switch_channel_set_variable_var_check(channel, "rtp_has_crypto"
, "AES_CM_256_HMAC_SHA1_80", SWITCH_TRUE)
;
3307 }
3308 break;
3309 case AES_CM_128_NULL_AUTH:
3310 crypto_policy_set_aes_cm_128_null_auth(&policy->rtp);
3311 crypto_policy_set_aes_cm_128_null_auth(&policy->rtcp);
3312
3313 if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
3314 switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_NULL_AUTH")switch_channel_set_variable_var_check(channel, "rtp_has_crypto"
, "AES_CM_128_NULL_AUTH", SWITCH_TRUE)
;
3315 }
3316 break;
3317 default:
3318 break;
3319 }
3320
3321 policy->key = (uint8_t *) crypto_key->key;
3322 policy->next = NULL((void*)0);
3323
3324 policy->window_size = 1024;
3325 policy->allow_repeat_tx = 1;
3326
3327 //policy->rtp.sec_serv = sec_serv_conf_and_auth;
3328 //policy->rtcp.sec_serv = sec_serv_conf_and_auth;
3329
3330 switch (direction) {
3331 case SWITCH_RTP_CRYPTO_RECV:
3332 policy->ssrc.type = ssrc_any_inbound;
3333
3334 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && idx == 0 && rtp_session->recv_ctx[idx]) {
3335 rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] = 1;
3336 } else {
3337 if ((stat = srtp_create(&rtp_session->recv_ctx[idx], policy))) {
3338 status = SWITCH_STATUS_FALSE;
3339 }
3340
3341 if (status == SWITCH_STATUS_SUCCESS) {
3342 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3342, (const char*)(rtp_session->session)
, SWITCH_LOG_INFO, "Activating %s Secure %s RECV\n",
3343 rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "Video" : "Audio", idx ? "RTCP" : "RTP");
3344 rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 1;
3345 } else {
3346 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3346, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error allocating srtp [%d]\n", stat);
3347 return status;
3348 }
3349 }
3350 break;
3351 case SWITCH_RTP_CRYPTO_SEND:
3352 policy->ssrc.type = ssrc_any_outbound;
3353 //policy->ssrc.type = ssrc_specific;
3354 //policy->ssrc.value = rtp_session->ssrc;
3355
3356 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] && idx == 0 && rtp_session->send_ctx[idx]) {
3357 rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET] = 1;
3358 } else {
3359 if ((stat = srtp_create(&rtp_session->send_ctx[idx], policy))) {
3360 status = SWITCH_STATUS_FALSE;
3361 }
3362
3363 if (status == SWITCH_STATUS_SUCCESS) {
3364 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3364, (const char*)(rtp_session->session)
, SWITCH_LOG_INFO, "Activating %s Secure %s SEND\n",
3365 rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "Video" : "Audio", idx ? "RTCP" : "RTP");
3366 rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 1;
3367 } else {
3368 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3368, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error allocating SRTP [%d]\n", stat);
3369 return status;
3370 }
3371 }
3372
3373 break;
3374 default:
3375 abort();
3376 break;
3377 }
3378
3379 if (switch_event_create(&fsevent, SWITCH_EVENT_CALL_SECURE)switch_event_create_subclass_detailed("src/switch_rtp.c", (const
char * )(const char *)__func__, 3379, &fsevent, SWITCH_EVENT_CALL_SECURE
, ((void*)0))
== SWITCH_STATUS_SUCCESS) {
3380 if (rtp_session->dtls) {
3381 switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "srtp:dtls:AES_CM_128_HMAC_SHA1_80");
3382 switch_channel_set_variable(channel, "rtp_has_crypto", "srtp:dtls:AES_CM_128_HMAC_SHA1_80")switch_channel_set_variable_var_check(channel, "rtp_has_crypto"
, "srtp:dtls:AES_CM_128_HMAC_SHA1_80", SWITCH_TRUE)
;
3383 } else {
3384 switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "srtp:sdes:%s", switch_channel_get_variable(channel, "rtp_has_crypto")switch_channel_get_variable_dup(channel, "rtp_has_crypto", SWITCH_TRUE
, -1)
);
3385 }
3386 switch_event_add_header_string(fsevent, SWITCH_STACK_BOTTOM, "caller-unique-id", switch_channel_get_uuid(channel));
3387 switch_event_fire(&fsevent)switch_event_fire_detailed("src/switch_rtp.c", (const char * )
(const char *)__func__, 3387, &fsevent, ((void*)0))
;
3388 }
3389
3390
3391 return SWITCH_STATUS_SUCCESS;
3392#endif
3393}
3394
3395SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_set_interval(switch_rtp_t *rtp_session, uint32_t ms_per_packet, uint32_t samples_per_interval)
3396{
3397 rtp_session->ms_per_packet = ms_per_packet;
3398 rtp_session->samples_per_interval = rtp_session->conf_samples_per_interval = samples_per_interval;
3399 rtp_session->missed_count = 0;
3400 rtp_session->samples_per_second =
3401 (uint32_t) ((double) (1000.0f / (double) (rtp_session->ms_per_packet / 1000)) * (double) rtp_session->samples_per_interval);
3402
3403 rtp_session->one_second = (rtp_session->samples_per_second / rtp_session->samples_per_interval);
3404
3405 return SWITCH_STATUS_SUCCESS;
3406}
3407
3408SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_change_interval(switch_rtp_t *rtp_session, uint32_t ms_per_packet, uint32_t samples_per_interval)
3409{
3410 switch_status_t status = SWITCH_STATUS_SUCCESS;
3411 int change_timer = 0;
3412
3413 if (rtp_session->ms_per_packet && rtp_session->ms_per_packet != ms_per_packet) {
3414 change_timer = 1;
3415 }
3416
3417 switch_rtp_set_interval(rtp_session, ms_per_packet, samples_per_interval);
3418
3419 if (change_timer && rtp_session->timer_name) {
3420 READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session->
reading++
;
3421 WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session->
writing++
;
3422
3423 if (rtp_session->timer.timer_interface) {
3424 switch_core_timer_destroy(&rtp_session->timer);
3425 }
3426 if ((status = switch_core_timer_init(&rtp_session->timer,
3427 rtp_session->timer_name, ms_per_packet / 1000,
3428 samples_per_interval, rtp_session->pool)) == SWITCH_STATUS_SUCCESS) {
3429
3430 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3430, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG,
3431 "RE-Starting timer [%s] %d bytes per %dms\n", rtp_session->timer_name, samples_per_interval, ms_per_packet / 1000);
3432 } else {
3433
3434 memset(&rtp_session->timer, 0, sizeof(rtp_session->timer));
3435 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3435, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR,
3436 "Problem RE-Starting timer [%s] %d bytes per %dms\n", rtp_session->timer_name, samples_per_interval, ms_per_packet / 1000);
3437 }
3438
3439 WRITE_DEC(rtp_session)switch_mutex_unlock(rtp_session->write_mutex); rtp_session
->writing--
;
3440 READ_DEC(rtp_session)switch_mutex_unlock(rtp_session->read_mutex); rtp_session->
reading--
;
3441 }
3442
3443 return status;
3444}
3445
3446SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_set_ssrc(switch_rtp_t *rtp_session, uint32_t ssrc)
3447{
3448 rtp_session->ssrc = ssrc;
3449 rtp_session->send_msg.header.ssrc = htonl(rtp_session->ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->ssrc
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
3450
3451 return SWITCH_STATUS_SUCCESS;
3452}
3453
3454SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_set_remote_ssrc(switch_rtp_t *rtp_session, uint32_t ssrc)
3455{
3456 rtp_session->remote_ssrc = ssrc;
3457
3458 return SWITCH_STATUS_SUCCESS;
3459}
3460
3461SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_create(switch_rtp_t **new_rtp_session,
3462 switch_payload_t payload,
3463 uint32_t samples_per_interval,
3464 uint32_t ms_per_packet,
3465 switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID], char *timer_name, const char **err, switch_memory_pool_t *pool)
3466{
3467 switch_rtp_t *rtp_session = NULL((void*)0);
3468 switch_core_session_t *session = switch_core_memory_pool_get_data(pool, "__session");
3469 switch_channel_t *channel = NULL((void*)0);
3470
3471 if (session) channel = switch_core_session_get_channel(session);
3472
3473 *new_rtp_session = NULL((void*)0);
3474
3475 if (samples_per_interval > SWITCH_RTP_MAX_BUF_LEN16384) {
3476 *err = "Packet Size Too Large!";
3477 return SWITCH_STATUS_FALSE;
3478 }
3479
3480 if (!(rtp_session = switch_core_alloc(pool, sizeof(*rtp_session))switch_core_perform_alloc(pool, sizeof(*rtp_session), "src/switch_rtp.c"
, (const char *)__func__, 3480)
)) {
3481 *err = "Memory Error!";
3482 return SWITCH_STATUS_MEMERR;
3483 }
3484
3485 rtp_session->pool = pool;
3486 rtp_session->te = 101;
3487 rtp_session->recv_te = 101;
3488 rtp_session->session = session;
3489
3490 switch_mutex_init(&rtp_session->flag_mutex, SWITCH_MUTEX_NESTED0x1, pool);
3491 switch_mutex_init(&rtp_session->read_mutex, SWITCH_MUTEX_NESTED0x1, pool);
3492 switch_mutex_init(&rtp_session->write_mutex, SWITCH_MUTEX_NESTED0x1, pool);
3493 switch_mutex_init(&rtp_session->dtmf_data.dtmf_mutex, SWITCH_MUTEX_NESTED0x1, pool);
3494 switch_queue_create(&rtp_session->dtmf_data.dtmf_queue, 100, rtp_session->pool);
3495 switch_queue_create(&rtp_session->dtmf_data.dtmf_inqueue, 100, rtp_session->pool);
3496
3497 switch_rtp_set_flags(rtp_session, flags);
3498
3499 /* for from address on recvfrom calls */
3500 switch_sockaddr_create(&rtp_session->from_addr, pool);
3501
3502 if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
3503 switch_sockaddr_create(&rtp_session->rtcp_from_addr, pool);
3504 }
3505 rtp_session->seq = (uint16_t) rand();
3506 rtp_session->ssrc = (uint32_t) ((intptr_t) rtp_session + (uint32_t) switch_epoch_time_now(NULL((void*)0)));
3507
3508 rtp_session->stats.inbound.R = 100.0;
3509 rtp_session->stats.inbound.mos = 4.5;
3510 rtp_session->send_msg.header.ssrc = htonl(rtp_session->ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->ssrc
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
3511 rtp_session->send_msg.header.ts = 0;
3512 rtp_session->send_msg.header.m = 0;
3513 rtp_session->send_msg.header.pt = (switch_payload_t) htonl(payload)(__extension__ ({ unsigned int __v, __x = (payload); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
3514 rtp_session->send_msg.header.version = 2;
3515 rtp_session->send_msg.header.p = 0;
3516 rtp_session->send_msg.header.x = 0;
3517 rtp_session->send_msg.header.cc = 0;
3518
3519 rtp_session->recv_msg.header.ssrc = 0;
3520 rtp_session->recv_msg.header.ts = 0;
3521 rtp_session->recv_msg.header.seq = 0;
3522 rtp_session->recv_msg.header.m = 0;
3523 rtp_session->recv_msg.header.pt = (switch_payload_t) htonl(payload)(__extension__ ({ unsigned int __v, __x = (payload); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
3524 rtp_session->recv_msg.header.version = 2;
3525 rtp_session->recv_msg.header.p = 0;
3526 rtp_session->recv_msg.header.x = 0;
3527 rtp_session->recv_msg.header.cc = 0;
3528
3529 rtp_session->payload = payload;
3530
3531 switch_rtp_set_interval(rtp_session, ms_per_packet, samples_per_interval);
3532 rtp_session->conf_samples_per_interval = samples_per_interval;
3533
3534 if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && zstr(timer_name)_zstr(timer_name)) {
3535 timer_name = "soft";
3536 }
3537
3538 if (!zstr(timer_name)_zstr(timer_name) && !strcasecmp(timer_name, "none")) {
3539 timer_name = NULL((void*)0);
3540 }
3541
3542 if (!zstr(timer_name)_zstr(timer_name)) {
3543 rtp_session->timer_name = switch_core_strdup(pool, timer_name)switch_core_perform_strdup(pool, timer_name, "src/switch_rtp.c"
, (const char *)__func__, 3543)
;
3544 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER);
3545 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
3546
3547 if (switch_core_timer_init(&rtp_session->timer, timer_name, ms_per_packet / 1000, samples_per_interval, pool) == SWITCH_STATUS_SUCCESS) {
3548 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3548, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG,
3549 "Starting timer [%s] %d bytes per %dms\n", timer_name, samples_per_interval, ms_per_packet / 1000);
3550 } else {
3551 memset(&rtp_session->timer, 0, sizeof(rtp_session->timer));
3552 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3552, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR,
3553 "Error Starting timer [%s] %d bytes per %dms, async RTP disabled\n", timer_name, samples_per_interval, ms_per_packet / 1000);
3554 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER);
3555 }
3556 } else {
3557 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3557, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "Not using a timer\n");
3558 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER);
3559 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
3560 }
3561
3562
3563 if (channel) {
3564 switch_channel_set_private(channel, "__rtcp_audio_rtp_session", rtp_session);
3565 }
3566
3567#ifdef ENABLE_ZRTP
3568 if (zrtp_on && session && channel && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
3569 switch_rtp_t *master_rtp_session = NULL((void*)0);
3570
3571 int initiator = 0;
3572 const char *zrtp_enabled = switch_channel_get_variable(channel, "zrtp_secure_media")switch_channel_get_variable_dup(channel, "zrtp_secure_media",
SWITCH_TRUE, -1)
;
3573 int srtp_enabled = switch_channel_test_flag(channel, CF_SECURE);
3574
3575 if (srtp_enabled && switch_true(zrtp_enabled)) {
3576 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3576, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING,
3577 "You can not have ZRTP and SRTP enabled simultaneously, ZRTP will be disabled for this call!\n");
3578 switch_channel_set_variable(channel, "zrtp_secure_media", NULL)switch_channel_set_variable_var_check(channel, "zrtp_secure_media"
, ((void*)0), SWITCH_TRUE)
;
3579 zrtp_enabled = NULL((void*)0);
3580 }
3581
3582
3583 if (switch_true(zrtp_enabled)) {
3584 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
3585 switch_channel_set_private(channel, "__zrtp_video_rtp_session", rtp_session);
3586 master_rtp_session = switch_channel_get_private(channel, "__zrtp_audio_rtp_session");
3587 } else {
3588 switch_channel_set_private(channel, "__zrtp_audio_rtp_session", rtp_session);
3589 master_rtp_session = rtp_session;
3590 }
3591
3592
3593 if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
3594 initiator = 1;
3595 }
3596
3597 if (rtp_session == master_rtp_session) {
3598 rtp_session->zrtp_profile = switch_core_alloc(rtp_session->pool, sizeof(*rtp_session->zrtp_profile))switch_core_perform_alloc(rtp_session->pool, sizeof(*rtp_session
->zrtp_profile), "src/switch_rtp.c", (const char *)__func__
, 3598)
;
3599 zrtp_profile_defaults(rtp_session->zrtp_profile, zrtp_global);
3600
3601 rtp_session->zrtp_profile->allowclear = 0;
3602 rtp_session->zrtp_profile->disclose_bit = 0;
3603 rtp_session->zrtp_profile->cache_ttl = (uint32_t) -1;
3604
3605 if (zrtp_status_ok != zrtp_session_init(zrtp_global, rtp_session->zrtp_profile, zid, initiator, &rtp_session->zrtp_session)) {
3606 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3606, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error! zRTP INIT Failed\n");
3607 zrtp_session_down(rtp_session->zrtp_session);
3608 rtp_session->zrtp_session = NULL((void*)0);
3609 goto end;
3610 }
3611
3612 zrtp_session_set_userdata(rtp_session->zrtp_session, session);
3613
3614
3615 if (zrtp_status_ok != zrtp_stream_attach(master_rtp_session->zrtp_session, &rtp_session->zrtp_stream)) {
3616 abort();
3617 }
3618
3619 zrtp_stream_set_userdata(rtp_session->zrtp_stream, rtp_session);
3620
3621 if (switch_true(switch_channel_get_variable(channel, "zrtp_enrollment")switch_channel_get_variable_dup(channel, "zrtp_enrollment", SWITCH_TRUE
, -1)
)) {
3622 zrtp_stream_registration_start(rtp_session->zrtp_stream, rtp_session->ssrc);
3623 } else {
3624 zrtp_stream_start(rtp_session->zrtp_stream, rtp_session->ssrc);
3625 }
3626 }
3627
3628 }
3629 }
3630
3631 end:
3632
3633#endif
3634
3635 /* Jitter */
3636 rtp_session->stats.inbound.last_proc_time = switch_time_now() / 1000;
3637 rtp_session->stats.inbound.jitter_n = 0;
3638 rtp_session->stats.inbound.jitter_add = 0;
3639 rtp_session->stats.inbound.jitter_addsq = 0;
3640 rtp_session->stats.inbound.min_variance = 0;
3641 rtp_session->stats.inbound.max_variance = 0;
3642
3643 /* Burst and Packet Loss */
3644 rtp_session->stats.inbound.lossrate = 0;
3645 rtp_session->stats.inbound.burstrate = 0;
3646 memset(rtp_session->stats.inbound.loss, 0, sizeof(rtp_session->stats.inbound.loss));
3647 rtp_session->stats.inbound.last_loss = 0;
3648 rtp_session->stats.inbound.last_processed_seq = -1;
3649
3650 rtp_session->ready = 1;
3651 *new_rtp_session = rtp_session;
3652
3653 return SWITCH_STATUS_SUCCESS;
3654}
3655
3656SWITCH_DECLARE(switch_rtp_t *)__attribute__((visibility("default"))) switch_rtp_t * switch_rtp_new(const char *rx_host,
3657 switch_port_t rx_port,
3658 const char *tx_host,
3659 switch_port_t tx_port,
3660 switch_payload_t payload,
3661 uint32_t samples_per_interval,
3662 uint32_t ms_per_packet,
3663 switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID], char *timer_name, const char **err, switch_memory_pool_t *pool)
3664{
3665 switch_rtp_t *rtp_session = NULL((void*)0);
3666
3667 if (zstr(rx_host)_zstr(rx_host)) {
3668 *err = "Missing local host";
3669 goto end;
3670 }
3671
3672 if (!rx_port) {
3673 *err = "Missing local port";
3674 goto end;
3675 }
3676
3677 if (zstr(tx_host)_zstr(tx_host)) {
3678 *err = "Missing remote host";
3679 goto end;
3680 }
3681
3682 if (!tx_port) {
3683 *err = "Missing remote port";
3684 goto end;
3685 }
3686
3687 if (switch_rtp_create(&rtp_session, payload, samples_per_interval, ms_per_packet, flags, timer_name, err, pool) != SWITCH_STATUS_SUCCESS) {
3688 goto end;
3689 }
3690
3691 switch_mutex_lock(rtp_session->flag_mutex);
3692
3693 if (switch_rtp_set_local_address(rtp_session, rx_host, rx_port, err) != SWITCH_STATUS_SUCCESS) {
3694 switch_mutex_unlock(rtp_session->flag_mutex);
3695 rtp_session = NULL((void*)0);
3696 goto end;
3697 }
3698
3699 if (switch_rtp_set_remote_address(rtp_session, tx_host, tx_port, 0, SWITCH_TRUE, err) != SWITCH_STATUS_SUCCESS) {
3700 switch_mutex_unlock(rtp_session->flag_mutex);
3701 rtp_session = NULL((void*)0);
3702 goto end;
3703 }
3704
3705 end:
3706
3707 if (rtp_session) {
3708 switch_mutex_unlock(rtp_session->flag_mutex);
3709 rtp_session->ready = 2;
3710 rtp_session->rx_host = switch_core_strdup(rtp_session->pool, rx_host)switch_core_perform_strdup(rtp_session->pool, rx_host, "src/switch_rtp.c"
, (const char *)__func__, 3710)
;
3711 rtp_session->rx_port = rx_port;
3712 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
3713 } else {
3714 switch_rtp_release_port(rx_host, rx_port);
3715 }
3716
3717 return rtp_session;
3718}
3719
3720SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_telephony_event(switch_rtp_t *rtp_session, switch_payload_t te)
3721{
3722 if (te > 95) {
3723 rtp_session->te = te;
3724 }
3725}
3726
3727
3728SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_telephony_recv_event(switch_rtp_t *rtp_session, switch_payload_t te)
3729{
3730 if (te > 95) {
3731 rtp_session->recv_te = te;
3732 }
3733}
3734
3735
3736SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_cng_pt(switch_rtp_t *rtp_session, switch_payload_t pt)
3737{
3738 rtp_session->cng_pt = pt;
3739 rtp_session->flags[SWITCH_RTP_FLAG_AUTO_CNG] = 1;
3740}
3741
3742static void jb_callback(stfu_instance_t *i, void *udata)
3743{
3744 switch_core_session_t *session = (switch_core_session_t *) udata;
3745 stfu_report_t r = { 0 };
3746
3747 stfu_n_report(i, &r);
3748
3749 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3749, (const char*)(session)
, SWITCH_LOG_DEBUG8,
3750 "%s JB REPORT:\nlen: %u\nin: %u\nclean: %u\ngood: %u\nbad: %u\njitter percent %0.2f\nmissing percent %0.2f\n\n",
3751 switch_core_session_get_name(session)switch_channel_get_name(switch_core_session_get_channel(session
))
,
3752 r.qlen,
3753 r.packet_in_count,
3754 r.clean_count,
3755 r.consecutive_good_count,
3756 r.consecutive_bad_count,
3757 r.period_jitter_percent,
3758 r.period_missing_percent
3759 );
3760
3761}
3762
3763SWITCH_DECLARE(stfu_instance_t *)__attribute__((visibility("default"))) stfu_instance_t * switch_rtp_get_jitter_buffer(switch_rtp_t *rtp_session)
3764{
3765 if (!switch_rtp_ready(rtp_session) || !rtp_session->jb) {
3766 return NULL((void*)0);
3767 }
3768
3769 return rtp_session->jb;
3770}
3771
3772SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_pause_jitter_buffer(switch_rtp_t *rtp_session, switch_bool_t pause)
3773{
3774
3775 if (!switch_rtp_ready(rtp_session) || !rtp_session->jb) {
3776 return SWITCH_STATUS_FALSE;
3777 }
3778
3779 if (!!pause == !!rtp_session->pause_jb) {
3780 return SWITCH_STATUS_FALSE;
3781 }
3782
3783 if (rtp_session->pause_jb && !pause) {
3784 stfu_n_reset(rtp_session->jb)_stfu_n_reset(rtp_session->jb, "src/switch_rtp.c", __FUNCTION__
, 3784)
;
3785 }
3786
3787 rtp_session->pause_jb = pause ? 1 : 0;
3788
3789 return SWITCH_STATUS_SUCCESS;
3790}
3791
3792SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_deactivate_jitter_buffer(switch_rtp_t *rtp_session)
3793{
3794
3795 if (!switch_rtp_ready(rtp_session) || !rtp_session->jb) {
3796 return SWITCH_STATUS_FALSE;
3797 }
3798
3799 rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB]++;
3800
3801 return SWITCH_STATUS_SUCCESS;
3802}
3803
3804static void jb_logger(const char *file, const char *func, int line, int level, const char *fmt, ...)
3805{
3806 int ret;
3807 char *data;
3808 va_list ap;
3809
3810 va_start(ap, fmt)__builtin_va_start(ap, fmt);
3811 ret = switch_vasprintf(&data, fmt, ap);
3812 if (ret != -1) {
3813 switch_log_printf(SWITCH_CHANNEL_ID_LOG_CLEAN, file, func, line, NULL((void*)0), SWITCH_LOG_CONSOLE, "%d: %s", line, data);
3814 free(data);
3815 }
3816
3817 //switch_log_printf(SWITCH_CHANNEL_ID_LOG_CLEAN, file, func, line, NULL, level, fmt, ap);
3818 va_end(ap)__builtin_va_end(ap);
3819}
3820
3821SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_debug_jitter_buffer(switch_rtp_t *rtp_session, const char *name)
3822{
3823
3824 if (!switch_rtp_ready(rtp_session) || !rtp_session->jb) {
3825 return SWITCH_STATUS_FALSE;
3826 }
3827
3828 stfu_n_debug(rtp_session->jb, name);
3829 stfu_global_set_logger(jb_logger);
3830
3831 return SWITCH_STATUS_SUCCESS;
3832}
3833
3834SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_activate_jitter_buffer(switch_rtp_t *rtp_session,
3835 uint32_t queue_frames,
3836 uint32_t max_queue_frames,
3837 uint32_t samples_per_packet,
3838 uint32_t samples_per_second,
3839 uint32_t max_drift)
3840{
3841
3842 if (!switch_rtp_ready(rtp_session)) {
3843 return SWITCH_STATUS_FALSE;
3844 }
3845
3846 if (queue_frames < 1) {
3847 queue_frames = 3;
3848 }
3849
3850 if (max_queue_frames < queue_frames) {
3851 max_queue_frames = queue_frames * 3;
3852 }
3853
3854 READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session->
reading++
;
3855
3856 if (rtp_session->jb) {
3857 stfu_n_destroy(&rtp_session->jb);
3858 }
3859
3860 rtp_session->jb = stfu_n_init(queue_frames, max_queue_frames ? max_queue_frames : 50, samples_per_packet, samples_per_second, max_drift);
3861
3862 READ_DEC(rtp_session)switch_mutex_unlock(rtp_session->read_mutex); rtp_session->
reading--
;
3863
3864 if (rtp_session->jb) {
3865
3866 stfu_n_call_me(rtp_session->jb, jb_callback, rtp_session->session);
3867
3868 return SWITCH_STATUS_SUCCESS;
3869 }
3870
3871 return SWITCH_STATUS_FALSE;
3872}
3873
3874SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_activate_rtcp(switch_rtp_t *rtp_session, int send_rate, switch_port_t remote_port, switch_bool_t mux)
3875{
3876 const char *err = NULL((void*)0);
3877
3878 if (!rtp_session->ms_per_packet) {
3879 return SWITCH_STATUS_FALSE;
3880 }
3881
3882 rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] = 1;
3883
3884 if (!(rtp_session->remote_rtcp_port = remote_port)) {
3885 rtp_session->remote_rtcp_port = rtp_session->remote_port + 1;
3886 }
3887
3888 if (mux) {
3889 rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]++;
3890 }
3891
3892
3893 if (send_rate == -1) {
3894
3895 rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] = 1;
3896 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3896, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "RTCP passthru enabled. Remote Port: %d\n", rtp_session->remote_rtcp_port);
3897 } else {
3898 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3898, (const char*)(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);
3899
3900 rtp_session->rtcp_interval = send_rate;
3901 rtp_session->rtcp_send_rate = (uint16_t)(send_rate/1000);
3902 rtp_session->next_rtcp_send = switch_time_now() + (rtp_session->rtcp_interval * 1000);
3903 }
3904
3905 if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
3906
3907 if (switch_sockaddr_info_get(&rtp_session->rtcp_remote_addr, rtp_session->eff_remote_host_str, SWITCH_UNSPEC0,
3908 rtp_session->remote_rtcp_port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !rtp_session->rtcp_remote_addr) {
3909 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3909, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "RTCP MUX Remote Address Error!");
3910 return SWITCH_STATUS_FALSE;
3911 }
3912
3913 rtp_session->rtcp_local_addr = rtp_session->local_addr;
3914 rtp_session->rtcp_from_addr = rtp_session->from_addr;
3915 rtp_session->rtcp_sock_input = rtp_session->sock_input;
3916 rtp_session->rtcp_sock_output = rtp_session->sock_output;
3917
3918 rtp_session->rtcp_recv_msg_p = (rtcp_msg_t *) &rtp_session->recv_msg;
3919
3920 return enable_remote_rtcp_socket(rtp_session, &err);
3921 } else {
3922 rtp_session->rtcp_recv_msg_p = (rtcp_msg_t *) &rtp_session->rtcp_recv_msg;
3923 }
3924
3925 return enable_local_rtcp_socket(rtp_session, &err) || enable_remote_rtcp_socket(rtp_session, &err);
3926
3927}
3928
3929SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_activate_ice(switch_rtp_t *rtp_session, char *login, char *rlogin,
3930 const char *password, const char *rpassword, ice_proto_t proto,
3931 switch_core_media_ice_type_t type, ice_t *ice_params)
3932{
3933 char ice_user[80];
3934 char user_ice[80];
3935 char luser_ice[80];
3936 switch_rtp_ice_t *ice;
3937 char *host = NULL((void*)0);
3938 switch_port_t port = 0;
3939 char bufc[30];
3940
3941
3942 if (proto == IPR_RTP) {
3943 ice = &rtp_session->ice;
3944 } else {
3945 ice = &rtp_session->rtcp_ice;
3946 }
3947
3948 ice->proto = proto;
3949
3950 if ((type & ICE_VANILLA)) {
3951 switch_snprintf(ice_user, sizeof(ice_user), "%s:%s", login, rlogin);
3952 switch_snprintf(user_ice, sizeof(user_ice), "%s:%s", rlogin, login);
3953 switch_snprintf(luser_ice, sizeof(user_ice), "%s%s", rlogin, login);
3954 ice->ready = ice->rready = 0;
3955 } else {
3956 switch_snprintf(ice_user, sizeof(ice_user), "%s%s", login, rlogin);
3957 switch_snprintf(user_ice, sizeof(user_ice), "%s%s", rlogin, login);
3958 ice->ready = ice->rready = 1;
3959 }
3960
3961 ice->ice_user = switch_core_strdup(rtp_session->pool, ice_user)switch_core_perform_strdup(rtp_session->pool, ice_user, "src/switch_rtp.c"
, (const char *)__func__, 3961)
;
3962 ice->user_ice = switch_core_strdup(rtp_session->pool, user_ice)switch_core_perform_strdup(rtp_session->pool, user_ice, "src/switch_rtp.c"
, (const char *)__func__, 3962)
;
3963 ice->luser_ice = switch_core_strdup(rtp_session->pool, luser_ice)switch_core_perform_strdup(rtp_session->pool, luser_ice, "src/switch_rtp.c"
, (const char *)__func__, 3963)
;
3964 ice->type = type;
3965 ice->ice_params = ice_params;
3966 ice->pass = "";
3967 ice->rpass = "";
3968 ice->next_run = switch_micro_time_now();
3969
3970 if (password) {
3971 ice->pass = switch_core_strdup(rtp_session->pool, password)switch_core_perform_strdup(rtp_session->pool, password, "src/switch_rtp.c"
, (const char *)__func__, 3971)
;
3972 }
3973
3974 if (rpassword) {
3975 ice->rpass = switch_core_strdup(rtp_session->pool, rpassword)switch_core_perform_strdup(rtp_session->pool, rpassword, "src/switch_rtp.c"
, (const char *)__func__, 3975)
;
3976 }
3977
3978 if ((ice->type & ICE_VANILLA) && ice->ice_params) {
3979 host = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr;
3980 port = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port;
3981
3982 if (!host || !port || switch_sockaddr_info_get(&ice->addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !ice->addr) {
3983 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3983, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error setting remote host!\n");
3984 return SWITCH_STATUS_FALSE;
3985 }
3986 } else {
3987 if (proto == IPR_RTP) {
3988 ice->addr = rtp_session->remote_addr;
3989 } else {
3990 ice->addr = rtp_session->rtcp_remote_addr;
3991 }
3992
3993 host = (char *)switch_get_addr(bufc, sizeof(bufc), ice->addr);
3994 port = switch_sockaddr_get_port(ice->addr);
3995 }
3996
3997 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 3997, (const char*)(rtp_session->session)
, SWITCH_LOG_NOTICE, "Activating %s %s ICE: %s %s:%d\n",
3998 proto == IPR_RTP ? "RTP" : "RTCP", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", ice_user, host, port);
3999
4000
4001 rtp_session->rtp_bugs |= RTP_BUG_ACCEPT_ANY_PACKETS;
4002
4003
4004 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
4005 rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK] = 1;
4006 switch_rtp_break(rtp_session);
4007 }
4008
4009 return SWITCH_STATUS_SUCCESS;
4010}
4011
4012
4013SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_flush(switch_rtp_t *rtp_session)
4014{
4015 if (!switch_rtp_ready(rtp_session)) {
4016 return;
4017 }
4018
4019 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
4020}
4021
4022SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_video_refresh(switch_rtp_t *rtp_session)
4023{
4024 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
4025 (rtp_session->ice.ice_user || rtp_session->flags[SWITCH_RTP_FLAG_FIR] || rtp_session->flags[SWITCH_RTP_FLAG_PLI])) {
4026 if (!rtp_session->fir_countdown) {
4027 rtp_session->fir_countdown = FIR_COUNTDOWN50;
4028 }
4029 }
4030}
4031
4032SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_break(switch_rtp_t *rtp_session)
4033{
4034 if (!switch_rtp_ready(rtp_session)) {
4035 return;
4036 }
4037
4038 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
4039 int ret = 1;
4040
4041 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK]) {
4042 rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK] = 0;
4043 ret = 0;
4044 } else if (rtp_session->session) {
4045 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
4046 if (switch_channel_test_flag(channel, CF_VIDEO_BREAK)) {
4047 switch_channel_clear_flag(channel, CF_VIDEO_BREAK);
4048 ret = 0;
4049 }
4050 }
4051
4052 if (ret) return;
4053
4054 switch_rtp_video_refresh(rtp_session);
4055 }
4056
4057 switch_mutex_lock(rtp_session->flag_mutex);
4058 rtp_session->flags[SWITCH_RTP_FLAG_BREAK] = 1;
4059
4060 if (rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK]) {
4061 switch_mutex_unlock(rtp_session->flag_mutex);
4062 return;
4063 }
4064
4065 if (rtp_session->sock_input) {
4066 ping_socket(rtp_session);
4067 }
4068
4069 switch_mutex_unlock(rtp_session->flag_mutex);
4070}
4071
4072SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_kill_socket(switch_rtp_t *rtp_session)
4073{
4074 switch_assert(rtp_session != NULL)((rtp_session != ((void*)0)) ? (void) (0) : __assert_fail ("rtp_session != ((void*)0)"
, "src/switch_rtp.c", 4074, __PRETTY_FUNCTION__))
;
4075 switch_mutex_lock(rtp_session->flag_mutex);
4076 if (rtp_session->flags[SWITCH_RTP_FLAG_IO]) {
4077 rtp_session->flags[SWITCH_RTP_FLAG_IO] = 0;
4078 if (rtp_session->sock_input) {
4079 ping_socket(rtp_session);
4080 switch_socket_shutdown(rtp_session->sock_input, SWITCH_SHUTDOWN_READWRITE);
4081 }
4082 if (rtp_session->sock_output && rtp_session->sock_output != rtp_session->sock_input) {
4083 switch_socket_shutdown(rtp_session->sock_output, SWITCH_SHUTDOWN_READWRITE);
4084 }
4085
4086 if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
4087 if (rtp_session->rtcp_sock_input) {
4088 ping_socket(rtp_session);
4089 switch_socket_shutdown(rtp_session->rtcp_sock_input, SWITCH_SHUTDOWN_READWRITE);
4090 }
4091 if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) {
4092 switch_socket_shutdown(rtp_session->rtcp_sock_output, SWITCH_SHUTDOWN_READWRITE);
4093 }
4094 }
4095 }
4096 switch_mutex_unlock(rtp_session->flag_mutex);
4097}
4098
4099SWITCH_DECLARE(uint8_t)__attribute__((visibility("default"))) uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
4100{
4101 uint8_t ret;
4102
4103 if (!rtp_session || !rtp_session->flag_mutex || rtp_session->flags[SWITCH_RTP_FLAG_SHUTDOWN]) {
4104 return 0;
4105 }
4106
4107 switch_mutex_lock(rtp_session->flag_mutex);
4108 ret = (rtp_session->flags[SWITCH_RTP_FLAG_IO] && rtp_session->sock_input && rtp_session->sock_output && rtp_session->remote_addr
4109 && rtp_session->ready == 2) ? 1 : 0;
4110 switch_mutex_unlock(rtp_session->flag_mutex);
4111
4112 return ret;
4113}
4114
4115SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_destroy(switch_rtp_t **rtp_session)
4116{
4117 void *pop;
4118 switch_socket_t *sock;
4119#ifdef ENABLE_SRTP1
4120 int x;
4121#endif
4122
4123 if (!rtp_session || !*rtp_session || !(*rtp_session)->ready) {
4124 return;
4125 }
4126
4127 (*rtp_session)->flags[SWITCH_RTP_FLAG_SHUTDOWN] = 1;
4128
4129 READ_INC((*rtp_session))switch_mutex_lock((*rtp_session)->read_mutex); (*rtp_session
)->reading++
;
4130 WRITE_INC((*rtp_session))switch_mutex_lock((*rtp_session)->write_mutex); (*rtp_session
)->writing++
;
4131
4132 (*rtp_session)->ready = 0;
4133
4134 READ_DEC((*rtp_session))switch_mutex_unlock((*rtp_session)->read_mutex); (*rtp_session
)->reading--
;
4135 WRITE_DEC((*rtp_session))switch_mutex_unlock((*rtp_session)->write_mutex); (*rtp_session
)->writing--
;
4136
4137 do_mos(*rtp_session, SWITCH_TRUE);
4138
4139 switch_mutex_lock((*rtp_session)->flag_mutex);
4140
4141 switch_rtp_kill_socket(*rtp_session);
4142
4143 while (switch_queue_trypop((*rtp_session)->dtmf_data.dtmf_inqueue, &pop) == SWITCH_STATUS_SUCCESS) {
4144 switch_safe_free(pop)if (pop) {free(pop);pop=((void*)0);};
4145 }
4146
4147 while (switch_queue_trypop((*rtp_session)->dtmf_data.dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
4148 switch_safe_free(pop)if (pop) {free(pop);pop=((void*)0);};
4149 }
4150
4151 if ((*rtp_session)->jb) {
4152 stfu_n_destroy(&(*rtp_session)->jb);
4153 }
4154
4155 if ((*rtp_session)->dtls && (*rtp_session)->dtls == (*rtp_session)->rtcp_dtls) {
4156 (*rtp_session)->rtcp_dtls = NULL((void*)0);
4157 }
4158
4159 if ((*rtp_session)->dtls) {
4160 free_dtls(&(*rtp_session)->dtls);
4161 }
4162
4163 if ((*rtp_session)->rtcp_dtls) {
4164 free_dtls(&(*rtp_session)->rtcp_dtls);
4165 }
4166
4167
4168 sock = (*rtp_session)->sock_input;
4169 (*rtp_session)->sock_input = NULL((void*)0);
4170 switch_socket_close(sock);
4171
4172 if ((*rtp_session)->sock_output != sock) {
4173 sock = (*rtp_session)->sock_output;
4174 (*rtp_session)->sock_output = NULL((void*)0);
4175 switch_socket_close(sock);
4176 }
4177
4178 if ((sock = (*rtp_session)->rtcp_sock_input)) {
4179 (*rtp_session)->rtcp_sock_input = NULL((void*)0);
4180 switch_socket_close(sock);
4181
4182 if ((*rtp_session)->rtcp_sock_output && (*rtp_session)->rtcp_sock_output != sock) {
4183 if ((sock = (*rtp_session)->rtcp_sock_output)) {
4184 (*rtp_session)->rtcp_sock_output = NULL((void*)0);
4185 switch_socket_close(sock);
4186 }
4187 }
4188 }
4189
4190 if ((*rtp_session)->flags[SWITCH_RTP_FLAG_VAD]) {
4191 switch_rtp_disable_vad(*rtp_session);
4192 }
4193
4194#ifdef ENABLE_SRTP1
4195 if ((*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
4196 for(x = 0; x < 2; x++) {
4197 if ((*rtp_session)->send_ctx[x]) {
4198 srtp_dealloc((*rtp_session)->send_ctx[x]);
4199 (*rtp_session)->send_ctx[x] = NULL((void*)0);
4200 }
4201 }
4202 (*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0;
4203 }
4204
4205 if ((*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_RECV]) {
4206 for (x = 0; x < 2; x++) {
4207 if ((*rtp_session)->recv_ctx[x]) {
4208 srtp_dealloc((*rtp_session)->recv_ctx[x]);
4209 (*rtp_session)->recv_ctx[x] = NULL((void*)0);
4210 }
4211 }
4212 (*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
4213 }
4214#endif
4215
4216#ifdef ENABLE_ZRTP
4217 /* ZRTP */
4218 if (zrtp_on && !(*rtp_session)->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
4219
4220 if ((*rtp_session)->zrtp_stream != NULL((void*)0)) {
4221 zrtp_stream_stop((*rtp_session)->zrtp_stream);
4222 }
4223
4224 if ((*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_SEND]) {
4225 (*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_SEND] = 0;
4226 }
4227
4228 if ((*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_RECV]) {
4229 (*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_RECV] = 0;
4230 }
4231
4232 if ((*rtp_session)->zrtp_session) {
4233 zrtp_session_down((*rtp_session)->zrtp_session);
4234 (*rtp_session)->zrtp_session = NULL((void*)0);
4235 }
4236 }
4237#endif
4238 if ((*rtp_session)->timer.timer_interface) {
4239 switch_core_timer_destroy(&(*rtp_session)->timer);
4240 }
4241
4242 switch_rtp_release_port((*rtp_session)->rx_host, (*rtp_session)->rx_port);
4243 switch_mutex_unlock((*rtp_session)->flag_mutex);
4244
4245 return;
4246}
4247
4248SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_interdigit_delay(switch_rtp_t *rtp_session, uint32_t delay)
4249{
4250 rtp_session->interdigit_delay = delay;
4251}
4252
4253SWITCH_DECLARE(switch_socket_t *)__attribute__((visibility("default"))) switch_socket_t * switch_rtp_get_rtp_socket(switch_rtp_t *rtp_session)
4254{
4255 return rtp_session->sock_input;
4256}
4257
4258SWITCH_DECLARE(uint32_t)__attribute__((visibility("default"))) uint32_t switch_rtp_get_default_samples_per_interval(switch_rtp_t *rtp_session)
4259{
4260 return rtp_session->samples_per_interval;
4261}
4262
4263SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_default_payload(switch_rtp_t *rtp_session, switch_payload_t payload)
4264{
4265 rtp_session->payload = payload;
4266}
4267
4268SWITCH_DECLARE(uint32_t)__attribute__((visibility("default"))) uint32_t switch_rtp_get_default_payload(switch_rtp_t *rtp_session)
4269{
4270 return rtp_session->payload;
4271}
4272
4273SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_invalid_handler(switch_rtp_t *rtp_session, switch_rtp_invalid_handler_t on_invalid)
4274{
4275 rtp_session->invalid_handler = on_invalid;
4276}
4277
4278SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_flags(switch_rtp_t *rtp_session, switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID])
4279{
4280 int i;
4281
4282 for(i = 0; i < SWITCH_RTP_FLAG_INVALID; i++) {
4283 if (flags[i]) {
4284 rtp_session->flags[i] = flags[i];
4285
4286 if (i == SWITCH_RTP_FLAG_AUTOADJ) {
4287 rtp_session->autoadj_window = 20;
4288 rtp_session->autoadj_threshold = 10;
4289 rtp_session->autoadj_tally = 0;
4290 if (rtp_session->session) {
4291 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
4292 const char *x = switch_channel_get_variable(channel, "rtp_auto_adjust_threshold")switch_channel_get_variable_dup(channel, "rtp_auto_adjust_threshold"
, SWITCH_TRUE, -1)
;
4293 if (x && *x) {
4294 int xn = atoi(x);
4295 if (xn > 0 && xn <= 65535) {
4296 rtp_session->autoadj_window = xn*2;
4297 rtp_session->autoadj_threshold = xn;
4298 }
4299 }
4300 }
4301 rtp_flush_read_buffer(rtp_session, SWITCH_RTP_FLUSH_ONCE);
4302 } else if (i == SWITCH_RTP_FLAG_NOBLOCK && rtp_session->sock_input) {
4303 switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, TRUE(!0));
4304 }
4305 }
4306 }
4307}
4308
4309SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_clear_flags(switch_rtp_t *rtp_session, switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID])
4310{
4311 int i;
4312
4313 for(i = 0; i < SWITCH_RTP_FLAG_INVALID; i++) {
4314 if (flags[i]) {
4315 rtp_session->flags[i] = 0;
4316 }
4317 }
4318}
4319
4320SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
4321{
4322
4323 switch_mutex_lock(rtp_session->flag_mutex);
4324 rtp_session->flags[flag] = 1;
4325 switch_mutex_unlock(rtp_session->flag_mutex);
4326
4327 if (flag == SWITCH_RTP_FLAG_DTMF_ON) {
4328 rtp_session->stats.inbound.last_processed_seq = 0;
4329 } else if (flag == SWITCH_RTP_FLAG_FLUSH) {
4330 reset_jitter_seq(rtp_session);
4331 } else if (flag == SWITCH_RTP_FLAG_AUTOADJ) {
4332 rtp_session->autoadj_window = 20;
4333 rtp_session->autoadj_threshold = 10;
4334 rtp_session->autoadj_tally = 0;
4335 if (rtp_session->session) {
4336 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
4337 const char *x = switch_channel_get_variable(channel, "rtp_auto_adjust_threshold")switch_channel_get_variable_dup(channel, "rtp_auto_adjust_threshold"
, SWITCH_TRUE, -1)
;
4338 if (x && *x) {
4339 int xn = atoi(x);
4340 if (xn > 0 && xn <= 65535) {
4341 rtp_session->autoadj_window = xn*2;
4342 rtp_session->autoadj_threshold = xn;
4343 }
4344 }
4345 }
4346 rtp_flush_read_buffer(rtp_session, SWITCH_RTP_FLUSH_ONCE);
4347 if (rtp_session->jb) {
4348 stfu_n_reset(rtp_session->jb)_stfu_n_reset(rtp_session->jb, "src/switch_rtp.c", __FUNCTION__
, 4348)
;
4349 }
4350 } else if (flag == SWITCH_RTP_FLAG_NOBLOCK && rtp_session->sock_input) {
4351 switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, TRUE(!0));
4352 }
4353
4354}
4355
4356SWITCH_DECLARE(uint32_t)__attribute__((visibility("default"))) uint32_t switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
4357{
4358 return (uint32_t) rtp_session->flags[flags];
4359}
4360
4361SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
4362{
4363
4364 switch_mutex_lock(rtp_session->flag_mutex);
4365 rtp_session->flags[flag] = 0;
4366 switch_mutex_unlock(rtp_session->flag_mutex);
4367
4368 if (flag == SWITCH_RTP_FLAG_DTMF_ON) {
4369 rtp_session->stats.inbound.last_processed_seq = 0;
4370 } else if (flag == SWITCH_RTP_FLAG_PAUSE) {
4371 reset_jitter_seq(rtp_session);
4372 } else if (flag == SWITCH_RTP_FLAG_NOBLOCK && rtp_session->sock_input) {
4373 switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, FALSE0);
4374 }
4375}
4376
4377static void set_dtmf_delay(switch_rtp_t *rtp_session, uint32_t ms, uint32_t max_ms)
4378{
4379 int upsamp, max_upsamp;
4380
4381
4382 if (!max_ms) max_ms = ms;
4383
4384 upsamp = ms * (rtp_session->samples_per_second / 1000);
4385 max_upsamp = max_ms * (rtp_session->samples_per_second / 1000);
4386
4387 rtp_session->queue_delay = upsamp;
4388
4389 if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
4390 rtp_session->max_next_write_samplecount = rtp_session->timer.samplecount + max_upsamp;
4391 rtp_session->next_write_samplecount = rtp_session->timer.samplecount + upsamp;
4392 rtp_session->last_write_ts += upsamp;
4393 }
4394
4395 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 4395, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "Queue digit delay of %dms\n", ms);
4396}
4397
4398static void do_2833(switch_rtp_t *rtp_session)
4399{
4400 switch_frame_flag_t flags = 0;
4401 uint32_t samples = rtp_session->samples_per_interval;
4402
4403 if (!rtp_session->last_write_ts) {
4404 return;
4405 }
4406
4407 if (rtp_session->dtmf_data.out_digit_dur > 0) {
4408 int x, loops = 1;
4409
4410 rtp_session->dtmf_data.out_digit_sofar += samples;
4411 rtp_session->dtmf_data.out_digit_sub_sofar += samples;
4412
4413 if (rtp_session->dtmf_data.out_digit_sub_sofar > 0xFFFF) {
4414 rtp_session->dtmf_data.out_digit_sub_sofar = samples;
4415 rtp_session->dtmf_data.timestamp_dtmf += 0xFFFF;
4416 }
4417
4418 if (rtp_session->dtmf_data.out_digit_sofar >= rtp_session->dtmf_data.out_digit_dur) {
4419 rtp_session->dtmf_data.out_digit_packet[1] |= 0x80;
4420 loops = 3;
4421 }
4422
4423 rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8);
4424 rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar;
4425
4426 for (x = 0; x < loops; x++) {
4427 switch_size_t wrote = switch_rtp_write_manual(rtp_session,
4428 rtp_session->dtmf_data.out_digit_packet, 4, 0,
4429 rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags);
4430
4431 rtp_session->stats.outbound.raw_bytes += wrote;
4432 rtp_session->stats.outbound.dtmf_packet_count++;
4433
4434 if (loops == 1) {
4435 rtp_session->last_write_ts += samples;
4436
4437 if (rtp_session->rtp_bugs & RTP_BUG_SONUS_SEND_INVALID_TIMESTAMP_2833) {
4438 rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts;
4439 }
4440 }
4441
4442 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 4442, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "Send %s packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u\n",
4443 loops == 1 ? "middle" : "end", rtp_session->dtmf_data.out_digit,
4444 rtp_session->dtmf_data.timestamp_dtmf,
4445 rtp_session->dtmf_data.out_digit_sofar,
4446 rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts);
4447 }
4448
4449 if (loops != 1) {
4450 rtp_session->sending_dtmf = 0;
4451 rtp_session->need_mark = 1;
4452
4453 if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
4454 rtp_session->last_write_samplecount = rtp_session->timer.samplecount;
4455 }
4456
4457 rtp_session->dtmf_data.out_digit_dur = 0;
4458
4459 if (rtp_session->interdigit_delay) {
4460 set_dtmf_delay(rtp_session, rtp_session->interdigit_delay, rtp_session->interdigit_delay * 10);
4461 }
4462
4463 return;
4464 }
4465 }
4466
4467 if (!rtp_session->dtmf_data.out_digit_dur && rtp_session->dtmf_data.dtmf_queue && switch_queue_size(rtp_session->dtmf_data.dtmf_queue)) {
4468 void *pop;
4469
4470 if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
4471 if (rtp_session->timer.samplecount < rtp_session->next_write_samplecount) {
4472 return;
4473 }
4474
4475 if (rtp_session->timer.samplecount >= rtp_session->max_next_write_samplecount) {
4476 rtp_session->queue_delay = 0;
4477 }
4478
4479 } else if (rtp_session->queue_delay) {
4480 if (rtp_session->delay_samples >= rtp_session->samples_per_interval) {
4481 rtp_session->delay_samples -= rtp_session->samples_per_interval;
4482 } else {
4483 rtp_session->delay_samples = 0;
4484 }
4485
4486 if (!rtp_session->delay_samples) {
4487 rtp_session->queue_delay = 0;
4488 }
4489 }
4490
4491 if (rtp_session->queue_delay) {
4492 return;
4493 }
4494
4495
4496 if (!rtp_session->sending_dtmf) {
4497 rtp_session->sending_dtmf = 1;
4498 }
4499
4500 if (switch_queue_trypop(rtp_session->dtmf_data.dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
4501 switch_dtmf_t *rdigit = pop;
4502 switch_size_t wrote;
4503
4504 if (rdigit->digit == 'w') {
4505 set_dtmf_delay(rtp_session, 500, 0);
4506 free(rdigit);
4507 return;
4508 }
4509
4510 if (rdigit->digit == 'W') {
4511 set_dtmf_delay(rtp_session, 1000, 0);
4512 free(rdigit);
4513 return;
4514 }
4515
4516
4517
4518 memset(rtp_session->dtmf_data.out_digit_packet, 0, 4);
4519 rtp_session->dtmf_data.out_digit_sofar = samples;
4520 rtp_session->dtmf_data.out_digit_sub_sofar = samples;
4521 rtp_session->dtmf_data.out_digit_dur = rdigit->duration;
4522 rtp_session->dtmf_data.out_digit = rdigit->digit;
4523 rtp_session->dtmf_data.out_digit_packet[0] = (unsigned char) switch_char_to_rfc2833(rdigit->digit);
4524 rtp_session->dtmf_data.out_digit_packet[1] = 13;
4525 rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8);
4526 rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar;
4527
4528
4529 rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts + samples;
4530 rtp_session->last_write_ts = rtp_session->dtmf_data.timestamp_dtmf;
4531 rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
4532
4533 wrote = switch_rtp_write_manual(rtp_session,
4534 rtp_session->dtmf_data.out_digit_packet,
4535 4,
4536 rtp_session->rtp_bugs & RTP_BUG_CISCO_SKIP_MARK_BIT_2833 ? 0 : 1,
4537 rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags);
4538
4539
4540 rtp_session->stats.outbound.raw_bytes += wrote;
4541 rtp_session->stats.outbound.dtmf_packet_count++;
4542
4543 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 4543, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "Send start packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u\n",
4544 rtp_session->dtmf_data.out_digit,
4545 rtp_session->dtmf_data.timestamp_dtmf,
4546 rtp_session->dtmf_data.out_digit_sofar,
4547 rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts);
4548
4549 free(rdigit);
4550 }
4551 }
4552}
4553
4554SWITCH_DECLARE(void)__attribute__((visibility("default"))) void rtp_flush_read_buffer(switch_rtp_t *rtp_session, switch_rtp_flush_t flush)
4555{
4556
4557 if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] ||
4558 rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ||
4559 rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
4560 return;
4561 }
4562
4563
4564 if (switch_rtp_ready(rtp_session)) {
4565 rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
4566 rtp_session->flags[SWITCH_RTP_FLAG_FLUSH] = 1;
4567 reset_jitter_seq(rtp_session);
4568
4569 switch (flush) {
4570 case SWITCH_RTP_FLUSH_STICK:
4571 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_STICKY_FLUSH);
4572 break;
4573 case SWITCH_RTP_FLUSH_UNSTICK:
4574 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_STICKY_FLUSH);
4575 break;
4576 default:
4577 break;
4578 }
4579 }
4580}
4581
4582static int jb_valid(switch_rtp_t *rtp_session)
4583{
4584 if (rtp_session->ice.ice_user) {
4585 if (!rtp_session->ice.ready && rtp_session->ice.rready) {
4586 return 0;
4587 }
4588 }
4589
4590 if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) {
4591 return 0;
4592 }
4593
4594 return 1;
4595}
4596
4597
4598static void do_flush(switch_rtp_t *rtp_session, int force)
4599{
4600 int was_blocking = 0;
4601 switch_size_t bytes;
4602 uint32_t flushed = 0;
4603
4604 if (!switch_rtp_ready(rtp_session)) {
4605 return;
4606 }
4607 reset_jitter_seq(rtp_session);
4608
4609 if (!force) {
4610 if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] ||
4611 rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ||
4612 rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] ||
4613 rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]
4614 ) {
4615 return;
4616 }
4617 }
4618
4619 READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session->
reading++
;
4620
4621 if (switch_rtp_ready(rtp_session) ) {
4622
4623 if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
4624 goto end;
4625 }
4626
4627 if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) {
4628 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 4628, (const char*)(rtp_session->session)
,
4629 SWITCH_LOG_CONSOLE, "%s FLUSH\n",
4630 rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName"
4631 );
4632 }
4633
4634 if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK]) {
4635 was_blocking = 1;
4636 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
4637 switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, TRUE(!0));
4638 }
4639
4640 do {
4641 if (switch_rtp_ready(rtp_session)) {
4642 bytes = sizeof(rtp_msg_t);
4643 switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock_input, 0, (void *) &rtp_session->recv_msg, &bytes);
4644
4645 if (bytes) {
4646 int do_cng = 0;
4647
4648 /* Make sure to handle RFC2833 packets, even if we're flushing the packets */
4649 if (bytes > rtp_header_len12 && rtp_session->recv_te && rtp_session->recv_msg.header.pt == rtp_session->recv_te) {
4650 handle_rfc2833(rtp_session, bytes, &do_cng);
4651#ifdef DEBUG_2833
4652 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 4652, ((void*)0)
, SWITCH_LOG_ERROR, "*** RTP packet handled in flush loop %d ***\n", do_cng);
4653#endif
4654 }
4655
4656 flushed++;
4657
4658 rtp_session->stats.inbound.raw_bytes += bytes;
4659 rtp_session->stats.inbound.flush_packet_count++;
4660 rtp_session->stats.inbound.packet_count++;
4661 }
4662 } else {
4663 break;
4664 }
4665 } while (bytes > 0);
4666
4667#if 0
4668 if (rtp_session->jb && flushed) {
4669 stfu_n_sync(rtp_session->jb, flushed);
4670 reset_jitter_seq(rtp_session);
4671 }
4672#endif
4673
4674 if (was_blocking && switch_rtp_ready(rtp_session)) {
4675 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK);
4676 switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, FALSE0);
4677 }
4678 }
4679
4680 end:
4681
4682 READ_DEC(rtp_session)switch_mutex_unlock(rtp_session->read_mutex); rtp_session->
reading--
;
4683}
4684
4685static int check_recv_payload(switch_rtp_t *rtp_session)
4686{
4687 int ok = 1;
4688
4689 if (rtp_session->pmaps && *rtp_session->pmaps) {
4690 payload_map_t *pmap;
4691 ok = 0;
4692
4693 switch_mutex_lock(rtp_session->flag_mutex);
4694
4695 for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
4696 if (!pmap->negotiated) {
4697 continue;
4698 }
4699
4700 if (rtp_session->recv_msg.header.pt == pmap->pt) {
4701 ok = 1;
4702 }
4703 }
4704 switch_mutex_unlock(rtp_session->flag_mutex);
4705 }
4706
4707 return ok;
4708}
4709
4710#define return_cng_frame()do_cng = 1; goto timer_check do_cng = 1; goto timer_check
4711
4712static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags, switch_bool_t return_jb_packet)
4713{
4714 switch_status_t status = SWITCH_STATUS_FALSE;
4715 stfu_frame_t *jb_frame;
4716 uint32_t ts = 0;
4717 unsigned char *b = NULL((void*)0);
4718 int sync = 0;
4719 switch_time_t now;
4720 switch_size_t xcheck_jitter = 0;
4721
4722 switch_assert(bytes)((bytes) ? (void) (0) : __assert_fail ("bytes", "src/switch_rtp.c"
, 4722, __PRETTY_FUNCTION__))
;
4723 more:
4724
4725 *bytes = sizeof(rtp_msg_t);
4726 sync = 0;
4727
4728 status = switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock_input, 0, (void *) &rtp_session->recv_msg, bytes);
4729
4730 if (*bytes) {
4731 rtp_session->missed_count = 0;
4732 }
4733
4734 if (!rtp_session->jb || rtp_session->pause_jb || !jb_valid(rtp_session)) {
4735 if (*bytes > rtp_header_len12 && (rtp_session->recv_msg.header.version == 2 && check_recv_payload(rtp_session))) {
4736 xcheck_jitter = *bytes;
4737 check_jitter(rtp_session);
4738 }
4739 }
4740
4741 if (check_rtcp_and_ice(rtp_session) == -1) {
4742 return SWITCH_STATUS_GENERR;
4743 }
4744
4745 if (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
4746 goto udptl;
4747 }
4748
4749
4750 if (*bytes) {
4751 b = (unsigned char *) &rtp_session->recv_msg;
4752
4753 *flags &= ~SFF_PROXY_PACKET;
4754
4755 if (*b == 0 || *b == 1) {
4756 if (rtp_session->ice.ice_user) {
4757 handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, *bytes);
4758 }
4759 *bytes = 0;
4760 sync = 1;
4761 }
4762 }
4763
4764 if (rtp_session->dtls) {
4765
4766 if (rtp_session->rtcp_dtls && rtp_session->rtcp_dtls != rtp_session->dtls) {
4767 rtp_session->rtcp_dtls->bytes = 0;
4768 rtp_session->rtcp_dtls->data = NULL((void*)0);
4769 do_dtls(rtp_session, rtp_session->rtcp_dtls);
4770 }
4771
4772 rtp_session->dtls->bytes = 0;
4773
4774 if (*bytes) {
4775 char *b = (char *) &rtp_session->recv_msg;
4776
4777 if ((*b >= 20) && (*b <= 64)) {
4778 rtp_session->dtls->bytes = *bytes;
4779 rtp_session->dtls->data = (void *) &rtp_session->recv_msg;
4780 } else {
4781 rtp_session->dtls->bytes = 0;
4782 rtp_session->dtls->data = NULL((void*)0);
4783
4784 if (*b != 0 && *b != 1 && rtp_session->dtls->state != DS_READY) {
4785 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 4785, ((void*)0)
, SWITCH_LOG_DEBUG1,
4786 "Drop %s packet %ld bytes (dtls not ready!) b=%u\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", (long)*bytes, *b);
4787 *bytes = 0;
4788 }
4789
4790 }
4791 }
4792
4793 do_dtls(rtp_session, rtp_session->dtls);
4794
4795 if (rtp_session->dtls->bytes) {
4796 *bytes = 0;
4797 sync = 1;
4798 }
4799 }
4800
4801 if (status == SWITCH_STATUS_SUCCESS && *bytes) {
4802 if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
4803 *flags &= ~SFF_RTCP;
4804 if (!check_recv_payload(rtp_session) &&
4805 (!rtp_session->recv_te || rtp_session->recv_msg.header.pt != rtp_session->recv_te) &&
4806 (!rtp_session->cng_pt || rtp_session->recv_msg.header.pt != rtp_session->cng_pt) &&
4807 rtp_session->rtcp_recv_msg_p->header.version == 2 &&
4808 rtp_session->rtcp_recv_msg_p->header.type > 199 && rtp_session->rtcp_recv_msg_p->header.type < 208) { //rtcp muxed
4809 *flags |= SFF_RTCP;
4810 return SWITCH_STATUS_SUCCESS;
4811 }
4812 }
4813 }
4814
4815
4816 if (*bytes && rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) {
4817 const char *tx_host;
4818 const char *old_host;
4819 const char *my_host;
4820
4821 char bufa[30], bufb[30], bufc[30];
4822
4823
4824 tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr);
4825 old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
4826 my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
4827
4828 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session)SWITCH_CHANNEL_ID_LOG_CLEAN, "src/switch_rtp.c", (const char *
)__func__, 4828, switch_core_session_get_uuid((rtp_session->
session))
, SWITCH_LOG_CONSOLE,
4829 "R %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u m=%d\n",
4830 rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "No-Name",
4831 (long) *bytes,
4832 my_host, switch_sockaddr_get_port(rtp_session->local_addr),
4833 old_host, rtp_session->remote_port,
4834 tx_host, switch_sockaddr_get_port(rtp_session->from_addr),
4835 rtp_session->recv_msg.header.pt, ntohl(rtp_session->recv_msg.header.ts)(__extension__ ({ unsigned int __v, __x = (rtp_session->recv_msg
.header.ts); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
, rtp_session->recv_msg.header.m);
4836
4837 }
4838
4839
4840 if (sync) {
4841 if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
4842 switch_core_timer_sync(&rtp_session->timer);
4843 reset_jitter_seq(rtp_session);
4844 }
4845 rtp_session->hot_hits = 0;
4846 goto more;
4847 }
4848
4849
4850 udptl:
4851
4852 ts = 0;
4853 rtp_session->recv_msg.ebody = NULL((void*)0);
4854 now = switch_micro_time_now();
4855
4856 if (*bytes) {
4857 uint16_t seq = ntohs((uint16_t) rtp_session->recv_msg.header.seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t) rtp_session->recv_msg.header.seq); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
4858 ts = ntohl(rtp_session->recv_msg.header.ts)(__extension__ ({ unsigned int __v, __x = (rtp_session->recv_msg
.header.ts); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
;
4859
4860 if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
4861 rtp_session->recv_msg.header.version == 2 && rtp_session->recv_msg.header.x) { /* header extensions */
4862 uint16_t length;
4863
4864 rtp_session->recv_msg.ext = (switch_rtp_hdr_ext_t *) rtp_session->recv_msg.body;
4865 length = ntohs((uint16_t)rtp_session->recv_msg.ext->length)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t)rtp_session->recv_msg.ext->length); if
(__builtin_constant_p (__x)) __v = ((unsigned short int) (((
(__x) >> 8) & 0xff) | (((__x) & 0xff) << 8
))); else __asm__ ("rorw $8, %w0" : "=r" (__v) : "0" (__x) : "cc"
); __v; }))
;
4866
4867 if (length < SWITCH_RTP_MAX_BUF_LEN_WORDS4094) {
4868 rtp_session->recv_msg.ebody = rtp_session->recv_msg.body + (length * 4) + 4;
4869 }
4870 }
4871
4872
4873#ifdef DEBUG_MISSED_SEQ
4874 if (rtp_session->last_seq && rtp_session->last_seq+1 != seq) {
4875 //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]
4876 switch_size_t flushed_packets_diff = rtp_session->stats.inbound.flush_packet_count - rtp_session->last_flush_packet_count;
4877 switch_size_t num_missed = (switch_size_t)seq - (rtp_session->last_seq+1);
4878
4879 if (num_missed == 1) { /* We missed one packet */
4880 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 4880, ((void*)0)
, SWITCH_LOG_ERROR, "Missed one RTP frame with sequence [%d]%s. Time since last read [%ld]\n",
4881 rtp_session->last_seq+1, (flushed_packets_diff == 1) ? " (flushed by FS)" : " (missed)",
4882 rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
4883 } else { /* We missed multiple packets */
4884 if (flushed_packets_diff == 0) {
4885 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 4885, ((void*)0)
, SWITCH_LOG_ERROR,
4886 "Missed %ld RTP frames from sequence [%d] to [%d] (missed). Time since last read [%ld]\n",
4887 num_missed, rtp_session->last_seq+1, seq-1,
4888 rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
4889 } else if (flushed_packets_diff == num_missed) {
4890 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 4890, ((void*)0)
, SWITCH_LOG_ERROR,
4891 "Missed %ld RTP frames from sequence [%d] to [%d] (flushed by FS). Time since last read [%ld]\n",
4892 num_missed, rtp_session->last_seq+1, seq-1,
4893 rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
4894 } else if (num_missed > flushed_packets_diff) {
4895 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 4895, ((void*)0)
, SWITCH_LOG_ERROR,
4896 "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS, %ld packets missed)."
4897 " Time since last read [%ld]\n",
4898 num_missed, rtp_session->last_seq+1, seq-1,
4899 flushed_packets_diff, num_missed-flushed_packets_diff,
4900 rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
4901 } else {
4902 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 4902, ((void*)0)
, SWITCH_LOG_ERROR,
4903 "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS). Time since last read [%ld]\n",
4904 num_missed, rtp_session->last_seq+1, seq-1,
4905 flushed_packets_diff, rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
4906 }
4907 }
4908
4909 }
4910#endif
4911 rtp_session->last_seq = seq;
4912
4913
4914 rtp_session->last_flush_packet_count = rtp_session->stats.inbound.flush_packet_count;
4915
4916
4917 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && now - rtp_session->last_read_time > 5000000) {
4918 switch_rtp_video_refresh(rtp_session);
4919 }
4920
4921 rtp_session->last_read_time = now;
4922 }
4923
4924 if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
4925 *bytes && (!rtp_session->recv_te || rtp_session->recv_msg.header.pt != rtp_session->recv_te) &&
4926 ts && !rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session) && ts == rtp_session->last_cng_ts) {
4927 /* we already sent this frame..... */
4928 *bytes = 0;
4929 return SWITCH_STATUS_SUCCESS;
4930 }
4931
4932 if (*bytes) {
4933 rtp_session->stats.inbound.raw_bytes += *bytes;
4934 if (rtp_session->recv_te && rtp_session->recv_msg.header.pt == rtp_session->recv_te) {
4935 rtp_session->stats.inbound.dtmf_packet_count++;
4936 } else if (rtp_session->cng_pt && (rtp_session->recv_msg.header.pt == rtp_session->cng_pt || rtp_session->recv_msg.header.pt == 13)) {
4937 rtp_session->stats.inbound.cng_packet_count++;
4938 } else {
4939 rtp_session->stats.inbound.media_packet_count++;
4940 rtp_session->stats.inbound.media_bytes += *bytes;
4941 }
4942
4943 rtp_session->stats.inbound.packet_count++;
4944 rtcp_stats(rtp_session);
4945
4946
4947 if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
4948#ifdef ENABLE_ZRTP
4949 /* ZRTP Recv */
4950 if (zrtp_on) {
4951
4952 unsigned int sbytes = (int) *bytes;
4953 zrtp_status_t stat = 0;
4954
4955 stat = zrtp_process_srtp(rtp_session->zrtp_stream, (void *) &rtp_session->recv_msg, &sbytes);
4956
4957 switch (stat) {
4958 case zrtp_status_ok:
4959 *bytes = sbytes;
4960 break;
4961 case zrtp_status_drop:
4962 /* switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */
4963 *bytes = 0;
4964 return SWITCH_STATUS_SUCCESS;
4965 case zrtp_status_fail:
4966 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 4966, ((void*)0)
, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
4967 return SWITCH_STATUS_FALSE;
4968 default:
4969 break;
4970 }
4971 }
4972#endif
4973
4974#ifdef ENABLE_SRTP1
4975 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->recv_msg.header.version == 2 &&
4976 (check_recv_payload(rtp_session) ||
4977 (rtp_session->recv_te && rtp_session->recv_msg.header.pt == rtp_session->recv_te) ||
4978 (rtp_session->cng_pt && rtp_session->recv_msg.header.pt == rtp_session->cng_pt))) {
4979 //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->recv_msg.header.version == 2)) {
4980 int sbytes = (int) *bytes;
4981 err_status_t stat = 0;
4982
4983 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET]) {
4984 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV_RESET);
4985 srtp_dealloc(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]);
4986 rtp_session->recv_ctx[rtp_session->srtp_idx_rtp] = NULL((void*)0);
4987 if ((stat = srtp_create(&rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_policy[rtp_session->srtp_idx_rtp]))) {
4988
4989 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 4989, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP RECV\n");
4990 return SWITCH_STATUS_FALSE;
4991 } else {
4992
4993 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 4993, (const char*)(rtp_session->session)
, SWITCH_LOG_INFO, "RE-Activating Secure RTP RECV\n");
4994 rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
4995 }
4996 }
4997
4998 if (!(*flags & SFF_PLC)) {
4999 stat = srtp_unprotect(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes);
5000 }
5001
5002 if (stat && rtp_session->recv_msg.header.pt != rtp_session->recv_te && rtp_session->recv_msg.header.pt != rtp_session->cng_pt) {
5003 int errs = ++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp];
5004 if (stat != 10) {
5005 char *msg;
5006 if (stat == err_status_replay_fail) msg="replay check failed";
5007 else if (stat == err_status_auth_fail) msg="auth check failed";
5008 else msg="";
5009 if (errs >= MAX_SRTP_ERRS100) {
5010 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
5011 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5011, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR,
5012 "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
5013 rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", stat, msg, (long)*bytes, errs);
5014 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5014, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR,
5015 "Ending call due to SRTP error\n");
5016 switch_channel_hangup(channel, SWITCH_CAUSE_SRTP_READ_ERROR)switch_channel_perform_hangup(channel, "src/switch_rtp.c", (const
char *)__func__, 5016, SWITCH_CAUSE_SRTP_READ_ERROR)
;
5017 } else if (errs >= WARN_SRTP_ERRS10 && !(errs % WARN_SRTP_ERRS10)) {
5018 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5018, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING,
5019 "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
5020 rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", stat, msg, (long)*bytes, errs);
5021 }
5022 }
5023 sbytes = 0;
5024 } else {
5025 rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
5026 }
5027
5028 *bytes = sbytes;
5029 }
5030#endif
5031 }
5032 }
5033
5034 if ((rtp_session->recv_te && rtp_session->recv_msg.header.pt == rtp_session->recv_te) ||
5035 (*bytes < rtp_header_len12 && *bytes > 0) ||
5036 rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
5037 return SWITCH_STATUS_SUCCESS;
5038 }
5039
5040 if (ts) {
5041 rtp_session->last_read_ts = ts;
5042 }
5043
5044 if (rtp_session->flags[SWITCH_RTP_FLAG_BYTESWAP] && check_recv_payload(rtp_session)) {
5045 switch_swap_linear((int16_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg
.ebody : rtp_session->recv_msg.body)
, (int) *bytes - rtp_header_len12);
5046 }
5047
5048 if (rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB]) {
5049 rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB] = 0;
5050 if (rtp_session->jb) {
5051 stfu_n_destroy(&rtp_session->jb);
5052 }
5053 }
5054
5055
5056 if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session) && rtp_session->recv_msg.header.version == 2 && *bytes) {
5057 if (rtp_session->recv_msg.header.m && rtp_session->recv_msg.header.pt != rtp_session->recv_te &&
5058 !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !(rtp_session->rtp_bugs & RTP_BUG_IGNORE_MARK_BIT)) {
5059 stfu_n_reset(rtp_session->jb)_stfu_n_reset(rtp_session->jb, "src/switch_rtp.c", __FUNCTION__
, 5059)
;
5060 }
5061
5062 if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
5063 switch_core_timer_sync(&rtp_session->timer);
5064 reset_jitter_seq(rtp_session);
5065 }
5066
5067 if (stfu_n_eat(rtp_session->jb, rtp_session->last_read_ts,stfu_n_add_data(rtp_session->jb, rtp_session->last_read_ts
, (__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t) rtp_session->recv_msg.header.seq); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; })), rtp_session->
recv_msg.header.pt, (char *) (rtp_session->recv_msg.ebody ?
rtp_session->recv_msg.ebody : rtp_session->recv_msg.body
), *bytes - 12, rtp_session->timer.samplecount, 0)
5068 ntohs((uint16_t) rtp_session->recv_msg.header.seq),stfu_n_add_data(rtp_session->jb, rtp_session->last_read_ts
, (__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t) rtp_session->recv_msg.header.seq); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; })), rtp_session->
recv_msg.header.pt, (char *) (rtp_session->recv_msg.ebody ?
rtp_session->recv_msg.ebody : rtp_session->recv_msg.body
), *bytes - 12, rtp_session->timer.samplecount, 0)
5069 rtp_session->recv_msg.header.pt,stfu_n_add_data(rtp_session->jb, rtp_session->last_read_ts
, (__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t) rtp_session->recv_msg.header.seq); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; })), rtp_session->
recv_msg.header.pt, (char *) (rtp_session->recv_msg.ebody ?
rtp_session->recv_msg.ebody : rtp_session->recv_msg.body
), *bytes - 12, rtp_session->timer.samplecount, 0)
5070 RTP_BODY(rtp_session), *bytes - rtp_header_len, rtp_session->timer.samplecount)stfu_n_add_data(rtp_session->jb, rtp_session->last_read_ts
, (__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t) rtp_session->recv_msg.header.seq); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; })), rtp_session->
recv_msg.header.pt, (char *) (rtp_session->recv_msg.ebody ?
rtp_session->recv_msg.ebody : rtp_session->recv_msg.body
), *bytes - 12, rtp_session->timer.samplecount, 0)
== STFU_ITS_TOO_LATE) {
5071
5072 goto more;
5073 }
5074
5075 status = SWITCH_STATUS_FALSE;
5076 *bytes = 0;
5077
5078 if (!return_jb_packet) {
5079 return status;
5080 }
5081
5082 }
5083
5084 if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
5085 if ((jb_frame = stfu_n_read_a_frame(rtp_session->jb))) {
5086 memcpy(RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg
.ebody : rtp_session->recv_msg.body)
, jb_frame->data, jb_frame->dlen);
5087
5088 if (jb_frame->plc) {
5089 (*flags) |= SFF_PLC;
5090 } else {
5091 rtp_session->stats.inbound.jb_packet_count++;
5092 }
5093 *bytes = jb_frame->dlen + rtp_header_len12;
5094 rtp_session->recv_msg.header.version = 2;
5095 rtp_session->recv_msg.header.x = 0;
5096 rtp_session->recv_msg.header.ts = htonl(jb_frame->ts)(__extension__ ({ unsigned int __v, __x = (jb_frame->ts); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
5097 rtp_session->recv_msg.header.pt = jb_frame->pt;
5098 rtp_session->recv_msg.header.seq = htons(jb_frame->seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) (jb_frame->seq); if (__builtin_constant_p (__x)) __v
= ((unsigned short int) ((((__x) >> 8) & 0xff) | (
((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
5099 status = SWITCH_STATUS_SUCCESS;
5100 if (!xcheck_jitter) {
5101 check_jitter(rtp_session);
5102 xcheck_jitter = *bytes;
5103 }
5104
5105 }
5106 }
5107
5108 return status;
5109}
5110
5111static switch_status_t process_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes)
5112{
5113 switch_status_t status = SWITCH_STATUS_FALSE;
5114 switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
5115 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5115, (const char*)(session)
, SWITCH_LOG_DEBUG10,"Received an RTCP packet of length %" SWITCH_SIZE_T_FMT"ld" " bytes\n", *bytes);
5116 if (rtp_session->rtcp_recv_msg.header.version == 2) {
5117 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5117, (const char*)(session)
, SWITCH_LOG_DEBUG10,"RTCP packet type is %d\n", rtp_session->rtcp_recv_msg.header.type);
5118
5119 if (rtp_session->rtcp_recv_msg.header.type == 200 || rtp_session->rtcp_recv_msg.header.type == 201) {
5120 struct switch_rtcp_report_block *report_block;
5121 switch_time_t now;
5122 switch_time_exp_t now_hr;
5123 uint32_t sec, ntp_sec, ntp_usec, lsr_now;
5124 uint32_t lsr;
5125 uint32_t packet_ssrc;
5126 now = switch_time_now(); /* number of microseconds since 00:00:00 january 1, 1970 UTC */
5127 sec = (uint32_t)(now/1000000); /* converted to second (NTP most significant bits) */
5128 ntp_sec = sec+NTP_TIME_OFFSET2208988800UL; /* 32bits most significant */
5129 ntp_usec = (uint32_t)(now - (sec*1000000)); /* micro seconds */
5130 lsr_now = (uint32_t)(ntp_usec*0.065536) | (ntp_sec&0x0000ffff)<<16; // 0.065536 is used for convertion from useconds
5131
5132 if (rtp_session->rtcp_recv_msg.header.type == 200) { /* Sender report */
5133 struct switch_rtcp_sender_report* sr = (struct switch_rtcp_sender_report*)rtp_session->rtcp_recv_msg.body;
5134 report_block = &sr->report_block;
5135 rtp_session->stats.rtcp.packet_count += ntohl(sr->sender_info.pc)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.pc); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
5136 rtp_session->stats.rtcp.octet_count += ntohl(sr->sender_info.oc)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.oc); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
5137 packet_ssrc = sr->ssrc;
5138 /* Extracting LSR from NTP timestamp and save it */
5139 lsr = (ntohl(sr->sender_info.ntp_lsw)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.ntp_lsw); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
&0xffff0000)>>16 | (ntohl(sr->sender_info.ntp_msw)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.ntp_msw); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
&0x0000ffff)<<16; /* The middle 32 bits out of 64 in the NTP timestamp */
5140 rtp_session->stats.rtcp.last_recv_lsr_peer = htonl(lsr)(__extension__ ({ unsigned int __v, __x = (lsr); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
; /* Save it include it in the next SR */
5141 rtp_session->stats.rtcp.last_recv_lsr_local = lsr_now; /* Save it to calculate DLSR when generating next SR */
5142 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5142, (const char*)(session)
, SWITCH_LOG_DEBUG10,"Received a SR with %d report blocks, " \
5143 "length in words = %d, " \
5144 "SSRC = 0x%X, " \
5145 "NTP MSW = %u, " \
5146 "NTP LSW = %u, " \
5147 "RTP timestamp = %u, " \
5148 "Sender Packet Count = %u, " \
5149 "Sender Octet Count = %u\n",
5150 rtp_session->rtcp_recv_msg.header.count,
5151 ntohs((uint16_t)rtp_session->rtcp_recv_msg.header.length)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t)rtp_session->rtcp_recv_msg.header.length)
; if (__builtin_constant_p (__x)) __v = ((unsigned short int)
((((__x) >> 8) & 0xff) | (((__x) & 0xff) <<
8))); else __asm__ ("rorw $8, %w0" : "=r" (__v) : "0" (__x) :
"cc"); __v; }))
,
5152 ntohl(sr->ssrc)(__extension__ ({ unsigned int __v, __x = (sr->ssrc); if (
__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000) >>
24) | (((__x) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00
) << 8) | (((__x) & 0x000000ff) << 24)); else
__asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
,
5153 ntohl(sr->sender_info.ntp_msw)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.ntp_msw); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
,
5154 ntohl(sr->sender_info.ntp_lsw)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.ntp_lsw); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
,
5155 ntohl(sr->sender_info.ts)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.ts); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
,
5156 ntohl(sr->sender_info.pc)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.pc); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
,
5157 ntohl(sr->sender_info.oc)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.oc); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
);
5158 } else { /* Receiver report */
5159 struct switch_rtcp_receiver_report* rr = (struct switch_rtcp_receiver_report*)rtp_session->rtcp_recv_msg.body;
5160 report_block = &rr->report_block;
5161 packet_ssrc = rr->ssrc;
5162 }
5163
5164 /* Currently in passthru mode RTT will not be accurate, some work as to be done (something like mapping the NTP timestamp with a local one) to have RTT from both legs */
5165 if (report_block->lsr && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
5166 switch_time_exp_gmt(&now_hr,now);
5167 /* Calculating RTT = A - DLSR - LSR */
5168 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5168, (const char*)(session)
, SWITCH_LOG_NOTICE,
5169 "Receiving an RTCP packet[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[%u]"
5170 "RTT[%f] A[%u] - DLSR[%u] - LSR[%u]\n",
5171 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,
5172 ntohl(packet_ssrc)(__extension__ ({ unsigned int __v, __x = (packet_ssrc); if (
__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000) >>
24) | (((__x) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00
) << 8) | (((__x) & 0x000000ff) << 24)); else
__asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
, (double)(lsr_now - ntohl(report_block->dlsr)(__extension__ ({ unsigned int __v, __x = (report_block->dlsr
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
- ntohl(report_block->lsr)(__extension__ ({ unsigned int __v, __x = (report_block->lsr
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
)/65536,
5173 lsr_now, ntohl(report_block->dlsr)(__extension__ ({ unsigned int __v, __x = (report_block->dlsr
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
, ntohl(report_block->lsr)(__extension__ ({ unsigned int __v, __x = (report_block->lsr
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
);
5174 }
5175
5176 rtp_session->rtcp_fresh_frame = 1;
5177 rtp_session->stats.rtcp.peer_ssrc = ntohl(packet_ssrc)(__extension__ ({ unsigned int __v, __x = (packet_ssrc); if (
__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000) >>
24) | (((__x) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00
) << 8) | (((__x) & 0x000000ff) << 24)); else
__asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
5178 status = SWITCH_STATUS_SUCCESS;
5179 }
5180 } else {
5181 if (rtp_session->rtcp_recv_msg_p->header.version != 2) {
5182 if (rtp_session->rtcp_recv_msg_p->header.version == 0) {
5183 if (rtp_session->ice.ice_user) {
5184 handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) rtp_session->rtcp_recv_msg_p, *bytes);
5185 }
5186 } else {
5187
5188 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5188, (const char*)(rtp_session->session)
,
5189 SWITCH_LOG_DEBUG, "Received an unsupported RTCP packet version %d\nn", rtp_session->rtcp_recv_msg_p->header.version);
5190 }
5191 }
5192
5193 status = SWITCH_STATUS_SUCCESS;
5194 }
5195
5196 return status;
5197}
5198
5199
5200
5201
5202static switch_status_t read_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags)
5203{
5204 switch_status_t status = SWITCH_STATUS_FALSE;
5205
5206 if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
5207 return SWITCH_STATUS_FALSE;
5208 }
5209
5210 switch_assert(bytes)((bytes) ? (void) (0) : __assert_fail ("bytes", "src/switch_rtp.c"
, 5210, __PRETTY_FUNCTION__))
;
5211
5212 *bytes = sizeof(rtcp_msg_t);
5213
5214 if ((status = switch_socket_recvfrom(rtp_session->rtcp_from_addr, rtp_session->rtcp_sock_input, 0, (void *) rtp_session->rtcp_recv_msg_p, bytes))
5215 != SWITCH_STATUS_SUCCESS) {
5216 *bytes = 0;
5217 }
5218
5219 if (rtp_session->rtcp_dtls) {
5220 char *b = (char *) &rtp_session->rtcp_recv_msg;
5221
5222 if (*b == 0 || *b == 1) {
5223 if (rtp_session->rtcp_ice.ice_user) {
5224 handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) &rtp_session->rtcp_recv_msg, *bytes);
5225 }
5226 *bytes = 0;
5227 }
5228
5229 if (*bytes && (*b >= 20) && (*b <= 64)) {
5230 rtp_session->rtcp_dtls->bytes = *bytes;
5231 rtp_session->rtcp_dtls->data = (void *) &rtp_session->rtcp_recv_msg;
5232 } else {
5233 rtp_session->rtcp_dtls->bytes = 0;
5234 rtp_session->rtcp_dtls->data = NULL((void*)0);
5235 }
5236
5237 do_dtls(rtp_session, rtp_session->rtcp_dtls);
5238
5239
5240 if (rtp_session->rtcp_dtls->bytes) {
5241 *bytes = 0;
5242 }
5243 }
5244
5245
5246
5247#ifdef ENABLE_SRTP1
5248 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->rtcp_recv_msg_p->header.version == 2) {
5249 //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->rtcp_recv_msg_p->header.version == 2)) {
5250 int sbytes = (int) *bytes;
5251 err_status_t stat = 0;
5252
5253
5254 if ((stat = srtp_unprotect_rtcp(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes))) {
5255 //++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++;
5256 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5256, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "RTCP UNPROTECT ERR\n");
5257 } else {
5258 //rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
5259 }
5260
5261 *bytes = sbytes;
5262
5263 }
5264#endif
5265
5266
5267#ifdef ENABLE_ZRTP
5268 if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && rtp_session->rtcp_recv_msg_p->header.version == 2) {
5269 /* ZRTP Recv */
5270 if (bytes) {
5271 unsigned int sbytes = (int) *bytes;
5272 zrtp_status_t stat = 0;
5273
5274 stat = zrtp_process_srtcp(rtp_session->zrtp_stream, (void *) rtp_session->rtcp_recv_msg_p, &sbytes);
5275
5276 switch (stat) {
5277 case zrtp_status_ok:
5278 *bytes = sbytes;
5279 break;
5280 case zrtp_status_drop:
5281 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 5281, ((void*)0)
, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
5282 *bytes = 0;
5283 break;
5284 case zrtp_status_fail:
5285 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 5285, ((void*)0)
, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
5286 *bytes = 0;
5287 break;
5288 default:
5289 break;
5290 }
5291 }
5292 }
5293#endif
5294
5295
5296 if (*bytes) {
5297 return process_rtcp_packet(rtp_session, bytes);
5298 }
5299
5300 return status;
5301}
5302
5303static int using_ice(switch_rtp_t *rtp_session)
5304{
5305 if (rtp_session->ice.ice_user || rtp_session->rtcp_ice.ice_user) {
5306 return 1;
5307 }
5308
5309 return 0;
5310}
5311
5312static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_type,
5313 payload_map_t **pmapP, switch_frame_flag_t *flags, switch_io_flag_t io_flags)
5314{
5315
5316 switch_channel_t *channel = NULL((void*)0);
5317 switch_size_t bytes = 0;
5318 switch_size_t rtcp_bytes = 0;
5319 switch_status_t status = SWITCH_STATUS_SUCCESS, poll_status = SWITCH_STATUS_SUCCESS;
5320 switch_status_t rtcp_status = SWITCH_STATUS_SUCCESS, rtcp_poll_status = SWITCH_STATUS_SUCCESS;
5321 int check = 0;
5322 int ret = -1;
5323 int sleep_mss = 1000;
5324 int poll_sec = 5;
5325 int poll_loop = 0;
5326 int fdr = 0;
5327 int rtcp_fdr = 0;
5328 int hot_socket = 0;
5329 int read_loops = 0;
5330 int slept = 0;
5331
5332 if (!switch_rtp_ready(rtp_session)) {
5333 return -1;
5334 }
5335
5336 if (rtp_session->session) {
5337 channel = switch_core_session_get_channel(rtp_session->session);
5338 }
5339
5340 if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
5341 sleep_mss = rtp_session->timer.interval * 1000;
5342 }
5343
5344 READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session->
reading++
;
5345
5346
5347
5348 while (switch_rtp_ready(rtp_session)) {
5349 int do_cng = 0;
5350 int read_pretriggered = 0;
5351 int has_rtcp = 0;
5352
5353 bytes = 0;
5354
5355 if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
5356 !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] &&
5357 !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
5358 !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
5359 rtp_session->read_pollfd) {
5360
5361 if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
5362 while (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
5363 status = read_rtp_packet(rtp_session, &bytes, flags, SWITCH_FALSE);
5364
5365 if (status == SWITCH_STATUS_GENERR) {
5366 ret = -1;
5367 goto end;
5368 }
5369
5370 if ((*flags & SFF_RTCP)) {
5371 *flags &= ~SFF_RTCP;
5372 has_rtcp = 1;
5373 read_pretriggered = 0;
5374 goto rtcp;
5375 }
5376
5377 if (status != SWITCH_STATUS_FALSE) {
5378 read_pretriggered = 1;
5379 break;
5380 }
5381 }
5382
5383 } else if ((rtp_session->flags[SWITCH_RTP_FLAG_AUTOFLUSH] || rtp_session->flags[SWITCH_RTP_FLAG_STICKY_FLUSH])) {
5384
5385 if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
5386 status = read_rtp_packet(rtp_session, &bytes, flags, SWITCH_FALSE);
5387 if (status == SWITCH_STATUS_GENERR) {
5388 ret = -1;
5389 goto end;
5390 }
5391 if ((*flags & SFF_RTCP)) {
5392 *flags &= ~SFF_RTCP;
5393 has_rtcp = 1;
5394 read_pretriggered = 0;
5395 goto rtcp;
5396 }
5397
5398 /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Initial (%i) %d\n", status, bytes); */
5399 if (status != SWITCH_STATUS_FALSE) {
5400 read_pretriggered = 1;
5401 }
5402
5403 if (bytes) {
5404 if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
5405 rtp_session->hot_hits++;//+= rtp_session->samples_per_interval;
5406
5407 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5407, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG10, "%s Hot Hit %d\n",
5408 rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
,
5409 rtp_session->hot_hits);
5410 } else {
5411 rtp_session->hot_hits = 0;
5412 }
5413 }
5414
5415 if (rtp_session->hot_hits > 1 && !rtp_session->sync_packets) {// >= (rtp_session->samples_per_second * 30)) {
5416 hot_socket = 1;
5417 }
5418 } else {
5419 rtp_session->hot_hits = 0;
5420 }
5421 }
5422
5423 if (hot_socket && (rtp_session->hot_hits % 10) != 0) {
5424 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5424, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG10, "%s timer while HOT\n", rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
);
5425 switch_core_timer_next(&rtp_session->timer);
5426 } else if (hot_socket) {
5427 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5427, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG10, "%s skip timer once\n", rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
);
5428 rtp_session->sync_packets++;
5429 switch_core_timer_sync(&rtp_session->timer);
5430 reset_jitter_seq(rtp_session);
5431 } else {
5432
5433 if (rtp_session->sync_packets) {
5434
5435 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5435, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG10,
5436 "%s Auto-Flush catching up %d packets (%d)ms.\n",
5437 rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
,
5438 rtp_session->sync_packets, (rtp_session->ms_per_packet * rtp_session->sync_packets) / 1000);
5439 if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) {
5440 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5440, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "%s syncing %d %s packet(s)\n",
5441 rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
,
5442 rtp_session->sync_packets, rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio");
5443
5444 rtp_session->stats.inbound.flaws += rtp_session->sync_packets;
5445 }
5446
5447 switch_core_timer_sync(&rtp_session->timer);
5448 reset_jitter_seq(rtp_session);
5449 rtp_session->hot_hits = 0;
5450 } else {
5451 if (slept) {
5452 switch_cond_next();
5453 } else {
5454 switch_core_timer_next(&rtp_session->timer);
5455 slept++;
5456 }
5457
5458 }
5459
5460 rtp_session->sync_packets = 0;
5461 }
5462 }
5463
5464 rtp_session->stats.read_count++;
5465
5466 recvfrom:
5467
5468 if (!read_pretriggered) {
5469 bytes = 0;
5470 }
5471 read_loops++;
5472 //poll_loop = 0;
5473
5474 if (!switch_rtp_ready(rtp_session)) {
5475 break;
5476 }
5477
5478 if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->read_pollfd) {
5479 int pt = poll_sec * 1000000;
5480
5481 do_2833(rtp_session);
5482
5483 if (rtp_session->dtmf_data.out_digit_dur > 0 || rtp_session->dtmf_data.in_digit_sanity || rtp_session->sending_dtmf ||
5484 switch_queue_size(rtp_session->dtmf_data.dtmf_queue) || switch_queue_size(rtp_session->dtmf_data.dtmf_inqueue)) {
5485 pt = 20000;
5486 }
5487
5488
5489 if ((io_flags & SWITCH_IO_FLAG_NOBLOCK)) {
5490 pt = 0;
5491 }
5492
5493 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
5494 pt = 100000;
5495 }
5496
5497 if (using_ice(rtp_session)) {
5498 pt = 20000;
5499 }
5500
5501 poll_status = switch_poll(rtp_session->read_pollfd, 1, &fdr, pt);
5502
5503
5504 if (rtp_session->dtmf_data.out_digit_dur > 0) {
5505 return_cng_frame()do_cng = 1; goto timer_check;
5506 }
5507
5508 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->flags[SWITCH_RTP_FLAG_BREAK]) {
5509 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_BREAK);
5510 bytes = 0;
5511 reset_jitter_seq(rtp_session);
5512 return_cng_frame()do_cng = 1; goto timer_check;
5513 }
5514
5515 }
5516
5517
5518 if (poll_status == SWITCH_STATUS_SUCCESS) {
5519 if (read_pretriggered) {
5520 read_pretriggered = 0;
5521 } else {
5522
5523 status = read_rtp_packet(rtp_session, &bytes, flags, SWITCH_TRUE);
5524
5525 if (status == SWITCH_STATUS_GENERR) {
5526 ret = -1;
5527 goto end;
5528 }
5529
5530 if (rtp_session->max_missed_packets && read_loops == 1 && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5531 if (bytes) {
5532 rtp_session->missed_count = 0;
5533 } else if (++rtp_session->missed_count >= rtp_session->max_missed_packets) {
5534 ret = -2;
5535 goto end;
5536 }
5537 }
5538
5539 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5540 //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "Read bytes (%i) %ld\n", status, bytes);
5541
5542 if (bytes == 0) {
5543 if (check_rtcp_and_ice(rtp_session) == -1) {
5544 ret = -1;
5545 goto end;
5546 }
5547 // This is dumb
5548 //switch_rtp_video_refresh(rtp_session);
5549 goto rtcp;
5550 }
5551 }
5552
5553 if ((*flags & SFF_PROXY_PACKET)) {
5554 ret = (int) bytes;
5555 goto end;
5556 }
5557
5558 if ((*flags & SFF_RTCP)) {
5559 *flags &= ~SFF_RTCP;
5560 has_rtcp = 1;
5561 goto rtcp;
5562 }
5563
5564
5565 }
5566 poll_loop = 0;
5567 } else {
5568 if (!SWITCH_STATUS_IS_BREAK(poll_status)(poll_status == SWITCH_STATUS_BREAK || poll_status == 730035 ||
poll_status == 35 || poll_status == SWITCH_STATUS_INTR)
&& poll_status != SWITCH_STATUS_TIMEOUT) {
5569 char tmp[128] = "";
5570 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5570, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Poll failed with error: %d [%s]\n",
5571 poll_status, switch_strerror_r(poll_status, tmp, sizeof(tmp)));
5572 ret = -1;
5573 goto end;
5574 }
5575
5576 if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5577 rtp_session->missed_count += (poll_sec * 1000) / (rtp_session->ms_per_packet ? rtp_session->ms_per_packet / 1000 : 20);
5578 bytes = 0;
5579
5580 if (rtp_session->max_missed_packets) {
5581 if (rtp_session->missed_count >= rtp_session->max_missed_packets) {
5582 ret = -2;
5583 goto end;
5584 }
5585 }
5586 }
5587
5588 if (using_ice(rtp_session)) {
5589 if (check_rtcp_and_ice(rtp_session) == -1) {
5590 ret = -1;
5591 goto end;
5592 }
5593 }
5594
5595 if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) &&
5596 (rtp_session->dtmf_data.out_digit_dur == 0)) {
5597 return_cng_frame()do_cng = 1; goto timer_check;
5598 }
5599 }
5600
5601 rtcp:
5602
5603 if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
5604 rtcp_poll_status = SWITCH_STATUS_FALSE;
5605
5606 if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX] && has_rtcp) {
5607 if (rtp_session->rtcp_recv_msg_p->header.version == 2) { //rtcp muxed
5608 rtp_session->rtcp_from_addr = rtp_session->from_addr;
5609 rtcp_status = rtcp_poll_status = SWITCH_STATUS_SUCCESS;
5610 rtcp_bytes = bytes;
5611 }
5612
5613 has_rtcp = 0;
5614
5615 } else if (rtp_session->rtcp_read_pollfd) {
5616 rtcp_poll_status = switch_poll(rtp_session->rtcp_read_pollfd, 1, &rtcp_fdr, 0);
5617 }
5618
5619 if (rtcp_poll_status == SWITCH_STATUS_SUCCESS) {
5620
5621 if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
5622 rtcp_status = read_rtcp_packet(rtp_session, &rtcp_bytes, flags);
5623 }
5624
5625 if (rtcp_status == SWITCH_STATUS_SUCCESS) {
5626 switch_rtp_reset_media_timer(rtp_session);
5627
5628 if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] || rtp_session->rtcp_recv_msg_p->header.type == 206) {
5629 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
5630 const char *uuid = switch_channel_get_partner_uuid(channel);
5631
5632 if (uuid) {
5633 switch_core_session_t *other_session;
5634 switch_rtp_t *other_rtp_session = NULL((void*)0);
5635
5636 if ((other_session = switch_core_session_locate(uuid)switch_core_session_perform_locate(uuid, "src/switch_rtp.c", (
const char *)__func__, 5636)
)) {
5637 switch_channel_t *other_channel = switch_core_session_get_channel(other_session);
5638 if ((other_rtp_session = switch_channel_get_private(other_channel, "__rtcp_audio_rtp_session")) &&
5639 other_rtp_session->rtcp_sock_output &&
5640 switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_ENABLE_RTCP)) {
5641 other_rtp_session->rtcp_send_msg = rtp_session->rtcp_recv_msg;
5642
5643 if (rtp_session->rtcp_recv_msg_p->header.type == 206) {
5644 rtcp_ext_msg_t *extp = (rtcp_ext_msg_t *) rtp_session->rtcp_recv_msg_p;
5645 extp->header.recv_ssrc = htonl(other_rtp_session->stats.rtcp.peer_ssrc)(__extension__ ({ unsigned int __v, __x = (other_rtp_session->
stats.rtcp.peer_ssrc); if (__builtin_constant_p (__x)) __v = (
(((__x) & 0xff000000) >> 24) | (((__x) & 0x00ff0000
) >> 8) | (((__x) & 0x0000ff00) << 8) | (((__x
) & 0x000000ff) << 24)); else __asm__ ("bswap %0" :
"=r" (__v) : "0" (__x)); __v; }))
;
5646 }
5647
5648
5649#ifdef ENABLE_SRTP1
5650 if (switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_SECURE_SEND)) {
5651 int sbytes = (int) rtcp_bytes;
5652 int stat = srtp_protect_rtcp(other_rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &other_rtp_session->rtcp_send_msg.header, &sbytes);
5653 if (stat) {
5654 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5654, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
5655 }
5656 rtcp_bytes = sbytes;
5657
5658 }
5659#endif
5660
5661#ifdef ENABLE_ZRTP
5662 /* ZRTP Send */
5663 if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
5664 unsigned int sbytes = (unsigned int) bytes;
5665 zrtp_status_t stat = zrtp_status_fail;
5666
5667 stat = zrtp_process_rtcp(other_rtp_session->zrtp_stream, (void *) &other_rtp_session->rtcp_send_msg, &sbytes);
5668
5669 switch (stat) {
5670 case zrtp_status_ok:
5671 break;
5672 case zrtp_status_drop:
5673 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5673, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
5674 ret = (int) bytes;
5675 goto end;
5676 break;
5677 case zrtp_status_fail:
5678 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5678, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
5679 break;
5680 default:
5681 break;
5682 }
5683
5684 bytes = sbytes;
5685 }
5686#endif
5687 if (switch_socket_sendto(other_rtp_session->rtcp_sock_output, other_rtp_session->rtcp_remote_addr, 0,
5688 (const char*)&other_rtp_session->rtcp_send_msg, &rtcp_bytes ) != SWITCH_STATUS_SUCCESS) {
5689 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5689, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG,"RTCP packet not written\n");
5690 }
5691
5692
5693 }
5694 switch_core_session_rwunlock(other_session);
5695 }
5696 }
5697
5698 }
5699
5700 if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
5701 process_rtcp_packet(rtp_session, &bytes);
5702 ret = 1;
5703
5704 if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
5705 switch_core_timer_sync(&rtp_session->timer);
5706 reset_jitter_seq(rtp_session);
5707 }
5708
5709
5710 goto recvfrom;
5711 }
5712 }
5713 }
5714 }
5715
5716 if (bytes && rtp_session->recv_msg.header.version == 2 &&
5717 !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
5718 rtp_session->recv_msg.header.pt != 13 &&
5719 rtp_session->recv_msg.header.pt != rtp_session->recv_te &&
5720 (!rtp_session->cng_pt || rtp_session->recv_msg.header.pt != rtp_session->cng_pt)) {
5721 int accept_packet = 1;
5722
5723
5724 if (rtp_session->pmaps && *rtp_session->pmaps) {
5725 payload_map_t *pmap;
5726 accept_packet = 0;
5727
5728 switch_mutex_lock(rtp_session->flag_mutex);
5729 for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
5730
5731 if (!pmap->negotiated) {
5732 continue;
5733 }
5734
5735 if (rtp_session->recv_msg.header.pt == pmap->pt) {
5736 accept_packet = 1;
5737 if (pmapP) {
5738 *pmapP = pmap;
5739 }
5740 break;
5741 }
5742 }
5743 switch_mutex_unlock(rtp_session->flag_mutex);
5744 }
5745
5746 if (!accept_packet &&
5747 !(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PAYLOAD) && !(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PACKETS)) {
5748 /* drop frames of incorrect payload number and return CNG frame instead */
5749
5750 return_cng_frame()do_cng = 1; goto timer_check;
5751 }
5752 }
5753
5754 if (!bytes && (io_flags & SWITCH_IO_FLAG_NOBLOCK)) {
5755 rtp_session->missed_count = 0;
5756 ret = 0;
5757 goto end;
5758 }
5759
5760 check = !bytes;
5761
5762 if (rtp_session->flags[SWITCH_RTP_FLAG_FLUSH]) {
5763 if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5764 do_flush(rtp_session, SWITCH_FALSE);
5765 bytes = 0;
5766 }
5767 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH);
5768 }
5769
5770 if (rtp_session->flags[SWITCH_RTP_FLAG_BREAK] || (bytes && bytes == 4 && *((int *) &rtp_session->recv_msg) == UINT_MAX(2147483647 *2U +1U))) {
5771 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_BREAK);
5772
5773 if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] || !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] ||
5774 rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] ||
5775 (bytes && bytes < 5) || (!bytes && poll_loop)) {
5776 bytes = 0;
5777 reset_jitter_seq(rtp_session);
5778 return_cng_frame()do_cng = 1; goto timer_check;
5779 }
5780 }
5781
5782 if (bytes && bytes < 5) {
5783 continue;
5784 }
5785
5786 if (!bytes && poll_loop) {
5787 goto recvfrom;
5788 }
5789
5790 if (bytes && rtp_session->recv_msg.header.m && rtp_session->recv_msg.header.pt != rtp_session->recv_te &&
5791 !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !(rtp_session->rtp_bugs & RTP_BUG_IGNORE_MARK_BIT)) {
5792 rtp_flush_read_buffer(rtp_session, SWITCH_RTP_FLUSH_ONCE);
5793 }
5794
5795
5796 if (((rtp_session->cng_pt && rtp_session->recv_msg.header.pt == rtp_session->cng_pt) || rtp_session->recv_msg.header.pt == 13)) {
5797 *flags |= SFF_NOT_AUDIO;
5798 } else {
5799 *flags &= ~SFF_NOT_AUDIO; /* If this flag was already set, make sure to remove it when we get real audio */
5800 }
5801
5802
5803 /* ignore packets not meant for us unless the auto-adjust window is open */
5804 if (bytes) {
5805 if (rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ]) {
5806 if (((rtp_session->cng_pt && rtp_session->recv_msg.header.pt == rtp_session->cng_pt) || rtp_session->recv_msg.header.pt == 13)) {
5807 goto recvfrom;
5808
5809 }
5810 } else if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PACKETS) && !switch_cmp_addr(rtp_session->from_addr, rtp_session->remote_addr)) {
5811 goto recvfrom;
5812
5813 }
5814 }
5815
5816 if (bytes && rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ] && switch_sockaddr_get_port(rtp_session->from_addr)) {
5817 if (!switch_cmp_addr(rtp_session->from_addr, rtp_session->remote_addr)) {
5818 if (++rtp_session->autoadj_tally >= rtp_session->autoadj_threshold) {
5819 const char *err;
5820 uint32_t old = rtp_session->remote_port;
5821 const char *tx_host;
5822 const char *old_host;
5823 char bufa[30], bufb[30];
5824 char adj_port[6];
5825
5826 tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr);
5827 old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
5828
5829 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5829, (const char*)(rtp_session->session)
, SWITCH_LOG_INFO,
5830 "Auto Changing port from %s:%u to %s:%u\n", old_host, old, tx_host,
5831 switch_sockaddr_get_port(rtp_session->from_addr));
5832
5833 if (channel) {
5834 switch_channel_set_variable(channel, "remote_media_ip_reported", switch_channel_get_variable(channel, "remote_media_ip"))switch_channel_set_variable_var_check(channel, "remote_media_ip_reported"
, switch_channel_get_variable_dup(channel, "remote_media_ip",
SWITCH_TRUE, -1), SWITCH_TRUE)
;
5835 switch_channel_set_variable(channel, "remote_media_ip", tx_host)switch_channel_set_variable_var_check(channel, "remote_media_ip"
, tx_host, SWITCH_TRUE)
;
5836 switch_snprintf(adj_port, sizeof(adj_port), "%u", switch_sockaddr_get_port(rtp_session->from_addr));
5837 switch_channel_set_variable(channel, "remote_media_port_reported", switch_channel_get_variable(channel, "remote_media_port"))switch_channel_set_variable_var_check(channel, "remote_media_port_reported"
, switch_channel_get_variable_dup(channel, "remote_media_port"
, SWITCH_TRUE, -1), SWITCH_TRUE)
;
5838 switch_channel_set_variable(channel, "remote_media_port", adj_port)switch_channel_set_variable_var_check(channel, "remote_media_port"
, adj_port, SWITCH_TRUE)
;
5839 switch_channel_set_variable(channel, "rtp_auto_adjust", "true")switch_channel_set_variable_var_check(channel, "rtp_auto_adjust"
, "true", SWITCH_TRUE)
;
5840 }
5841 rtp_session->auto_adj_used = 1;
5842 switch_rtp_set_remote_address(rtp_session, tx_host, switch_sockaddr_get_port(rtp_session->from_addr), 0, SWITCH_FALSE, &err);
5843 if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
5844 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
5845 } else {
5846 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
5847 }
5848 if (rtp_session->ice.ice_user) {
5849 rtp_session->ice.addr = rtp_session->remote_addr;
5850 }
5851 }
5852 } else {
5853 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5853, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "Correct ip/port confirmed.\n");
5854 if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
5855 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
5856 } else {
5857 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
5858 }
5859 rtp_session->auto_adj_used = 0;
5860 }
5861 }
5862
5863 if (bytes && !(rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST) && rtp_session->autoadj_window) {
5864 if (--rtp_session->autoadj_window == 0) {
5865 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
5866 }
5867 }
5868
5869 if (bytes && (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL])) {
5870 /* Fast PASS! */
5871 *flags |= SFF_PROXY_PACKET;
5872
5873 if (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
5874#if 0
5875 if (rtp_session->recv_msg.header.version == 2 && check_recv_payload(rtp_session)) {
5876 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5876, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING,
5877 "Ignoring udptl packet of size of %ld bytes that looks strikingly like a RTP packet.\n", (long)bytes);
5878 bytes = 0;
5879 goto do_continue;
5880 }
5881#endif
5882 *flags |= SFF_UDPTL_PACKET;
5883 }
5884
5885 ret = (int) bytes;
5886 goto end;
5887 }
5888
5889 if (bytes) {
5890 rtp_session->missed_count = 0;
5891
5892 if (bytes < rtp_header_len12) {
5893 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5893, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING, "Ignoring invalid RTP packet size of %ld bytes.\n", (long)bytes);
5894 bytes = 0;
5895 goto do_continue;
5896 }
5897
5898 if (rtp_session->recv_msg.header.pt && (rtp_session->recv_msg.header.pt == rtp_session->cng_pt || rtp_session->recv_msg.header.pt == 13)) {
5899 return_cng_frame()do_cng = 1; goto timer_check;
5900 }
5901 }
5902
5903 if (check || bytes) {
5904 do_2833(rtp_session);
5905 }
5906
5907 if (bytes && rtp_session->recv_msg.header.version != 2) {
5908 uint8_t *data = (uint8_t *) RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg
.ebody : rtp_session->recv_msg.body)
;
5909
5910 if (rtp_session->recv_msg.header.version == 0) {
5911 if (rtp_session->ice.ice_user) {
5912 handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, bytes);
5913 goto recvfrom;
5914 }
5915 }
5916
5917 if (rtp_session->invalid_handler) {
5918 rtp_session->invalid_handler(rtp_session, rtp_session->sock_input, (void *) &rtp_session->recv_msg, bytes, rtp_session->from_addr);
5919 }
5920
5921 memset(data, 0, 2);
5922 data[0] = 65;
5923
5924 rtp_session->recv_msg.header.pt = (uint32_t) rtp_session->cng_pt ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD13;
5925 *flags |= SFF_CNG;
5926 *payload_type = (switch_payload_t) rtp_session->recv_msg.header.pt;
5927 ret = 2 + rtp_header_len12;
5928 goto end;
5929 } else if (bytes) {
5930 rtp_session->stats.inbound.period_packet_count++;
5931 }
5932
5933
5934 /* Handle incoming RFC2833 packets */
5935 switch (handle_rfc2833(rtp_session, bytes, &do_cng)) {
5936 case RESULT_GOTO_END:
5937 goto end;
5938 case RESULT_GOTO_RECVFROM:
5939 goto recvfrom;
5940 case RESULT_GOTO_TIMERCHECK:
5941 goto timer_check;
5942 case RESULT_CONTINUE:
5943 goto result_continue;
5944 }
5945
5946 result_continue:
5947 timer_check:
5948
5949 if (rtp_session->flags[SWITCH_RTP_FLAG_MUTE]) {
5950 do_cng++;
5951 }
5952
5953 if (do_cng) {
5954 uint8_t *data = (uint8_t *) RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg
.ebody : rtp_session->recv_msg.body)
;
5955
5956 do_2833(rtp_session);
5957
5958 if (rtp_session->last_cng_ts == rtp_session->last_read_ts + rtp_session->samples_per_interval) {
5959 rtp_session->last_cng_ts = 0;
5960 } else {
5961 rtp_session->last_cng_ts = rtp_session->last_read_ts + rtp_session->samples_per_interval;
5962 }
5963
5964 memset(data, 0, 2);
5965 data[0] = 65;
5966 rtp_session->recv_msg.header.pt = (uint32_t) rtp_session->cng_pt ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD13;
5967 *flags |= SFF_CNG;
5968 *payload_type = (switch_payload_t) rtp_session->recv_msg.header.pt;
5969 ret = 2 + rtp_header_len12;
5970 rtp_session->stats.inbound.skip_packet_count++;
5971 goto end;
5972 }
5973
5974
5975 if (check || (bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER])) {
5976 if (!bytes && rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { /* We're late! We're Late! */
5977 if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] && status == SWITCH_STATUS_BREAK) {
5978 switch_cond_next();
5979 continue;
5980 }
5981
5982
5983
5984 if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] && !rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] && !rtp_session->dtmf_data.in_digit_ts
5985 && rtp_session->cng_count > (rtp_session->one_second * 2) && rtp_session->jitter_lead > JITTER_LEAD_FRAMES10) {
5986
5987 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 5987, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG1, "%s %s timeout\n",
5988 rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel
(rtp_session->session)) : "-"
, rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio");
5989
5990 rtp_session->stats.inbound.flaws++;
5991 do_mos(rtp_session, SWITCH_FALSE);
5992 }
5993
5994 rtp_session->cng_count++;
5995 return_cng_frame()do_cng = 1; goto timer_check;
5996 }
5997 }
5998
5999 rtp_session->cng_count = 0;
6000
6001 if (status == SWITCH_STATUS_BREAK || bytes == 0) {
6002 if (!(io_flags & SWITCH_IO_FLAG_SINGLE_READ) && rtp_session->flags[SWITCH_RTP_FLAG_DATAWAIT]) {
6003 goto do_continue;
6004 }
6005 return_cng_frame()do_cng = 1; goto timer_check;
6006 }
6007
6008 if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->recv_msg.header.pt == 102) {
6009 rtp_session->recv_msg.header.pt = 97;
6010 }
6011
6012 break;
6013
6014 do_continue:
6015
6016 if (!bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
6017 switch_yield(sleep_mss)switch_sleep(sleep_mss);;
6018 }
6019
6020 }
6021
6022 if (switch_rtp_ready(rtp_session)) {
6023 *payload_type = (switch_payload_t) rtp_session->recv_msg.header.pt;
6024
6025 if (*payload_type == SWITCH_RTP_CNG_PAYLOAD13) {
6026 *flags |= SFF_CNG;
6027 }
6028
6029 ret = (int) bytes;
6030 } else {
6031 ret = -1;
6032 }
6033
6034 end:
6035
6036 READ_DEC(rtp_session)switch_mutex_unlock(rtp_session->read_mutex); rtp_session->
reading--
;
6037
6038 return ret;
6039}
6040
6041
6042SWITCH_DECLARE(switch_byte_t)__attribute__((visibility("default"))) switch_byte_t switch_rtp_check_auto_adj(switch_rtp_t *rtp_session)
6043{
6044 return rtp_session->auto_adj_used;
6045}
6046
6047SWITCH_DECLARE(switch_size_t)__attribute__((visibility("default"))) switch_size_t switch_rtp_has_dtmf(switch_rtp_t *rtp_session)
6048{
6049 switch_size_t has = 0;
6050
6051 if (switch_rtp_ready(rtp_session)) {
6052 switch_mutex_lock(rtp_session->dtmf_data.dtmf_mutex);
6053 has = switch_queue_size(rtp_session->dtmf_data.dtmf_inqueue);
6054 switch_mutex_unlock(rtp_session->dtmf_data.dtmf_mutex);
6055 }
6056
6057 return has;
6058}
6059
6060SWITCH_DECLARE(switch_size_t)__attribute__((visibility("default"))) switch_size_t switch_rtp_dequeue_dtmf(switch_rtp_t *rtp_session, switch_dtmf_t *dtmf)
6061{
6062 switch_size_t bytes = 0;
6063 switch_dtmf_t *_dtmf = NULL((void*)0);
6064 void *pop;
6065
6066 if (!switch_rtp_ready(rtp_session)) {
6067 return bytes;
6068 }
6069
6070 switch_mutex_lock(rtp_session->dtmf_data.dtmf_mutex);
6071 if (switch_queue_trypop(rtp_session->dtmf_data.dtmf_inqueue, &pop) == SWITCH_STATUS_SUCCESS) {
6072
6073 _dtmf = (switch_dtmf_t *) pop;
6074 *dtmf = *_dtmf;
6075 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 6075, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "RTP RECV DTMF %c:%d\n", dtmf->digit, dtmf->duration);
6076 bytes++;
6077 free(pop);
6078 }
6079 switch_mutex_unlock(rtp_session->dtmf_data.dtmf_mutex);
6080
6081 return bytes;
6082}
6083
6084SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_queue_rfc2833(switch_rtp_t *rtp_session, const switch_dtmf_t *dtmf)
6085{
6086
6087 switch_dtmf_t *rdigit;
6088
6089 if (!switch_rtp_ready(rtp_session)) {
6090 return SWITCH_STATUS_FALSE;
6091 }
6092
6093 if ((rdigit = malloc(sizeof(*rdigit))) != 0) {
6094 *rdigit = *dtmf;
6095 if (rdigit->duration < switch_core_min_dtmf_duration(0)) {
6096 rdigit->duration = switch_core_min_dtmf_duration(0);
6097 }
6098
6099 if ((switch_queue_trypush(rtp_session->dtmf_data.dtmf_queue, rdigit)) != SWITCH_STATUS_SUCCESS) {
6100 free(rdigit);
6101 return SWITCH_STATUS_FALSE;
6102 }
6103 } else {
6104 abort();
6105 }
6106
6107 return SWITCH_STATUS_SUCCESS;
6108}
6109
6110SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_queue_rfc2833_in(switch_rtp_t *rtp_session, const switch_dtmf_t *dtmf)
6111{
6112 switch_dtmf_t *rdigit;
6113
6114 if (!switch_rtp_ready(rtp_session)) {
6115 return SWITCH_STATUS_FALSE;
6116 }
6117
6118 if ((rdigit = malloc(sizeof(*rdigit))) != 0) {
6119 *rdigit = *dtmf;
6120 if (rdigit->duration < switch_core_min_dtmf_duration(0)) {
6121 rdigit->duration = switch_core_min_dtmf_duration(0);
6122 }
6123
6124 if ((switch_queue_trypush(rtp_session->dtmf_data.dtmf_inqueue, rdigit)) != SWITCH_STATUS_SUCCESS) {
6125 free(rdigit);
6126 return SWITCH_STATUS_FALSE;
6127 }
6128 } else {
6129 abort();
6130 }
6131
6132 return SWITCH_STATUS_SUCCESS;
6133}
6134
6135SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_read(switch_rtp_t *rtp_session, void *data, uint32_t *datalen,
6136 switch_payload_t *payload_type, switch_frame_flag_t *flags, switch_io_flag_t io_flags)
6137{
6138 int bytes = 0;
6139
6140 if (!switch_rtp_ready(rtp_session)) {
6141 return SWITCH_STATUS_FALSE;
6142 }
6143
6144 bytes = rtp_common_read(rtp_session, payload_type, NULL((void*)0), flags, io_flags);
6145
6146 if (bytes < 0) {
6147 *datalen = 0;
6148 return bytes == -2 ? SWITCH_STATUS_TIMEOUT : SWITCH_STATUS_GENERR;
6149 } else if (bytes == 0) {
6150 *datalen = 0;
6151 return SWITCH_STATUS_BREAK;
6152 } else {
6153 if (bytes > rtp_header_len12) {
6154 bytes -= rtp_header_len12;
6155 }
6156 }
6157
6158 *datalen = bytes;
6159
6160 memcpy(data, RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg
.ebody : rtp_session->recv_msg.body)
, bytes);
6161
6162 return SWITCH_STATUS_SUCCESS;
6163}
6164
6165SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtcp_zerocopy_read_frame(switch_rtp_t *rtp_session, switch_rtcp_frame_t *frame)
6166{
6167
6168
6169
6170 if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
6171 return SWITCH_STATUS_FALSE;
6172 }
6173
6174 /* A fresh frame has been found! */
6175 if (rtp_session->rtcp_fresh_frame) {
6176 struct switch_rtcp_sender_report* sr = (struct switch_rtcp_sender_report*)rtp_session->rtcp_recv_msg.body;
6177 int i = 0;
6178
6179 /* turn the flag off! */
6180 rtp_session->rtcp_fresh_frame = 0;
6181
6182 frame->ssrc = ntohl(sr->ssrc)(__extension__ ({ unsigned int __v, __x = (sr->ssrc); if (
__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000) >>
24) | (((__x) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00
) << 8) | (((__x) & 0x000000ff) << 24)); else
__asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
6183 frame->packet_type = (uint16_t)rtp_session->rtcp_recv_msg.header.type;
6184 frame->ntp_msw = ntohl(sr->sender_info.ntp_msw)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.ntp_msw); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
;
6185 frame->ntp_lsw = ntohl(sr->sender_info.ntp_lsw)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.ntp_lsw); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
;
6186 frame->timestamp = ntohl(sr->sender_info.ts)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.ts); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6187 frame->packet_count = ntohl(sr->sender_info.pc)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.pc); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6188 frame->octect_count = ntohl(sr->sender_info.oc)(__extension__ ({ unsigned int __v, __x = (sr->sender_info
.oc); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6189
6190 for (i = 0; i < (int)rtp_session->rtcp_recv_msg_p->header.count && i < MAX_REPORT_BLOCKS5 ; i++) {
6191 struct switch_rtcp_report_block* report = (struct switch_rtcp_report_block*) (rtp_session->rtcp_recv_msg_p->body + (sizeof(struct switch_rtcp_sr_head) + (i * sizeof(struct switch_rtcp_report_block))));
6192 frame->reports[i].ssrc = ntohl(report->ssrc)(__extension__ ({ unsigned int __v, __x = (report->ssrc); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6193 frame->reports[i].fraction = (uint8_t)ntohl(report->fraction)(__extension__ ({ unsigned int __v, __x = (report->fraction
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6194 frame->reports[i].lost = ntohl(report->lost)(__extension__ ({ unsigned int __v, __x = (report->lost); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6195 frame->reports[i].highest_sequence_number_received = ntohl(report->highest_sequence_number_received)(__extension__ ({ unsigned int __v, __x = (report->highest_sequence_number_received
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6196 frame->reports[i].jitter = ntohl(report->jitter)(__extension__ ({ unsigned int __v, __x = (report->jitter)
; if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6197 frame->reports[i].lsr = ntohl(report->lsr)(__extension__ ({ unsigned int __v, __x = (report->lsr); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6198 frame->reports[i].dlsr = ntohl(report->dlsr)(__extension__ ({ unsigned int __v, __x = (report->dlsr); if
(__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6199 if (i >= MAX_REPORT_BLOCKS5) {
6200 break;
6201 }
6202 }
6203 frame->report_count = (uint16_t)i;
6204
6205 return SWITCH_STATUS_SUCCESS;
6206 }
6207
6208 return SWITCH_STATUS_TIMEOUT;
6209}
6210
6211SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_zerocopy_read_frame(switch_rtp_t *rtp_session, switch_frame_t *frame, switch_io_flag_t io_flags)
6212{
6213 int bytes = 0;
6214
6215 if (!switch_rtp_ready(rtp_session)) {
6216 return SWITCH_STATUS_FALSE;
6217 }
6218
6219 bytes = rtp_common_read(rtp_session, &frame->payload, &frame->pmap, &frame->flags, io_flags);
6220
6221 frame->data = RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg
.ebody : rtp_session->recv_msg.body)
;
6222 frame->packet = &rtp_session->recv_msg;
6223 frame->packetlen = bytes;
6224 frame->source = __FILE__"src/switch_rtp.c";
6225
6226 switch_set_flag(frame, SFF_RAW_RTP)(frame)->flags |= (SFF_RAW_RTP);
6227 if (frame->payload == rtp_session->recv_te) {
6228 switch_set_flag(frame, SFF_RFC2833)(frame)->flags |= (SFF_RFC2833);
6229 }
6230 frame->timestamp = ntohl(rtp_session->recv_msg.header.ts)(__extension__ ({ unsigned int __v, __x = (rtp_session->recv_msg
.header.ts); if (__builtin_constant_p (__x)) __v = ((((__x) &
0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
;
6231 frame->seq = (uint16_t) ntohs((uint16_t) rtp_session->recv_msg.header.seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) ((uint16_t) rtp_session->recv_msg.header.seq); if (__builtin_constant_p
(__x)) __v = ((unsigned short int) ((((__x) >> 8) &
0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
6232 frame->ssrc = ntohl(rtp_session->recv_msg.header.ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->recv_msg
.header.ssrc); if (__builtin_constant_p (__x)) __v = ((((__x)
& 0xff000000) >> 24) | (((__x) & 0x00ff0000) >>
8) | (((__x) & 0x0000ff00) << 8) | (((__x) & 0x000000ff
) << 24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (
__x)); __v; }))
;
6233 frame->m = rtp_session->recv_msg.header.m ? SWITCH_TRUE : SWITCH_FALSE;
6234
6235#ifdef ENABLE_ZRTP
6236 if (zrtp_on && rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV]) {
6237 zrtp_session_info_t zrtp_session_info;
6238
6239 if (rtp_session->zrtp_session && (zrtp_status_ok == zrtp_session_get(rtp_session->zrtp_session, &zrtp_session_info))) {
6240 if (zrtp_session_info.sas_is_ready) {
6241
6242 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
6243
6244 const char *uuid = switch_channel_get_partner_uuid(channel);
6245 if (uuid) {
6246 switch_core_session_t *other_session;
6247
6248 if ((other_session = switch_core_session_locate(uuid)switch_core_session_perform_locate(uuid, "src/switch_rtp.c", (
const char *)__func__, 6248)
)) {
6249 switch_channel_t *other_channel = switch_core_session_get_channel(other_session);
6250 switch_rtp_t *other_rtp_session = switch_channel_get_private(other_channel, "__zrtp_audio_rtp_session");
6251
6252 if (other_rtp_session) {
6253 if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND) {
6254 switch_mutex_lock(other_rtp_session->read_mutex);
6255 if (zrtp_status_ok == zrtp_session_get(other_rtp_session->zrtp_session, &zrtp_session_info)) {
6256 if (rtp_session->zrtp_mitm_tries > ZRTP_MITM_TRIES) {
6257 switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV);
6258 switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND);
6259 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
6260 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
6261 } else if (zrtp_status_ok == zrtp_resolve_mitm_call(other_rtp_session->zrtp_stream, rtp_session->zrtp_stream)) {
6262 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
6263 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
6264 switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV);
6265 switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND);
6266 rtp_session->zrtp_mitm_tries++;
6267 }
6268 }
6269 switch_mutex_unlock(other_rtp_session->read_mutex);
6270 }
6271 }
6272
6273 switch_core_session_rwunlock(other_session);
6274 }
6275 }
6276 }
6277 } else {
6278 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
6279 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
6280 }
6281 }
6282#endif
6283
6284 if (bytes < 0) {
6285 frame->datalen = 0;
6286 return bytes == -2 ? SWITCH_STATUS_TIMEOUT : SWITCH_STATUS_GENERR;
6287 } else if (bytes < rtp_header_len12) {
6288 frame->datalen = 0;
6289 return SWITCH_STATUS_BREAK;
6290 } else {
6291 bytes -= rtp_header_len12;
6292 }
6293
6294 frame->datalen = bytes;
6295 return SWITCH_STATUS_SUCCESS;
6296}
6297
6298SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_zerocopy_read(switch_rtp_t *rtp_session,
6299 void **data, uint32_t *datalen, switch_payload_t *payload_type, switch_frame_flag_t *flags,
6300 switch_io_flag_t io_flags)
6301{
6302 int bytes = 0;
6303
6304 if (!switch_rtp_ready(rtp_session)) {
6305 return SWITCH_STATUS_FALSE;
6306 }
6307
6308 bytes = rtp_common_read(rtp_session, payload_type, NULL((void*)0), flags, io_flags);
6309 *data = RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg
.ebody : rtp_session->recv_msg.body)
;
6310
6311 if (bytes < 0) {
6312 *datalen = 0;
6313 return SWITCH_STATUS_GENERR;
6314 } else {
6315 if (bytes > rtp_header_len12) {
6316 bytes -= rtp_header_len12;
6317 }
6318 }
6319
6320 *datalen = bytes;
6321 return SWITCH_STATUS_SUCCESS;
6322}
6323
6324static int rtp_write_ready(switch_rtp_t *rtp_session, uint32_t bytes, int line)
6325{
6326 if (rtp_session->ice.ice_user && !(rtp_session->ice.rready)) {
6327 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 6327, ((void*)0)
, SWITCH_LOG_DEBUG1, "Skip sending %s packet %ld bytes (ice not ready @ line %d!)\n",
6328 rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", (long)bytes, line);
6329 return 0;
6330 }
6331
6332 if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) {
6333 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 6333, ((void*)0)
, SWITCH_LOG_DEBUG1, "Skip sending %s packet %ld bytes (dtls not ready @ line %d!)\n",
6334 rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio", (long)bytes, line);
6335 return 0;
6336 }
6337
6338 return 1;
6339}
6340
6341
6342
6343
6344static int rtp_common_write(switch_rtp_t *rtp_session,
6345 rtp_msg_t *send_msg, void *data, uint32_t datalen, switch_payload_t payload, uint32_t timestamp, switch_frame_flag_t *flags)
6346{
6347 switch_size_t bytes;
6348 uint8_t send = 1;
6349 uint32_t this_ts = 0;
6350 int ret;
6351 switch_time_t now;
6352 uint8_t m = 0;
6353
6354 if (!switch_rtp_ready(rtp_session)) {
6355 return -1;
6356 }
6357
6358 if (!rtp_write_ready(rtp_session, datalen, __LINE__6358)) {
6359 return 0;
6360 }
6361
6362 WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session->
writing++
;
6363
6364 if (send_msg) {
6365 bytes = datalen;
6366
6367 m = (uint8_t) send_msg->header.m;
6368
6369 if (flags && *flags & SFF_RFC2833) {
6370 send_msg->header.pt = rtp_session->te;
6371 }
6372 data = send_msg->body;
6373 if (datalen > rtp_header_len12) {
6374 datalen -= rtp_header_len12;
6375 }
6376 } else {
6377 if (*flags & SFF_RFC2833) {
6378 payload = rtp_session->te;
6379 }
6380
6381 send_msg = &rtp_session->send_msg;
6382 send_msg->header.pt = payload;
6383
6384 m = get_next_write_ts(rtp_session, timestamp);
6385
6386 rtp_session->send_msg.header.ts = htonl(rtp_session->ts)(__extension__ ({ unsigned int __v, __x = (rtp_session->ts
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6387
6388 memcpy(send_msg->body, data, datalen);
6389 bytes = datalen + rtp_header_len12;
6390 }
6391
6392 if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
6393
6394 if ((rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
6395 m = 0;
6396 } else {
6397 if ((!rtp_session->flags[SWITCH_RTP_FLAG_RESET] && rtp_session->ts > (rtp_session->last_write_ts + (rtp_session->samples_per_interval * 10)))
6398 || rtp_session->ts == rtp_session->samples_per_interval) {
6399 m++;
6400 }
6401
6402 if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
6403 (rtp_session->timer.samplecount - rtp_session->last_write_samplecount) > rtp_session->samples_per_interval * 10) {
6404 m++;
6405 }
6406
6407 if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
6408 ((unsigned) ((switch_micro_time_now() - rtp_session->last_write_timestamp))) > (rtp_session->ms_per_packet * 10)) {
6409 m++;
6410 }
6411
6412 if (rtp_session->cn && payload != rtp_session->cng_pt) {
6413 rtp_session->cn = 0;
6414 m++;
6415 }
6416
6417 if (rtp_session->need_mark && !rtp_session->sending_dtmf) {
6418 m++;
6419 rtp_session->need_mark = 0;
6420 }
6421 }
6422
6423 if (m) {
6424 rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
6425 rtp_session->ts = 0;
6426 }
6427
6428 /* If the marker was set, and the timestamp seems to have started over - set a new SSRC, to indicate this is a new stream */
6429 if (m && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND) && (rtp_session->rtp_bugs & RTP_BUG_CHANGE_SSRC_ON_MARKER) &&
6430 (rtp_session->flags[SWITCH_RTP_FLAG_RESET] || (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->last_write_ts > 0))) {
6431 switch_rtp_set_ssrc(rtp_session, (uint32_t) ((intptr_t) rtp_session + (uint32_t) switch_epoch_time_now(NULL((void*)0))));
6432 }
6433
6434 if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO) && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_UDPTL)) {
6435 send_msg->header.m = (m && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) ? 1 : 0;
6436 }
6437 }
6438
6439
6440 if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
6441 /* Normalize the timestamps to our own base by generating a made up starting point then adding the measured deltas to that base
6442 so if the timestamps and ssrc of the source change, it will not break the other end's jitter bufffer / decoder etc *cough* CHROME *cough*
6443 */
6444
6445 if (!rtp_session->ts_norm.ts) {
6446 rtp_session->ts_norm.ts = (uint32_t) rand() % 1000000 + 1;
6447 }
6448
6449 if (!rtp_session->ts_norm.last_ssrc || send_msg->header.ssrc != rtp_session->ts_norm.last_ssrc) {
6450 if (rtp_session->ts_norm.last_ssrc) {
6451 rtp_session->ts_norm.delta_ct = 1;
6452 rtp_session->ts_norm.delta_ttl = 0;
6453 if (rtp_session->ts_norm.delta) {
6454 rtp_session->ts_norm.ts += rtp_session->ts_norm.delta;
6455 }
6456 }
6457 rtp_session->ts_norm.last_ssrc = send_msg->header.ssrc;
6458 rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts)(__extension__ ({ unsigned int __v, __x = (send_msg->header
.ts); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6459 }
6460
6461
6462 if (ntohl(send_msg->header.ts)(__extension__ ({ unsigned int __v, __x = (send_msg->header
.ts); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
!= rtp_session->ts_norm.last_frame) {
6463 rtp_session->ts_norm.delta = ntohl(send_msg->header.ts)(__extension__ ({ unsigned int __v, __x = (send_msg->header
.ts); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
- rtp_session->ts_norm.last_frame;
6464 rtp_session->ts_norm.ts += rtp_session->ts_norm.delta;
6465 }
6466
6467 rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts)(__extension__ ({ unsigned int __v, __x = (send_msg->header
.ts); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6468 send_msg->header.ts = htonl(rtp_session->ts_norm.ts)(__extension__ ({ unsigned int __v, __x = (rtp_session->ts_norm
.ts); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6469
6470 }
6471
6472 send_msg->header.ssrc = htonl(rtp_session->ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->ssrc
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6473
6474 if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->send_msg.header.pt == 97) {
6475 rtp_session->recv_msg.header.pt = 102;
6476 }
6477
6478 if (rtp_session->flags[SWITCH_RTP_FLAG_VAD] &&
6479 rtp_session->recv_msg.header.pt == rtp_session->vad_data.read_codec->implementation->ianacode) {
6480
6481 int16_t decoded[SWITCH_RECOMMENDED_BUFFER_SIZE8192 / sizeof(int16_t)] = { 0 };
6482 uint32_t rate = 0;
6483 uint32_t codec_flags = 0;
6484 uint32_t len = sizeof(decoded);
6485 time_t now = switch_epoch_time_now(NULL((void*)0));
6486 send = 0;
6487
6488 if (rtp_session->vad_data.scan_freq && rtp_session->vad_data.next_scan <= now) {
6489 rtp_session->vad_data.bg_count = rtp_session->vad_data.bg_level = 0;
6490 rtp_session->vad_data.next_scan = now + rtp_session->vad_data.scan_freq;
6491 }
6492
6493 if (switch_core_codec_decode(&rtp_session->vad_data.vad_codec,
6494 rtp_session->vad_data.read_codec,
6495 data,
6496 datalen,
6497 rtp_session->vad_data.read_codec->implementation->actual_samples_per_second,
6498 decoded, &len, &rate, &codec_flags) == SWITCH_STATUS_SUCCESS) {
6499
6500 uint32_t energy = 0;
6501 uint32_t x, y = 0, z = len / sizeof(int16_t);
6502 uint32_t score = 0;
6503 int divisor = 0;
6504 if (z) {
6505
6506 if (!(divisor = rtp_session->vad_data.read_codec->implementation->actual_samples_per_second / 8000)) {
6507 divisor = 1;
6508 }
6509
6510 for (x = 0; x < z; x++) {
6511 energy += abs(decoded[y]);
6512 y += rtp_session->vad_data.read_codec->implementation->number_of_channels;
6513 }
6514
6515 if (++rtp_session->vad_data.start_count < rtp_session->vad_data.start) {
6516 send = 1;
6517 } else {
6518 score = (energy / (z / divisor));
6519 if (score && (rtp_session->vad_data.bg_count < rtp_session->vad_data.bg_len)) {
6520 rtp_session->vad_data.bg_level += score;
6521 if (++rtp_session->vad_data.bg_count == rtp_session->vad_data.bg_len) {
6522 rtp_session->vad_data.bg_level /= rtp_session->vad_data.bg_len;
6523 }
6524 send = 1;
6525 } else {
6526 if (score > rtp_session->vad_data.bg_level && !switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)((&rtp_session->vad_data)->flags & SWITCH_VAD_FLAG_TALKING
)
) {
6527 uint32_t diff = score - rtp_session->vad_data.bg_level;
6528
6529 if (rtp_session->vad_data.hangover_hits) {
6530 rtp_session->vad_data.hangover_hits--;
6531 }
6532
6533 if (diff >= rtp_session->vad_data.diff_level || ++rtp_session->vad_data.hangunder_hits >= rtp_session->vad_data.hangunder) {
6534
6535 switch_set_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)(&rtp_session->vad_data)->flags |= (SWITCH_VAD_FLAG_TALKING
)
;
6536 if (!(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
6537 send_msg->header.m = 1;
6538 }
6539 rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
6540 if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_EVENTS_TALK)((&rtp_session->vad_data)->flags & SWITCH_VAD_FLAG_EVENTS_TALK
)
) {
6541
6542 if ((rtp_session->vad_data.fire_events & VAD_FIRE_TALK)) {
6543 switch_event_t *event;
6544 if (switch_event_create(&event, SWITCH_EVENT_TALK)switch_event_create_subclass_detailed("src/switch_rtp.c", (const
char * )(const char *)__func__, 6544, &event, SWITCH_EVENT_TALK
, ((void*)0))
== SWITCH_STATUS_SUCCESS) {
6545 switch_channel_event_set_data(switch_core_session_get_channel(rtp_session->vad_data.session), event);
6546 switch_event_fire(&event)switch_event_fire_detailed("src/switch_rtp.c", (const char * )
(const char *)__func__, 6546, &event, ((void*)0))
;
6547 }
6548 }
6549 }
6550 }
6551 } else {
6552 if (rtp_session->vad_data.hangunder_hits) {
6553 rtp_session->vad_data.hangunder_hits--;
6554 }
6555 if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)((&rtp_session->vad_data)->flags & SWITCH_VAD_FLAG_TALKING
)
) {
6556 if (++rtp_session->vad_data.hangover_hits >= rtp_session->vad_data.hangover) {
6557 switch_clear_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)(&rtp_session->vad_data)->flags &= ~(SWITCH_VAD_FLAG_TALKING
)
;
6558 rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
6559 if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_EVENTS_NOTALK)((&rtp_session->vad_data)->flags & SWITCH_VAD_FLAG_EVENTS_NOTALK
)
) {
6560
6561 if ((rtp_session->vad_data.fire_events & VAD_FIRE_NOT_TALK)) {
6562 switch_event_t *event;
6563 if (switch_event_create(&event, SWITCH_EVENT_NOTALK)switch_event_create_subclass_detailed("src/switch_rtp.c", (const
char * )(const char *)__func__, 6563, &event, SWITCH_EVENT_NOTALK
, ((void*)0))
== SWITCH_STATUS_SUCCESS) {
6564 switch_channel_event_set_data(switch_core_session_get_channel(rtp_session->vad_data.session), event);
6565 switch_event_fire(&event)switch_event_fire_detailed("src/switch_rtp.c", (const char * )
(const char *)__func__, 6565, &event, ((void*)0))
;
6566 }
6567 }
6568 }
6569 }
6570 }
6571 }
6572 }
6573 }
6574
6575 if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)((&rtp_session->vad_data)->flags & SWITCH_VAD_FLAG_TALKING
)
) {
6576 send = 1;
6577 }
6578 }
6579 } else {
6580 ret = -1;
6581 goto end;
6582 }
6583 }
6584
6585 if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
6586 this_ts = ntohl(send_msg->header.ts)(__extension__ ({ unsigned int __v, __x = (send_msg->header
.ts); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6587
6588 if ((this_ts - rtp_session->last_write_ts) > 16000) {
6589 rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
6590 }
6591
6592 if (!switch_rtp_ready(rtp_session) || rtp_session->sending_dtmf || !this_ts ||
6593 (!rtp_session->flags[SWITCH_RTP_FLAG_RESET] && this_ts < rtp_session->last_write_ts)) {
6594 send = 0;
6595 }
6596 }
6597
6598 if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) {
6599 send = 0;
6600 }
6601
6602 if (send) {
6603 send_msg->header.seq = htons(++rtp_session->seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) (++rtp_session->seq); if (__builtin_constant_p (__x)
) __v = ((unsigned short int) ((((__x) >> 8) & 0xff
) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
6604
6605 if (rtp_session->flags[SWITCH_RTP_FLAG_BYTESWAP] && send_msg->header.pt == rtp_session->payload) {
6606 switch_swap_linear((int16_t *)send_msg->body, (int) datalen);
6607 }
6608
6609#ifdef ENABLE_SRTP1
6610 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
6611 int sbytes = (int) bytes;
6612 err_status_t stat;
6613
6614
6615 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET]) {
6616
6617 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND_RESET);
6618 srtp_dealloc(rtp_session->send_ctx[rtp_session->srtp_idx_rtp]);
6619 rtp_session->send_ctx[rtp_session->srtp_idx_rtp] = NULL((void*)0);
6620 if ((stat = srtp_create(&rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &rtp_session->send_policy[rtp_session->srtp_idx_rtp]))) {
6621 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 6621, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP SEND\n");
6622 ret = -1;
6623 goto end;
6624 } else {
6625 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 6625, (const char*)(rtp_session->session)
, SWITCH_LOG_INFO, "RE-Activating Secure RTP SEND\n");
6626 }
6627 }
6628
6629
6630 stat = srtp_protect(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &send_msg->header, &sbytes);
6631
6632 if (stat) {
6633 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 6633, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error: SRTP protection failed with code %d\n", stat);
6634 }
6635
6636 bytes = sbytes;
6637 }
6638#endif
6639#ifdef ENABLE_ZRTP
6640 /* ZRTP Send */
6641 if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
6642 unsigned int sbytes = (int) bytes;
6643 zrtp_status_t stat = zrtp_status_fail;
6644
6645
6646 stat = zrtp_process_rtp(rtp_session->zrtp_stream, (void *) send_msg, &sbytes);
6647
6648 switch (stat) {
6649 case zrtp_status_ok:
6650 break;
6651 case zrtp_status_drop:
6652 /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */
6653 ret = (int) bytes;
6654 goto end;
6655 break;
6656 case zrtp_status_fail:
6657 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 6657, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
6658 break;
6659 default:
6660 break;
6661 }
6662
6663 bytes = sbytes;
6664 }
6665#endif
6666
6667 now = switch_micro_time_now();
6668#ifdef RTP_DEBUG_WRITE_DELTA
6669 {
6670 int delta = (int) (now - rtp_session->send_time) / 1000;
6671 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 6671, ((void*)0)
, SWITCH_LOG_ERROR, "WRITE %d delta %d\n", (int) bytes, delta);
6672 }
6673#endif
6674 rtp_session->send_time = now;
6675
6676 if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
6677 const char *tx_host;
6678 const char *old_host;
6679 const char *my_host;
6680
6681 char bufa[30], bufb[30], bufc[30];
6682
6683
6684 tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr);
6685 old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
6686 my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
6687
6688 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session)SWITCH_CHANNEL_ID_LOG_CLEAN, "src/switch_rtp.c", (const char *
)__func__, 6688, switch_core_session_get_uuid((rtp_session->
session))
, SWITCH_LOG_CONSOLE,
6689 "W %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u m=%d\n",
6690 rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
6691 (long) bytes,
6692 my_host, switch_sockaddr_get_port(rtp_session->local_addr),
6693 old_host, rtp_session->remote_port,
6694 tx_host, switch_sockaddr_get_port(rtp_session->from_addr),
6695 send_msg->header.pt, ntohl(send_msg->header.ts)(__extension__ ({ unsigned int __v, __x = (send_msg->header
.ts); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
, send_msg->header.m);
6696
6697 }
6698
6699 if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
6700 rtp_session->seq--;
6701 ret = -1;
6702 goto end;
6703 }
6704
6705 rtp_session->last_write_ts = this_ts;
6706 rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
6707
6708 if (rtp_session->queue_delay) {
6709 rtp_session->delay_samples = rtp_session->queue_delay;
6710 rtp_session->queue_delay = 0;
6711 }
6712
6713 rtp_session->stats.outbound.raw_bytes += bytes;
6714 rtp_session->stats.outbound.packet_count++;
6715
6716 if (rtp_session->cng_pt && send_msg->header.pt == rtp_session->cng_pt) {
6717 rtp_session->stats.outbound.cng_packet_count++;
6718 } else {
6719 rtp_session->stats.outbound.media_packet_count++;
6720 rtp_session->stats.outbound.media_bytes += bytes;
6721 }
6722
6723 if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
6724 rtp_session->last_write_samplecount = rtp_session->timer.samplecount;
6725 } else {
6726 rtp_session->last_write_timestamp = switch_micro_time_now();
6727 }
6728
6729 }
6730
6731 ret = (int) bytes;
6732
6733 end:
6734
6735 WRITE_DEC(rtp_session)switch_mutex_unlock(rtp_session->write_mutex); rtp_session
->writing--
;
6736
6737 return ret;
6738}
6739
6740SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_disable_vad(switch_rtp_t *rtp_session)
6741{
6742
6743 if (!rtp_session) {
6744 return SWITCH_STATUS_FALSE;
6745 }
6746
6747 if (!rtp_session->flags[SWITCH_RTP_FLAG_VAD]) {
6748 return SWITCH_STATUS_GENERR;
6749 }
6750 switch_core_codec_destroy(&rtp_session->vad_data.vad_codec);
6751 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_VAD);
6752 return SWITCH_STATUS_SUCCESS;
6753}
6754
6755SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_enable_vad(switch_rtp_t *rtp_session, switch_core_session_t *session, switch_codec_t *codec,
6756 switch_vad_flag_t flags)
6757{
6758 if (!switch_rtp_ready(rtp_session)) {
6759 return SWITCH_STATUS_FALSE;
6760 }
6761
6762 if (rtp_session->flags[SWITCH_RTP_FLAG_VAD]) {
6763 return SWITCH_STATUS_GENERR;
6764 }
6765
6766 memset(&rtp_session->vad_data, 0, sizeof(rtp_session->vad_data));
6767
6768 if (switch_true(switch_channel_get_variable(switch_core_session_get_channel(rtp_session->session), "fire_talk_events")switch_channel_get_variable_dup(switch_core_session_get_channel
(rtp_session->session), "fire_talk_events", SWITCH_TRUE, -
1)
)) {
6769 rtp_session->vad_data.fire_events |= VAD_FIRE_TALK;
6770 }
6771
6772 if (switch_true(switch_channel_get_variable(switch_core_session_get_channel(rtp_session->session), "fire_not_talk_events")switch_channel_get_variable_dup(switch_core_session_get_channel
(rtp_session->session), "fire_not_talk_events", SWITCH_TRUE
, -1)
)) {
6773 rtp_session->vad_data.fire_events |= VAD_FIRE_NOT_TALK;
6774 }
6775
6776
6777 if (switch_core_codec_init(&rtp_session->vad_data.vad_codec,switch_core_codec_init_with_bitrate(&rtp_session->vad_data
.vad_codec, codec->implementation->iananame, ((void*)0)
, codec->implementation->samples_per_second, codec->
implementation->microseconds_per_packet / 1000, codec->
implementation->number_of_channels, 0, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE, ((void*)0), rtp_session->pool
)
6778 codec->implementation->iananame,switch_core_codec_init_with_bitrate(&rtp_session->vad_data
.vad_codec, codec->implementation->iananame, ((void*)0)
, codec->implementation->samples_per_second, codec->
implementation->microseconds_per_packet / 1000, codec->
implementation->number_of_channels, 0, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE, ((void*)0), rtp_session->pool
)
6779 NULL,switch_core_codec_init_with_bitrate(&rtp_session->vad_data
.vad_codec, codec->implementation->iananame, ((void*)0)
, codec->implementation->samples_per_second, codec->
implementation->microseconds_per_packet / 1000, codec->
implementation->number_of_channels, 0, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE, ((void*)0), rtp_session->pool
)
6780 codec->implementation->samples_per_second,switch_core_codec_init_with_bitrate(&rtp_session->vad_data
.vad_codec, codec->implementation->iananame, ((void*)0)
, codec->implementation->samples_per_second, codec->
implementation->microseconds_per_packet / 1000, codec->
implementation->number_of_channels, 0, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE, ((void*)0), rtp_session->pool
)
6781 codec->implementation->microseconds_per_packet / 1000,switch_core_codec_init_with_bitrate(&rtp_session->vad_data
.vad_codec, codec->implementation->iananame, ((void*)0)
, codec->implementation->samples_per_second, codec->
implementation->microseconds_per_packet / 1000, codec->
implementation->number_of_channels, 0, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE, ((void*)0), rtp_session->pool
)
6782 codec->implementation->number_of_channels,switch_core_codec_init_with_bitrate(&rtp_session->vad_data
.vad_codec, codec->implementation->iananame, ((void*)0)
, codec->implementation->samples_per_second, codec->
implementation->microseconds_per_packet / 1000, codec->
implementation->number_of_channels, 0, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE, ((void*)0), rtp_session->pool
)
6783 SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, rtp_session->pool)switch_core_codec_init_with_bitrate(&rtp_session->vad_data
.vad_codec, codec->implementation->iananame, ((void*)0)
, codec->implementation->samples_per_second, codec->
implementation->microseconds_per_packet / 1000, codec->
implementation->number_of_channels, 0, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE, ((void*)0), rtp_session->pool
)
!= SWITCH_STATUS_SUCCESS) {
6784 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 6784, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Can't load codec?\n");
6785 return SWITCH_STATUS_FALSE;
6786 }
6787 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 6787, (const char*)(rtp_session->session)
, SWITCH_LOG_DEBUG, "Activate VAD codec %s %dms\n", codec->implementation->iananame,
6788 codec->implementation->microseconds_per_packet / 1000);
6789 rtp_session->vad_data.diff_level = 400;
6790 rtp_session->vad_data.hangunder = 15;
6791 rtp_session->vad_data.hangover = 40;
6792 rtp_session->vad_data.bg_len = 5;
6793 rtp_session->vad_data.bg_count = 5;
6794 rtp_session->vad_data.bg_level = 300;
6795 rtp_session->vad_data.read_codec = codec;
6796 rtp_session->vad_data.session = session;
6797 rtp_session->vad_data.flags = flags;
6798 rtp_session->vad_data.cng_freq = 50;
6799 rtp_session->vad_data.ts = 1;
6800 rtp_session->vad_data.start = 0;
6801 rtp_session->vad_data.next_scan = switch_epoch_time_now(NULL((void*)0));
6802 rtp_session->vad_data.scan_freq = 0;
6803 switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_VAD);
6804 switch_set_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_CNG)(&rtp_session->vad_data)->flags |= (SWITCH_VAD_FLAG_CNG
)
;
6805 return SWITCH_STATUS_SUCCESS;
6806}
6807
6808SWITCH_DECLARE(int)__attribute__((visibility("default"))) int switch_rtp_write_frame(switch_rtp_t *rtp_session, switch_frame_t *frame)
6809{
6810 uint8_t fwd = 0;
6811 void *data = NULL((void*)0);
6812 uint32_t len, ts = 0;
6813 switch_payload_t payload = 0;
6814 rtp_msg_t *send_msg = NULL((void*)0);
6815
6816 if (!switch_rtp_ready(rtp_session) || !rtp_session->remote_addr) {
6817 return -1;
6818 }
6819
6820 if (!rtp_write_ready(rtp_session, frame->datalen, __LINE__6820)) {
6821 return 0;
6822 }
6823
6824 //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
6825 // rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]++;
6826 // rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]++;
6827 //}
6828
6829
6830 if (switch_test_flag(frame, SFF_PROXY_PACKET)((frame)->flags & SFF_PROXY_PACKET) || switch_test_flag(frame, SFF_UDPTL_PACKET)((frame)->flags & SFF_UDPTL_PACKET) ||
6831 rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
6832
6833 //if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
6834 switch_size_t bytes;
6835 //char bufa[30];
6836
6837 /* Fast PASS! */
6838 if (!switch_test_flag(frame, SFF_PROXY_PACKET)((frame)->flags & SFF_PROXY_PACKET) && !switch_test_flag(frame, SFF_UDPTL_PACKET)((frame)->flags & SFF_UDPTL_PACKET)) {
6839 return 0;
6840 }
6841 bytes = frame->packetlen;
6842 //tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->remote_addr);
6843
6844 send_msg = frame->packet;
6845
6846 if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !switch_test_flag(frame, SFF_UDPTL_PACKET)((frame)->flags & SFF_UDPTL_PACKET)) {
6847
6848 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->payload > 0) {
6849 send_msg->header.pt = rtp_session->payload;
6850 }
6851
6852 send_msg->header.ssrc = htonl(rtp_session->ssrc)(__extension__ ({ unsigned int __v, __x = (rtp_session->ssrc
); if (__builtin_constant_p (__x)) __v = ((((__x) & 0xff000000
) >> 24) | (((__x) & 0x00ff0000) >> 8) | (((__x
) & 0x0000ff00) << 8) | (((__x) & 0x000000ff) <<
24)); else __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); __v
; }))
;
6853 send_msg->header.seq = htons(++rtp_session->seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) (++rtp_session->seq); if (__builtin_constant_p (__x)
) __v = ((unsigned short int) ((((__x) >> 8) & 0xff
) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
6854 }
6855
6856 if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, frame->packet, &bytes) != SWITCH_STATUS_SUCCESS) {
6857 return -1;
6858 }
6859
6860
6861 rtp_session->stats.outbound.raw_bytes += bytes;
6862 rtp_session->stats.outbound.media_bytes += bytes;
6863 rtp_session->stats.outbound.media_packet_count++;
6864 rtp_session->stats.outbound.packet_count++;
6865 return (int) bytes;
6866 }
6867#ifdef ENABLE_ZRTP
6868 if (zrtp_on && rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND]) {
6869 zrtp_session_info_t zrtp_session_info;
6870
6871 if (zrtp_status_ok == zrtp_session_get(rtp_session->zrtp_session, &zrtp_session_info)) {
6872 if (zrtp_session_info.sas_is_ready) {
6873
6874 switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
6875
6876 const char *uuid = switch_channel_get_partner_uuid(channel);
6877 if (uuid) {
6878 switch_core_session_t *other_session;
6879
6880 if ((other_session = switch_core_session_locate(uuid)switch_core_session_perform_locate(uuid, "src/switch_rtp.c", (
const char *)__func__, 6880)
)) {
6881 switch_channel_t *other_channel = switch_core_session_get_channel(other_session);
6882 switch_rtp_t *other_rtp_session = switch_channel_get_private(other_channel, "__zrtp_audio_rtp_session");
6883
6884
6885 if (other_rtp_session) {
6886 if (zrtp_status_ok == zrtp_session_get(other_rtp_session->zrtp_session, &zrtp_session_info)) {
6887 if (rtp_session->zrtp_mitm_tries > ZRTP_MITM_TRIES) {
6888 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
6889 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
6890 switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV);
6891 switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND);
6892 } else if (zrtp_status_ok == zrtp_resolve_mitm_call(other_rtp_session->zrtp_stream, rtp_session->zrtp_stream)) {
6893 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0;
6894 rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0;
6895 switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV);
6896 switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND);
6897 rtp_session->zrtp_mitm_tries++;
6898 }
6899 rtp_session->zrtp_mitm_tries++;
6900 }
6901 }
6902
6903 switch_core_session_rwunlock(other_session);
6904 }
6905 }
6906 }
6907 }
6908 }
6909#endif
6910
6911 fwd = (rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] && switch_test_flag(frame, SFF_RAW_RTP)((frame)->flags & SFF_RAW_RTP)) ? 1 : 0;
6912
6913 if (!fwd && !rtp_session->sending_dtmf && !rtp_session->queue_delay &&
6914 rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] && (rtp_session->rtp_bugs & RTP_BUG_GEN_ONE_GEN_ALL)) {
6915
6916 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 6916, (const char*)(rtp_session->session)
, SWITCH_LOG_WARNING, "Generating RTP locally but timestamp passthru is configured, disabling....\n");
6917 rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] = 0;
6918 rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
6919 }
6920
6921 switch_assert(frame != NULL)((frame != ((void*)0)) ? (void) (0) : __assert_fail ("frame != ((void*)0)"
, "src/switch_rtp.c", 6921, __PRETTY_FUNCTION__))
;
6922
6923 if (switch_test_flag(frame, SFF_CNG)((frame)->flags & SFF_CNG)) {
6924 if (rtp_session->cng_pt) {
6925 payload = rtp_session->cng_pt;
6926 } else {
6927 return (int) frame->packetlen;
6928 }
6929 } else {
6930 payload = rtp_session->payload;
6931#if 0
6932 if (rtp_session->pmaps && *rtp_session->pmaps) {
6933 payload_map_t *pmap;
6934 for (pmap = *rtp_session->pmaps; pmap; pmap = pmap->next) {
6935 if (pmap->current) {
6936 payload = pmap->pt;
6937 }
6938 }
6939 }
6940#endif
6941 }
6942
6943 if (switch_test_flag(frame, SFF_RTP_HEADER)((frame)->flags & SFF_RTP_HEADER)) {
6944 switch_size_t wrote = switch_rtp_write_manual(rtp_session, frame->data, frame->datalen,
6945 frame->m, frame->payload, (uint32_t) (frame->timestamp), &frame->flags);
6946
6947 rtp_session->stats.outbound.raw_bytes += wrote;
6948 rtp_session->stats.outbound.media_bytes += wrote;
6949 rtp_session->stats.outbound.media_packet_count++;
6950 rtp_session->stats.outbound.packet_count++;
6951 }
6952
6953 if (frame->pmap && rtp_session->pmaps && *rtp_session->pmaps) {
6954 payload_map_t *pmap;
6955
6956 switch_mutex_lock(rtp_session->flag_mutex);
6957 for (pmap = *rtp_session->pmaps; pmap; pmap = pmap->next) {
6958 if (pmap->negotiated && pmap->hash == frame->pmap->hash) {
6959 payload = pmap->recv_pt;
6960 break;
6961 }
6962 }
6963 switch_mutex_unlock(rtp_session->flag_mutex);
6964 }
6965
6966 if (fwd) {
6967 send_msg = frame->packet;
6968 len = frame->packetlen;
6969 ts = 0;
6970 // Trying this based on http://jira.freeswitch.org/browse/MODSOFIA-90
6971 //if (frame->codec && frame->codec->agreed_pt == frame->payload) {
6972
6973 send_msg->header.pt = payload;
6974 //}
6975 } else {
6976 data = frame->data;
6977 len = frame->datalen;
6978 ts = rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] ? (uint32_t) frame->timestamp : 0;
6979 }
6980
6981 /*
6982 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
6983 send_msg->header.pt = rtp_session->payload;
6984 }
6985 */
6986
6987 return rtp_common_write(rtp_session, send_msg, data, len, payload, ts, &frame->flags);
6988}
6989
6990SWITCH_DECLARE(switch_rtp_stats_t *)__attribute__((visibility("default"))) switch_rtp_stats_t * switch_rtp_get_stats(switch_rtp_t *rtp_session, switch_memory_pool_t *pool)
6991{
6992 switch_rtp_stats_t *s;
6993
6994 if (!rtp_session) {
6995 return NULL((void*)0);
6996 }
6997
6998 switch_mutex_lock(rtp_session->flag_mutex);
6999 if (pool) {
7000 s = switch_core_alloc(pool, sizeof(*s))switch_core_perform_alloc(pool, sizeof(*s), "src/switch_rtp.c"
, (const char *)__func__, 7000)
;
7001 *s = rtp_session->stats;
7002 } else {
7003 s = &rtp_session->stats;
7004 }
7005
7006 if (rtp_session->jb) {
7007 s->inbound.largest_jb_size = stfu_n_get_most_qlen(rtp_session->jb);
7008 }
7009
7010 do_mos(rtp_session, SWITCH_FALSE);
7011
7012 switch_mutex_unlock(rtp_session->flag_mutex);
7013
7014 return s;
7015}
7016
7017SWITCH_DECLARE(int)__attribute__((visibility("default"))) int switch_rtp_write_manual(switch_rtp_t *rtp_session,
7018 void *data, uint32_t datalen, uint8_t m, switch_payload_t payload, uint32_t ts, switch_frame_flag_t *flags)
7019{
7020 switch_size_t bytes;
7021 int ret = -1;
7022
7023 if (!switch_rtp_ready(rtp_session) || !rtp_session->remote_addr || datalen > SWITCH_RTP_MAX_BUF_LEN16384) {
7024 return -1;
7025 }
7026
7027 if (!rtp_write_ready(rtp_session, datalen, __LINE__7027)) {
7028 return 0;
7029 }
7030
7031 WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session->
writing++
;
7032
7033 rtp_session->write_msg = rtp_session->send_msg;
7034 rtp_session->write_msg.header.seq = htons(++rtp_session->seq)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) (++rtp_session->seq); if (__builtin_constant_p (__x)
) __v = ((unsigned short int) ((((__x) >> 8) & 0xff
) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
7035 rtp_session->write_msg.header.ts = htonl(ts)(__extension__ ({ unsigned int __v, __x = (ts); if (__builtin_constant_p
(__x)) __v = ((((__x) & 0xff000000) >> 24) | (((__x
) & 0x00ff0000) >> 8) | (((__x) & 0x0000ff00) <<
8) | (((__x) & 0x000000ff) << 24)); else __asm__ (
"bswap %0" : "=r" (__v) : "0" (__x)); __v; }))
;
7036 rtp_session->write_msg.header.pt = payload;
7037 rtp_session->write_msg.header.m = m;
7038 memcpy(rtp_session->write_msg.body, data, datalen);
7039
7040 bytes = rtp_header_len12 + datalen;
7041
7042#ifdef ENABLE_SRTP1
7043 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
7044
7045 int sbytes = (int) bytes;
7046 err_status_t stat;
7047
7048 if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET]) {
7049 switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND_RESET);
7050 srtp_dealloc(rtp_session->send_ctx[rtp_session->srtp_idx_rtp]);
7051 rtp_session->send_ctx[rtp_session->srtp_idx_rtp] = NULL((void*)0);
7052 if ((stat = srtp_create(&rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &rtp_session->send_policy[rtp_session->srtp_idx_rtp]))) {
7053 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 7053, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP SEND\n");
7054 ret = -1;
7055 goto end;
7056 } else {
7057 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 7057, (const char*)(rtp_session->session)
, SWITCH_LOG_INFO, "RE-Activating Secure RTP SEND\n");
7058 }
7059 }
7060
7061 stat = srtp_protect(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &rtp_session->write_msg.header, &sbytes);
7062 if (stat) {
7063 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *)
__func__, 7063, (const char*)(rtp_session->session)
, SWITCH_LOG_ERROR, "Error: SRTP protection failed with code %d\n", stat);
7064 }
7065 bytes = sbytes;
7066 }
7067#endif
7068#ifdef ENABLE_ZRTP
7069 /* ZRTP Send */
7070 if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
7071 unsigned int sbytes = (int) bytes;
7072 zrtp_status_t stat = zrtp_status_fail;
7073
7074 stat = zrtp_process_rtp(rtp_session->zrtp_stream, (void *) &rtp_session->write_msg, &sbytes);
7075
7076 switch (stat) {
7077 case zrtp_status_ok:
7078 break;
7079 case zrtp_status_drop:
7080 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 7080, ((void*)0)
, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
7081 ret = (int) bytes;
7082 goto end;
7083 break;
7084 case zrtp_status_fail:
7085 switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__
, 7085, ((void*)0)
, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
7086 break;
7087 default:
7088 break;
7089 }
7090
7091 bytes = sbytes;
7092 }
7093#endif
7094
7095 if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) &rtp_session->write_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
7096 rtp_session->seq--;
7097 ret = -1;
7098 goto end;
7099 }
7100
7101 if (((*flags) & SFF_RTP_HEADER)) {
7102 rtp_session->last_write_ts = ts;
7103 rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
7104 }
7105
7106 ret = (int) bytes;
7107
7108 end:
7109
7110 WRITE_DEC(rtp_session)switch_mutex_unlock(rtp_session->write_mutex); rtp_session
->writing--
;
7111
7112 return ret;
7113}
7114
7115SWITCH_DECLARE(uint32_t)__attribute__((visibility("default"))) uint32_t switch_rtp_get_ssrc(switch_rtp_t *rtp_session)
7116{
7117 return rtp_session->ssrc;
7118}
7119
7120SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_private(switch_rtp_t *rtp_session, void *private_data)
7121{
7122 rtp_session->private_data = private_data;
7123}
7124
7125SWITCH_DECLARE(void *)__attribute__((visibility("default"))) void * switch_rtp_get_private(switch_rtp_t *rtp_session)
7126{
7127 return rtp_session->private_data;
7128}
7129
7130/* For Emacs:
7131 * Local Variables:
7132 * mode:c
7133 * indent-tabs-mode:t
7134 * tab-width:4
7135 * c-basic-offset:4
7136 * End:
7137 * For VIM:
7138 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
7139 */
7140