demux:mkv: fix wrong value reset after clean of an array
[vlc.git] / lib / core.c
blob6edf14b1547a77434850a79517e277ec1da881d4
1 /*****************************************************************************
2 * core.c: Core libvlc new API functions : initialization
3 *****************************************************************************
4 * Copyright (C) 2005 VLC authors and VideoLAN
6 * Authors: Clément Stenac <zorglub@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include "libvlc_internal.h"
28 #include <vlc_modules.h>
29 #include <vlc/vlc.h>
31 #include <vlc_interface.h>
33 #include <stdarg.h>
34 #include <limits.h>
35 #include <assert.h>
37 #include "../src/revision.c"
39 libvlc_instance_t * libvlc_new( int argc, const char *const *argv )
41 libvlc_threads_init ();
43 libvlc_instance_t *p_new = malloc (sizeof (*p_new));
44 if (unlikely(p_new == NULL))
45 return NULL;
47 const char *my_argv[argc + 2];
48 my_argv[0] = "libvlc"; /* dummy arg0, skipped by getopt() et al */
49 for( int i = 0; i < argc; i++ )
50 my_argv[i + 1] = argv[i];
51 my_argv[argc + 1] = NULL; /* C calling conventions require a NULL */
53 libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
54 if (unlikely (p_libvlc_int == NULL))
55 goto error;
57 if (libvlc_InternalInit( p_libvlc_int, argc + 1, my_argv ))
59 libvlc_InternalDestroy( p_libvlc_int );
60 goto error;
63 p_new->p_libvlc_int = p_libvlc_int;
64 p_new->ref_count = 1;
65 p_new->p_callback_list = NULL;
66 vlc_mutex_init(&p_new->instance_lock);
67 return p_new;
69 error:
70 free (p_new);
71 libvlc_threads_deinit ();
72 return NULL;
75 void libvlc_retain( libvlc_instance_t *p_instance )
77 assert( p_instance != NULL );
78 assert( p_instance->ref_count < UINT_MAX );
80 vlc_mutex_lock( &p_instance->instance_lock );
81 p_instance->ref_count++;
82 vlc_mutex_unlock( &p_instance->instance_lock );
85 void libvlc_release( libvlc_instance_t *p_instance )
87 vlc_mutex_t *lock = &p_instance->instance_lock;
88 int refs;
90 vlc_mutex_lock( lock );
91 assert( p_instance->ref_count > 0 );
92 refs = --p_instance->ref_count;
93 vlc_mutex_unlock( lock );
95 if( refs == 0 )
97 vlc_mutex_destroy( lock );
98 libvlc_Quit( p_instance->p_libvlc_int );
99 libvlc_InternalCleanup( p_instance->p_libvlc_int );
100 libvlc_InternalDestroy( p_instance->p_libvlc_int );
101 free( p_instance );
102 libvlc_threads_deinit ();
106 void libvlc_set_exit_handler( libvlc_instance_t *p_i, void (*cb) (void *),
107 void *data )
109 libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
110 libvlc_SetExitHandler( p_libvlc, cb, data );
113 void libvlc_set_user_agent (libvlc_instance_t *p_i,
114 const char *name, const char *http)
116 libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
117 char *str;
119 var_SetString (p_libvlc, "user-agent", name);
120 if ((http != NULL)
121 && (asprintf (&str, "%s LibVLC/"PACKAGE_VERSION, http) != -1))
123 var_SetString (p_libvlc, "http-user-agent", str);
124 free (str);
128 void libvlc_set_app_id(libvlc_instance_t *p_i, const char *id,
129 const char *version, const char *icon)
131 libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
133 var_SetString(p_libvlc, "app-id", id ? id : "");
134 var_SetString(p_libvlc, "app-version", version ? version : "");
135 var_SetString(p_libvlc, "app-icon-name", icon ? icon : "");
138 const char * libvlc_get_version(void)
140 return VERSION_MESSAGE;
143 const char * libvlc_get_compiler(void)
145 return VLC_Compiler();
148 const char * libvlc_get_changeset(void)
150 extern const char psz_vlc_changeset[];
151 return psz_vlc_changeset;
154 static libvlc_module_description_t *module_description_list_get(
155 libvlc_instance_t *p_instance, const char *capability )
157 libvlc_module_description_t *p_list = NULL,
158 *p_actual = NULL,
159 *p_previous = NULL;
160 size_t count;
161 module_t **module_list = module_list_get( &count );
163 for (size_t i = 0; i < count; i++)
165 module_t *p_module = module_list[i];
167 if ( !module_provides( p_module, capability ) )
168 continue;
170 p_actual = ( libvlc_module_description_t * ) malloc( sizeof( libvlc_module_description_t ) );
171 if ( p_actual == NULL )
173 libvlc_printerr( "Not enough memory" );
174 libvlc_module_description_list_release( p_list );
175 module_list_free( module_list );
176 return NULL;
179 if ( p_list == NULL )
180 p_list = p_actual;
182 const char* name = module_get_object( p_module );
183 const char* shortname = module_get_name( p_module, false );
184 const char* longname = module_get_name( p_module, true );
185 const char* help = module_get_help( p_module );
186 p_actual->psz_name = name ? strdup( name ) : NULL;
187 p_actual->psz_shortname = shortname ? strdup( shortname ) : NULL;
188 p_actual->psz_longname = longname ? strdup( longname ) : NULL;
189 p_actual->psz_help = help ? strdup( help ) : NULL;
191 p_actual->p_next = NULL;
192 if ( p_previous )
193 p_previous->p_next = p_actual;
194 p_previous = p_actual;
197 module_list_free( module_list );
198 VLC_UNUSED( p_instance );
199 return p_list;
202 void libvlc_module_description_list_release( libvlc_module_description_t *p_list )
204 libvlc_module_description_t *p_actual, *p_before;
205 p_actual = p_list;
207 while ( p_actual )
209 free( p_actual->psz_name );
210 free( p_actual->psz_shortname );
211 free( p_actual->psz_longname );
212 free( p_actual->psz_help );
213 p_before = p_actual;
214 p_actual = p_before->p_next;
215 free( p_before );
219 libvlc_module_description_t *libvlc_audio_filter_list_get( libvlc_instance_t *p_instance )
221 return module_description_list_get( p_instance, "audio filter" );
224 libvlc_module_description_t *libvlc_video_filter_list_get( libvlc_instance_t *p_instance )
226 return module_description_list_get( p_instance, "video filter" );
229 int64_t libvlc_clock(void)
231 return US_FROM_VLC_TICK(vlc_tick_now());
234 const char vlc_module_name[] = "libvlc";