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_FILE_VERSION(__FILE__, "$Revision$")
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/channel.h"
60 #include "asterisk/acl.h"
61 #include "asterisk/config.h"
62 #include "asterisk/lock.h"
63 #include "asterisk/utils.h"
64 #include "asterisk/cli.h"
65 #include "asterisk/manager.h"
66 #include "asterisk/unaligned.h"
67 #include "asterisk/module.h"
68 #include "asterisk/rtp_engine.h"
69 #include "asterisk/test.h"
71 #define MAX_TIMESTAMP_SKEW 640
73 #define RTP_SEQ_MOD (1<<16) /*!< A sequence number can't be more than 16 bits */
74 #define RTCP_DEFAULT_INTERVALMS 5000 /*!< Default milli-seconds between RTCP reports we send */
75 #define RTCP_MIN_INTERVALMS 500 /*!< Min milli-seconds between RTCP reports we send */
76 #define RTCP_MAX_INTERVALMS 60000 /*!< Max milli-seconds between RTCP reports we send */
78 #define DEFAULT_RTP_START 5000 /*!< Default port number to start allocating RTP ports from */
79 #define DEFAULT_RTP_END 31000 /*!< Default maximum port number to end allocating RTP ports at */
81 #define MINIMUM_RTP_PORT 1024 /*!< Minimum port number to accept */
82 #define MAXIMUM_RTP_PORT 65535 /*!< Maximum port number to accept */
84 #define DEFAULT_TURN_PORT 34780
86 #define TURN_ALLOCATION_WAIT_TIME 2000
88 #define RTCP_PT_FUR 192
89 #define RTCP_PT_SR AST_RTP_RTCP_SR
90 #define RTCP_PT_RR AST_RTP_RTCP_RR
91 #define RTCP_PT_SDES 202
92 #define RTCP_PT_BYE 203
93 #define RTCP_PT_APP 204
94 /* VP8: RTCP Feedback */
95 #define RTCP_PT_PSFB 206
98 #define DTMF_SAMPLE_RATE_MS 8 /*!< DTMF samples per millisecond */
100 #define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000)) /*!< samples */
102 #define ZFONE_PROFILE_ID 0x505a
104 #define DEFAULT_LEARNING_MIN_SEQUENTIAL 4
106 #define SRTP_MASTER_KEY_LEN 16
107 #define SRTP_MASTER_SALT_LEN 14
108 #define SRTP_MASTER_LEN (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN)
110 enum strict_rtp_state {
111 STRICT_RTP_OPEN = 0, /*! No RTP packets should be dropped, all sources accepted */
112 STRICT_RTP_LEARN, /*! Accept next packet as source */
113 STRICT_RTP_CLOSED, /*! Drop all RTP packets not coming from source that was learned */
116 #define DEFAULT_STRICT_RTP STRICT_RTP_CLOSED
117 #define DEFAULT_ICESUPPORT 1
119 extern struct ast_srtp_res *res_srtp;
120 extern struct ast_srtp_policy_res *res_srtp_policy;
122 static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
124 static int rtpstart = DEFAULT_RTP_START; /*!< First port for RTP sessions (set in rtp.conf) */
125 static int rtpend = DEFAULT_RTP_END; /*!< Last port for RTP sessions (set in rtp.conf) */
126 static int rtpdebug; /*!< Are we debugging? */
127 static int rtcpdebug; /*!< Are we debugging RTCP? */
128 static int rtcpstats; /*!< Are we debugging RTCP? */
129 static int rtcpinterval = RTCP_DEFAULT_INTERVALMS; /*!< Time between rtcp reports in millisecs */
130 static struct ast_sockaddr rtpdebugaddr; /*!< Debug packets to/from this host */
131 static struct ast_sockaddr rtcpdebugaddr; /*!< Debug RTCP packets to/from this host */
132 static int rtpdebugport; /*< Debug only RTP packets from IP or IP+Port if port is > 0 */
133 static int rtcpdebugport; /*< Debug only RTCP packets from IP or IP+Port if port is > 0 */
135 static int nochecksums;
137 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. */
138 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. */
139 #ifdef HAVE_PJPROJECT
140 static int icesupport = DEFAULT_ICESUPPORT;
141 static struct sockaddr_in stunaddr;
142 static pj_str_t turnaddr;
143 static int turnport = DEFAULT_TURN_PORT;
144 static pj_str_t turnusername;
145 static pj_str_t turnpassword;
147 /*! \brief Pool factory used by pjlib to allocate memory. */
148 static pj_caching_pool cachingpool;
150 /*! \brief Pool used by pjlib functions which require memory allocation. */
151 static pj_pool_t *pool;
153 /*! \brief I/O queue for TURN relay traffic */
154 static pj_ioqueue_t *ioqueue;
156 /*! \brief Timer heap for ICE and TURN stuff */
157 static pj_timer_heap_t *timerheap;
159 /*! \brief Worker thread for ICE/TURN */
160 static pj_thread_t *thread;
162 /*! \brief Notification that the ICE/TURN worker thread should stop */
163 static int worker_terminate;
166 #define FLAG_3389_WARNING (1 << 0)
167 #define FLAG_NAT_ACTIVE (3 << 1)
168 #define FLAG_NAT_INACTIVE (0 << 1)
169 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
170 #define FLAG_NEED_MARKER_BIT (1 << 3)
171 #define FLAG_DTMF_COMPENSATE (1 << 4)
173 #define TRANSPORT_SOCKET_RTP 1
174 #define TRANSPORT_SOCKET_RTCP 2
175 #define TRANSPORT_TURN_RTP 3
176 #define TRANSPORT_TURN_RTCP 4
178 /*! \brief RTP learning mode tracking information */
179 struct rtp_learning_info {
180 int max_seq; /*!< The highest sequence number received */
181 int packets; /*!< The number of remaining packets before the source is accepted */
184 /*! \brief RTP session description */
188 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
189 unsigned int ssrc; /*!< Synchronization source, RFC 3550, page 10. */
190 unsigned int themssrc; /*!< Their SSRC */
193 unsigned int lastrxts;
194 unsigned int lastividtimestamp;
195 unsigned int lastovidtimestamp;
196 unsigned int lastitexttimestamp;
197 unsigned int lastotexttimestamp;
198 unsigned int lasteventseqn;
199 int lastrxseqno; /*!< Last received sequence number */
200 unsigned short seedrxseqno; /*!< What sequence number did they start with?*/
201 unsigned int seedrxts; /*!< What RTP timestamp did they start with? */
202 unsigned int rxcount; /*!< How many packets have we received? */
203 unsigned int rxoctetcount; /*!< How many octets have we received? should be rxcount *160*/
204 unsigned int txcount; /*!< How many packets have we sent? */
205 unsigned int txoctetcount; /*!< How many octets have we sent? (txcount*160)*/
206 unsigned int cycles; /*!< Shifted count of sequence number cycles */
207 double rxjitter; /*!< Interarrival jitter at the moment in seconds */
208 double rxtransit; /*!< Relative transit time for previous packet */
209 struct ast_format lasttxformat;
210 struct ast_format lastrxformat;
212 int rtptimeout; /*!< RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
213 int rtpholdtimeout; /*!< RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
214 int rtpkeepalive; /*!< Send RTP comfort noice packets for keepalive */
216 /* DTMF Reception Variables */
217 char resp; /*!< The current digit being processed */
218 unsigned int last_seqno; /*!< The last known sequence number for any DTMF packet */
219 unsigned int last_end_timestamp; /*!< The last known timestamp received from an END packet */
220 unsigned int dtmf_duration; /*!< Total duration in samples since the digit start event */
221 unsigned int dtmf_timeout; /*!< When this timestamp is reached we consider END frame lost and forcibly abort digit */
222 unsigned int dtmfsamples;
223 enum ast_rtp_dtmf_mode dtmfmode; /*!< The current DTMF mode of the RTP stream */
224 /* DTMF Transmission Variables */
225 unsigned int lastdigitts;
226 char sending_digit; /*!< boolean - are we sending digits */
227 char send_digit; /*!< digit we are sending */
231 struct timeval rxcore;
232 struct timeval txcore;
233 double drxcore; /*!< The double representation of the first received packet */
234 struct timeval lastrx; /*!< timeval when we last received a packet */
235 struct timeval dtmfmute;
236 struct ast_smoother *smoother;
238 unsigned short seqno; /*!< Sequence number, RFC 3550, page 13. */
239 unsigned short rxseqno;
240 struct ast_sched_context *sched;
241 struct io_context *io;
243 struct ast_rtcp *rtcp;
244 struct ast_rtp *bridged; /*!< Who we are Packet bridged to */
246 enum strict_rtp_state strict_rtp_state; /*!< Current state that strict RTP protection is in */
247 struct ast_sockaddr strict_rtp_address; /*!< Remote address information for strict RTP purposes */
250 * Learning mode values based on pjmedia's probation mode. Many of these values are redundant to the above,
251 * but these are in place to keep learning mode sequence values sealed from their normal counterparts.
253 struct rtp_learning_info rtp_source_learn; /* Learning mode track for the expected RTP source */
254 struct rtp_learning_info alt_source_learn; /* Learning mode tracking for a new RTP source after one has been chosen */
258 ast_mutex_t lock; /*!< Lock for synchronization purposes */
259 ast_cond_t cond; /*!< Condition for signaling */
261 #ifdef HAVE_PJPROJECT
262 pj_ice_sess *ice; /*!< ICE session */
263 pj_turn_sock *turn_rtp; /*!< RTP TURN relay */
264 pj_turn_sock *turn_rtcp; /*!< RTCP TURN relay */
265 pj_turn_state_t turn_state; /*!< Current state of the TURN relay session */
266 unsigned int passthrough:1; /*!< Bit to indicate that the received packet should be passed through */
267 unsigned int ice_port; /*!< Port that ICE was started with if it was previously started */
269 char remote_ufrag[256]; /*!< The remote ICE username */
270 char remote_passwd[256]; /*!< The remote ICE password */
272 char local_ufrag[256]; /*!< The local ICE username */
273 char local_passwd[256]; /*!< The local ICE password */
275 struct ao2_container *ice_local_candidates; /*!< The local ICE candidates */
276 struct ao2_container *ice_active_remote_candidates; /*!< The remote ICE candidates */
277 struct ao2_container *ice_proposed_remote_candidates; /*!< Incoming remote ICE candidates for new session */
278 struct ast_sockaddr ice_original_rtp_addr; /*!< rtp address that ICE started on first session */
281 #ifdef HAVE_OPENSSL_SRTP
282 SSL_CTX *ssl_ctx; /*!< SSL context */
283 SSL *ssl; /*!< SSL session */
284 BIO *read_bio; /*!< Memory buffer for reading */
285 BIO *write_bio; /*!< Memory buffer for writing */
286 enum ast_rtp_dtls_setup dtls_setup; /*!< Current setup state */
287 enum ast_srtp_suite suite; /*!< SRTP crypto suite */
288 char local_fingerprint[160]; /*!< Fingerprint of our certificate */
289 unsigned char remote_fingerprint[EVP_MAX_MD_SIZE]; /*!< Fingerprint of the peer certificate */
290 enum ast_rtp_dtls_connection connection; /*!< Whether this is a new or existing connection */
291 unsigned int dtls_failure:1; /*!< Failure occurred during DTLS negotiation */
292 unsigned int rekey; /*!< Interval at which to renegotiate and rekey */
293 int rekeyid; /*!< Scheduled item id for rekeying */
298 * \brief Structure defining an RTCP session.
300 * The concept "RTCP session" is not defined in RFC 3550, but since
301 * this structure is analogous to ast_rtp, which tracks a RTP session,
302 * it is logical to think of this as a RTCP session.
304 * RTCP packet is defined on page 9 of RFC 3550.
309 int s; /*!< Socket */
310 struct ast_sockaddr us; /*!< Socket representation of the local endpoint. */
311 struct ast_sockaddr them; /*!< Socket representation of the remote endpoint. */
312 unsigned int soc; /*!< What they told us */
313 unsigned int spc; /*!< What they told us */
314 unsigned int themrxlsr; /*!< The middle 32 bits of the NTP timestamp in the last received SR*/
315 struct timeval rxlsr; /*!< Time when we got their last SR */
316 struct timeval txlsr; /*!< Time when we sent or last SR*/
317 unsigned int expected_prior; /*!< no. packets in previous interval */
318 unsigned int received_prior; /*!< no. packets received in previous interval */
319 int schedid; /*!< Schedid returned from ast_sched_add() to schedule RTCP-transmissions*/
320 unsigned int rr_count; /*!< number of RRs we've sent, not including report blocks in SR's */
321 unsigned int sr_count; /*!< number of SRs we've sent */
322 unsigned int lastsrtxcount; /*!< Transmit packet count when last SR sent */
323 double accumulated_transit; /*!< accumulated a-dlsr-lsr */
324 double rtt; /*!< Last reported rtt */
325 unsigned int reported_jitter; /*!< The contents of their last jitter entry in the RR */
326 unsigned int reported_lost; /*!< Reported lost packets in their RR */
328 double reported_maxjitter;
329 double reported_minjitter;
330 double reported_normdev_jitter;
331 double reported_stdev_jitter;
332 unsigned int reported_jitter_count;
334 double reported_maxlost;
335 double reported_minlost;
336 double reported_normdev_lost;
337 double reported_stdev_lost;
342 double normdev_rxlost;
344 unsigned int rxlost_count;
348 double normdev_rxjitter;
349 double stdev_rxjitter;
350 unsigned int rxjitter_count;
355 unsigned int rtt_count;
357 /* VP8: sequence number for the RTCP FIR FCI */
362 struct ast_frame t140; /*!< Primary data */
363 struct ast_frame t140red; /*!< Redundant t140*/
364 unsigned char pt[AST_RED_MAX_GENERATION]; /*!< Payload types for redundancy data */
365 unsigned char ts[AST_RED_MAX_GENERATION]; /*!< Time stamps */
366 unsigned char len[AST_RED_MAX_GENERATION]; /*!< length of each generation */
367 int num_gen; /*!< Number of generations */
368 int schedid; /*!< Timer id */
369 int ti; /*!< How long to buffer data before send */
370 unsigned char t140red_data[64000];
371 unsigned char buf_data[64000]; /*!< buffered primary data */
376 AST_LIST_HEAD_NOLOCK(frame_list, ast_frame);
378 /* Forward Declarations */
379 static int ast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data);
380 static int ast_rtp_destroy(struct ast_rtp_instance *instance);
381 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit);
382 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit);
383 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration);
384 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode);
385 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance);
386 static void ast_rtp_update_source(struct ast_rtp_instance *instance);
387 static void ast_rtp_change_source(struct ast_rtp_instance *instance);
388 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame);
389 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp);
390 static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value);
391 static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp);
392 static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
393 static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations);
394 static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame);
395 static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1);
396 static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat);
397 static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1);
398 static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username);
399 static void ast_rtp_stop(struct ast_rtp_instance *instance);
400 static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char* desc);
401 static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level);
403 #ifdef HAVE_OPENSSL_SRTP
404 static int ast_rtp_activate(struct ast_rtp_instance *instance);
407 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);
409 /*! \brief Helper function which updates an ast_sockaddr with the candidate used for the component */
410 static void update_address_with_ice_candidate(struct ast_rtp *rtp, int component, struct ast_sockaddr *cand_address)
412 #ifdef HAVE_PJPROJECT
413 char address[PJ_INET6_ADDRSTRLEN];
415 if (!rtp->ice || (component < 1) || !rtp->ice->comp[component - 1].valid_check) {
419 ast_sockaddr_parse(cand_address, pj_sockaddr_print(&rtp->ice->comp[component - 1].valid_check->rcand->addr, address, sizeof(address), 0), 0);
420 ast_sockaddr_set_port(cand_address, pj_sockaddr_get_port(&rtp->ice->comp[component - 1].valid_check->rcand->addr));
424 #ifdef HAVE_PJPROJECT
425 /*! \brief Destructor for locally created ICE candidates */
426 static void ast_rtp_ice_candidate_destroy(void *obj)
428 struct ast_rtp_engine_ice_candidate *candidate = obj;
430 if (candidate->foundation) {
431 ast_free(candidate->foundation);
434 if (candidate->transport) {
435 ast_free(candidate->transport);
439 static void ast_rtp_ice_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
441 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
443 if (!ast_strlen_zero(ufrag)) {
444 ast_copy_string(rtp->remote_ufrag, ufrag, sizeof(rtp->remote_ufrag));
447 if (!ast_strlen_zero(password)) {
448 ast_copy_string(rtp->remote_passwd, password, sizeof(rtp->remote_passwd));
452 static int ice_candidate_cmp(void *obj, void *arg, int flags)
454 struct ast_rtp_engine_ice_candidate *candidate1 = obj, *candidate2 = arg;
456 if (strcmp(candidate1->foundation, candidate2->foundation) ||
457 candidate1->id != candidate2->id ||
458 ast_sockaddr_cmp(&candidate1->address, &candidate2->address) ||
459 candidate1->type != candidate1->type) {
463 return CMP_MATCH | CMP_STOP;
466 static void ast_rtp_ice_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
468 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
469 struct ast_rtp_engine_ice_candidate *remote_candidate;
471 if (!rtp->ice_proposed_remote_candidates &&
472 !(rtp->ice_proposed_remote_candidates = ao2_container_alloc(1, NULL, ice_candidate_cmp))) {
476 /* If this is going to exceed the maximum number of ICE candidates don't even add it */
477 if (ao2_container_count(rtp->ice_proposed_remote_candidates) == PJ_ICE_MAX_CAND) {
481 if (!(remote_candidate = ao2_alloc(sizeof(*remote_candidate), ast_rtp_ice_candidate_destroy))) {
485 remote_candidate->foundation = ast_strdup(candidate->foundation);
486 remote_candidate->id = candidate->id;
487 remote_candidate->transport = ast_strdup(candidate->transport);
488 remote_candidate->priority = candidate->priority;
489 ast_sockaddr_copy(&remote_candidate->address, &candidate->address);
490 ast_sockaddr_copy(&remote_candidate->relay_address, &candidate->relay_address);
491 remote_candidate->type = candidate->type;
493 ao2_link(rtp->ice_proposed_remote_candidates, remote_candidate);
494 ao2_ref(remote_candidate, -1);
497 AST_THREADSTORAGE(pj_thread_storage);
499 /*! \brief Function used to check if the calling thread is registered with pjlib. If it is not it will be registered. */
500 static void pj_thread_register_check(void)
502 pj_thread_desc *desc;
505 if (pj_thread_is_registered() == PJ_TRUE) {
509 desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc));
511 ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage. Expect awful things to occur\n");
514 pj_bzero(*desc, sizeof(*desc));
516 if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
517 ast_log(LOG_ERROR, "Coudln't register thread with PJLIB.\n");
522 static int ice_create(struct ast_rtp_instance *instance, struct ast_sockaddr *addr,
523 int port, int replace);
525 static void ast_rtp_ice_stop(struct ast_rtp_instance *instance)
527 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
533 pj_thread_register_check();
535 pj_ice_sess_destroy(rtp->ice);
539 static int ice_reset_session(struct ast_rtp_instance *instance)
541 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
543 ast_rtp_ice_stop(instance);
544 return ice_create(instance, &rtp->ice_original_rtp_addr, rtp->ice_port, 1);
547 static int ice_candidates_compare(struct ao2_container *left, struct ao2_container *right)
549 struct ao2_iterator i;
550 struct ast_rtp_engine_ice_candidate *right_candidate;
552 if (ao2_container_count(left) != ao2_container_count(right)) {
556 i = ao2_iterator_init(right, 0);
557 while ((right_candidate = ao2_iterator_next(&i))) {
558 struct ast_rtp_engine_ice_candidate *left_candidate = ao2_find(left, right_candidate, OBJ_POINTER);
560 if (!left_candidate) {
561 ao2_ref(right_candidate, -1);
562 ao2_iterator_destroy(&i);
566 ao2_ref(left_candidate, -1);
567 ao2_ref(right_candidate, -1);
569 ao2_iterator_destroy(&i);
574 static void ast_rtp_ice_start(struct ast_rtp_instance *instance)
576 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
577 pj_str_t ufrag = pj_str(rtp->remote_ufrag), passwd = pj_str(rtp->remote_passwd);
578 pj_ice_sess_cand candidates[PJ_ICE_MAX_CAND];
579 struct ao2_iterator i;
580 struct ast_rtp_engine_ice_candidate *candidate;
583 if (!rtp->ice || !rtp->ice_proposed_remote_candidates) {
587 /* Check for equivalence in the lists */
588 if (rtp->ice_active_remote_candidates &&
589 !ice_candidates_compare(rtp->ice_proposed_remote_candidates, rtp->ice_active_remote_candidates)) {
590 ao2_cleanup(rtp->ice_proposed_remote_candidates);
591 rtp->ice_proposed_remote_candidates = NULL;
595 /* Out with the old, in with the new */
596 ao2_cleanup(rtp->ice_active_remote_candidates);
597 rtp->ice_active_remote_candidates = rtp->ice_proposed_remote_candidates;
598 rtp->ice_proposed_remote_candidates = NULL;
600 /* Reset the ICE session. Is this going to work? */
601 if (ice_reset_session(instance)) {
602 ast_log(LOG_NOTICE, "Failed to create replacement ICE session\n");
606 pj_thread_register_check();
608 i = ao2_iterator_init(rtp->ice_active_remote_candidates, 0);
610 while ((candidate = ao2_iterator_next(&i)) && (cand_cnt < PJ_ICE_MAX_CAND)) {
613 pj_strdup2(rtp->ice->pool, &candidates[cand_cnt].foundation, candidate->foundation);
614 candidates[cand_cnt].comp_id = candidate->id;
615 candidates[cand_cnt].prio = candidate->priority;
617 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->address)), &candidates[cand_cnt].addr);
619 if (!ast_sockaddr_isnull(&candidate->relay_address)) {
620 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->relay_address)), &candidates[cand_cnt].rel_addr);
623 if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
624 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_HOST;
625 } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
626 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_SRFLX;
627 } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_RELAYED) {
628 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_RELAYED;
631 if (candidate->id == AST_RTP_ICE_COMPONENT_RTP && rtp->turn_rtp) {
632 pj_turn_sock_set_perm(rtp->turn_rtp, 1, &candidates[cand_cnt].addr, 1);
633 } else if (candidate->id == AST_RTP_ICE_COMPONENT_RTCP && rtp->turn_rtcp) {
634 pj_turn_sock_set_perm(rtp->turn_rtcp, 1, &candidates[cand_cnt].addr, 1);
638 ao2_ref(candidate, -1);
641 ao2_iterator_destroy(&i);
643 if (pj_ice_sess_create_check_list(rtp->ice, &ufrag, &passwd, ao2_container_count(rtp->ice_active_remote_candidates), &candidates[0]) == PJ_SUCCESS) {
644 ast_test_suite_event_notify("ICECHECKLISTCREATE", "Result: SUCCESS");
645 pj_ice_sess_start_check(rtp->ice);
646 pj_timer_heap_poll(timerheap, NULL);
647 rtp->strict_rtp_state = STRICT_RTP_OPEN;
651 ast_test_suite_event_notify("ICECHECKLISTCREATE", "Result: FAILURE");
653 /* even though create check list failed don't stop ice as
654 it might still work */
655 ast_debug(1, "Failed to create ICE session check list\n");
656 /* however we do need to reset remote candidates since
657 this function may be re-entered */
658 ao2_ref(rtp->ice_active_remote_candidates, -1);
659 rtp->ice_active_remote_candidates = NULL;
660 rtp->ice->rcand_cnt = rtp->ice->clist.count = 0;
663 static const char *ast_rtp_ice_get_ufrag(struct ast_rtp_instance *instance)
665 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
667 return rtp->local_ufrag;
670 static const char *ast_rtp_ice_get_password(struct ast_rtp_instance *instance)
672 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
674 return rtp->local_passwd;
677 static struct ao2_container *ast_rtp_ice_get_local_candidates(struct ast_rtp_instance *instance)
679 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
681 if (rtp->ice_local_candidates) {
682 ao2_ref(rtp->ice_local_candidates, +1);
685 return rtp->ice_local_candidates;
688 static void ast_rtp_ice_lite(struct ast_rtp_instance *instance)
690 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
696 pj_thread_register_check();
698 pj_ice_sess_change_role(rtp->ice, PJ_ICE_SESS_ROLE_CONTROLLING);
701 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,
702 const pj_sockaddr_t *addr, const pj_sockaddr_t *base_addr, const pj_sockaddr_t *rel_addr, int addr_len)
705 struct ast_rtp_engine_ice_candidate *candidate, *existing;
706 char address[PJ_INET6_ADDRSTRLEN];
708 pj_thread_register_check();
710 pj_ice_calc_foundation(rtp->ice->pool, &foundation, type, addr);
712 if (!rtp->ice_local_candidates && !(rtp->ice_local_candidates = ao2_container_alloc(1, NULL, ice_candidate_cmp))) {
716 if (!(candidate = ao2_alloc(sizeof(*candidate), ast_rtp_ice_candidate_destroy))) {
720 candidate->foundation = ast_strndup(pj_strbuf(&foundation), pj_strlen(&foundation));
721 candidate->id = comp_id;
722 candidate->transport = ast_strdup("UDP");
724 ast_sockaddr_parse(&candidate->address, pj_sockaddr_print(addr, address, sizeof(address), 0), 0);
725 ast_sockaddr_set_port(&candidate->address, pj_sockaddr_get_port(addr));
728 ast_sockaddr_parse(&candidate->relay_address, pj_sockaddr_print(rel_addr, address, sizeof(address), 0), 0);
729 ast_sockaddr_set_port(&candidate->relay_address, pj_sockaddr_get_port(rel_addr));
732 if (type == PJ_ICE_CAND_TYPE_HOST) {
733 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
734 } else if (type == PJ_ICE_CAND_TYPE_SRFLX) {
735 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
736 } else if (type == PJ_ICE_CAND_TYPE_RELAYED) {
737 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
740 if ((existing = ao2_find(rtp->ice_local_candidates, candidate, OBJ_POINTER))) {
741 ao2_ref(existing, -1);
742 ao2_ref(candidate, -1);
746 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) {
747 ao2_ref(candidate, -1);
751 /* By placing the candidate into the ICE session it will have produced the priority, so update the local candidate with it */
752 candidate->priority = rtp->ice->lcand[rtp->ice->lcand_cnt - 1].prio;
754 ao2_link(rtp->ice_local_candidates, candidate);
755 ao2_ref(candidate, -1);
758 static char *generate_random_string(char *buf, size_t size)
763 for (x=0; x<4; x++) {
764 val[x] = ast_random();
766 snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
771 /* ICE RTP Engine interface declaration */
772 static struct ast_rtp_engine_ice ast_rtp_ice = {
773 .set_authentication = ast_rtp_ice_set_authentication,
774 .add_remote_candidate = ast_rtp_ice_add_remote_candidate,
775 .start = ast_rtp_ice_start,
776 .stop = ast_rtp_ice_stop,
777 .get_ufrag = ast_rtp_ice_get_ufrag,
778 .get_password = ast_rtp_ice_get_password,
779 .get_local_candidates = ast_rtp_ice_get_local_candidates,
780 .ice_lite = ast_rtp_ice_lite,
784 #ifdef HAVE_OPENSSL_SRTP
785 static void dtls_info_callback(const SSL *ssl, int where, int ret)
787 struct ast_rtp *rtp = SSL_get_ex_data(ssl, 0);
789 /* We only care about alerts */
790 if (!(where & SSL_CB_ALERT)) {
794 rtp->dtls_failure = 1;
797 static int ast_rtp_dtls_set_configuration(struct ast_rtp_instance *instance, const struct ast_rtp_dtls_cfg *dtls_cfg)
799 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
801 if (!dtls_cfg->enabled) {
805 if (!ast_rtp_engine_srtp_is_registered()) {
809 if (!(rtp->ssl_ctx = SSL_CTX_new(DTLSv1_method()))) {
813 SSL_CTX_set_verify(rtp->ssl_ctx, dtls_cfg->verify ? SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT : SSL_VERIFY_NONE, NULL);
815 if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_80) {
816 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_80");
817 } else if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_32) {
818 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_32");
820 ast_log(LOG_ERROR, "Unsupported suite specified for DTLS-SRTP on RTP instance '%p'\n", instance);
824 if (!ast_strlen_zero(dtls_cfg->certfile)) {
825 char *private = ast_strlen_zero(dtls_cfg->pvtfile) ? dtls_cfg->certfile : dtls_cfg->pvtfile;
828 unsigned int size, i;
829 unsigned char fingerprint[EVP_MAX_MD_SIZE];
830 char *local_fingerprint = rtp->local_fingerprint;
832 if (!SSL_CTX_use_certificate_file(rtp->ssl_ctx, dtls_cfg->certfile, SSL_FILETYPE_PEM)) {
833 ast_log(LOG_ERROR, "Specified certificate file '%s' for RTP instance '%p' could not be used\n",
834 dtls_cfg->certfile, instance);
838 if (!SSL_CTX_use_PrivateKey_file(rtp->ssl_ctx, private, SSL_FILETYPE_PEM) ||
839 !SSL_CTX_check_private_key(rtp->ssl_ctx)) {
840 ast_log(LOG_ERROR, "Specified private key file '%s' for RTP instance '%p' could not be used\n",
845 if (!(certbio = BIO_new(BIO_s_file()))) {
846 ast_log(LOG_ERROR, "Failed to allocate memory for certificate fingerprinting on RTP instance '%p'\n",
851 if (!BIO_read_filename(certbio, dtls_cfg->certfile) ||
852 !(cert = PEM_read_bio_X509(certbio, NULL, 0, NULL)) ||
853 !X509_digest(cert, EVP_sha1(), fingerprint, &size) ||
855 ast_log(LOG_ERROR, "Could not produce fingerprint from certificate '%s' for RTP instance '%p'\n",
856 dtls_cfg->certfile, instance);
857 BIO_free_all(certbio);
861 for (i = 0; i < size; i++) {
862 sprintf(local_fingerprint, "%.2X:", fingerprint[i]);
863 local_fingerprint += 3;
866 *(local_fingerprint-1) = 0;
868 BIO_free_all(certbio);
871 if (!ast_strlen_zero(dtls_cfg->cipher)) {
872 if (!SSL_CTX_set_cipher_list(rtp->ssl_ctx, dtls_cfg->cipher)) {
873 ast_log(LOG_ERROR, "Invalid cipher specified in cipher list '%s' for RTP instance '%p'\n",
874 dtls_cfg->cipher, instance);
879 if (!ast_strlen_zero(dtls_cfg->cafile) || !ast_strlen_zero(dtls_cfg->capath)) {
880 if (!SSL_CTX_load_verify_locations(rtp->ssl_ctx, S_OR(dtls_cfg->cafile, NULL), S_OR(dtls_cfg->capath, NULL))) {
881 ast_log(LOG_ERROR, "Invalid certificate authority file '%s' or path '%s' specified for RTP instance '%p'\n",
882 S_OR(dtls_cfg->cafile, ""), S_OR(dtls_cfg->capath, ""), instance);
887 rtp->rekey = dtls_cfg->rekey;
888 rtp->dtls_setup = dtls_cfg->default_setup;
889 rtp->suite = dtls_cfg->suite;
891 if (!(rtp->ssl = SSL_new(rtp->ssl_ctx))) {
892 ast_log(LOG_ERROR, "Failed to allocate memory for SSL context on RTP instance '%p'\n",
897 SSL_set_ex_data(rtp->ssl, 0, rtp);
898 SSL_set_info_callback(rtp->ssl, dtls_info_callback);
900 if (!(rtp->read_bio = BIO_new(BIO_s_mem()))) {
901 ast_log(LOG_ERROR, "Failed to allocate memory for inbound SSL traffic on RTP instance '%p'\n",
905 BIO_set_mem_eof_return(rtp->read_bio, -1);
907 if (!(rtp->write_bio = BIO_new(BIO_s_mem()))) {
908 ast_log(LOG_ERROR, "Failed to allocate memory for outbound SSL traffic on RTP instance '%p'\n",
912 BIO_set_mem_eof_return(rtp->write_bio, -1);
914 SSL_set_bio(rtp->ssl, rtp->read_bio, rtp->write_bio);
916 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
917 SSL_set_accept_state(rtp->ssl);
919 SSL_set_connect_state(rtp->ssl);
922 rtp->connection = AST_RTP_DTLS_CONNECTION_NEW;
928 BIO_free(rtp->read_bio);
929 rtp->read_bio = NULL;
932 if (rtp->write_bio) {
933 BIO_free(rtp->write_bio);
934 rtp->write_bio = NULL;
942 SSL_CTX_free(rtp->ssl_ctx);
948 static int ast_rtp_dtls_active(struct ast_rtp_instance *instance)
950 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
952 return !rtp->ssl_ctx ? 0 : 1;
955 static void ast_rtp_dtls_stop(struct ast_rtp_instance *instance)
957 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
960 SSL_CTX_free(rtp->ssl_ctx);
970 static void ast_rtp_dtls_reset(struct ast_rtp_instance *instance)
972 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
974 /* If the SSL session is not yet finalized don't bother resetting */
975 if (!SSL_is_init_finished(rtp->ssl)) {
979 SSL_shutdown(rtp->ssl);
980 rtp->connection = AST_RTP_DTLS_CONNECTION_NEW;
983 static enum ast_rtp_dtls_connection ast_rtp_dtls_get_connection(struct ast_rtp_instance *instance)
985 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
987 return rtp->connection;
990 static enum ast_rtp_dtls_setup ast_rtp_dtls_get_setup(struct ast_rtp_instance *instance)
992 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
994 return rtp->dtls_setup;
997 static void ast_rtp_dtls_set_setup(struct ast_rtp_instance *instance, enum ast_rtp_dtls_setup setup)
999 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1000 enum ast_rtp_dtls_setup old = rtp->dtls_setup;
1003 case AST_RTP_DTLS_SETUP_ACTIVE:
1004 rtp->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
1006 case AST_RTP_DTLS_SETUP_PASSIVE:
1007 rtp->dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
1009 case AST_RTP_DTLS_SETUP_ACTPASS:
1010 /* We can't respond to an actpass setup with actpass ourselves... so respond with active, as we can initiate connections */
1011 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
1012 rtp->dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
1015 case AST_RTP_DTLS_SETUP_HOLDCONN:
1016 rtp->dtls_setup = AST_RTP_DTLS_SETUP_HOLDCONN;
1019 /* This should never occur... if it does exit early as we don't know what state things are in */
1023 /* If the setup state did not change we go on as if nothing happened */
1024 if (old == rtp->dtls_setup) {
1028 /* If they don't want us to establish a connection wait until later */
1029 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_HOLDCONN) {
1033 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
1034 SSL_set_connect_state(rtp->ssl);
1035 } else if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
1036 SSL_set_accept_state(rtp->ssl);
1042 static void ast_rtp_dtls_set_fingerprint(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash, const char *fingerprint)
1044 char *tmp = ast_strdupa(fingerprint), *value;
1046 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1048 if (hash != AST_RTP_DTLS_HASH_SHA1) {
1052 while ((value = strsep(&tmp, ":")) && (pos != (EVP_MAX_MD_SIZE - 1))) {
1053 sscanf(value, "%02x", (unsigned int*)&rtp->remote_fingerprint[pos++]);
1057 static const char *ast_rtp_dtls_get_fingerprint(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash)
1059 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1061 if (hash != AST_RTP_DTLS_HASH_SHA1) {
1065 return rtp->local_fingerprint;
1068 /* DTLS RTP Engine interface declaration */
1069 static struct ast_rtp_engine_dtls ast_rtp_dtls = {
1070 .set_configuration = ast_rtp_dtls_set_configuration,
1071 .active = ast_rtp_dtls_active,
1072 .stop = ast_rtp_dtls_stop,
1073 .reset = ast_rtp_dtls_reset,
1074 .get_connection = ast_rtp_dtls_get_connection,
1075 .get_setup = ast_rtp_dtls_get_setup,
1076 .set_setup = ast_rtp_dtls_set_setup,
1077 .set_fingerprint = ast_rtp_dtls_set_fingerprint,
1078 .get_fingerprint = ast_rtp_dtls_get_fingerprint,
1083 /* RTP Engine Declaration */
1084 static struct ast_rtp_engine asterisk_rtp_engine = {
1087 .destroy = ast_rtp_destroy,
1088 .dtmf_begin = ast_rtp_dtmf_begin,
1089 .dtmf_end = ast_rtp_dtmf_end,
1090 .dtmf_end_with_duration = ast_rtp_dtmf_end_with_duration,
1091 .dtmf_mode_set = ast_rtp_dtmf_mode_set,
1092 .dtmf_mode_get = ast_rtp_dtmf_mode_get,
1093 .update_source = ast_rtp_update_source,
1094 .change_source = ast_rtp_change_source,
1095 .write = ast_rtp_write,
1096 .read = ast_rtp_read,
1097 .prop_set = ast_rtp_prop_set,
1099 .remote_address_set = ast_rtp_remote_address_set,
1100 .red_init = rtp_red_init,
1101 .red_buffer = rtp_red_buffer,
1102 .local_bridge = ast_rtp_local_bridge,
1103 .get_stat = ast_rtp_get_stat,
1104 .dtmf_compatible = ast_rtp_dtmf_compatible,
1105 .stun_request = ast_rtp_stun_request,
1106 .stop = ast_rtp_stop,
1107 .qos = ast_rtp_qos_set,
1108 .sendcng = ast_rtp_sendcng,
1109 #ifdef HAVE_PJPROJECT
1110 .ice = &ast_rtp_ice,
1112 #ifdef HAVE_OPENSSL_SRTP
1113 .dtls = &ast_rtp_dtls,
1114 .activate = ast_rtp_activate,
1118 #ifdef HAVE_PJPROJECT
1119 static void rtp_learning_seq_init(struct rtp_learning_info *info, uint16_t seq);
1121 static void ast_rtp_on_ice_complete(pj_ice_sess *ice, pj_status_t status)
1123 struct ast_rtp *rtp = ice->user_data;
1129 rtp->strict_rtp_state = STRICT_RTP_LEARN;
1130 rtp_learning_seq_init(&rtp->rtp_source_learn, (uint16_t)rtp->seqno);
1133 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)
1135 struct ast_rtp *rtp = ice->user_data;
1137 /* 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
1139 rtp->passthrough = 1;
1142 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)
1144 struct ast_rtp *rtp = ice->user_data;
1145 pj_status_t status = PJ_EINVALIDOP;
1146 pj_ssize_t _size = (pj_ssize_t)size;
1148 if (transport_id == TRANSPORT_SOCKET_RTP) {
1149 /* Traffic is destined to go right out the RTP socket we already have */
1150 status = pj_sock_sendto(rtp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
1151 /* sendto on a connectionless socket should send all the data, or none at all */
1152 ast_assert(_size == size || status != PJ_SUCCESS);
1153 } else if (transport_id == TRANSPORT_SOCKET_RTCP) {
1154 /* Traffic is destined to go right out the RTCP socket we already have */
1156 status = pj_sock_sendto(rtp->rtcp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
1157 /* sendto on a connectionless socket should send all the data, or none at all */
1158 ast_assert(_size == size || status != PJ_SUCCESS);
1160 status = PJ_SUCCESS;
1162 } else if (transport_id == TRANSPORT_TURN_RTP) {
1163 /* Traffic is going through the RTP TURN relay */
1164 if (rtp->turn_rtp) {
1165 status = pj_turn_sock_sendto(rtp->turn_rtp, pkt, size, dst_addr, dst_addr_len);
1167 } else if (transport_id == TRANSPORT_TURN_RTCP) {
1168 /* Traffic is going through the RTCP TURN relay */
1169 if (rtp->turn_rtcp) {
1170 status = pj_turn_sock_sendto(rtp->turn_rtcp, pkt, size, dst_addr, dst_addr_len);
1177 /* ICE Session interface declaration */
1178 static pj_ice_sess_cb ast_rtp_ice_sess_cb = {
1179 .on_ice_complete = ast_rtp_on_ice_complete,
1180 .on_rx_data = ast_rtp_on_ice_rx_data,
1181 .on_tx_pkt = ast_rtp_on_ice_tx_pkt,
1184 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)
1186 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1187 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1188 struct ast_sockaddr dest = { { 0, }, };
1190 ast_rtp_instance_get_local_address(instance, &dest);
1192 ast_sendto(rtp->s, pkt, pkt_len, 0, &dest);
1195 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)
1197 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1198 struct ast_rtp *rtp = NULL;
1200 /* If this is a leftover from an already destroyed RTP instance just ignore the state change */
1205 rtp = ast_rtp_instance_get_data(instance);
1207 /* If the TURN session is being destroyed we need to remove it from the RTP instance */
1208 if (new_state == PJ_TURN_STATE_DESTROYING) {
1209 rtp->turn_rtp = NULL;
1213 /* We store the new state so the other thread can actually handle it */
1214 ast_mutex_lock(&rtp->lock);
1215 rtp->turn_state = new_state;
1217 /* If this is a state that the main thread should be notified about do so */
1218 if (new_state == PJ_TURN_STATE_READY || new_state == PJ_TURN_STATE_DEALLOCATING || new_state == PJ_TURN_STATE_DEALLOCATED) {
1219 ast_cond_signal(&rtp->cond);
1222 ast_mutex_unlock(&rtp->lock);
1225 /* RTP TURN Socket interface declaration */
1226 static pj_turn_sock_cb ast_rtp_turn_rtp_sock_cb = {
1227 .on_rx_data = ast_rtp_on_turn_rx_rtp_data,
1228 .on_state = ast_rtp_on_turn_rtp_state,
1231 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)
1233 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1234 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1236 ast_sendto(rtp->rtcp->s, pkt, pkt_len, 0, &rtp->rtcp->us);
1239 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)
1241 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1242 struct ast_rtp *rtp = NULL;
1244 /* If this is a leftover from an already destroyed RTP instance just ignore the state change */
1249 rtp = ast_rtp_instance_get_data(instance);
1251 /* If the TURN session is being destroyed we need to remove it from the RTP instance */
1252 if (new_state == PJ_TURN_STATE_DESTROYING) {
1253 rtp->turn_rtcp = NULL;
1257 /* We store the new state so the other thread can actually handle it */
1258 ast_mutex_lock(&rtp->lock);
1259 rtp->turn_state = new_state;
1261 /* If this is a state that the main thread should be notified about do so */
1262 if (new_state == PJ_TURN_STATE_READY || new_state == PJ_TURN_STATE_DEALLOCATING || new_state == PJ_TURN_STATE_DEALLOCATED) {
1263 ast_cond_signal(&rtp->cond);
1266 ast_mutex_unlock(&rtp->lock);
1269 /* RTCP TURN Socket interface declaration */
1270 static pj_turn_sock_cb ast_rtp_turn_rtcp_sock_cb = {
1271 .on_rx_data = ast_rtp_on_turn_rx_rtcp_data,
1272 .on_state = ast_rtp_on_turn_rtcp_state,
1275 /*! \brief Worker thread for I/O queue and timerheap */
1276 static int ice_worker_thread(void *data)
1278 while (!worker_terminate) {
1279 const pj_time_val delay = {0, 10};
1281 pj_ioqueue_poll(ioqueue, &delay);
1283 pj_timer_heap_poll(timerheap, NULL);
1290 static inline int rtp_debug_test_addr(struct ast_sockaddr *addr)
1295 if (!ast_sockaddr_isnull(&rtpdebugaddr)) {
1297 return (ast_sockaddr_cmp(&rtpdebugaddr, addr) == 0); /* look for RTP packets from IP+Port */
1299 return (ast_sockaddr_cmp_addr(&rtpdebugaddr, addr) == 0); /* only look for RTP packets from IP */
1306 static inline int rtcp_debug_test_addr(struct ast_sockaddr *addr)
1311 if (!ast_sockaddr_isnull(&rtcpdebugaddr)) {
1312 if (rtcpdebugport) {
1313 return (ast_sockaddr_cmp(&rtcpdebugaddr, addr) == 0); /* look for RTCP packets from IP+Port */
1315 return (ast_sockaddr_cmp_addr(&rtcpdebugaddr, addr) == 0); /* only look for RTCP packets from IP */
1322 #ifdef HAVE_OPENSSL_SRTP
1323 static void dtls_srtp_check_pending(struct ast_rtp_instance *instance, struct ast_rtp *rtp)
1325 size_t pending = BIO_ctrl_pending(rtp->write_bio);
1328 char outgoing[pending];
1330 struct ast_sockaddr remote_address = { {0, } };
1333 ast_rtp_instance_get_remote_address(instance, &remote_address);
1335 /* If we do not yet know an address to send this to defer it until we do */
1336 if (ast_sockaddr_isnull(&remote_address)) {
1340 out = BIO_read(rtp->write_bio, outgoing, sizeof(outgoing));
1342 __rtp_sendto(instance, outgoing, out, 0, &remote_address, 0, &ice, 0);
1346 static int dtls_srtp_renegotiate(const void *data)
1348 struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
1349 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1351 SSL_renegotiate(rtp->ssl);
1352 SSL_do_handshake(rtp->ssl);
1353 dtls_srtp_check_pending(instance, rtp);
1356 ao2_ref(instance, -1);
1361 static int dtls_srtp_setup(struct ast_rtp *rtp, struct ast_srtp *srtp, struct ast_rtp_instance *instance)
1363 unsigned char material[SRTP_MASTER_LEN * 2];
1364 unsigned char *local_key, *local_salt, *remote_key, *remote_salt;
1365 struct ast_srtp_policy *local_policy, *remote_policy = NULL;
1366 struct ast_rtp_instance_stats stats = { 0, };
1368 /* If a fingerprint is present in the SDP make sure that the peer certificate matches it */
1369 if (SSL_CTX_get_verify_mode(rtp->ssl_ctx) != SSL_VERIFY_NONE) {
1372 if (!(certificate = SSL_get_peer_certificate(rtp->ssl))) {
1373 ast_log(LOG_WARNING, "No certificate was provided by the peer on RTP instance '%p'\n", instance);
1377 /* If a fingerprint is present in the SDP make sure that the peer certificate matches it */
1378 if (rtp->remote_fingerprint[0]) {
1379 unsigned char fingerprint[EVP_MAX_MD_SIZE];
1382 if (!X509_digest(certificate, EVP_sha1(), fingerprint, &size) ||
1384 memcmp(fingerprint, rtp->remote_fingerprint, size)) {
1385 X509_free(certificate);
1386 ast_log(LOG_WARNING, "Fingerprint provided by remote party does not match that of peer certificate on RTP instance '%p'\n",
1392 X509_free(certificate);
1395 /* Ensure that certificate verification was successful */
1396 if (SSL_get_verify_result(rtp->ssl) != X509_V_OK) {
1397 ast_log(LOG_WARNING, "Peer certificate on RTP instance '%p' failed verification test\n",
1402 /* Produce key information and set up SRTP */
1403 if (!SSL_export_keying_material(rtp->ssl, material, SRTP_MASTER_LEN * 2, "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) {
1404 ast_log(LOG_WARNING, "Unable to extract SRTP keying material from DTLS-SRTP negotiation on RTP instance '%p'\n",
1409 /* Whether we are acting as a server or client determines where the keys/salts are */
1410 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
1411 local_key = material;
1412 remote_key = local_key + SRTP_MASTER_KEY_LEN;
1413 local_salt = remote_key + SRTP_MASTER_KEY_LEN;
1414 remote_salt = local_salt + SRTP_MASTER_SALT_LEN;
1416 remote_key = material;
1417 local_key = remote_key + SRTP_MASTER_KEY_LEN;
1418 remote_salt = local_key + SRTP_MASTER_KEY_LEN;
1419 local_salt = remote_salt + SRTP_MASTER_SALT_LEN;
1422 if (!(local_policy = res_srtp_policy->alloc())) {
1426 if (res_srtp_policy->set_master_key(local_policy, local_key, SRTP_MASTER_KEY_LEN, local_salt, SRTP_MASTER_SALT_LEN) < 0) {
1427 ast_log(LOG_WARNING, "Could not set key/salt information on local policy of '%p' when setting up DTLS-SRTP\n", rtp);
1431 if (res_srtp_policy->set_suite(local_policy, rtp->suite)) {
1432 ast_log(LOG_WARNING, "Could not set suite to '%d' on local policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
1436 if (ast_rtp_instance_get_stats(instance, &stats, AST_RTP_INSTANCE_STAT_LOCAL_SSRC)) {
1440 res_srtp_policy->set_ssrc(local_policy, stats.local_ssrc, 0);
1442 if (!(remote_policy = res_srtp_policy->alloc())) {
1446 if (res_srtp_policy->set_master_key(remote_policy, remote_key, SRTP_MASTER_KEY_LEN, remote_salt, SRTP_MASTER_SALT_LEN) < 0) {
1447 ast_log(LOG_WARNING, "Could not set key/salt information on remote policy of '%p' when setting up DTLS-SRTP\n", rtp);
1451 if (res_srtp_policy->set_suite(remote_policy, rtp->suite)) {
1452 ast_log(LOG_WARNING, "Could not set suite to '%d' on remote policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
1456 res_srtp_policy->set_ssrc(remote_policy, 0, 1);
1458 if (ast_rtp_instance_add_srtp_policy(instance, remote_policy, local_policy)) {
1459 ast_log(LOG_WARNING, "Could not set policies when setting up DTLS-SRTP on '%p'\n", rtp);
1464 ao2_ref(instance, +1);
1465 if ((rtp->rekeyid = ast_sched_add(rtp->sched, rtp->rekey * 1000, dtls_srtp_renegotiate, instance)) < 0) {
1466 ao2_ref(instance, -1);
1474 res_srtp_policy->destroy(local_policy);
1476 if (remote_policy) {
1477 res_srtp_policy->destroy(remote_policy);
1484 static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp)
1487 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1488 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
1491 if ((len = ast_recvfrom(rtcp ? rtp->rtcp->s : rtp->s, buf, size, flags, sa)) < 0) {
1495 #ifdef HAVE_OPENSSL_SRTP
1497 dtls_srtp_check_pending(instance, rtp);
1499 /* If this is an SSL packet pass it to OpenSSL for processing */
1500 if ((*in >= 20) && (*in <= 64)) {
1503 /* If no SSL session actually exists terminate things */
1505 ast_log(LOG_ERROR, "Received SSL traffic on RTP instance '%p' without an SSL session\n",
1510 /* If we don't yet know if we are active or passive and we receive a packet... we are obviously passive */
1511 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
1512 rtp->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
1513 SSL_set_accept_state(rtp->ssl);
1516 dtls_srtp_check_pending(instance, rtp);
1518 BIO_write(rtp->read_bio, buf, len);
1520 len = SSL_read(rtp->ssl, buf, len);
1522 dtls_srtp_check_pending(instance, rtp);
1524 if (rtp->dtls_failure) {
1525 ast_log(LOG_ERROR, "DTLS failure occurred on RTP instance '%p', terminating\n",
1530 if (SSL_is_init_finished(rtp->ssl)) {
1531 /* Any further connections will be existing since this is now established */
1532 rtp->connection = AST_RTP_DTLS_CONNECTION_EXISTING;
1534 /* Use the keying material to set up key/salt information */
1535 res = dtls_srtp_setup(rtp, srtp, instance);
1543 #ifdef HAVE_PJPROJECT
1545 pj_str_t combined = pj_str(ast_sockaddr_stringify(sa));
1546 pj_sockaddr address;
1549 pj_thread_register_check();
1551 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &combined, &address);
1553 status = pj_ice_sess_on_rx_pkt(rtp->ice, rtcp ? AST_RTP_ICE_COMPONENT_RTCP : AST_RTP_ICE_COMPONENT_RTP,
1554 rtcp ? TRANSPORT_SOCKET_RTCP : TRANSPORT_SOCKET_RTP, buf, len, &address,
1555 pj_sockaddr_get_len(&address));
1556 if (status != PJ_SUCCESS) {
1559 pj_strerror(status, buf, sizeof(buf));
1560 ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n",
1564 if (!rtp->passthrough) {
1567 rtp->passthrough = 0;
1571 if ((*in & 0xC0) && res_srtp && srtp && res_srtp->unprotect(srtp, buf, &len, rtcp) < 0) {
1578 static int rtcp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
1580 return __rtp_recvfrom(instance, buf, size, flags, sa, 1);
1583 static int rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
1585 return __rtp_recvfrom(instance, buf, size, flags, sa, 0);
1588 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)
1592 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1593 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
1597 if (use_srtp && res_srtp && srtp && res_srtp->protect(srtp, &temp, &len, rtcp) < 0) {
1601 #ifdef HAVE_PJPROJECT
1603 pj_thread_register_check();
1605 if (pj_ice_sess_send_data(rtp->ice, rtcp ? AST_RTP_ICE_COMPONENT_RTCP : AST_RTP_ICE_COMPONENT_RTP, temp, len) == PJ_SUCCESS) {
1612 return ast_sendto(rtcp ? rtp->rtcp->s : rtp->s, temp, len, flags, sa);
1615 static int rtcp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
1617 return __rtp_sendto(instance, buf, size, flags, sa, 1, ice, 1);
1620 static int rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
1622 return __rtp_sendto(instance, buf, size, flags, sa, 0, ice, 1);
1625 static int rtp_get_rate(struct ast_format *format)
1627 return (format->id == AST_FORMAT_G722) ? 8000 : ast_format_rate(format);
1630 static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
1632 unsigned int interval;
1633 /*! \todo XXX Do a more reasonable calculation on this one
1634 * Look in RFC 3550 Section A.7 for an example*/
1635 interval = rtcpinterval;
1639 /*! \brief Calculate normal deviation */
1640 static double normdev_compute(double normdev, double sample, unsigned int sample_count)
1642 normdev = normdev * sample_count + sample;
1645 return normdev / sample_count;
1648 static double stddev_compute(double stddev, double sample, double normdev, double normdev_curent, unsigned int sample_count)
1651 for the formula check http://www.cs.umd.edu/~austinjp/constSD.pdf
1652 return sqrt( (sample_count*pow(stddev,2) + sample_count*pow((sample-normdev)/(sample_count+1),2) + pow(sample-normdev_curent,2)) / (sample_count+1));
1653 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
1656 #define SQUARE(x) ((x) * (x))
1658 stddev = sample_count * stddev;
1662 ( sample_count * SQUARE( (sample - normdev) / sample_count ) ) +
1663 ( SQUARE(sample - normdev_curent) / sample_count );
1668 static int create_new_socket(const char *type, int af)
1670 int sock = socket(af, SOCK_DGRAM, 0);
1676 ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
1678 long flags = fcntl(sock, F_GETFL);
1679 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
1682 setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
1692 * \brief Initializes sequence values and probation for learning mode.
1693 * \note This is an adaptation of pjmedia's pjmedia_rtp_seq_init function.
1695 * \param info The learning information to track
1696 * \param seq sequence number read from the rtp header to initialize the information with
1698 static void rtp_learning_seq_init(struct rtp_learning_info *info, uint16_t seq)
1700 info->max_seq = seq - 1;
1701 info->packets = learning_min_sequential;
1706 * \brief Updates sequence information for learning mode and determines if probation/learning mode should remain in effect.
1707 * \note This function was adapted from pjmedia's pjmedia_rtp_seq_update function.
1709 * \param info Structure tracking the learning progress of some address
1710 * \param seq sequence number read from the rtp header
1711 * \retval 0 if probation mode should exit for this address
1712 * \retval non-zero if probation mode should continue
1714 static int rtp_learning_rtp_seq_update(struct rtp_learning_info *info, uint16_t seq)
1716 if (seq == info->max_seq + 1) {
1717 /* packet is in sequence */
1720 /* Sequence discontinuity; reset */
1721 info->packets = learning_min_sequential - 1;
1723 info->max_seq = seq;
1725 return (info->packets == 0);
1728 #ifdef HAVE_PJPROJECT
1729 static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *addr, int port, int component,
1730 int transport, const pj_turn_sock_cb *turn_cb, pj_turn_sock **turn_sock)
1732 pj_sockaddr address[16];
1733 unsigned int count = PJ_ARRAY_SIZE(address), pos = 0;
1735 /* Add all the local interface IP addresses */
1736 if (ast_sockaddr_is_ipv4(addr)) {
1737 pj_enum_ip_interface(pj_AF_INET(), &count, address);
1738 } else if (ast_sockaddr_is_any(addr)) {
1739 pj_enum_ip_interface(pj_AF_UNSPEC(), &count, address);
1741 pj_enum_ip_interface(pj_AF_INET6(), &count, address);
1744 for (pos = 0; pos < count; pos++) {
1745 pj_sockaddr_set_port(&address[pos], port);
1746 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_HOST, 65535, &address[pos], &address[pos], NULL,
1747 pj_sockaddr_get_len(&address[pos]));
1750 /* If configured to use a STUN server to get our external mapped address do so */
1751 if (stunaddr.sin_addr.s_addr && ast_sockaddr_is_ipv4(addr) && count) {
1752 struct sockaddr_in answer;
1754 if (!ast_stun_request(component == AST_RTP_ICE_COMPONENT_RTCP ? rtp->rtcp->s : rtp->s, &stunaddr, NULL, &answer)) {
1756 pj_str_t mapped = pj_str(ast_strdupa(ast_inet_ntoa(answer.sin_addr)));
1758 /* Use the first local host candidate as the base */
1759 pj_sockaddr_cp(&base, &address[0]);
1761 pj_sockaddr_init(pj_AF_INET(), &address[0], &mapped, ntohs(answer.sin_port));
1763 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_SRFLX, 65535, &address[0], &base,
1764 NULL, pj_sockaddr_get_len(&address[0]));
1768 /* If configured to use a TURN relay create a session and allocate */
1769 if (pj_strlen(&turnaddr) && pj_turn_sock_create(&rtp->ice->stun_cfg, ast_sockaddr_is_ipv4(addr) ? pj_AF_INET() : pj_AF_INET6(), PJ_TURN_TP_TCP,
1770 turn_cb, NULL, instance, turn_sock) == PJ_SUCCESS) {
1771 pj_stun_auth_cred cred = { 0, };
1772 struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_ALLOCATION_WAIT_TIME, 1000));
1773 struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
1775 cred.type = PJ_STUN_AUTH_CRED_STATIC;
1776 cred.data.static_cred.username = turnusername;
1777 cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
1778 cred.data.static_cred.data = turnpassword;
1780 /* Because the TURN socket is asynchronous but we are synchronous we need to wait until it is done */
1781 ast_mutex_lock(&rtp->lock);
1782 pj_turn_sock_alloc(*turn_sock, &turnaddr, turnport, NULL, &cred, NULL);
1783 ast_cond_timedwait(&rtp->cond, &rtp->lock, &ts);
1784 ast_mutex_unlock(&rtp->lock);
1786 /* If a TURN session was allocated add it as a candidate */
1787 if (rtp->turn_state == PJ_TURN_STATE_READY) {
1788 pj_turn_session_info info;
1790 pj_turn_sock_get_info(*turn_sock, &info);
1792 if (transport == TRANSPORT_SOCKET_RTP) {
1793 transport = TRANSPORT_TURN_RTP;
1794 } else if (transport == TRANSPORT_SOCKET_RTCP) {
1795 transport = TRANSPORT_TURN_RTCP;
1798 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_RELAYED, 65535, &info.relay_addr, &info.relay_addr,
1799 NULL, pj_sockaddr_get_len(&info.relay_addr));
1807 * \brief Calculates the elapsed time from issue of the first tx packet in an
1808 * rtp session and a specified time
1810 * \param rtp pointer to the rtp struct with the transmitted rtp packet
1811 * \param delivery time of delivery - if NULL or zero value, will be ast_tvnow()
1813 * \return time elapsed in milliseconds
1815 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
1820 if (ast_tvzero(rtp->txcore)) {
1821 rtp->txcore = ast_tvnow();
1822 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
1825 t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
1826 if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) {
1831 return (unsigned int) ms;
1834 #ifdef HAVE_PJPROJECT
1837 * \brief Creates an ICE session. Can be used to replace a destroyed ICE session.
1839 * \param instance RTP instance for which the ICE session is being replaced
1840 * \param addr ast_sockaddr to use for adding RTP candidates to the ICE session
1841 * \param port port to use for adding RTP candidates to the ICE session
1842 * \param replace 0 when creating a new session, 1 when replacing a destroyed session
1844 * \retval 0 on success
1845 * \retval -1 on failure
1847 static int ice_create(struct ast_rtp_instance *instance, struct ast_sockaddr *addr,
1848 int port, int replace)
1850 pj_stun_config stun_config;
1851 pj_str_t ufrag, passwd;
1852 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1854 ao2_cleanup(rtp->ice_local_candidates);
1855 rtp->ice_local_candidates = NULL;
1857 pj_thread_register_check();
1859 pj_stun_config_init(&stun_config, &cachingpool.factory, 0, ioqueue, timerheap);
1861 ufrag = pj_str(rtp->local_ufrag);
1862 passwd = pj_str(rtp->local_passwd);
1864 /* Create an ICE session for ICE negotiation */
1865 if (pj_ice_sess_create(&stun_config, NULL, PJ_ICE_SESS_ROLE_UNKNOWN, 2,
1866 &ast_rtp_ice_sess_cb, &ufrag, &passwd, NULL, &rtp->ice) == PJ_SUCCESS) {
1867 /* Make this available for the callbacks */
1868 rtp->ice->user_data = rtp;
1870 /* Add all of the available candidates to the ICE session */
1871 rtp_add_candidates_to_ice(instance, rtp, addr, port, AST_RTP_ICE_COMPONENT_RTP,
1872 TRANSPORT_SOCKET_RTP, &ast_rtp_turn_rtp_sock_cb, &rtp->turn_rtp);
1874 /* Only add the RTCP candidates to ICE when replacing the session. New sessions
1875 * handle this in a separate part of the setup phase */
1876 if (replace && rtp->rtcp) {
1877 rtp_add_candidates_to_ice(instance, rtp, &rtp->rtcp->us,
1878 ast_sockaddr_port(&rtp->rtcp->us), AST_RTP_ICE_COMPONENT_RTCP,
1879 TRANSPORT_SOCKET_RTCP, &ast_rtp_turn_rtcp_sock_cb, &rtp->turn_rtcp);
1890 static int ast_rtp_new(struct ast_rtp_instance *instance,
1891 struct ast_sched_context *sched, struct ast_sockaddr *addr,
1894 struct ast_rtp *rtp = NULL;
1897 /* Create a new RTP structure to hold all of our data */
1898 if (!(rtp = ast_calloc(1, sizeof(*rtp)))) {
1902 /* Initialize synchronization aspects */
1903 ast_mutex_init(&rtp->lock);
1904 ast_cond_init(&rtp->cond, NULL);
1906 /* Set default parameters on the newly created RTP structure */
1907 rtp->ssrc = ast_random();
1908 rtp->seqno = ast_random() & 0xffff;
1909 rtp->strict_rtp_state = (strictrtp ? STRICT_RTP_LEARN : STRICT_RTP_OPEN);
1911 rtp_learning_seq_init(&rtp->rtp_source_learn, (uint16_t)rtp->seqno);
1912 rtp_learning_seq_init(&rtp->alt_source_learn, (uint16_t)rtp->seqno);
1915 /* Create a new socket for us to listen on and use */
1917 create_new_socket("RTP",
1918 ast_sockaddr_is_ipv4(addr) ? AF_INET :
1919 ast_sockaddr_is_ipv6(addr) ? AF_INET6 : -1)) < 0) {
1920 ast_debug(1, "Failed to create a new socket for RTP instance '%p'\n", instance);
1925 /* Now actually find a free RTP port to use */
1926 x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
1931 ast_sockaddr_set_port(addr, x);
1932 /* Try to bind, this will tell us whether the port is available or not */
1933 if (!ast_bind(rtp->s, addr)) {
1934 ast_debug(1, "Allocated port %d for RTP instance '%p'\n", x, instance);
1935 ast_rtp_instance_set_local_address(instance, addr);
1941 x = (rtpstart + 1) & ~1;
1944 /* See if we ran out of ports or if the bind actually failed because of something other than the address being in use */
1945 if (x == startplace || (errno != EADDRINUSE && errno != EACCES)) {
1946 ast_log(LOG_ERROR, "Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance);
1953 #ifdef HAVE_PJPROJECT
1954 generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag));
1955 generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd));
1957 ast_rtp_instance_set_data(instance, rtp);
1958 #ifdef HAVE_PJPROJECT
1959 /* Create an ICE session for ICE negotiation */
1961 if (ice_create(instance, addr, x, 0)) {
1962 ast_log(LOG_NOTICE, "Failed to start ICE session\n");
1965 ast_sockaddr_copy(&rtp->ice_original_rtp_addr, addr);
1970 /* Record any information we may need */
1973 #ifdef HAVE_OPENSSL_SRTP
1980 static int ast_rtp_destroy(struct ast_rtp_instance *instance)
1982 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1984 /* Destroy the smoother that was smoothing out audio if present */
1985 if (rtp->smoother) {
1986 ast_smoother_free(rtp->smoother);
1989 /* Close our own socket so we no longer get packets */
1994 /* Destroy RTCP if it was being used */
1997 * It is not possible for there to be an active RTCP scheduler
1998 * entry at this point since it holds a reference to the
1999 * RTP instance while it's active.
2001 close(rtp->rtcp->s);
2002 ast_free(rtp->rtcp);
2005 /* Destroy RED if it was being used */
2007 AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
2011 #ifdef HAVE_PJPROJECT
2012 pj_thread_register_check();
2014 /* Destroy the ICE session if being used */
2016 pj_ice_sess_destroy(rtp->ice);
2019 /* Destroy the RTP TURN relay if being used */
2020 if (rtp->turn_rtp) {
2021 pj_turn_sock_set_user_data(rtp->turn_rtp, NULL);
2022 pj_turn_sock_destroy(rtp->turn_rtp);
2025 /* Destroy the RTCP TURN relay if being used */
2026 if (rtp->turn_rtcp) {
2027 pj_turn_sock_set_user_data(rtp->turn_rtcp, NULL);
2028 pj_turn_sock_destroy(rtp->turn_rtcp);
2031 /* Destroy any candidates */
2032 if (rtp->ice_local_candidates) {
2033 ao2_ref(rtp->ice_local_candidates, -1);
2036 if (rtp->ice_active_remote_candidates) {
2037 ao2_ref(rtp->ice_active_remote_candidates, -1);
2041 #ifdef HAVE_OPENSSL_SRTP
2042 /* Destroy the SSL context if present */
2044 SSL_CTX_free(rtp->ssl_ctx);
2047 /* Destroy the SSL session if present */
2053 /* Destroy synchronization items */
2054 ast_mutex_destroy(&rtp->lock);
2055 ast_cond_destroy(&rtp->cond);
2057 /* Finally destroy ourselves */
2063 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
2065 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2066 rtp->dtmfmode = dtmf_mode;
2070 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance)
2072 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2073 return rtp->dtmfmode;
2076 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit)
2078 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2079 struct ast_sockaddr remote_address = { {0,} };
2080 int hdrlen = 12, res = 0, i = 0, payload = 101;
2082 unsigned int *rtpheader = (unsigned int*)data;
2084 ast_rtp_instance_get_remote_address(instance, &remote_address);
2086 /* If we have no remote address information bail out now */
2087 if (ast_sockaddr_isnull(&remote_address)) {
2091 /* Convert given digit into what we want to transmit */
2092 if ((digit <= '9') && (digit >= '0')) {
2094 } else if (digit == '*') {
2096 } else if (digit == '#') {
2098 } else if ((digit >= 'A') && (digit <= 'D')) {
2099 digit = digit - 'A' + 12;
2100 } else if ((digit >= 'a') && (digit <= 'd')) {
2101 digit = digit - 'a' + 12;
2103 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
2107 /* Grab the payload that they expect the RFC2833 packet to be received in */
2108 payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 0, NULL, AST_RTP_DTMF);
2110 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2111 rtp->send_duration = 160;
2112 rtp->lastts += calc_txstamp(rtp, NULL) * DTMF_SAMPLE_RATE_MS;
2113 rtp->lastdigitts = rtp->lastts + rtp->send_duration;
2115 /* Create the actual packet that we will be sending */
2116 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
2117 rtpheader[1] = htonl(rtp->lastdigitts);
2118 rtpheader[2] = htonl(rtp->ssrc);
2120 /* Actually send the packet */
2121 for (i = 0; i < 2; i++) {
2124 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
2125 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
2127 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
2128 ast_sockaddr_stringify(&remote_address),
2131 update_address_with_ice_candidate(rtp, AST_RTP_ICE_COMPONENT_RTP, &remote_address);
2132 if (rtp_debug_test_addr(&remote_address)) {
2133 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2134 ast_sockaddr_stringify(&remote_address),
2135 ice ? " (via ICE)" : "",
2136 payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2139 rtp->send_duration += 160;
2140 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
2143 /* Record that we are in the process of sending a digit and information needed to continue doing so */
2144 rtp->sending_digit = 1;
2145 rtp->send_digit = digit;
2146 rtp->send_payload = payload;
2151 static int ast_rtp_dtmf_continuation(struct ast_rtp_instance *instance)
2153 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2154 struct ast_sockaddr remote_address = { {0,} };
2155 int hdrlen = 12, res = 0;
2157 unsigned int *rtpheader = (unsigned int*)data;
2160 ast_rtp_instance_get_remote_address(instance, &remote_address);
2162 /* Make sure we know where the other side is so we can send them the packet */
2163 if (ast_sockaddr_isnull(&remote_address)) {
2167 /* Actually create the packet we will be sending */
2168 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
2169 rtpheader[1] = htonl(rtp->lastdigitts);
2170 rtpheader[2] = htonl(rtp->ssrc);
2171 rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
2173 /* Boom, send it on out */
2174 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
2176 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
2177 ast_sockaddr_stringify(&remote_address),
2181 update_address_with_ice_candidate(rtp, AST_RTP_ICE_COMPONENT_RTP, &remote_address);
2183 if (rtp_debug_test_addr(&remote_address)) {
2184 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2185 ast_sockaddr_stringify(&remote_address),
2186 ice ? " (via ICE)" : "",
2187 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2190 /* And now we increment some values for the next time we swing by */
2192 rtp->send_duration += 160;
2193 rtp->lastts += calc_txstamp(rtp, NULL) * DTMF_SAMPLE_RATE_MS;
2198 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
2200 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2201 struct ast_sockaddr remote_address = { {0,} };
2202 int hdrlen = 12, res = -1, i = 0;
2204 unsigned int *rtpheader = (unsigned int*)data;
2205 unsigned int measured_samples;
2207 ast_rtp_instance_get_remote_address(instance, &remote_address);
2209 /* Make sure we know where the remote side is so we can send them the packet we construct */
2210 if (ast_sockaddr_isnull(&remote_address)) {
2214 /* Convert the given digit to the one we are going to send */
2215 if ((digit <= '9') && (digit >= '0')) {
2217 } else if (digit == '*') {
2219 } else if (digit == '#') {
2221 } else if ((digit >= 'A') && (digit <= 'D')) {
2222 digit = digit - 'A' + 12;
2223 } else if ((digit >= 'a') && (digit <= 'd')) {
2224 digit = digit - 'a' + 12;
2226 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
2230 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2232 if (duration > 0 && (measured_samples = duration * rtp_get_rate(&rtp->f.subclass.format) / 1000) > rtp->send_duration) {
2233 ast_debug(2, "Adjusting final end duration from %u to %u\n", rtp->send_duration, measured_samples);
2234 rtp->send_duration = measured_samples;
2237 /* Construct the packet we are going to send */
2238 rtpheader[1] = htonl(rtp->lastdigitts);
2239 rtpheader[2] = htonl(rtp->ssrc);
2240 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
2241 rtpheader[3] |= htonl((1 << 23));
2243 /* Send it 3 times, that's the magical number */
2244 for (i = 0; i < 3; i++) {
2247 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
2249 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
2252 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
2253 ast_sockaddr_stringify(&remote_address),
2257 update_address_with_ice_candidate(rtp, AST_RTP_ICE_COMPONENT_RTP, &remote_address);
2259 if (rtp_debug_test_addr(&remote_address)) {
2260 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2261 ast_sockaddr_stringify(&remote_address),
2262 ice ? " (via ICE)" : "",
2263 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2270 /* Oh and we can't forget to turn off the stuff that says we are sending DTMF */
2271 rtp->lastts += calc_txstamp(rtp, NULL) * DTMF_SAMPLE_RATE_MS;
2273 rtp->sending_digit = 0;
2274 rtp->send_digit = 0;
2279 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit)
2281 return ast_rtp_dtmf_end_with_duration(instance, digit, 0);
2284 static void ast_rtp_update_source(struct ast_rtp_instance *instance)
2286 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2288 /* We simply set this bit so that the next packet sent will have the marker bit turned on */
2289 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
2290 ast_debug(3, "Setting the marker bit due to a source update\n");
2295 static void ast_rtp_change_source(struct ast_rtp_instance *instance)
2297 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2298 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
2299 unsigned int ssrc = ast_random();
2302 ast_debug(3, "Not changing SSRC since we haven't sent any RTP yet\n");
2306 /* We simply set this bit so that the next packet sent will have the marker bit turned on */
2307 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
2309 ast_debug(3, "Changing ssrc from %u to %u due to a source change\n", rtp->ssrc, ssrc);
2312 ast_debug(3, "Changing ssrc for SRTP from %u to %u\n", rtp->ssrc, ssrc);
2313 res_srtp->change_source(srtp, rtp->ssrc, ssrc);
2321 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
2323 unsigned int sec, usec, frac;
2324 sec = tv.tv_sec + 2208988800u; /* Sec between 1900 and 1970 */
2326 frac = (usec << 12) + (usec << 8) - ((usec * 3650) >> 6);
2331 static void ntp2timeval(unsigned int msw, unsigned int lsw, struct timeval *tv)
2333 tv->tv_sec = msw - 2208988800u;
2334 tv->tv_usec = ((lsw << 6) / 3650) - (lsw >> 12) - (lsw >> 8);
2337 static void calculate_lost_packet_statistics(struct ast_rtp *rtp,
2338 unsigned int *lost_packets,
2341 unsigned int extended_seq_no;
2342 unsigned int expected_packets;
2343 unsigned int expected_interval;
2344 unsigned int received_interval;
2345 double rxlost_current;
2348 /* Compute statistics */
2349 extended_seq_no = rtp->cycles + rtp->lastrxseqno;
2350 expected_packets = extended_seq_no - rtp->seedrxseqno + 1;
2351 if (rtp->rxcount > expected_packets) {
2352 expected_packets += rtp->rxcount - expected_packets;
2354 *lost_packets = expected_packets - rtp->rxcount;
2355 expected_interval = expected_packets - rtp->rtcp->expected_prior;
2356 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
2357 lost_interval = expected_interval - received_interval;
2358 if (expected_interval == 0 || lost_interval <= 0) {
2361 *fraction_lost = (lost_interval << 8) / expected_interval;
2364 /* Update RTCP statistics */
2365 rtp->rtcp->received_prior = rtp->rxcount;
2366 rtp->rtcp->expected_prior = expected_packets;
2367 if (lost_interval <= 0) {
2368 rtp->rtcp->rxlost = 0;
2370 rtp->rtcp->rxlost = lost_interval;
2372 if (rtp->rtcp->rxlost_count == 0) {
2373 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
2375 if (lost_interval < rtp->rtcp->minrxlost) {
2376 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
2378 if (lost_interval > rtp->rtcp->maxrxlost) {
2379 rtp->rtcp->maxrxlost = rtp->rtcp->rxlost;
2381 rxlost_current = normdev_compute(rtp->rtcp->normdev_rxlost,
2383 rtp->rtcp->rxlost_count);
2384 rtp->rtcp->stdev_rxlost = stddev_compute(rtp->rtcp->stdev_rxlost,
2386 rtp->rtcp->normdev_rxlost,
2388 rtp->rtcp->rxlost_count);
2389 rtp->rtcp->normdev_rxlost = rxlost_current;
2390 rtp->rtcp->rxlost_count++;
2393 /*! \brief Send RTCP SR or RR report */
2394 static int ast_rtcp_write_report(struct ast_rtp_instance *instance, int sr)
2396 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2397 RAII_VAR(struct ast_json *, message_blob, NULL, ast_json_unref);
2401 unsigned int now_lsw;
2402 unsigned int now_msw;
2403 unsigned int *rtcpheader;
2404 unsigned int lost_packets;
2406 struct timeval dlsr = { 0, };
2408 int rate = rtp_get_rate(&rtp->f.subclass.format);
2410 int header_offset = 0;
2411 struct ast_sockaddr remote_address = { {0,} };
2412 struct ast_rtp_rtcp_report_block *report_block;
2413 RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report,
2414 ast_rtp_rtcp_report_alloc(1),
2417 if (!rtp || !rtp->rtcp) {
2421 if (ast_sockaddr_isnull(&rtp->rtcp->them)) { /* This'll stop rtcp for this rtp session */
2422 /* RTCP was stopped. */
2430 report_block = ast_calloc(1, sizeof(*report_block));
2431 if (!report_block) {
2435 /* Compute statistics */
2436 calculate_lost_packet_statistics(rtp, &lost_packets, &fraction_lost);
2438 gettimeofday(&now, NULL);
2439 rtcp_report->reception_report_count = 1;
2440 rtcp_report->ssrc = rtp->ssrc;
2441 rtcp_report->type = sr ? RTCP_PT_SR : RTCP_PT_RR;
2443 rtcp_report->sender_information.ntp_timestamp = now;
2444 rtcp_report->sender_information.rtp_timestamp = rtp->lastts;
2445 rtcp_report->sender_information.packet_count = rtp->txcount;
2446 rtcp_report->sender_information.octet_count = rtp->txoctetcount;
2448 rtcp_report->report_block[0] = report_block;
2449 report_block->source_ssrc = rtp->themssrc;
2450 report_block->lost_count.fraction = (fraction_lost & 0xff);
2451 report_block->lost_count.packets = (lost_packets & 0xffffff);
2452 report_block->highest_seq_no = (rtp->cycles | (rtp->lastrxseqno & 0xffff));
2453 report_block->ia_jitter = (unsigned int)(rtp->rxjitter * rate);
2454 report_block->lsr = rtp->rtcp->themrxlsr;
2455 /* If we haven't received an SR report, DLSR should be 0 */
2456 if (!ast_tvzero(rtp->rtcp->rxlsr)) {
2457 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
2458 report_block->dlsr = (((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000;
2460 timeval2ntp(rtcp_report->sender_information.ntp_timestamp, &now_msw, &now_lsw);
2461 rtcpheader = (unsigned int *)bdata;
2462 rtcpheader[1] = htonl(rtcp_report->ssrc); /* Our SSRC */
2466 rtcpheader[2] = htonl(now_msw); /* now, MSW. gettimeofday() + SEC_BETWEEN_1900_AND_1970*/
2467 rtcpheader[3] = htonl(now_lsw); /* now, LSW */
2468 rtcpheader[4] = htonl(rtcp_report->sender_information.rtp_timestamp);
2469 rtcpheader[5] = htonl(rtcp_report->sender_information.packet_count);
2470 rtcpheader[6] = htonl(rtcp_report->sender_information.octet_count);
2473 rtcpheader[2 + header_offset] = htonl(report_block->source_ssrc); /* Their SSRC */
2474 rtcpheader[3 + header_offset] = htonl((report_block->lost_count.fraction << 24) | report_block->lost_count.packets);
2475 rtcpheader[4 + header_offset] = htonl(report_block->highest_seq_no);
2476 rtcpheader[5 + header_offset] = htonl(report_block->ia_jitter);
2477 rtcpheader[6 + header_offset] = htonl(report_block->lsr);
2478 rtcpheader[7 + header_offset] = htonl(report_block->dlsr);
2480 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | ((sr ? RTCP_PT_SR : RTCP_PT_RR) << 16) | ((len/4)-1));
2482 /* Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos */
2483 /* it can change mid call, and SDES can't) */
2484 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
2485 rtcpheader[(len/4)+1] = htonl(rtcp_report->ssrc);
2486 rtcpheader[(len/4)+2] = htonl(0x01 << 24);
2489 ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
2490 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &remote_address, &ice);
2492 ast_log(LOG_ERROR, "RTCP %s transmission error to %s, rtcp halted %s\n",
2494 ast_sockaddr_stringify(&rtp->rtcp->them),
2499 /* Update RTCP SR/RR statistics */
2501 rtp->rtcp->txlsr = rtcp_report->sender_information.ntp_timestamp;
2502 rtp->rtcp->sr_count++;
2503 rtp->rtcp->lastsrtxcount = rtp->txcount;
2505 rtp->rtcp->rr_count++;
2508 update_address_with_ice_candidate(rtp, AST_RTP_ICE_COMPONENT_RTCP, &remote_address);
2510 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
2511 ast_verbose("* Sent RTCP %s to %s%s\n", sr ? "SR" : "RR",
2512 ast_sockaddr_stringify(&remote_address), ice ? " (via ICE)" : "");
2513 ast_verbose(" Our SSRC: %u\n", rtcp_report->ssrc);
2515 ast_verbose(" Sent(NTP): %u.%010u\n",
2516 (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_sec,
2517 (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_usec * 4096);
2518 ast_verbose(" Sent(RTP): %u\n", rtcp_report->sender_information.rtp_timestamp);
2519 ast_verbose(" Sent packets: %u\n", rtcp_report->sender_information.packet_count);
2520 ast_verbose(" Sent octets: %u\n", rtcp_report->sender_information.octet_count);
2522 ast_verbose(" Report block:\n");
2523 ast_verbose(" Their SSRC: %u\n", report_block->source_ssrc);
2524 ast_verbose(" Fraction lost: %u\n", report_block->lost_count.fraction);
2525 ast_verbose(" Cumulative loss: %u\n", report_block->lost_count.packets);
2526 ast_verbose(" Highest seq no: %u\n", report_block->highest_seq_no);
2527 ast_verbose(" IA jitter: %.4f\n", (double)report_block->ia_jitter / rate);
2528 ast_verbose(" Their last SR: %u\n", report_block->lsr);
2529 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(report_block->dlsr / 65536.0));
2532 message_blob = ast_json_pack("{s: s}",
2533 "to", ast_sockaddr_stringify(&remote_address));
2534 ast_rtp_publish_rtcp_message(instance, ast_rtp_rtcp_sent_type(),
2540 /*! \brief Write and RTCP packet to the far end
2541 * \note Decide if we are going to send an SR (with Reception Block) or RR
2542 * RR is sent if we have not sent any rtp packets in the previous interval */
2543 static int ast_rtcp_write(const void *data)
2545 struct ast_rtp_instance *instance = (struct ast_rtp_instance *) data;
2546 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2549 if (!rtp || !rtp->rtcp || rtp->rtcp->schedid == -1) {
2550 ao2_ref(instance, -1);
2554 if (rtp->txcount > rtp->rtcp->lastsrtxcount) {
2556 res = ast_rtcp_write_report(instance, 1);
2559 res = ast_rtcp_write_report(instance, 0);
2564 * Not being rescheduled.
2566 ao2_ref(instance, -1);
2567 rtp->rtcp->schedid = -1;
2573 static int ast_rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec)
2575 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2577 unsigned int ms = calc_txstamp(rtp, &frame->delivery);
2578 struct ast_sockaddr remote_address = { {0,} };
2579 int rate = rtp_get_rate(&frame->subclass.format) / 1000;
2581 if (frame->subclass.format.id == AST_FORMAT_G722) {
2582 frame->samples /= 2;
2585 if (rtp->sending_digit) {
2589 if (frame->frametype == AST_FRAME_VOICE) {
2590 pred = rtp->lastts + frame->samples;
2592 /* Re-calculate last TS */
2593 rtp->lastts = rtp->lastts + ms * rate;
2594 if (ast_tvzero(frame->delivery)) {
2595 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
2596 and if so, go with our prediction */
2597 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW) {
2600 ast_debug(3, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
2604 } else if (frame->frametype == AST_FRAME_VIDEO) {
2605 mark = ast_format_get_video_mark(&frame->subclass.format);
2606 pred = rtp->lastovidtimestamp + frame->samples;
2607 /* Re-calculate last TS */
2608 rtp->lastts = rtp->lastts + ms * 90;
2609 /* If it's close to our prediction, go for it */
2610 if (ast_tvzero(frame->delivery)) {
2611 if (abs(rtp->lastts - pred) < 7200) {
2613 rtp->lastovidtimestamp += frame->samples;
2615 ast_debug(3, "Difference is %d, ms is %d (%d), pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, frame->samples);
2616 rtp->lastovidtimestamp = rtp->lastts;
2620 pred = rtp->lastotexttimestamp + frame->samples;
2621 /* Re-calculate last TS */
2622 rtp->lastts = rtp->lastts + ms;
2623 /* If it's close to our prediction, go for it */
2624 if (ast_tvzero(frame->delivery)) {
2625 if (abs(rtp->lastts - pred) < 7200) {
2627 rtp->lastotexttimestamp += frame->samples;
2629 ast_debug(3, "Difference is %d, ms is %d, pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, rtp->lastts, pred, frame->samples);
2630 rtp->lastotexttimestamp = rtp->lastts;
2635 /* If we have been explicitly told to set the marker bit then do so */
2636 if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
2638 ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT);
2641 /* If the timestamp for non-digt packets has moved beyond the timestamp for digits, update the digit timestamp */
2642 if (rtp->lastts > rtp->lastdigitts) {
2643 rtp->lastdigitts = rtp->lastts;
2646 if (ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO)) {
2647 rtp->lastts = frame->ts * rate;
2650 ast_rtp_instance_get_remote_address(instance, &remote_address);
2652 /* If we know the remote address construct a packet and send it out */
2653 if (!ast_sockaddr_isnull(&remote_address)) {
2654 int hdrlen = 12, res, ice;
2655 unsigned char *rtpheader = (unsigned char *)(frame->data.ptr - hdrlen);
2657 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
2658 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
2659 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
2661 if ((res = rtp_sendto(instance, (void *)rtpheader, frame->datalen + hdrlen, 0, &remote_address, &ice)) < 0) {
2662 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))) {
2663 ast_debug(1, "RTP Transmission error of packet %d to %s: %s\n",
2665 ast_sockaddr_stringify(&remote_address),
2667 } else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) {
2668 /* Only give this error message once if we are not RTP debugging */
2670 ast_debug(0, "RTP NAT: Can't write RTP to private address %s, waiting for other end to send audio...\n",
2671 ast_sockaddr_stringify(&remote_address));
2672 ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN);
2676 rtp->txoctetcount += (res - hdrlen);
2678 if (rtp->rtcp && rtp->rtcp->schedid < 1) {
2679 ast_debug(1, "Starting RTCP transmission on RTP instance '%p'\n", instance);
2680 ao2_ref(instance, +1);
2681 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, instance);
2682 if (rtp->rtcp->schedid < 0) {
2683 ao2_ref(instance, -1);
2684 ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n");
2689 update_address_with_ice_candidate(rtp, AST_RTP_ICE_COMPONENT_RTP, &remote_address);
2691 if (rtp_debug_test_addr(&remote_address)) {
2692 ast_verbose("Sent RTP packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2693 ast_sockaddr_stringify(&remote_address),
2694 ice ? " (via ICE)" : "",
2695 codec, rtp->seqno, rtp->lastts, res - hdrlen);
2704 static struct ast_frame *red_t140_to_red(struct rtp_red *red) {
2705 unsigned char *data = red->t140red.data.ptr;
2709 /* replace most aged generation */
2711 for (i = 1; i < red->num_gen+1; i++)
2714 memmove(&data[red->hdrlen], &data[red->hdrlen+red->len[0]], len);
2717 /* Store length of each generation and primary data length*/
2718 for (i = 0; i < red->num_gen; i++)
2719 red->len[i] = red->len[i+1];
2720 red->len[i] = red->t140.datalen;
2722 /* write each generation length in red header */
2724 for (i = 0; i < red->num_gen; i++) {
2725 len += data[i*4+3] = red->len[i];
2728 /* add primary data to buffer */
2729 memcpy(&data[len], red->t140.data.ptr, red->t140.datalen);
2730 red->t140red.datalen = len + red->t140.datalen;
2732 /* no primary data and no generations to send */
2733 if (len == red->hdrlen && !red->t140.datalen) {
2737 /* reset t.140 buffer */
2738 red->t140.datalen = 0;
2740 return &red->t140red;
2743 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
2745 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2746 struct ast_sockaddr remote_address = { {0,} };
2747 struct ast_format subclass;
2750 ast_rtp_instance_get_remote_address(instance, &remote_address);
2752 /* If we don't actually know the remote address don't even bother doing anything */
2753 if (ast_sockaddr_isnull(&remote_address)) {
2754 ast_debug(1, "No remote address on RTP instance '%p' so dropping frame\n", instance);
2758 /* VP8: is this a request to send a RTCP FIR? */
2759 if (frame->frametype == AST_FRAME_CONTROL && frame->subclass.integer == AST_CONTROL_VIDUPDATE) {
2760 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2761 unsigned int *rtcpheader;
2767 if (!rtp || !rtp->rtcp) {
2771 if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
2778 /* Prepare RTCP FIR (PT=206, FMT=4) */
2779 rtp->rtcp->firseq++;
2780 if(rtp->rtcp->firseq == 256) {
2781 rtp->rtcp->firseq = 0;
2784 rtcpheader = (unsigned int *)bdata;
2785 rtcpheader[0] = htonl((2 << 30) | (4 << 24) | (RTCP_PT_PSFB << 16) | ((len/4)-1));
2786 rtcpheader[1] = htonl(rtp->ssrc);
2787 rtcpheader[2] = htonl(rtp->themssrc);
2788 rtcpheader[3] = htonl(rtp->themssrc); /* FCI: SSRC */
2789 rtcpheader[4] = htonl(rtp->rtcp->firseq << 24); /* FCI: Sequence number */
2790 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &rtp->rtcp->them, &ice);
2792 ast_log(LOG_ERROR, "RTCP FIR transmission error: %s\n", strerror(errno));
2797 /* If there is no data length we can't very well send the packet */
2798 if (!frame->datalen) {
2799 ast_debug(1, "Received frame with no data for RTP instance '%p' so dropping frame\n", instance);
2803 /* If the packet is not one our RTP stack supports bail out */
2804 if (frame->frametype != AST_FRAME_VOICE && frame->frametype != AST_FRAME_VIDEO && frame->frametype != AST_FRAME_TEXT) {
2805 ast_log(LOG_WARNING, "RTP can only send voice, video, and text\n");
2811 /* no primary data or generations to send */
2812 if ((frame = red_t140_to_red(rtp->red)) == NULL)
2816 /* Grab the subclass and look up the payload we are going to use */
2817 ast_format_copy(&subclass, &frame->subclass.format);
2818 if ((codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 1, &subclass, 0)) < 0) {
2819 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(&frame->subclass.format));
2823 /* Oh dear, if the format changed we will have to set up a new smoother */
2824 if (ast_format_cmp(&rtp->lasttxformat, &subclass) == AST_FORMAT_CMP_NOT_EQUAL) {
2825 ast_debug(1, "Ooh, format changed from %s to %s\n", ast_getformatname(&rtp->lasttxformat), ast_getformatname(&subclass));
2826 rtp->lasttxformat = subclass;
2827 ast_format_copy(&rtp->lasttxformat, &subclass);
2828 if (rtp->smoother) {
2829 ast_smoother_free(rtp->smoother);
2830 rtp->smoother = NULL;
2834 /* If no smoother is present see if we have to set one up */
2835 if (!rtp->smoother) {
2836 struct ast_format_list fmt = ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance)->pref, &subclass);
2838 switch (subclass.id) {
2839 case AST_FORMAT_SPEEX:
2840 case AST_FORMAT_SPEEX16:
2841 case AST_FORMAT_SPEEX32:
2842 case AST_FORMAT_SILK:
2843 case AST_FORMAT_CELT:
2844 case AST_FORMAT_G723_1:
2845 case AST_FORMAT_SIREN7:
2846 case AST_FORMAT_SIREN14:
2847 case AST_FORMAT_G719:
2849 case AST_FORMAT_OPUS:
2850 /* these are all frame-based codecs and cannot be safely run through
2855 if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
2856 ast_log(LOG_WARNING, "Unable to create smoother: format %s ms: %d len: %d\n", ast_getformatname(&subclass), fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
2860 ast_smoother_set_flags(rtp->smoother, fmt.flags);
2862 ast_debug(1, "Created smoother: format: %s ms: %d len: %d\n", ast_getformatname(&subclass), fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
2867 /* Feed audio frames into the actual function that will create a frame and send it */
2868 if (rtp->smoother) {
2869 struct ast_frame *f;
2871 if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
2872 ast_smoother_feed_be(rtp->smoother, frame);
2874 ast_smoother_feed(rtp->smoother, frame);
2877 while ((f = ast_smoother_read(rtp->smoother)) && (f->data.ptr)) {
2878 ast_rtp_raw_write(instance, f, codec);
2882 struct ast_frame *f = NULL;
2884 if (frame->offset < hdrlen) {
2885 f = ast_frdup(frame);
2890 ast_rtp_raw_write(instance, f, codec);
2901 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
2906 double current_time;
2910 int rate = rtp_get_rate(&rtp->f.subclass.format);
2912 double normdev_rxjitter_current;
2913 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
2914 gettimeofday(&rtp->rxcore, NULL);
2915 rtp->drxcore = (double) rtp->rxcore.tv_sec + (double) rtp->rxcore.tv_usec / 1000000;
2916 /* map timestamp to a real time */
2917 rtp->seedrxts = timestamp; /* Their RTP timestamp started with this */
2918 tmp = ast_samp2tv(timestamp, rate);
2919 rtp->rxcore = ast_tvsub(rtp->rxcore, tmp);
2920 /* Round to 0.1ms for nice, pretty timestamps */
2921 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
2924 gettimeofday(&now,NULL);
2925 /* rxcore is the mapping between the RTP timestamp and _our_ real time from gettimeofday() */
2926 tmp = ast_samp2tv(timestamp, rate);
2927 *tv = ast_tvadd(rtp->rxcore, tmp);
2929 prog = (double)((timestamp-rtp->seedrxts)/(float)(rate));
2930 dtv = (double)rtp->drxcore + (double)(prog);
2931 current_time = (double)now.tv_sec + (double)now.tv_usec/1000000;
2932 transit = current_time - dtv;
2933 d = transit - rtp->rxtransit;
2934 rtp->rxtransit = transit;
2938 rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
2940 if (rtp->rxjitter > rtp->rtcp->maxrxjitter)
2941 rtp->rtcp->maxrxjitter = rtp->rxjitter;
2942 if (rtp->rtcp->rxjitter_count == 1)
2943 rtp->rtcp->minrxjitter = rtp->rxjitter;
2944 if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter)
2945 rtp->rtcp->minrxjitter = rtp->rxjitter;
2947 normdev_rxjitter_current = normdev_compute(rtp->rtcp->normdev_rxjitter,rtp->rxjitter,rtp->rtcp->rxjitter_count);
2948 rtp->rtcp->stdev_rxjitter = stddev_compute(rtp->rtcp->stdev_rxjitter,rtp->rxjitter,rtp->rtcp->normdev_rxjitter,normdev_rxjitter_current,rtp->rtcp->rxjitter_count);
2950 rtp->rtcp->normdev_rxjitter = normdev_rxjitter_current;
2951 rtp->rtcp->rxjitter_count++;
2955 static struct ast_frame *create_dtmf_frame(struct ast_rtp_instance *instance, enum ast_frame_type type, int compensate)
2957 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2958 struct ast_sockaddr remote_address = { {0,} };
2960 ast_rtp_instance_get_remote_address(instance, &remote_address);
2962 if (((compensate && type == AST_FRAME_DTMF_END) || (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
2963 ast_debug(1, "Ignore potential DTMF echo from '%s'\n",
2964 ast_sockaddr_stringify(&remote_address));
2966 rtp->dtmfsamples = 0;
2967 return &ast_null_frame;
2969 ast_debug(1, "Creating %s DTMF Frame: %d (%c), at %s\n",
2970 type == AST_FRAME_DTMF_END ? "END" : "BEGIN",
2971 rtp->resp, rtp->resp,
2972 ast_sockaddr_stringify(&remote_address));
2973 if (rtp->resp == 'X') {
2974 rtp->f.frametype = AST_FRAME_CONTROL;
2975 rtp->f.subclass.integer = AST_CONTROL_FLASH;
2977 rtp->f.frametype = type;
2978 rtp->f.subclass.integer = rtp->resp;
2984 AST_LIST_NEXT(&rtp->f, frame_list) = NULL;
2989 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)
2991 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2992 struct ast_sockaddr remote_address = { {0,} };
2993 unsigned int event, event_end, samples;
2995 struct ast_frame *f = NULL;
2997 ast_rtp_instance_get_remote_address(instance, &remote_address);
2999 /* Figure out event, event end, and samples */
3000 event = ntohl(*((unsigned int *)(data)));
3002 event_end = ntohl(*((unsigned int *)(data)));
3005 samples = ntohl(*((unsigned int *)(data)));
3008 if (rtp_debug_test_addr(&remote_address)) {
3009 ast_verbose("Got RTP RFC2833 from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u, mark %d, event %08x, end %d, duration %-5.5d) \n",
3010 ast_sockaddr_stringify(&remote_address),
3011 payloadtype, seqno, timestamp, len, (mark?1:0), event, ((event_end & 0x80)?1:0), samples);
3014 /* Print out debug if turned on */
3016 ast_debug(0, "- RTP 2833 Event: %08x (len = %d)\n", event, len);
3018 /* Figure out what digit was pressed */
3021 } else if (event < 11) {
3023 } else if (event < 12) {
3025 } else if (event < 16) {
3026 resp = 'A' + (event - 12);
3027 } else if (event < 17) { /* Event 16: Hook flash */
3030 /* Not a supported event */
3031 ast_debug(1, "Ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", event);
3035 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) {
3036 if ((rtp->last_end_timestamp != timestamp) || (rtp->resp && rtp->resp != resp)) {
3038 rtp->dtmf_timeout = 0;
3039 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)));
3041 rtp->last_end_timestamp = timestamp;
3042 AST_LIST_INSERT_TAIL(frames, f, frame_list);
3045 /* The duration parameter measures the complete
3046 duration of the event (from the beginning) - RFC2833.
3047 Account for the fact that duration is only 16 bits long
3048 (about 8 seconds at 8000 Hz) and can wrap is digit
3049 is hold for too long. */
3050 unsigned int new_duration = rtp->dtmf_duration;
3051 unsigned int last_duration = new_duration & 0xFFFF;
3053 if (last_duration > 64000 && samples < last_duration) {
3054 new_duration += 0xFFFF + 1;
3056 new_duration = (new_duration & ~0xFFFF) | samples;
3058 if (event_end & 0x80) {
3060 if ((rtp->last_seqno != seqno) && (timestamp > rtp->last_end_timestamp)) {
3061 rtp->last_end_timestamp = timestamp;
3062 rtp->dtmf_duration = new_duration;
3064 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
3065 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0));
3067 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
3068 AST_LIST_INSERT_TAIL(frames, f, frame_list);
3069 } else if (rtpdebug) {
3070 ast_debug(1, "Dropping duplicate or out of order DTMF END frame (seqno: %d, ts %d, digit %c)\n",
3071 seqno, timestamp, resp);
3074 /* Begin/continuation */
3076 /* The second portion of the seqno check is to not mistakenly
3077 * stop accepting DTMF if the seqno rolls over beyond
3080 if ((rtp->last_seqno > seqno && rtp->last_seqno - seqno < 50)
3081 || timestamp <= rtp->last_end_timestamp) {
3082 /* Out of order frame. Processing this can cause us to
3083 * improperly duplicate incoming DTMF, so just drop
3087 ast_debug(1, "Dropping out of order DTMF frame (seqno %d, ts %d, digit %c)\n",
3088 seqno, timestamp, resp);
3093 if (rtp->resp && rtp->resp != resp) {
3094 /* Another digit already began. End it */
3095 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
3096 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0));
3098 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
3099 AST_LIST_INSERT_TAIL(frames, f, frame_list);
3103 /* Digit continues */
3104 rtp->dtmf_duration = new_duration;
3106 /* New digit began */
3108 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0));
3109 rtp->dtmf_duration = samples;
3110 AST_LIST_INSERT_TAIL(frames, f, frame_list);
3113 rtp->dtmf_timeout = timestamp + rtp->dtmf_duration + dtmftimeout;
3116 rtp->last_seqno = seqno;
3119 rtp->dtmfsamples = samples;
3124 static struct ast_frame *process_dtmf_cisco(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, struct ast_sockaddr *addr, int payloadtype, int mark)
3126 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3127 unsigned int event, flags, power;
3130 struct ast_frame *f = NULL;
3136 /* The format of Cisco RTP DTMF packet looks like next:
3137 +0 - sequence number of DTMF RTP packet (begins from 1,
3140 +1 (bit 0) - flaps by different DTMF digits delimited by audio
3141 or repeated digit without audio???
3142 +2 (+4,+6,...) - power level? (rises from 0 to 32 at begin of tone
3143 then falls to 0 at its end)
3144 +3 (+5,+7,...) - detected DTMF digit (0..9,*,#,A-D,...)
3145 Repeated DTMF information (bytes 4/5, 6/7) is history shifted right
3146 by each new packet and thus provides some redudancy.
3148 Sample of Cisco RTP DTMF packet is (all data in hex):
3149 19 07 00 02 12 02 20 02
3150 showing end of DTMF digit '2'.
3153 27 07 00 02 0A 02 20 02
3154 28 06 20 02 00 02 0A 02
3155 shows begin of new digit '2' with very short pause (20 ms) after
3156 previous digit '2'. Bit +1.0 flips at begin of new digit.
3158 Cisco RTP DTMF packets comes as replacement of audio RTP packets
3159 so its uses the same sequencing and timestamping rules as replaced
3160 audio packets. Repeat interval of DTMF packets is 20 ms and not rely
3161 on audio framing parameters. Marker bit isn't used within stream of
3162 DTMFs nor audio stream coming immediately after DTMF stream. Timestamps
3163 are not sequential at borders between DTMF and audio streams,
3169 event = data[3] & 0x1f;
3172 ast_debug(0, "Cisco DTMF Digit: %02x (len=%d, seq=%d, flags=%02x, power=%d, history count=%d)\n", event, len, seq, flags, power, (len - 4) / 2);
3175 } else if (event < 11) {
3177 } else if (event < 12) {
3179 } else if (event < 16) {
3180 resp = 'A' + (event - 12);
3181 } else if (event < 17) {
3184 if ((!rtp->resp && power) || (rtp->resp && (rtp->resp != resp))) {
3186 /* Why we should care on DTMF compensation at reception? */
3187 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) {
3188 f = create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0);
3189 rtp->dtmfsamples = 0;
3191 } else if ((rtp->resp == resp) && !power) {
3192 f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE));
3193 f->samples = rtp->dtmfsamples * (rtp->lastrxformat.id ? (rtp_get_rate(&rtp->lastrxformat) / 1000) : 8);
3195 } else if (rtp->resp == resp) {
3196 rtp->dtmfsamples += 20 * (rtp->lastrxformat.id ? (rtp_get_rate(&rtp->lastrxformat) / 1000) : 8);
3199 rtp->dtmf_timeout = 0;
3204 static struct ast_frame *process_cn_rfc3389(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, struct ast_sockaddr *addr, int payloadtype, int mark)
3206 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3208 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
3209 totally help us out becuase we don't have an engine to keep it going and we are not
3210 guaranteed to have it every 20ms or anything */
3212 ast_debug(0, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", (int) rtp->lastrxformat.id, len);
3215 if (ast_test_flag(rtp, FLAG_3389_WARNING)) {
3216 struct ast_sockaddr remote_address = { {0,} };
3218 ast_rtp_instance_get_remote_address(instance, &remote_address);
3220 ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client address: %s\n",
3221 ast_sockaddr_stringify(&remote_address));
3222 ast_set_flag(rtp, FLAG_3389_WARNING);
3225 /* Must have at least one byte */
3230 rtp->f.data.ptr = rtp->rawdata + AST_FRIENDLY_OFFSET;
3231 rtp->f.datalen = len - 1;
3232 rtp->f.offset = AST_FRIENDLY_OFFSET;
3233 memcpy(rtp->f.data.ptr, data + 1, len - 1);
3235 rtp->f.data.ptr = NULL;
3239 rtp->f.frametype = AST_FRAME_CNG;
3240 rtp->f.subclass.integer = data[0] & 0x7f;
3242 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
3247 static int update_rtt_stats(struct ast_rtp *rtp, unsigned int lsr, unsigned int dlsr)
3250 struct timeval rtt_tv;
3253 unsigned int rtt_msw;
3254 unsigned int rtt_lsw;
3257 double normdevrtt_current;
3259 gettimeofday(&now, NULL);
3260 timeval2ntp(now, &msw, &lsw);
3262 lsr_a = ((msw & 0x0000ffff) << 16) | ((lsw & 0xffff0000) >> 16);
3263 rtt = lsr_a - lsr - dlsr;
3264 rtt_msw = (rtt & 0xffff0000) >> 16;
3265 rtt_lsw = (rtt & 0x0000ffff) << 16;
3266 rtt_tv.tv_sec = rtt_msw;
3267 rtt_tv.tv_usec = ((rtt_lsw << 6) / 3650) - (rtt_lsw >> 12) - (rtt_lsw >> 8);
3268 rtp->rtcp->rtt = (double)rtt_tv.tv_sec + ((double)rtt_tv.tv_usec / 1000000);
3269 if (lsr_a - dlsr < lsr) {
3273 rtp->rtcp->accumulated_transit += rtp->rtcp->rtt;
3274 if (rtp->rtcp->rtt_count == 0 || rtp->rtcp->minrtt > rtp->rtcp->rtt) {
3275 rtp->rtcp->minrtt = rtp->rtcp->rtt;
3277 if (rtp->rtcp->maxrtt < rtp->rtcp->rtt) {
3278 rtp->rtcp->maxrtt = rtp->rtcp->rtt;
3281 normdevrtt_current = normdev_compute(rtp->rtcp->normdevrtt,
3283 rtp->rtcp->rtt_count);
3284 rtp->rtcp->stdevrtt = stddev_compute(rtp->rtcp->stdevrtt,
3286 rtp->rtcp->normdevrtt,
3288 rtp->rtcp->rtt_count);
3289 rtp->rtcp->normdevrtt = normdevrtt_current;
3290 rtp->rtcp->rtt_count++;
3297 * \brief Update RTCP interarrival jitter stats
3299 static void update_jitter_stats(struct ast_rtp *rtp, unsigned int ia_jitter)
3301 double reported_jitter;
3302 double reported_normdev_jitter_current;
3304 rtp->rtcp->reported_jitter = ia_jitter;
3305 reported_jitter = (double) rtp->rtcp->reported_jitter;
3306 if (rtp->rtcp->reported_jitter_count == 0) {
3307 rtp->rtcp->reported_minjitter = reported_jitter;
3309 if (reported_jitter < rtp->rtcp->reported_minjitter) {
3310 rtp->rtcp->reported_minjitter = reported_jitter;
3312 if (reported_jitter > rtp->rtcp->reported_maxjitter) {
3313 rtp->rtcp->reported_maxjitter = reported_jitter;
3315 reported_normdev_jitter_current = normdev_compute(rtp->rtcp->reported_normdev_jitter, reported_jitter, rtp->rtcp->reported_jitter_count);
3316 rtp->rtcp->reported_stdev_jitter = stddev_compute(rtp->rtcp->reported_stdev_jitter, reported_jitter, rtp->rtcp->reported_normdev_jitter, reported_normdev_jitter_current, rtp->rtcp->reported_jitter_count);
3317 rtp->rtcp->reported_normdev_jitter = reported_normdev_jitter_current;
3322 * \brief Update RTCP lost packet stats
3324 static void update_lost_stats(struct ast_rtp *rtp, unsigned int lost_packets)
3326 double reported_lost;
3327 double reported_normdev_lost_current;
3329 rtp->rtcp->reported_lost = lost_packets;
3330 reported_lost = (double)rtp->rtcp->reported_lost;
3331 if (rtp->rtcp->reported_jitter_count == 0) {
3332 rtp->rtcp->reported_minlost = reported_lost;
3334 if (reported_lost < rtp->rtcp->reported_minlost) {
3335 rtp->rtcp->reported_minlost = reported_lost;
3337 if (reported_lost > rtp->rtcp->reported_maxlost) {
3338 rtp->rtcp->reported_maxlost = reported_lost;
3340 reported_normdev_lost_current = normdev_compute(rtp->rtcp->reported_normdev_lost, reported_lost, rtp->rtcp->reported_jitter_count);
3341 rtp->rtcp->reported_stdev_lost = stddev_compute(rtp->rtcp->reported_stdev_lost, reported_lost, rtp->rtcp->reported_normdev_lost, reported_normdev_lost_current, rtp->rtcp->reported_jitter_count);
3342 rtp->rtcp->reported_normdev_lost = reported_normdev_lost_current;
3345 static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance)
3347 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3348 struct ast_sockaddr addr;
3349 unsigned char rtcpdata[8192 + AST_FRIENDLY_OFFSET];
3350 unsigned int *rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
3351 int res, packetwords, position = 0;
3352 int report_counter = 0;
3353 struct ast_rtp_rtcp_report_block *report_block;
3354 struct ast_frame *f = &ast_null_frame;
3356 /* Read in RTCP data from the socket */
3357 if ((res = rtcp_recvfrom(instance, rtcpdata + AST_FRIENDLY_OFFSET,
3358 sizeof(rtcpdata) - AST_FRIENDLY_OFFSET,
3360 ast_assert(errno != EBADF);
3361 if (errno != EAGAIN) {
3362 ast_log(LOG_WARNING, "RTCP Read error: %s. Hanging up.\n",
3363 (errno) ? strerror(errno) : "Unspecified");
3366 return &ast_null_frame;
3369 /* If this was handled by the ICE session don't do anything further */
3371 return &ast_null_frame;
3374 if (!*(rtcpdata + AST_FRIENDLY_OFFSET)) {
3375 struct sockaddr_in addr_tmp;
3376 struct ast_sockaddr addr_v4;
3378 if (ast_sockaddr_is_ipv4(&addr)) {
3379 ast_sockaddr_to_sin(&addr, &addr_tmp);
3380 } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) {
3381 ast_debug(1, "Using IPv6 mapped address %s for STUN\n",
3382 ast_sockaddr_stringify(&addr));
3383 ast_sockaddr_to_sin(&addr_v4, &addr_tmp);
3385 ast_debug(1, "Cannot do STUN for non IPv4 address %s\n",
3386 ast_sockaddr_stringify(&addr));
3387 return &ast_null_frame;
3389 if ((ast_stun_handle_packet(rtp->rtcp->s, &addr_tmp, rtcpdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == AST_STUN_ACCEPT)) {
3390 ast_sockaddr_from_sin(&addr, &addr_tmp);
3391 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
3393 return &ast_null_frame;
3396 packetwords = res / 4;
3398 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT)) {
3399 /* Send to whoever sent to us */
3400 if (ast_sockaddr_cmp(&rtp->rtcp->them, &addr)) {
3401 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
3403 ast_debug(0, "RTCP NAT: Got RTCP from other end. Now sending to address %s\n",
3404 ast_sockaddr_stringify(&rtp->rtcp->them));
3409 ast_debug(1, "Got RTCP report of %d bytes\n", res);
3411 while (position < packetwords) {
3413 unsigned int length;
3414 struct ast_json *message_blob;
3415 RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report, NULL, ao2_cleanup);
3418 length = ntohl(rtcpheader[i]);
3419 pt = (length & 0xff0000) >> 16;
3420 rc = (length & 0x1f000000) >> 24;
3423 rtcp_report = ast_rtp_rtcp_report_alloc(rc);
3425 return &ast_null_frame;
3427 rtcp_report->reception_report_count = rc;
3428 rtcp_report->ssrc = ntohl(rtcpheader[i + 1]);
3430 if ((i + length) > packetwords) {
3432 ast_debug(1, "RTCP Read too short\n");
3434 return &ast_null_frame;
3437 if (rtcp_debug_test_addr(&addr)) {
3438 ast_verbose("\n\nGot RTCP from %s\n",
3439 ast_sockaddr_stringify(&addr));
3440 ast_verbose("PT: %d(%s)\n", pt, (pt == RTCP_PT_SR) ? "Sender Report" :
3441 (pt == RTCP_PT_RR) ? "Receiver Report" :
3442 (pt == RTCP_PT_FUR) ? "H.261 FUR" : "Unknown");
3443 ast_verbose("Reception reports: %d\n", rc);
3444 ast_verbose("SSRC of sender: %u\n", rtcp_report->ssrc);
3447 i += 2; /* Advance past header and ssrc */
3448 if (rc == 0 && pt == RTCP_PT_RR) {
3449 /* We're receiving a receiver report with no reports, which is ok */
3450 position += (length + 1);
3455 gettimeofday(&rtp->rtcp->rxlsr, NULL);
3456 rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16);
3457 rtp->rtcp->spc = ntohl(rtcpheader[i + 3]);
3458 rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
3460 rtcp_report->type = RTCP_PT_SR;
3461 rtcp_report->sender_information.packet_count = rtp->rtcp->spc;
3462 rtcp_report->sender_information.octet_count = rtp->rtcp->soc;
3463 ntp2timeval((unsigned int)ntohl(rtcpheader[i]),
3464 (unsigned int)ntohl(rtcpheader[i + 1]),
3465 &rtcp_report->sender_information.ntp_timestamp);
3466 rtcp_report->sender_information.rtp_timestamp = ntohl(rtcpheader[i + 2]);
3467 if (rtcp_debug_test_addr(&addr)) {
3468 ast_verbose("NTP timestamp: %u.%010u\n",
3469 (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_sec,
3470 (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_usec * 4096);
3471 ast_verbose("RTP timestamp: %u\n", rtcp_report->sender_information.rtp_timestamp);
3472 ast_verbose("SPC: %u\tSOC: %u\n",
3473 rtcp_report->sender_information.packet_count,
3474 rtcp_report->sender_information.octet_count);
3480 /* Intentional fall through */
3482 if (rtcp_report->type != RTCP_PT_SR) {
3483 rtcp_report->type = RTCP_PT_RR;
3486 /* Don't handle multiple reception reports (rc > 1) yet */
3487 report_block = ast_calloc(1, sizeof(*report_block));
3488 if (!report_block) {
3489 return &ast_null_frame;
3491 rtcp_report->report_block[report_counter] = report_block;
3492 report_block->source_ssrc = ntohl(rtcpheader[i]);
3493 report_block->lost_count.packets = ntohl(rtcpheader[i + 1]) & 0x00ffffff;
3494 report_block->lost_count.fraction = ((ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24);
3495 report_block->highest_seq_no = ntohl(rtcpheader[i + 2]);
3496 report_block->ia_jitter = ntohl(rtcpheader[i + 3]);
3497 report_block->lsr = ntohl(rtcpheader[i + 4]);
3498 report_block->dlsr = ntohl(rtcpheader[i + 5]);
3499 if (report_block->lsr
3500 && update_rtt_stats(rtp, report_block->lsr, report_block->dlsr)
3501 && rtcp_debug_test_addr(&addr)) {
3503 unsigned int lsr_now, lsw, msw;
3504 gettimeofday(&now, NULL);
3505 timeval2ntp(now, &msw, &lsw);
3506 lsr_now = (((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16));
3507 ast_verbose("Internal RTCP NTP clock skew detected: "
3508 "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
3510 report_block->lsr, lsr_now, report_block->dlsr, report_block->dlsr / 65536,
3511 (report_block->dlsr % 65536) * 1000 / 65536,
3512 report_block->dlsr - (lsr_now - report_block->lsr));
3514 update_jitter_stats(rtp, report_block->ia_jitter);
3515 update_lost_stats(rtp, report_block->lost_count.packets);
3516 rtp->rtcp->reported_jitter_count++;
3518 if (rtcp_debug_test_addr(&addr)) {
3519 ast_verbose(" Fraction lost: %u\n", report_block->lost_count.fraction);
3520 ast_verbose(" Packets lost so far: %u\n", report_block->lost_count.packets);
3521 ast_verbose(" Highest sequence number: %u\n", report_block->highest_seq_no & 0x0000ffff);
3522 ast_verbose(" Sequence number cycles: %u\n", report_block->highest_seq_no >> 16);
3523 ast_verbose(" Interarrival jitter: %u\n", report_block->ia_jitter);
3524 ast_verbose(" Last SR(our NTP): %lu.%010lu\n",(unsigned long)(report_block->lsr) >> 16,((unsigned long)(report_block->lsr) << 16) * 4096);
3525 ast_verbose(" DLSR: %4.4f (sec)\n",(double)report_block->dlsr / 65536.0);
3526 ast_verbose(" RTT: %4.4f(sec)\n", rtp->rtcp->rtt);
3530 /* If and when we handle more than one report block, this should occur outside
3533 message_blob = ast_json_pack("{s: s, s: f}",
3534 "from", ast_sockaddr_stringify(&addr),
3535 "rtt", rtp->rtcp->rtt);
3536 ast_rtp_publish_rtcp_message(instance, ast_rtp_rtcp_received_type(),
3539 ast_json_unref(message_blob);
3542 /* Handle RTCP FIR as FUR */
3544 if (rtcp_debug_test_addr(&addr)) {
3545 ast_verbose("Received an RTCP Fast Update Request\n");
3547 rtp->f.frametype = AST_FRAME_CONTROL;
3548 rtp->f.subclass.integer = AST_CONTROL_VIDUPDATE;
3556 if (rtcp_debug_test_addr(&addr)) {
3557 ast_verbose("Received an SDES from %s\n",
3558 ast_sockaddr_stringify(&rtp->rtcp->them));
3562 if (rtcp_debug_test_addr(&addr)) {
3563 ast_verbose("Received a BYE from %s\n",
3564 ast_sockaddr_stringify(&rtp->rtcp->them));
3568 ast_debug(1, "Unknown RTCP packet (pt=%d) received from %s\n",
3569 pt, ast_sockaddr_stringify(&rtp->rtcp->them));
3572 position += (length + 1);
3574 rtp->rtcp->rtcp_info = 1;
3579 static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, unsigned int *rtpheader, int len, int hdrlen)
3581 struct ast_rtp_instance *instance1 = ast_rtp_instance_get_bridged(instance);
3582 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance), *bridged = ast_rtp_instance_get_data(instance1);
3583 int res = 0, payload = 0, bridged_payload = 0, mark;
3584 struct ast_rtp_payload_type payload_type;
3585 int reconstruct = ntohl(rtpheader[0]);
3586 struct ast_sockaddr remote_address = { {0,} };