]> git.sesse.net Git - vlc/blob - src/libvlc.c
macosx/configure.sh: Enable libsamplerate again
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: libvlc instances creation and deletion, interfaces handling
3  *****************************************************************************
4  * Copyright (C) 1998-2008 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *          RĂ©mi Denis-Courmont <rem # videolan : org>
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation; either version 2.1 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 /** \file
29  * This file contains functions to create and destroy libvlc instances
30  */
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40 #include "../lib/libvlc_internal.h"
41 #include <vlc_input.h>
42
43 #include "modules/modules.h"
44 #include "config/configuration.h"
45 #include "playlist/preparser.h"
46
47 #include <stdio.h>                                              /* sprintf() */
48 #include <string.h>
49 #include <stdlib.h>                                                /* free() */
50 #include <errno.h>
51
52 #include "config/vlc_getopt.h"
53
54 #ifdef HAVE_DBUS
55 /* used for one-instance mode */
56 #   include <dbus/dbus.h>
57 #endif
58
59
60 #include <vlc_playlist.h>
61 #include <vlc_interface.h>
62
63 #include <vlc_charset.h>
64 #include <vlc_fs.h>
65 #include <vlc_cpu.h>
66 #include <vlc_url.h>
67 #include <vlc_modules.h>
68
69 #include "libvlc.h"
70 #include "playlist/playlist_internal.h"
71 #include "misc/variables.h"
72
73 #include <vlc_vlm.h>
74
75 #ifdef __APPLE__
76 # include <libkern/OSAtomic.h>
77 #endif
78
79 #include <assert.h>
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 static void GetFilenames  ( libvlc_int_t *, unsigned, const char *const [] );
85
86 /**
87  * Allocate a blank libvlc instance, also setting the exit handler.
88  * Vlc's threading system must have been initialized first
89  */
90 libvlc_int_t * libvlc_InternalCreate( void )
91 {
92     libvlc_int_t *p_libvlc;
93     libvlc_priv_t *priv;
94
95     /* Allocate a libvlc instance object */
96     p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
97                                   "libvlc" );
98     if( p_libvlc == NULL )
99         return NULL;
100
101     priv = libvlc_priv (p_libvlc);
102     priv->playlist = NULL;
103     priv->p_dialog_provider = NULL;
104     priv->p_vlm = NULL;
105
106     vlc_ExitInit( &priv->exit );
107
108     return p_libvlc;
109 }
110
111 /**
112  * Initialize a libvlc instance
113  * This function initializes a previously allocated libvlc instance:
114  *  - CPU detection
115  *  - gettext initialization
116  *  - message queue, module bank and playlist initialization
117  *  - configuration and commandline parsing
118  */
119 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
120                          const char *ppsz_argv[] )
121 {
122     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
123     char *       psz_modules = NULL;
124     char *       psz_parser = NULL;
125     char *       psz_control = NULL;
126     char        *psz_val;
127
128     /* System specific initialization code */
129     system_Init();
130
131     /* Initialize the module bank and load the configuration of the
132      * core module. We need to do this at this stage to be able to display
133      * a short help if required by the user. (short help == core module
134      * options) */
135     module_InitBank ();
136
137     /* Get command line options that affect module loading. */
138     if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
139     {
140         module_EndBank (false);
141         return VLC_EGENERIC;
142     }
143
144     vlc_LogInit (p_libvlc);
145     vlc_threads_setup (p_libvlc);
146
147     /* Load the builtins and plugins into the module_bank.
148      * We have to do it before config_Load*() because this also gets the
149      * list of configuration options exported by each module and loads their
150      * default values. */
151     size_t module_count = module_LoadPlugins (p_libvlc);
152
153     /*
154      * Override default configuration with config file settings
155      */
156     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
157     {
158         if( var_InheritBool( p_libvlc, "reset-config" ) )
159             config_SaveConfigFile( p_libvlc ); /* Save default config */
160         else
161             config_LoadConfigFile( p_libvlc );
162     }
163
164     /*
165      * Override configuration with command line settings
166      */
167     int vlc_optind;
168     if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
169     {
170         module_EndBank (true);
171         vlc_LogDeinit (p_libvlc);
172         return VLC_EGENERIC;
173     }
174
175     /*
176      * Support for gettext
177      */
178 #if defined( ENABLE_NLS ) \
179      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
180     vlc_bindtextdomain (PACKAGE_NAME);
181 #endif
182     /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
183     msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
184
185     if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
186     {
187         module_EndBank (true);
188         exit(0);
189     }
190
191     if( module_count <= 1 )
192     {
193         msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
194         module_EndBank (true);
195         vlc_LogDeinit (p_libvlc);
196         return VLC_ENOMOD;
197     }
198
199 #ifdef HAVE_DAEMON
200     /* Check for daemon mode */
201     if( var_InheritBool( p_libvlc, "daemon" ) )
202     {
203         if( daemon( 1, 0) != 0 )
204         {
205             msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
206             module_EndBank (true);
207             vlc_LogDeinit (p_libvlc);
208             return VLC_ENOMEM;
209         }
210
211         /* lets check if we need to write the pidfile */
212         char *pidfile = var_InheritString( p_libvlc, "pidfile" );
213         if( pidfile != NULL )
214         {
215             FILE *stream = vlc_fopen( pidfile, "w" );
216             if( stream != NULL )
217             {
218                 fprintf( stream, "%d", (int)getpid() );
219                 fclose( stream );
220                 msg_Dbg( p_libvlc, "written PID file %s", pidfile );
221             }
222             else
223                 msg_Err( p_libvlc, "cannot write PID file %s: %s",
224                          pidfile, vlc_strerror_c(errno) );
225             free( pidfile );
226         }
227     }
228     else
229     {
230         var_Create( p_libvlc, "pidfile", VLC_VAR_STRING );
231         var_SetString( p_libvlc, "pidfile", "" );
232     }
233 #endif
234
235 /* FIXME: could be replaced by using Unix sockets */
236 #ifdef HAVE_DBUS
237
238 #define MPRIS_APPEND "/org/mpris/MediaPlayer2/TrackList/Append"
239 #define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
240 #define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
241 #define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"
242
243     if( var_InheritBool( p_libvlc, "one-instance" )
244     || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
245       && var_InheritBool( p_libvlc, "started-from-file" ) ) )
246     {
247         for( int i = vlc_optind; i < i_argc; i++ )
248             if( ppsz_argv[i][0] == ':' )
249             {
250                 msg_Err( p_libvlc, "item option %s incompatible with single instance",
251                          ppsz_argv[i] );
252                 goto dbus_out;
253             }
254
255         /* Initialise D-Bus interface, check for other instances */
256         dbus_threads_init_default();
257
258         DBusError err;
259         dbus_error_init( &err );
260
261         /* connect to the session bus */
262         DBusConnection  *conn = dbus_bus_get( DBUS_BUS_SESSION, &err );
263         if( conn == NULL )
264         {
265             msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
266                     err.message );
267             dbus_error_free( &err );
268             goto dbus_out;
269         }
270
271         /* check if VLC is available on the bus
272          * if not: D-Bus control is not enabled on the other
273          * instance and we can't pass MRLs to it */
274         /* FIXME: This check is totally brain-dead and buggy. */
275         if( !dbus_bus_name_has_owner( conn, MPRIS_BUS_NAME, &err ) )
276         {
277             dbus_connection_unref( conn );
278             if( dbus_error_is_set( &err ) )
279             {
280                 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
281             }
282             else
283                 msg_Dbg( p_libvlc, "No media player running. Continuing normally." );
284             dbus_error_free( &err );
285             goto dbus_out;
286         }
287
288         const dbus_bool_t play = !var_InheritBool( p_libvlc, "playlist-enqueue" );
289
290         msg_Warn( p_libvlc, "media player running. Exiting...");
291         for( int i = vlc_optind; i < i_argc; i++ )
292         {
293             DBusMessage *msg = dbus_message_new_method_call(
294                MPRIS_BUS_NAME, MPRIS_OBJECT_PATH, MPRIS_TRACKLIST_INTERFACE, "AddTrack" );
295             if( unlikely(msg == NULL) )
296                 continue;
297
298             /* We need to resolve relative paths in this instance */
299             char *mrl;
300             if( strstr( ppsz_argv[i], "://" ) )
301                 mrl = strdup( ppsz_argv[i] );
302             else
303                 mrl = vlc_path2uri( ppsz_argv[i], NULL );
304             if( mrl == NULL )
305             {
306                 dbus_message_unref( msg );
307                 continue;
308             }
309
310             const char *after_track = MPRIS_APPEND;
311
312             /* append MRLs */
313             if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &mrl,
314                                                 DBUS_TYPE_OBJECT_PATH, &after_track,
315                                                 DBUS_TYPE_BOOLEAN, &play,
316                                                 DBUS_TYPE_INVALID ) )
317             {
318                  dbus_message_unref( msg );
319                  msg = NULL;
320                  free( mrl );
321                  continue;
322             }
323
324             msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
325             free( mrl );
326
327             /* send message and get a handle for a reply */
328             DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
329                                                                             &err );
330             dbus_message_unref( msg );
331             if( reply == NULL )
332             {
333                 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
334                 continue;
335             }
336             dbus_message_unref( reply );
337         }
338         /* we unreference the connection when we've finished with it */
339         dbus_connection_unref( conn );
340         exit( 1 );
341     }
342 #undef MPRIS_APPEND
343 #undef MPRIS_BUS_NAME
344 #undef MPRIS_OBJECT_PATH
345 #undef MPRIS_TRACKLIST_INTERFACE
346 dbus_out:
347 #endif // HAVE_DBUS
348
349     vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
350
351     priv->b_stats = var_InheritBool( p_libvlc, "stats" );
352
353     /*
354      * Initialize hotkey handling
355      */
356     priv->actions = vlc_InitActions( p_libvlc );
357
358     /*
359      * Meta data handling
360      */
361     priv->parser = playlist_preparser_New(VLC_OBJECT(p_libvlc));
362
363     /* Create a variable for showing the fullscreen interface */
364     var_Create( p_libvlc, "intf-toggle-fscontrol", VLC_VAR_BOOL );
365     var_SetBool( p_libvlc, "intf-toggle-fscontrol", true );
366
367     /* Create a variable for the Boss Key */
368     var_Create( p_libvlc, "intf-boss", VLC_VAR_VOID );
369
370     /* Create a variable for showing the main interface */
371     var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
372
373     /* Create a variable for showing the right click menu */
374     var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
375
376     /* variables for signalling creation of new files */
377     var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
378     var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
379
380     /* some default internal settings */
381     var_Create( p_libvlc, "window", VLC_VAR_STRING );
382     /* NOTE: Because the playlist and interfaces start before this function
383      * returns control to the application (DESIGN BUG!), all these variables
384      * must be created (in place of libvlc_new()) and set to VLC defaults
385      * (in place of VLC main()) *here*. */
386     var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
387     var_SetString( p_libvlc, "user-agent",
388                    "VLC media player (LibVLC "VERSION")" );
389     var_Create( p_libvlc, "http-user-agent", VLC_VAR_STRING );
390     var_SetString( p_libvlc, "http-user-agent",
391                    "VLC/"PACKAGE_VERSION" LibVLC/"PACKAGE_VERSION );
392     var_Create( p_libvlc, "app-icon-name", VLC_VAR_STRING );
393     var_SetString( p_libvlc, "app-icon-name", PACKAGE_NAME );
394     var_Create( p_libvlc, "app-id", VLC_VAR_STRING );
395     var_SetString( p_libvlc, "app-id", "org.VideoLAN.VLC" );
396     var_Create( p_libvlc, "app-version", VLC_VAR_STRING );
397     var_SetString( p_libvlc, "app-version", PACKAGE_VERSION );
398
399     /* System specific configuration */
400     system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
401
402 #ifdef ENABLE_VLM
403     /* Initialize VLM if vlm-conf is specified */
404     psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
405     if( psz_parser )
406     {
407         priv->p_vlm = vlm_New( p_libvlc );
408         if( !priv->p_vlm )
409             msg_Err( p_libvlc, "VLM initialization failed" );
410     }
411     free( psz_parser );
412 #endif
413
414     /*
415      * Load background interfaces
416      */
417     psz_modules = var_CreateGetNonEmptyString( p_libvlc, "extraintf" );
418     psz_control = var_CreateGetNonEmptyString( p_libvlc, "control" );
419
420     if( psz_modules && psz_control )
421     {
422         char* psz_tmp;
423         if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
424         {
425             free( psz_modules );
426             psz_modules = psz_tmp;
427         }
428     }
429     else if( psz_control )
430     {
431         free( psz_modules );
432         psz_modules = strdup( psz_control );
433     }
434
435     psz_parser = psz_modules;
436     while ( psz_parser && *psz_parser )
437     {
438         char *psz_module, *psz_temp;
439         psz_module = psz_parser;
440         psz_parser = strchr( psz_module, ':' );
441         if ( psz_parser )
442         {
443             *psz_parser = '\0';
444             psz_parser++;
445         }
446         if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
447         {
448             libvlc_InternalAddIntf( p_libvlc, psz_temp );
449             free( psz_temp );
450         }
451     }
452     free( psz_modules );
453     free( psz_control );
454
455 #ifdef HAVE_SYSLOG_H
456     if( var_InheritBool( p_libvlc, "syslog" ) )
457     {
458         char *logmode = var_CreateGetNonEmptyString( p_libvlc, "logmode" );
459         var_SetString( p_libvlc, "logmode", "syslog" );
460         libvlc_InternalAddIntf( p_libvlc, "logger,none" );
461
462         if( logmode )
463         {
464             var_SetString( p_libvlc, "logmode", logmode );
465             free( logmode );
466         }
467         var_Destroy( p_libvlc, "logmode" );
468     }
469     else
470 #endif
471     if( var_InheritBool( p_libvlc, "file-logging" ) )
472         libvlc_InternalAddIntf( p_libvlc, "logger,none" );
473
474     if( var_InheritBool( p_libvlc, "network-synchronisation") )
475         libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
476
477 #ifdef __APPLE__
478     var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
479     var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
480     var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
481     var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
482     var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
483     var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
484     var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
485     var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
486     var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
487 #endif
488 #if defined (_WIN32) || defined (__OS2__)
489     var_Create( p_libvlc, "drawable-hwnd", VLC_VAR_INTEGER );
490 #endif
491
492     /*
493      * Get input filenames given as commandline arguments.
494      * We assume that the remaining parameters are filenames
495      * and their input options.
496      */
497     GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
498
499     /*
500      * Get --open argument
501      */
502     psz_val = var_InheritString( p_libvlc, "open" );
503     if ( psz_val != NULL )
504     {
505         intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 );
506         free( psz_val );
507     }
508
509     return VLC_SUCCESS;
510 }
511
512 /**
513  * Cleanup a libvlc instance. The instance is not completely deallocated
514  * \param p_libvlc the instance to clean
515  */
516 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
517 {
518     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
519
520     /* Ask the interfaces to stop and destroy them */
521     msg_Dbg( p_libvlc, "removing all interfaces" );
522     libvlc_Quit( p_libvlc );
523     intf_DestroyAll( p_libvlc );
524
525 #ifdef ENABLE_VLM
526     /* Destroy VLM if created in libvlc_InternalInit */
527     if( priv->p_vlm )
528     {
529         vlm_Delete( priv->p_vlm );
530     }
531 #endif
532
533 #if !defined( _WIN32 ) && !defined( __OS2__ )
534     char *pidfile = var_InheritString( p_libvlc, "pidfile" );
535     if( pidfile != NULL )
536     {
537         msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
538         if( unlink( pidfile ) )
539             msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
540                       pidfile, vlc_strerror_c(errno) );
541         free( pidfile );
542     }
543 #endif
544
545     if (priv->parser != NULL)
546         playlist_preparser_Delete(priv->parser);
547
548     vlc_DeinitActions( p_libvlc, priv->actions );
549
550     /* Save the configuration */
551     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
552         config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
553
554     /* Free module bank. It is refcounted, so we call this each time  */
555     module_EndBank (true);
556     vlc_LogDeinit (p_libvlc);
557 #if defined(_WIN32) || defined(__OS2__)
558     system_End( );
559 #endif
560 }
561
562 /**
563  * Destroy everything.
564  * This function requests the running threads to finish, waits for their
565  * termination, and destroys their structure.
566  * It stops the thread systems: no instance can run after this has run
567  * \param p_libvlc the instance to destroy
568  */
569 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
570 {
571     libvlc_priv_t *priv = libvlc_priv( p_libvlc );
572
573     vlc_ExitDestroy( &priv->exit );
574
575     assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
576     vlc_object_release( p_libvlc );
577 }
578
579 /*****************************************************************************
580  * GetFilenames: parse command line options which are not flags
581  *****************************************************************************
582  * Parse command line for input files as well as their associated options.
583  * An option always follows its associated input and begins with a ":".
584  *****************************************************************************/
585 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
586                           const char *const args[] )
587 {
588     while( n > 0 )
589     {
590         /* Count the input options */
591         unsigned i_options = 0;
592
593         while( args[--n][0] == ':' )
594         {
595             i_options++;
596             if( n == 0 )
597             {
598                 msg_Warn( p_vlc, "options %s without item", args[n] );
599                 return; /* syntax!? */
600             }
601         }
602
603         char *mrl = NULL;
604         if( strstr( args[n], "://" ) == NULL )
605         {
606             mrl = vlc_path2uri( args[n], NULL );
607             if( !mrl )
608                 continue;
609         }
610
611         intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
612                          ( i_options ? &args[n + 1] : NULL ),
613                          VLC_INPUT_OPTION_TRUSTED );
614         free( mrl );
615     }
616 }
617
618 /**
619  * Requests extraction of the meta data for an input item (a.k.a. preparsing).
620  * The actual extraction is asynchronous.
621  */
622 int libvlc_MetaRequest(libvlc_int_t *libvlc, input_item_t *item,
623                        input_item_meta_request_option_t i_options)
624 {
625     libvlc_priv_t *priv = libvlc_priv(libvlc);
626
627     if (unlikely(priv->parser == NULL))
628         return VLC_ENOMEM;
629
630     playlist_preparser_Push(priv->parser, item, i_options);
631     return VLC_SUCCESS;
632 }
633
634 /**
635  * Requests retrieving/downloading art for an input item.
636  * The retrieval is performed asynchronously.
637  */
638 int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
639                       input_item_meta_request_option_t i_options)
640 {
641     libvlc_priv_t *priv = libvlc_priv(libvlc);
642
643     if (unlikely(priv->parser == NULL))
644         return VLC_ENOMEM;
645
646     playlist_preparser_fetcher_Push(priv->parser, item, i_options);
647     return VLC_SUCCESS;
648 }