demux: hls: compute pdt time for pre discontinuity
[vlc.git] / lib / core.c
blob3bd3292e24c810fdffd829575083ba57505bab3e
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>
38 libvlc_instance_t * libvlc_new( int argc, const char *const *argv )
40 libvlc_threads_init ();
42 libvlc_instance_t *p_new = malloc (sizeof (*p_new));
43 if (unlikely(p_new == NULL))
44 return NULL;
46 const char *my_argv[argc + 2];
47 my_argv[0] = "libvlc"; /* dummy arg0, skipped by getopt() et al */
48 for( int i = 0; i < argc; i++ )
49 my_argv[i + 1] = argv[i];
50 my_argv[argc + 1] = NULL; /* C calling conventions require a NULL */
52 libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
53 if (unlikely (p_libvlc_int == NULL))
54 goto error;
56 if (libvlc_InternalInit( p_libvlc_int, argc + 1, my_argv ))
58 libvlc_InternalDestroy( p_libvlc_int );
59 goto error;
62 p_new->p_libvlc_int = p_libvlc_int;
63 vlc_atomic_rc_init( &p_new->ref_count );
64 p_new->p_callback_list = NULL;
65 return p_new;
67 error:
68 free (p_new);
69 libvlc_threads_deinit ();
70 return NULL;
73 void libvlc_retain( libvlc_instance_t *p_instance )
75 assert( p_instance != NULL );
77 vlc_atomic_rc_inc( &p_instance->ref_count );
80 void libvlc_release( libvlc_instance_t *p_instance )
82 if(vlc_atomic_rc_dec( &p_instance->ref_count ))
84 libvlc_Quit( p_instance->p_libvlc_int );
85 libvlc_InternalCleanup( p_instance->p_libvlc_int );
86 libvlc_InternalDestroy( p_instance->p_libvlc_int );
87 free( p_instance );
88 libvlc_threads_deinit ();
92 void libvlc_set_exit_handler( libvlc_instance_t *p_i, void (*cb) (void *),
93 void *data )
95 libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
96 libvlc_SetExitHandler( p_libvlc, cb, data );
99 void libvlc_set_user_agent (libvlc_instance_t *p_i,
100 const char *name, const char *http)
102 libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
103 char *str;
105 var_SetString (p_libvlc, "user-agent", name);
106 if ((http != NULL)
107 && (asprintf (&str, "%s LibVLC/"PACKAGE_VERSION, http) != -1))
109 var_SetString (p_libvlc, "http-user-agent", str);
110 free (str);
114 void libvlc_set_app_id(libvlc_instance_t *p_i, const char *id,
115 const char *version, const char *icon)
117 libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
119 var_SetString(p_libvlc, "app-id", id ? id : "");
120 var_SetString(p_libvlc, "app-version", version ? version : "");
121 var_SetString(p_libvlc, "app-icon-name", icon ? icon : "");
124 const char * libvlc_get_version(void)
126 return VERSION_MESSAGE;
129 const char * libvlc_get_compiler(void)
131 return VLC_Compiler();
134 const char * libvlc_get_changeset(void)
136 extern const char psz_vlc_changeset[];
137 return psz_vlc_changeset;
140 void libvlc_free( void *ptr )
142 free( ptr );
145 static libvlc_module_description_t *module_description_list_get(
146 libvlc_instance_t *p_instance, const char *capability )
148 libvlc_module_description_t *p_list = NULL,
149 *p_actual = NULL,
150 *p_previous = NULL;
151 size_t count;
152 module_t **module_list = module_list_get( &count );
154 for (size_t i = 0; i < count; i++)
156 module_t *p_module = module_list[i];
158 if ( !module_provides( p_module, capability ) )
159 continue;
161 p_actual = ( libvlc_module_description_t * ) malloc( sizeof( libvlc_module_description_t ) );
162 if ( p_actual == NULL )
164 libvlc_printerr( "Not enough memory" );
165 libvlc_module_description_list_release( p_list );
166 module_list_free( module_list );
167 return NULL;
170 if ( p_list == NULL )
171 p_list = p_actual;
173 const char* name = module_get_object( p_module );
174 const char* shortname = module_get_name( p_module, false );
175 const char* longname = module_get_name( p_module, true );
176 const char* help = module_get_help( p_module );
177 p_actual->psz_name = name ? strdup( name ) : NULL;
178 p_actual->psz_shortname = shortname ? strdup( shortname ) : NULL;
179 p_actual->psz_longname = longname ? strdup( longname ) : NULL;
180 p_actual->psz_help = help ? strdup( help ) : NULL;
182 p_actual->p_next = NULL;
183 if ( p_previous )
184 p_previous->p_next = p_actual;
185 p_previous = p_actual;
188 module_list_free( module_list );
189 VLC_UNUSED( p_instance );
190 return p_list;
193 void libvlc_module_description_list_release( libvlc_module_description_t *p_list )
195 libvlc_module_description_t *p_actual, *p_before;
196 p_actual = p_list;
198 while ( p_actual )
200 free( p_actual->psz_name );
201 free( p_actual->psz_shortname );
202 free( p_actual->psz_longname );
203 free( p_actual->psz_help );
204 p_before = p_actual;
205 p_actual = p_before->p_next;
206 free( p_before );
210 libvlc_module_description_t *libvlc_audio_filter_list_get( libvlc_instance_t *p_instance )
212 return module_description_list_get( p_instance, "audio filter" );
215 libvlc_module_description_t *libvlc_video_filter_list_get( libvlc_instance_t *p_instance )
217 return module_description_list_get( p_instance, "video filter" );
220 int64_t libvlc_clock(void)
222 return US_FROM_VLC_TICK(vlc_tick_now());
225 const char vlc_module_name[] = "libvlc";