1d93d559a6a4234aaffa85af40b7365fb213d952
[melted] / src / framework / mlt_repository.c
1 /*
2 * repository.c -- provides a map between service and shared objects
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Contributor: Dan Dennedy <dan@dennedy.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "mlt_repository.h"
23 #include "mlt_properties.h"
24 #include "mlt_tokeniser.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <dlfcn.h>
29 #include <string.h>
30
31 struct mlt_repository_s
32 {
33 struct mlt_properties_s parent; // a list of object files
34 mlt_properties consumers; // lists of entry points
35 mlt_properties filters;
36 mlt_properties producers;
37 mlt_properties transitions;
38 };
39
40 /** Construct a new repository
41 */
42
43 mlt_repository mlt_repository_init( const char *directory )
44 {
45 // Safety check
46 if ( directory == NULL || strcmp( directory, "" ) == 0 )
47 return NULL;
48
49 // Construct the repository
50 mlt_repository this = calloc( sizeof( struct mlt_repository_s ), 1 );
51 mlt_properties_init( &this->parent, this );
52 this->consumers = mlt_properties_new();
53 this->filters = mlt_properties_new();
54 this->producers = mlt_properties_new();
55 this->transitions = mlt_properties_new();
56
57 // Get the directory list
58 mlt_properties dir = mlt_properties_new();
59 int count = mlt_properties_dir_list( dir, directory, NULL, 0 );
60 int i;
61
62 // Iterate over files
63 for ( i = 0; i < count; i++ )
64 {
65 int flags = RTLD_NOW;
66 const char *object_name = mlt_properties_get_value( dir, i);
67
68 // Very temporary hack to allow the quicktime plugins to work
69 // TODO: extend repository to allow this to be used on a case by case basis
70 if ( strstr( object_name, "libmltkino" ) )
71 flags |= RTLD_GLOBAL;
72
73 // Open the shared object
74 void *object = dlopen( object_name, flags );
75 if ( object != NULL )
76 {
77 // Get the registration function
78 mlt_repository_callback symbol_ptr = dlsym( object, "mlt_register" );
79
80 // Call the registration function
81 if ( symbol_ptr != NULL )
82 {
83 symbol_ptr( this );
84
85 // Register the object file for closure
86 mlt_properties_set_data( &this->parent, object_name, object, 0, ( mlt_destructor )dlclose, NULL );
87 }
88 else
89 {
90 dlclose( object );
91 }
92 }
93 else if ( strstr( object_name, "libmlt" ) )
94 {
95 fprintf( stderr, "%s, %s: failed to dlopen %s\n", __FILE__, __FUNCTION__, object_name );
96 }
97 }
98
99 mlt_properties_close( dir );
100
101 return this;
102 }
103
104 static mlt_properties new_service( void *symbol )
105 {
106 mlt_properties properties = mlt_properties_new();
107 mlt_properties_set_data( properties, "symbol", symbol, 0, NULL, NULL );
108 return properties;
109 }
110
111 /** Register a service with the repository
112 Typically, this is invoked by a module within its mlt_register().
113 */
114
115 void mlt_repository_register( mlt_repository this, mlt_service_type service_type, const char *service, mlt_register_callback symbol )
116 {
117 // Add the entry point to the corresponding service list
118 switch ( service_type )
119 {
120 case consumer_type:
121 mlt_properties_set_data( this->consumers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
122 break;
123 case filter_type:
124 mlt_properties_set_data( this->filters, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
125 break;
126 case producer_type:
127 mlt_properties_set_data( this->producers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
128 break;
129 case transition_type:
130 mlt_properties_set_data( this->transitions, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
131 break;
132 default:
133 break;
134 }
135 }
136
137 static mlt_properties get_service_properties( mlt_repository this, mlt_service_type type, const char *service )
138 {
139 mlt_properties service_properties = NULL;
140
141 // Get the entry point from the corresponding service list
142 switch ( type )
143 {
144 case consumer_type:
145 service_properties = mlt_properties_get_data( this->consumers, service, NULL );
146 break;
147 case filter_type:
148 service_properties = mlt_properties_get_data( this->filters, service, NULL );
149 break;
150 case producer_type:
151 service_properties = mlt_properties_get_data( this->producers, service, NULL );
152 break;
153 case transition_type:
154 service_properties = mlt_properties_get_data( this->transitions, service, NULL );
155 break;
156 default:
157 break;
158 }
159 return service_properties;
160 }
161
162 /** Construct a new instance of a service
163 */
164
165 void *mlt_repository_create( mlt_repository this, mlt_profile profile, mlt_service_type type, const char *service, void *input )
166 {
167 mlt_properties properties = get_service_properties( this, type, service );
168 if ( properties != NULL )
169 {
170 mlt_register_callback symbol_ptr = mlt_properties_get_data( properties, "symbol", NULL );
171
172 // Construct the service
173 return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL;
174 }
175 return NULL;
176 }
177
178 /** Destroy a repository
179 */
180
181 void mlt_repository_close( mlt_repository this )
182 {
183 mlt_properties_close( this->consumers );
184 mlt_properties_close( this->filters );
185 mlt_properties_close( this->producers );
186 mlt_properties_close( this->transitions );
187 mlt_properties_close( &this->parent );
188 free( this );
189 }
190
191 /** Get the list of registered consumers
192 */
193
194 mlt_properties mlt_repository_consumers( mlt_repository self )
195 {
196 return self->consumers;
197 }
198
199 /** Get the list of registered filters
200 */
201
202 mlt_properties mlt_repository_filters( mlt_repository self )
203 {
204 return self->filters;
205 }
206
207 /** Get the list of registered producers
208 */
209
210 mlt_properties mlt_repository_producers( mlt_repository self )
211 {
212 return self->producers;
213 }
214
215 /** Get the list of registered transitions
216 */
217
218 mlt_properties mlt_repository_transitions( mlt_repository self )
219 {
220 return self->transitions;
221 }
222
223 /** Register the metadata for a service
224 IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties that you supply!
225 */
226 void mlt_repository_register_metadata( mlt_repository self, mlt_service_type type, const char *service, mlt_metadata_callback callback, void *callback_data )
227 {
228 mlt_properties service_properties = get_service_properties( self, type, service );
229 mlt_properties_set_data( service_properties, "metadata_cb", callback, 0, NULL, NULL );
230 mlt_properties_set_data( service_properties, "metadata_cb_data", callback_data, 0, NULL, NULL );
231 }
232
233 /** Get the metadata about a service
234 Returns NULL if service or its metadata are unavailable.
235 */
236
237 mlt_properties mlt_repository_metadata( mlt_repository self, mlt_service_type type, const char *service )
238 {
239 mlt_properties metadata = NULL;
240 mlt_properties properties = get_service_properties( self, type, service );
241
242 // If this is a valid service
243 if ( properties )
244 {
245 // Lookup cached metadata
246 metadata = mlt_properties_get_data( properties, "metadata", NULL );
247 if ( ! metadata )
248 {
249 // Not cached, so get the registered metadata callback function
250 mlt_metadata_callback callback = mlt_properties_get_data( properties, "metadata_cb", NULL );
251
252 // If a metadata callback function is registered
253 if ( callback )
254 {
255 // Fetch the callback data arg
256 void *data = mlt_properties_get_data( properties, "metadata_cb_data", NULL );
257
258 // Fetch the metadata through the callback
259 metadata = callback( type, service, data );
260
261 // Cache the metadata
262 if ( metadata )
263 // Include dellocation and serialisation
264 mlt_properties_set_data( properties, "metadata", metadata, 0, ( mlt_destructor )mlt_properties_close, ( mlt_serialiser )mlt_properties_serialise_yaml );
265 }
266 }
267 }
268 return metadata;
269 }
270
271 static char *getenv_locale()
272 {
273 char *s = getenv( "LANGUAGE" );
274 if ( s && s[0] )
275 return s;
276 s = getenv( "LC_ALL" );
277 if ( s && s[0] )
278 return s;
279 s = getenv( "LC_MESSAGES" );
280 if ( s && s[0] )
281 return s;
282 s = getenv( "LANG" );
283 if ( s && s[0] )
284 return s;
285 return NULL;
286 }
287
288 /** Return a list of user-preferred language codes taken from environment variables.
289 */
290
291 mlt_properties mlt_repository_languages( mlt_repository self )
292 {
293 mlt_properties languages = mlt_properties_get_data( &self->parent, "languages", NULL );
294 if ( languages )
295 return languages;
296
297 languages = mlt_properties_new();
298 char *locale = getenv_locale();
299 if ( locale )
300 {
301 locale = strdup( locale );
302 mlt_tokeniser tokeniser = mlt_tokeniser_init();
303 int count = mlt_tokeniser_parse_new( tokeniser, locale, ":" );
304 if ( count )
305 {
306 int i;
307 for ( i = 0; i < count; i++ )
308 {
309 char *locale = mlt_tokeniser_get_string( tokeniser, i );
310 if ( strcmp( locale, "C" ) == 0 || strcmp( locale, "POSIX" ) == 0 )
311 locale = "en";
312 else if ( strlen( locale ) > 2 )
313 locale[2] = 0;
314 char string[21];
315 snprintf( string, sizeof(string), "%d", i );
316 mlt_properties_set( languages, string, locale );
317 }
318 }
319 else
320 {
321 mlt_properties_set( languages, "0", "en" );
322 }
323 free( locale );
324 mlt_tokeniser_close( tokeniser );
325 }
326 else
327 {
328 mlt_properties_set( languages, "0", "en" );
329 }
330 mlt_properties_set_data( &self->parent, "languages", languages, 0, ( mlt_destructor )mlt_properties_close, NULL );
331 return languages;
332 }