Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

rtp.h File Reference

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

Go to the source code of this file.

Data Structures

struct  ast_rtp_protocol

Defines

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

Typedefs

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

Functions

ast_rtpast_rtp_new (struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
void ast_rtp_set_peer (struct ast_rtp *rtp, struct sockaddr_in *them)
void ast_rtp_get_peer (struct ast_rtp *rtp, struct sockaddr_in *them)
void ast_rtp_get_us (struct ast_rtp *rtp, struct sockaddr_in *us)
void ast_rtp_destroy (struct ast_rtp *rtp)
void ast_rtp_set_callback (struct ast_rtp *rtp, ast_rtp_callback callback)
void ast_rtp_set_data (struct ast_rtp *rtp, void *data)
int ast_rtp_write (struct ast_rtp *rtp, struct ast_frame *f)
ast_frameast_rtp_read (struct ast_rtp *rtp)
ast_frameast_rtcp_read (struct ast_rtp *rtp)
int ast_rtp_fd (struct ast_rtp *rtp)
int ast_rtcp_fd (struct ast_rtp *rtp)
int ast_rtp_senddigit (struct ast_rtp *rtp, char digit)
int ast_rtp_settos (struct ast_rtp *rtp, int tos)
void ast_rtp_pt_clear (struct ast_rtp *rtp)
void ast_rtp_pt_default (struct ast_rtp *rtp)
void ast_rtp_set_m_type (struct ast_rtp *rtp, int pt)
void ast_rtp_set_rtpmap_type (struct ast_rtp *rtp, int pt, char *mimeType, char *mimeSubtype)
rtpPayloadType ast_rtp_lookup_pt (struct ast_rtp *rtp, int pt)
int ast_rtp_lookup_code (struct ast_rtp *rtp, int isAstFormat, int code)
void ast_rtp_get_current_formats (struct ast_rtp *rtp, int *astFormats, int *nonAstFormats)
char * ast_rtp_lookup_mime_subtype (int isAstFormat, int code)
void ast_rtp_setnat (struct ast_rtp *rtp, int nat)
int ast_rtp_bridge (struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
int ast_rtp_proto_register (struct ast_rtp_protocol *proto)
void ast_rtp_proto_unregister (struct ast_rtp_protocol *proto)
void ast_rtp_stop (struct ast_rtp *rtp)
void ast_rtp_init (void)
void ast_rtp_reload (void)


Define Documentation

#define AST_RTP_CISCO_DTMF   (1 << 2)
 

DTMF (Cisco Proprietary)

Definition at line 34 of file rtp.h.

Referenced by ast_rtp_read().

#define AST_RTP_CN   (1 << 1)
 

'Comfort Noise' (RFC3389)

Definition at line 32 of file rtp.h.

Referenced by ast_rtp_read().

#define AST_RTP_DTMF   (1 << 0)
 

DTMF (RFC2833)

Definition at line 30 of file rtp.h.

Referenced by ast_rtp_read().

#define AST_RTP_MAX   AST_RTP_CISCO_DTMF
 

Maximum RTP-specific code

Definition at line 36 of file rtp.h.


Typedef Documentation

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

Definition at line 48 of file rtp.h.

Referenced by ast_rtp_set_callback().


Function Documentation

int ast_rtcp_fd struct ast_rtp rtp  ) 
 

Definition at line 105 of file rtp.c.

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

00106 {
00107    if (rtp->rtcp)
00108       return rtp->rtcp->s;
00109    return -1;
00110 }

struct ast_frame* ast_rtcp_read struct ast_rtp rtp  ) 
 

Definition at line 309 of file rtp.c.

References AST_FRAME_NULL, ast_log(), CRASH, LOG_DEBUG, LOG_WARNING, ast_rtp::nat, option_debug, ast_rtp::rtcp, ast_rtcp::s, ast_rtp::them, and ast_rtcp::them.

00310 {
00311    static struct ast_frame null_frame = { AST_FRAME_NULL, };
00312    int len;
00313    int hdrlen = 8;
00314    int res;
00315    struct sockaddr_in sin;
00316    unsigned int rtcpdata[1024];
00317    
00318    if (!rtp->rtcp)
00319       return &null_frame;
00320 
00321    len = sizeof(sin);
00322    
00323    res = recvfrom(rtp->rtcp->s, rtcpdata, sizeof(rtcpdata),
00324                0, (struct sockaddr *)&sin, &len);
00325    
00326    if (res < 0) {
00327       ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
00328       if (errno == EBADF)
00329          CRASH;
00330       return &null_frame;
00331    }
00332 
00333    if (res < hdrlen) {
00334       ast_log(LOG_WARNING, "RTP Read too short\n");
00335       return &null_frame;
00336    }
00337 
00338    if (rtp->nat) {
00339       /* Send to whoever sent to us */
00340       if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
00341           (rtp->rtcp->them.sin_port != sin.sin_port)) {
00342          memcpy(&rtp->them, &sin, sizeof(rtp->them));
00343          ast_log(LOG_DEBUG, "RTP NAT: Using address %s:%d\n", inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
00344       }
00345    }
00346    if (option_debug)
00347       ast_log(LOG_DEBUG, "Got RTCP report of %d bytes\n", res);
00348    return &null_frame;
00349 }

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

Definition at line 1133 of file rtp.c.

References ast_autoservice_start(), ast_autoservice_stop(), AST_BRIDGE_DTMF_CHANNEL_0, AST_BRIDGE_DTMF_CHANNEL_1, AST_FRAME_DTMF, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frfree(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_read(), ast_rtp_get_peer(), ast_waitfor_n(), ast_write(), ast_frame::frametype, inaddrcmp(), ast_channel::lock, LOG_DEBUG, LOG_WARNING, ast_channel::name, ast_channel_pvt::pvt, and ast_channel::pvt.

01134 {
01135    struct ast_frame *f;
01136    struct ast_channel *who, *cs[3];
01137    struct ast_rtp *p0, *p1;
01138    struct ast_rtp *vp0, *vp1;
01139    struct ast_rtp_protocol *pr0, *pr1;
01140    struct sockaddr_in ac0, ac1;
01141    struct sockaddr_in vac0, vac1;
01142    struct sockaddr_in t0, t1;
01143    struct sockaddr_in vt0, vt1;
01144    
01145    void *pvt0, *pvt1;
01146    int to;
01147 
01148    memset(&vt0, 0, sizeof(vt0));
01149    memset(&vt1, 0, sizeof(vt1));
01150    memset(&vac0, 0, sizeof(vac0));
01151    memset(&vac1, 0, sizeof(vac1));
01152 
01153    /* XXX Wait a half a second for things to settle up 
01154          this really should be fixed XXX */
01155    ast_autoservice_start(c0);
01156    ast_autoservice_start(c1);
01157    usleep(500000);
01158    ast_autoservice_stop(c0);
01159    ast_autoservice_stop(c1);
01160 
01161    /* if need DTMF, cant native bridge */
01162    if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
01163       return -2;
01164    ast_mutex_lock(&c0->lock);
01165    ast_mutex_lock(&c1->lock);
01166    pr0 = get_proto(c0);
01167    pr1 = get_proto(c1);
01168    if (!pr0) {
01169       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
01170       ast_mutex_unlock(&c0->lock);
01171       ast_mutex_unlock(&c1->lock);
01172       return -1;
01173    }
01174    if (!pr1) {
01175       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
01176       ast_mutex_unlock(&c0->lock);
01177       ast_mutex_unlock(&c1->lock);
01178       return -1;
01179    }
01180    pvt0 = c0->pvt->pvt;
01181    pvt1 = c1->pvt->pvt;
01182    p0 = pr0->get_rtp_info(c0);
01183    if (pr0->get_vrtp_info)
01184       vp0 = pr0->get_vrtp_info(c0);
01185    else
01186       vp0 = NULL;
01187    p1 = pr1->get_rtp_info(c1);
01188    if (pr1->get_vrtp_info)
01189       vp1 = pr1->get_vrtp_info(c1);
01190    else
01191       vp1 = NULL;
01192    if (!p0 || !p1) {
01193       /* Somebody doesn't want to play... */
01194       ast_mutex_unlock(&c0->lock);
01195       ast_mutex_unlock(&c1->lock);
01196       return -2;
01197    }
01198    if (pr0->set_rtp_peer(c0, p1, vp1)) 
01199       ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
01200    else {
01201       /* Store RTP peer */
01202       ast_rtp_get_peer(p1, &ac1);
01203       if (vp1)
01204          ast_rtp_get_peer(p1, &vac1);
01205    }
01206    if (pr1->set_rtp_peer(c1, p0, vp0))
01207       ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
01208    else {
01209       /* Store RTP peer */
01210       ast_rtp_get_peer(p0, &ac0);
01211       if (vp0)
01212          ast_rtp_get_peer(p0, &vac0);
01213    }
01214    ast_mutex_unlock(&c0->lock);
01215    ast_mutex_unlock(&c1->lock);
01216    cs[0] = c0;
01217    cs[1] = c1;
01218    cs[2] = NULL;
01219    for (;;) {
01220       if ((c0->pvt->pvt != pvt0)  ||
01221          (c1->pvt->pvt != pvt1) ||
01222          (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
01223             ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
01224             if (c0->pvt->pvt == pvt0) {
01225                if (pr0->set_rtp_peer(c0, NULL, NULL)) 
01226                   ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
01227             }
01228             if (c1->pvt->pvt == pvt1) {
01229                if (pr1->set_rtp_peer(c1, NULL, NULL)) 
01230                   ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
01231             }
01232             /* Tell it to try again later */
01233             return -3;
01234       }
01235       to = -1;
01236       ast_rtp_get_peer(p1, &t1);
01237       ast_rtp_get_peer(p0, &t0);
01238       if (vp1)
01239          ast_rtp_get_peer(vp1, &vt1);
01240       if (vp0)
01241          ast_rtp_get_peer(vp0, &vt0);
01242       if (inaddrcmp(&t1, &ac1) || (vp1 && inaddrcmp(&vt1, &vac1))) {
01243          ast_log(LOG_DEBUG, "Oooh, '%s' changed end address\n", c1->name);
01244          if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL)) 
01245             ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
01246          memcpy(&ac1, &t1, sizeof(ac1));
01247          memcpy(&vac1, &vt1, sizeof(vac1));
01248       }
01249       if (inaddrcmp(&t0, &ac0) || (vp0 && inaddrcmp(&vt0, &vac0))) {
01250          ast_log(LOG_DEBUG, "Oooh, '%s' changed end address\n", c0->name);
01251          if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL))
01252             ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
01253          memcpy(&ac0, &t0, sizeof(ac0));
01254          memcpy(&vac0, &vt0, sizeof(vac0));
01255       }
01256       who = ast_waitfor_n(cs, 2, &to);
01257       if (!who) {
01258          ast_log(LOG_DEBUG, "Ooh, empty read...\n");
01259          continue;
01260       }
01261       f = ast_read(who);
01262       if (!f || ((f->frametype == AST_FRAME_DTMF) &&
01263                (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) || 
01264                 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
01265          *fo = f;
01266          *rc = who;
01267          ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
01268          if ((c0->pvt->pvt == pvt0) && (!c0->_softhangup)) {
01269             if (pr0->set_rtp_peer(c0, NULL, NULL)) 
01270                ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
01271          }
01272          if ((c1->pvt->pvt == pvt1) && (!c1->_softhangup)) {
01273             if (pr1->set_rtp_peer(c1, NULL, NULL)) 
01274                ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
01275          }
01276          /* That's all we needed */
01277          return 0;
01278       } else {
01279          if ((f->frametype == AST_FRAME_DTMF) || 
01280             (f->frametype == AST_FRAME_VOICE) || 
01281             (f->frametype == AST_FRAME_VIDEO)) {
01282             /* Forward voice or DTMF frames if they happen upon us */
01283             if (who == c0) {
01284                ast_write(c1, f);
01285             } else if (who == c1) {
01286                ast_write(c0, f);
01287             }
01288          }
01289          ast_frfree(f);
01290       }
01291       /* Swap priority not that it's a big deal at this point */
01292       cs[2] = cs[0];
01293       cs[0] = cs[1];
01294       cs[1] = cs[2];
01295       
01296    }
01297    return -1;
01298 }

void ast_rtp_destroy struct ast_rtp rtp  ) 
 

Definition at line 803 of file rtp.c.

References ast_io_remove(), ast_smoother_free(), free, ast_rtp::io, ast_rtp::ioid, ast_rtp::rtcp, ast_rtcp::s, ast_rtp::s, and ast_rtp::smoother.

00804 {
00805    if (rtp->smoother)
00806       ast_smoother_free(rtp->smoother);
00807    if (rtp->ioid)
00808       ast_io_remove(rtp->io, rtp->ioid);
00809    if (rtp->s > -1)
00810       close(rtp->s);
00811    if (rtp->rtcp) {
00812       close(rtp->rtcp->s);
00813       free(rtp->rtcp);
00814    }
00815    free(rtp);
00816 }

int ast_rtp_fd struct ast_rtp rtp  ) 
 

Definition at line 100 of file rtp.c.

References ast_rtp::s.

00101 {
00102    return rtp->s;
00103 }

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

Definition at line 612 of file rtp.c.

References rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, and MAX_RTP_PT.

00613                                                    {
00614   int pt;
00615 
00616   *astFormats = *nonAstFormats = 0;
00617   for (pt = 0; pt < MAX_RTP_PT; ++pt) {
00618     if (rtp->current_RTP_PT[pt].isAstFormat) {
00619       *astFormats |= rtp->current_RTP_PT[pt].code;
00620     } else {
00621       *nonAstFormats |= rtp->current_RTP_PT[pt].code;
00622     }
00623   }
00624 }

void ast_rtp_get_peer struct ast_rtp rtp,
struct sockaddr_in *  them
 

Definition at line 781 of file rtp.c.

References ast_rtp::them.

Referenced by ast_rtp_bridge().

00782 {
00783    them->sin_family = AF_INET;
00784    them->sin_port = rtp->them.sin_port;
00785    them->sin_addr = rtp->them.sin_addr;
00786 }

void ast_rtp_get_us struct ast_rtp rtp,
struct sockaddr_in *  us
 

Definition at line 788 of file rtp.c.

References ast_rtp::us.

00789 {
00790    memcpy(us, &rtp->us, sizeof(rtp->us));
00791 }

void ast_rtp_init void   ) 
 

Definition at line 1333 of file rtp.c.

References ast_rtp_reload().

Referenced by main().

01334 {
01335    ast_rtp_reload();
01336 }

int ast_rtp_lookup_code struct ast_rtp rtp,
int  isAstFormat,
int  code
 

Definition at line 636 of file rtp.c.

References rtpPayloadType::code, rtpPayloadType::isAstFormat, MAX_RTP_PT, ast_rtp::rtp_lookup_code_cache_code, ast_rtp::rtp_lookup_code_cache_isAstFormat, and ast_rtp::rtp_lookup_code_cache_result.

Referenced by ast_rtp_write().

00636                                                                         {
00637   int pt;
00638 
00639   /* Looks up an RTP code out of our *static* outbound list */
00640 
00641   if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
00642       code == rtp->rtp_lookup_code_cache_code) {
00643     // Use our cached mapping, to avoid the overhead of the loop below
00644     return rtp->rtp_lookup_code_cache_result;
00645   }
00646 
00647   for (pt = 0; pt < MAX_RTP_PT; ++pt) {
00648     if (static_RTP_PT[pt].code == code &&
00649       static_RTP_PT[pt].isAstFormat == isAstFormat) {
00650       rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
00651       rtp->rtp_lookup_code_cache_code = code;
00652       rtp->rtp_lookup_code_cache_result = pt;
00653       return pt;
00654     }
00655   }
00656   return -1;
00657 }

char* ast_rtp_lookup_mime_subtype int  isAstFormat,
int  code
 

Definition at line 659 of file rtp.c.

00659                                                              {
00660   int i;
00661 
00662   for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
00663     if (mimeTypes[i].payloadType.code == code &&
00664    mimeTypes[i].payloadType.isAstFormat == isAstFormat) {
00665       return mimeTypes[i].subtype;
00666     }
00667   }
00668   return "";
00669 }

struct rtpPayloadType ast_rtp_lookup_pt struct ast_rtp rtp,
int  pt
 

Definition at line 626 of file rtp.c.

References ast_rtp_lookup_pt(), rtpPayloadType::code, rtpPayloadType::isAstFormat, and MAX_RTP_PT.

Referenced by ast_rtp_lookup_pt(), and ast_rtp_read().

00626                                                                      {
00627   if (pt < 0 || pt > MAX_RTP_PT) {
00628     struct rtpPayloadType result;
00629     result.isAstFormat = result.code = 0;
00630     return result; // bogus payload type
00631   }
00632   /* Gotta use our static one, since that's what we sent against */
00633   return static_RTP_PT[pt];
00634 }

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

Definition at line 691 of file rtp.c.

References ast_io_add(), AST_IO_IN, ast_log(), ast_rtp_pt_default(), free, LOG_WARNING, and malloc.

00692 {
00693    struct ast_rtp *rtp;
00694    int x;
00695    int flags;
00696    int startplace;
00697    rtp = malloc(sizeof(struct ast_rtp));
00698    if (!rtp)
00699       return NULL;
00700    memset(rtp, 0, sizeof(struct ast_rtp));
00701    rtp->them.sin_family = AF_INET;
00702    rtp->us.sin_family = AF_INET;
00703    rtp->s = socket(AF_INET, SOCK_DGRAM, 0);
00704    rtp->ssrc = rand();
00705    rtp->seqno = rand() & 0xffff;
00706    if (rtp->s < 0) {
00707       free(rtp);
00708       ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
00709       return NULL;
00710    }
00711    if (sched && rtcpenable) {
00712       rtp->sched = sched;
00713       rtp->rtcp = ast_rtcp_new();
00714    }
00715    flags = fcntl(rtp->s, F_GETFL);
00716    fcntl(rtp->s, F_SETFL, flags | O_NONBLOCK);
00717    /* Find us a place */
00718    x = (rand() % (rtpend-rtpstart)) + rtpstart;
00719    x = x & ~1;
00720    startplace = x;
00721    for (;;) {
00722       /* Must be an even port number by RTP spec */
00723       rtp->us.sin_port = htons(x);
00724       if (rtp->rtcp)
00725          rtp->rtcp->us.sin_port = htons(x + 1);
00726       if (!bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us)) &&
00727          (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
00728          break;
00729       if (errno != EADDRINUSE) {
00730          ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
00731          close(rtp->s);
00732          if (rtp->rtcp) {
00733             close(rtp->rtcp->s);
00734             free(rtp->rtcp);
00735          }
00736          free(rtp);
00737          return NULL;
00738       }
00739       x += 2;
00740       if (x > rtpend)
00741          x = (rtpstart + 1) & ~1;
00742       if (x == startplace) {
00743          ast_log(LOG_WARNING, "No RTP ports remaining\n");
00744          close(rtp->s);
00745          if (rtp->rtcp) {
00746             close(rtp->rtcp->s);
00747             free(rtp->rtcp);
00748          }
00749          free(rtp);
00750          return NULL;
00751       }
00752    }
00753    if (io && sched && callbackmode) {
00754       /* Operate this one in a callback mode */
00755       rtp->sched = sched;
00756       rtp->io = io;
00757       rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
00758    }
00759    ast_rtp_pt_default(rtp);
00760    return rtp;
00761 }

int ast_rtp_proto_register struct ast_rtp_protocol proto  ) 
 

Definition at line 1104 of file rtp.c.

References ast_log(), LOG_WARNING, ast_rtp_protocol::next, and ast_rtp_protocol::type.

01105 {
01106    struct ast_rtp_protocol *cur;
01107    cur = protos;
01108    while(cur) {
01109       if (cur->type == proto->type) {
01110          ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
01111          return -1;
01112       }
01113       cur = cur->next;
01114    }
01115    proto->next = protos;
01116    protos = proto;
01117    return 0;
01118 }

void ast_rtp_proto_unregister struct ast_rtp_protocol proto  ) 
 

Definition at line 1086 of file rtp.c.

References ast_rtp_protocol::next.

01087 {
01088    struct ast_rtp_protocol *cur, *prev;
01089    cur = protos;
01090    prev = NULL;
01091    while(cur) {
01092       if (cur == proto) {
01093          if (prev)
01094             prev->next = proto->next;
01095          else
01096             protos = proto->next;
01097          return;
01098       }
01099       prev = cur;
01100       cur = cur->next;
01101    }
01102 }

void ast_rtp_pt_clear struct ast_rtp rtp  ) 
 

Definition at line 554 of file rtp.c.

References rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, ast_rtp::rtp_lookup_code_cache_code, ast_rtp::rtp_lookup_code_cache_isAstFormat, and ast_rtp::rtp_lookup_code_cache_result.

00555 {
00556   int i;
00557 
00558   for (i = 0; i < MAX_RTP_PT; ++i) {
00559     rtp->current_RTP_PT[i].isAstFormat = 0;
00560     rtp->current_RTP_PT[i].code = 0;
00561   }
00562 
00563   rtp->rtp_lookup_code_cache_isAstFormat = 0;
00564   rtp->rtp_lookup_code_cache_code = 0;
00565   rtp->rtp_lookup_code_cache_result = 0;
00566 }

void ast_rtp_pt_default struct ast_rtp rtp  ) 
 

Definition at line 568 of file rtp.c.

References rtpPayloadType::code, ast_rtp::current_RTP_PT, rtpPayloadType::isAstFormat, MAX_RTP_PT, ast_rtp::rtp_lookup_code_cache_code, ast_rtp::rtp_lookup_code_cache_isAstFormat, and ast_rtp::rtp_lookup_code_cache_result.

Referenced by ast_rtp_new().

00569 {
00570   int i;
00571   /* Initialize to default payload types */
00572   for (i = 0; i < MAX_RTP_PT; ++i) {
00573     rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
00574     rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
00575   }
00576 
00577   rtp->rtp_lookup_code_cache_isAstFormat = 0;
00578   rtp->rtp_lookup_code_cache_code = 0;
00579   rtp->rtp_lookup_code_cache_result = 0;
00580 }

struct ast_frame* ast_rtp_read struct ast_rtp rtp  ) 
 

Definition at line 351 of file rtp.c.

References AST_FORMAT_ADPCM, AST_FORMAT_ALAW, AST_FORMAT_G723_1, AST_FORMAT_G729A, AST_FORMAT_GSM, AST_FORMAT_ILBC, AST_FORMAT_MAX_AUDIO, AST_FORMAT_SLINEAR, AST_FORMAT_SPEEX, AST_FORMAT_ULAW, AST_FRAME_NULL, AST_FRAME_VIDEO, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_getformatname(), ast_log(), AST_RTP_CISCO_DTMF, AST_RTP_CN, AST_RTP_DTMF, ast_rtp_lookup_pt(), rtpPayloadType::code, CRASH, ast_frame::data, ast_frame::datalen, ast_rtp::dtmfcount, ast_rtp::f, ast_frame::frametype, rtpPayloadType::isAstFormat, ast_rtp::lastividtimestamp, ast_rtp::lastrxformat, ast_rtp::lastrxts, LOG_DEBUG, LOG_NOTICE, LOG_WARNING, ast_frame::mallocd, ast_rtp::nat, ast_frame::offset, ast_rtp::rawdata, ast_rtp::resp, ast_rtp::s, ast_frame::samples, ast_frame::src, ast_frame::subclass, and ast_rtp::them.

00352 {
00353    int res;
00354    struct sockaddr_in sin;
00355    int len;
00356    unsigned int seqno;
00357    int payloadtype;
00358    int hdrlen = 12;
00359    int mark;
00360    unsigned int timestamp;
00361    unsigned int *rtpheader;
00362    static struct ast_frame *f, null_frame = { AST_FRAME_NULL, };
00363    struct rtpPayloadType rtpPT;
00364    
00365    len = sizeof(sin);
00366    
00367    /* Cache where the header will go */
00368    res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
00369                0, (struct sockaddr *)&sin, &len);
00370 
00371 
00372    rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
00373    if (res < 0) {
00374       ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
00375       if (errno == EBADF)
00376          CRASH;
00377       return &null_frame;
00378    }
00379    if (res < hdrlen) {
00380       ast_log(LOG_WARNING, "RTP Read too short\n");
00381       return &null_frame;
00382    }
00383    if (rtp->nat) {
00384       /* Send to whoever sent to us */
00385       if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
00386           (rtp->them.sin_port != sin.sin_port)) {
00387          memcpy(&rtp->them, &sin, sizeof(rtp->them));
00388          ast_log(LOG_DEBUG, "RTP NAT: Using address %s:%d\n", inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
00389       }
00390    }
00391    /* Get fields */
00392    seqno = ntohl(rtpheader[0]);
00393    payloadtype = (seqno & 0x7f0000) >> 16;
00394    mark = seqno & (1 << 23);
00395    seqno &= 0xffff;
00396    timestamp = ntohl(rtpheader[1]);
00397 
00398 #if 0
00399    printf("Got RTP packet from %s:%d (type %d, seq %d, ts %d, len = %d)\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
00400 #endif   
00401    rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
00402    if (!rtpPT.isAstFormat) {
00403      // This is special in-band data that's not one of our codecs
00404      if (rtpPT.code == AST_RTP_DTMF) {
00405        /* It's special -- rfc2833 process it */
00406        f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
00407        if (f) return f; else return &null_frame;
00408      } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
00409        /* It's really special -- process it the Cisco way */
00410        f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
00411        if (f) return f; else return &null_frame;
00412      } else if (rtpPT.code == AST_RTP_CN) {
00413        /* Comfort Noise */
00414        f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
00415        if (f) return f; else return &null_frame;
00416      } else {
00417        ast_log(LOG_NOTICE, "Unknown RTP codec %d received\n", payloadtype);
00418        return &null_frame;
00419      }
00420    }
00421    rtp->f.subclass = rtpPT.code;
00422    if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO)
00423       rtp->f.frametype = AST_FRAME_VOICE;
00424    else
00425       rtp->f.frametype = AST_FRAME_VIDEO;
00426    rtp->lastrxformat = rtp->f.subclass;
00427 
00428    if (!rtp->lastrxts)
00429       rtp->lastrxts = timestamp;
00430 
00431    if (rtp->dtmfcount) {
00432 #if 0
00433       printf("dtmfcount was %d\n", rtp->dtmfcount);
00434 #endif      
00435       rtp->dtmfcount -= (timestamp - rtp->lastrxts);
00436       if (rtp->dtmfcount < 0)
00437          rtp->dtmfcount = 0;
00438 #if 0
00439       if (dtmftimeout != rtp->dtmfcount)
00440          printf("dtmfcount is %d\n", rtp->dtmfcount);
00441 #endif
00442    }
00443    rtp->lastrxts = timestamp;
00444 
00445    /* Send any pending DTMF */
00446    if (rtp->resp && !rtp->dtmfcount) {
00447       ast_log(LOG_DEBUG, "Sending pending DTMF\n");
00448       return send_dtmf(rtp);
00449    }
00450    rtp->f.mallocd = 0;
00451    rtp->f.datalen = res - hdrlen;
00452    rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
00453    rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
00454    if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
00455       switch(rtp->f.subclass) {
00456       case AST_FORMAT_ULAW:
00457       case AST_FORMAT_ALAW:
00458          rtp->f.samples = rtp->f.datalen;
00459          break;
00460       case AST_FORMAT_SLINEAR:
00461          rtp->f.samples = rtp->f.datalen / 2;
00462          break;
00463       case AST_FORMAT_GSM:
00464          rtp->f.samples = 160 * (rtp->f.datalen / 33);
00465          break;
00466       case AST_FORMAT_ILBC:
00467          rtp->f.samples = 240 * (rtp->f.datalen / 50);
00468          break;
00469       case AST_FORMAT_ADPCM:
00470          rtp->f.samples = rtp->f.datalen * 2;
00471          break;
00472       case AST_FORMAT_G729A:
00473          rtp->f.samples = rtp->f.datalen * 8;
00474          break;
00475       case AST_FORMAT_G723_1:
00476          rtp->f.samples = g723_samples(rtp->f.data, rtp->f.datalen);
00477          break;
00478       case AST_FORMAT_SPEEX:
00479               rtp->f.samples = 160;
00480          // assumes that the RTP packet contained one Speex frame
00481          break;
00482       default:
00483          ast_log(LOG_NOTICE, "Unable to calculate samples for format %s\n", ast_getformatname(rtp->f.subclass));
00484          break;
00485       }
00486    } else {
00487       /* Video -- samples is # of samples vs. 90000 */
00488       if (!rtp->lastividtimestamp)
00489          rtp->lastividtimestamp = timestamp;
00490       rtp->f.samples = timestamp - rtp->lastividtimestamp;
00491       rtp->lastividtimestamp = timestamp;
00492       if (mark)
00493          rtp->f.subclass |= 0x1;
00494       
00495    }
00496    rtp->f.src = "RTP";
00497    return &rtp->f;
00498 }

void ast_rtp_reload void   ) 
 

Definition at line 1300 of file rtp.c.

References ast_destroy(), ast_load(), ast_log(), ast_variable_retrieve(), ast_verbose(), LOG_WARNING, option_verbose, and VERBOSE_PREFIX_2.

Referenced by ast_module_reload(), and ast_rtp_init().

01301 {
01302    struct ast_config *cfg;
01303    char *s;
01304    rtpstart = 5000;
01305    rtpend = 31000;
01306    cfg = ast_load("rtp.conf");
01307    if (cfg) {
01308       if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
01309          rtpstart = atoi(s);
01310          if (rtpstart < 1024)
01311             rtpstart = 1024;
01312          if (rtpstart > 65535)
01313             rtpstart = 65535;
01314       }
01315       if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
01316          rtpend = atoi(s);
01317          if (rtpend < 1024)
01318             rtpend = 1024;
01319          if (rtpend > 65535)
01320             rtpend = 65535;
01321       }
01322       ast_destroy(cfg);
01323    }
01324    if (rtpstart >= rtpend) {
01325       ast_log(LOG_WARNING, "Unreasonable values for RTP start/end\n");
01326       rtpstart = 5000;
01327       rtpend = 31000;
01328    }
01329    if (option_verbose > 1)
01330       ast_verbose(VERBOSE_PREFIX_2 "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
01331 }

int ast_rtp_senddigit struct ast_rtp rtp,
char  digit
 

Definition at line 834 of file rtp.c.

References ast_log(), ast_rtp::lastts, LOG_NOTICE, LOG_WARNING, ast_rtp::s, ast_rtp::seqno, ast_rtp::ssrc, and ast_rtp::them.

00835 {
00836    unsigned int *rtpheader;
00837    int hdrlen = 12;
00838    int res;
00839    int ms;
00840    int pred;
00841    int x;
00842    char data[256];
00843 
00844    if ((digit <= '9') && (digit >= '0'))
00845       digit -= '0';
00846    else if (digit == '*')
00847       digit = 10;
00848    else if (digit == '#')
00849       digit = 11;
00850    else if ((digit >= 'A') && (digit <= 'D')) 
00851       digit = digit - 'A' + 12;
00852    else if ((digit >= 'a') && (digit <= 'd')) 
00853       digit = digit - 'a' + 12;
00854    else {
00855       ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
00856       return -1;
00857    }
00858    
00859 
00860    /* If we have no peer, return immediately */ 
00861    if (!rtp->them.sin_addr.s_addr)
00862       return 0;
00863 
00864    ms = calc_txstamp(rtp);
00865    /* Default prediction */
00866    pred = rtp->lastts + ms * 8;
00867    
00868    /* Get a pointer to the header */
00869    rtpheader = (unsigned int *)data;
00870    rtpheader[0] = htonl((2 << 30) | (1 << 23) | (101 << 16) | (rtp->seqno++));
00871    rtpheader[1] = htonl(rtp->lastts);
00872    rtpheader[2] = htonl(rtp->ssrc); 
00873    rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
00874    for (x=0;x<4;x++) {
00875       if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
00876          res = sendto(rtp->s, (void *)rtpheader, hdrlen + 4, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
00877          if (res <0) 
00878             ast_log(LOG_NOTICE, "RTP Transmission error to %s:%d: %s\n", inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
00879    #if 0
00880       printf("Sent %d bytes of RTP data to %s:%d\n", res, inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
00881    #endif      
00882       }
00883       if (x ==0) {
00884          /* Clear marker bit and increment seqno */
00885          rtpheader[0] = htonl((2 << 30)  | (101 << 16) | (rtp->seqno++));
00886          /* Make duration 240 */
00887          rtpheader[3] |= htonl((240));
00888          /* Set the End bit for the last 3 */
00889          rtpheader[3] |= htonl((1 << 23));
00890       }
00891    }
00892    return 0;
00893 }

void ast_rtp_set_callback struct ast_rtp rtp,
ast_rtp_callback  callback
 

Definition at line 153 of file rtp.c.

References ast_rtp_callback, and ast_rtp::callback.

00154 {
00155    rtp->callback = callback;
00156 }

void ast_rtp_set_data struct ast_rtp rtp,
void *  data
 

Definition at line 148 of file rtp.c.

References ast_rtp::data.

00149 {
00150    rtp->data = data;
00151 }

void ast_rtp_set_m_type struct ast_rtp rtp,
int  pt
 

Definition at line 585 of file rtp.c.

References rtpPayloadType::code, ast_rtp::current_RTP_PT, and MAX_RTP_PT.

00585                                                      {
00586   if (pt < 0 || pt > MAX_RTP_PT) return; // bogus payload type
00587 
00588   if (static_RTP_PT[pt].code != 0) {
00589     rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
00590   }
00591 } 

void ast_rtp_set_peer struct ast_rtp rtp,
struct sockaddr_in *  them
 

Definition at line 771 of file rtp.c.

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

00772 {
00773    rtp->them.sin_port = them->sin_port;
00774    rtp->them.sin_addr = them->sin_addr;
00775    if (rtp->rtcp) {
00776       rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
00777       rtp->rtcp->them.sin_addr = them->sin_addr;
00778    }
00779 }

void ast_rtp_set_rtpmap_type struct ast_rtp rtp,
int  pt,
char *  mimeType,
char *  mimeSubtype
 

Definition at line 595 of file rtp.c.

References ast_rtp::current_RTP_PT, MAX_RTP_PT, subtype, and type.

00596                                              {
00597   int i;
00598 
00599   if (pt < 0 || pt > MAX_RTP_PT) return; // bogus payload type
00600 
00601   for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
00602     if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
00603    strcasecmp(mimeType, mimeTypes[i].type) == 0) {
00604       rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
00605       return;
00606     }
00607   }
00608 } 

void ast_rtp_setnat struct ast_rtp rtp,
int  nat
 

Definition at line 158 of file rtp.c.

References ast_rtp::nat.

00159 {
00160    rtp->nat = nat;
00161 }

int ast_rtp_settos struct ast_rtp rtp,
int  tos
 

Definition at line 763 of file rtp.c.

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

00764 {
00765    int res;
00766    if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) 
00767       ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
00768    return res;
00769 }

void ast_rtp_stop struct ast_rtp rtp  ) 
 

Definition at line 793 of file rtp.c.

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

00794 {
00795    memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
00796    memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
00797    if (rtp->rtcp) {
00798       memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
00799       memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->them.sin_port));
00800    }
00801 }

int ast_rtp_write struct ast_rtp rtp,
struct ast_frame f
 

Definition at line 973 of file rtp.c.

References AST_FORMAT_ALAW, AST_FORMAT_G723_1, AST_FORMAT_G729A, AST_FORMAT_GSM, AST_FORMAT_H261, AST_FORMAT_H263, AST_FORMAT_ILBC, AST_FORMAT_SPEEX, AST_FORMAT_ULAW, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frdup(), ast_getformatname(), ast_log(), ast_rtp_lookup_code(), ast_smoother_feed(), ast_smoother_free(), ast_smoother_new(), ast_smoother_read(), ast_frame::datalen, ast_frame::frametype, ast_rtp::lasttxformat, LOG_DEBUG, LOG_WARNING, ast_frame::offset, ast_rtp::smoother, ast_frame::subclass, and ast_rtp::them.

00974 {
00975    struct ast_frame *f;
00976    int codec;
00977    int hdrlen = 12;
00978    int subclass;
00979    
00980 
00981    /* If we have no peer, return immediately */ 
00982    if (!rtp->them.sin_addr.s_addr)
00983       return 0;
00984 
00985    /* If there is no data length, return immediately */
00986    if (!_f->datalen) 
00987       return 0;
00988    
00989    /* Make sure we have enough space for RTP header */
00990    if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
00991       ast_log(LOG_WARNING, "RTP can only send voice\n");
00992       return -1;
00993    }
00994 
00995    subclass = _f->subclass;
00996    if (_f->frametype == AST_FRAME_VIDEO)
00997       subclass &= ~0x1;
00998 
00999    codec = ast_rtp_lookup_code(rtp, 1, subclass);
01000    if (codec < 0) {
01001       ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
01002       return -1;
01003    }
01004 
01005    if (rtp->lasttxformat != subclass) {
01006       /* New format, reset the smoother */
01007       ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
01008       rtp->lasttxformat = subclass;
01009       if (rtp->smoother)
01010          ast_smoother_free(rtp->smoother);
01011       rtp->smoother = NULL;
01012    }
01013 
01014 
01015    switch(subclass) {
01016    case AST_FORMAT_ULAW:
01017    case AST_FORMAT_ALAW:
01018       if (!rtp->smoother) {
01019          rtp->smoother = ast_smoother_new(160);
01020       }
01021       if (!rtp->smoother) {
01022          ast_log(LOG_WARNING, "Unable to create smoother :(\n");
01023          return -1;
01024       }
01025       ast_smoother_feed(rtp->smoother, _f);
01026       
01027       while((f = ast_smoother_read(rtp->smoother)))
01028          ast_rtp_raw_write(rtp, f, codec);
01029       break;
01030    case AST_FORMAT_G729A:
01031       if (!rtp->smoother) {
01032          rtp->smoother = ast_smoother_new(20);
01033       }
01034       if (!rtp->smoother) {
01035          ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
01036          return -1;
01037       }
01038       ast_smoother_feed(rtp->smoother, _f);
01039       
01040       while((f = ast_smoother_read(rtp->smoother)))
01041          ast_rtp_raw_write(rtp, f, codec);
01042       break;
01043    case AST_FORMAT_GSM:
01044       if (!rtp->smoother) {
01045          rtp->smoother = ast_smoother_new(33);
01046       }
01047       if (!rtp->smoother) {
01048          ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
01049          return -1;
01050       }
01051       ast_smoother_feed(rtp->smoother, _f);
01052       while((f = ast_smoother_read(rtp->smoother)))
01053          ast_rtp_raw_write(rtp, f, codec);
01054       break;
01055    case AST_FORMAT_ILBC:
01056       if (!rtp->smoother) {
01057          rtp->smoother = ast_smoother_new(50);
01058       }
01059       if (!rtp->smoother) {
01060          ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
01061          return -1;
01062       }
01063       ast_smoother_feed(rtp->smoother, _f);
01064       while((f = ast_smoother_read(rtp->smoother)))
01065          ast_rtp_raw_write(rtp, f, codec);
01066       break;
01067    default: 
01068       ast_log(LOG_WARNING, "Not sure about sending format %s packets\n", ast_getformatname(subclass));
01069       // fall through to...
01070    case AST_FORMAT_H261:
01071    case AST_FORMAT_H263:
01072    case AST_FORMAT_G723_1:
01073    case AST_FORMAT_SPEEX:
01074            // Don't buffer outgoing frames; send them one-per-packet:
01075       if (_f->offset < hdrlen) {
01076          f = ast_frdup(_f);
01077       } else {
01078          f = _f;
01079       }
01080       ast_rtp_raw_write(rtp, f, codec);
01081    }
01082       
01083    return 0;
01084 }


Generated on Fri Oct 31 07:05:17 2003 for Asterisk by doxygen 1.3.4