codec_dahdi.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * DAHDI native transcoding support
00005  *
00006  * Copyright (C) 1999 - 2008, Digium, Inc.
00007  *
00008  * Mark Spencer <markster@digium.com>
00009  * Kevin P. Fleming <kpfleming@digium.com>
00010  *
00011  * See http://www.asterisk.org for more information about
00012  * the Asterisk project. Please do not directly contact
00013  * any of the maintainers of this project for assistance;
00014  * the project provides a web site, mailing lists and IRC
00015  * channels for your use.
00016  *
00017  * This program is free software, distributed under the terms of
00018  * the GNU General Public License Version 2. See the LICENSE file
00019  * at the top of the source tree.
00020  */
00021 
00022 /*! \file
00023  *
00024  * \brief Translate between various formats natively through DAHDI transcoding
00025  *
00026  * \ingroup codecs
00027  */
00028 
00029 /*** MODULEINFO
00030    <support_level>core</support_level>
00031    <depend>dahdi</depend>
00032  ***/
00033 
00034 #include "asterisk.h"
00035 #include <stdbool.h>
00036 
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 426099 $")
00038 
00039 #include <fcntl.h>
00040 #include <netinet/in.h>
00041 #include <sys/ioctl.h>
00042 #include <sys/mman.h>
00043 #include <sys/poll.h>
00044 #include <dahdi/user.h>
00045 
00046 #include "asterisk/lock.h"
00047 #include "asterisk/translate.h"
00048 #include "asterisk/config.h"
00049 #include "asterisk/module.h"
00050 #include "asterisk/cli.h"
00051 #include "asterisk/channel.h"
00052 #include "asterisk/utils.h"
00053 #include "asterisk/linkedlists.h"
00054 #include "asterisk/ulaw.h"
00055 #include "asterisk/format_compatibility.h"
00056 
00057 #define BUFFER_SIZE 8000
00058 
00059 #define G723_SAMPLES 240
00060 #define G729_SAMPLES 160
00061 #define ULAW_SAMPLES 160
00062 
00063 /* Defines from DAHDI. */
00064 #ifndef DAHDI_FORMAT_MAX_AUDIO
00065 /*! G.723.1 compression */
00066 #define DAHDI_FORMAT_G723_1    (1 << 0)
00067 /*! GSM compression */
00068 #define DAHDI_FORMAT_GSM       (1 << 1)
00069 /*! Raw mu-law data (G.711) */
00070 #define DAHDI_FORMAT_ULAW      (1 << 2)
00071 /*! Raw A-law data (G.711) */
00072 #define DAHDI_FORMAT_ALAW      (1 << 3)
00073 /*! ADPCM (G.726, 32kbps) */
00074 #define DAHDI_FORMAT_G726      (1 << 4)
00075 /*! ADPCM (IMA) */
00076 #define DAHDI_FORMAT_ADPCM     (1 << 5)
00077 /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
00078 #define DAHDI_FORMAT_SLINEAR   (1 << 6)
00079 /*! LPC10, 180 samples/frame */
00080 #define DAHDI_FORMAT_LPC10     (1 << 7)
00081 /*! G.729A audio */
00082 #define DAHDI_FORMAT_G729A     (1 << 8)
00083 /*! SpeeX Free Compression */
00084 #define DAHDI_FORMAT_SPEEX     (1 << 9)
00085 /*! iLBC Free Compression */
00086 #define DAHDI_FORMAT_ILBC      (1 << 10)
00087 #endif
00088 
00089 static struct channel_usage {
00090    int total;
00091    int encoders;
00092    int decoders;
00093 } channels;
00094 
00095 #if defined(NOT_NEEDED)
00096 /*!
00097  * \internal
00098  * \brief Convert DAHDI format bitfield to old Asterisk format bitfield.
00099  * \since 13.0.0
00100  *
00101  * \param dahdi Bitfield from DAHDI to convert.
00102  *
00103  * \note They should be the same values but they don't have to be.
00104  *
00105  * \return Old Asterisk bitfield equivalent.
00106  */
00107 static uint64_t bitfield_dahdi2ast(unsigned dahdi)
00108 {
00109    uint64_t ast;
00110 
00111    switch (dahdi) {
00112    case DAHDI_FORMAT_G723_1:
00113       ast = AST_FORMAT_G723;
00114       break;
00115    case DAHDI_FORMAT_GSM:
00116       ast = AST_FORMAT_GSM;
00117       break;
00118    case DAHDI_FORMAT_ULAW:
00119       ast = AST_FORMAT_ULAW;
00120       break;
00121    case DAHDI_FORMAT_ALAW:
00122       ast = AST_FORMAT_ALAW;
00123       break;
00124    case DAHDI_FORMAT_G726:
00125       ast = AST_FORMAT_G726_AAL2;
00126       break;
00127    case DAHDI_FORMAT_ADPCM:
00128       ast = AST_FORMAT_ADPCM;
00129       break;
00130    case DAHDI_FORMAT_SLINEAR:
00131       ast = AST_FORMAT_SLIN;
00132       break;
00133    case DAHDI_FORMAT_LPC10:
00134       ast = AST_FORMAT_LPC10;
00135       break;
00136    case DAHDI_FORMAT_G729A:
00137       ast = AST_FORMAT_G729;
00138       break;
00139    case DAHDI_FORMAT_SPEEX:
00140       ast = AST_FORMAT_SPEEX;
00141       break;
00142    case DAHDI_FORMAT_ILBC:
00143       ast = AST_FORMAT_ILBC;
00144       break;
00145    default:
00146       ast = 0;
00147       break;
00148    }
00149 
00150    return ast;
00151 }
00152 #endif   /* defined(NOT_NEEDED) */
00153 
00154 /*!
00155  * \internal
00156  * \brief Get the ast_codec by DAHDI format.
00157  * \since 13.0.0
00158  *
00159  * \param dahdi_fmt DAHDI specific codec identifier.
00160  *
00161  * \return Specified codec if exists otherwise NULL.
00162  */
00163 static const struct ast_codec *get_dahdi_codec(uint32_t dahdi_fmt)
00164 {
00165    const struct ast_codec *codec;
00166 
00167    static const struct ast_codec dahdi_g723_1 = {
00168       .name = "g723",
00169       .type = AST_MEDIA_TYPE_AUDIO,
00170       .sample_rate = 8000,
00171    };
00172    static const struct ast_codec dahdi_gsm = {
00173       .name = "gsm",
00174       .type = AST_MEDIA_TYPE_AUDIO,
00175       .sample_rate = 8000,
00176    };
00177    static const struct ast_codec dahdi_ulaw = {
00178       .name = "ulaw",
00179       .type = AST_MEDIA_TYPE_AUDIO,
00180       .sample_rate = 8000,
00181    };
00182    static const struct ast_codec dahdi_alaw = {
00183       .name = "alaw",
00184       .type = AST_MEDIA_TYPE_AUDIO,
00185       .sample_rate = 8000,
00186    };
00187    static const struct ast_codec dahdi_g726 = {
00188       .name = "g726",
00189       .type = AST_MEDIA_TYPE_AUDIO,
00190       .sample_rate = 8000,
00191    };
00192    static const struct ast_codec dahdi_adpcm = {
00193       .name = "adpcm",
00194       .type = AST_MEDIA_TYPE_AUDIO,
00195       .sample_rate = 8000,
00196    };
00197    static const struct ast_codec dahdi_slinear = {
00198       .name = "slin",
00199       .type = AST_MEDIA_TYPE_AUDIO,
00200       .sample_rate = 8000,
00201    };
00202    static const struct ast_codec dahdi_lpc10 = {
00203       .name = "lpc10",
00204       .type = AST_MEDIA_TYPE_AUDIO,
00205       .sample_rate = 8000,
00206    };
00207    static const struct ast_codec dahdi_g729a = {
00208       .name = "g729",
00209       .type = AST_MEDIA_TYPE_AUDIO,
00210       .sample_rate = 8000,
00211    };
00212    static const struct ast_codec dahdi_speex = {
00213       .name = "speex",
00214       .type = AST_MEDIA_TYPE_AUDIO,
00215       .sample_rate = 8000,
00216    };
00217    static const struct ast_codec dahdi_ilbc = {
00218       .name = "ilbc",
00219       .type = AST_MEDIA_TYPE_AUDIO,
00220       .sample_rate = 8000,
00221    };
00222 
00223    switch (dahdi_fmt) {
00224    case DAHDI_FORMAT_G723_1:
00225       codec = &dahdi_g723_1;
00226       break;
00227    case DAHDI_FORMAT_GSM:
00228       codec = &dahdi_gsm;
00229       break;
00230    case DAHDI_FORMAT_ULAW:
00231       codec = &dahdi_ulaw;
00232       break;
00233    case DAHDI_FORMAT_ALAW:
00234       codec = &dahdi_alaw;
00235       break;
00236    case DAHDI_FORMAT_G726:
00237       codec = &dahdi_g726;
00238       break;
00239    case DAHDI_FORMAT_ADPCM:
00240       codec = &dahdi_adpcm;
00241       break;
00242    case DAHDI_FORMAT_SLINEAR:
00243       codec = &dahdi_slinear;
00244       break;
00245    case DAHDI_FORMAT_LPC10:
00246       codec = &dahdi_lpc10;
00247       break;
00248    case DAHDI_FORMAT_G729A:
00249       codec = &dahdi_g729a;
00250       break;
00251    case DAHDI_FORMAT_SPEEX:
00252       codec = &dahdi_speex;
00253       break;
00254    case DAHDI_FORMAT_ILBC:
00255       codec = &dahdi_ilbc;
00256       break;
00257    default:
00258       codec = NULL;
00259       break;
00260    }
00261 
00262    return codec;
00263 }
00264 
00265 static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
00266 
00267 static struct ast_cli_entry cli[] = {
00268    AST_CLI_DEFINE(handle_cli_transcoder_show, "Display DAHDI transcoder utilization.")
00269 };
00270 
00271 struct translator {
00272    struct ast_translator t;
00273    uint32_t src_dahdi_fmt;
00274    uint32_t dst_dahdi_fmt;
00275    AST_LIST_ENTRY(translator) entry;
00276 };
00277 
00278 #ifndef container_of
00279 #define container_of(ptr, type, member) \
00280    ((type *)((char *)(ptr) - offsetof(type, member)))
00281 #endif
00282 
00283 static AST_LIST_HEAD_STATIC(translators, translator);
00284 
00285 struct codec_dahdi_pvt {
00286    int fd;
00287    struct dahdi_transcoder_formats fmts;
00288    unsigned int softslin:1;
00289    unsigned int fake:2;
00290    uint16_t required_samples;
00291    uint16_t samples_in_buffer;
00292    uint16_t samples_written_to_hardware;
00293    uint8_t ulaw_buffer[1024];
00294 };
00295 
00296 /* Only used by a decoder */
00297 static int ulawtolin(struct ast_trans_pvt *pvt, int samples)
00298 {
00299    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00300    int i = samples;
00301    uint8_t *src = &dahdip->ulaw_buffer[0];
00302    int16_t *dst = pvt->outbuf.i16 + pvt->datalen;
00303 
00304    /* convert and copy in outbuf */
00305    while (i--) {
00306       *dst++ = AST_MULAW(*src++);
00307    }
00308 
00309    return 0;
00310 }
00311 
00312 /* Only used by an encoder. */
00313 static int lintoulaw(struct ast_trans_pvt *pvt, struct ast_frame *f)
00314 {
00315    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00316    int i = f->samples;
00317    uint8_t *dst = &dahdip->ulaw_buffer[dahdip->samples_in_buffer];
00318    int16_t *src = f->data.ptr;
00319 
00320    if (dahdip->samples_in_buffer + i > sizeof(dahdip->ulaw_buffer)) {
00321       ast_log(LOG_ERROR, "Out of buffer space!\n");
00322       return -i;
00323    }
00324 
00325    while (i--) {
00326       *dst++ = AST_LIN2MU(*src++);
00327    }
00328 
00329    dahdip->samples_in_buffer += f->samples;
00330    return 0;
00331 }
00332 
00333 static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00334 {
00335    struct channel_usage copy;
00336 
00337    switch (cmd) {
00338    case CLI_INIT:
00339       e->command = "transcoder show";
00340       e->usage =
00341          "Usage: transcoder show\n"
00342          "       Displays channel utilization of DAHDI transcoder(s).\n";
00343       return NULL;
00344    case CLI_GENERATE:
00345       return NULL;
00346    }
00347 
00348    if (a->argc != 2)
00349       return CLI_SHOWUSAGE;
00350 
00351    copy = channels;
00352 
00353    if (copy.total == 0)
00354       ast_cli(a->fd, "No DAHDI transcoders found.\n");
00355    else
00356       ast_cli(a->fd, "%d/%d encoders/decoders of %d channels are in use.\n", copy.encoders, copy.decoders, copy.total);
00357 
00358    return CLI_SUCCESS;
00359 }
00360 
00361 static void dahdi_write_frame(struct codec_dahdi_pvt *dahdip, const uint8_t *buffer, const ssize_t count)
00362 {
00363    int res;
00364    if (!count) return;
00365    res = write(dahdip->fd, buffer, count);
00366    if (-1 == res) {
00367       ast_log(LOG_ERROR, "Failed to write to transcoder: %s\n", strerror(errno));
00368    }
00369    if (count != res) {
00370       ast_log(LOG_ERROR, "Requested write of %zd bytes, but only wrote %d bytes.\n", count, res);
00371    }
00372 }
00373 
00374 static int dahdi_encoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00375 {
00376    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00377 
00378    if (!f->subclass.format) {
00379       /* We're just faking a return for calculation purposes. */
00380       dahdip->fake = 2;
00381       pvt->samples = f->samples;
00382       return 0;
00383    }
00384 
00385    /* Buffer up the packets and send them to the hardware if we
00386     * have enough samples set up. */
00387    if (dahdip->softslin) {
00388       if (lintoulaw(pvt, f)) {
00389           return -1;
00390       }
00391    } else {
00392       /* NOTE:  If softslin support is not needed, and the sample
00393        * size is equal to the required sample size, we wouldn't
00394        * need this copy operation.  But at the time this was
00395        * written, only softslin is supported. */
00396       if (dahdip->samples_in_buffer + f->samples > sizeof(dahdip->ulaw_buffer)) {
00397          ast_log(LOG_ERROR, "Out of buffer space.\n");
00398          return -1;
00399       }
00400       memcpy(&dahdip->ulaw_buffer[dahdip->samples_in_buffer], f->data.ptr, f->samples);
00401       dahdip->samples_in_buffer += f->samples;
00402    }
00403 
00404    while (dahdip->samples_in_buffer >= dahdip->required_samples) {
00405       dahdi_write_frame(dahdip, dahdip->ulaw_buffer, dahdip->required_samples);
00406       dahdip->samples_written_to_hardware += dahdip->required_samples;
00407       dahdip->samples_in_buffer -= dahdip->required_samples;
00408       if (dahdip->samples_in_buffer) {
00409          /* Shift any remaining bytes down. */
00410          memmove(dahdip->ulaw_buffer, &dahdip->ulaw_buffer[dahdip->required_samples],
00411             dahdip->samples_in_buffer);
00412       }
00413    }
00414    pvt->samples += f->samples;
00415    pvt->datalen = 0;
00416    return -1;
00417 }
00418 
00419 static void dahdi_wait_for_packet(int fd)
00420 {
00421    struct pollfd p = {0};
00422    p.fd = fd;
00423    p.events = POLLIN;
00424    poll(&p, 1, 10);
00425 }
00426 
00427 static struct ast_frame *dahdi_encoder_frameout(struct ast_trans_pvt *pvt)
00428 {
00429    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00430    int res;
00431 
00432    if (2 == dahdip->fake) {
00433       struct ast_frame frm = {
00434          .frametype = AST_FRAME_VOICE,
00435          .samples = dahdip->required_samples,
00436          .src = pvt->t->name,
00437       };
00438 
00439       dahdip->fake = 1;
00440       pvt->samples = 0;
00441 
00442       return ast_frisolate(&frm);
00443    } else if (1 == dahdip->fake) {
00444       dahdip->fake = 0;
00445       return NULL;
00446    }
00447 
00448    if (dahdip->samples_written_to_hardware >= dahdip->required_samples) {
00449       dahdi_wait_for_packet(dahdip->fd);
00450    }
00451 
00452    res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
00453    if (-1 == res) {
00454       if (EWOULDBLOCK == errno) {
00455          /* Nothing waiting... */
00456          return NULL;
00457       } else {
00458          ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
00459          return NULL;
00460       }
00461    } else {
00462       pvt->f.datalen = res;
00463       pvt->f.samples = ast_codec_samples_count(&pvt->f);
00464 
00465       dahdip->samples_written_to_hardware =
00466         (dahdip->samples_written_to_hardware >= pvt->f.samples) ?
00467            dahdip->samples_written_to_hardware - pvt->f.samples : 0;
00468 
00469       pvt->samples = 0;
00470       pvt->datalen = 0;
00471       return ast_frisolate(&pvt->f);
00472    }
00473 
00474    /* Shouldn't get here... */
00475    return NULL;
00476 }
00477 
00478 static int dahdi_decoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00479 {
00480    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00481 
00482    if (!f->subclass.format) {
00483       /* We're just faking a return for calculation purposes. */
00484       dahdip->fake = 2;
00485       pvt->samples = f->samples;
00486       return 0;
00487    }
00488 
00489    if (!f->datalen) {
00490       if (f->samples != dahdip->required_samples) {
00491          ast_log(LOG_ERROR, "%d != %d %d\n", f->samples, dahdip->required_samples, f->datalen);
00492       }
00493    }
00494    dahdi_write_frame(dahdip, f->data.ptr, f->datalen);
00495    dahdip->samples_written_to_hardware += f->samples;
00496    pvt->samples += f->samples;
00497    pvt->datalen = 0;
00498    return -1;
00499 }
00500 
00501 static struct ast_frame *dahdi_decoder_frameout(struct ast_trans_pvt *pvt)
00502 {
00503    int res;
00504    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00505 
00506    if (2 == dahdip->fake) {
00507       struct ast_frame frm = {
00508          .frametype = AST_FRAME_VOICE,
00509          .samples = dahdip->required_samples,
00510          .src = pvt->t->name,
00511       };
00512 
00513       dahdip->fake = 1;
00514       pvt->samples = 0;
00515 
00516       return ast_frisolate(&frm);
00517    } else if (1 == dahdip->fake) {
00518       pvt->samples = 0;
00519       dahdip->fake = 0;
00520       return NULL;
00521    }
00522 
00523    if (dahdip->samples_written_to_hardware >= ULAW_SAMPLES) {
00524       dahdi_wait_for_packet(dahdip->fd);
00525    }
00526 
00527    /* Let's check to see if there is a new frame for us.... */
00528    if (dahdip->softslin) {
00529       res = read(dahdip->fd, dahdip->ulaw_buffer, sizeof(dahdip->ulaw_buffer));
00530    } else {
00531       res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
00532    }
00533 
00534    if (-1 == res) {
00535       if (EWOULDBLOCK == errno) {
00536          /* Nothing waiting... */
00537          return NULL;
00538       } else {
00539          ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
00540          return NULL;
00541       }
00542    } else {
00543       if (dahdip->softslin) {
00544          ulawtolin(pvt, res);
00545          pvt->f.datalen = res * 2;
00546       } else {
00547          pvt->f.datalen = res;
00548       }
00549       pvt->datalen = 0;
00550       pvt->f.samples = res;
00551       pvt->samples = 0;
00552       dahdip->samples_written_to_hardware =
00553          (dahdip->samples_written_to_hardware >= res) ?
00554                  dahdip->samples_written_to_hardware - res : 0;
00555 
00556       return ast_frisolate(&pvt->f);
00557    }
00558 
00559    /* Shouldn't get here... */
00560    return NULL;
00561 }
00562 
00563 
00564 static void dahdi_destroy(struct ast_trans_pvt *pvt)
00565 {
00566    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00567 
00568    switch (dahdip->fmts.dstfmt) {
00569    case DAHDI_FORMAT_G729A:
00570    case DAHDI_FORMAT_G723_1:
00571       ast_atomic_fetchadd_int(&channels.encoders, -1);
00572       break;
00573    default:
00574       ast_atomic_fetchadd_int(&channels.decoders, -1);
00575       break;
00576    }
00577 
00578    close(dahdip->fd);
00579 }
00580 
00581 static struct ast_format *dahdi_format_to_cached(int format)
00582 {
00583    switch (format) {
00584    case DAHDI_FORMAT_G723_1:
00585       return ast_format_g723;
00586    case DAHDI_FORMAT_GSM:
00587       return ast_format_gsm;
00588    case DAHDI_FORMAT_ULAW:
00589       return ast_format_ulaw;
00590    case DAHDI_FORMAT_ALAW:
00591       return ast_format_alaw;
00592    case DAHDI_FORMAT_G726:
00593       return ast_format_g726;
00594    case DAHDI_FORMAT_ADPCM:
00595       return ast_format_adpcm;
00596    case DAHDI_FORMAT_SLINEAR:
00597       return ast_format_slin;
00598    case DAHDI_FORMAT_LPC10:
00599       return ast_format_lpc10;
00600    case DAHDI_FORMAT_G729A:
00601       return ast_format_g729;
00602    case DAHDI_FORMAT_SPEEX:
00603       return ast_format_speex;
00604    case DAHDI_FORMAT_ILBC:
00605       return ast_format_ilbc;
00606    }
00607 
00608    /* This will never be reached */
00609    ast_assert(0);
00610    return NULL;
00611 }
00612 
00613 static int dahdi_translate(struct ast_trans_pvt *pvt, uint32_t dst_dahdi_fmt, uint32_t src_dahdi_fmt)
00614 {
00615    /* Request translation through zap if possible */
00616    int fd;
00617    struct codec_dahdi_pvt *dahdip = pvt->pvt;
00618    int flags;
00619    int tried_once = 0;
00620    const char *dev_filename = "/dev/dahdi/transcode";
00621 
00622    if ((fd = open(dev_filename, O_RDWR)) < 0) {
00623       ast_log(LOG_ERROR, "Failed to open %s: %s\n", dev_filename, strerror(errno));
00624       return -1;
00625    }
00626 
00627    dahdip->fmts.srcfmt = src_dahdi_fmt;
00628    dahdip->fmts.dstfmt = dst_dahdi_fmt;
00629 
00630    ast_assert(pvt->f.subclass.format == NULL);
00631    pvt->f.subclass.format = ao2_bump(dahdi_format_to_cached(dahdip->fmts.dstfmt));
00632 
00633    ast_debug(1, "Opening transcoder channel from %s to %s.\n", pvt->t->src_codec.name, pvt->t->dst_codec.name);
00634 
00635 retry:
00636    if (ioctl(fd, DAHDI_TC_ALLOCATE, &dahdip->fmts)) {
00637       if ((ENODEV == errno) && !tried_once) {
00638          /* We requested to translate to/from an unsupported
00639           * format.  Most likely this is because signed linear
00640           * was not supported by any hardware devices even
00641           * though this module always registers signed linear
00642           * support. In this case we'll retry, requesting
00643           * support for ULAW instead of signed linear and then
00644           * we'll just convert from ulaw to signed linear in
00645           * software. */
00646          if (dahdip->fmts.srcfmt == DAHDI_FORMAT_SLINEAR) {
00647             ast_debug(1, "Using soft_slin support on source\n");
00648             dahdip->softslin = 1;
00649             dahdip->fmts.srcfmt = DAHDI_FORMAT_ULAW;
00650          } else if (dahdip->fmts.dstfmt == DAHDI_FORMAT_SLINEAR) {
00651             ast_debug(1, "Using soft_slin support on destination\n");
00652             dahdip->softslin = 1;
00653             dahdip->fmts.dstfmt = DAHDI_FORMAT_ULAW;
00654          }
00655          tried_once = 1;
00656          goto retry;
00657       }
00658       ast_log(LOG_ERROR, "Unable to attach to transcoder: %s\n", strerror(errno));
00659       close(fd);
00660 
00661       return -1;
00662    }
00663 
00664    flags = fcntl(fd, F_GETFL);
00665    if (flags > - 1) {
00666       if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
00667          ast_log(LOG_WARNING, "Could not set non-block mode!\n");
00668    }
00669 
00670    dahdip->fd = fd;
00671 
00672    dahdip->required_samples = ((dahdip->fmts.dstfmt|dahdip->fmts.srcfmt) & (DAHDI_FORMAT_G723_1)) ? G723_SAMPLES : G729_SAMPLES;
00673 
00674    switch (dahdip->fmts.dstfmt) {
00675    case DAHDI_FORMAT_G729A:
00676       ast_atomic_fetchadd_int(&channels.encoders, +1);
00677       break;
00678    case DAHDI_FORMAT_G723_1:
00679       ast_atomic_fetchadd_int(&channels.encoders, +1);
00680       break;
00681    default:
00682       ast_atomic_fetchadd_int(&channels.decoders, +1);
00683       break;
00684    }
00685 
00686    return 0;
00687 }
00688 
00689 static int dahdi_new(struct ast_trans_pvt *pvt)
00690 {
00691    struct translator *zt = container_of(pvt->t, struct translator, t);
00692 
00693    return dahdi_translate(pvt, zt->dst_dahdi_fmt, zt->src_dahdi_fmt);
00694 }
00695 
00696 static struct ast_frame *fakesrc_sample(void)
00697 {
00698    /* Don't bother really trying to test hardware ones. */
00699    static struct ast_frame f = {
00700       .frametype = AST_FRAME_VOICE,
00701       .samples = 160,
00702       .src = __PRETTY_FUNCTION__
00703    };
00704 
00705    return &f;
00706 }
00707 
00708 static bool is_encoder(uint32_t src_dahdi_fmt)
00709 {
00710    return ((src_dahdi_fmt & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW | DAHDI_FORMAT_SLINEAR)) > 0);
00711 }
00712 
00713 /* Must be called with the translators list locked. */
00714 static int register_translator(uint32_t dst_dahdi_fmt, uint32_t src_dahdi_fmt)
00715 {
00716    const struct ast_codec *dst_codec;
00717    const struct ast_codec *src_codec;
00718    struct translator *zt;
00719    int res;
00720 
00721    dst_codec = get_dahdi_codec(dst_dahdi_fmt);
00722    src_codec = get_dahdi_codec(src_dahdi_fmt);
00723    if (!dst_codec || !src_codec) {
00724       return -1;
00725    }
00726 
00727    if (!(zt = ast_calloc(1, sizeof(*zt)))) {
00728       return -1;
00729    }
00730 
00731    zt->src_dahdi_fmt = src_dahdi_fmt;
00732    zt->dst_dahdi_fmt = dst_dahdi_fmt;
00733 
00734    snprintf(zt->t.name, sizeof(zt->t.name), "dahdi_%s_to_%s",
00735       src_codec->name, dst_codec->name);
00736 
00737    memcpy(&zt->t.src_codec, src_codec, sizeof(*src_codec));
00738    memcpy(&zt->t.dst_codec, dst_codec, sizeof(*dst_codec));
00739    zt->t.buf_size = BUFFER_SIZE;
00740    if (is_encoder(src_dahdi_fmt)) {
00741       zt->t.framein = dahdi_encoder_framein;
00742       zt->t.frameout = dahdi_encoder_frameout;
00743    } else {
00744       zt->t.framein = dahdi_decoder_framein;
00745       zt->t.frameout = dahdi_decoder_frameout;
00746    }
00747    zt->t.destroy = dahdi_destroy;
00748    zt->t.buffer_samples = 0;
00749    zt->t.newpvt = dahdi_new;
00750    zt->t.sample = fakesrc_sample;
00751    zt->t.native_plc = 0;
00752 
00753    zt->t.desc_size = sizeof(struct codec_dahdi_pvt);
00754    if ((res = ast_register_translator(&zt->t))) {
00755       ast_free(zt);
00756       return -1;
00757    }
00758 
00759    AST_LIST_INSERT_HEAD(&translators, zt, entry);
00760 
00761    return res;
00762 }
00763 
00764 static void unregister_translators(void)
00765 {
00766    struct translator *cur;
00767 
00768    AST_LIST_LOCK(&translators);
00769    while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
00770       ast_unregister_translator(&cur->t);
00771       ast_free(cur);
00772    }
00773    AST_LIST_UNLOCK(&translators);
00774 }
00775 
00776 /* Must be called with the translators list locked. */
00777 static bool is_already_registered(uint32_t dstfmt, uint32_t srcfmt)
00778 {
00779    bool res = false;
00780    const struct translator *zt;
00781 
00782    AST_LIST_TRAVERSE(&translators, zt, entry) {
00783       if ((zt->src_dahdi_fmt == srcfmt) && (zt->dst_dahdi_fmt == dstfmt)) {
00784          res = true;
00785          break;
00786       }
00787    }
00788 
00789    return res;
00790 }
00791 
00792 static void build_translators(uint32_t dstfmts, uint32_t srcfmts)
00793 {
00794    uint32_t srcfmt;
00795    uint32_t dstfmt;
00796 
00797    AST_LIST_LOCK(&translators);
00798 
00799    for (srcfmt = 1; srcfmt != 0; srcfmt <<= 1) {
00800       for (dstfmt = 1; dstfmt != 0; dstfmt <<= 1) {
00801          if (!(dstfmts & dstfmt) || !(srcfmts & srcfmt)) {
00802             continue;
00803          }
00804          if (is_already_registered(dstfmt, srcfmt)) {
00805             continue;
00806          }
00807          register_translator(dstfmt, srcfmt);
00808       }
00809    }
00810 
00811    AST_LIST_UNLOCK(&translators);
00812 }
00813 
00814 static int find_transcoders(void)
00815 {
00816    struct dahdi_transcoder_info info = { 0, };
00817    int fd;
00818 
00819    if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
00820       ast_log(LOG_ERROR, "Failed to open /dev/dahdi/transcode: %s\n", strerror(errno));
00821       return 0;
00822    }
00823 
00824    for (info.tcnum = 0; !ioctl(fd, DAHDI_TC_GETINFO, &info); info.tcnum++) {
00825       ast_verb(2, "Found transcoder '%s'.\n", info.name);
00826 
00827       /* Complex codecs need to support signed linear.  If the
00828        * hardware transcoder does not natively support signed linear
00829        * format, we will emulate it in software directly in this
00830        * module. Also, do not allow direct ulaw/alaw to complex
00831        * codec translation, since that will prevent the generic PLC
00832        * functions from working. */
00833       if (info.dstfmts & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW)) {
00834          info.dstfmts |= DAHDI_FORMAT_SLINEAR;
00835          info.dstfmts &= ~(DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW);
00836       }
00837       if (info.srcfmts & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW)) {
00838          info.srcfmts |= DAHDI_FORMAT_SLINEAR;
00839          info.srcfmts &= ~(DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW);
00840       }
00841 
00842       build_translators(info.dstfmts, info.srcfmts);
00843       ast_atomic_fetchadd_int(&channels.total, info.numchannels / 2);
00844    }
00845 
00846    close(fd);
00847 
00848    if (!info.tcnum) {
00849       ast_verb(2, "No hardware transcoders found.\n");
00850    }
00851 
00852    return 0;
00853 }
00854 
00855 static int reload(void)
00856 {
00857    return AST_MODULE_LOAD_SUCCESS;
00858 }
00859 
00860 static int unload_module(void)
00861 {
00862    ast_cli_unregister_multiple(cli, ARRAY_LEN(cli));
00863    unregister_translators();
00864 
00865    return 0;
00866 }
00867 
00868 static int load_module(void)
00869 {
00870    find_transcoders();
00871    ast_cli_register_multiple(cli, ARRAY_LEN(cli));
00872    return AST_MODULE_LOAD_SUCCESS;
00873 }
00874 
00875 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic DAHDI Transcoder Codec Translator",
00876       .support_level = AST_MODULE_SUPPORT_CORE,
00877       .load = load_module,
00878       .unload = unload_module,
00879       .reload = reload,
00880           );

Generated on Thu Apr 16 06:27:30 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6