<!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][16708] </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=16708">16708</a></dd>
<dt>Author</dt> <dd>gmaruzz</dd>
<dt>Date</dt> <dd>2010-02-20 06:32:10 -0600 (Sat, 20 Feb 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>skypiax: completely new audio threads, let's see how it works on windoz</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skypiaxmod_skypiaxc">freeswitch/trunk/src/mod/endpoints/mod_skypiax/mod_skypiax.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skypiaxskypiaxh">freeswitch/trunk/src/mod/endpoints/mod_skypiax/skypiax.h</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skypiaxskypiax_protocolc">freeswitch/trunk/src/mod/endpoints/mod_skypiax/skypiax_protocol.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodendpointsmod_skypiaxmod_skypiaxc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skypiax/mod_skypiax.c (16707 => 16708)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skypiax/mod_skypiax.c        2010-02-19 22:08:10 UTC (rev 16707)
+++ freeswitch/trunk/src/mod/endpoints/mod_skypiax/mod_skypiax.c        2010-02-20 12:32:10 UTC (rev 16708)
</span><span class="lines">@@ -258,6 +258,7 @@
</span><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="cx">         }
</span><span class="cx">
</span><ins>+#if 0
</ins><span class="cx">         if (switch_core_timer_init(&tech_pvt->timer_read, "soft", 20, tech_pvt->read_codec.implementation->samples_per_packet, skypiax_module_pool) !=
</span><span class="cx">                 SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                 ERRORA("setup timer failed\n", SKYPIAX_P_LOG);
</span><span class="lines">@@ -267,7 +268,7 @@
</span><span class="cx">         switch_core_timer_sync(&tech_pvt->timer_read);
</span><span class="cx">
</span><span class="cx"> #ifdef TIMER_WRITE
</span><del>-        if (switch_core_timer_init(&tech_pvt->timer_write, "soft", 20, tech_pvt->write_codec.implementation->samples_per_packet, skypiax_module_pool) !=
</del><ins>+        if (switch_core_timer_init(&tech_pvt->timer_write, "soft", 10, tech_pvt->write_codec.implementation->samples_per_packet, skypiax_module_pool) !=
</ins><span class="cx">                 SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                 ERRORA("setup timer failed\n", SKYPIAX_P_LOG);
</span><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="lines">@@ -275,6 +276,7 @@
</span><span class="cx">
</span><span class="cx">         switch_core_timer_sync(&tech_pvt->timer_write);
</span><span class="cx"> #endif // TIMER_WRITE
</span><ins>+#endif
</ins><span class="cx">
</span><span class="cx"> dtmf_rx_init(&tech_pvt->dtmf_state, NULL, NULL);
</span><span class="cx"> dtmf_rx_parms(&tech_pvt->dtmf_state, 0, 10, 10, -99);
</span><span class="lines">@@ -512,6 +514,7 @@
</span><span class="cx">         switch_channel_t *channel = NULL;
</span><span class="cx">         private_t *tech_pvt = NULL;
</span><span class="cx">         char msg_to_skype[256];
</span><ins>+        switch_status_t status;
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx">         channel = switch_core_session_get_channel(session);
</span><span class="lines">@@ -532,6 +535,8 @@
</span><span class="cx">         switch_clear_flag(tech_pvt, TFLAG_VOICE);
</span><span class="cx">         //switch_set_flag(tech_pvt, TFLAG_HANGUP);
</span><span class="cx">
</span><ins>+        tech_pvt->interface_state = SKYPIAX_STATE_HANGUP_REQUESTED;
+
</ins><span class="cx">         if (strlen(tech_pvt->skype_call_id)) {
</span><span class="cx">                 //switch_thread_cond_signal(tech_pvt->cond);
</span><span class="cx">                 DEBUGA_SKYPE("hanging up skype call: %s\n", SKYPIAX_P_LOG, tech_pvt->skype_call_id);
</span><span class="lines">@@ -540,6 +545,17 @@
</span><span class="cx">         }
</span><span class="cx">         //memset(tech_pvt->session_uuid_str, '\0', sizeof(tech_pvt->session_uuid_str));
</span><span class="cx">         //*tech_pvt->session_uuid_str = '\0';
</span><ins>+
+                        if (tech_pvt->tcp_cli_thread) {
+                                switch_thread_join(&status, tech_pvt->tcp_cli_thread);
+                        }
+                        if(status!=SWITCH_STATUS_SUCCESS)
+        ERRORA("%s cli_join HANGUP\n", SKYPIAX_P_LOG, tech_pvt->name);
+                        if (tech_pvt->tcp_srv_thread) {
+                                switch_thread_join(&status, tech_pvt->tcp_srv_thread);
+                        }
+                        if(status!=SWITCH_STATUS_SUCCESS)
+        ERRORA("%s srv_join HANGUP\n", SKYPIAX_P_LOG, tech_pvt->name);
</ins><span class="cx">         DEBUGA_SKYPE("%s CHANNEL HANGUP\n", SKYPIAX_P_LOG, tech_pvt->name);
</span><span class="cx">         switch_mutex_lock(globals.mutex);
</span><span class="cx">         globals.calls--;
</span><span class="lines">@@ -675,6 +691,8 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+#ifdef OLDTCP
+#if 0
</ins><span class="cx"> static switch_status_t channel_read_frame(switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id)
</span><span class="cx"> {
</span><span class="cx">         switch_channel_t *channel = NULL;
</span><span class="lines">@@ -855,7 +873,205 @@
</span><span class="cx">
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><ins>+#endif //0
+#else
+static switch_status_t channel_read_frame(switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id)
+{
+        switch_channel_t *channel = NULL;
+        private_t *tech_pvt = NULL;
+        switch_byte_t *data;
+        char digit_str[256];
+        short *frame_16_khz;
+        short frame_8_khz[160];        
+        int i;
+        int a;
+size_t bytes_read;
</ins><span class="cx">
</span><ins>+
+        channel = switch_core_session_get_channel(session);
+        switch_assert(channel != NULL);
+
+        tech_pvt = switch_core_session_get_private(session);
+        switch_assert(tech_pvt != NULL);
+
+        if (!switch_channel_ready(channel) || !switch_test_flag(tech_pvt, TFLAG_IO)) {
+                ERRORA("channel not ready \n", SKYPIAX_P_LOG);
+                //TODO: kill the bastard
+                return SWITCH_STATUS_FALSE;
+        }
+
+
+
+        if (!tech_pvt->read_buffer) {
+                int32_t len = 640 * 8;
+
+                switch_buffer_create(skypiax_module_pool, &tech_pvt->read_buffer, len);
+                switch_assert(tech_pvt->read_buffer);
+        }
+
+
+        tech_pvt->begin_to_read=1;
+
+
+
+        tech_pvt->read_frame.flags = SFF_NONE;
+        *frame = NULL;
+
+        switch_core_timer_next(&tech_pvt->timer_read);
+
+
+        switch_mutex_lock(tech_pvt->mutex_audio_srv);
+        if ((bytes_read = switch_buffer_read(tech_pvt->read_buffer, tech_pvt->read_frame.data, SAMPLES_PER_FRAME * sizeof(short)))) {
+                tech_pvt->read_frame.datalen = bytes_read;
+        }
+        switch_mutex_unlock(tech_pvt->mutex_audio_srv);
+
+
+        if (!bytes_read) {
+                ERRORA("skypiax_audio_read Silence\n", SKYPIAX_P_LOG);
+                memset(tech_pvt->read_frame.data, 255, SAMPLES_PER_FRAME * sizeof(short));
+                tech_pvt->read_frame.datalen = SAMPLES_PER_FRAME * sizeof(short);
+
+        }
+
+        switch_set_flag(tech_pvt, TFLAG_VOICE);
+        while (switch_test_flag(tech_pvt, TFLAG_IO)) {
+                if (switch_test_flag(tech_pvt, TFLAG_BREAK)) {
+                        switch_clear_flag(tech_pvt, TFLAG_BREAK);
+                        DEBUGA_SKYPE("CHANNEL READ FRAME goto CNG\n", SKYPIAX_P_LOG);
+                        goto cng;
+                }
+
+                if (!switch_test_flag(tech_pvt, TFLAG_IO)) {
+                        DEBUGA_SKYPE("CHANNEL READ FRAME not IO\n", SKYPIAX_P_LOG);
+                        return SWITCH_STATUS_FALSE;
+                }
+
+                if (switch_test_flag(tech_pvt, TFLAG_IO) && switch_test_flag(tech_pvt, TFLAG_VOICE)) {
+                        switch_clear_flag(tech_pvt, TFLAG_VOICE);
+                        if (!tech_pvt->read_frame.datalen) {
+                                DEBUGA_SKYPE("CHANNEL READ CONTINUE\n", SKYPIAX_P_LOG);
+                                continue;
+                        }
+                        *frame = &tech_pvt->read_frame;
+
+
+                        if (switch_true(switch_channel_get_variable(channel, "skype_get_inband_dtmf"))) {
+
+                                frame_16_khz = tech_pvt->read_frame.data;
+
+                                a = 0;
+                                for (i = 0; i < tech_pvt->read_frame.datalen / sizeof(short); i++) {
+                                        frame_8_khz[a] = frame_16_khz[i];
+                                        i++;
+                                        a++;
+                                }
+                                //DEBUGA_SKYPE("a=%d i=%d\n", SKYPIAX_P_LOG, a, i);
+
+                                memset(digit_str, 0, sizeof(digit_str));
+                                //dtmf_rx(&tech_pvt->dtmf_state, (int16_t *) tech_pvt->read_frame.data,tech_pvt->read_frame.datalen/sizeof(short) );
+                                dtmf_rx(&tech_pvt->dtmf_state, (int16_t *) frame_8_khz, 160);
+                                dtmf_rx_get(&tech_pvt->dtmf_state, digit_str, sizeof(digit_str));
+
+
+                                if (digit_str[0]) {
+                                        switch_time_t new_dtmf_timestamp = switch_time_now();
+                                        if ((new_dtmf_timestamp - tech_pvt->old_dtmf_timestamp) > 350000) { //FIXME: make it configurable
+                                                char *p = digit_str;
+                                                switch_channel_t *channel = switch_core_session_get_channel(session);
+
+                                                while (p && *p) {
+                                                        switch_dtmf_t dtmf;
+                                                        dtmf.digit = *p;
+                                                        dtmf.duration = SWITCH_DEFAULT_DTMF_DURATION;
+                                                        switch_channel_queue_dtmf(channel, &dtmf);
+                                                        p++;
+                                                }
+                                                NOTICA("DTMF DETECTED: [%s] new_dtmf_timestamp: %u, delta_t: %u\n", SKYPIAX_P_LOG, digit_str, (unsigned int) new_dtmf_timestamp,
+                                                                (unsigned int) (new_dtmf_timestamp - tech_pvt->old_dtmf_timestamp));
+                                                tech_pvt->old_dtmf_timestamp = new_dtmf_timestamp;
+                                        }
+                                }
+                        }
+
+
+
+
+
+#if SWITCH_BYTE_ORDER == __BIG_ENDIAN
+                        if (switch_test_flag(tech_pvt, TFLAG_LINEAR)) {
+                                switch_swap_linear((*frame)->data, (int) (*frame)->datalen / 2);
+                        }
+#endif
+                        return SWITCH_STATUS_SUCCESS;
+                }
+
+                DEBUGA_SKYPE("CHANNEL READ no TFLAG_IO\n", SKYPIAX_P_LOG);
+                return SWITCH_STATUS_FALSE;
+
+        }
+
+        DEBUGA_SKYPE("CHANNEL READ FALSE\n", SKYPIAX_P_LOG);
+        return SWITCH_STATUS_FALSE;
+
+cng:
+        data = (switch_byte_t *) tech_pvt->read_frame.data;
+        data[0] = 65;
+        data[1] = 0;
+        tech_pvt->read_frame.datalen = 2;
+        tech_pvt->read_frame.flags = SFF_CNG;
+        *frame = &tech_pvt->read_frame;
+        return SWITCH_STATUS_SUCCESS;
+
+}
+
+static switch_status_t channel_write_frame(switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id)
+{
+        switch_channel_t *channel = NULL;
+        private_t *tech_pvt = NULL;
+        unsigned int sent;
+
+        channel = switch_core_session_get_channel(session);
+        switch_assert(channel != NULL);
+
+        tech_pvt = switch_core_session_get_private(session);
+        switch_assert(tech_pvt != NULL);
+
+        if (!switch_channel_ready(channel) || !switch_test_flag(tech_pvt, TFLAG_IO)) {
+                ERRORA("channel not ready \n", SKYPIAX_P_LOG);
+                //TODO: kill the bastard
+                return SWITCH_STATUS_FALSE;
+        }
+#if SWITCH_BYTE_ORDER == __BIG_ENDIAN
+        if (switch_test_flag(tech_pvt, TFLAG_LINEAR)) {
+                switch_swap_linear(frame->data, (int) frame->datalen / 2);
+        }
+#endif
+
+        if (!tech_pvt->write_buffer) {
+                int32_t len = 320 * 8;
+
+                switch_buffer_create(skypiax_module_pool, &tech_pvt->write_buffer, len);
+                switch_assert(tech_pvt->write_buffer);
+        }
+
+
+
+        sent = frame->datalen;
+
+        switch_mutex_lock(tech_pvt->mutex_audio_cli);
+        switch_buffer_write(tech_pvt->write_buffer, frame->data, frame->datalen);
+        switch_mutex_unlock(tech_pvt->mutex_audio_cli);
+
+        tech_pvt->begin_to_write=1;
+        if (sent != frame->datalen && sent != -1) {
+                DEBUGA_SKYPE("CLI PIPE write %d\n", SKYPIAX_P_LOG, sent);
+        }
+
+        return SWITCH_STATUS_SUCCESS;
+}
+
+#endif //OLDTCP
</ins><span class="cx"> static switch_status_t channel_answer_channel(switch_core_session_t *session)
</span><span class="cx"> {
</span><span class="cx">         private_t *tech_pvt;
</span><span class="lines">@@ -1127,7 +1343,7 @@
</span><span class="cx">                 if (!(running && tech_pvt->running))
</span><span class="cx">                         break;
</span><span class="cx">                 res = skypiax_signaling_read(tech_pvt);
</span><del>-                if (res == CALLFLOW_INCOMING_HANGUP) {
</del><ins>+                if (res == CALLFLOW_INCOMING_HANGUP || tech_pvt->skype_callflow == CALLFLOW_INCOMING_HANGUP) {
</ins><span class="cx">                         switch_core_session_t *session = NULL;
</span><span class="cx">                         switch_channel_t *channel = NULL;
</span><span class="cx">                         //private_t *tech_pvt = NULL;
</span><span class="lines">@@ -1840,7 +2056,26 @@
</span><span class="cx">
</span><span class="cx"> tech_pvt->begin_to_write=0;
</span><span class="cx"> tech_pvt->begin_to_read=0;
</span><ins>+#if 1
+        if (switch_core_timer_init(&tech_pvt->timer_read, "soft", 20, 320, skypiax_module_pool) !=
+                SWITCH_STATUS_SUCCESS) {
+                ERRORA("setup timer failed\n", SKYPIAX_P_LOG);
+                return SWITCH_STATUS_FALSE;
+        }
</ins><span class="cx">
</span><ins>+        switch_core_timer_sync(&tech_pvt->timer_read);
+
+#ifdef TIMER_WRITE
+        if (switch_core_timer_init(&tech_pvt->timer_write, "soft", 10, 320, skypiax_module_pool) !=
+                SWITCH_STATUS_SUCCESS) {
+                ERRORA("setup timer failed\n", SKYPIAX_P_LOG);
+                return SWITCH_STATUS_FALSE;
+        }
+
+        switch_core_timer_sync(&tech_pvt->timer_write);
+#endif // TIMER_WRITE
+
+#endif
</ins><span class="cx">         switch_threadattr_create(&thd_attr, skypiax_module_pool);
</span><span class="cx">         switch_threadattr_detach_set(thd_attr, 0);
</span><span class="cx">         switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skypiaxskypiaxh"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skypiax/skypiax.h (16707 => 16708)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skypiax/skypiax.h        2010-02-19 22:08:10 UTC (rev 16707)
+++ freeswitch/trunk/src/mod/endpoints/mod_skypiax/skypiax.h        2010-02-20 12:32:10 UTC (rev 16708)
</span><span class="lines">@@ -288,7 +288,10 @@
</span><span class="cx"> int begin_to_read;
</span><span class="cx"> dtmf_rx_state_t dtmf_state;
</span><span class="cx"> switch_time_t old_dtmf_timestamp;
</span><ins>+         switch_buffer_t *write_buffer;
+         switch_buffer_t *read_buffer;
</ins><span class="cx">
</span><ins>+
</ins><span class="cx"> };
</span><span class="cx">
</span><span class="cx"> typedef struct private_object private_t;
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skypiaxskypiax_protocolc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skypiax/skypiax_protocol.c (16707 => 16708)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skypiax/skypiax_protocol.c        2010-02-19 22:08:10 UTC (rev 16707)
+++ freeswitch/trunk/src/mod/endpoints/mod_skypiax/skypiax_protocol.c        2010-02-20 12:32:10 UTC (rev 16708)
</span><span class="lines">@@ -728,6 +728,8 @@
</span><span class="cx">         return 0;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+#ifdef OLDTCP
+#if 0
</ins><span class="cx"> void *skypiax_do_tcp_srv_thread_func(void *obj)
</span><span class="cx"> {
</span><span class="cx">         private_t *tech_pvt = obj;
</span><span class="lines">@@ -1278,7 +1280,369 @@
</span><span class="cx">         }
</span><span class="cx">         return 1;
</span><span class="cx"> }
</span><ins>+#endif//0
+#else
</ins><span class="cx">
</span><ins>+void *skypiax_do_tcp_srv_thread_func(void *obj)
+{
+        private_t *tech_pvt = obj;
+        int s;
+        unsigned int len;
+        //unsigned int i;
+        //unsigned int a;
+#if defined(WIN32) && !defined(__CYGWIN__)
+        int sin_size;
+#else /* WIN32 */
+        unsigned int sin_size;
+#endif /* WIN32 */
+        unsigned int fd;
+        short srv_in[SAMPLES_PER_FRAME];
+        //short srv_out[SAMPLES_PER_FRAME / 2];
+        //struct sockaddr_in my_addr;
+        struct sockaddr_in remote_addr;
+        //int exit = 0;
+        //unsigned int kill_cli_size;
+        //short kill_cli_buff[SAMPLES_PER_FRAME];
+        //short totalbuf[SAMPLES_PER_FRAME];
+        int sockbufsize = 0;
+        unsigned int size = sizeof(int);
+
+        s = skypiax_socket_create_and_bind(tech_pvt, &tech_pvt->tcp_srv_port);
+        if (s < 0) {
+                ERRORA("skypiax_socket_create_and_bind error!\n", SKYPIAX_P_LOG);
+                return NULL;
+        }
+        DEBUGA_SKYPE("started tcp_srv_thread thread.\n", SKYPIAX_P_LOG);
+
+        listen(s, 6);
+
+        sin_size = sizeof(remote_addr);
+
+        /****************************/
+        while (tech_pvt->interface_state != SKYPIAX_STATE_DOWN
+                        && (tech_pvt->skype_callflow == CALLFLOW_STATUS_INPROGRESS
+                                || tech_pvt->skype_callflow == CALLFLOW_STATUS_EARLYMEDIA
+                                || tech_pvt->skype_callflow == CALLFLOW_STATUS_REMOTEHOLD || tech_pvt->skype_callflow == SKYPIAX_STATE_UP)) {
+
+                unsigned int fdselectgio;
+                int rtgio;
+                fd_set fsgio;
+                struct timeval togio;
+
+                if (!(running && tech_pvt->running))
+                        break;
+                FD_ZERO(&fsgio);
+                togio.tv_usec = 20000;        //20msec
+                togio.tv_sec = 0;
+                fdselectgio = s;
+                FD_SET(fdselectgio, &fsgio);
+
+                rtgio = select(fdselectgio + 1, &fsgio, NULL, NULL, &togio);
+
+                if (rtgio) {
+
+                        /****************************/
+
+                        while (s > 0 && (fd = accept(s, (struct sockaddr *) &remote_addr, &sin_size)) > 0) {
+                                DEBUGA_SKYPE("ACCEPTED here I send you %d\n", SKYPIAX_P_LOG, tech_pvt->tcp_srv_port);
+
+                                sockbufsize = 0;
+                                size = sizeof(int);
+                                getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *) &sockbufsize, &size);
+                                DEBUGA_SKYPE("3 SO_RCVBUF is %d, size is %d\n", SKYPIAX_P_LOG, sockbufsize, size);
+                                sockbufsize = 0;
+                                size = sizeof(int);
+                                getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *) &sockbufsize, &size);
+                                DEBUGA_SKYPE("3 SO_SNDBUF is %d, size is %d\n", SKYPIAX_P_LOG, sockbufsize, size);
+
+
+                                if (!(running && tech_pvt->running))
+                                        break;
+                                while (tech_pvt->interface_state != SKYPIAX_STATE_DOWN
+                                                && (tech_pvt->skype_callflow == CALLFLOW_STATUS_INPROGRESS
+                                                        || tech_pvt->skype_callflow == CALLFLOW_STATUS_EARLYMEDIA
+                                                        || tech_pvt->skype_callflow == CALLFLOW_STATUS_REMOTEHOLD || tech_pvt->skype_callflow == SKYPIAX_STATE_UP)) {
+
+                                        unsigned int fdselect;
+                                        int rt;
+                                        fd_set fs;
+                                        struct timeval to;
+
+                                        if (!(running && tech_pvt->running))
+                                                break;
+                                        fdselect = fd;
+                                        FD_ZERO(&fs);
+                                        FD_SET(fdselect, &fs);
+                                        to.tv_usec = 60000;        //60 msec
+                                        to.tv_sec = 0;
+
+                                        if(tech_pvt->begin_to_read==0){
+                                                skypiax_sleep(1000);
+                                                continue;
+                                        }
+                                        rt = select(fdselect + 1, &fs, NULL, NULL, &to);
+                                        if (rt > 0) {
+
+                                                if (tech_pvt->skype_callflow != CALLFLOW_STATUS_REMOTEHOLD) {
+                                                        len = recv(fd, (char *) srv_in, 640, 0);
+                                                } else {
+                                                        continue;
+                                                }
+
+                                                if (len == -1) {
+                                                        ERRORA("len=%d, error: %s\n", SKYPIAX_P_LOG, len, strerror(errno));
+                                                        break;
+                                                }
+                                                if (len > 0) {
+                                                        switch_mutex_lock(tech_pvt->mutex_audio_srv);
+                                                        switch_buffer_write(tech_pvt->read_buffer, srv_in, len);
+                                                        switch_mutex_unlock(tech_pvt->mutex_audio_srv);
+                                                } else if (len == 0) {
+                                                        ERRORA("CLOSING, len=%d, expected 640\n", SKYPIAX_P_LOG, len);
+                                                        break;
+                                                } else {
+                                                        WARNINGA("len=%d, expected 640\n", SKYPIAX_P_LOG, len);
+                                                }
+
+                                        } else if(rt==0){
+                                                continue;
+                                        } else {
+                                                ERRORA("SRV rt=%d\n", SKYPIAX_P_LOG, rt);
+                                                break;
+                                        }
+
+                                }
+
+                                DEBUGA_SKYPE("Skype incoming audio GONE\n", SKYPIAX_P_LOG);
+                                tech_pvt->skype_callflow = CALLFLOW_INCOMING_HANGUP;
+                                skypiax_sleep(20000);
+                                skypiax_close_socket(fd);
+                                break;
+                        }
+                        break;
+                }
+        }
+
+        DEBUGA_SKYPE("incoming audio (read) server (I am it) EXITING\n", SKYPIAX_P_LOG);
+        skypiax_close_socket(s);
+        s = -1;
+        tech_pvt->tcp_srv_thread = NULL;
+        return NULL;
+}
+
+void *skypiax_do_tcp_cli_thread_func(void *obj)
+{
+        private_t *tech_pvt = obj;
+        int s;
+        //struct sockaddr_in my_addr;
+        struct sockaddr_in remote_addr;
+        //unsigned int got;
+        unsigned int len;
+        //unsigned int i;
+        //unsigned int a;
+        unsigned int fd;
+        short cli_out[SAMPLES_PER_FRAME * 2];
+        //short cli_in[SAMPLES_PER_FRAME];
+#ifdef WIN32
+        int sin_size;
+#else
+        unsigned int sin_size;
+#endif /* WIN32 */
+        int sockbufsize = 0;
+        unsigned int size = sizeof(int);
+
+        s = skypiax_socket_create_and_bind(tech_pvt, &tech_pvt->tcp_cli_port);
+        if (s < 0) {
+                ERRORA("skypiax_socket_create_and_bind error!\n", SKYPIAX_P_LOG);
+                return NULL;
+        }
+
+
+
+        DEBUGA_SKYPE("started tcp_cli_thread thread.\n", SKYPIAX_P_LOG);
+
+        listen(s, 6);
+
+        sin_size = sizeof(remote_addr);
+
+        /****************************/
+        while (tech_pvt->interface_state != SKYPIAX_STATE_DOWN
+                        && (tech_pvt->skype_callflow == CALLFLOW_STATUS_INPROGRESS
+                                || tech_pvt->skype_callflow == CALLFLOW_STATUS_EARLYMEDIA
+                                || tech_pvt->skype_callflow == CALLFLOW_STATUS_REMOTEHOLD || tech_pvt->skype_callflow == SKYPIAX_STATE_UP)) {
+
+                unsigned int fdselectgio;
+                int rtgio;
+                fd_set fsgio;
+                struct timeval togio;
+
+                if (!(running && tech_pvt->running))
+                        break;
+                FD_ZERO(&fsgio);
+                togio.tv_usec = 20000;        //20msec
+                togio.tv_sec = 0;
+                fdselectgio = s;
+                FD_SET(fdselectgio, &fsgio);
+
+                rtgio = select(fdselectgio + 1, &fsgio, NULL, NULL, &togio);
+
+                if (rtgio) {
+
+                        /****************************/
+
+                        while (s > 0 && (fd = accept(s, (struct sockaddr *) &remote_addr, &sin_size)) > 0) {
+                                DEBUGA_SKYPE("ACCEPTED here you send me %d\n", SKYPIAX_P_LOG, tech_pvt->tcp_cli_port);
+
+                                sockbufsize = 0;
+                                size = sizeof(int);
+                                getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *) &sockbufsize, &size);
+                                DEBUGA_SKYPE("4 SO_RCVBUF is %d, size is %d\n", SKYPIAX_P_LOG, sockbufsize, size);
+                                sockbufsize = 0;
+                                size = sizeof(int);
+                                getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *) &sockbufsize, &size);
+                                DEBUGA_SKYPE("4 SO_SNDBUF is %d, size is %d\n", SKYPIAX_P_LOG, sockbufsize, size);
+
+
+
+                                if (!(running && tech_pvt->running))
+                                        break;
+                                while (tech_pvt->interface_state != SKYPIAX_STATE_DOWN
+                                                && (tech_pvt->skype_callflow == CALLFLOW_STATUS_INPROGRESS
+                                                        || tech_pvt->skype_callflow == CALLFLOW_STATUS_EARLYMEDIA
+                                                        || tech_pvt->skype_callflow == CALLFLOW_STATUS_REMOTEHOLD || tech_pvt->skype_callflow == SKYPIAX_STATE_UP)) {
+                                        unsigned int fdselect;
+                                        int rt;
+                                        fd_set fs;
+                                        struct timeval to;
+                                        size_t bytes_to_write;
+                                        int waitin;
+                                        int waited_max;
+
+                                        if (!(running && tech_pvt->running))
+                                                break;
+                                        FD_ZERO(&fs);
+                                        to.tv_usec = 1000;        //1msec
+                                        to.tv_sec = 0;
+
+                                        fdselect = fd;
+                                        FD_SET(fdselect, &fs);
+
+
+                                        if(tech_pvt->begin_to_write==0){
+                                                skypiax_sleep(1000);
+                                                continue;
+                                        }
+
+                                        //rt = select(fdselect + 1, NULL, &fs, NULL, &to);
+                                        //if(rt==-1)
+                                                //break;
+                                        rt=1;
+
+                                        if (rt > 0) {
+
+                                                        //DEBUGA_SKYPE("before!\n", SKYPIAX_P_LOG);
+                                                if (tech_pvt->timer_write.timer_interface && tech_pvt->timer_write.timer_interface->timer_next && tech_pvt->interface_state != SKYPIAX_STATE_HANGUP_REQUESTED) {
+                                                        switch_core_timer_next(&tech_pvt->timer_write);
+                                                }
+                                                        //DEBUGA_SKYPE("after!\n", SKYPIAX_P_LOG);
+                                                bytes_to_write=0;
+                                                waitin=0;
+                                                waited_max=1;
+
+                                                if(tech_pvt->skype_callflow == CALLFLOW_INCOMING_HANGUP){
+                                                        break;
+                                                }
+                                                if(!switch_buffer_inuse(tech_pvt->write_buffer)){
+                                                        memset(cli_out, 255, sizeof(cli_out));
+                                                        bytes_to_write = 320;
+                                                        DEBUGA_SKYPE("Silence!\n", SKYPIAX_P_LOG);
+                                                }else {
+                                                        switch_mutex_lock(tech_pvt->mutex_audio_cli);
+                                                        bytes_to_write = switch_buffer_read(tech_pvt->write_buffer, cli_out, 320);
+                                                        switch_mutex_unlock(tech_pvt->mutex_audio_cli);
+                                                }
+
+                                                /* send the 16khz frame to the Skype client waiting for incoming audio to be sent to the remote party */
+                                                if (tech_pvt->skype_callflow != CALLFLOW_STATUS_REMOTEHOLD) {
+                                                        len = send(fd, cli_out, bytes_to_write, 0);
+                                                        if (len == -1) {
+                                                                ERRORA("len=%d, error: %s\n", SKYPIAX_P_LOG, len, strerror(errno));
+                                                                break;
+                                                        }
+                                                        if (len != bytes_to_write) {
+                                                                WARNINGA("len=%d\n", SKYPIAX_P_LOG, len);
+                                                        }
+                                                }
+                                        } else if(rt==0){
+                                                continue;
+                                        } else {
+                                                ERRORA("CLI rt=%d\n", SKYPIAX_P_LOG, rt);
+                                                break;
+                                        }
+
+                                }
+                                DEBUGA_SKYPE("Skype outbound audio GONE\n", SKYPIAX_P_LOG);
+                                tech_pvt->skype_callflow = CALLFLOW_INCOMING_HANGUP;
+                                skypiax_sleep(20000);
+                                skypiax_close_socket(fd);
+                                break;
+                        }
+                        break;
+                }
+        }
+
+        DEBUGA_SKYPE("outbound audio server (I am it) EXITING\n", SKYPIAX_P_LOG);
+        skypiax_close_socket(s);
+        s = -1;
+        tech_pvt->tcp_cli_thread = NULL;
+        return NULL;
+}
+
+int skypiax_audio_read(private_t * tech_pvt)
+{
+        unsigned int samples;
+        int waitin;
+        int max_waitin=30;
+
+        waitin=0;
+        while (tech_pvt->flag_audio_srv == 0) {
+#ifdef WIN32
+                skypiax_sleep(1000);                //0.1 millisec
+#else
+                skypiax_sleep(1000);        //1 millisec
+#endif //WIN32
+                waitin++;
+
+                if(waitin == max_waitin){
+                        DEBUGA_SKYPE("read is now at max_waitin: %d\n", SKYPIAX_P_LOG, waitin);
+                        break;
+                }
+                //WARNINGA("read now is 0\n", SKYPIAX_P_LOG);
+        }
+        if(waitin > 22){
+                DEBUGA_SKYPE("read is now %d\n", SKYPIAX_P_LOG, waitin);
+        }
+        //samples = skypiax_pipe_read(tech_pvt->audiopipe_srv[0], tech_pvt->read_frame.data, SAMPLES_PER_FRAME * sizeof(short));
+                        switch_mutex_lock(tech_pvt->mutex_audio_srv);
+        memcpy(tech_pvt->read_frame.data, tech_pvt->audiobuf_srv, SAMPLES_PER_FRAME * sizeof(short));
+        tech_pvt->flag_audio_srv = 0;
+                        switch_mutex_unlock(tech_pvt->mutex_audio_srv);
+
+        samples = SAMPLES_PER_FRAME * sizeof(short);
+        if (samples != SAMPLES_PER_FRAME * sizeof(short)) {
+                if (samples)
+                        WARNINGA("read samples=%u expected=%u\n", SKYPIAX_P_LOG, samples, (int) (SAMPLES_PER_FRAME * sizeof(short)));
+                return 0;
+        } else {
+                /* A real frame */
+                tech_pvt->read_frame.datalen = samples;
+        }
+        return 1;
+}
+
+
+
+#endif //OLDTCP
</ins><span class="cx"> int skypiax_senddigit(private_t * tech_pvt, char digit)
</span><span class="cx"> {
</span><span class="cx">         char msg_to_skype[1024];
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>