]> git.sesse.net Git - mlt/blob - src/framework/mlt_repository.c
1ac8dff470b92de86d1aebc5bb49fb37686afe09
[mlt] / 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  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "mlt_repository.h"
22 #include "mlt_properties.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <dlfcn.h>
27 #include <string.h>
28
29 struct mlt_repository_s
30 {
31         struct mlt_properties_s parent; // a list of object files
32         mlt_properties consumers; // lists of entry points
33         mlt_properties filters;
34         mlt_properties producers;
35         mlt_properties transitions;
36 };
37
38 /** Construct a new repository
39 */
40
41 mlt_repository mlt_repository_init( const char *directory )
42 {
43         // Safety check
44         if ( directory == NULL || strcmp( directory, "" ) == 0 )
45                 return NULL;
46                 
47         // Construct the repository
48         mlt_repository this = calloc( sizeof( struct mlt_repository_s ), 1 );
49         mlt_properties_init( &this->parent, this );
50         this->consumers = mlt_properties_new();
51         this->filters = mlt_properties_new();
52         this->producers = mlt_properties_new();
53         this->transitions = mlt_properties_new();
54
55         // Get the directory list
56         mlt_properties dir = mlt_properties_new();
57         int count = mlt_properties_dir_list( dir, directory, NULL, 0 );
58         int i;
59         
60         // Iterate over files
61         for ( i = 0; i < count; i++ )
62         {
63                 int flags = RTLD_NOW;
64                 const char *object_name = mlt_properties_get_value( dir, i);
65
66                 // Very temporary hack to allow the quicktime plugins to work
67                 // TODO: extend repository to allow this to be used on a case by case basis
68                 if ( strstr( object_name, "libmltkino" ) )
69                         flags |= RTLD_GLOBAL;
70
71                 // Open the shared object
72                 void *object = dlopen( object_name, flags );            
73                 if ( object != NULL )
74                 {
75                         // Get the registration function
76                         int ( *symbol_ptr )( mlt_repository ) = dlsym( object, "mlt_register" );
77                         
78                         // Call the registration function
79                         if ( symbol_ptr != NULL )
80                         {
81                                 symbol_ptr( this );
82                                 
83                                 // Register the object file for closure
84                                 mlt_properties_set_data( &this->parent, object_name, object, 0, ( mlt_destructor )dlclose, NULL );
85                         }
86                         else
87                         {
88                                 dlclose( object );
89                         }
90                 }
91                 else if ( strstr( object_name, "libmlt" ) )
92                 {
93                         fprintf( stderr, "%s:%s: failed to dlopen %s\n", __FILE__, __FUNCTION__, object_name );
94                 }
95         }
96         
97         return this;
98 }
99
100 static mlt_properties new_service( void *symbol )
101 {
102         mlt_properties properties = mlt_properties_new();
103         mlt_properties_set_data( properties, "symbol", symbol, 0, NULL, NULL );
104         return properties;
105 }
106
107 /** Register a service with the repository
108     Typically, this is invoked by a module within its mlt_register().
109 */
110
111 void mlt_repository_register( mlt_repository this, mlt_service_type service_type, const char *service, void *symbol )
112 {
113         // Add the entry point to the corresponding service list
114         switch ( service_type )
115         {
116                 case consumer_type:
117                         mlt_properties_set_data( this->consumers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
118                         break;
119                 case filter_type:
120                         mlt_properties_set_data( this->filters, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
121                         break;
122                 case producer_type:
123                         mlt_properties_set_data( this->producers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
124                         break;
125                 case transition_type:
126                         mlt_properties_set_data( this->transitions, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
127                         break;
128                 default:
129                         break;
130         }
131 }
132
133 static mlt_properties get_service_properties( mlt_repository this, mlt_service_type type, const char *service )
134 {
135         mlt_properties service_properties = NULL;
136
137         // Get the entry point from the corresponding service list
138         switch ( type )
139         {
140                 case consumer_type:
141                         service_properties = mlt_properties_get_data( this->consumers, service, NULL );
142                         break;
143                 case filter_type:
144                         service_properties = mlt_properties_get_data( this->filters, service, NULL );
145                         break;
146                 case producer_type:
147                         service_properties = mlt_properties_get_data( this->producers, service, NULL );
148                         break;
149                 case transition_type:
150                         service_properties = mlt_properties_get_data( this->transitions, service, NULL );
151                         break;
152                 default:
153                         break;
154         }
155         return service_properties;
156 }
157
158 /** Construct a new instance of a service
159 */
160
161 void *mlt_repository_create( mlt_repository this, mlt_profile profile, mlt_service_type type, const char *service, void *input )
162 {
163         mlt_properties properties = get_service_properties( this, type, service );
164         if ( properties != NULL )
165         {
166                 void *( *symbol_ptr )( mlt_profile, mlt_service_type, const char *, void * ) =
167                         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_properties metadata )
224 {
225         mlt_properties service_properties = get_service_properties( self, type, service );
226         mlt_properties_set_data( service_properties, "metadata", metadata, 0, ( mlt_destructor )mlt_properties_close, NULL );
227 }
228
229 /** Get the metadata about a service
230 */
231
232 mlt_properties mlt_repository_metadata( mlt_repository self, mlt_service_type type, const char *service )
233 {
234         mlt_properties properties = get_service_properties( self, type, service );
235         return mlt_properties_get_data( properties, "metadata", NULL );
236 }