Thu Oct 8 00:59:50 2009

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"

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.
void ast_rtp_new_source (struct ast_rtp *rtp)
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_alt_peer (struct ast_rtp *rtp, struct sockaddr_in *alt)
 set potential alternate source for RTP media
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)
 Activate payload type.
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
int ast_rtp_set_rtpmap_type (struct ast_rtp *rtp, int pt, char *mimeType, char *mimeSubtype, enum ast_rtp_options options)
 Initiate payload type to a known MIME media type for a codec.
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)
void ast_rtp_unset_m_type (struct ast_rtp *rtp, int pt)
 clear payload type
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(), ast_rtp_set_rtpmap_type(), ast_rtp_unset_m_type(), and process_sdp().


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 520 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().

00521 {
00522    if (rtp->rtcp)
00523       return rtp->rtcp->s;
00524    return -1;
00525 }

struct ast_frame* ast_rtcp_read ( struct ast_rtp rtp  ) 

Definition at line 869 of file rtp.c.

References ast_rtcp::accumulated_transit, ast_rtcp::altthem, ast_assert, AST_CONTROL_VIDUPDATE, AST_FRAME_CONTROL, AST_FRIENDLY_OFFSET, ast_inet_ntoa(), ast_log(), ast_null_frame, ast_verbose(), ast_frame::datalen, errno, 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_rtp::s, 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().

00870 {
00871    socklen_t len;
00872    int position, i, packetwords;
00873    int res;
00874    struct sockaddr_in sin;
00875    unsigned int rtcpdata[8192 + AST_FRIENDLY_OFFSET];
00876    unsigned int *rtcpheader;
00877    int pt;
00878    struct timeval now;
00879    unsigned int length;
00880    int rc;
00881    double rttsec;
00882    uint64_t rtt = 0;
00883    unsigned int dlsr;
00884    unsigned int lsr;
00885    unsigned int msw;
00886    unsigned int lsw;
00887    unsigned int comp;
00888    struct ast_frame *f = &ast_null_frame;
00889    
00890    if (!rtp || !rtp->rtcp)
00891       return &ast_null_frame;
00892 
00893    len = sizeof(sin);
00894    
00895    res = recvfrom(rtp->rtcp->s, rtcpdata + AST_FRIENDLY_OFFSET, sizeof(rtcpdata) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET,
00896                0, (struct sockaddr *)&sin, &len);
00897    if (option_debug > 2)
00898       ast_log(LOG_DEBUG, "socket RTCP read: rtp %i rtcp %i\n", rtp->s, rtp->rtcp->s);
00899 
00900    rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
00901    
00902    if (res < 0) {
00903       ast_assert(errno != EBADF);
00904       if (errno != EAGAIN) {
00905          ast_log(LOG_WARNING, "RTCP Read error: %s.  Hanging up.\n", strerror(errno));
00906          ast_log(LOG_WARNING, "socket RTCP read: rtp %i rtcp %i\n", rtp->s, rtp->rtcp->s);
00907          return NULL;
00908       }
00909       return &ast_null_frame;
00910    }
00911 
00912    packetwords = res / 4;
00913 
00914    if (rtp->nat) {
00915       /* Send to whoever sent to us */
00916       if (((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
00917           (rtp->rtcp->them.sin_port != sin.sin_port)) &&
00918           ((rtp->rtcp->altthem.sin_addr.s_addr != sin.sin_addr.s_addr) ||
00919           (rtp->rtcp->altthem.sin_port != sin.sin_port))) {
00920          memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
00921          if (option_debug || rtpdebug)
00922             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));
00923       }
00924    }
00925 
00926    if (option_debug)
00927       ast_log(LOG_DEBUG, "Got RTCP report of %d bytes\n", res);
00928 
00929    /* Process a compound packet */
00930    position = 0;
00931    while (position < packetwords) {
00932       i = position;
00933       length = ntohl(rtcpheader[i]);
00934       pt = (length & 0xff0000) >> 16;
00935       rc = (length & 0x1f000000) >> 24;
00936       length &= 0xffff;
00937     
00938       if ((i + length) > packetwords) {
00939          ast_log(LOG_WARNING, "RTCP Read too short\n");
00940          return &ast_null_frame;
00941       }
00942       
00943       if (rtcp_debug_test_addr(&sin)) {
00944          ast_verbose("\n\nGot RTCP from %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
00945          ast_verbose("PT: %d(%s)\n", pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown");
00946          ast_verbose("Reception reports: %d\n", rc);
00947          ast_verbose("SSRC of sender: %u\n", rtcpheader[i + 1]);
00948       }
00949     
00950       i += 2; /* Advance past header and ssrc */
00951       
00952       switch (pt) {
00953       case RTCP_PT_SR:
00954          gettimeofday(&rtp->rtcp->rxlsr,NULL); /* To be able to populate the dlsr */
00955          rtp->rtcp->spc = ntohl(rtcpheader[i+3]);
00956          rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
00957          rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16); /* Going to LSR in RR*/
00958     
00959          if (rtcp_debug_test_addr(&sin)) {
00960             ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader[i]), (unsigned long) ntohl(rtcpheader[i + 1]) * 4096);
00961             ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader[i + 2]));
00962             ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader[i + 3]), (unsigned long) ntohl(rtcpheader[i + 4]));
00963          }
00964          i += 5;
00965          if (rc < 1)
00966             break;
00967          /* Intentional fall through */
00968       case RTCP_PT_RR:
00969          /* Don't handle multiple reception reports (rc > 1) yet */
00970          /* Calculate RTT per RFC */
00971          gettimeofday(&now, NULL);
00972          timeval2ntp(now, &msw, &lsw);
00973          if (ntohl(rtcpheader[i + 4]) && ntohl(rtcpheader[i + 5])) { /* We must have the LSR && DLSR */
00974             comp = ((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16);
00975             lsr = ntohl(rtcpheader[i + 4]);
00976             dlsr = ntohl(rtcpheader[i + 5]);
00977             rtt = comp - lsr - dlsr;
00978 
00979             /* Convert end to end delay to usec (keeping the calculation in 64bit space)
00980                sess->ee_delay = (eedelay * 1000) / 65536; */
00981             if (rtt < 4294) {
00982                 rtt = (rtt * 1000000) >> 16;
00983             } else {
00984                 rtt = (rtt * 1000) >> 16;
00985                 rtt *= 1000;
00986             }
00987             rtt = rtt / 1000.;
00988             rttsec = rtt / 1000.;
00989 
00990             if (comp - dlsr >= lsr) {
00991                rtp->rtcp->accumulated_transit += rttsec;
00992                rtp->rtcp->rtt = rttsec;
00993                if (rtp->rtcp->maxrtt<rttsec)
00994                   rtp->rtcp->maxrtt = rttsec;
00995                if (rtp->rtcp->minrtt>rttsec)
00996                   rtp->rtcp->minrtt = rttsec;
00997             } else if (rtcp_debug_test_addr(&sin)) {
00998                ast_verbose("Internal RTCP NTP clock skew detected: "
00999                         "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
01000                         "diff=%d\n",
01001                         lsr, comp, dlsr, dlsr / 65536,
01002                         (dlsr % 65536) * 1000 / 65536,
01003                         dlsr - (comp - lsr));
01004             }
01005          }
01006 
01007          rtp->rtcp->reported_jitter = ntohl(rtcpheader[i + 3]);
01008          rtp->rtcp->reported_lost = ntohl(rtcpheader[i + 1]) & 0xffffff;
01009          if (rtcp_debug_test_addr(&sin)) {
01010             ast_verbose("  Fraction lost: %ld\n", (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24));
01011             ast_verbose("  Packets lost so far: %d\n", rtp->rtcp->reported_lost);
01012             ast_verbose("  Highest sequence number: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff));
01013             ast_verbose("  Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16);
01014             ast_verbose("  Interarrival jitter: %u\n", rtp->rtcp->reported_jitter);
01015             ast_verbose("  Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader[i + 4]) >> 16,((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096);
01016             ast_verbose("  DLSR: %4.4f (sec)\n",ntohl(rtcpheader[i + 5])/65536.0);
01017             if (rtt)
01018                ast_verbose("  RTT: %lu(sec)\n", (unsigned long) rtt);
01019          }
01020          break;
01021       case RTCP_PT_FUR:
01022          if (rtcp_debug_test_addr(&sin))
01023             ast_verbose("Received an RTCP Fast Update Request\n");
01024          rtp->f.frametype = AST_FRAME_CONTROL;
01025          rtp->f.subclass = AST_CONTROL_VIDUPDATE;
01026          rtp->f.datalen = 0;
01027          rtp->f.samples = 0;
01028          rtp->f.mallocd = 0;
01029          rtp->f.src = "RTP";
01030          f = &rtp->f;
01031          break;
01032       case RTCP_PT_SDES:
01033          if (rtcp_debug_test_addr(&sin))
01034             ast_verbose("Received an SDES from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
01035          break;
01036       case RTCP_PT_BYE:
01037          if (rtcp_debug_test_addr(&sin))
01038             ast_verbose("Received a BYE from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
01039          break;
01040       default:
01041          if (option_debug)
01042             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));
01043          break;
01044       }
01045       position += (length + 1);
01046    }
01047          
01048    return f;
01049 }

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 2426 of file rtp.c.

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

02427 {
02428    struct ast_rtp *rtp = data;
02429    int res;
02430 
02431    rtp->rtcp->sendfur = 1;
02432    res = ast_rtcp_write(data);
02433    
02434    return res;
02435 }

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 400 of file rtp.c.

Referenced by process_sdp().

00401 {
00402    return sizeof(struct ast_rtp);
00403 }

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 3402 of file rtp.c.

References AST_BRIDGE_FAILED, AST_BRIDGE_FAILED_NOWARN, ast_channel_lock, ast_channel_trylock, ast_channel_unlock, ast_check_hangup(), 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_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, ast_channel::name, 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.

03403 {
03404    struct ast_rtp *p0 = NULL, *p1 = NULL;    /* Audio RTP Channels */
03405    struct ast_rtp *vp0 = NULL, *vp1 = NULL;  /* Video RTP channels */
03406    struct ast_rtp_protocol *pr0 = NULL, *pr1 = NULL;
03407    enum ast_rtp_get_result audio_p0_res = AST_RTP_GET_FAILED, video_p0_res = AST_RTP_GET_FAILED;
03408    enum ast_rtp_get_result audio_p1_res = AST_RTP_GET_FAILED, video_p1_res = AST_RTP_GET_FAILED;
03409    enum ast_bridge_result res = AST_BRIDGE_FAILED;
03410    int codec0 = 0, codec1 = 0;
03411    void *pvt0 = NULL, *pvt1 = NULL;
03412 
03413    /* Lock channels */
03414    ast_channel_lock(c0);
03415    while(ast_channel_trylock(c1)) {
03416       ast_channel_unlock(c0);
03417       usleep(1);
03418       ast_channel_lock(c0);
03419    }
03420 
03421    /* Ensure neither channel got hungup during lock avoidance */
03422    if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
03423       ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
03424       ast_channel_unlock(c0);
03425       ast_channel_unlock(c1);
03426       return AST_BRIDGE_FAILED;
03427    }
03428       
03429    /* Find channel driver interfaces */
03430    if (!(pr0 = get_proto(c0))) {
03431       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
03432       ast_channel_unlock(c0);
03433       ast_channel_unlock(c1);
03434       return AST_BRIDGE_FAILED;
03435    }
03436    if (!(pr1 = get_proto(c1))) {
03437       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
03438       ast_channel_unlock(c0);
03439       ast_channel_unlock(c1);
03440       return AST_BRIDGE_FAILED;
03441    }
03442 
03443    /* Get channel specific interface structures */
03444    pvt0 = c0->tech_pvt;
03445    pvt1 = c1->tech_pvt;
03446 
03447    /* Get audio and video interface (if native bridge is possible) */
03448    audio_p0_res = pr0->get_rtp_info(c0, &p0);
03449    video_p0_res = pr0->get_vrtp_info ? pr0->get_vrtp_info(c0, &vp0) : AST_RTP_GET_FAILED;
03450    audio_p1_res = pr1->get_rtp_info(c1, &p1);
03451    video_p1_res = pr1->get_vrtp_info ? pr1->get_vrtp_info(c1, &vp1) : AST_RTP_GET_FAILED;
03452 
03453    /* If we are carrying video, and both sides are not reinviting... then fail the native bridge */
03454    if (video_p0_res != AST_RTP_GET_FAILED && (audio_p0_res != AST_RTP_TRY_NATIVE || video_p0_res != AST_RTP_TRY_NATIVE))
03455       audio_p0_res = AST_RTP_GET_FAILED;
03456    if (video_p1_res != AST_RTP_GET_FAILED && (audio_p1_res != AST_RTP_TRY_NATIVE || video_p1_res != AST_RTP_TRY_NATIVE))
03457       audio_p1_res = AST_RTP_GET_FAILED;
03458 
03459    /* Check if a bridge is possible (partial/native) */
03460    if (audio_p0_res == AST_RTP_GET_FAILED || audio_p1_res == AST_RTP_GET_FAILED) {
03461       /* Somebody doesn't want to play... */
03462       ast_channel_unlock(c0);
03463       ast_channel_unlock(c1);
03464       return AST_BRIDGE_FAILED_NOWARN;
03465    }
03466 
03467    /* If we need to feed DTMF frames into the core then only do a partial native bridge */
03468    if (ast_test_flag(p0, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
03469       ast_set_flag(p0, FLAG_P2P_NEED_DTMF);
03470       audio_p0_res = AST_RTP_TRY_PARTIAL;
03471    }
03472 
03473    if (ast_test_flag(p1, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)) {
03474       ast_set_flag(p1, FLAG_P2P_NEED_DTMF);
03475       audio_p1_res = AST_RTP_TRY_PARTIAL;
03476    }
03477 
03478    /* If both sides are not using the same method of DTMF transmission 
03479     * (ie: one is RFC2833, other is INFO... then we can not do direct media. 
03480     * --------------------------------------------------
03481     * | DTMF Mode |  HAS_DTMF  |  Accepts Begin Frames |
03482     * |-----------|------------|-----------------------|
03483     * | Inband    | False      | True                  |
03484     * | RFC2833   | True       | True                  |
03485     * | SIP INFO  | False      | False                 |
03486     * --------------------------------------------------
03487     * However, if DTMF from both channels is being monitored by the core, then
03488     * we can still do packet-to-packet bridging, because passing through the 
03489     * core will handle DTMF mode translation.
03490     */
03491    if ( (ast_test_flag(p0, FLAG_HAS_DTMF) != ast_test_flag(p1, FLAG_HAS_DTMF)) ||
03492        (!c0->tech->send_digit_begin != !c1->tech->send_digit_begin)) {
03493       if (!ast_test_flag(p0, FLAG_P2P_NEED_DTMF) || !ast_test_flag(p1, FLAG_P2P_NEED_DTMF)) {
03494          ast_channel_unlock(c0);
03495          ast_channel_unlock(c1);
03496          return AST_BRIDGE_FAILED_NOWARN;
03497       }
03498       audio_p0_res = AST_RTP_TRY_PARTIAL;
03499       audio_p1_res = AST_RTP_TRY_PARTIAL;
03500    }
03501 
03502    /* If we need to feed frames into the core don't do a P2P bridge */
03503    if ((audio_p0_res == AST_RTP_TRY_PARTIAL && ast_test_flag(p0, FLAG_P2P_NEED_DTMF)) ||
03504        (audio_p1_res == AST_RTP_TRY_PARTIAL && ast_test_flag(p1, FLAG_P2P_NEED_DTMF))) {
03505       ast_channel_unlock(c0);
03506       ast_channel_unlock(c1);
03507       return AST_BRIDGE_FAILED_NOWARN;
03508    }
03509 
03510    /* Get codecs from both sides */
03511    codec0 = pr0->get_codec ? pr0->get_codec(c0) : 0;
03512    codec1 = pr1->get_codec ? pr1->get_codec(c1) : 0;
03513    if (codec0 && codec1 && !(codec0 & codec1)) {
03514       /* Hey, we can't do native bridging if both parties speak different codecs */
03515       if (option_debug)
03516          ast_log(LOG_DEBUG, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0, codec1);
03517       ast_channel_unlock(c0);
03518       ast_channel_unlock(c1);
03519       return AST_BRIDGE_FAILED_NOWARN;
03520    }
03521 
03522    /* If either side can only do a partial bridge, then don't try for a true native bridge */
03523    if (audio_p0_res == AST_RTP_TRY_PARTIAL || audio_p1_res == AST_RTP_TRY_PARTIAL) {
03524       struct ast_format_list fmt0, fmt1;
03525 
03526       /* In order to do Packet2Packet bridging both sides must be in the same rawread/rawwrite */
03527       if (c0->rawreadformat != c1->rawwriteformat || c1->rawreadformat != c0->rawwriteformat) {
03528          if (option_debug)
03529             ast_log(LOG_DEBUG, "Cannot packet2packet bridge - raw formats are incompatible\n");
03530          ast_channel_unlock(c0);
03531          ast_channel_unlock(c1);
03532          return AST_BRIDGE_FAILED_NOWARN;
03533       }
03534       /* They must also be using the same packetization */
03535       fmt0 = ast_codec_pref_getsize(&p0->pref, c0->rawreadformat);
03536       fmt1 = ast_codec_pref_getsize(&p1->pref, c1->rawreadformat);
03537       if (fmt0.cur_ms != fmt1.cur_ms) {
03538          if (option_debug)
03539             ast_log(LOG_DEBUG, "Cannot packet2packet bridge - packetization settings prevent it\n");
03540          ast_channel_unlock(c0);
03541          ast_channel_unlock(c1);
03542          return AST_BRIDGE_FAILED_NOWARN;
03543       }
03544 
03545       if (option_verbose > 2)
03546          ast_verbose(VERBOSE_PREFIX_3 "Packet2Packet bridging %s and %s\n", c0->name, c1->name);
03547       res = bridge_p2p_loop(c0, c1, p0, p1, timeoutms, flags, fo, rc, pvt0, pvt1);
03548    } else {
03549       if (option_verbose > 2) 
03550          ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name);
03551       res = bridge_native_loop(c0, c1, p0, p1, vp0, vp1, pr0, pr1, codec0, codec1, timeoutms, flags, fo, rc, pvt0, pvt1);
03552    }
03553 
03554    return res;
03555 }

int ast_rtp_codec_getformat ( int  pt  ) 

Definition at line 2854 of file rtp.c.

References rtpPayloadType::code, and static_RTP_PT.

Referenced by process_sdp().

02855 {
02856    if (pt < 0 || pt > MAX_RTP_PT)
02857       return 0; /* bogus payload type */
02858 
02859    if (static_RTP_PT[pt].isAstFormat)
02860       return static_RTP_PT[pt].code;
02861    else
02862       return 0;
02863 }

struct ast_codec_pref* ast_rtp_codec_getpref ( struct ast_rtp rtp  ) 

Definition at line 2849 of file rtp.c.

References ast_rtp::pref.

Referenced by add_codec_to_sdp(), and process_sdp().

02850 {
02851    return &rtp->pref;
02852 }

int ast_rtp_codec_setpref ( struct ast_rtp rtp,
struct ast_codec_pref prefs 
)

Definition at line 2802 of file rtp.c.

References ast_codec_pref_getsize(), ast_log(), ast_smoother_new(), ast_smoother_reconfigure(), ast_smoother_set_flags(), ast_format_list::cur_ms, ast_format_list::flags, ast_format_list::fr_len, ast_format_list::inc_ms, ast_rtp::lasttxformat, LOG_DEBUG, LOG_WARNING, option_debug, 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(), sip_alloc(), start_rtp(), and transmit_response_with_sdp().

02803 {
02804    struct ast_format_list current_format_old, current_format_new;
02805 
02806    /* if no packets have been sent through this session yet, then
02807     *  changing preferences does not require any extra work
02808     */
02809    if (rtp->lasttxformat == 0) {
02810       rtp->pref = *prefs;
02811       return 0;
02812    }
02813 
02814    current_format_old = ast_codec_pref_getsize(&rtp->pref, rtp->lasttxformat);
02815 
02816    rtp->pref = *prefs;
02817 
02818    current_format_new = ast_codec_pref_getsize(&rtp->pref, rtp->lasttxformat);
02819 
02820    /* if the framing desired for the current format has changed, we may have to create
02821     * or adjust the smoother for this session
02822     */
02823    if ((current_format_new.inc_ms != 0) &&
02824        (current_format_new.cur_ms != current_format_old.cur_ms)) {
02825       int new_size = (current_format_new.cur_ms * current_format_new.fr_len) / current_format_new.inc_ms;
02826 
02827       if (rtp->smoother) {
02828          ast_smoother_reconfigure(rtp->smoother, new_size);
02829          if (option_debug) {
02830             ast_log(LOG_DEBUG, "Adjusted smoother to %d ms and %d bytes\n", current_format_new.cur_ms, new_size);
02831          }
02832       } else {
02833          if (!(rtp->smoother = ast_smoother_new(new_size))) {
02834             ast_log(LOG_WARNING, "Unable to create smoother: format: %d ms: %d len: %d\n", rtp->lasttxformat, current_format_new.cur_ms, new_size);
02835             return -1;
02836          }
02837          if (current_format_new.flags) {
02838             ast_smoother_set_flags(rtp->smoother, current_format_new.flags);
02839          }
02840          if (option_debug) {
02841             ast_log(LOG_DEBUG, "Created smoother: format: %d ms: %d len: %d\n", rtp->lasttxformat, current_format_new.cur_ms, new_size);
02842          }
02843       }
02844    }
02845 
02846    return 0;
02847 }

void ast_rtp_destroy ( struct ast_rtp rtp  ) 

Definition at line 2209 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(), sip_alloc(), skinny_hangup(), start_rtp(), and unalloc_sub().

02210 {
02211    if (rtcp_debug_test_addr(&rtp->them) || rtcpstats) {
02212       /*Print some info on the call here */
02213       ast_verbose("  RTP-stats\n");
02214       ast_verbose("* Our Receiver:\n");
02215       ast_verbose("  SSRC:     %u\n", rtp->themssrc);
02216       ast_verbose("  Received packets: %u\n", rtp->rxcount);
02217       ast_verbose("  Lost packets:   %u\n", rtp->rtcp ? (rtp->rtcp->expected_prior - rtp->rtcp->received_prior) : 0);
02218       ast_verbose("  Jitter:      %.4f\n", rtp->rxjitter);
02219       ast_verbose("  Transit:     %.4f\n", rtp->rxtransit);
02220       ast_verbose("  RR-count:    %u\n", rtp->rtcp ? rtp->rtcp->rr_count : 0);
02221       ast_verbose("* Our Sender:\n");
02222       ast_verbose("  SSRC:     %u\n", rtp->ssrc);
02223       ast_verbose("  Sent packets:   %u\n", rtp->txcount);
02224       ast_verbose("  Lost packets:   %u\n", rtp->rtcp ? rtp->rtcp->reported_lost : 0);
02225       ast_verbose("  Jitter:      %u\n", rtp->rtcp ? (rtp->rtcp->reported_jitter / (unsigned int)65536.0) : 0);
02226       ast_verbose("  SR-count:    %u\n", rtp->rtcp ? rtp->rtcp->sr_count : 0);
02227       ast_verbose("  RTT:      %f\n", rtp->rtcp ? rtp->rtcp->rtt : 0);
02228    }
02229 
02230    if (rtp->smoother)
02231       ast_smoother_free(rtp->smoother);
02232    if (rtp->ioid)
02233       ast_io_remove(rtp->io, rtp->ioid);
02234    if (rtp->s > -1)
02235       close(rtp->s);
02236    if (rtp->rtcp) {
02237       AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
02238       close(rtp->rtcp->s);
02239       free(rtp->rtcp);
02240       rtp->rtcp=NULL;
02241    }
02242 
02243    ast_mutex_destroy(&rtp->bridge_lock);
02244 
02245    free(rtp);
02246 }

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 1541 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, ast_channel::name, option_debug, and ast_rtp_protocol::set_rtp_peer.

Referenced by wait_for_answer().

01542 {
01543    struct ast_rtp *destp = NULL, *srcp = NULL;     /* Audio RTP Channels */
01544    struct ast_rtp *vdestp = NULL, *vsrcp = NULL;      /* Video RTP channels */
01545    struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
01546    enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED;
01547    enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED;
01548    int srccodec, destcodec, nat_active = 0;
01549 
01550    /* Lock channels */
01551    ast_channel_lock(dest);
01552    if (src) {
01553       while(ast_channel_trylock(src)) {
01554          ast_channel_unlock(dest);
01555          usleep(1);
01556          ast_channel_lock(dest);
01557       }
01558    }
01559 
01560    /* Find channel driver interfaces */
01561    destpr = get_proto(dest);
01562    if (src)
01563       srcpr = get_proto(src);
01564    if (!destpr) {
01565       if (option_debug)
01566          ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", dest->name);
01567       ast_channel_unlock(dest);
01568       if (src)
01569          ast_channel_unlock(src);
01570       return 0;
01571    }
01572    if (!srcpr) {
01573       if (option_debug)
01574          ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", src ? src->name : "<unspecified>");
01575       ast_channel_unlock(dest);
01576       if (src)
01577          ast_channel_unlock(src);
01578       return 0;
01579    }
01580 
01581    /* Get audio and video interface (if native bridge is possible) */
01582    audio_dest_res = destpr->get_rtp_info(dest, &destp);
01583    video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
01584    if (srcpr) {
01585       audio_src_res = srcpr->get_rtp_info(src, &srcp);
01586       video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
01587    }
01588 
01589    /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
01590    if (audio_dest_res != AST_RTP_TRY_NATIVE || (video_dest_res != AST_RTP_GET_FAILED && video_dest_res != AST_RTP_TRY_NATIVE)) {
01591       /* Somebody doesn't want to play... */
01592       ast_channel_unlock(dest);
01593       if (src)
01594          ast_channel_unlock(src);
01595       return 0;
01596    }
01597    if (audio_src_res == AST_RTP_TRY_NATIVE && (video_src_res == AST_RTP_GET_FAILED || video_src_res == AST_RTP_TRY_NATIVE) && srcpr->get_codec)
01598       srccodec = srcpr->get_codec(src);
01599    else
01600       srccodec = 0;
01601    if (audio_dest_res == AST_RTP_TRY_NATIVE && (video_dest_res == AST_RTP_GET_FAILED || video_dest_res == AST_RTP_TRY_NATIVE) && destpr->get_codec)
01602       destcodec = destpr->get_codec(dest);
01603    else
01604       destcodec = 0;
01605    /* Ensure we have at least one matching codec */
01606    if (srcp && !(srccodec & destcodec)) {
01607       ast_channel_unlock(dest);
01608       ast_channel_unlock(src);
01609       return 0;
01610    }
01611    /* Consider empty media as non-existant */
01612    if (audio_src_res == AST_RTP_TRY_NATIVE && !srcp->them.sin_addr.s_addr)
01613       srcp = NULL;
01614    /* If the client has NAT stuff turned on then just safe NAT is active */
01615    if (srcp && (srcp->nat || ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
01616       nat_active = 1;
01617    /* Bridge media early */
01618    if (destpr->set_rtp_peer(dest, srcp, vsrcp, srccodec, nat_active))
01619       ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src ? src->name : "<unspecified>");
01620    ast_channel_unlock(dest);
01621    if (src)
01622       ast_channel_unlock(src);
01623    if (option_debug)
01624       ast_log(LOG_DEBUG, "Setting early bridge SDP of '%s' with that of '%s'\n", dest->name, src ? src->name : "<unspecified>");
01625    return 1;
01626 }

int ast_rtp_fd ( struct ast_rtp rtp  ) 

Definition at line 515 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().

00516 {
00517    return rtp->s;
00518 }

struct ast_rtp* ast_rtp_get_bridged ( struct ast_rtp rtp  ) 

Definition at line 2119 of file rtp.c.

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

Referenced by __sip_destroy(), and ast_rtp_read().

02120 {
02121    struct ast_rtp *bridged = NULL;
02122 
02123    ast_mutex_lock(&rtp->bridge_lock);
02124    bridged = rtp->bridged;
02125    ast_mutex_unlock(&rtp->bridge_lock);
02126 
02127    return bridged;
02128 }

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 1762 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 gtalk_is_answered(), gtalk_newcall(), and process_sdp().

01764 {
01765    int pt;
01766    
01767    ast_mutex_lock(&rtp->bridge_lock);
01768    
01769    *astFormats = *nonAstFormats = 0;
01770    for (pt = 0; pt < MAX_RTP_PT; ++pt) {
01771       if (rtp->current_RTP_PT[pt].isAstFormat) {
01772          *astFormats |= rtp->current_RTP_PT[pt].code;
01773       } else {
01774          *nonAstFormats |= rtp->current_RTP_PT[pt].code;
01775       }
01776    }
01777    
01778    ast_mutex_unlock(&rtp->bridge_lock);
01779    
01780    return;
01781 }

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

Definition at line 2101 of file rtp.c.

References ast_rtp::them.

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

02102 {
02103    if ((them->sin_family != AF_INET) ||
02104       (them->sin_port != rtp->them.sin_port) ||
02105       (them->sin_addr.s_addr != rtp->them.sin_addr.s_addr)) {
02106       them->sin_family = AF_INET;
02107       them->sin_port = rtp->them.sin_port;
02108       them->sin_addr = rtp->them.sin_addr;
02109       return 1;
02110    }
02111    return 0;
02112 }

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

Return RTCP quality string.

Definition at line 2165 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().

02166 {
02167    /*
02168    *ssrc          our ssrc
02169    *themssrc      their ssrc
02170    *lp            lost packets
02171    *rxjitter      our calculated jitter(rx)
02172    *rxcount       no. received packets
02173    *txjitter      reported jitter of the other end
02174    *txcount       transmitted packets
02175    *rlp           remote lost packets
02176    *rtt           round trip time
02177    */
02178 
02179    if (qual && rtp) {
02180       qual->local_ssrc = rtp->ssrc;
02181       qual->local_jitter = rtp->rxjitter;
02182       qual->local_count = rtp->rxcount;
02183       qual->remote_ssrc = rtp->themssrc;
02184       qual->remote_count = rtp->txcount;
02185       if (rtp->rtcp) {
02186          qual->local_lostpackets = rtp->rtcp->expected_prior - rtp->rtcp->received_prior;
02187          qual->remote_lostpackets = rtp->rtcp->reported_lost;
02188          qual->remote_jitter = rtp->rtcp->reported_jitter / 65536.0;
02189          qual->rtt = rtp->rtcp->rtt;
02190       }
02191    }
02192    if (rtp->rtcp) {
02193       snprintf(rtp->rtcp->quality, sizeof(rtp->rtcp->quality),
02194          "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
02195          rtp->ssrc,
02196          rtp->themssrc,
02197          rtp->rtcp->expected_prior - rtp->rtcp->received_prior,
02198          rtp->rxjitter,
02199          rtp->rxcount,
02200          (double)rtp->rtcp->reported_jitter / 65536.0,
02201          rtp->txcount,
02202          rtp->rtcp->reported_lost,
02203          rtp->rtcp->rtt);
02204       return rtp->rtcp->quality;
02205    } else
02206       return "<Unknown> - RTP/RTCP has already been destroyed";
02207 }

int ast_rtp_get_rtpholdtimeout ( struct ast_rtp rtp  ) 

Get rtp hold timeout.

Definition at line 575 of file rtp.c.

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

Referenced by do_monitor().

00576 {
00577    if (rtp->rtptimeout < 0)   /* We're not checking, but remembering the setting (during T.38 transmission) */
00578       return 0;
00579    return rtp->rtpholdtimeout;
00580 }

int ast_rtp_get_rtpkeepalive ( struct ast_rtp rtp  ) 

Get RTP keepalive interval.

Definition at line 583 of file rtp.c.

References ast_rtp::rtpkeepalive.

Referenced by do_monitor().

00584 {
00585    return rtp->rtpkeepalive;
00586 }

int ast_rtp_get_rtptimeout ( struct ast_rtp rtp  ) 

Get rtp timeout.

Definition at line 567 of file rtp.c.

References 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->rtptimeout;
00572 }

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

Definition at line 2114 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().

02115 {
02116    *us = rtp->us;
02117 }

int ast_rtp_getnat ( struct ast_rtp rtp  ) 

Definition at line 603 of file rtp.c.

References ast_test_flag, and FLAG_NAT_ACTIVE.

Referenced by sip_get_rtp_peer().

00604 {
00605    return ast_test_flag(rtp, FLAG_NAT_ACTIVE);
00606 }

void ast_rtp_init ( void   ) 

Initialize the RTP system in Asterisk.

Definition at line 3940 of file rtp.c.

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

Referenced by main().

03941 {
03942    ast_cli_register_multiple(cli_rtp, sizeof(cli_rtp) / sizeof(struct ast_cli_entry));
03943    ast_rtp_reload();
03944 }

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 1805 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().

01806 {
01807    int pt = 0;
01808 
01809    ast_mutex_lock(&rtp->bridge_lock);
01810 
01811    if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
01812       code == rtp->rtp_lookup_code_cache_code) {
01813       /* Use our cached mapping, to avoid the overhead of the loop below */
01814       pt = rtp->rtp_lookup_code_cache_result;
01815       ast_mutex_unlock(&rtp->bridge_lock);
01816       return pt;
01817    }
01818 
01819    /* Check the dynamic list first */
01820    for (pt = 0; pt < MAX_RTP_PT; ++pt) {
01821       if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
01822          rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
01823          rtp->rtp_lookup_code_cache_code = code;
01824          rtp->rtp_lookup_code_cache_result = pt;
01825          ast_mutex_unlock(&rtp->bridge_lock);
01826          return pt;
01827       }
01828    }
01829 
01830    /* Then the static list */
01831    for (pt = 0; pt < MAX_RTP_PT; ++pt) {
01832       if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
01833          rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
01834          rtp->rtp_lookup_code_cache_code = code;
01835          rtp->rtp_lookup_code_cache_result = pt;
01836          ast_mutex_unlock(&rtp->bridge_lock);
01837          return pt;
01838       }
01839    }
01840 
01841    ast_mutex_unlock(&rtp->bridge_lock);
01842 
01843    return -1;
01844 }

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 1865 of file rtp.c.

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

Referenced by process_sdp().

01867 {
01868    int format;
01869    unsigned len;
01870    char *end = buf;
01871    char *start = buf;
01872 
01873    if (!buf || !size)
01874       return NULL;
01875 
01876    snprintf(end, size, "0x%x (", capability);
01877 
01878    len = strlen(end);
01879    end += len;
01880    size -= len;
01881    start = end;
01882 
01883    for (format = 1; format < AST_RTP_MAX; format <<= 1) {
01884       if (capability & format) {
01885          const char *name = ast_rtp_lookup_mime_subtype(isAstFormat, format, options);
01886 
01887          snprintf(end, size, "%s|", name);
01888          len = strlen(end);
01889          end += len;
01890          size -= len;
01891       }
01892    }
01893 
01894    if (start == end)
01895       snprintf(start, size, "nothing)"); 
01896    else if (size > 1)
01897       *(end -1) = ')';
01898    
01899    return buf;
01900 }

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 1846 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().

01848 {
01849    unsigned int i;
01850 
01851    for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
01852       if ((mimeTypes[i].payloadType.code == code) && (mimeTypes[i].payloadType.isAstFormat == isAstFormat)) {
01853          if (isAstFormat &&
01854              (code == AST_FORMAT_G726_AAL2) &&
01855              (options & AST_RTP_OPT_G726_NONSTANDARD))
01856             return "G726-32";
01857          else
01858             return mimeTypes[i].subtype;
01859       }
01860    }
01861 
01862    return "";
01863 }

struct rtpPayloadType ast_rtp_lookup_pt ( struct ast_rtp rtp,
int  pt 
)

Mapping between RTP payload format codes and Asterisk codes:.

Definition at line 1783 of file rtp.c.

References ast_mutex_lock, ast_mutex_unlock, rtpPayloadType::isAstFormat, MAX_RTP_PT, and static_RTP_PT.

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

01784 {
01785    struct rtpPayloadType result;
01786 
01787    result.isAstFormat = result.code = 0;
01788 
01789    if (pt < 0 || pt > MAX_RTP_PT) 
01790       return result; /* bogus payload type */
01791 
01792    /* Start with negotiated codecs */
01793    ast_mutex_lock(&rtp->bridge_lock);
01794    result = rtp->current_RTP_PT[pt];
01795    ast_mutex_unlock(&rtp->bridge_lock);
01796 
01797    /* If it doesn't exist, check our static RTP type list, just in case */
01798    if (!result.code) 
01799       result = static_RTP_PT[pt];
01800 
01801    return result;
01802 }

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

Definition at line 1628 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, ast_channel::name, option_debug, and ast_rtp_protocol::set_rtp_peer.

Referenced by wait_for_answer().

01629 {
01630    struct ast_rtp *destp = NULL, *srcp = NULL;     /* Audio RTP Channels */
01631    struct ast_rtp *vdestp = NULL, *vsrcp = NULL;      /* Video RTP channels */
01632    struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
01633    enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED;
01634    enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED; 
01635    int srccodec, destcodec;
01636 
01637    /* Lock channels */
01638    ast_channel_lock(dest);
01639    while(ast_channel_trylock(src)) {
01640       ast_channel_unlock(dest);
01641       usleep(1);
01642       ast_channel_lock(dest);
01643    }
01644 
01645    /* Find channel driver interfaces */
01646    if (!(destpr = get_proto(dest))) {
01647       if (option_debug)
01648          ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", dest->name);
01649       ast_channel_unlock(dest);
01650       ast_channel_unlock(src);
01651       return 0;
01652    }
01653    if (!(srcpr = get_proto(src))) {
01654       if (option_debug)
01655          ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", src->name);
01656       ast_channel_unlock(dest);
01657       ast_channel_unlock(src);
01658       return 0;
01659    }
01660 
01661    /* Get audio and video interface (if native bridge is possible) */
01662    audio_dest_res = destpr->get_rtp_info(dest, &destp);
01663    video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
01664    audio_src_res = srcpr->get_rtp_info(src, &srcp);
01665    video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
01666 
01667    /* Ensure we have at least one matching codec */
01668    if (srcpr->get_codec)
01669       srccodec = srcpr->get_codec(src);
01670    else
01671       srccodec = 0;
01672    if (destpr->get_codec)
01673       destcodec = destpr->get_codec(dest);
01674    else
01675       destcodec = 0;
01676 
01677    /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
01678    if (audio_dest_res != AST_RTP_TRY_NATIVE || (video_dest_res != AST_RTP_GET_FAILED && video_dest_res != AST_RTP_TRY_NATIVE) || audio_src_res != AST_RTP_TRY_NATIVE || (video_src_res != AST_RTP_GET_FAILED && video_src_res != AST_RTP_TRY_NATIVE) || !(srccodec & destcodec)) {
01679       /* Somebody doesn't want to play... */
01680       ast_channel_unlock(dest);
01681       ast_channel_unlock(src);
01682       return 0;
01683    }
01684    ast_rtp_pt_copy(destp, srcp);
01685    if (vdestp && vsrcp)
01686       ast_rtp_pt_copy(vdestp, vsrcp);
01687    if (media) {
01688       /* Bridge early */
01689       if (destpr->set_rtp_peer(dest, srcp, vsrcp, srccodec, ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
01690          ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src->name);
01691    }
01692    ast_channel_unlock(dest);
01693    ast_channel_unlock(src);
01694    if (option_debug)
01695       ast_log(LOG_DEBUG, "Seeded SDP of '%s' with that of '%s'\n", dest->name, src->name);
01696    return 1;
01697 }

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 2055 of file rtp.c.

References ast_rtp_new_with_bindaddr(), io, and sched.

02056 {
02057    struct in_addr ia;
02058 
02059    memset(&ia, 0, sizeof(ia));
02060    return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
02061 }

void ast_rtp_new_init ( struct ast_rtp rtp  ) 

Initialize a new RTP structure.

Definition at line 1949 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().

01950 {
01951    ast_mutex_init(&rtp->bridge_lock);
01952 
01953    rtp->them.sin_family = AF_INET;
01954    rtp->us.sin_family = AF_INET;
01955    rtp->ssrc = ast_random();
01956    rtp->seqno = ast_random() & 0xffff;
01957    ast_set_flag(rtp, FLAG_HAS_DTMF);
01958 
01959    return;
01960 }

void ast_rtp_new_source ( struct ast_rtp rtp  ) 

Definition at line 2072 of file rtp.c.

References ast_rtp::set_marker_bit.

Referenced by mgcp_indicate(), oh323_indicate(), sip_indicate(), sip_write(), and skinny_indicate().

02073 {
02074    if (rtp) {
02075       rtp->set_marker_bit = 1;
02076    }
02077    return;
02078 }

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 1962 of file rtp.c.

References ast_calloc, ast_log(), ast_random(), ast_rtcp_new(), ast_rtp_new_init(), errno, first, free, LOG_DEBUG, LOG_ERROR, option_debug, rtp_socket(), and sched.

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

01963 {
01964    struct ast_rtp *rtp;
01965    int x;
01966    int first;
01967    int startplace;
01968    
01969    if (!(rtp = ast_calloc(1, sizeof(*rtp))))
01970       return NULL;
01971 
01972    ast_rtp_new_init(rtp);
01973 
01974    rtp->s = rtp_socket();
01975    if (option_debug > 2)
01976          ast_log(LOG_DEBUG, "socket RTP fd: %i\n", rtp->s); 
01977    if (rtp->s < 0) {
01978       free(rtp);
01979       ast_log(LOG_ERROR, "Unable to allocate socket: %s\n", strerror(errno));
01980       return NULL;
01981    }
01982    if (sched && rtcpenable) {
01983       rtp->sched = sched;
01984       rtp->rtcp = ast_rtcp_new();
01985       if (option_debug > 2)
01986             ast_log(LOG_DEBUG, "socket RTCP fd: %i\n", rtp->rtcp->s);
01987    }
01988    
01989    /* Select a random port number in the range of possible RTP */
01990    x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
01991    x = x & ~1;
01992    /* Save it for future references. */
01993    startplace = x;
01994    /* Iterate tring to bind that port and incrementing it otherwise untill a port was found or no ports are available. */
01995    for (;;) {
01996       /* Must be an even port number by RTP spec */
01997       rtp->us.sin_port = htons(x);
01998       rtp->us.sin_addr = addr;
01999       /* If there's rtcp, initialize it as well. */
02000       if (rtp->rtcp) {
02001          rtp->rtcp->us.sin_port = htons(x + 1);
02002          rtp->rtcp->us.sin_addr = addr;
02003       }
02004       /* Try to bind it/them. */
02005       if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
02006          (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
02007          break;
02008       if (!first) {
02009          /* Primary bind succeeded! Gotta recreate it */
02010          close(rtp->s);
02011          rtp->s = rtp_socket();
02012          if (option_debug > 2)
02013                ast_log(LOG_DEBUG, "socket RTP2 fd: %i\n", rtp->s); 
02014       }
02015       if (errno != EADDRINUSE) {
02016          /* We got an error that wasn't expected, abort! */
02017          ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
02018          close(rtp->s);
02019          if (rtp->rtcp) {
02020             close(rtp->rtcp->s);
02021             free(rtp->rtcp);
02022          }
02023          free(rtp);
02024          return NULL;
02025       }
02026       /* The port was used, increment it (by two). */
02027       x += 2;
02028       /* Did we go over the limit ? */
02029       if (x > rtpend)
02030          /* then, start from the begingig. */
02031          x = (rtpstart + 1) & ~1;
02032       /* Check if we reached the place were we started. */
02033       if (x == startplace) {
02034          /* If so, there's no ports available. */
02035          ast_log(LOG_ERROR, "No RTP ports remaining. Can't setup media stream for this call.\n");
02036          close(rtp->s);
02037          if (rtp->rtcp) {
02038             close(rtp->rtcp->s);
02039             free(rtp->rtcp);
02040          }
02041          free(rtp);
02042          return NULL;
02043       }
02044    }
02045    rtp->sched = sched;
02046    rtp->io = io;
02047    if (callbackmode) {
02048       rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
02049       ast_set_flag(rtp, FLAG_CALLBACK_MODE);
02050    }
02051    ast_rtp_pt_default(rtp);
02052    return rtp;
02053 }

int ast_rtp_proto_register ( struct ast_rtp_protocol proto  ) 

Register interface to channel driver.

Definition at line 2956 of file rtp.c.

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

Referenced by load_module().

02957 {
02958    struct ast_rtp_protocol *cur;
02959 
02960    AST_LIST_LOCK(&protos);
02961    AST_LIST_TRAVERSE(&protos, cur, list) {   
02962       if (!strcmp(cur->type, proto->type)) {
02963          ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
02964          AST_LIST_UNLOCK(&protos);
02965          return -1;
02966       }
02967    }
02968    AST_LIST_INSERT_HEAD(&protos, proto, list);
02969    AST_LIST_UNLOCK(&protos);
02970    
02971    return 0;
02972 }

void ast_rtp_proto_unregister ( struct ast_rtp_protocol proto  ) 

Unregister interface to channel driver.

Definition at line 2948 of file rtp.c.

References AST_LIST_LOCK, AST_LIST_REMOVE, and AST_LIST_UNLOCK.

Referenced by load_module(), and unload_module().

02949 {
02950    AST_LIST_LOCK(&protos);
02951    AST_LIST_REMOVE(&protos, proto, list);
02952    AST_LIST_UNLOCK(&protos);
02953 }

void ast_rtp_pt_clear ( struct ast_rtp rtp  ) 

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

Definition at line 1465 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 gtalk_alloc(), and process_sdp().

01466 {
01467    int i;
01468 
01469    if (!rtp)
01470       return;
01471 
01472    ast_mutex_lock(&rtp->bridge_lock);
01473 
01474    for (i = 0; i < MAX_RTP_PT; ++i) {
01475       rtp->current_RTP_PT[i].isAstFormat = 0;
01476       rtp->current_RTP_PT[i].code = 0;
01477    }
01478 
01479    rtp->rtp_lookup_code_cache_isAstFormat = 0;
01480    rtp->rtp_lookup_code_cache_code = 0;
01481    rtp->rtp_lookup_code_cache_result = 0;
01482 
01483    ast_mutex_unlock(&rtp->bridge_lock);
01484 }

void ast_rtp_pt_copy ( struct ast_rtp dest,
struct ast_rtp src 
)

Copy payload types between RTP structures.

Definition at line 1505 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().

01506 {
01507    unsigned int i;
01508 
01509    ast_mutex_lock(&dest->bridge_lock);
01510    ast_mutex_lock(&src->bridge_lock);
01511 
01512    for (i=0; i < MAX_RTP_PT; ++i) {
01513       dest->current_RTP_PT[i].isAstFormat = 
01514          src->current_RTP_PT[i].isAstFormat;
01515       dest->current_RTP_PT[i].code = 
01516          src->current_RTP_PT[i].code; 
01517    }
01518    dest->rtp_lookup_code_cache_isAstFormat = 0;
01519    dest->rtp_lookup_code_cache_code = 0;
01520    dest->rtp_lookup_code_cache_result = 0;
01521 
01522    ast_mutex_unlock(&src->bridge_lock);
01523    ast_mutex_unlock(&dest->bridge_lock);
01524 }

void ast_rtp_pt_default ( struct ast_rtp rtp  ) 

Set payload types to defaults.

Definition at line 1486 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.

01487 {
01488    int i;
01489 
01490    ast_mutex_lock(&rtp->bridge_lock);
01491 
01492    /* Initialize to default payload types */
01493    for (i = 0; i < MAX_RTP_PT; ++i) {
01494       rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
01495       rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
01496    }
01497 
01498    rtp->rtp_lookup_code_cache_isAstFormat = 0;
01499    rtp->rtp_lookup_code_cache_code = 0;
01500    rtp->rtp_lookup_code_cache_result = 0;
01501 
01502    ast_mutex_unlock(&rtp->bridge_lock);
01503 }

struct ast_frame* ast_rtp_read ( struct ast_rtp rtp  ) 

Definition at line 1155 of file rtp.c.

References ast_rtp::altthem, ast_assert, ast_codec_get_samples(), AST_FORMAT_MAX_AUDIO, ast_format_rate(), AST_FORMAT_SLINEAR, ast_frame_byteswap_be, AST_FRAME_DTMF_END, AST_FRAME_VIDEO, AST_FRAME_VOICE, AST_FRFLAG_HAS_TIMING_INFO, 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_samp2tv(), ast_sched_add(), ast_set_flag, ast_tv(), ast_tvdiff_ms(), ast_verbose(), bridge_p2p_rtp_write(), ast_rtp::bridged, calc_rxstamp(), rtpPayloadType::code, ast_rtp::cycles, ast_frame::data, ast_frame::datalen, ast_frame::delivery, ast_rtp::dtmf_duration, ast_rtp::dtmf_timeout, errno, ext, ast_rtp::f, f, FLAG_NAT_ACTIVE, ast_frame::frametype, rtpPayloadType::isAstFormat, ast_rtp::lastevent, ast_rtp::lastividtimestamp, ast_rtp::lastrxformat, ast_rtp::lastrxseqno, ast_rtp::lastrxts, ast_frame::len, len(), LOG_DEBUG, 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::resp, ast_rtp::rtcp, rtp_debug_test_addr(), rtp_get_rate(), RTP_SEQ_MOD, ast_rtp::rxcount, ast_rtp::rxseqno, ast_rtp::rxssrc, ast_rtcp::s, ast_rtp::s, ast_frame::samples, ast_rtp::sched, ast_rtcp::schedid, ast_rtp::seedrxseqno, send_dtmf(), 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().

01156 {
01157    int res;
01158    struct sockaddr_in sin;
01159    socklen_t len;
01160    unsigned int seqno;
01161    int version;
01162    int payloadtype;
01163    int hdrlen = 12;
01164    int padding;
01165    int mark;
01166    int ext;
01167    int cc;
01168    unsigned int ssrc;
01169    unsigned int timestamp;
01170    unsigned int *rtpheader;
01171    struct rtpPayloadType rtpPT;
01172    struct ast_rtp *bridged = NULL;
01173    
01174    /* If time is up, kill it */
01175    if (rtp->sending_digit)
01176       ast_rtp_senddigit_continuation(rtp);
01177 
01178    len = sizeof(sin);
01179    
01180    /* Cache where the header will go */
01181    res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
01182                0, (struct sockaddr *)&sin, &len);
01183    if (option_debug > 3)
01184       ast_log(LOG_DEBUG, "socket RTP read: rtp %i rtcp %i\n", rtp->s, rtp->rtcp->s);
01185 
01186    rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
01187    if (res < 0) {
01188       ast_assert(errno != EBADF);
01189       if (errno != EAGAIN) {
01190          ast_log(LOG_WARNING, "RTP Read error: %s.  Hanging up.\n", strerror(errno));
01191          ast_log(LOG_WARNING, "socket RTP read: rtp %i rtcp %i\n", rtp->s, rtp->rtcp->s);
01192          return NULL;
01193       }
01194       return &ast_null_frame;
01195    }
01196    
01197    if (res < hdrlen) {
01198       ast_log(LOG_WARNING, "RTP Read too short\n");
01199       return &ast_null_frame;
01200    }
01201 
01202    /* Get fields */
01203    seqno = ntohl(rtpheader[0]);
01204 
01205    /* Check RTP version */
01206    version = (seqno & 0xC0000000) >> 30;
01207    if (!version) {
01208       if ((stun_handle_packet(rtp->s, &sin, rtp->rawdata + AST_FRIENDLY_OFFSET, res) == STUN_ACCEPT) &&
01209          (!rtp->them.sin_port && !rtp->them.sin_addr.s_addr)) {
01210          memcpy(&rtp->them, &sin, sizeof(rtp->them));
01211       }
01212       return &ast_null_frame;
01213    }
01214 
01215 #if 0 /* Allow to receive RTP stream with closed transmission path */
01216    /* If we don't have the other side's address, then ignore this */
01217    if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
01218       return &ast_null_frame;
01219 #endif
01220 
01221    /* Send to whoever send to us if NAT is turned on */
01222    if (rtp->nat) {
01223       if (((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
01224           (rtp->them.sin_port != sin.sin_port)) &&
01225           ((rtp->altthem.sin_addr.s_addr != sin.sin_addr.s_addr) ||
01226           (rtp->altthem.sin_port != sin.sin_port))) {
01227          rtp->them = sin;
01228          if (rtp->rtcp) {
01229             memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
01230             rtp->rtcp->them.sin_port = htons(ntohs(rtp->them.sin_port)+1);
01231          }
01232          rtp->rxseqno = 0;
01233          ast_set_flag(rtp, FLAG_NAT_ACTIVE);
01234          if (option_debug || rtpdebug)
01235             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));
01236       }
01237    }
01238 
01239    /* If we are bridged to another RTP stream, send direct */
01240    if ((bridged = ast_rtp_get_bridged(rtp)) && !bridge_p2p_rtp_write(rtp, bridged, rtpheader, res, hdrlen))
01241       return &ast_null_frame;
01242 
01243    if (version != 2)
01244       return &ast_null_frame;
01245 
01246    payloadtype = (seqno & 0x7f0000) >> 16;
01247    padding = seqno & (1 << 29);
01248    mark = seqno & (1 << 23);
01249    ext = seqno & (1 << 28);
01250    cc = (seqno & 0xF000000) >> 24;
01251    seqno &= 0xffff;
01252    timestamp = ntohl(rtpheader[1]);
01253    ssrc = ntohl(rtpheader[2]);
01254    
01255    if (!mark && rtp->rxssrc && rtp->rxssrc != ssrc) {
01256       if (option_debug || rtpdebug)
01257          ast_log(LOG_DEBUG, "Forcing Marker bit, because SSRC has changed\n");
01258       mark = 1;
01259    }
01260 
01261    rtp->rxssrc = ssrc;
01262    
01263    if (padding) {
01264       /* Remove padding bytes */
01265       res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
01266    }
01267    
01268    if (cc) {
01269       /* CSRC fields present */
01270       hdrlen += cc*4;
01271    }
01272 
01273    if (ext) {
01274       /* RTP Extension present */
01275       hdrlen += (ntohl(rtpheader[hdrlen/4]) & 0xffff) << 2;
01276       hdrlen += 4;
01277    }
01278 
01279    if (res < hdrlen) {
01280       ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
01281       return &ast_null_frame;
01282    }
01283 
01284    rtp->rxcount++; /* Only count reasonably valid packets, this'll make the rtcp stats more accurate */
01285 
01286    if (rtp->rxcount==1) {
01287       /* This is the first RTP packet successfully received from source */
01288       rtp->seedrxseqno = seqno;
01289    }
01290 
01291    /* Do not schedule RR if RTCP isn't run */
01292    if (rtp->rtcp && rtp->rtcp->them.sin_addr.s_addr && rtp->rtcp->schedid < 1) {
01293       /* Schedule transmission of Receiver Report */
01294       rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
01295    }
01296    if ( (int)rtp->lastrxseqno - (int)seqno  > 100) /* if so it would indicate that the sender cycled; allow for misordering */
01297       rtp->cycles += RTP_SEQ_MOD;
01298 
01299    rtp->lastrxseqno = seqno;
01300    
01301    if (rtp->themssrc==0)
01302       rtp->themssrc = ntohl(rtpheader[2]); /* Record their SSRC to put in future RR */
01303    
01304    if (rtp_debug_test_addr(&sin))
01305       ast_verbose("Got  RTP packet from    %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
01306          ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
01307 
01308    rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
01309    if (!rtpPT.isAstFormat) {
01310       struct ast_frame *f = NULL;
01311 
01312       /* This is special in-band data that's not one of our codecs */
01313       if (rtpPT.code == AST_RTP_DTMF) {
01314          /* It's special -- rfc2833 process it */
01315          if (rtp_debug_test_addr(&sin)) {
01316             unsigned char *data;
01317             unsigned int event;
01318             unsigned int event_end;
01319             unsigned int duration;
01320             data = rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen;
01321             event = ntohl(*((unsigned int *)(data)));
01322             event >>= 24;
01323             event_end = ntohl(*((unsigned int *)(data)));
01324             event_end <<= 8;
01325             event_end >>= 24;
01326             duration = ntohl(*((unsigned int *)(data)));
01327             duration &= 0xFFFF;
01328             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);
01329          }
01330          f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp);
01331       } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
01332          /* It's really special -- process it the Cisco way */
01333          if (rtp->lastevent <= seqno || (rtp->lastevent >= 65530 && seqno <= 6)) {
01334             f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
01335             rtp->lastevent = seqno;
01336          }
01337       } else if (rtpPT.code == AST_RTP_CN) {
01338          /* Comfort Noise */
01339          f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
01340       } else {
01341          ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n", payloadtype, ast_inet_ntoa(rtp->them.sin_addr));
01342       }
01343       return f ? f : &ast_null_frame;
01344    }
01345    rtp->lastrxformat = rtp->f.subclass = rtpPT.code;
01346    rtp->f.frametype = (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) ? AST_FRAME_VOICE : AST_FRAME_VIDEO;
01347 
01348    rtp->rxseqno = seqno;
01349 
01350    if (rtp->dtmf_timeout && rtp->dtmf_timeout < timestamp) {
01351       rtp->dtmf_timeout = 0;
01352 
01353       if (rtp->resp) {
01354          struct ast_frame *f;
01355          f = send_dtmf(rtp, AST_FRAME_DTMF_END);
01356          f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass)), ast_tv(0, 0));
01357          rtp->resp = 0;
01358          rtp->dtmf_timeout = rtp->dtmf_duration = 0;
01359          return f;
01360       }
01361    }
01362 
01363    /* Record received timestamp as last received now */
01364    rtp->lastrxts = timestamp;
01365 
01366    rtp->f.mallocd = 0;
01367    rtp->f.datalen = res - hdrlen;
01368    rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
01369    rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
01370    rtp->f.seqno = seqno;
01371    if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
01372       rtp->f.samples = ast_codec_get_samples(&rtp->f);
01373       if (rtp->f.subclass == AST_FORMAT_SLINEAR) 
01374          ast_frame_byteswap_be(&rtp->f);
01375       calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
01376       /* Add timing data to let ast_generic_bridge() put the frame into a jitterbuf */
01377       ast_set_flag(&rtp->f, AST_FRFLAG_HAS_TIMING_INFO);
01378       rtp->f.ts = timestamp / (rtp_get_rate(rtp->f.subclass) / 1000);
01379       rtp->f.len = rtp->f.samples / (ast_format_rate(rtp->f.subclass) / 1000);
01380    } else {
01381       /* Video -- samples is # of samples vs. 90000 */
01382       if (!rtp->lastividtimestamp)
01383          rtp->lastividtimestamp = timestamp;
01384       rtp->f.samples = timestamp - rtp->lastividtimestamp;
01385       rtp->lastividtimestamp = timestamp;
01386       rtp->f.delivery.tv_sec = 0;
01387       rtp->f.delivery.tv_usec = 0;
01388       if (mark)
01389          rtp->f.subclass |= 0x1;
01390    }
01391    rtp->f.src = "RTP";
01392    return &rtp->f;
01393 }

int ast_rtp_reload ( void   ) 

Definition at line 3875 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().

03876 {
03877    struct ast_config *cfg;
03878    const char *s;
03879 
03880    rtpstart = 5000;
03881    rtpend = 31000;
03882    dtmftimeout = DEFAULT_DTMF_TIMEOUT;
03883    cfg = ast_config_load("rtp.conf");
03884    if (cfg) {
03885       if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
03886          rtpstart = atoi(s);
03887          if (rtpstart < 1024)
03888             rtpstart = 1024;
03889          if (rtpstart > 65535)
03890             rtpstart = 65535;
03891       }
03892       if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
03893          rtpend = atoi(s);
03894          if (rtpend < 1024)
03895             rtpend = 1024;
03896          if (rtpend > 65535)
03897             rtpend = 65535;
03898       }
03899       if ((s = ast_variable_retrieve(cfg, "general", "rtcpinterval"))) {
03900          rtcpinterval = atoi(s);
03901          if (rtcpinterval == 0)
03902             rtcpinterval = 0; /* Just so we're clear... it's zero */
03903          if (rtcpinterval < RTCP_MIN_INTERVALMS)
03904             rtcpinterval = RTCP_MIN_INTERVALMS; /* This catches negative numbers too */
03905          if (rtcpinterval > RTCP_MAX_INTERVALMS)
03906             rtcpinterval = RTCP_MAX_INTERVALMS;
03907       }
03908       if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
03909 #ifdef SO_NO_CHECK
03910          if (ast_false(s))
03911             nochecksums = 1;
03912          else
03913             nochecksums = 0;
03914 #else
03915          if (ast_false(s))
03916             ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
03917 #endif
03918       }
03919       if ((s = ast_variable_retrieve(cfg, "general", "dtmftimeout"))) {
03920          dtmftimeout = atoi(s);
03921          if ((dtmftimeout < 0) || (dtmftimeout > 64000)) {
03922             ast_log(LOG_WARNING, "DTMF timeout of '%d' outside range, using default of '%d' instead\n",
03923                dtmftimeout, DEFAULT_DTMF_TIMEOUT);
03924             dtmftimeout = DEFAULT_DTMF_TIMEOUT;
03925          };
03926       }
03927       ast_config_destroy(cfg);
03928    }
03929    if (rtpstart >= rtpend) {
03930       ast_log(LOG_WARNING, "Unreasonable values for RTP start/end port in rtp.conf\n");
03931       rtpstart = 5000;
03932       rtpend = 31000;
03933    }
03934    if (option_verbose > 1)
03935       ast_verbose(VERBOSE_PREFIX_2 "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
03936    return 0;
03937 }

void ast_rtp_reset ( struct ast_rtp rtp  ) 

Definition at line 2146 of file rtp.c.

References ast_rtp::dtmf_timeout, ast_rtp::dtmfmute, 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.

02147 {
02148    memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
02149    memset(&rtp->txcore, 0, sizeof(rtp->txcore));
02150    memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
02151    rtp->lastts = 0;
02152    rtp->lastdigitts = 0;
02153    rtp->lastrxts = 0;
02154    rtp->lastividtimestamp = 0;
02155    rtp->lastovidtimestamp = 0;
02156    rtp->lasteventseqn = 0;
02157    rtp->lastevent = 0;
02158    rtp->lasttxformat = 0;
02159    rtp->lastrxformat = 0;
02160    rtp->dtmf_timeout = 0;
02161    rtp->seqno = 0;
02162    rtp->rxseqno = 0;
02163 }

int ast_rtp_sendcng ( struct ast_rtp rtp,
int  level 
)

generate comfort noice (CNG)

Definition at line 2660 of file rtp.c.

References ast_inet_ntoa(), ast_log(), AST_RTP_CN, ast_rtp_lookup_code(), ast_tv(), ast_tvadd(), ast_tvnow(), ast_verbose(), ast_rtp::data, ast_rtp::dtmfmute, errno, 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().

02661 {
02662    unsigned int *rtpheader;
02663    int hdrlen = 12;
02664    int res;
02665    int payload;
02666    char data[256];
02667    level = 127 - (level & 0x7f);
02668    payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_CN);
02669 
02670    /* If we have no peer, return immediately */ 
02671    if (!rtp->them.sin_addr.s_addr)
02672       return 0;
02673 
02674    rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
02675 
02676    /* Get a pointer to the header */
02677    rtpheader = (unsigned int *)data;
02678    rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
02679    rtpheader[1] = htonl(rtp->lastts);
02680    rtpheader[2] = htonl(rtp->ssrc); 
02681    data[12] = level;
02682    if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
02683       res = sendto(rtp->s, (void *)rtpheader, hdrlen + 1, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
02684       if (res <0) 
02685          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));
02686       if (rtp_debug_test_addr(&rtp->them))
02687          ast_verbose("Sent Comfort Noise RTP packet to %s:%u (type %d, seq %u, ts %u, len %d)\n"
02688                , ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);         
02689          
02690    }
02691    return 0;
02692 }

int ast_rtp_senddigit_begin ( struct ast_rtp rtp,
char  digit 
)

Send begin frames for DTMF.

Definition at line 2268 of file rtp.c.

References ast_inet_ntoa(), ast_log(), AST_RTP_DTMF, ast_rtp_lookup_code(), ast_tv(), ast_tvadd(), ast_tvnow(), ast_verbose(), ast_rtp::dtmfmute, errno, ast_rtp::lastdigitts, ast_rtp::lastts, 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 mgcp_senddigit_begin(), oh323_digit_begin(), and sip_senddigit_begin().

02269 {
02270    unsigned int *rtpheader;
02271    int hdrlen = 12, res = 0, i = 0, payload = 0;
02272    char data[256];
02273 
02274    if ((digit <= '9') && (digit >= '0'))
02275       digit -= '0';
02276    else if (digit == '*')
02277       digit = 10;
02278    else if (digit == '#')
02279       digit = 11;
02280    else if ((digit >= 'A') && (digit <= 'D'))
02281       digit = digit - 'A' + 12;
02282    else if ((digit >= 'a') && (digit <= 'd'))
02283       digit = digit - 'a' + 12;
02284    else {
02285       ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
02286       return 0;
02287    }
02288 
02289    /* If we have no peer, return immediately */ 
02290    if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
02291       return 0;
02292 
02293    payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
02294 
02295    rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
02296    rtp->send_duration = 160;
02297    rtp->lastdigitts = rtp->lastts + rtp->send_duration;
02298    
02299    /* Get a pointer to the header */
02300    rtpheader = (unsigned int *)data;
02301    rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
02302    rtpheader[1] = htonl(rtp->lastdigitts);
02303    rtpheader[2] = htonl(rtp->ssrc); 
02304 
02305    for (i = 0; i < 2; i++) {
02306       rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
02307       res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
02308       if (res < 0) 
02309          ast_log(LOG_ERROR, "RTP Transmission error to %s:%u: %s\n",
02310             ast_inet_ntoa(rtp->them.sin_addr),
02311             ntohs(rtp->them.sin_port), strerror(errno));
02312       if (rtp_debug_test_addr(&rtp->them))
02313          ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
02314                 ast_inet_ntoa(rtp->them.sin_addr),
02315                 ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
02316       /* Increment sequence number */
02317       rtp->seqno++;
02318       /* Increment duration */
02319       rtp->send_duration += 160;
02320       /* Clear marker bit and set seqno */
02321       rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
02322    }
02323 
02324    /* Since we received a begin, we can safely store the digit and disable any compensation */
02325    rtp->sending_digit = 1;
02326    rtp->send_digit = digit;
02327    rtp->send_payload = payload;
02328 
02329    return 0;
02330 }

int ast_rtp_senddigit_end ( struct ast_rtp rtp,
char  digit 
)

void ast_rtp_set_alt_peer ( struct ast_rtp rtp,
struct sockaddr_in *  alt 
)

set potential alternate source for RTP media

Since:
1.4.26
This function may be used to give the RTP stack a hint that there is a potential second source of media. One case where this is used is when the SIP stack receives a REINVITE to which it will be replying with a 491. In such a scenario, the IP and port information in the SDP of that REINVITE lets us know that we may receive media from that source/those sources even though the SIP transaction was unable to be completed successfully

Parameters:
rtp The RTP structure we wish to set up an alternate host/port on
alt The address information for the alternate media source
Return values:
void 

Definition at line 2091 of file rtp.c.

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

Referenced by handle_request_invite().

02092 {
02093    rtp->altthem.sin_port = alt->sin_port;
02094    rtp->altthem.sin_addr = alt->sin_addr;
02095    if (rtp->rtcp) {
02096       rtp->rtcp->altthem.sin_port = htons(ntohs(alt->sin_port) + 1);
02097       rtp->rtcp->altthem.sin_addr = alt->sin_addr;
02098    }
02099 }

void ast_rtp_set_callback ( struct ast_rtp rtp,
ast_rtp_callback  callback 
)

Definition at line 593 of file rtp.c.

References ast_rtp::callback.

Referenced by start_rtp().

00594 {
00595    rtp->callback = callback;
00596 }

void ast_rtp_set_data ( struct ast_rtp rtp,
void *  data 
)

Definition at line 588 of file rtp.c.

References ast_rtp::data.

Referenced by start_rtp().

00589 {
00590    rtp->data = data;
00591 }

void ast_rtp_set_m_type ( struct ast_rtp rtp,
int  pt 
)

Activate payload type.

Definition at line 1703 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_is_answered(), gtalk_newcall(), and process_sdp().

01704 {
01705    if (pt < 0 || pt > MAX_RTP_PT || static_RTP_PT[pt].code == 0) 
01706       return; /* bogus payload type */
01707 
01708    ast_mutex_lock(&rtp->bridge_lock);
01709    rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
01710    ast_mutex_unlock(&rtp->bridge_lock);
01711 } 

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

Definition at line 2080 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().

02081 {
02082    rtp->them.sin_port = them->sin_port;
02083    rtp->them.sin_addr = them->sin_addr;
02084    if (rtp->rtcp) {
02085       rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
02086       rtp->rtcp->them.sin_addr = them->sin_addr;
02087    }
02088    rtp->rxseqno = 0;
02089 }

void ast_rtp_set_rtpholdtimeout ( struct ast_rtp rtp,
int  timeout 
)

Set rtp hold timeout.

Definition at line 555 of file rtp.c.

References ast_rtp::rtpholdtimeout.

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

00556 {
00557    rtp->rtpholdtimeout = timeout;
00558 }

void ast_rtp_set_rtpkeepalive ( struct ast_rtp rtp,
int  period 
)

set RTP keepalive interval

Definition at line 561 of file rtp.c.

References ast_rtp::rtpkeepalive.

Referenced by create_addr_from_peer(), and sip_alloc().

00562 {
00563    rtp->rtpkeepalive = period;
00564 }

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

Initiate payload type to a known MIME media type for a codec.

Returns:
0 if the MIME type was found and set, -1 if it wasn't found

Definition at line 1730 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_is_answered(), gtalk_newcall(), process_sdp(), and set_dtmf_payload().

01733 {
01734    unsigned int i;
01735    int found = 0;
01736 
01737    if (pt < 0 || pt > MAX_RTP_PT) 
01738       return -1; /* bogus payload type */
01739    
01740    ast_mutex_lock(&rtp->bridge_lock);
01741 
01742    for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
01743       if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
01744           strcasecmp(mimeType, mimeTypes[i].type) == 0) {
01745          found = 1;
01746          rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
01747          if ((mimeTypes[i].payloadType.code == AST_FORMAT_G726) &&
01748              mimeTypes[i].payloadType.isAstFormat &&
01749              (options & AST_RTP_OPT_G726_NONSTANDARD))
01750             rtp->current_RTP_PT[pt].code = AST_FORMAT_G726_AAL2;
01751          break;
01752       }
01753    }
01754 
01755    ast_mutex_unlock(&rtp->bridge_lock);
01756 
01757    return (found ? 0 : -1);
01758 } 

void ast_rtp_set_rtptimeout ( struct ast_rtp rtp,
int  timeout 
)

Set rtp timeout.

Definition at line 549 of file rtp.c.

References ast_rtp::rtptimeout.

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

00550 {
00551    rtp->rtptimeout = timeout;
00552 }

void ast_rtp_set_rtptimers_onhold ( struct ast_rtp rtp  ) 

Definition at line 542 of file rtp.c.

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

Referenced by handle_response_invite().

00543 {
00544    rtp->rtptimeout = (-1) * rtp->rtptimeout;
00545    rtp->rtpholdtimeout = (-1) * rtp->rtpholdtimeout;
00546 }

void ast_rtp_setdtmf ( struct ast_rtp rtp,
int  dtmf 
)

Indicate whether this RTP session is carrying DTMF or not.

Definition at line 608 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().

00609 {
00610    ast_set2_flag(rtp, dtmf ? 1 : 0, FLAG_HAS_DTMF);
00611 }

void ast_rtp_setdtmfcompensate ( struct ast_rtp rtp,
int  compensate 
)

Compensate for devices that send RFC2833 packets all at once.

Definition at line 613 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().

00614 {
00615    ast_set2_flag(rtp, compensate ? 1 : 0, FLAG_DTMF_COMPENSATE);
00616 }

void ast_rtp_setnat ( struct ast_rtp rtp,
int  nat 
)

Definition at line 598 of file rtp.c.

References ast_rtp::nat.

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

00599 {
00600    rtp->nat = nat;
00601 }

void ast_rtp_setstun ( struct ast_rtp rtp,
int  stun_enable 
)

Enable STUN capability.

Definition at line 618 of file rtp.c.

References ast_set2_flag, and FLAG_HAS_STUN.

Referenced by gtalk_new().

00619 {
00620    ast_set2_flag(rtp, stun_enable ? 1 : 0, FLAG_HAS_STUN);
00621 }

int ast_rtp_settos ( struct ast_rtp rtp,
int  tos 
)

Definition at line 2063 of file rtp.c.

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

Referenced by __oh323_rtp_create(), and sip_alloc().

02064 {
02065    int res;
02066 
02067    if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) 
02068       ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
02069    return res;
02070 }

void ast_rtp_stop ( struct ast_rtp rtp  ) 

Definition at line 2130 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().

02131 {
02132    if (rtp->rtcp) {
02133       AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
02134    }
02135 
02136    memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
02137    memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
02138    if (rtp->rtcp) {
02139       memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->rtcp->them.sin_addr));
02140       memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->rtcp->them.sin_port));
02141    }
02142    
02143    ast_clear_flag(rtp, FLAG_P2P_SENT_MARK);
02144 }

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

Definition at line 405 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().

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

void ast_rtp_unset_m_type ( struct ast_rtp rtp,
int  pt 
)

clear payload type

Definition at line 1715 of file rtp.c.

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

Referenced by process_sdp().

01716 {
01717    if (pt < 0 || pt > MAX_RTP_PT)
01718       return; /* bogus payload type */
01719 
01720    ast_mutex_lock(&rtp->bridge_lock);
01721    rtp->current_RTP_PT[pt].isAstFormat = 0;
01722    rtp->current_RTP_PT[pt].code = 0;
01723    ast_mutex_unlock(&rtp->bridge_lock);
01724 }

int ast_rtp_write ( struct ast_rtp rtp,
struct ast_frame f 
)

Definition at line 2865 of file rtp.c.

References ast_codec_pref_getsize(), AST_FORMAT_G723_1, 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_format_list::cur_ms, ast_frame::datalen, f, ast_format_list::flags, ast_format_list::fr_len, ast_frame::frametype, ast_format_list::inc_ms, 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().

02866 {
02867    struct ast_frame *f;
02868    int codec;
02869    int hdrlen = 12;
02870    int subclass;
02871    
02872 
02873    /* If we have no peer, return immediately */ 
02874    if (!rtp->them.sin_addr.s_addr)
02875       return 0;
02876 
02877    /* If there is no data length, return immediately */
02878    if (!_f->datalen) 
02879       return 0;
02880    
02881    /* Make sure we have enough space for RTP header */
02882    if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
02883       ast_log(LOG_WARNING, "RTP can only send voice and video\n");
02884       return -1;
02885    }
02886 
02887    subclass = _f->subclass;
02888    if (_f->frametype == AST_FRAME_VIDEO)
02889       subclass &= ~0x1;
02890 
02891    codec = ast_rtp_lookup_code(rtp, 1, subclass);
02892    if (codec < 0) {
02893       ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
02894       return -1;
02895    }
02896 
02897    if (rtp->lasttxformat != subclass) {
02898       /* New format, reset the smoother */
02899       if (option_debug)
02900          ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
02901       rtp->lasttxformat = subclass;
02902       if (rtp->smoother)
02903          ast_smoother_free(rtp->smoother);
02904       rtp->smoother = NULL;
02905    }
02906 
02907    if (!rtp->smoother && subclass != AST_FORMAT_SPEEX && subclass != AST_FORMAT_G723_1) {
02908       struct ast_format_list fmt = ast_codec_pref_getsize(&rtp->pref, subclass);
02909       if (fmt.inc_ms) { /* if codec parameters is set / avoid division by zero */
02910          if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
02911             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));
02912             return -1;
02913          }
02914          if (fmt.flags)
02915             ast_smoother_set_flags(rtp->smoother, fmt.flags);
02916          if (option_debug)
02917             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));
02918       }
02919    }
02920    if (rtp->smoother) {
02921       if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
02922          ast_smoother_feed_be(rtp->smoother, _f);
02923       } else {
02924          ast_smoother_feed(rtp->smoother, _f);
02925       }
02926 
02927       while ((f = ast_smoother_read(rtp->smoother)) && (f->data)) {
02928          ast_rtp_raw_write(rtp, f, codec);
02929       }
02930    } else {
02931       /* Don't buffer outgoing frames; send them one-per-packet: */
02932       if (_f->offset < hdrlen) {
02933          f = ast_frdup(_f);
02934       } else {
02935          f = _f;
02936       }
02937       if (f->data) {
02938          ast_rtp_raw_write(rtp, f, codec);
02939       }
02940       if (f != _f)
02941          ast_frfree(f);
02942    }
02943       
02944    return 0;
02945 }


Generated on Thu Oct 8 00:59:51 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7