Fri Aug 24 02:27:23 2007

Asterisk developer's documentation


rtp.h File Reference

Supports RTP and RTCP with Symmetric RTP support for NAT traversal. More...

#include <netinet/in.h>
#include "asterisk/frame.h"
#include "asterisk/io.h"
#include "asterisk/sched.h"
#include "asterisk/channel.h"
#include "asterisk/linkedlists.h"

Include dependency graph for rtp.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_rtp_protocol
struct  ast_rtp_quality

Defines

#define AST_RTP_CISCO_DTMF   (1 << 2)
#define AST_RTP_CN   (1 << 1)
#define AST_RTP_DTMF   (1 << 0)
#define AST_RTP_MAX   AST_RTP_CISCO_DTMF
#define FLAG_3389_WARNING   (1 << 0)
#define MAX_RTP_PT   256

Typedefs

typedef int(*) ast_rtp_callback (struct ast_rtp *rtp, struct ast_frame *f, void *data)

Enumerations

enum  ast_rtp_get_result { AST_RTP_GET_FAILED = 0, AST_RTP_TRY_PARTIAL, AST_RTP_TRY_NATIVE }
enum  ast_rtp_options { AST_RTP_OPT_G726_NONSTANDARD = (1 << 0) }

Functions

int ast_rtcp_fd (struct ast_rtp *rtp)
ast_frameast_rtcp_read (struct ast_rtp *rtp)
int ast_rtcp_send_h261fur (void *data)
 Send an H.261 fast update request. Some devices need this rather than the XML message in SIP.
size_t ast_rtp_alloc_size (void)
 Get the amount of space required to hold an RTP session.
int ast_rtp_bridge (struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
 Bridge calls. If possible and allowed, initiate re-invite so the peers exchange media directly outside of Asterisk.
int ast_rtp_codec_getformat (int pt)
ast_codec_prefast_rtp_codec_getpref (struct ast_rtp *rtp)
int ast_rtp_codec_setpref (struct ast_rtp *rtp, struct ast_codec_pref *prefs)
void ast_rtp_destroy (struct ast_rtp *rtp)
int ast_rtp_early_bridge (struct ast_channel *dest, struct ast_channel *src)
 If possible, create an early bridge directly between the devices without having to send a re-invite later.
int ast_rtp_fd (struct ast_rtp *rtp)
ast_rtpast_rtp_get_bridged (struct ast_rtp *rtp)
void ast_rtp_get_current_formats (struct ast_rtp *rtp, int *astFormats, int *nonAstFormats)
 Return the union of all of the codecs that were set by rtp_set...() calls They're returned as two distinct sets: AST_FORMATs, and AST_RTPs.
int ast_rtp_get_peer (struct ast_rtp *rtp, struct sockaddr_in *them)
char * ast_rtp_get_quality (struct ast_rtp *rtp, struct ast_rtp_quality *qual)
 Return RTCP quality string.
int ast_rtp_get_rtpholdtimeout (struct ast_rtp *rtp)
 Get rtp hold timeout.
int ast_rtp_get_rtpkeepalive (struct ast_rtp *rtp)
 Get RTP keepalive interval.
int ast_rtp_get_rtptimeout (struct ast_rtp *rtp)
 Get rtp timeout.
void ast_rtp_get_us (struct ast_rtp *rtp, struct sockaddr_in *us)
int ast_rtp_getnat (struct ast_rtp *rtp)
void ast_rtp_init (void)
 Initialize the RTP system in Asterisk.
int ast_rtp_lookup_code (struct ast_rtp *rtp, int isAstFormat, int code)
 Looks up an RTP code out of our *static* outbound list.
char * ast_rtp_lookup_mime_multiple (char *buf, size_t size, const int capability, const int isAstFormat, enum ast_rtp_options options)
 Build a string of MIME subtype names from a capability list.
const char * ast_rtp_lookup_mime_subtype (int isAstFormat, int code, enum ast_rtp_options options)
 Mapping an Asterisk code into a MIME subtype (string):.
rtpPayloadType ast_rtp_lookup_pt (struct ast_rtp *rtp, int pt)
 Mapping between RTP payload format codes and Asterisk codes:.
int ast_rtp_make_compatible (struct ast_channel *dest, struct ast_channel *src, int media)
ast_rtpast_rtp_new (struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
 Initializate a RTP session.
void ast_rtp_new_init (struct ast_rtp *rtp)
 Initialize a new RTP structure.
ast_rtpast_rtp_new_with_bindaddr (struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr in)
 Initializate a RTP session using an in_addr structure.
int ast_rtp_proto_register (struct ast_rtp_protocol *proto)
 Register interface to channel driver.
void ast_rtp_proto_unregister (struct ast_rtp_protocol *proto)
 Unregister interface to channel driver.
void ast_rtp_pt_clear (struct ast_rtp *rtp)
 Setting RTP payload types from lines in a SDP description:.
void ast_rtp_pt_copy (struct ast_rtp *dest, struct ast_rtp *src)
 Copy payload types between RTP structures.
void ast_rtp_pt_default (struct ast_rtp *rtp)
 Set payload types to defaults.
ast_frameast_rtp_read (struct ast_rtp *rtp)
int ast_rtp_reload (void)
void ast_rtp_reset (struct ast_rtp *rtp)
int ast_rtp_sendcng (struct ast_rtp *rtp, int level)
 generate comfort noice (CNG)
int ast_rtp_senddigit_begin (struct ast_rtp *rtp, char digit)
 Send begin frames for DTMF.
int ast_rtp_senddigit_end (struct ast_rtp *rtp, char digit)
void ast_rtp_set_callback (struct ast_rtp *rtp, ast_rtp_callback callback)
void ast_rtp_set_data (struct ast_rtp *rtp, void *data)
void ast_rtp_set_m_type (struct ast_rtp *rtp, int pt)
 Make a note of a RTP payload type that was seen in a SDP "m=" line. By default, use the well-known value for this type (although it may still be set to a different value by a subsequent "a=rtpmap:" line).
void ast_rtp_set_peer (struct ast_rtp *rtp, struct sockaddr_in *them)
void ast_rtp_set_rtpholdtimeout (struct ast_rtp *rtp, int timeout)
 Set rtp hold timeout.
void ast_rtp_set_rtpkeepalive (struct ast_rtp *rtp, int period)
 set RTP keepalive interval
void ast_rtp_set_rtpmap_type (struct ast_rtp *rtp, int pt, char *mimeType, char *mimeSubtype, enum ast_rtp_options options)
 Make a note of a RTP payload type (with MIME type) that was seen in an SDP "a=rtpmap:" line.
void ast_rtp_set_rtptimeout (struct ast_rtp *rtp, int timeout)
 Set rtp timeout.
void ast_rtp_set_rtptimers_onhold (struct ast_rtp *rtp)
void ast_rtp_setdtmf (struct ast_rtp *rtp, int dtmf)
 Indicate whether this RTP session is carrying DTMF or not.
void ast_rtp_setdtmfcompensate (struct ast_rtp *rtp, int compensate)
 Compensate for devices that send RFC2833 packets all at once.
void ast_rtp_setnat (struct ast_rtp *rtp, int nat)
void ast_rtp_setstun (struct ast_rtp *rtp, int stun_enable)
 Enable STUN capability.
int ast_rtp_settos (struct ast_rtp *rtp, int tos)
void ast_rtp_stop (struct ast_rtp *rtp)
void ast_rtp_stun_request (struct ast_rtp *rtp, struct sockaddr_in *suggestion, const char *username)
int ast_rtp_write (struct ast_rtp *rtp, struct ast_frame *f)


Detailed Description

Supports RTP and RTCP with Symmetric RTP support for NAT traversal.

RTP is defined in RFC 3550.

Definition in file rtp.h.


Define Documentation

#define AST_RTP_CISCO_DTMF   (1 << 2)

DTMF (Cisco Proprietary)

Definition at line 47 of file rtp.h.

Referenced by ast_rtp_read().

#define AST_RTP_CN   (1 << 1)

'Comfort Noise' (RFC3389)

Definition at line 45 of file rtp.h.

Referenced by ast_rtp_read(), and ast_rtp_sendcng().

#define AST_RTP_DTMF   (1 << 0)

DTMF (RFC2833)

Definition at line 43 of file rtp.h.

Referenced by add_noncodec_to_sdp(), ast_rtp_read(), ast_rtp_senddigit_begin(), bridge_p2p_rtp_write(), check_user_full(), create_addr(), create_addr_from_peer(), oh323_alloc(), oh323_request(), process_sdp(), sip_alloc(), and sip_dtmfmode().

#define AST_RTP_MAX   AST_RTP_CISCO_DTMF

Maximum RTP-specific code

Definition at line 49 of file rtp.h.

Referenced by add_sdp(), and ast_rtp_lookup_mime_multiple().

#define FLAG_3389_WARNING   (1 << 0)

Definition at line 93 of file rtp.h.

#define MAX_RTP_PT   256

Definition at line 51 of file rtp.h.

Referenced by ast_rtp_get_current_formats(), ast_rtp_lookup_code(), ast_rtp_lookup_pt(), ast_rtp_pt_clear(), ast_rtp_pt_copy(), ast_rtp_pt_default(), ast_rtp_set_m_type(), and ast_rtp_set_rtpmap_type().


Typedef Documentation

typedef int(*) ast_rtp_callback(struct ast_rtp *rtp, struct ast_frame *f, void *data)

Definition at line 95 of file rtp.h.


Enumeration Type Documentation

enum ast_rtp_get_result

Enumerator:
AST_RTP_GET_FAILED  Failed to find the RTP structure
AST_RTP_TRY_PARTIAL  RTP structure exists but true native bridge can not occur so try partial
AST_RTP_TRY_NATIVE  RTP structure exists and native bridge can occur

Definition at line 57 of file rtp.h.

00057                         {
00058    /*! Failed to find the RTP structure */
00059    AST_RTP_GET_FAILED = 0,
00060    /*! RTP structure exists but true native bridge can not occur so try partial */
00061    AST_RTP_TRY_PARTIAL,
00062    /*! RTP structure exists and native bridge can occur */
00063    AST_RTP_TRY_NATIVE,
00064 };

enum ast_rtp_options

Enumerator:
AST_RTP_OPT_G726_NONSTANDARD 

Definition at line 53 of file rtp.h.

00053                      {
00054    AST_RTP_OPT_G726_NONSTANDARD = (1 << 0),
00055 };


Function Documentation

int ast_rtcp_fd ( struct ast_rtp rtp  ) 

Definition at line 517 of file rtp.c.

References ast_rtp::rtcp, and ast_rtcp::s.

Referenced by __oh323_new(), __oh323_rtp_create(), __oh323_update_info(), gtalk_new(), sip_new(), and start_rtp().

00518 {
00519    if (rtp->rtcp)
00520       return rtp->rtcp->s;
00521    return -1;
00522 }

struct ast_frame* ast_rtcp_read ( struct ast_rtp rtp  ) 

Definition at line 822 of file rtp.c.

References ast_rtcp::accumulated_transit, AST_CONTROL_VIDUPDATE, AST_FRAME_CONTROL, AST_FRIENDLY_OFFSET, ast_inet_ntoa(), ast_log(), ast_null_frame, ast_verbose(), CRASH, ast_frame::datalen, ast_rtp::f, f, ast_frame::frametype, len, LOG_DEBUG, LOG_WARNING, ast_frame::mallocd, ast_rtcp::maxrtt, ast_rtcp::minrtt, ast_rtp::nat, option_debug, ast_rtcp::reported_jitter, ast_rtcp::reported_lost, ast_rtp::rtcp, rtcp_debug_test_addr(), RTCP_PT_BYE, RTCP_PT_FUR, RTCP_PT_RR, RTCP_PT_SDES, RTCP_PT_SR, ast_rtcp::rtt, ast_rtcp::rxlsr, ast_rtcp::s, ast_frame::samples, ast_rtcp::soc, ast_rtcp::spc, ast_frame::src, ast_frame::subclass, ast_rtcp::them, ast_rtcp::themrxlsr, and timeval2ntp().

Referenced by oh323_read(), sip_rtp_read(), and skinny_rtp_read().

00823 {
00824    socklen_t len;
00825    int position, i, packetwords;
00826    int res;
00827    struct sockaddr_in sin;
00828    unsigned int rtcpdata[8192 + AST_FRIENDLY_OFFSET];
00829    unsigned int *rtcpheader;
00830    int pt;
00831    struct timeval now;
00832    unsigned int length;
00833    int rc;
00834    double rttsec;
00835    uint64_t rtt = 0;
00836    unsigned int dlsr;
00837    unsigned int lsr;
00838    unsigned int msw;
00839    unsigned int lsw;
00840    unsigned int comp;
00841    struct ast_frame *f = &ast_null_frame;
00842    
00843    if (!rtp || !rtp->rtcp)
00844       return &ast_null_frame;
00845 
00846    len = sizeof(sin);
00847    
00848    res = recvfrom(rtp->rtcp->s, rtcpdata + AST_FRIENDLY_OFFSET, sizeof(rtcpdata) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET,
00849                0, (struct sockaddr *)&sin, &len);
00850    rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
00851    
00852    if (res < 0) {
00853       if (errno == EBADF)
00854          CRASH;
00855       if (errno != EAGAIN) {
00856          ast_log(LOG_WARNING, "RTCP Read error: %s.  Hanging up.\n", strerror(errno));
00857          return NULL;
00858       }
00859       return &ast_null_frame;
00860    }
00861 
00862    packetwords = res / 4;
00863    
00864    if (rtp->nat) {
00865       /* Send to whoever sent to us */
00866       if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
00867           (rtp->rtcp->them.sin_port != sin.sin_port)) {
00868          memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
00869          if (option_debug || rtpdebug)
00870             ast_log(LOG_DEBUG, "RTCP NAT: Got RTCP from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
00871       }
00872    }
00873 
00874    if (option_debug)
00875       ast_log(LOG_DEBUG, "Got RTCP report of %d bytes\n", res);
00876 
00877    /* Process a compound packet */
00878    position = 0;
00879    while (position < packetwords) {
00880       i = position;
00881       length = ntohl(rtcpheader[i]);
00882       pt = (length & 0xff0000) >> 16;
00883       rc = (length & 0x1f000000) >> 24;
00884       length &= 0xffff;
00885     
00886       if ((i + length) > packetwords) {
00887          ast_log(LOG_WARNING, "RTCP Read too short\n");
00888          return &ast_null_frame;
00889       }
00890       
00891       if (rtcp_debug_test_addr(&sin)) {
00892          ast_verbose("\n\nGot RTCP from %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
00893          ast_verbose("PT: %d(%s)\n", pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown");
00894          ast_verbose("Reception reports: %d\n", rc);
00895          ast_verbose("SSRC of sender: %u\n", rtcpheader[i + 1]);
00896       }
00897     
00898       i += 2; /* Advance past header and ssrc */
00899       
00900       switch (pt) {
00901       case RTCP_PT_SR:
00902          gettimeofday(&rtp->rtcp->rxlsr,NULL); /* To be able to populate the dlsr */
00903          rtp->rtcp->spc = ntohl(rtcpheader[i+3]);
00904          rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
00905          rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16); /* Going to LSR in RR*/
00906     
00907          if (rtcp_debug_test_addr(&sin)) {
00908             ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader[i]), (unsigned long) ntohl(rtcpheader[i + 1]) * 4096);
00909             ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader[i + 2]));
00910             ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader[i + 3]), (unsigned long) ntohl(rtcpheader[i + 4]));
00911          }
00912          i += 5;
00913          if (rc < 1)
00914             break;
00915          /* Intentional fall through */
00916       case RTCP_PT_RR:
00917          /* Don't handle multiple reception reports (rc > 1) yet */
00918          /* Calculate RTT per RFC */
00919          gettimeofday(&now, NULL);
00920          timeval2ntp(now, &msw, &lsw);
00921          if (ntohl(rtcpheader[i + 4]) && ntohl(rtcpheader[i + 5])) { /* We must have the LSR && DLSR */
00922             comp = ((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16);
00923             lsr = ntohl(rtcpheader[i + 4]);
00924             dlsr = ntohl(rtcpheader[i + 5]);
00925             rtt = comp - lsr - dlsr;
00926 
00927             /* Convert end to end delay to usec (keeping the calculation in 64bit space)
00928                sess->ee_delay = (eedelay * 1000) / 65536; */
00929             if (rtt < 4294) {
00930                 rtt = (rtt * 1000000) >> 16;
00931             } else {
00932                 rtt = (rtt * 1000) >> 16;
00933                 rtt *= 1000;
00934             }
00935             rtt = rtt / 1000.;
00936             rttsec = rtt / 1000.;
00937 
00938             if (comp - dlsr >= lsr) {
00939                rtp->rtcp->accumulated_transit += rttsec;
00940                rtp->rtcp->rtt = rttsec;
00941                if (rtp->rtcp->maxrtt<rttsec)
00942                   rtp->rtcp->maxrtt = rttsec;
00943                if (rtp->rtcp->minrtt>rttsec)
00944                   rtp->rtcp->minrtt = rttsec;
00945             } else if (rtcp_debug_test_addr(&sin)) {
00946                ast_verbose("Internal RTCP NTP clock skew detected: "
00947                         "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
00948                         "diff=%d\n",
00949                         lsr, comp, dlsr, dlsr / 65536,
00950                         (dlsr % 65536) * 1000 / 65536,
00951                         dlsr - (comp - lsr));
00952             }
00953          }
00954 
00955          rtp->rtcp->reported_jitter = ntohl(rtcpheader[i + 3]);
00956          rtp->rtcp->reported_lost = ntohl(rtcpheader[i + 1]) & 0xffffff;
00957          if (rtcp_debug_test_addr(&sin)) {
00958             ast_verbose("  Fraction lost: %ld\n", (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24));
00959             ast_verbose("  Packets lost so far: %d\n", rtp->rtcp->reported_lost);
00960             ast_verbose("  Highest sequence number: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff));
00961             ast_verbose("  Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16);
00962             ast_verbose("  Interarrival jitter: %u\n", rtp->rtcp->reported_jitter);
00963             ast_verbose("  Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader[i + 4]) >> 16,((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096);
00964             ast_verbose("  DLSR: %4.4f (sec)\n",ntohl(rtcpheader[i + 5])/65536.0);
00965             if (rtt)
00966                ast_verbose("  RTT: %lu(sec)\n", (unsigned long) rtt);
00967          }
00968          break;
00969       case RTCP_PT_FUR:
00970          if (rtcp_debug_test_addr(&sin))
00971             ast_verbose("Received an RTCP Fast Update Request\n");
00972          rtp->f.frametype = AST_FRAME_CONTROL;
00973          rtp->f.subclass = AST_CONTROL_VIDUPDATE;
00974          rtp->f.datalen = 0;
00975          rtp->f.samples = 0;
00976          rtp->f.mallocd = 0;
00977          rtp->f.src = "RTP";
00978          f = &rtp->f;
00979          break;
00980       case RTCP_PT_SDES:
00981          if (rtcp_debug_test_addr(&sin))
00982             ast_verbose("Received an SDES from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
00983          break;
00984       case RTCP_PT_BYE:
00985          if (rtcp_debug_test_addr(&sin))
00986             ast_verbose("Received a BYE from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
00987          break;
00988       default:
00989          if (option_debug)
00990             ast_log(LOG_DEBUG, "Unknown RTCP packet (pt=%d) received from %s:%d\n", pt, ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
00991          break;
00992       }
00993       position += (length + 1);
00994    }
00995          
00996    return f;
00997 }

int ast_rtcp_send_h261fur ( void *  data  ) 

Send an H.261 fast update request. Some devices need this rather than the XML message in SIP.

Definition at line 2314 of file rtp.c.

References ast_rtcp_write(), ast_rtp::rtcp, and ast_rtcp::sendfur.

02315 {
02316    struct ast_rtp *rtp = data;
02317    int res;
02318 
02319    rtp->rtcp->sendfur = 1;
02320    res = ast_rtcp_write(data);
02321    
02322    return res;
02323 }

size_t ast_rtp_alloc_size ( void   ) 

Get the amount of space required to hold an RTP session.

Returns:
number of bytes required

Definition at line 397 of file rtp.c.

Referenced by process_sdp().

00398 {
00399    return sizeof(struct ast_rtp);
00400 }

int ast_rtp_bridge ( struct ast_channel c0,
struct ast_channel c1,
int  flags,
struct ast_frame **  fo,
struct ast_channel **  rc,
int  timeoutms 
)

Bridge calls. If possible and allowed, initiate re-invite so the peers exchange media directly outside of Asterisk.

Definition at line 3222 of file rtp.c.

References AST_BRIDGE_DTMF_CHANNEL_0, AST_BRIDGE_DTMF_CHANNEL_1, AST_BRIDGE_FAILED, AST_BRIDGE_FAILED_NOWARN, ast_channel_lock, ast_channel_trylock, ast_channel_unlock, ast_codec_pref_getsize(), ast_log(), AST_RTP_GET_FAILED, AST_RTP_TRY_NATIVE, AST_RTP_TRY_PARTIAL, ast_set_flag, ast_test_flag, ast_verbose(), bridge_native_loop(), bridge_p2p_loop(), ast_format_list::cur_ms, FLAG_DTMF_COMPENSATE, FLAG_HAS_DTMF, FLAG_P2P_NEED_DTMF, ast_rtp_protocol::get_codec, get_proto(), ast_rtp_protocol::get_rtp_info, ast_rtp_protocol::get_vrtp_info, LOG_WARNING, option_debug, option_verbose, ast_rtp::pref, ast_channel::rawreadformat, ast_channel::rawwriteformat, ast_channel_tech::send_digit_begin, ast_channel::tech, ast_channel::tech_pvt, and VERBOSE_PREFIX_3.

03223 {
03224    struct ast_rtp *p0 = NULL, *p1 = NULL;    /* Audio RTP Channels */
03225    struct ast_rtp *vp0 = NULL, *vp1 = NULL;  /* Video RTP channels */
03226    struct ast_rtp_protocol *pr0 = NULL, *pr1 = NULL;
03227    enum ast_rtp_get_result audio_p0_res = AST_RTP_GET_FAILED, video_p0_res = AST_RTP_GET_FAILED;
03228    enum ast_rtp_get_result audio_p1_res = AST_RTP_GET_FAILED, video_p1_res = AST_RTP_GET_FAILED;
03229    enum ast_bridge_result res = AST_BRIDGE_FAILED;
03230    int codec0 = 0, codec1 = 0;
03231    void *pvt0 = NULL, *pvt1 = NULL;
03232 
03233    /* Lock channels */
03234    ast_channel_lock(c0);
03235    while(ast_channel_trylock(c1)) {
03236       ast_channel_unlock(c0);
03237       usleep(1);
03238       ast_channel_lock(c0);
03239    }
03240 
03241    /* Find channel driver interfaces */
03242    if (!(pr0 = get_proto(c0))) {
03243       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
03244       ast_channel_unlock(c0);
03245       ast_channel_unlock(c1);
03246       return AST_BRIDGE_FAILED;
03247    }
03248    if (!(pr1 = get_proto(c1))) {
03249       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
03250       ast_channel_unlock(c0);
03251       ast_channel_unlock(c1);
03252       return AST_BRIDGE_FAILED;
03253    }
03254 
03255    /* Get channel specific interface structures */
03256    pvt0 = c0->tech_pvt;
03257    pvt1 = c1->tech_pvt;
03258 
03259    /* Get audio and video interface (if native bridge is possible) */
03260    audio_p0_res = pr0->get_rtp_info(c0, &p0);
03261    video_p0_res = pr0->get_vrtp_info ? pr0->get_vrtp_info(c0, &vp0) : AST_RTP_GET_FAILED;
03262    audio_p1_res = pr1->get_rtp_info(c1, &p1);
03263    video_p1_res = pr1->get_vrtp_info ? pr1->get_vrtp_info(c1, &vp1) : AST_RTP_GET_FAILED;
03264 
03265    /* If we are carrying video, and both sides are not reinviting... then fail the native bridge */
03266    if (video_p0_res != AST_RTP_GET_FAILED && (audio_p0_res != AST_RTP_TRY_NATIVE || video_p0_res != AST_RTP_TRY_NATIVE))
03267       audio_p0_res = AST_RTP_GET_FAILED;
03268    if (video_p1_res != AST_RTP_GET_FAILED && (audio_p1_res != AST_RTP_TRY_NATIVE || video_p1_res != AST_RTP_TRY_NATIVE))
03269       audio_p1_res = AST_RTP_GET_FAILED;
03270 
03271    /* Check if a bridge is possible (partial/native) */
03272    if (audio_p0_res == AST_RTP_GET_FAILED || audio_p1_res == AST_RTP_GET_FAILED) {
03273       /* Somebody doesn't want to play... */
03274       ast_channel_unlock(c0);
03275       ast_channel_unlock(c1);
03276       return AST_BRIDGE_FAILED_NOWARN;
03277    }
03278 
03279    /* If we need to feed DTMF frames into the core then only do a partial native bridge */
03280    if (ast_test_flag(p0, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
03281       ast_set_flag(p0, FLAG_P2P_NEED_DTMF);
03282       audio_p0_res = AST_RTP_TRY_PARTIAL;
03283    }
03284 
03285    if (ast_test_flag(p1, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)) {
03286       ast_set_flag(p1, FLAG_P2P_NEED_DTMF);
03287       audio_p1_res = AST_RTP_TRY_PARTIAL;
03288    }
03289 
03290    /* If both sides are not using the same method of DTMF transmission 
03291     * (ie: one is RFC2833, other is INFO... then we can not do direct media. 
03292     * --------------------------------------------------
03293     * | DTMF Mode |  HAS_DTMF  |  Accepts Begin Frames |
03294     * |-----------|------------|-----------------------|
03295     * | Inband    | False      | True                  |
03296     * | RFC2833   | True       | True                  |
03297     * | SIP INFO  | False      | False                 |
03298     * --------------------------------------------------
03299     * However, if DTMF from both channels is being monitored by the core, then
03300     * we can still do packet-to-packet bridging, because passing through the 
03301     * core will handle DTMF mode translation.
03302     */
03303    if ( (ast_test_flag(p0, FLAG_HAS_DTMF) != ast_test_flag(p1, FLAG_HAS_DTMF)) ||
03304        (!c0->tech->send_digit_begin != !c1->tech->send_digit_begin)) {
03305       if (!ast_test_flag(p0, FLAG_P2P_NEED_DTMF) || !ast_test_flag(p1, FLAG_P2P_NEED_DTMF)) {
03306          ast_channel_unlock(c0);
03307          ast_channel_unlock(c1);
03308          return AST_BRIDGE_FAILED_NOWARN;
03309       }
03310       audio_p0_res = AST_RTP_TRY_PARTIAL;
03311       audio_p1_res = AST_RTP_TRY_PARTIAL;
03312    }
03313 
03314    /* If the core will need to compensate and the P2P bridge will need to feed up DTMF frames then we can not reliably do so yet, so do not P2P bridge */
03315    if ((audio_p0_res == AST_RTP_TRY_PARTIAL && ast_test_flag(p0, FLAG_P2P_NEED_DTMF) && ast_test_flag(p0, FLAG_DTMF_COMPENSATE)) ||
03316        (audio_p1_res == AST_RTP_TRY_PARTIAL && ast_test_flag(p1, FLAG_P2P_NEED_DTMF) && ast_test_flag(p1, FLAG_DTMF_COMPENSATE))) {
03317       ast_channel_unlock(c0);
03318       ast_channel_unlock(c1);
03319       return AST_BRIDGE_FAILED_NOWARN;
03320    }
03321 
03322    /* Get codecs from both sides */
03323    codec0 = pr0->get_codec ? pr0->get_codec(c0) : 0;
03324    codec1 = pr1->get_codec ? pr1->get_codec(c1) : 0;
03325    if (codec0 && codec1 && !(codec0 & codec1)) {
03326       /* Hey, we can't do native bridging if both parties speak different codecs */
03327       if (option_debug)
03328          ast_log(LOG_DEBUG, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0, codec1);
03329       ast_channel_unlock(c0);
03330       ast_channel_unlock(c1);
03331       return AST_BRIDGE_FAILED_NOWARN;
03332    }
03333 
03334    /* If either side can only do a partial bridge, then don't try for a true native bridge */
03335    if (audio_p0_res == AST_RTP_TRY_PARTIAL || audio_p1_res == AST_RTP_TRY_PARTIAL) {
03336       struct ast_format_list fmt0, fmt1;
03337 
03338       /* In order to do Packet2Packet bridging both sides must be in the same rawread/rawwrite */
03339       if (c0->rawreadformat != c1->rawwriteformat || c1->rawreadformat != c0->rawwriteformat) {
03340          if (option_debug)
03341             ast_log(LOG_DEBUG, "Cannot packet2packet bridge - raw formats are incompatible\n");
03342          ast_channel_unlock(c0);
03343          ast_channel_unlock(c1);
03344          return AST_BRIDGE_FAILED_NOWARN;
03345       }
03346       /* They must also be using the same packetization */
03347       fmt0 = ast_codec_pref_getsize(&p0->pref, c0->rawreadformat);
03348       fmt1 = ast_codec_pref_getsize(&p1->pref, c1->rawreadformat);
03349       if (fmt0.cur_ms != fmt1.cur_ms) {
03350          if (option_debug)
03351             ast_log(LOG_DEBUG, "Cannot packet2packet bridge - packetization settings prevent it\n");
03352          ast_channel_unlock(c0);
03353          ast_channel_unlock(c1);
03354          return AST_BRIDGE_FAILED_NOWARN;
03355       }
03356 
03357       if (option_verbose > 2)
03358          ast_verbose(VERBOSE_PREFIX_3 "Packet2Packet bridging %s and %s\n", c0->name, c1->name);
03359       res = bridge_p2p_loop(c0, c1, p0, p1, timeoutms, flags, fo, rc, pvt0, pvt1);
03360    } else {
03361       if (option_verbose > 2) 
03362          ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name);
03363       res = bridge_native_loop(c0, c1, p0, p1, vp0, vp1, pr0, pr1, codec0, codec1, timeoutms, flags, fo, rc, pvt0, pvt1);
03364    }
03365 
03366    return res;
03367 }

int ast_rtp_codec_getformat ( int  pt  ) 

Definition at line 2697 of file rtp.c.

References rtpPayloadType::code, and static_RTP_PT.

Referenced by process_sdp().

02698 {
02699    if (pt < 0 || pt > MAX_RTP_PT)
02700       return 0; /* bogus payload type */
02701 
02702    if (static_RTP_PT[pt].isAstFormat)
02703       return static_RTP_PT[pt].code;
02704    else
02705       return 0;
02706 }

struct ast_codec_pref* ast_rtp_codec_getpref ( struct ast_rtp rtp  ) 

Definition at line 2692 of file rtp.c.

References ast_rtp::pref.

Referenced by add_codec_to_sdp(), and process_sdp().

02693 {
02694    return &rtp->pref;
02695 }

int ast_rtp_codec_setpref ( struct ast_rtp rtp,
struct ast_codec_pref prefs 
)

Definition at line 2679 of file rtp.c.

References ast_smoother_free(), ast_codec_pref::framing, ast_codec_pref::order, ast_rtp::pref, prefs, and ast_rtp::smoother.

Referenced by __oh323_rtp_create(), check_user_full(), create_addr_from_peer(), process_sdp(), register_verify(), set_peer_capabilities(), start_rtp(), and transmit_response_with_sdp().

02680 {
02681    int x;
02682    for (x = 0; x < 32; x++) {  /* Ugly way */
02683       rtp->pref.order[x] = prefs->order[x];
02684       rtp->pref.framing[x] = prefs->framing[x];
02685    }
02686    if (rtp->smoother)
02687       ast_smoother_free(rtp->smoother);
02688    rtp->smoother = NULL;
02689    return 0;
02690 }

void ast_rtp_destroy ( struct ast_rtp rtp  ) 

Definition at line 2096 of file rtp.c.

References ast_io_remove(), ast_mutex_destroy(), ast_sched_del(), ast_smoother_free(), ast_verbose(), ast_rtp::bridge_lock, ast_rtcp::expected_prior, free, ast_rtp::io, ast_rtp::ioid, ast_rtcp::received_prior, ast_rtcp::reported_jitter, ast_rtcp::reported_lost, ast_rtcp::rr_count, ast_rtp::rtcp, rtcp_debug_test_addr(), ast_rtcp::rtt, ast_rtp::rxcount, ast_rtp::rxjitter, ast_rtp::rxtransit, ast_rtcp::s, ast_rtp::s, ast_rtp::sched, ast_rtcp::schedid, ast_rtp::smoother, ast_rtcp::sr_count, ast_rtp::ssrc, ast_rtp::them, ast_rtp::themssrc, and ast_rtp::txcount.

Referenced by __oh323_destroy(), __sip_destroy(), check_user_full(), cleanup_connection(), create_addr_from_peer(), destroy_endpoint(), gtalk_free_pvt(), mgcp_hangup(), oh323_alloc(), skinny_hangup(), start_rtp(), and unalloc_sub().

02097 {
02098    if (rtcp_debug_test_addr(&rtp->them) || rtcpstats) {
02099       /*Print some info on the call here */
02100       ast_verbose("  RTP-stats\n");
02101       ast_verbose("* Our Receiver:\n");
02102       ast_verbose("  SSRC:     %u\n", rtp->themssrc);
02103       ast_verbose("  Received packets: %u\n", rtp->rxcount);
02104       ast_verbose("  Lost packets:   %u\n", rtp->rtcp->expected_prior - rtp->rtcp->received_prior);
02105       ast_verbose("  Jitter:      %.4f\n", rtp->rxjitter);
02106       ast_verbose("  Transit:     %.4f\n", rtp->rxtransit);
02107       ast_verbose("  RR-count:    %u\n", rtp->rtcp->rr_count);
02108       ast_verbose("* Our Sender:\n");
02109       ast_verbose("  SSRC:     %u\n", rtp->ssrc);
02110       ast_verbose("  Sent packets:   %u\n", rtp->txcount);
02111       ast_verbose("  Lost packets:   %u\n", rtp->rtcp->reported_lost);
02112       ast_verbose("  Jitter:      %u\n", rtp->rtcp->reported_jitter);
02113       ast_verbose("  SR-count:    %u\n", rtp->rtcp->sr_count);
02114       ast_verbose("  RTT:      %f\n", rtp->rtcp->rtt);
02115    }
02116 
02117    if (rtp->smoother)
02118       ast_smoother_free(rtp->smoother);
02119    if (rtp->ioid)
02120       ast_io_remove(rtp->io, rtp->ioid);
02121    if (rtp->s > -1)
02122       close(rtp->s);
02123    if (rtp->rtcp) {
02124       if (rtp->rtcp->schedid > 0)
02125          ast_sched_del(rtp->sched, rtp->rtcp->schedid);
02126       close(rtp->rtcp->s);
02127       free(rtp->rtcp);
02128       rtp->rtcp=NULL;
02129    }
02130 
02131    ast_mutex_destroy(&rtp->bridge_lock);
02132 
02133    free(rtp);
02134 }

int ast_rtp_early_bridge ( struct ast_channel dest,
struct ast_channel src 
)

If possible, create an early bridge directly between the devices without having to send a re-invite later.

Definition at line 1482 of file rtp.c.

References ast_channel_lock, ast_channel_trylock, ast_channel_unlock, ast_log(), AST_RTP_GET_FAILED, AST_RTP_TRY_NATIVE, ast_test_flag, FLAG_NAT_ACTIVE, ast_rtp_protocol::get_codec, get_proto(), ast_rtp_protocol::get_rtp_info, ast_rtp_protocol::get_vrtp_info, LOG_DEBUG, LOG_WARNING, option_debug, and ast_rtp_protocol::set_rtp_peer.

Referenced by wait_for_answer().

01483 {
01484    struct ast_rtp *destp = NULL, *srcp = NULL;     /* Audio RTP Channels */
01485    struct ast_rtp *vdestp = NULL, *vsrcp = NULL;      /* Video RTP channels */
01486    struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
01487    enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED;
01488    enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED;
01489    int srccodec, destcodec, nat_active = 0;
01490 
01491    /* Lock channels */
01492    ast_channel_lock(dest);
01493    if (src) {
01494       while(ast_channel_trylock(src)) {
01495          ast_channel_unlock(dest);
01496          usleep(1);
01497          ast_channel_lock(dest);
01498       }
01499    }
01500 
01501    /* Find channel driver interfaces */
01502    destpr = get_proto(dest);
01503    if (src)
01504       srcpr = get_proto(src);
01505    if (!destpr) {
01506       if (option_debug)
01507          ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", dest->name);
01508       ast_channel_unlock(dest);
01509       if (src)
01510          ast_channel_unlock(src);
01511       return 0;
01512    }
01513    if (!srcpr) {
01514       if (option_debug)
01515          ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", src ? src->name : "<unspecified>");
01516       ast_channel_unlock(dest);
01517       if (src)
01518          ast_channel_unlock(src);
01519       return 0;
01520    }
01521 
01522    /* Get audio and video interface (if native bridge is possible) */
01523    audio_dest_res = destpr->get_rtp_info(dest, &destp);
01524    video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
01525    if (srcpr) {
01526       audio_src_res = srcpr->get_rtp_info(src, &srcp);
01527       video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
01528    }
01529 
01530    /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
01531    if (audio_dest_res != AST_RTP_TRY_NATIVE) {
01532       /* Somebody doesn't want to play... */
01533       ast_channel_unlock(dest);
01534       if (src)
01535          ast_channel_unlock(src);
01536       return 0;
01537    }
01538    if (audio_src_res == AST_RTP_TRY_NATIVE && srcpr->get_codec)
01539       srccodec = srcpr->get_codec(src);
01540    else
01541       srccodec = 0;
01542    if (audio_dest_res == AST_RTP_TRY_NATIVE && destpr->get_codec)
01543       destcodec = destpr->get_codec(dest);
01544    else
01545       destcodec = 0;
01546    /* Ensure we have at least one matching codec */
01547    if (!(srccodec & destcodec)) {
01548       ast_channel_unlock(dest);
01549       if (src)
01550          ast_channel_unlock(src);
01551       return 0;
01552    }
01553    /* Consider empty media as non-existant */
01554    if (audio_src_res == AST_RTP_TRY_NATIVE && !srcp->them.sin_addr.s_addr)
01555       srcp = NULL;
01556    /* If the client has NAT stuff turned on then just safe NAT is active */
01557    if (srcp && (srcp->nat || ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
01558       nat_active = 1;
01559    /* Bridge media early */
01560    if (destpr->set_rtp_peer(dest, srcp, vsrcp, srccodec, nat_active))
01561       ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src ? src->name : "<unspecified>");
01562    ast_channel_unlock(dest);
01563    if (src)
01564       ast_channel_unlock(src);
01565    if (option_debug)
01566       ast_log(LOG_DEBUG, "Setting early bridge SDP of '%s' with that of '%s'\n", dest->name, src ? src->name : "<unspecified>");
01567    return 1;
01568 }

int ast_rtp_fd ( struct ast_rtp rtp  ) 

Definition at line 512 of file rtp.c.

References ast_rtp::s.

Referenced by __oh323_new(), __oh323_rtp_create(), __oh323_update_info(), gtalk_new(), mgcp_new(), sip_new(), skinny_new(), and start_rtp().

00513 {
00514    return rtp->s;
00515 }

struct ast_rtp* ast_rtp_get_bridged ( struct ast_rtp rtp  ) 

Definition at line 2018 of file rtp.c.

References ast_mutex_lock(), ast_mutex_unlock(), ast_rtp::bridge_lock, and ast_rtp::bridged.

Referenced by ast_rtp_read(), and do_monitor().

02019 {
02020    struct ast_rtp *bridged = NULL;
02021 
02022    ast_mutex_lock(&rtp->bridge_lock);
02023    bridged = rtp->bridged;
02024    ast_mutex_unlock(&rtp->bridge_lock);
02025 
02026    return bridged;
02027 }

void ast_rtp_get_current_formats ( struct ast_rtp rtp,
int *  astFormats,
int *  nonAstFormats 
)

Return the union of all of the codecs that were set by rtp_set...() calls They're returned as two distinct sets: AST_FORMATs, and AST_RTPs.

Definition at line 1688 of file rtp.c.

References ast_mutex_lock(), ast_rtp::bridge_lock, rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, and MAX_RTP_PT.

Referenced by process_sdp().

01690 {
01691    int pt;
01692    
01693    ast_mutex_lock(&rtp->bridge_lock);
01694    
01695    *astFormats = *nonAstFormats = 0;
01696    for (pt = 0; pt < MAX_RTP_PT; ++pt) {
01697       if (rtp->current_RTP_PT[pt].isAstFormat) {
01698          *astFormats |= rtp->current_RTP_PT[pt].code;
01699       } else {
01700          *nonAstFormats |= rtp->current_RTP_PT[pt].code;
01701       }
01702    }
01703    
01704    ast_mutex_unlock(&rtp->bridge_lock);
01705    
01706    return;
01707 }

int ast_rtp_get_peer ( struct ast_rtp rtp,
struct sockaddr_in *  them 
)

Definition at line 2000 of file rtp.c.

References ast_rtp::them.

Referenced by add_sdp(), bridge_native_loop(), do_monitor(), gtalk_update_stun(), oh323_set_rtp_peer(), sip_set_rtp_peer(), and transmit_modify_with_sdp().

02001 {
02002    if ((them->sin_family != AF_INET) ||
02003       (them->sin_port != rtp->them.sin_port) ||
02004       (them->sin_addr.s_addr != rtp->them.sin_addr.s_addr)) {
02005       them->sin_family = AF_INET;
02006       them->sin_port = rtp->them.sin_port;
02007       them->sin_addr = rtp->them.sin_addr;
02008       return 1;
02009    }
02010    return 0;
02011 }

char* ast_rtp_get_quality ( struct ast_rtp rtp,
struct ast_rtp_quality qual 
)

Return RTCP quality string.

Definition at line 2066 of file rtp.c.

References ast_rtcp::expected_prior, ast_rtp_quality::local_count, ast_rtp_quality::local_jitter, ast_rtp_quality::local_lostpackets, ast_rtp_quality::local_ssrc, ast_rtcp::quality, ast_rtcp::received_prior, ast_rtp_quality::remote_count, ast_rtp_quality::remote_jitter, ast_rtp_quality::remote_lostpackets, ast_rtp_quality::remote_ssrc, ast_rtcp::reported_jitter, ast_rtcp::reported_lost, ast_rtp::rtcp, ast_rtcp::rtt, ast_rtp_quality::rtt, ast_rtp::rxcount, ast_rtp::rxjitter, ast_rtp::ssrc, ast_rtp::themssrc, and ast_rtp::txcount.

Referenced by acf_channel_read(), handle_request_bye(), and sip_hangup().

02067 {
02068    /*
02069    *ssrc          our ssrc
02070    *themssrc      their ssrc
02071    *lp            lost packets
02072    *rxjitter      our calculated jitter(rx)
02073    *rxcount       no. received packets
02074    *txjitter      reported jitter of the other end
02075    *txcount       transmitted packets
02076    *rlp           remote lost packets
02077    *rtt           round trip time
02078    */
02079 
02080    if (qual) {
02081       qual->local_ssrc = rtp->ssrc;
02082       qual->local_lostpackets = rtp->rtcp->expected_prior - rtp->rtcp->received_prior;
02083       qual->local_jitter = rtp->rxjitter;
02084       qual->local_count = rtp->rxcount;
02085       qual->remote_ssrc = rtp->themssrc;
02086       qual->remote_lostpackets = rtp->rtcp->reported_lost;
02087       qual->remote_jitter = rtp->rtcp->reported_jitter / 65536.0;
02088       qual->remote_count = rtp->txcount;
02089       qual->rtt = rtp->rtcp->rtt;
02090    }
02091    snprintf(rtp->rtcp->quality, sizeof(rtp->rtcp->quality), "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f", rtp->ssrc, rtp->themssrc, rtp->rtcp->expected_prior - rtp->rtcp->received_prior, rtp->rxjitter, rtp->rxcount, (double)rtp->rtcp->reported_jitter/65536., rtp->txcount, rtp->rtcp->reported_lost, rtp->rtcp->rtt);
02092    
02093    return rtp->rtcp->quality;
02094 }

int ast_rtp_get_rtpholdtimeout ( struct ast_rtp rtp  ) 

Get rtp hold timeout.

Definition at line 567 of file rtp.c.

References ast_rtp::rtpholdtimeout, and ast_rtp::rtptimeout.

Referenced by do_monitor().

00568 {
00569    if (rtp->rtptimeout < 0)   /* We're not checking, but remembering the setting (during T.38 transmission) */
00570       return 0;
00571    return rtp->rtpholdtimeout;
00572 }

int ast_rtp_get_rtpkeepalive ( struct ast_rtp rtp  ) 

Get RTP keepalive interval.

Definition at line 575 of file rtp.c.

References ast_rtp::rtpkeepalive.

Referenced by do_monitor().

00576 {
00577    return rtp->rtpkeepalive;
00578 }

int ast_rtp_get_rtptimeout ( struct ast_rtp rtp  ) 

Get rtp timeout.

Definition at line 559 of file rtp.c.

References ast_rtp::rtptimeout.

Referenced by do_monitor().

00560 {
00561    if (rtp->rtptimeout < 0)   /* We're not checking, but remembering the setting (during T.38 transmission) */
00562       return 0;
00563    return rtp->rtptimeout;
00564 }

void ast_rtp_get_us ( struct ast_rtp rtp,
struct sockaddr_in *  us 
)

Definition at line 2013 of file rtp.c.

References ast_rtp::us.

Referenced by add_sdp(), external_rtp_create(), gtalk_create_candidates(), handle_open_receive_channel_ack_message(), and oh323_set_rtp_peer().

02014 {
02015    *us = rtp->us;
02016 }

int ast_rtp_getnat ( struct ast_rtp rtp  ) 

Definition at line 595 of file rtp.c.

References ast_test_flag, and FLAG_NAT_ACTIVE.

Referenced by sip_get_rtp_peer().

00596 {
00597    return ast_test_flag(rtp, FLAG_NAT_ACTIVE);
00598 }

void ast_rtp_init ( void   ) 

Initialize the RTP system in Asterisk.

Definition at line 3752 of file rtp.c.

References ast_cli_register_multiple(), ast_rtp_reload(), and cli_rtp.

Referenced by main().

03753 {
03754    ast_cli_register_multiple(cli_rtp, sizeof(cli_rtp) / sizeof(struct ast_cli_entry));
03755    ast_rtp_reload();
03756 }

int ast_rtp_lookup_code ( struct ast_rtp rtp,
int  isAstFormat,
int  code 
)

Looks up an RTP code out of our *static* outbound list.

Definition at line 1731 of file rtp.c.

References ast_mutex_lock(), ast_mutex_unlock(), ast_rtp::bridge_lock, rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, ast_rtp::rtp_lookup_code_cache_code, ast_rtp::rtp_lookup_code_cache_isAstFormat, and ast_rtp::rtp_lookup_code_cache_result.

Referenced by add_codec_to_answer(), add_codec_to_sdp(), add_noncodec_to_sdp(), add_sdp(), ast_rtp_sendcng(), ast_rtp_senddigit_begin(), ast_rtp_write(), and bridge_p2p_rtp_write().

01732 {
01733    int pt = 0;
01734 
01735    ast_mutex_lock(&rtp->bridge_lock);
01736 
01737    if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
01738       code == rtp->rtp_lookup_code_cache_code) {
01739       /* Use our cached mapping, to avoid the overhead of the loop below */
01740       pt = rtp->rtp_lookup_code_cache_result;
01741       ast_mutex_unlock(&rtp->bridge_lock);
01742       return pt;
01743    }
01744 
01745    /* Check the dynamic list first */
01746    for (pt = 0; pt < MAX_RTP_PT; ++pt) {
01747       if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
01748          rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
01749          rtp->rtp_lookup_code_cache_code = code;
01750          rtp->rtp_lookup_code_cache_result = pt;
01751          ast_mutex_unlock(&rtp->bridge_lock);
01752          return pt;
01753       }
01754    }
01755 
01756    /* Then the static list */
01757    for (pt = 0; pt < MAX_RTP_PT; ++pt) {
01758       if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
01759          rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
01760          rtp->rtp_lookup_code_cache_code = code;
01761          rtp->rtp_lookup_code_cache_result = pt;
01762          ast_mutex_unlock(&rtp->bridge_lock);
01763          return pt;
01764       }
01765    }
01766 
01767    ast_mutex_unlock(&rtp->bridge_lock);
01768 
01769    return -1;
01770 }

char* ast_rtp_lookup_mime_multiple ( char *  buf,
size_t  size,
const int  capability,
const int  isAstFormat,
enum ast_rtp_options  options 
)

Build a string of MIME subtype names from a capability list.

Definition at line 1791 of file rtp.c.

References ast_rtp_lookup_mime_subtype(), AST_RTP_MAX, format, len, and name.

Referenced by process_sdp().

01793 {
01794    int format;
01795    unsigned len;
01796    char *end = buf;
01797    char *start = buf;
01798 
01799    if (!buf || !size)
01800       return NULL;
01801 
01802    snprintf(end, size, "0x%x (", capability);
01803 
01804    len = strlen(end);
01805    end += len;
01806    size -= len;
01807    start = end;
01808 
01809    for (format = 1; format < AST_RTP_MAX; format <<= 1) {
01810       if (capability & format) {
01811          const char *name = ast_rtp_lookup_mime_subtype(isAstFormat, format, options);
01812 
01813          snprintf(end, size, "%s|", name);
01814          len = strlen(end);
01815          end += len;
01816          size -= len;
01817       }
01818    }
01819 
01820    if (start == end)
01821       snprintf(start, size, "nothing)"); 
01822    else if (size > 1)
01823       *(end -1) = ')';
01824    
01825    return buf;
01826 }

const char* ast_rtp_lookup_mime_subtype ( int  isAstFormat,
int  code,
enum ast_rtp_options  options 
)

Mapping an Asterisk code into a MIME subtype (string):.

Definition at line 1772 of file rtp.c.

References AST_FORMAT_G726_AAL2, AST_RTP_OPT_G726_NONSTANDARD, rtpPayloadType::code, mimeTypes, and payloadType.

Referenced by add_codec_to_sdp(), add_noncodec_to_sdp(), add_sdp(), ast_rtp_lookup_mime_multiple(), transmit_connect_with_sdp(), and transmit_modify_with_sdp().

01774 {
01775    unsigned int i;
01776 
01777    for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
01778       if ((mimeTypes[i].payloadType.code == code) && (mimeTypes[i].payloadType.isAstFormat == isAstFormat)) {
01779          if (isAstFormat &&
01780              (code == AST_FORMAT_G726_AAL2) &&
01781              (options & AST_RTP_OPT_G726_NONSTANDARD))
01782             return "G726-32";
01783          else
01784             return mimeTypes[i].subtype;
01785       }
01786    }
01787 
01788    return "";
01789 }

struct rtpPayloadType ast_rtp_lookup_pt ( struct ast_rtp rtp,
int  pt 
)

Mapping between RTP payload format codes and Asterisk codes:.

Definition at line 1709 of file rtp.c.

References ast_mutex_lock(), ast_mutex_unlock(), MAX_RTP_PT, result, and static_RTP_PT.

Referenced by ast_rtp_read(), bridge_p2p_rtp_write(), and setup_rtp_connection().

01710 {
01711    struct rtpPayloadType result;
01712 
01713    result.isAstFormat = result.code = 0;
01714 
01715    if (pt < 0 || pt > MAX_RTP_PT) 
01716       return result; /* bogus payload type */
01717 
01718    /* Start with negotiated codecs */
01719    ast_mutex_lock(&rtp->bridge_lock);
01720    result = rtp->current_RTP_PT[pt];
01721    ast_mutex_unlock(&rtp->bridge_lock);
01722 
01723    /* If it doesn't exist, check our static RTP type list, just in case */
01724    if (!result.code) 
01725       result = static_RTP_PT[pt];
01726 
01727    return result;
01728 }

int ast_rtp_make_compatible ( struct ast_channel dest,
struct ast_channel src,
int  media 
)

Definition at line 1570 of file rtp.c.

References ast_channel_lock, ast_channel_trylock, ast_channel_unlock, ast_log(), AST_RTP_GET_FAILED, ast_rtp_pt_copy(), AST_RTP_TRY_NATIVE, ast_test_flag, FLAG_NAT_ACTIVE, ast_rtp_protocol::get_codec, get_proto(), ast_rtp_protocol::get_rtp_info, ast_rtp_protocol::get_vrtp_info, LOG_DEBUG, LOG_WARNING, option_debug, and ast_rtp_protocol::set_rtp_peer.

Referenced by wait_for_answer().

01571 {
01572    struct ast_rtp *destp = NULL, *srcp = NULL;     /* Audio RTP Channels */
01573    struct ast_rtp *vdestp = NULL, *vsrcp = NULL;      /* Video RTP channels */
01574    struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
01575    enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED;
01576    enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED; 
01577    int srccodec, destcodec;
01578 
01579    /* Lock channels */
01580    ast_channel_lock(dest);
01581    while(ast_channel_trylock(src)) {
01582       ast_channel_unlock(dest);
01583       usleep(1);
01584       ast_channel_lock(dest);
01585    }
01586 
01587    /* Find channel driver interfaces */
01588    if (!(destpr = get_proto(dest))) {
01589       if (option_debug)
01590          ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", dest->name);
01591       ast_channel_unlock(dest);
01592       ast_channel_unlock(src);
01593       return 0;
01594    }
01595    if (!(srcpr = get_proto(src))) {
01596       if (option_debug)
01597          ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", src->name);
01598       ast_channel_unlock(dest);
01599       ast_channel_unlock(src);
01600       return 0;
01601    }
01602 
01603    /* Get audio and video interface (if native bridge is possible) */
01604    audio_dest_res = destpr->get_rtp_info(dest, &destp);
01605    video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
01606    audio_src_res = srcpr->get_rtp_info(src, &srcp);
01607    video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
01608 
01609    /* Ensure we have at least one matching codec */
01610    if (srcpr->get_codec)
01611       srccodec = srcpr->get_codec(src);
01612    else
01613       srccodec = 0;
01614    if (destpr->get_codec)
01615       destcodec = destpr->get_codec(dest);
01616    else
01617       destcodec = 0;
01618 
01619    /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
01620    if (audio_dest_res != AST_RTP_TRY_NATIVE || audio_src_res != AST_RTP_TRY_NATIVE || !(srccodec & destcodec)) {
01621       /* Somebody doesn't want to play... */
01622       ast_channel_unlock(dest);
01623       ast_channel_unlock(src);
01624       return 0;
01625    }
01626    ast_rtp_pt_copy(destp, srcp);
01627    if (vdestp && vsrcp)
01628       ast_rtp_pt_copy(vdestp, vsrcp);
01629    if (media) {
01630       /* Bridge early */
01631       if (destpr->set_rtp_peer(dest, srcp, vsrcp, srccodec, ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
01632          ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src->name);
01633    }
01634    ast_channel_unlock(dest);
01635    ast_channel_unlock(src);
01636    if (option_debug)
01637       ast_log(LOG_DEBUG, "Seeded SDP of '%s' with that of '%s'\n", dest->name, src->name);
01638    return 1;
01639 }

struct ast_rtp* ast_rtp_new ( struct sched_context sched,
struct io_context io,
int  rtcpenable,
int  callbackmode 
)

Initializate a RTP session.

Parameters:
sched 
io 
rtcpenable 
callbackmode 
Returns:
A representation (structure) of an RTP session.

Definition at line 1972 of file rtp.c.

References ast_rtp_new_with_bindaddr(), io, and sched.

01973 {
01974    struct in_addr ia;
01975 
01976    memset(&ia, 0, sizeof(ia));
01977    return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
01978 }

void ast_rtp_new_init ( struct ast_rtp rtp  ) 

Initialize a new RTP structure.

Definition at line 1872 of file rtp.c.

References ast_mutex_init(), ast_random(), ast_set_flag, ast_rtp::bridge_lock, FLAG_HAS_DTMF, ast_rtp::seqno, ast_rtp::ssrc, ast_rtp::them, and ast_rtp::us.

Referenced by ast_rtp_new_with_bindaddr(), and process_sdp().

01873 {
01874    ast_mutex_init(&rtp->bridge_lock);
01875 
01876    rtp->them.sin_family = AF_INET;
01877    rtp->us.sin_family = AF_INET;
01878    rtp->ssrc = ast_random();
01879    rtp->seqno = ast_random() & 0xffff;
01880    ast_set_flag(rtp, FLAG_HAS_DTMF);
01881 
01882    return;
01883 }

struct ast_rtp* ast_rtp_new_with_bindaddr ( struct sched_context sched,
struct io_context io,
int  rtcpenable,
int  callbackmode,
struct in_addr  in 
)

Initializate a RTP session using an in_addr structure.

This fuction gets called by ast_rtp_new().

Parameters:
sched 
io 
rtcpenable 
callbackmode 
in 
Returns:
A representation (structure) of an RTP session.

Definition at line 1885 of file rtp.c.

References ast_calloc, ast_io_add(), AST_IO_IN, ast_log(), ast_random(), ast_rtcp_new(), ast_rtp_new_init(), ast_rtp_pt_default(), ast_set_flag, FLAG_CALLBACK_MODE, free, io, LOG_ERROR, rtp_socket(), rtpread(), and sched.

Referenced by __oh323_rtp_create(), ast_rtp_new(), gtalk_alloc(), sip_alloc(), and start_rtp().

01886 {
01887    struct ast_rtp *rtp;
01888    int x;
01889    int first;
01890    int startplace;
01891    
01892    if (!(rtp = ast_calloc(1, sizeof(*rtp))))
01893       return NULL;
01894 
01895    ast_rtp_new_init(rtp);
01896 
01897    rtp->s = rtp_socket();
01898    if (rtp->s < 0) {
01899       free(rtp);
01900       ast_log(LOG_ERROR, "Unable to allocate socket: %s\n", strerror(errno));
01901       return NULL;
01902    }
01903    if (sched && rtcpenable) {
01904       rtp->sched = sched;
01905       rtp->rtcp = ast_rtcp_new();
01906    }
01907    
01908    /* Select a random port number in the range of possible RTP */
01909    x = (ast_random() % (rtpend-rtpstart)) + rtpstart;
01910    x = x & ~1;
01911    /* Save it for future references. */
01912    startplace = x;
01913    /* Iterate tring to bind that port and incrementing it otherwise untill a port was found or no ports are available. */
01914    for (;;) {
01915       /* Must be an even port number by RTP spec */
01916       rtp->us.sin_port = htons(x);
01917       rtp->us.sin_addr = addr;
01918       /* If there's rtcp, initialize it as well. */
01919       if (rtp->rtcp) {
01920          rtp->rtcp->us.sin_port = htons(x + 1);
01921          rtp->rtcp->us.sin_addr = addr;
01922       }
01923       /* Try to bind it/them. */
01924       if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
01925          (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
01926          break;
01927       if (!first) {
01928          /* Primary bind succeeded! Gotta recreate it */
01929          close(rtp->s);
01930          rtp->s = rtp_socket();
01931       }
01932       if (errno != EADDRINUSE) {
01933          /* We got an error that wasn't expected, abort! */
01934          ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
01935          close(rtp->s);
01936          if (rtp->rtcp) {
01937             close(rtp->rtcp->s);
01938             free(rtp->rtcp);
01939          }
01940          free(rtp);
01941          return NULL;
01942       }
01943       /* The port was used, increment it (by two). */
01944       x += 2;
01945       /* Did we go over the limit ? */
01946       if (x > rtpend)
01947          /* then, start from the begingig. */
01948          x = (rtpstart + 1) & ~1;
01949       /* Check if we reached the place were we started. */
01950       if (x == startplace) {
01951          /* If so, there's no ports available. */
01952          ast_log(LOG_ERROR, "No RTP ports remaining. Can't setup media stream for this call.\n");
01953          close(rtp->s);
01954          if (rtp->rtcp) {
01955             close(rtp->rtcp->s);
01956             free(rtp->rtcp);
01957          }
01958          free(rtp);
01959          return NULL;
01960       }
01961    }
01962    rtp->sched = sched;
01963    rtp->io = io;
01964    if (callbackmode) {
01965       rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
01966       ast_set_flag(rtp, FLAG_CALLBACK_MODE);
01967    }
01968    ast_rtp_pt_default(rtp);
01969    return rtp;
01970 }

int ast_rtp_proto_register ( struct ast_rtp_protocol proto  ) 

Register interface to channel driver.

Definition at line 2797 of file rtp.c.

References AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), LOG_WARNING, protos, and ast_rtp_protocol::type.

Referenced by load_module().

02798 {
02799    struct ast_rtp_protocol *cur;
02800 
02801    AST_LIST_LOCK(&protos);
02802    AST_LIST_TRAVERSE(&protos, cur, list) {   
02803       if (!strcmp(cur->type, proto->type)) {
02804          ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
02805          AST_LIST_UNLOCK(&protos);
02806          return -1;
02807       }
02808    }
02809    AST_LIST_INSERT_HEAD(&protos, proto, list);
02810    AST_LIST_UNLOCK(&protos);
02811    
02812    return 0;
02813 }

void ast_rtp_proto_unregister ( struct ast_rtp_protocol proto  ) 

Unregister interface to channel driver.

Definition at line 2789 of file rtp.c.

References AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, and protos.

Referenced by load_module(), and unload_module().

02790 {
02791    AST_LIST_LOCK(&protos);
02792    AST_LIST_REMOVE(&protos, proto, list);
02793    AST_LIST_UNLOCK(&protos);
02794 }

void ast_rtp_pt_clear ( struct ast_rtp rtp  ) 

Setting RTP payload types from lines in a SDP description:.

Definition at line 1406 of file rtp.c.

References ast_mutex_lock(), ast_mutex_unlock(), ast_rtp::bridge_lock, rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, ast_rtp::rtp_lookup_code_cache_code, ast_rtp::rtp_lookup_code_cache_isAstFormat, and ast_rtp::rtp_lookup_code_cache_result.

Referenced by process_sdp().

01407 {
01408    int i;
01409 
01410    if (!rtp)
01411       return;
01412 
01413    ast_mutex_lock(&rtp->bridge_lock);
01414 
01415    for (i = 0; i < MAX_RTP_PT; ++i) {
01416       rtp->current_RTP_PT[i].isAstFormat = 0;
01417       rtp->current_RTP_PT[i].code = 0;
01418    }
01419 
01420    rtp->rtp_lookup_code_cache_isAstFormat = 0;
01421    rtp->rtp_lookup_code_cache_code = 0;
01422    rtp->rtp_lookup_code_cache_result = 0;
01423 
01424    ast_mutex_unlock(&rtp->bridge_lock);
01425 }

void ast_rtp_pt_copy ( struct ast_rtp dest,
struct ast_rtp src 
)

Copy payload types between RTP structures.

Definition at line 1446 of file rtp.c.

References ast_mutex_lock(), ast_mutex_unlock(), ast_rtp::bridge_lock, rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, ast_rtp::rtp_lookup_code_cache_code, ast_rtp::rtp_lookup_code_cache_isAstFormat, and ast_rtp::rtp_lookup_code_cache_result.

Referenced by ast_rtp_make_compatible(), and process_sdp().

01447 {
01448    unsigned int i;
01449 
01450    ast_mutex_lock(&dest->bridge_lock);
01451    ast_mutex_lock(&src->bridge_lock);
01452 
01453    for (i=0; i < MAX_RTP_PT; ++i) {
01454       dest->current_RTP_PT[i].isAstFormat = 
01455          src->current_RTP_PT[i].isAstFormat;
01456       dest->current_RTP_PT[i].code = 
01457          src->current_RTP_PT[i].code; 
01458    }
01459    dest->rtp_lookup_code_cache_isAstFormat = 0;
01460    dest->rtp_lookup_code_cache_code = 0;
01461    dest->rtp_lookup_code_cache_result = 0;
01462 
01463    ast_mutex_unlock(&src->bridge_lock);
01464    ast_mutex_unlock(&dest->bridge_lock);
01465 }

void ast_rtp_pt_default ( struct ast_rtp rtp  ) 

Set payload types to defaults.

Definition at line 1427 of file rtp.c.

References ast_mutex_lock(), ast_mutex_unlock(), ast_rtp::bridge_lock, rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, ast_rtp::rtp_lookup_code_cache_code, ast_rtp::rtp_lookup_code_cache_isAstFormat, ast_rtp::rtp_lookup_code_cache_result, and static_RTP_PT.

Referenced by ast_rtp_new_with_bindaddr().

01428 {
01429    int i;
01430 
01431    ast_mutex_lock(&rtp->bridge_lock);
01432 
01433    /* Initialize to default payload types */
01434    for (i = 0; i < MAX_RTP_PT; ++i) {
01435       rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
01436       rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
01437    }
01438 
01439    rtp->rtp_lookup_code_cache_isAstFormat = 0;
01440    rtp->rtp_lookup_code_cache_code = 0;
01441    rtp->rtp_lookup_code_cache_result = 0;
01442 
01443    ast_mutex_unlock(&rtp->bridge_lock);
01444 }

struct ast_frame* ast_rtp_read ( struct ast_rtp rtp  ) 

Definition at line 1097 of file rtp.c.

References ast_backtrace(), ast_codec_get_samples(), AST_FORMAT_MAX_AUDIO, AST_FORMAT_SLINEAR, ast_frame_byteswap_be, AST_FRAME_VIDEO, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_inet_ntoa(), ast_log(), ast_null_frame, ast_rtcp_calc_interval(), ast_rtcp_write(), AST_RTP_CISCO_DTMF, AST_RTP_CN, AST_RTP_DTMF, ast_rtp_get_bridged(), ast_rtp_lookup_pt(), ast_rtp_senddigit_continuation(), ast_sched_add(), ast_set_flag, ast_verbose(), bridge_p2p_rtp_write(), ast_rtp::bridged, calc_rxstamp(), rtpPayloadType::code, CRASH, ast_rtp::cycles, ast_frame::data, ast_frame::datalen, ast_frame::delivery, event, ext, ast_rtp::f, f, FLAG_NAT_ACTIVE, ast_frame::frametype, ast_frame::has_timing_info, rtpPayloadType::isAstFormat, ast_rtp::lastevent, ast_rtp::lastividtimestamp, ast_rtp::lastrxformat, ast_rtp::lastrxseqno, ast_rtp::lastrxts, ast_frame::len, len, LOG_DEBUG, LOG_ERROR, LOG_NOTICE, LOG_WARNING, ast_frame::mallocd, ast_rtp::nat, ast_frame::offset, option_debug, process_cisco_dtmf(), process_rfc2833(), process_rfc3389(), ast_rtp::rawdata, ast_rtp::rtcp, rtp_debug_test_addr(), RTP_SEQ_MOD, ast_rtp::rxcount, ast_rtp::rxseqno, ast_rtp::rxssrc, ast_rtp::s, ast_frame::samples, ast_rtp::sched, ast_rtcp::schedid, ast_rtp::seedrxseqno, ast_rtp::sending_digit, ast_frame::seqno, ast_frame::src, STUN_ACCEPT, stun_handle_packet(), ast_frame::subclass, ast_rtcp::them, ast_rtp::them, ast_rtp::themssrc, and ast_frame::ts.

Referenced by gtalk_rtp_read(), mgcp_rtp_read(), oh323_rtp_read(), rtpread(), sip_rtp_read(), and skinny_rtp_read().

01098 {
01099    int res;
01100    struct sockaddr_in sin;
01101    socklen_t len;
01102    unsigned int seqno;
01103    int version;
01104    int payloadtype;
01105    int hdrlen = 12;
01106    int padding;
01107    int mark;
01108    int ext;
01109    int cc;
01110    unsigned int ssrc;
01111    unsigned int timestamp;
01112    unsigned int *rtpheader;
01113    struct rtpPayloadType rtpPT;
01114    struct ast_rtp *bridged = NULL;
01115    
01116    if( !rtp ) {
01117        ast_log(LOG_ERROR, "ast_rtp_read(): called with rtp == NULL\n");
01118        ast_backtrace();
01119        return &ast_null_frame;
01120    }
01121 
01122    /* If time is up, kill it */
01123    if (rtp->sending_digit)
01124       ast_rtp_senddigit_continuation(rtp);
01125 
01126    len = sizeof(sin);
01127    
01128    /* Cache where the header will go */
01129    res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
01130                0, (struct sockaddr *)&sin, &len);
01131 
01132    rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
01133    if (res < 0) {
01134       if (errno == EBADF)
01135          CRASH;
01136       if (errno != EAGAIN) {
01137          ast_log(LOG_WARNING, "RTP Read error: %s.  Hanging up.\n", strerror(errno));
01138          return NULL;
01139       }
01140       return &ast_null_frame;
01141    }
01142    
01143    if (res < hdrlen) {
01144       ast_log(LOG_WARNING, "RTP Read too short\n");
01145       return &ast_null_frame;
01146    }
01147 
01148    /* Get fields */
01149    seqno = ntohl(rtpheader[0]);
01150 
01151    /* Check RTP version */
01152    version = (seqno & 0xC0000000) >> 30;
01153    if (!version) {
01154       if ((stun_handle_packet(rtp->s, &sin, rtp->rawdata + AST_FRIENDLY_OFFSET, res) == STUN_ACCEPT) &&
01155          (!rtp->them.sin_port && !rtp->them.sin_addr.s_addr)) {
01156          memcpy(&rtp->them, &sin, sizeof(rtp->them));
01157       }
01158       return &ast_null_frame;
01159    }
01160 
01161 #if 0 /* Allow to receive RTP stream with closed transmission path */
01162    /* If we don't have the other side's address, then ignore this */
01163    if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
01164       return &ast_null_frame;
01165 #endif
01166 
01167    /* Send to whoever send to us if NAT is turned on */
01168    if (rtp->nat) {
01169       if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
01170           (rtp->them.sin_port != sin.sin_port)) {
01171          rtp->them = sin;
01172          if (rtp->rtcp) {
01173             memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
01174             rtp->rtcp->them.sin_port = htons(ntohs(rtp->them.sin_port)+1);
01175          }
01176          rtp->rxseqno = 0;
01177          ast_set_flag(rtp, FLAG_NAT_ACTIVE);
01178          if (option_debug || rtpdebug)
01179             ast_log(LOG_DEBUG, "RTP NAT: Got audio from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
01180       }
01181    }
01182 
01183    /* If we are bridged to another RTP stream, send direct */
01184    if ((bridged = ast_rtp_get_bridged(rtp)) && !bridge_p2p_rtp_write(rtp, bridged, rtpheader, res, hdrlen))
01185       return &ast_null_frame;
01186 
01187    if (version != 2)
01188       return &ast_null_frame;
01189 
01190    payloadtype = (seqno & 0x7f0000) >> 16;
01191    padding = seqno & (1 << 29);
01192    mark = seqno & (1 << 23);
01193    ext = seqno & (1 << 28);
01194    cc = (seqno & 0xF000000) >> 24;
01195    seqno &= 0xffff;
01196    timestamp = ntohl(rtpheader[1]);
01197    ssrc = ntohl(rtpheader[2]);
01198    
01199    if (!mark && rtp->rxssrc && rtp->rxssrc != ssrc) {
01200       if (option_debug || rtpdebug)
01201          ast_log(LOG_DEBUG, "Forcing Marker bit, because SSRC has changed\n");
01202       mark = 1;
01203    }
01204 
01205    rtp->rxssrc = ssrc;
01206    
01207    if (padding) {
01208       /* Remove padding bytes */
01209       res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
01210    }
01211    
01212    if (cc) {
01213       /* CSRC fields present */
01214       hdrlen += cc*4;
01215    }
01216 
01217    if (ext) {
01218       /* RTP Extension present */
01219       hdrlen += (ntohl(rtpheader[hdrlen/4]) & 0xffff) << 2;
01220       hdrlen += 4;
01221       if (option_debug) {
01222          int profile;
01223          profile = (ntohl(rtpheader[3]) & 0xffff0000) >> 16;
01224          if (profile == 0x505a)
01225             ast_log(LOG_DEBUG, "Found Zfone extension in RTP stream - zrtp - not supported.\n");
01226          else
01227             ast_log(LOG_DEBUG, "Found unknown RTP Extensions %x\n", profile);
01228       }
01229    }
01230 
01231    if (res < hdrlen) {
01232       ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
01233       return &ast_null_frame;
01234    }
01235 
01236    rtp->rxcount++; /* Only count reasonably valid packets, this'll make the rtcp stats more accurate */
01237 
01238    if (rtp->rxcount==1) {
01239       /* This is the first RTP packet successfully received from source */
01240       rtp->seedrxseqno = seqno;
01241    }
01242 
01243    /* Do not schedule RR if RTCP isn't run */
01244    if (rtp->rtcp && rtp->rtcp->them.sin_addr.s_addr && rtp->rtcp->schedid < 1) {
01245       /* Schedule transmission of Receiver Report */
01246       rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
01247    }
01248    if ( (int)rtp->lastrxseqno - (int)seqno  > 100) /* if so it would indicate that the sender cycled; allow for misordering */
01249       rtp->cycles += RTP_SEQ_MOD;
01250 
01251    rtp->lastrxseqno = seqno;
01252    
01253    if (rtp->themssrc==0)
01254       rtp->themssrc = ntohl(rtpheader[2]); /* Record their SSRC to put in future RR */
01255    
01256    if (rtp_debug_test_addr(&sin))
01257       ast_verbose("Got  RTP packet from    %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
01258          ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
01259 
01260    rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
01261    if (!rtpPT.isAstFormat) {
01262       struct ast_frame *f = NULL;
01263 
01264       /* This is special in-band data that's not one of our codecs */
01265       if (rtpPT.code == AST_RTP_DTMF) {
01266          /* It's special -- rfc2833 process it */
01267          if (rtp_debug_test_addr(&sin)) {
01268             unsigned char *data;
01269             unsigned int event;
01270             unsigned int event_end;
01271             unsigned int duration;
01272             data = rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen;
01273             event = ntohl(*((unsigned int *)(data)));
01274             event >>= 24;
01275             event_end = ntohl(*((unsigned int *)(data)));
01276             event_end <<= 8;
01277             event_end >>= 24;
01278             duration = ntohl(*((unsigned int *)(data)));
01279             duration &= 0xFFFF;
01280             ast_verbose("Got  RTP RFC2833 from   %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u, mark %d, event %08x, end %d, duration %-5.5d) \n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp, res - hdrlen, (mark?1:0), event, ((event_end & 0x80)?1:0), duration);
01281          }
01282          f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp);
01283       } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
01284          /* It's really special -- process it the Cisco way */
01285          if (rtp->lastevent <= seqno || (rtp->lastevent >= 65530 && seqno <= 6)) {
01286             f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
01287             rtp->lastevent = seqno;
01288          }
01289       } else if (rtpPT.code == AST_RTP_CN) {
01290          /* Comfort Noise */
01291          f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
01292       } else {
01293          ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n", payloadtype, ast_inet_ntoa(rtp->them.sin_addr));
01294       }
01295       return f ? f : &ast_null_frame;
01296    }
01297    rtp->lastrxformat = rtp->f.subclass = rtpPT.code;
01298    rtp->f.frametype = (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) ? AST_FRAME_VOICE : AST_FRAME_VIDEO;
01299 
01300    if (!rtp->lastrxts)
01301       rtp->lastrxts = timestamp;
01302 
01303    rtp->rxseqno = seqno;
01304 
01305    /* Record received timestamp as last received now */
01306    rtp->lastrxts = timestamp;
01307 
01308    rtp->f.mallocd = 0;
01309    rtp->f.datalen = res - hdrlen;
01310    rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
01311    rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
01312    rtp->f.seqno = seqno;
01313    if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
01314       rtp->f.samples = ast_codec_get_samples(&rtp->f);
01315       if (rtp->f.subclass == AST_FORMAT_SLINEAR) 
01316          ast_frame_byteswap_be(&rtp->f);
01317       calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
01318       /* Add timing data to let ast_generic_bridge() put the frame into a jitterbuf */
01319       rtp->f.has_timing_info = 1;
01320       rtp->f.ts = timestamp / 8;
01321       rtp->f.len = rtp->f.samples / 8;
01322    } else {
01323       /* Video -- samples is # of samples vs. 90000 */
01324       if (!rtp->lastividtimestamp)
01325          rtp->lastividtimestamp = timestamp;
01326       rtp->f.samples = timestamp - rtp->lastividtimestamp;
01327       rtp->lastividtimestamp = timestamp;
01328       rtp->f.delivery.tv_sec = 0;
01329       rtp->f.delivery.tv_usec = 0;
01330       if (mark)
01331          rtp->f.subclass |= 0x1;
01332       
01333    }
01334    rtp->f.src = "RTP";
01335    return &rtp->f;
01336 }

int ast_rtp_reload ( void   ) 

Definition at line 3687 of file rtp.c.

References ast_config_destroy(), ast_config_load(), ast_false(), ast_log(), ast_variable_retrieve(), ast_verbose(), DEFAULT_DTMF_TIMEOUT, LOG_WARNING, option_verbose, RTCP_MAX_INTERVALMS, RTCP_MIN_INTERVALMS, s, and VERBOSE_PREFIX_2.

Referenced by ast_rtp_init().

03688 {
03689    struct ast_config *cfg;
03690    const char *s;
03691 
03692    rtpstart = 5000;
03693    rtpend = 31000;
03694    dtmftimeout = DEFAULT_DTMF_TIMEOUT;
03695    cfg = ast_config_load("rtp.conf");
03696    if (cfg) {
03697       if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
03698          rtpstart = atoi(s);
03699          if (rtpstart < 1024)
03700             rtpstart = 1024;
03701          if (rtpstart > 65535)
03702             rtpstart = 65535;
03703       }
03704       if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
03705          rtpend = atoi(s);
03706          if (rtpend < 1024)
03707             rtpend = 1024;
03708          if (rtpend > 65535)
03709             rtpend = 65535;
03710       }
03711       if ((s = ast_variable_retrieve(cfg, "general", "rtcpinterval"))) {
03712          rtcpinterval = atoi(s);
03713          if (rtcpinterval == 0)
03714             rtcpinterval = 0; /* Just so we're clear... it's zero */
03715          if (rtcpinterval < RTCP_MIN_INTERVALMS)
03716             rtcpinterval = RTCP_MIN_INTERVALMS; /* This catches negative numbers too */
03717          if (rtcpinterval > RTCP_MAX_INTERVALMS)
03718             rtcpinterval = RTCP_MAX_INTERVALMS;
03719       }
03720       if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
03721 #ifdef SO_NO_CHECK
03722          if (ast_false(s))
03723             nochecksums = 1;
03724          else
03725             nochecksums = 0;
03726 #else
03727          if (ast_false(s))
03728             ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
03729 #endif
03730       }
03731       if ((s = ast_variable_retrieve(cfg, "general", "dtmftimeout"))) {
03732          dtmftimeout = atoi(s);
03733          if ((dtmftimeout < 0) || (dtmftimeout > 20000)) {
03734             ast_log(LOG_WARNING, "DTMF timeout of '%d' outside range, using default of '%d' instead\n",
03735                dtmftimeout, DEFAULT_DTMF_TIMEOUT);
03736             dtmftimeout = DEFAULT_DTMF_TIMEOUT;
03737          };
03738       }
03739       ast_config_destroy(cfg);
03740    }
03741    if (rtpstart >= rtpend) {
03742       ast_log(LOG_WARNING, "Unreasonable values for RTP start/end port in rtp.conf\n");
03743       rtpstart = 5000;
03744       rtpend = 31000;
03745    }
03746    if (option_verbose > 1)
03747       ast_verbose(VERBOSE_PREFIX_2 "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
03748    return 0;
03749 }

void ast_rtp_reset ( struct ast_rtp rtp  ) 

Definition at line 2046 of file rtp.c.

References ast_rtp::dtmfcount, ast_rtp::dtmfmute, ast_rtp::dtmfsamples, ast_rtp::lastdigitts, ast_rtp::lastevent, ast_rtp::lasteventseqn, ast_rtp::lastividtimestamp, ast_rtp::lastovidtimestamp, ast_rtp::lastrxformat, ast_rtp::lastrxts, ast_rtp::lastts, ast_rtp::lasttxformat, ast_rtp::rxcore, ast_rtp::rxseqno, ast_rtp::seqno, and ast_rtp::txcore.

02047 {
02048    memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
02049    memset(&rtp->txcore, 0, sizeof(rtp->txcore));
02050    memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
02051    rtp->lastts = 0;
02052    rtp->lastdigitts = 0;
02053    rtp->lastrxts = 0;
02054    rtp->lastividtimestamp = 0;
02055    rtp->lastovidtimestamp = 0;
02056    rtp->lasteventseqn = 0;
02057    rtp->lastevent = 0;
02058    rtp->lasttxformat = 0;
02059    rtp->lastrxformat = 0;
02060    rtp->dtmfcount = 0;
02061    rtp->dtmfsamples = 0;
02062    rtp->seqno = 0;
02063    rtp->rxseqno = 0;
02064 }

int ast_rtp_sendcng ( struct ast_rtp rtp,
int  level 
)

generate comfort noice (CNG)

Definition at line 2556 of file rtp.c.

References ast_inet_ntoa(), ast_log(), AST_RTP_CN, ast_rtp_lookup_code(), ast_tvadd(), ast_verbose(), ast_rtp::data, ast_rtp::dtmfmute, ast_rtp::lastts, LOG_ERROR, rtp_debug_test_addr(), ast_rtp::s, ast_rtp::seqno, ast_rtp::ssrc, and ast_rtp::them.

Referenced by do_monitor().

02557 {
02558    unsigned int *rtpheader;
02559    int hdrlen = 12;
02560    int res;
02561    int payload;
02562    char data[256];
02563    level = 127 - (level & 0x7f);
02564    payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_CN);
02565 
02566    /* If we have no peer, return immediately */ 
02567    if (!rtp->them.sin_addr.s_addr)
02568       return 0;
02569 
02570    rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
02571 
02572    /* Get a pointer to the header */
02573    rtpheader = (unsigned int *)data;
02574    rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
02575    rtpheader[1] = htonl(rtp->lastts);
02576    rtpheader[2] = htonl(rtp->ssrc); 
02577    data[12] = level;
02578    if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
02579       res = sendto(rtp->s, (void *)rtpheader, hdrlen + 1, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
02580       if (res <0) 
02581          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));
02582       if (rtp_debug_test_addr(&rtp->them))
02583          ast_verbose("Sent Comfort Noise RTP packet to %s:%u (type %d, seq %u, ts %u, len %d)\n"
02584                , ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);         
02585          
02586    }
02587    return 0;
02588 }

int ast_rtp_senddigit_begin ( struct ast_rtp rtp,
char  digit 
)

Send begin frames for DTMF.

Definition at line 2156 of file rtp.c.

References ast_inet_ntoa(), ast_log(), AST_RTP_DTMF, ast_rtp_lookup_code(), ast_tvadd(), ast_verbose(), ast_rtp::dtmfmute, ast_rtp::lastdigitts, LOG_ERROR, LOG_WARNING, rtp_debug_test_addr(), ast_rtp::s, ast_rtp::send_digit, ast_rtp::send_duration, ast_rtp::send_payload, ast_rtp::sending_digit, ast_rtp::seqno, ast_rtp::ssrc, and ast_rtp::them.

Referenced by oh323_digit_begin(), and sip_senddigit_begin().

02157 {
02158    unsigned int *rtpheader;
02159    int hdrlen = 12, res = 0, i = 0, payload = 0;
02160    char data[256];
02161 
02162    if ((digit <= '9') && (digit >= '0'))
02163       digit -= '0';
02164    else if (digit == '*')
02165       digit = 10;
02166    else if (digit == '#')
02167       digit = 11;
02168    else if ((digit >= 'A') && (digit <= 'D'))
02169       digit = digit - 'A' + 12;
02170    else if ((digit >= 'a') && (digit <= 'd'))
02171       digit = digit - 'a' + 12;
02172    else {
02173       ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
02174       return 0;
02175    }
02176 
02177    /* If we have no peer, return immediately */ 
02178    if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
02179       return 0;
02180 
02181    payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
02182 
02183    rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
02184    rtp->send_duration = 160;
02185    
02186    /* Get a pointer to the header */
02187    rtpheader = (unsigned int *)data;
02188    rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
02189    rtpheader[1] = htonl(rtp->lastdigitts);
02190    rtpheader[2] = htonl(rtp->ssrc); 
02191 
02192    for (i = 0; i < 2; i++) {
02193       rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
02194       res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
02195       if (res < 0) 
02196          ast_log(LOG_ERROR, "RTP Transmission error to %s:%u: %s\n",
02197             ast_inet_ntoa(rtp->them.sin_addr),
02198             ntohs(rtp->them.sin_port), strerror(errno));
02199       if (rtp_debug_test_addr(&rtp->them))
02200          ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
02201                 ast_inet_ntoa(rtp->them.sin_addr),
02202                 ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
02203       /* Increment sequence number */
02204       rtp->seqno++;
02205       /* Increment duration */
02206       rtp->send_duration += 160;
02207       /* Clear marker bit and set seqno */
02208       rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
02209    }
02210 
02211    /* Since we received a begin, we can safely store the digit and disable any compensation */
02212    rtp->sending_digit = 1;
02213    rtp->send_digit = digit;
02214    rtp->send_payload = payload;
02215 
02216    return 0;
02217 }

int ast_rtp_senddigit_end ( struct ast_rtp rtp,
char  digit 
)

void ast_rtp_set_callback ( struct ast_rtp rtp,
ast_rtp_callback  callback 
)

Definition at line 585 of file rtp.c.

References ast_rtp::callback.

Referenced by start_rtp().

00586 {
00587    rtp->callback = callback;
00588 }

void ast_rtp_set_data ( struct ast_rtp rtp,
void *  data 
)

Definition at line 580 of file rtp.c.

References ast_rtp::data.

Referenced by start_rtp().

00581 {
00582    rtp->data = data;
00583 }

void ast_rtp_set_m_type ( struct ast_rtp rtp,
int  pt 
)

Make a note of a RTP payload type that was seen in a SDP "m=" line. By default, use the well-known value for this type (although it may still be set to a different value by a subsequent "a=rtpmap:" line).

Definition at line 1645 of file rtp.c.

References ast_mutex_lock(), ast_mutex_unlock(), ast_rtp::bridge_lock, ast_rtp::current_RTP_PT, MAX_RTP_PT, and static_RTP_PT.

Referenced by gtalk_newcall(), and process_sdp().

01646 {
01647    if (pt < 0 || pt > MAX_RTP_PT || static_RTP_PT[pt].code == 0) 
01648       return; /* bogus payload type */
01649 
01650    ast_mutex_lock(&rtp->bridge_lock);
01651    rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
01652    ast_mutex_unlock(&rtp->bridge_lock);
01653 } 

void ast_rtp_set_peer ( struct ast_rtp rtp,
struct sockaddr_in *  them 
)

Definition at line 1989 of file rtp.c.

References ast_rtp::rtcp, ast_rtp::rxseqno, ast_rtcp::them, and ast_rtp::them.

Referenced by handle_open_receive_channel_ack_message(), process_sdp(), and setup_rtp_connection().

01990 {
01991    rtp->them.sin_port = them->sin_port;
01992    rtp->them.sin_addr = them->sin_addr;
01993    if (rtp->rtcp) {
01994       rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
01995       rtp->rtcp->them.sin_addr = them->sin_addr;
01996    }
01997    rtp->rxseqno = 0;
01998 }

void ast_rtp_set_rtpholdtimeout ( struct ast_rtp rtp,
int  timeout 
)

Set rtp hold timeout.

Definition at line 547 of file rtp.c.

References ast_rtp::rtpholdtimeout.

Referenced by create_addr_from_peer(), do_monitor(), and sip_alloc().

00548 {
00549    rtp->rtpholdtimeout = timeout;
00550 }

void ast_rtp_set_rtpkeepalive ( struct ast_rtp rtp,
int  period 
)

set RTP keepalive interval

Definition at line 553 of file rtp.c.

References ast_rtp::rtpkeepalive.

Referenced by create_addr_from_peer(), and sip_alloc().

00554 {
00555    rtp->rtpkeepalive = period;
00556 }

void ast_rtp_set_rtpmap_type ( struct ast_rtp rtp,
int  pt,
char *  mimeType,
char *  mimeSubtype,
enum ast_rtp_options  options 
)

Make a note of a RTP payload type (with MIME type) that was seen in an SDP "a=rtpmap:" line.

Definition at line 1658 of file rtp.c.

References AST_FORMAT_G726, AST_FORMAT_G726_AAL2, ast_mutex_lock(), ast_mutex_unlock(), AST_RTP_OPT_G726_NONSTANDARD, ast_rtp::bridge_lock, rtpPayloadType::code, ast_rtp::current_RTP_PT, MAX_RTP_PT, mimeTypes, payloadType, subtype, and type.

Referenced by __oh323_rtp_create(), gtalk_newcall(), process_sdp(), and set_dtmf_payload().

01661 {
01662    unsigned int i;
01663 
01664    if (pt < 0 || pt > MAX_RTP_PT) 
01665       return; /* bogus payload type */
01666    
01667    ast_mutex_lock(&rtp->bridge_lock);
01668 
01669    for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
01670       if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
01671           strcasecmp(mimeType, mimeTypes[i].type) == 0) {
01672          rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
01673          if ((mimeTypes[i].payloadType.code == AST_FORMAT_G726) &&
01674              mimeTypes[i].payloadType.isAstFormat &&
01675              (options & AST_RTP_OPT_G726_NONSTANDARD))
01676             rtp->current_RTP_PT[pt].code = AST_FORMAT_G726_AAL2;
01677          break;
01678       }
01679    }
01680 
01681    ast_mutex_unlock(&rtp->bridge_lock);
01682 
01683    return;
01684 } 

void ast_rtp_set_rtptimeout ( struct ast_rtp rtp,
int  timeout 
)

Set rtp timeout.

Definition at line 541 of file rtp.c.

References ast_rtp::rtptimeout.

Referenced by create_addr_from_peer(), do_monitor(), and sip_alloc().

00542 {
00543    rtp->rtptimeout = timeout;
00544 }

void ast_rtp_set_rtptimers_onhold ( struct ast_rtp rtp  ) 

Definition at line 534 of file rtp.c.

References ast_rtp::rtpholdtimeout, and ast_rtp::rtptimeout.

Referenced by handle_response_invite().

00535 {
00536    rtp->rtptimeout = (-1) * rtp->rtptimeout;
00537    rtp->rtpholdtimeout = (-1) * rtp->rtpholdtimeout;
00538 }

void ast_rtp_setdtmf ( struct ast_rtp rtp,
int  dtmf 
)

Indicate whether this RTP session is carrying DTMF or not.

Definition at line 600 of file rtp.c.

References ast_set2_flag, and FLAG_HAS_DTMF.

Referenced by create_addr_from_peer(), handle_request_invite(), process_sdp(), sip_alloc(), and sip_dtmfmode().

00601 {
00602    ast_set2_flag(rtp, dtmf ? 1 : 0, FLAG_HAS_DTMF);
00603 }

void ast_rtp_setdtmfcompensate ( struct ast_rtp rtp,
int  compensate 
)

Compensate for devices that send RFC2833 packets all at once.

Definition at line 605 of file rtp.c.

References ast_set2_flag, and FLAG_DTMF_COMPENSATE.

Referenced by create_addr_from_peer(), handle_request_invite(), process_sdp(), and sip_alloc().

00606 {
00607    ast_set2_flag(rtp, compensate ? 1 : 0, FLAG_DTMF_COMPENSATE);
00608 }

void ast_rtp_setnat ( struct ast_rtp rtp,
int  nat 
)

Definition at line 590 of file rtp.c.

References ast_rtp::nat.

Referenced by __oh323_rtp_create(), do_setnat(), oh323_rtp_read(), and start_rtp().

00591 {
00592    rtp->nat = nat;
00593 }

void ast_rtp_setstun ( struct ast_rtp rtp,
int  stun_enable 
)

Enable STUN capability.

Definition at line 610 of file rtp.c.

References ast_set2_flag, and FLAG_HAS_STUN.

Referenced by gtalk_new().

00611 {
00612    ast_set2_flag(rtp, stun_enable ? 1 : 0, FLAG_HAS_STUN);
00613 }

int ast_rtp_settos ( struct ast_rtp rtp,
int  tos 
)

Definition at line 1980 of file rtp.c.

References ast_log(), LOG_WARNING, and ast_rtp::s.

Referenced by __oh323_rtp_create(), and sip_alloc().

01981 {
01982    int res;
01983 
01984    if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) 
01985       ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
01986    return res;
01987 }

void ast_rtp_stop ( struct ast_rtp rtp  ) 

Definition at line 2029 of file rtp.c.

References ast_clear_flag, ast_sched_del(), FLAG_P2P_SENT_MARK, ast_rtp::rtcp, ast_rtp::sched, ast_rtcp::schedid, ast_rtcp::them, and ast_rtp::them.

Referenced by process_sdp(), setup_rtp_connection(), and stop_media_flows().

02030 {
02031    if (rtp->rtcp && rtp->rtcp->schedid > 0) {
02032       ast_sched_del(rtp->sched, rtp->rtcp->schedid);
02033       rtp->rtcp->schedid = -1;
02034    }
02035 
02036    memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
02037    memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
02038    if (rtp->rtcp) {
02039       memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->rtcp->them.sin_addr));
02040       memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->rtcp->them.sin_port));
02041    }
02042    
02043    ast_clear_flag(rtp, FLAG_P2P_SENT_MARK);
02044 }

void ast_rtp_stun_request ( struct ast_rtp rtp,
struct sockaddr_in *  suggestion,
const char *  username 
)

Definition at line 402 of file rtp.c.

References append_attr_string(), stun_attr::attr, ast_rtp::s, STUN_BINDREQ, stun_req_id(), stun_send(), and STUN_USERNAME.

Referenced by gtalk_update_stun().

00403 {
00404    struct stun_header *req;
00405    unsigned char reqdata[1024];
00406    int reqlen, reqleft;
00407    struct stun_attr *attr;
00408 
00409    req = (struct stun_header *)reqdata;
00410    stun_req_id(req);
00411    reqlen = 0;
00412    reqleft = sizeof(reqdata) - sizeof(struct stun_header);
00413    req->msgtype = 0;
00414    req->msglen = 0;
00415    attr = (struct stun_attr *)req->ies;
00416    if (username)
00417       append_attr_string(&attr, STUN_USERNAME, username, &reqlen, &reqleft);
00418    req->msglen = htons(reqlen);
00419    req->msgtype = htons(STUN_BINDREQ);
00420    stun_send(rtp->s, suggestion, req);
00421 }

int ast_rtp_write ( struct ast_rtp rtp,
struct ast_frame f 
)

Definition at line 2708 of file rtp.c.

References ast_codec_pref_getsize(), AST_FORMAT_SPEEX, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frdup(), ast_frfree(), ast_getformatname(), ast_log(), ast_rtp_lookup_code(), ast_rtp_raw_write(), ast_smoother_feed, ast_smoother_feed_be, AST_SMOOTHER_FLAG_BE, ast_smoother_free(), ast_smoother_new(), ast_smoother_read(), ast_smoother_set_flags(), ast_smoother_test_flag(), ast_frame::datalen, f, fmt, ast_frame::frametype, ast_rtp::lasttxformat, LOG_DEBUG, LOG_WARNING, ast_frame::offset, option_debug, ast_rtp::pref, ast_rtp::smoother, ast_frame::subclass, and ast_rtp::them.

Referenced by gtalk_write(), mgcp_write(), oh323_write(), sip_write(), and skinny_write().

02709 {
02710    struct ast_frame *f;
02711    int codec;
02712    int hdrlen = 12;
02713    int subclass;
02714    
02715 
02716    /* If we have no peer, return immediately */ 
02717    if (!rtp->them.sin_addr.s_addr)
02718       return 0;
02719 
02720    /* If there is no data length, return immediately */
02721    if (!_f->datalen) 
02722       return 0;
02723    
02724    /* Make sure we have enough space for RTP header */
02725    if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
02726       ast_log(LOG_WARNING, "RTP can only send voice and video\n");
02727       return -1;
02728    }
02729 
02730    subclass = _f->subclass;
02731    if (_f->frametype == AST_FRAME_VIDEO)
02732       subclass &= ~0x1;
02733 
02734    codec = ast_rtp_lookup_code(rtp, 1, subclass);
02735    if (codec < 0) {
02736       ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
02737       return -1;
02738    }
02739 
02740    if (rtp->lasttxformat != subclass) {
02741       /* New format, reset the smoother */
02742       if (option_debug)
02743          ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
02744       rtp->lasttxformat = subclass;
02745       if (rtp->smoother)
02746          ast_smoother_free(rtp->smoother);
02747       rtp->smoother = NULL;
02748    }
02749 
02750    if (!rtp->smoother && subclass != AST_FORMAT_SPEEX) {
02751       struct ast_format_list fmt = ast_codec_pref_getsize(&rtp->pref, subclass);
02752       if (fmt.inc_ms) { /* if codec parameters is set / avoid division by zero */
02753          if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
02754             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));
02755             return -1;
02756          }
02757          if (fmt.flags)
02758             ast_smoother_set_flags(rtp->smoother, fmt.flags);
02759          if (option_debug)
02760             ast_log(LOG_DEBUG, "Created smoother: format: %d ms: %d len: %d\n", subclass, fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
02761       }
02762    }
02763    if (rtp->smoother) {
02764       if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
02765          ast_smoother_feed_be(rtp->smoother, _f);
02766       } else {
02767          ast_smoother_feed(rtp->smoother, _f);
02768       }
02769 
02770       while((f = ast_smoother_read(rtp->smoother)) && (f->data))
02771          ast_rtp_raw_write(rtp, f, codec);
02772    } else {
02773            /* Don't buffer outgoing frames; send them one-per-packet: */
02774       if (_f->offset < hdrlen) {
02775          f = ast_frdup(_f);
02776       } else {
02777          f = _f;
02778       }
02779       if (f->data)
02780          ast_rtp_raw_write(rtp, f, codec);
02781       if (f != _f)
02782          ast_frfree(f);
02783    }
02784       
02785    return 0;
02786 }


Generated on Fri Aug 24 02:27:24 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1