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 #include "asterisk.h"
00030
00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 231512 $")
00032
00033 #include <sys/time.h>
00034 #include <signal.h>
00035 #include <fcntl.h>
00036 #include <math.h>
00037
00038 #include "asterisk/rtp.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/frame.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/acl.h"
00043 #include "asterisk/config.h"
00044 #include "asterisk/lock.h"
00045 #include "asterisk/utils.h"
00046 #include "asterisk/netsock.h"
00047 #include "asterisk/cli.h"
00048 #include "asterisk/manager.h"
00049 #include "asterisk/unaligned.h"
00050
00051 #define MAX_TIMESTAMP_SKEW 640
00052
00053 #define RTP_SEQ_MOD (1<<16)
00054 #define RTCP_DEFAULT_INTERVALMS 5000
00055 #define RTCP_MIN_INTERVALMS 500
00056 #define RTCP_MAX_INTERVALMS 60000
00057
00058 #define RTCP_PT_FUR 192
00059 #define RTCP_PT_SR 200
00060 #define RTCP_PT_RR 201
00061 #define RTCP_PT_SDES 202
00062 #define RTCP_PT_BYE 203
00063 #define RTCP_PT_APP 204
00064
00065 #define RTP_MTU 1200
00066
00067 #define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000))
00068
00069 static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
00070
00071 static int rtpstart = 5000;
00072 static int rtpend = 31000;
00073 static int rtpdebug;
00074 static int rtcpdebug;
00075 static int rtcpstats;
00076 static int rtcpinterval = RTCP_DEFAULT_INTERVALMS;
00077 static int stundebug;
00078 static struct sockaddr_in rtpdebugaddr;
00079 static struct sockaddr_in rtcpdebugaddr;
00080 #ifdef SO_NO_CHECK
00081 static int nochecksums;
00082 #endif
00083 static int strictrtp;
00084
00085 enum strict_rtp_state {
00086 STRICT_RTP_OPEN = 0,
00087 STRICT_RTP_LEARN,
00088 STRICT_RTP_CLOSED,
00089 };
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 struct rtpPayloadType {
00102 int isAstFormat;
00103 int code;
00104 };
00105
00106
00107
00108 struct ast_rtp {
00109 int s;
00110 struct ast_frame f;
00111 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
00112 unsigned int ssrc;
00113 unsigned int themssrc;
00114 unsigned int rxssrc;
00115 unsigned int lastts;
00116 unsigned int lastrxts;
00117 unsigned int lastividtimestamp;
00118 unsigned int lastovidtimestamp;
00119 unsigned int lastitexttimestamp;
00120 unsigned int lastotexttimestamp;
00121 unsigned int lasteventseqn;
00122 int lastrxseqno;
00123 unsigned short seedrxseqno;
00124 unsigned int seedrxts;
00125 unsigned int rxcount;
00126 unsigned int rxoctetcount;
00127 unsigned int txcount;
00128 unsigned int txoctetcount;
00129 unsigned int cycles;
00130 double rxjitter;
00131 double rxtransit;
00132 int lasttxformat;
00133 int lastrxformat;
00134
00135 int rtptimeout;
00136 int rtpholdtimeout;
00137 int rtpkeepalive;
00138
00139
00140 char resp;
00141 unsigned int lastevent;
00142 unsigned int dtmf_duration;
00143 unsigned int dtmf_timeout;
00144 unsigned int dtmfsamples;
00145
00146 unsigned int lastdigitts;
00147 char sending_digit;
00148 char send_digit;
00149 int send_payload;
00150 int send_duration;
00151 int nat;
00152 unsigned int flags;
00153 struct sockaddr_in us;
00154 struct sockaddr_in them;
00155 struct sockaddr_in altthem;
00156 struct timeval rxcore;
00157 struct timeval txcore;
00158 double drxcore;
00159 struct timeval lastrx;
00160 struct timeval dtmfmute;
00161 struct ast_smoother *smoother;
00162 int *ioid;
00163 unsigned short seqno;
00164 unsigned short rxseqno;
00165 struct sched_context *sched;
00166 struct io_context *io;
00167 void *data;
00168 ast_rtp_callback callback;
00169 #ifdef P2P_INTENSE
00170 ast_mutex_t bridge_lock;
00171 #endif
00172 struct rtpPayloadType current_RTP_PT[MAX_RTP_PT];
00173 int rtp_lookup_code_cache_isAstFormat;
00174 int rtp_lookup_code_cache_code;
00175 int rtp_lookup_code_cache_result;
00176 struct ast_rtcp *rtcp;
00177 struct ast_codec_pref pref;
00178 struct ast_rtp *bridged;
00179
00180 enum strict_rtp_state strict_rtp_state;
00181 struct sockaddr_in strict_rtp_address;
00182
00183 int set_marker_bit:1;
00184 unsigned int constantssrc:1;
00185 struct rtp_red *red;
00186 };
00187
00188 static struct ast_frame *red_t140_to_red(struct rtp_red *red);
00189 static int red_write(const void *data);
00190
00191 struct rtp_red {
00192 struct ast_frame t140;
00193 struct ast_frame t140red;
00194 unsigned char pt[RED_MAX_GENERATION];
00195 unsigned char ts[RED_MAX_GENERATION];
00196 unsigned char len[RED_MAX_GENERATION];
00197 int num_gen;
00198 int schedid;
00199 int ti;
00200 unsigned char t140red_data[64000];
00201 unsigned char buf_data[64000];
00202 int hdrlen;
00203 long int prev_ts;
00204 };
00205
00206
00207 static int ast_rtcp_write(const void *data);
00208 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw);
00209 static int ast_rtcp_write_sr(const void *data);
00210 static int ast_rtcp_write_rr(const void *data);
00211 static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp);
00212 static int ast_rtp_senddigit_continuation(struct ast_rtp *rtp);
00213 int ast_rtp_senddigit_end(struct ast_rtp *rtp, char digit);
00214
00215 #define FLAG_3389_WARNING (1 << 0)
00216 #define FLAG_NAT_ACTIVE (3 << 1)
00217 #define FLAG_NAT_INACTIVE (0 << 1)
00218 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
00219 #define FLAG_HAS_DTMF (1 << 3)
00220 #define FLAG_P2P_SENT_MARK (1 << 4)
00221 #define FLAG_P2P_NEED_DTMF (1 << 5)
00222 #define FLAG_CALLBACK_MODE (1 << 6)
00223 #define FLAG_DTMF_COMPENSATE (1 << 7)
00224 #define FLAG_HAS_STUN (1 << 8)
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 struct ast_rtcp {
00237 int rtcp_info;
00238 int s;
00239 struct sockaddr_in us;
00240 struct sockaddr_in them;
00241 struct sockaddr_in altthem;
00242 unsigned int soc;
00243 unsigned int spc;
00244 unsigned int themrxlsr;
00245 struct timeval rxlsr;
00246 struct timeval txlsr;
00247 unsigned int expected_prior;
00248 unsigned int received_prior;
00249 int schedid;
00250 unsigned int rr_count;
00251 unsigned int sr_count;
00252 unsigned int lastsrtxcount;
00253 double accumulated_transit;
00254 double rtt;
00255 unsigned int reported_jitter;
00256 unsigned int reported_lost;
00257 char quality[AST_MAX_USER_FIELD];
00258 char quality_jitter[AST_MAX_USER_FIELD];
00259 char quality_loss[AST_MAX_USER_FIELD];
00260 char quality_rtt[AST_MAX_USER_FIELD];
00261
00262 double reported_maxjitter;
00263 double reported_minjitter;
00264 double reported_normdev_jitter;
00265 double reported_stdev_jitter;
00266 unsigned int reported_jitter_count;
00267
00268 double reported_maxlost;
00269 double reported_minlost;
00270 double reported_normdev_lost;
00271 double reported_stdev_lost;
00272
00273 double rxlost;
00274 double maxrxlost;
00275 double minrxlost;
00276 double normdev_rxlost;
00277 double stdev_rxlost;
00278 unsigned int rxlost_count;
00279
00280 double maxrxjitter;
00281 double minrxjitter;
00282 double normdev_rxjitter;
00283 double stdev_rxjitter;
00284 unsigned int rxjitter_count;
00285 double maxrtt;
00286 double minrtt;
00287 double normdevrtt;
00288 double stdevrtt;
00289 unsigned int rtt_count;
00290 int sendfur;
00291 };
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 typedef struct { unsigned int id[4]; } __attribute__((packed)) stun_trans_id;
00320
00321 struct stun_header {
00322 unsigned short msgtype;
00323 unsigned short msglen;
00324 stun_trans_id id;
00325 unsigned char ies[0];
00326 } __attribute__((packed));
00327
00328 struct stun_attr {
00329 unsigned short attr;
00330 unsigned short len;
00331 unsigned char value[0];
00332 } __attribute__((packed));
00333
00334
00335
00336
00337 struct stun_addr {
00338 unsigned char unused;
00339 unsigned char family;
00340 unsigned short port;
00341 unsigned int addr;
00342 } __attribute__((packed));
00343
00344 #define STUN_IGNORE (0)
00345 #define STUN_ACCEPT (1)
00346
00347
00348
00349
00350
00351
00352
00353
00354 #define STUN_BINDREQ 0x0001
00355 #define STUN_BINDRESP 0x0101
00356 #define STUN_BINDERR 0x0111
00357 #define STUN_SECREQ 0x0002
00358 #define STUN_SECRESP 0x0102
00359 #define STUN_SECERR 0x0112
00360
00361
00362
00363
00364 #define STUN_MAPPED_ADDRESS 0x0001
00365 #define STUN_RESPONSE_ADDRESS 0x0002
00366 #define STUN_CHANGE_REQUEST 0x0003
00367 #define STUN_SOURCE_ADDRESS 0x0004
00368 #define STUN_CHANGED_ADDRESS 0x0005
00369 #define STUN_USERNAME 0x0006
00370 #define STUN_PASSWORD 0x0007
00371 #define STUN_MESSAGE_INTEGRITY 0x0008
00372 #define STUN_ERROR_CODE 0x0009
00373 #define STUN_UNKNOWN_ATTRIBUTES 0x000a
00374 #define STUN_REFLECTED_FROM 0x000b
00375
00376
00377 static const char *stun_msg2str(int msg)
00378 {
00379 switch (msg) {
00380 case STUN_BINDREQ:
00381 return "Binding Request";
00382 case STUN_BINDRESP:
00383 return "Binding Response";
00384 case STUN_BINDERR:
00385 return "Binding Error Response";
00386 case STUN_SECREQ:
00387 return "Shared Secret Request";
00388 case STUN_SECRESP:
00389 return "Shared Secret Response";
00390 case STUN_SECERR:
00391 return "Shared Secret Error Response";
00392 }
00393 return "Non-RFC3489 Message";
00394 }
00395
00396
00397 static const char *stun_attr2str(int msg)
00398 {
00399 switch (msg) {
00400 case STUN_MAPPED_ADDRESS:
00401 return "Mapped Address";
00402 case STUN_RESPONSE_ADDRESS:
00403 return "Response Address";
00404 case STUN_CHANGE_REQUEST:
00405 return "Change Request";
00406 case STUN_SOURCE_ADDRESS:
00407 return "Source Address";
00408 case STUN_CHANGED_ADDRESS:
00409 return "Changed Address";
00410 case STUN_USERNAME:
00411 return "Username";
00412 case STUN_PASSWORD:
00413 return "Password";
00414 case STUN_MESSAGE_INTEGRITY:
00415 return "Message Integrity";
00416 case STUN_ERROR_CODE:
00417 return "Error Code";
00418 case STUN_UNKNOWN_ATTRIBUTES:
00419 return "Unknown Attributes";
00420 case STUN_REFLECTED_FROM:
00421 return "Reflected From";
00422 }
00423 return "Non-RFC3489 Attribute";
00424 }
00425
00426
00427 struct stun_state {
00428 const char *username;
00429 const char *password;
00430 };
00431
00432 static int stun_process_attr(struct stun_state *state, struct stun_attr *attr)
00433 {
00434 if (stundebug)
00435 ast_verbose("Found STUN Attribute %s (%04x), length %d\n",
00436 stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
00437 switch (ntohs(attr->attr)) {
00438 case STUN_USERNAME:
00439 state->username = (const char *) (attr->value);
00440 break;
00441 case STUN_PASSWORD:
00442 state->password = (const char *) (attr->value);
00443 break;
00444 default:
00445 if (stundebug)
00446 ast_verbose("Ignoring STUN attribute %s (%04x), length %d\n",
00447 stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
00448 }
00449 return 0;
00450 }
00451
00452
00453 static void append_attr_string(struct stun_attr **attr, int attrval, const char *s, int *len, int *left)
00454 {
00455 int size = sizeof(**attr) + strlen(s);
00456 if (*left > size) {
00457 (*attr)->attr = htons(attrval);
00458 (*attr)->len = htons(strlen(s));
00459 memcpy((*attr)->value, s, strlen(s));
00460 (*attr) = (struct stun_attr *)((*attr)->value + strlen(s));
00461 *len += size;
00462 *left -= size;
00463 }
00464 }
00465
00466
00467 static void append_attr_address(struct stun_attr **attr, int attrval, struct sockaddr_in *sock_in, int *len, int *left)
00468 {
00469 int size = sizeof(**attr) + 8;
00470 struct stun_addr *addr;
00471 if (*left > size) {
00472 (*attr)->attr = htons(attrval);
00473 (*attr)->len = htons(8);
00474 addr = (struct stun_addr *)((*attr)->value);
00475 addr->unused = 0;
00476 addr->family = 0x01;
00477 addr->port = sock_in->sin_port;
00478 addr->addr = sock_in->sin_addr.s_addr;
00479 (*attr) = (struct stun_attr *)((*attr)->value + 8);
00480 *len += size;
00481 *left -= size;
00482 }
00483 }
00484
00485
00486 static int stun_send(int s, struct sockaddr_in *dst, struct stun_header *resp)
00487 {
00488 return sendto(s, resp, ntohs(resp->msglen) + sizeof(*resp), 0,
00489 (struct sockaddr *)dst, sizeof(*dst));
00490 }
00491
00492
00493 static void stun_req_id(struct stun_header *req)
00494 {
00495 int x;
00496 for (x = 0; x < 4; x++)
00497 req->id.id[x] = ast_random();
00498 }
00499
00500 size_t ast_rtp_alloc_size(void)
00501 {
00502 return sizeof(struct ast_rtp);
00503 }
00504
00505
00506 typedef int (stun_cb_f)(struct stun_attr *attr, void *arg);
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 static int stun_handle_packet(int s, struct sockaddr_in *src,
00517 unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg)
00518 {
00519 struct stun_header *hdr = (struct stun_header *)data;
00520 struct stun_attr *attr;
00521 struct stun_state st;
00522 int ret = STUN_IGNORE;
00523 int x;
00524
00525
00526
00527
00528
00529 if (len < sizeof(struct stun_header)) {
00530 ast_debug(1, "Runt STUN packet (only %d, wanting at least %d)\n", (int) len, (int) sizeof(struct stun_header));
00531 return -1;
00532 }
00533 len -= sizeof(struct stun_header);
00534 data += sizeof(struct stun_header);
00535 x = ntohs(hdr->msglen);
00536 if (stundebug)
00537 ast_verbose("STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr->msgtype)), ntohs(hdr->msgtype), x);
00538 if (x > len) {
00539 ast_debug(1, "Scrambled STUN packet length (got %d, expecting %d)\n", x, (int)len);
00540 } else
00541 len = x;
00542 memset(&st, 0, sizeof(st));
00543 while (len) {
00544 if (len < sizeof(struct stun_attr)) {
00545 ast_debug(1, "Runt Attribute (got %d, expecting %d)\n", (int)len, (int) sizeof(struct stun_attr));
00546 break;
00547 }
00548 attr = (struct stun_attr *)data;
00549
00550 x = ntohs(attr->len) + sizeof(struct stun_attr);
00551 if (x > len) {
00552 ast_debug(1, "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", x, (int)len);
00553 break;
00554 }
00555 if (stun_cb)
00556 stun_cb(attr, arg);
00557 if (stun_process_attr(&st, attr)) {
00558 ast_debug(1, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr));
00559 break;
00560 }
00561
00562
00563
00564 attr->attr = 0;
00565 data += x;
00566 len -= x;
00567 }
00568
00569
00570
00571
00572
00573
00574 *data = '\0';
00575
00576
00577
00578
00579 if (len == 0) {
00580 unsigned char respdata[1024];
00581 struct stun_header *resp = (struct stun_header *)respdata;
00582 int resplen = 0;
00583 int respleft = sizeof(respdata) - sizeof(struct stun_header);
00584
00585 resp->id = hdr->id;
00586 resp->msgtype = 0;
00587 resp->msglen = 0;
00588 attr = (struct stun_attr *)resp->ies;
00589 switch (ntohs(hdr->msgtype)) {
00590 case STUN_BINDREQ:
00591 if (stundebug)
00592 ast_verbose("STUN Bind Request, username: %s\n",
00593 st.username ? st.username : "<none>");
00594 if (st.username)
00595 append_attr_string(&attr, STUN_USERNAME, st.username, &resplen, &respleft);
00596 append_attr_address(&attr, STUN_MAPPED_ADDRESS, src, &resplen, &respleft);
00597 resp->msglen = htons(resplen);
00598 resp->msgtype = htons(STUN_BINDRESP);
00599 stun_send(s, src, resp);
00600 ret = STUN_ACCEPT;
00601 break;
00602 default:
00603 if (stundebug)
00604 ast_verbose("Dunno what to do with STUN message %04x (%s)\n", ntohs(hdr->msgtype), stun_msg2str(ntohs(hdr->msgtype)));
00605 }
00606 }
00607 return ret;
00608 }
00609
00610
00611
00612
00613
00614 static int stun_get_mapped(struct stun_attr *attr, void *arg)
00615 {
00616 struct stun_addr *addr = (struct stun_addr *)(attr + 1);
00617 struct sockaddr_in *sa = (struct sockaddr_in *)arg;
00618
00619 if (ntohs(attr->attr) != STUN_MAPPED_ADDRESS || ntohs(attr->len) != 8)
00620 return 1;
00621 sa->sin_port = addr->port;
00622 sa->sin_addr.s_addr = addr->addr;
00623 return 0;
00624 }
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640 int ast_stun_request(int s, struct sockaddr_in *dst,
00641 const char *username, struct sockaddr_in *answer)
00642 {
00643 struct stun_header *req;
00644 unsigned char reqdata[1024];
00645 int reqlen, reqleft;
00646 struct stun_attr *attr;
00647 int res = 0;
00648 int retry;
00649
00650 req = (struct stun_header *)reqdata;
00651 stun_req_id(req);
00652 reqlen = 0;
00653 reqleft = sizeof(reqdata) - sizeof(struct stun_header);
00654 req->msgtype = 0;
00655 req->msglen = 0;
00656 attr = (struct stun_attr *)req->ies;
00657 if (username)
00658 append_attr_string(&attr, STUN_USERNAME, username, &reqlen, &reqleft);
00659 req->msglen = htons(reqlen);
00660 req->msgtype = htons(STUN_BINDREQ);
00661 for (retry = 0; retry < 3; retry++) {
00662
00663 unsigned char reply_buf[1024];
00664 fd_set rfds;
00665 struct timeval to = { 3, 0 };
00666 struct sockaddr_in src;
00667 socklen_t srclen;
00668
00669 res = stun_send(s, dst, req);
00670 if (res < 0) {
00671 ast_log(LOG_WARNING, "ast_stun_request send #%d failed error %d, retry\n",
00672 retry, res);
00673 continue;
00674 }
00675 if (answer == NULL)
00676 break;
00677 FD_ZERO(&rfds);
00678 FD_SET(s, &rfds);
00679 res = ast_select(s + 1, &rfds, NULL, NULL, &to);
00680 if (res <= 0)
00681 continue;
00682 memset(&src, '\0', sizeof(src));
00683 srclen = sizeof(src);
00684
00685
00686
00687 res = recvfrom(s, reply_buf, sizeof(reply_buf) - 1,
00688 0, (struct sockaddr *)&src, &srclen);
00689 if (res < 0) {
00690 ast_log(LOG_WARNING, "ast_stun_request recvfrom #%d failed error %d, retry\n",
00691 retry, res);
00692 continue;
00693 }
00694 memset(answer, '\0', sizeof(struct sockaddr_in));
00695 stun_handle_packet(s, &src, reply_buf, res,
00696 stun_get_mapped, answer);
00697 res = 0;
00698 break;
00699 }
00700 return res;
00701 }
00702
00703
00704
00705
00706 void ast_rtp_stun_request(struct ast_rtp *rtp, struct sockaddr_in *suggestion, const char *username)
00707 {
00708 ast_stun_request(rtp->s, suggestion, username, NULL);
00709 }
00710
00711
00712 static AST_RWLIST_HEAD_STATIC(protos, ast_rtp_protocol);
00713
00714 static void timeval2ntp(struct timeval when, unsigned int *msw, unsigned int *lsw)
00715 {
00716 unsigned int sec, usec, frac;
00717 sec = when.tv_sec + 2208988800u;
00718 usec = when.tv_usec;
00719 frac = (usec << 12) + (usec << 8) - ((usec * 3650) >> 6);
00720 *msw = sec;
00721 *lsw = frac;
00722 }
00723
00724 int ast_rtp_fd(struct ast_rtp *rtp)
00725 {
00726 return rtp->s;
00727 }
00728
00729 int ast_rtcp_fd(struct ast_rtp *rtp)
00730 {
00731 if (rtp->rtcp)
00732 return rtp->rtcp->s;
00733 return -1;
00734 }
00735
00736 static int rtp_get_rate(int subclass)
00737 {
00738 return (subclass == AST_FORMAT_G722) ? 8000 : ast_format_rate(subclass);
00739 }
00740
00741 unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
00742 {
00743 unsigned int interval;
00744
00745
00746 interval = rtcpinterval;
00747 return interval;
00748 }
00749
00750
00751 void ast_rtp_set_rtptimers_onhold(struct ast_rtp *rtp)
00752 {
00753 rtp->rtptimeout = (-1) * rtp->rtptimeout;
00754 rtp->rtpholdtimeout = (-1) * rtp->rtpholdtimeout;
00755 }
00756
00757
00758 void ast_rtp_set_rtptimeout(struct ast_rtp *rtp, int timeout)
00759 {
00760 rtp->rtptimeout = timeout;
00761 }
00762
00763
00764 void ast_rtp_set_rtpholdtimeout(struct ast_rtp *rtp, int timeout)
00765 {
00766 rtp->rtpholdtimeout = timeout;
00767 }
00768
00769
00770 void ast_rtp_set_rtpkeepalive(struct ast_rtp *rtp, int period)
00771 {
00772 rtp->rtpkeepalive = period;
00773 }
00774
00775
00776 int ast_rtp_get_rtptimeout(struct ast_rtp *rtp)
00777 {
00778 if (rtp->rtptimeout < 0)
00779 return 0;
00780 return rtp->rtptimeout;
00781 }
00782
00783
00784 int ast_rtp_get_rtpholdtimeout(struct ast_rtp *rtp)
00785 {
00786 if (rtp->rtptimeout < 0)
00787 return 0;
00788 return rtp->rtpholdtimeout;
00789 }
00790
00791
00792 int ast_rtp_get_rtpkeepalive(struct ast_rtp *rtp)
00793 {
00794 return rtp->rtpkeepalive;
00795 }
00796
00797 void ast_rtp_set_data(struct ast_rtp *rtp, void *data)
00798 {
00799 rtp->data = data;
00800 }
00801
00802 void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback)
00803 {
00804 rtp->callback = callback;
00805 }
00806
00807 void ast_rtp_setnat(struct ast_rtp *rtp, int nat)
00808 {
00809 rtp->nat = nat;
00810 }
00811
00812 int ast_rtp_getnat(struct ast_rtp *rtp)
00813 {
00814 return ast_test_flag(rtp, FLAG_NAT_ACTIVE);
00815 }
00816
00817 void ast_rtp_setdtmf(struct ast_rtp *rtp, int dtmf)
00818 {
00819 ast_set2_flag(rtp, dtmf ? 1 : 0, FLAG_HAS_DTMF);
00820 }
00821
00822 void ast_rtp_setdtmfcompensate(struct ast_rtp *rtp, int compensate)
00823 {
00824 ast_set2_flag(rtp, compensate ? 1 : 0, FLAG_DTMF_COMPENSATE);
00825 }
00826
00827 void ast_rtp_setstun(struct ast_rtp *rtp, int stun_enable)
00828 {
00829 ast_set2_flag(rtp, stun_enable ? 1 : 0, FLAG_HAS_STUN);
00830 }
00831
00832 static void rtp_bridge_lock(struct ast_rtp *rtp)
00833 {
00834 #ifdef P2P_INTENSE
00835 ast_mutex_lock(&rtp->bridge_lock);
00836 #endif
00837 return;
00838 }
00839
00840 static void rtp_bridge_unlock(struct ast_rtp *rtp)
00841 {
00842 #ifdef P2P_INTENSE
00843 ast_mutex_unlock(&rtp->bridge_lock);
00844 #endif
00845 return;
00846 }
00847
00848
00849 static double normdev_compute(double normdev, double sample, unsigned int sample_count)
00850 {
00851 normdev = normdev * sample_count + sample;
00852 sample_count++;
00853
00854 return normdev / sample_count;
00855 }
00856
00857 static double stddev_compute(double stddev, double sample, double normdev, double normdev_curent, unsigned int sample_count)
00858 {
00859
00860
00861
00862
00863
00864
00865 #define SQUARE(x) ((x) * (x))
00866
00867 stddev = sample_count * stddev;
00868 sample_count++;
00869
00870 return stddev +
00871 ( sample_count * SQUARE( (sample - normdev) / sample_count ) ) +
00872 ( SQUARE(sample - normdev_curent) / sample_count );
00873
00874 #undef SQUARE
00875 }
00876
00877 static struct ast_frame *send_dtmf(struct ast_rtp *rtp, enum ast_frame_type type)
00878 {
00879 if (((ast_test_flag(rtp, FLAG_DTMF_COMPENSATE) && type == AST_FRAME_DTMF_END) ||
00880 (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
00881 ast_debug(1, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(rtp->them.sin_addr));
00882 rtp->resp = 0;
00883 rtp->dtmfsamples = 0;
00884 return &ast_null_frame;
00885 }
00886 ast_debug(1, "Sending dtmf: %d (%c), at %s\n", rtp->resp, rtp->resp, ast_inet_ntoa(rtp->them.sin_addr));
00887 if (rtp->resp == 'X') {
00888 rtp->f.frametype = AST_FRAME_CONTROL;
00889 rtp->f.subclass = AST_CONTROL_FLASH;
00890 } else {
00891 rtp->f.frametype = type;
00892 rtp->f.subclass = rtp->resp;
00893 }
00894 rtp->f.datalen = 0;
00895 rtp->f.samples = 0;
00896 rtp->f.mallocd = 0;
00897 rtp->f.src = "RTP";
00898 return &rtp->f;
00899
00900 }
00901
00902 static inline int rtp_debug_test_addr(struct sockaddr_in *addr)
00903 {
00904 if (rtpdebug == 0)
00905 return 0;
00906 if (rtpdebugaddr.sin_addr.s_addr) {
00907 if (((ntohs(rtpdebugaddr.sin_port) != 0)
00908 && (rtpdebugaddr.sin_port != addr->sin_port))
00909 || (rtpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
00910 return 0;
00911 }
00912 return 1;
00913 }
00914
00915 static inline int rtcp_debug_test_addr(struct sockaddr_in *addr)
00916 {
00917 if (rtcpdebug == 0)
00918 return 0;
00919 if (rtcpdebugaddr.sin_addr.s_addr) {
00920 if (((ntohs(rtcpdebugaddr.sin_port) != 0)
00921 && (rtcpdebugaddr.sin_port != addr->sin_port))
00922 || (rtcpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
00923 return 0;
00924 }
00925 return 1;
00926 }
00927
00928
00929 static struct ast_frame *process_cisco_dtmf(struct ast_rtp *rtp, unsigned char *data, int len)
00930 {
00931 unsigned int event;
00932 char resp = 0;
00933 struct ast_frame *f = NULL;
00934 unsigned char seq;
00935 unsigned int flags;
00936 unsigned int power;
00937
00938
00939 if (len < 4)
00940 return f;
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972 seq = data[0];
00973 flags = data[1];
00974 power = data[2];
00975 event = data[3] & 0x1f;
00976
00977 if (option_debug > 2 || rtpdebug)
00978 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);
00979 if (event < 10) {
00980 resp = '0' + event;
00981 } else if (event < 11) {
00982 resp = '*';
00983 } else if (event < 12) {
00984 resp = '#';
00985 } else if (event < 16) {
00986 resp = 'A' + (event - 12);
00987 } else if (event < 17) {
00988 resp = 'X';
00989 }
00990 if ((!rtp->resp && power) || (rtp->resp && (rtp->resp != resp))) {
00991 rtp->resp = resp;
00992
00993 if (!ast_test_flag(rtp, FLAG_DTMF_COMPENSATE)) {
00994 f = send_dtmf(rtp, AST_FRAME_DTMF_BEGIN);
00995 rtp->dtmfsamples = 0;
00996 }
00997 } else if ((rtp->resp == resp) && !power) {
00998 f = send_dtmf(rtp, AST_FRAME_DTMF_END);
00999 f->samples = rtp->dtmfsamples * (rtp_get_rate(f->subclass) / 1000);
01000 rtp->resp = 0;
01001 } else if (rtp->resp == resp)
01002 rtp->dtmfsamples += 20 * (rtp_get_rate(f->subclass) / 1000);
01003 rtp->dtmf_timeout = dtmftimeout;
01004 return f;
01005 }
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019 static struct ast_frame *process_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp)
01020 {
01021 unsigned int event;
01022 unsigned int event_end;
01023 unsigned int samples;
01024 char resp = 0;
01025 struct ast_frame *f = NULL;
01026
01027
01028 event = ntohl(*((unsigned int *)(data)));
01029 event >>= 24;
01030 event_end = ntohl(*((unsigned int *)(data)));
01031 event_end <<= 8;
01032 event_end >>= 24;
01033 samples = ntohl(*((unsigned int *)(data)));
01034 samples &= 0xFFFF;
01035
01036
01037 if (rtpdebug || option_debug > 2)
01038 ast_debug(0, "- RTP 2833 Event: %08x (len = %d)\n", event, len);
01039
01040
01041 if (event < 10) {
01042 resp = '0' + event;
01043 } else if (event < 11) {
01044 resp = '*';
01045 } else if (event < 12) {
01046 resp = '#';
01047 } else if (event < 16) {
01048 resp = 'A' + (event - 12);
01049 } else if (event < 17) {
01050 resp = 'X';
01051 } else {
01052
01053 ast_log(LOG_DEBUG, "Ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", event);
01054 return &ast_null_frame;
01055 }
01056
01057 if (ast_test_flag(rtp, FLAG_DTMF_COMPENSATE)) {
01058 if ((rtp->lastevent != timestamp) || (rtp->resp && rtp->resp != resp)) {
01059 rtp->resp = resp;
01060 rtp->dtmf_timeout = 0;
01061 f = send_dtmf(rtp, AST_FRAME_DTMF_END);
01062 f->len = 0;
01063 rtp->lastevent = timestamp;
01064 }
01065 } else {
01066
01067
01068
01069
01070
01071 unsigned int new_duration = rtp->dtmf_duration;
01072 unsigned int last_duration = new_duration & 0xFFFF;
01073
01074 if (last_duration > 64000 && samples < last_duration)
01075 new_duration += 0xFFFF + 1;
01076 new_duration = (new_duration & ~0xFFFF) | samples;
01077
01078 if (event_end & 0x80) {
01079
01080 if ((rtp->lastevent != seqno) && rtp->resp) {
01081 rtp->dtmf_duration = new_duration;
01082 f = send_dtmf(rtp, AST_FRAME_DTMF_END);
01083 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass)), ast_tv(0, 0));
01084 rtp->resp = 0;
01085 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
01086 }
01087 } else {
01088
01089
01090 if (rtp->resp && rtp->resp != resp) {
01091
01092 f = send_dtmf(rtp, AST_FRAME_DTMF_END);
01093 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass)), ast_tv(0, 0));
01094 rtp->resp = 0;
01095 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
01096 }
01097
01098
01099 if (rtp->resp) {
01100
01101 rtp->dtmf_duration = new_duration;
01102 } else {
01103
01104 rtp->resp = resp;
01105 f = send_dtmf(rtp, AST_FRAME_DTMF_BEGIN);
01106 rtp->dtmf_duration = samples;
01107 }
01108
01109 rtp->dtmf_timeout = timestamp + rtp->dtmf_duration + dtmftimeout;
01110 }
01111
01112 rtp->lastevent = seqno;
01113 }
01114
01115 rtp->dtmfsamples = samples;
01116
01117 return f;
01118 }
01119
01120
01121
01122
01123
01124
01125
01126 static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
01127 {
01128 struct ast_frame *f = NULL;
01129
01130
01131
01132 if (rtpdebug)
01133 ast_debug(0, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", rtp->lastrxformat, len);
01134
01135 if (!(ast_test_flag(rtp, FLAG_3389_WARNING))) {
01136 ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client IP: %s\n",
01137 ast_inet_ntoa(rtp->them.sin_addr));
01138 ast_set_flag(rtp, FLAG_3389_WARNING);
01139 }
01140
01141
01142 if (!len)
01143 return NULL;
01144 if (len < 24) {
01145 rtp->f.data.ptr = rtp->rawdata + AST_FRIENDLY_OFFSET;
01146 rtp->f.datalen = len - 1;
01147 rtp->f.offset = AST_FRIENDLY_OFFSET;
01148 memcpy(rtp->f.data.ptr, data + 1, len - 1);
01149 } else {
01150 rtp->f.data.ptr = NULL;
01151 rtp->f.offset = 0;
01152 rtp->f.datalen = 0;
01153 }
01154 rtp->f.frametype = AST_FRAME_CNG;
01155 rtp->f.subclass = data[0] & 0x7f;
01156 rtp->f.samples = 0;
01157 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
01158 f = &rtp->f;
01159 return f;
01160 }
01161
01162 static int rtpread(int *id, int fd, short events, void *cbdata)
01163 {
01164 struct ast_rtp *rtp = cbdata;
01165 struct ast_frame *f;
01166 f = ast_rtp_read(rtp);
01167 if (f) {
01168 if (rtp->callback)
01169 rtp->callback(rtp, f, rtp->data);
01170 }
01171 return 1;
01172 }
01173
01174 struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp)
01175 {
01176 socklen_t len;
01177 int position, i, packetwords;
01178 int res;
01179 struct sockaddr_in sock_in;
01180 unsigned int rtcpdata[8192 + AST_FRIENDLY_OFFSET];
01181 unsigned int *rtcpheader;
01182 int pt;
01183 struct timeval now;
01184 unsigned int length;
01185 int rc;
01186 double rttsec;
01187 uint64_t rtt = 0;
01188 unsigned int dlsr;
01189 unsigned int lsr;
01190 unsigned int msw;
01191 unsigned int lsw;
01192 unsigned int comp;
01193 struct ast_frame *f = &ast_null_frame;
01194
01195 double reported_jitter;
01196 double reported_normdev_jitter_current;
01197 double normdevrtt_current;
01198 double reported_lost;
01199 double reported_normdev_lost_current;
01200
01201 if (!rtp || !rtp->rtcp)
01202 return &ast_null_frame;
01203
01204 len = sizeof(sock_in);
01205
01206 res = recvfrom(rtp->rtcp->s, rtcpdata + AST_FRIENDLY_OFFSET, sizeof(rtcpdata) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET,
01207 0, (struct sockaddr *)&sock_in, &len);
01208 rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
01209
01210 if (res < 0) {
01211 ast_assert(errno != EBADF);
01212 if (errno != EAGAIN) {
01213 ast_log(LOG_WARNING, "RTCP Read error: %s. Hanging up.\n", strerror(errno));
01214 return NULL;
01215 }
01216 return &ast_null_frame;
01217 }
01218
01219 packetwords = res / 4;
01220
01221 if (rtp->nat) {
01222
01223 if (((rtp->rtcp->them.sin_addr.s_addr != sock_in.sin_addr.s_addr) ||
01224 (rtp->rtcp->them.sin_port != sock_in.sin_port)) &&
01225 ((rtp->rtcp->altthem.sin_addr.s_addr != sock_in.sin_addr.s_addr) ||
01226 (rtp->rtcp->altthem.sin_port != sock_in.sin_port))) {
01227 memcpy(&rtp->rtcp->them, &sock_in, sizeof(rtp->rtcp->them));
01228 if (option_debug || rtpdebug)
01229 ast_debug(0, "RTCP NAT: Got RTCP from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
01230 }
01231 }
01232
01233 ast_debug(1, "Got RTCP report of %d bytes\n", res);
01234
01235
01236 position = 0;
01237 while (position < packetwords) {
01238 i = position;
01239 length = ntohl(rtcpheader[i]);
01240 pt = (length & 0xff0000) >> 16;
01241 rc = (length & 0x1f000000) >> 24;
01242 length &= 0xffff;
01243
01244 if ((i + length) > packetwords) {
01245 if (option_debug || rtpdebug)
01246 ast_log(LOG_DEBUG, "RTCP Read too short\n");
01247 return &ast_null_frame;
01248 }
01249
01250 if (rtcp_debug_test_addr(&sock_in)) {
01251 ast_verbose("\n\nGot RTCP from %s:%d\n", ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port));
01252 ast_verbose("PT: %d(%s)\n", pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown");
01253 ast_verbose("Reception reports: %d\n", rc);
01254 ast_verbose("SSRC of sender: %u\n", rtcpheader[i + 1]);
01255 }
01256
01257 i += 2;
01258
01259 switch (pt) {
01260 case RTCP_PT_SR:
01261 gettimeofday(&rtp->rtcp->rxlsr,NULL);
01262 rtp->rtcp->spc = ntohl(rtcpheader[i+3]);
01263 rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
01264 rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16);
01265
01266 if (rtcp_debug_test_addr(&sock_in)) {
01267 ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader[i]), (unsigned long) ntohl(rtcpheader[i + 1]) * 4096);
01268 ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader[i + 2]));
01269 ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader[i + 3]), (unsigned long) ntohl(rtcpheader[i + 4]));
01270 }
01271 i += 5;
01272 if (rc < 1)
01273 break;
01274
01275 case RTCP_PT_RR:
01276
01277
01278 gettimeofday(&now, NULL);
01279 timeval2ntp(now, &msw, &lsw);
01280 if (ntohl(rtcpheader[i + 4]) && ntohl(rtcpheader[i + 5])) {
01281 comp = ((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16);
01282 lsr = ntohl(rtcpheader[i + 4]);
01283 dlsr = ntohl(rtcpheader[i + 5]);
01284 rtt = comp - lsr - dlsr;
01285
01286
01287
01288 if (rtt < 4294) {
01289 rtt = (rtt * 1000000) >> 16;
01290 } else {
01291 rtt = (rtt * 1000) >> 16;
01292 rtt *= 1000;
01293 }
01294 rtt = rtt / 1000.;
01295 rttsec = rtt / 1000.;
01296 rtp->rtcp->rtt = rttsec;
01297
01298 if (comp - dlsr >= lsr) {
01299 rtp->rtcp->accumulated_transit += rttsec;
01300
01301 if (rtp->rtcp->rtt_count == 0)
01302 rtp->rtcp->minrtt = rttsec;
01303
01304 if (rtp->rtcp->maxrtt<rttsec)
01305 rtp->rtcp->maxrtt = rttsec;
01306
01307 if (rtp->rtcp->minrtt>rttsec)
01308 rtp->rtcp->minrtt = rttsec;
01309
01310 normdevrtt_current = normdev_compute(rtp->rtcp->normdevrtt, rttsec, rtp->rtcp->rtt_count);
01311
01312 rtp->rtcp->stdevrtt = stddev_compute(rtp->rtcp->stdevrtt, rttsec, rtp->rtcp->normdevrtt, normdevrtt_current, rtp->rtcp->rtt_count);
01313
01314 rtp->rtcp->normdevrtt = normdevrtt_current;
01315
01316 rtp->rtcp->rtt_count++;
01317 } else if (rtcp_debug_test_addr(&sock_in)) {
01318 ast_verbose("Internal RTCP NTP clock skew detected: "
01319 "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
01320 "diff=%d\n",
01321 lsr, comp, dlsr, dlsr / 65536,
01322 (dlsr % 65536) * 1000 / 65536,
01323 dlsr - (comp - lsr));
01324 }
01325 }
01326
01327 rtp->rtcp->reported_jitter = ntohl(rtcpheader[i + 3]);
01328 reported_jitter = (double) rtp->rtcp->reported_jitter;
01329
01330 if (rtp->rtcp->reported_jitter_count == 0)
01331 rtp->rtcp->reported_minjitter = reported_jitter;
01332
01333 if (reported_jitter < rtp->rtcp->reported_minjitter)
01334 rtp->rtcp->reported_minjitter = reported_jitter;
01335
01336 if (reported_jitter > rtp->rtcp->reported_maxjitter)
01337 rtp->rtcp->reported_maxjitter = reported_jitter;
01338
01339 reported_normdev_jitter_current = normdev_compute(rtp->rtcp->reported_normdev_jitter, reported_jitter, rtp->rtcp->reported_jitter_count);
01340
01341 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);
01342
01343 rtp->rtcp->reported_normdev_jitter = reported_normdev_jitter_current;
01344
01345 rtp->rtcp->reported_lost = ntohl(rtcpheader[i + 1]) & 0xffffff;
01346
01347 reported_lost = (double) rtp->rtcp->reported_lost;
01348
01349
01350 if (rtp->rtcp->reported_jitter_count == 0)
01351 rtp->rtcp->reported_minlost = reported_lost;
01352
01353 if (reported_lost < rtp->rtcp->reported_minlost)
01354 rtp->rtcp->reported_minlost = reported_lost;
01355
01356 if (reported_lost > rtp->rtcp->reported_maxlost)
01357 rtp->rtcp->reported_maxlost = reported_lost;
01358
01359 reported_normdev_lost_current = normdev_compute(rtp->rtcp->reported_normdev_lost, reported_lost, rtp->rtcp->reported_jitter_count);
01360
01361 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);
01362
01363 rtp->rtcp->reported_normdev_lost = reported_normdev_lost_current;
01364
01365 rtp->rtcp->reported_jitter_count++;
01366
01367 if (rtcp_debug_test_addr(&sock_in)) {
01368 ast_verbose(" Fraction lost: %ld\n", (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24));
01369 ast_verbose(" Packets lost so far: %d\n", rtp->rtcp->reported_lost);
01370 ast_verbose(" Highest sequence number: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff));
01371 ast_verbose(" Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16);
01372 ast_verbose(" Interarrival jitter: %u\n", rtp->rtcp->reported_jitter);
01373 ast_verbose(" Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader[i + 4]) >> 16,((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096);
01374 ast_verbose(" DLSR: %4.4f (sec)\n",ntohl(rtcpheader[i + 5])/65536.0);
01375 if (rtt)
01376 ast_verbose(" RTT: %lu(sec)\n", (unsigned long) rtt);
01377 }
01378
01379 if (rtt) {
01380 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s:%d\r\n"
01381 "PT: %d(%s)\r\n"
01382 "ReceptionReports: %d\r\n"
01383 "SenderSSRC: %u\r\n"
01384 "FractionLost: %ld\r\n"
01385 "PacketsLost: %d\r\n"
01386 "HighestSequence: %ld\r\n"
01387 "SequenceNumberCycles: %ld\r\n"
01388 "IAJitter: %u\r\n"
01389 "LastSR: %lu.%010lu\r\n"
01390 "DLSR: %4.4f(sec)\r\n"
01391 "RTT: %llu(sec)\r\n",
01392 ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port),
01393 pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
01394 rc,
01395 rtcpheader[i + 1],
01396 (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
01397 rtp->rtcp->reported_lost,
01398 (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
01399 (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16,
01400 rtp->rtcp->reported_jitter,
01401 (unsigned long) ntohl(rtcpheader[i + 4]) >> 16, ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
01402 ntohl(rtcpheader[i + 5])/65536.0,
01403 (unsigned long long)rtt);
01404 } else {
01405 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s:%d\r\n"
01406 "PT: %d(%s)\r\n"
01407 "ReceptionReports: %d\r\n"
01408 "SenderSSRC: %u\r\n"
01409 "FractionLost: %ld\r\n"
01410 "PacketsLost: %d\r\n"
01411 "HighestSequence: %ld\r\n"
01412 "SequenceNumberCycles: %ld\r\n"
01413 "IAJitter: %u\r\n"
01414 "LastSR: %lu.%010lu\r\n"
01415 "DLSR: %4.4f(sec)\r\n",
01416 ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port),
01417 pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
01418 rc,
01419 rtcpheader[i + 1],
01420 (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
01421 rtp->rtcp->reported_lost,
01422 (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
01423 (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16,
01424 rtp->rtcp->reported_jitter,
01425 (unsigned long) ntohl(rtcpheader[i + 4]) >> 16,
01426 ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
01427 ntohl(rtcpheader[i + 5])/65536.0);
01428 }
01429 break;
01430 case RTCP_PT_FUR:
01431 if (rtcp_debug_test_addr(&sock_in))
01432 ast_verbose("Received an RTCP Fast Update Request\n");
01433 rtp->f.frametype = AST_FRAME_CONTROL;
01434 rtp->f.subclass = AST_CONTROL_VIDUPDATE;
01435 rtp->f.datalen = 0;
01436 rtp->f.samples = 0;
01437 rtp->f.mallocd = 0;
01438 rtp->f.src = "RTP";
01439 f = &rtp->f;
01440 break;
01441 case RTCP_PT_SDES:
01442 if (rtcp_debug_test_addr(&sock_in))
01443 ast_verbose("Received an SDES from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
01444 break;
01445 case RTCP_PT_BYE:
01446 if (rtcp_debug_test_addr(&sock_in))
01447 ast_verbose("Received a BYE from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
01448 break;
01449 default:
01450 ast_debug(1, "Unknown RTCP packet (pt=%d) received from %s:%d\n", pt, ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
01451 break;
01452 }
01453 position += (length + 1);
01454 }
01455 rtp->rtcp->rtcp_info = 1;
01456 return f;
01457 }
01458
01459 static void sanitize_tv(struct timeval *tv)
01460 {
01461 while (tv->tv_usec < 0) {
01462 tv->tv_usec += 1000000;
01463 tv->tv_sec -= 1;
01464 }
01465 while (tv->tv_usec >= 1000000) {
01466 tv->tv_usec -= 1000000;
01467 tv->tv_sec += 1;
01468 }
01469 }
01470
01471 static void calc_rxstamp(struct timeval *when, struct ast_rtp *rtp, unsigned int timestamp, int mark)
01472 {
01473 struct timeval now;
01474 double transit;
01475 double current_time;
01476 double d;
01477 double dtv;
01478 double prog;
01479 double normdev_rxjitter_current;
01480 int rate = rtp_get_rate(rtp->f.subclass);
01481
01482 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
01483 gettimeofday(&rtp->rxcore, NULL);
01484 rtp->drxcore = (double) rtp->rxcore.tv_sec + (double) rtp->rxcore.tv_usec / 1000000;
01485
01486 rtp->seedrxts = timestamp;
01487 rtp->rxcore.tv_sec -= timestamp / rate;
01488 rtp->rxcore.tv_usec -= (timestamp % rate) * 125;
01489
01490 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
01491 sanitize_tv(&rtp->rxcore);
01492 }
01493
01494 gettimeofday(&now,NULL);
01495
01496 when->tv_sec = rtp->rxcore.tv_sec + timestamp / rate;
01497 when->tv_usec = rtp->rxcore.tv_usec + (timestamp % rate) * 125;
01498 sanitize_tv(when);
01499 prog = (double)((timestamp-rtp->seedrxts)/(float)(rate));
01500 dtv = (double)rtp->drxcore + (double)(prog);
01501 current_time = (double)now.tv_sec + (double)now.tv_usec/1000000;
01502 transit = current_time - dtv;
01503 d = transit - rtp->rxtransit;
01504 rtp->rxtransit = transit;
01505 if (d<0)
01506 d=-d;
01507 rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
01508
01509 if (rtp->rtcp) {
01510 if (rtp->rxjitter > rtp->rtcp->maxrxjitter)
01511 rtp->rtcp->maxrxjitter = rtp->rxjitter;
01512 if (rtp->rtcp->rxjitter_count == 1)
01513 rtp->rtcp->minrxjitter = rtp->rxjitter;
01514 if (rtp->rxjitter < rtp->rtcp->minrxjitter)
01515 rtp->rtcp->minrxjitter = rtp->rxjitter;
01516
01517 normdev_rxjitter_current = normdev_compute(rtp->rtcp->normdev_rxjitter,rtp->rxjitter,rtp->rtcp->rxjitter_count);
01518 rtp->rtcp->stdev_rxjitter = stddev_compute(rtp->rtcp->stdev_rxjitter,rtp->rxjitter,rtp->rtcp->normdev_rxjitter,normdev_rxjitter_current,rtp->rtcp->rxjitter_count);
01519
01520 rtp->rtcp->normdev_rxjitter = normdev_rxjitter_current;
01521 rtp->rtcp->rxjitter_count++;
01522 }
01523 }
01524
01525
01526 static int bridge_p2p_rtp_write(struct ast_rtp *rtp, struct ast_rtp *bridged, unsigned int *rtpheader, int len, int hdrlen)
01527 {
01528 int res = 0, payload = 0, bridged_payload = 0, mark;
01529 struct rtpPayloadType rtpPT;
01530 int reconstruct = ntohl(rtpheader[0]);
01531
01532
01533 payload = (reconstruct & 0x7f0000) >> 16;
01534 mark = (((reconstruct & 0x800000) >> 23) != 0);
01535
01536
01537 rtpPT = ast_rtp_lookup_pt(rtp, payload);
01538
01539
01540 if (ast_test_flag(rtp, FLAG_P2P_NEED_DTMF) && !rtpPT.isAstFormat && rtpPT.code == AST_RTP_DTMF)
01541 return -1;
01542
01543
01544 bridged_payload = ast_rtp_lookup_code(bridged, rtpPT.isAstFormat, rtpPT.code);
01545
01546
01547 if (!bridged->current_RTP_PT[bridged_payload].code)
01548 return -1;
01549
01550
01551
01552 if (!ast_test_flag(rtp, FLAG_P2P_SENT_MARK)) {
01553 mark = 1;
01554 ast_set_flag(rtp, FLAG_P2P_SENT_MARK);
01555 }
01556
01557
01558 reconstruct &= 0xFF80FFFF;
01559 reconstruct |= (bridged_payload << 16);
01560 reconstruct |= (mark << 23);
01561 rtpheader[0] = htonl(reconstruct);
01562
01563
01564 res = sendto(bridged->s, (void *)rtpheader, len, 0, (struct sockaddr *)&bridged->them, sizeof(bridged->them));
01565 if (res < 0) {
01566 if (!bridged->nat || (bridged->nat && (ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
01567 ast_debug(1, "RTP Transmission error of packet to %s:%d: %s\n", ast_inet_ntoa(bridged->them.sin_addr), ntohs(bridged->them.sin_port), strerror(errno));
01568 } else if (((ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(bridged, FLAG_NAT_INACTIVE_NOWARN)) {
01569 if (option_debug || rtpdebug)
01570 ast_debug(0, "RTP NAT: Can't write RTP to private address %s:%d, waiting for other end to send audio...\n", ast_inet_ntoa(bridged->them.sin_addr), ntohs(bridged->them.sin_port));
01571 ast_set_flag(bridged, FLAG_NAT_INACTIVE_NOWARN);
01572 }
01573 return 0;
01574 } else if (rtp_debug_test_addr(&bridged->them))
01575 ast_verbose("Sent RTP P2P packet to %s:%u (type %-2.2d, len %-6.6u)\n", ast_inet_ntoa(bridged->them.sin_addr), ntohs(bridged->them.sin_port), bridged_payload, len - hdrlen);
01576
01577 return 0;
01578 }
01579
01580 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
01581 {
01582 int res;
01583 struct sockaddr_in sock_in;
01584 socklen_t len;
01585 unsigned int seqno;
01586 int version;
01587 int payloadtype;
01588 int hdrlen = 12;
01589 int padding;
01590 int mark;
01591 int ext;
01592 int cc;
01593 unsigned int ssrc;
01594 unsigned int timestamp;
01595 unsigned int *rtpheader;
01596 struct rtpPayloadType rtpPT;
01597 struct ast_rtp *bridged = NULL;
01598 int prev_seqno;
01599
01600
01601 if (rtp->sending_digit)
01602 ast_rtp_senddigit_continuation(rtp);
01603
01604 len = sizeof(sock_in);
01605
01606
01607 res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
01608 0, (struct sockaddr *)&sock_in, &len);
01609
01610
01611 if (rtp->strict_rtp_state == STRICT_RTP_LEARN) {
01612
01613 memcpy(&rtp->strict_rtp_address, &sock_in, sizeof(rtp->strict_rtp_address));
01614
01615 rtp->strict_rtp_state = STRICT_RTP_CLOSED;
01616 ast_debug(1, "Learned remote address is %s:%d for strict RTP purposes, now protecting the port.\n", ast_inet_ntoa(rtp->strict_rtp_address.sin_addr), ntohs(rtp->strict_rtp_address.sin_port));
01617 } else if (rtp->strict_rtp_state == STRICT_RTP_CLOSED) {
01618
01619 if ((rtp->strict_rtp_address.sin_addr.s_addr != sock_in.sin_addr.s_addr) || (rtp->strict_rtp_address.sin_port != sock_in.sin_port)) {
01620 ast_debug(1, "Received RTP packet from %s:%d, dropping due to strict RTP protection. Expected it to be from %s:%d\n", ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port), ast_inet_ntoa(rtp->strict_rtp_address.sin_addr), ntohs(rtp->strict_rtp_address.sin_port));
01621 return &ast_null_frame;
01622 }
01623 }
01624
01625 rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
01626 if (res < 0) {
01627 ast_assert(errno != EBADF);
01628 if (errno != EAGAIN) {
01629 ast_log(LOG_WARNING, "RTP Read error: %s. Hanging up.\n", strerror(errno));
01630 return NULL;
01631 }
01632 return &ast_null_frame;
01633 }
01634
01635 if (res < hdrlen) {
01636 ast_log(LOG_WARNING, "RTP Read too short\n");
01637 return &ast_null_frame;
01638 }
01639
01640
01641 seqno = ntohl(rtpheader[0]);
01642
01643
01644 version = (seqno & 0xC0000000) >> 30;
01645 if (!version) {
01646
01647
01648
01649
01650
01651 if ((stun_handle_packet(rtp->s, &sock_in, rtp->rawdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == STUN_ACCEPT) &&
01652 (!rtp->them.sin_port && !rtp->them.sin_addr.s_addr)) {
01653 memcpy(&rtp->them, &sock_in, sizeof(rtp->them));
01654 }
01655 return &ast_null_frame;
01656 }
01657
01658 #if 0
01659
01660 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
01661 return &ast_null_frame;
01662 #endif
01663
01664
01665 if (rtp->nat) {
01666 if (((rtp->them.sin_addr.s_addr != sock_in.sin_addr.s_addr) ||
01667 (rtp->them.sin_port != sock_in.sin_port)) &&
01668 ((rtp->altthem.sin_addr.s_addr != sock_in.sin_addr.s_addr) ||
01669 (rtp->altthem.sin_port != sock_in.sin_port))) {
01670 rtp->them = sock_in;
01671 if (rtp->rtcp) {
01672 int h = 0;
01673 memcpy(&rtp->rtcp->them, &sock_in, sizeof(rtp->rtcp->them));
01674 h = ntohs(rtp->them.sin_port);
01675 rtp->rtcp->them.sin_port = htons(h + 1);
01676 }
01677 rtp->rxseqno = 0;
01678 ast_set_flag(rtp, FLAG_NAT_ACTIVE);
01679 if (option_debug || rtpdebug)
01680 ast_debug(0, "RTP NAT: Got audio from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
01681 }
01682 }
01683
01684
01685 if ((bridged = ast_rtp_get_bridged(rtp)) && !bridge_p2p_rtp_write(rtp, bridged, rtpheader, res, hdrlen))
01686 return &ast_null_frame;
01687
01688 if (version != 2)
01689 return &ast_null_frame;
01690
01691 payloadtype = (seqno & 0x7f0000) >> 16;
01692 padding = seqno & (1 << 29);
01693 mark = seqno & (1 << 23);
01694 ext = seqno & (1 << 28);
01695 cc = (seqno & 0xF000000) >> 24;
01696 seqno &= 0xffff;
01697 timestamp = ntohl(rtpheader[1]);
01698 ssrc = ntohl(rtpheader[2]);
01699
01700 if (!mark && rtp->rxssrc && rtp->rxssrc != ssrc) {
01701 if (option_debug || rtpdebug)
01702 ast_debug(0, "Forcing Marker bit, because SSRC has changed\n");
01703 mark = 1;
01704 }
01705
01706 rtp->rxssrc = ssrc;
01707
01708 if (padding) {
01709
01710 res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
01711 }
01712
01713 if (cc) {
01714
01715 hdrlen += cc*4;
01716 }
01717
01718 if (ext) {
01719
01720 hdrlen += (ntohl(rtpheader[hdrlen/4]) & 0xffff) << 2;
01721 hdrlen += 4;
01722 if (option_debug) {
01723 int profile;
01724 profile = (ntohl(rtpheader[3]) & 0xffff0000) >> 16;
01725 if (profile == 0x505a)
01726 ast_debug(1, "Found Zfone extension in RTP stream - zrtp - not supported.\n");
01727 else
01728 ast_debug(1, "Found unknown RTP Extensions %x\n", profile);
01729 }
01730 }
01731
01732 if (res < hdrlen) {
01733 ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
01734 return &ast_null_frame;
01735 }
01736
01737 rtp->rxcount++;
01738
01739 if (rtp->rxcount==1) {
01740
01741 rtp->seedrxseqno = seqno;
01742 }
01743
01744
01745 if (rtp->rtcp && rtp->rtcp->them.sin_addr.s_addr && rtp->rtcp->schedid < 1) {
01746
01747 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
01748 }
01749 if ((int)rtp->lastrxseqno - (int)seqno > 100)
01750 rtp->cycles += RTP_SEQ_MOD;
01751
01752 prev_seqno = rtp->lastrxseqno;
01753
01754 rtp->lastrxseqno = seqno;
01755
01756 if (!rtp->themssrc)
01757 rtp->themssrc = ntohl(rtpheader[2]);
01758
01759 if (rtp_debug_test_addr(&sock_in))
01760 ast_verbose("Got RTP packet from %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
01761 ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
01762
01763 rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
01764 if (!rtpPT.isAstFormat) {
01765 struct ast_frame *f = NULL;
01766
01767
01768 if (rtpPT.code == AST_RTP_DTMF) {
01769
01770 if (rtp_debug_test_addr(&sock_in)) {
01771 unsigned char *data;
01772 unsigned int event;
01773 unsigned int event_end;
01774 unsigned int duration;
01775 data = rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen;
01776 event = ntohl(*((unsigned int *)(data)));
01777 event >>= 24;
01778 event_end = ntohl(*((unsigned int *)(data)));
01779 event_end <<= 8;
01780 event_end >>= 24;
01781 duration = ntohl(*((unsigned int *)(data)));
01782 duration &= 0xFFFF;
01783 ast_verbose("Got RTP RFC2833 from %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u, mark %d, event %08x, end %d, duration %-5.5d) \n", ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port), payloadtype, seqno, timestamp, res - hdrlen, (mark?1:0), event, ((event_end & 0x80)?1:0), duration);
01784 }
01785 f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp);
01786 } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
01787
01788 if (rtp->lastevent <= seqno || (rtp->lastevent >= 65530 && seqno <= 6)) {
01789 f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
01790 rtp->lastevent = seqno;
01791 }
01792 } else if (rtpPT.code == AST_RTP_CN) {
01793
01794 f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
01795 } else {
01796 ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n", payloadtype, ast_inet_ntoa(rtp->them.sin_addr));
01797 }
01798 return f ? f : &ast_null_frame;
01799 }
01800 rtp->lastrxformat = rtp->f.subclass = rtpPT.code;
01801 rtp->f.frametype = (rtp->f.subclass & AST_FORMAT_AUDIO_MASK) ? AST_FRAME_VOICE : (rtp->f.subclass & AST_FORMAT_VIDEO_MASK) ? AST_FRAME_VIDEO : AST_FRAME_TEXT;
01802
01803 rtp->rxseqno = seqno;
01804
01805 if (rtp->dtmf_timeout && rtp->dtmf_timeout < timestamp) {
01806 rtp->dtmf_timeout = 0;
01807
01808 if (rtp->resp) {
01809 struct ast_frame *f;
01810 f = send_dtmf(rtp, AST_FRAME_DTMF_END);
01811 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass)), ast_tv(0, 0));
01812 rtp->resp = 0;
01813 rtp->dtmf_timeout = rtp->dtmf_duration = 0;
01814 return f;
01815 }
01816 }
01817
01818
01819 rtp->lastrxts = timestamp;
01820
01821 rtp->f.mallocd = 0;
01822 rtp->f.datalen = res - hdrlen;
01823 rtp->f.data.ptr = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
01824 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
01825 rtp->f.seqno = seqno;
01826
01827 if (rtp->f.subclass == AST_FORMAT_T140 && (int)seqno - (prev_seqno+1) > 0 && (int)seqno - (prev_seqno+1) < 10) {
01828 unsigned char *data = rtp->f.data.ptr;
01829
01830 memmove(rtp->f.data.ptr+3, rtp->f.data.ptr, rtp->f.datalen);
01831 rtp->f.datalen +=3;
01832 *data++ = 0xEF;
01833 *data++ = 0xBF;
01834 *data = 0xBD;
01835 }
01836
01837 if (rtp->f.subclass == AST_FORMAT_T140RED) {
01838 unsigned char *data = rtp->f.data.ptr;
01839 unsigned char *header_end;
01840 int num_generations;
01841 int header_length;
01842 int length;
01843 int diff =(int)seqno - (prev_seqno+1);
01844 int x;
01845
01846 rtp->f.subclass = AST_FORMAT_T140;
01847 header_end = memchr(data, ((*data) & 0x7f), rtp->f.datalen);
01848 if (header_end == NULL) {
01849 return &ast_null_frame;
01850 }
01851 header_end++;
01852
01853 header_length = header_end - data;
01854 num_generations = header_length / 4;
01855 length = header_length;
01856
01857 if (!diff) {
01858 for (x = 0; x < num_generations; x++)
01859 length += data[x * 4 + 3];
01860
01861 if (!(rtp->f.datalen - length))
01862 return &ast_null_frame;
01863
01864 rtp->f.data.ptr += length;
01865 rtp->f.datalen -= length;
01866 } else if (diff > num_generations && diff < 10) {
01867 length -= 3;
01868 rtp->f.data.ptr += length;
01869 rtp->f.datalen -= length;
01870
01871 data = rtp->f.data.ptr;
01872 *data++ = 0xEF;
01873 *data++ = 0xBF;
01874 *data = 0xBD;
01875 } else {
01876 for ( x = 0; x < num_generations - diff; x++)
01877 length += data[x * 4 + 3];
01878
01879 rtp->f.data.ptr += length;
01880 rtp->f.datalen -= length;
01881 }
01882 }
01883
01884 if (rtp->f.subclass & AST_FORMAT_AUDIO_MASK) {
01885 rtp->f.samples = ast_codec_get_samples(&rtp->f);
01886 if (rtp->f.subclass == AST_FORMAT_SLINEAR)
01887 ast_frame_byteswap_be(&rtp->f);
01888 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
01889
01890 ast_set_flag(&rtp->f, AST_FRFLAG_HAS_TIMING_INFO);
01891 rtp->f.ts = timestamp / (rtp_get_rate(rtp->f.subclass) / 1000);
01892 rtp->f.len = rtp->f.samples / ( (ast_format_rate(rtp->f.subclass) == 16000) ? 16 : 8 );
01893 } else if (rtp->f.subclass & AST_FORMAT_VIDEO_MASK) {
01894
01895 if (!rtp->lastividtimestamp)
01896 rtp->lastividtimestamp = timestamp;
01897 rtp->f.samples = timestamp - rtp->lastividtimestamp;
01898 rtp->lastividtimestamp = timestamp;
01899 rtp->f.delivery.tv_sec = 0;
01900 rtp->f.delivery.tv_usec = 0;
01901
01902
01903
01904
01905
01906 if (mark)
01907 rtp->f.subclass |= 0x1;
01908 } else {
01909
01910 if (!rtp->lastitexttimestamp)
01911 rtp->lastitexttimestamp = timestamp;
01912 rtp->f.samples = timestamp - rtp->lastitexttimestamp;
01913 rtp->lastitexttimestamp = timestamp;
01914 rtp->f.delivery.tv_sec = 0;
01915 rtp->f.delivery.tv_usec = 0;
01916 }
01917 rtp->f.src = "RTP";
01918 return &rtp->f;
01919 }
01920
01921
01922
01923 static struct {
01924 struct rtpPayloadType payloadType;
01925 char* type;
01926 char* subtype;
01927 } mimeTypes[] = {
01928 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
01929 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
01930 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
01931 {{1, AST_FORMAT_ULAW}, "audio", "G711U"},
01932 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
01933 {{1, AST_FORMAT_ALAW}, "audio", "G711A"},
01934 {{1, AST_FORMAT_G726}, "audio", "G726-32"},
01935 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
01936 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
01937 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
01938 {{1, AST_FORMAT_G729A}, "audio", "G729"},
01939 {{1, AST_FORMAT_G729A}, "audio", "G729A"},
01940 {{1, AST_FORMAT_G729A}, "audio", "G.729"},
01941 {{1, AST_FORMAT_SPEEX}, "audio", "speex"},
01942 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
01943 {{1, AST_FORMAT_G722}, "audio", "G722"},
01944 {{1, AST_FORMAT_G726_AAL2}, "audio", "AAL2-G726-32"},
01945 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
01946 {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
01947 {{0, AST_RTP_CN}, "audio", "CN"},
01948 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
01949 {{1, AST_FORMAT_PNG}, "video", "PNG"},
01950 {{1, AST_FORMAT_H261}, "video", "H261"},
01951 {{1, AST_FORMAT_H263}, "video", "H263"},
01952 {{1, AST_FORMAT_H263_PLUS}, "video", "h263-1998"},
01953 {{1, AST_FORMAT_H264}, "video", "H264"},
01954 {{1, AST_FORMAT_MP4_VIDEO}, "video", "MP4V-ES"},
01955 {{1, AST_FORMAT_T140RED}, "text", "RED"},
01956 {{1, AST_FORMAT_T140}, "text", "T140"},
01957 };
01958
01959
01960
01961
01962
01963
01964
01965
01966
01967
01968
01969 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
01970 [0] = {1, AST_FORMAT_ULAW},
01971 #ifdef USE_DEPRECATED_G726
01972 [2] = {1, AST_FORMAT_G726},
01973 #endif
01974 [3] = {1, AST_FORMAT_GSM},
01975 [4] = {1, AST_FORMAT_G723_1},
01976 [5] = {1, AST_FORMAT_ADPCM},
01977 [6] = {1, AST_FORMAT_ADPCM},
01978 [7] = {1, AST_FORMAT_LPC10},
01979 [8] = {1, AST_FORMAT_ALAW},
01980 [9] = {1, AST_FORMAT_G722},
01981 [10] = {1, AST_FORMAT_SLINEAR},
01982 [11] = {1, AST_FORMAT_SLINEAR},
01983 [13] = {0, AST_RTP_CN},
01984 [16] = {1, AST_FORMAT_ADPCM},
01985 [17] = {1, AST_FORMAT_ADPCM},
01986 [18] = {1, AST_FORMAT_G729A},
01987 [19] = {0, AST_RTP_CN},
01988 [26] = {1, AST_FORMAT_JPEG},
01989 [31] = {1, AST_FORMAT_H261},
01990 [34] = {1, AST_FORMAT_H263},
01991 [97] = {1, AST_FORMAT_ILBC},
01992 [98] = {1, AST_FORMAT_H263_PLUS},
01993 [99] = {1, AST_FORMAT_H264},
01994 [101] = {0, AST_RTP_DTMF},
01995 [103] = {1, AST_FORMAT_H263_PLUS},
01996 [104] = {1, AST_FORMAT_MP4_VIDEO},
01997 [105] = {1, AST_FORMAT_T140RED},
01998 [106] = {1, AST_FORMAT_T140},
01999 [110] = {1, AST_FORMAT_SPEEX},
02000 [111] = {1, AST_FORMAT_G726},
02001 [112] = {1, AST_FORMAT_G726_AAL2},
02002 [121] = {0, AST_RTP_CISCO_DTMF},
02003 };
02004
02005 void ast_rtp_pt_clear(struct ast_rtp* rtp)
02006 {
02007 int i;
02008
02009 if (!rtp)
02010 return;
02011
02012 rtp_bridge_lock(rtp);
02013
02014 for (i = 0; i < MAX_RTP_PT; ++i) {
02015 rtp->current_RTP_PT[i].isAstFormat = 0;
02016 rtp->current_RTP_PT[i].code = 0;
02017 }
02018
02019 rtp->rtp_lookup_code_cache_isAstFormat = 0;
02020 rtp->rtp_lookup_code_cache_code = 0;
02021 rtp->rtp_lookup_code_cache_result = 0;
02022
02023 rtp_bridge_unlock(rtp);
02024 }
02025
02026 void ast_rtp_pt_default(struct ast_rtp* rtp)
02027 {
02028 int i;
02029
02030 rtp_bridge_lock(rtp);
02031
02032
02033 for (i = 0; i < MAX_RTP_PT; ++i) {
02034 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
02035 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
02036 }
02037
02038 rtp->rtp_lookup_code_cache_isAstFormat = 0;
02039 rtp->rtp_lookup_code_cache_code = 0;
02040 rtp->rtp_lookup_code_cache_result = 0;
02041
02042 rtp_bridge_unlock(rtp);
02043 }
02044
02045 void ast_rtp_pt_copy(struct ast_rtp *dest, struct ast_rtp *src)
02046 {
02047 unsigned int i;
02048
02049 rtp_bridge_lock(dest);
02050 rtp_bridge_lock(src);
02051
02052 for (i = 0; i < MAX_RTP_PT; ++i) {
02053 dest->current_RTP_PT[i].isAstFormat =
02054 src->current_RTP_PT[i].isAstFormat;
02055 dest->current_RTP_PT[i].code =
02056 src->current_RTP_PT[i].code;
02057 }
02058 dest->rtp_lookup_code_cache_isAstFormat = 0;
02059 dest->rtp_lookup_code_cache_code = 0;
02060 dest->rtp_lookup_code_cache_result = 0;
02061
02062 rtp_bridge_unlock(src);
02063 rtp_bridge_unlock(dest);
02064 }
02065
02066
02067 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
02068 {
02069 struct ast_rtp_protocol *cur = NULL;
02070
02071 AST_RWLIST_RDLOCK(&protos);
02072 AST_RWLIST_TRAVERSE(&protos, cur, list) {
02073 if (cur->type == chan->tech->type)
02074 break;
02075 }
02076 AST_RWLIST_UNLOCK(&protos);
02077
02078 return cur;
02079 }
02080
02081 int ast_rtp_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
02082 {
02083 struct ast_rtp *destp = NULL, *srcp = NULL;
02084 struct ast_rtp *vdestp = NULL, *vsrcp = NULL;
02085 struct ast_rtp *tdestp = NULL, *tsrcp = NULL;
02086 struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
02087 enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED, text_dest_res = AST_RTP_GET_FAILED;
02088 enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED, text_src_res = AST_RTP_GET_FAILED;
02089 int srccodec, destcodec, nat_active = 0;
02090
02091
02092 ast_channel_lock(c0);
02093 if (c1) {
02094 while (ast_channel_trylock(c1)) {
02095 ast_channel_unlock(c0);
02096 usleep(1);
02097 ast_channel_lock(c0);
02098 }
02099 }
02100
02101
02102 destpr = get_proto(c0);
02103 if (c1)
02104 srcpr = get_proto(c1);
02105 if (!destpr) {
02106 ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", c0->name);
02107 ast_channel_unlock(c0);
02108 if (c1)
02109 ast_channel_unlock(c1);
02110 return -1;
02111 }
02112 if (!srcpr) {
02113 ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", c1 ? c1->name : "<unspecified>");
02114 ast_channel_unlock(c0);
02115 if (c1)
02116 ast_channel_unlock(c1);
02117 return -1;
02118 }
02119
02120
02121 audio_dest_res = destpr->get_rtp_info(c0, &destp);
02122 video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(c0, &vdestp) : AST_RTP_GET_FAILED;
02123 text_dest_res = destpr->get_trtp_info ? destpr->get_trtp_info(c0, &tdestp) : AST_RTP_GET_FAILED;
02124 if (srcpr) {
02125 audio_src_res = srcpr->get_rtp_info(c1, &srcp);
02126 video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(c1, &vsrcp) : AST_RTP_GET_FAILED;
02127 text_src_res = srcpr->get_trtp_info ? srcpr->get_trtp_info(c1, &tsrcp) : AST_RTP_GET_FAILED;
02128 }
02129
02130
02131 if (audio_dest_res != AST_RTP_TRY_NATIVE || (video_dest_res != AST_RTP_GET_FAILED && video_dest_res != AST_RTP_TRY_NATIVE)) {
02132
02133 ast_channel_unlock(c0);
02134 if (c1)
02135 ast_channel_unlock(c1);
02136 return -1;
02137 }
02138 if (audio_src_res == AST_RTP_TRY_NATIVE && (video_src_res == AST_RTP_GET_FAILED || video_src_res == AST_RTP_TRY_NATIVE) && srcpr->get_codec)
02139 srccodec = srcpr->get_codec(c1);
02140 else
02141 srccodec = 0;
02142 if (audio_dest_res == AST_RTP_TRY_NATIVE && (video_dest_res == AST_RTP_GET_FAILED || video_dest_res == AST_RTP_TRY_NATIVE) && destpr->get_codec)
02143 destcodec = destpr->get_codec(c0);
02144 else
02145 destcodec = 0;
02146
02147 if (srcp && !(srccodec & destcodec)) {
02148 ast_channel_unlock(c0);
02149 ast_channel_unlock(c1);
02150 return 0;
02151 }
02152
02153 if (audio_src_res == AST_RTP_TRY_NATIVE && !srcp->them.sin_addr.s_addr)
02154 srcp = NULL;
02155 if (srcp && (srcp->nat || ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
02156 nat_active = 1;
02157
02158 if (destpr->set_rtp_peer(c0, srcp, vsrcp, tsrcp, srccodec, nat_active))
02159 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
02160 ast_channel_unlock(c0);
02161 if (c1)
02162 ast_channel_unlock(c1);
02163 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
02164 return 0;
02165 }
02166
02167 int ast_rtp_make_compatible(struct ast_channel *dest, struct ast_channel *src, int media)
02168 {
02169 struct ast_rtp *destp = NULL, *srcp = NULL;
02170 struct ast_rtp *vdestp = NULL, *vsrcp = NULL;
02171 struct ast_rtp *tdestp = NULL, *tsrcp = NULL;
02172 struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
02173 enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED, text_dest_res = AST_RTP_GET_FAILED;
02174 enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED, text_src_res = AST_RTP_GET_FAILED;
02175 int srccodec, destcodec;
02176
02177
02178 ast_channel_lock(dest);
02179 while (ast_channel_trylock(src)) {
02180 ast_channel_unlock(dest);
02181 usleep(1);
02182 ast_channel_lock(dest);
02183 }
02184
02185
02186 if (!(destpr = get_proto(dest))) {
02187 ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", dest->name);
02188 ast_channel_unlock(dest);
02189 ast_channel_unlock(src);
02190 return 0;
02191 }
02192 if (!(srcpr = get_proto(src))) {
02193 ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", src->name);
02194 ast_channel_unlock(dest);
02195 ast_channel_unlock(src);
02196 return 0;
02197 }
02198
02199
02200 audio_dest_res = destpr->get_rtp_info(dest, &destp);
02201 video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
02202 text_dest_res = destpr->get_trtp_info ? destpr->get_trtp_info(dest, &tdestp) : AST_RTP_GET_FAILED;
02203 audio_src_res = srcpr->get_rtp_info(src, &srcp);
02204 video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
02205 text_src_res = srcpr->get_trtp_info ? srcpr->get_trtp_info(src, &tsrcp) : AST_RTP_GET_FAILED;
02206
02207
02208 if (srcpr->get_codec)
02209 srccodec = srcpr->get_codec(src);
02210 else
02211 srccodec = 0;
02212 if (destpr->get_codec)
02213 destcodec = destpr->get_codec(dest);
02214 else
02215 destcodec = 0;
02216
02217
02218 if (audio_dest_res != AST_RTP_TRY_NATIVE || (video_dest_res != AST_RTP_GET_FAILED && video_dest_res != AST_RTP_TRY_NATIVE) || audio_src_res != AST_RTP_TRY_NATIVE || (video_src_res != AST_RTP_GET_FAILED && video_src_res != AST_RTP_TRY_NATIVE) || !(srccodec & destcodec)) {
02219
02220 ast_channel_unlock(dest);
02221 ast_channel_unlock(src);
02222 return 0;
02223 }
02224 ast_rtp_pt_copy(destp, srcp);
02225 if (vdestp && vsrcp)
02226 ast_rtp_pt_copy(vdestp, vsrcp);
02227 if (tdestp && tsrcp)
02228 ast_rtp_pt_copy(tdestp, tsrcp);
02229 if (media) {
02230
02231 if (destpr->set_rtp_peer(dest, srcp, vsrcp, tsrcp, srccodec, ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
02232 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src->name);
02233 }
02234 ast_channel_unlock(dest);
02235 ast_channel_unlock(src);
02236 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n", dest->name, src->name);
02237 return 1;
02238 }
02239
02240
02241
02242
02243
02244 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt)
02245 {
02246 if (pt < 0 || pt >= MAX_RTP_PT || static_RTP_PT[pt].code == 0)
02247 return;
02248
02249 rtp_bridge_lock(rtp);
02250 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
02251 rtp_bridge_unlock(rtp);
02252 }
02253
02254
02255
02256 void ast_rtp_unset_m_type(struct ast_rtp* rtp, int pt)
02257 {
02258 if (pt < 0 || pt >= MAX_RTP_PT)
02259 return;
02260
02261 rtp_bridge_lock(rtp);
02262 rtp->current_RTP_PT[pt].isAstFormat = 0;
02263 rtp->current_RTP_PT[pt].code = 0;
02264 rtp_bridge_unlock(rtp);
02265 }
02266
02267
02268
02269
02270
02271 int ast_rtp_set_rtpmap_type(struct ast_rtp *rtp, int pt,
02272 char *mimeType, char *mimeSubtype,
02273 enum ast_rtp_options options)
02274 {
02275 unsigned int i;
02276 int found = 0;
02277
02278 if (pt < 0 || pt >= MAX_RTP_PT)
02279 return -1;
02280
02281 rtp_bridge_lock(rtp);
02282
02283 for (i = 0; i < ARRAY_LEN(mimeTypes); ++i) {
02284 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
02285 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
02286 found = 1;
02287 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
02288 if ((mimeTypes[i].payloadType.code == AST_FORMAT_G726) &&
02289 mimeTypes[i].payloadType.isAstFormat &&
02290 (options & AST_RTP_OPT_G726_NONSTANDARD))
02291 rtp->current_RTP_PT[pt].code = AST_FORMAT_G726_AAL2;
02292 break;
02293 }
02294 }
02295
02296 rtp_bridge_unlock(rtp);
02297
02298 return (found ? 0 : -1);
02299 }
02300
02301
02302
02303 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
02304 int* astFormats, int* nonAstFormats)
02305 {
02306 int pt;
02307
02308 rtp_bridge_lock(rtp);
02309
02310 *astFormats = *nonAstFormats = 0;
02311 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
02312 if (rtp->current_RTP_PT[pt].isAstFormat) {
02313 *astFormats |= rtp->current_RTP_PT[pt].code;
02314 } else {
02315 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
02316 }
02317 }
02318
02319 rtp_bridge_unlock(rtp);
02320 }
02321
02322 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
02323 {
02324 struct rtpPayloadType result;
02325
02326 result.isAstFormat = result.code = 0;
02327
02328 if (pt < 0 || pt >= MAX_RTP_PT)
02329 return result;
02330
02331
02332 rtp_bridge_lock(rtp);
02333 result = rtp->current_RTP_PT[pt];
02334 rtp_bridge_unlock(rtp);
02335
02336
02337 if (!result.code)
02338 result = static_RTP_PT[pt];
02339
02340 return result;
02341 }
02342
02343
02344 int ast_rtp_lookup_code(struct ast_rtp* rtp, const int isAstFormat, const int code)
02345 {
02346 int pt = 0;
02347
02348 rtp_bridge_lock(rtp);
02349
02350 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
02351 code == rtp->rtp_lookup_code_cache_code) {
02352
02353 pt = rtp->rtp_lookup_code_cache_result;
02354 rtp_bridge_unlock(rtp);
02355 return pt;
02356 }
02357
02358
02359 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
02360 if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
02361 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
02362 rtp->rtp_lookup_code_cache_code = code;
02363 rtp->rtp_lookup_code_cache_result = pt;
02364 rtp_bridge_unlock(rtp);
02365 return pt;
02366 }
02367 }
02368
02369
02370 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
02371 if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
02372 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
02373 rtp->rtp_lookup_code_cache_code = code;
02374 rtp->rtp_lookup_code_cache_result = pt;
02375 rtp_bridge_unlock(rtp);
02376 return pt;
02377 }
02378 }
02379
02380 rtp_bridge_unlock(rtp);
02381
02382 return -1;
02383 }
02384
02385 const char *ast_rtp_lookup_mime_subtype(const int isAstFormat, const int code,
02386 enum ast_rtp_options options)
02387 {
02388 unsigned int i;
02389
02390 for (i = 0; i < ARRAY_LEN(mimeTypes); ++i) {
02391 if ((mimeTypes[i].payloadType.code == code) && (mimeTypes[i].payloadType.isAstFormat == isAstFormat)) {
02392 if (isAstFormat &&
02393 (code == AST_FORMAT_G726_AAL2) &&
02394 (options & AST_RTP_OPT_G726_NONSTANDARD))
02395 return "G726-32";
02396 else
02397 return mimeTypes[i].subtype;
02398 }
02399 }
02400
02401 return "";
02402 }
02403
02404 char *ast_rtp_lookup_mime_multiple(char *buf, size_t size, const int capability,
02405 const int isAstFormat, enum ast_rtp_options options)
02406 {
02407 int format;
02408 unsigned len;
02409 char *end = buf;
02410 char *start = buf;
02411
02412 if (!buf || !size)
02413 return NULL;
02414
02415 snprintf(end, size, "0x%x (", capability);
02416
02417 len = strlen(end);
02418 end += len;
02419 size -= len;
02420 start = end;
02421
02422 for (format = 1; format < AST_RTP_MAX; format <<= 1) {
02423 if (capability & format) {
02424 const char *name = ast_rtp_lookup_mime_subtype(isAstFormat, format, options);
02425
02426 snprintf(end, size, "%s|", name);
02427 len = strlen(end);
02428 end += len;
02429 size -= len;
02430 }
02431 }
02432
02433 if (start == end)
02434 ast_copy_string(start, "nothing)", size);
02435 else if (size > 1)
02436 *(end -1) = ')';
02437
02438 return buf;
02439 }
02440
02441
02442
02443
02444 static int rtp_socket(const char *type)
02445 {
02446 int s = socket(AF_INET, SOCK_DGRAM, 0);
02447 if (s < 0) {
02448 if (type == NULL)
02449 type = "RTP/RTCP";
02450 ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
02451 } else {
02452 long flags = fcntl(s, F_GETFL);
02453 fcntl(s, F_SETFL, flags | O_NONBLOCK);
02454 #ifdef SO_NO_CHECK
02455 if (nochecksums)
02456 setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
02457 #endif
02458 }
02459 return s;
02460 }
02461
02462
02463
02464
02465
02466
02467 static struct ast_rtcp *ast_rtcp_new(void)
02468 {
02469 struct ast_rtcp *rtcp;
02470
02471 if (!(rtcp = ast_calloc(1, sizeof(*rtcp))))
02472 return NULL;
02473 rtcp->s = rtp_socket("RTCP");
02474 rtcp->us.sin_family = AF_INET;
02475 rtcp->them.sin_family = AF_INET;
02476 rtcp->schedid = -1;
02477
02478 if (rtcp->s < 0) {
02479 ast_free(rtcp);
02480 return NULL;
02481 }
02482
02483 return rtcp;
02484 }
02485
02486
02487
02488
02489
02490 void ast_rtp_new_init(struct ast_rtp *rtp)
02491 {
02492 #ifdef P2P_INTENSE
02493 ast_mutex_init(&rtp->bridge_lock);
02494 #endif
02495
02496 rtp->them.sin_family = AF_INET;
02497 rtp->us.sin_family = AF_INET;
02498 rtp->ssrc = ast_random();
02499 rtp->seqno = ast_random() & 0xffff;
02500 ast_set_flag(rtp, FLAG_HAS_DTMF);
02501 rtp->strict_rtp_state = (strictrtp ? STRICT_RTP_LEARN : STRICT_RTP_OPEN);
02502 }
02503
02504 struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
02505 {
02506 struct ast_rtp *rtp;
02507 int x;
02508 int startplace;
02509
02510 if (!(rtp = ast_calloc(1, sizeof(*rtp))))
02511 return NULL;
02512
02513 ast_rtp_new_init(rtp);
02514
02515 rtp->s = rtp_socket("RTP");
02516 if (rtp->s < 0)
02517 goto fail;
02518 if (sched && rtcpenable) {
02519 rtp->sched = sched;
02520 rtp->rtcp = ast_rtcp_new();
02521 }
02522
02523
02524
02525
02526
02527
02528
02529
02530
02531 x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
02532 x = x & ~1;
02533 startplace = x;
02534
02535 rtp->us.sin_addr = addr;
02536 if (rtp->rtcp)
02537 rtp->rtcp->us.sin_addr = addr;
02538 for (;;) {
02539 rtp->us.sin_port = htons(x);
02540 if (!bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) {
02541
02542 if (!rtp->rtcp)
02543 break;
02544
02545 rtp->rtcp->us.sin_port = htons(x + 1);
02546 if (!bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us)))
02547 break;
02548
02549
02550
02551
02552 close(rtp->s);
02553 rtp->s = rtp_socket("RTP");
02554 if (rtp->s < 0)
02555 goto fail;
02556 }
02557
02558
02559
02560
02561 if (errno != EADDRINUSE) {
02562
02563 ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
02564 goto fail;
02565 }
02566
02567
02568
02569
02570
02571 x += 2;
02572 if (x > rtpend)
02573 x = (rtpstart + 1) & ~1;
02574 if (x == startplace) {
02575 ast_log(LOG_ERROR, "No RTP ports remaining. Can't setup media stream for this call.\n");
02576 goto fail;
02577 }
02578 }
02579 rtp->sched = sched;
02580 rtp->io = io;
02581 if (callbackmode) {
02582 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
02583 ast_set_flag(rtp, FLAG_CALLBACK_MODE);
02584 }
02585 ast_rtp_pt_default(rtp);
02586 return rtp;
02587
02588 fail:
02589 if (rtp->s >= 0)
02590 close(rtp->s);
02591 if (rtp->rtcp) {
02592 close(rtp->rtcp->s);
02593 ast_free(rtp->rtcp);
02594 }
02595 ast_free(rtp);
02596 return NULL;
02597 }
02598
02599 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
02600 {
02601 struct in_addr ia;
02602
02603 memset(&ia, 0, sizeof(ia));
02604 return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
02605 }
02606
02607 int ast_rtp_setqos(struct ast_rtp *rtp, int type_of_service, int class_of_service, char *desc)
02608 {
02609 return ast_netsock_set_qos(rtp->s, type_of_service, class_of_service, desc);
02610 }
02611
02612 void ast_rtp_set_constantssrc(struct ast_rtp *rtp)
02613 {
02614 rtp->constantssrc = 1;
02615 }
02616
02617 void ast_rtp_new_source(struct ast_rtp *rtp)
02618 {
02619 if (rtp) {
02620 rtp->set_marker_bit = 1;
02621 if (!rtp->constantssrc) {
02622 rtp->ssrc = ast_random();
02623 }
02624 }
02625 }
02626
02627 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
02628 {
02629 rtp->them.sin_port = them->sin_port;
02630 rtp->them.sin_addr = them->sin_addr;
02631 if (rtp->rtcp) {
02632 int h = ntohs(them->sin_port);
02633 rtp->rtcp->them.sin_port = htons(h + 1);
02634 rtp->rtcp->them.sin_addr = them->sin_addr;
02635 }
02636 rtp->rxseqno = 0;
02637
02638 if (strictrtp)
02639 rtp->strict_rtp_state = STRICT_RTP_LEARN;
02640 }
02641
02642 void ast_rtp_set_alt_peer(struct ast_rtp *rtp, struct sockaddr_in *alt)
02643 {
02644 rtp->altthem.sin_port = alt->sin_port;
02645 rtp->altthem.sin_addr = alt->sin_addr;
02646 if (rtp->rtcp) {
02647 rtp->rtcp->altthem.sin_port = htons(ntohs(alt->sin_port) + 1);
02648 rtp->rtcp->altthem.sin_addr = alt->sin_addr;
02649 }
02650 }
02651
02652 int ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
02653 {
02654 if ((them->sin_family != AF_INET) ||
02655 (them->sin_port != rtp->them.sin_port) ||
02656 (them->sin_addr.s_addr != rtp->them.sin_addr.s_addr)) {
02657 them->sin_family = AF_INET;
02658 them->sin_port = rtp->them.sin_port;
02659 them->sin_addr = rtp->them.sin_addr;
02660 return 1;
02661 }
02662 return 0;
02663 }
02664
02665 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
02666 {
02667 *us = rtp->us;
02668 }
02669
02670 struct ast_rtp *ast_rtp_get_bridged(struct ast_rtp *rtp)
02671 {
02672 struct ast_rtp *bridged = NULL;
02673
02674 rtp_bridge_lock(rtp);
02675 bridged = rtp->bridged;
02676 rtp_bridge_unlock(rtp);
02677
02678 return bridged;
02679 }
02680
02681 void ast_rtp_stop(struct ast_rtp *rtp)
02682 {
02683 if (rtp->rtcp) {
02684 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
02685 }
02686 if (rtp->red) {
02687 AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
02688 free(rtp->red);
02689 rtp->red = NULL;
02690 }
02691
02692 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
02693 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
02694 if (rtp->rtcp) {
02695 memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->rtcp->them.sin_addr));
02696 memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->rtcp->them.sin_port));
02697 }
02698
02699 ast_clear_flag(rtp, FLAG_P2P_SENT_MARK);
02700 }
02701
02702 void ast_rtp_reset(struct ast_rtp *rtp)
02703 {
02704 memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
02705 memset(&rtp->txcore, 0, sizeof(rtp->txcore));
02706 memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
02707 rtp->lastts = 0;
02708 rtp->lastdigitts = 0;
02709 rtp->lastrxts = 0;
02710 rtp->lastividtimestamp = 0;
02711 rtp->lastovidtimestamp = 0;
02712 rtp->lastitexttimestamp = 0;
02713 rtp->lastotexttimestamp = 0;
02714 rtp->lasteventseqn = 0;
02715 rtp->lastevent = 0;
02716 rtp->lasttxformat = 0;
02717 rtp->lastrxformat = 0;
02718 rtp->dtmf_timeout = 0;
02719 rtp->dtmfsamples = 0;
02720 rtp->seqno = 0;
02721 rtp->rxseqno = 0;
02722 }
02723
02724
02725 unsigned int ast_rtp_get_qosvalue(struct ast_rtp *rtp, enum ast_rtp_qos_vars value)
02726 {
02727 if (rtp == NULL) {
02728 if (option_debug > 1)
02729 ast_log(LOG_DEBUG, "NO RTP Structure? Kidding me? \n");
02730 return 0;
02731 }
02732 if (option_debug > 1 && rtp->rtcp == NULL) {
02733 ast_log(LOG_DEBUG, "NO RTCP structure. Maybe in RTP p2p bridging mode? \n");
02734 }
02735
02736 switch (value) {
02737 case AST_RTP_TXCOUNT:
02738 return (unsigned int) rtp->txcount;
02739 case AST_RTP_RXCOUNT:
02740 return (unsigned int) rtp->rxcount;
02741 case AST_RTP_TXJITTER:
02742 return (unsigned int) (rtp->rxjitter * 100.0);
02743 case AST_RTP_RXJITTER:
02744 return (unsigned int) (rtp->rtcp ? (rtp->rtcp->reported_jitter / (unsigned int) 65536.0) : 0);
02745 case AST_RTP_RXPLOSS:
02746 return rtp->rtcp ? (rtp->rtcp->expected_prior - rtp->rtcp->received_prior) : 0;
02747 case AST_RTP_TXPLOSS:
02748 return rtp->rtcp ? rtp->rtcp->reported_lost : 0;
02749 case AST_RTP_RTT:
02750 return (unsigned int) (rtp->rtcp ? (rtp->rtcp->rtt * 100) : 0);
02751 }
02752 return 0;
02753 }
02754
02755 static double __ast_rtp_get_qos(struct ast_rtp *rtp, const char *qos, int *found)
02756 {
02757 *found = 1;
02758
02759 if (!strcasecmp(qos, "remote_maxjitter"))
02760 return rtp->rtcp->reported_maxjitter * 1000.0;
02761 if (!strcasecmp(qos, "remote_minjitter"))
02762 return rtp->rtcp->reported_minjitter * 1000.0;
02763 if (!strcasecmp(qos, "remote_normdevjitter"))
02764 return rtp->rtcp->reported_normdev_jitter * 1000.0;
02765 if (!strcasecmp(qos, "remote_stdevjitter"))
02766 return sqrt(rtp->rtcp->reported_stdev_jitter) * 1000.0;
02767
02768 if (!strcasecmp(qos, "local_maxjitter"))
02769 return rtp->rtcp->maxrxjitter * 1000.0;
02770 if (!strcasecmp(qos, "local_minjitter"))
02771 return rtp->rtcp->minrxjitter * 1000.0;
02772 if (!strcasecmp(qos, "local_normdevjitter"))
02773 return rtp->rtcp->normdev_rxjitter * 1000.0;
02774 if (!strcasecmp(qos, "local_stdevjitter"))
02775 return sqrt(rtp->rtcp->stdev_rxjitter) * 1000.0;
02776
02777 if (!strcasecmp(qos, "maxrtt"))
02778 return rtp->rtcp->maxrtt * 1000.0;
02779 if (!strcasecmp(qos, "minrtt"))
02780 return rtp->rtcp->minrtt * 1000.0;
02781 if (!strcasecmp(qos, "normdevrtt"))
02782 return rtp->rtcp->normdevrtt * 1000.0;
02783 if (!strcasecmp(qos, "stdevrtt"))
02784 return sqrt(rtp->rtcp->stdevrtt) * 1000.0;
02785
02786 *found = 0;
02787
02788 return 0.0;
02789 }
02790
02791 int ast_rtp_get_qos(struct ast_rtp *rtp, const char *qos, char *buf, unsigned int buflen)
02792 {
02793 double value;
02794 int found;
02795
02796 value = __ast_rtp_get_qos(rtp, qos, &found);
02797
02798 if (!found)
02799 return -1;
02800
02801 snprintf(buf, buflen, "%.0lf", value);
02802
02803 return 0;
02804 }
02805
02806 void ast_rtp_set_vars(struct ast_channel *chan, struct ast_rtp *rtp) {
02807 char *audioqos;
02808 char *audioqos_jitter;
02809 char *audioqos_loss;
02810 char *audioqos_rtt;
02811 struct ast_channel *bridge;
02812
02813 if (!rtp || !chan)
02814 return;
02815
02816 bridge = ast_bridged_channel(chan);
02817
02818 audioqos = ast_rtp_get_quality(rtp, NULL, RTPQOS_SUMMARY);
02819 audioqos_jitter = ast_rtp_get_quality(rtp, NULL, RTPQOS_JITTER);
02820 audioqos_loss = ast_rtp_get_quality(rtp, NULL, RTPQOS_LOSS);
02821 audioqos_rtt = ast_rtp_get_quality(rtp, NULL, RTPQOS_RTT);
02822
02823 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", audioqos);
02824 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", audioqos_jitter);
02825 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", audioqos_loss);
02826 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", audioqos_rtt);
02827
02828 if (!bridge)
02829 return;
02830
02831 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", audioqos);
02832 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", audioqos_jitter);
02833 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", audioqos_loss);
02834 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", audioqos_rtt);
02835 }
02836
02837 static char *__ast_rtp_get_quality_jitter(struct ast_rtp *rtp)
02838 {
02839
02840
02841
02842
02843
02844
02845
02846
02847
02848
02849
02850 #define RTCP_JITTER_FORMAT1 \
02851 "minrxjitter=%f;" \
02852 "maxrxjitter=%f;" \
02853 "avgrxjitter=%f;" \
02854 "stdevrxjitter=%f;" \
02855 "reported_minjitter=%f;" \
02856 "reported_maxjitter=%f;" \
02857 "reported_avgjitter=%f;" \
02858 "reported_stdevjitter=%f;"
02859
02860 #define RTCP_JITTER_FORMAT2 \
02861 "rxjitter=%f;"
02862
02863 if (rtp->rtcp && rtp->rtcp->rtcp_info) {
02864 snprintf(rtp->rtcp->quality_jitter, sizeof(rtp->rtcp->quality_jitter), RTCP_JITTER_FORMAT1,
02865 rtp->rtcp->minrxjitter,
02866 rtp->rtcp->maxrxjitter,
02867 rtp->rtcp->normdev_rxjitter,
02868 sqrt(rtp->rtcp->stdev_rxjitter),
02869 rtp->rtcp->reported_minjitter,
02870 rtp->rtcp->reported_maxjitter,
02871 rtp->rtcp->reported_normdev_jitter,
02872 sqrt(rtp->rtcp->reported_stdev_jitter)
02873 );
02874 } else {
02875 snprintf(rtp->rtcp->quality_jitter, sizeof(rtp->rtcp->quality_jitter), RTCP_JITTER_FORMAT2,
02876 rtp->rxjitter
02877 );
02878 }
02879
02880 return rtp->rtcp->quality_jitter;
02881
02882 #undef RTCP_JITTER_FORMAT1
02883 #undef RTCP_JITTER_FORMAT2
02884 }
02885
02886 static char *__ast_rtp_get_quality_loss(struct ast_rtp *rtp)
02887 {
02888 unsigned int lost;
02889 unsigned int extended;
02890 unsigned int expected;
02891 int fraction;
02892
02893 #define RTCP_LOSS_FORMAT1 \
02894 "minrxlost=%f;" \
02895 "maxrxlost=%f;" \
02896 "avgrxlostr=%f;" \
02897 "stdevrxlost=%f;" \
02898 "reported_minlost=%f;" \
02899 "reported_maxlost=%f;" \
02900 "reported_avglost=%f;" \
02901 "reported_stdevlost=%f;"
02902
02903 #define RTCP_LOSS_FORMAT2 \
02904 "lost=%d;" \
02905 "expected=%d;"
02906
02907 if (rtp->rtcp && rtp->rtcp->rtcp_info && rtp->rtcp->maxrxlost > 0) {
02908 snprintf(rtp->rtcp->quality_loss, sizeof(rtp->rtcp->quality_loss), RTCP_LOSS_FORMAT1,
02909 rtp->rtcp->minrxlost,
02910 rtp->rtcp->maxrxlost,
02911 rtp->rtcp->normdev_rxlost,
02912 sqrt(rtp->rtcp->stdev_rxlost),
02913 rtp->rtcp->reported_minlost,
02914 rtp->rtcp->reported_maxlost,
02915 rtp->rtcp->reported_normdev_lost,
02916 sqrt(rtp->rtcp->reported_stdev_lost)
02917 );
02918 } else {
02919 extended = rtp->cycles + rtp->lastrxseqno;
02920 expected = extended - rtp->seedrxseqno + 1;
02921 if (rtp->rxcount > expected)
02922 expected += rtp->rxcount - expected;
02923 lost = expected - rtp->rxcount;
02924
02925 if (!expected || lost <= 0)
02926 fraction = 0;
02927 else
02928 fraction = (lost << 8) / expected;
02929
02930 snprintf(rtp->rtcp->quality_loss, sizeof(rtp->rtcp->quality_loss), RTCP_LOSS_FORMAT2,
02931 lost,
02932 expected
02933 );
02934 }
02935
02936 return rtp->rtcp->quality_loss;
02937
02938 #undef RTCP_LOSS_FORMAT1
02939 #undef RTCP_LOSS_FORMAT2
02940 }
02941
02942 static char *__ast_rtp_get_quality_rtt(struct ast_rtp *rtp)
02943 {
02944 if (rtp->rtcp && rtp->rtcp->rtcp_info) {
02945 snprintf(rtp->rtcp->quality_rtt, sizeof(rtp->rtcp->quality_rtt), "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;",
02946 rtp->rtcp->minrtt,
02947 rtp->rtcp->maxrtt,
02948 rtp->rtcp->normdevrtt,
02949 sqrt(rtp->rtcp->stdevrtt)
02950 );
02951 } else {
02952 snprintf(rtp->rtcp->quality_rtt, sizeof(rtp->rtcp->quality_rtt), "Not available");
02953 }
02954
02955 return rtp->rtcp->quality_rtt;
02956 }
02957
02958 static char *__ast_rtp_get_quality(struct ast_rtp *rtp)
02959 {
02960
02961
02962
02963
02964
02965
02966
02967
02968
02969
02970
02971
02972 if (rtp->rtcp && rtp->rtcp->rtcp_info) {
02973 snprintf(rtp->rtcp->quality, sizeof(rtp->rtcp->quality),
02974 "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
02975 rtp->ssrc,
02976 rtp->themssrc,
02977 rtp->rtcp->expected_prior - rtp->rtcp->received_prior,
02978 rtp->rxjitter,
02979 rtp->rxcount,
02980 (double)rtp->rtcp->reported_jitter / 65536.0,
02981 rtp->txcount,
02982 rtp->rtcp->reported_lost,
02983 rtp->rtcp->rtt
02984 );
02985 } else {
02986 snprintf(rtp->rtcp->quality, sizeof(rtp->rtcp->quality), "ssrc=%u;themssrc=%u;rxjitter=%f;rxcount=%u;txcount=%u;",
02987 rtp->ssrc,
02988 rtp->themssrc,
02989 rtp->rxjitter,
02990 rtp->rxcount,
02991 rtp->txcount
02992 );
02993 }
02994
02995 return rtp->rtcp->quality;
02996 }
02997
02998 char *ast_rtp_get_quality(struct ast_rtp *rtp, struct ast_rtp_quality *qual, enum ast_rtp_quality_type qtype)
02999 {
03000 if (qual && rtp) {
03001 qual->local_ssrc = rtp->ssrc;
03002 qual->local_jitter = rtp->rxjitter;
03003 qual->local_count = rtp->rxcount;
03004 qual->remote_ssrc = rtp->themssrc;
03005 qual->remote_count = rtp->txcount;
03006
03007 if (rtp->rtcp) {
03008 qual->local_lostpackets = rtp->rtcp->expected_prior - rtp->rtcp->received_prior;
03009 qual->remote_lostpackets = rtp->rtcp->reported_lost;
03010 qual->remote_jitter = rtp->rtcp->reported_jitter / 65536.0;
03011 qual->rtt = rtp->rtcp->rtt;
03012 }
03013 }
03014
03015 switch (qtype) {
03016 case RTPQOS_SUMMARY:
03017 return __ast_rtp_get_quality(rtp);
03018 case RTPQOS_JITTER:
03019 return __ast_rtp_get_quality_jitter(rtp);
03020 case RTPQOS_LOSS:
03021 return __ast_rtp_get_quality_loss(rtp);
03022 case RTPQOS_RTT:
03023 return __ast_rtp_get_quality_rtt(rtp);
03024 }
03025
03026 return NULL;
03027 }
03028
03029 void ast_rtp_destroy(struct ast_rtp *rtp)
03030 {
03031 if (rtcp_debug_test_addr(&rtp->them) || rtcpstats) {
03032
03033 ast_verbose(" RTP-stats\n");
03034 ast_verbose("* Our Receiver:\n");
03035 ast_verbose(" SSRC: %u\n", rtp->themssrc);
03036 ast_verbose(" Received packets: %u\n", rtp->rxcount);
03037 ast_verbose(" Lost packets: %u\n", rtp->rtcp ? (rtp->rtcp->expected_prior - rtp->rtcp->received_prior) : 0);
03038 ast_verbose(" Jitter: %.4f\n", rtp->rxjitter);
03039 ast_verbose(" Transit: %.4f\n", rtp->rxtransit);
03040 ast_verbose(" RR-count: %u\n", rtp->rtcp ? rtp->rtcp->rr_count : 0);
03041 ast_verbose("* Our Sender:\n");
03042 ast_verbose(" SSRC: %u\n", rtp->ssrc);
03043 ast_verbose(" Sent packets: %u\n", rtp->txcount);
03044 ast_verbose(" Lost packets: %u\n", rtp->rtcp ? rtp->rtcp->reported_lost : 0);
03045 ast_verbose(" Jitter: %u\n", rtp->rtcp ? (rtp->rtcp->reported_jitter / (unsigned int)65536.0) : 0);
03046 ast_verbose(" SR-count: %u\n", rtp->rtcp ? rtp->rtcp->sr_count : 0);
03047 ast_verbose(" RTT: %f\n", rtp->rtcp ? rtp->rtcp->rtt : 0);
03048 }
03049
03050 manager_event(EVENT_FLAG_REPORTING, "RTPReceiverStat", "SSRC: %u\r\n"
03051 "ReceivedPackets: %u\r\n"
03052 "LostPackets: %u\r\n"
03053 "Jitter: %.4f\r\n"
03054 "Transit: %.4f\r\n"
03055 "RRCount: %u\r\n",
03056 rtp->themssrc,
03057 rtp->rxcount,
03058 rtp->rtcp ? (rtp->rtcp->expected_prior - rtp->rtcp->received_prior) : 0,
03059 rtp->rxjitter,
03060 rtp->rxtransit,
03061 rtp->rtcp ? rtp->rtcp->rr_count : 0);
03062 manager_event(EVENT_FLAG_REPORTING, "RTPSenderStat", "SSRC: %u\r\n"
03063 "SentPackets: %u\r\n"
03064 "LostPackets: %u\r\n"
03065 "Jitter: %u\r\n"
03066 "SRCount: %u\r\n"
03067 "RTT: %f\r\n",
03068 rtp->ssrc,
03069 rtp->txcount,
03070 rtp->rtcp ? rtp->rtcp->reported_lost : 0,
03071 rtp->rtcp ? rtp->rtcp->reported_jitter : 0,
03072 rtp->rtcp ? rtp->rtcp->sr_count : 0,
03073 rtp->rtcp ? rtp->rtcp->rtt : 0);
03074 if (rtp->smoother)
03075 ast_smoother_free(rtp->smoother);
03076 if (rtp->ioid)
03077 ast_io_remove(rtp->io, rtp->ioid);
03078 if (rtp->s > -1)
03079 close(rtp->s);
03080 if (rtp->rtcp) {
03081 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
03082 close(rtp->rtcp->s);
03083 ast_free(rtp->rtcp);
03084 rtp->rtcp=NULL;
03085 }
03086 #ifdef P2P_INTENSE
03087 ast_mutex_destroy(&rtp->bridge_lock);
03088 #endif
03089 ast_free(rtp);
03090 }
03091
03092 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
03093 {
03094 struct timeval t;
03095 long ms;
03096 if (ast_tvzero(rtp->txcore)) {
03097 rtp->txcore = ast_tvnow();
03098
03099 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
03100 }
03101
03102 t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
03103 ms = ast_tvdiff_ms(t, rtp->txcore);
03104 if (ms < 0)
03105 ms = 0;
03106
03107 rtp->txcore = t;
03108 return (unsigned int) ms;
03109 }
03110
03111
03112 int ast_rtp_senddigit_begin(struct ast_rtp *rtp, char digit)
03113 {
03114 unsigned int *rtpheader;
03115 int hdrlen = 12, res = 0, i = 0, payload = 0;
03116 char data[256];
03117
03118 if ((digit <= '9') && (digit >= '0'))
03119 digit -= '0';
03120 else if (digit == '*')
03121 digit = 10;
03122 else if (digit == '#')
03123 digit = 11;
03124 else if ((digit >= 'A') && (digit <= 'D'))
03125 digit = digit - 'A' + 12;
03126 else if ((digit >= 'a') && (digit <= 'd'))
03127 digit = digit - 'a' + 12;
03128 else {
03129 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
03130 return 0;
03131 }
03132
03133
03134 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
03135 return 0;
03136
03137 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
03138
03139 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
03140 rtp->send_duration = 160;
03141 rtp->lastdigitts = rtp->lastts + rtp->send_duration;
03142
03143
03144 rtpheader = (unsigned int *)data;
03145 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
03146 rtpheader[1] = htonl(rtp->lastdigitts);
03147 rtpheader[2] = htonl(rtp->ssrc);
03148
03149 for (i = 0; i < 2; i++) {
03150 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
03151 res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
03152 if (res < 0)
03153 ast_log(LOG_ERROR, "RTP Transmission error to %s:%u: %s\n",
03154 ast_inet_ntoa(rtp->them.sin_addr),
03155 ntohs(rtp->them.sin_port), strerror(errno));
03156 if (rtp_debug_test_addr(&rtp->them))
03157 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
03158 ast_inet_ntoa(rtp->them.sin_addr),
03159 ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
03160
03161 rtp->seqno++;
03162
03163 rtp->send_duration += 160;
03164
03165 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
03166 }
03167
03168
03169 rtp->sending_digit = 1;
03170 rtp->send_digit = digit;
03171 rtp->send_payload = payload;
03172
03173 return 0;
03174 }
03175
03176
03177 static int ast_rtp_senddigit_continuation(struct ast_rtp *rtp)
03178 {
03179 unsigned int *rtpheader;
03180 int hdrlen = 12, res = 0;
03181 char data[256];
03182
03183 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
03184 return 0;
03185
03186
03187 rtpheader = (unsigned int *)data;
03188 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (rtp->send_payload << 16) | (rtp->seqno));
03189 rtpheader[1] = htonl(rtp->lastdigitts);
03190 rtpheader[2] = htonl(rtp->ssrc);
03191 rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
03192 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
03193
03194
03195 res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
03196 if (res < 0)
03197 ast_log(LOG_ERROR, "RTP Transmission error to %s:%d: %s\n",
03198 ast_inet_ntoa(rtp->them.sin_addr),
03199 ntohs(rtp->them.sin_port), strerror(errno));
03200 if (rtp_debug_test_addr(&rtp->them))
03201 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
03202 ast_inet_ntoa(rtp->them.sin_addr),
03203 ntohs(rtp->them.sin_port), rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
03204
03205
03206 rtp->seqno++;
03207
03208 rtp->send_duration += 160;
03209
03210 return 0;
03211 }
03212
03213
03214 int ast_rtp_senddigit_end(struct ast_rtp *rtp, char digit)
03215 {
03216 unsigned int *rtpheader;
03217 int hdrlen = 12, res = 0, i = 0;
03218 char data[256];
03219
03220
03221 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
03222 return 0;
03223
03224 if ((digit <= '9') && (digit >= '0'))
03225 digit -= '0';
03226 else if (digit == '*')
03227 digit = 10;
03228 else if (digit == '#')
03229 digit = 11;
03230 else if ((digit >= 'A') && (digit <= 'D'))
03231 digit = digit - 'A' + 12;
03232 else if ((digit >= 'a') && (digit <= 'd'))
03233 digit = digit - 'a' + 12;
03234 else {
03235 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
03236 return 0;
03237 }
03238
03239 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
03240
03241 rtpheader = (unsigned int *)data;
03242 rtpheader[1] = htonl(rtp->lastdigitts);
03243 rtpheader[2] = htonl(rtp->ssrc);
03244 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
03245
03246 rtpheader[3] |= htonl((1 << 23));
03247
03248
03249 for (i = 0; i < 3; i++) {
03250 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
03251 res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
03252 rtp->seqno++;
03253 if (res < 0)
03254 ast_log(LOG_ERROR, "RTP Transmission error to %s:%d: %s\n",
03255 ast_inet_ntoa(rtp->them.sin_addr),
03256 ntohs(rtp->them.sin_port), strerror(errno));
03257 if (rtp_debug_test_addr(&rtp->them))
03258 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
03259 ast_inet_ntoa(rtp->them.sin_addr),
03260 ntohs(rtp->them.sin_port), rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
03261 }
03262 rtp->lastts += rtp->send_duration;
03263 rtp->sending_digit = 0;
03264 rtp->send_digit = 0;
03265
03266 return res;
03267 }
03268
03269
03270 int ast_rtcp_send_h261fur(void *data)
03271 {
03272 struct ast_rtp *rtp = data;
03273 int res;
03274
03275 rtp->rtcp->sendfur = 1;
03276 res = ast_rtcp_write(data);
03277
03278 return res;
03279 }
03280
03281
03282 static int ast_rtcp_write_sr(const void *data)
03283 {
03284 struct ast_rtp *rtp = (struct ast_rtp *)data;
03285 int res;
03286 int len = 0;
03287 struct timeval now;
03288 unsigned int now_lsw;
03289 unsigned int now_msw;
03290 unsigned int *rtcpheader;
03291 unsigned int lost;
03292 unsigned int extended;
03293 unsigned int expected;
03294 unsigned int expected_interval;
03295 unsigned int received_interval;
03296 int lost_interval;
03297 int fraction;
03298 struct timeval dlsr;
03299 char bdata[512];
03300
03301
03302 if (!rtp || !rtp->rtcp)
03303 return 0;
03304
03305 if (!rtp->rtcp->them.sin_addr.s_addr) {
03306 ast_verbose("RTCP SR transmission error, rtcp halted\n");
03307 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
03308 return 0;
03309 }
03310
03311 gettimeofday(&now, NULL);
03312 timeval2ntp(now, &now_msw, &now_lsw);
03313 rtcpheader = (unsigned int *)bdata;
03314 rtcpheader[1] = htonl(rtp->ssrc);
03315 rtcpheader[2] = htonl(now_msw);
03316 rtcpheader[3] = htonl(now_lsw);
03317 rtcpheader[4] = htonl(rtp->lastts);
03318 rtcpheader[5] = htonl(rtp->txcount);
03319 rtcpheader[6] = htonl(rtp->txoctetcount);
03320 len += 28;
03321
03322 extended = rtp->cycles + rtp->lastrxseqno;
03323 expected = extended - rtp->seedrxseqno + 1;
03324 if (rtp->rxcount > expected)
03325 expected += rtp->rxcount - expected;
03326 lost = expected - rtp->rxcount;
03327 expected_interval = expected - rtp->rtcp->expected_prior;
03328 rtp->rtcp->expected_prior = expected;
03329 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
03330 rtp->rtcp->received_prior = rtp->rxcount;
03331 lost_interval = expected_interval - received_interval;
03332 if (expected_interval == 0 || lost_interval <= 0)
03333 fraction = 0;
03334 else
03335 fraction = (lost_interval << 8) / expected_interval;
03336 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
03337 rtcpheader[7] = htonl(rtp->themssrc);
03338 rtcpheader[8] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
03339 rtcpheader[9] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
03340 rtcpheader[10] = htonl((unsigned int)(rtp->rxjitter * 65536.));
03341 rtcpheader[11] = htonl(rtp->rtcp->themrxlsr);
03342 rtcpheader[12] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
03343 len += 24;
03344
03345 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SR << 16) | ((len/4)-1));
03346
03347 if (rtp->rtcp->sendfur) {
03348 rtcpheader[13] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR << 16) | 1);
03349 rtcpheader[14] = htonl(rtp->ssrc);
03350 len += 8;
03351 rtp->rtcp->sendfur = 0;
03352 }
03353
03354
03355
03356 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
03357 rtcpheader[(len/4)+1] = htonl(rtp->ssrc);
03358 rtcpheader[(len/4)+2] = htonl(0x01 << 24);
03359 len += 12;
03360
03361 res = sendto(rtp->rtcp->s, (unsigned int *)rtcpheader, len, 0, (struct sockaddr *)&rtp->rtcp->them, sizeof(rtp->rtcp->them));
03362 if (res < 0) {
03363 ast_log(LOG_ERROR, "RTCP SR transmission error to %s:%d, rtcp halted %s\n",ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port), strerror(errno));
03364 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
03365 return 0;
03366 }
03367
03368
03369 gettimeofday(&rtp->rtcp->txlsr, NULL);
03370 rtp->rtcp->sr_count++;
03371
03372 rtp->rtcp->lastsrtxcount = rtp->txcount;
03373
03374 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
03375 ast_verbose("* Sent RTCP SR to %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
03376 ast_verbose(" Our SSRC: %u\n", rtp->ssrc);
03377 ast_verbose(" Sent(NTP): %u.%010u\n", (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096);
03378 ast_verbose(" Sent(RTP): %u\n", rtp->lastts);
03379 ast_verbose(" Sent packets: %u\n", rtp->txcount);
03380 ast_verbose(" Sent octets: %u\n", rtp->txoctetcount);
03381 ast_verbose(" Report block:\n");
03382 ast_verbose(" Fraction lost: %u\n", fraction);
03383 ast_verbose(" Cumulative loss: %u\n", lost);
03384 ast_verbose(" IA jitter: %.4f\n", rtp->rxjitter);
03385 ast_verbose(" Their last SR: %u\n", rtp->rtcp->themrxlsr);
03386 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(ntohl(rtcpheader[12])/65536.0));
03387 }
03388 manager_event(EVENT_FLAG_REPORTING, "RTCPSent", "To: %s:%d\r\n"
03389 "OurSSRC: %u\r\n"
03390 "SentNTP: %u.%010u\r\n"
03391 "SentRTP: %u\r\n"
03392 "SentPackets: %u\r\n"
03393 "SentOctets: %u\r\n"
03394 "ReportBlock:\r\n"
03395 "FractionLost: %u\r\n"
03396 "CumulativeLoss: %u\r\n"
03397 "IAJitter: %.4f\r\n"
03398 "TheirLastSR: %u\r\n"
03399 "DLSR: %4.4f (sec)\r\n",
03400 ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port),
03401 rtp->ssrc,
03402 (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096,
03403 rtp->lastts,
03404 rtp->txcount,
03405 rtp->txoctetcount,
03406 fraction,
03407 lost,
03408 rtp->rxjitter,
03409 rtp->rtcp->themrxlsr,
03410 (double)(ntohl(rtcpheader[12])/65536.0));
03411 return res;
03412 }
03413
03414
03415 static int ast_rtcp_write_rr(const void *data)
03416 {
03417 struct ast_rtp *rtp = (struct ast_rtp *)data;
03418 int res;
03419 int len = 32;
03420 unsigned int lost;
03421 unsigned int extended;
03422 unsigned int expected;
03423 unsigned int expected_interval;
03424 unsigned int received_interval;
03425 int lost_interval;
03426 struct timeval now;
03427 unsigned int *rtcpheader;
03428 char bdata[1024];
03429 struct timeval dlsr;
03430 int fraction;
03431
03432 double rxlost_current;
03433
03434 if (!rtp || !rtp->rtcp || (&rtp->rtcp->them.sin_addr == 0))
03435 return 0;
03436
03437 if (!rtp->rtcp->them.sin_addr.s_addr) {
03438 ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted\n");
03439 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
03440 return 0;
03441 }
03442
03443 extended = rtp->cycles + rtp->lastrxseqno;
03444 expected = extended - rtp->seedrxseqno + 1;
03445 lost = expected - rtp->rxcount;
03446 expected_interval = expected - rtp->rtcp->expected_prior;
03447 rtp->rtcp->expected_prior = expected;
03448 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
03449 rtp->rtcp->received_prior = rtp->rxcount;
03450 lost_interval = expected_interval - received_interval;
03451
03452 if (lost_interval <= 0)
03453 rtp->rtcp->rxlost = 0;
03454 else rtp->rtcp->rxlost = rtp->rtcp->rxlost;
03455 if (rtp->rtcp->rxlost_count == 0)
03456 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
03457 if (lost_interval < rtp->rtcp->minrxlost)
03458 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
03459 if (lost_interval > rtp->rtcp->maxrxlost)
03460 rtp->rtcp->maxrxlost = rtp->rtcp->rxlost;
03461
03462 rxlost_current = normdev_compute(rtp->rtcp->normdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->rxlost_count);
03463 rtp->rtcp->stdev_rxlost = stddev_compute(rtp->rtcp->stdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->normdev_rxlost, rxlost_current, rtp->rtcp->rxlost_count);
03464 rtp->rtcp->normdev_rxlost = rxlost_current;
03465 rtp->rtcp->rxlost_count++;
03466
03467 if (expected_interval == 0 || lost_interval <= 0)
03468 fraction = 0;
03469 else
03470 fraction = (lost_interval << 8) / expected_interval;
03471 gettimeofday(&now, NULL);
03472 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
03473 rtcpheader = (unsigned int *)bdata;
03474 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_RR << 16) | ((len/4)-1));
03475 rtcpheader[1] = htonl(rtp->ssrc);
03476 rtcpheader[2] = htonl(rtp->themssrc);
03477 rtcpheader[3] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
03478 rtcpheader[4] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
03479 rtcpheader[5] = htonl((unsigned int)(rtp->rxjitter * 65536.));
03480 rtcpheader[6] = htonl(rtp->rtcp->themrxlsr);
03481 rtcpheader[7] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
03482
03483 if (rtp->rtcp->sendfur) {
03484 rtcpheader[8] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR << 16) | 1);
03485 rtcpheader[9] = htonl(rtp->ssrc);
03486 len += 8;
03487 rtp->rtcp->sendfur = 0;
03488 }
03489
03490
03491
03492 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
03493 rtcpheader[(len/4)+1] = htonl(rtp->ssrc);
03494 rtcpheader[(len/4)+2] = htonl(0x01 << 24);
03495 len += 12;
03496
03497 res = sendto(rtp->rtcp->s, (unsigned int *)rtcpheader, len, 0, (struct sockaddr *)&rtp->rtcp->them, sizeof(rtp->rtcp->them));
03498
03499 if (res < 0) {
03500 ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno));
03501
03502 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
03503 return 0;
03504 }
03505
03506 rtp->rtcp->rr_count++;
03507
03508 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
03509 ast_verbose("\n* Sending RTCP RR to %s:%d\n"
03510 " Our SSRC: %u\nTheir SSRC: %u\niFraction lost: %d\nCumulative loss: %u\n"
03511 " IA jitter: %.4f\n"
03512 " Their last SR: %u\n"
03513 " DLSR: %4.4f (sec)\n\n",
03514 ast_inet_ntoa(rtp->rtcp->them.sin_addr),
03515 ntohs(rtp->rtcp->them.sin_port),
03516 rtp->ssrc, rtp->themssrc, fraction, lost,
03517 rtp->rxjitter,
03518 rtp->rtcp->themrxlsr,
03519 (double)(ntohl(rtcpheader[7])/65536.0));
03520 }
03521
03522 return res;
03523 }
03524
03525
03526
03527
03528 static int ast_rtcp_write(const void *data)
03529 {
03530 struct ast_rtp *rtp = (struct ast_rtp *)data;
03531 int res;
03532
03533 if (!rtp || !rtp->rtcp)
03534 return 0;
03535
03536 if (rtp->txcount > rtp->rtcp->lastsrtxcount)
03537 res = ast_rtcp_write_sr(data);
03538 else
03539 res = ast_rtcp_write_rr(data);
03540
03541 return res;
03542 }
03543
03544
03545 int ast_rtp_sendcng(struct ast_rtp *rtp, int level)
03546 {
03547 unsigned int *rtpheader;
03548 int hdrlen = 12;
03549 int res;
03550 int payload;
03551 char data[256];
03552 level = 127 - (level & 0x7f);
03553 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_CN);
03554
03555
03556 if (!rtp->them.sin_addr.s_addr)
03557 return 0;
03558
03559 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
03560
03561
03562 rtpheader = (unsigned int *)data;
03563 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
03564 rtpheader[1] = htonl(rtp->lastts);
03565 rtpheader[2] = htonl(rtp->ssrc);
03566 data[12] = level;
03567 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
03568 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 1, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
03569 if (res <0)
03570 ast_log(LOG_ERROR, "RTP Comfort Noise Transmission error to %s:%d: %s\n", ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
03571 if (rtp_debug_test_addr(&rtp->them))
03572 ast_verbose("Sent Comfort Noise RTP packet to %s:%u (type %d, seq %u, ts %u, len %d)\n"
03573 , ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);
03574
03575 }
03576 return 0;
03577 }
03578
03579
03580 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
03581 {
03582 unsigned char *rtpheader;
03583 int hdrlen = 12;
03584 int res;
03585 unsigned int ms;
03586 int pred;
03587 int mark = 0;
03588 int rate = rtp_get_rate(f->subclass) / 1000;
03589
03590 if (f->subclass == AST_FORMAT_G722) {
03591 f->samples /= 2;
03592 }
03593
03594 if (rtp->sending_digit) {
03595 return 0;
03596 }
03597
03598 ms = calc_txstamp(rtp, &f->delivery);
03599
03600 if (f->frametype == AST_FRAME_VOICE) {
03601 pred = rtp->lastts + f->samples;
03602
03603
03604 rtp->lastts = rtp->lastts + ms * rate;
03605 if (ast_tvzero(f->delivery)) {
03606
03607
03608 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW)
03609 rtp->lastts = pred;
03610 else {
03611 ast_debug(3, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
03612 mark = 1;
03613 }
03614 }
03615 } else if (f->frametype == AST_FRAME_VIDEO) {
03616 mark = f->subclass & 0x1;
03617 pred = rtp->lastovidtimestamp + f->samples;
03618
03619 rtp->lastts = rtp->lastts + ms * 90;
03620
03621 if (ast_tvzero(f->delivery)) {
03622 if (abs(rtp->lastts - pred) < 7200) {
03623 rtp->lastts = pred;
03624 rtp->lastovidtimestamp += f->samples;
03625 } else {
03626 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, f->samples);
03627 rtp->lastovidtimestamp = rtp->lastts;
03628 }
03629 }
03630 } else {
03631 pred = rtp->lastotexttimestamp + f->samples;
03632
03633 rtp->lastts = rtp->lastts + ms;
03634
03635 if (ast_tvzero(f->delivery)) {
03636 if (abs(rtp->lastts - pred) < 7200) {
03637 rtp->lastts = pred;
03638 rtp->lastotexttimestamp += f->samples;
03639 } else {
03640 ast_debug(3, "Difference is %d, ms is %d, pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, rtp->lastts, pred, f->samples);
03641 rtp->lastotexttimestamp = rtp->lastts;
03642 }
03643 }
03644 }
03645
03646
03647 if (rtp->set_marker_bit) {
03648 mark = 1;
03649 rtp->set_marker_bit = 0;
03650 }
03651
03652
03653
03654
03655 if (rtp->lastts > rtp->lastdigitts)
03656 rtp->lastdigitts = rtp->lastts;
03657
03658 if (ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO))
03659 rtp->lastts = f->ts * rate;
03660
03661
03662 rtpheader = (unsigned char *)(f->data.ptr - hdrlen);
03663
03664 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
03665 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
03666 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
03667
03668 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
03669 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
03670 if (res < 0) {
03671 if (!rtp->nat || (rtp->nat && (ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
03672 ast_debug(1, "RTP Transmission error of packet %d to %s:%d: %s\n", rtp->seqno, ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
03673 } else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) {
03674
03675 if (option_debug || rtpdebug)
03676 ast_debug(0, "RTP NAT: Can't write RTP to private address %s:%d, waiting for other end to send audio...\n", ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
03677 ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN);
03678 }
03679 } else {
03680 rtp->txcount++;
03681 rtp->txoctetcount +=(res - hdrlen);
03682
03683
03684 if (rtp->rtcp && rtp->rtcp->them.sin_addr.s_addr && rtp->rtcp->schedid < 1) {
03685 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
03686 }
03687 }
03688
03689 if (rtp_debug_test_addr(&rtp->them))
03690 ast_verbose("Sent RTP packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
03691 ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), codec, rtp->seqno, rtp->lastts,res - hdrlen);
03692 }
03693
03694 rtp->seqno++;
03695
03696 return 0;
03697 }
03698
03699 void ast_rtp_codec_setpref(struct ast_rtp *rtp, struct ast_codec_pref *prefs)
03700 {
03701 struct ast_format_list current_format_old, current_format_new;
03702
03703
03704
03705
03706 if (rtp->lasttxformat == 0) {
03707 rtp->pref = *prefs;
03708 return;
03709 }
03710
03711 current_format_old = ast_codec_pref_getsize(&rtp->pref, rtp->lasttxformat);
03712
03713 rtp->pref = *prefs;
03714
03715 current_format_new = ast_codec_pref_getsize(&rtp->pref, rtp->lasttxformat);
03716
03717
03718
03719
03720 if ((current_format_new.inc_ms != 0) &&
03721 (current_format_new.cur_ms != current_format_old.cur_ms)) {
03722 int new_size = (current_format_new.cur_ms * current_format_new.fr_len) / current_format_new.inc_ms;
03723
03724 if (rtp->smoother) {
03725 ast_smoother_reconfigure(rtp->smoother, new_size);
03726 if (option_debug) {
03727 ast_log(LOG_DEBUG, "Adjusted smoother to %d ms and %d bytes\n", current_format_new.cur_ms, new_size);
03728 }
03729 } else {
03730 if (!(rtp->smoother = ast_smoother_new(new_size))) {
03731 ast_log(LOG_WARNING, "Unable to create smoother: format: %d ms: %d len: %d\n", rtp->lasttxformat, current_format_new.cur_ms, new_size);
03732 return;
03733 }
03734 if (current_format_new.flags) {
03735 ast_smoother_set_flags(rtp->smoother, current_format_new.flags);
03736 }
03737 if (option_debug) {
03738 ast_log(LOG_DEBUG, "Created smoother: format: %d ms: %d len: %d\n", rtp->lasttxformat, current_format_new.cur_ms, new_size);
03739 }
03740 }
03741 }
03742
03743 }
03744
03745 struct ast_codec_pref *ast_rtp_codec_getpref(struct ast_rtp *rtp)
03746 {
03747 return &rtp->pref;
03748 }
03749
03750 int ast_rtp_codec_getformat(int pt)
03751 {
03752 if (pt < 0 || pt >= MAX_RTP_PT)
03753 return 0;
03754
03755 if (static_RTP_PT[pt].isAstFormat)
03756 return static_RTP_PT[pt].code;
03757 else
03758 return 0;
03759 }
03760
03761 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
03762 {
03763 struct ast_frame *f;
03764 int codec;
03765 int hdrlen = 12;
03766 int subclass;
03767
03768
03769
03770 if (!rtp->them.sin_addr.s_addr)
03771 return 0;
03772
03773
03774 if (!_f->datalen && !rtp->red)
03775 return 0;
03776
03777
03778 if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO) && (_f->frametype != AST_FRAME_TEXT)) {
03779 ast_log(LOG_WARNING, "RTP can only send voice, video and text\n");
03780 return -1;
03781 }
03782
03783 if (rtp->red) {
03784
03785
03786 if ((_f = red_t140_to_red(rtp->red)) == NULL)
03787 return 0;
03788 }
03789
03790
03791 subclass = _f->subclass;
03792 if (_f->frametype == AST_FRAME_VIDEO)
03793 subclass &= ~0x1;
03794
03795 codec = ast_rtp_lookup_code(rtp, 1, subclass);
03796 if (codec < 0) {
03797 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
03798 return -1;
03799 }
03800
03801 if (rtp->lasttxformat != subclass) {
03802
03803 ast_debug(1, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
03804 rtp->lasttxformat = subclass;
03805 if (rtp->smoother)
03806 ast_smoother_free(rtp->smoother);
03807 rtp->smoother = NULL;
03808 }
03809
03810 if (!rtp->smoother && subclass != AST_FORMAT_SPEEX && subclass != AST_FORMAT_G723_1) {
03811 struct ast_format_list fmt = ast_codec_pref_getsize(&rtp->pref, subclass);
03812 if (fmt.inc_ms) {
03813 if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
03814 ast_log(LOG_WARNING, "Unable to create smoother: format: %d ms: %d len: %d\n", subclass, fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
03815 return -1;
03816 }
03817 if (fmt.flags)
03818 ast_smoother_set_flags(rtp->smoother, fmt.flags);
03819 ast_debug(1, "Created smoother: format: %d ms: %d len: %d\n", subclass, fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
03820 }
03821 }
03822 if (rtp->smoother) {
03823 if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
03824 ast_smoother_feed_be(rtp->smoother, _f);
03825 } else {
03826 ast_smoother_feed(rtp->smoother, _f);
03827 }
03828
03829 while ((f = ast_smoother_read(rtp->smoother)) && (f->data.ptr)) {
03830 ast_rtp_raw_write(rtp, f, codec);
03831 }
03832 } else {
03833
03834 if (_f->offset < hdrlen)
03835 f = ast_frdup(_f);
03836 else
03837 f = _f;
03838 if (f->data.ptr)
03839 ast_rtp_raw_write(rtp, f, codec);
03840 if (f != _f)
03841 ast_frfree(f);
03842 }
03843
03844 return 0;
03845 }
03846
03847
03848 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
03849 {
03850 AST_RWLIST_WRLOCK(&protos);
03851 AST_RWLIST_REMOVE(&protos, proto, list);
03852 AST_RWLIST_UNLOCK(&protos);
03853 }
03854
03855
03856 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
03857 {
03858 struct ast_rtp_protocol *cur;
03859
03860 AST_RWLIST_WRLOCK(&protos);
03861 AST_RWLIST_TRAVERSE(&protos, cur, list) {
03862 if (!strcmp(cur->type, proto->type)) {
03863 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
03864 AST_RWLIST_UNLOCK(&protos);
03865 return -1;
03866 }
03867 }
03868 AST_RWLIST_INSERT_HEAD(&protos, proto, list);
03869 AST_RWLIST_UNLOCK(&protos);
03870
03871 return 0;
03872 }
03873
03874
03875 static enum ast_bridge_result bridge_native_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp *p0, struct ast_rtp *p1, struct ast_rtp *vp0, struct ast_rtp *vp1, struct ast_rtp *tp0, struct ast_rtp *tp1, struct ast_rtp_protocol *pr0, struct ast_rtp_protocol *pr1, int codec0, int codec1, int timeoutms, int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
03876 {
03877 struct ast_frame *fr = NULL;
03878 struct ast_channel *who = NULL, *other = NULL, *cs[3] = {NULL, };
03879 int oldcodec0 = codec0, oldcodec1 = codec1;
03880 struct sockaddr_in ac1 = {0,}, vac1 = {0,}, tac1 = {0,}, ac0 = {0,}, vac0 = {0,}, tac0 = {0,};
03881 struct sockaddr_in t1 = {0,}, vt1 = {0,}, tt1 = {0,}, t0 = {0,}, vt0 = {0,}, tt0 = {0,};
03882
03883
03884
03885
03886 if (!(pr0->set_rtp_peer(c0, p1, vp1, tp1, codec1, ast_test_flag(p1, FLAG_NAT_ACTIVE)))) {
03887 ast_rtp_get_peer(p1, &ac1);
03888 if (vp1)
03889 ast_rtp_get_peer(vp1, &vac1);
03890 if (tp1)
03891 ast_rtp_get_peer(tp1, &tac1);
03892 } else
03893 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
03894
03895
03896 if (!(pr1->set_rtp_peer(c1, p0, vp0, tp0, codec0, ast_test_flag(p0, FLAG_NAT_ACTIVE)))) {
03897 ast_rtp_get_peer(p0, &ac0);
03898 if (vp0)
03899 ast_rtp_get_peer(vp0, &vac0);
03900 if (tp0)
03901 ast_rtp_get_peer(tp0, &tac0);
03902 } else
03903 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c1->name, c0->name);
03904
03905
03906 ast_channel_unlock(c0);
03907 ast_channel_unlock(c1);
03908
03909 ast_poll_channel_add(c0, c1);
03910
03911
03912 cs[0] = c0;
03913 cs[1] = c1;
03914 cs[2] = NULL;
03915 for (;;) {
03916
03917 if ((c0->tech_pvt != pvt0) ||
03918 (c1->tech_pvt != pvt1) ||
03919 (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
03920 (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
03921 ast_debug(1, "Oooh, something is weird, backing out\n");
03922 if (c0->tech_pvt == pvt0)
03923 if (pr0->set_rtp_peer(c0, NULL, NULL, NULL, 0, 0))
03924 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
03925 if (c1->tech_pvt == pvt1)
03926 if (pr1->set_rtp_peer(c1, NULL, NULL, NULL, 0, 0))
03927 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
03928 ast_poll_channel_del(c0, c1);
03929 return AST_BRIDGE_RETRY;
03930 }
03931
03932
03933 ast_rtp_get_peer(p1, &t1);
03934 if (vp1)
03935 ast_rtp_get_peer(vp1, &vt1);
03936 if (tp1)
03937 ast_rtp_get_peer(tp1, &tt1);
03938 if (pr1->get_codec)
03939 codec1 = pr1->get_codec(c1);
03940 ast_rtp_get_peer(p0, &t0);
03941 if (vp0)
03942 ast_rtp_get_peer(vp0, &vt0);
03943 if (tp0)
03944 ast_rtp_get_peer(tp0, &tt0);
03945 if (pr0->get_codec)
03946 codec0 = pr0->get_codec(c0);
03947 if ((inaddrcmp(&t1, &ac1)) ||
03948 (vp1 && inaddrcmp(&vt1, &vac1)) ||
03949 (tp1 && inaddrcmp(&tt1, &tac1)) ||
03950 (codec1 != oldcodec1)) {
03951 ast_debug(2, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
03952 c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port), codec1);
03953 ast_debug(2, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
03954 c1->name, ast_inet_ntoa(vt1.sin_addr), ntohs(vt1.sin_port), codec1);
03955 ast_debug(2, "Oooh, '%s' changed end taddress to %s:%d (format %d)\n",
03956 c1->name, ast_inet_ntoa(tt1.sin_addr), ntohs(tt1.sin_port), codec1);
03957 ast_debug(2, "Oooh, '%s' was %s:%d/(format %d)\n",
03958 c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
03959 ast_debug(2, "Oooh, '%s' was %s:%d/(format %d)\n",
03960 c1->name, ast_inet_ntoa(vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
03961 ast_debug(2, "Oooh, '%s' was %s:%d/(format %d)\n",
03962 c1->name, ast_inet_ntoa(tac1.sin_addr), ntohs(tac1.sin_port), oldcodec1);
03963 if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL, tt1.sin_addr.s_addr ? tp1 : NULL, codec1, ast_test_flag(p1, FLAG_NAT_ACTIVE)))
03964 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
03965 memcpy(&ac1, &t1, sizeof(ac1));
03966 memcpy(&vac1, &vt1, sizeof(vac1));
03967 memcpy(&tac1, &tt1, sizeof(tac1));
03968 oldcodec1 = codec1;
03969 }
03970 if ((inaddrcmp(&t0, &ac0)) ||
03971 (vp0 && inaddrcmp(&vt0, &vac0)) ||
03972 (tp0 && inaddrcmp(&tt0, &tac0)) ||
03973 (codec0 != oldcodec0)) {
03974 ast_debug(2, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
03975 c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port), codec0);
03976 ast_debug(2, "Oooh, '%s' was %s:%d/(format %d)\n",
03977 c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
03978 if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, tt0.sin_addr.s_addr ? tp0 : NULL, codec0, ast_test_flag(p0, FLAG_NAT_ACTIVE)))
03979 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
03980 memcpy(&ac0, &t0, sizeof(ac0));
03981 memcpy(&vac0, &vt0, sizeof(vac0));
03982 memcpy(&tac0, &tt0, sizeof(tac0));
03983 oldcodec0 = codec0;
03984 }
03985
03986
03987 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
03988 if (!timeoutms) {
03989 if (pr0->set_rtp_peer(c0, NULL, NULL, NULL, 0, 0))
03990 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
03991 if (pr1->set_rtp_peer(c1, NULL, NULL, NULL, 0, 0))
03992 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
03993 return AST_BRIDGE_RETRY;
03994 }
03995 ast_debug(1, "Ooh, empty read...\n");
03996 if (ast_check_hangup(c0) || ast_check_hangup(c1))
03997 break;
03998 continue;
03999 }
04000 fr = ast_read(who);
04001 other = (who == c0) ? c1 : c0;
04002 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
04003 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
04004 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
04005
04006 *fo = fr;
04007 *rc = who;
04008 ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
04009 if (c0->tech_pvt == pvt0)
04010 if (pr0->set_rtp_peer(c0, NULL, NULL, NULL, 0, 0))
04011 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
04012 if (c1->tech_pvt == pvt1)
04013 if (pr1->set_rtp_peer(c1, NULL, NULL, NULL, 0, 0))
04014 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
04015 ast_poll_channel_del(c0, c1);
04016 return AST_BRIDGE_COMPLETE;
04017 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
04018 if ((fr->subclass == AST_CONTROL_HOLD) ||
04019 (fr->subclass == AST_CONTROL_UNHOLD) ||
04020 (fr->subclass == AST_CONTROL_VIDUPDATE) ||
04021 (fr->subclass == AST_CONTROL_SRCUPDATE) ||
04022 (fr->subclass == AST_CONTROL_T38_PARAMETERS)) {
04023 if (fr->subclass == AST_CONTROL_HOLD) {
04024
04025 if (who == c0)
04026 pr1->set_rtp_peer(c1, NULL, NULL, NULL, 0, 0);
04027 else
04028 pr0->set_rtp_peer(c0, NULL, NULL, NULL, 0, 0);
04029 } else if (fr->subclass == AST_CONTROL_UNHOLD) {
04030
04031 if (who == c0)
04032 pr1->set_rtp_peer(c1, p0, vp0, tp0, codec0, ast_test_flag(p0, FLAG_NAT_ACTIVE));
04033 else
04034 pr0->set_rtp_peer(c0, p1, vp1, tp1, codec1, ast_test_flag(p1, FLAG_NAT_ACTIVE));
04035 }
04036
04037 ast_rtp_get_peer(p0, &t0);
04038 memcpy(&ac0, &t0, sizeof(ac0));
04039 ast_rtp_get_peer(p1, &t1);
04040 memcpy(&ac1, &t1, sizeof(ac1));
04041
04042 if (pr0->get_codec && c0->tech_pvt)
04043 oldcodec0 = codec0 = pr0->get_codec(c0);
04044 if (pr1->get_codec && c1->tech_pvt)
04045 oldcodec1 = codec1 = pr1->get_codec(c1);
04046 ast_indicate_data(other, fr->subclass, fr->data.ptr, fr->datalen);
04047 ast_frfree(fr);
04048 } else {
04049 *fo = fr;
04050 *rc = who;
04051 ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
04052 return AST_BRIDGE_COMPLETE;
04053 }
04054 } else {
04055 if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
04056 (fr->frametype == AST_FRAME_DTMF_END) ||
04057 (fr->frametype == AST_FRAME_VOICE) ||
04058 (fr->frametype == AST_FRAME_VIDEO) ||
04059 (fr->frametype == AST_FRAME_IMAGE) ||
04060 (fr->frametype == AST_FRAME_HTML) ||
04061 (fr->frametype == AST_FRAME_MODEM) ||
04062 (fr->frametype == AST_FRAME_TEXT)) {
04063 ast_write(other, fr);
04064 }
04065 ast_frfree(fr);
04066 }
04067
04068 #ifndef HAVE_EPOLL
04069 cs[2] = cs[0];
04070 cs[0] = cs[1];
04071 cs[1] = cs[2];
04072 #endif
04073 }
04074
04075 ast_poll_channel_del(c0, c1);
04076
04077 if (pr0->set_rtp_peer(c0, NULL, NULL, NULL, 0, 0))
04078 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
04079 if (pr1->set_rtp_peer(c1, NULL, NULL, NULL, 0, 0))
04080 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
04081
04082 return AST_BRIDGE_FAILED;
04083 }
04084
04085
04086 #ifdef P2P_INTENSE
04087 static int p2p_rtp_callback(int *id, int fd, short events, void *cbdata)
04088 {
04089 int res = 0, hdrlen = 12;
04090 struct sockaddr_in sin;
04091 socklen_t len;
04092 unsigned int *header;
04093 struct ast_rtp *rtp = cbdata, *bridged = NULL;
04094
04095 if (!rtp)
04096 return 1;
04097
04098 len = sizeof(sin);
04099 if ((res = recvfrom(fd, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET, 0, (struct sockaddr *)&sin, &len)) < 0)
04100 return 1;
04101
04102 header = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
04103
04104
04105 if ((rtp->nat) &&
04106 ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
04107 (rtp->them.sin_port != sin.sin_port))) {
04108 rtp->them = sin;
04109 rtp->rxseqno = 0;
04110 ast_set_flag(rtp, FLAG_NAT_ACTIVE);
04111 if (option_debug || rtpdebug)
04112 ast_debug(0, "P2P RTP NAT: Got audio from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
04113 }
04114
04115
04116 if ((bridged = ast_rtp_get_bridged(rtp)))
04117 bridge_p2p_rtp_write(rtp, bridged, header, res, hdrlen);
04118
04119 return 1;
04120 }
04121
04122
04123 static int p2p_callback_enable(struct ast_channel *chan, struct ast_rtp *rtp, int **iod)
04124 {
04125
04126 if (ast_test_flag(rtp, FLAG_P2P_NEED_DTMF) || ast_test_flag(rtp, FLAG_HAS_STUN) || !rtp->io)
04127 return 0;
04128
04129
04130 if (rtp->ioid) {
04131 ast_io_remove(rtp->io, rtp->ioid);
04132 rtp->ioid = NULL;
04133 }
04134
04135
04136 chan->fds[0] = -1;
04137
04138
04139 iod[0] = ast_io_add(rtp->io, ast_rtp_fd(rtp), p2p_rtp_callback, AST_IO_IN, rtp);
04140
04141 return 1;
04142 }
04143 #else
04144 static int p2p_callback_enable(struct ast_channel *chan, struct ast_rtp *rtp, int **iod)
04145 {
04146 return 0;
04147 }
04148 #endif
04149
04150
04151 static int p2p_callback_disable(struct ast_channel *chan, struct ast_rtp *rtp, int **iod)
04152 {
04153 ast_channel_lock(chan);
04154
04155
04156 ast_io_remove(rtp->io, iod[0]);
04157
04158
04159 chan->fds[0] = ast_rtp_fd(rtp);
04160 ast_channel_unlock(chan);
04161
04162
04163 if (ast_test_flag(rtp, FLAG_CALLBACK_MODE))
04164 rtp->ioid = ast_io_add(rtp->io, ast_rtp_fd(rtp), rtpread, AST_IO_IN, rtp);
04165
04166 return 0;
04167 }
04168
04169
04170 static void p2p_set_bridge(struct ast_rtp *rtp0, struct ast_rtp *rtp1)
04171 {
04172 rtp_bridge_lock(rtp0);
04173 rtp0->bridged = rtp1;
04174 rtp_bridge_unlock(rtp0);
04175 }
04176
04177
04178
04179
04180
04181
04182
04183 static enum ast_bridge_result bridge_p2p_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp *p0, struct ast_rtp *p1, int timeoutms, int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
04184 {
04185 struct ast_frame *fr = NULL;
04186 struct ast_channel *who = NULL, *other = NULL, *cs[3] = {NULL, };
04187 int *p0_iod[2] = {NULL, NULL}, *p1_iod[2] = {NULL, NULL};
04188 int p0_callback = 0, p1_callback = 0;
04189 enum ast_bridge_result res = AST_BRIDGE_FAILED;
04190
04191
04192 ast_clear_flag(p0, FLAG_P2P_SENT_MARK);
04193 p2p_set_bridge(p0, p1);
04194 ast_clear_flag(p1, FLAG_P2P_SENT_MARK);
04195 p2p_set_bridge(p1, p0);
04196
04197
04198 p0_callback = p2p_callback_enable(c0, p0, &p0_iod[0]);
04199 p1_callback = p2p_callback_enable(c1, p1, &p1_iod[0]);
04200
04201
04202 ast_channel_unlock(c0);
04203 ast_channel_unlock(c1);
04204
04205 ast_poll_channel_add(c0, c1);
04206
04207
04208 cs[0] = c0;
04209 cs[1] = c1;
04210 cs[2] = NULL;
04211 for (;;) {
04212
04213 if ((c0->rawreadformat != c1->rawwriteformat) || (c1->rawreadformat != c0->rawwriteformat)) {
04214 ast_debug(3, "p2p-rtp-bridge: Oooh, formats changed, backing out\n");
04215 res = AST_BRIDGE_FAILED_NOWARN;
04216 break;
04217 }
04218
04219 if ((c0->tech_pvt != pvt0) ||
04220 (c1->tech_pvt != pvt1) ||
04221 (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
04222 (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
04223 ast_debug(3, "p2p-rtp-bridge: Oooh, something is weird, backing out\n");
04224
04225 if ((c0->masq || c0->masqr) && (fr = ast_read(c0)))
04226 ast_frfree(fr);
04227 if ((c1->masq || c1->masqr) && (fr = ast_read(c1)))
04228 ast_frfree(fr);
04229 res = AST_BRIDGE_RETRY;
04230 break;
04231 }
04232
04233 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
04234 if (!timeoutms) {
04235 res = AST_BRIDGE_RETRY;
04236 break;
04237 }
04238 if (option_debug > 2)
04239 ast_log(LOG_NOTICE, "p2p-rtp-bridge: Ooh, empty read...\n");
04240 if (ast_check_hangup(c0) || ast_check_hangup(c1))
04241 break;
04242 continue;
04243 }
04244
04245 fr = ast_read(who);
04246 other = (who == c0) ? c1 : c0;
04247
04248 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
04249 ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
04250 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
04251
04252 *fo = fr;
04253 *rc = who;
04254 ast_debug(3, "p2p-rtp-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
04255 res = AST_BRIDGE_COMPLETE;
04256 break;
04257 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
04258 if ((fr->subclass == AST_CONTROL_HOLD) ||
04259 (fr->subclass == AST_CONTROL_UNHOLD) ||
04260 (fr->subclass == AST_CONTROL_VIDUPDATE) ||
04261 (fr->subclass == AST_CONTROL_SRCUPDATE) ||
04262 (fr->subclass == AST_CONTROL_T38_PARAMETERS)) {
04263
04264 if (fr->subclass == AST_CONTROL_HOLD) {
04265 if (p0_callback)
04266 p0_callback = p2p_callback_disable(c0, p0, &p0_iod[0]);
04267 if (p1_callback)
04268 p1_callback = p2p_callback_disable(c1, p1, &p1_iod[0]);
04269 p2p_set_bridge(p0, NULL);
04270 p2p_set_bridge(p1, NULL);
04271 } else if (fr->subclass == AST_CONTROL_UNHOLD) {
04272
04273 ast_clear_flag(p0, FLAG_P2P_SENT_MARK);
04274 p2p_set_bridge(p0, p1);
04275 ast_clear_flag(p1, FLAG_P2P_SENT_MARK);
04276 p2p_set_bridge(p1, p0);
04277 p0_callback = p2p_callback_enable(c0, p0, &p0_iod[0]);
04278 p1_callback = p2p_callback_enable(c1, p1, &p1_iod[0]);
04279 }
04280 ast_indicate_data(other, fr->subclass, fr->data.ptr, fr->datalen);
04281 ast_frfree(fr);
04282 } else {
04283 *fo = fr;
04284 *rc = who;
04285 ast_debug(3, "p2p-rtp-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
04286 res = AST_BRIDGE_COMPLETE;
04287 break;
04288 }
04289 } else {
04290 if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
04291 (fr->frametype == AST_FRAME_DTMF_END) ||
04292 (fr->frametype == AST_FRAME_VOICE) ||
04293 (fr->frametype == AST_FRAME_VIDEO) ||
04294 (fr->frametype == AST_FRAME_IMAGE) ||
04295 (fr->frametype == AST_FRAME_HTML) ||
04296 (fr->frametype == AST_FRAME_MODEM) ||
04297 (fr->frametype == AST_FRAME_TEXT)) {
04298 ast_write(other, fr);
04299 }
04300
04301 ast_frfree(fr);
04302 }
04303
04304 #ifndef HAVE_EPOLL
04305 cs[2] = cs[0];
04306 cs[0] = cs[1];
04307 cs[1] = cs[2];
04308 #endif
04309 }
04310
04311
04312 if (p0_callback)
04313 p0_callback = p2p_callback_disable(c0, p0, &p0_iod[0]);
04314 if (p1_callback)
04315 p1_callback = p2p_callback_disable(c1, p1, &p1_iod[0]);
04316
04317
04318 p2p_set_bridge(p0, NULL);
04319 p2p_set_bridge(p1, NULL);
04320
04321 ast_poll_channel_del(c0, c1);
04322
04323 return res;
04324 }
04325
04326
04327
04328
04329
04330
04331
04332
04333
04334
04335
04336
04337
04338
04339
04340
04341
04342
04343
04344
04345
04346
04347
04348
04349
04350
04351
04352
04353
04354
04355
04356
04357
04358 enum ast_bridge_result ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
04359 {
04360 struct ast_rtp *p0 = NULL, *p1 = NULL;
04361 struct ast_rtp *vp0 = NULL, *vp1 = NULL;
04362 struct ast_rtp *tp0 = NULL, *tp1 = NULL;
04363 struct ast_rtp_protocol *pr0 = NULL, *pr1 = NULL;
04364 enum ast_rtp_get_result audio_p0_res = AST_RTP_GET_FAILED, video_p0_res = AST_RTP_GET_FAILED, text_p0_res = AST_RTP_GET_FAILED;
04365 enum ast_rtp_get_result audio_p1_res = AST_RTP_GET_FAILED, video_p1_res = AST_RTP_GET_FAILED, text_p1_res = AST_RTP_GET_FAILED;
04366 enum ast_bridge_result res = AST_BRIDGE_FAILED;
04367 int codec0 = 0, codec1 = 0;
04368 void *pvt0 = NULL, *pvt1 = NULL;
04369
04370
04371 ast_channel_lock(c0);
04372 while (ast_channel_trylock(c1)) {
04373 ast_channel_unlock(c0);
04374 usleep(1);
04375 ast_channel_lock(c0);
04376 }
04377
04378
04379 if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
04380 ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
04381 ast_channel_unlock(c0);
04382 ast_channel_unlock(c1);
04383 return AST_BRIDGE_FAILED;
04384 }
04385
04386
04387 if (!(pr0 = get_proto(c0))) {
04388 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
04389 ast_channel_unlock(c0);
04390 ast_channel_unlock(c1);
04391 return AST_BRIDGE_FAILED;
04392 }
04393 if (!(pr1 = get_proto(c1))) {
04394 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
04395 ast_channel_unlock(c0);
04396 ast_channel_unlock(c1);
04397 return AST_BRIDGE_FAILED;
04398 }
04399
04400
04401 pvt0 = c0->tech_pvt;
04402 pvt1 = c1->tech_pvt;
04403
04404
04405 audio_p0_res = pr0->get_rtp_info(c0, &p0);
04406 video_p0_res = pr0->get_vrtp_info ? pr0->get_vrtp_info(c0, &vp0) : AST_RTP_GET_FAILED;
04407 text_p0_res = pr0->get_trtp_info ? pr0->get_trtp_info(c0, &vp0) : AST_RTP_GET_FAILED;
04408 audio_p1_res = pr1->get_rtp_info(c1, &p1);
04409 video_p1_res = pr1->get_vrtp_info ? pr1->get_vrtp_info(c1, &vp1) : AST_RTP_GET_FAILED;
04410 text_p1_res = pr1->get_trtp_info ? pr1->get_trtp_info(c1, &vp1) : AST_RTP_GET_FAILED;
04411
04412
04413 if (video_p0_res != AST_RTP_GET_FAILED && (audio_p0_res != AST_RTP_TRY_NATIVE || video_p0_res != AST_RTP_TRY_NATIVE))
04414 audio_p0_res = AST_RTP_GET_FAILED;
04415 if (video_p1_res != AST_RTP_GET_FAILED && (audio_p1_res != AST_RTP_TRY_NATIVE || video_p1_res != AST_RTP_TRY_NATIVE))
04416 audio_p1_res = AST_RTP_GET_FAILED;
04417
04418
04419 if (audio_p0_res == AST_RTP_GET_FAILED || audio_p1_res == AST_RTP_GET_FAILED) {
04420
04421 ast_channel_unlock(c0);
04422 ast_channel_unlock(c1);
04423 return AST_BRIDGE_FAILED_NOWARN;
04424 }
04425
04426
04427 if (ast_test_flag(p0, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
04428 ast_set_flag(p0, FLAG_P2P_NEED_DTMF);
04429 audio_p0_res = AST_RTP_TRY_PARTIAL;
04430 }
04431
04432 if (ast_test_flag(p1, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)) {
04433 ast_set_flag(p1, FLAG_P2P_NEED_DTMF);
04434 audio_p1_res = AST_RTP_TRY_PARTIAL;
04435 }
04436
04437
04438
04439
04440
04441
04442
04443
04444
04445
04446
04447
04448
04449
04450 if ((ast_test_flag(p0, FLAG_HAS_DTMF) != ast_test_flag(p1, FLAG_HAS_DTMF)) ||
04451 (!c0->tech->send_digit_begin != !c1->tech->send_digit_begin)) {
04452 if (!ast_test_flag(p0, FLAG_P2P_NEED_DTMF) || !ast_test_flag(p1, FLAG_P2P_NEED_DTMF)) {
04453 ast_channel_unlock(c0);
04454 ast_channel_unlock(c1);
04455 return AST_BRIDGE_FAILED_NOWARN;
04456 }
04457 audio_p0_res = AST_RTP_TRY_PARTIAL;
04458 audio_p1_res = AST_RTP_TRY_PARTIAL;
04459 }
04460
04461
04462 if ((audio_p0_res == AST_RTP_TRY_PARTIAL && ast_test_flag(p0, FLAG_P2P_NEED_DTMF)) ||
04463 (audio_p1_res == AST_RTP_TRY_PARTIAL && ast_test_flag(p1, FLAG_P2P_NEED_DTMF))) {
04464 ast_channel_unlock(c0);
04465 ast_channel_unlock(c1);
04466 return AST_BRIDGE_FAILED_NOWARN;
04467 }
04468
04469
04470 codec0 = pr0->get_codec ? pr0->get_codec(c0) : 0;
04471 codec1 = pr1->get_codec ? pr1->get_codec(c1) : 0;
04472 if (codec0 && codec1 && !(codec0 & codec1)) {
04473
04474 ast_debug(3, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0, codec1);
04475 ast_channel_unlock(c0);
04476 ast_channel_unlock(c1);
04477 return AST_BRIDGE_FAILED_NOWARN;
04478 }
04479
04480
04481 if (audio_p0_res == AST_RTP_TRY_PARTIAL || audio_p1_res == AST_RTP_TRY_PARTIAL) {
04482 struct ast_format_list fmt0, fmt1;
04483
04484
04485 if (c0->rawreadformat != c1->rawwriteformat || c1->rawreadformat != c0->rawwriteformat) {
04486 ast_debug(1, "Cannot packet2packet bridge - raw formats are incompatible\n");
04487 ast_channel_unlock(c0);
04488 ast_channel_unlock(c1);
04489 return AST_BRIDGE_FAILED_NOWARN;
04490 }
04491
04492 fmt0 = ast_codec_pref_getsize(&p0->pref, c0->rawreadformat);
04493 fmt1 = ast_codec_pref_getsize(&p1->pref, c1->rawreadformat);
04494 if (fmt0.cur_ms != fmt1.cur_ms) {
04495 ast_debug(1, "Cannot packet2packet bridge - packetization settings prevent it\n");
04496 ast_channel_unlock(c0);
04497 ast_channel_unlock(c1);
04498 return AST_BRIDGE_FAILED_NOWARN;
04499 }
04500
04501 ast_verb(3, "Packet2Packet bridging %s and %s\n", c0->name, c1->name);
04502 res = bridge_p2p_loop(c0, c1, p0, p1, timeoutms, flags, fo, rc, pvt0, pvt1);
04503 } else {
04504 ast_verb(3, "Native bridging %s and %s\n", c0->name, c1->name);
04505 res = bridge_native_loop(c0, c1, p0, p1, vp0, vp1, tp0, tp1, pr0, pr1, codec0, codec1, timeoutms, flags, fo, rc, pvt0, pvt1);
04506 }
04507
04508 return res;
04509 }
04510
04511 static char *rtp_do_debug_ip(struct ast_cli_args *a, int deprecated)
04512 {
04513 struct hostent *hp;
04514 struct ast_hostent ahp;
04515 int port = 0;
04516 char *p, *arg;
04517
04518 if (deprecated == 1) {
04519 arg = a->argv[3];
04520 } else {
04521 arg = a->argv[4];
04522 }
04523 p = strstr(arg, ":");
04524 if (p) {
04525 *p = '\0';
04526 p++;
04527 port = atoi(p);
04528 }
04529 hp = ast_gethostbyname(arg, &ahp);
04530 if (hp == NULL) {
04531 ast_cli(a->fd, "Lookup failed for '%s'\n", arg);
04532 return CLI_FAILURE;
04533 }
04534 rtpdebugaddr.sin_family = AF_INET;
04535 memcpy(&rtpdebugaddr.sin_addr, hp->h_addr, sizeof(rtpdebugaddr.sin_addr));
04536 rtpdebugaddr.sin_port = htons(port);
04537 if (port == 0)
04538 ast_cli(a->fd, "RTP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtpdebugaddr.sin_addr));
04539 else
04540 ast_cli(a->fd, "RTP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtpdebugaddr.sin_addr), port);
04541 rtpdebug = 1;
04542 return CLI_SUCCESS;
04543 }
04544
04545 static char *rtcp_do_debug_ip(struct ast_cli_args *a)
04546 {
04547 struct hostent *hp;
04548 struct ast_hostent ahp;
04549 int port = 0;
04550 char *p, *arg;
04551
04552 arg = a->argv[3];
04553 p = strstr(arg, ":");
04554 if (p) {
04555 *p = '\0';
04556 p++;
04557 port = atoi(p);
04558 }
04559 hp = ast_gethostbyname(arg, &ahp);
04560 if (hp == NULL) {
04561 ast_cli(a->fd, "Lookup failed for '%s'\n", arg);
04562 return CLI_FAILURE;
04563 }
04564 rtcpdebugaddr.sin_family = AF_INET;
04565 memcpy(&rtcpdebugaddr.sin_addr, hp->h_addr, sizeof(rtcpdebugaddr.sin_addr));
04566 rtcpdebugaddr.sin_port = htons(port);
04567 if (port == 0)
04568 ast_cli(a->fd, "RTCP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtcpdebugaddr.sin_addr));
04569 else
04570 ast_cli(a->fd, "RTCP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtcpdebugaddr.sin_addr), port);
04571 rtcpdebug = 1;
04572 return CLI_SUCCESS;
04573 }
04574
04575 static char *handle_cli_rtp_debug_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04576 {
04577 switch (cmd) {
04578 case CLI_INIT:
04579 e->command = "rtp debug [off|ip]";
04580 e->usage =
04581 "Usage: rtp debug [off]|[ip host[:port]]\n"
04582 " Enable/Disable dumping of all RTP packets. If 'ip' is\n"
04583 " specified, limit the dumped packets to those to and from\n"
04584 " the specified 'host' with optional port.\n";
04585 return NULL;
04586 case CLI_GENERATE:
04587 return NULL;
04588 }
04589
04590 if (a->argc < 2 || a->argc > 4)
04591 return CLI_SHOWUSAGE;
04592 if (a->argc == 2) {
04593 rtpdebug = 1;
04594 memset(&rtpdebugaddr, 0, sizeof(rtpdebugaddr));
04595 ast_cli(a->fd, "RTP Debugging Enabled\n");
04596 } else if (a->argc == 3) {
04597 if (strncasecmp(a->argv[2], "off", 3))
04598 return CLI_SHOWUSAGE;
04599 rtpdebug = 0;
04600 ast_cli(a->fd, "RTP Debugging Disabled\n");
04601 } else {
04602 if (strncasecmp(a->argv[2], "ip", 2))
04603 return CLI_SHOWUSAGE;
04604 return rtp_do_debug_ip(a, 1);
04605 }
04606
04607 return CLI_SUCCESS;
04608 }
04609
04610 static char *handle_cli_rtp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04611 {
04612 switch (cmd) {
04613 case CLI_INIT:
04614 e->command = "rtp set debug {on|off|ip}";
04615 e->usage =
04616 "Usage: rtp set debug {on|off|ip host[:port]}\n"
04617 " Enable/Disable dumping of all RTP packets. If 'ip' is\n"
04618 " specified, limit the dumped packets to those to and from\n"
04619 " the specified 'host' with optional port.\n";
04620 return NULL;
04621 case CLI_GENERATE:
04622 return NULL;
04623 }
04624
04625 if (a->argc == e->args) {
04626 if (!strncasecmp(a->argv[e->args-1], "on", 2)) {
04627 rtpdebug = 1;
04628 memset(&rtpdebugaddr, 0, sizeof(rtpdebugaddr));
04629 ast_cli(a->fd, "RTP Debugging Enabled\n");
04630 return CLI_SUCCESS;
04631 } else if (!strncasecmp(a->argv[e->args-1], "off", 3)) {
04632 rtpdebug = 0;
04633 ast_cli(a->fd, "RTP Debugging Disabled\n");
04634 return CLI_SUCCESS;
04635 }
04636 } else if (a->argc == e->args +1) {
04637 return rtp_do_debug_ip(a, 0);
04638 }
04639
04640 return CLI_SHOWUSAGE;
04641 }
04642
04643 static char *handle_cli_rtcp_debug_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04644 {
04645 switch (cmd) {
04646 case CLI_INIT:
04647 e->command = "rtcp debug [off|ip]";
04648 e->usage =
04649 "Usage: rtcp debug [off]|[ip host[:port]]\n"
04650 " Enable/Disable dumping of all RTCP packets. If 'ip' is\n"
04651 " specified, limit the dumped packets to those to and from\n"
04652 " the specified 'host' with optional port.\n";
04653 return NULL;
04654 case CLI_GENERATE:
04655 return NULL;
04656 }
04657
04658 if (a->argc < 2 || a->argc > 4)
04659 return CLI_SHOWUSAGE;
04660 if (a->argc == 2) {
04661 rtcpdebug = 1;
04662 memset(&rtcpdebugaddr, 0, sizeof(rtcpdebugaddr));
04663 ast_cli(a->fd, "RTCP Debugging Enabled\n");
04664 } else if (a->argc == 3) {
04665 if (strncasecmp(a->argv[2], "off", 3))
04666 return CLI_SHOWUSAGE;
04667 rtcpdebug = 0;
04668 ast_cli(a->fd, "RTCP Debugging Disabled\n");
04669 } else {
04670 if (strncasecmp(a->argv[2], "ip", 2))
04671 return CLI_SHOWUSAGE;
04672 return rtcp_do_debug_ip(a);
04673 }
04674
04675 return CLI_SUCCESS;
04676 }
04677
04678 static char *handle_cli_rtcp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04679 {
04680 switch (cmd) {
04681 case CLI_INIT:
04682 e->command = "rtcp set debug {on|off|ip}";
04683 e->usage =
04684 "Usage: rtcp set debug {on|off|ip host[:port]}\n"
04685 " Enable/Disable dumping of all RTCP packets. If 'ip' is\n"
04686 " specified, limit the dumped packets to those to and from\n"
04687 " the specified 'host' with optional port.\n";
04688 return NULL;
04689 case CLI_GENERATE:
04690 return NULL;
04691 }
04692
04693 if (a->argc == e->args) {
04694 if (!strncasecmp(a->argv[e->args-1], "on", 2)) {
04695 rtcpdebug = 1;
04696 memset(&rtcpdebugaddr, 0, sizeof(rtcpdebugaddr));
04697 ast_cli(a->fd, "RTCP Debugging Enabled\n");
04698 return CLI_SUCCESS;
04699 } else if (!strncasecmp(a->argv[e->args-1], "off", 3)) {
04700 rtcpdebug = 0;
04701 ast_cli(a->fd, "RTCP Debugging Disabled\n");
04702 return CLI_SUCCESS;
04703 }
04704 } else if (a->argc == e->args +1) {
04705 return rtcp_do_debug_ip(a);
04706 }
04707
04708 return CLI_SHOWUSAGE;
04709 }
04710
04711 static char *handle_cli_rtcp_stats_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04712 {
04713 switch (cmd) {
04714 case CLI_INIT:
04715 e->command = "rtcp stats [off]";
04716 e->usage =
04717 "Usage: rtcp stats [off]\n"
04718 " Enable/Disable dumping of RTCP stats.\n";
04719 return NULL;
04720 case CLI_GENERATE:
04721 return NULL;
04722 }
04723
04724 if (a->argc < 2 || a->argc > 3)
04725 return CLI_SHOWUSAGE;
04726 if (a->argc == 3 && strncasecmp(a->argv[2], "off", 3))
04727 return CLI_SHOWUSAGE;
04728
04729 rtcpstats = (a->argc == 3) ? 0 : 1;
04730 ast_cli(a->fd, "RTCP Stats %s\n", rtcpstats ? "Enabled" : "Disabled");
04731 return CLI_SUCCESS;
04732 }
04733
04734 static char *handle_cli_rtcp_set_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04735 {
04736 switch (cmd) {
04737 case CLI_INIT:
04738 e->command = "rtcp set stats {on|off}";
04739 e->usage =
04740 "Usage: rtcp set stats {on|off}\n"
04741 " Enable/Disable dumping of RTCP stats.\n";
04742 return NULL;
04743 case CLI_GENERATE:
04744 return NULL;
04745 }
04746
04747 if (a->argc != e->args)
04748 return CLI_SHOWUSAGE;
04749
04750 if (!strncasecmp(a->argv[e->args-1], "on", 2))
04751 rtcpstats = 1;
04752 else if (!strncasecmp(a->argv[e->args-1], "off", 3))
04753 rtcpstats = 0;
04754 else
04755 return CLI_SHOWUSAGE;
04756
04757 ast_cli(a->fd, "RTCP Stats %s\n", rtcpstats ? "Enabled" : "Disabled");
04758 return CLI_SUCCESS;
04759 }
04760
04761 static char *handle_cli_stun_debug_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04762 {
04763 switch (cmd) {
04764 case CLI_INIT:
04765 e->command = "stun debug [off]";
04766 e->usage =
04767 "Usage: stun debug [off]\n"
04768 " Enable/Disable STUN (Simple Traversal of UDP through NATs)\n"
04769 " debugging\n";
04770 return NULL;
04771 case CLI_GENERATE:
04772 return NULL;
04773 }
04774
04775 if (a->argc < 2 || a->argc > 3)
04776 return CLI_SHOWUSAGE;
04777 if (a->argc == 3 && strncasecmp(a->argv[2], "off", 3))
04778 return CLI_SHOWUSAGE;
04779
04780 stundebug = (a->argc == 3) ? 0 : 1;
04781 ast_cli(a->fd, "STUN Debugging %s\n", stundebug ? "Enabled" : "Disabled");
04782 return CLI_SUCCESS;
04783 }
04784
04785 static char *handle_cli_stun_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04786 {
04787 switch (cmd) {
04788 case CLI_INIT:
04789 e->command = "stun set debug {on|off}";
04790 e->usage =
04791 "Usage: stun set debug {on|off}\n"
04792 " Enable/Disable STUN (Simple Traversal of UDP through NATs)\n"
04793 " debugging\n";
04794 return NULL;
04795 case CLI_GENERATE:
04796 return NULL;
04797 }
04798
04799 if (a->argc != e->args)
04800 return CLI_SHOWUSAGE;
04801
04802 if (!strncasecmp(a->argv[e->args-1], "on", 2))
04803 stundebug = 1;
04804 else if (!strncasecmp(a->argv[e->args-1], "off", 3))
04805 stundebug = 0;
04806 else
04807 return CLI_SHOWUSAGE;
04808
04809 ast_cli(a->fd, "STUN Debugging %s\n", stundebug ? "Enabled" : "Disabled");
04810 return CLI_SUCCESS;
04811 }
04812
04813 static struct ast_cli_entry cli_rtp_debug_deprecated = AST_CLI_DEFINE(handle_cli_rtp_debug_deprecated, "Enable/Disable RTP debugging");
04814 static struct ast_cli_entry cli_rtcp_debug_deprecated = AST_CLI_DEFINE(handle_cli_rtcp_debug_deprecated, "Enable/Disable RTCP debugging");
04815 static struct ast_cli_entry cli_rtcp_stats_deprecated = AST_CLI_DEFINE(handle_cli_rtcp_stats_deprecated, "Enable/Disable RTCP stats");
04816 static struct ast_cli_entry cli_stun_debug_deprecated = AST_CLI_DEFINE(handle_cli_stun_debug_deprecated, "Enable/Disable STUN debugging");
04817
04818 static struct ast_cli_entry cli_rtp[] = {
04819 AST_CLI_DEFINE(handle_cli_rtp_set_debug, "Enable/Disable RTP debugging", .deprecate_cmd = &cli_rtp_debug_deprecated),
04820 AST_CLI_DEFINE(handle_cli_rtcp_set_debug, "Enable/Disable RTCP debugging", .deprecate_cmd = &cli_rtcp_debug_deprecated),
04821 AST_CLI_DEFINE(handle_cli_rtcp_set_stats, "Enable/Disable RTCP stats", .deprecate_cmd = &cli_rtcp_stats_deprecated),
04822 AST_CLI_DEFINE(handle_cli_stun_set_debug, "Enable/Disable STUN debugging", .deprecate_cmd = &cli_stun_debug_deprecated),
04823 };
04824
04825 static int __ast_rtp_reload(int reload)
04826 {
04827 struct ast_config *cfg;
04828 const char *s;
04829 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
04830
04831 if ((cfg = ast_config_load2("rtp.conf", "rtp", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
04832 return 0;
04833
04834 rtpstart = 5000;
04835 rtpend = 31000;
04836 dtmftimeout = DEFAULT_DTMF_TIMEOUT;
04837 strictrtp = STRICT_RTP_OPEN;
04838 if (cfg) {
04839 if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
04840 rtpstart = atoi(s);
04841 if (rtpstart < 1024)
04842 rtpstart = 1024;
04843 if (rtpstart > 65535)
04844 rtpstart = 65535;
04845 }
04846 if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
04847 rtpend = atoi(s);
04848 if (rtpend < 1024)
04849 rtpend = 1024;
04850 if (rtpend > 65535)
04851 rtpend = 65535;
04852 }
04853 if ((s = ast_variable_retrieve(cfg, "general", "rtcpinterval"))) {
04854 rtcpinterval = atoi(s);
04855 if (rtcpinterval == 0)
04856 rtcpinterval = 0;
04857 if (rtcpinterval < RTCP_MIN_INTERVALMS)
04858 rtcpinterval = RTCP_MIN_INTERVALMS;
04859 if (rtcpinterval > RTCP_MAX_INTERVALMS)
04860 rtcpinterval = RTCP_MAX_INTERVALMS;
04861 }
04862 if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
04863 #ifdef SO_NO_CHECK
04864 if (ast_false(s))
04865 nochecksums = 1;
04866 else
04867 nochecksums = 0;
04868 #else
04869 if (ast_false(s))
04870 ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
04871 #endif
04872 }
04873 if ((s = ast_variable_retrieve(cfg, "general", "dtmftimeout"))) {
04874 dtmftimeout = atoi(s);
04875 if ((dtmftimeout < 0) || (dtmftimeout > 64000)) {
04876 ast_log(LOG_WARNING, "DTMF timeout of '%d' outside range, using default of '%d' instead\n",
04877 dtmftimeout, DEFAULT_DTMF_TIMEOUT);
04878 dtmftimeout = DEFAULT_DTMF_TIMEOUT;
04879 };
04880 }
04881 if ((s = ast_variable_retrieve(cfg, "general", "strictrtp"))) {
04882 strictrtp = ast_true(s);
04883 }
04884 ast_config_destroy(cfg);
04885 }
04886 if (rtpstart >= rtpend) {
04887 ast_log(LOG_WARNING, "Unreasonable values for RTP start/end port in rtp.conf\n");
04888 rtpstart = 5000;
04889 rtpend = 31000;
04890 }
04891 ast_verb(2, "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
04892 return 0;
04893 }
04894
04895 int ast_rtp_reload(void)
04896 {
04897 return __ast_rtp_reload(1);
04898 }
04899
04900
04901 void ast_rtp_init(void)
04902 {
04903 ast_cli_register_multiple(cli_rtp, sizeof(cli_rtp) / sizeof(struct ast_cli_entry));
04904 __ast_rtp_reload(0);
04905 }
04906
04907
04908
04909
04910 static int red_write(const void *data)
04911 {
04912 struct ast_rtp *rtp = (struct ast_rtp*) data;
04913
04914 ast_rtp_write(rtp, &rtp->red->t140);
04915
04916 return 1;
04917 }
04918
04919
04920
04921
04922 static struct ast_frame *red_t140_to_red(struct rtp_red *red) {
04923 unsigned char *data = red->t140red.data.ptr;
04924 int len = 0;
04925 int i;
04926
04927
04928 if (red->len[0]) {
04929 for (i = 1; i < red->num_gen+1; i++)
04930 len += red->len[i];
04931
04932 memmove(&data[red->hdrlen], &data[red->hdrlen+red->len[0]], len);
04933 }
04934
04935
04936 for (i = 0; i < red->num_gen; i++)
04937 red->len[i] = red->len[i+1];
04938 red->len[i] = red->t140.datalen;
04939
04940
04941 len = red->hdrlen;
04942 for (i = 0; i < red->num_gen; i++)
04943 len += data[i*4+3] = red->len[i];
04944
04945
04946 memcpy(&data[len], red->t140.data.ptr, red->t140.datalen);
04947 red->t140red.datalen = len + red->t140.datalen;
04948
04949
04950 if (len == red->hdrlen && !red->t140.datalen)
04951 return NULL;
04952
04953
04954 red->t140.datalen = 0;
04955
04956 return &red->t140red;
04957 }
04958
04959
04960
04961
04962
04963
04964
04965
04966 int rtp_red_init(struct ast_rtp *rtp, int ti, int *red_data_pt, int num_gen)
04967 {
04968 struct rtp_red *r;
04969 int x;
04970
04971 if (!(r = ast_calloc(1, sizeof(struct rtp_red))))
04972 return -1;
04973
04974 r->t140.frametype = AST_FRAME_TEXT;
04975 r->t140.subclass = AST_FORMAT_T140RED;
04976 r->t140.data.ptr = &r->buf_data;
04977
04978 r->t140.ts = 0;
04979 r->t140red = r->t140;
04980 r->t140red.data.ptr = &r->t140red_data;
04981 r->t140red.datalen = 0;
04982 r->ti = ti;
04983 r->num_gen = num_gen;
04984 r->hdrlen = num_gen * 4 + 1;
04985 r->prev_ts = 0;
04986
04987 for (x = 0; x < num_gen; x++) {
04988 r->pt[x] = red_data_pt[x];
04989 r->pt[x] |= 1 << 7;
04990 r->t140red_data[x*4] = r->pt[x];
04991 }
04992 r->t140red_data[x*4] = r->pt[x] = red_data_pt[x];
04993 r->schedid = ast_sched_add(rtp->sched, ti, red_write, rtp);
04994 rtp->red = r;
04995
04996 r->t140.datalen = 0;
04997
04998 return 0;
04999 }
05000
05001
05002
05003
05004
05005 void red_buffer_t140(struct ast_rtp *rtp, struct ast_frame *f)
05006 {
05007 if (f->datalen > -1) {
05008 struct rtp_red *red = rtp->red;
05009 memcpy(&red->buf_data[red->t140.datalen], f->data.ptr, f->datalen);
05010 red->t140.datalen += f->datalen;
05011 red->t140.ts = f->ts;
05012 }
05013 }
05014