3 * \brief the factory method interfaces
5 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
6 * \author Charles Yates <charles.yates@pandora.be>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "mlt_repository.h"
30 /** the default subdirectory of the libdir for holding modules (plugins) */
31 #define PREFIX_LIB LIBDIR "/mlt"
32 /** the default subdirectory of the install prefix for holding module (plugin) data */
33 #define PREFIX_DATA PREFIX "/share/mlt"
36 /** holds the full path to the modules directory - initialized and retained for the entire session */
37 static char *mlt_directory = NULL;
38 /** a global properties list for holding environment config data and things needing session-oriented cleanup */
39 static mlt_properties global_properties = NULL;
40 /** the global repository singleton */
41 static mlt_repository repository = NULL;
42 /** the events object for the factory events */
43 static mlt_properties event_object = NULL;
44 /** for tracking the unique_id set on each constructed service */
45 static int unique_id = 0;
47 /* Event transmitters. */
49 /** the -create-request event transmitter
57 static void mlt_factory_create_request( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
59 if ( listener != NULL )
60 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service * )args[ 2 ] );
63 /** the -create-done event transmitter
71 static void mlt_factory_create_done( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
73 if ( listener != NULL )
74 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
77 /** Construct the repository and factories.
79 * The environment variable MLT_PRODUCER is the name of a default producer often used by other services, defaults to "fezzil".
81 * The environment variable MLT_CONSUMER is the name of a default consumer, defaults to "sdl".
83 * The environment variable MLT_TEST_CARD is the name of a producer or file to be played when nothing is available (all tracks blank).
85 * The environment variable MLT_DATA overrides the default full path to the MLT and module supplemental data files, defaults to \p PREFIX_DATA.
87 * The environment variable MLT_PROFILE defaults to "dv_pal."
89 * The environment variable MLT_REPOSITORY overrides the default location of the plugin modules, defaults to \p PREFIX_LIB.
91 * \param directory an optional full path to a directory containing the modules that overrides the default and
92 * the MLT_REPOSITORY environment variable
93 * \return the repository
96 mlt_repository mlt_factory_init( const char *directory )
98 // Only initialise once
99 if ( mlt_directory == NULL )
101 // Allow user over rides
102 if ( directory == NULL || !strcmp( directory, "" ) )
103 directory = getenv( "MLT_REPOSITORY" );
105 // If no directory is specified, default to install directory
106 if ( directory == NULL )
107 directory = PREFIX_LIB;
109 // Store the prefix for later retrieval
110 mlt_directory = strdup( directory );
112 // Initialise the pool
115 // Create and set up the events object
116 event_object = mlt_properties_new( );
117 mlt_events_init( event_object );
118 mlt_events_register( event_object, "producer-create-request", ( mlt_transmitter )mlt_factory_create_request );
119 mlt_events_register( event_object, "producer-create-done", ( mlt_transmitter )mlt_factory_create_done );
120 mlt_events_register( event_object, "filter-create-request", ( mlt_transmitter )mlt_factory_create_request );
121 mlt_events_register( event_object, "filter-create-done", ( mlt_transmitter )mlt_factory_create_done );
122 mlt_events_register( event_object, "transition-create-request", ( mlt_transmitter )mlt_factory_create_request );
123 mlt_events_register( event_object, "transition-create-done", ( mlt_transmitter )mlt_factory_create_done );
124 mlt_events_register( event_object, "consumer-create-request", ( mlt_transmitter )mlt_factory_create_request );
125 mlt_events_register( event_object, "consumer-create-done", ( mlt_transmitter )mlt_factory_create_done );
127 // Create the global properties
128 global_properties = mlt_properties_new( );
130 // Create the repository of services
131 repository = mlt_repository_init( directory );
133 // Force a clean up when app closes
134 atexit( mlt_factory_close );
137 // Allow property refresh on a subsequent initialisation
138 if ( global_properties != NULL )
140 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
141 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "fezzik" );
142 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
143 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
144 mlt_properties_set_or_default( global_properties, "MLT_PROFILE", getenv( "MLT_PROFILE" ), "dv_pal" );
145 mlt_properties_set_or_default( global_properties, "MLT_DATA", getenv( "MLT_DATA" ), PREFIX_DATA );
152 /** Fetch the events object.
154 * \return the global factory event object
157 mlt_properties mlt_factory_event_object( )
162 /** Fetch the module directory used in this instance.
164 * \return the full path to the module directory that this session is using
167 const char *mlt_factory_directory( )
169 return mlt_directory;
172 /** Get a value from the environment.
174 * \param name the name of a MLT (runtime configuration) environment variable
175 * \return the value of the variable
178 char *mlt_environment( const char *name )
180 if ( global_properties )
181 return mlt_properties_get( global_properties, name );
186 /** Set a value in the environment.
188 * \param name the name of a MLT environment variable
189 * \param value the value of the variable
190 * \return true on error
193 int mlt_environment_set( const char *name, const char *value )
195 if ( global_properties )
196 return mlt_properties_set( global_properties, name, value );
201 /** Set some properties common to all services.
203 * This sets _unique_id, \p mlt_type, \p mlt_service (unless _mlt_service_hidden), and _profile.
205 * \param properties a service's properties list
206 * \param profile the \p mlt_profile supplied to the factory function
207 * \param type the MLT service class
208 * \param service the name of the service
211 static void set_common_properties( mlt_properties properties, mlt_profile profile, const char *type, const char *service )
213 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
214 mlt_properties_set( properties, "mlt_type", type );
215 if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
216 mlt_properties_set( properties, "mlt_service", service );
217 if ( profile != NULL )
218 mlt_properties_set_data( properties, "_profile", profile, 0, NULL, NULL );
221 /** Fetch a producer from the repository.
223 * \param profile the \p mlt_profile to use
224 * \param service the name of the producer (optional, defaults to MLT_PRODUCER)
225 * \param input an optional argument to the producer constructor, typically a string
226 * \return a new producer
229 mlt_producer mlt_factory_producer( mlt_profile profile, const char *service, const void *input )
231 mlt_producer obj = NULL;
233 // Pick up the default normalising producer if necessary
234 if ( service == NULL )
235 service = mlt_environment( "MLT_PRODUCER" );
237 // Offer the application the chance to 'create'
238 mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
240 // Try to instantiate via the specified service
243 obj = mlt_repository_create( repository, profile, producer_type, service, input );
244 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
247 mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
248 set_common_properties( properties, profile, "producer", service );
254 /** Fetch a filter from the repository.
256 * \param profile the \p mlt_profile to use
257 * \param service the name of the filter
258 * \param input an optional argument to the filter constructor, typically a string
259 * \return a new filter
262 mlt_filter mlt_factory_filter( mlt_profile profile, const char *service, const void *input )
264 mlt_filter obj = NULL;
266 // Offer the application the chance to 'create'
267 mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
271 obj = mlt_repository_create( repository, profile, filter_type, service, input );
272 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
277 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
278 set_common_properties( properties, profile, "filter", service );
283 /** Fetch a transition from the repository.
285 * \param profile the \p mlt_profile to use
286 * \param service the name of the transition
287 * \param input an optional argument to the transition constructor, typically a string
288 * \return a new transition
291 mlt_transition mlt_factory_transition( mlt_profile profile, const char *service, const void *input )
293 mlt_transition obj = NULL;
295 // Offer the application the chance to 'create'
296 mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
300 obj = mlt_repository_create( repository, profile, transition_type, service, input );
301 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
306 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
307 set_common_properties( properties, profile, "transition", service );
312 /** Fetch a consumer from the repository.
314 * \param profile the \p mlt_profile to use
315 * \param service the name of the consumer (optional, defaults to MLT_CONSUMER)
316 * \param input an optional argument to the consumer constructor, typically a string
317 * \return a new consumer
320 mlt_consumer mlt_factory_consumer( mlt_profile profile, const char *service, const void *input )
322 mlt_consumer obj = NULL;
324 if ( service == NULL )
325 service = mlt_environment( "MLT_CONSUMER" );
327 // Offer the application the chance to 'create'
328 mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
332 obj = mlt_repository_create( repository, profile, consumer_type, service, input );
333 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
338 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
339 set_common_properties( properties, profile, "consumer", service );
344 /** Register an object for clean up.
346 * \param ptr an opaque pointer to anything allocated on the heap
347 * \param destructor the function pointer of the deallocation subroutine (e.g., free or \p mlt_pool_release)
350 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
353 sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
354 mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
357 /** Close the factory.
359 * Cleanup all resources for the session.
362 void mlt_factory_close( )
364 if ( mlt_directory != NULL )
366 mlt_properties_close( event_object );
367 mlt_properties_close( global_properties );
368 mlt_repository_close( repository );
369 free( mlt_directory );
370 mlt_directory = NULL;
375 mlt_properties mlt_global_properties( )
377 return global_properties;