2 * \file mlt_repository.c
3 * \brief provides a map between service and shared objects
4 * \see mlt_repository_s
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
8 * \author Dan Dennedy <dan@dennedy.org>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "mlt_repository.h"
26 #include "mlt_properties.h"
27 #include "mlt_tokeniser.h"
29 #include "mlt_factory.h"
38 /** \brief Repository class
40 * The Repository is a collection of plugin modules and their services and service metadata.
42 * \extends mlt_properties_s
43 * \properties \p language a cached list of user locales
46 struct mlt_repository_s
48 struct mlt_properties_s parent; /// a list of object files
49 mlt_properties consumers; /// a list of entry points for consumers
50 mlt_properties filters; /// a list of entry points for filters
51 mlt_properties producers; /// a list of entry points for producers
52 mlt_properties transitions; /// a list of entry points for transitions
55 /** Construct a new repository.
57 * \public \memberof mlt_repository_s
58 * \param directory the full path of a directory from which to read modules
59 * \return a new repository or NULL if failed
62 mlt_repository mlt_repository_init( const char *directory )
65 if ( directory == NULL || strcmp( directory, "" ) == 0 )
68 // Construct the repository
69 mlt_repository self = calloc( sizeof( struct mlt_repository_s ), 1 );
70 mlt_properties_init( &self->parent, self );
71 self->consumers = mlt_properties_new();
72 self->filters = mlt_properties_new();
73 self->producers = mlt_properties_new();
74 self->transitions = mlt_properties_new();
76 // Get the directory list
77 mlt_properties dir = mlt_properties_new();
78 int count = mlt_properties_dir_list( dir, directory, NULL, 0 );
82 for ( i = 0; i < count; i++ )
85 const char *object_name = mlt_properties_get_value( dir, i);
87 // Very temporary hack to allow the quicktime plugins to work
88 // TODO: extend repository to allow this to be used on a case by case basis
89 if ( strstr( object_name, "libmltkino" ) )
92 // Open the shared object
93 void *object = dlopen( object_name, flags );
96 // Get the registration function
97 mlt_repository_callback symbol_ptr = dlsym( object, "mlt_register" );
99 // Call the registration function
100 if ( symbol_ptr != NULL )
104 // Register the object file for closure
105 mlt_properties_set_data( &self->parent, object_name, object, 0, ( mlt_destructor )dlclose, NULL );
112 else if ( strstr( object_name, "libmlt" ) )
114 mlt_log( NULL, MLT_LOG_WARNING, "%s: failed to dlopen %s\n (%s)\n", __FUNCTION__, object_name, dlerror() );
118 mlt_properties_close( dir );
123 /** Create a properties list for a service holding a function pointer to its constructor function.
125 * \private \memberof mlt_repository_s
126 * \param symbol a pointer to a function that can create the service.
127 * \return a properties list
130 static mlt_properties new_service( void *symbol )
132 mlt_properties properties = mlt_properties_new();
133 mlt_properties_set_data( properties, "symbol", symbol, 0, NULL, NULL );
137 /** Register a service with the repository.
139 * Typically, this is invoked by a module within its mlt_register().
141 * \public \memberof mlt_repository_s
142 * \param self a repository
143 * \param service_type a service class
144 * \param service the name of a service
145 * \param symbol a pointer to a function to create the service
148 void mlt_repository_register( mlt_repository self, mlt_service_type service_type, const char *service, mlt_register_callback symbol )
150 // Add the entry point to the corresponding service list
151 switch ( service_type )
154 mlt_properties_set_data( self->consumers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
157 mlt_properties_set_data( self->filters, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
160 mlt_properties_set_data( self->producers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
162 case transition_type:
163 mlt_properties_set_data( self->transitions, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
170 /** Get the repository properties for particular service class.
172 * \private \memberof mlt_repository_s
173 * \param self a repository
174 * \param type a service class
175 * \param service the name of a service
176 * \return a properties list or NULL if error
179 static mlt_properties get_service_properties( mlt_repository self, mlt_service_type type, const char *service )
181 mlt_properties service_properties = NULL;
183 // Get the entry point from the corresponding service list
187 service_properties = mlt_properties_get_data( self->consumers, service, NULL );
190 service_properties = mlt_properties_get_data( self->filters, service, NULL );
193 service_properties = mlt_properties_get_data( self->producers, service, NULL );
195 case transition_type:
196 service_properties = mlt_properties_get_data( self->transitions, service, NULL );
201 return service_properties;
204 /** Construct a new instance of a service.
206 * \public \memberof mlt_repository_s
207 * \param self a repository
208 * \param profile a \p mlt_profile to give the service
209 * \param type a service class
210 * \param service the name of the service
211 * \param input an optional argument to the service constructor
214 void *mlt_repository_create( mlt_repository self, mlt_profile profile, mlt_service_type type, const char *service, const void *input )
216 mlt_properties properties = get_service_properties( self, type, service );
217 if ( properties != NULL )
219 mlt_register_callback symbol_ptr = mlt_properties_get_data( properties, "symbol", NULL );
221 // Construct the service
222 return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL;
227 /** Destroy a repository and free its resources.
229 * \public \memberof mlt_repository_s
230 * \param self a repository
233 void mlt_repository_close( mlt_repository self )
235 mlt_properties_close( self->consumers );
236 mlt_properties_close( self->filters );
237 mlt_properties_close( self->producers );
238 mlt_properties_close( self->transitions );
239 mlt_properties_close( &self->parent );
243 /** Get the list of registered consumers.
245 * \public \memberof mlt_repository_s
246 * \param self a repository
247 * \return a properties list containing all of the consumers
250 mlt_properties mlt_repository_consumers( mlt_repository self )
252 return self->consumers;
255 /** Get the list of registered filters.
257 * \public \memberof mlt_repository_s
258 * \param self a repository
259 * \return a properties list of all of the filters
262 mlt_properties mlt_repository_filters( mlt_repository self )
264 return self->filters;
267 /** Get the list of registered producers.
269 * \public \memberof mlt_repository_s
270 * \param self a repository
271 * \return a properties list of all of the producers
274 mlt_properties mlt_repository_producers( mlt_repository self )
276 return self->producers;
279 /** Get the list of registered transitions.
281 * \public \memberof mlt_repository_s
282 * \param self a repository
283 * \return a properties list of all of the transitions
286 mlt_properties mlt_repository_transitions( mlt_repository self )
288 return self->transitions;
291 /** Register the metadata for a service.
293 * IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties
296 * \public \memberof mlt_repository_s
297 * \param self a repository
298 * \param type a service class
299 * \param service the name of a service
300 * \param callback the pointer to a function that can supply metadata
301 * \param callback_data an opaque user data pointer to be supplied on the callback
304 void mlt_repository_register_metadata( mlt_repository self, mlt_service_type type, const char *service, mlt_metadata_callback callback, void *callback_data )
306 mlt_properties service_properties = get_service_properties( self, type, service );
307 mlt_properties_set_data( service_properties, "metadata_cb", callback, 0, NULL, NULL );
308 mlt_properties_set_data( service_properties, "metadata_cb_data", callback_data, 0, NULL, NULL );
311 /** Get the metadata about a service.
313 * Returns NULL if service or its metadata are unavailable.
315 * \public \memberof mlt_repository_s
316 * \param self a repository
317 * \param type a service class
318 * \param service the name of a service
319 * \return the service metadata as a structured properties list
322 mlt_properties mlt_repository_metadata( mlt_repository self, mlt_service_type type, const char *service )
324 mlt_properties metadata = NULL;
325 mlt_properties properties = get_service_properties( self, type, service );
327 // If this is a valid service
330 // Lookup cached metadata
331 metadata = mlt_properties_get_data( properties, "metadata", NULL );
334 // Not cached, so get the registered metadata callback function
335 mlt_metadata_callback callback = mlt_properties_get_data( properties, "metadata_cb", NULL );
337 // If a metadata callback function is registered
340 // Fetch the callback data arg
341 void *data = mlt_properties_get_data( properties, "metadata_cb_data", NULL );
343 // Fetch the metadata through the callback
344 metadata = callback( type, service, data );
346 // Cache the metadata
348 // Include dellocation and serialisation
349 mlt_properties_set_data( properties, "metadata", metadata, 0, ( mlt_destructor )mlt_properties_close, ( mlt_serialiser )mlt_properties_serialise_yaml );
356 /** Try to determine the locale from some commonly used environment variables.
358 * \private \memberof mlt_repository_s
359 * \return a string containing the locale id or NULL if unknown
362 static char *getenv_locale()
364 char *s = getenv( "LANGUAGE" );
367 s = getenv( "LC_ALL" );
370 s = getenv( "LC_MESSAGES" );
373 s = getenv( "LANG" );
379 /** Return a list of user-preferred language codes taken from environment variables.
381 * A module should use this to locate a localized YAML Tiny file from which to build
382 * its metadata strucutured properties.
384 * \public \memberof mlt_repository_s
385 * \param self a repository
386 * \return a properties list that is a list (not a map) of locales, defaults to "en" if not
387 * overridden by environment variables, in order: LANGUAGE, LC_ALL, LC_MESSAGES, LANG
390 mlt_properties mlt_repository_languages( mlt_repository self )
392 mlt_properties languages = mlt_properties_get_data( &self->parent, "languages", NULL );
396 languages = mlt_properties_new();
397 char *locale = getenv_locale();
400 locale = strdup( locale );
401 mlt_tokeniser tokeniser = mlt_tokeniser_init();
402 int count = mlt_tokeniser_parse_new( tokeniser, locale, ":" );
406 for ( i = 0; i < count; i++ )
408 char *locale = mlt_tokeniser_get_string( tokeniser, i );
409 if ( strcmp( locale, "C" ) == 0 || strcmp( locale, "POSIX" ) == 0 )
411 else if ( strlen( locale ) > 2 )
414 snprintf( string, sizeof(string), "%d", i );
415 mlt_properties_set( languages, string, locale );
420 mlt_properties_set( languages, "0", "en" );
423 mlt_tokeniser_close( tokeniser );
427 mlt_properties_set( languages, "0", "en" );
429 mlt_properties_set_data( &self->parent, "languages", languages, 0, ( mlt_destructor )mlt_properties_close, NULL );
433 static void list_presets( mlt_properties properties, const char *path, const char *dirname )
435 DIR *dir = opendir( dirname );
439 struct dirent *de = readdir( dir );
440 char fullname[ PATH_MAX ];
444 if ( de->d_name[0] != '.' && de->d_name[strlen( de->d_name ) - 1] != '~' )
446 snprintf( fullname, sizeof(fullname), "%s/%s", dirname, de->d_name );
447 if ( de->d_type & DT_DIR )
449 // recurse into subdirectories
450 char sub[ PATH_MAX ];
452 snprintf( sub, sizeof(sub), "%s/%s", path, de->d_name );
454 strncpy( sub, de->d_name, sizeof(sub) );
455 list_presets( properties, sub, fullname );
460 mlt_properties preset = mlt_properties_load( fullname );
461 if ( preset && mlt_properties_count( preset ) )
463 snprintf( fullname, 1024, "%s/%s", path, de->d_name );
464 mlt_properties_set_data( properties, fullname, preset, 0, (mlt_destructor) mlt_properties_close, NULL );
474 /** Get the list of presets.
476 * \public \memberof mlt_repository_s
477 * \return a properties list of all the presets
480 mlt_properties mlt_repository_presets( )
482 mlt_properties result = mlt_properties_new();
483 char *path = getenv( "MLT_PRESETS_PATH" );
487 path = strdup( path );
491 path = malloc( strlen( mlt_environment( "MLT_DATA" ) ) + 9 );
492 strcpy( path, mlt_environment( "MLT_DATA" ) );
493 strcat( path, "/presets" );
495 list_presets( result, NULL, path );