Merged revisions 89046 via svnmerge from
[asterisk/asterisk.git] / codecs / codec_zap.c
index 7493204..c38aa5a 100644 (file)
@@ -60,24 +60,17 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #define BUFFER_SAMPLES 8000
 
 static unsigned int global_useplc = 0;
-static int cardsmode = 0;
 
-static int totalchannels = 0;
-static int complexinuse = 0;
-static int simpleinuse = 0;
-AST_MUTEX_DEFINE_STATIC(channelcount);
+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 const char show_transcoder_usage[] =
-"Usage: show transcoder\n"
-"       Displays transcoder utilization.\n";
-
-static int show_transcoder(int fd, int argc, char *argv[]);
-
-static struct ast_cli_entry transcoder_cli[] = {
-       { { "show", "transcoder", NULL},
-       show_transcoder, "Displays transcoder utilization.",
-       show_transcoder_usage }
+static struct ast_cli_entry cli[] = {
+       AST_CLI_DEFINE(handle_cli_transcoder_show, "Display Zaptel transcoder utilization.")
 };
 
 struct format_map {
@@ -91,12 +84,11 @@ struct translator {
        AST_LIST_ENTRY(translator) entry;
 };
 
-static AST_LIST_HEAD_STATIC(zap_translators, translator);
+static AST_LIST_HEAD_STATIC(translators, translator);
 
 struct pvt {
        int fd;
        int fake;
-       int inuse;
 #ifdef DEBUG_TRANSCODE
        int totalms;
        int lasttotalms;
@@ -105,27 +97,32 @@ struct pvt {
        struct ast_frame f;
 };
 
-static void activate_translator(int simple);
-static void deactivate_translator(int simple);
-
-
-static int show_transcoder(int fd, int argc, char *argv[])
+static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-       ast_mutex_lock(&channelcount);
-       if (!totalchannels) { 
-               ast_verbose("No transcoder card registered\n");
-               ast_mutex_unlock(&channelcount);
-               return RESULT_SUCCESS;
+       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(!cardsmode)             
-               ast_verbose("%d/%d encoders/decoders of %d channels (G.729a / G.723.1 5.3 kbps) are in use.\n",complexinuse, simpleinuse, totalchannels);
-       else if (cardsmode == 1)
-               ast_verbose("%d/%d encoders/decoders of %d channels (G.729a) are in use.\n",complexinuse, simpleinuse, totalchannels);
-       else if (cardsmode == 2)
-               ast_verbose("%d/%d encoders/decoders of %d channels (G.723.1 5.3 kbps) are in use.\n",complexinuse, simpleinuse, totalchannels);
-
-       ast_mutex_unlock(&channelcount);
-       return RESULT_SUCCESS;
+
+       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)
@@ -139,20 +136,7 @@ static int zap_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
                pvt->samples = f->samples;
                return 0;
        }
-       if(!ztp->inuse) {
-               ast_mutex_lock(&channelcount);
-               if(pvt->t->dstfmt == 8 || pvt->t->dstfmt == 0 ) {
-                       complexinuse++;
-                       if(complexinuse == totalchannels)
-                               deactivate_translator(0);
-               } else {
-                       simpleinuse++;
-                       if(simpleinuse == totalchannels)
-                               deactivate_translator(1);
-               }
-               ast_mutex_unlock(&channelcount);
-               ztp->inuse = 1;
-       }
+
        if (!hdr->srclen)
                /* Copy at front of buffer */
                hdr->srcoffset = 0;
@@ -185,7 +169,7 @@ static struct ast_frame *zap_frameout(struct ast_trans_pvt *pvt)
                ztp->fake = 1;
                ztp->f.frametype = AST_FRAME_VOICE;
                ztp->f.subclass = 0;
-               ztp->f.samples = 240;
+               ztp->f.samples = 160;
                ztp->f.data = NULL;
                ztp->f.offset = 0;
                ztp->f.datalen = 0;
@@ -198,7 +182,7 @@ static struct ast_frame *zap_frameout(struct ast_trans_pvt *pvt)
 #ifdef DEBUG_TRANSCODE
                        ztp->totalms += hdr->dstsamples;
                        if ((ztp->totalms - ztp->lasttotalms) > 8000) {
-                               ast_verbose("Whee %p, %d (%d to %d)\n", ztp, hdr->dstlen, ztp->lasttotalms, ztp->totalms);
+                               printf("Whee %p, %d (%d to %d)\n", ztp, hdr->dstlen, ztp->lasttotalms, ztp->totalms);
                                ztp->lasttotalms = ztp->totalms;
                        }
 #endif
@@ -234,21 +218,18 @@ static void zap_destroy(struct ast_trans_pvt *pvt)
        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));
-                               
-       munmap(ztp->hdr, sizeof(*ztp->hdr));
-       if(ztp->inuse) {
-               ast_mutex_lock(&channelcount);
-               if(pvt->t->dstfmt == 8 || pvt->t->dstfmt == 0) {
-                       if(complexinuse == totalchannels)
-                               activate_translator(0);
-                       complexinuse--;
-               } else {
-                       if(simpleinuse == totalchannels)
-                               activate_translator(1);
-                       simpleinuse--;
-               }
-               ast_mutex_unlock(&channelcount);
+
+       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);
 }
 
@@ -256,7 +237,7 @@ 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;
@@ -299,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;
 }
 
@@ -312,7 +303,7 @@ static struct ast_frame *fakesrc_sample(void)
        /* Don't bother really trying to test hardware ones. */
        static struct ast_frame f = {
                .frametype = AST_FRAME_VOICE,
-               .samples = 240,
+               .samples = 160,
                .src = __PRETTY_FUNCTION__
        };
 
@@ -326,8 +317,7 @@ static int register_translator(int dst, int src)
 
        if (!(zt = ast_calloc(1, sizeof(*zt))))
                return -1;
-       if (!((cardsmode  == 1 && (dst == 8 || src == 8)) || (cardsmode == 2 && (dst == 0 || src == 0)) || (cardsmode == 0)))
-               return -1;
+
        snprintf((char *) (zt->t.name), sizeof(zt->t.name), "zap%sto%s", 
                 ast_getformatname((1 << src)), ast_getformatname((1 << dst)));
        zt->t.srcfmt = (1 << src);
@@ -341,13 +331,13 @@ 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;
        }
 
-       AST_LIST_LOCK(&zap_translators);
-       AST_LIST_INSERT_HEAD(&zap_translators, zt, entry);
-       AST_LIST_UNLOCK(&zap_translators);
+       AST_LIST_LOCK(&translators);
+       AST_LIST_INSERT_HEAD(&translators, zt, entry);
+       AST_LIST_UNLOCK(&translators);
 
        global_format_map.map[dst][src] = 1;
 
@@ -358,110 +348,59 @@ static void drop_translator(int dst, int src)
 {
        struct translator *cur;
 
-       AST_LIST_LOCK(&zap_translators);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&zap_translators, cur, entry) {
+       AST_LIST_LOCK(&translators);
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, cur, entry) {
                if (cur->t.srcfmt != src)
                        continue;
 
                if (cur->t.dstfmt != dst)
                        continue;
 
-               AST_LIST_REMOVE_CURRENT(&zap_translators, entry);
+               AST_LIST_REMOVE_CURRENT(&translators, entry);
                ast_unregister_translator(&cur->t);
-               free(cur);
+               ast_free(cur);
                global_format_map.map[dst][src] = 0;
                break;
        }
        AST_LIST_TRAVERSE_SAFE_END;
-       AST_LIST_UNLOCK(&zap_translators);
+       AST_LIST_UNLOCK(&translators);
 }
 
 static void unregister_translators(void)
 {
        struct translator *cur;
 
-       AST_LIST_LOCK(&zap_translators);
-       while ((cur = AST_LIST_REMOVE_HEAD(&zap_translators, entry))) {
+       AST_LIST_LOCK(&translators);
+       while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
                ast_unregister_translator(&cur->t);
-               free(cur);
-       }
-       AST_LIST_UNLOCK(&zap_translators);
-}
-
-
-static void activate_translator(int simple)
-{
-       struct translator *cur;
-
-       AST_LIST_LOCK(&zap_translators);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&zap_translators, cur, entry) {
-               if(!simple) {
-                       if (cur->t.dstfmt == 0 || cur->t.dstfmt == 8) {
-                               ast_translator_activate(&cur->t);
-                       }
-               } else {
-                       if(cur->t.dstfmt == 2 || cur->t.dstfmt == 3) {
-                               ast_translator_activate(&cur->t);
-                       }
-               }
+               ast_free(cur);
        }
-       AST_LIST_TRAVERSE_SAFE_END;
-       AST_LIST_UNLOCK(&zap_translators);
+       AST_LIST_UNLOCK(&translators);
 }
 
-
-static void deactivate_translator(int simple)
-{
-       struct translator *cur;
-
-       AST_LIST_LOCK(&zap_translators);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&zap_translators, cur, entry) {
-               if(!simple) {
-                       if (cur->t.dstfmt == 0 || cur->t.dstfmt == 8) {
-                               ast_translator_deactivate(&cur->t);
-                       }
-               } else {
-                       if(cur->t.dstfmt == 2 || cur->t.dstfmt == 3) {
-                               ast_translator_deactivate(&cur->t);
-                       }
-               }
-       }
-       AST_LIST_TRAVERSE_SAFE_END;
-       AST_LIST_UNLOCK(&zap_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");
-       cardsmode = 0;
+       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 ");
               }
        }
-       for (var = ast_variable_browse(cfg, "transcoder_card"); var; var = var->next) {
-               if (!strcasecmp(var->name, "mode")) {
-                       if(strstr(var->value, "g729"))
-                               cardsmode = 1;
-                       else if(strstr(var->value, "g723"))
-                               cardsmode = 2;
-                       else if(strstr(var->value, "mixed"))
-                               cardsmode = 0;
-               }
-       }
-
        ast_config_destroy(cfg);
+       return 0;
 }
 
-static int build_translators(struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
+static void build_translators(struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
 {
        unsigned int src, dst;
 
@@ -478,11 +417,8 @@ static int build_translators(struct format_map *map, unsigned int dstfmts, unsig
 
                        if (!register_translator(dst, src))
                                map->map[dst][src] = 1;
-                       else
-                               return 0;
                }
        }
-       return 1;
 }
 
 static int find_transcoders(void)
@@ -494,23 +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);
-
-               if(build_translators(&map, info.dstfmts, info.srcfmts)) {
-                       ast_mutex_lock(&channelcount);
-                       totalchannels += info.numchannels;
-                       ast_mutex_unlock(&channelcount);
-               }
+               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++) {
@@ -518,9 +449,7 @@ static int find_transcoders(void)
                                drop_translator(x, y);
                }
        }
-       ast_mutex_lock(&channelcount);
-       totalchannels = totalchannels/2;
-       ast_mutex_unlock(&channelcount);
+
        return 0;
 }
 
@@ -528,36 +457,36 @@ static int reload(void)
 {
        struct translator *cur;
 
-       parse_config();
-       find_transcoders();
+       if (parse_config(1))
+               return AST_MODULE_LOAD_DECLINE;
 
-       AST_LIST_LOCK(&zap_translators);
-       AST_LIST_TRAVERSE(&zap_translators, cur, entry)
+       AST_LIST_LOCK(&translators);
+       AST_LIST_TRAVERSE(&translators, cur, entry)
                cur->t.useplc = global_useplc;
-       AST_LIST_UNLOCK(&zap_translators);
+       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();
-       ast_cli_unregister_multiple(transcoder_cli, sizeof(transcoder_cli) / sizeof(struct ast_cli_entry));
+
        return 0;
 }
 
 static int load_module(void)
 {
-       ast_mutex_init(&channelcount);
-       parse_config();
-
+       if (parse_config(0))
+               return AST_MODULE_LOAD_DECLINE;
        find_transcoders();
-       ast_cli_register_multiple(transcoder_cli, sizeof(transcoder_cli) / sizeof(struct ast_cli_entry));
-       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,
-              );
+               );