Merged revisions 89046 via svnmerge from
[asterisk/asterisk.git] / codecs / codec_zap.c
index 696a1b8..c38aa5a 100644 (file)
@@ -27,6 +27,7 @@
  */
 
 /*** MODULEINFO
+       <depend>zaptel_transcode</depend>
        <depend>zaptel</depend>
  ***/
 
@@ -50,6 +51,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/config.h"
 #include "asterisk/options.h"
 #include "asterisk/module.h"
+#include "asterisk/cli.h"
 #include "asterisk/logger.h"
 #include "asterisk/channel.h"
 #include "asterisk/utils.h"
@@ -59,6 +61,18 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 static unsigned int global_useplc = 0;
 
+static struct channel_usage {
+       int total;
+       int encoders;
+       int decoders;
+} channels;
+
+static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
+
+static struct ast_cli_entry cli[] = {
+       AST_CLI_DEFINE(handle_cli_transcoder_show, "Display Zaptel transcoder utilization.")
+};
+
 struct format_map {
        unsigned int map[32][32];
 };
@@ -75,10 +89,42 @@ static AST_LIST_HEAD_STATIC(translators, translator);
 struct pvt {
        int fd;
        int fake;
+#ifdef DEBUG_TRANSCODE
+       int totalms;
+       int lasttotalms;
+#endif
        struct zt_transcode_header *hdr;
        struct ast_frame f;
 };
 
+static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+       struct channel_usage copy;
+
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "transcoder show";
+               e->usage =
+                       "Usage: transcoder show\n"
+                       "       Displays channel utilization of Zaptel transcoder(s).\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }
+
+       if (a->argc != 2)
+               return CLI_SHOWUSAGE;
+
+       copy = channels;
+
+       if (copy.total == 0)
+               ast_cli(a->fd, "No Zaptel transcoders found.\n");
+       else
+               ast_cli(a->fd, "%d/%d encoders/decoders of %d channels are in use.\n", copy.encoders, copy.decoders, copy.total);
+
+       return CLI_SUCCESS;
+}
+
 static int zap_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
        struct pvt *ztp = pvt->pvt;
@@ -106,7 +152,7 @@ static int zap_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
                hdr->srcoffset = 0;
        }
 
-       memcpy(hdr->srcdata + hdr->srcoffset, f->data, f->datalen);
+       memcpy(hdr->srcdata + hdr->srcoffset + hdr->srclen, f->data, f->datalen);
        hdr->srclen += f->datalen;
        pvt->samples += f->samples;
 
@@ -131,24 +177,34 @@ static struct ast_frame *zap_frameout(struct ast_trans_pvt *pvt)
                pvt->samples = 0;
        } else if (ztp->fake == 1) {
                return NULL;
-       } else if (!hdr->srclen) {
-               return NULL;
        } else {
-               hdr->dstoffset = 0;
-               hdr->dstlen = 0;
-               x = ZT_TCOP_TRANSCODE;
-               if (ioctl(ztp->fd, ZT_TRANSCODE_OP, &x))
-                       ast_log(LOG_WARNING, "Failed to transcode: %s\n", strerror(errno));
-               if (!hdr->dstlen)
+               if (hdr->dstlen) {
+#ifdef DEBUG_TRANSCODE
+                       ztp->totalms += hdr->dstsamples;
+                       if ((ztp->totalms - ztp->lasttotalms) > 8000) {
+                               printf("Whee %p, %d (%d to %d)\n", ztp, hdr->dstlen, ztp->lasttotalms, ztp->totalms);
+                               ztp->lasttotalms = ztp->totalms;
+                       }
+#endif
+                       ztp->f.frametype = AST_FRAME_VOICE;
+                       ztp->f.subclass = hdr->dstfmt;
+                       ztp->f.samples = hdr->dstsamples;
+                       ztp->f.data = hdr->dstdata + hdr->dstoffset;
+                       ztp->f.offset = hdr->dstoffset;
+                       ztp->f.datalen = hdr->dstlen;
+                       ztp->f.mallocd = 0;
+                       pvt->samples -= ztp->f.samples;
+                       hdr->dstlen = 0;
+                       
+               } else {
+                       if (hdr->srclen) {
+                               hdr->dstoffset = AST_FRIENDLY_OFFSET;
+                               x = ZT_TCOP_TRANSCODE;
+                               if (ioctl(ztp->fd, ZT_TRANSCODE_OP, &x))
+                                       ast_log(LOG_WARNING, "Failed to transcode: %s\n", strerror(errno));
+                       }
                        return NULL;
-               ztp->f.frametype = AST_FRAME_VOICE;
-               ztp->f.subclass = hdr->dstfmt;
-               ztp->f.samples = hdr->dstsamples;
-               ztp->f.data = hdr->dstdata + hdr->dstoffset;
-               ztp->f.offset = hdr->dstoffset;
-               ztp->f.datalen = hdr->dstlen;
-               ztp->f.mallocd = 0;
-               pvt->samples -= ztp->f.samples;
+               }
        }
 
        return &ztp->f;
@@ -157,6 +213,21 @@ static struct ast_frame *zap_frameout(struct ast_trans_pvt *pvt)
 static void zap_destroy(struct ast_trans_pvt *pvt)
 {
        struct pvt *ztp = pvt->pvt;
+       unsigned int x;
+
+       x = ZT_TCOP_RELEASE;
+       if (ioctl(ztp->fd, ZT_TRANSCODE_OP, &x))
+               ast_log(LOG_WARNING, "Failed to release transcoder channel: %s\n", strerror(errno));
+
+       switch (ztp->hdr->dstfmt) {
+       case AST_FORMAT_G729A:
+       case AST_FORMAT_G723_1:
+               ast_atomic_fetchadd_int(&channels.encoders, -1);
+               break;
+       default:
+               ast_atomic_fetchadd_int(&channels.decoders, -1);
+               break;
+       }
 
        munmap(ztp->hdr, sizeof(*ztp->hdr));
        close(ztp->fd);
@@ -166,12 +237,19 @@ static int zap_translate(struct ast_trans_pvt *pvt, int dest, int source)
 {
        /* Request translation through zap if possible */
        int fd;
-       unsigned int x = ZT_TCOP_RESET;
+       unsigned int x = ZT_TCOP_ALLOCATE;
        struct pvt *ztp = pvt->pvt;
        struct zt_transcode_header *hdr;
+       int flags;
        
        if ((fd = open("/dev/zap/transcode", O_RDWR)) < 0)
                return -1;
+       flags = fcntl(fd, F_GETFL);
+       if (flags > - 1) {
+               if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
+                       ast_log(LOG_WARNING, "Could not set non-block mode!\n");
+       }
+       
 
        if ((hdr = mmap(NULL, sizeof(*hdr), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
                ast_log(LOG_ERROR, "Memory Map failed for transcoding (%s)\n", strerror(errno));
@@ -202,6 +280,16 @@ static int zap_translate(struct ast_trans_pvt *pvt, int dest, int source)
        ztp->fd = fd;
        ztp->hdr = hdr;
 
+       switch (hdr->dstfmt) {
+       case AST_FORMAT_G729A:
+       case AST_FORMAT_G723_1:
+               ast_atomic_fetchadd_int(&channels.encoders, +1);
+               break;
+       default:
+               ast_atomic_fetchadd_int(&channels.decoders, +1);
+               break;
+       }
+
        return 0;
 }
 
@@ -243,7 +331,7 @@ static int register_translator(int dst, int src)
        zt->t.buf_size = BUFFER_SAMPLES * 2;
        zt->t.desc_size = sizeof(struct pvt);
        if ((res = ast_register_translator(&zt->t))) {
-               free(zt);
+               ast_free(zt);
                return -1;
        }
 
@@ -270,7 +358,7 @@ static void drop_translator(int dst, int src)
 
                AST_LIST_REMOVE_CURRENT(&translators, entry);
                ast_unregister_translator(&cur->t);
-               free(cur);
+               ast_free(cur);
                global_format_map.map[dst][src] = 0;
                break;
        }
@@ -285,29 +373,31 @@ static void unregister_translators(void)
        AST_LIST_LOCK(&translators);
        while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
                ast_unregister_translator(&cur->t);
-               free(cur);
+               ast_free(cur);
        }
        AST_LIST_UNLOCK(&translators);
 }
 
-static void parse_config(void)
+static int parse_config(int reload)
 {
        struct ast_variable *var;
-       struct ast_config *cfg = ast_config_load("codecs.conf");
+       struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
+       struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
 
        if (!cfg)
-               return;
+               return -1;
+       if (cfg == CONFIG_STATUS_FILEUNCHANGED)
+               return 0;
 
        for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
               if (!strcasecmp(var->name, "genericplc")) {
                       global_useplc = ast_true(var->value);
-                      if (option_verbose > 2)
-                              ast_verbose(VERBOSE_PREFIX_3 "codec_zap: %susing generic PLC\n",
+                          ast_verb(3, "codec_zap: %susing generic PLC\n",
                                           global_useplc ? "" : "not ");
               }
        }
-
        ast_config_destroy(cfg);
+       return 0;
 }
 
 static void build_translators(struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
@@ -340,18 +430,18 @@ static int find_transcoders(void)
 
        info.op = ZT_TCOP_GETINFO;
        if ((fd = open("/dev/zap/transcode", O_RDWR)) < 0) {
-               ast_log(LOG_NOTICE, "No Zaptel transcoder support!\n");
+               ast_debug(1, "No Zaptel transcoder support!\n");
                return 0;
        }
        for (info.tcnum = 0; !(res = ioctl(fd, ZT_TRANSCODE_OP, &info)); info.tcnum++) {
-               if (option_verbose > 1)
-                       ast_verbose(VERBOSE_PREFIX_2 "Found transcoder '%s'.\n", info.name);
+               ast_verb(2, "Found transcoder '%s'.\n", info.name);
                build_translators(&map, info.dstfmts, info.srcfmts);
+               ast_atomic_fetchadd_int(&channels.total, info.numchannels / 2);
        }
        close(fd);
 
-       if (!info.tcnum && (option_verbose > 1))
-               ast_verbose(VERBOSE_PREFIX_2 "No hardware transcoders found.\n");
+       if (!info.tcnum)
+               ast_verb(2, "No hardware transcoders found.\n");
 
        for (x = 0; x < 32; x++) {
                for (y = 0; y < 32; y++) {
@@ -367,19 +457,20 @@ static int reload(void)
 {
        struct translator *cur;
 
-       parse_config();
-       find_transcoders();
+       if (parse_config(1))
+               return AST_MODULE_LOAD_DECLINE;
 
        AST_LIST_LOCK(&translators);
        AST_LIST_TRAVERSE(&translators, cur, entry)
                cur->t.useplc = global_useplc;
        AST_LIST_UNLOCK(&translators);
 
-       return 0;
+       return AST_MODULE_LOAD_SUCCESS;
 }
 
 static int unload_module(void)
 {
+       ast_cli_unregister_multiple(cli, sizeof(cli) / sizeof(cli[0]));
        unregister_translators();
 
        return 0;
@@ -387,14 +478,15 @@ static int unload_module(void)
 
 static int load_module(void)
 {
-       parse_config();
+       if (parse_config(0))
+               return AST_MODULE_LOAD_DECLINE;
        find_transcoders();
-
-       return 0;
+       ast_cli_register_multiple(cli, sizeof(cli) / sizeof(cli[0]));
+       return AST_MODULE_LOAD_SUCCESS;
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic Zaptel Transcoder Codec Translator",
                .load = load_module,
                .unload = unload_module,
                .reload = reload,
-              );
+               );