]> git.sesse.net Git - vlc/blobdiff - src/libvlc-common.c
new (failing) test for libvlc
[vlc] / src / libvlc-common.c
index 3f7a9d124c3e34c8f46d16cfc7129521354673ce..7a29a70c4e899db3366d4305b9176534a5527fa6 100644 (file)
@@ -93,6 +93,8 @@
 
 #include "playlist/playlist_internal.h"
 
+#include <vlc_vlm.h>
+
 /*****************************************************************************
  * The evil global variable. We handle it with care, don't worry.
  *****************************************************************************/
@@ -128,7 +130,7 @@ static void InitDeviceValues( libvlc_int_t * );
 
 libvlc_global_data_t *vlc_global( void )
 {
-    return &libvlc_global;
+    return p_libvlc_global;
 }
 
 /*****************************************************************************
@@ -151,39 +153,29 @@ libvlc_int_t * libvlc_InternalCreate( void )
 {
     int i_ret;
     libvlc_int_t * p_libvlc = NULL;
-    vlc_value_t lockval;
     char *psz_env = NULL;
 
-#if 0
-    /* &libvlc_global never changes,
-     * so we can safely call this multiple times. */
-    p_libvlc_global = &libvlc_global;
-#endif
-
     /* vlc_threads_init *must* be the first internal call! No other call is
      * allowed before the thread system has been initialized. */
     i_ret = vlc_threads_init( p_libvlc_global );
     if( i_ret < 0 ) return NULL;
 
     /* Now that the thread system is initialized, we don't have much, but
-     * at least we have var_Create */
-    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
-    var_Get( p_libvlc_global, "libvlc", &lockval );
-    vlc_mutex_lock( lockval.p_address );
+     * at least we have variables */
+    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
 
     i_instances++;
 
-    if( !libvlc_global.b_ready )
+    if( !p_libvlc_global->b_ready )
     {
         /* Guess what CPU we have */
         cpu_flags = CPUCapabilities();
        /* The module bank will be initialized later */
-        libvlc_global.p_module_bank = NULL;
+        p_libvlc_global->p_module_bank = NULL;
 
-        libvlc_global.b_ready = VLC_TRUE;
+        p_libvlc_global->b_ready = VLC_TRUE;
     }
-    vlc_mutex_unlock( lockval.p_address );
-    var_Destroy( p_libvlc_global, "libvlc" );
+    vlc_mutex_unlock( lock );
 
     /* Allocate a libvlc instance object */
     p_libvlc = vlc_object_create( p_libvlc_global, VLC_OBJECT_LIBVLC );
@@ -194,6 +186,7 @@ libvlc_int_t * libvlc_InternalCreate( void )
     }
     p_libvlc->p_playlist = NULL;
     p_libvlc->p_interaction = NULL;
+    p_libvlc->p_vlm = NULL;
     p_libvlc->psz_object_name = "libvlc";
 
     /* Initialize message queue */
@@ -318,7 +311,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     /* Check for plugins cache options */
     if( config_GetInt( p_libvlc, "reset-plugins-cache" ) > 0 )
     {
-        libvlc_global.p_module_bank->b_cache_delete = VLC_TRUE;
+        p_libvlc_global->p_module_bank->b_cache_delete = VLC_TRUE;
     }
 
     /* Will be re-done properly later on */
@@ -336,7 +329,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
             msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
             b_exit = VLC_TRUE;
         }
-        libvlc_global.b_daemon = VLC_TRUE;
+        p_libvlc_global->b_daemon = VLC_TRUE;
 
         /* lets check if we need to write the pidfile */
         psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
@@ -383,7 +376,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
             close( STDOUT_FILENO );
             close( STDERR_FILENO );
 
-            libvlc_global.b_daemon = VLC_TRUE;
+            p_libvlc_global->b_daemon = VLC_TRUE;
         }
 #endif
     }
@@ -408,7 +401,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     psz_language = config_GetPsz( p_libvlc, "language" );
     if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
     {
-        vlc_bool_t b_cache_delete = libvlc_global.p_module_bank->b_cache_delete;
+        vlc_bool_t b_cache_delete = p_libvlc_global->p_module_bank->b_cache_delete;
 
         /* Reset the default domain */
         SetLanguage( psz_language );
@@ -420,7 +413,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
         module_InitBank( p_libvlc );
         config_LoadConfigFile( p_libvlc, "main" );
         config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
-        libvlc_global.p_module_bank->b_cache_delete = b_cache_delete;
+        p_libvlc_global->p_module_bank->b_cache_delete = b_cache_delete;
     }
     free( psz_language );
 # endif
@@ -440,7 +433,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     }
 
     msg_Dbg( p_libvlc, "module bank initialized, found %i modules",
-                    libvlc_global.p_module_bank->i_children );
+             p_libvlc_global->p_module_bank->i_children );
 
     /* Check for help on modules */
     if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
@@ -726,13 +719,30 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     p_libvlc->i_timers = 0;
     p_libvlc->pp_timers = NULL;
 
+    /* Init stats */
+    p_libvlc->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
+    if( !p_libvlc->p_stats )
+    {
+        vlc_object_release( p_libvlc );
+        return VLC_ENOMEM;
+    }
+    vlc_mutex_init( p_libvlc, &p_libvlc->p_stats->lock );
+    p_libvlc->p_stats_computer = NULL;
+
+    /* Init the array that holds every input item */
+    ARRAY_INIT( p_libvlc->input_items );
+    p_libvlc->i_last_input_id = 0;
+
     /*
      * Initialize hotkey handling
      */
     var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
+    var_Create( p_libvlc, "key-action", VLC_VAR_INTEGER );
     p_libvlc->p_hotkeys = malloc( libvlc_hotkeys_size );
     /* Do a copy (we don't need to modify the strings) */
     memcpy( p_libvlc->p_hotkeys, libvlc_hotkeys, libvlc_hotkeys_size );
+    var_AddCallback( p_libvlc, "key-pressed", vlc_key_to_action,
+                     p_libvlc->p_hotkeys );
 
     /* Initialize interaction */
     p_libvlc->p_interaction = interaction_Init( p_libvlc );
@@ -759,6 +769,16 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     }
     free( psz_modules );
 
+    /* Initialize VLM if vlm-conf is specified */
+    psz_parser = config_GetPsz( p_libvlc, "vlm-conf" );
+    if( psz_parser && *psz_parser )
+    {
+        p_libvlc->p_vlm = vlm_New( p_libvlc );
+        if( !p_libvlc->p_vlm )
+            msg_Err( p_libvlc, "VLM initialization failed" );
+    }
+    free( psz_parser );
+
     /*
      * Load background interfaces
      */
@@ -950,6 +970,12 @@ int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
         aout_Delete( p_aout );
     }
 
+    /* Destroy VLM if created in libvlc_InternalInit */
+    if( p_libvlc->p_vlm )
+    {
+        vlm_Delete( p_libvlc->p_vlm );
+    }
+
     /* Free interaction */
     msg_Dbg( p_libvlc, "removing interaction" );
     vlc_object_release( p_libvlc->p_interaction );
@@ -966,6 +992,19 @@ int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
         vlc_object_release( p_announce );
         announce_HandlerDestroy( p_announce );
     }
+
+    vlc_bool_t b_clean = VLC_TRUE;
+    FOREACH_ARRAY( input_item_t *p_del, p_libvlc->input_items )
+        msg_Err( p_libvlc, "input item %p has not been deleted properly: refcount %d", p_del, p_del->i_gc_refcount );
+        b_clean = VLC_FALSE;
+    FOREACH_END();
+    assert( b_clean );
+    ARRAY_RESET( p_libvlc->input_items );
+
+    msg_Dbg( p_libvlc, "removing stats" );
+    vlc_mutex_destroy( &p_libvlc->p_stats->lock );
+    FREENULL( p_libvlc->p_stats );
+
     return VLC_SUCCESS;
 }
 
@@ -987,7 +1026,6 @@ int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
 #ifndef WIN32
     char* psz_pidfile = NULL;
 
-    if( libvlc_global.p_module_bank )
     if( config_GetInt( p_libvlc, "daemon" ) > 0 )
     {
         psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
@@ -1018,11 +1056,11 @@ int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
     FREENULL( p_libvlc->psz_datadir );
     FREENULL( p_libvlc->psz_cachedir );
     FREENULL( p_libvlc->psz_configfile );
+    var_DelCallback( p_libvlc, "key-pressed", vlc_key_to_action,
+                     p_libvlc->p_hotkeys );
     FREENULL( p_libvlc->p_hotkeys );
 
-    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
-    var_Get( p_libvlc_global, "libvlc", &lockval );
-    vlc_mutex_lock( lockval.p_address );
+    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
     i_instances--;
 
     if( i_instances == 0 )
@@ -1030,8 +1068,7 @@ int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
         /* System specific cleaning code */
         system_End( p_libvlc );
     }
-    vlc_mutex_unlock( lockval.p_address );
-    var_Destroy( p_libvlc_global, "libvlc" );
+    vlc_mutex_unlock( lock );
 
     msg_Flush( p_libvlc );
     msg_Destroy( p_libvlc );
@@ -1075,7 +1112,7 @@ int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc,
     }
 
 #ifndef WIN32
-    if( libvlc_global.b_daemon && b_block && !psz_module )
+    if( p_libvlc_global->b_daemon && b_block && !psz_module )
     {
         /* Daemon mode hack.
          * We prefer the dummy interface if none is specified. */
@@ -1167,7 +1204,7 @@ static inline int LoadMessages (void)
 #else
     char psz_path[1024];
     if (snprintf (psz_path, sizeof (psz_path), "%s/%s",
-                  libvlc_global.psz_vlcpath, "locale")
+                  p_libvlc_global->psz_vlcpath, "locale")
                      >= (int)sizeof (psz_path))
         return -1;