[vlc-devel] [PATCH 3/9] preparser: read item directly from the current thread

Thomas Guillem thomas at gllm.fr
Wed Jun 1 13:24:47 CEST 2016


This avoid creating a new thread each time the preparser is parsing an item.
---
 src/playlist/preparser.c | 63 +++++++++++++++++++-----------------------------
 1 file changed, 25 insertions(+), 38 deletions(-)

diff --git a/src/playlist/preparser.c b/src/playlist/preparser.c
index 5315dc4..b61050c 100644
--- a/src/playlist/preparser.c
+++ b/src/playlist/preparser.c
@@ -25,6 +25,8 @@
 # include "config.h"
 #endif
 
+#include <assert.h>
+
 #include <vlc_common.h>
 
 #include "fetcher.h"
@@ -47,9 +49,10 @@ struct playlist_preparser_t
     vlc_object_t        *object;
     playlist_fetcher_t  *p_fetcher;
 
+    input_thread_t      *input;
+
     vlc_mutex_t     lock;
     vlc_cond_t      wait;
-    vlc_sem_t       item_done;
     bool            b_live;
     preparser_entry_t  **pp_waiting;
     int             i_waiting;
@@ -66,6 +69,7 @@ playlist_preparser_t *playlist_preparser_New( vlc_object_t *parent )
     if( !p_preparser )
         return NULL;
 
+    p_preparser->input = NULL;
     p_preparser->object = parent;
     p_preparser->p_fetcher = playlist_fetcher_New( parent );
     if( unlikely(p_preparser->p_fetcher == NULL) )
@@ -73,7 +77,6 @@ playlist_preparser_t *playlist_preparser_New( vlc_object_t *parent )
 
     vlc_mutex_init( &p_preparser->lock );
     vlc_cond_init( &p_preparser->wait );
-    vlc_sem_init( &p_preparser->item_done, 0 );
     p_preparser->b_live = false;
     p_preparser->i_waiting = 0;
     p_preparser->pp_waiting = NULL;
@@ -125,14 +128,14 @@ void playlist_preparser_Delete( playlist_preparser_t *p_preparser )
         REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
     }
 
-    vlc_sem_post( &p_preparser->item_done );
+    if( p_preparser->input != NULL )
+        input_Stop( p_preparser->input );
 
     while( p_preparser->b_live )
         vlc_cond_wait( &p_preparser->wait, &p_preparser->lock );
     vlc_mutex_unlock( &p_preparser->lock );
 
     /* Destroy the item preparser */
-    vlc_sem_destroy( &p_preparser->item_done );
     vlc_cond_destroy( &p_preparser->wait );
     vlc_mutex_destroy( &p_preparser->lock );
 
@@ -145,19 +148,6 @@ void playlist_preparser_Delete( playlist_preparser_t *p_preparser )
  * Privates functions
  *****************************************************************************/
 
-static int InputEvent( vlc_object_t *obj, const char *varname,
-                       vlc_value_t old, vlc_value_t cur, void *data )
-{
-    vlc_sem_t *done = data;
-    int event = cur.i_int;
-
-    if( event == INPUT_EVENT_DEAD )
-        vlc_sem_post( done );
-
-    (void) obj; (void) varname; (void) old;
-    return VLC_SUCCESS;
-}
-
 /**
  * This function preparses an item when needed.
  */
@@ -183,25 +173,25 @@ static void Preparse( playlist_preparser_t *preparser, input_item_t *p_item,
     /* Do not preparse if it is already done (like by playing it) */
     if( b_preparse && !input_item_IsPreparsed( p_item ) )
     {
-        input_thread_t *input = input_CreatePreparser( preparser->object,
-                                                       p_item );
-        if( input == NULL )
+        preparser->input = input_CreatePreparser( preparser->object, p_item );
+        if( preparser->input == NULL )
+        {
+            input_item_SignalPreparseEnded( p_item, ITEM_PREPARSE_FAILED );
+            return;
+        }
+
+        vlc_mutex_unlock( &preparser->lock );
+        int i_ret = input_Read( preparser->input, true );
+        vlc_mutex_lock( &preparser->lock );
+
+        input_Close( preparser->input );
+        preparser->input = NULL;
+        if( i_ret != VLC_SUCCESS )
         {
             input_item_SignalPreparseEnded( p_item, ITEM_PREPARSE_FAILED );
             return;
         }
 
-        var_AddCallback( input, "intf-event", InputEvent,
-                         &preparser->item_done );
-        if( input_Start( input ) == VLC_SUCCESS )
-            vlc_sem_wait( &preparser->item_done );
-        var_DelCallback( input, "intf-event", InputEvent,
-                         &preparser->item_done );
-        /* Normally, the input is already stopped since we waited for it. But
-         * if the playlist preparser is being deleted, then the input might
-         * still be running. Force it to stop. */
-        input_Stop( input );
-        input_Close( input );
 
         var_SetAddress( preparser->object, "item-change", p_item );
         input_item_SetPreparsed( p_item, true );
@@ -209,7 +199,6 @@ static void Preparse( playlist_preparser_t *preparser, input_item_t *p_item,
     }
     else if (!b_preparse)
         input_item_SignalPreparseEnded( p_item, ITEM_PREPARSE_SKIPPED );
-
 }
 
 /**
@@ -260,13 +249,13 @@ static void *Thread( void *data )
 {
     playlist_preparser_t *p_preparser = data;
 
+    vlc_mutex_lock( &p_preparser->lock );
     for( ;; )
     {
         input_item_t *p_current;
         input_item_meta_request_option_t i_options;
 
         /* */
-        vlc_mutex_lock( &p_preparser->lock );
         if( p_preparser->i_waiting > 0 )
         {
             preparser_entry_t *p_entry = p_preparser->pp_waiting[0];
@@ -280,17 +269,15 @@ static void *Thread( void *data )
             p_current = NULL;
             p_preparser->b_live = false;
             vlc_cond_signal( &p_preparser->wait );
-        }
-        vlc_mutex_unlock( &p_preparser->lock );
-
-        if( !p_current )
             break;
+        }
+        assert( p_current );
 
         Preparse( p_preparser, p_current, i_options );
 
         Art( p_preparser, p_current );
         vlc_gc_decref(p_current);
     }
+    vlc_mutex_unlock( &p_preparser->lock );
     return NULL;
 }
-
-- 
2.8.1



More information about the vlc-devel mailing list