2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
22 * \brief Supports RTP and RTCP with Symmetric RTP support for NAT traversal.
24 * \author Mark Spencer <markster@digium.com>
26 * \note RTP is defined in RFC 3550.
28 * \ingroup rtp_engines
32 <support_level>core</support_level>
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43 #ifdef HAVE_OPENSSL_SRTP
44 #include <openssl/ssl.h>
45 #include <openssl/err.h>
46 #include <openssl/bio.h>
49 /* Asterisk discourages the use of bzero in favor of memset, in fact if you try to use bzero it will tell you to use memset. As a result bzero has to be undefined
50 * here since it is used internally by pjlib. The only other option would be to modify pjlib... which won't happen. */
54 #include "pjlib-util.h"
57 #include "asterisk/stun.h"
58 #include "asterisk/pbx.h"
59 #include "asterisk/frame.h"
60 #include "asterisk/channel.h"
61 #include "asterisk/acl.h"
62 #include "asterisk/config.h"
63 #include "asterisk/lock.h"
64 #include "asterisk/utils.h"
65 #include "asterisk/cli.h"
66 #include "asterisk/manager.h"
67 #include "asterisk/unaligned.h"
68 #include "asterisk/module.h"
69 #include "asterisk/rtp_engine.h"
71 #define MAX_TIMESTAMP_SKEW 640
73 #define RTP_SEQ_MOD (1<<16) /*!< A sequence number can't be more than 16 bits */
74 #define RTCP_DEFAULT_INTERVALMS 5000 /*!< Default milli-seconds between RTCP reports we send */
75 #define RTCP_MIN_INTERVALMS 500 /*!< Min milli-seconds between RTCP reports we send */
76 #define RTCP_MAX_INTERVALMS 60000 /*!< Max milli-seconds between RTCP reports we send */
78 #define DEFAULT_RTP_START 5000 /*!< Default port number to start allocating RTP ports from */
79 #define DEFAULT_RTP_END 31000 /*!< Default maximum port number to end allocating RTP ports at */
81 #define MINIMUM_RTP_PORT 1024 /*!< Minimum port number to accept */
82 #define MAXIMUM_RTP_PORT 65535 /*!< Maximum port number to accept */
84 #define DEFAULT_TURN_PORT 34780
86 #define TURN_ALLOCATION_WAIT_TIME 2000
88 #define RTCP_PT_FUR 192
89 #define RTCP_PT_SR 200
90 #define RTCP_PT_RR 201
91 #define RTCP_PT_SDES 202
92 #define RTCP_PT_BYE 203
93 #define RTCP_PT_APP 204
97 #define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000)) /*!< samples */
99 #define ZFONE_PROFILE_ID 0x505a
101 #define DEFAULT_LEARNING_MIN_SEQUENTIAL 4
103 #define SRTP_MASTER_KEY_LEN 16
104 #define SRTP_MASTER_SALT_LEN 14
105 #define SRTP_MASTER_LEN (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN)
107 enum strict_rtp_state {
108 STRICT_RTP_OPEN = 0, /*! No RTP packets should be dropped, all sources accepted */
109 STRICT_RTP_LEARN, /*! Accept next packet as source */
110 STRICT_RTP_CLOSED, /*! Drop all RTP packets not coming from source that was learned */
113 #define DEFAULT_STRICT_RTP STRICT_RTP_CLOSED
114 #define DEFAULT_ICESUPPORT 1
116 extern struct ast_srtp_res *res_srtp;
117 extern struct ast_srtp_policy_res *res_srtp_policy;
119 static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
121 static int rtpstart = DEFAULT_RTP_START; /*!< First port for RTP sessions (set in rtp.conf) */
122 static int rtpend = DEFAULT_RTP_END; /*!< Last port for RTP sessions (set in rtp.conf) */
123 static int rtpdebug; /*!< Are we debugging? */
124 static int rtcpdebug; /*!< Are we debugging RTCP? */
125 static int rtcpstats; /*!< Are we debugging RTCP? */
126 static int rtcpinterval = RTCP_DEFAULT_INTERVALMS; /*!< Time between rtcp reports in millisecs */
127 static struct ast_sockaddr rtpdebugaddr; /*!< Debug packets to/from this host */
128 static struct ast_sockaddr rtcpdebugaddr; /*!< Debug RTCP packets to/from this host */
129 static int rtpdebugport; /*< Debug only RTP packets from IP or IP+Port if port is > 0 */
130 static int rtcpdebugport; /*< Debug only RTCP packets from IP or IP+Port if port is > 0 */
132 static int nochecksums;
134 static int strictrtp = DEFAULT_STRICT_RTP; /*< Only accept RTP frames from a defined source. If we receive an indication of a changing source, enter learning mode. */
135 static int learning_min_sequential = DEFAULT_LEARNING_MIN_SEQUENTIAL; /*< Number of sequential RTP frames needed from a single source during learning mode to accept new source. */
136 static int icesupport = DEFAULT_ICESUPPORT;
137 static struct sockaddr_in stunaddr;
138 static pj_str_t turnaddr;
139 static int turnport = DEFAULT_TURN_PORT;
140 static pj_str_t turnusername;
141 static pj_str_t turnpassword;
143 /*! \brief Pool factory used by pjlib to allocate memory. */
144 static pj_caching_pool cachingpool;
146 /*! \brief Pool used by pjlib functions which require memory allocation. */
147 static pj_pool_t *pool;
149 /*! \brief I/O queue for TURN relay traffic */
150 static pj_ioqueue_t *ioqueue;
152 /*! \brief Timer heap for ICE and TURN stuff */
153 static pj_timer_heap_t *timerheap;
155 /*! \brief Worker thread for ICE/TURN */
156 static pj_thread_t *thread;
158 /*! \brief Notification that the ICE/TURN worker thread should stop */
159 static int worker_terminate;
161 #define FLAG_3389_WARNING (1 << 0)
162 #define FLAG_NAT_ACTIVE (3 << 1)
163 #define FLAG_NAT_INACTIVE (0 << 1)
164 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
165 #define FLAG_NEED_MARKER_BIT (1 << 3)
166 #define FLAG_DTMF_COMPENSATE (1 << 4)
168 #define TRANSPORT_SOCKET_RTP 1
169 #define TRANSPORT_SOCKET_RTCP 2
170 #define TRANSPORT_TURN_RTP 3
171 #define TRANSPORT_TURN_RTCP 4
173 #define COMPONENT_RTP 1
174 #define COMPONENT_RTCP 2
176 /*! \brief RTP session description */
180 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
181 unsigned int ssrc; /*!< Synchronization source, RFC 3550, page 10. */
182 unsigned int themssrc; /*!< Their SSRC */
185 unsigned int lastrxts;
186 unsigned int lastividtimestamp;
187 unsigned int lastovidtimestamp;
188 unsigned int lastitexttimestamp;
189 unsigned int lastotexttimestamp;
190 unsigned int lasteventseqn;
191 int lastrxseqno; /*!< Last received sequence number */
192 unsigned short seedrxseqno; /*!< What sequence number did they start with?*/
193 unsigned int seedrxts; /*!< What RTP timestamp did they start with? */
194 unsigned int rxcount; /*!< How many packets have we received? */
195 unsigned int rxoctetcount; /*!< How many octets have we received? should be rxcount *160*/
196 unsigned int txcount; /*!< How many packets have we sent? */
197 unsigned int txoctetcount; /*!< How many octets have we sent? (txcount*160)*/
198 unsigned int cycles; /*!< Shifted count of sequence number cycles */
199 double rxjitter; /*!< Interarrival jitter at the moment in seconds */
200 double rxtransit; /*!< Relative transit time for previous packet */
201 struct ast_format lasttxformat;
202 struct ast_format lastrxformat;
204 int rtptimeout; /*!< RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
205 int rtpholdtimeout; /*!< RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
206 int rtpkeepalive; /*!< Send RTP comfort noice packets for keepalive */
208 /* DTMF Reception Variables */
209 char resp; /*!< The current digit being processed */
210 unsigned int last_seqno; /*!< The last known sequence number for any DTMF packet */
211 unsigned int last_end_timestamp; /*!< The last known timestamp received from an END packet */
212 unsigned int dtmf_duration; /*!< Total duration in samples since the digit start event */
213 unsigned int dtmf_timeout; /*!< When this timestamp is reached we consider END frame lost and forcibly abort digit */
214 unsigned int dtmfsamples;
215 enum ast_rtp_dtmf_mode dtmfmode; /*!< The current DTMF mode of the RTP stream */
216 /* DTMF Transmission Variables */
217 unsigned int lastdigitts;
218 char sending_digit; /*!< boolean - are we sending digits */
219 char send_digit; /*!< digit we are sending */
223 struct timeval rxcore;
224 struct timeval txcore;
225 double drxcore; /*!< The double representation of the first received packet */
226 struct timeval lastrx; /*!< timeval when we last received a packet */
227 struct timeval dtmfmute;
228 struct ast_smoother *smoother;
230 unsigned short seqno; /*!< Sequence number, RFC 3550, page 13. */
231 unsigned short rxseqno;
232 struct ast_sched_context *sched;
233 struct io_context *io;
235 struct ast_rtcp *rtcp;
236 struct ast_rtp *bridged; /*!< Who we are Packet bridged to */
238 enum strict_rtp_state strict_rtp_state; /*!< Current state that strict RTP protection is in */
239 struct ast_sockaddr strict_rtp_address; /*!< Remote address information for strict RTP purposes */
240 struct ast_sockaddr alt_rtp_address; /*!<Alternate remote address information */
243 * Learning mode values based on pjmedia's probation mode. Many of these values are redundant to the above,
244 * but these are in place to keep learning mode sequence values sealed from their normal counterparts.
246 uint16_t learning_max_seq; /*!< Highest sequence number heard */
247 int learning_probation; /*!< Sequential packets untill source is valid */
251 pj_ice_sess *ice; /*!< ICE session */
252 pj_turn_sock *turn_rtp; /*!< RTP TURN relay */
253 pj_turn_sock *turn_rtcp; /*!< RTCP TURN relay */
254 ast_mutex_t lock; /*!< Lock for synchronization purposes */
255 pj_turn_state_t turn_state; /*!< Current state of the TURN relay session */
256 ast_cond_t cond; /*!< Condition for signaling */
257 unsigned int passthrough:1; /*!< Bit to indicate that the received packet should be passed through */
258 unsigned int ice_started:1; /*!< Bit to indicate ICE connectivity checks have started */
260 char remote_ufrag[256]; /*!< The remote ICE username */
261 char remote_passwd[256]; /*!< The remote ICE password */
263 char local_ufrag[256]; /*!< The local ICE username */
264 char local_passwd[256]; /*!< The local ICE password */
266 struct ao2_container *local_candidates; /*!< The local ICE candidates */
267 struct ao2_container *remote_candidates; /*!< The remote ICE candidates */
269 #ifdef HAVE_OPENSSL_SRTP
270 SSL_CTX *ssl_ctx; /*!< SSL context */
271 SSL *ssl; /*!< SSL session */
272 BIO *read_bio; /*!< Memory buffer for reading */
273 BIO *write_bio; /*!< Memory buffer for writing */
274 enum ast_rtp_dtls_setup dtls_setup; /*!< Current setup state */
275 enum ast_srtp_suite suite; /*!< SRTP crypto suite */
276 char local_fingerprint[160]; /*!< Fingerprint of our certificate */
277 unsigned char remote_fingerprint[EVP_MAX_MD_SIZE]; /*!< Fingerprint of the peer certificate */
278 enum ast_rtp_dtls_connection connection; /*!< Whether this is a new or existing connection */
279 unsigned int dtls_failure:1; /*!< Failure occurred during DTLS negotiation */
280 unsigned int rekey; /*!< Interval at which to renegotiate and rekey */
281 int rekeyid; /*!< Scheduled item id for rekeying */
286 * \brief Structure defining an RTCP session.
288 * The concept "RTCP session" is not defined in RFC 3550, but since
289 * this structure is analogous to ast_rtp, which tracks a RTP session,
290 * it is logical to think of this as a RTCP session.
292 * RTCP packet is defined on page 9 of RFC 3550.
297 int s; /*!< Socket */
298 struct ast_sockaddr us; /*!< Socket representation of the local endpoint. */
299 struct ast_sockaddr them; /*!< Socket representation of the remote endpoint. */
300 unsigned int soc; /*!< What they told us */
301 unsigned int spc; /*!< What they told us */
302 unsigned int themrxlsr; /*!< The middle 32 bits of the NTP timestamp in the last received SR*/
303 struct timeval rxlsr; /*!< Time when we got their last SR */
304 struct timeval txlsr; /*!< Time when we sent or last SR*/
305 unsigned int expected_prior; /*!< no. packets in previous interval */
306 unsigned int received_prior; /*!< no. packets received in previous interval */
307 int schedid; /*!< Schedid returned from ast_sched_add() to schedule RTCP-transmissions*/
308 unsigned int rr_count; /*!< number of RRs we've sent, not including report blocks in SR's */
309 unsigned int sr_count; /*!< number of SRs we've sent */
310 unsigned int lastsrtxcount; /*!< Transmit packet count when last SR sent */
311 double accumulated_transit; /*!< accumulated a-dlsr-lsr */
312 double rtt; /*!< Last reported rtt */
313 unsigned int reported_jitter; /*!< The contents of their last jitter entry in the RR */
314 unsigned int reported_lost; /*!< Reported lost packets in their RR */
316 double reported_maxjitter;
317 double reported_minjitter;
318 double reported_normdev_jitter;
319 double reported_stdev_jitter;
320 unsigned int reported_jitter_count;
322 double reported_maxlost;
323 double reported_minlost;
324 double reported_normdev_lost;
325 double reported_stdev_lost;
330 double normdev_rxlost;
332 unsigned int rxlost_count;
336 double normdev_rxjitter;
337 double stdev_rxjitter;
338 unsigned int rxjitter_count;
343 unsigned int rtt_count;
347 struct ast_frame t140; /*!< Primary data */
348 struct ast_frame t140red; /*!< Redundant t140*/
349 unsigned char pt[AST_RED_MAX_GENERATION]; /*!< Payload types for redundancy data */
350 unsigned char ts[AST_RED_MAX_GENERATION]; /*!< Time stamps */
351 unsigned char len[AST_RED_MAX_GENERATION]; /*!< length of each generation */
352 int num_gen; /*!< Number of generations */
353 int schedid; /*!< Timer id */
354 int ti; /*!< How long to buffer data before send */
355 unsigned char t140red_data[64000];
356 unsigned char buf_data[64000]; /*!< buffered primary data */
361 AST_LIST_HEAD_NOLOCK(frame_list, ast_frame);
363 /* Forward Declarations */
364 static int ast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data);
365 static int ast_rtp_destroy(struct ast_rtp_instance *instance);
366 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit);
367 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit);
368 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration);
369 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode);
370 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance);
371 static void ast_rtp_update_source(struct ast_rtp_instance *instance);
372 static void ast_rtp_change_source(struct ast_rtp_instance *instance);
373 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame);
374 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp);
375 static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value);
376 static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp);
377 static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
378 static void ast_rtp_alt_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
379 static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations);
380 static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame);
381 static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1);
382 static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat);
383 static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1);
384 static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username);
385 static void ast_rtp_stop(struct ast_rtp_instance *instance);
386 static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char* desc);
387 static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level);
389 #ifdef HAVE_OPENSSL_SRTP
390 static int ast_rtp_activate(struct ast_rtp_instance *instance);
393 static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *ice, int use_srtp);
395 /*! \brief Destructor for locally created ICE candidates */
396 static void ast_rtp_ice_candidate_destroy(void *obj)
398 struct ast_rtp_engine_ice_candidate *candidate = obj;
400 if (candidate->foundation) {
401 ast_free(candidate->foundation);
404 if (candidate->transport) {
405 ast_free(candidate->transport);
409 static void ast_rtp_ice_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
411 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
413 if (!ast_strlen_zero(ufrag)) {
414 ast_copy_string(rtp->remote_ufrag, ufrag, sizeof(rtp->remote_ufrag));
417 if (!ast_strlen_zero(password)) {
418 ast_copy_string(rtp->remote_passwd, password, sizeof(rtp->remote_passwd));
422 static void ast_rtp_ice_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
424 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
425 struct ast_rtp_engine_ice_candidate *remote_candidate;
427 if (!rtp->remote_candidates && !(rtp->remote_candidates = ao2_container_alloc(1, NULL, NULL))) {
431 /* If this is going to exceed the maximum number of ICE candidates don't even add it */
432 if (ao2_container_count(rtp->remote_candidates) == PJ_ICE_MAX_CAND) {
436 if (!(remote_candidate = ao2_alloc(sizeof(*remote_candidate), ast_rtp_ice_candidate_destroy))) {
440 remote_candidate->foundation = ast_strdup(candidate->foundation);
441 remote_candidate->id = candidate->id;
442 remote_candidate->transport = ast_strdup(candidate->transport);
443 remote_candidate->priority = candidate->priority;
444 ast_sockaddr_copy(&remote_candidate->address, &candidate->address);
445 ast_sockaddr_copy(&remote_candidate->relay_address, &candidate->relay_address);
446 remote_candidate->type = candidate->type;
448 ao2_link(rtp->remote_candidates, remote_candidate);
449 ao2_ref(remote_candidate, -1);
452 AST_THREADSTORAGE(pj_thread_storage);
454 /*! \brief Function used to check if the calling thread is registered with pjlib. If it is not it will be registered. */
455 static void pj_thread_register_check(void)
457 pj_thread_desc *desc;
460 if (pj_thread_is_registered() == PJ_TRUE) {
464 desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc));
466 ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage. Expect awful things to occur\n");
469 pj_bzero(*desc, sizeof(*desc));
471 if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
472 ast_log(LOG_ERROR, "Coudln't register thread with PJLIB.\n");
477 /*! \brief Helper function which updates an ast_sockaddr with the candidate used for the component */
478 static void update_address_with_ice_candidate(struct ast_rtp *rtp, int component, struct ast_sockaddr *cand_address)
480 char address[PJ_INET6_ADDRSTRLEN];
482 if (!rtp->ice || (component < 1) || !rtp->ice->comp[component - 1].valid_check) {
486 ast_sockaddr_parse(cand_address, pj_sockaddr_print(&rtp->ice->comp[component - 1].valid_check->rcand->addr, address, sizeof(address), 0), 0);
487 ast_sockaddr_set_port(cand_address, pj_sockaddr_get_port(&rtp->ice->comp[component - 1].valid_check->rcand->addr));
490 static void ast_rtp_ice_start(struct ast_rtp_instance *instance)
492 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
493 pj_str_t ufrag = pj_str(rtp->remote_ufrag), passwd = pj_str(rtp->remote_passwd);
494 pj_ice_sess_cand candidates[PJ_ICE_MAX_CAND];
495 struct ao2_iterator i;
496 struct ast_rtp_engine_ice_candidate *candidate;
499 if (!rtp->ice || !rtp->remote_candidates || rtp->ice_started) {
503 pj_thread_register_check();
505 i = ao2_iterator_init(rtp->remote_candidates, 0);
507 while ((candidate = ao2_iterator_next(&i)) && (cand_cnt < PJ_ICE_MAX_CAND)) {
510 pj_strdup2(rtp->ice->pool, &candidates[cand_cnt].foundation, candidate->foundation);
511 candidates[cand_cnt].comp_id = candidate->id;
512 candidates[cand_cnt].prio = candidate->priority;
514 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->address)), &candidates[cand_cnt].addr);
516 if (!ast_sockaddr_isnull(&candidate->relay_address)) {
517 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->relay_address)), &candidates[cand_cnt].rel_addr);
520 if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
521 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_HOST;
522 } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
523 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_SRFLX;
524 } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_RELAYED) {
525 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_RELAYED;
528 if (candidate->id == COMPONENT_RTP && rtp->turn_rtp) {
529 pj_turn_sock_set_perm(rtp->turn_rtp, 1, &candidates[cand_cnt].addr, 1);
530 } else if (candidate->id == COMPONENT_RTCP && rtp->turn_rtcp) {
531 pj_turn_sock_set_perm(rtp->turn_rtcp, 1, &candidates[cand_cnt].addr, 1);
537 ao2_iterator_destroy(&i);
539 if (pj_ice_sess_create_check_list(rtp->ice, &ufrag, &passwd, ao2_container_count(rtp->remote_candidates), &candidates[0]) == PJ_SUCCESS) {
540 pj_ice_sess_start_check(rtp->ice);
541 pj_timer_heap_poll(timerheap, NULL);
542 rtp->ice_started = 1;
546 static void ast_rtp_ice_stop(struct ast_rtp_instance *instance)
548 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
554 pj_thread_register_check();
556 pj_ice_sess_destroy(rtp->ice);
560 static const char *ast_rtp_ice_get_ufrag(struct ast_rtp_instance *instance)
562 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
564 return rtp->local_ufrag;
567 static const char *ast_rtp_ice_get_password(struct ast_rtp_instance *instance)
569 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
571 return rtp->local_passwd;
574 static struct ao2_container *ast_rtp_ice_get_local_candidates(struct ast_rtp_instance *instance)
576 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
578 if (rtp->local_candidates) {
579 ao2_ref(rtp->local_candidates, +1);
582 return rtp->local_candidates;
585 static void ast_rtp_ice_lite(struct ast_rtp_instance *instance)
587 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
593 pj_thread_register_check();
595 pj_ice_sess_change_role(rtp->ice, PJ_ICE_SESS_ROLE_CONTROLLING);
598 static int ice_candidate_cmp(void *obj, void *arg, int flags)
600 struct ast_rtp_engine_ice_candidate *candidate1 = obj, *candidate2 = arg;
602 if ((strcmp(candidate1->foundation, candidate2->foundation)) ||
603 (candidate1->id != candidate2->id) ||
604 (ast_sockaddr_cmp(&candidate1->address, &candidate2->address)) ||
605 (candidate1->type != candidate1->type)) {
609 return CMP_MATCH | CMP_STOP;
612 static void ast_rtp_ice_add_cand(struct ast_rtp *rtp, unsigned comp_id, unsigned transport_id, pj_ice_cand_type type, pj_uint16_t local_pref,
613 const pj_sockaddr_t *addr, const pj_sockaddr_t *base_addr, const pj_sockaddr_t *rel_addr, int addr_len)
616 struct ast_rtp_engine_ice_candidate *candidate, *existing;
617 char address[PJ_INET6_ADDRSTRLEN];
619 pj_thread_register_check();
621 pj_ice_calc_foundation(rtp->ice->pool, &foundation, type, addr);
623 if (!rtp->local_candidates && !(rtp->local_candidates = ao2_container_alloc(1, NULL, ice_candidate_cmp))) {
627 if (!(candidate = ao2_alloc(sizeof(*candidate), ast_rtp_ice_candidate_destroy))) {
631 candidate->foundation = ast_strndup(pj_strbuf(&foundation), pj_strlen(&foundation));
632 candidate->id = comp_id;
633 candidate->transport = ast_strdup("UDP");
635 ast_sockaddr_parse(&candidate->address, pj_sockaddr_print(addr, address, sizeof(address), 0), 0);
636 ast_sockaddr_set_port(&candidate->address, pj_sockaddr_get_port(addr));
639 ast_sockaddr_parse(&candidate->relay_address, pj_sockaddr_print(rel_addr, address, sizeof(address), 0), 0);
640 ast_sockaddr_set_port(&candidate->relay_address, pj_sockaddr_get_port(rel_addr));
643 if (type == PJ_ICE_CAND_TYPE_HOST) {
644 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
645 } else if (type == PJ_ICE_CAND_TYPE_SRFLX) {
646 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
647 } else if (type == PJ_ICE_CAND_TYPE_RELAYED) {
648 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
651 if ((existing = ao2_find(rtp->local_candidates, candidate, OBJ_POINTER))) {
652 ao2_ref(existing, -1);
653 ao2_ref(candidate, -1);
657 if (pj_ice_sess_add_cand(rtp->ice, comp_id, transport_id, type, local_pref, &foundation, addr, addr, rel_addr, addr_len, NULL) != PJ_SUCCESS) {
658 ao2_ref(candidate, -1);
662 /* By placing the candidate into the ICE session it will have produced the priority, so update the local candidate with it */
663 candidate->priority = rtp->ice->lcand[rtp->ice->lcand_cnt - 1].prio;
665 ao2_link(rtp->local_candidates, candidate);
666 ao2_ref(candidate, -1);
669 static char *generate_random_string(char *buf, size_t size)
675 val[x] = ast_random();
676 snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
681 /* ICE RTP Engine interface declaration */
682 static struct ast_rtp_engine_ice ast_rtp_ice = {
683 .set_authentication = ast_rtp_ice_set_authentication,
684 .add_remote_candidate = ast_rtp_ice_add_remote_candidate,
685 .start = ast_rtp_ice_start,
686 .stop = ast_rtp_ice_stop,
687 .get_ufrag = ast_rtp_ice_get_ufrag,
688 .get_password = ast_rtp_ice_get_password,
689 .get_local_candidates = ast_rtp_ice_get_local_candidates,
690 .ice_lite = ast_rtp_ice_lite,
693 #ifdef HAVE_OPENSSL_SRTP
694 static void dtls_info_callback(const SSL *ssl, int where, int ret)
696 struct ast_rtp *rtp = SSL_get_ex_data(ssl, 0);
698 /* We only care about alerts */
699 if (!(where & SSL_CB_ALERT)) {
703 rtp->dtls_failure = 1;
706 static int ast_rtp_dtls_set_configuration(struct ast_rtp_instance *instance, const struct ast_rtp_dtls_cfg *dtls_cfg)
708 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
710 if (!dtls_cfg->enabled) {
714 if (!ast_rtp_engine_srtp_is_registered()) {
718 if (!(rtp->ssl_ctx = SSL_CTX_new(DTLSv1_method()))) {
722 SSL_CTX_set_verify(rtp->ssl_ctx, dtls_cfg->verify ? SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT : SSL_VERIFY_NONE, NULL);
724 if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_80) {
725 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_80");
726 } else if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_32) {
727 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_32");
729 ast_log(LOG_ERROR, "Unsupported suite specified for DTLS-SRTP on RTP instance '%p'\n", instance);
733 if (!ast_strlen_zero(dtls_cfg->certfile)) {
734 char *private = ast_strlen_zero(dtls_cfg->pvtfile) ? dtls_cfg->certfile : dtls_cfg->pvtfile;
737 unsigned int size, i;
738 unsigned char fingerprint[EVP_MAX_MD_SIZE];
739 char *local_fingerprint = rtp->local_fingerprint;
741 if (!SSL_CTX_use_certificate_file(rtp->ssl_ctx, dtls_cfg->certfile, SSL_FILETYPE_PEM)) {
742 ast_log(LOG_ERROR, "Specified certificate file '%s' for RTP instance '%p' could not be used\n",
743 dtls_cfg->certfile, instance);
747 if (!SSL_CTX_use_PrivateKey_file(rtp->ssl_ctx, private, SSL_FILETYPE_PEM) ||
748 !SSL_CTX_check_private_key(rtp->ssl_ctx)) {
749 ast_log(LOG_ERROR, "Specified private key file '%s' for RTP instance '%p' could not be used\n",
754 if (!(certbio = BIO_new(BIO_s_file()))) {
755 ast_log(LOG_ERROR, "Failed to allocate memory for certificate fingerprinting on RTP instance '%p'\n",
760 if (!BIO_read_filename(certbio, dtls_cfg->certfile) ||
761 !(cert = PEM_read_bio_X509(certbio, NULL, 0, NULL)) ||
762 !X509_digest(cert, EVP_sha1(), fingerprint, &size) ||
764 ast_log(LOG_ERROR, "Could not produce fingerprint from certificate '%s' for RTP instance '%p'\n",
765 dtls_cfg->certfile, instance);
766 BIO_free_all(certbio);
770 for (i = 0; i < size; i++) {
771 sprintf(local_fingerprint, "%.2X:", fingerprint[i]);
772 local_fingerprint += 3;
775 *(local_fingerprint-1) = 0;
777 BIO_free_all(certbio);
780 if (!ast_strlen_zero(dtls_cfg->cipher)) {
781 if (!SSL_CTX_set_cipher_list(rtp->ssl_ctx, dtls_cfg->cipher)) {
782 ast_log(LOG_ERROR, "Invalid cipher specified in cipher list '%s' for RTP instance '%p'\n",
783 dtls_cfg->cipher, instance);
788 if (!ast_strlen_zero(dtls_cfg->cafile) || !ast_strlen_zero(dtls_cfg->capath)) {
789 if (!SSL_CTX_load_verify_locations(rtp->ssl_ctx, S_OR(dtls_cfg->cafile, NULL), S_OR(dtls_cfg->capath, NULL))) {
790 ast_log(LOG_ERROR, "Invalid certificate authority file '%s' or path '%s' specified for RTP instance '%p'\n",
791 S_OR(dtls_cfg->cafile, ""), S_OR(dtls_cfg->capath, ""), instance);
796 rtp->rekey = dtls_cfg->rekey;
797 rtp->dtls_setup = dtls_cfg->default_setup;
798 rtp->suite = dtls_cfg->suite;
800 if (!(rtp->ssl = SSL_new(rtp->ssl_ctx))) {
801 ast_log(LOG_ERROR, "Failed to allocate memory for SSL context on RTP instance '%p'\n",
806 SSL_set_ex_data(rtp->ssl, 0, rtp);
807 SSL_set_info_callback(rtp->ssl, dtls_info_callback);
809 if (!(rtp->read_bio = BIO_new(BIO_s_mem()))) {
810 ast_log(LOG_ERROR, "Failed to allocate memory for inbound SSL traffic on RTP instance '%p'\n",
814 BIO_set_mem_eof_return(rtp->read_bio, -1);
816 if (!(rtp->write_bio = BIO_new(BIO_s_mem()))) {
817 ast_log(LOG_ERROR, "Failed to allocate memory for outbound SSL traffic on RTP instance '%p'\n",
821 BIO_set_mem_eof_return(rtp->write_bio, -1);
823 SSL_set_bio(rtp->ssl, rtp->read_bio, rtp->write_bio);
825 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
826 SSL_set_accept_state(rtp->ssl);
828 SSL_set_connect_state(rtp->ssl);
831 rtp->connection = AST_RTP_DTLS_CONNECTION_NEW;
837 BIO_free(rtp->read_bio);
838 rtp->read_bio = NULL;
841 if (rtp->write_bio) {
842 BIO_free(rtp->write_bio);
843 rtp->write_bio = NULL;
851 SSL_CTX_free(rtp->ssl_ctx);
857 static int ast_rtp_dtls_active(struct ast_rtp_instance *instance)
859 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
861 return !rtp->ssl_ctx ? 0 : 1;
864 static void ast_rtp_dtls_stop(struct ast_rtp_instance *instance)
866 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
869 SSL_CTX_free(rtp->ssl_ctx);
879 static void ast_rtp_dtls_reset(struct ast_rtp_instance *instance)
881 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
883 /* If the SSL session is not yet finalized don't bother resetting */
884 if (!SSL_is_init_finished(rtp->ssl)) {
888 SSL_shutdown(rtp->ssl);
889 rtp->connection = AST_RTP_DTLS_CONNECTION_NEW;
892 static enum ast_rtp_dtls_connection ast_rtp_dtls_get_connection(struct ast_rtp_instance *instance)
894 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
896 return rtp->connection;
899 static enum ast_rtp_dtls_setup ast_rtp_dtls_get_setup(struct ast_rtp_instance *instance)
901 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
903 return rtp->dtls_setup;
906 static void ast_rtp_dtls_set_setup(struct ast_rtp_instance *instance, enum ast_rtp_dtls_setup setup)
908 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
909 enum ast_rtp_dtls_setup old = rtp->dtls_setup;
912 case AST_RTP_DTLS_SETUP_ACTIVE:
913 rtp->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
915 case AST_RTP_DTLS_SETUP_PASSIVE:
916 rtp->dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
918 case AST_RTP_DTLS_SETUP_ACTPASS:
919 /* We can't respond to an actpass setup with actpass ourselves... so respond with active, as we can initiate connections */
920 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
921 rtp->dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
924 case AST_RTP_DTLS_SETUP_HOLDCONN:
925 rtp->dtls_setup = AST_RTP_DTLS_SETUP_HOLDCONN;
928 /* This should never occur... if it does exit early as we don't know what state things are in */
932 /* If the setup state did not change we go on as if nothing happened */
933 if (old == rtp->dtls_setup) {
937 /* If they don't want us to establish a connection wait until later */
938 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_HOLDCONN) {
942 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
943 SSL_set_connect_state(rtp->ssl);
944 } else if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
945 SSL_set_accept_state(rtp->ssl);
951 static void ast_rtp_dtls_set_fingerprint(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash, const char *fingerprint)
953 char *tmp = ast_strdupa(fingerprint), *value;
955 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
957 if (hash != AST_RTP_DTLS_HASH_SHA1) {
961 while ((value = strsep(&tmp, ":")) && (pos != (EVP_MAX_MD_SIZE - 1))) {
962 sscanf(value, "%02x", (unsigned int*)&rtp->remote_fingerprint[pos++]);
966 static const char *ast_rtp_dtls_get_fingerprint(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash)
968 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
970 if (hash != AST_RTP_DTLS_HASH_SHA1) {
974 return rtp->local_fingerprint;
977 /* DTLS RTP Engine interface declaration */
978 static struct ast_rtp_engine_dtls ast_rtp_dtls = {
979 .set_configuration = ast_rtp_dtls_set_configuration,
980 .active = ast_rtp_dtls_active,
981 .stop = ast_rtp_dtls_stop,
982 .reset = ast_rtp_dtls_reset,
983 .get_connection = ast_rtp_dtls_get_connection,
984 .get_setup = ast_rtp_dtls_get_setup,
985 .set_setup = ast_rtp_dtls_set_setup,
986 .set_fingerprint = ast_rtp_dtls_set_fingerprint,
987 .get_fingerprint = ast_rtp_dtls_get_fingerprint,
992 /* RTP Engine Declaration */
993 static struct ast_rtp_engine asterisk_rtp_engine = {
996 .destroy = ast_rtp_destroy,
997 .dtmf_begin = ast_rtp_dtmf_begin,
998 .dtmf_end = ast_rtp_dtmf_end,
999 .dtmf_end_with_duration = ast_rtp_dtmf_end_with_duration,
1000 .dtmf_mode_set = ast_rtp_dtmf_mode_set,
1001 .dtmf_mode_get = ast_rtp_dtmf_mode_get,
1002 .update_source = ast_rtp_update_source,
1003 .change_source = ast_rtp_change_source,
1004 .write = ast_rtp_write,
1005 .read = ast_rtp_read,
1006 .prop_set = ast_rtp_prop_set,
1008 .remote_address_set = ast_rtp_remote_address_set,
1009 .alt_remote_address_set = ast_rtp_alt_remote_address_set,
1010 .red_init = rtp_red_init,
1011 .red_buffer = rtp_red_buffer,
1012 .local_bridge = ast_rtp_local_bridge,
1013 .get_stat = ast_rtp_get_stat,
1014 .dtmf_compatible = ast_rtp_dtmf_compatible,
1015 .stun_request = ast_rtp_stun_request,
1016 .stop = ast_rtp_stop,
1017 .qos = ast_rtp_qos_set,
1018 .sendcng = ast_rtp_sendcng,
1019 .ice = &ast_rtp_ice,
1020 #ifdef HAVE_OPENSSL_SRTP
1021 .dtls = &ast_rtp_dtls,
1022 .activate = ast_rtp_activate,
1026 static void ast_rtp_on_ice_rx_data(pj_ice_sess *ice, unsigned comp_id, unsigned transport_id, void *pkt, pj_size_t size, const pj_sockaddr_t *src_addr, unsigned src_addr_len)
1028 struct ast_rtp *rtp = ice->user_data;
1030 /* Instead of handling the packet here (which really doesn't work with our architecture) we set a bit to indicate that it should be handled after pj_ice_sess_on_rx_pkt
1032 rtp->passthrough = 1;
1035 static pj_status_t ast_rtp_on_ice_tx_pkt(pj_ice_sess *ice, unsigned comp_id, unsigned transport_id, const void *pkt, pj_size_t size, const pj_sockaddr_t *dst_addr, unsigned dst_addr_len)
1037 struct ast_rtp *rtp = ice->user_data;
1038 pj_status_t status = PJ_EINVALIDOP;
1039 pj_ssize_t _size = (pj_ssize_t)size;
1041 if (transport_id == TRANSPORT_SOCKET_RTP) {
1042 /* Traffic is destined to go right out the RTP socket we already have */
1043 status = pj_sock_sendto(rtp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
1044 /* sendto on a connectionless socket should send all the data, or none at all */
1045 ast_assert(_size == size || status != PJ_SUCCESS);
1046 } else if (transport_id == TRANSPORT_SOCKET_RTCP) {
1047 /* Traffic is destined to go right out the RTCP socket we already have */
1049 status = pj_sock_sendto(rtp->rtcp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
1050 /* sendto on a connectionless socket should send all the data, or none at all */
1051 ast_assert(_size == size || status != PJ_SUCCESS);
1053 status = PJ_SUCCESS;
1055 } else if (transport_id == TRANSPORT_TURN_RTP) {
1056 /* Traffic is going through the RTP TURN relay */
1057 if (rtp->turn_rtp) {
1058 status = pj_turn_sock_sendto(rtp->turn_rtp, pkt, size, dst_addr, dst_addr_len);
1060 } else if (transport_id == TRANSPORT_TURN_RTCP) {
1061 /* Traffic is going through the RTCP TURN relay */
1062 if (rtp->turn_rtcp) {
1063 status = pj_turn_sock_sendto(rtp->turn_rtcp, pkt, size, dst_addr, dst_addr_len);
1070 /* ICE Session interface declaration */
1071 static pj_ice_sess_cb ast_rtp_ice_sess_cb = {
1072 .on_rx_data = ast_rtp_on_ice_rx_data,
1073 .on_tx_pkt = ast_rtp_on_ice_tx_pkt,
1076 static void ast_rtp_on_turn_rx_rtp_data(pj_turn_sock *turn_sock, void *pkt, unsigned pkt_len, const pj_sockaddr_t *peer_addr, unsigned addr_len)
1078 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1079 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1080 struct ast_sockaddr dest = { { 0, }, };
1082 ast_rtp_instance_get_local_address(instance, &dest);
1084 ast_sendto(rtp->s, pkt, pkt_len, 0, &dest);
1087 static void ast_rtp_on_turn_rtp_state(pj_turn_sock *turn_sock, pj_turn_state_t old_state, pj_turn_state_t new_state)
1089 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1090 struct ast_rtp *rtp = NULL;
1092 /* If this is a leftover from an already destroyed RTP instance just ignore the state change */
1097 rtp = ast_rtp_instance_get_data(instance);
1099 /* If the TURN session is being destroyed we need to remove it from the RTP instance */
1100 if (new_state == PJ_TURN_STATE_DESTROYING) {
1101 rtp->turn_rtp = NULL;
1105 /* We store the new state so the other thread can actually handle it */
1106 ast_mutex_lock(&rtp->lock);
1107 rtp->turn_state = new_state;
1109 /* If this is a state that the main thread should be notified about do so */
1110 if (new_state == PJ_TURN_STATE_READY || new_state == PJ_TURN_STATE_DEALLOCATING || new_state == PJ_TURN_STATE_DEALLOCATED) {
1111 ast_cond_signal(&rtp->cond);
1114 ast_mutex_unlock(&rtp->lock);
1117 /* RTP TURN Socket interface declaration */
1118 static pj_turn_sock_cb ast_rtp_turn_rtp_sock_cb = {
1119 .on_rx_data = ast_rtp_on_turn_rx_rtp_data,
1120 .on_state = ast_rtp_on_turn_rtp_state,
1123 static void ast_rtp_on_turn_rx_rtcp_data(pj_turn_sock *turn_sock, void *pkt, unsigned pkt_len, const pj_sockaddr_t *peer_addr, unsigned addr_len)
1125 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1126 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1128 ast_sendto(rtp->rtcp->s, pkt, pkt_len, 0, &rtp->rtcp->us);
1131 static void ast_rtp_on_turn_rtcp_state(pj_turn_sock *turn_sock, pj_turn_state_t old_state, pj_turn_state_t new_state)
1133 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1134 struct ast_rtp *rtp = NULL;
1136 /* If this is a leftover from an already destroyed RTP instance just ignore the state change */
1141 rtp = ast_rtp_instance_get_data(instance);
1143 /* If the TURN session is being destroyed we need to remove it from the RTP instance */
1144 if (new_state == PJ_TURN_STATE_DESTROYING) {
1145 rtp->turn_rtcp = NULL;
1149 /* We store the new state so the other thread can actually handle it */
1150 ast_mutex_lock(&rtp->lock);
1151 rtp->turn_state = new_state;
1153 /* If this is a state that the main thread should be notified about do so */
1154 if (new_state == PJ_TURN_STATE_READY || new_state == PJ_TURN_STATE_DEALLOCATING || new_state == PJ_TURN_STATE_DEALLOCATED) {
1155 ast_cond_signal(&rtp->cond);
1158 ast_mutex_unlock(&rtp->lock);
1161 /* RTCP TURN Socket interface declaration */
1162 static pj_turn_sock_cb ast_rtp_turn_rtcp_sock_cb = {
1163 .on_rx_data = ast_rtp_on_turn_rx_rtcp_data,
1164 .on_state = ast_rtp_on_turn_rtcp_state,
1167 /*! \brief Worker thread for I/O queue and timerheap */
1168 static int ice_worker_thread(void *data)
1170 while (!worker_terminate) {
1171 const pj_time_val delay = {0, 10};
1173 pj_ioqueue_poll(ioqueue, &delay);
1175 pj_timer_heap_poll(timerheap, NULL);
1181 static inline int rtp_debug_test_addr(struct ast_sockaddr *addr)
1186 if (!ast_sockaddr_isnull(&rtpdebugaddr)) {
1188 return (ast_sockaddr_cmp(&rtpdebugaddr, addr) == 0); /* look for RTP packets from IP+Port */
1190 return (ast_sockaddr_cmp_addr(&rtpdebugaddr, addr) == 0); /* only look for RTP packets from IP */
1197 static inline int rtcp_debug_test_addr(struct ast_sockaddr *addr)
1202 if (!ast_sockaddr_isnull(&rtcpdebugaddr)) {
1203 if (rtcpdebugport) {
1204 return (ast_sockaddr_cmp(&rtcpdebugaddr, addr) == 0); /* look for RTCP packets from IP+Port */
1206 return (ast_sockaddr_cmp_addr(&rtcpdebugaddr, addr) == 0); /* only look for RTCP packets from IP */
1213 #ifdef HAVE_OPENSSL_SRTP
1214 static void dtls_srtp_check_pending(struct ast_rtp_instance *instance, struct ast_rtp *rtp)
1216 size_t pending = BIO_ctrl_pending(rtp->write_bio);
1219 char outgoing[pending];
1221 struct ast_sockaddr remote_address = { {0, } };
1224 ast_rtp_instance_get_remote_address(instance, &remote_address);
1226 /* If we do not yet know an address to send this to defer it until we do */
1227 if (ast_sockaddr_isnull(&remote_address)) {
1231 out = BIO_read(rtp->write_bio, outgoing, sizeof(outgoing));
1233 __rtp_sendto(instance, outgoing, out, 0, &remote_address, 0, &ice, 0);
1237 static int dtls_srtp_renegotiate(const void *data)
1239 struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
1240 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1242 SSL_renegotiate(rtp->ssl);
1243 SSL_do_handshake(rtp->ssl);
1244 dtls_srtp_check_pending(instance, rtp);
1247 ao2_ref(instance, -1);
1252 static int dtls_srtp_setup(struct ast_rtp *rtp, struct ast_srtp *srtp, struct ast_rtp_instance *instance)
1254 unsigned char material[SRTP_MASTER_LEN * 2];
1255 unsigned char *local_key, *local_salt, *remote_key, *remote_salt;
1256 struct ast_srtp_policy *local_policy, *remote_policy = NULL;
1257 struct ast_rtp_instance_stats stats = { 0, };
1259 /* If a fingerprint is present in the SDP make sure that the peer certificate matches it */
1260 if (SSL_CTX_get_verify_mode(rtp->ssl_ctx) != SSL_VERIFY_NONE) {
1263 if (!(certificate = SSL_get_peer_certificate(rtp->ssl))) {
1264 ast_log(LOG_WARNING, "No certificate was provided by the peer on RTP instance '%p'\n", instance);
1268 /* If a fingerprint is present in the SDP make sure that the peer certificate matches it */
1269 if (rtp->remote_fingerprint[0]) {
1270 unsigned char fingerprint[EVP_MAX_MD_SIZE];
1273 if (!X509_digest(certificate, EVP_sha1(), fingerprint, &size) ||
1275 memcmp(fingerprint, rtp->remote_fingerprint, size)) {
1276 X509_free(certificate);
1277 ast_log(LOG_WARNING, "Fingerprint provided by remote party does not match that of peer certificate on RTP instance '%p'\n",
1283 X509_free(certificate);
1286 /* Ensure that certificate verification was successful */
1287 if (SSL_get_verify_result(rtp->ssl) != X509_V_OK) {
1288 ast_log(LOG_WARNING, "Peer certificate on RTP instance '%p' failed verification test\n",
1293 /* Produce key information and set up SRTP */
1294 if (!SSL_export_keying_material(rtp->ssl, material, SRTP_MASTER_LEN * 2, "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) {
1295 ast_log(LOG_WARNING, "Unable to extract SRTP keying material from DTLS-SRTP negotiation on RTP instance '%p'\n",
1300 /* Whether we are acting as a server or client determines where the keys/salts are */
1301 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
1302 local_key = material;
1303 remote_key = local_key + SRTP_MASTER_KEY_LEN;
1304 local_salt = remote_key + SRTP_MASTER_KEY_LEN;
1305 remote_salt = local_salt + SRTP_MASTER_SALT_LEN;
1307 remote_key = material;
1308 local_key = remote_key + SRTP_MASTER_KEY_LEN;
1309 remote_salt = local_key + SRTP_MASTER_KEY_LEN;
1310 local_salt = remote_salt + SRTP_MASTER_SALT_LEN;
1313 if (!(local_policy = res_srtp_policy->alloc())) {
1317 if (res_srtp_policy->set_master_key(local_policy, local_key, SRTP_MASTER_KEY_LEN, local_salt, SRTP_MASTER_SALT_LEN) < 0) {
1318 ast_log(LOG_WARNING, "Could not set key/salt information on local policy of '%p' when setting up DTLS-SRTP\n", rtp);
1322 if (res_srtp_policy->set_suite(local_policy, rtp->suite)) {
1323 ast_log(LOG_WARNING, "Could not set suite to '%d' on local policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
1327 if (ast_rtp_instance_get_stats(instance, &stats, AST_RTP_INSTANCE_STAT_LOCAL_SSRC)) {
1331 res_srtp_policy->set_ssrc(local_policy, stats.local_ssrc, 0);
1333 if (!(remote_policy = res_srtp_policy->alloc())) {
1337 if (res_srtp_policy->set_master_key(remote_policy, remote_key, SRTP_MASTER_KEY_LEN, remote_salt, SRTP_MASTER_SALT_LEN) < 0) {
1338 ast_log(LOG_WARNING, "Could not set key/salt information on remote policy of '%p' when setting up DTLS-SRTP\n", rtp);
1342 if (res_srtp_policy->set_suite(remote_policy, rtp->suite)) {
1343 ast_log(LOG_WARNING, "Could not set suite to '%d' on remote policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
1347 res_srtp_policy->set_ssrc(remote_policy, 0, 1);
1349 if (ast_rtp_instance_add_srtp_policy(instance, remote_policy, local_policy)) {
1350 ast_log(LOG_WARNING, "Could not set policies when setting up DTLS-SRTP on '%p'\n", rtp);
1355 ao2_ref(instance, +1);
1356 if ((rtp->rekeyid = ast_sched_add(rtp->sched, rtp->rekey * 1000, dtls_srtp_renegotiate, instance)) < 0) {
1357 ao2_ref(instance, -1);
1365 res_srtp_policy->destroy(local_policy);
1367 if (remote_policy) {
1368 res_srtp_policy->destroy(remote_policy);
1375 static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp)
1378 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1379 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
1381 if ((len = ast_recvfrom(rtcp ? rtp->rtcp->s : rtp->s, buf, size, flags, sa)) < 0) {
1385 #ifdef HAVE_OPENSSL_SRTP
1389 dtls_srtp_check_pending(instance, rtp);
1391 /* If this is an SSL packet pass it to OpenSSL for processing */
1392 if ((*in >= 20) && (*in <= 64)) {
1395 /* If no SSL session actually exists terminate things */
1397 ast_log(LOG_ERROR, "Received SSL traffic on RTP instance '%p' without an SSL session\n",
1402 /* If we don't yet know if we are active or passive and we receive a packet... we are obviously passive */
1403 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
1404 rtp->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
1405 SSL_set_accept_state(rtp->ssl);
1408 dtls_srtp_check_pending(instance, rtp);
1410 BIO_write(rtp->read_bio, buf, len);
1412 len = SSL_read(rtp->ssl, buf, len);
1414 dtls_srtp_check_pending(instance, rtp);
1416 if (rtp->dtls_failure) {
1417 ast_log(LOG_ERROR, "DTLS failure occurred on RTP instance '%p', terminating\n",
1422 if (SSL_is_init_finished(rtp->ssl)) {
1423 /* Any further connections will be existing since this is now established */
1424 rtp->connection = AST_RTP_DTLS_CONNECTION_EXISTING;
1426 /* Use the keying material to set up key/salt information */
1427 res = dtls_srtp_setup(rtp, srtp, instance);
1436 pj_str_t combined = pj_str(ast_sockaddr_stringify(sa));
1437 pj_sockaddr address;
1440 pj_thread_register_check();
1442 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &combined, &address);
1444 status = pj_ice_sess_on_rx_pkt(rtp->ice, rtcp ? COMPONENT_RTCP : COMPONENT_RTP,
1445 rtcp ? TRANSPORT_SOCKET_RTCP : TRANSPORT_SOCKET_RTP, buf, len, &address,
1446 pj_sockaddr_get_len(&address));
1447 if (status != PJ_SUCCESS) {
1450 pj_strerror(status, buf, sizeof(buf));
1451 ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n",
1455 if (!rtp->passthrough) {
1458 rtp->passthrough = 0;
1461 if (res_srtp && srtp && res_srtp->unprotect(srtp, buf, &len, rtcp) < 0) {
1468 static int rtcp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
1470 return __rtp_recvfrom(instance, buf, size, flags, sa, 1);
1473 static int rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
1475 return __rtp_recvfrom(instance, buf, size, flags, sa, 0);
1478 static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *ice, int use_srtp)
1482 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1483 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
1487 if (use_srtp && res_srtp && srtp && res_srtp->protect(srtp, &temp, &len, rtcp) < 0) {
1492 pj_thread_register_check();
1494 if (pj_ice_sess_send_data(rtp->ice, rtcp ? COMPONENT_RTCP : COMPONENT_RTP, temp, len) == PJ_SUCCESS) {
1500 return ast_sendto(rtcp ? rtp->rtcp->s : rtp->s, temp, len, flags, sa);
1503 static int rtcp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
1505 return __rtp_sendto(instance, buf, size, flags, sa, 1, ice, 1);
1508 static int rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
1510 return __rtp_sendto(instance, buf, size, flags, sa, 0, ice, 1);
1513 static int rtp_get_rate(struct ast_format *format)
1515 return (format->id == AST_FORMAT_G722) ? 8000 : ast_format_rate(format);
1518 static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
1520 unsigned int interval;
1521 /*! \todo XXX Do a more reasonable calculation on this one
1522 * Look in RFC 3550 Section A.7 for an example*/
1523 interval = rtcpinterval;
1527 /*! \brief Calculate normal deviation */
1528 static double normdev_compute(double normdev, double sample, unsigned int sample_count)
1530 normdev = normdev * sample_count + sample;
1533 return normdev / sample_count;
1536 static double stddev_compute(double stddev, double sample, double normdev, double normdev_curent, unsigned int sample_count)
1539 for the formula check http://www.cs.umd.edu/~austinjp/constSD.pdf
1540 return sqrt( (sample_count*pow(stddev,2) + sample_count*pow((sample-normdev)/(sample_count+1),2) + pow(sample-normdev_curent,2)) / (sample_count+1));
1541 we can compute the sigma^2 and that way we would have to do the sqrt only 1 time at the end and would save another pow 2 compute
1544 #define SQUARE(x) ((x) * (x))
1546 stddev = sample_count * stddev;
1550 ( sample_count * SQUARE( (sample - normdev) / sample_count ) ) +
1551 ( SQUARE(sample - normdev_curent) / sample_count );
1556 static int create_new_socket(const char *type, int af)
1558 int sock = socket(af, SOCK_DGRAM, 0);
1564 ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
1566 long flags = fcntl(sock, F_GETFL);
1567 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
1570 setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
1580 * \brief Initializes sequence values and probation for learning mode.
1581 * \note This is an adaptation of pjmedia's pjmedia_rtp_seq_init function.
1583 * \param rtp pointer to rtp struct used with the received rtp packet.
1584 * \param seq sequence number read from the rtp header
1586 static void rtp_learning_seq_init(struct ast_rtp *rtp, uint16_t seq)
1588 rtp->learning_max_seq = seq - 1;
1589 rtp->learning_probation = learning_min_sequential;
1594 * \brief Updates sequence information for learning mode and determines if probation/learning mode should remain in effect.
1595 * \note This function was adapted from pjmedia's pjmedia_rtp_seq_update function.
1597 * \param rtp pointer to rtp struct used with the received rtp packet.
1598 * \param seq sequence number read from the rtp header
1599 * \return boolean value indicating if probation mode is active at the end of the function
1601 static int rtp_learning_rtp_seq_update(struct ast_rtp *rtp, uint16_t seq)
1605 ast_debug(1, "%p -- probation = %d, seq = %d\n", rtp, rtp->learning_probation, seq);
1607 if (seq == rtp->learning_max_seq + 1) {
1608 /* packet is in sequence */
1609 rtp->learning_probation--;
1610 rtp->learning_max_seq = seq;
1611 if (rtp->learning_probation == 0) {
1615 rtp->learning_probation = learning_min_sequential - 1;
1616 rtp->learning_max_seq = seq;
1622 static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *addr, int port, int component,
1623 int transport, const pj_turn_sock_cb *turn_cb, pj_turn_sock **turn_sock)
1625 pj_sockaddr address[16];
1626 unsigned int count = PJ_ARRAY_SIZE(address), pos = 0;
1628 /* Add all the local interface IP addresses */
1629 pj_enum_ip_interface(ast_sockaddr_is_ipv4(addr) ? pj_AF_INET() : pj_AF_INET6(), &count, address);
1631 for (pos = 0; pos < count; pos++) {
1632 pj_sockaddr_set_port(&address[pos], port);
1633 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_HOST, 65535, &address[pos], &address[pos], NULL,
1634 pj_sockaddr_get_len(&address[pos]));
1637 /* If configured to use a STUN server to get our external mapped address do so */
1638 if (stunaddr.sin_addr.s_addr && ast_sockaddr_is_ipv4(addr)) {
1639 struct sockaddr_in answer;
1641 if (!ast_stun_request(rtp->s, &stunaddr, NULL, &answer)) {
1642 pj_str_t mapped = pj_str(ast_strdupa(ast_inet_ntoa(answer.sin_addr)));
1644 pj_sockaddr_init(pj_AF_INET(), &address[0], &mapped, ntohs(answer.sin_port));
1646 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_SRFLX, 65535, &address[0], &address[0],
1647 NULL, pj_sockaddr_get_len(&address[0]));
1651 /* If configured to use a TURN relay create a session and allocate */
1652 if (pj_strlen(&turnaddr) && pj_turn_sock_create(&rtp->ice->stun_cfg, ast_sockaddr_is_ipv4(addr) ? pj_AF_INET() : pj_AF_INET6(), PJ_TURN_TP_TCP,
1653 turn_cb, NULL, instance, turn_sock) == PJ_SUCCESS) {
1654 pj_stun_auth_cred cred = { 0, };
1655 struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_ALLOCATION_WAIT_TIME, 1000));
1656 struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
1658 cred.type = PJ_STUN_AUTH_CRED_STATIC;
1659 cred.data.static_cred.username = turnusername;
1660 cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
1661 cred.data.static_cred.data = turnpassword;
1663 /* Because the TURN socket is asynchronous but we are synchronous we need to wait until it is done */
1664 ast_mutex_lock(&rtp->lock);
1665 pj_turn_sock_alloc(*turn_sock, &turnaddr, turnport, NULL, &cred, NULL);
1666 ast_cond_timedwait(&rtp->cond, &rtp->lock, &ts);
1667 ast_mutex_unlock(&rtp->lock);
1669 /* If a TURN session was allocated add it as a candidate */
1670 if (rtp->turn_state == PJ_TURN_STATE_READY) {
1671 pj_turn_session_info info;
1673 pj_turn_sock_get_info(*turn_sock, &info);
1675 if (transport == TRANSPORT_SOCKET_RTP) {
1676 transport = TRANSPORT_TURN_RTP;
1677 } else if (transport == TRANSPORT_SOCKET_RTCP) {
1678 transport = TRANSPORT_TURN_RTCP;
1681 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_RELAYED, 65535, &info.relay_addr, &info.relay_addr,
1682 NULL, pj_sockaddr_get_len(&info.relay_addr));
1687 static int ast_rtp_new(struct ast_rtp_instance *instance,
1688 struct ast_sched_context *sched, struct ast_sockaddr *addr,
1691 struct ast_rtp *rtp = NULL;
1693 pj_stun_config stun_config;
1694 pj_str_t ufrag, passwd;
1696 /* Create a new RTP structure to hold all of our data */
1697 if (!(rtp = ast_calloc(1, sizeof(*rtp)))) {
1701 /* Initialize synchronization aspects */
1702 ast_mutex_init(&rtp->lock);
1703 ast_cond_init(&rtp->cond, NULL);
1705 /* Set default parameters on the newly created RTP structure */
1706 rtp->ssrc = ast_random();
1707 rtp->seqno = ast_random() & 0xffff;
1708 rtp->strict_rtp_state = (strictrtp ? STRICT_RTP_LEARN : STRICT_RTP_OPEN);
1710 rtp_learning_seq_init(rtp, (uint16_t)rtp->seqno);
1713 /* Create a new socket for us to listen on and use */
1715 create_new_socket("RTP",
1716 ast_sockaddr_is_ipv4(addr) ? AF_INET :
1717 ast_sockaddr_is_ipv6(addr) ? AF_INET6 : -1)) < 0) {
1718 ast_debug(1, "Failed to create a new socket for RTP instance '%p'\n", instance);
1723 /* Now actually find a free RTP port to use */
1724 x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
1729 ast_sockaddr_set_port(addr, x);
1730 /* Try to bind, this will tell us whether the port is available or not */
1731 if (!ast_bind(rtp->s, addr)) {
1732 ast_debug(1, "Allocated port %d for RTP instance '%p'\n", x, instance);
1733 ast_rtp_instance_set_local_address(instance, addr);
1739 x = (rtpstart + 1) & ~1;
1742 /* See if we ran out of ports or if the bind actually failed because of something other than the address being in use */
1743 if (x == startplace || errno != EADDRINUSE) {
1744 ast_log(LOG_ERROR, "Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance);
1751 pj_thread_register_check();
1753 pj_stun_config_init(&stun_config, &cachingpool.factory, 0, ioqueue, timerheap);
1755 generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag));
1756 ufrag = pj_str(rtp->local_ufrag);
1757 generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd));
1758 passwd = pj_str(rtp->local_passwd);
1760 ast_rtp_instance_set_data(instance, rtp);
1762 /* Create an ICE session for ICE negotiation */
1763 if (icesupport && pj_ice_sess_create(&stun_config, NULL, PJ_ICE_SESS_ROLE_UNKNOWN, 2, &ast_rtp_ice_sess_cb, &ufrag, &passwd, &rtp->ice) == PJ_SUCCESS) {
1764 /* Make this available for the callbacks */
1765 rtp->ice->user_data = rtp;
1767 /* Add all of the available candidates to the ICE session */
1768 rtp_add_candidates_to_ice(instance, rtp, addr, x, COMPONENT_RTP, TRANSPORT_SOCKET_RTP, &ast_rtp_turn_rtp_sock_cb, &rtp->turn_rtp);
1771 /* Record any information we may need */
1774 #ifdef HAVE_OPENSSL_SRTP
1781 static int ast_rtp_destroy(struct ast_rtp_instance *instance)
1783 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1785 /* Destroy the smoother that was smoothing out audio if present */
1786 if (rtp->smoother) {
1787 ast_smoother_free(rtp->smoother);
1790 /* Close our own socket so we no longer get packets */
1795 /* Destroy RTCP if it was being used */
1798 * It is not possible for there to be an active RTCP scheduler
1799 * entry at this point since it holds a reference to the
1800 * RTP instance while it's active.
1802 close(rtp->rtcp->s);
1803 ast_free(rtp->rtcp);
1806 /* Destroy RED if it was being used */
1808 AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
1812 pj_thread_register_check();
1814 /* Destroy the ICE session if being used */
1816 pj_ice_sess_destroy(rtp->ice);
1819 /* Destroy the RTP TURN relay if being used */
1820 if (rtp->turn_rtp) {
1821 pj_turn_sock_set_user_data(rtp->turn_rtp, NULL);
1822 pj_turn_sock_destroy(rtp->turn_rtp);
1825 /* Destroy the RTCP TURN relay if being used */
1826 if (rtp->turn_rtcp) {
1827 pj_turn_sock_set_user_data(rtp->turn_rtcp, NULL);
1828 pj_turn_sock_destroy(rtp->turn_rtcp);
1831 /* Destroy any candidates */
1832 if (rtp->local_candidates) {
1833 ao2_ref(rtp->local_candidates, -1);
1836 if (rtp->remote_candidates) {
1837 ao2_ref(rtp->remote_candidates, -1);
1840 #ifdef HAVE_OPENSSL_SRTP
1841 /* Destroy the SSL context if present */
1843 SSL_CTX_free(rtp->ssl_ctx);
1846 /* Destroy the SSL session if present */
1852 /* Destroy synchronization items */
1853 ast_mutex_destroy(&rtp->lock);
1854 ast_cond_destroy(&rtp->cond);
1856 /* Finally destroy ourselves */
1862 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
1864 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1865 rtp->dtmfmode = dtmf_mode;
1869 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance)
1871 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1872 return rtp->dtmfmode;
1875 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit)
1877 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1878 struct ast_sockaddr remote_address = { {0,} };
1879 int hdrlen = 12, res = 0, i = 0, payload = 101;
1881 unsigned int *rtpheader = (unsigned int*)data;
1883 ast_rtp_instance_get_remote_address(instance, &remote_address);
1885 /* If we have no remote address information bail out now */
1886 if (ast_sockaddr_isnull(&remote_address)) {
1890 /* Convert given digit into what we want to transmit */
1891 if ((digit <= '9') && (digit >= '0')) {
1893 } else if (digit == '*') {
1895 } else if (digit == '#') {
1897 } else if ((digit >= 'A') && (digit <= 'D')) {
1898 digit = digit - 'A' + 12;
1899 } else if ((digit >= 'a') && (digit <= 'd')) {
1900 digit = digit - 'a' + 12;
1902 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
1906 /* Grab the payload that they expect the RFC2833 packet to be received in */
1907 payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 0, NULL, AST_RTP_DTMF);
1909 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
1910 rtp->send_duration = 160;
1911 rtp->lastdigitts = rtp->lastts + rtp->send_duration;
1913 /* Create the actual packet that we will be sending */
1914 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
1915 rtpheader[1] = htonl(rtp->lastdigitts);
1916 rtpheader[2] = htonl(rtp->ssrc);
1918 /* Actually send the packet */
1919 for (i = 0; i < 2; i++) {
1922 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
1923 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
1925 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
1926 ast_sockaddr_stringify(&remote_address),
1929 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
1930 if (rtp_debug_test_addr(&remote_address)) {
1931 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
1932 ast_sockaddr_stringify(&remote_address),
1933 ice ? " (via ICE)" : "",
1934 payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
1937 rtp->send_duration += 160;
1938 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
1941 /* Record that we are in the process of sending a digit and information needed to continue doing so */
1942 rtp->sending_digit = 1;
1943 rtp->send_digit = digit;
1944 rtp->send_payload = payload;
1949 static int ast_rtp_dtmf_continuation(struct ast_rtp_instance *instance)
1951 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1952 struct ast_sockaddr remote_address = { {0,} };
1953 int hdrlen = 12, res = 0;
1955 unsigned int *rtpheader = (unsigned int*)data;
1958 ast_rtp_instance_get_remote_address(instance, &remote_address);
1960 /* Make sure we know where the other side is so we can send them the packet */
1961 if (ast_sockaddr_isnull(&remote_address)) {
1965 /* Actually create the packet we will be sending */
1966 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
1967 rtpheader[1] = htonl(rtp->lastdigitts);
1968 rtpheader[2] = htonl(rtp->ssrc);
1969 rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
1971 /* Boom, send it on out */
1972 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
1974 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
1975 ast_sockaddr_stringify(&remote_address),
1979 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
1981 if (rtp_debug_test_addr(&remote_address)) {
1982 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
1983 ast_sockaddr_stringify(&remote_address),
1984 ice ? " (via ICE)" : "",
1985 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
1988 /* And now we increment some values for the next time we swing by */
1990 rtp->send_duration += 160;
1995 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
1997 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1998 struct ast_sockaddr remote_address = { {0,} };
1999 int hdrlen = 12, res = 0, i = 0;
2001 unsigned int *rtpheader = (unsigned int*)data;
2002 unsigned int measured_samples;
2004 ast_rtp_instance_get_remote_address(instance, &remote_address);
2006 /* Make sure we know where the remote side is so we can send them the packet we construct */
2007 if (ast_sockaddr_isnull(&remote_address)) {
2011 /* Convert the given digit to the one we are going to send */
2012 if ((digit <= '9') && (digit >= '0')) {
2014 } else if (digit == '*') {
2016 } else if (digit == '#') {
2018 } else if ((digit >= 'A') && (digit <= 'D')) {
2019 digit = digit - 'A' + 12;
2020 } else if ((digit >= 'a') && (digit <= 'd')) {
2021 digit = digit - 'a' + 12;
2023 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
2027 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2029 if (duration > 0 && (measured_samples = duration * rtp_get_rate(&rtp->f.subclass.format) / 1000) > rtp->send_duration) {
2030 ast_debug(2, "Adjusting final end duration from %u to %u\n", rtp->send_duration, measured_samples);
2031 rtp->send_duration = measured_samples;
2034 /* Construct the packet we are going to send */
2035 rtpheader[1] = htonl(rtp->lastdigitts);
2036 rtpheader[2] = htonl(rtp->ssrc);
2037 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
2038 rtpheader[3] |= htonl((1 << 23));
2040 /* Send it 3 times, that's the magical number */
2041 for (i = 0; i < 3; i++) {
2044 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
2046 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
2049 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
2050 ast_sockaddr_stringify(&remote_address),
2054 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
2056 if (rtp_debug_test_addr(&remote_address)) {
2057 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2058 ast_sockaddr_stringify(&remote_address),
2059 ice ? " (via ICE)" : "",
2060 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2066 /* Oh and we can't forget to turn off the stuff that says we are sending DTMF */
2067 rtp->lastts += rtp->send_duration;
2068 rtp->sending_digit = 0;
2069 rtp->send_digit = 0;
2074 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit)
2076 return ast_rtp_dtmf_end_with_duration(instance, digit, 0);
2079 static void ast_rtp_update_source(struct ast_rtp_instance *instance)
2081 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2083 /* We simply set this bit so that the next packet sent will have the marker bit turned on */
2084 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
2085 ast_debug(3, "Setting the marker bit due to a source update\n");
2090 static void ast_rtp_change_source(struct ast_rtp_instance *instance)
2092 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2093 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
2094 unsigned int ssrc = ast_random();
2097 ast_debug(3, "Not changing SSRC since we haven't sent any RTP yet\n");
2101 /* We simply set this bit so that the next packet sent will have the marker bit turned on */
2102 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
2104 ast_debug(3, "Changing ssrc from %u to %u due to a source change\n", rtp->ssrc, ssrc);
2107 ast_debug(3, "Changing ssrc for SRTP from %u to %u\n", rtp->ssrc, ssrc);
2108 res_srtp->change_source(srtp, rtp->ssrc, ssrc);
2116 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
2121 if (ast_tvzero(rtp->txcore)) {
2122 rtp->txcore = ast_tvnow();
2123 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
2126 t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
2127 if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) {
2132 return (unsigned int) ms;
2135 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
2137 unsigned int sec, usec, frac;
2138 sec = tv.tv_sec + 2208988800u; /* Sec between 1900 and 1970 */
2140 frac = (usec << 12) + (usec << 8) - ((usec * 3650) >> 6);
2145 /*! \brief Send RTCP recipient's report */
2146 static int ast_rtcp_write_rr(struct ast_rtp_instance *instance)
2148 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2152 unsigned int extended;
2153 unsigned int expected;
2154 unsigned int expected_interval;
2155 unsigned int received_interval;
2158 unsigned int *rtcpheader;
2160 struct timeval dlsr;
2162 int rate = rtp_get_rate(&rtp->f.subclass.format);
2164 double rxlost_current;
2165 struct ast_sockaddr remote_address = { {0,} };
2167 if (!rtp || !rtp->rtcp)
2170 if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
2177 extended = rtp->cycles + rtp->lastrxseqno;
2178 expected = extended - rtp->seedrxseqno + 1;
2179 lost = expected - rtp->rxcount;
2180 expected_interval = expected - rtp->rtcp->expected_prior;
2181 rtp->rtcp->expected_prior = expected;
2182 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
2183 rtp->rtcp->received_prior = rtp->rxcount;
2184 lost_interval = expected_interval - received_interval;
2186 if (lost_interval <= 0)
2187 rtp->rtcp->rxlost = 0;
2188 else rtp->rtcp->rxlost = rtp->rtcp->rxlost;
2189 if (rtp->rtcp->rxlost_count == 0)
2190 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
2191 if (lost_interval < rtp->rtcp->minrxlost)
2192 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
2193 if (lost_interval > rtp->rtcp->maxrxlost)
2194 rtp->rtcp->maxrxlost = rtp->rtcp->rxlost;
2196 rxlost_current = normdev_compute(rtp->rtcp->normdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->rxlost_count);
2197 rtp->rtcp->stdev_rxlost = stddev_compute(rtp->rtcp->stdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->normdev_rxlost, rxlost_current, rtp->rtcp->rxlost_count);
2198 rtp->rtcp->normdev_rxlost = rxlost_current;
2199 rtp->rtcp->rxlost_count++;
2201 if (expected_interval == 0 || lost_interval <= 0)
2204 fraction = (lost_interval << 8) / expected_interval;
2205 gettimeofday(&now, NULL);
2206 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
2207 rtcpheader = (unsigned int *)bdata;
2208 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_RR << 16) | ((len/4)-1));
2209 rtcpheader[1] = htonl(rtp->ssrc);
2210 rtcpheader[2] = htonl(rtp->themssrc);
2211 rtcpheader[3] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
2212 rtcpheader[4] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
2213 rtcpheader[5] = htonl((unsigned int)(rtp->rxjitter * rate));
2214 rtcpheader[6] = htonl(rtp->rtcp->themrxlsr);
2215 rtcpheader[7] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
2217 /*! \note Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos
2218 it can change mid call, and SDES can't) */
2219 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
2220 rtcpheader[(len/4)+1] = htonl(rtp->ssrc); /* Our SSRC */
2221 rtcpheader[(len/4)+2] = htonl(0x01 << 24); /* Empty for the moment */
2224 ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
2226 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &remote_address, &ice);
2229 ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno));
2233 rtp->rtcp->rr_count++;
2235 update_address_with_ice_candidate(rtp, COMPONENT_RTCP, &remote_address);
2237 if (rtcp_debug_test_addr(&remote_address)) {
2238 ast_verbose("\n* Sending RTCP RR to %s%s\n"
2239 " Our SSRC: %u\nTheir SSRC: %u\niFraction lost: %d\nCumulative loss: %u\n"
2240 " IA jitter: %.4f\n"
2241 " Their last SR: %u\n"
2242 " DLSR: %4.4f (sec)\n\n",
2243 ast_sockaddr_stringify(&remote_address),
2244 ice ? " (via ICE)" : "",
2245 rtp->ssrc, rtp->themssrc, fraction, lost,
2247 rtp->rtcp->themrxlsr,
2248 (double)(ntohl(rtcpheader[7])/65536.0));
2254 /*! \brief Send RTCP sender's report */
2255 static int ast_rtcp_write_sr(struct ast_rtp_instance *instance)
2257 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2261 unsigned int now_lsw;
2262 unsigned int now_msw;
2263 unsigned int *rtcpheader;
2265 unsigned int extended;
2266 unsigned int expected;
2267 unsigned int expected_interval;
2268 unsigned int received_interval;
2271 struct timeval dlsr;
2273 int rate = rtp_get_rate(&rtp->f.subclass.format);
2275 struct ast_sockaddr remote_address = { {0,} };
2277 if (!rtp || !rtp->rtcp)
2280 if (ast_sockaddr_isnull(&rtp->rtcp->them)) { /* This'll stop rtcp for this rtp session */
2287 gettimeofday(&now, NULL);
2288 timeval2ntp(now, &now_msw, &now_lsw); /* fill thses ones in from utils.c*/
2289 rtcpheader = (unsigned int *)bdata;
2290 rtcpheader[1] = htonl(rtp->ssrc); /* Our SSRC */
2291 rtcpheader[2] = htonl(now_msw); /* now, MSW. gettimeofday() + SEC_BETWEEN_1900_AND_1970*/
2292 rtcpheader[3] = htonl(now_lsw); /* now, LSW */
2293 rtcpheader[4] = htonl(rtp->lastts); /* FIXME shouldn't be that, it should be now */
2294 rtcpheader[5] = htonl(rtp->txcount); /* No. packets sent */
2295 rtcpheader[6] = htonl(rtp->txoctetcount); /* No. bytes sent */
2298 extended = rtp->cycles + rtp->lastrxseqno;
2299 expected = extended - rtp->seedrxseqno + 1;
2300 if (rtp->rxcount > expected)
2301 expected += rtp->rxcount - expected;
2302 lost = expected - rtp->rxcount;
2303 expected_interval = expected - rtp->rtcp->expected_prior;
2304 rtp->rtcp->expected_prior = expected;
2305 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
2306 rtp->rtcp->received_prior = rtp->rxcount;
2307 lost_interval = expected_interval - received_interval;
2308 if (expected_interval == 0 || lost_interval <= 0)
2311 fraction = (lost_interval << 8) / expected_interval;
2312 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
2313 rtcpheader[7] = htonl(rtp->themssrc);
2314 rtcpheader[8] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
2315 rtcpheader[9] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
2316 rtcpheader[10] = htonl((unsigned int)(rtp->rxjitter * rate));
2317 rtcpheader[11] = htonl(rtp->rtcp->themrxlsr);
2318 rtcpheader[12] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
2321 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SR << 16) | ((len/4)-1));
2323 /* Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos */
2324 /* it can change mid call, and SDES can't) */
2325 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
2326 rtcpheader[(len/4)+1] = htonl(rtp->ssrc); /* Our SSRC */
2327 rtcpheader[(len/4)+2] = htonl(0x01 << 24); /* Empty for the moment */
2330 ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
2332 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &remote_address, &ice);
2334 ast_log(LOG_ERROR, "RTCP SR transmission error to %s, rtcp halted %s\n",
2335 ast_sockaddr_stringify(&rtp->rtcp->them),
2340 /* FIXME Don't need to get a new one */
2341 gettimeofday(&rtp->rtcp->txlsr, NULL);
2342 rtp->rtcp->sr_count++;
2344 rtp->rtcp->lastsrtxcount = rtp->txcount;
2346 update_address_with_ice_candidate(rtp, COMPONENT_RTCP, &remote_address);
2348 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
2349 ast_verbose("* Sent RTCP SR to %s%s\n", ast_sockaddr_stringify(&remote_address), ice ? " (via ICE)" : "");
2350 ast_verbose(" Our SSRC: %u\n", rtp->ssrc);
2351 ast_verbose(" Sent(NTP): %u.%010u\n", (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096);
2352 ast_verbose(" Sent(RTP): %u\n", rtp->lastts);
2353 ast_verbose(" Sent packets: %u\n", rtp->txcount);
2354 ast_verbose(" Sent octets: %u\n", rtp->txoctetcount);
2355 ast_verbose(" Report block:\n");
2356 ast_verbose(" Fraction lost: %u\n", fraction);
2357 ast_verbose(" Cumulative loss: %u\n", lost);
2358 ast_verbose(" IA jitter: %.4f\n", rtp->rxjitter);
2359 ast_verbose(" Their last SR: %u\n", rtp->rtcp->themrxlsr);
2360 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(ntohl(rtcpheader[12])/65536.0));
2362 manager_event(EVENT_FLAG_REPORTING, "RTCPSent", "To: %s\r\n"
2364 "SentNTP: %u.%010u\r\n"
2366 "SentPackets: %u\r\n"
2367 "SentOctets: %u\r\n"
2369 "FractionLost: %u\r\n"
2370 "CumulativeLoss: %u\r\n"
2371 "IAJitter: %.4f\r\n"
2372 "TheirLastSR: %u\r\n"
2373 "DLSR: %4.4f (sec)\r\n",
2374 ast_sockaddr_stringify(&remote_address),
2376 (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096,
2383 rtp->rtcp->themrxlsr,
2384 (double)(ntohl(rtcpheader[12])/65536.0));
2388 /*! \brief Write and RTCP packet to the far end
2389 * \note Decide if we are going to send an SR (with Reception Block) or RR
2390 * RR is sent if we have not sent any rtp packets in the previous interval */
2391 static int ast_rtcp_write(const void *data)
2393 struct ast_rtp_instance *instance = (struct ast_rtp_instance *) data;
2394 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2397 if (!rtp || !rtp->rtcp || rtp->rtcp->schedid == -1) {
2398 ao2_ref(instance, -1);
2402 if (rtp->txcount > rtp->rtcp->lastsrtxcount) {
2403 res = ast_rtcp_write_sr(instance);
2405 res = ast_rtcp_write_rr(instance);
2410 * Not being rescheduled.
2412 ao2_ref(instance, -1);
2413 rtp->rtcp->schedid = -1;
2419 static int ast_rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec)
2421 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2423 unsigned int ms = calc_txstamp(rtp, &frame->delivery);
2424 struct ast_sockaddr remote_address = { {0,} };
2425 int rate = rtp_get_rate(&frame->subclass.format) / 1000;
2427 if (frame->subclass.format.id == AST_FORMAT_G722) {
2428 frame->samples /= 2;
2431 if (rtp->sending_digit) {
2435 if (frame->frametype == AST_FRAME_VOICE) {
2436 pred = rtp->lastts + frame->samples;
2438 /* Re-calculate last TS */
2439 rtp->lastts = rtp->lastts + ms * rate;
2440 if (ast_tvzero(frame->delivery)) {
2441 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
2442 and if so, go with our prediction */
2443 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW) {
2446 ast_debug(3, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
2450 } else if (frame->frametype == AST_FRAME_VIDEO) {
2451 mark = ast_format_get_video_mark(&frame->subclass.format);
2452 pred = rtp->lastovidtimestamp + frame->samples;
2453 /* Re-calculate last TS */
2454 rtp->lastts = rtp->lastts + ms * 90;
2455 /* If it's close to our prediction, go for it */
2456 if (ast_tvzero(frame->delivery)) {
2457 if (abs(rtp->lastts - pred) < 7200) {
2459 rtp->lastovidtimestamp += frame->samples;
2461 ast_debug(3, "Difference is %d, ms is %d (%d), pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, frame->samples);
2462 rtp->lastovidtimestamp = rtp->lastts;
2466 pred = rtp->lastotexttimestamp + frame->samples;
2467 /* Re-calculate last TS */
2468 rtp->lastts = rtp->lastts + ms;
2469 /* If it's close to our prediction, go for it */
2470 if (ast_tvzero(frame->delivery)) {
2471 if (abs(rtp->lastts - pred) < 7200) {
2473 rtp->lastotexttimestamp += frame->samples;
2475 ast_debug(3, "Difference is %d, ms is %d, pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, rtp->lastts, pred, frame->samples);
2476 rtp->lastotexttimestamp = rtp->lastts;
2481 /* If we have been explicitly told to set the marker bit then do so */
2482 if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
2484 ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT);
2487 /* If the timestamp for non-digt packets has moved beyond the timestamp for digits, update the digit timestamp */
2488 if (rtp->lastts > rtp->lastdigitts) {
2489 rtp->lastdigitts = rtp->lastts;
2492 if (ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO)) {
2493 rtp->lastts = frame->ts * rate;
2496 ast_rtp_instance_get_remote_address(instance, &remote_address);
2498 /* If we know the remote address construct a packet and send it out */
2499 if (!ast_sockaddr_isnull(&remote_address)) {
2500 int hdrlen = 12, res, ice;
2501 unsigned char *rtpheader = (unsigned char *)(frame->data.ptr - hdrlen);
2503 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
2504 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
2505 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
2507 if ((res = rtp_sendto(instance, (void *)rtpheader, frame->datalen + hdrlen, 0, &remote_address, &ice)) < 0) {
2508 if (!ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT) || (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT) && (ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
2509 ast_debug(1, "RTP Transmission error of packet %d to %s: %s\n",
2511 ast_sockaddr_stringify(&remote_address),
2513 } else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) {
2514 /* Only give this error message once if we are not RTP debugging */
2516 ast_debug(0, "RTP NAT: Can't write RTP to private address %s, waiting for other end to send audio...\n",
2517 ast_sockaddr_stringify(&remote_address));
2518 ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN);
2522 rtp->txoctetcount += (res - hdrlen);
2524 if (rtp->rtcp && rtp->rtcp->schedid < 1) {
2525 ast_debug(1, "Starting RTCP transmission on RTP instance '%p'\n", instance);
2526 ao2_ref(instance, +1);
2527 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, instance);
2528 if (rtp->rtcp->schedid < 0) {
2529 ao2_ref(instance, -1);
2530 ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n");
2535 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
2537 if (rtp_debug_test_addr(&remote_address)) {
2538 ast_verbose("Sent RTP packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2539 ast_sockaddr_stringify(&remote_address),
2540 ice ? " (via ICE)" : "",
2541 codec, rtp->seqno, rtp->lastts, res - hdrlen);
2550 static struct ast_frame *red_t140_to_red(struct rtp_red *red) {
2551 unsigned char *data = red->t140red.data.ptr;
2555 /* replace most aged generation */
2557 for (i = 1; i < red->num_gen+1; i++)
2560 memmove(&data[red->hdrlen], &data[red->hdrlen+red->len[0]], len);
2563 /* Store length of each generation and primary data length*/
2564 for (i = 0; i < red->num_gen; i++)
2565 red->len[i] = red->len[i+1];
2566 red->len[i] = red->t140.datalen;
2568 /* write each generation length in red header */
2570 for (i = 0; i < red->num_gen; i++)
2571 len += data[i*4+3] = red->len[i];
2573 /* add primary data to buffer */
2574 memcpy(&data[len], red->t140.data.ptr, red->t140.datalen);
2575 red->t140red.datalen = len + red->t140.datalen;
2577 /* no primary data and no generations to send */
2578 if (len == red->hdrlen && !red->t140.datalen)
2581 /* reset t.140 buffer */
2582 red->t140.datalen = 0;
2584 return &red->t140red;
2587 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
2589 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2590 struct ast_sockaddr remote_address = { {0,} };
2591 struct ast_format subclass;
2594 ast_rtp_instance_get_remote_address(instance, &remote_address);
2596 /* If we don't actually know the remote address don't even bother doing anything */
2597 if (ast_sockaddr_isnull(&remote_address)) {
2598 ast_debug(1, "No remote address on RTP instance '%p' so dropping frame\n", instance);
2602 /* If there is no data length we can't very well send the packet */
2603 if (!frame->datalen) {
2604 ast_debug(1, "Received frame with no data for RTP instance '%p' so dropping frame\n", instance);
2608 /* If the packet is not one our RTP stack supports bail out */
2609 if (frame->frametype != AST_FRAME_VOICE && frame->frametype != AST_FRAME_VIDEO && frame->frametype != AST_FRAME_TEXT) {
2610 ast_log(LOG_WARNING, "RTP can only send voice, video, and text\n");
2616 /* no primary data or generations to send */
2617 if ((frame = red_t140_to_red(rtp->red)) == NULL)
2621 /* Grab the subclass and look up the payload we are going to use */
2622 ast_format_copy(&subclass, &frame->subclass.format);
2623 if ((codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 1, &subclass, 0)) < 0) {
2624 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(&frame->subclass.format));
2628 /* Oh dear, if the format changed we will have to set up a new smoother */
2629 if (ast_format_cmp(&rtp->lasttxformat, &subclass) == AST_FORMAT_CMP_NOT_EQUAL) {
2630 ast_debug(1, "Ooh, format changed from %s to %s\n", ast_getformatname(&rtp->lasttxformat), ast_getformatname(&subclass));
2631 rtp->lasttxformat = subclass;
2632 ast_format_copy(&rtp->lasttxformat, &subclass);
2633 if (rtp->smoother) {
2634 ast_smoother_free(rtp->smoother);
2635 rtp->smoother = NULL;
2639 /* If no smoother is present see if we have to set one up */
2640 if (!rtp->smoother) {
2641 struct ast_format_list fmt = ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance)->pref, &subclass);
2643 switch (subclass.id) {
2644 case AST_FORMAT_SPEEX:
2645 case AST_FORMAT_SPEEX16:
2646 case AST_FORMAT_SPEEX32:
2647 case AST_FORMAT_SILK:
2648 case AST_FORMAT_CELT:
2649 case AST_FORMAT_G723_1:
2650 case AST_FORMAT_SIREN7:
2651 case AST_FORMAT_SIREN14:
2652 case AST_FORMAT_G719:
2653 /* these are all frame-based codecs and cannot be safely run through
2658 if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
2659 ast_log(LOG_WARNING, "Unable to create smoother: format %s ms: %d len: %d\n", ast_getformatname(&subclass), fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
2663 ast_smoother_set_flags(rtp->smoother, fmt.flags);
2665 ast_debug(1, "Created smoother: format: %s ms: %d len: %d\n", ast_getformatname(&subclass), fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
2670 /* Feed audio frames into the actual function that will create a frame and send it */
2671 if (rtp->smoother) {
2672 struct ast_frame *f;
2674 if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
2675 ast_smoother_feed_be(rtp->smoother, frame);
2677 ast_smoother_feed(rtp->smoother, frame);
2680 while ((f = ast_smoother_read(rtp->smoother)) && (f->data.ptr)) {
2681 ast_rtp_raw_write(instance, f, codec);
2685 struct ast_frame *f = NULL;
2687 if (frame->offset < hdrlen) {
2688 f = ast_frdup(frame);
2693 ast_rtp_raw_write(instance, f, codec);
2704 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
2709 double current_time;
2713 int rate = rtp_get_rate(&rtp->f.subclass.format);
2715 double normdev_rxjitter_current;
2716 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
2717 gettimeofday(&rtp->rxcore, NULL);
2718 rtp->drxcore = (double) rtp->rxcore.tv_sec + (double) rtp->rxcore.tv_usec / 1000000;
2719 /* map timestamp to a real time */
2720 rtp->seedrxts = timestamp; /* Their RTP timestamp started with this */
2721 tmp = ast_samp2tv(timestamp, rate);
2722 rtp->rxcore = ast_tvsub(rtp->rxcore, tmp);
2723 /* Round to 0.1ms for nice, pretty timestamps */
2724 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
2727 gettimeofday(&now,NULL);
2728 /* rxcore is the mapping between the RTP timestamp and _our_ real time from gettimeofday() */
2729 tmp = ast_samp2tv(timestamp, rate);
2730 *tv = ast_tvadd(rtp->rxcore, tmp);
2732 prog = (double)((timestamp-rtp->seedrxts)/(float)(rate));
2733 dtv = (double)rtp->drxcore + (double)(prog);
2734 current_time = (double)now.tv_sec + (double)now.tv_usec/1000000;
2735 transit = current_time - dtv;
2736 d = transit - rtp->rxtransit;
2737 rtp->rxtransit = transit;
2740 rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
2743 if (rtp->rxjitter > rtp->rtcp->maxrxjitter)
2744 rtp->rtcp->maxrxjitter = rtp->rxjitter;
2745 if (rtp->rtcp->rxjitter_count == 1)
2746 rtp->rtcp->minrxjitter = rtp->rxjitter;
2747 if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter)
2748 rtp->rtcp->minrxjitter = rtp->rxjitter;
2750 normdev_rxjitter_current = normdev_compute(rtp->rtcp->normdev_rxjitter,rtp->rxjitter,rtp->rtcp->rxjitter_count);
2751 rtp->rtcp->stdev_rxjitter = stddev_compute(rtp->rtcp->stdev_rxjitter,rtp->rxjitter,rtp->rtcp->normdev_rxjitter,normdev_rxjitter_current,rtp->rtcp->rxjitter_count);
2753 rtp->rtcp->normdev_rxjitter = normdev_rxjitter_current;
2754 rtp->rtcp->rxjitter_count++;
2758 static struct ast_frame *create_dtmf_frame(struct ast_rtp_instance *instance, enum ast_frame_type type, int compensate)
2760 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2761 struct ast_sockaddr remote_address = { {0,} };
2763 ast_rtp_instance_get_remote_address(instance, &remote_address);
2765 if (((compensate && type == AST_FRAME_DTMF_END) || (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
2766 ast_debug(1, "Ignore potential DTMF echo from '%s'\n",
2767 ast_sockaddr_stringify(&remote_address));
2769 rtp->dtmfsamples = 0;
2770 return &ast_null_frame;
2772 ast_debug(1, "Creating %s DTMF Frame: %d (%c), at %s\n",
2773 type == AST_FRAME_DTMF_END ? "END" : "BEGIN",
2774 rtp->resp, rtp->resp,
2775 ast_sockaddr_stringify(&remote_address));
2776 if (rtp->resp == 'X') {
2777 rtp->f.frametype = AST_FRAME_CONTROL;
2778 rtp->f.subclass.integer = AST_CONTROL_FLASH;
2780 rtp->f.frametype = type;
2781 rtp->f.subclass.integer = rtp->resp;
2787 AST_LIST_NEXT(&rtp->f, frame_list) = NULL;
2792 static void process_dtmf_rfc2833(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, struct ast_sockaddr *addr, int payloadtype, int mark, struct frame_list *frames)
2794 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2795 struct ast_sockaddr remote_address = { {0,} };
2796 unsigned int event, event_end, samples;
2798 struct ast_frame *f = NULL;
2800 ast_rtp_instance_get_remote_address(instance, &remote_address);
2802 /* Figure out event, event end, and samples */
2803 event = ntohl(*((unsigned int *)(data)));
2805 event_end = ntohl(*((unsigned int *)(data)));
2808 samples = ntohl(*((unsigned int *)(data)));
2811 if (rtp_debug_test_addr(&remote_address)) {
2812 ast_verbose("Got RTP RFC2833 from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u, mark %d, event %08x, end %d, duration %-5.5d) \n",
2813 ast_sockaddr_stringify(&remote_address),
2814 payloadtype, seqno, timestamp, len, (mark?1:0), event, ((event_end & 0x80)?1:0), samples);
2817 /* Print out debug if turned on */
2819 ast_debug(0, "- RTP 2833 Event: %08x (len = %d)\n", event, len);
2821 /* Figure out what digit was pressed */
2824 } else if (event < 11) {
2826 } else if (event < 12) {
2828 } else if (event < 16) {
2829 resp = 'A' + (event - 12);
2830 } else if (event < 17) { /* Event 16: Hook flash */
2833 /* Not a supported event */
2834 ast_debug(1, "Ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", event);
2838 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) {
2839 if ((rtp->last_end_timestamp != timestamp) || (rtp->resp && rtp->resp != resp)) {
2841 rtp->dtmf_timeout = 0;
2842 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)));
2844 rtp->last_end_timestamp = timestamp;
2845 AST_LIST_INSERT_TAIL(frames, f, frame_list);
2848 /* The duration parameter measures the complete
2849 duration of the event (from the beginning) - RFC2833.
2850 Account for the fact that duration is only 16 bits long
2851 (about 8 seconds at 8000 Hz) and can wrap is digit
2852 is hold for too long. */
2853 unsigned int new_duration = rtp->dtmf_duration;
2854 unsigned int last_duration = new_duration & 0xFFFF;
2856 if (last_duration > 64000 && samples < last_duration) {
2857 new_duration += 0xFFFF + 1;
2859 new_duration = (new_duration & ~0xFFFF) | samples;
2861 if (event_end & 0x80) {
2863 if ((rtp->last_seqno != seqno) && (timestamp > rtp->last_end_timestamp)) {
2864 rtp->last_end_timestamp = timestamp;
2865 rtp->dtmf_duration = new_duration;
2867 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
2868 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0));
2870 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
2871 AST_LIST_INSERT_TAIL(frames, f, frame_list);
2872 } else if (rtpdebug) {
2873 ast_debug(1, "Dropping duplicate or out of order DTMF END frame (seqno: %d, ts %d, digit %c)\n",
2874 seqno, timestamp, resp);
2877 /* Begin/continuation */
2879 /* The second portion of the seqno check is to not mistakenly
2880 * stop accepting DTMF if the seqno rolls over beyond
2883 if ((rtp->last_seqno > seqno && rtp->last_seqno - seqno < 50)
2884 || timestamp <= rtp->last_end_timestamp) {
2885 /* Out of order frame. Processing this can cause us to
2886 * improperly duplicate incoming DTMF, so just drop
2890 ast_debug(1, "Dropping out of order DTMF frame (seqno %d, ts %d, digit %c)\n",
2891 seqno, timestamp, resp);
2896 if (rtp->resp && rtp->resp != resp) {
2897 /* Another digit already began. End it */
2898 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
2899 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0));
2901 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
2902 AST_LIST_INSERT_TAIL(frames, f, frame_list);
2906 /* Digit continues */
2907 rtp->dtmf_duration = new_duration;
2909 /* New digit began */
2911 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0));
2912 rtp->dtmf_duration = samples;
2913 AST_LIST_INSERT_TAIL(frames, f, frame_list);
2916 rtp->dtmf_timeout = timestamp + rtp->dtmf_duration + dtmftimeout;
2919 rtp->last_seqno = seqno;
2922 rtp->dtmfsamples = samples;
2927 static struct ast_frame *process_dtmf_cisco(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, struct ast_sockaddr *addr, int payloadtype, int mark)
2929 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2930 unsigned int event, flags, power;
2933 struct ast_frame *f = NULL;
2939 /* The format of Cisco RTP DTMF packet looks like next:
2940 +0 - sequence number of DTMF RTP packet (begins from 1,
2943 +1 (bit 0) - flaps by different DTMF digits delimited by audio
2944 or repeated digit without audio???
2945 +2 (+4,+6,...) - power level? (rises from 0 to 32 at begin of tone
2946 then falls to 0 at its end)
2947 +3 (+5,+7,...) - detected DTMF digit (0..9,*,#,A-D,...)
2948 Repeated DTMF information (bytes 4/5, 6/7) is history shifted right
2949 by each new packet and thus provides some redudancy.
2951 Sample of Cisco RTP DTMF packet is (all data in hex):
2952 19 07 00 02 12 02 20 02
2953 showing end of DTMF digit '2'.
2956 27 07 00 02 0A 02 20 02
2957 28 06 20 02 00 02 0A 02
2958 shows begin of new digit '2' with very short pause (20 ms) after
2959 previous digit '2'. Bit +1.0 flips at begin of new digit.
2961 Cisco RTP DTMF packets comes as replacement of audio RTP packets
2962 so its uses the same sequencing and timestamping rules as replaced
2963 audio packets. Repeat interval of DTMF packets is 20 ms and not rely
2964 on audio framing parameters. Marker bit isn't used within stream of
2965 DTMFs nor audio stream coming immediately after DTMF stream. Timestamps
2966 are not sequential at borders between DTMF and audio streams,
2972 event = data[3] & 0x1f;
2975 ast_debug(0, "Cisco DTMF Digit: %02x (len=%d, seq=%d, flags=%02x, power=%d, history count=%d)\n", event, len, seq, flags, power, (len - 4) / 2);
2978 } else if (event < 11) {
2980 } else if (event < 12) {
2982 } else if (event < 16) {
2983 resp = 'A' + (event - 12);
2984 } else if (event < 17) {
2987 if ((!rtp->resp && power) || (rtp->resp && (rtp->resp != resp))) {
2989 /* Why we should care on DTMF compensation at reception? */
2990 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) {
2991 f = create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0);
2992 rtp->dtmfsamples = 0;
2994 } else if ((rtp->resp == resp) && !power) {
2995 f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE));
2996 f->samples = rtp->dtmfsamples * (rtp->lastrxformat.id ? (rtp_get_rate(&rtp->lastrxformat) / 1000) : 8);
2998 } else if (rtp->resp == resp)
2999 rtp->dtmfsamples += 20 * (rtp->lastrxformat.id ? (rtp_get_rate(&rtp->lastrxformat) / 1000) : 8);
3001 rtp->dtmf_timeout = 0;
3006 static struct ast_frame *process_cn_rfc3389(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, struct ast_sockaddr *addr, int payloadtype, int mark)
3008 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3010 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
3011 totally help us out becuase we don't have an engine to keep it going and we are not
3012 guaranteed to have it every 20ms or anything */
3014 ast_debug(0, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", (int) rtp->lastrxformat.id, len);
3016 if (ast_test_flag(rtp, FLAG_3389_WARNING)) {
3017 struct ast_sockaddr remote_address = { {0,} };
3019 ast_rtp_instance_get_remote_address(instance, &remote_address);
3021 ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client address: %s\n",
3022 ast_sockaddr_stringify(&remote_address));
3023 ast_set_flag(rtp, FLAG_3389_WARNING);
3026 /* Must have at least one byte */
3030 rtp->f.data.ptr = rtp->rawdata + AST_FRIENDLY_OFFSET;
3031 rtp->f.datalen = len - 1;
3032 rtp->f.offset = AST_FRIENDLY_OFFSET;
3033 memcpy(rtp->f.data.ptr, data + 1, len - 1);
3035 rtp->f.data.ptr = NULL;
3039 rtp->f.frametype = AST_FRAME_CNG;
3040 rtp->f.subclass.integer = data[0] & 0x7f;
3042 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
3047 static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance)
3049 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3050 struct ast_sockaddr addr;
3051 unsigned char rtcpdata[8192 + AST_FRIENDLY_OFFSET];
3052 unsigned int *rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
3053 int res, packetwords, position = 0;
3054 struct ast_frame *f = &ast_null_frame;
3056 /* Read in RTCP data from the socket */
3057 if ((res = rtcp_recvfrom(instance, rtcpdata + AST_FRIENDLY_OFFSET,
3058 sizeof(rtcpdata) - AST_FRIENDLY_OFFSET,
3060 ast_assert(errno != EBADF);
3061 if (errno != EAGAIN) {
3062 ast_log(LOG_WARNING, "RTCP Read error: %s. Hanging up.\n",
3063 (errno) ? strerror(errno) : "Unspecified");
3066 return &ast_null_frame;
3069 /* If this was handled by the ICE session don't do anything further */
3071 return &ast_null_frame;
3074 if (!*(rtcpdata + AST_FRIENDLY_OFFSET)) {
3075 struct sockaddr_in addr_tmp;
3076 struct ast_sockaddr addr_v4;
3078 if (ast_sockaddr_is_ipv4(&addr)) {
3079 ast_sockaddr_to_sin(&addr, &addr_tmp);
3080 } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) {
3081 ast_debug(1, "Using IPv6 mapped address %s for STUN\n",
3082 ast_sockaddr_stringify(&addr));
3083 ast_sockaddr_to_sin(&addr_v4, &addr_tmp);
3085 ast_debug(1, "Cannot do STUN for non IPv4 address %s\n",
3086 ast_sockaddr_stringify(&addr));
3087 return &ast_null_frame;
3089 if ((ast_stun_handle_packet(rtp->rtcp->s, &addr_tmp, rtcpdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == AST_STUN_ACCEPT)) {
3090 ast_sockaddr_from_sin(&addr, &addr_tmp);
3091 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
3093 return &ast_null_frame;
3096 packetwords = res / 4;
3098 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT)) {
3099 /* Send to whoever sent to us */
3100 if (ast_sockaddr_cmp(&rtp->rtcp->them, &addr)) {
3101 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
3103 ast_debug(0, "RTCP NAT: Got RTCP from other end. Now sending to address %s\n",
3104 ast_sockaddr_stringify(&rtp->rtcp->them));
3108 ast_debug(1, "Got RTCP report of %d bytes\n", res);
3110 while (position < packetwords) {
3112 unsigned int length, dlsr, lsr, msw, lsw, comp;
3114 double rttsec, reported_jitter, reported_normdev_jitter_current, normdevrtt_current, reported_lost, reported_normdev_lost_current;
3118 length = ntohl(rtcpheader[i]);
3119 pt = (length & 0xff0000) >> 16;
3120 rc = (length & 0x1f000000) >> 24;
3123 if ((i + length) > packetwords) {
3125 ast_debug(1, "RTCP Read too short\n");
3126 return &ast_null_frame;
3129 if (rtcp_debug_test_addr(&addr)) {
3130 ast_verbose("\n\nGot RTCP from %s\n",
3131 ast_sockaddr_stringify(&addr));
3132 ast_verbose("PT: %d(%s)\n", pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown");
3133 ast_verbose("Reception reports: %d\n", rc);
3134 ast_verbose("SSRC of sender: %u\n", rtcpheader[i + 1]);
3137 i += 2; /* Advance past header and ssrc */
3138 if (rc == 0 && pt == RTCP_PT_RR) { /* We're receiving a receiver report with no reports, which is ok */
3139 position += (length + 1);
3145 gettimeofday(&rtp->rtcp->rxlsr,NULL); /* To be able to populate the dlsr */
3146 rtp->rtcp->spc = ntohl(rtcpheader[i+3]);
3147 rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
3148 rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16); /* Going to LSR in RR*/
3150 if (rtcp_debug_test_addr(&addr)) {
3151 ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader[i]), (unsigned long) ntohl(rtcpheader[i + 1]) * 4096);
3152 ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader[i + 2]));
3153 ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader[i + 3]), (unsigned long) ntohl(rtcpheader[i + 4]));
3158 /* Intentional fall through */
3160 /* Don't handle multiple reception reports (rc > 1) yet */
3161 /* Calculate RTT per RFC */
3162 gettimeofday(&now, NULL);
3163 timeval2ntp(now, &msw, &lsw);
3164 if (ntohl(rtcpheader[i + 4]) && ntohl(rtcpheader[i + 5])) { /* We must have the LSR && DLSR */
3165 comp = ((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16);
3166 lsr = ntohl(rtcpheader[i + 4]);
3167 dlsr = ntohl(rtcpheader[i + 5]);
3168 rtt = comp - lsr - dlsr;
3170 /* Convert end to end delay to usec (keeping the calculation in 64bit space)
3171 sess->ee_delay = (eedelay * 1000) / 65536; */
3173 rtt = (rtt * 1000000) >> 16;
3175 rtt = (rtt * 1000) >> 16;
3179 rttsec = rtt / 1000.;
3180 rtp->rtcp->rtt = rttsec;
3182 if (comp - dlsr >= lsr) {
3183 rtp->rtcp->accumulated_transit += rttsec;
3185 if (rtp->rtcp->rtt_count == 0)
3186 rtp->rtcp->minrtt = rttsec;
3188 if (rtp->rtcp->maxrtt<rttsec)
3189 rtp->rtcp->maxrtt = rttsec;
3190 if (rtp->rtcp->minrtt>rttsec)
3191 rtp->rtcp->minrtt = rttsec;
3193 normdevrtt_current = normdev_compute(rtp->rtcp->normdevrtt, rttsec, rtp->rtcp->rtt_count);
3195 rtp->rtcp->stdevrtt = stddev_compute(rtp->rtcp->stdevrtt, rttsec, rtp->rtcp->normdevrtt, normdevrtt_current, rtp->rtcp->rtt_count);
3197 rtp->rtcp->normdevrtt = normdevrtt_current;
3199 rtp->rtcp->rtt_count++;
3200 } else if (rtcp_debug_test_addr(&addr)) {
3201 ast_verbose("Internal RTCP NTP clock skew detected: "
3202 "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
3204 lsr, comp, dlsr, dlsr / 65536,
3205 (dlsr % 65536) * 1000 / 65536,
3206 dlsr - (comp - lsr));
3210 rtp->rtcp->reported_jitter = ntohl(rtcpheader[i + 3]);
3211 reported_jitter = (double) rtp->rtcp->reported_jitter;
3213 if (rtp->rtcp->reported_jitter_count == 0)
3214 rtp->rtcp->reported_minjitter = reported_jitter;
3216 if (reported_jitter < rtp->rtcp->reported_minjitter)
3217 rtp->rtcp->reported_minjitter = reported_jitter;
3219 if (reported_jitter > rtp->rtcp->reported_maxjitter)
3220 rtp->rtcp->reported_maxjitter = reported_jitter;
3222 reported_normdev_jitter_current = normdev_compute(rtp->rtcp->reported_normdev_jitter, reported_jitter, rtp->rtcp->reported_jitter_count);
3224 rtp->rtcp->reported_stdev_jitter = stddev_compute(rtp->rtcp->reported_stdev_jitter, reported_jitter, rtp->rtcp->reported_normdev_jitter, reported_normdev_jitter_current, rtp->rtcp->reported_jitter_count);
3226 rtp->rtcp->reported_normdev_jitter = reported_normdev_jitter_current;
3228 rtp->rtcp->reported_lost = ntohl(rtcpheader[i + 1]) & 0xffffff;
3230 reported_lost = (double) rtp->rtcp->reported_lost;
3232 /* using same counter as for jitter */
3233 if (rtp->rtcp->reported_jitter_count == 0)
3234 rtp->rtcp->reported_minlost = reported_lost;
3236 if (reported_lost < rtp->rtcp->reported_minlost)
3237 rtp->rtcp->reported_minlost = reported_lost;
3239 if (reported_lost > rtp->rtcp->reported_maxlost)
3240 rtp->rtcp->reported_maxlost = reported_lost;
3241 reported_normdev_lost_current = normdev_compute(rtp->rtcp->reported_normdev_lost, reported_lost, rtp->rtcp->reported_jitter_count);
3243 rtp->rtcp->reported_stdev_lost = stddev_compute(rtp->rtcp->reported_stdev_lost, reported_lost, rtp->rtcp->reported_normdev_lost, reported_normdev_lost_current, rtp->rtcp->reported_jitter_count);
3245 rtp->rtcp->reported_normdev_lost = reported_normdev_lost_current;
3247 rtp->rtcp->reported_jitter_count++;
3249 if (rtcp_debug_test_addr(&addr)) {
3250 ast_verbose(" Fraction lost: %ld\n", (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24));
3251 ast_verbose(" Packets lost so far: %d\n", rtp->rtcp->reported_lost);
3252 ast_verbose(" Highest sequence number: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff));
3253 ast_verbose(" Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader[i + 2])) >> 16);
3254 ast_verbose(" Interarrival jitter: %u\n", rtp->rtcp->reported_jitter);
3255 ast_verbose(" Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader[i + 4]) >> 16,((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096);
3256 ast_verbose(" DLSR: %4.4f (sec)\n",ntohl(rtcpheader[i + 5])/65536.0);
3258 ast_verbose(" RTT: %lu(sec)\n", (unsigned long) rtt);
3261 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s\r\n"
3263 "ReceptionReports: %d\r\n"
3264 "SenderSSRC: %u\r\n"
3265 "FractionLost: %ld\r\n"
3266 "PacketsLost: %d\r\n"
3267 "HighestSequence: %ld\r\n"
3268 "SequenceNumberCycles: %ld\r\n"
3270 "LastSR: %lu.%010lu\r\n"
3271 "DLSR: %4.4f(sec)\r\n"
3272 "RTT: %llu(sec)\r\n",
3273 ast_sockaddr_stringify(&addr),
3274 pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
3277 (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
3278 rtp->rtcp->reported_lost,
3279 (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
3280 (long) (ntohl(rtcpheader[i + 2])) >> 16,
3281 rtp->rtcp->reported_jitter,
3282 (unsigned long) ntohl(rtcpheader[i + 4]) >> 16, ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
3283 ntohl(rtcpheader[i + 5])/65536.0,
3284 (unsigned long long)rtt);
3286 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s\r\n"
3288 "ReceptionReports: %d\r\n"
3289 "SenderSSRC: %u\r\n"
3290 "FractionLost: %ld\r\n"
3291 "PacketsLost: %d\r\n"
3292 "HighestSequence: %ld\r\n"
3293 "SequenceNumberCycles: %ld\r\n"
3295 "LastSR: %lu.%010lu\r\n"
3296 "DLSR: %4.4f(sec)\r\n",
3297 ast_sockaddr_stringify(&addr),
3298 pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
3301 (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
3302 rtp->rtcp->reported_lost,
3303 (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
3304 (long) (ntohl(rtcpheader[i + 2])) >> 16,
3305 rtp->rtcp->reported_jitter,
3306 (unsigned long) ntohl(rtcpheader[i + 4]) >> 16,
3307 ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
3308 ntohl(rtcpheader[i + 5])/65536.0);
3312 if (rtcp_debug_test_addr(&addr))
3313 ast_verbose("Received an RTCP Fast Update Request\n");
3314 rtp->f.frametype = AST_FRAME_CONTROL;
3315 rtp->f.subclass.integer = AST_CONTROL_VIDUPDATE;
3323 if (rtcp_debug_test_addr(&addr))
3324 ast_verbose("Received an SDES from %s\n",
3325 ast_sockaddr_stringify(&rtp->rtcp->them));
3328 if (rtcp_debug_test_addr(&addr))
3329 ast_verbose("Received a BYE from %s\n",
3330 ast_sockaddr_stringify(&rtp->rtcp->them));
3333 ast_debug(1, "Unknown RTCP packet (pt=%d) received from %s\n",
3334 pt, ast_sockaddr_stringify(&rtp->rtcp->them));
3337 position += (length + 1);
3340 rtp->rtcp->rtcp_info = 1;
3345 static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, unsigned int *rtpheader, int len, int hdrlen)
3347 struct ast_rtp_instance *instance1 = ast_rtp_instance_get_bridged(instance);
3348 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance), *bridged = ast_rtp_instance_get_data(instance1);
3349 int res = 0, payload = 0, bridged_payload = 0, mark;
3350 struct ast_rtp_payload_type payload_type;
3351 int reconstruct = ntohl(rtpheader[0]);
3352 struct ast_sockaddr remote_address = { {0,} };
3355 /* Get fields from packet */
3356 payload = (reconstruct & 0x7f0000) >> 16;
3357 mark = (((reconstruct & 0x800000) >> 23) != 0);
3359 /* Check what the payload value should be */
3360 payload_type = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(instance), payload);
3362 /* Otherwise adjust bridged payload to match */
3363 bridged_payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance1), payload_type.asterisk_format, &payload_type.format, payload_type.rtp_code);
3365 /* If no codec could be matched between instance and instance1, then somehow things were made incompatible while we were still bridged. Bail. */
3366 if (bridged_payload < 0) {
3370 /* If the payload coming in is not one of the negotiated ones then send it to the core, this will cause formats to change and the bridge to break */
3371 if (ast_rtp_codecs_find_payload_code(ast_rtp_instance_get_codecs(instance1), bridged_payload) == -1) {
3372 ast_debug(1, "Unsupported payload type received \n");
3376 /* If the marker bit has been explicitly set turn it on */
3377 if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
3379 ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT);
3382 /* Reconstruct part of the packet */
3383 reconstruct &= 0xFF80FFFF;
3384 reconstruct |= (bridged_payload << 16);
3385 reconstruct |= (mark << 23);
3386 rtpheader[0] = htonl(reconstruct);
3388 ast_rtp_instance_get_remote_address(instance1, &remote_address);
3390 if (ast_sockaddr_isnull(&remote_address)) {
3391 ast_debug(1, "Remote address is null, most likely RTP has been stopped\n");
3395 /* Send the packet back out */
3396 res = rtp_sendto(instance1, (void *)rtpheader, len, 0, &remote_address, &ice);
3398 if (!ast_rtp_instance_get_prop(instance1, AST_RTP_PROPERTY_NAT) || (ast_rtp_instance_get_prop(instance1, AST_RTP_PROPERTY_NAT) && (ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
3399 ast_log(LOG_WARNING,
3400 "RTP Transmission error of packet to %s: %s\n",
3401 ast_sockaddr_stringify(&remote_address),
3403 } else if (((ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(bridged, FLAG_NAT_INACTIVE_NOWARN)) {
3404 if (option_debug || rtpdebug)
3405 ast_log(LOG_WARNING,
3406 "RTP NAT: Can't write RTP to private "
3407 "address %s, waiting for other end to "
3409 ast_sockaddr_stringify(&remote_address));
3410 ast_set_flag(bridged, FLAG_NAT_INACTIVE_NOWARN);
3415 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
3417 if (rtp_debug_test_addr(&remote_address)) {
3418 ast_verbose("Sent RTP P2P packet to %s%s (type %-2.2d, len %-6.6u)\n",
3419 ast_sockaddr_stringify(&remote_address),
3420 ice ? " (via ICE)" : "",
3421 bridged_payload, len - hdrlen);
3427 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp)
3429 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3430 struct ast_sockaddr addr;
3431 int res, hdrlen = 12, version, payloadtype, padding, mark, ext, cc, prev_seqno;
3432 unsigned int *rtpheader = (unsigned int*)(rtp->rawdata + AST_FRIENDLY_OFFSET), seqno, ssrc, timestamp;
3433 struct ast_rtp_payload_type payload;
3434 struct ast_sockaddr remote_address = { {0,} };
3435 struct frame_list frames;
3437 /* If this is actually RTCP let's hop on over and handle it */
3440 return ast_rtcp_read(instance);
3442 return &ast_null_frame;
3445 /* If we are currently sending DTMF to the remote party send a continuation packet */
3446 if (rtp->sending_digit) {
3447 ast_rtp_dtmf_continuation(instance);
3450 /* Actually read in the data from the socket */
3451 if ((res = rtp_recvfrom(instance, rtp->rawdata + AST_FRIENDLY_OFFSET,
3452 sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET, 0,
3454 ast_assert(errno != EBADF);
3455 if (errno != EAGAIN) {
3456 ast_log(LOG_WARNING, "RTP Read error: %s. Hanging up.\n",
3457 (errno) ? strerror(errno) : "Unspecified");
3460 return &ast_null_frame;
3463 /* If this was handled by the ICE session don't do anything */
3465 return &ast_null_frame;
3468 /* Make sure the data that was read in is actually enough to make up an RTP packet */
3470 ast_log(LOG_WARNING, "RTP Read too short\n");
3471 return &ast_null_frame;
3474 /* Get fields and verify this is an RTP packet */
3475 seqno = ntohl(rtpheader[0]);
3477 ast_rtp_instance_get_remote_address(instance, &remote_address);
3479 if (!(version = (seqno & 0xC0000000) >> 30)) {
3480 struct sockaddr_in addr_tmp;
3481 struct ast_sockaddr addr_v4;
3482 if (ast_sockaddr_is_ipv4(&addr)) {
3483 ast_sockaddr_to_sin(&addr, &addr_tmp);
3484 } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) {
3485 ast_debug(1, "Using IPv6 mapped address %s for STUN\n",
3486 ast_sockaddr_stringify(&addr));
3487 ast_sockaddr_to_sin(&addr_v4, &addr_tmp);
3489 ast_debug(1, "Cannot do STUN for non IPv4 address %s\n",
3490 ast_sockaddr_stringify(&addr));
3491 return &ast_null_frame;
3493 if ((ast_stun_handle_packet(rtp->s, &addr_tmp, rtp->rawdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == AST_STUN_ACCEPT) &&
3494 ast_sockaddr_isnull(&remote_address)) {
3495 ast_sockaddr_from_sin(&addr, &addr_tmp);
3496 ast_rtp_instance_set_remote_address(instance, &addr);
3498 return &ast_null_frame;
3501 /* If strict RTP protection is enabled see if we need to learn the remote address or if we need to drop the packet */
3502 if (rtp->strict_rtp_state == STRICT_RTP_LEARN) {
3503 ast_debug(1, "%p -- start learning mode pass with addr = %s\n", rtp, ast_sockaddr_stringify(&addr));
3504 /* For now, we always copy the address. */
3505 ast_sockaddr_copy(&rtp->strict_rtp_address, &addr);
3507 /* Send the rtp and the seqno from header to rtp_learning_rtp_seq_upda