Media Project Phase2: SILK 8khz-24khz, SLINEAR 8khz-192khz, SPEEX 32khz, hd audio...
[asterisk/asterisk.git] / channels / chan_gtalk.c
index 326ea70..d8dd736 100644 (file)
@@ -37,7 +37,6 @@
        <use>openssl</use>
  ***/
 
-
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
@@ -75,8 +74,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/abstract_jb.h"
 #include "asterisk/jabber.h"
 #include "asterisk/jingle.h"
+#include "asterisk/features.h"
 
-#define GOOGLE_CONFIG "gtalk.conf"
+#define GOOGLE_CONFIG          "gtalk.conf"
 
 /*! Global jitterbuffer configuration - by default, jb is disabled */
 static struct ast_jb_conf default_jbconf =
@@ -100,14 +100,6 @@ enum gtalk_connect_type {
        AJI_CONNECT_RELAY = 3,
 };
 
-enum gtalk_client_type {
-       AJI_CLIENT_UNKNOWN,
-       AJI_CLIENT_GTALK,               /*!< Remote client type is GoogleTalk */
-       AJI_CLIENT_GMAIL,               /*!< Remote client type is Gmail */
-       AJI_CLIENT_GOOGLE_VOICE,/*!< Remote client type is Google Voice*/
-
-};
-
 struct gtalk_pvt {
        ast_mutex_t lock;                /*!< Channel private lock */
        time_t laststun;
@@ -118,9 +110,7 @@ struct gtalk_pvt {
        char ring[10];                   /*!< Message ID of ring */
        iksrule *ringrule;               /*!< Rule for matching RING request */
        int initiator;                   /*!< If we're the initiator */
-       enum gtalk_client_type ctype;
        int alreadygone;
-       int capability;
        struct ast_codec_pref prefs;
        struct gtalk_candidate *theircandidates;
        struct gtalk_candidate *ourcandidates;
@@ -130,8 +120,9 @@ struct gtalk_pvt {
        struct ast_channel *owner;       /*!< Master Channel */
        struct ast_rtp_instance *rtp;             /*!< RTP audio session */
        struct ast_rtp_instance *vrtp;            /*!< RTP video session */
-       format_t jointcapability;             /*!< Supported capability at both ends (codecs ) */
-       format_t peercapability;
+       struct ast_format_cap *cap;
+       struct ast_format_cap *jointcap;             /*!< Supported capability at both ends (codecs ) */
+       struct ast_format_cap *peercap;
        struct gtalk_pvt *next; /* Next entity */
 };
 
@@ -161,7 +152,7 @@ struct gtalk {
        char context[AST_MAX_CONTEXT];
        char parkinglot[AST_MAX_CONTEXT];       /*!<  Parkinglot */
        char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
-       format_t capability;
+       struct ast_format_cap *cap;
        ast_group_t callgroup;  /*!< Call group */
        ast_group_t pickupgroup;        /*!< Pickup group */
        int callingpres;                /*!< Calling presentation */
@@ -174,14 +165,16 @@ struct gtalk_container {
        ASTOBJ_CONTAINER_COMPONENTS(struct gtalk);
 };
 
-static const char desc[] = "Gtalk Channel";
+static const char desc[]               = "Gtalk Channel";
+static const char DEFAULT_CONTEXT[]    = "default";
+static const int DEFAULT_ALLOWGUEST    = 1;
 
-static format_t global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
+static struct ast_format_cap *global_capability;
 
 AST_MUTEX_DEFINE_STATIC(gtalklock); /*!< Protect the interface list (of gtalk_pvt's) */
 
 /* Forward declarations */
-static struct ast_channel *gtalk_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
+static struct ast_channel *gtalk_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause);
 /*static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration);*/
 static int gtalk_sendtext(struct ast_channel *ast, const char *text);
 static int gtalk_digit_begin(struct ast_channel *ast, char digit);
@@ -201,13 +194,15 @@ static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const
 static int gtalk_update_stun(struct gtalk *client, struct gtalk_pvt *p);
 /* static char *gtalk_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a); */
 static char *gtalk_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
+static char *gtalk_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 static int gtalk_update_externip(void);
+static int gtalk_parser(void *data, ikspak *pak);
+static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, char *sid, char *from, char *to);
 
 /*! \brief PBX interface structure for channel registration */
-static const struct ast_channel_tech gtalk_tech = {
+static struct ast_channel_tech gtalk_tech = {
        .type = "Gtalk",
        .description = "Gtalk Channel Driver",
-       .capabilities = AST_FORMAT_AUDIO_MASK,
        .requester = gtalk_request,
        .send_text = gtalk_sendtext,
        .send_digit_begin = gtalk_digit_begin,
@@ -230,22 +225,28 @@ static const struct ast_channel_tech gtalk_tech = {
 
 static struct sockaddr_in bindaddr = { 0, };   /*!< The address we bind to */
 
-static struct sched_context *sched;    /*!< The scheduling context */
+static struct ast_sched_context *sched;        /*!< The scheduling context */
 static struct io_context *io;  /*!< The IO context */
 static struct in_addr __ourip;
 
 static struct ast_cli_entry gtalk_cli[] = {
 /*     AST_CLI_DEFINE(gtalk_do_reload, "Reload GoogleTalk configuration"), XXX TODO reloads are not possible yet. */
        AST_CLI_DEFINE(gtalk_show_channels, "Show GoogleTalk channels"),
+       AST_CLI_DEFINE(gtalk_show_settings, "Show GoogleTalk global settings"),
 };
 
 static char externip[16];
+static char global_context[AST_MAX_CONTEXT];
+static char global_parkinglot[AST_MAX_CONTEXT];
+static int global_allowguest;
 static struct sockaddr_in stunaddr; /*!< the stun server we get the externip from */
+static int global_stunaddr;
 
 static struct gtalk_container gtalk_list;
 
 static void gtalk_member_destroy(struct gtalk *obj)
 {
+       obj->cap = ast_format_cap_destroy(obj->cap);
        ast_free(obj);
 }
 
@@ -281,10 +282,10 @@ static struct gtalk *find_gtalk(char *name, char *connection)
 }
 
 
-static int add_codec_to_answer(const struct gtalk_pvt *p, int codec, iks *dcodecs)
+static int add_codec_to_answer(const struct gtalk_pvt *p, struct ast_format *codec, iks *dcodecs)
 {
        int res = 0;
-       char *format = ast_getformatname(codec);
+       const char *format = ast_getformatname(codec);
 
        if (!strcasecmp("ulaw", format)) {
                iks *payload_eg711u, *payload_pcmu;
@@ -391,17 +392,17 @@ static int gtalk_invite(struct gtalk_pvt *p, char *to, char *from, char *sid, in
        struct gtalk *client = p->parent;
        iks *iq, *gtalk, *dcodecs, *payload_telephone, *transport;
        int x;
-       int pref_codec = 0;
-       int alreadysent = 0;
+       struct ast_format_cap *alreadysent;
        int codecs_num = 0;
        char *lowerto = NULL;
+       struct ast_format tmpfmt;
 
        iq = iks_new("iq");
        gtalk = iks_new("session");
        dcodecs = iks_new("description");
        transport = iks_new("transport");
        payload_telephone = iks_new("payload-type");
-       if (!(iq && gtalk && dcodecs && transport && payload_telephone)){
+       if (!(iq && gtalk && dcodecs && transport && payload_telephone)) {
                iks_delete(iq);
                iks_delete(gtalk);
                iks_delete(dcodecs);
@@ -414,16 +415,23 @@ static int gtalk_invite(struct gtalk_pvt *p, char *to, char *from, char *sid, in
        iks_insert_attrib(dcodecs, "xmlns", GOOGLE_AUDIO_NS);
        iks_insert_attrib(dcodecs, "xml:lang", "en");
 
-       for (x = 0; x < 64; x++) {
-               if (!(pref_codec = ast_codec_pref_index(&client->prefs, x)))
+       if (!(alreadysent = ast_format_cap_alloc_nolock())) {
+               return 0;
+       }
+       for (x = 0; x < AST_CODEC_PREF_SIZE; x++) {
+               if (!(ast_codec_pref_index(&client->prefs, x, &tmpfmt))) {
                        break;
-               if (!(client->capability & pref_codec))
+               }
+               if (!(ast_format_cap_iscompatible(client->cap, &tmpfmt))) {
                        continue;
-               if (alreadysent & pref_codec)
+               }
+               if (ast_format_cap_iscompatible(alreadysent, &tmpfmt)) {
                        continue;
-               codecs_num = add_codec_to_answer(p, pref_codec, dcodecs);
-               alreadysent |= pref_codec;
+               }
+               codecs_num = add_codec_to_answer(p, &tmpfmt, dcodecs);
+               ast_format_cap_add(alreadysent, &tmpfmt);
        }
+       alreadysent = ast_format_cap_destroy(alreadysent);
 
        if (codecs_num) {
                /* only propose DTMF within an audio session */
@@ -452,9 +460,6 @@ static int gtalk_invite(struct gtalk_pvt *p, char *to, char *from, char *sid, in
        iks_insert_attrib(gtalk, "id", sid);
        iks_insert_node(iq, gtalk);
        iks_insert_node(gtalk, dcodecs);
-       if (p->ctype != AJI_CLIENT_GOOGLE_VOICE) {
-               iks_insert_node(gtalk, transport);
-       }
        iks_insert_node(dcodecs, payload_telephone);
 
        ast_aji_send(client->connection, iq);
@@ -467,58 +472,49 @@ static int gtalk_invite(struct gtalk_pvt *p, char *to, char *from, char *sid, in
        return 1;
 }
 
-static int gtalk_invite_response(struct gtalk_pvt *p, char *to , char *from, char *sid, int initiator)
-{
-       iks *iq, *session, *transport;
-       char *lowerto = NULL;
-
-       iq = iks_new("iq");
-       session = iks_new("session");
-       transport = iks_new("transport");
-       if(!(iq && session && transport)) {
-               iks_delete(iq);
-               iks_delete(session);
-               iks_delete(transport);
-               ast_log(LOG_ERROR, " Unable to allocate IKS node\n");
-               return -1;
-       }
-       iks_insert_attrib(iq, "from", from);
-       iks_insert_attrib(iq, "to", to);
-       iks_insert_attrib(iq, "type", "set");
-       iks_insert_attrib(iq, "id",p->parent->connection->mid);
-       ast_aji_increment_mid(p->parent->connection->mid);
-       iks_insert_attrib(session, "type", "transport-accept");
-       iks_insert_attrib(session, "id", sid);
-       /* put the initiator attribute to lower case if we receive the call
-        * otherwise GoogleTalk won't establish the session */
-       if (!initiator) {
-               char c;
-               char *t = lowerto = ast_strdupa(to);
-               while (((c = *t) != '/') && (*t++ = tolower(c)));
-       }
-       iks_insert_attrib(session, "initiator", initiator ? from : lowerto);
-       iks_insert_attrib(session, "xmlns", GOOGLE_NS);
-       iks_insert_attrib(transport, "xmlns", GOOGLE_TRANSPORT_NS);
-       iks_insert_node(iq,session);
-       iks_insert_node(session,transport);
-       ast_aji_send(p->parent->connection, iq);
-
-       iks_delete(transport);
-       iks_delete(session);
-       iks_delete(iq);
-       return 1;
-
-}
-
 static int gtalk_ringing_ack(void *data, ikspak *pak)
 {
        struct gtalk_pvt *p = data;
+       struct ast_channel *owner;
 
-       if (p->ringrule)
+       ast_mutex_lock(&p->lock);
+
+       if (p->ringrule) {
                iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
+       }
        p->ringrule = NULL;
-       if (p->owner)
-               ast_queue_control(p->owner, AST_CONTROL_RINGING);
+
+       /* this may be a redirect */
+       if (!strcmp(S_OR(iks_find_attrib(pak->x, "type"), ""), "error")) {
+               char *name = NULL;
+               char *redirect = NULL;
+               iks *traversenodes = NULL;
+               traversenodes = pak->query;
+               while (traversenodes) {
+                       if (!(name = iks_name(traversenodes))) {
+                               break;
+                       }
+                       if (!strcasecmp(name, "error") &&
+                               (redirect = iks_find_cdata(traversenodes, "redirect")) &&
+                               (redirect = strstr(redirect, "xmpp:"))) {
+                               redirect += 5;
+                               ast_debug(1, "redirect %s\n", redirect);
+                               ast_copy_string(p->them, redirect, sizeof(p->them));
+
+                               gtalk_invite(p, p->them, p->us, p->sid, 1);
+                               break;
+                       }
+                       traversenodes = iks_next_tag(traversenodes);
+               }
+       }
+       gtalk_create_candidates(p->parent, p, p->sid, p->them, p->us);
+       owner = p->owner;
+       ast_mutex_unlock(&p->lock);
+
+       if (owner) {
+               ast_queue_control(owner, AST_CONTROL_RINGING);
+       }
+
        return IKS_FILTER_EAT;
 }
 
@@ -555,13 +551,15 @@ static enum ast_rtp_glue_result gtalk_get_rtp_peer(struct ast_channel *chan, str
        return res;
 }
 
-static format_t gtalk_get_codec(struct ast_channel *chan)
+static void gtalk_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
 {
        struct gtalk_pvt *p = chan->tech_pvt;
-       return p->peercapability;
+       ast_mutex_lock(&p->lock);
+       ast_format_cap_copy(result, p->peercap);
+       ast_mutex_unlock(&p->lock);
 }
 
-static int gtalk_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, format_t codecs, int nat_active)
+static int gtalk_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, const struct ast_format_cap *cap, int nat_active)
 {
        struct gtalk_pvt *p;
 
@@ -597,8 +595,8 @@ static int gtalk_response(struct gtalk *client, char *from, ikspak *pak, const c
        if (response) {
                iks_insert_attrib(response, "type", "result");
                iks_insert_attrib(response, "from", from);
-               iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from"));
-               iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id"));
+               iks_insert_attrib(response, "to", S_OR(iks_find_attrib(pak->x, "from"), ""));
+               iks_insert_attrib(response, "id", S_OR(iks_find_attrib(pak->x, "id"), ""));
                if (reasonstr) {
                        error = iks_new("error");
                        if (error) {
@@ -628,7 +626,7 @@ static int gtalk_is_answered(struct gtalk *client, ikspak *pak)
        char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ];
        int peernoncodeccapability;
 
-       ast_log(LOG_DEBUG, "The client is %s\n", client->name);
+       ast_debug(1, "The client is %s\n", client->name);
 
        /* Make sure our new call does exist */
        for (tmp = client->p; tmp; tmp = tmp->next) {
@@ -647,21 +645,37 @@ static int gtalk_is_answered(struct gtalk *client, ikspak *pak)
        /* codec points to the first <payload-type/> tag */
        codec = iks_first_tag(iks_first_tag(iks_first_tag(pak->x)));
        while (codec) {
-               ast_rtp_codecs_payloads_set_m_type(ast_rtp_instance_get_codecs(tmp->rtp), tmp->rtp, atoi(iks_find_attrib(codec, "id")));
-               ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(tmp->rtp), tmp->rtp, atoi(iks_find_attrib(codec, "id")), "audio", iks_find_attrib(codec, "name"), 0);
+               char *codec_id = iks_find_attrib(codec, "id");
+               char *codec_name = iks_find_attrib(codec, "name");
+               if (!codec_id || !codec_name) {
+                       codec = iks_next_tag(codec);
+                       continue;
+               }
+
+               ast_rtp_codecs_payloads_set_m_type(
+                       ast_rtp_instance_get_codecs(tmp->rtp),
+                       tmp->rtp,
+                       atoi(codec_id));
+               ast_rtp_codecs_payloads_set_rtpmap_type(
+                       ast_rtp_instance_get_codecs(tmp->rtp),
+                       tmp->rtp,
+                       atoi(codec_id),
+                       "audio",
+                       codec_name,
+                       0);
                codec = iks_next_tag(codec);
        }
 
        /* Now gather all of the codecs that we are asked for */
-       ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(tmp->rtp), &tmp->peercapability, &peernoncodeccapability);
+       ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(tmp->rtp), tmp->peercap, &peernoncodeccapability);
 
-       /* at this point, we received an awser from the remote Gtalk client,
+       /* at this point, we received an answer from the remote Gtalk client,
           which allows us to compare capabilities */
-       tmp->jointcapability = tmp->capability & tmp->peercapability;
-       if (!tmp->jointcapability) {
-               ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, tmp->capability),
-                       ast_getformatname_multiple(s2, BUFSIZ, tmp->peercapability),
-                       ast_getformatname_multiple(s3, BUFSIZ, tmp->jointcapability));
+       ast_format_cap_joint_copy(tmp->cap, tmp->peercap, tmp->jointcap);
+       if (ast_format_cap_is_empty(tmp->jointcap)) {
+               ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, tmp->cap),
+                       ast_getformatname_multiple(s2, BUFSIZ, tmp->peercap),
+                       ast_getformatname_multiple(s3, BUFSIZ, tmp->jointcap));
                /* close session if capabilities don't match */
                ast_queue_hangup(tmp->owner);
 
@@ -687,7 +701,7 @@ static int gtalk_is_accepted(struct gtalk *client, ikspak *pak)
        struct gtalk_pvt *tmp;
        char *from;
 
-       ast_log(LOG_DEBUG, "The client is %s\n", client->name);
+       ast_debug(1, "The client is %s\n", client->name);
        /* find corresponding call */
        for (tmp = client->p; tmp; tmp = tmp->next) {
                if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
@@ -810,6 +824,34 @@ static int gtalk_hangup_farend(struct gtalk *client, ikspak *pak)
        return 1;
 }
 
+static int gtalk_get_local_ip(struct ast_sockaddr *ourip)
+{
+       struct ast_sockaddr root;
+       struct ast_sockaddr bindaddr_tmp;
+       struct ast_sockaddr *addrs;
+       int addrs_cnt;
+
+       /* If bind address is not 0.0.0.0, then bindaddr is our local ip. */
+       ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
+       if (!ast_sockaddr_is_any(&bindaddr_tmp)) {
+               ast_sockaddr_copy(ourip, &bindaddr_tmp);
+               return 0;
+       }
+
+       /* If no bind address was provided, lets see what ip we would use to connect to google.com and use that.
+        * If you can't resolve google.com from your network, then this module is useless for you anyway. */
+       if ((addrs_cnt = ast_sockaddr_resolve(&addrs, "google.com", PARSE_PORT_FORBID, AF_INET)) > 0) {
+               ast_sockaddr_copy(&root, &addrs[0]);
+               ast_free(addrs);
+               if (!ast_ouraddrfor(&root, ourip)) {
+                       return 0;
+               }
+       }
+
+       /* As a last resort, use this function to find our local address. */
+       return ast_find_ourip(ourip, &bindaddr_tmp, AF_INET);
+}
+
 static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, char *sid, char *from, char *to)
 {
        struct gtalk_candidate *tmp;
@@ -817,7 +859,6 @@ static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, ch
        struct gtalk_candidate *ours1 = NULL, *ours2 = NULL;
        struct sockaddr_in sin = { 0, };
        struct ast_sockaddr sin_tmp;
-       struct ast_sockaddr bindaddr_tmp;
        struct ast_sockaddr us;
        iks *iq, *gtalk, *candidate, *transport;
        char user[17], pass[17], preference[5], port[7];
@@ -838,13 +879,8 @@ static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, ch
 
        iks_insert_attrib(transport, "xmlns",GOOGLE_TRANSPORT_NS);
        iks_insert_node(iq, gtalk);
-
-       if (p->ctype == AJI_CLIENT_GMAIL) {
-               iks_insert_node(gtalk,candidate);
-       } else {
-               iks_insert_node(gtalk,candidate);
-               iks_insert_node(gtalk,transport);
-       }
+       iks_insert_node(gtalk,candidate);
+       iks_insert_node(gtalk,transport);
 
        for (; p; p = p->next) {
                if (!strcasecmp(p->sid, sid))
@@ -858,8 +894,9 @@ static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, ch
 
        ast_rtp_instance_get_local_address(p->rtp, &sin_tmp);
        ast_sockaddr_to_sin(&sin_tmp, &sin);
-       ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
-       ast_find_ourip(&us, &bindaddr_tmp);
+
+       gtalk_get_local_ip(&us);
+
        if (!strcmp(ast_sockaddr_stringify_addr(&us), "127.0.0.1")) {
                ast_log(LOG_WARNING, "Found a loopback IP on the system, check your network configuration or set the bindaddr attribute.");
        }
@@ -906,18 +943,7 @@ static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, ch
                iks_insert_attrib(iq, "type", "set");
                iks_insert_attrib(iq, "id", c->mid);
                ast_aji_increment_mid(c->mid);
-               switch (p->ctype) {
-               case AJI_CLIENT_GTALK:
-                       iks_insert_attrib(gtalk, "type", "transport-info");
-                       break;
-               case AJI_CLIENT_GMAIL:
-                       iks_insert_attrib(gtalk, "type", "candidates");
-                       break;
-               default:
-                       ast_log(LOG_WARNING, "Client type is unknown\n");
-                       iks_insert_attrib(gtalk, "type", "candidates");
-                       break;
-               }
+               iks_insert_attrib(gtalk, "type", "candidates");
                iks_insert_attrib(gtalk, "id", sid);
                /* put the initiator attribute to lower case if we receive the call
                 * otherwise GoogleTalk won't establish the session */
@@ -971,7 +997,6 @@ static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const
        char idroster[200];
        char *data, *exten = NULL;
        struct ast_sockaddr bindaddr_tmp;
-       enum gtalk_client_type ctype = AJI_CLIENT_UNKNOWN;
 
        ast_debug(1, "The client is %s for alloc\n", client->name);
        if (!sid && !strchr(them, '/')) {       /* I started call! */
@@ -992,12 +1017,8 @@ static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const
                }
                if (resources) {
                        snprintf(idroster, sizeof(idroster), "%s/%s", them, resources->resource);
-                       if (strstr(resources->resource, "gmail")) {
-                               ctype = AJI_CLIENT_GMAIL;
-                       }
                } else if ((*them == '+') || (strstr(them, "@voice.google.com"))) {
-                       snprintf(idroster, sizeof(idroster), "%s/srvres", them);
-                       ctype = AJI_CLIENT_GOOGLE_VOICE;
+                       snprintf(idroster, sizeof(idroster), "%s", them);
                } else {
                        ast_log(LOG_ERROR, "no gtalk capable clients to talk to.\n");
                        return NULL;
@@ -1006,8 +1027,16 @@ static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const
        if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
                return NULL;
        }
-       /* set client type to unknown until we have more info */
-       tmp->ctype = ctype;
+       tmp->cap = ast_format_cap_alloc_nolock();
+       tmp->jointcap = ast_format_cap_alloc_nolock();
+       tmp->peercap = ast_format_cap_alloc_nolock();
+       if (!tmp->jointcap || !tmp->peercap || !tmp->cap) {
+               tmp->cap = ast_format_cap_destroy(tmp->cap);
+               tmp->jointcap = ast_format_cap_destroy(tmp->jointcap);
+               tmp->peercap = ast_format_cap_destroy(tmp->peercap);
+               ast_free(tmp);
+               return NULL;
+       }
 
        memcpy(&tmp->prefs, &client->prefs, sizeof(struct ast_codec_pref));
 
@@ -1036,10 +1065,11 @@ static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const
        ast_rtp_codecs_payloads_clear(ast_rtp_instance_get_codecs(tmp->rtp), tmp->rtp);
 
        /* add user configured codec capabilites */
-       if (client->capability)
-               tmp->capability = client->capability;
-       else if (global_capability)
-               tmp->capability = global_capability;
+       if (!(ast_format_cap_is_empty(client->cap))) {
+               ast_format_cap_copy(tmp->cap, client->cap);
+       } else if (!(ast_format_cap_is_empty(global_capability))) {
+               ast_format_cap_copy(tmp->cap, global_capability);
+       }
 
        tmp->parent = client;
        if (!tmp->rtp) {
@@ -1054,8 +1084,9 @@ static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const
        if(strchr(tmp->us, '/')) {
                data = ast_strdupa(tmp->us);
                exten = strsep(&data, "/");
-       } else
+       } else {
                exten = tmp->us;
+       }
        ast_copy_string(tmp->exten,  exten, sizeof(tmp->exten));
        ast_mutex_init(&tmp->lock);
        ast_mutex_lock(&gtalklock);
@@ -1069,9 +1100,9 @@ static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const
 static struct ast_channel *gtalk_new(struct gtalk *client, struct gtalk_pvt *i, int state, const char *title, const char *linkedid)
 {
        struct ast_channel *tmp;
-       int fmt;
-       int what;
        const char *n2;
+       struct ast_format_cap *what; /* used as SHALLOW COPY DO NOT DESTROY */
+       struct ast_format tmpfmt;
 
        if (title)
                n2 = title;
@@ -1086,20 +1117,29 @@ static struct ast_channel *gtalk_new(struct gtalk *client, struct gtalk_pvt *i,
 
        /* Select our native format based on codec preference until we receive
           something from another device to the contrary. */
-       if (i->jointcapability)
-               what = i->jointcapability;
-       else if (i->capability)
-               what = i->capability;
-       else
+       if (!(ast_format_cap_is_empty(i->jointcap))) {
+               what = i->jointcap;
+       } else if (i->cap) {
+               what = i->cap;
+       } else {
                what = global_capability;
+       }
 
        /* Set Frame packetization */
        if (i->rtp) {
                ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(i->rtp), i->rtp, &i->prefs);
        }
 
-       tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK);
-       fmt = ast_best_codec(tmp->nativeformats);
+       ast_codec_choose(&i->prefs, what, 1, &tmpfmt);
+       ast_format_cap_add(tmp->nativeformats, &tmpfmt);
+
+       ast_format_cap_iter_start(i->jointcap);
+       while (!(ast_format_cap_iter_next(i->jointcap, &tmpfmt))) {
+               if (AST_FORMAT_GET_TYPE(tmpfmt.id) == AST_FORMAT_TYPE_VIDEO) {
+                       ast_format_cap_add(tmp->nativeformats, &tmpfmt);
+               }
+       }
+       ast_format_cap_iter_end(i->jointcap);
 
        if (i->rtp) {
                ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(i->rtp, 0));
@@ -1112,10 +1152,12 @@ static struct ast_channel *gtalk_new(struct gtalk *client, struct gtalk_pvt *i,
        if (state == AST_STATE_RING)
                tmp->rings = 1;
        tmp->adsicpe = AST_ADSI_UNAVAILABLE;
-       tmp->writeformat = fmt;
-       tmp->rawwriteformat = fmt;
-       tmp->readformat = fmt;
-       tmp->rawreadformat = fmt;
+
+       ast_best_codec(tmp->nativeformats, &tmpfmt);
+       ast_format_copy(&tmp->writeformat, &tmpfmt);
+       ast_format_copy(&tmp->rawwriteformat, &tmpfmt);
+       ast_format_copy(&tmp->readformat, &tmpfmt);
+       ast_format_copy(&tmp->rawreadformat, &tmpfmt);
        tmp->tech_pvt = i;
 
        tmp->callgroup = client->callgroup;
@@ -1228,6 +1270,9 @@ static void gtalk_free_pvt(struct gtalk *client, struct gtalk_pvt *p)
        if (p->vrtp)
                ast_rtp_instance_destroy(p->vrtp);
        gtalk_free_candidates(p->theircandidates);
+       p->cap = ast_format_cap_destroy(p->cap);
+       p->jointcap = ast_format_cap_destroy(p->jointcap);
+       p->peercap = ast_format_cap_destroy(p->peercap);
        ast_free(p);
 }
 
@@ -1280,14 +1325,6 @@ static int gtalk_newcall(struct gtalk *client, ikspak *pak)
                return -1;
        }
 
-       /* if the node name of the query contains a semicolon, the remote peer
-        * is a gmail type client. If not, treat it as a regular GoogleTalk
-        * client */
-       if (strchr(iks_name(pak->query), ':')) {
-               p->ctype = AJI_CLIENT_GMAIL;
-       } else {
-               p->ctype = AJI_CLIENT_GTALK;
-       }
        chan = gtalk_new(client, p, AST_STATE_DOWN, pak->from->user, NULL);
        if (!chan) {
                gtalk_free_pvt(client, p);
@@ -1308,26 +1345,44 @@ static int gtalk_newcall(struct gtalk *client, ikspak *pak)
                        codec = iks_next_tag(codec);
                        continue;
                }
-               if (!strcmp(iks_name(codec), "vid:payload-type") && p->vrtp) {
-                       ast_rtp_codecs_payloads_set_m_type(ast_rtp_instance_get_codecs(p->vrtp), p->vrtp, atoi(codec_id));
-                       ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->vrtp), p->vrtp, atoi(codec_id), "video", codec_name, 0);
+               if (!strcmp(S_OR(iks_name(codec), ""), "vid:payload-type") && p->vrtp) {
+                       ast_rtp_codecs_payloads_set_m_type(
+                               ast_rtp_instance_get_codecs(p->vrtp),
+                               p->vrtp,
+                               atoi(codec_id));
+                       ast_rtp_codecs_payloads_set_rtpmap_type(
+                               ast_rtp_instance_get_codecs(p->vrtp),
+                               p->vrtp,
+                               atoi(codec_id),
+                               "video",
+                               codec_name,
+                               0);
                } else {
-                       ast_rtp_codecs_payloads_set_m_type(ast_rtp_instance_get_codecs(p->rtp), p->rtp, atoi(codec_id));
-                       ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp), p->rtp, atoi(codec_id), "audio", codec_name, 0);
+                       ast_rtp_codecs_payloads_set_m_type(
+                               ast_rtp_instance_get_codecs(p->rtp),
+                               p->rtp,
+                               atoi(codec_id));
+                       ast_rtp_codecs_payloads_set_rtpmap_type(
+                               ast_rtp_instance_get_codecs(p->rtp),
+                               p->rtp,
+                               atoi(codec_id),
+                               "audio",
+                               codec_name,
+                               0);
                }
                codec = iks_next_tag(codec);
        }
 
        /* Now gather all of the codecs that we are asked for */
-       ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(p->rtp), &p->peercapability, &peernoncodeccapability);
-       p->jointcapability = p->capability & p->peercapability;
+       ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(p->rtp), p->peercap, &peernoncodeccapability);
+       ast_format_cap_joint_copy(p->cap, p->peercap, p->jointcap);
        ast_mutex_unlock(&p->lock);
 
        ast_setstate(chan, AST_STATE_RING);
-       if (!p->jointcapability) {
-               ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, p->capability),
-                       ast_getformatname_multiple(s2, BUFSIZ, p->peercapability),
-                       ast_getformatname_multiple(s3, BUFSIZ, p->jointcapability));
+       if (ast_format_cap_is_empty(p->jointcap)) {
+               ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, p->cap),
+                       ast_getformatname_multiple(s2, BUFSIZ, p->peercap),
+                       ast_getformatname_multiple(s3, BUFSIZ, p->jointcap));
                /* close session if capabilities don't match */
                gtalk_action(client, p, "reject");
                p->alreadygone = 1;
@@ -1349,9 +1404,6 @@ static int gtalk_newcall(struct gtalk *client, ikspak *pak)
                break;
        case AST_PBX_SUCCESS:
                gtalk_response(client, from, pak, NULL, NULL);
-               if (p->ctype == AJI_CLIENT_GTALK) {
-                       gtalk_invite_response(p, p->them, p->us,p->sid, 0);
-               }
                gtalk_create_candidates(client, p, p->sid, p->them, p->us);
                /* nothing to do */
                break;
@@ -1477,15 +1529,15 @@ static int gtalk_add_candidate(struct gtalk *client, ikspak *pak)
        }
        traversenodes = pak->query;
        while(traversenodes) {
-               if(!strcasecmp(iks_name(traversenodes), "session")) {
+               if(!strcasecmp(S_OR(iks_name(traversenodes), ""), "session")) {
                        traversenodes = iks_first_tag(traversenodes);
                        continue;
                }
-               if(!strcasecmp(iks_name(traversenodes), "ses:session")) {
+               if(!strcasecmp(S_OR(iks_name(traversenodes), ""), "ses:session")) {
                        traversenodes = iks_child(traversenodes);
                        continue;
                }
-               if(!strcasecmp(iks_name(traversenodes), "candidate") || !strcasecmp(iks_name(traversenodes), "ses:candidate")) {
+               if(!strcasecmp(S_OR(iks_name(traversenodes), ""), "candidate") || !strcasecmp(S_OR(iks_name(traversenodes), ""), "ses:candidate")) {
                        newcandidate = ast_calloc(1, sizeof(*newcandidate));
                        if (!newcandidate)
                                return 0;
@@ -1553,12 +1605,12 @@ static struct ast_frame *gtalk_rtp_read(struct ast_channel *ast, struct gtalk_pv
        if (p->owner) {
                /* We already hold the channel lock */
                if (f->frametype == AST_FRAME_VOICE) {
-                       if (f->subclass.codec != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
-                               ast_debug(1, "Oooh, format changed to %s\n", ast_getformatname(f->subclass.codec));
-                               p->owner->nativeformats =
-                                       (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass.codec;
-                               ast_set_read_format(p->owner, p->owner->readformat);
-                               ast_set_write_format(p->owner, p->owner->writeformat);
+                       if (!ast_format_cap_iscompatible(p->owner->nativeformats, &f->subclass.format)) {
+                               ast_debug(1, "Oooh, format changed to %s\n", ast_getformatname(&f->subclass.format));
+                               ast_format_cap_remove_bytype(p->owner->nativeformats, AST_FORMAT_TYPE_AUDIO);
+                               ast_format_cap_add(p->owner->nativeformats, &f->subclass.format);
+                               ast_set_read_format(p->owner, &p->owner->readformat);
+                               ast_set_write_format(p->owner, &p->owner->writeformat);
                        }
                        /* if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
                                f = ast_dsp_process(p->owner, p->vad, f);
@@ -1590,13 +1642,13 @@ static int gtalk_write(struct ast_channel *ast, struct ast_frame *frame)
 
        switch (frame->frametype) {
        case AST_FRAME_VOICE:
-               if (!(frame->subclass.codec & ast->nativeformats)) {
+               if (!(ast_format_cap_iscompatible(ast->nativeformats, &frame->subclass.format))) {
                        ast_log(LOG_WARNING,
                                        "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
-                                       ast_getformatname(frame->subclass.codec),
+                                       ast_getformatname(&frame->subclass.format),
                                        ast_getformatname_multiple(buf, sizeof(buf), ast->nativeformats),
-                                       ast_getformatname(ast->readformat),
-                                       ast_getformatname(ast->writeformat));
+                                       ast_getformatname(&ast->readformat),
+                                       ast_getformatname(&ast->writeformat));
                        return 0;
                }
                if (p) {
@@ -1655,7 +1707,7 @@ static int gtalk_indicate(struct ast_channel *ast, int condition, const void *da
                ast_moh_stop(ast);
                break;
        default:
-               ast_log(LOG_NOTICE, "Don't know how to indicate condition '%d'\n", condition);
+               ast_debug(3, "Don't know how to indicate condition '%d'\n", condition);
                res = -1;
        }
 
@@ -1779,24 +1831,6 @@ static int gtalk_sendhtml(struct ast_channel *ast, int subclass, const char *dat
        return -1;
 }
 
-/* Not in use right now.
-static int gtalk_auto_congest(void *nothing)
-{
-       struct gtalk_pvt *p = nothing;
-
-       ast_mutex_lock(&p->lock);
-       if (p->owner) {
-               if (!ast_channel_trylock(p->owner)) {
-                       ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
-                       ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
-                       ast_channel_unlock(p->owner);
-               }
-       }
-       ast_mutex_unlock(&p->lock);
-       return 0;
-}
-*/
-
 /*!\brief Initiate new call, part of PBX interface
  * dest is the dial string */
 static int gtalk_call(struct ast_channel *ast, char *dest, int timeout)
@@ -1813,11 +1847,11 @@ static int gtalk_call(struct ast_channel *ast, char *dest, int timeout)
                ast_copy_string(p->ring, p->parent->connection->mid, sizeof(p->ring));
                p->ringrule = iks_filter_add_rule(p->parent->connection->f, gtalk_ringing_ack, p,
                                                        IKS_RULE_ID, p->ring, IKS_RULE_DONE);
-       } else
+       } else {
                ast_log(LOG_WARNING, "Whoa, already have a ring rule!\n");
+       }
 
        gtalk_invite(p, p->them, p->us, p->sid, 1);
-       gtalk_create_candidates(p->parent, p, p->sid, p->them, p->us);
 
        return 0;
 }
@@ -1844,7 +1878,7 @@ static int gtalk_hangup(struct ast_channel *ast)
 }
 
 /*!\brief Part of PBX interface */
-static struct ast_channel *gtalk_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
+static struct ast_channel *gtalk_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause)
 {
        struct gtalk_pvt *p = NULL;
        struct gtalk *client = NULL;
@@ -1855,8 +1889,9 @@ static struct ast_channel *gtalk_request(const char *type, format_t format, cons
                s = ast_strdupa(data);
                if (s) {
                        sender = strsep(&s, "/");
-                       if (sender && (sender[0] != '\0'))
+                       if (sender && (sender[0] != '\0')) {
                                to = strsep(&s, "/");
+                       }
                        if (!to) {
                                ast_log(LOG_ERROR, "Bad arguments in Gtalk Dialstring: %s\n", (char*) data);
                                return NULL;
@@ -1935,8 +1970,8 @@ static char *gtalk_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cl
                                        chan->name,
                                        jid,
                                        resource,
-                                       ast_getformatname(chan->readformat),
-                                       ast_getformatname(chan->writeformat)
+                                       ast_getformatname(&chan->readformat),
+                                       ast_getformatname(&chan->writeformat)
                                        );
                        else
                                ast_log(LOG_WARNING, "No available channel\n");
@@ -1953,6 +1988,42 @@ static char *gtalk_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cl
 #undef FORMAT
 }
 
+/*! \brief List global settings for the GoogleTalk channel */
+static char *gtalk_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+       char codec_buf[BUFSIZ];
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "gtalk show settings";
+               e->usage =
+                       "Usage: gtalk show settings\n"
+                       "       Provides detailed list of the configuration on the GoogleTalk channel.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }
+
+       if (a->argc != 3) {
+               return CLI_SHOWUSAGE;
+       }
+
+#define FORMAT "  %-25.20s  %-15.30s\n"
+
+       ast_cli(a->fd, "\nGlobal Settings:\n");
+       ast_cli(a->fd, "----------------\n");
+       ast_cli(a->fd, FORMAT, "UDP Bindaddress:", ast_inet_ntoa(bindaddr.sin_addr));
+       ast_cli(a->fd, FORMAT, "Stun Address:", global_stunaddr != 0 ? ast_inet_ntoa(stunaddr.sin_addr) : "Disabled");
+       ast_cli(a->fd, FORMAT, "External IP:", S_OR(externip, "Disabled"));
+       ast_cli(a->fd, FORMAT, "Context:", global_context);
+       ast_cli(a->fd, FORMAT, "Codecs:", ast_getformatname_multiple(codec_buf, sizeof(codec_buf) - 1, global_capability));
+       ast_cli(a->fd, FORMAT, "Parking Lot:", global_parkinglot);
+       ast_cli(a->fd, FORMAT, "Allow Guest:", AST_CLI_YESNO(global_allowguest));
+       ast_cli(a->fd, "\n----\n");
+
+       return CLI_SUCCESS;
+#undef FORMAT
+}
+
 /*! \brief CLI command "gtalk reload"
  *  \todo XXX TODO make this work. */
 #if 0
@@ -2029,9 +2100,9 @@ static int gtalk_create_member(char *label, struct ast_variable *var, int allowg
                if (!strcasecmp(var->name, "username"))
                        ast_copy_string(member->user, var->value, sizeof(member->user));
                else if (!strcasecmp(var->name, "disallow"))
-                       ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 0);
+                       ast_parse_allow_disallow(&member->prefs, member->cap, var->value, 0);
                else if (!strcasecmp(var->name, "allow"))
-                       ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 1);
+                       ast_parse_allow_disallow(&member->prefs, member->cap, var->value, 1);
                else if (!strcasecmp(var->name, "context"))
                        ast_copy_string(member->context, var->value, sizeof(member->context));
                else if (!strcasecmp(var->name, "parkinglot"))
@@ -2063,9 +2134,6 @@ static int gtalk_load_config(void)
 {
        char *cat = NULL;
        struct ast_config *cfg = NULL;
-       char context[AST_MAX_CONTEXT];
-       char parkinglot[AST_MAX_CONTEXT];
-       int allowguest = 1;
        struct ast_variable *var;
        struct gtalk *member;
        struct ast_codec_pref prefs;
@@ -2087,26 +2155,32 @@ static int gtalk_load_config(void)
        memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
 
        /* set defaults */
+       memset(&prefs, 0, sizeof(prefs));
        memset(&stunaddr, 0, sizeof(stunaddr));
+       global_stunaddr = 0;
+       global_allowguest = DEFAULT_ALLOWGUEST;
+       ast_copy_string(global_context, DEFAULT_CONTEXT, sizeof(global_context));
+       ast_copy_string(global_parkinglot, DEFAULT_PARKINGLOT, sizeof(global_parkinglot));
 
        cat = ast_category_browse(cfg, NULL);
        for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
                /* handle jb conf */
-               if (!ast_jb_read_conf(&global_jbconf, var->name, var->value))
+               if (!ast_jb_read_conf(&global_jbconf, var->name, var->value)) {
                        continue;
+               }
 
                if (!strcasecmp(var->name, "allowguest")) {
-                       allowguest = (ast_true(ast_variable_retrieve(cfg, "general", "allowguest"))) ? 1 : 0;
+                       global_allowguest = (ast_true(ast_variable_retrieve(cfg, "general", "allowguest"))) ? 1 : 0;
                } else if (!strcasecmp(var->name, "disallow")) {
-                       ast_parse_allow_disallow(&prefs, &global_capability, var->value, 0);
+                       ast_parse_allow_disallow(&prefs, global_capability, var->value, 0);
                } else if (!strcasecmp(var->name, "allow")) {
-                       ast_parse_allow_disallow(&prefs, &global_capability, var->value, 1);
+                       ast_parse_allow_disallow(&prefs, global_capability, var->value, 1);
                } else if (!strcasecmp(var->name, "context")) {
-                       ast_copy_string(context, var->value, sizeof(context));
+                       ast_copy_string(global_context, var->value, sizeof(global_context));
                } else if (!strcasecmp(var->name, "externip")) {
                        ast_copy_string(externip, var->value, sizeof(externip));
                } else if (!strcasecmp(var->name, "parkinglot")) {
-                       ast_copy_string(parkinglot, var->value, sizeof(parkinglot));
+                       ast_copy_string(global_parkinglot, var->value, sizeof(global_parkinglot));
                } else if (!strcasecmp(var->name, "bindaddr")) {
                        if (!(hp = ast_gethostbyname(var->value, &ahp))) {
                                ast_log(LOG_WARNING, "Invalid address: %s\n", var->value);
@@ -2115,6 +2189,7 @@ static int gtalk_load_config(void)
                        }
                } else if (!strcasecmp(var->name, "stunaddr")) {
                        stunaddr.sin_port = htons(STANDARD_STUN_PORT);
+                       global_stunaddr = 1;
                        if (ast_parse_arg(var->value, PARSE_INADDR, &stunaddr)) {
                                ast_log(LOG_WARNING, "Invalid STUN server address: %s\n", var->value);
                        }
@@ -2126,19 +2201,20 @@ static int gtalk_load_config(void)
                        member = ast_calloc(1, sizeof(*member));
                        ASTOBJ_INIT(member);
                        ASTOBJ_WRLOCK(member);
+                       member->cap = ast_format_cap_alloc_nolock();
                        if (!strcasecmp(cat, "guest")) {
                                ast_copy_string(member->name, "guest", sizeof(member->name));
                                ast_copy_string(member->user, "guest", sizeof(member->user));
-                               ast_copy_string(member->context, context, sizeof(member->context));
-                               ast_copy_string(member->parkinglot, parkinglot, sizeof(member->parkinglot));
-                               member->allowguest = allowguest;
+                               ast_copy_string(member->context, global_context, sizeof(member->context));
+                               ast_copy_string(member->parkinglot, global_parkinglot, sizeof(member->parkinglot));
+                               member->allowguest = global_allowguest;
                                member->prefs = prefs;
                                while (var) {
                                        if (!strcasecmp(var->name, "disallow")) {
-                                               ast_parse_allow_disallow(&member->prefs, &member->capability,
+                                               ast_parse_allow_disallow(&member->prefs, member->cap,
                                                                                                 var->value, 0);
                                        } else if (!strcasecmp(var->name, "allow")) {
-                                               ast_parse_allow_disallow(&member->prefs, &member->capability,
+                                               ast_parse_allow_disallow(&member->prefs, member->cap,
                                                                                                 var->value, 1);
                                        } else if (!strcasecmp(var->name, "context")) {
                                                ast_copy_string(member->context, var->value,
@@ -2169,8 +2245,9 @@ static int gtalk_load_config(void)
                                }
                        } else {
                                ASTOBJ_UNLOCK(member);
-                               if (gtalk_create_member(cat, var, allowguest, prefs, context, member))
+                               if (gtalk_create_member(cat, var, global_allowguest, prefs, global_context, member)) {
                                        ASTOBJ_CONTAINER_LINK(&gtalk_list, member);
+                               }
                                ASTOBJ_UNREF(member, gtalk_member_destroy);
                        }
                }
@@ -2187,8 +2264,22 @@ static int load_module(void)
 {
        struct ast_sockaddr bindaddr_tmp;
        struct ast_sockaddr ourip_tmp;
-
        char *jabber_loaded = ast_module_helper("", "res_jabber.so", 0, 0, 0, 0);
+       struct ast_format tmpfmt;
+
+       if (!(gtalk_tech.capabilities = ast_format_cap_alloc())) {
+               return AST_MODULE_LOAD_DECLINE;
+       }
+       if (!(global_capability = ast_format_cap_alloc())) {
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
+       ast_format_cap_add_all_by_type(gtalk_tech.capabilities, AST_FORMAT_TYPE_AUDIO);
+       ast_format_cap_add(global_capability, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0));
+       ast_format_cap_add(global_capability, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0));
+       ast_format_cap_add(global_capability, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0));
+       ast_format_cap_add(global_capability, ast_format_set(&tmpfmt, AST_FORMAT_H263, 0));
+
        free(jabber_loaded);
        if (!jabber_loaded) {
                /* If embedded, check for a different module name */
@@ -2206,7 +2297,7 @@ static int load_module(void)
                return 0;
        }
 
-       sched = sched_context_create();
+       sched = ast_sched_context_create();
        if (!sched) {
                ast_log(LOG_WARNING, "Unable to create schedule context\n");
        }
@@ -2217,7 +2308,7 @@ static int load_module(void)
        }
 
        ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
-       if (ast_find_ourip(&ourip_tmp, &bindaddr_tmp)) {
+       if (gtalk_get_local_ip(&ourip_tmp)) {
                ast_log(LOG_WARNING, "Unable to get own IP address, Gtalk disabled\n");
                return 0;
        }
@@ -2271,6 +2362,8 @@ static int unload_module(void)
        }
        ASTOBJ_CONTAINER_DESTROYALL(&gtalk_list, gtalk_member_destroy);
        ASTOBJ_CONTAINER_DESTROY(&gtalk_list);
+       global_capability = ast_format_cap_destroy(global_capability);
+       gtalk_tech.capabilities = ast_format_cap_destroy(gtalk_tech.capabilities);
        return 0;
 }
 
@@ -2279,4 +2372,4 @@ AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Gtalk Channel Driver"
                .unload = unload_module,
                /* .reload = reload, */
                .load_pri = AST_MODPRI_CHANNEL_DRIVER,
-              );
+               );