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"
33 /** the default subdirectory of the libdir for holding modules (plugins) */
34 #define PREFIX_LIB "lib\\mlt"
35 /** the default subdirectory of the install prefix for holding module (plugin) data */
36 #define PREFIX_DATA "share\\mlt"
38 /** the default subdirectory of the libdir for holding modules (plugins) */
39 #define PREFIX_LIB LIBDIR "/mlt"
40 /** the default subdirectory of the install prefix for holding module (plugin) data */
41 #define PREFIX_DATA PREFIX "/share/mlt"
44 /** holds the full path to the modules directory - initialized and retained for the entire session */
45 static char *mlt_directory = NULL;
46 /** a global properties list for holding environment config data and things needing session-oriented cleanup */
47 static mlt_properties global_properties = NULL;
48 /** the global repository singleton */
49 static mlt_repository repository = NULL;
50 /** the events object for the factory events */
51 static mlt_properties event_object = NULL;
52 /** for tracking the unique_id set on each constructed service */
53 static int unique_id = 0;
55 /* Event transmitters. */
57 /** the -create-request event transmitter
65 static void mlt_factory_create_request( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
67 if ( listener != NULL )
68 listener( owner, self, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service * )args[ 2 ] );
71 /** the -create-done event transmitter
79 static void mlt_factory_create_done( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
81 if ( listener != NULL )
82 listener( owner, self, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
85 /** Construct the repository and factories.
87 * The environment variable MLT_PRODUCER is the name of a default producer often used by other services, defaults to "loader".
89 * The environment variable MLT_CONSUMER is the name of a default consumer, defaults to "sdl".
91 * The environment variable MLT_TEST_CARD is the name of a producer or file to be played when nothing is available (all tracks blank).
93 * The environment variable MLT_DATA overrides the default full path to the MLT and module supplemental data files, defaults to \p PREFIX_DATA.
95 * The environment variable MLT_PROFILE defaults to "dv_pal."
97 * The environment variable MLT_REPOSITORY overrides the default location of the plugin modules, defaults to \p PREFIX_LIB.
99 * \param directory an optional full path to a directory containing the modules that overrides the default and
100 * the MLT_REPOSITORY environment variable
101 * \return the repository
104 mlt_repository mlt_factory_init( const char *directory )
106 // Load the system locales
107 setlocale( LC_ALL, "" );
109 if ( ! global_properties )
110 global_properties = mlt_properties_new( );
112 // Allow property refresh on a subsequent initialisation
113 if ( global_properties )
115 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
116 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "loader" );
117 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
118 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
119 mlt_properties_set_or_default( global_properties, "MLT_PROFILE", getenv( "MLT_PROFILE" ), "dv_pal" );
121 mlt_properties_set_or_default( global_properties, "MLT_DATA", getenv( "MLT_DATA" ), PREFIX_DATA );
124 // Only initialise once
125 if ( mlt_directory == NULL )
127 // Allow user over rides
128 if ( directory == NULL || !strcmp( directory, "" ) )
129 directory = getenv( "MLT_REPOSITORY" );
131 // If no directory is specified, default to install directory
132 if ( directory == NULL )
133 directory = PREFIX_LIB;
135 // Store the prefix for later retrieval
136 mlt_directory = strdup( directory );
138 // Initialise the pool
141 // Create and set up the events object
142 event_object = mlt_properties_new( );
143 mlt_events_init( event_object );
144 mlt_events_register( event_object, "producer-create-request", ( mlt_transmitter )mlt_factory_create_request );
145 mlt_events_register( event_object, "producer-create-done", ( mlt_transmitter )mlt_factory_create_done );
146 mlt_events_register( event_object, "filter-create-request", ( mlt_transmitter )mlt_factory_create_request );
147 mlt_events_register( event_object, "filter-create-done", ( mlt_transmitter )mlt_factory_create_done );
148 mlt_events_register( event_object, "transition-create-request", ( mlt_transmitter )mlt_factory_create_request );
149 mlt_events_register( event_object, "transition-create-done", ( mlt_transmitter )mlt_factory_create_done );
150 mlt_events_register( event_object, "consumer-create-request", ( mlt_transmitter )mlt_factory_create_request );
151 mlt_events_register( event_object, "consumer-create-done", ( mlt_transmitter )mlt_factory_create_done );
153 // Create the repository of services
154 repository = mlt_repository_init( directory );
156 // Force a clean up when app closes
157 atexit( mlt_factory_close );
163 /** Fetch the events object.
165 * \return the global factory event object
168 mlt_properties mlt_factory_event_object( )
173 /** Fetch the module directory used in this instance.
175 * \return the full path to the module directory that this session is using
178 const char *mlt_factory_directory( )
180 return mlt_directory;
183 /** Get a value from the environment.
185 * \param name the name of a MLT (runtime configuration) environment variable
186 * \return the value of the variable
189 char *mlt_environment( const char *name )
191 if ( global_properties )
192 return mlt_properties_get( global_properties, name );
197 /** Set a value in the environment.
199 * \param name the name of a MLT environment variable
200 * \param value the value of the variable
201 * \return true on error
204 int mlt_environment_set( const char *name, const char *value )
206 if ( global_properties )
207 return mlt_properties_set( global_properties, name, value );
212 /** Set some properties common to all services.
214 * This sets _unique_id, \p mlt_type, \p mlt_service (unless _mlt_service_hidden), and _profile.
216 * \param properties a service's properties list
217 * \param profile the \p mlt_profile supplied to the factory function
218 * \param type the MLT service class
219 * \param service the name of the service
222 static void set_common_properties( mlt_properties properties, mlt_profile profile, const char *type, const char *service )
224 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
225 mlt_properties_set( properties, "mlt_type", type );
226 if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
227 mlt_properties_set( properties, "mlt_service", service );
228 if ( profile != NULL )
229 mlt_properties_set_data( properties, "_profile", profile, 0, NULL, NULL );
232 /** Fetch a producer from the repository.
234 * \param profile the \p mlt_profile to use
235 * \param service the name of the producer (optional, defaults to MLT_PRODUCER)
236 * \param input an optional argument to the producer constructor, typically a string
237 * \return a new producer
240 mlt_producer mlt_factory_producer( mlt_profile profile, const char *service, const void *input )
242 mlt_producer obj = NULL;
244 // Pick up the default normalising producer if necessary
245 if ( service == NULL )
246 service = mlt_environment( "MLT_PRODUCER" );
248 // Offer the application the chance to 'create'
249 mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
251 // Try to instantiate via the specified service
254 obj = mlt_repository_create( repository, profile, producer_type, service, input );
255 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
258 mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
259 set_common_properties( properties, profile, "producer", service );
265 /** Fetch a filter from the repository.
267 * \param profile the \p mlt_profile to use
268 * \param service the name of the filter
269 * \param input an optional argument to the filter constructor, typically a string
270 * \return a new filter
273 mlt_filter mlt_factory_filter( mlt_profile profile, const char *service, const void *input )
275 mlt_filter obj = NULL;
277 // Offer the application the chance to 'create'
278 mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
282 obj = mlt_repository_create( repository, profile, filter_type, service, input );
283 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
288 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
289 set_common_properties( properties, profile, "filter", service );
294 /** Fetch a transition from the repository.
296 * \param profile the \p mlt_profile to use
297 * \param service the name of the transition
298 * \param input an optional argument to the transition constructor, typically a string
299 * \return a new transition
302 mlt_transition mlt_factory_transition( mlt_profile profile, const char *service, const void *input )
304 mlt_transition obj = NULL;
306 // Offer the application the chance to 'create'
307 mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
311 obj = mlt_repository_create( repository, profile, transition_type, service, input );
312 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
317 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
318 set_common_properties( properties, profile, "transition", service );
323 /** Fetch a consumer from the repository.
325 * \param profile the \p mlt_profile to use
326 * \param service the name of the consumer (optional, defaults to MLT_CONSUMER)
327 * \param input an optional argument to the consumer constructor, typically a string
328 * \return a new consumer
331 mlt_consumer mlt_factory_consumer( mlt_profile profile, const char *service, const void *input )
333 mlt_consumer obj = NULL;
335 if ( service == NULL )
336 service = mlt_environment( "MLT_CONSUMER" );
338 // Offer the application the chance to 'create'
339 mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
343 obj = mlt_repository_create( repository, profile, consumer_type, service, input );
344 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
349 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
350 set_common_properties( properties, profile, "consumer", service );
355 /** Register an object for clean up.
357 * \param ptr an opaque pointer to anything allocated on the heap
358 * \param destructor the function pointer of the deallocation subroutine (e.g., free or \p mlt_pool_release)
361 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
364 sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
365 mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
368 /** Close the factory.
370 * Cleanup all resources for the session.
373 void mlt_factory_close( )
375 if ( mlt_directory != NULL )
377 mlt_properties_close( event_object );
379 mlt_properties_close( global_properties );
380 global_properties = NULL;
383 mlt_repository_close( repository );
386 free( mlt_directory );
387 mlt_directory = NULL;
392 mlt_properties mlt_global_properties( )
394 return global_properties;