| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include "asterisk.h" |
| 38 |
|
| 39 |
#include <arpa/nameser.h> |
| 40 |
#include "asterisk/dns_core.h" |
| 41 |
#include "asterisk/dns_internal.h" |
| 42 |
#include "asterisk/dns_recurring.h" |
| 43 |
|
| 44 |
#include <sys/time.h> |
| 45 |
#include <signal.h> |
| 46 |
#include <fcntl.h> |
| 47 |
#include <math.h> |
| 48 |
|
| 49 |
#ifdef HAVE_OPENSSL |
| 50 |
#include <openssl/opensslconf.h> |
| 51 |
#include <openssl/opensslv.h> |
| 52 |
#if !defined(OPENSSL_NO_SRTP) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) |
| 53 |
#include <openssl/ssl.h> |
| 54 |
#include <openssl/err.h> |
| 55 |
#include <openssl/bio.h> |
| 56 |
#if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) |
| 57 |
#include <openssl/bn.h> |
| 58 |
#endif |
| 59 |
#ifndef OPENSSL_NO_DH |
| 60 |
#include <openssl/dh.h> |
| 61 |
#endif |
| 62 |
#endif |
| 63 |
#endif |
| 64 |
|
| 65 |
#ifdef HAVE_PJPROJECT |
| 66 |
#include <pjlib.h> |
| 67 |
#include <pjlib-util.h> |
| 68 |
#include <pjnath.h> |
| 69 |
#include <ifaddrs.h> |
| 70 |
#endif |
| 71 |
|
| 72 |
#include "asterisk/conversions.h" |
| 73 |
#include "asterisk/options.h" |
| 74 |
#include "asterisk/logger_category.h" |
| 75 |
#include "asterisk/stun.h" |
| 76 |
#include "asterisk/pbx.h" |
| 77 |
#include "asterisk/frame.h" |
| 78 |
#include "asterisk/format_cache.h" |
| 79 |
#include "asterisk/channel.h" |
| 80 |
#include "asterisk/acl.h" |
| 81 |
#include "asterisk/config.h" |
| 82 |
#include "asterisk/lock.h" |
| 83 |
#include "asterisk/utils.h" |
| 84 |
#include "asterisk/cli.h" |
| 85 |
#include "asterisk/manager.h" |
| 86 |
#include "asterisk/unaligned.h" |
| 87 |
#include "asterisk/module.h" |
| 88 |
#include "asterisk/rtp_engine.h" |
| 89 |
#include "asterisk/smoother.h" |
| 90 |
#include "asterisk/uuid.h" |
| 91 |
#include "asterisk/test.h" |
| 92 |
#include "asterisk/data_buffer.h" |
| 93 |
#ifdef HAVE_PJPROJECT |
| 94 |
#include "asterisk/res_pjproject.h" |
| 95 |
#include "asterisk/security_events.h" |
| 96 |
#endif |
| 97 |
|
| 98 |
#define MAX_TIMESTAMP_SKEW 640 |
| 99 |
|
| 100 |
#define RTP_SEQ_MOD (1<<16) |
| 101 |
#define RTCP_DEFAULT_INTERVALMS 5000 |
| 102 |
#define RTCP_MIN_INTERVALMS 500 |
| 103 |
#define RTCP_MAX_INTERVALMS 60000 |
| 104 |
|
| 105 |
#define DEFAULT_RTP_START 5000 |
| 106 |
#define DEFAULT_RTP_END 31000 |
| 107 |
|
| 108 |
#define MINIMUM_RTP_PORT 1024 |
| 109 |
#define MAXIMUM_RTP_PORT 65535 |
| 110 |
|
| 111 |
#define DEFAULT_TURN_PORT 3478 |
| 112 |
|
| 113 |
#define TURN_STATE_WAIT_TIME 2000 |
| 114 |
|
| 115 |
#define DEFAULT_RTP_SEND_BUFFER_SIZE 250 |
| 116 |
#define MAXIMUM_RTP_SEND_BUFFER_SIZE (DEFAULT_RTP_SEND_BUFFER_SIZE + 200) |
| 117 |
#define DEFAULT_RTP_RECV_BUFFER_SIZE 20 |
| 118 |
#define MAXIMUM_RTP_RECV_BUFFER_SIZE (DEFAULT_RTP_RECV_BUFFER_SIZE + 20) |
| 119 |
#define OLD_PACKET_COUNT 1000 |
| 120 |
#define MISSING_SEQNOS_ADDED_TRIGGER 2 |
| 121 |
|
| 122 |
#define SEQNO_CYCLE_OVER 65536 |
| 123 |
|
| 124 |
|
| 125 |
#define RTCP_PT_FUR 192 |
| 126 |
|
| 127 |
#define RTCP_PT_SR AST_RTP_RTCP_SR |
| 128 |
|
| 129 |
#define RTCP_PT_RR AST_RTP_RTCP_RR |
| 130 |
|
| 131 |
#define RTCP_PT_SDES 202 |
| 132 |
|
| 133 |
#define RTCP_PT_BYE 203 |
| 134 |
|
| 135 |
#define RTCP_PT_APP 204 |
| 136 |
|
| 137 |
|
| 138 |
#define RTCP_PT_PSFB AST_RTP_RTCP_PSFB |
| 139 |
|
| 140 |
#define RTP_MTU 1200 |
| 141 |
|
| 142 |
#define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000)) |
| 143 |
|
| 144 |
#define ZFONE_PROFILE_ID 0x505a |
| 145 |
|
| 146 |
#define DEFAULT_LEARNING_MIN_SEQUENTIAL 4 |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
#define CALC_LEARNING_MIN_DURATION(count) (((count) - 1) * 9 - 5) |
| 160 |
#define DEFAULT_LEARNING_MIN_DURATION CALC_LEARNING_MIN_DURATION(DEFAULT_LEARNING_MIN_SEQUENTIAL) |
| 161 |
|
| 162 |
#define SRTP_MASTER_KEY_LEN 16 |
| 163 |
#define SRTP_MASTER_SALT_LEN 14 |
| 164 |
#define SRTP_MASTER_LEN (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN) |
| 165 |
|
| 166 |
#define RTP_DTLS_ESTABLISHED -37 |
| 167 |
|
| 168 |
enum strict_rtp_state { |
| 169 |
STRICT_RTP_OPEN = 0, |
| 170 |
STRICT_RTP_LEARN, |
| 171 |
STRICT_RTP_CLOSED, |
| 172 |
}; |
| 173 |
|
| 174 |
enum strict_rtp_mode { |
| 175 |
STRICT_RTP_NO = 0, |
| 176 |
STRICT_RTP_YES, |
| 177 |
STRICT_RTP_SEQNO, |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
#define STRICT_RTP_LEARN_TIMEOUT 5000 |
| 188 |
|
| 189 |
#define DEFAULT_STRICT_RTP STRICT_RTP_YES |
| 190 |
#define DEFAULT_SRTP_REPLAY_PROTECTION 1 |
| 191 |
#define DEFAULT_ICESUPPORT 1 |
| 192 |
#define DEFAULT_STUN_SOFTWARE_ATTRIBUTE 1 |
| 193 |
#define DEFAULT_DTLS_MTU 1200 |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#define RTP_IGNORE_FIRST_PACKETS_COUNT 15 |
| 204 |
|
| 205 |
extern struct ast_srtp_res *res_srtp; |
| 206 |
extern struct ast_srtp_policy_res *res_srtp_policy; |
| 207 |
|
| 208 |
static int dtmftimeout = DEFAULT_DTMF_TIMEOUT; |
| 209 |
|
| 210 |
static int rtpstart = DEFAULT_RTP_START; |
| 211 |
static int rtpend = DEFAULT_RTP_END; |
| 212 |
static int rtcpstats; |
| 213 |
static int rtcpinterval = RTCP_DEFAULT_INTERVALMS; |
| 214 |
static struct ast_sockaddr rtpdebugaddr; |
| 215 |
static struct ast_sockaddr rtcpdebugaddr; |
| 216 |
static int rtpdebugport; |
| 217 |
static int rtcpdebugport; |
| 218 |
#ifdef SO_NO_CHECK |
| 219 |
static int nochecksums; |
| 220 |
#endif |
| 221 |
static int strictrtp = DEFAULT_STRICT_RTP; |
| 222 |
static int learning_min_sequential = DEFAULT_LEARNING_MIN_SEQUENTIAL; |
| 223 |
static int learning_min_duration = DEFAULT_LEARNING_MIN_DURATION; |
| 224 |
static int srtp_replay_protection = DEFAULT_SRTP_REPLAY_PROTECTION; |
| 225 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 226 |
static int dtls_mtu = DEFAULT_DTLS_MTU; |
| 227 |
#endif |
| 228 |
#ifdef HAVE_PJPROJECT |
| 229 |
static int icesupport = DEFAULT_ICESUPPORT; |
| 230 |
static int stun_software_attribute = DEFAULT_STUN_SOFTWARE_ATTRIBUTE; |
| 231 |
static pj_str_t turnaddr; |
| 232 |
static int turnport = DEFAULT_TURN_PORT; |
| 233 |
static pj_str_t turnusername; |
| 234 |
static pj_str_t turnpassword; |
| 235 |
static struct stasis_subscription *acl_change_sub = NULL; |
| 236 |
static struct ast_sockaddr lo6 = { .len = 0 }; |
| 237 |
|
| 238 |
|
| 239 |
static struct ast_acl_list *ice_acl = NULL; |
| 240 |
static ast_rwlock_t ice_acl_lock = AST_RWLOCK_INIT_VALUE; |
| 241 |
|
| 242 |
|
| 243 |
static struct ast_acl_list *stun_acl = NULL; |
| 244 |
static ast_rwlock_t stun_acl_lock = AST_RWLOCK_INIT_VALUE; |
| 245 |
|
| 246 |
static struct sockaddr_in stunaddr; |
| 247 |
|
| 248 |
static ast_rwlock_t stunaddr_lock = AST_RWLOCK_INIT_VALUE; |
| 249 |
static struct ast_dns_query_recurring *stunaddr_resolver = NULL; |
| 250 |
|
| 251 |
static int stunaddr_ttl = 0; |
| 252 |
|
| 253 |
static char *stun_hostname = NULL; |
| 254 |
|
| 255 |
static int stunaddr_reresolve_ttl_0 = 0; |
| 256 |
|
| 257 |
|
| 258 |
static pj_caching_pool cachingpool; |
| 259 |
|
| 260 |
|
| 261 |
static pj_pool_t *pool; |
| 262 |
|
| 263 |
|
| 264 |
static pj_timer_heap_t *timer_heap; |
| 265 |
|
| 266 |
|
| 267 |
static pj_thread_t *timer_thread; |
| 268 |
|
| 269 |
|
| 270 |
static int timer_terminate; |
| 271 |
|
| 272 |
|
| 273 |
struct ast_rtp_ioqueue_thread { |
| 274 |
|
| 275 |
pj_pool_t *pool; |
| 276 |
|
| 277 |
pj_thread_t *thread; |
| 278 |
|
| 279 |
pj_ioqueue_t *ioqueue; |
| 280 |
|
| 281 |
pj_timer_heap_t *timerheap; |
| 282 |
|
| 283 |
int terminate; |
| 284 |
|
| 285 |
unsigned int count; |
| 286 |
|
| 287 |
AST_LIST_ENTRY(ast_rtp_ioqueue_thread) next; |
| 288 |
}; |
| 289 |
|
| 290 |
|
| 291 |
static AST_LIST_HEAD_STATIC(ioqueues, ast_rtp_ioqueue_thread); |
| 292 |
|
| 293 |
|
| 294 |
struct ast_ice_host_candidate { |
| 295 |
struct ast_sockaddr local; |
| 296 |
struct ast_sockaddr advertised; |
| 297 |
unsigned int include_local; |
| 298 |
AST_RWLIST_ENTRY(ast_ice_host_candidate) next; |
| 299 |
}; |
| 300 |
|
| 301 |
|
| 302 |
static AST_RWLIST_HEAD_STATIC(host_candidates, ast_ice_host_candidate); |
| 303 |
|
| 304 |
static char *generate_random_string(char *buf, size_t size); |
| 305 |
|
| 306 |
#endif |
| 307 |
|
| 308 |
#define FLAG_3389_WARNING (1 << 0) |
| 309 |
#define FLAG_NAT_ACTIVE (3 << 1) |
| 310 |
#define FLAG_NAT_INACTIVE (0 << 1) |
| 311 |
#define FLAG_NAT_INACTIVE_NOWARN (1 << 1) |
| 312 |
#define FLAG_NEED_MARKER_BIT (1 << 3) |
| 313 |
#define FLAG_DTMF_COMPENSATE (1 << 4) |
| 314 |
#define FLAG_REQ_LOCAL_BRIDGE_BIT (1 << 5) |
| 315 |
|
| 316 |
#define TRANSPORT_SOCKET_RTP 0 |
| 317 |
#define TRANSPORT_SOCKET_RTCP 1 |
| 318 |
#define TRANSPORT_TURN_RTP 2 |
| 319 |
#define TRANSPORT_TURN_RTCP 3 |
| 320 |
|
| 321 |
|
| 322 |
struct rtp_learning_info { |
| 323 |
struct ast_sockaddr proposed_address; |
| 324 |
struct timeval start; |
| 325 |
struct timeval received; |
| 326 |
int max_seq; |
| 327 |
int packets; |
| 328 |
|
| 329 |
enum ast_media_type stream_type; |
| 330 |
}; |
| 331 |
|
| 332 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 333 |
struct dtls_details { |
| 334 |
SSL *ssl; |
| 335 |
BIO *read_bio; |
| 336 |
BIO *write_bio; |
| 337 |
enum ast_rtp_dtls_setup dtls_setup; |
| 338 |
enum ast_rtp_dtls_connection connection; |
| 339 |
int timeout_timer; |
| 340 |
}; |
| 341 |
#endif |
| 342 |
|
| 343 |
#ifdef HAVE_PJPROJECT |
| 344 |
|
| 345 |
struct ice_wrap { |
| 346 |
pj_ice_sess *real_ice; |
| 347 |
}; |
| 348 |
#endif |
| 349 |
|
| 350 |
|
| 351 |
struct rtp_ssrc_mapping { |
| 352 |
|
| 353 |
unsigned int ssrc; |
| 354 |
|
| 355 |
unsigned int ssrc_valid; |
| 356 |
|
| 357 |
struct ast_rtp_instance *instance; |
| 358 |
}; |
| 359 |
|
| 360 |
|
| 361 |
struct rtp_transport_wide_cc_packet_statistics { |
| 362 |
|
| 363 |
unsigned int seqno; |
| 364 |
|
| 365 |
struct timeval received; |
| 366 |
|
| 367 |
int delta; |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
struct rtp_transport_wide_cc_statistics { |
| 372 |
|
| 373 |
AST_VECTOR(, struct rtp_transport_wide_cc_packet_statistics) packet_statistics; |
| 374 |
|
| 375 |
unsigned int last_seqno; |
| 376 |
|
| 377 |
unsigned int last_extended_seqno; |
| 378 |
|
| 379 |
unsigned int feedback_count; |
| 380 |
|
| 381 |
unsigned int cycles; |
| 382 |
|
| 383 |
int schedid; |
| 384 |
}; |
| 385 |
|
| 386 |
typedef struct { |
| 387 |
unsigned int ts; |
| 388 |
unsigned char is_set; |
| 389 |
} optional_ts; |
| 390 |
|
| 391 |
|
| 392 |
struct ast_rtp { |
| 393 |
int s; |
| 394 |
|
| 395 |
struct ast_frame f; |
| 396 |
unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET]; |
| 397 |
unsigned int ssrc; |
| 398 |
unsigned int ssrc_orig; |
| 399 |
unsigned char ssrc_saved; |
| 400 |
char cname[AST_UUID_STR_LEN]; |
| 401 |
unsigned int themssrc; |
| 402 |
unsigned int themssrc_valid; |
| 403 |
unsigned int lastts; |
| 404 |
unsigned int lastividtimestamp; |
| 405 |
unsigned int lastovidtimestamp; |
| 406 |
unsigned int lastitexttimestamp; |
| 407 |
unsigned int lastotexttimestamp; |
| 408 |
int prevrxseqno; |
| 409 |
int lastrxseqno; |
| 410 |
int expectedrxseqno; |
| 411 |
AST_VECTOR(, int) missing_seqno; |
| 412 |
int expectedseqno; |
| 413 |
unsigned short seedrxseqno; |
| 414 |
unsigned int rxcount; |
| 415 |
unsigned int rxoctetcount; |
| 416 |
unsigned int txcount; |
| 417 |
unsigned int txoctetcount; |
| 418 |
unsigned int cycles; |
| 419 |
struct ast_format *lasttxformat; |
| 420 |
struct ast_format *lastrxformat; |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
double rxstart; |
| 426 |
double rxstart_stable; |
| 427 |
unsigned int remote_seed_rx_rtp_ts; |
| 428 |
unsigned int remote_seed_rx_rtp_ts_stable; |
| 429 |
unsigned int last_transit_time_samples; |
| 430 |
double rxjitter; |
| 431 |
double rxjitter_samples; |
| 432 |
double rxmes; |
| 433 |
|
| 434 |
|
| 435 |
char resp; |
| 436 |
unsigned int last_seqno; |
| 437 |
optional_ts last_end_timestamp; |
| 438 |
unsigned int dtmf_duration; |
| 439 |
unsigned int dtmf_timeout; |
| 440 |
unsigned int dtmfsamples; |
| 441 |
enum ast_rtp_dtmf_mode dtmfmode; |
| 442 |
unsigned int dtmf_samplerate_ms; |
| 443 |
|
| 444 |
unsigned int lastdigitts; |
| 445 |
char sending_digit; |
| 446 |
char send_digit; |
| 447 |
int send_payload; |
| 448 |
int send_duration; |
| 449 |
unsigned int flags; |
| 450 |
struct timeval rxcore; |
| 451 |
struct timeval txcore; |
| 452 |
|
| 453 |
struct timeval dtmfmute; |
| 454 |
struct ast_smoother *smoother; |
| 455 |
unsigned short seqno; |
| 456 |
struct ast_sched_context *sched; |
| 457 |
struct ast_rtcp *rtcp; |
| 458 |
unsigned int asymmetric_codec; |
| 459 |
|
| 460 |
struct ast_rtp_instance *bundled; |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
struct ast_rtp_instance *owner; |
| 467 |
int stream_num; |
| 468 |
AST_VECTOR(, struct rtp_ssrc_mapping) ssrc_mapping; |
| 469 |
struct ast_sockaddr bind_address; |
| 470 |
|
| 471 |
enum strict_rtp_state strict_rtp_state; |
| 472 |
struct ast_sockaddr strict_rtp_address; |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
struct rtp_learning_info rtp_source_learn; |
| 479 |
|
| 480 |
struct rtp_red *red; |
| 481 |
|
| 482 |
struct ast_data_buffer *send_buffer; |
| 483 |
struct ast_data_buffer *recv_buffer; |
| 484 |
|
| 485 |
struct rtp_transport_wide_cc_statistics transport_wide_cc; |
| 486 |
|
| 487 |
#ifdef HAVE_PJPROJECT |
| 488 |
ast_cond_t cond; |
| 489 |
|
| 490 |
struct ice_wrap *ice; |
| 491 |
enum ast_rtp_ice_role role; |
| 492 |
pj_turn_sock *turn_rtp; |
| 493 |
pj_turn_sock *turn_rtcp; |
| 494 |
pj_turn_state_t turn_state; |
| 495 |
unsigned int passthrough:1; |
| 496 |
unsigned int rtp_passthrough:1; |
| 497 |
unsigned int rtcp_passthrough:1; |
| 498 |
unsigned int ice_port; |
| 499 |
struct ast_sockaddr rtp_loop; |
| 500 |
struct ast_sockaddr rtcp_loop; |
| 501 |
|
| 502 |
struct ast_rtp_ioqueue_thread *ioqueue; |
| 503 |
|
| 504 |
char remote_ufrag[257]; |
| 505 |
char remote_passwd[257]; |
| 506 |
|
| 507 |
char local_ufrag[257]; |
| 508 |
char local_passwd[257]; |
| 509 |
|
| 510 |
struct ao2_container *ice_local_candidates; |
| 511 |
struct ao2_container *ice_active_remote_candidates; |
| 512 |
struct ao2_container *ice_proposed_remote_candidates; |
| 513 |
struct ast_sockaddr ice_original_rtp_addr; |
| 514 |
unsigned int ice_num_components; |
| 515 |
unsigned int ice_media_started:1; |
| 516 |
#endif |
| 517 |
|
| 518 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 519 |
SSL_CTX *ssl_ctx; |
| 520 |
enum ast_rtp_dtls_verify dtls_verify; |
| 521 |
enum ast_srtp_suite suite; |
| 522 |
enum ast_rtp_dtls_hash local_hash; |
| 523 |
char local_fingerprint[160]; |
| 524 |
enum ast_rtp_dtls_hash remote_hash; |
| 525 |
unsigned char remote_fingerprint[EVP_MAX_MD_SIZE]; |
| 526 |
unsigned int rekey; |
| 527 |
int rekeyid; |
| 528 |
struct dtls_details dtls; |
| 529 |
#endif |
| 530 |
}; |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
struct ast_rtcp { |
| 543 |
int rtcp_info; |
| 544 |
int s; |
| 545 |
struct ast_sockaddr us; |
| 546 |
struct ast_sockaddr them; |
| 547 |
unsigned int soc; |
| 548 |
unsigned int spc; |
| 549 |
unsigned int themrxlsr; |
| 550 |
struct timeval rxlsr; |
| 551 |
struct timeval txlsr; |
| 552 |
unsigned int expected_prior; |
| 553 |
unsigned int received_prior; |
| 554 |
int schedid; |
| 555 |
unsigned int rr_count; |
| 556 |
unsigned int sr_count; |
| 557 |
unsigned int lastsrtxcount; |
| 558 |
double accumulated_transit; |
| 559 |
double rtt; |
| 560 |
double reported_jitter; |
| 561 |
unsigned int reported_lost; |
| 562 |
|
| 563 |
double reported_maxjitter; |
| 564 |
double reported_minjitter; |
| 565 |
double reported_normdev_jitter; |
| 566 |
double reported_stdev_jitter; |
| 567 |
unsigned int reported_jitter_count; |
| 568 |
|
| 569 |
double reported_maxlost; |
| 570 |
double reported_minlost; |
| 571 |
double reported_normdev_lost; |
| 572 |
double reported_stdev_lost; |
| 573 |
unsigned int reported_lost_count; |
| 574 |
|
| 575 |
double rxlost; |
| 576 |
double maxrxlost; |
| 577 |
double minrxlost; |
| 578 |
double normdev_rxlost; |
| 579 |
double stdev_rxlost; |
| 580 |
unsigned int rxlost_count; |
| 581 |
|
| 582 |
double maxrxjitter; |
| 583 |
double minrxjitter; |
| 584 |
double normdev_rxjitter; |
| 585 |
double stdev_rxjitter; |
| 586 |
unsigned int rxjitter_count; |
| 587 |
|
| 588 |
double maxrtt; |
| 589 |
double minrtt; |
| 590 |
double normdevrtt; |
| 591 |
double stdevrtt; |
| 592 |
unsigned int rtt_count; |
| 593 |
|
| 594 |
double reported_mes; |
| 595 |
double reported_maxmes; |
| 596 |
double reported_minmes; |
| 597 |
double reported_normdev_mes; |
| 598 |
double reported_stdev_mes; |
| 599 |
unsigned int reported_mes_count; |
| 600 |
|
| 601 |
double maxrxmes; |
| 602 |
double minrxmes; |
| 603 |
double normdev_rxmes; |
| 604 |
double stdev_rxmes; |
| 605 |
unsigned int rxmes_count; |
| 606 |
|
| 607 |
|
| 608 |
int firseq; |
| 609 |
|
| 610 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 611 |
struct dtls_details dtls; |
| 612 |
#endif |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
char *local_addr_str; |
| 619 |
enum ast_rtp_instance_rtcp type; |
| 620 |
|
| 621 |
unsigned char frame_buf[512 + AST_FRIENDLY_OFFSET]; |
| 622 |
}; |
| 623 |
|
| 624 |
struct rtp_red { |
| 625 |
struct ast_frame t140; |
| 626 |
struct ast_frame t140red; |
| 627 |
unsigned char pt[AST_RED_MAX_GENERATION]; |
| 628 |
unsigned char ts[AST_RED_MAX_GENERATION]; |
| 629 |
unsigned char len[AST_RED_MAX_GENERATION]; |
| 630 |
int num_gen; |
| 631 |
int schedid; |
| 632 |
unsigned char t140red_data[64000]; |
| 633 |
unsigned char buf_data[64000]; |
| 634 |
int hdrlen; |
| 635 |
long int prev_ts; |
| 636 |
}; |
| 637 |
|
| 638 |
|
| 639 |
struct ast_rtp_rtcp_nack_payload { |
| 640 |
size_t size; |
| 641 |
unsigned char buf[0]; |
| 642 |
}; |
| 643 |
|
| 644 |
AST_LIST_HEAD_NOLOCK(frame_list, ast_frame); |
| 645 |
|
| 646 |
|
| 647 |
static int ast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data); |
| 648 |
static int ast_rtp_destroy(struct ast_rtp_instance *instance); |
| 649 |
static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit); |
| 650 |
static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit); |
| 651 |
static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration); |
| 652 |
static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode); |
| 653 |
static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance); |
| 654 |
static void ast_rtp_update_source(struct ast_rtp_instance *instance); |
| 655 |
static void ast_rtp_change_source(struct ast_rtp_instance *instance); |
| 656 |
static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame); |
| 657 |
static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp); |
| 658 |
static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value); |
| 659 |
static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp); |
| 660 |
static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr); |
| 661 |
static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations); |
| 662 |
static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame); |
| 663 |
static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1); |
| 664 |
static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat); |
| 665 |
static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1); |
| 666 |
static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username); |
| 667 |
static void ast_rtp_stop(struct ast_rtp_instance *instance); |
| 668 |
static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char* desc); |
| 669 |
static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level); |
| 670 |
static unsigned int ast_rtp_get_ssrc(struct ast_rtp_instance *instance); |
| 671 |
static const char *ast_rtp_get_cname(struct ast_rtp_instance *instance); |
| 672 |
static void ast_rtp_set_remote_ssrc(struct ast_rtp_instance *instance, unsigned int ssrc); |
| 673 |
static void ast_rtp_set_stream_num(struct ast_rtp_instance *instance, int stream_num); |
| 674 |
static int ast_rtp_extension_enable(struct ast_rtp_instance *instance, enum ast_rtp_extension extension); |
| 675 |
static int ast_rtp_bundle(struct ast_rtp_instance *child, struct ast_rtp_instance *parent); |
| 676 |
static void update_reported_mes_stats(struct ast_rtp *rtp); |
| 677 |
static void update_local_mes_stats(struct ast_rtp *rtp); |
| 678 |
|
| 679 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 680 |
static int ast_rtp_activate(struct ast_rtp_instance *instance); |
| 681 |
static void dtls_srtp_start_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp); |
| 682 |
static void dtls_srtp_stop_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp); |
| 683 |
static int dtls_bio_write(BIO *bio, const char *buf, int len); |
| 684 |
static long dtls_bio_ctrl(BIO *bio, int cmd, long arg1, void *arg2); |
| 685 |
static int dtls_bio_new(BIO *bio); |
| 686 |
static int dtls_bio_free(BIO *bio); |
| 687 |
|
| 688 |
#ifndef HAVE_OPENSSL_BIO_METHOD |
| 689 |
static BIO_METHOD dtls_bio_methods = { |
| 690 |
.type = BIO_TYPE_BIO, |
| 691 |
.name = "rtp write", |
| 692 |
.bwrite = dtls_bio_write, |
| 693 |
.ctrl = dtls_bio_ctrl, |
| 694 |
.create = dtls_bio_new, |
| 695 |
.destroy = dtls_bio_free, |
| 696 |
}; |
| 697 |
#else |
| 698 |
static BIO_METHOD *dtls_bio_methods; |
| 699 |
#endif |
| 700 |
#endif |
| 701 |
|
| 702 |
static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *via_ice, int use_srtp); |
| 703 |
|
| 704 |
#ifdef HAVE_PJPROJECT |
| 705 |
static void stunaddr_resolve_callback(const struct ast_dns_query *query); |
| 706 |
static int store_stunaddr_resolved(const char *name, const struct ast_dns_result *result, int lock); |
| 707 |
#endif |
| 708 |
|
| 709 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 710 |
static int dtls_bio_new(BIO *bio) |
| 711 |
{ |
| 712 |
#ifdef HAVE_OPENSSL_BIO_METHOD |
| 713 |
BIO_set_init(bio, 1); |
| 714 |
BIO_set_data(bio, NULL); |
| 715 |
BIO_set_shutdown(bio, 0); |
| 716 |
#else |
| 717 |
bio->init = 1; |
| 718 |
bio->ptr = NULL; |
| 719 |
bio->flags = 0; |
| 720 |
#endif |
| 721 |
return 1; |
| 722 |
} |
| 723 |
|
| 724 |
static int dtls_bio_free(BIO *bio) |
| 725 |
{ |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
#ifdef HAVE_OPENSSL_BIO_METHOD |
| 731 |
BIO_set_data(bio, NULL); |
| 732 |
#else |
| 733 |
bio->ptr = NULL; |
| 734 |
#endif |
| 735 |
return 1; |
| 736 |
} |
| 737 |
|
| 738 |
static int dtls_bio_write(BIO *bio, const char *buf, int len) |
| 739 |
{ |
| 740 |
#ifdef HAVE_OPENSSL_BIO_METHOD |
| 741 |
struct ast_rtp_instance *instance = BIO_get_data(bio); |
| 742 |
#else |
| 743 |
struct ast_rtp_instance *instance = bio->ptr; |
| 744 |
#endif |
| 745 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 746 |
int rtcp = 0; |
| 747 |
struct ast_sockaddr remote_address = { {0, } }; |
| 748 |
int ice; |
| 749 |
int bytes_sent; |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
if (rtp->rtcp && rtp->rtcp->dtls.write_bio == bio) { |
| 756 |
rtcp = 1; |
| 757 |
ast_sockaddr_copy(&remote_address, &rtp->rtcp->them); |
| 758 |
} else { |
| 759 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 760 |
} |
| 761 |
|
| 762 |
if (ast_sockaddr_isnull(&remote_address)) { |
| 763 |
return len; |
| 764 |
} |
| 765 |
|
| 766 |
bytes_sent = __rtp_sendto(instance, (char *)buf, len, 0, &remote_address, rtcp, &ice, 0); |
| 767 |
|
| 768 |
if (bytes_sent > 0 && ast_debug_dtls_packet_is_allowed) { |
| 769 |
ast_debug(0, "(%p) DTLS - sent %s packet to %s%s (len %-6.6d)\n", |
| 770 |
instance, rtcp ? "RTCP" : "RTP", ast_sockaddr_stringify(&remote_address), |
| 771 |
ice ? " (via ICE)" : "", bytes_sent); |
| 772 |
} |
| 773 |
|
| 774 |
return len; |
| 775 |
} |
| 776 |
|
| 777 |
static long dtls_bio_ctrl(BIO *bio, int cmd, long arg1, void *arg2) |
| 778 |
{ |
| 779 |
switch (cmd) { |
| 780 |
case BIO_CTRL_FLUSH: |
| 781 |
return 1; |
| 782 |
case BIO_CTRL_DGRAM_QUERY_MTU: |
| 783 |
return dtls_mtu; |
| 784 |
case BIO_CTRL_WPENDING: |
| 785 |
case BIO_CTRL_PENDING: |
| 786 |
return 0L; |
| 787 |
default: |
| 788 |
return 0; |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
#endif |
| 793 |
|
| 794 |
#ifdef HAVE_PJPROJECT |
| 795 |
|
| 796 |
static void host_candidate_overrides_clear(void) |
| 797 |
{ |
| 798 |
struct ast_ice_host_candidate *candidate; |
| 799 |
|
| 800 |
AST_RWLIST_WRLOCK(&host_candidates); |
| 801 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&host_candidates, candidate, next) { |
| 802 |
AST_RWLIST_REMOVE_CURRENT(next); |
| 803 |
ast_free(candidate); |
| 804 |
} |
| 805 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 806 |
AST_RWLIST_UNLOCK(&host_candidates); |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
static void update_address_with_ice_candidate(pj_ice_sess *ice, enum ast_rtp_ice_component_type component, |
| 811 |
struct ast_sockaddr *cand_address) |
| 812 |
{ |
| 813 |
char address[PJ_INET6_ADDRSTRLEN]; |
| 814 |
|
| 815 |
if (component < 1 || !ice->comp[component - 1].valid_check) { |
| 816 |
return; |
| 817 |
} |
| 818 |
|
| 819 |
ast_sockaddr_parse(cand_address, |
| 820 |
pj_sockaddr_print(&ice->comp[component - 1].valid_check->rcand->addr, address, |
| 821 |
sizeof(address), 0), 0); |
| 822 |
ast_sockaddr_set_port(cand_address, |
| 823 |
pj_sockaddr_get_port(&ice->comp[component - 1].valid_check->rcand->addr)); |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
static void ast_rtp_ice_candidate_destroy(void *obj) |
| 828 |
{ |
| 829 |
struct ast_rtp_engine_ice_candidate *candidate = obj; |
| 830 |
|
| 831 |
if (candidate->foundation) { |
| 832 |
ast_free(candidate->foundation); |
| 833 |
} |
| 834 |
|
| 835 |
if (candidate->transport) { |
| 836 |
ast_free(candidate->transport); |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
static void ast_rtp_ice_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password) |
| 842 |
{ |
| 843 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 844 |
int ice_attrb_reset = 0; |
| 845 |
|
| 846 |
if (!ast_strlen_zero(ufrag)) { |
| 847 |
if (!ast_strlen_zero(rtp->remote_ufrag) && strcmp(ufrag, rtp->remote_ufrag)) { |
| 848 |
ice_attrb_reset = 1; |
| 849 |
} |
| 850 |
ast_copy_string(rtp->remote_ufrag, ufrag, sizeof(rtp->remote_ufrag)); |
| 851 |
} |
| 852 |
|
| 853 |
if (!ast_strlen_zero(password)) { |
| 854 |
if (!ast_strlen_zero(rtp->remote_passwd) && strcmp(password, rtp->remote_passwd)) { |
| 855 |
ice_attrb_reset = 1; |
| 856 |
} |
| 857 |
ast_copy_string(rtp->remote_passwd, password, sizeof(rtp->remote_passwd)); |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
if (ice_attrb_reset) { |
| 862 |
generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag)); |
| 863 |
generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd)); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
static int ice_candidate_cmp(void *obj, void *arg, int flags) |
| 868 |
{ |
| 869 |
struct ast_rtp_engine_ice_candidate *candidate1 = obj, *candidate2 = arg; |
| 870 |
|
| 871 |
if (strcmp(candidate1->foundation, candidate2->foundation) || |
| 872 |
candidate1->id != candidate2->id || |
| 873 |
candidate1->type != candidate2->type || |
| 874 |
ast_sockaddr_cmp(&candidate1->address, &candidate2->address)) { |
| 875 |
return 0; |
| 876 |
} |
| 877 |
|
| 878 |
return CMP_MATCH | CMP_STOP; |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
static void ast_rtp_ice_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate) |
| 883 |
{ |
| 884 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 885 |
struct ast_rtp_engine_ice_candidate *remote_candidate; |
| 886 |
|
| 887 |
|
| 888 |
if (strcasecmp(candidate->transport, "udp")) { |
| 889 |
return; |
| 890 |
} |
| 891 |
|
| 892 |
if (!rtp->ice_proposed_remote_candidates) { |
| 893 |
rtp->ice_proposed_remote_candidates = ao2_container_alloc_list( |
| 894 |
AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, ice_candidate_cmp); |
| 895 |
if (!rtp->ice_proposed_remote_candidates) { |
| 896 |
return; |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
if (ao2_container_count(rtp->ice_proposed_remote_candidates) == PJ_ICE_MAX_CAND) { |
| 902 |
return; |
| 903 |
} |
| 904 |
|
| 905 |
if (!(remote_candidate = ao2_alloc(sizeof(*remote_candidate), ast_rtp_ice_candidate_destroy))) { |
| 906 |
return; |
| 907 |
} |
| 908 |
|
| 909 |
remote_candidate->foundation = ast_strdup(candidate->foundation); |
| 910 |
remote_candidate->id = candidate->id; |
| 911 |
remote_candidate->transport = ast_strdup(candidate->transport); |
| 912 |
remote_candidate->priority = candidate->priority; |
| 913 |
ast_sockaddr_copy(&remote_candidate->address, &candidate->address); |
| 914 |
ast_sockaddr_copy(&remote_candidate->relay_address, &candidate->relay_address); |
| 915 |
remote_candidate->type = candidate->type; |
| 916 |
|
| 917 |
ast_debug_ice(2, "(%p) ICE add remote candidate\n", instance); |
| 918 |
|
| 919 |
ao2_link(rtp->ice_proposed_remote_candidates, remote_candidate); |
| 920 |
ao2_ref(remote_candidate, -1); |
| 921 |
} |
| 922 |
|
| 923 |
AST_THREADSTORAGE(pj_thread_storage); |
| 924 |
|
| 925 |
|
| 926 |
static void pj_thread_register_check(void) |
| 927 |
{ |
| 928 |
pj_thread_desc *desc; |
| 929 |
pj_thread_t *thread; |
| 930 |
|
| 931 |
if (pj_thread_is_registered() == PJ_TRUE) { |
| 932 |
return; |
| 933 |
} |
| 934 |
|
| 935 |
desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc)); |
| 936 |
if (!desc) { |
| 937 |
ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage. Expect awful things to occur\n"); |
| 938 |
return; |
| 939 |
} |
| 940 |
pj_bzero(*desc, sizeof(*desc)); |
| 941 |
|
| 942 |
if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) { |
| 943 |
ast_log(LOG_ERROR, "Coudln't register thread with PJLIB.\n"); |
| 944 |
} |
| 945 |
return; |
| 946 |
} |
| 947 |
|
| 948 |
static int ice_create(struct ast_rtp_instance *instance, struct ast_sockaddr *addr, |
| 949 |
int port, int replace); |
| 950 |
|
| 951 |
|
| 952 |
static void ast_rtp_ice_stop(struct ast_rtp_instance *instance) |
| 953 |
{ |
| 954 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 955 |
struct ice_wrap *ice; |
| 956 |
|
| 957 |
ice = rtp->ice; |
| 958 |
rtp->ice = NULL; |
| 959 |
if (ice) { |
| 960 |
|
| 961 |
ao2_unlock(instance); |
| 962 |
ao2_ref(ice, -1); |
| 963 |
ao2_lock(instance); |
| 964 |
ast_debug_ice(2, "(%p) ICE stopped\n", instance); |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
static void ice_wrap_dtor(void *vdoomed) |
| 978 |
{ |
| 979 |
struct ice_wrap *ice = vdoomed; |
| 980 |
|
| 981 |
if (ice->real_ice) { |
| 982 |
pj_thread_register_check(); |
| 983 |
|
| 984 |
pj_ice_sess_destroy(ice->real_ice); |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
static void ast2pj_rtp_ice_role(enum ast_rtp_ice_role ast_role, enum pj_ice_sess_role *pj_role) |
| 989 |
{ |
| 990 |
switch (ast_role) { |
| 991 |
case AST_RTP_ICE_ROLE_CONTROLLED: |
| 992 |
*pj_role = PJ_ICE_SESS_ROLE_CONTROLLED; |
| 993 |
break; |
| 994 |
case AST_RTP_ICE_ROLE_CONTROLLING: |
| 995 |
*pj_role = PJ_ICE_SESS_ROLE_CONTROLLING; |
| 996 |
break; |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
static void pj2ast_rtp_ice_role(enum pj_ice_sess_role pj_role, enum ast_rtp_ice_role *ast_role) |
| 1001 |
{ |
| 1002 |
switch (pj_role) { |
| 1003 |
case PJ_ICE_SESS_ROLE_CONTROLLED: |
| 1004 |
*ast_role = AST_RTP_ICE_ROLE_CONTROLLED; |
| 1005 |
return; |
| 1006 |
case PJ_ICE_SESS_ROLE_CONTROLLING: |
| 1007 |
*ast_role = AST_RTP_ICE_ROLE_CONTROLLING; |
| 1008 |
return; |
| 1009 |
case PJ_ICE_SESS_ROLE_UNKNOWN: |
| 1010 |
|
| 1011 |
return; |
| 1012 |
default: |
| 1013 |
|
| 1014 |
ast_assert(0); |
| 1015 |
return; |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
static int ice_reset_session(struct ast_rtp_instance *instance) |
| 1021 |
{ |
| 1022 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1023 |
int res; |
| 1024 |
|
| 1025 |
ast_debug_ice(3, "(%p) ICE resetting\n", instance); |
| 1026 |
if (!rtp->ice->real_ice->is_nominating && !rtp->ice->real_ice->is_complete) { |
| 1027 |
ast_debug_ice(3, " (%p) ICE nevermind, not ready for a reset\n", instance); |
| 1028 |
return 0; |
| 1029 |
} |
| 1030 |
|
| 1031 |
ast_debug_ice(3, "(%p) ICE recreating ICE session %s (%d)\n", |
| 1032 |
instance, ast_sockaddr_stringify(&rtp->ice_original_rtp_addr), rtp->ice_port); |
| 1033 |
res = ice_create(instance, &rtp->ice_original_rtp_addr, rtp->ice_port, 1); |
| 1034 |
if (!res) { |
| 1035 |
|
| 1036 |
enum pj_ice_sess_role role = PJ_ICE_SESS_ROLE_UNKNOWN; |
| 1037 |
ast2pj_rtp_ice_role(rtp->role, &role); |
| 1038 |
pj_ice_sess_change_role(rtp->ice->real_ice, role); |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
if (rtp->ice_num_components == 1 && rtp->turn_rtcp) { |
| 1045 |
struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_STATE_WAIT_TIME, 1000)); |
| 1046 |
struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, }; |
| 1047 |
|
| 1048 |
rtp->turn_state = PJ_TURN_STATE_NULL; |
| 1049 |
|
| 1050 |
|
| 1051 |
ao2_unlock(instance); |
| 1052 |
pj_turn_sock_destroy(rtp->turn_rtcp); |
| 1053 |
ao2_lock(instance); |
| 1054 |
while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) { |
| 1055 |
ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
rtp->ice_media_started = 0; |
| 1060 |
|
| 1061 |
return res; |
| 1062 |
} |
| 1063 |
|
| 1064 |
static int ice_candidates_compare(struct ao2_container *left, struct ao2_container *right) |
| 1065 |
{ |
| 1066 |
struct ao2_iterator i; |
| 1067 |
struct ast_rtp_engine_ice_candidate *right_candidate; |
| 1068 |
|
| 1069 |
if (ao2_container_count(left) != ao2_container_count(right)) { |
| 1070 |
return -1; |
| 1071 |
} |
| 1072 |
|
| 1073 |
i = ao2_iterator_init(right, 0); |
| 1074 |
while ((right_candidate = ao2_iterator_next(&i))) { |
| 1075 |
struct ast_rtp_engine_ice_candidate *left_candidate = ao2_find(left, right_candidate, OBJ_POINTER); |
| 1076 |
|
| 1077 |
if (!left_candidate) { |
| 1078 |
ao2_ref(right_candidate, -1); |
| 1079 |
ao2_iterator_destroy(&i); |
| 1080 |
return -1; |
| 1081 |
} |
| 1082 |
|
| 1083 |
ao2_ref(left_candidate, -1); |
| 1084 |
ao2_ref(right_candidate, -1); |
| 1085 |
} |
| 1086 |
ao2_iterator_destroy(&i); |
| 1087 |
|
| 1088 |
return 0; |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
static void ast_rtp_ice_start(struct ast_rtp_instance *instance) |
| 1093 |
{ |
| 1094 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1095 |
pj_str_t ufrag = pj_str(rtp->remote_ufrag), passwd = pj_str(rtp->remote_passwd); |
| 1096 |
pj_ice_sess_cand candidates[PJ_ICE_MAX_CAND]; |
| 1097 |
struct ao2_iterator i; |
| 1098 |
struct ast_rtp_engine_ice_candidate *candidate; |
| 1099 |
int cand_cnt = 0, has_rtp = 0, has_rtcp = 0; |
| 1100 |
|
| 1101 |
if (!rtp->ice || !rtp->ice_proposed_remote_candidates) { |
| 1102 |
return; |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
if (rtp->ice_active_remote_candidates && |
| 1107 |
!ice_candidates_compare(rtp->ice_proposed_remote_candidates, rtp->ice_active_remote_candidates)) { |
| 1108 |
ast_debug_ice(2, "(%p) ICE proposed equals active candidates\n", instance); |
| 1109 |
ao2_cleanup(rtp->ice_proposed_remote_candidates); |
| 1110 |
rtp->ice_proposed_remote_candidates = NULL; |
| 1111 |
|
| 1112 |
pj2ast_rtp_ice_role(rtp->ice->real_ice->role, &rtp->role); |
| 1113 |
return; |
| 1114 |
} |
| 1115 |
|
| 1116 |
|
| 1117 |
ao2_cleanup(rtp->ice_active_remote_candidates); |
| 1118 |
rtp->ice_active_remote_candidates = rtp->ice_proposed_remote_candidates; |
| 1119 |
rtp->ice_proposed_remote_candidates = NULL; |
| 1120 |
|
| 1121 |
ast_debug_ice(2, "(%p) ICE start\n", instance); |
| 1122 |
|
| 1123 |
|
| 1124 |
if (ice_reset_session(instance)) { |
| 1125 |
ast_log(LOG_NOTICE, "(%p) ICE failed to create replacement session\n", instance); |
| 1126 |
return; |
| 1127 |
} |
| 1128 |
|
| 1129 |
pj_thread_register_check(); |
| 1130 |
|
| 1131 |
i = ao2_iterator_init(rtp->ice_active_remote_candidates, 0); |
| 1132 |
|
| 1133 |
while ((candidate = ao2_iterator_next(&i)) && (cand_cnt < PJ_ICE_MAX_CAND)) { |
| 1134 |
pj_str_t address; |
| 1135 |
|
| 1136 |
|
| 1137 |
has_rtp |= candidate->id == AST_RTP_ICE_COMPONENT_RTP; |
| 1138 |
has_rtcp |= candidate->id == AST_RTP_ICE_COMPONENT_RTCP; |
| 1139 |
|
| 1140 |
pj_strdup2(rtp->ice->real_ice->pool, &candidates[cand_cnt].foundation, |
| 1141 |
candidate->foundation); |
| 1142 |
candidates[cand_cnt].comp_id = candidate->id; |
| 1143 |
candidates[cand_cnt].prio = candidate->priority; |
| 1144 |
|
| 1145 |
pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->address)), &candidates[cand_cnt].addr); |
| 1146 |
|
| 1147 |
if (!ast_sockaddr_isnull(&candidate->relay_address)) { |
| 1148 |
pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->relay_address)), &candidates[cand_cnt].rel_addr); |
| 1149 |
} |
| 1150 |
|
| 1151 |
if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) { |
| 1152 |
candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_HOST; |
| 1153 |
} else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) { |
| 1154 |
candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_SRFLX; |
| 1155 |
} else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_RELAYED) { |
| 1156 |
candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_RELAYED; |
| 1157 |
} |
| 1158 |
|
| 1159 |
if (candidate->id == AST_RTP_ICE_COMPONENT_RTP && rtp->turn_rtp) { |
| 1160 |
ast_debug_ice(2, "(%p) ICE RTP candidate %s\n", instance, ast_sockaddr_stringify(&candidate->address)); |
| 1161 |
|
| 1162 |
ao2_unlock(instance); |
| 1163 |
pj_turn_sock_set_perm(rtp->turn_rtp, 1, &candidates[cand_cnt].addr, 1); |
| 1164 |
ao2_lock(instance); |
| 1165 |
} else if (candidate->id == AST_RTP_ICE_COMPONENT_RTCP && rtp->turn_rtcp) { |
| 1166 |
ast_debug_ice(2, "(%p) ICE RTCP candidate %s\n", instance, ast_sockaddr_stringify(&candidate->address)); |
| 1167 |
|
| 1168 |
ao2_unlock(instance); |
| 1169 |
pj_turn_sock_set_perm(rtp->turn_rtcp, 1, &candidates[cand_cnt].addr, 1); |
| 1170 |
ao2_lock(instance); |
| 1171 |
} |
| 1172 |
|
| 1173 |
cand_cnt++; |
| 1174 |
ao2_ref(candidate, -1); |
| 1175 |
} |
| 1176 |
|
| 1177 |
ao2_iterator_destroy(&i); |
| 1178 |
|
| 1179 |
if (cand_cnt < ao2_container_count(rtp->ice_active_remote_candidates)) { |
| 1180 |
ast_log(LOG_WARNING, "(%p) ICE lost %d candidates. Consider increasing PJ_ICE_MAX_CAND in PJSIP\n", |
| 1181 |
instance, ao2_container_count(rtp->ice_active_remote_candidates) - cand_cnt); |
| 1182 |
} |
| 1183 |
|
| 1184 |
if (!has_rtp) { |
| 1185 |
ast_log(LOG_WARNING, "(%p) ICE no RTP candidates; skipping checklist\n", instance); |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
if (!has_rtcp && rtp->ice_num_components > 1) { |
| 1190 |
ast_log(LOG_WARNING, "(%p) ICE no RTCP candidates; skipping checklist\n", instance); |
| 1191 |
} |
| 1192 |
|
| 1193 |
if (rtp->ice && has_rtp && (has_rtcp || rtp->ice_num_components == 1)) { |
| 1194 |
pj_status_t res; |
| 1195 |
char reason[80]; |
| 1196 |
struct ice_wrap *ice; |
| 1197 |
|
| 1198 |
|
| 1199 |
ice = rtp->ice; |
| 1200 |
ao2_ref(ice, +1); |
| 1201 |
ao2_unlock(instance); |
| 1202 |
res = pj_ice_sess_create_check_list(ice->real_ice, &ufrag, &passwd, cand_cnt, &candidates[0]); |
| 1203 |
if (res == PJ_SUCCESS) { |
| 1204 |
ast_debug_ice(2, "(%p) ICE successfully created checklist\n", instance); |
| 1205 |
ast_test_suite_event_notify("ICECHECKLISTCREATE", "Result: SUCCESS"); |
| 1206 |
pj_ice_sess_start_check(ice->real_ice); |
| 1207 |
pj_timer_heap_poll(timer_heap, NULL); |
| 1208 |
ao2_ref(ice, -1); |
| 1209 |
ao2_lock(instance); |
| 1210 |
rtp->strict_rtp_state = STRICT_RTP_OPEN; |
| 1211 |
return; |
| 1212 |
} |
| 1213 |
ao2_ref(ice, -1); |
| 1214 |
ao2_lock(instance); |
| 1215 |
|
| 1216 |
pj_strerror(res, reason, sizeof(reason)); |
| 1217 |
ast_log(LOG_WARNING, "(%p) ICE failed to create session check list: %s\n", instance, reason); |
| 1218 |
} |
| 1219 |
|
| 1220 |
ast_test_suite_event_notify("ICECHECKLISTCREATE", "Result: FAILURE"); |
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
ao2_ref(rtp->ice_active_remote_candidates, -1); |
| 1227 |
rtp->ice_active_remote_candidates = NULL; |
| 1228 |
if (rtp->ice) { |
| 1229 |
rtp->ice->real_ice->rcand_cnt = rtp->ice->real_ice->clist.count = 0; |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
|
| 1234 |
static const char *ast_rtp_ice_get_ufrag(struct ast_rtp_instance *instance) |
| 1235 |
{ |
| 1236 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1237 |
|
| 1238 |
return rtp->local_ufrag; |
| 1239 |
} |
| 1240 |
|
| 1241 |
|
| 1242 |
static const char *ast_rtp_ice_get_password(struct ast_rtp_instance *instance) |
| 1243 |
{ |
| 1244 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1245 |
|
| 1246 |
return rtp->local_passwd; |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
static struct ao2_container *ast_rtp_ice_get_local_candidates(struct ast_rtp_instance *instance) |
| 1251 |
{ |
| 1252 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1253 |
|
| 1254 |
if (rtp->ice_local_candidates) { |
| 1255 |
ao2_ref(rtp->ice_local_candidates, +1); |
| 1256 |
} |
| 1257 |
|
| 1258 |
return rtp->ice_local_candidates; |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
static void ast_rtp_ice_lite(struct ast_rtp_instance *instance) |
| 1263 |
{ |
| 1264 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1265 |
|
| 1266 |
if (!rtp->ice) { |
| 1267 |
return; |
| 1268 |
} |
| 1269 |
|
| 1270 |
pj_thread_register_check(); |
| 1271 |
|
| 1272 |
pj_ice_sess_change_role(rtp->ice->real_ice, PJ_ICE_SESS_ROLE_CONTROLLING); |
| 1273 |
} |
| 1274 |
|
| 1275 |
|
| 1276 |
static void ast_rtp_ice_set_role(struct ast_rtp_instance *instance, enum ast_rtp_ice_role role) |
| 1277 |
{ |
| 1278 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1279 |
|
| 1280 |
if (!rtp->ice) { |
| 1281 |
ast_debug_ice(3, "(%p) ICE set role failed; no ice instance\n", instance); |
| 1282 |
return; |
| 1283 |
} |
| 1284 |
|
| 1285 |
rtp->role = role; |
| 1286 |
|
| 1287 |
if (!rtp->ice->real_ice->is_nominating && !rtp->ice->real_ice->is_complete) { |
| 1288 |
pj_thread_register_check(); |
| 1289 |
ast_debug_ice(2, "(%p) ICE set role to %s\n", |
| 1290 |
instance, role == AST_RTP_ICE_ROLE_CONTROLLED ? "CONTROLLED" : "CONTROLLING"); |
| 1291 |
pj_ice_sess_change_role(rtp->ice->real_ice, role == AST_RTP_ICE_ROLE_CONTROLLED ? |
| 1292 |
PJ_ICE_SESS_ROLE_CONTROLLED : PJ_ICE_SESS_ROLE_CONTROLLING); |
| 1293 |
} else { |
| 1294 |
ast_debug_ice(2, "(%p) ICE not setting role because state is %s\n", |
| 1295 |
instance, rtp->ice->real_ice->is_nominating ? "nominating" : "complete"); |
| 1296 |
} |
| 1297 |
} |
| 1298 |
|
| 1299 |
|
| 1300 |
static void ast_rtp_ice_add_cand(struct ast_rtp_instance *instance, struct ast_rtp *rtp, |
| 1301 |
unsigned comp_id, unsigned transport_id, pj_ice_cand_type type, pj_uint16_t local_pref, |
| 1302 |
const pj_sockaddr_t *addr, const pj_sockaddr_t *base_addr, const pj_sockaddr_t *rel_addr, |
| 1303 |
int addr_len) |
| 1304 |
{ |
| 1305 |
pj_str_t foundation; |
| 1306 |
struct ast_rtp_engine_ice_candidate *candidate, *existing; |
| 1307 |
struct ice_wrap *ice; |
| 1308 |
char address[PJ_INET6_ADDRSTRLEN]; |
| 1309 |
pj_status_t status; |
| 1310 |
|
| 1311 |
if (!rtp->ice) { |
| 1312 |
return; |
| 1313 |
} |
| 1314 |
|
| 1315 |
pj_thread_register_check(); |
| 1316 |
|
| 1317 |
pj_ice_calc_foundation(rtp->ice->real_ice->pool, &foundation, type, addr); |
| 1318 |
|
| 1319 |
if (!rtp->ice_local_candidates) { |
| 1320 |
rtp->ice_local_candidates = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 1321 |
NULL, ice_candidate_cmp); |
| 1322 |
if (!rtp->ice_local_candidates) { |
| 1323 |
return; |
| 1324 |
} |
| 1325 |
} |
| 1326 |
|
| 1327 |
if (!(candidate = ao2_alloc(sizeof(*candidate), ast_rtp_ice_candidate_destroy))) { |
| 1328 |
return; |
| 1329 |
} |
| 1330 |
|
| 1331 |
candidate->foundation = ast_strndup(pj_strbuf(&foundation), pj_strlen(&foundation)); |
| 1332 |
candidate->id = comp_id; |
| 1333 |
candidate->transport = ast_strdup("UDP"); |
| 1334 |
|
| 1335 |
ast_sockaddr_parse(&candidate->address, pj_sockaddr_print(addr, address, sizeof(address), 0), 0); |
| 1336 |
ast_sockaddr_set_port(&candidate->address, pj_sockaddr_get_port(addr)); |
| 1337 |
|
| 1338 |
if (rel_addr) { |
| 1339 |
ast_sockaddr_parse(&candidate->relay_address, pj_sockaddr_print(rel_addr, address, sizeof(address), 0), 0); |
| 1340 |
ast_sockaddr_set_port(&candidate->relay_address, pj_sockaddr_get_port(rel_addr)); |
| 1341 |
} |
| 1342 |
|
| 1343 |
if (type == PJ_ICE_CAND_TYPE_HOST) { |
| 1344 |
candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_HOST; |
| 1345 |
} else if (type == PJ_ICE_CAND_TYPE_SRFLX) { |
| 1346 |
candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX; |
| 1347 |
} else if (type == PJ_ICE_CAND_TYPE_RELAYED) { |
| 1348 |
candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED; |
| 1349 |
} |
| 1350 |
|
| 1351 |
if ((existing = ao2_find(rtp->ice_local_candidates, candidate, OBJ_POINTER))) { |
| 1352 |
ao2_ref(existing, -1); |
| 1353 |
ao2_ref(candidate, -1); |
| 1354 |
return; |
| 1355 |
} |
| 1356 |
|
| 1357 |
|
| 1358 |
ice = rtp->ice; |
| 1359 |
ao2_ref(ice, +1); |
| 1360 |
ao2_unlock(instance); |
| 1361 |
status = pj_ice_sess_add_cand(ice->real_ice, comp_id, transport_id, type, local_pref, |
| 1362 |
&foundation, addr, base_addr, rel_addr, addr_len, NULL); |
| 1363 |
ao2_ref(ice, -1); |
| 1364 |
ao2_lock(instance); |
| 1365 |
if (!rtp->ice || status != PJ_SUCCESS) { |
| 1366 |
ast_debug_ice(2, "(%p) ICE unable to add candidate: %s, %d\n", instance, ast_sockaddr_stringify( |
| 1367 |
&candidate->address), candidate->priority); |
| 1368 |
ao2_ref(candidate, -1); |
| 1369 |
return; |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
candidate->priority = rtp->ice->real_ice->lcand[rtp->ice->real_ice->lcand_cnt - 1].prio; |
| 1374 |
|
| 1375 |
ast_debug_ice(2, "(%p) ICE add candidate: %s, %d\n", instance, ast_sockaddr_stringify( |
| 1376 |
&candidate->address), candidate->priority); |
| 1377 |
|
| 1378 |
ao2_link(rtp->ice_local_candidates, candidate); |
| 1379 |
ao2_ref(candidate, -1); |
| 1380 |
} |
| 1381 |
|
| 1382 |
|
| 1383 |
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) |
| 1384 |
{ |
| 1385 |
struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock); |
| 1386 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1387 |
struct ice_wrap *ice; |
| 1388 |
pj_status_t status; |
| 1389 |
|
| 1390 |
ao2_lock(instance); |
| 1391 |
ice = ao2_bump(rtp->ice); |
| 1392 |
ao2_unlock(instance); |
| 1393 |
|
| 1394 |
if (ice) { |
| 1395 |
status = pj_ice_sess_on_rx_pkt(ice->real_ice, AST_RTP_ICE_COMPONENT_RTP, |
| 1396 |
TRANSPORT_TURN_RTP, pkt, pkt_len, peer_addr, addr_len); |
| 1397 |
ao2_ref(ice, -1); |
| 1398 |
if (status != PJ_SUCCESS) { |
| 1399 |
char buf[100]; |
| 1400 |
|
| 1401 |
pj_strerror(status, buf, sizeof(buf)); |
| 1402 |
ast_log(LOG_WARNING, "(%p) ICE PJ Rx error status code: %d '%s'.\n", |
| 1403 |
instance, (int)status, buf); |
| 1404 |
return; |
| 1405 |
} |
| 1406 |
if (!rtp->rtp_passthrough) { |
| 1407 |
return; |
| 1408 |
} |
| 1409 |
rtp->rtp_passthrough = 0; |
| 1410 |
} |
| 1411 |
|
| 1412 |
ast_sendto(rtp->s, pkt, pkt_len, 0, &rtp->rtp_loop); |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
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) |
| 1417 |
{ |
| 1418 |
struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock); |
| 1419 |
struct ast_rtp *rtp; |
| 1420 |
|
| 1421 |
|
| 1422 |
if (!instance) { |
| 1423 |
return; |
| 1424 |
} |
| 1425 |
|
| 1426 |
rtp = ast_rtp_instance_get_data(instance); |
| 1427 |
|
| 1428 |
ao2_lock(instance); |
| 1429 |
|
| 1430 |
|
| 1431 |
rtp->turn_state = new_state; |
| 1432 |
ast_cond_signal(&rtp->cond); |
| 1433 |
|
| 1434 |
if (new_state == PJ_TURN_STATE_DESTROYING) { |
| 1435 |
pj_turn_sock_set_user_data(rtp->turn_rtp, NULL); |
| 1436 |
rtp->turn_rtp = NULL; |
| 1437 |
} |
| 1438 |
|
| 1439 |
ao2_unlock(instance); |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
static pj_turn_sock_cb ast_rtp_turn_rtp_sock_cb = { |
| 1444 |
.on_rx_data = ast_rtp_on_turn_rx_rtp_data, |
| 1445 |
.on_state = ast_rtp_on_turn_rtp_state, |
| 1446 |
}; |
| 1447 |
|
| 1448 |
|
| 1449 |
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) |
| 1450 |
{ |
| 1451 |
struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock); |
| 1452 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1453 |
struct ice_wrap *ice; |
| 1454 |
pj_status_t status; |
| 1455 |
|
| 1456 |
ao2_lock(instance); |
| 1457 |
ice = ao2_bump(rtp->ice); |
| 1458 |
ao2_unlock(instance); |
| 1459 |
|
| 1460 |
if (ice) { |
| 1461 |
status = pj_ice_sess_on_rx_pkt(ice->real_ice, AST_RTP_ICE_COMPONENT_RTCP, |
| 1462 |
TRANSPORT_TURN_RTCP, pkt, pkt_len, peer_addr, addr_len); |
| 1463 |
ao2_ref(ice, -1); |
| 1464 |
if (status != PJ_SUCCESS) { |
| 1465 |
char buf[100]; |
| 1466 |
|
| 1467 |
pj_strerror(status, buf, sizeof(buf)); |
| 1468 |
ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n", |
| 1469 |
(int)status, buf); |
| 1470 |
return; |
| 1471 |
} |
| 1472 |
if (!rtp->rtcp_passthrough) { |
| 1473 |
return; |
| 1474 |
} |
| 1475 |
rtp->rtcp_passthrough = 0; |
| 1476 |
} |
| 1477 |
|
| 1478 |
ast_sendto(rtp->rtcp->s, pkt, pkt_len, 0, &rtp->rtcp_loop); |
| 1479 |
} |
| 1480 |
|
| 1481 |
|
| 1482 |
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) |
| 1483 |
{ |
| 1484 |
struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock); |
| 1485 |
struct ast_rtp *rtp; |
| 1486 |
|
| 1487 |
|
| 1488 |
if (!instance) { |
| 1489 |
return; |
| 1490 |
} |
| 1491 |
|
| 1492 |
rtp = ast_rtp_instance_get_data(instance); |
| 1493 |
|
| 1494 |
ao2_lock(instance); |
| 1495 |
|
| 1496 |
|
| 1497 |
rtp->turn_state = new_state; |
| 1498 |
ast_cond_signal(&rtp->cond); |
| 1499 |
|
| 1500 |
if (new_state == PJ_TURN_STATE_DESTROYING) { |
| 1501 |
pj_turn_sock_set_user_data(rtp->turn_rtcp, NULL); |
| 1502 |
rtp->turn_rtcp = NULL; |
| 1503 |
} |
| 1504 |
|
| 1505 |
ao2_unlock(instance); |
| 1506 |
} |
| 1507 |
|
| 1508 |
|
| 1509 |
static pj_turn_sock_cb ast_rtp_turn_rtcp_sock_cb = { |
| 1510 |
.on_rx_data = ast_rtp_on_turn_rx_rtcp_data, |
| 1511 |
.on_state = ast_rtp_on_turn_rtcp_state, |
| 1512 |
}; |
| 1513 |
|
| 1514 |
|
| 1515 |
static int ioqueue_worker_thread(void *data) |
| 1516 |
{ |
| 1517 |
struct ast_rtp_ioqueue_thread *ioqueue = data; |
| 1518 |
|
| 1519 |
while (!ioqueue->terminate) { |
| 1520 |
const pj_time_val delay = {0, 10}; |
| 1521 |
|
| 1522 |
pj_ioqueue_poll(ioqueue->ioqueue, &delay); |
| 1523 |
|
| 1524 |
pj_timer_heap_poll(ioqueue->timerheap, NULL); |
| 1525 |
} |
| 1526 |
|
| 1527 |
return 0; |
| 1528 |
} |
| 1529 |
|
| 1530 |
|
| 1531 |
static void rtp_ioqueue_thread_destroy(struct ast_rtp_ioqueue_thread *ioqueue) |
| 1532 |
{ |
| 1533 |
if (ioqueue->thread) { |
| 1534 |
ioqueue->terminate = 1; |
| 1535 |
pj_thread_join(ioqueue->thread); |
| 1536 |
pj_thread_destroy(ioqueue->thread); |
| 1537 |
} |
| 1538 |
|
| 1539 |
if (ioqueue->pool) { |
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
pj_pool_t *temp_pool = ioqueue->pool; |
| 1544 |
|
| 1545 |
ioqueue->pool = NULL; |
| 1546 |
pj_ioqueue_destroy(ioqueue->ioqueue); |
| 1547 |
pj_pool_release(temp_pool); |
| 1548 |
} |
| 1549 |
|
| 1550 |
ast_free(ioqueue); |
| 1551 |
} |
| 1552 |
|
| 1553 |
|
| 1554 |
static void rtp_ioqueue_thread_remove(struct ast_rtp_ioqueue_thread *ioqueue) |
| 1555 |
{ |
| 1556 |
int destroy = 0; |
| 1557 |
|
| 1558 |
|
| 1559 |
AST_LIST_LOCK(&ioqueues); |
| 1560 |
if ((ioqueue->count -= 2) == 0) { |
| 1561 |
destroy = 1; |
| 1562 |
AST_LIST_REMOVE(&ioqueues, ioqueue, next); |
| 1563 |
} |
| 1564 |
AST_LIST_UNLOCK(&ioqueues); |
| 1565 |
|
| 1566 |
if (!destroy) { |
| 1567 |
return; |
| 1568 |
} |
| 1569 |
|
| 1570 |
rtp_ioqueue_thread_destroy(ioqueue); |
| 1571 |
} |
| 1572 |
|
| 1573 |
|
| 1574 |
static struct ast_rtp_ioqueue_thread *rtp_ioqueue_thread_get_or_create(void) |
| 1575 |
{ |
| 1576 |
struct ast_rtp_ioqueue_thread *ioqueue; |
| 1577 |
pj_lock_t *lock; |
| 1578 |
|
| 1579 |
AST_LIST_LOCK(&ioqueues); |
| 1580 |
|
| 1581 |
|
| 1582 |
AST_LIST_TRAVERSE(&ioqueues, ioqueue, next) { |
| 1583 |
if ((ioqueue->count + 2) < PJ_IOQUEUE_MAX_HANDLES) { |
| 1584 |
break; |
| 1585 |
} |
| 1586 |
} |
| 1587 |
|
| 1588 |
|
| 1589 |
if (ioqueue) { |
| 1590 |
ioqueue->count += 2; |
| 1591 |
goto end; |
| 1592 |
} |
| 1593 |
|
| 1594 |
ioqueue = ast_calloc(1, sizeof(*ioqueue)); |
| 1595 |
if (!ioqueue) { |
| 1596 |
goto end; |
| 1597 |
} |
| 1598 |
|
| 1599 |
ioqueue->pool = pj_pool_create(&cachingpool.factory, "rtp", 512, 512, NULL); |
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
if (pj_timer_heap_create(ioqueue->pool, 4, &ioqueue->timerheap) != PJ_SUCCESS) { |
| 1605 |
goto fatal; |
| 1606 |
} |
| 1607 |
|
| 1608 |
if (pj_lock_create_recursive_mutex(ioqueue->pool, "rtp%p", &lock) != PJ_SUCCESS) { |
| 1609 |
goto fatal; |
| 1610 |
} |
| 1611 |
|
| 1612 |
pj_timer_heap_set_lock(ioqueue->timerheap, lock, PJ_TRUE); |
| 1613 |
|
| 1614 |
if (pj_ioqueue_create(ioqueue->pool, PJ_IOQUEUE_MAX_HANDLES, &ioqueue->ioqueue) != PJ_SUCCESS) { |
| 1615 |
goto fatal; |
| 1616 |
} |
| 1617 |
|
| 1618 |
if (pj_thread_create(ioqueue->pool, "ice", &ioqueue_worker_thread, ioqueue, 0, 0, &ioqueue->thread) != PJ_SUCCESS) { |
| 1619 |
goto fatal; |
| 1620 |
} |
| 1621 |
|
| 1622 |
AST_LIST_INSERT_HEAD(&ioqueues, ioqueue, next); |
| 1623 |
|
| 1624 |
|
| 1625 |
ioqueue->count = 2; |
| 1626 |
|
| 1627 |
goto end; |
| 1628 |
|
| 1629 |
fatal: |
| 1630 |
rtp_ioqueue_thread_destroy(ioqueue); |
| 1631 |
ioqueue = NULL; |
| 1632 |
|
| 1633 |
end: |
| 1634 |
AST_LIST_UNLOCK(&ioqueues); |
| 1635 |
return ioqueue; |
| 1636 |
} |
| 1637 |
|
| 1638 |
|
| 1639 |
static void ast_rtp_ice_turn_request(struct ast_rtp_instance *instance, enum ast_rtp_ice_component_type component, |
| 1640 |
enum ast_transport transport, const char *server, unsigned int port, const char *username, const char *password) |
| 1641 |
{ |
| 1642 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1643 |
pj_turn_sock **turn_sock; |
| 1644 |
const pj_turn_sock_cb *turn_cb; |
| 1645 |
pj_turn_tp_type conn_type; |
| 1646 |
int conn_transport; |
| 1647 |
pj_stun_auth_cred cred = { 0, }; |
| 1648 |
pj_str_t turn_addr; |
| 1649 |
struct ast_sockaddr addr = { { 0, } }; |
| 1650 |
pj_stun_config stun_config; |
| 1651 |
struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_STATE_WAIT_TIME, 1000)); |
| 1652 |
struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, }; |
| 1653 |
pj_turn_session_info info; |
| 1654 |
struct ast_sockaddr local, loop; |
| 1655 |
pj_status_t status; |
| 1656 |
pj_turn_sock_cfg turn_sock_cfg; |
| 1657 |
struct ice_wrap *ice; |
| 1658 |
|
| 1659 |
ast_rtp_instance_get_local_address(instance, &local); |
| 1660 |
if (ast_sockaddr_is_ipv4(&local)) { |
| 1661 |
ast_sockaddr_parse(&loop, "127.0.0.1", PARSE_PORT_FORBID); |
| 1662 |
} else { |
| 1663 |
ast_sockaddr_parse(&loop, "::1", PARSE_PORT_FORBID); |
| 1664 |
} |
| 1665 |
|
| 1666 |
|
| 1667 |
if (component == AST_RTP_ICE_COMPONENT_RTP) { |
| 1668 |
turn_sock = &rtp->turn_rtp; |
| 1669 |
turn_cb = &ast_rtp_turn_rtp_sock_cb; |
| 1670 |
conn_transport = TRANSPORT_TURN_RTP; |
| 1671 |
ast_sockaddr_set_port(&loop, ast_sockaddr_port(&local)); |
| 1672 |
} else if (component == AST_RTP_ICE_COMPONENT_RTCP) { |
| 1673 |
turn_sock = &rtp->turn_rtcp; |
| 1674 |
turn_cb = &ast_rtp_turn_rtcp_sock_cb; |
| 1675 |
conn_transport = TRANSPORT_TURN_RTCP; |
| 1676 |
ast_sockaddr_set_port(&loop, ast_sockaddr_port(&rtp->rtcp->us)); |
| 1677 |
} else { |
| 1678 |
return; |
| 1679 |
} |
| 1680 |
|
| 1681 |
if (transport == AST_TRANSPORT_UDP) { |
| 1682 |
conn_type = PJ_TURN_TP_UDP; |
| 1683 |
} else if (transport == AST_TRANSPORT_TCP) { |
| 1684 |
conn_type = PJ_TURN_TP_TCP; |
| 1685 |
} else { |
| 1686 |
ast_assert(0); |
| 1687 |
return; |
| 1688 |
} |
| 1689 |
|
| 1690 |
ast_sockaddr_parse(&addr, server, PARSE_PORT_FORBID); |
| 1691 |
|
| 1692 |
if (*turn_sock) { |
| 1693 |
rtp->turn_state = PJ_TURN_STATE_NULL; |
| 1694 |
|
| 1695 |
|
| 1696 |
ao2_unlock(instance); |
| 1697 |
pj_turn_sock_destroy(*turn_sock); |
| 1698 |
ao2_lock(instance); |
| 1699 |
while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) { |
| 1700 |
ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts); |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
if (component == AST_RTP_ICE_COMPONENT_RTP && !rtp->ioqueue) { |
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
ao2_unlock(instance); |
| 1711 |
rtp->ioqueue = rtp_ioqueue_thread_get_or_create(); |
| 1712 |
ao2_lock(instance); |
| 1713 |
if (!rtp->ioqueue) { |
| 1714 |
return; |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
pj_stun_config_init(&stun_config, &cachingpool.factory, 0, rtp->ioqueue->ioqueue, rtp->ioqueue->timerheap); |
| 1719 |
if (!stun_software_attribute) { |
| 1720 |
stun_config.software_name = pj_str(NULL); |
| 1721 |
} |
| 1722 |
|
| 1723 |
|
| 1724 |
pj_turn_sock_cfg_default(&turn_sock_cfg); |
| 1725 |
ice = rtp->ice; |
| 1726 |
if (ice) { |
| 1727 |
turn_sock_cfg.grp_lock = ice->real_ice->grp_lock; |
| 1728 |
ao2_ref(ice, +1); |
| 1729 |
} |
| 1730 |
|
| 1731 |
|
| 1732 |
ao2_unlock(instance); |
| 1733 |
status = pj_turn_sock_create(&stun_config, |
| 1734 |
ast_sockaddr_is_ipv4(&addr) ? pj_AF_INET() : pj_AF_INET6(), conn_type, |
| 1735 |
turn_cb, &turn_sock_cfg, instance, turn_sock); |
| 1736 |
ao2_cleanup(ice); |
| 1737 |
if (status != PJ_SUCCESS) { |
| 1738 |
ast_log(LOG_WARNING, "(%p) Could not create a TURN client socket\n", instance); |
| 1739 |
ao2_lock(instance); |
| 1740 |
return; |
| 1741 |
} |
| 1742 |
|
| 1743 |
cred.type = PJ_STUN_AUTH_CRED_STATIC; |
| 1744 |
pj_strset2(&cred.data.static_cred.username, (char*)username); |
| 1745 |
cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN; |
| 1746 |
pj_strset2(&cred.data.static_cred.data, (char*)password); |
| 1747 |
|
| 1748 |
pj_turn_sock_alloc(*turn_sock, pj_cstr(&turn_addr, server), port, NULL, &cred, NULL); |
| 1749 |
|
| 1750 |
ast_debug_ice(2, "(%p) ICE request TURN %s %s candidate\n", instance, |
| 1751 |
transport == AST_TRANSPORT_UDP ? "UDP" : "TCP", |
| 1752 |
component == AST_RTP_ICE_COMPONENT_RTP ? "RTP" : "RTCP"); |
| 1753 |
|
| 1754 |
ao2_lock(instance); |
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
while (rtp->turn_state < PJ_TURN_STATE_READY) { |
| 1761 |
ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts); |
| 1762 |
} |
| 1763 |
|
| 1764 |
|
| 1765 |
if (rtp->turn_state != PJ_TURN_STATE_READY) { |
| 1766 |
return; |
| 1767 |
} |
| 1768 |
|
| 1769 |
pj_turn_sock_get_info(*turn_sock, &info); |
| 1770 |
|
| 1771 |
ast_rtp_ice_add_cand(instance, rtp, component, conn_transport, |
| 1772 |
PJ_ICE_CAND_TYPE_RELAYED, 65535, &info.relay_addr, &info.relay_addr, |
| 1773 |
&info.mapped_addr, pj_sockaddr_get_len(&info.relay_addr)); |
| 1774 |
|
| 1775 |
if (component == AST_RTP_ICE_COMPONENT_RTP) { |
| 1776 |
ast_sockaddr_copy(&rtp->rtp_loop, &loop); |
| 1777 |
} else if (component == AST_RTP_ICE_COMPONENT_RTCP) { |
| 1778 |
ast_sockaddr_copy(&rtp->rtcp_loop, &loop); |
| 1779 |
} |
| 1780 |
} |
| 1781 |
|
| 1782 |
static char *generate_random_string(char *buf, size_t size) |
| 1783 |
{ |
| 1784 |
long val[4]; |
| 1785 |
int x; |
| 1786 |
|
| 1787 |
for (x=0; x<4; x++) { |
| 1788 |
val[x] = ast_random(); |
| 1789 |
} |
| 1790 |
snprintf(buf, size, "%08lx%08lx%08lx%08lx", (long unsigned)val[0], (long unsigned)val[1], (long unsigned)val[2], (long unsigned)val[3]); |
| 1791 |
|
| 1792 |
return buf; |
| 1793 |
} |
| 1794 |
|
| 1795 |
|
| 1796 |
static void ast_rtp_ice_change_components(struct ast_rtp_instance *instance, int num_components) |
| 1797 |
{ |
| 1798 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
if (!icesupport || !rtp->ice || rtp->ice_num_components == num_components) { |
| 1804 |
return; |
| 1805 |
} |
| 1806 |
|
| 1807 |
ast_debug_ice(2, "(%p) ICE change number of components %u -> %u\n", instance, |
| 1808 |
rtp->ice_num_components, num_components); |
| 1809 |
|
| 1810 |
rtp->ice_num_components = num_components; |
| 1811 |
ice_reset_session(instance); |
| 1812 |
} |
| 1813 |
|
| 1814 |
|
| 1815 |
static struct ast_rtp_engine_ice ast_rtp_ice = { |
| 1816 |
.set_authentication = ast_rtp_ice_set_authentication, |
| 1817 |
.add_remote_candidate = ast_rtp_ice_add_remote_candidate, |
| 1818 |
.start = ast_rtp_ice_start, |
| 1819 |
.stop = ast_rtp_ice_stop, |
| 1820 |
.get_ufrag = ast_rtp_ice_get_ufrag, |
| 1821 |
.get_password = ast_rtp_ice_get_password, |
| 1822 |
.get_local_candidates = ast_rtp_ice_get_local_candidates, |
| 1823 |
.ice_lite = ast_rtp_ice_lite, |
| 1824 |
.set_role = ast_rtp_ice_set_role, |
| 1825 |
.turn_request = ast_rtp_ice_turn_request, |
| 1826 |
.change_components = ast_rtp_ice_change_components, |
| 1827 |
}; |
| 1828 |
#endif |
| 1829 |
|
| 1830 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 1831 |
static int dtls_verify_callback(int preverify_ok, X509_STORE_CTX *ctx) |
| 1832 |
{ |
| 1833 |
|
| 1834 |
return 1; |
| 1835 |
} |
| 1836 |
|
| 1837 |
static int dtls_details_initialize(struct dtls_details *dtls, SSL_CTX *ssl_ctx, |
| 1838 |
enum ast_rtp_dtls_setup setup, struct ast_rtp_instance *instance) |
| 1839 |
{ |
| 1840 |
dtls->dtls_setup = setup; |
| 1841 |
|
| 1842 |
if (!(dtls->ssl = SSL_new(ssl_ctx))) { |
| 1843 |
ast_log(LOG_ERROR, "Failed to allocate memory for SSL\n"); |
| 1844 |
goto error; |
| 1845 |
} |
| 1846 |
|
| 1847 |
if (!(dtls->read_bio = BIO_new(BIO_s_mem()))) { |
| 1848 |
ast_log(LOG_ERROR, "Failed to allocate memory for inbound SSL traffic\n"); |
| 1849 |
goto error; |
| 1850 |
} |
| 1851 |
BIO_set_mem_eof_return(dtls->read_bio, -1); |
| 1852 |
|
| 1853 |
#ifdef HAVE_OPENSSL_BIO_METHOD |
| 1854 |
if (!(dtls->write_bio = BIO_new(dtls_bio_methods))) { |
| 1855 |
ast_log(LOG_ERROR, "Failed to allocate memory for outbound SSL traffic\n"); |
| 1856 |
goto error; |
| 1857 |
} |
| 1858 |
|
| 1859 |
BIO_set_data(dtls->write_bio, instance); |
| 1860 |
#else |
| 1861 |
if (!(dtls->write_bio = BIO_new(&dtls_bio_methods))) { |
| 1862 |
ast_log(LOG_ERROR, "Failed to allocate memory for outbound SSL traffic\n"); |
| 1863 |
goto error; |
| 1864 |
} |
| 1865 |
dtls->write_bio->ptr = instance; |
| 1866 |
#endif |
| 1867 |
SSL_set_bio(dtls->ssl, dtls->read_bio, dtls->write_bio); |
| 1868 |
|
| 1869 |
if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) { |
| 1870 |
SSL_set_accept_state(dtls->ssl); |
| 1871 |
} else { |
| 1872 |
SSL_set_connect_state(dtls->ssl); |
| 1873 |
} |
| 1874 |
dtls->connection = AST_RTP_DTLS_CONNECTION_NEW; |
| 1875 |
|
| 1876 |
return 0; |
| 1877 |
|
| 1878 |
error: |
| 1879 |
if (dtls->read_bio) { |
| 1880 |
BIO_free(dtls->read_bio); |
| 1881 |
dtls->read_bio = NULL; |
| 1882 |
} |
| 1883 |
|
| 1884 |
if (dtls->write_bio) { |
| 1885 |
BIO_free(dtls->write_bio); |
| 1886 |
dtls->write_bio = NULL; |
| 1887 |
} |
| 1888 |
|
| 1889 |
if (dtls->ssl) { |
| 1890 |
SSL_free(dtls->ssl); |
| 1891 |
dtls->ssl = NULL; |
| 1892 |
} |
| 1893 |
return -1; |
| 1894 |
} |
| 1895 |
|
| 1896 |
static int dtls_setup_rtcp(struct ast_rtp_instance *instance) |
| 1897 |
{ |
| 1898 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 1899 |
|
| 1900 |
if (!rtp->ssl_ctx || !rtp->rtcp) { |
| 1901 |
return 0; |
| 1902 |
} |
| 1903 |
|
| 1904 |
ast_debug_dtls(3, "(%p) DTLS RTCP setup\n", instance); |
| 1905 |
return dtls_details_initialize(&rtp->rtcp->dtls, rtp->ssl_ctx, rtp->dtls.dtls_setup, instance); |
| 1906 |
} |
| 1907 |
|
| 1908 |
static const SSL_METHOD *get_dtls_method(void) |
| 1909 |
{ |
| 1910 |
#if OPENSSL_VERSION_NUMBER < 0x10002000L |
| 1911 |
return DTLSv1_method(); |
| 1912 |
#else |
| 1913 |
return DTLS_method(); |
| 1914 |
#endif |
| 1915 |
} |
| 1916 |
|
| 1917 |
struct dtls_cert_info { |
| 1918 |
EVP_PKEY *private_key; |
| 1919 |
X509 *certificate; |
| 1920 |
}; |
| 1921 |
|
| 1922 |
static int apply_dh_params(SSL_CTX *ctx, BIO *bio) |
| 1923 |
{ |
| 1924 |
int res = 0; |
| 1925 |
|
| 1926 |
#if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1927 |
EVP_PKEY *dhpkey = PEM_read_bio_Parameters(bio, NULL); |
| 1928 |
if (dhpkey && EVP_PKEY_is_a(dhpkey, "DH")) { |
| 1929 |
res = SSL_CTX_set0_tmp_dh_pkey(ctx, dhpkey); |
| 1930 |
} |
| 1931 |
if (!res) { |
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
EVP_PKEY_free(dhpkey); |
| 1936 |
} |
| 1937 |
#else |
| 1938 |
DH *dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
| 1939 |
if (dh) { |
| 1940 |
res = SSL_CTX_set_tmp_dh(ctx, dh); |
| 1941 |
} |
| 1942 |
DH_free(dh); |
| 1943 |
#endif |
| 1944 |
|
| 1945 |
return res; |
| 1946 |
} |
| 1947 |
|
| 1948 |
static void configure_dhparams(const struct ast_rtp *rtp, const struct ast_rtp_dtls_cfg *dtls_cfg) |
| 1949 |
{ |
| 1950 |
#if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L) |
| 1951 |
EC_KEY *ecdh; |
| 1952 |
#endif |
| 1953 |
|
| 1954 |
#ifndef OPENSSL_NO_DH |
| 1955 |
if (!ast_strlen_zero(dtls_cfg->pvtfile)) { |
| 1956 |
BIO *bio = BIO_new_file(dtls_cfg->pvtfile, "r"); |
| 1957 |
if (bio) { |
| 1958 |
if (apply_dh_params(rtp->ssl_ctx, bio)) { |
| 1959 |
long options = SSL_OP_CIPHER_SERVER_PREFERENCE | |
| 1960 |
SSL_OP_SINGLE_DH_USE | SSL_OP_SINGLE_ECDH_USE; |
| 1961 |
options = SSL_CTX_set_options(rtp->ssl_ctx, options); |
| 1962 |
ast_verb(2, "DTLS DH initialized, PFS enabled\n"); |
| 1963 |
} |
| 1964 |
BIO_free(bio); |
| 1965 |
} |
| 1966 |
} |
| 1967 |
#endif |
| 1968 |
|
| 1969 |
#if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L) |
| 1970 |
|
| 1971 |
ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 1972 |
if (ecdh) { |
| 1973 |
if (SSL_CTX_set_tmp_ecdh(rtp->ssl_ctx, ecdh)) { |
| 1974 |
#ifndef SSL_CTRL_SET_ECDH_AUTO |
| 1975 |
#define SSL_CTRL_SET_ECDH_AUTO 94 |
| 1976 |
#endif |
| 1977 |
|
| 1978 |
if (SSL_CTX_ctrl(rtp->ssl_ctx, SSL_CTRL_SET_ECDH_AUTO, 1, NULL)) { |
| 1979 |
ast_verb(2, "DTLS ECDH initialized (automatic), faster PFS enabled\n"); |
| 1980 |
} else { |
| 1981 |
ast_verb(2, "DTLS ECDH initialized (secp256r1), faster PFS enabled\n"); |
| 1982 |
} |
| 1983 |
} |
| 1984 |
EC_KEY_free(ecdh); |
| 1985 |
} |
| 1986 |
#endif |
| 1987 |
} |
| 1988 |
|
| 1989 |
#if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) |
| 1990 |
|
| 1991 |
static int create_ephemeral_ec_keypair(EVP_PKEY **keypair) |
| 1992 |
{ |
| 1993 |
#if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1994 |
*keypair = EVP_EC_gen(SN_X9_62_prime256v1); |
| 1995 |
return *keypair ? 0 : -1; |
| 1996 |
#else |
| 1997 |
EC_KEY *eckey = NULL; |
| 1998 |
EC_GROUP *group = NULL; |
| 1999 |
|
| 2000 |
group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 2001 |
if (!group) { |
| 2002 |
goto error; |
| 2003 |
} |
| 2004 |
|
| 2005 |
EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE); |
| 2006 |
EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_UNCOMPRESSED); |
| 2007 |
|
| 2008 |
eckey = EC_KEY_new(); |
| 2009 |
if (!eckey) { |
| 2010 |
goto error; |
| 2011 |
} |
| 2012 |
|
| 2013 |
if (!EC_KEY_set_group(eckey, group)) { |
| 2014 |
goto error; |
| 2015 |
} |
| 2016 |
|
| 2017 |
if (!EC_KEY_generate_key(eckey)) { |
| 2018 |
goto error; |
| 2019 |
} |
| 2020 |
|
| 2021 |
*keypair = EVP_PKEY_new(); |
| 2022 |
if (!*keypair) { |
| 2023 |
goto error; |
| 2024 |
} |
| 2025 |
|
| 2026 |
EVP_PKEY_assign_EC_KEY(*keypair, eckey); |
| 2027 |
EC_GROUP_free(group); |
| 2028 |
|
| 2029 |
return 0; |
| 2030 |
|
| 2031 |
error: |
| 2032 |
EC_KEY_free(eckey); |
| 2033 |
EC_GROUP_free(group); |
| 2034 |
|
| 2035 |
return -1; |
| 2036 |
#endif |
| 2037 |
} |
| 2038 |
|
| 2039 |
|
| 2040 |
#define SERIAL_RAND_BITS 159 |
| 2041 |
|
| 2042 |
static int create_ephemeral_certificate(EVP_PKEY *keypair, X509 **certificate) |
| 2043 |
{ |
| 2044 |
X509 *cert = NULL; |
| 2045 |
BIGNUM *serial = NULL; |
| 2046 |
X509_NAME *name = NULL; |
| 2047 |
|
| 2048 |
cert = X509_new(); |
| 2049 |
if (!cert) { |
| 2050 |
goto error; |
| 2051 |
} |
| 2052 |
|
| 2053 |
if (!X509_set_version(cert, 2)) { |
| 2054 |
goto error; |
| 2055 |
} |
| 2056 |
|
| 2057 |
|
| 2058 |
X509_set_pubkey(cert, keypair); |
| 2059 |
|
| 2060 |
|
| 2061 |
if (!(serial = BN_new()) |
| 2062 |
|| !BN_rand(serial, SERIAL_RAND_BITS, -1, 0) |
| 2063 |
|| !BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(cert))) { |
| 2064 |
BN_free(serial); |
| 2065 |
goto error; |
| 2066 |
} |
| 2067 |
|
| 2068 |
BN_free(serial); |
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 2075 |
if (!X509_time_adj_ex(X509_get_notBefore(cert), -1, 0, NULL) |
| 2076 |
|| !X509_time_adj_ex(X509_get_notAfter(cert), 30, 0, NULL)) { |
| 2077 |
goto error; |
| 2078 |
} |
| 2079 |
#else |
| 2080 |
if (!X509_time_adj_ex(X509_getm_notBefore(cert), -1, 0, NULL) |
| 2081 |
|| !X509_time_adj_ex(X509_getm_notAfter(cert), 30, 0, NULL)) { |
| 2082 |
goto error; |
| 2083 |
} |
| 2084 |
#endif |
| 2085 |
|
| 2086 |
|
| 2087 |
if (!(name = X509_get_subject_name(cert)) |
| 2088 |
|| !X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC, |
| 2089 |
(unsigned char *) "asterisk", -1, -1, 0) |
| 2090 |
|| !X509_set_issuer_name(cert, name)) { |
| 2091 |
goto error; |
| 2092 |
} |
| 2093 |
|
| 2094 |
|
| 2095 |
if (!X509_sign(cert, keypair, EVP_sha256())) { |
| 2096 |
goto error; |
| 2097 |
} |
| 2098 |
|
| 2099 |
*certificate = cert; |
| 2100 |
|
| 2101 |
return 0; |
| 2102 |
|
| 2103 |
error: |
| 2104 |
X509_free(cert); |
| 2105 |
|
| 2106 |
return -1; |
| 2107 |
} |
| 2108 |
|
| 2109 |
static int create_certificate_ephemeral(struct ast_rtp_instance *instance, |
| 2110 |
const struct ast_rtp_dtls_cfg *dtls_cfg, |
| 2111 |
struct dtls_cert_info *cert_info) |
| 2112 |
{ |
| 2113 |
|
| 2114 |
cert_info->private_key = NULL; |
| 2115 |
cert_info->certificate = NULL; |
| 2116 |
|
| 2117 |
if (create_ephemeral_ec_keypair(&cert_info->private_key)) { |
| 2118 |
ast_log(LOG_ERROR, "Failed to create ephemeral ECDSA keypair\n"); |
| 2119 |
goto error; |
| 2120 |
} |
| 2121 |
|
| 2122 |
if (create_ephemeral_certificate(cert_info->private_key, &cert_info->certificate)) { |
| 2123 |
ast_log(LOG_ERROR, "Failed to create ephemeral X509 certificate\n"); |
| 2124 |
goto error; |
| 2125 |
} |
| 2126 |
|
| 2127 |
return 0; |
| 2128 |
|
| 2129 |
error: |
| 2130 |
X509_free(cert_info->certificate); |
| 2131 |
EVP_PKEY_free(cert_info->private_key); |
| 2132 |
|
| 2133 |
return -1; |
| 2134 |
} |
| 2135 |
|
| 2136 |
#else |
| 2137 |
|
| 2138 |
static int create_certificate_ephemeral(struct ast_rtp_instance *instance, |
| 2139 |
const struct ast_rtp_dtls_cfg *dtls_cfg, |
| 2140 |
struct dtls_cert_info *cert_info) |
| 2141 |
{ |
| 2142 |
ast_log(LOG_ERROR, "Your version of OpenSSL does not support ECDSA keys\n"); |
| 2143 |
return -1; |
| 2144 |
} |
| 2145 |
|
| 2146 |
#endif |
| 2147 |
|
| 2148 |
static int create_certificate_from_file(struct ast_rtp_instance *instance, |
| 2149 |
const struct ast_rtp_dtls_cfg *dtls_cfg, |
| 2150 |
struct dtls_cert_info *cert_info) |
| 2151 |
{ |
| 2152 |
FILE *fp; |
| 2153 |
BIO *certbio = NULL; |
| 2154 |
EVP_PKEY *private_key = NULL; |
| 2155 |
X509 *cert = NULL; |
| 2156 |
char *private_key_file = ast_strlen_zero(dtls_cfg->pvtfile) ? dtls_cfg->certfile : dtls_cfg->pvtfile; |
| 2157 |
|
| 2158 |
fp = fopen(private_key_file, "r"); |
| 2159 |
if (!fp) { |
| 2160 |
ast_log(LOG_ERROR, "Failed to read private key from file '%s': %s\n", private_key_file, strerror(errno)); |
| 2161 |
goto error; |
| 2162 |
} |
| 2163 |
|
| 2164 |
if (!PEM_read_PrivateKey(fp, &private_key, NULL, NULL)) { |
| 2165 |
ast_log(LOG_ERROR, "Failed to read private key from PEM file '%s'\n", private_key_file); |
| 2166 |
fclose(fp); |
| 2167 |
goto error; |
| 2168 |
} |
| 2169 |
|
| 2170 |
if (fclose(fp)) { |
| 2171 |
ast_log(LOG_ERROR, "Failed to close private key file '%s': %s\n", private_key_file, strerror(errno)); |
| 2172 |
goto error; |
| 2173 |
} |
| 2174 |
|
| 2175 |
certbio = BIO_new(BIO_s_file()); |
| 2176 |
if (!certbio) { |
| 2177 |
ast_log(LOG_ERROR, "Failed to allocate memory for certificate fingerprinting on RTP instance '%p'\n", |
| 2178 |
instance); |
| 2179 |
goto error; |
| 2180 |
} |
| 2181 |
|
| 2182 |
if (!BIO_read_filename(certbio, dtls_cfg->certfile) |
| 2183 |
|| !(cert = PEM_read_bio_X509(certbio, NULL, 0, NULL))) { |
| 2184 |
ast_log(LOG_ERROR, "Failed to read certificate from file '%s'\n", dtls_cfg->certfile); |
| 2185 |
goto error; |
| 2186 |
} |
| 2187 |
|
| 2188 |
cert_info->private_key = private_key; |
| 2189 |
cert_info->certificate = cert; |
| 2190 |
|
| 2191 |
BIO_free_all(certbio); |
| 2192 |
|
| 2193 |
return 0; |
| 2194 |
|
| 2195 |
error: |
| 2196 |
X509_free(cert); |
| 2197 |
BIO_free_all(certbio); |
| 2198 |
EVP_PKEY_free(private_key); |
| 2199 |
|
| 2200 |
return -1; |
| 2201 |
} |
| 2202 |
|
| 2203 |
static int load_dtls_certificate(struct ast_rtp_instance *instance, |
| 2204 |
const struct ast_rtp_dtls_cfg *dtls_cfg, |
| 2205 |
struct dtls_cert_info *cert_info) |
| 2206 |
{ |
| 2207 |
if (dtls_cfg->ephemeral_cert) { |
| 2208 |
return create_certificate_ephemeral(instance, dtls_cfg, cert_info); |
| 2209 |
} else if (!ast_strlen_zero(dtls_cfg->certfile)) { |
| 2210 |
return create_certificate_from_file(instance, dtls_cfg, cert_info); |
| 2211 |
} else { |
| 2212 |
return -1; |
| 2213 |
} |
| 2214 |
} |
| 2215 |
|
| 2216 |
|
| 2217 |
static int ast_rtp_dtls_set_configuration(struct ast_rtp_instance *instance, const struct ast_rtp_dtls_cfg *dtls_cfg) |
| 2218 |
{ |
| 2219 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2220 |
struct dtls_cert_info cert_info = { 0 }; |
| 2221 |
int res; |
| 2222 |
|
| 2223 |
if (!dtls_cfg->enabled) { |
| 2224 |
return 0; |
| 2225 |
} |
| 2226 |
|
| 2227 |
ast_debug_dtls(3, "(%p) DTLS RTP setup\n", instance); |
| 2228 |
|
| 2229 |
if (!ast_rtp_engine_srtp_is_registered()) { |
| 2230 |
ast_log(LOG_ERROR, "SRTP support module is not loaded or available. Try loading res_srtp.so.\n"); |
| 2231 |
return -1; |
| 2232 |
} |
| 2233 |
|
| 2234 |
if (rtp->ssl_ctx) { |
| 2235 |
return 0; |
| 2236 |
} |
| 2237 |
|
| 2238 |
rtp->ssl_ctx = SSL_CTX_new(get_dtls_method()); |
| 2239 |
if (!rtp->ssl_ctx) { |
| 2240 |
return -1; |
| 2241 |
} |
| 2242 |
|
| 2243 |
SSL_CTX_set_read_ahead(rtp->ssl_ctx, 1); |
| 2244 |
|
| 2245 |
configure_dhparams(rtp, dtls_cfg); |
| 2246 |
|
| 2247 |
rtp->dtls_verify = dtls_cfg->verify; |
| 2248 |
|
| 2249 |
SSL_CTX_set_verify(rtp->ssl_ctx, (rtp->dtls_verify & AST_RTP_DTLS_VERIFY_FINGERPRINT) || (rtp->dtls_verify & AST_RTP_DTLS_VERIFY_CERTIFICATE) ? |
| 2250 |
SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT : SSL_VERIFY_NONE, !(rtp->dtls_verify & AST_RTP_DTLS_VERIFY_CERTIFICATE) ? |
| 2251 |
dtls_verify_callback : NULL); |
| 2252 |
|
| 2253 |
if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_80) { |
| 2254 |
SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_80"); |
| 2255 |
} else if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_32) { |
| 2256 |
SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_32"); |
| 2257 |
} else { |
| 2258 |
ast_log(LOG_ERROR, "Unsupported suite specified for DTLS-SRTP on RTP instance '%p'\n", instance); |
| 2259 |
return -1; |
| 2260 |
} |
| 2261 |
|
| 2262 |
rtp->local_hash = dtls_cfg->hash; |
| 2263 |
|
| 2264 |
if (!load_dtls_certificate(instance, dtls_cfg, &cert_info)) { |
| 2265 |
const EVP_MD *type; |
| 2266 |
unsigned int size, i; |
| 2267 |
unsigned char fingerprint[EVP_MAX_MD_SIZE]; |
| 2268 |
char *local_fingerprint = rtp->local_fingerprint; |
| 2269 |
|
| 2270 |
if (!SSL_CTX_use_certificate(rtp->ssl_ctx, cert_info.certificate)) { |
| 2271 |
ast_log(LOG_ERROR, "Specified certificate for RTP instance '%p' could not be used\n", |
| 2272 |
instance); |
| 2273 |
return -1; |
| 2274 |
} |
| 2275 |
|
| 2276 |
if (!SSL_CTX_use_PrivateKey(rtp->ssl_ctx, cert_info.private_key) |
| 2277 |
|| !SSL_CTX_check_private_key(rtp->ssl_ctx)) { |
| 2278 |
ast_log(LOG_ERROR, "Specified private key for RTP instance '%p' could not be used\n", |
| 2279 |
instance); |
| 2280 |
return -1; |
| 2281 |
} |
| 2282 |
|
| 2283 |
if (rtp->local_hash == AST_RTP_DTLS_HASH_SHA1) { |
| 2284 |
type = EVP_sha1(); |
| 2285 |
} else if (rtp->local_hash == AST_RTP_DTLS_HASH_SHA256) { |
| 2286 |
type = EVP_sha256(); |
| 2287 |
} else { |
| 2288 |
ast_log(LOG_ERROR, "Unsupported fingerprint hash type on RTP instance '%p'\n", |
| 2289 |
instance); |
| 2290 |
return -1; |
| 2291 |
} |
| 2292 |
|
| 2293 |
if (!X509_digest(cert_info.certificate, type, fingerprint, &size) || !size) { |
| 2294 |
ast_log(LOG_ERROR, "Could not produce fingerprint from certificate for RTP instance '%p'\n", |
| 2295 |
instance); |
| 2296 |
return -1; |
| 2297 |
} |
| 2298 |
|
| 2299 |
for (i = 0; i < size; i++) { |
| 2300 |
sprintf(local_fingerprint, "%02hhX:", fingerprint[i]); |
| 2301 |
local_fingerprint += 3; |
| 2302 |
} |
| 2303 |
|
| 2304 |
*(local_fingerprint - 1) = 0; |
| 2305 |
|
| 2306 |
EVP_PKEY_free(cert_info.private_key); |
| 2307 |
X509_free(cert_info.certificate); |
| 2308 |
} |
| 2309 |
|
| 2310 |
if (!ast_strlen_zero(dtls_cfg->cipher)) { |
| 2311 |
if (!SSL_CTX_set_cipher_list(rtp->ssl_ctx, dtls_cfg->cipher)) { |
| 2312 |
ast_log(LOG_ERROR, "Invalid cipher specified in cipher list '%s' for RTP instance '%p'\n", |
| 2313 |
dtls_cfg->cipher, instance); |
| 2314 |
return -1; |
| 2315 |
} |
| 2316 |
} |
| 2317 |
|
| 2318 |
if (!ast_strlen_zero(dtls_cfg->cafile) || !ast_strlen_zero(dtls_cfg->capath)) { |
| 2319 |
if (!SSL_CTX_load_verify_locations(rtp->ssl_ctx, S_OR(dtls_cfg->cafile, NULL), S_OR(dtls_cfg->capath, NULL))) { |
| 2320 |
ast_log(LOG_ERROR, "Invalid certificate authority file '%s' or path '%s' specified for RTP instance '%p'\n", |
| 2321 |
S_OR(dtls_cfg->cafile, ""), S_OR(dtls_cfg->capath, ""), instance); |
| 2322 |
return -1; |
| 2323 |
} |
| 2324 |
} |
| 2325 |
|
| 2326 |
rtp->rekey = dtls_cfg->rekey; |
| 2327 |
rtp->suite = dtls_cfg->suite; |
| 2328 |
|
| 2329 |
res = dtls_details_initialize(&rtp->dtls, rtp->ssl_ctx, dtls_cfg->default_setup, instance); |
| 2330 |
if (!res) { |
| 2331 |
dtls_setup_rtcp(instance); |
| 2332 |
} |
| 2333 |
|
| 2334 |
return res; |
| 2335 |
} |
| 2336 |
|
| 2337 |
|
| 2338 |
static int ast_rtp_dtls_active(struct ast_rtp_instance *instance) |
| 2339 |
{ |
| 2340 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2341 |
|
| 2342 |
return !rtp->ssl_ctx ? 0 : 1; |
| 2343 |
} |
| 2344 |
|
| 2345 |
|
| 2346 |
static void ast_rtp_dtls_stop(struct ast_rtp_instance *instance) |
| 2347 |
{ |
| 2348 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2349 |
SSL *ssl = rtp->dtls.ssl; |
| 2350 |
|
| 2351 |
ast_debug_dtls(3, "(%p) DTLS stop\n", instance); |
| 2352 |
ao2_unlock(instance); |
| 2353 |
dtls_srtp_stop_timeout_timer(instance, rtp, 0); |
| 2354 |
ao2_lock(instance); |
| 2355 |
|
| 2356 |
if (rtp->ssl_ctx) { |
| 2357 |
SSL_CTX_free(rtp->ssl_ctx); |
| 2358 |
rtp->ssl_ctx = NULL; |
| 2359 |
} |
| 2360 |
|
| 2361 |
if (rtp->dtls.ssl) { |
| 2362 |
SSL_free(rtp->dtls.ssl); |
| 2363 |
rtp->dtls.ssl = NULL; |
| 2364 |
} |
| 2365 |
|
| 2366 |
if (rtp->rtcp) { |
| 2367 |
ao2_unlock(instance); |
| 2368 |
dtls_srtp_stop_timeout_timer(instance, rtp, 1); |
| 2369 |
ao2_lock(instance); |
| 2370 |
|
| 2371 |
if (rtp->rtcp->dtls.ssl) { |
| 2372 |
if (rtp->rtcp->dtls.ssl != ssl) { |
| 2373 |
SSL_free(rtp->rtcp->dtls.ssl); |
| 2374 |
} |
| 2375 |
rtp->rtcp->dtls.ssl = NULL; |
| 2376 |
} |
| 2377 |
} |
| 2378 |
} |
| 2379 |
|
| 2380 |
|
| 2381 |
static void ast_rtp_dtls_reset(struct ast_rtp_instance *instance) |
| 2382 |
{ |
| 2383 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2384 |
|
| 2385 |
if (SSL_is_init_finished(rtp->dtls.ssl)) { |
| 2386 |
SSL_shutdown(rtp->dtls.ssl); |
| 2387 |
rtp->dtls.connection = AST_RTP_DTLS_CONNECTION_NEW; |
| 2388 |
} |
| 2389 |
|
| 2390 |
if (rtp->rtcp && SSL_is_init_finished(rtp->rtcp->dtls.ssl)) { |
| 2391 |
SSL_shutdown(rtp->rtcp->dtls.ssl); |
| 2392 |
rtp->rtcp->dtls.connection = AST_RTP_DTLS_CONNECTION_NEW; |
| 2393 |
} |
| 2394 |
} |
| 2395 |
|
| 2396 |
|
| 2397 |
static enum ast_rtp_dtls_connection ast_rtp_dtls_get_connection(struct ast_rtp_instance *instance) |
| 2398 |
{ |
| 2399 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2400 |
|
| 2401 |
return rtp->dtls.connection; |
| 2402 |
} |
| 2403 |
|
| 2404 |
|
| 2405 |
static enum ast_rtp_dtls_setup ast_rtp_dtls_get_setup(struct ast_rtp_instance *instance) |
| 2406 |
{ |
| 2407 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2408 |
|
| 2409 |
return rtp->dtls.dtls_setup; |
| 2410 |
} |
| 2411 |
|
| 2412 |
static void dtls_set_setup(enum ast_rtp_dtls_setup *dtls_setup, enum ast_rtp_dtls_setup setup, SSL *ssl) |
| 2413 |
{ |
| 2414 |
enum ast_rtp_dtls_setup old = *dtls_setup; |
| 2415 |
|
| 2416 |
switch (setup) { |
| 2417 |
case AST_RTP_DTLS_SETUP_ACTIVE: |
| 2418 |
*dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE; |
| 2419 |
break; |
| 2420 |
case AST_RTP_DTLS_SETUP_PASSIVE: |
| 2421 |
*dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE; |
| 2422 |
break; |
| 2423 |
case AST_RTP_DTLS_SETUP_ACTPASS: |
| 2424 |
|
| 2425 |
if (*dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) { |
| 2426 |
*dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE; |
| 2427 |
} |
| 2428 |
break; |
| 2429 |
case AST_RTP_DTLS_SETUP_HOLDCONN: |
| 2430 |
*dtls_setup = AST_RTP_DTLS_SETUP_HOLDCONN; |
| 2431 |
break; |
| 2432 |
default: |
| 2433 |
|
| 2434 |
return; |
| 2435 |
} |
| 2436 |
|
| 2437 |
|
| 2438 |
if (old == *dtls_setup) { |
| 2439 |
return; |
| 2440 |
} |
| 2441 |
|
| 2442 |
|
| 2443 |
if (*dtls_setup == AST_RTP_DTLS_SETUP_HOLDCONN) { |
| 2444 |
return; |
| 2445 |
} |
| 2446 |
|
| 2447 |
if (*dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) { |
| 2448 |
SSL_set_connect_state(ssl); |
| 2449 |
} else if (*dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) { |
| 2450 |
SSL_set_accept_state(ssl); |
| 2451 |
} else { |
| 2452 |
return; |
| 2453 |
} |
| 2454 |
} |
| 2455 |
|
| 2456 |
|
| 2457 |
static void ast_rtp_dtls_set_setup(struct ast_rtp_instance *instance, enum ast_rtp_dtls_setup setup) |
| 2458 |
{ |
| 2459 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2460 |
|
| 2461 |
if (rtp->dtls.ssl) { |
| 2462 |
dtls_set_setup(&rtp->dtls.dtls_setup, setup, rtp->dtls.ssl); |
| 2463 |
} |
| 2464 |
|
| 2465 |
if (rtp->rtcp && rtp->rtcp->dtls.ssl) { |
| 2466 |
dtls_set_setup(&rtp->rtcp->dtls.dtls_setup, setup, rtp->rtcp->dtls.ssl); |
| 2467 |
} |
| 2468 |
} |
| 2469 |
|
| 2470 |
|
| 2471 |
static void ast_rtp_dtls_set_fingerprint(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash, const char *fingerprint) |
| 2472 |
{ |
| 2473 |
char *tmp = ast_strdupa(fingerprint), *value; |
| 2474 |
int pos = 0; |
| 2475 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2476 |
|
| 2477 |
if (hash != AST_RTP_DTLS_HASH_SHA1 && hash != AST_RTP_DTLS_HASH_SHA256) { |
| 2478 |
return; |
| 2479 |
} |
| 2480 |
|
| 2481 |
rtp->remote_hash = hash; |
| 2482 |
|
| 2483 |
while ((value = strsep(&tmp, ":")) && (pos != (EVP_MAX_MD_SIZE - 1))) { |
| 2484 |
sscanf(value, "%02hhx", &rtp->remote_fingerprint[pos++]); |
| 2485 |
} |
| 2486 |
} |
| 2487 |
|
| 2488 |
|
| 2489 |
static enum ast_rtp_dtls_hash ast_rtp_dtls_get_fingerprint_hash(struct ast_rtp_instance *instance) |
| 2490 |
{ |
| 2491 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2492 |
|
| 2493 |
return rtp->local_hash; |
| 2494 |
} |
| 2495 |
|
| 2496 |
|
| 2497 |
static const char *ast_rtp_dtls_get_fingerprint(struct ast_rtp_instance *instance) |
| 2498 |
{ |
| 2499 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2500 |
|
| 2501 |
return rtp->local_fingerprint; |
| 2502 |
} |
| 2503 |
|
| 2504 |
|
| 2505 |
static struct ast_rtp_engine_dtls ast_rtp_dtls = { |
| 2506 |
.set_configuration = ast_rtp_dtls_set_configuration, |
| 2507 |
.active = ast_rtp_dtls_active, |
| 2508 |
.stop = ast_rtp_dtls_stop, |
| 2509 |
.reset = ast_rtp_dtls_reset, |
| 2510 |
.get_connection = ast_rtp_dtls_get_connection, |
| 2511 |
.get_setup = ast_rtp_dtls_get_setup, |
| 2512 |
.set_setup = ast_rtp_dtls_set_setup, |
| 2513 |
.set_fingerprint = ast_rtp_dtls_set_fingerprint, |
| 2514 |
.get_fingerprint_hash = ast_rtp_dtls_get_fingerprint_hash, |
| 2515 |
.get_fingerprint = ast_rtp_dtls_get_fingerprint, |
| 2516 |
}; |
| 2517 |
|
| 2518 |
#endif |
| 2519 |
|
| 2520 |
#ifdef TEST_FRAMEWORK |
| 2521 |
static size_t get_recv_buffer_count(struct ast_rtp_instance *instance) |
| 2522 |
{ |
| 2523 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2524 |
|
| 2525 |
if (rtp && rtp->recv_buffer) { |
| 2526 |
return ast_data_buffer_count(rtp->recv_buffer); |
| 2527 |
} |
| 2528 |
|
| 2529 |
return 0; |
| 2530 |
} |
| 2531 |
|
| 2532 |
static size_t get_recv_buffer_max(struct ast_rtp_instance *instance) |
| 2533 |
{ |
| 2534 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2535 |
|
| 2536 |
if (rtp && rtp->recv_buffer) { |
| 2537 |
return ast_data_buffer_max(rtp->recv_buffer); |
| 2538 |
} |
| 2539 |
|
| 2540 |
return 0; |
| 2541 |
} |
| 2542 |
|
| 2543 |
static size_t get_send_buffer_count(struct ast_rtp_instance *instance) |
| 2544 |
{ |
| 2545 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2546 |
|
| 2547 |
if (rtp && rtp->send_buffer) { |
| 2548 |
return ast_data_buffer_count(rtp->send_buffer); |
| 2549 |
} |
| 2550 |
|
| 2551 |
return 0; |
| 2552 |
} |
| 2553 |
|
| 2554 |
static void set_rtp_rtcp_schedid(struct ast_rtp_instance *instance, int id) |
| 2555 |
{ |
| 2556 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2557 |
|
| 2558 |
if (rtp && rtp->rtcp) { |
| 2559 |
rtp->rtcp->schedid = id; |
| 2560 |
} |
| 2561 |
} |
| 2562 |
|
| 2563 |
static struct ast_rtp_engine_test ast_rtp_test = { |
| 2564 |
.packets_to_drop = 0, |
| 2565 |
.send_report = 0, |
| 2566 |
.sdes_received = 0, |
| 2567 |
.recv_buffer_count = get_recv_buffer_count, |
| 2568 |
.recv_buffer_max = get_recv_buffer_max, |
| 2569 |
.send_buffer_count = get_send_buffer_count, |
| 2570 |
.set_schedid = set_rtp_rtcp_schedid, |
| 2571 |
}; |
| 2572 |
#endif |
| 2573 |
|
| 2574 |
|
| 2575 |
static struct ast_rtp_engine asterisk_rtp_engine = { |
| 2576 |
.name = "asterisk", |
| 2577 |
.new = ast_rtp_new, |
| 2578 |
.destroy = ast_rtp_destroy, |
| 2579 |
.dtmf_begin = ast_rtp_dtmf_begin, |
| 2580 |
.dtmf_end = ast_rtp_dtmf_end, |
| 2581 |
.dtmf_end_with_duration = ast_rtp_dtmf_end_with_duration, |
| 2582 |
.dtmf_mode_set = ast_rtp_dtmf_mode_set, |
| 2583 |
.dtmf_mode_get = ast_rtp_dtmf_mode_get, |
| 2584 |
.update_source = ast_rtp_update_source, |
| 2585 |
.change_source = ast_rtp_change_source, |
| 2586 |
.write = ast_rtp_write, |
| 2587 |
.read = ast_rtp_read, |
| 2588 |
.prop_set = ast_rtp_prop_set, |
| 2589 |
.fd = ast_rtp_fd, |
| 2590 |
.remote_address_set = ast_rtp_remote_address_set, |
| 2591 |
.red_init = rtp_red_init, |
| 2592 |
.red_buffer = rtp_red_buffer, |
| 2593 |
.local_bridge = ast_rtp_local_bridge, |
| 2594 |
.get_stat = ast_rtp_get_stat, |
| 2595 |
.dtmf_compatible = ast_rtp_dtmf_compatible, |
| 2596 |
.stun_request = ast_rtp_stun_request, |
| 2597 |
.stop = ast_rtp_stop, |
| 2598 |
.qos = ast_rtp_qos_set, |
| 2599 |
.sendcng = ast_rtp_sendcng, |
| 2600 |
#ifdef HAVE_PJPROJECT |
| 2601 |
.ice = &ast_rtp_ice, |
| 2602 |
#endif |
| 2603 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 2604 |
.dtls = &ast_rtp_dtls, |
| 2605 |
.activate = ast_rtp_activate, |
| 2606 |
#endif |
| 2607 |
.ssrc_get = ast_rtp_get_ssrc, |
| 2608 |
.cname_get = ast_rtp_get_cname, |
| 2609 |
.set_remote_ssrc = ast_rtp_set_remote_ssrc, |
| 2610 |
.set_stream_num = ast_rtp_set_stream_num, |
| 2611 |
.extension_enable = ast_rtp_extension_enable, |
| 2612 |
.bundle = ast_rtp_bundle, |
| 2613 |
#ifdef TEST_FRAMEWORK |
| 2614 |
.test = &ast_rtp_test, |
| 2615 |
#endif |
| 2616 |
}; |
| 2617 |
|
| 2618 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 2619 |
|
| 2620 |
static void dtls_perform_handshake(struct ast_rtp_instance *instance, struct dtls_details *dtls, int rtcp) |
| 2621 |
{ |
| 2622 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2623 |
|
| 2624 |
ast_debug_dtls(3, "(%p) DTLS perform handshake - ssl = %p, setup = %d\n", |
| 2625 |
rtp, dtls->ssl, dtls->dtls_setup); |
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
if (!dtls->ssl || (dtls->dtls_setup != AST_RTP_DTLS_SETUP_ACTIVE)) { |
| 2632 |
return; |
| 2633 |
} |
| 2634 |
|
| 2635 |
SSL_do_handshake(dtls->ssl); |
| 2636 |
|
| 2637 |
|
| 2638 |
|
| 2639 |
|
| 2640 |
|
| 2641 |
|
| 2642 |
|
| 2643 |
|
| 2644 |
|
| 2645 |
dtls_srtp_start_timeout_timer(instance, rtp, rtcp); |
| 2646 |
} |
| 2647 |
#endif |
| 2648 |
|
| 2649 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 2650 |
static void dtls_perform_setup(struct dtls_details *dtls) |
| 2651 |
{ |
| 2652 |
if (!dtls->ssl || !SSL_is_init_finished(dtls->ssl)) { |
| 2653 |
return; |
| 2654 |
} |
| 2655 |
|
| 2656 |
SSL_clear(dtls->ssl); |
| 2657 |
if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) { |
| 2658 |
SSL_set_accept_state(dtls->ssl); |
| 2659 |
} else { |
| 2660 |
SSL_set_connect_state(dtls->ssl); |
| 2661 |
} |
| 2662 |
dtls->connection = AST_RTP_DTLS_CONNECTION_NEW; |
| 2663 |
|
| 2664 |
ast_debug_dtls(3, "DTLS perform setup - connection reset\n"); |
| 2665 |
} |
| 2666 |
#endif |
| 2667 |
|
| 2668 |
#ifdef HAVE_PJPROJECT |
| 2669 |
static void rtp_learning_start(struct ast_rtp *rtp); |
| 2670 |
|
| 2671 |
|
| 2672 |
static void ast_rtp_ice_start_media(pj_ice_sess *ice, pj_status_t status) |
| 2673 |
{ |
| 2674 |
struct ast_rtp_instance *instance = ice->user_data; |
| 2675 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2676 |
|
| 2677 |
ao2_lock(instance); |
| 2678 |
|
| 2679 |
if (status == PJ_SUCCESS) { |
| 2680 |
struct ast_sockaddr remote_address; |
| 2681 |
|
| 2682 |
ast_sockaddr_setnull(&remote_address); |
| 2683 |
update_address_with_ice_candidate(ice, AST_RTP_ICE_COMPONENT_RTP, &remote_address); |
| 2684 |
if (!ast_sockaddr_isnull(&remote_address)) { |
| 2685 |
|
| 2686 |
ast_rtp_instance_set_prop(instance, AST_RTP_PROPERTY_NAT, 0); |
| 2687 |
|
| 2688 |
ast_rtp_instance_set_remote_address(instance, &remote_address); |
| 2689 |
} |
| 2690 |
|
| 2691 |
if (rtp->rtcp) { |
| 2692 |
update_address_with_ice_candidate(ice, AST_RTP_ICE_COMPONENT_RTCP, &rtp->rtcp->them); |
| 2693 |
} |
| 2694 |
} |
| 2695 |
|
| 2696 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 2697 |
|
| 2698 |
if (rtp->ice_media_started) { |
| 2699 |
ao2_unlock(instance); |
| 2700 |
return; |
| 2701 |
} |
| 2702 |
|
| 2703 |
ast_debug_category(2, AST_DEBUG_CATEGORY_ICE | AST_DEBUG_CATEGORY_DTLS, |
| 2704 |
"(%p) ICE starting media - perform DTLS - (%p)\n", instance, rtp); |
| 2705 |
|
| 2706 |
|
| 2707 |
|
| 2708 |
|
| 2709 |
|
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
|
| 2715 |
|
| 2716 |
|
| 2717 |
|
| 2718 |
dtls_perform_handshake(instance, &rtp->dtls, 0); |
| 2719 |
|
| 2720 |
if (rtp->rtcp && rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) { |
| 2721 |
dtls_perform_handshake(instance, &rtp->rtcp->dtls, 1); |
| 2722 |
} |
| 2723 |
#endif |
| 2724 |
|
| 2725 |
rtp->ice_media_started = 1; |
| 2726 |
|
| 2727 |
if (!strictrtp) { |
| 2728 |
ao2_unlock(instance); |
| 2729 |
return; |
| 2730 |
} |
| 2731 |
|
| 2732 |
ast_verb(4, "%p -- Strict RTP learning after ICE completion\n", rtp); |
| 2733 |
rtp_learning_start(rtp); |
| 2734 |
ao2_unlock(instance); |
| 2735 |
} |
| 2736 |
|
| 2737 |
#ifdef HAVE_PJPROJECT_ON_VALID_ICE_PAIR_CALLBACK |
| 2738 |
|
| 2739 |
static void ast_rtp_on_valid_pair(pj_ice_sess *ice) |
| 2740 |
{ |
| 2741 |
ast_debug_ice(2, "(%p) ICE valid pair, start media\n", ice->user_data); |
| 2742 |
ast_rtp_ice_start_media(ice, PJ_SUCCESS); |
| 2743 |
} |
| 2744 |
#endif |
| 2745 |
|
| 2746 |
|
| 2747 |
static void ast_rtp_on_ice_complete(pj_ice_sess *ice, pj_status_t status) |
| 2748 |
{ |
| 2749 |
ast_debug_ice(2, "(%p) ICE complete, start media\n", ice->user_data); |
| 2750 |
ast_rtp_ice_start_media(ice, status); |
| 2751 |
} |
| 2752 |
|
| 2753 |
|
| 2754 |
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) |
| 2755 |
{ |
| 2756 |
struct ast_rtp_instance *instance = ice->user_data; |
| 2757 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
if (transport_id == TRANSPORT_SOCKET_RTP || transport_id == TRANSPORT_SOCKET_RTCP) { |
| 2762 |
rtp->passthrough = 1; |
| 2763 |
} else if (transport_id == TRANSPORT_TURN_RTP) { |
| 2764 |
rtp->rtp_passthrough = 1; |
| 2765 |
} else if (transport_id == TRANSPORT_TURN_RTCP) { |
| 2766 |
rtp->rtcp_passthrough = 1; |
| 2767 |
} |
| 2768 |
} |
| 2769 |
|
| 2770 |
|
| 2771 |
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) |
| 2772 |
{ |
| 2773 |
struct ast_rtp_instance *instance = ice->user_data; |
| 2774 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2775 |
pj_status_t status = PJ_EINVALIDOP; |
| 2776 |
pj_ssize_t _size = (pj_ssize_t)size; |
| 2777 |
|
| 2778 |
if (transport_id == TRANSPORT_SOCKET_RTP) { |
| 2779 |
|
| 2780 |
status = pj_sock_sendto(rtp->s, pkt, &_size, 0, dst_addr, dst_addr_len); |
| 2781 |
|
| 2782 |
ast_assert(_size == size || status != PJ_SUCCESS); |
| 2783 |
} else if (transport_id == TRANSPORT_SOCKET_RTCP) { |
| 2784 |
|
| 2785 |
if (rtp->rtcp) { |
| 2786 |
status = pj_sock_sendto(rtp->rtcp->s, pkt, &_size, 0, dst_addr, dst_addr_len); |
| 2787 |
|
| 2788 |
ast_assert(_size == size || status != PJ_SUCCESS); |
| 2789 |
} else { |
| 2790 |
status = PJ_SUCCESS; |
| 2791 |
} |
| 2792 |
} else if (transport_id == TRANSPORT_TURN_RTP) { |
| 2793 |
|
| 2794 |
if (rtp->turn_rtp) { |
| 2795 |
status = pj_turn_sock_sendto(rtp->turn_rtp, pkt, size, dst_addr, dst_addr_len); |
| 2796 |
} |
| 2797 |
} else if (transport_id == TRANSPORT_TURN_RTCP) { |
| 2798 |
|
| 2799 |
if (rtp->turn_rtcp) { |
| 2800 |
status = pj_turn_sock_sendto(rtp->turn_rtcp, pkt, size, dst_addr, dst_addr_len); |
| 2801 |
} |
| 2802 |
} |
| 2803 |
|
| 2804 |
return status; |
| 2805 |
} |
| 2806 |
|
| 2807 |
|
| 2808 |
static pj_ice_sess_cb ast_rtp_ice_sess_cb = { |
| 2809 |
#ifdef HAVE_PJPROJECT_ON_VALID_ICE_PAIR_CALLBACK |
| 2810 |
.on_valid_pair = ast_rtp_on_valid_pair, |
| 2811 |
#endif |
| 2812 |
.on_ice_complete = ast_rtp_on_ice_complete, |
| 2813 |
.on_rx_data = ast_rtp_on_ice_rx_data, |
| 2814 |
.on_tx_pkt = ast_rtp_on_ice_tx_pkt, |
| 2815 |
}; |
| 2816 |
|
| 2817 |
|
| 2818 |
static int timer_worker_thread(void *data) |
| 2819 |
{ |
| 2820 |
pj_ioqueue_t *ioqueue; |
| 2821 |
|
| 2822 |
if (pj_ioqueue_create(pool, 1, &ioqueue) != PJ_SUCCESS) { |
| 2823 |
return -1; |
| 2824 |
} |
| 2825 |
|
| 2826 |
while (!timer_terminate) { |
| 2827 |
const pj_time_val delay = {0, 10}; |
| 2828 |
|
| 2829 |
pj_timer_heap_poll(timer_heap, NULL); |
| 2830 |
pj_ioqueue_poll(ioqueue, &delay); |
| 2831 |
} |
| 2832 |
|
| 2833 |
return 0; |
| 2834 |
} |
| 2835 |
#endif |
| 2836 |
|
| 2837 |
static inline int rtp_debug_test_addr(struct ast_sockaddr *addr) |
| 2838 |
{ |
| 2839 |
if (!ast_debug_rtp_packet_is_allowed) { |
| 2840 |
return 0; |
| 2841 |
} |
| 2842 |
if (!ast_sockaddr_isnull(&rtpdebugaddr)) { |
| 2843 |
if (rtpdebugport) { |
| 2844 |
return (ast_sockaddr_cmp(&rtpdebugaddr, addr) == 0); |
| 2845 |
} else { |
| 2846 |
return (ast_sockaddr_cmp_addr(&rtpdebugaddr, addr) == 0); |
| 2847 |
} |
| 2848 |
} |
| 2849 |
|
| 2850 |
return 1; |
| 2851 |
} |
| 2852 |
|
| 2853 |
static inline int rtcp_debug_test_addr(struct ast_sockaddr *addr) |
| 2854 |
{ |
| 2855 |
if (!ast_debug_rtcp_packet_is_allowed) { |
| 2856 |
return 0; |
| 2857 |
} |
| 2858 |
if (!ast_sockaddr_isnull(&rtcpdebugaddr)) { |
| 2859 |
if (rtcpdebugport) { |
| 2860 |
return (ast_sockaddr_cmp(&rtcpdebugaddr, addr) == 0); |
| 2861 |
} else { |
| 2862 |
return (ast_sockaddr_cmp_addr(&rtcpdebugaddr, addr) == 0); |
| 2863 |
} |
| 2864 |
} |
| 2865 |
|
| 2866 |
return 1; |
| 2867 |
} |
| 2868 |
|
| 2869 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 2870 |
|
| 2871 |
|
| 2872 |
|
| 2873 |
|
| 2874 |
|
| 2875 |
|
| 2876 |
|
| 2877 |
|
| 2878 |
|
| 2879 |
|
| 2880 |
|
| 2881 |
|
| 2882 |
|
| 2883 |
|
| 2884 |
|
| 2885 |
|
| 2886 |
static int dtls_srtp_handle_timeout(struct ast_rtp_instance *instance, int *timeout, int rtcp) |
| 2887 |
{ |
| 2888 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 2889 |
struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls; |
| 2890 |
struct timeval dtls_timeout; |
| 2891 |
int res = 0; |
| 2892 |
|
| 2893 |
res = DTLSv1_handle_timeout(dtls->ssl); |
| 2894 |
ast_debug_dtls(3, "(%p) DTLS srtp - handle timeout - rtcp=%d result: %d\n", instance, rtcp, res); |
| 2895 |
|
| 2896 |
|
| 2897 |
res = DTLSv1_get_timeout(dtls->ssl, &dtls_timeout); |
| 2898 |
if (!res) { |
| 2899 |
|
| 2900 |
dtls->timeout_timer = -1; |
| 2901 |
ao2_ref(instance, -1); |
| 2902 |
*timeout = 0; |
| 2903 |
ast_debug_dtls(3, "(%p) DTLS srtp - handle timeout - rtcp=%d get timeout failure\n", instance, rtcp); |
| 2904 |
return -1; |
| 2905 |
} |
| 2906 |
*timeout = dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000; |
| 2907 |
if (*timeout == 0) { |
| 2908 |
|
| 2909 |
|
| 2910 |
|
| 2911 |
|
| 2912 |
|
| 2913 |
|
| 2914 |
|
| 2915 |
|
| 2916 |
|
| 2917 |
|
| 2918 |
|
| 2919 |
|
| 2920 |
|
| 2921 |
|
| 2922 |
|
| 2923 |
|
| 2924 |
|
| 2925 |
*timeout = 1; |
| 2926 |
} |
| 2927 |
ast_debug_dtls(3, "(%p) DTLS srtp - handle timeout - rtcp=%d timeout=%d\n", instance, rtcp, *timeout); |
| 2928 |
|
| 2929 |
return 0; |
| 2930 |
} |
| 2931 |
|
| 2932 |
|
| 2933 |
static int dtls_srtp_handle_rtp_timeout(const void *data) |
| 2934 |
{ |
| 2935 |
struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data; |
| 2936 |
int timeout = 0; |
| 2937 |
int res = 0; |
| 2938 |
|
| 2939 |
ao2_lock(instance); |
| 2940 |
res = dtls_srtp_handle_timeout(instance, &timeout, 0); |
| 2941 |
ao2_unlock(instance); |
| 2942 |
if (res < 0) { |
| 2943 |
|
| 2944 |
return 0; |
| 2945 |
} |
| 2946 |
|
| 2947 |
|
| 2948 |
return timeout; |
| 2949 |
} |
| 2950 |
|
| 2951 |
|
| 2952 |
static int dtls_srtp_handle_rtcp_timeout(const void *data) |
| 2953 |
{ |
| 2954 |
struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data; |
| 2955 |
int timeout = 0; |
| 2956 |
int res = 0; |
| 2957 |
|
| 2958 |
ao2_lock(instance); |
| 2959 |
res = dtls_srtp_handle_timeout(instance, &timeout, 1); |
| 2960 |
ao2_unlock(instance); |
| 2961 |
if (res < 0) { |
| 2962 |
|
| 2963 |
return 0; |
| 2964 |
} |
| 2965 |
|
| 2966 |
|
| 2967 |
return timeout; |
| 2968 |
} |
| 2969 |
|
| 2970 |
static void dtls_srtp_start_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp) |
| 2971 |
{ |
| 2972 |
struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls; |
| 2973 |
ast_sched_cb cb = !rtcp ? dtls_srtp_handle_rtp_timeout : dtls_srtp_handle_rtcp_timeout; |
| 2974 |
struct timeval dtls_timeout; |
| 2975 |
int res = 0; |
| 2976 |
int timeout = 0; |
| 2977 |
|
| 2978 |
ast_assert(dtls->timeout_timer == -1); |
| 2979 |
|
| 2980 |
res = DTLSv1_get_timeout(dtls->ssl, &dtls_timeout); |
| 2981 |
if (res == 0) { |
| 2982 |
ast_debug_dtls(3, "(%p) DTLS srtp - DTLSv1_get_timeout return an error or there was no timeout set for %s\n", |
| 2983 |
instance, rtcp ? "RTCP" : "RTP"); |
| 2984 |
return; |
| 2985 |
} |
| 2986 |
|
| 2987 |
timeout = dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000; |
| 2988 |
|
| 2989 |
ao2_ref(instance, +1); |
| 2990 |
|
| 2991 |
|
| 2992 |
|
| 2993 |
|
| 2994 |
if ((dtls->timeout_timer = ast_sched_add_variable(rtp->sched, timeout, cb, instance, 1)) < 0) { |
| 2995 |
ao2_ref(instance, -1); |
| 2996 |
ast_log(LOG_WARNING, "Scheduling '%s' DTLS retransmission for RTP instance [%p] failed.\n", |
| 2997 |
!rtcp ? "RTP" : "RTCP", instance); |
| 2998 |
} else { |
| 2999 |
ast_debug_dtls(3, "(%p) DTLS srtp - scheduled timeout timer for '%d' %s\n", |
| 3000 |
instance, timeout, rtcp ? "RTCP" : "RTP"); |
| 3001 |
} |
| 3002 |
} |
| 3003 |
|
| 3004 |
|
| 3005 |
static void dtls_srtp_stop_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp) |
| 3006 |
{ |
| 3007 |
struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls; |
| 3008 |
|
| 3009 |
AST_SCHED_DEL_UNREF(rtp->sched, dtls->timeout_timer, ao2_ref(instance, -1)); |
| 3010 |
ast_debug_dtls(3, "(%p) DTLS srtp - stopped timeout timer'\n", instance); |
| 3011 |
} |
| 3012 |
|
| 3013 |
|
| 3014 |
static int dtls_srtp_renegotiate(const void *data) |
| 3015 |
{ |
| 3016 |
struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data; |
| 3017 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 3018 |
|
| 3019 |
ao2_lock(instance); |
| 3020 |
|
| 3021 |
ast_debug_dtls(3, "(%p) DTLS srtp - renegotiate'\n", instance); |
| 3022 |
SSL_renegotiate(rtp->dtls.ssl); |
| 3023 |
SSL_do_handshake(rtp->dtls.ssl); |
| 3024 |
|
| 3025 |
if (rtp->rtcp && rtp->rtcp->dtls.ssl && rtp->rtcp->dtls.ssl != rtp->dtls.ssl) { |
| 3026 |
SSL_renegotiate(rtp->rtcp->dtls.ssl); |
| 3027 |
SSL_do_handshake(rtp->rtcp->dtls.ssl); |
| 3028 |
} |
| 3029 |
|
| 3030 |
rtp->rekeyid = -1; |
| 3031 |
|
| 3032 |
ao2_unlock(instance); |
| 3033 |
ao2_ref(instance, -1); |
| 3034 |
|
| 3035 |
return 0; |
| 3036 |
} |
| 3037 |
|
| 3038 |
static int dtls_srtp_add_local_ssrc(struct ast_rtp *rtp, struct ast_rtp_instance *instance, int rtcp, unsigned int ssrc, int set_remote_policy) |
| 3039 |
{ |
| 3040 |
unsigned char material[SRTP_MASTER_LEN * 2]; |
| 3041 |
unsigned char *local_key, *local_salt, *remote_key, *remote_salt; |
| 3042 |
struct ast_srtp_policy *local_policy, *remote_policy = NULL; |
| 3043 |
int res = -1; |
| 3044 |
struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls; |
| 3045 |
|
| 3046 |
ast_debug_dtls(3, "(%p) DTLS srtp - add local ssrc - rtcp=%d, set_remote_policy=%d'\n", |
| 3047 |
instance, rtcp, set_remote_policy); |
| 3048 |
|
| 3049 |
|
| 3050 |
if (!SSL_export_keying_material(dtls->ssl, material, SRTP_MASTER_LEN * 2, "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) { |
| 3051 |
ast_log(LOG_WARNING, "Unable to extract SRTP keying material from DTLS-SRTP negotiation on RTP instance '%p'\n", |
| 3052 |
instance); |
| 3053 |
return -1; |
| 3054 |
} |
| 3055 |
|
| 3056 |
|
| 3057 |
if (rtp->dtls.dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) { |
| 3058 |
local_key = material; |
| 3059 |
remote_key = local_key + SRTP_MASTER_KEY_LEN; |
| 3060 |
local_salt = remote_key + SRTP_MASTER_KEY_LEN; |
| 3061 |
remote_salt = local_salt + SRTP_MASTER_SALT_LEN; |
| 3062 |
} else { |
| 3063 |
remote_key = material; |
| 3064 |
local_key = remote_key + SRTP_MASTER_KEY_LEN; |
| 3065 |
remote_salt = local_key + SRTP_MASTER_KEY_LEN; |
| 3066 |
local_salt = remote_salt + SRTP_MASTER_SALT_LEN; |
| 3067 |
} |
| 3068 |
|
| 3069 |
if (!(local_policy = res_srtp_policy->alloc())) { |
| 3070 |
return -1; |
| 3071 |
} |
| 3072 |
|
| 3073 |
if (res_srtp_policy->set_master_key(local_policy, local_key, SRTP_MASTER_KEY_LEN, local_salt, SRTP_MASTER_SALT_LEN) < 0) { |
| 3074 |
ast_log(LOG_WARNING, "Could not set key/salt information on local policy of '%p' when setting up DTLS-SRTP\n", rtp); |
| 3075 |
goto error; |
| 3076 |
} |
| 3077 |
|
| 3078 |
if (res_srtp_policy->set_suite(local_policy, rtp->suite)) { |
| 3079 |
ast_log(LOG_WARNING, "Could not set suite to '%u' on local policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp); |
| 3080 |
goto error; |
| 3081 |
} |
| 3082 |
|
| 3083 |
res_srtp_policy->set_ssrc(local_policy, ssrc, 0); |
| 3084 |
|
| 3085 |
if (set_remote_policy) { |
| 3086 |
if (!(remote_policy = res_srtp_policy->alloc())) { |
| 3087 |
goto error; |
| 3088 |
} |
| 3089 |
|
| 3090 |
if (res_srtp_policy->set_master_key(remote_policy, remote_key, SRTP_MASTER_KEY_LEN, remote_salt, SRTP_MASTER_SALT_LEN) < 0) { |
| 3091 |
ast_log(LOG_WARNING, "Could not set key/salt information on remote policy of '%p' when setting up DTLS-SRTP\n", rtp); |
| 3092 |
goto error; |
| 3093 |
} |
| 3094 |
|
| 3095 |
if (res_srtp_policy->set_suite(remote_policy, rtp->suite)) { |
| 3096 |
ast_log(LOG_WARNING, "Could not set suite to '%u' on remote policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp); |
| 3097 |
goto error; |
| 3098 |
} |
| 3099 |
|
| 3100 |
res_srtp_policy->set_ssrc(remote_policy, 0, 1); |
| 3101 |
} |
| 3102 |
|
| 3103 |
if (ast_rtp_instance_add_srtp_policy(instance, remote_policy, local_policy, rtcp)) { |
| 3104 |
ast_log(LOG_WARNING, "Could not set policies when setting up DTLS-SRTP on '%p'\n", rtp); |
| 3105 |
goto error; |
| 3106 |
} |
| 3107 |
|
| 3108 |
res = 0; |
| 3109 |
|
| 3110 |
error: |
| 3111 |
|
| 3112 |
res_srtp_policy->destroy(local_policy); |
| 3113 |
|
| 3114 |
if (remote_policy) { |
| 3115 |
res_srtp_policy->destroy(remote_policy); |
| 3116 |
} |
| 3117 |
|
| 3118 |
return res; |
| 3119 |
} |
| 3120 |
|
| 3121 |
static int dtls_srtp_setup(struct ast_rtp *rtp, struct ast_rtp_instance *instance, int rtcp) |
| 3122 |
{ |
| 3123 |
struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls; |
| 3124 |
int index; |
| 3125 |
|
| 3126 |
ast_debug_dtls(3, "(%p) DTLS setup SRTP rtp=%p'\n", instance, rtp); |
| 3127 |
|
| 3128 |
|
| 3129 |
if (rtp->dtls_verify & AST_RTP_DTLS_VERIFY_FINGERPRINT) { |
| 3130 |
X509 *certificate; |
| 3131 |
|
| 3132 |
if (!(certificate = SSL_get_peer_certificate(dtls->ssl))) { |
| 3133 |
ast_log(LOG_WARNING, "No certificate was provided by the peer on RTP instance '%p'\n", instance); |
| 3134 |
return -1; |
| 3135 |
} |
| 3136 |
|
| 3137 |
|
| 3138 |
if (rtp->remote_fingerprint[0]) { |
| 3139 |
const EVP_MD *type; |
| 3140 |
unsigned char fingerprint[EVP_MAX_MD_SIZE]; |
| 3141 |
unsigned int size; |
| 3142 |
|
| 3143 |
if (rtp->remote_hash == AST_RTP_DTLS_HASH_SHA1) { |
| 3144 |
type = EVP_sha1(); |
| 3145 |
} else if (rtp->remote_hash == AST_RTP_DTLS_HASH_SHA256) { |
| 3146 |
type = EVP_sha256(); |
| 3147 |
} else { |
| 3148 |
ast_log(LOG_WARNING, "Unsupported fingerprint hash type on RTP instance '%p'\n", instance); |
| 3149 |
return -1; |
| 3150 |
} |
| 3151 |
|
| 3152 |
if (!X509_digest(certificate, type, fingerprint, &size) || |
| 3153 |
!size || |
| 3154 |
memcmp(fingerprint, rtp->remote_fingerprint, size)) { |
| 3155 |
X509_free(certificate); |
| 3156 |
ast_log(LOG_WARNING, "Fingerprint provided by remote party does not match that of peer certificate on RTP instance '%p'\n", |
| 3157 |
instance); |
| 3158 |
return -1; |
| 3159 |
} |
| 3160 |
} |
| 3161 |
|
| 3162 |
X509_free(certificate); |
| 3163 |
} |
| 3164 |
|
| 3165 |
if (dtls_srtp_add_local_ssrc(rtp, instance, rtcp, ast_rtp_instance_get_ssrc(instance), 1)) { |
| 3166 |
ast_log(LOG_ERROR, "Failed to add local source '%p'\n", rtp); |
| 3167 |
return -1; |
| 3168 |
} |
| 3169 |
|
| 3170 |
for (index = 0; index < AST_VECTOR_SIZE(&rtp->ssrc_mapping); ++index) { |
| 3171 |
struct rtp_ssrc_mapping *mapping = AST_VECTOR_GET_ADDR(&rtp->ssrc_mapping, index); |
| 3172 |
|
| 3173 |
if (dtls_srtp_add_local_ssrc(rtp, instance, rtcp, ast_rtp_instance_get_ssrc(mapping->instance), 0)) { |
| 3174 |
return -1; |
| 3175 |
} |
| 3176 |
} |
| 3177 |
|
| 3178 |
if (rtp->rekey) { |
| 3179 |
ao2_ref(instance, +1); |
| 3180 |
if ((rtp->rekeyid = ast_sched_add(rtp->sched, rtp->rekey * 1000, dtls_srtp_renegotiate, instance)) < 0) { |
| 3181 |
ao2_ref(instance, -1); |
| 3182 |
return -1; |
| 3183 |
} |
| 3184 |
} |
| 3185 |
|
| 3186 |
return 0; |
| 3187 |
} |
| 3188 |
#endif |
| 3189 |
|
| 3190 |
|
| 3191 |
static int compare_by_value(int elem, int value) |
| 3192 |
{ |
| 3193 |
return elem - value; |
| 3194 |
} |
| 3195 |
|
| 3196 |
|
| 3197 |
static int find_by_value(int elem, int value) |
| 3198 |
{ |
| 3199 |
return elem == value; |
| 3200 |
} |
| 3201 |
|
| 3202 |
static int rtcp_mux(struct ast_rtp *rtp, const unsigned char *packet) |
| 3203 |
{ |
| 3204 |
uint8_t version; |
| 3205 |
uint8_t pt; |
| 3206 |
uint8_t m; |
| 3207 |
|
| 3208 |
if (!rtp->rtcp || rtp->rtcp->type != AST_RTP_INSTANCE_RTCP_MUX) { |
| 3209 |
return 0; |
| 3210 |
} |
| 3211 |
|
| 3212 |
version = (packet[0] & 0XC0) >> 6; |
| 3213 |
if (version == 0) { |
| 3214 |
|
| 3215 |
|
| 3216 |
|
| 3217 |
return 0; |
| 3218 |
} |
| 3219 |
|
| 3220 |
|
| 3221 |
|
| 3222 |
|
| 3223 |
|
| 3224 |
|
| 3225 |
|
| 3226 |
|
| 3227 |
m = packet[1] & 0x80; |
| 3228 |
pt = packet[1] & 0x7F; |
| 3229 |
if (m && pt >= 64 && pt <= 95) { |
| 3230 |
return 1; |
| 3231 |
} |
| 3232 |
return 0; |
| 3233 |
} |
| 3234 |
|
| 3235 |
|
| 3236 |
static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp) |
| 3237 |
{ |
| 3238 |
int len; |
| 3239 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 3240 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 3241 |
char *in = buf; |
| 3242 |
#endif |
| 3243 |
#ifdef HAVE_PJPROJECT |
| 3244 |
struct ast_sockaddr *loop = rtcp ? &rtp->rtcp_loop : &rtp->rtp_loop; |
| 3245 |
#endif |
| 3246 |
#ifdef TEST_FRAMEWORK |
| 3247 |
struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance); |
| 3248 |
#endif |
| 3249 |
|
| 3250 |
if ((len = ast_recvfrom(rtcp ? rtp->rtcp->s : rtp->s, buf, size, flags, sa)) < 0) { |
| 3251 |
return len; |
| 3252 |
} |
| 3253 |
|
| 3254 |
#ifdef TEST_FRAMEWORK |
| 3255 |
if (test && test->packets_to_drop > 0) { |
| 3256 |
test->packets_to_drop--; |
| 3257 |
return 0; |
| 3258 |
} |
| 3259 |
#endif |
| 3260 |
|
| 3261 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 3262 |
|
| 3263 |
|
| 3264 |
if ((*in >= 20) && (*in <= 63)) { |
| 3265 |
struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls; |
| 3266 |
int res = 0; |
| 3267 |
|
| 3268 |
|
| 3269 |
if (!dtls->ssl) { |
| 3270 |
ast_log(LOG_ERROR, "Received SSL traffic on RTP instance '%p' without an SSL session\n", |
| 3271 |
instance); |
| 3272 |
return -1; |
| 3273 |
} |
| 3274 |
|
| 3275 |
ast_debug_dtls(3, "(%p) DTLS - __rtp_recvfrom rtp=%p - Got SSL packet '%d'\n", instance, rtp, *in); |
| 3276 |
|
| 3277 |
#ifdef HAVE_PJPROJECT |
| 3278 |
|
| 3279 |
|
| 3280 |
|
| 3281 |
|
| 3282 |
if (!ast_sockaddr_isnull(&rtp->rtp_loop) && !ast_sockaddr_cmp(&rtp->rtp_loop, sa)) { |
| 3283 |
ast_rtp_instance_get_remote_address(instance, sa); |
| 3284 |
} else if (rtcp && !ast_sockaddr_isnull(&rtp->rtcp_loop) && !ast_sockaddr_cmp(&rtp->rtcp_loop, sa)) { |
| 3285 |
ast_sockaddr_copy(sa, &rtp->rtcp->them); |
| 3286 |
} |
| 3287 |
#endif |
| 3288 |
|
| 3289 |
|
| 3290 |
|
| 3291 |
|
| 3292 |
|
| 3293 |
|
| 3294 |
|
| 3295 |
|
| 3296 |
#ifdef HAVE_PJPROJECT |
| 3297 |
if (rtp->ice) { |
| 3298 |
int pass_src_check = 0; |
| 3299 |
int ix = 0; |
| 3300 |
|
| 3301 |
|
| 3302 |
|
| 3303 |
|
| 3304 |
|
| 3305 |
|
| 3306 |
|
| 3307 |
|
| 3308 |
|
| 3309 |
|
| 3310 |
|
| 3311 |
|
| 3312 |
if (!rtp->ice_media_started) { |
| 3313 |
ast_log(LOG_WARNING, "%s: DTLS packet from %s dropped. ICE not completed yet.\n", |
| 3314 |
ast_rtp_instance_get_channel_id(instance), |
| 3315 |
ast_sockaddr_stringify(sa)); |
| 3316 |
return 0; |
| 3317 |
} |
| 3318 |
|
| 3319 |
|
| 3320 |
|
| 3321 |
|
| 3322 |
|
| 3323 |
|
| 3324 |
|
| 3325 |
for (ix = 0; ix < rtp->ice->real_ice->rcand_cnt; ix++) { |
| 3326 |
pj_ice_sess_cand *rcand = &rtp->ice->real_ice->rcand[ix]; |
| 3327 |
if (ast_sockaddr_pj_sockaddr_cmp(sa, &rcand->addr) == 0) { |
| 3328 |
pass_src_check = 1; |
| 3329 |
break; |
| 3330 |
} |
| 3331 |
} |
| 3332 |
|
| 3333 |
if (!pass_src_check) { |
| 3334 |
ast_log(LOG_WARNING, "%s: DTLS packet from %s dropped. Source not in ICE active candidate list.\n", |
| 3335 |
ast_rtp_instance_get_channel_id(instance), |
| 3336 |
ast_sockaddr_stringify(sa)); |
| 3337 |
return 0; |
| 3338 |
} |
| 3339 |
} |
| 3340 |
#endif |
| 3341 |
|
| 3342 |
|
| 3343 |
|
| 3344 |
|
| 3345 |
|
| 3346 |
|
| 3347 |
|
| 3348 |
|
| 3349 |
|
| 3350 |
|
| 3351 |
ao2_unlock(instance); |
| 3352 |
dtls_srtp_stop_timeout_timer(instance, rtp, rtcp); |
| 3353 |
ao2_lock(instance); |
| 3354 |
|
| 3355 |
|
| 3356 |
if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) { |
| 3357 |
dtls->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE; |
| 3358 |
SSL_set_accept_state(dtls->ssl); |
| 3359 |
} |
| 3360 |
|
| 3361 |
BIO_write(dtls->read_bio, buf, len); |
| 3362 |
|
| 3363 |
len = SSL_read(dtls->ssl, buf, len); |
| 3364 |
|
| 3365 |
if ((len < 0) && (SSL_get_error(dtls->ssl, len) == SSL_ERROR_SSL)) { |
| 3366 |
unsigned long error = ERR_get_error(); |
| 3367 |
ast_log(LOG_ERROR, "DTLS failure occurred on RTP instance '%p' due to reason '%s', terminating\n", |
| 3368 |
instance, ERR_reason_error_string(error)); |
| 3369 |
return -1; |
| 3370 |
} |
| 3371 |
|
| 3372 |
if (SSL_is_init_finished(dtls->ssl)) { |
| 3373 |
|
| 3374 |
dtls->connection = AST_RTP_DTLS_CONNECTION_EXISTING; |
| 3375 |
|
| 3376 |
if ((res = dtls_srtp_setup(rtp, instance, rtcp))) { |
| 3377 |
return res; |
| 3378 |
} |
| 3379 |
|
| 3380 |
res = RTP_DTLS_ESTABLISHED; |
| 3381 |
|
| 3382 |
ast_debug_dtls(3, "(%p) DTLS - __rtp_recvfrom rtp=%p - established'\n", instance, rtp); |
| 3383 |
} else { |
| 3384 |
|
| 3385 |
dtls_srtp_start_timeout_timer(instance, rtp, rtcp); |
| 3386 |
} |
| 3387 |
|
| 3388 |
return res; |
| 3389 |
} |
| 3390 |
#endif |
| 3391 |
|
| 3392 |
#ifdef HAVE_PJPROJECT |
| 3393 |
if (!ast_sockaddr_isnull(loop) && !ast_sockaddr_cmp(loop, sa)) { |
| 3394 |
|
| 3395 |
|
| 3396 |
|
| 3397 |
if (rtcp) { |
| 3398 |
ast_sockaddr_copy(sa, &rtp->rtcp->them); |
| 3399 |
} else { |
| 3400 |
ast_rtp_instance_get_remote_address(instance, sa); |
| 3401 |
} |
| 3402 |
} else if (rtp->ice) { |
| 3403 |
pj_str_t combined = pj_str(ast_sockaddr_stringify(sa)); |
| 3404 |
pj_sockaddr address; |
| 3405 |
pj_status_t status; |
| 3406 |
struct ice_wrap *ice; |
| 3407 |
|
| 3408 |
pj_thread_register_check(); |
| 3409 |
|
| 3410 |
pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &combined, &address); |
| 3411 |
|
| 3412 |
|
| 3413 |
ice = rtp->ice; |
| 3414 |
ao2_ref(ice, +1); |
| 3415 |
ao2_unlock(instance); |
| 3416 |
status = pj_ice_sess_on_rx_pkt(ice->real_ice, |
| 3417 |
rtcp ? AST_RTP_ICE_COMPONENT_RTCP : AST_RTP_ICE_COMPONENT_RTP, |
| 3418 |
rtcp ? TRANSPORT_SOCKET_RTCP : TRANSPORT_SOCKET_RTP, buf, len, &address, |
| 3419 |
pj_sockaddr_get_len(&address)); |
| 3420 |
ao2_ref(ice, -1); |
| 3421 |
ao2_lock(instance); |
| 3422 |
if (status != PJ_SUCCESS) { |
| 3423 |
char err_buf[100]; |
| 3424 |
|
| 3425 |
pj_strerror(status, err_buf, sizeof(err_buf)); |
| 3426 |
ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n", |
| 3427 |
(int)status, err_buf); |
| 3428 |
return -1; |
| 3429 |
} |
| 3430 |
if (!rtp->passthrough) { |
| 3431 |
|
| 3432 |
|
| 3433 |
|
| 3434 |
|
| 3435 |
if (!rtp->ice_active_remote_candidates && !rtp->ice_proposed_remote_candidates) { |
| 3436 |
if (rtcp) { |
| 3437 |
ast_sockaddr_copy(&rtp->rtcp->them, sa); |
| 3438 |
} else { |
| 3439 |
ast_rtp_instance_set_remote_address(instance, sa); |
| 3440 |
} |
| 3441 |
} |
| 3442 |
return 0; |
| 3443 |
} |
| 3444 |
rtp->passthrough = 0; |
| 3445 |
} |
| 3446 |
#endif |
| 3447 |
|
| 3448 |
return len; |
| 3449 |
} |
| 3450 |
|
| 3451 |
|
| 3452 |
static int rtcp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa) |
| 3453 |
{ |
| 3454 |
return __rtp_recvfrom(instance, buf, size, flags, sa, 1); |
| 3455 |
} |
| 3456 |
|
| 3457 |
|
| 3458 |
static int rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa) |
| 3459 |
{ |
| 3460 |
return __rtp_recvfrom(instance, buf, size, flags, sa, 0); |
| 3461 |
} |
| 3462 |
|
| 3463 |
|
| 3464 |
static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *via_ice, int use_srtp) |
| 3465 |
{ |
| 3466 |
int len = size; |
| 3467 |
void *temp = buf; |
| 3468 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 3469 |
struct ast_rtp_instance *transport = rtp->bundled ? rtp->bundled : instance; |
| 3470 |
struct ast_rtp *transport_rtp = ast_rtp_instance_get_data(transport); |
| 3471 |
struct ast_srtp *srtp = ast_rtp_instance_get_srtp(transport, rtcp); |
| 3472 |
int res; |
| 3473 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 3474 |
char *out = buf; |
| 3475 |
struct dtls_details *dtls = (!rtcp || rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_MUX) ? &rtp->dtls : &rtp->rtcp->dtls; |
| 3476 |
|
| 3477 |
|
| 3478 |
if (dtls->ssl && ((*out < 20) || (*out > 63)) && dtls->connection == AST_RTP_DTLS_CONNECTION_NEW) { |
| 3479 |
*via_ice = 0; |
| 3480 |
return 0; |
| 3481 |
} |
| 3482 |
#endif |
| 3483 |
|
| 3484 |
*via_ice = 0; |
| 3485 |
|
| 3486 |
if (use_srtp && res_srtp && srtp && res_srtp->protect(srtp, &temp, &len, rtcp) < 0) { |
| 3487 |
return -1; |
| 3488 |
} |
| 3489 |
|
| 3490 |
#ifdef HAVE_PJPROJECT |
| 3491 |
if (transport_rtp->ice) { |
| 3492 |
enum ast_rtp_ice_component_type component = rtcp ? AST_RTP_ICE_COMPONENT_RTCP : AST_RTP_ICE_COMPONENT_RTP; |
| 3493 |
pj_status_t status; |
| 3494 |
struct ice_wrap *ice; |
| 3495 |
|
| 3496 |
|
| 3497 |
if (rtcp && rtp->rtcp->s == rtp->s) { |
| 3498 |
component = AST_RTP_ICE_COMPONENT_RTP; |
| 3499 |
} |
| 3500 |
|
| 3501 |
pj_thread_register_check(); |
| 3502 |
|
| 3503 |
|
| 3504 |
ice = transport_rtp->ice; |
| 3505 |
ao2_ref(ice, +1); |
| 3506 |
if (instance == transport) { |
| 3507 |
ao2_unlock(instance); |
| 3508 |
} |
| 3509 |
status = pj_ice_sess_send_data(ice->real_ice, component, temp, len); |
| 3510 |
ao2_ref(ice, -1); |
| 3511 |
if (instance == transport) { |
| 3512 |
ao2_lock(instance); |
| 3513 |
} |
| 3514 |
if (status == PJ_SUCCESS) { |
| 3515 |
*via_ice = 1; |
| 3516 |
return len; |
| 3517 |
} |
| 3518 |
} |
| 3519 |
#endif |
| 3520 |
|
| 3521 |
res = ast_sendto(rtcp ? transport_rtp->rtcp->s : transport_rtp->s, temp, len, flags, sa); |
| 3522 |
if (res > 0) { |
| 3523 |
ast_rtp_instance_set_last_tx(instance, time(NULL)); |
| 3524 |
} |
| 3525 |
|
| 3526 |
return res; |
| 3527 |
} |
| 3528 |
|
| 3529 |
|
| 3530 |
static int rtcp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice) |
| 3531 |
{ |
| 3532 |
return __rtp_sendto(instance, buf, size, flags, sa, 1, ice, 1); |
| 3533 |
} |
| 3534 |
|
| 3535 |
|
| 3536 |
static int rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice) |
| 3537 |
{ |
| 3538 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 3539 |
int hdrlen = 12; |
| 3540 |
int res; |
| 3541 |
|
| 3542 |
if ((res = __rtp_sendto(instance, buf, size, flags, sa, 0, ice, 1)) > 0) { |
| 3543 |
rtp->txcount++; |
| 3544 |
rtp->txoctetcount += (res - hdrlen); |
| 3545 |
} |
| 3546 |
|
| 3547 |
return res; |
| 3548 |
} |
| 3549 |
|
| 3550 |
static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp) |
| 3551 |
{ |
| 3552 |
unsigned int interval; |
| 3553 |
|
| 3554 |
|
| 3555 |
interval = rtcpinterval; |
| 3556 |
return interval; |
| 3557 |
} |
| 3558 |
|
| 3559 |
static void calc_mean_and_standard_deviation(double new_sample, double *mean, double *std_dev, unsigned int *count) |
| 3560 |
{ |
| 3561 |
double delta1; |
| 3562 |
double delta2; |
| 3563 |
|
| 3564 |
|
| 3565 |
double last_sum_of_squares = (*std_dev) * (*std_dev) * (*count ?: 1); |
| 3566 |
|
| 3567 |
if (++(*count) == 0) { |
| 3568 |
|
| 3569 |
*count = 1; |
| 3570 |
} |
| 3571 |
|
| 3572 |
|
| 3573 |
|
| 3574 |
|
| 3575 |
|
| 3576 |
|
| 3577 |
|
| 3578 |
|
| 3579 |
delta1 = new_sample - *mean; |
| 3580 |
*mean += (delta1 / *count); |
| 3581 |
delta2 = new_sample - *mean; |
| 3582 |
|
| 3583 |
|
| 3584 |
*std_dev = sqrt((last_sum_of_squares + (delta1 * delta2)) / *count); |
| 3585 |
} |
| 3586 |
|
| 3587 |
static int create_new_socket(const char *type, struct ast_sockaddr *bind_addr) |
| 3588 |
{ |
| 3589 |
int af, sock; |
| 3590 |
|
| 3591 |
af = ast_sockaddr_is_ipv4(bind_addr) ? AF_INET : |
| 3592 |
ast_sockaddr_is_ipv6(bind_addr) ? AF_INET6 : -1; |
| 3593 |
sock = ast_socket_nonblock(af, SOCK_DGRAM, 0); |
| 3594 |
|
| 3595 |
if (sock < 0) { |
| 3596 |
ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno)); |
| 3597 |
return sock; |
| 3598 |
} |
| 3599 |
|
| 3600 |
#ifdef SO_NO_CHECK |
| 3601 |
if (nochecksums) { |
| 3602 |
setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums)); |
| 3603 |
} |
| 3604 |
#endif |
| 3605 |
|
| 3606 |
#ifdef HAVE_SOCK_IPV6_V6ONLY |
| 3607 |
if (AF_INET6 == af && ast_sockaddr_is_any(bind_addr)) { |
| 3608 |
|
| 3609 |
if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &(int){0}, sizeof(int)) != 0) { |
| 3610 |
ast_log(LOG_WARNING, "setsockopt IPV6_V6ONLY=0 failed: %s\n", strerror(errno)); |
| 3611 |
} |
| 3612 |
} |
| 3613 |
#endif |
| 3614 |
|
| 3615 |
return sock; |
| 3616 |
} |
| 3617 |
|
| 3618 |
|
| 3619 |
|
| 3620 |
|
| 3621 |
|
| 3622 |
|
| 3623 |
|
| 3624 |
|
| 3625 |
|
| 3626 |
static void rtp_learning_seq_init(struct rtp_learning_info *info, uint16_t seq) |
| 3627 |
{ |
| 3628 |
info->max_seq = seq; |
| 3629 |
info->packets = learning_min_sequential; |
| 3630 |
memset(&info->received, 0, sizeof(info->received)); |
| 3631 |
} |
| 3632 |
|
| 3633 |
|
| 3634 |
|
| 3635 |
|
| 3636 |
|
| 3637 |
|
| 3638 |
|
| 3639 |
|
| 3640 |
|
| 3641 |
|
| 3642 |
|
| 3643 |
static int rtp_learning_rtp_seq_update(struct rtp_learning_info *info, uint16_t seq) |
| 3644 |
{ |
| 3645 |
if (seq == (uint16_t) (info->max_seq + 1)) { |
| 3646 |
|
| 3647 |
info->packets--; |
| 3648 |
} else { |
| 3649 |
|
| 3650 |
info->packets = learning_min_sequential - 1; |
| 3651 |
info->received = ast_tvnow(); |
| 3652 |
} |
| 3653 |
|
| 3654 |
|
| 3655 |
if (strictrtp == STRICT_RTP_YES) { |
| 3656 |
switch (info->stream_type) { |
| 3657 |
case AST_MEDIA_TYPE_UNKNOWN: |
| 3658 |
case AST_MEDIA_TYPE_AUDIO: |
| 3659 |
|
| 3660 |
|
| 3661 |
|
| 3662 |
|
| 3663 |
|
| 3664 |
if (ast_tvzero(info->received)) { |
| 3665 |
info->received = ast_tvnow(); |
| 3666 |
} else if (!info->packets |
| 3667 |
&& ast_tvdiff_ms(ast_tvnow(), info->received) < learning_min_duration) { |
| 3668 |
|
| 3669 |
info->packets = learning_min_sequential - 1; |
| 3670 |
info->received = ast_tvnow(); |
| 3671 |
} |
| 3672 |
break; |
| 3673 |
case AST_MEDIA_TYPE_VIDEO: |
| 3674 |
case AST_MEDIA_TYPE_IMAGE: |
| 3675 |
case AST_MEDIA_TYPE_TEXT: |
| 3676 |
case AST_MEDIA_TYPE_END: |
| 3677 |
break; |
| 3678 |
} |
| 3679 |
} |
| 3680 |
|
| 3681 |
info->max_seq = seq; |
| 3682 |
|
| 3683 |
return info->packets; |
| 3684 |
} |
| 3685 |
|
| 3686 |
|
| 3687 |
|
| 3688 |
|
| 3689 |
|
| 3690 |
|
| 3691 |
static void rtp_learning_start(struct ast_rtp *rtp) |
| 3692 |
{ |
| 3693 |
rtp->strict_rtp_state = STRICT_RTP_LEARN; |
| 3694 |
memset(&rtp->rtp_source_learn.proposed_address, 0, |
| 3695 |
sizeof(rtp->rtp_source_learn.proposed_address)); |
| 3696 |
rtp->rtp_source_learn.start = ast_tvnow(); |
| 3697 |
rtp_learning_seq_init(&rtp->rtp_source_learn, (uint16_t) rtp->lastrxseqno); |
| 3698 |
} |
| 3699 |
|
| 3700 |
#ifdef HAVE_PJPROJECT |
| 3701 |
static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message); |
| 3702 |
|
| 3703 |
|
| 3704 |
|
| 3705 |
|
| 3706 |
|
| 3707 |
static void rtp_unload_acl(ast_rwlock_t *lock, struct ast_acl_list **acl) |
| 3708 |
{ |
| 3709 |
ast_rwlock_wrlock(lock); |
| 3710 |
*acl = ast_free_acl_list(*acl); |
| 3711 |
ast_rwlock_unlock(lock); |
| 3712 |
} |
| 3713 |
|
| 3714 |
|
| 3715 |
|
| 3716 |
|
| 3717 |
|
| 3718 |
|
| 3719 |
|
| 3720 |
|
| 3721 |
|
| 3722 |
|
| 3723 |
static int rtp_address_is_ice_blacklisted(const struct ast_sockaddr *address) |
| 3724 |
{ |
| 3725 |
int result = 0; |
| 3726 |
|
| 3727 |
ast_rwlock_rdlock(&ice_acl_lock); |
| 3728 |
result |= ast_apply_acl_nolog(ice_acl, address) == AST_SENSE_DENY; |
| 3729 |
ast_rwlock_unlock(&ice_acl_lock); |
| 3730 |
|
| 3731 |
return result; |
| 3732 |
} |
| 3733 |
|
| 3734 |
|
| 3735 |
|
| 3736 |
|
| 3737 |
|
| 3738 |
|
| 3739 |
|
| 3740 |
|
| 3741 |
|
| 3742 |
|
| 3743 |
|
| 3744 |
|
| 3745 |
|
| 3746 |
static int stun_address_is_blacklisted(const struct ast_sockaddr *addr) |
| 3747 |
{ |
| 3748 |
int result = 0; |
| 3749 |
|
| 3750 |
ast_rwlock_rdlock(&stun_acl_lock); |
| 3751 |
result |= ast_apply_acl_nolog(stun_acl, addr) == AST_SENSE_DENY; |
| 3752 |
ast_rwlock_unlock(&stun_acl_lock); |
| 3753 |
|
| 3754 |
return result; |
| 3755 |
} |
| 3756 |
|
| 3757 |
|
| 3758 |
static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *addr, int port, int component, |
| 3759 |
int transport) |
| 3760 |
{ |
| 3761 |
unsigned int count = 0; |
| 3762 |
struct ifaddrs *ifa, *ia; |
| 3763 |
struct ast_sockaddr tmp; |
| 3764 |
pj_sockaddr pjtmp; |
| 3765 |
struct ast_ice_host_candidate *candidate; |
| 3766 |
int af_inet_ok = 0, af_inet6_ok = 0; |
| 3767 |
struct sockaddr_in stunaddr_copy; |
| 3768 |
int stunaddr_ttl_copy = 0; |
| 3769 |
char *stun_hostname_copy = NULL; |
| 3770 |
|
| 3771 |
if (ast_sockaddr_is_ipv4(addr)) { |
| 3772 |
af_inet_ok = 1; |
| 3773 |
} else if (ast_sockaddr_is_any(addr)) { |
| 3774 |
af_inet_ok = af_inet6_ok = 1; |
| 3775 |
} else { |
| 3776 |
af_inet6_ok = 1; |
| 3777 |
} |
| 3778 |
|
| 3779 |
if (getifaddrs(&ifa) < 0) { |
| 3780 |
|
| 3781 |
ast_log(LOG_ERROR, "(%p) ICE Error obtaining list of local addresses: %s\n", |
| 3782 |
instance, strerror(errno)); |
| 3783 |
} else { |
| 3784 |
ast_debug_ice(2, "(%p) ICE add system candidates\n", instance); |
| 3785 |
|
| 3786 |
|
| 3787 |
|
| 3788 |
for (ia = ifa; ia && count < PJ_ICE_MAX_CAND; ia = ia->ifa_next) { |
| 3789 |
|
| 3790 |
|
| 3791 |
if (!ia->ifa_addr || (ia->ifa_flags & IFF_UP) == 0) { |
| 3792 |
continue; |
| 3793 |
} |
| 3794 |
|
| 3795 |
|
| 3796 |
if (ia->ifa_addr->sa_family != AF_INET && ia->ifa_addr->sa_family != AF_INET6) { |
| 3797 |
continue; |
| 3798 |
} |
| 3799 |
|
| 3800 |
ast_sockaddr_from_sockaddr(&tmp, ia->ifa_addr); |
| 3801 |
|
| 3802 |
if (ia->ifa_addr->sa_family == AF_INET) { |
| 3803 |
const struct sockaddr_in *sa_in = (struct sockaddr_in*)ia->ifa_addr; |
| 3804 |
if (!af_inet_ok) { |
| 3805 |
continue; |
| 3806 |
} |
| 3807 |
|
| 3808 |
|
| 3809 |
|
| 3810 |
|
| 3811 |
if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == htonl(0x7F000000)) { |
| 3812 |
continue; |
| 3813 |
} |
| 3814 |
|
| 3815 |
|
| 3816 |
if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == 0) { |
| 3817 |
continue; |
| 3818 |
} |
| 3819 |
} else { |
| 3820 |
if (!af_inet6_ok) { |
| 3821 |
continue; |
| 3822 |
} |
| 3823 |
|
| 3824 |
|
| 3825 |
if (!ast_sockaddr_cmp_addr(&lo6, &tmp)) { |
| 3826 |
continue; |
| 3827 |
} |
| 3828 |
} |
| 3829 |
|
| 3830 |
|
| 3831 |
AST_RWLIST_RDLOCK(&host_candidates); |
| 3832 |
AST_LIST_TRAVERSE(&host_candidates, candidate, next) { |
| 3833 |
if (!ast_sockaddr_cmp(&candidate->local, &tmp)) { |
| 3834 |
|
| 3835 |
|
| 3836 |
|
| 3837 |
|
| 3838 |
|
| 3839 |
if (!rtp_address_is_ice_blacklisted(&candidate->advertised)) { |
| 3840 |
ast_sockaddr_to_pj_sockaddr(&candidate->advertised, &pjtmp); |
| 3841 |
pj_sockaddr_set_port(&pjtmp, port); |
| 3842 |
ast_rtp_ice_add_cand(instance, rtp, component, transport, |
| 3843 |
PJ_ICE_CAND_TYPE_HOST, 65535, &pjtmp, &pjtmp, NULL, |
| 3844 |
pj_sockaddr_get_len(&pjtmp)); |
| 3845 |
++count; |
| 3846 |
} |
| 3847 |
|
| 3848 |
if (!candidate->include_local) { |
| 3849 |
|
| 3850 |
ast_sockaddr_setnull(&tmp); |
| 3851 |
} |
| 3852 |
|
| 3853 |
break; |
| 3854 |
} |
| 3855 |
} |
| 3856 |
AST_RWLIST_UNLOCK(&host_candidates); |
| 3857 |
|
| 3858 |
|
| 3859 |
|
| 3860 |
|
| 3861 |
if (ast_sockaddr_isnull(&tmp) || count == PJ_ICE_MAX_CAND) { |
| 3862 |
continue; |
| 3863 |
} |
| 3864 |
|
| 3865 |
if (rtp_address_is_ice_blacklisted(&tmp)) { |
| 3866 |
continue; |
| 3867 |
} |
| 3868 |
|
| 3869 |
ast_sockaddr_to_pj_sockaddr(&tmp, &pjtmp); |
| 3870 |
pj_sockaddr_set_port(&pjtmp, port); |
| 3871 |
ast_rtp_ice_add_cand(instance, rtp, component, transport, |
| 3872 |
PJ_ICE_CAND_TYPE_HOST, 65535, &pjtmp, &pjtmp, NULL, |
| 3873 |
pj_sockaddr_get_len(&pjtmp)); |
| 3874 |
++count; |
| 3875 |
} |
| 3876 |
freeifaddrs(ifa); |
| 3877 |
} |
| 3878 |
|
| 3879 |
|
| 3880 |
|
| 3881 |
|
| 3882 |
|
| 3883 |
ast_rwlock_rdlock(&stunaddr_lock); |
| 3884 |
memcpy(&stunaddr_copy, &stunaddr, sizeof(stunaddr)); |
| 3885 |
stunaddr_ttl_copy = stunaddr_ttl; |
| 3886 |
stun_hostname_copy = ast_strdupa(S_OR(stun_hostname, "")); |
| 3887 |
ast_rwlock_unlock(&stunaddr_lock); |
| 3888 |
|
| 3889 |
|
| 3890 |
if ( (!ast_strlen_zero(stun_hostname_copy) || stunaddr_copy.sin_addr.s_addr) |
| 3891 |
&& !stun_address_is_blacklisted(addr) && |
| 3892 |
(ast_sockaddr_is_ipv4(addr) || ast_sockaddr_is_any(addr)) && |
| 3893 |
count < PJ_ICE_MAX_CAND) { |
| 3894 |
struct sockaddr_in answer; |
| 3895 |
int rsp; |
| 3896 |
|
| 3897 |
ast_debug_category(2, AST_DEBUG_CATEGORY_ICE | AST_DEBUG_CATEGORY_STUN, |
| 3898 |
"(%p) ICE request STUN %s %s candidate\n", instance, |
| 3899 |
transport == AST_TRANSPORT_UDP ? "UDP" : "TCP", |
| 3900 |
component == AST_RTP_ICE_COMPONENT_RTP ? "RTP" : "RTCP"); |
| 3901 |
|
| 3902 |
|
| 3903 |
|
| 3904 |
|
| 3905 |
|
| 3906 |
ao2_unlock(instance); |
| 3907 |
|
| 3908 |
|
| 3909 |
|
| 3910 |
|
| 3911 |
|
| 3912 |
|
| 3913 |
|
| 3914 |
|
| 3915 |
|
| 3916 |
|
| 3917 |
|
| 3918 |
|
| 3919 |
|
| 3920 |
|
| 3921 |
|
| 3922 |
|
| 3923 |
|
| 3924 |
|
| 3925 |
|
| 3926 |
|
| 3927 |
|
| 3928 |
ast_debug_stun(2, "Checking stunaddr_reresolve_ttl_0: %s TTL: %d Host: %s resolver: %p\n", |
| 3929 |
AST_CLI_YESNO(stunaddr_reresolve_ttl_0), stunaddr_ttl_copy, stun_hostname_copy, |
| 3930 |
stunaddr_resolver); |
| 3931 |
|
| 3932 |
if (stunaddr_reresolve_ttl_0 && stunaddr_ttl_copy == 0 |
| 3933 |
&& !ast_strlen_zero(stun_hostname_copy) && !stunaddr_resolver) { |
| 3934 |
|
| 3935 |
|
| 3936 |
|
| 3937 |
|
| 3938 |
ast_rwlock_wrlock(&stunaddr_lock); |
| 3939 |
|
| 3940 |
|
| 3941 |
|
| 3942 |
|
| 3943 |
|
| 3944 |
|
| 3945 |
|
| 3946 |
if (stunaddr_ttl == 0 && !ast_strlen_zero(stun_hostname) && !stunaddr_resolver) { |
| 3947 |
ast_debug_stun(2, "Restarting recurring resolution for stun server '%s'\n", |
| 3948 |
stun_hostname); |
| 3949 |
|
| 3950 |
|
| 3951 |
|
| 3952 |
|
| 3953 |
|
| 3954 |
stunaddr_resolver = ast_dns_resolve_recurring(stun_hostname, T_A, C_IN, |
| 3955 |
&stunaddr_resolve_callback, NULL); |
| 3956 |
if (!stunaddr_resolver) { |
| 3957 |
ast_log(LOG_ERROR, "Failed to setup recurring DNS resolution of stunaddr '%s'", |
| 3958 |
stun_hostname); |
| 3959 |
} |
| 3960 |
} else { |
| 3961 |
ast_debug_stun(2, "stun TTL: %d H: %s. Re-resolution skipped because another thread took care of it.\n", |
| 3962 |
stunaddr_ttl, stun_hostname); |
| 3963 |
} |
| 3964 |
|
| 3965 |
ast_rwlock_unlock(&stunaddr_lock); |
| 3966 |
} else { |
| 3967 |
ast_debug_stun(2, "stun TTL: %d H: %s. Re-resolution not needed or disabled.\n", stunaddr_ttl, stun_hostname); |
| 3968 |
} |
| 3969 |
|
| 3970 |
rsp = ast_stun_request(component == AST_RTP_ICE_COMPONENT_RTCP |
| 3971 |
? rtp->rtcp->s : rtp->s, &stunaddr_copy, NULL, &answer); |
| 3972 |
ao2_lock(instance); |
| 3973 |
if (!rsp) { |
| 3974 |
struct ast_rtp_engine_ice_candidate *candidate; |
| 3975 |
pj_sockaddr ext, base; |
| 3976 |
pj_str_t mapped = pj_str(ast_strdupa(ast_inet_ntoa(answer.sin_addr))); |
| 3977 |
int srflx = 1, baseset = 0; |
| 3978 |
struct ao2_iterator i; |
| 3979 |
|
| 3980 |
pj_sockaddr_init(pj_AF_INET(), &ext, &mapped, ntohs(answer.sin_port)); |
| 3981 |
|
| 3982 |
|
| 3983 |
|
| 3984 |
|
| 3985 |
|
| 3986 |
|
| 3987 |
i = ao2_iterator_init(rtp->ice_local_candidates, 0); |
| 3988 |
while (srflx && (candidate = ao2_iterator_next(&i))) { |
| 3989 |
if (!baseset && ast_sockaddr_is_ipv4(&candidate->address)) { |
| 3990 |
baseset = 1; |
| 3991 |
ast_sockaddr_to_pj_sockaddr(&candidate->address, &base); |
| 3992 |
} |
| 3993 |
|
| 3994 |
if (!pj_sockaddr_cmp(&candidate->address, &ext)) { |
| 3995 |
srflx = 0; |
| 3996 |
} |
| 3997 |
|
| 3998 |
ao2_ref(candidate, -1); |
| 3999 |
} |
| 4000 |
ao2_iterator_destroy(&i); |
| 4001 |
|
| 4002 |
if (srflx && baseset) { |
| 4003 |
pj_sockaddr_set_port(&base, port); |
| 4004 |
ast_rtp_ice_add_cand(instance, rtp, component, transport, |
| 4005 |
PJ_ICE_CAND_TYPE_SRFLX, 65535, &ext, &base, &base, |
| 4006 |
pj_sockaddr_get_len(&ext)); |
| 4007 |
} |
| 4008 |
} |
| 4009 |
} |
| 4010 |
|
| 4011 |
|
| 4012 |
if (pj_strlen(&turnaddr)) { |
| 4013 |
ast_rtp_ice_turn_request(instance, component, AST_TRANSPORT_TCP, pj_strbuf(&turnaddr), turnport, |
| 4014 |
pj_strbuf(&turnusername), pj_strbuf(&turnpassword)); |
| 4015 |
} |
| 4016 |
} |
| 4017 |
#endif |
| 4018 |
|
| 4019 |
|
| 4020 |
|
| 4021 |
|
| 4022 |
|
| 4023 |
|
| 4024 |
|
| 4025 |
|
| 4026 |
|
| 4027 |
|
| 4028 |
|
| 4029 |
static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery) |
| 4030 |
{ |
| 4031 |
struct timeval t; |
| 4032 |
long ms; |
| 4033 |
|
| 4034 |
if (ast_tvzero(rtp->txcore)) { |
| 4035 |
rtp->txcore = ast_tvnow(); |
| 4036 |
rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000; |
| 4037 |
} |
| 4038 |
|
| 4039 |
t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow(); |
| 4040 |
if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) { |
| 4041 |
ms = 0; |
| 4042 |
} |
| 4043 |
rtp->txcore = t; |
| 4044 |
|
| 4045 |
return (unsigned int) ms; |
| 4046 |
} |
| 4047 |
|
| 4048 |
#ifdef HAVE_PJPROJECT |
| 4049 |
|
| 4050 |
|
| 4051 |
|
| 4052 |
|
| 4053 |
|
| 4054 |
|
| 4055 |
|
| 4056 |
|
| 4057 |
|
| 4058 |
|
| 4059 |
|
| 4060 |
|
| 4061 |
|
| 4062 |
|
| 4063 |
static int ice_create(struct ast_rtp_instance *instance, struct ast_sockaddr *addr, |
| 4064 |
int port, int replace) |
| 4065 |
{ |
| 4066 |
pj_stun_config stun_config; |
| 4067 |
pj_str_t ufrag, passwd; |
| 4068 |
pj_status_t status; |
| 4069 |
struct ice_wrap *ice_old; |
| 4070 |
struct ice_wrap *ice; |
| 4071 |
pj_ice_sess *real_ice = NULL; |
| 4072 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 4073 |
|
| 4074 |
ao2_cleanup(rtp->ice_local_candidates); |
| 4075 |
rtp->ice_local_candidates = NULL; |
| 4076 |
|
| 4077 |
ast_debug_ice(2, "(%p) ICE create%s\n", instance, replace ? " and replace" : ""); |
| 4078 |
|
| 4079 |
ice = ao2_alloc_options(sizeof(*ice), ice_wrap_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 4080 |
if (!ice) { |
| 4081 |
ast_rtp_ice_stop(instance); |
| 4082 |
return -1; |
| 4083 |
} |
| 4084 |
|
| 4085 |
pj_thread_register_check(); |
| 4086 |
|
| 4087 |
pj_stun_config_init(&stun_config, &cachingpool.factory, 0, NULL, timer_heap); |
| 4088 |
if (!stun_software_attribute) { |
| 4089 |
stun_config.software_name = pj_str(NULL); |
| 4090 |
} |
| 4091 |
|
| 4092 |
ufrag = pj_str(rtp->local_ufrag); |
| 4093 |
passwd = pj_str(rtp->local_passwd); |
| 4094 |
|
| 4095 |
|
| 4096 |
ao2_unlock(instance); |
| 4097 |
|
| 4098 |
status = pj_ice_sess_create(&stun_config, NULL, PJ_ICE_SESS_ROLE_UNKNOWN, |
| 4099 |
rtp->ice_num_components, &ast_rtp_ice_sess_cb, &ufrag, &passwd, NULL, &real_ice); |
| 4100 |
ao2_lock(instance); |
| 4101 |
if (status == PJ_SUCCESS) { |
| 4102 |
|
| 4103 |
real_ice->user_data = instance; |
| 4104 |
ice->real_ice = real_ice; |
| 4105 |
ice_old = rtp->ice; |
| 4106 |
rtp->ice = ice; |
| 4107 |
if (ice_old) { |
| 4108 |
ao2_unlock(instance); |
| 4109 |
ao2_ref(ice_old, -1); |
| 4110 |
ao2_lock(instance); |
| 4111 |
} |
| 4112 |
|
| 4113 |
|
| 4114 |
rtp_add_candidates_to_ice(instance, rtp, addr, port, AST_RTP_ICE_COMPONENT_RTP, |
| 4115 |
TRANSPORT_SOCKET_RTP); |
| 4116 |
|
| 4117 |
|
| 4118 |
|
| 4119 |
|
| 4120 |
if (replace && rtp->rtcp && rtp->ice_num_components > 1) { |
| 4121 |
rtp_add_candidates_to_ice(instance, rtp, &rtp->rtcp->us, |
| 4122 |
ast_sockaddr_port(&rtp->rtcp->us), AST_RTP_ICE_COMPONENT_RTCP, |
| 4123 |
TRANSPORT_SOCKET_RTCP); |
| 4124 |
} |
| 4125 |
|
| 4126 |
return 0; |
| 4127 |
} |
| 4128 |
|
| 4129 |
|
| 4130 |
|
| 4131 |
|
| 4132 |
|
| 4133 |
ao2_ref(ice, -1); |
| 4134 |
|
| 4135 |
ast_rtp_ice_stop(instance); |
| 4136 |
return -1; |
| 4137 |
|
| 4138 |
} |
| 4139 |
#endif |
| 4140 |
|
| 4141 |
static int rtp_allocate_transport(struct ast_rtp_instance *instance, struct ast_rtp *rtp) |
| 4142 |
{ |
| 4143 |
int x, startplace, i, maxloops; |
| 4144 |
unsigned int port_start, port_end; |
| 4145 |
|
| 4146 |
rtp->strict_rtp_state = (strictrtp ? STRICT_RTP_CLOSED : STRICT_RTP_OPEN); |
| 4147 |
|
| 4148 |
|
| 4149 |
port_start = ast_rtp_instance_get_port_start(instance); |
| 4150 |
port_end = ast_rtp_instance_get_port_end(instance); |
| 4151 |
if (port_start > 0 && port_end > 0 && port_end > port_start) { |
| 4152 |
ast_debug_rtp(1, "(%p) RTP using per-instance port range %d-%d\n", |
| 4153 |
instance, port_start, port_end); |
| 4154 |
} else { |
| 4155 |
port_start = rtpstart; |
| 4156 |
port_end = rtpend; |
| 4157 |
} |
| 4158 |
|
| 4159 |
|
| 4160 |
if ((rtp->s = create_new_socket("RTP", &rtp->bind_address)) < 0) { |
| 4161 |
ast_log(LOG_WARNING, "Failed to create a new socket for RTP instance '%p'\n", instance); |
| 4162 |
return -1; |
| 4163 |
} |
| 4164 |
|
| 4165 |
|
| 4166 |
x = (ast_random() % (port_end - port_start)) + port_start; |
| 4167 |
x = x & ~1; |
| 4168 |
startplace = x; |
| 4169 |
|
| 4170 |
|
| 4171 |
|
| 4172 |
maxloops = port_end - port_start; |
| 4173 |
for (i = 0; i <= maxloops; i++) { |
| 4174 |
ast_sockaddr_set_port(&rtp->bind_address, x); |
| 4175 |
|
| 4176 |
if (!ast_bind(rtp->s, &rtp->bind_address)) { |
| 4177 |
ast_debug_rtp(1, "(%p) RTP allocated port %d\n", instance, x); |
| 4178 |
ast_rtp_instance_set_local_address(instance, &rtp->bind_address); |
| 4179 |
ast_test_suite_event_notify("RTP_PORT_ALLOCATED", "Port: %d", x); |
| 4180 |
break; |
| 4181 |
} |
| 4182 |
|
| 4183 |
x += 2; |
| 4184 |
if (x > port_end) { |
| 4185 |
x = (port_start + 1) & ~1; |
| 4186 |
} |
| 4187 |
|
| 4188 |
|
| 4189 |
if (x == startplace || (errno != EADDRINUSE && errno != EACCES)) { |
| 4190 |
ast_log(LOG_ERROR, "Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance); |
| 4191 |
close(rtp->s); |
| 4192 |
rtp->s = -1; |
| 4193 |
return -1; |
| 4194 |
} |
| 4195 |
} |
| 4196 |
|
| 4197 |
#ifdef HAVE_PJPROJECT |
| 4198 |
|
| 4199 |
ast_cond_init(&rtp->cond, NULL); |
| 4200 |
|
| 4201 |
generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag)); |
| 4202 |
generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd)); |
| 4203 |
|
| 4204 |
|
| 4205 |
if (icesupport) { |
| 4206 |
rtp->ice_num_components = 2; |
| 4207 |
ast_debug_ice(2, "(%p) ICE creating session %s (%d)\n", instance, |
| 4208 |
ast_sockaddr_stringify(&rtp->bind_address), x); |
| 4209 |
if (ice_create(instance, &rtp->bind_address, x, 0)) { |
| 4210 |
ast_log(LOG_NOTICE, "(%p) ICE failed to create session\n", instance); |
| 4211 |
} else { |
| 4212 |
rtp->ice_port = x; |
| 4213 |
ast_sockaddr_copy(&rtp->ice_original_rtp_addr, &rtp->bind_address); |
| 4214 |
} |
| 4215 |
} |
| 4216 |
#endif |
| 4217 |
|
| 4218 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 4219 |
rtp->rekeyid = -1; |
| 4220 |
rtp->dtls.timeout_timer = -1; |
| 4221 |
#endif |
| 4222 |
|
| 4223 |
return 0; |
| 4224 |
} |
| 4225 |
|
| 4226 |
static void rtp_deallocate_transport(struct ast_rtp_instance *instance, struct ast_rtp *rtp) |
| 4227 |
{ |
| 4228 |
int saved_rtp_s = rtp->s; |
| 4229 |
#ifdef HAVE_PJPROJECT |
| 4230 |
struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_STATE_WAIT_TIME, 1000)); |
| 4231 |
struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, }; |
| 4232 |
#endif |
| 4233 |
|
| 4234 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 4235 |
ast_rtp_dtls_stop(instance); |
| 4236 |
#endif |
| 4237 |
|
| 4238 |
|
| 4239 |
if (rtp->s > -1) { |
| 4240 |
close(rtp->s); |
| 4241 |
rtp->s = -1; |
| 4242 |
} |
| 4243 |
|
| 4244 |
|
| 4245 |
if (rtp->rtcp && rtp->rtcp->s > -1) { |
| 4246 |
if (saved_rtp_s != rtp->rtcp->s) { |
| 4247 |
close(rtp->rtcp->s); |
| 4248 |
} |
| 4249 |
rtp->rtcp->s = -1; |
| 4250 |
} |
| 4251 |
|
| 4252 |
#ifdef HAVE_PJPROJECT |
| 4253 |
pj_thread_register_check(); |
| 4254 |
|
| 4255 |
|
| 4256 |
|
| 4257 |
|
| 4258 |
|
| 4259 |
|
| 4260 |
if (rtp->turn_rtp) { |
| 4261 |
rtp->turn_state = PJ_TURN_STATE_NULL; |
| 4262 |
|
| 4263 |
|
| 4264 |
ao2_unlock(instance); |
| 4265 |
pj_turn_sock_destroy(rtp->turn_rtp); |
| 4266 |
ao2_lock(instance); |
| 4267 |
while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) { |
| 4268 |
ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts); |
| 4269 |
} |
| 4270 |
rtp->turn_rtp = NULL; |
| 4271 |
} |
| 4272 |
|
| 4273 |
|
| 4274 |
if (rtp->turn_rtcp) { |
| 4275 |
rtp->turn_state = PJ_TURN_STATE_NULL; |
| 4276 |
|
| 4277 |
|
| 4278 |
ao2_unlock(instance); |
| 4279 |
pj_turn_sock_destroy(rtp->turn_rtcp); |
| 4280 |
ao2_lock(instance); |
| 4281 |
while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) { |
| 4282 |
ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts); |
| 4283 |
} |
| 4284 |
rtp->turn_rtcp = NULL; |
| 4285 |
} |
| 4286 |
|
| 4287 |
ast_debug_ice(2, "(%p) ICE RTP transport deallocating\n", instance); |
| 4288 |
|
| 4289 |
ast_rtp_ice_stop(instance); |
| 4290 |
|
| 4291 |
|
| 4292 |
if (rtp->ice_local_candidates) { |
| 4293 |
ao2_ref(rtp->ice_local_candidates, -1); |
| 4294 |
rtp->ice_local_candidates = NULL; |
| 4295 |
} |
| 4296 |
|
| 4297 |
if (rtp->ice_active_remote_candidates) { |
| 4298 |
ao2_ref(rtp->ice_active_remote_candidates, -1); |
| 4299 |
rtp->ice_active_remote_candidates = NULL; |
| 4300 |
} |
| 4301 |
|
| 4302 |
if (rtp->ice_proposed_remote_candidates) { |
| 4303 |
ao2_ref(rtp->ice_proposed_remote_candidates, -1); |
| 4304 |
rtp->ice_proposed_remote_candidates = NULL; |
| 4305 |
} |
| 4306 |
|
| 4307 |
if (rtp->ioqueue) { |
| 4308 |
|
| 4309 |
|
| 4310 |
|
| 4311 |
|
| 4312 |
|
| 4313 |
ao2_unlock(instance); |
| 4314 |
rtp_ioqueue_thread_remove(rtp->ioqueue); |
| 4315 |
ao2_lock(instance); |
| 4316 |
rtp->ioqueue = NULL; |
| 4317 |
} |
| 4318 |
#endif |
| 4319 |
} |
| 4320 |
|
| 4321 |
|
| 4322 |
static int ast_rtp_new(struct ast_rtp_instance *instance, |
| 4323 |
struct ast_sched_context *sched, struct ast_sockaddr *addr, |
| 4324 |
void *data) |
| 4325 |
{ |
| 4326 |
struct ast_rtp *rtp = NULL; |
| 4327 |
|
| 4328 |
|
| 4329 |
if (!(rtp = ast_calloc(1, sizeof(*rtp)))) { |
| 4330 |
return -1; |
| 4331 |
} |
| 4332 |
rtp->owner = instance; |
| 4333 |
|
| 4334 |
rtp->ssrc = ast_random(); |
| 4335 |
ast_uuid_generate_str(rtp->cname, sizeof(rtp->cname)); |
| 4336 |
rtp->seqno = ast_random() & 0xffff; |
| 4337 |
rtp->expectedrxseqno = -1; |
| 4338 |
rtp->expectedseqno = -1; |
| 4339 |
rtp->rxstart = -1; |
| 4340 |
rtp->sched = sched; |
| 4341 |
ast_sockaddr_copy(&rtp->bind_address, addr); |
| 4342 |
|
| 4343 |
ast_rtp_instance_set_data(instance, rtp); |
| 4344 |
|
| 4345 |
if (rtp_allocate_transport(instance, rtp)) { |
| 4346 |
return -1; |
| 4347 |
} |
| 4348 |
|
| 4349 |
if (AST_VECTOR_INIT(&rtp->ssrc_mapping, 1)) { |
| 4350 |
return -1; |
| 4351 |
} |
| 4352 |
|
| 4353 |
if (AST_VECTOR_INIT(&rtp->transport_wide_cc.packet_statistics, 0)) { |
| 4354 |
return -1; |
| 4355 |
} |
| 4356 |
rtp->transport_wide_cc.schedid = -1; |
| 4357 |
|
| 4358 |
rtp->f.subclass.format = ao2_bump(ast_format_none); |
| 4359 |
rtp->lastrxformat = ao2_bump(ast_format_none); |
| 4360 |
rtp->lasttxformat = ao2_bump(ast_format_none); |
| 4361 |
rtp->stream_num = -1; |
| 4362 |
|
| 4363 |
return 0; |
| 4364 |
} |
| 4365 |
|
| 4366 |
|
| 4367 |
|
| 4368 |
|
| 4369 |
|
| 4370 |
|
| 4371 |
|
| 4372 |
|
| 4373 |
|
| 4374 |
|
| 4375 |
#define SSRC_MAPPING_ELEM_CMP(elem, value) ((elem).instance == (value)) |
| 4376 |
|
| 4377 |
|
| 4378 |
static int ast_rtp_destroy(struct ast_rtp_instance *instance) |
| 4379 |
{ |
| 4380 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 4381 |
|
| 4382 |
if (rtp->bundled) { |
| 4383 |
struct ast_rtp *bundled_rtp; |
| 4384 |
|
| 4385 |
|
| 4386 |
ao2_unlock(instance); |
| 4387 |
|
| 4388 |
ao2_lock(rtp->bundled); |
| 4389 |
bundled_rtp = ast_rtp_instance_get_data(rtp->bundled); |
| 4390 |
AST_VECTOR_REMOVE_CMP_UNORDERED(&bundled_rtp->ssrc_mapping, instance, SSRC_MAPPING_ELEM_CMP, AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 4391 |
ao2_unlock(rtp->bundled); |
| 4392 |
|
| 4393 |
ao2_lock(instance); |
| 4394 |
ao2_ref(rtp->bundled, -1); |
| 4395 |
} |
| 4396 |
|
| 4397 |
rtp_deallocate_transport(instance, rtp); |
| 4398 |
|
| 4399 |
|
| 4400 |
if (rtp->smoother) { |
| 4401 |
ast_smoother_free(rtp->smoother); |
| 4402 |
} |
| 4403 |
|
| 4404 |
|
| 4405 |
if (rtp->rtcp) { |
| 4406 |
|
| 4407 |
|
| 4408 |
|
| 4409 |
|
| 4410 |
|
| 4411 |
ast_free(rtp->rtcp->local_addr_str); |
| 4412 |
ast_free(rtp->rtcp); |
| 4413 |
} |
| 4414 |
|
| 4415 |
|
| 4416 |
if (rtp->red) { |
| 4417 |
ao2_unlock(instance); |
| 4418 |
AST_SCHED_DEL(rtp->sched, rtp->red->schedid); |
| 4419 |
ao2_lock(instance); |
| 4420 |
ast_free(rtp->red); |
| 4421 |
rtp->red = NULL; |
| 4422 |
} |
| 4423 |
|
| 4424 |
|
| 4425 |
if (rtp->send_buffer) { |
| 4426 |
ast_data_buffer_free(rtp->send_buffer); |
| 4427 |
} |
| 4428 |
|
| 4429 |
|
| 4430 |
if (rtp->recv_buffer) { |
| 4431 |
ast_data_buffer_free(rtp->recv_buffer); |
| 4432 |
} |
| 4433 |
|
| 4434 |
AST_VECTOR_FREE(&rtp->transport_wide_cc.packet_statistics); |
| 4435 |
|
| 4436 |
ao2_cleanup(rtp->lasttxformat); |
| 4437 |
ao2_cleanup(rtp->lastrxformat); |
| 4438 |
ao2_cleanup(rtp->f.subclass.format); |
| 4439 |
AST_VECTOR_FREE(&rtp->ssrc_mapping); |
| 4440 |
AST_VECTOR_FREE(&rtp->missing_seqno); |
| 4441 |
|
| 4442 |
|
| 4443 |
rtp->owner = NULL; |
| 4444 |
ast_free(rtp); |
| 4445 |
|
| 4446 |
return 0; |
| 4447 |
} |
| 4448 |
|
| 4449 |
|
| 4450 |
static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode) |
| 4451 |
{ |
| 4452 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 4453 |
rtp->dtmfmode = dtmf_mode; |
| 4454 |
return 0; |
| 4455 |
} |
| 4456 |
|
| 4457 |
|
| 4458 |
static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance) |
| 4459 |
{ |
| 4460 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 4461 |
return rtp->dtmfmode; |
| 4462 |
} |
| 4463 |
|
| 4464 |
|
| 4465 |
static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit) |
| 4466 |
{ |
| 4467 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 4468 |
struct ast_sockaddr remote_address = { {0,} }; |
| 4469 |
int hdrlen = 12, res = 0, i = 0, payload = -1, sample_rate = -1; |
| 4470 |
char data[256]; |
| 4471 |
unsigned int *rtpheader = (unsigned int*)data; |
| 4472 |
RAII_VAR(struct ast_format *, payload_format, NULL, ao2_cleanup); |
| 4473 |
|
| 4474 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 4475 |
|
| 4476 |
|
| 4477 |
if (ast_sockaddr_isnull(&remote_address)) { |
| 4478 |
return -1; |
| 4479 |
} |
| 4480 |
|
| 4481 |
|
| 4482 |
if ((digit <= '9') && (digit >= '0')) { |
| 4483 |
digit -= '0'; |
| 4484 |
} else if (digit == '*') { |
| 4485 |
digit = 10; |
| 4486 |
} else if (digit == '#') { |
| 4487 |
digit = 11; |
| 4488 |
} else if ((digit >= 'A') && (digit <= 'D')) { |
| 4489 |
digit = digit - 'A' + 12; |
| 4490 |
} else if ((digit >= 'a') && (digit <= 'd')) { |
| 4491 |
digit = digit - 'a' + 12; |
| 4492 |
} else { |
| 4493 |
ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit); |
| 4494 |
return -1; |
| 4495 |
} |
| 4496 |
|
| 4497 |
|
| 4498 |
|
| 4499 |
|
| 4500 |
|
| 4501 |
if (rtp->lasttxformat == ast_format_none) { |
| 4502 |
|
| 4503 |
payload_format = ast_rtp_codecs_get_preferred_format(ast_rtp_instance_get_codecs(instance)); |
| 4504 |
if (payload_format) { |
| 4505 |
|
| 4506 |
sample_rate = ast_rtp_get_rate(payload_format); |
| 4507 |
} |
| 4508 |
} else { |
| 4509 |
sample_rate = ast_rtp_get_rate(rtp->lasttxformat); |
| 4510 |
} |
| 4511 |
|
| 4512 |
if (sample_rate != -1) { |
| 4513 |
payload = ast_rtp_codecs_payload_code_tx_sample_rate(ast_rtp_instance_get_codecs(instance), 0, NULL, AST_RTP_DTMF, sample_rate); |
| 4514 |
} |
| 4515 |
|
| 4516 |
if (payload == -1 || |
| 4517 |
!ast_rtp_payload_mapping_tx_is_present( |
| 4518 |
ast_rtp_instance_get_codecs(instance), ast_rtp_codecs_get_payload(ast_rtp_instance_get_codecs(instance), payload))) { |
| 4519 |
|
| 4520 |
|
| 4521 |
|
| 4522 |
payload = ast_rtp_codecs_get_preferred_dtmf_format_pt(ast_rtp_instance_get_codecs(instance)); |
| 4523 |
sample_rate = ast_rtp_codecs_get_preferred_dtmf_format_rate(ast_rtp_instance_get_codecs(instance)); |
| 4524 |
} |
| 4525 |
|
| 4526 |
|
| 4527 |
if (payload == -1 || sample_rate == -1) { |
| 4528 |
sample_rate = DEFAULT_DTMF_SAMPLE_RATE_MS; |
| 4529 |
payload = ast_rtp_codecs_payload_code_tx(ast_rtp_instance_get_codecs(instance), 0, NULL, AST_RTP_DTMF); |
| 4530 |
} |
| 4531 |
|
| 4532 |
if (payload == -1) { |
| 4533 |
return -1; |
| 4534 |
} |
| 4535 |
|
| 4536 |
ast_test_suite_event_notify("DTMF_BEGIN", "Digit: %d\r\nPayload: %d\r\nRate: %d\r\n", digit, payload, sample_rate); |
| 4537 |
ast_debug(1, "Sending digit '%d' at rate %d with payload %d\n", digit, sample_rate, payload); |
| 4538 |
|
| 4539 |
rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000)); |
| 4540 |
rtp->send_duration = 160; |
| 4541 |
rtp->dtmf_samplerate_ms = (sample_rate / 1000); |
| 4542 |
rtp->lastts += calc_txstamp(rtp, NULL) * rtp->dtmf_samplerate_ms; |
| 4543 |
rtp->lastdigitts = rtp->lastts + rtp->send_duration; |
| 4544 |
|
| 4545 |
|
| 4546 |
rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno)); |
| 4547 |
rtpheader[1] = htonl(rtp->lastdigitts); |
| 4548 |
rtpheader[2] = htonl(rtp->ssrc); |
| 4549 |
|
| 4550 |
|
| 4551 |
for (i = 0; i < 2; i++) { |
| 4552 |
int ice; |
| 4553 |
|
| 4554 |
rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration)); |
| 4555 |
res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice); |
| 4556 |
if (res < 0) { |
| 4557 |
ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n", |
| 4558 |
ast_sockaddr_stringify(&remote_address), |
| 4559 |
strerror(errno)); |
| 4560 |
} |
| 4561 |
if (rtp_debug_test_addr(&remote_address)) { |
| 4562 |
ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n", |
| 4563 |
ast_sockaddr_stringify(&remote_address), |
| 4564 |
ice ? " (via ICE)" : "", |
| 4565 |
payload, rtp->seqno, rtp->lastdigitts, res - hdrlen); |
| 4566 |
} |
| 4567 |
rtp->seqno++; |
| 4568 |
rtp->send_duration += 160; |
| 4569 |
rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno)); |
| 4570 |
} |
| 4571 |
|
| 4572 |
|
| 4573 |
rtp->sending_digit = 1; |
| 4574 |
rtp->send_digit = digit; |
| 4575 |
rtp->send_payload = payload; |
| 4576 |
|
| 4577 |
return 0; |
| 4578 |
} |
| 4579 |
|
| 4580 |
|
| 4581 |
static int ast_rtp_dtmf_continuation(struct ast_rtp_instance *instance) |
| 4582 |
{ |
| 4583 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 4584 |
struct ast_sockaddr remote_address = { {0,} }; |
| 4585 |
int hdrlen = 12, res = 0; |
| 4586 |
char data[256]; |
| 4587 |
unsigned int *rtpheader = (unsigned int*)data; |
| 4588 |
int ice; |
| 4589 |
|
| 4590 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 4591 |
|
| 4592 |
|
| 4593 |
if (ast_sockaddr_isnull(&remote_address)) { |
| 4594 |
return -1; |
| 4595 |
} |
| 4596 |
|
| 4597 |
|
| 4598 |
rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno)); |
| 4599 |
rtpheader[1] = htonl(rtp->lastdigitts); |
| 4600 |
rtpheader[2] = htonl(rtp->ssrc); |
| 4601 |
rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration)); |
| 4602 |
|
| 4603 |
|
| 4604 |
res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice); |
| 4605 |
if (res < 0) { |
| 4606 |
ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n", |
| 4607 |
ast_sockaddr_stringify(&remote_address), |
| 4608 |
strerror(errno)); |
| 4609 |
} |
| 4610 |
|
| 4611 |
if (rtp_debug_test_addr(&remote_address)) { |
| 4612 |
ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n", |
| 4613 |
ast_sockaddr_stringify(&remote_address), |
| 4614 |
ice ? " (via ICE)" : "", |
| 4615 |
rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen); |
| 4616 |
} |
| 4617 |
|
| 4618 |
|
| 4619 |
rtp->seqno++; |
| 4620 |
rtp->send_duration += 160; |
| 4621 |
rtp->lastts += calc_txstamp(rtp, NULL) * rtp->dtmf_samplerate_ms; |
| 4622 |
|
| 4623 |
return 0; |
| 4624 |
} |
| 4625 |
|
| 4626 |
|
| 4627 |
static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration) |
| 4628 |
{ |
| 4629 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 4630 |
struct ast_sockaddr remote_address = { {0,} }; |
| 4631 |
int hdrlen = 12, res = -1, i = 0; |
| 4632 |
char data[256]; |
| 4633 |
unsigned int *rtpheader = (unsigned int*)data; |
| 4634 |
unsigned int measured_samples; |
| 4635 |
|
| 4636 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 4637 |
|
| 4638 |
|
| 4639 |
if (ast_sockaddr_isnull(&remote_address)) { |
| 4640 |
goto cleanup; |
| 4641 |
} |
| 4642 |
|
| 4643 |
|
| 4644 |
if ((digit <= '9') && (digit >= '0')) { |
| 4645 |
digit -= '0'; |
| 4646 |
} else if (digit == '*') { |
| 4647 |
digit = 10; |
| 4648 |
} else if (digit == '#') { |
| 4649 |
digit = 11; |
| 4650 |
} else if ((digit >= 'A') && (digit <= 'D')) { |
| 4651 |
digit = digit - 'A' + 12; |
| 4652 |
} else if ((digit >= 'a') && (digit <= 'd')) { |
| 4653 |
digit = digit - 'a' + 12; |
| 4654 |
} else { |
| 4655 |
ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit); |
| 4656 |
goto cleanup; |
| 4657 |
} |
| 4658 |
|
| 4659 |
rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000)); |
| 4660 |
|
| 4661 |
if (duration > 0 && (measured_samples = duration * ast_rtp_get_rate(rtp->f.subclass.format) / 1000) > rtp->send_duration) { |
| 4662 |
ast_debug_rtp(2, "(%p) RTP adjusting final end duration from %d to %u\n", |
| 4663 |
instance, rtp->send_duration, measured_samples); |
| 4664 |
rtp->send_duration = measured_samples; |
| 4665 |
} |
| 4666 |
|
| 4667 |
|
| 4668 |
rtpheader[1] = htonl(rtp->lastdigitts); |
| 4669 |
rtpheader[2] = htonl(rtp->ssrc); |
| 4670 |
rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration)); |
| 4671 |
rtpheader[3] |= htonl((1 << 23)); |
| 4672 |
|
| 4673 |
|
| 4674 |
for (i = 0; i < 3; i++) { |
| 4675 |
int ice; |
| 4676 |
|
| 4677 |
rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno)); |
| 4678 |
|
| 4679 |
res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice); |
| 4680 |
|
| 4681 |
if (res < 0) { |
| 4682 |
ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n", |
| 4683 |
ast_sockaddr_stringify(&remote_address), |
| 4684 |
strerror(errno)); |
| 4685 |
} |
| 4686 |
|
| 4687 |
if (rtp_debug_test_addr(&remote_address)) { |
| 4688 |
ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n", |
| 4689 |
ast_sockaddr_stringify(&remote_address), |
| 4690 |
ice ? " (via ICE)" : "", |
| 4691 |
rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen); |
| 4692 |
} |
| 4693 |
|
| 4694 |
rtp->seqno++; |
| 4695 |
} |
| 4696 |
res = 0; |
| 4697 |
|
| 4698 |
|
| 4699 |
rtp->lastts += calc_txstamp(rtp, NULL) * rtp->dtmf_samplerate_ms; |
| 4700 |
|
| 4701 |
|
| 4702 |
if (rtp->smoother) { |
| 4703 |
ast_smoother_free(rtp->smoother); |
| 4704 |
rtp->smoother = NULL; |
| 4705 |
} |
| 4706 |
cleanup: |
| 4707 |
rtp->sending_digit = 0; |
| 4708 |
rtp->send_digit = 0; |
| 4709 |
|
| 4710 |
|
| 4711 |
rtp->expectedseqno = -1; |
| 4712 |
|
| 4713 |
return res; |
| 4714 |
} |
| 4715 |
|
| 4716 |
|
| 4717 |
static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit) |
| 4718 |
{ |
| 4719 |
return ast_rtp_dtmf_end_with_duration(instance, digit, 0); |
| 4720 |
} |
| 4721 |
|
| 4722 |
|
| 4723 |
static void ast_rtp_update_source(struct ast_rtp_instance *instance) |
| 4724 |
{ |
| 4725 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 4726 |
|
| 4727 |
|
| 4728 |
ast_set_flag(rtp, FLAG_NEED_MARKER_BIT); |
| 4729 |
ast_debug_rtp(3, "(%p) RTP setting the marker bit due to a source update\n", instance); |
| 4730 |
|
| 4731 |
return; |
| 4732 |
} |
| 4733 |
|
| 4734 |
|
| 4735 |
static void ast_rtp_change_source(struct ast_rtp_instance *instance) |
| 4736 |
{ |
| 4737 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 4738 |
struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance, 0); |
| 4739 |
struct ast_srtp *rtcp_srtp = ast_rtp_instance_get_srtp(instance, 1); |
| 4740 |
unsigned int ssrc = ast_random(); |
| 4741 |
|
| 4742 |
if (rtp->lastts) { |
| 4743 |
|
| 4744 |
ast_set_flag(rtp, FLAG_NEED_MARKER_BIT); |
| 4745 |
} |
| 4746 |
|
| 4747 |
ast_debug_rtp(3, "(%p) RTP changing ssrc from %u to %u due to a source change\n", |
| 4748 |
instance, rtp->ssrc, ssrc); |
| 4749 |
|
| 4750 |
if (srtp) { |
| 4751 |
ast_debug_rtp(3, "(%p) RTP changing ssrc for SRTP from %u to %u\n", |
| 4752 |
instance, rtp->ssrc, ssrc); |
| 4753 |
res_srtp->change_source(srtp, rtp->ssrc, ssrc); |
| 4754 |
if (rtcp_srtp != srtp) { |
| 4755 |
res_srtp->change_source(rtcp_srtp, rtp->ssrc, ssrc); |
| 4756 |
} |
| 4757 |
} |
| 4758 |
|
| 4759 |
rtp->ssrc = ssrc; |
| 4760 |
|
| 4761 |
|
| 4762 |
rtp->expectedrxseqno = -1; |
| 4763 |
|
| 4764 |
return; |
| 4765 |
} |
| 4766 |
|
| 4767 |
static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw) |
| 4768 |
{ |
| 4769 |
unsigned int sec, usec, frac; |
| 4770 |
sec = tv.tv_sec + 2208988800u; |
| 4771 |
usec = tv.tv_usec; |
| 4772 |
|
| 4773 |
|
| 4774 |
|
| 4775 |
|
| 4776 |
|
| 4777 |
|
| 4778 |
|
| 4779 |
|
| 4780 |
|
| 4781 |
|
| 4782 |
|
| 4783 |
|
| 4784 |
|
| 4785 |
|
| 4786 |
|
| 4787 |
|
| 4788 |
|
| 4789 |
|
| 4790 |
|
| 4791 |
frac = ((((usec << 12) / 125) << 7) / 125) << 7; |
| 4792 |
*msw = sec; |
| 4793 |
*lsw = frac; |
| 4794 |
} |
| 4795 |
|
| 4796 |
static void ntp2timeval(unsigned int msw, unsigned int lsw, struct timeval *tv) |
| 4797 |
{ |
| 4798 |
tv->tv_sec = msw - 2208988800u; |
| 4799 |
|
| 4800 |
tv->tv_usec = ((((lsw >> 7) * 125) >> 7) * 125) >> 12; |
| 4801 |
} |
| 4802 |
|
| 4803 |
static void calculate_lost_packet_statistics(struct ast_rtp *rtp, |
| 4804 |
unsigned int *lost_packets, |
| 4805 |
int *fraction_lost) |
| 4806 |
{ |
| 4807 |
unsigned int extended_seq_no; |
| 4808 |
unsigned int expected_packets; |
| 4809 |
unsigned int expected_interval; |
| 4810 |
unsigned int received_interval; |
| 4811 |
int lost_interval; |
| 4812 |
|
| 4813 |
|
| 4814 |
extended_seq_no = rtp->cycles + rtp->lastrxseqno; |
| 4815 |
expected_packets = extended_seq_no - rtp->seedrxseqno + 1; |
| 4816 |
if (rtp->rxcount > expected_packets) { |
| 4817 |
expected_packets += rtp->rxcount - expected_packets; |
| 4818 |
} |
| 4819 |
*lost_packets = expected_packets - rtp->rxcount; |
| 4820 |
expected_interval = expected_packets - rtp->rtcp->expected_prior; |
| 4821 |
received_interval = rtp->rxcount - rtp->rtcp->received_prior; |
| 4822 |
if (received_interval > expected_interval) { |
| 4823 |
|
| 4824 |
|
| 4825 |
|
| 4826 |
|
| 4827 |
|
| 4828 |
expected_interval = received_interval; |
| 4829 |
} |
| 4830 |
lost_interval = expected_interval - received_interval; |
| 4831 |
if (expected_interval == 0 || lost_interval <= 0) { |
| 4832 |
*fraction_lost = 0; |
| 4833 |
} else { |
| 4834 |
*fraction_lost = (lost_interval << 8) / expected_interval; |
| 4835 |
} |
| 4836 |
|
| 4837 |
|
| 4838 |
rtp->rtcp->received_prior = rtp->rxcount; |
| 4839 |
rtp->rtcp->expected_prior = expected_packets; |
| 4840 |
|
| 4841 |
|
| 4842 |
|
| 4843 |
|
| 4844 |
|
| 4845 |
|
| 4846 |
|
| 4847 |
if (lost_interval <= 0) { |
| 4848 |
rtp->rtcp->rxlost = 0; |
| 4849 |
} else { |
| 4850 |
rtp->rtcp->rxlost = lost_interval; |
| 4851 |
} |
| 4852 |
if (rtp->rtcp->rxlost_count == 0) { |
| 4853 |
rtp->rtcp->minrxlost = rtp->rtcp->rxlost; |
| 4854 |
} |
| 4855 |
if (lost_interval && lost_interval < rtp->rtcp->minrxlost) { |
| 4856 |
rtp->rtcp->minrxlost = rtp->rtcp->rxlost; |
| 4857 |
} |
| 4858 |
if (lost_interval > rtp->rtcp->maxrxlost) { |
| 4859 |
rtp->rtcp->maxrxlost = rtp->rtcp->rxlost; |
| 4860 |
} |
| 4861 |
|
| 4862 |
calc_mean_and_standard_deviation(rtp->rtcp->rxlost, &rtp->rtcp->normdev_rxlost, |
| 4863 |
&rtp->rtcp->stdev_rxlost, &rtp->rtcp->rxlost_count); |
| 4864 |
} |
| 4865 |
|
| 4866 |
static int ast_rtcp_generate_report(struct ast_rtp_instance *instance, unsigned char *rtcpheader, |
| 4867 |
struct ast_rtp_rtcp_report *rtcp_report, int *sr) |
| 4868 |
{ |
| 4869 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 4870 |
int len = 0; |
| 4871 |
struct timeval now; |
| 4872 |
unsigned int now_lsw; |
| 4873 |
unsigned int now_msw; |
| 4874 |
unsigned int lost_packets; |
| 4875 |
int fraction_lost; |
| 4876 |
struct timeval dlsr = { 0, }; |
| 4877 |
struct ast_rtp_rtcp_report_block *report_block = NULL; |
| 4878 |
|
| 4879 |
if (!rtp || !rtp->rtcp) { |
| 4880 |
return 0; |
| 4881 |
} |
| 4882 |
|
| 4883 |
if (ast_sockaddr_isnull(&rtp->rtcp->them)) { |
| 4884 |
|
| 4885 |
return 0; |
| 4886 |
} |
| 4887 |
|
| 4888 |
if (!rtcp_report) { |
| 4889 |
return 1; |
| 4890 |
} |
| 4891 |
|
| 4892 |
*sr = rtp->txcount > rtp->rtcp->lastsrtxcount ? 1 : 0; |
| 4893 |
|
| 4894 |
|
| 4895 |
calculate_lost_packet_statistics(rtp, &lost_packets, &fraction_lost); |
| 4896 |
|
| 4897 |
|
| 4898 |
|
| 4899 |
|
| 4900 |
update_local_mes_stats(rtp); |
| 4901 |
|
| 4902 |
gettimeofday(&now, NULL); |
| 4903 |
rtcp_report->reception_report_count = rtp->themssrc_valid ? 1 : 0; |
| 4904 |
rtcp_report->ssrc = rtp->ssrc; |
| 4905 |
rtcp_report->type = *sr ? RTCP_PT_SR : RTCP_PT_RR; |
| 4906 |
if (*sr) { |
| 4907 |
rtcp_report->sender_information.ntp_timestamp = now; |
| 4908 |
rtcp_report->sender_information.rtp_timestamp = rtp->lastts; |
| 4909 |
rtcp_report->sender_information.packet_count = rtp->txcount; |
| 4910 |
rtcp_report->sender_information.octet_count = rtp->txoctetcount; |
| 4911 |
} |
| 4912 |
|
| 4913 |
if (rtp->themssrc_valid) { |
| 4914 |
report_block = ast_calloc(1, sizeof(*report_block)); |
| 4915 |
if (!report_block) { |
| 4916 |
return 1; |
| 4917 |
} |
| 4918 |
|
| 4919 |
rtcp_report->report_block[0] = report_block; |
| 4920 |
report_block->source_ssrc = rtp->themssrc; |
| 4921 |
report_block->lost_count.fraction = (fraction_lost & 0xff); |
| 4922 |
report_block->lost_count.packets = (lost_packets & 0xffffff); |
| 4923 |
report_block->highest_seq_no = (rtp->cycles | (rtp->lastrxseqno & 0xffff)); |
| 4924 |
report_block->ia_jitter = (unsigned int)rtp->rxjitter_samples; |
| 4925 |
report_block->lsr = rtp->rtcp->themrxlsr; |
| 4926 |
|
| 4927 |
if (!ast_tvzero(rtp->rtcp->rxlsr)) { |
| 4928 |
timersub(&now, &rtp->rtcp->rxlsr, &dlsr); |
| 4929 |
report_block->dlsr = (((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000; |
| 4930 |
} |
| 4931 |
} |
| 4932 |
timeval2ntp(rtcp_report->sender_information.ntp_timestamp, &now_msw, &now_lsw); |
| 4933 |
put_unaligned_uint32(rtcpheader + 4, htonl(rtcp_report->ssrc)); |
| 4934 |
len += 8; |
| 4935 |
if (*sr) { |
| 4936 |
put_unaligned_uint32(rtcpheader + len, htonl(now_msw)); |
| 4937 |
put_unaligned_uint32(rtcpheader + len + 4, htonl(now_lsw)); |
| 4938 |
put_unaligned_uint32(rtcpheader + len + 8, htonl(rtcp_report->sender_information.rtp_timestamp)); |
| 4939 |
put_unaligned_uint32(rtcpheader + len + 12, htonl(rtcp_report->sender_information.packet_count)); |
| 4940 |
put_unaligned_uint32(rtcpheader + len + 16, htonl(rtcp_report->sender_information.octet_count)); |
| 4941 |
len += 20; |
| 4942 |
} |
| 4943 |
if (report_block) { |
| 4944 |
put_unaligned_uint32(rtcpheader + len, htonl(report_block->source_ssrc)); |
| 4945 |
put_unaligned_uint32(rtcpheader + len + 4, htonl((report_block->lost_count.fraction << 24) | report_block->lost_count.packets)); |
| 4946 |
put_unaligned_uint32(rtcpheader + len + 8, htonl(report_block->highest_seq_no)); |
| 4947 |
put_unaligned_uint32(rtcpheader + len + 12, htonl(report_block->ia_jitter)); |
| 4948 |
put_unaligned_uint32(rtcpheader + len + 16, htonl(report_block->lsr)); |
| 4949 |
put_unaligned_uint32(rtcpheader + len + 20, htonl(report_block->dlsr)); |
| 4950 |
len += 24; |
| 4951 |
} |
| 4952 |
|
| 4953 |
put_unaligned_uint32(rtcpheader, htonl((2 << 30) | (rtcp_report->reception_report_count << 24) |
| 4954 |
| ((*sr ? RTCP_PT_SR : RTCP_PT_RR) << 16) | ((len/4)-1))); |
| 4955 |
|
| 4956 |
return len; |
| 4957 |
} |
| 4958 |
|
| 4959 |
static int ast_rtcp_calculate_sr_rr_statistics(struct ast_rtp_instance *instance, |
| 4960 |
struct ast_rtp_rtcp_report *rtcp_report, struct ast_sockaddr remote_address, int ice, int sr) |
| 4961 |
{ |
| 4962 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 4963 |
struct ast_rtp_rtcp_report_block *report_block = NULL; |
| 4964 |
RAII_VAR(struct ast_json *, message_blob, NULL, ast_json_unref); |
| 4965 |
|
| 4966 |
if (!rtp || !rtp->rtcp) { |
| 4967 |
return 0; |
| 4968 |
} |
| 4969 |
|
| 4970 |
if (ast_sockaddr_isnull(&rtp->rtcp->them)) { |
| 4971 |
return 0; |
| 4972 |
} |
| 4973 |
|
| 4974 |
if (!rtcp_report) { |
| 4975 |
return -1; |
| 4976 |
} |
| 4977 |
|
| 4978 |
report_block = rtcp_report->report_block[0]; |
| 4979 |
|
| 4980 |
if (sr) { |
| 4981 |
rtp->rtcp->txlsr = rtcp_report->sender_information.ntp_timestamp; |
| 4982 |
rtp->rtcp->sr_count++; |
| 4983 |
rtp->rtcp->lastsrtxcount = rtp->txcount; |
| 4984 |
} else { |
| 4985 |
rtp->rtcp->rr_count++; |
| 4986 |
} |
| 4987 |
|
| 4988 |
if (rtcp_debug_test_addr(&rtp->rtcp->them)) { |
| 4989 |
ast_verbose("* Sent RTCP %s to %s%s\n", sr ? "SR" : "RR", |
| 4990 |
ast_sockaddr_stringify(&remote_address), ice ? " (via ICE)" : ""); |
| 4991 |
ast_verbose(" Our SSRC: %u\n", rtcp_report->ssrc); |
| 4992 |
if (sr) { |
| 4993 |
ast_verbose(" Sent(NTP): %u.%06u\n", |
| 4994 |
(unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_sec, |
| 4995 |
(unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_usec); |
| 4996 |
ast_verbose(" Sent(RTP): %u\n", rtcp_report->sender_information.rtp_timestamp); |
| 4997 |
ast_verbose(" Sent packets: %u\n", rtcp_report->sender_information.packet_count); |
| 4998 |
ast_verbose(" Sent octets: %u\n", rtcp_report->sender_information.octet_count); |
| 4999 |
} |
| 5000 |
if (report_block) { |
| 5001 |
int rate = ast_rtp_get_rate(rtp->f.subclass.format); |
| 5002 |
ast_verbose(" Report block:\n"); |
| 5003 |
ast_verbose(" Their SSRC: %u\n", report_block->source_ssrc); |
| 5004 |
ast_verbose(" Fraction lost: %d\n", report_block->lost_count.fraction); |
| 5005 |
ast_verbose(" Cumulative loss: %u\n", report_block->lost_count.packets); |
| 5006 |
ast_verbose(" Highest seq no: %u\n", report_block->highest_seq_no); |
| 5007 |
ast_verbose(" IA jitter (samp): %u\n", report_block->ia_jitter); |
| 5008 |
ast_verbose(" IA jitter (secs): %.6f\n", ast_samp2sec(report_block->ia_jitter, rate)); |
| 5009 |
ast_verbose(" Their last SR: %u\n", report_block->lsr); |
| 5010 |
ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(report_block->dlsr / 65536.0)); |
| 5011 |
} |
| 5012 |
} |
| 5013 |
|
| 5014 |
message_blob = ast_json_pack("{s: s, s: s, s: f}", |
| 5015 |
"to", ast_sockaddr_stringify(&remote_address), |
| 5016 |
"from", rtp->rtcp->local_addr_str, |
| 5017 |
"mes", rtp->rxmes); |
| 5018 |
|
| 5019 |
ast_rtp_publish_rtcp_message(instance, ast_rtp_rtcp_sent_type(), |
| 5020 |
rtcp_report, message_blob); |
| 5021 |
|
| 5022 |
return 1; |
| 5023 |
} |
| 5024 |
|
| 5025 |
static int ast_rtcp_generate_sdes(struct ast_rtp_instance *instance, unsigned char *rtcpheader, |
| 5026 |
struct ast_rtp_rtcp_report *rtcp_report) |
| 5027 |
{ |
| 5028 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 5029 |
int len = 0; |
| 5030 |
uint16_t sdes_packet_len_bytes; |
| 5031 |
uint16_t sdes_packet_len_rounded; |
| 5032 |
|
| 5033 |
if (!rtp || !rtp->rtcp) { |
| 5034 |
return 0; |
| 5035 |
} |
| 5036 |
|
| 5037 |
if (ast_sockaddr_isnull(&rtp->rtcp->them)) { |
| 5038 |
return 0; |
| 5039 |
} |
| 5040 |
|
| 5041 |
if (!rtcp_report) { |
| 5042 |
return -1; |
| 5043 |
} |
| 5044 |
|
| 5045 |
sdes_packet_len_bytes = |
| 5046 |
4 + |
| 5047 |
4 + |
| 5048 |
1 + |
| 5049 |
1 + |
| 5050 |
AST_UUID_STR_LEN |
| 5051 |
; |
| 5052 |
|
| 5053 |
|
| 5054 |
sdes_packet_len_rounded = (sdes_packet_len_bytes + 3) & ~0x3; |
| 5055 |
|
| 5056 |
put_unaligned_uint32(rtcpheader, htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | ((sdes_packet_len_rounded / 4) - 1))); |
| 5057 |
put_unaligned_uint32(rtcpheader + 4, htonl(rtcp_report->ssrc)); |
| 5058 |
rtcpheader[8] = 0x01; |
| 5059 |
rtcpheader[9] = AST_UUID_STR_LEN - 1; |
| 5060 |
memcpy(rtcpheader + 10, rtp->cname, AST_UUID_STR_LEN); |
| 5061 |
len += 10 + AST_UUID_STR_LEN; |
| 5062 |
|
| 5063 |
|
| 5064 |
|
| 5065 |
|
| 5066 |
|
| 5067 |
|
| 5068 |
|
| 5069 |
|
| 5070 |
|
| 5071 |
|
| 5072 |
|
| 5073 |
len += (sdes_packet_len_rounded - sdes_packet_len_bytes); |
| 5074 |
|
| 5075 |
return len; |
| 5076 |
} |
| 5077 |
|
| 5078 |
|
| 5079 |
|
| 5080 |
|
| 5081 |
|
| 5082 |
|
| 5083 |
static int ast_rtcp_generate_compound_prefix(struct ast_rtp_instance *instance, unsigned char *rtcpheader, |
| 5084 |
struct ast_rtp_rtcp_report *report, int *sr) |
| 5085 |
{ |
| 5086 |
int packet_len = 0; |
| 5087 |
int res; |
| 5088 |
|
| 5089 |
|
| 5090 |
|
| 5091 |
|
| 5092 |
|
| 5093 |
|
| 5094 |
res = ast_rtcp_generate_report(instance, rtcpheader, report, sr); |
| 5095 |
|
| 5096 |
if (res == 0 || res == 1) { |
| 5097 |
ast_debug_rtcp(1, "(%p) RTCP failed to generate %s report!\n", instance, sr ? "SR" : "RR"); |
| 5098 |
return 0; |
| 5099 |
} |
| 5100 |
|
| 5101 |
packet_len += res; |
| 5102 |
|
| 5103 |
res = ast_rtcp_generate_sdes(instance, rtcpheader + packet_len, report); |
| 5104 |
|
| 5105 |
if (res == 0 || res == 1) { |
| 5106 |
ast_debug_rtcp(1, "(%p) RTCP failed to generate SDES!\n", instance); |
| 5107 |
return 0; |
| 5108 |
} |
| 5109 |
|
| 5110 |
return packet_len + res; |
| 5111 |
} |
| 5112 |
|
| 5113 |
static int ast_rtcp_generate_nack(struct ast_rtp_instance *instance, unsigned char *rtcpheader) |
| 5114 |
{ |
| 5115 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 5116 |
int packet_len; |
| 5117 |
int blp_index = -1; |
| 5118 |
int current_seqno; |
| 5119 |
unsigned int fci = 0; |
| 5120 |
size_t remaining_missing_seqno; |
| 5121 |
|
| 5122 |
if (!rtp || !rtp->rtcp) { |
| 5123 |
return 0; |
| 5124 |
} |
| 5125 |
|
| 5126 |
if (ast_sockaddr_isnull(&rtp->rtcp->them)) { |
| 5127 |
return 0; |
| 5128 |
} |
| 5129 |
|
| 5130 |
current_seqno = rtp->expectedrxseqno; |
| 5131 |
remaining_missing_seqno = AST_VECTOR_SIZE(&rtp->missing_seqno); |
| 5132 |
packet_len = 12; |
| 5133 |
|
| 5134 |
|
| 5135 |
if (!remaining_missing_seqno) { |
| 5136 |
return 0; |
| 5137 |
} |
| 5138 |
|
| 5139 |
|
| 5140 |
|
| 5141 |
|
| 5142 |
while (remaining_missing_seqno) { |
| 5143 |
int *missing_seqno; |
| 5144 |
|
| 5145 |
|
| 5146 |
|
| 5147 |
|
| 5148 |
blp_index++; |
| 5149 |
|
| 5150 |
missing_seqno = AST_VECTOR_GET_CMP(&rtp->missing_seqno, current_seqno, |
| 5151 |
find_by_value); |
| 5152 |
if (missing_seqno) { |
| 5153 |
|
| 5154 |
if (blp_index >= 17) { |
| 5155 |
put_unaligned_uint32(rtcpheader + packet_len, htonl(fci)); |
| 5156 |
fci = 0; |
| 5157 |
blp_index = 0; |
| 5158 |
packet_len += 4; |
| 5159 |
} |
| 5160 |
|
| 5161 |
if (blp_index == 0) { |
| 5162 |
fci |= (current_seqno << 16); |
| 5163 |
} else { |
| 5164 |
fci |= (1 << (blp_index - 1)); |
| 5165 |
} |
| 5166 |
|
| 5167 |
|
| 5168 |
remaining_missing_seqno--; |
| 5169 |
} |
| 5170 |
|
| 5171 |
|
| 5172 |
current_seqno++; |
| 5173 |
if (current_seqno == SEQNO_CYCLE_OVER) { |
| 5174 |
current_seqno = 0; |
| 5175 |
} |
| 5176 |
} |
| 5177 |
|
| 5178 |
put_unaligned_uint32(rtcpheader + packet_len, htonl(fci)); |
| 5179 |
packet_len += 4; |
| 5180 |
|
| 5181 |
|
| 5182 |
put_unaligned_uint32(rtcpheader, htonl((2 << 30) | (AST_RTP_RTCP_FMT_NACK << 24) |
| 5183 |
| (AST_RTP_RTCP_RTPFB << 16) | ((packet_len / 4) - 1))); |
| 5184 |
put_unaligned_uint32(rtcpheader + 4, htonl(rtp->ssrc)); |
| 5185 |
put_unaligned_uint32(rtcpheader + 8, htonl(rtp->themssrc)); |
| 5186 |
|
| 5187 |
return packet_len; |
| 5188 |
} |
| 5189 |
|
| 5190 |
|
| 5191 |
|
| 5192 |
|
| 5193 |
|
| 5194 |
|
| 5195 |
|
| 5196 |
|
| 5197 |
|
| 5198 |
static int ast_rtcp_write(const void *data) |
| 5199 |
{ |
| 5200 |
struct ast_rtp_instance *instance = (struct ast_rtp_instance *) data; |
| 5201 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 5202 |
int res; |
| 5203 |
int sr = 0; |
| 5204 |
int packet_len = 0; |
| 5205 |
int ice; |
| 5206 |
struct ast_sockaddr remote_address = { { 0, } }; |
| 5207 |
unsigned char *rtcpheader; |
| 5208 |
unsigned char bdata[AST_UUID_STR_LEN + 128] = ""; |
| 5209 |
RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report, NULL, ao2_cleanup); |
| 5210 |
|
| 5211 |
if (!rtp || !rtp->rtcp || rtp->rtcp->schedid == -1) { |
| 5212 |
ao2_ref(instance, -1); |
| 5213 |
return 0; |
| 5214 |
} |
| 5215 |
|
| 5216 |
ao2_lock(instance); |
| 5217 |
rtcpheader = bdata; |
| 5218 |
rtcp_report = ast_rtp_rtcp_report_alloc(rtp->themssrc_valid ? 1 : 0); |
| 5219 |
res = ast_rtcp_generate_compound_prefix(instance, rtcpheader, rtcp_report, &sr); |
| 5220 |
|
| 5221 |
if (res == 0 || res == 1) { |
| 5222 |
goto cleanup; |
| 5223 |
} |
| 5224 |
|
| 5225 |
packet_len += res; |
| 5226 |
|
| 5227 |
if (rtp->bundled) { |
| 5228 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 5229 |
} else { |
| 5230 |
ast_sockaddr_copy(&remote_address, &rtp->rtcp->them); |
| 5231 |
} |
| 5232 |
|
| 5233 |
res = rtcp_sendto(instance, (unsigned int *)rtcpheader, packet_len, 0, &remote_address, &ice); |
| 5234 |
if (res < 0) { |
| 5235 |
ast_log(LOG_ERROR, "RTCP %s transmission error to %s, rtcp halted %s\n", |
| 5236 |
sr ? "SR" : "RR", |
| 5237 |
ast_sockaddr_stringify(&rtp->rtcp->them), |
| 5238 |
strerror(errno)); |
| 5239 |
res = 0; |
| 5240 |
} else { |
| 5241 |
ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, remote_address, ice, sr); |
| 5242 |
} |
| 5243 |
|
| 5244 |
cleanup: |
| 5245 |
ao2_unlock(instance); |
| 5246 |
|
| 5247 |
if (!res) { |
| 5248 |
|
| 5249 |
|
| 5250 |
|
| 5251 |
rtp->rtcp->schedid = -1; |
| 5252 |
ao2_ref(instance, -1); |
| 5253 |
} |
| 5254 |
|
| 5255 |
return res; |
| 5256 |
} |
| 5257 |
|
| 5258 |
static void put_unaligned_time24(void *p, uint32_t time_msw, uint32_t time_lsw) |
| 5259 |
{ |
| 5260 |
unsigned char *cp = p; |
| 5261 |
uint32_t datum; |
| 5262 |
|
| 5263 |
|
| 5264 |
datum = (time_msw << 18) & 0x00fc0000; |
| 5265 |
datum |= (time_lsw >> 14) & 0x0003ffff; |
| 5266 |
|
| 5267 |
cp[0] = datum >> 16; |
| 5268 |
cp[1] = datum >> 8; |
| 5269 |
cp[2] = datum; |
| 5270 |
} |
| 5271 |
|
| 5272 |
|
| 5273 |
static int rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec) |
| 5274 |
{ |
| 5275 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 5276 |
int pred, mark = 0; |
| 5277 |
unsigned int ms = calc_txstamp(rtp, &frame->delivery); |
| 5278 |
struct ast_sockaddr remote_address = { {0,} }; |
| 5279 |
int rate = ast_rtp_get_rate(frame->subclass.format) / 1000; |
| 5280 |
unsigned int seqno; |
| 5281 |
#ifdef TEST_FRAMEWORK |
| 5282 |
struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance); |
| 5283 |
#endif |
| 5284 |
|
| 5285 |
if (ast_format_cmp(frame->subclass.format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) { |
| 5286 |
frame->samples /= 2; |
| 5287 |
} |
| 5288 |
|
| 5289 |
if (rtp->sending_digit) { |
| 5290 |
return 0; |
| 5291 |
} |
| 5292 |
|
| 5293 |
#ifdef TEST_FRAMEWORK |
| 5294 |
if (test && test->send_report) { |
| 5295 |
test->send_report = 0; |
| 5296 |
ast_rtcp_write(instance); |
| 5297 |
return 0; |
| 5298 |
} |
| 5299 |
#endif |
| 5300 |
|
| 5301 |
if (frame->frametype == AST_FRAME_VOICE) { |
| 5302 |
pred = rtp->lastts + frame->samples; |
| 5303 |
|
| 5304 |
|
| 5305 |
rtp->lastts = rtp->lastts + ms * rate; |
| 5306 |
if (ast_tvzero(frame->delivery)) { |
| 5307 |
|
| 5308 |
|
| 5309 |
if (abs((int)rtp->lastts - pred) < MAX_TIMESTAMP_SKEW) { |
| 5310 |
rtp->lastts = pred; |
| 5311 |
} else { |
| 5312 |
ast_debug_rtp(3, "(%p) RTP audio difference is %d, ms is %u\n", |
| 5313 |
instance, abs((int)rtp->lastts - pred), ms); |
| 5314 |
mark = 1; |
| 5315 |
} |
| 5316 |
} |
| 5317 |
} else if (frame->frametype == AST_FRAME_VIDEO) { |
| 5318 |
mark = frame->subclass.frame_ending; |
| 5319 |
pred = rtp->lastovidtimestamp + frame->samples; |
| 5320 |
|
| 5321 |
rtp->lastts = rtp->lastts + ms * 90; |
| 5322 |
|
| 5323 |
if (ast_tvzero(frame->delivery)) { |
| 5324 |
if (abs((int)rtp->lastts - pred) < 7200) { |
| 5325 |
rtp->lastts = pred; |
| 5326 |
rtp->lastovidtimestamp += frame->samples; |
| 5327 |
} else { |
| 5328 |
ast_debug_rtp(3, "(%p) RTP video difference is %d, ms is %u (%u), pred/ts/samples %u/%d/%d\n", |
| 5329 |
instance, abs((int)rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, frame->samples); |
| 5330 |
rtp->lastovidtimestamp = rtp->lastts; |
| 5331 |
} |
| 5332 |
} |
| 5333 |
} else { |
| 5334 |
pred = rtp->lastotexttimestamp + frame->samples; |
| 5335 |
|
| 5336 |
rtp->lastts = rtp->lastts + ms; |
| 5337 |
|
| 5338 |
if (ast_tvzero(frame->delivery)) { |
| 5339 |
if (abs((int)rtp->lastts - pred) < 7200) { |
| 5340 |
rtp->lastts = pred; |
| 5341 |
rtp->lastotexttimestamp += frame->samples; |
| 5342 |
} else { |
| 5343 |
ast_debug_rtp(3, "(%p) RTP other difference is %d, ms is %u, pred/ts/samples %u/%d/%d\n", |
| 5344 |
instance, abs((int)rtp->lastts - pred), ms, rtp->lastts, pred, frame->samples); |
| 5345 |
rtp->lastotexttimestamp = rtp->lastts; |
| 5346 |
} |
| 5347 |
} |
| 5348 |
} |
| 5349 |
|
| 5350 |
|
| 5351 |
if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) { |
| 5352 |
mark = 1; |
| 5353 |
ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT); |
| 5354 |
} |
| 5355 |
|
| 5356 |
|
| 5357 |
if (rtp->lastts > rtp->lastdigitts) { |
| 5358 |
rtp->lastdigitts = rtp->lastts; |
| 5359 |
} |
| 5360 |
|
| 5361 |
|
| 5362 |
seqno = rtp->seqno; |
| 5363 |
|
| 5364 |
|
| 5365 |
if (ast_test_flag(frame, AST_FRFLAG_HAS_SEQUENCE_NUMBER)) { |
| 5366 |
if (rtp->expectedseqno != -1) { |
| 5367 |
|
| 5368 |
int difference = frame->seqno - rtp->expectedseqno; |
| 5369 |
|
| 5370 |
|
| 5371 |
|
| 5372 |
|
| 5373 |
|
| 5374 |
|
| 5375 |
if (abs(difference) > 100) { |
| 5376 |
difference = 0; |
| 5377 |
} |
| 5378 |
|
| 5379 |
|
| 5380 |
seqno += difference; |
| 5381 |
|
| 5382 |
if (difference >= 0) { |
| 5383 |
|
| 5384 |
rtp->expectedseqno = frame->seqno + 1; |
| 5385 |
rtp->seqno += difference; |
| 5386 |
} |
| 5387 |
} else { |
| 5388 |
|
| 5389 |
rtp->expectedseqno = frame->seqno + 1; |
| 5390 |
} |
| 5391 |
} else { |
| 5392 |
rtp->expectedseqno = -1; |
| 5393 |
} |
| 5394 |
|
| 5395 |
if (ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO)) { |
| 5396 |
rtp->lastts = frame->ts * rate; |
| 5397 |
} |
| 5398 |
|
| 5399 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 5400 |
|
| 5401 |
|
| 5402 |
if (!ast_sockaddr_isnull(&remote_address)) { |
| 5403 |
int hdrlen = 12; |
| 5404 |
int res; |
| 5405 |
int ice; |
| 5406 |
int ext = 0; |
| 5407 |
int abs_send_time_id; |
| 5408 |
int packet_len; |
| 5409 |
unsigned char *rtpheader; |
| 5410 |
|
| 5411 |
|
| 5412 |
abs_send_time_id = ast_rtp_instance_extmap_get_id(instance, AST_RTP_EXTENSION_ABS_SEND_TIME); |
| 5413 |
if (abs_send_time_id != -1) { |
| 5414 |
|
| 5415 |
hdrlen += 8; |
| 5416 |
ext = 1; |
| 5417 |
} |
| 5418 |
|
| 5419 |
packet_len = frame->datalen + hdrlen; |
| 5420 |
rtpheader = (unsigned char *)(frame->data.ptr - hdrlen); |
| 5421 |
|
| 5422 |
put_unaligned_uint32(rtpheader, htonl((2 << 30) | (ext << 28) | (codec << 16) | (seqno) | (mark << 23))); |
| 5423 |
put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts)); |
| 5424 |
put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc)); |
| 5425 |
|
| 5426 |
|
| 5427 |
|
| 5428 |
|
| 5429 |
if (abs_send_time_id != -1) { |
| 5430 |
unsigned int now_msw; |
| 5431 |
unsigned int now_lsw; |
| 5432 |
|
| 5433 |
|
| 5434 |
|
| 5435 |
|
| 5436 |
put_unaligned_uint32(rtpheader + 12, htonl((0xBEDE << 16) | 1)); |
| 5437 |
rtpheader[16] = (abs_send_time_id << 4) | 2; |
| 5438 |
|
| 5439 |
timeval2ntp(ast_tvnow(), &now_msw, &now_lsw); |
| 5440 |
put_unaligned_time24(rtpheader + 17, now_msw, now_lsw); |
| 5441 |
} |
| 5442 |
|
| 5443 |
|
| 5444 |
if (rtp->send_buffer) { |
| 5445 |
struct ast_rtp_rtcp_nack_payload *payload; |
| 5446 |
|
| 5447 |
payload = ast_malloc(sizeof(*payload) + packet_len); |
| 5448 |
if (payload) { |
| 5449 |
payload->size = packet_len; |
| 5450 |
memcpy(payload->buf, rtpheader, packet_len); |
| 5451 |
if (ast_data_buffer_put(rtp->send_buffer, rtp->seqno, payload) == -1) { |
| 5452 |
ast_free(payload); |
| 5453 |
} |
| 5454 |
} |
| 5455 |
} |
| 5456 |
|
| 5457 |
res = rtp_sendto(instance, (void *)rtpheader, packet_len, 0, &remote_address, &ice); |
| 5458 |
if (res < 0) { |
| 5459 |
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))) { |
| 5460 |
ast_debug_rtp(1, "(%p) RTP transmission error of packet %d to %s: %s\n", |
| 5461 |
instance, rtp->seqno, |
| 5462 |
ast_sockaddr_stringify(&remote_address), |
| 5463 |
strerror(errno)); |
| 5464 |
} else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || ast_debug_rtp_packet_is_allowed) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) { |
| 5465 |
|
| 5466 |
if (ast_debug_rtp_packet_is_allowed) |
| 5467 |
ast_debug(0, "(%p) RTP NAT: Can't write RTP to private address %s, waiting for other end to send audio...\n", |
| 5468 |
instance, ast_sockaddr_stringify(&remote_address)); |
| 5469 |
ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN); |
| 5470 |
} |
| 5471 |
} else { |
| 5472 |
if (rtp->rtcp && rtp->rtcp->schedid < 0) { |
| 5473 |
ast_debug_rtcp(2, "(%s) RTCP starting transmission in %u ms\n", |
| 5474 |
ast_rtp_instance_get_channel_id(instance), ast_rtcp_calc_interval(rtp)); |
| 5475 |
ao2_ref(instance, +1); |
| 5476 |
rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, instance); |
| 5477 |
if (rtp->rtcp->schedid < 0) { |
| 5478 |
ao2_ref(instance, -1); |
| 5479 |
ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n"); |
| 5480 |
} |
| 5481 |
} |
| 5482 |
} |
| 5483 |
|
| 5484 |
if (rtp_debug_test_addr(&remote_address)) { |
| 5485 |
ast_verbose("Sent RTP packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n", |
| 5486 |
ast_sockaddr_stringify(&remote_address), |
| 5487 |
ice ? " (via ICE)" : "", |
| 5488 |
codec, rtp->seqno, rtp->lastts, res - hdrlen); |
| 5489 |
} |
| 5490 |
} |
| 5491 |
|
| 5492 |
|
| 5493 |
|
| 5494 |
|
| 5495 |
|
| 5496 |
if (seqno == rtp->seqno) { |
| 5497 |
rtp->seqno++; |
| 5498 |
} |
| 5499 |
|
| 5500 |
return 0; |
| 5501 |
} |
| 5502 |
|
| 5503 |
static struct ast_frame *red_t140_to_red(struct rtp_red *red) |
| 5504 |
{ |
| 5505 |
unsigned char *data = red->t140red.data.ptr; |
| 5506 |
int len = 0; |
| 5507 |
int i; |
| 5508 |
|
| 5509 |
|
| 5510 |
if (red->len[0]) { |
| 5511 |
for (i = 1; i < red->num_gen+1; i++) |
| 5512 |
len += red->len[i]; |
| 5513 |
|
| 5514 |
memmove(&data[red->hdrlen], &data[red->hdrlen+red->len[0]], len); |
| 5515 |
} |
| 5516 |
|
| 5517 |
|
| 5518 |
for (i = 0; i < red->num_gen; i++) |
| 5519 |
red->len[i] = red->len[i+1]; |
| 5520 |
|
| 5521 |
|
| 5522 |
|
| 5523 |
|
| 5524 |
|
| 5525 |
|
| 5526 |
if (red->t140.datalen > UCHAR_MAX) { |
| 5527 |
ast_log(LOG_WARNING, "New T.140 frame of %d bytes exceeds max of %u. Discarding %d bytes.\n", |
| 5528 |
red->t140.datalen, UCHAR_MAX, red->t140.datalen - UCHAR_MAX); |
| 5529 |
red->t140.datalen = UCHAR_MAX; |
| 5530 |
} |
| 5531 |
red->len[i] = red->t140.datalen; |
| 5532 |
|
| 5533 |
|
| 5534 |
len = red->hdrlen; |
| 5535 |
for (i = 0; i < red->num_gen; i++) { |
| 5536 |
len += data[i*4+3] = red->len[i]; |
| 5537 |
} |
| 5538 |
|
| 5539 |
|
| 5540 |
memcpy(&data[len], red->t140.data.ptr, red->t140.datalen); |
| 5541 |
red->t140red.datalen = len + red->t140.datalen; |
| 5542 |
|
| 5543 |
|
| 5544 |
if (len == red->hdrlen && !red->t140.datalen) { |
| 5545 |
return NULL; |
| 5546 |
} |
| 5547 |
|
| 5548 |
|
| 5549 |
red->t140.datalen = 0; |
| 5550 |
|
| 5551 |
return &red->t140red; |
| 5552 |
} |
| 5553 |
|
| 5554 |
static void rtp_write_rtcp_fir(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *remote_address) |
| 5555 |
{ |
| 5556 |
unsigned char *rtcpheader; |
| 5557 |
unsigned char bdata[1024]; |
| 5558 |
int packet_len = 0; |
| 5559 |
int fir_len = 20; |
| 5560 |
int ice; |
| 5561 |
int res; |
| 5562 |
int sr; |
| 5563 |
RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report, NULL, ao2_cleanup); |
| 5564 |
|
| 5565 |
if (!rtp || !rtp->rtcp) { |
| 5566 |
return; |
| 5567 |
} |
| 5568 |
|
| 5569 |
if (ast_sockaddr_isnull(&rtp->rtcp->them) || rtp->rtcp->schedid < 0) { |
| 5570 |
|
| 5571 |
|
| 5572 |
|
| 5573 |
return; |
| 5574 |
} |
| 5575 |
|
| 5576 |
if (!rtp->themssrc_valid) { |
| 5577 |
|
| 5578 |
return; |
| 5579 |
} |
| 5580 |
|
| 5581 |
|
| 5582 |
rtp->rtcp->firseq++; |
| 5583 |
if(rtp->rtcp->firseq == 256) { |
| 5584 |
rtp->rtcp->firseq = 0; |
| 5585 |
} |
| 5586 |
|
| 5587 |
rtcpheader = bdata; |
| 5588 |
|
| 5589 |
ao2_lock(instance); |
| 5590 |
rtcp_report = ast_rtp_rtcp_report_alloc(rtp->themssrc_valid ? 1 : 0); |
| 5591 |
res = ast_rtcp_generate_compound_prefix(instance, rtcpheader, rtcp_report, &sr); |
| 5592 |
|
| 5593 |
if (res == 0 || res == 1) { |
| 5594 |
ao2_unlock(instance); |
| 5595 |
return; |
| 5596 |
} |
| 5597 |
|
| 5598 |
packet_len += res; |
| 5599 |
|
| 5600 |
put_unaligned_uint32(rtcpheader + packet_len + 0, htonl((2 << 30) | (4 << 24) | (RTCP_PT_PSFB << 16) | ((fir_len/4)-1))); |
| 5601 |
put_unaligned_uint32(rtcpheader + packet_len + 4, htonl(rtp->ssrc)); |
| 5602 |
put_unaligned_uint32(rtcpheader + packet_len + 8, htonl(rtp->themssrc)); |
| 5603 |
put_unaligned_uint32(rtcpheader + packet_len + 12, htonl(rtp->themssrc)); |
| 5604 |
put_unaligned_uint32(rtcpheader + packet_len + 16, htonl(rtp->rtcp->firseq << 24)); |
| 5605 |
res = rtcp_sendto(instance, (unsigned int *)rtcpheader, packet_len + fir_len, 0, rtp->bundled ? remote_address : &rtp->rtcp->them, &ice); |
| 5606 |
if (res < 0) { |
| 5607 |
ast_log(LOG_ERROR, "RTCP FIR transmission error: %s\n", strerror(errno)); |
| 5608 |
} else { |
| 5609 |
ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, rtp->bundled ? *remote_address : rtp->rtcp->them, ice, sr); |
| 5610 |
} |
| 5611 |
|
| 5612 |
ao2_unlock(instance); |
| 5613 |
} |
| 5614 |
|
| 5615 |
static void rtp_write_rtcp_psfb(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_frame *frame, struct ast_sockaddr *remote_address) |
| 5616 |
{ |
| 5617 |
struct ast_rtp_rtcp_feedback *feedback = frame->data.ptr; |
| 5618 |
unsigned char *rtcpheader; |
| 5619 |
unsigned char bdata[1024]; |
| 5620 |
int remb_len = 24; |
| 5621 |
int ice; |
| 5622 |
int res; |
| 5623 |
int sr = 0; |
| 5624 |
int packet_len = 0; |
| 5625 |
RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report, NULL, ao2_cleanup); |
| 5626 |
|
| 5627 |
if (feedback->fmt != AST_RTP_RTCP_FMT_REMB) { |
| 5628 |
ast_debug_rtcp(1, "(%p) RTCP provided feedback frame of format %d to write, but only REMB is supported\n", |
| 5629 |
instance, feedback->fmt); |
| 5630 |
return; |
| 5631 |
} |
| 5632 |
|
| 5633 |
if (!rtp || !rtp->rtcp) { |
| 5634 |
return; |
| 5635 |
} |
| 5636 |
|
| 5637 |
|
| 5638 |
if (!ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_REMB)) { |
| 5639 |
ast_debug_rtcp(1, "(%p) RTCP provided feedback REMB report to write, but REMB support not enabled\n", |
| 5640 |
instance); |
| 5641 |
return; |
| 5642 |
} |
| 5643 |
|
| 5644 |
if (ast_sockaddr_isnull(&rtp->rtcp->them) || rtp->rtcp->schedid < 0) { |
| 5645 |
|
| 5646 |
|
| 5647 |
|
| 5648 |
return; |
| 5649 |
} |
| 5650 |
|
| 5651 |
rtcpheader = bdata; |
| 5652 |
|
| 5653 |
ao2_lock(instance); |
| 5654 |
rtcp_report = ast_rtp_rtcp_report_alloc(rtp->themssrc_valid ? 1 : 0); |
| 5655 |
res = ast_rtcp_generate_compound_prefix(instance, rtcpheader, rtcp_report, &sr); |
| 5656 |
|
| 5657 |
if (res == 0 || res == 1) { |
| 5658 |
ao2_unlock(instance); |
| 5659 |
return; |
| 5660 |
} |
| 5661 |
|
| 5662 |
packet_len += res; |
| 5663 |
|
| 5664 |
put_unaligned_uint32(rtcpheader + packet_len + 0, htonl((2 << 30) | (AST_RTP_RTCP_FMT_REMB << 24) | (RTCP_PT_PSFB << 16) | ((remb_len/4)-1))); |
| 5665 |
put_unaligned_uint32(rtcpheader + packet_len + 4, htonl(rtp->ssrc)); |
| 5666 |
put_unaligned_uint32(rtcpheader + packet_len + 8, htonl(0)); |
| 5667 |
put_unaligned_uint32(rtcpheader + packet_len + 12, htonl(('R' << 24) | ('E' << 16) | ('M' << 8) | ('B'))); |
| 5668 |
put_unaligned_uint32(rtcpheader + packet_len + 16, htonl((1 << 24) | (feedback->remb.br_exp << 18) | (feedback->remb.br_mantissa))); |
| 5669 |
put_unaligned_uint32(rtcpheader + packet_len + 20, htonl(rtp->ssrc)); |
| 5670 |
res = rtcp_sendto(instance, (unsigned int *)rtcpheader, packet_len + remb_len, 0, rtp->bundled ? remote_address : &rtp->rtcp->them, &ice); |
| 5671 |
if (res < 0) { |
| 5672 |
ast_log(LOG_ERROR, "RTCP PSFB transmission error: %s\n", strerror(errno)); |
| 5673 |
} else { |
| 5674 |
ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, rtp->bundled ? *remote_address : rtp->rtcp->them, ice, sr); |
| 5675 |
} |
| 5676 |
|
| 5677 |
ao2_unlock(instance); |
| 5678 |
} |
| 5679 |
|
| 5680 |
|
| 5681 |
static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame) |
| 5682 |
{ |
| 5683 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 5684 |
struct ast_sockaddr remote_address = { {0,} }; |
| 5685 |
struct ast_format *format; |
| 5686 |
int codec; |
| 5687 |
|
| 5688 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 5689 |
|
| 5690 |
|
| 5691 |
if (ast_sockaddr_isnull(&remote_address)) { |
| 5692 |
ast_debug_rtp(1, "(%p) RTP no remote address on instance, so dropping frame\n", instance); |
| 5693 |
return 0; |
| 5694 |
} |
| 5695 |
|
| 5696 |
|
| 5697 |
if (frame->frametype == AST_FRAME_CONTROL && frame->subclass.integer == AST_CONTROL_VIDUPDATE) { |
| 5698 |
rtp_write_rtcp_fir(instance, rtp, &remote_address); |
| 5699 |
return 0; |
| 5700 |
} else if (frame->frametype == AST_FRAME_RTCP) { |
| 5701 |
if (frame->subclass.integer == AST_RTP_RTCP_PSFB) { |
| 5702 |
rtp_write_rtcp_psfb(instance, rtp, frame, &remote_address); |
| 5703 |
} |
| 5704 |
return 0; |
| 5705 |
} |
| 5706 |
|
| 5707 |
|
| 5708 |
if (!frame->datalen) { |
| 5709 |
ast_debug_rtp(1, "(%p) RTP received frame with no data for instance, so dropping frame\n", instance); |
| 5710 |
return 0; |
| 5711 |
} |
| 5712 |
|
| 5713 |
|
| 5714 |
if (frame->frametype != AST_FRAME_VOICE && frame->frametype != AST_FRAME_VIDEO && frame->frametype != AST_FRAME_TEXT) { |
| 5715 |
ast_log(LOG_WARNING, "RTP can only send voice, video, and text\n"); |
| 5716 |
return -1; |
| 5717 |
} |
| 5718 |
|
| 5719 |
if (rtp->red) { |
| 5720 |
|
| 5721 |
|
| 5722 |
if ((frame = red_t140_to_red(rtp->red)) == NULL) |
| 5723 |
return 0; |
| 5724 |
} |
| 5725 |
|
| 5726 |
|
| 5727 |
codec = ast_rtp_codecs_payload_code_tx(ast_rtp_instance_get_codecs(instance), |
| 5728 |
1, frame->subclass.format, 0); |
| 5729 |
if (codec < 0) { |
| 5730 |
ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", |
| 5731 |
ast_format_get_name(frame->subclass.format)); |
| 5732 |
return -1; |
| 5733 |
} |
| 5734 |
|
| 5735 |
|
| 5736 |
|
| 5737 |
|
| 5738 |
format = frame->subclass.format; |
| 5739 |
if (ast_format_cmp(rtp->lasttxformat, format) == AST_FORMAT_CMP_NOT_EQUAL) { |
| 5740 |
|
| 5741 |
ast_debug_rtp(3, "(%s) RTP ooh, format changed from %s to %s\n", |
| 5742 |
ast_rtp_instance_get_channel_id(instance), |
| 5743 |
ast_format_get_name(rtp->lasttxformat), |
| 5744 |
ast_format_get_name(frame->subclass.format)); |
| 5745 |
ao2_replace(rtp->lasttxformat, format); |
| 5746 |
if (rtp->smoother) { |
| 5747 |
ast_smoother_free(rtp->smoother); |
| 5748 |
rtp->smoother = NULL; |
| 5749 |
} |
| 5750 |
} |
| 5751 |
|
| 5752 |
|
| 5753 |
if (!rtp->smoother && ast_format_can_be_smoothed(format)) { |
| 5754 |
unsigned int smoother_flags = ast_format_get_smoother_flags(format); |
| 5755 |
unsigned int framing_ms = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(instance)); |
| 5756 |
|
| 5757 |
if (!framing_ms && (smoother_flags & AST_SMOOTHER_FLAG_FORCED)) { |
| 5758 |
framing_ms = ast_format_get_default_ms(format); |
| 5759 |
} |
| 5760 |
|
| 5761 |
if (framing_ms) { |
| 5762 |
rtp->smoother = ast_smoother_new((framing_ms * ast_format_get_minimum_bytes(format)) / ast_format_get_minimum_ms(format)); |
| 5763 |
if (!rtp->smoother) { |
| 5764 |
ast_log(LOG_WARNING, "Unable to create smoother: format %s ms: %u len: %u\n", |
| 5765 |
ast_format_get_name(format), framing_ms, ast_format_get_minimum_bytes(format)); |
| 5766 |
return -1; |
| 5767 |
} |
| 5768 |
ast_smoother_set_flags(rtp->smoother, smoother_flags); |
| 5769 |
} |
| 5770 |
} |
| 5771 |
|
| 5772 |
|
| 5773 |
if (rtp->smoother) { |
| 5774 |
struct ast_frame *f; |
| 5775 |
|
| 5776 |
if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) { |
| 5777 |
ast_smoother_feed_be(rtp->smoother, frame); |
| 5778 |
} else { |
| 5779 |
ast_smoother_feed(rtp->smoother, frame); |
| 5780 |
} |
| 5781 |
|
| 5782 |
while ((f = ast_smoother_read(rtp->smoother)) && (f->data.ptr)) { |
| 5783 |
rtp_raw_write(instance, f, codec); |
| 5784 |
} |
| 5785 |
} else { |
| 5786 |
int hdrlen = 12; |
| 5787 |
struct ast_frame *f = NULL; |
| 5788 |
|
| 5789 |
if (frame->offset < hdrlen) { |
| 5790 |
f = ast_frdup(frame); |
| 5791 |
} else { |
| 5792 |
f = frame; |
| 5793 |
} |
| 5794 |
if (f->data.ptr) { |
| 5795 |
rtp_raw_write(instance, f, codec); |
| 5796 |
} |
| 5797 |
if (f != frame) { |
| 5798 |
ast_frfree(f); |
| 5799 |
} |
| 5800 |
|
| 5801 |
} |
| 5802 |
|
| 5803 |
return 0; |
| 5804 |
} |
| 5805 |
|
| 5806 |
static void calc_rxstamp_and_jitter(struct timeval *tv, |
| 5807 |
struct ast_rtp *rtp, unsigned int rx_rtp_ts, |
| 5808 |
int mark) |
| 5809 |
{ |
| 5810 |
int rate = ast_rtp_get_rate(rtp->f.subclass.format); |
| 5811 |
|
| 5812 |
double jitter = 0.0; |
| 5813 |
double prev_jitter = 0.0; |
| 5814 |
struct timeval now; |
| 5815 |
struct timeval tmp; |
| 5816 |
double rxnow; |
| 5817 |
double arrival_sec; |
| 5818 |
unsigned int arrival; |
| 5819 |
int transit; |
| 5820 |
int d; |
| 5821 |
|
| 5822 |
gettimeofday(&now,NULL); |
| 5823 |
|
| 5824 |
if (rtp->rxcount == 1 || mark) { |
| 5825 |
rtp->rxstart = ast_tv2double(&now); |
| 5826 |
rtp->remote_seed_rx_rtp_ts = rx_rtp_ts; |
| 5827 |
|
| 5828 |
|
| 5829 |
|
| 5830 |
|
| 5831 |
|
| 5832 |
|
| 5833 |
|
| 5834 |
|
| 5835 |
|
| 5836 |
|
| 5837 |
|
| 5838 |
|
| 5839 |
rtp->rxcore = now; |
| 5840 |
tmp = ast_samp2tv(rx_rtp_ts, rate); |
| 5841 |
rtp->rxcore = ast_tvsub(rtp->rxcore, tmp); |
| 5842 |
rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100; |
| 5843 |
*tv = ast_tvadd(rtp->rxcore, tmp); |
| 5844 |
|
| 5845 |
ast_debug_rtcp(3, "%s: " |
| 5846 |
"Seed ts: %u current time: %f\n", |
| 5847 |
ast_rtp_instance_get_channel_id(rtp->owner) |
| 5848 |
, rx_rtp_ts |
| 5849 |
, rtp->rxstart |
| 5850 |
); |
| 5851 |
|
| 5852 |
return; |
| 5853 |
} |
| 5854 |
|
| 5855 |
tmp = ast_samp2tv(rx_rtp_ts, rate); |
| 5856 |
|
| 5857 |
|
| 5858 |
|
| 5859 |
|
| 5860 |
|
| 5861 |
|
| 5862 |
*tv = ast_tvadd(rtp->rxcore, tmp); |
| 5863 |
|
| 5864 |
|
| 5865 |
|
| 5866 |
|
| 5867 |
|
| 5868 |
if (rtp->rxcount < RTP_IGNORE_FIRST_PACKETS_COUNT) { |
| 5869 |
ast_debug_rtcp(3, "%s: Packet %d < %d. Ignoring\n", |
| 5870 |
ast_rtp_instance_get_channel_id(rtp->owner) |
| 5871 |
, rtp->rxcount |
| 5872 |
, RTP_IGNORE_FIRST_PACKETS_COUNT |
| 5873 |
); |
| 5874 |
|
| 5875 |
return; |
| 5876 |
} |
| 5877 |
|
| 5878 |
|
| 5879 |
|
| 5880 |
|
| 5881 |
|
| 5882 |
if (rtp->rxcount == RTP_IGNORE_FIRST_PACKETS_COUNT) { |
| 5883 |
rtp->rxstart_stable = ast_tv2double(&now); |
| 5884 |
rtp->remote_seed_rx_rtp_ts_stable = rx_rtp_ts; |
| 5885 |
rtp->last_transit_time_samples = -rx_rtp_ts; |
| 5886 |
|
| 5887 |
ast_debug_rtcp(3, "%s: " |
| 5888 |
"pkt: %5u Stable Seed ts: %u current time: %f\n", |
| 5889 |
ast_rtp_instance_get_channel_id(rtp->owner) |
| 5890 |
, rtp->rxcount |
| 5891 |
, rx_rtp_ts |
| 5892 |
, rtp->rxstart_stable |
| 5893 |
); |
| 5894 |
|
| 5895 |
return; |
| 5896 |
} |
| 5897 |
|
| 5898 |
|
| 5899 |
|
| 5900 |
|
| 5901 |
|
| 5902 |
|
| 5903 |
if (rtp->lastrxseqno != rtp->prevrxseqno + 1) { |
| 5904 |
ast_debug_rtcp(3, "%s: Current packet seq %d != last packet seq %d + 1. Ignoring\n", |
| 5905 |
ast_rtp_instance_get_channel_id(rtp->owner) |
| 5906 |
, rtp->lastrxseqno |
| 5907 |
, rtp->prevrxseqno |
| 5908 |
); |
| 5909 |
|
| 5910 |
return; |
| 5911 |
} |
| 5912 |
|
| 5913 |
|
| 5914 |
|
| 5915 |
|
| 5916 |
|
| 5917 |
|
| 5918 |
|
| 5919 |
|
| 5920 |
|
| 5921 |
|
| 5922 |
|
| 5923 |
|
| 5924 |
|
| 5925 |
|
| 5926 |
rxnow = ast_tv2double(&now); |
| 5927 |
arrival_sec = rxnow - rtp->rxstart_stable; |
| 5928 |
arrival = ast_sec2samp(arrival_sec, rate); |
| 5929 |
|
| 5930 |
|
| 5931 |
|
| 5932 |
|
| 5933 |
|
| 5934 |
|
| 5935 |
|
| 5936 |
|
| 5937 |
|
| 5938 |
|
| 5939 |
|
| 5940 |
|
| 5941 |
|
| 5942 |
|
| 5943 |
|
| 5944 |
transit = arrival - rx_rtp_ts; |
| 5945 |
d = transit - rtp->last_transit_time_samples; |
| 5946 |
|
| 5947 |
if (d < 0) { |
| 5948 |
d = -d; |
| 5949 |
} |
| 5950 |
|
| 5951 |
prev_jitter = rtp->rxjitter_samples; |
| 5952 |
jitter = (1.0/16.0) * (((double)d) - prev_jitter); |
| 5953 |
rtp->rxjitter_samples = prev_jitter + jitter; |
| 5954 |
|
| 5955 |
|
| 5956 |
|
| 5957 |
|
| 5958 |
rtp->rxjitter = ast_samp2sec(rtp->rxjitter_samples, rate); |
| 5959 |
|
| 5960 |
ast_debug_rtcp(3, "%s: pkt: %5u " |
| 5961 |
"Arrival sec: %7.3f Arrival ts: %10u RX ts: %10u " |
| 5962 |
"Transit samp: %6d Last transit samp: %6d d: %4d " |
| 5963 |
"Curr jitter: %7.0f(%7.3f) Prev Jitter: %7.0f(%7.3f) New Jitter: %7.0f(%7.3f)\n", |
| 5964 |
ast_rtp_instance_get_channel_id(rtp->owner) |
| 5965 |
, rtp->rxcount |
| 5966 |
, arrival_sec |
| 5967 |
, arrival |
| 5968 |
, rx_rtp_ts |
| 5969 |
, transit |
| 5970 |
, rtp->last_transit_time_samples |
| 5971 |
, d |
| 5972 |
, jitter |
| 5973 |
, ast_samp2sec(jitter, rate) |
| 5974 |
, prev_jitter |
| 5975 |
, ast_samp2sec(prev_jitter, rate) |
| 5976 |
, rtp->rxjitter_samples |
| 5977 |
, rtp->rxjitter |
| 5978 |
); |
| 5979 |
|
| 5980 |
rtp->last_transit_time_samples = transit; |
| 5981 |
|
| 5982 |
|
| 5983 |
|
| 5984 |
|
| 5985 |
if (rtp->rtcp) { |
| 5986 |
if (rtp->rxjitter > rtp->rtcp->maxrxjitter) |
| 5987 |
rtp->rtcp->maxrxjitter = rtp->rxjitter; |
| 5988 |
if (rtp->rtcp->rxjitter_count == 1) |
| 5989 |
rtp->rtcp->minrxjitter = rtp->rxjitter; |
| 5990 |
if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter) |
| 5991 |
rtp->rtcp->minrxjitter = rtp->rxjitter; |
| 5992 |
|
| 5993 |
calc_mean_and_standard_deviation(rtp->rxjitter, |
| 5994 |
&rtp->rtcp->normdev_rxjitter, &rtp->rtcp->stdev_rxjitter, |
| 5995 |
&rtp->rtcp->rxjitter_count); |
| 5996 |
} |
| 5997 |
|
| 5998 |
return; |
| 5999 |
} |
| 6000 |
|
| 6001 |
static struct ast_frame *create_dtmf_frame(struct ast_rtp_instance *instance, enum ast_frame_type type, int compensate) |
| 6002 |
{ |
| 6003 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 6004 |
struct ast_sockaddr remote_address = { {0,} }; |
| 6005 |
|
| 6006 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 6007 |
|
| 6008 |
if (((compensate && type == AST_FRAME_DTMF_END) || (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) { |
| 6009 |
ast_debug_rtp(1, "(%p) RTP ignore potential DTMF echo from '%s'\n", |
| 6010 |
instance, ast_sockaddr_stringify(&remote_address)); |
| 6011 |
rtp->resp = 0; |
| 6012 |
rtp->dtmfsamples = 0; |
| 6013 |
return &ast_null_frame; |
| 6014 |
} else if (type == AST_FRAME_DTMF_BEGIN && rtp->resp == 'X') { |
| 6015 |
ast_debug_rtp(1, "(%p) RTP ignore flash begin from '%s'\n", |
| 6016 |
instance, ast_sockaddr_stringify(&remote_address)); |
| 6017 |
rtp->resp = 0; |
| 6018 |
rtp->dtmfsamples = 0; |
| 6019 |
return &ast_null_frame; |
| 6020 |
} |
| 6021 |
|
| 6022 |
if (rtp->resp == 'X') { |
| 6023 |
ast_debug_rtp(1, "(%p) RTP creating flash Frame at %s\n", |
| 6024 |
instance, ast_sockaddr_stringify(&remote_address)); |
| 6025 |
rtp->f.frametype = AST_FRAME_CONTROL; |
| 6026 |
rtp->f.subclass.integer = AST_CONTROL_FLASH; |
| 6027 |
} else { |
| 6028 |
ast_debug_rtp(1, "(%p) RTP creating %s DTMF Frame: %d (%c), at %s\n", |
| 6029 |
instance, type == AST_FRAME_DTMF_END ? "END" : "BEGIN", |
| 6030 |
rtp->resp, rtp->resp, |
| 6031 |
ast_sockaddr_stringify(&remote_address)); |
| 6032 |
rtp->f.frametype = type; |
| 6033 |
rtp->f.subclass.integer = rtp->resp; |
| 6034 |
} |
| 6035 |
rtp->f.datalen = 0; |
| 6036 |
rtp->f.samples = 0; |
| 6037 |
rtp->f.mallocd = 0; |
| 6038 |
rtp->f.src = "RTP"; |
| 6039 |
AST_LIST_NEXT(&rtp->f, frame_list) = NULL; |
| 6040 |
|
| 6041 |
return &rtp->f; |
| 6042 |
} |
| 6043 |
|
| 6044 |
static void process_dtmf_rfc2833(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark, struct frame_list *frames) |
| 6045 |
{ |
| 6046 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 6047 |
struct ast_sockaddr remote_address = { {0,} }; |
| 6048 |
unsigned int event, event_end, samples; |
| 6049 |
char resp = 0; |
| 6050 |
struct ast_frame *f = NULL; |
| 6051 |
|
| 6052 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 6053 |
|
| 6054 |
|
| 6055 |
event = ntohl(*((unsigned int *)(data))); |
| 6056 |
event >>= 24; |
| 6057 |
event_end = ntohl(*((unsigned int *)(data))); |
| 6058 |
event_end <<= 8; |
| 6059 |
event_end >>= 24; |
| 6060 |
samples = ntohl(*((unsigned int *)(data))); |
| 6061 |
samples &= 0xFFFF; |
| 6062 |
|
| 6063 |
if (rtp_debug_test_addr(&remote_address)) { |
| 6064 |
ast_verbose("Got RTP RFC2833 from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d, mark %d, event %08x, end %d, duration %-5.5u) \n", |
| 6065 |
ast_sockaddr_stringify(&remote_address), |
| 6066 |
payloadtype, seqno, timestamp, len, (mark?1:0), event, ((event_end & 0x80)?1:0), samples); |
| 6067 |
} |
| 6068 |
|
| 6069 |
|
| 6070 |
if (ast_debug_rtp_packet_is_allowed) |
| 6071 |
ast_debug(0, "- RTP 2833 Event: %08x (len = %d)\n", event, len); |
| 6072 |
|
| 6073 |
|
| 6074 |
if (event < 10) { |
| 6075 |
resp = '0' + event; |
| 6076 |
} else if (event < 11) { |
| 6077 |
resp = '*'; |
| 6078 |
} else if (event < 12) { |
| 6079 |
resp = '#'; |
| 6080 |
} else if (event < 16) { |
| 6081 |
resp = 'A' + (event - 12); |
| 6082 |
} else if (event < 17) { |
| 6083 |
resp = 'X'; |
| 6084 |
} else { |
| 6085 |
|
| 6086 |
ast_debug_rtp(1, "(%p) RTP ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", instance, event); |
| 6087 |
return; |
| 6088 |
} |
| 6089 |
|
| 6090 |
if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) { |
| 6091 |
if (!rtp->last_end_timestamp.is_set || rtp->last_end_timestamp.ts != timestamp || (rtp->resp && rtp->resp != resp)) { |
| 6092 |
rtp->resp = resp; |
| 6093 |
rtp->dtmf_timeout = 0; |
| 6094 |
f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE))); |
| 6095 |
f->len = 0; |
| 6096 |
rtp->last_end_timestamp.ts = timestamp; |
| 6097 |
rtp->last_end_timestamp.is_set = 1; |
| 6098 |
AST_LIST_INSERT_TAIL(frames, f, frame_list); |
| 6099 |
} |
| 6100 |
} else { |
| 6101 |
|
| 6102 |
|
| 6103 |
|
| 6104 |
|
| 6105 |
|
| 6106 |
unsigned int new_duration = rtp->dtmf_duration; |
| 6107 |
unsigned int last_duration = new_duration & 0xFFFF; |
| 6108 |
|
| 6109 |
if (last_duration > 64000 && samples < last_duration) { |
| 6110 |
new_duration += 0xFFFF + 1; |
| 6111 |
} |
| 6112 |
new_duration = (new_duration & ~0xFFFF) | samples; |
| 6113 |
|
| 6114 |
if (event_end & 0x80) { |
| 6115 |
|
| 6116 |
if (rtp->last_seqno != seqno && (!rtp->last_end_timestamp.is_set || timestamp > rtp->last_end_timestamp.ts)) { |
| 6117 |
rtp->last_end_timestamp.ts = timestamp; |
| 6118 |
rtp->last_end_timestamp.is_set = 1; |
| 6119 |
rtp->dtmf_duration = new_duration; |
| 6120 |
rtp->resp = resp; |
| 6121 |
f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0)); |
| 6122 |
f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, ast_rtp_get_rate(f->subclass.format)), ast_tv(0, 0)); |
| 6123 |
rtp->resp = 0; |
| 6124 |
rtp->dtmf_duration = rtp->dtmf_timeout = 0; |
| 6125 |
AST_LIST_INSERT_TAIL(frames, f, frame_list); |
| 6126 |
} else if (ast_debug_rtp_packet_is_allowed) { |
| 6127 |
ast_debug_rtp(1, "(%p) RTP dropping duplicate or out of order DTMF END frame (seqno: %u, ts %u, digit %c)\n", |
| 6128 |
instance, seqno, timestamp, resp); |
| 6129 |
} |
| 6130 |
} else { |
| 6131 |
|
| 6132 |
|
| 6133 |
|
| 6134 |
|
| 6135 |
|
| 6136 |
|
| 6137 |
if ((rtp->last_seqno > seqno && rtp->last_seqno - seqno < 50) |
| 6138 |
|| (rtp->last_end_timestamp.is_set |
| 6139 |
&& timestamp <= rtp->last_end_timestamp.ts)) { |
| 6140 |
|
| 6141 |
|
| 6142 |
|
| 6143 |
|
| 6144 |
if (ast_debug_rtp_packet_is_allowed) { |
| 6145 |
ast_debug(0, "Dropping out of order DTMF frame (seqno %u, ts %u, digit %c)\n", |
| 6146 |
seqno, timestamp, resp); |
| 6147 |
} |
| 6148 |
return; |
| 6149 |
} |
| 6150 |
|
| 6151 |
if (rtp->resp && rtp->resp != resp) { |
| 6152 |
|
| 6153 |
f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0)); |
| 6154 |
f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, ast_rtp_get_rate(f->subclass.format)), ast_tv(0, 0)); |
| 6155 |
rtp->resp = 0; |
| 6156 |
rtp->dtmf_duration = rtp->dtmf_timeout = 0; |
| 6157 |
AST_LIST_INSERT_TAIL(frames, f, frame_list); |
| 6158 |
} |
| 6159 |
|
| 6160 |
if (rtp->resp) { |
| 6161 |
|
| 6162 |
rtp->dtmf_duration = new_duration; |
| 6163 |
} else { |
| 6164 |
|
| 6165 |
rtp->resp = resp; |
| 6166 |
f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0)); |
| 6167 |
rtp->dtmf_duration = samples; |
| 6168 |
AST_LIST_INSERT_TAIL(frames, f, frame_list); |
| 6169 |
} |
| 6170 |
|
| 6171 |
rtp->dtmf_timeout = timestamp + rtp->dtmf_duration + dtmftimeout; |
| 6172 |
} |
| 6173 |
|
| 6174 |
rtp->last_seqno = seqno; |
| 6175 |
} |
| 6176 |
|
| 6177 |
rtp->dtmfsamples = samples; |
| 6178 |
|
| 6179 |
return; |
| 6180 |
} |
| 6181 |
|
| 6182 |
static struct ast_frame *process_dtmf_cisco(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark) |
| 6183 |
{ |
| 6184 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 6185 |
unsigned int event, flags, power; |
| 6186 |
char resp = 0; |
| 6187 |
unsigned char seq; |
| 6188 |
struct ast_frame *f = NULL; |
| 6189 |
|
| 6190 |
if (len < 4) { |
| 6191 |
return NULL; |
| 6192 |
} |
| 6193 |
|
| 6194 |
|
| 6195 |
|
| 6196 |
|
| 6197 |
|
| 6198 |
|
| 6199 |
|
| 6200 |
|
| 6201 |
|
| 6202 |
|
| 6203 |
|
| 6204 |
|
| 6205 |
|
| 6206 |
|
| 6207 |
|
| 6208 |
|
| 6209 |
|
| 6210 |
|
| 6211 |
|
| 6212 |
|
| 6213 |
|
| 6214 |
|
| 6215 |
|
| 6216 |
|
| 6217 |
|
| 6218 |
|
| 6219 |
|
| 6220 |
|
| 6221 |
|
| 6222 |
|
| 6223 |
|
| 6224 |
seq = data[0]; |
| 6225 |
flags = data[1]; |
| 6226 |
power = data[2]; |
| 6227 |
event = data[3] & 0x1f; |
| 6228 |
|
| 6229 |
if (ast_debug_rtp_packet_is_allowed) |
| 6230 |
ast_debug(0, "Cisco DTMF Digit: %02x (len=%d, seq=%d, flags=%02x, power=%u, history count=%d)\n", event, len, seq, flags, power, (len - 4) / 2); |
| 6231 |
if (event < 10) { |
| 6232 |
resp = '0' + event; |
| 6233 |
} else if (event < 11) { |
| 6234 |
resp = '*'; |
| 6235 |
} else if (event < 12) { |
| 6236 |
resp = '#'; |
| 6237 |
} else if (event < 16) { |
| 6238 |
resp = 'A' + (event - 12); |
| 6239 |
} else if (event < 17) { |
| 6240 |
resp = 'X'; |
| 6241 |
} |
| 6242 |
if ((!rtp->resp && power) || (rtp->resp && (rtp->resp != resp))) { |
| 6243 |
rtp->resp = resp; |
| 6244 |
|
| 6245 |
if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) { |
| 6246 |
f = create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0); |
| 6247 |
rtp->dtmfsamples = 0; |
| 6248 |
} |
| 6249 |
} else if ((rtp->resp == resp) && !power) { |
| 6250 |
f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)); |
| 6251 |
f->samples = rtp->dtmfsamples * (ast_rtp_get_rate(rtp->lastrxformat) / 1000); |
| 6252 |
rtp->resp = 0; |
| 6253 |
} else if (rtp->resp == resp) { |
| 6254 |
rtp->dtmfsamples += 20 * (ast_rtp_get_rate(rtp->lastrxformat) / 1000); |
| 6255 |
} |
| 6256 |
|
| 6257 |
rtp->dtmf_timeout = 0; |
| 6258 |
|
| 6259 |
return f; |
| 6260 |
} |
| 6261 |
|
| 6262 |
static struct ast_frame *process_cn_rfc3389(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark) |
| 6263 |
{ |
| 6264 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 6265 |
|
| 6266 |
|
| 6267 |
|
| 6268 |
|
| 6269 |
if (ast_debug_rtp_packet_is_allowed) { |
| 6270 |
ast_debug(0, "- RTP 3389 Comfort noise event: Format %s (len = %d)\n", |
| 6271 |
ast_format_get_name(rtp->lastrxformat), len); |
| 6272 |
} |
| 6273 |
|
| 6274 |
if (!ast_test_flag(rtp, FLAG_3389_WARNING)) { |
| 6275 |
struct ast_sockaddr remote_address = { {0,} }; |
| 6276 |
|
| 6277 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 6278 |
|
| 6279 |
ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client address: %s\n", |
| 6280 |
ast_sockaddr_stringify(&remote_address)); |
| 6281 |
ast_set_flag(rtp, FLAG_3389_WARNING); |
| 6282 |
} |
| 6283 |
|
| 6284 |
|
| 6285 |
if (!len) { |
| 6286 |
return NULL; |
| 6287 |
} |
| 6288 |
if (len < 24) { |
| 6289 |
rtp->f.data.ptr = rtp->rawdata + AST_FRIENDLY_OFFSET; |
| 6290 |
rtp->f.datalen = len - 1; |
| 6291 |
rtp->f.offset = AST_FRIENDLY_OFFSET; |
| 6292 |
memcpy(rtp->f.data.ptr, data + 1, len - 1); |
| 6293 |
} else { |
| 6294 |
rtp->f.data.ptr = NULL; |
| 6295 |
rtp->f.offset = 0; |
| 6296 |
rtp->f.datalen = 0; |
| 6297 |
} |
| 6298 |
rtp->f.frametype = AST_FRAME_CNG; |
| 6299 |
rtp->f.subclass.integer = data[0] & 0x7f; |
| 6300 |
rtp->f.samples = 0; |
| 6301 |
rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0; |
| 6302 |
|
| 6303 |
return &rtp->f; |
| 6304 |
} |
| 6305 |
|
| 6306 |
static int update_rtt_stats(struct ast_rtp *rtp, unsigned int lsr, unsigned int dlsr) |
| 6307 |
{ |
| 6308 |
struct timeval now; |
| 6309 |
struct timeval rtt_tv; |
| 6310 |
unsigned int msw; |
| 6311 |
unsigned int lsw; |
| 6312 |
unsigned int rtt_msw; |
| 6313 |
unsigned int rtt_lsw; |
| 6314 |
unsigned int lsr_a; |
| 6315 |
unsigned int rtt; |
| 6316 |
|
| 6317 |
gettimeofday(&now, NULL); |
| 6318 |
timeval2ntp(now, &msw, &lsw); |
| 6319 |
|
| 6320 |
lsr_a = ((msw & 0x0000ffff) << 16) | ((lsw & 0xffff0000) >> 16); |
| 6321 |
rtt = lsr_a - lsr - dlsr; |
| 6322 |
rtt_msw = (rtt & 0xffff0000) >> 16; |
| 6323 |
rtt_lsw = (rtt & 0x0000ffff); |
| 6324 |
rtt_tv.tv_sec = rtt_msw; |
| 6325 |
|
| 6326 |
|
| 6327 |
|
| 6328 |
|
| 6329 |
|
| 6330 |
|
| 6331 |
|
| 6332 |
|
| 6333 |
|
| 6334 |
|
| 6335 |
|
| 6336 |
|
| 6337 |
|
| 6338 |
rtt_tv.tv_usec = (rtt_lsw * 15625) >> 10; |
| 6339 |
rtp->rtcp->rtt = (double)rtt_tv.tv_sec + ((double)rtt_tv.tv_usec / 1000000); |
| 6340 |
if (lsr_a - dlsr < lsr) { |
| 6341 |
return 1; |
| 6342 |
} |
| 6343 |
|
| 6344 |
rtp->rtcp->accumulated_transit += rtp->rtcp->rtt; |
| 6345 |
if (rtp->rtcp->rtt_count == 0 || rtp->rtcp->minrtt > rtp->rtcp->rtt) { |
| 6346 |
rtp->rtcp->minrtt = rtp->rtcp->rtt; |
| 6347 |
} |
| 6348 |
if (rtp->rtcp->maxrtt < rtp->rtcp->rtt) { |
| 6349 |
rtp->rtcp->maxrtt = rtp->rtcp->rtt; |
| 6350 |
} |
| 6351 |
|
| 6352 |
calc_mean_and_standard_deviation(rtp->rtcp->rtt, &rtp->rtcp->normdevrtt, |
| 6353 |
&rtp->rtcp->stdevrtt, &rtp->rtcp->rtt_count); |
| 6354 |
|
| 6355 |
return 0; |
| 6356 |
} |
| 6357 |
|
| 6358 |
|
| 6359 |
|
| 6360 |
|
| 6361 |
|
| 6362 |
static void update_jitter_stats(struct ast_rtp *rtp, unsigned int ia_jitter) |
| 6363 |
{ |
| 6364 |
int rate = ast_rtp_get_rate(rtp->f.subclass.format); |
| 6365 |
|
| 6366 |
rtp->rtcp->reported_jitter = ast_samp2sec(ia_jitter, rate); |
| 6367 |
|
| 6368 |
if (rtp->rtcp->reported_jitter_count == 0) { |
| 6369 |
rtp->rtcp->reported_minjitter = rtp->rtcp->reported_jitter; |
| 6370 |
} |
| 6371 |
if (rtp->rtcp->reported_jitter < rtp->rtcp->reported_minjitter) { |
| 6372 |
rtp->rtcp->reported_minjitter = rtp->rtcp->reported_jitter; |
| 6373 |
} |
| 6374 |
if (rtp->rtcp->reported_jitter > rtp->rtcp->reported_maxjitter) { |
| 6375 |
rtp->rtcp->reported_maxjitter = rtp->rtcp->reported_jitter; |
| 6376 |
} |
| 6377 |
|
| 6378 |
calc_mean_and_standard_deviation(rtp->rtcp->reported_jitter, |
| 6379 |
&rtp->rtcp->reported_normdev_jitter, &rtp->rtcp->reported_stdev_jitter, |
| 6380 |
&rtp->rtcp->reported_jitter_count); |
| 6381 |
} |
| 6382 |
|
| 6383 |
|
| 6384 |
|
| 6385 |
|
| 6386 |
|
| 6387 |
static void update_lost_stats(struct ast_rtp *rtp, unsigned int lost_packets) |
| 6388 |
{ |
| 6389 |
double reported_lost; |
| 6390 |
|
| 6391 |
rtp->rtcp->reported_lost = lost_packets; |
| 6392 |
reported_lost = (double)rtp->rtcp->reported_lost; |
| 6393 |
if (rtp->rtcp->reported_lost_count == 0) { |
| 6394 |
rtp->rtcp->reported_minlost = reported_lost; |
| 6395 |
} |
| 6396 |
if (reported_lost < rtp->rtcp->reported_minlost) { |
| 6397 |
rtp->rtcp->reported_minlost = reported_lost; |
| 6398 |
} |
| 6399 |
if (reported_lost > rtp->rtcp->reported_maxlost) { |
| 6400 |
rtp->rtcp->reported_maxlost = reported_lost; |
| 6401 |
} |
| 6402 |
|
| 6403 |
calc_mean_and_standard_deviation(reported_lost, &rtp->rtcp->reported_normdev_lost, |
| 6404 |
&rtp->rtcp->reported_stdev_lost, &rtp->rtcp->reported_lost_count); |
| 6405 |
} |
| 6406 |
|
| 6407 |
#define RESCALE(in, inmin, inmax, outmin, outmax) ((((in - inmin)/(inmax-inmin))*(outmax-outmin))+outmin) |
| 6408 |
|
| 6409 |
|
| 6410 |
|
| 6411 |
|
| 6412 |
|
| 6413 |
|
| 6414 |
|
| 6415 |
|
| 6416 |
|
| 6417 |
|
| 6418 |
|
| 6419 |
|
| 6420 |
|
| 6421 |
|
| 6422 |
|
| 6423 |
|
| 6424 |
|
| 6425 |
|
| 6426 |
|
| 6427 |
|
| 6428 |
|
| 6429 |
|
| 6430 |
|
| 6431 |
static double calc_media_experience_score(struct ast_rtp_instance *instance, |
| 6432 |
double normdevrtt, double normdev_rxjitter, double stdev_rxjitter, |
| 6433 |
double normdev_rxlost) |
| 6434 |
{ |
| 6435 |
double r_value; |
| 6436 |
double pseudo_mos; |
| 6437 |
double mes = 0; |
| 6438 |
|
| 6439 |
|
| 6440 |
|
| 6441 |
|
| 6442 |
|
| 6443 |
|
| 6444 |
|
| 6445 |
|
| 6446 |
|
| 6447 |
|
| 6448 |
double effective_latency = (normdevrtt * 1000) |
| 6449 |
+ ((normdev_rxjitter * 2) * (stdev_rxjitter / 3)) |
| 6450 |
+ 10; |
| 6451 |
|
| 6452 |
|
| 6453 |
|
| 6454 |
|
| 6455 |
|
| 6456 |
|
| 6457 |
|
| 6458 |
|
| 6459 |
|
| 6460 |
if (effective_latency < 160) { |
| 6461 |
r_value = 93.2 - (effective_latency / 40); |
| 6462 |
} else { |
| 6463 |
r_value = 93.2 - (effective_latency - 120) / 10; |
| 6464 |
} |
| 6465 |
|
| 6466 |
|
| 6467 |
r_value = r_value - (normdev_rxlost * 2.0); |
| 6468 |
|
| 6469 |
|
| 6470 |
|
| 6471 |
|
| 6472 |
|
| 6473 |
if (r_value < 0) { |
| 6474 |
pseudo_mos = 1.0; |
| 6475 |
} else if (r_value > 100) { |
| 6476 |
pseudo_mos = 4.5; |
| 6477 |
} else { |
| 6478 |
pseudo_mos = 1 + (0.035 * r_value) + (r_value * (r_value - 60) * (100 - r_value) * 0.000007); |
| 6479 |
} |
| 6480 |
|
| 6481 |
|
| 6482 |
|
| 6483 |
|
| 6484 |
|
| 6485 |
|
| 6486 |
mes = RESCALE(pseudo_mos, 0.0, 5.0, 0.0, 100.0); |
| 6487 |
|
| 6488 |
return mes; |
| 6489 |
} |
| 6490 |
|
| 6491 |
|
| 6492 |
|
| 6493 |
|
| 6494 |
|
| 6495 |
|
| 6496 |
static void update_reported_mes_stats(struct ast_rtp *rtp) |
| 6497 |
{ |
| 6498 |
double mes = calc_media_experience_score(rtp->owner, |
| 6499 |
rtp->rtcp->normdevrtt, |
| 6500 |
rtp->rtcp->reported_jitter, |
| 6501 |
rtp->rtcp->reported_stdev_jitter, |
| 6502 |
rtp->rtcp->reported_normdev_lost); |
| 6503 |
|
| 6504 |
rtp->rtcp->reported_mes = mes; |
| 6505 |
if (rtp->rtcp->reported_mes_count == 0) { |
| 6506 |
rtp->rtcp->reported_minmes = mes; |
| 6507 |
} |
| 6508 |
if (mes < rtp->rtcp->reported_minmes) { |
| 6509 |
rtp->rtcp->reported_minmes = mes; |
| 6510 |
} |
| 6511 |
if (mes > rtp->rtcp->reported_maxmes) { |
| 6512 |
rtp->rtcp->reported_maxmes = mes; |
| 6513 |
} |
| 6514 |
|
| 6515 |
calc_mean_and_standard_deviation(mes, &rtp->rtcp->reported_normdev_mes, |
| 6516 |
&rtp->rtcp->reported_stdev_mes, &rtp->rtcp->reported_mes_count); |
| 6517 |
|
| 6518 |
ast_debug_rtcp(2, "%s: rtt: %.9f j: %.9f sjh: %.9f lost: %.9f mes: %4.1f\n", |
| 6519 |
ast_rtp_instance_get_channel_id(rtp->owner), |
| 6520 |
rtp->rtcp->normdevrtt, |
| 6521 |
rtp->rtcp->reported_jitter, |
| 6522 |
rtp->rtcp->reported_stdev_jitter, |
| 6523 |
rtp->rtcp->reported_normdev_lost, mes); |
| 6524 |
} |
| 6525 |
|
| 6526 |
|
| 6527 |
|
| 6528 |
|
| 6529 |
|
| 6530 |
|
| 6531 |
static void update_local_mes_stats(struct ast_rtp *rtp) |
| 6532 |
{ |
| 6533 |
rtp->rxmes = calc_media_experience_score(rtp->owner, |
| 6534 |
rtp->rtcp->normdevrtt, |
| 6535 |
rtp->rxjitter, |
| 6536 |
rtp->rtcp->stdev_rxjitter, |
| 6537 |
rtp->rtcp->normdev_rxlost); |
| 6538 |
|
| 6539 |
if (rtp->rtcp->rxmes_count == 0) { |
| 6540 |
rtp->rtcp->minrxmes = rtp->rxmes; |
| 6541 |
} |
| 6542 |
if (rtp->rxmes < rtp->rtcp->minrxmes) { |
| 6543 |
rtp->rtcp->minrxmes = rtp->rxmes; |
| 6544 |
} |
| 6545 |
if (rtp->rxmes > rtp->rtcp->maxrxmes) { |
| 6546 |
rtp->rtcp->maxrxmes = rtp->rxmes; |
| 6547 |
} |
| 6548 |
|
| 6549 |
calc_mean_and_standard_deviation(rtp->rxmes, &rtp->rtcp->normdev_rxmes, |
| 6550 |
&rtp->rtcp->stdev_rxmes, &rtp->rtcp->rxmes_count); |
| 6551 |
|
| 6552 |
ast_debug_rtcp(2, " %s: rtt: %.9f j: %.9f sjh: %.9f lost: %.9f mes: %4.1f\n", |
| 6553 |
ast_rtp_instance_get_channel_id(rtp->owner), |
| 6554 |
rtp->rtcp->normdevrtt, |
| 6555 |
rtp->rxjitter, |
| 6556 |
rtp->rtcp->stdev_rxjitter, |
| 6557 |
rtp->rtcp->normdev_rxlost, rtp->rxmes); |
| 6558 |
} |
| 6559 |
|
| 6560 |
|
| 6561 |
static struct ast_rtp_instance *__rtp_find_instance_by_ssrc(struct ast_rtp_instance *instance, |
| 6562 |
struct ast_rtp *rtp, unsigned int ssrc, int source) |
| 6563 |
{ |
| 6564 |
int index; |
| 6565 |
|
| 6566 |
if (!AST_VECTOR_SIZE(&rtp->ssrc_mapping)) { |
| 6567 |
|
| 6568 |
return instance; |
| 6569 |
} |
| 6570 |
|
| 6571 |
|
| 6572 |
for (index = 0; index < AST_VECTOR_SIZE(&rtp->ssrc_mapping); ++index) { |
| 6573 |
struct rtp_ssrc_mapping *mapping = AST_VECTOR_GET_ADDR(&rtp->ssrc_mapping, index); |
| 6574 |
unsigned int mapping_ssrc = source ? ast_rtp_get_ssrc(mapping->instance) : mapping->ssrc; |
| 6575 |
|
| 6576 |
if (mapping->ssrc_valid && mapping_ssrc == ssrc) { |
| 6577 |
return mapping->instance; |
| 6578 |
} |
| 6579 |
} |
| 6580 |
|
| 6581 |
|
| 6582 |
if (rtp->themssrc_valid && rtp->themssrc == ssrc) { |
| 6583 |
return instance; |
| 6584 |
} |
| 6585 |
return NULL; |
| 6586 |
} |
| 6587 |
|
| 6588 |
|
| 6589 |
static struct ast_rtp_instance *rtp_find_instance_by_packet_source_ssrc(struct ast_rtp_instance *instance, |
| 6590 |
struct ast_rtp *rtp, unsigned int ssrc) |
| 6591 |
{ |
| 6592 |
return __rtp_find_instance_by_ssrc(instance, rtp, ssrc, 0); |
| 6593 |
} |
| 6594 |
|
| 6595 |
|
| 6596 |
static struct ast_rtp_instance *rtp_find_instance_by_media_source_ssrc(struct ast_rtp_instance *instance, |
| 6597 |
struct ast_rtp *rtp, unsigned int ssrc) |
| 6598 |
{ |
| 6599 |
return __rtp_find_instance_by_ssrc(instance, rtp, ssrc, 1); |
| 6600 |
} |
| 6601 |
|
| 6602 |
static const char *rtcp_payload_type2str(unsigned int pt) |
| 6603 |
{ |
| 6604 |
const char *str; |
| 6605 |
|
| 6606 |
switch (pt) { |
| 6607 |
case RTCP_PT_SR: |
| 6608 |
str = "Sender Report"; |
| 6609 |
break; |
| 6610 |
case RTCP_PT_RR: |
| 6611 |
str = "Receiver Report"; |
| 6612 |
break; |
| 6613 |
case RTCP_PT_FUR: |
| 6614 |
|
| 6615 |
str = "H.261 FUR"; |
| 6616 |
break; |
| 6617 |
case RTCP_PT_PSFB: |
| 6618 |
|
| 6619 |
str = "PSFB"; |
| 6620 |
break; |
| 6621 |
case RTCP_PT_SDES: |
| 6622 |
str = "Source Description"; |
| 6623 |
break; |
| 6624 |
case RTCP_PT_BYE: |
| 6625 |
str = "BYE"; |
| 6626 |
break; |
| 6627 |
default: |
| 6628 |
str = "Unknown"; |
| 6629 |
break; |
| 6630 |
} |
| 6631 |
return str; |
| 6632 |
} |
| 6633 |
|
| 6634 |
static const char *rtcp_payload_subtype2str(unsigned int pt, unsigned int subtype) |
| 6635 |
{ |
| 6636 |
switch (pt) { |
| 6637 |
case AST_RTP_RTCP_RTPFB: |
| 6638 |
if (subtype == AST_RTP_RTCP_FMT_NACK) { |
| 6639 |
return "NACK"; |
| 6640 |
} |
| 6641 |
break; |
| 6642 |
case RTCP_PT_PSFB: |
| 6643 |
if (subtype == AST_RTP_RTCP_FMT_REMB) { |
| 6644 |
return "REMB"; |
| 6645 |
} |
| 6646 |
break; |
| 6647 |
default: |
| 6648 |
break; |
| 6649 |
} |
| 6650 |
|
| 6651 |
return NULL; |
| 6652 |
} |
| 6653 |
|
| 6654 |
|
| 6655 |
static int ast_rtp_rtcp_handle_nack(struct ast_rtp_instance *instance, unsigned int *nackdata, unsigned int position, |
| 6656 |
unsigned int length) |
| 6657 |
{ |
| 6658 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 6659 |
int res = 0; |
| 6660 |
int blp_index; |
| 6661 |
int packet_index; |
| 6662 |
int ice; |
| 6663 |
struct ast_rtp_rtcp_nack_payload *payload; |
| 6664 |
unsigned int current_word; |
| 6665 |
unsigned int pid; |
| 6666 |
unsigned int blp; |
| 6667 |
struct ast_sockaddr remote_address = { {0,} }; |
| 6668 |
int abs_send_time_id; |
| 6669 |
unsigned int now_msw = 0; |
| 6670 |
unsigned int now_lsw = 0; |
| 6671 |
unsigned int packets_not_found = 0; |
| 6672 |
|
| 6673 |
if (!rtp->send_buffer) { |
| 6674 |
ast_debug_rtcp(1, "(%p) RTCP tried to handle NACK request, " |
| 6675 |
"but we don't have a RTP packet storage!\n", instance); |
| 6676 |
return res; |
| 6677 |
} |
| 6678 |
|
| 6679 |
abs_send_time_id = ast_rtp_instance_extmap_get_id(instance, AST_RTP_EXTENSION_ABS_SEND_TIME); |
| 6680 |
if (abs_send_time_id != -1) { |
| 6681 |
timeval2ntp(ast_tvnow(), &now_msw, &now_lsw); |
| 6682 |
} |
| 6683 |
|
| 6684 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 6685 |
|
| 6686 |
|
| 6687 |
|
| 6688 |
|
| 6689 |
|
| 6690 |
for (packet_index = 3; packet_index < length; packet_index++) { |
| 6691 |
current_word = ntohl(nackdata[position + packet_index]); |
| 6692 |
pid = current_word >> 16; |
| 6693 |
|
| 6694 |
payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_get(rtp->send_buffer, pid); |
| 6695 |
if (payload) { |
| 6696 |
if (abs_send_time_id != -1) { |
| 6697 |
|
| 6698 |
|
| 6699 |
|
| 6700 |
put_unaligned_time24(payload->buf + 17, now_msw, now_lsw); |
| 6701 |
} |
| 6702 |
res += rtp_sendto(instance, payload->buf, payload->size, 0, &remote_address, &ice); |
| 6703 |
} else { |
| 6704 |
ast_debug_rtcp(1, "(%p) RTCP received NACK request for RTP packet with seqno %d, " |
| 6705 |
"but we don't have it\n", instance, pid); |
| 6706 |
packets_not_found++; |
| 6707 |
} |
| 6708 |
|
| 6709 |
|
| 6710 |
|
| 6711 |
|
| 6712 |
|
| 6713 |
|
| 6714 |
blp = current_word & 0xffff; |
| 6715 |
blp_index = 1; |
| 6716 |
while (blp) { |
| 6717 |
if (blp & 1) { |
| 6718 |
|
| 6719 |
unsigned int seqno = (pid + blp_index) % 65536; |
| 6720 |
payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_get(rtp->send_buffer, seqno); |
| 6721 |
if (payload) { |
| 6722 |
if (abs_send_time_id != -1) { |
| 6723 |
put_unaligned_time24(payload->buf + 17, now_msw, now_lsw); |
| 6724 |
} |
| 6725 |
res += rtp_sendto(instance, payload->buf, payload->size, 0, &remote_address, &ice); |
| 6726 |
} else { |
| 6727 |
ast_debug_rtcp(1, "(%p) RTCP remote end also requested RTP packet with seqno %d, " |
| 6728 |
"but we don't have it\n", instance, seqno); |
| 6729 |
packets_not_found++; |
| 6730 |
} |
| 6731 |
} |
| 6732 |
blp >>= 1; |
| 6733 |
blp_index++; |
| 6734 |
} |
| 6735 |
} |
| 6736 |
|
| 6737 |
if (packets_not_found) { |
| 6738 |
|
| 6739 |
|
| 6740 |
|
| 6741 |
ast_data_buffer_resize(rtp->send_buffer, MIN(MAXIMUM_RTP_SEND_BUFFER_SIZE, |
| 6742 |
ast_data_buffer_max(rtp->send_buffer) + packets_not_found)); |
| 6743 |
ast_debug_rtcp(2, "(%p) RTCP send buffer on RTP instance is now at maximum of %zu\n", |
| 6744 |
instance, ast_data_buffer_max(rtp->send_buffer)); |
| 6745 |
} |
| 6746 |
|
| 6747 |
return res; |
| 6748 |
} |
| 6749 |
|
| 6750 |
|
| 6751 |
|
| 6752 |
|
| 6753 |
#define RTCP_LENGTH_MASK 0xFFFF |
| 6754 |
#define RTCP_PAYLOAD_TYPE_MASK 0xFF |
| 6755 |
#define RTCP_REPORT_COUNT_MASK 0x1F |
| 6756 |
#define RTCP_PADDING_MASK 0x01 |
| 6757 |
#define RTCP_VERSION_MASK 0x03 |
| 6758 |
|
| 6759 |
|
| 6760 |
|
| 6761 |
|
| 6762 |
#define RTCP_LENGTH_SHIFT 0 |
| 6763 |
#define RTCP_PAYLOAD_TYPE_SHIFT 16 |
| 6764 |
#define RTCP_REPORT_COUNT_SHIFT 24 |
| 6765 |
#define RTCP_PADDING_SHIFT 29 |
| 6766 |
#define RTCP_VERSION_SHIFT 30 |
| 6767 |
|
| 6768 |
#define RTCP_VERSION 2U |
| 6769 |
#define RTCP_VERSION_SHIFTED (RTCP_VERSION << RTCP_VERSION_SHIFT) |
| 6770 |
#define RTCP_VERSION_MASK_SHIFTED (RTCP_VERSION_MASK << RTCP_VERSION_SHIFT) |
| 6771 |
|
| 6772 |
|
| 6773 |
|
| 6774 |
|
| 6775 |
|
| 6776 |
|
| 6777 |
|
| 6778 |
|
| 6779 |
|
| 6780 |
|
| 6781 |
|
| 6782 |
|
| 6783 |
#define RTCP_VALID_MASK (RTCP_VERSION_MASK_SHIFTED | (((RTCP_PAYLOAD_TYPE_MASK & ~0x1)) << RTCP_PAYLOAD_TYPE_SHIFT)) |
| 6784 |
#define RTCP_VALID_VALUE (RTCP_VERSION_SHIFTED | (RTCP_PT_SR << RTCP_PAYLOAD_TYPE_SHIFT)) |
| 6785 |
|
| 6786 |
#define RTCP_SR_BLOCK_WORD_LENGTH 5 |
| 6787 |
#define RTCP_RR_BLOCK_WORD_LENGTH 6 |
| 6788 |
#define RTCP_HEADER_SSRC_LENGTH 2 |
| 6789 |
#define RTCP_FB_REMB_BLOCK_WORD_LENGTH 4 |
| 6790 |
#define RTCP_FB_NACK_BLOCK_WORD_LENGTH 2 |
| 6791 |
|
| 6792 |
static struct ast_frame *ast_rtcp_interpret(struct ast_rtp_instance *instance, struct ast_srtp *srtp, |
| 6793 |
const unsigned char *rtcpdata, size_t size, struct ast_sockaddr *addr) |
| 6794 |
{ |
| 6795 |
struct ast_rtp_instance *transport = instance; |
| 6796 |
struct ast_rtp *transport_rtp = ast_rtp_instance_get_data(instance); |
| 6797 |
int len = size; |
| 6798 |
unsigned int *rtcpheader = (unsigned int *)(rtcpdata); |
| 6799 |
unsigned int packetwords; |
| 6800 |
unsigned int position; |
| 6801 |
unsigned int first_word; |
| 6802 |
|
| 6803 |
unsigned int ssrc_seen; |
| 6804 |
struct ast_rtp_rtcp_report_block *report_block; |
| 6805 |
struct ast_frame *f = &ast_null_frame; |
| 6806 |
#ifdef TEST_FRAMEWORK |
| 6807 |
struct ast_rtp_engine_test *test_engine; |
| 6808 |
#endif |
| 6809 |
|
| 6810 |
|
| 6811 |
if ((*rtcpheader & 0xC0) && res_srtp && srtp && res_srtp->unprotect( |
| 6812 |
srtp, rtcpheader, &len, 1 | (srtp_replay_protection << 1)) < 0) { |
| 6813 |
return &ast_null_frame; |
| 6814 |
} |
| 6815 |
|
| 6816 |
packetwords = len / 4; |
| 6817 |
|
| 6818 |
ast_debug_rtcp(2, "(%s) RTCP got report of %d bytes from %s\n", |
| 6819 |
ast_rtp_instance_get_channel_id(instance), |
| 6820 |
len, ast_sockaddr_stringify(addr)); |
| 6821 |
|
| 6822 |
|
| 6823 |
|
| 6824 |
|
| 6825 |
|
| 6826 |
if (packetwords < RTCP_HEADER_SSRC_LENGTH) { |
| 6827 |
ast_debug_rtcp(2, "(%s) RTCP %p -- from %s: Frame size (%u words) is too short\n", |
| 6828 |
ast_rtp_instance_get_channel_id(instance), |
| 6829 |
transport_rtp, ast_sockaddr_stringify(addr), packetwords); |
| 6830 |
return &ast_null_frame; |
| 6831 |
} |
| 6832 |
position = 0; |
| 6833 |
first_word = ntohl(rtcpheader[position]); |
| 6834 |
if ((first_word & RTCP_VALID_MASK) != RTCP_VALID_VALUE) { |
| 6835 |
ast_debug_rtcp(2, "(%s) RTCP %p -- from %s: Failed first packet validity check\n", |
| 6836 |
ast_rtp_instance_get_channel_id(instance), |
| 6837 |
transport_rtp, ast_sockaddr_stringify(addr)); |
| 6838 |
return &ast_null_frame; |
| 6839 |
} |
| 6840 |
do { |
| 6841 |
position += ((first_word >> RTCP_LENGTH_SHIFT) & RTCP_LENGTH_MASK) + 1; |
| 6842 |
if (packetwords <= position) { |
| 6843 |
break; |
| 6844 |
} |
| 6845 |
first_word = ntohl(rtcpheader[position]); |
| 6846 |
} while ((first_word & RTCP_VERSION_MASK_SHIFTED) == RTCP_VERSION_SHIFTED); |
| 6847 |
if (position != packetwords) { |
| 6848 |
ast_debug_rtcp(2, "(%s) RTCP %p -- from %s: Failed packet version or length check\n", |
| 6849 |
ast_rtp_instance_get_channel_id(instance), |
| 6850 |
transport_rtp, ast_sockaddr_stringify(addr)); |
| 6851 |
return &ast_null_frame; |
| 6852 |
} |
| 6853 |
|
| 6854 |
|
| 6855 |
|
| 6856 |
|
| 6857 |
|
| 6858 |
|
| 6859 |
|
| 6860 |
|
| 6861 |
|
| 6862 |
|
| 6863 |
|
| 6864 |
|
| 6865 |
|
| 6866 |
|
| 6867 |
|
| 6868 |
|
| 6869 |
|
| 6870 |
ssrc_seen = transport_rtp->strict_rtp_state == STRICT_RTP_OPEN; |
| 6871 |
|
| 6872 |
position = 0; |
| 6873 |
while (position < packetwords) { |
| 6874 |
unsigned int i; |
| 6875 |
unsigned int pt; |
| 6876 |
unsigned int rc; |
| 6877 |
unsigned int ssrc; |
| 6878 |
|
| 6879 |
unsigned int ssrc_valid; |
| 6880 |
unsigned int length; |
| 6881 |
unsigned int min_length; |
| 6882 |
|
| 6883 |
unsigned int use_packet_source = 1; |
| 6884 |
|
| 6885 |
struct ast_json *message_blob; |
| 6886 |
RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report, NULL, ao2_cleanup); |
| 6887 |
struct ast_rtp_instance *child; |
| 6888 |
struct ast_rtp *rtp; |
| 6889 |
struct ast_rtp_rtcp_feedback *feedback; |
| 6890 |
|
| 6891 |
i = position; |
| 6892 |
first_word = ntohl(rtcpheader[i]); |
| 6893 |
pt = (first_word >> RTCP_PAYLOAD_TYPE_SHIFT) & RTCP_PAYLOAD_TYPE_MASK; |
| 6894 |
rc = (first_word >> RTCP_REPORT_COUNT_SHIFT) & RTCP_REPORT_COUNT_MASK; |
| 6895 |
|
| 6896 |
length = ((first_word >> RTCP_LENGTH_SHIFT) & RTCP_LENGTH_MASK) + 1; |
| 6897 |
|
| 6898 |
|
| 6899 |
min_length = RTCP_HEADER_SSRC_LENGTH; |
| 6900 |
switch (pt) { |
| 6901 |
case RTCP_PT_SR: |
| 6902 |
min_length += RTCP_SR_BLOCK_WORD_LENGTH; |
| 6903 |
|
| 6904 |
case RTCP_PT_RR: |
| 6905 |
min_length += (rc * RTCP_RR_BLOCK_WORD_LENGTH); |
| 6906 |
use_packet_source = 0; |
| 6907 |
break; |
| 6908 |
case RTCP_PT_FUR: |
| 6909 |
break; |
| 6910 |
case AST_RTP_RTCP_RTPFB: |
| 6911 |
switch (rc) { |
| 6912 |
case AST_RTP_RTCP_FMT_NACK: |
| 6913 |
min_length += RTCP_FB_NACK_BLOCK_WORD_LENGTH; |
| 6914 |
break; |
| 6915 |
default: |
| 6916 |
break; |
| 6917 |
} |
| 6918 |
use_packet_source = 0; |
| 6919 |
break; |
| 6920 |
case RTCP_PT_PSFB: |
| 6921 |
switch (rc) { |
| 6922 |
case AST_RTP_RTCP_FMT_REMB: |
| 6923 |
min_length += RTCP_FB_REMB_BLOCK_WORD_LENGTH; |
| 6924 |
break; |
| 6925 |
default: |
| 6926 |
break; |
| 6927 |
} |
| 6928 |
break; |
| 6929 |
case RTCP_PT_SDES: |
| 6930 |
case RTCP_PT_BYE: |
| 6931 |
|
| 6932 |
|
| 6933 |
|
| 6934 |
|
| 6935 |
|
| 6936 |
|
| 6937 |
min_length = length; |
| 6938 |
break; |
| 6939 |
default: |
| 6940 |
ast_debug_rtcp(1, "(%p) RTCP %p -- from %s: %u(%s) skipping record\n", |
| 6941 |
instance, transport_rtp, ast_sockaddr_stringify(addr), pt, rtcp_payload_type2str(pt)); |
| 6942 |
if (rtcp_debug_test_addr(addr)) { |
| 6943 |
ast_verbose("\n"); |
| 6944 |
ast_verbose("RTCP from %s: %u(%s) skipping record\n", |
| 6945 |
ast_sockaddr_stringify(addr), pt, rtcp_payload_type2str(pt)); |
| 6946 |
} |
| 6947 |
position += length; |
| 6948 |
continue; |
| 6949 |
} |
| 6950 |
if (length < min_length) { |
| 6951 |
ast_debug_rtcp(1, "(%p) RTCP %p -- from %s: %u(%s) length field less than expected minimum. Min:%u Got:%u\n", |
| 6952 |
instance, transport_rtp, ast_sockaddr_stringify(addr), pt, rtcp_payload_type2str(pt), |
| 6953 |
min_length - 1, length - 1); |
| 6954 |
return &ast_null_frame; |
| 6955 |
} |
| 6956 |
|
| 6957 |
|
| 6958 |
ssrc_valid = 1; |
| 6959 |
switch (pt) { |
| 6960 |
case RTCP_PT_SR: |
| 6961 |
case RTCP_PT_RR: |
| 6962 |
rtcp_report = ast_rtp_rtcp_report_alloc(rc); |
| 6963 |
if (!rtcp_report) { |
| 6964 |
return &ast_null_frame; |
| 6965 |
} |
| 6966 |
rtcp_report->reception_report_count = rc; |
| 6967 |
|
| 6968 |
ssrc = ntohl(rtcpheader[i + 2]); |
| 6969 |
rtcp_report->ssrc = ssrc; |
| 6970 |
break; |
| 6971 |
case RTCP_PT_FUR: |
| 6972 |
case RTCP_PT_PSFB: |
| 6973 |
ssrc = ntohl(rtcpheader[i + 1]); |
| 6974 |
break; |
| 6975 |
case AST_RTP_RTCP_RTPFB: |
| 6976 |
ssrc = ntohl(rtcpheader[i + 2]); |
| 6977 |
break; |
| 6978 |
case RTCP_PT_SDES: |
| 6979 |
case RTCP_PT_BYE: |
| 6980 |
default: |
| 6981 |
ssrc = 0; |
| 6982 |
ssrc_valid = 0; |
| 6983 |
break; |
| 6984 |
} |
| 6985 |
|
| 6986 |
if (rtcp_debug_test_addr(addr)) { |
| 6987 |
const char *subtype = rtcp_payload_subtype2str(pt, rc); |
| 6988 |
|
| 6989 |
ast_verbose("\n"); |
| 6990 |
ast_verbose("RTCP from %s\n", ast_sockaddr_stringify(addr)); |
| 6991 |
ast_verbose("PT: %u (%s)\n", pt, rtcp_payload_type2str(pt)); |
| 6992 |
if (subtype) { |
| 6993 |
ast_verbose("Packet Subtype: %u (%s)\n", rc, subtype); |
| 6994 |
} else { |
| 6995 |
ast_verbose("Reception reports: %u\n", rc); |
| 6996 |
} |
| 6997 |
ast_verbose("SSRC of sender: %u\n", ssrc); |
| 6998 |
} |
| 6999 |
|
| 7000 |
|
| 7001 |
if (ssrc_valid) { |
| 7002 |
|
| 7003 |
|
| 7004 |
|
| 7005 |
|
| 7006 |
if (use_packet_source) { |
| 7007 |
child = rtp_find_instance_by_packet_source_ssrc(transport, transport_rtp, ssrc); |
| 7008 |
} else { |
| 7009 |
child = rtp_find_instance_by_media_source_ssrc(transport, transport_rtp, ssrc); |
| 7010 |
} |
| 7011 |
if (child && child != transport) { |
| 7012 |
|
| 7013 |
|
| 7014 |
|
| 7015 |
|
| 7016 |
|
| 7017 |
ao2_lock(child); |
| 7018 |
instance = child; |
| 7019 |
rtp = ast_rtp_instance_get_data(instance); |
| 7020 |
} else { |
| 7021 |
|
| 7022 |
child = NULL; |
| 7023 |
rtp = transport_rtp; |
| 7024 |
} |
| 7025 |
} else { |
| 7026 |
child = NULL; |
| 7027 |
rtp = transport_rtp; |
| 7028 |
} |
| 7029 |
|
| 7030 |
if (ssrc_valid && rtp->themssrc_valid) { |
| 7031 |
|
| 7032 |
|
| 7033 |
|
| 7034 |
|
| 7035 |
|
| 7036 |
|
| 7037 |
|
| 7038 |
|
| 7039 |
int exception = (ssrc == 1 && !((pt == RTCP_PT_PSFB && rc == AST_RTP_RTCP_FMT_PLI) || pt == RTCP_PT_FUR)); |
| 7040 |
if ((ssrc != rtp->themssrc && use_packet_source && ssrc != 1) |
| 7041 |
|| exception) { |
| 7042 |
|
| 7043 |
|
| 7044 |
|
| 7045 |
|
| 7046 |
|
| 7047 |
position += length; |
| 7048 |
ast_debug_rtcp(1, "(%p) RTCP %p -- from %s: Skipping record, received SSRC '%u' != expected '%u'\n", |
| 7049 |
instance, rtp, ast_sockaddr_stringify(addr), ssrc, rtp->themssrc); |
| 7050 |
if (child) { |
| 7051 |
ao2_unlock(child); |
| 7052 |
} |
| 7053 |
continue; |
| 7054 |
} |
| 7055 |
ssrc_seen = 1; |
| 7056 |
} |
| 7057 |
|
| 7058 |
if (ssrc_seen && ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT)) { |
| 7059 |
|
| 7060 |
if (ast_sockaddr_cmp(&rtp->rtcp->them, addr)) { |
| 7061 |
ast_sockaddr_copy(&rtp->rtcp->them, addr); |
| 7062 |
if (ast_debug_rtp_packet_is_allowed) { |
| 7063 |
ast_debug(0, "(%p) RTCP NAT: Got RTCP from other end. Now sending to address %s\n", |
| 7064 |
instance, ast_sockaddr_stringify(addr)); |
| 7065 |
} |
| 7066 |
} |
| 7067 |
} |
| 7068 |
|
| 7069 |
i += RTCP_HEADER_SSRC_LENGTH; |
| 7070 |
switch (pt) { |
| 7071 |
case RTCP_PT_SR: |
| 7072 |
gettimeofday(&rtp->rtcp->rxlsr, NULL); |
| 7073 |
rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16); |
| 7074 |
rtp->rtcp->spc = ntohl(rtcpheader[i + 3]); |
| 7075 |
rtp->rtcp->soc = ntohl(rtcpheader[i + 4]); |
| 7076 |
|
| 7077 |
rtcp_report->type = RTCP_PT_SR; |
| 7078 |
rtcp_report->sender_information.packet_count = rtp->rtcp->spc; |
| 7079 |
rtcp_report->sender_information.octet_count = rtp->rtcp->soc; |
| 7080 |
ntp2timeval((unsigned int)ntohl(rtcpheader[i]), |
| 7081 |
(unsigned int)ntohl(rtcpheader[i + 1]), |
| 7082 |
&rtcp_report->sender_information.ntp_timestamp); |
| 7083 |
rtcp_report->sender_information.rtp_timestamp = ntohl(rtcpheader[i + 2]); |
| 7084 |
if (rtcp_debug_test_addr(addr)) { |
| 7085 |
ast_verbose("NTP timestamp: %u.%06u\n", |
| 7086 |
(unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_sec, |
| 7087 |
(unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_usec); |
| 7088 |
ast_verbose("RTP timestamp: %u\n", rtcp_report->sender_information.rtp_timestamp); |
| 7089 |
ast_verbose("SPC: %u\tSOC: %u\n", |
| 7090 |
rtcp_report->sender_information.packet_count, |
| 7091 |
rtcp_report->sender_information.octet_count); |
| 7092 |
} |
| 7093 |
i += RTCP_SR_BLOCK_WORD_LENGTH; |
| 7094 |
|
| 7095 |
case RTCP_PT_RR: |
| 7096 |
if (rtcp_report->type != RTCP_PT_SR) { |
| 7097 |
rtcp_report->type = RTCP_PT_RR; |
| 7098 |
} |
| 7099 |
|
| 7100 |
if (rc > 0) { |
| 7101 |
|
| 7102 |
report_block = ast_calloc(1, sizeof(*report_block)); |
| 7103 |
if (!report_block) { |
| 7104 |
if (child) { |
| 7105 |
ao2_unlock(child); |
| 7106 |
} |
| 7107 |
return &ast_null_frame; |
| 7108 |
} |
| 7109 |
rtcp_report->report_block[0] = report_block; |
| 7110 |
report_block->source_ssrc = ntohl(rtcpheader[i]); |
| 7111 |
report_block->lost_count.packets = ntohl(rtcpheader[i + 1]) & 0x00ffffff; |
| 7112 |
report_block->lost_count.fraction = ((ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24); |
| 7113 |
report_block->highest_seq_no = ntohl(rtcpheader[i + 2]); |
| 7114 |
report_block->ia_jitter = ntohl(rtcpheader[i + 3]); |
| 7115 |
report_block->lsr = ntohl(rtcpheader[i + 4]); |
| 7116 |
report_block->dlsr = ntohl(rtcpheader[i + 5]); |
| 7117 |
if (report_block->lsr) { |
| 7118 |
int skewed = update_rtt_stats(rtp, report_block->lsr, report_block->dlsr); |
| 7119 |
if (skewed && rtcp_debug_test_addr(addr)) { |
| 7120 |
struct timeval now; |
| 7121 |
unsigned int lsr_now, lsw, msw; |
| 7122 |
gettimeofday(&now, NULL); |
| 7123 |
timeval2ntp(now, &msw, &lsw); |
| 7124 |
lsr_now = (((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16)); |
| 7125 |
ast_verbose("Internal RTCP NTP clock skew detected: " |
| 7126 |
"lsr=%u, now=%u, dlsr=%u (%u:%03ums), " |
| 7127 |
"diff=%u\n", |
| 7128 |
report_block->lsr, lsr_now, report_block->dlsr, report_block->dlsr / 65536, |
| 7129 |
(report_block->dlsr % 65536) * 1000 / 65536, |
| 7130 |
report_block->dlsr - (lsr_now - report_block->lsr)); |
| 7131 |
} |
| 7132 |
} |
| 7133 |
update_jitter_stats(rtp, report_block->ia_jitter); |
| 7134 |
update_lost_stats(rtp, report_block->lost_count.packets); |
| 7135 |
|
| 7136 |
|
| 7137 |
|
| 7138 |
|
| 7139 |
|
| 7140 |
update_reported_mes_stats(rtp); |
| 7141 |
|
| 7142 |
if (rtcp_debug_test_addr(addr)) { |
| 7143 |
int rate = ast_rtp_get_rate(rtp->f.subclass.format); |
| 7144 |
|
| 7145 |
ast_verbose(" Fraction lost: %d\n", report_block->lost_count.fraction); |
| 7146 |
ast_verbose(" Packets lost so far: %u\n", report_block->lost_count.packets); |
| 7147 |
ast_verbose(" Highest sequence number: %u\n", report_block->highest_seq_no & 0x0000ffff); |
| 7148 |
ast_verbose(" Sequence number cycles: %u\n", report_block->highest_seq_no >> 16); |
| 7149 |
ast_verbose(" Interarrival jitter (samp): %u\n", report_block->ia_jitter); |
| 7150 |
ast_verbose(" Interarrival jitter (secs): %.6f\n", ast_samp2sec(report_block->ia_jitter, rate)); |
| 7151 |
ast_verbose(" Last SR(our NTP): %lu.%010lu\n",(unsigned long)(report_block->lsr) >> 16,((unsigned long)(report_block->lsr) << 16) * 4096); |
| 7152 |
ast_verbose(" DLSR: %4.4f (sec)\n",(double)report_block->dlsr / 65536.0); |
| 7153 |
ast_verbose(" RTT: %4.4f(sec)\n", rtp->rtcp->rtt); |
| 7154 |
ast_verbose(" MES: %4.1f\n", rtp->rtcp->reported_mes); |
| 7155 |
} |
| 7156 |
} |
| 7157 |
|
| 7158 |
|
| 7159 |
|
| 7160 |
|
| 7161 |
message_blob = ast_json_pack("{s: s, s: s, s: f, s: f}", |
| 7162 |
"from", ast_sockaddr_stringify(addr), |
| 7163 |
"to", transport_rtp->rtcp->local_addr_str, |
| 7164 |
"rtt", rtp->rtcp->rtt, |
| 7165 |
"mes", rtp->rtcp->reported_mes); |
| 7166 |
ast_rtp_publish_rtcp_message(instance, ast_rtp_rtcp_received_type(), |
| 7167 |
rtcp_report, |
| 7168 |
message_blob); |
| 7169 |
ast_json_unref(message_blob); |
| 7170 |
|
| 7171 |
|
| 7172 |
|
| 7173 |
transport_rtp->f.frametype = AST_FRAME_RTCP; |
| 7174 |
transport_rtp->f.subclass.integer = pt; |
| 7175 |
transport_rtp->f.data.ptr = rtp->rtcp->frame_buf + AST_FRIENDLY_OFFSET; |
| 7176 |
memcpy(transport_rtp->f.data.ptr, rtcp_report, sizeof(struct ast_rtp_rtcp_report)); |
| 7177 |
transport_rtp->f.datalen = sizeof(struct ast_rtp_rtcp_report); |
| 7178 |
if (rc > 0) { |
| 7179 |
|
| 7180 |
struct ast_rtp_rtcp_report *rtcp_report2; |
| 7181 |
report_block = transport_rtp->f.data.ptr + transport_rtp->f.datalen + sizeof(struct ast_rtp_rtcp_report_block *); |
| 7182 |
memcpy(report_block, rtcp_report->report_block[0], sizeof(struct ast_rtp_rtcp_report_block)); |
| 7183 |
rtcp_report2 = (struct ast_rtp_rtcp_report *)transport_rtp->f.data.ptr; |
| 7184 |
rtcp_report2->report_block[0] = report_block; |
| 7185 |
transport_rtp->f.datalen += sizeof(struct ast_rtp_rtcp_report_block); |
| 7186 |
} |
| 7187 |
transport_rtp->f.offset = AST_FRIENDLY_OFFSET; |
| 7188 |
transport_rtp->f.samples = 0; |
| 7189 |
transport_rtp->f.mallocd = 0; |
| 7190 |
transport_rtp->f.delivery.tv_sec = 0; |
| 7191 |
transport_rtp->f.delivery.tv_usec = 0; |
| 7192 |
transport_rtp->f.src = "RTP"; |
| 7193 |
transport_rtp->f.stream_num = rtp->stream_num; |
| 7194 |
f = &transport_rtp->f; |
| 7195 |
break; |
| 7196 |
case AST_RTP_RTCP_RTPFB: |
| 7197 |
switch (rc) { |
| 7198 |
case AST_RTP_RTCP_FMT_NACK: |
| 7199 |
|
| 7200 |
if (!rtp->send_buffer) { |
| 7201 |
break; |
| 7202 |
} |
| 7203 |
|
| 7204 |
if (rtcp_debug_test_addr(addr)) { |
| 7205 |
ast_verbose("Received generic RTCP NACK message\n"); |
| 7206 |
} |
| 7207 |
|
| 7208 |
ast_rtp_rtcp_handle_nack(instance, rtcpheader, position, length); |
| 7209 |
break; |
| 7210 |
default: |
| 7211 |
break; |
| 7212 |
} |
| 7213 |
break; |
| 7214 |
case RTCP_PT_FUR: |
| 7215 |
|
| 7216 |
rc = AST_RTP_RTCP_FMT_FIR; |
| 7217 |
case RTCP_PT_PSFB: |
| 7218 |
switch (rc) { |
| 7219 |
case AST_RTP_RTCP_FMT_PLI: |
| 7220 |
case AST_RTP_RTCP_FMT_FIR: |
| 7221 |
if (rtcp_debug_test_addr(addr)) { |
| 7222 |
ast_verbose("Received an RTCP Fast Update Request\n"); |
| 7223 |
} |
| 7224 |
transport_rtp->f.frametype = AST_FRAME_CONTROL; |
| 7225 |
transport_rtp->f.subclass.integer = AST_CONTROL_VIDUPDATE; |
| 7226 |
transport_rtp->f.datalen = 0; |
| 7227 |
transport_rtp->f.samples = 0; |
| 7228 |
transport_rtp->f.mallocd = 0; |
| 7229 |
transport_rtp->f.src = "RTP"; |
| 7230 |
f = &transport_rtp->f; |
| 7231 |
break; |
| 7232 |
case AST_RTP_RTCP_FMT_REMB: |
| 7233 |
|
| 7234 |
if (!ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_REMB)) { |
| 7235 |
break; |
| 7236 |
} |
| 7237 |
|
| 7238 |
if (rtcp_debug_test_addr(addr)) { |
| 7239 |
ast_verbose("Received REMB report\n"); |
| 7240 |
} |
| 7241 |
transport_rtp->f.frametype = AST_FRAME_RTCP; |
| 7242 |
transport_rtp->f.subclass.integer = pt; |
| 7243 |
transport_rtp->f.stream_num = rtp->stream_num; |
| 7244 |
transport_rtp->f.data.ptr = rtp->rtcp->frame_buf + AST_FRIENDLY_OFFSET; |
| 7245 |
feedback = transport_rtp->f.data.ptr; |
| 7246 |
feedback->fmt = rc; |
| 7247 |
|
| 7248 |
|
| 7249 |
first_word = ntohl(rtcpheader[i + 2]); |
| 7250 |
feedback->remb.br_exp = (first_word >> 18) & ((1 << 6) - 1); |
| 7251 |
feedback->remb.br_mantissa = first_word & ((1 << 18) - 1); |
| 7252 |
|
| 7253 |
transport_rtp->f.datalen = sizeof(struct ast_rtp_rtcp_feedback); |
| 7254 |
transport_rtp->f.offset = AST_FRIENDLY_OFFSET; |
| 7255 |
transport_rtp->f.samples = 0; |
| 7256 |
transport_rtp->f.mallocd = 0; |
| 7257 |
transport_rtp->f.delivery.tv_sec = 0; |
| 7258 |
transport_rtp->f.delivery.tv_usec = 0; |
| 7259 |
transport_rtp->f.src = "RTP"; |
| 7260 |
f = &transport_rtp->f; |
| 7261 |
break; |
| 7262 |
default: |
| 7263 |
break; |
| 7264 |
} |
| 7265 |
break; |
| 7266 |
case RTCP_PT_SDES: |
| 7267 |
if (rtcp_debug_test_addr(addr)) { |
| 7268 |
ast_verbose("Received an SDES from %s\n", |
| 7269 |
ast_sockaddr_stringify(addr)); |
| 7270 |
} |
| 7271 |
#ifdef TEST_FRAMEWORK |
| 7272 |
if ((test_engine = ast_rtp_instance_get_test(instance))) { |
| 7273 |
test_engine->sdes_received = 1; |
| 7274 |
} |
| 7275 |
#endif |
| 7276 |
break; |
| 7277 |
case RTCP_PT_BYE: |
| 7278 |
if (rtcp_debug_test_addr(addr)) { |
| 7279 |
ast_verbose("Received a BYE from %s\n", |
| 7280 |
ast_sockaddr_stringify(addr)); |
| 7281 |
} |
| 7282 |
break; |
| 7283 |
default: |
| 7284 |
break; |
| 7285 |
} |
| 7286 |
position += length; |
| 7287 |
rtp->rtcp->rtcp_info = 1; |
| 7288 |
|
| 7289 |
if (child) { |
| 7290 |
ao2_unlock(child); |
| 7291 |
} |
| 7292 |
} |
| 7293 |
|
| 7294 |
return f; |
| 7295 |
} |
| 7296 |
|
| 7297 |
|
| 7298 |
static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance) |
| 7299 |
{ |
| 7300 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 7301 |
struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance, 1); |
| 7302 |
struct ast_sockaddr addr; |
| 7303 |
unsigned char rtcpdata[8192 + AST_FRIENDLY_OFFSET]; |
| 7304 |
unsigned char *read_area = rtcpdata + AST_FRIENDLY_OFFSET; |
| 7305 |
size_t read_area_size = sizeof(rtcpdata) - AST_FRIENDLY_OFFSET; |
| 7306 |
int res; |
| 7307 |
|
| 7308 |
|
| 7309 |
if ((res = rtcp_recvfrom(instance, read_area, read_area_size, |
| 7310 |
0, &addr)) < 0) { |
| 7311 |
if (res == RTP_DTLS_ESTABLISHED) { |
| 7312 |
rtp->f.frametype = AST_FRAME_CONTROL; |
| 7313 |
rtp->f.subclass.integer = AST_CONTROL_SRCCHANGE; |
| 7314 |
return &rtp->f; |
| 7315 |
} |
| 7316 |
|
| 7317 |
ast_assert(errno != EBADF); |
| 7318 |
if (errno != EAGAIN) { |
| 7319 |
ast_log(LOG_WARNING, "RTCP Read error: %s. Hanging up.\n", |
| 7320 |
(errno) ? strerror(errno) : "Unspecified"); |
| 7321 |
return NULL; |
| 7322 |
} |
| 7323 |
return &ast_null_frame; |
| 7324 |
} |
| 7325 |
|
| 7326 |
|
| 7327 |
if (!res) { |
| 7328 |
return &ast_null_frame; |
| 7329 |
} |
| 7330 |
|
| 7331 |
if (!*read_area) { |
| 7332 |
struct sockaddr_in addr_tmp; |
| 7333 |
struct ast_sockaddr addr_v4; |
| 7334 |
|
| 7335 |
if (ast_sockaddr_is_ipv4(&addr)) { |
| 7336 |
ast_sockaddr_to_sin(&addr, &addr_tmp); |
| 7337 |
} else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) { |
| 7338 |
ast_debug_stun(2, "(%p) STUN using IPv6 mapped address %s\n", |
| 7339 |
instance, ast_sockaddr_stringify(&addr)); |
| 7340 |
ast_sockaddr_to_sin(&addr_v4, &addr_tmp); |
| 7341 |
} else { |
| 7342 |
ast_debug_stun(2, "(%p) STUN cannot do for non IPv4 address %s\n", |
| 7343 |
instance, ast_sockaddr_stringify(&addr)); |
| 7344 |
return &ast_null_frame; |
| 7345 |
} |
| 7346 |
if ((ast_stun_handle_packet(rtp->rtcp->s, &addr_tmp, read_area, res, NULL, NULL) == AST_STUN_ACCEPT)) { |
| 7347 |
ast_sockaddr_from_sin(&addr, &addr_tmp); |
| 7348 |
ast_sockaddr_copy(&rtp->rtcp->them, &addr); |
| 7349 |
} |
| 7350 |
return &ast_null_frame; |
| 7351 |
} |
| 7352 |
|
| 7353 |
return ast_rtcp_interpret(instance, srtp, read_area, res, &addr); |
| 7354 |
} |
| 7355 |
|
| 7356 |
|
| 7357 |
static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, |
| 7358 |
struct ast_rtp_instance *instance1, unsigned int *rtpheader, int len, int hdrlen) |
| 7359 |
{ |
| 7360 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 7361 |
struct ast_rtp *bridged; |
| 7362 |
int res = 0, payload = 0, bridged_payload = 0, mark; |
| 7363 |
RAII_VAR(struct ast_rtp_payload_type *, payload_type, NULL, ao2_cleanup); |
| 7364 |
int reconstruct = ntohl(rtpheader[0]); |
| 7365 |
struct ast_sockaddr remote_address = { {0,} }; |
| 7366 |
int ice; |
| 7367 |
unsigned int timestamp = ntohl(rtpheader[1]); |
| 7368 |
|
| 7369 |
|
| 7370 |
payload = (reconstruct & 0x7f0000) >> 16; |
| 7371 |
mark = (reconstruct & 0x800000) >> 23; |
| 7372 |
|
| 7373 |
|
| 7374 |
payload_type = ast_rtp_codecs_get_payload(ast_rtp_instance_get_codecs(instance), payload); |
| 7375 |
if (!payload_type) { |
| 7376 |
return -1; |
| 7377 |
} |
| 7378 |
|
| 7379 |
|
| 7380 |
bridged_payload = ast_rtp_codecs_payload_code_tx_sample_rate(ast_rtp_instance_get_codecs(instance1), |
| 7381 |
payload_type->asterisk_format, payload_type->format, payload_type->rtp_code, payload_type->sample_rate); |
| 7382 |
|
| 7383 |
|
| 7384 |
if (bridged_payload < 0) { |
| 7385 |
return -1; |
| 7386 |
} |
| 7387 |
|
| 7388 |
|
| 7389 |
if (ast_rtp_codecs_find_payload_code(ast_rtp_instance_get_codecs(instance1), bridged_payload) == -1) { |
| 7390 |
ast_debug_rtp(1, "(%p, %p) RTP unsupported payload type received\n", instance, instance1); |
| 7391 |
return -1; |
| 7392 |
} |
| 7393 |
|
| 7394 |
|
| 7395 |
|
| 7396 |
|
| 7397 |
|
| 7398 |
|
| 7399 |
if (rtp->last_end_timestamp.is_set && rtp->last_end_timestamp.ts == timestamp) { |
| 7400 |
ast_debug_rtp(1, "(%p, %p) RTP feeding packet with duplicate timestamp to core\n", instance, instance1); |
| 7401 |
return -1; |
| 7402 |
} |
| 7403 |
|
| 7404 |
if (payload_type->asterisk_format) { |
| 7405 |
ao2_replace(rtp->lastrxformat, payload_type->format); |
| 7406 |
} |
| 7407 |
|
| 7408 |
|
| 7409 |
|
| 7410 |
|
| 7411 |
|
| 7412 |
|
| 7413 |
|
| 7414 |
|
| 7415 |
|
| 7416 |
|
| 7417 |
|
| 7418 |
|
| 7419 |
|
| 7420 |
ao2_unlock(instance); |
| 7421 |
ao2_lock(instance1); |
| 7422 |
|
| 7423 |
|
| 7424 |
|
| 7425 |
|
| 7426 |
|
| 7427 |
bridged = ast_rtp_instance_get_data(instance1); |
| 7428 |
|
| 7429 |
|
| 7430 |
|
| 7431 |
if (bridged->sending_digit) { |
| 7432 |
ast_debug_rtp(1, "(%p, %p) RTP Feeding packet to core until DTMF finishes\n", instance, instance1); |
| 7433 |
ao2_unlock(instance1); |
| 7434 |
ao2_lock(instance); |
| 7435 |
return -1; |
| 7436 |
} |
| 7437 |
|
| 7438 |
if (payload_type->asterisk_format) { |
| 7439 |
|
| 7440 |
|
| 7441 |
|
| 7442 |
|
| 7443 |
if (!bridged->asymmetric_codec |
| 7444 |
&& bridged->lastrxformat != ast_format_none |
| 7445 |
&& ast_format_cmp(payload_type->format, bridged->lastrxformat) == AST_FORMAT_CMP_NOT_EQUAL) { |
| 7446 |
ast_debug_rtp(1, "(%p, %p) RTP asymmetric RTP codecs detected (TX: %s, RX: %s) sending frame to core\n", |
| 7447 |
instance, instance1, ast_format_get_name(payload_type->format), |
| 7448 |
ast_format_get_name(bridged->lastrxformat)); |
| 7449 |
ao2_unlock(instance1); |
| 7450 |
ao2_lock(instance); |
| 7451 |
return -1; |
| 7452 |
} |
| 7453 |
|
| 7454 |
ao2_replace(bridged->lasttxformat, payload_type->format); |
| 7455 |
} |
| 7456 |
|
| 7457 |
ast_rtp_instance_get_remote_address(instance1, &remote_address); |
| 7458 |
|
| 7459 |
if (ast_sockaddr_isnull(&remote_address)) { |
| 7460 |
ast_debug_rtp(5, "(%p, %p) RTP remote address is null, most likely RTP has been stopped\n", |
| 7461 |
instance, instance1); |
| 7462 |
ao2_unlock(instance1); |
| 7463 |
ao2_lock(instance); |
| 7464 |
return 0; |
| 7465 |
} |
| 7466 |
|
| 7467 |
|
| 7468 |
if (ast_test_flag(bridged, FLAG_NEED_MARKER_BIT)) { |
| 7469 |
mark = 1; |
| 7470 |
ast_clear_flag(bridged, FLAG_NEED_MARKER_BIT); |
| 7471 |
} |
| 7472 |
|
| 7473 |
|
| 7474 |
if (ast_test_flag(bridged, FLAG_REQ_LOCAL_BRIDGE_BIT)) { |
| 7475 |
mark = 1; |
| 7476 |
ast_clear_flag(bridged, FLAG_REQ_LOCAL_BRIDGE_BIT); |
| 7477 |
} |
| 7478 |
|
| 7479 |
|
| 7480 |
reconstruct &= 0xFF80FFFF; |
| 7481 |
reconstruct |= (bridged_payload << 16); |
| 7482 |
reconstruct |= (mark << 23); |
| 7483 |
rtpheader[0] = htonl(reconstruct); |
| 7484 |
|
| 7485 |
if (mark) { |
| 7486 |
|
| 7487 |
bridged->ssrc = ntohl(rtpheader[2]); |
| 7488 |
} |
| 7489 |
|
| 7490 |
|
| 7491 |
res = rtp_sendto(instance1, (void *)rtpheader, len, 0, &remote_address, &ice); |
| 7492 |
if (res < 0) { |
| 7493 |
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))) { |
| 7494 |
ast_log(LOG_WARNING, |
| 7495 |
"RTP Transmission error of packet to %s: %s\n", |
| 7496 |
ast_sockaddr_stringify(&remote_address), |
| 7497 |
strerror(errno)); |
| 7498 |
} else if (((ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || ast_debug_rtp_packet_is_allowed) && !ast_test_flag(bridged, FLAG_NAT_INACTIVE_NOWARN)) { |
| 7499 |
if (ast_debug_rtp_packet_is_allowed || DEBUG_ATLEAST(1)) { |
| 7500 |
ast_log(LOG_WARNING, |
| 7501 |
"RTP NAT: Can't write RTP to private " |
| 7502 |
"address %s, waiting for other end to " |
| 7503 |
"send audio...\n", |
| 7504 |
ast_sockaddr_stringify(&remote_address)); |
| 7505 |
} |
| 7506 |
ast_set_flag(bridged, FLAG_NAT_INACTIVE_NOWARN); |
| 7507 |
} |
| 7508 |
ao2_unlock(instance1); |
| 7509 |
ao2_lock(instance); |
| 7510 |
return 0; |
| 7511 |
} |
| 7512 |
|
| 7513 |
if (rtp_debug_test_addr(&remote_address)) { |
| 7514 |
ast_verbose("Sent RTP P2P packet to %s%s (type %-2.2d, len %-6.6d)\n", |
| 7515 |
ast_sockaddr_stringify(&remote_address), |
| 7516 |
ice ? " (via ICE)" : "", |
| 7517 |
bridged_payload, len - hdrlen); |
| 7518 |
} |
| 7519 |
|
| 7520 |
ao2_unlock(instance1); |
| 7521 |
ao2_lock(instance); |
| 7522 |
return 0; |
| 7523 |
} |
| 7524 |
|
| 7525 |
static void rtp_instance_unlock(struct ast_rtp_instance *instance) |
| 7526 |
{ |
| 7527 |
if (instance) { |
| 7528 |
ao2_unlock(instance); |
| 7529 |
} |
| 7530 |
} |
| 7531 |
|
| 7532 |
static int rtp_transport_wide_cc_packet_statistics_cmp(struct rtp_transport_wide_cc_packet_statistics a, |
| 7533 |
struct rtp_transport_wide_cc_packet_statistics b) |
| 7534 |
{ |
| 7535 |
return a.seqno - b.seqno; |
| 7536 |
} |
| 7537 |
|
| 7538 |
static void rtp_transport_wide_cc_feedback_status_vector_append(unsigned char *rtcpheader, int *packet_len, int *status_vector_chunk_bits, |
| 7539 |
uint16_t *status_vector_chunk, int status) |
| 7540 |
{ |
| 7541 |
|
| 7542 |
*status_vector_chunk_bits -= 2; |
| 7543 |
|
| 7544 |
|
| 7545 |
|
| 7546 |
|
| 7547 |
|
| 7548 |
*status_vector_chunk |= (status << (16 - 2 - (14 - *status_vector_chunk_bits))); |
| 7549 |
|
| 7550 |
|
| 7551 |
if (*status_vector_chunk_bits) { |
| 7552 |
return; |
| 7553 |
} |
| 7554 |
|
| 7555 |
|
| 7556 |
put_unaligned_uint16(rtcpheader + *packet_len, htons(*status_vector_chunk)); |
| 7557 |
*status_vector_chunk_bits = 14; |
| 7558 |
|
| 7559 |
|
| 7560 |
|
| 7561 |
|
| 7562 |
|
| 7563 |
*status_vector_chunk = (1 << 15) | (1 << 14); |
| 7564 |
*packet_len += 2; |
| 7565 |
} |
| 7566 |
|
| 7567 |
static void rtp_transport_wide_cc_feedback_status_append(unsigned char *rtcpheader, int *packet_len, int *status_vector_chunk_bits, |
| 7568 |
uint16_t *status_vector_chunk, int *run_length_chunk_count, int *run_length_chunk_status, int status) |
| 7569 |
{ |
| 7570 |
if (*run_length_chunk_status != status) { |
| 7571 |
while (*run_length_chunk_count > 0 && *run_length_chunk_count < 8) { |
| 7572 |
|
| 7573 |
|
| 7574 |
|
| 7575 |
|
| 7576 |
|
| 7577 |
|
| 7578 |
rtp_transport_wide_cc_feedback_status_vector_append(rtcpheader, packet_len, status_vector_chunk_bits, |
| 7579 |
status_vector_chunk, *run_length_chunk_status); |
| 7580 |
*run_length_chunk_count -= 1; |
| 7581 |
} |
| 7582 |
|
| 7583 |
if (*run_length_chunk_count) { |
| 7584 |
|
| 7585 |
put_unaligned_uint16(rtcpheader + *packet_len, htons((0 << 15) | (*run_length_chunk_status << 13) | *run_length_chunk_count)); |
| 7586 |
*packet_len += 2; |
| 7587 |
} |
| 7588 |
|
| 7589 |
|
| 7590 |
*run_length_chunk_count = 0; |
| 7591 |
*run_length_chunk_status = -1; |
| 7592 |
|
| 7593 |
if (*status_vector_chunk_bits == 14) { |
| 7594 |
|
| 7595 |
*run_length_chunk_status = status; |
| 7596 |
*run_length_chunk_count = 1; |
| 7597 |
} else { |
| 7598 |
|
| 7599 |
rtp_transport_wide_cc_feedback_status_vector_append(rtcpheader, packet_len, status_vector_chunk_bits, |
| 7600 |
status_vector_chunk, status); |
| 7601 |
} |
| 7602 |
} else { |
| 7603 |
|
| 7604 |
*run_length_chunk_count += 1; |
| 7605 |
} |
| 7606 |
} |
| 7607 |
|
| 7608 |
static int rtp_transport_wide_cc_feedback_produce(const void *data) |
| 7609 |
{ |
| 7610 |
struct ast_rtp_instance *instance = (struct ast_rtp_instance *) data; |
| 7611 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 7612 |
unsigned char *rtcpheader; |
| 7613 |
char bdata[1024]; |
| 7614 |
struct rtp_transport_wide_cc_packet_statistics *first_packet; |
| 7615 |
struct rtp_transport_wide_cc_packet_statistics *previous_packet; |
| 7616 |
int i; |
| 7617 |
int status_vector_chunk_bits = 14; |
| 7618 |
uint16_t status_vector_chunk = (1 << 15) | (1 << 14); |
| 7619 |
int run_length_chunk_count = 0; |
| 7620 |
int run_length_chunk_status = -1; |
| 7621 |
int packet_len = 20; |
| 7622 |
int delta_len = 0; |
| 7623 |
int packet_count = 0; |
| 7624 |
unsigned int received_msw; |
| 7625 |
unsigned int received_lsw; |
| 7626 |
struct ast_sockaddr remote_address = { { 0, } }; |
| 7627 |
int res; |
| 7628 |
int ice; |
| 7629 |
unsigned int large_delta_count = 0; |
| 7630 |
unsigned int small_delta_count = 0; |
| 7631 |
unsigned int lost_count = 0; |
| 7632 |
|
| 7633 |
if (!rtp || !rtp->rtcp || rtp->transport_wide_cc.schedid == -1) { |
| 7634 |
ao2_ref(instance, -1); |
| 7635 |
return 0; |
| 7636 |
} |
| 7637 |
|
| 7638 |
ao2_lock(instance); |
| 7639 |
|
| 7640 |
|
| 7641 |
if (!AST_VECTOR_SIZE(&rtp->transport_wide_cc.packet_statistics)) { |
| 7642 |
ao2_unlock(instance); |
| 7643 |
return 1000; |
| 7644 |
} |
| 7645 |
|
| 7646 |
rtcpheader = (unsigned char *)bdata; |
| 7647 |
|
| 7648 |
|
| 7649 |
first_packet = AST_VECTOR_GET_ADDR(&rtp->transport_wide_cc.packet_statistics, 0); |
| 7650 |
previous_packet = first_packet; |
| 7651 |
|
| 7652 |
|
| 7653 |
|
| 7654 |
|
| 7655 |
|
| 7656 |
for (i = 0; i < AST_VECTOR_SIZE(&rtp->transport_wide_cc.packet_statistics); ++i) { |
| 7657 |
struct rtp_transport_wide_cc_packet_statistics *statistics; |
| 7658 |
int lost = 0; |
| 7659 |
int res = 0; |
| 7660 |
|
| 7661 |
statistics = AST_VECTOR_GET_ADDR(&rtp->transport_wide_cc.packet_statistics, i); |
| 7662 |
|
| 7663 |
packet_count++; |
| 7664 |
|
| 7665 |
if (first_packet != statistics) { |
| 7666 |
|
| 7667 |
|
| 7668 |
|
| 7669 |
|
| 7670 |
lost = statistics->seqno - (previous_packet->seqno + 1); |
| 7671 |
lost_count += lost; |
| 7672 |
} |
| 7673 |
|
| 7674 |
while (lost) { |
| 7675 |
|
| 7676 |
rtp_transport_wide_cc_feedback_status_append(rtcpheader, &packet_len, &status_vector_chunk_bits, |
| 7677 |
&status_vector_chunk, &run_length_chunk_count, &run_length_chunk_status, 0); |
| 7678 |
packet_count++; |
| 7679 |
|
| 7680 |
|
| 7681 |
|
| 7682 |
|
| 7683 |
if (packet_len + delta_len + 20 > sizeof(bdata)) { |
| 7684 |
res = -1; |
| 7685 |
break; |
| 7686 |
} |
| 7687 |
|
| 7688 |
lost--; |
| 7689 |
} |
| 7690 |
|
| 7691 |
|
| 7692 |
if (res) { |
| 7693 |
break; |
| 7694 |
} |
| 7695 |
|
| 7696 |
|
| 7697 |
statistics->delta = ast_tvdiff_us(statistics->received, previous_packet->received) / 250; |
| 7698 |
|
| 7699 |
|
| 7700 |
if (statistics->delta < 0 || statistics->delta > 127) { |
| 7701 |
|
| 7702 |
rtp_transport_wide_cc_feedback_status_append(rtcpheader, &packet_len, &status_vector_chunk_bits, |
| 7703 |
&status_vector_chunk, &run_length_chunk_count, &run_length_chunk_status, 2); |
| 7704 |
delta_len += 2; |
| 7705 |
large_delta_count++; |
| 7706 |
} else { |
| 7707 |
|
| 7708 |
rtp_transport_wide_cc_feedback_status_append(rtcpheader, &packet_len, &status_vector_chunk_bits, |
| 7709 |
&status_vector_chunk, &run_length_chunk_count, &run_length_chunk_status, 1); |
| 7710 |
delta_len += 1; |
| 7711 |
small_delta_count++; |
| 7712 |
} |
| 7713 |
|
| 7714 |
previous_packet = statistics; |
| 7715 |
|
| 7716 |
|
| 7717 |
if (packet_len + delta_len + 20 > sizeof(bdata)) { |
| 7718 |
break; |
| 7719 |
} |
| 7720 |
} |
| 7721 |
|
| 7722 |
if (status_vector_chunk_bits != 14) { |
| 7723 |
|
| 7724 |
put_unaligned_uint16(rtcpheader + packet_len, htons(status_vector_chunk)); |
| 7725 |
packet_len += 2; |
| 7726 |
} else if (run_length_chunk_count) { |
| 7727 |
|
| 7728 |
put_unaligned_uint16(rtcpheader + packet_len, htons((0 << 15) | (run_length_chunk_status << 13) | run_length_chunk_count)); |
| 7729 |
packet_len += 2; |
| 7730 |
} |
| 7731 |
|
| 7732 |
|
| 7733 |
for (i = 0; i < AST_VECTOR_SIZE(&rtp->transport_wide_cc.packet_statistics); ++i) { |
| 7734 |
struct rtp_transport_wide_cc_packet_statistics *statistics; |
| 7735 |
|
| 7736 |
statistics = AST_VECTOR_GET_ADDR(&rtp->transport_wide_cc.packet_statistics, i); |
| 7737 |
|
| 7738 |
if (statistics->delta < 0 || statistics->delta > 127) { |
| 7739 |
|
| 7740 |
put_unaligned_uint16(rtcpheader + packet_len, htons(statistics->delta)); |
| 7741 |
packet_len += 2; |
| 7742 |
} else { |
| 7743 |
|
| 7744 |
rtcpheader[packet_len] = statistics->delta; |
| 7745 |
packet_len += 1; |
| 7746 |
} |
| 7747 |
|
| 7748 |
|
| 7749 |
|
| 7750 |
|
| 7751 |
if (statistics == previous_packet) { |
| 7752 |
break; |
| 7753 |
} |
| 7754 |
} |
| 7755 |
|
| 7756 |
|
| 7757 |
while (packet_len % 4) { |
| 7758 |
rtcpheader[packet_len++] = 0; |
| 7759 |
} |
| 7760 |
|
| 7761 |
|
| 7762 |
put_unaligned_uint32(rtcpheader, htonl((2 << 30) | (AST_RTP_RTCP_FMT_TRANSPORT_WIDE_CC << 24) |
| 7763 |
| (AST_RTP_RTCP_RTPFB << 16) | ((packet_len / 4) - 1))); |
| 7764 |
put_unaligned_uint32(rtcpheader + 4, htonl(rtp->ssrc)); |
| 7765 |
put_unaligned_uint32(rtcpheader + 8, htonl(rtp->themssrc)); |
| 7766 |
|
| 7767 |
|
| 7768 |
put_unaligned_uint32(rtcpheader + 12, htonl((first_packet->seqno << 16) | packet_count)); |
| 7769 |
|
| 7770 |
timeval2ntp(first_packet->received, &received_msw, &received_lsw); |
| 7771 |
put_unaligned_time24(rtcpheader + 16, received_msw, received_lsw); |
| 7772 |
rtcpheader[19] = rtp->transport_wide_cc.feedback_count; |
| 7773 |
|
| 7774 |
|
| 7775 |
ast_sockaddr_copy(&remote_address, &rtp->rtcp->them); |
| 7776 |
|
| 7777 |
ast_debug_rtcp(2, "(%p) RTCP sending transport-cc feedback packet of size '%d' on '%s' with packet count of %d (small = %d, large = %d, lost = %d)\n", |
| 7778 |
instance, packet_len, ast_rtp_instance_get_channel_id(instance), packet_count, small_delta_count, large_delta_count, lost_count); |
| 7779 |
|
| 7780 |
res = rtcp_sendto(instance, (unsigned int *)rtcpheader, packet_len, 0, &remote_address, &ice); |
| 7781 |
if (res < 0) { |
| 7782 |
ast_log(LOG_ERROR, "RTCP transport-cc feedback error to %s due to %s\n", |
| 7783 |
ast_sockaddr_stringify(&remote_address), strerror(errno)); |
| 7784 |
} |
| 7785 |
|
| 7786 |
AST_VECTOR_RESET(&rtp->transport_wide_cc.packet_statistics, AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 7787 |
|
| 7788 |
rtp->transport_wide_cc.feedback_count++; |
| 7789 |
|
| 7790 |
ao2_unlock(instance); |
| 7791 |
|
| 7792 |
return 1000; |
| 7793 |
} |
| 7794 |
|
| 7795 |
static void rtp_instance_parse_transport_wide_cc(struct ast_rtp_instance *instance, struct ast_rtp *rtp, |
| 7796 |
unsigned char *data, int len) |
| 7797 |
{ |
| 7798 |
uint16_t *seqno = (uint16_t *)data; |
| 7799 |
struct rtp_transport_wide_cc_packet_statistics statistics; |
| 7800 |
struct ast_rtp_instance *transport = rtp->bundled ? rtp->bundled : instance; |
| 7801 |
struct ast_rtp *transport_rtp = ast_rtp_instance_get_data(transport); |
| 7802 |
|
| 7803 |
|
| 7804 |
if (((int)transport_rtp->transport_wide_cc.last_seqno - (int)ntohs(*seqno)) > 100) { |
| 7805 |
transport_rtp->transport_wide_cc.cycles += RTP_SEQ_MOD; |
| 7806 |
} |
| 7807 |
|
| 7808 |
|
| 7809 |
statistics.seqno = transport_rtp->transport_wide_cc.cycles + ntohs(*seqno); |
| 7810 |
statistics.received = ast_tvnow(); |
| 7811 |
|
| 7812 |
|
| 7813 |
|
| 7814 |
|
| 7815 |
if (AST_VECTOR_SIZE(&transport_rtp->transport_wide_cc.packet_statistics) > 1000) { |
| 7816 |
AST_VECTOR_RESET(&rtp->transport_wide_cc.packet_statistics, AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 7817 |
} |
| 7818 |
|
| 7819 |
if (!AST_VECTOR_SIZE(&transport_rtp->transport_wide_cc.packet_statistics) || |
| 7820 |
statistics.seqno > transport_rtp->transport_wide_cc.last_extended_seqno) { |
| 7821 |
|
| 7822 |
if (AST_VECTOR_APPEND(&transport_rtp->transport_wide_cc.packet_statistics, statistics)) { |
| 7823 |
return; |
| 7824 |
} |
| 7825 |
|
| 7826 |
transport_rtp->transport_wide_cc.last_extended_seqno = statistics.seqno; |
| 7827 |
transport_rtp->transport_wide_cc.last_seqno = ntohs(*seqno); |
| 7828 |
} else { |
| 7829 |
|
| 7830 |
if (AST_VECTOR_ADD_SORTED(&transport_rtp->transport_wide_cc.packet_statistics, statistics, |
| 7831 |
rtp_transport_wide_cc_packet_statistics_cmp)) { |
| 7832 |
return; |
| 7833 |
} |
| 7834 |
} |
| 7835 |
|
| 7836 |
|
| 7837 |
if (transport_rtp->transport_wide_cc.schedid < 0 && transport_rtp->rtcp) { |
| 7838 |
ast_debug_rtcp(1, "(%p) RTCP starting transport-cc feedback transmission on RTP instance '%p'\n", instance, transport); |
| 7839 |
ao2_ref(transport, +1); |
| 7840 |
transport_rtp->transport_wide_cc.schedid = ast_sched_add(rtp->sched, 1000, |
| 7841 |
rtp_transport_wide_cc_feedback_produce, transport); |
| 7842 |
if (transport_rtp->transport_wide_cc.schedid < 0) { |
| 7843 |
ao2_ref(transport, -1); |
| 7844 |
ast_log(LOG_WARNING, "Scheduling RTCP transport-cc feedback transmission failed on RTP instance '%p'\n", |
| 7845 |
transport); |
| 7846 |
} |
| 7847 |
} |
| 7848 |
} |
| 7849 |
|
| 7850 |
static void rtp_instance_parse_extmap_extensions(struct ast_rtp_instance *instance, struct ast_rtp *rtp, |
| 7851 |
unsigned char *extension, int len) |
| 7852 |
{ |
| 7853 |
int transport_wide_cc_id = ast_rtp_instance_extmap_get_id(instance, AST_RTP_EXTENSION_TRANSPORT_WIDE_CC); |
| 7854 |
int pos = 0; |
| 7855 |
|
| 7856 |
|
| 7857 |
if (transport_wide_cc_id == -1) { |
| 7858 |
return; |
| 7859 |
} |
| 7860 |
|
| 7861 |
|
| 7862 |
while (pos < len) { |
| 7863 |
int id = extension[pos] >> 4; |
| 7864 |
int extension_len = (extension[pos] & 0xF) + 1; |
| 7865 |
|
| 7866 |
|
| 7867 |
|
| 7868 |
|
| 7869 |
pos += 1; |
| 7870 |
|
| 7871 |
if (id == 0) { |
| 7872 |
|
| 7873 |
|
| 7874 |
|
| 7875 |
|
| 7876 |
|
| 7877 |
|
| 7878 |
|
| 7879 |
|
| 7880 |
continue; |
| 7881 |
} else if (id == 15) { |
| 7882 |
|
| 7883 |
|
| 7884 |
|
| 7885 |
|
| 7886 |
|
| 7887 |
|
| 7888 |
|
| 7889 |
|
| 7890 |
break; |
| 7891 |
} else if ((pos + extension_len) > len) { |
| 7892 |
|
| 7893 |
|
| 7894 |
|
| 7895 |
break; |
| 7896 |
} |
| 7897 |
|
| 7898 |
|
| 7899 |
if (id == transport_wide_cc_id) { |
| 7900 |
rtp_instance_parse_transport_wide_cc(instance, rtp, extension + pos, extension_len); |
| 7901 |
} |
| 7902 |
|
| 7903 |
|
| 7904 |
pos += extension_len; |
| 7905 |
} |
| 7906 |
} |
| 7907 |
|
| 7908 |
static struct ast_frame *ast_rtp_interpret(struct ast_rtp_instance *instance, struct ast_srtp *srtp, |
| 7909 |
const struct ast_sockaddr *remote_address, unsigned char *read_area, int length, int prev_seqno, |
| 7910 |
unsigned int bundled) |
| 7911 |
{ |
| 7912 |
unsigned int *rtpheader = (unsigned int*)(read_area); |
| 7913 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 7914 |
struct ast_rtp_instance *instance1; |
| 7915 |
int res = length, hdrlen = 12, ssrc, seqno, payloadtype, padding, mark, ext, cc; |
| 7916 |
unsigned int timestamp; |
| 7917 |
RAII_VAR(struct ast_rtp_payload_type *, payload, NULL, ao2_cleanup); |
| 7918 |
struct frame_list frames; |
| 7919 |
|
| 7920 |
|
| 7921 |
if ((*read_area & 0xC0) && res_srtp && srtp && res_srtp->unprotect( |
| 7922 |
srtp, read_area, &res, 0 | (srtp_replay_protection << 1)) < 0) { |
| 7923 |
return &ast_null_frame; |
| 7924 |
} |
| 7925 |
|
| 7926 |
|
| 7927 |
if (rtp->sending_digit) { |
| 7928 |
ast_rtp_dtmf_continuation(instance); |
| 7929 |
} |
| 7930 |
|
| 7931 |
|
| 7932 |
ssrc = ntohl(rtpheader[2]); |
| 7933 |
seqno = ntohl(rtpheader[0]); |
| 7934 |
payloadtype = (seqno & 0x7f0000) >> 16; |
| 7935 |
padding = seqno & (1 << 29); |
| 7936 |
mark = seqno & (1 << 23); |
| 7937 |
ext = seqno & (1 << 28); |
| 7938 |
cc = (seqno & 0xF000000) >> 24; |
| 7939 |
seqno &= 0xffff; |
| 7940 |
timestamp = ntohl(rtpheader[1]); |
| 7941 |
|
| 7942 |
AST_LIST_HEAD_INIT_NOLOCK(&frames); |
| 7943 |
|
| 7944 |
|
| 7945 |
if (padding) { |
| 7946 |
res -= read_area[res - 1]; |
| 7947 |
} |
| 7948 |
|
| 7949 |
|
| 7950 |
if (cc) { |
| 7951 |
hdrlen += cc * 4; |
| 7952 |
} |
| 7953 |
|
| 7954 |
|
| 7955 |
if (ext) { |
| 7956 |
int extensions_size = (ntohl(rtpheader[hdrlen/4]) & 0xffff) << 2; |
| 7957 |
unsigned int profile; |
| 7958 |
profile = (ntohl(rtpheader[3]) & 0xffff0000) >> 16; |
| 7959 |
|
| 7960 |
if (profile == 0xbede) { |
| 7961 |
|
| 7962 |
rtp_instance_parse_extmap_extensions(instance, rtp, read_area + hdrlen + 4, extensions_size); |
| 7963 |
} else if (DEBUG_ATLEAST(1)) { |
| 7964 |
if (profile == 0x505a) { |
| 7965 |
ast_log(LOG_DEBUG, "Found Zfone extension in RTP stream - zrtp - not supported.\n"); |
| 7966 |
} else { |
| 7967 |
|
| 7968 |
ast_log(LOG_DEBUG, "Found unknown RTP Extensions %x\n", profile); |
| 7969 |
} |
| 7970 |
} |
| 7971 |
|
| 7972 |
hdrlen += extensions_size; |
| 7973 |
hdrlen += 4; |
| 7974 |
} |
| 7975 |
|
| 7976 |
|
| 7977 |
if (res < hdrlen) { |
| 7978 |
ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d\n", res, hdrlen); |
| 7979 |
return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame; |
| 7980 |
} |
| 7981 |
|
| 7982 |
|
| 7983 |
if (!bundled) { |
| 7984 |
|
| 7985 |
if (rtp->themssrc_valid && rtp->themssrc != ssrc) { |
| 7986 |
struct ast_frame *f, srcupdate = { |
| 7987 |
AST_FRAME_CONTROL, |
| 7988 |
.subclass.integer = AST_CONTROL_SRCCHANGE, |
| 7989 |
}; |
| 7990 |
|
| 7991 |
if (!mark) { |
| 7992 |
if (ast_debug_rtp_packet_is_allowed) { |
| 7993 |
ast_debug(0, "(%p) RTP forcing Marker bit, because SSRC has changed\n", instance); |
| 7994 |
} |
| 7995 |
mark = 1; |
| 7996 |
} |
| 7997 |
|
| 7998 |
f = ast_frisolate(&srcupdate); |
| 7999 |
AST_LIST_INSERT_TAIL(&frames, f, frame_list); |
| 8000 |
|
| 8001 |
rtp->seedrxseqno = 0; |
| 8002 |
rtp->rxcount = 0; |
| 8003 |
rtp->rxoctetcount = 0; |
| 8004 |
rtp->cycles = 0; |
| 8005 |
prev_seqno = 0; |
| 8006 |
rtp->last_seqno = 0; |
| 8007 |
rtp->last_end_timestamp.ts = 0; |
| 8008 |
rtp->last_end_timestamp.is_set = 0; |
| 8009 |
if (rtp->rtcp) { |
| 8010 |
rtp->rtcp->expected_prior = 0; |
| 8011 |
rtp->rtcp->received_prior = 0; |
| 8012 |
} |
| 8013 |
} |
| 8014 |
|
| 8015 |
rtp->themssrc = ssrc; |
| 8016 |
rtp->themssrc_valid = 1; |
| 8017 |
} |
| 8018 |
|
| 8019 |
rtp->rxcount++; |
| 8020 |
rtp->rxoctetcount += (res - hdrlen); |
| 8021 |
if (rtp->rxcount == 1) { |
| 8022 |
rtp->seedrxseqno = seqno; |
| 8023 |
} |
| 8024 |
|
| 8025 |
|
| 8026 |
if (rtp->rtcp && !ast_sockaddr_isnull(&rtp->rtcp->them) && rtp->rtcp->schedid < 0) { |
| 8027 |
|
| 8028 |
ao2_ref(instance, +1); |
| 8029 |
rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, instance); |
| 8030 |
if (rtp->rtcp->schedid < 0) { |
| 8031 |
ao2_ref(instance, -1); |
| 8032 |
ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n"); |
| 8033 |
} |
| 8034 |
} |
| 8035 |
if ((int)prev_seqno - (int)seqno > 100) |
| 8036 |
rtp->cycles += RTP_SEQ_MOD; |
| 8037 |
|
| 8038 |
|
| 8039 |
|
| 8040 |
|
| 8041 |
|
| 8042 |
instance1 = ast_rtp_instance_get_bridged(instance); |
| 8043 |
if (instance1 |
| 8044 |
&& !bridge_p2p_rtp_write(instance, instance1, rtpheader, res, hdrlen)) { |
| 8045 |
struct timeval rxtime; |
| 8046 |
struct ast_frame *f; |
| 8047 |
|
| 8048 |
|
| 8049 |
calc_rxstamp_and_jitter(&rxtime, rtp, timestamp, mark); |
| 8050 |
|
| 8051 |
|
| 8052 |
|
| 8053 |
while ((f = AST_LIST_REMOVE_HEAD(&frames, frame_list)) != NULL) { |
| 8054 |
ast_frfree(f); |
| 8055 |
} |
| 8056 |
|
| 8057 |
return &ast_null_frame; |
| 8058 |
} |
| 8059 |
|
| 8060 |
payload = ast_rtp_codecs_get_payload(ast_rtp_instance_get_codecs(instance), payloadtype); |
| 8061 |
if (!payload) { |
| 8062 |
|
| 8063 |
return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame; |
| 8064 |
} |
| 8065 |
|
| 8066 |
|
| 8067 |
if (!payload->asterisk_format) { |
| 8068 |
struct ast_frame *f = NULL; |
| 8069 |
if (payload->rtp_code == AST_RTP_DTMF) { |
| 8070 |
|
| 8071 |
|
| 8072 |
|
| 8073 |
|
| 8074 |
process_dtmf_rfc2833(instance, read_area + hdrlen, res - hdrlen, seqno, timestamp, payloadtype, mark, &frames); |
| 8075 |
} else if (payload->rtp_code == AST_RTP_CISCO_DTMF) { |
| 8076 |
f = process_dtmf_cisco(instance, read_area + hdrlen, res - hdrlen, seqno, timestamp, payloadtype, mark); |
| 8077 |
} else if (payload->rtp_code == AST_RTP_CN) { |
| 8078 |
f = process_cn_rfc3389(instance, read_area + hdrlen, res - hdrlen, seqno, timestamp, payloadtype, mark); |
| 8079 |
} else { |
| 8080 |
ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n", |
| 8081 |
payloadtype, |
| 8082 |
ast_sockaddr_stringify(remote_address)); |
| 8083 |
} |
| 8084 |
|
| 8085 |
if (f) { |
| 8086 |
AST_LIST_INSERT_TAIL(&frames, f, frame_list); |
| 8087 |
} |
| 8088 |
|
| 8089 |
|
| 8090 |
|
| 8091 |
return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame; |
| 8092 |
} |
| 8093 |
|
| 8094 |
ao2_replace(rtp->lastrxformat, payload->format); |
| 8095 |
ao2_replace(rtp->f.subclass.format, payload->format); |
| 8096 |
switch (ast_format_get_type(rtp->f.subclass.format)) { |
| 8097 |
case AST_MEDIA_TYPE_AUDIO: |
| 8098 |
rtp->f.frametype = AST_FRAME_VOICE; |
| 8099 |
break; |
| 8100 |
case AST_MEDIA_TYPE_VIDEO: |
| 8101 |
rtp->f.frametype = AST_FRAME_VIDEO; |
| 8102 |
break; |
| 8103 |
case AST_MEDIA_TYPE_TEXT: |
| 8104 |
rtp->f.frametype = AST_FRAME_TEXT; |
| 8105 |
break; |
| 8106 |
case AST_MEDIA_TYPE_IMAGE: |
| 8107 |
|
| 8108 |
default: |
| 8109 |
ast_log(LOG_WARNING, "Unknown or unsupported media type: %s\n", |
| 8110 |
ast_codec_media_type2str(ast_format_get_type(rtp->f.subclass.format))); |
| 8111 |
return &ast_null_frame; |
| 8112 |
} |
| 8113 |
|
| 8114 |
if (rtp->dtmf_timeout && rtp->dtmf_timeout < timestamp) { |
| 8115 |
rtp->dtmf_timeout = 0; |
| 8116 |
|
| 8117 |
if (rtp->resp) { |
| 8118 |
struct ast_frame *f; |
| 8119 |
f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0); |
| 8120 |
f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, ast_rtp_get_rate(f->subclass.format)), ast_tv(0, 0)); |
| 8121 |
rtp->resp = 0; |
| 8122 |
rtp->dtmf_timeout = rtp->dtmf_duration = 0; |
| 8123 |
AST_LIST_INSERT_TAIL(&frames, f, frame_list); |
| 8124 |
return AST_LIST_FIRST(&frames); |
| 8125 |
} |
| 8126 |
} |
| 8127 |
|
| 8128 |
rtp->f.src = "RTP"; |
| 8129 |
rtp->f.mallocd = 0; |
| 8130 |
rtp->f.datalen = res - hdrlen; |
| 8131 |
rtp->f.data.ptr = read_area + hdrlen; |
| 8132 |
rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET; |
| 8133 |
ast_set_flag(&rtp->f, AST_FRFLAG_HAS_SEQUENCE_NUMBER); |
| 8134 |
rtp->f.seqno = seqno; |
| 8135 |
rtp->f.stream_num = rtp->stream_num; |
| 8136 |
|
| 8137 |
if ((ast_format_cmp(rtp->f.subclass.format, ast_format_t140) == AST_FORMAT_CMP_EQUAL) |
| 8138 |
&& ((int)seqno - (prev_seqno + 1) > 0) |
| 8139 |
&& ((int)seqno - (prev_seqno + 1) < 10)) { |
| 8140 |
unsigned char *data = rtp->f.data.ptr; |
| 8141 |
|
| 8142 |
memmove(rtp->f.data.ptr+3, rtp->f.data.ptr, rtp->f.datalen); |
| 8143 |
rtp->f.datalen +=3; |
| 8144 |
*data++ = 0xEF; |
| 8145 |
*data++ = 0xBF; |
| 8146 |
*data = 0xBD; |
| 8147 |
} |
| 8148 |
|
| 8149 |
if (ast_format_cmp(rtp->f.subclass.format, ast_format_t140_red) == AST_FORMAT_CMP_EQUAL) { |
| 8150 |
unsigned char *data = rtp->f.data.ptr; |
| 8151 |
unsigned char *header_end; |
| 8152 |
int num_generations; |
| 8153 |
int header_length; |
| 8154 |
int len; |
| 8155 |
int diff =(int)seqno - (prev_seqno+1); |
| 8156 |
int x; |
| 8157 |
|
| 8158 |
ao2_replace(rtp->f.subclass.format, ast_format_t140); |
| 8159 |
header_end = memchr(data, ((*data) & 0x7f), rtp->f.datalen); |
| 8160 |
if (header_end == NULL) { |
| 8161 |
return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame; |
| 8162 |
} |
| 8163 |
header_end++; |
| 8164 |
|
| 8165 |
header_length = header_end - data; |
| 8166 |
num_generations = header_length / 4; |
| 8167 |
len = header_length; |
| 8168 |
|
| 8169 |
if (!diff) { |
| 8170 |
for (x = 0; x < num_generations; x++) |
| 8171 |
len += data[x * 4 + 3]; |
| 8172 |
|
| 8173 |
if (!(rtp->f.datalen - len)) |
| 8174 |
return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame; |
| 8175 |
|
| 8176 |
rtp->f.data.ptr += len; |
| 8177 |
rtp->f.datalen -= len; |
| 8178 |
} else if (diff > num_generations && diff < 10) { |
| 8179 |
len -= 3; |
| 8180 |
rtp->f.data.ptr += len; |
| 8181 |
rtp->f.datalen -= len; |
| 8182 |
|
| 8183 |
data = rtp->f.data.ptr; |
| 8184 |
*data++ = 0xEF; |
| 8185 |
*data++ = 0xBF; |
| 8186 |
*data = 0xBD; |
| 8187 |
} else { |
| 8188 |
for ( x = 0; x < num_generations - diff; x++) |
| 8189 |
len += data[x * 4 + 3]; |
| 8190 |
|
| 8191 |
rtp->f.data.ptr += len; |
| 8192 |
rtp->f.datalen -= len; |
| 8193 |
} |
| 8194 |
} |
| 8195 |
|
| 8196 |
if (ast_format_get_type(rtp->f.subclass.format) == AST_MEDIA_TYPE_AUDIO) { |
| 8197 |
rtp->f.samples = ast_codec_samples_count(&rtp->f); |
| 8198 |
if (ast_format_cache_is_slinear(rtp->f.subclass.format)) { |
| 8199 |
ast_frame_byteswap_be(&rtp->f); |
| 8200 |
} |
| 8201 |
calc_rxstamp_and_jitter(&rtp->f.delivery, rtp, timestamp, mark); |
| 8202 |
|
| 8203 |
ast_set_flag(&rtp->f, AST_FRFLAG_HAS_TIMING_INFO); |
| 8204 |
rtp->f.ts = timestamp / (ast_rtp_get_rate(rtp->f.subclass.format) / 1000); |
| 8205 |
rtp->f.len = rtp->f.samples / ((ast_format_get_sample_rate(rtp->f.subclass.format) / 1000)); |
| 8206 |
} else if (ast_format_get_type(rtp->f.subclass.format) == AST_MEDIA_TYPE_VIDEO) { |
| 8207 |
|
| 8208 |
if (!rtp->lastividtimestamp) |
| 8209 |
rtp->lastividtimestamp = timestamp; |
| 8210 |
calc_rxstamp_and_jitter(&rtp->f.delivery, rtp, timestamp, mark); |
| 8211 |
ast_set_flag(&rtp->f, AST_FRFLAG_HAS_TIMING_INFO); |
| 8212 |
rtp->f.ts = timestamp / (ast_rtp_get_rate(rtp->f.subclass.format) / 1000); |
| 8213 |
rtp->f.samples = timestamp - rtp->lastividtimestamp; |
| 8214 |
rtp->lastividtimestamp = timestamp; |
| 8215 |
rtp->f.delivery.tv_sec = 0; |
| 8216 |
rtp->f.delivery.tv_usec = 0; |
| 8217 |
|
| 8218 |
rtp->f.subclass.frame_ending = mark ? 1 : 0; |
| 8219 |
} else if (ast_format_get_type(rtp->f.subclass.format) == AST_MEDIA_TYPE_TEXT) { |
| 8220 |
|
| 8221 |
if (!rtp->lastitexttimestamp) |
| 8222 |
rtp->lastitexttimestamp = timestamp; |
| 8223 |
rtp->f.samples = timestamp - rtp->lastitexttimestamp; |
| 8224 |
rtp->lastitexttimestamp = timestamp; |
| 8225 |
rtp->f.delivery.tv_sec = 0; |
| 8226 |
rtp->f.delivery.tv_usec = 0; |
| 8227 |
} else { |
| 8228 |
ast_log(LOG_WARNING, "Unknown or unsupported media type: %s\n", |
| 8229 |
ast_codec_media_type2str(ast_format_get_type(rtp->f.subclass.format))); |
| 8230 |
return &ast_null_frame; |
| 8231 |
} |
| 8232 |
|
| 8233 |
AST_LIST_INSERT_TAIL(&frames, &rtp->f, frame_list); |
| 8234 |
return AST_LIST_FIRST(&frames); |
| 8235 |
} |
| 8236 |
|
| 8237 |
#ifdef AST_DEVMODE |
| 8238 |
|
| 8239 |
struct rtp_drop_packets_data { |
| 8240 |
|
| 8241 |
unsigned int use_random_num; |
| 8242 |
|
| 8243 |
unsigned int use_random_interval; |
| 8244 |
|
| 8245 |
|
| 8246 |
unsigned int num_to_drop; |
| 8247 |
|
| 8248 |
unsigned int num_dropped; |
| 8249 |
|
| 8250 |
|
| 8251 |
struct timeval interval; |
| 8252 |
|
| 8253 |
struct timeval next; |
| 8254 |
|
| 8255 |
struct ast_sockaddr addr; |
| 8256 |
|
| 8257 |
unsigned int port; |
| 8258 |
}; |
| 8259 |
|
| 8260 |
static struct rtp_drop_packets_data drop_packets_data; |
| 8261 |
|
| 8262 |
static void drop_packets_data_update(struct timeval tv) |
| 8263 |
{ |
| 8264 |
|
| 8265 |
|
| 8266 |
|
| 8267 |
|
| 8268 |
|
| 8269 |
|
| 8270 |
|
| 8271 |
drop_packets_data.num_dropped = drop_packets_data.use_random_num ? |
| 8272 |
ast_random() % drop_packets_data.num_to_drop : 0; |
| 8273 |
|
| 8274 |
|
| 8275 |
|
| 8276 |
|
| 8277 |
|
| 8278 |
|
| 8279 |
|
| 8280 |
if (drop_packets_data.use_random_interval) { |
| 8281 |
|
| 8282 |
struct timeval interval = ast_time_create_by_unit(ast_time_tv_to_usec( |
| 8283 |
&drop_packets_data.interval) * ((double)(ast_random() % 100 + 1) / 100), |
| 8284 |
TIME_UNIT_MICROSECOND); |
| 8285 |
|
| 8286 |
drop_packets_data.next = ast_tvadd(tv, interval); |
| 8287 |
} else { |
| 8288 |
drop_packets_data.next = ast_tvadd(tv, drop_packets_data.interval); |
| 8289 |
} |
| 8290 |
} |
| 8291 |
|
| 8292 |
static int should_drop_packets(struct ast_sockaddr *addr) |
| 8293 |
{ |
| 8294 |
struct timeval tv; |
| 8295 |
|
| 8296 |
if (!drop_packets_data.num_to_drop) { |
| 8297 |
return 0; |
| 8298 |
} |
| 8299 |
|
| 8300 |
|
| 8301 |
|
| 8302 |
|
| 8303 |
|
| 8304 |
if (!ast_sockaddr_isnull(&drop_packets_data.addr) && |
| 8305 |
(drop_packets_data.port ? |
| 8306 |
ast_sockaddr_cmp(&drop_packets_data.addr, addr) : |
| 8307 |
ast_sockaddr_cmp_addr(&drop_packets_data.addr, addr)) != 0) { |
| 8308 |
|
| 8309 |
return 0; |
| 8310 |
} |
| 8311 |
|
| 8312 |
|
| 8313 |
if (drop_packets_data.num_dropped < drop_packets_data.num_to_drop) { |
| 8314 |
++drop_packets_data.num_dropped; |
| 8315 |
return 1; |
| 8316 |
} |
| 8317 |
|
| 8318 |
|
| 8319 |
|
| 8320 |
|
| 8321 |
|
| 8322 |
|
| 8323 |
if (ast_tvzero(drop_packets_data.interval)) { |
| 8324 |
|
| 8325 |
drop_packets_data.num_to_drop = 0; |
| 8326 |
return 0; |
| 8327 |
} |
| 8328 |
|
| 8329 |
tv = ast_tvnow(); |
| 8330 |
if (ast_tvcmp(tv, drop_packets_data.next) == -1) { |
| 8331 |
|
| 8332 |
return 0; |
| 8333 |
} |
| 8334 |
|
| 8335 |
|
| 8336 |
|
| 8337 |
|
| 8338 |
|
| 8339 |
|
| 8340 |
drop_packets_data_update(tv); |
| 8341 |
return 1; |
| 8342 |
} |
| 8343 |
|
| 8344 |
#endif |
| 8345 |
|
| 8346 |
|
| 8347 |
static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp) |
| 8348 |
{ |
| 8349 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 8350 |
struct ast_srtp *srtp; |
| 8351 |
RAII_VAR(struct ast_rtp_instance *, child, NULL, rtp_instance_unlock); |
| 8352 |
struct ast_sockaddr addr; |
| 8353 |
int res, hdrlen = 12, version, payloadtype; |
| 8354 |
unsigned char *read_area = rtp->rawdata + AST_FRIENDLY_OFFSET; |
| 8355 |
size_t read_area_size = sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET; |
| 8356 |
unsigned int *rtpheader = (unsigned int*)(read_area), seqno, ssrc, timestamp, prev_seqno; |
| 8357 |
struct ast_sockaddr remote_address = { {0,} }; |
| 8358 |
struct frame_list frames; |
| 8359 |
struct ast_frame *frame; |
| 8360 |
unsigned int bundled; |
| 8361 |
|
| 8362 |
|
| 8363 |
if (rtcp) { |
| 8364 |
if (rtp->rtcp && rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) { |
| 8365 |
return ast_rtcp_read(instance); |
| 8366 |
} |
| 8367 |
return &ast_null_frame; |
| 8368 |
} |
| 8369 |
|
| 8370 |
|
| 8371 |
if ((res = rtp_recvfrom(instance, read_area, read_area_size, 0, |
| 8372 |
&addr)) < 0) { |
| 8373 |
if (res == RTP_DTLS_ESTABLISHED) { |
| 8374 |
rtp->f.frametype = AST_FRAME_CONTROL; |
| 8375 |
rtp->f.subclass.integer = AST_CONTROL_SRCCHANGE; |
| 8376 |
return &rtp->f; |
| 8377 |
} |
| 8378 |
|
| 8379 |
ast_assert(errno != EBADF); |
| 8380 |
if (errno != EAGAIN) { |
| 8381 |
ast_log(LOG_WARNING, "RTP Read error: %s. Hanging up.\n", |
| 8382 |
(errno) ? strerror(errno) : "Unspecified"); |
| 8383 |
return NULL; |
| 8384 |
} |
| 8385 |
return &ast_null_frame; |
| 8386 |
} |
| 8387 |
|
| 8388 |
|
| 8389 |
if (!res) { |
| 8390 |
return &ast_null_frame; |
| 8391 |
} |
| 8392 |
|
| 8393 |
|
| 8394 |
if (rtcp_mux(rtp, read_area)) { |
| 8395 |
return ast_rtcp_interpret(instance, ast_rtp_instance_get_srtp(instance, 1), read_area, res, &addr); |
| 8396 |
} |
| 8397 |
|
| 8398 |
|
| 8399 |
if (res < hdrlen) { |
| 8400 |
|
| 8401 |
int i; |
| 8402 |
for (i = 0; i < res; ++i) { |
| 8403 |
if (read_area[i] != '\0') { |
| 8404 |
ast_log(LOG_WARNING, "RTP Read too short\n"); |
| 8405 |
return &ast_null_frame; |
| 8406 |
} |
| 8407 |
} |
| 8408 |
return &ast_null_frame; |
| 8409 |
} |
| 8410 |
|
| 8411 |
|
| 8412 |
seqno = ntohl(rtpheader[0]); |
| 8413 |
|
| 8414 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 8415 |
|
| 8416 |
if (!(version = (seqno & 0xC0000000) >> 30)) { |
| 8417 |
struct sockaddr_in addr_tmp; |
| 8418 |
struct ast_sockaddr addr_v4; |
| 8419 |
if (ast_sockaddr_is_ipv4(&addr)) { |
| 8420 |
ast_sockaddr_to_sin(&addr, &addr_tmp); |
| 8421 |
} else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) { |
| 8422 |
ast_debug_stun(1, "(%p) STUN using IPv6 mapped address %s\n", |
| 8423 |
instance, ast_sockaddr_stringify(&addr)); |
| 8424 |
ast_sockaddr_to_sin(&addr_v4, &addr_tmp); |
| 8425 |
} else { |
| 8426 |
ast_debug_stun(1, "(%p) STUN cannot do for non IPv4 address %s\n", |
| 8427 |
instance, ast_sockaddr_stringify(&addr)); |
| 8428 |
return &ast_null_frame; |
| 8429 |
} |
| 8430 |
if ((ast_stun_handle_packet(rtp->s, &addr_tmp, read_area, res, NULL, NULL) == AST_STUN_ACCEPT) && |
| 8431 |
ast_sockaddr_isnull(&remote_address)) { |
| 8432 |
ast_sockaddr_from_sin(&addr, &addr_tmp); |
| 8433 |
ast_rtp_instance_set_remote_address(instance, &addr); |
| 8434 |
} |
| 8435 |
return &ast_null_frame; |
| 8436 |
} |
| 8437 |
|
| 8438 |
|
| 8439 |
if (version != 2) { |
| 8440 |
return &ast_null_frame; |
| 8441 |
} |
| 8442 |
|
| 8443 |
|
| 8444 |
ssrc = ntohl(rtpheader[2]); |
| 8445 |
|
| 8446 |
|
| 8447 |
srtp = ast_rtp_instance_get_srtp(instance, 0); |
| 8448 |
|
| 8449 |
|
| 8450 |
child = rtp_find_instance_by_packet_source_ssrc(instance, rtp, ssrc); |
| 8451 |
if (!child) { |
| 8452 |
|
| 8453 |
return &ast_null_frame; |
| 8454 |
} |
| 8455 |
if (child != instance) { |
| 8456 |
|
| 8457 |
|
| 8458 |
|
| 8459 |
ao2_lock(child); |
| 8460 |
instance = child; |
| 8461 |
rtp = ast_rtp_instance_get_data(instance); |
| 8462 |
} else { |
| 8463 |
|
| 8464 |
child = NULL; |
| 8465 |
} |
| 8466 |
|
| 8467 |
|
| 8468 |
switch (rtp->strict_rtp_state) { |
| 8469 |
case STRICT_RTP_LEARN: |
| 8470 |
|
| 8471 |
|
| 8472 |
|
| 8473 |
|
| 8474 |
|
| 8475 |
|
| 8476 |
|
| 8477 |
|
| 8478 |
|
| 8479 |
|
| 8480 |
|
| 8481 |
|
| 8482 |
|
| 8483 |
|
| 8484 |
|
| 8485 |
|
| 8486 |
|
| 8487 |
|
| 8488 |
|
| 8489 |
|
| 8490 |
|
| 8491 |
|
| 8492 |
|
| 8493 |
|
| 8494 |
|
| 8495 |
|
| 8496 |
|
| 8497 |
|
| 8498 |
|
| 8499 |
|
| 8500 |
|
| 8501 |
|
| 8502 |
|
| 8503 |
|
| 8504 |
|
| 8505 |
|
| 8506 |
|
| 8507 |
|
| 8508 |
|
| 8509 |
|
| 8510 |
|
| 8511 |
if (!ast_sockaddr_isnull(&rtp->strict_rtp_address) |
| 8512 |
&& STRICT_RTP_LEARN_TIMEOUT < ast_tvdiff_ms(ast_tvnow(), rtp->rtp_source_learn.start)) { |
| 8513 |
ast_verb(4, "%p -- Strict RTP learning complete - Locking on source address %s\n", |
| 8514 |
rtp, ast_sockaddr_stringify(&rtp->strict_rtp_address)); |
| 8515 |
ast_test_suite_event_notify("STRICT_RTP_LEARN", "Source: %s", |
| 8516 |
ast_sockaddr_stringify(&rtp->strict_rtp_address)); |
| 8517 |
rtp->strict_rtp_state = STRICT_RTP_CLOSED; |
| 8518 |
} else { |
| 8519 |
struct ast_sockaddr target_address; |
| 8520 |
|
| 8521 |
if (!ast_sockaddr_cmp(&rtp->strict_rtp_address, &addr)) { |
| 8522 |
|
| 8523 |
|
| 8524 |
|
| 8525 |
|
| 8526 |
|
| 8527 |
|
| 8528 |
|
| 8529 |
rtp_learning_seq_init(&rtp->rtp_source_learn, seqno); |
| 8530 |
break; |
| 8531 |
} |
| 8532 |
|
| 8533 |
|
| 8534 |
|
| 8535 |
|
| 8536 |
|
| 8537 |
|
| 8538 |
|
| 8539 |
ast_rtp_instance_get_requested_target_address(instance, &target_address); |
| 8540 |
if (!ast_sockaddr_cmp(&target_address, &addr)) { |
| 8541 |
|
| 8542 |
ast_verb(4, "%p -- Strict RTP switching to RTP target address %s as source\n", |
| 8543 |
rtp, ast_sockaddr_stringify(&addr)); |
| 8544 |
ast_sockaddr_copy(&rtp->strict_rtp_address, &addr); |
| 8545 |
rtp_learning_seq_init(&rtp->rtp_source_learn, seqno); |
| 8546 |
break; |
| 8547 |
} |
| 8548 |
|
| 8549 |
|
| 8550 |
|
| 8551 |
|
| 8552 |
|
| 8553 |
|
| 8554 |
if (!ast_sockaddr_cmp(&rtp->rtp_source_learn.proposed_address, &addr)) { |
| 8555 |
if (rtp->rtp_source_learn.stream_type == AST_MEDIA_TYPE_UNKNOWN) { |
| 8556 |
struct ast_rtp_codecs *codecs; |
| 8557 |
|
| 8558 |
codecs = ast_rtp_instance_get_codecs(instance); |
| 8559 |
rtp->rtp_source_learn.stream_type = |
| 8560 |
ast_rtp_codecs_get_stream_type(codecs); |
| 8561 |
ast_verb(4, "%p -- Strict RTP qualifying stream type: %s\n", |
| 8562 |
rtp, ast_codec_media_type2str(rtp->rtp_source_learn.stream_type)); |
| 8563 |
} |
| 8564 |
if (!rtp_learning_rtp_seq_update(&rtp->rtp_source_learn, seqno)) { |
| 8565 |
|
| 8566 |
ast_verb(4, "%p -- Strict RTP switching source address to %s\n", |
| 8567 |
rtp, ast_sockaddr_stringify(&addr)); |
| 8568 |
ast_sockaddr_copy(&rtp->strict_rtp_address, &addr); |
| 8569 |
rtp_learning_seq_init(&rtp->rtp_source_learn, seqno); |
| 8570 |
break; |
| 8571 |
} |
| 8572 |
|
| 8573 |
ast_debug_rtp(1, "(%p) RTP %p -- Received packet from %s, dropping due to strict RTP protection. Will switch to it in %d packets.\n", |
| 8574 |
instance, rtp, ast_sockaddr_stringify(&addr), rtp->rtp_source_learn.packets); |
| 8575 |
} else { |
| 8576 |
|
| 8577 |
|
| 8578 |
|
| 8579 |
|
| 8580 |
ast_sockaddr_copy(&rtp->rtp_source_learn.proposed_address, &addr); |
| 8581 |
rtp_learning_seq_init(&rtp->rtp_source_learn, seqno); |
| 8582 |
ast_debug_rtp(1, "(%p) RTP %p -- Received packet from %s, dropping due to strict RTP protection. Qualifying new stream.\n", |
| 8583 |
instance, rtp, ast_sockaddr_stringify(&addr)); |
| 8584 |
} |
| 8585 |
return &ast_null_frame; |
| 8586 |
} |
| 8587 |
|
| 8588 |
case STRICT_RTP_CLOSED: |
| 8589 |
|
| 8590 |
|
| 8591 |
|
| 8592 |
|
| 8593 |
|
| 8594 |
|
| 8595 |
|
| 8596 |
|
| 8597 |
|
| 8598 |
if (!ast_sockaddr_cmp(&rtp->strict_rtp_address, &addr)) { |
| 8599 |
break; |
| 8600 |
} |
| 8601 |
ast_debug_rtp(1, "(%p) RTP %p -- Received packet from %s, dropping due to strict RTP protection.\n", |
| 8602 |
instance, rtp, ast_sockaddr_stringify(&addr)); |
| 8603 |
#ifdef TEST_FRAMEWORK |
| 8604 |
{ |
| 8605 |
static int strict_rtp_test_event = 1; |
| 8606 |
if (strict_rtp_test_event) { |
| 8607 |
ast_test_suite_event_notify("STRICT_RTP_CLOSED", "Source: %s", |
| 8608 |
ast_sockaddr_stringify(&addr)); |
| 8609 |
strict_rtp_test_event = 0; |
| 8610 |
} |
| 8611 |
} |
| 8612 |
#endif |
| 8613 |
return &ast_null_frame; |
| 8614 |
case STRICT_RTP_OPEN: |
| 8615 |
break; |
| 8616 |
} |
| 8617 |
|
| 8618 |
|
| 8619 |
if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT)) { |
| 8620 |
if (ast_sockaddr_cmp(&remote_address, &addr)) { |
| 8621 |
|
| 8622 |
ast_rtp_instance_set_incoming_source_address(instance, &addr); |
| 8623 |
ast_sockaddr_copy(&remote_address, &addr); |
| 8624 |
if (rtp->rtcp && rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) { |
| 8625 |
ast_sockaddr_copy(&rtp->rtcp->them, &addr); |
| 8626 |
ast_sockaddr_set_port(&rtp->rtcp->them, ast_sockaddr_port(&addr) + 1); |
| 8627 |
} |
| 8628 |
ast_set_flag(rtp, FLAG_NAT_ACTIVE); |
| 8629 |
if (ast_debug_rtp_packet_is_allowed) |
| 8630 |
ast_debug(0, "(%p) RTP NAT: Got audio from other end. Now sending to address %s\n", |
| 8631 |
instance, ast_sockaddr_stringify(&remote_address)); |
| 8632 |
} |
| 8633 |
} |
| 8634 |
|
| 8635 |
|
| 8636 |
payloadtype = (seqno & 0x7f0000) >> 16; |
| 8637 |
seqno &= 0xffff; |
| 8638 |
timestamp = ntohl(rtpheader[1]); |
| 8639 |
|
| 8640 |
#ifdef AST_DEVMODE |
| 8641 |
if (should_drop_packets(&addr)) { |
| 8642 |
ast_debug(0, "(%p) RTP: drop received packet from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d)\n", |
| 8643 |
instance, ast_sockaddr_stringify(&addr), payloadtype, seqno, timestamp, res - hdrlen); |
| 8644 |
return &ast_null_frame; |
| 8645 |
} |
| 8646 |
#endif |
| 8647 |
|
| 8648 |
if (rtp_debug_test_addr(&addr)) { |
| 8649 |
ast_verbose("Got RTP packet from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d)\n", |
| 8650 |
ast_sockaddr_stringify(&addr), |
| 8651 |
payloadtype, seqno, timestamp, res - hdrlen); |
| 8652 |
} |
| 8653 |
|
| 8654 |
AST_LIST_HEAD_INIT_NOLOCK(&frames); |
| 8655 |
|
| 8656 |
bundled = (child || AST_VECTOR_SIZE(&rtp->ssrc_mapping)) ? 1 : 0; |
| 8657 |
|
| 8658 |
prev_seqno = rtp->lastrxseqno; |
| 8659 |
|
| 8660 |
rtp->prevrxseqno = rtp->lastrxseqno; |
| 8661 |
rtp->lastrxseqno = seqno; |
| 8662 |
|
| 8663 |
if (!rtp->recv_buffer) { |
| 8664 |
|
| 8665 |
frame = ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled); |
| 8666 |
AST_LIST_INSERT_TAIL(&frames, frame, frame_list); |
| 8667 |
return AST_LIST_FIRST(&frames); |
| 8668 |
} else if (rtp->expectedrxseqno == -1 || seqno == rtp->expectedrxseqno) { |
| 8669 |
rtp->expectedrxseqno = seqno + 1; |
| 8670 |
|
| 8671 |
|
| 8672 |
if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) { |
| 8673 |
rtp->expectedrxseqno = 0; |
| 8674 |
} |
| 8675 |
|
| 8676 |
|
| 8677 |
|
| 8678 |
|
| 8679 |
if (!ast_data_buffer_count(rtp->recv_buffer)) { |
| 8680 |
frame = ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled); |
| 8681 |
AST_LIST_INSERT_TAIL(&frames, frame, frame_list); |
| 8682 |
return AST_LIST_FIRST(&frames); |
| 8683 |
} |
| 8684 |
|
| 8685 |
if (!AST_VECTOR_REMOVE_CMP_ORDERED(&rtp->missing_seqno, seqno, find_by_value, |
| 8686 |
AST_VECTOR_ELEM_CLEANUP_NOOP)) { |
| 8687 |
ast_debug_rtp(2, "(%p) RTP Packet with sequence number '%d' on instance is no longer missing\n", |
| 8688 |
instance, seqno); |
| 8689 |
} |
| 8690 |
|
| 8691 |
|
| 8692 |
|
| 8693 |
|
| 8694 |
if (!ast_data_buffer_get(rtp->recv_buffer, rtp->expectedrxseqno)) { |
| 8695 |
frame = ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled); |
| 8696 |
AST_LIST_INSERT_TAIL(&frames, frame, frame_list); |
| 8697 |
return AST_LIST_FIRST(&frames); |
| 8698 |
} |
| 8699 |
|
| 8700 |
|
| 8701 |
|
| 8702 |
|
| 8703 |
|
| 8704 |
|
| 8705 |
frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled)); |
| 8706 |
if (frame) { |
| 8707 |
AST_LIST_INSERT_TAIL(&frames, frame, frame_list); |
| 8708 |
prev_seqno = seqno; |
| 8709 |
} |
| 8710 |
|
| 8711 |
|
| 8712 |
while (ast_data_buffer_count(rtp->recv_buffer)) { |
| 8713 |
struct ast_rtp_rtcp_nack_payload *payload; |
| 8714 |
|
| 8715 |
payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_remove(rtp->recv_buffer, rtp->expectedrxseqno); |
| 8716 |
if (!payload) { |
| 8717 |
break; |
| 8718 |
} |
| 8719 |
|
| 8720 |
frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, payload->buf, payload->size, prev_seqno, bundled)); |
| 8721 |
ast_free(payload); |
| 8722 |
|
| 8723 |
if (!frame) { |
| 8724 |
|
| 8725 |
|
| 8726 |
|
| 8727 |
return AST_LIST_FIRST(&frames); |
| 8728 |
} |
| 8729 |
|
| 8730 |
ast_debug_rtp(2, "(%p) RTP pulled buffered packet with sequence number '%d' to additionally return\n", |
| 8731 |
instance, frame->seqno); |
| 8732 |
AST_LIST_INSERT_TAIL(&frames, frame, frame_list); |
| 8733 |
prev_seqno = rtp->expectedrxseqno; |
| 8734 |
rtp->expectedrxseqno++; |
| 8735 |
if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) { |
| 8736 |
rtp->expectedrxseqno = 0; |
| 8737 |
} |
| 8738 |
} |
| 8739 |
|
| 8740 |
return AST_LIST_FIRST(&frames); |
| 8741 |
} else if ((((seqno - rtp->expectedrxseqno) > 100) && timestamp > rtp->lastividtimestamp) || |
| 8742 |
ast_data_buffer_count(rtp->recv_buffer) == ast_data_buffer_max(rtp->recv_buffer)) { |
| 8743 |
int inserted = 0; |
| 8744 |
|
| 8745 |
|
| 8746 |
|
| 8747 |
|
| 8748 |
|
| 8749 |
|
| 8750 |
|
| 8751 |
if (rtp->rtp_source_learn.stream_type == AST_MEDIA_TYPE_VIDEO) { |
| 8752 |
ast_debug_rtp(2, "(%p) RTP source has wild gap or packet loss, sending FIR\n", |
| 8753 |
instance); |
| 8754 |
rtp_write_rtcp_fir(instance, rtp, &remote_address); |
| 8755 |
} |
| 8756 |
|
| 8757 |
|
| 8758 |
|
| 8759 |
|
| 8760 |
|
| 8761 |
while (ast_data_buffer_count(rtp->recv_buffer)) { |
| 8762 |
struct ast_rtp_rtcp_nack_payload *payload; |
| 8763 |
|
| 8764 |
|
| 8765 |
if (rtp->expectedrxseqno == seqno) { |
| 8766 |
frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled)); |
| 8767 |
if (frame) { |
| 8768 |
AST_LIST_INSERT_TAIL(&frames, frame, frame_list); |
| 8769 |
prev_seqno = seqno; |
| 8770 |
ast_debug_rtp(2, "(%p) RTP inserted just received packet with sequence number '%d' in correct order\n", |
| 8771 |
instance, seqno); |
| 8772 |
} |
| 8773 |
|
| 8774 |
|
| 8775 |
|
| 8776 |
|
| 8777 |
payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_remove(rtp->recv_buffer, seqno); |
| 8778 |
if (payload) { |
| 8779 |
ast_free(payload); |
| 8780 |
} |
| 8781 |
rtp->expectedrxseqno++; |
| 8782 |
if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) { |
| 8783 |
rtp->expectedrxseqno = 0; |
| 8784 |
} |
| 8785 |
inserted = 1; |
| 8786 |
continue; |
| 8787 |
} |
| 8788 |
|
| 8789 |
payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_remove(rtp->recv_buffer, rtp->expectedrxseqno); |
| 8790 |
if (payload) { |
| 8791 |
frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, payload->buf, payload->size, prev_seqno, bundled)); |
| 8792 |
if (frame) { |
| 8793 |
AST_LIST_INSERT_TAIL(&frames, frame, frame_list); |
| 8794 |
prev_seqno = rtp->expectedrxseqno; |
| 8795 |
ast_debug_rtp(2, "(%p) RTP emptying queue and returning packet with sequence number '%d'\n", |
| 8796 |
instance, frame->seqno); |
| 8797 |
} |
| 8798 |
ast_free(payload); |
| 8799 |
} |
| 8800 |
|
| 8801 |
rtp->expectedrxseqno++; |
| 8802 |
if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) { |
| 8803 |
rtp->expectedrxseqno = 0; |
| 8804 |
} |
| 8805 |
} |
| 8806 |
|
| 8807 |
if (!inserted) { |
| 8808 |
|
| 8809 |
|
| 8810 |
|
| 8811 |
|
| 8812 |
|
| 8813 |
frame = ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled); |
| 8814 |
AST_LIST_INSERT_TAIL(&frames, frame, frame_list); |
| 8815 |
rtp->expectedrxseqno = seqno + 1; |
| 8816 |
if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) { |
| 8817 |
rtp->expectedrxseqno = 0; |
| 8818 |
} |
| 8819 |
|
| 8820 |
ast_debug_rtp(2, "(%p) RTP adding just received packet with sequence number '%d' to end of dumped queue\n", |
| 8821 |
instance, seqno); |
| 8822 |
} |
| 8823 |
|
| 8824 |
|
| 8825 |
|
| 8826 |
|
| 8827 |
ast_data_buffer_resize(rtp->recv_buffer, MIN(MAXIMUM_RTP_RECV_BUFFER_SIZE, |
| 8828 |
ast_data_buffer_max(rtp->recv_buffer) + AST_VECTOR_SIZE(&rtp->missing_seqno))); |
| 8829 |
ast_debug_rtp(2, "(%p) RTP receive buffer is now at maximum of %zu\n", instance, ast_data_buffer_max(rtp->recv_buffer)); |
| 8830 |
|
| 8831 |
|
| 8832 |
|
| 8833 |
|
| 8834 |
AST_VECTOR_RESET(&rtp->missing_seqno, AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 8835 |
|
| 8836 |
return AST_LIST_FIRST(&frames); |
| 8837 |
} |
| 8838 |
|
| 8839 |
|
| 8840 |
ast_frame_free(AST_LIST_FIRST(&frames), 0); |
| 8841 |
|
| 8842 |
|
| 8843 |
|
| 8844 |
|
| 8845 |
|
| 8846 |
|
| 8847 |
|
| 8848 |
|
| 8849 |
|
| 8850 |
|
| 8851 |
|
| 8852 |
if ((seqno < rtp->expectedrxseqno && ((rtp->expectedrxseqno - seqno) <= OLD_PACKET_COUNT)) || |
| 8853 |
(seqno > rtp->expectedrxseqno && (seqno >= (65535 - OLD_PACKET_COUNT + rtp->expectedrxseqno)))) { |
| 8854 |
|
| 8855 |
ast_debug_rtp(2, "(%p) RTP received an old packet with sequence number '%d', dropping it\n", |
| 8856 |
instance, seqno); |
| 8857 |
return &ast_null_frame; |
| 8858 |
} else if (ast_data_buffer_get(rtp->recv_buffer, seqno)) { |
| 8859 |
|
| 8860 |
ast_debug_rtp(2, "(%p) RTP received a duplicate transmission of packet with sequence number '%d', dropping it\n", |
| 8861 |
instance, seqno); |
| 8862 |
return &ast_null_frame; |
| 8863 |
} else { |
| 8864 |
|
| 8865 |
struct ast_rtp_rtcp_nack_payload *payload; |
| 8866 |
int missing_seqno; |
| 8867 |
int remove_failed; |
| 8868 |
unsigned int missing_seqnos_added = 0; |
| 8869 |
|
| 8870 |
ast_debug_rtp(2, "(%p) RTP received an out of order packet with sequence number '%d' while expecting '%d' from the future\n", |
| 8871 |
instance, seqno, rtp->expectedrxseqno); |
| 8872 |
|
| 8873 |
payload = ast_malloc(sizeof(*payload) + res); |
| 8874 |
if (!payload) { |
| 8875 |
|
| 8876 |
|
| 8877 |
|
| 8878 |
|
| 8879 |
|
| 8880 |
|
| 8881 |
return &ast_null_frame; |
| 8882 |
} |
| 8883 |
|
| 8884 |
payload->size = res; |
| 8885 |
memcpy(payload->buf, rtpheader, res); |
| 8886 |
if (ast_data_buffer_put(rtp->recv_buffer, seqno, payload) == -1) { |
| 8887 |
ast_free(payload); |
| 8888 |
} |
| 8889 |
|
| 8890 |
|
| 8891 |
|
| 8892 |
|
| 8893 |
|
| 8894 |
|
| 8895 |
remove_failed = AST_VECTOR_REMOVE_CMP_ORDERED(&rtp->missing_seqno, seqno, find_by_value, |
| 8896 |
AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 8897 |
if (!remove_failed) { |
| 8898 |
ast_debug_rtp(2, "(%p) RTP packet with sequence number '%d' is no longer missing\n", |
| 8899 |
instance, seqno); |
| 8900 |
} |
| 8901 |
|
| 8902 |
|
| 8903 |
|
| 8904 |
|
| 8905 |
|
| 8906 |
|
| 8907 |
missing_seqno = seqno; |
| 8908 |
while (remove_failed) { |
| 8909 |
missing_seqno -= 1; |
| 8910 |
|
| 8911 |
|
| 8912 |
if (missing_seqno < 0) { |
| 8913 |
missing_seqno = 65535; |
| 8914 |
} |
| 8915 |
|
| 8916 |
|
| 8917 |
if (missing_seqno == prev_seqno) { |
| 8918 |
break; |
| 8919 |
} |
| 8920 |
|
| 8921 |
|
| 8922 |
|
| 8923 |
|
| 8924 |
|
| 8925 |
|
| 8926 |
|
| 8927 |
|
| 8928 |
|
| 8929 |
|
| 8930 |
|
| 8931 |
|
| 8932 |
|
| 8933 |
if (AST_VECTOR_GET_CMP(&rtp->missing_seqno, missing_seqno, |
| 8934 |
find_by_value)) { |
| 8935 |
continue; |
| 8936 |
} |
| 8937 |
|
| 8938 |
|
| 8939 |
|
| 8940 |
|
| 8941 |
if (ast_data_buffer_get(rtp->recv_buffer, missing_seqno)) { |
| 8942 |
continue; |
| 8943 |
} |
| 8944 |
|
| 8945 |
ast_debug_rtp(2, "(%p) RTP added missing sequence number '%d'\n", |
| 8946 |
instance, missing_seqno); |
| 8947 |
AST_VECTOR_ADD_SORTED(&rtp->missing_seqno, missing_seqno, |
| 8948 |
compare_by_value); |
| 8949 |
missing_seqnos_added++; |
| 8950 |
} |
| 8951 |
|
| 8952 |
|
| 8953 |
|
| 8954 |
|
| 8955 |
|
| 8956 |
|
| 8957 |
|
| 8958 |
if (missing_seqnos_added >= MISSING_SEQNOS_ADDED_TRIGGER || |
| 8959 |
ast_data_buffer_count(rtp->recv_buffer) == ast_data_buffer_max(rtp->recv_buffer) / 4 || |
| 8960 |
ast_data_buffer_count(rtp->recv_buffer) >= ast_data_buffer_max(rtp->recv_buffer) / 2) { |
| 8961 |
int packet_len = 0; |
| 8962 |
int res = 0; |
| 8963 |
int ice; |
| 8964 |
int sr; |
| 8965 |
size_t data_size = AST_UUID_STR_LEN + 128 + (AST_VECTOR_SIZE(&rtp->missing_seqno) * 4); |
| 8966 |
RAII_VAR(unsigned char *, rtcpheader, NULL, ast_free_ptr); |
| 8967 |
RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report, |
| 8968 |
ast_rtp_rtcp_report_alloc(rtp->themssrc_valid ? 1 : 0), |
| 8969 |
ao2_cleanup); |
| 8970 |
|
| 8971 |
|
| 8972 |
|
| 8973 |
|
| 8974 |
rtcpheader = ast_malloc(sizeof(*rtcpheader) + data_size); |
| 8975 |
if (!rtcpheader) { |
| 8976 |
ast_debug_rtcp(1, "(%p) RTCP failed to allocate memory for NACK\n", instance); |
| 8977 |
return &ast_null_frame; |
| 8978 |
} |
| 8979 |
|
| 8980 |
memset(rtcpheader, 0, data_size); |
| 8981 |
|
| 8982 |
res = ast_rtcp_generate_compound_prefix(instance, rtcpheader, rtcp_report, &sr); |
| 8983 |
|
| 8984 |
if (res == 0 || res == 1) { |
| 8985 |
return &ast_null_frame; |
| 8986 |
} |
| 8987 |
|
| 8988 |
packet_len += res; |
| 8989 |
|
| 8990 |
res = ast_rtcp_generate_nack(instance, rtcpheader + packet_len); |
| 8991 |
|
| 8992 |
if (res == 0) { |
| 8993 |
ast_debug_rtcp(1, "(%p) RTCP failed to construct NACK, stopping here\n", instance); |
| 8994 |
return &ast_null_frame; |
| 8995 |
} |
| 8996 |
|
| 8997 |
packet_len += res; |
| 8998 |
|
| 8999 |
res = rtcp_sendto(instance, rtcpheader, packet_len, 0, &remote_address, &ice); |
| 9000 |
if (res < 0) { |
| 9001 |
ast_debug_rtcp(1, "(%p) RTCP failed to send NACK request out\n", instance); |
| 9002 |
} else { |
| 9003 |
ast_debug_rtcp(2, "(%p) RTCP sending a NACK request to get missing packets\n", instance); |
| 9004 |
|
| 9005 |
ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, remote_address, ice, sr); |
| 9006 |
} |
| 9007 |
} |
| 9008 |
} |
| 9009 |
|
| 9010 |
return &ast_null_frame; |
| 9011 |
} |
| 9012 |
|
| 9013 |
|
| 9014 |
static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value) |
| 9015 |
{ |
| 9016 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9017 |
|
| 9018 |
if (property == AST_RTP_PROPERTY_RTCP) { |
| 9019 |
if (value) { |
| 9020 |
struct ast_sockaddr local_addr; |
| 9021 |
|
| 9022 |
if (rtp->rtcp && rtp->rtcp->type == value) { |
| 9023 |
ast_debug_rtcp(1, "(%p) RTCP ignoring duplicate property\n", instance); |
| 9024 |
return; |
| 9025 |
} |
| 9026 |
|
| 9027 |
if (!rtp->rtcp) { |
| 9028 |
rtp->rtcp = ast_calloc(1, sizeof(*rtp->rtcp)); |
| 9029 |
if (!rtp->rtcp) { |
| 9030 |
return; |
| 9031 |
} |
| 9032 |
rtp->rtcp->s = -1; |
| 9033 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 9034 |
rtp->rtcp->dtls.timeout_timer = -1; |
| 9035 |
#endif |
| 9036 |
rtp->rtcp->schedid = -1; |
| 9037 |
} |
| 9038 |
|
| 9039 |
rtp->rtcp->type = value; |
| 9040 |
|
| 9041 |
|
| 9042 |
ast_rtp_instance_get_local_address(instance, &rtp->rtcp->us); |
| 9043 |
if (value == AST_RTP_INSTANCE_RTCP_STANDARD) { |
| 9044 |
ast_sockaddr_set_port(&rtp->rtcp->us, |
| 9045 |
ast_sockaddr_port(&rtp->rtcp->us) + 1); |
| 9046 |
} |
| 9047 |
|
| 9048 |
ast_sockaddr_copy(&local_addr, &rtp->rtcp->us); |
| 9049 |
if (!ast_find_ourip(&local_addr, &rtp->rtcp->us, 0)) { |
| 9050 |
ast_sockaddr_set_port(&local_addr, ast_sockaddr_port(&rtp->rtcp->us)); |
| 9051 |
} else { |
| 9052 |
|
| 9053 |
ast_sockaddr_copy(&local_addr, &rtp->rtcp->us); |
| 9054 |
} |
| 9055 |
|
| 9056 |
ast_free(rtp->rtcp->local_addr_str); |
| 9057 |
rtp->rtcp->local_addr_str = ast_strdup(ast_sockaddr_stringify(&local_addr)); |
| 9058 |
if (!rtp->rtcp->local_addr_str) { |
| 9059 |
ast_free(rtp->rtcp); |
| 9060 |
rtp->rtcp = NULL; |
| 9061 |
return; |
| 9062 |
} |
| 9063 |
|
| 9064 |
if (value == AST_RTP_INSTANCE_RTCP_STANDARD) { |
| 9065 |
|
| 9066 |
|
| 9067 |
|
| 9068 |
|
| 9069 |
if ((rtp->rtcp->s = create_new_socket("RTCP", &rtp->rtcp->us)) < 0) { |
| 9070 |
ast_debug_rtcp(1, "(%p) RTCP failed to create a new socket\n", instance); |
| 9071 |
ast_free(rtp->rtcp->local_addr_str); |
| 9072 |
ast_free(rtp->rtcp); |
| 9073 |
rtp->rtcp = NULL; |
| 9074 |
return; |
| 9075 |
} |
| 9076 |
|
| 9077 |
|
| 9078 |
if (ast_bind(rtp->rtcp->s, &rtp->rtcp->us)) { |
| 9079 |
ast_debug_rtcp(1, "(%p) RTCP failed to setup RTP instance\n", instance); |
| 9080 |
close(rtp->rtcp->s); |
| 9081 |
ast_free(rtp->rtcp->local_addr_str); |
| 9082 |
ast_free(rtp->rtcp); |
| 9083 |
rtp->rtcp = NULL; |
| 9084 |
return; |
| 9085 |
} |
| 9086 |
#ifdef HAVE_PJPROJECT |
| 9087 |
if (rtp->ice) { |
| 9088 |
rtp_add_candidates_to_ice(instance, rtp, &rtp->rtcp->us, ast_sockaddr_port(&rtp->rtcp->us), AST_RTP_ICE_COMPONENT_RTCP, TRANSPORT_SOCKET_RTCP); |
| 9089 |
} |
| 9090 |
#endif |
| 9091 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 9092 |
dtls_setup_rtcp(instance); |
| 9093 |
#endif |
| 9094 |
} else { |
| 9095 |
struct ast_sockaddr addr; |
| 9096 |
|
| 9097 |
|
| 9098 |
|
| 9099 |
|
| 9100 |
|
| 9101 |
|
| 9102 |
|
| 9103 |
|
| 9104 |
|
| 9105 |
if (rtp->rtcp->s > -1 && rtp->rtcp->s != rtp->s) { |
| 9106 |
close(rtp->rtcp->s); |
| 9107 |
} |
| 9108 |
rtp->rtcp->s = rtp->s; |
| 9109 |
ast_rtp_instance_get_remote_address(instance, &addr); |
| 9110 |
ast_sockaddr_copy(&rtp->rtcp->them, &addr); |
| 9111 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 9112 |
if (rtp->rtcp->dtls.ssl && rtp->rtcp->dtls.ssl != rtp->dtls.ssl) { |
| 9113 |
SSL_free(rtp->rtcp->dtls.ssl); |
| 9114 |
} |
| 9115 |
rtp->rtcp->dtls.ssl = rtp->dtls.ssl; |
| 9116 |
#endif |
| 9117 |
} |
| 9118 |
|
| 9119 |
ast_debug_rtcp(1, "(%s) RTCP setup on RTP instance\n", |
| 9120 |
ast_rtp_instance_get_channel_id(instance)); |
| 9121 |
} else { |
| 9122 |
if (rtp->rtcp) { |
| 9123 |
if (rtp->rtcp->schedid > -1) { |
| 9124 |
ao2_unlock(instance); |
| 9125 |
if (!ast_sched_del(rtp->sched, rtp->rtcp->schedid)) { |
| 9126 |
|
| 9127 |
ao2_ref(instance, -1); |
| 9128 |
} else { |
| 9129 |
|
| 9130 |
ast_debug_rtcp(1, "(%p) RTCP failed to tear down RTCP\n", instance); |
| 9131 |
ao2_lock(instance); |
| 9132 |
return; |
| 9133 |
} |
| 9134 |
ao2_lock(instance); |
| 9135 |
rtp->rtcp->schedid = -1; |
| 9136 |
} |
| 9137 |
if (rtp->transport_wide_cc.schedid > -1) { |
| 9138 |
ao2_unlock(instance); |
| 9139 |
if (!ast_sched_del(rtp->sched, rtp->transport_wide_cc.schedid)) { |
| 9140 |
ao2_ref(instance, -1); |
| 9141 |
} else { |
| 9142 |
ast_debug_rtcp(1, "(%p) RTCP failed to tear down transport-cc feedback\n", instance); |
| 9143 |
ao2_lock(instance); |
| 9144 |
return; |
| 9145 |
} |
| 9146 |
ao2_lock(instance); |
| 9147 |
rtp->transport_wide_cc.schedid = -1; |
| 9148 |
} |
| 9149 |
if (rtp->rtcp->s > -1 && rtp->rtcp->s != rtp->s) { |
| 9150 |
close(rtp->rtcp->s); |
| 9151 |
} |
| 9152 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 9153 |
ao2_unlock(instance); |
| 9154 |
dtls_srtp_stop_timeout_timer(instance, rtp, 1); |
| 9155 |
ao2_lock(instance); |
| 9156 |
|
| 9157 |
if (rtp->rtcp->dtls.ssl && rtp->rtcp->dtls.ssl != rtp->dtls.ssl) { |
| 9158 |
SSL_free(rtp->rtcp->dtls.ssl); |
| 9159 |
} |
| 9160 |
#endif |
| 9161 |
ast_free(rtp->rtcp->local_addr_str); |
| 9162 |
ast_free(rtp->rtcp); |
| 9163 |
rtp->rtcp = NULL; |
| 9164 |
ast_debug_rtcp(1, "(%s) RTCP torn down on RTP instance\n", |
| 9165 |
ast_rtp_instance_get_channel_id(instance)); |
| 9166 |
} |
| 9167 |
} |
| 9168 |
} else if (property == AST_RTP_PROPERTY_ASYMMETRIC_CODEC) { |
| 9169 |
rtp->asymmetric_codec = value; |
| 9170 |
} else if (property == AST_RTP_PROPERTY_RETRANS_SEND) { |
| 9171 |
if (value) { |
| 9172 |
if (!rtp->send_buffer) { |
| 9173 |
rtp->send_buffer = ast_data_buffer_alloc(ast_free_ptr, DEFAULT_RTP_SEND_BUFFER_SIZE); |
| 9174 |
} |
| 9175 |
} else { |
| 9176 |
if (rtp->send_buffer) { |
| 9177 |
ast_data_buffer_free(rtp->send_buffer); |
| 9178 |
rtp->send_buffer = NULL; |
| 9179 |
} |
| 9180 |
} |
| 9181 |
} else if (property == AST_RTP_PROPERTY_RETRANS_RECV) { |
| 9182 |
if (value) { |
| 9183 |
if (!rtp->recv_buffer) { |
| 9184 |
rtp->recv_buffer = ast_data_buffer_alloc(ast_free_ptr, DEFAULT_RTP_RECV_BUFFER_SIZE); |
| 9185 |
AST_VECTOR_INIT(&rtp->missing_seqno, 0); |
| 9186 |
} |
| 9187 |
} else { |
| 9188 |
if (rtp->recv_buffer) { |
| 9189 |
ast_data_buffer_free(rtp->recv_buffer); |
| 9190 |
rtp->recv_buffer = NULL; |
| 9191 |
AST_VECTOR_FREE(&rtp->missing_seqno); |
| 9192 |
} |
| 9193 |
} |
| 9194 |
} |
| 9195 |
} |
| 9196 |
|
| 9197 |
|
| 9198 |
static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp) |
| 9199 |
{ |
| 9200 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9201 |
|
| 9202 |
return rtcp ? (rtp->rtcp ? rtp->rtcp->s : -1) : rtp->s; |
| 9203 |
} |
| 9204 |
|
| 9205 |
|
| 9206 |
static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr) |
| 9207 |
{ |
| 9208 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9209 |
struct ast_sockaddr local; |
| 9210 |
int index; |
| 9211 |
|
| 9212 |
ast_rtp_instance_get_local_address(instance, &local); |
| 9213 |
if (!ast_sockaddr_isnull(addr)) { |
| 9214 |
|
| 9215 |
if (ast_ouraddrfor(addr, &local)) { |
| 9216 |
|
| 9217 |
ast_rtp_instance_get_local_address(instance, &local); |
| 9218 |
} else { |
| 9219 |
ast_rtp_instance_set_local_address(instance, &local); |
| 9220 |
} |
| 9221 |
} |
| 9222 |
|
| 9223 |
if (rtp->rtcp && !ast_sockaddr_isnull(addr)) { |
| 9224 |
ast_debug_rtcp(1, "(%p) RTCP setting address on RTP instance\n", instance); |
| 9225 |
ast_sockaddr_copy(&rtp->rtcp->them, addr); |
| 9226 |
|
| 9227 |
if (rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) { |
| 9228 |
ast_sockaddr_set_port(&rtp->rtcp->them, ast_sockaddr_port(addr) + 1); |
| 9229 |
|
| 9230 |
|
| 9231 |
ast_sockaddr_set_port(&local, ast_sockaddr_port(&local) + 1); |
| 9232 |
} |
| 9233 |
ast_sockaddr_copy(&rtp->rtcp->us, &local); |
| 9234 |
|
| 9235 |
ast_free(rtp->rtcp->local_addr_str); |
| 9236 |
rtp->rtcp->local_addr_str = ast_strdup(ast_sockaddr_stringify(&local)); |
| 9237 |
} |
| 9238 |
|
| 9239 |
|
| 9240 |
for (index = 0; index < AST_VECTOR_SIZE(&rtp->ssrc_mapping); ++index) { |
| 9241 |
struct rtp_ssrc_mapping *mapping = AST_VECTOR_GET_ADDR(&rtp->ssrc_mapping, index); |
| 9242 |
|
| 9243 |
ast_rtp_instance_set_remote_address(mapping->instance, addr); |
| 9244 |
} |
| 9245 |
|
| 9246 |
|
| 9247 |
rtp->last_seqno = 0; |
| 9248 |
rtp->last_end_timestamp.ts = 0; |
| 9249 |
rtp->last_end_timestamp.is_set = 0; |
| 9250 |
|
| 9251 |
if (strictrtp && rtp->strict_rtp_state != STRICT_RTP_OPEN |
| 9252 |
&& !ast_sockaddr_isnull(addr) && ast_sockaddr_cmp(addr, &rtp->strict_rtp_address)) { |
| 9253 |
|
| 9254 |
|
| 9255 |
|
| 9256 |
ast_verb(4, "%p -- Strict RTP learning after remote address set to: %s\n", |
| 9257 |
rtp, ast_sockaddr_stringify(addr)); |
| 9258 |
rtp_learning_start(rtp); |
| 9259 |
} |
| 9260 |
} |
| 9261 |
|
| 9262 |
|
| 9263 |
|
| 9264 |
|
| 9265 |
|
| 9266 |
|
| 9267 |
|
| 9268 |
|
| 9269 |
static int red_write(const void *data) |
| 9270 |
{ |
| 9271 |
struct ast_rtp_instance *instance = (struct ast_rtp_instance*) data; |
| 9272 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9273 |
|
| 9274 |
ao2_lock(instance); |
| 9275 |
if (rtp->red->t140.datalen > 0) { |
| 9276 |
ast_rtp_write(instance, &rtp->red->t140); |
| 9277 |
} |
| 9278 |
ao2_unlock(instance); |
| 9279 |
|
| 9280 |
return 1; |
| 9281 |
} |
| 9282 |
|
| 9283 |
|
| 9284 |
static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations) |
| 9285 |
{ |
| 9286 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9287 |
int x; |
| 9288 |
|
| 9289 |
rtp->red = ast_calloc(1, sizeof(*rtp->red)); |
| 9290 |
if (!rtp->red) { |
| 9291 |
return -1; |
| 9292 |
} |
| 9293 |
|
| 9294 |
rtp->red->t140.frametype = AST_FRAME_TEXT; |
| 9295 |
rtp->red->t140.subclass.format = ast_format_t140_red; |
| 9296 |
rtp->red->t140.data.ptr = &rtp->red->buf_data; |
| 9297 |
|
| 9298 |
rtp->red->t140red = rtp->red->t140; |
| 9299 |
rtp->red->t140red.data.ptr = &rtp->red->t140red_data; |
| 9300 |
|
| 9301 |
rtp->red->num_gen = generations; |
| 9302 |
rtp->red->hdrlen = generations * 4 + 1; |
| 9303 |
|
| 9304 |
for (x = 0; x < generations; x++) { |
| 9305 |
rtp->red->pt[x] = payloads[x]; |
| 9306 |
rtp->red->pt[x] |= 1 << 7; |
| 9307 |
rtp->red->t140red_data[x*4] = rtp->red->pt[x]; |
| 9308 |
} |
| 9309 |
rtp->red->t140red_data[x*4] = rtp->red->pt[x] = payloads[x]; |
| 9310 |
rtp->red->schedid = ast_sched_add(rtp->sched, buffer_time, red_write, instance); |
| 9311 |
|
| 9312 |
return 0; |
| 9313 |
} |
| 9314 |
|
| 9315 |
|
| 9316 |
|
| 9317 |
|
| 9318 |
|
| 9319 |
|
| 9320 |
|
| 9321 |
|
| 9322 |
static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame) |
| 9323 |
{ |
| 9324 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9325 |
struct rtp_red *red = rtp->red; |
| 9326 |
|
| 9327 |
if (!red) { |
| 9328 |
return 0; |
| 9329 |
} |
| 9330 |
|
| 9331 |
if (frame->datalen > 0) { |
| 9332 |
int space_available = 0; |
| 9333 |
|
| 9334 |
if (red->t140.datalen > 0) { |
| 9335 |
const unsigned char *primary = red->buf_data; |
| 9336 |
|
| 9337 |
|
| 9338 |
if (primary[0] == 0x08 || primary[0] == 0x0a || primary[0] == 0x0d) { |
| 9339 |
|
| 9340 |
ast_rtp_write(instance, &rtp->red->t140); |
| 9341 |
} else { |
| 9342 |
primary = frame->data.ptr; |
| 9343 |
if (primary[0] == 0x08 || primary[0] == 0x0a || primary[0] == 0x0d) { |
| 9344 |
|
| 9345 |
ast_rtp_write(instance, &rtp->red->t140); |
| 9346 |
} |
| 9347 |
} |
| 9348 |
} |
| 9349 |
|
| 9350 |
|
| 9351 |
|
| 9352 |
|
| 9353 |
|
| 9354 |
|
| 9355 |
|
| 9356 |
|
| 9357 |
space_available = UCHAR_MAX - red->t140.datalen; |
| 9358 |
|
| 9359 |
if (frame->datalen > space_available) { |
| 9360 |
ast_rtp_write(instance, &rtp->red->t140); |
| 9361 |
|
| 9362 |
|
| 9363 |
|
| 9364 |
|
| 9365 |
space_available = UCHAR_MAX; |
| 9366 |
} |
| 9367 |
|
| 9368 |
if (frame->datalen > space_available) { |
| 9369 |
ast_log(LOG_WARNING, "%s: T.140 frame of %d bytes exceeds max of %u. Discarding.\n", |
| 9370 |
ast_rtp_instance_get_channel_id(instance), |
| 9371 |
frame->datalen, UCHAR_MAX); |
| 9372 |
return -1; |
| 9373 |
} |
| 9374 |
|
| 9375 |
memcpy(&red->buf_data[red->t140.datalen], frame->data.ptr, frame->datalen); |
| 9376 |
red->t140.datalen += frame->datalen; |
| 9377 |
red->t140.ts = frame->ts; |
| 9378 |
} |
| 9379 |
|
| 9380 |
return 0; |
| 9381 |
} |
| 9382 |
|
| 9383 |
|
| 9384 |
static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1) |
| 9385 |
{ |
| 9386 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance0); |
| 9387 |
|
| 9388 |
ao2_lock(instance0); |
| 9389 |
ast_set_flag(rtp, FLAG_NEED_MARKER_BIT | FLAG_REQ_LOCAL_BRIDGE_BIT); |
| 9390 |
if (rtp->smoother) { |
| 9391 |
ast_smoother_free(rtp->smoother); |
| 9392 |
rtp->smoother = NULL; |
| 9393 |
} |
| 9394 |
|
| 9395 |
|
| 9396 |
if (!instance1) { |
| 9397 |
rtp->ssrc = rtp->ssrc_orig; |
| 9398 |
rtp->ssrc_orig = 0; |
| 9399 |
rtp->ssrc_saved = 0; |
| 9400 |
} else if (!rtp->ssrc_saved) { |
| 9401 |
|
| 9402 |
rtp->ssrc_orig = rtp->ssrc; |
| 9403 |
rtp->ssrc_saved = 1; |
| 9404 |
} |
| 9405 |
|
| 9406 |
ao2_unlock(instance0); |
| 9407 |
|
| 9408 |
return 0; |
| 9409 |
} |
| 9410 |
|
| 9411 |
|
| 9412 |
static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat) |
| 9413 |
{ |
| 9414 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9415 |
|
| 9416 |
if (!rtp->rtcp) { |
| 9417 |
return -1; |
| 9418 |
} |
| 9419 |
|
| 9420 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_TXCOUNT, -1, stats->txcount, rtp->txcount); |
| 9421 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RXCOUNT, -1, stats->rxcount, rtp->rxcount); |
| 9422 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_TXOCTETCOUNT, -1, stats->txoctetcount, rtp->txoctetcount); |
| 9423 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RXOCTETCOUNT, -1, stats->rxoctetcount, rtp->rxoctetcount); |
| 9424 |
|
| 9425 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_TXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->txploss, rtp->rtcp->reported_lost); |
| 9426 |
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); |
| 9427 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MAXRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_maxrxploss, rtp->rtcp->reported_maxlost); |
| 9428 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MINRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_minrxploss, rtp->rtcp->reported_minlost); |
| 9429 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_normdevrxploss, rtp->rtcp->reported_normdev_lost); |
| 9430 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_STDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_stdevrxploss, rtp->rtcp->reported_stdev_lost); |
| 9431 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MAXRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_maxrxploss, rtp->rtcp->maxrxlost); |
| 9432 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MINRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_minrxploss, rtp->rtcp->minrxlost); |
| 9433 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_normdevrxploss, rtp->rtcp->normdev_rxlost); |
| 9434 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_STDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_stdevrxploss, rtp->rtcp->stdev_rxlost); |
| 9435 |
AST_RTP_STAT_TERMINATOR(AST_RTP_INSTANCE_STAT_COMBINED_LOSS); |
| 9436 |
|
| 9437 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_TXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->txjitter, rtp->rxjitter); |
| 9438 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->rxjitter, rtp->rtcp->reported_jitter); |
| 9439 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MAXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_maxjitter, rtp->rtcp->reported_maxjitter); |
| 9440 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MINJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_minjitter, rtp->rtcp->reported_minjitter); |
| 9441 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_normdevjitter, rtp->rtcp->reported_normdev_jitter); |
| 9442 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_STDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_stdevjitter, rtp->rtcp->reported_stdev_jitter); |
| 9443 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MAXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_maxjitter, rtp->rtcp->maxrxjitter); |
| 9444 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MINJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_minjitter, rtp->rtcp->minrxjitter); |
| 9445 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_normdevjitter, rtp->rtcp->normdev_rxjitter); |
| 9446 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_STDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_stdevjitter, rtp->rtcp->stdev_rxjitter); |
| 9447 |
AST_RTP_STAT_TERMINATOR(AST_RTP_INSTANCE_STAT_COMBINED_JITTER); |
| 9448 |
|
| 9449 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->rtt, rtp->rtcp->rtt); |
| 9450 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_MAX_RTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->maxrtt, rtp->rtcp->maxrtt); |
| 9451 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_MIN_RTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->minrtt, rtp->rtcp->minrtt); |
| 9452 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_NORMDEVRTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->normdevrtt, rtp->rtcp->normdevrtt); |
| 9453 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_STDEVRTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->stdevrtt, rtp->rtcp->stdevrtt); |
| 9454 |
AST_RTP_STAT_TERMINATOR(AST_RTP_INSTANCE_STAT_COMBINED_RTT); |
| 9455 |
|
| 9456 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_TXMES, AST_RTP_INSTANCE_STAT_COMBINED_MES, stats->txmes, rtp->rxmes); |
| 9457 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RXMES, AST_RTP_INSTANCE_STAT_COMBINED_MES, stats->rxmes, rtp->rtcp->reported_mes); |
| 9458 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MAXMES, AST_RTP_INSTANCE_STAT_COMBINED_MES, stats->remote_maxmes, rtp->rtcp->reported_maxmes); |
| 9459 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MINMES, AST_RTP_INSTANCE_STAT_COMBINED_MES, stats->remote_minmes, rtp->rtcp->reported_minmes); |
| 9460 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVMES, AST_RTP_INSTANCE_STAT_COMBINED_MES, stats->remote_normdevmes, rtp->rtcp->reported_normdev_mes); |
| 9461 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_STDEVMES, AST_RTP_INSTANCE_STAT_COMBINED_MES, stats->remote_stdevmes, rtp->rtcp->reported_stdev_mes); |
| 9462 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MAXMES, AST_RTP_INSTANCE_STAT_COMBINED_MES, stats->local_maxmes, rtp->rtcp->maxrxmes); |
| 9463 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MINMES, AST_RTP_INSTANCE_STAT_COMBINED_MES, stats->local_minmes, rtp->rtcp->minrxmes); |
| 9464 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVMES, AST_RTP_INSTANCE_STAT_COMBINED_MES, stats->local_normdevmes, rtp->rtcp->normdev_rxmes); |
| 9465 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_STDEVMES, AST_RTP_INSTANCE_STAT_COMBINED_MES, stats->local_stdevmes, rtp->rtcp->stdev_rxmes); |
| 9466 |
AST_RTP_STAT_TERMINATOR(AST_RTP_INSTANCE_STAT_COMBINED_MES); |
| 9467 |
|
| 9468 |
|
| 9469 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_SSRC, -1, stats->local_ssrc, rtp->ssrc); |
| 9470 |
AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_SSRC, -1, stats->remote_ssrc, rtp->themssrc); |
| 9471 |
AST_RTP_STAT_STRCPY(AST_RTP_INSTANCE_STAT_CHANNEL_UNIQUEID, -1, stats->channel_uniqueid, ast_rtp_instance_get_channel_id(instance)); |
| 9472 |
|
| 9473 |
return 0; |
| 9474 |
} |
| 9475 |
|
| 9476 |
|
| 9477 |
static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1) |
| 9478 |
{ |
| 9479 |
|
| 9480 |
|
| 9481 |
|
| 9482 |
|
| 9483 |
|
| 9484 |
|
| 9485 |
|
| 9486 |
|
| 9487 |
|
| 9488 |
|
| 9489 |
return (((ast_rtp_instance_get_prop(instance0, AST_RTP_PROPERTY_DTMF) != ast_rtp_instance_get_prop(instance1, AST_RTP_PROPERTY_DTMF)) || |
| 9490 |
(!ast_channel_tech(chan0)->send_digit_begin != !ast_channel_tech(chan1)->send_digit_begin)) ? 0 : 1); |
| 9491 |
} |
| 9492 |
|
| 9493 |
|
| 9494 |
static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username) |
| 9495 |
{ |
| 9496 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9497 |
struct sockaddr_in suggestion_tmp; |
| 9498 |
|
| 9499 |
|
| 9500 |
|
| 9501 |
|
| 9502 |
|
| 9503 |
ast_sockaddr_to_sin(suggestion, &suggestion_tmp); |
| 9504 |
ast_stun_request(rtp->s, &suggestion_tmp, username, NULL); |
| 9505 |
ast_sockaddr_from_sin(suggestion, &suggestion_tmp); |
| 9506 |
} |
| 9507 |
|
| 9508 |
|
| 9509 |
static void ast_rtp_stop(struct ast_rtp_instance *instance) |
| 9510 |
{ |
| 9511 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9512 |
struct ast_sockaddr addr = { {0,} }; |
| 9513 |
|
| 9514 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 9515 |
ao2_unlock(instance); |
| 9516 |
AST_SCHED_DEL_UNREF(rtp->sched, rtp->rekeyid, ao2_ref(instance, -1)); |
| 9517 |
|
| 9518 |
dtls_srtp_stop_timeout_timer(instance, rtp, 0); |
| 9519 |
if (rtp->rtcp) { |
| 9520 |
dtls_srtp_stop_timeout_timer(instance, rtp, 1); |
| 9521 |
} |
| 9522 |
ao2_lock(instance); |
| 9523 |
#endif |
| 9524 |
ast_debug_rtp(1, "(%s) RTP Stop\n", |
| 9525 |
ast_rtp_instance_get_channel_id(instance)); |
| 9526 |
|
| 9527 |
if (rtp->rtcp && rtp->rtcp->schedid > -1) { |
| 9528 |
ao2_unlock(instance); |
| 9529 |
if (!ast_sched_del(rtp->sched, rtp->rtcp->schedid)) { |
| 9530 |
|
| 9531 |
ao2_ref(instance, -1); |
| 9532 |
} |
| 9533 |
ao2_lock(instance); |
| 9534 |
rtp->rtcp->schedid = -1; |
| 9535 |
} |
| 9536 |
|
| 9537 |
if (rtp->transport_wide_cc.schedid > -1) { |
| 9538 |
ao2_unlock(instance); |
| 9539 |
if (!ast_sched_del(rtp->sched, rtp->transport_wide_cc.schedid)) { |
| 9540 |
ao2_ref(instance, -1); |
| 9541 |
} |
| 9542 |
ao2_lock(instance); |
| 9543 |
rtp->transport_wide_cc.schedid = -1; |
| 9544 |
} |
| 9545 |
|
| 9546 |
if (rtp->red) { |
| 9547 |
ao2_unlock(instance); |
| 9548 |
AST_SCHED_DEL(rtp->sched, rtp->red->schedid); |
| 9549 |
ao2_lock(instance); |
| 9550 |
ast_free(rtp->red); |
| 9551 |
rtp->red = NULL; |
| 9552 |
} |
| 9553 |
|
| 9554 |
ast_rtp_instance_set_remote_address(instance, &addr); |
| 9555 |
|
| 9556 |
ast_set_flag(rtp, FLAG_NEED_MARKER_BIT); |
| 9557 |
} |
| 9558 |
|
| 9559 |
|
| 9560 |
static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char *desc) |
| 9561 |
{ |
| 9562 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9563 |
|
| 9564 |
return ast_set_qos(rtp->s, tos, cos, desc); |
| 9565 |
} |
| 9566 |
|
| 9567 |
|
| 9568 |
|
| 9569 |
|
| 9570 |
|
| 9571 |
|
| 9572 |
static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level) |
| 9573 |
{ |
| 9574 |
unsigned int *rtpheader; |
| 9575 |
int hdrlen = 12; |
| 9576 |
int res, payload = 0; |
| 9577 |
char data[256]; |
| 9578 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9579 |
struct ast_sockaddr remote_address = { {0,} }; |
| 9580 |
int ice; |
| 9581 |
|
| 9582 |
ast_rtp_instance_get_remote_address(instance, &remote_address); |
| 9583 |
|
| 9584 |
if (ast_sockaddr_isnull(&remote_address)) { |
| 9585 |
return -1; |
| 9586 |
} |
| 9587 |
|
| 9588 |
payload = ast_rtp_codecs_payload_code_tx(ast_rtp_instance_get_codecs(instance), 0, NULL, AST_RTP_CN); |
| 9589 |
|
| 9590 |
level = 127 - (level & 0x7f); |
| 9591 |
|
| 9592 |
rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000)); |
| 9593 |
|
| 9594 |
|
| 9595 |
rtpheader = (unsigned int *)data; |
| 9596 |
rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno)); |
| 9597 |
rtpheader[1] = htonl(rtp->lastts); |
| 9598 |
rtpheader[2] = htonl(rtp->ssrc); |
| 9599 |
data[12] = level; |
| 9600 |
|
| 9601 |
res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 1, 0, &remote_address, &ice); |
| 9602 |
|
| 9603 |
if (res < 0) { |
| 9604 |
ast_log(LOG_ERROR, "RTP Comfort Noise Transmission error to %s: %s\n", ast_sockaddr_stringify(&remote_address), strerror(errno)); |
| 9605 |
return res; |
| 9606 |
} |
| 9607 |
|
| 9608 |
if (rtp_debug_test_addr(&remote_address)) { |
| 9609 |
ast_verbose("Sent Comfort Noise RTP packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n", |
| 9610 |
ast_sockaddr_stringify(&remote_address), |
| 9611 |
ice ? " (via ICE)" : "", |
| 9612 |
AST_RTP_CN, rtp->seqno, rtp->lastdigitts, res - hdrlen); |
| 9613 |
} |
| 9614 |
|
| 9615 |
rtp->seqno++; |
| 9616 |
|
| 9617 |
return res; |
| 9618 |
} |
| 9619 |
|
| 9620 |
|
| 9621 |
static unsigned int ast_rtp_get_ssrc(struct ast_rtp_instance *instance) |
| 9622 |
{ |
| 9623 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9624 |
|
| 9625 |
return rtp->ssrc; |
| 9626 |
} |
| 9627 |
|
| 9628 |
|
| 9629 |
static const char *ast_rtp_get_cname(struct ast_rtp_instance *instance) |
| 9630 |
{ |
| 9631 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9632 |
|
| 9633 |
return rtp->cname; |
| 9634 |
} |
| 9635 |
|
| 9636 |
|
| 9637 |
static void ast_rtp_set_remote_ssrc(struct ast_rtp_instance *instance, unsigned int ssrc) |
| 9638 |
{ |
| 9639 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9640 |
|
| 9641 |
if (rtp->themssrc_valid && rtp->themssrc == ssrc) { |
| 9642 |
return; |
| 9643 |
} |
| 9644 |
|
| 9645 |
rtp->themssrc = ssrc; |
| 9646 |
rtp->themssrc_valid = 1; |
| 9647 |
|
| 9648 |
|
| 9649 |
if (rtp->bundled) { |
| 9650 |
struct ast_rtp *bundled_rtp; |
| 9651 |
int index; |
| 9652 |
|
| 9653 |
ao2_unlock(instance); |
| 9654 |
|
| 9655 |
|
| 9656 |
ao2_lock(rtp->bundled); |
| 9657 |
bundled_rtp = ast_rtp_instance_get_data(rtp->bundled); |
| 9658 |
|
| 9659 |
for (index = 0; index < AST_VECTOR_SIZE(&bundled_rtp->ssrc_mapping); ++index) { |
| 9660 |
struct rtp_ssrc_mapping *mapping = AST_VECTOR_GET_ADDR(&bundled_rtp->ssrc_mapping, index); |
| 9661 |
|
| 9662 |
if (mapping->instance == instance) { |
| 9663 |
mapping->ssrc = ssrc; |
| 9664 |
mapping->ssrc_valid = 1; |
| 9665 |
break; |
| 9666 |
} |
| 9667 |
} |
| 9668 |
|
| 9669 |
ao2_unlock(rtp->bundled); |
| 9670 |
|
| 9671 |
ao2_lock(instance); |
| 9672 |
} |
| 9673 |
} |
| 9674 |
|
| 9675 |
static void ast_rtp_set_stream_num(struct ast_rtp_instance *instance, int stream_num) |
| 9676 |
{ |
| 9677 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9678 |
|
| 9679 |
rtp->stream_num = stream_num; |
| 9680 |
} |
| 9681 |
|
| 9682 |
static int ast_rtp_extension_enable(struct ast_rtp_instance *instance, enum ast_rtp_extension extension) |
| 9683 |
{ |
| 9684 |
switch (extension) { |
| 9685 |
case AST_RTP_EXTENSION_ABS_SEND_TIME: |
| 9686 |
case AST_RTP_EXTENSION_TRANSPORT_WIDE_CC: |
| 9687 |
return 1; |
| 9688 |
default: |
| 9689 |
return 0; |
| 9690 |
} |
| 9691 |
} |
| 9692 |
|
| 9693 |
|
| 9694 |
static int ast_rtp_bundle(struct ast_rtp_instance *child, struct ast_rtp_instance *parent) |
| 9695 |
{ |
| 9696 |
struct ast_rtp *child_rtp = ast_rtp_instance_get_data(child); |
| 9697 |
struct ast_rtp *parent_rtp; |
| 9698 |
struct rtp_ssrc_mapping mapping; |
| 9699 |
struct ast_sockaddr them = { { 0, } }; |
| 9700 |
|
| 9701 |
if (child_rtp->bundled == parent) { |
| 9702 |
return 0; |
| 9703 |
} |
| 9704 |
|
| 9705 |
|
| 9706 |
if (child_rtp->bundled) { |
| 9707 |
struct ast_rtp *bundled_rtp; |
| 9708 |
|
| 9709 |
ao2_unlock(child); |
| 9710 |
|
| 9711 |
|
| 9712 |
ao2_lock(child_rtp->bundled); |
| 9713 |
bundled_rtp = ast_rtp_instance_get_data(child_rtp->bundled); |
| 9714 |
AST_VECTOR_REMOVE_CMP_UNORDERED(&bundled_rtp->ssrc_mapping, child, SSRC_MAPPING_ELEM_CMP, AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 9715 |
ao2_unlock(child_rtp->bundled); |
| 9716 |
|
| 9717 |
ao2_lock(child); |
| 9718 |
ao2_ref(child_rtp->bundled, -1); |
| 9719 |
child_rtp->bundled = NULL; |
| 9720 |
} |
| 9721 |
|
| 9722 |
if (!parent) { |
| 9723 |
|
| 9724 |
rtp_allocate_transport(child, child_rtp); |
| 9725 |
return 0; |
| 9726 |
} |
| 9727 |
|
| 9728 |
parent_rtp = ast_rtp_instance_get_data(parent); |
| 9729 |
|
| 9730 |
|
| 9731 |
rtp_deallocate_transport(child, child_rtp); |
| 9732 |
|
| 9733 |
|
| 9734 |
child_rtp->bundled = ao2_bump(parent); |
| 9735 |
|
| 9736 |
mapping.ssrc = child_rtp->themssrc; |
| 9737 |
mapping.ssrc_valid = child_rtp->themssrc_valid; |
| 9738 |
mapping.instance = child; |
| 9739 |
|
| 9740 |
ao2_unlock(child); |
| 9741 |
|
| 9742 |
ao2_lock(parent); |
| 9743 |
|
| 9744 |
AST_VECTOR_APPEND(&parent_rtp->ssrc_mapping, mapping); |
| 9745 |
|
| 9746 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 9747 |
|
| 9748 |
|
| 9749 |
|
| 9750 |
if (parent_rtp->dtls.connection == AST_RTP_DTLS_CONNECTION_EXISTING) { |
| 9751 |
dtls_srtp_add_local_ssrc(parent_rtp, parent, 0, child_rtp->ssrc, 0); |
| 9752 |
} |
| 9753 |
#endif |
| 9754 |
|
| 9755 |
|
| 9756 |
ast_rtp_instance_get_remote_address(parent, &them); |
| 9757 |
|
| 9758 |
ao2_unlock(parent); |
| 9759 |
|
| 9760 |
ao2_lock(child); |
| 9761 |
|
| 9762 |
ast_rtp_instance_set_remote_address(child, &them); |
| 9763 |
|
| 9764 |
return 0; |
| 9765 |
} |
| 9766 |
|
| 9767 |
#ifdef HAVE_PJPROJECT |
| 9768 |
static void stunaddr_resolve_callback(const struct ast_dns_query *query) |
| 9769 |
{ |
| 9770 |
const char *stunaddr_name = ast_dns_query_get_name(query); |
| 9771 |
|
| 9772 |
|
| 9773 |
store_stunaddr_resolved(stunaddr_name, ast_dns_query_get_result(query), 1); |
| 9774 |
} |
| 9775 |
|
| 9776 |
static int store_stunaddr_resolved(const char *name, const struct ast_dns_result *result, int lock) |
| 9777 |
{ |
| 9778 |
const struct ast_dns_record *record; |
| 9779 |
struct ast_dns_query_recurring *last_resolver = stunaddr_resolver; |
| 9780 |
|
| 9781 |
|
| 9782 |
|
| 9783 |
|
| 9784 |
|
| 9785 |
const int ttl = ast_dns_result_get_lowest_ttl(result); |
| 9786 |
|
| 9787 |
for (record = ast_dns_result_get_records(result); record; record = ast_dns_record_get_next(record)) { |
| 9788 |
const size_t data_size = ast_dns_record_get_data_size(record); |
| 9789 |
const unsigned char *data = (unsigned char *)ast_dns_record_get_data(record); |
| 9790 |
const int rr_type = ast_dns_record_get_rr_type(record); |
| 9791 |
|
| 9792 |
ast_debug_stun(2, "Record rr_type '%u' ttl: %d data_size '%zu' from DNS query for stunaddr '%s'\n", |
| 9793 |
rr_type, ttl, data_size, name); |
| 9794 |
|
| 9795 |
if (rr_type == ns_t_a && data_size == 4) { |
| 9796 |
if (lock) { |
| 9797 |
ast_rwlock_wrlock(&stunaddr_lock); |
| 9798 |
} |
| 9799 |
memcpy(&stunaddr.sin_addr, data, data_size); |
| 9800 |
stunaddr.sin_family = AF_INET; |
| 9801 |
stunaddr_ttl = ttl; |
| 9802 |
ast_debug_stun(2, "Resolved stunaddr '%s' to '%s'. TTL = %d.\n", name, |
| 9803 |
ast_inet_ntoa(stunaddr.sin_addr), stunaddr_ttl); |
| 9804 |
if (stunaddr_ttl == 0) { |
| 9805 |
ast_log(LOG_WARNING, "Resolution for stunaddr '%s' returned TTL = 0. Recurring resolution disabled.\n", name); |
| 9806 |
ao2_cleanup(stunaddr_resolver); |
| 9807 |
stunaddr_resolver = NULL; |
| 9808 |
} |
| 9809 |
if (lock) { |
| 9810 |
ast_rwlock_unlock(&stunaddr_lock); |
| 9811 |
} |
| 9812 |
|
| 9813 |
return 1; |
| 9814 |
} else { |
| 9815 |
ast_debug_stun(2, "Unrecognized rr_type '%u' or data_size '%zu' from DNS query for stunaddr '%s'\n", |
| 9816 |
rr_type, data_size, name); |
| 9817 |
continue; |
| 9818 |
} |
| 9819 |
} |
| 9820 |
|
| 9821 |
ao2_cleanup(stunaddr_resolver); |
| 9822 |
stunaddr_resolver = NULL; |
| 9823 |
stunaddr_ttl = 0; |
| 9824 |
|
| 9825 |
if (stunaddr.sin_addr.s_addr) { |
| 9826 |
ast_log(LOG_WARNING, "Lookup of stunaddr '%s' failed.%s STUN continuing with server %s:%d\n", |
| 9827 |
name, last_resolver ? " Periodic resolution cancelled." : "", |
| 9828 |
ast_inet_ntoa(stunaddr.sin_addr), htons(stunaddr.sin_port)); |
| 9829 |
} else { |
| 9830 |
ast_log(LOG_WARNING, "Lookup of stunaddr '%s' failed. STUN disabled.\n", name); |
| 9831 |
} |
| 9832 |
|
| 9833 |
return 0; |
| 9834 |
} |
| 9835 |
|
| 9836 |
static void clean_stunaddr(void) { |
| 9837 |
ast_rwlock_wrlock(&stunaddr_lock); |
| 9838 |
ast_debug_stun(2, "Cleanup\n"); |
| 9839 |
if (stunaddr_resolver) { |
| 9840 |
ast_debug_stun(2, "Cancelling recurring resolution for '%s'\n", stun_hostname); |
| 9841 |
if (ast_dns_resolve_recurring_cancel(stunaddr_resolver)) { |
| 9842 |
ast_log(LOG_ERROR, "Failed to cancel recurring DNS resolution of previous stunaddr.\n"); |
| 9843 |
} |
| 9844 |
ao2_ref(stunaddr_resolver, -1); |
| 9845 |
stunaddr_resolver = NULL; |
| 9846 |
} |
| 9847 |
memset(&stunaddr, 0, sizeof(stunaddr)); |
| 9848 |
stunaddr_ttl = 0; |
| 9849 |
ast_free(stun_hostname); |
| 9850 |
stun_hostname = NULL; |
| 9851 |
ast_rwlock_unlock(&stunaddr_lock); |
| 9852 |
} |
| 9853 |
#endif |
| 9854 |
|
| 9855 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 9856 |
|
| 9857 |
static int ast_rtp_activate(struct ast_rtp_instance *instance) |
| 9858 |
{ |
| 9859 |
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); |
| 9860 |
|
| 9861 |
|
| 9862 |
#ifdef HAVE_PJPROJECT |
| 9863 |
if (rtp->ice) { |
| 9864 |
return 0; |
| 9865 |
} |
| 9866 |
#endif |
| 9867 |
|
| 9868 |
ast_debug_dtls(3, "(%p) DTLS - ast_rtp_activate rtp=%p - setup and perform DTLS'\n", instance, rtp); |
| 9869 |
|
| 9870 |
dtls_perform_setup(&rtp->dtls); |
| 9871 |
dtls_perform_handshake(instance, &rtp->dtls, 0); |
| 9872 |
|
| 9873 |
if (rtp->rtcp && rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) { |
| 9874 |
dtls_perform_setup(&rtp->rtcp->dtls); |
| 9875 |
dtls_perform_handshake(instance, &rtp->rtcp->dtls, 1); |
| 9876 |
} |
| 9877 |
|
| 9878 |
return 0; |
| 9879 |
} |
| 9880 |
#endif |
| 9881 |
|
| 9882 |
static char *rtp_do_debug_ip(struct ast_cli_args *a) |
| 9883 |
{ |
| 9884 |
char *arg = ast_strdupa(a->argv[4]); |
| 9885 |
char *debughost = NULL; |
| 9886 |
char *debugport = NULL; |
| 9887 |
|
| 9888 |
if (!ast_sockaddr_parse(&rtpdebugaddr, arg, 0) || !ast_sockaddr_split_hostport(arg, &debughost, &debugport, 0)) { |
| 9889 |
ast_cli(a->fd, "Lookup failed for '%s'\n", arg); |
| 9890 |
return CLI_FAILURE; |
| 9891 |
} |
| 9892 |
rtpdebugport = (!ast_strlen_zero(debugport) && debugport[0] != '0'); |
| 9893 |
ast_cli(a->fd, "RTP Packet Debugging Enabled for address: %s\n", |
| 9894 |
ast_sockaddr_stringify(&rtpdebugaddr)); |
| 9895 |
ast_debug_category_set_sublevel(AST_LOG_CATEGORY_RTP_PACKET, AST_LOG_CATEGORY_ENABLED); |
| 9896 |
return CLI_SUCCESS; |
| 9897 |
} |
| 9898 |
|
| 9899 |
static char *rtcp_do_debug_ip(struct ast_cli_args *a) |
| 9900 |
{ |
| 9901 |
char *arg = ast_strdupa(a->argv[4]); |
| 9902 |
char *debughost = NULL; |
| 9903 |
char *debugport = NULL; |
| 9904 |
|
| 9905 |
if (!ast_sockaddr_parse(&rtcpdebugaddr, arg, 0) || !ast_sockaddr_split_hostport(arg, &debughost, &debugport, 0)) { |
| 9906 |
ast_cli(a->fd, "Lookup failed for '%s'\n", arg); |
| 9907 |
return CLI_FAILURE; |
| 9908 |
} |
| 9909 |
rtcpdebugport = (!ast_strlen_zero(debugport) && debugport[0] != '0'); |
| 9910 |
ast_cli(a->fd, "RTCP Packet Debugging Enabled for address: %s\n", |
| 9911 |
ast_sockaddr_stringify(&rtcpdebugaddr)); |
| 9912 |
ast_debug_category_set_sublevel(AST_LOG_CATEGORY_RTCP_PACKET, AST_LOG_CATEGORY_ENABLED); |
| 9913 |
return CLI_SUCCESS; |
| 9914 |
} |
| 9915 |
|
| 9916 |
static char *handle_cli_rtp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 9917 |
{ |
| 9918 |
switch (cmd) { |
| 9919 |
case CLI_INIT: |
| 9920 |
e->command = "rtp set debug {on|off|ip}"; |
| 9921 |
e->usage = |
| 9922 |
"Usage: rtp set debug {on|off|ip host[:port]}\n" |
| 9923 |
" Enable/Disable dumping of all RTP packets. If 'ip' is\n" |
| 9924 |
" specified, limit the dumped packets to those to and from\n" |
| 9925 |
" the specified 'host' with optional port.\n"; |
| 9926 |
return NULL; |
| 9927 |
case CLI_GENERATE: |
| 9928 |
return NULL; |
| 9929 |
} |
| 9930 |
|
| 9931 |
if (a->argc == e->args) { |
| 9932 |
if (!strncasecmp(a->argv[e->args-1], "on", 2)) { |
| 9933 |
ast_debug_category_set_sublevel(AST_LOG_CATEGORY_RTP_PACKET, AST_LOG_CATEGORY_ENABLED); |
| 9934 |
memset(&rtpdebugaddr, 0, sizeof(rtpdebugaddr)); |
| 9935 |
ast_cli(a->fd, "RTP Packet Debugging Enabled\n"); |
| 9936 |
return CLI_SUCCESS; |
| 9937 |
} else if (!strncasecmp(a->argv[e->args-1], "off", 3)) { |
| 9938 |
ast_debug_category_set_sublevel(AST_LOG_CATEGORY_RTP_PACKET, AST_LOG_CATEGORY_DISABLED); |
| 9939 |
ast_cli(a->fd, "RTP Packet Debugging Disabled\n"); |
| 9940 |
return CLI_SUCCESS; |
| 9941 |
} |
| 9942 |
} else if (a->argc == e->args +1) { |
| 9943 |
return rtp_do_debug_ip(a); |
| 9944 |
} |
| 9945 |
|
| 9946 |
return CLI_SHOWUSAGE; |
| 9947 |
} |
| 9948 |
|
| 9949 |
|
| 9950 |
static char *handle_cli_rtp_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 9951 |
{ |
| 9952 |
#ifdef HAVE_PJPROJECT |
| 9953 |
struct sockaddr_in stunaddr_copy; |
| 9954 |
const char *stun_hostname_copy = NULL; |
| 9955 |
int stunaddr_ttl_copy = 0; |
| 9956 |
#endif |
| 9957 |
switch (cmd) { |
| 9958 |
case CLI_INIT: |
| 9959 |
e->command = "rtp show settings"; |
| 9960 |
e->usage = |
| 9961 |
"Usage: rtp show settings\n" |
| 9962 |
" Display RTP configuration settings\n"; |
| 9963 |
return NULL; |
| 9964 |
case CLI_GENERATE: |
| 9965 |
return NULL; |
| 9966 |
} |
| 9967 |
|
| 9968 |
if (a->argc != 3) { |
| 9969 |
return CLI_SHOWUSAGE; |
| 9970 |
} |
| 9971 |
|
| 9972 |
ast_cli(a->fd, "\n\nGeneral Settings:\n"); |
| 9973 |
ast_cli(a->fd, "----------------\n"); |
| 9974 |
ast_cli(a->fd, " Port start: %d\n", rtpstart); |
| 9975 |
ast_cli(a->fd, " Port end: %d\n", rtpend); |
| 9976 |
#ifdef SO_NO_CHECK |
| 9977 |
ast_cli(a->fd, " Checksums: %s\n", AST_CLI_YESNO(nochecksums == 0)); |
| 9978 |
#endif |
| 9979 |
ast_cli(a->fd, " DTMF Timeout: %d\n", dtmftimeout); |
| 9980 |
ast_cli(a->fd, " Strict RTP: %s\n", AST_CLI_YESNO(strictrtp)); |
| 9981 |
|
| 9982 |
if (strictrtp) { |
| 9983 |
ast_cli(a->fd, " Probation: %d frames\n", learning_min_sequential); |
| 9984 |
} |
| 9985 |
|
| 9986 |
ast_cli(a->fd, " Replay Protect: %s\n", AST_CLI_YESNO(srtp_replay_protection)); |
| 9987 |
#ifdef HAVE_PJPROJECT |
| 9988 |
ast_cli(a->fd, " ICE support: %s\n", AST_CLI_YESNO(icesupport)); |
| 9989 |
|
| 9990 |
ast_rwlock_rdlock(&stunaddr_lock); |
| 9991 |
memcpy(&stunaddr_copy, &stunaddr, sizeof(stunaddr)); |
| 9992 |
stun_hostname_copy = ast_strdupa(S_OR(stun_hostname, "")); |
| 9993 |
stunaddr_ttl_copy = stunaddr_ttl; |
| 9994 |
ast_rwlock_unlock(&stunaddr_lock); |
| 9995 |
|
| 9996 |
ast_cli(a->fd, " STUN: %s\n", stunaddr_copy.sin_addr.s_addr ? "enbabled" : "disabled"); |
| 9997 |
if (ast_strlen_zero(stun_hostname_copy)) { |
| 9998 |
ast_cli(a->fd, " Address: %s:%d\n", ast_inet_ntoa(stunaddr_copy.sin_addr), |
| 9999 |
htons(stunaddr_copy.sin_port)); |
| 10000 |
} else { |
| 10001 |
ast_cli(a->fd, " Hostname: %s:%d\n", stun_hostname_copy, htons(stunaddr_copy.sin_port)); |
| 10002 |
ast_cli(a->fd, " Resolved Addr: %s:%d%s\n", ast_inet_ntoa(stunaddr_copy.sin_addr), |
| 10003 |
htons(stunaddr_copy.sin_port), |
| 10004 |
stunaddr_copy.sin_addr.s_addr ? stunaddr_resolver ? "" : " (possibly stale)" : " (lookup failed)"); |
| 10005 |
ast_cli(a->fd, " Last TTL: %d (periodic resolution %s)\n", stunaddr_ttl_copy, |
| 10006 |
stunaddr_resolver ? "enabled" : "disabled"); |
| 10007 |
ast_cli(a->fd, " Reresove TTL 0: %s\n", AST_CLI_YESNO(stunaddr_reresolve_ttl_0)); |
| 10008 |
} |
| 10009 |
if (stun_acl) { |
| 10010 |
ast_acl_output(a->fd, stun_acl, " "); |
| 10011 |
} |
| 10012 |
#endif |
| 10013 |
return CLI_SUCCESS; |
| 10014 |
} |
| 10015 |
|
| 10016 |
|
| 10017 |
static char *handle_cli_rtcp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 10018 |
{ |
| 10019 |
switch (cmd) { |
| 10020 |
case CLI_INIT: |
| 10021 |
e->command = "rtcp set debug {on|off|ip}"; |
| 10022 |
e->usage = |
| 10023 |
"Usage: rtcp set debug {on|off|ip host[:port]}\n" |
| 10024 |
" Enable/Disable dumping of all RTCP packets. If 'ip' is\n" |
| 10025 |
" specified, limit the dumped packets to those to and from\n" |
| 10026 |
" the specified 'host' with optional port.\n"; |
| 10027 |
return NULL; |
| 10028 |
case CLI_GENERATE: |
| 10029 |
return NULL; |
| 10030 |
} |
| 10031 |
|
| 10032 |
if (a->argc == e->args) { |
| 10033 |
if (!strncasecmp(a->argv[e->args-1], "on", 2)) { |
| 10034 |
ast_debug_category_set_sublevel(AST_LOG_CATEGORY_RTCP_PACKET, AST_LOG_CATEGORY_ENABLED); |
| 10035 |
memset(&rtcpdebugaddr, 0, sizeof(rtcpdebugaddr)); |
| 10036 |
ast_cli(a->fd, "RTCP Packet Debugging Enabled\n"); |
| 10037 |
return CLI_SUCCESS; |
| 10038 |
} else if (!strncasecmp(a->argv[e->args-1], "off", 3)) { |
| 10039 |
ast_debug_category_set_sublevel(AST_LOG_CATEGORY_RTCP_PACKET, AST_LOG_CATEGORY_DISABLED); |
| 10040 |
ast_cli(a->fd, "RTCP Packet Debugging Disabled\n"); |
| 10041 |
return CLI_SUCCESS; |
| 10042 |
} |
| 10043 |
} else if (a->argc == e->args +1) { |
| 10044 |
return rtcp_do_debug_ip(a); |
| 10045 |
} |
| 10046 |
|
| 10047 |
return CLI_SHOWUSAGE; |
| 10048 |
} |
| 10049 |
|
| 10050 |
static char *handle_cli_rtcp_set_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 10051 |
{ |
| 10052 |
switch (cmd) { |
| 10053 |
case CLI_INIT: |
| 10054 |
e->command = "rtcp set stats {on|off}"; |
| 10055 |
e->usage = |
| 10056 |
"Usage: rtcp set stats {on|off}\n" |
| 10057 |
" Enable/Disable dumping of RTCP stats.\n"; |
| 10058 |
return NULL; |
| 10059 |
case CLI_GENERATE: |
| 10060 |
return NULL; |
| 10061 |
} |
| 10062 |
|
| 10063 |
if (a->argc != e->args) |
| 10064 |
return CLI_SHOWUSAGE; |
| 10065 |
|
| 10066 |
if (!strncasecmp(a->argv[e->args-1], "on", 2)) |
| 10067 |
rtcpstats = 1; |
| 10068 |
else if (!strncasecmp(a->argv[e->args-1], "off", 3)) |
| 10069 |
rtcpstats = 0; |
| 10070 |
else |
| 10071 |
return CLI_SHOWUSAGE; |
| 10072 |
|
| 10073 |
ast_cli(a->fd, "RTCP Stats %s\n", rtcpstats ? "Enabled" : "Disabled"); |
| 10074 |
return CLI_SUCCESS; |
| 10075 |
} |
| 10076 |
|
| 10077 |
#ifdef AST_DEVMODE |
| 10078 |
|
| 10079 |
static unsigned int use_random(struct ast_cli_args *a, int pos, unsigned int index) |
| 10080 |
{ |
| 10081 |
return pos >= index && !ast_strlen_zero(a->argv[index - 1]) && |
| 10082 |
!strcasecmp(a->argv[index - 1], "random"); |
| 10083 |
} |
| 10084 |
|
| 10085 |
static char *handle_cli_rtp_drop_incoming_packets(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 10086 |
{ |
| 10087 |
static const char * const completions_2[] = { "stop", "<N>", NULL }; |
| 10088 |
static const char * const completions_3[] = { "random", "incoming packets", NULL }; |
| 10089 |
static const char * const completions_5[] = { "on", "every", NULL }; |
| 10090 |
static const char * const completions_units[] = { "random", "usec", "msec", "sec", "min", NULL }; |
| 10091 |
|
| 10092 |
unsigned int use_random_num = 0; |
| 10093 |
unsigned int use_random_interval = 0; |
| 10094 |
unsigned int num_to_drop = 0; |
| 10095 |
unsigned int interval = 0; |
| 10096 |
const char *interval_s = NULL; |
| 10097 |
const char *unit_s = NULL; |
| 10098 |
struct ast_sockaddr addr; |
| 10099 |
const char *addr_s = NULL; |
| 10100 |
|
| 10101 |
switch (cmd) { |
| 10102 |
case CLI_INIT: |
| 10103 |
e->command = "rtp drop"; |
| 10104 |
e->usage = |
| 10105 |
"Usage: rtp drop [stop|[<N> [random] incoming packets[ every <N> [random] {usec|msec|sec|min}][ on <ip[:port]>]]\n" |
| 10106 |
" Drop RTP incoming packets.\n"; |
| 10107 |
return NULL; |
| 10108 |
case CLI_GENERATE: |
| 10109 |
use_random_num = use_random(a, a->pos, 4); |
| 10110 |
use_random_interval = use_random(a, a->pos, 8 + use_random_num) || |
| 10111 |
use_random(a, a->pos, 10 + use_random_num); |
| 10112 |
|
| 10113 |
switch (a->pos - use_random_num - use_random_interval) { |
| 10114 |
case 2: |
| 10115 |
return ast_cli_complete(a->word, completions_2, a->n); |
| 10116 |
case 3: |
| 10117 |
return ast_cli_complete(a->word, completions_3 + use_random_num, a->n); |
| 10118 |
case 5: |
| 10119 |
return ast_cli_complete(a->word, completions_5, a->n); |
| 10120 |
case 7: |
| 10121 |
if (!strcasecmp(a->argv[a->pos - 2], "on")) { |
| 10122 |
ast_cli_completion_add(ast_strdup("every")); |
| 10123 |
break; |
| 10124 |
} |
| 10125 |
|
| 10126 |
case 9: |
| 10127 |
if (!strcasecmp(a->argv[a->pos - 2 - use_random_interval], "every")) { |
| 10128 |
return ast_cli_complete(a->word, completions_units + use_random_interval, a->n); |
| 10129 |
} |
| 10130 |
break; |
| 10131 |
case 8: |
| 10132 |
if (!strcasecmp(a->argv[a->pos - 3 - use_random_interval], "every")) { |
| 10133 |
ast_cli_completion_add(ast_strdup("on")); |
| 10134 |
} |
| 10135 |
break; |
| 10136 |
} |
| 10137 |
|
| 10138 |
return NULL; |
| 10139 |
} |
| 10140 |
|
| 10141 |
if (a->argc < 3) { |
| 10142 |
return CLI_SHOWUSAGE; |
| 10143 |
} |
| 10144 |
|
| 10145 |
use_random_num = use_random(a, a->argc, 4); |
| 10146 |
use_random_interval = use_random(a, a->argc, 8 + use_random_num) || |
| 10147 |
use_random(a, a->argc, 10 + use_random_num); |
| 10148 |
|
| 10149 |
if (!strcasecmp(a->argv[2], "stop")) { |
| 10150 |
|
| 10151 |
} else if (a->argc < 5) { |
| 10152 |
return CLI_SHOWUSAGE; |
| 10153 |
} else if (ast_str_to_uint(a->argv[2], &num_to_drop)) { |
| 10154 |
ast_cli(a->fd, "%s is not a valid number of packets to drop\n", a->argv[2]); |
| 10155 |
return CLI_FAILURE; |
| 10156 |
} else if (a->argc - use_random_num == 5) { |
| 10157 |
|
| 10158 |
} else if (a->argc - use_random_num >= 7 && !strcasecmp(a->argv[5 + use_random_num], "on")) { |
| 10159 |
|
| 10160 |
addr_s = a->argv[6 + use_random_num]; |
| 10161 |
if (a->argc - use_random_num - use_random_interval == 10 && |
| 10162 |
!strcasecmp(a->argv[7 + use_random_num], "every")) { |
| 10163 |
|
| 10164 |
interval_s = a->argv[8 + use_random_num]; |
| 10165 |
unit_s = a->argv[9 + use_random_num + use_random_interval]; |
| 10166 |
} |
| 10167 |
} else if (a->argc - use_random_num >= 8 && !strcasecmp(a->argv[5 + use_random_num], "every")) { |
| 10168 |
|
| 10169 |
interval_s = a->argv[6 + use_random_num]; |
| 10170 |
unit_s = a->argv[7 + use_random_num + use_random_interval]; |
| 10171 |
if (a->argc == 10 + use_random_num + use_random_interval && |
| 10172 |
!strcasecmp(a->argv[8 + use_random_num + use_random_interval], "on")) { |
| 10173 |
|
| 10174 |
addr_s = a->argv[9 + use_random_num + use_random_interval]; |
| 10175 |
} |
| 10176 |
} else { |
| 10177 |
return CLI_SHOWUSAGE; |
| 10178 |
} |
| 10179 |
|
| 10180 |
if (a->argc - use_random_num >= 8 && !interval_s && !addr_s) { |
| 10181 |
return CLI_SHOWUSAGE; |
| 10182 |
} |
| 10183 |
|
| 10184 |
if (interval_s && ast_str_to_uint(interval_s, &interval)) { |
| 10185 |
ast_cli(a->fd, "%s is not a valid interval number\n", interval_s); |
| 10186 |
return CLI_FAILURE; |
| 10187 |
} |
| 10188 |
|
| 10189 |
memset(&addr, 0, sizeof(addr)); |
| 10190 |
if (addr_s && !ast_sockaddr_parse(&addr, addr_s, 0)) { |
| 10191 |
ast_cli(a->fd, "%s is not a valid hostname[:port]\n", addr_s); |
| 10192 |
return CLI_FAILURE; |
| 10193 |
} |
| 10194 |
|
| 10195 |
drop_packets_data.use_random_num = use_random_num; |
| 10196 |
drop_packets_data.use_random_interval = use_random_interval; |
| 10197 |
drop_packets_data.num_to_drop = num_to_drop; |
| 10198 |
drop_packets_data.interval = ast_time_create_by_unit_str(interval, unit_s); |
| 10199 |
ast_sockaddr_copy(&drop_packets_data.addr, &addr); |
| 10200 |
drop_packets_data.port = ast_sockaddr_port(&addr); |
| 10201 |
|
| 10202 |
drop_packets_data_update(ast_tvnow()); |
| 10203 |
|
| 10204 |
return CLI_SUCCESS; |
| 10205 |
} |
| 10206 |
#endif |
| 10207 |
|
| 10208 |
#ifdef HAVE_PJPROJECT |
| 10209 |
static char *handle_cli_rtp_refresh_stun(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 10210 |
{ |
| 10211 |
switch (cmd) { |
| 10212 |
case CLI_INIT: |
| 10213 |
e->command = "rtp resolve stun hostname"; |
| 10214 |
e->usage = |
| 10215 |
"Usage: rtp resolve stun hostname\n" |
| 10216 |
" Force a resolution of the STUN hostname (if set).\n"; |
| 10217 |
return NULL; |
| 10218 |
case CLI_GENERATE: |
| 10219 |
return NULL; |
| 10220 |
} |
| 10221 |
|
| 10222 |
if (a->argc != e->args) { |
| 10223 |
return CLI_SHOWUSAGE; |
| 10224 |
} |
| 10225 |
|
| 10226 |
ast_rwlock_wrlock(&stunaddr_lock); |
| 10227 |
if (ast_strlen_zero(stun_hostname)) { |
| 10228 |
if (stunaddr.sin_addr.s_addr) { |
| 10229 |
ast_cli(a->fd, "RTP STUN server specified as IP address '%s'. Resolution not required./n", |
| 10230 |
ast_inet_ntoa(stunaddr.sin_addr)); |
| 10231 |
} else { |
| 10232 |
ast_cli(a->fd, "RTP STUN disabled./n"); |
| 10233 |
} |
| 10234 |
} else { |
| 10235 |
if (stunaddr_resolver) { |
| 10236 |
ast_debug_stun(2, "Cancelling recurring resolution for '%s'\n", stun_hostname); |
| 10237 |
if (ast_dns_resolve_recurring_cancel(stunaddr_resolver)) { |
| 10238 |
ast_log(LOG_ERROR, "Failed to cancel recurring DNS resolution of previous stunaddr.\n"); |
| 10239 |
} |
| 10240 |
ao2_ref(stunaddr_resolver, -1); |
| 10241 |
stunaddr_resolver = NULL; |
| 10242 |
} |
| 10243 |
stunaddr_resolver = ast_dns_resolve_recurring(stun_hostname, T_A, C_IN, &stunaddr_resolve_callback, NULL); |
| 10244 |
if (!stunaddr_resolver) { |
| 10245 |
ast_cli(a->fd, "Failed to setup recurring DNS resolution of stunaddr '%s'", |
| 10246 |
stun_hostname); |
| 10247 |
} else { |
| 10248 |
ast_cli(a->fd, "Triggered background stun hostname resolution for '%s'. Run 'rtp show settings' to check results.\n", stun_hostname); |
| 10249 |
} |
| 10250 |
} |
| 10251 |
ast_rwlock_unlock(&stunaddr_lock); |
| 10252 |
|
| 10253 |
return CLI_SUCCESS; |
| 10254 |
} |
| 10255 |
#endif |
| 10256 |
|
| 10257 |
static struct ast_cli_entry cli_rtp[] = { |
| 10258 |
AST_CLI_DEFINE(handle_cli_rtp_set_debug, "Enable/Disable RTP debugging"), |
| 10259 |
AST_CLI_DEFINE(handle_cli_rtp_settings, "Display RTP settings"), |
| 10260 |
AST_CLI_DEFINE(handle_cli_rtcp_set_debug, "Enable/Disable RTCP debugging"), |
| 10261 |
AST_CLI_DEFINE(handle_cli_rtcp_set_stats, "Enable/Disable RTCP stats"), |
| 10262 |
#ifdef AST_DEVMODE |
| 10263 |
AST_CLI_DEFINE(handle_cli_rtp_drop_incoming_packets, "Drop RTP incoming packets"), |
| 10264 |
#endif |
| 10265 |
#ifdef HAVE_PJPROJECT |
| 10266 |
AST_CLI_DEFINE(handle_cli_rtp_refresh_stun, "Force a resolution of the STUN hostname"), |
| 10267 |
#endif |
| 10268 |
}; |
| 10269 |
|
| 10270 |
static int rtp_reload(int reload, int by_external_config) |
| 10271 |
{ |
| 10272 |
struct ast_config *cfg; |
| 10273 |
const char *s; |
| 10274 |
struct ast_flags config_flags = { (reload && !by_external_config) ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 10275 |
|
| 10276 |
#ifdef HAVE_PJPROJECT |
| 10277 |
struct ast_variable *var; |
| 10278 |
struct ast_ice_host_candidate *candidate; |
| 10279 |
int acl_subscription_flag = 0; |
| 10280 |
#endif |
| 10281 |
|
| 10282 |
cfg = ast_config_load2("rtp.conf", "rtp", config_flags); |
| 10283 |
if (!cfg || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) { |
| 10284 |
return 0; |
| 10285 |
} |
| 10286 |
|
| 10287 |
#ifdef SO_NO_CHECK |
| 10288 |
nochecksums = 0; |
| 10289 |
#endif |
| 10290 |
|
| 10291 |
rtpstart = DEFAULT_RTP_START; |
| 10292 |
rtpend = DEFAULT_RTP_END; |
| 10293 |
rtcpinterval = RTCP_DEFAULT_INTERVALMS; |
| 10294 |
dtmftimeout = DEFAULT_DTMF_TIMEOUT; |
| 10295 |
strictrtp = DEFAULT_STRICT_RTP; |
| 10296 |
learning_min_sequential = DEFAULT_LEARNING_MIN_SEQUENTIAL; |
| 10297 |
learning_min_duration = DEFAULT_LEARNING_MIN_DURATION; |
| 10298 |
srtp_replay_protection = DEFAULT_SRTP_REPLAY_PROTECTION; |
| 10299 |
|
| 10300 |
|
| 10301 |
|
| 10302 |
|
| 10303 |
|
| 10304 |
|
| 10305 |
|
| 10306 |
|
| 10307 |
|
| 10308 |
#ifdef HAVE_PJPROJECT |
| 10309 |
icesupport = DEFAULT_ICESUPPORT; |
| 10310 |
stun_software_attribute = DEFAULT_STUN_SOFTWARE_ATTRIBUTE; |
| 10311 |
turnport = DEFAULT_TURN_PORT; |
| 10312 |
clean_stunaddr(); |
| 10313 |
turnaddr = pj_str(NULL); |
| 10314 |
turnusername = pj_str(NULL); |
| 10315 |
turnpassword = pj_str(NULL); |
| 10316 |
host_candidate_overrides_clear(); |
| 10317 |
#endif |
| 10318 |
|
| 10319 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 10320 |
dtls_mtu = DEFAULT_DTLS_MTU; |
| 10321 |
#endif |
| 10322 |
|
| 10323 |
if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) { |
| 10324 |
rtpstart = atoi(s); |
| 10325 |
if (rtpstart < MINIMUM_RTP_PORT) |
| 10326 |
rtpstart = MINIMUM_RTP_PORT; |
| 10327 |
if (rtpstart > MAXIMUM_RTP_PORT) |
| 10328 |
rtpstart = MAXIMUM_RTP_PORT; |
| 10329 |
} |
| 10330 |
if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) { |
| 10331 |
rtpend = atoi(s); |
| 10332 |
if (rtpend < MINIMUM_RTP_PORT) |
| 10333 |
rtpend = MINIMUM_RTP_PORT; |
| 10334 |
if (rtpend > MAXIMUM_RTP_PORT) |
| 10335 |
rtpend = MAXIMUM_RTP_PORT; |
| 10336 |
} |
| 10337 |
if ((s = ast_variable_retrieve(cfg, "general", "rtcpinterval"))) { |
| 10338 |
rtcpinterval = atoi(s); |
| 10339 |
if (rtcpinterval == 0) |
| 10340 |
rtcpinterval = 0; |
| 10341 |
if (rtcpinterval < RTCP_MIN_INTERVALMS) |
| 10342 |
rtcpinterval = RTCP_MIN_INTERVALMS; |
| 10343 |
if (rtcpinterval > RTCP_MAX_INTERVALMS) |
| 10344 |
rtcpinterval = RTCP_MAX_INTERVALMS; |
| 10345 |
} |
| 10346 |
if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) { |
| 10347 |
#ifdef SO_NO_CHECK |
| 10348 |
nochecksums = ast_false(s) ? 1 : 0; |
| 10349 |
#else |
| 10350 |
if (ast_false(s)) |
| 10351 |
ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n"); |
| 10352 |
#endif |
| 10353 |
} |
| 10354 |
if ((s = ast_variable_retrieve(cfg, "general", "dtmftimeout"))) { |
| 10355 |
dtmftimeout = atoi(s); |
| 10356 |
if ((dtmftimeout < 0) || (dtmftimeout > 64000)) { |
| 10357 |
ast_log(LOG_WARNING, "DTMF timeout of '%d' outside range, using default of '%d' instead\n", |
| 10358 |
dtmftimeout, DEFAULT_DTMF_TIMEOUT); |
| 10359 |
dtmftimeout = DEFAULT_DTMF_TIMEOUT; |
| 10360 |
}; |
| 10361 |
} |
| 10362 |
if ((s = ast_variable_retrieve(cfg, "general", "strictrtp"))) { |
| 10363 |
if (ast_true(s)) { |
| 10364 |
strictrtp = STRICT_RTP_YES; |
| 10365 |
} else if (!strcasecmp(s, "seqno")) { |
| 10366 |
strictrtp = STRICT_RTP_SEQNO; |
| 10367 |
} else { |
| 10368 |
strictrtp = STRICT_RTP_NO; |
| 10369 |
} |
| 10370 |
} |
| 10371 |
if ((s = ast_variable_retrieve(cfg, "general", "probation"))) { |
| 10372 |
if ((sscanf(s, "%d", &learning_min_sequential) != 1) || learning_min_sequential <= 1) { |
| 10373 |
ast_log(LOG_WARNING, "Value for 'probation' could not be read, using default of '%d' instead\n", |
| 10374 |
DEFAULT_LEARNING_MIN_SEQUENTIAL); |
| 10375 |
learning_min_sequential = DEFAULT_LEARNING_MIN_SEQUENTIAL; |
| 10376 |
} |
| 10377 |
learning_min_duration = CALC_LEARNING_MIN_DURATION(learning_min_sequential); |
| 10378 |
} |
| 10379 |
if ((s = ast_variable_retrieve(cfg, "general", "srtpreplayprotection"))) { |
| 10380 |
srtp_replay_protection = ast_true(s); |
| 10381 |
} |
| 10382 |
#ifdef HAVE_PJPROJECT |
| 10383 |
if ((s = ast_variable_retrieve(cfg, "general", "icesupport"))) { |
| 10384 |
icesupport = ast_true(s); |
| 10385 |
} |
| 10386 |
if ((s = ast_variable_retrieve(cfg, "general", "stun_software_attribute"))) { |
| 10387 |
stun_software_attribute = ast_true(s); |
| 10388 |
} |
| 10389 |
if ((s = ast_variable_retrieve(cfg, "general", "stunaddr_reresolve_ttl_0"))) { |
| 10390 |
stunaddr_reresolve_ttl_0 = ast_true(s); |
| 10391 |
} |
| 10392 |
if ((s = ast_variable_retrieve(cfg, "general", "stunaddr"))) { |
| 10393 |
char *hostport, *host, *port; |
| 10394 |
unsigned int port_parsed = STANDARD_STUN_PORT; |
| 10395 |
struct ast_sockaddr stunaddr_parsed; |
| 10396 |
|
| 10397 |
hostport = ast_strdupa(s); |
| 10398 |
|
| 10399 |
if (!ast_parse_arg(hostport, PARSE_ADDR, &stunaddr_parsed)) { |
| 10400 |
ast_debug_stun(3, "stunaddr = '%s' does not need name resolution\n", |
| 10401 |
ast_sockaddr_stringify_host(&stunaddr_parsed)); |
| 10402 |
if (!ast_sockaddr_port(&stunaddr_parsed)) { |
| 10403 |
ast_sockaddr_set_port(&stunaddr_parsed, STANDARD_STUN_PORT); |
| 10404 |
} |
| 10405 |
ast_rwlock_wrlock(&stunaddr_lock); |
| 10406 |
ast_sockaddr_to_sin(&stunaddr_parsed, &stunaddr); |
| 10407 |
|
| 10408 |
stunaddr_ttl = -1; |
| 10409 |
ast_rwlock_unlock(&stunaddr_lock); |
| 10410 |
} else if (ast_sockaddr_split_hostport(hostport, &host, &port, 0)) { |
| 10411 |
if (port) { |
| 10412 |
ast_parse_arg(port, PARSE_UINT32|PARSE_IN_RANGE, &port_parsed, 1, 65535); |
| 10413 |
} |
| 10414 |
|
| 10415 |
ast_rwlock_wrlock(&stunaddr_lock); |
| 10416 |
|
| 10417 |
stunaddr.sin_port = htons(port_parsed); |
| 10418 |
ast_free(stun_hostname); |
| 10419 |
stun_hostname = ast_strdup(host); |
| 10420 |
if (!stun_hostname) { |
| 10421 |
ast_log(LOG_ERROR, "Failed to set stun_hostname from '%s'", host); |
| 10422 |
} else { |
| 10423 |
stunaddr_resolver = ast_dns_resolve_recurring(host, T_A, C_IN, |
| 10424 |
&stunaddr_resolve_callback, NULL); |
| 10425 |
if (!stunaddr_resolver) { |
| 10426 |
ast_log(LOG_ERROR, "Failed to setup recurring DNS resolution of stunaddr '%s'", |
| 10427 |
host); |
| 10428 |
} else { |
| 10429 |
ast_debug_stun(2, "Attemping to start recurring stun hostname resolution for '%s'\n", stun_hostname); |
| 10430 |
} |
| 10431 |
|
| 10432 |
|
| 10433 |
|
| 10434 |
|
| 10435 |
|
| 10436 |
|
| 10437 |
stunaddr_ttl = 0; |
| 10438 |
} |
| 10439 |
ast_rwlock_unlock(&stunaddr_lock); |
| 10440 |
|
| 10441 |
|
| 10442 |
} else { |
| 10443 |
ast_log(LOG_ERROR, "Failed to parse stunaddr '%s'", hostport); |
| 10444 |
} |
| 10445 |
} |
| 10446 |
if ((s = ast_variable_retrieve(cfg, "general", "turnaddr"))) { |
| 10447 |
struct sockaddr_in addr; |
| 10448 |
addr.sin_port = htons(DEFAULT_TURN_PORT); |
| 10449 |
if (ast_parse_arg(s, PARSE_INADDR, &addr)) { |
| 10450 |
ast_log(LOG_WARNING, "Invalid TURN server address: %s\n", s); |
| 10451 |
} else { |
| 10452 |
pj_strdup2_with_null(pool, &turnaddr, ast_inet_ntoa(addr.sin_addr)); |
| 10453 |
|
| 10454 |
|
| 10455 |
turnport = ntohs(addr.sin_port); |
| 10456 |
} |
| 10457 |
} |
| 10458 |
if ((s = ast_variable_retrieve(cfg, "general", "turnusername"))) { |
| 10459 |
pj_strdup2_with_null(pool, &turnusername, s); |
| 10460 |
} |
| 10461 |
if ((s = ast_variable_retrieve(cfg, "general", "turnpassword"))) { |
| 10462 |
pj_strdup2_with_null(pool, &turnpassword, s); |
| 10463 |
} |
| 10464 |
|
| 10465 |
AST_RWLIST_WRLOCK(&host_candidates); |
| 10466 |
for (var = ast_variable_browse(cfg, "ice_host_candidates"); var; var = var->next) { |
| 10467 |
struct ast_sockaddr local_addr, advertised_addr; |
| 10468 |
unsigned int include_local_address = 0; |
| 10469 |
char *sep; |
| 10470 |
|
| 10471 |
ast_sockaddr_setnull(&local_addr); |
| 10472 |
ast_sockaddr_setnull(&advertised_addr); |
| 10473 |
|
| 10474 |
if (ast_parse_arg(var->name, PARSE_ADDR | PARSE_PORT_IGNORE, &local_addr)) { |
| 10475 |
ast_log(LOG_WARNING, "Invalid local ICE host address: %s\n", var->name); |
| 10476 |
continue; |
| 10477 |
} |
| 10478 |
|
| 10479 |
sep = strchr((char *)var->value,','); |
| 10480 |
if (sep) { |
| 10481 |
*sep = '\0'; |
| 10482 |
sep++; |
| 10483 |
sep = ast_skip_blanks(sep); |
| 10484 |
include_local_address = strcmp(sep, "include_local_address") == 0; |
| 10485 |
} |
| 10486 |
|
| 10487 |
if (ast_parse_arg(var->value, PARSE_ADDR | PARSE_PORT_IGNORE, &advertised_addr)) { |
| 10488 |
ast_log(LOG_WARNING, "Invalid advertised ICE host address: %s\n", var->value); |
| 10489 |
continue; |
| 10490 |
} |
| 10491 |
|
| 10492 |
if (!(candidate = ast_calloc(1, sizeof(*candidate)))) { |
| 10493 |
ast_log(LOG_ERROR, "Failed to allocate ICE host candidate mapping.\n"); |
| 10494 |
break; |
| 10495 |
} |
| 10496 |
|
| 10497 |
candidate->include_local = include_local_address; |
| 10498 |
|
| 10499 |
ast_sockaddr_copy(&candidate->local, &local_addr); |
| 10500 |
ast_sockaddr_copy(&candidate->advertised, &advertised_addr); |
| 10501 |
|
| 10502 |
AST_RWLIST_INSERT_TAIL(&host_candidates, candidate, next); |
| 10503 |
} |
| 10504 |
AST_RWLIST_UNLOCK(&host_candidates); |
| 10505 |
|
| 10506 |
ast_rwlock_wrlock(&ice_acl_lock); |
| 10507 |
ast_rwlock_wrlock(&stun_acl_lock); |
| 10508 |
|
| 10509 |
ice_acl = ast_free_acl_list(ice_acl); |
| 10510 |
stun_acl = ast_free_acl_list(stun_acl); |
| 10511 |
|
| 10512 |
for (var = ast_variable_browse(cfg, "general"); var; var = var->next) { |
| 10513 |
const char* sense = NULL; |
| 10514 |
struct ast_acl_list **acl = NULL; |
| 10515 |
if (strncasecmp(var->name, "ice_", 4) == 0) { |
| 10516 |
sense = var->name + 4; |
| 10517 |
acl = &ice_acl; |
| 10518 |
} else if (strncasecmp(var->name, "stun_", 5) == 0) { |
| 10519 |
sense = var->name + 5; |
| 10520 |
acl = &stun_acl; |
| 10521 |
} else { |
| 10522 |
continue; |
| 10523 |
} |
| 10524 |
|
| 10525 |
if (strcasecmp(sense, "blacklist") == 0) { |
| 10526 |
sense = "deny"; |
| 10527 |
} |
| 10528 |
|
| 10529 |
if (strcasecmp(sense, "acl") && strcasecmp(sense, "permit") && strcasecmp(sense, "deny")) { |
| 10530 |
continue; |
| 10531 |
} |
| 10532 |
|
| 10533 |
ast_append_acl(sense, var->value, acl, NULL, &acl_subscription_flag); |
| 10534 |
} |
| 10535 |
ast_rwlock_unlock(&ice_acl_lock); |
| 10536 |
ast_rwlock_unlock(&stun_acl_lock); |
| 10537 |
|
| 10538 |
if (acl_subscription_flag && !acl_change_sub) { |
| 10539 |
acl_change_sub = stasis_subscribe(ast_security_topic(), acl_change_stasis_cb, NULL); |
| 10540 |
stasis_subscription_accept_message_type(acl_change_sub, ast_named_acl_change_type()); |
| 10541 |
stasis_subscription_set_filter(acl_change_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 10542 |
} else if (!acl_subscription_flag && acl_change_sub) { |
| 10543 |
acl_change_sub = stasis_unsubscribe_and_join(acl_change_sub); |
| 10544 |
} |
| 10545 |
#endif |
| 10546 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) |
| 10547 |
if ((s = ast_variable_retrieve(cfg, "general", "dtls_mtu"))) { |
| 10548 |
if ((sscanf(s, "%d", &dtls_mtu) != 1) || dtls_mtu < 256) { |
| 10549 |
ast_log(LOG_WARNING, "Value for 'dtls_mtu' could not be read, using default of '%d' instead\n", |
| 10550 |
DEFAULT_DTLS_MTU); |
| 10551 |
dtls_mtu = DEFAULT_DTLS_MTU; |
| 10552 |
} |
| 10553 |
} |
| 10554 |
#endif |
| 10555 |
|
| 10556 |
ast_config_destroy(cfg); |
| 10557 |
|
| 10558 |
|
| 10559 |
|
| 10560 |
if (rtpstart & 1) { |
| 10561 |
rtpstart++; |
| 10562 |
ast_log(LOG_WARNING, "Odd start value for RTP port in rtp.conf, rounding up to %d\n", rtpstart); |
| 10563 |
} |
| 10564 |
|
| 10565 |
if (rtpstart >= rtpend) { |
| 10566 |
ast_log(LOG_WARNING, "Unreasonable values for RTP start/end port in rtp.conf\n"); |
| 10567 |
rtpstart = DEFAULT_RTP_START; |
| 10568 |
rtpend = DEFAULT_RTP_END; |
| 10569 |
} |
| 10570 |
ast_verb(2, "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend); |
| 10571 |
return 0; |
| 10572 |
} |
| 10573 |
|
| 10574 |
static int reload_module(void) |
| 10575 |
{ |
| 10576 |
rtp_reload(1, 0); |
| 10577 |
return 0; |
| 10578 |
} |
| 10579 |
|
| 10580 |
#ifdef HAVE_PJPROJECT |
| 10581 |
static void rtp_terminate_pjproject(void) |
| 10582 |
{ |
| 10583 |
pj_thread_register_check(); |
| 10584 |
|
| 10585 |
if (timer_thread) { |
| 10586 |
timer_terminate = 1; |
| 10587 |
pj_thread_join(timer_thread); |
| 10588 |
pj_thread_destroy(timer_thread); |
| 10589 |
} |
| 10590 |
|
| 10591 |
ast_pjproject_caching_pool_destroy(&cachingpool); |
| 10592 |
pj_shutdown(); |
| 10593 |
} |
| 10594 |
|
| 10595 |
static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 10596 |
{ |
| 10597 |
if (stasis_message_type(message) != ast_named_acl_change_type()) { |
| 10598 |
return; |
| 10599 |
} |
| 10600 |
|
| 10601 |
|
| 10602 |
rtp_reload(1, 1); |
| 10603 |
} |
| 10604 |
#endif |
| 10605 |
|
| 10606 |
static int load_module(void) |
| 10607 |
{ |
| 10608 |
#ifdef HAVE_PJPROJECT |
| 10609 |
pj_lock_t *lock; |
| 10610 |
|
| 10611 |
ast_sockaddr_parse(&lo6, "::1", PARSE_PORT_IGNORE); |
| 10612 |
|
| 10613 |
AST_PJPROJECT_INIT_LOG_LEVEL(); |
| 10614 |
if (pj_init() != PJ_SUCCESS) { |
| 10615 |
return AST_MODULE_LOAD_DECLINE; |
| 10616 |
} |
| 10617 |
|
| 10618 |
if (pjlib_util_init() != PJ_SUCCESS) { |
| 10619 |
rtp_terminate_pjproject(); |
| 10620 |
return AST_MODULE_LOAD_DECLINE; |
| 10621 |
} |
| 10622 |
|
| 10623 |
if (pjnath_init() != PJ_SUCCESS) { |
| 10624 |
rtp_terminate_pjproject(); |
| 10625 |
return AST_MODULE_LOAD_DECLINE; |
| 10626 |
} |
| 10627 |
|
| 10628 |
ast_pjproject_caching_pool_init(&cachingpool, &pj_pool_factory_default_policy, 0); |
| 10629 |
|
| 10630 |
pool = pj_pool_create(&cachingpool.factory, "timer", 512, 512, NULL); |
| 10631 |
|
| 10632 |
if (pj_timer_heap_create(pool, 100, &timer_heap) != PJ_SUCCESS) { |
| 10633 |
rtp_terminate_pjproject(); |
| 10634 |
return AST_MODULE_LOAD_DECLINE; |
| 10635 |
} |
| 10636 |
|
| 10637 |
if (pj_lock_create_recursive_mutex(pool, "rtp%p", &lock) != PJ_SUCCESS) { |
| 10638 |
rtp_terminate_pjproject(); |
| 10639 |
return AST_MODULE_LOAD_DECLINE; |
| 10640 |
} |
| 10641 |
|
| 10642 |
pj_timer_heap_set_lock(timer_heap, lock, PJ_TRUE); |
| 10643 |
|
| 10644 |
if (pj_thread_create(pool, "timer", &timer_worker_thread, NULL, 0, 0, &timer_thread) != PJ_SUCCESS) { |
| 10645 |
rtp_terminate_pjproject(); |
| 10646 |
return AST_MODULE_LOAD_DECLINE; |
| 10647 |
} |
| 10648 |
|
| 10649 |
#endif |
| 10650 |
|
| 10651 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD) |
| 10652 |
dtls_bio_methods = BIO_meth_new(BIO_TYPE_BIO, "rtp write"); |
| 10653 |
if (!dtls_bio_methods) { |
| 10654 |
#ifdef HAVE_PJPROJECT |
| 10655 |
rtp_terminate_pjproject(); |
| 10656 |
#endif |
| 10657 |
return AST_MODULE_LOAD_DECLINE; |
| 10658 |
} |
| 10659 |
BIO_meth_set_write(dtls_bio_methods, dtls_bio_write); |
| 10660 |
BIO_meth_set_ctrl(dtls_bio_methods, dtls_bio_ctrl); |
| 10661 |
BIO_meth_set_create(dtls_bio_methods, dtls_bio_new); |
| 10662 |
BIO_meth_set_destroy(dtls_bio_methods, dtls_bio_free); |
| 10663 |
#endif |
| 10664 |
|
| 10665 |
if (ast_rtp_engine_register(&asterisk_rtp_engine)) { |
| 10666 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD) |
| 10667 |
BIO_meth_free(dtls_bio_methods); |
| 10668 |
#endif |
| 10669 |
#ifdef HAVE_PJPROJECT |
| 10670 |
rtp_terminate_pjproject(); |
| 10671 |
#endif |
| 10672 |
return AST_MODULE_LOAD_DECLINE; |
| 10673 |
} |
| 10674 |
|
| 10675 |
if (ast_cli_register_multiple(cli_rtp, ARRAY_LEN(cli_rtp))) { |
| 10676 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD) |
| 10677 |
BIO_meth_free(dtls_bio_methods); |
| 10678 |
#endif |
| 10679 |
#ifdef HAVE_PJPROJECT |
| 10680 |
ast_rtp_engine_unregister(&asterisk_rtp_engine); |
| 10681 |
rtp_terminate_pjproject(); |
| 10682 |
#endif |
| 10683 |
return AST_MODULE_LOAD_DECLINE; |
| 10684 |
} |
| 10685 |
|
| 10686 |
rtp_reload(0, 0); |
| 10687 |
|
| 10688 |
return AST_MODULE_LOAD_SUCCESS; |
| 10689 |
} |
| 10690 |
|
| 10691 |
static int unload_module(void) |
| 10692 |
{ |
| 10693 |
ast_rtp_engine_unregister(&asterisk_rtp_engine); |
| 10694 |
ast_cli_unregister_multiple(cli_rtp, ARRAY_LEN(cli_rtp)); |
| 10695 |
|
| 10696 |
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD) |
| 10697 |
if (dtls_bio_methods) { |
| 10698 |
BIO_meth_free(dtls_bio_methods); |
| 10699 |
} |
| 10700 |
#endif |
| 10701 |
|
| 10702 |
#ifdef HAVE_PJPROJECT |
| 10703 |
host_candidate_overrides_clear(); |
| 10704 |
pj_thread_register_check(); |
| 10705 |
rtp_terminate_pjproject(); |
| 10706 |
|
| 10707 |
acl_change_sub = stasis_unsubscribe_and_join(acl_change_sub); |
| 10708 |
rtp_unload_acl(&ice_acl_lock, &ice_acl); |
| 10709 |
rtp_unload_acl(&stun_acl_lock, &stun_acl); |
| 10710 |
clean_stunaddr(); |
| 10711 |
#endif |
| 10712 |
|
| 10713 |
return 0; |
| 10714 |
} |
| 10715 |
|
| 10716 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk RTP Stack", |
| 10717 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 10718 |
.load = load_module, |
| 10719 |
.unload = unload_module, |
| 10720 |
.reload = reload_module, |
| 10721 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND, |
| 10722 |
#ifdef HAVE_PJPROJECT |
| 10723 |
.requires = "res_pjproject", |
| 10724 |
#endif |
| 10725 |
); |