]> git.sesse.net Git - vlc/blobdiff - src/libvlc.c
opengl: Handle pixel pitch properly when repacking textures for upload
[vlc] / src / libvlc.c
index 0aac070808ffdee12e7b27d1949806095c85e0d2..349efa3f51cf1d2156e30becb0ee7db4addbc707 100644 (file)
@@ -70,8 +70,8 @@
 #include <vlc_modules.h>
 
 #include "libvlc.h"
-
 #include "playlist/playlist_internal.h"
+#include "misc/variables.h"
 
 #include <vlc_vlm.h>
 
@@ -102,7 +102,6 @@ libvlc_int_t * libvlc_InternalCreate( void )
 {
     libvlc_int_t *p_libvlc;
     libvlc_priv_t *priv;
-    char *psz_env = NULL;
 
     /* Now that the thread system is initialized, we don't have much, but
      * at least we have variables */
@@ -118,18 +117,6 @@ libvlc_int_t * libvlc_InternalCreate( void )
     priv->p_dialog_provider = NULL;
     priv->p_vlm = NULL;
 
-    /* Find verbosity from VLC_VERBOSE environment variable */
-    psz_env = getenv( "VLC_VERBOSE" );
-    if( psz_env != NULL )
-        priv->i_verbose = atoi( psz_env );
-    else
-        priv->i_verbose = 3;
-#if defined( HAVE_ISATTY ) && !defined( WIN32 )
-    priv->b_color = isatty( 2 ); /* 2 is for stderr */
-#else
-    priv->b_color = false;
-#endif
-
     /* Initialize mutexes */
     vlc_mutex_init( &priv->ml_lock );
     vlc_ExitInit( &priv->exit );
@@ -152,7 +139,6 @@ 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 */
@@ -170,13 +156,37 @@ 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" );
+
+    /*
+     * Message queue options (read-only afterwards)
+     */
+#if defined (HAVE_ISATTY) && !defined (WIN32)
+    if (isatty (STDERR_FILENO))
+        priv->b_color = var_InheritBool (p_libvlc, "color");
+    else
+#endif
+        priv->b_color = false;
+
+    priv->i_verbose = var_InheritInteger (p_libvlc, "verbose");
+    psz_val = getenv ("VLC_VERBOSE");
+    if (psz_val != NULL)
+        priv->i_verbose = atoi (psz_val);
+
+    if (var_InheritBool (p_libvlc, "quiet"))
+    {
+        var_Create (p_libvlc, "verbose", VLC_VAR_INTEGER);
+        var_SetInteger (p_libvlc, "verbose", -1);
+        priv->i_verbose = -1;
+    }
+
+    vlc_LogInit (p_libvlc);
 
     /* 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 );
+    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
@@ -201,15 +211,10 @@ 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
         module_EndBank (true);
+        vlc_LogDeinit (p_libvlc);
         return VLC_EGENERIC;
     }
-    priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
 
     /*
      * Support for gettext
@@ -224,14 +229,15 @@ 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.");
         module_EndBank (true);
-        return VLC_ENOITEM;
+        vlc_LogDeinit (p_libvlc);
+        return VLC_ENOMOD;
     }
 
 #ifdef HAVE_DAEMON
@@ -244,7 +250,8 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
         {
             msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
             module_EndBank (true);
-            return VLC_EEXIT;
+            vlc_LogDeinit (p_libvlc);
+            return VLC_ENOMEM;
         }
         b_daemon = true;
 
@@ -280,178 +287,113 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
 #define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
 #define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"
 
-    dbus_threads_init_default();
-
     if( var_InheritBool( p_libvlc, "one-instance" )
     || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
       && var_InheritBool( p_libvlc, "started-from-file" ) ) )
     {
+        for( int i = vlc_optind; i < i_argc; i++ )
+            if( ppsz_argv[i][0] == ':' )
+            {
+                msg_Err( p_libvlc, "item option %s incompatible with single instance",
+                         ppsz_argv[i] );
+                goto dbus_out;
+            }
+
         /* Initialise D-Bus interface, check for other instances */
-        DBusConnection  *p_conn = NULL;
-        DBusError       dbus_error;
+        dbus_threads_init_default();
 
-        dbus_error_init( &dbus_error );
+        DBusError err;
+        dbus_error_init( &err );
 
         /* connect to the session bus */
-        p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
-        if( !p_conn )
+        DBusConnection  *conn = dbus_bus_get( DBUS_BUS_SESSION, &err );
+        if( conn == NULL )
         {
             msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
-                    dbus_error.message );
-            dbus_error_free( &dbus_error );
+                    err.message );
+            dbus_error_free( &err );
+            goto dbus_out;
         }
-        else
+
+        /* check if VLC is available on the bus
+         * if not: D-Bus control is not enabled on the other
+         * instance and we can't pass MRLs to it */
+        /* FIXME: This check is totally brain-dead and buggy. */
+        if( !dbus_bus_name_has_owner( conn, MPRIS_BUS_NAME, &err ) )
         {
-            /* check if VLC is available on the bus
-             * if not: D-Bus control is not enabled on the other
-             * instance and we can't pass MRLs to it */
-            if( !dbus_bus_name_has_owner( p_conn, MPRIS_BUS_NAME, &dbus_error ) )
+            dbus_connection_unref( conn );
+            if( dbus_error_is_set( &err ) )
             {
-                if( dbus_error_is_set( &dbus_error ) )
-                {
-                    msg_Err( p_libvlc, "D-Bus error: %s", dbus_error.message );
-                    dbus_error_free( &dbus_error );
-                }
-                else
-                    msg_Dbg( p_libvlc, "No Media Player is running. "
-                            "Continuing normally." );
+                msg_Err( p_libvlc, "D-Bus error: %s", err.message );
             }
             else
+                msg_Dbg( p_libvlc, "No media player running. Continuing normally." );
+            dbus_error_free( &err );
+            goto dbus_out;
+        }
+
+        const dbus_bool_t play = !var_InheritBool( p_libvlc, "playlist-enqueue" );
+
+        msg_Warn( p_libvlc, "media player running. Exiting...");
+        for( int i = vlc_optind; i < i_argc; i++ )
+        {
+            DBusMessage *msg = dbus_message_new_method_call(
+               MPRIS_BUS_NAME, MPRIS_OBJECT_PATH, MPRIS_TRACKLIST_INTERFACE, "AddTrack" );
+            if( unlikely(msg == NULL) )
+                continue;
+
+            /* We need to resolve relative paths in this instance */
+            char *mrl;
+            if( strstr( ppsz_argv[i], "://" ) )
+                mrl = strdup( ppsz_argv[i] );
+            else
+                mrl = vlc_path2uri( ppsz_argv[i], NULL );
+            if( mrl == NULL )
             {
-                int i_input;
-                DBusMessage* p_dbus_msg = NULL;
-                DBusMessageIter dbus_args;
-                DBusPendingCall* p_dbus_pending = NULL;
-                dbus_bool_t b_play;
-
-                msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
-
-                for( i_input = vlc_optind; i_input < i_argc;i_input++ )
-                {
-                    /* Skip input options, we can't pass them through D-Bus */
-                    if( ppsz_argv[i_input][0] == ':' )
-                    {
-                        msg_Warn( p_libvlc, "Ignoring option %s",
-                                  ppsz_argv[i_input] );
-                        continue;
-                    }
-
-                    /* We need to resolve relative paths in this instance */
-                    char *psz_mrl = make_URI( ppsz_argv[i_input], NULL );
-                    const char *psz_after_track = MPRIS_APPEND;
-
-                    if( psz_mrl == NULL )
-                        continue;
-                    msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
-                             psz_mrl );
-
-                    p_dbus_msg = dbus_message_new_method_call(
-                        MPRIS_BUS_NAME, MPRIS_OBJECT_PATH,
-                        MPRIS_TRACKLIST_INTERFACE, "AddTrack" );
-
-                    if ( NULL == p_dbus_msg )
-                    {
-                        msg_Err( p_libvlc, "D-Bus problem" );
-                        free( psz_mrl );
-                        system_End( );
-                        exit( 1 );
-                    }
-
-                    /* append MRLs */
-                    dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
-                    if ( !dbus_message_iter_append_basic( &dbus_args,
-                                DBUS_TYPE_STRING, &psz_mrl ) )
-                    {
-                        dbus_message_unref( p_dbus_msg );
-                        free( psz_mrl );
-                        system_End( );
-                        exit( 1 );
-                    }
-                    free( psz_mrl );
-
-                    if( !dbus_message_iter_append_basic( &dbus_args,
-                                DBUS_TYPE_OBJECT_PATH, &psz_after_track ) )
-                    {
-                        dbus_message_unref( p_dbus_msg );
-                        system_End( );
-                        exit( 1 );
-                    }
-
-                    b_play = TRUE;
-                    if( var_InheritBool( p_libvlc, "playlist-enqueue" ) )
-                        b_play = FALSE;
-
-                    if ( !dbus_message_iter_append_basic( &dbus_args,
-                                DBUS_TYPE_BOOLEAN, &b_play ) )
-                    {
-                        dbus_message_unref( p_dbus_msg );
-                        system_End( );
-                        exit( 1 );
-                    }
-
-                    /* send message and get a handle for a reply */
-                    if ( !dbus_connection_send_with_reply ( p_conn,
-                                p_dbus_msg, &p_dbus_pending, -1 ) )
-                    {
-                        msg_Err( p_libvlc, "D-Bus problem" );
-                        dbus_message_unref( p_dbus_msg );
-                        system_End( );
-                        exit( 1 );
-                    }
-
-                    if ( NULL == p_dbus_pending )
-                    {
-                        msg_Err( p_libvlc, "D-Bus problem" );
-                        dbus_message_unref( p_dbus_msg );
-                        system_End( );
-                        exit( 1 );
-                    }
-                    dbus_connection_flush( p_conn );
-                    dbus_message_unref( p_dbus_msg );
-                    /* block until we receive a reply */
-                    dbus_pending_call_block( p_dbus_pending );
-                    dbus_pending_call_unref( p_dbus_pending );
-                } /* processes all command line MRLs */
-
-                /* bye bye */
-                system_End( );
-                exit( 0 );
+                dbus_message_unref( msg );
+                continue;
             }
+
+            const char *after_track = MPRIS_APPEND;
+
+            /* append MRLs */
+            if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &mrl,
+                                                DBUS_TYPE_OBJECT_PATH, &after_track,
+                                                DBUS_TYPE_BOOLEAN, &play,
+                                                DBUS_TYPE_INVALID ) )
+            {
+                 dbus_message_unref( msg );
+                 msg = NULL;
+            }
+            free( mrl );
+            if( unlikely(msg == NULL) )
+                continue;
+
+            msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
+
+            /* send message and get a handle for a reply */
+            DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
+                                                                            &err );
+            dbus_message_unref( msg );
+            if( reply == NULL )
+            {
+                msg_Err( p_libvlc, "D-Bus error: %s", err.message );
+                continue;
+            }
+            dbus_message_unref( reply );
         }
         /* we unreference the connection when we've finished with it */
-        if( p_conn ) dbus_connection_unref( p_conn );
+        dbus_connection_unref( conn );
+        exit( 1 );
     }
-
 #undef MPRIS_APPEND
 #undef MPRIS_BUS_NAME
 #undef MPRIS_OBJECT_PATH
 #undef MPRIS_TRACKLIST_INTERFACE
-
+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) );
-    /*
-     * Choose the best memcpy module
-     */
-    priv->p_memcpy_module = module_need( p_libvlc, "memcpy", "$memcpy", false );
-    /* Avoid being called "memcpy":*/
-    vlc_object_set_name( p_libvlc, "main" );
 
     priv->b_stats = var_InheritBool( p_libvlc, "stats" );
 
@@ -482,19 +424,6 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     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" );
-        if( priv->p_memcpy_module != NULL )
-        {
-            module_unneed( p_libvlc, priv->p_memcpy_module );
-        }
-        module_EndBank (true);
-        return VLC_EGENERIC;
-    }
-
     /* System specific configuration */
     system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
 
@@ -515,16 +444,6 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     }
 #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" );
@@ -578,11 +497,6 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     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" )
@@ -640,7 +554,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     psz_val = var_InheritString( p_libvlc, "open" );
     if ( psz_val != NULL )
     {
-        playlist_AddExt( p_playlist, psz_val, NULL, PLAYLIST_INSERT, 0,
+        playlist_AddExt( pl_Get(p_libvlc), psz_val, NULL, PLAYLIST_INSERT, 0,
                          -1, 0, NULL, 0, true, pl_Unlocked );
         free( psz_val );
     }
@@ -655,15 +569,6 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
 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" );
@@ -689,7 +594,9 @@ void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
 #endif
 
     /* Free playlist now, all threads are gone */
-    playlist_Destroy( p_playlist );
+    playlist_t *p_playlist = libvlc_priv (p_libvlc)->p_playlist;
+    if( p_playlist != NULL )
+        playlist_Destroy( p_playlist );
 
     msg_Dbg( p_libvlc, "removing stats" );
 
@@ -712,11 +619,7 @@ void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
     }
 #endif
 
-    if( priv->p_memcpy_module )
-    {
-        module_unneed( p_libvlc, priv->p_memcpy_module );
-        priv->p_memcpy_module = NULL;
-    }
+    vlc_DeinitActions( p_libvlc, priv->actions );
 
     /* Save the configuration */
     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
@@ -724,8 +627,10 @@ void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
 
     /* Free module bank. It is refcounted, so we call this each time  */
     module_EndBank (true);
-
-    vlc_DeinitActions( p_libvlc, priv->actions );
+    vlc_LogDeinit (p_libvlc);
+#if defined(WIN32) || defined(__OS2__)
+    system_End( );
+#endif
 }
 
 /**
@@ -739,19 +644,11 @@ void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
 {
     libvlc_priv_t *priv = libvlc_priv( p_libvlc );
 
-    system_End( );
-
     /* 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 );
 }
 
@@ -815,13 +712,18 @@ static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
             }
         }
 
-        char *mrl = make_URI( args[n], NULL );
-        if( !mrl )
-            continue;
+        char *mrl = NULL;
+        if( strstr( args[n], "://" ) == NULL )
+        {
+            mrl = vlc_path2uri( args[n], NULL );
+            if( !mrl )
+                continue;
+        }
 
-        playlist_AddExt( pl_Get( p_vlc ), mrl, NULL, PLAYLIST_INSERT,
-                0, -1, i_options, ( i_options ? &args[n + 1] : NULL ),
-                VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
+        playlist_AddExt( pl_Get( p_vlc ), (mrl != NULL) ? mrl : args[n], NULL,
+                         PLAYLIST_INSERT, 0, -1, i_options,
+                         ( i_options ? &args[n + 1] : NULL ),
+                         VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
         free( mrl );
     }
 }