<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][15266] </title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=15266">15266</a></dd>
<dt>Author</dt> <dd>anthm</dd>
<dt>Date</dt> <dd>2009-10-29 00:47:17 -0500 (Thu, 29 Oct 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>refactor ringback so it does not start early_media until it has to</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcincludeswitch_typesh">freeswitch/trunk/src/include/switch_types.h</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiasofiac">freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_channelc">freeswitch/trunk/src/switch_channel.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_ivr_originatec">freeswitch/trunk/src/switch_ivr_originate.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcincludeswitch_typesh"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/include/switch_types.h (15265 => 15266)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/include/switch_types.h        2009-10-29 05:11:54 UTC (rev 15265)
+++ freeswitch/trunk/src/include/switch_types.h        2009-10-29 05:47:17 UTC (rev 15266)
</span><span class="lines">@@ -126,6 +126,8 @@
</span><span class="cx"> #define SWITCH_CURRENT_APPLICATION_VARIABLE "current_application"
</span><span class="cx"> #define SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE "proto_specific_hangup_cause"
</span><span class="cx"> #define SWITCH_CHANNEL_EXECUTE_ON_ANSWER_VARIABLE "execute_on_answer"
</span><ins>+#define SWITCH_CHANNEL_EXECUTE_ON_PRE_ANSWER_VARIABLE "execute_on_pre_answer"
+#define SWITCH_CHANNEL_EXECUTE_ON_MEDIA_VARIABLE "execute_on_media"
</ins><span class="cx"> #define SWITCH_CHANNEL_API_ON_ANSWER_VARIABLE "api_on_answer"
</span><span class="cx"> #define SWITCH_CHANNEL_EXECUTE_ON_RING_VARIABLE "execute_on_ring"
</span><span class="cx"> #define SWITCH_CALL_TIMEOUT_VARIABLE "call_timeout"
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiasofiac"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c (15265 => 15266)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c        2009-10-29 05:11:54 UTC (rev 15265)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c        2009-10-29 05:47:17 UTC (rev 15266)
</span><span class="lines">@@ -3578,7 +3578,7 @@
</span><span class="cx">                         goto done;
</span><span class="cx">                 }
</span><span class="cx">
</span><del>-                if ((switch_channel_test_flag(channel, CF_EARLY_MEDIA) || switch_channel_test_flag(channel, CF_ANSWERED)) && status > 100 && status < 200) {
</del><ins>+                if ((switch_channel_test_flag(channel, CF_EARLY_MEDIA) || switch_channel_test_flag(channel, CF_ANSWERED)) && status == 180) {
</ins><span class="cx">                         /* Must you send 180 after 183 w/sdp ? sheesh */
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Channel %s skipping state [%s][%d]\n",
</span><span class="cx">                                                          switch_channel_get_name(channel), nua_callstate_name(ss_state), status);
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_channelc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_channel.c (15265 => 15266)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_channel.c        2009-10-29 05:11:54 UTC (rev 15265)
+++ freeswitch/trunk/src/switch_channel.c        2009-10-29 05:47:17 UTC (rev 15266)
</span><span class="lines">@@ -1953,6 +1953,8 @@
</span><span class="cx"> SWITCH_DECLARE(switch_status_t) switch_channel_perform_mark_pre_answered(switch_channel_t *channel, const char *file, const char *func, int line)
</span><span class="cx"> {
</span><span class="cx">         switch_event_t *event;
</span><ins>+        const char *var = NULL;
+        char *app;
</ins><span class="cx">
</span><span class="cx">         if (!switch_channel_test_flag(channel, CF_EARLY_MEDIA)) {
</span><span class="cx">                 const char *uuid;
</span><span class="lines">@@ -1984,6 +1986,16 @@
</span><span class="cx">                         switch_mutex_unlock(channel->profile_mutex);
</span><span class="cx">                 }
</span><span class="cx">
</span><ins>+                if (((var = switch_channel_get_variable(channel, SWITCH_CHANNEL_EXECUTE_ON_PRE_ANSWER_VARIABLE)) ||
+                         (var = switch_channel_get_variable(channel, SWITCH_CHANNEL_EXECUTE_ON_MEDIA_VARIABLE))) && !zstr(var)) {
+                        char *arg = NULL;
+                        app = switch_core_session_strdup(channel->session, var);
+                        if ((arg = strchr(app, ' '))) {
+                                *arg++ = '\0';
+                        }
+                        switch_core_session_execute_application(channel->session, app, arg);
+                }
+
</ins><span class="cx">                 /* if we're the child of another channel and the other channel is in a blocking read they will never realize we have answered so send
</span><span class="cx">                  a SWITCH_SIG_BREAK to interrupt any blocking reads on that channel
</span><span class="cx">                  */
</span><span class="lines">@@ -2126,7 +2138,10 @@
</span><span class="cx">
</span><span class="cx">         switch_channel_set_variable(channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "ANSWER");
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, switch_channel_get_uuid(channel), SWITCH_LOG_NOTICE, "Channel [%s] has been answered\n", channel->name);
</span><del>-        if ((var = switch_channel_get_variable(channel, SWITCH_CHANNEL_EXECUTE_ON_ANSWER_VARIABLE)) && !zstr(var)) {
</del><ins>+
+        if (((var = switch_channel_get_variable(channel, SWITCH_CHANNEL_EXECUTE_ON_ANSWER_VARIABLE)) ||
+                 (!switch_channel_test_flag(channel, CF_EARLY_MEDIA) && (var = switch_channel_get_variable(channel, SWITCH_CHANNEL_EXECUTE_ON_MEDIA_VARIABLE))))
+                && !zstr(var)) {
</ins><span class="cx">                 char *arg = NULL;
</span><span class="cx">
</span><span class="cx">                 app = switch_core_session_strdup(channel->session, var);
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_ivr_originatec"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_ivr_originate.c (15265 => 15266)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_ivr_originate.c        2009-10-29 05:11:54 UTC (rev 15265)
+++ freeswitch/trunk/src/switch_ivr_originate.c        2009-10-29 05:47:17 UTC (rev 15266)
</span><span class="lines">@@ -114,6 +114,8 @@
</span><span class="cx">         int monitor_early_media_ring_total;
</span><span class="cx">         int cancel_timeout;
</span><span class="cx">         int continue_on_timeout;
</span><ins>+        int ringback_ok;
+        int sending_ringback;
</ins><span class="cx"> } originate_global_t;
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -240,7 +242,8 @@
</span><span class="cx">                 return 0;
</span><span class="cx">         }
</span><span class="cx">         for (i = 0; i < max; i++) {
</span><del>-                if (originate_status[i].peer_channel && switch_channel_get_state(originate_status[i].peer_channel) != CS_DESTROY && switch_channel_get_state(originate_status[i].peer_channel) != CS_REPORTING) {
</del><ins>+                if (originate_status[i].peer_channel && switch_channel_get_state(originate_status[i].peer_channel) != CS_DESTROY &&
+                        switch_channel_get_state(originate_status[i].peer_channel) != CS_REPORTING) {
</ins><span class="cx">                         if (originate_status[i].per_channel_delay_start) {
</span><span class="cx">                                 delayed_channels++;
</span><span class="cx">                         } else {
</span><span class="lines">@@ -251,26 +254,32 @@
</span><span class="cx">
</span><span class="cx">         if (active_channels == 0 && delayed_channels) {
</span><span class="cx">                 for (i = 0; i < max; i++) {
</span><del>-                        if ( originate_status[i].peer_channel && originate_status[i].per_channel_delay_start && (! delayed_min || delayed_min > originate_status[i].per_channel_delay_start) ) {
</del><ins>+                        if ( originate_status[i].peer_channel && originate_status[i].per_channel_delay_start &&
+                                 (! delayed_min || delayed_min > originate_status[i].per_channel_delay_start) ) {
</ins><span class="cx">                                 delayed_min = originate_status[i].per_channel_delay_start;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">                 early_exit_time = delayed_min - (uint32_t) elapsed;
</span><span class="cx">         }
</span><span class="cx">         for (i = 0; i < max; i++) {
</span><del>-                if (originate_status[i].peer_channel && originate_status[i].per_channel_delay_start && (elapsed > originate_status[i].per_channel_delay_start || active_channels == 0) ) {
</del><ins>+                if (originate_status[i].peer_channel && originate_status[i].per_channel_delay_start &&
+                        (elapsed > originate_status[i].per_channel_delay_start || active_channels == 0) ) {
</ins><span class="cx">                         if (active_channels == 0) {
</span><span class="cx">                                 if (originate_status[i].per_channel_timelimit_sec) {
</span><del>-                                        if (originate_status[i].per_channel_timelimit_sec > early_exit_time) /* IN theory this check is not needed ( should just be if !0 then -= with no else), if its not 0 it should always be greater.... */
</del><ins>+                                        if (originate_status[i].per_channel_timelimit_sec > early_exit_time) {
+                                                /* IN theory this check is not needed ( should just be if !0 then -= with no else), if its not 0 it should always be greater.... */
</ins><span class="cx">                                                 originate_status[i].per_channel_timelimit_sec -= early_exit_time;
</span><del>-                                        else
</del><ins>+                                        } else {
</ins><span class="cx">                                                 originate_status[i].per_channel_timelimit_sec = 1;
</span><ins>+                                        }
</ins><span class="cx">                                 }
</span><span class="cx">                                 if (originate_status[i].per_channel_progress_timelimit_sec) {
</span><del>-                                        if (originate_status[i].per_channel_progress_timelimit_sec > early_exit_time) /* IN theory this check is not needed ( should just be if !0 then -= with no else), if its not 0 it should always be greater.... */
</del><ins>+                                        if (originate_status[i].per_channel_progress_timelimit_sec > early_exit_time) {
+                                                /* IN theory this check is not needed ( should just be if !0 then -= with no else), if its not 0 it should always be greater.... */
</ins><span class="cx">                                                 originate_status[i].per_channel_progress_timelimit_sec -= early_exit_time;
</span><del>-                                        else
</del><ins>+                                        } else {
</ins><span class="cx">                                                 originate_status[i].per_channel_progress_timelimit_sec = 1;
</span><ins>+                                        }
</ins><span class="cx">                                 }
</span><span class="cx">                                 originate_status[i].per_channel_delay_start -= delayed_min;
</span><span class="cx">                         } else {
</span><span class="lines">@@ -357,6 +366,7 @@
</span><span class="cx">         switch_channel_t *caller_channel = NULL;
</span><span class="cx">         int pindex = -1;
</span><span class="cx">         char bug_key[256] = "";
</span><ins>+        int send_ringback = 0;
</ins><span class="cx">
</span><span class="cx">         oglobals->hups = 0;
</span><span class="cx">         oglobals->idx = IDX_NADA;
</span><span class="lines">@@ -382,20 +392,29 @@
</span><span class="cx">                                 originate_status[i].ring_ready = 1;
</span><span class="cx">                         }
</span><span class="cx">
</span><del>-                        if (!oglobals->ring_ready) {
-                                oglobals->ring_ready = 1;
-                                if (caller_channel && !oglobals->ignore_ring_ready) {
-                                        if (len == 1) {
-                                                switch_channel_pass_callee_id(originate_status[0].peer_channel, caller_channel);
</del><ins>+                        if (oglobals->sending_ringback == 1) {
+                                send_ringback++;
+                                pindex = (uint32_t) i;
+                        } else {
+                                if (!oglobals->ring_ready) {
+                                        oglobals->ring_ready = 1;
+                                        if (caller_channel && !oglobals->ignore_ring_ready) {
+                                                if (len == 1) {
+                                                        switch_channel_pass_callee_id(originate_status[0].peer_channel, caller_channel);
+                                                }
+                                                switch_channel_ring_ready(caller_channel);
+                                                oglobals->sent_ring = 1;
</ins><span class="cx">                                         }
</span><del>-                                        switch_channel_ring_ready(caller_channel);
-                                        oglobals->sent_ring = 1;
</del><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 if (switch_channel_test_flag(originate_status[i].peer_channel, CF_EARLY_MEDIA)) {
</span><del>-                        if (!oglobals->sent_ring && oglobals->ignore_early_media == 2 && len == 1 && caller_channel && !oglobals->ignore_ring_ready) {
</del><ins>+
+                        if (oglobals->sending_ringback == 1) {
+                                send_ringback++;
+                                pindex = (uint32_t) i;
+                        } else if (!oglobals->sent_ring && oglobals->ignore_early_media == 2 && len == 1 && caller_channel && !oglobals->ignore_ring_ready) {
</ins><span class="cx">                                 switch_channel_pass_callee_id(originate_status[0].peer_channel, caller_channel);
</span><span class="cx">                                 switch_channel_ring_ready(caller_channel);
</span><span class="cx">                                 oglobals->sent_ring = 1;
</span><span class="lines">@@ -527,12 +546,13 @@
</span><span class="cx">                                                 if (var_total) {
</span><span class="cx">                                                         int tmp = atoi(var_total);
</span><span class="cx">                                                         if (tmp > 0 && tmp < 100) {
</span><del>-                                                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(originate_status[i].peer_session), SWITCH_LOG_DEBUG, "%s setting ring total to %d\n",
</del><ins>+                                                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(originate_status[i].peer_session), SWITCH_LOG_DEBUG,
+                                                                                                 "%s setting ring total to %d\n",
</ins><span class="cx">                                                                                                  switch_channel_get_name(originate_status[i].peer_channel), tmp);
</span><span class="cx">                                                                 oglobals->monitor_early_media_ring_total = tmp;
</span><span class="cx">                                                         }
</span><span class="cx">                                                 }
</span><del>-
</del><ins>+                                                
</ins><span class="cx">                                                 switch_safe_free(ring_data);
</span><span class="cx">                                         
</span><span class="cx">                                         }
</span><span class="lines">@@ -621,16 +641,21 @@
</span><span class="cx">                         if (switch_core_session_get_read_impl(originate_status[pindex].peer_session, &impl) == SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                                 switch_snprintf(tmp, sizeof(tmp), "%s@%uh@%ui", impl.iananame, impl.samples_per_second, impl.microseconds_per_packet / 1000);
</span><span class="cx">                                 switch_channel_set_variable(caller_channel, "absolute_codec_string", tmp);
</span><del>-                                switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "Setting codec string on %s to %s\n", switch_channel_get_name(caller_channel), tmp);
</del><ins>+                                switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "Setting codec string on %s to %s\n",
+                                                                 switch_channel_get_name(caller_channel), tmp);
</ins><span class="cx">                         } else {
</span><del>-                                switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(originate_status[pindex].peer_channel), SWITCH_LOG_WARNING, "Error inheriting codec. Channel %s has no read codec yet.\n",
</del><ins>+                                switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(originate_status[pindex].peer_channel), SWITCH_LOG_WARNING,
+                                                                 "Error inheriting codec. Channel %s has no read codec yet.\n",
</ins><span class="cx">                                                                  switch_channel_get_name(originate_status[pindex].peer_channel));
</span><span class="cx">                         }
</span><span class="cx">
</span><span class="cx">                 }
</span><span class="cx">         }
</span><del>-        
</del><span class="cx">
</span><ins>+        if (send_ringback) {
+                oglobals->sending_ringback++;        
+        }
+
</ins><span class="cx">         return rval;
</span><span class="cx">
</span><span class="cx"> }
</span><span class="lines">@@ -979,6 +1004,137 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx">
</span><ins>+static switch_status_t setup_ringback(originate_global_t *oglobals,
+                                                                         const char *ringback_data,
+                                                                         ringback_t *ringback,
+                                                                         switch_frame_t *write_frame,
+                                                                         switch_codec_t *write_codec)
+{
+        switch_status_t status = SWITCH_STATUS_SUCCESS;
+        switch_channel_t *caller_channel = switch_core_session_get_channel(oglobals->session);
+        switch_codec_t *read_codec = NULL;
+        char *tmp_data = NULL;
+
+        if (!ringback_data) {
+                switch_goto_status(SWITCH_STATUS_GENERR, end);
+        }
+
+        if (!switch_channel_test_flag(caller_channel, CF_ANSWERED)
+                && !switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)) {
+                if ((status = switch_channel_pre_answer(caller_channel)) != SWITCH_STATUS_SUCCESS) {
+                        switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "%s Media Establishment Failed.\n",
+                                                         switch_channel_get_name(caller_channel));
+                        switch_goto_status(SWITCH_STATUS_BREAK, end);
+                }
+        }
+
+        if (oglobals->session && (read_codec = switch_core_session_get_read_codec(oglobals->session))) {
+                if (switch_is_file_path(ringback_data)) {
+                        if (!(strrchr(ringback_data, '.') || strstr(ringback_data, SWITCH_URL_SEPARATOR))) {
+                                ringback->asis++;
+                        }
+                }
+                                
+                if (!ringback->asis) {
+                        if (switch_test_flag(read_codec, SWITCH_CODEC_FLAG_PASSTHROUGH)) {
+                                switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_WARNING, "%s Ringback not supported in passthrough codec mode.\n",
+                                                                 switch_channel_get_name(caller_channel));
+                                switch_goto_status(SWITCH_STATUS_GENERR, end);
+                        }
+
+                        if (switch_core_codec_init(write_codec,
+                                                                         "L16",
+                                                                         NULL,
+                                                                         read_codec->implementation->actual_samples_per_second,
+                                                                         read_codec->implementation->microseconds_per_packet / 1000,
+                                                                         1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
+                                                                         switch_core_session_get_pool(oglobals->session)) == SWITCH_STATUS_SUCCESS) {
+
+
+                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->session), SWITCH_LOG_DEBUG,
+                                                                 "Raw Codec Activation Success L16@%uhz 1 channel %dms\n",
+                                                                 read_codec->implementation->actual_samples_per_second,
+                                                                 read_codec->implementation->microseconds_per_packet / 1000);
+                                write_frame->codec = write_codec;
+                                write_frame->datalen = read_codec->implementation->decoded_bytes_per_packet;
+                                write_frame->samples = write_frame->datalen / 2;
+                                memset(write_frame->data, 255, write_frame->datalen);
+                                switch_core_session_set_read_codec(oglobals->session, write_codec);
+                        } else {
+                                switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_ERROR, "Codec Error!\n");
+                                switch_channel_hangup(caller_channel, SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE);
+                                read_codec = NULL;
+                                switch_goto_status(SWITCH_STATUS_BREAK, end);
+                        }
+                }        
+
+                oglobals->gen_ringback = 1;
+
+                if (switch_is_file_path(ringback_data)) {
+                        char *ext;
+
+                        if (ringback->asis) {
+                                write_frame->codec = read_codec;
+                                ext = read_codec->implementation->iananame;
+                                tmp_data = switch_mprintf("%s.%s", ringback_data, ext);
+                                ringback_data = tmp_data;
+                        }
+
+                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->session), SWITCH_LOG_DEBUG, "Play Ringback File [%s]\n", ringback_data);
+                        
+                        ringback->fhb.channels = read_codec->implementation->number_of_channels;
+                        ringback->fhb.samplerate = read_codec->implementation->actual_samples_per_second;
+                        if (switch_core_file_open(&ringback->fhb,
+                                                                         ringback_data,
+                                                                         read_codec->implementation->number_of_channels,
+                                                                         read_codec->implementation->actual_samples_per_second,
+                                                                         SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Playing File\n");
+                                switch_safe_free(tmp_data);
+                                switch_goto_status(SWITCH_STATUS_GENERR, end);
+                                //switch_goto_status(SWITCH_STATUS_FALSE, end);
+                        }
+                        ringback->fh = &ringback->fhb;
+
+                } else if (!strncasecmp(ringback_data, "silence", 7)) {
+                        const char *c = ringback_data + 7;
+                        if (*c == ':') {
+                                c++;
+                                if (c) {
+                                        ringback->silence = atoi(c);
+                                }
+                        }
+                        if (ringback->silence <= 0) {
+                                ringback->silence = 400;
+                        }
+                } else {
+                        switch_buffer_create_dynamic(&ringback->audio_buffer, 512, 1024, 0);
+                        switch_buffer_set_loops(ringback->audio_buffer, -1);
+
+                        teletone_init_session(&ringback->ts, 0, teletone_handler, ringback);
+                        ringback->ts.rate = read_codec->implementation->actual_samples_per_second;
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Play Ringback Tone [%s]\n", ringback_data);
+                        /* ringback->ts.debug = 1;
+                         ringback->ts.debug_stream = switch_core_get_console(); */
+                        
+                        if (teletone_run(&ringback->ts, ringback_data)) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Playing Tone\n");
+                                teletone_destroy_session(&ringback->ts);
+                                switch_buffer_destroy(&ringback->audio_buffer);
+                                switch_goto_status(SWITCH_STATUS_GENERR, end);
+                        }
+                }
+        }
+                        
+ end:
+
+        switch_safe_free(tmp_data);
+
+        return status;
+
+}
+
+
</ins><span class="cx"> #define peer_eligible(_peer) (_peer && !(switch_channel_test_flag(_peer, CF_TRANSFER) || \
</span><span class="cx">                                                                                  switch_channel_test_flag(_peer, CF_REDIRECT) || \
</span><span class="cx">                                                                                  switch_channel_test_flag(_peer, CF_BRIDGED) || \
</span><span class="lines">@@ -1018,7 +1174,6 @@
</span><span class="cx">         int32_t sleep_ms = 1000, try = 0, retries = 1;
</span><span class="cx">         switch_codec_t write_codec = { 0 };
</span><span class="cx">         switch_frame_t write_frame = { 0 };
</span><del>-        uint8_t pass = 0;
</del><span class="cx">         char *odata, *var;
</span><span class="cx">         switch_call_cause_t reason = SWITCH_CAUSE_NONE;
</span><span class="cx">         switch_call_cause_t force_reason = SWITCH_CAUSE_NONE;
</span><span class="lines">@@ -1027,7 +1182,6 @@
</span><span class="cx">         int var_block_count = 0;
</span><span class="cx">         char *e = NULL;
</span><span class="cx">         const char *ringback_data = NULL;
</span><del>-        switch_codec_t *read_codec = NULL;
</del><span class="cx">         switch_event_t *var_event = NULL;
</span><span class="cx">         uint8_t fail_on_single_reject = 0;
</span><span class="cx">         char *fail_on_single_reject_var = NULL;
</span><span class="lines">@@ -1040,6 +1194,8 @@
</span><span class="cx">         const char *cancel_key = NULL;
</span><span class="cx">         const char *holding = NULL;
</span><span class="cx">
</span><ins>+        oglobals.ringback_ok = 1;
+
</ins><span class="cx">         if (session) {
</span><span class="cx">                 const char *to_var;
</span><span class="cx">                 caller_channel = switch_core_session_get_channel(session);
</span><span class="lines">@@ -1065,7 +1221,8 @@
</span><span class="cx">                                         switch_ivr_media(switch_core_session_get_uuid(session), SMF_REBRIDGE);
</span><span class="cx">                                         switch_channel_set_flag(caller_channel, CF_PROXY_MODE);
</span><span class="cx">                                 } else {
</span><del>-                                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Channel is already up, delaying proxy mode 'till both legs are answered.\n");
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
+                                                                         "Channel is already up, delaying proxy mode 'till both legs are answered.\n");
</ins><span class="cx">                                         switch_channel_set_variable(caller_channel, "bypass_media_after_bridge", "true");
</span><span class="cx">                                         switch_channel_set_variable(caller_channel, SWITCH_BYPASS_MEDIA_VARIABLE, NULL);
</span><span class="cx">                                         switch_channel_clear_flag(caller_channel, CF_PROXY_MODE);
</span><span class="lines">@@ -1447,7 +1604,7 @@
</span><span class="cx">                         start = 0;
</span><span class="cx">                         read_frame = NULL;
</span><span class="cx">                         pool = NULL;
</span><del>-                        pass = 0;
</del><ins>+                        oglobals.ringback_ok = 1;
</ins><span class="cx">                         var = NULL;
</span><span class="cx">                         to = 0;
</span><span class="cx">                         oglobals.sent_ring = 0;
</span><span class="lines">@@ -1874,117 +2031,7 @@
</span><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="cx">
</span><del>-                        if (ringback_data && !switch_channel_test_flag(caller_channel, CF_ANSWERED)
-                                && !switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)) {
-                                if ((status = switch_channel_pre_answer(caller_channel)) != SWITCH_STATUS_SUCCESS) {
-                                        switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "%s Media Establishment Failed.\n", switch_channel_get_name(caller_channel));
-                                        goto done;
-                                }
-                        }
-
-                        if (oglobals.session && (read_codec = switch_core_session_get_read_codec(oglobals.session)) && ringback_data) {
-                                if (switch_is_file_path(ringback_data)) {
-                                        if (!(strrchr(ringback_data, '.') || strstr(ringback_data, SWITCH_URL_SEPARATOR))) {
-                                                ringback.asis++;
-                                        }
-                                }
-                                
-                                if (!ringback.asis) {
-                                        if ((pass = (uint8_t) switch_test_flag(read_codec, SWITCH_CODEC_FLAG_PASSTHROUGH))) {
-                                                ringback_data = NULL;
-                                                goto no_ringback;
-                                        }
-
-                                        if (switch_core_codec_init(&write_codec,
-                                                                                         "L16",
-                                                                                         NULL,
-                                                                                         read_codec->implementation->actual_samples_per_second,
-                                                                                         read_codec->implementation->microseconds_per_packet / 1000,
-                                                                                         1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
-                                                                                         switch_core_session_get_pool(oglobals.session)) == SWITCH_STATUS_SUCCESS) {
-
-
-                                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals.session), SWITCH_LOG_DEBUG,
-                                                                                 "Raw Codec Activation Success L16@%uhz 1 channel %dms\n",
-                                                                                 read_codec->implementation->actual_samples_per_second,
-                                                                                 read_codec->implementation->microseconds_per_packet / 1000);
-                                                write_frame.codec = &write_codec;
-                                                write_frame.datalen = read_codec->implementation->decoded_bytes_per_packet;
-                                                write_frame.samples = write_frame.datalen / 2;
-                                                memset(write_frame.data, 255, write_frame.datalen);
-                                                switch_core_session_set_read_codec(oglobals.session, &write_codec);
-                                        } else {
-                                                switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_ERROR, "Codec Error!\n");
-                                                switch_channel_hangup(caller_channel, SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE);
-                                                read_codec = NULL;
-                                                goto done;
-                                        }
-                                }        
-
-                                if (ringback_data) {
-                                        char *tmp_data = NULL;
-
-                                        oglobals.gen_ringback = 1;
-
-                                        if (switch_is_file_path(ringback_data)) {
-                                                char *ext;
-
-                                                if (ringback.asis) {
-                                                        write_frame.codec = read_codec;
-                                                        ext = read_codec->implementation->iananame;
-                                                        tmp_data = switch_mprintf("%s.%s", ringback_data, ext);
-                                                        ringback_data = tmp_data;
-                                                }
-
-                                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals.session), SWITCH_LOG_DEBUG, "Play Ringback File [%s]\n", ringback_data);
-
-                                                ringback.fhb.channels = read_codec->implementation->number_of_channels;
-                                                ringback.fhb.samplerate = read_codec->implementation->actual_samples_per_second;
-                                                if (switch_core_file_open(&ringback.fhb,
-                                                                                                 ringback_data,
-                                                                                                 read_codec->implementation->number_of_channels,
-                                                                                                 read_codec->implementation->actual_samples_per_second,
-                                                                                                 SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
-                                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Playing File\n");
-                                                        switch_safe_free(tmp_data);
-                                                        goto notready;
-                                                }
-                                                ringback.fh = &ringback.fhb;
-
-                                        } else if (!strncasecmp(ringback_data, "silence", 7)) {
-                                                const char *c = ringback_data + 7;
-                                                if (*c == ':') {
-                                                        c++;
-                                                        if (c) {
-                                                                ringback.silence = atoi(c);
-                                                        }
-                                                }
-                                                if (ringback.silence <= 0) {
-                                                        ringback.silence = 400;
-                                                }
-                                        } else {
-                                                switch_buffer_create_dynamic(&ringback.audio_buffer, 512, 1024, 0);
-                                                switch_buffer_set_loops(ringback.audio_buffer, -1);
-
-                                                teletone_init_session(&ringback.ts, 0, teletone_handler, &ringback);
-                                                ringback.ts.rate = read_codec->implementation->actual_samples_per_second;
-                                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Play Ringback Tone [%s]\n", ringback_data);
-                                                /* ringback.ts.debug = 1;
-                                                 ringback.ts.debug_stream = switch_core_get_console();
-                                                */
-                                                if (teletone_run(&ringback.ts, ringback_data)) {
-                                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Playing Tone\n");
-                                                        teletone_destroy_session(&ringback.ts);
-                                                        switch_buffer_destroy(&ringback.audio_buffer);
-                                                        ringback_data = NULL;
-                                                }
-                                        }
-                                        switch_safe_free(tmp_data);
-                                }
-                        }
</del><span class="cx">                         
</span><del>-                        
-                no_ringback:
</del><span class="cx">
</span><span class="cx"> #if 0
</span><span class="cx">                         /* changing behaviour ignore_early_media=true must also be explicitly set for previous behaviour */
</span><span class="lines">@@ -1993,6 +2040,12 @@
</span><span class="cx">                         }
</span><span class="cx"> #endif
</span><span class="cx">
</span><ins>+                        if (ringback_data) {
+                                oglobals.sending_ringback = 1;
+                        } else {
+                                oglobals.ringback_ok = 0;
+                        }
+                        
</ins><span class="cx">                         while ((!caller_channel || switch_channel_ready(caller_channel) || switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) &&
</span><span class="cx">                                  check_channel_status(&oglobals, originate_status, and_argc)) {
</span><span class="cx">                                 time_t elapsed = switch_epoch_time_now(NULL) - start;
</span><span class="lines">@@ -2021,7 +2074,7 @@
</span><span class="cx">                                                                 continue;
</span><span class="cx">                                                         }
</span><span class="cx">                                                         pchannel = switch_core_session_get_channel(originate_status[i].peer_session);
</span><del>-
</del><ins>+                                                        
</ins><span class="cx">                                                         if (switch_channel_down(pchannel)) {
</span><span class="cx">                                                                 cause_str = switch_channel_cause2str(switch_channel_get_cause(pchannel));
</span><span class="cx">                                                                 if (switch_stristr(cause_str, fail_on_single_reject_var)) {
</span><span class="lines">@@ -2042,7 +2095,7 @@
</span><span class="cx">                                         !switch_channel_test_flag(caller_channel, CF_PROXY_MODE) &&
</span><span class="cx">                                         !switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA) &&
</span><span class="cx">                                         !switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE) &&
</span><del>-                                        (ringback_data
</del><ins>+                                        (oglobals.ringback_ok
</ins><span class="cx">                                          || (switch_channel_test_flag(caller_channel, CF_ANSWERED) || switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)))) {
</span><span class="cx">                                         
</span><span class="cx">                                         switch_status_t tstatus = SWITCH_STATUS_SUCCESS;
</span><span class="lines">@@ -2072,7 +2125,30 @@
</span><span class="cx">                                                 read_frame = NULL;
</span><span class="cx">                                         }
</span><span class="cx">                                         
</span><del>-                                        if ((oglobals.ring_ready || oglobals.instant_ringback) && read_frame && !pass) {
</del><ins>+                                        if (oglobals.ringback_ok && (oglobals.ring_ready || oglobals.instant_ringback || oglobals.sending_ringback > 1)) {
+                                                if (oglobals.ringback_ok == 1) {
+                                                        switch_status_t rst = setup_ringback(&oglobals, ringback_data, &ringback, &write_frame, &write_codec);
+
+                                                        switch (rst) {
+                                                        case SWITCH_STATUS_SUCCESS:
+                                                                oglobals.ringback_ok++;
+                                                                break;
+                                                        case SWITCH_STATUS_FALSE:
+                                                                goto notready;
+                                                                break;
+                                                        case SWITCH_STATUS_BREAK:
+                                                                goto done;
+                                                                break;
+                                                        default:
+                                                                ringback_data = NULL;
+                                                                oglobals.ringback_ok = 0;
+                                                                oglobals.sending_ringback = 0;
+                                                                break;
+                                                        }
+
+                                                        continue;
+                                                }
+                                                        
</ins><span class="cx">                                                 if (ringback.fh) {
</span><span class="cx">                                                         switch_size_t mlen, olen;
</span><span class="cx">                                                         unsigned int pos = 0;
</span><span class="lines">@@ -2347,7 +2423,8 @@
</span><span class="cx">                                 }
</span><span class="cx">
</span><span class="cx">                                 if (status != SWITCH_STATUS_SUCCESS) {
</span><del>-                                        switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(peer_channel), SWITCH_LOG_DEBUG, "%s Media Establishment Failed.\n", switch_channel_get_name(caller_channel));
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(peer_channel), SWITCH_LOG_DEBUG, "%s Media Establishment Failed.\n",
+                                                                         switch_channel_get_name(caller_channel));
</ins><span class="cx">                                         switch_channel_hangup(peer_channel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION);
</span><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="lines">@@ -2383,7 +2460,8 @@
</span><span class="cx">                                                 switch_process_import(oglobals.session, peer_channel, "import");
</span><span class="cx">                                         }
</span><span class="cx">                                 }
</span><del>-                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals.session), SWITCH_LOG_DEBUG, "Originate Resulted in Success: [%s]\n", switch_channel_get_name(peer_channel));
</del><ins>+                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals.session), SWITCH_LOG_DEBUG, "Originate Resulted in Success: [%s]\n",
+                                                                 switch_channel_get_name(peer_channel));
</ins><span class="cx">                                 *cause = SWITCH_CAUSE_SUCCESS;
</span><span class="cx">
</span><span class="cx">                         } else {
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>