]> git.sesse.net Git - vlc/blobdiff - src/input/stream.c
Partial rewrite of stats to avoid lookups (Closes:#693)
[vlc] / src / input / stream.c
index 686be34d7337d30386cd6e8cb864e36dc3b9191b..7dddf52dc0751d4e36ef27526bc0159cd5f53b2e 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #include <stdlib.h>
 #include <vlc/vlc.h>
 #include <vlc/input.h>
+#include <assert.h>
 
 #include "input_internal.h"
 
+#undef STREAM_DEBUG
+
 /* TODO:
  *  - tune the 2 methods
  *  - compute cost for seek
@@ -623,7 +626,7 @@ static void AStreamPrebufferBlock( stream_t *s )
             b = b->p_next;
         }
 
-        if( p_access->info.b_prebuffered ) 
+        if( p_access->info.b_prebuffered )
         {
             /* Access has already prebufferred - update stats and exit */
             p_sys->stat.i_bytes = p_sys->block.i_size;
@@ -805,7 +808,7 @@ static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
 
         if( !b_aseek )
         {
-            msg_Err( s, "backward seek impossible (access non seekable)" );
+            msg_Err( s, "backward seeking impossible (access not seekable)" );
             return VLC_EGENERIC;
         }
 
@@ -877,7 +880,7 @@ static int AStreamSeekBlock( stream_t *s, int64_t i_pos )
     }
     else
     {
-        /* Read enought data */
+        /* Read enough data */
         while( p_sys->block.i_start + p_sys->block.i_size < i_pos )
         {
             if( AStreamRefillBlock( s ) )
@@ -997,7 +1000,7 @@ static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
             return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
     }
 
-#if 0
+#ifdef STREAM_DEBUG
     msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
              " offset=%d end="I64Fd,
              i_read, p_sys->i_pos, p_sys->stream.i_tk,
@@ -1052,7 +1055,7 @@ static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
 
     if( tk->i_start >= tk->i_end ) return 0; /* EOF */
 
-#if 0
+#ifdef STREAM_DEBUG
     msg_Dbg( s, "AStreamPeekStream: %d pos="I64Fd" tk=%d "
              "start="I64Fd" offset=%d end="I64Fd,
              i_read, p_sys->i_pos, p_sys->stream.i_tk,
@@ -1115,9 +1118,9 @@ static int AStreamSeekStream( stream_t *s, int64_t i_pos )
     int i_new;
     int i;
 
-#if 0
+#ifdef STREAM_DEBUG
     msg_Dbg( s, "AStreamSeekStream: to "I64Fd" pos="I64Fd
-             "tk=%d start="I64Fd" offset=%d end="I64Fd,
+             " tk=%d start="I64Fd" offset=%d end="I64Fd,
              i_pos, p_sys->i_pos, p_sys->stream.i_tk,
              p_sys->stream.tk[p_sys->stream.i_tk].i_start,
              p_sys->stream.i_offset,
@@ -1129,9 +1132,27 @@ static int AStreamSeekStream( stream_t *s, int64_t i_pos )
     if( i_pos >= p_sys->stream.tk[p_sys->stream.i_tk].i_start &&
         i_pos < p_sys->stream.tk[p_sys->stream.i_tk].i_end )
     {
-        //msg_Dbg( s, "AStreamSeekStream: current track" );
+        stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
+#ifdef STREAM_DEBUG
+        msg_Dbg( s, "AStreamSeekStream: current track" );
+#endif
         p_sys->i_pos = i_pos;
-        p_sys->stream.i_offset = i_pos - p_sys->stream.tk[p_sys->stream.i_tk].i_start;
+        p_sys->stream.i_offset = i_pos - tk->i_start;
+
+        /* If there is not enough data left in the track, refill  */
+        /* \todo How to get a correct value for
+         *    - refilling threshold
+         *    - how much to refill
+         */
+        if( (tk->i_end - tk->i_start ) - p_sys->stream.i_offset <
+                                             p_sys->stream.i_read_size )
+        {
+            if( p_sys->stream.i_used < STREAM_READ_ATONCE / 2  )
+            {
+                p_sys->stream.i_used = STREAM_READ_ATONCE / 2 ;
+                AStreamRefillStream( s );
+            }
+        }
         return VLC_SUCCESS;
     }
 
@@ -1153,10 +1174,11 @@ static int AStreamSeekStream( stream_t *s, int64_t i_pos )
 
         if( i_pos >= tk->i_start && i_pos <= tk->i_end )
         {
-#if 0
+#ifdef STREAM_DEBUG
             msg_Dbg( s, "AStreamSeekStream: reusing %d start="I64Fd
                      " end="I64Fd, i, tk->i_start, tk->i_end );
 #endif
+
             /* Seek at the end of the buffer */
             if( ASeek( s, tk->i_end ) ) return VLC_EGENERIC;
 
@@ -1240,7 +1262,10 @@ static int AStreamRefillStream( stream_t *s )
 
     if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
 
-    /* msg_Dbg( s, "AStreamRefillStream: toread=%d", i_toread ); */
+#ifdef STREAM_DEBUG
+    msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
+                 p_sys->stream.i_used, i_toread );
+#endif
 
     i_start = mdate();
     while( i_toread > 0 )
@@ -1303,7 +1328,7 @@ static void AStreamPrebufferStream( stream_t *s )
         ( (p_access->info.i_title > 1 || p_access->info.i_seekpoint > 1) ?
           STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3 );
 
-    msg_Dbg( s, "pre buffering" );
+    msg_Dbg( s, "pre-buffering..." );
     i_start = mdate();
     for( ;; )
     {
@@ -1323,7 +1348,7 @@ static void AStreamPrebufferStream( stream_t *s )
             i_byterate = ( I64C(1000000) * p_sys->stat.i_bytes ) /
                          (p_sys->stat.i_read_time+1);
 
-            msg_Dbg( s, "prebuffering done "I64Fd" bytes in "I64Fd"s - "
+            msg_Dbg( s, "pre-buffering done "I64Fd" bytes in "I64Fd"s - "
                      I64Fd" kbytes/s",
                      p_sys->stat.i_bytes,
                      p_sys->stat.i_read_time / I64C(1000000),
@@ -1441,6 +1466,7 @@ char * stream_ReadLine( stream_t *s )
             /* Open the converter if we need it */
             if( psz_encoding != NULL )
             {
+                input_thread_t *p_input;
                 msg_Dbg( s, "%s BOM detected", psz_encoding );
                 if( s->i_char_width > 1 )
                 {
@@ -1449,6 +1475,15 @@ char * stream_ReadLine( stream_t *s )
                     {
                         msg_Err( s, "iconv_open failed" );
                     }
+                    var_Create( s->p_parent->p_parent, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+                    var_SetString( s->p_parent->p_parent, "subsdec-encoding", "UTF-8" );
+                }
+                p_input = (input_thread_t *)vlc_object_find( s, VLC_OBJECT_INPUT, FIND_PARENT );
+                if( p_input != NULL)
+                {
+                    var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+                    var_SetString( p_input, "subsdec-encoding", "UTF-8" );
+                    vlc_object_release( p_input );
                 }
                 if( psz_encoding ) free( psz_encoding );
             }
@@ -1496,7 +1531,7 @@ char * stream_ReadLine( stream_t *s )
         if( s->i_char_width > 1 )
         {
             size_t i_in = 0, i_out = 0;
-            char * p_in = NULL;
+            const char * p_in = NULL;
             char * p_out = NULL;
             char * psz_new_line = NULL;
             
@@ -1541,10 +1576,21 @@ static int AReadStream( stream_t *s, void *p_read, int i_read )
     stream_sys_t *p_sys = s->p_sys;
     access_t *p_access = p_sys->p_access;
     int i_read_orig = i_read;
+    int i_total;
+
+    input_thread_t *p_input = (input_thread_t *)s->p_parent->p_parent ;
+    assert( p_input );
 
     if( !p_sys->i_list )
     {
         i_read = p_access->pf_read( p_access, p_read, i_read );
+        vlc_mutex_lock( &p_input->counters.counters_lock );
+        stats_UpdateInteger( s, p_input->counters.p_read_bytes, i_read,
+                             &i_total );
+        stats_UpdateFloat( s, p_input->counters.p_input_bitrate,
+                           (float)i_total, NULL );
+        stats_UpdateInteger( s, p_input->counters.p_read_packets, 1, NULL );
+        vlc_mutex_unlock( &p_input->counters.counters_lock );
         return i_read;
     }
 
@@ -1572,6 +1618,13 @@ static int AReadStream( stream_t *s, void *p_read, int i_read )
         return AReadStream( s, p_read, i_read_orig );
     }
 
+    /* Update read bytes in input */
+    vlc_mutex_lock( &p_input->counters.counters_lock );
+    stats_UpdateInteger( s, p_input->counters.p_read_bytes, i_read, &i_total );
+    stats_UpdateFloat( s, p_input->counters.p_input_bitrate,
+                       (float)i_total, NULL );
+    stats_UpdateInteger( s, p_input->counters.p_read_packets, 1, NULL );
+    vlc_mutex_unlock( &p_input->counters.counters_lock );
     return i_read;
 }
 
@@ -1581,11 +1634,25 @@ static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
     access_t *p_access = p_sys->p_access;
     block_t *p_block;
     vlc_bool_t b_eof;
+    int i_total;
+
+    input_thread_t *p_input = (input_thread_t *)s->p_parent->p_parent ;
+    assert( p_input );
 
     if( !p_sys->i_list )
     {
         p_block = p_access->pf_block( p_access );
         if( pb_eof ) *pb_eof = p_access->info.b_eof;
+        if( p_block && p_access->p_libvlc->b_stats )
+        {
+            vlc_mutex_lock( &p_input->counters.counters_lock );
+            stats_UpdateInteger( s, p_input->counters.p_read_bytes,
+                                 p_block->i_buffer, &i_total );
+            stats_UpdateFloat( s, p_input->counters.p_input_bitrate,
+                              (float)i_total, NULL );
+            stats_UpdateInteger( s, p_input->counters.p_read_packets, 1, NULL );
+            vlc_mutex_unlock( &p_input->counters.counters_lock );
+        }
         return p_block;
     }
 
@@ -1613,7 +1680,17 @@ static block_t *AReadBlock( stream_t *s, vlc_bool_t *pb_eof )
         /* We have to read some data */
         return AReadBlock( s, pb_eof );
     }
-
+    if( p_block )
+    {
+        vlc_mutex_lock( &p_input->counters.counters_lock );
+        stats_UpdateInteger( s, p_input->counters.p_read_bytes,
+                             p_block->i_buffer, &i_total );
+        stats_UpdateFloat( s, p_input->counters.p_input_bitrate,
+                          (float)i_total, NULL );
+        stats_UpdateInteger( s, p_input->counters.p_read_packets,
+                             1 , NULL);
+        vlc_mutex_unlock( &p_input->counters.counters_lock );
+    }
     return p_block;
 }