1 /*****************************************************************************
2 * core.c: Core libvlc new API functions : initialization, exceptions handling
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
23 #include "libvlc_internal.h"
24 #include <vlc/libvlc.h>
26 #include <vlc_interface.h>
32 static const char nomemstr[] = "Insufficient memory";
34 /*************************************************************************
36 *************************************************************************/
37 void libvlc_exception_init( libvlc_exception_t *p_exception )
39 p_exception->b_raised = 0;
40 p_exception->psz_message = NULL;
43 void libvlc_exception_clear( libvlc_exception_t *p_exception )
45 if( p_exception->psz_message != nomemstr )
46 free( p_exception->psz_message );
47 p_exception->psz_message = NULL;
48 p_exception->b_raised = 0;
51 int libvlc_exception_raised( const libvlc_exception_t *p_exception )
53 return (NULL != p_exception) && p_exception->b_raised;
57 libvlc_exception_get_message( const libvlc_exception_t *p_exception )
59 if( p_exception->b_raised == 1 && p_exception->psz_message )
61 return p_exception->psz_message;
66 void libvlc_exception_not_handled( const char *psz )
68 fprintf( stderr, "*** LibVLC Exception not handled: %s\nSet a breakpoint in '%s' to debug.\n", psz, __FUNCTION__ );
71 void libvlc_exception_raise( libvlc_exception_t *p_exception,
72 const char *psz_format, ... )
77 /* Unformat-ize the message */
78 va_start( args, psz_format );
79 if( vasprintf( &psz, psz_format, args ) == -1)
80 psz = (char *)nomemstr;
83 /* Does caller care about exceptions ? */
84 if( p_exception == NULL ) {
85 /* Print something, so lazy third-parties can easily
86 * notice that something may have gone unoticedly wrong */
87 libvlc_exception_not_handled( psz );
91 /* Make sure that there is no unoticed previous exception */
92 if( p_exception->b_raised )
94 libvlc_exception_not_handled( p_exception->psz_message );
95 libvlc_exception_clear( p_exception );
97 p_exception->psz_message = psz;
98 p_exception->b_raised = 1;
101 libvlc_instance_t * libvlc_new( int argc, const char *const *argv,
102 libvlc_exception_t *p_e )
104 libvlc_instance_t *p_new;
106 libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
107 if( !p_libvlc_int ) RAISENULL( "VLC initialization failed" );
109 p_new = (libvlc_instance_t *)malloc( sizeof( libvlc_instance_t ) );
110 if( !p_new ) RAISENULL( "Out of memory" );
112 const char *my_argv[argc + 2];
114 my_argv[0] = "libvlc"; /* dummy arg0, skipped by getopt() et al */
115 for( int i = 0; i < argc; i++ )
116 my_argv[i + 1] = argv[i];
117 my_argv[argc + 1] = NULL; /* C calling conventions require a NULL */
119 /** \todo Look for interface settings. If we don't have any, add -I dummy */
120 /* Because we probably don't want a GUI by default */
122 if( libvlc_InternalInit( p_libvlc_int, argc + 1, my_argv ) )
123 RAISENULL( "VLC initialization failed" );
125 p_new->p_libvlc_int = p_libvlc_int;
127 p_new->b_playlist_locked = 0;
128 p_new->ref_count = 1;
129 p_new->p_callback_list = NULL;
130 vlc_mutex_init(p_libvlc_int, &p_new->instance_lock);
131 vlc_mutex_init(p_libvlc_int, &p_new->event_callback_lock);
133 libvlc_event_init(p_new, p_e);
138 void libvlc_retain( libvlc_instance_t *p_instance )
140 assert( p_instance != NULL );
141 assert( p_instance->ref_count < UINT_MAX );
143 vlc_mutex_lock( &p_instance->instance_lock );
144 p_instance->ref_count++;
145 vlc_mutex_unlock( &p_instance->instance_lock );
148 void libvlc_release( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
150 vlc_mutex_t *lock = &p_instance->instance_lock;
153 assert( p_instance->ref_count > 0 );
155 vlc_mutex_lock( lock );
156 refs = --p_instance->ref_count;
158 libvlc_event_fini( p_instance );
159 vlc_mutex_unlock( lock );
163 vlc_mutex_destroy( lock );
164 vlc_mutex_destroy( &p_instance->event_callback_lock );
165 libvlc_InternalCleanup( p_instance->p_libvlc_int );
166 libvlc_InternalDestroy( p_instance->p_libvlc_int, VLC_FALSE );
171 int libvlc_get_vlc_id( libvlc_instance_t *p_instance )
173 return p_instance->p_libvlc_int->i_object_id;