00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "asterisk.h"
00036
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 346292 $")
00038
00039 #include <sys/time.h>
00040 #include <signal.h>
00041 #include <fcntl.h>
00042
00043 #include "asterisk/stun.h"
00044 #include "asterisk/pbx.h"
00045 #include "asterisk/frame.h"
00046 #include "asterisk/channel.h"
00047 #include "asterisk/acl.h"
00048 #include "asterisk/config.h"
00049 #include "asterisk/lock.h"
00050 #include "asterisk/utils.h"
00051 #include "asterisk/cli.h"
00052 #include "asterisk/manager.h"
00053 #include "asterisk/unaligned.h"
00054 #include "asterisk/module.h"
00055 #include "asterisk/rtp_engine.h"
00056
00057 #define MAX_TIMESTAMP_SKEW 640
00058
00059 #define RTP_SEQ_MOD (1<<16)
00060 #define RTCP_DEFAULT_INTERVALMS 5000
00061 #define RTCP_MIN_INTERVALMS 500
00062 #define RTCP_MAX_INTERVALMS 60000
00063
00064 #define DEFAULT_RTP_START 5000
00065 #define DEFAULT_RTP_END 31000
00066
00067 #define MINIMUM_RTP_PORT 1024
00068 #define MAXIMUM_RTP_PORT 65535
00069
00070 #define RTCP_PT_FUR 192
00071 #define RTCP_PT_SR 200
00072 #define RTCP_PT_RR 201
00073 #define RTCP_PT_SDES 202
00074 #define RTCP_PT_BYE 203
00075 #define RTCP_PT_APP 204
00076
00077 #define RTP_MTU 1200
00078
00079 #define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000))
00080
00081 #define ZFONE_PROFILE_ID 0x505a
00082
00083 extern struct ast_srtp_res *res_srtp;
00084 static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
00085
00086 static int rtpstart = DEFAULT_RTP_START;
00087 static int rtpend = DEFAULT_RTP_END;
00088 static int rtpdebug;
00089 static int rtcpdebug;
00090 static int rtcpstats;
00091 static int rtcpinterval = RTCP_DEFAULT_INTERVALMS;
00092 static struct ast_sockaddr rtpdebugaddr;
00093 static struct ast_sockaddr rtcpdebugaddr;
00094 static int rtpdebugport;
00095 static int rtcpdebugport;
00096 #ifdef SO_NO_CHECK
00097 static int nochecksums;
00098 #endif
00099 static int strictrtp;
00100
00101 enum strict_rtp_state {
00102 STRICT_RTP_OPEN = 0,
00103 STRICT_RTP_LEARN,
00104 STRICT_RTP_CLOSED,
00105 };
00106
00107 #define FLAG_3389_WARNING (1 << 0)
00108 #define FLAG_NAT_ACTIVE (3 << 1)
00109 #define FLAG_NAT_INACTIVE (0 << 1)
00110 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
00111 #define FLAG_NEED_MARKER_BIT (1 << 3)
00112 #define FLAG_DTMF_COMPENSATE (1 << 4)
00113
00114
00115 struct ast_rtp {
00116 int s;
00117 struct ast_frame f;
00118 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
00119 unsigned int ssrc;
00120 unsigned int themssrc;
00121 unsigned int rxssrc;
00122 unsigned int lastts;
00123 unsigned int lastrxts;
00124 unsigned int lastividtimestamp;
00125 unsigned int lastovidtimestamp;
00126 unsigned int lastitexttimestamp;
00127 unsigned int lastotexttimestamp;
00128 unsigned int lasteventseqn;
00129 int lastrxseqno;
00130 unsigned short seedrxseqno;
00131 unsigned int seedrxts;
00132 unsigned int rxcount;
00133 unsigned int rxoctetcount;
00134 unsigned int txcount;
00135 unsigned int txoctetcount;
00136 unsigned int cycles;
00137 double rxjitter;
00138 double rxtransit;
00139 format_t lasttxformat;
00140 format_t lastrxformat;
00141
00142 int rtptimeout;
00143 int rtpholdtimeout;
00144 int rtpkeepalive;
00145
00146
00147 char resp;
00148 unsigned int lastevent;
00149 unsigned int dtmf_duration;
00150 unsigned int dtmf_timeout;
00151 unsigned int dtmfsamples;
00152 enum ast_rtp_dtmf_mode dtmfmode;
00153
00154 unsigned int lastdigitts;
00155 char sending_digit;
00156 char send_digit;
00157 int send_payload;
00158 int send_duration;
00159 unsigned int flags;
00160 struct timeval rxcore;
00161 struct timeval txcore;
00162 double drxcore;
00163 struct timeval lastrx;
00164 struct timeval dtmfmute;
00165 struct ast_smoother *smoother;
00166 int *ioid;
00167 unsigned short seqno;
00168 unsigned short rxseqno;
00169 struct sched_context *sched;
00170 struct io_context *io;
00171 void *data;
00172 struct ast_rtcp *rtcp;
00173 struct ast_rtp *bridged;
00174
00175 enum strict_rtp_state strict_rtp_state;
00176 struct ast_sockaddr strict_rtp_address;
00177 struct ast_sockaddr alt_rtp_address;
00178
00179 struct rtp_red *red;
00180 };
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 struct ast_rtcp {
00193 int rtcp_info;
00194 int s;
00195 struct ast_sockaddr us;
00196 struct ast_sockaddr them;
00197 unsigned int soc;
00198 unsigned int spc;
00199 unsigned int themrxlsr;
00200 struct timeval rxlsr;
00201 struct timeval txlsr;
00202 unsigned int expected_prior;
00203 unsigned int received_prior;
00204 int schedid;
00205 unsigned int rr_count;
00206 unsigned int sr_count;
00207 unsigned int lastsrtxcount;
00208 double accumulated_transit;
00209 double rtt;
00210 unsigned int reported_jitter;
00211 unsigned int reported_lost;
00212
00213 double reported_maxjitter;
00214 double reported_minjitter;
00215 double reported_normdev_jitter;
00216 double reported_stdev_jitter;
00217 unsigned int reported_jitter_count;
00218
00219 double reported_maxlost;
00220 double reported_minlost;
00221 double reported_normdev_lost;
00222 double reported_stdev_lost;
00223
00224 double rxlost;
00225 double maxrxlost;
00226 double minrxlost;
00227 double normdev_rxlost;
00228 double stdev_rxlost;
00229 unsigned int rxlost_count;
00230
00231 double maxrxjitter;
00232 double minrxjitter;
00233 double normdev_rxjitter;
00234 double stdev_rxjitter;
00235 unsigned int rxjitter_count;
00236 double maxrtt;
00237 double minrtt;
00238 double normdevrtt;
00239 double stdevrtt;
00240 unsigned int rtt_count;
00241 };
00242
00243 struct rtp_red {
00244 struct ast_frame t140;
00245 struct ast_frame t140red;
00246 unsigned char pt[AST_RED_MAX_GENERATION];
00247 unsigned char ts[AST_RED_MAX_GENERATION];
00248 unsigned char len[AST_RED_MAX_GENERATION];
00249 int num_gen;
00250 int schedid;
00251 int ti;
00252 unsigned char t140red_data[64000];
00253 unsigned char buf_data[64000];
00254 int hdrlen;
00255 long int prev_ts;
00256 };
00257
00258 AST_LIST_HEAD_NOLOCK(frame_list, ast_frame);
00259
00260
00261 static int ast_rtp_new(struct ast_rtp_instance *instance, struct sched_context *sched, struct ast_sockaddr *addr, void *data);
00262 static int ast_rtp_destroy(struct ast_rtp_instance *instance);
00263 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit);
00264 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit);
00265 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration);
00266 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode);
00267 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance);
00268 static void ast_rtp_update_source(struct ast_rtp_instance *instance);
00269 static void ast_rtp_change_source(struct ast_rtp_instance *instance);
00270 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame);
00271 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp);
00272 static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value);
00273 static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp);
00274 static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
00275 static void ast_rtp_alt_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
00276 static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations);
00277 static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame);
00278 static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1);
00279 static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat);
00280 static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1);
00281 static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username);
00282 static void ast_rtp_stop(struct ast_rtp_instance *instance);
00283 static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char* desc);
00284 static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level);
00285
00286
00287 static struct ast_rtp_engine asterisk_rtp_engine = {
00288 .name = "asterisk",
00289 .new = ast_rtp_new,
00290 .destroy = ast_rtp_destroy,
00291 .dtmf_begin = ast_rtp_dtmf_begin,
00292 .dtmf_end = ast_rtp_dtmf_end,
00293 .dtmf_end_with_duration = ast_rtp_dtmf_end_with_duration,
00294 .dtmf_mode_set = ast_rtp_dtmf_mode_set,
00295 .dtmf_mode_get = ast_rtp_dtmf_mode_get,
00296 .update_source = ast_rtp_update_source,
00297 .change_source = ast_rtp_change_source,
00298 .write = ast_rtp_write,
00299 .read = ast_rtp_read,
00300 .prop_set = ast_rtp_prop_set,
00301 .fd = ast_rtp_fd,
00302 .remote_address_set = ast_rtp_remote_address_set,
00303 .alt_remote_address_set = ast_rtp_alt_remote_address_set,
00304 .red_init = rtp_red_init,
00305 .red_buffer = rtp_red_buffer,
00306 .local_bridge = ast_rtp_local_bridge,
00307 .get_stat = ast_rtp_get_stat,
00308 .dtmf_compatible = ast_rtp_dtmf_compatible,
00309 .stun_request = ast_rtp_stun_request,
00310 .stop = ast_rtp_stop,
00311 .qos = ast_rtp_qos_set,
00312 .sendcng = ast_rtp_sendcng,
00313 };
00314
00315 static inline int rtp_debug_test_addr(struct ast_sockaddr *addr)
00316 {
00317 if (!rtpdebug) {
00318 return 0;
00319 }
00320 if (!ast_sockaddr_isnull(&rtpdebugaddr)) {
00321 if (rtpdebugport) {
00322 return (ast_sockaddr_cmp(&rtpdebugaddr, addr) == 0);
00323 } else {
00324 return (ast_sockaddr_cmp_addr(&rtpdebugaddr, addr) == 0);
00325 }
00326 }
00327
00328 return 1;
00329 }
00330
00331 static inline int rtcp_debug_test_addr(struct ast_sockaddr *addr)
00332 {
00333 if (!rtcpdebug) {
00334 return 0;
00335 }
00336 if (!ast_sockaddr_isnull(&rtcpdebugaddr)) {
00337 if (rtcpdebugport) {
00338 return (ast_sockaddr_cmp(&rtcpdebugaddr, addr) == 0);
00339 } else {
00340 return (ast_sockaddr_cmp_addr(&rtcpdebugaddr, addr) == 0);
00341 }
00342 }
00343
00344 return 1;
00345 }
00346
00347 static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp)
00348 {
00349 int len;
00350 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00351 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
00352
00353 if ((len = ast_recvfrom(rtcp ? rtp->rtcp->s : rtp->s, buf, size, flags, sa)) < 0) {
00354 return len;
00355 }
00356
00357 if (res_srtp && srtp && res_srtp->unprotect(srtp, buf, &len, rtcp) < 0) {
00358 return -1;
00359 }
00360
00361 return len;
00362 }
00363
00364 static int rtcp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
00365 {
00366 return __rtp_recvfrom(instance, buf, size, flags, sa, 1);
00367 }
00368
00369 static int rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
00370 {
00371 return __rtp_recvfrom(instance, buf, size, flags, sa, 0);
00372 }
00373
00374 static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp)
00375 {
00376 int len = size;
00377 void *temp = buf;
00378 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00379 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
00380
00381 if (res_srtp && srtp && res_srtp->protect(srtp, &temp, &len, rtcp) < 0) {
00382 return -1;
00383 }
00384
00385 return ast_sendto(rtcp ? rtp->rtcp->s : rtp->s, temp, len, flags, sa);
00386 }
00387
00388 static int rtcp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
00389 {
00390 return __rtp_sendto(instance, buf, size, flags, sa, 1);
00391 }
00392
00393 static int rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
00394 {
00395 return __rtp_sendto(instance, buf, size, flags, sa, 0);
00396 }
00397
00398 static int rtp_get_rate(format_t subclass)
00399 {
00400 return (subclass == AST_FORMAT_G722) ? 8000 : ast_format_rate(subclass);
00401 }
00402
00403 static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
00404 {
00405 unsigned int interval;
00406
00407
00408 interval = rtcpinterval;
00409 return interval;
00410 }
00411
00412
00413 static double normdev_compute(double normdev, double sample, unsigned int sample_count)
00414 {
00415 normdev = normdev * sample_count + sample;
00416 sample_count++;
00417
00418 return normdev / sample_count;
00419 }
00420
00421 static double stddev_compute(double stddev, double sample, double normdev, double normdev_curent, unsigned int sample_count)
00422 {
00423
00424
00425
00426
00427
00428
00429 #define SQUARE(x) ((x) * (x))
00430
00431 stddev = sample_count * stddev;
00432 sample_count++;
00433
00434 return stddev +
00435 ( sample_count * SQUARE( (sample - normdev) / sample_count ) ) +
00436 ( SQUARE(sample - normdev_curent) / sample_count );
00437
00438 #undef SQUARE
00439 }
00440
00441 static int create_new_socket(const char *type, int af)
00442 {
00443 int sock = socket(af, SOCK_DGRAM, 0);
00444
00445 if (sock < 0) {
00446 if (!type) {
00447 type = "RTP/RTCP";
00448 }
00449 ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
00450 } else {
00451 long flags = fcntl(sock, F_GETFL);
00452 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
00453 #ifdef SO_NO_CHECK
00454 if (nochecksums) {
00455 setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
00456 }
00457 #endif
00458 }
00459
00460 return sock;
00461 }
00462
00463 static int ast_rtp_new(struct ast_rtp_instance *instance,
00464 struct sched_context *sched, struct ast_sockaddr *addr,
00465 void *data)
00466 {
00467 struct ast_rtp *rtp = NULL;
00468 int x, startplace;
00469
00470
00471 if (!(rtp = ast_calloc(1, sizeof(*rtp)))) {
00472 return -1;
00473 }
00474
00475
00476 rtp->ssrc = ast_random();
00477 rtp->seqno = ast_random() & 0xffff;
00478 rtp->strict_rtp_state = (strictrtp ? STRICT_RTP_LEARN : STRICT_RTP_OPEN);
00479
00480
00481 if ((rtp->s =
00482 create_new_socket("RTP",
00483 ast_sockaddr_is_ipv4(addr) ? AF_INET :
00484 ast_sockaddr_is_ipv6(addr) ? AF_INET6 : -1)) < 0) {
00485 ast_debug(1, "Failed to create a new socket for RTP instance '%p'\n", instance);
00486 ast_free(rtp);
00487 return -1;
00488 }
00489
00490
00491 x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
00492 x = x & ~1;
00493 startplace = x;
00494
00495 for (;;) {
00496 ast_sockaddr_set_port(addr, x);
00497
00498 if (!ast_bind(rtp->s, addr)) {
00499 ast_debug(1, "Allocated port %d for RTP instance '%p'\n", x, instance);
00500 ast_rtp_instance_set_local_address(instance, addr);
00501 break;
00502 }
00503
00504 x += 2;
00505 if (x > rtpend) {
00506 x = (rtpstart + 1) & ~1;
00507 }
00508
00509
00510 if (x == startplace || errno != EADDRINUSE) {
00511 ast_log(LOG_ERROR, "Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance);
00512 return -1;
00513 }
00514 }
00515
00516
00517 rtp->sched = sched;
00518
00519
00520 ast_rtp_instance_set_data(instance, rtp);
00521
00522 return 0;
00523 }
00524
00525 static int ast_rtp_destroy(struct ast_rtp_instance *instance)
00526 {
00527 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00528
00529
00530 if (rtp->smoother) {
00531 ast_smoother_free(rtp->smoother);
00532 }
00533
00534
00535 if (rtp->s > -1) {
00536 close(rtp->s);
00537 }
00538
00539
00540 if (rtp->rtcp) {
00541
00542
00543
00544
00545
00546 close(rtp->rtcp->s);
00547 ast_free(rtp->rtcp);
00548 }
00549
00550
00551 if (rtp->red) {
00552 AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
00553 ast_free(rtp->red);
00554 }
00555
00556
00557 ast_free(rtp);
00558
00559 return 0;
00560 }
00561
00562 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
00563 {
00564 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00565 rtp->dtmfmode = dtmf_mode;
00566 return 0;
00567 }
00568
00569 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance)
00570 {
00571 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00572 return rtp->dtmfmode;
00573 }
00574
00575 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit)
00576 {
00577 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00578 struct ast_sockaddr remote_address = { {0,} };
00579 int hdrlen = 12, res = 0, i = 0, payload = 101;
00580 char data[256];
00581 unsigned int *rtpheader = (unsigned int*)data;
00582
00583 ast_rtp_instance_get_remote_address(instance, &remote_address);
00584
00585
00586 if (ast_sockaddr_isnull(&remote_address)) {
00587 return -1;
00588 }
00589
00590
00591 if ((digit <= '9') && (digit >= '0')) {
00592 digit -= '0';
00593 } else if (digit == '*') {
00594 digit = 10;
00595 } else if (digit == '#') {
00596 digit = 11;
00597 } else if ((digit >= 'A') && (digit <= 'D')) {
00598 digit = digit - 'A' + 12;
00599 } else if ((digit >= 'a') && (digit <= 'd')) {
00600 digit = digit - 'a' + 12;
00601 } else {
00602 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
00603 return -1;
00604 }
00605
00606
00607 payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 0, AST_RTP_DTMF);
00608
00609 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
00610 rtp->send_duration = 160;
00611 rtp->lastdigitts = rtp->lastts + rtp->send_duration;
00612
00613
00614 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
00615 rtpheader[1] = htonl(rtp->lastdigitts);
00616 rtpheader[2] = htonl(rtp->ssrc);
00617
00618
00619 for (i = 0; i < 2; i++) {
00620 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
00621 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address);
00622 if (res < 0) {
00623 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
00624 ast_sockaddr_stringify(&remote_address),
00625 strerror(errno));
00626 }
00627 if (rtp_debug_test_addr(&remote_address)) {
00628 ast_verbose("Sent RTP DTMF packet to %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
00629 ast_sockaddr_stringify(&remote_address),
00630 payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
00631 }
00632 rtp->seqno++;
00633 rtp->send_duration += 160;
00634 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
00635 }
00636
00637
00638 rtp->sending_digit = 1;
00639 rtp->send_digit = digit;
00640 rtp->send_payload = payload;
00641
00642 return 0;
00643 }
00644
00645 static int ast_rtp_dtmf_continuation(struct ast_rtp_instance *instance)
00646 {
00647 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00648 struct ast_sockaddr remote_address = { {0,} };
00649 int hdrlen = 12, res = 0;
00650 char data[256];
00651 unsigned int *rtpheader = (unsigned int*)data;
00652
00653 ast_rtp_instance_get_remote_address(instance, &remote_address);
00654
00655
00656 if (ast_sockaddr_isnull(&remote_address)) {
00657 return -1;
00658 }
00659
00660
00661 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (rtp->send_payload << 16) | (rtp->seqno));
00662 rtpheader[1] = htonl(rtp->lastdigitts);
00663 rtpheader[2] = htonl(rtp->ssrc);
00664 rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
00665 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
00666
00667
00668 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address);
00669 if (res < 0) {
00670 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
00671 ast_sockaddr_stringify(&remote_address),
00672 strerror(errno));
00673 }
00674
00675 if (rtp_debug_test_addr(&remote_address)) {
00676 ast_verbose("Sent RTP DTMF packet to %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
00677 ast_sockaddr_stringify(&remote_address),
00678 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
00679 }
00680
00681
00682 rtp->seqno++;
00683 rtp->send_duration += 160;
00684
00685 return 0;
00686 }
00687
00688 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
00689 {
00690 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00691 struct ast_sockaddr remote_address = { {0,} };
00692 int hdrlen = 12, res = 0, i = 0;
00693 char data[256];
00694 unsigned int *rtpheader = (unsigned int*)data;
00695 unsigned int measured_samples;
00696
00697 ast_rtp_instance_get_remote_address(instance, &remote_address);
00698
00699
00700 if (ast_sockaddr_isnull(&remote_address)) {
00701 return -1;
00702 }
00703
00704
00705 if ((digit <= '9') && (digit >= '0')) {
00706 digit -= '0';
00707 } else if (digit == '*') {
00708 digit = 10;
00709 } else if (digit == '#') {
00710 digit = 11;
00711 } else if ((digit >= 'A') && (digit <= 'D')) {
00712 digit = digit - 'A' + 12;
00713 } else if ((digit >= 'a') && (digit <= 'd')) {
00714 digit = digit - 'a' + 12;
00715 } else {
00716 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
00717 return -1;
00718 }
00719
00720 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
00721
00722 if (duration > 0 && (measured_samples = duration * rtp_get_rate(rtp->f.subclass.codec) / 1000) > rtp->send_duration) {
00723 ast_debug(2, "Adjusting final end duration from %u to %u\n", rtp->send_duration, measured_samples);
00724 rtp->send_duration = measured_samples;
00725 }
00726
00727
00728 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (rtp->send_payload << 16) | (rtp->seqno));
00729 rtpheader[1] = htonl(rtp->lastdigitts);
00730 rtpheader[2] = htonl(rtp->ssrc);
00731 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
00732 rtpheader[3] |= htonl((1 << 23));
00733 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
00734
00735
00736 for (i = 0; i < 3; i++) {
00737 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address);
00738 if (res < 0) {
00739 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
00740 ast_sockaddr_stringify(&remote_address),
00741 strerror(errno));
00742 }
00743 if (rtp_debug_test_addr(&remote_address)) {
00744 ast_verbose("Sent RTP DTMF packet to %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
00745 ast_sockaddr_stringify(&remote_address),
00746 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
00747 }
00748 }
00749
00750
00751 rtp->lastts += rtp->send_duration;
00752 rtp->sending_digit = 0;
00753 rtp->send_digit = 0;
00754
00755 return 0;
00756 }
00757
00758 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit)
00759 {
00760 return ast_rtp_dtmf_end_with_duration(instance, digit, 0);
00761 }
00762
00763 static void ast_rtp_update_source(struct ast_rtp_instance *instance)
00764 {
00765 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00766
00767
00768 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
00769 ast_debug(3, "Setting the marker bit due to a source update\n");
00770
00771 return;
00772 }
00773
00774 static void ast_rtp_change_source(struct ast_rtp_instance *instance)
00775 {
00776 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00777 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
00778 unsigned int ssrc = ast_random();
00779
00780 if (!rtp->lastts) {
00781 ast_debug(3, "Not changing SSRC since we haven't sent any RTP yet\n");
00782 return;
00783 }
00784
00785
00786 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
00787
00788 ast_debug(3, "Changing ssrc from %u to %u due to a source change\n", rtp->ssrc, ssrc);
00789
00790 if (srtp) {
00791 ast_debug(3, "Changing ssrc for SRTP from %u to %u\n", rtp->ssrc, ssrc);
00792 res_srtp->change_source(srtp, rtp->ssrc, ssrc);
00793 }
00794
00795 rtp->ssrc = ssrc;
00796
00797 return;
00798 }
00799
00800 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
00801 {
00802 struct timeval t;
00803 long ms;
00804
00805 if (ast_tvzero(rtp->txcore)) {
00806 rtp->txcore = ast_tvnow();
00807 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
00808 }
00809
00810 t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
00811 if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) {
00812 ms = 0;
00813 }
00814 rtp->txcore = t;
00815
00816 return (unsigned int) ms;
00817 }
00818
00819 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
00820 {
00821 unsigned int sec, usec, frac;
00822 sec = tv.tv_sec + 2208988800u;
00823 usec = tv.tv_usec;
00824 frac = (usec << 12) + (usec << 8) - ((usec * 3650) >> 6);
00825 *msw = sec;
00826 *lsw = frac;
00827 }
00828
00829
00830 static int ast_rtcp_write_rr(struct ast_rtp_instance *instance)
00831 {
00832 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00833 int res;
00834 int len = 32;
00835 unsigned int lost;
00836 unsigned int extended;
00837 unsigned int expected;
00838 unsigned int expected_interval;
00839 unsigned int received_interval;
00840 int lost_interval;
00841 struct timeval now;
00842 unsigned int *rtcpheader;
00843 char bdata[1024];
00844 struct timeval dlsr;
00845 int fraction;
00846
00847 double rxlost_current;
00848
00849 if (!rtp || !rtp->rtcp)
00850 return 0;
00851
00852 if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
00853
00854
00855
00856 return 0;
00857 }
00858
00859 extended = rtp->cycles + rtp->lastrxseqno;
00860 expected = extended - rtp->seedrxseqno + 1;
00861 lost = expected - rtp->rxcount;
00862 expected_interval = expected - rtp->rtcp->expected_prior;
00863 rtp->rtcp->expected_prior = expected;
00864 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
00865 rtp->rtcp->received_prior = rtp->rxcount;
00866 lost_interval = expected_interval - received_interval;
00867
00868 if (lost_interval <= 0)
00869 rtp->rtcp->rxlost = 0;
00870 else rtp->rtcp->rxlost = rtp->rtcp->rxlost;
00871 if (rtp->rtcp->rxlost_count == 0)
00872 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
00873 if (lost_interval < rtp->rtcp->minrxlost)
00874 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
00875 if (lost_interval > rtp->rtcp->maxrxlost)
00876 rtp->rtcp->maxrxlost = rtp->rtcp->rxlost;
00877
00878 rxlost_current = normdev_compute(rtp->rtcp->normdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->rxlost_count);
00879 rtp->rtcp->stdev_rxlost = stddev_compute(rtp->rtcp->stdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->normdev_rxlost, rxlost_current, rtp->rtcp->rxlost_count);
00880 rtp->rtcp->normdev_rxlost = rxlost_current;
00881 rtp->rtcp->rxlost_count++;
00882
00883 if (expected_interval == 0 || lost_interval <= 0)
00884 fraction = 0;
00885 else
00886 fraction = (lost_interval << 8) / expected_interval;
00887 gettimeofday(&now, NULL);
00888 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
00889 rtcpheader = (unsigned int *)bdata;
00890 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_RR << 16) | ((len/4)-1));
00891 rtcpheader[1] = htonl(rtp->ssrc);
00892 rtcpheader[2] = htonl(rtp->themssrc);
00893 rtcpheader[3] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
00894 rtcpheader[4] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
00895 rtcpheader[5] = htonl((unsigned int)(rtp->rxjitter * 65536.));
00896 rtcpheader[6] = htonl(rtp->rtcp->themrxlsr);
00897 rtcpheader[7] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
00898
00899
00900
00901 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
00902 rtcpheader[(len/4)+1] = htonl(rtp->ssrc);
00903 rtcpheader[(len/4)+2] = htonl(0x01 << 24);
00904 len += 12;
00905
00906 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &rtp->rtcp->them);
00907
00908 if (res < 0) {
00909 ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno));
00910 return 0;
00911 }
00912
00913 rtp->rtcp->rr_count++;
00914 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
00915 ast_verbose("\n* Sending RTCP RR to %s\n"
00916 " Our SSRC: %u\nTheir SSRC: %u\niFraction lost: %d\nCumulative loss: %u\n"
00917 " IA jitter: %.4f\n"
00918 " Their last SR: %u\n"
00919 " DLSR: %4.4f (sec)\n\n",
00920 ast_sockaddr_stringify(&rtp->rtcp->them),
00921 rtp->ssrc, rtp->themssrc, fraction, lost,
00922 rtp->rxjitter,
00923 rtp->rtcp->themrxlsr,
00924 (double)(ntohl(rtcpheader[7])/65536.0));
00925 }
00926
00927 return res;
00928 }
00929
00930
00931 static int ast_rtcp_write_sr(struct ast_rtp_instance *instance)
00932 {
00933 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
00934 int res;
00935 int len = 0;
00936 struct timeval now;
00937 unsigned int now_lsw;
00938 unsigned int now_msw;
00939 unsigned int *rtcpheader;
00940 unsigned int lost;
00941 unsigned int extended;
00942 unsigned int expected;
00943 unsigned int expected_interval;
00944 unsigned int received_interval;
00945 int lost_interval;
00946 int fraction;
00947 struct timeval dlsr;
00948 char bdata[512];
00949
00950 if (!rtp || !rtp->rtcp)
00951 return 0;
00952
00953 if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
00954
00955
00956
00957 return 0;
00958 }
00959
00960 gettimeofday(&now, NULL);
00961 timeval2ntp(now, &now_msw, &now_lsw);
00962 rtcpheader = (unsigned int *)bdata;
00963 rtcpheader[1] = htonl(rtp->ssrc);
00964 rtcpheader[2] = htonl(now_msw);
00965 rtcpheader[3] = htonl(now_lsw);
00966 rtcpheader[4] = htonl(rtp->lastts);
00967 rtcpheader[5] = htonl(rtp->txcount);
00968 rtcpheader[6] = htonl(rtp->txoctetcount);
00969 len += 28;
00970
00971 extended = rtp->cycles + rtp->lastrxseqno;
00972 expected = extended - rtp->seedrxseqno + 1;
00973 if (rtp->rxcount > expected)
00974 expected += rtp->rxcount - expected;
00975 lost = expected - rtp->rxcount;
00976 expected_interval = expected - rtp->rtcp->expected_prior;
00977 rtp->rtcp->expected_prior = expected;
00978 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
00979 rtp->rtcp->received_prior = rtp->rxcount;
00980 lost_interval = expected_interval - received_interval;
00981 if (expected_interval == 0 || lost_interval <= 0)
00982 fraction = 0;
00983 else
00984 fraction = (lost_interval << 8) / expected_interval;
00985 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
00986 rtcpheader[7] = htonl(rtp->themssrc);
00987 rtcpheader[8] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
00988 rtcpheader[9] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
00989 rtcpheader[10] = htonl((unsigned int)(rtp->rxjitter * 65536.));
00990 rtcpheader[11] = htonl(rtp->rtcp->themrxlsr);
00991 rtcpheader[12] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
00992 len += 24;
00993
00994 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SR << 16) | ((len/4)-1));
00995
00996
00997
00998 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
00999 rtcpheader[(len/4)+1] = htonl(rtp->ssrc);
01000 rtcpheader[(len/4)+2] = htonl(0x01 << 24);
01001 len += 12;
01002
01003 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &rtp->rtcp->them);
01004 if (res < 0) {
01005 ast_log(LOG_ERROR, "RTCP SR transmission error to %s, rtcp halted %s\n",
01006 ast_sockaddr_stringify(&rtp->rtcp->them),
01007 strerror(errno));
01008 return 0;
01009 }
01010
01011
01012 gettimeofday(&rtp->rtcp->txlsr, NULL);
01013 rtp->rtcp->sr_count++;
01014
01015 rtp->rtcp->lastsrtxcount = rtp->txcount;
01016
01017 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
01018 ast_verbose("* Sent RTCP SR to %s\n", ast_sockaddr_stringify(&rtp->rtcp->them));
01019 ast_verbose(" Our SSRC: %u\n", rtp->ssrc);
01020 ast_verbose(" Sent(NTP): %u.%010u\n", (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096);
01021 ast_verbose(" Sent(RTP): %u\n", rtp->lastts);
01022 ast_verbose(" Sent packets: %u\n", rtp->txcount);
01023 ast_verbose(" Sent octets: %u\n", rtp->txoctetcount);
01024 ast_verbose(" Report block:\n");
01025 ast_verbose(" Fraction lost: %u\n", fraction);
01026 ast_verbose(" Cumulative loss: %u\n", lost);
01027 ast_verbose(" IA jitter: %.4f\n", rtp->rxjitter);
01028 ast_verbose(" Their last SR: %u\n", rtp->rtcp->themrxlsr);
01029 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(ntohl(rtcpheader[12])/65536.0));
01030 }
01031 manager_event(EVENT_FLAG_REPORTING, "RTCPSent", "To: %s\r\n"
01032 "OurSSRC: %u\r\n"
01033 "SentNTP: %u.%010u\r\n"
01034 "SentRTP: %u\r\n"
01035 "SentPackets: %u\r\n"
01036 "SentOctets: %u\r\n"
01037 "ReportBlock:\r\n"
01038 "FractionLost: %u\r\n"
01039 "CumulativeLoss: %u\r\n"
01040 "IAJitter: %.4f\r\n"
01041 "TheirLastSR: %u\r\n"
01042 "DLSR: %4.4f (sec)\r\n",
01043 ast_sockaddr_stringify(&rtp->rtcp->them),
01044 rtp->ssrc,
01045 (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096,
01046 rtp->lastts,
01047 rtp->txcount,
01048 rtp->txoctetcount,
01049 fraction,
01050 lost,
01051 rtp->rxjitter,
01052 rtp->rtcp->themrxlsr,
01053 (double)(ntohl(rtcpheader[12])/65536.0));
01054 return res;
01055 }
01056
01057
01058
01059
01060 static int ast_rtcp_write(const void *data)
01061 {
01062 struct ast_rtp_instance *instance = (struct ast_rtp_instance *) data;
01063 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01064 int res;
01065
01066 if (!rtp || !rtp->rtcp || rtp->rtcp->schedid == -1) {
01067 ao2_ref(instance, -1);
01068 return 0;
01069 }
01070
01071 if (rtp->txcount > rtp->rtcp->lastsrtxcount) {
01072 res = ast_rtcp_write_sr(instance);
01073 } else {
01074 res = ast_rtcp_write_rr(instance);
01075 }
01076
01077 if (!res) {
01078
01079
01080
01081 ao2_ref(instance, -1);
01082 rtp->rtcp->schedid = -1;
01083 }
01084
01085 return res;
01086 }
01087
01088 static int ast_rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec)
01089 {
01090 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01091 int pred, mark = 0;
01092 unsigned int ms = calc_txstamp(rtp, &frame->delivery);
01093 struct ast_sockaddr remote_address = { {0,} };
01094 int rate = rtp_get_rate(frame->subclass.codec) / 1000;
01095
01096 if (frame->subclass.codec == AST_FORMAT_G722) {
01097 frame->samples /= 2;
01098 }
01099
01100 if (rtp->sending_digit) {
01101 return 0;
01102 }
01103
01104 if (frame->frametype == AST_FRAME_VOICE) {
01105 pred = rtp->lastts + frame->samples;
01106
01107
01108 rtp->lastts = rtp->lastts + ms * rate;
01109 if (ast_tvzero(frame->delivery)) {
01110
01111
01112 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW) {
01113 rtp->lastts = pred;
01114 } else {
01115 ast_debug(3, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
01116 mark = 1;
01117 }
01118 }
01119 } else if (frame->frametype == AST_FRAME_VIDEO) {
01120 mark = frame->subclass.codec & 0x1;
01121 pred = rtp->lastovidtimestamp + frame->samples;
01122
01123 rtp->lastts = rtp->lastts + ms * 90;
01124
01125 if (ast_tvzero(frame->delivery)) {
01126 if (abs(rtp->lastts - pred) < 7200) {
01127 rtp->lastts = pred;
01128 rtp->lastovidtimestamp += frame->samples;
01129 } else {
01130 ast_debug(3, "Difference is %d, ms is %d (%d), pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, frame->samples);
01131 rtp->lastovidtimestamp = rtp->lastts;
01132 }
01133 }
01134 } else {
01135 pred = rtp->lastotexttimestamp + frame->samples;
01136
01137 rtp->lastts = rtp->lastts + ms;
01138
01139 if (ast_tvzero(frame->delivery)) {
01140 if (abs(rtp->lastts - pred) < 7200) {
01141 rtp->lastts = pred;
01142 rtp->lastotexttimestamp += frame->samples;
01143 } else {
01144 ast_debug(3, "Difference is %d, ms is %d, pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, rtp->lastts, pred, frame->samples);
01145 rtp->lastotexttimestamp = rtp->lastts;
01146 }
01147 }
01148 }
01149
01150
01151 if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
01152 mark = 1;
01153 ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT);
01154 }
01155
01156
01157 if (rtp->lastts > rtp->lastdigitts) {
01158 rtp->lastdigitts = rtp->lastts;
01159 }
01160
01161 if (ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO)) {
01162 rtp->lastts = frame->ts * rate;
01163 }
01164
01165 ast_rtp_instance_get_remote_address(instance, &remote_address);
01166
01167
01168 if (!ast_sockaddr_isnull(&remote_address)) {
01169 int hdrlen = 12, res;
01170 unsigned char *rtpheader = (unsigned char *)(frame->data.ptr - hdrlen);
01171
01172 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
01173 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
01174 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
01175
01176 if ((res = rtp_sendto(instance, (void *)rtpheader, frame->datalen + hdrlen, 0, &remote_address)) < 0) {
01177 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))) {
01178 ast_debug(1, "RTP Transmission error of packet %d to %s: %s\n",
01179 rtp->seqno,
01180 ast_sockaddr_stringify(&remote_address),
01181 strerror(errno));
01182 } else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) {
01183
01184 if (option_debug || rtpdebug)
01185 ast_debug(0, "RTP NAT: Can't write RTP to private address %s, waiting for other end to send audio...\n",
01186 ast_sockaddr_stringify(&remote_address));
01187 ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN);
01188 }
01189 } else {
01190 rtp->txcount++;
01191 rtp->txoctetcount += (res - hdrlen);
01192
01193 if (rtp->rtcp && rtp->rtcp->schedid < 1) {
01194 ast_debug(1, "Starting RTCP transmission on RTP instance '%p'\n", instance);
01195 ao2_ref(instance, +1);
01196 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, instance);
01197 if (rtp->rtcp->schedid < 0) {
01198 ao2_ref(instance, -1);
01199 ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n");
01200 }
01201 }
01202 }
01203
01204 if (rtp_debug_test_addr(&remote_address)) {
01205 ast_verbose("Sent RTP packet to %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
01206 ast_sockaddr_stringify(&remote_address),
01207 codec, rtp->seqno, rtp->lastts, res - hdrlen);
01208 }
01209 }
01210
01211 rtp->seqno++;
01212
01213 return 0;
01214 }
01215
01216 static struct ast_frame *red_t140_to_red(struct rtp_red *red) {
01217 unsigned char *data = red->t140red.data.ptr;
01218 int len = 0;
01219 int i;
01220
01221
01222 if (red->len[0]) {
01223 for (i = 1; i < red->num_gen+1; i++)
01224 len += red->len[i];
01225
01226 memmove(&data[red->hdrlen], &data[red->hdrlen+red->len[0]], len);
01227 }
01228
01229
01230 for (i = 0; i < red->num_gen; i++)
01231 red->len[i] = red->len[i+1];
01232 red->len[i] = red->t140.datalen;
01233
01234
01235 len = red->hdrlen;
01236 for (i = 0; i < red->num_gen; i++)
01237 len += data[i*4+3] = red->len[i];
01238
01239
01240 memcpy(&data[len], red->t140.data.ptr, red->t140.datalen);
01241 red->t140red.datalen = len + red->t140.datalen;
01242
01243
01244 if (len == red->hdrlen && !red->t140.datalen)
01245 return NULL;
01246
01247
01248 red->t140.datalen = 0;
01249
01250 return &red->t140red;
01251 }
01252
01253 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
01254 {
01255 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01256 struct ast_sockaddr remote_address = { {0,} };
01257 format_t codec, subclass;
01258
01259 ast_rtp_instance_get_remote_address(instance, &remote_address);
01260
01261
01262 if (ast_sockaddr_isnull(&remote_address)) {
01263 ast_debug(1, "No remote address on RTP instance '%p' so dropping frame\n", instance);
01264 return 0;
01265 }
01266
01267
01268 if (!frame->datalen) {
01269 ast_debug(1, "Received frame with no data for RTP instance '%p' so dropping frame\n", instance);
01270 return 0;
01271 }
01272
01273
01274 if (frame->frametype != AST_FRAME_VOICE && frame->frametype != AST_FRAME_VIDEO && frame->frametype != AST_FRAME_TEXT) {
01275 ast_log(LOG_WARNING, "RTP can only send voice, video, and text\n");
01276 return -1;
01277 }
01278
01279 if (rtp->red) {
01280
01281
01282 if ((frame = red_t140_to_red(rtp->red)) == NULL)
01283 return 0;
01284 }
01285
01286
01287 subclass = frame->subclass.codec;
01288 if (frame->frametype == AST_FRAME_VIDEO) {
01289 subclass &= ~0x1LL;
01290 }
01291 if ((codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 1, subclass)) < 0) {
01292 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(frame->subclass.codec));
01293 return -1;
01294 }
01295
01296
01297 if (rtp->lasttxformat != subclass) {
01298 ast_debug(1, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
01299 rtp->lasttxformat = subclass;
01300 if (rtp->smoother) {
01301 ast_smoother_free(rtp->smoother);
01302 rtp->smoother = NULL;
01303 }
01304 }
01305
01306
01307 if (!rtp->smoother) {
01308 struct ast_format_list fmt = ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance)->pref, subclass);
01309
01310 switch (subclass) {
01311 case AST_FORMAT_SPEEX:
01312 case AST_FORMAT_SPEEX16:
01313 case AST_FORMAT_G723_1:
01314 case AST_FORMAT_SIREN7:
01315 case AST_FORMAT_SIREN14:
01316 case AST_FORMAT_G719:
01317
01318
01319 break;
01320 default:
01321 if (fmt.inc_ms) {
01322 if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
01323 ast_log(LOG_WARNING, "Unable to create smoother: format %s ms: %d len: %d\n", ast_getformatname(subclass), fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
01324 return -1;
01325 }
01326 if (fmt.flags) {
01327 ast_smoother_set_flags(rtp->smoother, fmt.flags);
01328 }
01329 ast_debug(1, "Created smoother: format: %s ms: %d len: %d\n", ast_getformatname(subclass), fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
01330 }
01331 }
01332 }
01333
01334
01335 if (rtp->smoother) {
01336 struct ast_frame *f;
01337
01338 if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
01339 ast_smoother_feed_be(rtp->smoother, frame);
01340 } else {
01341 ast_smoother_feed(rtp->smoother, frame);
01342 }
01343
01344 while ((f = ast_smoother_read(rtp->smoother)) && (f->data.ptr)) {
01345 ast_rtp_raw_write(instance, f, codec);
01346 }
01347 } else {
01348 int hdrlen = 12;
01349 struct ast_frame *f = NULL;
01350
01351 if (frame->offset < hdrlen) {
01352 f = ast_frdup(frame);
01353 } else {
01354 f = frame;
01355 }
01356 if (f->data.ptr) {
01357 ast_rtp_raw_write(instance, f, codec);
01358 }
01359 if (f != frame) {
01360 ast_frfree(f);
01361 }
01362
01363 }
01364
01365 return 0;
01366 }
01367
01368 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
01369 {
01370 struct timeval now;
01371 struct timeval tmp;
01372 double transit;
01373 double current_time;
01374 double d;
01375 double dtv;
01376 double prog;
01377 int rate = rtp_get_rate(rtp->f.subclass.codec);
01378
01379 double normdev_rxjitter_current;
01380 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
01381 gettimeofday(&rtp->rxcore, NULL);
01382 rtp->drxcore = (double) rtp->rxcore.tv_sec + (double) rtp->rxcore.tv_usec / 1000000;
01383
01384 rtp->seedrxts = timestamp;
01385 tmp = ast_samp2tv(timestamp, rate);
01386 rtp->rxcore = ast_tvsub(rtp->rxcore, tmp);
01387
01388 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
01389 }
01390
01391 gettimeofday(&now,NULL);
01392
01393 tmp = ast_samp2tv(timestamp, rate);
01394 *tv = ast_tvadd(rtp->rxcore, tmp);
01395
01396 prog = (double)((timestamp-rtp->seedrxts)/(float)(rate));
01397 dtv = (double)rtp->drxcore + (double)(prog);
01398 current_time = (double)now.tv_sec + (double)now.tv_usec/1000000;
01399 transit = current_time - dtv;
01400 d = transit - rtp->rxtransit;
01401 rtp->rxtransit = transit;
01402 if (d<0)
01403 d=-d;
01404 rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
01405
01406 if (rtp->rtcp) {
01407 if (rtp->rxjitter > rtp->rtcp->maxrxjitter)
01408 rtp->rtcp->maxrxjitter = rtp->rxjitter;
01409 if (rtp->rtcp->rxjitter_count == 1)
01410 rtp->rtcp->minrxjitter = rtp->rxjitter;
01411 if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter)
01412 rtp->rtcp->minrxjitter = rtp->rxjitter;
01413
01414 normdev_rxjitter_current = normdev_compute(rtp->rtcp->normdev_rxjitter,rtp->rxjitter,rtp->rtcp->rxjitter_count);
01415 rtp->rtcp->stdev_rxjitter = stddev_compute(rtp->rtcp->stdev_rxjitter,rtp->rxjitter,rtp->rtcp->normdev_rxjitter,normdev_rxjitter_current,rtp->rtcp->rxjitter_count);
01416
01417 rtp->rtcp->normdev_rxjitter = normdev_rxjitter_current;
01418 rtp->rtcp->rxjitter_count++;
01419 }
01420 }
01421
01422 static struct ast_frame *create_dtmf_frame(struct ast_rtp_instance *instance, enum ast_frame_type type, int compensate)
01423 {
01424 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01425 struct ast_sockaddr remote_address = { {0,} };
01426
01427 ast_rtp_instance_get_remote_address(instance, &remote_address);
01428
01429 if (((compensate && type == AST_FRAME_DTMF_END) || (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
01430 ast_debug(1, "Ignore potential DTMF echo from '%s'\n",
01431 ast_sockaddr_stringify(&remote_address));
01432 rtp->resp = 0;
01433 rtp->dtmfsamples = 0;
01434 return &ast_null_frame;
01435 }
01436 ast_debug(1, "Sending dtmf: %d (%c), at %s\n", rtp->resp, rtp->resp,
01437 ast_sockaddr_stringify(&remote_address));
01438 if (rtp->resp == 'X') {
01439 rtp->f.frametype = AST_FRAME_CONTROL;
01440 rtp->f.subclass.integer = AST_CONTROL_FLASH;
01441 } else {
01442 rtp->f.frametype = type;
01443 rtp->f.subclass.integer = rtp->resp;
01444 }
01445 rtp->f.datalen = 0;
01446 rtp->f.samples = 0;
01447 rtp->f.mallocd = 0;
01448 rtp->f.src = "RTP";
01449 AST_LIST_NEXT(&rtp->f, frame_list) = NULL;
01450
01451 return &rtp->f;
01452 }
01453
01454 static void process_dtmf_rfc2833(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, struct ast_sockaddr *addr, int payloadtype, int mark, struct frame_list *frames)
01455 {
01456 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01457 struct ast_sockaddr remote_address = { {0,} };
01458 unsigned int event, event_end, samples;
01459 char resp = 0;
01460 struct ast_frame *f = NULL;
01461
01462 ast_rtp_instance_get_remote_address(instance, &remote_address);
01463
01464
01465 event = ntohl(*((unsigned int *)(data)));
01466 event >>= 24;
01467 event_end = ntohl(*((unsigned int *)(data)));
01468 event_end <<= 8;
01469 event_end >>= 24;
01470 samples = ntohl(*((unsigned int *)(data)));
01471 samples &= 0xFFFF;
01472
01473 if (rtp_debug_test_addr(&remote_address)) {
01474 ast_verbose("Got RTP RFC2833 from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u, mark %d, event %08x, end %d, duration %-5.5d) \n",
01475 ast_sockaddr_stringify(&remote_address),
01476 payloadtype, seqno, timestamp, len, (mark?1:0), event, ((event_end & 0x80)?1:0), samples);
01477 }
01478
01479
01480 if (rtpdebug || option_debug > 2)
01481 ast_debug(0, "- RTP 2833 Event: %08x (len = %d)\n", event, len);
01482
01483
01484 if (event < 10) {
01485 resp = '0' + event;
01486 } else if (event < 11) {
01487 resp = '*';
01488 } else if (event < 12) {
01489 resp = '#';
01490 } else if (event < 16) {
01491 resp = 'A' + (event - 12);
01492 } else if (event < 17) {
01493 resp = 'X';
01494 } else {
01495
01496 ast_log(LOG_DEBUG, "Ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", event);
01497 return;
01498 }
01499
01500 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) {
01501 if ((rtp->lastevent != timestamp) || (rtp->resp && rtp->resp != resp)) {
01502 rtp->resp = resp;
01503 rtp->dtmf_timeout = 0;
01504 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)));
01505 f->len = 0;
01506 rtp->lastevent = timestamp;
01507 AST_LIST_INSERT_TAIL(frames, f, frame_list);
01508 }
01509 } else {
01510
01511
01512
01513
01514
01515 unsigned int new_duration = rtp->dtmf_duration;
01516 unsigned int last_duration = new_duration & 0xFFFF;
01517
01518 if (last_duration > 64000 && samples < last_duration) {
01519 new_duration += 0xFFFF + 1;
01520 }
01521 new_duration = (new_duration & ~0xFFFF) | samples;
01522
01523
01524
01525
01526
01527 if (rtp->lastevent > seqno && rtp->lastevent - seqno < 50) {
01528
01529
01530
01531
01532 return;
01533 }
01534
01535 if (event_end & 0x80) {
01536
01537 if ((rtp->lastevent != seqno) && rtp->resp) {
01538 rtp->dtmf_duration = new_duration;
01539 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
01540 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass.codec)), ast_tv(0, 0));
01541 rtp->resp = 0;
01542 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
01543 AST_LIST_INSERT_TAIL(frames, f, frame_list);
01544 }
01545 } else {
01546
01547
01548 if (rtp->resp && rtp->resp != resp) {
01549
01550 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
01551 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass.codec)), ast_tv(0, 0));
01552 rtp->resp = 0;
01553 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
01554 AST_LIST_INSERT_TAIL(frames, f, frame_list);
01555 }
01556
01557 if (rtp->resp) {
01558
01559 rtp->dtmf_duration = new_duration;
01560 } else {
01561
01562 rtp->resp = resp;
01563 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0));
01564 rtp->dtmf_duration = samples;
01565 AST_LIST_INSERT_TAIL(frames, f, frame_list);
01566 }
01567
01568 rtp->dtmf_timeout = timestamp + rtp->dtmf_duration + dtmftimeout;
01569 }
01570
01571 rtp->lastevent = seqno;
01572 }
01573
01574 rtp->dtmfsamples = samples;
01575
01576 return;
01577 }
01578
01579 static struct ast_frame *process_dtmf_cisco(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, struct ast_sockaddr *addr, int payloadtype, int mark)
01580 {
01581 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01582 unsigned int event, flags, power;
01583 char resp = 0;
01584 unsigned char seq;
01585 struct ast_frame *f = NULL;
01586
01587 if (len < 4) {
01588 return NULL;
01589 }
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01608
01609
01610
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621 seq = data[0];
01622 flags = data[1];
01623 power = data[2];
01624 event = data[3] & 0x1f;
01625
01626 if (option_debug > 2 || rtpdebug)
01627 ast_debug(0, "Cisco DTMF Digit: %02x (len=%d, seq=%d, flags=%02x, power=%d, history count=%d)\n", event, len, seq, flags, power, (len - 4) / 2);
01628 if (event < 10) {
01629 resp = '0' + event;
01630 } else if (event < 11) {
01631 resp = '*';
01632 } else if (event < 12) {
01633 resp = '#';
01634 } else if (event < 16) {
01635 resp = 'A' + (event - 12);
01636 } else if (event < 17) {
01637 resp = 'X';
01638 }
01639 if ((!rtp->resp && power) || (rtp->resp && (rtp->resp != resp))) {
01640 rtp->resp = resp;
01641
01642 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) {
01643 f = create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0);
01644 rtp->dtmfsamples = 0;
01645 }
01646 } else if ((rtp->resp == resp) && !power) {
01647 f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE));
01648 f->samples = rtp->dtmfsamples * (rtp->lastrxformat ? (rtp_get_rate(rtp->lastrxformat) / 1000) : 8);
01649 rtp->resp = 0;
01650 } else if (rtp->resp == resp)
01651 rtp->dtmfsamples += 20 * (rtp->lastrxformat ? (rtp_get_rate(rtp->lastrxformat) / 1000) : 8);
01652
01653 rtp->dtmf_timeout = 0;
01654
01655 return f;
01656 }
01657
01658 static struct ast_frame *process_cn_rfc3389(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, struct ast_sockaddr *addr, int payloadtype, int mark)
01659 {
01660 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01661
01662
01663
01664
01665 if (rtpdebug)
01666 ast_debug(0, "- RTP 3389 Comfort noise event: Level %" PRId64 " (len = %d)\n", rtp->lastrxformat, len);
01667
01668 if (ast_test_flag(rtp, FLAG_3389_WARNING)) {
01669 struct ast_sockaddr remote_address = { {0,} };
01670
01671 ast_rtp_instance_get_remote_address(instance, &remote_address);
01672
01673 ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client address: %s\n",
01674 ast_sockaddr_stringify(&remote_address));
01675 ast_set_flag(rtp, FLAG_3389_WARNING);
01676 }
01677
01678
01679 if (!len)
01680 return NULL;
01681 if (len < 24) {
01682 rtp->f.data.ptr = rtp->rawdata + AST_FRIENDLY_OFFSET;
01683 rtp->f.datalen = len - 1;
01684 rtp->f.offset = AST_FRIENDLY_OFFSET;
01685 memcpy(rtp->f.data.ptr, data + 1, len - 1);
01686 } else {
01687 rtp->f.data.ptr = NULL;
01688 rtp->f.offset = 0;
01689 rtp->f.datalen = 0;
01690 }
01691 rtp->f.frametype = AST_FRAME_CNG;
01692 rtp->f.subclass.integer = data[0] & 0x7f;
01693 rtp->f.samples = 0;
01694 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
01695
01696 return &rtp->f;
01697 }
01698
01699 static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance)
01700 {
01701 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
01702 struct ast_sockaddr addr;
01703 unsigned int rtcpdata[8192 + AST_FRIENDLY_OFFSET];
01704 unsigned int *rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
01705 int res, packetwords, position = 0;
01706 struct ast_frame *f = &ast_null_frame;
01707
01708
01709 if ((res = rtcp_recvfrom(instance, rtcpdata + AST_FRIENDLY_OFFSET,
01710 sizeof(rtcpdata) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET,
01711 0, &addr)) < 0) {
01712 ast_assert(errno != EBADF);
01713 if (errno != EAGAIN) {
01714 ast_log(LOG_WARNING, "RTCP Read error: %s. Hanging up.\n", strerror(errno));
01715 return NULL;
01716 }
01717 return &ast_null_frame;
01718 }
01719
01720 packetwords = res / 4;
01721
01722 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT)) {
01723
01724 if (ast_sockaddr_cmp(&rtp->rtcp->them, &addr)) {
01725 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
01726 if (option_debug || rtpdebug)
01727 ast_debug(0, "RTCP NAT: Got RTCP from other end. Now sending to address %s\n",
01728 ast_sockaddr_stringify(&rtp->rtcp->them));
01729 }
01730 }
01731
01732 ast_debug(1, "Got RTCP report of %d bytes\n", res);
01733
01734 while (position < packetwords) {
01735 int i, pt, rc;
01736 unsigned int length, dlsr, lsr, msw, lsw, comp;
01737 struct timeval now;
01738 double rttsec, reported_jitter, reported_normdev_jitter_current, normdevrtt_current, reported_lost, reported_normdev_lost_current;
01739 uint64_t rtt = 0;
01740
01741 i = position;
01742 length = ntohl(rtcpheader[i]);
01743 pt = (length & 0xff0000) >> 16;
01744 rc = (length & 0x1f000000) >> 24;
01745 length &= 0xffff;
01746
01747 if ((i + length) > packetwords) {
01748 if (option_debug || rtpdebug)
01749 ast_log(LOG_DEBUG, "RTCP Read too short\n");
01750 return &ast_null_frame;
01751 }
01752
01753 if (rtcp_debug_test_addr(&addr)) {
01754 ast_verbose("\n\nGot RTCP from %s\n",
01755 ast_sockaddr_stringify(&addr));
01756 ast_verbose("PT: %d(%s)\n", pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown");
01757 ast_verbose("Reception reports: %d\n", rc);
01758 ast_verbose("SSRC of sender: %u\n", rtcpheader[i + 1]);
01759 }
01760
01761 i += 2;
01762 if (rc == 0 && pt == RTCP_PT_RR) {
01763 position += (length + 1);
01764 continue;
01765 }
01766
01767 switch (pt) {
01768 case RTCP_PT_SR:
01769 gettimeofday(&rtp->rtcp->rxlsr,NULL);
01770 rtp->rtcp->spc = ntohl(rtcpheader[i+3]);
01771 rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
01772 rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16);
01773
01774 if (rtcp_debug_test_addr(&addr)) {
01775 ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader[i]), (unsigned long) ntohl(rtcpheader[i + 1]) * 4096);
01776 ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader[i + 2]));
01777 ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader[i + 3]), (unsigned long) ntohl(rtcpheader[i + 4]));
01778 }
01779 i += 5;
01780 if (rc < 1)
01781 break;
01782
01783 case RTCP_PT_RR:
01784
01785
01786 gettimeofday(&now, NULL);
01787 timeval2ntp(now, &msw, &lsw);
01788 if (ntohl(rtcpheader[i + 4]) && ntohl(rtcpheader[i + 5])) {
01789 comp = ((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16);
01790 lsr = ntohl(rtcpheader[i + 4]);
01791 dlsr = ntohl(rtcpheader[i + 5]);
01792 rtt = comp - lsr - dlsr;
01793
01794
01795
01796 if (rtt < 4294) {
01797 rtt = (rtt * 1000000) >> 16;
01798 } else {
01799 rtt = (rtt * 1000) >> 16;
01800 rtt *= 1000;
01801 }
01802 rtt = rtt / 1000.;
01803 rttsec = rtt / 1000.;
01804 rtp->rtcp->rtt = rttsec;
01805
01806 if (comp - dlsr >= lsr) {
01807 rtp->rtcp->accumulated_transit += rttsec;
01808
01809 if (rtp->rtcp->rtt_count == 0)
01810 rtp->rtcp->minrtt = rttsec;
01811
01812 if (rtp->rtcp->maxrtt<rttsec)
01813 rtp->rtcp->maxrtt = rttsec;
01814 if (rtp->rtcp->minrtt>rttsec)
01815 rtp->rtcp->minrtt = rttsec;
01816
01817 normdevrtt_current = normdev_compute(rtp->rtcp->normdevrtt, rttsec, rtp->rtcp->rtt_count);
01818
01819 rtp->rtcp->stdevrtt = stddev_compute(rtp->rtcp->stdevrtt, rttsec, rtp->rtcp->normdevrtt, normdevrtt_current, rtp->rtcp->rtt_count);
01820
01821 rtp->rtcp->normdevrtt = normdevrtt_current;
01822
01823 rtp->rtcp->rtt_count++;
01824 } else if (rtcp_debug_test_addr(&addr)) {
01825 ast_verbose("Internal RTCP NTP clock skew detected: "
01826 "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
01827 "diff=%d\n",
01828 lsr, comp, dlsr, dlsr / 65536,
01829 (dlsr % 65536) * 1000 / 65536,
01830 dlsr - (comp - lsr));
01831 }
01832 }
01833
01834 rtp->rtcp->reported_jitter = ntohl(rtcpheader[i + 3]);
01835 reported_jitter = (double) rtp->rtcp->reported_jitter;
01836
01837 if (rtp->rtcp->reported_jitter_count == 0)
01838 rtp->rtcp->reported_minjitter = reported_jitter;
01839
01840 if (reported_jitter < rtp->rtcp->reported_minjitter)
01841 rtp->rtcp->reported_minjitter = reported_jitter;
01842
01843 if (reported_jitter > rtp->rtcp->reported_maxjitter)
01844 rtp->rtcp->reported_maxjitter = reported_jitter;
01845
01846 reported_normdev_jitter_current = normdev_compute(rtp->rtcp->reported_normdev_jitter, reported_jitter, rtp->rtcp->reported_jitter_count);
01847
01848 rtp->rtcp->reported_stdev_jitter = stddev_compute(rtp->rtcp->reported_stdev_jitter, reported_jitter, rtp->rtcp->reported_normdev_jitter, reported_normdev_jitter_current, rtp->rtcp->reported_jitter_count);
01849
01850 rtp->rtcp->reported_normdev_jitter = reported_normdev_jitter_current;
01851
01852 rtp->rtcp->reported_lost = ntohl(rtcpheader[i + 1]) & 0xffffff;
01853
01854 reported_lost = (double) rtp->rtcp->reported_lost;
01855
01856
01857 if (rtp->rtcp->reported_jitter_count == 0)
01858 rtp->rtcp->reported_minlost = reported_lost;
01859
01860 if (reported_lost < rtp->rtcp->reported_minlost)
01861 rtp->rtcp->reported_minlost = reported_lost;
01862
01863 if (reported_lost > rtp->rtcp->reported_maxlost)
01864 rtp->rtcp->reported_maxlost = reported_lost;
01865 reported_normdev_lost_current = normdev_compute(rtp->rtcp->reported_normdev_lost, reported_lost, rtp->rtcp->reported_jitter_count);
01866
01867 rtp->rtcp->reported_stdev_lost = stddev_compute(rtp->rtcp->reported_stdev_lost, reported_lost, rtp->rtcp->reported_normdev_lost, reported_normdev_lost_current, rtp->rtcp->reported_jitter_count);
01868
01869 rtp->rtcp->reported_normdev_lost = reported_normdev_lost_current;
01870
01871 rtp->rtcp->reported_jitter_count++;
01872
01873 if (rtcp_debug_test_addr(&addr)) {
01874 ast_verbose(" Fraction lost: %ld\n", (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24));
01875 ast_verbose(" Packets lost so far: %d\n", rtp->rtcp->reported_lost);
01876 ast_verbose(" Highest sequence number: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff));
01877 ast_verbose(" Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16);
01878 ast_verbose(" Interarrival jitter: %u\n", rtp->rtcp->reported_jitter);
01879 ast_verbose(" Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader[i + 4]) >> 16,((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096);
01880 ast_verbose(" DLSR: %4.4f (sec)\n",ntohl(rtcpheader[i + 5])/65536.0);
01881 if (rtt)
01882 ast_verbose(" RTT: %lu(sec)\n", (unsigned long) rtt);
01883 }
01884 if (rtt) {
01885 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s\r\n"
01886 "PT: %d(%s)\r\n"
01887 "ReceptionReports: %d\r\n"
01888 "SenderSSRC: %u\r\n"
01889 "FractionLost: %ld\r\n"
01890 "PacketsLost: %d\r\n"
01891 "HighestSequence: %ld\r\n"
01892 "SequenceNumberCycles: %ld\r\n"
01893 "IAJitter: %u\r\n"
01894 "LastSR: %lu.%010lu\r\n"
01895 "DLSR: %4.4f(sec)\r\n"
01896 "RTT: %llu(sec)\r\n",
01897 ast_sockaddr_stringify(&addr),
01898 pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
01899 rc,
01900 rtcpheader[i + 1],
01901 (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
01902 rtp->rtcp->reported_lost,
01903 (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
01904 (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16,
01905 rtp->rtcp->reported_jitter,
01906 (unsigned long) ntohl(rtcpheader[i + 4]) >> 16, ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
01907 ntohl(rtcpheader[i + 5])/65536.0,
01908 (unsigned long long)rtt);
01909 } else {
01910 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s\r\n"
01911 "PT: %d(%s)\r\n"
01912 "ReceptionReports: %d\r\n"
01913 "SenderSSRC: %u\r\n"
01914 "FractionLost: %ld\r\n"
01915 "PacketsLost: %d\r\n"
01916 "HighestSequence: %ld\r\n"
01917 "SequenceNumberCycles: %ld\r\n"
01918 "IAJitter: %u\r\n"
01919 "LastSR: %lu.%010lu\r\n"
01920 "DLSR: %4.4f(sec)\r\n",
01921 ast_sockaddr_stringify(&addr),
01922 pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
01923 rc,
01924 rtcpheader[i + 1],
01925 (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
01926 rtp->rtcp->reported_lost,
01927 (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
01928 (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16,
01929 rtp->rtcp->reported_jitter,
01930 (unsigned long) ntohl(rtcpheader[i + 4]) >> 16,
01931 ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
01932 ntohl(rtcpheader[i + 5])/65536.0);
01933 }
01934 break;
01935 case RTCP_PT_FUR:
01936 if (rtcp_debug_test_addr(&addr))
01937 ast_verbose("Received an RTCP Fast Update Request\n");
01938 rtp->f.frametype = AST_FRAME_CONTROL;
01939 rtp->f.subclass.integer = AST_CONTROL_VIDUPDATE;
01940 rtp->f.datalen = 0;
01941 rtp->f.samples = 0;
01942 rtp->f.mallocd = 0;
01943 rtp->f.src = "RTP";
01944 f = &rtp->f;
01945 break;
01946 case RTCP_PT_SDES:
01947 if (rtcp_debug_test_addr(&addr))
01948 ast_verbose("Received an SDES from %s\n",
01949 ast_sockaddr_stringify(&rtp->rtcp->them));
01950 break;
01951 case RTCP_PT_BYE:
01952 if (rtcp_debug_test_addr(&addr))
01953 ast_verbose("Received a BYE from %s\n",
01954 ast_sockaddr_stringify(&rtp->rtcp->them));
01955 break;
01956 default:
01957 ast_debug(1, "Unknown RTCP packet (pt=%d) received from %s\n",
01958 pt, ast_sockaddr_stringify(&rtp->rtcp->them));
01959 break;
01960 }
01961 position += (length + 1);
01962 }
01963
01964 rtp->rtcp->rtcp_info = 1;
01965
01966 return f;
01967 }
01968
01969 static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, unsigned int *rtpheader, int len, int hdrlen)
01970 {
01971 struct ast_rtp_instance *instance1 = ast_rtp_instance_get_bridged(instance);
01972 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance), *bridged = ast_rtp_instance_get_data(instance1);
01973 int res = 0, payload = 0, bridged_payload = 0, mark;
01974 struct ast_rtp_payload_type payload_type;
01975 int reconstruct = ntohl(rtpheader[0]);
01976 struct ast_sockaddr remote_address = { {0,} };
01977
01978
01979 payload = (reconstruct & 0x7f0000) >> 16;
01980 mark = (((reconstruct & 0x800000) >> 23) != 0);
01981
01982
01983 payload_type = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(instance), payload);
01984
01985
01986 bridged_payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance1), payload_type.asterisk_format, payload_type.code);
01987
01988
01989 if (!(ast_rtp_instance_get_codecs(instance1)->payloads[bridged_payload].code)) {
01990 return -1;
01991 }
01992
01993
01994 if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
01995 mark = 1;
01996 ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT);
01997 }
01998
01999
02000 reconstruct &= 0xFF80FFFF;
02001 reconstruct |= (bridged_payload << 16);
02002 reconstruct |= (mark << 23);
02003 rtpheader[0] = htonl(reconstruct);
02004
02005 ast_rtp_instance_get_remote_address(instance1, &remote_address);
02006
02007 if (ast_sockaddr_isnull(&remote_address)) {
02008 ast_debug(1, "Remote address is null, most likely RTP has been stopped\n");
02009 return 0;
02010 }
02011
02012
02013 res = rtp_sendto(instance1, (void *)rtpheader, len, 0, &remote_address);
02014 if (res < 0) {
02015 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))) {
02016 ast_log(LOG_WARNING,
02017 "RTP Transmission error of packet to %s: %s\n",
02018 ast_sockaddr_stringify(&remote_address),
02019 strerror(errno));
02020 } else if (((ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(bridged, FLAG_NAT_INACTIVE_NOWARN)) {
02021 if (option_debug || rtpdebug)
02022 ast_log(LOG_WARNING,
02023 "RTP NAT: Can't write RTP to private "
02024 "address %s, waiting for other end to "
02025 "send audio...\n",
02026 ast_sockaddr_stringify(&remote_address));
02027 ast_set_flag(bridged, FLAG_NAT_INACTIVE_NOWARN);
02028 }
02029 return 0;
02030 } else if (rtp_debug_test_addr(&remote_address)) {
02031 ast_verbose("Sent RTP P2P packet to %s (type %-2.2d, len %-6.6u)\n",
02032 ast_sockaddr_stringify(&remote_address),
02033 bridged_payload, len - hdrlen);
02034 }
02035
02036 return 0;
02037 }
02038
02039 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp)
02040 {
02041 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02042 struct ast_sockaddr addr;
02043 int res, hdrlen = 12, version, payloadtype, padding, mark, ext, cc, prev_seqno;
02044 unsigned int *rtpheader = (unsigned int*)(rtp->rawdata + AST_FRIENDLY_OFFSET), seqno, ssrc, timestamp;
02045 struct ast_rtp_payload_type payload;
02046 struct ast_sockaddr remote_address = { {0,} };
02047 struct frame_list frames;
02048
02049
02050 if (rtcp) {
02051 if (rtp->rtcp) {
02052 return ast_rtcp_read(instance);
02053 }
02054 return &ast_null_frame;
02055 }
02056
02057
02058 if (rtp->sending_digit) {
02059 ast_rtp_dtmf_continuation(instance);
02060 }
02061
02062
02063 if ((res = rtp_recvfrom(instance, rtp->rawdata + AST_FRIENDLY_OFFSET,
02064 sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET, 0,
02065 &addr)) < 0) {
02066 ast_assert(errno != EBADF);
02067 if (errno != EAGAIN) {
02068 ast_log(LOG_WARNING, "RTP Read error: %s. Hanging up.\n", strerror(errno));
02069 return NULL;
02070 }
02071 return &ast_null_frame;
02072 }
02073
02074
02075 if (res < hdrlen) {
02076 ast_log(LOG_WARNING, "RTP Read too short\n");
02077 return &ast_null_frame;
02078 }
02079
02080
02081 if (rtp->strict_rtp_state == STRICT_RTP_LEARN) {
02082 ast_sockaddr_copy(&rtp->strict_rtp_address, &addr);
02083 rtp->strict_rtp_state = STRICT_RTP_CLOSED;
02084 } else if (rtp->strict_rtp_state == STRICT_RTP_CLOSED) {
02085 if (ast_sockaddr_cmp(&rtp->strict_rtp_address, &addr)) {
02086
02087 if (!ast_sockaddr_cmp(&rtp->alt_rtp_address, &addr)) {
02088
02089 ast_sockaddr_copy(&rtp->strict_rtp_address,
02090 &addr);
02091 } else {
02092 const char *real_addr = ast_strdupa(ast_sockaddr_stringify(&addr));
02093 const char *expected_addr = ast_strdupa(ast_sockaddr_stringify(&rtp->strict_rtp_address));
02094
02095 ast_debug(1, "Received RTP packet from %s, dropping due to strict RTP protection. Expected it to be from %s\n",
02096 real_addr, expected_addr);
02097
02098 return &ast_null_frame;
02099 }
02100 }
02101 }
02102
02103
02104 seqno = ntohl(rtpheader[0]);
02105
02106 ast_rtp_instance_get_remote_address(instance, &remote_address);
02107
02108 if (!(version = (seqno & 0xC0000000) >> 30)) {
02109 struct sockaddr_in addr_tmp;
02110 struct ast_sockaddr addr_v4;
02111 if (ast_sockaddr_is_ipv4(&addr)) {
02112 ast_sockaddr_to_sin(&addr, &addr_tmp);
02113 } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) {
02114 ast_debug(1, "Using IPv6 mapped address %s for STUN\n",
02115 ast_sockaddr_stringify(&addr));
02116 ast_sockaddr_to_sin(&addr_v4, &addr_tmp);
02117 } else {
02118 ast_debug(1, "Cannot do STUN for non IPv4 address %s\n",
02119 ast_sockaddr_stringify(&addr));
02120 return &ast_null_frame;
02121 }
02122 if ((ast_stun_handle_packet(rtp->s, &addr_tmp, rtp->rawdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == AST_STUN_ACCEPT) &&
02123 ast_sockaddr_isnull(&remote_address)) {
02124 ast_sockaddr_from_sin(&addr, &addr_tmp);
02125 ast_rtp_instance_set_remote_address(instance, &addr);
02126 }
02127 return &ast_null_frame;
02128 }
02129
02130
02131 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT)) {
02132 if (ast_sockaddr_cmp(&remote_address, &addr)) {
02133 ast_rtp_instance_set_remote_address(instance, &addr);
02134 ast_sockaddr_copy(&remote_address, &addr);
02135 if (rtp->rtcp) {
02136 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
02137 ast_sockaddr_set_port(&rtp->rtcp->them, ast_sockaddr_port(&addr) + 1);
02138 }
02139 rtp->rxseqno = 0;
02140 ast_set_flag(rtp, FLAG_NAT_ACTIVE);
02141 if (option_debug || rtpdebug)
02142 ast_debug(0, "RTP NAT: Got audio from other end. Now sending to address %s\n",
02143 ast_sockaddr_stringify(&remote_address));
02144 }
02145 }
02146
02147
02148 if (ast_rtp_instance_get_bridged(instance) && !bridge_p2p_rtp_write(instance, rtpheader, res, hdrlen)) {
02149 return &ast_null_frame;
02150 }
02151
02152
02153 if (version != 2) {
02154 return &ast_null_frame;
02155 }
02156
02157
02158 payloadtype = (seqno & 0x7f0000) >> 16;
02159 padding = seqno & (1 << 29);
02160 mark = seqno & (1 << 23);
02161 ext = seqno & (1 << 28);
02162 cc = (seqno & 0xF000000) >> 24;
02163 seqno &= 0xffff;
02164 timestamp = ntohl(rtpheader[1]);
02165 ssrc = ntohl(rtpheader[2]);
02166
02167 AST_LIST_HEAD_INIT_NOLOCK(&frames);
02168
02169 if (rtp->rxssrc && rtp->rxssrc != ssrc) {
02170 struct ast_frame *f, srcupdate = {
02171 AST_FRAME_CONTROL,
02172 .subclass.integer = AST_CONTROL_SRCCHANGE,
02173 };
02174
02175 if (!mark) {
02176 if (option_debug || rtpdebug) {
02177 ast_debug(1, "Forcing Marker bit, because SSRC has changed\n");
02178 }
02179 mark = 1;
02180 }
02181
02182 f = ast_frisolate(&srcupdate);
02183 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
02184 }
02185
02186 rtp->rxssrc = ssrc;
02187
02188
02189 if (padding) {
02190 res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
02191 }
02192
02193
02194 if (cc) {
02195 hdrlen += cc * 4;
02196 }
02197
02198
02199 if (ext) {
02200 hdrlen += (ntohl(rtpheader[hdrlen/4]) & 0xffff) << 2;
02201 hdrlen += 4;
02202 if (option_debug) {
02203 int profile;
02204 profile = (ntohl(rtpheader[3]) & 0xffff0000) >> 16;
02205 if (profile == 0x505a)
02206 ast_debug(1, "Found Zfone extension in RTP stream - zrtp - not supported.\n");
02207 else
02208 ast_debug(1, "Found unknown RTP Extensions %x\n", profile);
02209 }
02210 }
02211
02212
02213 if (res < hdrlen) {
02214 ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d\n", res, hdrlen);
02215 return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame;
02216 }
02217
02218 rtp->rxcount++;
02219 if (rtp->rxcount == 1) {
02220 rtp->seedrxseqno = seqno;
02221 }
02222
02223
02224 if (rtp->rtcp && !ast_sockaddr_isnull(&rtp->rtcp->them) && rtp->rtcp->schedid < 1) {
02225
02226 ao2_ref(instance, +1);
02227 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, instance);
02228 if (rtp->rtcp->schedid < 0) {
02229 ao2_ref(instance, -1);
02230 ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n");
02231 }
02232 }
02233 if ((int)rtp->lastrxseqno - (int)seqno > 100)
02234 rtp->cycles += RTP_SEQ_MOD;
02235
02236 prev_seqno = rtp->lastrxseqno;
02237 rtp->lastrxseqno = seqno;
02238
02239 if (!rtp->themssrc) {
02240 rtp->themssrc = ntohl(rtpheader[2]);
02241 }
02242
02243 if (rtp_debug_test_addr(&addr)) {
02244 ast_verbose("Got RTP packet from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
02245 ast_sockaddr_stringify(&addr),
02246 payloadtype, seqno, timestamp,res - hdrlen);
02247 }
02248
02249 payload = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(instance), payloadtype);
02250
02251
02252 if (!payload.asterisk_format) {
02253 struct ast_frame *f = NULL;
02254 if (payload.code == AST_RTP_DTMF) {
02255
02256
02257
02258
02259 process_dtmf_rfc2833(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark, &frames);
02260 } else if (payload.code == AST_RTP_CISCO_DTMF) {
02261 f = process_dtmf_cisco(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark);
02262 } else if (payload.code == AST_RTP_CN) {
02263 f = process_cn_rfc3389(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark);
02264 } else {
02265 ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n",
02266 payloadtype,
02267 ast_sockaddr_stringify(&remote_address));
02268 }
02269
02270 if (f) {
02271 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
02272 }
02273
02274
02275
02276 if (!AST_LIST_EMPTY(&frames)) {
02277 return AST_LIST_FIRST(&frames);
02278 }
02279 return &ast_null_frame;
02280 }
02281
02282 rtp->lastrxformat = rtp->f.subclass.codec = payload.code;
02283 rtp->f.frametype = (rtp->f.subclass.codec & AST_FORMAT_AUDIO_MASK) ? AST_FRAME_VOICE : (rtp->f.subclass.codec & AST_FORMAT_VIDEO_MASK) ? AST_FRAME_VIDEO : AST_FRAME_TEXT;
02284
02285 rtp->rxseqno = seqno;
02286
02287 if (rtp->dtmf_timeout && rtp->dtmf_timeout < timestamp) {
02288 rtp->dtmf_timeout = 0;
02289
02290 if (rtp->resp) {
02291 struct ast_frame *f;
02292 f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0);
02293 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass.codec)), ast_tv(0, 0));
02294 rtp->resp = 0;
02295 rtp->dtmf_timeout = rtp->dtmf_duration = 0;
02296 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
02297 return AST_LIST_FIRST(&frames);
02298 }
02299 }
02300
02301 rtp->lastrxts = timestamp;
02302
02303 rtp->f.src = "RTP";
02304 rtp->f.mallocd = 0;
02305 rtp->f.datalen = res - hdrlen;
02306 rtp->f.data.ptr = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
02307 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
02308 rtp->f.seqno = seqno;
02309
02310 if (rtp->f.subclass.codec == AST_FORMAT_T140 && (int)seqno - (prev_seqno+1) > 0 && (int)seqno - (prev_seqno+1) < 10) {
02311 unsigned char *data = rtp->f.data.ptr;
02312
02313 memmove(rtp->f.data.ptr+3, rtp->f.data.ptr, rtp->f.datalen);
02314 rtp->f.datalen +=3;
02315 *data++ = 0xEF;
02316 *data++ = 0xBF;
02317 *data = 0xBD;
02318 }
02319
02320 if (rtp->f.subclass.codec == AST_FORMAT_T140RED) {
02321 unsigned char *data = rtp->f.data.ptr;
02322 unsigned char *header_end;
02323 int num_generations;
02324 int header_length;
02325 int len;
02326 int diff =(int)seqno - (prev_seqno+1);
02327 int x;
02328
02329 rtp->f.subclass.codec = AST_FORMAT_T140;
02330 header_end = memchr(data, ((*data) & 0x7f), rtp->f.datalen);
02331 if (header_end == NULL) {
02332 return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame;
02333 }
02334 header_end++;
02335
02336 header_length = header_end - data;
02337 num_generations = header_length / 4;
02338 len = header_length;
02339
02340 if (!diff) {
02341 for (x = 0; x < num_generations; x++)
02342 len += data[x * 4 + 3];
02343
02344 if (!(rtp->f.datalen - len))
02345 return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame;
02346
02347 rtp->f.data.ptr += len;
02348 rtp->f.datalen -= len;
02349 } else if (diff > num_generations && diff < 10) {
02350 len -= 3;
02351 rtp->f.data.ptr += len;
02352 rtp->f.datalen -= len;
02353
02354 data = rtp->f.data.ptr;
02355 *data++ = 0xEF;
02356 *data++ = 0xBF;
02357 *data = 0xBD;
02358 } else {
02359 for ( x = 0; x < num_generations - diff; x++)
02360 len += data[x * 4 + 3];
02361
02362 rtp->f.data.ptr += len;
02363 rtp->f.datalen -= len;
02364 }
02365 }
02366
02367 if (rtp->f.subclass.codec & AST_FORMAT_AUDIO_MASK) {
02368 rtp->f.samples = ast_codec_get_samples(&rtp->f);
02369 if ((rtp->f.subclass.codec == AST_FORMAT_SLINEAR) || (rtp->f.subclass.codec == AST_FORMAT_SLINEAR16)) {
02370 ast_frame_byteswap_be(&rtp->f);
02371 }
02372 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
02373
02374 ast_set_flag(&rtp->f, AST_FRFLAG_HAS_TIMING_INFO);
02375 rtp->f.ts = timestamp / (rtp_get_rate(rtp->f.subclass.codec) / 1000);
02376 rtp->f.len = rtp->f.samples / ((ast_format_rate(rtp->f.subclass.codec) / 1000));
02377 } else if (rtp->f.subclass.codec & AST_FORMAT_VIDEO_MASK) {
02378
02379 if (!rtp->lastividtimestamp)
02380 rtp->lastividtimestamp = timestamp;
02381 rtp->f.samples = timestamp - rtp->lastividtimestamp;
02382 rtp->lastividtimestamp = timestamp;
02383 rtp->f.delivery.tv_sec = 0;
02384 rtp->f.delivery.tv_usec = 0;
02385
02386
02387
02388
02389
02390 if (mark)
02391 rtp->f.subclass.codec |= 0x1;
02392 } else {
02393
02394 if (!rtp->lastitexttimestamp)
02395 rtp->lastitexttimestamp = timestamp;
02396 rtp->f.samples = timestamp - rtp->lastitexttimestamp;
02397 rtp->lastitexttimestamp = timestamp;
02398 rtp->f.delivery.tv_sec = 0;
02399 rtp->f.delivery.tv_usec = 0;
02400 }
02401
02402 AST_LIST_INSERT_TAIL(&frames, &rtp->f, frame_list);
02403 return AST_LIST_FIRST(&frames);
02404 }
02405
02406 static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
02407 {
02408 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02409
02410 if (property == AST_RTP_PROPERTY_RTCP) {
02411 if (value) {
02412 if (rtp->rtcp) {
02413 ast_debug(1, "Ignoring duplicate RTCP property on RTP instance '%p'\n", instance);
02414 return;
02415 }
02416
02417 if (!(rtp->rtcp = ast_calloc(1, sizeof(*rtp->rtcp)))) {
02418 return;
02419 }
02420
02421
02422 ast_rtp_instance_get_local_address(instance, &rtp->rtcp->us);
02423 ast_sockaddr_set_port(&rtp->rtcp->us,
02424 ast_sockaddr_port(&rtp->rtcp->us) + 1);
02425
02426 if ((rtp->rtcp->s =
02427 create_new_socket("RTCP",
02428 ast_sockaddr_is_ipv4(&rtp->rtcp->us) ?
02429 AF_INET :
02430 ast_sockaddr_is_ipv6(&rtp->rtcp->us) ?
02431 AF_INET6 : -1)) < 0) {
02432 ast_debug(1, "Failed to create a new socket for RTCP on instance '%p'\n", instance);
02433 ast_free(rtp->rtcp);
02434 rtp->rtcp = NULL;
02435 return;
02436 }
02437
02438
02439 if (ast_bind(rtp->rtcp->s, &rtp->rtcp->us)) {
02440 ast_debug(1, "Failed to setup RTCP on RTP instance '%p'\n", instance);
02441 close(rtp->rtcp->s);
02442 ast_free(rtp->rtcp);
02443 rtp->rtcp = NULL;
02444 return;
02445 }
02446
02447 ast_debug(1, "Setup RTCP on RTP instance '%p'\n", instance);
02448 rtp->rtcp->schedid = -1;
02449
02450 return;
02451 } else {
02452 if (rtp->rtcp) {
02453 if (rtp->rtcp->schedid > 0) {
02454 if (!ast_sched_del(rtp->sched, rtp->rtcp->schedid)) {
02455
02456 ao2_ref(instance, -1);
02457 } else {
02458
02459 ast_debug(1, "Failed to tear down RTCP on RTP instance '%p'\n", instance);
02460 return;
02461 }
02462 rtp->rtcp->schedid = -1;
02463 }
02464 close(rtp->rtcp->s);
02465 ast_free(rtp->rtcp);
02466 rtp->rtcp = NULL;
02467 }
02468 return;
02469 }
02470 }
02471
02472 return;
02473 }
02474
02475 static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp)
02476 {
02477 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02478
02479 return rtcp ? (rtp->rtcp ? rtp->rtcp->s : -1) : rtp->s;
02480 }
02481
02482 static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr)
02483 {
02484 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02485
02486 if (rtp->rtcp) {
02487 ast_debug(1, "Setting RTCP address on RTP instance '%p'\n", instance);
02488 ast_sockaddr_copy(&rtp->rtcp->them, addr);
02489 if (!ast_sockaddr_isnull(addr)) {
02490 ast_sockaddr_set_port(&rtp->rtcp->them,
02491 ast_sockaddr_port(addr) + 1);
02492 }
02493 }
02494
02495 rtp->rxseqno = 0;
02496
02497 if (strictrtp) {
02498 rtp->strict_rtp_state = STRICT_RTP_LEARN;
02499 }
02500
02501 return;
02502 }
02503
02504 static void ast_rtp_alt_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr)
02505 {
02506 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02507
02508
02509
02510
02511 ast_sockaddr_copy(&rtp->alt_rtp_address, addr);
02512
02513 return;
02514 }
02515
02516
02517
02518
02519 static int red_write(const void *data)
02520 {
02521 struct ast_rtp_instance *instance = (struct ast_rtp_instance*) data;
02522 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02523
02524 ast_rtp_write(instance, &rtp->red->t140);
02525
02526 return 1;
02527 }
02528
02529 static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
02530 {
02531 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02532 int x;
02533
02534 if (!(rtp->red = ast_calloc(1, sizeof(*rtp->red)))) {
02535 return -1;
02536 }
02537
02538 rtp->red->t140.frametype = AST_FRAME_TEXT;
02539 rtp->red->t140.subclass.codec = AST_FORMAT_T140RED;
02540 rtp->red->t140.data.ptr = &rtp->red->buf_data;
02541
02542 rtp->red->t140.ts = 0;
02543 rtp->red->t140red = rtp->red->t140;
02544 rtp->red->t140red.data.ptr = &rtp->red->t140red_data;
02545 rtp->red->t140red.datalen = 0;
02546 rtp->red->ti = buffer_time;
02547 rtp->red->num_gen = generations;
02548 rtp->red->hdrlen = generations * 4 + 1;
02549 rtp->red->prev_ts = 0;
02550
02551 for (x = 0; x < generations; x++) {
02552 rtp->red->pt[x] = payloads[x];
02553 rtp->red->pt[x] |= 1 << 7;
02554 rtp->red->t140red_data[x*4] = rtp->red->pt[x];
02555 }
02556 rtp->red->t140red_data[x*4] = rtp->red->pt[x] = payloads[x];
02557 rtp->red->schedid = ast_sched_add(rtp->sched, generations, red_write, instance);
02558
02559 rtp->red->t140.datalen = 0;
02560
02561 return 0;
02562 }
02563
02564 static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
02565 {
02566 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02567
02568 if (frame->datalen > -1) {
02569 struct rtp_red *red = rtp->red;
02570 memcpy(&red->buf_data[red->t140.datalen], frame->data.ptr, frame->datalen);
02571 red->t140.datalen += frame->datalen;
02572 red->t140.ts = frame->ts;
02573 }
02574
02575 return 0;
02576 }
02577
02578 static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1)
02579 {
02580 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance0);
02581
02582 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
02583
02584 return 0;
02585 }
02586
02587 static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
02588 {
02589 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02590
02591 if (!rtp->rtcp) {
02592 return -1;
02593 }
02594
02595 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_TXCOUNT, -1, stats->txcount, rtp->txcount);
02596 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RXCOUNT, -1, stats->rxcount, rtp->rxcount);
02597
02598 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_TXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->txploss, rtp->rtcp->reported_lost);
02599 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);
02600 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MAXRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_maxrxploss, rtp->rtcp->reported_maxlost);
02601 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MINRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_minrxploss, rtp->rtcp->reported_minlost);
02602 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_normdevrxploss, rtp->rtcp->reported_normdev_lost);
02603 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_STDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->remote_stdevrxploss, rtp->rtcp->reported_stdev_lost);
02604 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MAXRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_maxrxploss, rtp->rtcp->maxrxlost);
02605 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MINRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_minrxploss, rtp->rtcp->minrxlost);
02606 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_normdevrxploss, rtp->rtcp->normdev_rxlost);
02607 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_STDEVRXPLOSS, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, stats->local_stdevrxploss, rtp->rtcp->stdev_rxlost);
02608 AST_RTP_STAT_TERMINATOR(AST_RTP_INSTANCE_STAT_COMBINED_LOSS);
02609
02610 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_TXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->txjitter, rtp->rxjitter);
02611 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->rxjitter, rtp->rtcp->reported_jitter / (unsigned int) 65536.0);
02612 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MAXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_maxjitter, rtp->rtcp->reported_maxjitter);
02613 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_MINJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_minjitter, rtp->rtcp->reported_minjitter);
02614 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_normdevjitter, rtp->rtcp->reported_normdev_jitter);
02615 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_STDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->remote_stdevjitter, rtp->rtcp->reported_stdev_jitter);
02616 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MAXJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_maxjitter, rtp->rtcp->maxrxjitter);
02617 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_MINJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_minjitter, rtp->rtcp->minrxjitter);
02618 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_normdevjitter, rtp->rtcp->normdev_rxjitter);
02619 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_STDEVJITTER, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, stats->local_stdevjitter, rtp->rtcp->stdev_rxjitter);
02620 AST_RTP_STAT_TERMINATOR(AST_RTP_INSTANCE_STAT_COMBINED_JITTER);
02621
02622 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_RTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->rtt, rtp->rtcp->rtt);
02623 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_MAX_RTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->maxrtt, rtp->rtcp->maxrtt);
02624 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_MIN_RTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->minrtt, rtp->rtcp->minrtt);
02625 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_NORMDEVRTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->normdevrtt, rtp->rtcp->normdevrtt);
02626 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_STDEVRTT, AST_RTP_INSTANCE_STAT_COMBINED_RTT, stats->stdevrtt, rtp->rtcp->stdevrtt);
02627 AST_RTP_STAT_TERMINATOR(AST_RTP_INSTANCE_STAT_COMBINED_RTT);
02628
02629 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_LOCAL_SSRC, -1, stats->local_ssrc, rtp->ssrc);
02630 AST_RTP_STAT_SET(AST_RTP_INSTANCE_STAT_REMOTE_SSRC, -1, stats->remote_ssrc, rtp->themssrc);
02631
02632 return 0;
02633 }
02634
02635 static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1)
02636 {
02637
02638
02639
02640
02641
02642
02643
02644
02645
02646
02647 return (((ast_rtp_instance_get_prop(instance0, AST_RTP_PROPERTY_DTMF) != ast_rtp_instance_get_prop(instance1, AST_RTP_PROPERTY_DTMF)) ||
02648 (!chan0->tech->send_digit_begin != !chan1->tech->send_digit_begin)) ? 0 : 1);
02649 }
02650
02651 static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username)
02652 {
02653 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02654 struct sockaddr_in suggestion_tmp;
02655
02656 ast_sockaddr_to_sin(suggestion, &suggestion_tmp);
02657 ast_stun_request(rtp->s, &suggestion_tmp, username, NULL);
02658 ast_sockaddr_from_sin(suggestion, &suggestion_tmp);
02659 }
02660
02661 static void ast_rtp_stop(struct ast_rtp_instance *instance)
02662 {
02663 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02664 struct ast_sockaddr addr = { {0,} };
02665
02666 if (rtp->rtcp && rtp->rtcp->schedid > 0) {
02667 if (!ast_sched_del(rtp->sched, rtp->rtcp->schedid)) {
02668
02669 ao2_ref(instance, -1);
02670 }
02671 rtp->rtcp->schedid = -1;
02672 }
02673
02674 if (rtp->red) {
02675 AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
02676 free(rtp->red);
02677 rtp->red = NULL;
02678 }
02679
02680 ast_rtp_instance_set_remote_address(instance, &addr);
02681 if (rtp->rtcp) {
02682 ast_sockaddr_setnull(&rtp->rtcp->them);
02683 }
02684
02685 ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
02686 }
02687
02688 static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
02689 {
02690 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02691
02692 return ast_set_qos(rtp->s, tos, cos, desc);
02693 }
02694
02695
02696 static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level)
02697 {
02698 unsigned int *rtpheader;
02699 int hdrlen = 12;
02700 int res;
02701 struct ast_rtp_payload_type payload;
02702 char data[256];
02703 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
02704 struct ast_sockaddr remote_address = { {0,} };
02705
02706 ast_rtp_instance_get_remote_address(instance, &remote_address);
02707
02708 if (ast_sockaddr_isnull(&remote_address)) {
02709 return -1;
02710 }
02711
02712 payload = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(instance), AST_RTP_CN);
02713
02714 level = 127 - (level & 0x7f);
02715
02716 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
02717
02718
02719 rtpheader = (unsigned int *)data;
02720 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload.code << 16) | (rtp->seqno++));
02721 rtpheader[1] = htonl(rtp->lastts);
02722 rtpheader[2] = htonl(rtp->ssrc);
02723 data[12] = level;
02724
02725 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 1, 0, &remote_address);
02726
02727 if (res < 0) {
02728 ast_log(LOG_ERROR, "RTP Comfort Noise Transmission error to %s: %s\n", ast_sockaddr_stringify(&remote_address), strerror(errno));
02729 } else if (rtp_debug_test_addr(&remote_address)) {
02730 ast_verbose("Sent Comfort Noise RTP packet to %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
02731 ast_sockaddr_stringify(&remote_address),
02732 AST_RTP_CN, rtp->seqno, rtp->lastdigitts, res - hdrlen);
02733 }
02734
02735 return res;
02736 }
02737
02738 static char *rtp_do_debug_ip(struct ast_cli_args *a)
02739 {
02740 char *arg = ast_strdupa(a->argv[4]);
02741 char *debughost = NULL;
02742 char *debugport = NULL;
02743
02744 if (!ast_sockaddr_parse(&rtpdebugaddr, arg, 0) || !ast_sockaddr_split_hostport(arg, &debughost, &debugport, 0)) {
02745 ast_cli(a->fd, "Lookup failed for '%s'\n", arg);
02746 return CLI_FAILURE;
02747 }
02748 rtpdebugport = (!ast_strlen_zero(debugport) && debugport[0] != '0');
02749 ast_cli(a->fd, "RTP Debugging Enabled for address: %s\n",
02750 ast_sockaddr_stringify(&rtpdebugaddr));
02751 rtpdebug = 1;
02752 return CLI_SUCCESS;
02753 }
02754
02755 static char *rtcp_do_debug_ip(struct ast_cli_args *a)
02756 {
02757 char *arg = ast_strdupa(a->argv[4]);
02758 char *debughost = NULL;
02759 char *debugport = NULL;
02760
02761 if (!ast_sockaddr_parse(&rtcpdebugaddr, arg, 0) || !ast_sockaddr_split_hostport(arg, &debughost, &debugport, 0)) {
02762 ast_cli(a->fd, "Lookup failed for '%s'\n", arg);
02763 return CLI_FAILURE;
02764 }
02765 rtcpdebugport = (!ast_strlen_zero(debugport) && debugport[0] != '0');
02766 ast_cli(a->fd, "RTCP Debugging Enabled for address: %s\n",
02767 ast_sockaddr_stringify(&rtcpdebugaddr));
02768 rtcpdebug = 1;
02769 return CLI_SUCCESS;
02770 }
02771
02772 static char *handle_cli_rtp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02773 {
02774 switch (cmd) {
02775 case CLI_INIT:
02776 e->command = "rtp set debug {on|off|ip}";
02777 e->usage =
02778 "Usage: rtp set debug {on|off|ip host[:port]}\n"
02779 " Enable/Disable dumping of all RTP packets. If 'ip' is\n"
02780 " specified, limit the dumped packets to those to and from\n"
02781 " the specified 'host' with optional port.\n";
02782 return NULL;
02783 case CLI_GENERATE:
02784 return NULL;
02785 }
02786
02787 if (a->argc == e->args) {
02788 if (!strncasecmp(a->argv[e->args-1], "on", 2)) {
02789 rtpdebug = 1;
02790 memset(&rtpdebugaddr, 0, sizeof(rtpdebugaddr));
02791 ast_cli(a->fd, "RTP Debugging Enabled\n");
02792 return CLI_SUCCESS;
02793 } else if (!strncasecmp(a->argv[e->args-1], "off", 3)) {
02794 rtpdebug = 0;
02795 ast_cli(a->fd, "RTP Debugging Disabled\n");
02796 return CLI_SUCCESS;
02797 }
02798 } else if (a->argc == e->args +1) {
02799 return rtp_do_debug_ip(a);
02800 }
02801
02802 return CLI_SHOWUSAGE;
02803 }
02804
02805 static char *handle_cli_rtcp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02806 {
02807 switch (cmd) {
02808 case CLI_INIT:
02809 e->command = "rtcp set debug {on|off|ip}";
02810 e->usage =
02811 "Usage: rtcp set debug {on|off|ip host[:port]}\n"
02812 " Enable/Disable dumping of all RTCP packets. If 'ip' is\n"
02813 " specified, limit the dumped packets to those to and from\n"
02814 " the specified 'host' with optional port.\n";
02815 return NULL;
02816 case CLI_GENERATE:
02817 return NULL;
02818 }
02819
02820 if (a->argc == e->args) {
02821 if (!strncasecmp(a->argv[e->args-1], "on", 2)) {
02822 rtcpdebug = 1;
02823 memset(&rtcpdebugaddr, 0, sizeof(rtcpdebugaddr));
02824 ast_cli(a->fd, "RTCP Debugging Enabled\n");
02825 return CLI_SUCCESS;
02826 } else if (!strncasecmp(a->argv[e->args-1], "off", 3)) {
02827 rtcpdebug = 0;
02828 ast_cli(a->fd, "RTCP Debugging Disabled\n");
02829 return CLI_SUCCESS;
02830 }
02831 } else if (a->argc == e->args +1) {
02832 return rtcp_do_debug_ip(a);
02833 }
02834
02835 return CLI_SHOWUSAGE;
02836 }
02837
02838 static char *handle_cli_rtcp_set_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
02839 {
02840 switch (cmd) {
02841 case CLI_INIT:
02842 e->command = "rtcp set stats {on|off}";
02843 e->usage =
02844 "Usage: rtcp set stats {on|off}\n"
02845 " Enable/Disable dumping of RTCP stats.\n";
02846 return NULL;
02847 case CLI_GENERATE:
02848 return NULL;
02849 }
02850
02851 if (a->argc != e->args)
02852 return CLI_SHOWUSAGE;
02853
02854 if (!strncasecmp(a->argv[e->args-1], "on", 2))
02855 rtcpstats = 1;
02856 else if (!strncasecmp(a->argv[e->args-1], "off", 3))
02857 rtcpstats = 0;
02858 else
02859 return CLI_SHOWUSAGE;
02860
02861 ast_cli(a->fd, "RTCP Stats %s\n", rtcpstats ? "Enabled" : "Disabled");
02862 return CLI_SUCCESS;
02863 }
02864
02865 static struct ast_cli_entry cli_rtp[] = {
02866 AST_CLI_DEFINE(handle_cli_rtp_set_debug, "Enable/Disable RTP debugging"),
02867 AST_CLI_DEFINE(handle_cli_rtcp_set_debug, "Enable/Disable RTCP debugging"),
02868 AST_CLI_DEFINE(handle_cli_rtcp_set_stats, "Enable/Disable RTCP stats"),
02869 };
02870
02871 static int rtp_reload(int reload)
02872 {
02873 struct ast_config *cfg;
02874 const char *s;
02875 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
02876
02877 cfg = ast_config_load2("rtp.conf", "rtp", config_flags);
02878 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
02879 return 0;
02880 }
02881
02882 rtpstart = DEFAULT_RTP_START;
02883 rtpend = DEFAULT_RTP_END;
02884 dtmftimeout = DEFAULT_DTMF_TIMEOUT;
02885 strictrtp = STRICT_RTP_OPEN;
02886 if (cfg) {
02887 if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
02888 rtpstart = atoi(s);
02889 if (rtpstart < MINIMUM_RTP_PORT)
02890 rtpstart = MINIMUM_RTP_PORT;
02891 if (rtpstart > MAXIMUM_RTP_PORT)
02892 rtpstart = MAXIMUM_RTP_PORT;
02893 }
02894 if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
02895 rtpend = atoi(s);
02896 if (rtpend < MINIMUM_RTP_PORT)
02897 rtpend = MINIMUM_RTP_PORT;
02898 if (rtpend > MAXIMUM_RTP_PORT)
02899 rtpend = MAXIMUM_RTP_PORT;
02900 }
02901 if ((s = ast_variable_retrieve(cfg, "general", "rtcpinterval"))) {
02902 rtcpinterval = atoi(s);
02903 if (rtcpinterval == 0)
02904 rtcpinterval = 0;
02905 if (rtcpinterval < RTCP_MIN_INTERVALMS)
02906 rtcpinterval = RTCP_MIN_INTERVALMS;
02907 if (rtcpinterval > RTCP_MAX_INTERVALMS)
02908 rtcpinterval = RTCP_MAX_INTERVALMS;
02909 }
02910 if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
02911 #ifdef SO_NO_CHECK
02912 nochecksums = ast_false(s) ? 1 : 0;
02913 #else
02914 if (ast_false(s))
02915 ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
02916 #endif
02917 }
02918 if ((s = ast_variable_retrieve(cfg, "general", "dtmftimeout"))) {
02919 dtmftimeout = atoi(s);
02920 if ((dtmftimeout < 0) || (dtmftimeout > 64000)) {
02921 ast_log(LOG_WARNING, "DTMF timeout of '%d' outside range, using default of '%d' instead\n",
02922 dtmftimeout, DEFAULT_DTMF_TIMEOUT);
02923 dtmftimeout = DEFAULT_DTMF_TIMEOUT;
02924 };
02925 }
02926 if ((s = ast_variable_retrieve(cfg, "general", "strictrtp"))) {
02927 strictrtp = ast_true(s);
02928 }
02929 ast_config_destroy(cfg);
02930 }
02931 if (rtpstart >= rtpend) {
02932 ast_log(LOG_WARNING, "Unreasonable values for RTP start/end port in rtp.conf\n");
02933 rtpstart = DEFAULT_RTP_START;
02934 rtpend = DEFAULT_RTP_END;
02935 }
02936 ast_verb(2, "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
02937 return 0;
02938 }
02939
02940 static int reload_module(void)
02941 {
02942 rtp_reload(1);
02943 return 0;
02944 }
02945
02946 static int load_module(void)
02947 {
02948 if (ast_rtp_engine_register(&asterisk_rtp_engine)) {
02949 return AST_MODULE_LOAD_DECLINE;
02950 }
02951
02952 if (ast_cli_register_multiple(cli_rtp, ARRAY_LEN(cli_rtp))) {
02953 ast_rtp_engine_unregister(&asterisk_rtp_engine);
02954 return AST_MODULE_LOAD_DECLINE;
02955 }
02956
02957 rtp_reload(0);
02958
02959 return AST_MODULE_LOAD_SUCCESS;
02960 }
02961
02962 static int unload_module(void)
02963 {
02964 ast_rtp_engine_unregister(&asterisk_rtp_engine);
02965 ast_cli_unregister_multiple(cli_rtp, ARRAY_LEN(cli_rtp));
02966
02967 return 0;
02968 }
02969
02970 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk RTP Stack",
02971 .load = load_module,
02972 .unload = unload_module,
02973 .reload = reload_module,
02974 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
02975 );