]> git.sesse.net Git - mlt/blob - src/framework/mlt_repository.c
mlt_repository.c: report reason when dlopen fails.
[mlt] / src / framework / mlt_repository.c
1 /**
2  * \file mlt_repository.c
3  * \brief provides a map between service and shared objects
4  *
5  * Copyright (C) 2003-2008 Ushodaya Enterprises Limited
6  * \author Charles Yates <charles.yates@pandora.be>
7  *         Dan Dennedy <dan@dennedy.org>
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "mlt_repository.h"
25 #include "mlt_properties.h"
26 #include "mlt_tokeniser.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <dlfcn.h>
31 #include <string.h>
32
33 /** \brief Repository class
34  *
35  * \extends mlt_properties_s
36  */
37
38 struct mlt_repository_s
39 {
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;
45 };
46
47 /** Construct a new repository
48 */
49
50 mlt_repository mlt_repository_init( const char *directory )
51 {
52         // Safety check
53         if ( directory == NULL || strcmp( directory, "" ) == 0 )
54                 return NULL;
55
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();
63
64         // Get the directory list
65         mlt_properties dir = mlt_properties_new();
66         int count = mlt_properties_dir_list( dir, directory, NULL, 0 );
67         int i;
68
69         // Iterate over files
70         for ( i = 0; i < count; i++ )
71         {
72                 int flags = RTLD_NOW;
73                 const char *object_name = mlt_properties_get_value( dir, i);
74
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" ) )
78                         flags |= RTLD_GLOBAL;
79
80                 // Open the shared object
81                 void *object = dlopen( object_name, flags );
82                 if ( object != NULL )
83                 {
84                         // Get the registration function
85                         mlt_repository_callback symbol_ptr = dlsym( object, "mlt_register" );
86
87                         // Call the registration function
88                         if ( symbol_ptr != NULL )
89                         {
90                                 symbol_ptr( this );
91
92                                 // Register the object file for closure
93                                 mlt_properties_set_data( &this->parent, object_name, object, 0, ( mlt_destructor )dlclose, NULL );
94                         }
95                         else
96                         {
97                                 dlclose( object );
98                         }
99                 }
100                 else if ( strstr( object_name, "libmlt" ) )
101                 {
102                         fprintf( stderr, "%s, %s: failed to dlopen %s\n  (%s)\n", __FILE__, __FUNCTION__, object_name, dlerror() );
103                 }
104         }
105
106         mlt_properties_close( dir );
107
108         return this;
109 }
110
111 static mlt_properties new_service( void *symbol )
112 {
113         mlt_properties properties = mlt_properties_new();
114         mlt_properties_set_data( properties, "symbol", symbol, 0, NULL, NULL );
115         return properties;
116 }
117
118 /** Register a service with the repository
119     Typically, this is invoked by a module within its mlt_register().
120 */
121
122 void mlt_repository_register( mlt_repository this, mlt_service_type service_type, const char *service, mlt_register_callback symbol )
123 {
124         // Add the entry point to the corresponding service list
125         switch ( service_type )
126         {
127                 case consumer_type:
128                         mlt_properties_set_data( this->consumers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
129                         break;
130                 case filter_type:
131                         mlt_properties_set_data( this->filters, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
132                         break;
133                 case producer_type:
134                         mlt_properties_set_data( this->producers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
135                         break;
136                 case transition_type:
137                         mlt_properties_set_data( this->transitions, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
138                         break;
139                 default:
140                         break;
141         }
142 }
143
144 static mlt_properties get_service_properties( mlt_repository this, mlt_service_type type, const char *service )
145 {
146         mlt_properties service_properties = NULL;
147
148         // Get the entry point from the corresponding service list
149         switch ( type )
150         {
151                 case consumer_type:
152                         service_properties = mlt_properties_get_data( this->consumers, service, NULL );
153                         break;
154                 case filter_type:
155                         service_properties = mlt_properties_get_data( this->filters, service, NULL );
156                         break;
157                 case producer_type:
158                         service_properties = mlt_properties_get_data( this->producers, service, NULL );
159                         break;
160                 case transition_type:
161                         service_properties = mlt_properties_get_data( this->transitions, service, NULL );
162                         break;
163                 default:
164                         break;
165         }
166         return service_properties;
167 }
168
169 /** Construct a new instance of a service
170 */
171
172 void *mlt_repository_create( mlt_repository this, mlt_profile profile, mlt_service_type type, const char *service, void *input )
173 {
174         mlt_properties properties = get_service_properties( this, type, service );
175         if ( properties != NULL )
176         {
177                 mlt_register_callback symbol_ptr = mlt_properties_get_data( properties, "symbol", NULL );
178
179                 // Construct the service
180                 return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL;
181         }
182         return NULL;
183 }
184
185 /** Destroy a repository
186 */
187
188 void mlt_repository_close( mlt_repository this )
189 {
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 );
195         free( this );
196 }
197
198 /** Get the list of registered consumers
199 */
200
201 mlt_properties mlt_repository_consumers( mlt_repository self )
202 {
203         return self->consumers;
204 }
205
206 /** Get the list of registered filters
207 */
208
209 mlt_properties mlt_repository_filters( mlt_repository self )
210 {
211         return self->filters;
212 }
213
214 /** Get the list of registered producers
215 */
216
217 mlt_properties mlt_repository_producers( mlt_repository self )
218 {
219         return self->producers;
220 }
221
222 /** Get the list of registered transitions
223 */
224
225 mlt_properties mlt_repository_transitions( mlt_repository self )
226 {
227         return self->transitions;
228 }
229
230 /** Register the metadata for a service
231     IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties that you supply!
232 */
233 void mlt_repository_register_metadata( mlt_repository self, mlt_service_type type, const char *service, mlt_metadata_callback callback, void *callback_data )
234 {
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 );
238 }
239
240 /** Get the metadata about a service
241     Returns NULL if service or its metadata are unavailable.
242 */
243
244 mlt_properties mlt_repository_metadata( mlt_repository self, mlt_service_type type, const char *service )
245 {
246         mlt_properties metadata = NULL;
247         mlt_properties properties = get_service_properties( self, type, service );
248
249         // If this is a valid service
250         if ( properties )
251         {
252                 // Lookup cached metadata
253                 metadata = mlt_properties_get_data( properties, "metadata", NULL );
254                 if ( ! metadata )
255                 {
256                         // Not cached, so get the registered metadata callback function
257                         mlt_metadata_callback callback = mlt_properties_get_data( properties, "metadata_cb", NULL );
258
259                         // If a metadata callback function is registered
260                         if ( callback )
261                         {
262                                 // Fetch the callback data arg
263                                 void *data = mlt_properties_get_data( properties, "metadata_cb_data", NULL );
264
265                                 // Fetch the metadata through the callback
266                                 metadata = callback( type, service, data );
267
268                                 // Cache the metadata
269                                 if ( 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 );
272                         }
273                 }
274         }
275         return metadata;
276 }
277
278 static char *getenv_locale()
279 {
280         char *s = getenv( "LANGUAGE" );
281         if ( s && s[0] )
282                 return s;
283         s = getenv( "LC_ALL" );
284         if ( s && s[0] )
285                 return s;
286         s = getenv( "LC_MESSAGES" );
287         if ( s && s[0] )
288                 return s;
289         s = getenv( "LANG" );
290         if ( s && s[0] )
291                 return s;
292         return NULL;
293 }
294
295 /** Return a list of user-preferred language codes taken from environment variables.
296 */
297
298 mlt_properties mlt_repository_languages( mlt_repository self )
299 {
300         mlt_properties languages = mlt_properties_get_data( &self->parent, "languages", NULL );
301         if ( languages )
302                 return languages;
303
304         languages = mlt_properties_new();
305         char *locale = getenv_locale();
306         if ( locale )
307         {
308                 locale = strdup( locale );
309                 mlt_tokeniser tokeniser = mlt_tokeniser_init();
310                 int count = mlt_tokeniser_parse_new( tokeniser, locale, ":" );
311                 if ( count )
312                 {
313                         int i;
314                         for ( i = 0; i < count; i++ )
315                         {
316                                 char *locale = mlt_tokeniser_get_string( tokeniser, i );
317                                 if ( strcmp( locale, "C" ) == 0 || strcmp( locale, "POSIX" ) == 0 )
318                                         locale = "en";
319                                 else if ( strlen( locale ) > 2 )
320                                         locale[2] = 0;
321                                 char string[21];
322                                 snprintf( string, sizeof(string), "%d", i );
323                                 mlt_properties_set( languages, string, locale );
324                         }
325                 }
326                 else
327                 {
328                         mlt_properties_set( languages, "0", "en" );
329                 }
330                 free( locale );
331                 mlt_tokeniser_close( tokeniser );
332         }
333         else
334         {
335                 mlt_properties_set( languages, "0", "en" );
336         }
337         mlt_properties_set_data( &self->parent, "languages", languages, 0, ( mlt_destructor )mlt_properties_close, NULL );
338         return languages;
339 }