]> git.sesse.net Git - vlc/blobdiff - src/libvlc-common.c
Rename all sout_Cfg* stuff to config_Chain* (as it isn't really sout specific)
[vlc] / src / libvlc-common.c
index 05d967ad30b88ad1c754e1f727e2f623a276d93f..f7d5029b9bd0a73286d6381120f1174f620cd395 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * libvlc-common.c: libvlc instances creation and deletion
+ * libvlc-common.c: libvlc instances creation and deletion, interfaces handling
  *****************************************************************************
  * Copyright (C) 1998-2006 the VideoLAN team
  * $Id$
@@ -94,6 +94,7 @@
 static libvlc_global_data_t   libvlc_global;
 static libvlc_global_data_t * p_libvlc_global;
 static libvlc_int_t *    p_static_vlc;
+static volatile unsigned int i_instances = 0;
 
 /*****************************************************************************
  * Local prototypes
@@ -144,6 +145,7 @@ libvlc_int_t * libvlc_InternalCreate( void )
     int i_ret;
     libvlc_int_t * p_libvlc = NULL;
     vlc_value_t lockval;
+    char *psz_env;
 
     /* &libvlc_global never changes,
      * so we can safely call this multiple times. */
@@ -159,32 +161,16 @@ libvlc_int_t * libvlc_InternalCreate( void )
     var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
     var_Get( p_libvlc_global, "libvlc", &lockval );
     vlc_mutex_lock( lockval.p_address );
+
+       
+
+    i_instances++;
+
     if( !libvlc_global.b_ready )
     {
-        char *psz_env;
-
         /* Guess what CPU we have */
         libvlc_global.i_cpu = CPUCapabilities();
-
-        /* Find verbosity from VLC_VERBOSE environment variable */
-        psz_env = getenv( "VLC_VERBOSE" );
-        libvlc_global.i_verbose = psz_env ? atoi( psz_env ) : -1;
-
-#if defined( HAVE_ISATTY ) && !defined( WIN32 )
-        libvlc_global.b_color = isatty( 2 ); /* 2 is for stderr */
-#else
-        libvlc_global.b_color = VLC_FALSE;
-#endif
-
-        /* Initialize message queue */
-        msg_Create( p_libvlc_global );
-
-        /* Announce who we are */
-        msg_Dbg( p_libvlc_global, COPYRIGHT_MESSAGE );
-        msg_Dbg( p_libvlc_global, "libvlc was configured with %s",
-                                CONFIGURE_LINE );
-
-        /* The module bank will be initialized later */
+       /* The module bank will be initialized later */
         libvlc_global.p_module_bank = NULL;
 
         libvlc_global.b_ready = VLC_TRUE;
@@ -194,11 +180,27 @@ libvlc_int_t * libvlc_InternalCreate( void )
 
     /* Allocate a libvlc instance object */
     p_libvlc = vlc_object_create( p_libvlc_global, VLC_OBJECT_LIBVLC );
-    if( p_libvlc == NULL ) return NULL;
+    if( p_libvlc == NULL ) { i_instances--; return NULL; }
     p_libvlc->thread_id = 0;
     p_libvlc->p_playlist = NULL;
     p_libvlc->psz_object_name = "libvlc";
 
+    /* Initialize message queue */
+    msg_Create( p_libvlc );
+    /* Announce who we are - Do it only for first instance ? */
+    msg_Dbg( p_libvlc, COPYRIGHT_MESSAGE );
+    msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
+
+    /* Find verbosity from VLC_VERBOSE environment variable */
+    psz_env = getenv( "VLC_VERBOSE" );
+    p_libvlc->i_verbose = psz_env ? atoi( psz_env ) : -1;
+
+#if defined( HAVE_ISATTY ) && !defined( WIN32 )
+    p_libvlc->b_color = isatty( 2 ); /* 2 is for stderr */
+#else
+    p_libvlc->b_color = VLC_FALSE;
+#endif
+
     /* Initialize mutexes */
     vlc_mutex_init( p_libvlc, &p_libvlc->config_lock );
 #ifdef __APPLE__
@@ -346,7 +348,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc, char *ppsz_argv[] )
     /* End hack */
 
     /* Will be re-done properly later on */
-    p_libvlc->p_libvlc_global->i_verbose = config_GetInt( p_libvlc, "verbose" );
+    p_libvlc->i_verbose = config_GetInt( p_libvlc, "verbose" );
 
     /* Check for daemon mode */
 #ifndef WIN32
@@ -582,8 +584,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc, char *ppsz_argv[] )
     var_AddCallback( p_libvlc, "verbose", VerboseCallback, NULL );
     var_Change( p_libvlc, "verbose", VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
 
-    libvlc_global.b_color = libvlc_global.b_color && 
-                                config_GetInt( p_libvlc, "color" );
+    p_libvlc->b_color = p_libvlc->b_color && config_GetInt( p_libvlc, "color" );
 
     /*
      * Output messages that may still be in the queue
@@ -868,6 +869,8 @@ int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
  */
 int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
 {
+    vlc_value_t lockval;
+
     if( p_libvlc->p_memcpy_module )
     {
         module_Unneed( p_libvlc, p_libvlc->p_memcpy_module );
@@ -882,18 +885,24 @@ int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
     FREENULL( p_libvlc->psz_configfile );
     FREENULL( p_libvlc->p_hotkeys );
 
-    /* System specific cleaning code */
-    system_End( p_libvlc );
+    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
+    var_Get( p_libvlc_global, "libvlc", &lockval );
+    vlc_mutex_lock( lockval.p_address );
+    i_instances--;
 
-    /*
-     * Free message queue.
-     * Nobody shall use msg_* afterward.
-     */
-    msg_Flush( p_libvlc );
-    msg_Destroy( p_libvlc_global );
+    if( i_instances == 0 )
+    {
+        /* System specific cleaning code */
+        system_End( p_libvlc );
+
+       /* Destroy global iconv */
+        LocaleDeinit();
+    }
+    vlc_mutex_unlock( lockval.p_address );
+    var_Destroy( p_libvlc_global, "libvlc" );
 
-    /* Destroy global iconv */
-    LocaleDeinit();
+    msg_Flush( p_libvlc );
+    msg_Destroy( p_libvlc );
 
     /* Destroy mutexes */
     vlc_mutex_destroy( &p_libvlc->config_lock );
@@ -902,7 +911,9 @@ int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
     if( b_release ) vlc_object_release( p_libvlc );
     vlc_object_destroy( p_libvlc );
 
-    /* Stop thread system: last one out please shut the door! */
+    /* Stop thread system: last one out please shut the door!
+     * The number of initializations of the thread system is counted, we 
+     * can call this each time */
     vlc_threads_end( p_libvlc_global );
 
     return VLC_SUCCESS;
@@ -1545,11 +1556,11 @@ static int ConsoleWidth( void )
 static int VerboseCallback( vlc_object_t *p_this, const char *psz_variable,
                      vlc_value_t old_val, vlc_value_t new_val, void *param)
 {
-    libvlc_int_t *p_vlc = (libvlc_int_t *)p_this;
+    libvlc_int_t *p_libvlc = (libvlc_int_t *)p_this;
 
     if( new_val.i_int >= -1 )
     {
-        p_vlc->p_libvlc_global->i_verbose = __MIN( new_val.i_int, 2 );
+        p_libvlc->i_verbose = __MIN( new_val.i_int, 2 );
     }
     return VLC_SUCCESS;
 }