<!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][14223] </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=14223">14223</a></dd>
<dt>Author</dt> <dd>rupa</dd>
<dt>Date</dt> <dd>2009-07-13 14:01:03 -0500 (Mon, 13 Jul 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre><a href="http://jira.freeswitch.org/browse/MODFORM-32">MODFORM-32</a> - mp3 improvements (seeking, cleanup)</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodformatsmod_shoutmod_shoutc">freeswitch/trunk/src/mod/formats/mod_shout/mod_shout.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodformatsmod_shoutmod_shoutc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/formats/mod_shout/mod_shout.c (14222 => 14223)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/formats/mod_shout/mod_shout.c        2009-07-13 18:15:46 UTC (rev 14222)
+++ freeswitch/trunk/src/mod/formats/mod_shout/mod_shout.c        2009-07-13 19:01:03 UTC (rev 14223)
</span><span class="lines">@@ -25,6 +25,7 @@
</span><span class="cx">  * 
</span><span class="cx">  * Anthony Minessale II &lt;anthm@freeswitch.org&gt;
</span><span class="cx">  * Rupa Schomaker &lt;rupa@rupa.com&gt;
</span><ins>+ * John Wehle &lt;john@feith.com&gt;
</ins><span class="cx">  *
</span><span class="cx">  * mod_shout.c -- Icecast Module
</span><span class="cx">  *
</span><span class="lines">@@ -109,7 +110,6 @@
</span><span class="cx">         int err;
</span><span class="cx">         int mp3err;
</span><span class="cx">         int dlen;
</span><del>-        switch_file_t *fd;
</del><span class="cx">         FILE *fp;
</span><span class="cx">         int samplerate;
</span><span class="cx">         uint8_t thread_running;
</span><span class="lines">@@ -129,21 +129,35 @@
</span><span class="cx"> 
</span><span class="cx"> typedef struct shout_context shout_context_t;
</span><span class="cx"> 
</span><del>-static size_t decode_fd(shout_context_t *context, void *data, size_t bytes);
</del><ins>+static void decode_fd(shout_context_t *context, void *data, size_t bytes);
</ins><span class="cx"> 
</span><span class="cx"> static inline void free_context(shout_context_t *context)
</span><span class="cx"> {
</span><span class="cx">         int ret;
</span><span class="cx"> 
</span><span class="cx">         if (context) {
</span><ins>+                switch_mutex_lock(context-&gt;audio_mutex);
</ins><span class="cx">                 context-&gt;err++;
</span><del>-                switch_thread_rwlock_wrlock(context-&gt;rwlock);
</del><ins>+                switch_mutex_unlock(context-&gt;audio_mutex);
</ins><span class="cx"> 
</span><del>-                if (context-&gt;fd) {
-                        switch_file_close(context-&gt;fd);
-                        context-&gt;fd = NULL;
</del><ins>+                if (context-&gt;stream_url) {
+                        int sanity = 0;
+
+                        while (context-&gt;thread_running) {
+                                switch_yield(500000);
+                                if (++sanity &gt; 10) {
+                                        break;
+                                }
+                        }
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                switch_thread_rwlock_wrlock(context-&gt;rwlock);
+
+                if (context-&gt;mh) {
+                        mpg123_close(context-&gt;mh);
+                        mpg123_delete(context-&gt;mh);
+                }
+                
</ins><span class="cx">                 if (context-&gt;fp) {
</span><span class="cx">                         unsigned char mp3buffer[8192];
</span><span class="cx">                         int len;
</span><span class="lines">@@ -173,12 +187,6 @@
</span><span class="cx">                         context-&gt;fp = NULL;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if (context-&gt;audio_buffer) {
-                        switch_mutex_lock(context-&gt;audio_mutex);
-                        switch_buffer_destroy(&amp;context-&gt;audio_buffer);
-                        switch_mutex_unlock(context-&gt;audio_mutex);
-                }
-
</del><span class="cx">                 if (context-&gt;shout) {
</span><span class="cx">                         shout_close(context-&gt;shout);
</span><span class="cx">                         context-&gt;shout = NULL;
</span><span class="lines">@@ -189,23 +197,12 @@
</span><span class="cx">                         context-&gt;gfp = NULL;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if (context-&gt;stream_url) {
-                        int sanity = 0;
-
-                        while (context-&gt;thread_running) {
-                                switch_yield(500000);
-                                if (++sanity &gt; 10) {
-                                        break;
-                                }
-                        }
-
-                        
</del><ins>+                if (context-&gt;audio_buffer) {
+                        switch_buffer_destroy(&amp;context-&gt;audio_buffer);
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if (context-&gt;mh) {
-                        mpg123_delete(context-&gt;mh);
-                }
-                
</del><ins>+                switch_mutex_destroy(context-&gt;audio_mutex);
+
</ins><span class="cx">                 switch_thread_rwlock_unlock(context-&gt;rwlock);
</span><span class="cx">                 switch_thread_rwlock_destroy(context-&gt;rwlock);
</span><span class="cx">         }
</span><span class="lines">@@ -289,154 +286,60 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static size_t decode_fd(shout_context_t *context, void *data, size_t bytes)
</del><ins>+static void decode_fd(shout_context_t *context, void *data, size_t bytes)
</ins><span class="cx"> {
</span><span class="cx">         int decode_status = 0;
</span><del>-        size_t dlen = 0;
-        int x = 0;
-        unsigned char *in;
-        int inlen = 0;
-        unsigned char *out;
-        int outlen;
-        int usedlen;
-        unsigned char inbuf[MP3_SCACHE];
-        int done = 0;
-        size_t used;
-        size_t lp;
-        size_t rb = 0;
</del><ins>+        size_t usedlen;
</ins><span class="cx">         
</span><del>-        while (context-&gt;eof &lt; 2 &amp;&amp; switch_buffer_inuse(context-&gt;audio_buffer) &lt; bytes) {
-                lp = sizeof(inbuf);
-                if (!context-&gt;eof &amp;&amp; ((switch_file_read(context-&gt;fd, inbuf, &amp;lp) != SWITCH_STATUS_SUCCESS) || lp == 0)) {
-                        context-&gt;eof++;
-                }
-
-                inlen = (int) lp;
-                in = inbuf;
-
-                out = context-&gt;decode_buf;
-                outlen = (int) sizeof(context-&gt;decode_buf);
</del><ins>+        while (!context-&gt;err &amp;&amp; !context-&gt;eof &amp;&amp; switch_buffer_inuse(context-&gt;audio_buffer) &lt; bytes) {
</ins><span class="cx">                 usedlen = 0;
</span><del>-                x = 0;
</del><span class="cx"> 
</span><del>-                if (lp &lt; bytes) {
-                        done = 1;
-                }
-                
-                do {
-                        if (context-&gt;eof) {
-                                decode_status = mpg123_read(context-&gt;mh, out, outlen, &amp;dlen);
-                        } else {
-                                decode_status = mpg123_decode(context-&gt;mh, in, inlen, out, outlen, &amp;dlen);
-                        }
</del><ins>+                decode_status = mpg123_read(context-&gt;mh, context-&gt;decode_buf, sizeof(context-&gt;decode_buf), &amp;usedlen);
</ins><span class="cx"> 
</span><del>-                        if (context-&gt;err) {
</del><ins>+                if (decode_status == MPG123_NEW_FORMAT) {
+                        continue;
+                } else if (decode_status == MPG123_OK) {
+                        
+                } else if (decode_status == MPG123_DONE || decode_status == MPG123_NEED_MORE) {
+                        context-&gt;eof++;
+                } else if (decode_status == MPG123_ERR || decode_status &gt; 0) {
+                        if (++context-&gt;mp3err &gt;= 5) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Decoder Error!\n&quot;);
+                                context-&gt;eof++;
</ins><span class="cx">                                 goto error;
</span><span class="cx">                         }
</span><del>-
-                        if (!x) {
-                                in = NULL;
-                                inlen = 0;
-                                x++;
-                        }
-
-                        if (decode_status == MPG123_NEW_FORMAT) {
-                                continue;
-                        } else if (decode_status == MPG123_OK) {
-                                usedlen = dlen;
-                                break;
-                        } else if (decode_status == MPG123_DONE || (context-&gt;eof &amp;&amp; decode_status == MPG123_NEED_MORE)) {
-                                context-&gt;eof++;
-                                goto end;
-                        } else if (decode_status == MPG123_ERR || decode_status &gt; 0) {
-                                if (++context-&gt;mp3err &gt;= 5) {
-                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Decoder Error!\n&quot;);
-                                        context-&gt;eof++;
-                                        goto end;
-                                }
-                                continue;
-                        }
-
-                        context-&gt;mp3err = 0;
-                        
-                        usedlen += dlen;
-                        out += dlen;
-                        outlen -= dlen;
-                        dlen = 0;
-
-
-                } while (decode_status != MPG123_NEED_MORE);
-
-                if (context-&gt;audio_buffer) {
-                        switch_buffer_write(context-&gt;audio_buffer, context-&gt;decode_buf, usedlen);
-                } else {
-                        goto error;
</del><ins>+                        continue;
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if (done) {
-                        break;
-                }
-        }
</del><ins>+                context-&gt;mp3err = 0;
</ins><span class="cx"> 
</span><del>-  end:
-        
-        used = switch_buffer_inuse(context-&gt;audio_buffer);
-
-        if (context-&gt;eof || done || used &gt;= bytes) {
-                if (!(rb = switch_buffer_read(context-&gt;audio_buffer, data, bytes))) {
-                        goto error;
-                }
-                return rb;
</del><ins>+                switch_buffer_write(context-&gt;audio_buffer, context-&gt;decode_buf, usedlen);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        return 0;
</del><ins>+        return;
</ins><span class="cx"> 
</span><span class="cx">   error:
</span><span class="cx">         switch_mutex_lock(context-&gt;audio_mutex);
</span><span class="cx">         context-&gt;err++;
</span><span class="cx">         switch_mutex_unlock(context-&gt;audio_mutex);
</span><del>-        return 0;
</del><span class="cx"> }
</span><span class="cx"> 
</span><del>-#define error_check() if (context-&gt;err) goto error;
-
</del><span class="cx"> static size_t stream_callback(void *ptr, size_t size, size_t nmemb, void *data)
</span><span class="cx"> {
</span><span class="cx">         register unsigned int realsize = (unsigned int) (size * nmemb);
</span><span class="cx">         shout_context_t *context = data;
</span><span class="cx">         int decode_status = 0;
</span><del>-        size_t dlen = 0;
-        int x = 0;
-        unsigned char *in;
-        int inlen;
-        unsigned char *out;
-        int outlen;
-        int usedlen;
</del><ins>+        size_t usedlen;
</ins><span class="cx">         uint32_t used, buf_size = 1024 * 128; /* do not make this 64 or less, stutter will ensue after 
</span><span class="cx">                                                  first 64k buffer is dry */
</span><del>-        
-        in = ptr;
-        inlen = realsize;
-        out = context-&gt;decode_buf;
-        outlen = sizeof(context-&gt;decode_buf);
-        usedlen = 0;
</del><span class="cx"> 
</span><del>-        error_check();
-
</del><span class="cx">         if (context-&gt;prebuf) {
</span><span class="cx">                 buf_size = context-&gt;prebuf;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /* make sure we aren't over zealous by slowing down the stream when the buffer is too full */
</span><del>-        for (;;) {
-                error_check();
-
</del><ins>+        while (!context-&gt;err) {
</ins><span class="cx">                 switch_mutex_lock(context-&gt;audio_mutex);
</span><del>-                if (!context-&gt;audio_buffer) {
-                        context-&gt;err++;
-                        break;
-                }
-
</del><span class="cx">                 used = switch_buffer_inuse(context-&gt;audio_buffer);
</span><span class="cx">                 switch_mutex_unlock(context-&gt;audio_mutex);
</span><span class="cx"> 
</span><span class="lines">@@ -448,55 +351,36 @@
</span><span class="cx">                 switch_yield(500000);
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        error_check();
</del><ins>+        if (mpg123_feed(context-&gt;mh, ptr, realsize) != MPG123_OK) {
+                goto error;
+        }
</ins><span class="cx"> 
</span><span class="cx">         do {
</span><del>-                decode_status = mpg123_decode(context-&gt;mh, in, inlen, out, outlen, &amp;dlen);
</del><ins>+                usedlen = 0;
</ins><span class="cx"> 
</span><del>-                error_check();
</del><ins>+                decode_status = mpg123_read(context-&gt;mh, context-&gt;decode_buf, sizeof(context-&gt;decode_buf), &amp;usedlen);
</ins><span class="cx"> 
</span><del>-                if (!x) {
-                        in = NULL;
-                        inlen = 0;
-                        x++;
-                }
-
</del><span class="cx">                 if (decode_status == MPG123_NEW_FORMAT) {
</span><span class="cx">                         continue;
</span><del>-                } else if (decode_status == MPG123_OK) {
-                        usedlen = dlen;
-                        break;
-                } else if (decode_status == MPG123_ERR) {
-
-                        if (++context-&gt;mp3err &gt;= 20) {
</del><ins>+                } else if (decode_status == MPG123_OK || decode_status == MPG123_NEED_MORE) {
+                        
+                } else if (decode_status == MPG123_DONE) {
+                        context-&gt;eof++;
+                } else if (decode_status == MPG123_ERR || decode_status &gt; 0) {
+                        if (++context-&gt;mp3err &gt;= 5) {
</ins><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Decoder Error!\n&quot;);
</span><ins>+                                context-&gt;eof++;
</ins><span class="cx">                                 goto error;
</span><span class="cx">                         }
</span><del>-
-                        mpg123_delete(context-&gt;mh);
-                        context-&gt;mh = our_mpg123_new(NULL, NULL);
-                        mpg123_open_feed(context-&gt;mh);
-                        mpg123_param(context-&gt;mh, MPG123_FORCE_RATE, context-&gt;samplerate, 0);
-                        mpg123_param(context-&gt;mh, MPG123_FLAGS, MPG123_MONO_MIX, 0);
-                        mpg123_param(context-&gt;mh, MPG123_FLAGS, MPG123_SEEKBUFFER|MPG123_MONO_MIX, 0);
-                        return realsize;
</del><ins>+                        continue;
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 context-&gt;mp3err = 0;
</span><del>-                usedlen += dlen;
-                out += dlen;
-                outlen -= dlen;
-                dlen = 0;
-        } while (decode_status != MPG123_NEED_MORE);
</del><span class="cx"> 
</span><del>-
-        switch_mutex_lock(context-&gt;audio_mutex);
-        if (context-&gt;audio_buffer) {
</del><ins>+                switch_mutex_lock(context-&gt;audio_mutex);
</ins><span class="cx">                 switch_buffer_write(context-&gt;audio_buffer, context-&gt;decode_buf, usedlen);
</span><del>-        } else {
-                goto error;
-        }
-        switch_mutex_unlock(context-&gt;audio_mutex);
</del><ins>+                switch_mutex_unlock(context-&gt;audio_mutex);
+        } while (!context-&gt;err &amp;&amp; !context-&gt;eof &amp;&amp; decode_status != MPG123_NEED_MORE);
</ins><span class="cx"> 
</span><span class="cx">         return realsize;
</span><span class="cx"> 
</span><span class="lines">@@ -537,9 +421,7 @@
</span><span class="cx">         curl_easy_cleanup(curl_handle);
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Read Thread Done\n&quot;);
</span><span class="cx"> 
</span><del>-        switch_mutex_lock(context-&gt;audio_mutex);
-        context-&gt;err++;
-        switch_mutex_unlock(context-&gt;audio_mutex);
</del><ins>+        context-&gt;eof++;
</ins><span class="cx">         context-&gt;thread_running = 0;
</span><span class="cx">         switch_thread_rwlock_unlock(context-&gt;rwlock);
</span><span class="cx">         return NULL;
</span><span class="lines">@@ -549,18 +431,31 @@
</span><span class="cx"> {
</span><span class="cx">         switch_thread_t *thread;
</span><span class="cx">         switch_threadattr_t *thd_attr = NULL;
</span><ins>+        int sanity = 10;
+        size_t used;
</ins><span class="cx"> 
</span><del>-        if (context-&gt;err) {
-                return;
-        }
-
</del><span class="cx">         context-&gt;thread_running = 1;
</span><span class="cx">         switch_threadattr_create(&amp;thd_attr, context-&gt;memory_pool);
</span><span class="cx">         switch_threadattr_detach_set(thd_attr, 1);
</span><span class="cx">         switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
</span><span class="cx">         switch_thread_create(&amp;thread, thd_attr, read_stream_thread, context, context-&gt;memory_pool);
</span><ins>+
+        while (context-&gt;thread_running &amp;&amp; --sanity) {
+                /* at least 1s of audio and up to 5s initialize */
+                switch_mutex_lock(context-&gt;audio_mutex);
+                used = switch_buffer_inuse(context-&gt;audio_buffer);
+                switch_mutex_unlock(context-&gt;audio_mutex);
+
+                if (used &gt;= (2 * context-&gt;samplerate)) {
+                         break;
+                }
+
+                switch_yield(500000);
+        }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+#define error_check() if (context-&gt;err) goto error;
+
</ins><span class="cx"> static void *SWITCH_THREAD_FUNC write_stream_thread(switch_thread_t *thread, void *obj)
</span><span class="cx"> {
</span><span class="cx">         shout_context_t *context = (shout_context_t *) obj;
</span><span class="lines">@@ -677,7 +572,6 @@
</span><span class="cx">         char *username, *password, *port;
</span><span class="cx">         char *err = NULL;
</span><span class="cx">         int portno = 0;
</span><del>-        int sanity = 0;
</del><span class="cx"> 
</span><span class="cx">         if ((context = switch_core_alloc(handle-&gt;memory_pool, sizeof(*context))) == 0) {
</span><span class="cx">                 return SWITCH_STATUS_MEMERR;
</span><span class="lines">@@ -695,35 +589,35 @@
</span><span class="cx">         
</span><span class="cx">         switch_thread_rwlock_rdlock(context-&gt;rwlock);
</span><span class="cx"> 
</span><ins>+        switch_mutex_init(&amp;context-&gt;audio_mutex, SWITCH_MUTEX_NESTED, context-&gt;memory_pool);
+
</ins><span class="cx">         if (switch_test_flag(handle, SWITCH_FILE_FLAG_READ)) {
</span><span class="cx">                 if (switch_buffer_create_dynamic(&amp;context-&gt;audio_buffer, TC_BUFFER_SIZE, TC_BUFFER_SIZE * 2, 0) != SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Memory Error!\n&quot;);
</span><span class="cx">                         goto error;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                switch_mutex_init(&amp;context-&gt;audio_mutex, SWITCH_MUTEX_NESTED, context-&gt;memory_pool);
</del><span class="cx">                 context-&gt;mh = our_mpg123_new(NULL, NULL);
</span><del>-                mpg123_open_feed(context-&gt;mh);
</del><span class="cx">                 mpg123_format_all(context-&gt;mh);
</span><span class="cx">                 mpg123_param(context-&gt;mh, MPG123_FORCE_RATE, context-&gt;samplerate, 0);                
</span><span class="cx"> 
</span><span class="cx">                 if (handle-&gt;handler) {
</span><span class="cx">                         mpg123_param(context-&gt;mh, MPG123_FLAGS, MPG123_SEEKBUFFER|MPG123_MONO_MIX, 0);
</span><ins>+                        if (mpg123_open_feed(context-&gt;mh) != MPG123_OK) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error opening mpg feed\n&quot;);
+                                goto error;
+                        }
</ins><span class="cx">                         context-&gt;stream_url = switch_core_sprintf(context-&gt;memory_pool, &quot;http://%s&quot;, path);
</span><span class="cx">                         context-&gt;prebuf = handle-&gt;prebuf;
</span><span class="cx">                         launch_read_stream_thread(context);
</span><del>-                        while((switch_buffer_inuse(context-&gt;audio_buffer) &lt; (switch_size_t)(2 * context-&gt;samplerate)) &amp;&amp; ++sanity &lt; 10) { 
-                                /* at least 1s of audio and up to 5s initialize */
-                                switch_yield(500000);
-                        }
</del><span class="cx">                 } else {
</span><ins>+                        handle-&gt;seekable = 1;
</ins><span class="cx">                         mpg123_param(context-&gt;mh, MPG123_FLAGS, MPG123_MONO_MIX, 0);
</span><del>-                        if (switch_file_open(&amp;context-&gt;fd, path, SWITCH_FOPEN_READ, SWITCH_FPROT_UREAD | SWITCH_FPROT_UWRITE, handle-&gt;memory_pool) != 
-                                SWITCH_STATUS_SUCCESS) {
</del><ins>+                        if (mpg123_open(context-&gt;mh, path) != MPG123_OK) {
</ins><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error opening %s\n&quot;, path);
</span><span class="cx">                                 goto error;
</span><span class="cx">                         }
</span><del>-                        
</del><ins>+
</ins><span class="cx">                 }
</span><span class="cx">         } else if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) {
</span><span class="cx">                 if (!(context-&gt;gfp = lame_init())) {
</span><span class="lines">@@ -759,7 +653,6 @@
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Memory Error!\n&quot;);
</span><span class="cx">                                 goto error;
</span><span class="cx">                         }
</span><del>-                        switch_mutex_init(&amp;context-&gt;audio_mutex, SWITCH_MUTEX_NESTED, context-&gt;memory_pool);
</del><span class="cx">                         lame_set_bWriteVbrTag(context-&gt;gfp, 0);
</span><span class="cx">                         lame_mp3_tags_fid(context-&gt;gfp, NULL);
</span><span class="cx"> 
</span><span class="lines">@@ -850,7 +743,6 @@
</span><span class="cx">                         }
</span><span class="cx"> 
</span><span class="cx">                 } else {
</span><del>-                        handle-&gt;seekable = 1;
</del><span class="cx">                         /* lame being lame and all has FILE * coded into it's API for some functions so we gotta use it */
</span><span class="cx">                         if (!(context-&gt;fp = fopen(path, &quot;wb+&quot;))) {
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error opening %s\n&quot;, path);
</span><span class="lines">@@ -891,29 +783,17 @@
</span><span class="cx"> {
</span><span class="cx">         shout_context_t *context = handle-&gt;private_info;
</span><span class="cx"> 
</span><del>-        if (handle-&gt;handler) {
</del><ins>+        if (handle-&gt;handler || switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) {
</ins><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="cx">         } else {
</span><del>-                switch_mutex_lock(context-&gt;audio_mutex);
-                if (context-&gt;audio_buffer) {
-                        if (context-&gt;fd) {
-                                switch_file_seek(context-&gt;fd, whence, &amp;samples);
-                        } else if (context-&gt;fp) {
-                                *cur_sample = fseek(context-&gt;fp, *cur_sample, whence);
-                        }
</del><ins>+                if (whence == SWITCH_SEEK_CUR) {
+                        samples -= switch_buffer_inuse(context-&gt;audio_buffer) / sizeof(int16_t);
+                }
</ins><span class="cx"> 
</span><del>-                        mpg123_delete(context-&gt;mh);
-                        context-&gt;mh = our_mpg123_new(NULL, NULL);
-                        mpg123_open_feed(context-&gt;mh);
-                        mpg123_param(context-&gt;mh, MPG123_FORCE_RATE, context-&gt;samplerate, 0);
-                        mpg123_param(context-&gt;mh, MPG123_FLAGS, MPG123_MONO_MIX, 0);
-                        switch_buffer_zero(context-&gt;audio_buffer);
</del><ins>+                switch_buffer_zero(context-&gt;audio_buffer);
+                *cur_sample = mpg123_seek (context-&gt;mh, samples, whence);
</ins><span class="cx"> 
</span><del>-                } else {
-                        context-&gt;err++;
-                }
-                switch_mutex_unlock(context-&gt;audio_mutex);
-                return SWITCH_STATUS_SUCCESS;
</del><ins>+                return *cur_sample &gt;= 0 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
</ins><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -924,26 +804,18 @@
</span><span class="cx"> 
</span><span class="cx">         *len = 0;
</span><span class="cx"> 
</span><del>-        if (!context || context-&gt;err) {
</del><ins>+        if (!context) {
</ins><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if (context-&gt;fd) {
-                rb = decode_fd(context, data, bytes);
-        } else {
-                switch_mutex_lock(context-&gt;audio_mutex);
-                if (context-&gt;audio_buffer) {
-                        rb = switch_buffer_read(context-&gt;audio_buffer, data, bytes);
-                } else {
-                        switch_mutex_lock(context-&gt;audio_mutex);
-                        context-&gt;err++;
-                        switch_mutex_unlock(context-&gt;audio_mutex);
-                }
-                switch_mutex_unlock(context-&gt;audio_mutex);
-        }
-        
</del><ins>+        if (!handle-&gt;handler)
+                decode_fd(context, data, bytes);
</ins><span class="cx"> 
</span><del>-        if (context-&gt;err) {
</del><ins>+        switch_mutex_lock(context-&gt;audio_mutex);
+        rb = switch_buffer_read(context-&gt;audio_buffer, data, bytes);
+        switch_mutex_unlock(context-&gt;audio_mutex);
+
+        if (!rb &amp;&amp; (context-&gt;eof || context-&gt;err)) {
</ins><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="cx">         }
</span><span class="cx">         
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>