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 <use type="external">pjproject</use>
33 <support_level>core</support_level>
38 ASTERISK_REGISTER_FILE()
44 #ifdef HAVE_OPENSSL_SRTP
45 #include <openssl/ssl.h>
46 #include <openssl/err.h>
47 #include <openssl/bio.h>
52 #include <pjlib-util.h>
56 #include "asterisk/stun.h"
57 #include "asterisk/pbx.h"
58 #include "asterisk/frame.h"
59 #include "asterisk/format_cache.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"
70 #include "asterisk/smoother.h"
71 #include "asterisk/test.h"
73 #define MAX_TIMESTAMP_SKEW 640
75 #define RTP_SEQ_MOD (1<<16) /*!< A sequence number can't be more than 16 bits */
76 #define RTCP_DEFAULT_INTERVALMS 5000 /*!< Default milli-seconds between RTCP reports we send */
77 #define RTCP_MIN_INTERVALMS 500 /*!< Min milli-seconds between RTCP reports we send */
78 #define RTCP_MAX_INTERVALMS 60000 /*!< Max milli-seconds between RTCP reports we send */
80 #define DEFAULT_RTP_START 5000 /*!< Default port number to start allocating RTP ports from */
81 #define DEFAULT_RTP_END 31000 /*!< Default maximum port number to end allocating RTP ports at */
83 #define MINIMUM_RTP_PORT 1024 /*!< Minimum port number to accept */
84 #define MAXIMUM_RTP_PORT 65535 /*!< Maximum port number to accept */
86 #define DEFAULT_TURN_PORT 3478
88 #define TURN_STATE_WAIT_TIME 2000
90 #define RTCP_PT_FUR 192
91 #define RTCP_PT_SR AST_RTP_RTCP_SR
92 #define RTCP_PT_RR AST_RTP_RTCP_RR
93 #define RTCP_PT_SDES 202
94 #define RTCP_PT_BYE 203
95 #define RTCP_PT_APP 204
96 /* VP8: RTCP Feedback */
97 #define RTCP_PT_PSFB 206
100 #define DTMF_SAMPLE_RATE_MS 8 /*!< DTMF samples per millisecond */
102 #define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000)) /*!< samples */
104 #define ZFONE_PROFILE_ID 0x505a
106 #define DEFAULT_LEARNING_MIN_SEQUENTIAL 4
108 #define SRTP_MASTER_KEY_LEN 16
109 #define SRTP_MASTER_SALT_LEN 14
110 #define SRTP_MASTER_LEN (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN)
112 enum strict_rtp_state {
113 STRICT_RTP_OPEN = 0, /*! No RTP packets should be dropped, all sources accepted */
114 STRICT_RTP_LEARN, /*! Accept next packet as source */
115 STRICT_RTP_CLOSED, /*! Drop all RTP packets not coming from source that was learned */
118 #define DEFAULT_STRICT_RTP STRICT_RTP_CLOSED
119 #define DEFAULT_ICESUPPORT 1
121 extern struct ast_srtp_res *res_srtp;
122 extern struct ast_srtp_policy_res *res_srtp_policy;
124 static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
126 static int rtpstart = DEFAULT_RTP_START; /*!< First port for RTP sessions (set in rtp.conf) */
127 static int rtpend = DEFAULT_RTP_END; /*!< Last port for RTP sessions (set in rtp.conf) */
128 static int rtpdebug; /*!< Are we debugging? */
129 static int rtcpdebug; /*!< Are we debugging RTCP? */
130 static int rtcpstats; /*!< Are we debugging RTCP? */
131 static int rtcpinterval = RTCP_DEFAULT_INTERVALMS; /*!< Time between rtcp reports in millisecs */
132 static struct ast_sockaddr rtpdebugaddr; /*!< Debug packets to/from this host */
133 static struct ast_sockaddr rtcpdebugaddr; /*!< Debug RTCP packets to/from this host */
134 static int rtpdebugport; /*< Debug only RTP packets from IP or IP+Port if port is > 0 */
135 static int rtcpdebugport; /*< Debug only RTCP packets from IP or IP+Port if port is > 0 */
137 static int nochecksums;
139 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. */
140 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. */
141 #ifdef HAVE_PJPROJECT
142 static int icesupport = DEFAULT_ICESUPPORT;
143 static struct sockaddr_in stunaddr;
144 static pj_str_t turnaddr;
145 static int turnport = DEFAULT_TURN_PORT;
146 static pj_str_t turnusername;
147 static pj_str_t turnpassword;
149 /*! \brief Pool factory used by pjlib to allocate memory. */
150 static pj_caching_pool cachingpool;
152 /*! \brief Global memory pool for configuration and timers */
153 static pj_pool_t *pool;
155 /*! \brief Global timer heap */
156 static pj_timer_heap_t *timer_heap;
158 /*! \brief Thread executing the timer heap */
159 static pj_thread_t *timer_thread;
161 /*! \brief Used to tell the timer thread to terminate */
162 static int timer_terminate;
164 /*! \brief Structure which contains ioqueue thread information */
165 struct ast_rtp_ioqueue_thread {
166 /*! \brief Pool used by the thread */
168 /*! \brief The thread handling the queue and timer heap */
170 /*! \brief Ioqueue which polls on sockets */
171 pj_ioqueue_t *ioqueue;
172 /*! \brief Timer heap for scheduled items */
173 pj_timer_heap_t *timerheap;
174 /*! \brief Termination request */
176 /*! \brief Current number of descriptors being waited on */
178 /*! \brief Linked list information */
179 AST_LIST_ENTRY(ast_rtp_ioqueue_thread) next;
182 /*! \brief List of ioqueue threads */
183 static AST_LIST_HEAD_STATIC(ioqueues, ast_rtp_ioqueue_thread);
187 #define FLAG_3389_WARNING (1 << 0)
188 #define FLAG_NAT_ACTIVE (3 << 1)
189 #define FLAG_NAT_INACTIVE (0 << 1)
190 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
191 #define FLAG_NEED_MARKER_BIT (1 << 3)
192 #define FLAG_DTMF_COMPENSATE (1 << 4)
194 #define TRANSPORT_SOCKET_RTP 0
195 #define TRANSPORT_SOCKET_RTCP 1
196 #define TRANSPORT_TURN_RTP 2
197 #define TRANSPORT_TURN_RTCP 3
199 /*! \brief RTP learning mode tracking information */
200 struct rtp_learning_info {
201 int max_seq; /*!< The highest sequence number received */
202 int packets; /*!< The number of remaining packets before the source is accepted */
205 #ifdef HAVE_OPENSSL_SRTP
206 struct dtls_details {
207 ast_mutex_t lock; /*!< Lock for timeout timer synchronization */
208 SSL *ssl; /*!< SSL session */
209 BIO *read_bio; /*!< Memory buffer for reading */
210 BIO *write_bio; /*!< Memory buffer for writing */
211 enum ast_rtp_dtls_setup dtls_setup; /*!< Current setup state */
212 enum ast_rtp_dtls_connection connection; /*!< Whether this is a new or existing connection */
213 int timeout_timer; /*!< Scheduler id for timeout timer */
217 /*! \brief RTP session description */
221 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
222 unsigned int ssrc; /*!< Synchronization source, RFC 3550, page 10. */
223 unsigned int themssrc; /*!< Their SSRC */
226 unsigned int lastrxts;
227 unsigned int lastividtimestamp;
228 unsigned int lastovidtimestamp;
229 unsigned int lastitexttimestamp;
230 unsigned int lastotexttimestamp;
231 unsigned int lasteventseqn;
232 int lastrxseqno; /*!< Last received sequence number */
233 unsigned short seedrxseqno; /*!< What sequence number did they start with?*/
234 unsigned int seedrxts; /*!< What RTP timestamp did they start with? */
235 unsigned int rxcount; /*!< How many packets have we received? */
236 unsigned int rxoctetcount; /*!< How many octets have we received? should be rxcount *160*/
237 unsigned int txcount; /*!< How many packets have we sent? */
238 unsigned int txoctetcount; /*!< How many octets have we sent? (txcount*160)*/
239 unsigned int cycles; /*!< Shifted count of sequence number cycles */
240 double rxjitter; /*!< Interarrival jitter at the moment in seconds */
241 double rxtransit; /*!< Relative transit time for previous packet */
242 struct ast_format *lasttxformat;
243 struct ast_format *lastrxformat;
245 int rtptimeout; /*!< RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
246 int rtpholdtimeout; /*!< RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
247 int rtpkeepalive; /*!< Send RTP comfort noice packets for keepalive */
249 /* DTMF Reception Variables */
250 char resp; /*!< The current digit being processed */
251 unsigned int last_seqno; /*!< The last known sequence number for any DTMF packet */
252 unsigned int last_end_timestamp; /*!< The last known timestamp received from an END packet */
253 unsigned int dtmf_duration; /*!< Total duration in samples since the digit start event */
254 unsigned int dtmf_timeout; /*!< When this timestamp is reached we consider END frame lost and forcibly abort digit */
255 unsigned int dtmfsamples;
256 enum ast_rtp_dtmf_mode dtmfmode; /*!< The current DTMF mode of the RTP stream */
257 /* DTMF Transmission Variables */
258 unsigned int lastdigitts;
259 char sending_digit; /*!< boolean - are we sending digits */
260 char send_digit; /*!< digit we are sending */
264 struct timeval rxcore;
265 struct timeval txcore;
266 double drxcore; /*!< The double representation of the first received packet */
267 struct timeval lastrx; /*!< timeval when we last received a packet */
268 struct timeval dtmfmute;
269 struct ast_smoother *smoother;
271 unsigned short seqno; /*!< Sequence number, RFC 3550, page 13. */
272 unsigned short rxseqno;
273 struct ast_sched_context *sched;
274 struct io_context *io;
276 struct ast_rtcp *rtcp;
277 struct ast_rtp *bridged; /*!< Who we are Packet bridged to */
279 enum strict_rtp_state strict_rtp_state; /*!< Current state that strict RTP protection is in */
280 struct ast_sockaddr strict_rtp_address; /*!< Remote address information for strict RTP purposes */
283 * Learning mode values based on pjmedia's probation mode. Many of these values are redundant to the above,
284 * but these are in place to keep learning mode sequence values sealed from their normal counterparts.
286 struct rtp_learning_info rtp_source_learn; /* Learning mode track for the expected RTP source */
287 struct rtp_learning_info alt_source_learn; /* Learning mode tracking for a new RTP source after one has been chosen */
291 ast_mutex_t lock; /*!< Lock for synchronization purposes */
292 ast_cond_t cond; /*!< Condition for signaling */
294 #ifdef HAVE_PJPROJECT
295 pj_ice_sess *ice; /*!< ICE session */
296 pj_turn_sock *turn_rtp; /*!< RTP TURN relay */
297 pj_turn_sock *turn_rtcp; /*!< RTCP TURN relay */
298 pj_turn_state_t turn_state; /*!< Current state of the TURN relay session */
299 unsigned int passthrough:1; /*!< Bit to indicate that the received packet should be passed through */
300 unsigned int rtp_passthrough:1; /*!< Bit to indicate that TURN RTP should be passed through */
301 unsigned int rtcp_passthrough:1; /*!< Bit to indicate that TURN RTCP should be passed through */
302 unsigned int ice_port; /*!< Port that ICE was started with if it was previously started */
303 struct ast_sockaddr rtp_loop; /*!< Loopback address for forwarding RTP from TURN */
304 struct ast_sockaddr rtcp_loop; /*!< Loopback address for forwarding RTCP from TURN */
306 struct ast_rtp_ioqueue_thread *ioqueue; /*!< The ioqueue thread handling us */
308 char remote_ufrag[256]; /*!< The remote ICE username */
309 char remote_passwd[256]; /*!< The remote ICE password */
311 char local_ufrag[256]; /*!< The local ICE username */
312 char local_passwd[256]; /*!< The local ICE password */
314 struct ao2_container *ice_local_candidates; /*!< The local ICE candidates */
315 struct ao2_container *ice_active_remote_candidates; /*!< The remote ICE candidates */
316 struct ao2_container *ice_proposed_remote_candidates; /*!< Incoming remote ICE candidates for new session */
317 struct ast_sockaddr ice_original_rtp_addr; /*!< rtp address that ICE started on first session */
320 #ifdef HAVE_OPENSSL_SRTP
321 SSL_CTX *ssl_ctx; /*!< SSL context */
322 enum ast_rtp_dtls_verify dtls_verify; /*!< What to verify */
323 enum ast_srtp_suite suite; /*!< SRTP crypto suite */
324 enum ast_rtp_dtls_hash local_hash; /*!< Local hash used for the fingerprint */
325 char local_fingerprint[160]; /*!< Fingerprint of our certificate */
326 enum ast_rtp_dtls_hash remote_hash; /*!< Remote hash used for the fingerprint */
327 unsigned char remote_fingerprint[EVP_MAX_MD_SIZE]; /*!< Fingerprint of the peer certificate */
328 unsigned int rekey; /*!< Interval at which to renegotiate and rekey */
329 int rekeyid; /*!< Scheduled item id for rekeying */
330 struct dtls_details dtls; /*!< DTLS state information */
335 * \brief Structure defining an RTCP session.
337 * The concept "RTCP session" is not defined in RFC 3550, but since
338 * this structure is analogous to ast_rtp, which tracks a RTP session,
339 * it is logical to think of this as a RTCP session.
341 * RTCP packet is defined on page 9 of RFC 3550.
346 int s; /*!< Socket */
347 struct ast_sockaddr us; /*!< Socket representation of the local endpoint. */
348 struct ast_sockaddr them; /*!< Socket representation of the remote endpoint. */
349 unsigned int soc; /*!< What they told us */
350 unsigned int spc; /*!< What they told us */
351 unsigned int themrxlsr; /*!< The middle 32 bits of the NTP timestamp in the last received SR*/
352 struct timeval rxlsr; /*!< Time when we got their last SR */
353 struct timeval txlsr; /*!< Time when we sent or last SR*/
354 unsigned int expected_prior; /*!< no. packets in previous interval */
355 unsigned int received_prior; /*!< no. packets received in previous interval */
356 int schedid; /*!< Schedid returned from ast_sched_add() to schedule RTCP-transmissions*/
357 unsigned int rr_count; /*!< number of RRs we've sent, not including report blocks in SR's */
358 unsigned int sr_count; /*!< number of SRs we've sent */
359 unsigned int lastsrtxcount; /*!< Transmit packet count when last SR sent */
360 double accumulated_transit; /*!< accumulated a-dlsr-lsr */
361 double rtt; /*!< Last reported rtt */
362 unsigned int reported_jitter; /*!< The contents of their last jitter entry in the RR */
363 unsigned int reported_lost; /*!< Reported lost packets in their RR */
365 double reported_maxjitter;
366 double reported_minjitter;
367 double reported_normdev_jitter;
368 double reported_stdev_jitter;
369 unsigned int reported_jitter_count;
371 double reported_maxlost;
372 double reported_minlost;
373 double reported_normdev_lost;
374 double reported_stdev_lost;
379 double normdev_rxlost;
381 unsigned int rxlost_count;
385 double normdev_rxjitter;
386 double stdev_rxjitter;
387 unsigned int rxjitter_count;
392 unsigned int rtt_count;
394 /* VP8: sequence number for the RTCP FIR FCI */
397 #ifdef HAVE_OPENSSL_SRTP
398 struct dtls_details dtls; /*!< DTLS state information */
403 struct ast_frame t140; /*!< Primary data */
404 struct ast_frame t140red; /*!< Redundant t140*/
405 unsigned char pt[AST_RED_MAX_GENERATION]; /*!< Payload types for redundancy data */
406 unsigned char ts[AST_RED_MAX_GENERATION]; /*!< Time stamps */
407 unsigned char len[AST_RED_MAX_GENERATION]; /*!< length of each generation */
408 int num_gen; /*!< Number of generations */
409 int schedid; /*!< Timer id */
410 int ti; /*!< How long to buffer data before send */
411 unsigned char t140red_data[64000];
412 unsigned char buf_data[64000]; /*!< buffered primary data */
417 AST_LIST_HEAD_NOLOCK(frame_list, ast_frame);
419 /* Forward Declarations */
420 static int ast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data);
421 static int ast_rtp_destroy(struct ast_rtp_instance *instance);
422 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit);
423 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit);
424 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration);
425 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode);
426 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance);
427 static void ast_rtp_update_source(struct ast_rtp_instance *instance);
428 static void ast_rtp_change_source(struct ast_rtp_instance *instance);
429 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame);
430 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp);
431 static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value);
432 static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp);
433 static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
434 static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations);
435 static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame);
436 static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1);
437 static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat);
438 static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1);
439 static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username);
440 static void ast_rtp_stop(struct ast_rtp_instance *instance);
441 static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char* desc);
442 static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level);
444 #ifdef HAVE_OPENSSL_SRTP
445 static int ast_rtp_activate(struct ast_rtp_instance *instance);
446 static void dtls_srtp_check_pending(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp);
447 static void dtls_srtp_start_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp);
448 static void dtls_srtp_stop_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp);
451 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);
453 #ifdef HAVE_PJPROJECT
454 /*! \brief Helper function which updates an ast_sockaddr with the candidate used for the component */
455 static void update_address_with_ice_candidate(struct ast_rtp *rtp, enum ast_rtp_ice_component_type component,
456 struct ast_sockaddr *cand_address)
458 char address[PJ_INET6_ADDRSTRLEN];
460 if (!rtp->ice || (component < 1) || !rtp->ice->comp[component - 1].valid_check) {
464 ast_sockaddr_parse(cand_address, pj_sockaddr_print(&rtp->ice->comp[component - 1].valid_check->rcand->addr, address, sizeof(address), 0), 0);
465 ast_sockaddr_set_port(cand_address, pj_sockaddr_get_port(&rtp->ice->comp[component - 1].valid_check->rcand->addr));
468 /*! \brief Destructor for locally created ICE candidates */
469 static void ast_rtp_ice_candidate_destroy(void *obj)
471 struct ast_rtp_engine_ice_candidate *candidate = obj;
473 if (candidate->foundation) {
474 ast_free(candidate->foundation);
477 if (candidate->transport) {
478 ast_free(candidate->transport);
482 static void ast_rtp_ice_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
484 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
486 if (!ast_strlen_zero(ufrag)) {
487 ast_copy_string(rtp->remote_ufrag, ufrag, sizeof(rtp->remote_ufrag));
490 if (!ast_strlen_zero(password)) {
491 ast_copy_string(rtp->remote_passwd, password, sizeof(rtp->remote_passwd));
495 static int ice_candidate_cmp(void *obj, void *arg, int flags)
497 struct ast_rtp_engine_ice_candidate *candidate1 = obj, *candidate2 = arg;
499 if (strcmp(candidate1->foundation, candidate2->foundation) ||
500 candidate1->id != candidate2->id ||
501 ast_sockaddr_cmp(&candidate1->address, &candidate2->address) ||
502 candidate1->type != candidate1->type) {
506 return CMP_MATCH | CMP_STOP;
509 static void ast_rtp_ice_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
511 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
512 struct ast_rtp_engine_ice_candidate *remote_candidate;
514 /* ICE sessions only support UDP candidates */
515 if (strcasecmp(candidate->transport, "udp")) {
519 if (!rtp->ice_proposed_remote_candidates &&
520 !(rtp->ice_proposed_remote_candidates = ao2_container_alloc(1, NULL, ice_candidate_cmp))) {
524 /* If this is going to exceed the maximum number of ICE candidates don't even add it */
525 if (ao2_container_count(rtp->ice_proposed_remote_candidates) == PJ_ICE_MAX_CAND) {
529 if (!(remote_candidate = ao2_alloc(sizeof(*remote_candidate), ast_rtp_ice_candidate_destroy))) {
533 remote_candidate->foundation = ast_strdup(candidate->foundation);
534 remote_candidate->id = candidate->id;
535 remote_candidate->transport = ast_strdup(candidate->transport);
536 remote_candidate->priority = candidate->priority;
537 ast_sockaddr_copy(&remote_candidate->address, &candidate->address);
538 ast_sockaddr_copy(&remote_candidate->relay_address, &candidate->relay_address);
539 remote_candidate->type = candidate->type;
541 ao2_link(rtp->ice_proposed_remote_candidates, remote_candidate);
542 ao2_ref(remote_candidate, -1);
545 AST_THREADSTORAGE(pj_thread_storage);
547 /*! \brief Function used to check if the calling thread is registered with pjlib. If it is not it will be registered. */
548 static void pj_thread_register_check(void)
550 pj_thread_desc *desc;
553 if (pj_thread_is_registered() == PJ_TRUE) {
557 desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc));
559 ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage. Expect awful things to occur\n");
562 pj_bzero(*desc, sizeof(*desc));
564 if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
565 ast_log(LOG_ERROR, "Coudln't register thread with PJLIB.\n");
570 static int ice_create(struct ast_rtp_instance *instance, struct ast_sockaddr *addr,
571 int port, int replace);
573 static void ast_rtp_ice_stop(struct ast_rtp_instance *instance)
575 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
581 pj_thread_register_check();
583 pj_ice_sess_destroy(rtp->ice);
587 static int ice_reset_session(struct ast_rtp_instance *instance)
589 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
590 pj_ice_sess_role role = rtp->ice->role;
593 if (!rtp->ice->is_nominating && !rtp->ice->is_complete) {
597 ast_rtp_ice_stop(instance);
599 res = ice_create(instance, &rtp->ice_original_rtp_addr, rtp->ice_port, 1);
601 /* Preserve the role that the old ICE session used */
602 pj_ice_sess_change_role(rtp->ice, role);
608 static int ice_candidates_compare(struct ao2_container *left, struct ao2_container *right)
610 struct ao2_iterator i;
611 struct ast_rtp_engine_ice_candidate *right_candidate;
613 if (ao2_container_count(left) != ao2_container_count(right)) {
617 i = ao2_iterator_init(right, 0);
618 while ((right_candidate = ao2_iterator_next(&i))) {
619 struct ast_rtp_engine_ice_candidate *left_candidate = ao2_find(left, right_candidate, OBJ_POINTER);
621 if (!left_candidate) {
622 ao2_ref(right_candidate, -1);
623 ao2_iterator_destroy(&i);
627 ao2_ref(left_candidate, -1);
628 ao2_ref(right_candidate, -1);
630 ao2_iterator_destroy(&i);
635 static void ast_rtp_ice_start(struct ast_rtp_instance *instance)
637 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
638 pj_str_t ufrag = pj_str(rtp->remote_ufrag), passwd = pj_str(rtp->remote_passwd);
639 pj_ice_sess_cand candidates[PJ_ICE_MAX_CAND];
640 struct ao2_iterator i;
641 struct ast_rtp_engine_ice_candidate *candidate;
642 int cand_cnt = 0, has_rtp = 0, has_rtcp = 0;
644 if (!rtp->ice || !rtp->ice_proposed_remote_candidates) {
648 /* Check for equivalence in the lists */
649 if (rtp->ice_active_remote_candidates &&
650 !ice_candidates_compare(rtp->ice_proposed_remote_candidates, rtp->ice_active_remote_candidates)) {
651 ao2_cleanup(rtp->ice_proposed_remote_candidates);
652 rtp->ice_proposed_remote_candidates = NULL;
656 /* Out with the old, in with the new */
657 ao2_cleanup(rtp->ice_active_remote_candidates);
658 rtp->ice_active_remote_candidates = rtp->ice_proposed_remote_candidates;
659 rtp->ice_proposed_remote_candidates = NULL;
661 /* Reset the ICE session. Is this going to work? */
662 if (ice_reset_session(instance)) {
663 ast_log(LOG_NOTICE, "Failed to create replacement ICE session\n");
667 pj_thread_register_check();
669 i = ao2_iterator_init(rtp->ice_active_remote_candidates, 0);
671 while ((candidate = ao2_iterator_next(&i)) && (cand_cnt < PJ_ICE_MAX_CAND)) {
674 /* there needs to be at least one rtp and rtcp candidate in the list */
675 has_rtp |= candidate->id == AST_RTP_ICE_COMPONENT_RTP;
676 has_rtcp |= candidate->id == AST_RTP_ICE_COMPONENT_RTCP;
678 pj_strdup2(rtp->ice->pool, &candidates[cand_cnt].foundation, candidate->foundation);
679 candidates[cand_cnt].comp_id = candidate->id;
680 candidates[cand_cnt].prio = candidate->priority;
682 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->address)), &candidates[cand_cnt].addr);
684 if (!ast_sockaddr_isnull(&candidate->relay_address)) {
685 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->relay_address)), &candidates[cand_cnt].rel_addr);
688 if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
689 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_HOST;
690 } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
691 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_SRFLX;
692 } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_RELAYED) {
693 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_RELAYED;
696 if (candidate->id == AST_RTP_ICE_COMPONENT_RTP && rtp->turn_rtp) {
697 pj_turn_sock_set_perm(rtp->turn_rtp, 1, &candidates[cand_cnt].addr, 1);
698 } else if (candidate->id == AST_RTP_ICE_COMPONENT_RTCP && rtp->turn_rtcp) {
699 pj_turn_sock_set_perm(rtp->turn_rtcp, 1, &candidates[cand_cnt].addr, 1);
703 ao2_ref(candidate, -1);
706 ao2_iterator_destroy(&i);
708 if (has_rtp && has_rtcp &&
709 pj_ice_sess_create_check_list(rtp->ice, &ufrag, &passwd, cand_cnt, &candidates[0]) == PJ_SUCCESS) {
710 ast_test_suite_event_notify("ICECHECKLISTCREATE", "Result: SUCCESS");
711 pj_ice_sess_start_check(rtp->ice);
712 pj_timer_heap_poll(timer_heap, NULL);
713 rtp->strict_rtp_state = STRICT_RTP_OPEN;
717 ast_test_suite_event_notify("ICECHECKLISTCREATE", "Result: FAILURE");
719 /* even though create check list failed don't stop ice as
720 it might still work */
721 ast_debug(1, "Failed to create ICE session check list\n");
722 /* however we do need to reset remote candidates since
723 this function may be re-entered */
724 ao2_ref(rtp->ice_active_remote_candidates, -1);
725 rtp->ice_active_remote_candidates = NULL;
726 rtp->ice->rcand_cnt = rtp->ice->clist.count = 0;
729 static const char *ast_rtp_ice_get_ufrag(struct ast_rtp_instance *instance)
731 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
733 return rtp->local_ufrag;
736 static const char *ast_rtp_ice_get_password(struct ast_rtp_instance *instance)
738 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
740 return rtp->local_passwd;
743 static struct ao2_container *ast_rtp_ice_get_local_candidates(struct ast_rtp_instance *instance)
745 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
747 if (rtp->ice_local_candidates) {
748 ao2_ref(rtp->ice_local_candidates, +1);
751 return rtp->ice_local_candidates;
754 static void ast_rtp_ice_lite(struct ast_rtp_instance *instance)
756 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
762 pj_thread_register_check();
764 pj_ice_sess_change_role(rtp->ice, PJ_ICE_SESS_ROLE_CONTROLLING);
767 static void ast_rtp_ice_set_role(struct ast_rtp_instance *instance, enum ast_rtp_ice_role role)
769 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
775 pj_thread_register_check();
777 pj_ice_sess_change_role(rtp->ice, role == AST_RTP_ICE_ROLE_CONTROLLED ?
778 PJ_ICE_SESS_ROLE_CONTROLLED : PJ_ICE_SESS_ROLE_CONTROLLING);
781 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,
782 const pj_sockaddr_t *addr, const pj_sockaddr_t *base_addr, const pj_sockaddr_t *rel_addr, int addr_len)
785 struct ast_rtp_engine_ice_candidate *candidate, *existing;
786 char address[PJ_INET6_ADDRSTRLEN];
788 pj_thread_register_check();
790 pj_ice_calc_foundation(rtp->ice->pool, &foundation, type, addr);
792 if (!rtp->ice_local_candidates && !(rtp->ice_local_candidates = ao2_container_alloc(1, NULL, ice_candidate_cmp))) {
796 if (!(candidate = ao2_alloc(sizeof(*candidate), ast_rtp_ice_candidate_destroy))) {
800 candidate->foundation = ast_strndup(pj_strbuf(&foundation), pj_strlen(&foundation));
801 candidate->id = comp_id;
802 candidate->transport = ast_strdup("UDP");
804 ast_sockaddr_parse(&candidate->address, pj_sockaddr_print(addr, address, sizeof(address), 0), 0);
805 ast_sockaddr_set_port(&candidate->address, pj_sockaddr_get_port(addr));
808 ast_sockaddr_parse(&candidate->relay_address, pj_sockaddr_print(rel_addr, address, sizeof(address), 0), 0);
809 ast_sockaddr_set_port(&candidate->relay_address, pj_sockaddr_get_port(rel_addr));
812 if (type == PJ_ICE_CAND_TYPE_HOST) {
813 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
814 } else if (type == PJ_ICE_CAND_TYPE_SRFLX) {
815 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
816 } else if (type == PJ_ICE_CAND_TYPE_RELAYED) {
817 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
820 if ((existing = ao2_find(rtp->ice_local_candidates, candidate, OBJ_POINTER))) {
821 ao2_ref(existing, -1);
822 ao2_ref(candidate, -1);
826 if (pj_ice_sess_add_cand(rtp->ice, comp_id, transport_id, type, local_pref, &foundation, addr, base_addr, rel_addr, addr_len, NULL) != PJ_SUCCESS) {
827 ao2_ref(candidate, -1);
831 /* By placing the candidate into the ICE session it will have produced the priority, so update the local candidate with it */
832 candidate->priority = rtp->ice->lcand[rtp->ice->lcand_cnt - 1].prio;
834 ao2_link(rtp->ice_local_candidates, candidate);
835 ao2_ref(candidate, -1);
838 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)
840 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
841 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
844 status = pj_ice_sess_on_rx_pkt(rtp->ice, AST_RTP_ICE_COMPONENT_RTP, TRANSPORT_TURN_RTP, pkt, pkt_len, peer_addr,
846 if (status != PJ_SUCCESS) {
849 pj_strerror(status, buf, sizeof(buf));
850 ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n",
854 if (!rtp->rtp_passthrough) {
857 rtp->rtp_passthrough = 0;
859 ast_sendto(rtp->s, pkt, pkt_len, 0, &rtp->rtp_loop);
862 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)
864 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
867 /* If this is a leftover from an already notified RTP instance just ignore the state change */
872 rtp = ast_rtp_instance_get_data(instance);
874 /* We store the new state so the other thread can actually handle it */
875 ast_mutex_lock(&rtp->lock);
876 rtp->turn_state = new_state;
877 ast_cond_signal(&rtp->cond);
879 if (new_state == PJ_TURN_STATE_DESTROYING) {
880 pj_turn_sock_set_user_data(rtp->turn_rtp, NULL);
881 rtp->turn_rtp = NULL;
884 ast_mutex_unlock(&rtp->lock);
887 /* RTP TURN Socket interface declaration */
888 static pj_turn_sock_cb ast_rtp_turn_rtp_sock_cb = {
889 .on_rx_data = ast_rtp_on_turn_rx_rtp_data,
890 .on_state = ast_rtp_on_turn_rtp_state,
893 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)
895 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
896 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
899 status = pj_ice_sess_on_rx_pkt(rtp->ice, AST_RTP_ICE_COMPONENT_RTCP, TRANSPORT_TURN_RTCP, pkt, pkt_len, peer_addr,
901 if (status != PJ_SUCCESS) {
904 pj_strerror(status, buf, sizeof(buf));
905 ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n",
909 if (!rtp->rtcp_passthrough) {
912 rtp->rtcp_passthrough = 0;
914 ast_sendto(rtp->rtcp->s, pkt, pkt_len, 0, &rtp->rtcp_loop);
917 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)
919 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
920 struct ast_rtp *rtp = NULL;
922 /* If this is a leftover from an already destroyed RTP instance just ignore the state change */
927 rtp = ast_rtp_instance_get_data(instance);
929 /* We store the new state so the other thread can actually handle it */
930 ast_mutex_lock(&rtp->lock);
931 rtp->turn_state = new_state;
932 ast_cond_signal(&rtp->cond);
934 if (new_state == PJ_TURN_STATE_DESTROYING) {
935 pj_turn_sock_set_user_data(rtp->turn_rtcp, NULL);
936 rtp->turn_rtcp = NULL;
939 ast_mutex_unlock(&rtp->lock);
942 /* RTCP TURN Socket interface declaration */
943 static pj_turn_sock_cb ast_rtp_turn_rtcp_sock_cb = {
944 .on_rx_data = ast_rtp_on_turn_rx_rtcp_data,
945 .on_state = ast_rtp_on_turn_rtcp_state,
948 /*! \brief Worker thread for ioqueue and timerheap */
949 static int ioqueue_worker_thread(void *data)
951 struct ast_rtp_ioqueue_thread *ioqueue = data;
953 while (!ioqueue->terminate) {
954 const pj_time_val delay = {0, 10};
956 pj_ioqueue_poll(ioqueue->ioqueue, &delay);
958 pj_timer_heap_poll(ioqueue->timerheap, NULL);
964 /*! \brief Destroyer for ioqueue thread */
965 static void rtp_ioqueue_thread_destroy(struct ast_rtp_ioqueue_thread *ioqueue)
967 if (ioqueue->thread) {
968 ioqueue->terminate = 1;
969 pj_thread_join(ioqueue->thread);
970 pj_thread_destroy(ioqueue->thread);
973 pj_pool_release(ioqueue->pool);
977 /*! \brief Removal function for ioqueue thread, determines if it should be terminated and destroyed */
978 static void rtp_ioqueue_thread_remove(struct ast_rtp_ioqueue_thread *ioqueue)
982 /* If nothing is using this ioqueue thread destroy it */
983 AST_LIST_LOCK(&ioqueues);
984 if ((ioqueue->count - 2) == 0) {
986 AST_LIST_REMOVE(&ioqueues, ioqueue, next);
988 AST_LIST_UNLOCK(&ioqueues);
994 rtp_ioqueue_thread_destroy(ioqueue);
997 /*! \brief Finder and allocator for an ioqueue thread */
998 static struct ast_rtp_ioqueue_thread *rtp_ioqueue_thread_get_or_create(void)
1000 struct ast_rtp_ioqueue_thread *ioqueue;
1003 AST_LIST_LOCK(&ioqueues);
1005 /* See if an ioqueue thread exists that can handle more */
1006 AST_LIST_TRAVERSE(&ioqueues, ioqueue, next) {
1007 if ((ioqueue->count + 2) < PJ_IOQUEUE_MAX_HANDLES) {
1012 /* If we found one bump it up and return it */
1014 ioqueue->count += 2;
1018 ioqueue = ast_calloc(1, sizeof(*ioqueue));
1023 ioqueue->pool = pj_pool_create(&cachingpool.factory, "rtp", 512, 512, NULL);
1025 /* We use a timer on the ioqueue thread for TURN so that two threads aren't operating
1026 * on a session at the same time
1028 if (pj_timer_heap_create(ioqueue->pool, 4, &ioqueue->timerheap) != PJ_SUCCESS) {
1032 if (pj_lock_create_recursive_mutex(ioqueue->pool, "rtp%p", &lock) != PJ_SUCCESS) {
1036 pj_timer_heap_set_lock(ioqueue->timerheap, lock, PJ_TRUE);
1038 if (pj_ioqueue_create(ioqueue->pool, PJ_IOQUEUE_MAX_HANDLES, &ioqueue->ioqueue) != PJ_SUCCESS) {
1042 if (pj_thread_create(ioqueue->pool, "ice", &ioqueue_worker_thread, ioqueue, 0, 0, &ioqueue->thread) != PJ_SUCCESS) {
1046 AST_LIST_INSERT_HEAD(&ioqueues, ioqueue, next);
1048 /* Since this is being returned to an active session the count always starts at 2 */
1054 rtp_ioqueue_thread_destroy(ioqueue);
1058 AST_LIST_UNLOCK(&ioqueues);
1062 static void ast_rtp_ice_turn_request(struct ast_rtp_instance *instance, enum ast_rtp_ice_component_type component,
1063 enum ast_transport transport, const char *server, unsigned int port, const char *username, const char *password)
1065 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1066 pj_turn_sock **turn_sock;
1067 const pj_turn_sock_cb *turn_cb;
1068 pj_turn_tp_type conn_type;
1070 pj_stun_auth_cred cred = { 0, };
1072 struct ast_sockaddr addr = { { 0, } };
1073 pj_stun_config stun_config;
1074 struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_STATE_WAIT_TIME, 1000));
1075 struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
1076 pj_turn_session_info info;
1077 struct ast_sockaddr local, loop;
1079 ast_rtp_instance_get_local_address(instance, &local);
1080 if (ast_sockaddr_is_ipv4(&local)) {
1081 ast_sockaddr_parse(&loop, "127.0.0.1", PARSE_PORT_FORBID);
1083 ast_sockaddr_parse(&loop, "::1", PARSE_PORT_FORBID);
1086 /* Determine what component we are requesting a TURN session for */
1087 if (component == AST_RTP_ICE_COMPONENT_RTP) {
1088 turn_sock = &rtp->turn_rtp;
1089 turn_cb = &ast_rtp_turn_rtp_sock_cb;
1090 conn_transport = TRANSPORT_TURN_RTP;
1091 ast_sockaddr_set_port(&loop, ast_sockaddr_port(&local));
1092 } else if (component == AST_RTP_ICE_COMPONENT_RTCP) {
1093 turn_sock = &rtp->turn_rtcp;
1094 turn_cb = &ast_rtp_turn_rtcp_sock_cb;
1095 conn_transport = TRANSPORT_TURN_RTCP;
1096 ast_sockaddr_set_port(&loop, ast_sockaddr_port(&rtp->rtcp->us));
1101 if (transport == AST_TRANSPORT_UDP) {
1102 conn_type = PJ_TURN_TP_UDP;
1103 } else if (transport == AST_TRANSPORT_TCP) {
1104 conn_type = PJ_TURN_TP_TCP;
1110 ast_sockaddr_parse(&addr, server, PARSE_PORT_FORBID);
1112 ast_mutex_lock(&rtp->lock);
1114 pj_turn_sock_destroy(*turn_sock);
1115 rtp->turn_state = PJ_TURN_STATE_NULL;
1116 while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
1117 ast_cond_timedwait(&rtp->cond, &rtp->lock, &ts);
1120 ast_mutex_unlock(&rtp->lock);
1122 if (component == AST_RTP_ICE_COMPONENT_RTP && !rtp->ioqueue) {
1123 rtp->ioqueue = rtp_ioqueue_thread_get_or_create();
1124 if (!rtp->ioqueue) {
1129 pj_stun_config_init(&stun_config, &cachingpool.factory, 0, rtp->ioqueue->ioqueue, rtp->ioqueue->timerheap);
1131 if (pj_turn_sock_create(&stun_config, ast_sockaddr_is_ipv4(&addr) ? pj_AF_INET() : pj_AF_INET6(), conn_type,
1132 turn_cb, NULL, instance, turn_sock) != PJ_SUCCESS) {
1133 ast_log(LOG_WARNING, "Could not create a TURN client socket\n");
1137 cred.type = PJ_STUN_AUTH_CRED_STATIC;
1138 pj_strset2(&cred.data.static_cred.username, (char*)username);
1139 cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
1140 pj_strset2(&cred.data.static_cred.data, (char*)password);
1142 /* Because the TURN socket is asynchronous but we are synchronous we need to wait until it is done */
1143 ast_mutex_lock(&rtp->lock);
1144 pj_turn_sock_alloc(*turn_sock, pj_cstr(&turn_addr, server), port, NULL, &cred, NULL);
1145 while (rtp->turn_state < PJ_TURN_STATE_READY) {
1146 ast_cond_timedwait(&rtp->cond, &rtp->lock, &ts);
1148 ast_mutex_unlock(&rtp->lock);
1150 /* If a TURN session was allocated add it as a candidate */
1151 if (rtp->turn_state != PJ_TURN_STATE_READY) {
1155 pj_turn_sock_get_info(*turn_sock, &info);
1157 ast_rtp_ice_add_cand(rtp, component, conn_transport, PJ_ICE_CAND_TYPE_RELAYED, 65535, &info.relay_addr,
1158 &info.relay_addr, &info.mapped_addr, pj_sockaddr_get_len(&info.relay_addr));
1160 if (component == AST_RTP_ICE_COMPONENT_RTP) {
1161 ast_sockaddr_copy(&rtp->rtp_loop, &loop);
1162 } else if (component == AST_RTP_ICE_COMPONENT_RTCP) {
1163 ast_sockaddr_copy(&rtp->rtcp_loop, &loop);
1167 static char *generate_random_string(char *buf, size_t size)
1172 for (x=0; x<4; x++) {
1173 val[x] = ast_random();
1175 snprintf(buf, size, "%08lx%08lx%08lx%08lx", (long unsigned)val[0], (long unsigned)val[1], (long unsigned)val[2], (long unsigned)val[3]);
1180 /* ICE RTP Engine interface declaration */
1181 static struct ast_rtp_engine_ice ast_rtp_ice = {
1182 .set_authentication = ast_rtp_ice_set_authentication,
1183 .add_remote_candidate = ast_rtp_ice_add_remote_candidate,
1184 .start = ast_rtp_ice_start,
1185 .stop = ast_rtp_ice_stop,
1186 .get_ufrag = ast_rtp_ice_get_ufrag,
1187 .get_password = ast_rtp_ice_get_password,
1188 .get_local_candidates = ast_rtp_ice_get_local_candidates,
1189 .ice_lite = ast_rtp_ice_lite,
1190 .set_role = ast_rtp_ice_set_role,
1191 .turn_request = ast_rtp_ice_turn_request,
1195 #ifdef HAVE_OPENSSL_SRTP
1196 static int dtls_verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
1198 /* We don't want to actually verify the certificate so just accept what they have provided */
1202 static int dtls_details_initialize(struct dtls_details *dtls, SSL_CTX *ssl_ctx,
1203 enum ast_rtp_dtls_setup setup)
1205 dtls->dtls_setup = setup;
1207 if (!(dtls->ssl = SSL_new(ssl_ctx))) {
1208 ast_log(LOG_ERROR, "Failed to allocate memory for SSL\n");
1212 if (!(dtls->read_bio = BIO_new(BIO_s_mem()))) {
1213 ast_log(LOG_ERROR, "Failed to allocate memory for inbound SSL traffic\n");
1216 BIO_set_mem_eof_return(dtls->read_bio, -1);
1218 if (!(dtls->write_bio = BIO_new(BIO_s_mem()))) {
1219 ast_log(LOG_ERROR, "Failed to allocate memory for outbound SSL traffic\n");
1222 BIO_set_mem_eof_return(dtls->write_bio, -1);
1224 SSL_set_bio(dtls->ssl, dtls->read_bio, dtls->write_bio);
1226 if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
1227 SSL_set_accept_state(dtls->ssl);
1229 SSL_set_connect_state(dtls->ssl);
1231 dtls->connection = AST_RTP_DTLS_CONNECTION_NEW;
1233 ast_mutex_init(&dtls->lock);
1238 if (dtls->read_bio) {
1239 BIO_free(dtls->read_bio);
1240 dtls->read_bio = NULL;
1243 if (dtls->write_bio) {
1244 BIO_free(dtls->write_bio);
1245 dtls->write_bio = NULL;
1249 SSL_free(dtls->ssl);
1255 static int dtls_setup_rtcp(struct ast_rtp_instance *instance)
1257 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1259 if (!rtp->ssl_ctx || !rtp->rtcp) {
1263 return dtls_details_initialize(&rtp->rtcp->dtls, rtp->ssl_ctx, rtp->dtls.dtls_setup);
1266 static int ast_rtp_dtls_set_configuration(struct ast_rtp_instance *instance, const struct ast_rtp_dtls_cfg *dtls_cfg)
1268 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1270 #ifndef HAVE_OPENSSL_ECDH_AUTO
1274 if (!dtls_cfg->enabled) {
1278 if (!ast_rtp_engine_srtp_is_registered()) {
1279 ast_log(LOG_ERROR, "SRTP support module is not loaded or available. Try loading res_srtp.so.\n");
1287 if (!(rtp->ssl_ctx = SSL_CTX_new(DTLSv1_method()))) {
1291 SSL_CTX_set_read_ahead(rtp->ssl_ctx, 1);
1293 #ifdef HAVE_OPENSSL_ECDH_AUTO
1294 SSL_CTX_set_ecdh_auto(rtp->ssl_ctx, 1);
1296 ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
1298 SSL_CTX_set_tmp_ecdh(rtp->ssl_ctx, ecdh);
1303 rtp->dtls_verify = dtls_cfg->verify;
1305 SSL_CTX_set_verify(rtp->ssl_ctx, (rtp->dtls_verify & AST_RTP_DTLS_VERIFY_FINGERPRINT) || (rtp->dtls_verify & AST_RTP_DTLS_VERIFY_CERTIFICATE) ?
1306 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT : SSL_VERIFY_NONE, !(rtp->dtls_verify & AST_RTP_DTLS_VERIFY_CERTIFICATE) ?
1307 dtls_verify_callback : NULL);
1309 if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_80) {
1310 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_80");
1311 } else if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_32) {
1312 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_32");
1314 ast_log(LOG_ERROR, "Unsupported suite specified for DTLS-SRTP on RTP instance '%p'\n", instance);
1318 rtp->local_hash = dtls_cfg->hash;
1320 if (!ast_strlen_zero(dtls_cfg->certfile)) {
1321 char *private = ast_strlen_zero(dtls_cfg->pvtfile) ? dtls_cfg->certfile : dtls_cfg->pvtfile;
1325 unsigned int size, i;
1326 unsigned char fingerprint[EVP_MAX_MD_SIZE];
1327 char *local_fingerprint = rtp->local_fingerprint;
1329 if (!SSL_CTX_use_certificate_file(rtp->ssl_ctx, dtls_cfg->certfile, SSL_FILETYPE_PEM)) {
1330 ast_log(LOG_ERROR, "Specified certificate file '%s' for RTP instance '%p' could not be used\n",
1331 dtls_cfg->certfile, instance);
1335 if (!SSL_CTX_use_PrivateKey_file(rtp->ssl_ctx, private, SSL_FILETYPE_PEM) ||
1336 !SSL_CTX_check_private_key(rtp->ssl_ctx)) {
1337 ast_log(LOG_ERROR, "Specified private key file '%s' for RTP instance '%p' could not be used\n",
1342 if (!(certbio = BIO_new(BIO_s_file()))) {
1343 ast_log(LOG_ERROR, "Failed to allocate memory for certificate fingerprinting on RTP instance '%p'\n",
1348 if (rtp->local_hash == AST_RTP_DTLS_HASH_SHA1) {
1350 } else if (rtp->local_hash == AST_RTP_DTLS_HASH_SHA256) {
1351 type = EVP_sha256();
1353 ast_log(LOG_ERROR, "Unsupported fingerprint hash type on RTP instance '%p'\n",
1358 if (!BIO_read_filename(certbio, dtls_cfg->certfile) ||
1359 !(cert = PEM_read_bio_X509(certbio, NULL, 0, NULL)) ||
1360 !X509_digest(cert, type, fingerprint, &size) ||
1362 ast_log(LOG_ERROR, "Could not produce fingerprint from certificate '%s' for RTP instance '%p'\n",
1363 dtls_cfg->certfile, instance);
1364 BIO_free_all(certbio);
1368 for (i = 0; i < size; i++) {
1369 sprintf(local_fingerprint, "%02hhX:", fingerprint[i]);
1370 local_fingerprint += 3;
1373 *(local_fingerprint-1) = 0;
1375 BIO_free_all(certbio);
1378 if (!ast_strlen_zero(dtls_cfg->cipher)) {
1379 if (!SSL_CTX_set_cipher_list(rtp->ssl_ctx, dtls_cfg->cipher)) {
1380 ast_log(LOG_ERROR, "Invalid cipher specified in cipher list '%s' for RTP instance '%p'\n",
1381 dtls_cfg->cipher, instance);
1386 if (!ast_strlen_zero(dtls_cfg->cafile) || !ast_strlen_zero(dtls_cfg->capath)) {
1387 if (!SSL_CTX_load_verify_locations(rtp->ssl_ctx, S_OR(dtls_cfg->cafile, NULL), S_OR(dtls_cfg->capath, NULL))) {
1388 ast_log(LOG_ERROR, "Invalid certificate authority file '%s' or path '%s' specified for RTP instance '%p'\n",
1389 S_OR(dtls_cfg->cafile, ""), S_OR(dtls_cfg->capath, ""), instance);
1394 rtp->rekey = dtls_cfg->rekey;
1395 rtp->suite = dtls_cfg->suite;
1397 res = dtls_details_initialize(&rtp->dtls, rtp->ssl_ctx, dtls_cfg->default_setup);
1399 dtls_setup_rtcp(instance);
1405 static int ast_rtp_dtls_active(struct ast_rtp_instance *instance)
1407 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1409 return !rtp->ssl_ctx ? 0 : 1;
1412 static void ast_rtp_dtls_stop(struct ast_rtp_instance *instance)
1414 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1416 dtls_srtp_stop_timeout_timer(instance, rtp, 0);
1419 SSL_CTX_free(rtp->ssl_ctx);
1420 rtp->ssl_ctx = NULL;
1423 if (rtp->dtls.ssl) {
1424 SSL_free(rtp->dtls.ssl);
1425 rtp->dtls.ssl = NULL;
1426 ast_mutex_destroy(&rtp->dtls.lock);
1430 dtls_srtp_stop_timeout_timer(instance, rtp, 1);
1432 if (rtp->rtcp->dtls.ssl) {
1433 SSL_free(rtp->rtcp->dtls.ssl);
1434 rtp->rtcp->dtls.ssl = NULL;
1435 ast_mutex_destroy(&rtp->rtcp->dtls.lock);
1440 static void ast_rtp_dtls_reset(struct ast_rtp_instance *instance)
1442 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1444 if (SSL_is_init_finished(rtp->dtls.ssl)) {
1445 SSL_shutdown(rtp->dtls.ssl);
1446 rtp->dtls.connection = AST_RTP_DTLS_CONNECTION_NEW;
1449 if (rtp->rtcp && SSL_is_init_finished(rtp->rtcp->dtls.ssl)) {
1450 SSL_shutdown(rtp->rtcp->dtls.ssl);
1451 rtp->rtcp->dtls.connection = AST_RTP_DTLS_CONNECTION_NEW;
1455 static enum ast_rtp_dtls_connection ast_rtp_dtls_get_connection(struct ast_rtp_instance *instance)
1457 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1459 return rtp->dtls.connection;
1462 static enum ast_rtp_dtls_setup ast_rtp_dtls_get_setup(struct ast_rtp_instance *instance)
1464 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1466 return rtp->dtls.dtls_setup;
1469 static void dtls_set_setup(enum ast_rtp_dtls_setup *dtls_setup, enum ast_rtp_dtls_setup setup, SSL *ssl)
1471 enum ast_rtp_dtls_setup old = *dtls_setup;
1474 case AST_RTP_DTLS_SETUP_ACTIVE:
1475 *dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
1477 case AST_RTP_DTLS_SETUP_PASSIVE:
1478 *dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
1480 case AST_RTP_DTLS_SETUP_ACTPASS:
1481 /* We can't respond to an actpass setup with actpass ourselves... so respond with active, as we can initiate connections */
1482 if (*dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
1483 *dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
1486 case AST_RTP_DTLS_SETUP_HOLDCONN:
1487 *dtls_setup = AST_RTP_DTLS_SETUP_HOLDCONN;
1490 /* This should never occur... if it does exit early as we don't know what state things are in */
1494 /* If the setup state did not change we go on as if nothing happened */
1495 if (old == *dtls_setup) {
1499 /* If they don't want us to establish a connection wait until later */
1500 if (*dtls_setup == AST_RTP_DTLS_SETUP_HOLDCONN) {
1504 if (*dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
1505 SSL_set_connect_state(ssl);
1506 } else if (*dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
1507 SSL_set_accept_state(ssl);
1513 static void ast_rtp_dtls_set_setup(struct ast_rtp_instance *instance, enum ast_rtp_dtls_setup setup)
1515 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1517 if (rtp->dtls.ssl) {
1518 dtls_set_setup(&rtp->dtls.dtls_setup, setup, rtp->dtls.ssl);
1521 if (rtp->rtcp && rtp->rtcp->dtls.ssl) {
1522 dtls_set_setup(&rtp->rtcp->dtls.dtls_setup, setup, rtp->rtcp->dtls.ssl);
1526 static void ast_rtp_dtls_set_fingerprint(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash, const char *fingerprint)
1528 char *tmp = ast_strdupa(fingerprint), *value;
1530 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1532 if (hash != AST_RTP_DTLS_HASH_SHA1 && hash != AST_RTP_DTLS_HASH_SHA256) {
1536 rtp->remote_hash = hash;
1538 while ((value = strsep(&tmp, ":")) && (pos != (EVP_MAX_MD_SIZE - 1))) {
1539 sscanf(value, "%02hhx", &rtp->remote_fingerprint[pos++]);
1543 static enum ast_rtp_dtls_hash ast_rtp_dtls_get_fingerprint_hash(struct ast_rtp_instance *instance)
1545 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1547 return rtp->local_hash;
1550 static const char *ast_rtp_dtls_get_fingerprint(struct ast_rtp_instance *instance)
1552 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1554 return rtp->local_fingerprint;
1557 /* DTLS RTP Engine interface declaration */
1558 static struct ast_rtp_engine_dtls ast_rtp_dtls = {
1559 .set_configuration = ast_rtp_dtls_set_configuration,
1560 .active = ast_rtp_dtls_active,
1561 .stop = ast_rtp_dtls_stop,
1562 .reset = ast_rtp_dtls_reset,
1563 .get_connection = ast_rtp_dtls_get_connection,
1564 .get_setup = ast_rtp_dtls_get_setup,
1565 .set_setup = ast_rtp_dtls_set_setup,
1566 .set_fingerprint = ast_rtp_dtls_set_fingerprint,
1567 .get_fingerprint_hash = ast_rtp_dtls_get_fingerprint_hash,
1568 .get_fingerprint = ast_rtp_dtls_get_fingerprint,
1573 /* RTP Engine Declaration */
1574 static struct ast_rtp_engine asterisk_rtp_engine = {
1577 .destroy = ast_rtp_destroy,
1578 .dtmf_begin = ast_rtp_dtmf_begin,
1579 .dtmf_end = ast_rtp_dtmf_end,
1580 .dtmf_end_with_duration = ast_rtp_dtmf_end_with_duration,
1581 .dtmf_mode_set = ast_rtp_dtmf_mode_set,
1582 .dtmf_mode_get = ast_rtp_dtmf_mode_get,
1583 .update_source = ast_rtp_update_source,
1584 .change_source = ast_rtp_change_source,
1585 .write = ast_rtp_write,
1586 .read = ast_rtp_read,
1587 .prop_set = ast_rtp_prop_set,
1589 .remote_address_set = ast_rtp_remote_address_set,
1590 .red_init = rtp_red_init,
1591 .red_buffer = rtp_red_buffer,
1592 .local_bridge = ast_rtp_local_bridge,
1593 .get_stat = ast_rtp_get_stat,
1594 .dtmf_compatible = ast_rtp_dtmf_compatible,
1595 .stun_request = ast_rtp_stun_request,
1596 .stop = ast_rtp_stop,
1597 .qos = ast_rtp_qos_set,
1598 .sendcng = ast_rtp_sendcng,
1599 #ifdef HAVE_PJPROJECT
1600 .ice = &ast_rtp_ice,
1602 #ifdef HAVE_OPENSSL_SRTP
1603 .dtls = &ast_rtp_dtls,
1604 .activate = ast_rtp_activate,
1608 #ifdef HAVE_OPENSSL_SRTP
1609 static void dtls_perform_handshake(struct ast_rtp_instance *instance, struct dtls_details *dtls, int rtcp)
1611 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1613 /* If we are not acting as a client connecting to the remote side then
1614 * don't start the handshake as it will accomplish nothing and would conflict
1615 * with the handshake we receive from the remote side.
1617 if (!dtls->ssl || (dtls->dtls_setup != AST_RTP_DTLS_SETUP_ACTIVE)) {
1621 SSL_do_handshake(dtls->ssl);
1623 /* Since the handshake is started in a thread outside of the channel thread it's possible
1624 * for the response to be handled in the channel thread before we start the timeout timer.
1625 * To ensure this doesn't actually happen we hold the DTLS lock. The channel thread will
1626 * block until we're done at which point the timeout timer will be immediately stopped.
1628 ast_mutex_lock(&dtls->lock);
1629 dtls_srtp_check_pending(instance, rtp, rtcp);
1630 dtls_srtp_start_timeout_timer(instance, rtp, rtcp);
1631 ast_mutex_unlock(&dtls->lock);
1635 #ifdef HAVE_PJPROJECT
1636 static void rtp_learning_seq_init(struct rtp_learning_info *info, uint16_t seq);
1638 static void ast_rtp_on_ice_complete(pj_ice_sess *ice, pj_status_t status)
1640 struct ast_rtp_instance *instance = ice->user_data;
1641 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1643 if (status == PJ_SUCCESS) {
1644 struct ast_sockaddr remote_address;
1646 /* Symmetric RTP must be disabled for the remote address to not get overwritten */
1647 ast_rtp_instance_set_prop(instance, AST_RTP_PROPERTY_NAT, 0);
1649 update_address_with_ice_candidate(rtp, AST_RTP_ICE_COMPONENT_RTP, &remote_address);
1650 ast_rtp_instance_set_remote_address(instance, &remote_address);
1653 update_address_with_ice_candidate(rtp, AST_RTP_ICE_COMPONENT_RTCP, &rtp->rtcp->them);
1657 #ifdef HAVE_OPENSSL_SRTP
1658 dtls_perform_handshake(instance, &rtp->dtls, 0);
1661 dtls_perform_handshake(instance, &rtp->rtcp->dtls, 1);
1669 rtp->strict_rtp_state = STRICT_RTP_LEARN;
1670 rtp_learning_seq_init(&rtp->rtp_source_learn, (uint16_t)rtp->seqno);
1673 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)
1675 struct ast_rtp_instance *instance = ice->user_data;
1676 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1678 /* 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
1680 if (transport_id == TRANSPORT_SOCKET_RTP || transport_id == TRANSPORT_SOCKET_RTCP) {
1681 rtp->passthrough = 1;
1682 } else if (transport_id == TRANSPORT_TURN_RTP) {
1683 rtp->rtp_passthrough = 1;
1684 } else if (transport_id == TRANSPORT_TURN_RTCP) {
1685 rtp->rtcp_passthrough = 1;
1689 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)
1691 struct ast_rtp_instance *instance = ice->user_data;
1692 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1693 pj_status_t status = PJ_EINVALIDOP;
1694 pj_ssize_t _size = (pj_ssize_t)size;
1696 if (transport_id == TRANSPORT_SOCKET_RTP) {
1697 /* Traffic is destined to go right out the RTP socket we already have */
1698 status = pj_sock_sendto(rtp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
1699 /* sendto on a connectionless socket should send all the data, or none at all */
1700 ast_assert(_size == size || status != PJ_SUCCESS);
1701 } else if (transport_id == TRANSPORT_SOCKET_RTCP) {
1702 /* Traffic is destined to go right out the RTCP socket we already have */
1704 status = pj_sock_sendto(rtp->rtcp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
1705 /* sendto on a connectionless socket should send all the data, or none at all */
1706 ast_assert(_size == size || status != PJ_SUCCESS);
1708 status = PJ_SUCCESS;
1710 } else if (transport_id == TRANSPORT_TURN_RTP) {
1711 /* Traffic is going through the RTP TURN relay */
1712 if (rtp->turn_rtp) {
1713 status = pj_turn_sock_sendto(rtp->turn_rtp, pkt, size, dst_addr, dst_addr_len);
1715 } else if (transport_id == TRANSPORT_TURN_RTCP) {
1716 /* Traffic is going through the RTCP TURN relay */
1717 if (rtp->turn_rtcp) {
1718 status = pj_turn_sock_sendto(rtp->turn_rtcp, pkt, size, dst_addr, dst_addr_len);
1725 /* ICE Session interface declaration */
1726 static pj_ice_sess_cb ast_rtp_ice_sess_cb = {
1727 .on_ice_complete = ast_rtp_on_ice_complete,
1728 .on_rx_data = ast_rtp_on_ice_rx_data,
1729 .on_tx_pkt = ast_rtp_on_ice_tx_pkt,
1732 /*! \brief Worker thread for timerheap */
1733 static int timer_worker_thread(void *data)
1735 pj_ioqueue_t *ioqueue;
1737 if (pj_ioqueue_create(pool, 1, &ioqueue) != PJ_SUCCESS) {
1741 while (!timer_terminate) {
1742 const pj_time_val delay = {0, 10};
1744 pj_timer_heap_poll(timer_heap, NULL);
1745 pj_ioqueue_poll(ioqueue, &delay);
1752 static inline int rtp_debug_test_addr(struct ast_sockaddr *addr)
1757 if (!ast_sockaddr_isnull(&rtpdebugaddr)) {
1759 return (ast_sockaddr_cmp(&rtpdebugaddr, addr) == 0); /* look for RTP packets from IP+Port */
1761 return (ast_sockaddr_cmp_addr(&rtpdebugaddr, addr) == 0); /* only look for RTP packets from IP */
1768 static inline int rtcp_debug_test_addr(struct ast_sockaddr *addr)
1773 if (!ast_sockaddr_isnull(&rtcpdebugaddr)) {
1774 if (rtcpdebugport) {
1775 return (ast_sockaddr_cmp(&rtcpdebugaddr, addr) == 0); /* look for RTCP packets from IP+Port */
1777 return (ast_sockaddr_cmp_addr(&rtcpdebugaddr, addr) == 0); /* only look for RTCP packets from IP */
1784 #ifdef HAVE_OPENSSL_SRTP
1785 static int dtls_srtp_handle_timeout(struct ast_rtp_instance *instance, int rtcp)
1787 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1788 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
1789 struct timeval dtls_timeout;
1791 DTLSv1_handle_timeout(dtls->ssl);
1792 dtls_srtp_check_pending(instance, rtp, rtcp);
1794 /* If a timeout can't be retrieved then this recurring scheduled item must stop */
1795 if (!DTLSv1_get_timeout(dtls->ssl, &dtls_timeout)) {
1796 dtls->timeout_timer = -1;
1800 return dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000;
1803 static int dtls_srtp_handle_rtp_timeout(const void *data)
1805 struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
1808 reschedule = dtls_srtp_handle_timeout(instance, 0);
1811 ao2_ref(instance, -1);
1817 static int dtls_srtp_handle_rtcp_timeout(const void *data)
1819 struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
1822 reschedule = dtls_srtp_handle_timeout(instance, 1);
1825 ao2_ref(instance, -1);
1831 static void dtls_srtp_start_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp)
1833 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
1834 struct timeval dtls_timeout;
1836 if (DTLSv1_get_timeout(dtls->ssl, &dtls_timeout)) {
1837 int timeout = dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000;
1839 ast_assert(dtls->timeout_timer == -1);
1841 ao2_ref(instance, +1);
1842 if ((dtls->timeout_timer = ast_sched_add(rtp->sched, timeout,
1843 !rtcp ? dtls_srtp_handle_rtp_timeout : dtls_srtp_handle_rtcp_timeout, instance)) < 0) {
1844 ao2_ref(instance, -1);
1845 ast_log(LOG_WARNING, "Scheduling '%s' DTLS retransmission for RTP instance [%p] failed.\n",
1846 !rtcp ? "RTP" : "RTCP", instance);
1851 static void dtls_srtp_stop_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp)
1853 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
1855 AST_SCHED_DEL_UNREF(rtp->sched, dtls->timeout_timer, ao2_ref(instance, -1));
1858 static void dtls_srtp_check_pending(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp)
1860 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
1863 if (!dtls->ssl || !dtls->write_bio) {
1867 pending = BIO_ctrl_pending(dtls->write_bio);
1870 char outgoing[pending];
1872 struct ast_sockaddr remote_address = { {0, } };
1876 ast_rtp_instance_get_remote_address(instance, &remote_address);
1878 ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
1881 /* If we do not yet know an address to send this to defer it until we do */
1882 if (ast_sockaddr_isnull(&remote_address)) {
1886 out = BIO_read(dtls->write_bio, outgoing, sizeof(outgoing));
1887 __rtp_sendto(instance, outgoing, out, 0, &remote_address, rtcp, &ice, 0);
1891 static int dtls_srtp_renegotiate(const void *data)
1893 struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
1894 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1896 SSL_renegotiate(rtp->dtls.ssl);
1897 SSL_do_handshake(rtp->dtls.ssl);
1898 dtls_srtp_check_pending(instance, rtp, 0);
1900 if (rtp->rtcp && rtp->rtcp->dtls.ssl) {
1901 SSL_renegotiate(rtp->rtcp->dtls.ssl);
1902 SSL_do_handshake(rtp->rtcp->dtls.ssl);
1903 dtls_srtp_check_pending(instance, rtp, 1);
1907 ao2_ref(instance, -1);
1912 static int dtls_srtp_setup(struct ast_rtp *rtp, struct ast_srtp *srtp, struct ast_rtp_instance *instance)
1914 unsigned char material[SRTP_MASTER_LEN * 2];
1915 unsigned char *local_key, *local_salt, *remote_key, *remote_salt;
1916 struct ast_srtp_policy *local_policy, *remote_policy = NULL;
1917 struct ast_rtp_instance_stats stats = { 0, };
1920 /* If a fingerprint is present in the SDP make sure that the peer certificate matches it */
1921 if (rtp->dtls_verify & AST_RTP_DTLS_VERIFY_FINGERPRINT) {
1924 if (!(certificate = SSL_get_peer_certificate(rtp->dtls.ssl))) {
1925 ast_log(LOG_WARNING, "No certificate was provided by the peer on RTP instance '%p'\n", instance);
1929 /* If a fingerprint is present in the SDP make sure that the peer certificate matches it */
1930 if (rtp->remote_fingerprint[0]) {
1932 unsigned char fingerprint[EVP_MAX_MD_SIZE];
1935 if (rtp->remote_hash == AST_RTP_DTLS_HASH_SHA1) {
1937 } else if (rtp->remote_hash == AST_RTP_DTLS_HASH_SHA256) {
1938 type = EVP_sha256();
1940 ast_log(LOG_WARNING, "Unsupported fingerprint hash type on RTP instance '%p'\n", instance);
1944 if (!X509_digest(certificate, type, fingerprint, &size) ||
1946 memcmp(fingerprint, rtp->remote_fingerprint, size)) {
1947 X509_free(certificate);
1948 ast_log(LOG_WARNING, "Fingerprint provided by remote party does not match that of peer certificate on RTP instance '%p'\n",
1954 X509_free(certificate);
1957 /* Ensure that certificate verification was successful */
1958 if ((rtp->dtls_verify & AST_RTP_DTLS_VERIFY_CERTIFICATE) && SSL_get_verify_result(rtp->dtls.ssl) != X509_V_OK) {
1959 ast_log(LOG_WARNING, "Peer certificate on RTP instance '%p' failed verification test\n",
1964 /* Produce key information and set up SRTP */
1965 if (!SSL_export_keying_material(rtp->dtls.ssl, material, SRTP_MASTER_LEN * 2, "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) {
1966 ast_log(LOG_WARNING, "Unable to extract SRTP keying material from DTLS-SRTP negotiation on RTP instance '%p'\n",
1971 /* Whether we are acting as a server or client determines where the keys/salts are */
1972 if (rtp->dtls.dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
1973 local_key = material;
1974 remote_key = local_key + SRTP_MASTER_KEY_LEN;
1975 local_salt = remote_key + SRTP_MASTER_KEY_LEN;
1976 remote_salt = local_salt + SRTP_MASTER_SALT_LEN;
1978 remote_key = material;
1979 local_key = remote_key + SRTP_MASTER_KEY_LEN;
1980 remote_salt = local_key + SRTP_MASTER_KEY_LEN;
1981 local_salt = remote_salt + SRTP_MASTER_SALT_LEN;
1984 if (!(local_policy = res_srtp_policy->alloc())) {
1988 if (res_srtp_policy->set_master_key(local_policy, local_key, SRTP_MASTER_KEY_LEN, local_salt, SRTP_MASTER_SALT_LEN) < 0) {
1989 ast_log(LOG_WARNING, "Could not set key/salt information on local policy of '%p' when setting up DTLS-SRTP\n", rtp);
1993 if (res_srtp_policy->set_suite(local_policy, rtp->suite)) {
1994 ast_log(LOG_WARNING, "Could not set suite to '%u' on local policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
1998 if (ast_rtp_instance_get_stats(instance, &stats, AST_RTP_INSTANCE_STAT_LOCAL_SSRC)) {
2002 res_srtp_policy->set_ssrc(local_policy, stats.local_ssrc, 0);
2004 if (!(remote_policy = res_srtp_policy->alloc())) {
2008 if (res_srtp_policy->set_master_key(remote_policy, remote_key, SRTP_MASTER_KEY_LEN, remote_salt, SRTP_MASTER_SALT_LEN) < 0) {
2009 ast_log(LOG_WARNING, "Could not set key/salt information on remote policy of '%p' when setting up DTLS-SRTP\n", rtp);
2013 if (res_srtp_policy->set_suite(remote_policy, rtp->suite)) {
2014 ast_log(LOG_WARNING, "Could not set suite to '%u' on remote policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
2018 res_srtp_policy->set_ssrc(remote_policy, 0, 1);
2020 if (ast_rtp_instance_add_srtp_policy(instance, remote_policy, local_policy)) {
2021 ast_log(LOG_WARNING, "Could not set policies when setting up DTLS-SRTP on '%p'\n", rtp);
2026 ao2_ref(instance, +1);
2027 if ((rtp->rekeyid = ast_sched_add(rtp->sched, rtp->rekey * 1000, dtls_srtp_renegotiate, instance)) < 0) {
2028 ao2_ref(instance, -1);
2036 /* policy->destroy() called even on success to release local reference to these resources */
2037 res_srtp_policy->destroy(local_policy);
2039 if (remote_policy) {
2040 res_srtp_policy->destroy(remote_policy);
2047 static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp)
2050 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2051 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
2053 #ifdef HAVE_PJPROJECT
2054 struct ast_sockaddr *loop = rtcp ? &rtp->rtcp_loop : &rtp->rtp_loop;
2057 if ((len = ast_recvfrom(rtcp ? rtp->rtcp->s : rtp->s, buf, size, flags, sa)) < 0) {
2061 #ifdef HAVE_OPENSSL_SRTP
2062 /* If this is an SSL packet pass it to OpenSSL for processing. RFC section for first byte value:
2063 * https://tools.ietf.org/html/rfc5764#section-5.1.2 */
2064 if ((*in >= 20) && (*in <= 63)) {
2065 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
2068 /* If no SSL session actually exists terminate things */
2070 ast_log(LOG_ERROR, "Received SSL traffic on RTP instance '%p' without an SSL session\n",
2075 /* This mutex is locked so that this thread blocks until the dtls_perform_handshake function
2078 ast_mutex_lock(&dtls->lock);
2079 ast_mutex_unlock(&dtls->lock);
2081 /* Before we feed data into OpenSSL ensure that the timeout timer is either stopped or completed */
2082 dtls_srtp_stop_timeout_timer(instance, rtp, rtcp);
2084 /* If we don't yet know if we are active or passive and we receive a packet... we are obviously passive */
2085 if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
2086 dtls->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
2087 SSL_set_accept_state(dtls->ssl);
2090 dtls_srtp_check_pending(instance, rtp, rtcp);
2092 BIO_write(dtls->read_bio, buf, len);
2094 len = SSL_read(dtls->ssl, buf, len);
2096 if ((len < 0) && (SSL_get_error(dtls->ssl, len) == SSL_ERROR_SSL)) {
2097 unsigned long error = ERR_get_error();
2098 ast_log(LOG_ERROR, "DTLS failure occurred on RTP instance '%p' due to reason '%s', terminating\n",
2099 instance, ERR_reason_error_string(error));
2103 dtls_srtp_check_pending(instance, rtp, rtcp);
2105 if (SSL_is_init_finished(dtls->ssl)) {
2106 /* Any further connections will be existing since this is now established */
2107 dtls->connection = AST_RTP_DTLS_CONNECTION_EXISTING;
2109 /* Use the keying material to set up key/salt information */
2110 res = dtls_srtp_setup(rtp, srtp, instance);
2113 /* Since we've sent additional traffic start the timeout timer for retransmission */
2114 dtls_srtp_start_timeout_timer(instance, rtp, rtcp);
2121 #ifdef HAVE_PJPROJECT
2122 if (!ast_sockaddr_isnull(loop) && !ast_sockaddr_cmp(loop, sa)) {
2123 /* ICE traffic will have been handled in the TURN callback, so skip it but update the address
2124 * so it reflects the actual source and not the loopback
2127 ast_sockaddr_copy(sa, &rtp->rtcp->them);
2129 ast_rtp_instance_get_remote_address(instance, sa);
2131 } else if (rtp->ice) {
2132 pj_str_t combined = pj_str(ast_sockaddr_stringify(sa));
2133 pj_sockaddr address;
2136 pj_thread_register_check();
2138 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &combined, &address);
2140 status = pj_ice_sess_on_rx_pkt(rtp->ice, rtcp ? AST_RTP_ICE_COMPONENT_RTCP : AST_RTP_ICE_COMPONENT_RTP,
2141 rtcp ? TRANSPORT_SOCKET_RTCP : TRANSPORT_SOCKET_RTP, buf, len, &address,
2142 pj_sockaddr_get_len(&address));
2143 if (status != PJ_SUCCESS) {
2146 pj_strerror(status, buf, sizeof(buf));
2147 ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n",
2151 if (!rtp->passthrough) {
2154 rtp->passthrough = 0;
2158 if ((*in & 0xC0) && res_srtp && srtp && res_srtp->unprotect(srtp, buf, &len, rtcp) < 0) {
2165 static int rtcp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
2167 return __rtp_recvfrom(instance, buf, size, flags, sa, 1);
2170 static int rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
2172 return __rtp_recvfrom(instance, buf, size, flags, sa, 0);
2175 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)
2179 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2180 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
2185 if (use_srtp && res_srtp && srtp && res_srtp->protect(srtp, &temp, &len, rtcp) < 0) {
2189 #ifdef HAVE_PJPROJECT
2191 pj_thread_register_check();
2193 if (pj_ice_sess_send_data(rtp->ice, rtcp ? AST_RTP_ICE_COMPONENT_RTCP : AST_RTP_ICE_COMPONENT_RTP, temp, len) == PJ_SUCCESS) {
2200 res = ast_sendto(rtcp ? rtp->rtcp->s : rtp->s, temp, len, flags, sa);
2202 ast_rtp_instance_set_last_tx(instance, time(NULL));
2207 static int rtcp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
2209 return __rtp_sendto(instance, buf, size, flags, sa, 1, ice, 1);
2212 static int rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
2214 return __rtp_sendto(instance, buf, size, flags, sa, 0, ice, 1);
2217 static int rtp_get_rate(struct ast_format *format)
2219 /* For those wondering: due to a fluke in RFC publication, G.722 is advertised
2220 * as having a sample rate of 8kHz, while implementations must know that its
2221 * real rate is 16kHz. Seriously.
2223 return (ast_format_cmp(format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) ? 8000 : (int)ast_format_get_sample_rate(format);
2226 static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
2228 unsigned int interval;
2229 /*! \todo XXX Do a more reasonable calculation on this one
2230 * Look in RFC 3550 Section A.7 for an example*/
2231 interval = rtcpinterval;
2235 /*! \brief Calculate normal deviation */
2236 static double normdev_compute(double normdev, double sample, unsigned int sample_count)
2238 normdev = normdev * sample_count + sample;
2241 return normdev / sample_count;
2244 static double stddev_compute(double stddev, double sample, double normdev, double normdev_curent, unsigned int sample_count)
2247 for the formula check http://www.cs.umd.edu/~austinjp/constSD.pdf
2248 return sqrt( (sample_count*pow(stddev,2) + sample_count*pow((sample-normdev)/(sample_count+1),2) + pow(sample-normdev_curent,2)) / (sample_count+1));
2249 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
2252 #define SQUARE(x) ((x) * (x))
2254 stddev = sample_count * stddev;
2258 ( sample_count * SQUARE( (sample - normdev) / sample_count ) ) +
2259 ( SQUARE(sample - normdev_curent) / sample_count );
2264 static int create_new_socket(const char *type, int af)
2266 int sock = socket(af, SOCK_DGRAM, 0);
2272 ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
2274 long flags = fcntl(sock, F_GETFL);
2275 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
2278 setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
2288 * \brief Initializes sequence values and probation for learning mode.
2289 * \note This is an adaptation of pjmedia's pjmedia_rtp_seq_init function.
2291 * \param info The learning information to track
2292 * \param seq sequence number read from the rtp header to initialize the information with
2294 static void rtp_learning_seq_init(struct rtp_learning_info *info, uint16_t seq)
2296 info->max_seq = seq - 1;
2297 info->packets = learning_min_sequential;
2302 * \brief Updates sequence information for learning mode and determines if probation/learning mode should remain in effect.
2303 * \note This function was adapted from pjmedia's pjmedia_rtp_seq_update function.
2305 * \param info Structure tracking the learning progress of some address
2306 * \param seq sequence number read from the rtp header
2307 * \retval 0 if probation mode should exit for this address
2308 * \retval non-zero if probation mode should continue
2310 static int rtp_learning_rtp_seq_update(struct rtp_learning_info *info, uint16_t seq)
2312 if (seq == info->max_seq + 1) {
2313 /* packet is in sequence */
2316 /* Sequence discontinuity; reset */
2317 info->packets = learning_min_sequential - 1;
2319 info->max_seq = seq;
2321 return (info->packets == 0);
2324 #ifdef HAVE_PJPROJECT
2325 static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *addr, int port, int component,
2328 pj_sockaddr address[16];
2329 unsigned int count = PJ_ARRAY_SIZE(address), pos = 0;
2331 /* Add all the local interface IP addresses */
2332 if (ast_sockaddr_is_ipv4(addr)) {
2333 pj_enum_ip_interface(pj_AF_INET(), &count, address);
2334 } else if (ast_sockaddr_is_any(addr)) {
2335 pj_enum_ip_interface(pj_AF_UNSPEC(), &count, address);
2337 pj_enum_ip_interface(pj_AF_INET6(), &count, address);
2340 for (pos = 0; pos < count; pos++) {
2341 pj_sockaddr_set_port(&address[pos], port);
2342 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_HOST, 65535, &address[pos], &address[pos], NULL,
2343 pj_sockaddr_get_len(&address[pos]));
2346 /* If configured to use a STUN server to get our external mapped address do so */
2347 if (stunaddr.sin_addr.s_addr && ast_sockaddr_is_ipv4(addr) && count) {
2348 struct sockaddr_in answer;
2350 if (!ast_stun_request(component == AST_RTP_ICE_COMPONENT_RTCP ? rtp->rtcp->s : rtp->s, &stunaddr, NULL, &answer)) {
2352 pj_str_t mapped = pj_str(ast_strdupa(ast_inet_ntoa(answer.sin_addr)));
2354 /* Use the first local host candidate as the base */
2355 pj_sockaddr_cp(&base, &address[0]);
2357 pj_sockaddr_init(pj_AF_INET(), &address[0], &mapped, ntohs(answer.sin_port));
2359 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_SRFLX, 65535, &address[0], &base,
2360 &base, pj_sockaddr_get_len(&address[0]));
2364 /* If configured to use a TURN relay create a session and allocate */
2365 if (pj_strlen(&turnaddr)) {
2366 ast_rtp_ice_turn_request(instance, component, AST_TRANSPORT_TCP, pj_strbuf(&turnaddr), turnport,
2367 pj_strbuf(&turnusername), pj_strbuf(&turnpassword));
2374 * \brief Calculates the elapsed time from issue of the first tx packet in an
2375 * rtp session and a specified time
2377 * \param rtp pointer to the rtp struct with the transmitted rtp packet
2378 * \param delivery time of delivery - if NULL or zero value, will be ast_tvnow()
2380 * \return time elapsed in milliseconds
2382 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
2387 if (ast_tvzero(rtp->txcore)) {
2388 rtp->txcore = ast_tvnow();
2389 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
2392 t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
2393 if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) {
2398 return (unsigned int) ms;
2401 #ifdef HAVE_PJPROJECT
2404 * \brief Creates an ICE session. Can be used to replace a destroyed ICE session.
2406 * \param instance RTP instance for which the ICE session is being replaced
2407 * \param addr ast_sockaddr to use for adding RTP candidates to the ICE session
2408 * \param port port to use for adding RTP candidates to the ICE session
2409 * \param replace 0 when creating a new session, 1 when replacing a destroyed session
2411 * \retval 0 on success
2412 * \retval -1 on failure
2414 static int ice_create(struct ast_rtp_instance *instance, struct ast_sockaddr *addr,
2415 int port, int replace)
2417 pj_stun_config stun_config;
2418 pj_str_t ufrag, passwd;
2419 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2421 ao2_cleanup(rtp->ice_local_candidates);
2422 rtp->ice_local_candidates = NULL;
2424 pj_thread_register_check();
2426 pj_stun_config_init(&stun_config, &cachingpool.factory, 0, NULL, timer_heap);
2428 ufrag = pj_str(rtp->local_ufrag);
2429 passwd = pj_str(rtp->local_passwd);
2431 /* Create an ICE session for ICE negotiation */
2432 if (pj_ice_sess_create(&stun_config, NULL, PJ_ICE_SESS_ROLE_UNKNOWN, 2,
2433 &ast_rtp_ice_sess_cb, &ufrag, &passwd, NULL, &rtp->ice) == PJ_SUCCESS) {
2434 /* Make this available for the callbacks */
2435 rtp->ice->user_data = instance;
2437 /* Add all of the available candidates to the ICE session */
2438 rtp_add_candidates_to_ice(instance, rtp, addr, port, AST_RTP_ICE_COMPONENT_RTP,
2439 TRANSPORT_SOCKET_RTP);
2441 /* Only add the RTCP candidates to ICE when replacing the session. New sessions
2442 * handle this in a separate part of the setup phase */
2443 if (replace && rtp->rtcp) {
2444 rtp_add_candidates_to_ice(instance, rtp, &rtp->rtcp->us,
2445 ast_sockaddr_port(&rtp->rtcp->us), AST_RTP_ICE_COMPONENT_RTCP,
2446 TRANSPORT_SOCKET_RTCP);
2457 static int ast_rtp_new(struct ast_rtp_instance *instance,
2458 struct ast_sched_context *sched, struct ast_sockaddr *addr,
2461 struct ast_rtp *rtp = NULL;
2464 /* Create a new RTP structure to hold all of our data */
2465 if (!(rtp = ast_calloc(1, sizeof(*rtp)))) {
2469 /* Initialize synchronization aspects */
2470 ast_mutex_init(&rtp->lock);
2471 ast_cond_init(&rtp->cond, NULL);
2473 /* Set default parameters on the newly created RTP structure */
2474 rtp->ssrc = ast_random();
2475 rtp->seqno = ast_random() & 0xffff;
2476 rtp->strict_rtp_state = (strictrtp ? STRICT_RTP_LEARN : STRICT_RTP_OPEN);
2478 rtp_learning_seq_init(&rtp->rtp_source_learn, (uint16_t)rtp->seqno);
2479 rtp_learning_seq_init(&rtp->alt_source_learn, (uint16_t)rtp->seqno);
2482 /* Create a new socket for us to listen on and use */
2484 create_new_socket("RTP",
2485 ast_sockaddr_is_ipv4(addr) ? AF_INET :
2486 ast_sockaddr_is_ipv6(addr) ? AF_INET6 : -1)) < 0) {
2487 ast_debug(1, "Failed to create a new socket for RTP instance '%p'\n", instance);
2492 /* Now actually find a free RTP port to use */
2493 x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
2498 ast_sockaddr_set_port(addr, x);
2499 /* Try to bind, this will tell us whether the port is available or not */
2500 if (!ast_bind(rtp->s, addr)) {
2501 ast_debug(1, "Allocated port %d for RTP instance '%p'\n", x, instance);
2502 ast_rtp_instance_set_local_address(instance, addr);
2508 x = (rtpstart + 1) & ~1;
2511 /* See if we ran out of ports or if the bind actually failed because of something other than the address being in use */
2512 if (x == startplace || (errno != EADDRINUSE && errno != EACCES)) {
2513 ast_log(LOG_ERROR, "Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance);
2520 #ifdef HAVE_PJPROJECT
2521 generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag));
2522 generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd));
2524 ast_rtp_instance_set_data(instance, rtp);
2525 #ifdef HAVE_PJPROJECT
2526 /* Create an ICE session for ICE negotiation */
2528 if (ice_create(instance, addr, x, 0)) {
2529 ast_log(LOG_NOTICE, "Failed to start ICE session\n");
2532 ast_sockaddr_copy(&rtp->ice_original_rtp_addr, addr);
2537 /* Record any information we may need */
2540 #ifdef HAVE_OPENSSL_SRTP
2542 rtp->dtls.timeout_timer = -1;
2545 rtp->f.subclass.format = ao2_bump(ast_format_none);
2546 rtp->lastrxformat = ao2_bump(ast_format_none);
2547 rtp->lasttxformat = ao2_bump(ast_format_none);
2552 static int ast_rtp_destroy(struct ast_rtp_instance *instance)
2554 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2555 #ifdef HAVE_PJPROJECT
2556 struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_STATE_WAIT_TIME, 1000));
2557 struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
2560 #ifdef HAVE_OPENSSL_SRTP
2561 ast_rtp_dtls_stop(instance);
2564 /* Destroy the smoother that was smoothing out audio if present */
2565 if (rtp->smoother) {
2566 ast_smoother_free(rtp->smoother);
2569 /* Close our own socket so we no longer get packets */
2574 /* Destroy RTCP if it was being used */
2577 * It is not possible for there to be an active RTCP scheduler
2578 * entry at this point since it holds a reference to the
2579 * RTP instance while it's active.
2581 close(rtp->rtcp->s);
2582 ast_free(rtp->rtcp);
2585 /* Destroy RED if it was being used */
2587 AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
2591 #ifdef HAVE_PJPROJECT
2592 pj_thread_register_check();
2594 /* Destroy the RTP TURN relay if being used */
2595 ast_mutex_lock(&rtp->lock);
2596 if (rtp->turn_rtp) {
2597 pj_turn_sock_destroy(rtp->turn_rtp);
2598 rtp->turn_state = PJ_TURN_STATE_NULL;
2599 while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
2600 ast_cond_timedwait(&rtp->cond, &rtp->lock, &ts);
2604 /* Destroy the RTCP TURN relay if being used */
2605 if (rtp->turn_rtcp) {
2606 pj_turn_sock_destroy(rtp->turn_rtcp);
2607 rtp->turn_state = PJ_TURN_STATE_NULL;
2608 while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
2609 ast_cond_timedwait(&rtp->cond, &rtp->lock, &ts);
2612 ast_mutex_unlock(&rtp->lock);
2615 rtp_ioqueue_thread_remove(rtp->ioqueue);
2618 /* Destroy the ICE session if being used */
2620 pj_ice_sess_destroy(rtp->ice);
2623 /* Destroy any candidates */
2624 if (rtp->ice_local_candidates) {
2625 ao2_ref(rtp->ice_local_candidates, -1);
2628 if (rtp->ice_active_remote_candidates) {
2629 ao2_ref(rtp->ice_active_remote_candidates, -1);
2633 ao2_cleanup(rtp->lasttxformat);
2634 ao2_cleanup(rtp->lastrxformat);
2635 ao2_cleanup(rtp->f.subclass.format);
2637 /* Destroy synchronization items */
2638 ast_mutex_destroy(&rtp->lock);
2639 ast_cond_destroy(&rtp->cond);
2641 /* Finally destroy ourselves */
2647 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
2649 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2650 rtp->dtmfmode = dtmf_mode;
2654 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance)
2656 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2657 return rtp->dtmfmode;
2660 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit)
2662 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2663 struct ast_sockaddr remote_address = { {0,} };
2664 int hdrlen = 12, res = 0, i = 0, payload = 101;
2666 unsigned int *rtpheader = (unsigned int*)data;
2668 ast_rtp_instance_get_remote_address(instance, &remote_address);
2670 /* If we have no remote address information bail out now */
2671 if (ast_sockaddr_isnull(&remote_address)) {
2675 /* Convert given digit into what we want to transmit */
2676 if ((digit <= '9') && (digit >= '0')) {
2678 } else if (digit == '*') {
2680 } else if (digit == '#') {
2682 } else if ((digit >= 'A') && (digit <= 'D')) {
2683 digit = digit - 'A' + 12;
2684 } else if ((digit >= 'a') && (digit <= 'd')) {
2685 digit = digit - 'a' + 12;
2687 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
2691 /* Grab the payload that they expect the RFC2833 packet to be received in */
2692 payload = ast_rtp_codecs_payload_code_tx(ast_rtp_instance_get_codecs(instance), 0, NULL, AST_RTP_DTMF);
2694 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2695 rtp->send_duration = 160;
2696 rtp->lastts += calc_txstamp(rtp, NULL) * DTMF_SAMPLE_RATE_MS;
2697 rtp->lastdigitts = rtp->lastts + rtp->send_duration;
2699 /* Create the actual packet that we will be sending */
2700 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
2701 rtpheader[1] = htonl(rtp->lastdigitts);
2702 rtpheader[2] = htonl(rtp->ssrc);
2704 /* Actually send the packet */
2705 for (i = 0; i < 2; i++) {
2708 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
2709 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
2711 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
2712 ast_sockaddr_stringify(&remote_address),
2715 if (rtp_debug_test_addr(&remote_address)) {
2716 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
2717 ast_sockaddr_stringify(&remote_address),
2718 ice ? " (via ICE)" : "",
2719 payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2722 rtp->send_duration += 160;
2723 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
2726 /* Record that we are in the process of sending a digit and information needed to continue doing so */
2727 rtp->sending_digit = 1;
2728 rtp->send_digit = digit;
2729 rtp->send_payload = payload;
2734 static int ast_rtp_dtmf_continuation(struct ast_rtp_instance *instance)
2736 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2737 struct ast_sockaddr remote_address = { {0,} };
2738 int hdrlen = 12, res = 0;
2740 unsigned int *rtpheader = (unsigned int*)data;
2743 ast_rtp_instance_get_remote_address(instance, &remote_address);
2745 /* Make sure we know where the other side is so we can send them the packet */
2746 if (ast_sockaddr_isnull(&remote_address)) {
2750 /* Actually create the packet we will be sending */
2751 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
2752 rtpheader[1] = htonl(rtp->lastdigitts);
2753 rtpheader[2] = htonl(rtp->ssrc);
2754 rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
2756 /* Boom, send it on out */
2757 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
2759 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
2760 ast_sockaddr_stringify(&remote_address),
2764 if (rtp_debug_test_addr(&remote_address)) {
2765 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
2766 ast_sockaddr_stringify(&remote_address),
2767 ice ? " (via ICE)" : "",
2768 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2771 /* And now we increment some values for the next time we swing by */
2773 rtp->send_duration += 160;
2774 rtp->lastts += calc_txstamp(rtp, NULL) * DTMF_SAMPLE_RATE_MS;
2779 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
2781 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2782 struct ast_sockaddr remote_address = { {0,} };
2783 int hdrlen = 12, res = -1, i = 0;
2785 unsigned int *rtpheader = (unsigned int*)data;
2786 unsigned int measured_samples;
2788 ast_rtp_instance_get_remote_address(instance, &remote_address);
2790 /* Make sure we know where the remote side is so we can send them the packet we construct */
2791 if (ast_sockaddr_isnull(&remote_address)) {
2795 /* Convert the given digit to the one we are going to send */
2796 if ((digit <= '9') && (digit >= '0')) {
2798 } else if (digit == '*') {
2800 } else if (digit == '#') {
2802 } else if ((digit >= 'A') && (digit <= 'D')) {
2803 digit = digit - 'A' + 12;
2804 } else if ((digit >= 'a') && (digit <= 'd')) {
2805 digit = digit - 'a' + 12;
2807 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
2811 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2813 if (duration > 0 && (measured_samples = duration * rtp_get_rate(rtp->f.subclass.format) / 1000) > rtp->send_duration) {
2814 ast_debug(2, "Adjusting final end duration from %d to %u\n", rtp->send_duration, measured_samples);
2815 rtp->send_duration = measured_samples;
2818 /* Construct the packet we are going to send */
2819 rtpheader[1] = htonl(rtp->lastdigitts);
2820 rtpheader[2] = htonl(rtp->ssrc);
2821 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
2822 rtpheader[3] |= htonl((1 << 23));
2824 /* Send it 3 times, that's the magical number */
2825 for (i = 0; i < 3; i++) {
2828 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
2830 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
2833 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
2834 ast_sockaddr_stringify(&remote_address),
2838 if (rtp_debug_test_addr(&remote_address)) {
2839 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
2840 ast_sockaddr_stringify(&remote_address),
2841 ice ? " (via ICE)" : "",
2842 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2849 /* Oh and we can't forget to turn off the stuff that says we are sending DTMF */
2850 rtp->lastts += calc_txstamp(rtp, NULL) * DTMF_SAMPLE_RATE_MS;
2852 rtp->sending_digit = 0;
2853 rtp->send_digit = 0;
2858 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit)
2860 return ast_rtp_dtmf_end_with_duration(instance, digit, 0);
2863 static void ast_rtp_update_source(struct ast_rtp_instance *instance)
2865 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2867 /* We simply set this bit so that the next packet sent will have the marker bit turned on */
2868 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
2869 ast_debug(3, "Setting the marker bit due to a source update\n");
2874 static void ast_rtp_change_source(struct ast_rtp_instance *instance)
2876 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2877 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
2878 unsigned int ssrc = ast_random();
2881 ast_debug(3, "Not changing SSRC since we haven't sent any RTP yet\n");
2885 /* We simply set this bit so that the next packet sent will have the marker bit turned on */
2886 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
2888 ast_debug(3, "Changing ssrc from %u to %u due to a source change\n", rtp->ssrc, ssrc);
2891 ast_debug(3, "Changing ssrc for SRTP from %u to %u\n", rtp->ssrc, ssrc);
2892 res_srtp->change_source(srtp, rtp->ssrc, ssrc);
2900 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
2902 unsigned int sec, usec, frac;
2903 sec = tv.tv_sec + 2208988800u; /* Sec between 1900 and 1970 */
2905 frac = (usec << 12) + (usec << 8) - ((usec * 3650) >> 6);
2910 static void ntp2timeval(unsigned int msw, unsigned int lsw, struct timeval *tv)
2912 tv->tv_sec = msw - 2208988800u;
2913 tv->tv_usec = ((lsw << 6) / 3650) - (lsw >> 12) - (lsw >> 8);
2916 static void calculate_lost_packet_statistics(struct ast_rtp *rtp,
2917 unsigned int *lost_packets,
2920 unsigned int extended_seq_no;
2921 unsigned int expected_packets;
2922 unsigned int expected_interval;
2923 unsigned int received_interval;
2924 double rxlost_current;
2927 /* Compute statistics */
2928 extended_seq_no = rtp->cycles + rtp->lastrxseqno;
2929 expected_packets = extended_seq_no - rtp->seedrxseqno + 1;
2930 if (rtp->rxcount > expected_packets) {
2931 expected_packets += rtp->rxcount - expected_packets;
2933 *lost_packets = expected_packets - rtp->rxcount;
2934 expected_interval = expected_packets - rtp->rtcp->expected_prior;
2935 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
2936 lost_interval = expected_interval - received_interval;
2937 if (expected_interval == 0 || lost_interval <= 0) {
2940 *fraction_lost = (lost_interval << 8) / expected_interval;
2943 /* Update RTCP statistics */
2944 rtp->rtcp->received_prior = rtp->rxcount;
2945 rtp->rtcp->expected_prior = expected_packets;
2946 if (lost_interval <= 0) {
2947 rtp->rtcp->rxlost = 0;
2949 rtp->rtcp->rxlost = lost_interval;
2951 if (rtp->rtcp->rxlost_count == 0) {
2952 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
2954 if (lost_interval < rtp->rtcp->minrxlost) {
2955 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
2957 if (lost_interval > rtp->rtcp->maxrxlost) {
2958 rtp->rtcp->maxrxlost = rtp->rtcp->rxlost;
2960 rxlost_current = normdev_compute(rtp->rtcp->normdev_rxlost,
2962 rtp->rtcp->rxlost_count);
2963 rtp->rtcp->stdev_rxlost = stddev_compute(rtp->rtcp->stdev_rxlost,
2965 rtp->rtcp->normdev_rxlost,
2967 rtp->rtcp->rxlost_count);
2968 rtp->rtcp->normdev_rxlost = rxlost_current;
2969 rtp->rtcp->rxlost_count++;
2972 /*! \brief Send RTCP SR or RR report */
2973 static int ast_rtcp_write_report(struct ast_rtp_instance *instance, int sr)
2975 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2976 RAII_VAR(struct ast_json *, message_blob, NULL, ast_json_unref);
2980 unsigned int now_lsw;
2981 unsigned int now_msw;
2982 unsigned int *rtcpheader;
2983 unsigned int lost_packets;
2985 struct timeval dlsr = { 0, };
2987 int rate = rtp_get_rate(rtp->f.subclass.format);
2989 int header_offset = 0;
2990 char *str_remote_address;
2991 char *str_local_address;
2992 struct ast_sockaddr remote_address = { { 0, } };
2993 struct ast_sockaddr local_address = { { 0, } };
2994 struct ast_sockaddr real_remote_address = { { 0, } };
2995 struct ast_sockaddr real_local_address = { { 0, } };
2996 struct ast_rtp_rtcp_report_block *report_block = NULL;
2997 RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report,
2998 ast_rtp_rtcp_report_alloc(rtp->themssrc ? 1 : 0),
3001 if (!rtp || !rtp->rtcp) {
3005 if (ast_sockaddr_isnull(&rtp->rtcp->them)) { /* This'll stop rtcp for this rtp session */
3006 /* RTCP was stopped. */
3014 /* Compute statistics */
3015 calculate_lost_packet_statistics(rtp, &lost_packets, &fraction_lost);
3017 gettimeofday(&now, NULL);
3018 rtcp_report->reception_report_count = rtp->themssrc ? 1 : 0;
3019 rtcp_report->ssrc = rtp->ssrc;
3020 rtcp_report->type = sr ? RTCP_PT_SR : RTCP_PT_RR;
3022 rtcp_report->sender_information.ntp_timestamp = now;
3023 rtcp_report->sender_information.rtp_timestamp = rtp->lastts;
3024 rtcp_report->sender_information.packet_count = rtp->txcount;
3025 rtcp_report->sender_information.octet_count = rtp->txoctetcount;
3028 if (rtp->themssrc) {
3029 report_block = ast_calloc(1, sizeof(*report_block));
3030 if (!report_block) {
3034 rtcp_report->report_block[0] = report_block;
3035 report_block->source_ssrc = rtp->themssrc;
3036 report_block->lost_count.fraction = (fraction_lost & 0xff);
3037 report_block->lost_count.packets = (lost_packets & 0xffffff);
3038 report_block->highest_seq_no = (rtp->cycles | (rtp->lastrxseqno & 0xffff));
3039 report_block->ia_jitter = (unsigned int)(rtp->rxjitter * rate);
3040 report_block->lsr = rtp->rtcp->themrxlsr;
3041 /* If we haven't received an SR report, DLSR should be 0 */
3042 if (!ast_tvzero(rtp->rtcp->rxlsr)) {
3043 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
3044 report_block->dlsr = (((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000;
3047 timeval2ntp(rtcp_report->sender_information.ntp_timestamp, &now_msw, &now_lsw);
3048 rtcpheader = (unsigned int *)bdata;
3049 rtcpheader[1] = htonl(rtcp_report->ssrc); /* Our SSRC */
3053 rtcpheader[2] = htonl(now_msw); /* now, MSW. gettimeofday() + SEC_BETWEEN_1900_AND_1970*/
3054 rtcpheader[3] = htonl(now_lsw); /* now, LSW */
3055 rtcpheader[4] = htonl(rtcp_report->sender_information.rtp_timestamp);
3056 rtcpheader[5] = htonl(rtcp_report->sender_information.packet_count);
3057 rtcpheader[6] = htonl(rtcp_report->sender_information.octet_count);
3061 rtcpheader[2 + header_offset] = htonl(report_block->source_ssrc); /* Their SSRC */
3062 rtcpheader[3 + header_offset] = htonl((report_block->lost_count.fraction << 24) | report_block->lost_count.packets);
3063 rtcpheader[4 + header_offset] = htonl(report_block->highest_seq_no);
3064 rtcpheader[5 + header_offset] = htonl(report_block->ia_jitter);
3065 rtcpheader[6 + header_offset] = htonl(report_block->lsr);
3066 rtcpheader[7 + header_offset] = htonl(report_block->dlsr);
3069 rtcpheader[0] = htonl((2 << 30) | (rtcp_report->reception_report_count << 24)
3070 | ((sr ? RTCP_PT_SR : RTCP_PT_RR) << 16) | ((len/4)-1));
3072 /* Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos */
3073 /* it can change mid call, and SDES can't) */
3074 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
3075 rtcpheader[(len/4)+1] = htonl(rtcp_report->ssrc);
3076 rtcpheader[(len/4)+2] = htonl(0x01 << 24);
3079 ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
3080 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &remote_address, &ice);
3082 ast_log(LOG_ERROR, "RTCP %s transmission error to %s, rtcp halted %s\n",
3084 ast_sockaddr_stringify(&rtp->rtcp->them),
3089 /* Update RTCP SR/RR statistics */
3091 rtp->rtcp->txlsr = rtcp_report->sender_information.ntp_timestamp;
3092 rtp->rtcp->sr_count++;
3093 rtp->rtcp->lastsrtxcount = rtp->txcount;
3095 rtp->rtcp->rr_count++;
3098 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
3099 ast_verbose("* Sent RTCP %s to %s%s\n", sr ? "SR" : "RR",
3100 ast_sockaddr_stringify(&remote_address), ice ? " (via ICE)" : "");
3101 ast_verbose(" Our SSRC: %u\n", rtcp_report->ssrc);
3103 ast_verbose(" Sent(NTP): %u.%010u\n",
3104 (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_sec,
3105 (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_usec * 4096);
3106 ast_verbose(" Sent(RTP): %u\n", rtcp_report->sender_information.rtp_timestamp);
3107 ast_verbose(" Sent packets: %u\n", rtcp_report->sender_information.packet_count);
3108 ast_verbose(" Sent octets: %u\n", rtcp_report->sender_information.octet_count);
3111 ast_verbose(" Report block:\n");
3112 ast_verbose(" Their SSRC: %u\n", report_block->source_ssrc);
3113 ast_verbose(" Fraction lost: %d\n", report_block->lost_count.fraction);
3114 ast_verbose(" Cumulative loss: %u\n", report_block->lost_count.packets);
3115 ast_verbose(" Highest seq no: %u\n", report_block->highest_seq_no);
3116 ast_verbose(" IA jitter: %.4f\n", (double)report_block->ia_jitter / rate);
3117 ast_verbose(" Their last SR: %u\n", report_block->lsr);
3118 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(report_block->dlsr / 65536.0));
3122 ast_rtp_instance_get_local_address(instance, &local_address);
3123 if (!ast_find_ourip(&real_local_address, &local_address, 0)) {
3124 str_local_address = ast_strdupa(ast_sockaddr_stringify(&real_local_address));
3126 str_local_address = ast_strdupa(ast_sockaddr_stringify(&local_address));
3129 if (!ast_find_ourip(&real_remote_address, &remote_address, 0)) {
3130 str_remote_address = ast_strdupa(ast_sockaddr_stringify(&real_remote_address));
3132 str_remote_address = ast_strdupa(ast_sockaddr_stringify(&remote_address));
3135 message_blob = ast_json_pack("{s: s, s: s}",
3136 "to", str_remote_address,
3137 "from", str_local_address);
3138 ast_rtp_publish_rtcp_message(instance, ast_rtp_rtcp_sent_type(),
3144 /*! \brief Write and RTCP packet to the far end
3145 * \note Decide if we are going to send an SR (with Reception Block) or RR
3146 * RR is sent if we have not sent any rtp packets in the previous interval */
3147 static int ast_rtcp_write(const void *data)
3149 struct ast_rtp_instance *instance = (struct ast_rtp_instance *) data;
3150 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3153 if (!rtp || !rtp->rtcp || rtp->rtcp->schedid == -1) {
3154 ao2_ref(instance, -1);
3158 if (rtp->txcount > rtp->rtcp->lastsrtxcount) {
3160 res = ast_rtcp_write_report(instance, 1);
3163 res = ast_rtcp_write_report(instance, 0);
3168 * Not being rescheduled.
3170 ao2_ref(instance, -1);
3171 rtp->rtcp->schedid = -1;
3177 static int ast_rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec)
3179 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3181 unsigned int ms = calc_txstamp(rtp, &frame->delivery);
3182 struct ast_sockaddr remote_address = { {0,} };
3183 int rate = rtp_get_rate(frame->subclass.format) / 1000;
3185 if (ast_format_cmp(frame->subclass.format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) {
3186 frame->samples /= 2;
3189 if (rtp->sending_digit) {
3193 if (frame->frametype == AST_FRAME_VOICE) {
3194 pred = rtp->lastts + frame->samples;
3196 /* Re-calculate last TS */
3197 rtp->lastts = rtp->lastts + ms * rate;
3198 if (ast_tvzero(frame->delivery)) {
3199 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
3200 and if so, go with our prediction */
3201 if (abs((int)rtp->lastts - pred) < MAX_TIMESTAMP_SKEW) {
3204 ast_debug(3, "Difference is %d, ms is %u\n", abs((int)rtp->lastts - pred), ms);
3208 } else if (frame->frametype == AST_FRAME_VIDEO) {
3209 mark = frame->subclass.frame_ending;
3210 pred = rtp->lastovidtimestamp + frame->samples;
3211 /* Re-calculate last TS */
3212 rtp->lastts = rtp->lastts + ms * 90;
3213 /* If it's close to our prediction, go for it */
3214 if (ast_tvzero(frame->delivery)) {
3215 if (abs((int)rtp->lastts - pred) < 7200) {
3217 rtp->lastovidtimestamp += frame->samples;
3219 ast_debug(3, "Difference is %d, ms is %u (%u), pred/ts/samples %u/%d/%d\n", abs((int)rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, frame->samples);
3220 rtp->lastovidtimestamp = rtp->lastts;
3224 pred = rtp->lastotexttimestamp + frame->samples;
3225 /* Re-calculate last TS */
3226 rtp->lastts = rtp->lastts + ms;
3227 /* If it's close to our prediction, go for it */
3228 if (ast_tvzero(frame->delivery)) {
3229 if (abs((int)rtp->lastts - pred) < 7200) {
3231 rtp->lastotexttimestamp += frame->samples;
3233 ast_debug(3, "Difference is %d, ms is %u, pred/ts/samples %u/%d/%d\n", abs((int)rtp->lastts - pred), ms, rtp->lastts, pred, frame->samples);
3234 rtp->lastotexttimestamp = rtp->lastts;
3239 /* If we have been explicitly told to set the marker bit then do so */
3240 if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
3242 ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT);
3245 /* If the timestamp for non-digt packets has moved beyond the timestamp for digits, update the digit timestamp */
3246 if (rtp->lastts > rtp->lastdigitts) {
3247 rtp->lastdigitts = rtp->lastts;
3250 if (ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO)) {
3251 rtp->lastts = frame->ts * rate;
3254 ast_rtp_instance_get_remote_address(instance, &remote_address);
3256 /* If we know the remote address construct a packet and send it out */
3257 if (!ast_sockaddr_isnull(&remote_address)) {
3258 int hdrlen = 12, res, ice;
3259 unsigned char *rtpheader = (unsigned char *)(frame->data.ptr - hdrlen);
3261 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
3262 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
3263 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
3265 if ((res = rtp_sendto(instance, (void *)rtpheader, frame->datalen + hdrlen, 0, &remote_address, &ice)) < 0) {
3266 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))) {
3267 ast_debug(1, "RTP Transmission error of packet %d to %s: %s\n",
3269 ast_sockaddr_stringify(&remote_address),
3271 } else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) {
3272 /* Only give this error message once if we are not RTP debugging */
3274 ast_debug(0, "RTP NAT: Can't write RTP to private address %s, waiting for other end to send audio...\n",
3275 ast_sockaddr_stringify(&remote_address));
3276 ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN);
3280 rtp->txoctetcount += (res - hdrlen);
3282 if (rtp->rtcp && rtp->rtcp->schedid < 1) {
3283 ast_debug(1, "Starting RTCP transmission on RTP instance '%p'\n", instance);
3284 ao2_ref(instance, +1);
3285 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, instance);
3286 if (rtp->rtcp->schedid < 0) {
3287 ao2_ref(instance, -1);
3288 ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n");
3293 if (rtp_debug_test_addr(&remote_address)) {
3294 ast_verbose("Sent RTP packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
3295 ast_sockaddr_stringify(&remote_address),
3296 ice ? " (via ICE)" : "",
3297 codec, rtp->seqno, rtp->lastts, res - hdrlen);
3306 static struct ast_frame *red_t140_to_red(struct rtp_red *red) {
3307 unsigned char *data = red->t140red.data.ptr;
3311 /* replace most aged generation */
3313 for (i = 1; i < red->num_gen+1; i++)
3316 memmove(&data[red->hdrlen], &data[red->hdrlen+red->len[0]], len);
3319 /* Store length of each generation and primary data length*/
3320 for (i = 0; i < red->num_gen; i++)
3321 red->len[i] = red->len[i+1];
3322 red->len[i] = red->t140.datalen;
3324 /* write each generation length in red header */
3326 for (i = 0; i < red->num_gen; i++) {
3327 len += data[i*4+3] = red->len[i];
3330 /* add primary data to buffer */
3331 memcpy(&data[len], red->t140.data.ptr, red->t140.datalen);
3332 red->t140red.datalen = len + red->t140.datalen;
3334 /* no primary data and no generations to send */
3335 if (len == red->hdrlen && !red->t140.datalen) {
3339 /* reset t.140 buffer */
3340 red->t140.datalen = 0;
3342 return &red->t140red;
3345 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
3347 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3348 struct ast_sockaddr remote_address = { {0,} };
3349 struct ast_format *format;
3352 ast_rtp_instance_get_remote_address(instance, &remote_address);
3354 /* If we don't actually know the remote address don't even bother doing anything */
3355 if (ast_sockaddr_isnull(&remote_address)) {
3356 ast_debug(1, "No remote address on RTP instance '%p' so dropping frame\n", instance);
3360 /* VP8: is this a request to send a RTCP FIR? */
3361 if (frame->frametype == AST_FRAME_CONTROL && frame->subclass.integer == AST_CONTROL_VIDUPDATE) {
3362 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3363 unsigned int *rtcpheader;
3369 if (!rtp || !rtp->rtcp) {
3373 if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
3380 /* Prepare RTCP FIR (PT=206, FMT=4) */
3381 rtp->rtcp->firseq++;
3382 if(rtp->rtcp->firseq == 256) {
3383 rtp->rtcp->firseq = 0;
3386 rtcpheader = (unsigned int *)bdata;
3387 rtcpheader[0] = htonl((2 << 30) | (4 << 24) | (RTCP_PT_PSFB << 16) | ((len/4)-1));
3388 rtcpheader[1] = htonl(rtp->ssrc);
3389 rtcpheader[2] = htonl(rtp->themssrc);
3390 rtcpheader[3] = htonl(rtp->themssrc); /* FCI: SSRC */
3391 rtcpheader[4] = htonl(rtp->rtcp->firseq << 24); /* FCI: Sequence number */
3392 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &rtp->rtcp->them, &ice);
3394 ast_log(LOG_ERROR, "RTCP FIR transmission error: %s\n", strerror(errno));
3399 /* If there is no data length we can't very well send the packet */
3400 if (!frame->datalen) {
3401 ast_debug(1, "Received frame with no data for RTP instance '%p' so dropping frame\n", instance);
3405 /* If the packet is not one our RTP stack supports bail out */
3406 if (frame->frametype != AST_FRAME_VOICE && frame->frametype != AST_FRAME_VIDEO && frame->frametype != AST_FRAME_TEXT) {
3407 ast_log(LOG_WARNING, "RTP can only send voice, video, and text\n");
3413 /* no primary data or generations to send */
3414 if ((frame = red_t140_to_red(rtp->red)) == NULL)
3418 /* Grab the subclass and look up the payload we are going to use */
3419 codec = ast_rtp_codecs_payload_code_tx(ast_rtp_instance_get_codecs(instance),
3420 1, frame->subclass.format, 0);
3422 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n",
3423 ast_format_get_name(frame->subclass.format));
3427 /* Note that we do not increase the ref count here as this pointer
3428 * will not be held by any thing explicitly. The format variable is
3429 * merely a convenience reference to frame->subclass.format */
3430 format = frame->subclass.format;
3431 if (ast_format_cmp(rtp->lasttxformat, format) == AST_FORMAT_CMP_NOT_EQUAL) {
3432 /* Oh dear, if the format changed we will have to set up a new smoother */
3433 if (option_debug > 0) {
3434 ast_debug(1, "Ooh, format changed from %s to %s\n",
3435 ast_format_get_name(rtp->lasttxformat),
3436 ast_format_get_name(frame->subclass.format));
3438 ao2_replace(rtp->lasttxformat, format);
3439 if (rtp->smoother) {
3440 ast_smoother_free(rtp->smoother);
3441 rtp->smoother = NULL;
3445 /* If no smoother is present see if we have to set one up */
3446 if (!rtp->smoother && ast_format_can_be_smoothed(format)) {
3447 unsigned int framing_ms = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(instance));
3450 rtp->smoother = ast_smoother_new((framing_ms * ast_format_get_minimum_bytes(format)) / ast_format_get_minimum_ms(format));
3451 if (!rtp->smoother) {
3452 ast_log(LOG_WARNING, "Unable to create smoother: format %s ms: %u len: %u\n",
3453 ast_format_get_name(format), framing_ms, ast_format_get_minimum_bytes(format));
3459 /* Feed audio frames into the actual function that will create a frame and send it */
3460 if (rtp->smoother) {
3461 struct ast_frame *f;
3463 if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
3464 ast_smoother_feed_be(rtp->smoother, frame);
3466 ast_smoother_feed(rtp->smoother, frame);
3469 while ((f = ast_smoother_read(rtp->smoother)) && (f->data.ptr)) {
3470 ast_rtp_raw_write(instance, f, codec);
3474 struct ast_frame *f = NULL;
3476 if (frame->offset < hdrlen) {
3477 f = ast_frdup(frame);
3482 ast_rtp_raw_write(instance, f, codec);
3493 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
3498 double current_time;
3502 int rate = rtp_get_rate(rtp->f.subclass.format);
3504 double normdev_rxjitter_current;
3505 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
3506 gettimeofday(&rtp->rxcore, NULL);
3507 rtp->drxcore = (double) rtp->rxcore.tv_sec + (double) rtp->rxcore.tv_usec / 1000000;
3508 /* map timestamp to a real time */
3509 rtp->seedrxts = timestamp; /* Their RTP timestamp started with this */
3510 tmp = ast_samp2tv(timestamp, rate);
3511 rtp->rxcore = ast_tvsub(rtp->rxcore, tmp);
3512 /* Round to 0.1ms for nice, pretty timestamps */
3513 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
3516 gettimeofday(&now,NULL);
3517 /* rxcore is the mapping between the RTP timestamp and _our_ real time from gettimeofday() */
3518 tmp = ast_samp2tv(timestamp, rate);
3519 *tv = ast_tvadd(rtp->rxcore, tmp);
3521 prog = (double)((timestamp-rtp->seedrxts)/(float)(rate));
3522 dtv = (double)rtp->drxcore + (double)(prog);
3523 current_time = (double)now.tv_sec + (double)now.tv_usec/1000000;
3524 transit = current_time - dtv;
3525 d = transit - rtp->rxtransit;
3526 rtp->rxtransit = transit;
3530 rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
3532 if (rtp->rxjitter > rtp->rtcp->maxrxjitter)
3533 rtp->rtcp->maxrxjitter = rtp->rxjitter;
3534 if (rtp->rtcp->rxjitter_count == 1)
3535 rtp->rtcp->minrxjitter = rtp->rxjitter;
3536 if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter)
3537 rtp->rtcp->minrxjitter = rtp->rxjitter;
3539 normdev_rxjitter_current = normdev_compute(rtp->rtcp->normdev_rxjitter,rtp->rxjitter,rtp->rtcp->rxjitter_count);
3540 rtp->rtcp->stdev_rxjitter = stddev_compute(rtp->rtcp->stdev_rxjitter,rtp->rxjitter,rtp->rtcp->normdev_rxjitter,normdev_rxjitter_current,rtp->rtcp->rxjitter_count);
3542 rtp->rtcp->normdev_rxjitter = normdev_rxjitter_current;
3543 rtp->rtcp->rxjitter_count++;
3547 static struct ast_frame *create_dtmf_frame(struct ast_rtp_instance *instance, enum ast_frame_type type, int compensate)
3549 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3550 struct ast_sockaddr remote_address = { {0,} };
3552 ast_rtp_instance_get_remote_address(instance, &remote_address);
3554 if (((compensate && type == AST_FRAME_DTMF_END) || (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
3555 ast_debug(1, "Ignore potential DTMF echo from '%s'\n",
3556 ast_sockaddr_stringify(&remote_address));
3558 rtp->dtmfsamples = 0;
3559 return &ast_null_frame;
3561 ast_debug(1, "Creating %s DTMF Frame: %d (%c), at %s\n",
3562 type == AST_FRAME_DTMF_END ? "END" : "BEGIN",
3563 rtp->resp, rtp->resp,
3564 ast_sockaddr_stringify(&remote_address));
3565 if (rtp->resp == 'X') {
3566 rtp->f.frametype = AST_FRAME_CONTROL;
3567 rtp->f.subclass.integer = AST_CONTROL_FLASH;
3569 rtp->f.frametype = type;
3570 rtp->f.subclass.integer = rtp->resp;
3576 AST_LIST_NEXT(&rtp->f, frame_list) = NULL;
3581 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)
3583 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3584 struct ast_sockaddr remote_address = { {0,} };
3585 unsigned int event, event_end, samples;
3587 struct ast_frame *f = NULL;
3589 ast_rtp_instance_get_remote_address(instance, &remote_address);
3591 /* Figure out event, event end, and samples */
3592 event = ntohl(*((unsigned int *)(data)));
3594 event_end = ntohl(*((unsigned int *)(data)));
3597 samples = ntohl(*((unsigned int *)(data)));
3600 if (rtp_debug_test_addr(&remote_address)) {
3601 ast_verbose("Got RTP RFC2833 from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d, mark %d, event %08x, end %d, duration %-5.5u) \n",
3602 ast_sockaddr_stringify(&remote_address),
3603 payloadtype, seqno, timestamp, len, (mark?1:0), event, ((event_end & 0x80)?1:0), samples);
3606 /* Print out debug if turned on */
3608 ast_debug(0, "- RTP 2833 Event: %08x (len = %d)\n", event, len);
3610 /* Figure out what digit was pressed */
3613 } else if (event < 11) {
3615 } else if (event < 12) {
3617 } else if (event < 16) {
3618 resp = 'A' + (event - 12);
3619 } else if (event < 17) { /* Event 16: Hook flash */
3622 /* Not a supported event */
3623 ast_debug(1, "Ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", event);
3627 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) {
3628 if ((rtp->last_end_timestamp != timestamp) || (rtp->resp && rtp->resp != resp)) {
3630 rtp->dtmf_timeout = 0;
3631 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)));
3633 rtp->last_end_timestamp = timestamp;
3634 AST_LIST_INSERT_TAIL(frames, f, frame_list);
3637 /* The duration parameter measures the complete
3638 duration of the event (from the beginning) - RFC2833.
3639 Account for the fact that duration is only 16 bits long
3640 (about 8 seconds at 8000 Hz) and can wrap is digit
3641 is hold for too long. */
3642 unsigned int new_duration = rtp->dtmf_duration;
3643 unsigned int last_duration = new_duration & 0xFFFF;
3645 if (last_duration > 64000 && samples < last_duration) {
3646 new_duration += 0xFFFF + 1;
3648 new_duration = (new_duration & ~0xFFFF) | samples;
3650 if (event_end & 0x80) {
3652 if ((rtp->last_seqno != seqno) && (timestamp > rtp->last_end_timestamp)) {
3653 rtp->last_end_timestamp = timestamp;
3654 rtp->dtmf_duration = new_duration;
3656 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
3657 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass.format)), ast_tv(0, 0));
3659 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
3660 AST_LIST_INSERT_TAIL(frames, f, frame_list);
3661 } else if (rtpdebug) {
3662 ast_debug(1, "Dropping duplicate or out of order DTMF END frame (seqno: %u, ts %u, digit %c)\n",
3663 seqno, timestamp, resp);
3666 /* Begin/continuation */
3668 /* The second portion of the seqno check is to not mistakenly
3669 * stop accepting DTMF if the seqno rolls over beyond
3672 if ((rtp->last_seqno > seqno && rtp->last_seqno - seqno < 50)
3673 || timestamp <= rtp->last_end_timestamp) {
3674 /* Out of order frame. Processing this can cause us to
3675 * improperly duplicate incoming DTMF, so just drop
3679 ast_debug(1, "Dropping out of order DTMF frame (seqno %u, ts %u, digit %c)\n",
3680 seqno, timestamp, resp);
3685 if (rtp->resp && rtp->resp != resp) {
3686 /* Another digit already began. End it */
3687 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
3688 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass.format)), ast_tv(0, 0));