]> git.sesse.net Git - vlc/blobdiff - src/input/stream.c
* ALL: Major rework of the subpictures architecture.
[vlc] / src / input / stream.c
index 97fe2285d00361c6e3ae1379aaba5e573d81ccdd..762e0fa1a7d3ec67e5c76541487051f233253212 100644 (file)
 #define STREAM_CACHE_SIZE  (4*STREAM_CACHE_TRACK*1024*1024)
   /* How many data we try to prebuffer */
 #define STREAM_CACHE_PREBUFFER_SIZE (32767)
-
-#define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)    /* Maximum time we take to pre-buffer */
+/* Maximum time we take to pre-buffer */
+#define STREAM_CACHE_PREBUFFER_LENGTH (100*1000)
 
 
 /* Method1: Simple, for pf_block.
  *  We get blocks and put them in the linked list.
  *  We release blocks once the total size is bigger than CACHE_BLOCK_SIZE
  */
-#define STREAM_DATA_WAIT 40000                     /* Time between before a pf_block retry */
+#define STREAM_DATA_WAIT 40000       /* Time between before a pf_block retry */
 
 /* Method2: A bit more complex, for pf_read
  *  - We use ring buffers, only one if unseekable, all if seekable
@@ -106,7 +106,7 @@ struct stream_sys_t
     /* Method 2: for pf_read */
     struct
     {
-        int i_offset;   /* Buffer ofset in the current track */
+        int i_offset;   /* Buffer offset in the current track */
         int i_tk;       /* Current track */
         stream_track_t tk[STREAM_CACHE_TRACK];
 
@@ -226,7 +226,8 @@ stream_t *stream_AccessNew( access_t *p_access )
         p_sys->stream.i_tk     = 0;
         p_sys->stream.p_buffer = malloc( STREAM_CACHE_SIZE );
         p_sys->stream.i_used   = 0;
-        access2_Control( p_access, ACCESS_GET_MTU, &p_sys->stream.i_read_size );
+        access2_Control( p_access, ACCESS_GET_MTU,
+                         &p_sys->stream.i_read_size );
         if( p_sys->stream.i_read_size <= 0 )
             p_sys->stream.i_read_size = STREAM_READ_ATONCE;
         else if( p_sys->stream.i_read_size <= 256 )
@@ -449,7 +450,10 @@ static void AStreamPrebufferBlock( stream_t *s )
         }
 
         if( i_first == 0 )
+        {
             i_first = mdate();
+            msg_Dbg( s, "received first data for our buffer");
+        }
 
         /* Append the block */
         p_sys->block.i_size += b->i_buffer;
@@ -475,15 +479,21 @@ static int AStreamReadBlock( stream_t *s, void *p_read, int i_read )
     if( p_sys->block.p_current == NULL )
         return 0;
 
+    if( p_read == NULL )
+        return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
+
     while( i_data < i_read )
     {
-        int i_current = p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
+        int i_current =
+            p_sys->block.p_current->i_buffer - p_sys->block.i_offset;
         int i_copy = __MIN( i_current, i_read - i_data);
 
         /* Copy data */
         if( p_data )
         {
-            memcpy( p_data, &p_sys->block.p_current->p_buffer[p_sys->block.i_offset], i_copy );
+            memcpy( p_data,
+                    &p_sys->block.p_current->p_buffer[p_sys->block.i_offset],
+                    i_copy );
             p_data += i_copy;
         }
         i_data += i_copy;
@@ -536,7 +546,8 @@ static int AStreamPeekBlock( stream_t *s, uint8_t **pp_peek, int i_read )
     }
 
     /* Fill enough data */
-    while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start) < i_read )
+    while( p_sys->block.i_size - (p_sys->i_pos - p_sys->block.i_start)
+           < i_read )
     {
         block_t **pp_last = p_sys->block.pp_last;
 
@@ -785,8 +796,10 @@ static int AStreamReadStream( stream_t *s, void *p_read, int i_read )
     uint8_t *p_data = (uint8_t *)p_read;
     int      i_data = 0;
 
-    if( tk->i_start >= tk->i_end  )
-        return 0;   /* EOF */
+    if( tk->i_start >= tk->i_end ) return 0; /* EOF */
+
+    if( p_read == NULL )
+        return AStreamSeekStream( s, p_sys->i_pos + i_read ) ? 0 : i_read;
 
 #if 0
     msg_Dbg( s, "AStreamReadStream: %d pos="I64Fd" tk=%d start="I64Fd
@@ -856,7 +869,13 @@ static int AStreamPeekStream( stream_t *s, uint8_t **pp_peek, int i_read )
 
     while( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
     {
-        if( AStreamRefillStream( s ) ) break;
+        if( p_sys->stream.i_used <= 1 )
+        {
+            /* Be sure we will read something */
+            p_sys->stream.i_used += i_read - (tk->i_end - tk->i_start - p_sys->stream.i_offset);
+        }
+        if( AStreamRefillStream( s ) )
+            break;
     }
 
     if( tk->i_end - tk->i_start - p_sys->stream.i_offset < i_read )
@@ -1015,8 +1034,10 @@ static int AStreamRefillStream( stream_t *s )
     stream_track_t *tk = &p_sys->stream.tk[p_sys->stream.i_tk];
 
     /* We read but won't increase i_start after initial start + offset */
-    int i_toread = __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
-                          (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
+    int i_toread =
+        __MIN( p_sys->stream.i_used, STREAM_CACHE_TRACK_SIZE -
+               (tk->i_end - tk->i_start - p_sys->stream.i_offset) );
+    vlc_bool_t b_read = VLC_FALSE;
     int64_t i_start, i_stop;
 
     if( i_toread <= 0 ) return VLC_EGENERIC; /* EOF */
@@ -1043,8 +1064,11 @@ static int AStreamRefillStream( stream_t *s )
         }
         else if( i_read == 0 )
         {
-            return VLC_EGENERIC;
+            if( !b_read )
+                return VLC_EGENERIC;
+            return VLC_SUCCESS;
         }
+        b_read = VLC_TRUE;
 
         /* Update end */
         tk->i_end += i_read;
@@ -1058,7 +1082,6 @@ static int AStreamRefillStream( stream_t *s )
             p_sys->stream.i_offset -= i_invalid;
         }
 
-
         i_toread -= i_read;
         p_sys->stream.i_used -= i_read;
 
@@ -1079,6 +1102,8 @@ static void AStreamPrebufferStream( stream_t *s )
 
     int64_t i_first = 0;
     int64_t i_start;
+    int64_t i_prebuffer = (s->p_sys->p_access->info.i_title > 1 ||
+                           s->p_sys->p_access->info.i_seekpoint > 1) ? STREAM_CACHE_PREBUFFER_SIZE : STREAM_CACHE_TRACK_SIZE / 3;
 
     msg_Dbg( s, "pre buffering" );
     i_start = mdate();
@@ -1089,9 +1114,8 @@ static void AStreamPrebufferStream( stream_t *s )
         int64_t i_date = mdate();
         int i_read;
 
-        if( s->b_die ||
-            tk->i_end >= STREAM_CACHE_PREBUFFER_SIZE ||
-            ( i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date ) )
+        if( s->b_die || tk->i_end >= i_prebuffer ||
+            (i_first > 0 && i_first + STREAM_CACHE_PREBUFFER_LENGTH < i_date) )
         {
             int64_t i_byterate;
 
@@ -1126,7 +1150,10 @@ static void AStreamPrebufferStream( stream_t *s )
         }
 
         if( i_first == 0 )
+        {
             i_first = mdate();
+            msg_Dbg( s, "received first data for our buffer");
+        }
 
         tk->i_end += i_read;
 
@@ -1143,36 +1170,56 @@ static void AStreamPrebufferStream( stream_t *s )
  * \param s Stream handle to read from
  * \return A null-terminated string. This must be freed,
  */
-/* FIXME don't use stupid MAX_LINE -> do the same than net_ReadLine */
-#define MAX_LINE 1024
+#define STREAM_PROBE_LINE 2048
+#define STREAM_LINE_MAX (2048*100)
 char *stream_ReadLine( stream_t *s )
 {
-    uint8_t *p_data;
-    char    *p_line;
-    int      i_data;
-    int      i = 0;
-    i_data = stream_Peek( s, &p_data, MAX_LINE );
+    char *p_line = NULL;
+    int i_line = 0, i_read = 0;
 
-    while( i < i_data && p_data[i] != '\n' &&  p_data[i] != '\r' )
-    {
-        i++;
-    }
-    if( i_data <= 0 )
+    while( i_read < STREAM_LINE_MAX )
     {
-        return NULL;
-    }
-    else
-    {
-        p_line = malloc( i + 1 );
-        if( p_line == NULL )
+        char *psz_eol;
+        uint8_t *p_data;
+        int i_data;
+
+        /* Probe new data */
+        i_data = stream_Peek( s, &p_data, STREAM_PROBE_LINE );
+        if( i_data <= 0 ) break; /* No more data */
+
+        /* Check if there is an EOL */
+        if( ( psz_eol = memchr( p_data, '\n', i_data ) ) )
         {
-            msg_Err( s, "out of memory" );
-            return NULL;
+            i_data = (psz_eol - (char *)p_data) + 1;
+            p_line = realloc( p_line, i_line + i_data + 1 );
+            i_data = stream_Read( s, &p_line[i_line], i_data );
+            if( i_data <= 0 ) break; /* Hmmm */
+            i_line += (i_data - 1);
+            i_read += i_data;
+
+            /* We have our line */
+            break;
         }
-        i = stream_Read( s, p_line, i + 1 );
-        p_line[ i - 1 ] = '\0';
 
+        /* Read data (+1 for easy \0 append) */
+        p_line = realloc( p_line, i_line + STREAM_PROBE_LINE + 1 );
+        i_data = stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
+        if( i_data <= 0 ) break; /* Hmmm */
+        i_line += i_data;
+        i_read += i_data;
+    }
+
+    /* Remove trailing LF/CR */
+    while( i_line > 0 && ( p_line[i_line-1] == '\r' ||
+           p_line[i_line-1] == '\n') ) i_line--;
+
+    if( i_read > 0 )
+    {
+        p_line[i_line] = '\0';
         return p_line;
     }
-}
 
+    /* We failed to read any data, probably EOF */
+    if( p_line ) free( p_line );
+    return NULL;
+}