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$")
00032
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <sys/time.h>
00037 #include <signal.h>
00038 #include <errno.h>
00039 #include <unistd.h>
00040 #include <netinet/in.h>
00041 #include <sys/time.h>
00042 #include <sys/socket.h>
00043 #include <arpa/inet.h>
00044 #include <fcntl.h>
00045
00046 #include "asterisk/rtp.h"
00047 #include "asterisk/frame.h"
00048 #include "asterisk/logger.h"
00049 #include "asterisk/options.h"
00050 #include "asterisk/channel.h"
00051 #include "asterisk/acl.h"
00052 #include "asterisk/channel.h"
00053 #include "asterisk/config.h"
00054 #include "asterisk/lock.h"
00055 #include "asterisk/utils.h"
00056 #include "asterisk/cli.h"
00057 #include "asterisk/unaligned.h"
00058 #include "asterisk/utils.h"
00059
00060 #define MAX_TIMESTAMP_SKEW 640
00061
00062 #define RTP_SEQ_MOD (1<<16)
00063 #define RTCP_DEFAULT_INTERVALMS 5000
00064 #define RTCP_MIN_INTERVALMS 500
00065 #define RTCP_MAX_INTERVALMS 60000
00066
00067 #define RTCP_PT_FUR 192
00068 #define RTCP_PT_SR 200
00069 #define RTCP_PT_RR 201
00070 #define RTCP_PT_SDES 202
00071 #define RTCP_PT_BYE 203
00072 #define RTCP_PT_APP 204
00073
00074 #define RTP_MTU 1200
00075
00076 #define DEFAULT_DTMF_TIMEOUT 3000
00077
00078 static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
00079
00080 static int rtpstart;
00081 static int rtpend;
00082 static int rtpdebug;
00083 static int rtcpdebug;
00084 static int rtcpstats;
00085 static int rtcpinterval = RTCP_DEFAULT_INTERVALMS;
00086 static int stundebug;
00087 static struct sockaddr_in rtpdebugaddr;
00088 static struct sockaddr_in rtcpdebugaddr;
00089 #ifdef SO_NO_CHECK
00090 static int nochecksums;
00091 #endif
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 struct rtpPayloadType {
00104 int isAstFormat;
00105 int code;
00106 };
00107
00108
00109
00110 struct ast_rtp {
00111 int s;
00112 struct ast_frame f;
00113 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
00114 unsigned int ssrc;
00115 unsigned int themssrc;
00116 unsigned int rxssrc;
00117 unsigned int lastts;
00118 unsigned int lastrxts;
00119 unsigned int lastividtimestamp;
00120 unsigned int lastovidtimestamp;
00121 unsigned int lasteventseqn;
00122 int lastrxseqno;
00123 unsigned short seedrxseqno;
00124 unsigned int seedrxts;
00125 unsigned int rxcount;
00126 unsigned int rxoctetcount;
00127 unsigned int txcount;
00128 unsigned int txoctetcount;
00129 unsigned int cycles;
00130 double rxjitter;
00131 double rxtransit;
00132 int lasttxformat;
00133 int lastrxformat;
00134
00135 int rtptimeout;
00136 int rtpholdtimeout;
00137 int rtpkeepalive;
00138
00139
00140 char resp;
00141 unsigned int lastevent;
00142 int dtmfcount;
00143 unsigned int dtmfsamples;
00144
00145 unsigned int lastdigitts;
00146 char sending_digit;
00147 char send_digit;
00148 int send_payload;
00149 int send_duration;
00150 int nat;
00151 unsigned int flags;
00152 struct sockaddr_in us;
00153 struct sockaddr_in them;
00154 struct timeval rxcore;
00155 struct timeval txcore;
00156 double drxcore;
00157 struct timeval lastrx;
00158 struct timeval dtmfmute;
00159 struct ast_smoother *smoother;
00160 int *ioid;
00161 unsigned short seqno;
00162 unsigned short rxseqno;
00163 struct sched_context *sched;
00164 struct io_context *io;
00165 void *data;
00166 ast_rtp_callback callback;
00167 ast_mutex_t bridge_lock;
00168 struct rtpPayloadType current_RTP_PT[MAX_RTP_PT];
00169 int rtp_lookup_code_cache_isAstFormat;
00170 int rtp_lookup_code_cache_code;
00171 int rtp_lookup_code_cache_result;
00172 struct ast_rtcp *rtcp;
00173 struct ast_codec_pref pref;
00174 struct ast_rtp *bridged;
00175 int set_marker_bit:1;
00176 };
00177
00178
00179 static int ast_rtcp_write(const void *data);
00180 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw);
00181 static int ast_rtcp_write_sr(const void *data);
00182 static int ast_rtcp_write_rr(const void *data);
00183 static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp);
00184 static int ast_rtp_senddigit_continuation(struct ast_rtp *rtp);
00185 int ast_rtp_senddigit_end(struct ast_rtp *rtp, char digit);
00186
00187 #define FLAG_3389_WARNING (1 << 0)
00188 #define FLAG_NAT_ACTIVE (3 << 1)
00189 #define FLAG_NAT_INACTIVE (0 << 1)
00190 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
00191 #define FLAG_HAS_DTMF (1 << 3)
00192 #define FLAG_P2P_SENT_MARK (1 << 4)
00193 #define FLAG_P2P_NEED_DTMF (1 << 5)
00194 #define FLAG_CALLBACK_MODE (1 << 6)
00195 #define FLAG_DTMF_COMPENSATE (1 << 7)
00196 #define FLAG_HAS_STUN (1 << 8)
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 struct ast_rtcp {
00209 int s;
00210 struct sockaddr_in us;
00211 struct sockaddr_in them;
00212 unsigned int soc;
00213 unsigned int spc;
00214 unsigned int themrxlsr;
00215 struct timeval rxlsr;
00216 struct timeval txlsr;
00217 unsigned int expected_prior;
00218 unsigned int received_prior;
00219 int schedid;
00220 unsigned int rr_count;
00221 unsigned int sr_count;
00222 unsigned int lastsrtxcount;
00223 double accumulated_transit;
00224 double rtt;
00225 unsigned int reported_jitter;
00226 unsigned int reported_lost;
00227 char quality[AST_MAX_USER_FIELD];
00228 double maxrxjitter;
00229 double minrxjitter;
00230 double maxrtt;
00231 double minrtt;
00232 int sendfur;
00233 };
00234
00235
00236 typedef struct { unsigned int id[4]; } __attribute__((packed)) stun_trans_id;
00237
00238
00239 struct stun_header {
00240 unsigned short msgtype;
00241 unsigned short msglen;
00242 stun_trans_id id;
00243 unsigned char ies[0];
00244 } __attribute__((packed));
00245
00246 struct stun_attr {
00247 unsigned short attr;
00248 unsigned short len;
00249 unsigned char value[0];
00250 } __attribute__((packed));
00251
00252 struct stun_addr {
00253 unsigned char unused;
00254 unsigned char family;
00255 unsigned short port;
00256 unsigned int addr;
00257 } __attribute__((packed));
00258
00259 #define STUN_IGNORE (0)
00260 #define STUN_ACCEPT (1)
00261
00262 #define STUN_BINDREQ 0x0001
00263 #define STUN_BINDRESP 0x0101
00264 #define STUN_BINDERR 0x0111
00265 #define STUN_SECREQ 0x0002
00266 #define STUN_SECRESP 0x0102
00267 #define STUN_SECERR 0x0112
00268
00269 #define STUN_MAPPED_ADDRESS 0x0001
00270 #define STUN_RESPONSE_ADDRESS 0x0002
00271 #define STUN_CHANGE_REQUEST 0x0003
00272 #define STUN_SOURCE_ADDRESS 0x0004
00273 #define STUN_CHANGED_ADDRESS 0x0005
00274 #define STUN_USERNAME 0x0006
00275 #define STUN_PASSWORD 0x0007
00276 #define STUN_MESSAGE_INTEGRITY 0x0008
00277 #define STUN_ERROR_CODE 0x0009
00278 #define STUN_UNKNOWN_ATTRIBUTES 0x000a
00279 #define STUN_REFLECTED_FROM 0x000b
00280
00281 static const char *stun_msg2str(int msg)
00282 {
00283 switch(msg) {
00284 case STUN_BINDREQ:
00285 return "Binding Request";
00286 case STUN_BINDRESP:
00287 return "Binding Response";
00288 case STUN_BINDERR:
00289 return "Binding Error Response";
00290 case STUN_SECREQ:
00291 return "Shared Secret Request";
00292 case STUN_SECRESP:
00293 return "Shared Secret Response";
00294 case STUN_SECERR:
00295 return "Shared Secret Error Response";
00296 }
00297 return "Non-RFC3489 Message";
00298 }
00299
00300 static const char *stun_attr2str(int msg)
00301 {
00302 switch(msg) {
00303 case STUN_MAPPED_ADDRESS:
00304 return "Mapped Address";
00305 case STUN_RESPONSE_ADDRESS:
00306 return "Response Address";
00307 case STUN_CHANGE_REQUEST:
00308 return "Change Request";
00309 case STUN_SOURCE_ADDRESS:
00310 return "Source Address";
00311 case STUN_CHANGED_ADDRESS:
00312 return "Changed Address";
00313 case STUN_USERNAME:
00314 return "Username";
00315 case STUN_PASSWORD:
00316 return "Password";
00317 case STUN_MESSAGE_INTEGRITY:
00318 return "Message Integrity";
00319 case STUN_ERROR_CODE:
00320 return "Error Code";
00321 case STUN_UNKNOWN_ATTRIBUTES:
00322 return "Unknown Attributes";
00323 case STUN_REFLECTED_FROM:
00324 return "Reflected From";
00325 }
00326 return "Non-RFC3489 Attribute";
00327 }
00328
00329 struct stun_state {
00330 const char *username;
00331 const char *password;
00332 };
00333
00334 static int stun_process_attr(struct stun_state *state, struct stun_attr *attr)
00335 {
00336 if (stundebug)
00337 ast_verbose("Found STUN Attribute %s (%04x), length %d\n",
00338 stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
00339 switch(ntohs(attr->attr)) {
00340 case STUN_USERNAME:
00341 state->username = (const char *) (attr->value);
00342 break;
00343 case STUN_PASSWORD:
00344 state->password = (const char *) (attr->value);
00345 break;
00346 default:
00347 if (stundebug)
00348 ast_verbose("Ignoring STUN attribute %s (%04x), length %d\n",
00349 stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
00350 }
00351 return 0;
00352 }
00353
00354 static void append_attr_string(struct stun_attr **attr, int attrval, const char *s, int *len, int *left)
00355 {
00356 int size = sizeof(**attr) + strlen(s);
00357 if (*left > size) {
00358 (*attr)->attr = htons(attrval);
00359 (*attr)->len = htons(strlen(s));
00360 memcpy((*attr)->value, s, strlen(s));
00361 (*attr) = (struct stun_attr *)((*attr)->value + strlen(s));
00362 *len += size;
00363 *left -= size;
00364 }
00365 }
00366
00367 static void append_attr_address(struct stun_attr **attr, int attrval, struct sockaddr_in *sin, int *len, int *left)
00368 {
00369 int size = sizeof(**attr) + 8;
00370 struct stun_addr *addr;
00371 if (*left > size) {
00372 (*attr)->attr = htons(attrval);
00373 (*attr)->len = htons(8);
00374 addr = (struct stun_addr *)((*attr)->value);
00375 addr->unused = 0;
00376 addr->family = 0x01;
00377 addr->port = sin->sin_port;
00378 addr->addr = sin->sin_addr.s_addr;
00379 (*attr) = (struct stun_attr *)((*attr)->value + 8);
00380 *len += size;
00381 *left -= size;
00382 }
00383 }
00384
00385 static int stun_send(int s, struct sockaddr_in *dst, struct stun_header *resp)
00386 {
00387 return sendto(s, resp, ntohs(resp->msglen) + sizeof(*resp), 0,
00388 (struct sockaddr *)dst, sizeof(*dst));
00389 }
00390
00391 static void stun_req_id(struct stun_header *req)
00392 {
00393 int x;
00394 for (x=0;x<4;x++)
00395 req->id.id[x] = ast_random();
00396 }
00397
00398 size_t ast_rtp_alloc_size(void)
00399 {
00400 return sizeof(struct ast_rtp);
00401 }
00402
00403 void ast_rtp_stun_request(struct ast_rtp *rtp, struct sockaddr_in *suggestion, const char *username)
00404 {
00405 struct stun_header *req;
00406 unsigned char reqdata[1024];
00407 int reqlen, reqleft;
00408 struct stun_attr *attr;
00409
00410 req = (struct stun_header *)reqdata;
00411 stun_req_id(req);
00412 reqlen = 0;
00413 reqleft = sizeof(reqdata) - sizeof(struct stun_header);
00414 req->msgtype = 0;
00415 req->msglen = 0;
00416 attr = (struct stun_attr *)req->ies;
00417 if (username)
00418 append_attr_string(&attr, STUN_USERNAME, username, &reqlen, &reqleft);
00419 req->msglen = htons(reqlen);
00420 req->msgtype = htons(STUN_BINDREQ);
00421 stun_send(rtp->s, suggestion, req);
00422 }
00423
00424 static int stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data, size_t len)
00425 {
00426 struct stun_header *resp, *hdr = (struct stun_header *)data;
00427 struct stun_attr *attr;
00428 struct stun_state st;
00429 int ret = STUN_IGNORE;
00430 unsigned char respdata[1024];
00431 int resplen, respleft;
00432
00433 if (len < sizeof(struct stun_header)) {
00434 if (option_debug)
00435 ast_log(LOG_DEBUG, "Runt STUN packet (only %zd, wanting at least %zd)\n", len, sizeof(struct stun_header));
00436 return -1;
00437 }
00438 if (stundebug)
00439 ast_verbose("STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr->msgtype)), ntohs(hdr->msgtype), ntohs(hdr->msglen));
00440 if (ntohs(hdr->msglen) > len - sizeof(struct stun_header)) {
00441 if (option_debug)
00442 ast_log(LOG_DEBUG, "Scrambled STUN packet length (got %d, expecting %zd)\n", ntohs(hdr->msglen), len - sizeof(struct stun_header));
00443 } else
00444 len = ntohs(hdr->msglen);
00445 data += sizeof(struct stun_header);
00446 memset(&st, 0, sizeof(st));
00447 while(len) {
00448 if (len < sizeof(struct stun_attr)) {
00449 if (option_debug)
00450 ast_log(LOG_DEBUG, "Runt Attribute (got %zd, expecting %zd)\n", len, sizeof(struct stun_attr));
00451 break;
00452 }
00453 attr = (struct stun_attr *)data;
00454 if ((ntohs(attr->len) + sizeof(struct stun_attr)) > len) {
00455 if (option_debug)
00456 ast_log(LOG_DEBUG, "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", (int) (ntohs(attr->len) + sizeof(struct stun_attr)), (int) len);
00457 break;
00458 }
00459 if (stun_process_attr(&st, attr)) {
00460 if (option_debug)
00461 ast_log(LOG_DEBUG, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr));
00462 break;
00463 }
00464
00465 attr->attr = 0;
00466 data += ntohs(attr->len) + sizeof(struct stun_attr);
00467 len -= ntohs(attr->len) + sizeof(struct stun_attr);
00468 }
00469
00470 *data = '\0';
00471 resp = (struct stun_header *)respdata;
00472 resplen = 0;
00473 respleft = sizeof(respdata) - sizeof(struct stun_header);
00474 resp->id = hdr->id;
00475 resp->msgtype = 0;
00476 resp->msglen = 0;
00477 attr = (struct stun_attr *)resp->ies;
00478 if (!len) {
00479 switch(ntohs(hdr->msgtype)) {
00480 case STUN_BINDREQ:
00481 if (stundebug)
00482 ast_verbose("STUN Bind Request, username: %s\n",
00483 st.username ? st.username : "<none>");
00484 if (st.username)
00485 append_attr_string(&attr, STUN_USERNAME, st.username, &resplen, &respleft);
00486 append_attr_address(&attr, STUN_MAPPED_ADDRESS, src, &resplen, &respleft);
00487 resp->msglen = htons(resplen);
00488 resp->msgtype = htons(STUN_BINDRESP);
00489 stun_send(s, src, resp);
00490 ret = STUN_ACCEPT;
00491 break;
00492 default:
00493 if (stundebug)
00494 ast_verbose("Dunno what to do with STUN message %04x (%s)\n", ntohs(hdr->msgtype), stun_msg2str(ntohs(hdr->msgtype)));
00495 }
00496 }
00497 return ret;
00498 }
00499
00500
00501 static AST_LIST_HEAD_STATIC(protos, ast_rtp_protocol);
00502
00503 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
00504 {
00505 unsigned int sec, usec, frac;
00506 sec = tv.tv_sec + 2208988800u;
00507 usec = tv.tv_usec;
00508 frac = (usec << 12) + (usec << 8) - ((usec * 3650) >> 6);
00509 *msw = sec;
00510 *lsw = frac;
00511 }
00512
00513 int ast_rtp_fd(struct ast_rtp *rtp)
00514 {
00515 return rtp->s;
00516 }
00517
00518 int ast_rtcp_fd(struct ast_rtp *rtp)
00519 {
00520 if (rtp->rtcp)
00521 return rtp->rtcp->s;
00522 return -1;
00523 }
00524
00525 unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
00526 {
00527 unsigned int interval;
00528
00529
00530 interval = rtcpinterval;
00531 return interval;
00532 }
00533
00534
00535 void ast_rtp_set_rtptimers_onhold(struct ast_rtp *rtp)
00536 {
00537 rtp->rtptimeout = (-1) * rtp->rtptimeout;
00538 rtp->rtpholdtimeout = (-1) * rtp->rtpholdtimeout;
00539 }
00540
00541
00542 void ast_rtp_set_rtptimeout(struct ast_rtp *rtp, int timeout)
00543 {
00544 rtp->rtptimeout = timeout;
00545 }
00546
00547
00548 void ast_rtp_set_rtpholdtimeout(struct ast_rtp *rtp, int timeout)
00549 {
00550 rtp->rtpholdtimeout = timeout;
00551 }
00552
00553
00554 void ast_rtp_set_rtpkeepalive(struct ast_rtp *rtp, int period)
00555 {
00556 rtp->rtpkeepalive = period;
00557 }
00558
00559
00560 int ast_rtp_get_rtptimeout(struct ast_rtp *rtp)
00561 {
00562 if (rtp->rtptimeout < 0)
00563 return 0;
00564 return rtp->rtptimeout;
00565 }
00566
00567
00568 int ast_rtp_get_rtpholdtimeout(struct ast_rtp *rtp)
00569 {
00570 if (rtp->rtptimeout < 0)
00571 return 0;
00572 return rtp->rtpholdtimeout;
00573 }
00574
00575
00576 int ast_rtp_get_rtpkeepalive(struct ast_rtp *rtp)
00577 {
00578 return rtp->rtpkeepalive;
00579 }
00580
00581 void ast_rtp_set_data(struct ast_rtp *rtp, void *data)
00582 {
00583 rtp->data = data;
00584 }
00585
00586 void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback)
00587 {
00588 rtp->callback = callback;
00589 }
00590
00591 void ast_rtp_setnat(struct ast_rtp *rtp, int nat)
00592 {
00593 rtp->nat = nat;
00594 }
00595
00596 int ast_rtp_getnat(struct ast_rtp *rtp)
00597 {
00598 return ast_test_flag(rtp, FLAG_NAT_ACTIVE);
00599 }
00600
00601 void ast_rtp_setdtmf(struct ast_rtp *rtp, int dtmf)
00602 {
00603 ast_set2_flag(rtp, dtmf ? 1 : 0, FLAG_HAS_DTMF);
00604 }
00605
00606 void ast_rtp_setdtmfcompensate(struct ast_rtp *rtp, int compensate)
00607 {
00608 ast_set2_flag(rtp, compensate ? 1 : 0, FLAG_DTMF_COMPENSATE);
00609 }
00610
00611 void ast_rtp_setstun(struct ast_rtp *rtp, int stun_enable)
00612 {
00613 ast_set2_flag(rtp, stun_enable ? 1 : 0, FLAG_HAS_STUN);
00614 }
00615
00616 static struct ast_frame *send_dtmf(struct ast_rtp *rtp, enum ast_frame_type type)
00617 {
00618 if (((ast_test_flag(rtp, FLAG_DTMF_COMPENSATE) && type == AST_FRAME_DTMF_END) ||
00619 (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
00620 if (option_debug)
00621 ast_log(LOG_DEBUG, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(rtp->them.sin_addr));
00622 rtp->resp = 0;
00623 rtp->dtmfsamples = 0;
00624 return &ast_null_frame;
00625 }
00626 if (option_debug)
00627 ast_log(LOG_DEBUG, "Sending dtmf: %d (%c), at %s\n", rtp->resp, rtp->resp, ast_inet_ntoa(rtp->them.sin_addr));
00628 if (rtp->resp == 'X') {
00629 rtp->f.frametype = AST_FRAME_CONTROL;
00630 rtp->f.subclass = AST_CONTROL_FLASH;
00631 } else {
00632 rtp->f.frametype = type;
00633 rtp->f.subclass = rtp->resp;
00634 }
00635 rtp->f.datalen = 0;
00636 rtp->f.samples = 0;
00637 rtp->f.mallocd = 0;
00638 rtp->f.src = "RTP";
00639 return &rtp->f;
00640
00641 }
00642
00643 static inline int rtp_debug_test_addr(struct sockaddr_in *addr)
00644 {
00645 if (rtpdebug == 0)
00646 return 0;
00647 if (rtpdebugaddr.sin_addr.s_addr) {
00648 if (((ntohs(rtpdebugaddr.sin_port) != 0)
00649 && (rtpdebugaddr.sin_port != addr->sin_port))
00650 || (rtpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
00651 return 0;
00652 }
00653 return 1;
00654 }
00655
00656 static inline int rtcp_debug_test_addr(struct sockaddr_in *addr)
00657 {
00658 if (rtcpdebug == 0)
00659 return 0;
00660 if (rtcpdebugaddr.sin_addr.s_addr) {
00661 if (((ntohs(rtcpdebugaddr.sin_port) != 0)
00662 && (rtcpdebugaddr.sin_port != addr->sin_port))
00663 || (rtcpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
00664 return 0;
00665 }
00666 return 1;
00667 }
00668
00669
00670 static struct ast_frame *process_cisco_dtmf(struct ast_rtp *rtp, unsigned char *data, int len)
00671 {
00672 unsigned int event;
00673 char resp = 0;
00674 struct ast_frame *f = NULL;
00675 event = ntohl(*((unsigned int *)(data)));
00676 event &= 0x001F;
00677 if (option_debug > 2 || rtpdebug)
00678 ast_log(LOG_DEBUG, "Cisco DTMF Digit: %08x (len = %d)\n", event, len);
00679 if (event < 10) {
00680 resp = '0' + event;
00681 } else if (event < 11) {
00682 resp = '*';
00683 } else if (event < 12) {
00684 resp = '#';
00685 } else if (event < 16) {
00686 resp = 'A' + (event - 12);
00687 } else if (event < 17) {
00688 resp = 'X';
00689 }
00690 if (rtp->resp && (rtp->resp != resp)) {
00691 f = send_dtmf(rtp, AST_FRAME_DTMF_END);
00692 }
00693 rtp->resp = resp;
00694 rtp->dtmfcount = dtmftimeout;
00695 return f;
00696 }
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709 static struct ast_frame *process_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp)
00710 {
00711 unsigned int event;
00712 unsigned int event_end;
00713 unsigned int samples;
00714 char resp = 0;
00715 struct ast_frame *f = NULL;
00716
00717
00718 event = ntohl(*((unsigned int *)(data)));
00719 event >>= 24;
00720 event_end = ntohl(*((unsigned int *)(data)));
00721 event_end <<= 8;
00722 event_end >>= 24;
00723 samples = ntohl(*((unsigned int *)(data)));
00724 samples &= 0xFFFF;
00725
00726
00727 if (rtpdebug || option_debug > 2)
00728 ast_log(LOG_DEBUG, "- RTP 2833 Event: %08x (len = %d)\n", event, len);
00729
00730
00731 if (event < 10) {
00732 resp = '0' + event;
00733 } else if (event < 11) {
00734 resp = '*';
00735 } else if (event < 12) {
00736 resp = '#';
00737 } else if (event < 16) {
00738 resp = 'A' + (event - 12);
00739 } else if (event < 17) {
00740 resp = 'X';
00741 } else {
00742
00743 ast_log(LOG_DEBUG, "Ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", event);
00744 return &ast_null_frame;
00745 }
00746
00747 if (ast_test_flag(rtp, FLAG_DTMF_COMPENSATE)) {
00748 if ((rtp->lastevent != timestamp) || (rtp->resp && rtp->resp != resp)) {
00749 rtp->resp = resp;
00750 f = send_dtmf(rtp, AST_FRAME_DTMF_END);
00751 f->len = 0;
00752 rtp->lastevent = timestamp;
00753 }
00754 } else {
00755 if ((!(rtp->resp) && (!(event_end & 0x80))) || (rtp->resp && rtp->resp != resp)) {
00756 rtp->resp = resp;
00757 f = send_dtmf(rtp, AST_FRAME_DTMF_BEGIN);
00758 } else if ((event_end & 0x80) && (rtp->lastevent != seqno) && rtp->resp) {
00759 f = send_dtmf(rtp, AST_FRAME_DTMF_END);
00760 f->len = ast_tvdiff_ms(ast_samp2tv(samples, 8000), ast_tv(0, 0));
00761 rtp->resp = 0;
00762 rtp->lastevent = seqno;
00763 }
00764 }
00765
00766 rtp->dtmfcount = dtmftimeout;
00767 rtp->dtmfsamples = samples;
00768
00769 return f;
00770 }
00771
00772
00773
00774
00775
00776
00777
00778 static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
00779 {
00780 struct ast_frame *f = NULL;
00781
00782
00783
00784 if (rtpdebug)
00785 ast_log(LOG_DEBUG, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", rtp->lastrxformat, len);
00786
00787 if (!(ast_test_flag(rtp, FLAG_3389_WARNING))) {
00788 ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client IP: %s\n",
00789 ast_inet_ntoa(rtp->them.sin_addr));
00790 ast_set_flag(rtp, FLAG_3389_WARNING);
00791 }
00792
00793
00794 if (!len)
00795 return NULL;
00796 if (len < 24) {
00797 rtp->f.data = rtp->rawdata + AST_FRIENDLY_OFFSET;
00798 rtp->f.datalen = len - 1;
00799 rtp->f.offset = AST_FRIENDLY_OFFSET;
00800 memcpy(rtp->f.data, data + 1, len - 1);
00801 } else {
00802 rtp->f.data = NULL;
00803 rtp->f.offset = 0;
00804 rtp->f.datalen = 0;
00805 }
00806 rtp->f.frametype = AST_FRAME_CNG;
00807 rtp->f.subclass = data[0] & 0x7f;
00808 rtp->f.datalen = len - 1;
00809 rtp->f.samples = 0;
00810 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
00811 f = &rtp->f;
00812 return f;
00813 }
00814
00815 static int rtpread(int *id, int fd, short events, void *cbdata)
00816 {
00817 struct ast_rtp *rtp = cbdata;
00818 struct ast_frame *f;
00819 f = ast_rtp_read(rtp);
00820 if (f) {
00821 if (rtp->callback)
00822 rtp->callback(rtp, f, rtp->data);
00823 }
00824 return 1;
00825 }
00826
00827 struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp)
00828 {
00829 socklen_t len;
00830 int position, i, packetwords;
00831 int res;
00832 struct sockaddr_in sin;
00833 unsigned int rtcpdata[8192 + AST_FRIENDLY_OFFSET];
00834 unsigned int *rtcpheader;
00835 int pt;
00836 struct timeval now;
00837 unsigned int length;
00838 int rc;
00839 double rttsec;
00840 uint64_t rtt = 0;
00841 unsigned int dlsr;
00842 unsigned int lsr;
00843 unsigned int msw;
00844 unsigned int lsw;
00845 unsigned int comp;
00846 struct ast_frame *f = &ast_null_frame;
00847
00848 if (!rtp || !rtp->rtcp)
00849 return &ast_null_frame;
00850
00851 len = sizeof(sin);
00852
00853 res = recvfrom(rtp->rtcp->s, rtcpdata + AST_FRIENDLY_OFFSET, sizeof(rtcpdata) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET,
00854 0, (struct sockaddr *)&sin, &len);
00855 rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
00856
00857 if (res < 0) {
00858 ast_assert(errno != EBADF);
00859 if (errno != EAGAIN) {
00860 ast_log(LOG_WARNING, "RTCP Read error: %s. Hanging up.\n", strerror(errno));
00861 return NULL;
00862 }
00863 return &ast_null_frame;
00864 }
00865
00866 packetwords = res / 4;
00867
00868 if (rtp->nat) {
00869
00870 if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
00871 (rtp->rtcp->them.sin_port != sin.sin_port)) {
00872 memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
00873 if (option_debug || rtpdebug)
00874 ast_log(LOG_DEBUG, "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));
00875 }
00876 }
00877
00878 if (option_debug)
00879 ast_log(LOG_DEBUG, "Got RTCP report of %d bytes\n", res);
00880
00881
00882 position = 0;
00883 while (position < packetwords) {
00884 i = position;
00885 length = ntohl(rtcpheader[i]);
00886 pt = (length & 0xff0000) >> 16;
00887 rc = (length & 0x1f000000) >> 24;
00888 length &= 0xffff;
00889
00890 if ((i + length) > packetwords) {
00891 ast_log(LOG_WARNING, "RTCP Read too short\n");
00892 return &ast_null_frame;
00893 }
00894
00895 if (rtcp_debug_test_addr(&sin)) {
00896 ast_verbose("\n\nGot RTCP from %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
00897 ast_verbose("PT: %d(%s)\n", pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown");
00898 ast_verbose("Reception reports: %d\n", rc);
00899 ast_verbose("SSRC of sender: %u\n", rtcpheader[i + 1]);
00900 }
00901
00902 i += 2;
00903
00904 switch (pt) {
00905 case RTCP_PT_SR:
00906 gettimeofday(&rtp->rtcp->rxlsr,NULL);
00907 rtp->rtcp->spc = ntohl(rtcpheader[i+3]);
00908 rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
00909 rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16);
00910
00911 if (rtcp_debug_test_addr(&sin)) {
00912 ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader[i]), (unsigned long) ntohl(rtcpheader[i + 1]) * 4096);
00913 ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader[i + 2]));
00914 ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader[i + 3]), (unsigned long) ntohl(rtcpheader[i + 4]));
00915 }
00916 i += 5;
00917 if (rc < 1)
00918 break;
00919
00920 case RTCP_PT_RR:
00921
00922
00923 gettimeofday(&now, NULL);
00924 timeval2ntp(now, &msw, &lsw);
00925 if (ntohl(rtcpheader[i + 4]) && ntohl(rtcpheader[i + 5])) {
00926 comp = ((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16);
00927 lsr = ntohl(rtcpheader[i + 4]);
00928 dlsr = ntohl(rtcpheader[i + 5]);
00929 rtt = comp - lsr - dlsr;
00930
00931
00932
00933 if (rtt < 4294) {
00934 rtt = (rtt * 1000000) >> 16;
00935 } else {
00936 rtt = (rtt * 1000) >> 16;
00937 rtt *= 1000;
00938 }
00939 rtt = rtt / 1000.;
00940 rttsec = rtt / 1000.;
00941
00942 if (comp - dlsr >= lsr) {
00943 rtp->rtcp->accumulated_transit += rttsec;
00944 rtp->rtcp->rtt = rttsec;
00945 if (rtp->rtcp->maxrtt<rttsec)
00946 rtp->rtcp->maxrtt = rttsec;
00947 if (rtp->rtcp->minrtt>rttsec)
00948 rtp->rtcp->minrtt = rttsec;
00949 } else if (rtcp_debug_test_addr(&sin)) {
00950 ast_verbose("Internal RTCP NTP clock skew detected: "
00951 "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
00952 "diff=%d\n",
00953 lsr, comp, dlsr, dlsr / 65536,
00954 (dlsr % 65536) * 1000 / 65536,
00955 dlsr - (comp - lsr));
00956 }
00957 }
00958
00959 rtp->rtcp->reported_jitter = ntohl(rtcpheader[i + 3]);
00960 rtp->rtcp->reported_lost = ntohl(rtcpheader[i + 1]) & 0xffffff;
00961 if (rtcp_debug_test_addr(&sin)) {
00962 ast_verbose(" Fraction lost: %ld\n", (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24));
00963 ast_verbose(" Packets lost so far: %d\n", rtp->rtcp->reported_lost);
00964 ast_verbose(" Highest sequence number: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff));
00965 ast_verbose(" Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16);
00966 ast_verbose(" Interarrival jitter: %u\n", rtp->rtcp->reported_jitter);
00967 ast_verbose(" Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader[i + 4]) >> 16,((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096);
00968 ast_verbose(" DLSR: %4.4f (sec)\n",ntohl(rtcpheader[i + 5])/65536.0);
00969 if (rtt)
00970 ast_verbose(" RTT: %lu(sec)\n", (unsigned long) rtt);
00971 }
00972 break;
00973 case RTCP_PT_FUR:
00974 if (rtcp_debug_test_addr(&sin))
00975 ast_verbose("Received an RTCP Fast Update Request\n");
00976 rtp->f.frametype = AST_FRAME_CONTROL;
00977 rtp->f.subclass = AST_CONTROL_VIDUPDATE;
00978 rtp->f.datalen = 0;
00979 rtp->f.samples = 0;
00980 rtp->f.mallocd = 0;
00981 rtp->f.src = "RTP";
00982 f = &rtp->f;
00983 break;
00984 case RTCP_PT_SDES:
00985 if (rtcp_debug_test_addr(&sin))
00986 ast_verbose("Received an SDES from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
00987 break;
00988 case RTCP_PT_BYE:
00989 if (rtcp_debug_test_addr(&sin))
00990 ast_verbose("Received a BYE from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
00991 break;
00992 default:
00993 if (option_debug)
00994 ast_log(LOG_DEBUG, "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));
00995 break;
00996 }
00997 position += (length + 1);
00998 }
00999
01000 return f;
01001 }
01002
01003 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
01004 {
01005 struct timeval now;
01006 double transit;
01007 double current_time;
01008 double d;
01009 double dtv;
01010 double prog;
01011
01012 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
01013 gettimeofday(&rtp->rxcore, NULL);
01014 rtp->drxcore = (double) rtp->rxcore.tv_sec + (double) rtp->rxcore.tv_usec / 1000000;
01015
01016 rtp->seedrxts = timestamp;
01017 rtp->rxcore.tv_sec -= timestamp / 8000;
01018 rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
01019
01020 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
01021 if (rtp->rxcore.tv_usec < 0) {
01022
01023 rtp->rxcore.tv_usec += 1000000;
01024 rtp->rxcore.tv_sec -= 1;
01025 }
01026 }
01027
01028 gettimeofday(&now,NULL);
01029
01030 tv->tv_sec = rtp->rxcore.tv_sec + timestamp / 8000;
01031 tv->tv_usec = rtp->rxcore.tv_usec + (timestamp % 8000) * 125;
01032 if (tv->tv_usec >= 1000000) {
01033 tv->tv_usec -= 1000000;
01034 tv->tv_sec += 1;
01035 }
01036 prog = (double)((timestamp-rtp->seedrxts)/8000.);
01037 dtv = (double)rtp->drxcore + (double)(prog);
01038 current_time = (double)now.tv_sec + (double)now.tv_usec/1000000;
01039 transit = current_time - dtv;
01040 d = transit - rtp->rxtransit;
01041 rtp->rxtransit = transit;
01042 if (d<0)
01043 d=-d;
01044 rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
01045 if (rtp->rtcp && rtp->rxjitter > rtp->rtcp->maxrxjitter)
01046 rtp->rtcp->maxrxjitter = rtp->rxjitter;
01047 if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter)
01048 rtp->rtcp->minrxjitter = rtp->rxjitter;
01049 }
01050
01051
01052 static int bridge_p2p_rtp_write(struct ast_rtp *rtp, struct ast_rtp *bridged, unsigned int *rtpheader, int len, int hdrlen)
01053 {
01054 int res = 0, payload = 0, bridged_payload = 0, mark;
01055 struct rtpPayloadType rtpPT;
01056 int reconstruct = ntohl(rtpheader[0]);
01057
01058
01059 payload = (reconstruct & 0x7f0000) >> 16;
01060 mark = (((reconstruct & 0x800000) >> 23) != 0);
01061
01062
01063 rtpPT = ast_rtp_lookup_pt(rtp, payload);
01064
01065
01066 if (ast_test_flag(rtp, FLAG_P2P_NEED_DTMF) && !rtpPT.isAstFormat && rtpPT.code == AST_RTP_DTMF)
01067 return -1;
01068
01069
01070 bridged_payload = ast_rtp_lookup_code(bridged, rtpPT.isAstFormat, rtpPT.code);
01071
01072
01073 if (!bridged->current_RTP_PT[bridged_payload].code)
01074 return -1;
01075
01076
01077
01078 if (!ast_test_flag(rtp, FLAG_P2P_SENT_MARK)) {
01079 mark = 1;
01080 ast_set_flag(rtp, FLAG_P2P_SENT_MARK);
01081 }
01082
01083
01084 reconstruct &= 0xFF80FFFF;
01085 reconstruct |= (bridged_payload << 16);
01086 reconstruct |= (mark << 23);
01087 rtpheader[0] = htonl(reconstruct);
01088
01089
01090 res = sendto(bridged->s, (void *)rtpheader, len, 0, (struct sockaddr *)&bridged->them, sizeof(bridged->them));
01091 if (res < 0) {
01092 if (!bridged->nat || (bridged->nat && (ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
01093 ast_log(LOG_DEBUG, "RTP Transmission error of packet to %s:%d: %s\n", ast_inet_ntoa(bridged->them.sin_addr), ntohs(bridged->them.sin_port), strerror(errno));
01094 } else if (((ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(bridged, FLAG_NAT_INACTIVE_NOWARN)) {
01095 if (option_debug || rtpdebug)
01096 ast_log(LOG_DEBUG, "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));
01097 ast_set_flag(bridged, FLAG_NAT_INACTIVE_NOWARN);
01098 }
01099 return 0;
01100 } else if (rtp_debug_test_addr(&bridged->them))
01101 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);
01102
01103 return 0;
01104 }
01105
01106 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
01107 {
01108 int res;
01109 struct sockaddr_in sin;
01110 socklen_t len;
01111 unsigned int seqno;
01112 int version;
01113 int payloadtype;
01114 int hdrlen = 12;
01115 int padding;
01116 int mark;
01117 int ext;
01118 int cc;
01119 unsigned int ssrc;
01120 unsigned int timestamp;
01121 unsigned int *rtpheader;
01122 struct rtpPayloadType rtpPT;
01123 struct ast_rtp *bridged = NULL;
01124
01125 if( !rtp ) {
01126 ast_log(LOG_ERROR, "ast_rtp_read(): called with rtp == NULL\n");
01127 ast_backtrace();
01128 return &ast_null_frame;
01129 }
01130
01131
01132 if (rtp->sending_digit)
01133 ast_rtp_senddigit_continuation(rtp);
01134
01135 len = sizeof(sin);
01136
01137
01138 res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
01139 0, (struct sockaddr *)&sin, &len);
01140
01141 rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
01142 if (res < 0) {
01143 ast_assert(errno != EBADF);
01144 if (errno != EAGAIN) {
01145 ast_log(LOG_WARNING, "RTP Read error: %s. Hanging up.\n", strerror(errno));
01146 return NULL;
01147 }
01148 return &ast_null_frame;
01149 }
01150
01151 if (res < hdrlen) {
01152 ast_log(LOG_WARNING, "RTP Read too short\n");
01153 return &ast_null_frame;
01154 }
01155
01156
01157 seqno = ntohl(rtpheader[0]);
01158
01159
01160 version = (seqno & 0xC0000000) >> 30;
01161 if (!version) {
01162 if ((stun_handle_packet(rtp->s, &sin, rtp->rawdata + AST_FRIENDLY_OFFSET, res) == STUN_ACCEPT) &&
01163 (!rtp->them.sin_port && !rtp->them.sin_addr.s_addr)) {
01164 memcpy(&rtp->them, &sin, sizeof(rtp->them));
01165 }
01166 return &ast_null_frame;
01167 }
01168
01169 #if 0
01170
01171 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
01172 return &ast_null_frame;
01173 #endif
01174
01175
01176 if (rtp->nat) {
01177 if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
01178 (rtp->them.sin_port != sin.sin_port)) {
01179 rtp->them = sin;
01180 if (rtp->rtcp) {
01181 memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
01182 rtp->rtcp->them.sin_port = htons(ntohs(rtp->them.sin_port)+1);
01183 }
01184 rtp->rxseqno = 0;
01185 ast_set_flag(rtp, FLAG_NAT_ACTIVE);
01186 if (option_debug || rtpdebug)
01187 ast_log(LOG_DEBUG, "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));
01188 }
01189 }
01190
01191
01192 if ((bridged = ast_rtp_get_bridged(rtp)) && !bridge_p2p_rtp_write(rtp, bridged, rtpheader, res, hdrlen))
01193 return &ast_null_frame;
01194
01195 if (version != 2)
01196 return &ast_null_frame;
01197
01198 payloadtype = (seqno & 0x7f0000) >> 16;
01199 padding = seqno & (1 << 29);
01200 mark = seqno & (1 << 23);
01201 ext = seqno & (1 << 28);
01202 cc = (seqno & 0xF000000) >> 24;
01203 seqno &= 0xffff;
01204 timestamp = ntohl(rtpheader[1]);
01205 ssrc = ntohl(rtpheader[2]);
01206
01207 if (!mark && rtp->rxssrc && rtp->rxssrc != ssrc) {
01208 if (option_debug || rtpdebug)
01209 ast_log(LOG_DEBUG, "Forcing Marker bit, because SSRC has changed\n");
01210 mark = 1;
01211 }
01212
01213 rtp->rxssrc = ssrc;
01214
01215 if (padding) {
01216
01217 res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
01218 }
01219
01220 if (cc) {
01221
01222 hdrlen += cc*4;
01223 }
01224
01225 if (ext) {
01226
01227 hdrlen += (ntohl(rtpheader[hdrlen/4]) & 0xffff) << 2;
01228 hdrlen += 4;
01229 if (option_debug) {
01230 int profile;
01231 profile = (ntohl(rtpheader[3]) & 0xffff0000) >> 16;
01232 if (profile == 0x505a)
01233 ast_log(LOG_DEBUG, "Found Zfone extension in RTP stream - zrtp - not supported.\n");
01234 else
01235 ast_log(LOG_DEBUG, "Found unknown RTP Extensions %x\n", profile);
01236 }
01237 }
01238
01239 if (res < hdrlen) {
01240 ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
01241 return &ast_null_frame;
01242 }
01243
01244 rtp->rxcount++;
01245
01246 if (rtp->rxcount==1) {
01247
01248 rtp->seedrxseqno = seqno;
01249 }
01250
01251
01252 if (rtp->rtcp && rtp->rtcp->them.sin_addr.s_addr && rtp->rtcp->schedid < 1) {
01253
01254 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
01255 }
01256 if ( (int)rtp->lastrxseqno - (int)seqno > 100)
01257 rtp->cycles += RTP_SEQ_MOD;
01258
01259 rtp->lastrxseqno = seqno;
01260
01261 if (rtp->themssrc==0)
01262 rtp->themssrc = ntohl(rtpheader[2]);
01263
01264 if (rtp_debug_test_addr(&sin))
01265 ast_verbose("Got RTP packet from %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
01266 ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
01267
01268 rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
01269 if (!rtpPT.isAstFormat) {
01270 struct ast_frame *f = NULL;
01271
01272
01273 if (rtpPT.code == AST_RTP_DTMF) {
01274
01275 if (rtp_debug_test_addr(&sin)) {
01276 unsigned char *data;
01277 unsigned int event;
01278 unsigned int event_end;
01279 unsigned int duration;
01280 data = rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen;
01281 event = ntohl(*((unsigned int *)(data)));
01282 event >>= 24;
01283 event_end = ntohl(*((unsigned int *)(data)));
01284 event_end <<= 8;
01285 event_end >>= 24;
01286 duration = ntohl(*((unsigned int *)(data)));
01287 duration &= 0xFFFF;
01288 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(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp, res - hdrlen, (mark?1:0), event, ((event_end & 0x80)?1:0), duration);
01289 }
01290 f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp);
01291 } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
01292
01293 if (rtp->lastevent <= seqno || (rtp->lastevent >= 65530 && seqno <= 6)) {
01294 f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
01295 rtp->lastevent = seqno;
01296 }
01297 } else if (rtpPT.code == AST_RTP_CN) {
01298
01299 f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
01300 } else {
01301 ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n", payloadtype, ast_inet_ntoa(rtp->them.sin_addr));
01302 }
01303 return f ? f : &ast_null_frame;
01304 }
01305 rtp->lastrxformat = rtp->f.subclass = rtpPT.code;
01306 rtp->f.frametype = (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) ? AST_FRAME_VOICE : AST_FRAME_VIDEO;
01307
01308 if (!rtp->lastrxts)
01309 rtp->lastrxts = timestamp;
01310
01311 rtp->rxseqno = seqno;
01312
01313
01314 rtp->lastrxts = timestamp;
01315
01316 rtp->f.mallocd = 0;
01317 rtp->f.datalen = res - hdrlen;
01318 rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
01319 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
01320 rtp->f.seqno = seqno;
01321 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
01322 rtp->f.samples = ast_codec_get_samples(&rtp->f);
01323 if (rtp->f.subclass == AST_FORMAT_SLINEAR)
01324 ast_frame_byteswap_be(&rtp->f);
01325 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
01326
01327 ast_set_flag(&rtp->f, AST_FRFLAG_HAS_TIMING_INFO);
01328 rtp->f.ts = timestamp / 8;
01329 rtp->f.len = rtp->f.samples / (ast_format_rate(rtp->f.subclass) / 1000);
01330 } else {
01331
01332 if (!rtp->lastividtimestamp)
01333 rtp->lastividtimestamp = timestamp;
01334 rtp->f.samples = timestamp - rtp->lastividtimestamp;
01335 rtp->lastividtimestamp = timestamp;
01336 rtp->f.delivery.tv_sec = 0;
01337 rtp->f.delivery.tv_usec = 0;
01338 if (mark)
01339 rtp->f.subclass |= 0x1;
01340
01341 }
01342 rtp->f.src = "RTP";
01343 return &rtp->f;
01344 }
01345
01346
01347
01348 static struct {
01349 struct rtpPayloadType payloadType;
01350 char* type;
01351 char* subtype;
01352 } mimeTypes[] = {
01353 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
01354 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
01355 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
01356 {{1, AST_FORMAT_ULAW}, "audio", "G711U"},
01357 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
01358 {{1, AST_FORMAT_ALAW}, "audio", "G711A"},
01359 {{1, AST_FORMAT_G726}, "audio", "G726-32"},
01360 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
01361 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
01362 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
01363 {{1, AST_FORMAT_G729A}, "audio", "G729"},
01364 {{1, AST_FORMAT_G729A}, "audio", "G729A"},
01365 {{1, AST_FORMAT_G729A}, "audio", "G.729"},
01366 {{1, AST_FORMAT_SPEEX}, "audio", "speex"},
01367 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
01368 {{1, AST_FORMAT_G722}, "audio", "G722"},
01369 {{1, AST_FORMAT_G726_AAL2}, "audio", "AAL2-G726-32"},
01370 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
01371 {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
01372 {{0, AST_RTP_CN}, "audio", "CN"},
01373 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
01374 {{1, AST_FORMAT_PNG}, "video", "PNG"},
01375 {{1, AST_FORMAT_H261}, "video", "H261"},
01376 {{1, AST_FORMAT_H263}, "video", "H263"},
01377 {{1, AST_FORMAT_H263_PLUS}, "video", "h263-1998"},
01378 {{1, AST_FORMAT_H264}, "video", "H264"},
01379 {{1, AST_FORMAT_MP4_VIDEO}, "video", "MP4V-ES"},
01380 };
01381
01382
01383
01384
01385 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
01386 [0] = {1, AST_FORMAT_ULAW},
01387 #ifdef USE_DEPRECATED_G726
01388 [2] = {1, AST_FORMAT_G726},
01389 #endif
01390 [3] = {1, AST_FORMAT_GSM},
01391 [4] = {1, AST_FORMAT_G723_1},
01392 [5] = {1, AST_FORMAT_ADPCM},
01393 [6] = {1, AST_FORMAT_ADPCM},
01394 [7] = {1, AST_FORMAT_LPC10},
01395 [8] = {1, AST_FORMAT_ALAW},
01396 [9] = {1, AST_FORMAT_G722},
01397 [10] = {1, AST_FORMAT_SLINEAR},
01398 [11] = {1, AST_FORMAT_SLINEAR},
01399 [13] = {0, AST_RTP_CN},
01400 [16] = {1, AST_FORMAT_ADPCM},
01401 [17] = {1, AST_FORMAT_ADPCM},
01402 [18] = {1, AST_FORMAT_G729A},
01403 [19] = {0, AST_RTP_CN},
01404 [26] = {1, AST_FORMAT_JPEG},
01405 [31] = {1, AST_FORMAT_H261},
01406 [34] = {1, AST_FORMAT_H263},
01407 [97] = {1, AST_FORMAT_ILBC},
01408 [99] = {1, AST_FORMAT_H264},
01409 [101] = {0, AST_RTP_DTMF},
01410 [103] = {1, AST_FORMAT_H263_PLUS},
01411 [104] = {1, AST_FORMAT_MP4_VIDEO},
01412 [110] = {1, AST_FORMAT_SPEEX},
01413 [111] = {1, AST_FORMAT_G726},
01414 [112] = {1, AST_FORMAT_G726_AAL2},
01415 [121] = {0, AST_RTP_CISCO_DTMF},
01416 };
01417
01418 void ast_rtp_pt_clear(struct ast_rtp* rtp)
01419 {
01420 int i;
01421
01422 if (!rtp)
01423 return;
01424
01425 ast_mutex_lock(&rtp->bridge_lock);
01426
01427 for (i = 0; i < MAX_RTP_PT; ++i) {
01428 rtp->current_RTP_PT[i].isAstFormat = 0;
01429 rtp->current_RTP_PT[i].code = 0;
01430 }
01431
01432 rtp->rtp_lookup_code_cache_isAstFormat = 0;
01433 rtp->rtp_lookup_code_cache_code = 0;
01434 rtp->rtp_lookup_code_cache_result = 0;
01435
01436 ast_mutex_unlock(&rtp->bridge_lock);
01437 }
01438
01439 void ast_rtp_pt_default(struct ast_rtp* rtp)
01440 {
01441 int i;
01442
01443 ast_mutex_lock(&rtp->bridge_lock);
01444
01445
01446 for (i = 0; i < MAX_RTP_PT; ++i) {
01447 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
01448 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
01449 }
01450
01451 rtp->rtp_lookup_code_cache_isAstFormat = 0;
01452 rtp->rtp_lookup_code_cache_code = 0;
01453 rtp->rtp_lookup_code_cache_result = 0;
01454
01455 ast_mutex_unlock(&rtp->bridge_lock);
01456 }
01457
01458 void ast_rtp_pt_copy(struct ast_rtp *dest, struct ast_rtp *src)
01459 {
01460 unsigned int i;
01461
01462 ast_mutex_lock(&dest->bridge_lock);
01463 ast_mutex_lock(&src->bridge_lock);
01464
01465 for (i=0; i < MAX_RTP_PT; ++i) {
01466 dest->current_RTP_PT[i].isAstFormat =
01467 src->current_RTP_PT[i].isAstFormat;
01468 dest->current_RTP_PT[i].code =
01469 src->current_RTP_PT[i].code;
01470 }
01471 dest->rtp_lookup_code_cache_isAstFormat = 0;
01472 dest->rtp_lookup_code_cache_code = 0;
01473 dest->rtp_lookup_code_cache_result = 0;
01474
01475 ast_mutex_unlock(&src->bridge_lock);
01476 ast_mutex_unlock(&dest->bridge_lock);
01477 }
01478
01479
01480 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
01481 {
01482 struct ast_rtp_protocol *cur = NULL;
01483
01484 AST_LIST_LOCK(&protos);
01485 AST_LIST_TRAVERSE(&protos, cur, list) {
01486 if (cur->type == chan->tech->type)
01487 break;
01488 }
01489 AST_LIST_UNLOCK(&protos);
01490
01491 return cur;
01492 }
01493
01494 int ast_rtp_early_bridge(struct ast_channel *dest, struct ast_channel *src)
01495 {
01496 struct ast_rtp *destp = NULL, *srcp = NULL;
01497 struct ast_rtp *vdestp = NULL, *vsrcp = NULL;
01498 struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
01499 enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED;
01500 enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED;
01501 int srccodec, destcodec, nat_active = 0;
01502
01503
01504 ast_channel_lock(dest);
01505 if (src) {
01506 while(ast_channel_trylock(src)) {
01507 ast_channel_unlock(dest);
01508 usleep(1);
01509 ast_channel_lock(dest);
01510 }
01511 }
01512
01513
01514 destpr = get_proto(dest);
01515 if (src)
01516 srcpr = get_proto(src);
01517 if (!destpr) {
01518 if (option_debug)
01519 ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", dest->name);
01520 ast_channel_unlock(dest);
01521 if (src)
01522 ast_channel_unlock(src);
01523 return 0;
01524 }
01525 if (!srcpr) {
01526 if (option_debug)
01527 ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", src ? src->name : "<unspecified>");
01528 ast_channel_unlock(dest);
01529 if (src)
01530 ast_channel_unlock(src);
01531 return 0;
01532 }
01533
01534
01535 audio_dest_res = destpr->get_rtp_info(dest, &destp);
01536 video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
01537 if (srcpr) {
01538 audio_src_res = srcpr->get_rtp_info(src, &srcp);
01539 video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
01540 }
01541
01542
01543 if (audio_dest_res != AST_RTP_TRY_NATIVE || (video_dest_res != AST_RTP_GET_FAILED && video_dest_res != AST_RTP_TRY_NATIVE)) {
01544
01545 ast_channel_unlock(dest);
01546 if (src)
01547 ast_channel_unlock(src);
01548 return 0;
01549 }
01550 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)
01551 srccodec = srcpr->get_codec(src);
01552 else
01553 srccodec = 0;
01554 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)
01555 destcodec = destpr->get_codec(dest);
01556 else
01557 destcodec = 0;
01558
01559 if (!(srccodec & destcodec)) {
01560 ast_channel_unlock(dest);
01561 if (src)
01562 ast_channel_unlock(src);
01563 return 0;
01564 }
01565
01566 if (audio_src_res == AST_RTP_TRY_NATIVE && !srcp->them.sin_addr.s_addr)
01567 srcp = NULL;
01568
01569 if (srcp && (srcp->nat || ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
01570 nat_active = 1;
01571
01572 if (destpr->set_rtp_peer(dest, srcp, vsrcp, srccodec, nat_active))
01573 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src ? src->name : "<unspecified>");
01574 ast_channel_unlock(dest);
01575 if (src)
01576 ast_channel_unlock(src);
01577 if (option_debug)
01578 ast_log(LOG_DEBUG, "Setting early bridge SDP of '%s' with that of '%s'\n", dest->name, src ? src->name : "<unspecified>");
01579 return 1;
01580 }
01581
01582 int ast_rtp_make_compatible(struct ast_channel *dest, struct ast_channel *src, int media)
01583 {
01584 struct ast_rtp *destp = NULL, *srcp = NULL;
01585 struct ast_rtp *vdestp = NULL, *vsrcp = NULL;
01586 struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
01587 enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED;
01588 enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED;
01589 int srccodec, destcodec;
01590
01591
01592 ast_channel_lock(dest);
01593 while(ast_channel_trylock(src)) {
01594 ast_channel_unlock(dest);
01595 usleep(1);
01596 ast_channel_lock(dest);
01597 }
01598
01599
01600 if (!(destpr = get_proto(dest))) {
01601 if (option_debug)
01602 ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", dest->name);
01603 ast_channel_unlock(dest);
01604 ast_channel_unlock(src);
01605 return 0;
01606 }
01607 if (!(srcpr = get_proto(src))) {
01608 if (option_debug)
01609 ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", src->name);
01610 ast_channel_unlock(dest);
01611 ast_channel_unlock(src);
01612 return 0;
01613 }
01614
01615
01616 audio_dest_res = destpr->get_rtp_info(dest, &destp);
01617 video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
01618 audio_src_res = srcpr->get_rtp_info(src, &srcp);
01619 video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
01620
01621
01622 if (srcpr->get_codec)
01623 srccodec = srcpr->get_codec(src);
01624 else
01625 srccodec = 0;
01626 if (destpr->get_codec)
01627 destcodec = destpr->get_codec(dest);
01628 else
01629 destcodec = 0;
01630
01631
01632 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)) {
01633
01634 ast_channel_unlock(dest);
01635 ast_channel_unlock(src);
01636 return 0;
01637 }
01638 ast_rtp_pt_copy(destp, srcp);
01639 if (vdestp && vsrcp)
01640 ast_rtp_pt_copy(vdestp, vsrcp);
01641 if (media) {
01642
01643 if (destpr->set_rtp_peer(dest, srcp, vsrcp, srccodec, ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
01644 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src->name);
01645 }
01646 ast_channel_unlock(dest);
01647 ast_channel_unlock(src);
01648 if (option_debug)
01649 ast_log(LOG_DEBUG, "Seeded SDP of '%s' with that of '%s'\n", dest->name, src->name);
01650 return 1;
01651 }
01652
01653
01654
01655
01656
01657 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt)
01658 {
01659 if (pt < 0 || pt > MAX_RTP_PT || static_RTP_PT[pt].code == 0)
01660 return;
01661
01662 ast_mutex_lock(&rtp->bridge_lock);
01663 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
01664 ast_mutex_unlock(&rtp->bridge_lock);
01665 }
01666
01667
01668
01669 void ast_rtp_unset_m_type(struct ast_rtp* rtp, int pt)
01670 {
01671 if (pt < 0 || pt > MAX_RTP_PT)
01672 return;
01673
01674 ast_mutex_lock(&rtp->bridge_lock);
01675 rtp->current_RTP_PT[pt].isAstFormat = 0;
01676 rtp->current_RTP_PT[pt].code = 0;
01677 ast_mutex_unlock(&rtp->bridge_lock);
01678 }
01679
01680
01681
01682
01683
01684 int ast_rtp_set_rtpmap_type(struct ast_rtp *rtp, int pt,
01685 char *mimeType, char *mimeSubtype,
01686 enum ast_rtp_options options)
01687 {
01688 unsigned int i;
01689 int found = 0;
01690
01691 if (pt < 0 || pt > MAX_RTP_PT)
01692 return -1;
01693
01694 ast_mutex_lock(&rtp->bridge_lock);
01695
01696 for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
01697 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
01698 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
01699 found = 1;
01700 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
01701 if ((mimeTypes[i].payloadType.code == AST_FORMAT_G726) &&
01702 mimeTypes[i].payloadType.isAstFormat &&
01703 (options & AST_RTP_OPT_G726_NONSTANDARD))
01704 rtp->current_RTP_PT[pt].code = AST_FORMAT_G726_AAL2;
01705 break;
01706 }
01707 }
01708
01709 ast_mutex_unlock(&rtp->bridge_lock);
01710
01711 return (found ? 0 : -1);
01712 }
01713
01714
01715
01716 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
01717 int* astFormats, int* nonAstFormats)
01718 {
01719 int pt;
01720
01721 ast_mutex_lock(&rtp->bridge_lock);
01722
01723 *astFormats = *nonAstFormats = 0;
01724 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
01725 if (rtp->current_RTP_PT[pt].isAstFormat) {
01726 *astFormats |= rtp->current_RTP_PT[pt].code;
01727 } else {
01728 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
01729 }
01730 }
01731
01732 ast_mutex_unlock(&rtp->bridge_lock);
01733
01734 return;
01735 }
01736
01737 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
01738 {
01739 struct rtpPayloadType result;
01740
01741 result.isAstFormat = result.code = 0;
01742
01743 if (pt < 0 || pt > MAX_RTP_PT)
01744 return result;
01745
01746
01747 ast_mutex_lock(&rtp->bridge_lock);
01748 result = rtp->current_RTP_PT[pt];
01749 ast_mutex_unlock(&rtp->bridge_lock);
01750
01751
01752 if (!result.code)
01753 result = static_RTP_PT[pt];
01754
01755 return result;
01756 }
01757
01758
01759 int ast_rtp_lookup_code(struct ast_rtp* rtp, const int isAstFormat, const int code)
01760 {
01761 int pt = 0;
01762
01763 ast_mutex_lock(&rtp->bridge_lock);
01764
01765 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
01766 code == rtp->rtp_lookup_code_cache_code) {
01767
01768 pt = rtp->rtp_lookup_code_cache_result;
01769 ast_mutex_unlock(&rtp->bridge_lock);
01770 return pt;
01771 }
01772
01773
01774 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
01775 if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
01776 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
01777 rtp->rtp_lookup_code_cache_code = code;
01778 rtp->rtp_lookup_code_cache_result = pt;
01779 ast_mutex_unlock(&rtp->bridge_lock);
01780 return pt;
01781 }
01782 }
01783
01784
01785 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
01786 if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
01787 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
01788 rtp->rtp_lookup_code_cache_code = code;
01789 rtp->rtp_lookup_code_cache_result = pt;
01790 ast_mutex_unlock(&rtp->bridge_lock);
01791 return pt;
01792 }
01793 }
01794
01795 ast_mutex_unlock(&rtp->bridge_lock);
01796
01797 return -1;
01798 }
01799
01800 const char *ast_rtp_lookup_mime_subtype(const int isAstFormat, const int code,
01801 enum ast_rtp_options options)
01802 {
01803 unsigned int i;
01804
01805 for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
01806 if ((mimeTypes[i].payloadType.code == code) && (mimeTypes[i].payloadType.isAstFormat == isAstFormat)) {
01807 if (isAstFormat &&
01808 (code == AST_FORMAT_G726_AAL2) &&
01809 (options & AST_RTP_OPT_G726_NONSTANDARD))
01810 return "G726-32";
01811 else
01812 return mimeTypes[i].subtype;
01813 }
01814 }
01815
01816 return "";
01817 }
01818
01819 char *ast_rtp_lookup_mime_multiple(char *buf, size_t size, const int capability,
01820 const int isAstFormat, enum ast_rtp_options options)
01821 {
01822 int format;
01823 unsigned len;
01824 char *end = buf;
01825 char *start = buf;
01826
01827 if (!buf || !size)
01828 return NULL;
01829
01830 snprintf(end, size, "0x%x (", capability);
01831
01832 len = strlen(end);
01833 end += len;
01834 size -= len;
01835 start = end;
01836
01837 for (format = 1; format < AST_RTP_MAX; format <<= 1) {
01838 if (capability & format) {
01839 const char *name = ast_rtp_lookup_mime_subtype(isAstFormat, format, options);
01840
01841 snprintf(end, size, "%s|", name);
01842 len = strlen(end);
01843 end += len;
01844 size -= len;
01845 }
01846 }
01847
01848 if (start == end)
01849 snprintf(start, size, "nothing)");
01850 else if (size > 1)
01851 *(end -1) = ')';
01852
01853 return buf;
01854 }
01855
01856 static int rtp_socket(void)
01857 {
01858 int s;
01859 long flags;
01860 s = socket(AF_INET, SOCK_DGRAM, 0);
01861 if (s > -1) {
01862 flags = fcntl(s, F_GETFL);
01863 fcntl(s, F_SETFL, flags | O_NONBLOCK);
01864 #ifdef SO_NO_CHECK
01865 if (nochecksums)
01866 setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
01867 #endif
01868 }
01869 return s;
01870 }
01871
01872
01873
01874
01875
01876
01877 static struct ast_rtcp *ast_rtcp_new(void)
01878 {
01879 struct ast_rtcp *rtcp;
01880
01881 if (!(rtcp = ast_calloc(1, sizeof(*rtcp))))
01882 return NULL;
01883 rtcp->s = rtp_socket();
01884 rtcp->us.sin_family = AF_INET;
01885 rtcp->them.sin_family = AF_INET;
01886 rtcp->schedid = -1;
01887
01888 if (rtcp->s < 0) {
01889 free(rtcp);
01890 ast_log(LOG_WARNING, "Unable to allocate RTCP socket: %s\n", strerror(errno));
01891 return NULL;
01892 }
01893
01894 return rtcp;
01895 }
01896
01897
01898
01899
01900
01901 void ast_rtp_new_init(struct ast_rtp *rtp)
01902 {
01903 ast_mutex_init(&rtp->bridge_lock);
01904
01905 rtp->them.sin_family = AF_INET;
01906 rtp->us.sin_family = AF_INET;
01907 rtp->ssrc = ast_random();
01908 rtp->seqno = ast_random() & 0xffff;
01909 ast_set_flag(rtp, FLAG_HAS_DTMF);
01910
01911 return;
01912 }
01913
01914 struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
01915 {
01916 struct ast_rtp *rtp;
01917 int x;
01918 int first;
01919 int startplace;
01920
01921 if (!(rtp = ast_calloc(1, sizeof(*rtp))))
01922 return NULL;
01923
01924 ast_rtp_new_init(rtp);
01925
01926 rtp->s = rtp_socket();
01927 if (rtp->s < 0) {
01928 free(rtp);
01929 ast_log(LOG_ERROR, "Unable to allocate socket: %s\n", strerror(errno));
01930 return NULL;
01931 }
01932 if (sched && rtcpenable) {
01933 rtp->sched = sched;
01934 rtp->rtcp = ast_rtcp_new();
01935 }
01936
01937
01938 x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
01939 x = x & ~1;
01940
01941 startplace = x;
01942
01943 for (;;) {
01944
01945 rtp->us.sin_port = htons(x);
01946 rtp->us.sin_addr = addr;
01947
01948 if (rtp->rtcp) {
01949 rtp->rtcp->us.sin_port = htons(x + 1);
01950 rtp->rtcp->us.sin_addr = addr;
01951 }
01952
01953 if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
01954 (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
01955 break;
01956 if (!first) {
01957
01958 close(rtp->s);
01959 rtp->s = rtp_socket();
01960 }
01961 if (errno != EADDRINUSE) {
01962
01963 ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
01964 close(rtp->s);
01965 if (rtp->rtcp) {
01966 close(rtp->rtcp->s);
01967 free(rtp->rtcp);
01968 }
01969 free(rtp);
01970 return NULL;
01971 }
01972
01973 x += 2;
01974
01975 if (x > rtpend)
01976
01977 x = (rtpstart + 1) & ~1;
01978
01979 if (x == startplace) {
01980
01981 ast_log(LOG_ERROR, "No RTP ports remaining. Can't setup media stream for this call.\n");
01982 close(rtp->s);
01983 if (rtp->rtcp) {
01984 close(rtp->rtcp->s);
01985 free(rtp->rtcp);
01986 }
01987 free(rtp);
01988 return NULL;
01989 }
01990 }
01991 rtp->sched = sched;
01992 rtp->io = io;
01993 if (callbackmode) {
01994 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
01995 ast_set_flag(rtp, FLAG_CALLBACK_MODE);
01996 }
01997 ast_rtp_pt_default(rtp);
01998 return rtp;
01999 }
02000
02001 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
02002 {
02003 struct in_addr ia;
02004
02005 memset(&ia, 0, sizeof(ia));
02006 return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
02007 }
02008
02009 int ast_rtp_settos(struct ast_rtp *rtp, int tos)
02010 {
02011 int res;
02012
02013 if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
02014 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
02015 return res;
02016 }
02017
02018 void ast_rtp_new_source(struct ast_rtp *rtp)
02019 {
02020 if (rtp) {
02021 rtp->set_marker_bit = 1;
02022 }
02023 return;
02024 }
02025
02026 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
02027 {
02028 rtp->them.sin_port = them->sin_port;
02029 rtp->them.sin_addr = them->sin_addr;
02030 if (rtp->rtcp) {
02031 rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
02032 rtp->rtcp->them.sin_addr = them->sin_addr;
02033 }
02034 rtp->rxseqno = 0;
02035 }
02036
02037 int ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
02038 {
02039 if ((them->sin_family != AF_INET) ||
02040 (them->sin_port != rtp->them.sin_port) ||
02041 (them->sin_addr.s_addr != rtp->them.sin_addr.s_addr)) {
02042 them->sin_family = AF_INET;
02043 them->sin_port = rtp->them.sin_port;
02044 them->sin_addr = rtp->them.sin_addr;
02045 return 1;
02046 }
02047 return 0;
02048 }
02049
02050 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
02051 {
02052 *us = rtp->us;
02053 }
02054
02055 struct ast_rtp *ast_rtp_get_bridged(struct ast_rtp *rtp)
02056 {
02057 struct ast_rtp *bridged = NULL;
02058
02059 ast_mutex_lock(&rtp->bridge_lock);
02060 bridged = rtp->bridged;
02061 ast_mutex_unlock(&rtp->bridge_lock);
02062
02063 return bridged;
02064 }
02065
02066 void ast_rtp_stop(struct ast_rtp *rtp)
02067 {
02068 if (rtp->rtcp) {
02069 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
02070 }
02071
02072 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
02073 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
02074 if (rtp->rtcp) {
02075 memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->rtcp->them.sin_addr));
02076 memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->rtcp->them.sin_port));
02077 }
02078
02079 ast_clear_flag(rtp, FLAG_P2P_SENT_MARK);
02080 }
02081
02082 void ast_rtp_reset(struct ast_rtp *rtp)
02083 {
02084 memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
02085 memset(&rtp->txcore, 0, sizeof(rtp->txcore));
02086 memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
02087 rtp->lastts = 0;
02088 rtp->lastdigitts = 0;
02089 rtp->lastrxts = 0;
02090 rtp->lastividtimestamp = 0;
02091 rtp->lastovidtimestamp = 0;
02092 rtp->lasteventseqn = 0;
02093 rtp->lastevent = 0;
02094 rtp->lasttxformat = 0;
02095 rtp->lastrxformat = 0;
02096 rtp->dtmfcount = 0;
02097 rtp->dtmfsamples = 0;
02098 rtp->seqno = 0;
02099 rtp->rxseqno = 0;
02100 }
02101
02102 char *ast_rtp_get_quality(struct ast_rtp *rtp, struct ast_rtp_quality *qual)
02103 {
02104
02105
02106
02107
02108
02109
02110
02111
02112
02113
02114
02115
02116 if (qual && rtp) {
02117 qual->local_ssrc = rtp->ssrc;
02118 qual->local_jitter = rtp->rxjitter;
02119 qual->local_count = rtp->rxcount;
02120 qual->remote_ssrc = rtp->themssrc;
02121 qual->remote_count = rtp->txcount;
02122 if (rtp->rtcp) {
02123 qual->local_lostpackets = rtp->rtcp->expected_prior - rtp->rtcp->received_prior;
02124 qual->remote_lostpackets = rtp->rtcp->reported_lost;
02125 qual->remote_jitter = rtp->rtcp->reported_jitter / 65536.0;
02126 qual->rtt = rtp->rtcp->rtt;
02127 }
02128 }
02129 if (rtp->rtcp) {
02130 snprintf(rtp->rtcp->quality, sizeof(rtp->rtcp->quality),
02131 "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
02132 rtp->ssrc,
02133 rtp->themssrc,
02134 rtp->rtcp->expected_prior - rtp->rtcp->received_prior,
02135 rtp->rxjitter,
02136 rtp->rxcount,
02137 (double)rtp->rtcp->reported_jitter / 65536.0,
02138 rtp->txcount,
02139 rtp->rtcp->reported_lost,
02140 rtp->rtcp->rtt);
02141 return rtp->rtcp->quality;
02142 } else
02143 return "<Unknown> - RTP/RTCP has already been destroyed";
02144 }
02145
02146 void ast_rtp_destroy(struct ast_rtp *rtp)
02147 {
02148 if (rtcp_debug_test_addr(&rtp->them) || rtcpstats) {
02149
02150 ast_verbose(" RTP-stats\n");
02151 ast_verbose("* Our Receiver:\n");
02152 ast_verbose(" SSRC: %u\n", rtp->themssrc);
02153 ast_verbose(" Received packets: %u\n", rtp->rxcount);
02154 ast_verbose(" Lost packets: %u\n", rtp->rtcp->expected_prior - rtp->rtcp->received_prior);
02155 ast_verbose(" Jitter: %.4f\n", rtp->rxjitter);
02156 ast_verbose(" Transit: %.4f\n", rtp->rxtransit);
02157 ast_verbose(" RR-count: %u\n", rtp->rtcp->rr_count);
02158 ast_verbose("* Our Sender:\n");
02159 ast_verbose(" SSRC: %u\n", rtp->ssrc);
02160 ast_verbose(" Sent packets: %u\n", rtp->txcount);
02161 ast_verbose(" Lost packets: %u\n", rtp->rtcp->reported_lost);
02162 ast_verbose(" Jitter: %u\n", rtp->rtcp->reported_jitter / (unsigned int)65536.0);
02163 ast_verbose(" SR-count: %u\n", rtp->rtcp->sr_count);
02164 ast_verbose(" RTT: %f\n", rtp->rtcp->rtt);
02165 }
02166
02167 if (rtp->smoother)
02168 ast_smoother_free(rtp->smoother);
02169 if (rtp->ioid)
02170 ast_io_remove(rtp->io, rtp->ioid);
02171 if (rtp->s > -1)
02172 close(rtp->s);
02173 if (rtp->rtcp) {
02174 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
02175 close(rtp->rtcp->s);
02176 free(rtp->rtcp);
02177 rtp->rtcp=NULL;
02178 }
02179
02180 ast_mutex_destroy(&rtp->bridge_lock);
02181
02182 free(rtp);
02183 }
02184
02185 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
02186 {
02187 struct timeval t;
02188 long ms;
02189 if (ast_tvzero(rtp->txcore)) {
02190 rtp->txcore = ast_tvnow();
02191
02192 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
02193 }
02194
02195 t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
02196 ms = ast_tvdiff_ms(t, rtp->txcore);
02197 if (ms < 0)
02198 ms = 0;
02199
02200 rtp->txcore = t;
02201 return (unsigned int) ms;
02202 }
02203
02204
02205 int ast_rtp_senddigit_begin(struct ast_rtp *rtp, char digit)
02206 {
02207 unsigned int *rtpheader;
02208 int hdrlen = 12, res = 0, i = 0, payload = 0;
02209 char data[256];
02210
02211 if ((digit <= '9') && (digit >= '0'))
02212 digit -= '0';
02213 else if (digit == '*')
02214 digit = 10;
02215 else if (digit == '#')
02216 digit = 11;
02217 else if ((digit >= 'A') && (digit <= 'D'))
02218 digit = digit - 'A' + 12;
02219 else if ((digit >= 'a') && (digit <= 'd'))
02220 digit = digit - 'a' + 12;
02221 else {
02222 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
02223 return 0;
02224 }
02225
02226
02227 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
02228 return 0;
02229
02230 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
02231
02232 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
02233 rtp->send_duration = 160;
02234 rtp->lastdigitts = rtp->lastts + rtp->send_duration;
02235
02236
02237 rtpheader = (unsigned int *)data;
02238 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
02239 rtpheader[1] = htonl(rtp->lastdigitts);
02240 rtpheader[2] = htonl(rtp->ssrc);
02241
02242 for (i = 0; i < 2; i++) {
02243 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
02244 res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
02245 if (res < 0)
02246 ast_log(LOG_ERROR, "RTP Transmission error to %s:%u: %s\n",
02247 ast_inet_ntoa(rtp->them.sin_addr),
02248 ntohs(rtp->them.sin_port), strerror(errno));
02249 if (rtp_debug_test_addr(&rtp->them))
02250 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
02251 ast_inet_ntoa(rtp->them.sin_addr),
02252 ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
02253
02254 rtp->seqno++;
02255
02256 rtp->send_duration += 160;
02257
02258 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
02259 }
02260
02261
02262 rtp->sending_digit = 1;
02263 rtp->send_digit = digit;
02264 rtp->send_payload = payload;
02265
02266 return 0;
02267 }
02268
02269
02270 static int ast_rtp_senddigit_continuation(struct ast_rtp *rtp)
02271 {
02272 unsigned int *rtpheader;
02273 int hdrlen = 12, res = 0;
02274 char data[256];
02275
02276 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
02277 return 0;
02278
02279
02280 rtpheader = (unsigned int *)data;
02281 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (rtp->send_payload << 16) | (rtp->seqno));
02282 rtpheader[1] = htonl(rtp->lastdigitts);
02283 rtpheader[2] = htonl(rtp->ssrc);
02284 rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
02285 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
02286
02287
02288 res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
02289 if (res < 0)
02290 ast_log(LOG_ERROR, "RTP Transmission error to %s:%d: %s\n",
02291 ast_inet_ntoa(rtp->them.sin_addr),
02292 ntohs(rtp->them.sin_port), strerror(errno));
02293 if (rtp_debug_test_addr(&rtp->them))
02294 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
02295 ast_inet_ntoa(rtp->them.sin_addr),
02296 ntohs(rtp->them.sin_port), rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
02297
02298
02299 rtp->seqno++;
02300
02301 rtp->send_duration += 160;
02302
02303 return 0;
02304 }
02305
02306
02307 int ast_rtp_senddigit_end(struct ast_rtp *rtp, char digit)
02308 {
02309 unsigned int *rtpheader;
02310 int hdrlen = 12, res = 0, i = 0;
02311 char data[256];
02312
02313
02314 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
02315 return 0;
02316
02317 if ((digit <= '9') && (digit >= '0'))
02318 digit -= '0';
02319 else if (digit == '*')
02320 digit = 10;
02321 else if (digit == '#')
02322 digit = 11;
02323 else if ((digit >= 'A') && (digit <= 'D'))
02324 digit = digit - 'A' + 12;
02325 else if ((digit >= 'a') && (digit <= 'd'))
02326 digit = digit - 'a' + 12;
02327 else {
02328 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
02329 return 0;
02330 }
02331
02332 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
02333
02334 rtpheader = (unsigned int *)data;
02335 rtpheader[1] = htonl(rtp->lastdigitts);
02336 rtpheader[2] = htonl(rtp->ssrc);
02337 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
02338
02339 rtpheader[3] |= htonl((1 << 23));
02340
02341
02342 for (i = 0; i < 3; i++) {
02343 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
02344 res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
02345 rtp->seqno++;
02346 if (res < 0)
02347 ast_log(LOG_ERROR, "RTP Transmission error to %s:%d: %s\n",
02348 ast_inet_ntoa(rtp->them.sin_addr),
02349 ntohs(rtp->them.sin_port), strerror(errno));
02350 if (rtp_debug_test_addr(&rtp->them))
02351 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
02352 ast_inet_ntoa(rtp->them.sin_addr),
02353 ntohs(rtp->them.sin_port), rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
02354 }
02355 rtp->lastts += rtp->send_duration;
02356 rtp->sending_digit = 0;
02357 rtp->send_digit = 0;
02358
02359 return res;
02360 }
02361
02362
02363 int ast_rtcp_send_h261fur(void *data)
02364 {
02365 struct ast_rtp *rtp = data;
02366 int res;
02367
02368 rtp->rtcp->sendfur = 1;
02369 res = ast_rtcp_write(data);
02370
02371 return res;
02372 }
02373
02374
02375 static int ast_rtcp_write_sr(const void *data)
02376 {
02377 struct ast_rtp *rtp = (struct ast_rtp *)data;
02378 int res;
02379 int len = 0;
02380 struct timeval now;
02381 unsigned int now_lsw;
02382 unsigned int now_msw;
02383 unsigned int *rtcpheader;
02384 unsigned int lost;
02385 unsigned int extended;
02386 unsigned int expected;
02387 unsigned int expected_interval;
02388 unsigned int received_interval;
02389 int lost_interval;
02390 int fraction;
02391 struct timeval dlsr;
02392 char bdata[512];
02393
02394
02395 if (!rtp || !rtp->rtcp)
02396 return 0;
02397
02398 if (!rtp->rtcp->them.sin_addr.s_addr) {
02399 ast_verbose("RTCP SR transmission error, rtcp halted\n");
02400 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
02401 return 0;
02402 }
02403
02404 gettimeofday(&now, NULL);
02405 timeval2ntp(now, &now_msw, &now_lsw);
02406 rtcpheader = (unsigned int *)bdata;
02407 rtcpheader[1] = htonl(rtp->ssrc);
02408 rtcpheader[2] = htonl(now_msw);
02409 rtcpheader[3] = htonl(now_lsw);
02410 rtcpheader[4] = htonl(rtp->lastts);
02411 rtcpheader[5] = htonl(rtp->txcount);
02412 rtcpheader[6] = htonl(rtp->txoctetcount);
02413 len += 28;
02414
02415 extended = rtp->cycles + rtp->lastrxseqno;
02416 expected = extended - rtp->seedrxseqno + 1;
02417 if (rtp->rxcount > expected)
02418 expected += rtp->rxcount - expected;
02419 lost = expected - rtp->rxcount;
02420 expected_interval = expected - rtp->rtcp->expected_prior;
02421 rtp->rtcp->expected_prior = expected;
02422 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
02423 rtp->rtcp->received_prior = rtp->rxcount;
02424 lost_interval = expected_interval - received_interval;
02425 if (expected_interval == 0 || lost_interval <= 0)
02426 fraction = 0;
02427 else
02428 fraction = (lost_interval << 8) / expected_interval;
02429 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
02430 rtcpheader[7] = htonl(rtp->themssrc);
02431 rtcpheader[8] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
02432 rtcpheader[9] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
02433 rtcpheader[10] = htonl((unsigned int)(rtp->rxjitter * 65536.));
02434 rtcpheader[11] = htonl(rtp->rtcp->themrxlsr);
02435 rtcpheader[12] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
02436 len += 24;
02437
02438 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SR << 16) | ((len/4)-1));
02439
02440 if (rtp->rtcp->sendfur) {
02441 rtcpheader[13] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR << 16) | 1);
02442 rtcpheader[14] = htonl(rtp->ssrc);
02443 len += 8;
02444 rtp->rtcp->sendfur = 0;
02445 }
02446
02447
02448
02449 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
02450 rtcpheader[(len/4)+1] = htonl(rtp->ssrc);
02451 rtcpheader[(len/4)+2] = htonl(0x01 << 24);
02452 len += 12;
02453
02454 res = sendto(rtp->rtcp->s, (unsigned int *)rtcpheader, len, 0, (struct sockaddr *)&rtp->rtcp->them, sizeof(rtp->rtcp->them));
02455 if (res < 0) {
02456 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));
02457 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
02458 return 0;
02459 }
02460
02461
02462 gettimeofday(&rtp->rtcp->txlsr, NULL);
02463 rtp->rtcp->sr_count++;
02464
02465 rtp->rtcp->lastsrtxcount = rtp->txcount;
02466
02467 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
02468 ast_verbose("* Sent RTCP SR to %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
02469 ast_verbose(" Our SSRC: %u\n", rtp->ssrc);
02470 ast_verbose(" Sent(NTP): %u.%010u\n", (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096);
02471 ast_verbose(" Sent(RTP): %u\n", rtp->lastts);
02472 ast_verbose(" Sent packets: %u\n", rtp->txcount);
02473 ast_verbose(" Sent octets: %u\n", rtp->txoctetcount);
02474 ast_verbose(" Report block:\n");
02475 ast_verbose(" Fraction lost: %u\n", fraction);
02476 ast_verbose(" Cumulative loss: %u\n", lost);
02477 ast_verbose(" IA jitter: %.4f\n", rtp->rxjitter);
02478 ast_verbose(" Their last SR: %u\n", rtp->rtcp->themrxlsr);
02479 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(ntohl(rtcpheader[12])/65536.0));
02480 }
02481 return res;
02482 }
02483
02484
02485 static int ast_rtcp_write_rr(const void *data)
02486 {
02487 struct ast_rtp *rtp = (struct ast_rtp *)data;
02488 int res;
02489 int len = 32;
02490 unsigned int lost;
02491 unsigned int extended;
02492 unsigned int expected;
02493 unsigned int expected_interval;
02494 unsigned int received_interval;
02495 int lost_interval;
02496 struct timeval now;
02497 unsigned int *rtcpheader;
02498 char bdata[1024];
02499 struct timeval dlsr;
02500 int fraction;
02501
02502 if (!rtp || !rtp->rtcp || (&rtp->rtcp->them.sin_addr == 0))
02503 return 0;
02504
02505 if (!rtp->rtcp->them.sin_addr.s_addr) {
02506 ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted\n");
02507 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
02508 return 0;
02509 }
02510
02511 extended = rtp->cycles + rtp->lastrxseqno;
02512 expected = extended - rtp->seedrxseqno + 1;
02513 lost = expected - rtp->rxcount;
02514 expected_interval = expected - rtp->rtcp->expected_prior;
02515 rtp->rtcp->expected_prior = expected;
02516 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
02517 rtp->rtcp->received_prior = rtp->rxcount;
02518 lost_interval = expected_interval - received_interval;
02519 if (expected_interval == 0 || lost_interval <= 0)
02520 fraction = 0;
02521 else
02522 fraction = (lost_interval << 8) / expected_interval;
02523 gettimeofday(&now, NULL);
02524 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
02525 rtcpheader = (unsigned int *)bdata;
02526 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_RR << 16) | ((len/4)-1));
02527 rtcpheader[1] = htonl(rtp->ssrc);
02528 rtcpheader[2] = htonl(rtp->themssrc);
02529 rtcpheader[3] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
02530 rtcpheader[4] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
02531 rtcpheader[5] = htonl((unsigned int)(rtp->rxjitter * 65536.));
02532 rtcpheader[6] = htonl(rtp->rtcp->themrxlsr);
02533 rtcpheader[7] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
02534
02535 if (rtp->rtcp->sendfur) {
02536 rtcpheader[8] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR << 16) | 1);
02537 rtcpheader[9] = htonl(rtp->ssrc);
02538 len += 8;
02539 rtp->rtcp->sendfur = 0;
02540 }
02541
02542
02543
02544 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
02545 rtcpheader[(len/4)+1] = htonl(rtp->ssrc);
02546 rtcpheader[(len/4)+2] = htonl(0x01 << 24);
02547 len += 12;
02548
02549 res = sendto(rtp->rtcp->s, (unsigned int *)rtcpheader, len, 0, (struct sockaddr *)&rtp->rtcp->them, sizeof(rtp->rtcp->them));
02550
02551 if (res < 0) {
02552 ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno));
02553
02554 AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
02555 return 0;
02556 }
02557
02558 rtp->rtcp->rr_count++;
02559
02560 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
02561 ast_verbose("\n* Sending RTCP RR to %s:%d\n"
02562 " Our SSRC: %u\nTheir SSRC: %u\niFraction lost: %d\nCumulative loss: %u\n"
02563 " IA jitter: %.4f\n"
02564 " Their last SR: %u\n"
02565 " DLSR: %4.4f (sec)\n\n",
02566 ast_inet_ntoa(rtp->rtcp->them.sin_addr),
02567 ntohs(rtp->rtcp->them.sin_port),
02568 rtp->ssrc, rtp->themssrc, fraction, lost,
02569 rtp->rxjitter,
02570 rtp->rtcp->themrxlsr,
02571 (double)(ntohl(rtcpheader[7])/65536.0));
02572 }
02573
02574 return res;
02575 }
02576
02577
02578
02579
02580 static int ast_rtcp_write(const void *data)
02581 {
02582 struct ast_rtp *rtp = (struct ast_rtp *)data;
02583 int res;
02584
02585 if (!rtp || !rtp->rtcp)
02586 return 0;
02587
02588 if (rtp->txcount > rtp->rtcp->lastsrtxcount)
02589 res = ast_rtcp_write_sr(data);
02590 else
02591 res = ast_rtcp_write_rr(data);
02592
02593 return res;
02594 }
02595
02596
02597 int ast_rtp_sendcng(struct ast_rtp *rtp, int level)
02598 {
02599 unsigned int *rtpheader;
02600 int hdrlen = 12;
02601 int res;
02602 int payload;
02603 char data[256];
02604 level = 127 - (level & 0x7f);
02605 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_CN);
02606
02607
02608 if (!rtp->them.sin_addr.s_addr)
02609 return 0;
02610
02611 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
02612
02613
02614 rtpheader = (unsigned int *)data;
02615 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
02616 rtpheader[1] = htonl(rtp->lastts);
02617 rtpheader[2] = htonl(rtp->ssrc);
02618 data[12] = level;
02619 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
02620 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 1, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
02621 if (res <0)
02622 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));
02623 if (rtp_debug_test_addr(&rtp->them))
02624 ast_verbose("Sent Comfort Noise RTP packet to %s:%u (type %d, seq %u, ts %u, len %d)\n"
02625 , ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);
02626
02627 }
02628 return 0;
02629 }
02630
02631 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
02632 {
02633 unsigned char *rtpheader;
02634 int hdrlen = 12;
02635 int res;
02636 unsigned int ms;
02637 int pred;
02638 int mark = 0;
02639
02640 if (rtp->sending_digit) {
02641 return 0;
02642 }
02643
02644 ms = calc_txstamp(rtp, &f->delivery);
02645
02646 if (f->frametype == AST_FRAME_VOICE) {
02647 pred = rtp->lastts + f->samples;
02648
02649
02650 rtp->lastts = rtp->lastts + ms * 8;
02651 if (ast_tvzero(f->delivery)) {
02652
02653
02654 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW)
02655 rtp->lastts = pred;
02656 else {
02657 if (option_debug > 2)
02658 ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
02659 mark = 1;
02660 }
02661 }
02662 } else if (f->frametype == AST_FRAME_VIDEO) {
02663 mark = f->subclass & 0x1;
02664 pred = rtp->lastovidtimestamp + f->samples;
02665
02666 rtp->lastts = rtp->lastts + ms * 90;
02667
02668 if (ast_tvzero(f->delivery)) {
02669 if (abs(rtp->lastts - pred) < 7200) {
02670 rtp->lastts = pred;
02671 rtp->lastovidtimestamp += f->samples;
02672 } else {
02673 if (option_debug > 2)
02674 ast_log(LOG_DEBUG, "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);
02675 rtp->lastovidtimestamp = rtp->lastts;
02676 }
02677 }
02678 }
02679
02680
02681 if (rtp->set_marker_bit) {
02682 mark = 1;
02683 rtp->set_marker_bit = 0;
02684 }
02685
02686
02687
02688
02689 if (rtp->lastts > rtp->lastdigitts)
02690 rtp->lastdigitts = rtp->lastts;
02691
02692 if (ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO))
02693 rtp->lastts = f->ts * 8;
02694
02695
02696 rtpheader = (unsigned char *)(f->data - hdrlen);
02697
02698 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
02699 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
02700 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
02701
02702 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
02703 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
02704 if (res <0) {
02705 if (!rtp->nat || (rtp->nat && (ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
02706 ast_log(LOG_DEBUG, "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));
02707 } else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) {
02708
02709 if (option_debug || rtpdebug)
02710 ast_log(LOG_DEBUG, "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));
02711 ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN);
02712 }
02713 } else {
02714 rtp->txcount++;
02715 rtp->txoctetcount +=(res - hdrlen);
02716
02717
02718 if (rtp->rtcp && rtp->rtcp->them.sin_addr.s_addr && rtp->rtcp->schedid < 1) {
02719 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
02720 }
02721 }
02722
02723 if (rtp_debug_test_addr(&rtp->them))
02724 ast_verbose("Sent RTP packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
02725 ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), codec, rtp->seqno, rtp->lastts,res - hdrlen);
02726 }
02727
02728 rtp->seqno++;
02729
02730 return 0;
02731 }
02732
02733 int ast_rtp_codec_setpref(struct ast_rtp *rtp, struct ast_codec_pref *prefs)
02734 {
02735 int x;
02736 for (x = 0; x < 32; x++) {
02737 rtp->pref.order[x] = prefs->order[x];
02738 rtp->pref.framing[x] = prefs->framing[x];
02739 }
02740 if (rtp->smoother)
02741 ast_smoother_free(rtp->smoother);
02742 rtp->smoother = NULL;
02743 return 0;
02744 }
02745
02746 struct ast_codec_pref *ast_rtp_codec_getpref(struct ast_rtp *rtp)
02747 {
02748 return &rtp->pref;
02749 }
02750
02751 int ast_rtp_codec_getformat(int pt)
02752 {
02753 if (pt < 0 || pt > MAX_RTP_PT)
02754 return 0;
02755
02756 if (static_RTP_PT[pt].isAstFormat)
02757 return static_RTP_PT[pt].code;
02758 else
02759 return 0;
02760 }
02761
02762 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
02763 {
02764 struct ast_frame *f;
02765 int codec;
02766 int hdrlen = 12;
02767 int subclass;
02768
02769
02770
02771 if (!rtp->them.sin_addr.s_addr)
02772 return 0;
02773
02774
02775 if (!_f->datalen)
02776 return 0;
02777
02778
02779 if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
02780 ast_log(LOG_WARNING, "RTP can only send voice and video\n");
02781 return -1;
02782 }
02783
02784 subclass = _f->subclass;
02785 if (_f->frametype == AST_FRAME_VIDEO)
02786 subclass &= ~0x1;
02787
02788 codec = ast_rtp_lookup_code(rtp, 1, subclass);
02789 if (codec < 0) {
02790 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
02791 return -1;
02792 }
02793
02794 if (rtp->lasttxformat != subclass) {
02795
02796 if (option_debug)
02797 ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
02798 rtp->lasttxformat = subclass;
02799 if (rtp->smoother)
02800 ast_smoother_free(rtp->smoother);
02801 rtp->smoother = NULL;
02802 }
02803
02804 if (!rtp->smoother && subclass != AST_FORMAT_SPEEX && subclass != AST_FORMAT_G723_1) {
02805 struct ast_format_list fmt = ast_codec_pref_getsize(&rtp->pref, subclass);
02806 if (fmt.inc_ms) {
02807 if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
02808 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));
02809 return -1;
02810 }
02811 if (fmt.flags)
02812 ast_smoother_set_flags(rtp->smoother, fmt.flags);
02813 if (option_debug)
02814 ast_log(LOG_DEBUG, "Created smoother: format: %d ms: %d len: %d\n", subclass, fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
02815 }
02816 }
02817 if (rtp->smoother) {
02818 if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
02819 ast_smoother_feed_be(rtp->smoother, _f);
02820 } else {
02821 ast_smoother_feed(rtp->smoother, _f);
02822 }
02823
02824 while ((f = ast_smoother_read(rtp->smoother)) && (f->data)) {
02825 if (f->subclass == AST_FORMAT_G722) {
02826
02827 f->samples /= 2;
02828 }
02829
02830 ast_rtp_raw_write(rtp, f, codec);
02831 }
02832 } else {
02833
02834 if (_f->offset < hdrlen) {
02835 f = ast_frdup(_f);
02836 } else {
02837 f = _f;
02838 }
02839 if (f->data) {
02840 if (f->subclass == AST_FORMAT_G722) {
02841
02842 f->samples /= 2;
02843 }
02844 ast_rtp_raw_write(rtp, f, codec);
02845 }
02846 if (f != _f)
02847 ast_frfree(f);
02848 }
02849
02850 return 0;
02851 }
02852
02853
02854 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
02855 {
02856 AST_LIST_LOCK(&protos);
02857 AST_LIST_REMOVE(&protos, proto, list);
02858 AST_LIST_UNLOCK(&protos);
02859 }
02860
02861
02862 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
02863 {
02864 struct ast_rtp_protocol *cur;
02865
02866 AST_LIST_LOCK(&protos);
02867 AST_LIST_TRAVERSE(&protos, cur, list) {
02868 if (!strcmp(cur->type, proto->type)) {
02869 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
02870 AST_LIST_UNLOCK(&protos);
02871 return -1;
02872 }
02873 }
02874 AST_LIST_INSERT_HEAD(&protos, proto, list);
02875 AST_LIST_UNLOCK(&protos);
02876
02877 return 0;
02878 }
02879
02880
02881 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_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)
02882 {
02883 struct ast_frame *fr = NULL;
02884 struct ast_channel *who = NULL, *other = NULL, *cs[3] = {NULL, };
02885 int oldcodec0 = codec0, oldcodec1 = codec1;
02886 struct sockaddr_in ac1 = {0,}, vac1 = {0,}, ac0 = {0,}, vac0 = {0,};
02887 struct sockaddr_in t1 = {0,}, vt1 = {0,}, t0 = {0,}, vt0 = {0,};
02888
02889
02890
02891
02892 if (!(pr0->set_rtp_peer(c0, p1, vp1, codec1, ast_test_flag(p1, FLAG_NAT_ACTIVE)))) {
02893 ast_rtp_get_peer(p1, &ac1);
02894 if (vp1)
02895 ast_rtp_get_peer(vp1, &vac1);
02896 } else
02897 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
02898
02899
02900 if (!(pr1->set_rtp_peer(c1, p0, vp0, codec0, ast_test_flag(p0, FLAG_NAT_ACTIVE)))) {
02901 ast_rtp_get_peer(p0, &ac0);
02902 if (vp0)
02903 ast_rtp_get_peer(vp0, &vac0);
02904 } else
02905 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c1->name, c0->name);
02906
02907
02908 ast_channel_unlock(c0);
02909 ast_channel_unlock(c1);
02910
02911
02912 cs[0] = c0;
02913 cs[1] = c1;
02914 cs[2] = NULL;
02915 for (;;) {
02916
02917 if ((c0->tech_pvt != pvt0) ||
02918 (c1->tech_pvt != pvt1) ||
02919 (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
02920 (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
02921 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
02922 if (c0->tech_pvt == pvt0)
02923 if (pr0->set_rtp_peer(c0, NULL, NULL, 0, 0))
02924 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
02925 if (c1->tech_pvt == pvt1)
02926 if (pr1->set_rtp_peer(c1, NULL, NULL, 0, 0))
02927 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
02928 return AST_BRIDGE_RETRY;
02929 }
02930
02931
02932 ast_rtp_get_peer(p1, &t1);
02933 if (vp1)
02934 ast_rtp_get_peer(vp1, &vt1);
02935 if (pr1->get_codec)
02936 codec1 = pr1->get_codec(c1);
02937 ast_rtp_get_peer(p0, &t0);
02938 if (vp0)
02939 ast_rtp_get_peer(vp0, &vt0);
02940 if (pr0->get_codec)
02941 codec0 = pr0->get_codec(c0);
02942 if ((inaddrcmp(&t1, &ac1)) ||
02943 (vp1 && inaddrcmp(&vt1, &vac1)) ||
02944 (codec1 != oldcodec1)) {
02945 if (option_debug > 1) {
02946 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
02947 c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port), codec1);
02948 ast_log(LOG_DEBUG, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
02949 c1->name, ast_inet_ntoa(vt1.sin_addr), ntohs(vt1.sin_port), codec1);
02950 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
02951 c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
02952 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
02953 c1->name, ast_inet_ntoa(vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
02954 }
02955 if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL, codec1, ast_test_flag(p1, FLAG_NAT_ACTIVE)))
02956 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
02957 memcpy(&ac1, &t1, sizeof(ac1));
02958 memcpy(&vac1, &vt1, sizeof(vac1));
02959 oldcodec1 = codec1;
02960 }
02961 if ((inaddrcmp(&t0, &ac0)) ||
02962 (vp0 && inaddrcmp(&vt0, &vac0))) {
02963 if (option_debug > 1) {
02964 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
02965 c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port), codec0);
02966 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
02967 c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
02968 }
02969 if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, codec0, ast_test_flag(p0, FLAG_NAT_ACTIVE)))
02970 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
02971 memcpy(&ac0, &t0, sizeof(ac0));
02972 memcpy(&vac0, &vt0, sizeof(vac0));
02973 oldcodec0 = codec0;
02974 }
02975
02976
02977 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
02978 if (!timeoutms) {
02979 if (pr0->set_rtp_peer(c0, NULL, NULL, 0, 0))
02980 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
02981 if (pr1->set_rtp_peer(c1, NULL, NULL, 0, 0))
02982 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
02983 return AST_BRIDGE_RETRY;
02984 }
02985 if (option_debug)
02986 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
02987 if (ast_check_hangup(c0) || ast_check_hangup(c1))
02988 break;
02989 continue;
02990 }
02991 fr = ast_read(who);
02992 other = (who == c0) ? c1 : c0;
02993 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
02994 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
02995 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
02996
02997 *fo = fr;
02998 *rc = who;
02999 if (option_debug)
03000 ast_log(LOG_DEBUG, "Oooh, got a %s\n", fr ? "digit" : "hangup");
03001 if (c0->tech_pvt == pvt0)
03002 if (pr0->set_rtp_peer(c0, NULL, NULL, 0, 0))
03003 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
03004 if (c1->tech_pvt == pvt1)
03005 if (pr1->set_rtp_peer(c1, NULL, NULL, 0, 0))
03006 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
03007 return AST_BRIDGE_COMPLETE;
03008 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
03009 if ((fr->subclass == AST_CONTROL_HOLD) ||
03010 (fr->subclass == AST_CONTROL_UNHOLD) ||
03011 (fr->subclass == AST_CONTROL_VIDUPDATE) ||
03012 (fr->subclass == AST_CONTROL_SRCUPDATE)) {
03013 if (fr->subclass == AST_CONTROL_HOLD) {
03014
03015 if (who == c0)
03016 pr1->set_rtp_peer(c1, NULL, NULL, 0, 0);
03017 else
03018 pr0->set_rtp_peer(c0, NULL, NULL, 0, 0);
03019 } else if (fr->subclass == AST_CONTROL_UNHOLD) {
03020
03021 if (who == c0)
03022 pr1->set_rtp_peer(c1, p0, vp0, codec0, ast_test_flag(p0, FLAG_NAT_ACTIVE));
03023 else
03024 pr0->set_rtp_peer(c0, p1, vp1, codec1, ast_test_flag(p1, FLAG_NAT_ACTIVE));
03025 }
03026
03027 ast_rtp_get_peer(p0, &t0);
03028 memcpy(&ac0, &t0, sizeof(ac0));
03029 ast_rtp_get_peer(p1, &t1);
03030 memcpy(&ac1, &t1, sizeof(ac1));
03031
03032 if (pr0->get_codec && c0->tech_pvt)
03033 oldcodec0 = codec0 = pr0->get_codec(c0);
03034 if (pr1->get_codec && c1->tech_pvt)
03035 oldcodec1 = codec1 = pr1->get_codec(c1);
03036 ast_indicate_data(other, fr->subclass, fr->data, fr->datalen);
03037 ast_frfree(fr);
03038 } else {
03039 *fo = fr;
03040 *rc = who;
03041 ast_log(LOG_DEBUG, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
03042 return AST_BRIDGE_COMPLETE;
03043 }
03044 } else {
03045 if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
03046 (fr->frametype == AST_FRAME_DTMF_END) ||
03047 (fr->frametype == AST_FRAME_VOICE) ||
03048 (fr->frametype == AST_FRAME_VIDEO) ||
03049 (fr->frametype == AST_FRAME_IMAGE) ||
03050 (fr->frametype == AST_FRAME_HTML) ||
03051 (fr->frametype == AST_FRAME_MODEM) ||
03052 (fr->frametype == AST_FRAME_TEXT)) {
03053 ast_write(other, fr);
03054 }
03055 ast_frfree(fr);
03056 }
03057
03058 cs[2] = cs[0];
03059 cs[0] = cs[1];
03060 cs[1] = cs[2];
03061 }
03062
03063 if (pr0->set_rtp_peer(c0, NULL, NULL, 0, 0))
03064 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
03065 if (pr1->set_rtp_peer(c1, NULL, NULL, 0, 0))
03066 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
03067
03068 return AST_BRIDGE_FAILED;
03069 }
03070
03071
03072 #ifdef P2P_INTENSE
03073 static int p2p_rtp_callback(int *id, int fd, short events, void *cbdata)
03074 {
03075 int res = 0, hdrlen = 12;
03076 struct sockaddr_in sin;
03077 socklen_t len;
03078 unsigned int *header;
03079 struct ast_rtp *rtp = cbdata, *bridged = NULL;
03080
03081 if (!rtp)
03082 return 1;
03083
03084 len = sizeof(sin);
03085 if ((res = recvfrom(fd, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET, 0, (struct sockaddr *)&sin, &len)) < 0)
03086 return 1;
03087
03088 header = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
03089
03090
03091 if ((rtp->nat) &&
03092 ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
03093 (rtp->them.sin_port != sin.sin_port))) {
03094 rtp->them = sin;
03095 rtp->rxseqno = 0;
03096 ast_set_flag(rtp, FLAG_NAT_ACTIVE);
03097 if (option_debug || rtpdebug)
03098 ast_log(LOG_DEBUG, "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));
03099 }
03100
03101
03102 if ((bridged = ast_rtp_get_bridged(rtp)))
03103 bridge_p2p_rtp_write(rtp, bridged, header, res, hdrlen);
03104
03105 return 1;
03106 }
03107
03108
03109 static int p2p_callback_enable(struct ast_channel *chan, struct ast_rtp *rtp, int *fds, int **iod)
03110 {
03111
03112 if (ast_test_flag(rtp, FLAG_P2P_NEED_DTMF) || ast_test_flag(rtp, FLAG_HAS_STUN) || !rtp->io)
03113 return 0;
03114
03115
03116 if (rtp->ioid) {
03117 ast_io_remove(rtp->io, rtp->ioid);
03118 rtp->ioid = NULL;
03119 }
03120
03121
03122 fds[0] = chan->fds[0];
03123 chan->fds[0] = -1;
03124
03125
03126 iod[0] = ast_io_add(rtp->io, fds[0], p2p_rtp_callback, AST_IO_IN, rtp);
03127
03128 return 1;
03129 }
03130 #else
03131 static int p2p_callback_enable(struct ast_channel *chan, struct ast_rtp *rtp, int *fds, int **iod)
03132 {
03133 return 0;
03134 }
03135 #endif
03136
03137
03138 static int p2p_callback_disable(struct ast_channel *chan, struct ast_rtp *rtp, int *fds, int **iod)
03139 {
03140 ast_channel_lock(chan);
03141
03142
03143 ast_io_remove(rtp->io, iod[0]);
03144
03145
03146 chan->fds[0] = fds[0];
03147 ast_channel_unlock(chan);
03148
03149
03150 if (ast_test_flag(rtp, FLAG_CALLBACK_MODE))
03151 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
03152
03153 return 0;
03154 }
03155
03156
03157 static void p2p_set_bridge(struct ast_rtp *rtp0, struct ast_rtp *rtp1)
03158 {
03159 ast_mutex_lock(&rtp0->bridge_lock);
03160 rtp0->bridged = rtp1;
03161 ast_mutex_unlock(&rtp0->bridge_lock);
03162
03163 return;
03164 }
03165
03166
03167 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)
03168 {
03169 struct ast_frame *fr = NULL;
03170 struct ast_channel *who = NULL, *other = NULL, *cs[3] = {NULL, };
03171 int p0_fds[2] = {-1, -1}, p1_fds[2] = {-1, -1};
03172 int *p0_iod[2] = {NULL, NULL}, *p1_iod[2] = {NULL, NULL};
03173 int p0_callback = 0, p1_callback = 0;
03174 enum ast_bridge_result res = AST_BRIDGE_FAILED;
03175
03176
03177 ast_clear_flag(p0, FLAG_P2P_SENT_MARK);
03178 p2p_set_bridge(p0, p1);
03179 ast_clear_flag(p1, FLAG_P2P_SENT_MARK);
03180 p2p_set_bridge(p1, p0);
03181
03182
03183 p0_callback = p2p_callback_enable(c0, p0, &p0_fds[0], &p0_iod[0]);
03184 p1_callback = p2p_callback_enable(c1, p1, &p1_fds[0], &p1_iod[0]);
03185
03186
03187 ast_channel_unlock(c0);
03188 ast_channel_unlock(c1);
03189
03190
03191 cs[0] = c0;
03192 cs[1] = c1;
03193 cs[2] = NULL;
03194 for (;;) {
03195
03196 if ((c0->rawreadformat != c1->rawwriteformat) || (c1->rawreadformat != c0->rawwriteformat)) {
03197 ast_log(LOG_DEBUG, "Oooh, formats changed, backing out\n");
03198 res = AST_BRIDGE_FAILED_NOWARN;
03199 break;
03200 }
03201
03202 if ((c0->tech_pvt != pvt0) ||
03203 (c1->tech_pvt != pvt1) ||
03204 (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
03205 (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
03206 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
03207 if ((c0->masq || c0->masqr) && (fr = ast_read(c0)))
03208 ast_frfree(fr);
03209 if ((c1->masq || c1->masqr) && (fr = ast_read(c1)))
03210 ast_frfree(fr);
03211 res = AST_BRIDGE_RETRY;
03212 break;
03213 }
03214
03215 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
03216 if (!timeoutms) {
03217 res = AST_BRIDGE_RETRY;
03218 break;
03219 }
03220 if (option_debug)
03221 ast_log(LOG_NOTICE, "Ooh, empty read...\n");
03222 if (ast_check_hangup(c0) || ast_check_hangup(c1))
03223 break;
03224 continue;
03225 }
03226
03227 fr = ast_read(who);
03228 other = (who == c0) ? c1 : c0;
03229
03230 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
03231 ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
03232 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
03233
03234 *fo = fr;
03235 *rc = who;
03236 if (option_debug)
03237 ast_log(LOG_DEBUG, "Oooh, got a %s\n", fr ? "digit" : "hangup");
03238 res = AST_BRIDGE_COMPLETE;
03239 break;
03240 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
03241 if ((fr->subclass == AST_CONTROL_HOLD) ||
03242 (fr->subclass == AST_CONTROL_UNHOLD) ||
03243 (fr->subclass == AST_CONTROL_VIDUPDATE) ||
03244 (fr->subclass == AST_CONTROL_SRCUPDATE)) {
03245
03246 if (fr->subclass == AST_CONTROL_HOLD) {
03247 if (p0_callback)
03248 p0_callback = p2p_callback_disable(c0, p0, &p0_fds[0], &p0_iod[0]);
03249 if (p1_callback)
03250 p1_callback = p2p_callback_disable(c1, p1, &p1_fds[0], &p1_iod[0]);
03251 p2p_set_bridge(p0, NULL);
03252 p2p_set_bridge(p1, NULL);
03253 } else if (fr->subclass == AST_CONTROL_UNHOLD) {
03254
03255 ast_clear_flag(p0, FLAG_P2P_SENT_MARK);
03256 p2p_set_bridge(p0, p1);
03257 ast_clear_flag(p1, FLAG_P2P_SENT_MARK);
03258 p2p_set_bridge(p1, p0);
03259 p0_callback = p2p_callback_enable(c0, p0, &p0_fds[0], &p0_iod[0]);
03260 p1_callback = p2p_callback_enable(c1, p1, &p1_fds[0], &p1_iod[0]);
03261 }
03262 ast_indicate_data(other, fr->subclass, fr->data, fr->datalen);
03263 ast_frfree(fr);
03264 } else {
03265 *fo = fr;
03266 *rc = who;
03267 ast_log(LOG_DEBUG, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
03268 res = AST_BRIDGE_COMPLETE;
03269 break;
03270 }
03271 } else {
03272 if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
03273 (fr->frametype == AST_FRAME_DTMF_END) ||
03274 (fr->frametype == AST_FRAME_VOICE) ||
03275 (fr->frametype == AST_FRAME_VIDEO) ||
03276 (fr->frametype == AST_FRAME_IMAGE) ||
03277 (fr->frametype == AST_FRAME_HTML) ||
03278 (fr->frametype == AST_FRAME_MODEM) ||
03279 (fr->frametype == AST_FRAME_TEXT)) {
03280 ast_write(other, fr);
03281 }
03282
03283 ast_frfree(fr);
03284 }
03285
03286 cs[2] = cs[0];
03287 cs[0] = cs[1];
03288 cs[1] = cs[2];
03289 }
03290
03291
03292 if (p0_callback)
03293 p0_callback = p2p_callback_disable(c0, p0, &p0_fds[0], &p0_iod[0]);
03294 if (p1_callback)
03295 p1_callback = p2p_callback_disable(c1, p1, &p1_fds[0], &p1_iod[0]);
03296
03297
03298 p2p_set_bridge(p0, NULL);
03299 p2p_set_bridge(p1, NULL);
03300
03301 return res;
03302 }
03303
03304
03305
03306
03307 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)
03308 {
03309 struct ast_rtp *p0 = NULL, *p1 = NULL;
03310 struct ast_rtp *vp0 = NULL, *vp1 = NULL;
03311 struct ast_rtp_protocol *pr0 = NULL, *pr1 = NULL;
03312 enum ast_rtp_get_result audio_p0_res = AST_RTP_GET_FAILED, video_p0_res = AST_RTP_GET_FAILED;
03313 enum ast_rtp_get_result audio_p1_res = AST_RTP_GET_FAILED, video_p1_res = AST_RTP_GET_FAILED;
03314 enum ast_bridge_result res = AST_BRIDGE_FAILED;
03315 int codec0 = 0, codec1 = 0;
03316 void *pvt0 = NULL, *pvt1 = NULL;
03317
03318
03319 ast_channel_lock(c0);
03320 while(ast_channel_trylock(c1)) {
03321 ast_channel_unlock(c0);
03322 usleep(1);
03323 ast_channel_lock(c0);
03324 }
03325
03326
03327 if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
03328 ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
03329 ast_channel_unlock(c0);
03330 ast_channel_unlock(c1);
03331 return AST_BRIDGE_FAILED;
03332 }
03333
03334
03335 if (!(pr0 = get_proto(c0))) {
03336 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
03337 ast_channel_unlock(c0);
03338 ast_channel_unlock(c1);
03339 return AST_BRIDGE_FAILED;
03340 }
03341 if (!(pr1 = get_proto(c1))) {
03342 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
03343 ast_channel_unlock(c0);
03344 ast_channel_unlock(c1);
03345 return AST_BRIDGE_FAILED;
03346 }
03347
03348
03349 pvt0 = c0->tech_pvt;
03350 pvt1 = c1->tech_pvt;
03351
03352
03353 audio_p0_res = pr0->get_rtp_info(c0, &p0);
03354 video_p0_res = pr0->get_vrtp_info ? pr0->get_vrtp_info(c0, &vp0) : AST_RTP_GET_FAILED;
03355 audio_p1_res = pr1->get_rtp_info(c1, &p1);
03356 video_p1_res = pr1->get_vrtp_info ? pr1->get_vrtp_info(c1, &vp1) : AST_RTP_GET_FAILED;
03357
03358
03359 if (video_p0_res != AST_RTP_GET_FAILED && (audio_p0_res != AST_RTP_TRY_NATIVE || video_p0_res != AST_RTP_TRY_NATIVE))
03360 audio_p0_res = AST_RTP_GET_FAILED;
03361 if (video_p1_res != AST_RTP_GET_FAILED && (audio_p1_res != AST_RTP_TRY_NATIVE || video_p1_res != AST_RTP_TRY_NATIVE))
03362 audio_p1_res = AST_RTP_GET_FAILED;
03363
03364
03365 if (audio_p0_res == AST_RTP_GET_FAILED || audio_p1_res == AST_RTP_GET_FAILED) {
03366
03367 ast_channel_unlock(c0);
03368 ast_channel_unlock(c1);
03369 return AST_BRIDGE_FAILED_NOWARN;
03370 }
03371
03372
03373 if (ast_test_flag(p0, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
03374 ast_set_flag(p0, FLAG_P2P_NEED_DTMF);
03375 audio_p0_res = AST_RTP_TRY_PARTIAL;
03376 }
03377
03378 if (ast_test_flag(p1, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)) {
03379 ast_set_flag(p1, FLAG_P2P_NEED_DTMF);
03380 audio_p1_res = AST_RTP_TRY_PARTIAL;
03381 }
03382
03383
03384
03385
03386
03387
03388
03389
03390
03391
03392
03393
03394
03395
03396 if ( (ast_test_flag(p0, FLAG_HAS_DTMF) != ast_test_flag(p1, FLAG_HAS_DTMF)) ||
03397 (!c0->tech->send_digit_begin != !c1->tech->send_digit_begin)) {
03398 if (!ast_test_flag(p0, FLAG_P2P_NEED_DTMF) || !ast_test_flag(p1, FLAG_P2P_NEED_DTMF)) {
03399 ast_channel_unlock(c0);
03400 ast_channel_unlock(c1);
03401 return AST_BRIDGE_FAILED_NOWARN;
03402 }
03403 audio_p0_res = AST_RTP_TRY_PARTIAL;
03404 audio_p1_res = AST_RTP_TRY_PARTIAL;
03405 }
03406
03407
03408 if ((audio_p0_res == AST_RTP_TRY_PARTIAL && ast_test_flag(p0, FLAG_P2P_NEED_DTMF)) ||
03409 (audio_p1_res == AST_RTP_TRY_PARTIAL && ast_test_flag(p1, FLAG_P2P_NEED_DTMF))) {
03410 ast_channel_unlock(c0);
03411 ast_channel_unlock(c1);
03412 return AST_BRIDGE_FAILED_NOWARN;
03413 }
03414
03415
03416 codec0 = pr0->get_codec ? pr0->get_codec(c0) : 0;
03417 codec1 = pr1->get_codec ? pr1->get_codec(c1) : 0;
03418 if (codec0 && codec1 && !(codec0 & codec1)) {
03419
03420 if (option_debug)
03421 ast_log(LOG_DEBUG, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0, codec1);
03422 ast_channel_unlock(c0);
03423 ast_channel_unlock(c1);
03424 return AST_BRIDGE_FAILED_NOWARN;
03425 }
03426
03427
03428 if (audio_p0_res == AST_RTP_TRY_PARTIAL || audio_p1_res == AST_RTP_TRY_PARTIAL) {
03429 struct ast_format_list fmt0, fmt1;
03430
03431
03432 if (c0->rawreadformat != c1->rawwriteformat || c1->rawreadformat != c0->rawwriteformat) {
03433 if (option_debug)
03434 ast_log(LOG_DEBUG, "Cannot packet2packet bridge - raw formats are incompatible\n");
03435 ast_channel_unlock(c0);
03436 ast_channel_unlock(c1);
03437 return AST_BRIDGE_FAILED_NOWARN;
03438 }
03439
03440 fmt0 = ast_codec_pref_getsize(&p0->pref, c0->rawreadformat);
03441 fmt1 = ast_codec_pref_getsize(&p1->pref, c1->rawreadformat);
03442 if (fmt0.cur_ms != fmt1.cur_ms) {
03443 if (option_debug)
03444 ast_log(LOG_DEBUG, "Cannot packet2packet bridge - packetization settings prevent it\n");
03445 ast_channel_unlock(c0);
03446 ast_channel_unlock(c1);
03447 return AST_BRIDGE_FAILED_NOWARN;
03448 }
03449
03450 if (option_verbose > 2)
03451 ast_verbose(VERBOSE_PREFIX_3 "Packet2Packet bridging %s and %s\n", c0->name, c1->name);
03452 res = bridge_p2p_loop(c0, c1, p0, p1, timeoutms, flags, fo, rc, pvt0, pvt1);
03453 } else {
03454 if (option_verbose > 2)
03455 ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name);
03456 res = bridge_native_loop(c0, c1, p0, p1, vp0, vp1, pr0, pr1, codec0, codec1, timeoutms, flags, fo, rc, pvt0, pvt1);
03457 }
03458
03459 return res;
03460 }
03461
03462 static int rtp_do_debug_ip(int fd, int argc, char *argv[])
03463 {
03464 struct hostent *hp;
03465 struct ast_hostent ahp;
03466 int port = 0;
03467 char *p, *arg;
03468
03469 if (argc != 4)
03470 return RESULT_SHOWUSAGE;
03471 arg = argv[3];
03472 p = strstr(arg, ":");
03473 if (p) {
03474 *p = '\0';
03475 p++;
03476 port = atoi(p);
03477 }
03478 hp = ast_gethostbyname(arg, &ahp);
03479 if (hp == NULL)
03480 return RESULT_SHOWUSAGE;
03481 rtpdebugaddr.sin_family = AF_INET;
03482 memcpy(&rtpdebugaddr.sin_addr, hp->h_addr, sizeof(rtpdebugaddr.sin_addr));
03483 rtpdebugaddr.sin_port = htons(port);
03484 if (port == 0)
03485 ast_cli(fd, "RTP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtpdebugaddr.sin_addr));
03486 else
03487 ast_cli(fd, "RTP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtpdebugaddr.sin_addr), port);
03488 rtpdebug = 1;
03489 return RESULT_SUCCESS;
03490 }
03491
03492 static int rtcp_do_debug_ip_deprecated(int fd, int argc, char *argv[])
03493 {
03494 struct hostent *hp;
03495 struct ast_hostent ahp;
03496 int port = 0;
03497 char *p, *arg;
03498 if (argc != 5)
03499 return RESULT_SHOWUSAGE;
03500
03501 arg = argv[4];
03502 p = strstr(arg, ":");
03503 if (p) {
03504 *p = '\0';
03505 p++;
03506 port = atoi(p);
03507 }
03508 hp = ast_gethostbyname(arg, &ahp);
03509 if (hp == NULL)
03510 return RESULT_SHOWUSAGE;
03511 rtcpdebugaddr.sin_family = AF_INET;
03512 memcpy(&rtcpdebugaddr.sin_addr, hp->h_addr, sizeof(rtcpdebugaddr.sin_addr));
03513 rtcpdebugaddr.sin_port = htons(port);
03514 if (port == 0)
03515 ast_cli(fd, "RTCP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtcpdebugaddr.sin_addr));
03516 else
03517 ast_cli(fd, "RTCP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtcpdebugaddr.sin_addr), port);
03518 rtcpdebug = 1;
03519 return RESULT_SUCCESS;
03520 }
03521
03522 static int rtcp_do_debug_ip(int fd, int argc, char *argv[])
03523 {
03524 struct hostent *hp;
03525 struct ast_hostent ahp;
03526 int port = 0;
03527 char *p, *arg;
03528 if (argc != 4)
03529 return RESULT_SHOWUSAGE;
03530
03531 arg = argv[3];
03532 p = strstr(arg, ":");
03533 if (p) {
03534 *p = '\0';
03535 p++;
03536 port = atoi(p);
03537 }
03538 hp = ast_gethostbyname(arg, &ahp);
03539 if (hp == NULL)
03540 return RESULT_SHOWUSAGE;
03541 rtcpdebugaddr.sin_family = AF_INET;
03542 memcpy(&rtcpdebugaddr.sin_addr, hp->h_addr, sizeof(rtcpdebugaddr.sin_addr));
03543 rtcpdebugaddr.sin_port = htons(port);
03544 if (port == 0)
03545 ast_cli(fd, "RTCP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtcpdebugaddr.sin_addr));
03546 else
03547 ast_cli(fd, "RTCP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtcpdebugaddr.sin_addr), port);
03548 rtcpdebug = 1;
03549 return RESULT_SUCCESS;
03550 }
03551
03552 static int rtp_do_debug(int fd, int argc, char *argv[])
03553 {
03554 if (argc != 2) {
03555 if (argc != 4)
03556 return RESULT_SHOWUSAGE;
03557 return rtp_do_debug_ip(fd, argc, argv);
03558 }
03559 rtpdebug = 1;
03560 memset(&rtpdebugaddr,0,sizeof(rtpdebugaddr));
03561 ast_cli(fd, "RTP Debugging Enabled\n");
03562 return RESULT_SUCCESS;
03563 }
03564
03565 static int rtcp_do_debug_deprecated(int fd, int argc, char *argv[]) {
03566 if (argc != 3) {
03567 if (argc != 5)
03568 return RESULT_SHOWUSAGE;
03569 return rtcp_do_debug_ip_deprecated(fd, argc, argv);
03570 }
03571 rtcpdebug = 1;
03572 memset(&rtcpdebugaddr,0,sizeof(rtcpdebugaddr));
03573 ast_cli(fd, "RTCP Debugging Enabled\n");
03574 return RESULT_SUCCESS;
03575 }
03576
03577 static int rtcp_do_debug(int fd, int argc, char *argv[]) {
03578 if (argc != 2) {
03579 if (argc != 4)
03580 return RESULT_SHOWUSAGE;
03581 return rtcp_do_debug_ip(fd, argc, argv);
03582 }
03583 rtcpdebug = 1;
03584 memset(&rtcpdebugaddr,0,sizeof(rtcpdebugaddr));
03585 ast_cli(fd, "RTCP Debugging Enabled\n");
03586 return RESULT_SUCCESS;
03587 }
03588
03589 static int rtcp_do_stats_deprecated(int fd, int argc, char *argv[]) {
03590 if (argc != 3) {
03591 return RESULT_SHOWUSAGE;
03592 }
03593 rtcpstats = 1;
03594 ast_cli(fd, "RTCP Stats Enabled\n");
03595 return RESULT_SUCCESS;
03596 }
03597
03598 static int rtcp_do_stats(int fd, int argc, char *argv[]) {
03599 if (argc != 2) {
03600 return RESULT_SHOWUSAGE;
03601 }
03602 rtcpstats = 1;
03603 ast_cli(fd, "RTCP Stats Enabled\n");
03604 return RESULT_SUCCESS;
03605 }
03606
03607 static int rtp_no_debug(int fd, int argc, char *argv[])
03608 {
03609 if (argc != 3)
03610 return RESULT_SHOWUSAGE;
03611 rtpdebug = 0;
03612 ast_cli(fd,"RTP Debugging Disabled\n");
03613 return RESULT_SUCCESS;
03614 }
03615
03616 static int rtcp_no_debug_deprecated(int fd, int argc, char *argv[])
03617 {
03618 if (argc != 4)
03619 return RESULT_SHOWUSAGE;
03620 rtcpdebug = 0;
03621 ast_cli(fd,"RTCP Debugging Disabled\n");
03622 return RESULT_SUCCESS;
03623 }
03624
03625 static int rtcp_no_debug(int fd, int argc, char *argv[])
03626 {
03627 if (argc != 3)
03628 return RESULT_SHOWUSAGE;
03629 rtcpdebug = 0;
03630 ast_cli(fd,"RTCP Debugging Disabled\n");
03631 return RESULT_SUCCESS;
03632 }
03633
03634 static int rtcp_no_stats_deprecated(int fd, int argc, char *argv[])
03635 {
03636 if (argc != 4)
03637 return RESULT_SHOWUSAGE;
03638 rtcpstats = 0;
03639 ast_cli(fd,"RTCP Stats Disabled\n");
03640 return RESULT_SUCCESS;
03641 }
03642
03643 static int rtcp_no_stats(int fd, int argc, char *argv[])
03644 {
03645 if (argc != 3)
03646 return RESULT_SHOWUSAGE;
03647 rtcpstats = 0;
03648 ast_cli(fd,"RTCP Stats Disabled\n");
03649 return RESULT_SUCCESS;
03650 }
03651
03652 static int stun_do_debug(int fd, int argc, char *argv[])
03653 {
03654 if (argc != 2) {
03655 return RESULT_SHOWUSAGE;
03656 }
03657 stundebug = 1;
03658 ast_cli(fd, "STUN Debugging Enabled\n");
03659 return RESULT_SUCCESS;
03660 }
03661
03662 static int stun_no_debug(int fd, int argc, char *argv[])
03663 {
03664 if (argc != 3)
03665 return RESULT_SHOWUSAGE;
03666 stundebug = 0;
03667 ast_cli(fd, "STUN Debugging Disabled\n");
03668 return RESULT_SUCCESS;
03669 }
03670
03671 static char debug_usage[] =
03672 "Usage: rtp debug [ip host[:port]]\n"
03673 " Enable dumping of all RTP packets to and from host.\n";
03674
03675 static char no_debug_usage[] =
03676 "Usage: rtp debug off\n"
03677 " Disable all RTP debugging\n";
03678
03679 static char stun_debug_usage[] =
03680 "Usage: stun debug\n"
03681 " Enable STUN (Simple Traversal of UDP through NATs) debugging\n";
03682
03683 static char stun_no_debug_usage[] =
03684 "Usage: stun debug off\n"
03685 " Disable STUN debugging\n";
03686
03687 static char rtcp_debug_usage[] =
03688 "Usage: rtcp debug [ip host[:port]]\n"
03689 " Enable dumping of all RTCP packets to and from host.\n";
03690
03691 static char rtcp_no_debug_usage[] =
03692 "Usage: rtcp debug off\n"
03693 " Disable all RTCP debugging\n";
03694
03695 static char rtcp_stats_usage[] =
03696 "Usage: rtcp stats\n"
03697 " Enable dumping of RTCP stats.\n";
03698
03699 static char rtcp_no_stats_usage[] =
03700 "Usage: rtcp stats off\n"
03701 " Disable all RTCP stats\n";
03702
03703 static struct ast_cli_entry cli_rtp_no_debug_deprecated = {
03704 { "rtp", "no", "debug", NULL },
03705 rtp_no_debug, NULL,
03706 NULL };
03707
03708 static struct ast_cli_entry cli_rtp_rtcp_debug_ip_deprecated = {
03709 { "rtp", "rtcp", "debug", "ip", NULL },
03710 rtcp_do_debug_deprecated, NULL,
03711 NULL };
03712
03713 static struct ast_cli_entry cli_rtp_rtcp_debug_deprecated = {
03714 { "rtp", "rtcp", "debug", NULL },
03715 rtcp_do_debug_deprecated, NULL,
03716 NULL };
03717
03718 static struct ast_cli_entry cli_rtp_rtcp_no_debug_deprecated = {
03719 { "rtp", "rtcp", "no", "debug", NULL },
03720 rtcp_no_debug_deprecated, NULL,
03721 NULL };
03722
03723 static struct ast_cli_entry cli_rtp_rtcp_stats_deprecated = {
03724 { "rtp", "rtcp", "stats", NULL },
03725 rtcp_do_stats_deprecated, NULL,
03726 NULL };
03727
03728 static struct ast_cli_entry cli_rtp_rtcp_no_stats_deprecated = {
03729 { "rtp", "rtcp", "no", "stats", NULL },
03730 rtcp_no_stats_deprecated, NULL,
03731 NULL };
03732
03733 static struct ast_cli_entry cli_stun_no_debug_deprecated = {
03734 { "stun", "no", "debug", NULL },
03735 stun_no_debug, NULL,
03736 NULL };
03737
03738 static struct ast_cli_entry cli_rtp[] = {
03739 { { "rtp", "debug", "ip", NULL },
03740 rtp_do_debug, "Enable RTP debugging on IP",
03741 debug_usage },
03742
03743 { { "rtp", "debug", NULL },
03744 rtp_do_debug, "Enable RTP debugging",
03745 debug_usage },
03746
03747 { { "rtp", "debug", "off", NULL },
03748 rtp_no_debug, "Disable RTP debugging",
03749 no_debug_usage, NULL, &cli_rtp_no_debug_deprecated },
03750
03751 { { "rtcp", "debug", "ip", NULL },
03752 rtcp_do_debug, "Enable RTCP debugging on IP",
03753 rtcp_debug_usage, NULL, &cli_rtp_rtcp_debug_ip_deprecated },
03754
03755 { { "rtcp", "debug", NULL },
03756 rtcp_do_debug, "Enable RTCP debugging",
03757 rtcp_debug_usage, NULL, &cli_rtp_rtcp_debug_deprecated },
03758
03759 { { "rtcp", "debug", "off", NULL },
03760 rtcp_no_debug, "Disable RTCP debugging",
03761 rtcp_no_debug_usage, NULL, &cli_rtp_rtcp_no_debug_deprecated },
03762
03763 { { "rtcp", "stats", NULL },
03764 rtcp_do_stats, "Enable RTCP stats",
03765 rtcp_stats_usage, NULL, &cli_rtp_rtcp_stats_deprecated },
03766
03767 { { "rtcp", "stats", "off", NULL },
03768 rtcp_no_stats, "Disable RTCP stats",
03769 rtcp_no_stats_usage, NULL, &cli_rtp_rtcp_no_stats_deprecated },
03770
03771 { { "stun", "debug", NULL },
03772 stun_do_debug, "Enable STUN debugging",
03773 stun_debug_usage },
03774
03775 { { "stun", "debug", "off", NULL },
03776 stun_no_debug, "Disable STUN debugging",
03777 stun_no_debug_usage, NULL, &cli_stun_no_debug_deprecated },
03778 };
03779
03780 int ast_rtp_reload(void)
03781 {
03782 struct ast_config *cfg;
03783 const char *s;
03784
03785 rtpstart = 5000;
03786 rtpend = 31000;
03787 dtmftimeout = DEFAULT_DTMF_TIMEOUT;
03788 cfg = ast_config_load("rtp.conf");
03789 if (cfg) {
03790 if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
03791 rtpstart = atoi(s);
03792 if (rtpstart < 1024)
03793 rtpstart = 1024;
03794 if (rtpstart > 65535)
03795 rtpstart = 65535;
03796 }
03797 if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
03798 rtpend = atoi(s);
03799 if (rtpend < 1024)
03800 rtpend = 1024;
03801 if (rtpend > 65535)
03802 rtpend = 65535;
03803 }
03804 if ((s = ast_variable_retrieve(cfg, "general", "rtcpinterval"))) {
03805 rtcpinterval = atoi(s);
03806 if (rtcpinterval == 0)
03807 rtcpinterval = 0;
03808 if (rtcpinterval < RTCP_MIN_INTERVALMS)
03809 rtcpinterval = RTCP_MIN_INTERVALMS;
03810 if (rtcpinterval > RTCP_MAX_INTERVALMS)
03811 rtcpinterval = RTCP_MAX_INTERVALMS;
03812 }
03813 if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
03814 #ifdef SO_NO_CHECK
03815 if (ast_false(s))
03816 nochecksums = 1;
03817 else
03818 nochecksums = 0;
03819 #else
03820 if (ast_false(s))
03821 ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
03822 #endif
03823 }
03824 if ((s = ast_variable_retrieve(cfg, "general", "dtmftimeout"))) {
03825 dtmftimeout = atoi(s);
03826 if ((dtmftimeout < 0) || (dtmftimeout > 20000)) {
03827 ast_log(LOG_WARNING, "DTMF timeout of '%d' outside range, using default of '%d' instead\n",
03828 dtmftimeout, DEFAULT_DTMF_TIMEOUT);
03829 dtmftimeout = DEFAULT_DTMF_TIMEOUT;
03830 };
03831 }
03832 ast_config_destroy(cfg);
03833 }
03834 if (rtpstart >= rtpend) {
03835 ast_log(LOG_WARNING, "Unreasonable values for RTP start/end port in rtp.conf\n");
03836 rtpstart = 5000;
03837 rtpend = 31000;
03838 }
03839 if (option_verbose > 1)
03840 ast_verbose(VERBOSE_PREFIX_2 "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
03841 return 0;
03842 }
03843
03844
03845 void ast_rtp_init(void)
03846 {
03847 ast_cli_register_multiple(cli_rtp, sizeof(cli_rtp) / sizeof(struct ast_cli_entry));
03848 ast_rtp_reload();
03849 }
03850