2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3 * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
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/
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
17 * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
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.
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>
36 #include <switch_private.h>
38 #include <switch_stun.h>
39 #include <fspr_network_io.h>
42 #undef PACKAGE_TARNAME
43 #undef PACKAGE_VERSION
44 #undef PACKAGE_BUGREPORT
49 #include <srtp_priv.h>
50 #include <switch_ssl.h>
51 #include <switch_jitterbuffer.h>
53 //#define DEBUG_TS_ROLLOVER
54 #ifdef DEBUG_TS_ROLLOVER
55 #define TS_ROLLOVER_START 4294951295
59 //#define RTP_DEBUG_WRITE_DELTA
60 //#define DEBUG_MISSED_SEQ
63 #define DEBUG_ESTIMATORS_
66 #define JITTER_LEAD_FRAMES 10
67 #define READ_INC(rtp_session) switch_mutex_lock(rtp_session->read_mutex); rtp_session->reading++
68 #define READ_DEC(rtp_session) rtp_session->reading--; switch_mutex_unlock(rtp_session->read_mutex)
69 #define WRITE_INC(rtp_session) switch_mutex_lock(rtp_session->write_mutex); rtp_session->writing++
70 #define WRITE_DEC(rtp_session) rtp_session->writing--; switch_mutex_unlock(rtp_session->write_mutex)
72 #define RTP_STUN_FREQ 1000000
73 #define rtp_header_len 12
74 #define RTP_START_PORT 16384
75 #define RTP_END_PORT 32768
76 #define MASTER_KEY_LEN 30
77 #define RTP_MAGIC_NUMBER 42
78 #define WARN_SRTP_ERRS 10
79 #define MAX_SRTP_ERRS 100
80 #define NTP_TIME_OFFSET 2208988800UL
81 static const switch_payload_t INVALID_PT
= 255;
83 #define DTMF_SANITY (rtp_session->one_second * 30)
85 #define rtp_session_name(_rtp_session) _rtp_session->session ? switch_core_session_get_name(_rtp_session->session) : "-"
87 #define STUN_USERNAME_MAX_SIZE 513 /* From RFC5389: "It MUST contain a UTF-8 [RFC3629] encoded sequence of less than 513 bytes" */
88 #define SDP_UFRAG_MAX_SIZE 256 /* From draft-ietf-mmusic-ice-sip-sdp-24: "the ice-ufrag attribute MUST NOT be longer than 32
89 * characters when sending, but an implementation MUST accept up to 256
90 * characters when receiving." */
92 static switch_port_t START_PORT
= RTP_START_PORT
;
93 static switch_port_t END_PORT
= RTP_END_PORT
;
94 static switch_mutex_t
*port_lock
= NULL
;
95 static switch_size_t
do_flush(switch_rtp_t
*rtp_session
, int force
, switch_size_t bytes_in
);
97 typedef srtp_hdr_t rtp_hdr_t
;
109 static switch_hash_t
*alloc_hash
= NULL
;
113 char body
[SWITCH_RTP_MAX_BUF_LEN
+4+sizeof(char *)];
114 switch_rtp_hdr_ext_t
*ext
;
118 #define RTP_BODY(_s) (char *) (_s->recv_msg.ebody ? _s->recv_msg.ebody : _s->recv_msg.body)
129 #pragma pack(push, r1, 1)
132 typedef struct switch_rtcp_sdes_unit_s
{
134 unsigned char length
;
136 } switch_rtcp_sdes_unit_t
;
143 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
153 } switch_rtcp_ext_hdr_t
;
155 #else /* BIG_ENDIAN */
165 } switch_rtcp_ext_hdr_t
;
170 #pragma pack(pop, r1)
173 #define KALMAN_SYSTEM_MODELS 3 /*loss, jitter, rtt*/
179 switch_rtcp_ext_hdr_t header
;
180 char body
[SWITCH_RTCP_MAX_BUF_LEN
];
184 switch_rtcp_hdr_t header
;
185 char body
[SWITCH_RTCP_MAX_BUF_LEN
];
190 VAD_FIRE_TALK
= (1 << 0),
191 VAD_FIRE_NOT_TALK
= (1 << 1)
194 struct switch_rtp_vad_data
{
195 switch_core_session_t
*session
;
196 switch_codec_t vad_codec
;
197 switch_codec_t
*read_codec
;
203 uint8_t hangunder_hits
;
205 uint8_t hangover_hits
;
208 switch_vad_flag_t flags
;
214 switch_time_t start_talking
;
215 switch_time_t stop_talking
;
216 switch_time_t total_talk_time
;
220 struct switch_rtp_rfc2833_data
{
221 switch_queue_t
*dtmf_queue
;
223 unsigned char out_digit_packet
[4];
224 unsigned int out_digit_sofar
;
225 unsigned int out_digit_sub_sofar
;
226 unsigned int out_digit_dur
;
227 uint16_t in_digit_seq
;
228 uint32_t in_digit_ts
;
229 uint32_t last_in_digit_ts
;
230 uint32_t in_digit_sanity
;
231 uint32_t in_interleaved
;
232 uint32_t timestamp_dtmf
;
233 uint16_t last_duration
;
237 switch_queue_t
*dtmf_inqueue
;
238 switch_mutex_t
*dtmf_mutex
;
239 uint8_t in_digit_queued
;
248 switch_sockaddr_t
*addr
;
250 switch_time_t next_run
;
251 switch_core_media_ice_type_t type
;
259 char last_sent_id
[13];
260 switch_time_t last_ok
;
265 static void switch_rtp_dtls_init();
266 static void switch_rtp_dtls_destroy();
268 #define MAX_DTLS_MTU 4096
270 typedef struct switch_dtls_s
{
277 dtls_fingerprint_t
*local_fp
;
278 dtls_fingerprint_t
*remote_fp
;
280 dtls_state_t last_state
;
285 switch_socket_t
*sock_output
;
286 switch_sockaddr_t
*remote_addr
;
291 struct switch_rtp
*rtp_session
;
295 typedef int (*dtls_state_handler_t
)(switch_rtp_t
*, switch_dtls_t
*);
298 static int dtls_state_handshake(switch_rtp_t
*rtp_session
, switch_dtls_t
*dtls
);
299 static int dtls_state_ready(switch_rtp_t
*rtp_session
, switch_dtls_t
*dtls
);
300 static int dtls_state_setup(switch_rtp_t
*rtp_session
, switch_dtls_t
*dtls
);
301 static int dtls_state_fail(switch_rtp_t
*rtp_session
, switch_dtls_t
*dtls
);
303 dtls_state_handler_t dtls_states
[DS_INVALID
] = {NULL
, dtls_state_handshake
, dtls_state_setup
, dtls_state_ready
, dtls_state_fail
};
305 typedef struct ts_normalize_s
{
316 * Two sockets are needed because we might be transcoding protocol families
317 * (e.g. receive over IPv4 and send over IPv6). In case the protocol
318 * families are equal, sock_input == sock_output and only one socket is
321 switch_socket_t
*sock_input
, *sock_output
, *rtcp_sock_input
, *rtcp_sock_output
;
322 switch_pollfd_t
*read_pollfd
, *rtcp_read_pollfd
;
323 switch_pollfd_t
*jb_pollfd
;
325 switch_sockaddr_t
*local_addr
, *rtcp_local_addr
;
327 rtcp_msg_t rtcp_send_msg
;
328 switch_rtcp_frame_t rtcp_frame
;
338 ts_normalize_t ts_norm
;
339 switch_sockaddr_t
*remote_addr
, *rtcp_remote_addr
;
341 rtcp_msg_t rtcp_recv_msg
;
342 rtcp_msg_t
*rtcp_recv_msg_p
;
344 uint32_t autoadj_window
;
345 uint32_t autoadj_threshold
;
346 uint32_t autoadj_tally
;
348 uint32_t rtcp_autoadj_window
;
349 uint32_t rtcp_autoadj_threshold
;
350 uint32_t rtcp_autoadj_tally
;
352 srtp_ctx_t
*send_ctx
[2];
353 srtp_ctx_t
*recv_ctx
[2];
355 srtp_policy_t send_policy
[2];
356 srtp_policy_t recv_policy
[2];
358 uint32_t srtp_errs
[2];
359 uint32_t srctp_errs
[2];
366 switch_dtls_t
*rtcp_dtls
;
368 rtp_hdr_t last_rtp_hdr
;
372 uint32_t remote_ssrc
;
373 uint32_t last_jb_read_ssrc
;
376 switch_payload_t payload
;
377 switch_rtp_invalid_handler_t invalid_handler
;
380 //uint32_t last_clock_ts;
381 uint32_t last_write_ts
;
382 uint32_t last_read_ts
;
383 uint32_t prev_read_ts
;
384 uint32_t last_cng_ts
;
385 uint32_t last_write_samplecount
;
386 uint32_t delay_samples
;
387 uint32_t next_write_samplecount
;
388 uint32_t max_next_write_samplecount
;
389 uint32_t queue_delay
;
390 switch_time_t last_write_timestamp
;
391 uint32_t flags
[SWITCH_RTP_FLAG_INVALID
];
392 switch_memory_pool_t
*pool
;
393 switch_sockaddr_t
*from_addr
, *rtp_from_addr
, *rtcp_from_addr
, *bundle_internal_addr
, *bundle_external_addr
;
395 switch_port_t rx_port
;
396 switch_rtp_ice_t ice
;
397 switch_rtp_ice_t rtcp_ice
;
399 char *local_host_str
;
400 char *remote_host_str
;
401 char *eff_remote_host_str
;
402 switch_time_t first_stun
;
403 switch_time_t last_stun
;
404 uint32_t wrong_addrs
;
405 uint32_t samples_per_interval
;
406 uint32_t samples_per_second
;
407 uint32_t conf_samples_per_interval
;
408 switch_time_t rtcp_last_sent
;
409 uint32_t rsamples_per_interval
;
410 uint32_t ms_per_packet
;
412 uint32_t consecutive_flaws
;
413 uint32_t jitter_lead
;
415 switch_time_t next_stat_check_time
;
416 switch_port_t local_port
;
417 switch_port_t remote_port
;
418 switch_port_t eff_remote_port
;
419 switch_port_t remote_rtcp_port
;
421 struct switch_rtp_vad_data vad_data
;
422 struct switch_rtp_rfc2833_data dtmf_data
;
424 switch_payload_t recv_te
;
425 switch_payload_t cng_pt
;
426 switch_mutex_t
*flag_mutex
;
427 switch_mutex_t
*read_mutex
;
428 switch_mutex_t
*write_mutex
;
429 switch_mutex_t
*ice_mutex
;
430 switch_timer_t timer
;
431 switch_timer_t write_timer
;
437 uint32_t max_missed_packets
;
438 uint32_t missed_count
;
439 switch_time_t last_media
;
440 uint32_t media_timeout
;
442 switch_rtp_crypto_key_t
*crypto_keys
[SWITCH_RTP_CRYPTO_MAX
];
446 switch_port_t stun_port
;
449 switch_rtp_bug_flag_t rtp_bugs
;
450 switch_rtp_stats_t stats
;
451 switch_rtcp_video_stats_t rtcp_vstats
;
452 uint32_t clean_stream
;
454 uint32_t recovering_stream
;
457 uint32_t sync_packets
;
459 int rtcp_sent_packets
;
460 switch_bool_t rtcp_fresh_frame
;
462 switch_time_t send_time
;
463 switch_byte_t auto_adj_used
;
464 switch_byte_t rtcp_auto_adj_used
;
467 uint16_t last_write_seq
;
468 uint8_t video_delta_mode
;
469 switch_time_t last_read_time
;
470 switch_size_t last_flush_packet_count
;
471 uint32_t interdigit_delay
;
472 switch_core_session_t
*session
;
473 payload_map_t
**pmaps
;
474 payload_map_t
*pmap_tail
;
475 kalman_estimator_t
*estimators
[KALMAN_SYSTEM_MODELS
];
476 cusum_kalman_detector_t
*detectors
[KALMAN_SYSTEM_MODELS
];
483 uint32_t last_max_vb_frames
;
485 uint32_t prev_nacks_inflight
;
488 struct switch_rtcp_report_block
{
489 uint32_t ssrc
; /* The SSRC identifier of the source to which the information in this reception report block pertains. */
490 unsigned int fraction
:8; /* The fraction of RTP data packets from source SSRC_n lost since the previous SR or RR packet was sent */
491 int lost
:24; /* The total number of RTP data packets from source SSRC_n that have been lost since the beginning of reception */
492 uint32_t highest_sequence_number_received
;
493 uint32_t jitter
; /* An estimate of the statistical variance of the RTP data packet interarrival time, measured in timestamp units and expressed as an unsigned integer. */
494 uint32_t lsr
; /* The middle 32 bits out of 64 in the NTP timestamp */
495 uint32_t dlsr
; /* The delay, expressed in units of 1/65536 seconds, between receiving the last SR packet from source SSRC_n and sending this reception report block */
498 struct switch_rtcp_sr_head
{
507 struct switch_rtcp_sender_info
{
515 struct switch_rtcp_sender_report
{
517 struct switch_rtcp_sender_info sender_info
;
518 struct switch_rtcp_report_block report_block
;
521 struct switch_rtcp_receiver_report
{
523 struct switch_rtcp_report_block report_block
;
529 RESULT_GOTO_RECVFROM
,
530 RESULT_GOTO_TIMERCHECK
531 } handle_rfc2833_result_t
;
533 static void do_2833(switch_rtp_t
*rtp_session
);
536 #define rtp_type(rtp_session) rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")
539 static void switch_rtp_change_ice_dest(switch_rtp_t
*rtp_session
, switch_rtp_ice_t
*ice
, const char *host
, switch_port_t port
)
541 int is_rtcp
= ice
== &rtp_session
->rtcp_ice
;
542 const char *err
= "";
544 ice
->ice_params
->cands
[ice
->ice_params
->chosen
[ice
->proto
]][ice
->proto
].con_addr
= switch_core_strdup(rtp_session
->pool
, host
);
545 ice
->ice_params
->cands
[ice
->ice_params
->chosen
[ice
->proto
]][ice
->proto
].con_port
= port
;
546 ice
->missed_count
= 0;
549 ice
->addr
= rtp_session
->rtcp_remote_addr
;
551 switch_rtp_set_remote_address(rtp_session
, host
, port
, 0, SWITCH_FALSE
, &err
);
553 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
554 ice
->addr
= rtp_session
->remote_addr
;
562 static handle_rfc2833_result_t
handle_rfc2833(switch_rtp_t
*rtp_session
, switch_size_t bytes
, int *do_cng
)
565 if (rtp_session
->flags
[SWITCH_RTP_FLAG_DTMF_ON
]) {
566 rtp_session
->flags
[SWITCH_RTP_FLAG_DTMF_ON
]++;
568 if (rtp_session
->flags
[SWITCH_RTP_FLAG_DTMF_ON
] > DTMF_SANITY
) {
569 rtp_session
->flags
[SWITCH_RTP_FLAG_DTMF_ON
] = 0;
571 rtp_session
->stats
.inbound
.last_processed_seq
= 0;
577 if (rtp_session
->dtmf_data
.in_digit_sanity
&& !(rtp_session
->dtmf_data
.in_digit_sanity
% 100)) {
578 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "sanity %d %ld\n", rtp_session
->dtmf_data
.in_digit_sanity
, bytes
);
582 if (rtp_session
->dtmf_data
.in_digit_sanity
&& !--rtp_session
->dtmf_data
.in_digit_sanity
) {
584 rtp_session
->dtmf_data
.last_digit
= 0;
585 rtp_session
->dtmf_data
.in_digit_ts
= 0;
586 rtp_session
->dtmf_data
.in_digit_queued
= 0;
587 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Failed DTMF sanity check.\n");
590 if (!bytes
) return RESULT_CONTINUE
;
593 /* RFC2833 ... like all RFC RE: VoIP, guaranteed to drive you to insanity!
594 We know the real rules here, but if we enforce them, it's an interop nightmare so,
595 we put up with as much as we can so we don't have to deal with being punished for
596 doing it right. Nice guys finish last!
599 if (bytes
> rtp_header_len
&& !rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] &&
600 rtp_session
->last_rtp_hdr
.pt
== rtp_session
->recv_te
) {
601 switch_size_t len
= bytes
- rtp_header_len
;
602 unsigned char *packet
= (unsigned char *) RTP_BODY(rtp_session
);
606 uint16_t in_digit_seq
;
609 rtp_session
->stats
.inbound
.last_processed_seq
= 0;
611 if (!(packet
[0] || packet
[1] || packet
[2] || packet
[3]) && len
>= 8) {
613 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
, "DTMF payload offset by 4 bytes.\n");
616 if (!(packet
[0] || packet
[1] || packet
[2] || packet
[3]) && rtp_session
->dtmf_data
.in_digit_ts
) {
617 switch_core_session_t
*session
= switch_core_memory_pool_get_data(rtp_session
->pool
, "__session");
618 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Failed DTMF payload check.\n");
619 rtp_session
->dtmf_data
.last_digit
= 0;
620 rtp_session
->dtmf_data
.in_digit_ts
= 0;
621 rtp_session
->dtmf_data
.in_digit_sanity
= 0;
622 rtp_session
->dtmf_data
.in_digit_queued
= 0;
625 end
= packet
[1] & 0x80 ? 1 : 0;
626 duration
= (packet
[2] << 8) + packet
[3];
627 key
= switch_rfc2833_to_char(packet
[0]);
628 in_digit_seq
= ntohs((uint16_t) rtp_session
->last_rtp_hdr
.seq
);
629 ts
= htonl(rtp_session
->last_rtp_hdr
.ts
);
631 if (rtp_session
->flags
[SWITCH_RTP_FLAG_PASS_RFC2833
]) {
634 rtp_session
->flags
[SWITCH_RTP_FLAG_DTMF_ON
] = DTMF_SANITY
- 3;
635 } else if (!rtp_session
->flags
[SWITCH_RTP_FLAG_DTMF_ON
]) {
636 rtp_session
->flags
[SWITCH_RTP_FLAG_DTMF_ON
] = 1;
639 return RESULT_CONTINUE
;
642 if (in_digit_seq
< rtp_session
->dtmf_data
.in_digit_seq
) {
643 if (rtp_session
->dtmf_data
.in_digit_seq
- in_digit_seq
> 100) {
644 rtp_session
->dtmf_data
.in_digit_seq
= 0;
648 if (!(packet
[0] || packet
[1] || packet
[2] || packet
[3]) && len
>= 8) {
651 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "packet[%d]: %02x %02x %02x %02x\n", (int) len
, (unsigned char) packet
[0], (unsigned char) packet
[1], (unsigned char) packet
[2], (unsigned char) packet
[3]);
654 if (in_digit_seq
> rtp_session
->dtmf_data
.in_digit_seq
) {
656 rtp_session
->dtmf_data
.in_digit_seq
= in_digit_seq
;
659 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "read: %c %u %u %u %u %d %d %s\n",
660 key
, in_digit_seq
, rtp_session
->dtmf_data
.in_digit_seq
,
661 ts
, duration
, rtp_session
->last_rtp_hdr
.m
, end
, end
&& !rtp_session
->dtmf_data
.in_digit_ts
? "ignored" : "");
665 if (rtp_session
->dtmf_data
.in_digit_ts
&& rtp_session
->dtmf_data
.in_digit_ts
!= ts
) {
666 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_DEBUG
, "TS changed from last packet, resetting....\n");
667 rtp_session
->dtmf_data
.last_digit
= 0;
668 rtp_session
->dtmf_data
.in_digit_ts
= 0;
669 rtp_session
->dtmf_data
.in_digit_sanity
= 0;
670 rtp_session
->dtmf_data
.in_digit_queued
= 0;
674 if (!rtp_session
->dtmf_data
.in_digit_queued
&& rtp_session
->dtmf_data
.in_digit_ts
) {
675 if ((rtp_session
->rtp_bugs
& RTP_BUG_IGNORE_DTMF_DURATION
)) {
676 switch_dtmf_t dtmf
= { key
, switch_core_min_dtmf_duration(0), 0, SWITCH_DTMF_RTP
};
678 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "Early Queuing digit %c:%d\n", dtmf
.digit
, dtmf
.duration
/ 8);
680 switch_rtp_queue_rfc2833_in(rtp_session
, &dtmf
);
681 rtp_session
->dtmf_data
.in_digit_queued
= 1;
684 if (rtp_session
->jb
&& (rtp_session
->rtp_bugs
& RTP_BUG_FLUSH_JB_ON_DTMF
)) {
685 switch_jb_reset(rtp_session
->jb
);
690 /* only set sanity if we do NOT ignore the packet */
691 if (rtp_session
->dtmf_data
.in_digit_ts
) {
692 rtp_session
->dtmf_data
.in_digit_sanity
= 2000;
695 if (rtp_session
->dtmf_data
.last_duration
> duration
&&
696 rtp_session
->dtmf_data
.last_duration
> 0xFC17 && ts
== rtp_session
->dtmf_data
.in_digit_ts
) {
697 rtp_session
->dtmf_data
.flip
++;
701 if (!rtp_session
->dtmf_data
.in_digit_ts
&& rtp_session
->dtmf_data
.last_in_digit_ts
!= ts
) {
703 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "start with end packet %d\n", ts
);
705 rtp_session
->dtmf_data
.last_in_digit_ts
= ts
;
706 rtp_session
->dtmf_data
.in_digit_ts
= ts
;
707 rtp_session
->dtmf_data
.first_digit
= key
;
708 rtp_session
->dtmf_data
.in_digit_sanity
= 2000;
710 if (rtp_session
->dtmf_data
.in_digit_ts
) {
711 switch_dtmf_t dtmf
= { key
, duration
, 0, SWITCH_DTMF_RTP
};
713 if (ts
> rtp_session
->dtmf_data
.in_digit_ts
) {
714 dtmf
.duration
+= (ts
- rtp_session
->dtmf_data
.in_digit_ts
);
716 if (rtp_session
->dtmf_data
.flip
) {
717 dtmf
.duration
+= rtp_session
->dtmf_data
.flip
* 0xFFFF;
718 rtp_session
->dtmf_data
.flip
= 0;
720 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "you're welcome!\n");
724 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "done digit=%c ts=%u start_ts=%u dur=%u ddur=%u\n",
725 dtmf
.digit
, ts
, rtp_session
->dtmf_data
.in_digit_ts
, duration
, dtmf
.duration
);
728 if (!(rtp_session
->rtp_bugs
& RTP_BUG_IGNORE_DTMF_DURATION
) && !rtp_session
->dtmf_data
.in_digit_queued
) {
730 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "Queuing digit %c:%d\n", dtmf
.digit
, dtmf
.duration
/ 8);
732 switch_rtp_queue_rfc2833_in(rtp_session
, &dtmf
);
735 rtp_session
->dtmf_data
.last_digit
= rtp_session
->dtmf_data
.first_digit
;
737 rtp_session
->dtmf_data
.in_digit_ts
= 0;
738 rtp_session
->dtmf_data
.in_digit_sanity
= 0;
739 rtp_session
->dtmf_data
.in_digit_queued
= 0;
742 if (!switch_rtp_ready(rtp_session
)) {
743 return RESULT_GOTO_END
;
746 return RESULT_GOTO_RECVFROM
;
749 } else if (!rtp_session
->dtmf_data
.in_digit_ts
) {
751 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "start %d [%c]\n", ts
, key
);
753 rtp_session
->dtmf_data
.in_digit_ts
= ts
;
754 rtp_session
->dtmf_data
.last_in_digit_ts
= ts
;
755 rtp_session
->dtmf_data
.first_digit
= key
;
756 rtp_session
->dtmf_data
.in_digit_sanity
= 2000;
759 rtp_session
->dtmf_data
.last_duration
= duration
;
762 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "drop: %c %u %u %u %u %d %d\n",
763 key
, in_digit_seq
, rtp_session
->dtmf_data
.in_digit_seq
, ts
, duration
, rtp_session
->last_rtp_hdr
.m
, end
);
766 return RESULT_GOTO_RECVFROM
;
770 if (rtp_session
->dtmf_data
.in_digit_ts
) {
771 if (!switch_rtp_ready(rtp_session
)) {
772 return RESULT_GOTO_END
;
775 if (!rtp_session
->dtmf_data
.in_interleaved
&& rtp_session
->last_rtp_hdr
.pt
!= rtp_session
->recv_te
) {
776 /* Drat, they are sending audio still as well as DTMF ok fine..... *sigh* */
777 rtp_session
->dtmf_data
.in_interleaved
= 1;
780 if (rtp_session
->dtmf_data
.in_interleaved
|| (rtp_session
->rtp_bugs
& RTP_BUG_IGNORE_DTMF_DURATION
)) {
781 if (rtp_session
->last_rtp_hdr
.pt
== rtp_session
->recv_te
) {
782 return RESULT_GOTO_RECVFROM
;
786 return RESULT_GOTO_TIMERCHECK
;
790 return RESULT_CONTINUE
;
793 static int rtp_write_ready(switch_rtp_t
*rtp_session
, uint32_t bytes
, int line
);
794 static int global_init
= 0;
795 static int rtp_common_write(switch_rtp_t
*rtp_session
,
796 rtp_msg_t
*send_msg
, void *data
, uint32_t datalen
, switch_payload_t payload
, uint32_t timestamp
, switch_frame_flag_t
*flags
);
799 static switch_status_t
ice_out(switch_rtp_t
*rtp_session
, switch_rtp_ice_t
*ice
)
801 uint8_t buf
[256] = { 0 };
802 switch_stun_packet_t
*packet
;
803 unsigned int elapsed
;
805 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
806 //switch_sockaddr_t *remote_addr = rtp_session->remote_addr;
807 switch_socket_t
*sock_output
= rtp_session
->sock_output
;
808 switch_time_t now
= switch_micro_time_now();
810 if (ice
->type
& ICE_LITE
) {
811 // no connectivity checks for ICE-Lite
812 return SWITCH_STATUS_BREAK
;
815 if (ice
->next_run
&& ice
->next_run
> now
) {
816 return SWITCH_STATUS_BREAK
;
819 ice
->next_run
= now
+ RTP_STUN_FREQ
;
821 if (ice
== &rtp_session
->rtcp_ice
&& rtp_session
->rtcp_sock_output
) {
822 sock_output
= rtp_session
->rtcp_sock_output
;
826 return SWITCH_STATUS_FALSE
;
829 switch_assert(rtp_session
!= NULL
);
830 switch_assert(ice
->ice_user
!= NULL
);
832 READ_INC(rtp_session
);
834 if (rtp_session
->last_stun
) {
835 elapsed
= (unsigned int) ((switch_micro_time_now() - rtp_session
->last_stun
) / 1000);
837 if (elapsed
> 30000) {
838 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
, "No %s stun for a long time!\n", rtp_type(rtp_session
));
839 rtp_session
->last_stun
= switch_micro_time_now();
840 //status = SWITCH_STATUS_GENERR;
845 packet
= switch_stun_packet_build_header(SWITCH_STUN_BINDING_REQUEST
, NULL
, buf
);
846 switch_stun_packet_attribute_add_username(packet
, ice
->ice_user
, (uint16_t)strlen(ice
->ice_user
));
848 memcpy(ice
->last_sent_id
, packet
->header
.id
, 12);
850 //if (ice->pass && ice->type == ICE_GOOGLE_JINGLE) {
851 // switch_stun_packet_attribute_add_password(packet, ice->pass, (uint16_t)strlen(ice->pass));
854 if ((ice
->type
& ICE_VANILLA
)) {
857 switch_stun_packet_attribute_add_priority(packet
, ice
->ice_params
->cands
[ice
->ice_params
->chosen
[ice
->proto
]][ice
->proto
].priority
);
859 switch_snprintf(sw
, sizeof(sw
), "FreeSWITCH (%s)", switch_version_revision_human());
860 switch_stun_packet_attribute_add_software(packet
, sw
, (uint16_t)strlen(sw
));
862 if ((ice
->type
& ICE_CONTROLLED
)) {
863 switch_stun_packet_attribute_add_controlled(packet
);
865 switch_stun_packet_attribute_add_controlling(packet
);
866 switch_stun_packet_attribute_add_use_candidate(packet
);
869 switch_stun_packet_attribute_add_integrity(packet
, ice
->rpass
);
870 switch_stun_packet_attribute_add_fingerprint(packet
);
874 bytes
= switch_stun_packet_length(packet
);
877 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_CRIT
, "%s send %s stun\n", rtp_session_name(rtp_session
), rtp_type(rtp_session
));
879 switch_socket_sendto(sock_output
, ice
->addr
, 0, (void *) packet
, &bytes
);
884 READ_DEC(rtp_session
);
889 int icecmp(const char *them
, switch_rtp_ice_t
*ice
)
891 if (strchr(them
, ':')) {
892 return strcmp(them
, ice
->user_ice
);
895 return strcmp(them
, ice
->luser_ice
);
898 static void handle_ice(switch_rtp_t
*rtp_session
, switch_rtp_ice_t
*ice
, void *data
, switch_size_t len
)
900 switch_stun_packet_t
*packet
;
901 switch_stun_packet_attribute_t
*attr
;
903 char username
[STUN_USERNAME_MAX_SIZE
] = { 0 };
904 unsigned char buf
[1500] = { 0 };
905 switch_size_t cpylen
= len
;
908 uint32_t *pri
= NULL
;
909 int is_rtcp
= ice
== &rtp_session
->rtcp_ice
;
911 switch_time_t ref_point
;
913 //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
914 // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s CALL\n", rtp_type(rtp_session));
917 if (!switch_rtp_ready(rtp_session
) || zstr(ice
->user_ice
) || zstr(ice
->ice_user
)) {
921 READ_INC(rtp_session
);
922 WRITE_INC(rtp_session
);
924 switch_mutex_lock(rtp_session
->ice_mutex
);
926 if (!switch_rtp_ready(rtp_session
)) {
930 if (cpylen
> sizeof(buf
)) {
931 cpylen
= sizeof(buf
);
935 memcpy(buf
, data
, cpylen
);
936 packet
= switch_stun_packet_parse(buf
, (uint32_t)cpylen
);
938 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Invalid STUN/ICE packet received %ld bytes\n", (long)cpylen
);
943 rtp_session
->last_stun
= switch_micro_time_now();
945 if (!rtp_session
->first_stun
) {
946 rtp_session
->first_stun
= rtp_session
->last_stun
;
949 if (ice
->last_ok
&& (!rtp_session
->dtls
|| rtp_session
->dtls
->state
== DS_READY
)) {
950 ref_point
= ice
->last_ok
;
952 ref_point
= rtp_session
->first_stun
;
955 elapsed
= (unsigned int) ((switch_micro_time_now() - ref_point
) / 1000);
958 end_buf
= buf
+ ((sizeof(buf
) > packet
->header
.length
) ? packet
->header
.length
: sizeof(buf
));
960 switch_stun_packet_first_attribute(packet
, attr
);
961 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG8
, "%s STUN PACKET TYPE: %s\n",
962 rtp_type(rtp_session
), switch_stun_value_to_name(SWITCH_STUN_TYPE_PACKET_TYPE
, packet
->header
.type
));
964 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG8
, "|---: %s STUN ATTR %d %x %s\n", rtp_type(rtp_session
), attr
->type
, attr
->type
,
965 switch_stun_value_to_name(SWITCH_STUN_TYPE_ATTRIBUTE
, attr
->type
));
967 switch (attr
->type
) {
968 case SWITCH_STUN_ATTR_USE_CAND
:
973 case SWITCH_STUN_ATTR_ERROR_CODE
:
975 switch_stun_error_code_t
*err
= (switch_stun_error_code_t
*) attr
->value
;
976 uint32_t code
= (err
->code
* 100) + err
->number
;
978 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
, "%s got %s stun binding response %u\n",
979 rtp_session_name(rtp_session
),
980 rtp_type(rtp_session
),
984 if ((ice
->type
& ICE_VANILLA
) && code
== 487) {
985 if ((ice
->type
& ICE_CONTROLLED
)) {
986 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
, "%s STUN Changing role to CONTROLLING\n", rtp_type(rtp_session
));
987 ice
->type
&= ~ICE_CONTROLLED
;
989 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
, "%s STUN Changing role to CONTROLLED\n", rtp_type(rtp_session
));
990 ice
->type
|= ICE_CONTROLLED
;
992 packet
->header
.type
= SWITCH_STUN_BINDING_RESPONSE
;
997 case SWITCH_STUN_ATTR_MAPPED_ADDRESS
:
1001 switch_stun_packet_attribute_get_mapped_address(attr
, ip
, sizeof(ip
), &port
);
1002 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG8
, "|------: %s:%d\n", ip
, port
);
1005 case SWITCH_STUN_ATTR_XOR_MAPPED_ADDRESS
:
1009 switch_stun_packet_attribute_get_xor_mapped_address(attr
, &packet
->header
, ip
, sizeof(ip
), &port
);
1010 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG8
, "|------: %s:%d\n", ip
, port
);
1013 case SWITCH_STUN_ATTR_USERNAME
:
1015 switch_stun_packet_attribute_get_username(attr
, username
, sizeof(username
));
1016 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG8
, "|------: %s\n", username
);
1020 case SWITCH_STUN_ATTR_PRIORITY
:
1022 uint32_t priority
= 0;
1023 pri
= (uint32_t *) attr
->value
;
1024 priority
= ntohl(*pri
);
1025 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG8
, "|------: %u\n", priority
);
1026 ok
= priority
== ice
->ice_params
->cands
[ice
->ice_params
->chosen
[ice
->proto
]][ice
->proto
].priority
;
1031 if (!switch_stun_packet_next_attribute(attr
, end_buf
)) {
1035 xlen
+= 4 + switch_stun_attribute_padded_length(attr
);
1036 } while (xlen
<= packet
->header
.length
);
1038 if ((ice
->type
& ICE_GOOGLE_JINGLE
) && ok
) {
1039 ok
= !strcmp(ice
->user_ice
, username
);
1042 if (packet
->header
.type
!= SWITCH_STUN_BINDING_REQUEST
&& packet
->header
.type
!= SWITCH_STUN_BINDING_RESPONSE
) {
1046 if ((ice
->type
& ICE_VANILLA
)) {
1047 if (!ok
) ok
= !memcmp(packet
->header
.id
, ice
->last_sent_id
, 12);
1049 if (packet
->header
.type
== SWITCH_STUN_BINDING_RESPONSE
) {
1052 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
1053 rtp_session
->ice
.rready
= 1;
1054 rtp_session
->rtcp_ice
.rready
= 1;
1059 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
1060 switch_core_session_video_reinit(rtp_session
->session
);
1065 if (!ok
&& ice
== &rtp_session
->ice
&& rtp_session
->rtcp_ice
.ice_params
&& pri
&&
1066 *pri
== rtp_session
->rtcp_ice
.ice_params
->cands
[rtp_session
->rtcp_ice
.ice_params
->chosen
[1]][1].priority
) {
1067 ice
= &rtp_session
->rtcp_ice
;
1071 if (!zstr(username
)) {
1072 if (!icecmp(username
, ice
)) {
1074 } else if(!zstr(rtp_session
->rtcp_ice
.user_ice
) && !icecmp(username
, &rtp_session
->rtcp_ice
)) {
1075 ice
= &rtp_session
->rtcp_ice
;
1081 ice
->missed_count
= 0;
1083 switch_rtp_ice_t
*icep
[2] = { &rtp_session
->ice
, &rtp_session
->rtcp_ice
};
1084 switch_port_t port
= 0;
1087 if (elapsed
> 20000 && pri
) {
1090 //const char *tx_host;
1091 const char *old_host
, *err
= NULL
;
1095 switch_channel_t
*channel
= NULL
;
1098 ice
->missed_count
++;
1099 //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "missed %d\n", ice->missed_count);
1102 if (rtp_session
->session
) {
1103 channel
= switch_core_session_get_channel(rtp_session
->session
);
1106 //ice->ice_params->cands[ice->ice_params->chosen][ice->proto].priority;
1107 for (j
= 0; j
< 2; j
++) {
1108 if (!icep
[j
] || !icep
[j
]->ice_params
) {
1111 for (i
= 0; i
< icep
[j
]->ice_params
->cand_idx
[icep
[j
]->proto
]; i
++) {
1112 if (icep
[j
]->ice_params
&& icep
[j
]->ice_params
->cands
[i
][icep
[j
]->proto
].priority
== *pri
) {
1114 icep
[j
]->ice_params
->chosen
[j
] = i
;
1115 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_INFO
, "Change candidate index to %d\n", i
);
1125 old
= rtp_session
->remote_port
;
1127 //tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr);
1128 old_host
= switch_get_addr(bufb
, sizeof(bufb
), rtp_session
->remote_addr
);
1130 host
= ice
->ice_params
->cands
[ice
->ice_params
->chosen
[ice
->proto
]][ice
->proto
].con_addr
;
1131 port
= ice
->ice_params
->cands
[ice
->ice_params
->chosen
[ice
->proto
]][ice
->proto
].con_port
;
1133 if (!host
|| !port
) {
1134 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Error setting remote host!\n");
1138 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_INFO
,
1139 "%s ICE Auto Changing port from %s:%u to %s:%u\n", rtp_type(rtp_session
), old_host
, old
, host
, port
);
1143 switch_channel_set_variable(channel
, "remote_media_ip_reported", switch_channel_get_variable(channel
, "remote_media_ip"));
1144 switch_channel_set_variable(channel
, "remote_media_ip", host
);
1145 switch_channel_set_variable(channel
, "rtp_auto_adjust_ip", host
);
1146 switch_snprintf(adj_port
, sizeof(adj_port
), "%u", port
);
1147 switch_channel_set_variable(channel
, "remote_media_port_reported", switch_channel_get_variable(channel
, "remote_media_port"));
1148 switch_channel_set_variable(channel
, "remote_media_port", adj_port
);
1149 switch_channel_set_variable(channel
, "rtp_auto_adjust_port", adj_port
);
1150 switch_channel_set_variable(channel
, "rtp_auto_candidate_adjust", "true");
1152 rtp_session
->auto_adj_used
= 1;
1155 switch_rtp_set_remote_address(rtp_session
, host
, port
, 0, SWITCH_FALSE
, &err
);
1156 if (switch_sockaddr_info_get(&ice
->addr
, host
, SWITCH_UNSPEC
, port
, 0, rtp_session
->pool
) != SWITCH_STATUS_SUCCESS
||
1158 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Error setting remote host!\n");
1162 if ((rtp_session
->rtp_bugs
& RTP_BUG_ALWAYS_AUTO_ADJUST
)) {
1163 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_AUTOADJ
);
1165 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_AUTOADJ
);
1175 if (ice
->missed_count
> 5 && !(ice
->type
& ICE_GOOGLE_JINGLE
)) {
1176 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
, "missed too many: %d, looking for new ICE dest.\n",
1183 //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] || 1) {
1184 // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s %d\n", rtp_type(rtp_session), ok);
1188 const char *host
= NULL
, *host2
= NULL
;
1189 switch_port_t port
= 0, port2
= 0;
1193 if (packet
->header
.type
== SWITCH_STUN_BINDING_REQUEST
) {
1194 uint8_t stunbuf
[512];
1195 switch_stun_packet_t
*rpacket
;
1196 const char *remote_ip
;
1197 switch_size_t bytes
;
1199 switch_sockaddr_t
*from_addr
= rtp_session
->from_addr
;
1200 switch_socket_t
*sock_output
= rtp_session
->sock_output
;
1202 switch_time_t now
= switch_micro_time_now();
1204 int cur_idx
= -1;//, is_relay = 0;
1208 from_addr
= rtp_session
->rtcp_from_addr
;
1209 sock_output
= rtp_session
->rtcp_sock_output
;
1216 memset(stunbuf
, 0, sizeof(stunbuf
));
1217 rpacket
= switch_stun_packet_build_header(SWITCH_STUN_BINDING_RESPONSE
, packet
->header
.id
, stunbuf
);
1219 if ((ice
->type
& ICE_GOOGLE_JINGLE
)) {
1220 switch_stun_packet_attribute_add_username(rpacket
, username
, (uint16_t)strlen(username
));
1223 remote_ip
= switch_get_addr(ipbuf
, sizeof(ipbuf
), from_addr
);
1225 switch_stun_packet_attribute_add_xor_binded_address(rpacket
, (char *) remote_ip
, switch_sockaddr_get_port(from_addr
), from_addr
->family
);
1227 if ((ice
->type
& ICE_VANILLA
)) {
1228 switch_stun_packet_attribute_add_integrity(rpacket
, ice
->pass
);
1229 switch_stun_packet_attribute_add_fingerprint(rpacket
);
1232 bytes
= switch_stun_packet_length(rpacket
);
1234 host
= switch_get_addr(buf
, sizeof(buf
), from_addr
);
1235 port
= switch_sockaddr_get_port(from_addr
);
1236 host2
= switch_get_addr(buf2
, sizeof(buf2
), ice
->addr
);
1237 port2
= switch_sockaddr_get_port(ice
->addr
);
1238 cmp
= switch_cmp_addr(from_addr
, ice
->addr
, SWITCH_FALSE
);
1240 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
,
1241 "STUN from %s:%d %s\n", host
, port
, cmp
? "EXPECTED" : "IGNORED");
1243 if (ice
->init
&& !cmp
&& switch_cmp_addr(from_addr
, ice
->addr
, SWITCH_TRUE
)) {
1245 rtp_session
->ice_adj
++;
1246 rtp_session
->wrong_addrs
= 0;
1252 rtp_session
->wrong_addrs
= 0;
1254 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG10
, "ICE %d dt:%d i:%d i2:%d w:%d cmp:%d adj:%d\n", elapsed
, (rtp_session
->dtls
&& rtp_session
->dtls
->state
!= DS_READY
), !ice
->ready
, !ice
->rready
, rtp_session
->wrong_addrs
, switch_cmp_addr(from_addr
, ice
->addr
, SWITCH_TRUE
), rtp_session
->ice_adj
);
1256 if ((rtp_session
->dtls
&& rtp_session
->dtls
->state
!= DS_READY
) ||
1257 ((!ice
->ready
|| !ice
->rready
) && (rtp_session
->wrong_addrs
> 2 || switch_cmp_addr(from_addr
, ice
->addr
, SWITCH_TRUE
)) &&
1258 rtp_session
->ice_adj
< 10)) {
1260 rtp_session
->ice_adj
++;
1261 rtp_session
->wrong_addrs
= 0;
1262 } else if (rtp_session
->wrong_addrs
> 10 || elapsed
>= 5000) {
1267 rtp_session
->wrong_addrs
++;
1270 for (i
= 0; i
< ice
->ice_params
->cand_idx
[ice
->proto
]; i
++) {
1271 if (!strcmp(ice
->ice_params
->cands
[i
][ice
->proto
].con_addr
, host
)) {
1273 //if (!strcasecmp(ice->ice_params->cands[i][ice->proto].cand_type, "relay")) {
1280 if (ice
->ice_params
->cands
[ice
->ice_params
->chosen
[ice
->proto
]][ice
->proto
].cand_type
&&
1281 !strcasecmp(ice
->ice_params
->cands
[ice
->ice_params
->chosen
[ice
->proto
]][ice
->proto
].cand_type
, "relay")) {
1286 if ((ice
->type
& ICE_VANILLA
) && ice
->ice_params
&& do_adj
) {
1287 ice
->missed_count
= 0;
1291 ice
->ice_params
->chosen
[ice
->proto
] = cur_idx
;
1294 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_NOTICE
,
1295 "Auto Changing %s stun/%s/dtls port from %s:%u to %s:%u idx:%d\n", rtp_type(rtp_session
), is_rtcp
? "rtcp" : "rtp",
1297 host
, port
, cur_idx
);
1299 switch_rtp_change_ice_dest(rtp_session
, ice
, host
, port
);
1301 rtp_session
->wrong_addrs
= 0;
1304 switch_socket_sendto(sock_output
, from_addr
, 0, (void *) rpacket
, &bytes
);
1307 } else if (packet
->header
.type
== SWITCH_STUN_BINDING_ERROR_RESPONSE
) {
1309 if (rtp_session
->session
) {
1310 switch_core_session_message_t msg
= { 0 };
1311 msg
.from
= __FILE__
;
1312 msg
.numeric_arg
= packet
->header
.type
;
1313 msg
.pointer_arg
= packet
;
1314 msg
.message_id
= SWITCH_MESSAGE_INDICATE_STUN_ERROR
;
1315 switch_core_session_receive_message(rtp_session
->session
, &msg
);
1316 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
,
1317 "STUN/ICE binding error received on %s channel\n", rtp_type(rtp_session
));
1326 switch_mutex_unlock(rtp_session
->ice_mutex
);
1327 WRITE_DEC(rtp_session
);
1328 READ_DEC(rtp_session
);
1333 SWITCH_DECLARE(void) switch_srtp_err_to_txt(srtp_err_status_t stat
, char **msg
)
1335 if (stat
== srtp_err_status_replay_fail
) *msg
="replay check failed";
1336 else if (stat
== srtp_err_status_auth_fail
) *msg
="auth check failed";
1337 else if (stat
== srtp_err_status_fail
) *msg
="unspecified failure";
1338 else if (stat
== srtp_err_status_bad_param
) *msg
="unsupported parameter";
1339 else if (stat
== srtp_err_status_alloc_fail
) *msg
="couldn't allocate memory";
1340 else if (stat
== srtp_err_status_dealloc_fail
) *msg
="couldn't deallocate properly";
1341 else if (stat
== srtp_err_status_init_fail
) *msg
="couldn't initialize";
1342 else if (stat
== srtp_err_status_terminus
) *msg
="can't process as much data as requested";
1343 else if (stat
== srtp_err_status_cipher_fail
) *msg
="cipher failure";
1344 else if (stat
== srtp_err_status_replay_old
) *msg
="replay check failed";
1345 else if (stat
== srtp_err_status_algo_fail
) *msg
="algorithm failed test routine";
1346 else if (stat
== srtp_err_status_no_such_op
) *msg
="unsupported operation";
1347 else if (stat
== srtp_err_status_no_ctx
) *msg
="no appropriate context found";
1348 else if (stat
== srtp_err_status_cant_check
) *msg
="auth check failed";
1349 else if (stat
== srtp_err_status_key_expired
) *msg
="can't use key any more";
1350 else if (stat
== srtp_err_status_socket_err
) *msg
="error in use of socket";
1351 else if (stat
== srtp_err_status_signal_err
) *msg
="error in use POSIX signals";
1352 else if (stat
== srtp_err_status_nonce_bad
) *msg
="nonce check failed";
1353 else if (stat
== srtp_err_status_read_fail
) *msg
="couldn't read data";
1354 else if (stat
== srtp_err_status_write_fail
) *msg
="couldn't write data";
1355 else if (stat
== srtp_err_status_parse_err
) *msg
="error parsing data";
1356 else if (stat
== srtp_err_status_encode_err
) *msg
="error encoding data";
1357 else if (stat
== srtp_err_status_semaphore_err
) *msg
="error while using semaphores";
1358 else if (stat
== srtp_err_status_pfkey_err
) *msg
="error while using pfkey ";
1359 else if (stat
== srtp_err_status_bad_mki
) *msg
="error MKI present in packet is invalid";
1360 else if (stat
== srtp_err_status_pkt_idx_old
) *msg
="packet index is too old to consider";
1361 else if (stat
== srtp_err_status_pkt_idx_adv
) *msg
="packet index advanced, reset needed";
1366 SWITCH_DECLARE(void) switch_rtp_init(switch_memory_pool_t
*pool
)
1371 switch_core_hash_init(&alloc_hash
);
1374 srtp_err_status_t stat
= srtp_init();
1375 if (stat
== srtp_err_status_ok
) {
1376 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_DEBUG
, "SRTP (%s) initialized.\n", srtp_get_version_string());
1378 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "Error initializing SRTP (%d).\n", stat
);
1382 switch_mutex_init(&port_lock
, SWITCH_MUTEX_NESTED
, pool
);
1383 switch_rtp_dtls_init();
1387 static uint8_t get_next_write_ts(switch_rtp_t
*rtp_session
, uint32_t timestamp
)
1389 uint8_t m
= 0, changed
= 0;
1391 if (!(rtp_session
->rtp_bugs
& RTP_BUG_SEND_LINEAR_TIMESTAMPS
)) {
1393 rtp_session
->ts
= (uint32_t) timestamp
;
1395 } else if (switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_USE_TIMER
)) {
1396 switch_core_timer_next(&rtp_session
->write_timer
);
1397 rtp_session
->ts
= rtp_session
->write_timer
.samplecount
;
1403 rtp_session
->ts
= rtp_session
->last_write_ts
+ rtp_session
->samples_per_interval
;
1405 /* Send marker bit if timestamp is lower/same as before (resetted/new timer) */
1406 if (abs((int32_t)(rtp_session
->ts
- rtp_session
->last_write_ts
)) > rtp_session
->samples_per_interval
1407 && !(rtp_session
->rtp_bugs
& RTP_BUG_NEVER_SEND_MARKER
)) {
1415 static void do_mos(switch_rtp_t
*rtp_session
) {
1418 if ((switch_size_t
)rtp_session
->stats
.inbound
.recved
< rtp_session
->stats
.inbound
.flaws
) {
1419 rtp_session
->stats
.inbound
.flaws
= 0;
1422 if (rtp_session
->stats
.inbound
.recved
> 0 &&
1423 rtp_session
->stats
.inbound
.flaws
&& (rtp_session
->stats
.inbound
.last_flaw
!= rtp_session
->stats
.inbound
.flaws
)) {
1425 if (rtp_session
->consecutive_flaws
++) {
1426 int penalty
= rtp_session
->consecutive_flaws
;
1428 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
, "%s %s %d consecutive flaws, adding %d flaw penalty\n",
1429 rtp_session_name(rtp_session
), rtp_type(rtp_session
),
1430 rtp_session
->consecutive_flaws
, penalty
);
1431 rtp_session
->bad_stream
++;
1432 rtp_session
->stats
.inbound
.flaws
+= penalty
;
1433 rtp_session
->stats
.inbound
.last_flaw
= rtp_session
->stats
.inbound
.flaws
;
1435 if (rtp_session
->stats
.inbound
.error_log
) {
1436 rtp_session
->stats
.inbound
.error_log
->flaws
+= penalty
;
1437 rtp_session
->stats
.inbound
.error_log
->consecutive_flaws
++;
1441 rtp_session
->consecutive_flaws
= 0;
1444 R
= (int)((double)((double)(rtp_session
->stats
.inbound
.recved
- rtp_session
->stats
.inbound
.flaws
) / (double)rtp_session
->stats
.inbound
.recved
) * 100.0);
1446 if (R
< 0 || R
> 100) R
= 100;
1448 rtp_session
->stats
.inbound
.R
= R
;
1449 rtp_session
->stats
.inbound
.mos
= 1 + (0.035) * R
+ (.000007) * R
* (R
-60) * (100-R
);
1451 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
, "%s %s stat %0.2f %ld/%d flaws: %ld mos: %0.2f v: %0.2f %0.2f/%0.2f\n",
1452 rtp_session_name(rtp_session
),
1453 rtp_type(rtp_session
),
1454 rtp_session
->stats
.inbound
.R
,
1455 (long int)(rtp_session
->stats
.inbound
.recved
- rtp_session
->stats
.inbound
.flaws
), rtp_session
->stats
.inbound
.recved
,
1456 (long int)rtp_session
->stats
.inbound
.flaws
,
1457 rtp_session
->stats
.inbound
.mos
,
1458 rtp_session
->stats
.inbound
.variance
,
1459 rtp_session
->stats
.inbound
.min_variance
,
1460 rtp_session
->stats
.inbound
.max_variance
1465 void burstr_calculate ( int loss
[], int received
, double *burstr
, double *lossr
)
1471 for ( i
= 0; i
< LOST_BURST_ANALYZE
; i
++ ) {
1472 lost
+= i
* loss
[i
];
1475 if (received
> 0 && bursts
> 0) {
1476 *burstr
= (double)((double)lost
/ (double)bursts
) / (double)(1.0 / ( 1.0 - (double)lost
/ (double)received
));
1478 *burstr
= - *burstr
;
1484 *lossr
= (double)((double)lost
/ (double)received
);
1490 static void reset_jitter_seq(switch_rtp_t
*rtp_session
)
1492 rtp_session
->stats
.inbound
.last_proc_time
= 0;
1493 rtp_session
->stats
.inbound
.last_processed_seq
= 0;
1494 rtp_session
->jitter_lead
= 0;
1495 rtp_session
->consecutive_flaws
= 0;
1496 rtp_session
->stats
.inbound
.last_flaw
= 0;
1499 static void check_jitter(switch_rtp_t
*rtp_session
)
1501 switch_time_t current_time
;
1502 int64_t diff_time
= 0, cur_diff
= 0;
1505 current_time
= switch_micro_time_now() / 1000;
1507 if (rtp_session
->flags
[SWITCH_RTP_FLAG_PAUSE
] || rtp_session
->flags
[SWITCH_RTP_FLAG_DTMF_ON
] || rtp_session
->dtmf_data
.in_digit_ts
) {
1508 reset_jitter_seq(rtp_session
);
1512 if (++rtp_session
->jitter_lead
< JITTER_LEAD_FRAMES
|| !rtp_session
->stats
.inbound
.last_proc_time
) {
1513 rtp_session
->stats
.inbound
.last_proc_time
= current_time
;
1517 diff_time
= (current_time
- rtp_session
->stats
.inbound
.last_proc_time
);
1518 seq
= (int)(uint16_t) ntohs((uint16_t) rtp_session
->last_rtp_hdr
.seq
);
1520 /* Burst and Packet Loss */
1521 rtp_session
->stats
.inbound
.recved
++;
1523 if (rtp_session
->stats
.inbound
.last_processed_seq
> 0 && seq
> (int)(rtp_session
->stats
.inbound
.last_processed_seq
+ 1)) {
1524 int lost
= (seq
- rtp_session
->stats
.inbound
.last_processed_seq
- 1);
1526 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
, "%s Got: %s seq %d but expected: %d lost: %d\n",
1527 rtp_session_name(rtp_session
),
1528 rtp_type(rtp_session
),
1530 (rtp_session
->stats
.inbound
.last_processed_seq
+ 1), lost
);
1531 rtp_session
->stats
.inbound
.last_loss
++;
1533 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
1534 switch_core_session_request_video_refresh(rtp_session
->session
);
1537 if (rtp_session
->stats
.inbound
.last_loss
> 0 && rtp_session
->stats
.inbound
.last_loss
< LOST_BURST_CAPTURE
) {
1538 rtp_session
->stats
.inbound
.loss
[rtp_session
->stats
.inbound
.last_loss
] += lost
;
1541 rtp_session
->bad_stream
++;
1542 rtp_session
->stats
.inbound
.flaws
+= lost
;
1544 if (rtp_session
->stats
.inbound
.error_log
) {
1545 rtp_session
->stats
.inbound
.error_log
->flaws
+= lost
;
1549 rtp_session
->stats
.inbound
.last_loss
= 0;
1552 rtp_session
->stats
.inbound
.last_processed_seq
= seq
;
1554 /* Burst and Packet Loss */
1556 if (current_time
> rtp_session
->next_stat_check_time
) {
1557 rtp_session
->next_stat_check_time
= current_time
+ 5000;
1558 burstr_calculate(rtp_session
->stats
.inbound
.loss
, rtp_session
->stats
.inbound
.recved
,
1559 &(rtp_session
->stats
.inbound
.burstrate
), &(rtp_session
->stats
.inbound
.lossrate
));
1560 do_mos(rtp_session
);
1562 do_mos(rtp_session
);
1565 if (rtp_session
->stats
.inbound
.last_loss
|| rtp_session
->bad_stream
) {
1566 if (rtp_session
->session
&& (!rtp_session
->stats
.inbound
.error_log
|| rtp_session
->stats
.inbound
.error_log
->stop
)) {
1567 struct error_period
*error
= switch_core_session_alloc(rtp_session
->session
, sizeof(*error
));
1568 error
->start
= switch_micro_time_now();
1569 error
->next
= rtp_session
->stats
.inbound
.error_log
;
1570 rtp_session
->stats
.inbound
.error_log
= error
;
1573 if (!rtp_session
->stats
.inbound
.last_loss
) {
1574 if (++rtp_session
->recovering_stream
> (rtp_session
->one_second
* 3)) {
1575 if (rtp_session
->session
&& rtp_session
->stats
.inbound
.error_log
) {
1576 rtp_session
->stats
.inbound
.error_log
->stop
= switch_micro_time_now();
1579 rtp_session
->bad_stream
= 0;
1582 rtp_session
->recovering_stream
= 0;
1583 rtp_session
->bad_stream
++;
1586 rtp_session
->recovering_stream
= 0;
1587 rtp_session
->clean_stream
++;
1591 if ( diff_time
< 0 ) {
1592 diff_time
= -diff_time
;
1595 rtp_session
->stats
.inbound
.jitter_n
++;
1596 rtp_session
->stats
.inbound
.jitter_add
+= diff_time
;
1598 if (rtp_session
->stats
.inbound
.mean_interval
) {
1599 cur_diff
= (int64_t)(diff_time
- rtp_session
->stats
.inbound
.mean_interval
);
1604 rtp_session
->stats
.inbound
.jitter_addsq
+= (cur_diff
* cur_diff
);
1605 rtp_session
->stats
.inbound
.last_proc_time
= current_time
;
1607 if (rtp_session
->stats
.inbound
.jitter_n
> 0) {
1610 rtp_session
->stats
.inbound
.mean_interval
= (double)rtp_session
->stats
.inbound
.jitter_add
/ (double)rtp_session
->stats
.inbound
.jitter_n
;
1612 if (!rtp_session
->old_mean
) {
1613 rtp_session
->old_mean
= rtp_session
->stats
.inbound
.mean_interval
;
1616 rtp_session
->stats
.inbound
.variance
= (double)rtp_session
->stats
.inbound
.jitter_addsq
/ (double)rtp_session
->stats
.inbound
.jitter_n
;
1618 //printf("CHECK %d +%ld +%ld %f %f\n", rtp_session->write_timer.samplecount, diff_time, (diff_time * diff_time), rtp_session->stats.inbound.mean_interval, rtp_session->stats.inbound.variance);
1620 ipdv
= rtp_session
->old_mean
- rtp_session
->stats
.inbound
.mean_interval
;
1622 if ( ipdv
> IPDV_THRESHOLD
) { /* It shows Increasing Delays */
1623 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_DEBUG3
, "Calculated Instantaneous Packet Delay Variation: %s packet %lf\n",
1624 rtp_type(rtp_session
), ipdv
);
1627 if ( rtp_session
->stats
.inbound
.variance
< rtp_session
->stats
.inbound
.min_variance
|| rtp_session
->stats
.inbound
.min_variance
== 0 ) {
1628 rtp_session
->stats
.inbound
.min_variance
= rtp_session
->stats
.inbound
.variance
;
1631 if ( rtp_session
->stats
.inbound
.variance
> rtp_session
->stats
.inbound
.max_variance
) {
1632 rtp_session
->stats
.inbound
.max_variance
= rtp_session
->stats
.inbound
.variance
;
1635 rtp_session
->old_mean
= rtp_session
->stats
.inbound
.mean_interval
;
1639 static void rtcp_generate_sender_info(switch_rtp_t
*rtp_session
, struct switch_rtcp_sender_info
*sr
){
1640 switch_core_session_t
*session
= switch_core_memory_pool_get_data(rtp_session
->pool
, "__session");
1642 uint32_t sec
, ntp_sec
, ntp_usec
;
1643 switch_time_exp_t now_hr
;
1644 now
= switch_micro_time_now();
1645 sec
= (uint32_t)(now
/1000000); /* convert to seconds */
1646 ntp_sec
= sec
+NTP_TIME_OFFSET
; /* convert to NTP seconds */
1647 sr
->ntp_msw
= htonl(ntp_sec
); /* store result in "most significant word" */
1648 ntp_usec
= (uint32_t)(now
- (sec
*1000000)); /* remove seconds to keep only the microseconds */
1649 sr
->ntp_lsw
= htonl((u_long
)(ntp_usec
*(double)(((uint64_t)1)<<32)*1.0e-6)); /* convert microseconds to fraction of 32bits and store result in "least significatn word" */
1651 sr
->ts
= htonl(rtp_session
->last_write_ts
);
1652 sr
->pc
= htonl(rtp_session
->stats
.outbound
.packet_count
);
1653 sr
->oc
= htonl(rtp_session
->stats
.outbound
.raw_bytes
- rtp_session
->stats
.outbound
.packet_count
* sizeof(srtp_hdr_t
));
1655 switch_time_exp_gmt(&now_hr
,now
);
1656 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG10
,"Sending an RTCP packet[%04d-%02d-%02d %02d:%02d:%02d.%d] lsr[%u] msw[%u] lsw[%u] stats_ssrc[%u] packet_count[%u] OC[%u]\n",
1657 1900 + now_hr
.tm_year
, now_hr
.tm_mday
, now_hr
.tm_mon
, now_hr
.tm_hour
, now_hr
.tm_min
, now_hr
.tm_sec
, now_hr
.tm_usec
,
1658 (ntohl(sr
->ntp_lsw
)&0xffff0000)>>16 | (ntohl(sr
->ntp_msw
)&0x0000ffff)<<16,
1659 ntohl(sr
->ntp_msw
),ntohl(sr
->ntp_lsw
), rtp_session
->stats
.rtcp
.ssrc
, ntohl(sr
->pc
), ntohl(sr
->oc
)
1663 static inline uint32_t calc_local_lsr_now()
1666 uint32_t ntp_sec
, ntp_usec
, lsr_now
, sec
;
1667 now
= switch_micro_time_now();
1668 sec
= (uint32_t)(now
/1000000); /* convert to seconds */
1669 ntp_sec
= sec
+NTP_TIME_OFFSET
; /* convert to NTP seconds */
1670 ntp_usec
= (uint32_t)(now
- ((switch_time_t
) sec
*1000000)); /* remove seconds to keep only the microseconds */
1672 lsr_now
= (uint32_t)(ntp_usec
*0.065536) | (ntp_sec
&0x0000ffff)<<16; /* 0.065536 is used for convertion from useconds to fraction of 65536 (x65536/1000000) */
1677 //#define DEBUG_RTCP
1678 /* extra param is for duplicates (received NACKed packets) */
1679 static void rtcp_generate_report_block(switch_rtp_t
*rtp_session
, struct switch_rtcp_report_block
*rtcp_report_block
,
1680 int16_t extra_expected
)
1683 switch_core_session_t
*session
= switch_core_memory_pool_get_data(rtp_session
->pool
, "__session");
1685 switch_rtcp_numbers_t
* stats
=&rtp_session
->stats
.rtcp
;
1686 uint32_t expected_pkt
, dlsr
= 0;
1690 if (stats
->rtcp_rtp_count
== 0) {
1691 expected_pkt
= stats
->high_ext_seq_recv
- stats
->base_seq
+ 1;
1693 expected_pkt
= stats
->high_ext_seq_recv
- stats
->last_rpt_ext_seq
+ extra_expected
;
1696 pkt_lost
= expected_pkt
- stats
->period_pkt_count
;
1697 if (pkt_lost
< 0) pkt_lost
= 0;
1699 stats
->cum_lost
=stats
->cum_lost
+pkt_lost
;
1700 if (expected_pkt
> 0 && pkt_lost
> 0) {
1701 rtcp_report_block
->fraction
= (pkt_lost
== expected_pkt
? 255 : (uint8_t) (pkt_lost
* 256 / expected_pkt
)); /* if X packets were expected and X was lost, we want 0xff to be reported, not 0 */
1703 rtcp_report_block
->fraction
= 0;
1705 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
1706 rtcp_report_block
->lost
= stats
->cum_lost
;
1708 /* Reversing byte order for 24bits */
1709 rtcp_report_block
->lost
= htonl(stats
->cum_lost
) >> 8;
1713 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
])
1714 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_CRIT
, "rtcp_generate_sr: stats_ssrc[%u]\nreceived[%d]\nexpected[%d]\ncum[%d]\nlost[%d|%d/256]pkt\nlast_seq[%d]\ncyc[%d]\nlast_rpt_seq[%d]\ncyc[%d]\nssrc[%d]\n",
1715 rtp_session
->remote_ssrc
, stats
->period_pkt_count
, expected_pkt
,
1716 stats
->cum_lost
, pkt_lost
, rtcp_report_block
->fraction
, stats
->high_ext_seq_recv
&0x0000ffff,
1717 stats
->cycle
, stats
->last_rpt_ext_seq
&0x0000ffff, stats
->last_rpt_cycle
, rtp_session
->stats
.rtcp
.peer_ssrc
1720 rtcp_report_block
->highest_sequence_number_received
= htonl(stats
->high_ext_seq_recv
);
1723 rtcp_report_block
->jitter
= htonl((uint32_t)stats
->inter_jitter
);
1725 /* Delay since Last Sender Report (DLSR) : 32bits, 1/65536 seconds */
1726 if (stats
->last_recv_lsr_local
) {
1727 uint32_t lsr_now
= calc_local_lsr_now();
1728 /* check lsr_now: what we just read from clock may be in the past (race cond), don't send huge dlsr due to uint wrap around */
1729 if (lsr_now
> stats
->last_recv_lsr_local
) {
1730 dlsr
= lsr_now
- stats
->last_recv_lsr_local
;
1733 rtcp_report_block
->lsr
= stats
->last_recv_lsr_peer
;
1734 rtcp_report_block
->dlsr
= htonl(dlsr
);
1735 if (rtp_session
->stats
.rtcp
.peer_ssrc
) {
1736 rtcp_report_block
->ssrc
= htonl(rtp_session
->stats
.rtcp
.peer_ssrc
);
1738 /* if remote is not sending rtcp reports, take ssrc as assigned from rtp */
1739 rtcp_report_block
->ssrc
= htonl(rtp_session
->remote_ssrc
);
1742 stats
->rtcp_rtp_count
++;
1745 static void rtcp_stats_init(switch_rtp_t
*rtp_session
)
1747 switch_rtcp_numbers_t
* stats
= &rtp_session
->stats
.rtcp
;
1748 srtp_hdr_t
* hdr
= &rtp_session
->last_rtp_hdr
;
1749 switch_core_session_t
*session
= switch_core_memory_pool_get_data(rtp_session
->pool
, "__session");
1750 stats
->ssrc
= ntohl(hdr
->ssrc
);
1751 stats
->last_rpt_ts
= rtp_session
->write_timer
.samplecount
;
1753 stats
->last_rpt_ext_seq
= 0;
1754 stats
->last_rpt_cycle
= 0;
1755 stats
->last_pkt_tsdiff
= 0;
1756 stats
->inter_jitter
= 0;
1758 stats
->high_ext_seq_recv
= ntohs((uint16_t)hdr
->seq
);
1759 stats
->base_seq
= ntohs((uint16_t)hdr
->seq
);
1760 stats
->bad_seq
= (1<<16) + 1; /* Make sure we wont missmatch 2 consecutive packets, so seq == bad_seq is false */
1761 stats
->cum_lost
= 0;
1762 stats
->period_pkt_count
= 0;
1763 stats
->sent_pkt_count
= 0;
1764 stats
->pkt_count
= 0;
1765 stats
->rtcp_rtp_count
= 0;
1767 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
1768 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "rtcp_stats_init: %s rtcp disabled\n", rtp_type(rtp_session
));
1769 } else if (!rtp_session
->rtcp_sock_output
) {
1770 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
, "rtcp_stats_init: %s no rtcp socket\n", rtp_type(rtp_session
));
1771 } else if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_PASSTHRU
]) {
1772 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "rtcp_stats_init: %s rtcp passthru\n", rtp_type(rtp_session
));
1774 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "rtcp_stats_init: %s ssrc[%u] base_seq[%u]\n", rtp_type(rtp_session
), stats
->ssrc
, stats
->base_seq
);
1777 if (rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
] && (switch_core_media_codec_get_cap(rtp_session
->session
,
1778 SWITCH_MEDIA_TYPE_AUDIO
, SWITCH_CODEC_FLAG_HAS_ADJ_BITRATE
))) {
1779 kalman_estimator_t
*estimators
[KALMAN_SYSTEM_MODELS
];
1780 cusum_kalman_detector_t
*detectors
[KALMAN_SYSTEM_MODELS
];
1782 rtp_session
->flags
[SWITCH_RTP_FLAG_ADJ_BITRATE_CAP
] = 1;
1783 rtp_session
->flags
[SWITCH_RTP_FLAG_ESTIMATORS
] = 1;
1785 rtp_session
->estimators
[EST_LOSS
] = switch_core_alloc(rtp_session
->pool
, sizeof(*estimators
[0]));
1786 switch_kalman_init(rtp_session
->estimators
[EST_LOSS
],0.1,0.1);
1787 rtp_session
->estimators
[EST_RTT
] = switch_core_alloc(rtp_session
->pool
, sizeof(*estimators
[0]));
1788 switch_kalman_init(rtp_session
->estimators
[EST_RTT
],0.03,1);
1789 rtp_session
->detectors
[EST_RTT
] = switch_core_alloc(rtp_session
->pool
, sizeof(*detectors
[0]));
1790 switch_kalman_cusum_init(rtp_session
->detectors
[EST_RTT
],0.005,0.5);
1791 rtp_session
->detectors
[EST_LOSS
] = switch_core_alloc(rtp_session
->pool
, sizeof(*detectors
[0]));
1792 switch_kalman_cusum_init(rtp_session
->detectors
[EST_LOSS
], 0.5, 1);
1796 static int rtcp_stats(switch_rtp_t
*rtp_session
)
1798 switch_core_session_t
*session
= switch_core_memory_pool_get_data(rtp_session
->pool
, "__session");
1799 srtp_hdr_t
* hdr
= &rtp_session
->last_rtp_hdr
;
1800 switch_rtcp_numbers_t
* stats
= &rtp_session
->stats
.rtcp
;
1801 uint32_t packet_spacing_diff
= 0, pkt_tsdiff
, pkt_extended_seq
;
1802 uint16_t pkt_seq
, seq_diff
, max_seq
;
1803 const int MAX_DROPOUT
= 3000;
1804 const int MAX_MISORDER
= 100;
1805 const int RTP_SEQ_MOD
= (1<<16);
1807 if(!rtp_session
->rtcp_sock_output
|| !rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
] || rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_PASSTHRU
] || !rtp_session
->rtcp_interval
)
1808 return 0; /* do not process RTCP in current state */
1810 pkt_seq
= (uint16_t) ntohs((uint16_t) rtp_session
->last_rtp_hdr
.seq
);
1812 /* Detect sequence number cycle change */
1813 max_seq
= stats
->high_ext_seq_recv
&0x0000ffff;
1814 seq_diff
= pkt_seq
- max_seq
;
1816 if (seq_diff
< MAX_DROPOUT
) { /* in order, with permissible gap */
1817 if (pkt_seq
< max_seq
) {
1819 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "rtcp_stats:[cycle change] pkt_seq[%d] cycle[%d] max_seq[%d] stats_ssrc[%u] local_ts[%u]\n",
1820 pkt_seq
, stats
->cycle
, max_seq
, stats
->ssrc
, rtp_session
->timer
.samplecount
);
1822 pkt_extended_seq
= stats
->cycle
<< 16 | pkt_seq
; /* getting the extended packet extended sequence ID */
1823 if (pkt_extended_seq
> stats
->high_ext_seq_recv
) {
1824 stats
->high_ext_seq_recv
= pkt_extended_seq
;
1827 else if (seq_diff
<= (RTP_SEQ_MOD
- MAX_MISORDER
)) { /* the sequence number made a very large jump */
1828 if (pkt_seq
== stats
->bad_seq
) {
1829 rtcp_stats_init(rtp_session
);
1831 stats
->bad_seq
= (pkt_seq
+ 1) & (RTP_SEQ_MOD
-1);
1833 return 0; /* no stats, packet is out of sync and will be accounted as lost */
1835 /* duplicate or reordered packet */
1838 /* Verify that we are on the same stream source (we do not support multiple sources) */
1839 if (ntohl(hdr
->ssrc
) != stats
->ssrc
|| !stats
->init
) {
1840 rtcp_stats_init(rtp_session
);
1843 stats
->period_pkt_count
++;
1846 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG10
, "rtcp_stats: period_pkt_count[%d]last_seq[%d]cycle[%d]stats_ssrc[%u]local_ts[%u]\n",
1847 stats
->period_pkt_count
, pkt_seq
, stats
->cycle
, stats
->ssrc
, rtp_session
->write_timer
.samplecount
);
1849 /* Interarrival jitter calculation */
1850 pkt_tsdiff
= abs((int32_t)(rtp_session
->timer
.samplecount
- ntohl(hdr
->ts
))); /* relative transit times for this packet */
1851 if (stats
->pkt_count
< 2) { /* Can not compute Jitter with only one packet */
1852 stats
->last_pkt_tsdiff
= pkt_tsdiff
;
1854 /* Jitter : difference of relative transit times for the two packets */
1855 packet_spacing_diff
= abs((int32_t)(pkt_tsdiff
- stats
->last_pkt_tsdiff
));
1856 stats
->last_pkt_tsdiff
= pkt_tsdiff
;
1857 /* Interarrival jitter estimation, "J(i) = J(i-1) + ( |D(i-1,i)| - J(i-1) )/16" */
1858 stats
->inter_jitter
= (stats
->inter_jitter
+ (((double)packet_spacing_diff
- stats
->inter_jitter
) /16.));
1862 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG10
, "rtcp_stats: pkt_ts[%d]local_ts[%d]diff[%d]pkt_spacing[%d]inter_jitter[%f]seq[%d]stats_ssrc[%d]",
1863 ntohl(hdr
->ts
), rtp_session
->timer
.samplecount
, pkt_tsdiff
, packet_spacing_diff
, stats
->inter_jitter
, ntohs(hdr
->seq
), stats
->ssrc
);
1868 static void calc_bw_exp(uint32_t bps
, uint8_t bits
, rtcp_tmmbx_t
*tmmbx
)
1870 uint32_t mantissa_max
, i
= 0;
1872 uint32_t mantissa
= 0;
1873 uint16_t overhead
= 60;
1875 switch_assert(bits
<=32);
1877 mantissa_max
= (1 << bits
) - 1;
1879 for (i
= 0; i
< 32; ++i
) {
1880 if (bps
<= (mantissa_max
<< i
)) {
1886 mantissa
= (bps
>> exp
);
1888 tmmbx
->parts
[0] = (uint8_t) ((exp
<< 2) + ((mantissa
>> 15) & 0x03));
1889 tmmbx
->parts
[1] = (uint8_t) (mantissa
>> 7);
1890 tmmbx
->parts
[2] = (uint8_t) ((mantissa
>> 1) + ((overhead
>> 8) & 0x01));
1891 tmmbx
->parts
[3] = (uint8_t) (overhead
);
1894 static int using_ice(switch_rtp_t
*rtp_session
)
1896 if (rtp_session
->ice
.ice_user
|| rtp_session
->rtcp_ice
.ice_user
) {
1903 static void switch_send_rtcp_event(switch_rtp_t
*rtp_session
,struct switch_rtcp_sender_report
*sr
,struct switch_rtcp_report_block
*rtcp_report_block
)
1905 if (sr
&& rtcp_report_block
) {
1906 switch_event_t
*event
;
1908 if (switch_event_create(&event
, SWITCH_EVENT_SEND_RTCP_MESSAGE
) == SWITCH_STATUS_SUCCESS
) {
1912 char *uuid
= switch_core_session_get_uuid(rtp_session
->session
);
1914 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, "Unique-ID", switch_core_session_get_uuid(rtp_session
->session
));
1917 snprintf(value
, sizeof(value
), "%.8x", rtp_session
->stats
.rtcp
.ssrc
);
1918 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, "SSRC", value
);
1920 snprintf(value
, sizeof(value
), "%u", ntohl(sr
->sender_info
.ntp_msw
));
1921 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, "NTP-Most-Significant-Word", value
);
1923 snprintf(value
, sizeof(value
), "%u", ntohl(sr
->sender_info
.ntp_lsw
));
1924 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, "NTP-Least-Significant-Word", value
);
1926 snprintf(value
, sizeof(value
), "%u", ntohl(sr
->sender_info
.ts
));
1927 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, "RTP-Timestamp", value
);
1929 snprintf(value
, sizeof(value
), "%u", ntohl(sr
->sender_info
.pc
));
1930 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, "Sender-Packet-Count", value
);
1932 snprintf(value
, sizeof(value
), "%u", ntohl(sr
->sender_info
.oc
));
1933 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, "Octect-Packet-Count", value
);
1935 snprintf(value
, sizeof(value
), "%u", ntohl(sr
->sender_info
.ts
));
1936 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, "Last-RTP-Timestamp", value
);
1938 snprintf(value
, sizeof(value
), "%" SWITCH_TIME_T_FMT
, switch_time_now());
1939 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, "Capture-Time", value
);
1941 /* Add sources info */
1942 snprintf(header
, sizeof(header
), "Source-SSRC");
1943 snprintf(value
, sizeof(value
), "%.8x", rtp_session
->stats
.rtcp
.peer_ssrc
);
1944 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, header
, value
);
1945 snprintf(header
, sizeof(header
), "Source-Fraction");
1946 snprintf(value
, sizeof(value
), "%u", (uint8_t)rtcp_report_block
->fraction
);
1947 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, header
, value
);
1948 snprintf(header
, sizeof(header
), "Source-Lost");
1949 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
1950 tmpLost
= rtcp_report_block
->lost
; /* signed 24bit will extended signess to int32_t automatically */
1952 tmpLost
= ntohl(rtcp_report_block
->lost
)>>8;
1953 tmpLost
= tmpLost
| ((tmpLost
& 0x00800000) ? 0xff000000 : 0x00000000); /* ...and signess compensation */
1955 snprintf(value
, sizeof(value
), "%u", tmpLost
);
1956 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, header
, value
);
1957 snprintf(header
, sizeof(header
), "Source-Highest-Sequence-Number-Received");
1958 snprintf(value
, sizeof(value
), "%u", ntohl(rtcp_report_block
->highest_sequence_number_received
));
1959 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, header
, value
);
1960 snprintf(header
, sizeof(header
), "Source-Jitter");
1961 snprintf(value
, sizeof(value
), "%u", ntohl(rtcp_report_block
->jitter
));
1962 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, header
, value
);
1963 snprintf(header
, sizeof(header
), "Source-LSR");
1964 snprintf(value
, sizeof(value
), "%u", ntohl(rtcp_report_block
->lsr
));
1965 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, header
, value
);
1966 snprintf(header
, sizeof(header
), "Source-DLSR");
1967 snprintf(value
, sizeof(value
), "%u", ntohl(rtcp_report_block
->dlsr
));
1968 switch_event_add_header_string(event
, SWITCH_STACK_BOTTOM
, header
, value
);
1970 switch_event_fire(&event
);
1971 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG10
, "Dispatched RTCP SEND event\n");
1977 static int check_rtcp_and_ice(switch_rtp_t
*rtp_session
)
1980 int rtcp_ok
= 0, rtcp_cyclic
= 0, rtcp_fb
= 0, force_send_rr
= 0;
1981 switch_time_t now
= switch_micro_time_now();
1982 int rate
= 0, nack_ttl
= 0, nack_dup
= 0;
1983 uint32_t cur_nack
[MAX_NACK
] = { 0 };
1986 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
] &&
1987 rtp_session
->flags
[SWITCH_RTP_FLAG_AUTO_CNG
] &&
1988 rtp_session
->send_msg
.header
.ts
&&
1989 rtp_session
->cng_pt
!= INVALID_PT
&&
1990 (rtp_session
->write_timer
.samplecount
- rtp_session
->last_write_samplecount
>= rtp_session
->samples_per_interval
* 60)) {
1991 uint8_t data
[10] = { 0 };
1992 switch_frame_flag_t frame_flags
= SFF_NONE
;
1996 get_next_write_ts(rtp_session
, 0);
1997 rtp_session
->send_msg
.header
.ts
= htonl(rtp_session
->ts
);
1999 switch_rtp_write_manual(rtp_session
, (void *) data
, 2, 0, rtp_session
->cng_pt
, ntohl(rtp_session
->send_msg
.header
.ts
), &frame_flags
);
2001 if (switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_USE_TIMER
)) {
2002 rtp_session
->last_write_samplecount
= rtp_session
->write_timer
.samplecount
;
2006 rate
= rtp_session
->rtcp_interval
;
2008 if (rtp_session
->flags
[SWITCH_RTP_FLAG_NACK
] && rtp_session
->vb
) {
2010 for (n
= 0; n
< MAX_NACK
; n
++) {
2011 uint32_t nack
= switch_jb_pop_nack(rtp_session
->vb
);
2015 seq
= ntohs(nack
& 0xFFFF);
2017 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
, "%s Got NACK [%u][0x%x] for seq %u\n",
2018 switch_core_session_get_name(rtp_session
->session
), nack
, nack
, seq
);
2020 cur_nack
[nack_ttl
++] = nack
;
2030 if (rtp_session
->rtcp_sent_packets
< 4) {
2033 if (rtp_session
->pli_count
|| rtp_session
->fir_count
|| rtp_session
->tmmbr
|| rtp_session
->tmmbn
) {
2034 //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "MARK BW/FIR ETC %d %d\n", rtp_session->pli_count, rtp_session->fir_count);
2040 if (rtp_session
->send_rr
) {
2041 rtp_session
->send_rr
= 0;
2046 //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "TIME CHECK %d > %d\n", (int)((now - rtp_session->rtcp_last_sent) / 1000), rate);
2048 if (!rtcp_ok
&& (!rtp_session
->rtcp_last_sent
|| (int)((now
- rtp_session
->rtcp_last_sent
) / 1000) > rate
)) {
2049 //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "TIME UP\n");
2054 if (rtcp_ok
&& using_ice(rtp_session
)) {
2055 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
2056 if (!rtp_session
->ice
.rready
) {
2060 if (!rtp_session
->rtcp_ice
.rready
) {
2066 //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "WTF %d %d %d %d\n", rate, rtp_session->rtcp_sent_packets, rtcp_ok, nack_ttl);
2068 if (rtp_session
->rtcp_sock_output
&& rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
] && !rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_PASSTHRU
] && rtcp_ok
) {
2069 switch_rtcp_numbers_t
* stats
= &rtp_session
->stats
.rtcp
;
2070 struct switch_rtcp_receiver_report
*rr
;
2071 struct switch_rtcp_sender_report
*sr
;
2072 struct switch_rtcp_report_block
*rtcp_report_block
= NULL
;
2073 switch_size_t rtcp_bytes
= sizeof(struct switch_rtcp_hdr_s
)+sizeof(uint32_t); /* add size of the packet header and the ssrc */
2074 switch_rtcp_hdr_t
*sdes
;
2076 switch_size_t sdes_bytes
= sizeof(struct switch_rtcp_hdr_s
);
2078 switch_rtcp_sdes_unit_t
*unit
;
2079 switch_bool_t is_only_receiver
= FALSE
;
2082 rtp_session
->rtcp_last_sent
= now
;
2083 rtp_session
->rtcp_sent_packets
++;
2086 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] &&
2087 rtp_session
->vb
&& rtcp_cyclic
) {
2088 nack_dup
= rtp_session
->prev_nacks_inflight
;
2089 rtp_session
->prev_nacks_inflight
= 0;
2092 rtp_session
->rtcp_send_msg
.header
.version
= 2;
2093 rtp_session
->rtcp_send_msg
.header
.p
= 0;
2095 if ((switch_core_session_media_flow(rtp_session
->session
, SWITCH_MEDIA_TYPE_AUDIO
) == SWITCH_MEDIA_FLOW_RECVONLY
) ||
2096 switch_core_session_media_flow(rtp_session
->session
, SWITCH_MEDIA_TYPE_VIDEO
) == SWITCH_MEDIA_FLOW_RECVONLY
) {
2097 is_only_receiver
= TRUE
;
2099 if (!rtp_session
->stats
.rtcp
.sent_pkt_count
|| is_only_receiver
|| force_send_rr
) {
2100 rtp_session
->rtcp_send_msg
.header
.type
= _RTCP_PT_RR
; /* Receiver report */
2101 rr
=(struct switch_rtcp_receiver_report
*) rtp_session
->rtcp_send_msg
.body
;
2102 rr
->ssrc
= htonl(rtp_session
->ssrc
);
2103 rtcp_report_block
= &rr
->report_block
;
2104 rtcp_bytes
+= sizeof(struct switch_rtcp_report_block
);
2105 rtcp_generate_report_block(rtp_session
, rtcp_report_block
, nack_dup
);
2106 rtp_session
->rtcp_send_msg
.header
.count
= 1; /* reception report block count */
2107 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
, "Sending RTCP RR (ssrc=%u)\n", rtp_session
->ssrc
);
2109 struct switch_rtcp_sender_info
*rtcp_sender_info
;
2110 rtp_session
->rtcp_send_msg
.header
.type
= _RTCP_PT_SR
; /* Sender report */
2111 sr
= (struct switch_rtcp_sender_report
*) rtp_session
->rtcp_send_msg
.body
;
2112 sr
->ssrc
= htonl(rtp_session
->ssrc
);
2113 rtcp_sender_info
= &sr
->sender_info
;
2114 rtcp_generate_sender_info(rtp_session
, rtcp_sender_info
);
2115 rtcp_bytes
+= sizeof(struct switch_rtcp_sender_info
);
2116 if (!rtcp_cyclic
&& rtcp_fb
) {
2117 /* rtcp-fb only, don't send receive report block */
2118 rtp_session
->rtcp_send_msg
.header
.count
= 0;
2120 rtcp_report_block
= &sr
->report_block
;
2121 rtcp_bytes
+= sizeof(struct switch_rtcp_report_block
);
2122 rtcp_generate_report_block(rtp_session
, rtcp_report_block
, nack_dup
);
2123 rtp_session
->rtcp_send_msg
.header
.count
= 1; /* reception report block count */
2124 stats
->sent_pkt_count
= 0;
2125 if ((!rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && rtp_session
->flags
[SWITCH_RTP_FLAG_AUDIO_FIRE_SEND_RTCP_EVENT
]) ||
2126 (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO_FIRE_SEND_RTCP_EVENT
])) {
2127 switch_send_rtcp_event(rtp_session
, sr
, rtcp_report_block
);
2130 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
, "Sending RTCP SR (ssrc=%u)\n", rtp_session
->ssrc
);
2133 rtp_session
->rtcp_send_msg
.header
.length
= htons((uint16_t)(rtcp_bytes
/ 4) - 1);
2135 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
2136 if (rtp_session
->pli_count
) {
2137 switch_rtcp_ext_hdr_t
*ext_hdr
;
2139 p
= (uint8_t *) (&rtp_session
->rtcp_send_msg
) + rtcp_bytes
;
2140 ext_hdr
= (switch_rtcp_ext_hdr_t
*) p
;
2142 ext_hdr
->version
= 2;
2144 ext_hdr
->fmt
= _RTCP_PSFB_PLI
;
2145 ext_hdr
->pt
= _RTCP_PT_PSFB
;
2147 ext_hdr
->send_ssrc
= htonl(rtp_session
->ssrc
);
2148 ext_hdr
->recv_ssrc
= htonl(rtp_session
->remote_ssrc
);
2149 rtp_session
->rtcp_vstats
.video_in
.pli_count
++;
2150 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
, "Sending RTCP PLI %u %u [%u]\n",
2151 rtp_session
->ssrc
, rtp_session
->remote_ssrc
, rtp_session
->rtcp_vstats
.video_in
.pli_count
);
2153 ext_hdr
->length
= htons((uint8_t)(sizeof(switch_rtcp_ext_hdr_t
) / 4) - 1);
2154 rtcp_bytes
+= sizeof(switch_rtcp_ext_hdr_t
);
2155 rtp_session
->pli_count
= 0;
2158 if (rtp_session
->flags
[SWITCH_RTP_FLAG_NACK
] && nack_ttl
> 0) {
2161 rtp_session
->rtcp_vstats
.video_in
.nack_count
++;
2162 for (n
= 0; n
< nack_ttl
; n
++) {
2163 switch_rtcp_ext_hdr_t
*ext_hdr
;
2165 p
= (uint8_t *) (&rtp_session
->rtcp_send_msg
) + rtcp_bytes
;
2166 ext_hdr
= (switch_rtcp_ext_hdr_t
*) p
;
2168 ext_hdr
->version
= 2;
2170 ext_hdr
->fmt
= _RTCP_RTPFB_NACK
;
2171 ext_hdr
->pt
= _RTCP_PT_RTPFB
;
2172 ext_hdr
->send_ssrc
= htonl(rtp_session
->ssrc
);
2173 ext_hdr
->recv_ssrc
= htonl(rtp_session
->remote_ssrc
);
2174 ext_hdr
->length
= htons(3);
2175 p
+= sizeof(switch_rtcp_ext_hdr_t
);
2176 nack
= (uint32_t *) p
;
2177 *nack
= cur_nack
[n
];
2179 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "Sending RTCP NACK %u [%d]\n",
2180 ntohs(*nack
& 0xFFFF), rtp_session
->rtcp_vstats
.video_in
.nack_count
);
2182 rtcp_bytes
+= sizeof(switch_rtcp_ext_hdr_t
) + sizeof(cur_nack
[n
]);
2185 rtp_session
->prev_nacks_inflight
= n
;
2188 if (rtp_session
->fir_count
) {
2189 switch_rtcp_ext_hdr_t
*ext_hdr
;
2192 if (switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_OLD_FIR
)) {
2193 p
= (uint8_t *) (&rtp_session
->rtcp_send_msg
) + rtcp_bytes
;
2194 ext_hdr
= (switch_rtcp_ext_hdr_t
*) p
;
2196 ext_hdr
->version
= 2;
2197 ext_hdr
->pt
= _RTCP_PT_FIR
;
2198 rtcp_bytes
+= sizeof(switch_rtcp_ext_hdr_t
);
2202 p
= (uint8_t *) (&rtp_session
->rtcp_send_msg
) + rtcp_bytes
;
2203 ext_hdr
= (switch_rtcp_ext_hdr_t
*) p
;
2205 p
+= sizeof(switch_rtcp_ext_hdr_t
);
2206 fir
= (rtcp_fir_t
*) p
;
2208 ext_hdr
->version
= 2;
2210 ext_hdr
->fmt
= _RTCP_PSFB_FIR
;
2211 ext_hdr
->pt
= _RTCP_PT_PSFB
;
2213 ext_hdr
->send_ssrc
= htonl(rtp_session
->ssrc
);
2214 ext_hdr
->recv_ssrc
= htonl(rtp_session
->remote_ssrc
);
2216 fir
->ssrc
= htonl(rtp_session
->remote_ssrc
);
2217 fir
->seq
= rtp_session
->fir_seq
;
2218 fir
->r1
= fir
->r2
= fir
->r3
= 0;
2220 rtp_session
->rtcp_vstats
.video_in
.fir_count
++;
2221 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "Sending RTCP FIR SEQ %d [%u]\n", rtp_session
->fir_seq
, rtp_session
->rtcp_vstats
.video_in
.fir_count
);
2223 rtp_session
->fir_seq
++;
2225 ext_hdr
->length
= htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t
) + sizeof(rtcp_fir_t
)) / 4) - 1);
2226 rtcp_bytes
+= sizeof(switch_rtcp_ext_hdr_t
) + sizeof(rtcp_fir_t
);
2227 rtp_session
->fir_count
= 0;
2230 //if (!rtp_session->tmmbr && rtp_session->cur_tmmbr) {
2231 // rtp_session->tmmbr = rtp_session->cur_tmmbr;
2234 while (rtp_session
->tmmbr
|| rtp_session
->tmmbn
) {
2235 switch_rtcp_ext_hdr_t
*ext_hdr
;
2236 rtcp_tmmbx_t
*tmmbx
;
2238 p
= (uint8_t *) (&rtp_session
->rtcp_send_msg
) + rtcp_bytes
;
2239 ext_hdr
= (switch_rtcp_ext_hdr_t
*) p
;
2241 p
+= sizeof(switch_rtcp_ext_hdr_t
);
2242 tmmbx
= (rtcp_tmmbx_t
*) p
;
2244 ext_hdr
->version
= 2;
2246 ext_hdr
->pt
= _RTCP_PT_RTPFB
;
2247 ext_hdr
->send_ssrc
= htonl(rtp_session
->ssrc
);
2248 ext_hdr
->recv_ssrc
= 0;
2250 if (rtp_session
->tmmbr
) {
2251 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "Sending RTCP TMMBR %u\n", rtp_session
->tmmbr
);
2252 ext_hdr
->fmt
= _RTCP_RTPFB_TMMBR
;
2253 bps
= rtp_session
->tmmbr
;
2254 rtp_session
->tmmbr
= 0;
2256 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "Sending RTCP TMMBN %u\n", rtp_session
->tmmbr
);
2257 ext_hdr
->fmt
= _RTCP_RTPFB_TMMBN
;
2258 bps
= rtp_session
->tmmbn
;
2259 rtp_session
->tmmbn
= 0;
2262 tmmbx
->ssrc
= htonl(rtp_session
->remote_ssrc
);
2263 calc_bw_exp(bps
, 17, tmmbx
);
2265 ext_hdr
->length
= htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t
) + sizeof(rtcp_tmmbx_t
)) / 4) - 1);
2266 rtcp_bytes
+= sizeof(switch_rtcp_ext_hdr_t
) + sizeof(rtcp_tmmbx_t
);
2272 p
= (uint8_t *) (&rtp_session
->rtcp_send_msg
) + rtcp_bytes
;
2273 sdes
= (switch_rtcp_hdr_t
*) p
;
2275 sdes
->type
= _RTCP_PT_SDES
;
2278 p
= (uint8_t *) (sdes
) + sdes_bytes
;
2279 ssrc
= (uint32_t *) p
;
2280 *ssrc
= htonl(rtp_session
->ssrc
);
2281 sdes_bytes
+= sizeof(uint32_t);
2284 p
= (uint8_t *) (sdes
) + sdes_bytes
;
2285 unit
= (switch_rtcp_sdes_unit_t
*) p
;
2286 unit
->type
= _RTCP_SDES_CNAME
;
2287 snprintf((char *)unit
->value
, 80, "%x", rtp_session
->ssrc
);
2288 unit
->length
= strlen((char *)unit
->value
);
2289 sdes_bytes
+= sizeof(switch_rtcp_sdes_unit_t
) + unit
->length
;
2292 p
+= sizeof(switch_rtcp_sdes_unit_t
) + unit
->length
;
2293 unit
= (switch_rtcp_sdes_unit_t
*) p
;
2294 unit
->type
= _RTCP_SDES_NOTE
;
2295 snprintf((char *)unit
->value
, 80, "FreeSWITCH.org -- Come to ClueCon.com");
2296 unit
->length
= strlen((char *)unit
->value
);
2297 sdes_bytes
+= sizeof(switch_rtcp_sdes_unit_t
) + unit
->length
;
2301 sdes_bytes
+= 4 - (sdes_bytes
% 4);
2303 sdes
->length
= htons((uint16_t)(sdes_bytes
/ 4) - 1);
2304 rtcp_bytes
+= sdes_bytes
;
2306 /* Prepare next report */
2307 if (rtp_session
->rtcp_send_msg
.header
.count
) {
2308 stats
->last_rpt_cycle
= stats
->cycle
;
2309 stats
->last_rpt_ext_seq
= stats
->high_ext_seq_recv
;
2310 stats
->last_rpt_ts
= rtp_session
->write_timer
.samplecount
;
2311 stats
->period_pkt_count
= 0;
2317 switch_mutex_lock(rtp_session
->ice_mutex
);
2318 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND
]) {
2320 int sbytes
= (int) rtcp_bytes
;
2322 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND_MKI
]) {
2323 stat
= srtp_protect_rtcp(rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtcp
], &rtp_session
->rtcp_send_msg
.header
, &sbytes
);
2325 stat
= srtp_protect_rtcp_mki(rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtcp
], &rtp_session
->rtcp_send_msg
.header
, &sbytes
, 1, SWITCH_CRYPTO_MKI_INDEX
);
2329 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Error: SRTP RTCP protection failed with code %d\n", stat
);
2330 switch_mutex_unlock(rtp_session
->ice_mutex
);
2333 rtcp_bytes
= sbytes
;
2336 switch_mutex_unlock(rtp_session
->ice_mutex
);
2339 //#define DEBUG_EXTRA
2342 const char *old_host
;
2344 old_host
= switch_get_addr(bufb
, sizeof(bufb
), rtp_session
->rtcp_remote_addr
);
2345 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_CRIT
, "%s SEND %s RTCP %s:%d %ld\n",
2346 rtp_session_name(rtp_session
),
2347 rtp_type(rtp_session
),
2349 switch_sockaddr_get_port(rtp_session
->rtcp_remote_addr
),
2353 if (switch_socket_sendto(rtp_session
->rtcp_sock_output
, rtp_session
->rtcp_remote_addr
, 0, (void *)&rtp_session
->rtcp_send_msg
, &rtcp_bytes
) != SWITCH_STATUS_SUCCESS
) {
2354 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
,"RTCP packet not written\n");
2356 rtp_session
->stats
.inbound
.period_packet_count
= 0;
2360 if (rtp_session
->ice
.ice_user
) {
2361 if (ice_out(rtp_session
, &rtp_session
->ice
) == SWITCH_STATUS_GENERR
) {
2367 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
2368 if (rtp_session
->rtcp_ice
.ice_user
) {
2369 if (ice_out(rtp_session
, &rtp_session
->rtcp_ice
) == SWITCH_STATUS_GENERR
) {
2381 SWITCH_DECLARE(void) switch_rtp_ping(switch_rtp_t
*rtp_session
)
2383 check_rtcp_and_ice(rtp_session
);
2386 SWITCH_DECLARE(void) switch_rtp_get_random(void *buf
, uint32_t len
)
2389 RAND_bytes(buf
, len
);
2391 switch_stun_random_string(buf
, len
, NULL
);
2396 SWITCH_DECLARE(void) switch_rtp_shutdown(void)
2398 switch_core_port_allocator_t
*alloc
= NULL
;
2399 switch_hash_index_t
*hi
;
2407 switch_mutex_lock(port_lock
);
2409 for (hi
= switch_core_hash_first(alloc_hash
); hi
; hi
= switch_core_hash_next(&hi
)) {
2410 switch_core_hash_this(hi
, &var
, NULL
, &val
);
2411 if ((alloc
= (switch_core_port_allocator_t
*) val
)) {
2412 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_DEBUG
, "Destroy port allocator for %s\n", (char *) var
);
2413 switch_core_port_allocator_destroy(&alloc
);
2417 switch_core_hash_destroy(&alloc_hash
);
2418 switch_mutex_unlock(port_lock
);
2421 srtp_crypto_kernel_shutdown();
2423 switch_rtp_dtls_destroy();
2426 SWITCH_DECLARE(switch_port_t
) switch_rtp_set_start_port(switch_port_t port
)
2430 switch_mutex_lock(port_lock
);
2434 switch_mutex_unlock(port_lock
);
2440 SWITCH_DECLARE(switch_port_t
) switch_rtp_set_end_port(switch_port_t port
)
2444 switch_mutex_lock(port_lock
);
2448 switch_mutex_unlock(port_lock
);
2454 SWITCH_DECLARE(void) switch_rtp_release_port(const char *ip
, switch_port_t port
)
2456 switch_core_port_allocator_t
*alloc
= NULL
;
2462 switch_mutex_lock(port_lock
);
2463 if ((alloc
= switch_core_hash_find(alloc_hash
, ip
))) {
2464 switch_core_port_allocator_free_port(alloc
, port
);
2466 switch_mutex_unlock(port_lock
);
2470 SWITCH_DECLARE(switch_port_t
) switch_rtp_request_port(const char *ip
)
2472 switch_port_t port
= 0;
2473 switch_core_port_allocator_t
*alloc
= NULL
;
2475 switch_mutex_lock(port_lock
);
2476 alloc
= switch_core_hash_find(alloc_hash
, ip
);
2478 if (switch_core_port_allocator_new(ip
, START_PORT
, END_PORT
, SPF_EVEN
, &alloc
) != SWITCH_STATUS_SUCCESS
) {
2482 switch_core_hash_insert(alloc_hash
, ip
, alloc
);
2485 if (switch_core_port_allocator_request_port(alloc
, &port
) != SWITCH_STATUS_SUCCESS
) {
2489 switch_mutex_unlock(port_lock
);
2493 SWITCH_DECLARE(switch_status_t
) switch_rtp_set_payload_map(switch_rtp_t
*rtp_session
, payload_map_t
**pmap
)
2497 switch_mutex_lock(rtp_session
->flag_mutex
);
2498 rtp_session
->pmaps
= pmap
;
2499 switch_mutex_unlock(rtp_session
->flag_mutex
);
2500 return SWITCH_STATUS_SUCCESS
;
2503 return SWITCH_STATUS_FALSE
;
2506 SWITCH_DECLARE(void) switch_rtp_intentional_bugs(switch_rtp_t
*rtp_session
, switch_rtp_bug_flag_t bugs
)
2508 rtp_session
->rtp_bugs
= bugs
;
2510 if ((rtp_session
->rtp_bugs
& RTP_BUG_START_SEQ_AT_ZERO
)) {
2511 rtp_session
->seq
= 0;
2517 static switch_status_t
enable_remote_rtcp_socket(switch_rtp_t
*rtp_session
, const char **err
) {
2519 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
2521 if (rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
2523 if (switch_sockaddr_info_get(&rtp_session
->rtcp_remote_addr
, rtp_session
->eff_remote_host_str
, SWITCH_UNSPEC
,
2524 rtp_session
->remote_rtcp_port
, 0, rtp_session
->pool
) != SWITCH_STATUS_SUCCESS
|| !rtp_session
->rtcp_remote_addr
) {
2525 *err
= "RTCP Remote Address Error!";
2526 return SWITCH_STATUS_FALSE
;
2531 host
= switch_get_addr(bufa
, sizeof(bufa
), rtp_session
->rtcp_remote_addr
);
2533 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
,
2534 "Setting RTCP remote addr to %s:%d %d\n", host
, rtp_session
->remote_rtcp_port
, rtp_session
->rtcp_remote_addr
->family
);
2537 if (rtp_session
->rtcp_sock_input
&& switch_sockaddr_get_family(rtp_session
->rtcp_remote_addr
) ==
2538 switch_sockaddr_get_family(rtp_session
->rtcp_local_addr
)) {
2539 rtp_session
->rtcp_sock_output
= rtp_session
->rtcp_sock_input
;
2542 if (rtp_session
->rtcp_sock_output
&& rtp_session
->rtcp_sock_output
!= rtp_session
->rtcp_sock_input
) {
2543 switch_socket_close(rtp_session
->rtcp_sock_output
);
2546 if ((status
= switch_socket_create(&rtp_session
->rtcp_sock_output
,
2547 switch_sockaddr_get_family(rtp_session
->rtcp_remote_addr
),
2548 SOCK_DGRAM
, 0, rtp_session
->pool
)) != SWITCH_STATUS_SUCCESS
) {
2549 *err
= "RTCP Socket Error!";
2554 *err
= "RTCP NOT ACTIVE!";
2561 static switch_status_t
enable_local_rtcp_socket(switch_rtp_t
*rtp_session
, const char **err
) {
2563 const char *host
= rtp_session
->local_host_str
;
2564 switch_port_t port
= rtp_session
->local_port
;
2565 switch_socket_t
*rtcp_new_sock
= NULL
, *rtcp_old_sock
= NULL
;
2566 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
2569 if (rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
2570 if (switch_sockaddr_info_get(&rtp_session
->rtcp_local_addr
, host
, SWITCH_UNSPEC
, port
+1, 0, rtp_session
->pool
) != SWITCH_STATUS_SUCCESS
) {
2571 *err
= "RTCP Local Address Error!";
2575 if (switch_socket_create(&rtcp_new_sock
, switch_sockaddr_get_family(rtp_session
->rtcp_local_addr
), SOCK_DGRAM
, 0, rtp_session
->pool
) != SWITCH_STATUS_SUCCESS
) {
2576 *err
= "RTCP Socket Error!";
2580 if (switch_socket_opt_set(rtcp_new_sock
, SWITCH_SO_REUSEADDR
, 1) != SWITCH_STATUS_SUCCESS
) {
2581 *err
= "RTCP Socket Error!";
2585 if (switch_socket_bind(rtcp_new_sock
, rtp_session
->rtcp_local_addr
) != SWITCH_STATUS_SUCCESS
) {
2586 *err
= "RTCP Bind Error!";
2590 if (switch_sockaddr_info_get(&rtp_session
->rtcp_from_addr
, switch_get_addr(bufa
, sizeof(bufa
), rtp_session
->from_addr
),
2591 SWITCH_UNSPEC
, switch_sockaddr_get_port(rtp_session
->from_addr
) + 1, 0, rtp_session
->pool
) != SWITCH_STATUS_SUCCESS
) {
2592 *err
= "RTCP From Address Error!";
2596 rtcp_old_sock
= rtp_session
->rtcp_sock_input
;
2597 rtp_session
->rtcp_sock_input
= rtcp_new_sock
;
2598 rtcp_new_sock
= NULL
;
2600 switch_socket_create_pollset(&rtp_session
->rtcp_read_pollfd
, rtp_session
->rtcp_sock_input
, SWITCH_POLLIN
| SWITCH_POLLERR
, rtp_session
->pool
);
2606 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Error allocating rtcp [%s]\n", *err
);
2607 status
= SWITCH_STATUS_FALSE
;
2610 if (rtcp_new_sock
) {
2611 switch_socket_close(rtcp_new_sock
);
2614 if (rtcp_old_sock
) {
2615 switch_socket_close(rtcp_old_sock
);
2618 status
= SWITCH_STATUS_FALSE
;
2624 SWITCH_DECLARE(switch_status_t
) switch_rtp_set_local_address(switch_rtp_t
*rtp_session
, const char *host
, switch_port_t port
, const char **err
)
2626 switch_socket_t
*new_sock
= NULL
, *old_sock
= NULL
;
2627 switch_status_t status
= SWITCH_STATUS_FALSE
;
2630 char o
[5] = "TEST", i
[5] = "";
2631 switch_size_t len
, ilen
= 0;
2635 if (rtp_session
->ready
!= 1) {
2636 if (!switch_rtp_ready(rtp_session
)) {
2637 return SWITCH_STATUS_FALSE
;
2640 READ_INC(rtp_session
);
2641 WRITE_INC(rtp_session
);
2643 if (!switch_rtp_ready(rtp_session
)) {
2651 if (zstr(host
) || !port
) {
2652 *err
= "Address Error";
2657 rtp_session
->local_host_str
= switch_core_strdup(rtp_session
->pool
, host
);
2658 rtp_session
->local_port
= port
;
2661 if (switch_sockaddr_info_get(&rtp_session
->local_addr
, host
, SWITCH_UNSPEC
, port
, 0, rtp_session
->pool
) != SWITCH_STATUS_SUCCESS
) {
2662 *err
= "Local Address Error!";
2667 if (rtp_session
->sock_input
) {
2668 switch_rtp_kill_socket(rtp_session
);
2671 if (switch_socket_create(&new_sock
, switch_sockaddr_get_family(rtp_session
->local_addr
), SOCK_DGRAM
, 0, rtp_session
->pool
) != SWITCH_STATUS_SUCCESS
) {
2672 *err
= "Socket Error!";
2676 if (switch_socket_opt_set(new_sock
, SWITCH_SO_REUSEADDR
, 1) != SWITCH_STATUS_SUCCESS
) {
2677 *err
= "Socket Error!";
2681 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
2682 switch_socket_opt_set(new_sock
, SWITCH_SO_RCVBUF
, 1572864);
2683 switch_socket_opt_set(new_sock
, SWITCH_SO_SNDBUF
, 1572864);
2685 switch_socket_opt_set(new_sock
, SWITCH_SO_RCVBUF
, 851968);
2686 switch_socket_opt_set(new_sock
, SWITCH_SO_SNDBUF
, 851968);
2689 if (switch_socket_bind(new_sock
, rtp_session
->local_addr
) != SWITCH_STATUS_SUCCESS
) {
2690 char *em
= switch_core_sprintf(rtp_session
->pool
, "Bind Error! %s:%d", host
, port
);
2696 if ((j
= atoi(host
)) && j
> 223 && j
< 240) { /* mcast */
2697 if (switch_mcast_interface(new_sock
, rtp_session
->local_addr
) != SWITCH_STATUS_SUCCESS
) {
2698 *err
= "Multicast Socket interface Error";
2702 if (switch_mcast_join(new_sock
, rtp_session
->local_addr
, NULL
, NULL
) != SWITCH_STATUS_SUCCESS
) {
2703 *err
= "Multicast Error";
2707 if (rtp_session
->session
) {
2708 switch_channel_t
*channel
= switch_core_session_get_channel(rtp_session
->session
);
2711 if ((var
= switch_channel_get_variable(channel
, "multicast_ttl"))) {
2712 int ttl
= atoi(var
);
2714 if (ttl
> 0 && ttl
< 256) {
2715 if (switch_mcast_hops(new_sock
, (uint8_t) ttl
) != SWITCH_STATUS_SUCCESS
) {
2716 *err
= "Mutlicast TTL set failed";
2731 switch_socket_opt_set(new_sock
, SWITCH_SO_NONBLOCK
, TRUE
);
2733 switch_socket_sendto(new_sock
, rtp_session
->local_addr
, 0, (void *) o
, &len
);
2737 switch_status_t status
;
2739 status
= switch_socket_recvfrom(rtp_session
->from_addr
, new_sock
, 0, (void *) i
, &ilen
);
2741 if (status
!= SWITCH_STATUS_SUCCESS
&& status
!= SWITCH_STATUS_BREAK
) {
2750 switch_socket_opt_set(new_sock
, SWITCH_SO_NONBLOCK
, FALSE
);
2754 old_sock
= rtp_session
->sock_input
;
2755 rtp_session
->sock_input
= new_sock
;
2758 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
] || rtp_session
->flags
[SWITCH_RTP_FLAG_NOBLOCK
] || rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
2759 switch_socket_opt_set(rtp_session
->sock_input
, SWITCH_SO_NONBLOCK
, TRUE
);
2760 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_NOBLOCK
);
2763 switch_socket_create_pollset(&rtp_session
->read_pollfd
, rtp_session
->sock_input
, SWITCH_POLLIN
| SWITCH_POLLERR
, rtp_session
->pool
);
2765 if (rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
2766 if ((status
= enable_local_rtcp_socket(rtp_session
, err
)) == SWITCH_STATUS_SUCCESS
) {
2770 status
= SWITCH_STATUS_SUCCESS
;
2774 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_IO
);
2779 switch_socket_close(new_sock
);
2783 switch_socket_close(old_sock
);
2787 if (rtp_session
->ready
!= 1) {
2788 WRITE_DEC(rtp_session
);
2789 READ_DEC(rtp_session
);
2795 SWITCH_DECLARE(void) switch_rtp_set_media_timeout(switch_rtp_t
*rtp_session
, uint32_t ms
)
2797 if (!switch_rtp_ready(rtp_session
) || rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
]) {
2801 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
,
2802 "%s MEDIA TIMEOUT %s set to %u\n", switch_core_session_get_name(rtp_session
->session
), rtp_type(rtp_session
), ms
);
2803 rtp_session
->media_timeout
= ms
;
2804 switch_rtp_reset_media_timer(rtp_session
);
2807 SWITCH_DECLARE(void) switch_rtp_set_max_missed_packets(switch_rtp_t
*rtp_session
, uint32_t max
)
2809 if (!switch_rtp_ready(rtp_session
) || rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
]) {
2813 if (rtp_session
->missed_count
> max
) {
2815 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
,
2816 "new max missed packets(%d->%d) greater than current missed packets(%d). RTP will timeout.\n",
2817 rtp_session
->max_missed_packets
, max
, rtp_session
->missed_count
);
2820 rtp_session
->max_missed_packets
= max
;
2823 SWITCH_DECLARE(void) switch_rtp_reset_jb(switch_rtp_t
*rtp_session
)
2825 if (switch_rtp_ready(rtp_session
)) {
2826 if (rtp_session
->jb
) {
2827 switch_jb_reset(rtp_session
->jb
);
2832 SWITCH_DECLARE(void) switch_rtp_reset_vb(switch_rtp_t
*rtp_session
)
2835 if (rtp_session
->vb
) {
2836 switch_jb_reset(rtp_session
->vb
);
2839 if (rtp_session
->vbw
) {
2840 switch_jb_reset(rtp_session
->vbw
);
2844 SWITCH_DECLARE(void) switch_rtp_reset(switch_rtp_t
*rtp_session
)
2850 //rtp_session->seq = (uint16_t) rand();
2851 //rtp_session->ts = 0;
2852 memset(&rtp_session
->ts_norm
, 0, sizeof(rtp_session
->ts_norm
));
2854 rtp_session
->last_stun
= rtp_session
->first_stun
= 0;
2855 rtp_session
->wrong_addrs
= 0;
2856 rtp_session
->rtcp_sent_packets
= 0;
2857 rtp_session
->rtcp_last_sent
= 0;
2858 rtp_session
->ice_adj
= 0;
2860 //switch_rtp_del_dtls(rtp_session, DTLS_TYPE_RTP|DTLS_TYPE_RTCP);
2861 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_PAUSE
);
2862 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_MUTE
);
2863 rtcp_stats_init(rtp_session
);
2865 if (rtp_session
->ice
.ready
) {
2866 switch_rtp_reset_vb(rtp_session
);
2867 rtp_session
->ice
.ready
= rtp_session
->ice
.rready
= 0;
2872 SWITCH_DECLARE(void) switch_rtp_reset_media_timer(switch_rtp_t
*rtp_session
)
2874 rtp_session
->missed_count
= 0;
2875 rtp_session
->last_media
= switch_micro_time_now();
2878 SWITCH_DECLARE(char *) switch_rtp_get_remote_host(switch_rtp_t
*rtp_session
)
2880 return zstr(rtp_session
->remote_host_str
) ? "0.0.0.0" : rtp_session
->remote_host_str
;
2883 SWITCH_DECLARE(switch_port_t
) switch_rtp_get_remote_port(switch_rtp_t
*rtp_session
)
2885 return rtp_session
->remote_port
;
2888 static void ping_socket(switch_rtp_t
*rtp_session
)
2890 uint32_t o
= UINT_MAX
;
2891 switch_size_t len
= sizeof(o
);
2892 switch_socket_sendto(rtp_session
->sock_input
, rtp_session
->local_addr
, 0, (void *) &o
, &len
);
2894 if (rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
] && rtp_session
->rtcp_sock_input
&& rtp_session
->rtcp_sock_input
!= rtp_session
->sock_input
) {
2895 switch_socket_sendto(rtp_session
->rtcp_sock_input
, rtp_session
->rtcp_local_addr
, 0, (void *) &o
, &len
);
2899 SWITCH_DECLARE(switch_status_t
) switch_rtp_udptl_mode(switch_rtp_t
*rtp_session
)
2901 switch_socket_t
*sock
;
2903 if (!switch_rtp_ready(rtp_session
)) {
2904 return SWITCH_STATUS_FALSE
;
2907 if (switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_PROXY_MEDIA
)) {
2908 ping_socket(rtp_session
);
2911 READ_INC(rtp_session
);
2912 WRITE_INC(rtp_session
);
2914 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
] || rtp_session
->timer
.timer_interface
) {
2915 switch_core_timer_destroy(&rtp_session
->timer
);
2916 memset(&rtp_session
->timer
, 0, sizeof(rtp_session
->timer
));
2917 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_USE_TIMER
);
2920 rtp_session
->missed_count
= 0;
2921 rtp_session
->max_missed_packets
= 0;
2923 rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
] = 0;
2925 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
2926 rtp_session
->rtcp_sock_input
= NULL
;
2927 rtp_session
->rtcp_sock_output
= NULL
;
2929 if (rtp_session
->rtcp_sock_input
&& rtp_session
->rtcp_sock_input
!= rtp_session
->sock_input
) {
2930 ping_socket(rtp_session
);
2931 switch_socket_shutdown(rtp_session
->rtcp_sock_input
, SWITCH_SHUTDOWN_READWRITE
);
2934 if (rtp_session
->rtcp_sock_output
&& rtp_session
->rtcp_sock_output
!= rtp_session
->rtcp_sock_input
&&
2935 rtp_session
->rtcp_sock_output
!= rtp_session
->sock_input
) {
2936 switch_socket_shutdown(rtp_session
->rtcp_sock_output
, SWITCH_SHUTDOWN_READWRITE
);
2939 if ((sock
= rtp_session
->rtcp_sock_input
)) {
2940 rtp_session
->rtcp_sock_input
= NULL
;
2941 switch_socket_close(sock
);
2943 if (rtp_session
->rtcp_sock_output
&& rtp_session
->rtcp_sock_output
!= sock
) {
2944 sock
= rtp_session
->rtcp_sock_output
;
2945 rtp_session
->rtcp_sock_output
= NULL
;
2946 switch_socket_close(sock
);
2951 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_UDPTL
);
2952 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_PROXY_MEDIA
);
2953 switch_socket_opt_set(rtp_session
->sock_input
, SWITCH_SO_NONBLOCK
, FALSE
);
2954 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_NOBLOCK
);
2956 WRITE_DEC(rtp_session
);
2957 READ_DEC(rtp_session
);
2959 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_STICKY_FLUSH
);
2960 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_FLUSH
);
2962 switch_rtp_break(rtp_session
);
2964 return SWITCH_STATUS_SUCCESS
;
2969 SWITCH_DECLARE(switch_status_t
) switch_rtp_set_remote_address(switch_rtp_t
*rtp_session
, const char *host
, switch_port_t port
, switch_port_t remote_rtcp_port
,
2970 switch_bool_t change_adv_addr
, const char **err
)
2972 switch_sockaddr_t
*remote_addr
;
2973 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
2976 if (switch_sockaddr_info_get(&remote_addr
, host
, SWITCH_UNSPEC
, port
, 0, rtp_session
->pool
) != SWITCH_STATUS_SUCCESS
|| !remote_addr
) {
2977 *err
= "Remote Address Error!";
2978 return SWITCH_STATUS_FALSE
;
2982 switch_mutex_lock(rtp_session
->write_mutex
);
2984 rtp_session
->remote_addr
= remote_addr
;
2986 if (change_adv_addr
) {
2987 rtp_session
->remote_host_str
= switch_core_strdup(rtp_session
->pool
, host
);
2988 rtp_session
->remote_port
= port
;
2991 rtp_session
->eff_remote_host_str
= switch_core_strdup(rtp_session
->pool
, host
);
2992 rtp_session
->eff_remote_port
= port
;
2994 if (rtp_session
->sock_input
&& switch_sockaddr_get_family(rtp_session
->remote_addr
) == switch_sockaddr_get_family(rtp_session
->local_addr
)) {
2995 rtp_session
->sock_output
= rtp_session
->sock_input
;
2997 if (rtp_session
->sock_output
&& rtp_session
->sock_output
!= rtp_session
->sock_input
) {
2998 switch_socket_close(rtp_session
->sock_output
);
3000 if ((status
= switch_socket_create(&rtp_session
->sock_output
,
3001 switch_sockaddr_get_family(rtp_session
->remote_addr
),
3002 SOCK_DGRAM
, 0, rtp_session
->pool
)) != SWITCH_STATUS_SUCCESS
) {
3004 *err
= "Socket Error!";
3008 if (rtp_session
->dtls
) {
3009 rtp_session
->dtls
->sock_output
= rtp_session
->sock_output
;
3011 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
3012 status
= switch_sockaddr_info_get(&rtp_session
->dtls
->remote_addr
, host
, SWITCH_UNSPEC
, port
, 0, rtp_session
->pool
);
3017 if (rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
3018 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
3019 rtp_session
->rtcp_remote_addr
= rtp_session
->remote_addr
;
3020 rtp_session
->rtcp_sock_output
= rtp_session
->sock_output
;
3022 if (remote_rtcp_port) {
3023 rtp_session->remote_rtcp_port = remote_rtcp_port;
3025 rtp_session->remote_rtcp_port = rtp_session->eff_remote_port + 1;
3027 status = enable_remote_rtcp_socket(rtp_session, err);
3029 if (rtp_session->rtcp_dtls) {
3030 //switch_sockaddr_info_get(&rtp_session->rtcp_dtls->remote_addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool);
3031 rtp_session->rtcp_dtls->remote_addr = rtp_session->rtcp_remote_addr;
3032 rtp_session->rtcp_dtls->sock_output = rtp_session->rtcp_sock_output;
3037 switch_mutex_unlock(rtp_session
->write_mutex
);
3043 static const char *dtls_state_names_t
[] = {"OFF", "HANDSHAKE", "SETUP", "READY", "FAIL", "INVALID"};
3044 static const char *dtls_state_names(dtls_state_t s
)
3046 if (s
> DS_INVALID
) {
3050 return dtls_state_names_t
[s
];
3053 #define dtls_set_state(_dtls, _state) switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n", rtp_type(rtp_session), dtls_state_names(_dtls->state), dtls_state_names(_state)); _dtls->new_state = 1; _dtls->last_state = _dtls->state; _dtls->state = _state
3055 #define cr_keylen 16
3056 #define cr_saltlen 14
3059 static int dtls_state_setup(switch_rtp_t
*rtp_session
, switch_dtls_t
*dtls
)
3062 switch_secure_settings_t ssec
; /* Used just to wrap over params in a call to switch_rtp_add_crypto_key. */
3065 uint8_t raw_key_data
[cr_kslen
* 2];
3066 unsigned char local_key_buf
[cr_kslen
];
3067 unsigned char remote_key_buf
[cr_kslen
];
3069 memset(&ssec
, 0, sizeof(ssec
));
3070 memset(&raw_key_data
, 0, cr_kslen
* 2 * sizeof(uint8_t));
3071 memset(&local_key_buf
, 0, cr_kslen
* sizeof(unsigned char));
3072 memset(&remote_key_buf
, 0, cr_kslen
* sizeof(unsigned char));
3074 if ((dtls
->type
& DTLS_TYPE_SERVER
)) {
3076 } else if ((cert
= SSL_get_peer_certificate(dtls
->ssl
))) {
3077 switch_core_cert_extract_fingerprint(cert
, dtls
->remote_fp
);
3078 r
= switch_core_cert_verify(dtls
->remote_fp
);
3083 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s Fingerprint Verification Failed!\n", rtp_type(rtp_session
));
3084 dtls_set_state(dtls
, DS_FAIL
);
3087 unsigned char *local_key
, *remote_key
, *local_salt
, *remote_salt
;
3089 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_INFO
, "%s Fingerprint Verified.\n", rtp_type(rtp_session
));
3091 #ifdef HAVE_OPENSSL_DTLS_SRTP
3092 if (!SSL_export_keying_material(dtls
->ssl
, raw_key_data
, sizeof(raw_key_data
), "EXTRACTOR-dtls_srtp", 19, NULL
, 0, 0)) {
3093 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s Key material export failure\n", rtp_type(rtp_session
));
3094 dtls_set_state(dtls
, DS_FAIL
);
3101 if ((dtls
->type
& DTLS_TYPE_CLIENT
)) {
3102 local_key
= raw_key_data
;
3103 remote_key
= local_key
+ cr_keylen
;
3104 local_salt
= remote_key
+ cr_keylen
;
3105 remote_salt
= local_salt
+ cr_saltlen
;
3108 remote_key
= raw_key_data
;
3109 local_key
= remote_key
+ cr_keylen
;
3110 remote_salt
= local_key
+ cr_keylen
;
3111 local_salt
= remote_salt
+ cr_saltlen
;
3114 memcpy(ssec
.local_raw_key
, local_key
, cr_keylen
);
3115 memcpy(ssec
.local_raw_key
+ cr_keylen
, local_salt
, cr_saltlen
);
3117 memcpy(ssec
.remote_raw_key
, remote_key
, cr_keylen
);
3118 memcpy(ssec
.remote_raw_key
+ cr_keylen
, remote_salt
, cr_saltlen
);
3120 ssec
.crypto_type
= AES_CM_128_HMAC_SHA1_80
;
3122 if (dtls
== rtp_session
->rtcp_dtls
&& rtp_session
->rtcp_dtls
!= rtp_session
->dtls
) {
3123 switch_rtp_add_crypto_key(rtp_session
, SWITCH_RTP_CRYPTO_SEND_RTCP
, 0, &ssec
);
3124 switch_rtp_add_crypto_key(rtp_session
, SWITCH_RTP_CRYPTO_RECV_RTCP
, 0, &ssec
);
3126 switch_rtp_add_crypto_key(rtp_session
, SWITCH_RTP_CRYPTO_SEND
, 0, &ssec
);
3127 switch_rtp_add_crypto_key(rtp_session
, SWITCH_RTP_CRYPTO_RECV
, 0, &ssec
);
3131 dtls_set_state(dtls
, DS_READY
);
3136 static int dtls_state_ready(switch_rtp_t
*rtp_session
, switch_dtls_t
*dtls
)
3139 if (dtls
->new_state
) {
3140 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
3141 switch_core_session_t
*other_session
;
3143 if (rtp_session
->session
&& switch_core_session_get_partner(rtp_session
->session
, &other_session
) == SWITCH_STATUS_SUCCESS
) {
3144 switch_core_session_request_video_refresh(other_session
);
3145 switch_core_session_rwunlock(other_session
);
3148 dtls
->new_state
= 0;
3153 static int dtls_state_fail(switch_rtp_t
*rtp_session
, switch_dtls_t
*dtls
)
3155 if (rtp_session
->session
) {
3156 switch_channel_t
*channel
= switch_core_session_get_channel(rtp_session
->session
);
3157 switch_channel_hangup(channel
, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
);
3164 static int dtls_state_handshake(switch_rtp_t
*rtp_session
, switch_dtls_t
*dtls
)
3168 if ((ret
= SSL_do_handshake(dtls
->ssl
)) != 1){
3169 switch((ret
= SSL_get_error(dtls
->ssl
, ret
))){
3170 case SSL_ERROR_WANT_READ
:
3171 case SSL_ERROR_WANT_WRITE
:
3172 case SSL_ERROR_NONE
:
3175 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
, "%s Handshake failure %d. This may happen when you use legacy DTLS v1.0 (legacyDTLS channel var is set) but endpoint requires DTLS v1.2.\n", rtp_type(rtp_session
), ret
);
3176 dtls_set_state(dtls
, DS_FAIL
);
3181 if (SSL_is_init_finished(dtls
->ssl
)) {
3182 dtls_set_state(dtls
, DS_SETUP
);
3188 static void free_dtls(switch_dtls_t
**dtlsp
)
3190 switch_dtls_t
*dtls
;
3200 SSL_free(dtls
->ssl
);
3203 if (dtls
->ssl_ctx
) {
3204 SSL_CTX_free(dtls
->ssl_ctx
);
3208 static int do_dtls(switch_rtp_t
*rtp_session
, switch_dtls_t
*dtls
)
3210 int r
= 0, ret
= 0, len
;
3211 switch_size_t bytes
;
3212 unsigned char buf
[MAX_DTLS_MTU
] = "";
3213 int ready
= rtp_session
->ice
.ice_user
? (rtp_session
->ice
.rready
&& rtp_session
->ice
.ready
) : 1;
3216 if (!dtls
->bytes
&& !ready
) {
3220 if (dtls
->bytes
> 0 && dtls
->data
) {
3221 ret
= BIO_write(dtls
->read_bio
, dtls
->data
, (int)dtls
->bytes
);
3223 ret
= SSL_get_error(dtls
->ssl
, ret
);
3224 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s DTLS packet decode err: SSL err %d\n", rtp_type(rtp_session
), ret
);
3225 } else if (ret
!= (int)dtls
->bytes
) {
3226 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s DTLS packet decode err: read %d bytes instead of %d\n", rtp_type(rtp_session
), ret
, (int)dtls
->bytes
);
3230 if (dtls_states
[dtls
->state
]) {
3231 r
= dtls_states
[dtls
->state
](rtp_session
, dtls
);
3234 while ((pending
= BIO_ctrl_pending(dtls
->filter_bio
)) > 0) {
3235 switch_assert(pending
<= sizeof(buf
));
3237 len
= BIO_read(dtls
->write_bio
, buf
, pending
);
3240 ret
= switch_socket_sendto(dtls
->sock_output
, dtls
->remote_addr
, 0, (void *)buf
, &bytes
);
3242 if (ret
!= SWITCH_STATUS_SUCCESS
) {
3243 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s DTLS packet not written to socket: %d\n", rtp_type(rtp_session
), ret
);
3244 } else if (bytes
!= len
) {
3245 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s DTLS packet write err: written %d bytes instead of %d\n", rtp_type(rtp_session
), (int)bytes
, len
);
3248 ret
= SSL_get_error(dtls
->ssl
, len
);
3249 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s DTLS packet encode err: SSL err %d\n", rtp_type(rtp_session
), ret
);
3257 static int cb_verify_peer(int preverify_ok
, X509_STORE_CTX
*ctx
)
3260 switch_dtls_t
*dtls
;
3264 ssl
= X509_STORE_CTX_get_app_data(ctx
);
3265 dtls
= (switch_dtls_t
*) SSL_get_app_data(ssl
);
3267 if (!(ssl
&& dtls
)) {
3271 if ((cert
= SSL_get_peer_certificate(dtls
->ssl
))) {
3272 switch_core_cert_extract_fingerprint(cert
, dtls
->remote_fp
);
3274 r
= switch_core_cert_verify(dtls
->remote_fp
);
3278 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(dtls
->rtp_session
->session
), SWITCH_LOG_ERROR
, "%s CERT ERR!\n", rtp_type(dtls
->rtp_session
));
3288 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3289 static BIO_METHOD dtls_bio_filter_methods
;
3291 static BIO_METHOD
*dtls_bio_filter_methods
;
3294 BIO_METHOD
*BIO_dtls_filter(void) {
3295 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3296 return(&dtls_bio_filter_methods
);
3298 return(dtls_bio_filter_methods
);
3302 typedef struct packet_list_s
{
3305 struct packet_list_s
*next
;
3308 /* Helper struct to keep the filter state */
3309 typedef struct dtls_bio_filter
{
3310 packet_list_t
*packets
;
3311 packet_list_t
*unused
;
3312 packet_list_t
*tail
;
3313 switch_mutex_t
*mutex
;
3314 switch_memory_pool_t
*pool
;
3319 static int dtls_bio_filter_new(BIO
*bio
) {
3320 /* Create a filter state struct */
3321 dtls_bio_filter
*filter
;
3322 switch_memory_pool_t
*pool
;
3324 switch_core_new_memory_pool(&pool
);
3325 filter
= switch_core_alloc(pool
, sizeof(*filter
));
3326 filter
->pool
= pool
;
3328 filter
->packets
= NULL
;
3329 switch_mutex_init(&filter
->mutex
, SWITCH_MUTEX_NESTED
, filter
->pool
);
3331 /* Set the BIO as initialized */
3332 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3337 BIO_set_init(bio
, 1);
3338 BIO_set_data(bio
, filter
);
3339 BIO_clear_flags(bio
, ~0);
3345 static int dtls_bio_filter_free(BIO
*bio
) {
3346 dtls_bio_filter
*filter
;
3352 /* Get rid of the filter state */
3353 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3354 filter
= (dtls_bio_filter
*)bio
->ptr
;
3356 filter
= (dtls_bio_filter
*)BIO_get_data(bio
);
3359 if (filter
!= NULL
) {
3360 switch_memory_pool_t
*pool
= filter
->pool
;
3361 switch_core_destroy_memory_pool(&pool
);
3366 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3371 BIO_set_init(bio
, 0);
3372 BIO_set_data(bio
, NULL
);
3373 BIO_clear_flags(bio
, ~0);
3378 static int dtls_bio_filter_write(BIO
*bio
, const char *in
, int inl
) {
3380 dtls_bio_filter
*filter
;
3382 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_DEBUG1
, "dtls_bio_filter_write: %p, %d\n", (void *)in
, inl
);
3383 /* Forward data to the write BIO */
3384 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3385 ret
= BIO_write(bio
->next_bio
, in
, inl
);
3387 ret
= BIO_write(BIO_next(bio
), in
, inl
);
3390 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_DEBUG1
, " -- %ld\n", ret
);
3392 /* Keep track of the packet, as we'll advertize them one by one after a pending check */
3393 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3394 filter
= (dtls_bio_filter
*)bio
->ptr
;
3396 filter
= (dtls_bio_filter
*)BIO_get_data(bio
);
3399 if (filter
!= NULL
) {
3400 packet_list_t
*node
;
3402 switch_mutex_lock(filter
->mutex
);
3403 if (filter
->unused
) {
3404 node
= filter
->unused
;
3406 filter
->unused
= filter
->unused
->next
;
3408 node
= switch_core_alloc(filter
->pool
, sizeof(*node
));
3414 filter
->tail
->next
= node
;
3416 filter
->packets
= node
;
3419 filter
->tail
= node
;
3421 switch_mutex_unlock(filter
->mutex
);
3422 //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "New list length: %d\n", g_list_length(filter->packets));
3427 static long dtls_bio_filter_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
) {
3428 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3429 dtls_bio_filter
*filter
= (dtls_bio_filter
*)bio
->ptr
;
3431 dtls_bio_filter
*filter
= (dtls_bio_filter
*)BIO_get_data(bio
);
3435 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU
:
3437 case BIO_CTRL_DGRAM_SET_MTU
:
3439 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_DEBUG1
, "Setting MTU: %ld\n", filter
->mtu
);
3441 case BIO_CTRL_FLUSH
:
3443 case BIO_CTRL_DGRAM_QUERY_MTU
:
3444 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_DEBUG1
, "Advertizing MTU: %ld\n", filter
->mtu
);
3446 case BIO_CTRL_WPENDING
:
3448 case BIO_CTRL_PENDING
: {
3452 if (filter
== NULL
) {
3456 switch_mutex_lock(filter
->mutex
);
3457 if ((top
= filter
->packets
)) {
3458 filter
->packets
= filter
->packets
->next
;
3460 if (top
== filter
->tail
|| !filter
->packets
) {
3461 filter
->tail
= NULL
;
3464 pending
= top
->size
;
3465 top
->next
= filter
->unused
;
3466 filter
->unused
= top
;
3468 switch_mutex_unlock(filter
->mutex
);
3473 //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dtls_bio_filter_ctrl: %d\n", cmd);
3479 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3480 static BIO_METHOD dtls_bio_filter_methods
= {
3483 dtls_bio_filter_write
,
3487 dtls_bio_filter_ctrl
,
3488 dtls_bio_filter_new
,
3489 dtls_bio_filter_free
,
3493 static BIO_METHOD
*dtls_bio_filter_methods
= NULL
;
3496 static void switch_rtp_dtls_init() {
3497 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
3498 dtls_bio_filter_methods
= BIO_meth_new(BIO_TYPE_FILTER
| BIO_get_new_index(), "DTLS filter");
3499 BIO_meth_set_write(dtls_bio_filter_methods
, dtls_bio_filter_write
);
3500 BIO_meth_set_ctrl(dtls_bio_filter_methods
, dtls_bio_filter_ctrl
);
3501 BIO_meth_set_create(dtls_bio_filter_methods
, dtls_bio_filter_new
);
3502 BIO_meth_set_destroy(dtls_bio_filter_methods
, dtls_bio_filter_free
);
3506 static void switch_rtp_dtls_destroy() {
3507 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
3508 if (dtls_bio_filter_methods
) {
3509 BIO_meth_free(dtls_bio_filter_methods
);
3510 dtls_bio_filter_methods
= NULL
;
3519 SWITCH_DECLARE(int) switch_rtp_has_dtls(void) {
3520 #ifdef HAVE_OPENSSL_DTLS_SRTP
3527 SWITCH_DECLARE(dtls_state_t
) switch_rtp_dtls_state(switch_rtp_t
*rtp_session
, dtls_type_t type
)
3529 dtls_state_t s
= DS_OFF
;
3535 switch_mutex_lock(rtp_session
->ice_mutex
);
3537 if (!rtp_session
->dtls
&& !rtp_session
->rtcp_dtls
) {
3542 if ((type
== DTLS_TYPE_RTP
) && rtp_session
->dtls
) {
3543 s
= rtp_session
->dtls
->state
;
3547 if ((type
== DTLS_TYPE_RTCP
) && rtp_session
->rtcp_dtls
) {
3548 s
= rtp_session
->rtcp_dtls
->state
;
3553 switch_mutex_unlock(rtp_session
->ice_mutex
);
3558 SWITCH_DECLARE(switch_status_t
) switch_rtp_del_dtls(switch_rtp_t
*rtp_session
, dtls_type_t type
)
3560 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
3563 return SWITCH_STATUS_FALSE
;
3566 switch_mutex_lock(rtp_session
->ice_mutex
);
3568 if (!rtp_session
->dtls
&& !rtp_session
->rtcp_dtls
) {
3569 switch_goto_status(SWITCH_STATUS_FALSE
, done
);
3572 if ((type
& DTLS_TYPE_RTP
)) {
3573 if (rtp_session
->dtls
&& rtp_session
->dtls
== rtp_session
->rtcp_dtls
) {
3574 rtp_session
->rtcp_dtls
= NULL
;
3577 if (rtp_session
->dtls
) {
3578 free_dtls(&rtp_session
->dtls
);
3581 if (rtp_session
->jb
) {
3582 switch_jb_reset(rtp_session
->jb
);
3585 if (rtp_session
->vb
) {
3586 switch_jb_reset(rtp_session
->vb
);
3589 if (rtp_session
->vbw
) {
3590 switch_jb_reset(rtp_session
->vbw
);
3595 if ((type
& DTLS_TYPE_RTCP
) && rtp_session
->rtcp_dtls
) {
3596 free_dtls(&rtp_session
->rtcp_dtls
);
3601 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND
]) {
3604 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND
] = 0;
3606 for(x
= 0; x
< 2; x
++) {
3607 if (rtp_session
->send_ctx
[x
]) {
3608 srtp_dealloc(rtp_session
->send_ctx
[x
]);
3609 rtp_session
->send_ctx
[x
] = NULL
;
3614 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV
]) {
3617 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV
] = 0;
3619 for (x
= 0; x
< 2; x
++) {
3620 if (rtp_session
->recv_ctx
[x
]) {
3621 srtp_dealloc(rtp_session
->recv_ctx
[x
]);
3622 rtp_session
->recv_ctx
[x
] = NULL
;
3630 switch_mutex_unlock(rtp_session
->ice_mutex
);
3635 SWITCH_DECLARE(switch_status_t
) switch_rtp_add_dtls(switch_rtp_t
*rtp_session
, dtls_fingerprint_t
*local_fp
, dtls_fingerprint_t
*remote_fp
, dtls_type_t type
, uint8_t want_DTLSv1_2
)
3637 switch_dtls_t
*dtls
;
3640 const char *kind
= "";
3641 unsigned long ssl_method_error
= 0;
3642 unsigned long ssl_ctx_error
= 0;
3643 const SSL_METHOD
*ssl_method
;
3647 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
3648 #ifndef OPENSSL_NO_EC
3649 #if OPENSSL_VERSION_NUMBER < 0x10002000L
3654 #ifndef HAVE_OPENSSL_DTLS_SRTP
3655 return SWITCH_STATUS_FALSE
;
3658 if (!switch_rtp_ready(rtp_session
)) {
3659 return SWITCH_STATUS_FALSE
;
3662 switch_mutex_lock(rtp_session
->ice_mutex
);
3664 if (!((type
& DTLS_TYPE_RTP
) || (type
& DTLS_TYPE_RTCP
)) || !((type
& DTLS_TYPE_CLIENT
) || (type
& DTLS_TYPE_SERVER
))) {
3665 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_CRIT
, "INVALID TYPE!\n");
3668 switch_rtp_del_dtls(rtp_session
, type
);
3670 if ((type
& DTLS_TYPE_RTP
) && (type
& DTLS_TYPE_RTCP
)) {
3672 } else if ((type
& DTLS_TYPE_RTP
)) {
3678 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_INFO
,
3679 "Activate %s %s DTLS %s\n", kind
, rtp_type(rtp_session
), (type
& DTLS_TYPE_SERVER
) ? "server" : "client");
3681 if (((type
& DTLS_TYPE_RTP
) && rtp_session
->dtls
) || ((type
& DTLS_TYPE_RTCP
) && rtp_session
->rtcp_dtls
)) {
3682 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
, "DTLS ALREADY INIT\n");
3683 switch_goto_status(SWITCH_STATUS_FALSE
, done
);
3686 dtls
= switch_core_alloc(rtp_session
->pool
, sizeof(*dtls
));
3688 dtls
->pem
= switch_core_sprintf(rtp_session
->pool
, "%s%s%s.pem", SWITCH_GLOBAL_dirs
.certs_dir
, SWITCH_PATH_SEPARATOR
, DTLS_SRTP_FNAME
);
3690 if (switch_file_exists(dtls
->pem
, rtp_session
->pool
) == SWITCH_STATUS_SUCCESS
) {
3691 dtls
->pvt
= dtls
->rsa
= dtls
->pem
;
3693 dtls
->pvt
= switch_core_sprintf(rtp_session
->pool
, "%s%s%s.key", SWITCH_GLOBAL_dirs
.certs_dir
, SWITCH_PATH_SEPARATOR
, DTLS_SRTP_FNAME
);
3694 dtls
->rsa
= switch_core_sprintf(rtp_session
->pool
, "%s%s%s.crt", SWITCH_GLOBAL_dirs
.certs_dir
, SWITCH_PATH_SEPARATOR
, DTLS_SRTP_FNAME
);
3697 dtls
->ca
= switch_core_sprintf(rtp_session
->pool
, "%s%sca-bundle.crt", SWITCH_GLOBAL_dirs
.certs_dir
, SWITCH_PATH_SEPARATOR
);
3699 #if OPENSSL_VERSION_NUMBER >= 0x10100000
3700 ssl_method
= (type
& DTLS_TYPE_SERVER
) ? DTLS_server_method() : DTLS_client_method();
3702 #ifdef HAVE_OPENSSL_DTLSv1_2_method
3703 ssl_method
= (type
& DTLS_TYPE_SERVER
) ? (want_DTLSv1_2
? DTLSv1_2_server_method() : DTLSv1_server_method()) : (want_DTLSv1_2
? DTLSv1_2_client_method() : DTLSv1_client_method());
3705 ssl_method
= (type
& DTLS_TYPE_SERVER
) ? DTLSv1_server_method() : DTLSv1_client_method();
3706 #endif // HAVE_OPENSSL_DTLSv1_2_method
3710 ssl_method_error
= ERR_peek_error();
3711 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s ssl_method is NULL [%lu]\n", rtp_type(rtp_session
), ssl_method_error
);
3714 dtls
->ssl_ctx
= ssl_ctx
= SSL_CTX_new(ssl_method
);
3717 ssl_ctx_error
= ERR_peek_error();
3718 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s SSL_CTX_new failed [%lu]\n", rtp_type(rtp_session
), ssl_ctx_error
);
3719 switch_channel_hangup(switch_core_session_get_channel(rtp_session
->session
), SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE
);
3720 switch_goto_status(SWITCH_STATUS_FALSE
, done
);
3723 switch_assert(dtls
->ssl_ctx
);
3725 bio
= BIO_new_file(dtls
->pem
, "r");
3726 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
3729 SSL_CTX_set_tmp_dh(dtls
->ssl_ctx
, dh
);
3733 SSL_CTX_set_mode(dtls
->ssl_ctx
, SSL_MODE_AUTO_RETRY
);
3735 //SSL_CTX_set_verify(dtls->ssl_ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
3736 SSL_CTX_set_verify(dtls
->ssl_ctx
, SSL_VERIFY_NONE
, NULL
);
3738 //SSL_CTX_set_cipher_list(dtls->ssl_ctx, "ECDH:!RC4:!SSLv3:RSA_WITH_AES_128_CBC_SHA");
3739 //SSL_CTX_set_cipher_list(dtls->ssl_ctx, "ECDHE-RSA-AES256-GCM-SHA384");
3740 SSL_CTX_set_cipher_list(dtls
->ssl_ctx
, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
3741 //SSL_CTX_set_cipher_list(dtls->ssl_ctx, "SUITEB128");
3742 SSL_CTX_set_read_ahead(dtls
->ssl_ctx
, 1);
3743 #ifdef HAVE_OPENSSL_DTLS_SRTP
3744 //SSL_CTX_set_tlsext_use_srtp(dtls->ssl_ctx, "SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32");
3745 SSL_CTX_set_tlsext_use_srtp(dtls
->ssl_ctx
, "SRTP_AES128_CM_SHA1_80");
3749 dtls
->read_bio
= BIO_new(BIO_s_mem());
3750 switch_assert(dtls
->read_bio
);
3752 dtls
->write_bio
= BIO_new(BIO_s_mem());
3753 switch_assert(dtls
->write_bio
);
3755 BIO_set_mem_eof_return(dtls
->read_bio
, -1);
3756 BIO_set_mem_eof_return(dtls
->write_bio
, -1);
3758 if ((ret
=SSL_CTX_use_certificate_file(dtls
->ssl_ctx
, dtls
->rsa
, SSL_FILETYPE_PEM
)) != 1) {
3759 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s DTLS cert err [%d]\n", rtp_type(rtp_session
), SSL_get_error(dtls
->ssl
, ret
));
3760 switch_goto_status(SWITCH_STATUS_FALSE
, done
);
3763 if ((ret
=SSL_CTX_use_PrivateKey_file(dtls
->ssl_ctx
, dtls
->pvt
, SSL_FILETYPE_PEM
)) != 1) {
3764 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s DTLS key err [%d]\n", rtp_type(rtp_session
), SSL_get_error(dtls
->ssl
, ret
));
3765 switch_goto_status(SWITCH_STATUS_FALSE
, done
);
3768 if (SSL_CTX_check_private_key(dtls
->ssl_ctx
) == 0) {
3769 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s DTLS check key failed\n", rtp_type(rtp_session
));
3770 switch_goto_status(SWITCH_STATUS_FALSE
, done
);
3773 if (!zstr(dtls
->ca
) && switch_file_exists(dtls
->ca
, rtp_session
->pool
) == SWITCH_STATUS_SUCCESS
3774 && (ret
= SSL_CTX_load_verify_locations(dtls
->ssl_ctx
, dtls
->ca
, NULL
)) != 1) {
3775 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "%s DTLS check chain cert failed [%d]\n",
3776 rtp_type(rtp_session
) ,
3777 SSL_get_error(dtls
->ssl
, ret
));
3778 switch_goto_status(SWITCH_STATUS_FALSE
, done
);
3781 dtls
->ssl
= SSL_new(dtls
->ssl_ctx
);
3783 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3784 dtls
->filter_bio
= BIO_new(BIO_dtls_filter());
3786 switch_assert(dtls_bio_filter_methods
);
3787 dtls
->filter_bio
= BIO_new(dtls_bio_filter_methods
);
3790 switch_assert(dtls
->filter_bio
);
3792 BIO_push(dtls
->filter_bio
, dtls
->write_bio
);
3794 SSL_set_bio(dtls
->ssl
, dtls
->read_bio
, dtls
->filter_bio
);
3796 SSL_set_mode(dtls
->ssl
, SSL_MODE_AUTO_RETRY
);
3797 SSL_set_read_ahead(dtls
->ssl
, 1);
3800 //SSL_set_verify(dtls->ssl, (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), cb_verify_peer);
3802 #ifndef OPENSSL_NO_EC
3803 #if OPENSSL_VERSION_NUMBER < 0x10002000L
3804 ecdh
= EC_KEY_new_by_curve_name(NID_X9_62_prime256v1
);
3806 switch_goto_status(SWITCH_STATUS_FALSE
, done
);
3808 SSL_set_options(dtls
->ssl
, SSL_OP_SINGLE_ECDH_USE
);
3809 SSL_set_tmp_ecdh(dtls
->ssl
, ecdh
);
3811 #elif OPENSSL_VERSION_NUMBER < 0x10100000L
3812 SSL_set_ecdh_auto(dtls
->ssl
, 1);
3813 SSL_set_options(dtls
->ssl
, SSL_OP_SINGLE_ECDH_USE
);
3817 SSL_set_verify(dtls
->ssl
, SSL_VERIFY_NONE
, NULL
);
3818 SSL_set_app_data(dtls
->ssl
, dtls
);
3820 dtls
->local_fp
= local_fp
;
3821 dtls
->remote_fp
= remote_fp
;
3822 dtls
->rtp_session
= rtp_session
;
3825 if (rtp_session
->session
) {
3826 switch_channel_t
*channel
= switch_core_session_get_channel(rtp_session
->session
);
3827 if ((var
= switch_channel_get_variable(channel
, "rtp_dtls_mtu"))) {
3828 int mtu
= atoi(var
);
3830 if (mtu
> 0 && mtu
< MAX_DTLS_MTU
) {
3837 BIO_ctrl(dtls
->filter_bio
, BIO_CTRL_DGRAM_SET_MTU
, dtls
->mtu
, NULL
);
3839 switch_core_cert_expand_fingerprint(remote_fp
, remote_fp
->str
);
3841 if ((type
& DTLS_TYPE_RTP
)) {
3842 rtp_session
->dtls
= dtls
;
3843 dtls
->sock_output
= rtp_session
->sock_output
;
3844 dtls
->remote_addr
= rtp_session
->remote_addr
;
3847 if ((type
& DTLS_TYPE_RTCP
)) {
3848 rtp_session
->rtcp_dtls
= dtls
;
3849 if (!(type
& DTLS_TYPE_RTP
)) {
3850 dtls
->sock_output
= rtp_session
->rtcp_sock_output
;
3851 dtls
->remote_addr
= rtp_session
->rtcp_remote_addr
;
3855 if ((type
& DTLS_TYPE_SERVER
)) {
3856 SSL_set_accept_state(dtls
->ssl
);
3858 SSL_set_connect_state(dtls
->ssl
);
3861 dtls_set_state(dtls
, DS_HANDSHAKE
);
3863 rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO_BREAK
] = 1;
3864 switch_rtp_break(rtp_session
);
3868 switch_mutex_unlock(rtp_session
->ice_mutex
);
3874 SWITCH_DECLARE(switch_status_t
) switch_rtp_add_crypto_key(switch_rtp_t
*rtp_session
, switch_rtp_crypto_direction_t direction
, uint32_t index
, switch_secure_settings_t
*ssec
)
3877 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_CRIT
, "SRTP NOT SUPPORTED IN THIS BUILD!\n");
3878 return SWITCH_STATUS_FALSE
;
3881 switch_rtp_crypto_key_t
*crypto_key
;
3882 srtp_policy_t
*policy
;
3883 srtp_err_status_t stat
;
3884 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
3886 switch_channel_t
*channel
= switch_core_session_get_channel(rtp_session
->session
);
3887 switch_event_t
*fsevent
= NULL
;
3890 unsigned char b64_key
[512] = "";
3892 unsigned char *keysalt
= NULL
;
3893 switch_size_t keysalt_len
= 0;
3895 switch_crypto_key_material_t
*key_material
= NULL
;
3896 unsigned long *key_material_n
= NULL
;
3897 srtp_master_key_t
**mkis
= NULL
;
3898 srtp_master_key_t
*mki
= NULL
;
3901 keysalt_len
= switch_core_media_crypto_keysalt_len(ssec
->crypto_type
);
3903 if (direction
>= SWITCH_RTP_CRYPTO_MAX
|| keysalt_len
> SWITCH_RTP_MAX_CRYPTO_LEN
) {
3904 return SWITCH_STATUS_FALSE
;
3907 if (direction
== SWITCH_RTP_CRYPTO_RECV_RTCP
) {
3908 direction
= SWITCH_RTP_CRYPTO_RECV
;
3909 rtp_session
->srtp_idx_rtcp
= idx
= 1;
3910 } else if (direction
== SWITCH_RTP_CRYPTO_SEND_RTCP
) {
3911 direction
= SWITCH_RTP_CRYPTO_SEND
;
3912 rtp_session
->srtp_idx_rtcp
= idx
= 1;
3915 if (direction
== SWITCH_RTP_CRYPTO_RECV
) {
3916 policy
= &rtp_session
->recv_policy
[idx
];
3917 keysalt
= ssec
->remote_raw_key
;
3918 key_material
= ssec
->remote_key_material_next
;
3919 key_material_n
= &ssec
->remote_key_material_n
;
3921 policy
= &rtp_session
->send_policy
[idx
];
3922 keysalt
= ssec
->local_raw_key
;
3923 key_material
= ssec
->local_key_material_next
;
3924 key_material_n
= &ssec
->local_key_material_n
;
3927 switch_b64_encode(keysalt
, keysalt_len
, b64_key
, sizeof(b64_key
));
3929 if (switch_true(switch_core_get_variable("rtp_retain_crypto_keys"))) {
3931 case SWITCH_RTP_CRYPTO_SEND
:
3932 switch_channel_set_variable(channel
, "srtp_local_crypto_key", (const char *)b64_key
);
3934 case SWITCH_RTP_CRYPTO_RECV
:
3935 switch_channel_set_variable(channel
, "srtp_remote_crypto_key", (const char *)b64_key
);
3937 case SWITCH_RTP_CRYPTO_SEND_RTCP
:
3938 switch_channel_set_variable(channel
, "srtcp_local_crypto_key", (const char *)b64_key
);
3940 case SWITCH_RTP_CRYPTO_RECV_RTCP
:
3941 switch_channel_set_variable(channel
, "srtcp_remote_crypto_key", (const char *)b64_key
);
3947 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
3949 case SWITCH_RTP_CRYPTO_SEND
:
3950 switch_channel_set_variable(channel
, "srtp_local_video_crypto_key", (const char *)b64_key
);
3952 case SWITCH_RTP_CRYPTO_RECV
:
3953 switch_channel_set_variable(channel
, "srtp_remote_video_crypto_key", (const char *)b64_key
);
3955 case SWITCH_RTP_CRYPTO_SEND_RTCP
:
3956 switch_channel_set_variable(channel
, "srtcp_local_video_crypto_key", (const char *)b64_key
);
3958 case SWITCH_RTP_CRYPTO_RECV_RTCP
:
3959 switch_channel_set_variable(channel
, "srtcp_remote_video_crypto_key", (const char *)b64_key
);
3967 case SWITCH_RTP_CRYPTO_SEND
:
3968 switch_channel_set_variable(channel
, "srtp_local_audio_crypto_key", (const char *)b64_key
);
3970 case SWITCH_RTP_CRYPTO_RECV
:
3971 switch_channel_set_variable(channel
, "srtp_remote_audio_crypto_key", (const char *)b64_key
);
3973 case SWITCH_RTP_CRYPTO_SEND_RTCP
:
3974 switch_channel_set_variable(channel
, "srtcp_local_audio_crypto_key", (const char *)b64_key
);
3976 case SWITCH_RTP_CRYPTO_RECV_RTCP
:
3977 switch_channel_set_variable(channel
, "srtcp_remote_audio_crypto_key", (const char *)b64_key
);
3985 crypto_key
= switch_core_alloc(rtp_session
->pool
, sizeof(*crypto_key
));
3987 crypto_key
->type
= ssec
->crypto_type
;
3988 crypto_key
->index
= index
;
3989 memcpy(crypto_key
->keysalt
, keysalt
, keysalt_len
);
3990 crypto_key
->next
= rtp_session
->crypto_keys
[direction
];
3991 rtp_session
->crypto_keys
[direction
] = crypto_key
;
3993 memset(policy
, 0, sizeof(*policy
));
3995 /* many devices can't handle gaps in SRTP streams */
3996 if (!((var
= switch_channel_get_variable(channel
, "srtp_allow_idle_gaps"))
3997 && switch_true(var
))
3998 && (!(var
= switch_channel_get_variable(channel
, "send_silence_when_idle"))
4000 switch_channel_set_variable(channel
, "send_silence_when_idle", "-1");
4003 switch (crypto_key
->type
) {
4004 case AES_CM_128_HMAC_SHA1_80
:
4005 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy
->rtp
);
4006 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy
->rtcp
);
4008 if (switch_channel_direction(channel
) == SWITCH_CALL_DIRECTION_OUTBOUND
) {
4009 switch_channel_set_variable(channel
, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_80");
4012 case AES_CM_128_HMAC_SHA1_32
:
4013 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy
->rtp
);
4014 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy
->rtcp
);
4017 if (switch_channel_direction(channel
) == SWITCH_CALL_DIRECTION_OUTBOUND
) {
4018 switch_channel_set_variable(channel
, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_32");
4022 case AEAD_AES_256_GCM_8
:
4023 srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy
->rtp
);
4024 srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy
->rtcp
);
4026 if (switch_channel_direction(channel
) == SWITCH_CALL_DIRECTION_OUTBOUND
) {
4027 switch_channel_set_variable(channel
, "rtp_has_crypto", "AEAD_AES_256_GCM_8");
4031 case AEAD_AES_256_GCM
:
4032 srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy
->rtp
);
4033 srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy
->rtcp
);
4035 if (switch_channel_direction(channel
) == SWITCH_CALL_DIRECTION_OUTBOUND
) {
4036 switch_channel_set_variable(channel
, "rtp_has_crypto", "AEAD_AES_256_GCM");
4040 case AEAD_AES_128_GCM_8
:
4041 srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy
->rtp
);
4042 srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy
->rtcp
);
4044 if (switch_channel_direction(channel
) == SWITCH_CALL_DIRECTION_OUTBOUND
) {
4045 switch_channel_set_variable(channel
, "rtp_has_crypto", "AEAD_AES_128_GCM_8");
4049 case AEAD_AES_128_GCM
:
4050 srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy
->rtp
);
4051 srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy
->rtcp
);
4053 if (switch_channel_direction(channel
) == SWITCH_CALL_DIRECTION_OUTBOUND
) {
4054 switch_channel_set_variable(channel
, "rtp_has_crypto", "AEAD_AES_128_GCM");
4058 case AES_CM_256_HMAC_SHA1_80
:
4059 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy
->rtp
);
4060 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy
->rtcp
);
4061 if (switch_channel_direction(channel
) == SWITCH_CALL_DIRECTION_OUTBOUND
) {
4062 switch_channel_set_variable(channel
, "rtp_has_crypto", "AES_CM_256_HMAC_SHA1_80");
4065 case AES_CM_256_HMAC_SHA1_32
:
4066 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy
->rtp
);
4067 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy
->rtcp
);
4068 if (switch_channel_direction(channel
) == SWITCH_CALL_DIRECTION_OUTBOUND
) {
4069 switch_channel_set_variable(channel
, "rtp_has_crypto", "AES_CM_256_HMAC_SHA1_32");
4072 case AES_CM_128_NULL_AUTH
:
4073 srtp_crypto_policy_set_aes_cm_128_null_auth(&policy
->rtp
);
4074 srtp_crypto_policy_set_aes_cm_128_null_auth(&policy
->rtcp
);
4076 if (switch_channel_direction(channel
) == SWITCH_CALL_DIRECTION_OUTBOUND
) {
4077 switch_channel_set_variable(channel
, "rtp_has_crypto", "AES_CM_128_NULL_AUTH");
4084 /* Setup the policy with MKI if they are used. Number of key materials must be positive to use MKI. */
4085 if (key_material
&& (*key_material_n
> 0)) {
4087 if (direction
== SWITCH_RTP_CRYPTO_RECV
) {
4088 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV_MKI
] = 1; /* tell the rest of the environment MKI is used */
4090 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND_MKI
] = 1; /* tell the rest of the environment MKI is used */
4093 /* key must be NULL for libsrtp to work correctly with MKI. */
4096 /* Allocate array for MKIs. */
4097 mkis
= switch_core_alloc(rtp_session
->pool
, *key_material_n
* sizeof(srtp_master_key_t
*));
4099 return SWITCH_STATUS_FALSE
;
4102 /* Build array of MKIs. */
4105 while (key_material
&& (mki_idx
< *key_material_n
)) {
4107 if (key_material
->mki_size
< 1) {
4108 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "MKI bad key size at MKI %d\n", mki_idx
);
4109 return SWITCH_STATUS_FALSE
;
4112 mki
= switch_core_alloc(rtp_session
->pool
, sizeof(srtp_master_key_t
));
4114 return SWITCH_STATUS_FALSE
;
4118 mki
->mki_id
= switch_core_alloc(rtp_session
->pool
, sizeof(key_material
->mki_size
));
4120 return SWITCH_STATUS_FALSE
;
4123 mki
->key
= switch_core_alloc(rtp_session
->pool
, keysalt_len
);
4125 return SWITCH_STATUS_FALSE
;
4128 memcpy(mki
->mki_id
, &key_material
->mki_id
, key_material
->mki_size
);
4129 mki
->mki_size
= key_material
->mki_size
;
4130 memcpy(mki
->key
, key_material
->raw_key
, keysalt_len
);
4132 mkis
[mki_idx
] = mki
;
4134 key_material
= key_material
->next
;
4138 /* And pass the array of MKIs to libsrtp. */
4139 policy
->keys
= mkis
;
4140 policy
->num_master_keys
= mki_idx
;
4143 policy
->key
= (uint8_t *) crypto_key
->keysalt
;
4146 policy
->next
= NULL
;
4148 policy
->window_size
= 1024;
4149 policy
->allow_repeat_tx
= 1;
4151 //policy->rtp.sec_serv = sec_serv_conf_and_auth;
4152 //policy->rtcp.sec_serv = sec_serv_conf_and_auth;
4154 switch (direction
) {
4155 case SWITCH_RTP_CRYPTO_RECV
:
4156 policy
->ssrc
.type
= ssrc_any_inbound
;
4158 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV
] && idx
== 0 && rtp_session
->recv_ctx
[idx
]) {
4159 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV_RESET
] = 1;
4161 if ((stat
= srtp_create(&rtp_session
->recv_ctx
[idx
], policy
)) || !rtp_session
->recv_ctx
[idx
]) {
4162 status
= SWITCH_STATUS_FALSE
;
4165 if (status
== SWITCH_STATUS_SUCCESS
) {
4166 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_INFO
, "Activating %s Secure %s RECV%s\n",
4167 rtp_type(rtp_session
), idx
? "RTCP" : "RTP", rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV_MKI
] ? " (with MKI)" : "");
4168 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV
] = 1;
4170 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Error allocating srtp [%d]\n", stat
);
4175 case SWITCH_RTP_CRYPTO_SEND
:
4176 policy
->ssrc
.type
= ssrc_any_outbound
;
4177 //policy->ssrc.type = ssrc_specific;
4178 //policy->ssrc.value = rtp_session->ssrc;
4180 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND
] && idx
== 0 && rtp_session
->send_ctx
[idx
]) {
4181 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND_RESET
] = 1;
4183 if ((stat
= srtp_create(&rtp_session
->send_ctx
[idx
], policy
)) || !rtp_session
->send_ctx
[idx
]) {
4184 status
= SWITCH_STATUS_FALSE
;
4187 if (status
== SWITCH_STATUS_SUCCESS
) {
4188 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_INFO
, "Activating %s Secure %s SEND%s\n",
4189 rtp_type(rtp_session
), idx
? "RTCP" : "RTP", rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND_MKI
] ? " (with MKI)" : "");
4190 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND
] = 1;
4192 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Error allocating SRTP [%d]\n", stat
);
4203 if (switch_event_create(&fsevent
, SWITCH_EVENT_CALL_SECURE
) == SWITCH_STATUS_SUCCESS
) {
4204 if (rtp_session
->dtls
) {
4205 switch_event_add_header(fsevent
, SWITCH_STACK_BOTTOM
, "secure_type", "srtp:dtls:AES_CM_128_HMAC_SHA1_80");
4206 switch_channel_set_variable(channel
, "rtp_has_crypto", "srtp:dtls:AES_CM_128_HMAC_SHA1_80");
4208 switch_event_add_header(fsevent
, SWITCH_STACK_BOTTOM
, "secure_type", "srtp:sdes:%s", switch_channel_get_variable(channel
, "rtp_has_crypto"));
4210 switch_event_add_header_string(fsevent
, SWITCH_STACK_BOTTOM
, "caller-unique-id", switch_channel_get_uuid(channel
));
4211 switch_event_fire(&fsevent
);
4215 return SWITCH_STATUS_SUCCESS
;
4219 SWITCH_DECLARE(switch_status_t
) switch_rtp_set_interval(switch_rtp_t
*rtp_session
, uint32_t ms_per_packet
, uint32_t samples_per_interval
)
4221 rtp_session
->ms_per_packet
= ms_per_packet
;
4222 rtp_session
->samples_per_interval
= rtp_session
->conf_samples_per_interval
= samples_per_interval
;
4223 rtp_session
->missed_count
= 0;
4224 rtp_session
->samples_per_second
=
4225 (uint32_t) ((double) (1000.0f
/ (double) (rtp_session
->ms_per_packet
/ 1000)) * (double) rtp_session
->samples_per_interval
);
4227 rtp_session
->one_second
= (rtp_session
->samples_per_second
/ rtp_session
->samples_per_interval
);
4229 return SWITCH_STATUS_SUCCESS
;
4232 SWITCH_DECLARE(switch_status_t
) switch_rtp_change_interval(switch_rtp_t
*rtp_session
, uint32_t ms_per_packet
, uint32_t samples_per_interval
)
4234 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
4235 int change_timer
= 0;
4237 if (rtp_session
->ms_per_packet
!= ms_per_packet
|| rtp_session
->samples_per_interval
!= samples_per_interval
) {
4241 switch_rtp_set_interval(rtp_session
, ms_per_packet
, samples_per_interval
);
4243 if (change_timer
&& rtp_session
->timer_name
) {
4244 READ_INC(rtp_session
);
4245 WRITE_INC(rtp_session
);
4247 if (rtp_session
->timer
.timer_interface
) {
4248 switch_core_timer_destroy(&rtp_session
->timer
);
4251 if (rtp_session
->write_timer
.timer_interface
) {
4252 switch_core_timer_destroy(&rtp_session
->write_timer
);
4255 if ((status
= switch_core_timer_init(&rtp_session
->timer
,
4256 rtp_session
->timer_name
, ms_per_packet
/ 1000,
4257 samples_per_interval
, rtp_session
->pool
)) == SWITCH_STATUS_SUCCESS
) {
4259 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
,
4260 "RE-Starting timer [%s] %d bytes per %dms\n", rtp_session
->timer_name
, samples_per_interval
, ms_per_packet
/ 1000);
4261 switch_core_timer_init(&rtp_session
->write_timer
, rtp_session
->timer_name
, (ms_per_packet
/ 1000), samples_per_interval
, rtp_session
->pool
);
4264 memset(&rtp_session
->timer
, 0, sizeof(rtp_session
->timer
));
4265 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
,
4266 "Problem RE-Starting timer [%s] %d bytes per %dms\n", rtp_session
->timer_name
, samples_per_interval
, ms_per_packet
/ 1000);
4269 WRITE_DEC(rtp_session
);
4270 READ_DEC(rtp_session
);
4276 SWITCH_DECLARE(switch_status_t
) switch_rtp_set_ssrc(switch_rtp_t
*rtp_session
, uint32_t ssrc
)
4278 rtp_session
->ssrc
= ssrc
;
4279 rtp_session
->send_msg
.header
.ssrc
= htonl(rtp_session
->ssrc
);
4281 return SWITCH_STATUS_SUCCESS
;
4284 SWITCH_DECLARE(switch_status_t
) switch_rtp_set_remote_ssrc(switch_rtp_t
*rtp_session
, uint32_t ssrc
)
4286 rtp_session
->remote_ssrc
= ssrc
;
4287 rtp_session
->flags
[SWITCH_RTP_FLAG_DETECT_SSRC
] = 0;
4288 return SWITCH_STATUS_SUCCESS
;
4291 SWITCH_DECLARE(switch_status_t
) switch_rtp_create(switch_rtp_t
**new_rtp_session
,
4292 switch_payload_t payload
,
4293 uint32_t samples_per_interval
,
4294 uint32_t ms_per_packet
,
4295 switch_rtp_flag_t flags
[SWITCH_RTP_FLAG_INVALID
], char *timer_name
, const char **err
, switch_memory_pool_t
*pool
)
4297 switch_rtp_t
*rtp_session
= NULL
;
4298 switch_core_session_t
*session
= switch_core_memory_pool_get_data(pool
, "__session");
4299 switch_channel_t
*channel
= NULL
;
4301 if (session
) channel
= switch_core_session_get_channel(session
);
4303 *new_rtp_session
= NULL
;
4305 if (samples_per_interval
> SWITCH_RTP_MAX_BUF_LEN
) {
4306 *err
= "Packet Size Too Large!";
4307 return SWITCH_STATUS_FALSE
;
4310 if (!(rtp_session
= switch_core_alloc(pool
, sizeof(*rtp_session
)))) {
4311 *err
= "Memory Error!";
4312 return SWITCH_STATUS_MEMERR
;
4315 rtp_session
->pool
= pool
;
4316 rtp_session
->te
= INVALID_PT
;
4317 rtp_session
->recv_te
= INVALID_PT
;
4318 rtp_session
->cng_pt
= INVALID_PT
;
4319 rtp_session
->session
= session
;
4321 switch_mutex_init(&rtp_session
->flag_mutex
, SWITCH_MUTEX_NESTED
, pool
);
4322 switch_mutex_init(&rtp_session
->read_mutex
, SWITCH_MUTEX_NESTED
, pool
);
4323 switch_mutex_init(&rtp_session
->write_mutex
, SWITCH_MUTEX_NESTED
, pool
);
4324 switch_mutex_init(&rtp_session
->ice_mutex
, SWITCH_MUTEX_NESTED
, pool
);
4325 switch_mutex_init(&rtp_session
->dtmf_data
.dtmf_mutex
, SWITCH_MUTEX_NESTED
, pool
);
4326 switch_queue_create(&rtp_session
->dtmf_data
.dtmf_queue
, 100, rtp_session
->pool
);
4327 switch_queue_create(&rtp_session
->dtmf_data
.dtmf_inqueue
, 100, rtp_session
->pool
);
4329 switch_rtp_set_flags(rtp_session
, flags
);
4331 /* for from address on recvfrom calls */
4332 switch_sockaddr_create(&rtp_session
->from_addr
, pool
);
4333 switch_sockaddr_create(&rtp_session
->rtp_from_addr
, pool
);
4335 if (rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
4336 switch_sockaddr_create(&rtp_session
->rtcp_from_addr
, pool
);
4338 rtp_session
->seq
= (uint16_t) rand();
4339 rtp_session
->ssrc
= (uint32_t) ((intptr_t) rtp_session
+ (uint32_t) switch_epoch_time_now(NULL
));
4340 #ifdef DEBUG_TS_ROLLOVER
4341 rtp_session
->last_write_ts
= TS_ROLLOVER_START
;
4343 rtp_session
->stats
.inbound
.R
= 100.0;
4344 rtp_session
->stats
.inbound
.mos
= 4.5;
4345 rtp_session
->send_msg
.header
.ssrc
= htonl(rtp_session
->ssrc
);
4346 rtp_session
->send_msg
.header
.ts
= 0;
4347 rtp_session
->send_msg
.header
.m
= 0;
4348 rtp_session
->send_msg
.header
.pt
= (switch_payload_t
) htonl(payload
);
4349 rtp_session
->send_msg
.header
.version
= 2;
4350 rtp_session
->send_msg
.header
.p
= 0;
4351 rtp_session
->send_msg
.header
.x
= 0;
4352 rtp_session
->send_msg
.header
.cc
= 0;
4354 rtp_session
->recv_msg
.header
.ssrc
= 0;
4355 rtp_session
->recv_msg
.header
.ts
= 0;
4356 rtp_session
->recv_msg
.header
.seq
= 0;
4357 rtp_session
->recv_msg
.header
.m
= 0;
4358 rtp_session
->recv_msg
.header
.pt
= (switch_payload_t
) htonl(payload
);
4359 rtp_session
->recv_msg
.header
.version
= 2;
4360 rtp_session
->recv_msg
.header
.p
= 0;
4361 rtp_session
->recv_msg
.header
.x
= 0;
4362 rtp_session
->recv_msg
.header
.cc
= 0;
4364 rtp_session
->payload
= payload
;
4365 rtp_session
->rtcp_last_sent
= switch_micro_time_now();
4367 switch_rtp_set_interval(rtp_session
, ms_per_packet
, samples_per_interval
);
4368 rtp_session
->conf_samples_per_interval
= samples_per_interval
;
4370 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
] && zstr(timer_name
)) {
4371 timer_name
= "soft";
4374 if (!zstr(timer_name
) && !strcasecmp(timer_name
, "none")) {
4378 if (!zstr(timer_name
)) {
4379 rtp_session
->timer_name
= switch_core_strdup(pool
, timer_name
);
4380 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_USE_TIMER
);
4381 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_NOBLOCK
);
4383 if (switch_core_timer_init(&rtp_session
->timer
, timer_name
, ms_per_packet
/ 1000, samples_per_interval
, pool
) == SWITCH_STATUS_SUCCESS
) {
4384 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
,
4385 "Starting timer [%s] %d bytes per %dms\n", timer_name
, samples_per_interval
, ms_per_packet
/ 1000);
4386 switch_core_timer_init(&rtp_session
->write_timer
, timer_name
, (ms_per_packet
/ 1000), samples_per_interval
, pool
);
4387 #ifdef DEBUG_TS_ROLLOVER
4388 rtp_session
->timer
.tick
= TS_ROLLOVER_START
/ samples_per_interval
;
4391 memset(&rtp_session
->timer
, 0, sizeof(rtp_session
->timer
));
4392 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
,
4393 "Error Starting timer [%s] %d bytes per %dms, async RTP disabled\n", timer_name
, samples_per_interval
, ms_per_packet
/ 1000);
4394 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_USE_TIMER
);
4397 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
4398 if (switch_core_timer_init(&rtp_session
->timer
, "soft", 1, 90, pool
) == SWITCH_STATUS_SUCCESS
) {
4399 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
, "Starting video timer.\n");
4402 //switch_jb_create(&rtp_session->vb, 3, 10, rtp_session->pool);
4403 //switch_jb_debug_level(rtp_session->vb, 10);
4406 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
, "Not using a timer\n");
4409 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_USE_TIMER
);
4410 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_NOBLOCK
);
4415 switch_channel_set_private(channel
, "__rtcp_audio_rtp_session", rtp_session
);
4420 rtp_session
->stats
.inbound
.last_proc_time
= switch_micro_time_now() / 1000;
4421 rtp_session
->stats
.inbound
.jitter_n
= 0;
4422 rtp_session
->stats
.inbound
.jitter_add
= 0;
4423 rtp_session
->stats
.inbound
.jitter_addsq
= 0;
4424 rtp_session
->stats
.inbound
.min_variance
= 0;
4425 rtp_session
->stats
.inbound
.max_variance
= 0;
4427 /* Burst and Packet Loss */
4428 rtp_session
->stats
.inbound
.lossrate
= 0;
4429 rtp_session
->stats
.inbound
.burstrate
= 0;
4430 memset(rtp_session
->stats
.inbound
.loss
, 0, sizeof(rtp_session
->stats
.inbound
.loss
));
4431 rtp_session
->stats
.inbound
.last_loss
= 0;
4432 rtp_session
->stats
.inbound
.last_processed_seq
= -1;
4434 rtp_session
->ready
= 1;
4435 *new_rtp_session
= rtp_session
;
4437 return SWITCH_STATUS_SUCCESS
;
4440 SWITCH_DECLARE(switch_rtp_t
*) switch_rtp_new(const char *rx_host
,
4441 switch_port_t rx_port
,
4442 const char *tx_host
,
4443 switch_port_t tx_port
,
4444 switch_payload_t payload
,
4445 uint32_t samples_per_interval
,
4446 uint32_t ms_per_packet
,
4447 switch_rtp_flag_t flags
[SWITCH_RTP_FLAG_INVALID
], char *timer_name
, const char **err
, switch_memory_pool_t
*pool
,
4448 switch_port_t bundle_internal_port
,
4449 switch_port_t bundle_external_port
)
4451 switch_rtp_t
*rtp_session
= NULL
;
4453 if (zstr(rx_host
)) {
4454 *err
= "Missing local host";
4459 *err
= "Missing local port";
4463 if (zstr(tx_host
)) {
4464 *err
= "Missing remote host";
4469 *err
= "Missing remote port";
4473 if (switch_rtp_create(&rtp_session
, payload
, samples_per_interval
, ms_per_packet
, flags
, timer_name
, err
, pool
) != SWITCH_STATUS_SUCCESS
) {
4477 switch_mutex_lock(rtp_session
->flag_mutex
);
4479 if (switch_rtp_set_local_address(rtp_session
, rx_host
, rx_port
, err
) != SWITCH_STATUS_SUCCESS
) {
4480 switch_mutex_unlock(rtp_session
->flag_mutex
);
4485 if (switch_rtp_set_remote_address(rtp_session
, tx_host
, tx_port
, 0, SWITCH_TRUE
, err
) != SWITCH_STATUS_SUCCESS
) {
4486 switch_mutex_unlock(rtp_session
->flag_mutex
);
4494 switch_mutex_unlock(rtp_session
->flag_mutex
);
4495 rtp_session
->ready
= 2;
4496 rtp_session
->rx_host
= switch_core_strdup(rtp_session
->pool
, rx_host
);
4497 rtp_session
->rx_port
= rx_port
;
4498 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_FLUSH
);
4499 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_DETECT_SSRC
);
4501 switch_rtp_release_port(rx_host
, rx_port
);
4507 SWITCH_DECLARE(void) switch_rtp_set_telephony_event(switch_rtp_t
*rtp_session
, switch_payload_t te
)
4510 rtp_session
->te
= te
;
4515 SWITCH_DECLARE(void) switch_rtp_set_telephony_recv_event(switch_rtp_t
*rtp_session
, switch_payload_t te
)
4518 rtp_session
->recv_te
= te
;
4523 SWITCH_DECLARE(void) switch_rtp_set_cng_pt(switch_rtp_t
*rtp_session
, switch_payload_t pt
)
4525 rtp_session
->cng_pt
= pt
;
4526 rtp_session
->flags
[SWITCH_RTP_FLAG_AUTO_CNG
] = 1;
4529 SWITCH_DECLARE(switch_timer_t
*) switch_rtp_get_media_timer(switch_rtp_t
*rtp_session
)
4532 if (rtp_session
&& rtp_session
->timer
.timer_interface
) {
4533 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
4534 switch_core_timer_sync(&rtp_session
->timer
);
4536 return &rtp_session
->timer
;
4543 SWITCH_DECLARE(switch_jb_t
*) switch_rtp_get_jitter_buffer(switch_rtp_t
*rtp_session
)
4545 if (!switch_rtp_ready(rtp_session
)) {
4549 return rtp_session
->jb
? rtp_session
->jb
: rtp_session
->vb
;
4552 SWITCH_DECLARE(switch_status_t
) switch_rtp_pause_jitter_buffer(switch_rtp_t
*rtp_session
, switch_bool_t pause
)
4556 if (rtp_session
->pause_jb
&& !pause
) {
4557 if (rtp_session
->jb
) {
4558 switch_jb_reset(rtp_session
->jb
);
4561 if (rtp_session
->vb
) {
4562 switch_jb_reset(rtp_session
->vb
);
4566 new_val
= pause
? 1 : -1;
4568 if (rtp_session
->pause_jb
+ new_val
> -1) {
4569 rtp_session
->pause_jb
+= new_val
;
4572 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
,
4573 "Jitterbuffer %s is %s\n", rtp_type(rtp_session
), rtp_session
->pause_jb
? "paused" : "enabled");
4575 return SWITCH_STATUS_SUCCESS
;
4578 SWITCH_DECLARE(switch_status_t
) switch_rtp_deactivate_jitter_buffer(switch_rtp_t
*rtp_session
)
4581 if (!switch_rtp_ready(rtp_session
)) {
4582 return SWITCH_STATUS_FALSE
;
4585 rtp_session
->flags
[SWITCH_RTP_FLAG_KILL_JB
]++;
4587 return SWITCH_STATUS_SUCCESS
;
4590 SWITCH_DECLARE(switch_status_t
) switch_rtp_get_video_buffer_size(switch_rtp_t
*rtp_session
, uint32_t *min_frame_len
, uint32_t *max_frame_len
, uint32_t *cur_frame_len
, uint32_t *highest_frame_len
)
4593 if (rtp_session
->vb
) {
4594 return switch_jb_get_frames(rtp_session
->vb
, min_frame_len
, max_frame_len
, cur_frame_len
, highest_frame_len
);
4597 return SWITCH_STATUS_FALSE
;
4600 SWITCH_DECLARE(switch_status_t
) switch_rtp_set_video_buffer_size(switch_rtp_t
*rtp_session
, uint32_t frames
, uint32_t max_frames
)
4602 if (!switch_rtp_ready(rtp_session
)) {
4603 return SWITCH_STATUS_FALSE
;
4607 max_frames
= rtp_session
->last_max_vb_frames
;
4610 if (!max_frames
|| frames
>= max_frames
) {
4611 max_frames
= frames
* 10;
4614 rtp_session
->last_max_vb_frames
= max_frames
;
4616 if (!rtp_session
->vb
) {
4617 switch_jb_create(&rtp_session
->vb
, rtp_session
->flags
[SWITCH_RTP_FLAG_TEXT
] ? SJB_TEXT
: SJB_VIDEO
, frames
, max_frames
, rtp_session
->pool
);
4618 switch_jb_set_session(rtp_session
->vb
, rtp_session
->session
);
4620 switch_jb_set_frames(rtp_session
->vb
, frames
, max_frames
);
4623 //switch_rtp_flush(rtp_session);
4624 switch_core_session_request_video_refresh(rtp_session
->session
);
4626 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
, "Setting video buffer %u Frames.\n", frames
);
4628 return SWITCH_STATUS_SUCCESS
;
4631 SWITCH_DECLARE(switch_status_t
) switch_rtp_debug_jitter_buffer(switch_rtp_t
*rtp_session
, const char *name
)
4635 if (!switch_rtp_ready(rtp_session
)) {
4636 return SWITCH_STATUS_FALSE
;
4639 if (name
) x
= atoi(name
);
4642 if (rtp_session
->jb
) {
4643 switch_jb_debug_level(rtp_session
->jb
, x
);
4644 } else if (rtp_session
->vb
) {
4645 switch_jb_debug_level(rtp_session
->vb
, x
);
4648 return SWITCH_STATUS_SUCCESS
;
4651 SWITCH_DECLARE(switch_status_t
) switch_rtp_activate_jitter_buffer(switch_rtp_t
*rtp_session
,
4652 uint32_t queue_frames
,
4653 uint32_t max_queue_frames
,
4654 uint32_t samples_per_packet
,
4655 uint32_t samples_per_second
)
4657 switch_status_t status
= SWITCH_STATUS_FALSE
;
4659 if (!switch_rtp_ready(rtp_session
)) {
4660 return SWITCH_STATUS_FALSE
;
4663 if (queue_frames
< 1) {
4667 if (max_queue_frames
< queue_frames
) {
4668 max_queue_frames
= queue_frames
* 3;
4673 if (rtp_session
->jb
) {
4674 status
= switch_jb_set_frames(rtp_session
->jb
, queue_frames
, max_queue_frames
);
4676 READ_INC(rtp_session
);
4677 status
= switch_jb_create(&rtp_session
->jb
, SJB_AUDIO
, queue_frames
, max_queue_frames
, rtp_session
->pool
);
4678 switch_jb_set_session(rtp_session
->jb
, rtp_session
->session
);
4679 switch_jb_set_jitter_estimator(rtp_session
->jb
, &rtp_session
->stats
.rtcp
.inter_jitter
, samples_per_packet
, samples_per_second
);
4680 if (switch_true(switch_channel_get_variable_dup(switch_core_session_get_channel(rtp_session
->session
), "jb_use_timestamps", SWITCH_FALSE
, -1))) {
4681 switch_jb_ts_mode(rtp_session
->jb
, samples_per_packet
, samples_per_second
);
4683 //switch_jb_debug_level(rtp_session->jb, 10);
4684 READ_DEC(rtp_session
);
4692 SWITCH_DECLARE(switch_status_t
) switch_rtp_activate_rtcp(switch_rtp_t
*rtp_session
, int send_rate
, switch_port_t remote_port
, switch_bool_t mux
)
4694 const char *err
= NULL
;
4696 if (!rtp_session
->ms_per_packet
) {
4697 return SWITCH_STATUS_FALSE
;
4700 rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
] = 1;
4702 if (!(rtp_session
->remote_rtcp_port
= remote_port
)) {
4703 rtp_session
->remote_rtcp_port
= rtp_session
->remote_port
+ 1;
4707 rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]++;
4711 if (send_rate
== -1) {
4713 rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_PASSTHRU
] = 1;
4714 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
, "RTCP passthru enabled. Remote Port: %d\n", rtp_session
->remote_rtcp_port
);
4716 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
, "RTCP send rate is: %d and packet rate is: %d Remote Port: %d\n", send_rate
, rtp_session
->ms_per_packet
, rtp_session
->remote_rtcp_port
);
4718 rtp_session
->rtcp_interval
= send_rate
;
4721 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
4723 if (switch_sockaddr_info_get(&rtp_session
->rtcp_remote_addr
, rtp_session
->eff_remote_host_str
, SWITCH_UNSPEC
,
4724 rtp_session
->remote_rtcp_port
, 0, rtp_session
->pool
) != SWITCH_STATUS_SUCCESS
|| !rtp_session
->rtcp_remote_addr
) {
4725 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "RTCP MUX Remote Address Error!");
4726 return SWITCH_STATUS_FALSE
;
4729 rtp_session
->rtcp_local_addr
= rtp_session
->local_addr
;
4730 rtp_session
->rtcp_from_addr
= rtp_session
->from_addr
;
4731 rtp_session
->rtcp_sock_input
= rtp_session
->sock_input
;
4732 rtp_session
->rtcp_sock_output
= rtp_session
->sock_output
;
4734 rtp_session
->rtcp_recv_msg_p
= (rtcp_msg_t
*) &rtp_session
->recv_msg
;
4736 return SWITCH_STATUS_SUCCESS
;
4738 //return enable_remote_rtcp_socket(rtp_session, &err);
4740 rtp_session
->rtcp_recv_msg_p
= (rtcp_msg_t
*) &rtp_session
->rtcp_recv_msg
;
4743 return enable_local_rtcp_socket(rtp_session
, &err
) || enable_remote_rtcp_socket(rtp_session
, &err
);
4747 SWITCH_DECLARE(switch_status_t
) switch_rtp_activate_ice(switch_rtp_t
*rtp_session
, char *login
, char *rlogin
,
4748 const char *password
, const char *rpassword
, ice_proto_t proto
,
4749 switch_core_media_ice_type_t type
, ice_t
*ice_params
)
4751 char ice_user
[STUN_USERNAME_MAX_SIZE
];
4752 char user_ice
[STUN_USERNAME_MAX_SIZE
];
4753 char luser_ice
[SDP_UFRAG_MAX_SIZE
];
4754 switch_rtp_ice_t
*ice
;
4756 switch_port_t port
= 0;
4760 switch_mutex_lock(rtp_session
->ice_mutex
);
4762 if (proto
== IPR_RTP
) {
4763 ice
= &rtp_session
->ice
;
4764 rtp_session
->flags
[SWITCH_RTP_FLAG_PAUSE
] = 0;
4765 rtp_session
->flags
[SWITCH_RTP_FLAG_MUTE
] = 0;
4767 ice
= &rtp_session
->rtcp_ice
;
4772 if ((type
& ICE_VANILLA
)) {
4773 switch_snprintf(ice_user
, sizeof(ice_user
), "%s:%s", login
, rlogin
);
4774 switch_snprintf(user_ice
, sizeof(user_ice
), "%s:%s", rlogin
, login
);
4775 switch_snprintf(luser_ice
, sizeof(luser_ice
), "%s%s", rlogin
, login
);
4776 ice
->ready
= ice
->rready
= 0;
4778 switch_snprintf(ice_user
, sizeof(ice_user
), "%s%s", login
, rlogin
);
4779 switch_snprintf(user_ice
, sizeof(user_ice
), "%s%s", rlogin
, login
);
4780 switch_snprintf(luser_ice
, sizeof(luser_ice
), "");
4781 ice
->ready
= ice
->rready
= 1;
4784 ice
->ice_user
= switch_core_strdup(rtp_session
->pool
, ice_user
);
4785 ice
->user_ice
= switch_core_strdup(rtp_session
->pool
, user_ice
);
4786 ice
->luser_ice
= switch_core_strdup(rtp_session
->pool
, luser_ice
);
4788 ice
->ice_params
= ice_params
;
4791 ice
->next_run
= switch_micro_time_now();
4795 ice
->pass
= switch_core_strdup(rtp_session
->pool
, password
);
4799 ice
->rpass
= switch_core_strdup(rtp_session
->pool
, rpassword
);
4802 if ((ice
->type
& ICE_VANILLA
) && ice
->ice_params
) {
4803 host
= ice
->ice_params
->cands
[ice
->ice_params
->chosen
[ice
->proto
]][ice
->proto
].con_addr
;
4804 port
= ice
->ice_params
->cands
[ice
->ice_params
->chosen
[ice
->proto
]][ice
->proto
].con_port
;
4806 if (!host
|| !port
|| switch_sockaddr_info_get(&ice
->addr
, host
, SWITCH_UNSPEC
, port
, 0, rtp_session
->pool
) != SWITCH_STATUS_SUCCESS
|| !ice
->addr
) {
4807 switch_mutex_unlock(rtp_session
->ice_mutex
);
4808 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Error setting remote host!\n");
4809 return SWITCH_STATUS_FALSE
;
4812 if (proto
== IPR_RTP
) {
4813 ice
->addr
= rtp_session
->remote_addr
;
4815 ice
->addr
= rtp_session
->rtcp_remote_addr
;
4818 host
= (char *)switch_get_addr(bufc
, sizeof(bufc
), ice
->addr
);
4819 port
= switch_sockaddr_get_port(ice
->addr
);
4822 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_NOTICE
, "Activating %s %s ICE: %s %s:%d\n",
4823 proto
== IPR_RTP
? "RTP" : "RTCP", rtp_type(rtp_session
), ice_user
, host
, port
);
4826 rtp_session
->rtp_bugs
|= RTP_BUG_ACCEPT_ANY_PACKETS
;
4829 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
4830 rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO_BREAK
] = 1;
4831 switch_rtp_break(rtp_session
);
4834 switch_mutex_unlock(rtp_session
->ice_mutex
);
4836 return SWITCH_STATUS_SUCCESS
;
4840 SWITCH_DECLARE(void) switch_rtp_flush(switch_rtp_t
*rtp_session
)
4842 if (!switch_rtp_ready(rtp_session
)) {
4846 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_FLUSH
);
4849 SWITCH_DECLARE(switch_status_t
) switch_rtp_req_bitrate(switch_rtp_t
*rtp_session
, uint32_t bps
)
4851 if (!rtp_write_ready(rtp_session
, 0, __LINE__
) || rtp_session
->tmmbr
) {
4852 return SWITCH_STATUS_FALSE
;
4855 rtp_session
->tmmbr
= bps
;
4857 return SWITCH_STATUS_SUCCESS
;
4860 SWITCH_DECLARE(switch_status_t
) switch_rtp_ack_bitrate(switch_rtp_t
*rtp_session
, uint32_t bps
)
4862 if (!rtp_write_ready(rtp_session
, 0, __LINE__
) || rtp_session
->tmmbn
) {
4863 return SWITCH_STATUS_FALSE
;
4866 rtp_session
->tmmbn
= bps
;
4868 return SWITCH_STATUS_SUCCESS
;
4871 SWITCH_DECLARE(void) switch_rtp_video_refresh(switch_rtp_t
*rtp_session
)
4874 if (!rtp_write_ready(rtp_session
, 0, __LINE__
)) {
4878 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && (rtp_session
->ice
.ice_user
|| rtp_session
->flags
[SWITCH_RTP_FLAG_FIR
] || rtp_session
->flags
[SWITCH_RTP_FLAG_OLD_FIR
])) {
4879 rtp_session
->fir_count
= 1;
4883 SWITCH_DECLARE(void) switch_rtp_video_loss(switch_rtp_t
*rtp_session
)
4886 if (!rtp_write_ready(rtp_session
, 0, __LINE__
)) {
4890 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && (rtp_session
->ice
.ice_user
|| rtp_session
->flags
[SWITCH_RTP_FLAG_PLI
])) {
4891 rtp_session
->pli_count
= 1;
4895 SWITCH_DECLARE(void) switch_rtp_break(switch_rtp_t
*rtp_session
)
4897 if (!switch_rtp_ready(rtp_session
)) {
4901 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
4904 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO_BREAK
]) {
4905 rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO_BREAK
] = 0;
4907 } else if (rtp_session
->session
) {
4908 switch_channel_t
*channel
= switch_core_session_get_channel(rtp_session
->session
);
4909 if (switch_channel_test_flag(channel
, CF_VIDEO_BREAK
)) {
4910 switch_channel_clear_flag(channel
, CF_VIDEO_BREAK
);
4917 switch_rtp_video_refresh(rtp_session
);
4920 switch_mutex_lock(rtp_session
->flag_mutex
);
4921 rtp_session
->flags
[SWITCH_RTP_FLAG_BREAK
] = 1;
4923 if (rtp_session
->flags
[SWITCH_RTP_FLAG_NOBLOCK
]) {
4924 switch_mutex_unlock(rtp_session
->flag_mutex
);
4928 if (rtp_session
->sock_input
) {
4929 ping_socket(rtp_session
);
4932 switch_mutex_unlock(rtp_session
->flag_mutex
);
4935 SWITCH_DECLARE(void) switch_rtp_kill_socket(switch_rtp_t
*rtp_session
)
4937 switch_assert(rtp_session
!= NULL
);
4938 switch_mutex_lock(rtp_session
->flag_mutex
);
4939 if (rtp_session
->flags
[SWITCH_RTP_FLAG_IO
]) {
4940 rtp_session
->flags
[SWITCH_RTP_FLAG_IO
] = 0;
4941 if (rtp_session
->sock_input
) {
4942 ping_socket(rtp_session
);
4943 switch_socket_shutdown(rtp_session
->sock_input
, SWITCH_SHUTDOWN_READWRITE
);
4945 if (rtp_session
->sock_output
&& rtp_session
->sock_output
!= rtp_session
->sock_input
) {
4946 switch_socket_shutdown(rtp_session
->sock_output
, SWITCH_SHUTDOWN_READWRITE
);
4949 if (rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
4950 if (rtp_session
->sock_input
&& rtp_session
->rtcp_sock_input
&& rtp_session
->rtcp_sock_input
!= rtp_session
->sock_input
) {
4951 ping_socket(rtp_session
);
4952 switch_socket_shutdown(rtp_session
->rtcp_sock_input
, SWITCH_SHUTDOWN_READWRITE
);
4954 if (rtp_session
->rtcp_sock_output
&& rtp_session
->rtcp_sock_output
!= rtp_session
->sock_output
&& rtp_session
->rtcp_sock_output
!= rtp_session
->rtcp_sock_input
) {
4955 switch_socket_shutdown(rtp_session
->rtcp_sock_output
, SWITCH_SHUTDOWN_READWRITE
);
4959 switch_mutex_unlock(rtp_session
->flag_mutex
);
4962 SWITCH_DECLARE(uint8_t) switch_rtp_ready(switch_rtp_t
*rtp_session
)
4966 if (!rtp_session
|| !rtp_session
->flag_mutex
|| rtp_session
->flags
[SWITCH_RTP_FLAG_SHUTDOWN
]) {
4970 switch_mutex_lock(rtp_session
->flag_mutex
);
4971 ret
= (rtp_session
->flags
[SWITCH_RTP_FLAG_IO
] && rtp_session
->sock_input
&& rtp_session
->sock_output
&& rtp_session
->remote_addr
4972 && rtp_session
->ready
== 2) ? 1 : 0;
4973 switch_mutex_unlock(rtp_session
->flag_mutex
);
4978 SWITCH_DECLARE(switch_status_t
) switch_rtp_sync_stats(switch_rtp_t
*rtp_session
)
4981 return SWITCH_STATUS_FALSE
;
4984 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VAD
]) {
4985 switch_channel_t
*channel
= switch_core_session_get_channel(rtp_session
->vad_data
.session
);
4987 switch_channel_set_variable_printf(channel
, "vad_total_talk_time_ms", "%u", (uint32_t)rtp_session
->vad_data
.total_talk_time
/ 1000);
4988 switch_channel_set_variable_printf(channel
, "vad_total_talk_time_sec", "%u", (uint32_t)rtp_session
->vad_data
.total_talk_time
/ 1000000);
4991 do_mos(rtp_session
);
4993 if (rtp_session
->stats
.inbound
.error_log
&& !rtp_session
->stats
.inbound
.error_log
->stop
) {
4994 rtp_session
->stats
.inbound
.error_log
->stop
= switch_micro_time_now();
4997 return SWITCH_STATUS_SUCCESS
;
5001 SWITCH_DECLARE(void) switch_rtp_destroy(switch_rtp_t
**rtp_session
)
5004 switch_socket_t
*sock
;
5009 if (!rtp_session
|| !*rtp_session
|| !(*rtp_session
)->ready
) {
5013 if ((*rtp_session
)->vb
) {
5014 /* retrieve counter for ALL received NACKed packets */
5015 uint32_t nack_jb_ok
= switch_jb_get_nack_success((*rtp_session
)->vb
);
5016 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG((*rtp_session
)->session
), SWITCH_LOG_DEBUG
,
5017 "NACK: Added to JB: [%u]\n", nack_jb_ok
);
5020 (*rtp_session
)->flags
[SWITCH_RTP_FLAG_SHUTDOWN
] = 1;
5022 READ_INC((*rtp_session
));
5023 WRITE_INC((*rtp_session
));
5025 (*rtp_session
)->ready
= 0;
5027 WRITE_DEC((*rtp_session
));
5028 READ_DEC((*rtp_session
));
5030 if ((*rtp_session
)->flags
[SWITCH_RTP_FLAG_VAD
]) {
5031 switch_rtp_disable_vad(*rtp_session
);
5034 switch_mutex_lock((*rtp_session
)->flag_mutex
);
5036 switch_rtp_kill_socket(*rtp_session
);
5038 while (switch_queue_trypop((*rtp_session
)->dtmf_data
.dtmf_inqueue
, &pop
) == SWITCH_STATUS_SUCCESS
) {
5039 switch_safe_free(pop
);
5042 while (switch_queue_trypop((*rtp_session
)->dtmf_data
.dtmf_queue
, &pop
) == SWITCH_STATUS_SUCCESS
) {
5043 switch_safe_free(pop
);
5046 if ((*rtp_session
)->jb
) {
5047 switch_jb_destroy(&(*rtp_session
)->jb
);
5050 if ((*rtp_session
)->vb
) {
5051 switch_jb_destroy(&(*rtp_session
)->vb
);
5054 if ((*rtp_session
)->vbw
) {
5055 switch_jb_destroy(&(*rtp_session
)->vbw
);
5058 if ((*rtp_session
)->dtls
&& (*rtp_session
)->dtls
== (*rtp_session
)->rtcp_dtls
) {
5059 (*rtp_session
)->rtcp_dtls
= NULL
;
5062 if ((*rtp_session
)->dtls
) {
5063 free_dtls(&(*rtp_session
)->dtls
);
5066 if ((*rtp_session
)->rtcp_dtls
) {
5067 free_dtls(&(*rtp_session
)->rtcp_dtls
);
5070 if ((*rtp_session
)->rtcp_sock_input
== (*rtp_session
)->sock_input
) {
5071 (*rtp_session
)->rtcp_sock_input
= NULL
;
5074 if ((*rtp_session
)->rtcp_sock_output
== (*rtp_session
)->sock_output
) {
5075 (*rtp_session
)->rtcp_sock_output
= NULL
;
5078 sock
= (*rtp_session
)->sock_input
;
5079 (*rtp_session
)->sock_input
= NULL
;
5080 switch_socket_close(sock
);
5082 if ((*rtp_session
)->sock_output
!= sock
) {
5083 sock
= (*rtp_session
)->sock_output
;
5084 (*rtp_session
)->sock_output
= NULL
;
5085 switch_socket_close(sock
);
5088 if ((sock
= (*rtp_session
)->rtcp_sock_input
)) {
5089 (*rtp_session
)->rtcp_sock_input
= NULL
;
5090 switch_socket_close(sock
);
5093 if ((*rtp_session
)->rtcp_sock_output
&& (*rtp_session
)->rtcp_sock_output
!= sock
) {
5094 sock
= (*rtp_session
)->rtcp_sock_output
;
5095 (*rtp_session
)->rtcp_sock_output
= NULL
;
5096 switch_socket_close(sock
);
5100 if ((*rtp_session
)->flags
[SWITCH_RTP_FLAG_SECURE_SEND
]) {
5101 for(x
= 0; x
< 2; x
++) {
5102 if ((*rtp_session
)->send_ctx
[x
]) {
5103 srtp_dealloc((*rtp_session
)->send_ctx
[x
]);
5104 (*rtp_session
)->send_ctx
[x
] = NULL
;
5107 (*rtp_session
)->flags
[SWITCH_RTP_FLAG_SECURE_SEND
] = 0;
5110 if ((*rtp_session
)->flags
[SWITCH_RTP_FLAG_SECURE_RECV
]) {
5111 for (x
= 0; x
< 2; x
++) {
5112 if ((*rtp_session
)->recv_ctx
[x
]) {
5113 srtp_dealloc((*rtp_session
)->recv_ctx
[x
]);
5114 (*rtp_session
)->recv_ctx
[x
] = NULL
;
5117 (*rtp_session
)->flags
[SWITCH_RTP_FLAG_SECURE_RECV
] = 0;
5121 if ((*rtp_session
)->timer
.timer_interface
) {
5122 switch_core_timer_destroy(&(*rtp_session
)->timer
);
5125 if ((*rtp_session
)->write_timer
.timer_interface
) {
5126 switch_core_timer_destroy(&(*rtp_session
)->write_timer
);
5129 switch_rtp_release_port((*rtp_session
)->rx_host
, (*rtp_session
)->rx_port
);
5130 switch_mutex_unlock((*rtp_session
)->flag_mutex
);
5135 SWITCH_DECLARE(void) switch_rtp_set_interdigit_delay(switch_rtp_t
*rtp_session
, uint32_t delay
)
5137 rtp_session
->interdigit_delay
= delay
;
5140 SWITCH_DECLARE(switch_socket_t
*) switch_rtp_get_rtp_socket(switch_rtp_t
*rtp_session
)
5142 return rtp_session
->sock_input
;
5145 SWITCH_DECLARE(uint32_t) switch_rtp_get_default_samples_per_interval(switch_rtp_t
*rtp_session
)
5147 return rtp_session
->samples_per_interval
;
5150 SWITCH_DECLARE(void) switch_rtp_set_default_payload(switch_rtp_t
*rtp_session
, switch_payload_t payload
)
5152 rtp_session
->payload
= payload
;
5155 SWITCH_DECLARE(uint32_t) switch_rtp_get_default_payload(switch_rtp_t
*rtp_session
)
5157 return rtp_session
->payload
;
5160 SWITCH_DECLARE(void) switch_rtp_set_invalid_handler(switch_rtp_t
*rtp_session
, switch_rtp_invalid_handler_t on_invalid
)
5162 rtp_session
->invalid_handler
= on_invalid
;
5165 SWITCH_DECLARE(void) switch_rtp_set_flags(switch_rtp_t
*rtp_session
, switch_rtp_flag_t flags
[SWITCH_RTP_FLAG_INVALID
])
5169 for(i
= 0; i
< SWITCH_RTP_FLAG_INVALID
; i
++) {
5171 switch_rtp_set_flag(rtp_session
, i
);
5176 SWITCH_DECLARE(void) switch_rtp_clear_flags(switch_rtp_t
*rtp_session
, switch_rtp_flag_t flags
[SWITCH_RTP_FLAG_INVALID
])
5180 for(i
= 0; i
< SWITCH_RTP_FLAG_INVALID
; i
++) {
5182 switch_rtp_clear_flag(rtp_session
, i
);
5187 SWITCH_DECLARE(void) switch_rtp_set_flag(switch_rtp_t
*rtp_session
, switch_rtp_flag_t flag
)
5189 int old_flag
= rtp_session
->flags
[flag
];
5191 switch_mutex_lock(rtp_session
->flag_mutex
);
5192 rtp_session
->flags
[flag
] = 1;
5193 switch_mutex_unlock(rtp_session
->flag_mutex
);
5195 if (flag
== SWITCH_RTP_FLAG_PASSTHRU
) {
5197 switch_rtp_pause_jitter_buffer(rtp_session
, SWITCH_TRUE
);
5199 } else if (flag
== SWITCH_RTP_FLAG_DTMF_ON
) {
5200 rtp_session
->stats
.inbound
.last_processed_seq
= 0;
5201 } else if (flag
== SWITCH_RTP_FLAG_FLUSH
) {
5202 reset_jitter_seq(rtp_session
);
5203 } else if (flag
== SWITCH_RTP_FLAG_AUTOADJ
) {
5204 rtp_session
->autoadj_window
= 20;
5205 rtp_session
->autoadj_threshold
= 10;
5206 rtp_session
->autoadj_tally
= 0;
5208 switch_mutex_lock(rtp_session
->flag_mutex
);
5209 rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_AUTOADJ
] = 1;
5210 switch_mutex_unlock(rtp_session
->flag_mutex
);
5212 rtp_session
->rtcp_autoadj_window
= 20;
5213 rtp_session
->rtcp_autoadj_threshold
= 1;
5214 rtp_session
->rtcp_autoadj_tally
= 0;
5216 if (rtp_session
->session
) {
5217 switch_channel_t
*channel
= switch_core_session_get_channel(rtp_session
->session
);
5218 const char *x
= switch_channel_get_variable(channel
, "rtp_auto_adjust_threshold");
5221 if (xn
> 0 && xn
<= 65535) {
5222 rtp_session
->autoadj_window
= xn
*2;
5223 rtp_session
->autoadj_threshold
= xn
;
5229 rtp_flush_read_buffer(rtp_session
, SWITCH_RTP_FLUSH_ONCE
);
5232 if (rtp_session
->jb
) {
5233 switch_jb_reset(rtp_session
->jb
);
5235 } else if (flag
== SWITCH_RTP_FLAG_NOBLOCK
&& rtp_session
->sock_input
) {
5236 switch_socket_opt_set(rtp_session
->sock_input
, SWITCH_SO_NONBLOCK
, TRUE
);
5241 SWITCH_DECLARE(uint32_t) switch_rtp_test_flag(switch_rtp_t
*rtp_session
, switch_rtp_flag_t flags
)
5243 return (uint32_t) rtp_session
->flags
[flags
];
5246 SWITCH_DECLARE(void) switch_rtp_clear_flag(switch_rtp_t
*rtp_session
, switch_rtp_flag_t flag
)
5248 int old_flag
= rtp_session
->flags
[flag
];
5250 switch_mutex_lock(rtp_session
->flag_mutex
);
5251 rtp_session
->flags
[flag
] = 0;
5252 switch_mutex_unlock(rtp_session
->flag_mutex
);
5254 if (flag
== SWITCH_RTP_FLAG_PASSTHRU
) {
5256 switch_rtp_pause_jitter_buffer(rtp_session
, SWITCH_FALSE
);
5258 } else if (flag
== SWITCH_RTP_FLAG_DTMF_ON
) {
5259 rtp_session
->stats
.inbound
.last_processed_seq
= 0;
5260 } else if (flag
== SWITCH_RTP_FLAG_PAUSE
) {
5261 reset_jitter_seq(rtp_session
);
5262 } else if (flag
== SWITCH_RTP_FLAG_NOBLOCK
&& rtp_session
->sock_input
) {
5263 switch_socket_opt_set(rtp_session
->sock_input
, SWITCH_SO_NONBLOCK
, FALSE
);
5267 static void set_dtmf_delay(switch_rtp_t
*rtp_session
, uint32_t ms
, uint32_t max_ms
)
5269 int upsamp
, max_upsamp
;
5272 if (!max_ms
) max_ms
= ms
;
5274 upsamp
= ms
* (rtp_session
->samples_per_second
/ 1000);
5275 max_upsamp
= max_ms
* (rtp_session
->samples_per_second
/ 1000);
5277 rtp_session
->sending_dtmf
= 0;
5278 rtp_session
->queue_delay
= upsamp
;
5280 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
]) {
5281 rtp_session
->max_next_write_samplecount
= rtp_session
->timer
.samplecount
+ max_upsamp
;
5282 rtp_session
->next_write_samplecount
= rtp_session
->timer
.samplecount
+ upsamp
;
5283 rtp_session
->last_write_ts
+= upsamp
;
5286 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
, "Queue digit delay of %dms\n", ms
);
5289 static void do_2833(switch_rtp_t
*rtp_session
)
5291 switch_frame_flag_t flags
= 0;
5292 uint32_t samples
= rtp_session
->samples_per_interval
;
5294 if (rtp_session
->dtmf_data
.out_digit_dur
> 0) {
5297 if (!rtp_session
->last_write_ts
) {
5298 if (rtp_session
->timer
.timer_interface
) {
5299 //switch_core_timer_sync(&rtp_session->write_timer);
5300 rtp_session
->last_write_ts
= rtp_session
->write_timer
.samplecount
;
5302 rtp_session
->last_write_ts
= rtp_session
->samples_per_interval
;
5306 rtp_session
->dtmf_data
.out_digit_sofar
+= samples
;
5307 rtp_session
->dtmf_data
.out_digit_sub_sofar
+= samples
;
5309 if (rtp_session
->dtmf_data
.out_digit_sub_sofar
> 0xFFFF) {
5310 rtp_session
->dtmf_data
.out_digit_sub_sofar
= samples
;
5311 rtp_session
->dtmf_data
.timestamp_dtmf
+= 0xFFFF;
5314 if (rtp_session
->dtmf_data
.out_digit_sofar
>= rtp_session
->dtmf_data
.out_digit_dur
) {
5315 rtp_session
->dtmf_data
.out_digit_packet
[1] |= 0x80;
5319 rtp_session
->dtmf_data
.out_digit_packet
[2] = (unsigned char) (rtp_session
->dtmf_data
.out_digit_sub_sofar
>> 8);
5320 rtp_session
->dtmf_data
.out_digit_packet
[3] = (unsigned char) rtp_session
->dtmf_data
.out_digit_sub_sofar
;
5322 for (x
= 0; x
< loops
; x
++) {
5323 switch_size_t wrote
= switch_rtp_write_manual(rtp_session
,
5324 rtp_session
->dtmf_data
.out_digit_packet
, 4, 0,
5325 rtp_session
->te
, rtp_session
->dtmf_data
.timestamp_dtmf
, &flags
);
5327 rtp_session
->stats
.outbound
.raw_bytes
+= wrote
;
5328 rtp_session
->stats
.outbound
.dtmf_packet_count
++;
5331 rtp_session
->last_write_ts
+= samples
;
5333 if (rtp_session
->rtp_bugs
& RTP_BUG_SONUS_SEND_INVALID_TIMESTAMP_2833
) {
5334 rtp_session
->dtmf_data
.timestamp_dtmf
= rtp_session
->last_write_ts
;
5338 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
, "Send %s packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u\n",
5339 loops
== 1 ? "middle" : "end", rtp_session
->dtmf_data
.out_digit
,
5340 rtp_session
->dtmf_data
.timestamp_dtmf
,
5341 rtp_session
->dtmf_data
.out_digit_sofar
,
5342 rtp_session
->dtmf_data
.out_digit_sub_sofar
, rtp_session
->dtmf_data
.out_digit_dur
, rtp_session
->seq
, rtp_session
->last_write_ts
);
5346 rtp_session
->sending_dtmf
= 0;
5347 rtp_session
->need_mark
= 1;
5349 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
]) {
5350 //switch_core_timer_sync(&rtp_session->write_timer);
5351 rtp_session
->last_write_samplecount
= rtp_session
->write_timer
.samplecount
;
5354 rtp_session
->dtmf_data
.out_digit_dur
= 0;
5356 if (rtp_session
->interdigit_delay
) {
5357 set_dtmf_delay(rtp_session
, rtp_session
->interdigit_delay
, rtp_session
->interdigit_delay
* 10);
5364 if (!rtp_session
->dtmf_data
.out_digit_dur
&& rtp_session
->dtmf_data
.dtmf_queue
&& switch_queue_size(rtp_session
->dtmf_data
.dtmf_queue
)) {
5367 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
]) {
5368 //switch_core_timer_sync(&rtp_session->write_timer);
5369 if (rtp_session
->timer
.samplecount
< rtp_session
->next_write_samplecount
) {
5373 if (rtp_session
->timer
.samplecount
>= rtp_session
->max_next_write_samplecount
) {
5374 rtp_session
->queue_delay
= 0;
5377 } else if (rtp_session
->queue_delay
) {
5378 if (rtp_session
->delay_samples
>= rtp_session
->samples_per_interval
) {
5379 rtp_session
->delay_samples
-= rtp_session
->samples_per_interval
;
5381 rtp_session
->delay_samples
= 0;
5384 if (!rtp_session
->delay_samples
) {
5385 rtp_session
->queue_delay
= 0;
5389 if (rtp_session
->queue_delay
) {
5394 if (!rtp_session
->sending_dtmf
) {
5395 rtp_session
->sending_dtmf
= 1;
5398 if (switch_queue_trypop(rtp_session
->dtmf_data
.dtmf_queue
, &pop
) == SWITCH_STATUS_SUCCESS
) {
5399 switch_dtmf_t
*rdigit
= pop
;
5400 switch_size_t wrote
;
5402 if (rdigit
->digit
== 'w') {
5403 set_dtmf_delay(rtp_session
, 500, 0);
5408 if (rdigit
->digit
== 'W') {
5409 set_dtmf_delay(rtp_session
, 1000, 0);
5414 memset(rtp_session
->dtmf_data
.out_digit_packet
, 0, 4);
5415 rtp_session
->dtmf_data
.out_digit_sofar
= samples
;
5416 rtp_session
->dtmf_data
.out_digit_sub_sofar
= samples
;
5417 rtp_session
->dtmf_data
.out_digit_dur
= rdigit
->duration
;
5418 rtp_session
->dtmf_data
.out_digit
= rdigit
->digit
;
5419 rtp_session
->dtmf_data
.out_digit_packet
[0] = (unsigned char) switch_char_to_rfc2833(rdigit
->digit
);
5420 rtp_session
->dtmf_data
.out_digit_packet
[1] = 13;
5421 rtp_session
->dtmf_data
.out_digit_packet
[2] = (unsigned char) (rtp_session
->dtmf_data
.out_digit_sub_sofar
>> 8);
5422 rtp_session
->dtmf_data
.out_digit_packet
[3] = (unsigned char) rtp_session
->dtmf_data
.out_digit_sub_sofar
;
5425 rtp_session
->dtmf_data
.timestamp_dtmf
= rtp_session
->last_write_ts
+ samples
;
5426 rtp_session
->last_write_ts
= rtp_session
->dtmf_data
.timestamp_dtmf
;
5427 rtp_session
->flags
[SWITCH_RTP_FLAG_RESET
] = 0;
5429 wrote
= switch_rtp_write_manual(rtp_session
,
5430 rtp_session
->dtmf_data
.out_digit_packet
,
5432 rtp_session
->rtp_bugs
& RTP_BUG_CISCO_SKIP_MARK_BIT_2833
? 0 : 1,
5433 rtp_session
->te
, rtp_session
->dtmf_data
.timestamp_dtmf
, &flags
);
5436 rtp_session
->stats
.outbound
.raw_bytes
+= wrote
;
5437 rtp_session
->stats
.outbound
.dtmf_packet_count
++;
5439 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
, "Send start packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u\n",
5440 rtp_session
->dtmf_data
.out_digit
,
5441 rtp_session
->dtmf_data
.timestamp_dtmf
,
5442 rtp_session
->dtmf_data
.out_digit_sofar
,
5443 rtp_session
->dtmf_data
.out_digit_sub_sofar
, rtp_session
->dtmf_data
.out_digit_dur
, rtp_session
->seq
, rtp_session
->last_write_ts
);
5450 SWITCH_DECLARE(void) rtp_flush_read_buffer(switch_rtp_t
*rtp_session
, switch_rtp_flush_t flush
)
5453 if (rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] ||
5454 rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
]) {
5459 if (switch_rtp_ready(rtp_session
)) {
5460 rtp_session
->flags
[SWITCH_RTP_FLAG_RESET
] = 1;
5461 rtp_session
->flags
[SWITCH_RTP_FLAG_FLUSH
] = 1;
5462 reset_jitter_seq(rtp_session
);
5465 case SWITCH_RTP_FLUSH_STICK
:
5466 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_STICKY_FLUSH
);
5468 case SWITCH_RTP_FLUSH_UNSTICK
:
5469 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_STICKY_FLUSH
);
5477 static int jb_valid(switch_rtp_t
*rtp_session
)
5479 if (rtp_session
->ice
.ice_user
) {
5480 if (!rtp_session
->ice
.ready
&& rtp_session
->ice
.rready
) {
5485 if (rtp_session
->dtls
&& rtp_session
->dtls
->state
!= DS_READY
) {
5493 static switch_size_t
do_flush(switch_rtp_t
*rtp_session
, int force
, switch_size_t bytes_in
)
5495 int was_blocking
= 0;
5496 switch_size_t bytes
;
5497 uint32_t flushed
= 0;
5498 switch_size_t bytes_out
= 0;
5500 if (!switch_rtp_ready(rtp_session
)) {
5504 reset_jitter_seq(rtp_session
);
5507 if ((rtp_session
->jb
&& !rtp_session
->pause_jb
&& jb_valid(rtp_session
)) ||
5508 rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] ||
5509 rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
] ||
5510 rtp_session
->flags
[SWITCH_RTP_FLAG_DTMF_ON
]
5516 READ_INC(rtp_session
);
5518 if (switch_rtp_ready(rtp_session
) ) {
5520 if (rtp_session
->jb
&& !rtp_session
->pause_jb
&& jb_valid(rtp_session
)) {
5521 //switch_jb_reset(rtp_session->jb);
5522 bytes_out
= bytes_in
;
5526 if (rtp_session
->vbw
) {
5527 switch_jb_reset(rtp_session
->vbw
);
5530 if (rtp_session
->vb
) {
5531 //switch_jb_reset(rtp_session->vb);
5532 bytes_out
= bytes_in
;
5536 if (rtp_session
->flags
[SWITCH_RTP_FLAG_DEBUG_RTP_READ
]) {
5537 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
),
5538 SWITCH_LOG_CONSOLE
, "%s FLUSH\n",
5539 rtp_session
->session
? switch_channel_get_name(switch_core_session_get_channel(rtp_session
->session
)) : "NoName"
5543 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_NOBLOCK
]) {
5545 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_NOBLOCK
);
5546 switch_socket_opt_set(rtp_session
->sock_input
, SWITCH_SO_NONBLOCK
, TRUE
);
5549 // before processing/flushing packets, if current packet is rfc2833, handle it (else it would be lost)
5550 if (bytes_in
> rtp_header_len
&& rtp_session
->last_rtp_hdr
.version
== 2 && rtp_session
->last_rtp_hdr
.pt
== rtp_session
->recv_te
) {
5553 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "*** Handling current RTP packet before flushing. seq=%u ***\n", ntohs(rtp_session
->last_rtp_hdr
.seq
));
5555 handle_rfc2833(rtp_session
, bytes_in
, &do_cng
);
5559 if (switch_rtp_ready(rtp_session
)) {
5560 bytes
= sizeof(rtp_msg_t
);
5561 switch_socket_recvfrom(rtp_session
->from_addr
, rtp_session
->sock_input
, 0, (void *) &rtp_session
->recv_msg
, &bytes
);
5566 if (rtp_session
->media_timeout
) {
5567 rtp_session
->last_media
= switch_micro_time_now();
5570 /* Make sure to handle RFC2833 packets, even if we're flushing the packets */
5571 if (bytes
> rtp_header_len
&& rtp_session
->recv_msg
.header
.version
== 2 && rtp_session
->recv_msg
.header
.pt
== rtp_session
->recv_te
) {
5572 rtp_session
->last_rtp_hdr
= rtp_session
->recv_msg
.header
;
5573 handle_rfc2833(rtp_session
, bytes
, &do_cng
);
5575 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "*** RTP packet handled in flush loop %d ***\n", do_cng
);
5581 rtp_session
->stats
.inbound
.raw_bytes
+= bytes
;
5582 rtp_session
->stats
.inbound
.flush_packet_count
++;
5583 rtp_session
->stats
.inbound
.packet_count
++;
5588 } while (bytes
> 0);
5592 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "*** do_flush: total flushed packets: %ld ***\n",(long)flushed
);
5597 if (was_blocking
&& switch_rtp_ready(rtp_session
)) {
5598 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_NOBLOCK
);
5599 switch_socket_opt_set(rtp_session
->sock_input
, SWITCH_SO_NONBLOCK
, FALSE
);
5603 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && rtp_session
->session
) {
5604 //int type = 1; // sum flags: 1 encoder; 2; decoder
5605 //switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_IO_READ, SCC_VIDEO_RESET, SCCT_INT, (void *)&type, NULL, NULL);
5606 switch_core_session_request_video_refresh(rtp_session
->session
);
5612 READ_DEC(rtp_session
);
5617 static int check_recv_payload(switch_rtp_t
*rtp_session
)
5621 if (!(rtp_session
->rtp_bugs
& RTP_BUG_ACCEPT_ANY_PAYLOAD
) && rtp_session
->pmaps
&& *rtp_session
->pmaps
) {
5622 payload_map_t
*pmap
;
5625 switch_mutex_lock(rtp_session
->flag_mutex
);
5627 for (pmap
= *rtp_session
->pmaps
; pmap
&& pmap
->allocated
; pmap
= pmap
->next
) {
5628 if (!pmap
->negotiated
) {
5632 if (rtp_session
->last_rtp_hdr
.pt
== pmap
->pt
) {
5636 switch_mutex_unlock(rtp_session
->flag_mutex
);
5642 static int get_recv_payload(switch_rtp_t
*rtp_session
)
5646 if (rtp_session
->pmaps
&& *rtp_session
->pmaps
) {
5647 payload_map_t
*pmap
;
5649 switch_mutex_lock(rtp_session
->flag_mutex
);
5651 for (pmap
= *rtp_session
->pmaps
; pmap
&& pmap
->allocated
; pmap
= pmap
->next
) {
5652 if (pmap
->negotiated
) {
5657 switch_mutex_unlock(rtp_session
->flag_mutex
);
5663 #define return_cng_frame() do_cng = 1; goto timer_check
5665 static switch_status_t
read_rtp_packet(switch_rtp_t
*rtp_session
, switch_size_t
*bytes
, switch_frame_flag_t
*flags
,
5666 payload_map_t
**pmapP
, switch_status_t poll_status
, switch_bool_t return_jb_packet
)
5668 switch_status_t status
= SWITCH_STATUS_FALSE
;
5670 unsigned char *b
= NULL
;
5673 switch_size_t xcheck_jitter
= 0;
5677 switch_assert(bytes
);
5683 if (rtp_session
->jb
&& !rtp_session
->pause_jb
&& jb_valid(rtp_session
)) {
5684 switch_jb_reset(rtp_session
->jb
);
5686 rtp_session
->punts
++;
5687 rtp_session
->clean
= 0;
5689 return SWITCH_STATUS_BREAK
;
5696 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
5699 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
] && rtp_session
->timer
.interval
) {
5700 to
= rtp_session
->timer
.interval
* 1000;
5704 poll_status
= switch_poll(rtp_session
->read_pollfd
, 1, &fdr
, to
);
5706 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
] && rtp_session
->timer
.interval
) {
5707 switch_core_timer_sync(&rtp_session
->timer
);
5710 if (rtp_session
->session
) {
5711 switch_ivr_parse_all_messages(rtp_session
->session
);
5717 *bytes
= sizeof(rtp_msg_t
);
5720 rtp_session
->has_rtp
= 0;
5721 rtp_session
->has_ice
= 0;
5722 rtp_session
->has_rtcp
= 0;
5723 if (rtp_session
->dtls
) {
5724 rtp_session
->dtls
->bytes
= 0;
5725 rtp_session
->dtls
->data
= NULL
;
5727 memset(&rtp_session
->last_rtp_hdr
, 0, sizeof(rtp_session
->last_rtp_hdr
));
5729 if (poll_status
== SWITCH_STATUS_SUCCESS
) {
5730 status
= switch_socket_recvfrom(rtp_session
->from_addr
, rtp_session
->sock_input
, 0, (void *) &rtp_session
->recv_msg
, bytes
);
5736 b
= (unsigned char *) &rtp_session
->recv_msg
;
5738 /* version 2 probably rtp */
5739 rtp_session
->has_rtp
= (rtp_session
->recv_msg
.header
.version
== 2);
5741 if (rtp_session
->media_timeout
) {
5742 rtp_session
->last_media
= switch_micro_time_now();
5745 if ((*b
>= 20) && (*b
<= 64)) {
5746 if (rtp_session
->dtls
) {
5747 rtp_session
->dtls
->bytes
= *bytes
;
5748 rtp_session
->dtls
->data
= (void *) &rtp_session
->recv_msg
;
5750 rtp_session
->has_ice
= 0;
5751 rtp_session
->has_rtp
= 0;
5752 rtp_session
->has_rtcp
= 0;
5753 } else if (*b
== 0 || *b
== 1) {
5754 rtp_session
->has_ice
= 1;
5755 rtp_session
->has_rtp
= 0;
5756 rtp_session
->has_rtcp
= 0;
5758 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
5759 switch(rtp_session
->recv_msg
.header
.pt
) {
5760 case 64: // 192 Full INTRA-frame request.
5761 case 72: // 200 Sender report.
5762 case 73: // 201 Receiver report.
5763 case 74: // 202 Source description.
5764 case 75: // 203 Goodbye.
5765 case 76: // 204 Application-defined.
5766 case 77: // 205 Transport layer FB message.
5767 case 78: // 206 Payload-specific FB message.
5768 case 79: // 207 Extended report.
5769 rtp_session
->has_rtcp
= 1;
5770 rtp_session
->has_rtp
= 0;
5771 rtp_session
->has_ice
= 0;
5774 if (rtp_session
->rtcp_recv_msg_p
->header
.version
== 2 &&
5775 rtp_session
->rtcp_recv_msg_p
->header
.type
> 199 && rtp_session
->rtcp_recv_msg_p
->header
.type
< 208) {
5776 rtp_session
->has_rtcp
= 1;
5777 rtp_session
->has_rtp
= 0;
5778 rtp_session
->has_ice
= 0;
5785 if (rtp_session
->has_rtp
|| rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
]) {
5786 rtp_session
->missed_count
= 0;
5787 switch_cp_addr(rtp_session
->rtp_from_addr
, rtp_session
->from_addr
);
5790 if (rtp_session
->has_rtp
) {
5791 rtp_session
->last_rtp_hdr
= rtp_session
->recv_msg
.header
;
5794 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] && !rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
] &&
5795 rtp_session
->last_rtp_hdr
.pt
!= 13 &&
5796 rtp_session
->last_rtp_hdr
.pt
!= rtp_session
->recv_te
&&
5797 rtp_session
->last_rtp_hdr
.pt
!= rtp_session
->cng_pt
) {
5798 int accept_packet
= 1;
5801 if (!(rtp_session
->rtp_bugs
& RTP_BUG_ACCEPT_ANY_PAYLOAD
) &&
5802 !(rtp_session
->rtp_bugs
& RTP_BUG_ACCEPT_ANY_PACKETS
) && rtp_session
->pmaps
&& *rtp_session
->pmaps
) {
5803 payload_map_t
*pmap
;
5806 switch_mutex_lock(rtp_session
->flag_mutex
);
5807 for (pmap
= *rtp_session
->pmaps
; pmap
&& pmap
->allocated
; pmap
= pmap
->next
) {
5809 if (!pmap
->negotiated
) {
5813 if (rtp_session
->last_rtp_hdr
.pt
== pmap
->pt
) {
5821 switch_mutex_unlock(rtp_session
->flag_mutex
);
5824 if (!accept_packet
) {
5825 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
,
5826 "Invalid Packet SEQ: %d TS: %d PT:%d ignored\n",
5827 ntohs(rtp_session
->recv_msg
.header
.seq
), ntohl(rtp_session
->last_rtp_hdr
.ts
), rtp_session
->last_rtp_hdr
.pt
);
5832 if (rtp_session
->flags
[SWITCH_RTP_FLAG_DETECT_SSRC
]) {
5833 //if (rtp_session->remote_ssrc != rtp_session->stats.rtcp.peer_ssrc && rtp_session->stats.rtcp.peer_ssrc) {
5834 // rtp_session->remote_ssrc = rtp_session->stats.rtcp.peer_ssrc;
5837 if (rtp_session
->remote_ssrc
!= rtp_session
->last_rtp_hdr
.ssrc
&& rtp_session
->last_rtp_hdr
.ssrc
) {
5838 rtp_session
->remote_ssrc
= ntohl(rtp_session
->last_rtp_hdr
.ssrc
);
5844 if (!rtp_session
->vb
&& (!rtp_session
->jb
|| rtp_session
->pause_jb
|| !jb_valid(rtp_session
))) {
5845 if (*bytes
> rtp_header_len
&& (rtp_session
->has_rtp
&& check_recv_payload(rtp_session
))) {
5846 xcheck_jitter
= *bytes
;
5847 check_jitter(rtp_session
);
5851 if (check_rtcp_and_ice(rtp_session
) == -1) {
5852 //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5853 //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF CHECK FAIL\n");
5855 return SWITCH_STATUS_GENERR
;
5858 if (rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
]) {
5864 *flags
&= ~SFF_PROXY_PACKET
;
5866 //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5867 // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF BYTES %ld b=%d\n", *bytes, *b);
5871 if (rtp_session
->has_ice
) {
5872 if (rtp_session
->ice
.ice_user
) {
5873 handle_ice(rtp_session
, &rtp_session
->ice
, (void *) &rtp_session
->recv_msg
, *bytes
);
5880 switch_mutex_lock(rtp_session
->ice_mutex
);
5882 if (rtp_session
->dtls
) {
5884 if (rtp_session
->rtcp_dtls
&& rtp_session
->rtcp_dtls
!= rtp_session
->dtls
) {
5885 rtp_session
->rtcp_dtls
->bytes
= 0;
5886 rtp_session
->rtcp_dtls
->data
= NULL
;
5887 do_dtls(rtp_session
, rtp_session
->rtcp_dtls
);
5890 do_dtls(rtp_session
, rtp_session
->dtls
);
5892 if (rtp_session
->dtls
&& rtp_session
->dtls
->bytes
) {
5900 if (status
== SWITCH_STATUS_SUCCESS
&& *bytes
) {
5901 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
5902 *flags
&= ~SFF_RTCP
;
5903 if (rtp_session
->has_rtcp
) {
5907 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV
]) {
5908 int sbytes
= (int) *bytes
;
5909 srtp_err_status_t stat
= 0;
5911 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV_MKI
]) {
5912 stat
= srtp_unprotect_rtcp(rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtcp
], &rtp_session
->rtcp_recv_msg_p
->header
, &sbytes
);
5914 stat
= srtp_unprotect_rtcp_mki(rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtcp
], &rtp_session
->rtcp_recv_msg_p
->header
, &sbytes
, 1);
5918 //++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++;
5919 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "RTCP UNPROTECT ERR\n");
5921 //rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
5927 switch_mutex_unlock(rtp_session
->ice_mutex
);
5928 return SWITCH_STATUS_SUCCESS
;
5933 switch_mutex_unlock(rtp_session
->ice_mutex
);
5935 if ((*bytes
&& (!rtp_write_ready(rtp_session
, *bytes
, __LINE__
) || !rtp_session
->has_rtp
|| rtp_session
->has_rtcp
)) || sync
) {
5936 rtp_session
->hot_hits
= 0;
5942 if (*bytes
&& rtp_session
->flags
[SWITCH_RTP_FLAG_DEBUG_RTP_READ
]) {
5943 const char *tx_host
;
5944 const char *old_host
;
5945 const char *my_host
;
5947 char bufa
[50], bufb
[50], bufc
[50];
5950 tx_host
= switch_get_addr(bufa
, sizeof(bufa
), rtp_session
->rtp_from_addr
);
5951 old_host
= switch_get_addr(bufb
, sizeof(bufb
), rtp_session
->remote_addr
);
5952 my_host
= switch_get_addr(bufc
, sizeof(bufc
), rtp_session
->local_addr
);
5954 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session
->session
), SWITCH_LOG_CONSOLE
,
5955 "R %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
5956 rtp_session
->session
? switch_channel_get_name(switch_core_session_get_channel(rtp_session
->session
)) : "No-Name",
5958 my_host
, switch_sockaddr_get_port(rtp_session
->local_addr
),
5959 old_host
, rtp_session
->remote_port
,
5960 tx_host
, switch_sockaddr_get_port(rtp_session
->rtp_from_addr
),
5961 rtp_session
->last_rtp_hdr
.pt
, ntohl(rtp_session
->last_rtp_hdr
.ts
), ntohs(rtp_session
->last_rtp_hdr
.seq
),
5962 rtp_session
->last_rtp_hdr
.m
);
5966 #ifdef RTP_READ_PLOSS
5968 int r
= (rand() % 10000) + 1;
5970 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ALERT
,
5971 "Simulate dropped packet ......... ts: %u seq: %u\n", ntohl(rtp_session
->last_rtp_hdr
.ts
), ntohs(rtp_session
->last_rtp_hdr
.seq
));
5982 rtp_session
->recv_msg
.ebody
= NULL
;
5983 now
= switch_micro_time_now();
5986 uint16_t seq
= ntohs((uint16_t) rtp_session
->last_rtp_hdr
.seq
);
5987 ts
= ntohl(rtp_session
->last_rtp_hdr
.ts
);
5989 #ifdef DEBUG_MISSED_SEQ
5990 if (rtp_session
->last_seq
&& rtp_session
->last_seq
+1 != seq
) {
5991 //2012-11-28 18:33:11.799070 [ERR] switch_rtp.c:2883 Missed -65536 RTP frames from sequence [65536] to [-1] (missed). Time since last read [20021]
5992 switch_size_t flushed_packets_diff
= rtp_session
->stats
.inbound
.flush_packet_count
- rtp_session
->last_flush_packet_count
;
5993 switch_size_t num_missed
= (switch_size_t
)seq
- (rtp_session
->last_seq
+1);
5995 if (num_missed
== 1) { /* We missed one packet */
5996 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "Missed one RTP frame with sequence [%d]%s. Time since last read [%ld]\n",
5997 rtp_session
->last_seq
+1, (flushed_packets_diff
== 1) ? " (flushed by FS)" : " (missed)",
5998 rtp_session
->last_read_time
? now
-rtp_session
->last_read_time
: 0);
5999 } else { /* We missed multiple packets */
6000 if (flushed_packets_diff
== 0) {
6001 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
,
6002 "Missed %ld RTP frames from sequence [%d] to [%d] (missed). Time since last read [%ld]\n",
6003 num_missed
, rtp_session
->last_seq
+1, seq
-1,
6004 rtp_session
->last_read_time
? now
-rtp_session
->last_read_time
: 0);
6005 } else if (flushed_packets_diff
== num_missed
) {
6006 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
,
6007 "Missed %ld RTP frames from sequence [%d] to [%d] (flushed by FS). Time since last read [%ld]\n",
6008 num_missed
, rtp_session
->last_seq
+1, seq
-1,
6009 rtp_session
->last_read_time
? now
-rtp_session
->last_read_time
: 0);
6010 } else if (num_missed
> flushed_packets_diff
) {
6011 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
,
6012 "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS, %ld packets missed)."
6013 " Time since last read [%ld]\n",
6014 num_missed
, rtp_session
->last_seq
+1, seq
-1,
6015 flushed_packets_diff
, num_missed
-flushed_packets_diff
,
6016 rtp_session
->last_read_time
? now
-rtp_session
->last_read_time
: 0);
6018 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
,
6019 "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS). Time since last read [%ld]\n",
6020 num_missed
, rtp_session
->last_seq
+1, seq
-1,
6021 flushed_packets_diff
, rtp_session
->last_read_time
? now
-rtp_session
->last_read_time
: 0);
6027 rtp_session
->last_seq
= seq
;
6030 rtp_session
->last_flush_packet_count
= rtp_session
->stats
.inbound
.flush_packet_count
;
6033 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && now
- rtp_session
->last_read_time
> 5000000) {
6034 switch_rtp_video_refresh(rtp_session
);
6037 rtp_session
->last_read_time
= now
;
6040 if (*bytes
&& rtp_session
->has_rtp
&& rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]){
6041 rtcp_stats(rtp_session
);
6045 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] && !rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
] && !rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] &&
6046 *bytes
&& rtp_session
->last_rtp_hdr
.pt
!= rtp_session
->recv_te
&&
6047 ts
&& !rtp_session
->jb
&& !rtp_session
->pause_jb
&& jb_valid(rtp_session
) && ts
== rtp_session
->last_cng_ts
) {
6048 /* we already sent this frame..... */
6050 return SWITCH_STATUS_SUCCESS
;
6054 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] && !rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
]) {
6057 switch_mutex_lock(rtp_session
->ice_mutex
);
6058 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV
] && rtp_session
->has_rtp
&&
6059 (check_recv_payload(rtp_session
) ||
6060 rtp_session
->last_rtp_hdr
.pt
== rtp_session
->recv_te
||
6061 rtp_session
->last_rtp_hdr
.pt
== rtp_session
->cng_pt
)) {
6062 //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->has_rtp)) {
6063 int sbytes
= (int) *bytes
;
6064 srtp_err_status_t stat
= 0;
6066 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV_RESET
] || !rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtp
]) {
6067 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_SECURE_RECV_RESET
);
6068 srtp_dealloc(rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtp
]);
6069 rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtp
] = NULL
;
6070 if ((stat
= srtp_create(&rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtp
],
6071 &rtp_session
->recv_policy
[rtp_session
->srtp_idx_rtp
])) || !rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtp
]) {
6073 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV
] = 0;
6074 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Error! RE-Activating Secure RTP RECV\n");
6075 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV
] = 0;
6076 switch_mutex_unlock(rtp_session
->ice_mutex
);
6077 return SWITCH_STATUS_FALSE
;
6080 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_INFO
, "RE-Activating Secure RTP RECV\n");
6081 rtp_session
->srtp_errs
[rtp_session
->srtp_idx_rtp
] = 0;
6085 if (!(*flags
& SFF_PLC
) && rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtp
]) {
6086 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV_MKI
]) {
6087 stat
= srtp_unprotect(rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtp
], &rtp_session
->recv_msg
.header
, &sbytes
);
6089 stat
= srtp_unprotect_mki(rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtp
], &rtp_session
->recv_msg
.header
, &sbytes
, 1);
6092 if (rtp_session
->flags
[SWITCH_RTP_FLAG_NACK
] && stat
== srtp_err_status_replay_fail
) {
6093 /* false alarm nack */
6094 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
, "REPLAY ERR, FALSE NACK\n");
6097 if (rtp_session
->stats
.rtcp
.pkt_count
) {
6098 rtp_session
->stats
.rtcp
.period_pkt_count
--;
6099 rtp_session
->stats
.rtcp
.pkt_count
--;
6101 switch_mutex_unlock(rtp_session
->ice_mutex
);
6106 if (stat
&& rtp_session
->recv_msg
.header
.pt
!= rtp_session
->recv_te
&& rtp_session
->recv_msg
.header
.pt
!= rtp_session
->cng_pt
) {
6107 int errs
= ++rtp_session
->srtp_errs
[rtp_session
->srtp_idx_rtp
];
6108 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SRTP_HANGUP_ON_ERROR
] && stat
!= srtp_err_status_replay_old
) {
6110 switch_srtp_err_to_txt(stat
, &msg
);
6111 if (errs
>= MAX_SRTP_ERRS
) {
6112 switch_channel_t
*channel
= switch_core_session_get_channel(rtp_session
->session
);
6113 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
,
6114 "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
6115 rtp_type(rtp_session
), stat
, msg
, (long)*bytes
, errs
);
6116 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
,
6117 "Ending call due to SRTP error\n");
6118 switch_channel_hangup(channel
, SWITCH_CAUSE_SRTP_READ_ERROR
);
6119 } else if (errs
>= WARN_SRTP_ERRS
&& !(errs
% WARN_SRTP_ERRS
)) {
6120 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
,
6121 "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
6122 rtp_type(rtp_session
), stat
, msg
, (long)*bytes
, errs
);
6127 rtp_session
->srtp_errs
[rtp_session
->srtp_idx_rtp
] = 0;
6132 switch_mutex_unlock(rtp_session
->ice_mutex
);
6137 if (rtp_session
->has_rtp
) {
6138 if (rtp_session
->recv_msg
.header
.cc
> 0) { /* Contributing Source Identifiers (4 bytes = sizeof CSRC header)*/
6139 rtp_session
->recv_msg
.ebody
= RTP_BODY(rtp_session
) + (rtp_session
->recv_msg
.header
.cc
* 4);
6142 /* recalculate body length in case rtp extension used */
6143 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] && !rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
] &&
6144 rtp_session
->recv_msg
.header
.x
) { /* header extensions */
6147 rtp_session
->recv_msg
.ext
= (switch_rtp_hdr_ext_t
*) RTP_BODY(rtp_session
);
6148 length
= ntohs((uint16_t)rtp_session
->recv_msg
.ext
->length
);
6150 if (length
< SWITCH_RTP_MAX_BUF_LEN_WORDS
) {
6151 rtp_session
->recv_msg
.ebody
= (char *)rtp_session
->recv_msg
.ext
+ (length
* 4) + 4;
6152 if (*bytes
> (length
* 4 + 4)) {
6153 *bytes
-= (length
* 4 + 4);
6163 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && rtp_session
->has_rtp
) {
6165 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_INFO
,
6166 "VIDEO: seq: %d ts: %u len: %ld %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x mark: %d\n",
6167 ntohs(rtp_session
->last_rtp_hdr
.seq
), ntohl(rtp_session
->last_rtp_hdr
.ts
), *bytes
,
6168 *((uint8_t *)RTP_BODY(rtp_session
)), *((uint8_t *)RTP_BODY(rtp_session
) + 1),
6169 *((uint8_t *)RTP_BODY(rtp_session
) + 2), *((uint8_t *)RTP_BODY(rtp_session
) + 3),
6170 *((uint8_t *)RTP_BODY(rtp_session
) + 4), *((uint8_t *)RTP_BODY(rtp_session
) + 5),
6171 *((uint8_t *)RTP_BODY(rtp_session
) + 6), *((uint8_t *)RTP_BODY(rtp_session
) + 7),
6172 *((uint8_t *)RTP_BODY(rtp_session
) + 8), *((uint8_t *)RTP_BODY(rtp_session
) + 9),
6173 *((uint8_t *)RTP_BODY(rtp_session
) + 10), rtp_session
->last_rtp_hdr
.m
);
6183 rtp_session
->stats
.inbound
.raw_bytes
+= *bytes
;
6185 if (rtp_session
->last_rtp_hdr
.pt
== rtp_session
->recv_te
) {
6186 rtp_session
->stats
.inbound
.dtmf_packet_count
++;
6187 } else if (rtp_session
->last_rtp_hdr
.pt
== rtp_session
->cng_pt
|| rtp_session
->last_rtp_hdr
.pt
== 13) {
6188 rtp_session
->stats
.inbound
.cng_packet_count
++;
6190 rtp_session
->stats
.inbound
.media_packet_count
++;
6191 rtp_session
->stats
.inbound
.media_bytes
+= *bytes
;
6194 rtp_session
->stats
.inbound
.packet_count
++;
6197 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] &&
6198 ((rtp_session
->recv_te
&& rtp_session
->last_rtp_hdr
.pt
== rtp_session
->recv_te
) ||
6199 (*bytes
< rtp_header_len
&& *bytes
> 0 && !(rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] || rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
])))) {
6200 return SWITCH_STATUS_BREAK
;
6204 rtp_session
->prev_read_ts
= rtp_session
->last_read_ts
;
6205 rtp_session
->last_read_ts
= ts
;
6208 if (rtp_session
->flags
[SWITCH_RTP_FLAG_BYTESWAP
] && check_recv_payload(rtp_session
)) {
6209 switch_swap_linear((int16_t *)RTP_BODY(rtp_session
), (int) *bytes
- rtp_header_len
);
6212 if (rtp_session
->flags
[SWITCH_RTP_FLAG_KILL_JB
]) {
6213 rtp_session
->flags
[SWITCH_RTP_FLAG_KILL_JB
] = 0;
6215 if (rtp_session
->jb
) {
6216 switch_jb_destroy(&rtp_session
->jb
);
6219 if (rtp_session
->vb
) {
6220 switch_jb_destroy(&rtp_session
->vb
);
6223 if (rtp_session
->vbw
) {
6224 switch_jb_destroy(&rtp_session
->vbw
);
6229 if (rtp_session
->has_rtp
&& *bytes
) {
6230 uint32_t read_ssrc
= ntohl(rtp_session
->last_rtp_hdr
.ssrc
);
6232 if (rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] || rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
]) {
6233 return SWITCH_STATUS_SUCCESS
;
6236 if (rtp_session
->vb
&& !rtp_session
->pause_jb
&& jb_valid(rtp_session
)) {
6237 status
= switch_jb_put_packet(rtp_session
->vb
, (switch_rtp_packet_t
*) &rtp_session
->recv_msg
, *bytes
);
6239 if (status
== SWITCH_STATUS_TOO_LATE
) {
6243 status
= SWITCH_STATUS_FALSE
;
6246 if (!return_jb_packet
) {
6251 if (rtp_session
->jb
&& jb_valid(rtp_session
)) {
6252 if (rtp_session
->last_jb_read_ssrc
&& rtp_session
->last_jb_read_ssrc
!= read_ssrc
) {
6253 switch_jb_reset(rtp_session
->jb
);
6256 rtp_session
->last_jb_read_ssrc
= read_ssrc
;
6259 if (rtp_session
->jb
&& !rtp_session
->pause_jb
&& jb_valid(rtp_session
)) {
6261 status
= switch_jb_put_packet(rtp_session
->jb
, (switch_rtp_packet_t
*) &rtp_session
->recv_msg
, *bytes
);
6262 if (status
== SWITCH_STATUS_TOO_LATE
) {
6267 status
= SWITCH_STATUS_FALSE
;
6270 if (!return_jb_packet
) {
6274 if (rtp_session
->last_rtp_hdr
.m
&& rtp_session
->last_rtp_hdr
.pt
!= rtp_session
->recv_te
&&
6275 !rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && !(rtp_session
->rtp_bugs
& RTP_BUG_IGNORE_MARK_BIT
) &&
6276 rtp_session
->last_read_ts
- rtp_session
->prev_read_ts
< rtp_session
->samples_per_interval
* 3) {
6277 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_FLUSH
);
6278 } else if (rtp_session
->last_jb_read_ssrc
&& rtp_session
->last_jb_read_ssrc
!= read_ssrc
) {
6279 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_FLUSH
);
6284 if (!*bytes
|| rtp_session
->has_rtp
) {
6286 if (rtp_session
->jb
&& !rtp_session
->pause_jb
&& jb_valid(rtp_session
)) {
6287 switch_status_t jstatus
= switch_jb_get_packet(rtp_session
->jb
, (switch_rtp_packet_t
*) &rtp_session
->recv_msg
, bytes
);
6292 case SWITCH_STATUS_MORE_DATA
:
6293 if (rtp_session
->punts
< 4) {
6299 case SWITCH_STATUS_NOTFOUND
:
6301 int pt
= get_recv_payload(rtp_session
);
6302 (*flags
) |= SFF_PLC
;
6303 status
= SWITCH_STATUS_SUCCESS
;
6304 *bytes
= switch_jb_get_last_read_len(rtp_session
->jb
);
6305 rtp_session
->last_rtp_hdr
= rtp_session
->recv_msg
.header
;
6306 rtp_session
->last_rtp_hdr
.pt
= pt
;
6309 case SWITCH_STATUS_BREAK
:
6311 case SWITCH_STATUS_SUCCESS
:
6312 case SWITCH_STATUS_TIMEOUT
:
6315 if (status
== SWITCH_STATUS_TIMEOUT
) {
6316 rtp_session
->skip_timer
= 1;
6318 rtp_session
->stats
.inbound
.jb_packet_count
++;
6319 status
= SWITCH_STATUS_SUCCESS
;
6320 rtp_session
->last_rtp_hdr
= rtp_session
->recv_msg
.header
;
6321 if (++rtp_session
->clean
> 200) {
6322 rtp_session
->punts
= 0;
6324 if (!xcheck_jitter
) {
6325 check_jitter(rtp_session
);
6332 if (rtp_session
->vb
&& !rtp_session
->pause_jb
&& jb_valid(rtp_session
)) {
6333 switch_status_t vstatus
= switch_jb_get_packet(rtp_session
->vb
, (switch_rtp_packet_t
*) &rtp_session
->recv_msg
, bytes
);
6337 case SWITCH_STATUS_RESTART
:
6338 switch_core_session_request_video_refresh(rtp_session
->session
);
6339 status
= SWITCH_STATUS_BREAK
;
6341 case SWITCH_STATUS_MORE_DATA
:
6342 status
= SWITCH_STATUS_BREAK
;
6344 case SWITCH_STATUS_BREAK
:
6349 if (vstatus
== SWITCH_STATUS_NOTFOUND
&& rtp_session
->flags
[SWITCH_RTP_FLAG_TEXT
]) {
6350 int pt
= get_recv_payload(rtp_session
);
6351 (*flags
) |= SFF_PLC
;
6352 status
= SWITCH_STATUS_SUCCESS
;
6353 *bytes
= switch_jb_get_last_read_len(rtp_session
->vb
);
6354 rtp_session
->last_rtp_hdr
= rtp_session
->recv_msg
.header
;
6356 rtp_session
->last_rtp_hdr
.pt
= pt
;
6360 if (vstatus
== SWITCH_STATUS_SUCCESS
) {
6361 rtp_session
->last_rtp_hdr
= rtp_session
->recv_msg
.header
;
6363 if (!xcheck_jitter
) {
6364 check_jitter(rtp_session
);
6373 static void handle_nack(switch_rtp_t
*rtp_session
, uint32_t nack
)
6375 switch_size_t bytes
= 0;
6376 rtp_msg_t send_msg
[1] = {{{0}}};
6377 uint16_t seq
= (uint16_t) (nack
& 0xFFFF);
6378 uint16_t blp
= (uint16_t) (nack
>> 16);
6380 const char *tx_host
= NULL
;
6381 const char *old_host
= NULL
;
6382 const char *my_host
= NULL
;
6383 char bufa
[50], bufb
[50], bufc
[50];
6385 if (!(rtp_session
->flags
[SWITCH_RTP_FLAG_NACK
] && rtp_session
->vbw
)) {
6386 return; /* not enabled */
6389 if (rtp_session
->flags
[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE
]) {
6390 tx_host
= switch_get_addr(bufa
, sizeof(bufa
), rtp_session
->rtcp_from_addr
);
6391 old_host
= switch_get_addr(bufb
, sizeof(bufb
), rtp_session
->remote_addr
);
6392 my_host
= switch_get_addr(bufc
, sizeof(bufc
), rtp_session
->local_addr
);
6395 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "%s Got NACK [%u][0x%x] for seq %u\n",
6396 switch_core_session_get_name(rtp_session
->session
), nack
, nack
, ntohs(seq
));
6398 if (switch_jb_get_packet_by_seq(rtp_session
->vbw
, seq
, (switch_rtp_packet_t
*) send_msg
, &bytes
) == SWITCH_STATUS_SUCCESS
) {
6400 if (rtp_session
->flags
[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE
]) {
6401 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session
->session
), SWITCH_LOG_CONSOLE
,
6402 "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
6403 rtp_session
->session
? switch_channel_get_name(switch_core_session_get_channel(rtp_session
->session
)) : "NoName",
6405 my_host
, switch_sockaddr_get_port(rtp_session
->local_addr
),
6406 old_host
, rtp_session
->remote_port
,
6407 tx_host
, switch_sockaddr_get_port(rtp_session
->rtcp_from_addr
),
6408 send_msg
->header
.pt
, ntohl(send_msg
->header
.ts
), ntohs(send_msg
->header
.seq
), send_msg
->header
.m
);
6411 //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG2, "RE----SEND %u\n", ntohs(send_msg->header.seq));
6412 switch_rtp_write_raw(rtp_session
, (void *) send_msg
, &bytes
, SWITCH_FALSE
);
6414 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "Cannot send NACK for seq %u\n", ntohs(seq
));
6418 for (i
= 0; i
< 16; i
++) {
6419 if (blp
& (1 << i
)) {
6420 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "%s Also Got NACK for seq %u\n",
6421 switch_core_session_get_name(rtp_session
->session
), ntohs(seq
) + i
+ 1);
6422 /* If they are missing more than one, may as well gen a key frame for good measure */
6423 //switch_core_media_gen_key_frame(rtp_session->session);
6424 if (switch_jb_get_packet_by_seq(rtp_session
->vbw
, htons(ntohs(seq
) + i
+ 1), (switch_rtp_packet_t
*) &send_msg
, &bytes
) == SWITCH_STATUS_SUCCESS
) {
6425 if (rtp_session
->flags
[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE
]) {
6426 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session
->session
), SWITCH_LOG_CONSOLE
,
6427 "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
6428 rtp_session
->session
? switch_channel_get_name(switch_core_session_get_channel(rtp_session
->session
)) : "NoName",
6430 my_host
, switch_sockaddr_get_port(rtp_session
->local_addr
),
6431 old_host
, rtp_session
->remote_port
,
6432 tx_host
, switch_sockaddr_get_port(rtp_session
->rtcp_from_addr
),
6433 send_msg
->header
.pt
, ntohl(send_msg
->header
.ts
), ntohs(send_msg
->header
.seq
), send_msg
->header
.m
);
6436 //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "RE----SEND %u\n", ntohs(send_msg->header.seq));
6438 switch_rtp_write_raw(rtp_session
, (void *) &send_msg
, &bytes
, SWITCH_FALSE
);
6440 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "Cannot send NACK for seq %u\n", ntohs(seq
) + i
);
6446 static switch_status_t
process_rtcp_report(switch_rtp_t
*rtp_session
, rtcp_msg_t
*msg
, switch_size_t bytes
)
6448 switch_status_t status
= SWITCH_STATUS_FALSE
;
6450 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
,
6451 "RTCP packet bytes %" SWITCH_SIZE_T_FMT
" type %d pad %d\n",
6452 bytes
, msg
->header
.type
, msg
->header
.p
);
6454 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && (msg
->header
.type
== _RTCP_PT_RTPFB
|| msg
->header
.type
== _RTCP_PT_PSFB
|| msg
->header
.type
< 200)) {
6455 rtcp_ext_msg_t
*extp
= (rtcp_ext_msg_t
*) msg
;
6457 if (extp
->header
.fmt
!= 15) { // <---- REMOVE WHEN BRIA STOPS SENDING UNSOLICITED REMB
6458 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "%s PICKED UP %s XRTCP type: %d fmt: %d\n",
6459 switch_core_session_get_name(rtp_session
->session
), rtp_type(rtp_session
), msg
->header
.type
, extp
->header
.fmt
);
6462 if (msg
->header
.type
== _RTCP_PT_FIR
||
6463 (msg
->header
.type
== _RTCP_PT_PSFB
&& (extp
->header
.fmt
== _RTCP_PSFB_FIR
|| extp
->header
.fmt
== _RTCP_PSFB_PLI
))) {
6465 if (msg
->header
.type
== _RTCP_PT_FIR
) {
6466 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
, "Ancient FIR Received. Hello from 1996!\n");
6468 if (!switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_OLD_FIR
)) {
6469 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_OLD_FIR
);
6470 switch_core_session_request_video_refresh(rtp_session
->session
);
6475 if (switch_core_session_media_flow(rtp_session
->session
, SWITCH_MEDIA_TYPE_VIDEO
) == SWITCH_MEDIA_FLOW_RECVONLY
) {
6476 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "%s Ignoring FIR/PLI from a sendonly stream.\n",
6477 switch_core_session_get_name(rtp_session
->session
));
6479 switch_core_media_gen_key_frame(rtp_session
->session
);
6480 switch_core_session_request_video_refresh(rtp_session
->session
);
6481 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "%s Got FIR/PLI\n",
6482 switch_core_session_get_name(rtp_session
->session
));
6483 switch_channel_set_flag(switch_core_session_get_channel(rtp_session
->session
), CF_VIDEO_REFRESH_REQ
);
6487 if (msg
->header
.type
== _RTCP_PT_RTPFB
&& extp
->header
.fmt
== _RTCP_RTPFB_NACK
) {
6488 uint32_t *nack
= (uint32_t *) extp
->body
;
6491 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG2
, "%s Got NACK count %d\n",
6492 switch_core_session_get_name(rtp_session
->session
), ntohs(extp
->header
.length
) - 2);
6495 for (i
= 0; i
< ntohs(extp
->header
.length
) - 2; i
++) {
6496 handle_nack(rtp_session
, nack
[i
]);
6499 //switch_core_media_gen_key_frame(rtp_session->session);
6503 struct switch_rtcp_report_block
*report
;
6505 if (msg
->header
.type
== _RTCP_PT_SR
|| msg
->header
.type
== _RTCP_PT_RR
) {
6508 switch_time_t now
= switch_micro_time_now();
6512 uint32_t packet_ssrc
;
6514 uint8_t rtt_valid
= 0;
6515 int rtt_increase
= 0, packet_loss_increase
=0;
6517 //if (msg->header.type == _RTCP_PT_SR && rtp_session->ice.ice_user) {
6518 // rtp_session->send_rr = 1;
6521 lsr_now
= calc_local_lsr_now();
6523 if (msg
->header
.type
== _RTCP_PT_SR
) { /* Sender report */
6524 struct switch_rtcp_sender_report
* sr
= (struct switch_rtcp_sender_report
*)msg
->body
;
6526 rtp_session
->stats
.rtcp
.packet_count
= ntohl(sr
->sender_info
.pc
);
6527 rtp_session
->stats
.rtcp
.octet_count
= ntohl(sr
->sender_info
.oc
);
6528 packet_ssrc
= sr
->ssrc
;
6529 /* Extracting LSR from NTP timestamp and save it */
6530 lsr
= (ntohl(sr
->sender_info
.ntp_lsw
)&0xffff0000)>>16 | (ntohl(sr
->sender_info
.ntp_msw
)&0x0000ffff)<<16; /* The middle 32 bits out of 64 in the NTP timestamp */
6531 rtp_session
->stats
.rtcp
.last_recv_lsr_peer
= htonl(lsr
); /* Save it include it in the next SR */
6532 rtp_session
->stats
.rtcp
.last_recv_lsr_local
= lsr_now
; /* Save it to calculate DLSR when generating next SR */
6533 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
,"Received a SR with %d report blocks, " \
6534 "length in words = %d, " \
6538 "RTP timestamp = %u, " \
6539 "Sender Packet Count = %u, " \
6540 "Sender Octet Count = %u\n",
6542 ntohs((uint16_t)msg
->header
.length
),
6544 ntohl(sr
->sender_info
.ntp_msw
),
6545 ntohl(sr
->sender_info
.ntp_lsw
),
6546 ntohl(sr
->sender_info
.ts
),
6547 ntohl(sr
->sender_info
.pc
),
6548 ntohl(sr
->sender_info
.oc
));
6551 rtp_session
->rtcp_frame
.ssrc
= ntohl(sr
->ssrc
);
6552 rtp_session
->rtcp_frame
.packet_type
= (uint16_t)rtp_session
->rtcp_recv_msg_p
->header
.type
;
6553 rtp_session
->rtcp_frame
.ntp_msw
= ntohl(sr
->sender_info
.ntp_msw
);
6554 rtp_session
->rtcp_frame
.ntp_lsw
= ntohl(sr
->sender_info
.ntp_lsw
);
6555 rtp_session
->rtcp_frame
.timestamp
= ntohl(sr
->sender_info
.ts
);
6556 rtp_session
->rtcp_frame
.packet_count
= ntohl(sr
->sender_info
.pc
);
6557 rtp_session
->rtcp_frame
.octect_count
= ntohl(sr
->sender_info
.oc
);
6559 report
= &sr
->report_block
;
6560 } else { /* Receiver report */
6561 struct switch_rtcp_receiver_report
* rr
= (struct switch_rtcp_receiver_report
*)msg
->body
;
6562 packet_ssrc
= rr
->ssrc
;
6563 //memset(&rtp_session->rtcp_frame, 0, sizeof(rtp_session->rtcp_frame));
6564 report
= &rr
->report_block
;
6566 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
,"Received a RR with %d report blocks, " \
6567 "length in words = %d, " \
6570 ntohs((uint16_t)msg
->header
.length
),
6576 for (i
= 0; i
< (int)msg
->header
.count
&& i
< MAX_REPORT_BLOCKS
; i
++) {
6577 uint32_t old_avg
= rtp_session
->rtcp_frame
.reports
[i
].loss_avg
;
6578 uint8_t percent_fraction
= (uint8_t)((uint16_t/* prevent overflow when '* 100' */)(uint8_t)report
->fraction
* 100 / 255);
6579 if (!rtp_session
->rtcp_frame
.reports
[i
].loss_avg
) {
6580 rtp_session
->rtcp_frame
.reports
[i
].loss_avg
= percent_fraction
;
6582 rtp_session
->rtcp_frame
.reports
[i
].loss_avg
= (uint32_t)(((float)rtp_session
->rtcp_frame
.reports
[i
].loss_avg
* .7) +
6583 ((float)percent_fraction
* .3));
6586 rtp_session
->rtcp_frame
.reports
[i
].ssrc
= ntohl(report
->ssrc
);
6587 rtp_session
->rtcp_frame
.reports
[i
].fraction
= (uint8_t)report
->fraction
;
6588 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
6589 rtp_session
->rtcp_frame
.reports
[i
].lost
= report
->lost
; // signed 24bit will extended signess to int32_t automatically
6591 rtp_session
->rtcp_frame
.reports
[i
].lost
= ntohl(report
->lost
)>>8; // signed 24bit casted to uint32_t need >>8 after ntohl()...
6592 rtp_session
->rtcp_frame
.reports
[i
].lost
= rtp_session
->rtcp_frame
.reports
[i
].lost
| ((rtp_session
->rtcp_frame
.reports
[i
].lost
& 0x00800000) ? 0xff000000 : 0x00000000); // ...and signess compensation
6594 rtp_session
->rtcp_frame
.reports
[i
].highest_sequence_number_received
= ntohl(report
->highest_sequence_number_received
);
6595 rtp_session
->rtcp_frame
.reports
[i
].jitter
= ntohl(report
->jitter
);
6596 rtp_session
->rtcp_frame
.reports
[i
].lsr
= ntohl(report
->lsr
);
6597 rtp_session
->rtcp_frame
.reports
[i
].dlsr
= ntohl(report
->dlsr
);
6599 if (rtp_session
->rtcp_frame
.reports
[i
].lsr
&& !rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_PASSTHRU
]) {
6601 /* Calculating RTT = A - DLSR - LSR */
6602 rtt_now
= ((double)(((int64_t)lsr_now
) - rtp_session
->rtcp_frame
.reports
[i
].dlsr
- rtp_session
->rtcp_frame
.reports
[i
].lsr
))/65536;
6604 /* Only account RTT if it didn't overflow. */
6605 if (lsr_now
> rtp_session
->rtcp_frame
.reports
[i
].dlsr
+ rtp_session
->rtcp_frame
.reports
[i
].lsr
) {
6607 switch_time_exp_t now_hr
;
6608 switch_time_exp_gmt(&now_hr
,now
);
6609 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
,
6610 "Receiving an RTCP packet\n[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[0x%x]\n"
6611 "RTT[%f] = A[%u] - DLSR[%u] - LSR[%u]\n",
6612 1900 + now_hr
.tm_year
, now_hr
.tm_mday
, now_hr
.tm_mon
, now_hr
.tm_hour
, now_hr
.tm_min
, now_hr
.tm_sec
, now_hr
.tm_usec
,
6613 rtp_session
->rtcp_frame
.reports
[i
].ssrc
, rtt_now
,
6614 lsr_now
, rtp_session
->rtcp_frame
.reports
[i
].dlsr
, rtp_session
->rtcp_frame
.reports
[i
].lsr
);
6617 if (!rtp_session
->rtcp_frame
.reports
[i
].rtt_avg
) {
6618 rtp_session
->rtcp_frame
.reports
[i
].rtt_avg
= rtt_now
;
6620 rtp_session
->rtcp_frame
.reports
[i
].rtt_avg
= (double)((rtp_session
->rtcp_frame
.reports
[i
].rtt_avg
* .7) + (rtt_now
* .3 ));
6624 switch_time_exp_t now_hr
;
6625 switch_time_exp_gmt(&now_hr
,now
);
6626 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
,
6627 "Receiving RTCP packet\n[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[0x%x]\n"
6628 "Ignoring erroneous RTT[%f] = A[%u] - DLSR[%u] - LSR[%u]\n",
6629 1900 + now_hr
.tm_year
, now_hr
.tm_mday
, now_hr
.tm_mon
, now_hr
.tm_hour
, now_hr
.tm_min
, now_hr
.tm_sec
, now_hr
.tm_usec
,
6630 rtp_session
->rtcp_frame
.reports
[i
].ssrc
, rtt_now
,
6631 lsr_now
, rtp_session
->rtcp_frame
.reports
[i
].dlsr
, rtp_session
->rtcp_frame
.reports
[i
].lsr
);
6638 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
, "RTT average %f\n",
6639 rtp_session
->rtcp_frame
.reports
[i
].rtt_avg
);
6642 if (rtp_session
->flags
[SWITCH_RTP_FLAG_ADJ_BITRATE_CAP
] && rtp_session
->flags
[SWITCH_RTP_FLAG_ESTIMATORS
] && !rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
6644 /* SWITCH_RTP_FLAG_ADJ_BITRATE_CAP : Can the codec change its bitrate on the fly per API command ? */
6645 #ifdef DEBUG_ESTIMATORS_
6646 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
, "Current packet loss: [%d %%] Current RTT: [%f ms]\n", percent_fraction
, rtt_now
);
6651 switch_kalman_estimate(rtp_session
->estimators
[EST_RTT
], rtt_now
, EST_RTT
);
6653 if (switch_kalman_cusum_detect_change(rtp_session
->detectors
[EST_RTT
], rtt_now
, rtp_session
->estimators
[EST_RTT
]->val_estimate_last
)) {
6654 /* sudden change in the mean value of RTT */
6655 #ifdef DEBUG_ESTIMATORS_
6656 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
,"Sudden change in the mean value of RTT !\n");
6662 switch_kalman_estimate(rtp_session
->estimators
[EST_LOSS
], percent_fraction
, EST_LOSS
);
6664 if (switch_kalman_cusum_detect_change(rtp_session
->detectors
[EST_LOSS
], percent_fraction
, rtp_session
->estimators
[EST_LOSS
]->val_estimate_last
)){
6665 /* sudden change in the mean value of packet loss */
6666 #ifdef DEBUG_ESTIMATORS_
6667 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
,"Sudden change in the mean value of packet loss!\n");
6669 packet_loss_increase
= 1;
6671 #ifdef DEBUG_ESTIMATORS_
6672 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
, "ESTIMATORS: Packet loss will be: [%f] RTT will be: [%f ms]\n",
6673 rtp_session
->estimators
[EST_LOSS
]->val_estimate_last
, rtp_session
->estimators
[EST_RTT
]->val_estimate_last
);
6676 if (rtp_session
->rtcp_frame
.reports
[i
].loss_avg
!= old_avg
) {
6678 if (switch_kalman_is_slow_link(rtp_session
->estimators
[EST_LOSS
],
6679 rtp_session
->estimators
[EST_RTT
])) {
6680 /* going to minimum bitrate */
6681 #ifdef DEBUG_ESTIMATORS_
6682 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
, "Slow link conditions: Loss average: [%d %%], Previous loss: [%d %%]. \
6683 Going to minimum bitrate!",rtp_session
->rtcp_frame
.reports
[i
].loss_avg
, old_avg
);
6685 switch_core_media_codec_control(rtp_session
->session
, SWITCH_MEDIA_TYPE_AUDIO
,
6686 SWITCH_IO_WRITE
, SCC_AUDIO_ADJUST_BITRATE
, SCCT_STRING
, "minimum", SCCT_NONE
, NULL
, NULL
, NULL
);
6687 /* if after going to minimum bitrate we still have packet loss then we increase ptime. TODO */
6689 } else if (packet_loss_increase
&& (rtp_session
->estimators
[EST_LOSS
]->val_estimate_last
>= 5)) {
6690 /* sudden change in the mean value of packet loss percentage */
6691 switch_core_media_codec_control(rtp_session
->session
, SWITCH_MEDIA_TYPE_AUDIO
,
6692 SWITCH_IO_WRITE
, SCC_AUDIO_ADJUST_BITRATE
,
6693 SCCT_STRING
, "decrease",
6694 SCCT_NONE
, NULL
, NULL
, NULL
);
6695 #ifdef DEBUG_ESTIMATORS_
6696 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
,"Sudden change in the mean value of packet loss percentage !\n");
6698 switch_core_media_codec_control(rtp_session
->session
, SWITCH_MEDIA_TYPE_AUDIO
,
6699 SWITCH_IO_WRITE
, SCC_AUDIO_PACKET_LOSS
, SCCT_INT
,
6700 (void *)&rtp_session
->rtcp_frame
.reports
[i
].loss_avg
,
6701 SCCT_NONE
, NULL
, NULL
, NULL
);
6703 } else if (rtt_valid
&& !rtt_increase
&& rtp_session
->estimators
[EST_LOSS
]->val_estimate_last
>= rtp_session
->rtcp_frame
.reports
[i
].loss_avg
) {
6704 /* lossy because of congestion (queues full somewhere -> some packets are dropped , but RTT is good ), packet loss with many small gaps */
6705 #ifdef DEBUG_ESTIMATORS_
6706 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
, "packet loss, but RTT is not bad\n");
6708 switch_core_media_codec_control(rtp_session
->session
, SWITCH_MEDIA_TYPE_AUDIO
,
6709 SWITCH_IO_WRITE
, SCC_AUDIO_PACKET_LOSS
, SCCT_INT
,
6710 (void *)&rtp_session
->rtcp_frame
.reports
[i
].loss_avg
,
6711 SCCT_NONE
, NULL
, NULL
, NULL
);
6713 } else if ((rtp_session
->estimators
[EST_LOSS
]->val_estimate_last
< 1) && packet_loss_increase
) {
6714 #ifdef DEBUG_ESTIMATORS_
6715 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
, "small packet loss average\n");
6718 switch_core_media_codec_control(rtp_session
->session
, SWITCH_MEDIA_TYPE_AUDIO
,
6719 SWITCH_IO_WRITE
, SCC_AUDIO_ADJUST_BITRATE
,
6720 SCCT_STRING
, "default",
6721 SCCT_NONE
, NULL
, NULL
, NULL
);
6723 switch_core_media_codec_control(rtp_session
->session
, SWITCH_MEDIA_TYPE_AUDIO
,
6724 SWITCH_IO_WRITE
, SCC_AUDIO_PACKET_LOSS
, SCCT_INT
,
6725 (void *)&rtp_session
->rtcp_frame
.reports
[i
].loss_avg
,
6726 SCCT_NONE
, NULL
, NULL
, NULL
);
6728 } else if ((rtp_session
->estimators
[EST_LOSS
]->val_estimate_last
< 5) &&
6729 (rtp_session
->rtcp_frame
.reports
[i
].rtt_avg
< rtp_session
->estimators
[EST_RTT
]->val_estimate_last
)) {
6731 /* estimate that packet loss will decrease, we can increase the bitrate */
6732 switch_core_media_codec_control(rtp_session
->session
, SWITCH_MEDIA_TYPE_AUDIO
,
6733 SWITCH_IO_WRITE
, SCC_AUDIO_ADJUST_BITRATE
,
6734 SCCT_STRING
, "increase",
6735 SCCT_NONE
, NULL
, NULL
, NULL
);
6737 switch_core_media_codec_control(rtp_session
->session
, SWITCH_MEDIA_TYPE_AUDIO
,
6738 SWITCH_IO_WRITE
, SCC_AUDIO_PACKET_LOSS
, SCCT_INT
,
6739 (void *)&rtp_session
->rtcp_frame
.reports
[i
].loss_avg
,
6740 SCCT_NONE
, NULL
, NULL
, NULL
);
6743 /* *do nothing about bitrate, just pass the packet loss to the codec */
6744 #ifdef DEBUG_ESTIMATORS_
6745 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
,"do nothing about bitrate, just pass the packet loss to the codec\n");
6747 switch_core_media_codec_control(rtp_session
->session
, SWITCH_MEDIA_TYPE_AUDIO
,
6748 SWITCH_IO_WRITE
, SCC_AUDIO_PACKET_LOSS
, SCCT_INT
,
6749 (void *)&rtp_session
->rtcp_frame
.reports
[i
].loss_avg
,
6750 SCCT_NONE
, NULL
, NULL
, NULL
);
6754 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && rtp_session
->rtcp_frame
.reports
[i
].loss_avg
!= old_avg
) {
6755 switch_core_media_codec_control(rtp_session
->session
, SWITCH_MEDIA_TYPE_AUDIO
,
6756 SWITCH_IO_WRITE
, SCC_AUDIO_PACKET_LOSS
, SCCT_INT
,
6757 (void *)&rtp_session
->rtcp_frame
.reports
[i
].loss_avg
,
6758 SCCT_NONE
, NULL
, NULL
, NULL
);
6764 rtp_session
->rtcp_frame
.report_count
= (uint16_t)i
;
6770 rtp_session
->rtcp_fresh_frame
= 1;
6771 rtp_session
->stats
.rtcp
.peer_ssrc
= ntohl(packet_ssrc
);
6775 if (msg
->header
.type
> 194 && msg
->header
.type
< 255) {
6776 status
= SWITCH_STATUS_SUCCESS
;
6783 static switch_status_t
process_rtcp_packet(switch_rtp_t
*rtp_session
, switch_size_t
*bytes
)
6786 switch_size_t remain
= *bytes
;
6787 switch_status_t status
= SWITCH_STATUS_FALSE
;
6788 rtcp_msg_t
*msg
= rtp_session
->rtcp_recv_msg_p
;
6790 if (remain
< sizeof(switch_rtcp_ext_hdr_t
) || remain
> sizeof(rtcp_msg_t
)) {
6793 if (msg
->header
.version
!= 2) {
6794 if (msg
->header
.version
== 0) {
6795 if (rtp_session
->ice
.ice_user
) {
6796 handle_ice(rtp_session
, &rtp_session
->rtcp_ice
, (void *) msg
, *bytes
);
6798 return SWITCH_STATUS_SUCCESS
;
6800 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
),
6801 SWITCH_LOG_WARNING
, "Received an unsupported RTCP packet version %d\n", msg
->header
.version
);
6802 return SWITCH_STATUS_FALSE
;
6807 len
= ((switch_size_t
)ntohs(msg
->header
.length
) * 4) + 4;
6809 if (msg
->header
.version
!= 2 || !(msg
->header
.type
> 191 && msg
->header
.type
< 210)) {
6810 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
,
6811 "INVALID RTCP PACKET TYPE %d VER %d LEN %" SWITCH_SIZE_T_FMT
"\n", msg
->header
.type
,
6812 msg
->header
.version
, len
);
6813 status
= SWITCH_STATUS_BREAK
;
6817 //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT,
6818 //"WTF BYTES %ld REMAIN %ld PACKET TYPE %d LEN %ld\n", *bytes, remain, msg->header.type, len);
6821 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
,
6822 "RTCP INVALID LENGTH %" SWITCH_SIZE_T_FMT
"\n", len
);
6826 status
= process_rtcp_report(rtp_session
, msg
, len
);
6829 unsigned char *p
= (unsigned char *) msg
;
6831 msg
= (rtcp_msg_t
*) p
;
6836 } while (remain
>= 4);
6841 static switch_status_t
read_rtcp_packet(switch_rtp_t
*rtp_session
, switch_size_t
*bytes
, switch_frame_flag_t
*flags
)
6843 switch_status_t status
= SWITCH_STATUS_FALSE
;
6845 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
6846 return SWITCH_STATUS_FALSE
;
6849 switch_assert(bytes
);
6851 *bytes
= sizeof(rtcp_msg_t
);
6853 if ((status
= switch_socket_recvfrom(rtp_session
->rtcp_from_addr
, rtp_session
->rtcp_sock_input
, 0, (void *) rtp_session
->rtcp_recv_msg_p
, bytes
))
6854 != SWITCH_STATUS_SUCCESS
) {
6858 switch_mutex_lock(rtp_session
->ice_mutex
);
6859 if (rtp_session
->rtcp_dtls
) {
6860 char *b
= (char *) rtp_session
->rtcp_recv_msg_p
;
6862 if (*b
== 0 || *b
== 1) {
6863 if (rtp_session
->rtcp_ice
.ice_user
) {
6864 handle_ice(rtp_session
, &rtp_session
->rtcp_ice
, (void *) rtp_session
->rtcp_recv_msg_p
, *bytes
);
6869 if (*bytes
&& (*b
>= 20) && (*b
<= 64)) {
6870 rtp_session
->rtcp_dtls
->bytes
= *bytes
;
6871 rtp_session
->rtcp_dtls
->data
= (void *) rtp_session
->rtcp_recv_msg_p
;
6873 rtp_session
->rtcp_dtls
->bytes
= 0;
6874 rtp_session
->rtcp_dtls
->data
= NULL
;
6877 do_dtls(rtp_session
, rtp_session
->rtcp_dtls
);
6880 if (rtp_session
->rtcp_dtls
->bytes
) {
6886 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV
] && rtp_session
->rtcp_recv_msg_p
->header
.version
== 2) {
6887 //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->rtcp_recv_msg_p->header.version == 2)) {
6888 int sbytes
= (int) *bytes
;
6889 srtp_err_status_t stat
= 0;
6892 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_RECV_MKI
]) {
6893 stat
= srtp_unprotect_rtcp(rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtcp
], &rtp_session
->rtcp_recv_msg_p
->header
, &sbytes
);
6895 stat
= srtp_unprotect_rtcp_mki(rtp_session
->recv_ctx
[rtp_session
->srtp_idx_rtcp
], &rtp_session
->rtcp_recv_msg_p
->header
, &sbytes
, 1);
6899 //++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++;
6900 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "RTCP UNPROTECT ERR\n");
6902 //rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
6910 switch_mutex_unlock(rtp_session
->ice_mutex
);
6914 if (*bytes
&& rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_AUTOADJ
] && switch_sockaddr_get_port(rtp_session
->rtcp_from_addr
)) {
6915 if (!switch_cmp_addr(rtp_session
->rtcp_from_addr
, rtp_session
->rtcp_remote_addr
, SWITCH_FALSE
)) {
6916 if (++rtp_session
->rtcp_autoadj_tally
>= rtp_session
->rtcp_autoadj_threshold
) {
6918 uint32_t old
= rtp_session
->remote_rtcp_port
;
6919 const char *tx_host
;
6920 const char *old_host
;
6921 char bufa
[50], bufb
[50];
6923 tx_host
= switch_get_addr(bufa
, sizeof(bufa
), rtp_session
->rtcp_from_addr
);
6924 old_host
= switch_get_addr(bufb
, sizeof(bufb
), rtp_session
->rtcp_remote_addr
);
6926 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_INFO
,
6927 "Auto Changing %s RTCP port from %s:%u to %s:%u\n", rtp_type(rtp_session
), old_host
, old
, tx_host
,
6928 switch_sockaddr_get_port(rtp_session
->rtcp_from_addr
));
6931 rtp_session
->eff_remote_host_str
= switch_core_strdup(rtp_session
->pool
, tx_host
);
6932 rtp_session
->remote_rtcp_port
= switch_sockaddr_get_port(rtp_session
->rtcp_from_addr
);
6933 status
= enable_remote_rtcp_socket(rtp_session
, &err
);
6934 rtp_session
->rtcp_auto_adj_used
= 1;
6936 if ((rtp_session
->rtp_bugs
& RTP_BUG_ALWAYS_AUTO_ADJUST
)) {
6937 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_RTCP_AUTOADJ
);
6939 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_RTCP_AUTOADJ
);
6944 if ((rtp_session
->rtp_bugs
& RTP_BUG_ALWAYS_AUTO_ADJUST
)) {
6945 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_RTCP_AUTOADJ
);
6947 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
),
6948 SWITCH_LOG_DEBUG
, "Correct %s RTCP ip/port confirmed.\n", rtp_type(rtp_session
));
6949 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_RTCP_AUTOADJ
);
6951 rtp_session
->rtcp_auto_adj_used
= 0;
6957 return process_rtcp_packet(rtp_session
, bytes
);
6963 static void check_timeout(switch_rtp_t
*rtp_session
)
6966 switch_time_t now
= switch_micro_time_now();
6967 uint32_t elapsed
= 0;
6969 if (now
>= rtp_session
->last_media
) {
6970 elapsed
= (now
- rtp_session
->last_media
) / 1000;
6973 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG10
,
6974 "%s MEDIA TIMEOUT %s %d/%d\n", switch_core_session_get_name(rtp_session
->session
), rtp_type(rtp_session
),
6975 elapsed
, rtp_session
->media_timeout
);
6977 if (elapsed
> rtp_session
->media_timeout
) {
6978 switch_channel_t
*channel
= switch_core_session_get_channel(rtp_session
->session
);
6980 switch_channel_execute_on(channel
, "execute_on_media_timeout");
6981 switch_channel_hangup(channel
, SWITCH_CAUSE_MEDIA_TIMEOUT
);
6985 static int rtp_common_read(switch_rtp_t
*rtp_session
, switch_payload_t
*payload_type
,
6986 payload_map_t
**pmapP
, switch_frame_flag_t
*flags
, switch_io_flag_t io_flags
)
6989 switch_channel_t
*channel
= NULL
;
6990 switch_size_t bytes
= 0;
6991 switch_size_t rtcp_bytes
= 0;
6992 switch_status_t status
= SWITCH_STATUS_SUCCESS
, poll_status
= SWITCH_STATUS_SUCCESS
;
6993 switch_status_t rtcp_status
= SWITCH_STATUS_SUCCESS
, rtcp_poll_status
= SWITCH_STATUS_SUCCESS
;
6996 int sleep_mss
= 1000;
7004 switch_bool_t got_jb
= SWITCH_FALSE
;
7006 if (!switch_rtp_ready(rtp_session
)) {
7010 if (rtp_session
->session
) {
7011 channel
= switch_core_session_get_channel(rtp_session
->session
);
7014 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
]) {
7015 sleep_mss
= rtp_session
->timer
.interval
* 1000;
7018 READ_INC(rtp_session
);
7022 while (switch_rtp_ready(rtp_session
)) {
7024 int read_pretriggered
= 0;
7026 int got_rtp_poll
= 0;
7030 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
] &&
7031 !rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] &&
7032 !rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] &&
7033 !rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
] &&
7034 rtp_session
->read_pollfd
) {
7036 if (rtp_session
->jb
&& !rtp_session
->pause_jb
&& jb_valid(rtp_session
)) {
7037 while (switch_poll(rtp_session
->read_pollfd
, 1, &fdr
, 0) == SWITCH_STATUS_SUCCESS
) {
7038 status
= read_rtp_packet(rtp_session
, &bytes
, flags
, pmapP
, SWITCH_STATUS_SUCCESS
, SWITCH_FALSE
);
7040 if (status
== SWITCH_STATUS_GENERR
) {
7045 if ((*flags
& SFF_RTCP
)) {
7046 *flags
&= ~SFF_RTCP
;
7048 read_pretriggered
= 0;
7052 if (status
== SWITCH_STATUS_BREAK
) {
7053 read_pretriggered
= 1;
7058 } else if ((rtp_session
->flags
[SWITCH_RTP_FLAG_AUTOFLUSH
] || rtp_session
->flags
[SWITCH_RTP_FLAG_STICKY_FLUSH
])) {
7060 if (switch_poll(rtp_session
->read_pollfd
, 1, &fdr
, 0) == SWITCH_STATUS_SUCCESS
) {
7061 status
= read_rtp_packet(rtp_session
, &bytes
, flags
, pmapP
, SWITCH_STATUS_SUCCESS
, SWITCH_FALSE
);
7062 if (status
== SWITCH_STATUS_GENERR
) {
7066 if ((*flags
& SFF_RTCP
)) {
7067 *flags
&= ~SFF_RTCP
;
7069 read_pretriggered
= 0;
7073 /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Initial (%i) %d\n", status, bytes); */
7074 if (status
!= SWITCH_STATUS_FALSE
) {
7075 read_pretriggered
= 1;
7079 if (switch_poll(rtp_session
->read_pollfd
, 1, &fdr
, 0) == SWITCH_STATUS_SUCCESS
) {
7080 rtp_session
->hot_hits
++;//+= rtp_session->samples_per_interval;
7082 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG10
, "%s Hot Hit %d\n",
7083 rtp_session_name(rtp_session
),
7084 rtp_session
->hot_hits
);
7086 rtp_session
->hot_hits
= 0;
7090 if (rtp_session
->hot_hits
> 1 && !rtp_session
->sync_packets
) {// >= (rtp_session->samples_per_second * 30)) {
7094 rtp_session
->hot_hits
= 0;
7098 if (rtp_session
->flags
[SWITCH_RTP_FLAG_TEXT
]) {
7100 } else if (hot_socket
&& (rtp_session
->hot_hits
% 10) != 0) {
7101 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG10
, "%s timer while HOT\n", rtp_session_name(rtp_session
));
7102 switch_core_timer_next(&rtp_session
->timer
);
7103 } else if (hot_socket
) {
7104 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG10
, "%s skip timer once\n", rtp_session_name(rtp_session
));
7105 rtp_session
->sync_packets
++;
7106 switch_core_timer_sync(&rtp_session
->timer
);
7107 reset_jitter_seq(rtp_session
);
7110 if (rtp_session
->sync_packets
) {
7112 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG10
,
7113 "%s Auto-Flush catching up %d packets (%d)ms.\n",
7114 rtp_session_name(rtp_session
),
7115 rtp_session
->sync_packets
, (rtp_session
->ms_per_packet
* rtp_session
->sync_packets
) / 1000);
7116 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_PAUSE
]) {
7117 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
, "%s syncing %d %s packet(s)\n",
7118 rtp_session_name(rtp_session
),
7119 rtp_session
->sync_packets
, rtp_type(rtp_session
));
7121 rtp_session
->bad_stream
++;
7122 rtp_session
->stats
.inbound
.flaws
+= rtp_session
->sync_packets
;
7124 if (rtp_session
->stats
.inbound
.error_log
) {
7125 rtp_session
->stats
.inbound
.error_log
->flaws
+= rtp_session
->sync_packets
;
7129 switch_core_timer_sync(&rtp_session
->timer
);
7130 reset_jitter_seq(rtp_session
);
7131 rtp_session
->hot_hits
= 0;
7136 if (rtp_session
->skip_timer
) {
7137 rtp_session
->skip_timer
= 0;
7140 switch_core_timer_next(&rtp_session
->timer
);
7147 rtp_session
->sync_packets
= 0;
7151 rtp_session
->stats
.read_count
++;
7155 if (!read_pretriggered
) {
7161 if (!switch_rtp_ready(rtp_session
)) {
7165 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
] && rtp_session
->read_pollfd
) {
7166 int pt
= poll_sec
* 1000000;
7168 do_2833(rtp_session
);
7170 if (rtp_session
->dtmf_data
.out_digit_dur
> 0 || rtp_session
->dtmf_data
.in_digit_sanity
|| rtp_session
->sending_dtmf
||
7171 switch_queue_size(rtp_session
->dtmf_data
.dtmf_queue
) || switch_queue_size(rtp_session
->dtmf_data
.dtmf_inqueue
)) {
7175 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && !rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
]) {
7179 if (rtp_session
->vb
&& !rtp_session
->pause_jb
) {
7180 if (switch_jb_poll(rtp_session
->vb
)) {
7185 if ((io_flags
& SWITCH_IO_FLAG_NOBLOCK
)) {
7189 poll_status
= switch_poll(rtp_session
->read_pollfd
, 1, &fdr
, pt
);
7191 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && poll_status
!= SWITCH_STATUS_SUCCESS
&& rtp_session
->media_timeout
&& rtp_session
->last_media
) {
7192 check_timeout(rtp_session
);
7195 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && rtp_session
->dtmf_data
.out_digit_dur
> 0) {
7199 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && rtp_session
->flags
[SWITCH_RTP_FLAG_BREAK
]) {
7200 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_BREAK
);
7202 reset_jitter_seq(rtp_session
);
7208 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
7209 got_jb
= (rtp_session
->vb
&& !rtp_session
->pause_jb
&& switch_jb_poll(rtp_session
->vb
));
7211 got_jb
= SWITCH_TRUE
;
7214 if (poll_status
== SWITCH_STATUS_SUCCESS
|| got_jb
) {
7218 if (read_pretriggered
) {
7219 read_pretriggered
= 0;
7223 status
= read_rtp_packet(rtp_session
, &bytes
, flags
, pmapP
, poll_status
, got_jb
);
7225 if (status
== SWITCH_STATUS_GENERR
) {
7230 if (rtp_session
->max_missed_packets
&& read_loops
== 1 && !rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] &&
7231 !rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
]) {
7232 if (bytes
&& status
== SWITCH_STATUS_SUCCESS
) {
7233 rtp_session
->missed_count
= 0;
7235 if (rtp_session
->media_timeout
&& rtp_session
->last_media
) {
7236 check_timeout(rtp_session
);
7238 if (++rtp_session
->missed_count
>= rtp_session
->max_missed_packets
) {
7246 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
7247 //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "Read bytes (%i) %ld\n", status, bytes);
7250 if (check_rtcp_and_ice(rtp_session
) == -1) {
7255 //switch_rtp_video_refresh(rtp_session);
7260 if ((*flags
& SFF_PROXY_PACKET
)) {
7265 if ((*flags
& SFF_RTCP
)) {
7266 *flags
&= ~SFF_RTCP
;
7276 if (!switch_rtp_ready(rtp_session
)) {
7281 if (!SWITCH_STATUS_IS_BREAK(poll_status
) && poll_status
!= SWITCH_STATUS_TIMEOUT
) {
7283 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Poll failed with error: %d [%s]\n",
7284 poll_status
, switch_strerror_r(poll_status
, tmp
, sizeof(tmp
)));
7289 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
] && !rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
]) {
7290 rtp_session
->missed_count
+= (poll_sec
* 1000) / (rtp_session
->ms_per_packet
? rtp_session
->ms_per_packet
/ 1000 : 20);
7293 if (rtp_session
->media_timeout
&& rtp_session
->last_media
) {
7294 check_timeout(rtp_session
);
7295 } else if (rtp_session
->max_missed_packets
) {
7296 if (rtp_session
->missed_count
>= rtp_session
->max_missed_packets
) {
7304 if (check_rtcp_and_ice(rtp_session
) == -1) {
7310 if ((!(io_flags
& SWITCH_IO_FLAG_NOBLOCK
)) &&
7311 (rtp_session
->dtmf_data
.out_digit_dur
== 0) && !rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
7318 if (rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
7319 rtcp_poll_status
= SWITCH_STATUS_FALSE
;
7321 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
] && has_rtcp
) {
7322 if (rtp_session
->rtcp_recv_msg_p
->header
.version
== 2) { //rtcp muxed
7323 rtp_session
->rtcp_from_addr
= rtp_session
->from_addr
;
7324 rtcp_status
= rtcp_poll_status
= SWITCH_STATUS_SUCCESS
;
7330 } else if (rtp_session
->rtcp_read_pollfd
) {
7331 rtcp_poll_status
= switch_poll(rtp_session
->rtcp_read_pollfd
, 1, &rtcp_fdr
, 0);
7334 if (rtcp_poll_status
== SWITCH_STATUS_SUCCESS
) {
7336 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
7337 rtcp_status
= read_rtcp_packet(rtp_session
, &rtcp_bytes
, flags
);
7340 if (rtcp_status
== SWITCH_STATUS_SUCCESS
) {
7341 switch_rtp_reset_media_timer(rtp_session
);
7343 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_PASSTHRU
]) {
7344 switch_channel_t
*channel
= switch_core_session_get_channel(rtp_session
->session
);
7345 const char *uuid
= switch_channel_get_partner_uuid(channel
);
7348 switch_core_session_t
*other_session
;
7349 switch_rtp_t
*other_rtp_session
= NULL
;
7351 if ((other_session
= switch_core_session_locate(uuid
))) {
7352 switch_channel_t
*other_channel
= switch_core_session_get_channel(other_session
);
7353 if ((other_rtp_session
= switch_channel_get_private(other_channel
, "__rtcp_audio_rtp_session")) &&
7354 other_rtp_session
->rtcp_sock_output
&&
7355 switch_rtp_test_flag(other_rtp_session
, SWITCH_RTP_FLAG_ENABLE_RTCP
)) {
7356 other_rtp_session
->rtcp_send_msg
= rtp_session
->rtcp_recv_msg
;
7359 switch_mutex_lock(other_rtp_session
->ice_mutex
);
7360 if (switch_rtp_test_flag(other_rtp_session
, SWITCH_RTP_FLAG_SECURE_SEND
)) {
7362 int sbytes
= (int) rtcp_bytes
;
7364 if (!other_rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND_MKI
]) {
7365 stat
= srtp_protect_rtcp(other_rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtcp
], &other_rtp_session
->rtcp_send_msg
.header
, &sbytes
);
7367 stat
= srtp_protect_rtcp_mki(other_rtp_session
->send_ctx
[other_rtp_session
->srtp_idx_rtcp
], &other_rtp_session
->rtcp_send_msg
.header
, &sbytes
, 1, SWITCH_CRYPTO_MKI_INDEX
);
7371 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Error: SRTP RTCP protection failed with code %d\n", stat
);
7373 rtcp_bytes
= sbytes
;
7375 switch_mutex_unlock(other_rtp_session
->ice_mutex
);
7378 if (switch_socket_sendto(other_rtp_session
->rtcp_sock_output
, other_rtp_session
->rtcp_remote_addr
, 0,
7379 (const char*)&other_rtp_session
->rtcp_send_msg
, &rtcp_bytes
) != SWITCH_STATUS_SUCCESS
) {
7380 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
,"RTCP packet not written\n");
7385 switch_core_session_rwunlock(other_session
);
7391 if (rtp_session
->flags
[SWITCH_RTP_FLAG_RTCP_MUX
]) {
7392 process_rtcp_packet(rtp_session
, &rtcp_bytes
);
7401 if ((!(io_flags
& SWITCH_IO_FLAG_NOBLOCK
)) &&
7402 (rtp_session
->dtmf_data
.out_digit_dur
== 0) && !got_rtp_poll
) {
7406 if (!bytes
&& (io_flags
& SWITCH_IO_FLAG_NOBLOCK
)) {
7407 rtp_session
->missed_count
= 0;
7414 if (rtp_session
->flags
[SWITCH_RTP_FLAG_FLUSH
]) {
7415 bytes
= do_flush(rtp_session
, SWITCH_FALSE
, bytes
);
7416 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_FLUSH
);
7419 if ((!bytes
&& rtp_session
->flags
[SWITCH_RTP_FLAG_BREAK
]) || (bytes
&& bytes
== 4 && *((int *) &rtp_session
->recv_msg
) == UINT_MAX
)) {
7420 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_BREAK
);
7422 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_NOBLOCK
] || !rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
] ||
7423 rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] || rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
] ||
7424 (bytes
&& bytes
< 5) || (!bytes
&& poll_loop
)) {
7426 reset_jitter_seq(rtp_session
);
7431 if (bytes
&& bytes
< 5) {
7435 if (!bytes
&& poll_loop
) {
7439 if (bytes
&& rtp_session
->last_rtp_hdr
.m
&& rtp_session
->last_rtp_hdr
.pt
!= rtp_session
->recv_te
&&
7440 !rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] &&
7441 !rtp_session
->flags
[SWITCH_RTP_FLAG_TEXT
] &&
7442 !(rtp_session
->rtp_bugs
& RTP_BUG_IGNORE_MARK_BIT
)) {
7443 rtp_flush_read_buffer(rtp_session
, SWITCH_RTP_FLUSH_ONCE
);
7446 if (rtp_session
->last_rtp_hdr
.pt
== rtp_session
->cng_pt
|| rtp_session
->last_rtp_hdr
.pt
== 13) {
7447 *flags
|= SFF_NOT_AUDIO
;
7449 *flags
&= ~SFF_NOT_AUDIO
; /* If this flag was already set, make sure to remove it when we get real audio */
7452 /* ignore packets not meant for us unless the auto-adjust window is open (ice mode has its own alternatives to this) */
7453 if (!using_ice(rtp_session
) && bytes
) {
7454 if (rtp_session
->flags
[SWITCH_RTP_FLAG_AUTOADJ
]) {
7455 if (rtp_session
->last_rtp_hdr
.pt
== rtp_session
->cng_pt
|| rtp_session
->last_rtp_hdr
.pt
== 13) {
7459 } else if (!(rtp_session
->rtp_bugs
& RTP_BUG_ACCEPT_ANY_PACKETS
) && !switch_cmp_addr(rtp_session
->rtp_from_addr
, rtp_session
->remote_addr
, SWITCH_FALSE
)) {
7464 if (bytes
&& rtp_session
->flags
[SWITCH_RTP_FLAG_AUTOADJ
] && switch_sockaddr_get_port(rtp_session
->rtp_from_addr
)) {
7465 if (!switch_cmp_addr(rtp_session
->rtp_from_addr
, rtp_session
->remote_addr
, SWITCH_FALSE
)) {
7466 if (++rtp_session
->autoadj_tally
>= rtp_session
->autoadj_threshold
) {
7468 uint32_t old
= rtp_session
->remote_port
;
7469 const char *tx_host
;
7470 const char *old_host
;
7471 char bufa
[50], bufb
[50];
7474 tx_host
= switch_get_addr(bufa
, sizeof(bufa
), rtp_session
->rtp_from_addr
);
7475 old_host
= switch_get_addr(bufb
, sizeof(bufb
), rtp_session
->remote_addr
);
7477 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_INFO
,
7478 "Auto Changing %s port from %s:%u to %s:%u\n", rtp_type(rtp_session
), old_host
, old
, tx_host
,
7479 switch_sockaddr_get_port(rtp_session
->rtp_from_addr
));
7482 char varname
[80] = "";
7484 switch_snprintf(varname
, sizeof(varname
), "remote_%s_ip_reported", rtp_type(rtp_session
));
7485 switch_channel_set_variable(channel
, varname
, switch_channel_get_variable(channel
, "remote_media_ip"));
7487 switch_snprintf(varname
, sizeof(varname
), "remote_%s_ip", rtp_type(rtp_session
));
7488 switch_channel_set_variable(channel
, varname
, tx_host
);
7490 switch_snprintf(varname
, sizeof(varname
), "remote_%s_port_reported", rtp_type(rtp_session
));
7491 switch_snprintf(adj_port
, sizeof(adj_port
), "%u", switch_sockaddr_get_port(rtp_session
->rtp_from_addr
));
7492 switch_channel_set_variable(channel
, varname
, switch_channel_get_variable(channel
, "remote_media_port"));
7494 switch_snprintf(varname
, sizeof(varname
), "remote_%s_port", rtp_type(rtp_session
));
7495 switch_channel_set_variable(channel
, varname
, adj_port
);
7497 switch_snprintf(varname
, sizeof(varname
), "rtp_auto_adjust_%s", rtp_type(rtp_session
));
7498 switch_channel_set_variable(channel
, varname
, "true");
7500 rtp_session
->auto_adj_used
= 1;
7501 switch_rtp_set_remote_address(rtp_session
, tx_host
, switch_sockaddr_get_port(rtp_session
->rtp_from_addr
), 0, SWITCH_FALSE
, &err
);
7502 if ((rtp_session
->rtp_bugs
& RTP_BUG_ALWAYS_AUTO_ADJUST
)) {
7503 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_AUTOADJ
);
7504 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_RTCP_AUTOADJ
);
7506 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_AUTOADJ
);
7508 if (rtp_session
->ice
.ice_user
) {
7509 rtp_session
->ice
.addr
= rtp_session
->remote_addr
;
7513 if ((rtp_session
->rtp_bugs
& RTP_BUG_ALWAYS_AUTO_ADJUST
)) {
7514 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_AUTOADJ
);
7515 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_RTCP_AUTOADJ
);
7517 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
, "Correct %s ip/port confirmed.\n", rtp_type(rtp_session
));
7518 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_AUTOADJ
);
7520 rtp_session
->auto_adj_used
= 0;
7524 if (bytes
&& !(rtp_session
->rtp_bugs
& RTP_BUG_ALWAYS_AUTO_ADJUST
) && rtp_session
->autoadj_window
) {
7525 if (--rtp_session
->autoadj_window
== 0) {
7526 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_AUTOADJ
);
7530 if (rtp_session
->flags
[SWITCH_RTP_FLAG_TEXT
]) {
7532 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
]) {
7533 switch_core_timer_next(&rtp_session
->timer
);
7537 *payload_type
= rtp_session
->last_rtp_hdr
.pt
;
7543 if (bytes
&& (rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] || rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
])) {
7545 *flags
|= SFF_PROXY_PACKET
;
7547 if (rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
]) {
7549 if (rtp_session
->has_rtp
&& check_recv_payload(rtp_session
)) {
7550 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
,
7551 "Ignoring udptl packet of size of %ld bytes that looks strikingly like a RTP packet.\n", (long)bytes
);
7556 *flags
|= SFF_UDPTL_PACKET
;
7564 rtp_session
->missed_count
= 0;
7566 if (bytes
< rtp_header_len
) {
7567 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
, "Ignoring invalid RTP packet size of %ld bytes.\n", (long)bytes
);
7572 if (rtp_session
->last_rtp_hdr
.pt
&& (rtp_session
->last_rtp_hdr
.pt
== rtp_session
->cng_pt
|| rtp_session
->last_rtp_hdr
.pt
== 13)) {
7577 if (check
|| bytes
) {
7578 do_2833(rtp_session
);
7581 if (bytes
&& rtp_session
->recv_msg
.header
.version
!= 2) {
7582 uint8_t *data
= (uint8_t *) RTP_BODY(rtp_session
);
7584 //if (rtp_session->recv_msg.header.version == 0) {
7585 // if (rtp_session->ice.ice_user) {
7586 // handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, bytes);
7591 if (rtp_session
->invalid_handler
) {
7592 rtp_session
->invalid_handler(rtp_session
, rtp_session
->sock_input
, (void *) &rtp_session
->recv_msg
, bytes
, rtp_session
->rtp_from_addr
);
7598 rtp_session
->last_rtp_hdr
.pt
= rtp_session
->cng_pt
!= INVALID_PT
? rtp_session
->cng_pt
: SWITCH_RTP_CNG_PAYLOAD
;
7600 *payload_type
= (switch_payload_t
) rtp_session
->last_rtp_hdr
.pt
;
7601 ret
= 2 + rtp_header_len
;
7604 rtp_session
->stats
.inbound
.period_packet_count
++;
7608 /* Handle incoming RFC2833 packets */
7609 switch (handle_rfc2833(rtp_session
, bytes
, &do_cng
)) {
7610 case RESULT_GOTO_END
:
7612 case RESULT_GOTO_RECVFROM
:
7614 case RESULT_GOTO_TIMERCHECK
:
7616 case RESULT_CONTINUE
:
7617 status
= SWITCH_STATUS_SUCCESS
;
7618 goto result_continue
;
7624 if (!rtp_session
->media_timeout
&& rtp_session
->flags
[SWITCH_RTP_FLAG_MUTE
]) {
7629 uint8_t *data
= (uint8_t *) RTP_BODY(rtp_session
);
7631 do_2833(rtp_session
);
7633 if (rtp_session
->last_cng_ts
== rtp_session
->last_read_ts
+ rtp_session
->samples_per_interval
) {
7634 rtp_session
->last_cng_ts
= 0;
7636 rtp_session
->last_cng_ts
= rtp_session
->last_read_ts
+ rtp_session
->samples_per_interval
;
7641 rtp_session
->last_rtp_hdr
.pt
= rtp_session
->cng_pt
!= INVALID_PT
? rtp_session
->cng_pt
: SWITCH_RTP_CNG_PAYLOAD
;
7643 *payload_type
= (switch_payload_t
) rtp_session
->last_rtp_hdr
.pt
;
7644 ret
= 2 + rtp_header_len
;
7645 rtp_session
->stats
.inbound
.skip_packet_count
++;
7650 if (check
|| (bytes
&& !rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
])) {
7651 if (!bytes
&& rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
]) { /* We're late! We're Late! */
7652 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_NOBLOCK
] && status
== SWITCH_STATUS_BREAK
) {
7659 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_PAUSE
] && !rtp_session
->flags
[SWITCH_RTP_FLAG_DTMF_ON
] && !rtp_session
->dtmf_data
.in_digit_ts
7660 && rtp_session
->cng_count
> (rtp_session
->one_second
* 2) && rtp_session
->jitter_lead
> JITTER_LEAD_FRAMES
) {
7662 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
, "%s %s timeout\n",
7663 rtp_session_name(rtp_session
), rtp_type(rtp_session
));
7665 if (rtp_session
->media_timeout
&& rtp_session
->last_media
) {
7666 check_timeout(rtp_session
);
7669 if (rtp_session
->stats
.inbound
.error_log
) {
7670 rtp_session
->stats
.inbound
.error_log
->flaws
++;
7672 rtp_session
->stats
.inbound
.flaws
++;
7673 do_mos(rtp_session
);
7676 rtp_session
->cng_count
++;
7681 rtp_session
->cng_count
= 0;
7683 if (status
== SWITCH_STATUS_BREAK
|| bytes
== 0) {
7684 if (!(io_flags
& SWITCH_IO_FLAG_SINGLE_READ
) && rtp_session
->flags
[SWITCH_RTP_FLAG_DATAWAIT
]) {
7690 if (rtp_session
->flags
[SWITCH_RTP_FLAG_GOOGLEHACK
] && rtp_session
->last_rtp_hdr
.pt
== 102) {
7691 rtp_session
->last_rtp_hdr
.pt
= 97;
7698 if (!bytes
&& !rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
] && !switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_VIDEO
)) {
7701 switch_yield(sleep_mss
);
7707 if (switch_rtp_ready(rtp_session
)) {
7708 *payload_type
= (switch_payload_t
) rtp_session
->last_rtp_hdr
.pt
;
7710 if (*payload_type
== SWITCH_RTP_CNG_PAYLOAD
) {
7721 READ_DEC(rtp_session
);
7727 SWITCH_DECLARE(switch_byte_t
) switch_rtp_check_auto_adj(switch_rtp_t
*rtp_session
)
7729 return rtp_session
->auto_adj_used
;
7732 SWITCH_DECLARE(switch_size_t
) switch_rtp_has_dtmf(switch_rtp_t
*rtp_session
)
7734 switch_size_t has
= 0;
7736 if (switch_rtp_ready(rtp_session
)) {
7737 switch_mutex_lock(rtp_session
->dtmf_data
.dtmf_mutex
);
7738 has
= switch_queue_size(rtp_session
->dtmf_data
.dtmf_inqueue
);
7739 switch_mutex_unlock(rtp_session
->dtmf_data
.dtmf_mutex
);
7745 SWITCH_DECLARE(switch_size_t
) switch_rtp_dequeue_dtmf(switch_rtp_t
*rtp_session
, switch_dtmf_t
*dtmf
)
7747 switch_size_t bytes
= 0;
7748 switch_dtmf_t
*_dtmf
= NULL
;
7751 if (!switch_rtp_ready(rtp_session
)) {
7755 switch_mutex_lock(rtp_session
->dtmf_data
.dtmf_mutex
);
7756 if (switch_queue_trypop(rtp_session
->dtmf_data
.dtmf_inqueue
, &pop
) == SWITCH_STATUS_SUCCESS
) {
7758 _dtmf
= (switch_dtmf_t
*)pop
;
7760 /* Only log DTMF buffer if sensitive_dtmf channel variable not set to true */
7761 if (!(switch_channel_var_true(switch_core_session_get_channel(rtp_session
->session
), SWITCH_SENSITIVE_DTMF_VARIABLE
))) {
7762 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
,"RTP RECV DTMF %c:%d\n", dtmf
->digit
, dtmf
->duration
);
7767 switch_mutex_unlock(rtp_session
->dtmf_data
.dtmf_mutex
);
7772 SWITCH_DECLARE(switch_status_t
) switch_rtp_queue_rfc2833(switch_rtp_t
*rtp_session
, const switch_dtmf_t
*dtmf
)
7775 switch_dtmf_t
*rdigit
;
7777 if (!switch_rtp_ready(rtp_session
)) {
7778 return SWITCH_STATUS_FALSE
;
7781 if ((rdigit
= malloc(sizeof(*rdigit
))) != 0) {
7783 if (rdigit
->duration
< switch_core_min_dtmf_duration(0)) {
7784 rdigit
->duration
= switch_core_min_dtmf_duration(0);
7787 if ((switch_queue_trypush(rtp_session
->dtmf_data
.dtmf_queue
, rdigit
)) != SWITCH_STATUS_SUCCESS
) {
7789 return SWITCH_STATUS_FALSE
;
7795 return SWITCH_STATUS_SUCCESS
;
7798 SWITCH_DECLARE(switch_status_t
) switch_rtp_queue_rfc2833_in(switch_rtp_t
*rtp_session
, const switch_dtmf_t
*dtmf
)
7800 switch_dtmf_t
*rdigit
;
7802 if (!switch_rtp_ready(rtp_session
)) {
7803 return SWITCH_STATUS_FALSE
;
7806 if ((rdigit
= malloc(sizeof(*rdigit
))) != 0) {
7808 if (rdigit
->duration
< switch_core_min_dtmf_duration(0)) {
7809 rdigit
->duration
= switch_core_min_dtmf_duration(0);
7812 if ((switch_queue_trypush(rtp_session
->dtmf_data
.dtmf_inqueue
, rdigit
)) != SWITCH_STATUS_SUCCESS
) {
7814 return SWITCH_STATUS_FALSE
;
7820 return SWITCH_STATUS_SUCCESS
;
7823 SWITCH_DECLARE(switch_status_t
) switch_rtp_read(switch_rtp_t
*rtp_session
, void *data
, uint32_t *datalen
,
7824 switch_payload_t
*payload_type
, switch_frame_flag_t
*flags
, switch_io_flag_t io_flags
)
7828 if (!switch_rtp_ready(rtp_session
)) {
7829 return SWITCH_STATUS_FALSE
;
7832 bytes
= rtp_common_read(rtp_session
, payload_type
, NULL
, flags
, io_flags
);
7836 return bytes
== -2 ? SWITCH_STATUS_TIMEOUT
: SWITCH_STATUS_GENERR
;
7837 } else if (bytes
== 0) {
7839 return SWITCH_STATUS_BREAK
;
7841 if (bytes
> rtp_header_len
) {
7842 bytes
-= rtp_header_len
;
7848 memcpy(data
, RTP_BODY(rtp_session
), bytes
);
7850 return SWITCH_STATUS_SUCCESS
;
7853 SWITCH_DECLARE(switch_status_t
) switch_rtcp_zerocopy_read_frame(switch_rtp_t
*rtp_session
, switch_rtcp_frame_t
*frame
)
7856 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
7857 return SWITCH_STATUS_FALSE
;
7860 /* A fresh frame has been found! */
7861 if (rtp_session
->rtcp_fresh_frame
) {
7862 /* turn the flag off! */
7863 rtp_session
->rtcp_fresh_frame
= 0;
7865 *frame
= rtp_session
->rtcp_frame
;
7867 return SWITCH_STATUS_SUCCESS
;
7870 return SWITCH_STATUS_TIMEOUT
;
7873 SWITCH_DECLARE(switch_status_t
) switch_rtp_zerocopy_read_frame(switch_rtp_t
*rtp_session
, switch_frame_t
*frame
, switch_io_flag_t io_flags
)
7877 if (!switch_rtp_ready(rtp_session
)) {
7878 return SWITCH_STATUS_FALSE
;
7881 bytes
= rtp_common_read(rtp_session
, &frame
->payload
, &frame
->pmap
, &frame
->flags
, io_flags
);
7883 frame
->data
= RTP_BODY(rtp_session
);
7885 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
] && (bytes
< rtp_header_len
|| switch_test_flag(frame
, SFF_CNG
))) {
7886 frame
->packet
= NULL
;
7887 frame
->timestamp
= 0;
7893 frame
->packet
= &rtp_session
->recv_msg
;
7894 frame
->packetlen
= bytes
;
7895 frame
->source
= __FILE__
;
7897 switch_set_flag(frame
, SFF_RAW_RTP
);
7898 switch_set_flag(frame
, SFF_EXTERNAL
);
7899 if (frame
->payload
== rtp_session
->recv_te
) {
7900 switch_set_flag(frame
, SFF_RFC2833
);
7902 frame
->timestamp
= ntohl(rtp_session
->last_rtp_hdr
.ts
);
7903 frame
->seq
= (uint16_t) ntohs((uint16_t) rtp_session
->last_rtp_hdr
.seq
);
7904 frame
->ssrc
= ntohl(rtp_session
->last_rtp_hdr
.ssrc
);
7905 frame
->m
= rtp_session
->last_rtp_hdr
.m
? SWITCH_TRUE
: SWITCH_FALSE
;
7911 return bytes
== -2 ? SWITCH_STATUS_TIMEOUT
: SWITCH_STATUS_GENERR
;
7912 } else if (!rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
]) {
7913 if (bytes
< rtp_header_len
) {
7915 return SWITCH_STATUS_BREAK
;
7917 bytes
-= rtp_header_len
;
7921 frame
->datalen
= bytes
;
7922 return SWITCH_STATUS_SUCCESS
;
7925 SWITCH_DECLARE(switch_status_t
) switch_rtp_zerocopy_read(switch_rtp_t
*rtp_session
,
7926 void **data
, uint32_t *datalen
, switch_payload_t
*payload_type
, switch_frame_flag_t
*flags
,
7927 switch_io_flag_t io_flags
)
7931 if (!switch_rtp_ready(rtp_session
)) {
7932 return SWITCH_STATUS_FALSE
;
7935 bytes
= rtp_common_read(rtp_session
, payload_type
, NULL
, flags
, io_flags
);
7936 *data
= RTP_BODY(rtp_session
);
7940 return SWITCH_STATUS_GENERR
;
7942 if (bytes
> rtp_header_len
) {
7943 bytes
-= rtp_header_len
;
7948 return SWITCH_STATUS_SUCCESS
;
7951 static int rtp_write_ready(switch_rtp_t
*rtp_session
, uint32_t bytes
, int line
)
7953 if (!rtp_session
) return 0;
7955 if (rtp_session
->ice
.ice_user
&& !(rtp_session
->ice
.rready
|| rtp_session
->ice
.ready
)) {
7956 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
, "Skip sending %s packet %ld bytes (ice not ready @ line %d!)\n",
7957 rtp_type(rtp_session
), (long)bytes
, line
);
7961 if (rtp_session
->dtls
&& rtp_session
->dtls
->state
!= DS_READY
) {
7962 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG3
, "Skip sending %s packet %ld bytes (dtls not ready @ line %d!)\n",
7963 rtp_type(rtp_session
), (long)bytes
, line
);
7971 static int rtp_common_write(switch_rtp_t
*rtp_session
,
7972 rtp_msg_t
*send_msg
, void *data
, uint32_t datalen
, switch_payload_t payload
, uint32_t timestamp
, switch_frame_flag_t
*flags
)
7974 switch_size_t bytes
;
7976 uint32_t this_ts
= 0;
7981 if (!switch_rtp_ready(rtp_session
)) {
7985 if (!rtp_write_ready(rtp_session
, datalen
, __LINE__
)) {
7989 WRITE_INC(rtp_session
);
7991 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
]) {
7992 //switch_core_timer_sync(&rtp_session->write_timer);
7998 m
= (uint8_t) send_msg
->header
.m
;
7999 rtp_session
->ts
= ntohl(send_msg
->header
.ts
);
8001 if (flags
&& *flags
& SFF_RFC2833
) {
8002 if (rtp_session
->te
== INVALID_PT
) {
8006 send_msg
->header
.pt
= rtp_session
->te
;
8008 data
= send_msg
->body
;
8009 if (datalen
> rtp_header_len
) {
8010 datalen
-= rtp_header_len
;
8013 if (*flags
& SFF_RFC2833
) {
8014 if (rtp_session
->te
== INVALID_PT
) {
8018 payload
= rtp_session
->te
;
8021 send_msg
= &rtp_session
->send_msg
;
8022 send_msg
->header
.pt
= payload
;
8024 m
= get_next_write_ts(rtp_session
, timestamp
);
8026 rtp_session
->send_msg
.header
.ts
= htonl(rtp_session
->ts
);
8028 memcpy(send_msg
->body
, data
, datalen
);
8029 bytes
= datalen
+ rtp_header_len
;
8032 if (!switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_VIDEO
)) {
8034 if ((rtp_session
->rtp_bugs
& RTP_BUG_NEVER_SEND_MARKER
)) {
8037 int delta
= rtp_session
->ts
- rtp_session
->last_write_ts
;
8039 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
] &&
8040 ((!rtp_session
->flags
[SWITCH_RTP_FLAG_RESET
] && (abs(delta
) > rtp_session
->samples_per_interval
* 10))
8041 || rtp_session
->ts
== rtp_session
->samples_per_interval
)) {
8045 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
]) {
8046 //switch_core_timer_sync(&rtp_session->write_timer);
8049 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
] &&
8050 (rtp_session
->write_timer
.samplecount
- rtp_session
->last_write_samplecount
) > rtp_session
->samples_per_interval
* 10) {
8054 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
] &&
8055 ((unsigned) ((switch_micro_time_now() - rtp_session
->last_write_timestamp
))) > (rtp_session
->ms_per_packet
* 10)) {
8059 if (rtp_session
->cn
&& payload
!= rtp_session
->cng_pt
) {
8060 rtp_session
->cn
= 0;
8064 if (rtp_session
->need_mark
&& !rtp_session
->sending_dtmf
) {
8066 rtp_session
->need_mark
= 0;
8071 rtp_session
->flags
[SWITCH_RTP_FLAG_RESET
] = 1;
8072 rtp_session
->ts
= 0;
8075 /* If the marker was set, and the timestamp seems to have started over - set a new SSRC, to indicate this is a new stream */
8076 if (m
&& !switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_SECURE_SEND
) && (rtp_session
->rtp_bugs
& RTP_BUG_CHANGE_SSRC_ON_MARKER
) &&
8077 (rtp_session
->flags
[SWITCH_RTP_FLAG_RESET
] || (rtp_session
->ts
<= rtp_session
->last_write_ts
&& rtp_session
->last_write_ts
> 0))) {
8078 switch_rtp_set_ssrc(rtp_session
, (uint32_t) ((intptr_t) rtp_session
+ (uint32_t) switch_epoch_time_now(NULL
)));
8081 if (!switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_VIDEO
) && !switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_UDPTL
)) {
8082 send_msg
->header
.m
= (m
&& !(rtp_session
->rtp_bugs
& RTP_BUG_NEVER_SEND_MARKER
)) ? 1 : 0;
8086 if (switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_VIDEO
)) {
8087 int external
= (flags
&& *flags
& SFF_EXTERNAL
);
8088 /* Normalize the timestamps to our own base by generating a made up starting point then adding the measured deltas to that base
8089 so if the timestamps and ssrc of the source change, it will not break the other end's jitter bufffer / decoder etc *cough* CHROME *cough*
8092 if (!rtp_session
->ts_norm
.ts
) {
8093 rtp_session
->ts_norm
.ts
= (uint32_t) rand() % 1000000 + 1;
8096 if (!rtp_session
->ts_norm
.last_ssrc
|| send_msg
->header
.ssrc
!= rtp_session
->ts_norm
.last_ssrc
|| rtp_session
->ts_norm
.last_external
!= external
) {
8097 switch_core_session_t
*other_session
;
8099 switch_core_session_request_video_refresh(rtp_session
->session
);
8100 switch_core_media_gen_key_frame(rtp_session
->session
);
8102 if (switch_core_session_get_partner(rtp_session
->session
, &other_session
) == SWITCH_STATUS_SUCCESS
) {
8103 switch_core_session_request_video_refresh(other_session
);
8104 switch_core_media_gen_key_frame(other_session
);
8105 switch_core_session_rwunlock(other_session
);
8108 if (rtp_session
->ts_norm
.last_ssrc
) {
8109 rtp_session
->ts_norm
.delta_ttl
= 0;
8110 rtp_session
->ts_norm
.ts
++;
8113 rtp_session
->ts_norm
.last_ssrc
= send_msg
->header
.ssrc
;
8114 rtp_session
->ts_norm
.last_frame
= ntohl(send_msg
->header
.ts
);
8117 rtp_session
->ts_norm
.last_external
= external
;
8119 if (ntohl(send_msg
->header
.ts
) != rtp_session
->ts_norm
.last_frame
) {
8120 int32_t delta
= ntohl(send_msg
->header
.ts
) - rtp_session
->ts_norm
.last_frame
;
8122 if (delta
< 0 || delta
> 90000) {
8123 switch_core_media_gen_key_frame(rtp_session
->session
);
8124 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG1
,
8125 "Timestamp shift detected last: %d this: %d delta: %d stick with prev delta: %d\n",
8126 rtp_session
->ts_norm
.last_frame
, ntohl(send_msg
->header
.ts
), delta
, rtp_session
->ts_norm
.delta
);
8128 rtp_session
->ts_norm
.delta
= delta
;
8131 rtp_session
->ts_norm
.ts
+= rtp_session
->ts_norm
.delta
;
8135 rtp_session
->ts_norm
.last_frame
= ntohl(send_msg
->header
.ts
);
8136 send_msg
->header
.ts
= htonl(rtp_session
->ts_norm
.ts
);
8137 this_ts
= rtp_session
->ts_norm
.ts
;
8140 send_msg
->header
.ssrc
= htonl(rtp_session
->ssrc
);
8142 if (rtp_session
->flags
[SWITCH_RTP_FLAG_GOOGLEHACK
] && rtp_session
->send_msg
.header
.pt
== 97) {
8143 rtp_session
->last_rtp_hdr
.pt
= 102;
8146 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VAD
] &&
8147 rtp_session
->last_rtp_hdr
.pt
== rtp_session
->vad_data
.read_codec
->implementation
->ianacode
) {
8149 int16_t decoded
[SWITCH_RECOMMENDED_BUFFER_SIZE
/ sizeof(int16_t)] = { 0 };
8151 uint32_t codec_flags
= 0;
8152 uint32_t len
= sizeof(decoded
);
8153 time_t now
= switch_epoch_time_now(NULL
);
8156 if (rtp_session
->vad_data
.scan_freq
&& rtp_session
->vad_data
.next_scan
<= now
) {
8157 rtp_session
->vad_data
.bg_count
= rtp_session
->vad_data
.bg_level
= 0;
8158 rtp_session
->vad_data
.next_scan
= now
+ rtp_session
->vad_data
.scan_freq
;
8161 if (switch_core_codec_decode(&rtp_session
->vad_data
.vad_codec
,
8162 rtp_session
->vad_data
.read_codec
,
8165 rtp_session
->vad_data
.read_codec
->implementation
->actual_samples_per_second
,
8166 decoded
, &len
, &rate
, &codec_flags
) == SWITCH_STATUS_SUCCESS
) {
8168 uint32_t energy
= 0;
8169 uint32_t x
, y
= 0, z
= len
/ sizeof(int16_t);
8174 if (!(divisor
= rtp_session
->vad_data
.read_codec
->implementation
->actual_samples_per_second
/ 8000)) {
8178 for (x
= 0; x
< z
; x
++) {
8179 energy
+= abs(decoded
[y
]);
8180 y
+= rtp_session
->vad_data
.read_codec
->implementation
->number_of_channels
;
8183 if (++rtp_session
->vad_data
.start_count
< rtp_session
->vad_data
.start
) {
8186 score
= (energy
/ (z
/ divisor
));
8187 if (score
&& (rtp_session
->vad_data
.bg_count
< rtp_session
->vad_data
.bg_len
)) {
8188 rtp_session
->vad_data
.bg_level
+= score
;
8189 if (++rtp_session
->vad_data
.bg_count
== rtp_session
->vad_data
.bg_len
) {
8190 rtp_session
->vad_data
.bg_level
/= rtp_session
->vad_data
.bg_len
;
8194 if (score
> rtp_session
->vad_data
.bg_level
&& !switch_test_flag(&rtp_session
->vad_data
, SWITCH_VAD_FLAG_TALKING
)) {
8195 uint32_t diff
= score
- rtp_session
->vad_data
.bg_level
;
8197 if (rtp_session
->vad_data
.hangover_hits
) {
8198 rtp_session
->vad_data
.hangover_hits
--;
8201 if (diff
>= rtp_session
->vad_data
.diff_level
|| ++rtp_session
->vad_data
.hangunder_hits
>= rtp_session
->vad_data
.hangunder
) {
8203 switch_set_flag(&rtp_session
->vad_data
, SWITCH_VAD_FLAG_TALKING
);
8205 rtp_session
->vad_data
.start_talking
= switch_micro_time_now();
8207 if (!(rtp_session
->rtp_bugs
& RTP_BUG_NEVER_SEND_MARKER
)) {
8208 send_msg
->header
.m
= 1;
8210 rtp_session
->vad_data
.hangover_hits
= rtp_session
->vad_data
.hangunder_hits
= rtp_session
->vad_data
.cng_count
= 0;
8211 if (switch_test_flag(&rtp_session
->vad_data
, SWITCH_VAD_FLAG_EVENTS_TALK
)) {
8213 if ((rtp_session
->vad_data
.fire_events
& VAD_FIRE_TALK
)) {
8214 switch_event_t
*event
;
8215 if (switch_event_create(&event
, SWITCH_EVENT_TALK
) == SWITCH_STATUS_SUCCESS
) {
8216 switch_channel_event_set_data(switch_core_session_get_channel(rtp_session
->vad_data
.session
), event
);
8217 switch_event_fire(&event
);
8223 if (rtp_session
->vad_data
.hangunder_hits
) {
8224 rtp_session
->vad_data
.hangunder_hits
--;
8226 if (switch_test_flag(&rtp_session
->vad_data
, SWITCH_VAD_FLAG_TALKING
)) {
8227 if (++rtp_session
->vad_data
.hangover_hits
>= rtp_session
->vad_data
.hangover
) {
8228 rtp_session
->vad_data
.stop_talking
= switch_micro_time_now();
8229 rtp_session
->vad_data
.total_talk_time
+= (rtp_session
->vad_data
.stop_talking
- rtp_session
->vad_data
.start_talking
);
8231 switch_clear_flag(&rtp_session
->vad_data
, SWITCH_VAD_FLAG_TALKING
);
8233 rtp_session
->vad_data
.hangover_hits
= rtp_session
->vad_data
.hangunder_hits
= rtp_session
->vad_data
.cng_count
= 0;
8234 if (switch_test_flag(&rtp_session
->vad_data
, SWITCH_VAD_FLAG_EVENTS_NOTALK
)) {
8236 if ((rtp_session
->vad_data
.fire_events
& VAD_FIRE_NOT_TALK
)) {
8237 switch_event_t
*event
;
8238 if (switch_event_create(&event
, SWITCH_EVENT_NOTALK
) == SWITCH_STATUS_SUCCESS
) {
8239 switch_channel_event_set_data(switch_core_session_get_channel(rtp_session
->vad_data
.session
), event
);
8240 switch_event_fire(&event
);
8250 if (switch_test_flag(&rtp_session
->vad_data
, SWITCH_VAD_FLAG_TALKING
)) {
8260 if (!switch_rtp_test_flag(rtp_session
, SWITCH_RTP_FLAG_VIDEO
)) {
8263 this_ts
= ntohl(send_msg
->header
.ts
);
8265 ts_delta
= abs((int32_t)(this_ts
- rtp_session
->last_write_ts
));
8267 if (ts_delta
> rtp_session
->samples_per_second
* 2) {
8268 rtp_session
->flags
[SWITCH_RTP_FLAG_RESET
] = 1;
8270 #ifdef DEBUG_TS_ROLLOVER
8271 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "WRITE TS LAST:%u THIS:%u DELTA:%u\n", rtp_session
->last_write_ts
, this_ts
, ts_delta
);
8273 if ((!(flags
&& *flags
& SFF_RFC2833
) && ts_delta
== 0) || !switch_rtp_ready(rtp_session
) || rtp_session
->sending_dtmf
) {
8278 if (rtp_session
->flags
[SWITCH_RTP_FLAG_PAUSE
]) {
8285 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && (*flags
& SFF_EXTERNAL
) &&
8286 rtp_session
->stats
.outbound
.packet_count
&& rtp_session
->flags
[SWITCH_RTP_FLAG_PASSTHRU
]) {
8287 int32_t x
= rtp_session
->last_write_seq
;
8288 int32_t y
= ntohs(send_msg
->header
.seq
);
8290 if (!rtp_session
->video_delta_mode
) {
8291 rtp_session
->video_delta_mode
= 1;
8293 if (x
> UINT16_MAX
/ 2 && y
< UINT16_MAX
/ 2) {
8294 x
-= (int32_t)UINT16_MAX
+1;
8300 rtp_session
->last_write_seq
= y
;
8303 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_PASSTHRU
]) {
8304 rtp_session
->video_delta_mode
= 0;
8307 rtp_session
->seq
+= delta
;
8309 send_msg
->header
.seq
= htons(rtp_session
->seq
);
8311 if (rtp_session
->flags
[SWITCH_RTP_FLAG_BYTESWAP
] && send_msg
->header
.pt
== rtp_session
->payload
) {
8312 switch_swap_linear((int16_t *)send_msg
->body
, (int) datalen
);
8316 switch_mutex_lock(rtp_session
->ice_mutex
);
8317 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND
]) {
8318 int sbytes
= (int) bytes
;
8319 srtp_err_status_t stat
;
8322 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND_RESET
] || !rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
]) {
8324 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_SECURE_SEND_RESET
);
8325 srtp_dealloc(rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
]);
8326 rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
] = NULL
;
8327 if (srtp_create(&rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
],
8328 &rtp_session
->send_policy
[rtp_session
->srtp_idx_rtp
]) || !rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
]) {
8329 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
,
8330 "Error! RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session
));
8331 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND
] = 0;
8333 switch_mutex_unlock(rtp_session
->ice_mutex
);
8336 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_INFO
,
8337 "RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session
));
8341 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND_MKI
]) {
8342 stat
= srtp_protect(rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
], &send_msg
->header
, &sbytes
);
8344 stat
= srtp_protect_mki(rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
], &send_msg
->header
, &sbytes
, 1, SWITCH_CRYPTO_MKI_INDEX
);
8348 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
,
8349 "Error: %s SRTP protection failed with code %d\n", rtp_type(rtp_session
), stat
);
8354 switch_mutex_unlock(rtp_session
->ice_mutex
);
8357 now
= switch_micro_time_now();
8358 #ifdef RTP_DEBUG_WRITE_DELTA
8360 int delta
= (int) (now
- rtp_session
->send_time
) / 1000;
8361 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_ERROR
, "WRITE %d delta %d\n", (int) bytes
, delta
);
8364 rtp_session
->send_time
= now
;
8366 if (rtp_session
->flags
[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE
]) {
8367 const char *tx_host
;
8368 const char *old_host
;
8369 const char *my_host
;
8371 char bufa
[50], bufb
[50], bufc
[50];
8374 tx_host
= switch_get_addr(bufa
, sizeof(bufa
), rtp_session
->rtp_from_addr
);
8375 old_host
= switch_get_addr(bufb
, sizeof(bufb
), rtp_session
->remote_addr
);
8376 my_host
= switch_get_addr(bufc
, sizeof(bufc
), rtp_session
->local_addr
);
8378 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session
->session
), SWITCH_LOG_CONSOLE
,
8379 "W %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
8380 rtp_session
->session
? switch_channel_get_name(switch_core_session_get_channel(rtp_session
->session
)) : "NoName",
8382 my_host
, switch_sockaddr_get_port(rtp_session
->local_addr
),
8383 old_host
, rtp_session
->remote_port
,
8384 tx_host
, switch_sockaddr_get_port(rtp_session
->rtp_from_addr
),
8385 send_msg
->header
.pt
, ntohl(send_msg
->header
.ts
), ntohs(send_msg
->header
.seq
), send_msg
->header
.m
);
8389 if (rtp_session
->flags
[SWITCH_RTP_FLAG_NACK
]) {
8390 switch_channel_t
*channel
= switch_core_session_get_channel(rtp_session
->session
);
8392 if (!rtp_session
->vbw
) {
8393 int nack_size
= 100;
8396 if ((var
= switch_channel_get_variable(channel
, "rtp_nack_buffer_size"))) {
8397 int tmp
= atoi(var
);
8399 if (tmp
> 0 && tmp
< 500) {
8404 switch_jb_create(&rtp_session
->vbw
, SJB_VIDEO
, nack_size
, nack_size
, rtp_session
->pool
);
8406 if (rtp_session
->vbw
) {
8407 switch_jb_set_flag(rtp_session
->vbw
, SJB_QUEUE_ONLY
);
8408 //switch_jb_debug_level(rtp_session->vbw, 10);
8411 switch_jb_put_packet(rtp_session
->vbw
, (switch_rtp_packet_t
*)send_msg
, bytes
);
8414 #ifdef RTP_WRITE_PLOSS
8416 int r
= (rand() % 10000) + 1;
8419 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ALERT
,
8420 "Simulate dropping packet ......... ts: %u seq: %u\n", ntohl(send_msg
->header
.ts
), ntohs(send_msg
->header
.seq
));
8422 if (switch_socket_sendto(rtp_session
->sock_output
, rtp_session
->remote_addr
, 0, (void *) send_msg
, &bytes
) != SWITCH_STATUS_SUCCESS
) {
8430 //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
8432 // rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]++;
8434 // //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "SEND %u\n", ntohs(send_msg->header.seq));
8436 if (switch_socket_sendto(rtp_session
->sock_output
, rtp_session
->remote_addr
, 0, (void *) send_msg
, &bytes
) != SWITCH_STATUS_SUCCESS
) {
8437 rtp_session
->seq
-= delta
;
8443 rtp_session
->last_write_ts
= this_ts
;
8444 rtp_session
->flags
[SWITCH_RTP_FLAG_RESET
] = 0;
8446 if (rtp_session
->queue_delay
) {
8447 rtp_session
->delay_samples
= rtp_session
->queue_delay
;
8448 rtp_session
->queue_delay
= 0;
8451 rtp_session
->stats
.outbound
.raw_bytes
+= bytes
;
8452 rtp_session
->stats
.outbound
.packet_count
++;
8454 if (rtp_session
->flags
[SWITCH_RTP_FLAG_ENABLE_RTCP
]) {
8455 rtp_session
->stats
.rtcp
.sent_pkt_count
++;
8458 if (send_msg
->header
.pt
== rtp_session
->cng_pt
) {
8459 rtp_session
->stats
.outbound
.cng_packet_count
++;
8461 rtp_session
->stats
.outbound
.media_packet_count
++;
8462 rtp_session
->stats
.outbound
.media_bytes
+= bytes
;
8465 if (rtp_session
->flags
[SWITCH_RTP_FLAG_USE_TIMER
]) {
8466 //switch_core_timer_sync(&rtp_session->write_timer);
8467 rtp_session
->last_write_samplecount
= rtp_session
->write_timer
.samplecount
;
8470 rtp_session
->last_write_timestamp
= switch_micro_time_now();
8477 WRITE_DEC(rtp_session
);
8482 SWITCH_DECLARE(switch_status_t
) switch_rtp_disable_vad(switch_rtp_t
*rtp_session
)
8486 return SWITCH_STATUS_FALSE
;
8489 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_VAD
]) {
8490 return SWITCH_STATUS_GENERR
;
8492 switch_core_codec_destroy(&rtp_session
->vad_data
.vad_codec
);
8493 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_VAD
);
8494 return SWITCH_STATUS_SUCCESS
;
8497 SWITCH_DECLARE(switch_status_t
) switch_rtp_enable_vad(switch_rtp_t
*rtp_session
, switch_core_session_t
*session
, switch_codec_t
*codec
,
8498 switch_vad_flag_t flags
)
8500 if (!switch_rtp_ready(rtp_session
)) {
8501 return SWITCH_STATUS_FALSE
;
8504 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VAD
]) {
8505 return SWITCH_STATUS_GENERR
;
8508 memset(&rtp_session
->vad_data
, 0, sizeof(rtp_session
->vad_data
));
8510 if (switch_true(switch_channel_get_variable(switch_core_session_get_channel(rtp_session
->session
), "fire_talk_events"))) {
8511 rtp_session
->vad_data
.fire_events
|= VAD_FIRE_TALK
;
8514 if (switch_true(switch_channel_get_variable(switch_core_session_get_channel(rtp_session
->session
), "fire_not_talk_events"))) {
8515 rtp_session
->vad_data
.fire_events
|= VAD_FIRE_NOT_TALK
;
8519 if (switch_core_codec_init(&rtp_session
->vad_data
.vad_codec
,
8520 codec
->implementation
->iananame
,
8521 codec
->implementation
->modname
,
8523 codec
->implementation
->samples_per_second
,
8524 codec
->implementation
->microseconds_per_packet
/ 1000,
8525 codec
->implementation
->number_of_channels
,
8526 SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE
, NULL
, rtp_session
->pool
) != SWITCH_STATUS_SUCCESS
) {
8527 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Can't load codec?\n");
8528 return SWITCH_STATUS_FALSE
;
8530 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_DEBUG
, "Activate VAD codec %s %dms\n", codec
->implementation
->iananame
,
8531 codec
->implementation
->microseconds_per_packet
/ 1000);
8532 rtp_session
->vad_data
.diff_level
= 400;
8533 rtp_session
->vad_data
.hangunder
= 15;
8534 rtp_session
->vad_data
.hangover
= 40;
8535 rtp_session
->vad_data
.bg_len
= 5;
8536 rtp_session
->vad_data
.bg_count
= 5;
8537 rtp_session
->vad_data
.bg_level
= 300;
8538 rtp_session
->vad_data
.read_codec
= codec
;
8539 rtp_session
->vad_data
.session
= session
;
8540 rtp_session
->vad_data
.flags
= flags
;
8541 rtp_session
->vad_data
.cng_freq
= 50;
8542 rtp_session
->vad_data
.ts
= 1;
8543 rtp_session
->vad_data
.start
= 0;
8544 rtp_session
->vad_data
.next_scan
= switch_epoch_time_now(NULL
);
8545 rtp_session
->vad_data
.scan_freq
= 0;
8546 if (switch_test_flag(&rtp_session
->vad_data
, SWITCH_VAD_FLAG_TALKING
)) {
8547 rtp_session
->vad_data
.start_talking
= switch_micro_time_now();
8549 switch_rtp_set_flag(rtp_session
, SWITCH_RTP_FLAG_VAD
);
8550 switch_set_flag(&rtp_session
->vad_data
, SWITCH_VAD_FLAG_CNG
);
8551 return SWITCH_STATUS_SUCCESS
;
8554 SWITCH_DECLARE(int) switch_rtp_write_frame(switch_rtp_t
*rtp_session
, switch_frame_t
*frame
)
8558 uint32_t len
, ts
= 0;
8559 switch_payload_t payload
= 0;
8560 rtp_msg_t
*send_msg
= NULL
;
8561 srtp_hdr_t local_header
;
8563 switch_status_t status
;
8565 if (!switch_rtp_ready(rtp_session
) || !rtp_session
->remote_addr
) {
8569 if (!rtp_write_ready(rtp_session
, frame
->datalen
, __LINE__
)) {
8573 //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
8574 // rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]++;
8575 // rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]++;
8579 if (switch_test_flag(frame
, SFF_PROXY_PACKET
) || switch_test_flag(frame
, SFF_UDPTL_PACKET
) ||
8580 rtp_session
->flags
[SWITCH_RTP_FLAG_PROXY_MEDIA
] || rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
]) {
8582 //if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
8583 switch_size_t bytes
;
8587 if (!switch_test_flag(frame
, SFF_PROXY_PACKET
) && !switch_test_flag(frame
, SFF_UDPTL_PACKET
)) {
8590 bytes
= frame
->packetlen
;
8591 //tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->remote_addr);
8593 send_msg
= frame
->packet
;
8595 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_UDPTL
] && !switch_test_flag(frame
, SFF_UDPTL_PACKET
)) {
8597 if (rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] && rtp_session
->payload
> 0) {
8598 send_msg
->header
.pt
= rtp_session
->payload
;
8601 send_msg
->header
.ssrc
= htonl(rtp_session
->ssrc
);
8602 send_msg
->header
.seq
= htons(++rtp_session
->seq
);
8605 if (rtp_session
->flags
[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE
]) {
8606 const char *tx_host
;
8607 const char *old_host
;
8608 const char *my_host
;
8610 char bufa
[50], bufb
[50], bufc
[50];
8613 tx_host
= switch_get_addr(bufa
, sizeof(bufa
), rtp_session
->rtp_from_addr
);
8614 old_host
= switch_get_addr(bufb
, sizeof(bufb
), rtp_session
->remote_addr
);
8615 my_host
= switch_get_addr(bufc
, sizeof(bufc
), rtp_session
->local_addr
);
8617 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session
->session
), SWITCH_LOG_CONSOLE
,
8618 "W %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
8619 rtp_session
->session
? switch_channel_get_name(switch_core_session_get_channel(rtp_session
->session
)) : "NoName",
8621 my_host
, switch_sockaddr_get_port(rtp_session
->local_addr
),
8622 old_host
, rtp_session
->remote_port
,
8623 tx_host
, switch_sockaddr_get_port(rtp_session
->rtp_from_addr
),
8624 send_msg
->header
.pt
, ntohl(send_msg
->header
.ts
), ntohs(send_msg
->header
.seq
), send_msg
->header
.m
);
8628 if ((status
= switch_socket_sendto(rtp_session
->sock_output
, rtp_session
->remote_addr
, 0, frame
->packet
, &bytes
)) != SWITCH_STATUS_SUCCESS
) {
8629 if (rtp_session
->flags
[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE
]) {
8630 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG_CLEAN(rtp_session
->session
), SWITCH_LOG_ERROR
, "bytes: %" SWITCH_SIZE_T_FMT
", status: %d", bytes
, status
);
8637 rtp_session
->stats
.outbound
.raw_bytes
+= bytes
;
8638 rtp_session
->stats
.outbound
.media_bytes
+= bytes
;
8639 rtp_session
->stats
.outbound
.media_packet_count
++;
8640 rtp_session
->stats
.outbound
.packet_count
++;
8644 fwd
= (rtp_session
->flags
[SWITCH_RTP_FLAG_RAW_WRITE
] &&
8645 (switch_test_flag(frame
, SFF_RAW_RTP
) || switch_test_flag(frame
, SFF_RAW_RTP_PARSE_FRAME
))) ? 1 : 0;
8647 if (!fwd
&& !rtp_session
->sending_dtmf
&& !rtp_session
->queue_delay
&& !rtp_session
->flags
[SWITCH_RTP_FLAG_VIDEO
] &&
8648 rtp_session
->flags
[SWITCH_RTP_FLAG_RAW_WRITE
] && (rtp_session
->rtp_bugs
& RTP_BUG_GEN_ONE_GEN_ALL
)) {
8650 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_WARNING
, "Generating RTP locally but timestamp passthru is configured, disabling....\n");
8651 rtp_session
->flags
[SWITCH_RTP_FLAG_RAW_WRITE
] = 0;
8652 rtp_session
->flags
[SWITCH_RTP_FLAG_RESET
] = 1;
8655 switch_assert(frame
!= NULL
);
8657 if (switch_test_flag(frame
, SFF_CNG
)) {
8658 if (rtp_session
->cng_pt
!= INVALID_PT
) {
8659 payload
= rtp_session
->cng_pt
;
8661 return (int) frame
->packetlen
;
8664 payload
= rtp_session
->payload
;
8666 if (rtp_session
->pmaps
&& *rtp_session
->pmaps
) {
8667 payload_map_t
*pmap
;
8668 for (pmap
= *rtp_session
->pmaps
; pmap
; pmap
= pmap
->next
) {
8669 if (pmap
->current
) {
8678 if (switch_test_flag(frame
, SFF_RTP_HEADER
) || rtp_session
->flags
[SWITCH_RTP_FLAG_TEXT
]) {
8679 switch_size_t wrote
;
8681 wrote
= switch_rtp_write_manual(rtp_session
, frame
->data
, frame
->datalen
,
8682 frame
->m
, frame
->payload
, (uint32_t) (frame
->timestamp
), &frame
->flags
);
8684 rtp_session
->stats
.outbound
.raw_bytes
+= wrote
;
8685 rtp_session
->stats
.outbound
.media_bytes
+= wrote
;
8686 rtp_session
->stats
.outbound
.media_packet_count
++;
8687 rtp_session
->stats
.outbound
.packet_count
++;
8692 if (frame
->pmap
&& rtp_session
->pmaps
&& *rtp_session
->pmaps
) {
8693 payload_map_t
*pmap
;
8695 switch_mutex_lock(rtp_session
->flag_mutex
);
8696 for (pmap
= *rtp_session
->pmaps
; pmap
; pmap
= pmap
->next
) {
8697 if (pmap
->negotiated
&& pmap
->hash
== frame
->pmap
->hash
) {
8698 payload
= pmap
->recv_pt
;
8702 switch_mutex_unlock(rtp_session
->flag_mutex
);
8706 send_msg
= frame
->packet
;
8707 local_header
= send_msg
->header
;
8708 len
= frame
->packetlen
;
8711 send_msg
->header
.pt
= payload
;
8713 if (switch_test_flag(frame
, SFF_RAW_RTP_PARSE_FRAME
)) {
8714 send_msg
->header
.version
= 2;
8715 send_msg
->header
.m
= frame
->m
;
8717 send_msg
->header
.ts
= htonl(frame
->timestamp
);
8719 send_msg
->header
.ssrc
= htonl(frame
->ssrc
);
8721 send_msg
->header
.ssrc
= htonl(rtp_session
->ssrc
);
8727 len
= frame
->datalen
;
8728 ts
= rtp_session
->flags
[SWITCH_RTP_FLAG_RAW_WRITE
] ? (uint32_t) frame
->timestamp
: 0;
8732 if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
8733 send_msg->header.pt = rtp_session->payload;
8737 r
= rtp_common_write(rtp_session
, send_msg
, data
, len
, payload
, ts
, &frame
->flags
);
8740 send_msg
->header
= local_header
;
8747 SWITCH_DECLARE(switch_rtp_stats_t
*) switch_rtp_get_stats(switch_rtp_t
*rtp_session
, switch_memory_pool_t
*pool
)
8749 switch_rtp_stats_t
*s
;
8755 switch_mutex_lock(rtp_session
->flag_mutex
);
8757 s
= switch_core_alloc(pool
, sizeof(*s
));
8758 *s
= rtp_session
->stats
;
8760 s
= &rtp_session
->stats
;
8763 if (rtp_session
->jb
) {
8764 switch_jb_get_frames(rtp_session
->jb
, NULL
, NULL
, NULL
, (uint32_t *)&s
->inbound
.largest_jb_size
);
8767 do_mos(rtp_session
);
8769 switch_mutex_unlock(rtp_session
->flag_mutex
);
8774 SWITCH_DECLARE(int) switch_rtp_write_manual(switch_rtp_t
*rtp_session
,
8775 void *data
, uint32_t datalen
, uint8_t m
, switch_payload_t payload
, uint32_t ts
, switch_frame_flag_t
*flags
)
8777 switch_size_t bytes
;
8780 if (!switch_rtp_ready(rtp_session
) || !rtp_session
->remote_addr
|| datalen
> SWITCH_RTP_MAX_BUF_LEN
) {
8784 if (!rtp_write_ready(rtp_session
, datalen
, __LINE__
)) {
8788 if (payload
== INVALID_PT
) {
8792 WRITE_INC(rtp_session
);
8794 rtp_session
->write_msg
= rtp_session
->send_msg
;
8795 rtp_session
->write_msg
.header
.seq
= htons(++rtp_session
->seq
);
8796 rtp_session
->write_msg
.header
.ts
= htonl(ts
);
8797 rtp_session
->write_msg
.header
.pt
= payload
;
8798 rtp_session
->write_msg
.header
.m
= m
;
8799 memcpy(rtp_session
->write_msg
.body
, data
, datalen
);
8801 bytes
= rtp_header_len
+ datalen
;
8803 if (switch_rtp_write_raw(rtp_session
, (void *) &rtp_session
->write_msg
, &bytes
, SWITCH_TRUE
) != SWITCH_STATUS_SUCCESS
) {
8809 if (((*flags
) & SFF_RTP_HEADER
)) {
8810 rtp_session
->last_write_ts
= ts
;
8811 rtp_session
->flags
[SWITCH_RTP_FLAG_RESET
] = 0;
8818 WRITE_DEC(rtp_session
);
8825 SWITCH_DECLARE(switch_status_t
) switch_rtp_write_raw(switch_rtp_t
*rtp_session
, void *data
, switch_size_t
*bytes
, switch_bool_t process_encryption
)
8827 switch_status_t status
= SWITCH_STATUS_FALSE
;
8829 switch_assert(bytes
);
8831 if (!switch_rtp_ready(rtp_session
) || !rtp_session
->remote_addr
|| *bytes
> SWITCH_RTP_MAX_BUF_LEN
) {
8835 if (!rtp_write_ready(rtp_session
, *bytes
, __LINE__
)) {
8836 return SWITCH_STATUS_NOT_INITALIZED
;
8839 WRITE_INC(rtp_session
);
8841 if (process_encryption
) {
8843 switch_mutex_lock(rtp_session
->ice_mutex
);
8844 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND
]) {
8846 int sbytes
= (int) *bytes
;
8847 srtp_err_status_t stat
;
8849 if (rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND_RESET
]) {
8850 switch_rtp_clear_flag(rtp_session
, SWITCH_RTP_FLAG_SECURE_SEND_RESET
);
8851 srtp_dealloc(rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
]);
8852 rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
] = NULL
;
8853 if (srtp_create(&rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
],
8854 &rtp_session
->send_policy
[rtp_session
->srtp_idx_rtp
]) || !rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
]) {
8855 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Error! RE-Activating Secure RTP SEND\n");
8856 rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND
] = 0;
8857 status
= SWITCH_STATUS_FALSE
;
8858 switch_mutex_unlock(rtp_session
->ice_mutex
);
8861 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_INFO
, "RE-Activating Secure RTP SEND\n");
8865 if (!rtp_session
->flags
[SWITCH_RTP_FLAG_SECURE_SEND_MKI
]) {
8866 stat
= srtp_protect(rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
], &rtp_session
->write_msg
.header
, &sbytes
);
8868 stat
= srtp_protect_mki(rtp_session
->send_ctx
[rtp_session
->srtp_idx_rtp
], &rtp_session
->write_msg
.header
, &sbytes
, 1, SWITCH_CRYPTO_MKI_INDEX
);
8872 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session
->session
), SWITCH_LOG_ERROR
, "Error: SRTP protection failed with code %d\n", stat
);
8876 switch_mutex_unlock(rtp_session
->ice_mutex
);
8880 status
= switch_socket_sendto(rtp_session
->sock_output
, rtp_session
->remote_addr
, 0, data
, bytes
);
8881 #if defined(ENABLE_SRTP)
8885 WRITE_DEC(rtp_session
);
8890 SWITCH_DECLARE(uint32_t) switch_rtp_get_ssrc(switch_rtp_t
*rtp_session
)
8892 return rtp_session
->ssrc
;
8895 SWITCH_DECLARE(void) switch_rtp_set_private(switch_rtp_t
*rtp_session
, void *private_data
)
8897 rtp_session
->private_data
= private_data
;
8900 SWITCH_DECLARE(void *) switch_rtp_get_private(switch_rtp_t
*rtp_session
)
8902 return rtp_session
->private_data
;
8905 SWITCH_DECLARE(switch_core_session_t
*) switch_rtp_get_core_session(switch_rtp_t
*rtp_session
)
8907 return rtp_session
->session
;
8913 * indent-tabs-mode:t
8918 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: