/*****************************************************************************
- * 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$
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
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. */
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;
/* 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__
/* 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
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
*/
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 );
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 );
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;
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;
}