[Asterisk-code-review] codec negotiation: add incoming_call_offer_prefs option (asterisk[master])

George Joseph asteriskteam at digium.com
Mon Mar 9 15:07:18 CDT 2020


George Joseph has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/13842 )

Change subject: codec negotiation: add incoming_call_offer_prefs option
......................................................................

codec negotiation: add incoming_call_offer_prefs option

Add a new option, incoming_call_offer_pref, to res_pjsip endpoints that
specifies the preferred order of codecs after receiving an offer.

This patch does the following:

  Adds a new enumeration, ast_sip_call_codec_pref, used by the the new
configuration option that's added to the endpoint media structure.

  Adds a new ast_sip_session_caps structure that's set for each session media
object.

  Creates a new file, res_pjsip_session_caps that "implements" the new
structure and option, and is compiled into the res_pjsip_session library.

ASTERISK-28756 #close

Change-Id: I35e7a2a0c236cfb6bd9cdf89539f57a1ffefc76f
---
M configs/samples/pjsip.conf.sample
A doc/CHANGES-staging/res_pjsip_incoming_call_offer_pref.txt
M include/asterisk/res_pjsip.h
M include/asterisk/res_pjsip_session.h
A include/asterisk/res_pjsip_session_caps.h
M res/Makefile
M res/res_pjsip.c
M res/res_pjsip/pjsip_configuration.c
M res/res_pjsip_sdp_rtp.c
M res/res_pjsip_session.c
A res/res_pjsip_session/pjsip_session_caps.c
11 files changed, 484 insertions(+), 20 deletions(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved; Approved for Submit



diff --git a/configs/samples/pjsip.conf.sample b/configs/samples/pjsip.conf.sample
index 92d9aaa..695ba5d 100644
--- a/configs/samples/pjsip.conf.sample
+++ b/configs/samples/pjsip.conf.sample
@@ -798,6 +798,16 @@
                    ; "0" or not enabled)
 ;contact_user= ; On outgoing requests, force the user portion of the Contact
                ; header to this value (default: "")
+;incoming_call_offer_pref= ; Sets the preferred codecs, and order to use between
+                           ; those received in the offer, and those set in this
+                           ; configuration's allow line. Valid values include:
+                           ;
+                           ; local - prefer and order by configuration (default).
+                           ; local_single - prefer and order by configuration,
+                           ;     but only choose 'top' most codec
+                           ; remote - prefer and order by incoming sdp.
+                           ; remote_single - prefer and order by incoming sdp,
+                           ;     but only choose 'top' most codec
 ;preferred_codec_only=yes       ; Respond to a SIP invite with the single most preferred codec
                                 ; rather than advertising all joint codec capabilities. This
                                 ; limits the other side's codec choice to exactly what we prefer.
diff --git a/doc/CHANGES-staging/res_pjsip_incoming_call_offer_pref.txt b/doc/CHANGES-staging/res_pjsip_incoming_call_offer_pref.txt
new file mode 100644
index 0000000..5a12052
--- /dev/null
+++ b/doc/CHANGES-staging/res_pjsip_incoming_call_offer_pref.txt
@@ -0,0 +1,53 @@
+Subject: res_pjsip
+Subject: res_pjsip_session
+Master-Only: True
+
+A new option, incoming_call_offer_pref, was added to res_pjsip endpoints that
+specifies the preferred order of codecs to use between those received in the
+offer, and those set in the configuration.
+
+Valid values include:
+  local - prefer and order by configuration (default).
+  local_single - prefer and order by configuration, but only choose 'top'
+                 most codec
+  remote - prefer and order by incoming sdp.
+  remote_single - prefer and order by incoming sdp, but only choose 'top' most
+                  most codec
+
+Example A:
+  [alice]
+  type=endpoint
+  incoming_call_offer_pref=local
+  allow=!all,opus,alaw,ulaw
+
+  Alice's incoming sdp=g722,ulaw,alaw
+  RESULT: alaw,ulaw
+
+Example B:
+  [alice]
+  type=endpoint
+  incoming_call_offer_pref=local_single
+  allow=!all,opus,alaw,ulaw
+
+  Alice's incoming sdp=g722,ulaw,alaw
+  RESULT: alaw
+
+Example C:
+  [alice]
+  type=endpoint
+  incoming_call_offer_pref=remote
+  allow=!all,opus,alaw,ulaw
+
+  Alice's incoming sdp=g722,ulaw,alaw
+  RESULT: ulaw,alaw
+
+Example D:
+  [alice]
+  type=endpoint
+  incoming_call_offer_pref=remote_single
+  allow=!all,opus,alaw,ulaw
+
+  Alice's incoming sdp=g722,ulaw,alaw
+  RESULT: ulaw
+
+
diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h
index b26aba9..816e614 100644
--- a/include/asterisk/res_pjsip.h
+++ b/include/asterisk/res_pjsip.h
@@ -510,6 +510,24 @@
 };
 
 /*!
+ * \brief Incoming/Outgoing call offer/answer joint codec preference.
+ */
+enum ast_sip_call_codec_pref {
+	/*! Prefer, and order by local values */
+	AST_SIP_CALL_CODEC_PREF_LOCAL,
+	/*! Prefer, and order by local values (intersection) */
+	AST_SIP_CALL_CODEC_PREF_LOCAL_LIMIT,
+	/*! Prefer, and order by local values (top/first only) */
+	AST_SIP_CALL_CODEC_PREF_LOCAL_SINGLE,
+	/*! Prefer, and order by remote values */
+	AST_SIP_CALL_CODEC_PREF_REMOTE,
+	/*! Prefer, and order by remote values (intersection) */
+	AST_SIP_CALL_CODEC_PREF_REMOTE_LIMIT,
+	/*! Prefer, and order by remote values (top/first only) */
+	AST_SIP_CALL_CODEC_PREF_REMOTE_SINGLE,
+};
+
+/*!
  * \brief Session timers options
  */
 struct ast_sip_timer_options {
@@ -750,6 +768,8 @@
 	unsigned int bundle;
 	/*! Enable webrtc settings and defaults */
 	unsigned int webrtc;
+	/*! Codec preference for an incoming offer */
+	enum ast_sip_call_codec_pref incoming_call_offer_pref;
 };
 
 /*!
diff --git a/include/asterisk/res_pjsip_session.h b/include/asterisk/res_pjsip_session.h
index 7e89742..a5ae6f1 100644
--- a/include/asterisk/res_pjsip_session.h
+++ b/include/asterisk/res_pjsip_session.h
@@ -59,6 +59,7 @@
 
 struct ast_sip_session_sdp_handler;
 struct ast_sip_session;
+struct ast_sip_session_caps;
 struct ast_sip_session_media;
 
 typedef struct ast_frame *(*ast_sip_session_media_read_cb)(struct ast_sip_session *session, struct ast_sip_session_media *session_media);
@@ -79,6 +80,8 @@
 	struct ast_sip_session_sdp_handler *handler;
 	/*! \brief Holds SRTP information */
 	struct ast_sdp_srtp *srtp;
+	/*! \brief Media format capabilities */
+	struct ast_sip_session_caps *caps;
 	/*! \brief What type of encryption is in use on this stream */
 	enum ast_sip_session_media_encryption encryption;
 	/*! \brief The media transport in use for this stream */
diff --git a/include/asterisk/res_pjsip_session_caps.h b/include/asterisk/res_pjsip_session_caps.h
new file mode 100644
index 0000000..810a1e6
--- /dev/null
+++ b/include/asterisk/res_pjsip_session_caps.h
@@ -0,0 +1,82 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2020, Sangoma Technologies Corporation
+ *
+ * Kevin Harwell <kharwell at sangoma.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.
+ */
+#ifndef RES_PJSIP_SESSION_CAPS_H
+#define RES_PJSIP_SESSION_CAPS_H
+
+struct ast_format_cap;
+struct ast_sip_session;
+struct ast_sip_session_media;
+struct ast_sip_session_caps;
+
+/*!
+ * \brief Allocate a SIP session capabilities object.
+ * \since 18.0.0
+ *
+ * \retval An ao2 allocated SIP session capabilities object, or NULL on error
+ */
+struct ast_sip_session_caps *ast_sip_session_caps_alloc(void);
+
+/*!
+ * \brief Set the incoming call offer capabilities for a session.
+ * \since 18.0.0
+ *
+ * This will replace any capabilities already present.
+ *
+ * \param caps A session's capabilities object
+ * \param cap The capabilities to set it to
+ */
+void ast_sip_session_set_incoming_call_offer_cap(struct ast_sip_session_caps *caps,
+	struct ast_format_cap *cap);
+
+/*!
+ * \brief Get the incoming call offer capabilities.
+ * \since 18.0.0
+ *
+ * \note Returned objects reference is not incremented.
+ *
+ * \param caps A session's capabilities object
+ *
+ * \retval An incoming call offer capabilities object
+ */
+const struct ast_format_cap *ast_sip_session_get_incoming_call_offer_cap(
+	const struct ast_sip_session_caps *caps);
+
+/*!
+ * \brief Make the incoming call offer capabilities for a session.
+ * \since 18.0.0
+ *
+ * Creates and sets a list of joint capabilities between the given remote
+ * capabilities, and pre-configured ones. The resulting joint list is then
+ * stored, and 'owned' (reference held) by the session.
+ *
+ * If the incoming capabilities have been set elsewhere, this will not replace
+ * those. It will however, return a pointer to the current set.
+ *
+ * \note Returned object's reference is not incremented.
+ *
+ * \param session The session
+ * \param session_media An associated media session
+ * \param remote Capabilities of a device
+ *
+ * \retval A pointer to the incoming call offer capabilities
+ */
+const struct ast_format_cap *ast_sip_session_join_incoming_call_offer_cap(
+	const struct ast_sip_session *session, const struct ast_sip_session_media *session_media,
+	const struct ast_format_cap *remote);
+
+#endif /* RES_PJSIP_SESSION_CAPS_H */
diff --git a/res/Makefile b/res/Makefile
index 78410ad..b4f50b7 100644
--- a/res/Makefile
+++ b/res/Makefile
@@ -66,6 +66,7 @@
 $(call MOD_ADD_C,res_snmp,snmp/agent.c)
 $(call MOD_ADD_C,res_parking,$(wildcard parking/*.c))
 $(call MOD_ADD_C,res_pjsip,$(wildcard res_pjsip/*.c))
+$(call MOD_ADD_C,res_pjsip_session,$(wildcard res_pjsip_session/*.c))
 $(call MOD_ADD_C,res_prometheus,$(wildcard prometheus/*.c))
 $(call MOD_ADD_C,res_ari,ari/cli.c ari/config.c ari/ari_websockets.c)
 $(call MOD_ADD_C,res_ari_model,ari/ari_model_validators.c)
diff --git a/res/res_pjsip.c b/res/res_pjsip.c
index 0dcbcea..4d77a6d 100644
--- a/res/res_pjsip.c
+++ b/res/res_pjsip.c
@@ -925,6 +925,27 @@
 				<configOption name="preferred_codec_only" default="no">
 					<synopsis>Respond to a SIP invite with the single most preferred codec rather than advertising all joint codec capabilities. This limits the other side's codec choice to exactly what we prefer.</synopsis>
 				</configOption>
+				<configOption name="incoming_call_offer_pref" default="local">
+					<synopsis>After receiving an incoming offer create a list of preferred codecs between
+					those received in the SDP offer, and those specified in endpoint configuration.</synopsis>
+					<description>
+						<note><para>This list will consist of only those codecs found in both.</para></note>
+						<enumlist>
+							<enum name="local"><para>
+								Order by the endpoint configuration allow line (default)
+							</para></enum>
+							<enum name="local_single"><para>
+								Order by the endpoint configuration allow line, but the list will only contain the first, or 'top' item
+							</para></enum>
+							<enum name="remote"><para>
+								Order by what is received in the SDP offer
+							</para></enum>
+							<enum name="remote_single"><para>
+								Order by what is received in the SDP offer, but the list will only contain the first, or 'top' item
+							</para></enum>
+						</enumlist>
+					</description>
+				</configOption>
 				<configOption name="rtp_keepalive">
 					<synopsis>Number of seconds between RTP comfort noise keepalive packets.</synopsis>
 					<description><para>
diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c
index 8d5b5a8..1d61558 100644
--- a/res/res_pjsip/pjsip_configuration.c
+++ b/res/res_pjsip/pjsip_configuration.c
@@ -1121,6 +1121,48 @@
 	return 0;
 }
 
+static const char *sip_call_codec_pref_strings[] = {
+	[AST_SIP_CALL_CODEC_PREF_LOCAL] = "local",
+	[AST_SIP_CALL_CODEC_PREF_LOCAL_LIMIT] = "local_limit",
+	[AST_SIP_CALL_CODEC_PREF_LOCAL_SINGLE] = "local_single",
+	[AST_SIP_CALL_CODEC_PREF_REMOTE] = "remote",
+	[AST_SIP_CALL_CODEC_PREF_REMOTE_LIMIT] = "remote_limit",
+	[AST_SIP_CALL_CODEC_PREF_REMOTE_SINGLE] = "remote_single",
+};
+
+static int incoming_call_offer_pref_handler(const struct aco_option *opt,
+	struct ast_variable *var, void *obj)
+{
+	struct ast_sip_endpoint *endpoint = obj;
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_LEN(sip_call_codec_pref_strings); ++i) {
+		if (!strcmp(var->value, sip_call_codec_pref_strings[i])) {
+			/* Local and remote limit are not available values for this option */
+			if (i == AST_SIP_CALL_CODEC_PREF_LOCAL_LIMIT ||
+				i == AST_SIP_CALL_CODEC_PREF_REMOTE_LIMIT) {
+				return -1;
+			}
+
+			endpoint->media.incoming_call_offer_pref = i;
+			return 0;
+		}
+	}
+
+	return -1;
+}
+
+static int incoming_call_offer_pref_to_str(const void *obj, const intptr_t *args, char **buf)
+{
+	const struct ast_sip_endpoint *endpoint = obj;
+
+	if (ARRAY_IN_BOUNDS(endpoint->media.incoming_call_offer_pref, sip_call_codec_pref_strings)) {
+		*buf = ast_strdup(sip_call_codec_pref_strings[endpoint->media.incoming_call_offer_pref]);
+	}
+
+	return 0;
+}
+
 static void *sip_nat_hook_alloc(const char *name)
 {
 	return ast_sorcery_generic_alloc(sizeof(struct ast_sip_nat_hook), NULL);
@@ -1966,6 +2008,8 @@
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "accept_multiple_sdp_answers", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.accept_multiple_sdp_answers));
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "suppress_q850_reason_headers", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, suppress_q850_reason_headers));
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ignore_183_without_sdp", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, ignore_183_without_sdp));
+	ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "incoming_call_offer_pref", "local",
+		incoming_call_offer_pref_handler, incoming_call_offer_pref_to_str, NULL, 0, 0);
 
 	if (ast_sip_initialize_sorcery_transport()) {
 		ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
diff --git a/res/res_pjsip_sdp_rtp.c b/res/res_pjsip_sdp_rtp.c
index fb249a7..d7bd065 100644
--- a/res/res_pjsip_sdp_rtp.c
+++ b/res/res_pjsip_sdp_rtp.c
@@ -56,6 +56,7 @@
 
 #include "asterisk/res_pjsip.h"
 #include "asterisk/res_pjsip_session.h"
+#include "asterisk/res_pjsip_session_caps.h"
 
 /*! \brief Scheduler for RTCP purposes */
 static struct ast_sched_context *sched;
@@ -373,6 +374,81 @@
 	}
 }
 
+static int apply_cap_to_bundled(struct ast_sip_session_media *session_media,
+	struct ast_sip_session_media *session_media_transport,
+	struct ast_stream *asterisk_stream, const struct ast_format_cap *joint)
+{
+	if (!joint) {
+		return -1;
+	}
+
+	ast_stream_set_formats(asterisk_stream, (struct ast_format_cap *)joint);
+
+	/* If this is a bundled stream then apply the payloads to RTP instance acting as transport to prevent conflicts */
+	if (session_media_transport != session_media && session_media->bundled) {
+		int index;
+
+		for (index = 0; index < ast_format_cap_count(joint); ++index) {
+			struct ast_format *format = ast_format_cap_get_format(joint, index);
+			int rtp_code;
+
+			/* Ensure this payload is in the bundle group transport codecs, this purposely doesn't check the return value for
+			 * things as the format is guaranteed to have a payload already.
+			 */
+			rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0);
+			ast_rtp_codecs_payload_set_rx(ast_rtp_instance_get_codecs(session_media_transport->rtp), rtp_code, format);
+
+			ao2_ref(format, -1);
+		}
+	}
+
+	return 0;
+}
+
+static const struct ast_format_cap *set_incoming_call_offer_cap(
+	struct ast_sip_session *session, struct ast_sip_session_media *session_media,
+	const struct pjmedia_sdp_media *stream)
+{
+	const struct ast_format_cap *incoming_call_offer_cap;
+	struct ast_format_cap *remote;
+	struct ast_rtp_codecs codecs = AST_RTP_CODECS_NULL_INIT;
+	int fmts = 0;
+
+	remote = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	if (!remote) {
+		ast_log(LOG_ERROR, "Failed to allocate %s incoming remote capabilities\n",
+				ast_codec_media_type2str(session_media->type));
+		return NULL;
+	}
+
+	/* Get the peer's capabilities*/
+	get_codecs(session, stream, &codecs, session_media);
+	ast_rtp_codecs_payload_formats(&codecs, remote, &fmts);
+
+	incoming_call_offer_cap = ast_sip_session_join_incoming_call_offer_cap(
+		session, session_media, remote);
+
+	ao2_ref(remote, -1);
+
+	if (!incoming_call_offer_cap) {
+		ast_rtp_codecs_payloads_destroy(&codecs);
+		return NULL;
+	}
+
+	/*
+	 * Setup rx payload type mapping to prefer the mapping
+	 * from the peer that the RFC says we SHOULD use.
+	 */
+	ast_rtp_codecs_payloads_xover(&codecs, &codecs, NULL);
+
+	ast_rtp_codecs_payloads_copy(&codecs,
+		ast_rtp_instance_get_codecs(session_media->rtp), session_media->rtp);
+
+	ast_rtp_codecs_payloads_destroy(&codecs);
+
+	return incoming_call_offer_cap;
+}
+
 static int set_caps(struct ast_sip_session *session,
 	struct ast_sip_session_media *session_media,
 	struct ast_sip_session_media *session_media_transport,
@@ -432,25 +508,7 @@
 	ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp),
 		session_media->rtp);
 
-	ast_stream_set_formats(asterisk_stream, joint);
-
-	/* If this is a bundled stream then apply the payloads to RTP instance acting as transport to prevent conflicts */
-	if (session_media_transport != session_media && session_media->bundled) {
-		int index;
-
-		for (index = 0; index < ast_format_cap_count(joint); ++index) {
-			struct ast_format *format = ast_format_cap_get_format(joint, index);
-			int rtp_code;
-
-			/* Ensure this payload is in the bundle group transport codecs, this purposely doesn't check the return value for
-			 * things as the format is guaranteed to have a payload already.
-			 */
-			rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0);
-			ast_rtp_codecs_payload_set_rx(ast_rtp_instance_get_codecs(session_media_transport->rtp), rtp_code, format);
-
-			ao2_ref(format, -1);
-		}
-	}
+	apply_cap_to_bundled(session_media, session_media_transport, asterisk_stream, joint);
 
 	if (session->channel && ast_sip_session_is_pending_stream_default(session, asterisk_stream)) {
 		ast_channel_lock(session->channel);
@@ -1420,7 +1478,8 @@
 		session_media->remotely_held_changed = 1;
 	}
 
-	if (set_caps(session, session_media, session_media_transport, stream, 1, asterisk_stream)) {
+	if (apply_cap_to_bundled(session_media, session_media_transport, asterisk_stream,
+			set_incoming_call_offer_cap(session, session_media, stream))) {
 		return 0;
 	}
 
diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index c1e89c5..0c752b8 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -30,6 +30,7 @@
 
 #include "asterisk/res_pjsip.h"
 #include "asterisk/res_pjsip_session.h"
+#include "asterisk/res_pjsip_session_caps.h"
 #include "asterisk/callerid.h"
 #include "asterisk/datastore.h"
 #include "asterisk/module.h"
@@ -466,6 +467,8 @@
 
 	ast_free(session_media->mid);
 	ast_free(session_media->remote_mslabel);
+
+	ao2_cleanup(session_media->caps);
 }
 
 struct ast_sip_session_media *ast_sip_session_media_state_add(struct ast_sip_session *session,
@@ -524,6 +527,12 @@
 		} else {
 			session_media->bundle_group = -1;
 		}
+
+		session_media->caps = ast_sip_session_caps_alloc();
+		if (!session_media->caps) {
+			ao2_ref(session_media, -1);
+			return NULL;
+		}
 	}
 
 	if (AST_VECTOR_REPLACE(&media_state->sessions, position, session_media)) {
diff --git a/res/res_pjsip_session/pjsip_session_caps.c b/res/res_pjsip_session/pjsip_session_caps.c
new file mode 100644
index 0000000..e131200
--- /dev/null
+++ b/res/res_pjsip_session/pjsip_session_caps.c
@@ -0,0 +1,162 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2020, Sangoma Technologies Corporation
+ *
+ * Kevin Harwell <kharwell 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.
+ */
+
+#include "asterisk.h"
+
+#include "asterisk/astobj2.h"
+#include "asterisk/channel.h"
+#include "asterisk/format.h"
+#include "asterisk/format_cap.h"
+#include "asterisk/logger.h"
+#include "asterisk/sorcery.h"
+
+#include <pjsip_ua.h>
+
+#include "asterisk/res_pjsip.h"
+#include "asterisk/res_pjsip_session.h"
+#include "asterisk/res_pjsip_session_caps.h"
+
+struct ast_sip_session_caps {
+	struct ast_format_cap *incoming_call_offer_cap;
+};
+
+static void log_caps(int level, const char *file, int line, const char *function,
+	const char *msg, const struct ast_sip_session *session,
+	const struct ast_sip_session_media *session_media, const struct ast_format_cap *local,
+	const struct ast_format_cap *remote, const struct ast_format_cap *joint)
+{
+	struct ast_str *s1;
+	struct ast_str *s2;
+	struct ast_str *s3;
+
+	if (level == __LOG_DEBUG && !DEBUG_ATLEAST(3)) {
+		return;
+	}
+
+	s1 = local ? ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN) : NULL;
+	s2 = remote ? ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN) : NULL;
+	s3 = joint ? ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN) : NULL;
+
+	ast_log(level, file, line, function, "'%s' %s '%s' capabilities -%s%s%s%s%s%s\n",
+		session->channel ? ast_channel_name(session->channel) :
+			ast_sorcery_object_get_id(session->endpoint),
+		msg ? msg : "-", ast_codec_media_type2str(session_media->type),
+		s1 ? " local: " : "", s1 ? ast_format_cap_get_names(local, &s1) : "",
+		s2 ? " remote: " : "", s2 ? ast_format_cap_get_names(remote, &s2) : "",
+		s3 ? " joint: " : "", s3 ? ast_format_cap_get_names(joint, &s3) : "");
+}
+
+static void sip_session_caps_destroy(void *obj)
+{
+	struct ast_sip_session_caps *caps = obj;
+
+	ao2_cleanup(caps->incoming_call_offer_cap);
+}
+
+struct ast_sip_session_caps *ast_sip_session_caps_alloc(void)
+{
+	return ao2_alloc_options(sizeof(struct ast_sip_session_caps),
+		sip_session_caps_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
+}
+
+void ast_sip_session_set_incoming_call_offer_cap(struct ast_sip_session_caps *caps,
+	struct ast_format_cap *cap)
+{
+	ao2_cleanup(caps->incoming_call_offer_cap);
+	caps->incoming_call_offer_cap = ao2_bump(cap);
+}
+
+const struct ast_format_cap *ast_sip_session_get_incoming_call_offer_cap(
+	const struct ast_sip_session_caps *caps)
+{
+	return caps->incoming_call_offer_cap;
+}
+
+const struct ast_format_cap *ast_sip_session_join_incoming_call_offer_cap(
+	const struct ast_sip_session *session, const struct ast_sip_session_media *session_media,
+	const struct ast_format_cap *remote)
+{
+	enum ast_sip_call_codec_pref pref;
+	struct ast_format_cap *joint;
+	struct ast_format_cap *local;
+
+	joint = session_media->caps->incoming_call_offer_cap;
+
+	if (joint) {
+		/*
+		 * If the incoming call offer capabilities have been set elsewhere, e.g. dialplan
+		 * then those take precedence.
+		 */
+		return joint;
+	}
+
+	joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	local = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+
+	if (!joint || !local) {
+		ast_log(LOG_ERROR, "Failed to allocate %s incoming call offer capabilities\n",
+				ast_codec_media_type2str(session_media->type));
+
+		ao2_cleanup(joint);
+		ao2_cleanup(local);
+		return NULL;
+	}
+
+	pref = session->endpoint->media.incoming_call_offer_pref;
+	ast_format_cap_append_from_cap(local, session->endpoint->media.codecs,
+		session_media->type);
+
+	if (pref < AST_SIP_CALL_CODEC_PREF_REMOTE) {
+		ast_format_cap_get_compatible(local, remote, joint); /* Prefer local */
+	} else {
+		ast_format_cap_get_compatible(remote, local, joint); /* Prefer remote */
+	}
+
+	if (ast_format_cap_empty(joint)) {
+		log_caps(LOG_NOTICE, "No joint incoming", session, session_media, local, remote, NULL);
+
+		ao2_ref(joint, -1);
+		ao2_ref(local, -1);
+		return NULL;
+	}
+
+	if (pref == AST_SIP_CALL_CODEC_PREF_LOCAL_SINGLE ||
+		pref == AST_SIP_CALL_CODEC_PREF_REMOTE_SINGLE ||
+		session->endpoint->preferred_codec_only) {
+
+		/*
+		 * Save the most preferred one. Session capabilities are per stream and
+		 * a stream only carries a single media type, so no reason to worry with
+		 * the type here (i.e different or multiple types)
+		 */
+		struct ast_format *single = ast_format_cap_get_format(joint, 0);
+		/* Remove all formats */
+		ast_format_cap_remove_by_type(joint, AST_MEDIA_TYPE_UNKNOWN);
+		/* Put the most preferred one back */
+		ast_format_cap_append(joint, single, 0);
+		ao2_ref(single, -1);
+	}
+
+	log_caps(LOG_DEBUG, "Joint incoming", session, session_media, local, remote, joint);
+
+	ao2_ref(local, -1);
+
+	ast_sip_session_set_incoming_call_offer_cap(session_media->caps, joint);
+
+	return joint;
+}

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/13842
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I35e7a2a0c236cfb6bd9cdf89539f57a1ffefc76f
Gerrit-Change-Number: 13842
Gerrit-PatchSet: 3
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-CC: Sean Bright <sean.bright at gmail.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200309/d4693a55/attachment-0001.html>


More information about the asterisk-code-review mailing list