00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "asterisk.h"
00036
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 374677 $")
00038
00039 #include <sys/time.h>
00040 #include <signal.h>
00041 #include <fcntl.h>
00042
00043 #ifdef HAVE_OPENSSL_SRTP
00044 #include <openssl/ssl.h>
00045 #include <openssl/err.h>
00046 #include <openssl/bio.h>
00047 #endif
00048
00049
00050
00051 #undef bzero
00052 #define bzero bzero
00053 #include "pjlib.h"
00054 #include "pjlib-util.h"
00055 #include "pjnath.h"
00056
00057 #include "asterisk/stun.h"
00058 #include "asterisk/pbx.h"
00059 #include "asterisk/frame.h"
00060 #include "asterisk/channel.h"
00061 #include "asterisk/acl.h"
00062 #include "asterisk/config.h"
00063 #include "asterisk/lock.h"
00064 #include "asterisk/utils.h"
00065 #include "asterisk/cli.h"
00066 #include "asterisk/manager.h"
00067 #include "asterisk/unaligned.h"
00068 #include "asterisk/module.h"
00069 #include "asterisk/rtp_engine.h"
00070
00071 #define MAX_TIMESTAMP_SKEW 640
00072
00073 #define RTP_SEQ_MOD (1<<16)
00074 #define RTCP_DEFAULT_INTERVALMS 5000
00075 #define RTCP_MIN_INTERVALMS 500
00076 #define RTCP_MAX_INTERVALMS 60000
00077
00078 #define DEFAULT_RTP_START 5000
00079 #define DEFAULT_RTP_END 31000
00080
00081 #define MINIMUM_RTP_PORT 1024
00082 #define MAXIMUM_RTP_PORT 65535
00083
00084 #define DEFAULT_TURN_PORT 34780
00085
00086 #define TURN_ALLOCATION_WAIT_TIME 2000
00087
00088 #define RTCP_PT_FUR 192
00089 #define RTCP_PT_SR 200
00090 #define RTCP_PT_RR 201
00091 #define RTCP_PT_SDES 202
00092 #define RTCP_PT_BYE 203
00093 #define RTCP_PT_APP 204
00094
00095 #define RTP_MTU 1200
00096
00097 #define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000))
00098
00099 #define ZFONE_PROFILE_ID 0x505a
00100
00101 #define DEFAULT_LEARNING_MIN_SEQUENTIAL 4
00102
00103 #define SRTP_MASTER_KEY_LEN 16
00104 #define SRTP_MASTER_SALT_LEN 14
00105 #define SRTP_MASTER_LEN (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN)
00106
00107 enum strict_rtp_state {
00108 STRICT_RTP_OPEN = 0,
00109 STRICT_RTP_LEARN,
00110 STRICT_RTP_CLOSED,
00111 };
00112
00113 #define DEFAULT_STRICT_RTP STRICT_RTP_CLOSED
00114 #define DEFAULT_ICESUPPORT 0
00115
00116 extern struct ast_srtp_res *res_srtp;
00117 extern struct ast_srtp_policy_res *res_srtp_policy;
00118
00119 static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
00120
00121 static int rtpstart = DEFAULT_RTP_START;
00122 static int rtpend = DEFAULT_RTP_END;
00123 static int rtpdebug;
00124 static int rtcpdebug;
00125 static int rtcpstats;
00126 static int rtcpinterval = RTCP_DEFAULT_INTERVALMS;
00127 static struct ast_sockaddr rtpdebugaddr;
00128 static struct ast_sockaddr rtcpdebugaddr;
00129 static int rtpdebugport;
00130 static int rtcpdebugport;
00131 #ifdef SO_NO_CHECK
00132 static int nochecksums;
00133 #endif
00134 static int strictrtp = DEFAULT_STRICT_RTP;
00135 static int learning_min_sequential = DEFAULT_LEARNING_MIN_SEQUENTIAL;
00136 static int icesupport = DEFAULT_ICESUPPORT;
00137 static struct sockaddr_in stunaddr;
00138 static pj_str_t turnaddr;
00139 static int turnport = DEFAULT_TURN_PORT;
00140 static pj_str_t turnusername;
00141 static pj_str_t turnpassword;
00142
00143
00144 static pj_caching_pool cachingpool;
00145
00146
00147 static pj_pool_t *pool;
00148
00149
00150 static pj_ioqueue_t *ioqueue;
00151
00152
00153 static pj_timer_heap_t *timerheap;
00154
00155
00156 static pj_thread_t *thread;
00157
00158
00159 static int worker_terminate;
00160
00161 #define FLAG_3389_WARNING (1 << 0)
00162 #define FLAG_NAT_ACTIVE (3 << 1)
00163 #define FLAG_NAT_INACTIVE (0 << 1)
00164 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
00165 #define FLAG_NEED_MARKER_BIT (1 << 3)
00166 #define FLAG_DTMF_COMPENSATE (1 << 4)
00167
00168 #define TRANSPORT_SOCKET_RTP 1
00169 #define TRANSPORT_SOCKET_RTCP 2
00170 #define TRANSPORT_TURN_RTP 3
00171 #define TRANSPORT_TURN_RTCP 4
00172
00173 #define COMPONENT_RTP 1
00174 #define COMPONENT_RTCP 2
00175
00176
00177 struct ast_rtp {
00178 int s;
00179 struct ast_frame f;
00180 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
00181 unsigned int ssrc;
00182 unsigned int themssrc;
00183 unsigned int rxssrc;
00184 unsigned int lastts;
00185 unsigned int lastrxts;
00186 unsigned int lastividtimestamp;
00187 unsigned int lastovidtimestamp;
00188 unsigned int lastitexttimestamp;
00189 unsigned int lastotexttimestamp;
00190 unsigned int lasteventseqn;
00191 int lastrxseqno;
00192 unsigned short seedrxseqno;
00193 unsigned int seedrxts;
00194 unsigned int rxcount;
00195 unsigned int rxoctetcount;
00196 unsigned int txcount;
00197 unsigned int txoctetcount;
00198 unsigned int cycles;
00199 double rxjitter;
00200 double rxtransit;
00201 struct ast_format lasttxformat;
00202 struct ast_format lastrxformat;
00203
00204 int rtptimeout;
00205 int rtpholdtimeout;
00206 int rtpkeepalive;
00207
00208
00209 char resp;
00210 unsigned int last_seqno;
00211 unsigned int last_end_timestamp;
00212 unsigned int dtmf_duration;
00213 unsigned int dtmf_timeout;
00214 unsigned int dtmfsamples;
00215 enum ast_rtp_dtmf_mode dtmfmode;
00216
00217 unsigned int lastdigitts;
00218 char sending_digit;
00219 char send_digit;
00220 int send_payload;
00221 int send_duration;
00222 unsigned int flags;
00223 struct timeval rxcore;
00224 struct timeval txcore;
00225 double drxcore;
00226 struct timeval lastrx;
00227 struct timeval dtmfmute;
00228 struct ast_smoother *smoother;
00229 int *ioid;
00230 unsigned short seqno;
00231 unsigned short rxseqno;
00232 struct ast_sched_context *sched;
00233 struct io_context *io;
00234 void *data;
00235 struct ast_rtcp *rtcp;
00236 struct ast_rtp *bridged;
00237
00238 enum strict_rtp_state strict_rtp_state;
00239 struct ast_sockaddr strict_rtp_address;
00240 struct ast_sockaddr alt_rtp_address;
00241
00242
00243
00244
00245
00246 uint16_t learning_max_seq;
00247 int learning_probation;
00248
00249 struct rtp_red *red;
00250
00251 pj_ice_sess *ice;
00252 pj_turn_sock *turn_rtp;
00253 pj_turn_sock *turn_rtcp;
00254 ast_mutex_t lock;
00255 pj_turn_state_t turn_state;
00256 ast_cond_t cond;
00257 unsigned int passthrough:1;
00258 unsigned int ice_started:1;
00259
00260 char remote_ufrag[256];
00261 char remote_passwd[256];
00262
00263 char local_ufrag[256];
00264 char local_passwd[256];
00265
00266 struct ao2_container *local_candidates;
00267 struct ao2_container *remote_candidates;
00268
00269 #ifdef HAVE_OPENSSL_SRTP
00270 SSL_CTX *ssl_ctx;
00271 SSL *ssl;
00272 BIO *read_bio;
00273 BIO *write_bio;
00274 enum ast_rtp_dtls_setup dtls_setup;
00275 enum ast_srtp_suite suite;
00276 char local_fingerprint[160];
00277 unsigned char remote_fingerprint[EVP_MAX_MD_SIZE];
00278 enum ast_rtp_dtls_connection connection;
00279 unsigned int dtls_failure:1;
00280 unsigned int rekey;
00281 int rekeyid;
00282 #endif
00283 };
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 struct ast_rtcp {
00296 int rtcp_info;
00297 int s;
00298 struct ast_sockaddr us;
00299 struct ast_sockaddr them;
00300 unsigned int soc;
00301 unsigned int spc;
00302 unsigned int themrxlsr;
00303 struct timeval rxlsr;
00304 struct timeval txlsr;
00305 unsigned int expected_prior;
00306 unsigned int received_prior;
00307 int schedid;
00308 unsigned int rr_count;
00309 unsigned int sr_count;
00310 unsigned int lastsrtxcount;
00311 double accumulated_transit;
00312 double rtt;
00313 unsigned int reported_jitter;
00314 unsigned int reported_lost;
00315
00316 double reported_maxjitter;
00317 double reported_minjitter;
00318 double reported_normdev_jitter;
00319 double reported_stdev_jitter;
00320 unsigned int reported_jitter_count;
00321
00322 double reported_maxlost;
00323 double reported_minlost;
00324 double reported_normdev_lost;
00325 double reported_stdev_lost;
00326
00327 double rxlost;
00328 double maxrxlost;
00329 double minrxlost;
00330 double normdev_rxlost;
00331 double stdev_rxlost;
00332 unsigned int rxlost_count;
00333
00334 double maxrxjitter;
00335 double minrxjitter;
00336 double normdev_rxjitter;
00337 double stdev_rxjitter;
00338 unsigned int rxjitter_count;
00339 double maxrtt;
00340 double minrtt;
00341 double normdevrtt;
00342 double stdevrtt;
00343 unsigned int rtt_count;
00344 };
00345
00346 struct rtp_red {
00347 struct ast_frame t140;
00348 struct ast_frame t140red;
00349 unsigned char pt[AST_RED_MAX_GENERATION];
00350 unsigned char ts[AST_RED_MAX_GENERATION];
00351 unsigned char len[AST_RED_MAX_GENERATION];
00352 int num_gen;
00353 int schedid;
00354 int ti;
00355 unsigned char t140red_data[64000];
00356 unsigned char buf_data[64000];
00357 int hdrlen;
00358 long int prev_ts;
00359 };
00360
00361 AST_LIST_HEAD_NOLOCK(frame_list, ast_frame);
00362
00363
00364 static int ast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data);
00365 static int ast_rtp_destroy(struct ast_rtp_instance *instance);
00366 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit);
00367 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit);
00368 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration);
00369 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode);
00370 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance);
00371 static void ast_rtp_update_source(struct ast_rtp_instance *instance);
00372 static void ast_rtp_change_source(struct ast_rtp_instance *instance);
00373 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame);
00374 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp);
00375 static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value);
00376 static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp);
00377 static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
00378 static void ast_rtp_alt_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
00379 static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations);
00380 static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame);
00381 static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1);
00382 static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat);
00383 static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1);
00384 static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username);
00385 static void ast_rtp_stop(struct ast_rtp_instance *instance);
00386 static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char* desc);
00387 static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level);
00388
00389 #ifdef HAVE_OPENSSL_SRTP
00390 static int ast_rtp_activate(struct ast_rtp_instance *instance);
00391 #endif
00392
00393 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);
00394
00395
00396 static void ast_rtp_ice_candidate_destroy(void *obj)
00397 {
00398 struct ast_rtp_engine_ice_candidate *candidate = obj;
00399
00400 if (candidate->foundation) {
00401 ast_free(candidate->foundation);
00402 }
00403
00404 if (candidate->transport) {
00405 ast_free(candidate->transport);
00406 }
00407 }
00408
00409 static void ast_rtp_ice_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
00410 {
00411 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00412
00413 if (!ast_strlen_zero(ufrag)) {
00414 ast_copy_string(rtp->remote_ufrag, ufrag, sizeof(rtp->remote_ufrag));
00415 }
00416
00417 if (!ast_strlen_zero(password)) {
00418 ast_copy_string(rtp->remote_passwd, password, sizeof(rtp->remote_passwd));
00419 }
00420 }
00421
00422 static void ast_rtp_ice_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
00423 {
00424 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00425 struct ast_rtp_engine_ice_candidate *remote_candidate;
00426
00427 if (!rtp->remote_candidates && !(rtp->remote_candidates = ao2_container_alloc(1, NULL, NULL))) {
00428 return;
00429 }
00430
00431
00432 if (ao2_container_count(rtp->remote_candidates) == PJ_ICE_MAX_CAND) {
00433 return;
00434 }
00435
00436 if (!(remote_candidate = ao2_alloc(sizeof(*remote_candidate), ast_rtp_ice_candidate_destroy))) {
00437 return;
00438 }
00439
00440 remote_candidate->foundation = ast_strdup(candidate->foundation);
00441 remote_candidate->id = candidate->id;
00442 remote_candidate->transport = ast_strdup(candidate->transport);
00443 remote_candidate->priority = candidate->priority;
00444 ast_sockaddr_copy(&remote_candidate->address, &candidate->address);
00445 ast_sockaddr_copy(&remote_candidate->relay_address, &candidate->relay_address);
00446 remote_candidate->type = candidate->type;
00447
00448 ao2_link(rtp->remote_candidates, remote_candidate);
00449 ao2_ref(remote_candidate, -1);
00450 }
00451
00452 AST_THREADSTORAGE(pj_thread_storage);
00453
00454
00455 static void pj_thread_register_check(void)
00456 {
00457 pj_thread_desc *desc;
00458 pj_thread_t *thread;
00459
00460 if (pj_thread_is_registered() == PJ_TRUE) {
00461 return;
00462 }
00463
00464 desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc));
00465 if (!desc) {
00466 ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage. Expect awful things to occur\n");
00467 return;
00468 }
00469 pj_bzero(*desc, sizeof(*desc));
00470
00471 if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
00472 ast_log(LOG_ERROR, "Coudln't register thread with PJLIB.\n");
00473 }
00474 return;
00475 }
00476
00477
00478 static void update_address_with_ice_candidate(struct ast_rtp *rtp, int component, struct ast_sockaddr *cand_address)
00479 {
00480 char address[PJ_INET6_ADDRSTRLEN];
00481
00482 if (!rtp->ice || (component < 1) || !rtp->ice->comp[component - 1].valid_check) {
00483 return;
00484 }
00485
00486 ast_sockaddr_parse(cand_address, pj_sockaddr_print(&rtp->ice->comp[component - 1].valid_check->rcand->addr, address, sizeof(address), 0), 0);
00487 ast_sockaddr_set_port(cand_address, pj_sockaddr_get_port(&rtp->ice->comp[component - 1].valid_check->rcand->addr));
00488 }
00489
00490 static void ast_rtp_ice_start(struct ast_rtp_instance *instance)
00491 {
00492 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00493 pj_str_t ufrag = pj_str(rtp->remote_ufrag), passwd = pj_str(rtp->remote_passwd);
00494 pj_ice_sess_cand candidates[PJ_ICE_MAX_CAND];
00495 struct ao2_iterator i;
00496 struct ast_rtp_engine_ice_candidate *candidate;
00497 int cand_cnt = 0;
00498
00499 if (!rtp->ice || !rtp->remote_candidates || rtp->ice_started) {
00500 return;
00501 }
00502
00503 pj_thread_register_check();
00504
00505 i = ao2_iterator_init(rtp->remote_candidates, 0);
00506
00507 while ((candidate = ao2_iterator_next(&i)) && (cand_cnt < PJ_ICE_MAX_CAND)) {
00508 pj_str_t address;
00509
00510 pj_strdup2(rtp->ice->pool, &candidates[cand_cnt].foundation, candidate->foundation);
00511 candidates[cand_cnt].comp_id = candidate->id;
00512 candidates[cand_cnt].prio = candidate->priority;
00513
00514 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->address)), &candidates[cand_cnt].addr);
00515
00516 if (!ast_sockaddr_isnull(&candidate->relay_address)) {
00517 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->relay_address)), &candidates[cand_cnt].rel_addr);
00518 }
00519
00520 if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
00521 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_HOST;
00522 } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
00523 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_SRFLX;
00524 } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_RELAYED) {
00525 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_RELAYED;
00526 }
00527
00528 if (candidate->id == COMPONENT_RTP && rtp->turn_rtp) {
00529 pj_turn_sock_set_perm(rtp->turn_rtp, 1, &candidates[cand_cnt].addr, 1);
00530 } else if (candidate->id == COMPONENT_RTCP && rtp->turn_rtcp) {
00531 pj_turn_sock_set_perm(rtp->turn_rtcp, 1, &candidates[cand_cnt].addr, 1);
00532 }
00533
00534 cand_cnt++;
00535 }
00536
00537 ao2_iterator_destroy(&i);
00538
00539 if (pj_ice_sess_create_check_list(rtp->ice, &ufrag, &passwd, ao2_container_count(rtp->remote_candidates), &candidates[0]) == PJ_SUCCESS) {
00540 pj_ice_sess_start_check(rtp->ice);
00541 pj_timer_heap_poll(timerheap, NULL);
00542 rtp->ice_started = 1;
00543 }
00544 }
00545
00546 static void ast_rtp_ice_stop(struct ast_rtp_instance *instance)
00547 {
00548 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00549
00550 if (!rtp->ice) {
00551 return;
00552 }
00553
00554 pj_thread_register_check();
00555
00556 pj_ice_sess_destroy(rtp->ice);
00557 rtp->ice = NULL;
00558 }
00559
00560 static const char *ast_rtp_ice_get_ufrag(struct ast_rtp_instance *instance)
00561 {
00562 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00563
00564 return rtp->local_ufrag;
00565 }
00566
00567 static const char *ast_rtp_ice_get_password(struct ast_rtp_instance *instance)
00568 {
00569 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00570
00571 return rtp->local_passwd;
00572 }
00573
00574 static struct ao2_container *ast_rtp_ice_get_local_candidates(struct ast_rtp_instance *instance)
00575 {
00576 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00577
00578 if (rtp->local_candidates) {
00579 ao2_ref(rtp->local_candidates, +1);
00580 }
00581
00582 return rtp->local_candidates;
00583 }
00584
00585 static void ast_rtp_ice_lite(struct ast_rtp_instance *instance)
00586 {
00587 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00588
00589 if (!rtp->ice) {
00590 return;
00591 }
00592
00593 pj_thread_register_check();
00594
00595 pj_ice_sess_change_role(rtp->ice, PJ_ICE_SESS_ROLE_CONTROLLING);
00596 }
00597
00598 static int ice_candidate_cmp(void *obj, void *arg, int flags)
00599 {
00600 struct ast_rtp_engine_ice_candidate *candidate1 = obj, *candidate2 = arg;
00601
00602 if ((strcmp(candidate1->foundation, candidate2->foundation)) ||
00603 (candidate1->id != candidate2->id) ||
00604 (ast_sockaddr_cmp(&candidate1->address, &candidate2->address)) ||
00605 (candidate1->type != candidate1->type)) {
00606 return 0;
00607 }
00608
00609 return CMP_MATCH | CMP_STOP;
00610 }
00611
00612 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,
00613 const pj_sockaddr_t *addr, const pj_sockaddr_t *base_addr, const pj_sockaddr_t *rel_addr, int addr_len)
00614 {
00615 pj_str_t foundation;
00616 struct ast_rtp_engine_ice_candidate *candidate, *existing;
00617 char address[PJ_INET6_ADDRSTRLEN];
00618
00619 pj_thread_register_check();
00620
00621 pj_ice_calc_foundation(rtp->ice->pool, &foundation, type, addr);
00622
00623 if (!rtp->local_candidates && !(rtp->local_candidates = ao2_container_alloc(1, NULL, ice_candidate_cmp))) {
00624 return;
00625 }
00626
00627 if (!(candidate = ao2_alloc(sizeof(*candidate), ast_rtp_ice_candidate_destroy))) {
00628 return;
00629 }
00630
00631 candidate->foundation = ast_strndup(pj_strbuf(&foundation), pj_strlen(&foundation));
00632 candidate->id = comp_id;
00633 candidate->transport = ast_strdup("UDP");
00634
00635 ast_sockaddr_parse(&candidate->address, pj_sockaddr_print(addr, address, sizeof(address), 0), 0);
00636 ast_sockaddr_set_port(&candidate->address, pj_sockaddr_get_port(addr));
00637
00638 if (rel_addr) {
00639 ast_sockaddr_parse(&candidate->relay_address, pj_sockaddr_print(rel_addr, address, sizeof(address), 0), 0);
00640 ast_sockaddr_set_port(&candidate->relay_address, pj_sockaddr_get_port(rel_addr));
00641 }
00642
00643 if (type == PJ_ICE_CAND_TYPE_HOST) {
00644 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
00645 } else if (type == PJ_ICE_CAND_TYPE_SRFLX) {
00646 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
00647 } else if (type == PJ_ICE_CAND_TYPE_RELAYED) {
00648 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
00649 }
00650
00651 if ((existing = ao2_find(rtp->local_candidates, candidate, OBJ_POINTER))) {
00652 ao2_ref(existing, -1);
00653 ao2_ref(candidate, -1);
00654 return;
00655 }
00656
00657 if (pj_ice_sess_add_cand(rtp->ice, comp_id, transport_id, type, local_pref, &foundation, addr, addr, rel_addr, addr_len, NULL) != PJ_SUCCESS) {
00658 ao2_ref(candidate, -1);
00659 return;
00660 }
00661
00662
00663 candidate->priority = rtp->ice->lcand[rtp->ice->lcand_cnt - 1].prio;
00664
00665 ao2_link(rtp->local_candidates, candidate);
00666 ao2_ref(candidate, -1);
00667 }
00668
00669 static char *generate_random_string(char *buf, size_t size)
00670 {
00671 long val[4];
00672 int x;
00673
00674 for (x=0; x<4; x++)
00675 val[x] = ast_random();
00676 snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
00677
00678 return buf;
00679 }
00680
00681
00682 static struct ast_rtp_engine_ice ast_rtp_ice = {
00683 .set_authentication = ast_rtp_ice_set_authentication,
00684 .add_remote_candidate = ast_rtp_ice_add_remote_candidate,
00685 .start = ast_rtp_ice_start,
00686 .stop = ast_rtp_ice_stop,
00687 .get_ufrag = ast_rtp_ice_get_ufrag,
00688 .get_password = ast_rtp_ice_get_password,
00689 .get_local_candidates = ast_rtp_ice_get_local_candidates,
00690 .ice_lite = ast_rtp_ice_lite,
00691 };
00692
00693 #ifdef HAVE_OPENSSL_SRTP
00694 static void dtls_info_callback(const SSL *ssl, int where, int ret)
00695 {
00696 struct ast_rtp *rtp = SSL_get_ex_data(ssl, 0);
00697
00698
00699 if (!(where & SSL_CB_ALERT)) {
00700 return;
00701 }
00702
00703 rtp->dtls_failure = 1;
00704 }
00705
00706 static int ast_rtp_dtls_set_configuration(struct ast_rtp_instance *instance, const struct ast_rtp_dtls_cfg *dtls_cfg)
00707 {
00708 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00709
00710 if (!dtls_cfg->enabled) {
00711 return 0;
00712 }
00713
00714 if (!ast_rtp_engine_srtp_is_registered()) {
00715 return -1;
00716 }
00717
00718 if (!(rtp->ssl_ctx = SSL_CTX_new(DTLSv1_method()))) {
00719 return -1;
00720 }
00721
00722 SSL_CTX_set_verify(rtp->ssl_ctx, dtls_cfg->verify ? SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT : SSL_VERIFY_NONE, NULL);
00723
00724 if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_80) {
00725 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_80");
00726 } else if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_32) {
00727 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_32");
00728 } else {
00729 ast_log(LOG_ERROR, "Unsupported suite specified for DTLS-SRTP on RTP instance '%p'\n", instance);
00730 goto error;
00731 }
00732
00733 if (!ast_strlen_zero(dtls_cfg->certfile)) {
00734 char *private = ast_strlen_zero(dtls_cfg->pvtfile) ? dtls_cfg->certfile : dtls_cfg->pvtfile;
00735 BIO *certbio;
00736 X509 *cert;
00737 unsigned int size, i;
00738 unsigned char fingerprint[EVP_MAX_MD_SIZE];
00739 char *local_fingerprint = rtp->local_fingerprint;
00740
00741 if (!SSL_CTX_use_certificate_file(rtp->ssl_ctx, dtls_cfg->certfile, SSL_FILETYPE_PEM)) {
00742 ast_log(LOG_ERROR, "Specified certificate file '%s' for RTP instance '%p' could not be used\n",
00743 dtls_cfg->certfile, instance);
00744 goto error;
00745 }
00746
00747 if (!SSL_CTX_use_PrivateKey_file(rtp->ssl_ctx, private, SSL_FILETYPE_PEM) ||
00748 !SSL_CTX_check_private_key(rtp->ssl_ctx)) {
00749 ast_log(LOG_ERROR, "Specified private key file '%s' for RTP instance '%p' could not be used\n",
00750 private, instance);
00751 goto error;
00752 }
00753
00754 if (!(certbio = BIO_new(BIO_s_file()))) {
00755 ast_log(LOG_ERROR, "Failed to allocate memory for certificate fingerprinting on RTP instance '%p'\n",
00756 instance);
00757 goto error;
00758 }
00759
00760 if (!BIO_read_filename(certbio, dtls_cfg->certfile) ||
00761 !(cert = PEM_read_bio_X509(certbio, NULL, 0, NULL)) ||
00762 !X509_digest(cert, EVP_sha1(), fingerprint, &size) ||
00763 !size) {
00764 ast_log(LOG_ERROR, "Could not produce fingerprint from certificate '%s' for RTP instance '%p'\n",
00765 dtls_cfg->certfile, instance);
00766 BIO_free_all(certbio);
00767 goto error;
00768 }
00769
00770 for (i = 0; i < size; i++) {
00771 sprintf(local_fingerprint, "%.2X:", fingerprint[i]);
00772 local_fingerprint += 3;
00773 }
00774
00775 *(local_fingerprint-1) = 0;
00776
00777 BIO_free_all(certbio);
00778 }
00779
00780 if (!ast_strlen_zero(dtls_cfg->cipher)) {
00781 if (!SSL_CTX_set_cipher_list(rtp->ssl_ctx, dtls_cfg->cipher)) {
00782 ast_log(LOG_ERROR, "Invalid cipher specified in cipher list '%s' for RTP instance '%p'\n",
00783 dtls_cfg->cipher, instance);
00784 goto error;
00785 }
00786 }
00787
00788 if (!ast_strlen_zero(dtls_cfg->cafile) || !ast_strlen_zero(dtls_cfg->capath)) {
00789 if (!SSL_CTX_load_verify_locations(rtp->ssl_ctx, S_OR(dtls_cfg->cafile, NULL), S_OR(dtls_cfg->capath, NULL))) {
00790 ast_log(LOG_ERROR, "Invalid certificate authority file '%s' or path '%s' specified for RTP instance '%p'\n",
00791 S_OR(dtls_cfg->cafile, ""), S_OR(dtls_cfg->capath, ""), instance);
00792 goto error;
00793 }
00794 }
00795
00796 rtp->rekey = dtls_cfg->rekey;
00797 rtp->dtls_setup = dtls_cfg->default_setup;
00798 rtp->suite = dtls_cfg->suite;
00799
00800 if (!(rtp->ssl = SSL_new(rtp->ssl_ctx))) {
00801 ast_log(LOG_ERROR, "Failed to allocate memory for SSL context on RTP instance '%p'\n",
00802 instance);
00803 goto error;
00804 }
00805
00806 SSL_set_ex_data(rtp->ssl, 0, rtp);
00807 SSL_set_info_callback(rtp->ssl, dtls_info_callback);
00808
00809 if (!(rtp->read_bio = BIO_new(BIO_s_mem()))) {
00810 ast_log(LOG_ERROR, "Failed to allocate memory for inbound SSL traffic on RTP instance '%p'\n",
00811 instance);
00812 goto error;
00813 }
00814 BIO_set_mem_eof_return(rtp->read_bio, -1);
00815
00816 if (!(rtp->write_bio = BIO_new(BIO_s_mem()))) {
00817 ast_log(LOG_ERROR, "Failed to allocate memory for outbound SSL traffic on RTP instance '%p'\n",
00818 instance);
00819 goto error;
00820 }
00821 BIO_set_mem_eof_return(rtp->write_bio, -1);
00822
00823 SSL_set_bio(rtp->ssl, rtp->read_bio, rtp->write_bio);
00824
00825 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
00826 SSL_set_accept_state(rtp->ssl);
00827 } else {
00828 SSL_set_connect_state(rtp->ssl);
00829 }
00830
00831 rtp->connection = AST_RTP_DTLS_CONNECTION_NEW;
00832
00833 return 0;
00834
00835 error:
00836 if (rtp->read_bio) {
00837 BIO_free(rtp->read_bio);
00838 rtp->read_bio = NULL;
00839 }
00840
00841 if (rtp->write_bio) {
00842 BIO_free(rtp->write_bio);
00843 rtp->write_bio = NULL;
00844 }
00845
00846 if (rtp->ssl) {
00847 SSL_free(rtp->ssl);
00848 rtp->ssl = NULL;
00849 }
00850
00851 SSL_CTX_free(rtp->ssl_ctx);
00852 rtp->ssl_ctx = NULL;
00853
00854 return -1;
00855 }
00856
00857 static int ast_rtp_dtls_active(struct ast_rtp_instance *instance)
00858 {
00859 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00860
00861 return !rtp->ssl_ctx ? 0 : 1;
00862 }
00863
00864 static void ast_rtp_dtls_stop(struct ast_rtp_instance *instance)
00865 {
00866 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00867
00868 if (rtp->ssl_ctx) {
00869 SSL_CTX_free(rtp->ssl_ctx);
00870 rtp->ssl_ctx = NULL;
00871 }
00872
00873 if (rtp->ssl) {
00874 SSL_free(rtp->ssl);
00875 rtp->ssl = NULL;
00876 }
00877 }
00878
00879 static void ast_rtp_dtls_reset(struct ast_rtp_instance *instance)
00880 {
00881 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00882
00883
00884 if (!SSL_is_init_finished(rtp->ssl)) {
00885 return;
00886 }
00887
00888 SSL_shutdown(rtp->ssl);
00889 rtp->connection = AST_RTP_DTLS_CONNECTION_NEW;
00890 }
00891
00892 static enum ast_rtp_dtls_connection ast_rtp_dtls_get_connection(struct ast_rtp_instance *instance)
00893 {
00894 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00895
00896 return rtp->connection;
00897 }
00898
00899 static enum ast_rtp_dtls_setup ast_rtp_dtls_get_setup(struct ast_rtp_instance *instance)
00900 {
00901 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00902
00903 return rtp->dtls_setup;
00904 }
00905
00906 static void ast_rtp_dtls_set_setup(struct ast_rtp_instance *instance, enum ast_rtp_dtls_setup setup)
00907 {
00908 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00909 enum ast_rtp_dtls_setup old = rtp->dtls_setup;
00910
00911 switch (setup) {
00912 case AST_RTP_DTLS_SETUP_ACTIVE:
00913 rtp->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
00914 break;
00915 case AST_RTP_DTLS_SETUP_PASSIVE:
00916 rtp->dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
00917 break;
00918 case AST_RTP_DTLS_SETUP_ACTPASS:
00919
00920 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
00921 rtp->dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
00922 }
00923 break;
00924 case AST_RTP_DTLS_SETUP_HOLDCONN:
00925 rtp->dtls_setup = AST_RTP_DTLS_SETUP_HOLDCONN;
00926 break;
00927 default:
00928
00929 return;
00930 }
00931
00932
00933 if (old == rtp->dtls_setup) {
00934 return;
00935 }
00936
00937
00938 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_HOLDCONN) {
00939 return;
00940 }
00941
00942 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
00943 SSL_set_connect_state(rtp->ssl);
00944 } else if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
00945 SSL_set_accept_state(rtp->ssl);
00946 } else {
00947 return;
00948 }
00949 }
00950
00951 static void ast_rtp_dtls_set_fingerprint(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash, const char *fingerprint)
00952 {
00953 char *tmp = ast_strdupa(fingerprint), *value;
00954 int pos = 0;
00955 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00956
00957 if (hash != AST_RTP_DTLS_HASH_SHA1) {
00958 return;
00959 }
00960
00961 while ((value = strsep(&tmp, ":")) && (pos != (EVP_MAX_MD_SIZE - 1))) {
00962 sscanf(value, "%02x", (unsigned int*)&rtp->remote_fingerprint[pos++]);
00963 }
00964 }
00965
00966 static const char *ast_rtp_dtls_get_fingerprint(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash)
00967 {
00968 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00969
00970 if (hash != AST_RTP_DTLS_HASH_SHA1) {
00971 return NULL;
00972 }
00973
00974 return rtp->local_fingerprint;
00975 }
00976
00977
00978 static struct ast_rtp_engine_dtls ast_rtp_dtls = {
00979 .set_configuration = ast_rtp_dtls_set_configuration,
00980 .active = ast_rtp_dtls_active,
00981 .stop = ast_rtp_dtls_stop,
00982 .reset = ast_rtp_dtls_reset,
00983 .get_connection = ast_rtp_dtls_get_connection,
00984 .get_setup = ast_rtp_dtls_get_setup,
00985 .set_setup = ast_rtp_dtls_set_setup,
00986 .set_fingerprint = ast_rtp_dtls_set_fingerprint,
00987 .get_fingerprint = ast_rtp_dtls_get_fingerprint,
00988 };
00989
00990 #endif
00991
00992
00993 static struct ast_rtp_engine asterisk_rtp_engine = {
00994 .name = "asterisk",
00995 .new = ast_rtp_new,
00996 .destroy = ast_rtp_destroy,
00997 .dtmf_begin = ast_rtp_dtmf_begin,
00998 .dtmf_end = ast_rtp_dtmf_end,
00999 .dtmf_end_with_duration = ast_rtp_dtmf_end_with_duration,
01000 .dtmf_mode_set = ast_rtp_dtmf_mode_set,
01001 .dtmf_mode_get = ast_rtp_dtmf_mode_get,
01002 .update_source = ast_rtp_update_source,
01003 .change_source = ast_rtp_change_source,
01004 .write = ast_rtp_write,
01005 .read = ast_rtp_read,
01006 .prop_set = ast_rtp_prop_set,
01007 .fd = ast_rtp_fd,
01008 .remote_address_set = ast_rtp_remote_address_set,
01009 .alt_remote_address_set = ast_rtp_alt_remote_address_set,
01010 .red_init = rtp_red_init,
01011 .red_buffer = rtp_red_buffer,
01012 .local_bridge = ast_rtp_local_bridge,
01013 .get_stat = ast_rtp_get_stat,
01014 .dtmf_compatible = ast_rtp_dtmf_compatible,
01015 .stun_request = ast_rtp_stun_request,
01016 .stop = ast_rtp_stop,
01017 .qos = ast_rtp_qos_set,
01018 .sendcng = ast_rtp_sendcng,
01019 .ice = &ast_rtp_ice,
01020 #ifdef HAVE_OPENSSL_SRTP
01021 .dtls = &ast_rtp_dtls,
01022 .activate = ast_rtp_activate,
01023 #endif
01024 };
01025
01026 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)
01027 {
01028 struct ast_rtp *rtp = ice->user_data;
01029
01030
01031
01032 rtp->passthrough = 1;
01033 }
01034
01035 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)
01036 {
01037 struct ast_rtp *rtp = ice->user_data;
01038 pj_status_t status = PJ_EINVALIDOP;
01039 pj_ssize_t _size = (pj_ssize_t)size;
01040
01041 if (transport_id == TRANSPORT_SOCKET_RTP) {
01042
01043 status = pj_sock_sendto(rtp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
01044
01045 ast_assert(_size == size || status != PJ_SUCCESS);
01046 } else if (transport_id == TRANSPORT_SOCKET_RTCP) {
01047
01048 if (rtp->rtcp) {
01049 status = pj_sock_sendto(rtp->rtcp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
01050
01051 ast_assert(_size == size || status != PJ_SUCCESS);
01052 } else {
01053 status = PJ_SUCCESS;
01054 }
01055 } else if (transport_id == TRANSPORT_TURN_RTP) {
01056
01057 if (rtp->turn_rtp) {
01058 status = pj_turn_sock_sendto(rtp->turn_rtp, pkt, size, dst_addr, dst_addr_len);
01059 }
01060 } else if (transport_id == TRANSPORT_TURN_RTCP) {
01061
01062 if (rtp->turn_rtcp) {
01063 status = pj_turn_sock_sendto(rtp->turn_rtcp, pkt, size, dst_addr, dst_addr_len);
01064 }
01065 }
01066
01067 return status;
01068 }
01069
01070
01071 static pj_ice_sess_cb ast_rtp_ice_sess_cb = {
01072 .on_rx_data = ast_rtp_on_ice_rx_data,
01073 .on_tx_pkt = ast_rtp_on_ice_tx_pkt,
01074 };
01075
01076 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)
01077 {
01078 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
01079 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01080 struct ast_sockaddr dest = { { 0, }, };
01081
01082 ast_rtp_instance_get_local_address(instance, &dest);
01083
01084 ast_sendto(rtp->s, pkt, pkt_len, 0, &dest);
01085 }
01086
01087 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)
01088 {
01089 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
01090 struct ast_rtp *rtp = NULL;
01091
01092
01093 if (!instance) {
01094 return;
01095 }
01096
01097 rtp = ast_rtp_instance_get_data(instance);
01098
01099
01100 if (new_state == PJ_TURN_STATE_DESTROYING) {
01101 rtp->turn_rtp = NULL;
01102 return;
01103 }
01104
01105
01106 ast_mutex_lock(&rtp->lock);
01107 rtp->turn_state = new_state;
01108
01109
01110 if (new_state == PJ_TURN_STATE_READY || new_state == PJ_TURN_STATE_DEALLOCATING || new_state == PJ_TURN_STATE_DEALLOCATED) {
01111 ast_cond_signal(&rtp->cond);
01112 }
01113
01114 ast_mutex_unlock(&rtp->lock);
01115 }
01116
01117
01118 static pj_turn_sock_cb ast_rtp_turn_rtp_sock_cb = {
01119 .on_rx_data = ast_rtp_on_turn_rx_rtp_data,
01120 .on_state = ast_rtp_on_turn_rtp_state,
01121 };
01122
01123 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)
01124 {
01125 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
01126 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01127
01128 ast_sendto(rtp->rtcp->s, pkt, pkt_len, 0, &rtp->rtcp->us);
01129 }
01130
01131 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)
01132 {
01133 struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
01134 struct ast_rtp *rtp = NULL;
01135
01136
01137 if (!instance) {
01138 return;
01139 }
01140
01141 rtp = ast_rtp_instance_get_data(instance);
01142
01143
01144 if (new_state == PJ_TURN_STATE_DESTROYING) {
01145 rtp->turn_rtcp = NULL;
01146 return;
01147 }
01148
01149
01150 ast_mutex_lock(&rtp->lock);
01151 rtp->turn_state = new_state;
01152
01153
01154 if (new_state == PJ_TURN_STATE_READY || new_state == PJ_TURN_STATE_DEALLOCATING || new_state == PJ_TURN_STATE_DEALLOCATED) {
01155 ast_cond_signal(&rtp->cond);
01156 }
01157
01158 ast_mutex_unlock(&rtp->lock);
01159 }
01160
01161
01162 static pj_turn_sock_cb ast_rtp_turn_rtcp_sock_cb = {
01163 .on_rx_data = ast_rtp_on_turn_rx_rtcp_data,
01164 .on_state = ast_rtp_on_turn_rtcp_state,
01165 };
01166
01167
01168 static int ice_worker_thread(void *data)
01169 {
01170 while (!worker_terminate) {
01171 const pj_time_val delay = {0, 10};
01172
01173 pj_ioqueue_poll(ioqueue, &delay);
01174
01175 pj_timer_heap_poll(timerheap, NULL);
01176 }
01177
01178 return 0;
01179 }
01180
01181 static inline int rtp_debug_test_addr(struct ast_sockaddr *addr)
01182 {
01183 if (!rtpdebug) {
01184 return 0;
01185 }
01186 if (!ast_sockaddr_isnull(&rtpdebugaddr)) {
01187 if (rtpdebugport) {
01188 return (ast_sockaddr_cmp(&rtpdebugaddr, addr) == 0);
01189 } else {
01190 return (ast_sockaddr_cmp_addr(&rtpdebugaddr, addr) == 0);
01191 }
01192 }
01193
01194 return 1;
01195 }
01196
01197 static inline int rtcp_debug_test_addr(struct ast_sockaddr *addr)
01198 {
01199 if (!rtcpdebug) {
01200 return 0;
01201 }
01202 if (!ast_sockaddr_isnull(&rtcpdebugaddr)) {
01203 if (rtcpdebugport) {
01204 return (ast_sockaddr_cmp(&rtcpdebugaddr, addr) == 0);
01205 } else {
01206 return (ast_sockaddr_cmp_addr(&rtcpdebugaddr, addr) == 0);
01207 }
01208 }
01209
01210 return 1;
01211 }
01212
01213 #ifdef HAVE_OPENSSL_SRTP
01214 static void dtls_srtp_check_pending(struct ast_rtp_instance *instance, struct ast_rtp *rtp)
01215 {
01216 size_t pending = BIO_ctrl_pending(rtp->write_bio);
01217
01218 if (pending > 0) {
01219 char outgoing[pending];
01220 size_t out;
01221 struct ast_sockaddr remote_address = { {0, } };
01222 int ice;
01223
01224 ast_rtp_instance_get_remote_address(instance, &remote_address);
01225
01226
01227 if (ast_sockaddr_isnull(&remote_address)) {
01228 return;
01229 }
01230
01231 out = BIO_read(rtp->write_bio, outgoing, sizeof(outgoing));
01232
01233 __rtp_sendto(instance, outgoing, out, 0, &remote_address, 0, &ice, 0);
01234 }
01235 }
01236
01237 static int dtls_srtp_renegotiate(const void *data)
01238 {
01239 struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
01240 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01241
01242 SSL_renegotiate(rtp->ssl);
01243 SSL_do_handshake(rtp->ssl);
01244 dtls_srtp_check_pending(instance, rtp);
01245
01246 rtp->rekeyid = -1;
01247 ao2_ref(instance, -1);
01248
01249 return 0;
01250 }
01251
01252 static int dtls_srtp_setup(struct ast_rtp *rtp, struct ast_srtp *srtp, struct ast_rtp_instance *instance)
01253 {
01254 unsigned char material[SRTP_MASTER_LEN * 2];
01255 unsigned char *local_key, *local_salt, *remote_key, *remote_salt;
01256 struct ast_srtp_policy *local_policy, *remote_policy = NULL;
01257 struct ast_rtp_instance_stats stats = { 0, };
01258
01259
01260 if (SSL_CTX_get_verify_mode(rtp->ssl_ctx) != SSL_VERIFY_NONE) {
01261 X509 *certificate;
01262
01263 if (!(certificate = SSL_get_peer_certificate(rtp->ssl))) {
01264 ast_log(LOG_WARNING, "No certificate was provided by the peer on RTP instance '%p'\n", instance);
01265 return -1;
01266 }
01267
01268
01269 if (rtp->remote_fingerprint[0]) {
01270 unsigned char fingerprint[EVP_MAX_MD_SIZE];
01271 unsigned int size;
01272
01273 if (!X509_digest(certificate, EVP_sha1(), fingerprint, &size) ||
01274 !size ||
01275 memcmp(fingerprint, rtp->remote_fingerprint, size)) {
01276 X509_free(certificate);
01277 ast_log(LOG_WARNING, "Fingerprint provided by remote party does not match that of peer certificate on RTP instance '%p'\n",
01278 instance);
01279 return -1;
01280 }
01281 }
01282
01283 X509_free(certificate);
01284 }
01285
01286
01287 if (SSL_get_verify_result(rtp->ssl) != X509_V_OK) {
01288 ast_log(LOG_WARNING, "Peer certificate on RTP instance '%p' failed verification test\n",
01289 instance);
01290 return -1;
01291 }
01292
01293
01294 if (!SSL_export_keying_material(rtp->ssl, material, SRTP_MASTER_LEN * 2, "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) {
01295 ast_log(LOG_WARNING, "Unable to extract SRTP keying material from DTLS-SRTP negotiation on RTP instance '%p'\n",
01296 instance);
01297 return -1;
01298 }
01299
01300
01301 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
01302 local_key = material;
01303 remote_key = local_key + SRTP_MASTER_KEY_LEN;
01304 local_salt = remote_key + SRTP_MASTER_KEY_LEN;
01305 remote_salt = local_salt + SRTP_MASTER_SALT_LEN;
01306 } else {
01307 remote_key = material;
01308 local_key = remote_key + SRTP_MASTER_KEY_LEN;
01309 remote_salt = local_key + SRTP_MASTER_KEY_LEN;
01310 local_salt = remote_salt + SRTP_MASTER_SALT_LEN;
01311 }
01312
01313 if (!(local_policy = res_srtp_policy->alloc())) {
01314 return -1;
01315 }
01316
01317 if (res_srtp_policy->set_master_key(local_policy, local_key, SRTP_MASTER_KEY_LEN, local_salt, SRTP_MASTER_SALT_LEN) < 0) {
01318 ast_log(LOG_WARNING, "Could not set key/salt information on local policy of '%p' when setting up DTLS-SRTP\n", rtp);
01319 goto error;
01320 }
01321
01322 if (res_srtp_policy->set_suite(local_policy, rtp->suite)) {
01323 ast_log(LOG_WARNING, "Could not set suite to '%d' on local policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
01324 goto error;
01325 }
01326
01327 if (ast_rtp_instance_get_stats(instance, &stats, AST_RTP_INSTANCE_STAT_LOCAL_SSRC)) {
01328 goto error;
01329 }
01330
01331 res_srtp_policy->set_ssrc(local_policy, stats.local_ssrc, 0);
01332
01333 if (!(remote_policy = res_srtp_policy->alloc())) {
01334 goto error;
01335 }
01336
01337 if (res_srtp_policy->set_master_key(remote_policy, remote_key, SRTP_MASTER_KEY_LEN, remote_salt, SRTP_MASTER_SALT_LEN) < 0) {
01338 ast_log(LOG_WARNING, "Could not set key/salt information on remote policy of '%p' when setting up DTLS-SRTP\n", rtp);
01339 goto error;
01340 }
01341
01342 if (res_srtp_policy->set_suite(remote_policy, rtp->suite)) {
01343 ast_log(LOG_WARNING, "Could not set suite to '%d' on remote policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
01344 goto error;
01345 }
01346
01347 res_srtp_policy->set_ssrc(remote_policy, 0, 1);
01348
01349 if (ast_rtp_instance_add_srtp_policy(instance, remote_policy, local_policy)) {
01350 ast_log(LOG_WARNING, "Could not set policies when setting up DTLS-SRTP on '%p'\n", rtp);
01351 goto error;
01352 }
01353
01354 if (rtp->rekey) {
01355 ao2_ref(instance, +1);
01356 if ((rtp->rekeyid = ast_sched_add(rtp->sched, rtp->rekey * 1000, dtls_srtp_renegotiate, instance)) < 0) {
01357 ao2_ref(instance, -1);
01358 goto error;
01359 }
01360 }
01361
01362 return 0;
01363
01364 error:
01365 res_srtp_policy->destroy(local_policy);
01366
01367 if (remote_policy) {
01368 res_srtp_policy->destroy(remote_policy);
01369 }
01370
01371 return -1;
01372 }
01373 #endif
01374
01375 static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp)
01376 {
01377 int len;
01378 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01379 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
01380
01381 if ((len = ast_recvfrom(rtcp ? rtp->rtcp->s : rtp->s, buf, size, flags, sa)) < 0) {
01382 return len;
01383 }
01384
01385 #ifdef HAVE_OPENSSL_SRTP
01386 if (!rtcp) {
01387 char *in = buf;
01388
01389 dtls_srtp_check_pending(instance, rtp);
01390
01391
01392 if ((*in >= 20) && (*in <= 64)) {
01393 int res = 0;
01394
01395
01396 if (!rtp->ssl) {
01397 ast_log(LOG_ERROR, "Received SSL traffic on RTP instance '%p' without an SSL session\n",
01398 instance);
01399 return -1;
01400 }
01401
01402
01403 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
01404 rtp->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
01405 SSL_set_accept_state(rtp->ssl);
01406 }
01407
01408 dtls_srtp_check_pending(instance, rtp);
01409
01410 BIO_write(rtp->read_bio, buf, len);
01411
01412 len = SSL_read(rtp->ssl, buf, len);
01413
01414 dtls_srtp_check_pending(instance, rtp);
01415
01416 if (rtp->dtls_failure) {
01417 ast_log(LOG_ERROR, "DTLS failure occurred on RTP instance '%p', terminating\n",
01418 instance);
01419 return -1;
01420 }
01421
01422 if (SSL_is_init_finished(rtp->ssl)) {
01423
01424 rtp->connection = AST_RTP_DTLS_CONNECTION_EXISTING;
01425
01426
01427 res = dtls_srtp_setup(rtp, srtp, instance);
01428 }
01429
01430 return res;
01431 }
01432 }
01433 #endif
01434
01435 if (rtp->ice) {
01436 pj_str_t combined = pj_str(ast_sockaddr_stringify(sa));
01437 pj_sockaddr address;
01438 pj_status_t status;
01439
01440 pj_thread_register_check();
01441
01442 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &combined, &address);
01443
01444 status = pj_ice_sess_on_rx_pkt(rtp->ice, rtcp ? COMPONENT_RTCP : COMPONENT_RTP,
01445 rtcp ? TRANSPORT_SOCKET_RTCP : TRANSPORT_SOCKET_RTP, buf, len, &address,
01446 pj_sockaddr_get_len(&address));
01447 if (status != PJ_SUCCESS) {
01448 char buf[100];
01449
01450 pj_strerror(status, buf, sizeof(buf));
01451 ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n",
01452 (int) status, buf);
01453 return -1;
01454 }
01455 if (!rtp->passthrough) {
01456 return 0;
01457 }
01458 rtp->passthrough = 0;
01459 }
01460
01461 if (res_srtp && srtp && res_srtp->unprotect(srtp, buf, &len, rtcp) < 0) {
01462 return -1;
01463 }
01464
01465 return len;
01466 }
01467
01468 static int rtcp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
01469 {
01470 return __rtp_recvfrom(instance, buf, size, flags, sa, 1);
01471 }
01472
01473 static int rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
01474 {
01475 return __rtp_recvfrom(instance, buf, size, flags, sa, 0);
01476 }
01477
01478 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)
01479 {
01480 int len = size;
01481 void *temp = buf;
01482 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01483 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
01484
01485 *ice = 0;
01486
01487 if (use_srtp && res_srtp && srtp && res_srtp->protect(srtp, &temp, &len, rtcp) < 0) {
01488 return -1;
01489 }
01490
01491 if (rtp->ice) {
01492 pj_thread_register_check();
01493
01494 if (pj_ice_sess_send_data(rtp->ice, rtcp ? COMPONENT_RTCP : COMPONENT_RTP, temp, len) == PJ_SUCCESS) {
01495 *ice = 1;
01496 return 0;
01497 }
01498 }
01499
01500 return ast_sendto(rtcp ? rtp->rtcp->s : rtp->s, temp, len, flags, sa);
01501 }
01502
01503 static int rtcp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
01504 {
01505 return __rtp_sendto(instance, buf, size, flags, sa, 1, ice, 1);
01506 }
01507
01508 static int rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
01509 {
01510 return __rtp_sendto(instance, buf, size, flags, sa, 0, ice, 1);
01511 }
01512
01513 static int rtp_get_rate(struct ast_format *format)
01514 {
01515 return (format->id == AST_FORMAT_G722) ? 8000 : ast_format_rate(format);
01516 }
01517
01518 static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
01519 {
01520 unsigned int interval;
01521
01522
01523 interval = rtcpinterval;
01524 return interval;
01525 }
01526
01527
01528 static double normdev_compute(double normdev, double sample, unsigned int sample_count)
01529 {
01530 normdev = normdev * sample_count + sample;
01531 sample_count++;
01532
01533 return normdev / sample_count;
01534 }
01535
01536 static double stddev_compute(double stddev, double sample, double normdev, double normdev_curent, unsigned int sample_count)
01537 {
01538
01539
01540
01541
01542
01543
01544 #define SQUARE(x) ((x) * (x))
01545
01546 stddev = sample_count * stddev;
01547 sample_count++;
01548
01549 return stddev +
01550 ( sample_count * SQUARE( (sample - normdev) / sample_count ) ) +
01551 ( SQUARE(sample - normdev_curent) / sample_count );
01552
01553 #undef SQUARE
01554 }
01555
01556 static int create_new_socket(const char *type, int af)
01557 {
01558 int sock = socket(af, SOCK_DGRAM, 0);
01559
01560 if (sock < 0) {
01561 if (!type) {
01562 type = "RTP/RTCP";
01563 }
01564 ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
01565 } else {
01566 long flags = fcntl(sock, F_GETFL);
01567 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
01568 #ifdef SO_NO_CHECK
01569 if (nochecksums) {
01570 setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
01571 }
01572 #endif
01573 }
01574
01575 return sock;
01576 }
01577
01578
01579
01580
01581
01582
01583
01584
01585
01586 static void rtp_learning_seq_init(struct ast_rtp *rtp, uint16_t seq)
01587 {
01588 rtp->learning_max_seq = seq - 1;
01589 rtp->learning_probation = learning_min_sequential;
01590 }
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601 static int rtp_learning_rtp_seq_update(struct ast_rtp *rtp, uint16_t seq)
01602 {
01603 int probation = 1;
01604
01605 ast_debug(1, "%p -- probation = %d, seq = %d\n", rtp, rtp->learning_probation, seq);
01606
01607 if (seq == rtp->learning_max_seq + 1) {
01608
01609 rtp->learning_probation--;
01610 rtp->learning_max_seq = seq;
01611 if (rtp->learning_probation == 0) {
01612 probation = 0;
01613 }
01614 } else {
01615 rtp->learning_probation = learning_min_sequential - 1;
01616 rtp->learning_max_seq = seq;
01617 }
01618
01619 return probation;
01620 }
01621
01622 static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *addr, int port, int component,
01623 int transport, const pj_turn_sock_cb *turn_cb, pj_turn_sock **turn_sock)
01624 {
01625 pj_sockaddr address[16];
01626 unsigned int count = PJ_ARRAY_SIZE(address), pos = 0;
01627
01628
01629 pj_enum_ip_interface(ast_sockaddr_is_ipv4(addr) ? pj_AF_INET() : pj_AF_INET6(), &count, address);
01630
01631 for (pos = 0; pos < count; pos++) {
01632 pj_sockaddr_set_port(&address[pos], port);
01633 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_HOST, 65535, &address[pos], &address[pos], NULL,
01634 pj_sockaddr_get_len(&address[pos]));
01635 }
01636
01637
01638 if (stunaddr.sin_addr.s_addr && ast_sockaddr_is_ipv4(addr)) {
01639 struct sockaddr_in answer;
01640
01641 if (!ast_stun_request(rtp->s, &stunaddr, NULL, &answer)) {
01642 pj_str_t mapped = pj_str(ast_strdupa(ast_inet_ntoa(answer.sin_addr)));
01643
01644 pj_sockaddr_init(pj_AF_INET(), &address[0], &mapped, ntohs(answer.sin_port));
01645
01646 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_SRFLX, 65535, &address[0], &address[0],
01647 NULL, pj_sockaddr_get_len(&address[0]));
01648 }
01649 }
01650
01651
01652 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,
01653 turn_cb, NULL, instance, turn_sock) == PJ_SUCCESS) {
01654 pj_stun_auth_cred cred = { 0, };
01655 struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_ALLOCATION_WAIT_TIME, 1000));
01656 struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
01657
01658 cred.type = PJ_STUN_AUTH_CRED_STATIC;
01659 cred.data.static_cred.username = turnusername;
01660 cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
01661 cred.data.static_cred.data = turnpassword;
01662
01663
01664 ast_mutex_lock(&rtp->lock);
01665 pj_turn_sock_alloc(*turn_sock, &turnaddr, turnport, NULL, &cred, NULL);
01666 ast_cond_timedwait(&rtp->cond, &rtp->lock, &ts);
01667 ast_mutex_unlock(&rtp->lock);
01668
01669
01670 if (rtp->turn_state == PJ_TURN_STATE_READY) {
01671 pj_turn_session_info info;
01672
01673 pj_turn_sock_get_info(*turn_sock, &info);
01674
01675 if (transport == TRANSPORT_SOCKET_RTP) {
01676 transport = TRANSPORT_TURN_RTP;
01677 } else if (transport == TRANSPORT_SOCKET_RTCP) {
01678 transport = TRANSPORT_TURN_RTCP;
01679 }
01680
01681 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_RELAYED, 65535, &info.relay_addr, &info.relay_addr,
01682 NULL, pj_sockaddr_get_len(&info.relay_addr));
01683 }
01684 }
01685 }
01686
01687 static int ast_rtp_new(struct ast_rtp_instance *instance,
01688 struct ast_sched_context *sched, struct ast_sockaddr *addr,
01689 void *data)
01690 {
01691 struct ast_rtp *rtp = NULL;
01692 int x, startplace;
01693 pj_stun_config stun_config;
01694 pj_str_t ufrag, passwd;
01695
01696
01697 if (!(rtp = ast_calloc(1, sizeof(*rtp)))) {
01698 return -1;
01699 }
01700
01701
01702 ast_mutex_init(&rtp->lock);
01703 ast_cond_init(&rtp->cond, NULL);
01704
01705
01706 rtp->ssrc = ast_random();
01707 rtp->seqno = ast_random() & 0xffff;
01708 rtp->strict_rtp_state = (strictrtp ? STRICT_RTP_LEARN : STRICT_RTP_OPEN);
01709 if (strictrtp) {
01710 rtp_learning_seq_init(rtp, (uint16_t)rtp->seqno);
01711 }
01712
01713
01714 if ((rtp->s =
01715 create_new_socket("RTP",
01716 ast_sockaddr_is_ipv4(addr) ? AF_INET :
01717 ast_sockaddr_is_ipv6(addr) ? AF_INET6 : -1)) < 0) {
01718 ast_debug(1, "Failed to create a new socket for RTP instance '%p'\n", instance);
01719 ast_free(rtp);
01720 return -1;
01721 }
01722
01723
01724 x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
01725 x = x & ~1;
01726 startplace = x;
01727
01728 for (;;) {
01729 ast_sockaddr_set_port(addr, x);
01730
01731 if (!ast_bind(rtp->s, addr)) {
01732 ast_debug(1, "Allocated port %d for RTP instance '%p'\n", x, instance);
01733 ast_rtp_instance_set_local_address(instance, addr);
01734 break;
01735 }
01736
01737 x += 2;
01738 if (x > rtpend) {
01739 x = (rtpstart + 1) & ~1;
01740 }
01741
01742
01743 if (x == startplace || errno != EADDRINUSE) {
01744 ast_log(LOG_ERROR, "Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance);
01745 close(rtp->s);
01746 ast_free(rtp);
01747 return -1;
01748 }
01749 }
01750
01751 pj_thread_register_check();
01752
01753 pj_stun_config_init(&stun_config, &cachingpool.factory, 0, ioqueue, timerheap);
01754
01755 generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag));
01756 ufrag = pj_str(rtp->local_ufrag);
01757 generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd));
01758 passwd = pj_str(rtp->local_passwd);
01759
01760 ast_rtp_instance_set_data(instance, rtp);
01761
01762
01763 if (icesupport && pj_ice_sess_create(&stun_config, NULL, PJ_ICE_SESS_ROLE_UNKNOWN, 2, &ast_rtp_ice_sess_cb, &ufrag, &passwd, &rtp->ice) == PJ_SUCCESS) {
01764
01765 rtp->ice->user_data = rtp;
01766
01767
01768 rtp_add_candidates_to_ice(instance, rtp, addr, x, COMPONENT_RTP, TRANSPORT_SOCKET_RTP, &ast_rtp_turn_rtp_sock_cb, &rtp->turn_rtp);
01769 }
01770
01771
01772 rtp->sched = sched;
01773
01774 #ifdef HAVE_OPENSSL_SRTP
01775 rtp->rekeyid = -1;
01776 #endif
01777
01778 return 0;
01779 }
01780
01781 static int ast_rtp_destroy(struct ast_rtp_instance *instance)
01782 {
01783 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01784
01785
01786 if (rtp->smoother) {
01787 ast_smoother_free(rtp->smoother);
01788 }
01789
01790
01791 if (rtp->s > -1) {
01792 close(rtp->s);
01793 }
01794
01795
01796 if (rtp->rtcp) {
01797
01798
01799
01800
01801
01802 close(rtp->rtcp->s);
01803 ast_free(rtp->rtcp);
01804 }
01805
01806
01807 if (rtp->red) {
01808 AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
01809 ast_free(rtp->red);
01810 }
01811
01812 pj_thread_register_check();
01813
01814
01815 if (rtp->ice) {
01816 pj_ice_sess_destroy(rtp->ice);
01817 }
01818
01819
01820 if (rtp->turn_rtp) {
01821 pj_turn_sock_set_user_data(rtp->turn_rtp, NULL);
01822 pj_turn_sock_destroy(rtp->turn_rtp);
01823 }
01824
01825
01826 if (rtp->turn_rtcp) {
01827 pj_turn_sock_set_user_data(rtp->turn_rtcp, NULL);
01828 pj_turn_sock_destroy(rtp->turn_rtcp);
01829 }
01830
01831
01832 if (rtp->local_candidates) {
01833 ao2_ref(rtp->local_candidates, -1);
01834 }
01835
01836 if (rtp->remote_candidates) {
01837 ao2_ref(rtp->remote_candidates, -1);
01838 }
01839
01840 #ifdef HAVE_OPENSSL_SRTP
01841
01842 if (rtp->ssl_ctx) {
01843 SSL_CTX_free(rtp->ssl_ctx);
01844 }
01845
01846
01847 if (rtp->ssl) {
01848 SSL_free(rtp->ssl);
01849 }
01850 #endif
01851
01852
01853 ast_mutex_destroy(&rtp->lock);
01854 ast_cond_destroy(&rtp->cond);
01855
01856
01857 ast_free(rtp);
01858
01859 return 0;
01860 }
01861
01862 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
01863 {
01864 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01865 rtp->dtmfmode = dtmf_mode;
01866 return 0;
01867 }
01868
01869 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance)
01870 {
01871 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01872 return rtp->dtmfmode;
01873 }
01874
01875 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit)
01876 {
01877 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01878 struct ast_sockaddr remote_address = { {0,} };
01879 int hdrlen = 12, res = 0, i = 0, payload = 101;
01880 char data[256];
01881 unsigned int *rtpheader = (unsigned int*)data;
01882
01883 ast_rtp_instance_get_remote_address(instance, &remote_address);
01884
01885
01886 if (ast_sockaddr_isnull(&remote_address)) {
01887 return -1;
01888 }
01889
01890
01891 if ((digit <= '9') && (digit >= '0')) {
01892 digit -= '0';
01893 } else if (digit == '*') {
01894 digit = 10;
01895 } else if (digit == '#') {
01896 digit = 11;
01897 } else if ((digit >= 'A') && (digit <= 'D')) {
01898 digit = digit - 'A' + 12;
01899 } else if ((digit >= 'a') && (digit <= 'd')) {
01900 digit = digit - 'a' + 12;
01901 } else {
01902 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
01903 return -1;
01904 }
01905
01906
01907 payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 0, NULL, AST_RTP_DTMF);
01908
01909 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
01910 rtp->send_duration = 160;
01911 rtp->lastdigitts = rtp->lastts + rtp->send_duration;
01912
01913
01914 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
01915 rtpheader[1] = htonl(rtp->lastdigitts);
01916 rtpheader[2] = htonl(rtp->ssrc);
01917
01918
01919 for (i = 0; i < 2; i++) {
01920 int ice;
01921
01922 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
01923 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
01924 if (res < 0) {
01925 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
01926 ast_sockaddr_stringify(&remote_address),
01927 strerror(errno));
01928 }
01929 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
01930 if (rtp_debug_test_addr(&remote_address)) {
01931 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
01932 ast_sockaddr_stringify(&remote_address),
01933 ice ? " (via ICE)" : "",
01934 payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
01935 }
01936 rtp->seqno++;
01937 rtp->send_duration += 160;
01938 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
01939 }
01940
01941
01942 rtp->sending_digit = 1;
01943 rtp->send_digit = digit;
01944 rtp->send_payload = payload;
01945
01946 return 0;
01947 }
01948
01949 static int ast_rtp_dtmf_continuation(struct ast_rtp_instance *instance)
01950 {
01951 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01952 struct ast_sockaddr remote_address = { {0,} };
01953 int hdrlen = 12, res = 0;
01954 char data[256];
01955 unsigned int *rtpheader = (unsigned int*)data;
01956 int ice;
01957
01958 ast_rtp_instance_get_remote_address(instance, &remote_address);
01959
01960
01961 if (ast_sockaddr_isnull(&remote_address)) {
01962 return -1;
01963 }
01964
01965
01966 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
01967 rtpheader[1] = htonl(rtp->lastdigitts);
01968 rtpheader[2] = htonl(rtp->ssrc);
01969 rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
01970
01971
01972 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
01973 if (res < 0) {
01974 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
01975 ast_sockaddr_stringify(&remote_address),
01976 strerror(errno));
01977 }
01978
01979 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
01980
01981 if (rtp_debug_test_addr(&remote_address)) {
01982 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
01983 ast_sockaddr_stringify(&remote_address),
01984 ice ? " (via ICE)" : "",
01985 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
01986 }
01987
01988
01989 rtp->seqno++;
01990 rtp->send_duration += 160;
01991
01992 return 0;
01993 }
01994
01995 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
01996 {
01997 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01998 struct ast_sockaddr remote_address = { {0,} };
01999 int hdrlen = 12, res = 0, i = 0;
02000 char data[256];
02001 unsigned int *rtpheader = (unsigned int*)data;
02002 unsigned int measured_samples;
02003
02004 ast_rtp_instance_get_remote_address(instance, &remote_address);
02005
02006
02007 if (ast_sockaddr_isnull(&remote_address)) {
02008 return -1;
02009 }
02010
02011
02012 if ((digit <= '9') && (digit >= '0')) {
02013 digit -= '0';
02014 } else if (digit == '*') {
02015 digit = 10;
02016 } else if (digit == '#') {
02017 digit = 11;
02018 } else if ((digit >= 'A') && (digit <= 'D')) {
02019 digit = digit - 'A' + 12;
02020 } else if ((digit >= 'a') && (digit <= 'd')) {
02021 digit = digit - 'a' + 12;
02022 } else {
02023 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
02024 return -1;
02025 }
02026
02027 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
02028
02029 if (duration > 0 && (measured_samples = duration * rtp_get_rate(&rtp->f.subclass.format) / 1000) > rtp->send_duration) {
02030 ast_debug(2, "Adjusting final end duration from %u to %u\n", rtp->send_duration, measured_samples);
02031 rtp->send_duration = measured_samples;
02032 }
02033
02034
02035 rtpheader[1] = htonl(rtp->lastdigitts);
02036 rtpheader[2] = htonl(rtp->ssrc);
02037 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
02038 rtpheader[3] |= htonl((1 << 23));
02039
02040
02041 for (i = 0; i < 3; i++) {
02042 int ice;
02043
02044 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
02045
02046 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
02047
02048 if (res < 0) {
02049 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
02050 ast_sockaddr_stringify(&remote_address),
02051 strerror(errno));
02052 }
02053
02054 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
02055
02056 if (rtp_debug_test_addr(&remote_address)) {
02057 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
02058 ast_sockaddr_stringify(&remote_address),
02059 ice ? " (via ICE)" : "",
02060 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
02061 }
02062
02063 rtp->seqno++;
02064 }
02065
02066
02067 rtp->lastts += rtp->send_duration;
02068 rtp->sending_digit = 0;
02069 rtp->send_digit = 0;
02070
02071 return 0;
02072 }
02073
02074 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit)
02075 {
02076 return ast_rtp_dtmf_end_with_duration(instance, digit, 0);
02077 }
02078
02079 static void ast_rtp_update_source(struct ast_rtp_instance *instance)
02080 {
02081 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02082
02083
02084 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
02085 ast_debug(3, "Setting the marker bit due to a source update\n");
02086
02087 return;
02088 }
02089
02090 static void ast_rtp_change_source(struct ast_rtp_instance *instance)
02091 {
02092 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02093 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
02094 unsigned int ssrc = ast_random();
02095
02096 if (!rtp->lastts) {
02097 ast_debug(3, "Not changing SSRC since we haven't sent any RTP yet\n");
02098 return;
02099 }
02100
02101
02102 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
02103
02104 ast_debug(3, "Changing ssrc from %u to %u due to a source change\n", rtp->ssrc, ssrc);
02105
02106 if (srtp) {
02107 ast_debug(3, "Changing ssrc for SRTP from %u to %u\n", rtp->ssrc, ssrc);
02108 res_srtp->change_source(srtp, rtp->ssrc, ssrc);
02109 }
02110
02111 rtp->ssrc = ssrc;
02112
02113 return;
02114 }
02115
02116 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
02117 {
02118 struct timeval t;
02119 long ms;
02120
02121 if (ast_tvzero(rtp->txcore)) {
02122 rtp->txcore = ast_tvnow();
02123 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
02124 }
02125
02126 t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
02127 if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) {
02128 ms = 0;
02129 }
02130 rtp->txcore = t;
02131
02132 return (unsigned int) ms;
02133 }
02134
02135 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
02136 {
02137 unsigned int sec, usec, frac;
02138 sec = tv.tv_sec + 2208988800u;
02139 usec = tv.tv_usec;
02140 frac = (usec << 12) + (usec << 8) - ((usec * 3650) >> 6);
02141 *msw = sec;
02142 *lsw = frac;
02143 }
02144
02145
02146 static int ast_rtcp_write_rr(struct ast_rtp_instance *instance)
02147 {
02148 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02149 int res;
02150 int len = 32;
02151 unsigned int lost;
02152 unsigned int extended;
02153 unsigned int expected;
02154 unsigned int expected_interval;
02155 unsigned int received_interval;
02156 int lost_interval;
02157 struct timeval now;
02158 unsigned int *rtcpheader;
02159 char bdata[1024];
02160 struct timeval dlsr;
02161 int fraction;
02162 int rate = rtp_get_rate(&rtp->f.subclass.format);
02163 int ice;
02164 double rxlost_current;
02165 struct ast_sockaddr remote_address = { {0,} };
02166
02167 if (!rtp || !rtp->rtcp)
02168 return 0;
02169
02170 if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
02171
02172
02173
02174 return 0;
02175 }
02176
02177 extended = rtp->cycles + rtp->lastrxseqno;
02178 expected = extended - rtp->seedrxseqno + 1;
02179 lost = expected - rtp->rxcount;
02180 expected_interval = expected - rtp->rtcp->expected_prior;
02181 rtp->rtcp->expected_prior = expected;
02182 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
02183 rtp->rtcp->received_prior = rtp->rxcount;
02184 lost_interval = expected_interval - received_interval;
02185
02186 if (lost_interval <= 0)
02187 rtp->rtcp->rxlost = 0;
02188 else rtp->rtcp->rxlost = rtp->rtcp->rxlost;
02189 if (rtp->rtcp->rxlost_count == 0)
02190 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
02191 if (lost_interval < rtp->rtcp->minrxlost)
02192 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
02193 if (lost_interval > rtp->rtcp->maxrxlost)
02194 rtp->rtcp->maxrxlost = rtp->rtcp->rxlost;
02195
02196 rxlost_current = normdev_compute(rtp->rtcp->normdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->rxlost_count);
02197 rtp->rtcp->stdev_rxlost = stddev_compute(rtp->rtcp->stdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->normdev_rxlost, rxlost_current, rtp->rtcp->rxlost_count);
02198 rtp->rtcp->normdev_rxlost = rxlost_current;
02199 rtp->rtcp->rxlost_count++;
02200
02201 if (expected_interval == 0 || lost_interval <= 0)
02202 fraction = 0;
02203 else
02204 fraction = (lost_interval << 8) / expected_interval;
02205 gettimeofday(&now, NULL);
02206 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
02207 rtcpheader = (unsigned int *)bdata;
02208 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_RR << 16) | ((len/4)-1));
02209 rtcpheader[1] = htonl(rtp->ssrc);
02210 rtcpheader[2] = htonl(rtp->themssrc);
02211 rtcpheader[3] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
02212 rtcpheader[4] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
02213 rtcpheader[5] = htonl((unsigned int)(rtp->rxjitter * rate));
02214 rtcpheader[6] = htonl(rtp->rtcp->themrxlsr);
02215 rtcpheader[7] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
02216
02217
02218
02219 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
02220 rtcpheader[(len/4)+1] = htonl(rtp->ssrc);
02221 rtcpheader[(len/4)+2] = htonl(0x01 << 24);
02222 len += 12;
02223
02224 ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
02225
02226 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &remote_address, &ice);
02227
02228 if (res < 0) {
02229 ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno));
02230 return 0;
02231 }
02232
02233 rtp->rtcp->rr_count++;
02234
02235 update_address_with_ice_candidate(rtp, COMPONENT_RTCP, &remote_address);
02236
02237 if (rtcp_debug_test_addr(&remote_address)) {
02238 ast_verbose("\n* Sending RTCP RR to %s%s\n"
02239 " Our SSRC: %u\nTheir SSRC: %u\niFraction lost: %d\nCumulative loss: %u\n"
02240 " IA jitter: %.4f\n"
02241 " Their last SR: %u\n"
02242 " DLSR: %4.4f (sec)\n\n",
02243 ast_sockaddr_stringify(&remote_address),
02244 ice ? " (via ICE)" : "",
02245 rtp->ssrc, rtp->themssrc, fraction, lost,
02246 rtp->rxjitter,
02247 rtp->rtcp->themrxlsr,
02248 (double)(ntohl(rtcpheader[7])/65536.0));
02249 }
02250
02251 return res;
02252 }
02253
02254
02255 static int ast_rtcp_write_sr(struct ast_rtp_instance *instance)
02256 {
02257 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02258 int res;
02259 int len = 0;
02260 struct timeval now;
02261 unsigned int now_lsw;
02262 unsigned int now_msw;
02263 unsigned int *rtcpheader;
02264 unsigned int lost;
02265 unsigned int extended;
02266 unsigned int expected;
02267 unsigned int expected_interval;
02268 unsigned int received_interval;
02269 int lost_interval;
02270 int fraction;
02271 struct timeval dlsr;
02272 char bdata[512];
02273 int rate = rtp_get_rate(&rtp->f.subclass.format);
02274 int ice;
02275 struct ast_sockaddr remote_address = { {0,} };
02276
02277 if (!rtp || !rtp->rtcp)
02278 return 0;
02279
02280 if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
02281
02282
02283
02284 return 0;
02285 }
02286
02287 gettimeofday(&now, NULL);
02288 timeval2ntp(now, &now_msw, &now_lsw);
02289 rtcpheader = (unsigned int *)bdata;
02290 rtcpheader[1] = htonl(rtp->ssrc);
02291 rtcpheader[2] = htonl(now_msw);
02292 rtcpheader[3] = htonl(now_lsw);
02293 rtcpheader[4] = htonl(rtp->lastts);
02294 rtcpheader[5] = htonl(rtp->txcount);
02295 rtcpheader[6] = htonl(rtp->txoctetcount);
02296 len += 28;
02297
02298 extended = rtp->cycles + rtp->lastrxseqno;
02299 expected = extended - rtp->seedrxseqno + 1;
02300 if (rtp->rxcount > expected)
02301 expected += rtp->rxcount - expected;
02302 lost = expected - rtp->rxcount;
02303 expected_interval = expected - rtp->rtcp->expected_prior;
02304 rtp->rtcp->expected_prior = expected;
02305 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
02306 rtp->rtcp->received_prior = rtp->rxcount;
02307 lost_interval = expected_interval - received_interval;
02308 if (expected_interval == 0 || lost_interval <= 0)
02309 fraction = 0;
02310 else
02311 fraction = (lost_interval << 8) / expected_interval;
02312 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
02313 rtcpheader[7] = htonl(rtp->themssrc);
02314 rtcpheader[8] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
02315 rtcpheader[9] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
02316 rtcpheader[10] = htonl((unsigned int)(rtp->rxjitter * rate));
02317 rtcpheader[11] = htonl(rtp->rtcp->themrxlsr);
02318 rtcpheader[12] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
02319 len += 24;
02320
02321 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SR << 16) | ((len/4)-1));
02322
02323
02324
02325 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
02326 rtcpheader[(len/4)+1] = htonl(rtp->ssrc);
02327 rtcpheader[(len/4)+2] = htonl(0x01 << 24);
02328 len += 12;
02329
02330 ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
02331
02332 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &remote_address, &ice);
02333 if (res < 0) {
02334 ast_log(LOG_ERROR, "RTCP SR transmission error to %s, rtcp halted %s\n",
02335 ast_sockaddr_stringify(&rtp->rtcp->them),
02336 strerror(errno));
02337 return 0;
02338 }
02339
02340
02341 gettimeofday(&rtp->rtcp->txlsr, NULL);
02342 rtp->rtcp->sr_count++;
02343
02344 rtp->rtcp->lastsrtxcount = rtp->txcount;
02345
02346 update_address_with_ice_candidate(rtp, COMPONENT_RTCP, &remote_address);
02347
02348 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
02349 ast_verbose("* Sent RTCP SR to %s%s\n", ast_sockaddr_stringify(&remote_address), ice ? " (via ICE)" : "");
02350 ast_verbose(" Our SSRC: %u\n", rtp->ssrc);
02351 ast_verbose(" Sent(NTP): %u.%010u\n", (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096);
02352 ast_verbose(" Sent(RTP): %u\n", rtp->lastts);
02353 ast_verbose(" Sent packets: %u\n", rtp->txcount);
02354 ast_verbose(" Sent octets: %u\n", rtp->txoctetcount);
02355 ast_verbose(" Report block:\n");
02356 ast_verbose(" Fraction lost: %u\n", fraction);
02357 ast_verbose(" Cumulative loss: %u\n", lost);
02358 ast_verbose(" IA jitter: %.4f\n", rtp->rxjitter);
02359 ast_verbose(" Their last SR: %u\n", rtp->rtcp->themrxlsr);
02360 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(ntohl(rtcpheader[12])/65536.0));
02361 }
02362 manager_event(EVENT_FLAG_REPORTING, "RTCPSent", "To: %s\r\n"
02363 "OurSSRC: %u\r\n"
02364 "SentNTP: %u.%010u\r\n"
02365 "SentRTP: %u\r\n"
02366 "SentPackets: %u\r\n"
02367 "SentOctets: %u\r\n"
02368 "ReportBlock:\r\n"
02369 "FractionLost: %u\r\n"
02370 "CumulativeLoss: %u\r\n"
02371 "IAJitter: %.4f\r\n"
02372 "TheirLastSR: %u\r\n"
02373 "DLSR: %4.4f (sec)\r\n",
02374 ast_sockaddr_stringify(&remote_address),
02375 rtp->ssrc,
02376 (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096,
02377 rtp->lastts,
02378 rtp->txcount,
02379 rtp->txoctetcount,
02380 fraction,
02381 lost,
02382 rtp->rxjitter,
02383 rtp->rtcp->themrxlsr,
02384 (double)(ntohl(rtcpheader[12])/65536.0));
02385 return res;
02386 }
02387
02388
02389
02390
02391 static int ast_rtcp_write(const void *data)
02392 {
02393 struct ast_rtp_instance *instance = (struct ast_rtp_instance *) data;
02394 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02395 int res;
02396
02397 if (!rtp || !rtp->rtcp || rtp->rtcp->schedid == -1) {
02398 ao2_ref(instance, -1);
02399 return 0;
02400 }
02401
02402 if (rtp->txcount > rtp->rtcp->lastsrtxcount) {
02403 res = ast_rtcp_write_sr(instance);
02404 } else {
02405 res = ast_rtcp_write_rr(instance);
02406 }
02407
02408 if (!res) {
02409
02410
02411
02412 ao2_ref(instance, -1);
02413 rtp->rtcp->schedid = -1;
02414 }
02415
02416 return res;
02417 }
02418
02419 static int ast_rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec)
02420 {
02421 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02422 int pred, mark = 0;
02423 unsigned int ms = calc_txstamp(rtp, &frame->delivery);
02424 struct ast_sockaddr remote_address = { {0,} };
02425 int rate = rtp_get_rate(&frame->subclass.format) / 1000;
02426
02427 if (frame->subclass.format.id == AST_FORMAT_G722) {
02428 frame->samples /= 2;
02429 }
02430
02431 if (rtp->sending_digit) {
02432 return 0;
02433 }
02434
02435 if (frame->frametype == AST_FRAME_VOICE) {
02436 pred = rtp->lastts + frame->samples;
02437
02438
02439 rtp->lastts = rtp->lastts + ms * rate;
02440 if (ast_tvzero(frame->delivery)) {
02441
02442
02443 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW) {
02444 rtp->lastts = pred;
02445 } else {
02446 ast_debug(3, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
02447 mark = 1;
02448 }
02449 }
02450 } else if (frame->frametype == AST_FRAME_VIDEO) {
02451 mark = ast_format_get_video_mark(&frame->subclass.format);
02452 pred = rtp->lastovidtimestamp + frame->samples;
02453
02454 rtp->lastts = rtp->lastts + ms * 90;
02455
02456 if (ast_tvzero(frame->delivery)) {
02457 if (abs(rtp->lastts - pred) < 7200) {
02458 rtp->lastts = pred;
02459 rtp->lastovidtimestamp += frame->samples;
02460 } else {
02461 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);
02462 rtp->lastovidtimestamp = rtp->lastts;
02463 }
02464 }
02465 } else {
02466 pred = rtp->lastotexttimestamp + frame->samples;
02467
02468 rtp->lastts = rtp->lastts + ms;
02469
02470 if (ast_tvzero(frame->delivery)) {
02471 if (abs(rtp->lastts - pred) < 7200) {
02472 rtp->lastts = pred;
02473 rtp->lastotexttimestamp += frame->samples;
02474 } else {
02475 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);
02476 rtp->lastotexttimestamp = rtp->lastts;
02477 }
02478 }
02479 }
02480
02481
02482 if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
02483 mark = 1;
02484 ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT);
02485 }
02486
02487
02488 if (rtp->lastts > rtp->lastdigitts) {
02489 rtp->lastdigitts = rtp->lastts;
02490 }
02491
02492 if (ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO)) {
02493 rtp->lastts = frame->ts * rate;
02494 }
02495
02496 ast_rtp_instance_get_remote_address(instance, &remote_address);
02497
02498
02499 if (!ast_sockaddr_isnull(&remote_address)) {
02500 int hdrlen = 12, res, ice;
02501 unsigned char *rtpheader = (unsigned char *)(frame->data.ptr - hdrlen);
02502
02503 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
02504 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
02505 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
02506
02507 if ((res = rtp_sendto(instance, (void *)rtpheader, frame->datalen + hdrlen, 0, &remote_address, &ice)) < 0) {
02508 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))) {
02509 ast_debug(1, "RTP Transmission error of packet %d to %s: %s\n",
02510 rtp->seqno,
02511 ast_sockaddr_stringify(&remote_address),
02512 strerror(errno));
02513 } else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) {
02514
02515 if (rtpdebug)
02516 ast_debug(0, "RTP NAT: Can't write RTP to private address %s, waiting for other end to send audio...\n",
02517 ast_sockaddr_stringify(&remote_address));
02518 ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN);
02519 }
02520 } else {
02521 rtp->txcount++;
02522 rtp->txoctetcount += (res - hdrlen);
02523
02524 if (rtp->rtcp && rtp->rtcp->schedid < 1) {
02525 ast_debug(1, "Starting RTCP transmission on RTP instance '%p'\n", instance);
02526 ao2_ref(instance, +1);
02527 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, instance);
02528 if (rtp->rtcp->schedid < 0) {
02529 ao2_ref(instance, -1);
02530 ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n");
02531 }
02532 }
02533 }
02534
02535 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
02536
02537 if (rtp_debug_test_addr(&remote_address)) {
02538 ast_verbose("Sent RTP packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
02539 ast_sockaddr_stringify(&remote_address),
02540 ice ? " (via ICE)" : "",
02541 codec, rtp->seqno, rtp->lastts, res - hdrlen);
02542 }
02543 }
02544
02545 rtp->seqno++;
02546
02547 return 0;
02548 }
02549
02550 static struct ast_frame *red_t140_to_red(struct rtp_red *red) {
02551 unsigned char *data = red->t140red.data.ptr;
02552 int len = 0;
02553 int i;
02554
02555
02556 if (red->len[0]) {
02557 for (i = 1; i < red->num_gen+1; i++)
02558 len += red->len[i];
02559
02560 memmove(&data[red->hdrlen], &data[red->hdrlen+red->len[0]], len);
02561 }
02562
02563
02564 for (i = 0; i < red->num_gen; i++)
02565 red->len[i] = red->len[i+1];
02566 red->len[i] = red->t140.datalen;
02567
02568
02569 len = red->hdrlen;
02570 for (i = 0; i < red->num_gen; i++)
02571 len += data[i*4+3] = red->len[i];
02572
02573
02574 memcpy(&data[len], red->t140.data.ptr, red->t140.datalen);
02575 red->t140red.datalen = len + red->t140.datalen;
02576
02577
02578 if (len == red->hdrlen && !red->t140.datalen)
02579 return NULL;
02580
02581
02582 red->t140.datalen = 0;
02583
02584 return &red->t140red;
02585 }
02586
02587 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
02588 {
02589 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02590 struct ast_sockaddr remote_address = { {0,} };
02591 struct ast_format subclass;
02592 int codec;
02593
02594 ast_rtp_instance_get_remote_address(instance, &remote_address);
02595
02596
02597 if (ast_sockaddr_isnull(&remote_address)) {
02598 ast_debug(1, "No remote address on RTP instance '%p' so dropping frame\n", instance);
02599 return 0;
02600 }
02601
02602
02603 if (!frame->datalen) {
02604 ast_debug(1, "Received frame with no data for RTP instance '%p' so dropping frame\n", instance);
02605 return 0;
02606 }
02607
02608
02609 if (frame->frametype != AST_FRAME_VOICE && frame->frametype != AST_FRAME_VIDEO && frame->frametype != AST_FRAME_TEXT) {
02610 ast_log(LOG_WARNING, "RTP can only send voice, video, and text\n");
02611 return -1;
02612 }
02613
02614 if (rtp->red) {
02615
02616
02617 if ((frame = red_t140_to_red(rtp->red)) == NULL)
02618 return 0;
02619 }
02620
02621
02622 ast_format_copy(&subclass, &frame->subclass.format);
02623 if ((codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 1, &subclass, 0)) < 0) {
02624 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(&frame->subclass.format));
02625 return -1;
02626 }
02627
02628
02629 if (ast_format_cmp(&rtp->lasttxformat, &subclass) == AST_FORMAT_CMP_NOT_EQUAL) {
02630 ast_debug(1, "Ooh, format changed from %s to %s\n", ast_getformatname(&rtp->lasttxformat), ast_getformatname(&subclass));
02631 rtp->lasttxformat = subclass;
02632 ast_format_copy(&rtp->lasttxformat, &subclass);
02633 if (rtp->smoother) {
02634 ast_smoother_free(rtp->smoother);
02635 rtp->smoother = NULL;
02636 }
02637 }
02638
02639
02640 if (!rtp->smoother) {
02641 struct ast_format_list fmt = ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance)->pref, &subclass);
02642
02643 switch (subclass.id) {
02644 case AST_FORMAT_SPEEX:
02645 case AST_FORMAT_SPEEX16:
02646 case AST_FORMAT_SPEEX32:
02647 case AST_FORMAT_SILK:
02648 case AST_FORMAT_CELT:
02649 case AST_FORMAT_G723_1:
02650 case AST_FORMAT_SIREN7:
02651 case AST_FORMAT_SIREN14:
02652 case AST_FORMAT_G719:
02653
02654
02655 break;
02656 default:
02657 if (fmt.inc_ms) {
02658 if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
02659 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));
02660 return -1;
02661 }
02662 if (fmt.flags) {
02663 ast_smoother_set_flags(rtp->smoother, fmt.flags);
02664 }
02665 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));
02666 }
02667 }
02668 }
02669
02670
02671 if (rtp->smoother) {
02672 struct ast_frame *f;
02673
02674 if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
02675 ast_smoother_feed_be(rtp->smoother, frame);
02676 } else {
02677 ast_smoother_feed(rtp->smoother, frame);
02678 }
02679
02680 while ((f = ast_smoother_read(rtp->smoother)) && (f->data.ptr)) {
02681 ast_rtp_raw_write(instance, f, codec);
02682 }
02683 } else {
02684 int hdrlen = 12;
02685 struct ast_frame *f = NULL;
02686
02687 if (frame->offset < hdrlen) {
02688 f = ast_frdup(frame);
02689 } else {
02690 f = frame;
02691 }
02692 if (f->data.ptr) {
02693 ast_rtp_raw_write(instance, f, codec);
02694 }
02695 if (f != frame) {
02696 ast_frfree(f);
02697 }
02698
02699 }
02700
02701 return 0;
02702 }
02703
02704 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
02705 {
02706 struct timeval now;
02707 struct timeval tmp;
02708 double transit;
02709 double current_time;
02710 double d;
02711 double dtv;
02712 double prog;
02713 int rate = rtp_get_rate(&rtp->f.subclass.format);
02714
02715 double normdev_rxjitter_current;
02716 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
02717 gettimeofday(&rtp->rxcore, NULL);
02718 rtp->drxcore = (double) rtp->rxcore.tv_sec + (double) rtp->rxcore.tv_usec / 1000000;
02719
02720 rtp->seedrxts = timestamp;
02721 tmp = ast_samp2tv(timestamp, rate);
02722 rtp->rxcore = ast_tvsub(rtp->rxcore, tmp);
02723
02724 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
02725 }
02726
02727 gettimeofday(&now,NULL);
02728
02729 tmp = ast_samp2tv(timestamp, rate);
02730 *tv = ast_tvadd(rtp->rxcore, tmp);
02731
02732 prog = (double)((timestamp-rtp->seedrxts)/(float)(rate));
02733 dtv = (double)rtp->drxcore + (double)(prog);
02734 current_time = (double)now.tv_sec + (double)now.tv_usec/1000000;
02735 transit = current_time - dtv;
02736 d = transit - rtp->rxtransit;
02737 rtp->rxtransit = transit;
02738 if (d<0)
02739 d=-d;
02740 rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
02741
02742 if (rtp->rtcp) {
02743 if (rtp->rxjitter > rtp->rtcp->maxrxjitter)
02744 rtp->rtcp->maxrxjitter = rtp->rxjitter;
02745 if (rtp->rtcp->rxjitter_count == 1)
02746 rtp->rtcp->minrxjitter = rtp->rxjitter;
02747 if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter)
02748 rtp->rtcp->minrxjitter = rtp->rxjitter;
02749
02750 normdev_rxjitter_current = normdev_compute(rtp->rtcp->normdev_rxjitter,rtp->rxjitter,rtp->rtcp->rxjitter_count);
02751 rtp->rtcp->stdev_rxjitter = stddev_compute(rtp->rtcp->stdev_rxjitter,rtp->rxjitter,rtp->rtcp->normdev_rxjitter,normdev_rxjitter_current,rtp->rtcp->rxjitter_count);
02752
02753 rtp->rtcp->normdev_rxjitter = normdev_rxjitter_current;
02754 rtp->rtcp->rxjitter_count++;
02755 }
02756 }
02757
02758 static struct ast_frame *create_dtmf_frame(struct ast_rtp_instance *instance, enum ast_frame_type type, int compensate)
02759 {
02760 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02761 struct ast_sockaddr remote_address = { {0,} };
02762
02763 ast_rtp_instance_get_remote_address(instance, &remote_address);
02764
02765 if (((compensate && type == AST_FRAME_DTMF_END) || (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
02766 ast_debug(1, "Ignore potential DTMF echo from '%s'\n",
02767 ast_sockaddr_stringify(&remote_address));
02768 rtp->resp = 0;
02769 rtp->dtmfsamples = 0;
02770 return &ast_null_frame;
02771 }
02772 ast_debug(1, "Creating %s DTMF Frame: %d (%c), at %s\n",
02773 type == AST_FRAME_DTMF_END ? "END" : "BEGIN",
02774 rtp->resp, rtp->resp,
02775 ast_sockaddr_stringify(&remote_address));
02776 if (rtp->resp == 'X') {
02777 rtp->f.frametype = AST_FRAME_CONTROL;
02778 rtp->f.subclass.integer = AST_CONTROL_FLASH;
02779 } else {
02780 rtp->f.frametype = type;
02781 rtp->f.subclass.integer = rtp->resp;
02782 }
02783 rtp->f.datalen = 0;
02784 rtp->f.samples = 0;
02785 rtp->f.mallocd = 0;
02786 rtp->f.src = "RTP";
02787 AST_LIST_NEXT(&rtp->f, frame_list) = NULL;
02788
02789 return &rtp->f;
02790 }
02791
02792 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)
02793 {
02794 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02795 struct ast_sockaddr remote_address = { {0,} };
02796 unsigned int event, event_end, samples;
02797 char resp = 0;
02798 struct ast_frame *f = NULL;
02799
02800 ast_rtp_instance_get_remote_address(instance, &remote_address);
02801
02802
02803 event = ntohl(*((unsigned int *)(data)));
02804 event >>= 24;
02805 event_end = ntohl(*((unsigned int *)(data)));
02806 event_end <<= 8;
02807 event_end >>= 24;
02808 samples = ntohl(*((unsigned int *)(data)));
02809 samples &= 0xFFFF;
02810
02811 if (rtp_debug_test_addr(&remote_address)) {
02812 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",
02813 ast_sockaddr_stringify(&remote_address),
02814 payloadtype, seqno, timestamp, len, (mark?1:0), event, ((event_end & 0x80)?1:0), samples);
02815 }
02816
02817
02818 if (rtpdebug)
02819 ast_debug(0, "- RTP 2833 Event: %08x (len = %d)\n", event, len);
02820
02821
02822 if (event < 10) {
02823 resp = '0' + event;
02824 } else if (event < 11) {
02825 resp = '*';
02826 } else if (event < 12) {
02827 resp = '#';
02828 } else if (event < 16) {
02829 resp = 'A' + (event - 12);
02830 } else if (event < 17) {
02831 resp = 'X';
02832 } else {
02833
02834 ast_debug(1, "Ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", event);
02835 return;
02836 }
02837
02838 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) {
02839 if ((rtp->last_end_timestamp != timestamp) || (rtp->resp && rtp->resp != resp)) {
02840 rtp->resp = resp;
02841 rtp->dtmf_timeout = 0;
02842 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)));
02843 f->len = 0;
02844 rtp->last_end_timestamp = timestamp;
02845 AST_LIST_INSERT_TAIL(frames, f, frame_list);
02846 }
02847 } else {
02848
02849
02850
02851
02852
02853 unsigned int new_duration = rtp->dtmf_duration;
02854 unsigned int last_duration = new_duration & 0xFFFF;
02855
02856 if (last_duration > 64000 && samples < last_duration) {
02857 new_duration += 0xFFFF + 1;
02858 }
02859 new_duration = (new_duration & ~0xFFFF) | samples;
02860
02861 if (event_end & 0x80) {
02862
02863 if ((rtp->last_seqno != seqno) && (timestamp > rtp->last_end_timestamp)) {
02864 rtp->last_end_timestamp = timestamp;
02865 rtp->dtmf_duration = new_duration;
02866 rtp->resp = resp;
02867 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
02868 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0));
02869 rtp->resp = 0;
02870 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
02871 AST_LIST_INSERT_TAIL(frames, f, frame_list);
02872 } else if (rtpdebug) {
02873 ast_debug(1, "Dropping duplicate or out of order DTMF END frame (seqno: %d, ts %d, digit %c)\n",
02874 seqno, timestamp, resp);
02875 }
02876 } else {
02877
02878
02879
02880
02881
02882
02883 if ((rtp->last_seqno > seqno && rtp->last_seqno - seqno < 50)
02884 || timestamp <= rtp->last_end_timestamp) {
02885
02886
02887
02888
02889 if (rtpdebug) {
02890 ast_debug(1, "Dropping out of order DTMF frame (seqno %d, ts %d, digit %c)\n",
02891 seqno, timestamp, resp);
02892 }
02893 return;
02894 }
02895
02896 if (rtp->resp && rtp->resp != resp) {
02897
02898 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
02899 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0));
02900 rtp->resp = 0;
02901 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
02902 AST_LIST_INSERT_TAIL(frames, f, frame_list);
02903 }
02904
02905 if (rtp->resp) {
02906
02907 rtp->dtmf_duration = new_duration;
02908 } else {
02909
02910 rtp->resp = resp;
02911 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0));
02912 rtp->dtmf_duration = samples;
02913 AST_LIST_INSERT_TAIL(frames, f, frame_list);
02914 }
02915
02916 rtp->dtmf_timeout = timestamp + rtp->dtmf_duration + dtmftimeout;
02917 }
02918
02919 rtp->last_seqno = seqno;
02920 }
02921
02922 rtp->dtmfsamples = samples;
02923
02924 return;
02925 }
02926
02927 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)
02928 {
02929 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02930 unsigned int event, flags, power;
02931 char resp = 0;
02932 unsigned char seq;
02933 struct ast_frame *f = NULL;
02934
02935 if (len < 4) {
02936 return NULL;
02937 }
02938
02939
02940
02941
02942
02943
02944
02945
02946
02947
02948
02949
02950
02951
02952
02953
02954
02955
02956
02957
02958
02959
02960
02961
02962
02963
02964
02965
02966
02967
02968
02969 seq = data[0];
02970 flags = data[1];
02971 power = data[2];
02972 event = data[3] & 0x1f;
02973
02974 if (rtpdebug)
02975 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);
02976 if (event < 10) {
02977 resp = '0' + event;
02978 } else if (event < 11) {
02979 resp = '*';
02980 } else if (event < 12) {
02981 resp = '#';
02982 } else if (event < 16) {
02983 resp = 'A' + (event - 12);
02984 } else if (event < 17) {
02985 resp = 'X';
02986 }
02987 if ((!rtp->resp && power) || (rtp->resp && (rtp->resp != resp))) {
02988 rtp->resp = resp;
02989
02990 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) {
02991 f = create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0);
02992 rtp->dtmfsamples = 0;
02993 }
02994 } else if ((rtp->resp == resp) && !power) {
02995 f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE));
02996 f->samples = rtp->dtmfsamples * (rtp->lastrxformat.id ? (rtp_get_rate(&rtp->lastrxformat) / 1000) : 8);
02997 rtp->resp = 0;
02998 } else if (rtp->resp == resp)
02999 rtp->dtmfsamples += 20 * (rtp->lastrxformat.id ? (rtp_get_rate(&rtp->lastrxformat) / 1000) : 8);
03000
03001 rtp->dtmf_timeout = 0;
03002
03003 return f;
03004 }
03005
03006 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)
03007 {
03008 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
03009
03010
03011
03012
03013 if (rtpdebug)
03014 ast_debug(0, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", (int) rtp->lastrxformat.id, len);
03015
03016 if (ast_test_flag(rtp, FLAG_3389_WARNING)) {
03017 struct ast_sockaddr remote_address = { {0,} };
03018
03019 ast_rtp_instance_get_remote_address(instance, &remote_address);
03020
03021 ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client address: %s\n",
03022 ast_sockaddr_stringify(&remote_address));
03023 ast_set_flag(rtp, FLAG_3389_WARNING);
03024 }
03025
03026
03027 if (!len)
03028 return NULL;
03029 if (len < 24) {
03030 rtp->f.data.ptr = rtp->rawdata + AST_FRIENDLY_OFFSET;
03031 rtp->f.datalen = len - 1;
03032 rtp->f.offset = AST_FRIENDLY_OFFSET;
03033 memcpy(rtp->f.data.ptr, data + 1, len - 1);
03034 } else {
03035 rtp->f.data.ptr = NULL;
03036 rtp->f.offset = 0;
03037 rtp->f.datalen = 0;
03038 }
03039 rtp->f.frametype = AST_FRAME_CNG;
03040 rtp->f.subclass.integer = data[0] & 0x7f;
03041 rtp->f.samples = 0;
03042 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
03043
03044 return &rtp->f;
03045 }
03046
03047 static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance)
03048 {
03049 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
03050 struct ast_sockaddr addr;
03051 unsigned char rtcpdata[8192 + AST_FRIENDLY_OFFSET];
03052 unsigned int *rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
03053 int res, packetwords, position = 0;
03054 struct ast_frame *f = &ast_null_frame;
03055
03056
03057 if ((res = rtcp_recvfrom(instance, rtcpdata + AST_FRIENDLY_OFFSET,
03058 sizeof(rtcpdata) - AST_FRIENDLY_OFFSET,
03059 0, &addr)) < 0) {
03060 ast_assert(errno != EBADF);
03061 if (errno != EAGAIN) {
03062 ast_log(LOG_WARNING, "RTCP Read error: %s. Hanging up.\n",
03063 (errno) ? strerror(errno) : "Unspecified");
03064 return NULL;
03065 }
03066 return &ast_null_frame;
03067 }
03068
03069
03070 if (!res) {
03071 return &ast_null_frame;
03072 }
03073
03074 if (!*(rtcpdata + AST_FRIENDLY_OFFSET)) {
03075 struct sockaddr_in addr_tmp;
03076 struct ast_sockaddr addr_v4;
03077
03078 if (ast_sockaddr_is_ipv4(&addr)) {
03079 ast_sockaddr_to_sin(&addr, &addr_tmp);
03080 } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) {
03081 ast_debug(1, "Using IPv6 mapped address %s for STUN\n",
03082 ast_sockaddr_stringify(&addr));
03083 ast_sockaddr_to_sin(&addr_v4, &addr_tmp);
03084 } else {
03085 ast_debug(1, "Cannot do STUN for non IPv4 address %s\n",
03086 ast_sockaddr_stringify(&addr));
03087 return &ast_null_frame;
03088 }
03089 if ((ast_stun_handle_packet(rtp->rtcp->s, &addr_tmp, rtcpdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == AST_STUN_ACCEPT)) {
03090 ast_sockaddr_from_sin(&addr, &addr_tmp);
03091 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
03092 }
03093 return &ast_null_frame;
03094 }
03095
03096 packetwords = res / 4;
03097
03098 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT)) {
03099
03100 if (ast_sockaddr_cmp(&rtp->rtcp->them, &addr)) {
03101 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
03102 if (rtpdebug)
03103 ast_debug(0, "RTCP NAT: Got RTCP from other end. Now sending to address %s\n",
03104 ast_sockaddr_stringify(&rtp->rtcp->them));
03105 }
03106 }
03107
03108 ast_debug(1, "Got RTCP report of %d bytes\n", res);
03109
03110 while (position < packetwords) {
03111 int i, pt, rc;
03112 unsigned int length, dlsr, lsr, msw, lsw, comp;
03113 struct timeval now;
03114 double rttsec, reported_jitter, reported_normdev_jitter_current, normdevrtt_current, reported_lost, reported_normdev_lost_current;
03115 uint64_t rtt = 0;
03116
03117 i = position;
03118 length = ntohl(rtcpheader[i]);
03119 pt = (length & 0xff0000) >> 16;
03120 rc = (length & 0x1f000000) >> 24;
03121 length &= 0xffff;
03122
03123 if ((i + length) > packetwords) {
03124 if (rtpdebug)
03125 ast_debug(1, "RTCP Read too short\n");
03126 return &ast_null_frame;
03127 }
03128
03129 if (rtcp_debug_test_addr(&addr)) {
03130 ast_verbose("\n\nGot RTCP from %s\n",
03131 ast_sockaddr_stringify(&addr));
03132 ast_verbose("PT: %d(%s)\n", pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown");
03133 ast_verbose("Reception reports: %d\n", rc);
03134 ast_verbose("SSRC of sender: %u\n", rtcpheader[i + 1]);
03135 }
03136
03137 i += 2;
03138 if (rc == 0 && pt == RTCP_PT_RR) {
03139 position += (length + 1);
03140 continue;
03141 }
03142
03143 switch (pt) {
03144 case RTCP_PT_SR:
03145 gettimeofday(&rtp->rtcp->rxlsr,NULL);
03146 rtp->rtcp->spc = ntohl(rtcpheader[i+3]);
03147 rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
03148 rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16);
03149
03150 if (rtcp_debug_test_addr(&addr)) {
03151 ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader[i]), (unsigned long) ntohl(rtcpheader[i + 1]) * 4096);
03152 ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader[i + 2]));
03153 ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader[i + 3]), (unsigned long) ntohl(rtcpheader[i + 4]));
03154 }
03155 i += 5;
03156 if (rc < 1)
03157 break;
03158
03159 case RTCP_PT_RR:
03160
03161
03162 gettimeofday(&now, NULL);
03163 timeval2ntp(now, &msw, &lsw);
03164 if (ntohl(rtcpheader[i + 4]) && ntohl(rtcpheader[i + 5])) {
03165 comp = ((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16);
03166 lsr = ntohl(rtcpheader[i + 4]);
03167 dlsr = ntohl(rtcpheader[i + 5]);
03168 rtt = comp - lsr - dlsr;
03169
03170
03171
03172 if (rtt < 4294) {
03173 rtt = (rtt * 1000000) >> 16;
03174 } else {
03175 rtt = (rtt * 1000) >> 16;
03176 rtt *= 1000;
03177 }
03178 rtt = rtt / 1000.;
03179 rttsec = rtt / 1000.;
03180 rtp->rtcp->rtt = rttsec;
03181
03182 if (comp - dlsr >= lsr) {
03183 rtp->rtcp->accumulated_transit += rttsec;
03184
03185 if (rtp->rtcp->rtt_count == 0)
03186 rtp->rtcp->minrtt = rttsec;
03187
03188 if (rtp->rtcp->maxrtt<rttsec)
03189 rtp->rtcp->maxrtt = rttsec;
03190 if (rtp->rtcp->minrtt>rttsec)
03191 rtp->rtcp->minrtt = rttsec;
03192
03193 normdevrtt_current = normdev_compute(rtp->rtcp->normdevrtt, rttsec, rtp->rtcp->rtt_count);
03194
03195 rtp->rtcp->stdevrtt = stddev_compute(rtp->rtcp->stdevrtt, rttsec, rtp->rtcp->normdevrtt, normdevrtt_current, rtp->rtcp->rtt_count);
03196
03197 rtp->rtcp->normdevrtt = normdevrtt_current;
03198
03199 rtp->rtcp->rtt_count++;
03200 } else if (rtcp_debug_test_addr(&addr)) {
03201 ast_verbose("Internal RTCP NTP clock skew detected: "
03202 "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
03203 "diff=%d\n",
03204 lsr, comp, dlsr, dlsr / 65536,
03205 (dlsr % 65536) * 1000 / 65536,
03206 dlsr - (comp - lsr));
03207 }
03208 }
03209
03210 rtp->rtcp->reported_jitter = ntohl(rtcpheader[i + 3]);
03211 reported_jitter = (double) rtp->rtcp->reported_jitter;
03212
03213 if (rtp->rtcp->reported_jitter_count == 0)
03214 rtp->rtcp->reported_minjitter = reported_jitter;
03215
03216 if (reported_jitter < rtp->rtcp->reported_minjitter)
03217 rtp->rtcp->reported_minjitter = reported_jitter;
03218
03219 if (reported_jitter > rtp->rtcp->reported_maxjitter)
03220 rtp->rtcp->reported_maxjitter = reported_jitter;
03221
03222 reported_normdev_jitter_current = normdev_compute(rtp->rtcp->reported_normdev_jitter, reported_jitter, rtp->rtcp->reported_jitter_count);
03223
03224 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);
03225
03226 rtp->rtcp->reported_normdev_jitter = reported_normdev_jitter_current;
03227
03228 rtp->rtcp->reported_lost = ntohl(rtcpheader[i + 1]) & 0xffffff;
03229
03230 reported_lost = (double) rtp->rtcp->reported_lost;
03231
03232
03233 if (rtp->rtcp->reported_jitter_count == 0)
03234 rtp->rtcp->reported_minlost = reported_lost;
03235
03236 if (reported_lost < rtp->rtcp->reported_minlost)
03237 rtp->rtcp->reported_minlost = reported_lost;
03238
03239 if (reported_lost > rtp->rtcp->reported_maxlost)
03240 rtp->rtcp->reported_maxlost = reported_lost;
03241 reported_normdev_lost_current = normdev_compute(rtp->rtcp->reported_normdev_lost, reported_lost, rtp->rtcp->reported_jitter_count);
03242
03243 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);
03244
03245 rtp->rtcp->reported_normdev_lost = reported_normdev_lost_current;
03246
03247 rtp->rtcp->reported_jitter_count++;
03248
03249 if (rtcp_debug_test_addr(&addr)) {
03250 ast_verbose(" Fraction lost: %ld\n", (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24));
03251 ast_verbose(" Packets lost so far: %d\n", rtp->rtcp->reported_lost);
03252 ast_verbose(" Highest sequence number: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff));
03253 ast_verbose(" Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader[i + 2])) >> 16);
03254 ast_verbose(" Interarrival jitter: %u\n", rtp->rtcp->reported_jitter);
03255 ast_verbose(" Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader[i + 4]) >> 16,((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096);
03256 ast_verbose(" DLSR: %4.4f (sec)\n",ntohl(rtcpheader[i + 5])/65536.0);
03257 if (rtt)
03258 ast_verbose(" RTT: %lu(sec)\n", (unsigned long) rtt);
03259 }
03260 if (rtt) {
03261 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s\r\n"
03262 "PT: %d(%s)\r\n"
03263 "ReceptionReports: %d\r\n"
03264 "SenderSSRC: %u\r\n"
03265 "FractionLost: %ld\r\n"
03266 "PacketsLost: %d\r\n"
03267 "HighestSequence: %ld\r\n"
03268 "SequenceNumberCycles: %ld\r\n"
03269 "IAJitter: %u\r\n"
03270 "LastSR: %lu.%010lu\r\n"
03271 "DLSR: %4.4f(sec)\r\n"
03272 "RTT: %llu(sec)\r\n",
03273 ast_sockaddr_stringify(&addr),
03274 pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
03275 rc,
03276 rtcpheader[i + 1],
03277 (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
03278 rtp->rtcp->reported_lost,
03279 (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
03280 (long) (ntohl(rtcpheader[i + 2])) >> 16,
03281 rtp->rtcp->reported_jitter,
03282 (unsigned long) ntohl(rtcpheader[i + 4]) >> 16, ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
03283 ntohl(rtcpheader[i + 5])/65536.0,
03284 (unsigned long long)rtt);
03285 } else {
03286 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s\r\n"
03287 "PT: %d(%s)\r\n"
03288 "ReceptionReports: %d\r\n"
03289 "SenderSSRC: %u\r\n"
03290 "FractionLost: %ld\r\n"
03291 "PacketsLost: %d\r\n"
03292 "HighestSequence: %ld\r\n"
03293 "SequenceNumberCycles: %ld\r\n"
03294 "IAJitter: %u\r\n"
03295 "LastSR: %lu.%010lu\r\n"
03296 "DLSR: %4.4f(sec)\r\n",
03297 ast_sockaddr_stringify(&addr),
03298 pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
03299 rc,
03300 rtcpheader[i + 1],
03301 (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
03302 rtp->rtcp->reported_lost,
03303 (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
03304 (long) (ntohl(rtcpheader[i + 2])) >> 16,
03305 rtp->rtcp->reported_jitter,
03306 (unsigned long) ntohl(rtcpheader[i + 4]) >> 16,
03307 ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
03308 ntohl(rtcpheader[i + 5])/65536.0);
03309 }
03310 break;
03311 case RTCP_PT_FUR:
03312 if (rtcp_debug_test_addr(&addr))
03313 ast_verbose("Received an RTCP Fast Update Request\n");
03314 rtp->f.frametype = AST_FRAME_CONTROL;
03315 rtp->f.subclass.integer = AST_CONTROL_VIDUPDATE;
03316 rtp->f.datalen = 0;
03317 rtp->f.samples = 0;
03318 rtp->f.mallocd = 0;
03319 rtp->f.src = "RTP";
03320 f = &rtp->f;
03321 break;
03322 case RTCP_PT_SDES:
03323 if (rtcp_debug_test_addr(&addr))
03324 ast_verbose("Received an SDES from %s\n",
03325 ast_sockaddr_stringify(&rtp->rtcp->them));
03326 break;
03327 case RTCP_PT_BYE:
03328 if (rtcp_debug_test_addr(&addr))
03329 ast_verbose("Received a BYE from %s\n",
03330 ast_sockaddr_stringify(&rtp->rtcp->them));
03331 break;
03332 default:
03333 ast_debug(1, "Unknown RTCP packet (pt=%d) received from %s\n",
03334 pt, ast_sockaddr_stringify(&rtp->rtcp->them));
03335 break;
03336 }
03337 position += (length + 1);
03338 }
03339
03340 rtp->rtcp->rtcp_info = 1;
03341
03342 return f;
03343 }
03344
03345 static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, unsigned int *rtpheader, int len, int hdrlen)
03346 {
03347 struct ast_rtp_instance *instance1 = ast_rtp_instance_get_bridged(instance);
03348 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance), *bridged = ast_rtp_instance_get_data(instance1);
03349 int res = 0, payload = 0, bridged_payload = 0, mark;
03350 struct ast_rtp_payload_type payload_type;
03351 int reconstruct = ntohl(rtpheader[0]);
03352 struct ast_sockaddr remote_address = { {0,} };
03353 int ice;
03354
03355
03356 payload = (reconstruct & 0x7f0000) >> 16;
03357 mark = (((reconstruct & 0x800000) >> 23) != 0);
03358
03359
03360 payload_type = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(instance), payload);
03361
03362
03363 bridged_payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance1), payload_type.asterisk_format, &payload_type.format, payload_type.rtp_code);
03364
03365
03366 if (bridged_payload < 0) {
03367 return -1;
03368 }
03369
03370
03371 if (ast_rtp_codecs_find_payload_code(ast_rtp_instance_get_codecs(instance1), bridged_payload) == -1) {
03372 ast_debug(1, "Unsupported payload type received \n");
03373 return -1;
03374 }
03375
03376
03377 if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
03378 mark = 1;
03379 ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT);
03380 }
03381
03382
03383 reconstruct &= 0xFF80FFFF;
03384 reconstruct |= (bridged_payload << 16);
03385 reconstruct |= (mark << 23);
03386 rtpheader[0] = htonl(reconstruct);
03387
03388 ast_rtp_instance_get_remote_address(instance1, &remote_address);
03389
03390 if (ast_sockaddr_isnull(&remote_address)) {
03391 ast_debug(1, "Remote address is null, most likely RTP has been stopped\n");
03392 return 0;
03393 }
03394
03395
03396 res = rtp_sendto(instance1, (void *)rtpheader, len, 0, &remote_address, &ice);
03397 if (res < 0) {
03398 if (!ast_rtp_instance_get_prop(instance1, AST_RTP_PROPERTY_NAT) || (ast_rtp_instance_get_prop(instance1, AST_RTP_PROPERTY_NAT) && (ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
03399 ast_log(LOG_WARNING,
03400 "RTP Transmission error of packet to %s: %s\n",
03401 ast_sockaddr_stringify(&remote_address),
03402 strerror(errno));
03403 } else if (((ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(bridged, FLAG_NAT_INACTIVE_NOWARN)) {
03404 if (option_debug || rtpdebug)
03405 ast_log(LOG_WARNING,
03406 "RTP NAT: Can't write RTP to private "
03407 "address %s, waiting for other end to "
03408 "send audio...\n",
03409 ast_sockaddr_stringify(&remote_address));
03410 ast_set_flag(bridged, FLAG_NAT_INACTIVE_NOWARN);
03411 }
03412 return 0;
03413 }
03414
03415 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
03416
03417 if (rtp_debug_test_addr(&remote_address)) {
03418 ast_verbose("Sent RTP P2P packet to %s%s (type %-2.2d, len %-6.6u)\n",
03419 ast_sockaddr_stringify(&remote_address),
03420 ice ? " (via ICE)" : "",
03421 bridged_payload, len - hdrlen);
03422 }
03423
03424 return 0;
03425 }
03426
03427 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp)
03428 {
03429 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
03430 struct ast_sockaddr addr;
03431 int res, hdrlen = 12, version, payloadtype, padding, mark, ext, cc, prev_seqno;
03432 unsigned int *rtpheader = (unsigned int*)(rtp->rawdata + AST_FRIENDLY_OFFSET), seqno, ssrc, timestamp;
03433 struct ast_rtp_payload_type payload;
03434 struct ast_sockaddr remote_address = { {0,} };
03435 struct frame_list frames;
03436
03437
03438 if (rtcp) {
03439 if (rtp->rtcp) {
03440 return ast_rtcp_read(instance);
03441 }
03442 return &ast_null_frame;
03443 }
03444
03445
03446 if (rtp->sending_digit) {
03447 ast_rtp_dtmf_continuation(instance);
03448 }
03449
03450
03451 if ((res = rtp_recvfrom(instance, rtp->rawdata + AST_FRIENDLY_OFFSET,
03452 sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET, 0,
03453 &addr)) < 0) {
03454 ast_assert(errno != EBADF);
03455 if (errno != EAGAIN) {
03456 ast_log(LOG_WARNING, "RTP Read error: %s. Hanging up.\n",
03457 (errno) ? strerror(errno) : "Unspecified");
03458 return NULL;
03459 }
03460 return &ast_null_frame;
03461 }
03462
03463
03464 if (!res) {
03465 return &ast_null_frame;
03466 }
03467
03468
03469 if (res < hdrlen) {
03470 ast_log(LOG_WARNING, "RTP Read too short\n");
03471 return &ast_null_frame;
03472 }
03473
03474
03475 seqno = ntohl(rtpheader[0]);
03476
03477 ast_rtp_instance_get_remote_address(instance, &remote_address);
03478
03479 if (!(version = (seqno & 0xC0000000) >> 30)) {
03480 struct sockaddr_in addr_tmp;
03481 struct ast_sockaddr addr_v4;
03482 if (ast_sockaddr_is_ipv4(&addr)) {
03483 ast_sockaddr_to_sin(&addr, &addr_tmp);
03484 } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) {
03485 ast_debug(1, "Using IPv6 mapped address %s for STUN\n",
03486 ast_sockaddr_stringify(&addr));
03487 ast_sockaddr_to_sin(&addr_v4, &addr_tmp);
03488 } else {
03489 ast_debug(1, "Cannot do STUN for non IPv4 address %s\n",
03490 ast_sockaddr_stringify(&addr));
03491 return &ast_null_frame;
03492 }
03493 if ((ast_stun_handle_packet(rtp->s, &addr_tmp, rtp->rawdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == AST_STUN_ACCEPT) &&
03494 ast_sockaddr_isnull(&remote_address)) {
03495 ast_sockaddr_from_sin(&addr, &addr_tmp);
03496 ast_rtp_instance_set_remote_address(instance, &addr);
03497 }
03498 return &ast_null_frame;
03499 }
03500
03501
03502 if (rtp->strict_rtp_state == STRICT_RTP_LEARN) {
03503 ast_debug(1, "%p -- start learning mode pass with addr = %s\n", rtp, ast_sockaddr_stringify(&addr));
03504
03505 ast_sockaddr_copy(&rtp->strict_rtp_address, &addr);
03506
03507
03508 if (rtp_learning_rtp_seq_update(rtp, ntohl(rtpheader[0]))) {
03509 ast_debug(1, "%p -- Condition for learning hasn't exited, so reject the frame.\n", rtp);
03510 return &ast_null_frame;
03511 }
03512
03513 ast_debug(1, "%p -- Probation Ended. Set strict_rtp_state to STRICT_RTP_CLOSED with address %s\n", rtp, ast_sockaddr_stringify(&addr));
03514 rtp->strict_rtp_state = STRICT_RTP_CLOSED;
03515 } else if (rtp->strict_rtp_state == STRICT_RTP_CLOSED) {
03516 if (ast_sockaddr_cmp(&rtp->strict_rtp_address, &addr)) {
03517
03518 if (!ast_sockaddr_cmp(&rtp->alt_rtp_address, &addr)) {
03519
03520 ast_sockaddr_copy(&rtp->strict_rtp_address,
03521 &addr);
03522 } else {
03523 const char *real_addr = ast_strdupa(ast_sockaddr_stringify(&addr));
03524 const char *expected_addr = ast_strdupa(ast_sockaddr_stringify(&rtp->strict_rtp_address));
03525
03526 ast_debug(1, "Received RTP packet from %s, dropping due to strict RTP protection. Expected it to be from %s\n",
03527 real_addr, expected_addr);
03528
03529 return &ast_null_frame;
03530 }
03531 }
03532 }
03533
03534
03535 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT)) {
03536 if (ast_sockaddr_cmp(&remote_address, &addr)) {
03537 ast_rtp_instance_set_remote_address(instance, &addr);
03538 ast_sockaddr_copy(&remote_address, &addr);
03539 if (rtp->rtcp) {
03540 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
03541 ast_sockaddr_set_port(&rtp->rtcp->them, ast_sockaddr_port(&addr) + 1);
03542 }
03543 rtp->rxseqno = 0;
03544 ast_set_flag(rtp, FLAG_NAT_ACTIVE);
03545 if (rtpdebug)
03546 ast_debug(0, "RTP NAT: Got audio from other end. Now sending to address %s\n",
03547 ast_sockaddr_stringify(&remote_address));
03548 }
03549 }
03550
03551
03552 if (ast_rtp_instance_get_bridged(instance) && !bridge_p2p_rtp_write(instance, rtpheader, res, hdrlen)) {
03553 return &ast_null_frame;
03554 }
03555
03556
03557 if (version != 2) {
03558 return &ast_null_frame;
03559 }
03560
03561
03562 payloadtype = (seqno & 0x7f0000) >> 16;
03563 padding = seqno & (1 << 29);
03564 mark = seqno & (1 << 23);
03565 ext = seqno & (1 << 28);
03566 cc = (seqno & 0xF000000) >> 24;
03567 seqno &= 0xffff;
03568 timestamp = ntohl(rtpheader[1]);
03569 ssrc = ntohl(rtpheader[2]);
03570
03571 AST_LIST_HEAD_INIT_NOLOCK(&frames);
03572
03573 if (rtp->rxssrc && rtp->rxssrc != ssrc) {
03574 struct ast_frame *f, srcupdate = {
03575 AST_FRAME_CONTROL,
03576 .subclass.integer = AST_CONTROL_SRCCHANGE,
03577 };
03578
03579 if (!mark) {
03580 if (rtpdebug) {
03581 ast_debug(1, "Forcing Marker bit, because SSRC has changed\n");
03582 }
03583 mark = 1;
03584 }
03585
03586 f = ast_frisolate(&srcupdate);
03587 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
03588 }
03589
03590 rtp->rxssrc = ssrc;
03591
03592
03593 if (padding) {
03594 res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
03595 }
03596
03597
03598 if (cc) {
03599 hdrlen += cc * 4;
03600 }
03601
03602
03603 if (ext) {
03604 hdrlen += (ntohl(rtpheader[hdrlen/4]) & 0xffff) << 2;
03605 hdrlen += 4;
03606 if (option_debug) {
03607 int profile;
03608 profile = (ntohl(rtpheader[3]) & 0xffff0000) >> 16;
03609 if (profile == 0x505a)
03610 ast_debug(1, "Found Zfone extension in RTP stream - zrtp - not supported.\n");
03611 else
03612 ast_debug(1, "Found unknown RTP Extensions %x\n", profile);
03613 }
03614 }
03615
03616
03617 if (res < hdrlen) {
03618 ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d\n", res, hdrlen);
03619 return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame;
03620 }
03621
03622 rtp->rxcount++;
03623 if (rtp->rxcount == 1) {
03624 rtp->seedrxseqno = seqno;
03625 }
03626
03627
03628 if (rtp->rtcp && !ast_sockaddr_isnull(&rtp->rtcp->them) && rtp->rtcp->schedid < 1) {
03629
03630 ao2_ref(instance, +1);
03631 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, instance);
03632 if (rtp->rtcp->schedid < 0) {
03633 ao2_ref(instance, -1);
03634 ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n");
03635 }
03636 }
03637 if ((int)rtp->lastrxseqno - (int)seqno > 100)
03638 rtp->cycles += RTP_SEQ_MOD;
03639
03640 prev_seqno = rtp->lastrxseqno;
03641 rtp->lastrxseqno = seqno;
03642
03643 if (!rtp->themssrc) {
03644 rtp->themssrc = ntohl(rtpheader[2]);
03645 }
03646
03647 if (rtp_debug_test_addr(&addr)) {
03648 ast_verbose("Got RTP packet from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
03649 ast_sockaddr_stringify(&addr),
03650 payloadtype, seqno, timestamp,res - hdrlen);
03651 }
03652
03653 payload = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(instance), payloadtype);
03654
03655
03656 if (!payload.asterisk_format) {
03657 struct ast_frame *f = NULL;
03658 if (payload.rtp_code == AST_RTP_DTMF) {
03659
03660
03661
03662
03663 process_dtmf_rfc2833(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark, &frames);
03664 } else if (payload.rtp_code == AST_RTP_CISCO_DTMF) {
03665 f = process_dtmf_cisco(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark);
03666 } else if (payload.rtp_code == AST_RTP_CN) {
03667 f = process_cn_rfc3389(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark);
03668 } else {
03669 ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n",
03670 payloadtype,
03671 ast_sockaddr_stringify(&remote_address));
03672 }
03673
03674 if (f) {
03675 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
03676 }
03677
03678
03679
03680 if (!AST_LIST_EMPTY(&frames)) {
03681 return AST_LIST_FIRST(&frames);
03682 }
03683 return &ast_null_frame;
03684 }
03685
03686 ast_format_copy(&rtp->lastrxformat, &payload.format);
03687 ast_format_copy(&rtp->f.subclass.format, &payload.format);
03688 rtp->f.frametype = (AST_FORMAT_GET_TYPE(rtp->f.subclass.format.id) == AST_FORMAT_TYPE_AUDIO) ? AST_FRAME_VOICE : (AST_FORMAT_GET_TYPE(rtp->f.subclass.format.id) == AST_FORMAT_TYPE_VIDEO) ? AST_FRAME_VIDEO : AST_FRAME_TEXT;
03689
03690 rtp->rxseqno = seqno;
03691
03692 if (rtp->dtmf_timeout && rtp->dtmf_timeout < timestamp) {
03693 rtp->dtmf_timeout = 0;
03694
03695 if (rtp->resp) {
03696 struct ast_frame *f;
03697 f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0);
03698 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0));
03699 rtp->resp = 0;
03700 rtp->dtmf_timeout = rtp->dtmf_duration = 0;
03701 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
03702 return AST_LIST_FIRST(&frames);
03703 }
03704 }
03705
03706 rtp->lastrxts = timestamp;
03707
03708 rtp->f.src = "RTP";
03709 rtp->f.mallocd = 0;
03710 rtp->f.datalen = res - hdrlen;
03711 rtp->f.data.ptr = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
03712 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
03713 rtp->f.seqno = seqno;
03714
03715 if (rtp->f.subclass.format.id == AST_FORMAT_T140 && (int)seqno - (prev_seqno+1) > 0 && (int)seqno - (prev_seqno+1) < 10) {
03716 unsigned char *data = rtp->f.data.ptr;
03717
03718 memmove(rtp->f.data.ptr+3, rtp->f.data.ptr, rtp->f.datalen);
03719 rtp->f.datalen +=3;
03720 *data++ = 0xEF;
03721 *data++ = 0xBF;
03722 *data = 0xBD;
03723 }
03724
03725 if (rtp->f.subclass.format.id == AST_FORMAT_T140RED) {
03726 unsigned char *data = rtp->f.data.ptr;
03727 unsigned char *header_end;
03728 int num_generations;
03729 int header_length;
03730 int len;
03731 int diff =(int)seqno - (prev_seqno+1);
03732 int x;
03733
03734 ast_format_set(&rtp->f.subclass.format, AST_FORMAT_T140, 0);
03735 header_end = memchr(data, ((*data) & 0x7f), rtp->f.datalen);
03736 if (header_end == NULL) {
03737 return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame;
03738 }
03739 header_end++;
03740
03741 header_length = header_end - data;
03742 num_generations = header_length / 4;
03743 len = header_length;
03744
03745 if (!diff) {
03746 for (x = 0; x < num_generations; x++)
03747 len += data[x * 4 + 3];
03748
03749 if (!(rtp->f.datalen - len))
03750 return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame;
03751
03752 rtp->f.data.ptr += len;
03753 rtp->f.datalen -= len;
03754 } else if (diff > num_generations && diff < 10) {
03755 len -= 3;
03756 rtp->f.data.ptr += len;
03757 rtp->f.datalen -= len;
03758
03759 data = rtp->f.data.ptr;
03760 *data++ = 0xEF;
03761 *data++ = 0xBF;
03762 *data = 0xBD;
03763 } else {
03764 for ( x = 0; x < num_generations - diff; x++)
03765 len += data[x * 4 + 3];
03766
03767 rtp->f.data.ptr += len;
03768 rtp->f.datalen -= len;
03769 }
03770 }
03771
03772 if (AST_FORMAT_GET_TYPE(rtp->f.subclass.format.id) == AST_FORMAT_TYPE_AUDIO) {
03773 rtp->f.samples = ast_codec_get_samples(&rtp->f);
03774 if (ast_format_is_slinear(&rtp->f.subclass.format)) {
03775 ast_frame_byteswap_be(&rtp->f);
03776 }
03777 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
03778
03779 ast_set_flag(&rtp->f, AST_FRFLAG_HAS_TIMING_INFO);
03780 rtp->f.ts = timestamp / (rtp_get_rate(&rtp->f.subclass.format) / 1000);
03781 rtp->f.len = rtp->f.samples / ((ast_format_rate(&rtp->f.subclass.format) / 1000));
03782 } else if (AST_FORMAT_GET_TYPE(rtp->f.subclass.format.id) == AST_FORMAT_TYPE_VIDEO) {
03783
03784 if (!rtp->lastividtimestamp)
03785 rtp->lastividtimestamp = timestamp;
03786 rtp->f.samples = timestamp - rtp->lastividtimestamp;
03787 rtp->lastividtimestamp = timestamp;
03788 rtp->f.delivery.tv_sec = 0;
03789 rtp->f.delivery.tv_usec = 0;
03790
03791 if (mark) {
03792 ast_format_set_video_mark(&rtp->f.subclass.format);
03793 }
03794 } else {
03795
03796 if (!rtp->lastitexttimestamp)
03797 rtp->lastitexttimestamp = timestamp;
03798 rtp->f.samples = timestamp - rtp->lastitexttimestamp;
03799 rtp->lastitexttimestamp = timestamp;
03800 rtp->f.delivery.tv_sec = 0;
03801 rtp->f.delivery.tv_usec = 0;
03802 }
03803
03804 AST_LIST_INSERT_TAIL(&frames, &rtp->f, frame_list);
03805 return AST_LIST_FIRST(&frames);
03806 }
03807
03808 static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
03809 {
03810 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
03811
03812 if (property == AST_RTP_PROPERTY_RTCP) {
03813 if (value) {
03814 if (rtp->rtcp) {
03815 ast_debug(1, "Ignoring duplicate RTCP property on RTP instance '%p'\n", instance);
03816 return;
03817 }
03818
03819 if (!(rtp->rtcp = ast_calloc(1, sizeof(*rtp->rtcp)))) {
03820 return;
03821 }
03822
03823
03824 ast_rtp_instance_get_local_address(instance, &rtp->rtcp->us);
03825 ast_sockaddr_set_port(&rtp->rtcp->us,
03826 ast_sockaddr_port(&rtp->rtcp->us) + 1);
03827
03828 if ((rtp->rtcp->s =
03829 create_new_socket("RTCP",
03830 ast_sockaddr_is_ipv4(&rtp->rtcp->us) ?
03831 AF_INET :
03832 ast_sockaddr_is_ipv6(&rtp->rtcp->us) ?
03833 AF_INET6 : -1)) < 0) {
03834 ast_debug(1, "Failed to create a new socket for RTCP on instance '%p'\n", instance);
03835 ast_free(rtp->rtcp);
03836 rtp->rtcp = NULL;
03837 return;
03838 }
03839
03840
03841 if (ast_bind(rtp->rtcp->s, &rtp->rtcp->us)) {
03842 ast_debug(1, "Failed to setup RTCP on RTP instance '%p'\n", instance);
03843 close(rtp->rtcp->s);
03844 ast_free(rtp->rtcp);
03845 rtp->rtcp = NULL;
03846 return;
03847 }
03848
03849 ast_debug(1, "Setup RTCP on RTP instance '%p'\n", instance);
03850 rtp->rtcp->schedid = -1;
03851
03852 if (rtp->ice) {
03853 rtp_add_candidates_to_ice(instance, rtp, &rtp->rtcp->us, ast_sockaddr_port(&rtp->rtcp->us), COMPONENT_RTCP, TRANSPORT_SOCKET_RTCP,
03854 &ast_rtp_turn_rtcp_sock_cb, &rtp->turn_rtcp);
03855 }
03856
03857 return;
03858 } else {
03859 if (rtp->rtcp) {
03860 if (rtp->rtcp->schedid > 0) {
03861 if (!ast_sched_del(rtp->sched, rtp->rtcp->schedid)) {
03862
03863 ao2_ref(instance, -1);
03864 } else {
03865
03866 ast_debug(1, "Failed to tear down RTCP on RTP instance '%p'\n", instance);
03867 return;
03868 }
03869 rtp->rtcp->schedid = -1;
03870 }
03871 close(rtp->rtcp->s);
03872 ast_free(rtp->rtcp);
03873 rtp->rtcp = NULL;
03874 }
03875 return;
03876 }
03877 }
03878
03879 return;
03880 }
03881
03882 static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp)
03883 {
03884 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
03885
03886 return rtcp ? (rtp->rtcp ? rtp->rtcp->s : -1) : rtp->s;
03887 }
03888
03889 static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr)
03890 {
03891 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
03892
03893 if (rtp->rtcp) {
03894 ast_debug(1, "Setting RTCP address on RTP instance '%p'\n", instance);
03895 ast_sockaddr_copy(&rtp->rtcp->them, addr);
03896 if (!ast_sockaddr_isnull(addr)) {
03897 ast_sockaddr_set_port(&rtp->rtcp->them,
03898 ast_sockaddr_port(addr) + 1);
03899 }
03900 }
03901
03902 rtp->rxseqno = 0;
03903
03904 if (strictrtp) {
03905 rtp->strict_rtp_state = STRICT_RTP_LEARN;
03906 rtp_learning_seq_init(rtp, rtp->seqno);
03907 }
03908
03909 return;
03910 }
03911
03912 static void ast_rtp_alt_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr)
03913 {
03914 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
03915
03916
03917
03918
03919 ast_sockaddr_copy(&rtp->alt_rtp_address, addr);
03920
03921 return;
03922 }
03923
03924
03925
03926
03927 static int red_write(const void *data)
03928 {
03929 struct ast_rtp_instance *instance = (struct ast_rtp_instance*) data;
03930 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
03931
03932 ast_rtp_write(instance, &rtp->red->t140);
03933
03934 return 1;
03935 }
03936
03937 static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
03938 {
03939 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
03940 int x;
03941
03942 if (!(rtp->red = ast_calloc(1, sizeof(*rtp->red)))) {
03943 return -1;
03944 }
03945
03946 rtp->red->t140.frametype = AST_FRAME_TEXT;
03947 ast_format_set(&rtp->red->t140.subclass.format, AST_FORMAT_T140RED, 0);
03948 rtp->red->t140.data.ptr = &rtp->red->buf_data;
03949
03950 rtp->red->t140.ts = 0;
03951 rtp->red->t140red = rtp->red->t140;
03952 rtp->red->t140red.data.ptr = &rtp->red->t140red_data;
03953 rtp->red->t140red.datalen = 0;
03954 rtp->red->ti = buffer_time;
03955 rtp->red->num_gen = generations;
03956 rtp->red->hdrlen = generations * 4 + 1;
03957 rtp->red->prev_ts = 0;
03958
03959 for (x = 0; x < generations; x++) {
03960 rtp->red->pt[x] = payloads[x];
03961 rtp->red->pt[x] |= 1 << 7;
03962 rtp->red->t140red_data[x*4] = rtp->red->pt[x];
03963 }
03964 rtp->red->t140red_data[x*4] = rtp->red->pt[x] = payloads[x];
03965 rtp->red->schedid = ast_sched_add(rtp->sched, generations, red_write, instance);
03966
03967 rtp->red->t140.datalen = 0;
03968
03969 return 0;
03970 }
03971
03972 static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
03973 {
03974 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
03975
03976 if (frame->datalen > -1) {
03977 struct rtp_red *red = rtp->red;
03978 memcpy(&red->buf_data[red->t140.datalen], frame->data.ptr, frame->datalen);
03979 red->t140.datalen += frame->datalen;
03980 red->t140.ts = frame->ts;
03981 }
03982
03983 return 0;
03984 }
03985
03986 static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1)
03987 {
03988 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance0);
03989
03990 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
03991
03992 return 0;
03993 }
03994
03995 static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
03996 {
03997 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
03998
03999 if (!rtp->rtcp) {
04000 return -1;
04001 }
04002
04003 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_TXCOUNT, -1, stats->txcount, rtp->txcount);
04004 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RXCOUNT, -1, stats->rxcount, rtp->rxcount);
04005
04006 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_TXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->txploss, rtp->rtcp->reported_lost);
04007 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->rxploss, rtp->rtcp->expected_prior - rtp->rtcp->received_prior);
04008 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MAXRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_maxrxploss, rtp->rtcp->reported_maxlost);
04009 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MINRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_minrxploss, rtp->rtcp->reported_minlost);
04010 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_normdevrxploss, rtp->rtcp->reported_normdev_lost);
04011 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_STDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_stdevrxploss, rtp->rtcp->reported_stdev_lost);
04012 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MAXRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_maxrxploss, rtp->rtcp->maxrxlost);
04013 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MINRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_minrxploss, rtp->rtcp->minrxlost);
04014 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_normdevrxploss, rtp->rtcp->normdev_rxlost);
04015 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_STDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_stdevrxploss, rtp->rtcp->stdev_rxlost);
04016 AST_RTP_STAT_TERMINATOR(AST_RTP_INSTANCE_STAT_COMBINED_LOSS);
04017
04018 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_TXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->txjitter, rtp->rxjitter);
04019 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->rxjitter, rtp->rtcp->reported_jitter / (unsigned int) 65536.0);
04020 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MAXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_maxjitter, rtp->rtcp->reported_maxjitter);
04021 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MINJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_minjitter, rtp->rtcp->reported_minjitter);
04022 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_normdevjitter, rtp->rtcp->reported_normdev_jitter);
04023 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_STDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_stdevjitter, rtp->rtcp->reported_stdev_jitter);
04024 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MAXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_maxjitter, rtp->rtcp->maxrxjitter);
04025 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MINJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_minjitter, rtp->rtcp->minrxjitter);
04026 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_normdevjitter, rtp->rtcp->normdev_rxjitter);
04027 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_STDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_stdevjitter, rtp->rtcp->stdev_rxjitter);
04028 AST_RTP_STAT_TERMINATOR(AST_RTP_INSTANCE_STAT_COMBINED_JITTER);
04029
04030 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->rtt, rtp->rtcp->rtt);
04031 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_MAX_RTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->maxrtt, rtp->rtcp->maxrtt);
04032 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_MIN_RTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->minrtt, rtp->rtcp->minrtt);
04033 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_NORMDEVRTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->normdevrtt, rtp->rtcp->normdevrtt);
04034 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_STDEVRTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->stdevrtt, rtp->rtcp->stdevrtt);
04035 AST_RTP_STAT_TERMINATOR(AST_RTP_INSTANCE_STAT_COMBINED_RTT);
04036
04037 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_SSRC, -1, stats->local_ssrc, rtp->ssrc);
04038 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_SSRC, -1, stats->remote_ssrc, rtp->themssrc);
04039
04040 return 0;
04041 }
04042
04043 static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1)
04044 {
04045
04046
04047
04048
04049
04050
04051
04052
04053
04054
04055 return (((ast_rtp_instance_get_prop(instance0, AST_RTP_PROPERTY_DTMF) != ast_rtp_instance_get_prop(instance1, AST_RTP_PROPERTY_DTMF)) ||
04056 (!ast_channel_tech(chan0)->send_digit_begin != !ast_channel_tech(chan1)->send_digit_begin)) ? 0 : 1);
04057 }
04058
04059 static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username)
04060 {
04061 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
04062 struct sockaddr_in suggestion_tmp;
04063
04064 ast_sockaddr_to_sin(suggestion, &suggestion_tmp);
04065 ast_stun_request(rtp->s, &suggestion_tmp, username, NULL);
04066 ast_sockaddr_from_sin(suggestion, &suggestion_tmp);
04067 }
04068
04069 static void ast_rtp_stop(struct ast_rtp_instance *instance)
04070 {
04071 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
04072 struct ast_sockaddr addr = { {0,} };
04073
04074 #ifdef HAVE_OPENSSL_SRTP
04075 AST_SCHED_DEL_UNREF(rtp->sched, rtp->rekeyid, ao2_ref(instance, -1));
04076 #endif
04077
04078 if (rtp->rtcp && rtp->rtcp->schedid > 0) {
04079 if (!ast_sched_del(rtp->sched, rtp->rtcp->schedid)) {
04080
04081 ao2_ref(instance, -1);
04082 }
04083 rtp->rtcp->schedid = -1;
04084 }
04085
04086 if (rtp->red) {
04087 AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
04088 free(rtp->red);
04089 rtp->red = NULL;
04090 }
04091
04092 ast_rtp_instance_set_remote_address(instance, &addr);
04093 if (rtp->rtcp) {
04094 ast_sockaddr_setnull(&rtp->rtcp->them);
04095 }
04096
04097 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
04098 }
04099
04100 static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
04101 {
04102 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
04103
04104 return ast_set_qos(rtp->s, tos, cos, desc);
04105 }
04106
04107
04108 static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level)
04109 {
04110 unsigned int *rtpheader;
04111 int hdrlen = 12;
04112 int res;
04113 int payload;
04114 char data[256];
04115 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
04116 struct ast_sockaddr remote_address = { {0,} };
04117 int ice;
04118
04119 ast_rtp_instance_get_remote_address(instance, &remote_address);
04120
04121 if (ast_sockaddr_isnull(&remote_address)) {
04122 return -1;
04123 }
04124
04125 payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 0, NULL, AST_RTP_CN);
04126
04127 level = 127 - (level & 0x7f);
04128
04129 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
04130
04131
04132 rtpheader = (unsigned int *)data;
04133 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
04134 rtpheader[1] = htonl(rtp->lastts);
04135 rtpheader[2] = htonl(rtp->ssrc);
04136 data[12] = level;
04137
04138 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 1, 0, &remote_address, &ice);
04139
04140 if (res < 0) {
04141 ast_log(LOG_ERROR, "RTP Comfort Noise Transmission error to %s: %s\n", ast_sockaddr_stringify(&remote_address), strerror(errno));
04142 return res;
04143 }
04144
04145 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
04146
04147 if (rtp_debug_test_addr(&remote_address)) {
04148 ast_verbose("Sent Comfort Noise RTP packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
04149 ast_sockaddr_stringify(&remote_address),
04150 ice ? " (via ICE)" : "",
04151 AST_RTP_CN, rtp->seqno, rtp->lastdigitts, res - hdrlen);
04152 }
04153
04154 return res;
04155 }
04156
04157 #ifdef HAVE_OPENSSL_SRTP
04158 static int ast_rtp_activate(struct ast_rtp_instance *instance)
04159 {
04160 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
04161
04162 if (!rtp->ssl) {
04163 return 0;
04164 }
04165
04166 SSL_do_handshake(rtp->ssl);
04167
04168 dtls_srtp_check_pending(instance, rtp);
04169
04170 return 0;
04171 }
04172 #endif
04173
04174 static char *rtp_do_debug_ip(struct ast_cli_args *a)
04175 {
04176 char *arg = ast_strdupa(a->argv[4]);
04177 char *debughost = NULL;
04178 char *debugport = NULL;
04179
04180 if (!ast_sockaddr_parse(&rtpdebugaddr, arg, 0) || !ast_sockaddr_split_hostport(arg, &debughost, &debugport, 0)) {
04181 ast_cli(a->fd, "Lookup failed for '%s'\n", arg);
04182 return CLI_FAILURE;
04183 }
04184 rtpdebugport = (!ast_strlen_zero(debugport) && debugport[0] != '0');
04185 ast_cli(a->fd, "RTP Debugging Enabled for address: %s\n",
04186 ast_sockaddr_stringify(&rtpdebugaddr));
04187 rtpdebug = 1;
04188 return CLI_SUCCESS;
04189 }
04190
04191 static char *rtcp_do_debug_ip(struct ast_cli_args *a)
04192 {
04193 char *arg = ast_strdupa(a->argv[4]);
04194 char *debughost = NULL;
04195 char *debugport = NULL;
04196
04197 if (!ast_sockaddr_parse(&rtcpdebugaddr, arg, 0) || !ast_sockaddr_split_hostport(arg, &debughost, &debugport, 0)) {
04198 ast_cli(a->fd, "Lookup failed for '%s'\n", arg);
04199 return CLI_FAILURE;
04200 }
04201 rtcpdebugport = (!ast_strlen_zero(debugport) && debugport[0] != '0');
04202 ast_cli(a->fd, "RTCP Debugging Enabled for address: %s\n",
04203 ast_sockaddr_stringify(&rtcpdebugaddr));
04204 rtcpdebug = 1;
04205 return CLI_SUCCESS;
04206 }
04207
04208 static char *handle_cli_rtp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04209 {
04210 switch (cmd) {
04211 case CLI_INIT:
04212 e->command = "rtp set debug {on|off|ip}";
04213 e->usage =
04214 "Usage: rtp set debug {on|off|ip host[:port]}\n"
04215 " Enable/Disable dumping of all RTP packets. If 'ip' is\n"
04216 " specified, limit the dumped packets to those to and from\n"
04217 " the specified 'host' with optional port.\n";
04218 return NULL;
04219 case CLI_GENERATE:
04220 return NULL;
04221 }
04222
04223 if (a->argc == e->args) {
04224 if (!strncasecmp(a->argv[e->args-1], "on", 2)) {
04225 rtpdebug = 1;
04226 memset(&rtpdebugaddr, 0, sizeof(rtpdebugaddr));
04227 ast_cli(a->fd, "RTP Debugging Enabled\n");
04228 return CLI_SUCCESS;
04229 } else if (!strncasecmp(a->argv[e->args-1], "off", 3)) {
04230 rtpdebug = 0;
04231 ast_cli(a->fd, "RTP Debugging Disabled\n");
04232 return CLI_SUCCESS;
04233 }
04234 } else if (a->argc == e->args +1) {
04235 return rtp_do_debug_ip(a);
04236 }
04237
04238 return CLI_SHOWUSAGE;
04239 }
04240
04241 static char *handle_cli_rtcp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04242 {
04243 switch (cmd) {
04244 case CLI_INIT:
04245 e->command = "rtcp set debug {on|off|ip}";
04246 e->usage =
04247 "Usage: rtcp set debug {on|off|ip host[:port]}\n"
04248 " Enable/Disable dumping of all RTCP packets. If 'ip' is\n"
04249 " specified, limit the dumped packets to those to and from\n"
04250 " the specified 'host' with optional port.\n";
04251 return NULL;
04252 case CLI_GENERATE:
04253 return NULL;
04254 }
04255
04256 if (a->argc == e->args) {
04257 if (!strncasecmp(a->argv[e->args-1], "on", 2)) {
04258 rtcpdebug = 1;
04259 memset(&rtcpdebugaddr, 0, sizeof(rtcpdebugaddr));
04260 ast_cli(a->fd, "RTCP Debugging Enabled\n");
04261 return CLI_SUCCESS;
04262 } else if (!strncasecmp(a->argv[e->args-1], "off", 3)) {
04263 rtcpdebug = 0;
04264 ast_cli(a->fd, "RTCP Debugging Disabled\n");
04265 return CLI_SUCCESS;
04266 }
04267 } else if (a->argc == e->args +1) {
04268 return rtcp_do_debug_ip(a);
04269 }
04270
04271 return CLI_SHOWUSAGE;
04272 }
04273
04274 static char *handle_cli_rtcp_set_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04275 {
04276 switch (cmd) {
04277 case CLI_INIT:
04278 e->command = "rtcp set stats {on|off}";
04279 e->usage =
04280 "Usage: rtcp set stats {on|off}\n"
04281 " Enable/Disable dumping of RTCP stats.\n";
04282 return NULL;
04283 case CLI_GENERATE:
04284 return NULL;
04285 }
04286
04287 if (a->argc != e->args)
04288 return CLI_SHOWUSAGE;
04289
04290 if (!strncasecmp(a->argv[e->args-1], "on", 2))
04291 rtcpstats = 1;
04292 else if (!strncasecmp(a->argv[e->args-1], "off", 3))
04293 rtcpstats = 0;
04294 else
04295 return CLI_SHOWUSAGE;
04296
04297 ast_cli(a->fd, "RTCP Stats %s\n", rtcpstats ? "Enabled" : "Disabled");
04298 return CLI_SUCCESS;
04299 }
04300
04301 static struct ast_cli_entry cli_rtp[] = {
04302 AST_CLI_DEFINE(handle_cli_rtp_set_debug, "Enable/Disable RTP debugging"),
04303 AST_CLI_DEFINE(handle_cli_rtcp_set_debug, "Enable/Disable RTCP debugging"),
04304 AST_CLI_DEFINE(handle_cli_rtcp_set_stats, "Enable/Disable RTCP stats"),
04305 };
04306
04307 static int rtp_reload(int reload)
04308 {
04309 struct ast_config *cfg;
04310 const char *s;
04311 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
04312
04313 cfg = ast_config_load2("rtp.conf", "rtp", config_flags);
04314 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
04315 return 0;
04316 }
04317
04318 rtpstart = DEFAULT_RTP_START;
04319 rtpend = DEFAULT_RTP_END;
04320 dtmftimeout = DEFAULT_DTMF_TIMEOUT;
04321 strictrtp = DEFAULT_STRICT_RTP;
04322 learning_min_sequential = DEFAULT_LEARNING_MIN_SEQUENTIAL;
04323
04324
04325
04326
04327
04328
04329
04330
04331
04332 icesupport = DEFAULT_ICESUPPORT;
04333 turnport = DEFAULT_TURN_PORT;
04334 memset(&stunaddr, 0, sizeof(stunaddr));
04335 turnaddr = pj_str(NULL);
04336 turnusername = pj_str(NULL);
04337 turnpassword = pj_str(NULL);
04338
04339 if (cfg) {
04340 if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
04341 rtpstart = atoi(s);
04342 if (rtpstart < MINIMUM_RTP_PORT)
04343 rtpstart = MINIMUM_RTP_PORT;
04344 if (rtpstart > MAXIMUM_RTP_PORT)
04345 rtpstart = MAXIMUM_RTP_PORT;
04346 }
04347 if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
04348 rtpend = atoi(s);
04349 if (rtpend < MINIMUM_RTP_PORT)
04350 rtpend = MINIMUM_RTP_PORT;
04351 if (rtpend > MAXIMUM_RTP_PORT)
04352 rtpend = MAXIMUM_RTP_PORT;
04353 }
04354 if ((s = ast_variable_retrieve(cfg, "general", "rtcpinterval"))) {
04355 rtcpinterval = atoi(s);
04356 if (rtcpinterval == 0)
04357 rtcpinterval = 0;
04358 if (rtcpinterval < RTCP_MIN_INTERVALMS)
04359 rtcpinterval = RTCP_MIN_INTERVALMS;
04360 if (rtcpinterval > RTCP_MAX_INTERVALMS)
04361 rtcpinterval = RTCP_MAX_INTERVALMS;
04362 }
04363 if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
04364 #ifdef SO_NO_CHECK
04365 nochecksums = ast_false(s) ? 1 : 0;
04366 #else
04367 if (ast_false(s))
04368 ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
04369 #endif
04370 }
04371 if ((s = ast_variable_retrieve(cfg, "general", "dtmftimeout"))) {
04372 dtmftimeout = atoi(s);
04373 if ((dtmftimeout < 0) || (dtmftimeout > 64000)) {
04374 ast_log(LOG_WARNING, "DTMF timeout of '%d' outside range, using default of '%d' instead\n",
04375 dtmftimeout, DEFAULT_DTMF_TIMEOUT);
04376 dtmftimeout = DEFAULT_DTMF_TIMEOUT;
04377 };
04378 }
04379 if ((s = ast_variable_retrieve(cfg, "general", "strictrtp"))) {
04380 strictrtp = ast_true(s);
04381 }
04382 if ((s = ast_variable_retrieve(cfg, "general", "probation"))) {
04383 if ((sscanf(s, "%d", &learning_min_sequential) <= 0) || learning_min_sequential <= 0) {
04384 ast_log(LOG_WARNING, "Value for 'probation' could not be read, using default of '%d' instead\n",
04385 DEFAULT_LEARNING_MIN_SEQUENTIAL);
04386 }
04387 }
04388 if ((s = ast_variable_retrieve(cfg, "general", "icesupport"))) {
04389 icesupport = ast_true(s);
04390 }
04391 if ((s = ast_variable_retrieve(cfg, "general", "stunaddr"))) {
04392 stunaddr.sin_port = htons(STANDARD_STUN_PORT);
04393 if (ast_parse_arg(s, PARSE_INADDR, &stunaddr)) {
04394 ast_log(LOG_WARNING, "Invalid STUN server address: %s\n", s);
04395 }
04396 }
04397 if ((s = ast_variable_retrieve(cfg, "general", "turnaddr"))) {
04398 struct sockaddr_in addr;
04399 addr.sin_port = htons(DEFAULT_TURN_PORT);
04400 if (ast_parse_arg(s, PARSE_INADDR, &addr)) {
04401 ast_log(LOG_WARNING, "Invalid TURN server address: %s\n", s);
04402 } else {
04403 pj_strdup2(pool, &turnaddr, ast_inet_ntoa(addr.sin_addr));
04404
04405
04406 turnport = ntohs(addr.sin_port);
04407 }
04408 }
04409 if ((s = ast_variable_retrieve(cfg, "general", "turnusername"))) {
04410 pj_strdup2(pool, &turnusername, s);
04411 }
04412 if ((s = ast_variable_retrieve(cfg, "general", "turnpassword"))) {
04413 pj_strdup2(pool, &turnpassword, s);
04414 }
04415 ast_config_destroy(cfg);
04416 }
04417 if (rtpstart >= rtpend) {
04418 ast_log(LOG_WARNING, "Unreasonable values for RTP start/end port in rtp.conf\n");
04419 rtpstart = DEFAULT_RTP_START;
04420 rtpend = DEFAULT_RTP_END;
04421 }
04422 ast_verb(2, "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
04423 return 0;
04424 }
04425
04426 static int reload_module(void)
04427 {
04428 rtp_reload(1);
04429 return 0;
04430 }
04431
04432 static int load_module(void)
04433 {
04434 pj_lock_t *lock;
04435
04436 pj_log_set_level(0);
04437
04438 if (pj_init() != PJ_SUCCESS) {
04439 return AST_MODULE_LOAD_DECLINE;
04440 }
04441
04442 if (pjlib_util_init() != PJ_SUCCESS) {
04443 pj_shutdown();
04444 return AST_MODULE_LOAD_DECLINE;
04445 }
04446
04447 if (pjnath_init() != PJ_SUCCESS) {
04448 pj_shutdown();
04449 return AST_MODULE_LOAD_DECLINE;
04450 }
04451
04452 pj_caching_pool_init(&cachingpool, &pj_pool_factory_default_policy, 0);
04453
04454 pool = pj_pool_create(&cachingpool.factory, "rtp", 512, 512, NULL);
04455
04456 if (pj_timer_heap_create(pool, 100, &timerheap) != PJ_SUCCESS) {
04457 pj_caching_pool_destroy(&cachingpool);
04458 pj_shutdown();
04459 return AST_MODULE_LOAD_DECLINE;
04460 }
04461
04462 if (pj_lock_create_recursive_mutex(pool, "rtp%p", &lock) != PJ_SUCCESS) {
04463 pj_caching_pool_destroy(&cachingpool);
04464 pj_shutdown();
04465 return AST_MODULE_LOAD_DECLINE;
04466 }
04467
04468 pj_timer_heap_set_lock(timerheap, lock, PJ_TRUE);
04469
04470 if (pj_ioqueue_create(pool, 16, &ioqueue) != PJ_SUCCESS) {
04471 pj_caching_pool_destroy(&cachingpool);
04472 pj_shutdown();
04473 return AST_MODULE_LOAD_DECLINE;
04474 }
04475
04476 if (pj_thread_create(pool, "ice", &ice_worker_thread, NULL, 0, 0, &thread) != PJ_SUCCESS) {
04477 pj_caching_pool_destroy(&cachingpool);
04478 pj_shutdown();
04479 return AST_MODULE_LOAD_DECLINE;
04480 }
04481
04482 if (ast_rtp_engine_register(&asterisk_rtp_engine)) {
04483 worker_terminate = 1;
04484 pj_thread_join(thread);
04485 pj_thread_destroy(thread);
04486 pj_caching_pool_destroy(&cachingpool);
04487 pj_shutdown();
04488 return AST_MODULE_LOAD_DECLINE;
04489 }
04490
04491 if (ast_cli_register_multiple(cli_rtp, ARRAY_LEN(cli_rtp))) {
04492 worker_terminate = 1;
04493 pj_thread_join(thread);
04494 pj_thread_destroy(thread);
04495 ast_rtp_engine_unregister(&asterisk_rtp_engine);
04496 pj_caching_pool_destroy(&cachingpool);
04497 pj_shutdown();
04498 return AST_MODULE_LOAD_DECLINE;
04499 }
04500
04501 rtp_reload(0);
04502
04503 return AST_MODULE_LOAD_SUCCESS;
04504 }
04505
04506 static int unload_module(void)
04507 {
04508 ast_rtp_engine_unregister(&asterisk_rtp_engine);
04509 ast_cli_unregister_multiple(cli_rtp, ARRAY_LEN(cli_rtp));
04510
04511 worker_terminate = 1;
04512
04513 pj_thread_register_check();
04514
04515 pj_thread_join(thread);
04516 pj_thread_destroy(thread);
04517
04518 pj_caching_pool_destroy(&cachingpool);
04519 pj_shutdown();
04520
04521 return 0;
04522 }
04523
04524 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk RTP Stack",
04525 .load = load_module,
04526 .unload = unload_module,
04527 .reload = reload_module,
04528 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
04529 );