]> git.sesse.net Git - vlc/blobdiff - src/input/stream.c
stream: remove shadow variable
[vlc] / src / input / stream.c
index b2a5ac61390f97b0e9af3c13527529537afe023b..42cd823bf5e1a918605278872610b24ebc7b8b7c 100644 (file)
@@ -116,7 +116,8 @@ typedef struct
 typedef enum
 {
     STREAM_METHOD_BLOCK,
-    STREAM_METHOD_STREAM
+    STREAM_METHOD_STREAM,
+    STREAM_METHOD_READDIR
 } stream_read_method_t;
 
 struct stream_sys_t
@@ -197,7 +198,11 @@ static int  AStreamSeekStream( stream_t *s, uint64_t i_pos );
 static void AStreamPrebufferStream( stream_t *s );
 static int  AReadStream( stream_t *s, void *p_read, unsigned int i_read );
 
+/* ReadDir */
+static int  AStreamReadDir( stream_t *s, input_item_node_t *p_node );
+
 /* Common */
+static int  AStreamGenericError( ) { return VLC_EGENERIC; }
 static int AStreamControl( stream_t *s, int i_query, va_list );
 static void AStreamDestroy( stream_t *s );
 static int  ASeek( stream_t *s, uint64_t i_pos );
@@ -285,17 +290,21 @@ stream_t *stream_AccessNew( access_t *p_access, char **ppsz_list )
         return NULL;
     }
 
-    s->pf_read   = NULL;    /* Set up later */
-    s->pf_peek   = NULL;
+    s->pf_read    = AStreamGenericError;    /* Replaced later */
+    s->pf_peek    = AStreamGenericError;
+    s->pf_readdir = AStreamGenericError;
     s->pf_control = AStreamControl;
     s->pf_destroy = AStreamDestroy;
 
     /* Common field */
     p_sys->p_access = p_access;
+    assert( p_access->pf_block || p_access->pf_read || p_access->pf_readdir );
     if( p_access->pf_block )
         p_sys->method = STREAM_METHOD_BLOCK;
-    else
+    else if( p_access->pf_read )
         p_sys->method = STREAM_METHOD_STREAM;
+    else
+        p_sys->method = STREAM_METHOD_READDIR;
 
     p_sys->i_pos = p_access->info.i_pos;
 
@@ -340,7 +349,10 @@ stream_t *stream_AccessNew( access_t *p_access, char **ppsz_list )
             access_t *p_tmp = access_New( p_access, p_access->p_input,
                                           p_access->psz_access, "", psz_name );
             if( !p_tmp )
+            {
+                free( psz_name );
                 continue;
+            }
 
             p_entry = malloc( sizeof(*p_entry) );
             if( p_entry )
@@ -382,12 +394,10 @@ stream_t *stream_AccessNew( access_t *p_access, char **ppsz_list )
             goto error;
         }
     }
-    else
+    else if ( p_sys->method == STREAM_METHOD_STREAM )
     {
         int i;
 
-        assert( p_sys->method == STREAM_METHOD_STREAM );
-
         msg_Dbg( s, "Using stream method for AStream*" );
 
         s->pf_read = AStreamReadStream;
@@ -423,6 +433,13 @@ stream_t *stream_AccessNew( access_t *p_access, char **ppsz_list )
             goto error;
         }
     }
+    else
+    {
+        msg_Dbg( s, "Using readdir method for AStream*" );
+
+        assert( p_sys->method == STREAM_METHOD_READDIR );
+        s->pf_readdir = AStreamReadDir;
+    }
 
     return s;
 
@@ -431,7 +448,7 @@ error:
     {
         /* Nothing yet */
     }
-    else
+    else if( p_sys->method == STREAM_METHOD_STREAM )
     {
         free( p_sys->stream.p_buffer );
     }
@@ -453,7 +470,7 @@ static void AStreamDestroy( stream_t *s )
 
     if( p_sys->method == STREAM_METHOD_BLOCK )
         block_ChainRelease( p_sys->block.p_first );
-    else
+    else if( p_sys->method == STREAM_METHOD_STREAM )
         free( p_sys->stream.p_buffer );
 
     free( p_sys->p_peek );
@@ -550,19 +567,23 @@ static int AStreamControl( stream_t *s, int i_query, va_list args )
     stream_sys_t *p_sys = s->p_sys;
     access_t     *p_access = p_sys->p_access;
 
-    uint64_t *pi_64, i_64;
-
     static_control_match(CAN_SEEK);
     static_control_match(CAN_FASTSEEK);
     static_control_match(CAN_PAUSE);
     static_control_match(CAN_CONTROL_PACE);
+    static_control_match(GET_PTS_DELAY);
     static_control_match(GET_TITLE_INFO);
+    static_control_match(GET_TITLE);
+    static_control_match(GET_SEEKPOINT);
     static_control_match(GET_META);
     static_control_match(GET_CONTENT_TYPE);
     static_control_match(GET_SIGNAL);
     static_control_match(SET_PAUSE_STATE);
     static_control_match(SET_TITLE);
     static_control_match(SET_SEEKPOINT);
+    static_control_match(SET_PRIVATE_ID_STATE);
+    static_control_match(SET_PRIVATE_ID_CA);
+    static_control_match(GET_PRIVATE_ID_STATE);
 
     switch( i_query )
     {
@@ -570,15 +591,22 @@ static int AStreamControl( stream_t *s, int i_query, va_list args )
         case STREAM_CAN_FASTSEEK:
         case STREAM_CAN_PAUSE:
         case STREAM_CAN_CONTROL_PACE:
+        case STREAM_GET_PTS_DELAY:
         case STREAM_GET_TITLE_INFO:
+        case STREAM_GET_TITLE:
+        case STREAM_GET_SEEKPOINT:
         case STREAM_GET_META:
         case STREAM_GET_CONTENT_TYPE:
         case STREAM_GET_SIGNAL:
         case STREAM_SET_PAUSE_STATE:
+        case STREAM_SET_PRIVATE_ID_STATE:
+        case STREAM_SET_PRIVATE_ID_CA:
+        case STREAM_GET_PRIVATE_ID_STATE:
             return access_vaControl( p_access, i_query, args );
 
         case STREAM_GET_SIZE:
-            pi_64 = va_arg( args, uint64_t * );
+        {
+            uint64_t *pi_64 = va_arg( args, uint64_t * );
             if( s->p_sys->i_list )
             {
                 int i;
@@ -589,37 +617,25 @@ static int AStreamControl( stream_t *s, int i_query, va_list args )
             }
             *pi_64 = access_GetSize( p_access );
             break;
+        }
 
         case STREAM_GET_POSITION:
-            pi_64 = va_arg( args, uint64_t * );
-            *pi_64 = p_sys->i_pos;
+            *va_arg( args, uint64_t * ) = p_sys->i_pos;
             break;
 
         case STREAM_SET_POSITION:
-            i_64 = va_arg( args, uint64_t );
+        {
+            uint64_t offset = va_arg( args, uint64_t );
             switch( p_sys->method )
             {
             case STREAM_METHOD_BLOCK:
-                return AStreamSeekBlock( s, i_64 );
+                return AStreamSeekBlock( s, offset );
             case STREAM_METHOD_STREAM:
-                return AStreamSeekStream( s, i_64 );
+                return AStreamSeekStream( s, offset );
             default:
                 assert(0);
                 return VLC_EGENERIC;
             }
-
-        case STREAM_CONTROL_ACCESS:
-        {
-            int i_int = (int) va_arg( args, int );
-            if( i_int != ACCESS_SET_PRIVATE_ID_STATE &&
-                i_int != ACCESS_SET_PRIVATE_ID_CA &&
-                i_int != ACCESS_GET_PRIVATE_ID_STATE )
-            {
-                msg_Err( s, "Hey, what are you thinking ?"
-                            "DON'T USE STREAM_CONTROL_ACCESS !!!" );
-                return VLC_EGENERIC;
-            }
-            return access_vaControl( p_access, i_int, args );
         }
 
         case STREAM_UPDATE_SIZE:
@@ -635,6 +651,13 @@ static int AStreamControl( stream_t *s, int i_query, va_list args )
             return ret;
         }
 
+        case STREAM_IS_DIRECTORY:
+        {
+            bool *pb_canreaddir = va_arg( args, bool * );
+            *pb_canreaddir = p_sys->method == STREAM_METHOD_READDIR;
+            return VLC_SUCCESS;
+        }
+
         case STREAM_SET_RECORD_STATE:
         default:
             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
@@ -668,13 +691,13 @@ static void AStreamPrebufferBlock( stream_t *s )
             /* Update stat */
             p_sys->stat.i_bytes = p_sys->block.i_size;
             p_sys->stat.i_read_time = i_date - i_start;
-            i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
+            i_byterate = ( CLOCK_FREQ * p_sys->stat.i_bytes ) /
                          (p_sys->stat.i_read_time + 1);
 
             msg_Dbg( s, "prebuffering done %"PRId64" bytes in %"PRId64"s - "
                      "%"PRId64" KiB/s",
                      p_sys->stat.i_bytes,
-                     p_sys->stat.i_read_time / INT64_C(1000000),
+                     p_sys->stat.i_read_time / CLOCK_FREQ,
                      i_byterate / 1024 );
             break;
         }
@@ -725,9 +748,9 @@ static int AStreamReadBlock( stream_t *s, void *p_read, unsigned int i_read )
     if( p_data == NULL )
     {
         /* seek within this stream if possible, else use plain old read and discard */
-        stream_sys_t *p_sys = s->p_sys;
-        access_t     *p_access = p_sys->p_access;
-        bool   b_aseek;
+        access_t *p_access = p_sys->p_access;
+        bool b_aseek;
+
         access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
         if( b_aseek )
             return AStreamSeekBlock( s, p_sys->i_pos + i_read ) ? 0 : i_read;
@@ -971,7 +994,6 @@ static int AStreamSeekBlock( stream_t *s, uint64_t i_pos )
 static int AStreamRefillBlock( stream_t *s )
 {
     stream_sys_t *p_sys = s->p_sys;
-    block_t      *b;
 
     /* Release data */
     while( p_sys->block.i_size >= STREAM_CACHE_SIZE &&
@@ -995,6 +1017,8 @@ static int AStreamRefillBlock( stream_t *s )
 
     /* Now read a new block */
     const int64_t i_start = mdate();
+    block_t *b;
+
     for( ;; )
     {
         bool b_eof;
@@ -1423,13 +1447,13 @@ static void AStreamPrebufferStream( stream_t *s )
             /* Update stat */
             p_sys->stat.i_bytes = i_buffered;
             p_sys->stat.i_read_time = i_date - i_start;
-            i_byterate = ( INT64_C(1000000) * p_sys->stat.i_bytes ) /
+            i_byterate = ( CLOCK_FREQ * p_sys->stat.i_bytes ) /
                          (p_sys->stat.i_read_time+1);
 
             msg_Dbg( s, "pre-buffering done %"PRId64" bytes in %"PRId64"s - "
                      "%"PRId64" KiB/s",
                      p_sys->stat.i_bytes,
-                     p_sys->stat.i_read_time / INT64_C(1000000),
+                     p_sys->stat.i_read_time / CLOCK_FREQ,
                      i_byterate / 1024 );
             break;
         }
@@ -1471,6 +1495,10 @@ char *stream_ReadLine( stream_t *s )
     char *p_line = NULL;
     int i_line = 0, i_read = 0;
 
+    /* Let's fail quickly if this is a readdir access */
+    if( s->pf_read == NULL )
+        return NULL;
+
     for( ;; )
     {
         char *psz_eol;
@@ -1828,6 +1856,12 @@ static int ASeek( stream_t *s, uint64_t i_pos )
     return p_access->pf_seek( p_access, i_pos );
 }
 
+static int AStreamReadDir( stream_t *s, input_item_node_t *p_node )
+{
+    access_t *p_access = s->p_sys->p_access;
+
+    return p_access->pf_readdir( p_access, p_node );
+}
 
 /**
  * Try to read "i_read" bytes into a buffer pointed by "p_read".  If
@@ -1960,3 +1994,11 @@ block_t *stream_BlockRemaining( stream_t *s, int i_max_size )
     return p_block;
 }
 
+/**
+ * Returns a node containing all the input_item of the directory pointer by
+ * this stream. returns VLC_SUCCESS on success.
+ */
+int stream_ReadDir( stream_t *s, input_item_node_t *p_node )
+{
+    return s->pf_readdir( s, p_node );
+}