[svn-commits] file: branch file/gulp_fax r394183 - /team/file/gulp_fax/res/res_sip_t38.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Jul 12 08:50:39 CDT 2013


Author: file
Date: Fri Jul 12 08:50:38 2013
New Revision: 394183

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=394183
Log:
I went crazy so here is a res_sip_t38 module! Not yet working, but bit by bit.

It currently uses a frame hook to react to T.38 control frames, and to send/receive UDPTL.

It will also create an image udptl stream in SDP (although audio still remains, so hilarity ensues).

Added:
    team/file/gulp_fax/res/res_sip_t38.c   (with props)

Added: team/file/gulp_fax/res/res_sip_t38.c
URL: http://svnview.digium.com/svn/asterisk/team/file/gulp_fax/res/res_sip_t38.c?view=auto&rev=394183
==============================================================================
--- team/file/gulp_fax/res/res_sip_t38.c (added)
+++ team/file/gulp_fax/res/res_sip_t38.c Fri Jul 12 08:50:38 2013
@@ -1,0 +1,571 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, 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 SIP T.38 handling
+ */
+
+/*** MODULEINFO
+	<depend>pjproject</depend>
+	<depend>res_sip</depend>
+	<depend>res_sip_session</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include <pjsip.h>
+#include <pjsip_ua.h>
+#include <pjmedia.h>
+#include <pjlib.h>
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/module.h"
+#include "asterisk/udptl.h"
+#include "asterisk/netsock2.h"
+#include "asterisk/channel.h"
+#include "asterisk/acl.h"
+
+#include "asterisk/res_sip.h"
+#include "asterisk/res_sip_session.h"
+
+/* XXX The Gulp stuff below is temporary until a method for accessing the session given a channel appears, give it time! */
+
+/*!
+ * \brief Positions of various media
+ */
+enum sip_session_media_position {
+	/*! \brief First is audio */
+	SIP_MEDIA_AUDIO = 0,
+	/*! \brief Second is video */
+	SIP_MEDIA_VIDEO,
+	/*! \brief Last is the size for media details */
+	SIP_MEDIA_SIZE,
+};
+
+struct gulp_pvt {
+	struct ast_sip_session *session;
+	struct ast_sip_session_media *media[SIP_MEDIA_SIZE];
+};
+
+/*! \brief Address for IPv4 UDPTL */
+static struct ast_sockaddr address_ipv4;
+
+/*! \brief T.38 states for a session */                                                                                                                                                                                                          
+enum t38state {                                                                                                                                                                                                                              
+	T38_DISABLED = 0,     /*!< Not enabled */                                                                                                                                                                                            
+	T38_LOCAL_REINVITE,   /*!< Offered from local - REINVITE */                                                                                                                                                                          
+	T38_PEER_REINVITE,    /*!< Offered from peer - REINVITE */                                                                                                                                                                           
+	T38_ENABLED,          /*!< Negotiated (enabled) */                                                                                                                                                                                   
+	T38_REJECTED          /*!< Refused */                                                                                                                                                                                                
+};
+
+/*! \brief T.38 state information */
+struct t38_state {
+	/*! \brief Current state */
+	enum t38state state;
+	/*! \brief Our T.38 parameters */
+	struct ast_control_t38_parameters our_parms;
+	/*! \brief Their T.38 parameters */
+	struct ast_control_t38_parameters their_parms;
+};
+
+/*! \brief Destructor for T.38 state information */
+static void t38_state_destroy(void *obj)
+{
+	ast_free(obj);
+}
+
+/*! \brief Datastore for attaching T.38 state information */
+static const struct ast_datastore_info t38_datastore = {
+	.type = "t38",
+	.destroy = t38_state_destroy,
+};
+
+/*! \brief Structure for T.38 parameters task data */
+struct t38_parameters_task_data {
+	/*! \brief Session itself */
+	struct ast_sip_session *session;
+	/*! \brief T.38 control frame */
+	struct ast_frame *frame;
+};
+
+/*! \brief Destructor for T.38 data */
+static void t38_parameters_task_data_destroy(void *obj)
+{
+	struct t38_parameters_task_data *data = obj;
+
+	ao2_cleanup(data->session);
+
+	if (data->frame) {
+		ast_frfree(data->frame);
+	}
+}
+
+/*! \brief Allocator for T.38 data */
+static struct t38_parameters_task_data *t38_parameters_task_data_alloc(struct ast_sip_session *session,
+	struct ast_frame *frame)
+{
+	struct t38_parameters_task_data *data = ao2_alloc(sizeof(*data), t38_parameters_task_data_destroy);
+
+	if (!data) {
+		return NULL;
+	}
+
+	data->session = session;
+	ao2_ref(session, +1);
+	data->frame = frame;
+
+	return data;
+}
+
+/*! \brief Helper function which retrieves or allocates a T.38 state information datastore */
+static struct t38_state *t38_state_get_or_alloc(struct ast_sip_session *session)
+{
+	RAII_VAR(struct ast_datastore *, datastore, ast_sip_session_get_datastore(session, "t38"), ao2_cleanup);
+
+	/* While the datastore refcount is decremented this is operating in the serializer so it will remain valid regardless */
+	if (datastore) {
+		return datastore->data;
+	}
+
+	if (!(datastore = ast_sip_session_alloc_datastore(&t38_datastore, "t38")) ||
+		!(datastore->data = ast_calloc(1, sizeof(struct t38_state))) ||
+		ast_sip_session_add_datastore(session, datastore)) {
+		return NULL;
+	}
+
+	return datastore->data;
+}
+
+/*! \brief Initializes UDPTL support on a session, only done when actually needed */
+static int t38_initialize_session(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
+{
+	if (session_media->udptl) {
+		return 0;
+	}
+
+	if (!(session_media->udptl = ast_udptl_new_with_bindaddr(NULL, NULL, 0, &address_ipv4))) {
+		return -1;
+	}
+
+	ast_channel_set_fd(session->channel, 5, ast_udptl_fd(session_media->udptl));
+	ast_udptl_set_error_correction_scheme(session_media->udptl, session->endpoint->t38udptl_ec);
+
+	/* XXX Need to set NAT */
+	return 0;
+}
+
+/*! \brief Task for reacting to T.38 control frame */
+static int t38_interpret_parameters(void *obj)
+{
+	RAII_VAR(struct t38_parameters_task_data *, data, obj, ao2_cleanup);
+	const struct ast_control_t38_parameters *parameters = data->frame->data.ptr;
+	struct t38_state *state = t38_state_get_or_alloc(data->session);
+	RAII_VAR(struct ast_sip_session_media *, session_media, ao2_find(data->session->media, "image", OBJ_KEY), ao2_cleanup);
+
+	/* Without state we can't interpret parameters */
+	if (!state) {
+		return 0;
+	}
+
+	switch (parameters->request_response) {
+	case AST_T38_NEGOTIATED:
+	case AST_T38_REQUEST_NEGOTIATE:         /* Request T38 */
+		/* Negotiation can not take place without a valid max_ifp value. */
+		if (!parameters->max_ifp) {
+			if (state->state == T38_PEER_REINVITE) {
+				/* XXX Need to reject their reinvite */
+			}
+			/* XXX Need to change state to rejected */
+			break;
+		} else if (state->state == T38_PEER_REINVITE) {
+			state->our_parms = *parameters;
+			/* modify our parameters to conform to the peer's parameters,
+			 * based on the rules in the ITU T.38 recommendation
+			 */
+			if (!state->their_parms.fill_bit_removal) {
+				state->our_parms.fill_bit_removal = 0;
+			}
+			if (!state->their_parms.transcoding_mmr) {
+				state->our_parms.transcoding_mmr = 0;
+			}
+			if (!state->their_parms.transcoding_jbig) {
+				state->our_parms.transcoding_jbig = 0;
+			}
+			state->our_parms.version = MIN(state->our_parms.version, state->their_parms.version);
+			state->our_parms.rate_management = state->their_parms.rate_management;
+			ast_udptl_set_local_max_ifp(session_media->udptl, state->our_parms.max_ifp);
+			/* XXX Need to change state to enabled */
+			/* XXX Need to accept their reinvite */
+		} else if (state->state != T38_ENABLED) {
+			if (t38_initialize_session(data->session, session_media)) {
+				break;
+			}
+			state->our_parms = *parameters;
+			ast_udptl_set_local_max_ifp(session_media->udptl, state->our_parms.max_ifp);
+			/* XXX Need to suppress other streams temporarily */
+			/* XXX Need to change state to local reinvite */
+			/* XXX Need to send reinvite */
+			ast_sip_session_refresh(data->session, NULL, NULL, AST_SIP_SESSION_REFRESH_METHOD_INVITE, 1);
+		}
+		break;
+	case AST_T38_TERMINATED:
+	case AST_T38_REFUSED:
+	case AST_T38_REQUEST_TERMINATE:         /* Shutdown T38 */
+		if (state->state == T38_PEER_REINVITE) {
+			/* XXX Need to change state to rejected */
+			/* XXX Need to reject their reinvite */
+		} else if (state->state == T38_ENABLED) {
+			/* XXX Need to send reinvite */
+		}
+		break;
+	case AST_T38_REQUEST_PARMS: {		/* Application wants remote's parameters re-sent */
+		struct ast_control_t38_parameters parameters = state->their_parms;
+
+		if (state->state == T38_PEER_REINVITE) {
+			parameters.max_ifp = ast_udptl_get_far_max_ifp(session_media->udptl);
+			parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
+			ast_queue_control_data(data->session->channel, AST_CONTROL_T38_PARAMETERS, &parameters, sizeof(parameters));
+		}
+		break;
+	}
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+/*! \brief Frame hook callback for writing */
+static struct ast_frame *t38_framehook_write(struct ast_sip_session *session, struct ast_frame *f)
+{
+	if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_T38_PARAMETERS &&
+		session->endpoint->t38udptl) {
+		struct t38_parameters_task_data *data = t38_parameters_task_data_alloc(session, f);
+
+		if (!data) {
+			return f;
+		}
+
+		/* The frame is now on the task data structure */
+		f = &ast_null_frame;
+
+		if (ast_sip_push_task(session->serializer, t38_interpret_parameters, data)) {
+			ao2_ref(data, -1);
+		}
+	} else if (f->frametype == AST_FRAME_MODEM) {
+		RAII_VAR(struct ast_sip_session_media *, session_media, NULL, ao2_cleanup);
+
+		if ((session_media = ao2_find(session->media, "image", OBJ_KEY)) &&
+			session_media->udptl) {
+			ast_udptl_write(session_media->udptl, f);
+		}
+	}
+
+	return f;
+}
+
+/*! \brief Frame hook callback for reading */
+static struct ast_frame *t38_framehook_read(struct ast_sip_session *session, struct ast_frame *f)
+{
+	if (ast_channel_fdno(session->channel) == 5) {
+		RAII_VAR(struct ast_sip_session_media *, session_media, NULL, ao2_cleanup);
+
+		if ((session_media = ao2_find(session->media, "image", OBJ_KEY)) &&
+			session_media->udptl) {
+			f = ast_udptl_read(session_media->udptl);
+		}
+	}
+
+	return f;
+}
+
+/*! \brief Frame hook callback for T.38 related stuff */
+static struct ast_frame *t38_framehook(struct ast_channel *chan, struct ast_frame *f,
+	enum ast_framehook_event event, void *data)
+{
+	struct gulp_pvt *pvt = ast_channel_tech_pvt(chan);
+
+	if (event == AST_FRAMEHOOK_EVENT_READ) {
+		f = t38_framehook_read(pvt->session, f);
+	} else if (event == AST_FRAMEHOOK_EVENT_WRITE) {
+		f = t38_framehook_write(pvt->session, f);
+	}
+
+	return f;
+}
+
+/*! \brief Function called to attach T.38 framehook to channel when appropriate */
+static void t38_attach_framehook(struct ast_sip_session *session)
+{
+	static struct ast_framehook_interface hook = {
+		.version = AST_FRAMEHOOK_INTERFACE_VERSION,
+		.event_cb = t38_framehook,
+	};
+
+	if ((ast_channel_state(session->channel) == AST_STATE_UP) || !session->endpoint->t38udptl) {
+		return;
+	}
+
+	if (ast_framehook_attach(session->channel, &hook) < 0) {
+		ast_log(LOG_WARNING, "Could not attach T.38 Frame hook to channel, T.38 will be unavailable on '%s'\n",
+			ast_channel_name(session->channel));
+	}
+}
+
+/*! \brief Function called when an INVITE goes out */
+static int t38_incoming_invite_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
+{
+	t38_attach_framehook(session);
+	return 0;
+}
+
+/*! \brief Function called when an INVITE comes in */
+static void t38_outgoing_invite_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
+{
+	t38_attach_framehook(session);
+}
+
+/*! \brief Get Max T.38 Transmission rate from T38 capabilities */
+static unsigned int t38_get_rate(enum ast_control_t38_rate rate)
+{
+	switch (rate) {
+	case AST_T38_RATE_2400:
+		return 2400;
+	case AST_T38_RATE_4800:
+		return 4800;
+	case AST_T38_RATE_7200:
+		return 7200;
+	case AST_T38_RATE_9600:
+		return 9600;
+	case AST_T38_RATE_12000:
+		return 12000;
+	case AST_T38_RATE_14400:
+		return 14400;
+	default:
+		return 0;
+	}
+}
+
+/*! \brief Supplement for adding framehook to session channel */
+static struct ast_sip_session_supplement t38_supplement = {
+	.method = "INVITE",
+	.priority = AST_SIP_SESSION_SUPPLEMENT_PRIORITY_CHANNEL + 1,
+	.incoming_request = t38_incoming_invite_request,
+	.outgoing_request = t38_outgoing_invite_request,
+};
+
+/*! \brief Function which negotiates an incoming media stream */
+static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
+					 const struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_media *stream)
+{
+	return 0;
+}
+
+/*! \brief Function which creates an outgoing stream */
+static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
+				      struct pjmedia_sdp_session *sdp)
+{
+	pj_pool_t *pool = session->inv_session->pool_prov;
+	static const pj_str_t STR_IN = { "IN", 2 };
+	static const pj_str_t STR_IP4 = { "IP4", 3};
+	static const pj_str_t STR_UDPTL = { "udptl", 5 };
+	static const pj_str_t STR_T38 = { "t38", 3 };
+	static const pj_str_t STR_TRANSFERREDTCF = { "transferredTCF", 14 };
+	static const pj_str_t STR_LOCALTCF = { "localTCF", 8 };
+	static const pj_str_t STR_T38UDPFEC = { "t38UDPFEC", 9 };
+	static const pj_str_t STR_T38UDPREDUNDANCY = { "t38UDPRedundancy", 16 };
+	pjmedia_sdp_media *media;
+	char hostip[PJ_INET6_ADDRSTRLEN+2];
+	struct ast_sockaddr addr;
+	struct t38_state *state;
+	char tmp[512];
+	pj_str_t stmp;
+
+	if (!session_media->udptl) {
+		return 0;
+	}
+
+	if (!(media = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_media))) ||
+		!(media->conn = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_conn)))) {
+		return -1;
+	}
+
+	media->desc.media = pj_str(session_media->stream_type);
+	media->desc.transport = STR_UDPTL;
+
+	if (ast_strlen_zero(session->endpoint->external_media_address)) {
+		pj_sockaddr localaddr;
+
+		if (pj_gethostip(pj_AF_INET(), &localaddr)) {
+			return -1;
+		}
+		pj_sockaddr_print(&localaddr, hostip, sizeof(hostip), 2);
+	} else {
+		ast_copy_string(hostip, session->endpoint->external_media_address, sizeof(hostip));
+	}
+
+	media->conn->net_type = STR_IN;
+	media->conn->addr_type = STR_IP4;
+	pj_strdup2(pool, &media->conn->addr, hostip);
+	ast_udptl_get_us(session_media->udptl, &addr);
+	media->desc.port = (pj_uint16_t) ast_sockaddr_port(&addr);
+	media->desc.port_count = 1;
+	media->desc.fmt[media->desc.fmt_count++] = STR_T38;
+
+	state = t38_state_get_or_alloc(session);
+
+	snprintf(tmp, sizeof(tmp), "%d", state->our_parms.version);
+	media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxVersion", pj_cstr(&stmp, tmp));
+
+	snprintf(tmp, sizeof(tmp), "%d", t38_get_rate(state->our_parms.rate));
+	media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38MaxBitRate", pj_cstr(&stmp, tmp));
+
+	if (state->our_parms.fill_bit_removal) {
+		media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxFillBitRemoval", NULL);
+	}
+
+	if (state->our_parms.transcoding_mmr) {
+		media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxTranscodingMMR", NULL);
+	}
+
+	if (state->our_parms.transcoding_jbig) {
+		media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxTranscodingJBIG", NULL);
+	}
+
+	switch (state->our_parms.rate_management) {
+	case AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF:
+		media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxRateManagement", &STR_TRANSFERREDTCF);
+		break;
+	case AST_T38_RATE_MANAGEMENT_LOCAL_TCF:
+		media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxRateManagement", &STR_LOCALTCF);
+		break;
+	}
+
+	snprintf(tmp, sizeof(tmp), "%d", ast_udptl_get_local_max_datagram(session_media->udptl));
+	media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxMaxDatagram", pj_cstr(&stmp, tmp));
+
+	switch (ast_udptl_get_error_correction_scheme(session_media->udptl)) {
+	case UDPTL_ERROR_CORRECTION_NONE:
+		break;
+	case UDPTL_ERROR_CORRECTION_FEC:
+		media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxUdpEC", &STR_T38UDPFEC);
+		break;
+	case UDPTL_ERROR_CORRECTION_REDUNDANCY:
+		media->attr[media->attr_count++] = pjmedia_sdp_attr_create(pool, "T38FaxUdpEC", &STR_T38UDPREDUNDANCY);
+		break;
+	}
+
+	sdp->media[sdp->media_count++] = media;
+
+	return 1;
+}
+
+/*! \brief Function which applies a negotiated stream */
+static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
+				       const struct pjmedia_sdp_session *local, const struct pjmedia_sdp_media *local_stream,
+				       const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
+{
+	return 0;
+}
+
+/*! \brief Function which updates the media stream with external media address, if applicable */
+static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struct pjmedia_sdp_media *stream, struct ast_sip_transport *transport)
+{
+	char host[NI_MAXHOST];
+	struct ast_sockaddr addr = { { 0, } };
+
+	ast_copy_pj_str(host, &stream->conn->addr, sizeof(host));
+	ast_sockaddr_parse(&addr, host, PARSE_PORT_FORBID);
+
+	/* Is the address within the SDP inside the same network? */
+	if (ast_apply_ha(transport->localnet, &addr) == AST_SENSE_ALLOW) {
+		return;
+	}
+
+	pj_strdup2(tdata->pool, &stream->conn->addr, transport->external_media_address);
+}
+
+/*! \brief Function which destroys the UDPTL instance when session ends */
+static void stream_destroy(struct ast_sip_session_media *session_media)
+{
+	if (session_media->udptl) {
+		ast_udptl_destroy(session_media->udptl);
+	}
+}
+
+/*! \brief SDP handler for 'image' media stream */
+static struct ast_sip_session_sdp_handler image_sdp_handler = {
+	.id = "image",
+	.negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
+	.create_outgoing_sdp_stream = create_outgoing_sdp_stream,
+	.apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
+	.change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
+	.stream_destroy = stream_destroy,
+};
+
+/*! \brief Unloads the SIP T.38 module from Asterisk */
+static int unload_module(void)
+{
+	ast_sip_session_unregister_sdp_handler(&image_sdp_handler, "image");
+	ast_sip_session_unregister_supplement(&t38_supplement);
+
+	return 0;
+}
+
+/*!
+ * \brief Load the module
+ *
+ * Module loading including tests for configuration or dependencies.
+ * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
+ * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
+ * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
+ * configuration file or other non-critical problem return
+ * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
+ */
+static int load_module(void)
+{
+	ast_sockaddr_parse(&address_ipv4, "0.0.0.0", 0);
+
+	if (ast_sip_session_register_supplement(&t38_supplement)) {
+		ast_log(LOG_ERROR, "Unable to register T.38 session supplement\n");
+		goto end;
+	}
+
+	if (ast_sip_session_register_sdp_handler(&image_sdp_handler, "image")) {
+		ast_log(LOG_ERROR, "Unable to register SDP handler for image stream type\n");
+		goto end;
+	}
+
+	return AST_MODULE_LOAD_SUCCESS;
+end:
+	unload_module();
+
+	return AST_MODULE_LOAD_FAILURE;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP T.38 UDPTL Support",
+		.load = load_module,
+		.unload = unload_module,
+		.load_pri = AST_MODPRI_CHANNEL_DRIVER,
+	);

Propchange: team/file/gulp_fax/res/res_sip_t38.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/file/gulp_fax/res/res_sip_t38.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/file/gulp_fax/res/res_sip_t38.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the svn-commits mailing list