File: | src/switch_rtp.c |
Warning: | line 8506, column 19 Dereference of null pointer (loaded from variable 'flags') |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | |||
2 | * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application | |||
3 | * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org> | |||
4 | * | |||
5 | * Version: MPL 1.1 | |||
6 | * | |||
7 | * The contents of this file are subject to the Mozilla Public License Version | |||
8 | * 1.1 (the "License"); you may not use this file except in compliance with | |||
9 | * the License. You may obtain a copy of the License at | |||
10 | * http://www.mozilla.org/MPL/ | |||
11 | * | |||
12 | * Software distributed under the License is distributed on an "AS IS" basis, | |||
13 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |||
14 | * for the specific language governing rights and limitations under the | |||
15 | * License. | |||
16 | * | |||
17 | * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application | |||
18 | * | |||
19 | * The Initial Developer of the Original Code is | |||
20 | * Anthony Minessale II <anthm@freeswitch.org> | |||
21 | * Portions created by the Initial Developer are Copyright (C) | |||
22 | * the Initial Developer. All Rights Reserved. | |||
23 | * | |||
24 | * Contributor(s): | |||
25 | * | |||
26 | * Anthony Minessale II <anthm@freeswitch.org> | |||
27 | * Marcel Barbulescu <marcelbarbulescu@gmail.com> | |||
28 | * Seven Du <dujinfang@gmail.com> | |||
29 | * Noah Mehl - Open Telecom Foundation <https://opentelecom.foundation> | |||
30 | * | |||
31 | * switch_rtp.c -- RTP | |||
32 | * | |||
33 | */ | |||
34 | #include <switch.h> | |||
35 | #ifndef _MSC_VER | |||
36 | #include <switch_private.h> | |||
37 | #endif | |||
38 | #include <switch_stun.h> | |||
39 | #include <apr_network_io.h> | |||
40 | #undef PACKAGE_NAME"srtp" | |||
41 | #undef PACKAGE_STRING"srtp 2.4.0" | |||
42 | #undef PACKAGE_TARNAME"srtp" | |||
43 | #undef PACKAGE_VERSION"2.4.0" | |||
44 | #undef PACKAGE_BUGREPORT"mcgrew@cisco.com" | |||
45 | #undef VERSION"2.4.0" | |||
46 | #undef PACKAGE"srtp" | |||
47 | #undef inlineinline | |||
48 | #include <srtp.h> | |||
49 | #include <srtp_priv.h> | |||
50 | #include <switch_ssl.h> | |||
51 | #include <switch_jitterbuffer.h> | |||
52 | ||||
53 | //#define DEBUG_TS_ROLLOVER | |||
54 | #ifdef DEBUG_TS_ROLLOVER | |||
55 | #define TS_ROLLOVER_START 4294951295 | |||
56 | #endif | |||
57 | ||||
58 | //#define DEBUG_2833 | |||
59 | //#define RTP_DEBUG_WRITE_DELTA | |||
60 | //#define DEBUG_MISSED_SEQ | |||
61 | //#define DEBUG_EXTRA | |||
62 | //#define DEBUG_RTCP | |||
63 | #define DEBUG_ESTIMATORS_ | |||
64 | ||||
65 | ||||
66 | #define JITTER_LEAD_FRAMES10 10 | |||
67 | #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++ | |||
68 | #define READ_DEC(rtp_session)rtp_session->reading--; switch_mutex_unlock(rtp_session-> read_mutex) rtp_session->reading--; switch_mutex_unlock(rtp_session->read_mutex) | |||
69 | #define WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session-> writing++ switch_mutex_lock(rtp_session->write_mutex); rtp_session->writing++ | |||
70 | #define WRITE_DEC(rtp_session)rtp_session->writing--; switch_mutex_unlock(rtp_session-> write_mutex) rtp_session->writing--; switch_mutex_unlock(rtp_session->write_mutex) | |||
71 | ||||
72 | #define RTP_STUN_FREQ1000000 1000000 | |||
73 | #define rtp_header_len12 12 | |||
74 | #define RTP_START_PORT16384 16384 | |||
75 | #define RTP_END_PORT32768 32768 | |||
76 | #define MASTER_KEY_LEN30 30 | |||
77 | #define RTP_MAGIC_NUMBER42 42 | |||
78 | #define WARN_SRTP_ERRS10 10 | |||
79 | #define MAX_SRTP_ERRS100 100 | |||
80 | #define NTP_TIME_OFFSET2208988800UL 2208988800UL | |||
81 | #define ZRTP_MAGIC_COOKIE0x5a525450 0x5a525450 | |||
82 | static const switch_payload_t INVALID_PT = 255; | |||
83 | ||||
84 | #define DTMF_SANITY(rtp_session->one_second * 30) (rtp_session->one_second * 30) | |||
85 | ||||
86 | #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)) : "-" | |||
87 | ||||
88 | #define STUN_USERNAME_MAX_SIZE513 513 /* From RFC5389: "It MUST contain a UTF-8 [RFC3629] encoded sequence of less than 513 bytes" */ | |||
89 | #define SDP_UFRAG_MAX_SIZE256 256 /* From draft-ietf-mmusic-ice-sip-sdp-24: "the ice-ufrag attribute MUST NOT be longer than 32 | |||
90 | * characters when sending, but an implementation MUST accept up to 256 | |||
91 | * characters when receiving." */ | |||
92 | ||||
93 | static switch_port_t START_PORT = RTP_START_PORT16384; | |||
94 | static switch_port_t END_PORT = RTP_END_PORT32768; | |||
95 | static switch_mutex_t *port_lock = NULL((void*)0); | |||
96 | static switch_size_t do_flush(switch_rtp_t *rtp_session, int force, switch_size_t bytes_in); | |||
97 | ||||
98 | typedef srtp_hdr_t rtp_hdr_t; | |||
99 | ||||
100 | #ifdef ENABLE_ZRTP | |||
101 | #include "zrtp.h" | |||
102 | static zrtp_global_t *zrtp_global; | |||
103 | #ifndef WIN32 | |||
104 | static zrtp_zid_t zid = { "FreeSWITCH01" }; | |||
105 | #else | |||
106 | static zrtp_zid_t zid = { "FreeSWITCH0" }; | |||
107 | #endif | |||
108 | static int zrtp_on = 0; | |||
109 | #define ZRTP_MITM_TRIES 100 | |||
110 | #endif | |||
111 | ||||
112 | #ifdef _MSC_VER | |||
113 | #pragma pack(4) | |||
114 | #endif | |||
115 | ||||
116 | #ifdef _MSC_VER | |||
117 | #pragma pack() | |||
118 | #define ENABLE_SRTP1 | |||
119 | #endif | |||
120 | ||||
121 | static switch_hash_t *alloc_hash = NULL((void*)0); | |||
122 | ||||
123 | typedef struct { | |||
124 | srtp_hdr_t header; | |||
125 | char body[SWITCH_RTP_MAX_BUF_LEN16384+4+sizeof(char *)]; | |||
126 | switch_rtp_hdr_ext_t *ext; | |||
127 | char *ebody; | |||
128 | } rtp_msg_t; | |||
129 | ||||
130 | #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) | |||
131 | ||||
132 | typedef struct { | |||
133 | uint32_t ssrc; | |||
134 | uint8_t seq; | |||
135 | uint8_t r1; | |||
136 | uint8_t r2; | |||
137 | uint8_t r3; | |||
138 | } rtcp_fir_t; | |||
139 | ||||
140 | #ifdef _MSC_VER | |||
141 | #pragma pack(push, r1, 1) | |||
142 | #endif | |||
143 | ||||
144 | typedef struct switch_rtcp_sdes_unit_s { | |||
145 | unsigned char type; | |||
146 | unsigned char length; | |||
147 | char value[]; | |||
148 | } switch_rtcp_sdes_unit_t; | |||
149 | ||||
150 | typedef struct { | |||
151 | uint32_t ssrc; | |||
152 | uint8_t parts[4]; | |||
153 | } rtcp_tmmbx_t; | |||
154 | ||||
155 | #if SWITCH_BYTE_ORDER1234 == __BIG_ENDIAN4321 | |||
156 | ||||
157 | typedef struct { | |||
158 | unsigned version:2; | |||
159 | unsigned p:1; | |||
160 | unsigned fmt:5; | |||
161 | unsigned pt:8; | |||
162 | unsigned length:16; | |||
163 | uint32_t send_ssrc; | |||
164 | uint32_t recv_ssrc; | |||
165 | } switch_rtcp_ext_hdr_t; | |||
166 | ||||
167 | #else /* BIG_ENDIAN */ | |||
168 | ||||
169 | typedef struct { | |||
170 | unsigned fmt:5; | |||
171 | unsigned p:1; | |||
172 | unsigned version:2; | |||
173 | unsigned pt:8; | |||
174 | unsigned length:16; | |||
175 | uint32_t send_ssrc; | |||
176 | uint32_t recv_ssrc; | |||
177 | } switch_rtcp_ext_hdr_t; | |||
178 | ||||
179 | #endif | |||
180 | ||||
181 | #ifdef _MSC_VER | |||
182 | #pragma pack(pop, r1) | |||
183 | #endif | |||
184 | ||||
185 | #define KALMAN_SYSTEM_MODELS3 3 /*loss, jitter, rtt*/ | |||
186 | #define EST_LOSS0 0 | |||
187 | #define EST_JITTER1 1 | |||
188 | #define EST_RTT2 2 | |||
189 | ||||
190 | typedef struct { | |||
191 | switch_rtcp_ext_hdr_t header; | |||
192 | char body[SWITCH_RTCP_MAX_BUF_LEN16384]; | |||
193 | } rtcp_ext_msg_t; | |||
194 | ||||
195 | typedef struct { | |||
196 | switch_rtcp_hdr_t header; | |||
197 | char body[SWITCH_RTCP_MAX_BUF_LEN16384]; | |||
198 | } rtcp_msg_t; | |||
199 | ||||
200 | ||||
201 | typedef enum { | |||
202 | VAD_FIRE_TALK = (1 << 0), | |||
203 | VAD_FIRE_NOT_TALK = (1 << 1) | |||
204 | } vad_talk_mask_t; | |||
205 | ||||
206 | struct switch_rtp_vad_data { | |||
207 | switch_core_session_t *session; | |||
208 | switch_codec_t vad_codec; | |||
209 | switch_codec_t *read_codec; | |||
210 | uint32_t bg_level; | |||
211 | uint32_t bg_count; | |||
212 | uint32_t bg_len; | |||
213 | uint32_t diff_level; | |||
214 | uint8_t hangunder; | |||
215 | uint8_t hangunder_hits; | |||
216 | uint8_t hangover; | |||
217 | uint8_t hangover_hits; | |||
218 | uint8_t cng_freq; | |||
219 | uint8_t cng_count; | |||
220 | switch_vad_flag_t flags; | |||
221 | uint32_t ts; | |||
222 | uint8_t start; | |||
223 | uint8_t start_count; | |||
224 | uint8_t scan_freq; | |||
225 | time_t next_scan; | |||
226 | switch_time_t start_talking; | |||
227 | switch_time_t stop_talking; | |||
228 | switch_time_t total_talk_time; | |||
229 | int fire_events; | |||
230 | }; | |||
231 | ||||
232 | struct switch_rtp_rfc2833_data { | |||
233 | switch_queue_t *dtmf_queue; | |||
234 | char out_digit; | |||
235 | unsigned char out_digit_packet[4]; | |||
236 | unsigned int out_digit_sofar; | |||
237 | unsigned int out_digit_sub_sofar; | |||
238 | unsigned int out_digit_dur; | |||
239 | uint16_t in_digit_seq; | |||
240 | uint32_t in_digit_ts; | |||
241 | uint32_t last_in_digit_ts; | |||
242 | uint32_t in_digit_sanity; | |||
243 | uint32_t in_interleaved; | |||
244 | uint32_t timestamp_dtmf; | |||
245 | uint16_t last_duration; | |||
246 | uint32_t flip; | |||
247 | char first_digit; | |||
248 | char last_digit; | |||
249 | switch_queue_t *dtmf_inqueue; | |||
250 | switch_mutex_t *dtmf_mutex; | |||
251 | uint8_t in_digit_queued; | |||
252 | }; | |||
253 | ||||
254 | typedef struct { | |||
255 | char *ice_user; | |||
256 | char *user_ice; | |||
257 | char *luser_ice; | |||
258 | char *pass; | |||
259 | char *rpass; | |||
260 | switch_sockaddr_t *addr; | |||
261 | uint32_t funny_stun; | |||
262 | switch_time_t next_run; | |||
263 | switch_core_media_ice_type_t type; | |||
264 | ice_t *ice_params; | |||
265 | ice_proto_t proto; | |||
266 | uint8_t sending; | |||
267 | uint8_t ready; | |||
268 | uint8_t rready; | |||
269 | int missed_count; | |||
270 | char last_sent_id[13]; | |||
271 | switch_time_t last_ok; | |||
272 | } switch_rtp_ice_t; | |||
273 | ||||
274 | struct switch_rtp; | |||
275 | ||||
276 | static void switch_rtp_dtls_init(); | |||
277 | static void switch_rtp_dtls_destroy(); | |||
278 | ||||
279 | #define MAX_DTLS_MTU4096 4096 | |||
280 | ||||
281 | typedef struct switch_dtls_s { | |||
282 | /* DTLS */ | |||
283 | SSL_CTX *ssl_ctx; | |||
284 | SSL *ssl; | |||
285 | BIO *read_bio; | |||
286 | BIO *write_bio; | |||
287 | BIO *filter_bio; | |||
288 | dtls_fingerprint_t *local_fp; | |||
289 | dtls_fingerprint_t *remote_fp; | |||
290 | dtls_state_t state; | |||
291 | dtls_state_t last_state; | |||
292 | uint8_t new_state; | |||
293 | dtls_type_t type; | |||
294 | switch_size_t bytes; | |||
295 | void *data; | |||
296 | switch_socket_t *sock_output; | |||
297 | switch_sockaddr_t *remote_addr; | |||
298 | char *rsa; | |||
299 | char *pvt; | |||
300 | char *ca; | |||
301 | char *pem; | |||
302 | struct switch_rtp *rtp_session; | |||
303 | int mtu; | |||
304 | } switch_dtls_t; | |||
305 | ||||
306 | typedef int (*dtls_state_handler_t)(switch_rtp_t *, switch_dtls_t *); | |||
307 | ||||
308 | ||||
309 | static int dtls_state_handshake(switch_rtp_t *rtp_session, switch_dtls_t *dtls); | |||
310 | static int dtls_state_ready(switch_rtp_t *rtp_session, switch_dtls_t *dtls); | |||
311 | static int dtls_state_setup(switch_rtp_t *rtp_session, switch_dtls_t *dtls); | |||
312 | static int dtls_state_fail(switch_rtp_t *rtp_session, switch_dtls_t *dtls); | |||
313 | ||||
314 | dtls_state_handler_t dtls_states[DS_INVALID] = {NULL((void*)0), dtls_state_handshake, dtls_state_setup, dtls_state_ready, dtls_state_fail}; | |||
315 | ||||
316 | typedef struct ts_normalize_s { | |||
317 | uint32_t last_ssrc; | |||
318 | uint32_t last_frame; | |||
319 | uint32_t ts; | |||
320 | uint32_t delta; | |||
321 | uint32_t delta_ttl; | |||
322 | int last_external; | |||
323 | } ts_normalize_t; | |||
324 | ||||
325 | struct switch_rtp { | |||
326 | /* | |||
327 | * Two sockets are needed because we might be transcoding protocol families | |||
328 | * (e.g. receive over IPv4 and send over IPv6). In case the protocol | |||
329 | * families are equal, sock_input == sock_output and only one socket is | |||
330 | * used. | |||
331 | */ | |||
332 | switch_socket_t *sock_input, *sock_output, *rtcp_sock_input, *rtcp_sock_output; | |||
333 | switch_pollfd_t *read_pollfd, *rtcp_read_pollfd; | |||
334 | switch_pollfd_t *jb_pollfd; | |||
335 | ||||
336 | switch_sockaddr_t *local_addr, *rtcp_local_addr; | |||
337 | rtp_msg_t send_msg; | |||
338 | rtcp_msg_t rtcp_send_msg; | |||
339 | switch_rtcp_frame_t rtcp_frame; | |||
340 | ||||
341 | uint8_t send_rr; | |||
342 | uint8_t fir_seq; | |||
343 | uint16_t fir_count; | |||
344 | uint16_t pli_count; | |||
345 | uint32_t cur_tmmbr; | |||
346 | uint32_t tmmbr; | |||
347 | uint32_t tmmbn; | |||
348 | ||||
349 | ts_normalize_t ts_norm; | |||
350 | switch_sockaddr_t *remote_addr, *rtcp_remote_addr; | |||
351 | rtp_msg_t recv_msg; | |||
352 | rtcp_msg_t rtcp_recv_msg; | |||
353 | rtcp_msg_t *rtcp_recv_msg_p; | |||
354 | ||||
355 | uint32_t autoadj_window; | |||
356 | uint32_t autoadj_threshold; | |||
357 | uint32_t autoadj_tally; | |||
358 | ||||
359 | uint32_t rtcp_autoadj_window; | |||
360 | uint32_t rtcp_autoadj_threshold; | |||
361 | uint32_t rtcp_autoadj_tally; | |||
362 | ||||
363 | srtp_ctx_t *send_ctx[2]; | |||
364 | srtp_ctx_t *recv_ctx[2]; | |||
365 | ||||
366 | srtp_policy_t send_policy[2]; | |||
367 | srtp_policy_t recv_policy[2]; | |||
368 | ||||
369 | uint32_t srtp_errs[2]; | |||
370 | uint32_t srctp_errs[2]; | |||
371 | ||||
372 | ||||
373 | int srtp_idx_rtp; | |||
374 | int srtp_idx_rtcp; | |||
375 | ||||
376 | switch_dtls_t *dtls; | |||
377 | switch_dtls_t *rtcp_dtls; | |||
378 | ||||
379 | rtp_hdr_t last_rtp_hdr; | |||
380 | ||||
381 | uint16_t seq; | |||
382 | uint32_t ssrc; | |||
383 | uint32_t remote_ssrc; | |||
384 | uint32_t last_jb_read_ssrc; | |||
385 | int8_t sending_dtmf; | |||
386 | uint8_t need_mark; | |||
387 | switch_payload_t payload; | |||
388 | switch_rtp_invalid_handler_t invalid_handler; | |||
389 | void *private_data; | |||
390 | uint32_t ts; | |||
391 | //uint32_t last_clock_ts; | |||
392 | uint32_t last_write_ts; | |||
393 | uint32_t last_read_ts; | |||
394 | uint32_t prev_read_ts; | |||
395 | uint32_t last_cng_ts; | |||
396 | uint32_t last_write_samplecount; | |||
397 | uint32_t delay_samples; | |||
398 | uint32_t next_write_samplecount; | |||
399 | uint32_t max_next_write_samplecount; | |||
400 | uint32_t queue_delay; | |||
401 | switch_time_t last_write_timestamp; | |||
402 | uint32_t flags[SWITCH_RTP_FLAG_INVALID]; | |||
403 | switch_memory_pool_t *pool; | |||
404 | switch_sockaddr_t *from_addr, *rtp_from_addr, *rtcp_from_addr, *bundle_internal_addr, *bundle_external_addr; | |||
405 | char *rx_host; | |||
406 | switch_port_t rx_port; | |||
407 | switch_rtp_ice_t ice; | |||
408 | switch_rtp_ice_t rtcp_ice; | |||
409 | char *timer_name; | |||
410 | char *local_host_str; | |||
411 | char *remote_host_str; | |||
412 | char *eff_remote_host_str; | |||
413 | switch_time_t first_stun; | |||
414 | switch_time_t last_stun; | |||
415 | uint32_t wrong_addrs; | |||
416 | uint32_t samples_per_interval; | |||
417 | uint32_t samples_per_second; | |||
418 | uint32_t conf_samples_per_interval; | |||
419 | switch_time_t rtcp_last_sent; | |||
420 | uint32_t rsamples_per_interval; | |||
421 | uint32_t ms_per_packet; | |||
422 | uint32_t one_second; | |||
423 | uint32_t consecutive_flaws; | |||
424 | uint32_t jitter_lead; | |||
425 | double old_mean; | |||
426 | switch_time_t next_stat_check_time; | |||
427 | switch_port_t local_port; | |||
428 | switch_port_t remote_port; | |||
429 | switch_port_t eff_remote_port; | |||
430 | switch_port_t remote_rtcp_port; | |||
431 | ||||
432 | struct switch_rtp_vad_data vad_data; | |||
433 | struct switch_rtp_rfc2833_data dtmf_data; | |||
434 | switch_payload_t te; | |||
435 | switch_payload_t recv_te; | |||
436 | switch_payload_t cng_pt; | |||
437 | switch_mutex_t *flag_mutex; | |||
438 | switch_mutex_t *read_mutex; | |||
439 | switch_mutex_t *write_mutex; | |||
440 | switch_mutex_t *ice_mutex; | |||
441 | switch_timer_t timer; | |||
442 | switch_timer_t write_timer; | |||
443 | uint8_t ready; | |||
444 | uint8_t cn; | |||
445 | switch_jb_t *jb; | |||
446 | switch_jb_t *vb; | |||
447 | switch_jb_t *vbw; | |||
448 | uint32_t max_missed_packets; | |||
449 | uint32_t missed_count; | |||
450 | switch_time_t last_media; | |||
451 | uint32_t media_timeout; | |||
452 | rtp_msg_t write_msg; | |||
453 | switch_rtp_crypto_key_t *crypto_keys[SWITCH_RTP_CRYPTO_MAX]; | |||
454 | int reading; | |||
455 | int writing; | |||
456 | char *stun_ip; | |||
457 | switch_port_t stun_port; | |||
458 | int from_auto; | |||
459 | uint32_t cng_count; | |||
460 | switch_rtp_bug_flag_t rtp_bugs; | |||
461 | switch_rtp_stats_t stats; | |||
462 | switch_rtcp_video_stats_t rtcp_vstats; | |||
463 | uint32_t clean_stream; | |||
464 | uint32_t bad_stream; | |||
465 | uint32_t recovering_stream; | |||
466 | ||||
467 | uint32_t hot_hits; | |||
468 | uint32_t sync_packets; | |||
469 | int rtcp_interval; | |||
470 | int rtcp_sent_packets; | |||
471 | switch_bool_t rtcp_fresh_frame; | |||
472 | ||||
473 | switch_time_t send_time; | |||
474 | switch_byte_t auto_adj_used; | |||
475 | switch_byte_t rtcp_auto_adj_used; | |||
476 | uint8_t pause_jb; | |||
477 | uint16_t last_seq; | |||
478 | uint16_t last_write_seq; | |||
479 | uint8_t video_delta_mode; | |||
480 | switch_time_t last_read_time; | |||
481 | switch_size_t last_flush_packet_count; | |||
482 | uint32_t interdigit_delay; | |||
483 | switch_core_session_t *session; | |||
484 | payload_map_t **pmaps; | |||
485 | payload_map_t *pmap_tail; | |||
486 | kalman_estimator_t *estimators[KALMAN_SYSTEM_MODELS3]; | |||
487 | cusum_kalman_detector_t *detectors[KALMAN_SYSTEM_MODELS3]; | |||
488 | int ice_adj; | |||
489 | uint8_t has_rtp; | |||
490 | uint8_t has_rtcp; | |||
491 | uint8_t has_ice; | |||
492 | uint8_t punts; | |||
493 | uint8_t clean; | |||
494 | uint32_t last_max_vb_frames; | |||
495 | int skip_timer; | |||
496 | uint32_t prev_nacks_inflight; | |||
497 | #ifdef ENABLE_ZRTP | |||
498 | zrtp_session_t *zrtp_session; | |||
499 | zrtp_profile_t *zrtp_profile; | |||
500 | zrtp_stream_t *zrtp_stream; | |||
501 | int zrtp_mitm_tries; | |||
502 | int zinit; | |||
503 | #endif | |||
504 | ||||
505 | }; | |||
506 | ||||
507 | struct switch_rtcp_report_block { | |||
508 | uint32_t ssrc; /* The SSRC identifier of the source to which the information in this reception report block pertains. */ | |||
509 | unsigned int fraction :8; /* The fraction of RTP data packets from source SSRC_n lost since the previous SR or RR packet was sent */ | |||
510 | int lost :24; /* The total number of RTP data packets from source SSRC_n that have been lost since the beginning of reception */ | |||
511 | uint32_t highest_sequence_number_received; | |||
512 | 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. */ | |||
513 | uint32_t lsr; /* The middle 32 bits out of 64 in the NTP timestamp */ | |||
514 | 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 */ | |||
515 | }; | |||
516 | ||||
517 | struct switch_rtcp_sr_head { | |||
518 | uint32_t ssrc; | |||
519 | uint32_t ntp_msw; | |||
520 | uint32_t ntp_lsw; | |||
521 | uint32_t ts; | |||
522 | uint32_t pc; | |||
523 | uint32_t oc; | |||
524 | }; | |||
525 | ||||
526 | struct switch_rtcp_sender_info { | |||
527 | uint32_t ntp_msw; | |||
528 | uint32_t ntp_lsw; | |||
529 | uint32_t ts; | |||
530 | uint32_t pc; | |||
531 | uint32_t oc; | |||
532 | }; | |||
533 | ||||
534 | struct switch_rtcp_sender_report { | |||
535 | uint32_t ssrc; | |||
536 | struct switch_rtcp_sender_info sender_info; | |||
537 | struct switch_rtcp_report_block report_block; | |||
538 | }; | |||
539 | ||||
540 | struct switch_rtcp_receiver_report { | |||
541 | uint32_t ssrc; | |||
542 | struct switch_rtcp_report_block report_block; | |||
543 | }; | |||
544 | ||||
545 | typedef enum { | |||
546 | RESULT_CONTINUE, | |||
547 | RESULT_GOTO_END, | |||
548 | RESULT_GOTO_RECVFROM, | |||
549 | RESULT_GOTO_TIMERCHECK | |||
550 | } handle_rfc2833_result_t; | |||
551 | ||||
552 | static void do_2833(switch_rtp_t *rtp_session); | |||
553 | ||||
554 | ||||
555 | #define rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio") rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio") | |||
556 | ||||
557 | ||||
558 | static void switch_rtp_change_ice_dest(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, const char *host, switch_port_t port) | |||
559 | { | |||
560 | int is_rtcp = ice == &rtp_session->rtcp_ice; | |||
561 | const char *err = ""; | |||
562 | ||||
563 | 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__, 563); | |||
564 | ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port = port; | |||
565 | ice->missed_count = 0; | |||
566 | ||||
567 | if (is_rtcp) { | |||
568 | ice->addr = rtp_session->rtcp_remote_addr; | |||
569 | } else { | |||
570 | switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err); | |||
571 | ||||
572 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
573 | ice->addr = rtp_session->remote_addr; | |||
574 | } | |||
575 | } | |||
576 | ||||
577 | } | |||
578 | ||||
579 | ||||
580 | ||||
581 | static handle_rfc2833_result_t handle_rfc2833(switch_rtp_t *rtp_session, switch_size_t bytes, int *do_cng) | |||
582 | { | |||
583 | ||||
584 | if (rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]) { | |||
585 | rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]++; | |||
586 | ||||
587 | if (rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] > DTMF_SANITY(rtp_session->one_second * 30)) { | |||
588 | rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = 0; | |||
589 | } else { | |||
590 | rtp_session->stats.inbound.last_processed_seq = 0; | |||
591 | } | |||
592 | } | |||
593 | ||||
594 | ||||
595 | #ifdef DEBUG_2833 | |||
596 | if (rtp_session->dtmf_data.in_digit_sanity && !(rtp_session->dtmf_data.in_digit_sanity % 100)) { | |||
597 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 597, ((void*)0), SWITCH_LOG_ERROR, "sanity %d %ld\n", rtp_session->dtmf_data.in_digit_sanity, bytes); | |||
598 | } | |||
599 | #endif | |||
600 | ||||
601 | if (rtp_session->dtmf_data.in_digit_sanity && !--rtp_session->dtmf_data.in_digit_sanity) { | |||
602 | ||||
603 | rtp_session->dtmf_data.last_digit = 0; | |||
604 | rtp_session->dtmf_data.in_digit_ts = 0; | |||
605 | rtp_session->dtmf_data.in_digit_queued = 0; | |||
606 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 606, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Failed DTMF sanity check.\n"); | |||
607 | } | |||
608 | ||||
609 | if (!bytes) return RESULT_CONTINUE; | |||
610 | ||||
611 | ||||
612 | /* RFC2833 ... like all RFC RE: VoIP, guaranteed to drive you to insanity! | |||
613 | We know the real rules here, but if we enforce them, it's an interop nightmare so, | |||
614 | we put up with as much as we can so we don't have to deal with being punished for | |||
615 | doing it right. Nice guys finish last! | |||
616 | */ | |||
617 | ||||
618 | if (bytes > rtp_header_len12 && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && | |||
619 | rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) { | |||
620 | switch_size_t len = bytes - rtp_header_len12; | |||
621 | 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); | |||
622 | int end; | |||
623 | uint16_t duration; | |||
624 | char key; | |||
625 | uint16_t in_digit_seq; | |||
626 | uint32_t ts; | |||
627 | ||||
628 | rtp_session->stats.inbound.last_processed_seq = 0; | |||
629 | ||||
630 | if (!(packet[0] || packet[1] || packet[2] || packet[3]) && len >= 8) { | |||
631 | packet += 4; | |||
632 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 632, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, "DTMF payload offset by 4 bytes.\n"); | |||
633 | } | |||
634 | ||||
635 | if (!(packet[0] || packet[1] || packet[2] || packet[3]) && rtp_session->dtmf_data.in_digit_ts) { | |||
636 | switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session"); | |||
637 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 637, (const char*)(session), SWITCH_LOG_ERROR, "Failed DTMF payload check.\n"); | |||
638 | rtp_session->dtmf_data.last_digit = 0; | |||
639 | rtp_session->dtmf_data.in_digit_ts = 0; | |||
640 | rtp_session->dtmf_data.in_digit_sanity = 0; | |||
641 | rtp_session->dtmf_data.in_digit_queued = 0; | |||
642 | } | |||
643 | ||||
644 | end = packet[1] & 0x80 ? 1 : 0; | |||
645 | duration = (packet[2] << 8) + packet[3]; | |||
646 | key = switch_rfc2833_to_char(packet[0]); | |||
647 | in_digit_seq = ntohs((uint16_t) rtp_session->last_rtp_hdr.seq)__bswap_16 ((uint16_t) rtp_session->last_rtp_hdr.seq); | |||
648 | ts = htonl(rtp_session->last_rtp_hdr.ts)__bswap_32 (rtp_session->last_rtp_hdr.ts); | |||
649 | ||||
650 | if (rtp_session->flags[SWITCH_RTP_FLAG_PASS_RFC2833]) { | |||
651 | ||||
652 | if (end) { | |||
653 | rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = DTMF_SANITY(rtp_session->one_second * 30) - 3; | |||
654 | } else if (!rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]) { | |||
655 | rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = 1; | |||
656 | } | |||
657 | ||||
658 | return RESULT_CONTINUE; | |||
659 | } | |||
660 | ||||
661 | if (in_digit_seq < rtp_session->dtmf_data.in_digit_seq) { | |||
662 | if (rtp_session->dtmf_data.in_digit_seq - in_digit_seq > 100) { | |||
663 | rtp_session->dtmf_data.in_digit_seq = 0; | |||
664 | } | |||
665 | } | |||
666 | #ifdef DEBUG_2833 | |||
667 | if (!(packet[0] || packet[1] || packet[2] || packet[3]) && len >= 8) { | |||
668 | len -= 4; | |||
669 | } | |||
670 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 670, ((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]); | |||
671 | #endif | |||
672 | ||||
673 | if (in_digit_seq > rtp_session->dtmf_data.in_digit_seq) { | |||
674 | ||||
675 | rtp_session->dtmf_data.in_digit_seq = in_digit_seq; | |||
676 | #ifdef DEBUG_2833 | |||
677 | ||||
678 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 678, ((void*)0), SWITCH_LOG_ERROR, "read: %c %u %u %u %u %d %d %s\n", | |||
679 | key, in_digit_seq, rtp_session->dtmf_data.in_digit_seq, | |||
680 | ts, duration, rtp_session->last_rtp_hdr.m, end, end && !rtp_session->dtmf_data.in_digit_ts ? "ignored" : ""); | |||
681 | #endif | |||
682 | ||||
683 | ||||
684 | if (rtp_session->dtmf_data.in_digit_ts && rtp_session->dtmf_data.in_digit_ts != ts) { | |||
685 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 685, ((void*)0), SWITCH_LOG_DEBUG, "TS changed from last packet, resetting....\n"); | |||
686 | rtp_session->dtmf_data.last_digit = 0; | |||
687 | rtp_session->dtmf_data.in_digit_ts = 0; | |||
688 | rtp_session->dtmf_data.in_digit_sanity = 0; | |||
689 | rtp_session->dtmf_data.in_digit_queued = 0; | |||
690 | } | |||
691 | ||||
692 | ||||
693 | if (!rtp_session->dtmf_data.in_digit_queued && rtp_session->dtmf_data.in_digit_ts) { | |||
694 | if ((rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION)) { | |||
695 | switch_dtmf_t dtmf = { key, switch_core_min_dtmf_duration(0), 0, SWITCH_DTMF_RTP }; | |||
696 | #ifdef DEBUG_2833 | |||
697 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 697, ((void*)0), SWITCH_LOG_ERROR, "Early Queuing digit %c:%d\n", dtmf.digit, dtmf.duration / 8); | |||
698 | #endif | |||
699 | switch_rtp_queue_rfc2833_in(rtp_session, &dtmf); | |||
700 | rtp_session->dtmf_data.in_digit_queued = 1; | |||
701 | } | |||
702 | ||||
703 | if (rtp_session->jb && (rtp_session->rtp_bugs & RTP_BUG_FLUSH_JB_ON_DTMF)) { | |||
704 | switch_jb_reset(rtp_session->jb); | |||
705 | } | |||
706 | ||||
707 | } | |||
708 | ||||
709 | /* only set sanity if we do NOT ignore the packet */ | |||
710 | if (rtp_session->dtmf_data.in_digit_ts) { | |||
711 | rtp_session->dtmf_data.in_digit_sanity = 2000; | |||
712 | } | |||
713 | ||||
714 | if (rtp_session->dtmf_data.last_duration > duration && | |||
715 | rtp_session->dtmf_data.last_duration > 0xFC17 && ts == rtp_session->dtmf_data.in_digit_ts) { | |||
716 | rtp_session->dtmf_data.flip++; | |||
717 | } | |||
718 | ||||
719 | if (end) { | |||
720 | if (!rtp_session->dtmf_data.in_digit_ts && rtp_session->dtmf_data.last_in_digit_ts != ts) { | |||
721 | #ifdef DEBUG_2833 | |||
722 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 722, ((void*)0), SWITCH_LOG_ERROR, "start with end packet %d\n", ts); | |||
723 | #endif | |||
724 | rtp_session->dtmf_data.last_in_digit_ts = ts; | |||
725 | rtp_session->dtmf_data.in_digit_ts = ts; | |||
726 | rtp_session->dtmf_data.first_digit = key; | |||
727 | rtp_session->dtmf_data.in_digit_sanity = 2000; | |||
728 | } | |||
729 | if (rtp_session->dtmf_data.in_digit_ts) { | |||
730 | switch_dtmf_t dtmf = { key, duration, 0, SWITCH_DTMF_RTP }; | |||
731 | ||||
732 | if (ts > rtp_session->dtmf_data.in_digit_ts) { | |||
733 | dtmf.duration += (ts - rtp_session->dtmf_data.in_digit_ts); | |||
734 | } | |||
735 | if (rtp_session->dtmf_data.flip) { | |||
736 | dtmf.duration += rtp_session->dtmf_data.flip * 0xFFFF; | |||
737 | rtp_session->dtmf_data.flip = 0; | |||
738 | #ifdef DEBUG_2833 | |||
739 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 739, ((void*)0), SWITCH_LOG_ERROR, "you're welcome!\n"); | |||
740 | #endif | |||
741 | } | |||
742 | #ifdef DEBUG_2833 | |||
743 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 743, ((void*)0), SWITCH_LOG_ERROR, "done digit=%c ts=%u start_ts=%u dur=%u ddur=%u\n", | |||
744 | dtmf.digit, ts, rtp_session->dtmf_data.in_digit_ts, duration, dtmf.duration); | |||
745 | #endif | |||
746 | ||||
747 | if (!(rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION) && !rtp_session->dtmf_data.in_digit_queued) { | |||
748 | #ifdef DEBUG_2833 | |||
749 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 749, ((void*)0), SWITCH_LOG_ERROR, "Queuing digit %c:%d\n", dtmf.digit, dtmf.duration / 8); | |||
750 | #endif | |||
751 | switch_rtp_queue_rfc2833_in(rtp_session, &dtmf); | |||
752 | } | |||
753 | ||||
754 | rtp_session->dtmf_data.last_digit = rtp_session->dtmf_data.first_digit; | |||
755 | ||||
756 | rtp_session->dtmf_data.in_digit_ts = 0; | |||
757 | rtp_session->dtmf_data.in_digit_sanity = 0; | |||
758 | rtp_session->dtmf_data.in_digit_queued = 0; | |||
759 | *do_cng = 1; | |||
760 | } else { | |||
761 | if (!switch_rtp_ready(rtp_session)) { | |||
762 | return RESULT_GOTO_END; | |||
763 | } | |||
764 | switch_cond_next(); | |||
765 | return RESULT_GOTO_RECVFROM; | |||
766 | } | |||
767 | ||||
768 | } else if (!rtp_session->dtmf_data.in_digit_ts) { | |||
769 | #ifdef DEBUG_2833 | |||
770 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 770, ((void*)0), SWITCH_LOG_ERROR, "start %d [%c]\n", ts, key); | |||
771 | #endif | |||
772 | rtp_session->dtmf_data.in_digit_ts = ts; | |||
773 | rtp_session->dtmf_data.last_in_digit_ts = ts; | |||
774 | rtp_session->dtmf_data.first_digit = key; | |||
775 | rtp_session->dtmf_data.in_digit_sanity = 2000; | |||
776 | } | |||
777 | ||||
778 | rtp_session->dtmf_data.last_duration = duration; | |||
779 | } else { | |||
780 | #ifdef DEBUG_2833 | |||
781 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 781, ((void*)0), SWITCH_LOG_ERROR, "drop: %c %u %u %u %u %d %d\n", | |||
782 | key, in_digit_seq, rtp_session->dtmf_data.in_digit_seq, ts, duration, rtp_session->last_rtp_hdr.m, end); | |||
783 | #endif | |||
784 | switch_cond_next(); | |||
785 | return RESULT_GOTO_RECVFROM; | |||
786 | } | |||
787 | } | |||
788 | ||||
789 | if (rtp_session->dtmf_data.in_digit_ts) { | |||
790 | if (!switch_rtp_ready(rtp_session)) { | |||
791 | return RESULT_GOTO_END; | |||
792 | } | |||
793 | ||||
794 | if (!rtp_session->dtmf_data.in_interleaved && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te) { | |||
795 | /* Drat, they are sending audio still as well as DTMF ok fine..... *sigh* */ | |||
796 | rtp_session->dtmf_data.in_interleaved = 1; | |||
797 | } | |||
798 | ||||
799 | if (rtp_session->dtmf_data.in_interleaved || (rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION)) { | |||
800 | if (rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) { | |||
801 | return RESULT_GOTO_RECVFROM; | |||
802 | } | |||
803 | } else { | |||
804 | *do_cng = 1; | |||
805 | return RESULT_GOTO_TIMERCHECK; | |||
806 | } | |||
807 | } | |||
808 | ||||
809 | return RESULT_CONTINUE; | |||
810 | } | |||
811 | ||||
812 | static int rtp_write_ready(switch_rtp_t *rtp_session, uint32_t bytes, int line); | |||
813 | static int global_init = 0; | |||
814 | static int rtp_common_write(switch_rtp_t *rtp_session, | |||
815 | rtp_msg_t *send_msg, void *data, uint32_t datalen, switch_payload_t payload, uint32_t timestamp, switch_frame_flag_t *flags); | |||
816 | ||||
817 | ||||
818 | static switch_status_t ice_out(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice) | |||
819 | { | |||
820 | uint8_t buf[256] = { 0 }; | |||
821 | switch_stun_packet_t *packet; | |||
822 | unsigned int elapsed; | |||
823 | switch_size_t bytes; | |||
824 | switch_status_t status = SWITCH_STATUS_SUCCESS; | |||
825 | //switch_sockaddr_t *remote_addr = rtp_session->remote_addr; | |||
826 | switch_socket_t *sock_output = rtp_session->sock_output; | |||
827 | switch_time_t now = switch_micro_time_now(); | |||
828 | ||||
829 | if (ice->type & ICE_LITE) { | |||
830 | // no connectivity checks for ICE-Lite | |||
831 | return SWITCH_STATUS_BREAK; | |||
832 | } | |||
833 | ||||
834 | if (ice->next_run && ice->next_run > now) { | |||
835 | return SWITCH_STATUS_BREAK; | |||
836 | } | |||
837 | ||||
838 | ice->next_run = now + RTP_STUN_FREQ1000000; | |||
839 | ||||
840 | if (ice == &rtp_session->rtcp_ice && rtp_session->rtcp_sock_output) { | |||
841 | sock_output = rtp_session->rtcp_sock_output; | |||
842 | } | |||
843 | ||||
844 | if (!sock_output) { | |||
845 | return SWITCH_STATUS_FALSE; | |||
846 | } | |||
847 | ||||
848 | switch_assert(rtp_session != NULL)((rtp_session != ((void*)0)) ? (void) (0) : __assert_fail ("rtp_session != ((void*)0)" , "src/switch_rtp.c", 848, __extension__ __PRETTY_FUNCTION__) ); | |||
849 | switch_assert(ice->ice_user != NULL)((ice->ice_user != ((void*)0)) ? (void) (0) : __assert_fail ("ice->ice_user != ((void*)0)", "src/switch_rtp.c", 849, __extension__ __PRETTY_FUNCTION__)); | |||
850 | ||||
851 | READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session-> reading++; | |||
852 | ||||
853 | if (rtp_session->last_stun) { | |||
854 | elapsed = (unsigned int) ((switch_micro_time_now() - rtp_session->last_stun) / 1000); | |||
855 | ||||
856 | if (elapsed > 30000) { | |||
857 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 857, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, "No %s stun for a long time!\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
858 | rtp_session->last_stun = switch_micro_time_now(); | |||
859 | //status = SWITCH_STATUS_GENERR; | |||
860 | //goto end; | |||
861 | } | |||
862 | } | |||
863 | ||||
864 | packet = switch_stun_packet_build_header(SWITCH_STUN_BINDING_REQUEST, NULL((void*)0), buf); | |||
865 | switch_stun_packet_attribute_add_username(packet, ice->ice_user, (uint16_t)strlen(ice->ice_user)); | |||
866 | ||||
867 | memcpy(ice->last_sent_id, packet->header.id, 12); | |||
868 | ||||
869 | //if (ice->pass && ice->type == ICE_GOOGLE_JINGLE) { | |||
870 | // switch_stun_packet_attribute_add_password(packet, ice->pass, (uint16_t)strlen(ice->pass)); | |||
871 | //} | |||
872 | ||||
873 | if ((ice->type & ICE_VANILLA)) { | |||
874 | char sw[128] = ""; | |||
875 | ||||
876 | switch_stun_packet_attribute_add_priority(packet, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].priority); | |||
877 | ||||
878 | switch_snprintf(sw, sizeof(sw), "FreeSWITCH (%s)", switch_version_revision_human()); | |||
879 | switch_stun_packet_attribute_add_software(packet, sw, (uint16_t)strlen(sw)); | |||
880 | ||||
881 | if ((ice->type & ICE_CONTROLLED)) { | |||
882 | switch_stun_packet_attribute_add_controlled(packet); | |||
883 | } else { | |||
884 | switch_stun_packet_attribute_add_controlling(packet); | |||
885 | switch_stun_packet_attribute_add_use_candidate(packet); | |||
886 | } | |||
887 | ||||
888 | switch_stun_packet_attribute_add_integrity(packet, ice->rpass); | |||
889 | switch_stun_packet_attribute_add_fingerprint(packet); | |||
890 | } | |||
891 | ||||
892 | ||||
893 | bytes = switch_stun_packet_length(packet)__bswap_16 (packet->header.length) + (sizeof(switch_stun_packet_header_t )); | |||
894 | ||||
895 | #ifdef DEBUG_EXTRA | |||
896 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 896, (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_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
897 | #endif | |||
898 | switch_socket_sendto(sock_output, ice->addr, 0, (void *) packet, &bytes); | |||
899 | ||||
900 | ice->sending = 3; | |||
901 | ||||
902 | // end: | |||
903 | READ_DEC(rtp_session)rtp_session->reading--; switch_mutex_unlock(rtp_session-> read_mutex); | |||
904 | ||||
905 | return status; | |||
906 | } | |||
907 | ||||
908 | int icecmp(const char *them, switch_rtp_ice_t *ice) | |||
909 | { | |||
910 | if (strchr(them, ':')) { | |||
911 | return strcmp(them, ice->user_ice); | |||
912 | } | |||
913 | ||||
914 | return strcmp(them, ice->luser_ice); | |||
915 | } | |||
916 | ||||
917 | static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len) | |||
918 | { | |||
919 | switch_stun_packet_t *packet; | |||
920 | switch_stun_packet_attribute_t *attr; | |||
921 | void *end_buf; | |||
922 | char username[STUN_USERNAME_MAX_SIZE513] = { 0 }; | |||
923 | unsigned char buf[1500] = { 0 }; | |||
924 | switch_size_t cpylen = len; | |||
925 | int xlen = 0; | |||
926 | int ok = 1; | |||
927 | uint32_t *pri = NULL((void*)0); | |||
928 | int is_rtcp = ice == &rtp_session->rtcp_ice; | |||
929 | uint32_t elapsed; | |||
930 | switch_time_t ref_point; | |||
931 | ||||
932 | //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
933 | // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s CALL\n", rtp_type(rtp_session)); | |||
934 | //} | |||
935 | ||||
936 | if (!switch_rtp_ready(rtp_session) || zstr(ice->user_ice)_zstr(ice->user_ice) || zstr(ice->ice_user)_zstr(ice->ice_user)) { | |||
937 | return; | |||
938 | } | |||
939 | ||||
940 | READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session-> reading++; | |||
941 | WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session-> writing++; | |||
942 | ||||
943 | switch_mutex_lock(rtp_session->ice_mutex); | |||
944 | ||||
945 | if (!switch_rtp_ready(rtp_session)) { | |||
946 | goto end; | |||
947 | } | |||
948 | ||||
949 | if (cpylen > sizeof(buf)) { | |||
950 | cpylen = sizeof(buf); | |||
951 | } | |||
952 | ||||
953 | ||||
954 | memcpy(buf, data, cpylen); | |||
955 | packet = switch_stun_packet_parse(buf, (uint32_t)cpylen); | |||
956 | if (!packet) { | |||
957 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 957, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Invalid STUN/ICE packet received %ld bytes\n", (long)cpylen); | |||
958 | goto end; | |||
959 | ||||
960 | } | |||
961 | ||||
962 | rtp_session->last_stun = switch_micro_time_now(); | |||
963 | ||||
964 | if (!rtp_session->first_stun) { | |||
965 | rtp_session->first_stun = rtp_session->last_stun; | |||
966 | } | |||
967 | ||||
968 | if (ice->last_ok) { | |||
969 | ref_point = ice->last_ok; | |||
970 | } else { | |||
971 | ref_point = rtp_session->first_stun; | |||
972 | } | |||
973 | ||||
974 | elapsed = (unsigned int) ((switch_micro_time_now() - ref_point) / 1000); | |||
975 | ||||
976 | ||||
977 | end_buf = buf + ((sizeof(buf) > packet->header.length) ? packet->header.length : sizeof(buf)); | |||
978 | ||||
979 | switch_stun_packet_first_attribute(packet, attr)attr = (switch_stun_packet_attribute_t *)(&packet->first_attribute );; | |||
980 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 980, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG8, "%s STUN PACKET TYPE: %s\n", | |||
981 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), switch_stun_value_to_name(SWITCH_STUN_TYPE_PACKET_TYPE, packet->header.type)); | |||
982 | do { | |||
983 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 983, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG8, "|---: %s STUN ATTR %d %x %s\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), attr->type, attr->type, | |||
984 | switch_stun_value_to_name(SWITCH_STUN_TYPE_ATTRIBUTE, attr->type)); | |||
985 | ||||
986 | switch (attr->type) { | |||
987 | case SWITCH_STUN_ATTR_USE_CAND: | |||
988 | { | |||
989 | ice->rready = 1; | |||
990 | } | |||
991 | break; | |||
992 | case SWITCH_STUN_ATTR_ERROR_CODE: | |||
993 | { | |||
994 | switch_stun_error_code_t *err = (switch_stun_error_code_t *) attr->value; | |||
995 | uint32_t code = (err->code * 100) + err->number; | |||
996 | ||||
997 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 997, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, "%s got %s stun binding response %u\n", | |||
998 | rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel (rtp_session->session)) : "-", | |||
999 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), | |||
1000 | code | |||
1001 | ); | |||
1002 | ||||
1003 | if ((ice->type & ICE_VANILLA) && code == 487) { | |||
1004 | if ((ice->type & ICE_CONTROLLED)) { | |||
1005 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1005, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, "%s STUN Changing role to CONTROLLING\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
1006 | ice->type &= ~ICE_CONTROLLED; | |||
1007 | } else { | |||
1008 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1008, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, "%s STUN Changing role to CONTROLLED\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
1009 | ice->type |= ICE_CONTROLLED; | |||
1010 | } | |||
1011 | packet->header.type = SWITCH_STUN_BINDING_RESPONSE; | |||
1012 | } | |||
1013 | ||||
1014 | } | |||
1015 | break; | |||
1016 | case SWITCH_STUN_ATTR_MAPPED_ADDRESS: | |||
1017 | { | |||
1018 | char ip[50]; | |||
1019 | uint16_t port; | |||
1020 | switch_stun_packet_attribute_get_mapped_address(attr, ip, sizeof(ip), &port); | |||
1021 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1021, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s:%d\n", ip, port); | |||
1022 | } | |||
1023 | break; | |||
1024 | case SWITCH_STUN_ATTR_XOR_MAPPED_ADDRESS: | |||
1025 | { | |||
1026 | char ip[50]; | |||
1027 | uint16_t port; | |||
1028 | switch_stun_packet_attribute_get_xor_mapped_address(attr, &packet->header, ip, sizeof(ip), &port); | |||
1029 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1029, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s:%d\n", ip, port); | |||
1030 | } | |||
1031 | break; | |||
1032 | case SWITCH_STUN_ATTR_USERNAME: | |||
1033 | { | |||
1034 | switch_stun_packet_attribute_get_username(attr, username, sizeof(username)); | |||
1035 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1035, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s\n", username); | |||
1036 | } | |||
1037 | break; | |||
1038 | ||||
1039 | case SWITCH_STUN_ATTR_PRIORITY: | |||
1040 | { | |||
1041 | uint32_t priority = 0; | |||
1042 | pri = (uint32_t *) attr->value; | |||
1043 | priority = ntohl(*pri)__bswap_32 (*pri); | |||
1044 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1044, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %u\n", priority); | |||
1045 | ok = priority == ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].priority; | |||
1046 | } | |||
1047 | break; | |||
1048 | } | |||
1049 | ||||
1050 | 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))) { | |||
1051 | break; | |||
1052 | } | |||
1053 | ||||
1054 | xlen += 4 + switch_stun_attribute_padded_length(attr)(int16_t)((attr->length & 0x3) ? 0x4 + (attr->length & ~0x3) : attr->length); | |||
1055 | } while (xlen <= packet->header.length); | |||
1056 | ||||
1057 | if ((ice->type & ICE_GOOGLE_JINGLE) && ok) { | |||
1058 | ok = !strcmp(ice->user_ice, username); | |||
1059 | } | |||
1060 | ||||
1061 | if (packet->header.type != SWITCH_STUN_BINDING_REQUEST && packet->header.type != SWITCH_STUN_BINDING_RESPONSE) { | |||
1062 | goto end; | |||
1063 | } | |||
1064 | ||||
1065 | if ((ice->type & ICE_VANILLA)) { | |||
1066 | if (!ok) ok = !memcmp(packet->header.id, ice->last_sent_id, 12); | |||
1067 | ||||
1068 | if (packet->header.type == SWITCH_STUN_BINDING_RESPONSE) { | |||
1069 | ok = 1; | |||
1070 | if (!ice->rready) { | |||
1071 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
1072 | rtp_session->ice.rready = 1; | |||
1073 | rtp_session->rtcp_ice.rready = 1; | |||
1074 | } else { | |||
1075 | ice->rready = 1; | |||
1076 | } | |||
1077 | ||||
1078 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
1079 | switch_core_session_video_reinit(rtp_session->session); | |||
1080 | } | |||
1081 | } | |||
1082 | } | |||
1083 | ||||
1084 | if (!ok && ice == &rtp_session->ice && rtp_session->rtcp_ice.ice_params && pri && | |||
1085 | *pri == rtp_session->rtcp_ice.ice_params->cands[rtp_session->rtcp_ice.ice_params->chosen[1]][1].priority) { | |||
1086 | ice = &rtp_session->rtcp_ice; | |||
1087 | ok = 1; | |||
1088 | } | |||
1089 | ||||
1090 | if (!zstr(username)_zstr(username)) { | |||
1091 | if (!icecmp(username, ice)) { | |||
1092 | ok = 1; | |||
1093 | } else if(!zstr(rtp_session->rtcp_ice.user_ice)_zstr(rtp_session->rtcp_ice.user_ice) && !icecmp(username, &rtp_session->rtcp_ice)) { | |||
1094 | ice = &rtp_session->rtcp_ice; | |||
1095 | ok = 1; | |||
1096 | } | |||
1097 | } | |||
1098 | ||||
1099 | if (ok) { | |||
1100 | ice->missed_count = 0; | |||
1101 | } else { | |||
1102 | switch_rtp_ice_t *icep[2] = { &rtp_session->ice, &rtp_session->rtcp_ice }; | |||
1103 | switch_port_t port = 0; | |||
1104 | char *host = NULL((void*)0); | |||
1105 | ||||
1106 | if (elapsed > 20000 && pri) { | |||
1107 | int i, j; | |||
1108 | uint32_t old; | |||
1109 | //const char *tx_host; | |||
1110 | const char *old_host, *err = NULL((void*)0); | |||
1111 | //char bufa[50]; | |||
1112 | char bufb[50]; | |||
1113 | char adj_port[6]; | |||
1114 | switch_channel_t *channel = NULL((void*)0); | |||
1115 | ||||
1116 | ||||
1117 | ice->missed_count++; | |||
1118 | //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "missed %d\n", ice->missed_count); | |||
1119 | ||||
1120 | ||||
1121 | if (rtp_session->session) { | |||
1122 | channel = switch_core_session_get_channel(rtp_session->session); | |||
1123 | } | |||
1124 | ||||
1125 | //ice->ice_params->cands[ice->ice_params->chosen][ice->proto].priority; | |||
1126 | for (j = 0; j < 2; j++) { | |||
1127 | if (!icep[j] || !icep[j]->ice_params) { | |||
1128 | continue; | |||
1129 | } | |||
1130 | for (i = 0; i < icep[j]->ice_params->cand_idx[icep[j]->proto]; i++) { | |||
1131 | if (icep[j]->ice_params && icep[j]->ice_params->cands[i][icep[j]->proto].priority == *pri) { | |||
1132 | if (j == IPR_RTP) { | |||
1133 | icep[j]->ice_params->chosen[j] = i; | |||
1134 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1134, (const char*)(rtp_session->session), SWITCH_LOG_INFO, "Change candidate index to %d\n", i); | |||
1135 | } | |||
1136 | ||||
1137 | ice = icep[j]; | |||
1138 | ok = 1; | |||
1139 | ||||
1140 | if (j != IPR_RTP) { | |||
1141 | break; | |||
1142 | } | |||
1143 | ||||
1144 | old = rtp_session->remote_port; | |||
1145 | ||||
1146 | //tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr); | |||
1147 | old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr); | |||
1148 | ||||
1149 | host = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr; | |||
1150 | port = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port; | |||
1151 | ||||
1152 | if (!host || !port) { | |||
1153 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1153, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error setting remote host!\n"); | |||
1154 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
1155 | return; | |||
1156 | } | |||
1157 | ||||
1158 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1158, (const char*)(rtp_session->session), SWITCH_LOG_INFO, | |||
1159 | "%s ICE Auto Changing port from %s:%u to %s:%u\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), old_host, old, host, port); | |||
1160 | ||||
1161 | ||||
1162 | if (channel) { | |||
1163 | 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); | |||
1164 | switch_channel_set_variable(channel, "remote_media_ip", host)switch_channel_set_variable_var_check(channel, "remote_media_ip" , host, SWITCH_TRUE); | |||
1165 | switch_channel_set_variable(channel, "rtp_auto_adjust_ip", host)switch_channel_set_variable_var_check(channel, "rtp_auto_adjust_ip" , host, SWITCH_TRUE); | |||
1166 | switch_snprintf(adj_port, sizeof(adj_port), "%u", port); | |||
1167 | 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); | |||
1168 | switch_channel_set_variable(channel, "remote_media_port", adj_port)switch_channel_set_variable_var_check(channel, "remote_media_port" , adj_port, SWITCH_TRUE); | |||
1169 | 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); | |||
1170 | switch_channel_set_variable(channel, "rtp_auto_candidate_adjust", "true")switch_channel_set_variable_var_check(channel, "rtp_auto_candidate_adjust" , "true", SWITCH_TRUE); | |||
1171 | } | |||
1172 | rtp_session->auto_adj_used = 1; | |||
1173 | ||||
1174 | ||||
1175 | switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err); | |||
1176 | if (switch_sockaddr_info_get(&ice->addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || | |||
1177 | !ice->addr) { | |||
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_ERROR, "Error setting remote host!\n"); | |||
1179 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
1180 | return; | |||
1181 | } | |||
1182 | ||||
1183 | if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) { | |||
1184 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ); | |||
1185 | } else { | |||
1186 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ); | |||
1187 | } | |||
1188 | ||||
1189 | } | |||
1190 | } | |||
1191 | } | |||
1192 | } | |||
1193 | } | |||
1194 | } | |||
1195 | ||||
1196 | if (ice->missed_count > 5 && !(ice->type & ICE_GOOGLE_JINGLE)) { | |||
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_WARNING, "missed too many: %d, looking for new ICE dest.\n", | |||
1198 | ice->missed_count); | |||
1199 | ice->rready = 0; | |||
1200 | ok = 1; | |||
1201 | } | |||
1202 | ||||
1203 | ||||
1204 | //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] || 1) { | |||
1205 | // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s %d\n", rtp_type(rtp_session), ok); | |||
1206 | //} | |||
1207 | ||||
1208 | if (ok) { | |||
1209 | const char *host = NULL((void*)0), *host2 = NULL((void*)0); | |||
1210 | switch_port_t port = 0, port2 = 0; | |||
1211 | char buf[80] = ""; | |||
1212 | char buf2[80] = ""; | |||
1213 | ||||
1214 | if (packet->header.type == SWITCH_STUN_BINDING_REQUEST) { | |||
1215 | uint8_t stunbuf[512]; | |||
1216 | switch_stun_packet_t *rpacket; | |||
1217 | const char *remote_ip; | |||
1218 | switch_size_t bytes; | |||
1219 | char ipbuf[50]; | |||
1220 | switch_sockaddr_t *from_addr = rtp_session->from_addr; | |||
1221 | switch_socket_t *sock_output = rtp_session->sock_output; | |||
1222 | uint8_t do_adj = 0; | |||
1223 | switch_time_t now = switch_micro_time_now(); | |||
1224 | int cmp = 0; | |||
1225 | int cur_idx = -1;//, is_relay = 0; | |||
1226 | int i; | |||
1227 | ||||
1228 | if (is_rtcp) { | |||
1229 | from_addr = rtp_session->rtcp_from_addr; | |||
1230 | sock_output = rtp_session->rtcp_sock_output; | |||
1231 | } | |||
1232 | ||||
1233 | if (!ice->ready) { | |||
1234 | ice->ready = 1; | |||
1235 | } | |||
1236 | ||||
1237 | memset(stunbuf, 0, sizeof(stunbuf)); | |||
1238 | rpacket = switch_stun_packet_build_header(SWITCH_STUN_BINDING_RESPONSE, packet->header.id, stunbuf); | |||
1239 | ||||
1240 | if ((ice->type & ICE_GOOGLE_JINGLE)) { | |||
1241 | switch_stun_packet_attribute_add_username(rpacket, username, (uint16_t)strlen(username)); | |||
1242 | } | |||
1243 | ||||
1244 | remote_ip = switch_get_addr(ipbuf, sizeof(ipbuf), from_addr); | |||
1245 | ||||
1246 | switch_stun_packet_attribute_add_xor_binded_address(rpacket, (char *) remote_ip, switch_sockaddr_get_port(from_addr), from_addr->family); | |||
1247 | ||||
1248 | if ((ice->type & ICE_VANILLA)) { | |||
1249 | switch_stun_packet_attribute_add_integrity(rpacket, ice->pass); | |||
1250 | switch_stun_packet_attribute_add_fingerprint(rpacket); | |||
1251 | } | |||
1252 | ||||
1253 | bytes = switch_stun_packet_length(rpacket)__bswap_16 (rpacket->header.length) + (sizeof(switch_stun_packet_header_t )); | |||
1254 | ||||
1255 | host = switch_get_addr(buf, sizeof(buf), from_addr); | |||
1256 | port = switch_sockaddr_get_port(from_addr); | |||
1257 | host2 = switch_get_addr(buf2, sizeof(buf2), ice->addr); | |||
1258 | port2 = switch_sockaddr_get_port(ice->addr); | |||
1259 | cmp = switch_cmp_addr(from_addr, ice->addr); | |||
1260 | ||||
1261 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1261, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG4, | |||
1262 | "STUN from %s:%d %s\n", host, port, cmp ? "EXPECTED" : "IGNORED"); | |||
1263 | ||||
1264 | if (cmp) { | |||
1265 | ice->last_ok = now; | |||
1266 | rtp_session->wrong_addrs = 0; | |||
1267 | } else { | |||
1268 | if (((rtp_session->dtls && rtp_session->dtls->state != DS_READY) || !ice->ready || !ice->rready) && | |||
1269 | rtp_session->wrong_addrs > 2 && rtp_session->ice_adj == 0) { | |||
1270 | do_adj++; | |||
1271 | rtp_session->ice_adj = 1; | |||
1272 | rtp_session->wrong_addrs = 0; | |||
1273 | } else if (rtp_session->wrong_addrs > 10 || elapsed >= 10000) { | |||
1274 | do_adj++; | |||
1275 | } | |||
1276 | ||||
1277 | if (!do_adj) { | |||
1278 | rtp_session->wrong_addrs++; | |||
1279 | } | |||
1280 | ||||
1281 | for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) { | |||
1282 | if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, host)) { | |||
1283 | cur_idx = i; | |||
1284 | //if (!strcasecmp(ice->ice_params->cands[i][ice->proto].cand_type, "relay")) { | |||
1285 | // is_relay = 1; | |||
1286 | //} | |||
1287 | } | |||
1288 | } | |||
1289 | ||||
1290 | ||||
1291 | if (ice->ice_params && ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type && | |||
1292 | !strcasecmp(ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type, "relay")) { | |||
1293 | do_adj++; | |||
1294 | } | |||
1295 | } | |||
1296 | ||||
1297 | if ((ice->type & ICE_VANILLA) && ice->ice_params && do_adj) { | |||
1298 | ice->missed_count = 0; | |||
1299 | ice->rready = 1; | |||
1300 | ||||
1301 | if (cur_idx > -1) { | |||
1302 | ice->ice_params->chosen[ice->proto] = cur_idx; | |||
1303 | } | |||
1304 | ||||
1305 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1305, (const char*)(rtp_session->session), SWITCH_LOG_NOTICE, | |||
1306 | "Auto Changing %s stun/%s/dtls port from %s:%u to %s:%u idx:%d\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), is_rtcp ? "rtcp" : "rtp", | |||
1307 | host2, port2, | |||
1308 | host, port, cur_idx); | |||
1309 | ||||
1310 | switch_rtp_change_ice_dest(rtp_session, ice, host, port); | |||
1311 | ice->last_ok = now; | |||
1312 | rtp_session->wrong_addrs = 0; | |||
1313 | } | |||
1314 | //if (cmp) { | |||
1315 | switch_socket_sendto(sock_output, from_addr, 0, (void *) rpacket, &bytes); | |||
1316 | //} | |||
1317 | } | |||
1318 | } else if (packet->header.type == SWITCH_STUN_BINDING_ERROR_RESPONSE) { | |||
1319 | ||||
1320 | if (rtp_session->session) { | |||
1321 | switch_core_session_message_t msg = { 0 }; | |||
1322 | msg.from = __FILE__"src/switch_rtp.c"; | |||
1323 | msg.numeric_arg = packet->header.type; | |||
1324 | msg.pointer_arg = packet; | |||
1325 | msg.message_id = SWITCH_MESSAGE_INDICATE_STUN_ERROR; | |||
1326 | 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__, 1326); | |||
1327 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1327, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, | |||
1328 | "STUN/ICE binding error received on %s channel\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
1329 | } | |||
1330 | ||||
1331 | } | |||
1332 | ||||
1333 | ||||
1334 | ||||
1335 | ||||
1336 | end: | |||
1337 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
1338 | WRITE_DEC(rtp_session)rtp_session->writing--; switch_mutex_unlock(rtp_session-> write_mutex); | |||
1339 | READ_DEC(rtp_session)rtp_session->reading--; switch_mutex_unlock(rtp_session-> read_mutex); | |||
1340 | } | |||
1341 | ||||
1342 | #ifdef ENABLE_ZRTP | |||
1343 | SWITCH_STANDARD_SCHED_FUNC(zrtp_cache_save_callback)static void zrtp_cache_save_callback (switch_scheduler_task_t *task) | |||
1344 | { | |||
1345 | zrtp_status_t status = zrtp_status_ok; | |||
1346 | ||||
1347 | status = zrtp_def_cache_store(zrtp_global); | |||
1348 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 1348, ((void*)0), SWITCH_LOG_DEBUG, "Saving ZRTP cache: %s\n", zrtp_status_ok == status ? "OK" : "FAIL"); | |||
1349 | task->runtime = switch_epoch_time_now(NULL((void*)0)) + 900; | |||
1350 | } | |||
1351 | ||||
1352 | static int zrtp_send_rtp_callback(const zrtp_stream_t *stream, char *rtp_packet, unsigned int rtp_packet_length) | |||
1353 | { | |||
1354 | switch_rtp_t *rtp_session = zrtp_stream_get_userdata(stream); | |||
1355 | switch_size_t len = rtp_packet_length; | |||
1356 | zrtp_status_t status = zrtp_status_ok; | |||
1357 | ||||
1358 | if (!rtp_session->sock_output) { | |||
1359 | return status; | |||
1360 | } | |||
1361 | ||||
1362 | switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, rtp_packet, &len); | |||
1363 | return status; | |||
1364 | } | |||
1365 | ||||
1366 | static void zrtp_event_callback(zrtp_stream_t *stream, unsigned event) | |||
1367 | { | |||
1368 | switch_rtp_t *rtp_session = zrtp_stream_get_userdata(stream); | |||
1369 | zrtp_session_info_t zrtp_session_info; | |||
1370 | ||||
1371 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
1372 | switch_event_t *fsevent = NULL((void*)0); | |||
1373 | const char *type; | |||
1374 | ||||
1375 | type = rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"); | |||
1376 | ||||
1377 | switch (event) { | |||
1378 | case ZRTP_EVENT_IS_SECURE: | |||
1379 | { | |||
1380 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_SEND] = 1; | |||
1381 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_RECV] = 1; | |||
1382 | if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
1383 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 1; | |||
1384 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 1; | |||
1385 | } | |||
1386 | if (zrtp_status_ok == zrtp_session_get(stream->session, &zrtp_session_info)) { | |||
1387 | if (zrtp_session_info.sas_is_ready) { | |||
1388 | ||||
1389 | switch_channel_set_variable_name_printf(channel, "true", "zrtp_secure_media_confirmed_%s", type); | |||
1390 | switch_channel_set_variable_name_printf(channel, stream->session->sas1.buffer, "zrtp_sas1_string_%s", type); | |||
1391 | switch_channel_set_variable_name_printf(channel, stream->session->sas2.buffer, "zrtp_sas2_string", type); | |||
1392 | zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, (uint8_t)1); | |||
1393 | } | |||
1394 | } | |||
1395 | ||||
1396 | if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
1397 | ||||
1398 | ||||
1399 | if (rtp_session->session) { | |||
1400 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
1401 | switch_rtp_t *video_rtp_session = switch_channel_get_private(channel, "__zrtp_video_rtp_session"); | |||
1402 | ||||
1403 | if (!video_rtp_session) { | |||
1404 | video_rtp_session = switch_channel_get_private_partner(channel, "__zrtp_video_rtp_session"); | |||
1405 | } | |||
1406 | ||||
1407 | if (video_rtp_session) { | |||
1408 | if (zrtp_status_ok != zrtp_stream_attach(stream->session, &video_rtp_session->zrtp_stream)) { | |||
1409 | abort(); | |||
1410 | } | |||
1411 | zrtp_stream_set_userdata(video_rtp_session->zrtp_stream, video_rtp_session); | |||
1412 | if (switch_true(switch_channel_get_variable(channel, "zrtp_enrollment")switch_channel_get_variable_dup(channel, "zrtp_enrollment", SWITCH_TRUE , -1))) { | |||
1413 | zrtp_stream_registration_start(video_rtp_session->zrtp_stream, video_rtp_session->ssrc); | |||
1414 | } else { | |||
1415 | zrtp_stream_start(video_rtp_session->zrtp_stream, video_rtp_session->ssrc); | |||
1416 | } | |||
1417 | } | |||
1418 | } | |||
1419 | } | |||
1420 | ||||
1421 | if (switch_event_create(&fsevent, SWITCH_EVENT_CALL_SECURE)switch_event_create_subclass_detailed("src/switch_rtp.c", (const char * )(const char *)__func__, 1421, &fsevent, SWITCH_EVENT_CALL_SECURE , ((void*)0)) == SWITCH_STATUS_SUCCESS) { | |||
1422 | switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_media_type", "%s", type); | |||
1423 | switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "zrtp:%s:%s", stream->session->sas1.buffer, | |||
1424 | stream->session->sas2.buffer); | |||
1425 | switch_event_add_header_string(fsevent, SWITCH_STACK_BOTTOM, "caller-unique-id", switch_channel_get_uuid(channel)); | |||
1426 | switch_event_fire(&fsevent)switch_event_fire_detailed("src/switch_rtp.c", (const char * ) (const char *)__func__, 1426, &fsevent, ((void*)0)); | |||
1427 | } | |||
1428 | } | |||
1429 | break; | |||
1430 | #if 0 | |||
1431 | case ZRTP_EVENT_NO_ZRTP_QUICK: | |||
1432 | { | |||
1433 | if (stream != NULL((void*)0)) { | |||
1434 | zrtp_stream_stop(stream); | |||
1435 | } | |||
1436 | } | |||
1437 | break; | |||
1438 | #endif | |||
1439 | case ZRTP_EVENT_IS_CLIENT_ENROLLMENT: | |||
1440 | { | |||
1441 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1441, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "Enrolled complete!\n"); | |||
1442 | switch_channel_set_variable_name_printf(channel, "true", "zrtp_enroll_complete_%s", type); | |||
1443 | } | |||
1444 | break; | |||
1445 | ||||
1446 | case ZRTP_EVENT_USER_ALREADY_ENROLLED: | |||
1447 | { | |||
1448 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1448, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "User already enrolled!\n"); | |||
1449 | switch_channel_set_variable_name_printf(channel, "true", "zrtp_already_enrolled_%s", type); | |||
1450 | } | |||
1451 | break; | |||
1452 | ||||
1453 | case ZRTP_EVENT_NEW_USER_ENROLLED: | |||
1454 | { | |||
1455 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1455, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "New user enrolled!\n"); | |||
1456 | switch_channel_set_variable_name_printf(channel, "true", "zrtp_new_user_enrolled_%s", type); | |||
1457 | } | |||
1458 | break; | |||
1459 | ||||
1460 | case ZRTP_EVENT_USER_UNENROLLED: | |||
1461 | { | |||
1462 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1462, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "User unenrolled!\n"); | |||
1463 | switch_channel_set_variable_name_printf(channel, "true", "zrtp_user_unenrolled_%s", type); | |||
1464 | } | |||
1465 | break; | |||
1466 | ||||
1467 | case ZRTP_EVENT_IS_PENDINGCLEAR: | |||
1468 | { | |||
1469 | switch_channel_set_variable_name_printf(channel, "false", "zrtp_secure_media_confirmed_%s", type); | |||
1470 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_SEND] = 0; | |||
1471 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_RECV] = 0; | |||
1472 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0; | |||
1473 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0; | |||
1474 | rtp_session->zrtp_mitm_tries = 0; | |||
1475 | } | |||
1476 | break; | |||
1477 | ||||
1478 | case ZRTP_EVENT_NO_ZRTP: | |||
1479 | { | |||
1480 | switch_channel_set_variable_name_printf(channel, "false", "zrtp_secure_media_confirmed_%s", type); | |||
1481 | } | |||
1482 | break; | |||
1483 | ||||
1484 | default: | |||
1485 | break; | |||
1486 | } | |||
1487 | } | |||
1488 | ||||
1489 | static void zrtp_logger(int level, const char *data, int len, int offset) | |||
1490 | { | |||
1491 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 1491, ((void*)0), SWITCH_LOG_DEBUG, "%s", data); | |||
1492 | } | |||
1493 | #endif | |||
1494 | ||||
1495 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_init(switch_memory_pool_t *pool) | |||
1496 | { | |||
1497 | #ifdef ENABLE_ZRTP | |||
1498 | const char *zid_string = switch_core_get_variable_pdup("switch_serial", pool); | |||
1499 | const char *zrtp_enabled = switch_core_get_variable_pdup("zrtp_enabled", pool); | |||
1500 | zrtp_config_t zrtp_config; | |||
1501 | char zrtp_cache_path[256] = ""; | |||
1502 | zrtp_on = zrtp_enabled ? switch_true(zrtp_enabled) : 0; | |||
1503 | #endif | |||
1504 | if (global_init) { | |||
1505 | return; | |||
1506 | } | |||
1507 | switch_core_hash_init(&alloc_hash)switch_core_hash_init_case(&alloc_hash, SWITCH_TRUE); | |||
1508 | #ifdef ENABLE_ZRTP | |||
1509 | if (zrtp_on) { | |||
1510 | uint32_t cache_len; | |||
1511 | zrtp_config_defaults(&zrtp_config); | |||
1512 | strcpy(zrtp_config.client_id, "FreeSWITCH"); | |||
1513 | zrtp_config.is_mitm = 1; | |||
1514 | zrtp_config.lic_mode = ZRTP_LICENSE_MODE_ACTIVE; | |||
1515 | switch_snprintf(zrtp_cache_path, sizeof(zrtp_cache_path), "%s%szrtp.dat", SWITCH_GLOBAL_dirs.db_dir, SWITCH_PATH_SEPARATOR"/"); | |||
1516 | cache_len=(uint32_t)strlen(zrtp_cache_path); | |||
1517 | ZSTR_SET_EMPTY(zrtp_config.def_cache_path); | |||
1518 | 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; | |||
1519 | strncpy(zrtp_config.def_cache_path.buffer, zrtp_cache_path, zrtp_config.def_cache_path.max_length); | |||
1520 | zrtp_config.cb.event_cb.on_zrtp_protocol_event = (void (*)(zrtp_stream_t*,zrtp_protocol_event_t))zrtp_event_callback; | |||
1521 | zrtp_config.cb.misc_cb.on_send_packet = zrtp_send_rtp_callback; | |||
1522 | zrtp_config.cb.event_cb.on_zrtp_security_event = (void (*)(zrtp_stream_t*,zrtp_security_event_t))zrtp_event_callback; | |||
1523 | zrtp_log_set_log_engine((zrtp_log_engine *) zrtp_logger); | |||
1524 | zrtp_log_set_level(4); | |||
1525 | if (zrtp_status_ok == zrtp_init(&zrtp_config, &zrtp_global)) { | |||
1526 | memcpy(zid, zid_string, 12); | |||
1527 | switch_scheduler_add_task(switch_epoch_time_now(NULL((void*)0)) + 900, zrtp_cache_save_callback, "zrtp_cache_save", "core", 0, NULL((void*)0), | |||
1528 | SSHF_NONE | SSHF_NO_DEL); | |||
1529 | } else { | |||
1530 | switch_core_set_variable("zrtp_enabled", NULL((void*)0)); | |||
1531 | zrtp_on = 0; | |||
1532 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 1532, ((void*)0), SWITCH_LOG_CRIT, "ZRTP init failed!\n"); | |||
1533 | } | |||
1534 | } | |||
1535 | #endif | |||
1536 | #ifdef ENABLE_SRTP1 | |||
1537 | { | |||
1538 | srtp_err_status_t stat = srtp_init(); | |||
1539 | if (stat == srtp_err_status_ok) { | |||
1540 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 1540, ((void*)0), SWITCH_LOG_DEBUG, "SRTP (%s) initialized.\n", srtp_get_version_string()); | |||
1541 | } else { | |||
1542 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 1542, ((void*)0), SWITCH_LOG_ERROR, "Error initializing SRTP (%d).\n", stat); | |||
1543 | } | |||
1544 | } | |||
1545 | #endif | |||
1546 | switch_mutex_init(&port_lock, SWITCH_MUTEX_NESTED0x1, pool); | |||
1547 | switch_rtp_dtls_init(); | |||
1548 | global_init = 1; | |||
1549 | } | |||
1550 | ||||
1551 | static uint8_t get_next_write_ts(switch_rtp_t *rtp_session, uint32_t timestamp) | |||
1552 | { | |||
1553 | uint8_t m = 0, changed = 0; | |||
1554 | ||||
1555 | if (!(rtp_session->rtp_bugs & RTP_BUG_SEND_LINEAR_TIMESTAMPS)) { | |||
1556 | if (timestamp) { | |||
1557 | rtp_session->ts = (uint32_t) timestamp; | |||
1558 | changed++; | |||
1559 | } else if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) { | |||
1560 | switch_core_timer_next(&rtp_session->write_timer); | |||
1561 | rtp_session->ts = rtp_session->write_timer.samplecount; | |||
1562 | changed++; | |||
1563 | } | |||
1564 | } | |||
1565 | ||||
1566 | if (!changed) { | |||
1567 | rtp_session->ts = rtp_session->last_write_ts + rtp_session->samples_per_interval; | |||
1568 | } else { | |||
1569 | /* Send marker bit if timestamp is lower/same as before (resetted/new timer) */ | |||
1570 | if (abs((int32_t)(rtp_session->ts - rtp_session->last_write_ts)) > rtp_session->samples_per_interval | |||
1571 | && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) { | |||
1572 | m++; | |||
1573 | } | |||
1574 | } | |||
1575 | ||||
1576 | return m; | |||
1577 | } | |||
1578 | ||||
1579 | static void do_mos(switch_rtp_t *rtp_session) { | |||
1580 | int R; | |||
1581 | ||||
1582 | if ((switch_size_t)rtp_session->stats.inbound.recved < rtp_session->stats.inbound.flaws) { | |||
1583 | rtp_session->stats.inbound.flaws = 0; | |||
1584 | } | |||
1585 | ||||
1586 | if (rtp_session->stats.inbound.recved > 0 && | |||
1587 | rtp_session->stats.inbound.flaws && (rtp_session->stats.inbound.last_flaw != rtp_session->stats.inbound.flaws)) { | |||
1588 | ||||
1589 | if (rtp_session->consecutive_flaws++) { | |||
1590 | int penalty = rtp_session->consecutive_flaws; | |||
1591 | ||||
1592 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1592, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, "%s %s %d consecutive flaws, adding %d flaw penalty\n", | |||
1593 | 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_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), | |||
1594 | rtp_session->consecutive_flaws, penalty); | |||
1595 | rtp_session->bad_stream++; | |||
1596 | rtp_session->stats.inbound.flaws += penalty; | |||
1597 | rtp_session->stats.inbound.last_flaw = rtp_session->stats.inbound.flaws; | |||
1598 | ||||
1599 | if (rtp_session->stats.inbound.error_log) { | |||
1600 | rtp_session->stats.inbound.error_log->flaws += penalty; | |||
1601 | rtp_session->stats.inbound.error_log->consecutive_flaws++; | |||
1602 | } | |||
1603 | } | |||
1604 | } else { | |||
1605 | rtp_session->consecutive_flaws = 0; | |||
1606 | } | |||
1607 | ||||
1608 | R = (int)((double)((double)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws) / (double)rtp_session->stats.inbound.recved) * 100.0); | |||
1609 | ||||
1610 | if (R < 0 || R > 100) R = 100; | |||
1611 | ||||
1612 | rtp_session->stats.inbound.R = R; | |||
1613 | rtp_session->stats.inbound.mos = 1 + (0.035) * R + (.000007) * R * (R-60) * (100-R); | |||
1614 | ||||
1615 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1615, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, "%s %s stat %0.2f %ld/%d flaws: %ld mos: %0.2f v: %0.2f %0.2f/%0.2f\n", | |||
1616 | rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel (rtp_session->session)) : "-", | |||
1617 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), | |||
1618 | rtp_session->stats.inbound.R, | |||
1619 | (long int)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws), rtp_session->stats.inbound.recved, | |||
1620 | (long int)rtp_session->stats.inbound.flaws, | |||
1621 | rtp_session->stats.inbound.mos, | |||
1622 | rtp_session->stats.inbound.variance, | |||
1623 | rtp_session->stats.inbound.min_variance, | |||
1624 | rtp_session->stats.inbound.max_variance | |||
1625 | ); | |||
1626 | ||||
1627 | } | |||
1628 | ||||
1629 | void burstr_calculate ( int loss[], int received, double *burstr, double *lossr ) | |||
1630 | { | |||
1631 | int lost = 0; | |||
1632 | int bursts = 0; | |||
1633 | int i; | |||
1634 | ||||
1635 | for ( i = 0; i < LOST_BURST_ANALYZE500; i++ ) { | |||
1636 | lost += i * loss[i]; | |||
1637 | bursts += loss[i]; | |||
1638 | } | |||
1639 | if (received > 0 && bursts > 0) { | |||
1640 | *burstr = (double)((double)lost / (double)bursts) / (double)(1.0 / ( 1.0 - (double)lost / (double)received )); | |||
1641 | if (*burstr < 0) { | |||
1642 | *burstr = - *burstr; | |||
1643 | } | |||
1644 | } else { | |||
1645 | *burstr = 0; | |||
1646 | } | |||
1647 | if (received > 0) { | |||
1648 | *lossr = (double)((double)lost / (double)received); | |||
1649 | } else { | |||
1650 | *lossr = 0; | |||
1651 | } | |||
1652 | } | |||
1653 | ||||
1654 | static void reset_jitter_seq(switch_rtp_t *rtp_session) | |||
1655 | { | |||
1656 | rtp_session->stats.inbound.last_proc_time = 0; | |||
1657 | rtp_session->stats.inbound.last_processed_seq = 0; | |||
1658 | rtp_session->jitter_lead = 0; | |||
1659 | rtp_session->consecutive_flaws = 0; | |||
1660 | rtp_session->stats.inbound.last_flaw = 0; | |||
1661 | } | |||
1662 | ||||
1663 | static void check_jitter(switch_rtp_t *rtp_session) | |||
1664 | { | |||
1665 | switch_time_t current_time; | |||
1666 | int64_t diff_time = 0, cur_diff = 0; | |||
1667 | int seq; | |||
1668 | ||||
1669 | current_time = switch_micro_time_now() / 1000; | |||
1670 | ||||
1671 | if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] || rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] || rtp_session->dtmf_data.in_digit_ts) { | |||
1672 | reset_jitter_seq(rtp_session); | |||
1673 | return; | |||
1674 | } | |||
1675 | ||||
1676 | if (++rtp_session->jitter_lead < JITTER_LEAD_FRAMES10 || !rtp_session->stats.inbound.last_proc_time) { | |||
1677 | rtp_session->stats.inbound.last_proc_time = current_time; | |||
1678 | return; | |||
1679 | } | |||
1680 | ||||
1681 | diff_time = (current_time - rtp_session->stats.inbound.last_proc_time); | |||
1682 | seq = (int)(uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq)__bswap_16 ((uint16_t) rtp_session->last_rtp_hdr.seq); | |||
1683 | ||||
1684 | /* Burst and Packet Loss */ | |||
1685 | rtp_session->stats.inbound.recved++; | |||
1686 | ||||
1687 | if (rtp_session->stats.inbound.last_processed_seq > 0 && seq > (int)(rtp_session->stats.inbound.last_processed_seq + 1)) { | |||
1688 | int lost = (seq - rtp_session->stats.inbound.last_processed_seq - 1); | |||
1689 | ||||
1690 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1690, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, "%s Got: %s seq %d but expected: %d lost: %d\n", | |||
1691 | rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel (rtp_session->session)) : "-", | |||
1692 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), | |||
1693 | seq, | |||
1694 | (rtp_session->stats.inbound.last_processed_seq + 1), lost); | |||
1695 | rtp_session->stats.inbound.last_loss++; | |||
1696 | ||||
1697 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
1698 | switch_core_session_request_video_refresh(rtp_session->session)_switch_core_session_request_video_refresh(rtp_session->session , 0, "src/switch_rtp.c", (const char *)__func__, 1698); | |||
1699 | } | |||
1700 | ||||
1701 | if (rtp_session->stats.inbound.last_loss > 0 && rtp_session->stats.inbound.last_loss < LOST_BURST_CAPTURE1024) { | |||
1702 | rtp_session->stats.inbound.loss[rtp_session->stats.inbound.last_loss] += lost; | |||
1703 | } | |||
1704 | ||||
1705 | rtp_session->bad_stream++; | |||
1706 | rtp_session->stats.inbound.flaws += lost; | |||
1707 | ||||
1708 | if (rtp_session->stats.inbound.error_log) { | |||
1709 | rtp_session->stats.inbound.error_log->flaws += lost; | |||
1710 | } | |||
1711 | ||||
1712 | } else { | |||
1713 | rtp_session->stats.inbound.last_loss = 0; | |||
1714 | } | |||
1715 | ||||
1716 | rtp_session->stats.inbound.last_processed_seq = seq; | |||
1717 | ||||
1718 | /* Burst and Packet Loss */ | |||
1719 | ||||
1720 | if (current_time > rtp_session->next_stat_check_time) { | |||
1721 | rtp_session->next_stat_check_time = current_time + 5000; | |||
1722 | burstr_calculate(rtp_session->stats.inbound.loss, rtp_session->stats.inbound.recved, | |||
1723 | &(rtp_session->stats.inbound.burstrate), &(rtp_session->stats.inbound.lossrate)); | |||
1724 | do_mos(rtp_session); | |||
1725 | } else { | |||
1726 | do_mos(rtp_session); | |||
1727 | } | |||
1728 | ||||
1729 | if (rtp_session->stats.inbound.last_loss || rtp_session->bad_stream) { | |||
1730 | if (rtp_session->session && (!rtp_session->stats.inbound.error_log || rtp_session->stats.inbound.error_log->stop)) { | |||
1731 | struct error_period *error = switch_core_session_alloc(rtp_session->session, sizeof(*error))switch_core_perform_session_alloc(rtp_session->session, sizeof (*error), "src/switch_rtp.c", (const char *)__func__, 1731); | |||
1732 | error->start = switch_micro_time_now(); | |||
1733 | error->next = rtp_session->stats.inbound.error_log; | |||
1734 | rtp_session->stats.inbound.error_log = error; | |||
1735 | } | |||
1736 | ||||
1737 | if (!rtp_session->stats.inbound.last_loss) { | |||
1738 | if (++rtp_session->recovering_stream > (rtp_session->one_second * 3)) { | |||
1739 | if (rtp_session->session && rtp_session->stats.inbound.error_log) { | |||
1740 | rtp_session->stats.inbound.error_log->stop = switch_micro_time_now(); | |||
1741 | } | |||
1742 | ||||
1743 | rtp_session->bad_stream = 0; | |||
1744 | } | |||
1745 | } else { | |||
1746 | rtp_session->recovering_stream = 0; | |||
1747 | rtp_session->bad_stream++; | |||
1748 | } | |||
1749 | } else { | |||
1750 | rtp_session->recovering_stream = 0; | |||
1751 | rtp_session->clean_stream++; | |||
1752 | } | |||
1753 | ||||
1754 | ||||
1755 | if ( diff_time < 0 ) { | |||
1756 | diff_time = -diff_time; | |||
1757 | } | |||
1758 | ||||
1759 | rtp_session->stats.inbound.jitter_n++; | |||
1760 | rtp_session->stats.inbound.jitter_add += diff_time; | |||
1761 | ||||
1762 | if (rtp_session->stats.inbound.mean_interval) { | |||
1763 | cur_diff = (int64_t)(diff_time - rtp_session->stats.inbound.mean_interval); | |||
1764 | } else { | |||
1765 | cur_diff = 0; | |||
1766 | } | |||
1767 | ||||
1768 | rtp_session->stats.inbound.jitter_addsq += (cur_diff * cur_diff); | |||
1769 | rtp_session->stats.inbound.last_proc_time = current_time; | |||
1770 | ||||
1771 | if (rtp_session->stats.inbound.jitter_n > 0) { | |||
1772 | double ipdv; | |||
1773 | ||||
1774 | rtp_session->stats.inbound.mean_interval = (double)rtp_session->stats.inbound.jitter_add / (double)rtp_session->stats.inbound.jitter_n; | |||
1775 | ||||
1776 | if (!rtp_session->old_mean) { | |||
1777 | rtp_session->old_mean = rtp_session->stats.inbound.mean_interval; | |||
1778 | } | |||
1779 | ||||
1780 | rtp_session->stats.inbound.variance = (double)rtp_session->stats.inbound.jitter_addsq / (double)rtp_session->stats.inbound.jitter_n; | |||
1781 | ||||
1782 | //printf("CHECK %d +%ld +%ld %f %f\n", rtp_session->write_timer.samplecount, diff_time, (diff_time * diff_time), rtp_session->stats.inbound.mean_interval, rtp_session->stats.inbound.variance); | |||
1783 | ||||
1784 | ipdv = rtp_session->old_mean - rtp_session->stats.inbound.mean_interval; | |||
1785 | ||||
1786 | if ( ipdv > IPDV_THRESHOLD1.0 ) { /* It shows Increasing Delays */ | |||
1787 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 1787, ((void*)0), SWITCH_LOG_DEBUG3, "Calculated Instantaneous Packet Delay Variation: %s packet %lf\n", | |||
1788 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), ipdv); | |||
1789 | } | |||
1790 | ||||
1791 | if ( rtp_session->stats.inbound.variance < rtp_session->stats.inbound.min_variance || rtp_session->stats.inbound.min_variance == 0 ) { | |||
1792 | rtp_session->stats.inbound.min_variance = rtp_session->stats.inbound.variance; | |||
1793 | } | |||
1794 | ||||
1795 | if ( rtp_session->stats.inbound.variance > rtp_session->stats.inbound.max_variance ) { | |||
1796 | rtp_session->stats.inbound.max_variance = rtp_session->stats.inbound.variance; | |||
1797 | } | |||
1798 | ||||
1799 | rtp_session->old_mean = rtp_session->stats.inbound.mean_interval; | |||
1800 | } | |||
1801 | } | |||
1802 | ||||
1803 | static void rtcp_generate_sender_info(switch_rtp_t *rtp_session, struct switch_rtcp_sender_info *sr){ | |||
1804 | switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session"); | |||
1805 | switch_time_t now; | |||
1806 | uint32_t sec, ntp_sec, ntp_usec; | |||
1807 | switch_time_exp_t now_hr; | |||
1808 | now = switch_micro_time_now(); | |||
1809 | sec = (uint32_t)(now/1000000); /* convert to seconds */ | |||
1810 | ntp_sec = sec+NTP_TIME_OFFSET2208988800UL; /* convert to NTP seconds */ | |||
1811 | sr->ntp_msw = htonl(ntp_sec)__bswap_32 (ntp_sec); /* store result in "most significant word" */ | |||
1812 | ntp_usec = (uint32_t)(now - (sec*1000000)); /* remove seconds to keep only the microseconds */ | |||
1813 | sr->ntp_lsw = htonl((u_long)(ntp_usec*(double)(((uint64_t)1)<<32)*1.0e-6))__bswap_32 ((u_long)(ntp_usec*(double)(((uint64_t)1)<<32 )*1.0e-6)); /* convert microseconds to fraction of 32bits and store result in "least significatn word" */ | |||
1814 | ||||
1815 | sr->ts = htonl(rtp_session->last_write_ts)__bswap_32 (rtp_session->last_write_ts); | |||
1816 | sr->pc = htonl(rtp_session->stats.outbound.packet_count)__bswap_32 (rtp_session->stats.outbound.packet_count); | |||
1817 | sr->oc = htonl(rtp_session->stats.outbound.raw_bytes - rtp_session->stats.outbound.packet_count * sizeof(srtp_hdr_t))__bswap_32 (rtp_session->stats.outbound.raw_bytes - rtp_session ->stats.outbound.packet_count * sizeof(srtp_hdr_t)); | |||
1818 | ||||
1819 | switch_time_exp_gmt(&now_hr,now); | |||
1820 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1820, (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] packet_count[%u] OC[%u]\n", | |||
1821 | 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, | |||
1822 | (ntohl(sr->ntp_lsw)__bswap_32 (sr->ntp_lsw)&0xffff0000)>>16 | (ntohl(sr->ntp_msw)__bswap_32 (sr->ntp_msw)&0x0000ffff)<<16, | |||
1823 | ntohl(sr->ntp_msw)__bswap_32 (sr->ntp_msw),ntohl(sr->ntp_lsw)__bswap_32 (sr->ntp_lsw), rtp_session->stats.rtcp.ssrc, ntohl(sr->pc)__bswap_32 (sr->pc), ntohl(sr->oc)__bswap_32 (sr->oc) | |||
1824 | ); | |||
1825 | } | |||
1826 | ||||
1827 | static inlineinline uint32_t calc_local_lsr_now() | |||
1828 | { | |||
1829 | switch_time_t now; | |||
1830 | uint32_t ntp_sec, ntp_usec, lsr_now, sec; | |||
1831 | now = switch_micro_time_now(); | |||
1832 | sec = (uint32_t)(now/1000000); /* convert to seconds */ | |||
1833 | ntp_sec = sec+NTP_TIME_OFFSET2208988800UL; /* convert to NTP seconds */ | |||
1834 | ntp_usec = (uint32_t)(now - ((switch_time_t) sec*1000000)); /* remove seconds to keep only the microseconds */ | |||
1835 | ||||
1836 | 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) */ | |||
1837 | ||||
1838 | return lsr_now; | |||
1839 | } | |||
1840 | ||||
1841 | //#define DEBUG_RTCP | |||
1842 | /* extra param is for duplicates (received NACKed packets) */ | |||
1843 | static void rtcp_generate_report_block(switch_rtp_t *rtp_session, struct switch_rtcp_report_block *rtcp_report_block, | |||
1844 | int16_t extra_expected) | |||
1845 | { | |||
1846 | #ifdef DEBUG_RTCP | |||
1847 | switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session"); | |||
1848 | #endif | |||
1849 | switch_rtcp_numbers_t * stats=&rtp_session->stats.rtcp; | |||
1850 | uint32_t expected_pkt, dlsr = 0; | |||
1851 | int32_t pkt_lost; | |||
1852 | ||||
1853 | /* Packet loss */ | |||
1854 | if (stats->rtcp_rtp_count == 0) { | |||
1855 | expected_pkt = stats->high_ext_seq_recv - stats->base_seq + 1; | |||
1856 | } else { | |||
1857 | expected_pkt = stats->high_ext_seq_recv - stats->last_rpt_ext_seq + extra_expected; | |||
1858 | } | |||
1859 | ||||
1860 | pkt_lost = expected_pkt - stats->period_pkt_count; | |||
1861 | if (pkt_lost < 0) pkt_lost = 0; | |||
1862 | ||||
1863 | stats->cum_lost=stats->cum_lost+pkt_lost; | |||
1864 | if (expected_pkt > 0 && pkt_lost > 0) { | |||
1865 | rtcp_report_block->fraction = (pkt_lost == expected_pkt ? 255 : (uint8_t) (pkt_lost * 256 / expected_pkt)); /* if X packets were expected and X was lost, we want 0xff to be reported, not 0 */ | |||
1866 | } else { | |||
1867 | rtcp_report_block->fraction = 0; | |||
1868 | } | |||
1869 | #if SWITCH_BYTE_ORDER1234 == __BIG_ENDIAN4321 | |||
1870 | rtcp_report_block->lost = stats->cum_lost; | |||
1871 | #else | |||
1872 | /* Reversing byte order for 24bits */ | |||
1873 | rtcp_report_block->lost = htonl(stats->cum_lost)__bswap_32 (stats->cum_lost) >> 8; | |||
1874 | #endif | |||
1875 | ||||
1876 | #ifdef DEBUG_RTCP | |||
1877 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) | |||
1878 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1878, (const char*)(session), SWITCH_LOG_CRIT, "rtcp_generate_sr: stats_ssrc[%u]\nreceived[%d]\nexpected[%d]\ncum[%d]\nlost[%d|%d/256]pkt\nlast_seq[%d]\ncyc[%d]\nlast_rpt_seq[%d]\ncyc[%d]\nssrc[%d]\n", | |||
1879 | rtp_session->remote_ssrc, stats->period_pkt_count, expected_pkt, | |||
1880 | stats->cum_lost, pkt_lost, rtcp_report_block->fraction, stats->high_ext_seq_recv&0x0000ffff, | |||
1881 | stats->cycle, stats->last_rpt_ext_seq&0x0000ffff, stats->last_rpt_cycle, rtp_session->stats.rtcp.peer_ssrc | |||
1882 | ); | |||
1883 | #endif | |||
1884 | rtcp_report_block->highest_sequence_number_received = htonl(stats->high_ext_seq_recv)__bswap_32 (stats->high_ext_seq_recv); | |||
1885 | ||||
1886 | /* Jitter */ | |||
1887 | rtcp_report_block->jitter = htonl((uint32_t)stats->inter_jitter)__bswap_32 ((uint32_t)stats->inter_jitter); | |||
1888 | ||||
1889 | /* Delay since Last Sender Report (DLSR) : 32bits, 1/65536 seconds */ | |||
1890 | if (stats->last_recv_lsr_local) { | |||
1891 | uint32_t lsr_now = calc_local_lsr_now(); | |||
1892 | /* check lsr_now: what we just read from clock may be in the past (race cond), don't send huge dlsr due to uint wrap around */ | |||
1893 | if (lsr_now > stats->last_recv_lsr_local) { | |||
1894 | dlsr = lsr_now - stats->last_recv_lsr_local; | |||
1895 | } | |||
1896 | } | |||
1897 | rtcp_report_block->lsr = stats->last_recv_lsr_peer; | |||
1898 | rtcp_report_block->dlsr = htonl(dlsr)__bswap_32 (dlsr); | |||
1899 | if (rtp_session->stats.rtcp.peer_ssrc) { | |||
1900 | rtcp_report_block->ssrc = htonl(rtp_session->stats.rtcp.peer_ssrc)__bswap_32 (rtp_session->stats.rtcp.peer_ssrc); | |||
1901 | } else { | |||
1902 | /* if remote is not sending rtcp reports, take ssrc as assigned from rtp */ | |||
1903 | rtcp_report_block->ssrc = htonl(rtp_session->remote_ssrc)__bswap_32 (rtp_session->remote_ssrc); | |||
1904 | } | |||
1905 | ||||
1906 | stats->rtcp_rtp_count++; | |||
1907 | } | |||
1908 | ||||
1909 | static void rtcp_stats_init(switch_rtp_t *rtp_session) | |||
1910 | { | |||
1911 | switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp; | |||
1912 | srtp_hdr_t * hdr = &rtp_session->last_rtp_hdr; | |||
1913 | switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session"); | |||
1914 | stats->ssrc = ntohl(hdr->ssrc)__bswap_32 (hdr->ssrc); | |||
1915 | stats->last_rpt_ts = rtp_session->write_timer.samplecount; | |||
1916 | stats->init = 1; | |||
1917 | stats->last_rpt_ext_seq = 0; | |||
1918 | stats->last_rpt_cycle = 0; | |||
1919 | stats->last_pkt_tsdiff = 0; | |||
1920 | stats->inter_jitter = 0; | |||
1921 | stats->cycle = 0; | |||
1922 | stats->high_ext_seq_recv = ntohs((uint16_t)hdr->seq)__bswap_16 ((uint16_t)hdr->seq); | |||
1923 | stats->base_seq = ntohs((uint16_t)hdr->seq)__bswap_16 ((uint16_t)hdr->seq); | |||
1924 | stats->bad_seq = (1<<16) + 1; /* Make sure we wont missmatch 2 consecutive packets, so seq == bad_seq is false */ | |||
1925 | stats->cum_lost = 0; | |||
1926 | stats->period_pkt_count = 0; | |||
1927 | stats->sent_pkt_count = 0; | |||
1928 | stats->pkt_count = 0; | |||
1929 | stats->rtcp_rtp_count = 0; | |||
1930 | ||||
1931 | if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
1932 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1932, (const char*)(session), SWITCH_LOG_DEBUG, "rtcp_stats_init: %s rtcp disabled\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
1933 | } else if (!rtp_session->rtcp_sock_output) { | |||
1934 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1934, (const char*)(session), SWITCH_LOG_WARNING, "rtcp_stats_init: %s no rtcp socket\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
1935 | } else if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) { | |||
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: %s rtcp passthru\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
1937 | } else { | |||
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_DEBUG, "rtcp_stats_init: %s ssrc[%u] base_seq[%u]\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), stats->ssrc, stats->base_seq); | |||
1939 | } | |||
1940 | ||||
1941 | if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && (switch_core_media_codec_get_cap(rtp_session->session, | |||
1942 | SWITCH_MEDIA_TYPE_AUDIO, SWITCH_CODEC_FLAG_HAS_ADJ_BITRATE))) { | |||
1943 | kalman_estimator_t *estimators[KALMAN_SYSTEM_MODELS3]; | |||
1944 | cusum_kalman_detector_t *detectors[KALMAN_SYSTEM_MODELS3]; | |||
1945 | ||||
1946 | rtp_session->flags[SWITCH_RTP_FLAG_ADJ_BITRATE_CAP] = 1; | |||
1947 | rtp_session->flags[SWITCH_RTP_FLAG_ESTIMATORS] = 1; | |||
1948 | ||||
1949 | rtp_session->estimators[EST_LOSS0] = switch_core_alloc(rtp_session->pool, sizeof(*estimators[0]))switch_core_perform_alloc(rtp_session->pool, sizeof(*estimators [0]), "src/switch_rtp.c", (const char *)__func__, 1949); | |||
1950 | switch_kalman_init(rtp_session->estimators[EST_LOSS0],0.1,0.1); | |||
1951 | rtp_session->estimators[EST_RTT2] = switch_core_alloc(rtp_session->pool, sizeof(*estimators[0]))switch_core_perform_alloc(rtp_session->pool, sizeof(*estimators [0]), "src/switch_rtp.c", (const char *)__func__, 1951); | |||
1952 | switch_kalman_init(rtp_session->estimators[EST_RTT2],0.03,1); | |||
1953 | rtp_session->detectors[EST_RTT2] = switch_core_alloc(rtp_session->pool, sizeof(*detectors[0]))switch_core_perform_alloc(rtp_session->pool, sizeof(*detectors [0]), "src/switch_rtp.c", (const char *)__func__, 1953); | |||
1954 | switch_kalman_cusum_init(rtp_session->detectors[EST_RTT2],0.005,0.5); | |||
1955 | rtp_session->detectors[EST_LOSS0] = switch_core_alloc(rtp_session->pool, sizeof(*detectors[0]))switch_core_perform_alloc(rtp_session->pool, sizeof(*detectors [0]), "src/switch_rtp.c", (const char *)__func__, 1955); | |||
1956 | switch_kalman_cusum_init(rtp_session->detectors[EST_LOSS0],0.005,0.5); | |||
1957 | } | |||
1958 | } | |||
1959 | ||||
1960 | static int rtcp_stats(switch_rtp_t *rtp_session) | |||
1961 | { | |||
1962 | switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session"); | |||
1963 | srtp_hdr_t * hdr = &rtp_session->last_rtp_hdr; | |||
1964 | switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp; | |||
1965 | uint32_t packet_spacing_diff = 0, pkt_tsdiff, pkt_extended_seq; | |||
1966 | uint16_t pkt_seq, seq_diff, max_seq; | |||
1967 | const int MAX_DROPOUT = 3000; | |||
1968 | const int MAX_MISORDER = 100; | |||
1969 | const int RTP_SEQ_MOD = (1<<16); | |||
1970 | ||||
1971 | 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) | |||
1972 | return 0; /* do not process RTCP in current state */ | |||
1973 | ||||
1974 | pkt_seq = (uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq)__bswap_16 ((uint16_t) rtp_session->last_rtp_hdr.seq); | |||
1975 | ||||
1976 | /* Detect sequence number cycle change */ | |||
1977 | max_seq = stats->high_ext_seq_recv&0x0000ffff; | |||
1978 | seq_diff = pkt_seq - max_seq; | |||
1979 | ||||
1980 | if (seq_diff < MAX_DROPOUT) { /* in order, with permissible gap */ | |||
1981 | if (pkt_seq < max_seq) { | |||
1982 | stats->cycle++; | |||
1983 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 1983, (const char*)(session), SWITCH_LOG_DEBUG, "rtcp_stats:[cycle change] pkt_seq[%d] cycle[%d] max_seq[%d] stats_ssrc[%u] local_ts[%u]\n", | |||
1984 | pkt_seq, stats->cycle, max_seq, stats->ssrc, rtp_session->timer.samplecount); | |||
1985 | } | |||
1986 | pkt_extended_seq = stats->cycle << 16 | pkt_seq; /* getting the extended packet extended sequence ID */ | |||
1987 | if (pkt_extended_seq > stats->high_ext_seq_recv) { | |||
1988 | stats->high_ext_seq_recv = pkt_extended_seq; | |||
1989 | } | |||
1990 | } | |||
1991 | else if (seq_diff <= (RTP_SEQ_MOD - MAX_MISORDER)) { /* the sequence number made a very large jump */ | |||
1992 | if (pkt_seq == stats->bad_seq) { | |||
1993 | rtcp_stats_init(rtp_session); | |||
1994 | } else { | |||
1995 | stats->bad_seq = (pkt_seq + 1) & (RTP_SEQ_MOD-1); | |||
1996 | } | |||
1997 | return 0; /* no stats, packet is out of sync and will be accounted as lost */ | |||
1998 | } else { | |||
1999 | /* duplicate or reordered packet */ | |||
2000 | } | |||
2001 | ||||
2002 | /* Verify that we are on the same stream source (we do not support multiple sources) */ | |||
2003 | if (ntohl(hdr->ssrc)__bswap_32 (hdr->ssrc) != stats->ssrc || !stats->init) { | |||
2004 | rtcp_stats_init(rtp_session); | |||
2005 | } | |||
2006 | ||||
2007 | stats->period_pkt_count++; | |||
2008 | stats->pkt_count++; | |||
2009 | #ifdef DEBUG_RTCP | |||
2010 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2010, (const char*)(session), SWITCH_LOG_DEBUG10, "rtcp_stats: period_pkt_count[%d]last_seq[%d]cycle[%d]stats_ssrc[%u]local_ts[%u]\n", | |||
2011 | stats->period_pkt_count, pkt_seq, stats->cycle, stats->ssrc, rtp_session->write_timer.samplecount); | |||
2012 | #endif | |||
2013 | /* Interarrival jitter calculation */ | |||
2014 | pkt_tsdiff = abs((int32_t)(rtp_session->timer.samplecount - ntohl(hdr->ts)__bswap_32 (hdr->ts))); /* relative transit times for this packet */ | |||
2015 | if (stats->pkt_count < 2) { /* Can not compute Jitter with only one packet */ | |||
2016 | stats->last_pkt_tsdiff = pkt_tsdiff; | |||
2017 | } else { | |||
2018 | /* Jitter : difference of relative transit times for the two packets */ | |||
2019 | packet_spacing_diff = abs((int32_t)(pkt_tsdiff - stats->last_pkt_tsdiff)); | |||
2020 | stats->last_pkt_tsdiff = pkt_tsdiff; | |||
2021 | /* Interarrival jitter estimation, "J(i) = J(i-1) + ( |D(i-1,i)| - J(i-1) )/16" */ | |||
2022 | stats->inter_jitter = (stats->inter_jitter + (((double)packet_spacing_diff - stats->inter_jitter) /16.)); | |||
2023 | } | |||
2024 | ||||
2025 | #ifdef DEBUG_RTCP | |||
2026 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2026, (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]", | |||
2027 | ntohl(hdr->ts)__bswap_32 (hdr->ts), rtp_session->timer.samplecount, pkt_tsdiff, packet_spacing_diff, stats->inter_jitter, ntohs(hdr->seq)__bswap_16 (hdr->seq), stats->ssrc); | |||
2028 | #endif | |||
2029 | return 1; | |||
2030 | } | |||
2031 | ||||
2032 | static void calc_bw_exp(uint32_t bps, uint8_t bits, rtcp_tmmbx_t *tmmbx) | |||
2033 | { | |||
2034 | uint32_t mantissa_max, i = 0; | |||
2035 | uint8_t exp = 0; | |||
2036 | uint32_t mantissa = 0; | |||
2037 | uint16_t overhead = 60; | |||
2038 | ||||
2039 | switch_assert(bits<=32)((bits<=32) ? (void) (0) : __assert_fail ("bits<=32", "src/switch_rtp.c" , 2039, __extension__ __PRETTY_FUNCTION__)); | |||
2040 | ||||
2041 | mantissa_max = (1 << bits) - 1; | |||
2042 | ||||
2043 | for (i = 0; i < 32; ++i) { | |||
2044 | if (bps <= (mantissa_max << i)) { | |||
2045 | exp = i; | |||
2046 | break; | |||
2047 | } | |||
2048 | } | |||
2049 | ||||
2050 | mantissa = (bps >> exp); | |||
2051 | ||||
2052 | tmmbx->parts[0] = (uint8_t) ((exp << 2) + ((mantissa >> 15) & 0x03)); | |||
2053 | tmmbx->parts[1] = (uint8_t) (mantissa >> 7); | |||
2054 | tmmbx->parts[2] = (uint8_t) ((mantissa >> 1) + ((overhead >> 8) & 0x01)); | |||
2055 | tmmbx->parts[3] = (uint8_t) (overhead); | |||
2056 | } | |||
2057 | ||||
2058 | static int using_ice(switch_rtp_t *rtp_session) | |||
2059 | { | |||
2060 | if (rtp_session->ice.ice_user || rtp_session->rtcp_ice.ice_user) { | |||
2061 | return 1; | |||
2062 | } | |||
2063 | ||||
2064 | return 0; | |||
2065 | } | |||
2066 | ||||
2067 | static void switch_send_rtcp_event(switch_rtp_t *rtp_session ,struct switch_rtcp_sender_report *sr,struct switch_rtcp_report_block *rtcp_report_block) | |||
2068 | { | |||
2069 | if (sr && rtcp_report_block) { | |||
2070 | switch_event_t *event; | |||
2071 | ||||
2072 | if (switch_event_create(&event, SWITCH_EVENT_SEND_RTCP_MESSAGE)switch_event_create_subclass_detailed("src/switch_rtp.c", (const char * )(const char *)__func__, 2072, &event, SWITCH_EVENT_SEND_RTCP_MESSAGE , ((void*)0)) == SWITCH_STATUS_SUCCESS) { | |||
2073 | char value[30]; | |||
2074 | char header[50]; | |||
2075 | uint32_t tmpLost; | |||
2076 | char *uuid = switch_core_session_get_uuid(rtp_session->session); | |||
2077 | if (uuid) { | |||
2078 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Unique-ID", switch_core_session_get_uuid(rtp_session->session)); | |||
2079 | } | |||
2080 | ||||
2081 | snprintf(value, sizeof(value), "%.8x", rtp_session->stats.rtcp.ssrc); | |||
2082 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "SSRC", value); | |||
2083 | ||||
2084 | snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ntp_msw)__bswap_32 (sr->sender_info.ntp_msw)); | |||
2085 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "NTP-Most-Significant-Word", value); | |||
2086 | ||||
2087 | snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ntp_lsw)__bswap_32 (sr->sender_info.ntp_lsw)); | |||
2088 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "NTP-Least-Significant-Word", value); | |||
2089 | ||||
2090 | snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ts)__bswap_32 (sr->sender_info.ts)); | |||
2091 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "RTP-Timestamp", value); | |||
2092 | ||||
2093 | snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.pc)__bswap_32 (sr->sender_info.pc)); | |||
2094 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Sender-Packet-Count", value); | |||
2095 | ||||
2096 | snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.oc)__bswap_32 (sr->sender_info.oc)); | |||
2097 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Octect-Packet-Count", value); | |||
2098 | ||||
2099 | snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ts)__bswap_32 (sr->sender_info.ts)); | |||
2100 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Last-RTP-Timestamp", value); | |||
2101 | ||||
2102 | snprintf(value, sizeof(value), "%" SWITCH_TIME_T_FMT"ld", switch_time_now()); | |||
2103 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Capture-Time", value); | |||
2104 | ||||
2105 | /* Add sources info */ | |||
2106 | snprintf(header, sizeof(header), "Source-SSRC"); | |||
2107 | snprintf(value, sizeof(value), "%.8x", rtp_session->stats.rtcp.peer_ssrc); | |||
2108 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); | |||
2109 | snprintf(header, sizeof(header), "Source-Fraction"); | |||
2110 | snprintf(value, sizeof(value), "%u", (uint8_t)rtcp_report_block->fraction); | |||
2111 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); | |||
2112 | snprintf(header, sizeof(header), "Source-Lost"); | |||
2113 | #if SWITCH_BYTE_ORDER1234 == __BIG_ENDIAN4321 | |||
2114 | tmpLost = report->lost; /* signed 24bit will extended signess to int32_t automatically */ | |||
2115 | #else | |||
2116 | tmpLost = ntohl(rtcp_report_block->lost)__bswap_32 (rtcp_report_block->lost)>>8; | |||
2117 | tmpLost = tmpLost | ((tmpLost & 0x00800000) ? 0xff000000 : 0x00000000); /* ...and signess compensation */ | |||
2118 | #endif | |||
2119 | snprintf(value, sizeof(value), "%u", tmpLost); | |||
2120 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); | |||
2121 | snprintf(header, sizeof(header), "Source-Highest-Sequence-Number-Received"); | |||
2122 | snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->highest_sequence_number_received)__bswap_32 (rtcp_report_block->highest_sequence_number_received )); | |||
2123 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); | |||
2124 | snprintf(header, sizeof(header), "Source-Jitter"); | |||
2125 | snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->jitter)__bswap_32 (rtcp_report_block->jitter)); | |||
2126 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); | |||
2127 | snprintf(header, sizeof(header), "Source-LSR"); | |||
2128 | snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->lsr)__bswap_32 (rtcp_report_block->lsr)); | |||
2129 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); | |||
2130 | snprintf(header, sizeof(header), "Source-DLSR"); | |||
2131 | snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->dlsr)__bswap_32 (rtcp_report_block->dlsr)); | |||
2132 | switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); | |||
2133 | ||||
2134 | switch_event_fire(&event)switch_event_fire_detailed("src/switch_rtp.c", (const char * ) (const char *)__func__, 2134, &event, ((void*)0)); | |||
2135 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2135, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG10, "Dispatched RTCP SEND event\n"); | |||
2136 | } | |||
2137 | } | |||
2138 | } | |||
2139 | ||||
2140 | #define MAX_NACK10 10 | |||
2141 | static int check_rtcp_and_ice(switch_rtp_t *rtp_session) | |||
2142 | { | |||
2143 | int ret = 0; | |||
2144 | int rtcp_ok = 0, rtcp_cyclic = 0, rtcp_fb = 0, force_send_rr = 0; | |||
2145 | switch_time_t now = switch_micro_time_now(); | |||
2146 | int rate = 0, nack_ttl = 0, nack_dup = 0; | |||
2147 | uint32_t cur_nack[MAX_NACK10] = { 0 }; | |||
2148 | uint16_t seq = 0; | |||
2149 | ||||
2150 | if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && | |||
2151 | rtp_session->flags[SWITCH_RTP_FLAG_AUTO_CNG] && | |||
2152 | rtp_session->send_msg.header.ts && | |||
2153 | rtp_session->cng_pt != INVALID_PT && | |||
2154 | (rtp_session->write_timer.samplecount - rtp_session->last_write_samplecount >= rtp_session->samples_per_interval * 60)) { | |||
2155 | uint8_t data[10] = { 0 }; | |||
2156 | switch_frame_flag_t frame_flags = SFF_NONE; | |||
2157 | data[0] = 65; | |||
2158 | rtp_session->cn++; | |||
2159 | ||||
2160 | get_next_write_ts(rtp_session, 0); | |||
2161 | rtp_session->send_msg.header.ts = htonl(rtp_session->ts)__bswap_32 (rtp_session->ts); | |||
2162 | ||||
2163 | switch_rtp_write_manual(rtp_session, (void *) data, 2, 0, rtp_session->cng_pt, ntohl(rtp_session->send_msg.header.ts)__bswap_32 (rtp_session->send_msg.header.ts), &frame_flags); | |||
2164 | ||||
2165 | if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) { | |||
2166 | rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount; | |||
2167 | } | |||
2168 | } | |||
2169 | ||||
2170 | rate = rtp_session->rtcp_interval; | |||
2171 | ||||
2172 | if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->vb) { | |||
2173 | int n; | |||
2174 | for (n = 0; n < MAX_NACK10; n++) { | |||
2175 | uint32_t nack = switch_jb_pop_nack(rtp_session->vb); | |||
2176 | ||||
2177 | if (!nack) break; | |||
2178 | ||||
2179 | seq = ntohs(nack & 0xFFFF)__bswap_16 (nack & 0xFFFF); | |||
2180 | ||||
2181 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2181, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, "%s Got NACK [%u][0x%x] for seq %u\n", | |||
2182 | switch_core_session_get_name(rtp_session->session)switch_channel_get_name(switch_core_session_get_channel(rtp_session ->session)), nack, nack, seq); | |||
2183 | ||||
2184 | cur_nack[nack_ttl++] = nack; | |||
2185 | } | |||
2186 | if (nack_ttl) { | |||
2187 | rtcp_ok = 1; | |||
2188 | rtcp_fb = 1; | |||
2189 | } | |||
2190 | } | |||
2191 | ||||
2192 | ||||
2193 | ||||
2194 | if (rtp_session->rtcp_sent_packets < 4) { | |||
2195 | rate = 4000; | |||
2196 | } else { | |||
2197 | if (rtp_session->pli_count || rtp_session->fir_count || rtp_session->tmmbr || rtp_session->tmmbn) { | |||
2198 | //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "MARK BW/FIR ETC %d %d\n", rtp_session->pli_count, rtp_session->fir_count); | |||
2199 | rtcp_ok = 1; | |||
2200 | rtcp_fb = 1; | |||
2201 | } | |||
2202 | } | |||
2203 | ||||
2204 | if (rtp_session->send_rr) { | |||
2205 | rtp_session->send_rr = 0; | |||
2206 | rtcp_ok = 1; | |||
2207 | force_send_rr = 1; | |||
2208 | } | |||
2209 | ||||
2210 | //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "TIME CHECK %d > %d\n", (int)((now - rtp_session->rtcp_last_sent) / 1000), rate); | |||
2211 | ||||
2212 | if (!rtcp_ok && (!rtp_session->rtcp_last_sent || (int)((now - rtp_session->rtcp_last_sent) / 1000) > rate)) { | |||
2213 | //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "TIME UP\n"); | |||
2214 | rtcp_cyclic = 1; | |||
2215 | rtcp_ok = 1; | |||
2216 | } | |||
2217 | ||||
2218 | if (rtcp_ok && using_ice(rtp_session)) { | |||
2219 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
2220 | if (!rtp_session->ice.rready) { | |||
2221 | rtcp_ok = 0; | |||
2222 | } | |||
2223 | } else { | |||
2224 | if (!rtp_session->rtcp_ice.rready) { | |||
2225 | rtcp_ok = 0; | |||
2226 | } | |||
2227 | } | |||
2228 | } | |||
2229 | ||||
2230 | //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "WTF %d %d %d %d\n", rate, rtp_session->rtcp_sent_packets, rtcp_ok, nack_ttl); | |||
2231 | ||||
2232 | if (rtp_session->rtcp_sock_output && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] && rtcp_ok) { | |||
2233 | switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp; | |||
2234 | struct switch_rtcp_receiver_report *rr; | |||
2235 | struct switch_rtcp_sender_report *sr; | |||
2236 | struct switch_rtcp_report_block *rtcp_report_block = NULL((void*)0); | |||
2237 | switch_size_t rtcp_bytes = sizeof(struct switch_rtcp_hdr_s)+sizeof(uint32_t); /* add size of the packet header and the ssrc */ | |||
2238 | switch_rtcp_hdr_t *sdes; | |||
2239 | uint8_t *p; | |||
2240 | switch_size_t sdes_bytes = sizeof(struct switch_rtcp_hdr_s); | |||
2241 | uint32_t *ssrc; | |||
2242 | switch_rtcp_sdes_unit_t *unit; | |||
2243 | switch_bool_t is_only_receiver = FALSE0; | |||
2244 | ||||
2245 | if (!rtcp_fb) { | |||
2246 | rtp_session->rtcp_last_sent = now; | |||
2247 | rtp_session->rtcp_sent_packets++; | |||
2248 | } | |||
2249 | ||||
2250 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && | |||
2251 | rtp_session->vb && rtcp_cyclic) { | |||
2252 | nack_dup = rtp_session->prev_nacks_inflight; | |||
2253 | rtp_session->prev_nacks_inflight = 0; | |||
2254 | } | |||
2255 | ||||
2256 | rtp_session->rtcp_send_msg.header.version = 2; | |||
2257 | rtp_session->rtcp_send_msg.header.p = 0; | |||
2258 | ||||
2259 | if ((switch_core_session_media_flow(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO) == SWITCH_MEDIA_FLOW_RECVONLY) || | |||
2260 | switch_core_session_media_flow(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO) == SWITCH_MEDIA_FLOW_RECVONLY) { | |||
2261 | is_only_receiver = TRUE(!0); | |||
2262 | } | |||
2263 | if (!rtp_session->stats.rtcp.sent_pkt_count || is_only_receiver || force_send_rr) { | |||
2264 | rtp_session->rtcp_send_msg.header.type = _RTCP_PT_RR; /* Receiver report */ | |||
2265 | rr=(struct switch_rtcp_receiver_report*) rtp_session->rtcp_send_msg.body; | |||
2266 | rr->ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
2267 | rtcp_report_block = &rr->report_block; | |||
2268 | rtcp_bytes += sizeof(struct switch_rtcp_report_block); | |||
2269 | rtcp_generate_report_block(rtp_session, rtcp_report_block, nack_dup); | |||
2270 | rtp_session->rtcp_send_msg.header.count = 1; /* reception report block count */ | |||
2271 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2271, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP RR (ssrc=%u)\n", rtp_session->ssrc); | |||
2272 | } else { | |||
2273 | struct switch_rtcp_sender_info *rtcp_sender_info; | |||
2274 | rtp_session->rtcp_send_msg.header.type = _RTCP_PT_SR; /* Sender report */ | |||
2275 | sr = (struct switch_rtcp_sender_report*) rtp_session->rtcp_send_msg.body; | |||
2276 | sr->ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
2277 | rtcp_sender_info = &sr->sender_info; | |||
2278 | rtcp_generate_sender_info(rtp_session, rtcp_sender_info); | |||
2279 | rtcp_bytes += sizeof(struct switch_rtcp_sender_info); | |||
2280 | if (!rtcp_cyclic && rtcp_fb) { | |||
2281 | /* rtcp-fb only, don't send receive report block */ | |||
2282 | rtp_session->rtcp_send_msg.header.count = 0; | |||
2283 | } else { | |||
2284 | rtcp_report_block = &sr->report_block; | |||
2285 | rtcp_bytes += sizeof(struct switch_rtcp_report_block); | |||
2286 | rtcp_generate_report_block(rtp_session, rtcp_report_block, nack_dup); | |||
2287 | rtp_session->rtcp_send_msg.header.count = 1; /* reception report block count */ | |||
2288 | stats->sent_pkt_count = 0; | |||
2289 | if ((!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->flags[SWITCH_RTP_FLAG_AUDIO_FIRE_SEND_RTCP_EVENT]) || | |||
2290 | (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_FIRE_SEND_RTCP_EVENT])) { | |||
2291 | switch_send_rtcp_event(rtp_session, sr, rtcp_report_block); | |||
2292 | } | |||
2293 | } | |||
2294 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2294, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP SR (ssrc=%u)\n", rtp_session->ssrc); | |||
2295 | } | |||
2296 | ||||
2297 | rtp_session->rtcp_send_msg.header.length = htons((uint16_t)(rtcp_bytes / 4) - 1)__bswap_16 ((uint16_t)(rtcp_bytes / 4) - 1); | |||
2298 | ||||
2299 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
2300 | if (rtp_session->pli_count) { | |||
2301 | switch_rtcp_ext_hdr_t *ext_hdr; | |||
2302 | ||||
2303 | p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes; | |||
2304 | ext_hdr = (switch_rtcp_ext_hdr_t *) p; | |||
2305 | ||||
2306 | ext_hdr->version = 2; | |||
2307 | ext_hdr->p = 0; | |||
2308 | ext_hdr->fmt = _RTCP_PSFB_PLI; | |||
2309 | ext_hdr->pt = _RTCP_PT_PSFB; | |||
2310 | ||||
2311 | ext_hdr->send_ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
2312 | ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc)__bswap_32 (rtp_session->remote_ssrc); | |||
2313 | rtp_session->rtcp_vstats.video_in.pli_count++; | |||
2314 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2314, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP PLI %u %u [%u]\n", | |||
2315 | rtp_session->ssrc, rtp_session->remote_ssrc, rtp_session->rtcp_vstats.video_in.pli_count); | |||
2316 | ||||
2317 | ext_hdr->length = htons((uint8_t)(sizeof(switch_rtcp_ext_hdr_t) / 4) - 1)__bswap_16 ((uint8_t)(sizeof(switch_rtcp_ext_hdr_t) / 4) - 1); | |||
2318 | rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t); | |||
2319 | rtp_session->pli_count = 0; | |||
2320 | } | |||
2321 | ||||
2322 | if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && nack_ttl > 0) { | |||
2323 | int n = 0; | |||
2324 | ||||
2325 | rtp_session->rtcp_vstats.video_in.nack_count++; | |||
2326 | for (n = 0; n < nack_ttl; n++) { | |||
2327 | switch_rtcp_ext_hdr_t *ext_hdr; | |||
2328 | uint32_t *nack; | |||
2329 | p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes; | |||
2330 | ext_hdr = (switch_rtcp_ext_hdr_t *) p; | |||
2331 | ||||
2332 | ext_hdr->version = 2; | |||
2333 | ext_hdr->p = 0; | |||
2334 | ext_hdr->fmt = _RTCP_RTPFB_NACK; | |||
2335 | ext_hdr->pt = _RTCP_PT_RTPFB; | |||
2336 | ext_hdr->send_ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
2337 | ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc)__bswap_32 (rtp_session->remote_ssrc); | |||
2338 | ext_hdr->length = htons(3)__bswap_16 (3); | |||
2339 | p += sizeof(switch_rtcp_ext_hdr_t); | |||
2340 | nack = (uint32_t *) p; | |||
2341 | *nack = cur_nack[n]; | |||
2342 | ||||
2343 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2343, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP NACK %u [%d]\n", | |||
2344 | ntohs(*nack & 0xFFFF)__bswap_16 (*nack & 0xFFFF), rtp_session->rtcp_vstats.video_in.nack_count); | |||
2345 | ||||
2346 | rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(cur_nack[n]); | |||
2347 | cur_nack[n] = 0; | |||
2348 | } | |||
2349 | rtp_session->prev_nacks_inflight = n; | |||
2350 | } | |||
2351 | ||||
2352 | if (rtp_session->fir_count) { | |||
2353 | switch_rtcp_ext_hdr_t *ext_hdr; | |||
2354 | rtcp_fir_t *fir; | |||
2355 | ||||
2356 | if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_OLD_FIR)) { | |||
2357 | p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes; | |||
2358 | ext_hdr = (switch_rtcp_ext_hdr_t *) p; | |||
2359 | ||||
2360 | ext_hdr->version = 2; | |||
2361 | ext_hdr->pt = _RTCP_PT_FIR; | |||
2362 | rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t); | |||
2363 | } | |||
2364 | ||||
2365 | ||||
2366 | p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes; | |||
2367 | ext_hdr = (switch_rtcp_ext_hdr_t *) p; | |||
2368 | ||||
2369 | p += sizeof(switch_rtcp_ext_hdr_t); | |||
2370 | fir = (rtcp_fir_t *) p; | |||
2371 | ||||
2372 | ext_hdr->version = 2; | |||
2373 | ext_hdr->p = 0; | |||
2374 | ext_hdr->fmt = _RTCP_PSFB_FIR; | |||
2375 | ext_hdr->pt = _RTCP_PT_PSFB; | |||
2376 | ||||
2377 | ext_hdr->send_ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
2378 | ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc)__bswap_32 (rtp_session->remote_ssrc); | |||
2379 | ||||
2380 | fir->ssrc = htonl(rtp_session->remote_ssrc)__bswap_32 (rtp_session->remote_ssrc); | |||
2381 | fir->seq = rtp_session->fir_seq; | |||
2382 | fir->r1 = fir->r2 = fir->r3 = 0; | |||
2383 | ||||
2384 | rtp_session->rtcp_vstats.video_in.fir_count++; | |||
2385 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2385, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP FIR SEQ %d [%u]\n", rtp_session->fir_seq, rtp_session->rtcp_vstats.video_in.fir_count); | |||
2386 | ||||
2387 | rtp_session->fir_seq++; | |||
2388 | ||||
2389 | ext_hdr->length = htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t)) / 4) - 1)__bswap_16 ((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof (rtcp_fir_t)) / 4) - 1); | |||
2390 | rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t); | |||
2391 | rtp_session->fir_count = 0; | |||
2392 | } | |||
2393 | ||||
2394 | //if (!rtp_session->tmmbr && rtp_session->cur_tmmbr) { | |||
2395 | // rtp_session->tmmbr = rtp_session->cur_tmmbr; | |||
2396 | //} | |||
2397 | ||||
2398 | while (rtp_session->tmmbr || rtp_session->tmmbn) { | |||
2399 | switch_rtcp_ext_hdr_t *ext_hdr; | |||
2400 | rtcp_tmmbx_t *tmmbx; | |||
2401 | uint32_t bps = 0; | |||
2402 | p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes; | |||
2403 | ext_hdr = (switch_rtcp_ext_hdr_t *) p; | |||
2404 | ||||
2405 | p += sizeof(switch_rtcp_ext_hdr_t); | |||
2406 | tmmbx = (rtcp_tmmbx_t *) p; | |||
2407 | ||||
2408 | ext_hdr->version = 2; | |||
2409 | ext_hdr->p = 0; | |||
2410 | ext_hdr->pt = _RTCP_PT_RTPFB; | |||
2411 | ext_hdr->send_ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
2412 | ext_hdr->recv_ssrc = 0; | |||
2413 | ||||
2414 | if (rtp_session->tmmbr) { | |||
2415 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2415, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP TMMBR %u\n", rtp_session->tmmbr); | |||
2416 | ext_hdr->fmt = _RTCP_RTPFB_TMMBR; | |||
2417 | bps = rtp_session->tmmbr; | |||
2418 | rtp_session->tmmbr = 0; | |||
2419 | } else { | |||
2420 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2420, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP TMMBN %u\n", rtp_session->tmmbr); | |||
2421 | ext_hdr->fmt = _RTCP_RTPFB_TMMBN; | |||
2422 | bps = rtp_session->tmmbn; | |||
2423 | rtp_session->tmmbn = 0; | |||
2424 | } | |||
2425 | ||||
2426 | tmmbx->ssrc = htonl(rtp_session->remote_ssrc)__bswap_32 (rtp_session->remote_ssrc); | |||
2427 | calc_bw_exp(bps, 17, tmmbx); | |||
2428 | ||||
2429 | ext_hdr->length = htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_tmmbx_t)) / 4) - 1)__bswap_16 ((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof (rtcp_tmmbx_t)) / 4) - 1); | |||
2430 | rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_tmmbx_t); | |||
2431 | } | |||
2432 | ||||
2433 | } | |||
2434 | ||||
2435 | //SDES + CNAME | |||
2436 | p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes; | |||
2437 | sdes = (switch_rtcp_hdr_t *) p; | |||
2438 | sdes->version = 2; | |||
2439 | sdes->type = _RTCP_PT_SDES; | |||
2440 | sdes->count = 1; | |||
2441 | sdes->p = 0; | |||
2442 | p = (uint8_t *) (sdes) + sdes_bytes; | |||
2443 | ssrc = (uint32_t *) p; | |||
2444 | *ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
2445 | sdes_bytes += sizeof(uint32_t); | |||
2446 | ||||
2447 | ||||
2448 | p = (uint8_t *) (sdes) + sdes_bytes; | |||
2449 | unit = (switch_rtcp_sdes_unit_t *) p; | |||
2450 | unit->type = _RTCP_SDES_CNAME; | |||
2451 | snprintf((char *)unit->value, 80, "%x", rtp_session->ssrc); | |||
2452 | unit->length = strlen((char *)unit->value); | |||
2453 | sdes_bytes += sizeof(switch_rtcp_sdes_unit_t) + unit->length; | |||
2454 | ||||
2455 | ||||
2456 | p += sizeof(switch_rtcp_sdes_unit_t) + unit->length; | |||
2457 | unit = (switch_rtcp_sdes_unit_t *) p; | |||
2458 | unit->type = _RTCP_SDES_NOTE; | |||
2459 | snprintf((char *)unit->value, 80, "FreeSWITCH.org -- Come to ClueCon.com"); | |||
2460 | unit->length = strlen((char *)unit->value); | |||
2461 | sdes_bytes += sizeof(switch_rtcp_sdes_unit_t) + unit->length; | |||
2462 | ||||
2463 | sdes_bytes ++;//END | |||
2464 | ||||
2465 | sdes_bytes += 4 - (sdes_bytes % 4); | |||
2466 | ||||
2467 | sdes->length = htons((uint16_t)(sdes_bytes / 4) - 1)__bswap_16 ((uint16_t)(sdes_bytes / 4) - 1); | |||
2468 | rtcp_bytes += sdes_bytes; | |||
2469 | ||||
2470 | /* Prepare next report */ | |||
2471 | if (rtp_session->rtcp_send_msg.header.count) { | |||
2472 | stats->last_rpt_cycle = stats->cycle; | |||
2473 | stats->last_rpt_ext_seq = stats->high_ext_seq_recv; | |||
2474 | stats->last_rpt_ts = rtp_session->write_timer.samplecount; | |||
2475 | stats->period_pkt_count = 0; | |||
2476 | } | |||
2477 | ||||
2478 | ||||
2479 | ||||
2480 | #ifdef ENABLE_SRTP1 | |||
2481 | switch_mutex_lock(rtp_session->ice_mutex); | |||
2482 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) { | |||
2483 | int stat = 0; | |||
2484 | int sbytes = (int) rtcp_bytes; | |||
2485 | ||||
2486 | if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) { | |||
2487 | stat = srtp_protect_rtcp(rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_send_msg.header, &sbytes); | |||
2488 | } else { | |||
2489 | stat = srtp_protect_rtcp_mki(rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_send_msg.header, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX0); | |||
2490 | } | |||
2491 | ||||
2492 | if (stat) { | |||
2493 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2493, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat); | |||
2494 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
2495 | goto end; | |||
2496 | } else { | |||
2497 | rtcp_bytes = sbytes; | |||
2498 | } | |||
2499 | } | |||
2500 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
2501 | #endif | |||
2502 | ||||
2503 | #ifdef ENABLE_ZRTP | |||
2504 | /* ZRTP Send */ | |||
2505 | if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) { | |||
2506 | unsigned int sbytes = (int) rtcp_bytes; | |||
2507 | zrtp_status_t stat = zrtp_status_fail; | |||
2508 | ||||
2509 | stat = zrtp_process_rtcp(rtp_session->zrtp_stream, (void *) &rtp_session->rtcp_send_msg, &sbytes); | |||
2510 | ||||
2511 | switch (stat) { | |||
2512 | case zrtp_status_ok: | |||
2513 | break; | |||
2514 | case zrtp_status_drop: | |||
2515 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 2515, ((void*)0), SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat); | |||
2516 | ret = (int)rtcp_bytes; | |||
2517 | goto end; | |||
2518 | break; | |||
2519 | case zrtp_status_fail: | |||
2520 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 2520, ((void*)0), SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat); | |||
2521 | break; | |||
2522 | default: | |||
2523 | break; | |||
2524 | } | |||
2525 | ||||
2526 | rtcp_bytes = sbytes; | |||
2527 | } | |||
2528 | #endif | |||
2529 | //#define DEBUG_EXTRA | |||
2530 | #ifdef DEBUG_EXTRA | |||
2531 | { | |||
2532 | const char *old_host; | |||
2533 | char bufb[50]; | |||
2534 | old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr); | |||
2535 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2535, (const char*)(rtp_session->session), SWITCH_LOG_CRIT, "%s SEND %s RTCP %s:%d %ld\n", | |||
2536 | rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel (rtp_session->session)) : "-", | |||
2537 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), | |||
2538 | old_host, | |||
2539 | switch_sockaddr_get_port(rtp_session->rtcp_remote_addr), | |||
2540 | rtcp_bytes); | |||
2541 | } | |||
2542 | #endif | |||
2543 | 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) { | |||
2544 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2544, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG,"RTCP packet not written\n"); | |||
2545 | } else { | |||
2546 | rtp_session->stats.inbound.period_packet_count = 0; | |||
2547 | } | |||
2548 | } | |||
2549 | ||||
2550 | if (rtp_session->ice.ice_user) { | |||
2551 | if (ice_out(rtp_session, &rtp_session->ice) == SWITCH_STATUS_GENERR) { | |||
2552 | ret = -1; | |||
2553 | goto end; | |||
2554 | } | |||
2555 | } | |||
2556 | ||||
2557 | if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
2558 | if (rtp_session->rtcp_ice.ice_user) { | |||
2559 | if (ice_out(rtp_session, &rtp_session->rtcp_ice) == SWITCH_STATUS_GENERR) { | |||
2560 | ret = -1; | |||
2561 | goto end; | |||
2562 | } | |||
2563 | } | |||
2564 | } | |||
2565 | ||||
2566 | end: | |||
2567 | ||||
2568 | return ret; | |||
2569 | } | |||
2570 | ||||
2571 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_ping(switch_rtp_t *rtp_session) | |||
2572 | { | |||
2573 | check_rtcp_and_ice(rtp_session); | |||
2574 | } | |||
2575 | ||||
2576 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_get_random(void *buf, uint32_t len) | |||
2577 | { | |||
2578 | #ifdef HAVE_OPENSSL1 | |||
2579 | RAND_bytes(buf, len); | |||
2580 | #else | |||
2581 | switch_stun_random_string(buf, len, NULL((void*)0)); | |||
2582 | #endif | |||
2583 | } | |||
2584 | ||||
2585 | ||||
2586 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_shutdown(void) | |||
2587 | { | |||
2588 | switch_core_port_allocator_t *alloc = NULL((void*)0); | |||
2589 | switch_hash_index_t *hi; | |||
2590 | const void *var; | |||
2591 | void *val; | |||
2592 | ||||
2593 | if (!global_init) { | |||
2594 | return; | |||
2595 | } | |||
2596 | ||||
2597 | switch_mutex_lock(port_lock); | |||
2598 | ||||
2599 | for (hi = switch_core_hash_first(alloc_hash)switch_core_hash_first_iter(alloc_hash, ((void*)0)); hi; hi = switch_core_hash_next(&hi)) { | |||
2600 | switch_core_hash_this(hi, &var, NULL((void*)0), &val); | |||
2601 | if ((alloc = (switch_core_port_allocator_t *) val)) { | |||
2602 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 2602, ((void*)0), SWITCH_LOG_DEBUG, "Destroy port allocator for %s\n", (char *) var); | |||
2603 | switch_core_port_allocator_destroy(&alloc); | |||
2604 | } | |||
2605 | } | |||
2606 | ||||
2607 | switch_core_hash_destroy(&alloc_hash); | |||
2608 | switch_mutex_unlock(port_lock); | |||
2609 | ||||
2610 | #ifdef ENABLE_ZRTP | |||
2611 | if (zrtp_on) { | |||
2612 | zrtp_status_t status = zrtp_status_ok; | |||
2613 | ||||
2614 | status = zrtp_def_cache_store(zrtp_global); | |||
2615 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 2615, ((void*)0), SWITCH_LOG_DEBUG, "Saving ZRTP cache: %s\n", zrtp_status_ok == status ? "OK" : "FAIL"); | |||
2616 | zrtp_down(zrtp_global); | |||
2617 | } | |||
2618 | #endif | |||
2619 | #ifdef ENABLE_SRTP1 | |||
2620 | srtp_crypto_kernel_shutdown(); | |||
2621 | #endif | |||
2622 | switch_rtp_dtls_destroy(); | |||
2623 | } | |||
2624 | ||||
2625 | SWITCH_DECLARE(switch_port_t)__attribute__((visibility("default"))) switch_port_t switch_rtp_set_start_port(switch_port_t port) | |||
2626 | { | |||
2627 | if (port) { | |||
2628 | if (port_lock) { | |||
2629 | switch_mutex_lock(port_lock); | |||
2630 | } | |||
2631 | START_PORT = port; | |||
2632 | if (port_lock) { | |||
2633 | switch_mutex_unlock(port_lock); | |||
2634 | } | |||
2635 | } | |||
2636 | return START_PORT; | |||
2637 | } | |||
2638 | ||||
2639 | SWITCH_DECLARE(switch_port_t)__attribute__((visibility("default"))) switch_port_t switch_rtp_set_end_port(switch_port_t port) | |||
2640 | { | |||
2641 | if (port) { | |||
2642 | if (port_lock) { | |||
2643 | switch_mutex_lock(port_lock); | |||
2644 | } | |||
2645 | END_PORT = port; | |||
2646 | if (port_lock) { | |||
2647 | switch_mutex_unlock(port_lock); | |||
2648 | } | |||
2649 | } | |||
2650 | return END_PORT; | |||
2651 | } | |||
2652 | ||||
2653 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_release_port(const char *ip, switch_port_t port) | |||
2654 | { | |||
2655 | switch_core_port_allocator_t *alloc = NULL((void*)0); | |||
2656 | ||||
2657 | if (!ip || !port) { | |||
2658 | return; | |||
2659 | } | |||
2660 | ||||
2661 | switch_mutex_lock(port_lock); | |||
2662 | if ((alloc = switch_core_hash_find(alloc_hash, ip))) { | |||
2663 | switch_core_port_allocator_free_port(alloc, port); | |||
2664 | } | |||
2665 | switch_mutex_unlock(port_lock); | |||
2666 | ||||
2667 | } | |||
2668 | ||||
2669 | SWITCH_DECLARE(switch_port_t)__attribute__((visibility("default"))) switch_port_t switch_rtp_request_port(const char *ip) | |||
2670 | { | |||
2671 | switch_port_t port = 0; | |||
2672 | switch_core_port_allocator_t *alloc = NULL((void*)0); | |||
2673 | ||||
2674 | switch_mutex_lock(port_lock); | |||
2675 | alloc = switch_core_hash_find(alloc_hash, ip); | |||
2676 | if (!alloc) { | |||
2677 | if (switch_core_port_allocator_new(ip, START_PORT, END_PORT, SPF_EVEN, &alloc) != SWITCH_STATUS_SUCCESS) { | |||
2678 | abort(); | |||
2679 | } | |||
2680 | ||||
2681 | switch_core_hash_insert(alloc_hash, ip, alloc)switch_core_hash_insert_destructor(alloc_hash, ip, alloc, ((void *)0)); | |||
2682 | } | |||
2683 | ||||
2684 | if (switch_core_port_allocator_request_port(alloc, &port) != SWITCH_STATUS_SUCCESS) { | |||
2685 | port = 0; | |||
2686 | } | |||
2687 | ||||
2688 | switch_mutex_unlock(port_lock); | |||
2689 | return port; | |||
2690 | } | |||
2691 | ||||
2692 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_set_payload_map(switch_rtp_t *rtp_session, payload_map_t **pmap) | |||
2693 | { | |||
2694 | ||||
2695 | if (rtp_session) { | |||
2696 | switch_mutex_lock(rtp_session->flag_mutex); | |||
2697 | rtp_session->pmaps = pmap; | |||
2698 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
2699 | return SWITCH_STATUS_SUCCESS; | |||
2700 | } | |||
2701 | ||||
2702 | return SWITCH_STATUS_FALSE; | |||
2703 | } | |||
2704 | ||||
2705 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_intentional_bugs(switch_rtp_t *rtp_session, switch_rtp_bug_flag_t bugs) | |||
2706 | { | |||
2707 | rtp_session->rtp_bugs = bugs; | |||
2708 | ||||
2709 | if ((rtp_session->rtp_bugs & RTP_BUG_START_SEQ_AT_ZERO)) { | |||
2710 | rtp_session->seq = 0; | |||
2711 | } | |||
2712 | ||||
2713 | } | |||
2714 | ||||
2715 | ||||
2716 | static switch_status_t enable_remote_rtcp_socket(switch_rtp_t *rtp_session, const char **err) { | |||
2717 | ||||
2718 | switch_status_t status = SWITCH_STATUS_SUCCESS; | |||
2719 | ||||
2720 | if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
2721 | ||||
2722 | if (switch_sockaddr_info_get(&rtp_session->rtcp_remote_addr, rtp_session->eff_remote_host_str, SWITCH_UNSPEC0, | |||
2723 | rtp_session->remote_rtcp_port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !rtp_session->rtcp_remote_addr) { | |||
2724 | *err = "RTCP Remote Address Error!"; | |||
2725 | return SWITCH_STATUS_FALSE; | |||
2726 | } else { | |||
2727 | const char *host; | |||
2728 | char bufa[50]; | |||
2729 | ||||
2730 | host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_remote_addr); | |||
2731 | ||||
2732 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2732, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, | |||
2733 | "Setting RTCP remote addr to %s:%d %d\n", host, rtp_session->remote_rtcp_port, rtp_session->rtcp_remote_addr->family); | |||
2734 | } | |||
2735 | ||||
2736 | if (rtp_session->rtcp_sock_input && switch_sockaddr_get_family(rtp_session->rtcp_remote_addr) == | |||
2737 | switch_sockaddr_get_family(rtp_session->rtcp_local_addr)) { | |||
2738 | rtp_session->rtcp_sock_output = rtp_session->rtcp_sock_input; | |||
2739 | } else { | |||
2740 | ||||
2741 | if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) { | |||
2742 | switch_socket_close(rtp_session->rtcp_sock_output); | |||
2743 | } | |||
2744 | ||||
2745 | if ((status = switch_socket_create(&rtp_session->rtcp_sock_output, | |||
2746 | switch_sockaddr_get_family(rtp_session->rtcp_remote_addr), | |||
2747 | SOCK_DGRAMSOCK_DGRAM, 0, rtp_session->pool)) != SWITCH_STATUS_SUCCESS) { | |||
2748 | *err = "RTCP Socket Error!"; | |||
2749 | } | |||
2750 | } | |||
2751 | ||||
2752 | } else { | |||
2753 | *err = "RTCP NOT ACTIVE!"; | |||
2754 | } | |||
2755 | ||||
2756 | return status; | |||
2757 | ||||
2758 | } | |||
2759 | ||||
2760 | static switch_status_t enable_local_rtcp_socket(switch_rtp_t *rtp_session, const char **err) { | |||
2761 | ||||
2762 | const char *host = rtp_session->local_host_str; | |||
2763 | switch_port_t port = rtp_session->local_port; | |||
2764 | switch_socket_t *rtcp_new_sock = NULL((void*)0), *rtcp_old_sock = NULL((void*)0); | |||
2765 | switch_status_t status = SWITCH_STATUS_SUCCESS; | |||
2766 | char bufa[50]; | |||
2767 | ||||
2768 | if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
2769 | if (switch_sockaddr_info_get(&rtp_session->rtcp_local_addr, host, SWITCH_UNSPEC0, port+1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) { | |||
2770 | *err = "RTCP Local Address Error!"; | |||
2771 | goto done; | |||
2772 | } | |||
2773 | ||||
2774 | 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) { | |||
2775 | *err = "RTCP Socket Error!"; | |||
2776 | goto done; | |||
2777 | } | |||
2778 | ||||
2779 | if (switch_socket_opt_set(rtcp_new_sock, SWITCH_SO_REUSEADDR16, 1) != SWITCH_STATUS_SUCCESS) { | |||
2780 | *err = "RTCP Socket Error!"; | |||
2781 | goto done; | |||
2782 | } | |||
2783 | ||||
2784 | if (switch_socket_bind(rtcp_new_sock, rtp_session->rtcp_local_addr) != SWITCH_STATUS_SUCCESS) { | |||
2785 | *err = "RTCP Bind Error!"; | |||
2786 | goto done; | |||
2787 | } | |||
2788 | ||||
2789 | if (switch_sockaddr_info_get(&rtp_session->rtcp_from_addr, switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr), | |||
2790 | SWITCH_UNSPEC0, switch_sockaddr_get_port(rtp_session->from_addr) + 1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) { | |||
2791 | *err = "RTCP From Address Error!"; | |||
2792 | goto done; | |||
2793 | } | |||
2794 | ||||
2795 | rtcp_old_sock = rtp_session->rtcp_sock_input; | |||
2796 | rtp_session->rtcp_sock_input = rtcp_new_sock; | |||
2797 | rtcp_new_sock = NULL((void*)0); | |||
2798 | ||||
2799 | switch_socket_create_pollset(&rtp_session->rtcp_read_pollfd, rtp_session->rtcp_sock_input, SWITCH_POLLIN0x001 | SWITCH_POLLERR0x010, rtp_session->pool); | |||
2800 | ||||
2801 | done: | |||
2802 | ||||
2803 | if (*err) { | |||
2804 | ||||
2805 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2805, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating rtcp [%s]\n", *err); | |||
2806 | status = SWITCH_STATUS_FALSE; | |||
2807 | } | |||
2808 | ||||
2809 | if (rtcp_new_sock) { | |||
2810 | switch_socket_close(rtcp_new_sock); | |||
2811 | } | |||
2812 | ||||
2813 | if (rtcp_old_sock) { | |||
2814 | switch_socket_close(rtcp_old_sock); | |||
2815 | } | |||
2816 | } else { | |||
2817 | status = SWITCH_STATUS_FALSE; | |||
2818 | } | |||
2819 | ||||
2820 | return status; | |||
2821 | } | |||
2822 | ||||
2823 | SWITCH_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) | |||
2824 | { | |||
2825 | switch_socket_t *new_sock = NULL((void*)0), *old_sock = NULL((void*)0); | |||
2826 | switch_status_t status = SWITCH_STATUS_FALSE; | |||
2827 | int j = 0; | |||
2828 | #ifndef WIN32 | |||
2829 | char o[5] = "TEST", i[5] = ""; | |||
2830 | switch_size_t len, ilen = 0; | |||
2831 | int x; | |||
2832 | #endif | |||
2833 | ||||
2834 | if (rtp_session->ready != 1) { | |||
2835 | if (!switch_rtp_ready(rtp_session)) { | |||
2836 | return SWITCH_STATUS_FALSE; | |||
2837 | } | |||
2838 | ||||
2839 | READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session-> reading++; | |||
2840 | WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session-> writing++; | |||
2841 | ||||
2842 | if (!switch_rtp_ready(rtp_session)) { | |||
2843 | goto done; | |||
2844 | } | |||
2845 | } | |||
2846 | ||||
2847 | ||||
2848 | *err = NULL((void*)0); | |||
2849 | ||||
2850 | if (zstr(host)_zstr(host) || !port) { | |||
2851 | *err = "Address Error"; | |||
2852 | goto done; | |||
2853 | } | |||
2854 | ||||
2855 | ||||
2856 | 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__, 2856); | |||
2857 | rtp_session->local_port = port; | |||
2858 | ||||
2859 | ||||
2860 | if (switch_sockaddr_info_get(&rtp_session->local_addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) { | |||
2861 | *err = "Local Address Error!"; | |||
2862 | goto done; | |||
2863 | } | |||
2864 | ||||
2865 | ||||
2866 | if (rtp_session->sock_input) { | |||
2867 | switch_rtp_kill_socket(rtp_session); | |||
2868 | } | |||
2869 | ||||
2870 | if (switch_socket_create(&new_sock, switch_sockaddr_get_family(rtp_session->local_addr), SOCK_DGRAMSOCK_DGRAM, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) { | |||
2871 | *err = "Socket Error!"; | |||
2872 | goto done; | |||
2873 | } | |||
2874 | ||||
2875 | if (switch_socket_opt_set(new_sock, SWITCH_SO_REUSEADDR16, 1) != SWITCH_STATUS_SUCCESS) { | |||
2876 | *err = "Socket Error!"; | |||
2877 | goto done; | |||
2878 | } | |||
2879 | ||||
2880 | //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
2881 | // switch_socket_opt_set(new_sock, SWITCH_SO_RCVBUF, 1572864); | |||
2882 | // switch_socket_opt_set(new_sock, SWITCH_SO_SNDBUF, 1572864); | |||
2883 | //} | |||
2884 | ||||
2885 | if (switch_socket_bind(new_sock, rtp_session->local_addr) != SWITCH_STATUS_SUCCESS) { | |||
2886 | char *em = switch_core_sprintf(rtp_session->pool, "Bind Error! %s:%d", host, port); | |||
2887 | *err = em; | |||
2888 | goto done; | |||
2889 | } | |||
2890 | ||||
2891 | ||||
2892 | if ((j = atoi(host)) && j > 223 && j < 240) { /* mcast */ | |||
2893 | if (switch_mcast_interface(new_sock, rtp_session->local_addr) != SWITCH_STATUS_SUCCESS) { | |||
2894 | *err = "Multicast Socket interface Error"; | |||
2895 | goto done; | |||
2896 | } | |||
2897 | ||||
2898 | if (switch_mcast_join(new_sock, rtp_session->local_addr, NULL((void*)0), NULL((void*)0)) != SWITCH_STATUS_SUCCESS) { | |||
2899 | *err = "Multicast Error"; | |||
2900 | goto done; | |||
2901 | } | |||
2902 | ||||
2903 | if (rtp_session->session) { | |||
2904 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
2905 | const char *var; | |||
2906 | ||||
2907 | if ((var = switch_channel_get_variable(channel, "multicast_ttl")switch_channel_get_variable_dup(channel, "multicast_ttl", SWITCH_TRUE , -1))) { | |||
2908 | int ttl = atoi(var); | |||
2909 | ||||
2910 | if (ttl > 0 && ttl < 256) { | |||
2911 | if (switch_mcast_hops(new_sock, (uint8_t) ttl) != SWITCH_STATUS_SUCCESS) { | |||
2912 | *err = "Mutlicast TTL set failed"; | |||
2913 | goto done; | |||
2914 | } | |||
2915 | ||||
2916 | } | |||
2917 | } | |||
2918 | ||||
2919 | } | |||
2920 | ||||
2921 | } | |||
2922 | ||||
2923 | ||||
2924 | ||||
2925 | #ifndef WIN32 | |||
2926 | len = sizeof(i); | |||
2927 | switch_socket_opt_set(new_sock, SWITCH_SO_NONBLOCK8, TRUE(!0)); | |||
2928 | ||||
2929 | switch_socket_sendto(new_sock, rtp_session->local_addr, 0, (void *) o, &len); | |||
2930 | ||||
2931 | x = 0; | |||
2932 | while (!ilen) { | |||
2933 | switch_status_t status; | |||
2934 | ilen = len; | |||
2935 | status = switch_socket_recvfrom(rtp_session->from_addr, new_sock, 0, (void *) i, &ilen); | |||
2936 | ||||
2937 | if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) { | |||
2938 | break; | |||
2939 | } | |||
2940 | ||||
2941 | if (++x > 1000) { | |||
2942 | break; | |||
2943 | } | |||
2944 | switch_cond_next(); | |||
2945 | } | |||
2946 | switch_socket_opt_set(new_sock, SWITCH_SO_NONBLOCK8, FALSE0); | |||
2947 | ||||
2948 | #endif | |||
2949 | ||||
2950 | old_sock = rtp_session->sock_input; | |||
2951 | rtp_session->sock_input = new_sock; | |||
2952 | new_sock = NULL((void*)0); | |||
2953 | ||||
2954 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] || rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] || rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
2955 | switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, TRUE(!0)); | |||
2956 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK); | |||
2957 | } | |||
2958 | ||||
2959 | switch_socket_create_pollset(&rtp_session->read_pollfd, rtp_session->sock_input, SWITCH_POLLIN0x001 | SWITCH_POLLERR0x010, rtp_session->pool); | |||
2960 | ||||
2961 | if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
2962 | if ((status = enable_local_rtcp_socket(rtp_session, err)) == SWITCH_STATUS_SUCCESS) { | |||
2963 | *err = "Success"; | |||
2964 | } | |||
2965 | } else { | |||
2966 | status = SWITCH_STATUS_SUCCESS; | |||
2967 | *err = "Success"; | |||
2968 | } | |||
2969 | ||||
2970 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_IO); | |||
2971 | ||||
2972 | done: | |||
2973 | ||||
2974 | if (new_sock) { | |||
2975 | switch_socket_close(new_sock); | |||
2976 | } | |||
2977 | ||||
2978 | if (old_sock) { | |||
2979 | switch_socket_close(old_sock); | |||
2980 | } | |||
2981 | ||||
2982 | ||||
2983 | if (rtp_session->ready != 1) { | |||
2984 | WRITE_DEC(rtp_session)rtp_session->writing--; switch_mutex_unlock(rtp_session-> write_mutex); | |||
2985 | READ_DEC(rtp_session)rtp_session->reading--; switch_mutex_unlock(rtp_session-> read_mutex); | |||
2986 | } | |||
2987 | ||||
2988 | return status; | |||
2989 | } | |||
2990 | ||||
2991 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_media_timeout(switch_rtp_t *rtp_session, uint32_t ms) | |||
2992 | { | |||
2993 | if (!switch_rtp_ready(rtp_session) || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
2994 | return; | |||
2995 | } | |||
2996 | ||||
2997 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 2997, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, | |||
2998 | "%s MEDIA TIMEOUT %s set to %u", switch_core_session_get_name(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_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), ms); | |||
2999 | rtp_session->media_timeout = ms; | |||
3000 | switch_rtp_reset_media_timer(rtp_session); | |||
3001 | } | |||
3002 | ||||
3003 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_max_missed_packets(switch_rtp_t *rtp_session, uint32_t max) | |||
3004 | { | |||
3005 | if (!switch_rtp_ready(rtp_session) || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
3006 | return; | |||
3007 | } | |||
3008 | ||||
3009 | if (rtp_session->missed_count > max) { | |||
3010 | ||||
3011 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3011, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, | |||
3012 | "new max missed packets(%d->%d) greater than current missed packets(%d). RTP will timeout.\n", | |||
3013 | rtp_session->max_missed_packets, max, rtp_session->missed_count); | |||
3014 | } | |||
3015 | ||||
3016 | rtp_session->max_missed_packets = max; | |||
3017 | } | |||
3018 | ||||
3019 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_reset_jb(switch_rtp_t *rtp_session) | |||
3020 | { | |||
3021 | if (switch_rtp_ready(rtp_session)) { | |||
3022 | if (rtp_session->jb) { | |||
3023 | switch_jb_reset(rtp_session->jb); | |||
3024 | } | |||
3025 | } | |||
3026 | } | |||
3027 | ||||
3028 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_reset_vb(switch_rtp_t *rtp_session) | |||
3029 | { | |||
3030 | ||||
3031 | if (rtp_session->vb) { | |||
3032 | switch_jb_reset(rtp_session->vb); | |||
3033 | } | |||
3034 | ||||
3035 | if (rtp_session->vbw) { | |||
3036 | switch_jb_reset(rtp_session->vbw); | |||
3037 | } | |||
3038 | } | |||
3039 | ||||
3040 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_reset(switch_rtp_t *rtp_session) | |||
3041 | { | |||
3042 | if (!rtp_session) { | |||
3043 | return; | |||
3044 | } | |||
3045 | ||||
3046 | rtp_session->seq = (uint16_t) rand(); | |||
3047 | rtp_session->ts = 0; | |||
3048 | memset(&rtp_session->ts_norm, 0, sizeof(rtp_session->ts_norm)); | |||
3049 | ||||
3050 | rtp_session->last_stun = rtp_session->first_stun = 0; | |||
3051 | rtp_session->wrong_addrs = 0; | |||
3052 | rtp_session->rtcp_sent_packets = 0; | |||
3053 | rtp_session->rtcp_last_sent = 0; | |||
3054 | rtp_session->ice_adj = 0; | |||
3055 | ||||
3056 | //switch_rtp_del_dtls(rtp_session, DTLS_TYPE_RTP|DTLS_TYPE_RTCP); | |||
3057 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_PAUSE); | |||
3058 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_MUTE); | |||
3059 | rtcp_stats_init(rtp_session); | |||
3060 | ||||
3061 | if (rtp_session->ice.ready) { | |||
3062 | switch_rtp_reset_vb(rtp_session); | |||
3063 | rtp_session->ice.ready = rtp_session->ice.rready = 0; | |||
3064 | } | |||
3065 | ||||
3066 | } | |||
3067 | ||||
3068 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_reset_media_timer(switch_rtp_t *rtp_session) | |||
3069 | { | |||
3070 | rtp_session->missed_count = 0; | |||
3071 | rtp_session->last_media = switch_micro_time_now(); | |||
3072 | } | |||
3073 | ||||
3074 | SWITCH_DECLARE(char *)__attribute__((visibility("default"))) char * switch_rtp_get_remote_host(switch_rtp_t *rtp_session) | |||
3075 | { | |||
3076 | return zstr(rtp_session->remote_host_str)_zstr(rtp_session->remote_host_str) ? "0.0.0.0" : rtp_session->remote_host_str; | |||
3077 | } | |||
3078 | ||||
3079 | SWITCH_DECLARE(switch_port_t)__attribute__((visibility("default"))) switch_port_t switch_rtp_get_remote_port(switch_rtp_t *rtp_session) | |||
3080 | { | |||
3081 | return rtp_session->remote_port; | |||
3082 | } | |||
3083 | ||||
3084 | static void ping_socket(switch_rtp_t *rtp_session) | |||
3085 | { | |||
3086 | uint32_t o = UINT_MAX(2147483647 *2U +1U); | |||
3087 | switch_size_t len = sizeof(o); | |||
3088 | switch_socket_sendto(rtp_session->sock_input, rtp_session->local_addr, 0, (void *) &o, &len); | |||
3089 | ||||
3090 | if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && rtp_session->rtcp_sock_input && rtp_session->rtcp_sock_input != rtp_session->sock_input) { | |||
3091 | switch_socket_sendto(rtp_session->rtcp_sock_input, rtp_session->rtcp_local_addr, 0, (void *) &o, &len); | |||
3092 | } | |||
3093 | } | |||
3094 | ||||
3095 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_udptl_mode(switch_rtp_t *rtp_session) | |||
3096 | { | |||
3097 | switch_socket_t *sock; | |||
3098 | ||||
3099 | if (!switch_rtp_ready(rtp_session)) { | |||
3100 | return SWITCH_STATUS_FALSE; | |||
3101 | } | |||
3102 | ||||
3103 | if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_PROXY_MEDIA)) { | |||
3104 | ping_socket(rtp_session); | |||
3105 | } | |||
3106 | ||||
3107 | READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session-> reading++; | |||
3108 | WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session-> writing++; | |||
3109 | ||||
3110 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] || rtp_session->timer.timer_interface) { | |||
3111 | switch_core_timer_destroy(&rtp_session->timer); | |||
3112 | memset(&rtp_session->timer, 0, sizeof(rtp_session->timer)); | |||
3113 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER); | |||
3114 | } | |||
3115 | ||||
3116 | rtp_session->missed_count = 0; | |||
3117 | rtp_session->max_missed_packets = 0; | |||
3118 | ||||
3119 | rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] = 0; | |||
3120 | ||||
3121 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
3122 | rtp_session->rtcp_sock_input = NULL((void*)0); | |||
3123 | rtp_session->rtcp_sock_output = NULL((void*)0); | |||
3124 | } else { | |||
3125 | if (rtp_session->rtcp_sock_input && rtp_session->rtcp_sock_input != rtp_session->sock_input) { | |||
3126 | ping_socket(rtp_session); | |||
3127 | switch_socket_shutdown(rtp_session->rtcp_sock_input, SWITCH_SHUTDOWN_READWRITE); | |||
3128 | } | |||
3129 | ||||
3130 | if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input && | |||
3131 | rtp_session->rtcp_sock_output != rtp_session->sock_input) { | |||
3132 | switch_socket_shutdown(rtp_session->rtcp_sock_output, SWITCH_SHUTDOWN_READWRITE); | |||
3133 | } | |||
3134 | ||||
3135 | if ((sock = rtp_session->rtcp_sock_input)) { | |||
3136 | rtp_session->rtcp_sock_input = NULL((void*)0); | |||
3137 | switch_socket_close(sock); | |||
3138 | ||||
3139 | if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != sock) { | |||
3140 | sock = rtp_session->rtcp_sock_output; | |||
3141 | rtp_session->rtcp_sock_output = NULL((void*)0); | |||
3142 | switch_socket_close(sock); | |||
3143 | } | |||
3144 | } | |||
3145 | } | |||
3146 | ||||
3147 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_UDPTL); | |||
3148 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_PROXY_MEDIA); | |||
3149 | switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, FALSE0); | |||
3150 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK); | |||
3151 | ||||
3152 | WRITE_DEC(rtp_session)rtp_session->writing--; switch_mutex_unlock(rtp_session-> write_mutex); | |||
3153 | READ_DEC(rtp_session)rtp_session->reading--; switch_mutex_unlock(rtp_session-> read_mutex); | |||
3154 | ||||
3155 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_STICKY_FLUSH); | |||
3156 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH); | |||
3157 | ||||
3158 | switch_rtp_break(rtp_session); | |||
3159 | ||||
3160 | return SWITCH_STATUS_SUCCESS; | |||
3161 | ||||
3162 | } | |||
3163 | ||||
3164 | ||||
3165 | SWITCH_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, | |||
3166 | switch_bool_t change_adv_addr, const char **err) | |||
3167 | { | |||
3168 | switch_sockaddr_t *remote_addr; | |||
3169 | switch_status_t status = SWITCH_STATUS_SUCCESS; | |||
3170 | *err = "Success"; | |||
3171 | ||||
3172 | if (switch_sockaddr_info_get(&remote_addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !remote_addr) { | |||
3173 | *err = "Remote Address Error!"; | |||
3174 | return SWITCH_STATUS_FALSE; | |||
3175 | } | |||
3176 | ||||
3177 | ||||
3178 | switch_mutex_lock(rtp_session->write_mutex); | |||
3179 | ||||
3180 | rtp_session->remote_addr = remote_addr; | |||
3181 | ||||
3182 | if (change_adv_addr) { | |||
3183 | 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__, 3183); | |||
3184 | rtp_session->remote_port = port; | |||
3185 | } | |||
3186 | ||||
3187 | 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__, 3187); | |||
3188 | rtp_session->eff_remote_port = port; | |||
3189 | ||||
3190 | if (rtp_session->sock_input && switch_sockaddr_get_family(rtp_session->remote_addr) == switch_sockaddr_get_family(rtp_session->local_addr)) { | |||
3191 | rtp_session->sock_output = rtp_session->sock_input; | |||
3192 | } else { | |||
3193 | if (rtp_session->sock_output && rtp_session->sock_output != rtp_session->sock_input) { | |||
3194 | switch_socket_close(rtp_session->sock_output); | |||
3195 | } | |||
3196 | if ((status = switch_socket_create(&rtp_session->sock_output, | |||
3197 | switch_sockaddr_get_family(rtp_session->remote_addr), | |||
3198 | SOCK_DGRAMSOCK_DGRAM, 0, rtp_session->pool)) != SWITCH_STATUS_SUCCESS) { | |||
3199 | ||||
3200 | *err = "Socket Error!"; | |||
3201 | } | |||
3202 | } | |||
3203 | ||||
3204 | if (rtp_session->dtls) { | |||
3205 | rtp_session->dtls->sock_output = rtp_session->sock_output; | |||
3206 | ||||
3207 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
3208 | switch_sockaddr_info_get(&rtp_session->dtls->remote_addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool); | |||
3209 | } | |||
3210 | } | |||
3211 | ||||
3212 | ||||
3213 | if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
3214 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
3215 | rtp_session->rtcp_remote_addr = rtp_session->remote_addr; | |||
3216 | rtp_session->rtcp_sock_output = rtp_session->sock_output; | |||
3217 | }/* else { | |||
3218 | if (remote_rtcp_port) { | |||
3219 | rtp_session->remote_rtcp_port = remote_rtcp_port; | |||
3220 | } else { | |||
3221 | rtp_session->remote_rtcp_port = rtp_session->eff_remote_port + 1; | |||
3222 | } | |||
3223 | status = enable_remote_rtcp_socket(rtp_session, err); | |||
3224 | ||||
3225 | if (rtp_session->rtcp_dtls) { | |||
3226 | //switch_sockaddr_info_get(&rtp_session->rtcp_dtls->remote_addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool); | |||
3227 | rtp_session->rtcp_dtls->remote_addr = rtp_session->rtcp_remote_addr; | |||
3228 | rtp_session->rtcp_dtls->sock_output = rtp_session->rtcp_sock_output; | |||
3229 | } | |||
3230 | }*/ | |||
3231 | } | |||
3232 | ||||
3233 | switch_mutex_unlock(rtp_session->write_mutex); | |||
3234 | ||||
3235 | return status; | |||
3236 | } | |||
3237 | ||||
3238 | ||||
3239 | static const char *dtls_state_names_t[] = {"OFF", "HANDSHAKE", "SETUP", "READY", "FAIL", "INVALID"}; | |||
3240 | static const char *dtls_state_names(dtls_state_t s) | |||
3241 | { | |||
3242 | if (s > DS_INVALID) { | |||
3243 | s = DS_INVALID; | |||
3244 | } | |||
3245 | ||||
3246 | return dtls_state_names_t[s]; | |||
3247 | } | |||
3248 | ||||
3249 | #define dtls_set_state(_dtls, _state)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c" , (const char *)__func__, 3249, (const char*)(rtp_session-> session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n" , rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (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__, 3249, (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_TEXT] ? "text" : (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 | |||
3250 | ||||
3251 | #define cr_keylen16 16 | |||
3252 | #define cr_saltlen14 14 | |||
3253 | #define cr_kslen30 30 | |||
3254 | ||||
3255 | static int dtls_state_setup(switch_rtp_t *rtp_session, switch_dtls_t *dtls) | |||
3256 | { | |||
3257 | X509 *cert; | |||
3258 | switch_secure_settings_t ssec; /* Used just to wrap over params in a call to switch_rtp_add_crypto_key. */ | |||
3259 | int r = 0; | |||
3260 | ||||
3261 | uint8_t raw_key_data[cr_kslen30 * 2]; | |||
3262 | unsigned char local_key_buf[cr_kslen30]; | |||
3263 | unsigned char remote_key_buf[cr_kslen30]; | |||
3264 | ||||
3265 | memset(&ssec, 0, sizeof(ssec)); | |||
3266 | memset(&raw_key_data, 0, cr_kslen30 * 2 * sizeof(uint8_t)); | |||
3267 | memset(&local_key_buf, 0, cr_kslen30 * sizeof(unsigned char)); | |||
3268 | memset(&remote_key_buf, 0, cr_kslen30 * sizeof(unsigned char)); | |||
3269 | ||||
3270 | if ((dtls->type & DTLS_TYPE_SERVER)) { | |||
3271 | r = 1; | |||
3272 | } else if ((cert = SSL_get_peer_certificate(dtls->ssl))) { | |||
3273 | switch_core_cert_extract_fingerprint(cert, dtls->remote_fp); | |||
3274 | r = switch_core_cert_verify(dtls->remote_fp); | |||
3275 | X509_free(cert); | |||
3276 | } | |||
3277 | ||||
3278 | if (!r) { | |||
3279 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3279, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s Fingerprint Verification Failed!\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
3280 | dtls_set_state(dtls, DS_FAIL)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c" , (const char *)__func__, 3280, (const char*)(rtp_session-> session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n" , rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (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; | |||
3281 | return -1; | |||
3282 | } else { | |||
3283 | unsigned char *local_key, *remote_key, *local_salt, *remote_salt; | |||
3284 | ||||
3285 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3285, (const char*)(rtp_session->session), SWITCH_LOG_INFO, "%s Fingerprint Verified.\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
3286 | ||||
3287 | #ifdef HAVE_OPENSSL_DTLS_SRTP1 | |||
3288 | if (!SSL_export_keying_material(dtls->ssl, raw_key_data, sizeof(raw_key_data), "EXTRACTOR-dtls_srtp", 19, NULL((void*)0), 0, 0)) { | |||
3289 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3289, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s Key material export failure\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
3290 | dtls_set_state(dtls, DS_FAIL)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c" , (const char *)__func__, 3290, (const char*)(rtp_session-> session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n" , rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (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; | |||
3291 | return -1; | |||
3292 | } | |||
3293 | #else | |||
3294 | return -1; | |||
3295 | #endif | |||
3296 | ||||
3297 | if ((dtls->type & DTLS_TYPE_CLIENT)) { | |||
3298 | local_key = raw_key_data; | |||
3299 | remote_key = local_key + cr_keylen16; | |||
3300 | local_salt = remote_key + cr_keylen16; | |||
3301 | remote_salt = local_salt + cr_saltlen14; | |||
3302 | ||||
3303 | } else { | |||
3304 | remote_key = raw_key_data; | |||
3305 | local_key = remote_key + cr_keylen16; | |||
3306 | remote_salt = local_key + cr_keylen16; | |||
3307 | local_salt = remote_salt + cr_saltlen14; | |||
3308 | } | |||
3309 | ||||
3310 | memcpy(ssec.local_raw_key, local_key, cr_keylen16); | |||
3311 | memcpy(ssec.local_raw_key + cr_keylen16, local_salt, cr_saltlen14); | |||
3312 | ||||
3313 | memcpy(ssec.remote_raw_key, remote_key, cr_keylen16); | |||
3314 | memcpy(ssec.remote_raw_key + cr_keylen16, remote_salt, cr_saltlen14); | |||
3315 | ||||
3316 | ssec.crypto_type = AES_CM_128_HMAC_SHA1_80; | |||
3317 | ||||
3318 | if (dtls == rtp_session->rtcp_dtls && rtp_session->rtcp_dtls != rtp_session->dtls) { | |||
3319 | switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_SEND_RTCP, 0, &ssec); | |||
3320 | switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_RECV_RTCP, 0, &ssec); | |||
3321 | } else { | |||
3322 | switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_SEND, 0, &ssec); | |||
3323 | switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_RECV, 0, &ssec); | |||
3324 | } | |||
3325 | } | |||
3326 | ||||
3327 | dtls_set_state(dtls, DS_READY)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c" , (const char *)__func__, 3327, (const char*)(rtp_session-> session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n" , rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (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; | |||
3328 | ||||
3329 | return 0; | |||
3330 | } | |||
3331 | ||||
3332 | static int dtls_state_ready(switch_rtp_t *rtp_session, switch_dtls_t *dtls) | |||
3333 | { | |||
3334 | ||||
3335 | if (dtls->new_state) { | |||
3336 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
3337 | switch_core_session_t *other_session; | |||
3338 | ||||
3339 | 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__ , 3339) == SWITCH_STATUS_SUCCESS) { | |||
3340 | switch_core_session_request_video_refresh(other_session)_switch_core_session_request_video_refresh(other_session, 0, "src/switch_rtp.c" , (const char *)__func__, 3340); | |||
3341 | switch_core_session_rwunlock(other_session); | |||
3342 | } | |||
3343 | } | |||
3344 | dtls->new_state = 0; | |||
3345 | } | |||
3346 | return 0; | |||
3347 | } | |||
3348 | ||||
3349 | static int dtls_state_fail(switch_rtp_t *rtp_session, switch_dtls_t *dtls) | |||
3350 | { | |||
3351 | if (rtp_session->session) { | |||
3352 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
3353 | switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER)switch_channel_perform_hangup(channel, "src/switch_rtp.c", (const char *)__func__, 3353, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER ); | |||
3354 | } | |||
3355 | ||||
3356 | return -1; | |||
3357 | } | |||
3358 | ||||
3359 | ||||
3360 | static int dtls_state_handshake(switch_rtp_t *rtp_session, switch_dtls_t *dtls) | |||
3361 | { | |||
3362 | int ret; | |||
3363 | ||||
3364 | if ((ret = SSL_do_handshake(dtls->ssl)) != 1){ | |||
3365 | switch((ret = SSL_get_error(dtls->ssl, ret))){ | |||
3366 | case SSL_ERROR_WANT_READ2: | |||
3367 | case SSL_ERROR_WANT_WRITE3: | |||
3368 | case SSL_ERROR_NONE0: | |||
3369 | break; | |||
3370 | default: | |||
3371 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3371, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, "%s Handshake failure %d. This may happen when you use legacy DTLS v1.0 (legacyDTLS channel var is set) but endpoint requires DTLS v1.2.\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), ret); | |||
3372 | dtls_set_state(dtls, DS_FAIL)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c" , (const char *)__func__, 3372, (const char*)(rtp_session-> session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n" , rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (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; | |||
3373 | return -1; | |||
3374 | } | |||
3375 | } | |||
3376 | ||||
3377 | if (SSL_is_init_finished(dtls->ssl)) { | |||
3378 | dtls_set_state(dtls, DS_SETUP)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c" , (const char *)__func__, 3378, (const char*)(rtp_session-> session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n" , rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (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; | |||
3379 | } | |||
3380 | ||||
3381 | return 0; | |||
3382 | } | |||
3383 | ||||
3384 | static void free_dtls(switch_dtls_t **dtlsp) | |||
3385 | { | |||
3386 | switch_dtls_t *dtls; | |||
3387 | ||||
3388 | if (!dtlsp) { | |||
3389 | return; | |||
3390 | } | |||
3391 | ||||
3392 | dtls = *dtlsp; | |||
3393 | *dtlsp = NULL((void*)0); | |||
3394 | ||||
3395 | if (dtls->ssl) { | |||
3396 | SSL_free(dtls->ssl); | |||
3397 | } | |||
3398 | ||||
3399 | if (dtls->ssl_ctx) { | |||
3400 | SSL_CTX_free(dtls->ssl_ctx); | |||
3401 | } | |||
3402 | } | |||
3403 | ||||
3404 | static int do_dtls(switch_rtp_t *rtp_session, switch_dtls_t *dtls) | |||
3405 | { | |||
3406 | int r = 0, ret = 0, len; | |||
3407 | switch_size_t bytes; | |||
3408 | unsigned char buf[MAX_DTLS_MTU4096] = ""; | |||
3409 | int ready = rtp_session->ice.ice_user ? (rtp_session->ice.rready && rtp_session->ice.ready) : 1; | |||
3410 | int pending; | |||
3411 | ||||
3412 | if (!dtls->bytes && !ready) { | |||
3413 | return 0; | |||
3414 | } | |||
3415 | ||||
3416 | if (dtls->bytes > 0 && dtls->data) { | |||
3417 | ret = BIO_write(dtls->read_bio, dtls->data, (int)dtls->bytes); | |||
3418 | if (ret <= 0) { | |||
3419 | ret = SSL_get_error(dtls->ssl, ret); | |||
3420 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3420, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet decode err: SSL err %d\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), ret); | |||
3421 | } else if (ret != (int)dtls->bytes) { | |||
3422 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3422, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet decode err: read %d bytes instead of %d\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), ret, (int)dtls->bytes); | |||
3423 | } | |||
3424 | } | |||
3425 | ||||
3426 | if (dtls_states[dtls->state]) { | |||
3427 | r = dtls_states[dtls->state](rtp_session, dtls); | |||
3428 | } | |||
3429 | ||||
3430 | while ((pending = BIO_ctrl_pending(dtls->filter_bio)) > 0) { | |||
3431 | switch_assert(pending <= sizeof(buf))((pending <= sizeof(buf)) ? (void) (0) : __assert_fail ("pending <= sizeof(buf)" , "src/switch_rtp.c", 3431, __extension__ __PRETTY_FUNCTION__ )); | |||
3432 | ||||
3433 | len = BIO_read(dtls->write_bio, buf, pending); | |||
3434 | if (len > 0) { | |||
3435 | bytes = len; | |||
3436 | ret = switch_socket_sendto(dtls->sock_output, dtls->remote_addr, 0, (void *)buf, &bytes); | |||
3437 | ||||
3438 | if (ret != SWITCH_STATUS_SUCCESS) { | |||
3439 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3439, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet not written to socket: %d\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), ret); | |||
3440 | } else if (bytes != len) { | |||
3441 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3441, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet write err: written %d bytes instead of %d\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), (int)bytes, len); | |||
3442 | } | |||
3443 | } else { | |||
3444 | ret = SSL_get_error(dtls->ssl, len); | |||
3445 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3445, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet encode err: SSL err %d\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), ret); | |||
3446 | } | |||
3447 | } | |||
3448 | ||||
3449 | return r; | |||
3450 | } | |||
3451 | ||||
3452 | #if VERIFY | |||
3453 | static int cb_verify_peer(int preverify_ok, X509_STORE_CTX *ctx) | |||
3454 | { | |||
3455 | SSL *ssl = NULL((void*)0); | |||
3456 | switch_dtls_t *dtls; | |||
3457 | X509 *cert; | |||
3458 | int r = 0; | |||
3459 | ||||
3460 | ssl = X509_STORE_CTX_get_app_data(ctx)X509_STORE_CTX_get_ex_data(ctx,0); | |||
3461 | dtls = (switch_dtls_t *) SSL_get_app_data(ssl)(SSL_get_ex_data(ssl,0)); | |||
3462 | ||||
3463 | if (!(ssl && dtls)) { | |||
3464 | return 0; | |||
3465 | } | |||
3466 | ||||
3467 | if ((cert = SSL_get_peer_certificate(dtls->ssl))) { | |||
3468 | switch_core_cert_extract_fingerprint(cert, dtls->remote_fp); | |||
3469 | ||||
3470 | r = switch_core_cert_verify(dtls->remote_fp); | |||
3471 | ||||
3472 | X509_free(cert); | |||
3473 | } else { | |||
3474 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(dtls->rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3474, (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_TEXT] ? "text" : (dtls->rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
3475 | } | |||
3476 | ||||
3477 | return r; | |||
3478 | } | |||
3479 | #endif | |||
3480 | ||||
3481 | ||||
3482 | //////////// | |||
3483 | ||||
3484 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10100000L | |||
3485 | static BIO_METHOD dtls_bio_filter_methods; | |||
3486 | #else | |||
3487 | static BIO_METHOD *dtls_bio_filter_methods; | |||
3488 | #endif | |||
3489 | ||||
3490 | BIO_METHOD *BIO_dtls_filter(void) { | |||
3491 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10100000L | |||
3492 | return(&dtls_bio_filter_methods); | |||
3493 | #else | |||
3494 | return(dtls_bio_filter_methods); | |||
3495 | #endif | |||
3496 | } | |||
3497 | ||||
3498 | typedef struct packet_list_s { | |||
3499 | //void *packet; | |||
3500 | int size; | |||
3501 | struct packet_list_s *next; | |||
3502 | } packet_list_t; | |||
3503 | ||||
3504 | /* Helper struct to keep the filter state */ | |||
3505 | typedef struct dtls_bio_filter { | |||
3506 | packet_list_t *packets; | |||
3507 | packet_list_t *unused; | |||
3508 | packet_list_t *tail; | |||
3509 | switch_mutex_t *mutex; | |||
3510 | switch_memory_pool_t *pool; | |||
3511 | long mtu; | |||
3512 | } dtls_bio_filter; | |||
3513 | ||||
3514 | ||||
3515 | static int dtls_bio_filter_new(BIO *bio) { | |||
3516 | /* Create a filter state struct */ | |||
3517 | dtls_bio_filter *filter; | |||
3518 | switch_memory_pool_t *pool; | |||
3519 | ||||
3520 | switch_core_new_memory_pool(&pool)switch_core_perform_new_memory_pool(&pool, "src/switch_rtp.c" , (const char *)__func__, 3520); | |||
3521 | filter = switch_core_alloc(pool, sizeof(*filter))switch_core_perform_alloc(pool, sizeof(*filter), "src/switch_rtp.c" , (const char *)__func__, 3521); | |||
3522 | filter->pool = pool; | |||
3523 | ||||
3524 | filter->packets = NULL((void*)0); | |||
3525 | switch_mutex_init(&filter->mutex, SWITCH_MUTEX_NESTED0x1, filter->pool); | |||
3526 | ||||
3527 | /* Set the BIO as initialized */ | |||
3528 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10100000L | |||
3529 | bio->init = 1; | |||
3530 | bio->ptr = filter; | |||
3531 | bio->flags = 0; | |||
3532 | #else | |||
3533 | BIO_set_init(bio, 1); | |||
3534 | BIO_set_data(bio, filter); | |||
3535 | BIO_clear_flags(bio, ~0); | |||
3536 | #endif | |||
3537 | ||||
3538 | return 1; | |||
3539 | } | |||
3540 | ||||
3541 | static int dtls_bio_filter_free(BIO *bio) { | |||
3542 | dtls_bio_filter *filter; | |||
3543 | ||||
3544 | if (bio == NULL((void*)0)) { | |||
3545 | return 0; | |||
3546 | } | |||
3547 | ||||
3548 | /* Get rid of the filter state */ | |||
3549 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10100000L | |||
3550 | filter = (dtls_bio_filter *)bio->ptr; | |||
3551 | #else | |||
3552 | filter = (dtls_bio_filter *)BIO_get_data(bio); | |||
3553 | #endif | |||
3554 | ||||
3555 | if (filter != NULL((void*)0)) { | |||
3556 | switch_memory_pool_t *pool = filter->pool; | |||
3557 | switch_core_destroy_memory_pool(&pool)switch_core_perform_destroy_memory_pool(&pool, "src/switch_rtp.c" , (const char *)__func__, 3557); | |||
3558 | pool = NULL((void*)0); | |||
3559 | filter = NULL((void*)0); | |||
3560 | } | |||
3561 | ||||
3562 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10100000L | |||
3563 | bio->ptr = NULL((void*)0); | |||
3564 | bio->init = 0; | |||
3565 | bio->flags = 0; | |||
3566 | #else | |||
3567 | BIO_set_init(bio, 0); | |||
3568 | BIO_set_data(bio, NULL((void*)0)); | |||
3569 | BIO_clear_flags(bio, ~0); | |||
3570 | #endif | |||
3571 | return 1; | |||
3572 | } | |||
3573 | ||||
3574 | static int dtls_bio_filter_write(BIO *bio, const char *in, int inl) { | |||
3575 | long ret; | |||
3576 | dtls_bio_filter *filter; | |||
3577 | ||||
3578 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 3578, ((void*)0), SWITCH_LOG_DEBUG1, "dtls_bio_filter_write: %p, %d\n", (void *)in, inl); | |||
3579 | /* Forward data to the write BIO */ | |||
3580 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10100000L | |||
3581 | ret = BIO_write(bio->next_bio, in, inl); | |||
3582 | #else | |||
3583 | ret = BIO_write(BIO_next(bio), in, inl); | |||
3584 | #endif | |||
3585 | ||||
3586 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 3586, ((void*)0), SWITCH_LOG_DEBUG1, " -- %ld\n", ret); | |||
3587 | ||||
3588 | /* Keep track of the packet, as we'll advertize them one by one after a pending check */ | |||
3589 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10100000L | |||
3590 | filter = (dtls_bio_filter *)bio->ptr; | |||
3591 | #else | |||
3592 | filter = (dtls_bio_filter *)BIO_get_data(bio); | |||
3593 | #endif | |||
3594 | ||||
3595 | if (filter != NULL((void*)0)) { | |||
3596 | packet_list_t *node; | |||
3597 | ||||
3598 | switch_mutex_lock(filter->mutex); | |||
3599 | if (filter->unused) { | |||
3600 | node = filter->unused; | |||
3601 | node->next = NULL((void*)0); | |||
3602 | filter->unused = filter->unused->next; | |||
3603 | } else { | |||
3604 | node = switch_core_alloc(filter->pool, sizeof(*node))switch_core_perform_alloc(filter->pool, sizeof(*node), "src/switch_rtp.c" , (const char *)__func__, 3604); | |||
3605 | } | |||
3606 | ||||
3607 | node->size = ret; | |||
3608 | ||||
3609 | if (filter->tail) { | |||
3610 | filter->tail->next = node; | |||
3611 | } else { | |||
3612 | filter->packets = node; | |||
3613 | } | |||
3614 | ||||
3615 | filter->tail = node; | |||
3616 | ||||
3617 | switch_mutex_unlock(filter->mutex); | |||
3618 | //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "New list length: %d\n", g_list_length(filter->packets)); | |||
3619 | } | |||
3620 | return ret; | |||
3621 | } | |||
3622 | ||||
3623 | static long dtls_bio_filter_ctrl(BIO *bio, int cmd, long num, void *ptr) { | |||
3624 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10100000L | |||
3625 | dtls_bio_filter *filter = (dtls_bio_filter *)bio->ptr; | |||
3626 | #else | |||
3627 | dtls_bio_filter *filter = (dtls_bio_filter *)BIO_get_data(bio); | |||
3628 | #endif | |||
3629 | ||||
3630 | switch(cmd) { | |||
3631 | case BIO_CTRL_DGRAM_GET_FALLBACK_MTU47: | |||
3632 | return 1200; | |||
3633 | case BIO_CTRL_DGRAM_SET_MTU42: | |||
3634 | filter->mtu = num; | |||
3635 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 3635, ((void*)0), SWITCH_LOG_DEBUG1, "Setting MTU: %ld\n", filter->mtu); | |||
3636 | return num; | |||
3637 | case BIO_CTRL_FLUSH11: | |||
3638 | return 1; | |||
3639 | case BIO_CTRL_DGRAM_QUERY_MTU40: | |||
3640 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 3640, ((void*)0), SWITCH_LOG_DEBUG1, "Advertizing MTU: %ld\n", filter->mtu); | |||
3641 | return filter->mtu; | |||
3642 | case BIO_CTRL_WPENDING13: | |||
3643 | return 0L; | |||
3644 | case BIO_CTRL_PENDING10: { | |||
3645 | int pending = 0; | |||
3646 | packet_list_t *top; | |||
3647 | ||||
3648 | if (filter == NULL((void*)0)) { | |||
3649 | return 0; | |||
3650 | } | |||
3651 | ||||
3652 | switch_mutex_lock(filter->mutex); | |||
3653 | if ((top = filter->packets)) { | |||
3654 | filter->packets = filter->packets->next; | |||
3655 | ||||
3656 | if (top == filter->tail || !filter->packets) { | |||
3657 | filter->tail = NULL((void*)0); | |||
3658 | } | |||
3659 | ||||
3660 | pending = top->size; | |||
3661 | top->next = filter->unused; | |||
3662 | filter->unused = top; | |||
3663 | } | |||
3664 | switch_mutex_unlock(filter->mutex); | |||
3665 | ||||
3666 | return pending; | |||
3667 | } | |||
3668 | default: | |||
3669 | //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dtls_bio_filter_ctrl: %d\n", cmd); | |||
3670 | break; | |||
3671 | } | |||
3672 | return 0; | |||
3673 | } | |||
3674 | ||||
3675 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10100000L | |||
3676 | static BIO_METHOD dtls_bio_filter_methods = { | |||
3677 | BIO_TYPE_FILTER0x0200, | |||
3678 | "DTLS filter", | |||
3679 | dtls_bio_filter_write, | |||
3680 | NULL((void*)0), | |||
3681 | NULL((void*)0), | |||
3682 | NULL((void*)0), | |||
3683 | dtls_bio_filter_ctrl, | |||
3684 | dtls_bio_filter_new, | |||
3685 | dtls_bio_filter_free, | |||
3686 | NULL((void*)0) | |||
3687 | }; | |||
3688 | #else | |||
3689 | static BIO_METHOD *dtls_bio_filter_methods = NULL((void*)0); | |||
3690 | #endif | |||
3691 | ||||
3692 | static void switch_rtp_dtls_init() { | |||
3693 | #if OPENSSL_VERSION_NUMBER0x1010104fL >= 0x10100000L | |||
3694 | dtls_bio_filter_methods = BIO_meth_new(BIO_TYPE_FILTER0x0200 | BIO_get_new_index(), "DTLS filter"); | |||
3695 | BIO_meth_set_write(dtls_bio_filter_methods, dtls_bio_filter_write); | |||
3696 | BIO_meth_set_ctrl(dtls_bio_filter_methods, dtls_bio_filter_ctrl); | |||
3697 | BIO_meth_set_create(dtls_bio_filter_methods, dtls_bio_filter_new); | |||
3698 | BIO_meth_set_destroy(dtls_bio_filter_methods, dtls_bio_filter_free); | |||
3699 | #endif | |||
3700 | } | |||
3701 | ||||
3702 | static void switch_rtp_dtls_destroy() { | |||
3703 | #if OPENSSL_VERSION_NUMBER0x1010104fL >= 0x10100000L | |||
3704 | if (dtls_bio_filter_methods) { | |||
3705 | BIO_meth_free(dtls_bio_filter_methods); | |||
3706 | dtls_bio_filter_methods = NULL((void*)0); | |||
3707 | } | |||
3708 | #endif | |||
3709 | } | |||
3710 | ||||
3711 | /////////// | |||
3712 | ||||
3713 | ||||
3714 | ||||
3715 | SWITCH_DECLARE(int)__attribute__((visibility("default"))) int switch_rtp_has_dtls(void) { | |||
3716 | #ifdef HAVE_OPENSSL_DTLS_SRTP1 | |||
3717 | return 1; | |||
3718 | #else | |||
3719 | return 0; | |||
3720 | #endif | |||
3721 | } | |||
3722 | ||||
3723 | SWITCH_DECLARE(dtls_state_t)__attribute__((visibility("default"))) dtls_state_t switch_rtp_dtls_state(switch_rtp_t *rtp_session, dtls_type_t type) | |||
3724 | { | |||
3725 | dtls_state_t s = DS_OFF; | |||
3726 | ||||
3727 | switch_mutex_lock(rtp_session->ice_mutex); | |||
3728 | ||||
3729 | if (!rtp_session || (!rtp_session->dtls && !rtp_session->rtcp_dtls)) { | |||
3730 | s = DS_OFF; | |||
3731 | goto done; | |||
3732 | } | |||
3733 | ||||
3734 | if ((type == DTLS_TYPE_RTP) && rtp_session->dtls) { | |||
3735 | s = rtp_session->dtls->state; | |||
3736 | goto done; | |||
3737 | } | |||
3738 | ||||
3739 | if ((type == DTLS_TYPE_RTCP) && rtp_session->rtcp_dtls) { | |||
3740 | s = rtp_session->rtcp_dtls->state; | |||
3741 | } | |||
3742 | ||||
3743 | done: | |||
3744 | ||||
3745 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
3746 | ||||
3747 | return s; | |||
3748 | } | |||
3749 | ||||
3750 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_del_dtls(switch_rtp_t *rtp_session, dtls_type_t type) | |||
3751 | { | |||
3752 | switch_status_t status = SWITCH_STATUS_SUCCESS; | |||
3753 | ||||
3754 | switch_mutex_lock(rtp_session->ice_mutex); | |||
3755 | ||||
3756 | if (!rtp_session || (!rtp_session->dtls && !rtp_session->rtcp_dtls)) { | |||
3757 | switch_goto_status(SWITCH_STATUS_FALSE, done)status = SWITCH_STATUS_FALSE; goto done; | |||
3758 | } | |||
3759 | ||||
3760 | if ((type & DTLS_TYPE_RTP)) { | |||
3761 | if (rtp_session->dtls && rtp_session->dtls == rtp_session->rtcp_dtls) { | |||
3762 | rtp_session->rtcp_dtls = NULL((void*)0); | |||
3763 | } | |||
3764 | ||||
3765 | if (rtp_session->dtls) { | |||
3766 | free_dtls(&rtp_session->dtls); | |||
3767 | } | |||
3768 | ||||
3769 | if (rtp_session->jb) { | |||
3770 | switch_jb_reset(rtp_session->jb); | |||
3771 | } | |||
3772 | ||||
3773 | if (rtp_session->vb) { | |||
3774 | switch_jb_reset(rtp_session->vb); | |||
3775 | } | |||
3776 | ||||
3777 | if (rtp_session->vbw) { | |||
3778 | switch_jb_reset(rtp_session->vbw); | |||
3779 | } | |||
3780 | ||||
3781 | } | |||
3782 | ||||
3783 | if ((type & DTLS_TYPE_RTCP) && rtp_session->rtcp_dtls) { | |||
3784 | free_dtls(&rtp_session->rtcp_dtls); | |||
3785 | } | |||
3786 | ||||
3787 | ||||
3788 | #ifdef ENABLE_SRTP1 | |||
3789 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) { | |||
3790 | int x; | |||
3791 | ||||
3792 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0; | |||
3793 | ||||
3794 | for(x = 0; x < 2; x++) { | |||
3795 | if (rtp_session->send_ctx[x]) { | |||
3796 | srtp_dealloc(rtp_session->send_ctx[x]); | |||
3797 | rtp_session->send_ctx[x] = NULL((void*)0); | |||
3798 | } | |||
3799 | } | |||
3800 | } | |||
3801 | ||||
3802 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV]) { | |||
3803 | int x; | |||
3804 | ||||
3805 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0; | |||
3806 | ||||
3807 | for (x = 0; x < 2; x++) { | |||
3808 | if (rtp_session->recv_ctx[x]) { | |||
3809 | srtp_dealloc(rtp_session->recv_ctx[x]); | |||
3810 | rtp_session->recv_ctx[x] = NULL((void*)0); | |||
3811 | } | |||
3812 | } | |||
3813 | } | |||
3814 | #endif | |||
3815 | ||||
3816 | done: | |||
3817 | ||||
3818 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
3819 | ||||
3820 | return status; | |||
3821 | } | |||
3822 | ||||
3823 | SWITCH_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, uint8_t want_DTLSv1_2) | |||
3824 | { | |||
3825 | switch_dtls_t *dtls; | |||
3826 | const char *var; | |||
3827 | int ret; | |||
3828 | const char *kind = ""; | |||
3829 | unsigned long ssl_method_error = 0; | |||
3830 | unsigned long ssl_ctx_error = 0; | |||
3831 | const SSL_METHOD *ssl_method; | |||
3832 | SSL_CTX *ssl_ctx; | |||
3833 | BIO *bio; | |||
3834 | DH *dh; | |||
3835 | switch_status_t status = SWITCH_STATUS_SUCCESS; | |||
3836 | #ifndef OPENSSL_NO_EC | |||
3837 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10002000L | |||
3838 | EC_KEY* ecdh; | |||
3839 | #endif | |||
3840 | #endif | |||
3841 | ||||
3842 | #ifndef HAVE_OPENSSL_DTLS_SRTP1 | |||
3843 | return SWITCH_STATUS_FALSE; | |||
3844 | #endif | |||
3845 | ||||
3846 | if (!switch_rtp_ready(rtp_session)) { | |||
3847 | return SWITCH_STATUS_FALSE; | |||
3848 | } | |||
3849 | ||||
3850 | switch_mutex_lock(rtp_session->ice_mutex); | |||
3851 | ||||
3852 | if (!((type & DTLS_TYPE_RTP) || (type & DTLS_TYPE_RTCP)) || !((type & DTLS_TYPE_CLIENT) || (type & DTLS_TYPE_SERVER))) { | |||
3853 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3853, (const char*)(rtp_session->session), SWITCH_LOG_CRIT, "INVALID TYPE!\n"); | |||
3854 | } | |||
3855 | ||||
3856 | switch_rtp_del_dtls(rtp_session, type); | |||
3857 | ||||
3858 | if ((type & DTLS_TYPE_RTP) && (type & DTLS_TYPE_RTCP)) { | |||
3859 | kind = "RTP/RTCP"; | |||
3860 | } else if ((type & DTLS_TYPE_RTP)) { | |||
3861 | kind = "RTP"; | |||
3862 | } else { | |||
3863 | kind = "RTCP"; | |||
3864 | } | |||
3865 | ||||
3866 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3866, (const char*)(rtp_session->session), SWITCH_LOG_INFO, | |||
3867 | "Activate %s %s DTLS %s\n", kind, rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), (type & DTLS_TYPE_SERVER) ? "server" : "client"); | |||
3868 | ||||
3869 | if (((type & DTLS_TYPE_RTP) && rtp_session->dtls) || ((type & DTLS_TYPE_RTCP) && rtp_session->rtcp_dtls)) { | |||
3870 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3870, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, "DTLS ALREADY INIT\n"); | |||
3871 | switch_goto_status(SWITCH_STATUS_FALSE, done)status = SWITCH_STATUS_FALSE; goto done; | |||
3872 | } | |||
3873 | ||||
3874 | 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__, 3874); | |||
3875 | ||||
3876 | 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"); | |||
3877 | ||||
3878 | if (switch_file_exists(dtls->pem, rtp_session->pool) == SWITCH_STATUS_SUCCESS) { | |||
3879 | dtls->pvt = dtls->rsa = dtls->pem; | |||
3880 | } else { | |||
3881 | 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"); | |||
3882 | 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"); | |||
3883 | } | |||
3884 | ||||
3885 | dtls->ca = switch_core_sprintf(rtp_session->pool, "%s%sca-bundle.crt", SWITCH_GLOBAL_dirs.certs_dir, SWITCH_PATH_SEPARATOR"/"); | |||
3886 | ||||
3887 | #if OPENSSL_VERSION_NUMBER0x1010104fL >= 0x10100000 | |||
3888 | ssl_method = (type & DTLS_TYPE_SERVER) ? DTLS_server_method() : DTLS_client_method(); | |||
3889 | #else | |||
3890 | #ifdef HAVE_OPENSSL_DTLSv1_2_method1 | |||
3891 | ssl_method = (type & DTLS_TYPE_SERVER) ? (want_DTLSv1_2 ? DTLSv1_2_server_method() : DTLSv1_server_method()) : (want_DTLSv1_2 ? DTLSv1_2_client_method() : DTLSv1_client_method()); | |||
3892 | #else | |||
3893 | ssl_method = (type & DTLS_TYPE_SERVER) ? DTLSv1_server_method() : DTLSv1_client_method(); | |||
3894 | #endif // HAVE_OPENSSL_DTLSv1_2_method | |||
3895 | #endif | |||
3896 | ||||
3897 | if (!ssl_method) { | |||
3898 | ssl_method_error = ERR_peek_error(); | |||
3899 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3899, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s ssl_method is NULL [%lu]\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), ssl_method_error); | |||
3900 | } | |||
3901 | ||||
3902 | dtls->ssl_ctx = ssl_ctx = SSL_CTX_new(ssl_method); | |||
3903 | ||||
3904 | if (!ssl_ctx) { | |||
3905 | ssl_ctx_error = ERR_peek_error(); | |||
3906 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3906, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s SSL_CTX_new failed [%lu]\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), ssl_ctx_error); | |||
3907 | switch_channel_hangup(switch_core_session_get_channel(rtp_session->session), SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE)switch_channel_perform_hangup(switch_core_session_get_channel (rtp_session->session), "src/switch_rtp.c", (const char *) __func__, 3907, SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE); | |||
3908 | switch_goto_status(SWITCH_STATUS_FALSE, done)status = SWITCH_STATUS_FALSE; goto done; | |||
3909 | } | |||
3910 | ||||
3911 | switch_assert(dtls->ssl_ctx)((dtls->ssl_ctx) ? (void) (0) : __assert_fail ("dtls->ssl_ctx" , "src/switch_rtp.c", 3911, __extension__ __PRETTY_FUNCTION__ )); | |||
3912 | ||||
3913 | bio = BIO_new_file(dtls->pem, "r"); | |||
3914 | dh = PEM_read_bio_DHparams(bio, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
3915 | BIO_free(bio); | |||
3916 | if (dh) { | |||
3917 | SSL_CTX_set_tmp_dh(dtls->ssl_ctx, dh)SSL_CTX_ctrl(dtls->ssl_ctx,3,0,(char *)(dh)); | |||
3918 | DH_free(dh); | |||
3919 | } | |||
3920 | ||||
3921 | SSL_CTX_set_mode(dtls->ssl_ctx, SSL_MODE_AUTO_RETRY)SSL_CTX_ctrl((dtls->ssl_ctx),33,(0x00000004U),((void*)0)); | |||
3922 | ||||
3923 | //SSL_CTX_set_verify(dtls->ssl_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL); | |||
3924 | SSL_CTX_set_verify(dtls->ssl_ctx, SSL_VERIFY_NONE0x00, NULL((void*)0)); | |||
3925 | ||||
3926 | //SSL_CTX_set_cipher_list(dtls->ssl_ctx, "ECDH:!RC4:!SSLv3:RSA_WITH_AES_128_CBC_SHA"); | |||
3927 | //SSL_CTX_set_cipher_list(dtls->ssl_ctx, "ECDHE-RSA-AES256-GCM-SHA384"); | |||
3928 | SSL_CTX_set_cipher_list(dtls->ssl_ctx, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); | |||
3929 | //SSL_CTX_set_cipher_list(dtls->ssl_ctx, "SUITEB128"); | |||
3930 | SSL_CTX_set_read_ahead(dtls->ssl_ctx, 1)SSL_CTX_ctrl(dtls->ssl_ctx,41,1,((void*)0)); | |||
3931 | #ifdef HAVE_OPENSSL_DTLS_SRTP1 | |||
3932 | //SSL_CTX_set_tlsext_use_srtp(dtls->ssl_ctx, "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32"); | |||
3933 | SSL_CTX_set_tlsext_use_srtp(dtls->ssl_ctx, "SRTP_AES128_CM_SHA1_80"); | |||
3934 | #endif | |||
3935 | ||||
3936 | dtls->type = type; | |||
3937 | dtls->read_bio = BIO_new(BIO_s_mem()); | |||
3938 | switch_assert(dtls->read_bio)((dtls->read_bio) ? (void) (0) : __assert_fail ("dtls->read_bio" , "src/switch_rtp.c", 3938, __extension__ __PRETTY_FUNCTION__ )); | |||
3939 | ||||
3940 | dtls->write_bio = BIO_new(BIO_s_mem()); | |||
3941 | switch_assert(dtls->write_bio)((dtls->write_bio) ? (void) (0) : __assert_fail ("dtls->write_bio" , "src/switch_rtp.c", 3941, __extension__ __PRETTY_FUNCTION__ )); | |||
3942 | ||||
3943 | BIO_set_mem_eof_return(dtls->read_bio, -1)BIO_ctrl(dtls->read_bio,130,-1,((void*)0)); | |||
3944 | BIO_set_mem_eof_return(dtls->write_bio, -1)BIO_ctrl(dtls->write_bio,130,-1,((void*)0)); | |||
3945 | ||||
3946 | if ((ret=SSL_CTX_use_certificate_file(dtls->ssl_ctx, dtls->rsa, SSL_FILETYPE_PEM1)) != 1) { | |||
3947 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3947, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS cert err [%d]\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), SSL_get_error(dtls->ssl, ret)); | |||
3948 | switch_goto_status(SWITCH_STATUS_FALSE, done)status = SWITCH_STATUS_FALSE; goto done; | |||
3949 | } | |||
3950 | ||||
3951 | if ((ret=SSL_CTX_use_PrivateKey_file(dtls->ssl_ctx, dtls->pvt, SSL_FILETYPE_PEM1)) != 1) { | |||
3952 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3952, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS key err [%d]\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), SSL_get_error(dtls->ssl, ret)); | |||
3953 | switch_goto_status(SWITCH_STATUS_FALSE, done)status = SWITCH_STATUS_FALSE; goto done; | |||
3954 | } | |||
3955 | ||||
3956 | if (SSL_CTX_check_private_key(dtls->ssl_ctx) == 0) { | |||
3957 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3957, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS check key failed\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
3958 | switch_goto_status(SWITCH_STATUS_FALSE, done)status = SWITCH_STATUS_FALSE; goto done; | |||
3959 | } | |||
3960 | ||||
3961 | if (!zstr(dtls->ca)_zstr(dtls->ca) && switch_file_exists(dtls->ca, rtp_session->pool) == SWITCH_STATUS_SUCCESS | |||
3962 | && (ret = SSL_CTX_load_verify_locations(dtls->ssl_ctx, dtls->ca, NULL((void*)0))) != 1) { | |||
3963 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 3963, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS check chain cert failed [%d]\n", | |||
3964 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio") , | |||
3965 | SSL_get_error(dtls->ssl, ret)); | |||
3966 | switch_goto_status(SWITCH_STATUS_FALSE, done)status = SWITCH_STATUS_FALSE; goto done; | |||
3967 | } | |||
3968 | ||||
3969 | dtls->ssl = SSL_new(dtls->ssl_ctx); | |||
3970 | ||||
3971 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10100000L | |||
3972 | dtls->filter_bio = BIO_new(BIO_dtls_filter()); | |||
3973 | #else | |||
3974 | switch_assert(dtls_bio_filter_methods)((dtls_bio_filter_methods) ? (void) (0) : __assert_fail ("dtls_bio_filter_methods" , "src/switch_rtp.c", 3974, __extension__ __PRETTY_FUNCTION__ )); | |||
3975 | dtls->filter_bio = BIO_new(dtls_bio_filter_methods); | |||
3976 | #endif | |||
3977 | ||||
3978 | switch_assert(dtls->filter_bio)((dtls->filter_bio) ? (void) (0) : __assert_fail ("dtls->filter_bio" , "src/switch_rtp.c", 3978, __extension__ __PRETTY_FUNCTION__ )); | |||
3979 | ||||
3980 | BIO_push(dtls->filter_bio, dtls->write_bio); | |||
3981 | ||||
3982 | SSL_set_bio(dtls->ssl, dtls->read_bio, dtls->filter_bio); | |||
3983 | ||||
3984 | SSL_set_mode(dtls->ssl, SSL_MODE_AUTO_RETRY)SSL_ctrl((dtls->ssl),33,(0x00000004U),((void*)0)); | |||
3985 | SSL_set_read_ahead(dtls->ssl, 1); | |||
3986 | ||||
3987 | ||||
3988 | //SSL_set_verify(dtls->ssl, (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), cb_verify_peer); | |||
3989 | ||||
3990 | #ifndef OPENSSL_NO_EC | |||
3991 | #if OPENSSL_VERSION_NUMBER0x1010104fL < 0x10002000L | |||
3992 | ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1415); | |||
3993 | if (!ecdh) { | |||
3994 | switch_goto_status(SWITCH_STATUS_FALSE, done)status = SWITCH_STATUS_FALSE; goto done; | |||
3995 | } | |||
3996 | SSL_set_options(dtls->ssl, SSL_OP_SINGLE_ECDH_USE0x0); | |||
3997 | SSL_set_tmp_ecdh(dtls->ssl, ecdh)SSL_ctrl(dtls->ssl,4,0,(char *)(ecdh)); | |||
3998 | EC_KEY_free(ecdh); | |||
3999 | #elif OPENSSL_VERSION_NUMBER0x1010104fL < 0x10100000L | |||
4000 | SSL_set_ecdh_auto(dtls->ssl, 1)((1) != 0); | |||
4001 | SSL_set_options(dtls->ssl, SSL_OP_SINGLE_ECDH_USE0x0); | |||
4002 | #endif | |||
4003 | #endif | |||
4004 | ||||
4005 | SSL_set_verify(dtls->ssl, SSL_VERIFY_NONE0x00, NULL((void*)0)); | |||
4006 | SSL_set_app_data(dtls->ssl, dtls)(SSL_set_ex_data(dtls->ssl,0,(char *)(dtls))); | |||
4007 | ||||
4008 | dtls->local_fp = local_fp; | |||
4009 | dtls->remote_fp = remote_fp; | |||
4010 | dtls->rtp_session = rtp_session; | |||
4011 | dtls->mtu = 1200; | |||
4012 | ||||
4013 | if (rtp_session->session) { | |||
4014 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
4015 | if ((var = switch_channel_get_variable(channel, "rtp_dtls_mtu")switch_channel_get_variable_dup(channel, "rtp_dtls_mtu", SWITCH_TRUE , -1))) { | |||
4016 | int mtu = atoi(var); | |||
4017 | ||||
4018 | if (mtu > 0 && mtu < MAX_DTLS_MTU4096) { | |||
4019 | dtls->mtu = mtu; | |||
4020 | } | |||
4021 | ||||
4022 | } | |||
4023 | } | |||
4024 | ||||
4025 | BIO_ctrl(dtls->filter_bio, BIO_CTRL_DGRAM_SET_MTU42, dtls->mtu, NULL((void*)0)); | |||
4026 | ||||
4027 | switch_core_cert_expand_fingerprint(remote_fp, remote_fp->str); | |||
4028 | ||||
4029 | if ((type & DTLS_TYPE_RTP)) { | |||
4030 | rtp_session->dtls = dtls; | |||
4031 | dtls->sock_output = rtp_session->sock_output; | |||
4032 | dtls->remote_addr = rtp_session->remote_addr; | |||
4033 | } | |||
4034 | ||||
4035 | if ((type & DTLS_TYPE_RTCP)) { | |||
4036 | rtp_session->rtcp_dtls = dtls; | |||
4037 | if (!(type & DTLS_TYPE_RTP)) { | |||
4038 | dtls->sock_output = rtp_session->rtcp_sock_output; | |||
4039 | dtls->remote_addr = rtp_session->rtcp_remote_addr; | |||
4040 | } | |||
4041 | } | |||
4042 | ||||
4043 | if ((type & DTLS_TYPE_SERVER)) { | |||
4044 | SSL_set_accept_state(dtls->ssl); | |||
4045 | } else { | |||
4046 | SSL_set_connect_state(dtls->ssl); | |||
4047 | } | |||
4048 | ||||
4049 | dtls_set_state(dtls, DS_HANDSHAKE)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c" , (const char *)__func__, 4049, (const char*)(rtp_session-> session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n" , rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), dtls_state_names (dtls->state), dtls_state_names(DS_HANDSHAKE)); dtls->new_state = 1; dtls->last_state = dtls->state; dtls->state = DS_HANDSHAKE; | |||
4050 | ||||
4051 | rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK] = 1; | |||
4052 | switch_rtp_break(rtp_session); | |||
4053 | ||||
4054 | done: | |||
4055 | ||||
4056 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
4057 | ||||
4058 | return status; | |||
4059 | ||||
4060 | } | |||
4061 | ||||
4062 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_add_crypto_key(switch_rtp_t *rtp_session, switch_rtp_crypto_direction_t direction, uint32_t index, switch_secure_settings_t *ssec) | |||
4063 | { | |||
4064 | #ifndef ENABLE_SRTP1 | |||
4065 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4065, (const char*)(rtp_session->session), SWITCH_LOG_CRIT, "SRTP NOT SUPPORTED IN THIS BUILD!\n"); | |||
4066 | return SWITCH_STATUS_FALSE; | |||
4067 | #else | |||
4068 | ||||
4069 | switch_rtp_crypto_key_t *crypto_key; | |||
4070 | srtp_policy_t *policy; | |||
4071 | srtp_err_status_t stat; | |||
4072 | switch_status_t status = SWITCH_STATUS_SUCCESS; | |||
4073 | ||||
4074 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
4075 | switch_event_t *fsevent = NULL((void*)0); | |||
4076 | int idx = 0; | |||
4077 | const char *var; | |||
4078 | unsigned char b64_key[512] = ""; | |||
4079 | ||||
4080 | unsigned char *keysalt = NULL((void*)0); | |||
4081 | switch_size_t keysalt_len = 0; | |||
4082 | ||||
4083 | switch_crypto_key_material_t *key_material = NULL((void*)0); | |||
4084 | unsigned long *key_material_n = NULL((void*)0); | |||
4085 | srtp_master_key_t **mkis = NULL((void*)0); | |||
4086 | srtp_master_key_t *mki = NULL((void*)0); | |||
4087 | int mki_idx = 0; | |||
4088 | ||||
4089 | keysalt_len = switch_core_media_crypto_keysalt_len(ssec->crypto_type); | |||
4090 | ||||
4091 | if (direction >= SWITCH_RTP_CRYPTO_MAX || keysalt_len > SWITCH_RTP_MAX_CRYPTO_LEN64) { | |||
4092 | return SWITCH_STATUS_FALSE; | |||
4093 | } | |||
4094 | ||||
4095 | if (direction == SWITCH_RTP_CRYPTO_RECV_RTCP) { | |||
4096 | direction = SWITCH_RTP_CRYPTO_RECV; | |||
4097 | rtp_session->srtp_idx_rtcp = idx = 1; | |||
4098 | } else if (direction == SWITCH_RTP_CRYPTO_SEND_RTCP) { | |||
4099 | direction = SWITCH_RTP_CRYPTO_SEND; | |||
4100 | rtp_session->srtp_idx_rtcp = idx = 1; | |||
4101 | } | |||
4102 | ||||
4103 | if (direction == SWITCH_RTP_CRYPTO_RECV) { | |||
4104 | policy = &rtp_session->recv_policy[idx]; | |||
4105 | keysalt = ssec->remote_raw_key; | |||
4106 | key_material = ssec->remote_key_material_next; | |||
4107 | key_material_n = &ssec->remote_key_material_n; | |||
4108 | } else { | |||
4109 | policy = &rtp_session->send_policy[idx]; | |||
4110 | keysalt = ssec->local_raw_key; | |||
4111 | key_material = ssec->local_key_material_next; | |||
4112 | key_material_n = &ssec->local_key_material_n; | |||
4113 | } | |||
4114 | ||||
4115 | switch_b64_encode(keysalt, keysalt_len, b64_key, sizeof(b64_key)); | |||
4116 | ||||
4117 | if (switch_true(switch_core_get_variable("rtp_retain_crypto_keys"))) { | |||
4118 | switch(direction) { | |||
4119 | case SWITCH_RTP_CRYPTO_SEND: | |||
4120 | switch_channel_set_variable(channel, "srtp_local_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtp_local_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4121 | break; | |||
4122 | case SWITCH_RTP_CRYPTO_RECV: | |||
4123 | switch_channel_set_variable(channel, "srtp_remote_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtp_remote_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4124 | break; | |||
4125 | case SWITCH_RTP_CRYPTO_SEND_RTCP: | |||
4126 | switch_channel_set_variable(channel, "srtcp_local_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtcp_local_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4127 | break; | |||
4128 | case SWITCH_RTP_CRYPTO_RECV_RTCP: | |||
4129 | switch_channel_set_variable(channel, "srtcp_remote_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtcp_remote_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4130 | break; | |||
4131 | default: | |||
4132 | break; | |||
4133 | } | |||
4134 | ||||
4135 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
4136 | switch(direction) { | |||
4137 | case SWITCH_RTP_CRYPTO_SEND: | |||
4138 | switch_channel_set_variable(channel, "srtp_local_video_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtp_local_video_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4139 | break; | |||
4140 | case SWITCH_RTP_CRYPTO_RECV: | |||
4141 | switch_channel_set_variable(channel, "srtp_remote_video_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtp_remote_video_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4142 | break; | |||
4143 | case SWITCH_RTP_CRYPTO_SEND_RTCP: | |||
4144 | switch_channel_set_variable(channel, "srtcp_local_video_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtcp_local_video_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4145 | break; | |||
4146 | case SWITCH_RTP_CRYPTO_RECV_RTCP: | |||
4147 | switch_channel_set_variable(channel, "srtcp_remote_video_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtcp_remote_video_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4148 | break; | |||
4149 | default: | |||
4150 | break; | |||
4151 | } | |||
4152 | ||||
4153 | } else { | |||
4154 | switch(direction) { | |||
4155 | case SWITCH_RTP_CRYPTO_SEND: | |||
4156 | switch_channel_set_variable(channel, "srtp_local_audio_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtp_local_audio_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4157 | break; | |||
4158 | case SWITCH_RTP_CRYPTO_RECV: | |||
4159 | switch_channel_set_variable(channel, "srtp_remote_audio_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtp_remote_audio_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4160 | break; | |||
4161 | case SWITCH_RTP_CRYPTO_SEND_RTCP: | |||
4162 | switch_channel_set_variable(channel, "srtcp_local_audio_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtcp_local_audio_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4163 | break; | |||
4164 | case SWITCH_RTP_CRYPTO_RECV_RTCP: | |||
4165 | switch_channel_set_variable(channel, "srtcp_remote_audio_crypto_key", (const char *)b64_key)switch_channel_set_variable_var_check(channel, "srtcp_remote_audio_crypto_key" , (const char *)b64_key, SWITCH_TRUE); | |||
4166 | break; | |||
4167 | default: | |||
4168 | break; | |||
4169 | } | |||
4170 | } | |||
4171 | } | |||
4172 | ||||
4173 | 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__, 4173); | |||
4174 | ||||
4175 | crypto_key->type = ssec->crypto_type; | |||
4176 | crypto_key->index = index; | |||
4177 | memcpy(crypto_key->keysalt, keysalt, keysalt_len); | |||
4178 | crypto_key->next = rtp_session->crypto_keys[direction]; | |||
4179 | rtp_session->crypto_keys[direction] = crypto_key; | |||
4180 | ||||
4181 | memset(policy, 0, sizeof(*policy)); | |||
4182 | ||||
4183 | /* many devices can't handle gaps in SRTP streams */ | |||
4184 | if (!((var = switch_channel_get_variable(channel, "srtp_allow_idle_gaps")switch_channel_get_variable_dup(channel, "srtp_allow_idle_gaps" , SWITCH_TRUE, -1)) | |||
4185 | && switch_true(var)) | |||
4186 | && (!(var = switch_channel_get_variable(channel, "send_silence_when_idle")switch_channel_get_variable_dup(channel, "send_silence_when_idle" , SWITCH_TRUE, -1)) | |||
4187 | || !(atoi(var)))) { | |||
4188 | switch_channel_set_variable(channel, "send_silence_when_idle", "-1")switch_channel_set_variable_var_check(channel, "send_silence_when_idle" , "-1", SWITCH_TRUE); | |||
4189 | } | |||
4190 | ||||
4191 | switch (crypto_key->type) { | |||
4192 | case AES_CM_128_HMAC_SHA1_80: | |||
4193 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtp)srtp_crypto_policy_set_rtp_default(&policy->rtp); | |||
4194 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtcp)srtp_crypto_policy_set_rtp_default(&policy->rtcp); | |||
4195 | ||||
4196 | if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { | |||
4197 | 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); | |||
4198 | } | |||
4199 | break; | |||
4200 | case AES_CM_128_HMAC_SHA1_32: | |||
4201 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtp); | |||
4202 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtcp); | |||
4203 | ||||
4204 | ||||
4205 | if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { | |||
4206 | 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); | |||
4207 | } | |||
4208 | break; | |||
4209 | ||||
4210 | case AEAD_AES_256_GCM_8: | |||
4211 | srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy->rtp); | |||
4212 | srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy->rtcp); | |||
4213 | ||||
4214 | if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { | |||
4215 | 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); | |||
4216 | } | |||
4217 | break; | |||
4218 | ||||
4219 | case AEAD_AES_256_GCM: | |||
4220 | srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy->rtp); | |||
4221 | srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy->rtcp); | |||
4222 | ||||
4223 | if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { | |||
4224 | switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_256_GCM")switch_channel_set_variable_var_check(channel, "rtp_has_crypto" , "AEAD_AES_256_GCM", SWITCH_TRUE); | |||
4225 | } | |||
4226 | break; | |||
4227 | ||||
4228 | case AEAD_AES_128_GCM_8: | |||
4229 | srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy->rtp); | |||
4230 | srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy->rtcp); | |||
4231 | ||||
4232 | if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { | |||
4233 | 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); | |||
4234 | } | |||
4235 | break; | |||
4236 | ||||
4237 | case AEAD_AES_128_GCM: | |||
4238 | srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy->rtp); | |||
4239 | srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy->rtcp); | |||
4240 | ||||
4241 | if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { | |||
4242 | switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_128_GCM")switch_channel_set_variable_var_check(channel, "rtp_has_crypto" , "AEAD_AES_128_GCM", SWITCH_TRUE); | |||
4243 | } | |||
4244 | break; | |||
4245 | ||||
4246 | case AES_CM_256_HMAC_SHA1_80: | |||
4247 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtp); | |||
4248 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtcp); | |||
4249 | if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { | |||
4250 | 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); | |||
4251 | } | |||
4252 | break; | |||
4253 | case AES_CM_256_HMAC_SHA1_32: | |||
4254 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy->rtp); | |||
4255 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy->rtcp); | |||
4256 | if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { | |||
4257 | switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_256_HMAC_SHA1_32")switch_channel_set_variable_var_check(channel, "rtp_has_crypto" , "AES_CM_256_HMAC_SHA1_32", SWITCH_TRUE); | |||
4258 | } | |||
4259 | break; | |||
4260 | case AES_CM_128_NULL_AUTH: | |||
4261 | srtp_crypto_policy_set_aes_cm_128_null_auth(&policy->rtp); | |||
4262 | srtp_crypto_policy_set_aes_cm_128_null_auth(&policy->rtcp); | |||
4263 | ||||
4264 | if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { | |||
4265 | 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); | |||
4266 | } | |||
4267 | break; | |||
4268 | default: | |||
4269 | break; | |||
4270 | } | |||
4271 | ||||
4272 | /* Setup the policy with MKI if they are used. Number of key materials must be positive to use MKI. */ | |||
4273 | if (key_material && (*key_material_n > 0)) { | |||
4274 | ||||
4275 | if (direction == SWITCH_RTP_CRYPTO_RECV) { | |||
4276 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI] = 1; /* tell the rest of the environment MKI is used */ | |||
4277 | } else { | |||
4278 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI] = 1; /* tell the rest of the environment MKI is used */ | |||
4279 | } | |||
4280 | ||||
4281 | /* key must be NULL for libsrtp to work correctly with MKI. */ | |||
4282 | policy->key = NULL((void*)0); | |||
4283 | ||||
4284 | /* Allocate array for MKIs. */ | |||
4285 | mkis = switch_core_alloc(rtp_session->pool, *key_material_n * sizeof(srtp_master_key_t*))switch_core_perform_alloc(rtp_session->pool, *key_material_n * sizeof(srtp_master_key_t*), "src/switch_rtp.c", (const char *)__func__, 4285); | |||
4286 | if (!mkis) { | |||
4287 | return SWITCH_STATUS_FALSE; | |||
4288 | } | |||
4289 | ||||
4290 | /* Build array of MKIs. */ | |||
4291 | mki_idx = 0; | |||
4292 | ||||
4293 | while (key_material && (mki_idx < *key_material_n)) { | |||
4294 | ||||
4295 | if (key_material->mki_size < 1) { | |||
4296 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4296, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "MKI bad key size at MKI %d\n", mki_idx); | |||
4297 | return SWITCH_STATUS_FALSE; | |||
4298 | } | |||
4299 | ||||
4300 | mki = switch_core_alloc(rtp_session->pool, sizeof(srtp_master_key_t))switch_core_perform_alloc(rtp_session->pool, sizeof(srtp_master_key_t ), "src/switch_rtp.c", (const char *)__func__, 4300); | |||
4301 | if (!mki) { | |||
4302 | return SWITCH_STATUS_FALSE; | |||
4303 | } | |||
4304 | ||||
4305 | /* Setup MKI. */ | |||
4306 | mki->mki_id = switch_core_alloc(rtp_session->pool, sizeof(key_material->mki_size))switch_core_perform_alloc(rtp_session->pool, sizeof(key_material ->mki_size), "src/switch_rtp.c", (const char *)__func__, 4306 ); | |||
4307 | if (!mki->mki_id) { | |||
4308 | return SWITCH_STATUS_FALSE; | |||
4309 | } | |||
4310 | ||||
4311 | mki->key = switch_core_alloc(rtp_session->pool, keysalt_len)switch_core_perform_alloc(rtp_session->pool, keysalt_len, "src/switch_rtp.c" , (const char *)__func__, 4311); | |||
4312 | if (!mki->key) { | |||
4313 | return SWITCH_STATUS_FALSE; | |||
4314 | } | |||
4315 | ||||
4316 | memcpy(mki->mki_id, &key_material->mki_id, key_material->mki_size); | |||
4317 | mki->mki_size = key_material->mki_size; | |||
4318 | memcpy(mki->key, key_material->raw_key, keysalt_len); | |||
4319 | ||||
4320 | mkis[mki_idx] = mki; | |||
4321 | ||||
4322 | key_material = key_material->next; | |||
4323 | ++mki_idx; | |||
4324 | } | |||
4325 | ||||
4326 | /* And pass the array of MKIs to libsrtp. */ | |||
4327 | policy->keys = mkis; | |||
4328 | policy->num_master_keys = mki_idx; | |||
4329 | ||||
4330 | } else { | |||
4331 | policy->key = (uint8_t *) crypto_key->keysalt; | |||
4332 | } | |||
4333 | ||||
4334 | policy->next = NULL((void*)0); | |||
4335 | ||||
4336 | policy->window_size = 1024; | |||
4337 | policy->allow_repeat_tx = 1; | |||
4338 | ||||
4339 | //policy->rtp.sec_serv = sec_serv_conf_and_auth; | |||
4340 | //policy->rtcp.sec_serv = sec_serv_conf_and_auth; | |||
4341 | ||||
4342 | switch (direction) { | |||
4343 | case SWITCH_RTP_CRYPTO_RECV: | |||
4344 | policy->ssrc.type = ssrc_any_inbound; | |||
4345 | ||||
4346 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && idx == 0 && rtp_session->recv_ctx[idx]) { | |||
4347 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] = 1; | |||
4348 | } else { | |||
4349 | if ((stat = srtp_create(&rtp_session->recv_ctx[idx], policy)) || !rtp_session->recv_ctx[idx]) { | |||
4350 | status = SWITCH_STATUS_FALSE; | |||
4351 | } | |||
4352 | ||||
4353 | if (status == SWITCH_STATUS_SUCCESS) { | |||
4354 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4354, (const char*)(rtp_session->session), SWITCH_LOG_INFO, "Activating %s Secure %s RECV%s\n", | |||
4355 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), idx ? "RTCP" : "RTP", rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI] ? " (with MKI)" : ""); | |||
4356 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 1; | |||
4357 | } else { | |||
4358 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4358, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating srtp [%d]\n", stat); | |||
4359 | return status; | |||
4360 | } | |||
4361 | } | |||
4362 | break; | |||
4363 | case SWITCH_RTP_CRYPTO_SEND: | |||
4364 | policy->ssrc.type = ssrc_any_outbound; | |||
4365 | //policy->ssrc.type = ssrc_specific; | |||
4366 | //policy->ssrc.value = rtp_session->ssrc; | |||
4367 | ||||
4368 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] && idx == 0 && rtp_session->send_ctx[idx]) { | |||
4369 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET] = 1; | |||
4370 | } else { | |||
4371 | if ((stat = srtp_create(&rtp_session->send_ctx[idx], policy)) || !rtp_session->send_ctx[idx]) { | |||
4372 | status = SWITCH_STATUS_FALSE; | |||
4373 | } | |||
4374 | ||||
4375 | if (status == SWITCH_STATUS_SUCCESS) { | |||
4376 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4376, (const char*)(rtp_session->session), SWITCH_LOG_INFO, "Activating %s Secure %s SEND%s\n", | |||
4377 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), idx ? "RTCP" : "RTP", rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI] ? " (with MKI)" : ""); | |||
4378 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 1; | |||
4379 | } else { | |||
4380 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4380, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating SRTP [%d]\n", stat); | |||
4381 | return status; | |||
4382 | } | |||
4383 | } | |||
4384 | ||||
4385 | break; | |||
4386 | default: | |||
4387 | abort(); | |||
4388 | break; | |||
4389 | } | |||
4390 | ||||
4391 | if (switch_event_create(&fsevent, SWITCH_EVENT_CALL_SECURE)switch_event_create_subclass_detailed("src/switch_rtp.c", (const char * )(const char *)__func__, 4391, &fsevent, SWITCH_EVENT_CALL_SECURE , ((void*)0)) == SWITCH_STATUS_SUCCESS) { | |||
4392 | if (rtp_session->dtls) { | |||
4393 | switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "srtp:dtls:AES_CM_128_HMAC_SHA1_80"); | |||
4394 | 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); | |||
4395 | } else { | |||
4396 | 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)); | |||
4397 | } | |||
4398 | switch_event_add_header_string(fsevent, SWITCH_STACK_BOTTOM, "caller-unique-id", switch_channel_get_uuid(channel)); | |||
4399 | switch_event_fire(&fsevent)switch_event_fire_detailed("src/switch_rtp.c", (const char * ) (const char *)__func__, 4399, &fsevent, ((void*)0)); | |||
4400 | } | |||
4401 | ||||
4402 | ||||
4403 | return SWITCH_STATUS_SUCCESS; | |||
4404 | #endif | |||
4405 | } | |||
4406 | ||||
4407 | SWITCH_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) | |||
4408 | { | |||
4409 | rtp_session->ms_per_packet = ms_per_packet; | |||
4410 | rtp_session->samples_per_interval = rtp_session->conf_samples_per_interval = samples_per_interval; | |||
4411 | rtp_session->missed_count = 0; | |||
4412 | rtp_session->samples_per_second = | |||
4413 | (uint32_t) ((double) (1000.0f / (double) (rtp_session->ms_per_packet / 1000)) * (double) rtp_session->samples_per_interval); | |||
4414 | ||||
4415 | rtp_session->one_second = (rtp_session->samples_per_second / rtp_session->samples_per_interval); | |||
4416 | ||||
4417 | return SWITCH_STATUS_SUCCESS; | |||
4418 | } | |||
4419 | ||||
4420 | SWITCH_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) | |||
4421 | { | |||
4422 | switch_status_t status = SWITCH_STATUS_SUCCESS; | |||
4423 | int change_timer = 0; | |||
4424 | ||||
4425 | if (rtp_session->ms_per_packet != ms_per_packet || rtp_session->samples_per_interval != samples_per_interval) { | |||
4426 | change_timer = 1; | |||
4427 | } | |||
4428 | ||||
4429 | switch_rtp_set_interval(rtp_session, ms_per_packet, samples_per_interval); | |||
4430 | ||||
4431 | if (change_timer && rtp_session->timer_name) { | |||
4432 | READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session-> reading++; | |||
4433 | WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session-> writing++; | |||
4434 | ||||
4435 | if (rtp_session->timer.timer_interface) { | |||
4436 | switch_core_timer_destroy(&rtp_session->timer); | |||
4437 | } | |||
4438 | ||||
4439 | if (rtp_session->write_timer.timer_interface) { | |||
4440 | switch_core_timer_destroy(&rtp_session->write_timer); | |||
4441 | } | |||
4442 | ||||
4443 | if ((status = switch_core_timer_init(&rtp_session->timer, | |||
4444 | rtp_session->timer_name, ms_per_packet / 1000, | |||
4445 | samples_per_interval, rtp_session->pool)) == SWITCH_STATUS_SUCCESS) { | |||
4446 | ||||
4447 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4447, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, | |||
4448 | "RE-Starting timer [%s] %d bytes per %dms\n", rtp_session->timer_name, samples_per_interval, ms_per_packet / 1000); | |||
4449 | switch_core_timer_init(&rtp_session->write_timer, rtp_session->timer_name, (ms_per_packet / 1000), samples_per_interval, rtp_session->pool); | |||
4450 | } else { | |||
4451 | ||||
4452 | memset(&rtp_session->timer, 0, sizeof(rtp_session->timer)); | |||
4453 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4453, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, | |||
4454 | "Problem RE-Starting timer [%s] %d bytes per %dms\n", rtp_session->timer_name, samples_per_interval, ms_per_packet / 1000); | |||
4455 | } | |||
4456 | ||||
4457 | WRITE_DEC(rtp_session)rtp_session->writing--; switch_mutex_unlock(rtp_session-> write_mutex); | |||
4458 | READ_DEC(rtp_session)rtp_session->reading--; switch_mutex_unlock(rtp_session-> read_mutex); | |||
4459 | } | |||
4460 | ||||
4461 | return status; | |||
4462 | } | |||
4463 | ||||
4464 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_set_ssrc(switch_rtp_t *rtp_session, uint32_t ssrc) | |||
4465 | { | |||
4466 | rtp_session->ssrc = ssrc; | |||
4467 | rtp_session->send_msg.header.ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
4468 | ||||
4469 | return SWITCH_STATUS_SUCCESS; | |||
4470 | } | |||
4471 | ||||
4472 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_set_remote_ssrc(switch_rtp_t *rtp_session, uint32_t ssrc) | |||
4473 | { | |||
4474 | rtp_session->remote_ssrc = ssrc; | |||
4475 | rtp_session->flags[SWITCH_RTP_FLAG_DETECT_SSRC] = 0; | |||
4476 | return SWITCH_STATUS_SUCCESS; | |||
4477 | } | |||
4478 | ||||
4479 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_create(switch_rtp_t **new_rtp_session, | |||
4480 | switch_payload_t payload, | |||
4481 | uint32_t samples_per_interval, | |||
4482 | uint32_t ms_per_packet, | |||
4483 | switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID], char *timer_name, const char **err, switch_memory_pool_t *pool) | |||
4484 | { | |||
4485 | switch_rtp_t *rtp_session = NULL((void*)0); | |||
4486 | switch_core_session_t *session = switch_core_memory_pool_get_data(pool, "__session"); | |||
4487 | switch_channel_t *channel = NULL((void*)0); | |||
4488 | ||||
4489 | if (session) channel = switch_core_session_get_channel(session); | |||
4490 | ||||
4491 | *new_rtp_session = NULL((void*)0); | |||
4492 | ||||
4493 | if (samples_per_interval > SWITCH_RTP_MAX_BUF_LEN16384) { | |||
4494 | *err = "Packet Size Too Large!"; | |||
4495 | return SWITCH_STATUS_FALSE; | |||
4496 | } | |||
4497 | ||||
4498 | 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__, 4498))) { | |||
4499 | *err = "Memory Error!"; | |||
4500 | return SWITCH_STATUS_MEMERR; | |||
4501 | } | |||
4502 | ||||
4503 | rtp_session->pool = pool; | |||
4504 | rtp_session->te = INVALID_PT; | |||
4505 | rtp_session->recv_te = INVALID_PT; | |||
4506 | rtp_session->cng_pt = INVALID_PT; | |||
4507 | rtp_session->session = session; | |||
4508 | ||||
4509 | switch_mutex_init(&rtp_session->flag_mutex, SWITCH_MUTEX_NESTED0x1, pool); | |||
4510 | switch_mutex_init(&rtp_session->read_mutex, SWITCH_MUTEX_NESTED0x1, pool); | |||
4511 | switch_mutex_init(&rtp_session->write_mutex, SWITCH_MUTEX_NESTED0x1, pool); | |||
4512 | switch_mutex_init(&rtp_session->ice_mutex, SWITCH_MUTEX_NESTED0x1, pool); | |||
4513 | switch_mutex_init(&rtp_session->dtmf_data.dtmf_mutex, SWITCH_MUTEX_NESTED0x1, pool); | |||
4514 | switch_queue_create(&rtp_session->dtmf_data.dtmf_queue, 100, rtp_session->pool); | |||
4515 | switch_queue_create(&rtp_session->dtmf_data.dtmf_inqueue, 100, rtp_session->pool); | |||
4516 | ||||
4517 | switch_rtp_set_flags(rtp_session, flags); | |||
4518 | ||||
4519 | /* for from address on recvfrom calls */ | |||
4520 | switch_sockaddr_create(&rtp_session->from_addr, pool); | |||
4521 | switch_sockaddr_create(&rtp_session->rtp_from_addr, pool); | |||
4522 | ||||
4523 | if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
4524 | switch_sockaddr_create(&rtp_session->rtcp_from_addr, pool); | |||
4525 | } | |||
4526 | rtp_session->seq = (uint16_t) rand(); | |||
4527 | rtp_session->ssrc = (uint32_t) ((intptr_t) rtp_session + (uint32_t) switch_epoch_time_now(NULL((void*)0))); | |||
4528 | #ifdef DEBUG_TS_ROLLOVER | |||
4529 | rtp_session->last_write_ts = TS_ROLLOVER_START; | |||
4530 | #endif | |||
4531 | rtp_session->stats.inbound.R = 100.0; | |||
4532 | rtp_session->stats.inbound.mos = 4.5; | |||
4533 | rtp_session->send_msg.header.ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
4534 | rtp_session->send_msg.header.ts = 0; | |||
4535 | rtp_session->send_msg.header.m = 0; | |||
4536 | rtp_session->send_msg.header.pt = (switch_payload_t) htonl(payload)__bswap_32 (payload); | |||
4537 | rtp_session->send_msg.header.version = 2; | |||
4538 | rtp_session->send_msg.header.p = 0; | |||
4539 | rtp_session->send_msg.header.x = 0; | |||
4540 | rtp_session->send_msg.header.cc = 0; | |||
4541 | ||||
4542 | rtp_session->recv_msg.header.ssrc = 0; | |||
4543 | rtp_session->recv_msg.header.ts = 0; | |||
4544 | rtp_session->recv_msg.header.seq = 0; | |||
4545 | rtp_session->recv_msg.header.m = 0; | |||
4546 | rtp_session->recv_msg.header.pt = (switch_payload_t) htonl(payload)__bswap_32 (payload); | |||
4547 | rtp_session->recv_msg.header.version = 2; | |||
4548 | rtp_session->recv_msg.header.p = 0; | |||
4549 | rtp_session->recv_msg.header.x = 0; | |||
4550 | rtp_session->recv_msg.header.cc = 0; | |||
4551 | ||||
4552 | rtp_session->payload = payload; | |||
4553 | rtp_session->rtcp_last_sent = switch_micro_time_now(); | |||
4554 | ||||
4555 | switch_rtp_set_interval(rtp_session, ms_per_packet, samples_per_interval); | |||
4556 | rtp_session->conf_samples_per_interval = samples_per_interval; | |||
4557 | ||||
4558 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && zstr(timer_name)_zstr(timer_name)) { | |||
4559 | timer_name = "soft"; | |||
4560 | } | |||
4561 | ||||
4562 | if (!zstr(timer_name)_zstr(timer_name) && !strcasecmp(timer_name, "none")) { | |||
4563 | timer_name = NULL((void*)0); | |||
4564 | } | |||
4565 | ||||
4566 | if (!zstr(timer_name)_zstr(timer_name)) { | |||
4567 | rtp_session->timer_name = switch_core_strdup(pool, timer_name)switch_core_perform_strdup(pool, timer_name, "src/switch_rtp.c" , (const char *)__func__, 4567); | |||
4568 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER); | |||
4569 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK); | |||
4570 | ||||
4571 | if (switch_core_timer_init(&rtp_session->timer, timer_name, ms_per_packet / 1000, samples_per_interval, pool) == SWITCH_STATUS_SUCCESS) { | |||
4572 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4572, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, | |||
4573 | "Starting timer [%s] %d bytes per %dms\n", timer_name, samples_per_interval, ms_per_packet / 1000); | |||
4574 | switch_core_timer_init(&rtp_session->write_timer, timer_name, (ms_per_packet / 1000), samples_per_interval, pool); | |||
4575 | #ifdef DEBUG_TS_ROLLOVER | |||
4576 | rtp_session->timer.tick = TS_ROLLOVER_START / samples_per_interval; | |||
4577 | #endif | |||
4578 | } else { | |||
4579 | memset(&rtp_session->timer, 0, sizeof(rtp_session->timer)); | |||
4580 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4580, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, | |||
4581 | "Error Starting timer [%s] %d bytes per %dms, async RTP disabled\n", timer_name, samples_per_interval, ms_per_packet / 1000); | |||
4582 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER); | |||
4583 | } | |||
4584 | } else { | |||
4585 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
4586 | if (switch_core_timer_init(&rtp_session->timer, "soft", 1, 90, pool) == SWITCH_STATUS_SUCCESS) { | |||
4587 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4587, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "Starting video timer.\n"); | |||
4588 | } | |||
4589 | ||||
4590 | //switch_jb_create(&rtp_session->vb, 3, 10, rtp_session->pool); | |||
4591 | //switch_jb_debug_level(rtp_session->vb, 10); | |||
4592 | ||||
4593 | } else { | |||
4594 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4594, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "Not using a timer\n"); | |||
4595 | } | |||
4596 | ||||
4597 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER); | |||
4598 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK); | |||
4599 | } | |||
4600 | ||||
4601 | ||||
4602 | if (channel) { | |||
4603 | switch_channel_set_private(channel, "__rtcp_audio_rtp_session", rtp_session); | |||
4604 | } | |||
4605 | ||||
4606 | #ifdef ENABLE_ZRTP | |||
4607 | if (zrtp_on && session && channel && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) { | |||
4608 | switch_rtp_t *master_rtp_session = NULL((void*)0); | |||
4609 | ||||
4610 | int initiator = 0; | |||
4611 | const char *zrtp_enabled = switch_channel_get_variable(channel, "zrtp_secure_media")switch_channel_get_variable_dup(channel, "zrtp_secure_media", SWITCH_TRUE, -1); | |||
4612 | int srtp_enabled = switch_channel_test_flag(channel, CF_SECURE); | |||
4613 | ||||
4614 | if (srtp_enabled && switch_true(zrtp_enabled)) { | |||
4615 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4615, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, | |||
4616 | "You can not have ZRTP and SRTP enabled simultaneously, ZRTP will be disabled for this call!\n"); | |||
4617 | switch_channel_set_variable(channel, "zrtp_secure_media", NULL)switch_channel_set_variable_var_check(channel, "zrtp_secure_media" , ((void*)0), SWITCH_TRUE); | |||
4618 | zrtp_enabled = NULL((void*)0); | |||
4619 | } | |||
4620 | ||||
4621 | ||||
4622 | if (switch_true(zrtp_enabled)) { | |||
4623 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
4624 | switch_channel_set_private(channel, "__zrtp_video_rtp_session", rtp_session); | |||
4625 | master_rtp_session = switch_channel_get_private(channel, "__zrtp_audio_rtp_session"); | |||
4626 | } else { | |||
4627 | switch_channel_set_private(channel, "__zrtp_audio_rtp_session", rtp_session); | |||
4628 | master_rtp_session = rtp_session; | |||
4629 | } | |||
4630 | ||||
4631 | ||||
4632 | if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { | |||
4633 | initiator = 1; | |||
4634 | } | |||
4635 | ||||
4636 | if (rtp_session == master_rtp_session) { | |||
4637 | 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__ , 4637); | |||
4638 | zrtp_profile_defaults(rtp_session->zrtp_profile, zrtp_global); | |||
4639 | ||||
4640 | rtp_session->zrtp_profile->allowclear = 0; | |||
4641 | rtp_session->zrtp_profile->disclose_bit = 0; | |||
4642 | rtp_session->zrtp_profile->cache_ttl = (uint32_t) -1; | |||
4643 | ||||
4644 | if (zrtp_status_ok != zrtp_session_init(zrtp_global, rtp_session->zrtp_profile, zid, initiator, &rtp_session->zrtp_session)) { | |||
4645 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4645, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error! zRTP INIT Failed\n"); | |||
4646 | zrtp_session_down(rtp_session->zrtp_session); | |||
4647 | rtp_session->zrtp_session = NULL((void*)0); | |||
4648 | goto end; | |||
4649 | } | |||
4650 | ||||
4651 | zrtp_session_set_userdata(rtp_session->zrtp_session, session); | |||
4652 | ||||
4653 | ||||
4654 | if (zrtp_status_ok != zrtp_stream_attach(master_rtp_session->zrtp_session, &rtp_session->zrtp_stream)) { | |||
4655 | abort(); | |||
4656 | } | |||
4657 | ||||
4658 | zrtp_stream_set_userdata(rtp_session->zrtp_stream, rtp_session); | |||
4659 | ||||
4660 | if (switch_true(switch_channel_get_variable(channel, "zrtp_enrollment")switch_channel_get_variable_dup(channel, "zrtp_enrollment", SWITCH_TRUE , -1))) { | |||
4661 | zrtp_stream_registration_start(rtp_session->zrtp_stream, rtp_session->ssrc); | |||
4662 | } else { | |||
4663 | zrtp_stream_start(rtp_session->zrtp_stream, rtp_session->ssrc); | |||
4664 | } | |||
4665 | } | |||
4666 | ||||
4667 | } | |||
4668 | } | |||
4669 | ||||
4670 | end: | |||
4671 | ||||
4672 | #endif | |||
4673 | ||||
4674 | /* Jitter */ | |||
4675 | rtp_session->stats.inbound.last_proc_time = switch_micro_time_now() / 1000; | |||
4676 | rtp_session->stats.inbound.jitter_n = 0; | |||
4677 | rtp_session->stats.inbound.jitter_add = 0; | |||
4678 | rtp_session->stats.inbound.jitter_addsq = 0; | |||
4679 | rtp_session->stats.inbound.min_variance = 0; | |||
4680 | rtp_session->stats.inbound.max_variance = 0; | |||
4681 | ||||
4682 | /* Burst and Packet Loss */ | |||
4683 | rtp_session->stats.inbound.lossrate = 0; | |||
4684 | rtp_session->stats.inbound.burstrate = 0; | |||
4685 | memset(rtp_session->stats.inbound.loss, 0, sizeof(rtp_session->stats.inbound.loss)); | |||
4686 | rtp_session->stats.inbound.last_loss = 0; | |||
4687 | rtp_session->stats.inbound.last_processed_seq = -1; | |||
4688 | ||||
4689 | rtp_session->ready = 1; | |||
4690 | *new_rtp_session = rtp_session; | |||
4691 | ||||
4692 | return SWITCH_STATUS_SUCCESS; | |||
4693 | } | |||
4694 | ||||
4695 | SWITCH_DECLARE(switch_rtp_t *)__attribute__((visibility("default"))) switch_rtp_t * switch_rtp_new(const char *rx_host, | |||
4696 | switch_port_t rx_port, | |||
4697 | const char *tx_host, | |||
4698 | switch_port_t tx_port, | |||
4699 | switch_payload_t payload, | |||
4700 | uint32_t samples_per_interval, | |||
4701 | uint32_t ms_per_packet, | |||
4702 | switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID], char *timer_name, const char **err, switch_memory_pool_t *pool, | |||
4703 | switch_port_t bundle_internal_port, | |||
4704 | switch_port_t bundle_external_port) | |||
4705 | { | |||
4706 | switch_rtp_t *rtp_session = NULL((void*)0); | |||
4707 | ||||
4708 | if (zstr(rx_host)_zstr(rx_host)) { | |||
4709 | *err = "Missing local host"; | |||
4710 | goto end; | |||
4711 | } | |||
4712 | ||||
4713 | if (!rx_port) { | |||
4714 | *err = "Missing local port"; | |||
4715 | goto end; | |||
4716 | } | |||
4717 | ||||
4718 | if (zstr(tx_host)_zstr(tx_host)) { | |||
4719 | *err = "Missing remote host"; | |||
4720 | goto end; | |||
4721 | } | |||
4722 | ||||
4723 | if (!tx_port) { | |||
4724 | *err = "Missing remote port"; | |||
4725 | goto end; | |||
4726 | } | |||
4727 | ||||
4728 | if (switch_rtp_create(&rtp_session, payload, samples_per_interval, ms_per_packet, flags, timer_name, err, pool) != SWITCH_STATUS_SUCCESS) { | |||
4729 | goto end; | |||
4730 | } | |||
4731 | ||||
4732 | switch_mutex_lock(rtp_session->flag_mutex); | |||
4733 | ||||
4734 | if (switch_rtp_set_local_address(rtp_session, rx_host, rx_port, err) != SWITCH_STATUS_SUCCESS) { | |||
4735 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
4736 | rtp_session = NULL((void*)0); | |||
4737 | goto end; | |||
4738 | } | |||
4739 | ||||
4740 | if (switch_rtp_set_remote_address(rtp_session, tx_host, tx_port, 0, SWITCH_TRUE, err) != SWITCH_STATUS_SUCCESS) { | |||
4741 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
4742 | rtp_session = NULL((void*)0); | |||
4743 | goto end; | |||
4744 | } | |||
4745 | ||||
4746 | end: | |||
4747 | ||||
4748 | if (rtp_session) { | |||
4749 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
4750 | rtp_session->ready = 2; | |||
4751 | 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__, 4751); | |||
4752 | rtp_session->rx_port = rx_port; | |||
4753 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH); | |||
4754 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_DETECT_SSRC); | |||
4755 | } else { | |||
4756 | switch_rtp_release_port(rx_host, rx_port); | |||
4757 | } | |||
4758 | ||||
4759 | return rtp_session; | |||
4760 | } | |||
4761 | ||||
4762 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_telephony_event(switch_rtp_t *rtp_session, switch_payload_t te) | |||
4763 | { | |||
4764 | if (te > 95) { | |||
4765 | rtp_session->te = te; | |||
4766 | } | |||
4767 | } | |||
4768 | ||||
4769 | ||||
4770 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_telephony_recv_event(switch_rtp_t *rtp_session, switch_payload_t te) | |||
4771 | { | |||
4772 | if (te > 95) { | |||
4773 | rtp_session->recv_te = te; | |||
4774 | } | |||
4775 | } | |||
4776 | ||||
4777 | ||||
4778 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_cng_pt(switch_rtp_t *rtp_session, switch_payload_t pt) | |||
4779 | { | |||
4780 | rtp_session->cng_pt = pt; | |||
4781 | rtp_session->flags[SWITCH_RTP_FLAG_AUTO_CNG] = 1; | |||
4782 | } | |||
4783 | ||||
4784 | SWITCH_DECLARE(switch_timer_t *)__attribute__((visibility("default"))) switch_timer_t * switch_rtp_get_media_timer(switch_rtp_t *rtp_session) | |||
4785 | { | |||
4786 | ||||
4787 | if (rtp_session && rtp_session->timer.timer_interface) { | |||
4788 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
4789 | switch_core_timer_sync(&rtp_session->timer); | |||
4790 | } | |||
4791 | return &rtp_session->timer; | |||
4792 | } | |||
4793 | ||||
4794 | return NULL((void*)0); | |||
4795 | } | |||
4796 | ||||
4797 | ||||
4798 | SWITCH_DECLARE(switch_jb_t *)__attribute__((visibility("default"))) switch_jb_t * switch_rtp_get_jitter_buffer(switch_rtp_t *rtp_session) | |||
4799 | { | |||
4800 | if (!switch_rtp_ready(rtp_session)) { | |||
4801 | return NULL((void*)0); | |||
4802 | } | |||
4803 | ||||
4804 | return rtp_session->jb ? rtp_session->jb : rtp_session->vb; | |||
4805 | } | |||
4806 | ||||
4807 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_pause_jitter_buffer(switch_rtp_t *rtp_session, switch_bool_t pause) | |||
4808 | { | |||
4809 | int new_val; | |||
4810 | ||||
4811 | if (rtp_session->pause_jb && !pause) { | |||
4812 | if (rtp_session->jb) { | |||
4813 | switch_jb_reset(rtp_session->jb); | |||
4814 | } | |||
4815 | ||||
4816 | if (rtp_session->vb) { | |||
4817 | switch_jb_reset(rtp_session->vb); | |||
4818 | } | |||
4819 | } | |||
4820 | ||||
4821 | new_val = pause ? 1 : -1; | |||
4822 | ||||
4823 | if (rtp_session->pause_jb + new_val > -1) { | |||
4824 | rtp_session->pause_jb += new_val; | |||
4825 | } | |||
4826 | ||||
4827 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4827, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, | |||
4828 | "Jitterbuffer %s is %s\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), rtp_session->pause_jb ? "paused" : "enabled"); | |||
4829 | ||||
4830 | return SWITCH_STATUS_SUCCESS; | |||
4831 | } | |||
4832 | ||||
4833 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_deactivate_jitter_buffer(switch_rtp_t *rtp_session) | |||
4834 | { | |||
4835 | ||||
4836 | if (!switch_rtp_ready(rtp_session)) { | |||
4837 | return SWITCH_STATUS_FALSE; | |||
4838 | } | |||
4839 | ||||
4840 | rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB]++; | |||
4841 | ||||
4842 | return SWITCH_STATUS_SUCCESS; | |||
4843 | } | |||
4844 | ||||
4845 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_get_video_buffer_size(switch_rtp_t *rtp_session, uint32_t *min_frame_len, uint32_t *max_frame_len, uint32_t *cur_frame_len, uint32_t *highest_frame_len) | |||
4846 | { | |||
4847 | ||||
4848 | if (rtp_session->vb) { | |||
4849 | return switch_jb_get_frames(rtp_session->vb, min_frame_len, max_frame_len, cur_frame_len, highest_frame_len); | |||
4850 | } | |||
4851 | ||||
4852 | return SWITCH_STATUS_FALSE; | |||
4853 | } | |||
4854 | ||||
4855 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_set_video_buffer_size(switch_rtp_t *rtp_session, uint32_t frames, uint32_t max_frames) | |||
4856 | { | |||
4857 | if (!switch_rtp_ready(rtp_session)) { | |||
4858 | return SWITCH_STATUS_FALSE; | |||
4859 | } | |||
4860 | ||||
4861 | if (!max_frames) { | |||
4862 | max_frames = rtp_session->last_max_vb_frames; | |||
4863 | } | |||
4864 | ||||
4865 | if (!max_frames || frames >= max_frames) { | |||
4866 | max_frames = frames * 10; | |||
4867 | } | |||
4868 | ||||
4869 | rtp_session->last_max_vb_frames = max_frames; | |||
4870 | ||||
4871 | if (!rtp_session->vb) { | |||
4872 | switch_jb_create(&rtp_session->vb, rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? SJB_TEXT : SJB_VIDEO, frames, max_frames, rtp_session->pool); | |||
4873 | switch_jb_set_session(rtp_session->vb, rtp_session->session); | |||
4874 | } else { | |||
4875 | switch_jb_set_frames(rtp_session->vb, frames, max_frames); | |||
4876 | } | |||
4877 | ||||
4878 | //switch_rtp_flush(rtp_session); | |||
4879 | switch_core_session_request_video_refresh(rtp_session->session)_switch_core_session_request_video_refresh(rtp_session->session , 0, "src/switch_rtp.c", (const char *)__func__, 4879); | |||
4880 | ||||
4881 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4881, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, "Setting video buffer %u Frames.\n", frames); | |||
4882 | ||||
4883 | return SWITCH_STATUS_SUCCESS; | |||
4884 | } | |||
4885 | ||||
4886 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_debug_jitter_buffer(switch_rtp_t *rtp_session, const char *name) | |||
4887 | { | |||
4888 | int x = 0; | |||
4889 | ||||
4890 | if (!switch_rtp_ready(rtp_session)) { | |||
4891 | return SWITCH_STATUS_FALSE; | |||
4892 | } | |||
4893 | ||||
4894 | if (name) x = atoi(name); | |||
4895 | if (x < 0) x = 0; | |||
4896 | ||||
4897 | if (rtp_session->jb) { | |||
4898 | switch_jb_debug_level(rtp_session->jb, x); | |||
4899 | } else if (rtp_session->vb) { | |||
4900 | switch_jb_debug_level(rtp_session->vb, x); | |||
4901 | } | |||
4902 | ||||
4903 | return SWITCH_STATUS_SUCCESS; | |||
4904 | } | |||
4905 | ||||
4906 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_activate_jitter_buffer(switch_rtp_t *rtp_session, | |||
4907 | uint32_t queue_frames, | |||
4908 | uint32_t max_queue_frames, | |||
4909 | uint32_t samples_per_packet, | |||
4910 | uint32_t samples_per_second) | |||
4911 | { | |||
4912 | switch_status_t status = SWITCH_STATUS_FALSE; | |||
4913 | ||||
4914 | if (!switch_rtp_ready(rtp_session)) { | |||
4915 | return SWITCH_STATUS_FALSE; | |||
4916 | } | |||
4917 | ||||
4918 | if (queue_frames < 1) { | |||
4919 | queue_frames = 3; | |||
4920 | } | |||
4921 | ||||
4922 | if (max_queue_frames < queue_frames) { | |||
4923 | max_queue_frames = queue_frames * 3; | |||
4924 | } | |||
4925 | ||||
4926 | ||||
4927 | ||||
4928 | if (rtp_session->jb) { | |||
4929 | status = switch_jb_set_frames(rtp_session->jb, queue_frames, max_queue_frames); | |||
4930 | } else { | |||
4931 | READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session-> reading++; | |||
4932 | status = switch_jb_create(&rtp_session->jb, SJB_AUDIO, queue_frames, max_queue_frames, rtp_session->pool); | |||
4933 | switch_jb_set_session(rtp_session->jb, rtp_session->session); | |||
4934 | if (switch_true(switch_channel_get_variable_dup(switch_core_session_get_channel(rtp_session->session), "jb_use_timestamps", SWITCH_FALSE, -1))) { | |||
4935 | switch_jb_ts_mode(rtp_session->jb, samples_per_packet, samples_per_second); | |||
4936 | } | |||
4937 | //switch_jb_debug_level(rtp_session->jb, 10); | |||
4938 | READ_DEC(rtp_session)rtp_session->reading--; switch_mutex_unlock(rtp_session-> read_mutex); | |||
4939 | } | |||
4940 | ||||
4941 | ||||
4942 | ||||
4943 | return status; | |||
4944 | } | |||
4945 | ||||
4946 | SWITCH_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) | |||
4947 | { | |||
4948 | const char *err = NULL((void*)0); | |||
4949 | ||||
4950 | if (!rtp_session->ms_per_packet) { | |||
4951 | return SWITCH_STATUS_FALSE; | |||
4952 | } | |||
4953 | ||||
4954 | rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] = 1; | |||
4955 | ||||
4956 | if (!(rtp_session->remote_rtcp_port = remote_port)) { | |||
4957 | rtp_session->remote_rtcp_port = rtp_session->remote_port + 1; | |||
4958 | } | |||
4959 | ||||
4960 | if (mux) { | |||
4961 | rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]++; | |||
4962 | } | |||
4963 | ||||
4964 | ||||
4965 | if (send_rate == -1) { | |||
4966 | ||||
4967 | rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] = 1; | |||
4968 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4968, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "RTCP passthru enabled. Remote Port: %d\n", rtp_session->remote_rtcp_port); | |||
4969 | } else { | |||
4970 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4970, (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); | |||
4971 | ||||
4972 | rtp_session->rtcp_interval = send_rate; | |||
4973 | } | |||
4974 | ||||
4975 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
4976 | ||||
4977 | if (switch_sockaddr_info_get(&rtp_session->rtcp_remote_addr, rtp_session->eff_remote_host_str, SWITCH_UNSPEC0, | |||
4978 | rtp_session->remote_rtcp_port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !rtp_session->rtcp_remote_addr) { | |||
4979 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 4979, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "RTCP MUX Remote Address Error!"); | |||
4980 | return SWITCH_STATUS_FALSE; | |||
4981 | } | |||
4982 | ||||
4983 | rtp_session->rtcp_local_addr = rtp_session->local_addr; | |||
4984 | rtp_session->rtcp_from_addr = rtp_session->from_addr; | |||
4985 | rtp_session->rtcp_sock_input = rtp_session->sock_input; | |||
4986 | rtp_session->rtcp_sock_output = rtp_session->sock_output; | |||
4987 | ||||
4988 | rtp_session->rtcp_recv_msg_p = (rtcp_msg_t *) &rtp_session->recv_msg; | |||
4989 | ||||
4990 | return SWITCH_STATUS_SUCCESS; | |||
4991 | ||||
4992 | //return enable_remote_rtcp_socket(rtp_session, &err); | |||
4993 | } else { | |||
4994 | rtp_session->rtcp_recv_msg_p = (rtcp_msg_t *) &rtp_session->rtcp_recv_msg; | |||
4995 | } | |||
4996 | ||||
4997 | return enable_local_rtcp_socket(rtp_session, &err) || enable_remote_rtcp_socket(rtp_session, &err); | |||
4998 | ||||
4999 | } | |||
5000 | ||||
5001 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_activate_ice(switch_rtp_t *rtp_session, char *login, char *rlogin, | |||
5002 | const char *password, const char *rpassword, ice_proto_t proto, | |||
5003 | switch_core_media_ice_type_t type, ice_t *ice_params) | |||
5004 | { | |||
5005 | char ice_user[STUN_USERNAME_MAX_SIZE513]; | |||
5006 | char user_ice[STUN_USERNAME_MAX_SIZE513]; | |||
5007 | char luser_ice[SDP_UFRAG_MAX_SIZE256]; | |||
5008 | switch_rtp_ice_t *ice; | |||
5009 | char *host = NULL((void*)0); | |||
5010 | switch_port_t port = 0; | |||
5011 | char bufc[50]; | |||
5012 | ||||
5013 | ||||
5014 | switch_mutex_lock(rtp_session->ice_mutex); | |||
5015 | ||||
5016 | if (proto == IPR_RTP) { | |||
5017 | ice = &rtp_session->ice; | |||
5018 | rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] = 0; | |||
5019 | rtp_session->flags[SWITCH_RTP_FLAG_MUTE] = 0; | |||
5020 | } else { | |||
5021 | ice = &rtp_session->rtcp_ice; | |||
5022 | } | |||
5023 | ||||
5024 | ice->proto = proto; | |||
5025 | ||||
5026 | if ((type & ICE_VANILLA)) { | |||
5027 | switch_snprintf(ice_user, sizeof(ice_user), "%s:%s", login, rlogin); | |||
5028 | switch_snprintf(user_ice, sizeof(user_ice), "%s:%s", rlogin, login); | |||
5029 | switch_snprintf(luser_ice, sizeof(user_ice), "%s%s", rlogin, login); | |||
5030 | ice->ready = ice->rready = 0; | |||
5031 | } else { | |||
5032 | switch_snprintf(ice_user, sizeof(ice_user), "%s%s", login, rlogin); | |||
5033 | switch_snprintf(user_ice, sizeof(user_ice), "%s%s", rlogin, login); | |||
5034 | ice->ready = ice->rready = 1; | |||
5035 | } | |||
5036 | ||||
5037 | 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__, 5037); | |||
5038 | 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__, 5038); | |||
5039 | 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__, 5039); | |||
5040 | ice->type = type; | |||
5041 | ice->ice_params = ice_params; | |||
5042 | ice->pass = ""; | |||
5043 | ice->rpass = ""; | |||
5044 | ice->next_run = switch_micro_time_now(); | |||
5045 | ||||
5046 | if (password) { | |||
5047 | ice->pass = switch_core_strdup(rtp_session->pool, password)switch_core_perform_strdup(rtp_session->pool, password, "src/switch_rtp.c" , (const char *)__func__, 5047); | |||
5048 | } | |||
5049 | ||||
5050 | if (rpassword) { | |||
5051 | ice->rpass = switch_core_strdup(rtp_session->pool, rpassword)switch_core_perform_strdup(rtp_session->pool, rpassword, "src/switch_rtp.c" , (const char *)__func__, 5051); | |||
5052 | } | |||
5053 | ||||
5054 | if ((ice->type & ICE_VANILLA) && ice->ice_params) { | |||
5055 | host = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr; | |||
5056 | port = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port; | |||
5057 | ||||
5058 | if (!host || !port || switch_sockaddr_info_get(&ice->addr, host, SWITCH_UNSPEC0, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !ice->addr) { | |||
5059 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
5060 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 5060, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error setting remote host!\n"); | |||
5061 | return SWITCH_STATUS_FALSE; | |||
5062 | } | |||
5063 | } else { | |||
5064 | if (proto == IPR_RTP) { | |||
5065 | ice->addr = rtp_session->remote_addr; | |||
5066 | } else { | |||
5067 | ice->addr = rtp_session->rtcp_remote_addr; | |||
5068 | } | |||
5069 | ||||
5070 | host = (char *)switch_get_addr(bufc, sizeof(bufc), ice->addr); | |||
5071 | port = switch_sockaddr_get_port(ice->addr); | |||
5072 | } | |||
5073 | ||||
5074 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 5074, (const char*)(rtp_session->session), SWITCH_LOG_NOTICE, "Activating %s %s ICE: %s %s:%d\n", | |||
5075 | proto == IPR_RTP ? "RTP" : "RTCP", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), ice_user, host, port); | |||
5076 | ||||
5077 | ||||
5078 | rtp_session->rtp_bugs |= RTP_BUG_ACCEPT_ANY_PACKETS; | |||
5079 | ||||
5080 | ||||
5081 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
5082 | rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK] = 1; | |||
5083 | switch_rtp_break(rtp_session); | |||
5084 | } | |||
5085 | ||||
5086 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
5087 | ||||
5088 | return SWITCH_STATUS_SUCCESS; | |||
5089 | } | |||
5090 | ||||
5091 | ||||
5092 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_flush(switch_rtp_t *rtp_session) | |||
5093 | { | |||
5094 | if (!switch_rtp_ready(rtp_session)) { | |||
5095 | return; | |||
5096 | } | |||
5097 | ||||
5098 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH); | |||
5099 | } | |||
5100 | ||||
5101 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_req_bitrate(switch_rtp_t *rtp_session, uint32_t bps) | |||
5102 | { | |||
5103 | if (!rtp_write_ready(rtp_session, 0, __LINE__5103) || rtp_session->tmmbr) { | |||
5104 | return SWITCH_STATUS_FALSE; | |||
5105 | } | |||
5106 | ||||
5107 | rtp_session->tmmbr = bps; | |||
5108 | ||||
5109 | return SWITCH_STATUS_SUCCESS; | |||
5110 | } | |||
5111 | ||||
5112 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_ack_bitrate(switch_rtp_t *rtp_session, uint32_t bps) | |||
5113 | { | |||
5114 | if (!rtp_write_ready(rtp_session, 0, __LINE__5114) || rtp_session->tmmbn) { | |||
5115 | return SWITCH_STATUS_FALSE; | |||
5116 | } | |||
5117 | ||||
5118 | rtp_session->tmmbn = bps; | |||
5119 | ||||
5120 | return SWITCH_STATUS_SUCCESS; | |||
5121 | } | |||
5122 | ||||
5123 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_video_refresh(switch_rtp_t *rtp_session) | |||
5124 | { | |||
5125 | ||||
5126 | if (!rtp_write_ready(rtp_session, 0, __LINE__5126)) { | |||
5127 | return; | |||
5128 | } | |||
5129 | ||||
5130 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (rtp_session->ice.ice_user || rtp_session->flags[SWITCH_RTP_FLAG_FIR] || rtp_session->flags[SWITCH_RTP_FLAG_OLD_FIR])) { | |||
5131 | rtp_session->fir_count = 1; | |||
5132 | } | |||
5133 | } | |||
5134 | ||||
5135 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_video_loss(switch_rtp_t *rtp_session) | |||
5136 | { | |||
5137 | ||||
5138 | if (!rtp_write_ready(rtp_session, 0, __LINE__5138)) { | |||
5139 | return; | |||
5140 | } | |||
5141 | ||||
5142 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (rtp_session->ice.ice_user || rtp_session->flags[SWITCH_RTP_FLAG_PLI])) { | |||
5143 | rtp_session->pli_count = 1; | |||
5144 | } | |||
5145 | } | |||
5146 | ||||
5147 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_break(switch_rtp_t *rtp_session) | |||
5148 | { | |||
5149 | if (!switch_rtp_ready(rtp_session)) { | |||
5150 | return; | |||
5151 | } | |||
5152 | ||||
5153 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
5154 | int ret = 1; | |||
5155 | ||||
5156 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK]) { | |||
5157 | rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_BREAK] = 0; | |||
5158 | ret = 0; | |||
5159 | } else if (rtp_session->session) { | |||
5160 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
5161 | if (switch_channel_test_flag(channel, CF_VIDEO_BREAK)) { | |||
5162 | switch_channel_clear_flag(channel, CF_VIDEO_BREAK); | |||
5163 | ret = 0; | |||
5164 | } | |||
5165 | } | |||
5166 | ||||
5167 | if (ret) return; | |||
5168 | ||||
5169 | switch_rtp_video_refresh(rtp_session); | |||
5170 | } | |||
5171 | ||||
5172 | switch_mutex_lock(rtp_session->flag_mutex); | |||
5173 | rtp_session->flags[SWITCH_RTP_FLAG_BREAK] = 1; | |||
5174 | ||||
5175 | if (rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK]) { | |||
5176 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
5177 | return; | |||
5178 | } | |||
5179 | ||||
5180 | if (rtp_session->sock_input) { | |||
5181 | ping_socket(rtp_session); | |||
5182 | } | |||
5183 | ||||
5184 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
5185 | } | |||
5186 | ||||
5187 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_kill_socket(switch_rtp_t *rtp_session) | |||
5188 | { | |||
5189 | switch_assert(rtp_session != NULL)((rtp_session != ((void*)0)) ? (void) (0) : __assert_fail ("rtp_session != ((void*)0)" , "src/switch_rtp.c", 5189, __extension__ __PRETTY_FUNCTION__ )); | |||
5190 | switch_mutex_lock(rtp_session->flag_mutex); | |||
5191 | if (rtp_session->flags[SWITCH_RTP_FLAG_IO]) { | |||
5192 | rtp_session->flags[SWITCH_RTP_FLAG_IO] = 0; | |||
5193 | if (rtp_session->sock_input) { | |||
5194 | ping_socket(rtp_session); | |||
5195 | switch_socket_shutdown(rtp_session->sock_input, SWITCH_SHUTDOWN_READWRITE); | |||
5196 | } | |||
5197 | if (rtp_session->sock_output && rtp_session->sock_output != rtp_session->sock_input) { | |||
5198 | switch_socket_shutdown(rtp_session->sock_output, SWITCH_SHUTDOWN_READWRITE); | |||
5199 | } | |||
5200 | ||||
5201 | if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
5202 | if (rtp_session->rtcp_sock_input && rtp_session->rtcp_sock_input != rtp_session->sock_input) { | |||
5203 | ping_socket(rtp_session); | |||
5204 | switch_socket_shutdown(rtp_session->rtcp_sock_input, SWITCH_SHUTDOWN_READWRITE); | |||
5205 | } | |||
5206 | if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) { | |||
5207 | switch_socket_shutdown(rtp_session->rtcp_sock_output, SWITCH_SHUTDOWN_READWRITE); | |||
5208 | } | |||
5209 | } | |||
5210 | } | |||
5211 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
5212 | } | |||
5213 | ||||
5214 | SWITCH_DECLARE(uint8_t)__attribute__((visibility("default"))) uint8_t switch_rtp_ready(switch_rtp_t *rtp_session) | |||
5215 | { | |||
5216 | uint8_t ret; | |||
5217 | ||||
5218 | if (!rtp_session || !rtp_session->flag_mutex || rtp_session->flags[SWITCH_RTP_FLAG_SHUTDOWN]) { | |||
5219 | return 0; | |||
5220 | } | |||
5221 | ||||
5222 | switch_mutex_lock(rtp_session->flag_mutex); | |||
5223 | ret = (rtp_session->flags[SWITCH_RTP_FLAG_IO] && rtp_session->sock_input && rtp_session->sock_output && rtp_session->remote_addr | |||
5224 | && rtp_session->ready == 2) ? 1 : 0; | |||
5225 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
5226 | ||||
5227 | return ret; | |||
5228 | } | |||
5229 | ||||
5230 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_sync_stats(switch_rtp_t *rtp_session) | |||
5231 | { | |||
5232 | if (!rtp_session) { | |||
5233 | return SWITCH_STATUS_FALSE; | |||
5234 | } | |||
5235 | ||||
5236 | if (rtp_session->flags[SWITCH_RTP_FLAG_VAD]) { | |||
5237 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->vad_data.session); | |||
5238 | ||||
5239 | switch_channel_set_variable_printf(channel, "vad_total_talk_time_ms", "%u", (uint32_t)rtp_session->vad_data.total_talk_time / 1000); | |||
5240 | switch_channel_set_variable_printf(channel, "vad_total_talk_time_sec", "%u", (uint32_t)rtp_session->vad_data.total_talk_time / 1000000); | |||
5241 | } | |||
5242 | ||||
5243 | do_mos(rtp_session); | |||
5244 | ||||
5245 | if (rtp_session->stats.inbound.error_log && !rtp_session->stats.inbound.error_log->stop) { | |||
5246 | rtp_session->stats.inbound.error_log->stop = switch_micro_time_now(); | |||
5247 | } | |||
5248 | ||||
5249 | return SWITCH_STATUS_SUCCESS; | |||
5250 | } | |||
5251 | ||||
5252 | ||||
5253 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_destroy(switch_rtp_t **rtp_session) | |||
5254 | { | |||
5255 | void *pop; | |||
5256 | switch_socket_t *sock; | |||
5257 | #ifdef ENABLE_SRTP1 | |||
5258 | int x; | |||
5259 | #endif | |||
5260 | ||||
5261 | if (!rtp_session || !*rtp_session || !(*rtp_session)->ready) { | |||
5262 | return; | |||
5263 | } | |||
5264 | ||||
5265 | if ((*rtp_session)->vb) { | |||
5266 | /* retrieve counter for ALL received NACKed packets */ | |||
5267 | uint32_t nack_jb_ok = switch_jb_get_nack_success((*rtp_session)->vb); | |||
5268 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG((*rtp_session)->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 5268, (const char*)((*rtp_session)->session), SWITCH_LOG_DEBUG, | |||
5269 | "NACK: Added to JB: [%u]\n", nack_jb_ok); | |||
5270 | } | |||
5271 | ||||
5272 | (*rtp_session)->flags[SWITCH_RTP_FLAG_SHUTDOWN] = 1; | |||
5273 | ||||
5274 | READ_INC((*rtp_session))switch_mutex_lock((*rtp_session)->read_mutex); (*rtp_session )->reading++; | |||
5275 | WRITE_INC((*rtp_session))switch_mutex_lock((*rtp_session)->write_mutex); (*rtp_session )->writing++; | |||
5276 | ||||
5277 | (*rtp_session)->ready = 0; | |||
5278 | ||||
5279 | WRITE_DEC((*rtp_session))(*rtp_session)->writing--; switch_mutex_unlock((*rtp_session )->write_mutex); | |||
5280 | READ_DEC((*rtp_session))(*rtp_session)->reading--; switch_mutex_unlock((*rtp_session )->read_mutex); | |||
5281 | ||||
5282 | if ((*rtp_session)->flags[SWITCH_RTP_FLAG_VAD]) { | |||
5283 | switch_rtp_disable_vad(*rtp_session); | |||
5284 | } | |||
5285 | ||||
5286 | switch_mutex_lock((*rtp_session)->flag_mutex); | |||
5287 | ||||
5288 | switch_rtp_kill_socket(*rtp_session); | |||
5289 | ||||
5290 | while (switch_queue_trypop((*rtp_session)->dtmf_data.dtmf_inqueue, &pop) == SWITCH_STATUS_SUCCESS) { | |||
5291 | switch_safe_free(pop)if (pop) {free(pop);pop=((void*)0);}; | |||
5292 | } | |||
5293 | ||||
5294 | while (switch_queue_trypop((*rtp_session)->dtmf_data.dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) { | |||
5295 | switch_safe_free(pop)if (pop) {free(pop);pop=((void*)0);}; | |||
5296 | } | |||
5297 | ||||
5298 | if ((*rtp_session)->jb) { | |||
5299 | switch_jb_destroy(&(*rtp_session)->jb); | |||
5300 | } | |||
5301 | ||||
5302 | if ((*rtp_session)->vb) { | |||
5303 | switch_jb_destroy(&(*rtp_session)->vb); | |||
5304 | } | |||
5305 | ||||
5306 | if ((*rtp_session)->vbw) { | |||
5307 | switch_jb_destroy(&(*rtp_session)->vbw); | |||
5308 | } | |||
5309 | ||||
5310 | if ((*rtp_session)->dtls && (*rtp_session)->dtls == (*rtp_session)->rtcp_dtls) { | |||
5311 | (*rtp_session)->rtcp_dtls = NULL((void*)0); | |||
5312 | } | |||
5313 | ||||
5314 | if ((*rtp_session)->dtls) { | |||
5315 | free_dtls(&(*rtp_session)->dtls); | |||
5316 | } | |||
5317 | ||||
5318 | if ((*rtp_session)->rtcp_dtls) { | |||
5319 | free_dtls(&(*rtp_session)->rtcp_dtls); | |||
5320 | } | |||
5321 | ||||
5322 | if ((*rtp_session)->rtcp_sock_input == (*rtp_session)->sock_input) { | |||
5323 | (*rtp_session)->rtcp_sock_input = NULL((void*)0); | |||
5324 | } | |||
5325 | ||||
5326 | if ((*rtp_session)->rtcp_sock_output == (*rtp_session)->sock_output) { | |||
5327 | (*rtp_session)->rtcp_sock_output = NULL((void*)0); | |||
5328 | } | |||
5329 | ||||
5330 | sock = (*rtp_session)->sock_input; | |||
5331 | (*rtp_session)->sock_input = NULL((void*)0); | |||
5332 | switch_socket_close(sock); | |||
5333 | ||||
5334 | if ((*rtp_session)->sock_output != sock) { | |||
5335 | sock = (*rtp_session)->sock_output; | |||
5336 | (*rtp_session)->sock_output = NULL((void*)0); | |||
5337 | switch_socket_close(sock); | |||
5338 | } | |||
5339 | ||||
5340 | if ((sock = (*rtp_session)->rtcp_sock_input)) { | |||
5341 | (*rtp_session)->rtcp_sock_input = NULL((void*)0); | |||
5342 | switch_socket_close(sock); | |||
5343 | } | |||
5344 | ||||
5345 | if ((*rtp_session)->rtcp_sock_output && (*rtp_session)->rtcp_sock_output != sock) { | |||
5346 | sock = (*rtp_session)->rtcp_sock_output; | |||
5347 | (*rtp_session)->rtcp_sock_output = NULL((void*)0); | |||
5348 | switch_socket_close(sock); | |||
5349 | } | |||
5350 | ||||
5351 | #ifdef ENABLE_SRTP1 | |||
5352 | if ((*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_SEND]) { | |||
5353 | for(x = 0; x < 2; x++) { | |||
5354 | if ((*rtp_session)->send_ctx[x]) { | |||
5355 | srtp_dealloc((*rtp_session)->send_ctx[x]); | |||
5356 | (*rtp_session)->send_ctx[x] = NULL((void*)0); | |||
5357 | } | |||
5358 | } | |||
5359 | (*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0; | |||
5360 | } | |||
5361 | ||||
5362 | if ((*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_RECV]) { | |||
5363 | for (x = 0; x < 2; x++) { | |||
5364 | if ((*rtp_session)->recv_ctx[x]) { | |||
5365 | srtp_dealloc((*rtp_session)->recv_ctx[x]); | |||
5366 | (*rtp_session)->recv_ctx[x] = NULL((void*)0); | |||
5367 | } | |||
5368 | } | |||
5369 | (*rtp_session)->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0; | |||
5370 | } | |||
5371 | #endif | |||
5372 | ||||
5373 | #ifdef ENABLE_ZRTP | |||
5374 | /* ZRTP */ | |||
5375 | if (zrtp_on && !(*rtp_session)->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) { | |||
5376 | ||||
5377 | if ((*rtp_session)->zrtp_stream != NULL((void*)0)) { | |||
5378 | zrtp_stream_stop((*rtp_session)->zrtp_stream); | |||
5379 | } | |||
5380 | ||||
5381 | if ((*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_SEND]) { | |||
5382 | (*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_SEND] = 0; | |||
5383 | } | |||
5384 | ||||
5385 | if ((*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_RECV]) { | |||
5386 | (*rtp_session)->flags[SWITCH_ZRTP_FLAG_SECURE_RECV] = 0; | |||
5387 | } | |||
5388 | ||||
5389 | if ((*rtp_session)->zrtp_session) { | |||
5390 | zrtp_session_down((*rtp_session)->zrtp_session); | |||
5391 | (*rtp_session)->zrtp_session = NULL((void*)0); | |||
5392 | } | |||
5393 | } | |||
5394 | #endif | |||
5395 | if ((*rtp_session)->timer.timer_interface) { | |||
5396 | switch_core_timer_destroy(&(*rtp_session)->timer); | |||
5397 | } | |||
5398 | ||||
5399 | if ((*rtp_session)->write_timer.timer_interface) { | |||
5400 | switch_core_timer_destroy(&(*rtp_session)->write_timer); | |||
5401 | } | |||
5402 | ||||
5403 | switch_rtp_release_port((*rtp_session)->rx_host, (*rtp_session)->rx_port); | |||
5404 | switch_mutex_unlock((*rtp_session)->flag_mutex); | |||
5405 | ||||
5406 | return; | |||
5407 | } | |||
5408 | ||||
5409 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_interdigit_delay(switch_rtp_t *rtp_session, uint32_t delay) | |||
5410 | { | |||
5411 | rtp_session->interdigit_delay = delay; | |||
5412 | } | |||
5413 | ||||
5414 | SWITCH_DECLARE(switch_socket_t *)__attribute__((visibility("default"))) switch_socket_t * switch_rtp_get_rtp_socket(switch_rtp_t *rtp_session) | |||
5415 | { | |||
5416 | return rtp_session->sock_input; | |||
5417 | } | |||
5418 | ||||
5419 | SWITCH_DECLARE(uint32_t)__attribute__((visibility("default"))) uint32_t switch_rtp_get_default_samples_per_interval(switch_rtp_t *rtp_session) | |||
5420 | { | |||
5421 | return rtp_session->samples_per_interval; | |||
5422 | } | |||
5423 | ||||
5424 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_default_payload(switch_rtp_t *rtp_session, switch_payload_t payload) | |||
5425 | { | |||
5426 | rtp_session->payload = payload; | |||
5427 | } | |||
5428 | ||||
5429 | SWITCH_DECLARE(uint32_t)__attribute__((visibility("default"))) uint32_t switch_rtp_get_default_payload(switch_rtp_t *rtp_session) | |||
5430 | { | |||
5431 | return rtp_session->payload; | |||
5432 | } | |||
5433 | ||||
5434 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_invalid_handler(switch_rtp_t *rtp_session, switch_rtp_invalid_handler_t on_invalid) | |||
5435 | { | |||
5436 | rtp_session->invalid_handler = on_invalid; | |||
5437 | } | |||
5438 | ||||
5439 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_flags(switch_rtp_t *rtp_session, switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID]) | |||
5440 | { | |||
5441 | int i; | |||
5442 | ||||
5443 | for(i = 0; i < SWITCH_RTP_FLAG_INVALID; i++) { | |||
5444 | if (flags[i]) { | |||
5445 | switch_rtp_set_flag(rtp_session, i); | |||
5446 | } | |||
5447 | } | |||
5448 | } | |||
5449 | ||||
5450 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_clear_flags(switch_rtp_t *rtp_session, switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID]) | |||
5451 | { | |||
5452 | int i; | |||
5453 | ||||
5454 | for(i = 0; i < SWITCH_RTP_FLAG_INVALID; i++) { | |||
5455 | if (flags[i]) { | |||
5456 | switch_rtp_clear_flag(rtp_session, i); | |||
5457 | } | |||
5458 | } | |||
5459 | } | |||
5460 | ||||
5461 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag) | |||
5462 | { | |||
5463 | int old_flag = rtp_session->flags[flag]; | |||
5464 | ||||
5465 | switch_mutex_lock(rtp_session->flag_mutex); | |||
5466 | rtp_session->flags[flag] = 1; | |||
5467 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
5468 | ||||
5469 | if (flag == SWITCH_RTP_FLAG_PASSTHRU) { | |||
5470 | if (!old_flag) { | |||
5471 | switch_rtp_pause_jitter_buffer(rtp_session, SWITCH_TRUE); | |||
5472 | } | |||
5473 | } else if (flag == SWITCH_RTP_FLAG_DTMF_ON) { | |||
5474 | rtp_session->stats.inbound.last_processed_seq = 0; | |||
5475 | } else if (flag == SWITCH_RTP_FLAG_FLUSH) { | |||
5476 | reset_jitter_seq(rtp_session); | |||
5477 | } else if (flag == SWITCH_RTP_FLAG_AUTOADJ) { | |||
5478 | rtp_session->autoadj_window = 20; | |||
5479 | rtp_session->autoadj_threshold = 10; | |||
5480 | rtp_session->autoadj_tally = 0; | |||
5481 | ||||
5482 | switch_mutex_lock(rtp_session->flag_mutex); | |||
5483 | rtp_session->flags[SWITCH_RTP_FLAG_RTCP_AUTOADJ] = 1; | |||
5484 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
5485 | ||||
5486 | rtp_session->rtcp_autoadj_window = 20; | |||
5487 | rtp_session->rtcp_autoadj_threshold = 1; | |||
5488 | rtp_session->rtcp_autoadj_tally = 0; | |||
5489 | ||||
5490 | if (rtp_session->session) { | |||
5491 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
5492 | 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); | |||
5493 | if (x && *x) { | |||
5494 | int xn = atoi(x); | |||
5495 | if (xn > 0 && xn <= 65535) { | |||
5496 | rtp_session->autoadj_window = xn*2; | |||
5497 | rtp_session->autoadj_threshold = xn; | |||
5498 | } | |||
5499 | } | |||
5500 | } | |||
5501 | ||||
5502 | ||||
5503 | rtp_flush_read_buffer(rtp_session, SWITCH_RTP_FLUSH_ONCE); | |||
5504 | ||||
5505 | ||||
5506 | if (rtp_session->jb) { | |||
5507 | switch_jb_reset(rtp_session->jb); | |||
5508 | } | |||
5509 | } else if (flag == SWITCH_RTP_FLAG_NOBLOCK && rtp_session->sock_input) { | |||
5510 | switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, TRUE(!0)); | |||
5511 | } | |||
5512 | ||||
5513 | } | |||
5514 | ||||
5515 | SWITCH_DECLARE(uint32_t)__attribute__((visibility("default"))) uint32_t switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags) | |||
5516 | { | |||
5517 | return (uint32_t) rtp_session->flags[flags]; | |||
5518 | } | |||
5519 | ||||
5520 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag) | |||
5521 | { | |||
5522 | int old_flag = rtp_session->flags[flag]; | |||
5523 | ||||
5524 | switch_mutex_lock(rtp_session->flag_mutex); | |||
5525 | rtp_session->flags[flag] = 0; | |||
5526 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
5527 | ||||
5528 | if (flag == SWITCH_RTP_FLAG_PASSTHRU) { | |||
5529 | if (old_flag) { | |||
5530 | switch_rtp_pause_jitter_buffer(rtp_session, SWITCH_FALSE); | |||
5531 | } | |||
5532 | } else if (flag == SWITCH_RTP_FLAG_DTMF_ON) { | |||
5533 | rtp_session->stats.inbound.last_processed_seq = 0; | |||
5534 | } else if (flag == SWITCH_RTP_FLAG_PAUSE) { | |||
5535 | reset_jitter_seq(rtp_session); | |||
5536 | } else if (flag == SWITCH_RTP_FLAG_NOBLOCK && rtp_session->sock_input) { | |||
5537 | switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, FALSE0); | |||
5538 | } | |||
5539 | } | |||
5540 | ||||
5541 | static void set_dtmf_delay(switch_rtp_t *rtp_session, uint32_t ms, uint32_t max_ms) | |||
5542 | { | |||
5543 | int upsamp, max_upsamp; | |||
5544 | ||||
5545 | ||||
5546 | if (!max_ms) max_ms = ms; | |||
5547 | ||||
5548 | upsamp = ms * (rtp_session->samples_per_second / 1000); | |||
5549 | max_upsamp = max_ms * (rtp_session->samples_per_second / 1000); | |||
5550 | ||||
5551 | rtp_session->sending_dtmf = 0; | |||
5552 | rtp_session->queue_delay = upsamp; | |||
5553 | ||||
5554 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { | |||
5555 | rtp_session->max_next_write_samplecount = rtp_session->timer.samplecount + max_upsamp; | |||
5556 | rtp_session->next_write_samplecount = rtp_session->timer.samplecount + upsamp; | |||
5557 | rtp_session->last_write_ts += upsamp; | |||
5558 | } | |||
5559 | ||||
5560 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 5560, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "Queue digit delay of %dms\n", ms); | |||
5561 | } | |||
5562 | ||||
5563 | static void do_2833(switch_rtp_t *rtp_session) | |||
5564 | { | |||
5565 | switch_frame_flag_t flags = 0; | |||
5566 | uint32_t samples = rtp_session->samples_per_interval; | |||
5567 | ||||
5568 | if (rtp_session->dtmf_data.out_digit_dur > 0) { | |||
5569 | int x, loops = 1; | |||
5570 | ||||
5571 | if (!rtp_session->last_write_ts) { | |||
5572 | if (rtp_session->timer.timer_interface) { | |||
5573 | //switch_core_timer_sync(&rtp_session->write_timer); | |||
5574 | rtp_session->last_write_ts = rtp_session->write_timer.samplecount; | |||
5575 | } else { | |||
5576 | rtp_session->last_write_ts = rtp_session->samples_per_interval; | |||
5577 | } | |||
5578 | } | |||
5579 | ||||
5580 | rtp_session->dtmf_data.out_digit_sofar += samples; | |||
5581 | rtp_session->dtmf_data.out_digit_sub_sofar += samples; | |||
5582 | ||||
5583 | if (rtp_session->dtmf_data.out_digit_sub_sofar > 0xFFFF) { | |||
5584 | rtp_session->dtmf_data.out_digit_sub_sofar = samples; | |||
5585 | rtp_session->dtmf_data.timestamp_dtmf += 0xFFFF; | |||
5586 | } | |||
5587 | ||||
5588 | if (rtp_session->dtmf_data.out_digit_sofar >= rtp_session->dtmf_data.out_digit_dur) { | |||
5589 | rtp_session->dtmf_data.out_digit_packet[1] |= 0x80; | |||
5590 | loops = 3; | |||
5591 | } | |||
5592 | ||||
5593 | rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8); | |||
5594 | rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar; | |||
5595 | ||||
5596 | for (x = 0; x < loops; x++) { | |||
5597 | switch_size_t wrote = switch_rtp_write_manual(rtp_session, | |||
5598 | rtp_session->dtmf_data.out_digit_packet, 4, 0, | |||
5599 | rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags); | |||
5600 | ||||
5601 | rtp_session->stats.outbound.raw_bytes += wrote; | |||
5602 | rtp_session->stats.outbound.dtmf_packet_count++; | |||
5603 | ||||
5604 | if (loops == 1) { | |||
5605 | rtp_session->last_write_ts += samples; | |||
5606 | ||||
5607 | if (rtp_session->rtp_bugs & RTP_BUG_SONUS_SEND_INVALID_TIMESTAMP_2833) { | |||
5608 | rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts; | |||
5609 | } | |||
5610 | } | |||
5611 | ||||
5612 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 5612, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "Send %s packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u\n", | |||
5613 | loops == 1 ? "middle" : "end", rtp_session->dtmf_data.out_digit, | |||
5614 | rtp_session->dtmf_data.timestamp_dtmf, | |||
5615 | rtp_session->dtmf_data.out_digit_sofar, | |||
5616 | rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts); | |||
5617 | } | |||
5618 | ||||
5619 | if (loops != 1) { | |||
5620 | rtp_session->sending_dtmf = 0; | |||
5621 | rtp_session->need_mark = 1; | |||
5622 | ||||
5623 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { | |||
5624 | //switch_core_timer_sync(&rtp_session->write_timer); | |||
5625 | rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount; | |||
5626 | } | |||
5627 | ||||
5628 | rtp_session->dtmf_data.out_digit_dur = 0; | |||
5629 | ||||
5630 | if (rtp_session->interdigit_delay) { | |||
5631 | set_dtmf_delay(rtp_session, rtp_session->interdigit_delay, rtp_session->interdigit_delay * 10); | |||
5632 | } | |||
5633 | ||||
5634 | return; | |||
5635 | } | |||
5636 | } | |||
5637 | ||||
5638 | if (!rtp_session->dtmf_data.out_digit_dur && rtp_session->dtmf_data.dtmf_queue && switch_queue_size(rtp_session->dtmf_data.dtmf_queue)) { | |||
5639 | void *pop; | |||
5640 | ||||
5641 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { | |||
5642 | //switch_core_timer_sync(&rtp_session->write_timer); | |||
5643 | if (rtp_session->timer.samplecount < rtp_session->next_write_samplecount) { | |||
5644 | return; | |||
5645 | } | |||
5646 | ||||
5647 | if (rtp_session->timer.samplecount >= rtp_session->max_next_write_samplecount) { | |||
5648 | rtp_session->queue_delay = 0; | |||
5649 | } | |||
5650 | ||||
5651 | } else if (rtp_session->queue_delay) { | |||
5652 | if (rtp_session->delay_samples >= rtp_session->samples_per_interval) { | |||
5653 | rtp_session->delay_samples -= rtp_session->samples_per_interval; | |||
5654 | } else { | |||
5655 | rtp_session->delay_samples = 0; | |||
5656 | } | |||
5657 | ||||
5658 | if (!rtp_session->delay_samples) { | |||
5659 | rtp_session->queue_delay = 0; | |||
5660 | } | |||
5661 | } | |||
5662 | ||||
5663 | if (rtp_session->queue_delay) { | |||
5664 | return; | |||
5665 | } | |||
5666 | ||||
5667 | ||||
5668 | if (!rtp_session->sending_dtmf) { | |||
5669 | rtp_session->sending_dtmf = 1; | |||
5670 | } | |||
5671 | ||||
5672 | if (switch_queue_trypop(rtp_session->dtmf_data.dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) { | |||
5673 | switch_dtmf_t *rdigit = pop; | |||
5674 | switch_size_t wrote; | |||
5675 | ||||
5676 | if (rdigit->digit == 'w') { | |||
5677 | set_dtmf_delay(rtp_session, 500, 0); | |||
5678 | free(rdigit); | |||
5679 | return; | |||
5680 | } | |||
5681 | ||||
5682 | if (rdigit->digit == 'W') { | |||
5683 | set_dtmf_delay(rtp_session, 1000, 0); | |||
5684 | free(rdigit); | |||
5685 | return; | |||
5686 | } | |||
5687 | ||||
5688 | memset(rtp_session->dtmf_data.out_digit_packet, 0, 4); | |||
5689 | rtp_session->dtmf_data.out_digit_sofar = samples; | |||
5690 | rtp_session->dtmf_data.out_digit_sub_sofar = samples; | |||
5691 | rtp_session->dtmf_data.out_digit_dur = rdigit->duration; | |||
5692 | rtp_session->dtmf_data.out_digit = rdigit->digit; | |||
5693 | rtp_session->dtmf_data.out_digit_packet[0] = (unsigned char) switch_char_to_rfc2833(rdigit->digit); | |||
5694 | rtp_session->dtmf_data.out_digit_packet[1] = 13; | |||
5695 | rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8); | |||
5696 | rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar; | |||
5697 | ||||
5698 | ||||
5699 | rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts + samples; | |||
5700 | rtp_session->last_write_ts = rtp_session->dtmf_data.timestamp_dtmf; | |||
5701 | rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0; | |||
5702 | ||||
5703 | wrote = switch_rtp_write_manual(rtp_session, | |||
5704 | rtp_session->dtmf_data.out_digit_packet, | |||
5705 | 4, | |||
5706 | rtp_session->rtp_bugs & RTP_BUG_CISCO_SKIP_MARK_BIT_2833 ? 0 : 1, | |||
5707 | rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags); | |||
5708 | ||||
5709 | ||||
5710 | rtp_session->stats.outbound.raw_bytes += wrote; | |||
5711 | rtp_session->stats.outbound.dtmf_packet_count++; | |||
5712 | ||||
5713 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 5713, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "Send start packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u\n", | |||
5714 | rtp_session->dtmf_data.out_digit, | |||
5715 | rtp_session->dtmf_data.timestamp_dtmf, | |||
5716 | rtp_session->dtmf_data.out_digit_sofar, | |||
5717 | rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts); | |||
5718 | ||||
5719 | free(rdigit); | |||
5720 | } | |||
5721 | } | |||
5722 | } | |||
5723 | ||||
5724 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void rtp_flush_read_buffer(switch_rtp_t *rtp_session, switch_rtp_flush_t flush) | |||
5725 | { | |||
5726 | ||||
5727 | if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || | |||
5728 | rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
5729 | return; | |||
5730 | } | |||
5731 | ||||
5732 | ||||
5733 | if (switch_rtp_ready(rtp_session)) { | |||
5734 | rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1; | |||
5735 | rtp_session->flags[SWITCH_RTP_FLAG_FLUSH] = 1; | |||
5736 | reset_jitter_seq(rtp_session); | |||
5737 | ||||
5738 | switch (flush) { | |||
5739 | case SWITCH_RTP_FLUSH_STICK: | |||
5740 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_STICKY_FLUSH); | |||
5741 | break; | |||
5742 | case SWITCH_RTP_FLUSH_UNSTICK: | |||
5743 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_STICKY_FLUSH); | |||
5744 | break; | |||
5745 | default: | |||
5746 | break; | |||
5747 | } | |||
5748 | } | |||
5749 | } | |||
5750 | ||||
5751 | static int jb_valid(switch_rtp_t *rtp_session) | |||
5752 | { | |||
5753 | if (rtp_session->ice.ice_user) { | |||
5754 | if (!rtp_session->ice.ready && rtp_session->ice.rready) { | |||
5755 | return 0; | |||
5756 | } | |||
5757 | } | |||
5758 | ||||
5759 | if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) { | |||
5760 | return 0; | |||
5761 | } | |||
5762 | ||||
5763 | return 1; | |||
5764 | } | |||
5765 | ||||
5766 | ||||
5767 | static switch_size_t do_flush(switch_rtp_t *rtp_session, int force, switch_size_t bytes_in) | |||
5768 | { | |||
5769 | int was_blocking = 0; | |||
5770 | switch_size_t bytes; | |||
5771 | uint32_t flushed = 0; | |||
5772 | switch_size_t bytes_out = 0; | |||
5773 | ||||
5774 | if (!switch_rtp_ready(rtp_session)) { | |||
5775 | return 0; | |||
5776 | } | |||
5777 | ||||
5778 | reset_jitter_seq(rtp_session); | |||
5779 | ||||
5780 | if (!force) { | |||
5781 | if ((rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) || | |||
5782 | rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || | |||
5783 | rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] || | |||
5784 | rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] | |||
5785 | ) { | |||
5786 | return bytes_in; | |||
5787 | } | |||
5788 | } | |||
5789 | ||||
5790 | READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session-> reading++; | |||
5791 | ||||
5792 | if (switch_rtp_ready(rtp_session) ) { | |||
5793 | ||||
5794 | if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) { | |||
5795 | //switch_jb_reset(rtp_session->jb); | |||
5796 | bytes_out = bytes_in; | |||
5797 | goto end; | |||
5798 | } | |||
5799 | ||||
5800 | if (rtp_session->vbw) { | |||
5801 | switch_jb_reset(rtp_session->vbw); | |||
5802 | } | |||
5803 | ||||
5804 | if (rtp_session->vb) { | |||
5805 | //switch_jb_reset(rtp_session->vb); | |||
5806 | bytes_out = bytes_in; | |||
5807 | goto end; | |||
5808 | } | |||
5809 | ||||
5810 | if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) { | |||
5811 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 5811, (const char*)(rtp_session->session), | |||
5812 | SWITCH_LOG_CONSOLE, "%s FLUSH\n", | |||
5813 | rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName" | |||
5814 | ); | |||
5815 | } | |||
5816 | ||||
5817 | if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK]) { | |||
5818 | was_blocking = 1; | |||
5819 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK); | |||
5820 | switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, TRUE(!0)); | |||
5821 | } | |||
5822 | ||||
5823 | // before processing/flushing packets, if current packet is rfc2833, handle it (else it would be lost) | |||
5824 | if (bytes_in > rtp_header_len12 && rtp_session->last_rtp_hdr.version == 2 && rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) { | |||
5825 | int do_cng = 0; | |||
5826 | #ifdef DEBUG_2833 | |||
5827 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 5827, ((void*)0), SWITCH_LOG_ERROR, "*** Handling current RTP packet before flushing. seq=%u ***\n", ntohs(rtp_session->last_rtp_hdr.seq)__bswap_16 (rtp_session->last_rtp_hdr.seq)); | |||
5828 | #endif | |||
5829 | handle_rfc2833(rtp_session, bytes_in, &do_cng); | |||
5830 | } | |||
5831 | ||||
5832 | do { | |||
5833 | if (switch_rtp_ready(rtp_session)) { | |||
5834 | bytes = sizeof(rtp_msg_t); | |||
5835 | switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock_input, 0, (void *) &rtp_session->recv_msg, &bytes); | |||
5836 | ||||
5837 | if (bytes) { | |||
5838 | int do_cng = 0; | |||
5839 | ||||
5840 | if (rtp_session->media_timeout) { | |||
5841 | rtp_session->last_media = switch_micro_time_now(); | |||
5842 | } | |||
5843 | ||||
5844 | /* Make sure to handle RFC2833 packets, even if we're flushing the packets */ | |||
5845 | if (bytes > rtp_header_len12 && rtp_session->recv_msg.header.version == 2 && rtp_session->recv_msg.header.pt == rtp_session->recv_te) { | |||
5846 | rtp_session->last_rtp_hdr = rtp_session->recv_msg.header; | |||
5847 | handle_rfc2833(rtp_session, bytes, &do_cng); | |||
5848 | #ifdef DEBUG_2833 | |||
5849 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 5849, ((void*)0), SWITCH_LOG_ERROR, "*** RTP packet handled in flush loop %d ***\n", do_cng); | |||
5850 | #endif | |||
5851 | } | |||
5852 | ||||
5853 | flushed++; | |||
5854 | ||||
5855 | rtp_session->stats.inbound.raw_bytes += bytes; | |||
5856 | rtp_session->stats.inbound.flush_packet_count++; | |||
5857 | rtp_session->stats.inbound.packet_count++; | |||
5858 | } | |||
5859 | } else { | |||
5860 | break; | |||
5861 | } | |||
5862 | } while (bytes > 0); | |||
5863 | ||||
5864 | #ifdef DEBUG_2833 | |||
5865 | if (flushed) { | |||
5866 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 5866, ((void*)0), SWITCH_LOG_ERROR, "*** do_flush: total flushed packets: %ld ***\n",(long)flushed); | |||
5867 | } | |||
5868 | #endif | |||
5869 | ||||
5870 | ||||
5871 | if (was_blocking && switch_rtp_ready(rtp_session)) { | |||
5872 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_NOBLOCK); | |||
5873 | switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK8, FALSE0); | |||
5874 | } | |||
5875 | ||||
5876 | ||||
5877 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->session) { | |||
5878 | //int type = 1; // sum flags: 1 encoder; 2; decoder | |||
5879 | //switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_IO_READ, SCC_VIDEO_RESET, SCCT_INT, (void *)&type, NULL, NULL); | |||
5880 | switch_core_session_request_video_refresh(rtp_session->session)_switch_core_session_request_video_refresh(rtp_session->session , 0, "src/switch_rtp.c", (const char *)__func__, 5880); | |||
5881 | } | |||
5882 | } | |||
5883 | ||||
5884 | end: | |||
5885 | ||||
5886 | READ_DEC(rtp_session)rtp_session->reading--; switch_mutex_unlock(rtp_session-> read_mutex); | |||
5887 | ||||
5888 | return bytes_out; | |||
5889 | } | |||
5890 | ||||
5891 | static int check_recv_payload(switch_rtp_t *rtp_session) | |||
5892 | { | |||
5893 | int ok = 1; | |||
5894 | ||||
5895 | if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PAYLOAD) && rtp_session->pmaps && *rtp_session->pmaps) { | |||
5896 | payload_map_t *pmap; | |||
5897 | ok = 0; | |||
5898 | ||||
5899 | switch_mutex_lock(rtp_session->flag_mutex); | |||
5900 | ||||
5901 | for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) { | |||
5902 | if (!pmap->negotiated) { | |||
5903 | continue; | |||
5904 | } | |||
5905 | ||||
5906 | if (rtp_session->last_rtp_hdr.pt == pmap->pt) { | |||
5907 | ok = 1; | |||
5908 | } | |||
5909 | } | |||
5910 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
5911 | } | |||
5912 | ||||
5913 | return ok; | |||
5914 | } | |||
5915 | ||||
5916 | static int get_recv_payload(switch_rtp_t *rtp_session) | |||
5917 | { | |||
5918 | int r = -1; | |||
5919 | ||||
5920 | if (rtp_session->pmaps && *rtp_session->pmaps) { | |||
5921 | payload_map_t *pmap; | |||
5922 | ||||
5923 | switch_mutex_lock(rtp_session->flag_mutex); | |||
5924 | ||||
5925 | for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) { | |||
5926 | if (pmap->negotiated) { | |||
5927 | r = pmap->pt; | |||
5928 | break; | |||
5929 | } | |||
5930 | } | |||
5931 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
5932 | } | |||
5933 | ||||
5934 | return r; | |||
5935 | } | |||
5936 | ||||
5937 | #define return_cng_frame()do_cng = 1; goto timer_check do_cng = 1; goto timer_check | |||
5938 | ||||
5939 | static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags, | |||
5940 | payload_map_t **pmapP, switch_status_t poll_status, switch_bool_t return_jb_packet) | |||
5941 | { | |||
5942 | switch_status_t status = SWITCH_STATUS_FALSE; | |||
5943 | uint32_t ts = 0; | |||
5944 | unsigned char *b = NULL((void*)0); | |||
5945 | int sync = 0; | |||
5946 | switch_time_t now; | |||
5947 | switch_size_t xcheck_jitter = 0; | |||
5948 | int tries = 0; | |||
5949 | int block = 0; | |||
5950 | ||||
5951 | switch_assert(bytes)((bytes) ? (void) (0) : __assert_fail ("bytes", "src/switch_rtp.c" , 5951, __extension__ __PRETTY_FUNCTION__)); | |||
5952 | more: | |||
5953 | ||||
5954 | tries++; | |||
5955 | ||||
5956 | if (tries > 20) { | |||
5957 | if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) { | |||
5958 | switch_jb_reset(rtp_session->jb); | |||
5959 | } | |||
5960 | rtp_session->punts++; | |||
5961 | rtp_session->clean = 0; | |||
5962 | *bytes = 0; | |||
5963 | return SWITCH_STATUS_BREAK; | |||
5964 | } | |||
5965 | ||||
5966 | if (block) { | |||
5967 | int to = 20000; | |||
5968 | int fdr = 0; | |||
5969 | ||||
5970 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
5971 | to = 100000; | |||
5972 | } else { | |||
5973 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) { | |||
5974 | to = rtp_session->timer.interval * 1000; | |||
5975 | } | |||
5976 | } | |||
5977 | ||||
5978 | poll_status = switch_poll(rtp_session->read_pollfd, 1, &fdr, to); | |||
5979 | ||||
5980 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) { | |||
5981 | switch_core_timer_sync(&rtp_session->timer); | |||
5982 | } | |||
5983 | ||||
5984 | if (rtp_session->session) { | |||
5985 | switch_ivr_parse_all_messages(rtp_session->session); | |||
5986 | } | |||
5987 | ||||
5988 | block = 0; | |||
5989 | } | |||
5990 | ||||
5991 | *bytes = sizeof(rtp_msg_t); | |||
5992 | sync = 0; | |||
5993 | ||||
5994 | rtp_session->has_rtp = 0; | |||
5995 | rtp_session->has_ice = 0; | |||
5996 | rtp_session->has_rtcp = 0; | |||
5997 | if (rtp_session->dtls) { | |||
5998 | rtp_session->dtls->bytes = 0; | |||
5999 | rtp_session->dtls->data = NULL((void*)0); | |||
6000 | } | |||
6001 | memset(&rtp_session->last_rtp_hdr, 0, sizeof(rtp_session->last_rtp_hdr)); | |||
6002 | ||||
6003 | if (poll_status == SWITCH_STATUS_SUCCESS) { | |||
6004 | status = switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock_input, 0, (void *) &rtp_session->recv_msg, bytes); | |||
6005 | } else { | |||
6006 | *bytes = 0; | |||
6007 | } | |||
6008 | ||||
6009 | if (*bytes) { | |||
6010 | b = (unsigned char *) &rtp_session->recv_msg; | |||
6011 | ||||
6012 | /* version 2 probably rtp, zrtp cookie present means zrtp */ | |||
6013 | rtp_session->has_rtp = (rtp_session->recv_msg.header.version == 2 || ntohl(*(int *)(b+4))__bswap_32 (*(int *)(b+4)) == ZRTP_MAGIC_COOKIE0x5a525450); | |||
6014 | ||||
6015 | if (rtp_session->media_timeout) { | |||
6016 | rtp_session->last_media = switch_micro_time_now(); | |||
6017 | } | |||
6018 | ||||
6019 | if ((*b >= 20) && (*b <= 64)) { | |||
6020 | if (rtp_session->dtls) { | |||
6021 | rtp_session->dtls->bytes = *bytes; | |||
6022 | rtp_session->dtls->data = (void *) &rtp_session->recv_msg; | |||
6023 | } | |||
6024 | rtp_session->has_ice = 0; | |||
6025 | rtp_session->has_rtp = 0; | |||
6026 | rtp_session->has_rtcp = 0; | |||
6027 | } else if (*b == 0 || *b == 1) { | |||
6028 | rtp_session->has_ice = 1; | |||
6029 | rtp_session->has_rtp = 0; | |||
6030 | rtp_session->has_rtcp = 0; | |||
6031 | } else { | |||
6032 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
6033 | switch(rtp_session->recv_msg.header.pt) { | |||
6034 | case 64: // 192 Full INTRA-frame request. | |||
6035 | case 72: // 200 Sender report. | |||
6036 | case 73: // 201 Receiver report. | |||
6037 | case 74: // 202 Source description. | |||
6038 | case 75: // 203 Goodbye. | |||
6039 | case 76: // 204 Application-defined. | |||
6040 | case 77: // 205 Transport layer FB message. | |||
6041 | case 78: // 206 Payload-specific FB message. | |||
6042 | case 79: // 207 Extended report. | |||
6043 | rtp_session->has_rtcp = 1; | |||
6044 | rtp_session->has_rtp = 0; | |||
6045 | rtp_session->has_ice = 0; | |||
6046 | break; | |||
6047 | default: | |||
6048 | if (rtp_session->rtcp_recv_msg_p->header.version == 2 && | |||
6049 | rtp_session->rtcp_recv_msg_p->header.type > 199 && rtp_session->rtcp_recv_msg_p->header.type < 208) { | |||
6050 | rtp_session->has_rtcp = 1; | |||
6051 | rtp_session->has_rtp = 0; | |||
6052 | rtp_session->has_ice = 0; | |||
6053 | } | |||
6054 | break; | |||
6055 | } | |||
6056 | } | |||
6057 | } | |||
6058 | ||||
6059 | if (rtp_session->has_rtp || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
6060 | rtp_session->missed_count = 0; | |||
6061 | switch_cp_addr(rtp_session->rtp_from_addr, rtp_session->from_addr); | |||
6062 | } | |||
6063 | ||||
6064 | if (rtp_session->has_rtp) { | |||
6065 | rtp_session->last_rtp_hdr = rtp_session->recv_msg.header; | |||
6066 | ||||
6067 | ||||
6068 | if (bytes && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && | |||
6069 | rtp_session->last_rtp_hdr.pt != 13 && | |||
6070 | rtp_session->last_rtp_hdr.pt != rtp_session->recv_te && | |||
6071 | rtp_session->last_rtp_hdr.pt != rtp_session->cng_pt) { | |||
6072 | int accept_packet = 1; | |||
6073 | ||||
6074 | ||||
6075 | if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PAYLOAD) && | |||
6076 | !(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PACKETS) && rtp_session->pmaps && *rtp_session->pmaps) { | |||
6077 | payload_map_t *pmap; | |||
6078 | accept_packet = 0; | |||
6079 | ||||
6080 | switch_mutex_lock(rtp_session->flag_mutex); | |||
6081 | for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) { | |||
6082 | ||||
6083 | if (ntohl(*(int *)(b+4))__bswap_32 (*(int *)(b+4)) == ZRTP_MAGIC_COOKIE0x5a525450) { | |||
6084 | accept_packet = 1; | |||
6085 | break; | |||
6086 | } | |||
6087 | ||||
6088 | if (!pmap->negotiated) { | |||
6089 | continue; | |||
6090 | } | |||
6091 | ||||
6092 | if (rtp_session->last_rtp_hdr.pt == pmap->pt) { | |||
6093 | accept_packet = 1; | |||
6094 | if (pmapP) { | |||
6095 | *pmapP = pmap; | |||
6096 | } | |||
6097 | break; | |||
6098 | } | |||
6099 | } | |||
6100 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
6101 | } | |||
6102 | ||||
6103 | if (!accept_packet) { | |||
6104 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6104, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, | |||
6105 | "Invalid Packet SEQ: %d TS: %d PT:%d ignored\n", | |||
6106 | ntohs(rtp_session->recv_msg.header.seq)__bswap_16 (rtp_session->recv_msg.header.seq), ntohl(rtp_session->last_rtp_hdr.ts)__bswap_32 (rtp_session->last_rtp_hdr.ts), rtp_session->last_rtp_hdr.pt); | |||
6107 | *bytes = 0; | |||
6108 | } | |||
6109 | } | |||
6110 | ||||
6111 | if (rtp_session->flags[SWITCH_RTP_FLAG_DETECT_SSRC]) { | |||
6112 | //if (rtp_session->remote_ssrc != rtp_session->stats.rtcp.peer_ssrc && rtp_session->stats.rtcp.peer_ssrc) { | |||
6113 | // rtp_session->remote_ssrc = rtp_session->stats.rtcp.peer_ssrc; | |||
6114 | //} | |||
6115 | ||||
6116 | if (rtp_session->remote_ssrc != rtp_session->last_rtp_hdr.ssrc && rtp_session->last_rtp_hdr.ssrc) { | |||
6117 | rtp_session->remote_ssrc = ntohl(rtp_session->last_rtp_hdr.ssrc)__bswap_32 (rtp_session->last_rtp_hdr.ssrc); | |||
6118 | } | |||
6119 | } | |||
6120 | } | |||
6121 | } | |||
6122 | ||||
6123 | if (!rtp_session->vb && (!rtp_session->jb || rtp_session->pause_jb || !jb_valid(rtp_session))) { | |||
6124 | if (*bytes > rtp_header_len12 && (rtp_session->has_rtp && check_recv_payload(rtp_session))) { | |||
6125 | xcheck_jitter = *bytes; | |||
6126 | check_jitter(rtp_session); | |||
6127 | } | |||
6128 | } | |||
6129 | ||||
6130 | if (check_rtcp_and_ice(rtp_session) == -1) { | |||
6131 | //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
6132 | //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF CHECK FAIL\n"); | |||
6133 | //} | |||
6134 | return SWITCH_STATUS_GENERR; | |||
6135 | } | |||
6136 | ||||
6137 | if (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
6138 | goto udptl; | |||
6139 | } | |||
6140 | ||||
6141 | ||||
6142 | if (*bytes) { | |||
6143 | *flags &= ~SFF_PROXY_PACKET; | |||
6144 | ||||
6145 | //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
6146 | // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF BYTES %ld b=%d\n", *bytes, *b); | |||
6147 | //} | |||
6148 | ||||
6149 | ||||
6150 | if (rtp_session->has_ice) { | |||
6151 | if (rtp_session->ice.ice_user) { | |||
6152 | handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, *bytes); | |||
6153 | } | |||
6154 | *bytes = 0; | |||
6155 | sync = 1; | |||
6156 | } | |||
6157 | } | |||
6158 | ||||
6159 | switch_mutex_lock(rtp_session->ice_mutex); | |||
6160 | ||||
6161 | if (rtp_session->dtls) { | |||
6162 | ||||
6163 | if (rtp_session->rtcp_dtls && rtp_session->rtcp_dtls != rtp_session->dtls) { | |||
6164 | rtp_session->rtcp_dtls->bytes = 0; | |||
6165 | rtp_session->rtcp_dtls->data = NULL((void*)0); | |||
6166 | do_dtls(rtp_session, rtp_session->rtcp_dtls); | |||
6167 | } | |||
6168 | ||||
6169 | do_dtls(rtp_session, rtp_session->dtls); | |||
6170 | ||||
6171 | if (rtp_session->dtls && rtp_session->dtls->bytes) { | |||
6172 | *bytes = 0; | |||
6173 | sync = 1; | |||
6174 | } | |||
6175 | } | |||
6176 | ||||
6177 | ||||
6178 | ||||
6179 | if (status == SWITCH_STATUS_SUCCESS && *bytes) { | |||
6180 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
6181 | *flags &= ~SFF_RTCP; | |||
6182 | if (rtp_session->has_rtcp) { | |||
6183 | *flags |= SFF_RTCP; | |||
6184 | ||||
6185 | #ifdef ENABLE_SRTP1 | |||
6186 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV]) { | |||
6187 | int sbytes = (int) *bytes; | |||
6188 | srtp_err_status_t stat = 0; | |||
6189 | ||||
6190 | if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) { | |||
6191 | stat = srtp_unprotect_rtcp(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes); | |||
6192 | } else { | |||
6193 | stat = srtp_unprotect_rtcp_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes, 1); | |||
6194 | } | |||
6195 | ||||
6196 | if (stat) { | |||
6197 | //++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++; | |||
6198 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6198, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "RTCP UNPROTECT ERR\n"); | |||
6199 | } else { | |||
6200 | //rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0; | |||
6201 | } | |||
6202 | ||||
6203 | *bytes = sbytes; | |||
6204 | } | |||
6205 | #endif | |||
6206 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
6207 | return SWITCH_STATUS_SUCCESS; | |||
6208 | } | |||
6209 | } | |||
6210 | } | |||
6211 | ||||
6212 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
6213 | ||||
6214 | if ((*bytes && (!rtp_write_ready(rtp_session, *bytes, __LINE__6214) || !rtp_session->has_rtp || rtp_session->has_rtcp)) || sync) { | |||
6215 | rtp_session->hot_hits = 0; | |||
6216 | block = 1; | |||
6217 | *bytes = 0; | |||
6218 | goto more; | |||
6219 | } | |||
6220 | ||||
6221 | if (*bytes && rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) { | |||
6222 | const char *tx_host; | |||
6223 | const char *old_host; | |||
6224 | const char *my_host; | |||
6225 | ||||
6226 | char bufa[50], bufb[50], bufc[50]; | |||
6227 | ||||
6228 | ||||
6229 | tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr); | |||
6230 | old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr); | |||
6231 | my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr); | |||
6232 | ||||
6233 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session)SWITCH_CHANNEL_ID_LOG_CLEAN, "src/switch_rtp.c", (const char * )__func__, 6233, switch_core_session_get_uuid((rtp_session-> session)), SWITCH_LOG_CONSOLE, | |||
6234 | "R %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n", | |||
6235 | rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "No-Name", | |||
6236 | (long) *bytes, | |||
6237 | my_host, switch_sockaddr_get_port(rtp_session->local_addr), | |||
6238 | old_host, rtp_session->remote_port, | |||
6239 | tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr), | |||
6240 | rtp_session->last_rtp_hdr.pt, ntohl(rtp_session->last_rtp_hdr.ts)__bswap_32 (rtp_session->last_rtp_hdr.ts), ntohs(rtp_session->last_rtp_hdr.seq)__bswap_16 (rtp_session->last_rtp_hdr.seq), | |||
6241 | rtp_session->last_rtp_hdr.m); | |||
6242 | ||||
6243 | } | |||
6244 | ||||
6245 | #ifdef RTP_READ_PLOSS | |||
6246 | { | |||
6247 | int r = (rand() % 10000) + 1; | |||
6248 | if (r <= 200) { | |||
6249 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6249, (const char*)(rtp_session->session), SWITCH_LOG_ALERT, | |||
6250 | "Simulate dropped packet ......... ts: %u seq: %u\n", ntohl(rtp_session->last_rtp_hdr.ts)__bswap_32 (rtp_session->last_rtp_hdr.ts), ntohs(rtp_session->last_rtp_hdr.seq)__bswap_16 (rtp_session->last_rtp_hdr.seq)); | |||
6251 | *bytes = 0; | |||
6252 | } | |||
6253 | } | |||
6254 | #endif | |||
6255 | ||||
6256 | ||||
6257 | ||||
6258 | udptl: | |||
6259 | ||||
6260 | ts = 0; | |||
6261 | rtp_session->recv_msg.ebody = NULL((void*)0); | |||
6262 | now = switch_micro_time_now(); | |||
6263 | ||||
6264 | if (*bytes) { | |||
6265 | uint16_t seq = ntohs((uint16_t) rtp_session->last_rtp_hdr.seq)__bswap_16 ((uint16_t) rtp_session->last_rtp_hdr.seq); | |||
6266 | ts = ntohl(rtp_session->last_rtp_hdr.ts)__bswap_32 (rtp_session->last_rtp_hdr.ts); | |||
6267 | ||||
6268 | #ifdef DEBUG_MISSED_SEQ | |||
6269 | if (rtp_session->last_seq && rtp_session->last_seq+1 != seq) { | |||
6270 | //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] | |||
6271 | switch_size_t flushed_packets_diff = rtp_session->stats.inbound.flush_packet_count - rtp_session->last_flush_packet_count; | |||
6272 | switch_size_t num_missed = (switch_size_t)seq - (rtp_session->last_seq+1); | |||
6273 | ||||
6274 | if (num_missed == 1) { /* We missed one packet */ | |||
6275 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 6275, ((void*)0), SWITCH_LOG_ERROR, "Missed one RTP frame with sequence [%d]%s. Time since last read [%ld]\n", | |||
6276 | rtp_session->last_seq+1, (flushed_packets_diff == 1) ? " (flushed by FS)" : " (missed)", | |||
6277 | rtp_session->last_read_time ? now-rtp_session->last_read_time : 0); | |||
6278 | } else { /* We missed multiple packets */ | |||
6279 | if (flushed_packets_diff == 0) { | |||
6280 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 6280, ((void*)0), SWITCH_LOG_ERROR, | |||
6281 | "Missed %ld RTP frames from sequence [%d] to [%d] (missed). Time since last read [%ld]\n", | |||
6282 | num_missed, rtp_session->last_seq+1, seq-1, | |||
6283 | rtp_session->last_read_time ? now-rtp_session->last_read_time : 0); | |||
6284 | } else if (flushed_packets_diff == num_missed) { | |||
6285 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 6285, ((void*)0), SWITCH_LOG_ERROR, | |||
6286 | "Missed %ld RTP frames from sequence [%d] to [%d] (flushed by FS). Time since last read [%ld]\n", | |||
6287 | num_missed, rtp_session->last_seq+1, seq-1, | |||
6288 | rtp_session->last_read_time ? now-rtp_session->last_read_time : 0); | |||
6289 | } else if (num_missed > flushed_packets_diff) { | |||
6290 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 6290, ((void*)0), SWITCH_LOG_ERROR, | |||
6291 | "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS, %ld packets missed)." | |||
6292 | " Time since last read [%ld]\n", | |||
6293 | num_missed, rtp_session->last_seq+1, seq-1, | |||
6294 | flushed_packets_diff, num_missed-flushed_packets_diff, | |||
6295 | rtp_session->last_read_time ? now-rtp_session->last_read_time : 0); | |||
6296 | } else { | |||
6297 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 6297, ((void*)0), SWITCH_LOG_ERROR, | |||
6298 | "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS). Time since last read [%ld]\n", | |||
6299 | num_missed, rtp_session->last_seq+1, seq-1, | |||
6300 | flushed_packets_diff, rtp_session->last_read_time ? now-rtp_session->last_read_time : 0); | |||
6301 | } | |||
6302 | } | |||
6303 | ||||
6304 | } | |||
6305 | #endif | |||
6306 | rtp_session->last_seq = seq; | |||
6307 | ||||
6308 | ||||
6309 | rtp_session->last_flush_packet_count = rtp_session->stats.inbound.flush_packet_count; | |||
6310 | ||||
6311 | ||||
6312 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && now - rtp_session->last_read_time > 5000000) { | |||
6313 | switch_rtp_video_refresh(rtp_session); | |||
6314 | } | |||
6315 | ||||
6316 | rtp_session->last_read_time = now; | |||
6317 | } | |||
6318 | ||||
6319 | if (*bytes && rtp_session->has_rtp && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]){ | |||
6320 | rtcp_stats(rtp_session); | |||
6321 | } | |||
6322 | ||||
6323 | ||||
6324 | if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && | |||
6325 | *bytes && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te && | |||
6326 | ts && !rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session) && ts == rtp_session->last_cng_ts) { | |||
6327 | /* we already sent this frame..... */ | |||
6328 | *bytes = 0; | |||
6329 | return SWITCH_STATUS_SUCCESS; | |||
6330 | } | |||
6331 | ||||
6332 | if (*bytes) { | |||
6333 | if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
6334 | #ifdef ENABLE_ZRTP | |||
6335 | /* ZRTP Recv */ | |||
6336 | if (zrtp_on) { | |||
6337 | ||||
6338 | unsigned int sbytes = (int) *bytes; | |||
6339 | zrtp_status_t stat = 0; | |||
6340 | ||||
6341 | stat = zrtp_process_srtp(rtp_session->zrtp_stream, (void *) &rtp_session->recv_msg, &sbytes); | |||
6342 | ||||
6343 | switch (stat) { | |||
6344 | case zrtp_status_ok: | |||
6345 | *bytes = sbytes; | |||
6346 | break; | |||
6347 | case zrtp_status_drop: | |||
6348 | /* switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */ | |||
6349 | *bytes = 0; | |||
6350 | return SWITCH_STATUS_SUCCESS; | |||
6351 | case zrtp_status_fail: | |||
6352 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 6352, ((void*)0), SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat); | |||
6353 | return SWITCH_STATUS_FALSE; | |||
6354 | default: | |||
6355 | break; | |||
6356 | } | |||
6357 | } | |||
6358 | #endif | |||
6359 | ||||
6360 | #ifdef ENABLE_SRTP1 | |||
6361 | switch_mutex_lock(rtp_session->ice_mutex); | |||
6362 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->has_rtp && | |||
6363 | (check_recv_payload(rtp_session) || | |||
6364 | rtp_session->last_rtp_hdr.pt == rtp_session->recv_te || | |||
6365 | rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt)) { | |||
6366 | //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->has_rtp)) { | |||
6367 | int sbytes = (int) *bytes; | |||
6368 | srtp_err_status_t stat = 0; | |||
6369 | ||||
6370 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) { | |||
6371 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV_RESET); | |||
6372 | srtp_dealloc(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]); | |||
6373 | rtp_session->recv_ctx[rtp_session->srtp_idx_rtp] = NULL((void*)0); | |||
6374 | if ((stat = srtp_create(&rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], | |||
6375 | &rtp_session->recv_policy[rtp_session->srtp_idx_rtp])) || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) { | |||
6376 | ||||
6377 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0; | |||
6378 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6378, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP RECV\n"); | |||
6379 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0; | |||
6380 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
6381 | return SWITCH_STATUS_FALSE; | |||
6382 | } else { | |||
6383 | ||||
6384 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6384, (const char*)(rtp_session->session), SWITCH_LOG_INFO, "RE-Activating Secure RTP RECV\n"); | |||
6385 | rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0; | |||
6386 | } | |||
6387 | } | |||
6388 | ||||
6389 | if (!(*flags & SFF_PLC) && rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) { | |||
6390 | if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) { | |||
6391 | stat = srtp_unprotect(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes); | |||
6392 | } else { | |||
6393 | stat = srtp_unprotect_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes, 1); | |||
6394 | } | |||
6395 | ||||
6396 | if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && stat == srtp_err_status_replay_fail) { | |||
6397 | /* false alarm nack */ | |||
6398 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6398, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, "REPLAY ERR, FALSE NACK\n"); | |||
6399 | sbytes = 0; | |||
6400 | *bytes = 0; | |||
6401 | if (rtp_session->stats.rtcp.pkt_count) { | |||
6402 | rtp_session->stats.rtcp.period_pkt_count--; | |||
6403 | rtp_session->stats.rtcp.pkt_count--; | |||
6404 | } | |||
6405 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
6406 | goto more; | |||
6407 | } | |||
6408 | } | |||
6409 | ||||
6410 | if (stat && rtp_session->recv_msg.header.pt != rtp_session->recv_te && rtp_session->recv_msg.header.pt != rtp_session->cng_pt) { | |||
6411 | int errs = ++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]; | |||
6412 | if (stat != 10) { | |||
6413 | char *msg; | |||
6414 | if (stat == srtp_err_status_replay_fail) msg="replay check failed"; | |||
6415 | else if (stat == srtp_err_status_auth_fail) msg="auth check failed"; | |||
6416 | else msg=""; | |||
6417 | if (errs >= MAX_SRTP_ERRS100) { | |||
6418 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
6419 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6419, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, | |||
6420 | "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n", | |||
6421 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), stat, msg, (long)*bytes, errs); | |||
6422 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6422, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, | |||
6423 | "Ending call due to SRTP error\n"); | |||
6424 | switch_channel_hangup(channel, SWITCH_CAUSE_SRTP_READ_ERROR)switch_channel_perform_hangup(channel, "src/switch_rtp.c", (const char *)__func__, 6424, SWITCH_CAUSE_SRTP_READ_ERROR); | |||
6425 | } else if (errs >= WARN_SRTP_ERRS10 && !(errs % WARN_SRTP_ERRS10)) { | |||
6426 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6426, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, | |||
6427 | "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n", | |||
6428 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), stat, msg, (long)*bytes, errs); | |||
6429 | } | |||
6430 | } | |||
6431 | sbytes = 0; | |||
6432 | } else { | |||
6433 | rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0; | |||
6434 | } | |||
6435 | ||||
6436 | *bytes = sbytes; | |||
6437 | } | |||
6438 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
6439 | #endif | |||
6440 | } | |||
6441 | ||||
6442 | ||||
6443 | if (rtp_session->has_rtp) { | |||
6444 | if (rtp_session->recv_msg.header.cc > 0) { /* Contributing Source Identifiers (4 bytes = sizeof CSRC header)*/ | |||
6445 | rtp_session->recv_msg.ebody = RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body) + (rtp_session->recv_msg.header.cc * 4); | |||
6446 | } | |||
6447 | ||||
6448 | /* recalculate body length in case rtp extension used */ | |||
6449 | if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && | |||
6450 | rtp_session->recv_msg.header.x) { /* header extensions */ | |||
6451 | uint16_t length; | |||
6452 | ||||
6453 | rtp_session->recv_msg.ext = (switch_rtp_hdr_ext_t *) RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body); | |||
6454 | length = ntohs((uint16_t)rtp_session->recv_msg.ext->length)__bswap_16 ((uint16_t)rtp_session->recv_msg.ext->length ); | |||
6455 | ||||
6456 | if (length < SWITCH_RTP_MAX_BUF_LEN_WORDS4094) { | |||
6457 | rtp_session->recv_msg.ebody = (char *)rtp_session->recv_msg.ext + (length * 4) + 4; | |||
6458 | if (*bytes > (length * 4 + 4)) { | |||
6459 | *bytes -= (length * 4 + 4); | |||
6460 | } else { | |||
6461 | *bytes = 0; | |||
6462 | } | |||
6463 | } | |||
6464 | } | |||
6465 | ||||
6466 | ||||
6467 | #ifdef DEBUG_CHROME | |||
6468 | ||||
6469 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->has_rtp) { | |||
6470 | ||||
6471 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 6471, ((void*)0), SWITCH_LOG_INFO, | |||
6472 | "VIDEO: seq: %d ts: %u len: %ld %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x mark: %d\n", | |||
6473 | ntohs(rtp_session->last_rtp_hdr.seq)__bswap_16 (rtp_session->last_rtp_hdr.seq), ntohl(rtp_session->last_rtp_hdr.ts)__bswap_32 (rtp_session->last_rtp_hdr.ts), *bytes, | |||
6474 | *((uint8_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body)), *((uint8_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body) + 1), | |||
6475 | *((uint8_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body) + 2), *((uint8_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body) + 3), | |||
6476 | *((uint8_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body) + 4), *((uint8_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body) + 5), | |||
6477 | *((uint8_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body) + 6), *((uint8_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body) + 7), | |||
6478 | *((uint8_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body) + 8), *((uint8_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body) + 9), | |||
6479 | *((uint8_t *)RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body) + 10), rtp_session->last_rtp_hdr.m); | |||
6480 | ||||
6481 | } | |||
6482 | #endif | |||
6483 | ||||
6484 | ||||
6485 | ||||
6486 | } | |||
6487 | ||||
6488 | ||||
6489 | rtp_session->stats.inbound.raw_bytes += *bytes; | |||
6490 | ||||
6491 | if (rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) { | |||
6492 | rtp_session->stats.inbound.dtmf_packet_count++; | |||
6493 | } else if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) { | |||
6494 | rtp_session->stats.inbound.cng_packet_count++; | |||
6495 | } else { | |||
6496 | rtp_session->stats.inbound.media_packet_count++; | |||
6497 | rtp_session->stats.inbound.media_bytes += *bytes; | |||
6498 | } | |||
6499 | ||||
6500 | rtp_session->stats.inbound.packet_count++; | |||
6501 | } | |||
6502 | ||||
6503 | if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && | |||
6504 | ((rtp_session->recv_te && rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) || | |||
6505 | (*bytes < rtp_header_len12 && *bytes > 0 && !(rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL])))) { | |||
6506 | return SWITCH_STATUS_BREAK; | |||
6507 | } | |||
6508 | ||||
6509 | if (ts) { | |||
6510 | rtp_session->prev_read_ts = rtp_session->last_read_ts; | |||
6511 | rtp_session->last_read_ts = ts; | |||
6512 | } | |||
6513 | ||||
6514 | if (rtp_session->flags[SWITCH_RTP_FLAG_BYTESWAP] && check_recv_payload(rtp_session)) { | |||
6515 | 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); | |||
6516 | } | |||
6517 | ||||
6518 | if (rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB]) { | |||
6519 | rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB] = 0; | |||
6520 | ||||
6521 | if (rtp_session->jb) { | |||
6522 | switch_jb_destroy(&rtp_session->jb); | |||
6523 | } | |||
6524 | ||||
6525 | if (rtp_session->vb) { | |||
6526 | switch_jb_destroy(&rtp_session->vb); | |||
6527 | } | |||
6528 | ||||
6529 | if (rtp_session->vbw) { | |||
6530 | switch_jb_destroy(&rtp_session->vbw); | |||
6531 | } | |||
6532 | ||||
6533 | } | |||
6534 | ||||
6535 | if (rtp_session->has_rtp && *bytes) { | |||
6536 | uint32_t read_ssrc = ntohl(rtp_session->last_rtp_hdr.ssrc)__bswap_32 (rtp_session->last_rtp_hdr.ssrc); | |||
6537 | ||||
6538 | if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
6539 | return SWITCH_STATUS_SUCCESS; | |||
6540 | } | |||
6541 | ||||
6542 | if (rtp_session->vb && !rtp_session->pause_jb && jb_valid(rtp_session)) { | |||
6543 | status = switch_jb_put_packet(rtp_session->vb, (switch_rtp_packet_t *) &rtp_session->recv_msg, *bytes); | |||
6544 | ||||
6545 | if (status == SWITCH_STATUS_TOO_LATE) { | |||
6546 | goto more; | |||
6547 | } | |||
6548 | ||||
6549 | status = SWITCH_STATUS_FALSE; | |||
6550 | *bytes = 0; | |||
6551 | ||||
6552 | if (!return_jb_packet) { | |||
6553 | return status; | |||
6554 | } | |||
6555 | } | |||
6556 | ||||
6557 | if (rtp_session->jb && jb_valid(rtp_session)) { | |||
6558 | if (rtp_session->last_jb_read_ssrc && rtp_session->last_jb_read_ssrc != read_ssrc) { | |||
6559 | switch_jb_reset(rtp_session->jb); | |||
6560 | } | |||
6561 | ||||
6562 | rtp_session->last_jb_read_ssrc = read_ssrc; | |||
6563 | } | |||
6564 | ||||
6565 | if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) { | |||
6566 | ||||
6567 | if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) { | |||
6568 | switch_core_timer_sync(&rtp_session->timer); | |||
6569 | reset_jitter_seq(rtp_session); | |||
6570 | } | |||
6571 | ||||
6572 | status = switch_jb_put_packet(rtp_session->jb, (switch_rtp_packet_t *) &rtp_session->recv_msg, *bytes); | |||
6573 | if (status == SWITCH_STATUS_TOO_LATE) { | |||
6574 | goto more; | |||
6575 | } | |||
6576 | ||||
6577 | ||||
6578 | status = SWITCH_STATUS_FALSE; | |||
6579 | *bytes = 0; | |||
6580 | ||||
6581 | if (!return_jb_packet) { | |||
6582 | return status; | |||
6583 | } | |||
6584 | } else { | |||
6585 | if (rtp_session->last_rtp_hdr.m && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te && | |||
6586 | !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !(rtp_session->rtp_bugs & RTP_BUG_IGNORE_MARK_BIT) && | |||
6587 | rtp_session->last_read_ts - rtp_session->prev_read_ts < rtp_session->samples_per_interval * 3) { | |||
6588 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH); | |||
6589 | } else if (rtp_session->last_jb_read_ssrc && rtp_session->last_jb_read_ssrc != read_ssrc) { | |||
6590 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH); | |||
6591 | } | |||
6592 | } | |||
6593 | } | |||
6594 | ||||
6595 | if (!*bytes || rtp_session->has_rtp) { | |||
6596 | ||||
6597 | if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) { | |||
6598 | switch_status_t jstatus = switch_jb_get_packet(rtp_session->jb, (switch_rtp_packet_t *) &rtp_session->recv_msg, bytes); | |||
6599 | ||||
6600 | status = jstatus; | |||
6601 | ||||
6602 | switch(jstatus) { | |||
6603 | case SWITCH_STATUS_MORE_DATA: | |||
6604 | if (rtp_session->punts < 4) { | |||
6605 | block = 1; | |||
6606 | goto more; | |||
6607 | } | |||
6608 | *bytes = 0; | |||
6609 | break; | |||
6610 | case SWITCH_STATUS_NOTFOUND: | |||
6611 | { | |||
6612 | int pt = get_recv_payload(rtp_session); | |||
6613 | (*flags) |= SFF_PLC; | |||
6614 | status = SWITCH_STATUS_SUCCESS; | |||
6615 | *bytes = switch_jb_get_last_read_len(rtp_session->jb); | |||
6616 | rtp_session->last_rtp_hdr = rtp_session->recv_msg.header; | |||
6617 | rtp_session->last_rtp_hdr.pt = pt; | |||
6618 | } | |||
6619 | break; | |||
6620 | case SWITCH_STATUS_BREAK: | |||
6621 | break; | |||
6622 | case SWITCH_STATUS_SUCCESS: | |||
6623 | case SWITCH_STATUS_TIMEOUT: | |||
6624 | default: | |||
6625 | { | |||
6626 | if (status == SWITCH_STATUS_TIMEOUT) { | |||
6627 | rtp_session->skip_timer = 1; | |||
6628 | } | |||
6629 | rtp_session->stats.inbound.jb_packet_count++; | |||
6630 | status = SWITCH_STATUS_SUCCESS; | |||
6631 | rtp_session->last_rtp_hdr = rtp_session->recv_msg.header; | |||
6632 | if (++rtp_session->clean > 200) { | |||
6633 | rtp_session->punts = 0; | |||
6634 | } | |||
6635 | if (!xcheck_jitter) { | |||
6636 | check_jitter(rtp_session); | |||
6637 | } | |||
6638 | } | |||
6639 | break; | |||
6640 | } | |||
6641 | } | |||
6642 | ||||
6643 | if (rtp_session->vb && !rtp_session->pause_jb && jb_valid(rtp_session)) { | |||
6644 | switch_status_t vstatus = switch_jb_get_packet(rtp_session->vb, (switch_rtp_packet_t *) &rtp_session->recv_msg, bytes); | |||
6645 | status = vstatus; | |||
6646 | ||||
6647 | switch(vstatus) { | |||
6648 | case SWITCH_STATUS_RESTART: | |||
6649 | switch_core_session_request_video_refresh(rtp_session->session)_switch_core_session_request_video_refresh(rtp_session->session , 0, "src/switch_rtp.c", (const char *)__func__, 6649); | |||
6650 | status = SWITCH_STATUS_BREAK; | |||
6651 | break; | |||
6652 | case SWITCH_STATUS_MORE_DATA: | |||
6653 | status = SWITCH_STATUS_BREAK; | |||
6654 | break; | |||
6655 | case SWITCH_STATUS_BREAK: | |||
6656 | default: | |||
6657 | break; | |||
6658 | } | |||
6659 | ||||
6660 | if (vstatus == SWITCH_STATUS_NOTFOUND && rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) { | |||
6661 | int pt = get_recv_payload(rtp_session); | |||
6662 | (*flags) |= SFF_PLC; | |||
6663 | status = SWITCH_STATUS_SUCCESS; | |||
6664 | *bytes = switch_jb_get_last_read_len(rtp_session->vb); | |||
6665 | rtp_session->last_rtp_hdr = rtp_session->recv_msg.header; | |||
6666 | if (pt > -1) { | |||
6667 | rtp_session->last_rtp_hdr.pt = pt; | |||
6668 | } | |||
6669 | } | |||
6670 | ||||
6671 | if (vstatus == SWITCH_STATUS_SUCCESS) { | |||
6672 | rtp_session->last_rtp_hdr = rtp_session->recv_msg.header; | |||
6673 | ||||
6674 | if (!xcheck_jitter) { | |||
6675 | check_jitter(rtp_session); | |||
6676 | } | |||
6677 | } | |||
6678 | } | |||
6679 | } | |||
6680 | ||||
6681 | return status; | |||
6682 | } | |||
6683 | ||||
6684 | static void handle_nack(switch_rtp_t *rtp_session, uint32_t nack) | |||
6685 | { | |||
6686 | switch_size_t bytes = 0; | |||
6687 | rtp_msg_t send_msg[1] = {{{0}}}; | |||
6688 | uint16_t seq = (uint16_t) (nack & 0xFFFF); | |||
6689 | uint16_t blp = (uint16_t) (nack >> 16); | |||
6690 | int i; | |||
6691 | const char *tx_host = NULL((void*)0); | |||
6692 | const char *old_host = NULL((void*)0); | |||
6693 | const char *my_host = NULL((void*)0); | |||
6694 | char bufa[50], bufb[50], bufc[50]; | |||
6695 | ||||
6696 | if (!(rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->vbw)) { | |||
6697 | return; /* not enabled */ | |||
6698 | } | |||
6699 | ||||
6700 | if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) { | |||
6701 | tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_from_addr); | |||
6702 | old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr); | |||
6703 | my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr); | |||
6704 | } | |||
6705 | ||||
6706 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6706, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got NACK [%u][0x%x] for seq %u\n", | |||
6707 | switch_core_session_get_name(rtp_session->session)switch_channel_get_name(switch_core_session_get_channel(rtp_session ->session)), nack, nack, ntohs(seq)__bswap_16 (seq)); | |||
6708 | ||||
6709 | if (switch_jb_get_packet_by_seq(rtp_session->vbw, seq, (switch_rtp_packet_t *) send_msg, &bytes) == SWITCH_STATUS_SUCCESS) { | |||
6710 | ||||
6711 | if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) { | |||
6712 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session)SWITCH_CHANNEL_ID_LOG_CLEAN, "src/switch_rtp.c", (const char * )__func__, 6712, switch_core_session_get_uuid((rtp_session-> session)), SWITCH_LOG_CONSOLE, | |||
6713 | "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n", | |||
6714 | rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName", | |||
6715 | (long) bytes, | |||
6716 | my_host, switch_sockaddr_get_port(rtp_session->local_addr), | |||
6717 | old_host, rtp_session->remote_port, | |||
6718 | tx_host, switch_sockaddr_get_port(rtp_session->rtcp_from_addr), | |||
6719 | send_msg->header.pt, ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts), ntohs(send_msg->header.seq)__bswap_16 (send_msg->header.seq), send_msg->header.m); | |||
6720 | ||||
6721 | } | |||
6722 | //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG2, "RE----SEND %u\n", ntohs(send_msg->header.seq)); | |||
6723 | switch_rtp_write_raw(rtp_session, (void *) send_msg, &bytes, SWITCH_FALSE); | |||
6724 | } else { | |||
6725 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6725, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG2, "Cannot send NACK for seq %u\n", ntohs(seq)__bswap_16 (seq)); | |||
6726 | } | |||
6727 | ||||
6728 | blp = ntohs(blp)__bswap_16 (blp); | |||
6729 | for (i = 0; i < 16; i++) { | |||
6730 | if (blp & (1 << i)) { | |||
6731 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6731, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Also Got NACK for seq %u\n", | |||
6732 | switch_core_session_get_name(rtp_session->session)switch_channel_get_name(switch_core_session_get_channel(rtp_session ->session)), ntohs(seq)__bswap_16 (seq) + i + 1); | |||
6733 | /* If they are missing more than one, may as well gen a key frame for good measure */ | |||
6734 | //switch_core_media_gen_key_frame(rtp_session->session); | |||
6735 | if (switch_jb_get_packet_by_seq(rtp_session->vbw, htons(ntohs(seq) + i + 1)__bswap_16 (__bswap_16 (seq) + i + 1), (switch_rtp_packet_t *) &send_msg, &bytes) == SWITCH_STATUS_SUCCESS) { | |||
6736 | if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) { | |||
6737 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session)SWITCH_CHANNEL_ID_LOG_CLEAN, "src/switch_rtp.c", (const char * )__func__, 6737, switch_core_session_get_uuid((rtp_session-> session)), SWITCH_LOG_CONSOLE, | |||
6738 | "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n", | |||
6739 | rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName", | |||
6740 | (long) bytes, | |||
6741 | my_host, switch_sockaddr_get_port(rtp_session->local_addr), | |||
6742 | old_host, rtp_session->remote_port, | |||
6743 | tx_host, switch_sockaddr_get_port(rtp_session->rtcp_from_addr), | |||
6744 | send_msg->header.pt, ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts), ntohs(send_msg->header.seq)__bswap_16 (send_msg->header.seq), send_msg->header.m); | |||
6745 | ||||
6746 | } | |||
6747 | //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "RE----SEND %u\n", ntohs(send_msg->header.seq)); | |||
6748 | ||||
6749 | switch_rtp_write_raw(rtp_session, (void *) &send_msg, &bytes, SWITCH_FALSE); | |||
6750 | } else { | |||
6751 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6751, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG2, "Cannot send NACK for seq %u\n", ntohs(seq)__bswap_16 (seq) + i); | |||
6752 | } | |||
6753 | } | |||
6754 | } | |||
6755 | } | |||
6756 | ||||
6757 | static switch_status_t process_rtcp_report(switch_rtp_t *rtp_session, rtcp_msg_t *msg, switch_size_t bytes) | |||
6758 | { | |||
6759 | switch_status_t status = SWITCH_STATUS_FALSE; | |||
6760 | ||||
6761 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6761, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, | |||
6762 | "RTCP packet bytes %" SWITCH_SIZE_T_FMT"ld" " type %d pad %d\n", | |||
6763 | bytes, msg->header.type, msg->header.p); | |||
6764 | ||||
6765 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (msg->header.type == _RTCP_PT_RTPFB || msg->header.type == _RTCP_PT_PSFB || msg->header.type < 200)) { | |||
6766 | rtcp_ext_msg_t *extp = (rtcp_ext_msg_t *) msg; | |||
6767 | ||||
6768 | if (extp->header.fmt != 15) { // <---- REMOVE WHEN BRIA STOPS SENDING UNSOLICITED REMB | |||
6769 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6769, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG2, "%s PICKED UP %s XRTCP type: %d fmt: %d\n", | |||
6770 | switch_core_session_get_name(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_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), msg->header.type, extp->header.fmt); | |||
6771 | } | |||
6772 | ||||
6773 | if (msg->header.type == _RTCP_PT_FIR || | |||
6774 | (msg->header.type == _RTCP_PT_PSFB && (extp->header.fmt == _RTCP_PSFB_FIR || extp->header.fmt == _RTCP_PSFB_PLI))) { | |||
6775 | #if 0 | |||
6776 | if (msg->header.type == _RTCP_PT_FIR) { | |||
6777 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6777, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, "Ancient FIR Received. Hello from 1996!\n"); | |||
6778 | ||||
6779 | if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_OLD_FIR)) { | |||
6780 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_OLD_FIR); | |||
6781 | switch_core_session_request_video_refresh(rtp_session->session)_switch_core_session_request_video_refresh(rtp_session->session , 0, "src/switch_rtp.c", (const char *)__func__, 6781); | |||
6782 | } | |||
6783 | } | |||
6784 | #endif | |||
6785 | ||||
6786 | if (switch_core_session_media_flow(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO) == SWITCH_MEDIA_FLOW_RECVONLY) { | |||
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_DEBUG2, "%s Ignoring FIR/PLI from a sendonly stream.\n", | |||
6788 | switch_core_session_get_name(rtp_session->session)switch_channel_get_name(switch_core_session_get_channel(rtp_session ->session))); | |||
6789 | } else { | |||
6790 | switch_core_media_gen_key_frame(rtp_session->session)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c" , (const char *)__func__, 6790, (const char*)(rtp_session-> session), SWITCH_LOG_DEBUG1, "%s Send KeyFrame\n", switch_channel_get_name (switch_core_session_get_channel(rtp_session->session))); switch_core_media_codec_control (rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_IO_WRITE , SCC_VIDEO_GEN_KEYFRAME, SCCT_NONE, ((void*)0), SCCT_NONE, ( (void*)0), ((void*)0), ((void*)0)); | |||
6791 | switch_core_session_request_video_refresh(rtp_session->session)_switch_core_session_request_video_refresh(rtp_session->session , 0, "src/switch_rtp.c", (const char *)__func__, 6791); | |||
6792 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6792, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got FIR/PLI\n", | |||
6793 | switch_core_session_get_name(rtp_session->session)switch_channel_get_name(switch_core_session_get_channel(rtp_session ->session))); | |||
6794 | switch_channel_set_flag(switch_core_session_get_channel(rtp_session->session), CF_VIDEO_REFRESH_REQ)switch_channel_set_flag_value(switch_core_session_get_channel (rtp_session->session), CF_VIDEO_REFRESH_REQ, 1); | |||
6795 | } | |||
6796 | } | |||
6797 | ||||
6798 | if (msg->header.type == _RTCP_PT_RTPFB && extp->header.fmt == _RTCP_RTPFB_NACK) { | |||
6799 | uint32_t *nack = (uint32_t *) extp->body; | |||
6800 | int i; | |||
6801 | ||||
6802 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6802, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got NACK count %d\n", | |||
6803 | switch_core_session_get_name(rtp_session->session)switch_channel_get_name(switch_core_session_get_channel(rtp_session ->session)), ntohs(extp->header.length)__bswap_16 (extp->header.length) - 2); | |||
6804 | ||||
6805 | ||||
6806 | for (i = 0; i < ntohs(extp->header.length)__bswap_16 (extp->header.length) - 2; i++) { | |||
6807 | handle_nack(rtp_session, nack[i]); | |||
6808 | } | |||
6809 | ||||
6810 | //switch_core_media_gen_key_frame(rtp_session->session); | |||
6811 | } | |||
6812 | ||||
6813 | } else { | |||
6814 | struct switch_rtcp_report_block *report; | |||
6815 | ||||
6816 | if (msg->header.type == _RTCP_PT_SR || msg->header.type == _RTCP_PT_RR) { | |||
6817 | int i; | |||
6818 | #ifdef DEBUG_RTCP | |||
6819 | switch_time_t now = switch_micro_time_now(); | |||
6820 | #endif | |||
6821 | uint32_t lsr_now; | |||
6822 | uint32_t lsr; | |||
6823 | uint32_t packet_ssrc; | |||
6824 | double rtt_now = 0; | |||
6825 | uint8_t rtt_valid = 0; | |||
6826 | int rtt_increase = 0, packet_loss_increase=0; | |||
6827 | ||||
6828 | //if (msg->header.type == _RTCP_PT_SR && rtp_session->ice.ice_user) { | |||
6829 | // rtp_session->send_rr = 1; | |||
6830 | //} | |||
6831 | ||||
6832 | lsr_now = calc_local_lsr_now(); | |||
6833 | ||||
6834 | if (msg->header.type == _RTCP_PT_SR) { /* Sender report */ | |||
6835 | struct switch_rtcp_sender_report* sr = (struct switch_rtcp_sender_report*)msg->body; | |||
6836 | ||||
6837 | rtp_session->stats.rtcp.packet_count = ntohl(sr->sender_info.pc)__bswap_32 (sr->sender_info.pc); | |||
6838 | rtp_session->stats.rtcp.octet_count = ntohl(sr->sender_info.oc)__bswap_32 (sr->sender_info.oc); | |||
6839 | packet_ssrc = sr->ssrc; | |||
6840 | /* Extracting LSR from NTP timestamp and save it */ | |||
6841 | lsr = (ntohl(sr->sender_info.ntp_lsw)__bswap_32 (sr->sender_info.ntp_lsw)&0xffff0000)>>16 | (ntohl(sr->sender_info.ntp_msw)__bswap_32 (sr->sender_info.ntp_msw)&0x0000ffff)<<16; /* The middle 32 bits out of 64 in the NTP timestamp */ | |||
6842 | rtp_session->stats.rtcp.last_recv_lsr_peer = htonl(lsr)__bswap_32 (lsr); /* Save it include it in the next SR */ | |||
6843 | rtp_session->stats.rtcp.last_recv_lsr_local = lsr_now; /* Save it to calculate DLSR when generating next SR */ | |||
6844 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6844, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3,"Received a SR with %d report blocks, " \ | |||
6845 | "length in words = %d, " \ | |||
6846 | "SSRC = 0x%X, " \ | |||
6847 | "NTP MSW = %u, " \ | |||
6848 | "NTP LSW = %u, " \ | |||
6849 | "RTP timestamp = %u, " \ | |||
6850 | "Sender Packet Count = %u, " \ | |||
6851 | "Sender Octet Count = %u\n", | |||
6852 | msg->header.count, | |||
6853 | ntohs((uint16_t)msg->header.length)__bswap_16 ((uint16_t)msg->header.length), | |||
6854 | ntohl(sr->ssrc)__bswap_32 (sr->ssrc), | |||
6855 | ntohl(sr->sender_info.ntp_msw)__bswap_32 (sr->sender_info.ntp_msw), | |||
6856 | ntohl(sr->sender_info.ntp_lsw)__bswap_32 (sr->sender_info.ntp_lsw), | |||
6857 | ntohl(sr->sender_info.ts)__bswap_32 (sr->sender_info.ts), | |||
6858 | ntohl(sr->sender_info.pc)__bswap_32 (sr->sender_info.pc), | |||
6859 | ntohl(sr->sender_info.oc)__bswap_32 (sr->sender_info.oc)); | |||
6860 | ||||
6861 | ||||
6862 | rtp_session->rtcp_frame.ssrc = ntohl(sr->ssrc)__bswap_32 (sr->ssrc); | |||
6863 | rtp_session->rtcp_frame.packet_type = (uint16_t)rtp_session->rtcp_recv_msg_p->header.type; | |||
6864 | rtp_session->rtcp_frame.ntp_msw = ntohl(sr->sender_info.ntp_msw)__bswap_32 (sr->sender_info.ntp_msw); | |||
6865 | rtp_session->rtcp_frame.ntp_lsw = ntohl(sr->sender_info.ntp_lsw)__bswap_32 (sr->sender_info.ntp_lsw); | |||
6866 | rtp_session->rtcp_frame.timestamp = ntohl(sr->sender_info.ts)__bswap_32 (sr->sender_info.ts); | |||
6867 | rtp_session->rtcp_frame.packet_count = ntohl(sr->sender_info.pc)__bswap_32 (sr->sender_info.pc); | |||
6868 | rtp_session->rtcp_frame.octect_count = ntohl(sr->sender_info.oc)__bswap_32 (sr->sender_info.oc); | |||
6869 | ||||
6870 | report = &sr->report_block; | |||
6871 | } else { /* Receiver report */ | |||
6872 | struct switch_rtcp_receiver_report* rr = (struct switch_rtcp_receiver_report*)msg->body; | |||
6873 | packet_ssrc = rr->ssrc; | |||
6874 | //memset(&rtp_session->rtcp_frame, 0, sizeof(rtp_session->rtcp_frame)); | |||
6875 | report = &rr->report_block; | |||
6876 | ||||
6877 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6877, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3,"Received a RR with %d report blocks, " \ | |||
6878 | "length in words = %d, " \ | |||
6879 | "SSRC = 0x%X, ", | |||
6880 | msg->header.count, | |||
6881 | ntohs((uint16_t)msg->header.length)__bswap_16 ((uint16_t)msg->header.length), | |||
6882 | ntohl(rr->ssrc)__bswap_32 (rr->ssrc)); | |||
6883 | ||||
6884 | } | |||
6885 | ||||
6886 | ||||
6887 | for (i = 0; i < (int)msg->header.count && i < MAX_REPORT_BLOCKS5 ; i++) { | |||
6888 | uint32_t old_avg = rtp_session->rtcp_frame.reports[i].loss_avg; | |||
6889 | uint8_t percent_fraction = (uint8_t)((uint16_t/* prevent overflow when '* 100' */)(uint8_t)report->fraction * 100 / 255); | |||
6890 | if (!rtp_session->rtcp_frame.reports[i].loss_avg) { | |||
6891 | rtp_session->rtcp_frame.reports[i].loss_avg = percent_fraction; | |||
6892 | } else { | |||
6893 | rtp_session->rtcp_frame.reports[i].loss_avg = (uint32_t)(((float)rtp_session->rtcp_frame.reports[i].loss_avg * .7) + | |||
6894 | ((float)percent_fraction * .3)); | |||
6895 | } | |||
6896 | ||||
6897 | rtp_session->rtcp_frame.reports[i].ssrc = ntohl(report->ssrc)__bswap_32 (report->ssrc); | |||
6898 | rtp_session->rtcp_frame.reports[i].fraction = (uint8_t)report->fraction; | |||
6899 | #if SWITCH_BYTE_ORDER1234 == __BIG_ENDIAN4321 | |||
6900 | rtp_session->rtcp_frame.reports[i].lost = report->lost; // signed 24bit will extended signess to int32_t automatically | |||
6901 | #else | |||
6902 | rtp_session->rtcp_frame.reports[i].lost = ntohl(report->lost)__bswap_32 (report->lost)>>8; // signed 24bit casted to uint32_t need >>8 after ntohl()... | |||
6903 | rtp_session->rtcp_frame.reports[i].lost = rtp_session->rtcp_frame.reports[i].lost | ((rtp_session->rtcp_frame.reports[i].lost & 0x00800000) ? 0xff000000 : 0x00000000); // ...and signess compensation | |||
6904 | #endif | |||
6905 | rtp_session->rtcp_frame.reports[i].highest_sequence_number_received = ntohl(report->highest_sequence_number_received)__bswap_32 (report->highest_sequence_number_received); | |||
6906 | rtp_session->rtcp_frame.reports[i].jitter = ntohl(report->jitter)__bswap_32 (report->jitter); | |||
6907 | rtp_session->rtcp_frame.reports[i].lsr = ntohl(report->lsr)__bswap_32 (report->lsr); | |||
6908 | rtp_session->rtcp_frame.reports[i].dlsr = ntohl(report->dlsr)__bswap_32 (report->dlsr); | |||
6909 | ||||
6910 | if (rtp_session->rtcp_frame.reports[i].lsr && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) { | |||
6911 | ||||
6912 | /* Calculating RTT = A - DLSR - LSR */ | |||
6913 | rtt_now = ((double)(((int64_t)lsr_now) - rtp_session->rtcp_frame.reports[i].dlsr - rtp_session->rtcp_frame.reports[i].lsr))/65536; | |||
6914 | ||||
6915 | /* Only account RTT if it didn't overflow. */ | |||
6916 | if (lsr_now > rtp_session->rtcp_frame.reports[i].dlsr + rtp_session->rtcp_frame.reports[i].lsr) { | |||
6917 | #ifdef DEBUG_RTCP | |||
6918 | switch_time_exp_t now_hr; | |||
6919 | switch_time_exp_gmt(&now_hr,now); | |||
6920 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6920, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, | |||
6921 | "Receiving an RTCP packet\n[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[0x%x]\n" | |||
6922 | "RTT[%f] = A[%u] - DLSR[%u] - LSR[%u]\n", | |||
6923 | 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, | |||
6924 | rtp_session->rtcp_frame.reports[i].ssrc, rtt_now, | |||
6925 | lsr_now, rtp_session->rtcp_frame.reports[i].dlsr, rtp_session->rtcp_frame.reports[i].lsr); | |||
6926 | #endif | |||
6927 | rtt_valid = 1; | |||
6928 | if (!rtp_session->rtcp_frame.reports[i].rtt_avg) { | |||
6929 | rtp_session->rtcp_frame.reports[i].rtt_avg = rtt_now; | |||
6930 | } else { | |||
6931 | rtp_session->rtcp_frame.reports[i].rtt_avg = (double)((rtp_session->rtcp_frame.reports[i].rtt_avg * .7) + (rtt_now * .3 )); | |||
6932 | } | |||
6933 | } else { | |||
6934 | #ifdef DEBUG_RTCP | |||
6935 | switch_time_exp_t now_hr; | |||
6936 | switch_time_exp_gmt(&now_hr,now); | |||
6937 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6937, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, | |||
6938 | "Receiving RTCP packet\n[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[0x%x]\n" | |||
6939 | "Ignoring erroneous RTT[%f] = A[%u] - DLSR[%u] - LSR[%u]\n", | |||
6940 | 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, | |||
6941 | rtp_session->rtcp_frame.reports[i].ssrc, rtt_now, | |||
6942 | lsr_now, rtp_session->rtcp_frame.reports[i].dlsr, rtp_session->rtcp_frame.reports[i].lsr); | |||
6943 | #endif | |||
6944 | rtt_valid = 0; | |||
6945 | rtt_now = 0; | |||
6946 | } | |||
6947 | ||||
6948 | ||||
6949 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6949, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, "RTT average %f\n", | |||
6950 | rtp_session->rtcp_frame.reports[i].rtt_avg); | |||
6951 | } | |||
6952 | ||||
6953 | if (rtp_session->flags[SWITCH_RTP_FLAG_ADJ_BITRATE_CAP] && rtp_session->flags[SWITCH_RTP_FLAG_ESTIMATORS] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
6954 | ||||
6955 | /* SWITCH_RTP_FLAG_ADJ_BITRATE_CAP : Can the codec change its bitrate on the fly per API command ? */ | |||
6956 | #ifdef DEBUG_ESTIMATORS_ | |||
6957 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6957, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, "Current packet loss: [%d %%] Current RTT: [%f ms]\n", percent_fraction, rtt_now); | |||
6958 | #endif | |||
6959 | ||||
6960 | if (rtt_valid) { | |||
6961 | ||||
6962 | switch_kalman_estimate(rtp_session->estimators[EST_RTT2], rtt_now, EST_RTT2); | |||
6963 | ||||
6964 | if (switch_kalman_cusum_detect_change(rtp_session->detectors[EST_RTT2], rtt_now, rtp_session->estimators[EST_RTT2]->val_estimate_last)) { | |||
6965 | /* sudden change in the mean value of RTT */ | |||
6966 | #ifdef DEBUG_ESTIMATORS_ | |||
6967 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6967, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of RTT !\n"); | |||
6968 | #endif | |||
6969 | rtt_increase = 1; | |||
6970 | } | |||
6971 | } | |||
6972 | ||||
6973 | switch_kalman_estimate(rtp_session->estimators[EST_LOSS0], percent_fraction, EST_LOSS0); | |||
6974 | ||||
6975 | if (switch_kalman_cusum_detect_change(rtp_session->detectors[EST_LOSS0], percent_fraction, rtp_session->estimators[EST_LOSS0]->val_estimate_last)){ | |||
6976 | /* sudden change in the mean value of packet loss */ | |||
6977 | #ifdef DEBUG_ESTIMATORS_ | |||
6978 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6978, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of packet loss!\n"); | |||
6979 | #endif | |||
6980 | packet_loss_increase = 1; | |||
6981 | } | |||
6982 | #ifdef DEBUG_ESTIMATORS_ | |||
6983 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6983, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, "ESTIMATORS: Packet loss will be: [%f] RTT will be: [%f ms]\n", | |||
6984 | rtp_session->estimators[EST_LOSS0]->val_estimate_last, rtp_session->estimators[EST_RTT2]->val_estimate_last); | |||
6985 | #endif | |||
6986 | ||||
6987 | if (rtp_session->rtcp_frame.reports[i].loss_avg != old_avg) { | |||
6988 | /*getting bad*/ | |||
6989 | if (switch_kalman_is_slow_link(rtp_session->estimators[EST_LOSS0], | |||
6990 | rtp_session->estimators[EST_RTT2])) { | |||
6991 | /* going to minimum bitrate */ | |||
6992 | #ifdef DEBUG_ESTIMATORS_ | |||
6993 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 6993, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, "Slow link conditions: Loss average: [%d %%], Previous loss: [%d %%]. \ | |||
6994 | Going to minimum bitrate!",rtp_session->rtcp_frame.reports[i].loss_avg, old_avg); | |||
6995 | #endif | |||
6996 | switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO, | |||
6997 | SWITCH_IO_WRITE, SCC_AUDIO_ADJUST_BITRATE, SCCT_STRING, "minimum", SCCT_NONE, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
6998 | /* if after going to minimum bitrate we still have packet loss then we increase ptime. TODO */ | |||
6999 | ||||
7000 | } else if (packet_loss_increase && (rtp_session->estimators[EST_LOSS0]->val_estimate_last >= 5)) { | |||
7001 | /* sudden change in the mean value of packet loss percentage */ | |||
7002 | switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO, | |||
7003 | SWITCH_IO_WRITE, SCC_AUDIO_ADJUST_BITRATE, | |||
7004 | SCCT_STRING, "decrease", | |||
7005 | SCCT_NONE, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
7006 | #ifdef DEBUG_ESTIMATORS_ | |||
7007 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7007, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of packet loss percentage !\n"); | |||
7008 | #endif | |||
7009 | switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO, | |||
7010 | SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT, | |||
7011 | (void *)&rtp_session->rtcp_frame.reports[i].loss_avg, | |||
7012 | SCCT_NONE, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
7013 | ||||
7014 | } else if (rtt_valid && !rtt_increase && rtp_session->estimators[EST_LOSS0]->val_estimate_last >= rtp_session->rtcp_frame.reports[i].loss_avg ) { | |||
7015 | /* lossy because of congestion (queues full somewhere -> some packets are dropped , but RTT is good ), packet loss with many small gaps */ | |||
7016 | #ifdef DEBUG_ESTIMATORS_ | |||
7017 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7017, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, "packet loss, but RTT is not bad\n"); | |||
7018 | #endif | |||
7019 | switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO, | |||
7020 | SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT, | |||
7021 | (void *)&rtp_session->rtcp_frame.reports[i].loss_avg, | |||
7022 | SCCT_NONE, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
7023 | ||||
7024 | } else if ((rtp_session->estimators[EST_LOSS0]->val_estimate_last < 1) && packet_loss_increase) { | |||
7025 | #ifdef DEBUG_ESTIMATORS_ | |||
7026 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7026, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, "small packet loss average\n"); | |||
7027 | #endif | |||
7028 | /*small loss_avg*/ | |||
7029 | switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO, | |||
7030 | SWITCH_IO_WRITE, SCC_AUDIO_ADJUST_BITRATE, | |||
7031 | SCCT_STRING, "default", | |||
7032 | SCCT_NONE, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
7033 | ||||
7034 | switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO, | |||
7035 | SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT, | |||
7036 | (void *)&rtp_session->rtcp_frame.reports[i].loss_avg, | |||
7037 | SCCT_NONE, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
7038 | ||||
7039 | } else if ((rtp_session->estimators[EST_LOSS0]->val_estimate_last < 5) && | |||
7040 | (rtp_session->rtcp_frame.reports[i].rtt_avg < rtp_session->estimators[EST_RTT2]->val_estimate_last)) { | |||
7041 | ||||
7042 | /* estimate that packet loss will decrease, we can increase the bitrate */ | |||
7043 | switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO, | |||
7044 | SWITCH_IO_WRITE, SCC_AUDIO_ADJUST_BITRATE, | |||
7045 | SCCT_STRING, "increase", | |||
7046 | SCCT_NONE, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
7047 | ||||
7048 | switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO, | |||
7049 | SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT, | |||
7050 | (void *)&rtp_session->rtcp_frame.reports[i].loss_avg, | |||
7051 | SCCT_NONE, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
7052 | ||||
7053 | } else { | |||
7054 | /* *do nothing about bitrate, just pass the packet loss to the codec */ | |||
7055 | #ifdef DEBUG_ESTIMATORS_ | |||
7056 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7056, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3,"do nothing about bitrate, just pass the packet loss to the codec\n"); | |||
7057 | #endif | |||
7058 | switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO, | |||
7059 | SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT, | |||
7060 | (void *)&rtp_session->rtcp_frame.reports[i].loss_avg, | |||
7061 | SCCT_NONE, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
7062 | } | |||
7063 | } | |||
7064 | } else { | |||
7065 | if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->rtcp_frame.reports[i].loss_avg != old_avg) { | |||
7066 | switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_AUDIO, | |||
7067 | SWITCH_IO_WRITE, SCC_AUDIO_PACKET_LOSS, SCCT_INT, | |||
7068 | (void *)&rtp_session->rtcp_frame.reports[i].loss_avg, | |||
7069 | SCCT_NONE, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
7070 | } | |||
7071 | } | |||
7072 | ||||
7073 | report++; | |||
7074 | } | |||
7075 | rtp_session->rtcp_frame.report_count = (uint16_t)i; | |||
7076 | ||||
7077 | ||||
7078 | ||||
7079 | ||||
7080 | ||||
7081 | rtp_session->rtcp_fresh_frame = 1; | |||
7082 | rtp_session->stats.rtcp.peer_ssrc = ntohl(packet_ssrc)__bswap_32 (packet_ssrc); | |||
7083 | } | |||
7084 | } | |||
7085 | ||||
7086 | if (msg->header.type > 194 && msg->header.type < 255) { | |||
7087 | status = SWITCH_STATUS_SUCCESS; | |||
7088 | } | |||
7089 | ||||
7090 | return status; | |||
7091 | } | |||
7092 | ||||
7093 | ||||
7094 | static switch_status_t process_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes) | |||
7095 | { | |||
7096 | switch_size_t len; | |||
7097 | switch_size_t remain = *bytes; | |||
7098 | switch_status_t status = SWITCH_STATUS_FALSE; | |||
7099 | rtcp_msg_t *msg = rtp_session->rtcp_recv_msg_p; | |||
7100 | ||||
7101 | if (remain < sizeof(switch_rtcp_ext_hdr_t) || remain > sizeof(rtcp_msg_t)) { | |||
7102 | return status; | |||
7103 | } | |||
7104 | if (msg->header.version != 2) { | |||
7105 | if (msg->header.version == 0) { | |||
7106 | if (rtp_session->ice.ice_user) { | |||
7107 | handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) msg, *bytes); | |||
7108 | } | |||
7109 | return SWITCH_STATUS_SUCCESS; | |||
7110 | } else { | |||
7111 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7111, (const char*)(rtp_session->session), | |||
7112 | SWITCH_LOG_WARNING, "Received an unsupported RTCP packet version %d\n", msg->header.version); | |||
7113 | return SWITCH_STATUS_FALSE; | |||
7114 | } | |||
7115 | } | |||
7116 | ||||
7117 | do { | |||
7118 | len = ((switch_size_t)ntohs(msg->header.length)__bswap_16 (msg->header.length) * 4) + 4; | |||
7119 | ||||
7120 | if (msg->header.version != 2 || !(msg->header.type > 191 && msg->header.type < 210)) { | |||
7121 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7121, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, | |||
7122 | "INVALID RTCP PACKET TYPE %d VER %d LEN %" SWITCH_SIZE_T_FMT"ld" "\n", msg->header.type, | |||
7123 | msg->header.version, len); | |||
7124 | status = SWITCH_STATUS_BREAK; | |||
7125 | break; | |||
7126 | } | |||
7127 | ||||
7128 | //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, | |||
7129 | //"WTF BYTES %ld REMAIN %ld PACKET TYPE %d LEN %ld\n", *bytes, remain, msg->header.type, len); | |||
7130 | ||||
7131 | if (len > remain) { | |||
7132 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7132, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, | |||
7133 | "RTCP INVALID LENGTH %" SWITCH_SIZE_T_FMT"ld" "\n", len); | |||
7134 | len = remain; | |||
7135 | } | |||
7136 | ||||
7137 | status = process_rtcp_report(rtp_session, msg, len); | |||
7138 | ||||
7139 | if (remain > len) { | |||
7140 | unsigned char *p = (unsigned char *) msg; | |||
7141 | p += len; | |||
7142 | msg = (rtcp_msg_t *) p; | |||
7143 | } | |||
7144 | ||||
7145 | remain -= len; | |||
7146 | ||||
7147 | } while (remain >= 4); | |||
7148 | ||||
7149 | return status; | |||
7150 | } | |||
7151 | ||||
7152 | static switch_status_t read_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags) | |||
7153 | { | |||
7154 | switch_status_t status = SWITCH_STATUS_FALSE; | |||
7155 | ||||
7156 | if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
7157 | return SWITCH_STATUS_FALSE; | |||
7158 | } | |||
7159 | ||||
7160 | switch_assert(bytes)((bytes) ? (void) (0) : __assert_fail ("bytes", "src/switch_rtp.c" , 7160, __extension__ __PRETTY_FUNCTION__)); | |||
7161 | ||||
7162 | *bytes = sizeof(rtcp_msg_t); | |||
7163 | ||||
7164 | if ((status = switch_socket_recvfrom(rtp_session->rtcp_from_addr, rtp_session->rtcp_sock_input, 0, (void *) rtp_session->rtcp_recv_msg_p, bytes)) | |||
7165 | != SWITCH_STATUS_SUCCESS) { | |||
7166 | *bytes = 0; | |||
7167 | } | |||
7168 | ||||
7169 | switch_mutex_lock(rtp_session->ice_mutex); | |||
7170 | if (rtp_session->rtcp_dtls) { | |||
7171 | char *b = (char *) rtp_session->rtcp_recv_msg_p; | |||
7172 | ||||
7173 | if (*b == 0 || *b == 1) { | |||
7174 | if (rtp_session->rtcp_ice.ice_user) { | |||
7175 | handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) rtp_session->rtcp_recv_msg_p, *bytes); | |||
7176 | } | |||
7177 | *bytes = 0; | |||
7178 | } | |||
7179 | ||||
7180 | if (*bytes && (*b >= 20) && (*b <= 64)) { | |||
7181 | rtp_session->rtcp_dtls->bytes = *bytes; | |||
7182 | rtp_session->rtcp_dtls->data = (void *) rtp_session->rtcp_recv_msg_p; | |||
7183 | } else { | |||
7184 | rtp_session->rtcp_dtls->bytes = 0; | |||
7185 | rtp_session->rtcp_dtls->data = NULL((void*)0); | |||
7186 | } | |||
7187 | ||||
7188 | do_dtls(rtp_session, rtp_session->rtcp_dtls); | |||
7189 | ||||
7190 | ||||
7191 | if (rtp_session->rtcp_dtls->bytes) { | |||
7192 | *bytes = 0; | |||
7193 | } | |||
7194 | } | |||
7195 | ||||
7196 | #ifdef ENABLE_SRTP1 | |||
7197 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->rtcp_recv_msg_p->header.version == 2) { | |||
7198 | //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->rtcp_recv_msg_p->header.version == 2)) { | |||
7199 | int sbytes = (int) *bytes; | |||
7200 | srtp_err_status_t stat = 0; | |||
7201 | ||||
7202 | ||||
7203 | if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) { | |||
7204 | stat = srtp_unprotect_rtcp(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes); | |||
7205 | } else { | |||
7206 | stat = srtp_unprotect_rtcp_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes, 1); | |||
7207 | } | |||
7208 | ||||
7209 | if (stat) { | |||
7210 | //++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++; | |||
7211 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7211, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "RTCP UNPROTECT ERR\n"); | |||
7212 | } else { | |||
7213 | //rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0; | |||
7214 | } | |||
7215 | ||||
7216 | *bytes = sbytes; | |||
7217 | ||||
7218 | } | |||
7219 | #endif | |||
7220 | ||||
7221 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
7222 | ||||
7223 | #ifdef ENABLE_ZRTP | |||
7224 | if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && rtp_session->rtcp_recv_msg_p->header.version == 2) { | |||
7225 | /* ZRTP Recv */ | |||
7226 | if (bytes) { | |||
7227 | unsigned int sbytes = (int) *bytes; | |||
7228 | zrtp_status_t stat = 0; | |||
7229 | ||||
7230 | stat = zrtp_process_srtcp(rtp_session->zrtp_stream, (void *) rtp_session->rtcp_recv_msg_p, &sbytes); | |||
7231 | ||||
7232 | switch (stat) { | |||
7233 | case zrtp_status_ok: | |||
7234 | *bytes = sbytes; | |||
7235 | break; | |||
7236 | case zrtp_status_drop: | |||
7237 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 7237, ((void*)0), SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat); | |||
7238 | *bytes = 0; | |||
7239 | break; | |||
7240 | case zrtp_status_fail: | |||
7241 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 7241, ((void*)0), SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat); | |||
7242 | *bytes = 0; | |||
7243 | break; | |||
7244 | default: | |||
7245 | break; | |||
7246 | } | |||
7247 | } | |||
7248 | } | |||
7249 | #endif | |||
7250 | ||||
7251 | /* RTCP Auto ADJ */ | |||
7252 | if (*bytes && rtp_session->flags[SWITCH_RTP_FLAG_RTCP_AUTOADJ] && switch_sockaddr_get_port(rtp_session->rtcp_from_addr)) { | |||
7253 | if (!switch_cmp_addr(rtp_session->rtcp_from_addr, rtp_session->rtcp_remote_addr)) { | |||
7254 | if (++rtp_session->rtcp_autoadj_tally >= rtp_session->rtcp_autoadj_threshold) { | |||
7255 | const char *err; | |||
7256 | uint32_t old = rtp_session->remote_rtcp_port; | |||
7257 | const char *tx_host; | |||
7258 | const char *old_host; | |||
7259 | char bufa[50], bufb[50]; | |||
7260 | ||||
7261 | tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_from_addr); | |||
7262 | old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr); | |||
7263 | ||||
7264 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7264, (const char*)(rtp_session->session), SWITCH_LOG_INFO, | |||
7265 | "Auto Changing %s RTCP port from %s:%u to %s:%u\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), old_host, old, tx_host, | |||
7266 | switch_sockaddr_get_port(rtp_session->rtcp_from_addr)); | |||
7267 | ||||
7268 | ||||
7269 | rtp_session->eff_remote_host_str = switch_core_strdup(rtp_session->pool, tx_host)switch_core_perform_strdup(rtp_session->pool, tx_host, "src/switch_rtp.c" , (const char *)__func__, 7269); | |||
7270 | rtp_session->remote_rtcp_port = switch_sockaddr_get_port(rtp_session->rtcp_from_addr); | |||
7271 | status = enable_remote_rtcp_socket(rtp_session, &err); | |||
7272 | rtp_session->rtcp_auto_adj_used = 1; | |||
7273 | ||||
7274 | if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) { | |||
7275 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ); | |||
7276 | } else { | |||
7277 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ); | |||
7278 | } | |||
7279 | } | |||
7280 | } else { | |||
7281 | ||||
7282 | if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) { | |||
7283 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ); | |||
7284 | } else { | |||
7285 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7285, (const char*)(rtp_session->session), | |||
7286 | SWITCH_LOG_DEBUG, "Correct %s RTCP ip/port confirmed.\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
7287 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ); | |||
7288 | } | |||
7289 | rtp_session->rtcp_auto_adj_used = 0; | |||
7290 | ||||
7291 | } | |||
7292 | } | |||
7293 | ||||
7294 | if (*bytes) { | |||
7295 | return process_rtcp_packet(rtp_session, bytes); | |||
7296 | } | |||
7297 | ||||
7298 | return status; | |||
7299 | } | |||
7300 | ||||
7301 | static void check_timeout(switch_rtp_t *rtp_session) | |||
7302 | { | |||
7303 | ||||
7304 | switch_time_t now = switch_micro_time_now(); | |||
7305 | uint32_t elapsed = 0; | |||
7306 | ||||
7307 | if (now >= rtp_session->last_media) { | |||
7308 | elapsed = (now - rtp_session->last_media) / 1000; | |||
7309 | } | |||
7310 | ||||
7311 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7311, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG10, | |||
7312 | "%s MEDIA TIMEOUT %s %d/%d", switch_core_session_get_name(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_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), | |||
7313 | elapsed, rtp_session->media_timeout); | |||
7314 | ||||
7315 | if (elapsed > rtp_session->media_timeout) { | |||
7316 | ||||
7317 | if (rtp_session->session) { | |||
7318 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
7319 | ||||
7320 | switch_channel_execute_on(channel, "execute_on_media_timeout"); | |||
7321 | switch_channel_hangup(channel, SWITCH_CAUSE_MEDIA_TIMEOUT)switch_channel_perform_hangup(channel, "src/switch_rtp.c", (const char *)__func__, 7321, SWITCH_CAUSE_MEDIA_TIMEOUT); | |||
7322 | } | |||
7323 | } | |||
7324 | } | |||
7325 | ||||
7326 | static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_type, | |||
7327 | payload_map_t **pmapP, switch_frame_flag_t *flags, switch_io_flag_t io_flags) | |||
7328 | { | |||
7329 | ||||
7330 | switch_channel_t *channel = NULL((void*)0); | |||
7331 | switch_size_t bytes = 0; | |||
7332 | switch_size_t rtcp_bytes = 0; | |||
7333 | switch_status_t status = SWITCH_STATUS_SUCCESS, poll_status = SWITCH_STATUS_SUCCESS; | |||
7334 | switch_status_t rtcp_status = SWITCH_STATUS_SUCCESS, rtcp_poll_status = SWITCH_STATUS_SUCCESS; | |||
7335 | int check = 0; | |||
7336 | int ret = -1; | |||
7337 | int sleep_mss = 1000; | |||
7338 | int poll_sec = 5; | |||
7339 | int poll_loop = 0; | |||
7340 | int fdr = 0; | |||
7341 | int rtcp_fdr = 0; | |||
7342 | int hot_socket = 0; | |||
7343 | int read_loops = 0; | |||
7344 | int slept = 0; | |||
7345 | switch_bool_t got_jb = SWITCH_FALSE; | |||
7346 | ||||
7347 | if (!switch_rtp_ready(rtp_session)) { | |||
7348 | return -1; | |||
7349 | } | |||
7350 | ||||
7351 | if (rtp_session->session) { | |||
7352 | channel = switch_core_session_get_channel(rtp_session->session); | |||
7353 | } | |||
7354 | ||||
7355 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { | |||
7356 | sleep_mss = rtp_session->timer.interval * 1000; | |||
7357 | } | |||
7358 | ||||
7359 | READ_INC(rtp_session)switch_mutex_lock(rtp_session->read_mutex); rtp_session-> reading++; | |||
7360 | ||||
7361 | ||||
7362 | ||||
7363 | while (switch_rtp_ready(rtp_session)) { | |||
7364 | int do_cng = 0; | |||
7365 | int read_pretriggered = 0; | |||
7366 | int has_rtcp = 0; | |||
7367 | int got_rtp_poll = 0; | |||
7368 | ||||
7369 | bytes = 0; | |||
7370 | ||||
7371 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && | |||
7372 | !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && | |||
7373 | !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && | |||
7374 | !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && | |||
7375 | rtp_session->read_pollfd) { | |||
7376 | ||||
7377 | if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) { | |||
7378 | while (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) { | |||
7379 | status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, SWITCH_STATUS_SUCCESS, SWITCH_FALSE); | |||
7380 | ||||
7381 | if (status == SWITCH_STATUS_GENERR) { | |||
7382 | ret = -1; | |||
7383 | goto end; | |||
7384 | } | |||
7385 | ||||
7386 | if ((*flags & SFF_RTCP)) { | |||
7387 | *flags &= ~SFF_RTCP; | |||
7388 | has_rtcp = 1; | |||
7389 | read_pretriggered = 0; | |||
7390 | goto rtcp; | |||
7391 | } | |||
7392 | ||||
7393 | if (status == SWITCH_STATUS_BREAK) { | |||
7394 | read_pretriggered = 1; | |||
7395 | break; | |||
7396 | } | |||
7397 | } | |||
7398 | ||||
7399 | } else if ((rtp_session->flags[SWITCH_RTP_FLAG_AUTOFLUSH] || rtp_session->flags[SWITCH_RTP_FLAG_STICKY_FLUSH])) { | |||
7400 | ||||
7401 | if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) { | |||
7402 | status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, SWITCH_STATUS_SUCCESS, SWITCH_FALSE); | |||
7403 | if (status == SWITCH_STATUS_GENERR) { | |||
7404 | ret = -1; | |||
7405 | goto end; | |||
7406 | } | |||
7407 | if ((*flags & SFF_RTCP)) { | |||
7408 | *flags &= ~SFF_RTCP; | |||
7409 | has_rtcp = 1; | |||
7410 | read_pretriggered = 0; | |||
7411 | goto rtcp; | |||
7412 | } | |||
7413 | ||||
7414 | /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Initial (%i) %d\n", status, bytes); */ | |||
7415 | if (status != SWITCH_STATUS_FALSE) { | |||
7416 | read_pretriggered = 1; | |||
7417 | } | |||
7418 | ||||
7419 | if (bytes) { | |||
7420 | if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) { | |||
7421 | rtp_session->hot_hits++;//+= rtp_session->samples_per_interval; | |||
7422 | ||||
7423 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7423, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG10, "%s Hot Hit %d\n", | |||
7424 | rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel (rtp_session->session)) : "-", | |||
7425 | rtp_session->hot_hits); | |||
7426 | } else { | |||
7427 | rtp_session->hot_hits = 0; | |||
7428 | } | |||
7429 | } | |||
7430 | ||||
7431 | if (rtp_session->hot_hits > 1 && !rtp_session->sync_packets) {// >= (rtp_session->samples_per_second * 30)) { | |||
7432 | hot_socket = 1; | |||
7433 | } | |||
7434 | } else { | |||
7435 | rtp_session->hot_hits = 0; | |||
7436 | } | |||
7437 | } | |||
7438 | ||||
7439 | if (rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) { | |||
7440 | ///NOOP | |||
7441 | } else if (hot_socket && (rtp_session->hot_hits % 10) != 0) { | |||
7442 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7442, (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)) : "-"); | |||
7443 | switch_core_timer_next(&rtp_session->timer); | |||
7444 | } else if (hot_socket) { | |||
7445 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7445, (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)) : "-"); | |||
7446 | rtp_session->sync_packets++; | |||
7447 | switch_core_timer_sync(&rtp_session->timer); | |||
7448 | reset_jitter_seq(rtp_session); | |||
7449 | } else { | |||
7450 | ||||
7451 | if (rtp_session->sync_packets) { | |||
7452 | ||||
7453 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7453, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG10, | |||
7454 | "%s Auto-Flush catching up %d packets (%d)ms.\n", | |||
7455 | rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel (rtp_session->session)) : "-", | |||
7456 | rtp_session->sync_packets, (rtp_session->ms_per_packet * rtp_session->sync_packets) / 1000); | |||
7457 | if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) { | |||
7458 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7458, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, "%s syncing %d %s packet(s)\n", | |||
7459 | rtp_session_name(rtp_session)rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel (rtp_session->session)) : "-", | |||
7460 | rtp_session->sync_packets, rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
7461 | ||||
7462 | rtp_session->bad_stream++; | |||
7463 | rtp_session->stats.inbound.flaws += rtp_session->sync_packets; | |||
7464 | ||||
7465 | if (rtp_session->stats.inbound.error_log) { | |||
7466 | rtp_session->stats.inbound.error_log->flaws += rtp_session->sync_packets; | |||
7467 | } | |||
7468 | } | |||
7469 | ||||
7470 | switch_core_timer_sync(&rtp_session->timer); | |||
7471 | reset_jitter_seq(rtp_session); | |||
7472 | rtp_session->hot_hits = 0; | |||
7473 | } else { | |||
7474 | if (slept) { | |||
7475 | switch_cond_next(); | |||
7476 | } else { | |||
7477 | if (rtp_session->skip_timer) { | |||
7478 | rtp_session->skip_timer = 0; | |||
7479 | switch_cond_next(); | |||
7480 | } else { | |||
7481 | switch_core_timer_next(&rtp_session->timer); | |||
7482 | } | |||
7483 | slept++; | |||
7484 | } | |||
7485 | ||||
7486 | } | |||
7487 | ||||
7488 | rtp_session->sync_packets = 0; | |||
7489 | } | |||
7490 | } | |||
7491 | ||||
7492 | rtp_session->stats.read_count++; | |||
7493 | ||||
7494 | recvfrom: | |||
7495 | ||||
7496 | if (!read_pretriggered) { | |||
7497 | bytes = 0; | |||
7498 | } | |||
7499 | read_loops++; | |||
7500 | //poll_loop = 0; | |||
7501 | ||||
7502 | if (!switch_rtp_ready(rtp_session)) { | |||
7503 | break; | |||
7504 | } | |||
7505 | ||||
7506 | if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->read_pollfd) { | |||
7507 | int pt = poll_sec * 1000000; | |||
7508 | ||||
7509 | do_2833(rtp_session); | |||
7510 | ||||
7511 | if (rtp_session->dtmf_data.out_digit_dur > 0 || rtp_session->dtmf_data.in_digit_sanity || rtp_session->sending_dtmf || | |||
7512 | switch_queue_size(rtp_session->dtmf_data.dtmf_queue) || switch_queue_size(rtp_session->dtmf_data.dtmf_inqueue)) { | |||
7513 | pt = 20000; | |||
7514 | } | |||
7515 | ||||
7516 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) { | |||
7517 | pt = 100000; | |||
7518 | } | |||
7519 | ||||
7520 | if (rtp_session->vb && !rtp_session->pause_jb) { | |||
7521 | if (switch_jb_poll(rtp_session->vb)) { | |||
7522 | pt = 1000; | |||
7523 | } | |||
7524 | } | |||
7525 | ||||
7526 | if ((io_flags & SWITCH_IO_FLAG_NOBLOCK)) { | |||
7527 | pt = 0; | |||
7528 | } | |||
7529 | ||||
7530 | poll_status = switch_poll(rtp_session->read_pollfd, 1, &fdr, pt); | |||
7531 | ||||
7532 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && poll_status != SWITCH_STATUS_SUCCESS && rtp_session->media_timeout && rtp_session->last_media) { | |||
7533 | check_timeout(rtp_session); | |||
7534 | } | |||
7535 | ||||
7536 | if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->dtmf_data.out_digit_dur > 0) { | |||
7537 | return_cng_frame()do_cng = 1; goto timer_check; | |||
7538 | } | |||
7539 | ||||
7540 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->flags[SWITCH_RTP_FLAG_BREAK]) { | |||
7541 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_BREAK); | |||
7542 | bytes = 0; | |||
7543 | reset_jitter_seq(rtp_session); | |||
7544 | return_cng_frame()do_cng = 1; goto timer_check; | |||
7545 | } | |||
7546 | ||||
7547 | } | |||
7548 | ||||
7549 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
7550 | got_jb = (rtp_session->vb && !rtp_session->pause_jb && switch_jb_poll(rtp_session->vb)); | |||
7551 | } else { | |||
7552 | got_jb = SWITCH_TRUE; | |||
7553 | } | |||
7554 | ||||
7555 | if (poll_status == SWITCH_STATUS_SUCCESS || got_jb) { | |||
7556 | ||||
7557 | got_rtp_poll = 1; | |||
7558 | ||||
7559 | if (read_pretriggered) { | |||
7560 | read_pretriggered = 0; | |||
7561 | } else { | |||
7562 | ||||
7563 | ||||
7564 | status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, poll_status, got_jb); | |||
7565 | ||||
7566 | if (status == SWITCH_STATUS_GENERR) { | |||
7567 | ret = -1; | |||
7568 | goto end; | |||
7569 | } | |||
7570 | ||||
7571 | if (rtp_session->max_missed_packets && read_loops == 1 && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && | |||
7572 | !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
7573 | if (bytes && status == SWITCH_STATUS_SUCCESS) { | |||
7574 | rtp_session->missed_count = 0; | |||
7575 | } else { | |||
7576 | if (rtp_session->media_timeout && rtp_session->last_media) { | |||
7577 | check_timeout(rtp_session); | |||
7578 | } else { | |||
7579 | if (++rtp_session->missed_count >= rtp_session->max_missed_packets) { | |||
7580 | ret = -2; | |||
7581 | goto end; | |||
7582 | } | |||
7583 | } | |||
7584 | } | |||
7585 | } | |||
7586 | ||||
7587 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
7588 | //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "Read bytes (%i) %ld\n", status, bytes); | |||
7589 | ||||
7590 | if (bytes == 0) { | |||
7591 | if (check_rtcp_and_ice(rtp_session) == -1) { | |||
7592 | ret = -1; | |||
7593 | goto end; | |||
7594 | } | |||
7595 | // This is dumb | |||
7596 | //switch_rtp_video_refresh(rtp_session); | |||
7597 | goto rtcp; | |||
7598 | } | |||
7599 | } | |||
7600 | ||||
7601 | if ((*flags & SFF_PROXY_PACKET)) { | |||
7602 | ret = (int) bytes; | |||
7603 | goto end; | |||
7604 | } | |||
7605 | ||||
7606 | if ((*flags & SFF_RTCP)) { | |||
7607 | *flags &= ~SFF_RTCP; | |||
7608 | has_rtcp = 1; | |||
7609 | goto rtcp; | |||
7610 | } | |||
7611 | ||||
7612 | ||||
7613 | } | |||
7614 | poll_loop = 0; | |||
7615 | } else { | |||
7616 | ||||
7617 | if (!switch_rtp_ready(rtp_session)) { | |||
7618 | ret = -1; | |||
7619 | goto end; | |||
7620 | } | |||
7621 | ||||
7622 | 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) { | |||
7623 | char tmp[128] = ""; | |||
7624 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7624, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Poll failed with error: %d [%s]\n", | |||
7625 | poll_status, switch_strerror_r(poll_status, tmp, sizeof(tmp))); | |||
7626 | ret = -1; | |||
7627 | goto end; | |||
7628 | } | |||
7629 | ||||
7630 | if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
7631 | rtp_session->missed_count += (poll_sec * 1000) / (rtp_session->ms_per_packet ? rtp_session->ms_per_packet / 1000 : 20); | |||
7632 | bytes = 0; | |||
7633 | ||||
7634 | if (rtp_session->media_timeout && rtp_session->last_media) { | |||
7635 | check_timeout(rtp_session); | |||
7636 | } else if (rtp_session->max_missed_packets) { | |||
7637 | if (rtp_session->missed_count >= rtp_session->max_missed_packets) { | |||
7638 | ret = -2; | |||
7639 | goto end; | |||
7640 | } | |||
7641 | } | |||
7642 | } | |||
7643 | ||||
7644 | ||||
7645 | if (check_rtcp_and_ice(rtp_session) == -1) { | |||
7646 | ret = -1; | |||
7647 | goto end; | |||
7648 | } | |||
7649 | ||||
7650 | ||||
7651 | if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) && | |||
7652 | (rtp_session->dtmf_data.out_digit_dur == 0) && !rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
7653 | return_cng_frame()do_cng = 1; goto timer_check; | |||
7654 | } | |||
7655 | } | |||
7656 | ||||
7657 | rtcp: | |||
7658 | ||||
7659 | if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
7660 | rtcp_poll_status = SWITCH_STATUS_FALSE; | |||
7661 | ||||
7662 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX] && has_rtcp) { | |||
7663 | if (rtp_session->rtcp_recv_msg_p->header.version == 2) { //rtcp muxed | |||
7664 | rtp_session->rtcp_from_addr = rtp_session->from_addr; | |||
7665 | rtcp_status = rtcp_poll_status = SWITCH_STATUS_SUCCESS; | |||
7666 | rtcp_bytes = bytes; | |||
7667 | } | |||
7668 | ||||
7669 | has_rtcp = 0; | |||
7670 | ||||
7671 | } else if (rtp_session->rtcp_read_pollfd) { | |||
7672 | rtcp_poll_status = switch_poll(rtp_session->rtcp_read_pollfd, 1, &rtcp_fdr, 0); | |||
7673 | } | |||
7674 | ||||
7675 | if (rtcp_poll_status == SWITCH_STATUS_SUCCESS) { | |||
7676 | ||||
7677 | if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
7678 | rtcp_status = read_rtcp_packet(rtp_session, &rtcp_bytes, flags); | |||
7679 | } | |||
7680 | ||||
7681 | if (rtcp_status == SWITCH_STATUS_SUCCESS) { | |||
7682 | switch_rtp_reset_media_timer(rtp_session); | |||
7683 | ||||
7684 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) { | |||
7685 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
7686 | const char *uuid = switch_channel_get_partner_uuid(channel); | |||
7687 | ||||
7688 | if (uuid) { | |||
7689 | switch_core_session_t *other_session; | |||
7690 | switch_rtp_t *other_rtp_session = NULL((void*)0); | |||
7691 | ||||
7692 | if ((other_session = switch_core_session_locate(uuid)switch_core_session_perform_locate(uuid, "src/switch_rtp.c", ( const char *)__func__, 7692))) { | |||
7693 | switch_channel_t *other_channel = switch_core_session_get_channel(other_session); | |||
7694 | if ((other_rtp_session = switch_channel_get_private(other_channel, "__rtcp_audio_rtp_session")) && | |||
7695 | other_rtp_session->rtcp_sock_output && | |||
7696 | switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_ENABLE_RTCP)) { | |||
7697 | other_rtp_session->rtcp_send_msg = rtp_session->rtcp_recv_msg; | |||
7698 | ||||
7699 | #ifdef ENABLE_SRTP1 | |||
7700 | switch_mutex_lock(other_rtp_session->ice_mutex); | |||
7701 | if (switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_SECURE_SEND)) { | |||
7702 | int stat = 0; | |||
7703 | int sbytes = (int) rtcp_bytes; | |||
7704 | ||||
7705 | if (!other_rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) { | |||
7706 | stat = srtp_protect_rtcp(other_rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &other_rtp_session->rtcp_send_msg.header, &sbytes); | |||
7707 | } else { | |||
7708 | stat = srtp_protect_rtcp_mki(other_rtp_session->send_ctx[other_rtp_session->srtp_idx_rtcp], &other_rtp_session->rtcp_send_msg.header, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX0); | |||
7709 | } | |||
7710 | ||||
7711 | if (stat) { | |||
7712 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7712, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat); | |||
7713 | } | |||
7714 | rtcp_bytes = sbytes; | |||
7715 | } | |||
7716 | switch_mutex_unlock(other_rtp_session->ice_mutex); | |||
7717 | #endif | |||
7718 | ||||
7719 | #ifdef ENABLE_ZRTP | |||
7720 | /* ZRTP Send */ | |||
7721 | if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) { | |||
7722 | unsigned int sbytes = (unsigned int) bytes; | |||
7723 | zrtp_status_t stat = zrtp_status_fail; | |||
7724 | ||||
7725 | stat = zrtp_process_rtcp(other_rtp_session->zrtp_stream, (void *) &other_rtp_session->rtcp_send_msg, &sbytes); | |||
7726 | ||||
7727 | switch (stat) { | |||
7728 | case zrtp_status_ok: | |||
7729 | break; | |||
7730 | case zrtp_status_drop: | |||
7731 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7731, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat); | |||
7732 | ret = (int) bytes; | |||
7733 | goto end; | |||
7734 | break; | |||
7735 | case zrtp_status_fail: | |||
7736 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7736, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat); | |||
7737 | break; | |||
7738 | default: | |||
7739 | break; | |||
7740 | } | |||
7741 | ||||
7742 | bytes = sbytes; | |||
7743 | } | |||
7744 | #endif | |||
7745 | if (switch_socket_sendto(other_rtp_session->rtcp_sock_output, other_rtp_session->rtcp_remote_addr, 0, | |||
7746 | (const char*)&other_rtp_session->rtcp_send_msg, &rtcp_bytes ) != SWITCH_STATUS_SUCCESS) { | |||
7747 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7747, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG,"RTCP packet not written\n"); | |||
7748 | } | |||
7749 | ||||
7750 | ||||
7751 | } | |||
7752 | switch_core_session_rwunlock(other_session); | |||
7753 | } | |||
7754 | } | |||
7755 | ||||
7756 | } | |||
7757 | ||||
7758 | if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { | |||
7759 | process_rtcp_packet(rtp_session, &rtcp_bytes); | |||
7760 | ret = 1; | |||
7761 | ||||
7762 | if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
7763 | switch_core_timer_sync(&rtp_session->timer); | |||
7764 | reset_jitter_seq(rtp_session); | |||
7765 | } | |||
7766 | goto recvfrom; | |||
7767 | } | |||
7768 | } | |||
7769 | } | |||
7770 | } | |||
7771 | ||||
7772 | if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) && | |||
7773 | (rtp_session->dtmf_data.out_digit_dur == 0) && !got_rtp_poll) { | |||
7774 | return_cng_frame()do_cng = 1; goto timer_check; | |||
7775 | } | |||
7776 | ||||
7777 | if (!bytes && (io_flags & SWITCH_IO_FLAG_NOBLOCK)) { | |||
7778 | rtp_session->missed_count = 0; | |||
7779 | ret = 0; | |||
7780 | goto end; | |||
7781 | } | |||
7782 | ||||
7783 | check = !bytes; | |||
7784 | ||||
7785 | if (rtp_session->flags[SWITCH_RTP_FLAG_FLUSH]) { | |||
7786 | bytes = do_flush(rtp_session, SWITCH_FALSE, bytes); | |||
7787 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_FLUSH); | |||
7788 | } | |||
7789 | ||||
7790 | if ((!bytes && rtp_session->flags[SWITCH_RTP_FLAG_BREAK]) || (bytes && bytes == 4 && *((int *) &rtp_session->recv_msg) == UINT_MAX(2147483647 *2U +1U))) { | |||
7791 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_BREAK); | |||
7792 | ||||
7793 | if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] || !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] || | |||
7794 | rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] || | |||
7795 | (bytes && bytes < 5) || (!bytes && poll_loop)) { | |||
7796 | bytes = 0; | |||
7797 | reset_jitter_seq(rtp_session); | |||
7798 | return_cng_frame()do_cng = 1; goto timer_check; | |||
7799 | } | |||
7800 | } | |||
7801 | ||||
7802 | if (bytes && bytes < 5) { | |||
7803 | continue; | |||
7804 | } | |||
7805 | ||||
7806 | if (!bytes && poll_loop) { | |||
7807 | goto recvfrom; | |||
7808 | } | |||
7809 | ||||
7810 | if (bytes && rtp_session->last_rtp_hdr.m && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te && | |||
7811 | !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && | |||
7812 | !rtp_session->flags[SWITCH_RTP_FLAG_TEXT] && | |||
7813 | !(rtp_session->rtp_bugs & RTP_BUG_IGNORE_MARK_BIT)) { | |||
7814 | rtp_flush_read_buffer(rtp_session, SWITCH_RTP_FLUSH_ONCE); | |||
7815 | } | |||
7816 | ||||
7817 | if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) { | |||
7818 | *flags |= SFF_NOT_AUDIO; | |||
7819 | } else { | |||
7820 | *flags &= ~SFF_NOT_AUDIO; /* If this flag was already set, make sure to remove it when we get real audio */ | |||
7821 | } | |||
7822 | ||||
7823 | /* ignore packets not meant for us unless the auto-adjust window is open (ice mode has its own alternatives to this) */ | |||
7824 | if (!using_ice(rtp_session) && bytes) { | |||
7825 | if (rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ]) { | |||
7826 | if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) { | |||
7827 | goto recvfrom; | |||
7828 | ||||
7829 | } | |||
7830 | } else if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PACKETS) && !switch_cmp_addr(rtp_session->rtp_from_addr, rtp_session->remote_addr)) { | |||
7831 | goto recvfrom; | |||
7832 | } | |||
7833 | } | |||
7834 | ||||
7835 | if (bytes && rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ] && switch_sockaddr_get_port(rtp_session->rtp_from_addr)) { | |||
7836 | if (!switch_cmp_addr(rtp_session->rtp_from_addr, rtp_session->remote_addr)) { | |||
7837 | if (++rtp_session->autoadj_tally >= rtp_session->autoadj_threshold) { | |||
7838 | const char *err; | |||
7839 | uint32_t old = rtp_session->remote_port; | |||
7840 | const char *tx_host; | |||
7841 | const char *old_host; | |||
7842 | char bufa[50], bufb[50]; | |||
7843 | char adj_port[6]; | |||
7844 | ||||
7845 | tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr); | |||
7846 | old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr); | |||
7847 | ||||
7848 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7848, (const char*)(rtp_session->session), SWITCH_LOG_INFO, | |||
7849 | "Auto Changing %s port from %s:%u to %s:%u\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), old_host, old, tx_host, | |||
7850 | switch_sockaddr_get_port(rtp_session->rtp_from_addr)); | |||
7851 | ||||
7852 | if (channel) { | |||
7853 | char varname[80] = ""; | |||
7854 | ||||
7855 | switch_snprintf(varname, sizeof(varname), "remote_%s_ip_reported", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
7856 | switch_channel_set_variable(channel, varname, switch_channel_get_variable(channel, "remote_media_ip"))switch_channel_set_variable_var_check(channel, varname, switch_channel_get_variable_dup (channel, "remote_media_ip", SWITCH_TRUE, -1), SWITCH_TRUE); | |||
7857 | ||||
7858 | switch_snprintf(varname, sizeof(varname), "remote_%s_ip", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
7859 | switch_channel_set_variable(channel, varname, tx_host)switch_channel_set_variable_var_check(channel, varname, tx_host , SWITCH_TRUE); | |||
7860 | ||||
7861 | switch_snprintf(varname, sizeof(varname), "remote_%s_port_reported", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
7862 | switch_snprintf(adj_port, sizeof(adj_port), "%u", switch_sockaddr_get_port(rtp_session->rtp_from_addr)); | |||
7863 | switch_channel_set_variable(channel, varname, switch_channel_get_variable(channel, "remote_media_port"))switch_channel_set_variable_var_check(channel, varname, switch_channel_get_variable_dup (channel, "remote_media_port", SWITCH_TRUE, -1), SWITCH_TRUE); | |||
7864 | ||||
7865 | switch_snprintf(varname, sizeof(varname), "remote_%s_port", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
7866 | switch_channel_set_variable(channel, varname, adj_port)switch_channel_set_variable_var_check(channel, varname, adj_port , SWITCH_TRUE); | |||
7867 | ||||
7868 | switch_snprintf(varname, sizeof(varname), "rtp_auto_adjust_%s", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
7869 | switch_channel_set_variable(channel, varname, "true")switch_channel_set_variable_var_check(channel, varname, "true" , SWITCH_TRUE); | |||
7870 | } | |||
7871 | rtp_session->auto_adj_used = 1; | |||
7872 | switch_rtp_set_remote_address(rtp_session, tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr), 0, SWITCH_FALSE, &err); | |||
7873 | if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) { | |||
7874 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ); | |||
7875 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ); | |||
7876 | } else { | |||
7877 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ); | |||
7878 | } | |||
7879 | if (rtp_session->ice.ice_user) { | |||
7880 | rtp_session->ice.addr = rtp_session->remote_addr; | |||
7881 | } | |||
7882 | } | |||
7883 | } else { | |||
7884 | if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) { | |||
7885 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ); | |||
7886 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_AUTOADJ); | |||
7887 | } else { | |||
7888 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7888, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "Correct %s ip/port confirmed.\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
7889 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ); | |||
7890 | } | |||
7891 | rtp_session->auto_adj_used = 0; | |||
7892 | } | |||
7893 | } | |||
7894 | ||||
7895 | if (bytes && !(rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST) && rtp_session->autoadj_window) { | |||
7896 | if (--rtp_session->autoadj_window == 0) { | |||
7897 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ); | |||
7898 | } | |||
7899 | } | |||
7900 | ||||
7901 | if (rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) { | |||
7902 | if (!bytes) { | |||
7903 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { | |||
7904 | switch_core_timer_next(&rtp_session->timer); | |||
7905 | } | |||
7906 | return_cng_frame()do_cng = 1; goto timer_check; | |||
7907 | } else { | |||
7908 | *payload_type = rtp_session->last_rtp_hdr.pt; | |||
7909 | ret = (int) bytes; | |||
7910 | goto end; | |||
7911 | } | |||
7912 | } | |||
7913 | ||||
7914 | if (bytes && (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL])) { | |||
7915 | /* Fast PASS! */ | |||
7916 | *flags |= SFF_PROXY_PACKET; | |||
7917 | ||||
7918 | if (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
7919 | #if 0 | |||
7920 | if (rtp_session->has_rtp && check_recv_payload(rtp_session)) { | |||
7921 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7921, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, | |||
7922 | "Ignoring udptl packet of size of %ld bytes that looks strikingly like a RTP packet.\n", (long)bytes); | |||
7923 | bytes = 0; | |||
7924 | goto do_continue; | |||
7925 | } | |||
7926 | #endif | |||
7927 | *flags |= SFF_UDPTL_PACKET; | |||
7928 | } | |||
7929 | ||||
7930 | ret = (int) bytes; | |||
7931 | goto end; | |||
7932 | } | |||
7933 | ||||
7934 | if (bytes) { | |||
7935 | rtp_session->missed_count = 0; | |||
7936 | ||||
7937 | if (bytes < rtp_header_len12) { | |||
7938 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 7938, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, "Ignoring invalid RTP packet size of %ld bytes.\n", (long)bytes); | |||
7939 | bytes = 0; | |||
7940 | goto do_continue; | |||
7941 | } | |||
7942 | ||||
7943 | if (rtp_session->last_rtp_hdr.pt && (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13)) { | |||
7944 | return_cng_frame()do_cng = 1; goto timer_check; | |||
7945 | } | |||
7946 | } | |||
7947 | ||||
7948 | if (check || bytes) { | |||
7949 | do_2833(rtp_session); | |||
7950 | } | |||
7951 | ||||
7952 | if (bytes && rtp_session->recv_msg.header.version != 2) { | |||
7953 | 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); | |||
7954 | ||||
7955 | //if (rtp_session->recv_msg.header.version == 0) { | |||
7956 | // if (rtp_session->ice.ice_user) { | |||
7957 | // handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, bytes); | |||
7958 | // goto recvfrom; | |||
7959 | // } | |||
7960 | //} | |||
7961 | ||||
7962 | if (rtp_session->invalid_handler) { | |||
7963 | rtp_session->invalid_handler(rtp_session, rtp_session->sock_input, (void *) &rtp_session->recv_msg, bytes, rtp_session->rtp_from_addr); | |||
7964 | } | |||
7965 | ||||
7966 | memset(data, 0, 2); | |||
7967 | data[0] = 65; | |||
7968 | ||||
7969 | rtp_session->last_rtp_hdr.pt = rtp_session->cng_pt != INVALID_PT ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD13; | |||
7970 | *flags |= SFF_CNG; | |||
7971 | *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt; | |||
7972 | ret = 2 + rtp_header_len12; | |||
7973 | goto end; | |||
7974 | } else if (bytes) { | |||
7975 | rtp_session->stats.inbound.period_packet_count++; | |||
7976 | } | |||
7977 | ||||
7978 | ||||
7979 | /* Handle incoming RFC2833 packets */ | |||
7980 | switch (handle_rfc2833(rtp_session, bytes, &do_cng)) { | |||
7981 | case RESULT_GOTO_END: | |||
7982 | goto end; | |||
7983 | case RESULT_GOTO_RECVFROM: | |||
7984 | goto recvfrom; | |||
7985 | case RESULT_GOTO_TIMERCHECK: | |||
7986 | goto timer_check; | |||
7987 | case RESULT_CONTINUE: | |||
7988 | status = SWITCH_STATUS_SUCCESS; | |||
7989 | goto result_continue; | |||
7990 | } | |||
7991 | ||||
7992 | result_continue: | |||
7993 | timer_check: | |||
7994 | ||||
7995 | if (!rtp_session->media_timeout && rtp_session->flags[SWITCH_RTP_FLAG_MUTE]) { | |||
7996 | do_cng++; | |||
7997 | } | |||
7998 | ||||
7999 | if (do_cng) { | |||
8000 | 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); | |||
8001 | ||||
8002 | do_2833(rtp_session); | |||
8003 | ||||
8004 | if (rtp_session->last_cng_ts == rtp_session->last_read_ts + rtp_session->samples_per_interval) { | |||
8005 | rtp_session->last_cng_ts = 0; | |||
8006 | } else { | |||
8007 | rtp_session->last_cng_ts = rtp_session->last_read_ts + rtp_session->samples_per_interval; | |||
8008 | } | |||
8009 | ||||
8010 | memset(data, 0, 2); | |||
8011 | data[0] = 65; | |||
8012 | rtp_session->last_rtp_hdr.pt = rtp_session->cng_pt != INVALID_PT ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD13; | |||
8013 | *flags |= SFF_CNG; | |||
8014 | *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt; | |||
8015 | ret = 2 + rtp_header_len12; | |||
8016 | rtp_session->stats.inbound.skip_packet_count++; | |||
8017 | goto end; | |||
8018 | } | |||
8019 | ||||
8020 | ||||
8021 | if (check || (bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER])) { | |||
8022 | if (!bytes && rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { /* We're late! We're Late! */ | |||
8023 | if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] && status == SWITCH_STATUS_BREAK) { | |||
8024 | switch_cond_next(); | |||
8025 | continue; | |||
8026 | } | |||
8027 | ||||
8028 | ||||
8029 | ||||
8030 | if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] && !rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] && !rtp_session->dtmf_data.in_digit_ts | |||
8031 | && rtp_session->cng_count > (rtp_session->one_second * 2) && rtp_session->jitter_lead > JITTER_LEAD_FRAMES10) { | |||
8032 | ||||
8033 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8033, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, "%s %s timeout\n", | |||
8034 | 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_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
8035 | ||||
8036 | if (rtp_session->media_timeout && rtp_session->last_media) { | |||
8037 | check_timeout(rtp_session); | |||
8038 | } | |||
8039 | ||||
8040 | if (rtp_session->stats.inbound.error_log) { | |||
8041 | rtp_session->stats.inbound.error_log->flaws++; | |||
8042 | } | |||
8043 | rtp_session->stats.inbound.flaws++; | |||
8044 | do_mos(rtp_session); | |||
8045 | } | |||
8046 | ||||
8047 | rtp_session->cng_count++; | |||
8048 | return_cng_frame()do_cng = 1; goto timer_check; | |||
8049 | } | |||
8050 | } | |||
8051 | ||||
8052 | rtp_session->cng_count = 0; | |||
8053 | ||||
8054 | if (status == SWITCH_STATUS_BREAK || bytes == 0) { | |||
8055 | if (!(io_flags & SWITCH_IO_FLAG_SINGLE_READ) && rtp_session->flags[SWITCH_RTP_FLAG_DATAWAIT]) { | |||
8056 | goto do_continue; | |||
8057 | } | |||
8058 | return_cng_frame()do_cng = 1; goto timer_check; | |||
8059 | } | |||
8060 | ||||
8061 | if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->last_rtp_hdr.pt == 102) { | |||
8062 | rtp_session->last_rtp_hdr.pt = 97; | |||
8063 | } | |||
8064 | ||||
8065 | break; | |||
8066 | ||||
8067 | do_continue: | |||
8068 | ||||
8069 | if (!bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) { | |||
8070 | ||||
8071 | if (sleep_mss) { | |||
8072 | switch_yield(sleep_mss)switch_sleep(sleep_mss);; | |||
8073 | } | |||
8074 | } | |||
8075 | ||||
8076 | } | |||
8077 | ||||
8078 | if (switch_rtp_ready(rtp_session)) { | |||
8079 | *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt; | |||
8080 | ||||
8081 | if (*payload_type == SWITCH_RTP_CNG_PAYLOAD13) { | |||
8082 | *flags |= SFF_CNG; | |||
8083 | } | |||
8084 | ||||
8085 | ret = (int) bytes; | |||
8086 | } else { | |||
8087 | ret = -1; | |||
8088 | } | |||
8089 | ||||
8090 | end: | |||
8091 | ||||
8092 | READ_DEC(rtp_session)rtp_session->reading--; switch_mutex_unlock(rtp_session-> read_mutex); | |||
8093 | ||||
8094 | return ret; | |||
8095 | } | |||
8096 | ||||
8097 | ||||
8098 | SWITCH_DECLARE(switch_byte_t)__attribute__((visibility("default"))) switch_byte_t switch_rtp_check_auto_adj(switch_rtp_t *rtp_session) | |||
8099 | { | |||
8100 | return rtp_session->auto_adj_used; | |||
8101 | } | |||
8102 | ||||
8103 | SWITCH_DECLARE(switch_size_t)__attribute__((visibility("default"))) switch_size_t switch_rtp_has_dtmf(switch_rtp_t *rtp_session) | |||
8104 | { | |||
8105 | switch_size_t has = 0; | |||
8106 | ||||
8107 | if (switch_rtp_ready(rtp_session)) { | |||
8108 | switch_mutex_lock(rtp_session->dtmf_data.dtmf_mutex); | |||
8109 | has = switch_queue_size(rtp_session->dtmf_data.dtmf_inqueue); | |||
8110 | switch_mutex_unlock(rtp_session->dtmf_data.dtmf_mutex); | |||
8111 | } | |||
8112 | ||||
8113 | return has; | |||
8114 | } | |||
8115 | ||||
8116 | SWITCH_DECLARE(switch_size_t)__attribute__((visibility("default"))) switch_size_t switch_rtp_dequeue_dtmf(switch_rtp_t *rtp_session, switch_dtmf_t *dtmf) | |||
8117 | { | |||
8118 | switch_size_t bytes = 0; | |||
8119 | switch_dtmf_t *_dtmf = NULL((void*)0); | |||
8120 | void *pop; | |||
8121 | ||||
8122 | if (!switch_rtp_ready(rtp_session)) { | |||
8123 | return bytes; | |||
8124 | } | |||
8125 | ||||
8126 | switch_mutex_lock(rtp_session->dtmf_data.dtmf_mutex); | |||
8127 | if (switch_queue_trypop(rtp_session->dtmf_data.dtmf_inqueue, &pop) == SWITCH_STATUS_SUCCESS) { | |||
8128 | ||||
8129 | _dtmf = (switch_dtmf_t *)pop; | |||
8130 | *dtmf = *_dtmf; | |||
8131 | /* Only log DTMF buffer if sensitive_dtmf channel variable not set to true */ | |||
8132 | if (!(switch_channel_var_true(switch_core_session_get_channel(rtp_session->session), SWITCH_SENSITIVE_DTMF_VARIABLE"sensitive_dtmf"))) { | |||
8133 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8133, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG,"RTP RECV DTMF %c:%d\n", dtmf->digit, dtmf->duration); | |||
8134 | } | |||
8135 | bytes++; | |||
8136 | free(pop); | |||
8137 | } | |||
8138 | switch_mutex_unlock(rtp_session->dtmf_data.dtmf_mutex); | |||
8139 | ||||
8140 | return bytes; | |||
8141 | } | |||
8142 | ||||
8143 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_queue_rfc2833(switch_rtp_t *rtp_session, const switch_dtmf_t *dtmf) | |||
8144 | { | |||
8145 | ||||
8146 | switch_dtmf_t *rdigit; | |||
8147 | ||||
8148 | if (!switch_rtp_ready(rtp_session)) { | |||
8149 | return SWITCH_STATUS_FALSE; | |||
8150 | } | |||
8151 | ||||
8152 | if ((rdigit = malloc(sizeof(*rdigit))) != 0) { | |||
8153 | *rdigit = *dtmf; | |||
8154 | if (rdigit->duration < switch_core_min_dtmf_duration(0)) { | |||
8155 | rdigit->duration = switch_core_min_dtmf_duration(0); | |||
8156 | } | |||
8157 | ||||
8158 | if ((switch_queue_trypush(rtp_session->dtmf_data.dtmf_queue, rdigit)) != SWITCH_STATUS_SUCCESS) { | |||
8159 | free(rdigit); | |||
8160 | return SWITCH_STATUS_FALSE; | |||
8161 | } | |||
8162 | } else { | |||
8163 | abort(); | |||
8164 | } | |||
8165 | ||||
8166 | return SWITCH_STATUS_SUCCESS; | |||
8167 | } | |||
8168 | ||||
8169 | SWITCH_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) | |||
8170 | { | |||
8171 | switch_dtmf_t *rdigit; | |||
8172 | ||||
8173 | if (!switch_rtp_ready(rtp_session)) { | |||
8174 | return SWITCH_STATUS_FALSE; | |||
8175 | } | |||
8176 | ||||
8177 | if ((rdigit = malloc(sizeof(*rdigit))) != 0) { | |||
8178 | *rdigit = *dtmf; | |||
8179 | if (rdigit->duration < switch_core_min_dtmf_duration(0)) { | |||
8180 | rdigit->duration = switch_core_min_dtmf_duration(0); | |||
8181 | } | |||
8182 | ||||
8183 | if ((switch_queue_trypush(rtp_session->dtmf_data.dtmf_inqueue, rdigit)) != SWITCH_STATUS_SUCCESS) { | |||
8184 | free(rdigit); | |||
8185 | return SWITCH_STATUS_FALSE; | |||
8186 | } | |||
8187 | } else { | |||
8188 | abort(); | |||
8189 | } | |||
8190 | ||||
8191 | return SWITCH_STATUS_SUCCESS; | |||
8192 | } | |||
8193 | ||||
8194 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_read(switch_rtp_t *rtp_session, void *data, uint32_t *datalen, | |||
8195 | switch_payload_t *payload_type, switch_frame_flag_t *flags, switch_io_flag_t io_flags) | |||
8196 | { | |||
8197 | int bytes = 0; | |||
8198 | ||||
8199 | if (!switch_rtp_ready(rtp_session)) { | |||
8200 | return SWITCH_STATUS_FALSE; | |||
8201 | } | |||
8202 | ||||
8203 | bytes = rtp_common_read(rtp_session, payload_type, NULL((void*)0), flags, io_flags); | |||
8204 | ||||
8205 | if (bytes < 0) { | |||
8206 | *datalen = 0; | |||
8207 | return bytes == -2 ? SWITCH_STATUS_TIMEOUT : SWITCH_STATUS_GENERR; | |||
8208 | } else if (bytes == 0) { | |||
8209 | *datalen = 0; | |||
8210 | return SWITCH_STATUS_BREAK; | |||
8211 | } else { | |||
8212 | if (bytes > rtp_header_len12) { | |||
8213 | bytes -= rtp_header_len12; | |||
8214 | } | |||
8215 | } | |||
8216 | ||||
8217 | *datalen = bytes; | |||
8218 | ||||
8219 | memcpy(data, RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body), bytes); | |||
8220 | ||||
8221 | return SWITCH_STATUS_SUCCESS; | |||
8222 | } | |||
8223 | ||||
8224 | SWITCH_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) | |||
8225 | { | |||
8226 | ||||
8227 | if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
8228 | return SWITCH_STATUS_FALSE; | |||
8229 | } | |||
8230 | ||||
8231 | /* A fresh frame has been found! */ | |||
8232 | if (rtp_session->rtcp_fresh_frame) { | |||
8233 | /* turn the flag off! */ | |||
8234 | rtp_session->rtcp_fresh_frame = 0; | |||
8235 | ||||
8236 | *frame = rtp_session->rtcp_frame; | |||
8237 | ||||
8238 | return SWITCH_STATUS_SUCCESS; | |||
8239 | } | |||
8240 | ||||
8241 | return SWITCH_STATUS_TIMEOUT; | |||
8242 | } | |||
8243 | ||||
8244 | SWITCH_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) | |||
8245 | { | |||
8246 | int bytes = 0; | |||
8247 | ||||
8248 | if (!switch_rtp_ready(rtp_session)) { | |||
8249 | return SWITCH_STATUS_FALSE; | |||
8250 | } | |||
8251 | ||||
8252 | bytes = rtp_common_read(rtp_session, &frame->payload, &frame->pmap, &frame->flags, io_flags); | |||
8253 | ||||
8254 | frame->data = RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body); | |||
8255 | ||||
8256 | if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && (bytes < rtp_header_len12 || switch_test_flag(frame, SFF_CNG)((frame)->flags & SFF_CNG))) { | |||
8257 | frame->packet = NULL((void*)0); | |||
8258 | frame->timestamp = 0; | |||
8259 | frame->seq = 0; | |||
8260 | frame->ssrc = 0; | |||
8261 | frame->m = 0; | |||
8262 | } else { | |||
8263 | ||||
8264 | frame->packet = &rtp_session->recv_msg; | |||
8265 | frame->packetlen = bytes; | |||
8266 | frame->source = __FILE__"src/switch_rtp.c"; | |||
8267 | ||||
8268 | switch_set_flag(frame, SFF_RAW_RTP)(frame)->flags |= (SFF_RAW_RTP); | |||
8269 | switch_set_flag(frame, SFF_EXTERNAL)(frame)->flags |= (SFF_EXTERNAL); | |||
8270 | if (frame->payload == rtp_session->recv_te) { | |||
8271 | switch_set_flag(frame, SFF_RFC2833)(frame)->flags |= (SFF_RFC2833); | |||
8272 | } | |||
8273 | frame->timestamp = ntohl(rtp_session->last_rtp_hdr.ts)__bswap_32 (rtp_session->last_rtp_hdr.ts); | |||
8274 | frame->seq = (uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq)__bswap_16 ((uint16_t) rtp_session->last_rtp_hdr.seq); | |||
8275 | frame->ssrc = ntohl(rtp_session->last_rtp_hdr.ssrc)__bswap_32 (rtp_session->last_rtp_hdr.ssrc); | |||
8276 | frame->m = rtp_session->last_rtp_hdr.m ? SWITCH_TRUE : SWITCH_FALSE; | |||
8277 | } | |||
8278 | ||||
8279 | #ifdef ENABLE_ZRTP | |||
8280 | if (zrtp_on && rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV]) { | |||
8281 | zrtp_session_info_t zrtp_session_info; | |||
8282 | ||||
8283 | if (rtp_session->zrtp_session && (zrtp_status_ok == zrtp_session_get(rtp_session->zrtp_session, &zrtp_session_info))) { | |||
8284 | if (zrtp_session_info.sas_is_ready) { | |||
8285 | ||||
8286 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
8287 | ||||
8288 | const char *uuid = switch_channel_get_partner_uuid(channel); | |||
8289 | if (uuid) { | |||
8290 | switch_core_session_t *other_session; | |||
8291 | ||||
8292 | if ((other_session = switch_core_session_locate(uuid)switch_core_session_perform_locate(uuid, "src/switch_rtp.c", ( const char *)__func__, 8292))) { | |||
8293 | switch_channel_t *other_channel = switch_core_session_get_channel(other_session); | |||
8294 | switch_rtp_t *other_rtp_session = switch_channel_get_private(other_channel, "__zrtp_audio_rtp_session"); | |||
8295 | ||||
8296 | if (other_rtp_session) { | |||
8297 | if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND) { | |||
8298 | switch_mutex_lock(other_rtp_session->read_mutex); | |||
8299 | if (zrtp_status_ok == zrtp_session_get(other_rtp_session->zrtp_session, &zrtp_session_info)) { | |||
8300 | if (rtp_session->zrtp_mitm_tries > ZRTP_MITM_TRIES) { | |||
8301 | switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV); | |||
8302 | switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND); | |||
8303 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0; | |||
8304 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0; | |||
8305 | } else if (zrtp_status_ok == zrtp_resolve_mitm_call(other_rtp_session->zrtp_stream, rtp_session->zrtp_stream)) { | |||
8306 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0; | |||
8307 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0; | |||
8308 | switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV); | |||
8309 | switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND); | |||
8310 | rtp_session->zrtp_mitm_tries++; | |||
8311 | } | |||
8312 | } | |||
8313 | switch_mutex_unlock(other_rtp_session->read_mutex); | |||
8314 | } | |||
8315 | } | |||
8316 | ||||
8317 | switch_core_session_rwunlock(other_session); | |||
8318 | } | |||
8319 | } | |||
8320 | } | |||
8321 | } else { | |||
8322 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0; | |||
8323 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0; | |||
8324 | } | |||
8325 | } | |||
8326 | #endif | |||
8327 | ||||
8328 | if (bytes < 0) { | |||
8329 | frame->datalen = 0; | |||
8330 | return bytes == -2 ? SWITCH_STATUS_TIMEOUT : SWITCH_STATUS_GENERR; | |||
8331 | } else if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
8332 | if (bytes < rtp_header_len12) { | |||
8333 | frame->datalen = 0; | |||
8334 | return SWITCH_STATUS_BREAK; | |||
8335 | } else { | |||
8336 | bytes -= rtp_header_len12; | |||
8337 | } | |||
8338 | } | |||
8339 | ||||
8340 | frame->datalen = bytes; | |||
8341 | return SWITCH_STATUS_SUCCESS; | |||
8342 | } | |||
8343 | ||||
8344 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_zerocopy_read(switch_rtp_t *rtp_session, | |||
8345 | void **data, uint32_t *datalen, switch_payload_t *payload_type, switch_frame_flag_t *flags, | |||
8346 | switch_io_flag_t io_flags) | |||
8347 | { | |||
8348 | int bytes = 0; | |||
8349 | ||||
8350 | if (!switch_rtp_ready(rtp_session)) { | |||
8351 | return SWITCH_STATUS_FALSE; | |||
8352 | } | |||
8353 | ||||
8354 | bytes = rtp_common_read(rtp_session, payload_type, NULL((void*)0), flags, io_flags); | |||
8355 | *data = RTP_BODY(rtp_session)(char *) (rtp_session->recv_msg.ebody ? rtp_session->recv_msg .ebody : rtp_session->recv_msg.body); | |||
8356 | ||||
8357 | if (bytes < 0) { | |||
8358 | *datalen = 0; | |||
8359 | return SWITCH_STATUS_GENERR; | |||
8360 | } else { | |||
8361 | if (bytes > rtp_header_len12) { | |||
8362 | bytes -= rtp_header_len12; | |||
8363 | } | |||
8364 | } | |||
8365 | ||||
8366 | *datalen = bytes; | |||
8367 | return SWITCH_STATUS_SUCCESS; | |||
8368 | } | |||
8369 | ||||
8370 | static int rtp_write_ready(switch_rtp_t *rtp_session, uint32_t bytes, int line) | |||
8371 | { | |||
8372 | if (!rtp_session) return 0; | |||
8373 | ||||
8374 | if (rtp_session->ice.ice_user && !(rtp_session->ice.rready || rtp_session->ice.ready)) { | |||
8375 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8375, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, "Skip sending %s packet %ld bytes (ice not ready @ line %d!)\n", | |||
8376 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), (long)bytes, line); | |||
8377 | return 0; | |||
8378 | } | |||
8379 | ||||
8380 | if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) { | |||
8381 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8381, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG3, "Skip sending %s packet %ld bytes (dtls not ready @ line %d!)\n", | |||
8382 | rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), (long)bytes, line); | |||
8383 | return 0; | |||
8384 | } | |||
8385 | ||||
8386 | return 1; | |||
8387 | } | |||
8388 | ||||
8389 | ||||
8390 | static int rtp_common_write(switch_rtp_t *rtp_session, | |||
8391 | rtp_msg_t *send_msg, void *data, uint32_t datalen, switch_payload_t payload, uint32_t timestamp, switch_frame_flag_t *flags) | |||
8392 | { | |||
8393 | switch_size_t bytes; | |||
8394 | uint8_t send = 1; | |||
8395 | uint32_t this_ts = 0; | |||
8396 | int ret; | |||
8397 | switch_time_t now; | |||
8398 | uint8_t m = 0; | |||
8399 | ||||
8400 | if (!switch_rtp_ready(rtp_session)) { | |||
| ||||
8401 | return -1; | |||
8402 | } | |||
8403 | ||||
8404 | if (!rtp_write_ready(rtp_session, datalen, __LINE__8404)) { | |||
8405 | return 0; | |||
8406 | } | |||
8407 | ||||
8408 | WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session-> writing++; | |||
8409 | ||||
8410 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { | |||
8411 | //switch_core_timer_sync(&rtp_session->write_timer); | |||
8412 | } | |||
8413 | ||||
8414 | if (send_msg) { | |||
8415 | bytes = datalen; | |||
8416 | ||||
8417 | m = (uint8_t) send_msg->header.m; | |||
8418 | rtp_session->ts = ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts); | |||
8419 | ||||
8420 | if (flags && *flags & SFF_RFC2833) { | |||
8421 | if (rtp_session->te == INVALID_PT) { | |||
8422 | ret = 0; | |||
8423 | goto end; | |||
8424 | } | |||
8425 | send_msg->header.pt = rtp_session->te; | |||
8426 | } | |||
8427 | data = send_msg->body; | |||
8428 | if (datalen > rtp_header_len12) { | |||
8429 | datalen -= rtp_header_len12; | |||
8430 | } | |||
8431 | } else { | |||
8432 | if (*flags & SFF_RFC2833) { | |||
8433 | if (rtp_session->te == INVALID_PT) { | |||
8434 | ret = 0; | |||
8435 | goto end; | |||
8436 | } | |||
8437 | payload = rtp_session->te; | |||
8438 | } | |||
8439 | ||||
8440 | send_msg = &rtp_session->send_msg; | |||
8441 | send_msg->header.pt = payload; | |||
8442 | ||||
8443 | m = get_next_write_ts(rtp_session, timestamp); | |||
8444 | ||||
8445 | rtp_session->send_msg.header.ts = htonl(rtp_session->ts)__bswap_32 (rtp_session->ts); | |||
8446 | ||||
8447 | memcpy(send_msg->body, data, datalen); | |||
8448 | bytes = datalen + rtp_header_len12; | |||
8449 | } | |||
8450 | ||||
8451 | if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) { | |||
8452 | ||||
8453 | if ((rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) { | |||
8454 | m = 0; | |||
8455 | } else { | |||
8456 | int delta = rtp_session->ts - rtp_session->last_write_ts; | |||
8457 | ||||
8458 | if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && | |||
8459 | ((!rtp_session->flags[SWITCH_RTP_FLAG_RESET] && (abs(delta) > rtp_session->samples_per_interval * 10)) | |||
8460 | || rtp_session->ts == rtp_session->samples_per_interval)) { | |||
8461 | m++; | |||
8462 | } | |||
8463 | ||||
8464 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { | |||
8465 | //switch_core_timer_sync(&rtp_session->write_timer); | |||
8466 | } | |||
8467 | ||||
8468 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && | |||
8469 | (rtp_session->write_timer.samplecount - rtp_session->last_write_samplecount) > rtp_session->samples_per_interval * 10) { | |||
8470 | m++; | |||
8471 | } | |||
8472 | ||||
8473 | if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && | |||
8474 | ((unsigned) ((switch_micro_time_now() - rtp_session->last_write_timestamp))) > (rtp_session->ms_per_packet * 10)) { | |||
8475 | m++; | |||
8476 | } | |||
8477 | ||||
8478 | if (rtp_session->cn && payload != rtp_session->cng_pt) { | |||
8479 | rtp_session->cn = 0; | |||
8480 | m++; | |||
8481 | } | |||
8482 | ||||
8483 | if (rtp_session->need_mark && !rtp_session->sending_dtmf) { | |||
8484 | m++; | |||
8485 | rtp_session->need_mark = 0; | |||
8486 | } | |||
8487 | } | |||
8488 | ||||
8489 | if (m) { | |||
8490 | rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1; | |||
8491 | rtp_session->ts = 0; | |||
8492 | } | |||
8493 | ||||
8494 | /* If the marker was set, and the timestamp seems to have started over - set a new SSRC, to indicate this is a new stream */ | |||
8495 | if (m && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND) && (rtp_session->rtp_bugs & RTP_BUG_CHANGE_SSRC_ON_MARKER) && | |||
8496 | (rtp_session->flags[SWITCH_RTP_FLAG_RESET] || (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->last_write_ts > 0))) { | |||
8497 | switch_rtp_set_ssrc(rtp_session, (uint32_t) ((intptr_t) rtp_session + (uint32_t) switch_epoch_time_now(NULL((void*)0)))); | |||
8498 | } | |||
8499 | ||||
8500 | if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO) && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_UDPTL)) { | |||
8501 | send_msg->header.m = (m && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) ? 1 : 0; | |||
8502 | } | |||
8503 | } | |||
8504 | ||||
8505 | if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) { | |||
8506 | int external = (*flags & SFF_EXTERNAL); | |||
| ||||
8507 | /* Normalize the timestamps to our own base by generating a made up starting point then adding the measured deltas to that base | |||
8508 | 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* | |||
8509 | */ | |||
8510 | ||||
8511 | if (!rtp_session->ts_norm.ts) { | |||
8512 | rtp_session->ts_norm.ts = (uint32_t) rand() % 1000000 + 1; | |||
8513 | } | |||
8514 | ||||
8515 | if (!rtp_session->ts_norm.last_ssrc || send_msg->header.ssrc != rtp_session->ts_norm.last_ssrc || rtp_session->ts_norm.last_external != external) { | |||
8516 | switch_core_session_t *other_session; | |||
8517 | ||||
8518 | switch_core_session_request_video_refresh(rtp_session->session)_switch_core_session_request_video_refresh(rtp_session->session , 0, "src/switch_rtp.c", (const char *)__func__, 8518); | |||
8519 | switch_core_media_gen_key_frame(rtp_session->session)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c" , (const char *)__func__, 8519, (const char*)(rtp_session-> session), SWITCH_LOG_DEBUG1, "%s Send KeyFrame\n", switch_channel_get_name (switch_core_session_get_channel(rtp_session->session))); switch_core_media_codec_control (rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_IO_WRITE , SCC_VIDEO_GEN_KEYFRAME, SCCT_NONE, ((void*)0), SCCT_NONE, ( (void*)0), ((void*)0), ((void*)0)); | |||
8520 | ||||
8521 | if (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__ , 8521) == SWITCH_STATUS_SUCCESS) { | |||
8522 | switch_core_session_request_video_refresh(other_session)_switch_core_session_request_video_refresh(other_session, 0, "src/switch_rtp.c" , (const char *)__func__, 8522); | |||
8523 | switch_core_media_gen_key_frame(other_session)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c" , (const char *)__func__, 8523, (const char*)(other_session), SWITCH_LOG_DEBUG1, "%s Send KeyFrame\n", switch_channel_get_name (switch_core_session_get_channel(other_session))); switch_core_media_codec_control (other_session, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_IO_WRITE, SCC_VIDEO_GEN_KEYFRAME , SCCT_NONE, ((void*)0), SCCT_NONE, ((void*)0), ((void*)0), ( (void*)0)); | |||
8524 | switch_core_session_rwunlock(other_session); | |||
8525 | } | |||
8526 | ||||
8527 | if (rtp_session->ts_norm.last_ssrc) { | |||
8528 | rtp_session->ts_norm.delta_ttl = 0; | |||
8529 | rtp_session->ts_norm.ts++; | |||
8530 | } | |||
8531 | ||||
8532 | rtp_session->ts_norm.last_ssrc = send_msg->header.ssrc; | |||
8533 | rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts); | |||
8534 | } | |||
8535 | ||||
8536 | rtp_session->ts_norm.last_external = external; | |||
8537 | ||||
8538 | if (ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts) != rtp_session->ts_norm.last_frame) { | |||
8539 | int32_t delta = ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts) - rtp_session->ts_norm.last_frame; | |||
8540 | ||||
8541 | if (delta < 0 || delta > 90000) { | |||
8542 | switch_core_media_gen_key_frame(rtp_session->session)switch_log_printf(SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c" , (const char *)__func__, 8542, (const char*)(rtp_session-> session), SWITCH_LOG_DEBUG1, "%s Send KeyFrame\n", switch_channel_get_name (switch_core_session_get_channel(rtp_session->session))); switch_core_media_codec_control (rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_IO_WRITE , SCC_VIDEO_GEN_KEYFRAME, SCCT_NONE, ((void*)0), SCCT_NONE, ( (void*)0), ((void*)0), ((void*)0)); | |||
8543 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8543, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG1, | |||
8544 | "Timestamp shift detected last: %d this: %d delta: %d stick with prev delta: %d\n", | |||
8545 | rtp_session->ts_norm.last_frame, ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts), delta, rtp_session->ts_norm.delta); | |||
8546 | } else { | |||
8547 | rtp_session->ts_norm.delta = delta; | |||
8548 | } | |||
8549 | ||||
8550 | rtp_session->ts_norm.ts += rtp_session->ts_norm.delta; | |||
8551 | ||||
8552 | } | |||
8553 | ||||
8554 | rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts); | |||
8555 | send_msg->header.ts = htonl(rtp_session->ts_norm.ts)__bswap_32 (rtp_session->ts_norm.ts); | |||
8556 | this_ts = rtp_session->ts_norm.ts; | |||
8557 | } | |||
8558 | ||||
8559 | send_msg->header.ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
8560 | ||||
8561 | if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->send_msg.header.pt == 97) { | |||
8562 | rtp_session->last_rtp_hdr.pt = 102; | |||
8563 | } | |||
8564 | ||||
8565 | if (rtp_session->flags[SWITCH_RTP_FLAG_VAD] && | |||
8566 | rtp_session->last_rtp_hdr.pt == rtp_session->vad_data.read_codec->implementation->ianacode) { | |||
8567 | ||||
8568 | int16_t decoded[SWITCH_RECOMMENDED_BUFFER_SIZE8192 / sizeof(int16_t)] = { 0 }; | |||
8569 | uint32_t rate = 0; | |||
8570 | uint32_t codec_flags = 0; | |||
8571 | uint32_t len = sizeof(decoded); | |||
8572 | time_t now = switch_epoch_time_now(NULL((void*)0)); | |||
8573 | send = 0; | |||
8574 | ||||
8575 | if (rtp_session->vad_data.scan_freq && rtp_session->vad_data.next_scan <= now) { | |||
8576 | rtp_session->vad_data.bg_count = rtp_session->vad_data.bg_level = 0; | |||
8577 | rtp_session->vad_data.next_scan = now + rtp_session->vad_data.scan_freq; | |||
8578 | } | |||
8579 | ||||
8580 | if (switch_core_codec_decode(&rtp_session->vad_data.vad_codec, | |||
8581 | rtp_session->vad_data.read_codec, | |||
8582 | data, | |||
8583 | datalen, | |||
8584 | rtp_session->vad_data.read_codec->implementation->actual_samples_per_second, | |||
8585 | decoded, &len, &rate, &codec_flags) == SWITCH_STATUS_SUCCESS) { | |||
8586 | ||||
8587 | uint32_t energy = 0; | |||
8588 | uint32_t x, y = 0, z = len / sizeof(int16_t); | |||
8589 | uint32_t score = 0; | |||
8590 | int divisor = 0; | |||
8591 | if (z) { | |||
8592 | ||||
8593 | if (!(divisor = rtp_session->vad_data.read_codec->implementation->actual_samples_per_second / 8000)) { | |||
8594 | divisor = 1; | |||
8595 | } | |||
8596 | ||||
8597 | for (x = 0; x < z; x++) { | |||
8598 | energy += abs(decoded[y]); | |||
8599 | y += rtp_session->vad_data.read_codec->implementation->number_of_channels; | |||
8600 | } | |||
8601 | ||||
8602 | if (++rtp_session->vad_data.start_count < rtp_session->vad_data.start) { | |||
8603 | send = 1; | |||
8604 | } else { | |||
8605 | score = (energy / (z / divisor)); | |||
8606 | if (score && (rtp_session->vad_data.bg_count < rtp_session->vad_data.bg_len)) { | |||
8607 | rtp_session->vad_data.bg_level += score; | |||
8608 | if (++rtp_session->vad_data.bg_count == rtp_session->vad_data.bg_len) { | |||
8609 | rtp_session->vad_data.bg_level /= rtp_session->vad_data.bg_len; | |||
8610 | } | |||
8611 | send = 1; | |||
8612 | } else { | |||
8613 | 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 )) { | |||
8614 | uint32_t diff = score - rtp_session->vad_data.bg_level; | |||
8615 | ||||
8616 | if (rtp_session->vad_data.hangover_hits) { | |||
8617 | rtp_session->vad_data.hangover_hits--; | |||
8618 | } | |||
8619 | ||||
8620 | if (diff >= rtp_session->vad_data.diff_level || ++rtp_session->vad_data.hangunder_hits >= rtp_session->vad_data.hangunder) { | |||
8621 | ||||
8622 | switch_set_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)(&rtp_session->vad_data)->flags |= (SWITCH_VAD_FLAG_TALKING ); | |||
8623 | ||||
8624 | rtp_session->vad_data.start_talking = switch_micro_time_now(); | |||
8625 | ||||
8626 | if (!(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) { | |||
8627 | send_msg->header.m = 1; | |||
8628 | } | |||
8629 | rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0; | |||
8630 | if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_EVENTS_TALK)((&rtp_session->vad_data)->flags & SWITCH_VAD_FLAG_EVENTS_TALK )) { | |||
8631 | ||||
8632 | if ((rtp_session->vad_data.fire_events & VAD_FIRE_TALK)) { | |||
8633 | switch_event_t *event; | |||
8634 | if (switch_event_create(&event, SWITCH_EVENT_TALK)switch_event_create_subclass_detailed("src/switch_rtp.c", (const char * )(const char *)__func__, 8634, &event, SWITCH_EVENT_TALK , ((void*)0)) == SWITCH_STATUS_SUCCESS) { | |||
8635 | switch_channel_event_set_data(switch_core_session_get_channel(rtp_session->vad_data.session), event); | |||
8636 | switch_event_fire(&event)switch_event_fire_detailed("src/switch_rtp.c", (const char * ) (const char *)__func__, 8636, &event, ((void*)0)); | |||
8637 | } | |||
8638 | } | |||
8639 | } | |||
8640 | } | |||
8641 | } else { | |||
8642 | if (rtp_session->vad_data.hangunder_hits) { | |||
8643 | rtp_session->vad_data.hangunder_hits--; | |||
8644 | } | |||
8645 | if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)((&rtp_session->vad_data)->flags & SWITCH_VAD_FLAG_TALKING )) { | |||
8646 | if (++rtp_session->vad_data.hangover_hits >= rtp_session->vad_data.hangover) { | |||
8647 | rtp_session->vad_data.stop_talking = switch_micro_time_now(); | |||
8648 | rtp_session->vad_data.total_talk_time += (rtp_session->vad_data.stop_talking - rtp_session->vad_data.start_talking); | |||
8649 | ||||
8650 | switch_clear_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)(&rtp_session->vad_data)->flags &= ~(SWITCH_VAD_FLAG_TALKING ); | |||
8651 | ||||
8652 | rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0; | |||
8653 | if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_EVENTS_NOTALK)((&rtp_session->vad_data)->flags & SWITCH_VAD_FLAG_EVENTS_NOTALK )) { | |||
8654 | ||||
8655 | if ((rtp_session->vad_data.fire_events & VAD_FIRE_NOT_TALK)) { | |||
8656 | switch_event_t *event; | |||
8657 | if (switch_event_create(&event, SWITCH_EVENT_NOTALK)switch_event_create_subclass_detailed("src/switch_rtp.c", (const char * )(const char *)__func__, 8657, &event, SWITCH_EVENT_NOTALK , ((void*)0)) == SWITCH_STATUS_SUCCESS) { | |||
8658 | switch_channel_event_set_data(switch_core_session_get_channel(rtp_session->vad_data.session), event); | |||
8659 | switch_event_fire(&event)switch_event_fire_detailed("src/switch_rtp.c", (const char * ) (const char *)__func__, 8659, &event, ((void*)0)); | |||
8660 | } | |||
8661 | } | |||
8662 | } | |||
8663 | } | |||
8664 | } | |||
8665 | } | |||
8666 | } | |||
8667 | } | |||
8668 | ||||
8669 | if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)((&rtp_session->vad_data)->flags & SWITCH_VAD_FLAG_TALKING )) { | |||
8670 | send = 1; | |||
8671 | } | |||
8672 | } | |||
8673 | } else { | |||
8674 | ret = -1; | |||
8675 | goto end; | |||
8676 | } | |||
8677 | } | |||
8678 | ||||
8679 | if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) { | |||
8680 | uint32_t ts_delta; | |||
8681 | ||||
8682 | this_ts = ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts); | |||
8683 | ||||
8684 | ts_delta = abs((int32_t)(this_ts - rtp_session->last_write_ts)); | |||
8685 | ||||
8686 | if (ts_delta > rtp_session->samples_per_second * 2) { | |||
8687 | rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1; | |||
8688 | } | |||
8689 | #ifdef DEBUG_TS_ROLLOVER | |||
8690 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 8690, ((void*)0), SWITCH_LOG_ERROR, "WRITE TS LAST:%u THIS:%u DELTA:%u\n", rtp_session->last_write_ts, this_ts, ts_delta); | |||
8691 | #endif | |||
8692 | if ((!(flags && *flags & SFF_RFC2833) && ts_delta == 0) || !switch_rtp_ready(rtp_session) || rtp_session->sending_dtmf) { | |||
8693 | send = 0; | |||
8694 | } | |||
8695 | } | |||
8696 | ||||
8697 | if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) { | |||
8698 | send = 0; | |||
8699 | } | |||
8700 | ||||
8701 | if (send) { | |||
8702 | int delta = 1; | |||
8703 | ||||
8704 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (*flags & SFF_EXTERNAL) && | |||
8705 | rtp_session->stats.outbound.packet_count && rtp_session->flags[SWITCH_RTP_FLAG_PASSTHRU]) { | |||
8706 | int32_t x = rtp_session->last_write_seq; | |||
8707 | int32_t y = ntohs(send_msg->header.seq)__bswap_16 (send_msg->header.seq); | |||
8708 | ||||
8709 | if (!rtp_session->video_delta_mode) { | |||
8710 | rtp_session->video_delta_mode = 1; | |||
8711 | } else { | |||
8712 | if (x > UINT16_MAX(65535) / 2 && y < UINT16_MAX(65535) / 2) { | |||
8713 | x -= (int32_t)UINT16_MAX(65535)+1; | |||
8714 | } | |||
8715 | ||||
8716 | delta = y-x; | |||
8717 | } | |||
8718 | ||||
8719 | rtp_session->last_write_seq = y; | |||
8720 | } | |||
8721 | ||||
8722 | if (!rtp_session->flags[SWITCH_RTP_FLAG_PASSTHRU]) { | |||
8723 | rtp_session->video_delta_mode = 0; | |||
8724 | } | |||
8725 | ||||
8726 | rtp_session->seq += delta; | |||
8727 | ||||
8728 | send_msg->header.seq = htons(rtp_session->seq)__bswap_16 (rtp_session->seq); | |||
8729 | ||||
8730 | if (rtp_session->flags[SWITCH_RTP_FLAG_BYTESWAP] && send_msg->header.pt == rtp_session->payload) { | |||
8731 | switch_swap_linear((int16_t *)send_msg->body, (int) datalen); | |||
8732 | } | |||
8733 | ||||
8734 | #ifdef ENABLE_SRTP1 | |||
8735 | switch_mutex_lock(rtp_session->ice_mutex); | |||
8736 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) { | |||
8737 | int sbytes = (int) bytes; | |||
8738 | srtp_err_status_t stat; | |||
8739 | ||||
8740 | ||||
8741 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET] || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) { | |||
8742 | ||||
8743 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND_RESET); | |||
8744 | srtp_dealloc(rtp_session->send_ctx[rtp_session->srtp_idx_rtp]); | |||
8745 | rtp_session->send_ctx[rtp_session->srtp_idx_rtp] = NULL((void*)0); | |||
8746 | if ((stat = srtp_create(&rtp_session->send_ctx[rtp_session->srtp_idx_rtp], | |||
8747 | &rtp_session->send_policy[rtp_session->srtp_idx_rtp])) || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) { | |||
8748 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8748, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, | |||
8749 | "Error! RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
8750 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0; | |||
8751 | ret = -1; | |||
8752 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
8753 | goto end; | |||
8754 | } else { | |||
8755 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8755, (const char*)(rtp_session->session), SWITCH_LOG_INFO, | |||
8756 | "RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")); | |||
8757 | } | |||
8758 | } | |||
8759 | ||||
8760 | if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) { | |||
8761 | stat = srtp_protect(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &send_msg->header, &sbytes); | |||
8762 | } else { | |||
8763 | stat = srtp_protect_mki(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &send_msg->header, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX0); | |||
8764 | } | |||
8765 | ||||
8766 | if (stat) { | |||
8767 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8767, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, | |||
8768 | "Error: %s SRTP protection failed with code %d\n", rtp_type(rtp_session)rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session ->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"), stat); | |||
8769 | } | |||
8770 | ||||
8771 | bytes = sbytes; | |||
8772 | } | |||
8773 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
8774 | #endif | |||
8775 | #ifdef ENABLE_ZRTP | |||
8776 | /* ZRTP Send */ | |||
8777 | if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) { | |||
8778 | unsigned int sbytes = (int) bytes; | |||
8779 | zrtp_status_t stat = zrtp_status_fail; | |||
8780 | ||||
8781 | ||||
8782 | stat = zrtp_process_rtp(rtp_session->zrtp_stream, (void *) send_msg, &sbytes); | |||
8783 | ||||
8784 | switch (stat) { | |||
8785 | case zrtp_status_ok: | |||
8786 | break; | |||
8787 | case zrtp_status_drop: | |||
8788 | /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */ | |||
8789 | ret = (int) bytes; | |||
8790 | goto end; | |||
8791 | break; | |||
8792 | case zrtp_status_fail: | |||
8793 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8793, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat); | |||
8794 | break; | |||
8795 | default: | |||
8796 | break; | |||
8797 | } | |||
8798 | ||||
8799 | bytes = sbytes; | |||
8800 | } | |||
8801 | #endif | |||
8802 | ||||
8803 | now = switch_micro_time_now(); | |||
8804 | #ifdef RTP_DEBUG_WRITE_DELTA | |||
8805 | { | |||
8806 | int delta = (int) (now - rtp_session->send_time) / 1000; | |||
8807 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 8807, ((void*)0), SWITCH_LOG_ERROR, "WRITE %d delta %d\n", (int) bytes, delta); | |||
8808 | } | |||
8809 | #endif | |||
8810 | rtp_session->send_time = now; | |||
8811 | ||||
8812 | if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) { | |||
8813 | const char *tx_host; | |||
8814 | const char *old_host; | |||
8815 | const char *my_host; | |||
8816 | ||||
8817 | char bufa[50], bufb[50], bufc[50]; | |||
8818 | ||||
8819 | ||||
8820 | tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr); | |||
8821 | old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr); | |||
8822 | my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr); | |||
8823 | ||||
8824 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session)SWITCH_CHANNEL_ID_LOG_CLEAN, "src/switch_rtp.c", (const char * )__func__, 8824, switch_core_session_get_uuid((rtp_session-> session)), SWITCH_LOG_CONSOLE, | |||
8825 | "W %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n", | |||
8826 | rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName", | |||
8827 | (long) bytes, | |||
8828 | my_host, switch_sockaddr_get_port(rtp_session->local_addr), | |||
8829 | old_host, rtp_session->remote_port, | |||
8830 | tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr), | |||
8831 | send_msg->header.pt, ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts), ntohs(send_msg->header.seq)__bswap_16 (send_msg->header.seq), send_msg->header.m); | |||
8832 | ||||
8833 | } | |||
8834 | ||||
8835 | if (rtp_session->flags[SWITCH_RTP_FLAG_NACK]) { | |||
8836 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
8837 | ||||
8838 | if (!rtp_session->vbw) { | |||
8839 | int nack_size = 100; | |||
8840 | const char *var; | |||
8841 | ||||
8842 | if ((var = switch_channel_get_variable(channel, "rtp_nack_buffer_size")switch_channel_get_variable_dup(channel, "rtp_nack_buffer_size" , SWITCH_TRUE, -1))) { | |||
8843 | int tmp = atoi(var); | |||
8844 | ||||
8845 | if (tmp > 0 && tmp < 500) { | |||
8846 | nack_size = tmp; | |||
8847 | } | |||
8848 | } | |||
8849 | ||||
8850 | switch_jb_create(&rtp_session->vbw, SJB_VIDEO, nack_size, nack_size, rtp_session->pool); | |||
8851 | ||||
8852 | if (rtp_session->vbw) { | |||
8853 | switch_jb_set_flag(rtp_session->vbw, SJB_QUEUE_ONLY); | |||
8854 | //switch_jb_debug_level(rtp_session->vbw, 10); | |||
8855 | } | |||
8856 | } | |||
8857 | switch_jb_put_packet(rtp_session->vbw, (switch_rtp_packet_t *)send_msg, bytes); | |||
8858 | } | |||
8859 | ||||
8860 | #ifdef RTP_WRITE_PLOSS | |||
8861 | { | |||
8862 | int r = (rand() % 10000) + 1; | |||
8863 | ||||
8864 | if (r <= 200) { | |||
8865 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8865, (const char*)(rtp_session->session), SWITCH_LOG_ALERT, | |||
8866 | "Simulate dropping packet ......... ts: %u seq: %u\n", ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts), ntohs(send_msg->header.seq)__bswap_16 (send_msg->header.seq)); | |||
8867 | } else { | |||
8868 | if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) { | |||
8869 | rtp_session->seq--; | |||
8870 | ret = -1; | |||
8871 | goto end; | |||
8872 | } | |||
8873 | } | |||
8874 | } | |||
8875 | #else | |||
8876 | //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
8877 | // | |||
8878 | // rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]++; | |||
8879 | // | |||
8880 | // //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "SEND %u\n", ntohs(send_msg->header.seq)); | |||
8881 | //} | |||
8882 | if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) { | |||
8883 | rtp_session->seq -= delta; | |||
8884 | ||||
8885 | ret = -1; | |||
8886 | goto end; | |||
8887 | } | |||
8888 | #endif | |||
8889 | rtp_session->last_write_ts = this_ts; | |||
8890 | rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0; | |||
8891 | ||||
8892 | if (rtp_session->queue_delay) { | |||
8893 | rtp_session->delay_samples = rtp_session->queue_delay; | |||
8894 | rtp_session->queue_delay = 0; | |||
8895 | } | |||
8896 | ||||
8897 | rtp_session->stats.outbound.raw_bytes += bytes; | |||
8898 | rtp_session->stats.outbound.packet_count++; | |||
8899 | ||||
8900 | if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { | |||
8901 | rtp_session->stats.rtcp.sent_pkt_count++; | |||
8902 | } | |||
8903 | ||||
8904 | if (send_msg->header.pt == rtp_session->cng_pt) { | |||
8905 | rtp_session->stats.outbound.cng_packet_count++; | |||
8906 | } else { | |||
8907 | rtp_session->stats.outbound.media_packet_count++; | |||
8908 | rtp_session->stats.outbound.media_bytes += bytes; | |||
8909 | } | |||
8910 | ||||
8911 | if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { | |||
8912 | //switch_core_timer_sync(&rtp_session->write_timer); | |||
8913 | rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount; | |||
8914 | } | |||
8915 | ||||
8916 | rtp_session->last_write_timestamp = switch_micro_time_now(); | |||
8917 | } | |||
8918 | ||||
8919 | ret = (int) bytes; | |||
8920 | ||||
8921 | end: | |||
8922 | ||||
8923 | WRITE_DEC(rtp_session)rtp_session->writing--; switch_mutex_unlock(rtp_session-> write_mutex); | |||
8924 | ||||
8925 | return ret; | |||
8926 | } | |||
8927 | ||||
8928 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_disable_vad(switch_rtp_t *rtp_session) | |||
8929 | { | |||
8930 | ||||
8931 | if (!rtp_session) { | |||
8932 | return SWITCH_STATUS_FALSE; | |||
8933 | } | |||
8934 | ||||
8935 | if (!rtp_session->flags[SWITCH_RTP_FLAG_VAD]) { | |||
8936 | return SWITCH_STATUS_GENERR; | |||
8937 | } | |||
8938 | switch_core_codec_destroy(&rtp_session->vad_data.vad_codec); | |||
8939 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_VAD); | |||
8940 | return SWITCH_STATUS_SUCCESS; | |||
8941 | } | |||
8942 | ||||
8943 | SWITCH_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, | |||
8944 | switch_vad_flag_t flags) | |||
8945 | { | |||
8946 | if (!switch_rtp_ready(rtp_session)) { | |||
8947 | return SWITCH_STATUS_FALSE; | |||
8948 | } | |||
8949 | ||||
8950 | if (rtp_session->flags[SWITCH_RTP_FLAG_VAD]) { | |||
8951 | return SWITCH_STATUS_GENERR; | |||
8952 | } | |||
8953 | ||||
8954 | memset(&rtp_session->vad_data, 0, sizeof(rtp_session->vad_data)); | |||
8955 | ||||
8956 | 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))) { | |||
8957 | rtp_session->vad_data.fire_events |= VAD_FIRE_TALK; | |||
8958 | } | |||
8959 | ||||
8960 | 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))) { | |||
8961 | rtp_session->vad_data.fire_events |= VAD_FIRE_NOT_TALK; | |||
8962 | } | |||
8963 | ||||
8964 | ||||
8965 | 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, codec-> implementation->modname, ((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 ) | |||
8966 | codec->implementation->iananame,switch_core_codec_init_with_bitrate(&rtp_session->vad_data .vad_codec, codec->implementation->iananame, codec-> implementation->modname, ((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 ) | |||
8967 | codec->implementation->modname,switch_core_codec_init_with_bitrate(&rtp_session->vad_data .vad_codec, codec->implementation->iananame, codec-> implementation->modname, ((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 ) | |||
8968 | NULL,switch_core_codec_init_with_bitrate(&rtp_session->vad_data .vad_codec, codec->implementation->iananame, codec-> implementation->modname, ((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 ) | |||
8969 | codec->implementation->samples_per_second,switch_core_codec_init_with_bitrate(&rtp_session->vad_data .vad_codec, codec->implementation->iananame, codec-> implementation->modname, ((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 ) | |||
8970 | codec->implementation->microseconds_per_packet / 1000,switch_core_codec_init_with_bitrate(&rtp_session->vad_data .vad_codec, codec->implementation->iananame, codec-> implementation->modname, ((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 ) | |||
8971 | codec->implementation->number_of_channels,switch_core_codec_init_with_bitrate(&rtp_session->vad_data .vad_codec, codec->implementation->iananame, codec-> implementation->modname, ((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 ) | |||
8972 | 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, codec-> implementation->modname, ((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) { | |||
8973 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8973, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Can't load codec?\n"); | |||
8974 | return SWITCH_STATUS_FALSE; | |||
8975 | } | |||
8976 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 8976, (const char*)(rtp_session->session), SWITCH_LOG_DEBUG, "Activate VAD codec %s %dms\n", codec->implementation->iananame, | |||
8977 | codec->implementation->microseconds_per_packet / 1000); | |||
8978 | rtp_session->vad_data.diff_level = 400; | |||
8979 | rtp_session->vad_data.hangunder = 15; | |||
8980 | rtp_session->vad_data.hangover = 40; | |||
8981 | rtp_session->vad_data.bg_len = 5; | |||
8982 | rtp_session->vad_data.bg_count = 5; | |||
8983 | rtp_session->vad_data.bg_level = 300; | |||
8984 | rtp_session->vad_data.read_codec = codec; | |||
8985 | rtp_session->vad_data.session = session; | |||
8986 | rtp_session->vad_data.flags = flags; | |||
8987 | rtp_session->vad_data.cng_freq = 50; | |||
8988 | rtp_session->vad_data.ts = 1; | |||
8989 | rtp_session->vad_data.start = 0; | |||
8990 | rtp_session->vad_data.next_scan = switch_epoch_time_now(NULL((void*)0)); | |||
8991 | rtp_session->vad_data.scan_freq = 0; | |||
8992 | if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)((&rtp_session->vad_data)->flags & SWITCH_VAD_FLAG_TALKING )) { | |||
8993 | rtp_session->vad_data.start_talking = switch_micro_time_now(); | |||
8994 | } | |||
8995 | switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_VAD); | |||
8996 | switch_set_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_CNG)(&rtp_session->vad_data)->flags |= (SWITCH_VAD_FLAG_CNG ); | |||
8997 | return SWITCH_STATUS_SUCCESS; | |||
8998 | } | |||
8999 | ||||
9000 | SWITCH_DECLARE(int)__attribute__((visibility("default"))) int switch_rtp_write_frame(switch_rtp_t *rtp_session, switch_frame_t *frame) | |||
9001 | { | |||
9002 | uint8_t fwd = 0; | |||
9003 | void *data = NULL((void*)0); | |||
9004 | uint32_t len, ts = 0; | |||
9005 | switch_payload_t payload = 0; | |||
9006 | rtp_msg_t *send_msg = NULL((void*)0); | |||
9007 | srtp_hdr_t local_header; | |||
9008 | int r = 0; | |||
9009 | switch_status_t status; | |||
9010 | ||||
9011 | if (!switch_rtp_ready(rtp_session) || !rtp_session->remote_addr) { | |||
9012 | return -1; | |||
9013 | } | |||
9014 | ||||
9015 | if (!rtp_write_ready(rtp_session, frame->datalen, __LINE__9015)) { | |||
9016 | return 0; | |||
9017 | } | |||
9018 | ||||
9019 | //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
9020 | // rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]++; | |||
9021 | // rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]++; | |||
9022 | //} | |||
9023 | ||||
9024 | ||||
9025 | 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) || | |||
9026 | rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
9027 | ||||
9028 | //if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) { | |||
9029 | switch_size_t bytes; | |||
9030 | //char bufa[50]; | |||
9031 | ||||
9032 | /* Fast PASS! */ | |||
9033 | 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)) { | |||
9034 | return 0; | |||
9035 | } | |||
9036 | bytes = frame->packetlen; | |||
9037 | //tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->remote_addr); | |||
9038 | ||||
9039 | send_msg = frame->packet; | |||
9040 | ||||
9041 | if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !switch_test_flag(frame, SFF_UDPTL_PACKET)((frame)->flags & SFF_UDPTL_PACKET)) { | |||
9042 | ||||
9043 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->payload > 0) { | |||
9044 | send_msg->header.pt = rtp_session->payload; | |||
9045 | } | |||
9046 | ||||
9047 | send_msg->header.ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
9048 | send_msg->header.seq = htons(++rtp_session->seq)__bswap_16 (++rtp_session->seq); | |||
9049 | } | |||
9050 | ||||
9051 | if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) { | |||
9052 | const char *tx_host; | |||
9053 | const char *old_host; | |||
9054 | const char *my_host; | |||
9055 | ||||
9056 | char bufa[50], bufb[50], bufc[50]; | |||
9057 | ||||
9058 | ||||
9059 | tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr); | |||
9060 | old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr); | |||
9061 | my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr); | |||
9062 | ||||
9063 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session)SWITCH_CHANNEL_ID_LOG_CLEAN, "src/switch_rtp.c", (const char * )__func__, 9063, switch_core_session_get_uuid((rtp_session-> session)), SWITCH_LOG_CONSOLE, | |||
9064 | "W %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n", | |||
9065 | rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName", | |||
9066 | (long) bytes, | |||
9067 | my_host, switch_sockaddr_get_port(rtp_session->local_addr), | |||
9068 | old_host, rtp_session->remote_port, | |||
9069 | tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr), | |||
9070 | send_msg->header.pt, ntohl(send_msg->header.ts)__bswap_32 (send_msg->header.ts), ntohs(send_msg->header.seq)__bswap_16 (send_msg->header.seq), send_msg->header.m); | |||
9071 | ||||
9072 | } | |||
9073 | ||||
9074 | if ((status = switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, frame->packet, &bytes)) != SWITCH_STATUS_SUCCESS) { | |||
9075 | if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) { | |||
9076 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session->session)SWITCH_CHANNEL_ID_LOG_CLEAN, "src/switch_rtp.c", (const char * )__func__, 9076, switch_core_session_get_uuid((rtp_session-> session)), SWITCH_LOG_ERROR, "bytes: %" SWITCH_SIZE_T_FMT"ld" ", status: %d", bytes, status); | |||
9077 | } | |||
9078 | ||||
9079 | return -1 * status; | |||
9080 | } | |||
9081 | ||||
9082 | ||||
9083 | rtp_session->stats.outbound.raw_bytes += bytes; | |||
9084 | rtp_session->stats.outbound.media_bytes += bytes; | |||
9085 | rtp_session->stats.outbound.media_packet_count++; | |||
9086 | rtp_session->stats.outbound.packet_count++; | |||
9087 | return (int) bytes; | |||
9088 | } | |||
9089 | #ifdef ENABLE_ZRTP | |||
9090 | if (zrtp_on && rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND]) { | |||
9091 | zrtp_session_info_t zrtp_session_info; | |||
9092 | ||||
9093 | if (zrtp_status_ok == zrtp_session_get(rtp_session->zrtp_session, &zrtp_session_info)) { | |||
9094 | if (zrtp_session_info.sas_is_ready) { | |||
9095 | ||||
9096 | switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session); | |||
9097 | ||||
9098 | const char *uuid = switch_channel_get_partner_uuid(channel); | |||
9099 | if (uuid) { | |||
9100 | switch_core_session_t *other_session; | |||
9101 | ||||
9102 | if ((other_session = switch_core_session_locate(uuid)switch_core_session_perform_locate(uuid, "src/switch_rtp.c", ( const char *)__func__, 9102))) { | |||
9103 | switch_channel_t *other_channel = switch_core_session_get_channel(other_session); | |||
9104 | switch_rtp_t *other_rtp_session = switch_channel_get_private(other_channel, "__zrtp_audio_rtp_session"); | |||
9105 | ||||
9106 | ||||
9107 | if (other_rtp_session) { | |||
9108 | if (zrtp_status_ok == zrtp_session_get(other_rtp_session->zrtp_session, &zrtp_session_info)) { | |||
9109 | if (rtp_session->zrtp_mitm_tries > ZRTP_MITM_TRIES) { | |||
9110 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0; | |||
9111 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0; | |||
9112 | switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV); | |||
9113 | switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND); | |||
9114 | } else if (zrtp_status_ok == zrtp_resolve_mitm_call(other_rtp_session->zrtp_stream, rtp_session->zrtp_stream)) { | |||
9115 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_RECV] = 0; | |||
9116 | rtp_session->flags[SWITCH_ZRTP_FLAG_SECURE_MITM_SEND] = 0; | |||
9117 | switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV); | |||
9118 | switch_rtp_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND); | |||
9119 | rtp_session->zrtp_mitm_tries++; | |||
9120 | } | |||
9121 | rtp_session->zrtp_mitm_tries++; | |||
9122 | } | |||
9123 | } | |||
9124 | ||||
9125 | switch_core_session_rwunlock(other_session); | |||
9126 | } | |||
9127 | } | |||
9128 | } | |||
9129 | } | |||
9130 | } | |||
9131 | #endif | |||
9132 | ||||
9133 | fwd = (rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] && | |||
9134 | (switch_test_flag(frame, SFF_RAW_RTP)((frame)->flags & SFF_RAW_RTP) || switch_test_flag(frame, SFF_RAW_RTP_PARSE_FRAME)((frame)->flags & SFF_RAW_RTP_PARSE_FRAME))) ? 1 : 0; | |||
9135 | ||||
9136 | if (!fwd && !rtp_session->sending_dtmf && !rtp_session->queue_delay && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && | |||
9137 | rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] && (rtp_session->rtp_bugs & RTP_BUG_GEN_ONE_GEN_ALL)) { | |||
9138 | ||||
9139 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 9139, (const char*)(rtp_session->session), SWITCH_LOG_WARNING, "Generating RTP locally but timestamp passthru is configured, disabling....\n"); | |||
9140 | rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] = 0; | |||
9141 | rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1; | |||
9142 | } | |||
9143 | ||||
9144 | switch_assert(frame != NULL)((frame != ((void*)0)) ? (void) (0) : __assert_fail ("frame != ((void*)0)" , "src/switch_rtp.c", 9144, __extension__ __PRETTY_FUNCTION__ )); | |||
9145 | ||||
9146 | if (switch_test_flag(frame, SFF_CNG)((frame)->flags & SFF_CNG)) { | |||
9147 | if (rtp_session->cng_pt != INVALID_PT) { | |||
9148 | payload = rtp_session->cng_pt; | |||
9149 | } else { | |||
9150 | return (int) frame->packetlen; | |||
9151 | } | |||
9152 | } else { | |||
9153 | payload = rtp_session->payload; | |||
9154 | #if 0 | |||
9155 | if (rtp_session->pmaps && *rtp_session->pmaps) { | |||
9156 | payload_map_t *pmap; | |||
9157 | for (pmap = *rtp_session->pmaps; pmap; pmap = pmap->next) { | |||
9158 | if (pmap->current) { | |||
9159 | payload = pmap->pt; | |||
9160 | } | |||
9161 | } | |||
9162 | } | |||
9163 | #endif | |||
9164 | } | |||
9165 | ||||
9166 | ||||
9167 | if (switch_test_flag(frame, SFF_RTP_HEADER)((frame)->flags & SFF_RTP_HEADER) || rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) { | |||
9168 | switch_size_t wrote; | |||
9169 | ||||
9170 | wrote = switch_rtp_write_manual(rtp_session, frame->data, frame->datalen, | |||
9171 | frame->m, frame->payload, (uint32_t) (frame->timestamp), &frame->flags); | |||
9172 | ||||
9173 | rtp_session->stats.outbound.raw_bytes += wrote; | |||
9174 | rtp_session->stats.outbound.media_bytes += wrote; | |||
9175 | rtp_session->stats.outbound.media_packet_count++; | |||
9176 | rtp_session->stats.outbound.packet_count++; | |||
9177 | ||||
9178 | return wrote; | |||
9179 | } | |||
9180 | ||||
9181 | if (frame->pmap && rtp_session->pmaps && *rtp_session->pmaps) { | |||
9182 | payload_map_t *pmap; | |||
9183 | ||||
9184 | switch_mutex_lock(rtp_session->flag_mutex); | |||
9185 | for (pmap = *rtp_session->pmaps; pmap; pmap = pmap->next) { | |||
9186 | if (pmap->negotiated && pmap->hash == frame->pmap->hash) { | |||
9187 | payload = pmap->recv_pt; | |||
9188 | break; | |||
9189 | } | |||
9190 | } | |||
9191 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
9192 | } | |||
9193 | ||||
9194 | if (fwd) { | |||
9195 | send_msg = frame->packet; | |||
9196 | local_header = send_msg->header; | |||
9197 | len = frame->packetlen; | |||
9198 | ts = 0; | |||
9199 | ||||
9200 | send_msg->header.pt = payload; | |||
9201 | ||||
9202 | if (switch_test_flag(frame, SFF_RAW_RTP_PARSE_FRAME)((frame)->flags & SFF_RAW_RTP_PARSE_FRAME)) { | |||
9203 | send_msg->header.version = 2; | |||
9204 | send_msg->header.m = frame->m; | |||
9205 | ||||
9206 | send_msg->header.ts = htonl(frame->timestamp)__bswap_32 (frame->timestamp); | |||
9207 | if (frame->ssrc) { | |||
9208 | send_msg->header.ssrc = htonl(frame->ssrc)__bswap_32 (frame->ssrc); | |||
9209 | } else { | |||
9210 | send_msg->header.ssrc = htonl(rtp_session->ssrc)__bswap_32 (rtp_session->ssrc); | |||
9211 | } | |||
9212 | } | |||
9213 | ||||
9214 | } else { | |||
9215 | data = frame->data; | |||
9216 | len = frame->datalen; | |||
9217 | ts = rtp_session->flags[SWITCH_RTP_FLAG_RAW_WRITE] ? (uint32_t) frame->timestamp : 0; | |||
9218 | } | |||
9219 | ||||
9220 | /* | |||
9221 | if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) { | |||
9222 | send_msg->header.pt = rtp_session->payload; | |||
9223 | } | |||
9224 | */ | |||
9225 | ||||
9226 | r = rtp_common_write(rtp_session, send_msg, data, len, payload, ts, &frame->flags); | |||
9227 | ||||
9228 | if (send_msg) { | |||
9229 | send_msg->header = local_header; | |||
9230 | } | |||
9231 | ||||
9232 | return r; | |||
9233 | ||||
9234 | } | |||
9235 | ||||
9236 | SWITCH_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) | |||
9237 | { | |||
9238 | switch_rtp_stats_t *s; | |||
9239 | ||||
9240 | if (!rtp_session) { | |||
9241 | return NULL((void*)0); | |||
9242 | } | |||
9243 | ||||
9244 | switch_mutex_lock(rtp_session->flag_mutex); | |||
9245 | if (pool) { | |||
9246 | s = switch_core_alloc(pool, sizeof(*s))switch_core_perform_alloc(pool, sizeof(*s), "src/switch_rtp.c" , (const char *)__func__, 9246); | |||
9247 | *s = rtp_session->stats; | |||
9248 | } else { | |||
9249 | s = &rtp_session->stats; | |||
9250 | } | |||
9251 | ||||
9252 | if (rtp_session->jb) { | |||
9253 | switch_jb_get_frames(rtp_session->jb, NULL((void*)0), NULL((void*)0), NULL((void*)0), (uint32_t *)&s->inbound.largest_jb_size); | |||
9254 | } | |||
9255 | ||||
9256 | do_mos(rtp_session); | |||
9257 | ||||
9258 | switch_mutex_unlock(rtp_session->flag_mutex); | |||
9259 | ||||
9260 | return s; | |||
9261 | } | |||
9262 | ||||
9263 | SWITCH_DECLARE(int)__attribute__((visibility("default"))) int switch_rtp_write_manual(switch_rtp_t *rtp_session, | |||
9264 | void *data, uint32_t datalen, uint8_t m, switch_payload_t payload, uint32_t ts, switch_frame_flag_t *flags) | |||
9265 | { | |||
9266 | switch_size_t bytes; | |||
9267 | int ret = -1; | |||
9268 | ||||
9269 | if (!switch_rtp_ready(rtp_session) || !rtp_session->remote_addr || datalen > SWITCH_RTP_MAX_BUF_LEN16384) { | |||
9270 | return -1; | |||
9271 | } | |||
9272 | ||||
9273 | if (!rtp_write_ready(rtp_session, datalen, __LINE__9273)) { | |||
9274 | return 0; | |||
9275 | } | |||
9276 | ||||
9277 | if (payload == INVALID_PT) { | |||
9278 | return 0; | |||
9279 | } | |||
9280 | ||||
9281 | WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session-> writing++; | |||
9282 | ||||
9283 | rtp_session->write_msg = rtp_session->send_msg; | |||
9284 | rtp_session->write_msg.header.seq = htons(++rtp_session->seq)__bswap_16 (++rtp_session->seq); | |||
9285 | rtp_session->write_msg.header.ts = htonl(ts)__bswap_32 (ts); | |||
9286 | rtp_session->write_msg.header.pt = payload; | |||
9287 | rtp_session->write_msg.header.m = m; | |||
9288 | memcpy(rtp_session->write_msg.body, data, datalen); | |||
9289 | ||||
9290 | bytes = rtp_header_len12 + datalen; | |||
9291 | ||||
9292 | if (switch_rtp_write_raw(rtp_session, (void *) &rtp_session->write_msg, &bytes, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) { | |||
9293 | rtp_session->seq--; | |||
9294 | ret = -1; | |||
9295 | goto end; | |||
9296 | } | |||
9297 | ||||
9298 | if (((*flags) & SFF_RTP_HEADER)) { | |||
9299 | rtp_session->last_write_ts = ts; | |||
9300 | rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0; | |||
9301 | } | |||
9302 | ||||
9303 | ret = (int) bytes; | |||
9304 | ||||
9305 | end: | |||
9306 | ||||
9307 | WRITE_DEC(rtp_session)rtp_session->writing--; switch_mutex_unlock(rtp_session-> write_mutex); | |||
9308 | ||||
9309 | return ret; | |||
9310 | } | |||
9311 | ||||
9312 | ||||
9313 | ||||
9314 | SWITCH_DECLARE(switch_status_t)__attribute__((visibility("default"))) switch_status_t switch_rtp_write_raw(switch_rtp_t *rtp_session, void *data, switch_size_t *bytes, switch_bool_t process_encryption) | |||
9315 | { | |||
9316 | switch_status_t status = SWITCH_STATUS_FALSE; | |||
9317 | ||||
9318 | switch_assert(bytes)((bytes) ? (void) (0) : __assert_fail ("bytes", "src/switch_rtp.c" , 9318, __extension__ __PRETTY_FUNCTION__)); | |||
9319 | ||||
9320 | if (!switch_rtp_ready(rtp_session) || !rtp_session->remote_addr || *bytes > SWITCH_RTP_MAX_BUF_LEN16384) { | |||
9321 | return status; | |||
9322 | } | |||
9323 | ||||
9324 | if (!rtp_write_ready(rtp_session, *bytes, __LINE__9324)) { | |||
9325 | return SWITCH_STATUS_NOT_INITALIZED; | |||
9326 | } | |||
9327 | ||||
9328 | WRITE_INC(rtp_session)switch_mutex_lock(rtp_session->write_mutex); rtp_session-> writing++; | |||
9329 | ||||
9330 | if (process_encryption) { | |||
9331 | #ifdef ENABLE_SRTP1 | |||
9332 | switch_mutex_lock(rtp_session->ice_mutex); | |||
9333 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) { | |||
9334 | ||||
9335 | int sbytes = (int) *bytes; | |||
9336 | srtp_err_status_t stat; | |||
9337 | ||||
9338 | if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET]) { | |||
9339 | switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND_RESET); | |||
9340 | srtp_dealloc(rtp_session->send_ctx[rtp_session->srtp_idx_rtp]); | |||
9341 | rtp_session->send_ctx[rtp_session->srtp_idx_rtp] = NULL((void*)0); | |||
9342 | if ((stat = srtp_create(&rtp_session->send_ctx[rtp_session->srtp_idx_rtp], | |||
9343 | &rtp_session->send_policy[rtp_session->srtp_idx_rtp])) || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) { | |||
9344 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 9344, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP SEND\n"); | |||
9345 | rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0; | |||
9346 | status = SWITCH_STATUS_FALSE; | |||
9347 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
9348 | goto end; | |||
9349 | } else { | |||
9350 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 9350, (const char*)(rtp_session->session), SWITCH_LOG_INFO, "RE-Activating Secure RTP SEND\n"); | |||
9351 | } | |||
9352 | } | |||
9353 | ||||
9354 | if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) { | |||
9355 | stat = srtp_protect(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &rtp_session->write_msg.header, &sbytes); | |||
9356 | } else { | |||
9357 | stat = srtp_protect_mki(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &rtp_session->write_msg.header, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX0); | |||
9358 | } | |||
9359 | ||||
9360 | if (stat) { | |||
9361 | switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session)SWITCH_CHANNEL_ID_SESSION, "src/switch_rtp.c", (const char *) __func__, 9361, (const char*)(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP protection failed with code %d\n", stat); | |||
9362 | } | |||
9363 | *bytes = sbytes; | |||
9364 | } | |||
9365 | switch_mutex_unlock(rtp_session->ice_mutex); | |||
9366 | #endif | |||
9367 | #ifdef ENABLE_ZRTP | |||
9368 | /* ZRTP Send */ | |||
9369 | if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) { | |||
9370 | unsigned int sbytes = (int) *bytes; | |||
9371 | zrtp_status_t stat = zrtp_status_fail; | |||
9372 | ||||
9373 | stat = zrtp_process_rtp(rtp_session->zrtp_stream, (void *) &rtp_session->write_msg, &sbytes); | |||
9374 | ||||
9375 | switch (stat) { | |||
9376 | case zrtp_status_ok: | |||
9377 | break; | |||
9378 | case zrtp_status_drop: | |||
9379 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 9379, ((void*)0), SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat); | |||
9380 | status = SWITCH_STATUS_SUCCESS; | |||
9381 | goto end; | |||
9382 | break; | |||
9383 | case zrtp_status_fail: | |||
9384 | switch_log_printf(SWITCH_CHANNEL_LOGSWITCH_CHANNEL_ID_LOG, "src/switch_rtp.c", (const char *)__func__ , 9384, ((void*)0), SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat); | |||
9385 | break; | |||
9386 | default: | |||
9387 | break; | |||
9388 | } | |||
9389 | ||||
9390 | *bytes = sbytes; | |||
9391 | } | |||
9392 | #endif | |||
9393 | } | |||
9394 | ||||
9395 | status = switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, data, bytes); | |||
9396 | #if defined(ENABLE_SRTP1) || defined(ENABLE_ZRTP) | |||
9397 | end: | |||
9398 | #endif | |||
9399 | ||||
9400 | WRITE_DEC(rtp_session)rtp_session->writing--; switch_mutex_unlock(rtp_session-> write_mutex); | |||
9401 | ||||
9402 | return status; | |||
9403 | } | |||
9404 | ||||
9405 | SWITCH_DECLARE(uint32_t)__attribute__((visibility("default"))) uint32_t switch_rtp_get_ssrc(switch_rtp_t *rtp_session) | |||
9406 | { | |||
9407 | return rtp_session->ssrc; | |||
9408 | } | |||
9409 | ||||
9410 | SWITCH_DECLARE(void)__attribute__((visibility("default"))) void switch_rtp_set_private(switch_rtp_t *rtp_session, void *private_data) | |||
9411 | { | |||
9412 | rtp_session->private_data = private_data; | |||
9413 | } | |||
9414 | ||||
9415 | SWITCH_DECLARE(void *)__attribute__((visibility("default"))) void * switch_rtp_get_private(switch_rtp_t *rtp_session) | |||
9416 | { | |||
9417 | return rtp_session->private_data; | |||
9418 | } | |||
9419 | ||||
9420 | SWITCH_DECLARE(switch_core_session_t*)__attribute__((visibility("default"))) switch_core_session_t* switch_rtp_get_core_session(switch_rtp_t *rtp_session) | |||
9421 | { | |||
9422 | return rtp_session->session; | |||
9423 | } | |||
9424 | ||||
9425 | /* For Emacs: | |||
9426 | * Local Variables: | |||
9427 | * mode:c | |||
9428 | * indent-tabs-mode:t | |||
9429 | * tab-width:4 | |||
9430 | * c-basic-offset:4 | |||
9431 | * End: | |||
9432 | * For VIM: | |||
9433 | * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: | |||
9434 | */ |