]> git.sesse.net Git - vlc/blob - src/control/core.c
libvlc: Remove unused mutex.
[vlc] / src / control / core.c
1 /*****************************************************************************
2  * core.c: Core libvlc new API functions : initialization, exceptions handling
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26
27 #include <vlc_interface.h>
28 #include <vlc_vlm.h>
29
30 #include <stdarg.h>
31 #include <limits.h>
32 #include <assert.h>
33
34 static const char nomemstr[] = "Insufficient memory";
35
36 /*************************************************************************
37  * Exceptions handling
38  *************************************************************************/
39 void libvlc_exception_init( libvlc_exception_t *p_exception )
40 {
41     p_exception->b_raised = 0;
42 }
43
44 void libvlc_exception_clear( libvlc_exception_t *p_exception )
45 {
46     if( NULL == p_exception )
47         return;
48     p_exception->b_raised = 0;
49     libvlc_clearerr ();
50 }
51
52 int libvlc_exception_raised( const libvlc_exception_t *p_exception )
53 {
54     return (NULL != p_exception) && p_exception->b_raised;
55 }
56
57 static void libvlc_exception_not_handled( const char *psz )
58 {
59     fprintf( stderr, "*** LibVLC Exception not handled: %s\nSet a breakpoint in '%s' to debug.\n",
60              psz, __func__ );
61     abort();
62 }
63
64 void libvlc_exception_raise( libvlc_exception_t *p_exception,
65                              const char *psz_format, ... )
66 {
67     va_list args;
68
69     /* Make sure that there is no unnoticed previous exception */
70     if( p_exception && p_exception->b_raised )
71     {
72         libvlc_exception_not_handled( libvlc_errmsg() );
73         libvlc_exception_clear( p_exception );
74     }
75
76     /* Unformat-ize the message */
77     va_start( args, psz_format );
78     libvlc_vprinterr( psz_format, args );
79     va_end( args );
80
81     /* Does caller care about exceptions ? */
82     if( p_exception == NULL ) {
83         /* Print something, so that lazy third-parties can easily
84          * notice that something may have gone unnoticedly wrong */
85         libvlc_exception_not_handled( libvlc_errmsg() );
86         return;
87     }
88
89     p_exception->b_raised = 1;
90 }
91
92 libvlc_instance_t * libvlc_new( int argc, const char *const *argv,
93                                 libvlc_exception_t *p_e )
94 {
95     libvlc_instance_t *p_new;
96     int i_ret;
97
98     libvlc_init_threads ();
99
100     libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
101     if( !p_libvlc_int )
102     {
103         libvlc_deinit_threads ();
104         RAISENULL( "VLC initialization failed" );
105     }
106
107     p_new = malloc( sizeof( libvlc_instance_t ) );
108     if( !p_new )
109     {
110         libvlc_deinit_threads ();
111         RAISENULL( "Out of memory" );
112     }
113
114     const char *my_argv[argc + 2];
115
116     my_argv[0] = "libvlc"; /* dummy arg0, skipped by getopt() et al */
117     for( int i = 0; i < argc; i++ )
118          my_argv[i + 1] = argv[i];
119     my_argv[argc + 1] = NULL; /* C calling conventions require a NULL */
120
121     /** \todo Look for interface settings. If we don't have any, add -I dummy */
122     /* Because we probably don't want a GUI by default */
123
124     i_ret = libvlc_InternalInit( p_libvlc_int, argc + 1, my_argv );
125     if( i_ret )
126     {
127         libvlc_InternalDestroy( p_libvlc_int );
128         free( p_new );
129         libvlc_deinit_threads ();
130
131         if( i_ret == VLC_EEXITSUCCESS )
132             return NULL;
133         else
134             RAISENULL( "VLC initialization failed" );
135     }
136
137     p_new->p_libvlc_int = p_libvlc_int;
138     p_new->libvlc_vlm.p_vlm = NULL;
139     p_new->libvlc_vlm.p_event_manager = NULL;
140     p_new->libvlc_vlm.pf_release = NULL;
141     p_new->ref_count = 1;
142     p_new->verbosity = 1;
143     p_new->p_callback_list = NULL;
144     vlc_mutex_init(&p_new->instance_lock);
145
146     return p_new;
147 }
148
149 void libvlc_retain( libvlc_instance_t *p_instance )
150 {
151     assert( p_instance != NULL );
152     assert( p_instance->ref_count < UINT_MAX );
153
154     vlc_mutex_lock( &p_instance->instance_lock );
155     p_instance->ref_count++;
156     vlc_mutex_unlock( &p_instance->instance_lock );
157 }
158
159 void libvlc_release( libvlc_instance_t *p_instance )
160 {
161     vlc_mutex_t *lock = &p_instance->instance_lock;
162     int refs;
163
164     vlc_mutex_lock( lock );
165     assert( p_instance->ref_count > 0 );
166     refs = --p_instance->ref_count;
167     vlc_mutex_unlock( lock );
168
169     if( refs == 0 )
170     {
171         vlc_mutex_destroy( lock );
172         if( p_instance->libvlc_vlm.pf_release )
173             p_instance->libvlc_vlm.pf_release( p_instance );
174         libvlc_InternalCleanup( p_instance->p_libvlc_int );
175         libvlc_InternalDestroy( p_instance->p_libvlc_int );
176         free( p_instance );
177         libvlc_deinit_threads ();
178     }
179 }
180
181 int libvlc_add_intf( libvlc_instance_t *p_i, const char *name,
182                       libvlc_exception_t *p_e )
183 {
184     if( libvlc_InternalAddIntf( p_i->p_libvlc_int, name ) )
185     {
186         libvlc_exception_raise( p_e, "Interface initialization failed" );
187         return -1;
188     }
189     return 0;
190 }
191
192 void libvlc_wait( libvlc_instance_t *p_i )
193 {
194     libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
195     libvlc_InternalWait( p_libvlc );
196 }
197
198 const char * libvlc_get_version(void)
199 {
200     return VLC_Version();
201 }
202
203 const char * libvlc_get_compiler(void)
204 {
205     return VLC_Compiler();
206 }
207
208 const char * libvlc_get_changeset(void)
209 {
210     extern const char psz_vlc_changeset[];
211     return psz_vlc_changeset;
212 }
213
214 /* export internal libvlc_instance for ugly hacks with libvlccore */
215 vlc_object_t *libvlc_get_vlc_instance( libvlc_instance_t* p_instance )
216 {
217     vlc_object_hold( p_instance->p_libvlc_int ) ;
218     return (vlc_object_t*) p_instance->p_libvlc_int ;
219 }
220
221 void libvlc_free( void *ptr )
222 {
223     free( ptr );
224 }