]> git.sesse.net Git - mlt/blob - src/framework/mlt_repository.c
mlt_consumer.[hc]: added new functions mlt_repository_consumers, mlt_repository_filters,
[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         }
92         
93         return this;
94 }
95
96 static mlt_properties new_service( void *symbol )
97 {
98         mlt_properties properties = mlt_properties_new();
99         mlt_properties_set_data( properties, "symbol", symbol, 0, NULL, NULL );
100         return properties;
101 }
102
103 /** Register a service with the repository
104     Typically, this is invoked by a module within its mlt_register().
105 */
106
107 void mlt_repository_register( mlt_repository this, mlt_service_type service_type, const char *service, void *symbol )
108 {
109         // Add the entry point to the corresponding service list
110         switch ( service_type )
111         {
112                 case consumer_type:
113                         mlt_properties_set_data( this->consumers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
114                         break;
115                 case filter_type:
116                         mlt_properties_set_data( this->filters, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
117                         break;
118                 case producer_type:
119                         mlt_properties_set_data( this->producers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
120                         break;
121                 case transition_type:
122                         mlt_properties_set_data( this->transitions, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
123                         break;
124                 default:
125                         break;
126         }
127 }
128
129 static mlt_properties get_service_properties( mlt_repository this, mlt_service_type type, const char *service )
130 {
131         mlt_properties service_properties = NULL;
132
133         // Get the entry point from the corresponding service list
134         switch ( type )
135         {
136                 case consumer_type:
137                         service_properties = mlt_properties_get_data( this->consumers, service, NULL );
138                         break;
139                 case filter_type:
140                         service_properties = mlt_properties_get_data( this->filters, service, NULL );
141                         break;
142                 case producer_type:
143                         service_properties = mlt_properties_get_data( this->producers, service, NULL );
144                         break;
145                 case transition_type:
146                         service_properties = mlt_properties_get_data( this->transitions, service, NULL );
147                         break;
148                 default:
149                         break;
150         }
151         return service_properties;
152 }
153
154 /** Construct a new instance of a service
155 */
156
157 void *mlt_repository_create( mlt_repository this, mlt_profile profile, mlt_service_type type, const char *service, void *input )
158 {
159         mlt_properties properties = get_service_properties( this, type, service );
160         if ( properties != NULL )
161         {
162                 void *( *symbol_ptr )( mlt_profile, mlt_service_type, const char *, void * ) =
163                         mlt_properties_get_data( properties, "symbol", NULL );
164         
165                 // Construct the service
166                 return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL;
167         }
168         return NULL;
169 }
170
171 /** Destroy a repository
172 */
173
174 void mlt_repository_close( mlt_repository this )
175 {
176         mlt_properties_close( this->consumers );
177         mlt_properties_close( this->filters );
178         mlt_properties_close( this->producers );
179         mlt_properties_close( this->transitions );
180         mlt_properties_close( &this->parent );
181         free( this );
182 }
183
184 /** Get the list of registered consumers
185 */
186
187 mlt_properties mlt_repository_consumers( mlt_repository self )
188 {
189         return self->consumers;
190 }
191
192 /** Get the list of registered filters
193 */
194
195 mlt_properties mlt_repository_filters( mlt_repository self )
196 {
197         return self->filters;
198 }
199
200 /** Get the list of registered producers
201 */
202
203 mlt_properties mlt_repository_producers( mlt_repository self )
204 {
205         return self->producers;
206 }
207
208 /** Get the list of registered transitions
209 */
210
211 mlt_properties mlt_repository_transitions( mlt_repository self )
212 {
213         return self->transitions;
214 }
215
216 /** Register the metadata for a service
217     IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties that you supply!
218 */
219 void mlt_repository_register_metadata( mlt_repository self, mlt_service_type type, const char *service, mlt_properties metadata )
220 {
221         mlt_properties service_properties = get_service_properties( self, type, service );
222         mlt_properties_set_data( service_properties, "metadata", metadata, 0, ( mlt_destructor )mlt_properties_close, NULL );
223 }
224
225 /** Get the metadata about a service
226 */
227
228 mlt_properties mlt_repository_metadata( mlt_repository self, mlt_service_type type, const char *service )
229 {
230         mlt_properties properties = get_service_properties( self, type, service );
231         return mlt_properties_get_data( properties, "metadata", NULL );
232 }