convert uses of LOG_DEBUG to use ast_debug()
[asterisk/asterisk.git] / channels / chan_unistim.c
index 7efd289..39e3e00 100644 (file)
@@ -202,6 +202,17 @@ static void dummy(char *dummy, ...)
        return;
 }
 
+/*! \brief Global jitterbuffer configuration - by default, jb is disabled */
+static struct ast_jb_conf default_jbconf =
+{
+        .flags = 0,
+       .max_size = -1,
+       .resync_threshold = -1,
+       .impl = ""
+};
+static struct ast_jb_conf global_jbconf;
+                               
+
 /* #define DUMP_PACKET 1 */
 /* #define DEBUG_TIMER ast_verbose */
 
@@ -697,7 +708,7 @@ static const struct ast_channel_tech unistim_tech = {
        .type = type,
        .description = tdesc,
        .capabilities = CAPABILITY,
-       .properties = AST_CHAN_TP_WANTSJITTER,
+       .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
        .requester = unistim_request,
        .call = unistim_call,
        .hangup = unistim_hangup,
@@ -1169,7 +1180,7 @@ static void close_client(struct unistimsession *s)
                else
                        sessions = cur->next;
                ast_mutex_destroy(&s->lock);
-               free(s);
+               ast_free(s);
        } else
                ast_log(LOG_WARNING, "Trying to delete non-existant session %p?\n", s);
        ast_mutex_unlock(&sessionlock);
@@ -1546,10 +1557,10 @@ static int unalloc_sub(struct unistim_line *p, int x)
                return -1;
        }
        if (unistimdebug)
-               ast_log(LOG_DEBUG, "Released sub %d of channel %s@%s\n", x, p->name,
+               ast_debug(1, "Released sub %d of channel %s@%s\n", x, p->name,
                                p->parent->name);
        ast_mutex_destroy(&p->lock);
-       free(p->subs[x]);
+       ast_free(p->subs[x]);
        p->subs[x] = 0;
        return 0;
 }
@@ -1588,16 +1599,25 @@ static void rcv_mac_addr(struct unistimsession *pte, const unsigned char *buf)
                                while (d) {
                                        if (!strcasecmp(d->name, "template")) {
                                                /* Found, cloning this entry */
-                                               if (!(newd = ast_malloc(sizeof(struct unistim_device))));
-                                               return;
+                                               if (!(newd = ast_malloc(sizeof(*newd)))) {
+                                                       ast_mutex_unlock(&devicelock);
+                                                       return;
+                                               }
 
-                                               memcpy(newd, d, sizeof(struct unistim_device));
-                                               if (!(newl = ast_malloc(sizeof(struct unistim_line))));
-                                               return;
+                                               memcpy(newd, d, sizeof(*newd));
+                                               if (!(newl = ast_malloc(sizeof(*newl)))) {
+                                                       ast_free(newd);
+                                                       ast_mutex_unlock(&devicelock);
+                                                       return;
+                                               }
 
-                                               memcpy(newl, d->lines, sizeof(struct unistim_line));
-                                               if (!alloc_sub(newl, SUB_REAL))
+                                               memcpy(newl, d->lines, sizeof(*newl));
+                                               if (!alloc_sub(newl, SUB_REAL)) {
+                                                       ast_free(newd);
+                                                       ast_free(newl);
+                                                       ast_mutex_unlock(&devicelock);
                                                        return;
+                                               }
                                                /* Ok, now updating some fields */
                                                ast_copy_string(newd->id, addrmac, sizeof(newd->id));
                                                ast_copy_string(newd->name, addrmac, sizeof(newd->name));
@@ -1853,20 +1873,20 @@ static int write_history(struct unistimsession *pte, char way, char ismissed)
                return -1;
        }
        if (fread(histbuf, size, 1, f) != 1) {
-               free(histbuf);
+               ast_free(histbuf);
                fclose(f);
                fclose(f2);
                display_last_error("Unable to read previous history entries.");
                return -1;
        }
        if (fwrite(histbuf, size, 1, f2) != 1) {
-               free(histbuf);
+               ast_free(histbuf);
                fclose(f);
                fclose(f2);
                display_last_error("Unable to write previous history entries.");
                return -1;
        }
-       free(histbuf);
+       ast_free(histbuf);
        if (fclose(f))
                display_last_error("Unable to close history log.");
        if (fclose(f2))
@@ -2466,7 +2486,7 @@ static void HandleCallOutgoing(struct unistimsession *s)
                                         p->subs[SUB_THREEWAY]->owner, p->subs[SUB_THREEWAY]->owner->name,
                                         p->subs[SUB_THREEWAY]->subtype);
                } else
-                       ast_log(LOG_DEBUG, "Current sub [%s] already has owner\n", sub->owner->name);
+                       ast_debug(1, "Current sub [%s] already has owner\n", sub->owner->name);
        }
        return;
 }
@@ -3498,7 +3518,7 @@ static void process_request(int size, unsigned char *buf, struct unistimsession
        strcpy(tmpbuf, ast_inet_ntoa(pte->sin.sin_addr));
        strcat(tmpbuf, " Unknown request packet\n");
        if (unistimdebug)
-               ast_log(LOG_DEBUG, tmpbuf);
+               ast_debug(1, "%s", tmpbuf);
        return;
 }
 
@@ -3756,7 +3776,7 @@ static int unistim_hangup(struct ast_channel *ast)
        s = channel_to_session(ast);
        sub = ast->tech_pvt;
        if (!s) {
-               ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
+               ast_debug(1, "Asked to hangup channel not connected\n");
                ast_mutex_lock(&sub->lock);
                sub->owner = NULL;
                ast->tech_pvt = NULL;
@@ -3975,7 +3995,7 @@ static struct ast_frame *unistim_rtp_read(const struct ast_channel *ast,
                /* We already hold the channel lock */
                if (f->frametype == AST_FRAME_VOICE) {
                        if (f->subclass != sub->owner->nativeformats) {
-                               ast_log(LOG_DEBUG,
+                               ast_debug(1,
                                                "Oooh, format changed from %s (%d) to %s (%d)\n",
                                                ast_getformatname(sub->owner->nativeformats),
                                                sub->owner->nativeformats, ast_getformatname(f->subclass),
@@ -4046,7 +4066,7 @@ static int unistim_fixup(struct ast_channel *oldchan, struct ast_channel *newcha
 
        ast_mutex_lock(&p->lock);
 
-       ast_log(LOG_DEBUG, "New owner for channel USTM/%s@%s-%d is %s\n", l->name,
+       ast_debug(1, "New owner for channel USTM/%s@%s-%d is %s\n", l->name,
                        l->parent->name, p->subtype, newchan->name);
 
        if (p->owner != oldchan) {
@@ -4466,10 +4486,14 @@ static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state
        struct unistim_line *l;
        int fmt;
 
-       if (!sub)
+       if (!sub) {
                ast_log(LOG_WARNING, "subchannel null in unistim_new\n");
-       if (!sub->parent)
+               return NULL;
+       }
+       if (!sub->parent) {
                ast_log(LOG_WARNING, "no line for subchannel %p\n", sub);
+               return NULL;
+       }
        l = sub->parent;
        tmp = ast_channel_alloc(1, state, l->cid_num, NULL, l->accountcode, l->exten, 
                l->context, l->amaflags, "%s-%08x", l->fullname, (int) (long) sub);
@@ -4496,6 +4520,9 @@ static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state
                tmp->fds[0] = ast_rtp_fd(sub->rtp);
                tmp->fds[1] = ast_rtcp_fd(sub->rtp);
        }
+       if (sub->rtp)
+               ast_jb_configure(tmp, &global_jbconf);
+               
 /*      tmp->type = type; */
        ast_setstate(tmp, state);
        if (state == AST_STATE_RING)
@@ -4523,7 +4550,7 @@ static struct ast_channel *unistim_new(struct unistim_subchannel *sub, int state
                        ast_callerid_parse(instr, &name, &loc);
                        tmp->cid.cid_num = ast_strdup(loc);
                        tmp->cid.cid_name = ast_strdup(name);
-                       free(instr);
+                       ast_free(instr);
                }
        }
        tmp->priority = 1;
@@ -4794,8 +4821,8 @@ static char *unistim_sp(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a
        case CLI_GENERATE:
                return NULL;    /* no completion */
        }
-
-       if (a->argc != e->args)
+       
+       if (a->argc < 4)
                return CLI_SHOWUSAGE;
 
        if (strlen(a->argv[2]) < 9)
@@ -4882,7 +4909,7 @@ static char *unistim_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
                return NULL;    /* no completion */
        }
 
-       if (a->argc != e->args)
+       if (e && a && a->argc != e->args)
                return CLI_SHOWUSAGE;
 
        if (unistimdebug)
@@ -5066,7 +5093,7 @@ static struct unistim_device *build_device(const char *cat, const struct ast_var
                        return NULL;
 
                if (!(l = ast_calloc(1, sizeof(*l)))) {
-                       free(d);
+                       ast_free(d);
                        return NULL;
                }
                ast_copy_string(d->name, cat, sizeof(d->name));
@@ -5178,8 +5205,8 @@ static struct unistim_device *build_device(const char *cat, const struct ast_var
                                ast_log(LOG_WARNING,
                                                "You must use bookmark AFTER line=>. Only one line is supported in this version\n");
                                if (create) {
-                                       free(d);
-                                       free(l);
+                                       ast_free(d);
+                                       ast_free(l);
                                }
                                return NULL;
                        }
@@ -5231,8 +5258,8 @@ static struct unistim_device *build_device(const char *cat, const struct ast_var
                        if (create) {
                                if (!alloc_sub(l, SUB_REAL)) {
                                        ast_mutex_destroy(&l->lock);
-                                       free(l);
-                                       free(d);
+                                       ast_free(l);
+                                       ast_free(d);
                                        return NULL;
                                }
                                l->next = d->lines;
@@ -5255,8 +5282,8 @@ static struct unistim_device *build_device(const char *cat, const struct ast_var
        if (!d->lines) {
                ast_log(LOG_ERROR, "An Unistim device must have at least one line!\n");
                ast_mutex_destroy(&l->lock);
-               free(l);
-               free(d);
+               ast_free(l);
+               ast_free(d);
                return NULL;
        }
        if ((autoprovisioning == AUTOPROVISIONING_TN) &&
@@ -5272,8 +5299,8 @@ static struct unistim_device *build_device(const char *cat, const struct ast_var
                if (strcmp(d->name, "template")) {
                        ast_log(LOG_ERROR, "You must specify the mac address with device=\n");
                        ast_mutex_destroy(&l->lock);
-                       free(l);
-                       free(d);
+                       ast_free(l);
+                       ast_free(d);
                        return NULL;
                } else
                        strcpy(d->id, "000000000000");
@@ -5340,11 +5367,18 @@ static int reload_config(void)
                ast_log(LOG_ERROR, "Unable to load config %s\n", config);
                return -1;
        }
+       
+       /* Copy the default jb config over global_jbconf */
+       memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
 
        unistim_keepalive = 120;
        unistim_port = 0;
        v = ast_variable_browse(cfg, "general");
        while (v) {
+               /* handle jb conf */
+               if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
+                       continue;       
+       
                if (!strcasecmp(v->name, "keepalive"))
                        unistim_keepalive = atoi(v->value);
                else if (!strcasecmp(v->name, "port"))
@@ -5376,12 +5410,14 @@ static int reload_config(void)
                (unistim_keepalive >
                 255 - (((NB_MAX_RETRANSMIT + 1) * RETRANSMIT_TIMER) / 1000))) {
                ast_log(LOG_ERROR, "keepalive is invalid in %s\n", config);
+               ast_config_destroy(cfg);
                return -1;
        }
        packet_send_ping[4] =
                unistim_keepalive + (((NB_MAX_RETRANSMIT + 1) * RETRANSMIT_TIMER) / 1000);
        if ((unistim_port < 1) || (unistim_port > 65535)) {
                ast_log(LOG_ERROR, "port is not set or invalid in %s\n", config);
+               ast_config_destroy(cfg);
                return -1;
        }
        unistim_keepalive *= 1000;
@@ -5412,11 +5448,13 @@ static int reload_config(void)
                                ast_verbose("Removing device '%s'\n", d->name);
                        if (!d->lines) {
                                ast_log(LOG_ERROR, "Device '%s' without a line !, aborting\n", d->name);
+                               ast_config_destroy(cfg);
                                return 0;
                        }
                        if (!d->lines->subs[0]) {
                                ast_log(LOG_ERROR, "Device '%s' without a subchannel !, aborting\n",
                                                d->name);
+                               ast_config_destroy(cfg);
                                return 0;
                        }
                        if (d->lines->subs[0]->owner) {
@@ -5427,7 +5465,7 @@ static int reload_config(void)
                                continue;
                        }
                        ast_mutex_destroy(&d->lines->subs[0]->lock);
-                       free(d->lines->subs[0]);
+                       ast_free(d->lines->subs[0]);
                        for (i = 1; i < MAX_SUBS; i++) {
                                if (d->lines->subs[i]) {
                                        ast_log(LOG_WARNING,
@@ -5441,7 +5479,7 @@ static int reload_config(void)
                                continue;
                        }
                        ast_mutex_destroy(&d->lines->lock);
-                       free(d->lines);
+                       ast_free(d->lines);
                        if (d->session) {
                                if (sessions == d->session)
                                        sessions = d->session->next;
@@ -5456,7 +5494,7 @@ static int reload_config(void)
                                        }
                                }
                                ast_mutex_destroy(&d->session->lock);
-                               free(d->session);
+                               ast_free(d->session);
                        }
                        if (devices == d)
                                devices = d->next;
@@ -5470,7 +5508,7 @@ static int reload_config(void)
                                        d2 = d2->next;
                                }
                        }
-                       free(d);
+                       ast_free(d);
                        d = devices;
                        continue;
                }
@@ -5599,7 +5637,7 @@ int load_module(void)
 
        res = reload_config();
        if (res)
-               goto reload_failed;
+               return AST_MODULE_LOAD_DECLINE;
 
        /* Make sure we can register our unistim channel type */
        if (ast_channel_register(&unistim_tech)) {
@@ -5617,17 +5655,16 @@ int load_module(void)
 
 chanreg_failed:
        /*! XXX \todo Leaking anything allocated by reload_config() ... */
-reload_failed:
        sched_context_destroy(sched);
        sched = NULL;
 sched_failed:
        io_context_destroy(io);
        io = NULL;
 io_failed:
-       free(buff);
+       ast_free(buff);
        buff = NULL;
 buff_failed:
-       return AST_MODULE_LOAD_DECLINE;
+       return AST_MODULE_LOAD_FAILURE;
 }
 
 static int unload_module(void)
@@ -5651,7 +5688,7 @@ static int unload_module(void)
        ast_mutex_unlock(&monlock);
 
        if (buff)
-               free(buff);
+               ast_free(buff);
        if (unistimsock > -1)
                close(unistimsock);
 
@@ -5661,7 +5698,7 @@ static int unload_module(void)
 /*! reload: Part of Asterisk module interface ---*/
 int reload(void)
 {
-       unistim_reload(0, 0, NULL);
+       unistim_reload(NULL, 0, NULL);
        return 0;
 }