<!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][16325] </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=16325">16325</a></dd>
<dt>Author</dt> <dd>anthm</dd>
<dt>Date</dt> <dd>2010-01-15 15:10:31 -0600 (Fri, 15 Jan 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>add early media muxing from long thread on mailing list =/</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 (16324 => 16325)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_ivr_originate.c        2010-01-15 21:09:51 UTC (rev 16324)
+++ freeswitch/trunk/src/switch_ivr_originate.c        2010-01-15 21:10:31 UTC (rev 16325)
</span><span class="lines">@@ -117,6 +117,7 @@
</span><span class="cx">         int ringback_ok;
</span><span class="cx">         int sending_ringback;
</span><span class="cx">         int bridge_early_media;
</span><ins>+        switch_thread_t *ethread;
</ins><span class="cx"> } originate_global_t;
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -1518,6 +1519,91 @@
</span><span class="cx">
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+struct early_state {
+        originate_global_t *oglobals;
+        originate_status_t *originate_status;
+        switch_mutex_t *mutex;
+        switch_buffer_t *buffer;
+        int ready;
+};
+typedef struct early_state early_state_t;
+
+
+static void *SWITCH_THREAD_FUNC early_thread_run(switch_thread_t *thread, void *obj)
+{
+        early_state_t *state = (early_state_t *) obj;
+        originate_status_t originate_status[MAX_PEERS] = { { 0 }};
+        int16_t mux_data[SWITCH_RECOMMENDED_BUFFER_SIZE/2] = { 0 };
+        int32_t sample;
+        switch_core_session_t *session;
+        switch_codec_t *read_codec, read_codecs[MAX_PEERS] = { { 0 } };
+        int i,x;
+        int16_t *data;
+        uint32_t datalen = 0;
+        switch_status_t status;
+        switch_frame_t *read_frame;
+        
+        for(i = 0; i < MAX_PEERS && (session = state->originate_status[i].peer_session); i++) {
+                originate_status[i].peer_session = session;
+                switch_core_session_read_lock(session);
+        }
+
+        while (state->ready) {
+                datalen = 0;
+                memset(mux_data, 0, sizeof(mux_data));
+                
+                for(i = 0; i < MAX_PEERS && (session = originate_status[i].peer_session); i++) {
+                        switch_channel_t *channel = switch_core_session_get_channel(session);
+                        if (switch_channel_media_ready(channel)) {
+                                if (!switch_core_codec_ready((&read_codecs[i]))) {
+                                        read_codec = switch_core_session_get_read_codec(session);
+                                        
+                                        if (switch_core_codec_init(&read_codecs[i],
+                                                                                         "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(session)) != SWITCH_STATUS_SUCCESS) {
+                                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec Error!\n");
+                                        } else {
+                                                switch_core_session_set_read_codec(session, &read_codecs[i]);
+                                        }
+                                }
+                                status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
+                                if (SWITCH_READ_ACCEPTABLE(status)) {
+                                        data = (int16_t *) read_frame->data;
+                                        if (datalen < read_frame->datalen) {
+                                                datalen = read_frame->datalen;
+                                        }
+                                        for (x = 0; x < read_frame->datalen / 2; x++) {
+                                                sample = data[x] + mux_data[x];
+                                                switch_normalize_to_16bit(sample);
+                                                mux_data[x] = sample;
+                                        }
+                                        
+                                }
+                        }
+                }
+                if (datalen) {
+                        switch_mutex_lock(state->mutex);
+                        switch_buffer_write(state->buffer, mux_data, datalen);
+                        switch_mutex_unlock(state->mutex);
+                }
+        }
+
+
+        for(i = 0; i < MAX_PEERS && (session = originate_status[i].peer_session); i++) {
+                if (switch_core_codec_ready((&read_codecs[i]))) {
+                        switch_core_codec_destroy(&read_codecs[i]);
+                }
+                switch_core_session_reset(session, SWITCH_FALSE, SWITCH_TRUE);
+                switch_core_session_rwunlock(session);
+        }
+
+        return NULL;
+}
+
</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">@@ -1579,8 +1665,8 @@
</span><span class="cx">         const char *holding = NULL;
</span><span class="cx">         const char *soft_holding = NULL;
</span><span class="cx">         const char *export_vars = NULL;
</span><ins>+        early_state_t early_state = { 0 };
</ins><span class="cx">
</span><del>-
</del><span class="cx">         if (strstr(bridgeto, SWITCH_ENT_ORIGINATE_DELIM)) {
</span><span class="cx">                 return switch_ivr_enterprise_originate(session, bleg, cause, bridgeto, timelimit_sec, table, cid_name_override, cid_num_override,
</span><span class="cx">                                                                                  caller_profile_override, ovars, flags);
</span><span class="lines">@@ -2580,11 +2666,26 @@
</span><span class="cx">                                                 read_frame = NULL;
</span><span class="cx">                                         }
</span><span class="cx">
</span><ins>+                                        
</ins><span class="cx">                                         if (oglobals.ringback_ok && (oglobals.ring_ready || oglobals.instant_ringback ||
</span><span class="cx">                                                                                                  oglobals.sending_ringback > 1 || oglobals.bridge_early_media > -1)) {
</span><span class="cx">                                                 if (oglobals.ringback_ok == 1) {
</span><span class="cx">                                                         switch_status_t rst = setup_ringback(&oglobals, ringback_data, &ringback, &write_frame, &write_codec);
</span><span class="cx">
</span><ins>+                                                        
+                                                        if (oglobals.bridge_early_media > -1) {
+                                                                switch_threadattr_t *thd_attr = NULL;
+                                                                switch_threadattr_create(&thd_attr, switch_core_session_get_pool(session));
+                                                                switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
+                                                                early_state.oglobals = &oglobals;
+                                                                early_state.originate_status = originate_status;
+                                                                early_state.ready = 1;
+                                                                switch_mutex_init(&early_state.mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
+                                                                switch_buffer_create_dynamic(&early_state.buffer, 1024, 1024, 0);
+                                                                switch_thread_create(&oglobals.ethread, thd_attr, early_thread_run, &early_state, switch_core_session_get_pool(session));
+                                                        }
+                                                        
+                                                        
</ins><span class="cx">                                                         switch (rst) {
</span><span class="cx">                                                         case SWITCH_STATUS_SUCCESS:
</span><span class="cx">                                                                 oglobals.ringback_ok++;
</span><span class="lines">@@ -2606,24 +2707,13 @@
</span><span class="cx">                                                 }
</span><span class="cx">                                                         
</span><span class="cx">                                                 if (oglobals.bridge_early_media > -1) {
</span><del>-                                                        switch_channel_t *b_channel = originate_status[oglobals.bridge_early_media].peer_channel;
-                                                        switch_core_session_t *b_session = originate_status[oglobals.bridge_early_media].peer_session;
-                                                        switch_status_t b_status = SWITCH_STATUS_FALSE;
-                                                        switch_frame_t *b_frame = NULL;
-                                                        
-                                                        if (b_channel && b_session) {
-                                                                b_status = switch_core_session_read_frame(b_session, &b_frame, SWITCH_IO_FLAG_NONE, 0);
</del><ins>+                                                        write_frame.datalen = 0;
+                                                        switch_mutex_lock(early_state.mutex);
+                                                        if (switch_buffer_inuse(early_state.buffer) >= write_frame.codec->implementation->decoded_bytes_per_packet) {
+                                                                write_frame.datalen = switch_buffer_read(early_state.buffer, write_frame.data,
+                                                                                                                                                 write_frame.codec->implementation->decoded_bytes_per_packet);
</ins><span class="cx">                                                         }
</span><del>-                                                        
-                                                        if (!SWITCH_READ_ACCEPTABLE(status)) {
-                                                                oglobals.bridge_early_media = -1;
-                                                        } else {
-                                                                if (switch_core_session_write_frame(oglobals.session, b_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
-                                                                        oglobals.bridge_early_media = -1;
-                                                                }
-                                                                continue;
-                                                        }
-                                                        
</del><ins>+                                                        switch_mutex_unlock(early_state.mutex);
</ins><span class="cx">                                                 } else if (ringback.fh) {
</span><span class="cx">                                                         switch_size_t mlen, olen;
</span><span class="cx">                                                         unsigned int pos = 0;
</span><span class="lines">@@ -2671,7 +2761,7 @@
</span><span class="cx">                                                 silence = 600;
</span><span class="cx">                                         }
</span><span class="cx">                                         
</span><del>-                                        if ((ringback.fh || silence || ringback.audio_buffer) && write_frame.codec && write_frame.datalen) {
</del><ins>+                                        if ((ringback.fh || silence || ringback.audio_buffer || oglobals.bridge_early_media > -1) && write_frame.codec && write_frame.datalen) {
</ins><span class="cx">                                                 if (silence) {
</span><span class="cx">                                                         write_frame.datalen = write_frame.codec->implementation->decoded_bytes_per_packet;
</span><span class="cx">                                                         switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.datalen / 2, silence);
</span><span class="lines">@@ -3056,6 +3146,17 @@
</span><span class="cx">                                 switch_channel_set_variable(caller_channel, "originate_disposition", switch_channel_cause2str(*cause));
</span><span class="cx">                         }
</span><span class="cx">
</span><ins>+                        early_state.ready = 0;
+                                
+                        if (oglobals.ethread) {
+                                switch_status_t st;
+                                switch_thread_join(&st, oglobals.ethread);
+                        }
+
+                        if (early_state.buffer) {
+                                switch_buffer_destroy(&early_state.buffer);
+                        }
+
</ins><span class="cx">                         if (ringback.fh) {
</span><span class="cx">                                 switch_core_file_close(ringback.fh);
</span><span class="cx">                                 ringback.fh = NULL;
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>