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