]> git.sesse.net Git - mlt/blob - src/framework/mlt_factory.c
move binary modules to libdir - affects MLT_REPOSITORY
[mlt] / src / framework / mlt_factory.c
1 /*
2  * mlt_factory.c -- the factory method interfaces
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.h"
22 #include "mlt_repository.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define PREFIX_LIB LIBDIR "/mlt"
29 #define PREFIX_DATA PREFIX "/share/mlt"
30
31 /** Singleton repositories
32 */
33
34 static char *mlt_prefix = NULL;
35 static mlt_properties global_properties = NULL;
36 static mlt_properties object_list = NULL;
37 static mlt_repository producers = NULL;
38 static mlt_repository filters = NULL;
39 static mlt_repository transitions = NULL;
40 static mlt_repository consumers = NULL;
41 static mlt_properties event_object = NULL;
42 static int unique_id = 0;
43
44 /** Event transmitters.
45 */
46
47 static void mlt_factory_create_request( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
48 {
49         if ( listener != NULL )
50                 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service * )args[ 2 ] );
51 }
52
53 static void mlt_factory_create_done( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
54 {
55         if ( listener != NULL )
56                 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
57 }
58
59 /** Construct the factories.
60 */
61
62 int mlt_factory_init( const char *prefix )
63 {
64         // Only initialise once
65         if ( mlt_prefix == NULL )
66         {
67                 // Allow user over rides
68                 if ( prefix == NULL || !strcmp( prefix, "" ) )
69                         prefix = getenv( "MLT_REPOSITORY" );
70
71                 // If no directory is specified, default to install directory
72                 if ( prefix == NULL )
73                         prefix = PREFIX_LIB;
74
75                 // Store the prefix for later retrieval
76                 mlt_prefix = strdup( prefix );
77
78                 // Initialise the pool
79                 mlt_pool_init( );
80
81                 // Create and set up the events object
82                 event_object = mlt_properties_new( );
83                 mlt_events_init( event_object );
84                 mlt_events_register( event_object, "producer-create-request", ( mlt_transmitter )mlt_factory_create_request );
85                 mlt_events_register( event_object, "producer-create-done", ( mlt_transmitter )mlt_factory_create_done );
86                 mlt_events_register( event_object, "filter-create-request", ( mlt_transmitter )mlt_factory_create_request );
87                 mlt_events_register( event_object, "filter-create-done", ( mlt_transmitter )mlt_factory_create_done );
88                 mlt_events_register( event_object, "transition-create-request", ( mlt_transmitter )mlt_factory_create_request );
89                 mlt_events_register( event_object, "transition-create-done", ( mlt_transmitter )mlt_factory_create_done );
90                 mlt_events_register( event_object, "consumer-create-request", ( mlt_transmitter )mlt_factory_create_request );
91                 mlt_events_register( event_object, "consumer-create-done", ( mlt_transmitter )mlt_factory_create_done );
92
93                 // Create the global properties
94                 global_properties = mlt_properties_new( );
95
96                 // Create the object list.
97                 object_list = mlt_properties_new( );
98
99                 // Create a repository for each service type
100                 producers = mlt_repository_init( object_list, prefix, "producers", "mlt_create_producer" );
101                 filters = mlt_repository_init( object_list, prefix, "filters", "mlt_create_filter" );
102                 transitions = mlt_repository_init( object_list, prefix, "transitions", "mlt_create_transition" );
103                 consumers = mlt_repository_init( object_list, prefix, "consumers", "mlt_create_consumer" );
104
105                 // Force a clean up when app closes
106                 atexit( mlt_factory_close );
107         }
108
109         // Allow property refresh on a subsequent initialisation
110         if ( global_properties != NULL )
111         {
112                 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
113                 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "fezzik" );
114                 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
115                 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
116                 mlt_properties_set_or_default( global_properties, "MLT_PROFILE", getenv( "MLT_PROFILE" ), "dv_pal" );
117                 mlt_properties_set_or_default( global_properties, "MLT_DATA", getenv( "MLT_DATA" ), PREFIX_DATA );
118         }
119
120
121         return 0;
122 }
123
124 /** Fetch the events object.
125 */
126
127 mlt_properties mlt_factory_event_object( )
128 {
129         return event_object;
130 }
131
132 /** Fetch the prefix used in this instance.
133 */
134
135 const char *mlt_factory_prefix( )
136 {
137         return mlt_prefix;
138 }
139
140 /** Get a value from the environment.
141 */
142
143 char *mlt_environment( const char *name )
144 {
145         if ( global_properties )
146                 return mlt_properties_get( global_properties, name );
147         else
148                 return NULL;
149 }
150
151 /** Set a value in the environment.
152 */
153
154 int mlt_environment_set( const char *name, const char *value )
155 {
156         if ( global_properties )
157                 return mlt_properties_set( global_properties, name, value );
158         else
159                 return -1;
160 }
161
162 static void set_common_properties( mlt_properties properties, mlt_profile profile, const char *type, const char *service )
163 {
164         mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
165         mlt_properties_set( properties, "mlt_type", type );
166         if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
167                 mlt_properties_set( properties, "mlt_service", service );
168         if ( profile != NULL )
169                 mlt_properties_set_data( properties, "_profile", profile, 0, NULL, NULL );
170 }
171
172 /** Fetch a producer from the repository.
173 */
174
175 mlt_producer mlt_factory_producer( mlt_profile profile, const char *service, void *input )
176 {
177         mlt_producer obj = NULL;
178
179         // Pick up the default normalising producer if necessary
180         if ( service == NULL )
181                 service = mlt_environment( "MLT_PRODUCER" );
182
183         // Offer the application the chance to 'create'
184         mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
185
186         // Try to instantiate via the specified service
187         if ( obj == NULL )
188         {
189                 obj = mlt_repository_fetch( producers, profile, producer_type, service, input );
190                 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
191                 if ( obj != NULL )
192                 {
193                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
194                         set_common_properties( properties, profile, "producer", service );
195                 }
196         }
197         return obj;
198 }
199
200 /** Fetch a filter from the repository.
201 */
202
203 mlt_filter mlt_factory_filter( mlt_profile profile, const char *service, void *input )
204 {
205         mlt_filter obj = NULL;
206
207         // Offer the application the chance to 'create'
208         mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
209
210         if ( obj == NULL )
211         {
212                 obj = mlt_repository_fetch( filters, profile, filter_type, service, input );
213                 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
214         }
215
216         if ( obj != NULL )
217         {
218                 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
219                 set_common_properties( properties, profile, "filter", service );
220         }
221         return obj;
222 }
223
224 /** Fetch a transition from the repository.
225 */
226
227 mlt_transition mlt_factory_transition( mlt_profile profile, const char *service, void *input )
228 {
229         mlt_transition obj = NULL;
230
231         // Offer the application the chance to 'create'
232         mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
233
234         if ( obj == NULL )
235         {
236                 obj = mlt_repository_fetch( transitions, profile, filter_type, service, input );
237                 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
238         }
239
240         if ( obj != NULL )
241         {
242                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
243                 set_common_properties( properties, profile, "transition", service );
244         }
245         return obj;
246 }
247
248 /** Fetch a consumer from the repository
249 */
250
251 mlt_consumer mlt_factory_consumer( mlt_profile profile, const char *service, void *input )
252 {
253         mlt_consumer obj = NULL;
254
255         if ( service == NULL )
256                 service = mlt_environment( "MLT_CONSUMER" );
257
258         // Offer the application the chance to 'create'
259         mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
260
261         if ( obj == NULL )
262         {
263                 obj = mlt_repository_fetch( consumers, profile, consumer_type, service, input );
264                 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
265         }
266
267         if ( obj != NULL )
268         {
269                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
270                 set_common_properties( properties, profile, "consumer", service );
271         }
272         return obj;
273 }
274
275 /** Register an object for clean up.
276 */
277
278 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
279 {
280         char unique[ 256 ];
281         sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
282         mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
283 }
284
285 /** Close the factory.
286 */
287
288 void mlt_factory_close( )
289 {
290         if ( mlt_prefix != NULL )
291         {
292                 mlt_properties_close( event_object );
293                 mlt_repository_close( producers );
294                 mlt_repository_close( filters );
295                 mlt_repository_close( transitions );
296                 mlt_repository_close( consumers );
297                 mlt_properties_close( global_properties );
298                 mlt_properties_close( object_list );
299                 free( mlt_prefix );
300                 mlt_prefix = NULL;
301                 mlt_pool_close( );
302         }
303 }