2 * mlt_factory.c -- the factory method interfaces
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "mlt_repository.h"
29 /** Singleton repositories
32 static char *mlt_prefix = NULL;
33 static mlt_properties global_properties = NULL;
34 static mlt_properties object_list = NULL;
35 static mlt_repository producers = NULL;
36 static mlt_repository filters = NULL;
37 static mlt_repository transitions = NULL;
38 static mlt_repository consumers = NULL;
39 static mlt_properties event_object = NULL;
40 static int unique_id = 0;
42 /** Event transmitters.
45 static void mlt_factory_create_request( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
47 if ( listener != NULL )
48 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service * )args[ 2 ] );
51 static void mlt_factory_create_done( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
53 if ( listener != NULL )
54 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
57 /** Construct the factories.
60 int mlt_factory_init( const char *prefix )
62 // Only initialise once
63 if ( mlt_prefix == NULL )
65 // Allow user over rides
66 if ( prefix == NULL || !strcmp( prefix, "" ) )
67 prefix = getenv( "MLT_REPOSITORY" );
69 // If no directory is specified, default to install directory
73 // Store the prefix for later retrieval
74 mlt_prefix = strdup( prefix );
76 // Initialise the pool
79 // Create and set up the events object
80 event_object = mlt_properties_new( );
81 mlt_events_init( event_object );
82 mlt_events_register( event_object, "producer-create-request", ( mlt_transmitter )mlt_factory_create_request );
83 mlt_events_register( event_object, "producer-create-done", ( mlt_transmitter )mlt_factory_create_done );
84 mlt_events_register( event_object, "filter-create-request", ( mlt_transmitter )mlt_factory_create_request );
85 mlt_events_register( event_object, "filter-create-done", ( mlt_transmitter )mlt_factory_create_done );
86 mlt_events_register( event_object, "transition-create-request", ( mlt_transmitter )mlt_factory_create_request );
87 mlt_events_register( event_object, "transition-create-done", ( mlt_transmitter )mlt_factory_create_done );
88 mlt_events_register( event_object, "consumer-create-request", ( mlt_transmitter )mlt_factory_create_request );
89 mlt_events_register( event_object, "consumer-create-done", ( mlt_transmitter )mlt_factory_create_done );
91 // Create the global properties
92 global_properties = mlt_properties_new( );
94 // Create the object list.
95 object_list = mlt_properties_new( );
97 // Create a repository for each service type
98 producers = mlt_repository_init( object_list, prefix, "producers", "mlt_create_producer" );
99 filters = mlt_repository_init( object_list, prefix, "filters", "mlt_create_filter" );
100 transitions = mlt_repository_init( object_list, prefix, "transitions", "mlt_create_transition" );
101 consumers = mlt_repository_init( object_list, prefix, "consumers", "mlt_create_consumer" );
103 // Force a clean up when app closes
104 atexit( mlt_factory_close );
107 // Allow property refresh on a subsequent initialisation
108 if ( global_properties != NULL )
110 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
111 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "fezzik" );
112 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
113 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
119 /** Fetch the events object.
122 mlt_properties mlt_factory_event_object( )
127 /** Fetch the prefix used in this instance.
130 const char *mlt_factory_prefix( )
135 /** Get a value from the environment.
138 char *mlt_environment( const char *name )
140 return mlt_properties_get( global_properties, name );
143 /** Fetch a producer from the repository.
146 mlt_producer mlt_factory_producer( const char *service, void *input )
148 mlt_producer obj = NULL;
150 // Pick up the default normalising producer if necessary
151 if ( service == NULL )
152 service = mlt_environment( "MLT_PRODUCER" );
154 // Offer the application the chance to 'create'
155 mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
157 // Try to instantiate via the specified service
160 obj = mlt_repository_fetch( producers, service, input );
161 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
164 mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
165 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
166 mlt_properties_set( properties, "mlt_type", "producer" );
167 if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
168 mlt_properties_set( properties, "mlt_service", service );
174 /** Fetch a filter from the repository.
177 mlt_filter mlt_factory_filter( const char *service, void *input )
179 mlt_filter obj = NULL;
181 // Offer the application the chance to 'create'
182 mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
186 obj = mlt_repository_fetch( filters, service, input );
187 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
192 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
193 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
194 mlt_properties_set( properties, "mlt_type", "filter" );
195 mlt_properties_set( properties, "mlt_service", service );
200 /** Fetch a transition from the repository.
203 mlt_transition mlt_factory_transition( const char *service, void *input )
205 mlt_transition obj = NULL;
207 // Offer the application the chance to 'create'
208 mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
212 obj = mlt_repository_fetch( transitions, service, input );
213 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
218 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
219 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
220 mlt_properties_set( properties, "mlt_type", "transition" );
221 mlt_properties_set( properties, "mlt_service", service );
226 /** Fetch a consumer from the repository
229 mlt_consumer mlt_factory_consumer( const char *service, void *input )
231 mlt_consumer obj = NULL;
233 if ( service == NULL )
234 service = mlt_environment( "MLT_CONSUMER" );
236 // Offer the application the chance to 'create'
237 mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
241 obj = mlt_repository_fetch( consumers, service, input );
242 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
247 mlt_filter filter = mlt_factory_filter( "data_show", NULL );
248 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
249 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
250 mlt_properties_set( properties, "mlt_type", "consumer" );
251 mlt_properties_set( properties, "mlt_service", service );
252 mlt_service_attach( MLT_CONSUMER_SERVICE( obj ), filter );
253 mlt_filter_close( filter );
258 /** Register an object for clean up.
261 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
264 sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
265 mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
268 /** Close the factory.
271 void mlt_factory_close( )
273 if ( mlt_prefix != NULL )
275 mlt_properties_close( event_object );
276 mlt_repository_close( producers );
277 mlt_repository_close( filters );
278 mlt_repository_close( transitions );
279 mlt_repository_close( consumers );
280 mlt_properties_close( global_properties );
281 mlt_properties_close( object_list );