]> git.sesse.net Git - vlc/blobdiff - src/libvlc.c
input: handle recursive parsing in preparser
[vlc] / src / libvlc.c
index 857960c5417f4c3ce6263c51a5ceb5554ddc38c1..cff3784c1660d59886f8ce6e58967df8811a4fbc 100644 (file)
 
 #include "modules/modules.h"
 #include "config/configuration.h"
+#include "playlist/preparser.h"
 
 #include <stdio.h>                                              /* sprintf() */
 #include <string.h>
 #include <stdlib.h>                                                /* free() */
+#include <errno.h>
 
 #include "config/vlc_getopt.h"
 
-#ifdef HAVE_UNISTD_H
-#   include <unistd.h> /* isatty() */
-#endif
-
 #ifdef HAVE_DBUS
 /* used for one-instance mode */
 #   include <dbus/dbus.h>
 #endif
 
 
-#include <vlc_media_library.h>
 #include <vlc_playlist.h>
 #include <vlc_interface.h>
 
@@ -70,8 +67,8 @@
 #include <vlc_modules.h>
 
 #include "libvlc.h"
-
 #include "playlist/playlist_internal.h"
+#include "misc/variables.h"
 
 #include <vlc_vlm.h>
 
 
 #include <assert.h>
 
-/*****************************************************************************
- * The evil global variables. We handle them with care, don't worry.
- *****************************************************************************/
-
-#if !defined(WIN32) && !defined(__OS2__)
-static bool b_daemon = false;
-#endif
-
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
 static void GetFilenames  ( libvlc_int_t *, unsigned, const char *const [] );
 
 /**
- * Allocate a libvlc instance, initialize global data if needed
- * It also initializes the threading system
+ * Allocate a blank libvlc instance, also setting the exit handler.
+ * Vlc's threading system must have been initialized first
  */
 libvlc_int_t * libvlc_InternalCreate( void )
 {
     libvlc_int_t *p_libvlc;
     libvlc_priv_t *priv;
 
-    /* Now that the thread system is initialized, we don't have much, but
-     * at least we have variables */
     /* Allocate a libvlc instance object */
     p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
                                   "libvlc" );
@@ -112,19 +99,10 @@ libvlc_int_t * libvlc_InternalCreate( void )
         return NULL;
 
     priv = libvlc_priv (p_libvlc);
-    priv->p_playlist = NULL;
-    priv->p_ml = NULL;
+    priv->playlist = NULL;
     priv->p_dialog_provider = NULL;
     priv->p_vlm = NULL;
-    priv->i_verbose = 3; /* initial value until config is loaded */
-#if defined( HAVE_ISATTY ) && !defined( WIN32 )
-    priv->b_color = isatty( STDERR_FILENO ); /* 2 is for stderr */
-#else
-    priv->b_color = false;
-#endif
 
-    /* Initialize mutexes */
-    vlc_mutex_init( &priv->ml_lock );
     vlc_ExitInit( &priv->exit );
 
     return p_libvlc;
@@ -145,15 +123,16 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     char *       psz_modules = NULL;
     char *       psz_parser = NULL;
     char *       psz_control = NULL;
-    playlist_t  *p_playlist = NULL;
     char        *psz_val;
 
     /* System specific initialization code */
     system_Init();
 
+    vlc_LogPreinit(p_libvlc);
+
     /* Initialize the module bank and load the configuration of the
-     * main module. We need to do this at this stage to be able to display
-     * a short help if required by the user. (short help == main module
+     * core module. We need to do this at this stage to be able to display
+     * a short help if required by the user. (short help == core module
      * options) */
     module_InitBank ();
 
@@ -163,23 +142,8 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
         module_EndBank (false);
         return VLC_EGENERIC;
     }
-    priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
 
-    /* Find verbosity from VLC_VERBOSE environment variable */
-    {
-        char *env = getenv( "VLC_VERBOSE" );
-        if( env != NULL )
-            priv->i_verbose = atoi( env );
-    }
-
-    /* Announce who we are (TODO: only first instance?) */
-    msg_Dbg( p_libvlc, "VLC media player - %s", VERSION_MESSAGE );
-    msg_Dbg( p_libvlc, "%s", COPYRIGHT_MESSAGE );
-    msg_Dbg( p_libvlc, "revision %s", psz_vlc_changeset );
-    msg_Dbg( p_libvlc, "configured with %s", CONFIGURE_LINE );
-#ifdef WIN32
-    SelectClockSource (VLC_OBJECT(p_libvlc));
-#endif
+    vlc_threads_setup (p_libvlc);
 
     /* Load the builtins and plugins into the module_bank.
      * We have to do it before config_Load*() because this also gets the
@@ -204,15 +168,12 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     int vlc_optind;
     if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
     {
-#ifdef WIN32
-        MessageBox (NULL, TEXT("The command line options could not be parsed.\n"
-                    "Make sure they are valid."), TEXT("VLC media player"),
-                    MB_OK|MB_ICONERROR);
-#endif
+        vlc_LogDeinit (p_libvlc);
         module_EndBank (true);
         return VLC_EGENERIC;
     }
-    priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
+
+    vlc_LogInit(p_libvlc);
 
     /*
      * Support for gettext
@@ -227,51 +188,50 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
     {
         module_EndBank (true);
-        return VLC_EEXITSUCCESS;
+        exit(0);
     }
 
     if( module_count <= 1 )
     {
         msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
+        vlc_LogDeinit (p_libvlc);
         module_EndBank (true);
-        return VLC_ENOITEM;
+        return VLC_ENOMOD;
     }
 
 #ifdef HAVE_DAEMON
     /* Check for daemon mode */
     if( var_InheritBool( p_libvlc, "daemon" ) )
     {
-        char *psz_pidfile = NULL;
-
         if( daemon( 1, 0) != 0 )
         {
             msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
+            vlc_LogDeinit (p_libvlc);
             module_EndBank (true);
-            return VLC_EEXIT;
+            return VLC_ENOMEM;
         }
-        b_daemon = true;
 
         /* lets check if we need to write the pidfile */
-        psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
-        if( psz_pidfile != NULL )
+        char *pidfile = var_InheritString( p_libvlc, "pidfile" );
+        if( pidfile != NULL )
         {
-            FILE *pidfile;
-            pid_t i_pid = getpid ();
-            msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
-                               i_pid, psz_pidfile );
-            pidfile = vlc_fopen( psz_pidfile,"w" );
-            if( pidfile != NULL )
+            FILE *stream = vlc_fopen( pidfile, "w" );
+            if( stream != NULL )
             {
-                utf8_fprintf( pidfile, "%d", (int)i_pid );
-                fclose( pidfile );
+                fprintf( stream, "%d", (int)getpid() );
+                fclose( stream );
+                msg_Dbg( p_libvlc, "written PID file %s", pidfile );
             }
             else
-            {
-                msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
-                         psz_pidfile );
-            }
+                msg_Err( p_libvlc, "cannot write PID file %s: %s",
+                         pidfile, vlc_strerror_c(errno) );
+            free( pidfile );
         }
-        free( psz_pidfile );
+    }
+    else
+    {
+        var_Create( p_libvlc, "pidfile", VLC_VAR_STRING );
+        var_SetString( p_libvlc, "pidfile", "" );
     }
 #endif
 
@@ -354,18 +314,18 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
 
             /* append MRLs */
             if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &mrl,
-                                                DBUS_TYPE_STRING, &after_track,
+                                                DBUS_TYPE_OBJECT_PATH, &after_track,
                                                 DBUS_TYPE_BOOLEAN, &play,
                                                 DBUS_TYPE_INVALID ) )
             {
                  dbus_message_unref( msg );
                  msg = NULL;
+                 free( mrl );
+                 continue;
             }
-            free( mrl );
-            if( unlikely(msg == NULL) )
-                continue;
 
             msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
+            free( mrl );
 
             /* send message and get a handle for a reply */
             DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
@@ -389,24 +349,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
 dbus_out:
 #endif // HAVE_DBUS
 
-    /*
-     * Message queue options
-     */
-    /* Last chance to set the verbosity. Once we start interfaces and other
-     * threads, verbosity becomes read-only. */
-    var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    if( var_InheritBool( p_libvlc, "quiet" ) )
-    {
-        var_SetInteger( p_libvlc, "verbose", -1 );
-        priv->i_verbose = -1;
-    }
-    vlc_threads_setup( p_libvlc );
-
-    if( priv->b_color )
-        priv->b_color = var_InheritBool( p_libvlc, "color" );
-
     vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
-    vlc_object_set_name( p_libvlc, "main" );
 
     priv->b_stats = var_InheritBool( p_libvlc, "stats" );
 
@@ -415,6 +358,11 @@ dbus_out:
      */
     priv->actions = vlc_InitActions( p_libvlc );
 
+    /*
+     * Meta data handling
+     */
+    priv->parser = playlist_preparser_New(VLC_OBJECT(p_libvlc));
+
     /* Create a variable for showing the fullscreen interface */
     var_Create( p_libvlc, "intf-toggle-fscontrol", VLC_VAR_BOOL );
     var_SetBool( p_libvlc, "intf-toggle-fscontrol", true );
@@ -434,48 +382,26 @@ dbus_out:
 
     /* some default internal settings */
     var_Create( p_libvlc, "window", VLC_VAR_STRING );
+    /* NOTE: Because the playlist and interfaces start before this function
+     * returns control to the application (DESIGN BUG!), all these variables
+     * must be created (in place of libvlc_new()) and set to VLC defaults
+     * (in place of VLC main()) *here*. */
     var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
-    var_SetString( p_libvlc, "user-agent", "(LibVLC "VERSION")" );
-
-    /* Initialize playlist and get commandline files */
-    p_playlist = playlist_Create( VLC_OBJECT(p_libvlc) );
-    if( !p_playlist )
-    {
-        msg_Err( p_libvlc, "playlist initialization failed" );
-        module_EndBank (true);
-        return VLC_EGENERIC;
-    }
+    var_SetString( p_libvlc, "user-agent",
+                   "VLC media player (LibVLC "VERSION")" );
+    var_Create( p_libvlc, "http-user-agent", VLC_VAR_STRING );
+    var_SetString( p_libvlc, "http-user-agent",
+                   "VLC/"PACKAGE_VERSION" LibVLC/"PACKAGE_VERSION );
+    var_Create( p_libvlc, "app-icon-name", VLC_VAR_STRING );
+    var_SetString( p_libvlc, "app-icon-name", PACKAGE_NAME );
+    var_Create( p_libvlc, "app-id", VLC_VAR_STRING );
+    var_SetString( p_libvlc, "app-id", "org.VideoLAN.VLC" );
+    var_Create( p_libvlc, "app-version", VLC_VAR_STRING );
+    var_SetString( p_libvlc, "app-version", PACKAGE_VERSION );
 
     /* System specific configuration */
     system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
 
-#if defined(MEDIA_LIBRARY)
-    /* Get the ML */
-    if( var_GetBool( p_libvlc, "load-media-library-on-startup" ) )
-    {
-        priv->p_ml = ml_Create( VLC_OBJECT( p_libvlc ), NULL );
-        if( !priv->p_ml )
-        {
-            msg_Err( p_libvlc, "ML initialization failed" );
-            return VLC_EGENERIC;
-        }
-    }
-    else
-    {
-        priv->p_ml = NULL;
-    }
-#endif
-
-    /* Add service discovery modules */
-    psz_modules = var_InheritString( p_libvlc, "services-discovery" );
-    if( psz_modules )
-    {
-        char *p = psz_modules, *m;
-        while( ( m = strsep( &p, " :," ) ) != NULL )
-            playlist_ServicesDiscoveryAdd( p_playlist, m );
-        free( psz_modules );
-    }
-
 #ifdef ENABLE_VLM
     /* Initialize VLM if vlm-conf is specified */
     psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
@@ -522,46 +448,15 @@ dbus_out:
         }
         if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
         {
-            intf_Create( p_libvlc, psz_temp );
+            libvlc_InternalAddIntf( p_libvlc, psz_temp );
             free( psz_temp );
         }
     }
     free( psz_modules );
     free( psz_control );
 
-    /*
-     * Always load the hotkeys interface if it exists
-     */
-    intf_Create( p_libvlc, "hotkeys,none" );
-
-    if( var_InheritBool( p_libvlc, "file-logging" )
-#ifdef HAVE_SYSLOG_H
-        && !var_InheritBool( p_libvlc, "syslog" )
-#endif
-        )
-    {
-        intf_Create( p_libvlc, "logger,none" );
-    }
-#ifdef HAVE_SYSLOG_H
-    if( var_InheritBool( p_libvlc, "syslog" ) )
-    {
-        char *logmode = var_CreateGetNonEmptyString( p_libvlc, "logmode" );
-        var_SetString( p_libvlc, "logmode", "syslog" );
-        intf_Create( p_libvlc, "logger,none" );
-
-        if( logmode )
-        {
-            var_SetString( p_libvlc, "logmode", logmode );
-            free( logmode );
-        }
-        var_Destroy( p_libvlc, "logmode" );
-    }
-#endif
-
     if( var_InheritBool( p_libvlc, "network-synchronisation") )
-    {
-        intf_Create( p_libvlc, "netsync,none" );
-    }
+        libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
 
 #ifdef __APPLE__
     var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
@@ -574,9 +469,6 @@ dbus_out:
     var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
     var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
 #endif
-#if defined (WIN32) || defined (__OS2__)
-    var_Create( p_libvlc, "drawable-hwnd", VLC_VAR_INTEGER );
-#endif
 
     /*
      * Get input filenames given as commandline arguments.
@@ -591,8 +483,7 @@ dbus_out:
     psz_val = var_InheritString( p_libvlc, "open" );
     if ( psz_val != NULL )
     {
-        playlist_AddExt( p_playlist, psz_val, NULL, PLAYLIST_INSERT, 0,
-                         -1, 0, NULL, 0, true, pl_Unlocked );
+        intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 );
         free( psz_val );
     }
 
@@ -606,15 +497,6 @@ dbus_out:
 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
 {
     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
-    playlist_t    *p_playlist = libvlc_priv (p_libvlc)->p_playlist;
-
-    /* Deactivate the playlist */
-    msg_Dbg( p_libvlc, "deactivating the playlist" );
-    pl_Deactivate( p_libvlc );
-
-    /* Remove all services discovery */
-    msg_Dbg( p_libvlc, "removing all services discovery tasks" );
-    playlist_ServicesDiscoveryKillAll( p_playlist );
 
     /* Ask the interfaces to stop and destroy them */
     msg_Dbg( p_libvlc, "removing all interfaces" );
@@ -629,49 +511,31 @@ void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
     }
 #endif
 
-#if defined(MEDIA_LIBRARY)
-    media_library_t* p_ml = priv->p_ml;
-    if( p_ml )
+#if !defined( _WIN32 ) && !defined( __OS2__ )
+    char *pidfile = var_InheritString( p_libvlc, "pidfile" );
+    if( pidfile != NULL )
     {
-        ml_Destroy( VLC_OBJECT( p_ml ) );
-        vlc_object_release( p_ml );
-        libvlc_priv(p_playlist->p_libvlc)->p_ml = NULL;
+        msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
+        if( unlink( pidfile ) )
+            msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
+                      pidfile, vlc_strerror_c(errno) );
+        free( pidfile );
     }
 #endif
 
-    /* Free playlist now, all threads are gone */
-    playlist_Destroy( p_playlist );
-
-    msg_Dbg( p_libvlc, "removing stats" );
-
-#if !defined( WIN32 ) && !defined( __OS2__ )
-    char* psz_pidfile = NULL;
+    if (priv->parser != NULL)
+        playlist_preparser_Delete(priv->parser);
 
-    if( b_daemon )
-    {
-        psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
-        if( psz_pidfile != NULL )
-        {
-            msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
-            if( unlink( psz_pidfile ) == -1 )
-            {
-                msg_Dbg( p_libvlc, "removing pid file %s: %m",
-                        psz_pidfile );
-            }
-        }
-        free( psz_pidfile );
-    }
-#endif
+    vlc_DeinitActions( p_libvlc, priv->actions );
 
     /* Save the configuration */
     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
         config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
 
     /* Free module bank. It is refcounted, so we call this each time  */
+    vlc_LogDeinit (p_libvlc);
     module_EndBank (true);
-
-    vlc_DeinitActions( p_libvlc, priv->actions );
-#ifdef WIN32
+#if defined(_WIN32) || defined(__OS2__)
     system_End( );
 #endif
 }
@@ -687,56 +551,12 @@ void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
 {
     libvlc_priv_t *priv = libvlc_priv( p_libvlc );
 
-    /* Destroy mutexes */
     vlc_ExitDestroy( &priv->exit );
-    vlc_mutex_destroy( &priv->ml_lock );
 
-#ifndef NDEBUG /* Hack to dump leaked objects tree */
-    if( vlc_internals( p_libvlc )->i_refcount > 1 )
-        while( vlc_internals( p_libvlc )->i_refcount > 0 )
-            vlc_object_release( p_libvlc );
-#endif
-
-    assert( vlc_internals( p_libvlc )->i_refcount == 1 );
+    assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
     vlc_object_release( p_libvlc );
 }
 
-/**
- * Add an interface plugin and run it
- */
-int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
-{
-    if( !p_libvlc )
-        return VLC_EGENERIC;
-
-    if( !psz_module ) /* requesting the default interface */
-    {
-        char *psz_interface = var_CreateGetNonEmptyString( p_libvlc, "intf" );
-        if( !psz_interface ) /* "intf" has not been set */
-        {
-#if !defined( WIN32 ) && !defined( __OS2__ )
-            if( b_daemon )
-                 /* Daemon mode hack.
-                  * We prefer the dummy interface if none is specified. */
-                psz_module = "dummy";
-            else
-#endif
-                msg_Info( p_libvlc, "%s",
-                          _("Running vlc with the default interface. "
-                            "Use 'cvlc' to use vlc without interface.") );
-        }
-        free( psz_interface );
-        var_Destroy( p_libvlc, "intf" );
-    }
-
-    /* Try to create the interface */
-    int ret = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
-    if( ret )
-        msg_Err( p_libvlc, "interface \"%s\" initialization failed",
-                 psz_module ? psz_module : "default" );
-    return ret;
-}
-
 /*****************************************************************************
  * GetFilenames: parse command line options which are not flags
  *****************************************************************************
@@ -769,10 +589,41 @@ static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
                 continue;
         }
 
-        playlist_AddExt( pl_Get( p_vlc ), (mrl != NULL) ? mrl : args[n], NULL,
-                         PLAYLIST_INSERT, 0, -1, i_options,
+        intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
                          ( i_options ? &args[n + 1] : NULL ),
-                         VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
+                         VLC_INPUT_OPTION_TRUSTED );
         free( mrl );
     }
 }
+
+/**
+ * Requests extraction of the meta data for an input item (a.k.a. preparsing).
+ * The actual extraction is asynchronous.
+ */
+int libvlc_MetaRequest(libvlc_int_t *libvlc, input_item_t *item,
+                       input_item_meta_request_option_t i_options)
+{
+    libvlc_priv_t *priv = libvlc_priv(libvlc);
+
+    if (unlikely(priv->parser == NULL))
+        return VLC_ENOMEM;
+
+    playlist_preparser_Push(priv->parser, item, i_options);
+    return VLC_SUCCESS;
+}
+
+/**
+ * Requests retrieving/downloading art for an input item.
+ * The retrieval is performed asynchronously.
+ */
+int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
+                      input_item_meta_request_option_t i_options)
+{
+    libvlc_priv_t *priv = libvlc_priv(libvlc);
+
+    if (unlikely(priv->parser == NULL))
+        return VLC_ENOMEM;
+
+    playlist_preparser_fetcher_Push(priv->parser, item, i_options);
+    return VLC_SUCCESS;
+}