forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibvlc.c
512 lines (426 loc) · 16 KB
/
libvlc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*****************************************************************************
* libvlc.c: libvlc instances creation and deletion, interfaces handling
*****************************************************************************
* Copyright (C) 1998-2008 VLC authors and VideoLAN
*
* Authors: Vincent Seguin <[email protected]>
* Samuel Hocevar <[email protected]>
* Gildas Bazin <[email protected]>
* Derk-Jan Hartman <hartman at videolan dot org>
* Rémi Denis-Courmont
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/** \file
* This file contains functions to create and destroy libvlc instances
*/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <vlc_preparser.h>
#include "../lib/libvlc_internal.h"
#include "modules/modules.h"
#include "config/configuration.h"
#include "media_source/media_source.h"
#include <stdio.h> /* sprintf() */
#include <string.h>
#include <stdlib.h> /* free() */
#include <errno.h>
#include "config/vlc_getopt.h"
#include <vlc_playlist.h>
#include <vlc_interface.h>
#include <vlc_actions.h>
#include <vlc_charset.h>
#include <vlc_dialog.h>
#include <vlc_keystore.h>
#include <vlc_fs.h>
#include <vlc_cpu.h>
#include <vlc_url.h>
#include <vlc_modules.h>
#include <vlc_media_library.h>
#include <vlc_thumbnailer.h>
#include <vlc_tracer.h>
#include "libvlc.h"
#include <vlc_vlm.h>
#include <assert.h>
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static void GetFilenames ( libvlc_int_t *, unsigned, const char *const [] );
/**
* 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;
/* Allocate a libvlc instance object */
p_libvlc = (vlc_custom_create)( NULL, sizeof (*priv), "libvlc" );
if( p_libvlc == NULL )
return NULL;
priv = libvlc_priv (p_libvlc);
vlc_mutex_init(&priv->lock);
priv->interfaces = NULL;
priv->main_playlist = NULL;
priv->p_vlm = NULL;
priv->media_source_provider = NULL;
vlc_ExitInit( &priv->exit );
return p_libvlc;
}
static void libvlc_AddInterfaces(libvlc_int_t *libvlc, const char *varname)
{
char *str = var_InheritString(libvlc, varname);
if (str == NULL)
return;
char *state;
char *intf = strtok_r(str, ":", &state);
while (intf != NULL) {
libvlc_InternalAddIntf(libvlc, intf);
intf = strtok_r(NULL, ":", &state);
}
free(str);
}
/**
* 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_val;
int i_ret = VLC_EGENERIC;
if (unlikely(vlc_LogPreinit(p_libvlc)))
return VLC_ENOMEM;
/* System specific initialization code */
system_Init();
/*
* Initialize the module bank and load the core config only.
*/
module_InitBank ();
/*
* Perform early check for commandline arguments that affect module loading
* or vlc_threads_setup()
*/
config_CmdLineEarlyScan( p_libvlc, i_argc, ppsz_argv );
vlc_threads_setup (p_libvlc);
/*
* Load plugin data into the module bank.
* We need to do this here such that option sets from plugins are added to
* the config system in order that full commandline argument parsing and
* saved settings handling can function properly.
*/
module_LoadPlugins (p_libvlc);
/*
* Fully process command line settings.
* Results are stored as runtime state within `p_libvlc` object variables.
*/
int vlc_optind;
if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
goto error;
/*
* Load saved settings into the config system, as applicable.
*/
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 );
}
vlc_LogInit(p_libvlc);
char *tracer_name = var_InheritString(p_libvlc, "tracer");
priv->tracer = vlc_tracer_Create(VLC_OBJECT(p_libvlc), tracer_name);
free(tracer_name);
/*
* 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") );
/*
* Handle info requests such as for help or version text.
*/
if (config_PrintHelp (p_libvlc))
{
libvlc_InternalCleanup (p_libvlc);
exit(0);
}
i_ret = VLC_ENOMEM;
if( libvlc_InternalDialogInit( p_libvlc ) != VLC_SUCCESS )
goto error;
if( libvlc_InternalKeystoreInit( p_libvlc ) != VLC_SUCCESS )
msg_Warn( p_libvlc, "memory keystore init failed" );
vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
if( var_InheritBool( p_libvlc, "media-library") )
{
priv->p_media_library = libvlc_MlCreate( p_libvlc );
if ( priv->p_media_library == NULL )
msg_Warn( p_libvlc, "Media library initialization failed" );
}
priv->p_thumbnailer = vlc_thumbnailer_Create( VLC_OBJECT( p_libvlc ) );
if ( priv->p_thumbnailer == NULL )
msg_Warn( p_libvlc, "Failed to instantiate thumbnailer" );
/*
* Initialize hotkey handling
*/
if( libvlc_InternalActionsInit( p_libvlc ) != VLC_SUCCESS )
goto error;
/*
* Meta data handling
*/
priv->parser = vlc_preparser_New(VLC_OBJECT(p_libvlc));
if( !priv->parser )
goto error;
priv->media_source_provider = vlc_media_source_provider_New( VLC_OBJECT( p_libvlc ) );
if( !priv->media_source_provider )
goto error;
/* 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 );
var_Create( p_libvlc, "vout-cb-type", VLC_VAR_INTEGER );
/* 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 */
char *psz_parser = var_InheritString( p_libvlc, "vlm-conf" );
if( psz_parser )
{
priv->p_vlm = vlm_New( p_libvlc, psz_parser );
if( !priv->p_vlm )
msg_Err( p_libvlc, "VLM initialization failed" );
free( psz_parser );
}
#endif
/*
* Load background interfaces
*/
libvlc_AddInterfaces(p_libvlc, "extraintf");
libvlc_AddInterfaces(p_libvlc, "control");
#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 );
}
/* Callbacks between interfaces */
/* Create a variable for showing the right click menu */
var_Create(p_libvlc, "intf-popupmenu", VLC_VAR_BOOL);
/* Create a variable for showing the fullscreen interface */
var_Create(p_libvlc, "intf-toggle-fscontrol", VLC_VAR_VOID);
/* 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_VOID);
return VLC_SUCCESS;
error:
libvlc_InternalCleanup( p_libvlc );
return i_ret;
}
/**
* Cleanup a libvlc instance. The instance is not completely deallocated
* \param p_libvlc the instance to clean
*/
void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
{
libvlc_priv_t *priv = libvlc_priv (p_libvlc);
if (priv->parser != NULL)
vlc_preparser_Deactivate(priv->parser);
/* Ask the interfaces to stop and destroy them */
msg_Dbg( p_libvlc, "removing all interfaces" );
intf_DestroyAll( p_libvlc );
if ( priv->p_thumbnailer )
vlc_thumbnailer_Release( priv->p_thumbnailer );
#ifdef ENABLE_VLM
/* Destroy VLM if created in libvlc_InternalInit */
if( priv->p_vlm )
{
vlm_Delete( priv->p_vlm );
}
#endif
#if !defined( _WIN32 ) && !defined( __OS2__ )
char *pidfile = var_InheritString( p_libvlc, "pidfile" );
if( pidfile != 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
if (priv->parser != NULL)
vlc_preparser_Delete(priv->parser);
if (priv->main_playlist)
vlc_playlist_Delete(priv->main_playlist);
if ( priv->p_media_library )
libvlc_MlRelease( priv->p_media_library );
if( priv->media_source_provider )
vlc_media_source_provider_Delete( priv->media_source_provider );
libvlc_InternalDialogClean( p_libvlc );
libvlc_InternalKeystoreClean( p_libvlc );
libvlc_InternalActionsClean( p_libvlc );
/* Save the configuration */
if( !var_InheritBool( p_libvlc, "ignore-config" ) )
config_AutoSaveConfigFile( p_libvlc );
vlc_LogDestroy(p_libvlc->obj.logger);
if (priv->tracer != NULL)
vlc_tracer_Destroy(priv->tracer);
/* Free module bank. It is refcounted, so we call this each time */
module_EndBank (true);
#if defined(_WIN32) || defined(__OS2__)
system_End( );
#endif
}
/**
* Destroy libvlc instance.
* \param p_libvlc the instance to destroy
*/
void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
{
vlc_object_delete(p_libvlc);
}
/*****************************************************************************
* GetFilenames: parse command line options which are not flags
*****************************************************************************
* Parse command line for input files as well as their associated options.
* An option always follows its associated input and begins with a ":".
*****************************************************************************/
static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
const char *const args[] )
{
while( n > 0 )
{
/* Count the input options */
unsigned i_options = 0;
while( args[--n][0] == ':' )
{
i_options++;
if( n == 0 )
{
msg_Warn( p_vlc, "options %s without item", args[n] );
return; /* syntax!? */
}
}
char *mrl = NULL;
if( strstr( args[n], "://" ) == NULL )
{
mrl = vlc_path2uri( args[n], NULL );
if( !mrl )
continue;
}
intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
( i_options ? &args[n + 1] : NULL ),
VLC_INPUT_OPTION_TRUSTED );
free( mrl );
}
}
int vlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
input_item_meta_request_option_t i_options,
const struct vlc_metadata_cbs *cbs,
void *cbs_userdata,
int timeout, void *id)
{
libvlc_priv_t *priv = libvlc_priv(libvlc);
if (unlikely(priv->parser == NULL))
return VLC_ENOMEM;
return vlc_preparser_Push( priv->parser, item, i_options, cbs,
cbs_userdata, timeout, id );
}
/**
* Requests extraction of the meta data for an input item (a.k.a. preparsing).
* The actual extraction is asynchronous. It can be cancelled with
* libvlc_MetadataCancel()
*/
int libvlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
input_item_meta_request_option_t i_options,
const struct vlc_metadata_cbs *cbs,
void *cbs_userdata,
int timeout, void *id)
{
libvlc_priv_t *priv = libvlc_priv(libvlc);
assert(i_options & META_REQUEST_OPTION_SCOPE_ANY ||
i_options & META_REQUEST_OPTION_FETCH_ANY);
if (unlikely(priv->parser == NULL))
return VLC_ENOMEM;
vlc_mutex_lock( &item->lock );
if( item->i_preparse_depth == 0 )
item->i_preparse_depth = 1;
vlc_mutex_unlock( &item->lock );
return vlc_MetadataRequest(libvlc, item, i_options, cbs, cbs_userdata, timeout, id);
}
/**
* Cancels extraction of the meta data for an input item.
*
* This does nothing if the input item is already processed or if it was not
* added with libvlc_MetadataRequest()
*/
void libvlc_MetadataCancel(libvlc_int_t *libvlc, void *id)
{
libvlc_priv_t *priv = libvlc_priv(libvlc);
if (unlikely(priv->parser == NULL))
return;
vlc_preparser_Cancel(priv->parser, id);
}