2 * \file mlt_repository.c
3 * \brief provides a map between service and shared objects
5 * Copyright (C) 2003-2008 Ushodaya Enterprises Limited
6 * \author Charles Yates <charles.yates@pandora.be>
7 * Dan Dennedy <dan@dennedy.org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library 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 GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "mlt_repository.h"
25 #include "mlt_properties.h"
26 #include "mlt_tokeniser.h"
33 /** \brief Repository class
35 * \extends mlt_properties_s
38 struct mlt_repository_s
40 struct mlt_properties_s parent; // a list of object files
41 mlt_properties consumers; // lists of entry points
42 mlt_properties filters;
43 mlt_properties producers;
44 mlt_properties transitions;
47 /** Construct a new repository
50 mlt_repository mlt_repository_init( const char *directory )
53 if ( directory == NULL || strcmp( directory, "" ) == 0 )
56 // Construct the repository
57 mlt_repository this = calloc( sizeof( struct mlt_repository_s ), 1 );
58 mlt_properties_init( &this->parent, this );
59 this->consumers = mlt_properties_new();
60 this->filters = mlt_properties_new();
61 this->producers = mlt_properties_new();
62 this->transitions = mlt_properties_new();
64 // Get the directory list
65 mlt_properties dir = mlt_properties_new();
66 int count = mlt_properties_dir_list( dir, directory, NULL, 0 );
70 for ( i = 0; i < count; i++ )
73 const char *object_name = mlt_properties_get_value( dir, i);
75 // Very temporary hack to allow the quicktime plugins to work
76 // TODO: extend repository to allow this to be used on a case by case basis
77 if ( strstr( object_name, "libmltkino" ) )
80 // Open the shared object
81 void *object = dlopen( object_name, flags );
84 // Get the registration function
85 mlt_repository_callback symbol_ptr = dlsym( object, "mlt_register" );
87 // Call the registration function
88 if ( symbol_ptr != NULL )
92 // Register the object file for closure
93 mlt_properties_set_data( &this->parent, object_name, object, 0, ( mlt_destructor )dlclose, NULL );
100 else if ( strstr( object_name, "libmlt" ) )
102 fprintf( stderr, "%s, %s: failed to dlopen %s\n (%s)\n", __FILE__, __FUNCTION__, object_name, dlerror() );
106 mlt_properties_close( dir );
111 static mlt_properties new_service( void *symbol )
113 mlt_properties properties = mlt_properties_new();
114 mlt_properties_set_data( properties, "symbol", symbol, 0, NULL, NULL );
118 /** Register a service with the repository
119 Typically, this is invoked by a module within its mlt_register().
122 void mlt_repository_register( mlt_repository this, mlt_service_type service_type, const char *service, mlt_register_callback symbol )
124 // Add the entry point to the corresponding service list
125 switch ( service_type )
128 mlt_properties_set_data( this->consumers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
131 mlt_properties_set_data( this->filters, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
134 mlt_properties_set_data( this->producers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
136 case transition_type:
137 mlt_properties_set_data( this->transitions, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
144 static mlt_properties get_service_properties( mlt_repository this, mlt_service_type type, const char *service )
146 mlt_properties service_properties = NULL;
148 // Get the entry point from the corresponding service list
152 service_properties = mlt_properties_get_data( this->consumers, service, NULL );
155 service_properties = mlt_properties_get_data( this->filters, service, NULL );
158 service_properties = mlt_properties_get_data( this->producers, service, NULL );
160 case transition_type:
161 service_properties = mlt_properties_get_data( this->transitions, service, NULL );
166 return service_properties;
169 /** Construct a new instance of a service
172 void *mlt_repository_create( mlt_repository this, mlt_profile profile, mlt_service_type type, const char *service, void *input )
174 mlt_properties properties = get_service_properties( this, type, service );
175 if ( properties != NULL )
177 mlt_register_callback symbol_ptr = mlt_properties_get_data( properties, "symbol", NULL );
179 // Construct the service
180 return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL;
185 /** Destroy a repository
188 void mlt_repository_close( mlt_repository this )
190 mlt_properties_close( this->consumers );
191 mlt_properties_close( this->filters );
192 mlt_properties_close( this->producers );
193 mlt_properties_close( this->transitions );
194 mlt_properties_close( &this->parent );
198 /** Get the list of registered consumers
201 mlt_properties mlt_repository_consumers( mlt_repository self )
203 return self->consumers;
206 /** Get the list of registered filters
209 mlt_properties mlt_repository_filters( mlt_repository self )
211 return self->filters;
214 /** Get the list of registered producers
217 mlt_properties mlt_repository_producers( mlt_repository self )
219 return self->producers;
222 /** Get the list of registered transitions
225 mlt_properties mlt_repository_transitions( mlt_repository self )
227 return self->transitions;
230 /** Register the metadata for a service
231 IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties that you supply!
233 void mlt_repository_register_metadata( mlt_repository self, mlt_service_type type, const char *service, mlt_metadata_callback callback, void *callback_data )
235 mlt_properties service_properties = get_service_properties( self, type, service );
236 mlt_properties_set_data( service_properties, "metadata_cb", callback, 0, NULL, NULL );
237 mlt_properties_set_data( service_properties, "metadata_cb_data", callback_data, 0, NULL, NULL );
240 /** Get the metadata about a service
241 Returns NULL if service or its metadata are unavailable.
244 mlt_properties mlt_repository_metadata( mlt_repository self, mlt_service_type type, const char *service )
246 mlt_properties metadata = NULL;
247 mlt_properties properties = get_service_properties( self, type, service );
249 // If this is a valid service
252 // Lookup cached metadata
253 metadata = mlt_properties_get_data( properties, "metadata", NULL );
256 // Not cached, so get the registered metadata callback function
257 mlt_metadata_callback callback = mlt_properties_get_data( properties, "metadata_cb", NULL );
259 // If a metadata callback function is registered
262 // Fetch the callback data arg
263 void *data = mlt_properties_get_data( properties, "metadata_cb_data", NULL );
265 // Fetch the metadata through the callback
266 metadata = callback( type, service, data );
268 // Cache the metadata
270 // Include dellocation and serialisation
271 mlt_properties_set_data( properties, "metadata", metadata, 0, ( mlt_destructor )mlt_properties_close, ( mlt_serialiser )mlt_properties_serialise_yaml );
278 static char *getenv_locale()
280 char *s = getenv( "LANGUAGE" );
283 s = getenv( "LC_ALL" );
286 s = getenv( "LC_MESSAGES" );
289 s = getenv( "LANG" );
295 /** Return a list of user-preferred language codes taken from environment variables.
298 mlt_properties mlt_repository_languages( mlt_repository self )
300 mlt_properties languages = mlt_properties_get_data( &self->parent, "languages", NULL );
304 languages = mlt_properties_new();
305 char *locale = getenv_locale();
308 locale = strdup( locale );
309 mlt_tokeniser tokeniser = mlt_tokeniser_init();
310 int count = mlt_tokeniser_parse_new( tokeniser, locale, ":" );
314 for ( i = 0; i < count; i++ )
316 char *locale = mlt_tokeniser_get_string( tokeniser, i );
317 if ( strcmp( locale, "C" ) == 0 || strcmp( locale, "POSIX" ) == 0 )
319 else if ( strlen( locale ) > 2 )
322 snprintf( string, sizeof(string), "%d", i );
323 mlt_properties_set( languages, string, locale );
328 mlt_properties_set( languages, "0", "en" );
331 mlt_tokeniser_close( tokeniser );
335 mlt_properties_set( languages, "0", "en" );
337 mlt_properties_set_data( &self->parent, "languages", languages, 0, ( mlt_destructor )mlt_properties_close, NULL );