Thu Jan 28 17:36:13 2010

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_constantssrc (struct ast_rtp *rtp)
 When changing sources, don't generate a new SSRC.
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_a_audio().


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

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

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

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

02439 {
02440    struct ast_rtp *rtp = data;
02441    int res;
02442 
02443    rtp->rtcp->sendfur = 1;
02444    res = ast_rtcp_write(data);
02445    
02446    return res;
02447 }

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

Referenced by process_sdp().

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

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 3414 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.

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

int ast_rtp_codec_getformat ( int  pt  ) 

Definition at line 2866 of file rtp.c.

References rtpPayloadType::code, and static_RTP_PT.

Referenced by process_sdp_a_audio().

02867 {
02868    if (pt < 0 || pt >= MAX_RTP_PT)
02869       return 0; /* bogus payload type */
02870 
02871    if (static_RTP_PT[pt].isAstFormat)
02872       return static_RTP_PT[pt].code;
02873    else
02874       return 0;
02875 }

struct ast_codec_pref* ast_rtp_codec_getpref ( struct ast_rtp rtp  ) 

Definition at line 2861 of file rtp.c.

References ast_rtp::pref.

Referenced by add_codec_to_sdp(), and process_sdp_a_audio().

02862 {
02863    return &rtp->pref;
02864 }

int ast_rtp_codec_setpref ( struct ast_rtp rtp,
struct ast_codec_pref prefs 
)

Definition at line 2814 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_a_audio(), register_verify(), set_peer_capabilities(), sip_alloc(), start_rtp(), and transmit_response_with_sdp().

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

void ast_rtp_destroy ( struct ast_rtp rtp  ) 

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

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

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

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

int ast_rtp_fd ( struct ast_rtp rtp  ) 

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

00517 {
00518    return rtp->s;
00519 }

struct ast_rtp* ast_rtp_get_bridged ( struct ast_rtp rtp  ) 

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

02132 {
02133    struct ast_rtp *bridged = NULL;
02134 
02135    ast_mutex_lock(&rtp->bridge_lock);
02136    bridged = rtp->bridged;
02137    ast_mutex_unlock(&rtp->bridge_lock);
02138 
02139    return bridged;
02140 }

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

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

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

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

02114 {
02115    if ((them->sin_family != AF_INET) ||
02116       (them->sin_port != rtp->them.sin_port) ||
02117       (them->sin_addr.s_addr != rtp->them.sin_addr.s_addr)) {
02118       them->sin_family = AF_INET;
02119       them->sin_port = rtp->them.sin_port;
02120       them->sin_addr = rtp->them.sin_addr;
02121       return 1;
02122    }
02123    return 0;
02124 }

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

Return RTCP quality string.

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

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

int ast_rtp_get_rtpholdtimeout ( struct ast_rtp rtp  ) 

Get rtp hold timeout.

Definition at line 576 of file rtp.c.

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

Referenced by do_monitor().

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

int ast_rtp_get_rtpkeepalive ( struct ast_rtp rtp  ) 

Get RTP keepalive interval.

Definition at line 584 of file rtp.c.

References ast_rtp::rtpkeepalive.

Referenced by do_monitor().

00585 {
00586    return rtp->rtpkeepalive;
00587 }

int ast_rtp_get_rtptimeout ( struct ast_rtp rtp  ) 

Get rtp timeout.

Definition at line 568 of file rtp.c.

References ast_rtp::rtptimeout.

Referenced by do_monitor().

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

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

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

02127 {
02128    *us = rtp->us;
02129 }

int ast_rtp_getnat ( struct ast_rtp rtp  ) 

Definition at line 604 of file rtp.c.

References ast_test_flag, and FLAG_NAT_ACTIVE.

Referenced by sip_get_rtp_peer().

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

void ast_rtp_init ( void   ) 

Initialize the RTP system in Asterisk.

Definition at line 3952 of file rtp.c.

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

Referenced by main().

03953 {
03954    ast_cli_register_multiple(cli_rtp, sizeof(cli_rtp) / sizeof(struct ast_cli_entry));
03955    ast_rtp_reload();
03956 }

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

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

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

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

Referenced by process_sdp().

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

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

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

struct rtpPayloadType ast_rtp_lookup_pt ( struct ast_rtp rtp,
int  pt 
)

Mapping between RTP payload format codes and Asterisk codes:.

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

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

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

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

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

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

References ast_rtp_new_with_bindaddr(), io, and sched.

02061 {
02062    struct in_addr ia;
02063 
02064    memset(&ia, 0, sizeof(ia));
02065    return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
02066 }

void ast_rtp_new_init ( struct ast_rtp rtp  ) 

Initialize a new RTP structure.

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

01955 {
01956    ast_mutex_init(&rtp->bridge_lock);
01957 
01958    rtp->them.sin_family = AF_INET;
01959    rtp->us.sin_family = AF_INET;
01960    rtp->ssrc = ast_random();
01961    rtp->seqno = ast_random() & 0xffff;
01962    ast_set_flag(rtp, FLAG_HAS_DTMF);
01963 
01964    return;
01965 }

void ast_rtp_new_source ( struct ast_rtp rtp  ) 

Definition at line 2082 of file rtp.c.

References ast_random(), ast_rtp::constantssrc, ast_rtp::set_marker_bit, and ast_rtp::ssrc.

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

02083 {
02084    if (rtp) {
02085       rtp->set_marker_bit = 1;
02086       if (!rtp->constantssrc) {
02087          rtp->ssrc = ast_random();
02088       }
02089    }
02090 }

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

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

int ast_rtp_proto_register ( struct ast_rtp_protocol proto  ) 

Register interface to channel driver.

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

02969 {
02970    struct ast_rtp_protocol *cur;
02971 
02972    AST_LIST_LOCK(&protos);
02973    AST_LIST_TRAVERSE(&protos, cur, list) {   
02974       if (!strcmp(cur->type, proto->type)) {
02975          ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
02976          AST_LIST_UNLOCK(&protos);
02977          return -1;
02978       }
02979    }
02980    AST_LIST_INSERT_HEAD(&protos, proto, list);
02981    AST_LIST_UNLOCK(&protos);
02982    
02983    return 0;
02984 }

void ast_rtp_proto_unregister ( struct ast_rtp_protocol proto  ) 

Unregister interface to channel driver.

Definition at line 2960 of file rtp.c.

References AST_LIST_LOCK, AST_LIST_REMOVE, and AST_LIST_UNLOCK.

Referenced by load_module(), and unload_module().

02961 {
02962    AST_LIST_LOCK(&protos);
02963    AST_LIST_REMOVE(&protos, proto, list);
02964    AST_LIST_UNLOCK(&protos);
02965 }

void ast_rtp_pt_clear ( struct ast_rtp rtp  ) 

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

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

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

void ast_rtp_pt_copy ( struct ast_rtp dest,
struct ast_rtp src 
)

Copy payload types between RTP structures.

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

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

void ast_rtp_pt_default ( struct ast_rtp rtp  ) 

Set payload types to defaults.

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

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

struct ast_frame* ast_rtp_read ( struct ast_rtp rtp  ) 

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

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

int ast_rtp_reload ( void   ) 

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

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

void ast_rtp_reset ( struct ast_rtp rtp  ) 

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

02159 {
02160    memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
02161    memset(&rtp->txcore, 0, sizeof(rtp->txcore));
02162    memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
02163    rtp->lastts = 0;
02164    rtp->lastdigitts = 0;
02165    rtp->lastrxts = 0;
02166    rtp->lastividtimestamp = 0;
02167    rtp->lastovidtimestamp = 0;
02168    rtp->lasteventseqn = 0;
02169    rtp->lastevent = 0;
02170    rtp->lasttxformat = 0;
02171    rtp->lastrxformat = 0;
02172    rtp->dtmf_timeout = 0;
02173    rtp->seqno = 0;
02174    rtp->rxseqno = 0;
02175 }

int ast_rtp_sendcng ( struct ast_rtp rtp,
int  level 
)

generate comfort noice (CNG)

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

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

int ast_rtp_senddigit_begin ( struct ast_rtp rtp,
char  digit 
)

Send begin frames for DTMF.

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

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

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

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

Referenced by handle_request_invite().

02104 {
02105    rtp->altthem.sin_port = alt->sin_port;
02106    rtp->altthem.sin_addr = alt->sin_addr;
02107    if (rtp->rtcp) {
02108       rtp->rtcp->altthem.sin_port = htons(ntohs(alt->sin_port) + 1);
02109       rtp->rtcp->altthem.sin_addr = alt->sin_addr;
02110    }
02111 }

void ast_rtp_set_callback ( struct ast_rtp rtp,
ast_rtp_callback  callback 
)

Definition at line 594 of file rtp.c.

References ast_rtp::callback.

Referenced by start_rtp().

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

void ast_rtp_set_constantssrc ( struct ast_rtp rtp  ) 

When changing sources, don't generate a new SSRC.

Definition at line 2077 of file rtp.c.

References ast_rtp::constantssrc.

Referenced by create_addr_from_peer(), and handle_request_invite().

02078 {
02079    rtp->constantssrc = 1;
02080 }

void ast_rtp_set_data ( struct ast_rtp rtp,
void *  data 
)

Definition at line 589 of file rtp.c.

References ast_rtp::data.

Referenced by start_rtp().

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

void ast_rtp_set_m_type ( struct ast_rtp rtp,
int  pt 
)

Activate payload type.

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

01709 {
01710    if (pt < 0 || pt >= MAX_RTP_PT || static_RTP_PT[pt].code == 0) 
01711       return; /* bogus payload type */
01712 
01713    ast_mutex_lock(&rtp->bridge_lock);
01714    rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
01715    ast_mutex_unlock(&rtp->bridge_lock);
01716 } 

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

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

02093 {
02094    rtp->them.sin_port = them->sin_port;
02095    rtp->them.sin_addr = them->sin_addr;
02096    if (rtp->rtcp) {
02097       rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
02098       rtp->rtcp->them.sin_addr = them->sin_addr;
02099    }
02100    rtp->rxseqno = 0;
02101 }

void ast_rtp_set_rtpholdtimeout ( struct ast_rtp rtp,
int  timeout 
)

Set rtp hold timeout.

Definition at line 556 of file rtp.c.

References ast_rtp::rtpholdtimeout.

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

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

void ast_rtp_set_rtpkeepalive ( struct ast_rtp rtp,
int  period 
)

set RTP keepalive interval

Definition at line 562 of file rtp.c.

References ast_rtp::rtpkeepalive.

Referenced by create_addr_from_peer(), and sip_alloc().

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

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 1735 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(), process_sdp_a_audio(), process_sdp_a_video(), and set_dtmf_payload().

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

void ast_rtp_set_rtptimeout ( struct ast_rtp rtp,
int  timeout 
)

Set rtp timeout.

Definition at line 550 of file rtp.c.

References ast_rtp::rtptimeout.

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

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

void ast_rtp_set_rtptimers_onhold ( struct ast_rtp rtp  ) 

Definition at line 543 of file rtp.c.

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

Referenced by handle_response_invite().

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

void ast_rtp_setdtmf ( struct ast_rtp rtp,
int  dtmf 
)

Indicate whether this RTP session is carrying DTMF or not.

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

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

void ast_rtp_setdtmfcompensate ( struct ast_rtp rtp,
int  compensate 
)

Compensate for devices that send RFC2833 packets all at once.

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

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

void ast_rtp_setnat ( struct ast_rtp rtp,
int  nat 
)

Definition at line 599 of file rtp.c.

References ast_rtp::nat.

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

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

void ast_rtp_setstun ( struct ast_rtp rtp,
int  stun_enable 
)

Enable STUN capability.

Definition at line 619 of file rtp.c.

References ast_set2_flag, and FLAG_HAS_STUN.

Referenced by gtalk_new().

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

int ast_rtp_settos ( struct ast_rtp rtp,
int  tos 
)

Definition at line 2068 of file rtp.c.

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

Referenced by __oh323_rtp_create(), and sip_alloc().

02069 {
02070    int res;
02071 
02072    if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) 
02073       ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
02074    return res;
02075 }

void ast_rtp_stop ( struct ast_rtp rtp  ) 

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

02143 {
02144    if (rtp->rtcp) {
02145       AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
02146    }
02147 
02148    memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
02149    memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
02150    if (rtp->rtcp) {
02151       memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->rtcp->them.sin_addr));
02152       memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->rtcp->them.sin_port));
02153    }
02154    
02155    ast_clear_flag(rtp, FLAG_P2P_SENT_MARK);
02156 }

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

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

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

void ast_rtp_unset_m_type ( struct ast_rtp rtp,
int  pt 
)

clear payload type

Definition at line 1720 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_a_audio(), and process_sdp_a_video().

01721 {
01722    if (pt < 0 || pt >= MAX_RTP_PT)
01723       return; /* bogus payload type */
01724 
01725    ast_mutex_lock(&rtp->bridge_lock);
01726    rtp->current_RTP_PT[pt].isAstFormat = 0;
01727    rtp->current_RTP_PT[pt].code = 0;
01728    ast_mutex_unlock(&rtp->bridge_lock);
01729 }

int ast_rtp_write ( struct ast_rtp rtp,
struct ast_frame f 
)

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

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


Generated on Thu Jan 28 17:36:13 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7