Wed Aug 18 22:33:55 2010

Asterisk developer's documentation


rtp.c

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

Generated on Wed Aug 18 22:33:56 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7