]> git.sesse.net Git - vlc/blobdiff - src/input/stream.c
Removed useless buffering at stream level.
[vlc] / src / input / stream.c
index 8ddb246158e8b51aa842e8484bb122a05421e608..9870293dbcdc3ff9fb8d873c09c0babfeefdc642 100644 (file)
 #endif
 
 #include <dirent.h>
+#include <assert.h>
 
 #include <vlc_common.h>
 #include <vlc_strings.h>
 #include <vlc_osd.h>
 #include <vlc_charset.h>
 
-#include <assert.h>
+#include <libvlc.h>
+
+#include "access.h"
+#include "stream.h"
 
 #include "input_internal.h"
 
 #   define STREAM_CACHE_SIZE  (4*STREAM_CACHE_TRACK*1024*1024)
 #endif
 
-/* How many data we try to prebuffer */
-#define STREAM_CACHE_PREBUFFER_SIZE (32767)
-/* Maximum time we take to pre-buffer */
-#define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
+/* How many data we try to prebuffer
+ * XXX it should be small to avoid useless latency but big enough for
+ * efficient demux probing */
+#define STREAM_CACHE_PREBUFFER_SIZE (128)
 
 /* Method1: Simple, for pf_block.
  *  We get blocks and put them in the linked list.
@@ -90,7 +94,7 @@
  *        - compute a good value for i_read_size
  *        - ?
  */
-#define STREAM_READ_ATONCE 32767
+#define STREAM_READ_ATONCE 1024
 #define STREAM_CACHE_TRACK_SIZE (STREAM_CACHE_SIZE/STREAM_CACHE_TRACK)
 
 typedef struct
@@ -259,6 +263,15 @@ static inline uint8_t * stream_buffer( stream_t *s )
     return s->p_sys->immediate.p_buffer;
 }
 
+/****************************************************************************
+ * stream_CommonNew: create an empty stream structure
+ ****************************************************************************/
+stream_t *stream_CommonNew( vlc_object_t *p_obj )
+{
+    return (stream_t *)vlc_custom_create( p_obj, sizeof(stream_t),
+                                          VLC_OBJECT_GENERIC, "stream" );
+}
+
 /****************************************************************************
  * stream_UrlNew: create a stream from a access
  ****************************************************************************/
@@ -297,11 +310,19 @@ stream_t *__stream_UrlNew( vlc_object_t *p_parent, const char *psz_url )
 
 stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
 {
-    stream_t *s = vlc_stream_create( VLC_OBJECT(p_access) );
+    stream_t *s = stream_CommonNew( VLC_OBJECT(p_access) );
     stream_sys_t *p_sys;
     char *psz_list = NULL;
 
-    if( !s ) return NULL;
+    if( !s )
+        return NULL;
+
+    s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
+    if( !p_sys )
+    {
+        vlc_object_release( s );
+        return NULL;
+    }
 
     /* Attach it now, needed for b_die */
     vlc_object_attach( s, p_access );
@@ -311,10 +332,6 @@ stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
     s->pf_control = AStreamControl;
     s->pf_destroy = AStreamDestroy;
 
-    s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
-    if( p_sys == NULL )
-        goto error;
-
     /* UTF16 and UTF32 text file conversion */
     s->i_char_width = 1;
     s->b_little_endian = false;
@@ -443,16 +460,12 @@ stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
         p_sys->immediate.i_end = 0;
         p_sys->immediate.p_buffer = malloc( STREAM_CACHE_SIZE );
 
+        if( p_sys->immediate.p_buffer == NULL )
+            goto error;
+
         msg_Dbg( s, "p_buffer %p-%p",
                  p_sys->immediate.p_buffer,
                  &p_sys->immediate.p_buffer[STREAM_CACHE_SIZE] );
-
-        if( p_sys->immediate.p_buffer == NULL )
-        {
-            msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
-                        STREAM_CACHE_SIZE );
-            goto error;
-        }
     }
     else
     {
@@ -470,11 +483,7 @@ stream_t *stream_AccessNew( access_t *p_access, bool b_quick )
         p_sys->stream.i_tk     = 0;
         p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
         if( p_sys->stream.p_buffer == NULL )
-        {
-            msg_Err( s, "Out of memory when allocating stream cache (%d bytes)",
-                        STREAM_CACHE_SIZE );
             goto error;
-        }
         p_sys->stream.i_used   = 0;
         access_Control( p_access, ACCESS_GET_MTU,
                          &p_sys->stream.i_read_size );
@@ -836,12 +845,11 @@ static void AStreamPrebufferBlock( stream_t *s )
     i_start = mdate();
     for( ;; )
     {
-        int64_t i_date = mdate();
+        const int64_t i_date = mdate();
         bool b_eof;
         block_t *b;
 
-        if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE ||
-            ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
+        if( s->b_die || p_sys->block.i_size > STREAM_CACHE_PREBUFFER_SIZE )
         {
             int64_t i_byterate;
 
@@ -878,20 +886,12 @@ static void AStreamPrebufferBlock( stream_t *s )
             b = b->p_next;
         }
 
-        if( p_access->info.b_prebuffered )
-        {
-            /* Access has already prebufferred - update stats and exit */
-            p_sys->stat.i_bytes = p_sys->block.i_size;
-            p_sys->stat.i_read_time = mdate() - i_start;
-            break;
-        }
-
         if( i_first == 0 )
         {
             i_first = mdate();
-            msg_Dbg( s, "received first data for our buffer");
+            msg_Dbg( s, "received first data after %d ms",
+                     (int)((i_first-i_start)/1000) );
         }
-
     }
 
     p_sys->block.p_current = p_sys->block.p_first;
@@ -1306,6 +1306,9 @@ static int AStreamReadStream( stream_t *s, void *p_read, unsigned int i_read )
         if( tk->i_start + p_sys->stream.i_offset >= tk->i_end ||
             p_sys->stream.i_used >= p_sys->stream.i_read_size )
         {
+            if( p_sys->stream.i_used < i_read - i_data )
+                p_sys->stream.i_used = __MIN( i_read - i_data, STREAM_READ_ATONCE * 10 );
+
             if( AStreamRefillStream( s ) )
             {
                 /* EOF */
@@ -1540,10 +1543,10 @@ static int AStreamRefillStream( stream_t *s )
 
     if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
 
-#ifdef STREAM_DEBUG
+//#ifdef STREAM_DEBUG
     msg_Dbg( s, "AStreamRefillStream: used=%d toread=%d",
                  p_sys->stream.i_used, i_toread );
-#endif
+//#endif
 
     i_start = mdate();
     while( i_toread > 0 )
@@ -1602,11 +1605,8 @@ static void AStreamPrebufferStream( stream_t *s )
 
     int64_t i_first = 0;
     int64_t i_start;
-    int64_t i_prebuffer = p_sys->b_quick ? STREAM_CACHE_TRACK_SIZE /100 :
-        ( (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( ;; )
     {
@@ -1615,8 +1615,7 @@ static void AStreamPrebufferStream( stream_t *s )
         int64_t i_date = mdate();
         int i_read;
 
-        if( s->b_die || tk->i_end >= i_prebuffer ||
-            (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
+        if( s->b_die || tk->i_end >= STREAM_CACHE_PREBUFFER_SIZE )
         {
             int64_t i_byterate;
 
@@ -1646,7 +1645,8 @@ static void AStreamPrebufferStream( stream_t *s )
         if( i_first == 0 )
         {
             i_first = mdate();
-            msg_Dbg( s, "received first data for our buffer");
+            msg_Dbg( s, "received first data after %d ms",
+                     (int)((i_first-i_start)/1000) );
         }
 
         tk->i_end += i_read;
@@ -2104,7 +2104,7 @@ static block_t *AReadBlock( stream_t *s, bool *pb_eof )
         return p_block;
     }
 
-    p_block = p_sys->p_list_access->pf_block( p_access );
+    p_block = p_sys->p_list_access->pf_block( p_sys->p_list_access );
     if( p_access->b_die )
         vlc_object_kill( s );
     b_eof = p_sys->p_list_access->info.b_eof;