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 "loader".
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 if ( ! global_properties )
99 global_properties = mlt_properties_new( );
101 // Allow property refresh on a subsequent initialisation
102 if ( global_properties )
104 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
105 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "loader" );
106 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
107 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
108 mlt_properties_set_or_default( global_properties, "MLT_PROFILE", getenv( "MLT_PROFILE" ), "dv_pal" );
109 mlt_properties_set_or_default( global_properties, "MLT_DATA", getenv( "MLT_DATA" ), PREFIX_DATA );
112 // Only initialise once
113 if ( mlt_directory == NULL )
115 // Allow user over rides
116 if ( directory == NULL || !strcmp( directory, "" ) )
117 directory = getenv( "MLT_REPOSITORY" );
119 // If no directory is specified, default to install directory
120 if ( directory == NULL )
121 directory = PREFIX_LIB;
123 // Store the prefix for later retrieval
124 mlt_directory = strdup( directory );
126 // Initialise the pool
129 // Create and set up the events object
130 event_object = mlt_properties_new( );
131 mlt_events_init( event_object );
132 mlt_events_register( event_object, "producer-create-request", ( mlt_transmitter )mlt_factory_create_request );
133 mlt_events_register( event_object, "producer-create-done", ( mlt_transmitter )mlt_factory_create_done );
134 mlt_events_register( event_object, "filter-create-request", ( mlt_transmitter )mlt_factory_create_request );
135 mlt_events_register( event_object, "filter-create-done", ( mlt_transmitter )mlt_factory_create_done );
136 mlt_events_register( event_object, "transition-create-request", ( mlt_transmitter )mlt_factory_create_request );
137 mlt_events_register( event_object, "transition-create-done", ( mlt_transmitter )mlt_factory_create_done );
138 mlt_events_register( event_object, "consumer-create-request", ( mlt_transmitter )mlt_factory_create_request );
139 mlt_events_register( event_object, "consumer-create-done", ( mlt_transmitter )mlt_factory_create_done );
141 // Create the repository of services
142 repository = mlt_repository_init( directory );
144 // Force a clean up when app closes
145 atexit( mlt_factory_close );
151 /** Fetch the events object.
153 * \return the global factory event object
156 mlt_properties mlt_factory_event_object( )
161 /** Fetch the module directory used in this instance.
163 * \return the full path to the module directory that this session is using
166 const char *mlt_factory_directory( )
168 return mlt_directory;
171 /** Get a value from the environment.
173 * \param name the name of a MLT (runtime configuration) environment variable
174 * \return the value of the variable
177 char *mlt_environment( const char *name )
179 if ( global_properties )
180 return mlt_properties_get( global_properties, name );
185 /** Set a value in the environment.
187 * \param name the name of a MLT environment variable
188 * \param value the value of the variable
189 * \return true on error
192 int mlt_environment_set( const char *name, const char *value )
194 if ( global_properties )
195 return mlt_properties_set( global_properties, name, value );
200 /** Set some properties common to all services.
202 * This sets _unique_id, \p mlt_type, \p mlt_service (unless _mlt_service_hidden), and _profile.
204 * \param properties a service's properties list
205 * \param profile the \p mlt_profile supplied to the factory function
206 * \param type the MLT service class
207 * \param service the name of the service
210 static void set_common_properties( mlt_properties properties, mlt_profile profile, const char *type, const char *service )
212 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
213 mlt_properties_set( properties, "mlt_type", type );
214 if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
215 mlt_properties_set( properties, "mlt_service", service );
216 if ( profile != NULL )
217 mlt_properties_set_data( properties, "_profile", profile, 0, NULL, NULL );
220 /** Fetch a producer from the repository.
222 * \param profile the \p mlt_profile to use
223 * \param service the name of the producer (optional, defaults to MLT_PRODUCER)
224 * \param input an optional argument to the producer constructor, typically a string
225 * \return a new producer
228 mlt_producer mlt_factory_producer( mlt_profile profile, const char *service, const void *input )
230 mlt_producer obj = NULL;
232 // Pick up the default normalising producer if necessary
233 if ( service == NULL )
234 service = mlt_environment( "MLT_PRODUCER" );
236 // Offer the application the chance to 'create'
237 mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
239 // Try to instantiate via the specified service
242 obj = mlt_repository_create( repository, profile, producer_type, service, input );
243 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
246 mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
247 set_common_properties( properties, profile, "producer", service );
253 /** Fetch a filter from the repository.
255 * \param profile the \p mlt_profile to use
256 * \param service the name of the filter
257 * \param input an optional argument to the filter constructor, typically a string
258 * \return a new filter
261 mlt_filter mlt_factory_filter( mlt_profile profile, const char *service, const void *input )
263 mlt_filter obj = NULL;
265 // Offer the application the chance to 'create'
266 mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
270 obj = mlt_repository_create( repository, profile, filter_type, service, input );
271 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
276 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
277 set_common_properties( properties, profile, "filter", service );
282 /** Fetch a transition from the repository.
284 * \param profile the \p mlt_profile to use
285 * \param service the name of the transition
286 * \param input an optional argument to the transition constructor, typically a string
287 * \return a new transition
290 mlt_transition mlt_factory_transition( mlt_profile profile, const char *service, const void *input )
292 mlt_transition obj = NULL;
294 // Offer the application the chance to 'create'
295 mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
299 obj = mlt_repository_create( repository, profile, transition_type, service, input );
300 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
305 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
306 set_common_properties( properties, profile, "transition", service );
311 /** Fetch a consumer from the repository.
313 * \param profile the \p mlt_profile to use
314 * \param service the name of the consumer (optional, defaults to MLT_CONSUMER)
315 * \param input an optional argument to the consumer constructor, typically a string
316 * \return a new consumer
319 mlt_consumer mlt_factory_consumer( mlt_profile profile, const char *service, const void *input )
321 mlt_consumer obj = NULL;
323 if ( service == NULL )
324 service = mlt_environment( "MLT_CONSUMER" );
326 // Offer the application the chance to 'create'
327 mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
331 obj = mlt_repository_create( repository, profile, consumer_type, service, input );
332 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
337 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
338 set_common_properties( properties, profile, "consumer", service );
343 /** Register an object for clean up.
345 * \param ptr an opaque pointer to anything allocated on the heap
346 * \param destructor the function pointer of the deallocation subroutine (e.g., free or \p mlt_pool_release)
349 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
352 sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
353 mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
356 /** Close the factory.
358 * Cleanup all resources for the session.
361 void mlt_factory_close( )
363 if ( mlt_directory != NULL )
365 mlt_properties_close( event_object );
366 mlt_properties_close( global_properties );
367 mlt_repository_close( repository );
368 free( mlt_directory );
369 mlt_directory = NULL;
374 mlt_properties mlt_global_properties( )
376 return global_properties;