mlt_repository.[hc]: implement the metadata registration and lookup interface
[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
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <dlfcn.h>
28 #include <string.h>
29
30 struct mlt_repository_s
31 {
32 struct mlt_properties_s parent; // a list of object files
33 mlt_properties consumers; // lists of entry points
34 mlt_properties filters;
35 mlt_properties producers;
36 mlt_properties transitions;
37 };
38
39 /** Construct a new repository
40 */
41
42 mlt_repository mlt_repository_init( const char *directory )
43 {
44 // Safety check
45 if ( directory == NULL || strcmp( directory, "" ) == 0 )
46 return NULL;
47
48 // Construct the repository
49 mlt_repository this = calloc( sizeof( struct mlt_repository_s ), 1 );
50 mlt_properties_init( &this->parent, this );
51 this->consumers = mlt_properties_new();
52 this->filters = mlt_properties_new();
53 this->producers = mlt_properties_new();
54 this->transitions = mlt_properties_new();
55
56 // Get the directory list
57 mlt_properties dir = mlt_properties_new();
58 int count = mlt_properties_dir_list( dir, directory, NULL, 0 );
59 int i;
60
61 // Iterate over files
62 for ( i = 0; i < count; i++ )
63 {
64 int flags = RTLD_NOW;
65 const char *object_name = mlt_properties_get_value( dir, i);
66
67 // Very temporary hack to allow the quicktime plugins to work
68 // TODO: extend repository to allow this to be used on a case by case basis
69 if ( strstr( object_name, "libmltkino" ) )
70 flags |= RTLD_GLOBAL;
71
72 // Open the shared object
73 void *object = dlopen( object_name, flags );
74 if ( object != NULL )
75 {
76 // Get the registration function
77 mlt_repository_callback symbol_ptr = dlsym( object, "mlt_register" );
78
79 // Call the registration function
80 if ( symbol_ptr != NULL )
81 {
82 symbol_ptr( this );
83
84 // Register the object file for closure
85 mlt_properties_set_data( &this->parent, object_name, object, 0, ( mlt_destructor )dlclose, NULL );
86 }
87 else
88 {
89 dlclose( object );
90 }
91 }
92 else if ( strstr( object_name, "libmlt" ) )
93 {
94 fprintf( stderr, "%s, %s: failed to dlopen %s\n", __FILE__, __FUNCTION__, object_name );
95 }
96 }
97
98 return this;
99 }
100
101 static mlt_properties new_service( void *symbol )
102 {
103 mlt_properties properties = mlt_properties_new();
104 mlt_properties_set_data( properties, "symbol", symbol, 0, NULL, NULL );
105 return properties;
106 }
107
108 /** Register a service with the repository
109 Typically, this is invoked by a module within its mlt_register().
110 */
111
112 void mlt_repository_register( mlt_repository this, mlt_service_type service_type, const char *service, mlt_register_callback symbol )
113 {
114 // Add the entry point to the corresponding service list
115 switch ( service_type )
116 {
117 case consumer_type:
118 mlt_properties_set_data( this->consumers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
119 break;
120 case filter_type:
121 mlt_properties_set_data( this->filters, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
122 break;
123 case producer_type:
124 mlt_properties_set_data( this->producers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
125 break;
126 case transition_type:
127 mlt_properties_set_data( this->transitions, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
128 break;
129 default:
130 break;
131 }
132 }
133
134 static mlt_properties get_service_properties( mlt_repository this, mlt_service_type type, const char *service )
135 {
136 mlt_properties service_properties = NULL;
137
138 // Get the entry point from the corresponding service list
139 switch ( type )
140 {
141 case consumer_type:
142 service_properties = mlt_properties_get_data( this->consumers, service, NULL );
143 break;
144 case filter_type:
145 service_properties = mlt_properties_get_data( this->filters, service, NULL );
146 break;
147 case producer_type:
148 service_properties = mlt_properties_get_data( this->producers, service, NULL );
149 break;
150 case transition_type:
151 service_properties = mlt_properties_get_data( this->transitions, service, NULL );
152 break;
153 default:
154 break;
155 }
156 return service_properties;
157 }
158
159 /** Construct a new instance of a service
160 */
161
162 void *mlt_repository_create( mlt_repository this, mlt_profile profile, mlt_service_type type, const char *service, void *input )
163 {
164 mlt_properties properties = get_service_properties( this, type, service );
165 if ( properties != NULL )
166 {
167 mlt_register_callback symbol_ptr = mlt_properties_get_data( properties, "symbol", NULL );
168
169 // Construct the service
170 return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL;
171 }
172 return NULL;
173 }
174
175 /** Destroy a repository
176 */
177
178 void mlt_repository_close( mlt_repository this )
179 {
180 mlt_properties_close( this->consumers );
181 mlt_properties_close( this->filters );
182 mlt_properties_close( this->producers );
183 mlt_properties_close( this->transitions );
184 mlt_properties_close( &this->parent );
185 free( this );
186 }
187
188 /** Get the list of registered consumers
189 */
190
191 mlt_properties mlt_repository_consumers( mlt_repository self )
192 {
193 return self->consumers;
194 }
195
196 /** Get the list of registered filters
197 */
198
199 mlt_properties mlt_repository_filters( mlt_repository self )
200 {
201 return self->filters;
202 }
203
204 /** Get the list of registered producers
205 */
206
207 mlt_properties mlt_repository_producers( mlt_repository self )
208 {
209 return self->producers;
210 }
211
212 /** Get the list of registered transitions
213 */
214
215 mlt_properties mlt_repository_transitions( mlt_repository self )
216 {
217 return self->transitions;
218 }
219
220 /** Register the metadata for a service
221 IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties that you supply!
222 */
223 void mlt_repository_register_metadata( mlt_repository self, mlt_service_type type, const char *service, mlt_metadata_callback callback, void *callback_data )
224 {
225 mlt_properties service_properties = get_service_properties( self, type, service );
226 mlt_properties_set_data( service_properties, "metadata_cb", callback, 0, NULL, NULL );
227 mlt_properties_set_data( service_properties, "metadata_cb_data", callback_data, 0, NULL, NULL );
228 }
229
230 /** Get the metadata about a service
231 Returns NULL if service or its metadata are unavailable.
232 */
233
234 mlt_properties mlt_repository_metadata( mlt_repository self, mlt_service_type type, const char *service )
235 {
236 mlt_properties metadata = NULL;
237 mlt_properties properties = get_service_properties( self, type, service );
238
239 // If this is a valid service
240 if ( properties )
241 {
242 // Lookup cached metadata
243 metadata = mlt_properties_get_data( properties, "metadata", NULL );
244 if ( ! metadata )
245 {
246 // Not cached, so get the registered metadata callback function
247 mlt_metadata_callback callback = mlt_properties_get_data( properties, "metadata_cb", NULL );
248
249 // If a metadata callback function is registered
250 if ( callback )
251 {
252 // Fetch the callback data arg
253 void *data = mlt_properties_get_data( properties, "metadata_cb_data", NULL );
254
255 // Fetch the metadata through the callback
256 metadata = callback( type, service, data );
257
258 // Cache the metadata
259 if ( metadata )
260 // Include dellocation and serialisation
261 mlt_properties_set_data( properties, "metadata", metadata, 0, ( mlt_destructor )mlt_properties_close, ( mlt_serialiser )mlt_properties_serialise_yaml );
262 }
263 }
264 }
265 return metadata;
266 }