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"
35 /** \brief Repository class
37 * The Repository is a collection of plugin modules and their services and service metadata.
39 * \extends mlt_properties_s
40 * \properties \p language a cached list of user locales
43 struct mlt_repository_s
45 struct mlt_properties_s parent
; /// a list of object files
46 mlt_properties consumers
; /// a list of entry points for consumers
47 mlt_properties filters
; /// a list of entry points for filters
48 mlt_properties producers
; /// a list of entry points for producers
49 mlt_properties transitions
; /// a list of entry points for transitions
52 /** Construct a new repository.
54 * \public \memberof mlt_repository_s
55 * \param directory the full path of a directory from which to read modules
56 * \return a new repository or NULL if failed
59 mlt_repository
mlt_repository_init( const char *directory
)
62 if ( directory
== NULL
|| strcmp( directory
, "" ) == 0 )
65 // Construct the repository
66 mlt_repository
this = calloc( sizeof( struct mlt_repository_s
), 1 );
67 mlt_properties_init( &this->parent
, this );
68 this->consumers
= mlt_properties_new();
69 this->filters
= mlt_properties_new();
70 this->producers
= mlt_properties_new();
71 this->transitions
= mlt_properties_new();
73 // Get the directory list
74 mlt_properties dir
= mlt_properties_new();
75 int count
= mlt_properties_dir_list( dir
, directory
, NULL
, 0 );
79 for ( i
= 0; i
< count
; i
++ )
82 const char *object_name
= mlt_properties_get_value( dir
, i
);
84 // Very temporary hack to allow the quicktime plugins to work
85 // TODO: extend repository to allow this to be used on a case by case basis
86 if ( strstr( object_name
, "libmltkino" ) )
89 // Open the shared object
90 void *object
= dlopen( object_name
, flags
);
93 // Get the registration function
94 mlt_repository_callback symbol_ptr
= dlsym( object
, "mlt_register" );
96 // Call the registration function
97 if ( symbol_ptr
!= NULL
)
101 // Register the object file for closure
102 mlt_properties_set_data( &this->parent
, object_name
, object
, 0, ( mlt_destructor
)dlclose
, NULL
);
109 else if ( strstr( object_name
, "libmlt" ) )
111 mlt_log( NULL
, MLT_LOG_WARNING
, "%s: failed to dlopen %s\n (%s)\n", __FUNCTION__
, object_name
, dlerror() );
115 mlt_properties_close( dir
);
120 /** Create a properties list for a service holding a function pointer to its constructor function.
122 * \private \memberof mlt_repository_s
123 * \param symbol a pointer to a function that can create the service.
124 * \return a properties list
127 static mlt_properties
new_service( void *symbol
)
129 mlt_properties properties
= mlt_properties_new();
130 mlt_properties_set_data( properties
, "symbol", symbol
, 0, NULL
, NULL
);
134 /** Register a service with the repository.
136 * Typically, this is invoked by a module within its mlt_register().
138 * \public \memberof mlt_repository_s
139 * \param this a repository
140 * \param service_type a service class
141 * \param service the name of a service
142 * \param symbol a pointer to a function to create the service
145 void mlt_repository_register( mlt_repository
this, mlt_service_type service_type
, const char *service
, mlt_register_callback symbol
)
147 // Add the entry point to the corresponding service list
148 switch ( service_type
)
151 mlt_properties_set_data( this->consumers
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
154 mlt_properties_set_data( this->filters
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
157 mlt_properties_set_data( this->producers
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
159 case transition_type
:
160 mlt_properties_set_data( this->transitions
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
167 /** Get the repository properties for particular service class.
169 * \private \memberof mlt_repository_s
170 * \param this a repository
171 * \param type a service class
172 * \param service the name of a service
173 * \return a properties list or NULL if error
176 static mlt_properties
get_service_properties( mlt_repository
this, mlt_service_type type
, const char *service
)
178 mlt_properties service_properties
= NULL
;
180 // Get the entry point from the corresponding service list
184 service_properties
= mlt_properties_get_data( this->consumers
, service
, NULL
);
187 service_properties
= mlt_properties_get_data( this->filters
, service
, NULL
);
190 service_properties
= mlt_properties_get_data( this->producers
, service
, NULL
);
192 case transition_type
:
193 service_properties
= mlt_properties_get_data( this->transitions
, service
, NULL
);
198 return service_properties
;
201 /** Construct a new instance of a service.
203 * \public \memberof mlt_repository_s
204 * \param this a repository
205 * \param profile a \p mlt_profile to give the service
206 * \param type a service class
207 * \param service the name of the service
208 * \param input an optional argument to the service constructor
211 void *mlt_repository_create( mlt_repository
this, mlt_profile profile
, mlt_service_type type
, const char *service
, const void *input
)
213 mlt_properties properties
= get_service_properties( this, type
, service
);
214 if ( properties
!= NULL
)
216 mlt_register_callback symbol_ptr
= mlt_properties_get_data( properties
, "symbol", NULL
);
218 // Construct the service
219 return ( symbol_ptr
!= NULL
) ?
symbol_ptr( profile
, type
, service
, input
) : NULL
;
224 /** Destroy a repository and free its resources.
226 * \public \memberof mlt_repository_s
227 * \param this a repository
230 void mlt_repository_close( mlt_repository
this )
232 mlt_properties_close( this->consumers
);
233 mlt_properties_close( this->filters
);
234 mlt_properties_close( this->producers
);
235 mlt_properties_close( this->transitions
);
236 mlt_properties_close( &this->parent
);
240 /** Get the list of registered consumers.
242 * \public \memberof mlt_repository_s
243 * \param self a repository
244 * \return a properties list containing all of the consumers
247 mlt_properties
mlt_repository_consumers( mlt_repository self
)
249 return self
->consumers
;
252 /** Get the list of registered filters.
254 * \public \memberof mlt_repository_s
255 * \param self a repository
256 * \return a properties list of all of the filters
259 mlt_properties
mlt_repository_filters( mlt_repository self
)
261 return self
->filters
;
264 /** Get the list of registered producers.
266 * \public \memberof mlt_repository_s
267 * \param self a repository
268 * \return a properties list of all of the producers
271 mlt_properties
mlt_repository_producers( mlt_repository self
)
273 return self
->producers
;
276 /** Get the list of registered transitions.
278 * \public \memberof mlt_repository_s
279 * \param self a repository
280 * \return a properties list of all of the transitions
283 mlt_properties
mlt_repository_transitions( mlt_repository self
)
285 return self
->transitions
;
288 /** Register the metadata for a service.
290 * IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties
293 * \public \memberof mlt_repository_s
294 * \param self a repository
295 * \param type a service class
296 * \param service the name of a service
297 * \param callback the pointer to a function that can supply metadata
298 * \param callback_data an opaque user data pointer to be supplied on the callback
301 void mlt_repository_register_metadata( mlt_repository self
, mlt_service_type type
, const char *service
, mlt_metadata_callback callback
, void *callback_data
)
303 mlt_properties service_properties
= get_service_properties( self
, type
, service
);
304 mlt_properties_set_data( service_properties
, "metadata_cb", callback
, 0, NULL
, NULL
);
305 mlt_properties_set_data( service_properties
, "metadata_cb_data", callback_data
, 0, NULL
, NULL
);
308 /** Get the metadata about a service.
310 * Returns NULL if service or its metadata are unavailable.
312 * \public \memberof mlt_repository_s
313 * \param self a repository
314 * \param type a service class
315 * \param service the name of a service
316 * \return the service metadata as a structured properties list
319 mlt_properties
mlt_repository_metadata( mlt_repository self
, mlt_service_type type
, const char *service
)
321 mlt_properties metadata
= NULL
;
322 mlt_properties properties
= get_service_properties( self
, type
, service
);
324 // If this is a valid service
327 // Lookup cached metadata
328 metadata
= mlt_properties_get_data( properties
, "metadata", NULL
);
331 // Not cached, so get the registered metadata callback function
332 mlt_metadata_callback callback
= mlt_properties_get_data( properties
, "metadata_cb", NULL
);
334 // If a metadata callback function is registered
337 // Fetch the callback data arg
338 void *data
= mlt_properties_get_data( properties
, "metadata_cb_data", NULL
);
340 // Fetch the metadata through the callback
341 metadata
= callback( type
, service
, data
);
343 // Cache the metadata
345 // Include dellocation and serialisation
346 mlt_properties_set_data( properties
, "metadata", metadata
, 0, ( mlt_destructor
)mlt_properties_close
, ( mlt_serialiser
)mlt_properties_serialise_yaml
);
353 /** Try to determine the locale from some commonly used environment variables.
355 * \private \memberof mlt_repository_s
356 * \return a string containing the locale id or NULL if unknown
359 static char *getenv_locale()
361 char *s
= getenv( "LANGUAGE" );
364 s
= getenv( "LC_ALL" );
367 s
= getenv( "LC_MESSAGES" );
370 s
= getenv( "LANG" );
376 /** Return a list of user-preferred language codes taken from environment variables.
378 * A module should use this to locate a localized YAML Tiny file from which to build
379 * its metadata strucutured properties.
381 * \public \memberof mlt_repository_s
382 * \param self a repository
383 * \return a properties list that is a list (not a map) of locales, defaults to "en" if not
384 * overridden by environment variables, in order: LANGUAGE, LC_ALL, LC_MESSAGES, LANG
387 mlt_properties
mlt_repository_languages( mlt_repository self
)
389 mlt_properties languages
= mlt_properties_get_data( &self
->parent
, "languages", NULL
);
393 languages
= mlt_properties_new();
394 char *locale
= getenv_locale();
397 locale
= strdup( locale
);
398 mlt_tokeniser tokeniser
= mlt_tokeniser_init();
399 int count
= mlt_tokeniser_parse_new( tokeniser
, locale
, ":" );
403 for ( i
= 0; i
< count
; i
++ )
405 char *locale
= mlt_tokeniser_get_string( tokeniser
, i
);
406 if ( strcmp( locale
, "C" ) == 0 || strcmp( locale
, "POSIX" ) == 0 )
408 else if ( strlen( locale
) > 2 )
411 snprintf( string
, sizeof(string
), "%d", i
);
412 mlt_properties_set( languages
, string
, locale
);
417 mlt_properties_set( languages
, "0", "en" );
420 mlt_tokeniser_close( tokeniser
);
424 mlt_properties_set( languages
, "0", "en" );
426 mlt_properties_set_data( &self
->parent
, "languages", languages
, 0, ( mlt_destructor
)mlt_properties_close
, NULL
);