[asterisk-commits] file: branch file/chan_jingle2 r365458 - /team/file/chan_jingle2/channels/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun May 6 10:27:11 CDT 2012


Author: file
Date: Sun May  6 10:27:07 2012
New Revision: 365458

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=365458
Log:
So I went a little coding crazy Friday night and yesterday... the end result being this: chan_jingle2, a channel driver supporting
the Jingle, Google Jingle, and original Google Phone specifications.

The Jingle specification (XEP-0166) support includes ICE-UDP (XEP-0176) and RTP (XEP-0167) for inbound and outbound.
ICE negotiation works happily, reasons are mapped bidirectionally with cause codes, ringing indication works on supported clients as does hold and unhold.
Codec support includes any available in Asterisk, including G.722.

The Google Jingle specification support is similar to the above except the original Asterisk STUN support is used instead of the new
STUN/TURN/ICE support.

The Google Phone specification support includes inbound and outbound calling using the pre-Jingle signaling. The original Asterisk STUN
support is used instead of the new STUN/TURN/ICE support like the Google Jingle support.

Clients tested include Jitsi, Windows Google Talk Client, Google Talk Plug-in in GMail, and Google Voice.

There is still more testing to do and tweaking but this is a substantial step forward.


Added:
    team/file/chan_jingle2/channels/chan_jingle2.c   (with props)

Added: team/file/chan_jingle2/channels/chan_jingle2.c
URL: http://svnview.digium.com/svn/asterisk/team/file/chan_jingle2/channels/chan_jingle2.c?view=auto&rev=365458
==============================================================================
--- team/file/chan_jingle2/channels/chan_jingle2.c (added)
+++ team/file/chan_jingle2/channels/chan_jingle2.c Sun May  6 10:27:07 2012
@@ -1,0 +1,2204 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2012, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ *
+ * \brief Jingle Channel Driver
+ *
+ * \extref Iksemel http://iksemel.jabberstudio.org/
+ * 
+ * \ingroup channel_drivers
+ */
+
+/*** MODULEINFO
+	<depend>iksemel</depend>
+	<depend>res_jabber</depend>
+	<use type="external">openssl</use>
+	<defaultenabled>no</defaultenabled>
+	<support_level>extended</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <sys/socket.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/signal.h>
+#include <iksemel.h>
+#include <pthread.h>
+
+#include "asterisk/lock.h"
+#include "asterisk/channel.h"
+#include "asterisk/config.h"
+#include "asterisk/module.h"
+#include "asterisk/pbx.h"
+#include "asterisk/sched.h"
+#include "asterisk/io.h"
+#include "asterisk/rtp_engine.h"
+#include "asterisk/acl.h"
+#include "asterisk/callerid.h"
+#include "asterisk/file.h"
+#include "asterisk/cli.h"
+#include "asterisk/app.h"
+#include "asterisk/musiconhold.h"
+#include "asterisk/manager.h"
+#include "asterisk/stringfields.h"
+#include "asterisk/utils.h"
+#include "asterisk/causes.h"
+#include "asterisk/astobj.h"
+#include "asterisk/abstract_jb.h"
+#include "asterisk/jabber.h"
+
+/*! \brief Default jitterbuffer configuration */
+static struct ast_jb_conf default_jbconf =
+{
+	.flags = 0,
+	.max_size = -1,
+	.resync_threshold = -1,
+	.impl = "",
+	.target_extra = -1,
+};
+
+/*! \brief Global jitterbuffer configuration */
+static struct ast_jb_conf global_jbconf;
+
+/*! \brief Default filename for the configuration file */
+#define JINGLE_CONFIG "jingle2.conf"
+
+/*! \brief Maximum size for a username */
+#define MAX_SIZE_USERNAME 100
+
+/*! \brief Maximum size for a session identifier */
+#define MAX_SIZE_SID 100
+
+/*! \brief Maximum number of ICE candidates we will offer */
+#define MAX_ICE_CANDIDATES 10
+
+/*! \brief Maximum number of payloads we will offer */
+#define MAX_PAYLOADS 30
+
+/*! \brief Number of buckets for endpoints */
+#define ENDPOINT_BUCKETS 37
+
+/*! \brief Number of buckets for sessions, on a per-endpoint basis */
+#define SESSION_BUCKETS 37
+
+/*! \brief Namespace for Jingle itself */
+#define JINGLE_NS "urn:xmpp:jingle:1"
+
+/*! \brief Namespace for Jingle RTP sessions */
+#define JINGLE_RTP_NS "urn:xmpp:jingle:apps:rtp:1"
+
+/*! \brief Namespace for Jingle RTP info */
+#define JINGLE_RTP_INFO_NS "urn:xmpp:jingle:apps:rtp:info:1"
+
+/*! \brief Namespace for Jingle ICE-UDP */
+#define JINGLE_ICE_UDP_NS "urn:xmpp:jingle:transports:ice-udp:1"
+
+/*! \brief Namespace for Google Talk ICE-UDP */
+#define GOOGLE_TRANSPORT_NS "http://www.google.com/transport/p2p"
+
+/*! \brief Namespace for Google Talk Raw UDP */
+#define GOOGLE_TRANSPORT_RAW_NS "http://www.google.com/transport/raw-udp"
+
+/*! \brief Namespace for Google Session */
+#define GOOGLE_SESSION_NS "http://www.google.com/session"
+
+/*! \brief Namespace for Google Phone description */
+#define GOOGLE_PHONE_NS "http://www.google.com/session/phone"
+
+/*! \brief Namespace for XMPP stanzas */
+#define XMPP_STANZAS_NS "urn:ietf:params:xml:ns:xmpp-stanzas"
+
+/*! \brief The various transport methods supported, from highest priority to lowest priority when doing fallback */
+enum jingle_transport {
+	JINGLE_TRANSPORT_ICE_UDP = 3,   /*!< XEP-0176 */
+	JINGLE_TRANSPORT_GOOGLE_V2 = 2, /*!< https://developers.google.com/talk/call_signaling */
+	JINGLE_TRANSPORT_GOOGLE_V1 = 1, /*!< Undocumented initial Google specification */
+	JINGLE_TRANSPORT_NONE = 0,      /*!< No transport specified */
+};
+
+/*! \brief Endpoint which contains configuration information and active sessions */
+struct jingle_endpoint {
+	char name[MAX_SIZE_USERNAME];           /*!< Name of the endpoint */
+	struct aji_client *connection;          /*!< Connection to use for traffic */
+	iksrule *rule;                          /*!< Action matching rule */
+	struct ast_codec_pref prefs;            /*!< Codec preferences */
+	char context[AST_MAX_CONTEXT];          /*!< Context to place incoming calls into */
+	char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
+	struct ast_format_cap *cap;             /*!< Formats to use */
+	ast_group_t callgroup;                  /*!< Call group */
+	ast_group_t pickupgroup;                /*!< Pickup group */
+	char language[MAX_LANGUAGE];            /*!< Default language for prompts */
+	char musicclass[MAX_MUSICCLASS];        /*!< Configured music on hold class */
+	char parkinglot[AST_MAX_CONTEXT];       /*!< Configured parking lot */
+	enum jingle_transport transport;        /*!< Default transport to use on outgoing sessions */
+	struct ao2_container *sessions;         /*!< Active sessions to or from the endpoint */
+	unsigned int destroy:1;                 /*!< Bit to indicate that the endpoint should be destroyed */
+};
+
+/*! \brief Session which contains information about an active session */
+struct jingle_session {
+	char sid[MAX_SIZE_SID];               /*!< Session identifier */
+	struct jingle_endpoint *endpoint;     /*!< Endpoint we belong to */
+	struct aji_client *connection;        /*!< Connection to use for traffic */
+	enum jingle_transport transport;      /*!< Transport type to use for this session */
+	char remote_original[AJI_MAX_JIDLEN]; /*!< Identifier of the original remote party (remote may have changed due to redirect) */
+	char remote[AJI_MAX_JIDLEN];          /*!< Identifier of the remote party */
+	iksrule *rule;                        /*!< Session matching rule */
+	struct ast_codec_pref prefs;          /*!< Codec preferences */
+	struct ast_channel *owner;            /*!< Master Channel */
+	char audio_content_name[100];         /*!< Name attribute for audio content */
+	struct ast_rtp_instance *rtp;         /*!< RTP audio session */
+	char video_content_name[100];         /*!< Name attribute for video content */
+	struct ast_rtp_instance *vrtp;        /*!< RTP video session */
+	struct ast_format_cap *cap;           /*!< Local codec capabilities */
+	struct ast_format_cap *jointcap;      /*!< Joint codec capabilities */
+	struct ast_format_cap *peercap;       /*!< Peer codec capabilities */
+	unsigned int outgoing:1;              /*!< Whether this is an outgoing leg or not */
+	unsigned int gone:1;                  /*!< In the eyes of Jingle this session is already gone */
+};
+
+static const char desc[] = "Jingle Channel";
+static const char channel_type[] = "Jingle2";
+
+static struct ao2_container *endpoints; /*!< Configured endpoints */
+static struct ast_sched_context *sched; /*!< Scheduling context for RTCP */
+
+/* \brief Asterisk core interaction functions */
+static struct ast_channel *jingle_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause);
+static int jingle_sendtext(struct ast_channel *ast, const char *text);
+static int jingle_digit_begin(struct ast_channel *ast, char digit);
+static int jingle_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
+static int jingle_call(struct ast_channel *ast, const char *dest, int timeout);
+static int jingle_hangup(struct ast_channel *ast);
+static int jingle_answer(struct ast_channel *ast);
+static struct ast_frame *jingle_read(struct ast_channel *ast);
+static int jingle_write(struct ast_channel *ast, struct ast_frame *f);
+static int jingle_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
+static int jingle_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
+static struct jingle_session *jingle_alloc(struct jingle_endpoint *endpoint, const char *from, const char *sid);
+
+/*! \brief Action handlers */
+static void jingle_action_session_initiate(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
+static void jingle_action_transport_info(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
+static void jingle_action_session_accept(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
+static void jingle_action_session_info(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
+static void jingle_action_session_terminate(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
+
+/*! \brief PBX interface structure for channel registration */
+static struct ast_channel_tech jingle_tech = {
+	.type = "Jingle",
+	.description = "Jingle Channel Driver",
+	.requester = jingle_request,
+	.send_text = jingle_sendtext,
+	.send_digit_begin = jingle_digit_begin,
+	.send_digit_end = jingle_digit_end,
+	.bridge = ast_rtp_instance_bridge,
+	.call = jingle_call,
+	.hangup = jingle_hangup,
+	.answer = jingle_answer,
+	.read = jingle_read,
+	.write = jingle_write,
+	.exception = jingle_read,
+	.indicate = jingle_indicate,
+	.fixup = jingle_fixup,
+	.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER
+};
+
+/*! \brief Defined handlers for different Jingle actions */
+static const struct jingle_action_handler {
+	const char *action;
+	void (*handler)(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
+} jingle_action_handlers[] = {
+	/* Jingle actions */
+	{ "session-initiate", jingle_action_session_initiate, },
+	{ "transport-info", jingle_action_transport_info, },
+	{ "session-accept", jingle_action_session_accept, },
+	{ "session-info", jingle_action_session_info, },
+	{ "session-terminate", jingle_action_session_terminate, },
+	/* Google-V1 actions */
+	{ "initiate", jingle_action_session_initiate, },
+	{ "candidates", jingle_action_transport_info, },
+	{ "accept", jingle_action_session_accept, },
+	{ "terminate", jingle_action_session_terminate, },
+};
+
+/*! \brief Reason text <-> cause code mapping */
+static const struct jingle_reason_mapping {
+	const char *reason;
+	int cause;
+} jingle_reason_mappings[] = {
+	{ "busy", AST_CAUSE_BUSY, },
+	{ "cancel", AST_CAUSE_CALL_REJECTED, },
+	{ "connectivity-error", AST_CAUSE_INTERWORKING, },
+	{ "decline", AST_CAUSE_CALL_REJECTED, },
+	{ "expired", AST_CAUSE_NO_USER_RESPONSE, },
+	{ "failed-transport", AST_CAUSE_PROTOCOL_ERROR, },
+	{ "failed-application", AST_CAUSE_SWITCH_CONGESTION, },
+	{ "general-error", AST_CAUSE_CONGESTION, },
+	{ "gone", AST_CAUSE_NORMAL_CLEARING, },
+	{ "incompatible-parameters", AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, },
+	{ "media-error", AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, },
+	{ "security-error", AST_CAUSE_PROTOCOL_ERROR, },
+	{ "success", AST_CAUSE_NORMAL_CLEARING, },
+	{ "timeout", AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, },
+	{ "unsupported-applications", AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, },
+	{ "unsupported-transports", AST_CAUSE_FACILITY_NOT_IMPLEMENTED, },
+};
+
+/*! \brief Destructor for Jingle endpoints */
+static void jingle_endpoint_destructor(void *obj)
+{
+	struct jingle_endpoint *endpoint = obj;
+
+	if (endpoint->rule) {
+		iks_filter_remove_rule(endpoint->connection->f, endpoint->rule);
+	}
+
+	if (endpoint->connection) {
+		ASTOBJ_UNREF(endpoint->connection, ast_aji_client_destroy);
+	}
+
+	ast_format_cap_destroy(endpoint->cap);
+
+	ao2_ref(endpoint->sessions, -1);
+}
+
+/*! \brief Hashing function for Jingle endpoints */
+static int jingle_endpoint_hash(const void *obj, const int flags)
+{
+	const struct jingle_endpoint *endpoint = obj;
+
+	return ast_str_hash(endpoint->name);
+}
+
+/*! \brief Comparator function for Jingle endpoints */
+static int jingle_endpoint_cmp(void *obj, void *arg, int flags)
+{
+	struct jingle_endpoint *endpoint1 = obj, *endpoint2 = arg;
+
+	return !strcmp(endpoint1->name, endpoint2->name) ? CMP_MATCH | CMP_STOP : 0;
+}
+
+/*! \brief Destructor for Jingle sessions */
+static void jingle_session_destructor(void *obj)
+{
+	struct jingle_session *session = obj;
+
+	if (session->rule) {
+		iks_filter_remove_rule(session->connection->f, session->rule);
+	}
+
+	if (session->connection) {
+		ASTOBJ_UNREF(session->connection, ast_aji_client_destroy);
+	}
+
+	if (session->rtp) {
+		ast_rtp_instance_destroy(session->rtp);
+	}
+
+	if (session->vrtp) {
+		ast_rtp_instance_destroy(session->vrtp);
+	}
+
+	ast_format_cap_destroy(session->cap);
+	ast_format_cap_destroy(session->jointcap);
+	ast_format_cap_destroy(session->peercap);
+}
+
+/*! \brief Hashing function for Jingle sessions */
+static int jingle_session_hash(const void *obj, const int flags)
+{
+	const struct jingle_session *session = obj;
+
+	return ast_str_hash(session->sid);
+}
+
+/*! \brief Comparator function for Jingle sessions */
+static int jingle_session_cmp(void *obj, void *arg, int flags)
+{
+	struct jingle_session *session1 = obj, *session2 = arg;
+
+	return !strcmp(session1->sid, session2->sid) ? CMP_MATCH | CMP_STOP : 0;
+}
+
+/*! \brief Function called by RTP engine to get local RTP peer */
+static enum ast_rtp_glue_result jingle_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
+{
+	struct jingle_session *session = ast_channel_tech_pvt(chan);
+	enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_LOCAL;
+
+	if (!session->rtp) {
+		return AST_RTP_GLUE_RESULT_FORBID;
+	}
+
+	ao2_ref(session->rtp, +1);
+	*instance = session->rtp;
+
+	if (ast_test_flag(&global_jbconf, AST_JB_FORCED)) {
+		res = AST_RTP_GLUE_RESULT_FORBID;
+	}
+
+	return res;
+}
+
+/*! \brief Function called by RTP engine to get peer capabilities */
+static void jingle_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
+{
+}
+
+/*! \brief Function called by RTP engine to change where the remote party should send media */
+static int jingle_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *tpeer, const struct ast_format_cap *cap, int nat_active)
+{
+	return -1;
+}
+
+/*! \brief Local glue for interacting with the RTP engine core */
+static struct ast_rtp_glue jingle_rtp_glue = {
+	.type = "Jingle",
+	.get_rtp_info = jingle_get_rtp_peer,
+	.get_codec = jingle_get_codec,
+	.update_peer = jingle_set_rtp_peer,
+};
+
+/*! \brief Internal helper function used to allocate Jingle session on an endpoint */
+static struct jingle_session *jingle_alloc(struct jingle_endpoint *endpoint, const char *from, const char *sid)
+{
+	struct jingle_session *session;
+	struct ast_sockaddr tmp;
+
+	if (!(session = ao2_alloc(sizeof(*session), jingle_session_destructor))) {
+		return NULL;
+	}
+
+	if (!ast_strlen_zero(from)) {
+		ast_copy_string(session->remote_original, from, sizeof(session->remote_original));
+		ast_copy_string(session->remote, from, sizeof(session->remote));
+	}
+
+	if (ast_strlen_zero(sid)) {
+		snprintf(session->sid, sizeof(session->sid), "%08lx%08lx", ast_random(), ast_random());
+		session->outgoing = 1;
+		ast_copy_string(session->audio_content_name, "audio", sizeof(session->audio_content_name));
+		ast_copy_string(session->video_content_name, "video", sizeof(session->video_content_name));
+	} else {
+		ast_copy_string(session->sid, sid, sizeof(session->sid));
+	}
+
+	ao2_lock(endpoint);
+
+	ao2_ref(endpoint, +1);
+	session->endpoint = endpoint;
+	session->connection = ASTOBJ_REF(endpoint->connection);
+	session->transport = endpoint->transport;
+
+	if (!(session->cap = ast_format_cap_alloc_nolock()) ||
+	    !(session->jointcap = ast_format_cap_alloc_nolock()) ||
+	    !(session->peercap = ast_format_cap_alloc_nolock())) {
+		ao2_unlock(endpoint);
+		ao2_ref(endpoint, -1);
+		ao2_ref(session, -1);
+		return NULL;
+	}
+
+	ast_format_cap_copy(session->cap, endpoint->cap);
+
+	/* While we rely on res_jabber for communication we still need a temporary ast_sockaddr to tell the RTP engine
+	 * that we want IPv4 */
+	ast_sockaddr_parse(&tmp, "0.0.0.0", 0);
+
+	if (!(session->rtp = ast_rtp_instance_new("asterisk", sched, &tmp, NULL))) {
+		ao2_unlock(endpoint);
+		ao2_ref(endpoint, -1);
+		ao2_ref(session, -1);
+		return NULL;
+	}
+	ast_rtp_instance_set_prop(session->rtp, AST_RTP_PROPERTY_RTCP, 1);
+	ast_rtp_instance_set_prop(session->rtp, AST_RTP_PROPERTY_DTMF, 1);
+
+	memcpy(&session->prefs, &endpoint->prefs, sizeof(session->prefs));
+
+	ao2_unlock(endpoint);
+
+	return session;
+}
+
+/*! \brief Function called to create a new Jingle Asterisk channel */
+static struct ast_channel *jingle_new(struct jingle_endpoint *endpoint, struct jingle_session *session, int state, const char *title, const char *linkedid, const char *cid_name)
+{
+	struct ast_channel *chan;
+	const char *str = S_OR(title, session->remote);
+	struct ast_format tmpfmt;
+
+	if (ast_format_cap_is_empty(session->cap)) {
+		return NULL;
+	}
+
+	if (!(chan = ast_channel_alloc(1, state, S_OR(title, ""), S_OR(cid_name, ""), "", "", "", linkedid, 0, "Jingle/%s-%04lx", str, ast_random() & 0xffff))) {
+		return NULL;
+	}
+
+	ast_channel_tech_set(chan, &jingle_tech);
+	ast_channel_tech_pvt_set(chan, session);
+	session->owner = chan;
+
+        ast_codec_choose(&session->prefs, session->cap, 1, &tmpfmt);
+        ast_format_cap_add(ast_channel_nativeformats(chan), &tmpfmt);
+
+	if (session->rtp) {
+		struct ast_rtp_engine_ice *ice;
+
+		ast_channel_set_fd(chan, 0, ast_rtp_instance_fd(session->rtp, 0));
+		ast_channel_set_fd(chan, 1, ast_rtp_instance_fd(session->rtp, 1));
+		ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(session->rtp), session->rtp, &session->prefs);
+
+		if (((session->transport == JINGLE_TRANSPORT_GOOGLE_V2) ||
+		     (session->transport == JINGLE_TRANSPORT_GOOGLE_V1)) &&
+		    (ice = ast_rtp_instance_get_ice(session->rtp))) {
+			/* We stop built in ICE support because we need to fall back to old old old STUN support */
+			ice->stop(session->rtp);
+		}
+	}
+
+	if (session->vrtp) {
+		struct ast_rtp_engine_ice *ice;
+	
+		ast_channel_set_fd(chan, 0, ast_rtp_instance_fd(session->vrtp, 0));
+		ast_channel_set_fd(chan, 1, ast_rtp_instance_fd(session->vrtp, 1));
+		ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(session->vrtp), session->vrtp, &session->prefs);
+
+		if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2 && (ice = ast_rtp_instance_get_ice(session->vrtp))) {
+			ice->stop(session->vrtp);
+		}
+	}
+
+	if (state == AST_STATE_RING)
+		ast_channel_rings_set(chan, 1);
+
+	ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
+
+	ast_best_codec(ast_channel_nativeformats(chan), &tmpfmt);
+	ast_format_copy(ast_channel_writeformat(chan), &tmpfmt);
+	ast_format_copy(ast_channel_rawwriteformat(chan), &tmpfmt);
+	ast_format_copy(ast_channel_readformat(chan), &tmpfmt);
+	ast_format_copy(ast_channel_rawreadformat(chan), &tmpfmt);
+
+	ao2_lock(endpoint);
+
+	ast_channel_callgroup_set(chan, endpoint->callgroup);
+	ast_channel_pickupgroup_set(chan, endpoint->pickupgroup);
+
+	if (!ast_strlen_zero(endpoint->accountcode)) {
+		ast_channel_accountcode_set(chan, endpoint->accountcode);
+	}
+
+	if (!ast_strlen_zero(endpoint->language)) {
+		ast_channel_language_set(chan, endpoint->language);
+	}
+
+	if (!ast_strlen_zero(endpoint->musicclass)) {
+		ast_channel_musicclass_set(chan, endpoint->musicclass);
+	}
+
+	ast_channel_context_set(chan, endpoint->context);
+	ast_channel_exten_set(chan, "s");
+	ast_channel_priority_set(chan, 1);
+
+	ao2_unlock(endpoint);
+
+	return chan;
+}
+
+/*! \brief Internal helper function which sends a response */
+static void jingle_send_response(struct aji_client *connection, ikspak *pak)
+{
+	iks *response;
+
+	if (!(response = iks_new("iq"))) {
+		return;
+	}
+
+	iks_insert_attrib(response, "type", "result");
+	iks_insert_attrib(response, "from", connection->jid->full);
+	iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from"));
+	iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id"));
+
+	ast_aji_send(connection, response);
+
+	iks_delete(response);
+}
+
+/*! \brief Internal helper function which sends an error response */
+static void jingle_send_error_response(struct aji_client *connection, ikspak *pak, const char *type, const char *reasonstr, const char *reasonstr2)
+{
+	iks *response, *error = NULL, *reason = NULL, *reason2 = NULL;
+
+	if (!(response = iks_new("iq")) ||
+	    !(error = iks_new("error")) ||
+	    !(reason = iks_new(reasonstr))) {
+		goto end;
+	}
+
+	iks_insert_attrib(response, "type", "error");
+	iks_insert_attrib(response, "from", connection->jid->full);
+	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(error, "type", type);
+	iks_insert_node(error, reason);
+
+	if (!ast_strlen_zero(reasonstr2) && (reason2 = iks_new(reasonstr2))) {
+		iks_insert_node(error, reason2);
+	}
+
+	iks_insert_node(response, error);
+
+	ast_aji_send(connection, response);
+end:
+	iks_delete(reason2);
+	iks_delete(reason);
+	iks_delete(error);
+	iks_delete(response);
+}
+
+/*! \brief Internal helper function which adds ICE-UDP candidates to a transport node */
+static int jingle_add_ice_udp_candidates_to_transport(struct ast_rtp_instance *rtp, iks *transport, iks **candidates)
+{
+	struct ast_rtp_engine_ice *ice;
+	struct ao2_container *local_candidates;
+	struct ao2_iterator it;
+	struct ast_rtp_engine_ice_candidate *candidate;
+	int i = 0, res = 0;
+
+	if (!(ice = ast_rtp_instance_get_ice(rtp)) || !(local_candidates = ice->get_local_candidates(rtp))) {
+		return -1;
+	}
+
+	iks_insert_attrib(transport, "xmlns", JINGLE_ICE_UDP_NS);
+	iks_insert_attrib(transport, "pwd", ice->get_password(rtp));
+	iks_insert_attrib(transport, "ufrag", ice->get_ufrag(rtp));
+
+	it = ao2_iterator_init(local_candidates, 0);
+
+	while ((candidate = ao2_iterator_next(&it)) && (i < MAX_ICE_CANDIDATES)) {
+		iks *local_candidate;
+		char tmp[30];
+
+		if (!(local_candidate = iks_new("candidate"))) {
+			res = -1;
+			goto end;
+		}
+
+		snprintf(tmp, sizeof(tmp), "%d", candidate->id);
+		iks_insert_attrib(local_candidate, "component", tmp);
+		snprintf(tmp, sizeof(tmp), "%d", ast_str_hash(candidate->foundation));
+		iks_insert_attrib(local_candidate, "foundation", tmp);
+		iks_insert_attrib(local_candidate, "generation", "0");
+		snprintf(tmp, sizeof(tmp), "%04lx", ast_random() & 0xffff);
+		iks_insert_attrib(local_candidate, "id", tmp);
+		iks_insert_attrib(local_candidate, "ip", ast_sockaddr_stringify_host(&candidate->address));
+		iks_insert_attrib(local_candidate, "port", ast_sockaddr_stringify_port(&candidate->address));
+		snprintf(tmp, sizeof(tmp), "%d", candidate->priority);
+		iks_insert_attrib(local_candidate, "priority", tmp);
+		iks_insert_attrib(local_candidate, "protocol", "udp");
+
+		if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
+			iks_insert_attrib(local_candidate, "type", "host");
+		} else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
+			iks_insert_attrib(local_candidate, "type", "srflx");
+		} else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_RELAYED) {
+			iks_insert_attrib(local_candidate, "type", "relay");
+		}
+
+		iks_insert_node(transport, local_candidate);
+		candidates[i++] = local_candidate;
+	}
+
+end:
+	ao2_iterator_destroy(&it);
+	ao2_ref(local_candidates, -1);
+
+	return res;
+}
+
+/*! \brief Internal helper function which adds Google candidates to a transport node */
+static int jingle_add_google_candidates_to_transport(struct ast_rtp_instance *rtp, iks *transport, iks **candidates, unsigned int video, enum jingle_transport transport_type)
+{
+	struct ast_rtp_engine_ice *ice;
+	struct ao2_container *local_candidates;
+	struct ao2_iterator it;
+	struct ast_rtp_engine_ice_candidate *candidate;
+	int i = 0, res = 0;
+
+	if (!(ice = ast_rtp_instance_get_ice(rtp)) || !(local_candidates = ice->get_local_candidates(rtp))) {
+		return -1;
+	}
+
+	if (transport_type != JINGLE_TRANSPORT_GOOGLE_V1) {
+		iks_insert_attrib(transport, "xmlns", GOOGLE_TRANSPORT_NS);
+	}
+
+	it = ao2_iterator_init(local_candidates, 0);
+
+	while ((candidate = ao2_iterator_next(&it)) && (i < MAX_ICE_CANDIDATES)) {
+		iks *local_candidate;
+		/* In Google land a username is 16 bytes, explicitly */
+		char ufrag[17] = "";
+
+		if (!(local_candidate = iks_new("candidate"))) {
+			res = -1;
+			goto end;
+		}
+
+		/* We only support RTP candidates */
+		if (candidate->id != 1) {
+			continue;
+		}
+
+		iks_insert_attrib(local_candidate, "name", !video ? "rtp" : "video_rtp");
+		iks_insert_attrib(local_candidate, "address", ast_sockaddr_stringify_host(&candidate->address));
+		iks_insert_attrib(local_candidate, "port", ast_sockaddr_stringify_port(&candidate->address));
+
+		if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
+			iks_insert_attrib(local_candidate, "preference", "0.95");
+			iks_insert_attrib(local_candidate, "type", "local");
+		} else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
+			iks_insert_attrib(local_candidate, "preference", "0.9");
+			iks_insert_attrib(local_candidate, "type", "stun");
+		}
+
+		iks_insert_attrib(local_candidate, "protocol", "udp");
+		iks_insert_attrib(local_candidate, "network", "0");
+		snprintf(ufrag, sizeof(ufrag), "%s", ice->get_ufrag(rtp));
+		iks_insert_attrib(local_candidate, "username", ufrag);
+		iks_insert_attrib(local_candidate, "generation", "0");
+
+		if (transport_type == JINGLE_TRANSPORT_GOOGLE_V1) {
+			iks_insert_attrib(local_candidate, "password", "");
+			iks_insert_attrib(local_candidate, "foundation", "0");
+			iks_insert_attrib(local_candidate, "component", "1");
+		} else {
+			iks_insert_attrib(local_candidate, "password", ice->get_password(rtp));
+		}
+
+		/* You may notice a lack of relay support up above - this is because we don't support it for use with
+		 * the Google talk transport due to their arcane support. */
+
+		iks_insert_node(transport, local_candidate);
+		candidates[i++] = local_candidate;
+	}
+
+end:
+	ao2_iterator_destroy(&it);
+	ao2_ref(local_candidates, -1);
+
+	return res;
+}
+
+/*! \brief Internal function which sends a session-terminate message */
+static void jingle_send_session_terminate(struct jingle_session *session, const char *reasontext)
+{
+	iks *iq = NULL, *jingle = NULL, *reason = NULL, *text = NULL;
+
+	if (!(iq = iks_new("iq")) || !(jingle = iks_new(session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "session" : "jingle")) ||
+	    !(reason = iks_new("reason")) || !(text = iks_new(reasontext))) {
+		goto end;
+	}
+
+	iks_insert_attrib(iq, "to", session->remote);
+	iks_insert_attrib(iq, "type", "set");
+	iks_insert_attrib(iq, "id", session->connection->mid);
+	ast_aji_increment_mid(session->connection->mid);
+
+	if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
+		iks_insert_attrib(jingle, "type", "terminate");
+		iks_insert_attrib(jingle, "id", session->sid);
+		iks_insert_attrib(jingle, "xmlns", GOOGLE_SESSION_NS);
+		iks_insert_attrib(jingle, "initiator", session->outgoing ? session->connection->jid->full : session->remote);
+	} else {
+		iks_insert_attrib(jingle, "action", "session-terminate");
+		iks_insert_attrib(jingle, "sid", session->sid);
+		iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
+	}
+
+	iks_insert_node(iq, jingle);
+	iks_insert_node(jingle, reason);
+	iks_insert_node(reason, text);
+
+	ast_aji_send(session->connection, iq);
+
+end:
+	iks_delete(text);
+	iks_delete(reason);
+	iks_delete(jingle);
+	iks_delete(iq);
+}
+
+/*! \brief Internal function which sends a session-info message */
+static void jingle_send_session_info(struct jingle_session *session, const char *info)
+{
+	iks *iq = NULL, *jingle = NULL, *text = NULL;
+
+	/* Google-V1 has no way to send informational messages so don't even bother trying */
+	if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
+		return;
+	}
+
+	if (!(iq = iks_new("iq")) || !(jingle = iks_new("jingle")) || !(text = iks_new(info))) {
+		goto end;
+	}
+
+	iks_insert_attrib(iq, "to", session->remote);
+	iks_insert_attrib(iq, "type", "set");
+	iks_insert_attrib(iq, "id", session->connection->mid);
+	ast_aji_increment_mid(session->connection->mid);
+
+	iks_insert_attrib(jingle, "action", "session-info");
+	iks_insert_attrib(jingle, "sid", session->sid);
+	iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
+	iks_insert_node(iq, jingle);
+	iks_insert_node(jingle, text);
+
+	ast_aji_send(session->connection, iq);
+
+end:
+	iks_delete(text);
+	iks_delete(jingle);
+	iks_delete(iq);
+}
+
+/*! \brief Internal function which sends a transport-info message */
+static void jingle_send_transport_info(struct jingle_session *session, const char *from)
+{
+	iks *iq, *jingle = NULL, *audio = NULL, *audio_transport = NULL, *video = NULL, *video_transport = NULL;
+	iks *audio_candidates[MAX_ICE_CANDIDATES] = { 0, }, *video_candidates[MAX_ICE_CANDIDATES] = { 0, };
+	int i, res = 0;
+
+	if (!(iq = iks_new("iq")) ||
+	    !(jingle = iks_new(session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "session" : "jingle"))) {
+		iks_delete(iq);
+		ast_queue_hangup_with_cause(session->owner, AST_CAUSE_SWITCH_CONGESTION);
+		return;
+	}
+
+	iks_insert_attrib(iq, "from", session->connection->jid->full);
+	iks_insert_attrib(iq, "to", from);
+	iks_insert_attrib(iq, "type", "set");
+	iks_insert_attrib(iq, "id", session->connection->mid);
+	ast_aji_increment_mid(session->connection->mid);
+
+	if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
+		iks_insert_attrib(jingle, "type", "candidates");
+		iks_insert_attrib(jingle, "id", session->sid);
+		iks_insert_attrib(jingle, "xmlns", GOOGLE_SESSION_NS);
+		iks_insert_attrib(jingle, "initiator", session->outgoing ? session->connection->jid->full : from);
+	} else {
+		iks_insert_attrib(jingle, "action", "transport-info");
+		iks_insert_attrib(jingle, "sid", session->sid);
+		iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
+	}
+	iks_insert_node(iq, jingle);
+
+	if (session->rtp) {
+		if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
+			/* V1 protocol has the candidates directly in the session */
+			res = jingle_add_google_candidates_to_transport(session->rtp, jingle, audio_candidates, 0, session->transport);
+		} else if ((audio = iks_new("content")) && (audio_transport = iks_new("transport"))) {
+			iks_insert_attrib(audio, "creator", session->outgoing ? "initiator" : "responder");
+			iks_insert_attrib(audio, "name", session->audio_content_name);
+			iks_insert_node(jingle, audio);
+			iks_insert_node(audio, audio_transport);
+
+			if (session->transport == JINGLE_TRANSPORT_ICE_UDP) {
+				res = jingle_add_ice_udp_candidates_to_transport(session->rtp, audio_transport, audio_candidates);
+			} else if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2) {
+				res = jingle_add_google_candidates_to_transport(session->rtp, audio_transport, audio_candidates, 0, session->transport);
+			}
+		} else {
+			res = -1;
+		}
+	}
+
+	if ((session->transport != JINGLE_TRANSPORT_GOOGLE_V1) && !res && session->vrtp) {
+		if ((video = iks_new("content")) && (video_transport = iks_new("transport"))) {
+			iks_insert_attrib(video, "creator", session->outgoing ? "initiator" : "responder");
+			iks_insert_attrib(video, "name", session->video_content_name);
+			iks_insert_node(jingle, video);
+			iks_insert_node(video, video_transport);
+
+			if (session->transport == JINGLE_TRANSPORT_ICE_UDP) {
+				res = jingle_add_ice_udp_candidates_to_transport(session->vrtp, video_transport, video_candidates);
+			} else if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2) {
+				res = jingle_add_google_candidates_to_transport(session->vrtp, video_transport, video_candidates, 1, session->transport);
+			}
+		} else {
+			res = -1;
+		}
+	}
+
+	if (!res) {
+		ast_aji_send(session->connection, iq);
+	} else {
+		ast_queue_hangup_with_cause(session->owner, AST_CAUSE_SWITCH_CONGESTION);
+	}
+
+	/* Clean up after ourselves */
+	for (i = 0; i < MAX_ICE_CANDIDATES; i++) {
+		iks_delete(video_candidates[i]);
+		iks_delete(audio_candidates[i]);
+	}
+
+	iks_delete(video_transport);
+	iks_delete(video);
+	iks_delete(audio_transport);
+	iks_delete(audio);
+	iks_delete(jingle);
+	iks_delete(iq);
+}
+
+/*! \brief Internal helper function which adds payloads to a description */
+static int jingle_add_payloads_to_description(struct jingle_session *session, struct ast_rtp_instance *rtp, iks *description, iks **payloads, enum ast_format_type type)
+{
+	struct ast_format format;
+	int i = 0, res = 0;
+
+	ast_format_cap_iter_start(session->jointcap);
+	while (!(ast_format_cap_iter_next(session->jointcap, &format)) && (i < MAX_PAYLOADS)) {
+		int rtp_code;
+		iks *payload;
+		char tmp[32];
+
+		if ((AST_FORMAT_GET_TYPE(format.id) != type) ||
+		    ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(rtp), 1, &format, 0)) == -1) ||
+		    (!(payload = iks_new("payload-type")))) {
+			res = -1;
+			goto end;
+		}
+
+		if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
+			iks_insert_attrib(payload, "xmlns", GOOGLE_PHONE_NS);
+		}
+
+		snprintf(tmp, sizeof(tmp), "%d", rtp_code);
+		iks_insert_attrib(payload, "id", tmp);
+		iks_insert_attrib(payload, "name", ast_rtp_lookup_mime_subtype2(1, &format, 0, 0));
+		iks_insert_attrib(payload, "channels", "1");
+		snprintf(tmp, sizeof(tmp), "%d", ast_rtp_lookup_sample_rate2(1, &format, 0));
+		iks_insert_attrib(payload, "clockrate", tmp);
+
+		iks_insert_node(description, payload);
+		payloads[i++] = payload;
+	}
+	/* If this is for audio and there is room for RFC2833 add it in */
+	if ((type == AST_FORMAT_TYPE_AUDIO) && (i < MAX_PAYLOADS)) {
+		iks *payload;
+
+		if ((payload = iks_new("payload-type"))) {
+			if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
+				iks_insert_attrib(payload, "xmlns", GOOGLE_PHONE_NS);
+			}
+
+			iks_insert_attrib(payload, "id", "101");
+			iks_insert_attrib(payload, "name", "telephone-event");
+			iks_insert_attrib(payload, "channels", "1");
+			iks_insert_attrib(payload, "clockrate", "8000");
+			iks_insert_node(description, payload);
+			payloads[i++] = payload;
+		}
+	}
+
+end:
+	ast_format_cap_iter_end(session->jointcap);
+
+	return res;
+}
+
+/*! \brief Internal function which sends a complete session message */
+static void jingle_send_session_action(struct jingle_session *session, const char *action)
+{
+	iks *iq, *jingle, *audio = NULL, *audio_description = NULL, *video = NULL, *video_description = NULL;
+	iks *audio_payloads[MAX_PAYLOADS] = { 0, }, *video_payloads[MAX_PAYLOADS] = { 0, };
+	iks *audio_transport = NULL, *video_transport = NULL;
+	int i, res = 0;
+
+	if (!(iq = iks_new("iq")) ||
+	    !(jingle = iks_new(session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "session" : "jingle"))) {
+		ast_queue_hangup_with_cause(session->owner, AST_CAUSE_SWITCH_CONGESTION);
+		iks_delete(iq);
+		return;
+	}
+
+	iks_insert_attrib(iq, "from", session->connection->jid->full);
+	iks_insert_attrib(iq, "to", session->remote);
+	iks_insert_attrib(iq, "type", "set");
+	iks_insert_attrib(iq, "id", session->connection->mid);
+	ast_aji_increment_mid(session->connection->mid);
+
+	if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
+		iks_insert_attrib(jingle, "type", action);
+		iks_insert_attrib(jingle, "id", session->sid);
+		iks_insert_attrib(jingle, "xmlns", GOOGLE_SESSION_NS);
+	} else {
+		iks_insert_attrib(jingle, "action", action);
+		iks_insert_attrib(jingle, "sid", session->sid);
+		iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
+	}
+
+	if (!strcasecmp(action, "session-initiate") || !strcasecmp(action, "initiate") || !strcasecmp(action, "accept")) {
+		iks_insert_attrib(jingle, "initiator", session->outgoing ? session->connection->jid->full : session->remote);
+	}
+
+	iks_insert_node(iq, jingle);
+
+	if (session->rtp && (audio = iks_new("content")) && (audio_description = iks_new("description")) &&
+	    (audio_transport = iks_new("transport"))) {
+		if (session->transport != JINGLE_TRANSPORT_GOOGLE_V1) {
+			iks_insert_attrib(audio, "creator", session->outgoing ? "initiator" : "responder");
+			iks_insert_attrib(audio, "name", session->audio_content_name);
+			iks_insert_node(jingle, audio);
+
+			iks_insert_attrib(audio_description, "xmlns", JINGLE_RTP_NS);
+			iks_insert_attrib(audio_description, "media", "audio");
+			iks_insert_node(audio, audio_description);
+		} else {
+			iks_insert_attrib(audio_description, "xmlns", GOOGLE_PHONE_NS);
+			iks_insert_node(jingle, audio_description);
+		}
+
+		if (!(res = jingle_add_payloads_to_description(session, session->rtp, audio_description, audio_payloads, AST_FORMAT_TYPE_AUDIO))) {
+			if (session->transport == JINGLE_TRANSPORT_ICE_UDP) {
+				iks_insert_attrib(audio_transport, "xmlns", JINGLE_ICE_UDP_NS);
+				iks_insert_node(audio, audio_transport);
+			} else if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2) {
+				iks_insert_attrib(audio_transport, "xmlns", GOOGLE_TRANSPORT_NS);
+				iks_insert_node(audio, audio_transport);
+			}
+		}
+	} else {
+		res = -1;
+	}
+
+	if ((session->transport != JINGLE_TRANSPORT_GOOGLE_V1) && !res && session->vrtp) {
+		if ((video = iks_new("content")) && (video_description = iks_new("description"))) {
+			iks_insert_attrib(video, "creator", session->outgoing ? "initiator" : "responder");
+			iks_insert_attrib(video, "name", session->video_content_name);
+			iks_insert_node(jingle, video);
+
+			iks_insert_attrib(video_description, "xmlns", JINGLE_RTP_NS);
+			iks_insert_attrib(video_description, "media", "video");
+			iks_insert_node(video, video_description);
+
+			if (!(res = jingle_add_payloads_to_description(session, session->vrtp, video_description, video_payloads, AST_FORMAT_TYPE_VIDEO))) {
+				if (session->transport == JINGLE_TRANSPORT_ICE_UDP) {
+					iks_insert_attrib(video_transport, "xmlns", JINGLE_ICE_UDP_NS);
+				} else if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2) {
+					iks_insert_attrib(video_transport, "xmlns", GOOGLE_TRANSPORT_NS);
+				}

[... 1199 lines stripped ...]



More information about the asterisk-commits mailing list