/** * Initialize a libvlc instance * This function initializes a previously allocated libvlc instance: * - CPU detection * - gettext initialization * - message queue, module bank and playlist initialization * - configuration and commandline parsing */ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc, const char *ppsz_argv[] ) { libvlc_priv_t *priv = libvlc_priv (p_libvlc); char * psz_modules = NULL; char * psz_parser = NULL; char * psz_control = NULL; char *psz_val; /* System specific initialization code */ system_Init(); vlc_LogPreinit(p_libvlc); /* Initialize the module bank and load the configuration of the * 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 (); /* Get command line options that affect module loading. */ if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) ) { module_EndBank (false); return VLC_EGENERIC; } 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 * list of configuration options exported by each module and loads their * default values. */ size_t module_count = module_LoadPlugins (p_libvlc); /* * Override default configuration with config file settings */ if( !var_InheritBool( p_libvlc, "ignore-config" ) ) { if( var_InheritBool( p_libvlc, "reset-config" ) ) config_SaveConfigFile( p_libvlc ); /* Save default config */ else config_LoadConfigFile( p_libvlc ); } /* * Override configuration with command line settings */ int vlc_optind; if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) ) { vlc_LogDeinit (p_libvlc); module_EndBank (true); return VLC_EGENERIC; } vlc_LogInit(p_libvlc); /* * Support for gettext */ #if defined( ENABLE_NLS ) \ && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) ) vlc_bindtextdomain (PACKAGE_NAME); #endif /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */ msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") ); if (config_PrintHelp (VLC_OBJECT(p_libvlc))) { module_EndBank (true); 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_ENOMOD; } #ifdef HAVE_DAEMON /* Check for daemon mode */ if( var_InheritBool( p_libvlc, "daemon" ) ) { 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_ENOMEM; } /* lets check if we need to write the pidfile */ char *pidfile = var_InheritString( p_libvlc, "pidfile" ); if( pidfile != NULL ) { FILE *stream = vlc_fopen( pidfile, "w" ); if( stream != NULL ) { fprintf( stream, "%d", (int)getpid() ); fclose( stream ); msg_Dbg( p_libvlc, "written PID file %s", pidfile ); } else msg_Err( p_libvlc, "cannot write PID file %s: %s", pidfile, vlc_strerror_c(errno) ); free( pidfile ); } } else { var_Create( p_libvlc, "pidfile", VLC_VAR_STRING ); var_SetString( p_libvlc, "pidfile", "" ); } #endif /* FIXME: could be replaced by using Unix sockets */ #ifdef HAVE_DBUS #define MPRIS_APPEND "/org/mpris/MediaPlayer2/TrackList/Append" #define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc" #define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2" #define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList" 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 */ dbus_threads_init_default(); DBusError err; dbus_error_init( &err ); /* connect to the session bus */ 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", err.message ); dbus_error_free( &err ); goto dbus_out; } /* 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 ) ) { dbus_connection_unref( conn ); if( dbus_error_is_set( &err ) ) { 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 ) { 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 ); 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, &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 */ dbus_connection_unref( conn ); exit( 0 ); } #undef MPRIS_APPEND #undef MPRIS_BUS_NAME #undef MPRIS_OBJECT_PATH #undef MPRIS_TRACKLIST_INTERFACE dbus_out: #endif // HAVE_DBUS vlc_CPU_dump( VLC_OBJECT(p_libvlc) ); priv->b_stats = var_InheritBool( p_libvlc, "stats" ); /* * Initialize hotkey handling */ 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 ); /* Create a variable for the Boss Key */ var_Create( p_libvlc, "intf-boss", VLC_VAR_VOID ); /* Create a variable for showing the main interface */ var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL ); /* Create a variable for showing the right click menu */ var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL ); /* variables for signalling creation of new files */ var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING ); var_Create( p_libvlc, "record-file", VLC_VAR_STRING ); /* 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", "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 ); #ifdef ENABLE_VLM /* Initialize VLM if vlm-conf is specified */ psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" ); if( psz_parser ) { priv->p_vlm = vlm_New( p_libvlc ); if( !priv->p_vlm ) msg_Err( p_libvlc, "VLM initialization failed" ); } free( psz_parser ); #endif /* * Load background interfaces */ psz_modules = var_CreateGetNonEmptyString( p_libvlc, "extraintf" ); psz_control = var_CreateGetNonEmptyString( p_libvlc, "control" ); if( psz_modules && psz_control ) { char* psz_tmp; if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 ) { free( psz_modules ); psz_modules = psz_tmp; } } else if( psz_control ) { free( psz_modules ); psz_modules = strdup( psz_control ); } psz_parser = psz_modules; while ( psz_parser && *psz_parser ) { char *psz_module, *psz_temp; psz_module = psz_parser; psz_parser = strchr( psz_module, ':' ); if ( psz_parser ) { *psz_parser = '\0'; psz_parser++; } if( asprintf( &psz_temp, "%s,none", psz_module ) != -1) { libvlc_InternalAddIntf( p_libvlc, psz_temp ); free( psz_temp ); } } free( psz_modules ); free( psz_control ); if( var_InheritBool( p_libvlc, "network-synchronisation") ) libvlc_InternalAddIntf( p_libvlc, "netsync,none" ); #ifdef __APPLE__ var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER ); var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER ); var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER ); var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER ); var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER ); var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER ); var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER ); var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER ); var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS ); #endif /* * Get input filenames given as commandline arguments. * We assume that the remaining parameters are filenames * and their input options. */ GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind ); /* * Get --open argument */ psz_val = var_InheritString( p_libvlc, "open" ); if ( psz_val != NULL ) { intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 ); free( psz_val ); } return VLC_SUCCESS; }
/** * Create playlist * * Create a playlist structure. * \param p_parent the vlc object that is to be the parent of this playlist * \return a pointer to the created playlist, or NULL on error */ static playlist_t *playlist_Create( vlc_object_t *p_parent ) { playlist_t *p_playlist; playlist_private_t *p; /* Allocate structure */ p = vlc_custom_create( p_parent, sizeof( *p ), "playlist" ); if( !p ) return NULL; assert( offsetof( playlist_private_t, public_data ) == 0 ); p_playlist = &p->public_data; TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds ); VariablesInit( p_playlist ); vlc_mutex_init( &p->lock ); vlc_cond_init( &p->signal ); p->killed = false; /* Initialise data structures */ pl_priv(p_playlist)->i_last_playlist_id = 0; pl_priv(p_playlist)->p_input = NULL; ARRAY_INIT( p_playlist->items ); ARRAY_INIT( p_playlist->all_items ); ARRAY_INIT( pl_priv(p_playlist)->items_to_delete ); ARRAY_INIT( p_playlist->current ); p_playlist->i_current_index = 0; pl_priv(p_playlist)->b_reset_currently_playing = true; pl_priv(p_playlist)->b_tree = var_InheritBool( p_parent, "playlist-tree" ); pl_priv(p_playlist)->b_doing_ml = false; pl_priv(p_playlist)->b_auto_preparse = var_InheritBool( p_parent, "auto-preparse" ); /* Preparser (and meta retriever) */ p->p_preparser = playlist_preparser_New( VLC_OBJECT(p_playlist) ); if( unlikely(p->p_preparser == NULL) ) msg_Err( p_playlist, "cannot create preparser" ); /* Create the root node */ PL_LOCK; p_playlist->p_root = playlist_NodeCreate( p_playlist, NULL, NULL, PLAYLIST_END, 0, NULL ); PL_UNLOCK; if( !p_playlist->p_root ) return NULL; /* Create currently playing items node */ PL_LOCK; p_playlist->p_playing = playlist_NodeCreate( p_playlist, _( "Playlist" ), p_playlist->p_root, PLAYLIST_END, PLAYLIST_RO_FLAG, NULL ); PL_UNLOCK; if( !p_playlist->p_playing ) return NULL; /* Create media library node */ const bool b_ml = var_InheritBool( p_parent, "media-library"); if( b_ml ) { PL_LOCK; p_playlist->p_media_library = playlist_NodeCreate( p_playlist, _( "Media Library" ), p_playlist->p_root, PLAYLIST_END, PLAYLIST_RO_FLAG, NULL ); PL_UNLOCK; } else { p_playlist->p_media_library = NULL; } p_playlist->p_root_category = p_playlist->p_root; p_playlist->p_root_onelevel = p_playlist->p_root; p_playlist->p_local_category = p_playlist->p_playing; p_playlist->p_local_onelevel = p_playlist->p_playing; p_playlist->p_ml_category = p_playlist->p_media_library; p_playlist->p_ml_onelevel = p_playlist->p_media_library;; /* Initial status */ pl_priv(p_playlist)->status.p_item = NULL; pl_priv(p_playlist)->status.p_node = p_playlist->p_playing; pl_priv(p_playlist)->request.b_request = false; pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED; if(b_ml) { const bool b_auto_preparse = pl_priv(p_playlist)->b_auto_preparse; pl_priv(p_playlist)->b_auto_preparse = false; playlist_MLLoad( p_playlist ); pl_priv(p_playlist)->b_auto_preparse = b_auto_preparse; } /* Input resources */ p->p_input_resource = input_resource_New( VLC_OBJECT( p_playlist ) ); if( unlikely(p->p_input_resource == NULL) ) abort(); /* Audio output (needed for volume and device controls). */ audio_output_t *aout = input_resource_GetAout( p->p_input_resource ); if( aout != NULL ) input_resource_PutAout( p->p_input_resource, aout ); /* Thread */ playlist_Activate (p_playlist); /* Add service discovery modules */ char *mods = var_InheritString( p_playlist, "services-discovery" ); if( mods != NULL ) { char *p = mods, *m; while( (m = strsep( &p, " :," )) != NULL ) playlist_ServicesDiscoveryAdd( p_playlist, m ); free( mods ); } return p_playlist; }
/** * Create playlist * * Create a playlist structure. * \param p_parent the vlc object that is to be the parent of this playlist * \return a pointer to the created playlist, or NULL on error */ playlist_t *playlist_Create( vlc_object_t *p_parent ) { playlist_t *p_playlist; playlist_private_t *p; /* Allocate structure */ p = vlc_custom_create( p_parent, sizeof( *p ), "playlist" ); if( !p ) return NULL; assert( offsetof( playlist_private_t, public_data ) == 0 ); p_playlist = &p->public_data; TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds ); VariablesInit( p_playlist ); vlc_mutex_init( &p->lock ); vlc_cond_init( &p->signal ); p->killed = false; /* Initialise data structures */ pl_priv(p_playlist)->i_last_playlist_id = 0; pl_priv(p_playlist)->p_input = NULL; ARRAY_INIT( p_playlist->items ); ARRAY_INIT( p_playlist->all_items ); ARRAY_INIT( pl_priv(p_playlist)->items_to_delete ); ARRAY_INIT( p_playlist->current ); p_playlist->i_current_index = 0; pl_priv(p_playlist)->b_reset_currently_playing = true; pl_priv(p_playlist)->b_tree = var_InheritBool( p_parent, "playlist-tree" ); /* Create the root, playing items and meida library nodes */ playlist_item_t *root, *playing, *ml; PL_LOCK; root = playlist_NodeCreate( p_playlist, NULL, NULL, PLAYLIST_END, 0, NULL ); playing = playlist_NodeCreate( p_playlist, _( "Playlist" ), root, PLAYLIST_END, PLAYLIST_RO_FLAG, NULL ); if( var_InheritBool( p_parent, "media-library") ) ml = playlist_NodeCreate( p_playlist, _( "Media Library" ), root, PLAYLIST_END, PLAYLIST_RO_FLAG, NULL ); else ml = NULL; PL_UNLOCK; if( unlikely(root == NULL || playing == NULL) ) abort(); p_playlist->p_root = root; p_playlist->p_playing = playing; p_playlist->p_media_library = ml; p_playlist->p_root_category = p_playlist->p_root; p_playlist->p_root_onelevel = p_playlist->p_root; p_playlist->p_local_category = p_playlist->p_playing; p_playlist->p_local_onelevel = p_playlist->p_playing; p_playlist->p_ml_category = p_playlist->p_media_library; p_playlist->p_ml_onelevel = p_playlist->p_media_library;; /* Initial status */ pl_priv(p_playlist)->status.p_item = NULL; pl_priv(p_playlist)->status.p_node = p_playlist->p_playing; pl_priv(p_playlist)->request.b_request = false; if (ml != NULL) playlist_MLLoad( p_playlist ); /* Preparser (and meta retriever) _after_ the Media Library*/ if( var_InheritBool( p_parent, "auto-preparse" ) ) { p->p_preparser = playlist_preparser_New( VLC_OBJECT(p_playlist) ); if( unlikely(p->p_preparser == NULL) ) msg_Err( p_playlist, "cannot create preparser" ); } /* Input resources */ p->p_input_resource = input_resource_New( VLC_OBJECT( p_playlist ) ); if( unlikely(p->p_input_resource == NULL) ) abort(); /* Audio output (needed for volume and device controls). */ audio_output_t *aout = input_resource_GetAout( p->p_input_resource ); if( aout != NULL ) input_resource_PutAout( p->p_input_resource, aout ); /* Initialize the shared HTTP cookie jar */ vlc_value_t cookies; cookies.p_address = vlc_http_cookies_new(); if ( likely(cookies.p_address) ) { var_Create( p_playlist, "http-cookies", VLC_VAR_ADDRESS ); var_SetChecked( p_playlist, "http-cookies", VLC_VAR_ADDRESS, cookies ); } /* Thread */ playlist_Activate (p_playlist); /* Add service discovery modules */ char *mods = var_InheritString( p_playlist, "services-discovery" ); if( mods != NULL ) { char *s = mods, *m; while( (m = strsep( &s, " :," )) != NULL ) playlist_ServicesDiscoveryAdd( p_playlist, m ); free( mods ); } return p_playlist; }