<!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][17383] </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=17383">17383</a></dd>
<dt>Author</dt> <dd>mrene</dd>
<dt>Date</dt> <dd>2010-04-23 16:49:44 -0500 (Fri, 23 Apr 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Tweak bridge_early_media to support passthrough codecs</pre>

<h3>Modified Paths</h3>
<ul>
<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="freeswitchtrunksrcswitch_ivr_originatec"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_ivr_originate.c (17382 => 17383)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_ivr_originate.c        2010-04-23 20:53:30 UTC (rev 17382)
+++ freeswitch/trunk/src/switch_ivr_originate.c        2010-04-23 21:49:44 UTC (rev 17383)
</span><span class="lines">@@ -372,6 +372,38 @@
</span><span class="cx">         return SWITCH_FALSE;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void inherit_codec(switch_channel_t *caller_channel, switch_core_session_t *session)
+{
+        const char *var = switch_channel_get_variable(caller_channel, &quot;inherit_codec&quot;);
+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        
+        if (switch_true(var)) {
+                switch_codec_implementation_t impl = { 0 };
+                switch_codec_implementation_t video_impl = { 0 };
+                char tmp[128] = &quot;&quot;;
+
+
+                if (switch_core_session_get_read_impl(session, &amp;impl) == SWITCH_STATUS_SUCCESS) {
+                        if (switch_core_session_get_video_read_impl(session, &amp;impl) == SWITCH_STATUS_SUCCESS) {
+                                switch_snprintf(tmp, sizeof(tmp), &quot;%s@%uh@%ui,%s&quot;,
+                                                                impl.iananame, impl.samples_per_second, impl.microseconds_per_packet / 1000,
+                                                                video_impl.iananame);
+                        } else {
+                                switch_snprintf(tmp, sizeof(tmp), &quot;%s@%uh@%ui&quot;,
+                                                                impl.iananame, impl.samples_per_second, impl.microseconds_per_packet / 1000);
+                        }
+                        switch_channel_set_variable(caller_channel, &quot;absolute_codec_string&quot;, tmp);
+                        switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, &quot;Setting codec string on %s to %s\n&quot;,
+                                                          switch_channel_get_name(caller_channel), tmp);
+                } else {
+                        switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_WARNING,
+                                                          &quot;Error inheriting codec.  Channel %s has no read codec yet.\n&quot;,
+                                                          switch_channel_get_name(channel));
+                }
+
+        }
+}
+
</ins><span class="cx"> static uint8_t check_channel_status(originate_global_t *oglobals, originate_status_t *originate_status, uint32_t len)
</span><span class="cx"> {
</span><span class="cx"> 
</span><span class="lines">@@ -671,32 +703,7 @@
</span><span class="cx">   end:
</span><span class="cx"> 
</span><span class="cx">         if (pindex &gt; -1 &amp;&amp; caller_channel &amp;&amp; switch_channel_ready(caller_channel) &amp;&amp; !switch_channel_media_ready(caller_channel)) {
</span><del>-                const char *var = switch_channel_get_variable(caller_channel, &quot;inherit_codec&quot;);
-                if (switch_true(var)) {
-                        switch_codec_implementation_t impl = { 0 };
-                        switch_codec_implementation_t video_impl = { 0 };
-                        char tmp[128] = &quot;&quot;;
-
-
-                        if (switch_core_session_get_read_impl(originate_status[pindex].peer_session, &amp;impl) == SWITCH_STATUS_SUCCESS) {
-                                if (switch_core_session_get_video_read_impl(originate_status[pindex].peer_session, &amp;impl) == SWITCH_STATUS_SUCCESS) {
-                                        switch_snprintf(tmp, sizeof(tmp), &quot;%s@%uh@%ui,%s&quot;,
-                                                                        impl.iananame, impl.samples_per_second, impl.microseconds_per_packet / 1000,
-                                                                        video_impl.iananame);
-                                } else {
-                                        switch_snprintf(tmp, sizeof(tmp), &quot;%s@%uh@%ui&quot;,
-                                                                        impl.iananame, impl.samples_per_second, impl.microseconds_per_packet / 1000);
-                                }
-                                switch_channel_set_variable(caller_channel, &quot;absolute_codec_string&quot;, tmp);
-                                switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, &quot;Setting codec string on %s to %s\n&quot;,
-                                                                  switch_channel_get_name(caller_channel), tmp);
-                        } else {
-                                switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(originate_status[pindex].peer_channel), SWITCH_LOG_WARNING,
-                                                                  &quot;Error inheriting codec.  Channel %s has no read codec yet.\n&quot;,
-                                                                  switch_channel_get_name(originate_status[pindex].peer_channel));
-                        }
-
-                }
</del><ins>+                inherit_codec(caller_channel, originate_status[pindex].peer_session);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (send_ringback) {
</span><span class="lines">@@ -1051,7 +1058,7 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-static switch_status_t setup_ringback(originate_global_t *oglobals,
</del><ins>+static switch_status_t setup_ringback(originate_global_t *oglobals, originate_status_t *originate_status, int len,
</ins><span class="cx">                                                                           const char *ringback_data, ringback_t *ringback, switch_frame_t *write_frame, switch_codec_t *write_codec)
</span><span class="cx"> {
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_SUCCESS;
</span><span class="lines">@@ -1061,6 +1068,10 @@
</span><span class="cx"> 
</span><span class="cx">         if (!switch_channel_test_flag(caller_channel, CF_ANSWERED)
</span><span class="cx">                 &amp;&amp; !switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)) {
</span><ins>+                if (oglobals-&gt;bridge_early_media &gt; -1  &amp;&amp; len == 1 &amp;&amp; originate_status[0].peer_session &amp;&amp; 
+                        switch_channel_media_ready(originate_status[0].peer_channel)) {                                
+                        inherit_codec(caller_channel, originate_status[0].peer_session);
+                }
</ins><span class="cx">                 if ((status = switch_channel_pre_answer(caller_channel)) != SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, &quot;%s Media Establishment Failed.\n&quot;,
</span><span class="cx">                                                           switch_channel_get_name(caller_channel));
</span><span class="lines">@@ -1073,6 +1084,25 @@
</span><span class="cx">                         if (!(strrchr(ringback_data, '.') || strstr(ringback_data, SWITCH_URL_SEPARATOR))) {
</span><span class="cx">                                 ringback-&gt;asis++;
</span><span class="cx">                         }
</span><ins>+                } else if (oglobals-&gt;bridge_early_media &gt; -1 &amp;&amp; zstr(ringback_data) &amp;&amp; len == 1 &amp;&amp; originate_status[0].peer_session) {
+                        switch_codec_implementation_t read_impl = { 0 }, write_impl = { 0 };
+                        
+                        if (switch_channel_ready(originate_status[0].peer_channel)
+                                &amp;&amp; switch_core_session_get_read_impl(originate_status[0].peer_session, &amp;read_impl) == SWITCH_STATUS_SUCCESS
+                                &amp;&amp; switch_core_session_get_write_impl(oglobals-&gt;session, &amp;write_impl) == SWITCH_STATUS_SUCCESS) {
+                                if (read_impl.impl_id == write_impl.impl_id &amp;&amp; 
+                                        read_impl.microseconds_per_packet == write_impl.microseconds_per_packet &amp;&amp;
+                                        read_impl.actual_samples_per_second == write_impl.actual_samples_per_second) {
+                                                ringback-&gt;asis++;
+                                                write_frame-&gt;codec = switch_core_session_get_write_codec(originate_status[0].peer_session);
+                                                write_frame-&gt;datalen = write_frame-&gt;codec-&gt;implementation-&gt;decoded_bytes_per_packet;
+                                                switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, &quot;bridge_early_media: passthrough enabled\n&quot;);
+                                        } else {
+                                                switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, &quot;bridge_early_media: codecs don't match (%s@%uh@%di / %s@%uh@%di)\n&quot;,
+                                                        read_impl.iananame, read_impl.actual_samples_per_second, read_impl.microseconds_per_packet / 1000, 
+                                                        write_impl.iananame, write_impl.actual_samples_per_second, write_impl.microseconds_per_packet / 1000);
+                                        }
+                        }
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 if (!ringback-&gt;asis) {
</span><span class="lines">@@ -1553,6 +1583,7 @@
</span><span class="cx">         switch_mutex_t *mutex;
</span><span class="cx">         switch_buffer_t *buffer;
</span><span class="cx">         int ready;
</span><ins>+        ringback_t *ringback;
</ins><span class="cx"> };
</span><span class="cx"> typedef struct early_state early_state_t;
</span><span class="cx"> 
</span><span class="lines">@@ -1591,38 +1622,52 @@
</span><span class="cx">                                         answered++;
</span><span class="cx">                                 }
</span><span class="cx"> 
</span><del>-                                if (!switch_core_codec_ready((&amp;read_codecs[i]))) {
-                                        read_codec = switch_core_session_get_read_codec(session);
</del><ins>+                                if (!state-&gt;ringback-&gt;asis) {
+                                        if (!switch_core_codec_ready((&amp;read_codecs[i]))) {
+                                                read_codec = switch_core_session_get_read_codec(session);
</ins><span class="cx"> 
</span><del>-                                        if (switch_core_codec_init(&amp;read_codecs[i],
-                                                                                           &quot;L16&quot;,
-                                                                                           NULL,
-                                                                                           read_codec-&gt;implementation-&gt;actual_samples_per_second,
-                                                                                           read_codec-&gt;implementation-&gt;microseconds_per_packet / 1000,
-                                                                                           1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
-                                                                                           switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
-                                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, &quot;Codec Error!\n&quot;);
-                                        } else {
-                                                switch_core_session_set_read_codec(session, &amp;read_codecs[i]);
</del><ins>+                                                if (switch_core_codec_init(&amp;read_codecs[i],
+                                                                                                   &quot;L16&quot;,
+                                                                                                   NULL,
+                                                                                                   read_codec-&gt;implementation-&gt;actual_samples_per_second,
+                                                                                                   read_codec-&gt;implementation-&gt;microseconds_per_packet / 1000,
+                                                                                                   1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
+                                                                                                   switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
+                                                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, &quot;Codec Error!\n&quot;);
+                                                } else {
+                                                        switch_core_session_set_read_codec(session, &amp;read_codecs[i]);
+                                                }
</ins><span class="cx">                                         }
</span><del>-                                }
-                                status = switch_core_session_read_frame(session, &amp;read_frame, SWITCH_IO_FLAG_NONE, 0);
-                                if (SWITCH_READ_ACCEPTABLE(status)) {
-                                        data = (int16_t *) read_frame-&gt;data;
-                                        if (datalen &lt; read_frame-&gt;datalen) {
</del><ins>+                                        status = switch_core_session_read_frame(session, &amp;read_frame, SWITCH_IO_FLAG_NONE, 0);
+                                        if (SWITCH_READ_ACCEPTABLE(status)) {
+                                                data = (int16_t *) read_frame-&gt;data;
+                                                if (datalen &lt; read_frame-&gt;datalen) {
+                                                        datalen = read_frame-&gt;datalen;
+                                                }
+                                                for (x = 0; x &lt; (int) read_frame-&gt;datalen / 2; x++) {
+                                                        sample = data[x] + mux_data[x];
+                                                        switch_normalize_to_16bit(sample);
+                                                        mux_data[x] = (int16_t) sample;
+                                                }
+                                        }
+                                } else {
+                                        status = switch_core_session_read_frame(session, &amp;read_frame, SWITCH_IO_FLAG_NONE, 0);
+                                        if (SWITCH_READ_ACCEPTABLE(status)) {
</ins><span class="cx">                                                 datalen = read_frame-&gt;datalen;
</span><span class="cx">                                         }
</span><del>-                                        for (x = 0; x &lt; (int) read_frame-&gt;datalen / 2; x++) {
-                                                sample = data[x] + mux_data[x];
-                                                switch_normalize_to_16bit(sample);
-                                                mux_data[x] = (int16_t) sample;
-                                        }
-
</del><ins>+                                        break;
</ins><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-                if (datalen) {
</del><ins>+                
+                if (state-&gt;ringback-&gt;asis &amp;&amp; datalen) {
+                        uint16_t flen = datalen;
</ins><span class="cx">                         switch_mutex_lock(state-&gt;mutex);
</span><ins>+                        switch_buffer_write(state-&gt;buffer, &amp;flen, sizeof(uint16_t));
+                        switch_buffer_write(state-&gt;buffer, read_frame-&gt;data, datalen);
+                        switch_mutex_unlock(state-&gt;mutex);
+                } else if (datalen) {
+                        switch_mutex_lock(state-&gt;mutex);
</ins><span class="cx">                         switch_buffer_write(state-&gt;buffer, mux_data, datalen);
</span><span class="cx">                         switch_mutex_unlock(state-&gt;mutex);
</span><span class="cx">                 }
</span><span class="lines">@@ -2758,9 +2803,10 @@
</span><span class="cx">                                         if (oglobals.ringback_ok &amp;&amp; (oglobals.ring_ready || oglobals.instant_ringback ||
</span><span class="cx">                                                                                                  oglobals.sending_ringback &gt; 1 || oglobals.bridge_early_media &gt; -1)) {
</span><span class="cx">                                                 if (oglobals.ringback_ok == 1) {
</span><del>-                                                        switch_status_t rst = setup_ringback(&amp;oglobals, ringback_data, &amp;ringback, &amp;write_frame, &amp;write_codec);
-
-
</del><ins>+                                                        switch_status_t rst;
+                                                        
+                                                        rst = setup_ringback(&amp;oglobals, originate_status, and_argc, ringback_data, &amp;ringback, &amp;write_frame, &amp;write_codec);
+                                                        
</ins><span class="cx">                                                         if (oglobals.bridge_early_media &gt; -1) {
</span><span class="cx">                                                                 switch_threadattr_t *thd_attr = NULL;
</span><span class="cx">                                                                 switch_threadattr_create(&amp;thd_attr, switch_core_session_get_pool(session));
</span><span class="lines">@@ -2768,6 +2814,7 @@
</span><span class="cx">                                                                 early_state.oglobals = &amp;oglobals;
</span><span class="cx">                                                                 early_state.originate_status = originate_status;
</span><span class="cx">                                                                 early_state.ready = 1;
</span><ins>+                                                                early_state.ringback = &amp;ringback;
</ins><span class="cx">                                                                 switch_mutex_init(&amp;early_state.mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
</span><span class="cx">                                                                 switch_buffer_create_dynamic(&amp;early_state.buffer, 1024, 1024, 0);
</span><span class="cx">                                                                 switch_thread_create(&amp;oglobals.ethread, thd_attr, early_thread_run, &amp;early_state, switch_core_session_get_pool(session));
</span><span class="lines">@@ -2794,12 +2841,24 @@
</span><span class="cx">                                                         continue;
</span><span class="cx">                                                 }
</span><span class="cx"> 
</span><del>-                                                if (oglobals.bridge_early_media &gt; -1) {
</del><ins>+                                                if (oglobals.bridge_early_media &gt; -1) {                                                        
</ins><span class="cx">                                                         write_frame.datalen = 0;
</span><span class="cx">                                                         switch_mutex_lock(early_state.mutex);
</span><del>-                                                        if (switch_buffer_inuse(early_state.buffer) &gt;= write_frame.codec-&gt;implementation-&gt;decoded_bytes_per_packet) {
-                                                                write_frame.datalen = switch_buffer_read(early_state.buffer, write_frame.data,
-                                                                                                                                                 write_frame.codec-&gt;implementation-&gt;decoded_bytes_per_packet);
</del><ins>+                                                        if (ringback.asis) {
+                                                                uint16_t mlen;
+                                                                switch_size_t buflen = switch_buffer_inuse(early_state.buffer);
+                                                                if (buflen &gt; sizeof(uint16_t)) {
+                                                                        switch_buffer_peek(early_state.buffer, &amp;mlen, sizeof(uint16_t));
+                                                                        if (buflen &gt;= (mlen + sizeof(uint16_t))) {
+                                                                                switch_buffer_toss(early_state.buffer, sizeof(uint16_t));
+                                                                                write_frame.datalen = switch_buffer_read(early_state.buffer, write_frame.data, mlen);
+                                                                        }
+                                                                }
+                                                        } else {
+                                                                if (switch_buffer_inuse(early_state.buffer) &gt;= write_frame.codec-&gt;implementation-&gt;decoded_bytes_per_packet) {
+                                                                        write_frame.datalen = switch_buffer_read(early_state.buffer, write_frame.data,
+                                                                                                                                                         write_frame.codec-&gt;implementation-&gt;decoded_bytes_per_packet);
+                                                                }
</ins><span class="cx">                                                         }
</span><span class="cx">                                                         switch_mutex_unlock(early_state.mutex);
</span><span class="cx">                                                 } else if (ringback.fh) {
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>