Sat Mar 26 11:26:16 2011

Asterisk developer's documentation


rtp.h File Reference

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

#include "asterisk/network.h"
#include "asterisk/frame.h"
#include "asterisk/io.h"
#include "asterisk/sched.h"
#include "asterisk/channel.h"
#include "asterisk/linkedlists.h"

Include dependency graph for rtp.h:

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

Go to the source code of this file.

Data Structures

struct  ast_rtp_protocol
 This is the structure that binds a channel (SIP/Jingle/H.323) to the RTP subsystem. More...
struct  ast_rtp_quality
 RTCP quality report storage. More...
struct  rtpPayloadType
 The value of each payload format mapping:. More...

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
#define RED_MAX_GENERATION   5

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) }
enum  ast_rtp_qos_vars {
  AST_RTP_TXCOUNT, AST_RTP_RXCOUNT, AST_RTP_TXJITTER, AST_RTP_RXJITTER,
  AST_RTP_RXPLOSS, AST_RTP_TXPLOSS, AST_RTP_RTT
}
 Variables used in ast_rtcp_get function. More...
enum  ast_rtp_quality_type { RTPQOS_SUMMARY = 0, RTPQOS_JITTER, RTPQOS_LOSS, RTPQOS_RTT }

Functions

int ast_rtcp_fd (struct ast_rtp *rtp)
struct ast_frame * ast_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)
 The RTP bridge.
void ast_rtp_change_source (struct ast_rtp *rtp)
 Indicate that we need to set the marker bit and change the ssrc.
int ast_rtp_codec_getformat (int pt)
 get format from predefined dynamic payload format
struct ast_codec_pref * ast_rtp_codec_getpref (struct ast_rtp *rtp)
 Get codec preference.
void ast_rtp_codec_setpref (struct ast_rtp *rtp, struct ast_codec_pref *prefs)
 Set codec preference.
void ast_rtp_destroy (struct ast_rtp *rtp)
int ast_rtp_early_bridge (struct ast_channel *c0, struct ast_channel *c1)
 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)
struct ast_rtp * ast_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)
int ast_rtp_get_qos (struct ast_rtp *rtp, const char *qos, char *buf, unsigned int buflen)
 Get QOS stats on a RTP channel.
unsigned int ast_rtp_get_qosvalue (struct ast_rtp *rtp, enum ast_rtp_qos_vars value)
 Return RTP and RTCP QoS values.
char * ast_rtp_get_quality (struct ast_rtp *rtp, struct ast_rtp_quality *qual, enum ast_rtp_quality_type qtype)
 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):.
struct rtpPayloadType ast_rtp_lookup_pt (struct ast_rtp *rtp, int pt)
 Mapping between RTP payload format codes and Asterisk codes:.
unsigned int ast_rtp_lookup_sample_rate (int isAstFormat, int code)
 Get the sample rate associated with known RTP payload types.
int ast_rtp_make_compatible (struct ast_channel *dest, struct ast_channel *src, int media)
struct ast_rtp * ast_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)
 Indicate that we need to set the marker bit.
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.
int ast_rtp_proto_register (struct ast_rtp_protocol *proto)
 Register an RTP channel client.
void ast_rtp_proto_unregister (struct ast_rtp_protocol *proto)
 Unregister an RTP channel client.
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.
struct ast_frame * ast_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)
int ast_rtp_senddigit_end_with_duration (struct ast_rtp *rtp, char digit, unsigned int duration)
 Send end packets for DTMF.
void ast_rtp_set_alt_peer (struct ast_rtp *rtp, struct sockaddr_in *alt)
 set potential alternate source for RTP media
void ast_rtp_set_callback (struct ast_rtp *rtp, ast_rtp_callback callback)
void ast_rtp_set_data (struct ast_rtp *rtp, void *data)
void ast_rtp_set_m_type (struct ast_rtp *rtp, int pt)
 Activate payload type.
void ast_rtp_set_peer (struct ast_rtp *rtp, struct sockaddr_in *them)
void ast_rtp_set_rtpholdtimeout (struct ast_rtp *rtp, int timeout)
 Set rtp hold timeout.
void ast_rtp_set_rtpkeepalive (struct ast_rtp *rtp, int period)
 set RTP keepalive interval
int ast_rtp_set_rtpmap_type (struct ast_rtp *rtp, int pt, char *mimeType, char *mimeSubtype, enum ast_rtp_options options)
 Set payload type to a known MIME media type for a codec.
int ast_rtp_set_rtpmap_type_rate (struct ast_rtp *rtp, int pt, char *mimeType, char *mimeSubtype, enum ast_rtp_options options, unsigned int sample_rate)
 Set payload type to a known MIME media type for a codec with a specific sample rate.
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_set_vars (struct ast_channel *chan, struct ast_rtp *rtp)
 Set RTPAUDIOQOS(...) variables on a channel when it is being hung up.
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)
int ast_rtp_setqos (struct ast_rtp *rtp, int tos, int cos, char *desc)
void ast_rtp_setstun (struct ast_rtp *rtp, int stun_enable)
 Enable STUN capability.
void ast_rtp_stop (struct ast_rtp *rtp)
void ast_rtp_stun_request (struct ast_rtp *rtp, struct sockaddr_in *suggestion, const char *username)
 Send STUN request for an RTP socket Deprecated, this is just a wrapper for ast_rtp_stun_request().
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)
int ast_stun_request (int s, struct sockaddr_in *dst, const char *username, struct sockaddr_in *answer)
 Generic STUN request send a generic stun request to the server specified.
void red_buffer_t140 (struct ast_rtp *rtp, struct ast_frame *f)
 Buffer t.140 data.
int rtp_red_init (struct ast_rtp *rtp, int ti, int *pt, int num_gen)
 Initalize t.140 redudancy.


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)

#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 57 of file rtp.h.

Referenced by process_rfc3389().

#define MAX_RTP_PT   256

#define RED_MAX_GENERATION   5

T.140 Redundancy Maxium number of generations

Definition at line 55 of file rtp.h.

Referenced by process_sdp_a_text().


Typedef Documentation

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

RTP callback structure

Definition at line 130 of file rtp.h.


Enumeration Type Documentation

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 63 of file rtp.h.

00063                         {
00064    /*! Failed to find the RTP structure */
00065    AST_RTP_GET_FAILED = 0,
00066    /*! RTP structure exists but true native bridge can not occur so try partial */
00067    AST_RTP_TRY_PARTIAL,
00068    /*! RTP structure exists and native bridge can occur */
00069    AST_RTP_TRY_NATIVE,
00070 };

Enumerator:
AST_RTP_OPT_G726_NONSTANDARD 

Definition at line 59 of file rtp.h.

00059                      {
00060    AST_RTP_OPT_G726_NONSTANDARD = (1 << 0),
00061 };

Variables used in ast_rtcp_get function.

Enumerator:
AST_RTP_TXCOUNT 
AST_RTP_RXCOUNT 
AST_RTP_TXJITTER 
AST_RTP_RXJITTER 
AST_RTP_RXPLOSS 
AST_RTP_TXPLOSS 
AST_RTP_RTT 

Definition at line 73 of file rtp.h.

00073                       {
00074    AST_RTP_TXCOUNT,
00075    AST_RTP_RXCOUNT,
00076    AST_RTP_TXJITTER,
00077    AST_RTP_RXJITTER,
00078    AST_RTP_RXPLOSS,
00079    AST_RTP_TXPLOSS,
00080    AST_RTP_RTT
00081 };

Enumerator:
RTPQOS_SUMMARY 
RTPQOS_JITTER 
RTPQOS_LOSS 
RTPQOS_RTT 

Definition at line 109 of file rtp.h.

00109                           {
00110    RTPQOS_SUMMARY = 0,
00111    RTPQOS_JITTER,
00112    RTPQOS_LOSS,
00113    RTPQOS_RTT
00114 };


Function Documentation

int ast_rtcp_fd ( struct ast_rtp *  rtp  ) 

Definition at line 722 of file rtp.c.

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

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

00723 {
00724    if (rtp->rtcp)
00725       return rtp->rtcp->s;
00726    return -1;
00727 }

struct ast_frame* ast_rtcp_read ( struct ast_rtp *  rtp  )  [read]

Definition at line 1182 of file rtp.c.

References ast_rtcp::accumulated_transit, ast_rtcp::altthem, ast_assert, AST_CONTROL_VIDUPDATE, ast_debug, AST_FRAME_CONTROL, AST_FRIENDLY_OFFSET, ast_inet_ntoa(), ast_log(), ast_null_frame, ast_verbose, ast_frame::datalen, errno, EVENT_FLAG_REPORTING, ast_rtp::f, f, ast_frame::frametype, len(), LOG_DEBUG, LOG_WARNING, ast_frame::mallocd, manager_event, ast_rtcp::maxrtt, ast_rtcp::minrtt, ast_rtp::nat, normdev_compute(), ast_rtcp::normdevrtt, option_debug, ast_rtcp::reported_jitter, ast_rtcp::reported_jitter_count, ast_rtcp::reported_lost, ast_rtcp::reported_maxjitter, ast_rtcp::reported_maxlost, ast_rtcp::reported_minjitter, ast_rtcp::reported_minlost, ast_rtcp::reported_normdev_jitter, ast_rtcp::reported_normdev_lost, ast_rtcp::reported_stdev_jitter, ast_rtcp::reported_stdev_lost, ast_rtp::rtcp, rtcp_debug_test_addr(), ast_rtcp::rtcp_info, RTCP_PT_BYE, RTCP_PT_FUR, RTCP_PT_RR, RTCP_PT_SDES, RTCP_PT_SR, ast_rtcp::rtt, ast_rtcp::rtt_count, ast_rtcp::rxlsr, ast_rtcp::s, ast_frame::samples, ast_rtcp::soc, ast_rtcp::spc, ast_frame::src, stddev_compute(), ast_rtcp::stdevrtt, ast_frame::subclass, ast_rtcp::them, ast_rtcp::themrxlsr, and timeval2ntp().

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

01183 {
01184    socklen_t len;
01185    int position, i, packetwords;
01186    int res;
01187    struct sockaddr_in sock_in;
01188    unsigned int rtcpdata[8192 + AST_FRIENDLY_OFFSET];
01189    unsigned int *rtcpheader;
01190    int pt;
01191    struct timeval now;
01192    unsigned int length;
01193    int rc;
01194    double rttsec;
01195    uint64_t rtt = 0;
01196    unsigned int dlsr;
01197    unsigned int lsr;
01198    unsigned int msw;
01199    unsigned int lsw;
01200    unsigned int comp;
01201    struct ast_frame *f = &ast_null_frame;
01202    
01203    double reported_jitter;
01204    double reported_normdev_jitter_current;
01205    double normdevrtt_current;
01206    double reported_lost;
01207    double reported_normdev_lost_current;
01208 
01209    if (!rtp || !rtp->rtcp)
01210       return &ast_null_frame;
01211 
01212    len = sizeof(sock_in);
01213    
01214    res = recvfrom(rtp->rtcp->s, rtcpdata + AST_FRIENDLY_OFFSET, sizeof(rtcpdata) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET,
01215                0, (struct sockaddr *)&sock_in, &len);
01216    rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
01217    
01218    if (res < 0) {
01219       ast_assert(errno != EBADF);
01220       if (errno != EAGAIN) {
01221          ast_log(LOG_WARNING, "RTCP Read error: %s.  Hanging up.\n", strerror(errno));
01222          return NULL;
01223       }
01224       return &ast_null_frame;
01225    }
01226 
01227    packetwords = res / 4;
01228    
01229    if (rtp->nat) {
01230       /* Send to whoever sent to us */
01231       if (((rtp->rtcp->them.sin_addr.s_addr != sock_in.sin_addr.s_addr) ||
01232           (rtp->rtcp->them.sin_port != sock_in.sin_port)) && 
01233           ((rtp->rtcp->altthem.sin_addr.s_addr != sock_in.sin_addr.s_addr) || 
01234           (rtp->rtcp->altthem.sin_port != sock_in.sin_port))) {
01235          memcpy(&rtp->rtcp->them, &sock_in, sizeof(rtp->rtcp->them));
01236          if (option_debug || rtpdebug)
01237             ast_debug(0, "RTCP NAT: Got RTCP from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
01238       }
01239    }
01240 
01241    ast_debug(1, "Got RTCP report of %d bytes\n", res);
01242 
01243    /* Process a compound packet */
01244    position = 0;
01245    while (position < packetwords) {
01246       i = position;
01247       length = ntohl(rtcpheader[i]);
01248       pt = (length & 0xff0000) >> 16;
01249       rc = (length & 0x1f000000) >> 24;
01250       length &= 0xffff;
01251  
01252       if ((i + length) > packetwords) {
01253          if (option_debug || rtpdebug)
01254             ast_log(LOG_DEBUG, "RTCP Read too short\n");
01255          return &ast_null_frame;
01256       }
01257       
01258       if (rtcp_debug_test_addr(&sock_in)) {
01259          ast_verbose("\n\nGot RTCP from %s:%d\n", ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port));
01260          ast_verbose("PT: %d(%s)\n", pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown");
01261          ast_verbose("Reception reports: %d\n", rc);
01262          ast_verbose("SSRC of sender: %u\n", rtcpheader[i + 1]);
01263       }
01264  
01265       i += 2; /* Advance past header and ssrc */
01266       
01267       switch (pt) {
01268       case RTCP_PT_SR:
01269          gettimeofday(&rtp->rtcp->rxlsr,NULL); /* To be able to populate the dlsr */
01270          rtp->rtcp->spc = ntohl(rtcpheader[i+3]);
01271          rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
01272          rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16); /* Going to LSR in RR*/
01273  
01274          if (rtcp_debug_test_addr(&sock_in)) {
01275             ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader[i]), (unsigned long) ntohl(rtcpheader[i + 1]) * 4096);
01276             ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader[i + 2]));
01277             ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader[i + 3]), (unsigned long) ntohl(rtcpheader[i + 4]));
01278          }
01279          i += 5;
01280          if (rc < 1)
01281             break;
01282          /* Intentional fall through */
01283       case RTCP_PT_RR:
01284          /* Don't handle multiple reception reports (rc > 1) yet */
01285          /* Calculate RTT per RFC */
01286          gettimeofday(&now, NULL);
01287          timeval2ntp(now, &msw, &lsw);
01288          if (ntohl(rtcpheader[i + 4]) && ntohl(rtcpheader[i + 5])) { /* We must have the LSR && DLSR */
01289             comp = ((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16);
01290             lsr = ntohl(rtcpheader[i + 4]);
01291             dlsr = ntohl(rtcpheader[i + 5]);
01292             rtt = comp - lsr - dlsr;
01293 
01294             /* Convert end to end delay to usec (keeping the calculation in 64bit space)
01295                sess->ee_delay = (eedelay * 1000) / 65536; */
01296             if (rtt < 4294) {
01297                 rtt = (rtt * 1000000) >> 16;
01298             } else {
01299                 rtt = (rtt * 1000) >> 16;
01300                 rtt *= 1000;
01301             }
01302             rtt = rtt / 1000.;
01303             rttsec = rtt / 1000.;
01304             rtp->rtcp->rtt = rttsec;
01305 
01306             if (comp - dlsr >= lsr) {
01307                rtp->rtcp->accumulated_transit += rttsec;
01308 
01309                if (rtp->rtcp->rtt_count == 0) 
01310                   rtp->rtcp->minrtt = rttsec;
01311 
01312                if (rtp->rtcp->maxrtt<rttsec)
01313                   rtp->rtcp->maxrtt = rttsec;
01314 
01315                if (rtp->rtcp->minrtt>rttsec)
01316                   rtp->rtcp->minrtt = rttsec;
01317 
01318                normdevrtt_current = normdev_compute(rtp->rtcp->normdevrtt, rttsec, rtp->rtcp->rtt_count);
01319 
01320                rtp->rtcp->stdevrtt = stddev_compute(rtp->rtcp->stdevrtt, rttsec, rtp->rtcp->normdevrtt, normdevrtt_current, rtp->rtcp->rtt_count);
01321 
01322                rtp->rtcp->normdevrtt = normdevrtt_current;
01323 
01324                rtp->rtcp->rtt_count++;
01325             } else if (rtcp_debug_test_addr(&sock_in)) {
01326                ast_verbose("Internal RTCP NTP clock skew detected: "
01327                         "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
01328                         "diff=%d\n",
01329                         lsr, comp, dlsr, dlsr / 65536,
01330                         (dlsr % 65536) * 1000 / 65536,
01331                         dlsr - (comp - lsr));
01332             }
01333          }
01334 
01335          rtp->rtcp->reported_jitter = ntohl(rtcpheader[i + 3]);
01336          reported_jitter = (double) rtp->rtcp->reported_jitter;
01337 
01338          if (rtp->rtcp->reported_jitter_count == 0) 
01339             rtp->rtcp->reported_minjitter = reported_jitter;
01340 
01341          if (reported_jitter < rtp->rtcp->reported_minjitter) 
01342             rtp->rtcp->reported_minjitter = reported_jitter;
01343 
01344          if (reported_jitter > rtp->rtcp->reported_maxjitter) 
01345             rtp->rtcp->reported_maxjitter = reported_jitter;
01346 
01347          reported_normdev_jitter_current = normdev_compute(rtp->rtcp->reported_normdev_jitter, reported_jitter, rtp->rtcp->reported_jitter_count);
01348 
01349          rtp->rtcp->reported_stdev_jitter = stddev_compute(rtp->rtcp->reported_stdev_jitter, reported_jitter, rtp->rtcp->reported_normdev_jitter, reported_normdev_jitter_current, rtp->rtcp->reported_jitter_count);
01350 
01351          rtp->rtcp->reported_normdev_jitter = reported_normdev_jitter_current;
01352 
01353          rtp->rtcp->reported_lost = ntohl(rtcpheader[i + 1]) & 0xffffff;
01354 
01355          reported_lost = (double) rtp->rtcp->reported_lost;
01356 
01357          /* using same counter as for jitter */
01358          if (rtp->rtcp->reported_jitter_count == 0)
01359             rtp->rtcp->reported_minlost = reported_lost;
01360 
01361          if (reported_lost < rtp->rtcp->reported_minlost)
01362             rtp->rtcp->reported_minlost = reported_lost;
01363 
01364          if (reported_lost > rtp->rtcp->reported_maxlost) 
01365             rtp->rtcp->reported_maxlost = reported_lost;
01366 
01367          reported_normdev_lost_current = normdev_compute(rtp->rtcp->reported_normdev_lost, reported_lost, rtp->rtcp->reported_jitter_count);
01368 
01369          rtp->rtcp->reported_stdev_lost = stddev_compute(rtp->rtcp->reported_stdev_lost, reported_lost, rtp->rtcp->reported_normdev_lost, reported_normdev_lost_current, rtp->rtcp->reported_jitter_count);
01370 
01371          rtp->rtcp->reported_normdev_lost = reported_normdev_lost_current;
01372 
01373          rtp->rtcp->reported_jitter_count++;
01374 
01375          if (rtcp_debug_test_addr(&sock_in)) {
01376             ast_verbose("  Fraction lost: %ld\n", (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24));
01377             ast_verbose("  Packets lost so far: %d\n", rtp->rtcp->reported_lost);
01378             ast_verbose("  Highest sequence number: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff));
01379             ast_verbose("  Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16);
01380             ast_verbose("  Interarrival jitter: %u\n", rtp->rtcp->reported_jitter);
01381             ast_verbose("  Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader[i + 4]) >> 16,((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096);
01382             ast_verbose("  DLSR: %4.4f (sec)\n",ntohl(rtcpheader[i + 5])/65536.0);
01383             if (rtt)
01384                ast_verbose("  RTT: %lu(sec)\n", (unsigned long) rtt);
01385          }
01386 
01387          if (rtt) {
01388             manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s:%d\r\n"
01389                             "PT: %d(%s)\r\n"
01390                             "ReceptionReports: %d\r\n"
01391                             "SenderSSRC: %u\r\n"
01392                             "FractionLost: %ld\r\n"
01393                             "PacketsLost: %d\r\n"
01394                             "HighestSequence: %ld\r\n"
01395                             "SequenceNumberCycles: %ld\r\n"
01396                             "IAJitter: %u\r\n"
01397                             "LastSR: %lu.%010lu\r\n"
01398                             "DLSR: %4.4f(sec)\r\n"
01399                             "RTT: %llu(sec)\r\n",
01400                             ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port),
01401                             pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
01402                             rc,
01403                             rtcpheader[i + 1],
01404                             (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
01405                             rtp->rtcp->reported_lost,
01406                             (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
01407                             (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16,
01408                             rtp->rtcp->reported_jitter,
01409                             (unsigned long) ntohl(rtcpheader[i + 4]) >> 16, ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
01410                             ntohl(rtcpheader[i + 5])/65536.0,
01411                             (unsigned long long)rtt);
01412          } else {
01413             manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s:%d\r\n"
01414                             "PT: %d(%s)\r\n"
01415                             "ReceptionReports: %d\r\n"
01416                             "SenderSSRC: %u\r\n"
01417                             "FractionLost: %ld\r\n"
01418                             "PacketsLost: %d\r\n"
01419                             "HighestSequence: %ld\r\n"
01420                             "SequenceNumberCycles: %ld\r\n"
01421                             "IAJitter: %u\r\n"
01422                             "LastSR: %lu.%010lu\r\n"
01423                             "DLSR: %4.4f(sec)\r\n",
01424                             ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port),
01425                             pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
01426                             rc,
01427                             rtcpheader[i + 1],
01428                             (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
01429                             rtp->rtcp->reported_lost,
01430                             (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
01431                             (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16,
01432                             rtp->rtcp->reported_jitter,
01433                             (unsigned long) ntohl(rtcpheader[i + 4]) >> 16,
01434                             ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
01435                             ntohl(rtcpheader[i + 5])/65536.0);
01436          }
01437          break;
01438       case RTCP_PT_FUR:
01439          if (rtcp_debug_test_addr(&sock_in))
01440             ast_verbose("Received an RTCP Fast Update Request\n");
01441          rtp->f.frametype = AST_FRAME_CONTROL;
01442          rtp->f.subclass = AST_CONTROL_VIDUPDATE;
01443          rtp->f.datalen = 0;
01444          rtp->f.samples = 0;
01445          rtp->f.mallocd = 0;
01446          rtp->f.src = "RTP";
01447          f = &rtp->f;
01448          break;
01449       case RTCP_PT_SDES:
01450          if (rtcp_debug_test_addr(&sock_in))
01451             ast_verbose("Received an SDES from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
01452          break;
01453       case RTCP_PT_BYE:
01454          if (rtcp_debug_test_addr(&sock_in))
01455             ast_verbose("Received a BYE from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
01456          break;
01457       default:
01458          ast_debug(1, "Unknown RTCP packet (pt=%d) received from %s:%d\n", pt, ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
01459          break;
01460       }
01461       position += (length + 1);
01462    }
01463    rtp->rtcp->rtcp_info = 1;  
01464    return f;
01465 }

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

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

03358 {
03359    struct ast_rtp *rtp = data;
03360    int res;
03361 
03362    rtp->rtcp->sendfur = 1;
03363    res = ast_rtcp_write(data);
03364    
03365    return res;
03366 }

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

Referenced by process_sdp().

00497 {
00498    return sizeof(struct ast_rtp);
00499 }

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

The RTP bridge.

Definition at line 4456 of file rtp.c.

References AST_BRIDGE_DTMF_CHANNEL_0, AST_BRIDGE_DTMF_CHANNEL_1, AST_BRIDGE_FAILED, AST_BRIDGE_FAILED_NOWARN, ast_channel_lock, ast_channel_trylock, ast_channel_unlock, ast_check_hangup(), ast_codec_pref_getsize(), ast_debug, ast_log(), AST_RTP_GET_FAILED, AST_RTP_TRY_NATIVE, AST_RTP_TRY_PARTIAL, ast_set_flag, ast_test_flag, ast_verb, 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_trtp_info, ast_rtp_protocol::get_vrtp_info, LOG_WARNING, ast_channel::name, ast_rtp::pref, ast_channel::rawreadformat, ast_channel::rawwriteformat, ast_channel_tech::send_digit_begin, ast_channel::tech, and ast_channel::tech_pvt.

04457 {
04458    struct ast_rtp *p0 = NULL, *p1 = NULL;    /* Audio RTP Channels */
04459    struct ast_rtp *vp0 = NULL, *vp1 = NULL;  /* Video RTP channels */
04460    struct ast_rtp *tp0 = NULL, *tp1 = NULL;  /* Text RTP channels */
04461    struct ast_rtp_protocol *pr0 = NULL, *pr1 = NULL;
04462    enum ast_rtp_get_result audio_p0_res = AST_RTP_GET_FAILED, video_p0_res = AST_RTP_GET_FAILED, text_p0_res = AST_RTP_GET_FAILED;
04463    enum ast_rtp_get_result audio_p1_res = AST_RTP_GET_FAILED, video_p1_res = AST_RTP_GET_FAILED, text_p1_res = AST_RTP_GET_FAILED;
04464    enum ast_bridge_result res = AST_BRIDGE_FAILED;
04465    int codec0 = 0, codec1 = 0;
04466    void *pvt0 = NULL, *pvt1 = NULL;
04467 
04468    /* Lock channels */
04469    ast_channel_lock(c0);
04470    while (ast_channel_trylock(c1)) {
04471       ast_channel_unlock(c0);
04472       usleep(1);
04473       ast_channel_lock(c0);
04474    }
04475 
04476    /* Ensure neither channel got hungup during lock avoidance */
04477    if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
04478       ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
04479       ast_channel_unlock(c0);
04480       ast_channel_unlock(c1);
04481       return AST_BRIDGE_FAILED;
04482    }
04483       
04484    /* Find channel driver interfaces */
04485    if (!(pr0 = get_proto(c0))) {
04486       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
04487       ast_channel_unlock(c0);
04488       ast_channel_unlock(c1);
04489       return AST_BRIDGE_FAILED;
04490    }
04491    if (!(pr1 = get_proto(c1))) {
04492       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
04493       ast_channel_unlock(c0);
04494       ast_channel_unlock(c1);
04495       return AST_BRIDGE_FAILED;
04496    }
04497 
04498    /* Get channel specific interface structures */
04499    pvt0 = c0->tech_pvt;
04500    pvt1 = c1->tech_pvt;
04501 
04502    /* Get audio and video interface (if native bridge is possible) */
04503    audio_p0_res = pr0->get_rtp_info(c0, &p0);
04504    video_p0_res = pr0->get_vrtp_info ? pr0->get_vrtp_info(c0, &vp0) : AST_RTP_GET_FAILED;
04505    text_p0_res = pr0->get_trtp_info ? pr0->get_trtp_info(c0, &vp0) : AST_RTP_GET_FAILED;
04506    audio_p1_res = pr1->get_rtp_info(c1, &p1);
04507    video_p1_res = pr1->get_vrtp_info ? pr1->get_vrtp_info(c1, &vp1) : AST_RTP_GET_FAILED;
04508    text_p1_res = pr1->get_trtp_info ? pr1->get_trtp_info(c1, &vp1) : AST_RTP_GET_FAILED;
04509 
04510    /* If we are carrying video, and both sides are not reinviting... then fail the native bridge */
04511    if (video_p0_res != AST_RTP_GET_FAILED && (audio_p0_res != AST_RTP_TRY_NATIVE || video_p0_res != AST_RTP_TRY_NATIVE))
04512       audio_p0_res = AST_RTP_GET_FAILED;
04513    if (video_p1_res != AST_RTP_GET_FAILED && (audio_p1_res != AST_RTP_TRY_NATIVE || video_p1_res != AST_RTP_TRY_NATIVE))
04514       audio_p1_res = AST_RTP_GET_FAILED;
04515 
04516    /* Check if a bridge is possible (partial/native) */
04517    if (audio_p0_res == AST_RTP_GET_FAILED || audio_p1_res == AST_RTP_GET_FAILED) {
04518       /* Somebody doesn't want to play... */
04519       ast_channel_unlock(c0);
04520       ast_channel_unlock(c1);
04521       return AST_BRIDGE_FAILED_NOWARN;
04522    }
04523 
04524    /* If we need to feed DTMF frames into the core then only do a partial native bridge */
04525    if (ast_test_flag(p0, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
04526       ast_set_flag(p0, FLAG_P2P_NEED_DTMF);
04527       audio_p0_res = AST_RTP_TRY_PARTIAL;
04528    }
04529 
04530    if (ast_test_flag(p1, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)) {
04531       ast_set_flag(p1, FLAG_P2P_NEED_DTMF);
04532       audio_p1_res = AST_RTP_TRY_PARTIAL;
04533    }
04534 
04535    /* If both sides are not using the same method of DTMF transmission 
04536     * (ie: one is RFC2833, other is INFO... then we can not do direct media. 
04537     * --------------------------------------------------
04538     * | DTMF Mode |  HAS_DTMF  |  Accepts Begin Frames |
04539     * |-----------|------------|-----------------------|
04540     * | Inband    | False      | True                  |
04541     * | RFC2833   | True       | True                  |
04542     * | SIP INFO  | False      | False                 |
04543     * --------------------------------------------------
04544     * However, if DTMF from both channels is being monitored by the core, then
04545     * we can still do packet-to-packet bridging, because passing through the 
04546     * core will handle DTMF mode translation.
04547     */
04548    if ((ast_test_flag(p0, FLAG_HAS_DTMF) != ast_test_flag(p1, FLAG_HAS_DTMF)) ||
04549       (!c0->tech->send_digit_begin != !c1->tech->send_digit_begin)) {
04550       if (!ast_test_flag(p0, FLAG_P2P_NEED_DTMF) || !ast_test_flag(p1, FLAG_P2P_NEED_DTMF)) {
04551          ast_channel_unlock(c0);
04552          ast_channel_unlock(c1);
04553          return AST_BRIDGE_FAILED_NOWARN;
04554       }
04555       audio_p0_res = AST_RTP_TRY_PARTIAL;
04556       audio_p1_res = AST_RTP_TRY_PARTIAL;
04557    }
04558 
04559    /* If we need to feed frames into the core don't do a P2P bridge */
04560    if ((audio_p0_res == AST_RTP_TRY_PARTIAL && ast_test_flag(p0, FLAG_P2P_NEED_DTMF)) ||
04561        (audio_p1_res == AST_RTP_TRY_PARTIAL && ast_test_flag(p1, FLAG_P2P_NEED_DTMF))) {
04562       ast_channel_unlock(c0);
04563       ast_channel_unlock(c1);
04564       return AST_BRIDGE_FAILED_NOWARN;
04565    }
04566 
04567    /* Get codecs from both sides */
04568    codec0 = pr0->get_codec ? pr0->get_codec(c0) : 0;
04569    codec1 = pr1->get_codec ? pr1->get_codec(c1) : 0;
04570    if (codec0 && codec1 && !(codec0 & codec1)) {
04571       /* Hey, we can't do native bridging if both parties speak different codecs */
04572       ast_debug(3, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0, codec1);
04573       ast_channel_unlock(c0);
04574       ast_channel_unlock(c1);
04575       return AST_BRIDGE_FAILED_NOWARN;
04576    }
04577 
04578    /* If either side can only do a partial bridge, then don't try for a true native bridge */
04579    if (audio_p0_res == AST_RTP_TRY_PARTIAL || audio_p1_res == AST_RTP_TRY_PARTIAL) {
04580       struct ast_format_list fmt0, fmt1;
04581 
04582       /* In order to do Packet2Packet bridging both sides must be in the same rawread/rawwrite */
04583       if (c0->rawreadformat != c1->rawwriteformat || c1->rawreadformat != c0->rawwriteformat) {
04584          ast_debug(1, "Cannot packet2packet bridge - raw formats are incompatible\n");
04585          ast_channel_unlock(c0);
04586          ast_channel_unlock(c1);
04587          return AST_BRIDGE_FAILED_NOWARN;
04588       }
04589       /* They must also be using the same packetization */
04590       fmt0 = ast_codec_pref_getsize(&p0->pref, c0->rawreadformat);
04591       fmt1 = ast_codec_pref_getsize(&p1->pref, c1->rawreadformat);
04592       if (fmt0.cur_ms != fmt1.cur_ms) {
04593          ast_debug(1, "Cannot packet2packet bridge - packetization settings prevent it\n");
04594          ast_channel_unlock(c0);
04595          ast_channel_unlock(c1);
04596          return AST_BRIDGE_FAILED_NOWARN;
04597       }
04598 
04599       ast_verb(3, "Packet2Packet bridging %s and %s\n", c0->name, c1->name);
04600       res = bridge_p2p_loop(c0, c1, p0, p1, timeoutms, flags, fo, rc, pvt0, pvt1);
04601    } else {
04602       ast_verb(3, "Native bridging %s and %s\n", c0->name, c1->name);
04603       res = bridge_native_loop(c0, c1, p0, p1, vp0, vp1, tp0, tp1, pr0, pr1, codec0, codec1, timeoutms, flags, fo, rc, pvt0, pvt1);
04604    }
04605 
04606    return res;
04607 }

void ast_rtp_change_source ( struct ast_rtp *  rtp  ) 

Indicate that we need to set the marker bit and change the ssrc.

Definition at line 2692 of file rtp.c.

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

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

02693 {
02694    if (rtp) {
02695       unsigned int ssrc = ast_random();
02696 
02697       rtp->set_marker_bit = 1;
02698       ast_debug(3, "Changing ssrc from %u to %u due to a source change\n", rtp->ssrc, ssrc);
02699       rtp->ssrc = ssrc;
02700    }
02701 }

int ast_rtp_codec_getformat ( int  pt  ) 

get format from predefined dynamic payload format

Definition at line 3837 of file rtp.c.

References rtpPayloadType::code, and MAX_RTP_PT.

Referenced by process_sdp_a_audio().

03838 {
03839    if (pt < 0 || pt >= MAX_RTP_PT)
03840       return 0; /* bogus payload type */
03841 
03842    if (static_RTP_PT[pt].isAstFormat)
03843       return static_RTP_PT[pt].code;
03844    else
03845       return 0;
03846 }

struct ast_codec_pref* ast_rtp_codec_getpref ( struct ast_rtp *  rtp  )  [read]

Get codec preference.

Definition at line 3832 of file rtp.c.

References ast_rtp::pref.

Referenced by add_codec_to_sdp(), and process_sdp_a_audio().

03833 {
03834    return &rtp->pref;
03835 }

void ast_rtp_codec_setpref ( struct ast_rtp *  rtp,
struct ast_codec_pref *  prefs 
)

Set codec preference.

Definition at line 3786 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, and ast_rtp::smoother.

Referenced by __oh323_rtp_create(), check_peer_ok(), create_addr_from_peer(), gtalk_new(), jingle_new(), process_sdp_a_audio(), register_verify(), set_peer_capabilities(), sip_alloc(), start_rtp(), and transmit_response_with_sdp().

03787 {
03788    struct ast_format_list current_format_old, current_format_new;
03789 
03790    /* if no packets have been sent through this session yet, then
03791     *  changing preferences does not require any extra work
03792     */
03793    if (rtp->lasttxformat == 0) {
03794       rtp->pref = *prefs;
03795       return;
03796    }
03797 
03798    current_format_old = ast_codec_pref_getsize(&rtp->pref, rtp->lasttxformat);
03799 
03800    rtp->pref = *prefs;
03801 
03802    current_format_new = ast_codec_pref_getsize(&rtp->pref, rtp->lasttxformat);
03803 
03804    /* if the framing desired for the current format has changed, we may have to create
03805     * or adjust the smoother for this session
03806     */
03807    if ((current_format_new.inc_ms != 0) &&
03808        (current_format_new.cur_ms != current_format_old.cur_ms)) {
03809       int new_size = (current_format_new.cur_ms * current_format_new.fr_len) / current_format_new.inc_ms;
03810 
03811       if (rtp->smoother) {
03812          ast_smoother_reconfigure(rtp->smoother, new_size);
03813          if (option_debug) {
03814             ast_log(LOG_DEBUG, "Adjusted smoother to %d ms and %d bytes\n", current_format_new.cur_ms, new_size);
03815          }
03816       } else {
03817          if (!(rtp->smoother = ast_smoother_new(new_size))) {
03818             ast_log(LOG_WARNING, "Unable to create smoother: format: %d ms: %d len: %d\n", rtp->lasttxformat, current_format_new.cur_ms, new_size);
03819             return;
03820          }
03821          if (current_format_new.flags) {
03822             ast_smoother_set_flags(rtp->smoother, current_format_new.flags);
03823          }
03824          if (option_debug) {
03825             ast_log(LOG_DEBUG, "Created smoother: format: %d ms: %d len: %d\n", rtp->lasttxformat, current_format_new.cur_ms, new_size);
03826          }
03827       }
03828    }
03829 
03830 }

void ast_rtp_destroy ( struct ast_rtp *  rtp  ) 

Destroy RTP session

Definition at line 3105 of file rtp.c.

References ast_free, ast_io_remove(), ast_mutex_destroy(), AST_SCHED_DEL, ast_smoother_free(), ast_verbose, EVENT_FLAG_REPORTING, ast_rtcp::expected_prior, ast_rtp::io, ast_rtp::ioid, manager_event, 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_peer_ok(), cleanup_connection(), create_addr_from_peer(), destroy_endpoint(), gtalk_free_pvt(), jingle_free_pvt(), mgcp_hangup(), oh323_alloc(), skinny_hangup(), start_rtp(), unalloc_sub(), and unistim_hangup().

03106 {
03107    if (rtcp_debug_test_addr(&rtp->them) || rtcpstats) {
03108       /*Print some info on the call here */
03109       ast_verbose("  RTP-stats\n");
03110       ast_verbose("* Our Receiver:\n");
03111       ast_verbose("  SSRC:     %u\n", rtp->themssrc);
03112       ast_verbose("  Received packets: %u\n", rtp->rxcount);
03113       ast_verbose("  Lost packets:   %u\n", rtp->rtcp ? (rtp->rtcp->expected_prior - rtp->rtcp->received_prior) : 0);
03114       ast_verbose("  Jitter:      %.4f\n", rtp->rxjitter);
03115       ast_verbose("  Transit:     %.4f\n", rtp->rxtransit);
03116       ast_verbose("  RR-count:    %u\n", rtp->rtcp ? rtp->rtcp->rr_count : 0);
03117       ast_verbose("* Our Sender:\n");
03118       ast_verbose("  SSRC:     %u\n", rtp->ssrc);
03119       ast_verbose("  Sent packets:   %u\n", rtp->txcount);
03120       ast_verbose("  Lost packets:   %u\n", rtp->rtcp ? rtp->rtcp->reported_lost : 0);
03121       ast_verbose("  Jitter:      %u\n", rtp->rtcp ? (rtp->rtcp->reported_jitter / (unsigned int)65536.0) : 0);
03122       ast_verbose("  SR-count:    %u\n", rtp->rtcp ? rtp->rtcp->sr_count : 0);
03123       ast_verbose("  RTT:      %f\n", rtp->rtcp ? rtp->rtcp->rtt : 0);
03124    }
03125 
03126    manager_event(EVENT_FLAG_REPORTING, "RTPReceiverStat", "SSRC: %u\r\n"
03127                    "ReceivedPackets: %u\r\n"
03128                    "LostPackets: %u\r\n"
03129                    "Jitter: %.4f\r\n"
03130                    "Transit: %.4f\r\n"
03131                    "RRCount: %u\r\n",
03132                    rtp->themssrc,
03133                    rtp->rxcount,
03134                    rtp->rtcp ? (rtp->rtcp->expected_prior - rtp->rtcp->received_prior) : 0,
03135                    rtp->rxjitter,
03136                    rtp->rxtransit,
03137                    rtp->rtcp ? rtp->rtcp->rr_count : 0);
03138    manager_event(EVENT_FLAG_REPORTING, "RTPSenderStat", "SSRC: %u\r\n"
03139                    "SentPackets: %u\r\n"
03140                    "LostPackets: %u\r\n"
03141                    "Jitter: %u\r\n"
03142                    "SRCount: %u\r\n"
03143                    "RTT: %f\r\n",
03144                    rtp->ssrc,
03145                    rtp->txcount,
03146                    rtp->rtcp ? rtp->rtcp->reported_lost : 0,
03147                    rtp->rtcp ? rtp->rtcp->reported_jitter : 0,
03148                    rtp->rtcp ? rtp->rtcp->sr_count : 0,
03149                    rtp->rtcp ? rtp->rtcp->rtt : 0);
03150    if (rtp->smoother)
03151       ast_smoother_free(rtp->smoother);
03152    if (rtp->ioid)
03153       ast_io_remove(rtp->io, rtp->ioid);
03154    if (rtp->s > -1)
03155       close(rtp->s);
03156    if (rtp->rtcp) {
03157       AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
03158       close(rtp->rtcp->s);
03159       ast_free(rtp->rtcp);
03160       rtp->rtcp=NULL;
03161    }
03162 #ifdef P2P_INTENSE
03163    ast_mutex_destroy(&rtp->bridge_lock);
03164 #endif
03165    ast_free(rtp);
03166 }

int ast_rtp_early_bridge ( struct ast_channel *  c0,
struct ast_channel *  c1 
)

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

Definition at line 2114 of file rtp.c.

References ast_channel_lock, ast_channel_trylock, ast_channel_unlock, ast_debug, 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_trtp_info, ast_rtp_protocol::get_vrtp_info, LOG_WARNING, ast_channel::name, and ast_rtp_protocol::set_rtp_peer.

02115 {
02116    struct ast_rtp *destp = NULL, *srcp = NULL;     /* Audio RTP Channels */
02117    struct ast_rtp *vdestp = NULL, *vsrcp = NULL;      /* Video RTP channels */
02118    struct ast_rtp *tdestp = NULL, *tsrcp = NULL;      /* Text RTP channels */
02119    struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
02120    enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED, text_dest_res = AST_RTP_GET_FAILED;
02121    enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED, text_src_res = AST_RTP_GET_FAILED;
02122    int srccodec, destcodec, nat_active = 0;
02123 
02124    /* Lock channels */
02125    ast_channel_lock(c0);
02126    if (c1) {
02127       while (ast_channel_trylock(c1)) {
02128          ast_channel_unlock(c0);
02129          usleep(1);
02130          ast_channel_lock(c0);
02131       }
02132    }
02133 
02134    /* Find channel driver interfaces */
02135    destpr = get_proto(c0);
02136    if (c1)
02137       srcpr = get_proto(c1);
02138    if (!destpr) {
02139       ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", c0->name);
02140       ast_channel_unlock(c0);
02141       if (c1)
02142          ast_channel_unlock(c1);
02143       return -1;
02144    }
02145    if (!srcpr) {
02146       ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", c1 ? c1->name : "<unspecified>");
02147       ast_channel_unlock(c0);
02148       if (c1)
02149          ast_channel_unlock(c1);
02150       return -1;
02151    }
02152 
02153    /* Get audio, video  and text interface (if native bridge is possible) */
02154    audio_dest_res = destpr->get_rtp_info(c0, &destp);
02155    video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(c0, &vdestp) : AST_RTP_GET_FAILED;
02156    text_dest_res = destpr->get_trtp_info ? destpr->get_trtp_info(c0, &tdestp) : AST_RTP_GET_FAILED;
02157    if (srcpr) {
02158       audio_src_res = srcpr->get_rtp_info(c1, &srcp);
02159       video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(c1, &vsrcp) : AST_RTP_GET_FAILED;
02160       text_src_res = srcpr->get_trtp_info ? srcpr->get_trtp_info(c1, &tsrcp) : AST_RTP_GET_FAILED;
02161    }
02162 
02163    /* Check if bridge is still possible (In SIP directmedia=no stops this, like NAT) */
02164    if (audio_dest_res != AST_RTP_TRY_NATIVE || (video_dest_res != AST_RTP_GET_FAILED && video_dest_res != AST_RTP_TRY_NATIVE)) {
02165       /* Somebody doesn't want to play... */
02166       ast_channel_unlock(c0);
02167       if (c1)
02168          ast_channel_unlock(c1);
02169       return -1;
02170    }
02171    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)
02172       srccodec = srcpr->get_codec(c1);
02173    else
02174       srccodec = 0;
02175    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)
02176       destcodec = destpr->get_codec(c0);
02177    else
02178       destcodec = 0;
02179    /* Ensure we have at least one matching codec */
02180    if (srcp && !(srccodec & destcodec)) {
02181       ast_channel_unlock(c0);
02182       ast_channel_unlock(c1);
02183       return 0;
02184    }
02185    /* Consider empty media as non-existent */
02186    if (audio_src_res == AST_RTP_TRY_NATIVE && !srcp->them.sin_addr.s_addr)
02187       srcp = NULL;
02188    if (srcp && (srcp->nat || ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
02189       nat_active = 1;
02190    /* Bridge media early */
02191    if (destpr->set_rtp_peer(c0, srcp, vsrcp, tsrcp, srccodec, nat_active))
02192       ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
02193    ast_channel_unlock(c0);
02194    if (c1)
02195       ast_channel_unlock(c1);
02196    ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
02197    return 0;
02198 }

int ast_rtp_fd ( struct ast_rtp *  rtp  ) 

Definition at line 717 of file rtp.c.

References ast_rtp::s.

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

00718 {
00719    return rtp->s;
00720 }

struct ast_rtp* ast_rtp_get_bridged ( struct ast_rtp *  rtp  )  [read]

Definition at line 2746 of file rtp.c.

References ast_rtp::bridged, rtp_bridge_lock(), and rtp_bridge_unlock().

Referenced by __sip_destroy(), ast_rtp_read(), and dialog_needdestroy().

02747 {
02748    struct ast_rtp *bridged = NULL;
02749 
02750    rtp_bridge_lock(rtp);
02751    bridged = rtp->bridged;
02752    rtp_bridge_unlock(rtp);
02753 
02754    return bridged;
02755 }

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

References rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, rtp_bridge_lock(), and rtp_bridge_unlock().

Referenced by gtalk_is_answered(), gtalk_newcall(), and process_sdp().

02364 {
02365    int pt;
02366    
02367    rtp_bridge_lock(rtp);
02368    
02369    *astFormats = *nonAstFormats = 0;
02370    for (pt = 0; pt < MAX_RTP_PT; ++pt) {
02371       if (rtp->current_RTP_PT[pt].isAstFormat) {
02372          *astFormats |= rtp->current_RTP_PT[pt].code;
02373       } else {
02374          *nonAstFormats |= rtp->current_RTP_PT[pt].code;
02375       }
02376    }
02377 
02378    rtp_bridge_unlock(rtp);
02379 }

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

Definition at line 2728 of file rtp.c.

References ast_rtp::them.

Referenced by acf_channel_read(), add_sdp(), bridge_native_loop(), check_rtp_timeout(), gtalk_update_stun(), oh323_set_rtp_peer(), process_sdp(), sip_set_rtp_peer(), skinny_set_rtp_peer(), and transmit_modify_with_sdp().

02729 {
02730    if ((them->sin_family != AF_INET) ||
02731       (them->sin_port != rtp->them.sin_port) ||
02732       (them->sin_addr.s_addr != rtp->them.sin_addr.s_addr)) {
02733       them->sin_family = AF_INET;
02734       them->sin_port = rtp->them.sin_port;
02735       them->sin_addr = rtp->them.sin_addr;
02736       return 1;
02737    }
02738    return 0;
02739 }

int ast_rtp_get_qos ( struct ast_rtp *  rtp,
const char *  qos,
char *  buf,
unsigned int  buflen 
)

Get QOS stats on a RTP channel.

Since:
1.6.1

Definition at line 2867 of file rtp.c.

References __ast_rtp_get_qos().

Referenced by acf_channel_read().

02868 {
02869    double value;
02870    int found;
02871 
02872    value = __ast_rtp_get_qos(rtp, qos, &found);
02873 
02874    if (!found)
02875       return -1;
02876 
02877    snprintf(buf, buflen, "%.0lf", value);
02878 
02879    return 0;
02880 }

unsigned int ast_rtp_get_qosvalue ( struct ast_rtp *  rtp,
enum ast_rtp_qos_vars  value 
)

Return RTP and RTCP QoS values.

Since:
1.6.1
Get QoS values from RTP and RTCP data (used in "sip show channelstats")

Definition at line 2801 of file rtp.c.

References ast_log(), AST_RTP_RTT, AST_RTP_RXCOUNT, AST_RTP_RXJITTER, AST_RTP_RXPLOSS, AST_RTP_TXCOUNT, AST_RTP_TXJITTER, AST_RTP_TXPLOSS, ast_rtcp::expected_prior, LOG_DEBUG, option_debug, ast_rtcp::received_prior, ast_rtcp::reported_jitter, ast_rtcp::reported_lost, ast_rtp::rtcp, ast_rtcp::rtt, ast_rtp::rxcount, ast_rtp::rxjitter, and ast_rtp::txcount.

Referenced by show_chanstats_cb().

02802 {
02803    if (rtp == NULL) {
02804       if (option_debug > 1)
02805          ast_log(LOG_DEBUG, "NO RTP Structure? Kidding me? \n");
02806       return 0;
02807    }
02808    if (option_debug > 1 && rtp->rtcp == NULL) {
02809       ast_log(LOG_DEBUG, "NO RTCP structure. Maybe in RTP p2p bridging mode? \n");
02810    }
02811 
02812    switch (value) {
02813    case AST_RTP_TXCOUNT:
02814       return (unsigned int) rtp->txcount;
02815    case AST_RTP_RXCOUNT:
02816       return (unsigned int) rtp->rxcount;
02817    case AST_RTP_TXJITTER:
02818       return (unsigned int) (rtp->rxjitter * 1000.0);
02819    case AST_RTP_RXJITTER:
02820       return (unsigned int) (rtp->rtcp ? (rtp->rtcp->reported_jitter / (unsigned int) 65536.0) : 0);
02821    case AST_RTP_RXPLOSS:
02822       return rtp->rtcp ? (rtp->rtcp->expected_prior - rtp->rtcp->received_prior) : 0;
02823    case AST_RTP_TXPLOSS:
02824       return rtp->rtcp ? rtp->rtcp->reported_lost : 0;
02825    case AST_RTP_RTT:
02826       return (unsigned int) (rtp->rtcp ? (rtp->rtcp->rtt * 100) : 0);
02827    }
02828    return 0;   /* To make the compiler happy */
02829 }

char* ast_rtp_get_quality ( struct ast_rtp *  rtp,
struct ast_rtp_quality *  qual,
enum ast_rtp_quality_type  qtype 
)

Return RTCP quality string.

Parameters:
rtp An rtp structure to get qos information about.
qual An (optional) rtp quality structure that will be filled with the quality information described in the ast_rtp_quality structure. This structure is not dependent on any qtype, so a call for any type of information would yield the same results because ast_rtp_quality is not a data type specific to any qos type.
qtype The quality type you'd like, default should be RTPQOS_SUMMARY which returns basic information about the call. The return from RTPQOS_SUMMARY is basically ast_rtp_quality in a string. The other types are RTPQOS_JITTER, RTPQOS_LOSS and RTPQOS_RTT which will return more specific statistics.
Version:
1.6.1 added qtype parameter

Definition at line 3074 of file rtp.c.

References __ast_rtp_get_quality(), __ast_rtp_get_quality_jitter(), __ast_rtp_get_quality_loss(), __ast_rtp_get_quality_rtt(), 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::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, RTPQOS_JITTER, RTPQOS_LOSS, RTPQOS_RTT, RTPQOS_SUMMARY, 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(), ast_rtp_set_vars(), handle_request_bye(), and sip_hangup().

03075 {
03076    if (qual && rtp) {
03077       qual->local_ssrc   = rtp->ssrc;
03078       qual->local_jitter = rtp->rxjitter;
03079       qual->local_count  = rtp->rxcount;
03080       qual->remote_ssrc  = rtp->themssrc;
03081       qual->remote_count = rtp->txcount;
03082 
03083       if (rtp->rtcp) {
03084          qual->local_lostpackets  = rtp->rtcp->expected_prior - rtp->rtcp->received_prior;
03085          qual->remote_lostpackets = rtp->rtcp->reported_lost;
03086          qual->remote_jitter      = rtp->rtcp->reported_jitter / 65536.0;
03087          qual->rtt                = rtp->rtcp->rtt;
03088       }
03089    }
03090 
03091    switch (qtype) {
03092    case RTPQOS_SUMMARY:
03093       return __ast_rtp_get_quality(rtp);
03094    case RTPQOS_JITTER:
03095       return __ast_rtp_get_quality_jitter(rtp);
03096    case RTPQOS_LOSS:
03097       return __ast_rtp_get_quality_loss(rtp);
03098    case RTPQOS_RTT:
03099       return __ast_rtp_get_quality_rtt(rtp);
03100    }
03101 
03102    return NULL;
03103 }

int ast_rtp_get_rtpholdtimeout ( struct ast_rtp *  rtp  ) 

Get rtp hold timeout.

Definition at line 777 of file rtp.c.

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

Referenced by check_rtp_timeout().

00778 {
00779    if (rtp->rtptimeout < 0)   /* We're not checking, but remembering the setting (during T.38 transmission) */
00780       return 0;
00781    return rtp->rtpholdtimeout;
00782 }

int ast_rtp_get_rtpkeepalive ( struct ast_rtp *  rtp  ) 

Get RTP keepalive interval.

Definition at line 785 of file rtp.c.

References ast_rtp::rtpkeepalive.

Referenced by check_rtp_timeout().

00786 {
00787    return rtp->rtpkeepalive;
00788 }

int ast_rtp_get_rtptimeout ( struct ast_rtp *  rtp  ) 

Get rtp timeout.

Definition at line 769 of file rtp.c.

References ast_rtp::rtptimeout.

Referenced by check_rtp_timeout().

00770 {
00771    if (rtp->rtptimeout < 0)   /* We're not checking, but remembering the setting (during T.38 transmission) */
00772       return 0;
00773    return rtp->rtptimeout;
00774 }

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

int ast_rtp_getnat ( struct ast_rtp *  rtp  ) 

Definition at line 805 of file rtp.c.

References ast_test_flag, and FLAG_NAT_ACTIVE.

Referenced by sip_get_rtp_peer().

00806 {
00807    return ast_test_flag(rtp, FLAG_NAT_ACTIVE);
00808 }

void ast_rtp_init ( void   ) 

Initialize the RTP system in Asterisk.

Definition at line 4877 of file rtp.c.

References __ast_rtp_reload(), and ast_cli_register_multiple().

Referenced by main().

04878 {
04879    ast_cli_register_multiple(cli_rtp, sizeof(cli_rtp) / sizeof(struct ast_cli_entry));
04880    __ast_rtp_reload(0);
04881 }

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

References rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, rtp_bridge_lock(), rtp_bridge_unlock(), 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(), add_tcodec_to_sdp(), add_vcodec_to_sdp(), ast_rtp_sendcng(), ast_rtp_senddigit_begin(), ast_rtp_write(), bridge_p2p_rtp_write(), and start_rtp().

02404 {
02405    int pt = 0;
02406 
02407    rtp_bridge_lock(rtp);
02408 
02409    if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
02410       code == rtp->rtp_lookup_code_cache_code) {
02411       /* Use our cached mapping, to avoid the overhead of the loop below */
02412       pt = rtp->rtp_lookup_code_cache_result;
02413       rtp_bridge_unlock(rtp);
02414       return pt;
02415    }
02416 
02417    /* Check the dynamic list first */
02418    for (pt = 0; pt < MAX_RTP_PT; ++pt) {
02419       if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
02420          rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
02421          rtp->rtp_lookup_code_cache_code = code;
02422          rtp->rtp_lookup_code_cache_result = pt;
02423          rtp_bridge_unlock(rtp);
02424          return pt;
02425       }
02426    }
02427 
02428    /* Then the static list */
02429    for (pt = 0; pt < MAX_RTP_PT; ++pt) {
02430       if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
02431          rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
02432          rtp->rtp_lookup_code_cache_code = code;
02433          rtp->rtp_lookup_code_cache_result = pt;
02434          rtp_bridge_unlock(rtp);
02435          return pt;
02436       }
02437    }
02438 
02439    rtp_bridge_unlock(rtp);
02440 
02441    return -1;
02442 }

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

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

Referenced by process_sdp().

02478 {
02479    int format;
02480    unsigned len;
02481    char *end = buf;
02482    char *start = buf;
02483 
02484    if (!buf || !size)
02485       return NULL;
02486 
02487    snprintf(end, size, "0x%x (", capability);
02488 
02489    len = strlen(end);
02490    end += len;
02491    size -= len;
02492    start = end;
02493 
02494    for (format = 1; format < AST_RTP_MAX; format <<= 1) {
02495       if (capability & format) {
02496          const char *name = ast_rtp_lookup_mime_subtype(isAstFormat, format, options);
02497 
02498          snprintf(end, size, "%s|", name);
02499          len = strlen(end);
02500          end += len;
02501          size -= len;
02502       }
02503    }
02504 
02505    if (start == end)
02506       ast_copy_string(start, "nothing)", size); 
02507    else if (size > 1)
02508       *(end -1) = ')';
02509    
02510    return buf;
02511 }

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

References ARRAY_LEN, AST_FORMAT_G726_AAL2, AST_RTP_OPT_G726_NONSTANDARD, rtpPayloadType::isAstFormat, mimeTypes, mimeType::payloadType, and mimeType::subtype.

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

02446 {
02447    unsigned int i;
02448 
02449    for (i = 0; i < ARRAY_LEN(mimeTypes); ++i) {
02450       if ((mimeTypes[i].payloadType.code == code) && (mimeTypes[i].payloadType.isAstFormat == isAstFormat)) {
02451          if (isAstFormat &&
02452              (code == AST_FORMAT_G726_AAL2) &&
02453              (options & AST_RTP_OPT_G726_NONSTANDARD))
02454             return "G726-32";
02455          else
02456             return mimeTypes[i].subtype;
02457       }
02458    }
02459 
02460    return "";
02461 }

struct rtpPayloadType ast_rtp_lookup_pt ( struct ast_rtp *  rtp,
int  pt 
) [read]

Mapping between RTP payload format codes and Asterisk codes:.

Definition at line 2381 of file rtp.c.

References rtpPayloadType::code, rtpPayloadType::isAstFormat, MAX_RTP_PT, rtp_bridge_lock(), and rtp_bridge_unlock().

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

02382 {
02383    struct rtpPayloadType result;
02384 
02385    result.isAstFormat = result.code = 0;
02386 
02387    if (pt < 0 || pt >= MAX_RTP_PT) 
02388       return result; /* bogus payload type */
02389 
02390    /* Start with negotiated codecs */
02391    rtp_bridge_lock(rtp);
02392    result = rtp->current_RTP_PT[pt];
02393    rtp_bridge_unlock(rtp);
02394 
02395    /* If it doesn't exist, check our static RTP type list, just in case */
02396    if (!result.code) 
02397       result = static_RTP_PT[pt];
02398 
02399    return result;
02400 }

unsigned int ast_rtp_lookup_sample_rate ( int  isAstFormat,
int  code 
)

Get the sample rate associated with known RTP payload types.

Parameters:
isAstFormat True if the value in the 'code' parameter is an AST_FORMAT value
code Format code, either from AST_FORMAT list or from AST_RTP list
Returns:
the sample rate if the format was found, zero if it was not found

Definition at line 2463 of file rtp.c.

References ARRAY_LEN, rtpPayloadType::isAstFormat, mimeTypes, mimeType::payloadType, and mimeType::sample_rate.

Referenced by add_codec_to_sdp(), add_noncodec_to_sdp(), add_tcodec_to_sdp(), and add_vcodec_to_sdp().

02464 {
02465    unsigned int i;
02466 
02467    for (i = 0; i < ARRAY_LEN(mimeTypes); ++i) {
02468       if ((mimeTypes[i].payloadType.code == code) && (mimeTypes[i].payloadType.isAstFormat == isAstFormat)) {
02469          return mimeTypes[i].sample_rate;
02470       }
02471    }
02472 
02473    return 0;
02474 }

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

Definition at line 2200 of file rtp.c.

References ast_channel_lock, ast_channel_trylock, ast_channel_unlock, ast_debug, 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_trtp_info, ast_rtp_protocol::get_vrtp_info, LOG_WARNING, ast_channel::name, and ast_rtp_protocol::set_rtp_peer.

Referenced by dial_exec_full(), and do_forward().

02201 {
02202    struct ast_rtp *destp = NULL, *srcp = NULL;     /* Audio RTP Channels */
02203    struct ast_rtp *vdestp = NULL, *vsrcp = NULL;      /* Video RTP channels */
02204    struct ast_rtp *tdestp = NULL, *tsrcp = NULL;      /* Text RTP channels */
02205    struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
02206    enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED, text_dest_res = AST_RTP_GET_FAILED;
02207    enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED, text_src_res = AST_RTP_GET_FAILED; 
02208    int srccodec, destcodec;
02209 
02210    /* Lock channels */
02211    ast_channel_lock(dest);
02212    while (ast_channel_trylock(src)) {
02213       ast_channel_unlock(dest);
02214       usleep(1);
02215       ast_channel_lock(dest);
02216    }
02217 
02218    /* Find channel driver interfaces */
02219    if (!(destpr = get_proto(dest))) {
02220       ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", dest->name);
02221       ast_channel_unlock(dest);
02222       ast_channel_unlock(src);
02223       return 0;
02224    }
02225    if (!(srcpr = get_proto(src))) {
02226       ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", src->name);
02227       ast_channel_unlock(dest);
02228       ast_channel_unlock(src);
02229       return 0;
02230    }
02231 
02232    /* Get audio and video interface (if native bridge is possible) */
02233    audio_dest_res = destpr->get_rtp_info(dest, &destp);
02234    video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
02235    text_dest_res = destpr->get_trtp_info ? destpr->get_trtp_info(dest, &tdestp) : AST_RTP_GET_FAILED;
02236    audio_src_res = srcpr->get_rtp_info(src, &srcp);
02237    video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
02238    text_src_res = srcpr->get_trtp_info ? srcpr->get_trtp_info(src, &tsrcp) : AST_RTP_GET_FAILED;
02239 
02240    /* Ensure we have at least one matching codec */
02241    if (srcpr->get_codec)
02242       srccodec = srcpr->get_codec(src);
02243    else
02244       srccodec = 0;
02245    if (destpr->get_codec)
02246       destcodec = destpr->get_codec(dest);
02247    else
02248       destcodec = 0;
02249 
02250    /* Check if bridge is still possible (In SIP directmedia=no stops this, like NAT) */
02251    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)) {
02252       /* Somebody doesn't want to play... */
02253       ast_channel_unlock(dest);
02254       ast_channel_unlock(src);
02255       return 0;
02256    }
02257    ast_rtp_pt_copy(destp, srcp);
02258    if (vdestp && vsrcp)
02259       ast_rtp_pt_copy(vdestp, vsrcp);
02260    if (tdestp && tsrcp)
02261       ast_rtp_pt_copy(tdestp, tsrcp);
02262    if (media) {
02263       /* Bridge early */
02264       if (destpr->set_rtp_peer(dest, srcp, vsrcp, tsrcp, srccodec, ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
02265          ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src->name);
02266    }
02267    ast_channel_unlock(dest);
02268    ast_channel_unlock(src);
02269    ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n", dest->name, src->name);
02270    return 1;
02271 }

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

Initializate a RTP session.

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

Definition at line 2671 of file rtp.c.

References ast_rtp_new_with_bindaddr().

02672 {
02673    struct in_addr ia;
02674 
02675    memset(&ia, 0, sizeof(ia));
02676    return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
02677 }

void ast_rtp_new_init ( struct ast_rtp *  rtp  ) 

Initialize a new RTP structure.

reload rtp configuration

Definition at line 2562 of file rtp.c.

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

Referenced by ast_rtp_new_with_bindaddr(), and process_sdp().

02563 {
02564 #ifdef P2P_INTENSE
02565    ast_mutex_init(&rtp->bridge_lock);
02566 #endif
02567 
02568    rtp->them.sin_family = AF_INET;
02569    rtp->us.sin_family = AF_INET;
02570    rtp->ssrc = ast_random();
02571    rtp->seqno = ast_random() & 0xffff;
02572    ast_set_flag(rtp, FLAG_HAS_DTMF);
02573    rtp->strict_rtp_state = (strictrtp ? STRICT_RTP_LEARN : STRICT_RTP_OPEN);
02574 }

void ast_rtp_new_source ( struct ast_rtp *  rtp  ) 

Indicate that we need to set the marker bit.

Definition at line 2684 of file rtp.c.

References ast_debug, and ast_rtp::set_marker_bit.

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

02685 {
02686    if (rtp) {
02687       rtp->set_marker_bit = 1;
02688       ast_debug(3, "Setting the marker bit due to a source update\n");
02689    }
02690 }

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

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

References ast_calloc, ast_free, ast_io_add(), AST_IO_IN, ast_log(), ast_random(), ast_rtcp_new(), ast_rtp_new_init(), ast_rtp_pt_default(), ast_set_flag, errno, FLAG_CALLBACK_MODE, ast_rtp::io, ast_rtp::ioid, LOG_ERROR, ast_rtp::rtcp, rtp_socket(), rtpread(), ast_rtcp::s, ast_rtp::s, ast_rtp::sched, ast_rtcp::us, and ast_rtp::us.

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

02577 {
02578    struct ast_rtp *rtp;
02579    int x;
02580    int startplace;
02581    
02582    if (!(rtp = ast_calloc(1, sizeof(*rtp))))
02583       return NULL;
02584 
02585    ast_rtp_new_init(rtp);
02586 
02587    rtp->s = rtp_socket("RTP");
02588    if (rtp->s < 0)
02589       goto fail;
02590    if (sched && rtcpenable) {
02591       rtp->sched = sched;
02592       rtp->rtcp = ast_rtcp_new();
02593    }
02594    
02595    /*
02596     * Try to bind the RTP port, x, and possibly the RTCP port, x+1 as well.
02597     * Start from a random (even, by RTP spec) port number, and
02598     * iterate until success or no ports are available.
02599     * Note that the requirement of RTP port being even, or RTCP being the
02600     * next one, cannot be enforced in presence of a NAT box because the
02601     * mapping is not under our control.
02602     */
02603    x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
02604    x = x & ~1;    /* make it an even number */
02605    startplace = x;      /* remember the starting point */
02606    /* this is constant across the loop */
02607    rtp->us.sin_addr = addr;
02608    if (rtp->rtcp)
02609       rtp->rtcp->us.sin_addr = addr;
02610    for (;;) {
02611       rtp->us.sin_port = htons(x);
02612       if (!bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) {
02613          /* bind succeeded, if no rtcp then we are done */
02614          if (!rtp->rtcp)
02615             break;
02616          /* have rtcp, try to bind it */
02617          rtp->rtcp->us.sin_port = htons(x + 1);
02618          if (!bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us)))
02619             break;   /* success again, we are really done */
02620          /*
02621           * RTCP bind failed, so close and recreate the
02622           * already bound RTP socket for the next round.
02623           */
02624          close(rtp->s);
02625          rtp->s = rtp_socket("RTP");
02626          if (rtp->s < 0)
02627             goto fail;
02628       }
02629       /*
02630        * If we get here, there was an error in one of the bind()
02631        * calls, so make sure it is nothing unexpected.
02632        */
02633       if (errno != EADDRINUSE) {
02634          /* We got an error that wasn't expected, abort! */
02635          ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
02636          goto fail;
02637       }
02638       /*
02639        * One of the ports is in use. For the next iteration,
02640        * increment by two and handle wraparound.
02641        * If we reach the starting point, then declare failure.
02642        */
02643       x += 2;
02644       if (x > rtpend)
02645          x = (rtpstart + 1) & ~1;
02646       if (x == startplace) {
02647          ast_log(LOG_ERROR, "No RTP ports remaining. Can't setup media stream for this call.\n");
02648          goto fail;
02649       }
02650    }
02651    rtp->sched = sched;
02652    rtp->io = io;
02653    if (callbackmode) {
02654       rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
02655       ast_set_flag(rtp, FLAG_CALLBACK_MODE);
02656    }
02657    ast_rtp_pt_default(rtp);
02658    return rtp;
02659 
02660 fail:
02661    if (rtp->s >= 0)
02662       close(rtp->s);
02663    if (rtp->rtcp) {
02664       close(rtp->rtcp->s);
02665       ast_free(rtp->rtcp);
02666    }
02667    ast_free(rtp);
02668    return NULL;
02669 }

int ast_rtp_proto_register ( struct ast_rtp_protocol *  proto  ) 

Register an RTP channel client.

Definition at line 3954 of file rtp.c.

References ast_log(), AST_RWLIST_INSERT_HEAD, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_rtp_protocol::list, LOG_WARNING, and ast_rtp_protocol::type.

Referenced by load_module().

03955 {
03956    struct ast_rtp_protocol *cur;
03957 
03958    AST_RWLIST_WRLOCK(&protos);
03959    AST_RWLIST_TRAVERSE(&protos, cur, list) { 
03960       if (!strcmp(cur->type, proto->type)) {
03961          ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
03962          AST_RWLIST_UNLOCK(&protos);
03963          return -1;
03964       }
03965    }
03966    AST_RWLIST_INSERT_HEAD(&protos, proto, list);
03967    AST_RWLIST_UNLOCK(&protos);
03968    
03969    return 0;
03970 }

void ast_rtp_proto_unregister ( struct ast_rtp_protocol *  proto  ) 

Unregister an RTP channel client.

Definition at line 3946 of file rtp.c.

References AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

Referenced by load_module(), and unload_module().

03947 {
03948    AST_RWLIST_WRLOCK(&protos);
03949    AST_RWLIST_REMOVE(&protos, proto, list);
03950    AST_RWLIST_UNLOCK(&protos);
03951 }

void ast_rtp_pt_clear ( struct ast_rtp *  rtp  ) 

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

Definition at line 2038 of file rtp.c.

References rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, rtp_bridge_lock(), rtp_bridge_unlock(), 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().

02039 {
02040    int i;
02041 
02042    if (!rtp)
02043       return;
02044 
02045    rtp_bridge_lock(rtp);
02046 
02047    for (i = 0; i < MAX_RTP_PT; ++i) {
02048       rtp->current_RTP_PT[i].isAstFormat = 0;
02049       rtp->current_RTP_PT[i].code = 0;
02050    }
02051 
02052    rtp->rtp_lookup_code_cache_isAstFormat = 0;
02053    rtp->rtp_lookup_code_cache_code = 0;
02054    rtp->rtp_lookup_code_cache_result = 0;
02055 
02056    rtp_bridge_unlock(rtp);
02057 }

void ast_rtp_pt_copy ( struct ast_rtp *  dest,
struct ast_rtp *  src 
)

Copy payload types between RTP structures.

Definition at line 2078 of file rtp.c.

References rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, rtp_bridge_lock(), rtp_bridge_unlock(), 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().

02079 {
02080    unsigned int i;
02081 
02082    rtp_bridge_lock(dest);
02083    rtp_bridge_lock(src);
02084 
02085    for (i = 0; i < MAX_RTP_PT; ++i) {
02086       dest->current_RTP_PT[i].isAstFormat = 
02087          src->current_RTP_PT[i].isAstFormat;
02088       dest->current_RTP_PT[i].code = 
02089          src->current_RTP_PT[i].code; 
02090    }
02091    dest->rtp_lookup_code_cache_isAstFormat = 0;
02092    dest->rtp_lookup_code_cache_code = 0;
02093    dest->rtp_lookup_code_cache_result = 0;
02094 
02095    rtp_bridge_unlock(src);
02096    rtp_bridge_unlock(dest);
02097 }

void ast_rtp_pt_default ( struct ast_rtp *  rtp  ) 

Set payload types to defaults.

Definition at line 2059 of file rtp.c.

References rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, rtp_bridge_lock(), rtp_bridge_unlock(), 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_new_with_bindaddr().

02060 {
02061    int i;
02062 
02063    rtp_bridge_lock(rtp);
02064 
02065    /* Initialize to default payload types */
02066    for (i = 0; i < MAX_RTP_PT; ++i) {
02067       rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
02068       rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
02069    }
02070 
02071    rtp->rtp_lookup_code_cache_isAstFormat = 0;
02072    rtp->rtp_lookup_code_cache_code = 0;
02073    rtp->rtp_lookup_code_cache_result = 0;
02074 
02075    rtp_bridge_unlock(rtp);
02076 }

struct ast_frame* ast_rtp_read ( struct ast_rtp *  rtp  )  [read]

Definition at line 1576 of file rtp.c.

References ast_rtp::altthem, ast_assert, ast_codec_get_samples(), AST_CONTROL_SRCCHANGE, ast_debug, AST_FORMAT_AUDIO_MASK, ast_format_rate(), AST_FORMAT_SLINEAR, AST_FORMAT_T140, AST_FORMAT_T140RED, AST_FORMAT_VIDEO_MASK, ast_frame_byteswap_be, AST_FRAME_CONTROL, AST_FRAME_DTMF_END, AST_FRAME_TEXT, AST_FRAME_VIDEO, AST_FRAME_VOICE, AST_FRFLAG_HAS_TIMING_INFO, AST_FRIENDLY_OFFSET, ast_frisolate(), ast_inet_ntoa(), AST_LIST_EMPTY, AST_LIST_FIRST, AST_LIST_HEAD_INIT_NOLOCK, AST_LIST_INSERT_TAIL, 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, create_dtmf_frame(), 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::lastitexttimestamp, ast_rtp::lastividtimestamp, ast_rtp::lastrxformat, ast_rtp::lastrxseqno, ast_rtp::lastrxts, ast_frame::len, len(), LOG_NOTICE, LOG_WARNING, ast_frame::mallocd, ast_rtp::nat, ast_frame::offset, option_debug, process_cisco_dtmf(), process_rfc2833(), process_rfc3389(), ast_frame::ptr, 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_rtp::s, ast_frame::samples, ast_rtp::sched, ast_rtcp::schedid, ast_rtp::seedrxseqno, ast_rtp::sending_digit, ast_frame::seqno, ast_frame::src, ast_rtp::strict_rtp_address, STRICT_RTP_CLOSED, STRICT_RTP_LEARN, ast_rtp::strict_rtp_state, STUN_ACCEPT, stun_handle_packet(), ast_frame::subclass, ast_rtcp::them, ast_rtp::them, ast_rtp::themssrc, ast_frame::ts, and version.

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

01577 {
01578    int res;
01579    struct sockaddr_in sock_in;
01580    socklen_t len;
01581    unsigned int seqno;
01582    int version;
01583    int payloadtype;
01584    int hdrlen = 12;
01585    int padding;
01586    int mark;
01587    int ext;
01588    int cc;
01589    unsigned int ssrc;
01590    unsigned int timestamp;
01591    unsigned int *rtpheader;
01592    struct rtpPayloadType rtpPT;
01593    struct ast_rtp *bridged = NULL;
01594    int prev_seqno;
01595    struct frame_list frames;
01596    
01597    /* If time is up, kill it */
01598    if (rtp->sending_digit)
01599       ast_rtp_senddigit_continuation(rtp);
01600 
01601    len = sizeof(sock_in);
01602    
01603    /* Cache where the header will go */
01604    res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
01605                0, (struct sockaddr *)&sock_in, &len);
01606 
01607    /* If strict RTP protection is enabled see if we need to learn this address or if the packet should be dropped */
01608    if (rtp->strict_rtp_state == STRICT_RTP_LEARN) {
01609       /* Copy over address that this packet was received on */
01610       memcpy(&rtp->strict_rtp_address, &sock_in, sizeof(rtp->strict_rtp_address));
01611       /* Now move over to actually protecting the RTP port */
01612       rtp->strict_rtp_state = STRICT_RTP_CLOSED;
01613       ast_debug(1, "Learned remote address is %s:%d for strict RTP purposes, now protecting the port.\n", ast_inet_ntoa(rtp->strict_rtp_address.sin_addr), ntohs(rtp->strict_rtp_address.sin_port));
01614    } else if (rtp->strict_rtp_state == STRICT_RTP_CLOSED) {
01615       /* If the address we previously learned doesn't match the address this packet came in on simply drop it */
01616       if ((rtp->strict_rtp_address.sin_addr.s_addr != sock_in.sin_addr.s_addr) || (rtp->strict_rtp_address.sin_port != sock_in.sin_port)) {
01617          ast_debug(1, "Received RTP packet from %s:%d, dropping due to strict RTP protection. Expected it to be from %s:%d\n", ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port), ast_inet_ntoa(rtp->strict_rtp_address.sin_addr), ntohs(rtp->strict_rtp_address.sin_port));
01618          return &ast_null_frame;
01619       }
01620    }
01621 
01622    rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
01623    if (res < 0) {
01624       ast_assert(errno != EBADF);
01625       if (errno != EAGAIN) {
01626          ast_log(LOG_WARNING, "RTP Read error: %s.  Hanging up.\n", strerror(errno));
01627          return NULL;
01628       }
01629       return &ast_null_frame;
01630    }
01631    
01632    if (res < hdrlen) {
01633       ast_log(LOG_WARNING, "RTP Read too short\n");
01634       return &ast_null_frame;
01635    }
01636 
01637    /* Get fields */
01638    seqno = ntohl(rtpheader[0]);
01639 
01640    /* Check RTP version */
01641    version = (seqno & 0xC0000000) >> 30;
01642    if (!version) {
01643       /* If the two high bits are 0, this might be a
01644        * STUN message, so process it. stun_handle_packet()
01645        * answers to requests, and it returns STUN_ACCEPT
01646        * if the request is valid.
01647        */
01648       if ((stun_handle_packet(rtp->s, &sock_in, rtp->rawdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == STUN_ACCEPT) &&
01649          (!rtp->them.sin_port && !rtp->them.sin_addr.s_addr)) {
01650          memcpy(&rtp->them, &sock_in, sizeof(rtp->them));
01651       }
01652       return &ast_null_frame;
01653    }
01654 
01655 #if 0 /* Allow to receive RTP stream with closed transmission path */
01656    /* If we don't have the other side's address, then ignore this */
01657    if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
01658       return &ast_null_frame;
01659 #endif
01660 
01661    /* Send to whoever send to us if NAT is turned on */
01662    if (rtp->nat) {
01663       if (((rtp->them.sin_addr.s_addr != sock_in.sin_addr.s_addr) ||
01664           (rtp->them.sin_port != sock_in.sin_port)) && 
01665           ((rtp->altthem.sin_addr.s_addr != sock_in.sin_addr.s_addr) ||
01666           (rtp->altthem.sin_port != sock_in.sin_port))) {
01667          rtp->them = sock_in;
01668          if (rtp->rtcp) {
01669             int h = 0;
01670             memcpy(&rtp->rtcp->them, &sock_in, sizeof(rtp->rtcp->them));
01671             h = ntohs(rtp->them.sin_port);
01672             rtp->rtcp->them.sin_port = htons(h + 1);
01673          }
01674          rtp->rxseqno = 0;
01675          ast_set_flag(rtp, FLAG_NAT_ACTIVE);
01676          if (option_debug || rtpdebug)
01677             ast_debug(0, "RTP NAT: Got audio from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
01678       }
01679    }
01680 
01681    /* If we are bridged to another RTP stream, send direct */
01682    if ((bridged = ast_rtp_get_bridged(rtp)) && !bridge_p2p_rtp_write(rtp, bridged, rtpheader, res, hdrlen))
01683       return &ast_null_frame;
01684 
01685    if (version != 2)
01686       return &ast_null_frame;
01687 
01688    payloadtype = (seqno & 0x7f0000) >> 16;
01689    padding = seqno & (1 << 29);
01690    mark = seqno & (1 << 23);
01691    ext = seqno & (1 << 28);
01692    cc = (seqno & 0xF000000) >> 24;
01693    seqno &= 0xffff;
01694    timestamp = ntohl(rtpheader[1]);
01695    ssrc = ntohl(rtpheader[2]);
01696    
01697    AST_LIST_HEAD_INIT_NOLOCK(&frames);
01698    /* Force a marker bit and change SSRC if the SSRC changes */
01699    if (rtp->rxssrc && rtp->rxssrc != ssrc) {
01700       struct ast_frame *f, srcupdate = {
01701          AST_FRAME_CONTROL,
01702          .subclass = AST_CONTROL_SRCCHANGE,
01703       };
01704 
01705       if (!mark) {
01706          if (option_debug || rtpdebug) {
01707             ast_debug(0, "Forcing Marker bit, because SSRC has changed\n");
01708          }
01709          mark = 1;
01710       }
01711       f = ast_frisolate(&srcupdate);
01712       AST_LIST_INSERT_TAIL(&frames, f, frame_list);
01713    }
01714 
01715    rtp->rxssrc = ssrc;
01716    
01717    if (padding) {
01718       /* Remove padding bytes */
01719       res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
01720    }
01721    
01722    if (cc) {
01723       /* CSRC fields present */
01724       hdrlen += cc*4;
01725    }
01726 
01727    if (ext) {
01728       /* RTP Extension present */
01729       hdrlen += (ntohl(rtpheader[hdrlen/4]) & 0xffff) << 2;
01730       hdrlen += 4;
01731       if (option_debug) {
01732          int profile;
01733          profile = (ntohl(rtpheader[3]) & 0xffff0000) >> 16;
01734          if (profile == 0x505a)
01735             ast_debug(1, "Found Zfone extension in RTP stream - zrtp - not supported.\n");
01736          else
01737             ast_debug(1, "Found unknown RTP Extensions %x\n", profile);
01738       }
01739    }
01740 
01741    if (res < hdrlen) {
01742       ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
01743       return AST_LIST_FIRST(&frames) ? AST_LIST_FIRST(&frames) : &ast_null_frame;
01744    }
01745 
01746    rtp->rxcount++; /* Only count reasonably valid packets, this'll make the rtcp stats more accurate */
01747 
01748    if (rtp->rxcount==1) {
01749       /* This is the first RTP packet successfully received from source */
01750       rtp->seedrxseqno = seqno;
01751    }
01752 
01753    /* Do not schedule RR if RTCP isn't run */
01754    if (rtp->rtcp && rtp->rtcp->them.sin_addr.s_addr && rtp->rtcp->schedid < 1) {
01755       /* Schedule transmission of Receiver Report */
01756       rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
01757    }
01758    if ((int)rtp->lastrxseqno - (int)seqno  > 100) /* if so it would indicate that the sender cycled; allow for misordering */
01759       rtp->cycles += RTP_SEQ_MOD;
01760    
01761    prev_seqno = rtp->lastrxseqno;
01762 
01763    rtp->lastrxseqno = seqno;
01764    
01765    if (!rtp->themssrc)
01766       rtp->themssrc = ntohl(rtpheader[2]); /* Record their SSRC to put in future RR */
01767    
01768    if (rtp_debug_test_addr(&sock_in))
01769       ast_verbose("Got  RTP packet from    %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
01770          ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
01771 
01772    rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
01773    if (!rtpPT.isAstFormat) {
01774       struct ast_frame *f = NULL;
01775 
01776       /* This is special in-band data that's not one of our codecs */
01777       if (rtpPT.code == AST_RTP_DTMF) {
01778          /* It's special -- rfc2833 process it */
01779          if (rtp_debug_test_addr(&sock_in)) {
01780             unsigned char *data;
01781             unsigned int event;
01782             unsigned int event_end;
01783             unsigned int duration;
01784             data = rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen;
01785             event = ntohl(*((unsigned int *)(data)));
01786             event >>= 24;
01787             event_end = ntohl(*((unsigned int *)(data)));
01788             event_end <<= 8;
01789             event_end >>= 24;
01790             duration = ntohl(*((unsigned int *)(data)));
01791             duration &= 0xFFFF;
01792             ast_verbose("Got  RTP RFC2833 from   %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u, mark %d, event %08x, end %d, duration %-5.5d) \n", ast_inet_ntoa(sock_in.sin_addr), ntohs(sock_in.sin_port), payloadtype, seqno, timestamp, res - hdrlen, (mark?1:0), event, ((event_end & 0x80)?1:0), duration);
01793          }
01794          /* process_rfc2833 may need to return multiple frames. We do this
01795           * by passing the pointer to the frame list to it so that the method
01796           * can append frames to the list as needed
01797           */
01798          process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &frames);
01799       } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
01800          /* It's really special -- process it the Cisco way */
01801          if (rtp->lastevent <= seqno || (rtp->lastevent >= 65530 && seqno <= 6)) {
01802             f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
01803             rtp->lastevent = seqno;
01804          }
01805       } else if (rtpPT.code == AST_RTP_CN) {
01806          /* Comfort Noise */
01807          f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
01808       } else {
01809          ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n", payloadtype, ast_inet_ntoa(rtp->them.sin_addr));
01810       }
01811       if (f) {
01812          AST_LIST_INSERT_TAIL(&frames, f, frame_list);
01813       }
01814       /* Even if no frame was returned by one of the above methods,
01815        * we may have a frame to return in our frame list
01816        */
01817       if (!AST_LIST_EMPTY(&frames)) {
01818          return AST_LIST_FIRST(&frames);
01819       }
01820       return &ast_null_frame;
01821    }
01822    rtp->lastrxformat = rtp->f.subclass = rtpPT.code;
01823    rtp->f.frametype = (rtp->f.subclass & AST_FORMAT_AUDIO_MASK) ? AST_FRAME_VOICE : (rtp->f.subclass & AST_FORMAT_VIDEO_MASK) ? AST_FRAME_VIDEO : AST_FRAME_TEXT;
01824 
01825    rtp->rxseqno = seqno;
01826 
01827    if (rtp->dtmf_timeout && rtp->dtmf_timeout < timestamp) {
01828       rtp->dtmf_timeout = 0;
01829 
01830       if (rtp->resp) {
01831          struct ast_frame *f;
01832          f = create_dtmf_frame(rtp, AST_FRAME_DTMF_END);
01833          f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(f->subclass)), ast_tv(0, 0));
01834          rtp->resp = 0;
01835          rtp->dtmf_timeout = rtp->dtmf_duration = 0;
01836          AST_LIST_INSERT_TAIL(&frames, f, frame_list);
01837          return AST_LIST_FIRST(&frames);
01838       }
01839    }
01840 
01841    /* Record received timestamp as last received now */
01842    rtp->lastrxts = timestamp;
01843 
01844    rtp->f.mallocd = 0;
01845    rtp->f.datalen = res - hdrlen;
01846    rtp->f.data.ptr = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
01847    rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
01848    rtp->f.seqno = seqno;
01849 
01850    if (rtp->f.subclass == AST_FORMAT_T140 && (int)seqno - (prev_seqno+1) > 0 && (int)seqno - (prev_seqno+1) < 10) {
01851         unsigned char *data = rtp->f.data.ptr;
01852         
01853         memmove(rtp->f.data.ptr+3, rtp->f.data.ptr, rtp->f.datalen);
01854         rtp->f.datalen +=3;
01855         *data++ = 0xEF;
01856         *data++ = 0xBF;
01857         *data = 0xBD;
01858    }
01859  
01860    if (rtp->f.subclass == AST_FORMAT_T140RED) {
01861       unsigned char *data = rtp->f.data.ptr;
01862       unsigned char *header_end;
01863       int num_generations;
01864       int header_length;
01865       int length;
01866       int diff =(int)seqno - (prev_seqno+1); /* if diff = 0, no drop*/
01867       int x;
01868 
01869       rtp->f.subclass = AST_FORMAT_T140;
01870       header_end = memchr(data, ((*data) & 0x7f), rtp->f.datalen);
01871       if (header_end == NULL) {
01872          return &ast_null_frame;
01873       }
01874       header_end++;
01875       
01876       header_length = header_end - data;
01877       num_generations = header_length / 4;
01878       length = header_length;
01879 
01880       if (!diff) {
01881          for (x = 0; x < num_generations; x++)
01882             length += data[x * 4 + 3];
01883          
01884          if (!(rtp->f.datalen - length))
01885             return &ast_null_frame;
01886          
01887          rtp->f.data.ptr += length;
01888          rtp->f.datalen -= length;
01889       } else if (diff > num_generations && diff < 10) {
01890          length -= 3;
01891          rtp->f.data.ptr += length;
01892          rtp->f.datalen -= length;
01893          
01894          data = rtp->f.data.ptr;
01895          *data++ = 0xEF;
01896          *data++ = 0xBF;
01897          *data = 0xBD;
01898       } else   {
01899          for ( x = 0; x < num_generations - diff; x++) 
01900             length += data[x * 4 + 3];
01901          
01902          rtp->f.data.ptr += length;
01903          rtp->f.datalen -= length;
01904       }
01905    }
01906 
01907    if (rtp->f.subclass & AST_FORMAT_AUDIO_MASK) {
01908       rtp->f.samples = ast_codec_get_samples(&rtp->f);
01909       if (rtp->f.subclass == AST_FORMAT_SLINEAR) 
01910          ast_frame_byteswap_be(&rtp->f);
01911       calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
01912       /* Add timing data to let ast_generic_bridge() put the frame into a jitterbuf */
01913       ast_set_flag(&rtp->f, AST_FRFLAG_HAS_TIMING_INFO);
01914       rtp->f.ts = timestamp / (rtp_get_rate(rtp->f.subclass) / 1000);
01915       rtp->f.len = rtp->f.samples / ((ast_format_rate(rtp->f.subclass) / 1000));
01916    } else if (rtp->f.subclass & AST_FORMAT_VIDEO_MASK) {
01917       /* Video -- samples is # of samples vs. 90000 */
01918       if (!rtp->lastividtimestamp)
01919          rtp->lastividtimestamp = timestamp;
01920       rtp->f.samples = timestamp - rtp->lastividtimestamp;
01921       rtp->lastividtimestamp = timestamp;
01922       rtp->f.delivery.tv_sec = 0;
01923       rtp->f.delivery.tv_usec = 0;
01924       /* Pass the RTP marker bit as bit 0 in the subclass field.
01925        * This is ok because subclass is actually a bitmask, and
01926        * the low bits represent audio formats, that are not
01927        * involved here since we deal with video.
01928        */
01929       if (mark)
01930          rtp->f.subclass |= 0x1;
01931    } else {
01932       /* TEXT -- samples is # of samples vs. 1000 */
01933       if (!rtp->lastitexttimestamp)
01934          rtp->lastitexttimestamp = timestamp;
01935       rtp->f.samples = timestamp - rtp->lastitexttimestamp;
01936       rtp->lastitexttimestamp = timestamp;
01937       rtp->f.delivery.tv_sec = 0;
01938       rtp->f.delivery.tv_usec = 0;
01939    }
01940    rtp->f.src = "RTP";
01941 
01942    AST_LIST_INSERT_TAIL(&frames, &rtp->f, frame_list);
01943    return AST_LIST_FIRST(&frames);
01944 }

int ast_rtp_reload ( void   ) 

Initialize RTP subsystem

Definition at line 4871 of file rtp.c.

References __ast_rtp_reload().

04872 {
04873    return __ast_rtp_reload(1);
04874 }

void ast_rtp_reset ( struct ast_rtp *  rtp  ) 

Definition at line 2778 of file rtp.c.

References ast_rtp::dtmf_timeout, ast_rtp::dtmfmute, ast_rtp::dtmfsamples, ast_rtp::lastdigitts, ast_rtp::lastevent, ast_rtp::lasteventseqn, ast_rtp::lastitexttimestamp, ast_rtp::lastividtimestamp, ast_rtp::lastotexttimestamp, 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.

02779 {
02780    memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
02781    memset(&rtp->txcore, 0, sizeof(rtp->txcore));
02782    memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
02783    rtp->lastts = 0;
02784    rtp->lastdigitts = 0;
02785    rtp->lastrxts = 0;
02786    rtp->lastividtimestamp = 0;
02787    rtp->lastovidtimestamp = 0;
02788    rtp->lastitexttimestamp = 0;
02789    rtp->lastotexttimestamp = 0;
02790    rtp->lasteventseqn = 0;
02791    rtp->lastevent = 0;
02792    rtp->lasttxformat = 0;
02793    rtp->lastrxformat = 0;
02794    rtp->dtmf_timeout = 0;
02795    rtp->dtmfsamples = 0;
02796    rtp->seqno = 0;
02797    rtp->rxseqno = 0;
02798 }

int ast_rtp_sendcng ( struct ast_rtp *  rtp,
int  level 
)

generate comfort noice (CNG)

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

03633 {
03634    unsigned int *rtpheader;
03635    int hdrlen = 12;
03636    int res;
03637    int payload;
03638    char data[256];
03639    level = 127 - (level & 0x7f);
03640    payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_CN);
03641 
03642    /* If we have no peer, return immediately */ 
03643    if (!rtp->them.sin_addr.s_addr)
03644       return 0;
03645 
03646    rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
03647 
03648    /* Get a pointer to the header */
03649    rtpheader = (unsigned int *)data;
03650    rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
03651    rtpheader[1] = htonl(rtp->lastts);
03652    rtpheader[2] = htonl(rtp->ssrc); 
03653    data[12] = level;
03654    if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
03655       res = sendto(rtp->s, (void *)rtpheader, hdrlen + 1, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
03656       if (res <0) 
03657          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));
03658       if (rtp_debug_test_addr(&rtp->them))
03659          ast_verbose("Sent Comfort Noise RTP packet to %s:%u (type %d, seq %u, ts %u, len %d)\n"
03660                , ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);         
03661          
03662    }
03663    return 0;
03664 }

int ast_rtp_senddigit_begin ( struct ast_rtp *  rtp,
char  digit 
)

Send begin frames for DTMF.

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

03189 {
03190    unsigned int *rtpheader;
03191    int hdrlen = 12, res = 0, i = 0, payload = 0;
03192    char data[256];
03193 
03194    if ((digit <= '9') && (digit >= '0'))
03195       digit -= '0';
03196    else if (digit == '*')
03197       digit = 10;
03198    else if (digit == '#')
03199       digit = 11;
03200    else if ((digit >= 'A') && (digit <= 'D'))
03201       digit = digit - 'A' + 12;
03202    else if ((digit >= 'a') && (digit <= 'd'))
03203       digit = digit - 'a' + 12;
03204    else {
03205       ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
03206       return 0;
03207    }
03208 
03209    /* If we have no peer, return immediately */ 
03210    if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
03211       return 0;
03212 
03213    payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
03214 
03215    rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
03216    rtp->send_duration = 160;
03217    rtp->lastdigitts = rtp->lastts + rtp->send_duration;
03218    
03219    /* Get a pointer to the header */
03220    rtpheader = (unsigned int *)data;
03221    rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
03222    rtpheader[1] = htonl(rtp->lastdigitts);
03223    rtpheader[2] = htonl(rtp->ssrc); 
03224 
03225    for (i = 0; i < 2; i++) {
03226       rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
03227       res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
03228       if (res < 0) 
03229          ast_log(LOG_ERROR, "RTP Transmission error to %s:%u: %s\n",
03230             ast_inet_ntoa(rtp->them.sin_addr),
03231             ntohs(rtp->them.sin_port), strerror(errno));
03232       if (rtp_debug_test_addr(&rtp->them))
03233          ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
03234                 ast_inet_ntoa(rtp->them.sin_addr),
03235                 ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
03236       /* Increment sequence number */
03237       rtp->seqno++;
03238       /* Increment duration */
03239       rtp->send_duration += 160;
03240       /* Clear marker bit and set seqno */
03241       rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
03242    }
03243 
03244    /* Since we received a begin, we can safely store the digit and disable any compensation */
03245    rtp->sending_digit = 1;
03246    rtp->send_digit = digit;
03247    rtp->send_payload = payload;
03248 
03249    return 0;
03250 }

int ast_rtp_senddigit_end ( struct ast_rtp *  rtp,
char  digit 
)

Definition at line 3289 of file rtp.c.

References ast_rtp_senddigit_end_with_duration().

Referenced by mgcp_senddigit_end(), and oh323_digit_end().

03290 {
03291    return ast_rtp_senddigit_end_with_duration(rtp, digit, 0);
03292 }

int ast_rtp_senddigit_end_with_duration ( struct ast_rtp *  rtp,
char  digit,
unsigned int  duration 
)

Send end packets for DTMF.

Definition at line 3295 of file rtp.c.

References ast_debug, ast_inet_ntoa(), ast_log(), ast_tv(), ast_tvadd(), ast_tvnow(), ast_verbose, ast_rtp::dtmfmute, errno, ast_rtp::f, ast_rtp::lastdigitts, ast_rtp::lastts, LOG_ERROR, LOG_WARNING, rtp_debug_test_addr(), rtp_get_rate(), 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, ast_frame::subclass, and ast_rtp::them.

Referenced by ast_rtp_senddigit_end(), and sip_senddigit_end().

03296 {
03297    unsigned int *rtpheader;
03298    int hdrlen = 12, res = 0, i = 0;
03299    char data[256];
03300    unsigned int measured_samples;
03301    
03302    /* If no address, then bail out */
03303    if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
03304       return 0;
03305    
03306    if ((digit <= '9') && (digit >= '0'))
03307       digit -= '0';
03308    else if (digit == '*')
03309       digit = 10;
03310    else if (digit == '#')
03311       digit = 11;
03312    else if ((digit >= 'A') && (digit <= 'D'))
03313       digit = digit - 'A' + 12;
03314    else if ((digit >= 'a') && (digit <= 'd'))
03315       digit = digit - 'a' + 12;
03316    else {
03317       ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
03318       return 0;
03319    }
03320 
03321    rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
03322 
03323    if (duration > 0 && (measured_samples = duration * rtp_get_rate(rtp->f.subclass) / 1000) > rtp->send_duration) {
03324       ast_debug(2, "Adjusting final end duration from %u to %u\n", rtp->send_duration, measured_samples);
03325       rtp->send_duration = measured_samples;
03326    }
03327 
03328    rtpheader = (unsigned int *)data;
03329    rtpheader[1] = htonl(rtp->lastdigitts);
03330    rtpheader[2] = htonl(rtp->ssrc);
03331    rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
03332    /* Set end bit */
03333    rtpheader[3] |= htonl((1 << 23));
03334 
03335    /* Send 3 termination packets */
03336    for (i = 0; i < 3; i++) {
03337       rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
03338       res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
03339       rtp->seqno++;
03340       if (res < 0)
03341          ast_log(LOG_ERROR, "RTP Transmission error to %s:%d: %s\n",
03342             ast_inet_ntoa(rtp->them.sin_addr),
03343             ntohs(rtp->them.sin_port), strerror(errno));
03344       if (rtp_debug_test_addr(&rtp->them))
03345          ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
03346                 ast_inet_ntoa(rtp->them.sin_addr),
03347                 ntohs(rtp->them.sin_port), rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
03348    }
03349    rtp->lastts += rtp->send_duration;
03350    rtp->sending_digit = 0;
03351    rtp->send_digit = 0;
03352 
03353    return res;
03354 }

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

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

Referenced by handle_request_invite().

02719 {
02720    rtp->altthem.sin_port = alt->sin_port;
02721    rtp->altthem.sin_addr = alt->sin_addr;
02722    if (rtp->rtcp) {
02723       rtp->rtcp->altthem.sin_port = htons(ntohs(alt->sin_port) + 1);
02724       rtp->rtcp->altthem.sin_addr = alt->sin_addr;
02725    }
02726 }

void ast_rtp_set_callback ( struct ast_rtp *  rtp,
ast_rtp_callback  callback 
)

Definition at line 795 of file rtp.c.

References ast_rtp::callback.

Referenced by start_rtp().

00796 {
00797    rtp->callback = callback;
00798 }

void ast_rtp_set_data ( struct ast_rtp *  rtp,
void *  data 
)

Definition at line 790 of file rtp.c.

References ast_rtp::data.

Referenced by start_rtp().

00791 {
00792    rtp->data = data;
00793 }

void ast_rtp_set_m_type ( struct ast_rtp *  rtp,
int  pt 
)

Activate payload type.

Definition at line 2277 of file rtp.c.

References ast_rtp::current_RTP_PT, MAX_RTP_PT, rtp_bridge_lock(), and rtp_bridge_unlock().

Referenced by gtalk_is_answered(), gtalk_newcall(), jingle_newcall(), and process_sdp().

02278 {
02279    if (pt < 0 || pt >= MAX_RTP_PT || static_RTP_PT[pt].code == 0) 
02280       return; /* bogus payload type */
02281 
02282    rtp_bridge_lock(rtp);
02283    rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
02284    rtp_bridge_unlock(rtp);
02285 } 

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

Definition at line 2703 of file rtp.c.

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

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

02704 {
02705    rtp->them.sin_port = them->sin_port;
02706    rtp->them.sin_addr = them->sin_addr;
02707    if (rtp->rtcp) {
02708       int h = ntohs(them->sin_port);
02709       rtp->rtcp->them.sin_port = htons(h + 1);
02710       rtp->rtcp->them.sin_addr = them->sin_addr;
02711    }
02712    rtp->rxseqno = 0;
02713    /* If strict RTP protection is enabled switch back to the learn state so we don't drop packets from above */
02714    if (strictrtp)
02715       rtp->strict_rtp_state = STRICT_RTP_LEARN;
02716 }

void ast_rtp_set_rtpholdtimeout ( struct ast_rtp *  rtp,
int  timeout 
)

Set rtp hold timeout.

Definition at line 757 of file rtp.c.

References ast_rtp::rtpholdtimeout.

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

00758 {
00759    rtp->rtpholdtimeout = timeout;
00760 }

void ast_rtp_set_rtpkeepalive ( struct ast_rtp *  rtp,
int  period 
)

set RTP keepalive interval

Definition at line 763 of file rtp.c.

References ast_rtp::rtpkeepalive.

Referenced by create_addr_from_peer(), and sip_alloc().

00764 {
00765    rtp->rtpkeepalive = period;
00766 }

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

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

Parameters:
rtp RTP structure to modify
pt Payload type entry to modify
mimeType top-level MIME type of media stream (typically "audio", "video", "text", etc.)
mimeSubtype MIME subtype of media stream (typically a codec name)
options Zero or more flags from the ast_rtp_options enum
This function 'fills in' an entry in the list of possible formats for a media stream associated with an RTP structure.

Return values:
0 on success
-1 if the payload type is out of range
-2 if the mimeType/mimeSubtype combination was not found

Definition at line 2353 of file rtp.c.

References ast_rtp_set_rtpmap_type_rate().

Referenced by __oh323_rtp_create(), gtalk_is_answered(), gtalk_newcall(), jingle_newcall(), process_sdp(), process_sdp_a_text(), set_dtmf_payload(), and setup_rtp_connection().

02356 {
02357    return ast_rtp_set_rtpmap_type_rate(rtp, pt, mimeType, mimeSubtype, options, 0);
02358 }

int ast_rtp_set_rtpmap_type_rate ( struct ast_rtp *  rtp,
int  pt,
char *  mimeType,
char *  mimeSubtype,
enum ast_rtp_options  options,
unsigned int  sample_rate 
)

Set payload type to a known MIME media type for a codec with a specific sample rate.

Parameters:
rtp RTP structure to modify
pt Payload type entry to modify
mimeType top-level MIME type of media stream (typically "audio", "video", "text", etc.)
mimeSubtype MIME subtype of media stream (typically a codec name)
options Zero or more flags from the ast_rtp_options enum
sample_rate The sample rate of the media stream
This function 'fills in' an entry in the list of possible formats for a media stream associated with an RTP structure.

Return values:
0 on success
-1 if the payload type is out of range
-2 if the mimeType/mimeSubtype combination was not found
Set payload type to a known MIME media type for a codec with a specific sample rate.

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

Definition at line 2304 of file rtp.c.

References ARRAY_LEN, AST_FORMAT_G726, AST_FORMAT_G726_AAL2, AST_RTP_OPT_G726_NONSTANDARD, rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, mimeTypes, mimeType::payloadType, rtp_bridge_lock(), rtp_bridge_unlock(), mimeType::sample_rate, mimeType::subtype, and mimeType::type.

Referenced by ast_rtp_set_rtpmap_type(), process_sdp_a_audio(), and process_sdp_a_video().

02308 {
02309    unsigned int i;
02310    int found = 0;
02311 
02312    if (pt < 0 || pt >= MAX_RTP_PT)
02313       return -1; /* bogus payload type */
02314 
02315    rtp_bridge_lock(rtp);
02316 
02317    for (i = 0; i < ARRAY_LEN(mimeTypes); ++i) {
02318       const struct mimeType *t = &mimeTypes[i];
02319 
02320       if (strcasecmp(mimeSubtype, t->subtype)) {
02321          continue;
02322       }
02323 
02324       if (strcasecmp(mimeType, t->type)) {
02325          continue;
02326       }
02327 
02328       /* if both sample rates have been supplied, and they don't match,
02329          then this not a match; if one has not been supplied, then the
02330          rates are not compared */
02331       if (sample_rate && t->sample_rate &&
02332           (sample_rate != t->sample_rate)) {
02333          continue;
02334       }
02335 
02336       found = 1;
02337       rtp->current_RTP_PT[pt] = t->payloadType;
02338 
02339       if ((t->payloadType.code == AST_FORMAT_G726) &&
02340           t->payloadType.isAstFormat &&
02341           (options & AST_RTP_OPT_G726_NONSTANDARD)) {
02342          rtp->current_RTP_PT[pt].code = AST_FORMAT_G726_AAL2;
02343       }
02344 
02345       break;
02346    }
02347 
02348    rtp_bridge_unlock(rtp);
02349 
02350    return (found ? 0 : -2);
02351 }

void ast_rtp_set_rtptimeout ( struct ast_rtp *  rtp,
int  timeout 
)

Set rtp timeout.

Definition at line 751 of file rtp.c.

References ast_rtp::rtptimeout.

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

00752 {
00753    rtp->rtptimeout = timeout;
00754 }

void ast_rtp_set_rtptimers_onhold ( struct ast_rtp *  rtp  ) 

Definition at line 744 of file rtp.c.

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

Referenced by handle_response_invite().

00745 {
00746    rtp->rtptimeout = (-1) * rtp->rtptimeout;
00747    rtp->rtpholdtimeout = (-1) * rtp->rtpholdtimeout;
00748 }

void ast_rtp_set_vars ( struct ast_channel *  chan,
struct ast_rtp *  rtp 
)

Set RTPAUDIOQOS(...) variables on a channel when it is being hung up.

Since:
1.6.1

Definition at line 2882 of file rtp.c.

References ast_bridged_channel(), ast_rtp_get_quality(), ast_channel::bridge, pbx_builtin_setvar_helper(), RTPQOS_JITTER, RTPQOS_LOSS, RTPQOS_RTT, and RTPQOS_SUMMARY.

Referenced by handle_request_bye(), and sip_hangup().

02882                                                                      {
02883    char *audioqos;
02884    char *audioqos_jitter;
02885    char *audioqos_loss;
02886    char *audioqos_rtt;
02887    struct ast_channel *bridge;
02888 
02889    if (!rtp || !chan)
02890       return;
02891 
02892    bridge = ast_bridged_channel(chan);
02893 
02894    audioqos        = ast_rtp_get_quality(rtp, NULL, RTPQOS_SUMMARY);
02895    audioqos_jitter = ast_rtp_get_quality(rtp, NULL, RTPQOS_JITTER);
02896    audioqos_loss   = ast_rtp_get_quality(rtp, NULL, RTPQOS_LOSS);
02897    audioqos_rtt    = ast_rtp_get_quality(rtp, NULL, RTPQOS_RTT);
02898 
02899    pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", audioqos);
02900    pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", audioqos_jitter);
02901    pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", audioqos_loss);
02902    pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", audioqos_rtt);
02903 
02904    if (!bridge)
02905       return;
02906 
02907    pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", audioqos);
02908    pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", audioqos_jitter);
02909    pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", audioqos_loss);
02910    pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", audioqos_rtt);
02911 }

void ast_rtp_setdtmf ( struct ast_rtp *  rtp,
int  dtmf 
)

Indicate whether this RTP session is carrying DTMF or not.

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

00811 {
00812    ast_set2_flag(rtp, dtmf ? 1 : 0, FLAG_HAS_DTMF);
00813 }

void ast_rtp_setdtmfcompensate ( struct ast_rtp *  rtp,
int  compensate 
)

Compensate for devices that send RFC2833 packets all at once.

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

00816 {
00817    ast_set2_flag(rtp, compensate ? 1 : 0, FLAG_DTMF_COMPENSATE);
00818 }

void ast_rtp_setnat ( struct ast_rtp *  rtp,
int  nat 
)

Definition at line 800 of file rtp.c.

References ast_rtp::nat.

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

00801 {
00802    rtp->nat = nat;
00803 }

int ast_rtp_setqos ( struct ast_rtp *  rtp,
int  tos,
int  cos,
char *  desc 
)

Definition at line 2679 of file rtp.c.

References ast_netsock_set_qos(), and ast_rtp::s.

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

02680 {
02681    return ast_netsock_set_qos(rtp->s, type_of_service, class_of_service, desc);
02682 }

void ast_rtp_setstun ( struct ast_rtp *  rtp,
int  stun_enable 
)

Enable STUN capability.

Definition at line 820 of file rtp.c.

References ast_set2_flag, and FLAG_HAS_STUN.

Referenced by gtalk_new().

00821 {
00822    ast_set2_flag(rtp, stun_enable ? 1 : 0, FLAG_HAS_STUN);
00823 }

void ast_rtp_stop ( struct ast_rtp *  rtp  ) 

Stop RTP session, do not destroy structure

Definition at line 2757 of file rtp.c.

References ast_clear_flag, AST_SCHED_DEL, FLAG_P2P_SENT_MARK, free, ast_rtp::red, ast_rtp::rtcp, ast_rtp::sched, rtp_red::schedid, ast_rtcp::schedid, ast_rtcp::them, and ast_rtp::them.

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

02758 {
02759    if (rtp->rtcp) {
02760       AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
02761    }
02762    if (rtp->red) {
02763       AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
02764       free(rtp->red);
02765       rtp->red = NULL;
02766    }
02767 
02768    memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
02769    memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
02770    if (rtp->rtcp) {
02771       memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->rtcp->them.sin_addr));
02772       memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->rtcp->them.sin_port));
02773    }
02774    
02775    ast_clear_flag(rtp, FLAG_P2P_SENT_MARK);
02776 }

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

Send STUN request for an RTP socket Deprecated, this is just a wrapper for ast_rtp_stun_request().

Definition at line 699 of file rtp.c.

References ast_stun_request(), and ast_rtp::s.

Referenced by gtalk_update_stun(), and jingle_update_stun().

00700 {
00701    ast_stun_request(rtp->s, suggestion, username, NULL);
00702 }

void ast_rtp_unset_m_type ( struct ast_rtp *  rtp,
int  pt 
)

clear payload type

Definition at line 2289 of file rtp.c.

References rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, rtp_bridge_lock(), and rtp_bridge_unlock().

Referenced by process_sdp_a_audio(), and process_sdp_a_video().

02290 {
02291    if (pt < 0 || pt >= MAX_RTP_PT)
02292       return; /* bogus payload type */
02293 
02294    rtp_bridge_lock(rtp);
02295    rtp->current_RTP_PT[pt].isAstFormat = 0;
02296    rtp->current_RTP_PT[pt].code = 0;
02297    rtp_bridge_unlock(rtp);
02298 }

int ast_rtp_write ( struct ast_rtp *  rtp,
struct ast_frame *  f 
)

Bug:
XXX this might never be free'd. Why do we do this?

Definition at line 3848 of file rtp.c.

References ast_codec_pref_getsize(), ast_debug, AST_FORMAT_G723_1, AST_FORMAT_SIREN14, AST_FORMAT_SIREN7, AST_FORMAT_SPEEX, AST_FRAME_TEXT, 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::data, 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_WARNING, ast_frame::offset, ast_rtp::pref, ast_frame::ptr, ast_rtp::red, red_t140_to_red(), ast_rtp::smoother, ast_frame::subclass, and ast_rtp::them.

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

03849 {
03850    struct ast_frame *f;
03851    int codec;
03852    int hdrlen = 12;
03853    int subclass;
03854    
03855 
03856    /* If we have no peer, return immediately */ 
03857    if (!rtp->them.sin_addr.s_addr)
03858       return 0;
03859 
03860    /* If there is no data length, return immediately */
03861    if (!_f->datalen && !rtp->red)
03862       return 0;
03863    
03864    /* Make sure we have enough space for RTP header */
03865    if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO) && (_f->frametype != AST_FRAME_TEXT)) {
03866       ast_log(LOG_WARNING, "RTP can only send voice, video and text\n");
03867       return -1;
03868    }
03869 
03870    if (rtp->red) {
03871       /* return 0; */
03872       /* no primary data or generations to send */
03873       if ((_f = red_t140_to_red(rtp->red)) == NULL) 
03874          return 0;
03875    }
03876 
03877    /* The bottom bit of a video subclass contains the marker bit */
03878    subclass = _f->subclass;
03879    if (_f->frametype == AST_FRAME_VIDEO)
03880       subclass &= ~0x1;
03881 
03882    codec = ast_rtp_lookup_code(rtp, 1, subclass);
03883    if (codec < 0) {
03884       ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
03885       return -1;
03886    }
03887 
03888    if (rtp->lasttxformat != subclass) {
03889       /* New format, reset the smoother */
03890       ast_debug(1, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
03891       rtp->lasttxformat = subclass;
03892       if (rtp->smoother)
03893          ast_smoother_free(rtp->smoother);
03894       rtp->smoother = NULL;
03895    }
03896 
03897    if (!rtp->smoother) {
03898       struct ast_format_list fmt = ast_codec_pref_getsize(&rtp->pref, subclass);
03899 
03900       switch (subclass) {
03901       case AST_FORMAT_SPEEX:
03902       case AST_FORMAT_G723_1:
03903       case AST_FORMAT_SIREN7:
03904       case AST_FORMAT_SIREN14:
03905          /* these are all frame-based codecs and cannot be safely run through
03906             a smoother */
03907          break;
03908       default:
03909          if (fmt.inc_ms) { /* if codec parameters is set / avoid division by zero */
03910             if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
03911                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));
03912                return -1;
03913             }
03914             if (fmt.flags)
03915                ast_smoother_set_flags(rtp->smoother, fmt.flags);
03916             ast_debug(1, "Created smoother: format: %d ms: %d len: %d\n", subclass, fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
03917          }
03918       }
03919    }
03920    if (rtp->smoother) {
03921       if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
03922          ast_smoother_feed_be(rtp->smoother, _f);
03923       } else {
03924          ast_smoother_feed(rtp->smoother, _f);
03925       }
03926 
03927       while ((f = ast_smoother_read(rtp->smoother)) && (f->data.ptr)) {
03928          ast_rtp_raw_write(rtp, f, codec);
03929       }
03930    } else {
03931       /* Don't buffer outgoing frames; send them one-per-packet: */
03932       if (_f->offset < hdrlen) 
03933          f = ast_frdup(_f);   /*! \bug XXX this might never be free'd. Why do we do this? */
03934       else
03935          f = _f;
03936       if (f->data.ptr)
03937          ast_rtp_raw_write(rtp, f, codec);
03938       if (f != _f)
03939          ast_frfree(f);
03940    }
03941       
03942    return 0;
03943 }

int ast_stun_request ( int  s,
struct sockaddr_in *  dst,
const char *  username,
struct sockaddr_in *  answer 
)

Generic STUN request send a generic stun request to the server specified.

Parameters:
s the socket used to send the request
dst the address of the STUN server
username if non null, add the username in the request
answer if non null, the function waits for a response and puts here the externally visible address.
Returns:
0 on success, other values on error. The interface it may change in the future.
Generic STUN request send a generic stun request to the server specified.

Parameters:
s the socket used to send the request
dst the address of the STUN server
username if non null, add the username in the request
answer if non null, the function waits for a response and puts here the externally visible address.
Returns:
0 on success, other values on error.

Definition at line 636 of file rtp.c.

References append_attr_string(), ast_log(), ast_poll, stun_header::ies, LOG_WARNING, stun_header::msglen, stun_header::msgtype, STUN_BINDREQ, stun_get_mapped(), stun_handle_packet(), stun_req_id(), stun_send(), and STUN_USERNAME.

Referenced by ast_rtp_stun_request(), ast_sip_ouraddrfor(), and reload_config().

00638 {
00639    struct stun_header *req;
00640    unsigned char reqdata[1024];
00641    int reqlen, reqleft;
00642    struct stun_attr *attr;
00643    int res = 0;
00644    int retry;
00645    
00646    req = (struct stun_header *)reqdata;
00647    stun_req_id(req);
00648    reqlen = 0;
00649    reqleft = sizeof(reqdata) - sizeof(struct stun_header);
00650    req->msgtype = 0;
00651    req->msglen = 0;
00652    attr = (struct stun_attr *)req->ies;
00653    if (username)
00654       append_attr_string(&attr, STUN_USERNAME, username, &reqlen, &reqleft);
00655    req->msglen = htons(reqlen);
00656    req->msgtype = htons(STUN_BINDREQ);
00657    for (retry = 0; retry < 3; retry++) {  /* XXX make retries configurable */
00658       /* send request, possibly wait for reply */
00659       unsigned char reply_buf[1024];
00660       struct pollfd pfds = { .fd = s, .events = POLLIN, };
00661       struct sockaddr_in src;
00662       socklen_t srclen;
00663 
00664       res = stun_send(s, dst, req);
00665       if (res < 0) {
00666          ast_log(LOG_WARNING, "ast_stun_request send #%d failed error %d, retry\n",
00667             retry, res);
00668          continue;
00669       }
00670       if (answer == NULL)
00671          break;
00672       res = ast_poll(&pfds, 1, 3000);
00673       if (res <= 0)  /* timeout or error */
00674          continue;
00675       memset(&src, '\0', sizeof(src));
00676       srclen = sizeof(src);
00677       /* XXX pass -1 in the size, because stun_handle_packet might
00678        * write past the end of the buffer.
00679        */
00680       res = recvfrom(s, reply_buf, sizeof(reply_buf) - 1,
00681          0, (struct sockaddr *)&src, &srclen);
00682       if (res < 0) {
00683          ast_log(LOG_WARNING, "ast_stun_request recvfrom #%d failed error %d, retry\n",
00684             retry, res);
00685          continue;
00686       }
00687       memset(answer, '\0', sizeof(struct sockaddr_in));
00688       stun_handle_packet(s, &src, reply_buf, res,
00689          stun_get_mapped, answer);
00690       res = 0; /* signal regular exit */
00691       break;
00692    }
00693    return res;
00694 }

void red_buffer_t140 ( struct ast_rtp *  rtp,
struct ast_frame *  f 
)

Buffer t.140 data.

Buffer t.140 data.

Parameters:
rtp 
f frame

Definition at line 4981 of file rtp.c.

References rtp_red::buf_data, ast_frame::data, ast_frame::datalen, ast_frame::ptr, ast_rtp::red, rtp_red::t140, and ast_frame::ts.

Referenced by sip_write().

04982 {
04983    if (f->datalen > -1) {
04984       struct rtp_red *red = rtp->red;
04985       memcpy(&red->buf_data[red->t140.datalen], f->data.ptr, f->datalen);
04986       red->t140.datalen += f->datalen;
04987       red->t140.ts = f->ts;
04988    }
04989 }

int rtp_red_init ( struct ast_rtp *  rtp,
int  ti,
int *  red_data_pt,
int  num_gen 
)

Initalize t.140 redudancy.

Parameters:
ti time between each t140red frame is sent
red_pt payloadtype for RTP packet
pt payloadtype numbers for each generation including primary data
num_gen number of redundant generations, primary data excluded
Since:
1.6.1
Initalize t.140 redudancy.

Parameters:
rtp 
ti buffer t140 for ti (msecs) before sending redundant frame
red_data_pt Payloadtypes for primary- and generation-data
num_gen numbers of generations (primary generation not encounted)

Definition at line 4942 of file rtp.c.

References ast_calloc, AST_FORMAT_T140RED, AST_FRAME_TEXT, ast_sched_add(), rtp_red::buf_data, ast_frame::data, ast_frame::datalen, ast_frame::frametype, rtp_red::hdrlen, rtp_red::num_gen, rtp_red::prev_ts, rtp_red::pt, ast_frame::ptr, ast_rtp::red, red_write(), ast_rtp::sched, rtp_red::schedid, ast_frame::subclass, rtp_red::t140, rtp_red::t140red, rtp_red::t140red_data, rtp_red::ti, and ast_frame::ts.

Referenced by process_sdp().

04943 {
04944    struct rtp_red *r;
04945    int x;
04946    
04947    if (!(r = ast_calloc(1, sizeof(struct rtp_red))))
04948       return -1;
04949 
04950    r->t140.frametype = AST_FRAME_TEXT;
04951    r->t140.subclass = AST_FORMAT_T140RED;
04952    r->t140.data.ptr = &r->buf_data; 
04953 
04954    r->t140.ts = 0;
04955    r->t140red = r->t140;
04956    r->t140red.data.ptr = &r->t140red_data;
04957    r->t140red.datalen = 0;
04958    r->ti = ti;
04959    r->num_gen = num_gen;
04960    r->hdrlen = num_gen * 4 + 1;
04961    r->prev_ts = 0;
04962 
04963    for (x = 0; x < num_gen; x++) {
04964       r->pt[x] = red_data_pt[x];
04965       r->pt[x] |= 1 << 7; /* mark redundant generations pt */ 
04966       r->t140red_data[x*4] = r->pt[x];
04967    }
04968    r->t140red_data[x*4] = r->pt[x] = red_data_pt[x]; /* primary pt */
04969    r->schedid = ast_sched_add(rtp->sched, ti, red_write, rtp);
04970    rtp->red = r;
04971 
04972    r->t140.datalen = 0;
04973    
04974    return 0;
04975 }


Generated on Sat Mar 26 11:26:21 2011 for Asterisk - the Open Source PBX by  doxygen 1.5.9