[svn-commits] file: trunk r395731 - in /trunk: channels/ include/asterisk/ res/ res/res_sip/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Jul 30 09:16:44 CDT 2013


Author: file
Date: Tue Jul 30 09:16:41 2013
New Revision: 395731

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=395731
Log:
Add support for T.38 fax to chan_pjsip.

Review: https://reviewboard.asterisk.org/r/2692/

Added:
    trunk/res/res_sip_t38.c   (with props)
Modified:
    trunk/channels/chan_gulp.c
    trunk/include/asterisk/res_sip.h
    trunk/include/asterisk/res_sip_session.h
    trunk/res/res_sip.c
    trunk/res/res_sip/sip_configuration.c
    trunk/res/res_sip_session.c
    trunk/res/res_sip_session.exports.in

Modified: trunk/channels/chan_gulp.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_gulp.c?view=diff&rev=395731&r1=395730&r2=395731
==============================================================================
--- trunk/channels/chan_gulp.c (original)
+++ trunk/channels/chan_gulp.c Tue Jul 30 09:16:41 2013
@@ -142,6 +142,7 @@
 static int gulp_transfer(struct ast_channel *ast, const char *target);
 static int gulp_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
 static int gulp_devicestate(const char *data);
+static int gulp_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
 
 /*! \brief PBX interface structure for channel registration */
 static struct ast_channel_tech gulp_tech = {
@@ -162,6 +163,7 @@
 	.transfer = gulp_transfer,
 	.fixup = gulp_fixup,
 	.devicestate = gulp_devicestate,
+	.queryoption = gulp_queryoption,
 	.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER
 };
 
@@ -431,7 +433,7 @@
 {
 	RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
 
-	return ast_sip_session_refresh(session, NULL, NULL, session->endpoint->direct_media_method, 1);
+	return ast_sip_session_refresh(session, NULL, NULL, NULL, session->endpoint->direct_media_method, 1);
 }
 
 static struct ast_datastore_info direct_media_mitigation_info = { };
@@ -668,6 +670,55 @@
 	return 0;
 }
 
+/*! \brief Internal helper function called when CNG tone is detected */
+static struct ast_frame *gulp_cng_tone_detected(struct ast_sip_session *session, struct ast_frame *f)
+{
+	const char *target_context;
+	int exists;
+
+	/* If we only needed this DSP for fax detection purposes we can just drop it now */
+	if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) {
+		ast_dsp_set_features(session->dsp, DSP_FEATURE_DIGIT_DETECT);
+	} else {
+		ast_dsp_free(session->dsp);
+		session->dsp = NULL;
+	}
+
+	/* If already executing in the fax extension don't do anything */
+	if (!strcmp(ast_channel_exten(session->channel), "fax")) {
+		return f;
+	}
+
+	target_context = S_OR(ast_channel_macrocontext(session->channel), ast_channel_context(session->channel));
+
+	/* We need to unlock the channel here because ast_exists_extension has the
+	 * potential to start and stop an autoservice on the channel. Such action
+	 * is prone to deadlock if the channel is locked.
+	 */
+	ast_channel_unlock(session->channel);
+	exists = ast_exists_extension(session->channel, target_context, "fax", 1,
+		S_COR(ast_channel_caller(session->channel)->id.number.valid,
+			ast_channel_caller(session->channel)->id.number.str, NULL));
+	ast_channel_lock(session->channel);
+
+	if (exists) {
+		ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n",
+			ast_channel_name(session->channel));
+		pbx_builtin_setvar_helper(session->channel, "FAXEXTEN", ast_channel_exten(session->channel));
+		if (ast_async_goto(session->channel, target_context, "fax", 1)) {
+			ast_log(LOG_ERROR, "Failed to async goto '%s' into fax extension in '%s'\n",
+				ast_channel_name(session->channel), target_context);
+		}
+		ast_frfree(f);
+		f = &ast_null_frame;
+	} else {
+		ast_log(LOG_NOTICE, "FAX CNG detected on '%s' but no fax extension in '%s'\n",
+			ast_channel_name(session->channel), target_context);
+	}
+
+	return f;
+}
+
 /*! \brief Function called by core to read any waiting frames */
 static struct ast_frame *gulp_read(struct ast_channel *ast)
 {
@@ -718,8 +769,13 @@
 		f = ast_dsp_process(ast, channel->session->dsp, f);
 
 		if (f && (f->frametype == AST_FRAME_DTMF)) {
-			ast_debug(3, "* Detected inband DTMF '%c' on '%s'\n", f->subclass.integer,
-				ast_channel_name(ast));
+			if (f->subclass.integer == 'f') {
+				ast_debug(3, "Fax CNG detected on %s\n", ast_channel_name(ast));
+				f = gulp_cng_tone_detected(channel->session, f);
+			} else {
+				ast_debug(3, "* Detected inband DTMF '%c' on '%s'\n", f->subclass.integer,
+					ast_channel_name(ast));
+			}
 		}
 	}
 
@@ -760,6 +816,8 @@
 		if ((media = pvt->media[SIP_MEDIA_VIDEO]) && media->rtp) {
 			res = ast_rtp_instance_write(media->rtp, frame);
 		}
+		break;
+	case AST_FRAME_MODEM:
 		break;
 	default:
 		ast_log(LOG_WARNING, "Can't send %d type frames with Gulp\n", frame->frametype);
@@ -872,6 +930,45 @@
 	}
 
 	return state;
+}
+
+/*! \brief Function called to query options on a channel */
+static int gulp_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
+{
+	struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
+	struct ast_sip_session *session = channel->session;
+	int res = -1;
+	enum ast_sip_session_t38state state = T38_STATE_UNAVAILABLE;
+
+	switch (option) {
+	case AST_OPTION_T38_STATE:
+		if (session->endpoint->t38udptl) {
+			switch (session->t38state) {
+			case T38_LOCAL_REINVITE:
+			case T38_PEER_REINVITE:
+				state = T38_STATE_NEGOTIATING;
+				break;
+			case T38_ENABLED:
+				state = T38_STATE_NEGOTIATED;
+				break;
+			case T38_REJECTED:
+				state = T38_STATE_REJECTED;
+				break;
+			default:
+				state = T38_STATE_UNKNOWN;
+				break;
+			}
+		}
+
+		*((enum ast_t38_state *) data) = state;
+		res = 0;
+
+		break;
+	default:
+		break;
+	}
+
+	return res;
 }
 
 struct indicate_data {
@@ -994,7 +1091,7 @@
 			method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
 		}
 
-		ast_sip_session_refresh(session, NULL, NULL, method, 0);
+		ast_sip_session_refresh(session, NULL, NULL, NULL, method, 0);
 	}
 
 	return 0;
@@ -1097,6 +1194,18 @@
 		} else {
 			res = -1;
 		}
+		break;
+	case AST_CONTROL_T38_PARAMETERS:
+		res = 0;
+
+		if (channel->session->t38state == T38_PEER_REINVITE) {
+			const struct ast_control_t38_parameters *parameters = data;
+
+			if (parameters->request_response == AST_T38_REQUEST_PARMS) {
+				res = AST_T38_REQUEST_PARMS;
+			}
+		}
+
 		break;
 	case -1:
 		res = -1;

Modified: trunk/include/asterisk/res_sip.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/res_sip.h?view=diff&rev=395731&r1=395730&r2=395731
==============================================================================
--- trunk/include/asterisk/res_sip.h (original)
+++ trunk/include/asterisk/res_sip.h Tue Jul 30 09:16:41 2013
@@ -32,6 +32,8 @@
 #include "asterisk/dnsmgr.h"
 /* Needed for ast_endpoint */
 #include "asterisk/endpoints.h"
+/* Needed for ast_t38_ec_modes */
+#include "asterisk/udptl.h"
 /* Needed for pj_sockaddr */
 #include <pjlib.h>
 /* Needed for ast_rtp_dtls_cfg struct */
@@ -434,6 +436,18 @@
 	struct ast_endpoint *persistent;
 	/*! The number of channels at which busy device state is returned */
 	unsigned int devicestate_busy_at;
+	/*! Whether T.38 UDPTL support is enabled or not */
+	unsigned int t38udptl;
+	/*! Error correction setting for T.38 UDPTL */
+	enum ast_t38_ec_modes t38udptl_ec;
+	/*! Explicit T.38 max datagram value, may be 0 to indicate the remote side can be trusted */
+	unsigned int t38udptl_maxdatagram;
+	/*! Whether fax detection is enabled or not (CNG tone detection) */
+	unsigned int faxdetect;
+	/*! Whether NAT Support is enabled for T.38 UDPTL sessions or not */
+	unsigned int t38udptl_nat;
+	/*! Whether to use IPv6 for UDPTL or not */
+	unsigned int t38udptl_ipv6;
 	/*! Determines if transfers (using REFER) are allowed by this endpoint */
 	unsigned int allowtransfer;
 	/*! DSCP TOS bits for audio streams */

Modified: trunk/include/asterisk/res_sip_session.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/res_sip_session.h?view=diff&rev=395731&r1=395730&r2=395731
==============================================================================
--- trunk/include/asterisk/res_sip_session.h (original)
+++ trunk/include/asterisk/res_sip_session.h Tue Jul 30 09:16:41 2013
@@ -43,6 +43,16 @@
 struct pjmedia_sdp_media;
 struct pjmedia_sdp_session;
 struct ast_dsp;
+struct ast_udptl;
+
+/*! \brief T.38 states for a session */
+enum ast_sip_session_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 */
+};
 
 struct ast_sip_session_sdp_handler;
 
@@ -50,8 +60,12 @@
  * \brief A structure containing SIP session media information
  */
 struct ast_sip_session_media {
-	/*! \brief RTP instance itself */
-	struct ast_rtp_instance *rtp;
+	union {
+		/*! \brief RTP instance itself */
+		struct ast_rtp_instance *rtp;
+		/*! \brief UDPTL instance itself */
+		struct ast_udptl *udptl;
+	};
 	/*! \brief Direct media address */
 	struct ast_sockaddr direct_media_addr;
 	/*! \brief SDP handler that setup the RTP */
@@ -113,10 +127,15 @@
 	struct ast_dsp *dsp;
 	/* Whether the termination of the session should be deferred */
 	unsigned int defer_terminate:1;
+	/* Deferred incoming re-invite */
+	pjsip_rx_data *deferred_reinvite;
+	/* Current T.38 state */
+	enum ast_sip_session_t38state t38state;
 };
 
 typedef int (*ast_sip_session_request_creation_cb)(struct ast_sip_session *session, pjsip_tx_data *tdata);
 typedef int (*ast_sip_session_response_cb)(struct ast_sip_session *session, pjsip_rx_data *rdata);
+typedef int (*ast_sip_session_sdp_creation_cb)(struct ast_sip_session *session, pjmedia_sdp_session *sdp);
 
 enum ast_sip_session_supplement_priority {
 	/*! Top priority. Supplements with this priority are those that need to run before any others */
@@ -210,6 +229,19 @@
 	/*! An identifier for this handler */
 	const char *id;
 	/*!
+	 * \brief Determine whether a stream requires that the re-invite be deferred.
+	 * If a stream can not be immediately negotiated the re-invite can be deferred and
+	 * resumed at a later time. It is up to the handler which caused deferral to occur
+	 * to resume it.
+	 * \param session The session for which the media is being re-invited
+	 * \param session_media The media being reinvited
+	 * \param sdp The entire SDP.
+	 * \retval 0 The stream was unhandled or does not need the re-invite to be deferred.
+	 * \retval 1 Re-invite should be deferred and will be resumed later. No further operations will take place.
+	 * \note This is optional, if not implemented the stream is assumed to not be deferred.
+	 */
+	int (*defer_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);
+	/*!
 	 * \brief Set session details based on a stream in an incoming SDP offer or answer
 	 * \param session The session for which the media is being negotiated
 	 * \param session_media The media to be setup for this session
@@ -443,6 +475,7 @@
  * 
  * \param session The session on which the reinvite will be sent
  * \param on_request_creation Callback called when request is created
+ * \param on_sdp_creation Callback called when SDP is created
  * \param on_response Callback called when response for request is received
  * \param method The method that should be used when constructing the session refresh
  * \param generate_new_sdp Boolean to indicate if a new SDP should be created
@@ -451,6 +484,7 @@
  */
 int ast_sip_session_refresh(struct ast_sip_session *session,
 		ast_sip_session_request_creation_cb on_request_creation,
+		ast_sip_session_sdp_creation_cb on_sdp_creation,
 		ast_sip_session_response_cb on_response,
 		enum ast_sip_session_refresh_method method,
 		int generate_new_sdp);
@@ -513,4 +547,15 @@
  */
 struct ast_sip_session *ast_sip_dialog_get_session(pjsip_dialog *dlg);
 
+/*!
+ * \brief Resumes processing of a deferred incoming re-invite
+ *
+ * \param session The session which has a pending incoming re-invite
+ *
+ * \note When resuming a re-invite it is given to the pjsip stack as if it
+ *       had just been received from a transport, this means that the deferral
+ *       callback will be called again.
+ */
+void ast_sip_session_resume_reinvite(struct ast_sip_session *session);
+
 #endif /* _RES_SIP_SESSION_H */

Modified: trunk/res/res_sip.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_sip.c?view=diff&rev=395731&r1=395730&r2=395731
==============================================================================
--- trunk/res/res_sip.c (original)
+++ trunk/res/res_sip.c Tue Jul 30 09:16:41 2013
@@ -398,6 +398,56 @@
 						Gulp channel driver will return busy as the device state instead of in use.
 					</para></description>
 				</configOption>
+				<configOption name="t38udptl" default="no">
+					<synopsis>Whether T.38 UDPTL support is enabled or not</synopsis>
+					<description><para>
+						If set to yes T.38 UDPTL support will be enabled, and T.38 negotiation requests will be accepted
+						and relayed.
+					</para></description>
+				</configOption>
+				<configOption name="t38udptl_ec" default="none">
+					<synopsis>T.38 UDPTL error correction method</synopsis>
+					<description>
+						<enumlist>
+							<enum name="none"><para>
+								No error correction should be used.
+							</para></enum>
+							<enum name="fec"><para>
+								Forward error correction should be used.
+							</para></enum>
+							<enum name="redundancy"><para>
+								Redundacy error correction should be used.
+							</para></enum>
+						</enumlist>
+					</description>
+				</configOption>
+				<configOption name="t38udptl_maxdatagram" default="0">
+					<synopsis>T.38 UDPTL maximum datagram size</synopsis>
+					<description><para>
+						This option can be set to override the maximum datagram of a remote endpoint for broken
+						endpoints.
+					</para></description>
+				</configOption>
+				<configOption name="faxdetect" default="no">
+					<synopsis>Whether CNG tone detection is enabled</synopsis>
+					<description><para>
+						This option can be set to send the session to the fax extension when a CNG tone is
+						detected.
+					</para></description>
+				</configOption>
+				<configOption name="t38udptl_nat" default="no">
+					<synopsis>Whether NAT support is enabled on UDPTL sessions</synopsis>
+					<description><para>
+						When enabled the UDPTL stack will send UDPTL packets to the source address of
+						received packets.
+					</para></description>
+				</configOption>
+				<configOption name="t38udptl_ipv6" default="no">
+					<synopsis>Whether IPv6 is used for UDPTL Sessions</synopsis>
+					<description><para>
+						When enabled the UDPTL stack will use IPv6.
+					</para></description>
+				</configOption>
 				<configOption name="tonezone">
 					<synopsis>Set which country's indications to use for channels created for this endpoint.</synopsis>
 				</configOption>

Modified: trunk/res/res_sip/sip_configuration.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_sip/sip_configuration.c?view=diff&rev=395731&r1=395730&r2=395731
==============================================================================
--- trunk/res/res_sip/sip_configuration.c (original)
+++ trunk/res/res_sip/sip_configuration.c Tue Jul 30 09:16:41 2013
@@ -525,6 +525,24 @@
 	struct ast_sip_endpoint *endpoint = obj;
 
 	return ast_rtp_dtls_cfg_parse(&endpoint->dtls_cfg, var->name, var->value);
+}
+
+static int t38udptl_ec_handler(const struct aco_option *opt,
+	struct ast_variable *var, void *obj)
+{
+	struct ast_sip_endpoint *endpoint = obj;
+
+	if (!strcmp(var->value, "none")) {
+		endpoint->t38udptl_ec = UDPTL_ERROR_CORRECTION_NONE;
+	} else if (!strcmp(var->value, "fec")) {
+		endpoint->t38udptl_ec = UDPTL_ERROR_CORRECTION_FEC;
+	} else if (!strcmp(var->value, "redundancy")) {
+		endpoint->t38udptl_ec = UDPTL_ERROR_CORRECTION_REDUNDANCY;
+	} else {
+		return -1;
+	}
+
+	return 0;
 }
 
 static void *sip_nat_hook_alloc(const char *name)
@@ -667,6 +685,12 @@
 	ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "namedcallgroup", "", named_groups_handler, NULL, 0, 0);
 	ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "namedpickupgroup", "", named_groups_handler, NULL, 0, 0);
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "devicestate_busy_at", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, devicestate_busy_at));
+	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38udptl", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, t38udptl));
+	ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "t38udptl_ec", "none", t38udptl_ec_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38udptl_maxdatagram", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, t38udptl_maxdatagram));
+	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "faxdetect", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, faxdetect));
+	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38udptl_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, t38udptl_nat));
+	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38udptl_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, t38udptl_ipv6));
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtpengine", "asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, rtp_engine));
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tonezone", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, zone));
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "language", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, language));

Modified: trunk/res/res_sip_session.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_sip_session.c?view=diff&rev=395731&r1=395730&r2=395731
==============================================================================
--- trunk/res/res_sip_session.c (original)
+++ trunk/res/res_sip_session.c Tue Jul 30 09:16:41 2013
@@ -412,6 +412,10 @@
 		struct ast_sip_session_sdp_handler *handler;
 		RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
 
+		if (!remote->media[i]) {
+			continue;
+		}
+
 		/* We need a null-terminated version of the media string */
 		ast_copy_pj_str(media, &local->media[i]->desc.media, sizeof(media));
 
@@ -602,6 +606,8 @@
 	char method[15];
 	/*! Callback to call when the delayed request is created. */
 	ast_sip_session_request_creation_cb on_request_creation;
+	/*! Callback to call when the delayed request SDP is created */
+	ast_sip_session_sdp_creation_cb on_sdp_creation;
 	/*! Callback to call when the delayed request receives a response */
 	ast_sip_session_response_cb on_response;
 	/*! Request to send */
@@ -611,6 +617,7 @@
 
 static struct ast_sip_session_delayed_request *delayed_request_alloc(const char *method,
 		ast_sip_session_request_creation_cb on_request_creation,
+		ast_sip_session_sdp_creation_cb on_sdp_creation,
 		ast_sip_session_response_cb on_response,
 		pjsip_tx_data *tdata)
 {
@@ -620,6 +627,7 @@
 	}
 	ast_copy_string(delay->method, method, sizeof(delay->method));
 	delay->on_request_creation = on_request_creation;
+	delay->on_sdp_creation = on_sdp_creation;
 	delay->on_response = on_response;
 	delay->tdata = tdata;
 	return delay;
@@ -636,10 +644,10 @@
 
 	if (!strcmp(delay->method, "INVITE")) {
 		ast_sip_session_refresh(session, delay->on_request_creation,
-				delay->on_response, AST_SIP_SESSION_REFRESH_METHOD_INVITE, 1);
+				delay->on_sdp_creation, delay->on_response, AST_SIP_SESSION_REFRESH_METHOD_INVITE, 1);
 	} else if (!strcmp(delay->method, "UPDATE")) {
 		ast_sip_session_refresh(session, delay->on_request_creation,
-				delay->on_response, AST_SIP_SESSION_REFRESH_METHOD_UPDATE, 1);
+				delay->on_sdp_creation, delay->on_response, AST_SIP_SESSION_REFRESH_METHOD_UPDATE, 1);
 	} else {
 		ast_log(LOG_WARNING, "Unexpected delayed %s request with no existing request structure\n", delay->method);
 		return -1;
@@ -675,10 +683,11 @@
 }
 
 static int delay_request(struct ast_sip_session *session, ast_sip_session_request_creation_cb on_request,
-		ast_sip_session_response_cb on_response, const char *method, pjsip_tx_data *tdata)
+		ast_sip_session_sdp_creation_cb on_sdp_creation, ast_sip_session_response_cb on_response,
+		const char *method, pjsip_tx_data *tdata)
 {
 	struct ast_sip_session_delayed_request *delay = delayed_request_alloc(method,
-			on_request, on_response, tdata);
+			on_request, on_sdp_creation, on_response, tdata);
 
 	if (!delay) {
 		return -1;
@@ -702,7 +711,9 @@
 }
 
 int ast_sip_session_refresh(struct ast_sip_session *session,
-		ast_sip_session_request_creation_cb on_request_creation, ast_sip_session_response_cb on_response,
+		ast_sip_session_request_creation_cb on_request_creation,
+		ast_sip_session_sdp_creation_cb on_sdp_creation,
+		ast_sip_session_response_cb on_response,
 		enum ast_sip_session_refresh_method method, int generate_new_sdp)
 {
 	pjsip_inv_session *inv_session = session->inv_session;
@@ -721,7 +732,7 @@
 			/* We can't send a reinvite yet, so delay it */
 			ast_debug(3, "Delaying sending reinvite to %s because of outstanding transaction...\n",
 					ast_sorcery_object_get_id(session->endpoint));
-			return delay_request(session, on_request_creation, on_response, "INVITE", NULL);
+			return delay_request(session, on_request_creation, on_sdp_creation, on_response, "INVITE", NULL);
 		} else if (inv_session->state != PJSIP_INV_STATE_CONFIRMED) {
 			/* Initial INVITE transaction failed to progress us to a confirmed state
 			 * which means re-invites are not possible
@@ -738,6 +749,11 @@
 			ast_log(LOG_ERROR, "Failed to generate session refresh SDP. Not sending session refresh\n");
 			return -1;
 		}
+		if (on_sdp_creation) {
+			if (on_sdp_creation(session, new_sdp)) {
+				return -1;
+			}
+		}
 	}
 
 	if (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE) {
@@ -771,6 +787,132 @@
 	.name = {"Session Module", 14},
 	.priority = PJSIP_MOD_PRIORITY_APPLICATION,
 	.on_rx_request = session_on_rx_request,
+};
+
+/*! \brief Determine whether the SDP provided requires deferral of negotiating or not
+ *
+ * \retval 1 re-invite should be deferred and resumed later
+ * \retval 0 re-invite should not be deferred
+ */
+static int sdp_requires_deferral(struct ast_sip_session *session, const pjmedia_sdp_session *sdp)
+{
+	int i;
+	if (validate_incoming_sdp(sdp)) {
+		return 0;
+	}
+
+	for (i = 0; i < sdp->media_count; ++i) {
+		/* See if there are registered handlers for this media stream type */
+		char media[20];
+		struct ast_sip_session_sdp_handler *handler;
+		RAII_VAR(struct sdp_handler_list *, handler_list, NULL, ao2_cleanup);
+		RAII_VAR(struct ast_sip_session_media *, session_media, NULL, ao2_cleanup);
+
+		/* We need a null-terminated version of the media string */
+		ast_copy_pj_str(media, &sdp->media[i]->desc.media, sizeof(media));
+
+		session_media = ao2_find(session->media, media, OBJ_KEY);
+		if (!session_media) {
+			/* if the session_media doesn't exist, there weren't
+			 * any handlers at the time of its creation */
+			continue;
+		}
+
+		if (session_media->handler && session_media->handler->defer_incoming_sdp_stream) {
+			int res;
+			handler = session_media->handler;
+			res = handler->defer_incoming_sdp_stream(
+				session, session_media, sdp, sdp->media[i]);
+			if (res) {
+				return 1;
+			}
+		}
+
+		handler_list = ao2_find(sdp_handlers, media, OBJ_KEY);
+		if (!handler_list) {
+			ast_debug(1, "No registered SDP handlers for media type '%s'\n", media);
+			continue;
+		}
+		AST_LIST_TRAVERSE(&handler_list->list, handler, next) {
+			int res;
+			if (session_media->handler) {
+				/* There is only one slot for this stream type and it has already been claimed
+				 * so it will go unhandled */
+				break;
+			}
+			if (!handler->defer_incoming_sdp_stream) {
+				continue;
+			}
+			res = handler->defer_incoming_sdp_stream(session, session_media, sdp, sdp->media[i]);
+			if (res) {
+				return 1;
+			}
+		}
+	}
+	return 0;
+}
+
+static pj_bool_t session_reinvite_on_rx_request(pjsip_rx_data *rdata)
+{
+	pjsip_dialog *dlg;
+	RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
+	pjsip_rdata_sdp_info *sdp_info;
+
+	if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD ||
+		!(dlg = pjsip_ua_find_dialog(&rdata->msg_info.cid->id, &rdata->msg_info.to->tag, &rdata->msg_info.from->tag, PJ_FALSE)) ||
+		!(session = ast_sip_dialog_get_session(dlg))) {
+		return PJ_FALSE;
+	}
+
+	if (session->deferred_reinvite) {
+		pj_str_t key, deferred_key;
+		pjsip_tx_data *tdata;
+
+		/* We use memory from the new request on purpose so the deferred reinvite pool does not grow uncontrollably */
+		pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS, &rdata->msg_info.cseq->method, rdata);
+		pjsip_tsx_create_key(rdata->tp_info.pool, &deferred_key, PJSIP_ROLE_UAS, &session->deferred_reinvite->msg_info.cseq->method,
+			session->deferred_reinvite);
+
+		/* If this is a retransmission ignore it */
+		if (!pj_strcmp(&key, &deferred_key)) {
+			return PJ_TRUE;
+		}
+
+		/* Otherwise this is a new re-invite, so reject it */
+		if (pjsip_dlg_create_response(dlg, rdata, 491, NULL, &tdata) == PJ_SUCCESS) {
+			pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL);
+		}
+
+		return PJ_TRUE;
+	}
+
+	if (!(sdp_info = pjsip_rdata_get_sdp_info(rdata)) ||
+		(sdp_info->sdp_err != PJ_SUCCESS) ||
+		!sdp_info->sdp ||
+		!sdp_requires_deferral(session, sdp_info->sdp)) {
+		return PJ_FALSE;
+	}
+
+	pjsip_rx_data_clone(rdata, 0, &session->deferred_reinvite);
+
+	return PJ_TRUE;
+}
+
+void ast_sip_session_resume_reinvite(struct ast_sip_session *session)
+{
+	if (!session->deferred_reinvite) {
+		return;
+	}
+
+	pjsip_endpt_process_rx_data(ast_sip_get_pjsip_endpoint(), session->deferred_reinvite, NULL, NULL);
+	pjsip_rx_data_free_cloned(session->deferred_reinvite);
+	session->deferred_reinvite = NULL;
+}
+
+static pjsip_module session_reinvite_module = {
+	.name = { "Session Re-Invite Module", 24 },
+	.priority = PJSIP_MOD_PRIORITY_UA_PROXY_LAYER - 1,
+	.on_rx_request = session_reinvite_on_rx_request,
 };
 
 void ast_sip_session_send_request_with_cb(struct ast_sip_session *session, pjsip_tx_data *tdata,
@@ -940,6 +1082,7 @@
 {
 	RAII_VAR(struct ast_sip_session *, session, ao2_alloc(sizeof(*session), session_destructor), ao2_cleanup);
 	struct ast_sip_session_supplement *iter;
+	int dsp_features = 0;
 	if (!session) {
 		return NULL;
 	}
@@ -971,12 +1114,20 @@
 	session->req_caps = ast_format_cap_alloc_nolock();
 
 	if (endpoint->dtmf == AST_SIP_DTMF_INBAND) {
+		dsp_features |= DSP_FEATURE_DIGIT_DETECT;
+	}
+
+	if (endpoint->faxdetect) {
+		dsp_features |= DSP_FEATURE_FAX_DETECT;
+	}
+
+	if (dsp_features) {
 		if (!(session->dsp = ast_dsp_new())) {
 			ao2_ref(session, -1);
 			return NULL;
 		}
 
-		ast_dsp_set_features(session->dsp, DSP_FEATURE_DIGIT_DETECT);
+		ast_dsp_set_features(session->dsp, dsp_features);
 	}
 
 	if (add_supplements(session)) {
@@ -1044,6 +1195,9 @@
 		pjsip_dlg_terminate(dlg);
 		return NULL;
 	}
+#ifdef PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE
+	inv_session->sdp_neg_flags = PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE;
+#endif
 
 	pjsip_timer_setting_default(&timer);
 	timer.min_se = endpoint->min_se;
@@ -1189,6 +1343,9 @@
 		pjsip_dlg_terminate(dlg);
 		return NULL;
 	}
+#ifdef PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE
+	inv_session->sdp_neg_flags = PJMEDIA_SDP_NEG_ALLOW_MEDIA_CHANGE;
+#endif
 	if (pjsip_dlg_add_usage(dlg, &session_module, NULL) != PJ_SUCCESS) {
 		if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) != PJ_SUCCESS) {
 			pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
@@ -1473,7 +1630,7 @@
 static void reschedule_reinvite(struct ast_sip_session *session, ast_sip_session_response_cb on_response, pjsip_tx_data *tdata)
 {
 	struct ast_sip_session_delayed_request *delay = delayed_request_alloc("INVITE",
-			NULL, on_response, tdata);
+			NULL, NULL, on_response, tdata);
 	pjsip_inv_session *inv = session->inv_session;
 	struct reschedule_reinvite_data *rrd = reschedule_reinvite_data_alloc(session, delay);
 	pj_time_val tv;
@@ -1710,8 +1867,9 @@
 				if (tsx->status_code == PJSIP_SC_REQUEST_PENDING) {
 					reschedule_reinvite(session, tsx->mod_data[session_module.id], tsx->last_tx);
 					return;
-				} else if (inv->state == PJSIP_INV_STATE_CONFIRMED) {
-					/* Other reinvite failures result in destroying the session. */
+				} else if (inv->state == PJSIP_INV_STATE_CONFIRMED &&
+					   tsx->status_code != 488) {
+					/* Other reinvite failures (except 488) result in destroying the session. */
 					pjsip_tx_data *tdata;
 					if (pjsip_inv_end_session(inv, 500, NULL, &tdata) == PJ_SUCCESS) {
 						ast_sip_session_send_request(session, tdata);
@@ -1952,12 +2110,14 @@
 	if (ast_sip_register_service(&session_module)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
+	ast_sip_register_service(&session_reinvite_module);
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
 static int unload_module(void)
 {
 	ast_sip_unregister_service(&session_module);
+	ast_sip_unregister_service(&session_reinvite_module);
 	if (nat_hook) {
 		ast_sorcery_delete(ast_sip_get_sorcery(), nat_hook);
 		nat_hook = NULL;

Modified: trunk/res/res_sip_session.exports.in
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_sip_session.exports.in?view=diff&rev=395731&r1=395730&r2=395731
==============================================================================
--- trunk/res/res_sip_session.exports.in (original)
+++ trunk/res/res_sip_session.exports.in Tue Jul 30 09:16:41 2013
@@ -16,6 +16,7 @@
 		LINKER_SYMBOL_PREFIXast_sip_session_create_invite;
 		LINKER_SYMBOL_PREFIXast_sip_session_create_outgoing;
 		LINKER_SYMBOL_PREFIXast_sip_dialog_get_session;
+		LINKER_SYMBOL_PREFIXast_sip_session_resume_reinvite;
 		LINKER_SYMBOL_PREFIXast_sip_channel_pvt_alloc;
 	local:
 		*;

Added: trunk/res/res_sip_t38.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_sip_t38.c?view=auto&rev=395731
==============================================================================
--- trunk/res/res_sip_t38.c (added)
+++ trunk/res/res_sip_t38.c Tue Jul 30 09:16:41 2013
@@ -1,0 +1,853 @@
+/*
+ * 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"
+
+/*! \brief The number of seconds after receiving a T.38 re-invite before automatically rejecting it */
+#define T38_AUTOMATIC_REJECTION_SECONDS 5
+
+/*! \brief Address for IPv4 UDPTL */
+static struct ast_sockaddr address_ipv4;
+
+/*! \brief Address for IPv6 UDPTL */
+static struct ast_sockaddr address_ipv6;
+
+/*! \brief T.38 state information */
+struct t38_state {
+	/*! \brief Current state */
+	enum ast_sip_session_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 Timer entry for automatically rejecting an inbound re-invite */
+	pj_timer_entry timer;
+};
+
+/*! \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);
+
+	if (!(data->frame = ast_frdup(frame))) {
+		ao2_cleanup(data);
+		return NULL;
+	}
+
+	return data;
+}
+
+/*! \brief Helper function for changing the T.38 state */
+static void t38_change_state(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
+	struct t38_state *state, enum ast_sip_session_t38state new_state)
+{
+	enum ast_sip_session_t38state old_state = session->t38state;
+	struct ast_control_t38_parameters parameters = { .request_response = 0, };
+	pj_time_val delay = { .sec = T38_AUTOMATIC_REJECTION_SECONDS };
+
+	if (old_state == new_state) {
+		return;
+	}
+
+	session->t38state = new_state;
+	ast_debug(2, "T.38 state changed to '%d' from '%d' on channel '%s'\n", new_state, old_state, ast_channel_name(session->channel));
+
+	if (pj_timer_heap_cancel(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()), &state->timer)) {
+		ast_debug(2, "Automatic T.38 rejection on channel '%s' terminated\n", ast_channel_name(session->channel));
+		ao2_ref(session, -1);
+	}
+
+	if (!session->channel) {
+		return;
+	}
+
+	switch (new_state) {
+	case T38_PEER_REINVITE:
+		ao2_ref(session, +1);
+		if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(), &state->timer, &delay) != PJ_SUCCESS) {
+			ast_log(LOG_WARNING, "Scheduling of automatic T.38 rejection for channel '%s' failed\n",
+				ast_channel_name(session->channel));
+			ao2_ref(session, -1);
+		}
+		parameters = state->their_parms;
+		parameters.max_ifp = ast_udptl_get_far_max_ifp(session_media->udptl);
+		parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
+		ast_udptl_set_tag(session_media->udptl, "%s", ast_channel_name(session->channel));
+		break;
+	case T38_ENABLED:
+		parameters = state->their_parms;
+		parameters.max_ifp = ast_udptl_get_far_max_ifp(session_media->udptl);
+		parameters.request_response = AST_T38_NEGOTIATED;
+		ast_udptl_set_tag(session_media->udptl, "%s", ast_channel_name(session->channel));
+		break;
+	case T38_REJECTED:
+	case T38_DISABLED:
+		if (old_state == T38_ENABLED) {
+			parameters.request_response = AST_T38_TERMINATED;
+		} else if (old_state == T38_LOCAL_REINVITE) {
+			parameters.request_response = AST_T38_REFUSED;
+		}
+		break;
+	case T38_LOCAL_REINVITE:
+		/* wait until we get a peer response before responding to local reinvite */
+		break;
+	}
+
+	if (parameters.request_response) {
+		ast_queue_control_data(session->channel, AST_CONTROL_T38_PARAMETERS, &parameters, sizeof(parameters));
+	}
+}
+
+/*! \brief Task function which rejects a T.38 re-invite and resumes handling it */
+static int t38_automatic_reject(void *obj)
+{
+	RAII_VAR(struct ast_sip_session *, session, obj, ao2_cleanup);
+	RAII_VAR(struct ast_datastore *, datastore, ast_sip_session_get_datastore(session, "t38"), ao2_cleanup);
+	RAII_VAR(struct ast_sip_session_media *, session_media, ao2_find(session->media, "image", OBJ_KEY), ao2_cleanup);
+
+	if (!datastore) {
+		return 0;
+	}
+
+	ast_debug(2, "Automatically rejecting T.38 request on channel '%s'\n", ast_channel_name(session->channel));
+
+	t38_change_state(session, session_media, datastore->data, T38_REJECTED);
+	ast_sip_session_resume_reinvite(session);
+
+	return 0;
+}
+
+/*! \brief Timer entry callback which queues a task to reject a T.38 re-invite and resume handling it */
+static void t38_automatic_reject_timer_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry)
+{
+	struct ast_sip_session *session = entry->user_data;
+
+	if (ast_sip_push_task(session->serializer, t38_automatic_reject, session)) {
+		ao2_ref(session, -1);
+	}
+}
+
+/*! \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);
+	struct t38_state *state;
+
+	/* 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;
+	}
+
+	state = datastore->data;
+
+	/* This will get bumped up before scheduling */
+	state->timer.user_data = session;
+	state->timer.cb = t38_automatic_reject_timer_cb;
+
+	datastore->data = state;
+
+	return state;
+}
+
+/*! \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,
+		session->endpoint->t38udptl_ipv6 ? &address_ipv6 : &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);
+	ast_udptl_setnat(session_media->udptl, session->endpoint->t38udptl_nat);
+
+	return 0;
+}
+
+/*! \brief Callback for when T.38 reinvite SDP is created */
+static int t38_reinvite_sdp_cb(struct ast_sip_session *session, pjmedia_sdp_session *sdp)
+{
+	int stream;
+
+	/* Move the image media stream to the front and have it as the only stream, pjmedia will fill in
+	 * dummy streams for the rest
+	 */
+	for (stream = 0; stream < sdp->media_count++; ++stream) {
+		if (!pj_strcmp2(&sdp->media[stream]->desc.media, "image")) {
+			sdp->media[0] = sdp->media[stream];
+			sdp->media_count = 1;
+			break;
+		}
+	}
+
+	return 0;
+}
+
+/*! \brief Callback for when a response is received for a T.38 re-invite */
+static int t38_reinvite_response_cb(struct ast_sip_session *session, pjsip_rx_data *rdata)
+{
+	struct pjsip_status_line status = rdata->msg_info.msg->line.status;
+	struct t38_state *state;
+	RAII_VAR(struct ast_sip_session_media *, session_media, NULL, ao2_cleanup);
+
+	if (status.code == 100) {
+		return 0;
+	}
+
+	if (!(state = t38_state_get_or_alloc(session)) ||
+		!(session_media = ao2_find(session->media, "image", OBJ_KEY))) {
+		ast_log(LOG_WARNING, "Received response to T.38 re-invite on '%s' but state unavailable\n",
+			ast_channel_name(session->channel));
+		return 0;
+	}
+

[... 568 lines stripped ...]



More information about the svn-commits mailing list