Wed Aug 11 01:07:40 2010

Asterisk developer's documentation


codec_dahdi.c File Reference

Translate between various formats natively through DAHDI transcoding. More...

#include "asterisk.h"
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/poll.h>
#include <dahdi/user.h>
#include "asterisk/lock.h"
#include "asterisk/translate.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/cli.h"
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "asterisk/linkedlists.h"
#include "asterisk/ulaw.h"

Include dependency graph for codec_dahdi.c:

Go to the source code of this file.

Data Structures

struct  channel_usage
struct  codec_dahdi_pvt
struct  format_map
struct  translator
struct  translators
 the list of translators More...

Defines

#define BUFFER_SIZE   8000
#define G723_SAMPLES   240
#define G729_SAMPLES   160

Functions

static void __reg_module (void)
static void __unreg_module (void)
static void build_translators (struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
static int dahdi_decoder_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
static struct ast_frame * dahdi_decoder_frameout (struct ast_trans_pvt *pvt)
static void dahdi_destroy (struct ast_trans_pvt *pvt)
static int dahdi_encoder_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
static struct ast_frame * dahdi_encoder_frameout (struct ast_trans_pvt *pvt)
static int dahdi_new (struct ast_trans_pvt *pvt)
static int dahdi_translate (struct ast_trans_pvt *pvt, int dest, int source)
static void dahdi_write_frame (struct codec_dahdi_pvt *dahdip, const uint8_t *buffer, const ssize_t count)
static void drop_translator (int dst, int src)
static struct ast_frame * fakesrc_sample (void)
static int find_transcoders (void)
static char * handle_cli_transcoder_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int is_encoder (struct translator *zt)
static int lintoulaw (struct ast_trans_pvt *pvt, struct ast_frame *f)
static int load_module (void)
static int register_translator (int dst, int src)
static int reload (void)
static int ulawtolin (struct ast_trans_pvt *pvt)
static int unload_module (void)
static void unregister_translators (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Generic DAHDI Transcoder Codec Translator" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, }
static struct ast_module_info * ast_module_info = &__mod_info
static struct channel_usage channels
static struct ast_cli_entry cli []
static struct format_map global_format_map = { { { 0 } } }


Detailed Description

Translate between various formats natively through DAHDI transcoding.

Definition in file codec_dahdi.c.


Define Documentation

#define BUFFER_SIZE   8000

Definition at line 54 of file codec_dahdi.c.

Referenced by register_translator().

#define G723_SAMPLES   240

Definition at line 56 of file codec_dahdi.c.

Referenced by dahdi_translate().

#define G729_SAMPLES   160

Definition at line 57 of file codec_dahdi.c.

Referenced by dahdi_translate().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 650 of file codec_dahdi.c.

static void __unreg_module ( void   )  [static]

Definition at line 650 of file codec_dahdi.c.

static void build_translators ( struct format_map *  map,
unsigned int  dstfmts,
unsigned int  srcfmts 
) [static]

Definition at line 553 of file codec_dahdi.c.

References format_map::map, and register_translator().

Referenced by find_transcoders().

00554 {
00555    unsigned int src, dst;
00556 
00557    for (src = 0; src < 32; src++) {
00558       for (dst = 0; dst < 32; dst++) {
00559          if (!(srcfmts & (1 << src)))
00560             continue;
00561 
00562          if (!(dstfmts & (1 << dst)))
00563             continue;
00564 
00565          if (global_format_map.map[dst][src])
00566             continue;
00567 
00568          if (!register_translator(dst, src))
00569             map->map[dst][src] = 1;
00570       }
00571    }
00572 }

static int dahdi_decoder_framein ( struct ast_trans_pvt *  pvt,
struct ast_frame *  f 
) [static]

Definition at line 275 of file codec_dahdi.c.

References ast_log(), dahdi_write_frame(), ast_frame::data, ast_trans_pvt::datalen, ast_frame::datalen, codec_dahdi_pvt::fake, LOG_ERROR, ast_frame::ptr, ast_trans_pvt::pvt, codec_dahdi_pvt::required_samples, ast_frame::samples, ast_trans_pvt::samples, and ast_frame::subclass.

Referenced by register_translator().

00276 {
00277    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00278 
00279    if (!f->subclass) {
00280       /* We're just faking a return for calculation purposes. */
00281       dahdip->fake = 2;
00282       pvt->samples = f->samples;
00283       return 0;
00284    }
00285 
00286    if (!f->datalen) {
00287       if (f->samples != dahdip->required_samples) {
00288          ast_log(LOG_ERROR, "%d != %d %d\n", f->samples, dahdip->required_samples, f->datalen);
00289       }
00290    }
00291    dahdi_write_frame(dahdip, f->data.ptr, f->datalen);
00292    pvt->samples += f->samples;
00293    pvt->datalen = 0;
00294    return -1;
00295 }

static struct ast_frame* dahdi_decoder_frameout ( struct ast_trans_pvt *  pvt  )  [static, read]

Definition at line 297 of file codec_dahdi.c.

References AST_FRAME_VOICE, AST_FRFLAG_FROM_TRANSLATOR, AST_FRIENDLY_OFFSET, ast_log(), ast_set_flag, ast_translator::buf_size, ast_trans_pvt::c, ast_frame::data, ast_trans_pvt::datalen, ast_frame::datalen, ast_translator::dstfmt, errno, ast_trans_pvt::f, codec_dahdi_pvt::fake, codec_dahdi_pvt::fd, ast_frame::frametype, LOG_ERROR, ast_frame::mallocd, ast_translator::name, ast_frame::offset, ast_trans_pvt::outbuf, ast_frame::ptr, ast_trans_pvt::pvt, codec_dahdi_pvt::required_samples, ast_trans_pvt::samples, ast_frame::samples, codec_dahdi_pvt::softslin, ast_frame::src, ast_frame::subclass, ast_trans_pvt::t, codec_dahdi_pvt::ulaw_buffer, and ulawtolin().

Referenced by register_translator().

00298 {
00299    int res;
00300    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00301 
00302    if (2 == dahdip->fake) {
00303       dahdip->fake = 1;
00304       pvt->f.frametype = AST_FRAME_VOICE;
00305       pvt->f.subclass = 0;
00306       pvt->f.samples = dahdip->required_samples;
00307       pvt->f.data.ptr = NULL;
00308       pvt->f.offset = 0;
00309       pvt->f.datalen = 0;
00310       pvt->f.mallocd = 0;
00311       ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
00312       pvt->samples = 0;
00313       return &pvt->f;
00314    } else if (1 == dahdip->fake) {
00315       pvt->samples = 0;
00316       dahdip->fake = 0;
00317       return NULL;
00318    }
00319 
00320    /* Let's check to see if there is a new frame for us.... */
00321    if (dahdip->softslin) {
00322       res = read(dahdip->fd, dahdip->ulaw_buffer, sizeof(dahdip->ulaw_buffer));
00323    } else {
00324       res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
00325    }
00326 
00327    if (-1 == res) {
00328       if (EWOULDBLOCK == errno) {
00329          /* Nothing waiting... */
00330          return NULL;
00331       } else {
00332          ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
00333          return NULL;
00334       }
00335    } else {
00336       if (dahdip->softslin) {
00337          ulawtolin(pvt);
00338          pvt->f.datalen = res * 2;
00339       } else {
00340          pvt->f.datalen = res;
00341       }
00342       pvt->datalen = 0;
00343       pvt->f.frametype = AST_FRAME_VOICE;
00344       pvt->f.subclass = 1 <<  (pvt->t->dstfmt);
00345       pvt->f.mallocd = 0;
00346       pvt->f.offset = AST_FRIENDLY_OFFSET;
00347       pvt->f.src = pvt->t->name;
00348       pvt->f.data.ptr = pvt->outbuf.c;
00349       pvt->f.samples = dahdip->required_samples;
00350       ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
00351       pvt->samples = 0;
00352 
00353       return &pvt->f;
00354    }
00355 
00356    /* Shouldn't get here... */
00357    return NULL;
00358 }

static void dahdi_destroy ( struct ast_trans_pvt *  pvt  )  [static]

Definition at line 361 of file codec_dahdi.c.

References ast_atomic_fetchadd_int(), AST_FORMAT_G723_1, AST_FORMAT_G729A, codec_dahdi_pvt::fd, codec_dahdi_pvt::fmts, and ast_trans_pvt::pvt.

Referenced by register_translator().

00362 {
00363    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00364 
00365    switch (dahdip->fmts.dstfmt) {
00366    case AST_FORMAT_G729A:
00367    case AST_FORMAT_G723_1:
00368       ast_atomic_fetchadd_int(&channels.encoders, -1);
00369       break;
00370    default:
00371       ast_atomic_fetchadd_int(&channels.decoders, -1);
00372       break;
00373    }
00374 
00375    close(dahdip->fd);
00376 }

static int dahdi_encoder_framein ( struct ast_trans_pvt *  pvt,
struct ast_frame *  f 
) [static]

Definition at line 178 of file codec_dahdi.c.

References ast_log(), dahdi_write_frame(), ast_frame::data, ast_trans_pvt::datalen, codec_dahdi_pvt::fake, lintoulaw(), LOG_ERROR, ast_frame::ptr, ast_trans_pvt::pvt, codec_dahdi_pvt::required_samples, ast_frame::samples, ast_trans_pvt::samples, codec_dahdi_pvt::samples_in_buffer, codec_dahdi_pvt::softslin, ast_frame::subclass, and codec_dahdi_pvt::ulaw_buffer.

Referenced by register_translator().

00179 {
00180    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00181 
00182    if (!f->subclass) {
00183       /* We're just faking a return for calculation purposes. */
00184       dahdip->fake = 2;
00185       pvt->samples = f->samples;
00186       return 0;
00187    }
00188 
00189    /* Buffer up the packets and send them to the hardware if we
00190     * have enough samples set up. */
00191    if (dahdip->softslin) {
00192       if (lintoulaw(pvt, f)) {
00193           return -1;
00194       }
00195    } else {
00196       /* NOTE:  If softslin support is not needed, and the sample
00197        * size is equal to the required sample size, we wouldn't
00198        * need this copy operation.  But at the time this was
00199        * written, only softslin is supported. */
00200       if (dahdip->samples_in_buffer + f->samples > sizeof(dahdip->ulaw_buffer)) {
00201          ast_log(LOG_ERROR, "Out of buffer space.\n");
00202          return -1;
00203       }
00204       memcpy(&dahdip->ulaw_buffer[dahdip->samples_in_buffer], f->data.ptr, f->samples);
00205       dahdip->samples_in_buffer += f->samples;
00206    }
00207 
00208    while (dahdip->samples_in_buffer > dahdip->required_samples) {
00209       dahdi_write_frame(dahdip, dahdip->ulaw_buffer, dahdip->required_samples);
00210       dahdip->samples_in_buffer -= dahdip->required_samples;
00211       if (dahdip->samples_in_buffer) {
00212          /* Shift any remaining bytes down. */
00213          memmove(dahdip->ulaw_buffer, &dahdip->ulaw_buffer[dahdip->required_samples],
00214             dahdip->samples_in_buffer);
00215       }
00216    }
00217    pvt->samples += f->samples;
00218    pvt->datalen = 0;
00219    return -1;
00220 }

static struct ast_frame* dahdi_encoder_frameout ( struct ast_trans_pvt *  pvt  )  [static, read]

Definition at line 222 of file codec_dahdi.c.

References AST_FRAME_VOICE, AST_FRFLAG_FROM_TRANSLATOR, AST_FRIENDLY_OFFSET, ast_log(), ast_set_flag, ast_translator::buf_size, ast_trans_pvt::c, ast_frame::data, ast_trans_pvt::datalen, ast_frame::datalen, ast_translator::dstfmt, errno, ast_trans_pvt::f, codec_dahdi_pvt::fake, codec_dahdi_pvt::fd, ast_frame::frametype, LOG_ERROR, ast_frame::mallocd, ast_translator::name, ast_frame::offset, ast_trans_pvt::outbuf, ast_frame::ptr, ast_trans_pvt::pvt, codec_dahdi_pvt::required_samples, ast_trans_pvt::samples, ast_frame::samples, ast_frame::src, ast_frame::subclass, and ast_trans_pvt::t.

Referenced by register_translator().

00223 {
00224    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00225    int res;
00226 
00227    if (2 == dahdip->fake) {
00228       dahdip->fake = 1;
00229       pvt->f.frametype = AST_FRAME_VOICE;
00230       pvt->f.subclass = 0;
00231       pvt->f.samples = dahdip->required_samples;
00232       pvt->f.data.ptr = NULL;
00233       pvt->f.offset = 0;
00234       pvt->f.datalen = 0;
00235       pvt->f.mallocd = 0;
00236       ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
00237       pvt->samples = 0;
00238 
00239       return &pvt->f;
00240 
00241    } else if (1 == dahdip->fake) {
00242       dahdip->fake = 0;
00243       return NULL;
00244    }
00245 
00246    res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
00247    if (-1 == res) {
00248       if (EWOULDBLOCK == errno) {
00249          /* Nothing waiting... */
00250          return NULL;
00251       } else {
00252          ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
00253          return NULL;
00254       }
00255    } else {
00256       pvt->f.datalen = res;
00257       pvt->f.samples = dahdip->required_samples;
00258       pvt->f.frametype = AST_FRAME_VOICE;
00259       pvt->f.subclass = 1 <<  (pvt->t->dstfmt);
00260       pvt->f.mallocd = 0;
00261       pvt->f.offset = AST_FRIENDLY_OFFSET;
00262       pvt->f.src = pvt->t->name;
00263       pvt->f.data.ptr = pvt->outbuf.c;
00264       ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
00265 
00266       pvt->samples = 0;
00267       pvt->datalen = 0;
00268       return &pvt->f;
00269    }
00270 
00271    /* Shouldn't get here... */
00272    return NULL;
00273 }

static int dahdi_new ( struct ast_trans_pvt *  pvt  )  [static]

Definition at line 451 of file codec_dahdi.c.

References dahdi_translate(), ast_translator::dstfmt, ast_translator::srcfmt, and ast_trans_pvt::t.

Referenced by register_translator().

00452 {
00453    return dahdi_translate(pvt, pvt->t->dstfmt, pvt->t->srcfmt);
00454 }

static int dahdi_translate ( struct ast_trans_pvt *  pvt,
int  dest,
int  source 
) [static]

Definition at line 378 of file codec_dahdi.c.

References ast_atomic_fetchadd_int(), ast_debug, AST_FORMAT_G723_1, AST_FORMAT_G729A, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, ast_log(), errno, codec_dahdi_pvt::fd, codec_dahdi_pvt::fmts, G723_SAMPLES, G729_SAMPLES, LOG_ERROR, LOG_WARNING, ast_trans_pvt::pvt, codec_dahdi_pvt::required_samples, and codec_dahdi_pvt::softslin.

Referenced by dahdi_new().

00379 {
00380    /* Request translation through zap if possible */
00381    int fd;
00382    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00383    int flags;
00384    int tried_once = 0;
00385    const char *dev_filename = "/dev/dahdi/transcode";
00386 
00387    if ((fd = open(dev_filename, O_RDWR)) < 0) {
00388       ast_log(LOG_ERROR, "Failed to open %s: %s\n", dev_filename, strerror(errno));
00389       return -1;
00390    }
00391 
00392    dahdip->fmts.srcfmt = (1 << source);
00393    dahdip->fmts.dstfmt = (1 << dest);
00394 
00395    ast_debug(1, "Opening transcoder channel from %d to %d.\n", source, dest);
00396 
00397 retry:
00398    if (ioctl(fd, DAHDI_TC_ALLOCATE, &dahdip->fmts)) {
00399       if ((ENODEV == errno) && !tried_once) {
00400          /* We requested to translate to/from an unsupported
00401           * format.  Most likely this is because signed linear
00402           * was not supported by any hardware devices even
00403           * though this module always registers signed linear
00404           * support. In this case we'll retry, requesting
00405           * support for ULAW instead of signed linear and then
00406           * we'll just convert from ulaw to signed linear in
00407           * software. */
00408          if (AST_FORMAT_SLINEAR == dahdip->fmts.srcfmt) {
00409             ast_debug(1, "Using soft_slin support on source\n");
00410             dahdip->softslin = 1;
00411             dahdip->fmts.srcfmt = AST_FORMAT_ULAW;
00412          } else if (AST_FORMAT_SLINEAR == dahdip->fmts.dstfmt) {
00413             ast_debug(1, "Using soft_slin support on destination\n");
00414             dahdip->softslin = 1;
00415             dahdip->fmts.dstfmt = AST_FORMAT_ULAW;
00416          }
00417          tried_once = 1;
00418          goto retry;
00419       }
00420       ast_log(LOG_ERROR, "Unable to attach to transcoder: %s\n", strerror(errno));
00421       close(fd);
00422 
00423       return -1;
00424    }
00425 
00426    flags = fcntl(fd, F_GETFL);
00427    if (flags > - 1) {
00428       if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
00429          ast_log(LOG_WARNING, "Could not set non-block mode!\n");
00430    }
00431 
00432    dahdip->fd = fd;
00433 
00434    dahdip->required_samples = ((dahdip->fmts.dstfmt|dahdip->fmts.srcfmt)&AST_FORMAT_G723_1) ? G723_SAMPLES : G729_SAMPLES;
00435 
00436    switch (dahdip->fmts.dstfmt) {
00437    case AST_FORMAT_G729A:
00438       ast_atomic_fetchadd_int(&channels.encoders, +1);
00439       break;
00440    case AST_FORMAT_G723_1:
00441       ast_atomic_fetchadd_int(&channels.encoders, +1);
00442       break;
00443    default:
00444       ast_atomic_fetchadd_int(&channels.decoders, +1);
00445       break;
00446    }
00447 
00448    return 0;
00449 }

static void dahdi_write_frame ( struct codec_dahdi_pvt *  dahdip,
const uint8_t *  buffer,
const ssize_t  count 
) [static]

Definition at line 159 of file codec_dahdi.c.

References ast_log(), errno, codec_dahdi_pvt::fd, LOG_ERROR, and option_verbose.

Referenced by dahdi_decoder_framein(), and dahdi_encoder_framein().

00160 {
00161    int res;
00162    struct pollfd p = {0};
00163    if (!count) return;
00164    res = write(dahdip->fd, buffer, count);
00165    if (option_verbose > 10) {
00166       if (-1 == res) {
00167          ast_log(LOG_ERROR, "Failed to write to transcoder: %s\n", strerror(errno));
00168       }
00169       if (count != res) {
00170          ast_log(LOG_ERROR, "Requested write of %zd bytes, but only wrote %d bytes.\n", count, res);
00171       }
00172    }
00173    p.fd = dahdip->fd;
00174    p.events = POLLOUT;
00175    res = poll(&p, 1, 50);
00176 }

static void drop_translator ( int  dst,
int  src 
) [static]

Definition at line 519 of file codec_dahdi.c.

References ast_free, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_unregister_translator(), ast_translator::dstfmt, translator::entry, format_map::map, ast_translator::srcfmt, and translator::t.

Referenced by find_transcoders().

00520 {
00521    struct translator *cur;
00522 
00523    AST_LIST_LOCK(&translators);
00524    AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, cur, entry) {
00525       if (cur->t.srcfmt != src)
00526          continue;
00527 
00528       if (cur->t.dstfmt != dst)
00529          continue;
00530 
00531       AST_LIST_REMOVE_CURRENT(entry);
00532       ast_unregister_translator(&cur->t);
00533       ast_free(cur);
00534       global_format_map.map[dst][src] = 0;
00535       break;
00536    }
00537    AST_LIST_TRAVERSE_SAFE_END;
00538    AST_LIST_UNLOCK(&translators);
00539 }

static struct ast_frame* fakesrc_sample ( void   )  [static, read]

Definition at line 456 of file codec_dahdi.c.

References AST_FRAME_VOICE, and ast_frame::frametype.

Referenced by register_translator().

00457 {
00458    /* Don't bother really trying to test hardware ones. */
00459    static struct ast_frame f = {
00460       .frametype = AST_FRAME_VOICE,
00461       .samples = 160,
00462       .src = __PRETTY_FUNCTION__
00463    };
00464 
00465    return &f;
00466 }

static int find_transcoders ( void   )  [static]

Definition at line 574 of file codec_dahdi.c.

References ast_atomic_fetchadd_int(), AST_FORMAT_ALAW, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, ast_log(), ast_verbose, build_translators(), drop_translator(), errno, LOG_ERROR, format_map::map, option_verbose, and VERBOSE_PREFIX_2.

Referenced by load_module().

00575 {
00576    struct dahdi_transcoder_info info = { 0, };
00577    struct format_map map = { { { 0 } } };
00578    int fd, res;
00579    unsigned int x, y;
00580 
00581    if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
00582       ast_log(LOG_ERROR, "Failed to open /dev/dahdi/transcode: %s\n", strerror(errno));
00583       return 0;
00584    }
00585 
00586    for (info.tcnum = 0; !(res = ioctl(fd, DAHDI_TC_GETINFO, &info)); info.tcnum++) {
00587       if (option_verbose > 1)
00588          ast_verbose(VERBOSE_PREFIX_2 "Found transcoder '%s'.\n", info.name);
00589 
00590       /* Complex codecs need to support signed linear.  If the
00591        * hardware transcoder does not natively support signed linear
00592        * format, we will emulate it in software directly in this
00593        * module. Also, do not allow direct ulaw/alaw to complex
00594        * codec translation, since that will prevent the generic PLC
00595        * functions from working. */
00596       if (info.dstfmts & (AST_FORMAT_ULAW | AST_FORMAT_ALAW)) {
00597          info.dstfmts |= AST_FORMAT_SLINEAR;
00598          info.dstfmts &= ~(AST_FORMAT_ULAW | AST_FORMAT_ALAW);
00599       }
00600       if (info.srcfmts & (AST_FORMAT_ULAW | AST_FORMAT_ALAW)) {
00601          info.srcfmts |= AST_FORMAT_SLINEAR;
00602          info.srcfmts &= ~(AST_FORMAT_ULAW | AST_FORMAT_ALAW);
00603       }
00604 
00605       build_translators(&map, info.dstfmts, info.srcfmts);
00606       ast_atomic_fetchadd_int(&channels.total, info.numchannels / 2);
00607 
00608    }
00609 
00610    close(fd);
00611 
00612    if (!info.tcnum && (option_verbose > 1))
00613       ast_verbose(VERBOSE_PREFIX_2 "No hardware transcoders found.\n");
00614 
00615    for (x = 0; x < 32; x++) {
00616       for (y = 0; y < 32; y++) {
00617          if (!map.map[x][y] && global_format_map.map[x][y])
00618             drop_translator(x, y);
00619       }
00620    }
00621 
00622    return 0;
00623 }

static char * handle_cli_transcoder_show ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 131 of file codec_dahdi.c.

References ast_cli_args::argc, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, channel_usage::decoders, channel_usage::encoders, ast_cli_args::fd, channel_usage::total, and ast_cli_entry::usage.

00132 {
00133    struct channel_usage copy;
00134 
00135    switch (cmd) {
00136    case CLI_INIT:
00137       e->command = "transcoder show";
00138       e->usage =
00139          "Usage: transcoder show\n"
00140          "       Displays channel utilization of DAHDI transcoder(s).\n";
00141       return NULL;
00142    case CLI_GENERATE:
00143       return NULL;
00144    }
00145 
00146    if (a->argc != 2)
00147       return CLI_SHOWUSAGE;
00148 
00149    copy = channels;
00150 
00151    if (copy.total == 0)
00152       ast_cli(a->fd, "No DAHDI transcoders found.\n");
00153    else
00154       ast_cli(a->fd, "%d/%d encoders/decoders of %d channels are in use.\n", copy.encoders, copy.decoders, copy.total);
00155 
00156    return CLI_SUCCESS;
00157 }

static int is_encoder ( struct translator *  zt  )  [static]

Definition at line 468 of file codec_dahdi.c.

References AST_FORMAT_ALAW, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, ast_translator::srcfmt, and translator::t.

Referenced by register_translator().

00469 {
00470    if (zt->t.srcfmt&(AST_FORMAT_ULAW|AST_FORMAT_ALAW|AST_FORMAT_SLINEAR)) {
00471       return 1;
00472    } else {
00473       return 0;
00474    }
00475 }

static int lintoulaw ( struct ast_trans_pvt *  pvt,
struct ast_frame *  f 
) [static]

Definition at line 111 of file codec_dahdi.c.

References AST_LIN2MU, ast_log(), ast_frame::data, LOG_ERROR, ast_frame::ptr, ast_trans_pvt::pvt, ast_frame::samples, codec_dahdi_pvt::samples_in_buffer, and codec_dahdi_pvt::ulaw_buffer.

Referenced by dahdi_encoder_framein().

00112 {
00113    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00114    int i = f->samples;
00115    uint8_t *dst = &dahdip->ulaw_buffer[dahdip->samples_in_buffer];
00116    int16_t *src = f->data.ptr;
00117 
00118    if (dahdip->samples_in_buffer + i > sizeof(dahdip->ulaw_buffer)) {
00119       ast_log(LOG_ERROR, "Out of buffer space!\n");
00120       return -i;
00121    }
00122 
00123    while (i--) {
00124       *dst++ = AST_LIN2MU(*src++);
00125    }
00126 
00127    dahdip->samples_in_buffer += f->samples;
00128    return 0;
00129 }

static int load_module ( void   )  [static]

static int register_translator ( int  dst,
int  src 
) [static]

Definition at line 477 of file codec_dahdi.c.

References ast_calloc, ast_free, ast_getformatname(), AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_register_translator, ast_translator::buf_size, ast_translator::buffer_samples, BUFFER_SIZE, dahdi_decoder_framein(), dahdi_decoder_frameout(), dahdi_destroy(), dahdi_encoder_framein(), dahdi_encoder_frameout(), dahdi_new(), ast_translator::desc_size, ast_translator::destroy, ast_translator::dstfmt, translator::entry, fakesrc_sample(), ast_translator::framein, ast_translator::frameout, is_encoder(), format_map::map, ast_translator::name, ast_translator::native_plc, ast_translator::newpvt, ast_translator::sample, ast_translator::srcfmt, and translator::t.

Referenced by build_translators().

00478 {
00479    struct translator *zt;
00480    int res;
00481 
00482    if (!(zt = ast_calloc(1, sizeof(*zt)))) {
00483       return -1;
00484    }
00485 
00486    snprintf((char *) (zt->t.name), sizeof(zt->t.name), "zap%sto%s",
00487        ast_getformatname((1 << src)), ast_getformatname((1 << dst)));
00488    zt->t.srcfmt = (1 << src);
00489    zt->t.dstfmt = (1 << dst);
00490    zt->t.buf_size = BUFFER_SIZE;
00491    if (is_encoder(zt)) {
00492       zt->t.framein = dahdi_encoder_framein;
00493       zt->t.frameout = dahdi_encoder_frameout;
00494    } else {
00495       zt->t.framein = dahdi_decoder_framein;
00496       zt->t.frameout = dahdi_decoder_frameout;
00497    }
00498    zt->t.destroy = dahdi_destroy;
00499    zt->t.buffer_samples = 0;
00500    zt->t.newpvt = dahdi_new;
00501    zt->t.sample = fakesrc_sample;
00502    zt->t.native_plc = 0;
00503 
00504    zt->t.desc_size = sizeof(struct codec_dahdi_pvt);
00505    if ((res = ast_register_translator(&zt->t))) {
00506       ast_free(zt);
00507       return -1;
00508    }
00509 
00510    AST_LIST_LOCK(&translators);
00511    AST_LIST_INSERT_HEAD(&translators, zt, entry);
00512    AST_LIST_UNLOCK(&translators);
00513 
00514    global_format_map.map[dst][src] = 1;
00515 
00516    return res;
00517 }

static int reload ( void   )  [static]

Definition at line 625 of file codec_dahdi.c.

References AST_MODULE_LOAD_SUCCESS.

00626 {
00627    return AST_MODULE_LOAD_SUCCESS;
00628 }

static int ulawtolin ( struct ast_trans_pvt *  pvt  )  [static]

Definition at line 95 of file codec_dahdi.c.

References AST_MULAW, ast_trans_pvt::datalen, ast_trans_pvt::i16, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, codec_dahdi_pvt::required_samples, and codec_dahdi_pvt::ulaw_buffer.

Referenced by dahdi_decoder_frameout().

00096 {
00097    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00098    int i = dahdip->required_samples;
00099    uint8_t *src = &dahdip->ulaw_buffer[0];
00100    int16_t *dst = pvt->outbuf.i16 + pvt->datalen;
00101 
00102    /* convert and copy in outbuf */
00103    while (i--) {
00104       *dst++ = AST_MULAW(*src++);
00105    }
00106 
00107    return 0;
00108 }

static int unload_module ( void   )  [static]

Definition at line 630 of file codec_dahdi.c.

References ARRAY_LEN, ast_cli_unregister_multiple(), and unregister_translators().

00631 {
00632    ast_cli_unregister_multiple(cli, ARRAY_LEN(cli));
00633    unregister_translators();
00634 
00635    return 0;
00636 }

static void unregister_translators ( void   )  [static]

Definition at line 541 of file codec_dahdi.c.

References ast_free, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, ast_unregister_translator(), translator::entry, and translator::t.

Referenced by unload_module().

00542 {
00543    struct translator *cur;
00544 
00545    AST_LIST_LOCK(&translators);
00546    while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
00547       ast_unregister_translator(&cur->t);
00548       ast_free(cur);
00549    }
00550    AST_LIST_UNLOCK(&translators);
00551 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Generic DAHDI Transcoder Codec Translator" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, } [static]

Definition at line 650 of file codec_dahdi.c.

Definition at line 650 of file codec_dahdi.c.

struct channel_usage channels [static]

struct ast_cli_entry cli[] [static]

Initial value:

 {
   AST_CLI_DEFINE(handle_cli_transcoder_show, "Display DAHDI transcoder utilization.")
}

Definition at line 67 of file codec_dahdi.c.

struct format_map global_format_map = { { { 0 } } } [static]

Definition at line 75 of file codec_dahdi.c.


Generated on Wed Aug 11 01:07:41 2010 for Asterisk - the Open Source PBX by  doxygen 1.5.9