Allow the SIP_CODEC family of variables to specify more than one codec
authorMatthew Jordan <mjordan@digium.com>
Wed, 21 Aug 2013 13:41:05 +0000 (13:41 +0000)
committerMatthew Jordan <mjordan@digium.com>
Wed, 21 Aug 2013 13:41:05 +0000 (13:41 +0000)
The SIP_CODEC family of variables let you set the preferred codec to be
offered on an outbound INVITE request. However, for video calls, you need to
be able to set both the audio and video codecs to be offered. This patch lets
the SIP_CODEC variables accept a comma delineated list of codecs. The first
codec in the list is set as the preferred codec; additional codecs are still
offered however.

This lets a dialplan writer set both audio and video codecs, e.g.,
Set(SIP_CODEC=ulaw,h264)

Note that this feature was written by both Dennis Guse and Frank Haase

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

(closes issue ASTERISK-21976)
Reported by: Denis Guse
Tested by: mjordan, sysreq
patches:
  patch-channels-chan__sip.c-393919 uploaded by dennis.guse (license 6513)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@397243 65c4cc65-6c06-0410-ace0-fbb531ad65f3

CHANGES
channels/chan_sip.c

diff --git a/CHANGES b/CHANGES
index 292cde2..e7998d4 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -553,6 +553,10 @@ chan_sip
    set of proxies by using a pre-loaded route-set defined by the Path headers in
    the REGISTER request. See Realtime updates for more configuration information.
 
+ * The SIP_CODEC family of variables may now specify more than one codec. Each
+   codec must be separated by a comma. The first codec specified is the
+   preferred codec for the offer. This allows a dialplan writer to specify both
+   audio and video codecs, e.g., Set(SIP_CODEC=ulaw,h264)
 
 Functions
 ------------------
index 2e7615a..519f1dc 100644 (file)
@@ -7350,35 +7350,57 @@ static int sip_hangup(struct ast_channel *ast)
        return 0;
 }
 
-/*! \brief Try setting codec suggested by the SIP_CODEC channel variable */
+/*! \brief Try setting the codecs suggested by the SIP_CODEC channel variable */
 static void try_suggested_sip_codec(struct sip_pvt *p)
 {
        struct ast_format fmt;
-       const char *codec;
+       const char *codec_list;
+       char *codec_list_copy;
+       struct ast_format_cap *original_jointcaps;
+       char *codec;
+       int first_codec = 1;
 
-       ast_format_clear(&fmt);
+       char *strtok_ptr;
 
        if (p->outgoing_call) {
-               codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_OUTBOUND");
-       } else if (!(codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_INBOUND"))) {
-               codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
+               codec_list = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_OUTBOUND");
+       } else if (!(codec_list = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_INBOUND"))) {
+               codec_list = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
        }
 
-       if (!codec)
+       if (ast_strlen_zero(codec_list)) {
                return;
+       }
 
-       ast_getformatbyname(codec, &fmt);
-       if (fmt.id) {
-               ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
-               if (ast_format_cap_iscompatible(p->jointcaps, &fmt)) {
-                       ast_format_cap_set(p->jointcaps, &fmt);
-                       ast_format_cap_set(p->caps, &fmt);
-               } else
-                       ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
-       } else
-               ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
+       codec_list_copy = ast_strdupa(codec_list);
+       original_jointcaps = ast_format_cap_dup(p->jointcaps);
+
+       for (codec = strtok_r(codec_list_copy, ",", &strtok_ptr); codec; codec = strtok_r(NULL, ",", &strtok_ptr)) {
+               codec = ast_strip(codec);
+
+               if (!ast_getformatbyname(codec, &fmt)) {
+                       ast_log(AST_LOG_NOTICE, "Ignoring ${SIP_CODEC*} variable because of unrecognized/not configured codec %s (check allow/disallow in sip.conf)\n", codec);
+                       continue;
+               }
+               if (ast_format_cap_iscompatible(original_jointcaps, &fmt)) {
+                       if (first_codec) {
+                               ast_verb(4, "Set codec to '%s' for this call because of ${SIP_CODEC*} variable\n", codec);
+                               ast_format_cap_set(p->jointcaps, &fmt);
+                               ast_format_cap_set(p->caps, &fmt);
+                               first_codec = 0;
+                       } else {
+                               ast_verb(4, "Add codec to '%s' for this call because of ${SIP_CODEC*} variable\n", codec);
+                               ast_format_cap_add(p->jointcaps, &fmt);
+                               ast_format_cap_add(p->caps, &fmt);
+                       }
+               } else {
+                       ast_log(AST_LOG_NOTICE, "Ignoring ${SIP_CODEC*} variable because it is not shared by both ends: %s\n", codec);
+               }
+       }
+       ast_format_cap_destroy(original_jointcaps);
        return;
-}
+ }
+
 
 /*! \brief  sip_answer: Answer SIP call , send 200 OK on Invite
  * Part of PBX interface */