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