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"
32 /** the default subdirectory of the libdir for holding modules (plugins) */
33 #define PREFIX_LIB "lib\\mlt"
34 /** the default subdirectory of the install prefix for holding module (plugin) data */
35 #define PREFIX_DATA "share\\mlt"
37 /** the default subdirectory of the libdir for holding modules (plugins) */
38 #define PREFIX_LIB LIBDIR "/mlt"
39 /** the default subdirectory of the install prefix for holding module (plugin) data */
40 #define PREFIX_DATA PREFIX "/share/mlt"
43 /** holds the full path to the modules directory - initialized and retained for the entire session */
44 static char *mlt_directory = NULL;
45 /** a global properties list for holding environment config data and things needing session-oriented cleanup */
46 static mlt_properties global_properties = NULL;
47 /** the global repository singleton */
48 static mlt_repository repository = NULL;
49 /** the events object for the factory events */
50 static mlt_properties event_object = NULL;
51 /** for tracking the unique_id set on each constructed service */
52 static int unique_id = 0;
54 /* Event transmitters. */
56 /** the -create-request event transmitter
64 static void mlt_factory_create_request( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
66 if ( listener != NULL )
67 listener( owner, self, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service * )args[ 2 ] );
70 /** the -create-done event transmitter
78 static void mlt_factory_create_done( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
80 if ( listener != NULL )
81 listener( owner, self, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
84 /** Construct the repository and factories.
86 * The environment variable MLT_PRODUCER is the name of a default producer often used by other services, defaults to "loader".
88 * The environment variable MLT_CONSUMER is the name of a default consumer, defaults to "sdl".
90 * The environment variable MLT_TEST_CARD is the name of a producer or file to be played when nothing is available (all tracks blank).
92 * The environment variable MLT_DATA overrides the default full path to the MLT and module supplemental data files, defaults to \p PREFIX_DATA.
94 * The environment variable MLT_PROFILE defaults to "dv_pal."
96 * The environment variable MLT_REPOSITORY overrides the default location of the plugin modules, defaults to \p PREFIX_LIB.
98 * \param directory an optional full path to a directory containing the modules that overrides the default and
99 * the MLT_REPOSITORY environment variable
100 * \return the repository
103 mlt_repository mlt_factory_init( const char *directory )
105 if ( ! global_properties )
106 global_properties = mlt_properties_new( );
108 // Allow property refresh on a subsequent initialisation
109 if ( global_properties )
111 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
112 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "loader" );
113 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
114 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
115 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 );
120 // Only initialise once
121 if ( mlt_directory == NULL )
123 // Allow user over rides
124 if ( directory == NULL || !strcmp( directory, "" ) )
125 directory = getenv( "MLT_REPOSITORY" );
127 // If no directory is specified, default to install directory
128 if ( directory == NULL )
129 directory = PREFIX_LIB;
131 // Store the prefix for later retrieval
132 mlt_directory = strdup( directory );
134 // Initialise the pool
137 // Create and set up the events object
138 event_object = mlt_properties_new( );
139 mlt_events_init( event_object );
140 mlt_events_register( event_object, "producer-create-request", ( mlt_transmitter )mlt_factory_create_request );
141 mlt_events_register( event_object, "producer-create-done", ( mlt_transmitter )mlt_factory_create_done );
142 mlt_events_register( event_object, "filter-create-request", ( mlt_transmitter )mlt_factory_create_request );
143 mlt_events_register( event_object, "filter-create-done", ( mlt_transmitter )mlt_factory_create_done );
144 mlt_events_register( event_object, "transition-create-request", ( mlt_transmitter )mlt_factory_create_request );
145 mlt_events_register( event_object, "transition-create-done", ( mlt_transmitter )mlt_factory_create_done );
146 mlt_events_register( event_object, "consumer-create-request", ( mlt_transmitter )mlt_factory_create_request );
147 mlt_events_register( event_object, "consumer-create-done", ( mlt_transmitter )mlt_factory_create_done );
149 // Create the repository of services
150 repository = mlt_repository_init( directory );
152 // Force a clean up when app closes
153 atexit( mlt_factory_close );
159 /** Fetch the events object.
161 * \return the global factory event object
164 mlt_properties mlt_factory_event_object( )
169 /** Fetch the module directory used in this instance.
171 * \return the full path to the module directory that this session is using
174 const char *mlt_factory_directory( )
176 return mlt_directory;
179 /** Get a value from the environment.
181 * \param name the name of a MLT (runtime configuration) environment variable
182 * \return the value of the variable
185 char *mlt_environment( const char *name )
187 if ( global_properties )
188 return mlt_properties_get( global_properties, name );
193 /** Set a value in the environment.
195 * \param name the name of a MLT environment variable
196 * \param value the value of the variable
197 * \return true on error
200 int mlt_environment_set( const char *name, const char *value )
202 if ( global_properties )
203 return mlt_properties_set( global_properties, name, value );
208 /** Set some properties common to all services.
210 * This sets _unique_id, \p mlt_type, \p mlt_service (unless _mlt_service_hidden), and _profile.
212 * \param properties a service's properties list
213 * \param profile the \p mlt_profile supplied to the factory function
214 * \param type the MLT service class
215 * \param service the name of the service
218 static void set_common_properties( mlt_properties properties, mlt_profile profile, const char *type, const char *service )
220 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
221 mlt_properties_set( properties, "mlt_type", type );
222 if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
223 mlt_properties_set( properties, "mlt_service", service );
224 if ( profile != NULL )
225 mlt_properties_set_data( properties, "_profile", profile, 0, NULL, NULL );
228 /** Fetch a producer from the repository.
230 * \param profile the \p mlt_profile to use
231 * \param service the name of the producer (optional, defaults to MLT_PRODUCER)
232 * \param input an optional argument to the producer constructor, typically a string
233 * \return a new producer
236 mlt_producer mlt_factory_producer( mlt_profile profile, const char *service, const void *input )
238 mlt_producer obj = NULL;
240 // Pick up the default normalising producer if necessary
241 if ( service == NULL )
242 service = mlt_environment( "MLT_PRODUCER" );
244 // Offer the application the chance to 'create'
245 mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
247 // Try to instantiate via the specified service
250 obj = mlt_repository_create( repository, profile, producer_type, service, input );
251 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
254 mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
255 set_common_properties( properties, profile, "producer", service );
261 /** Fetch a filter from the repository.
263 * \param profile the \p mlt_profile to use
264 * \param service the name of the filter
265 * \param input an optional argument to the filter constructor, typically a string
266 * \return a new filter
269 mlt_filter mlt_factory_filter( mlt_profile profile, const char *service, const void *input )
271 mlt_filter obj = NULL;
273 // Offer the application the chance to 'create'
274 mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
278 obj = mlt_repository_create( repository, profile, filter_type, service, input );
279 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
284 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
285 set_common_properties( properties, profile, "filter", service );
290 /** Fetch a transition from the repository.
292 * \param profile the \p mlt_profile to use
293 * \param service the name of the transition
294 * \param input an optional argument to the transition constructor, typically a string
295 * \return a new transition
298 mlt_transition mlt_factory_transition( mlt_profile profile, const char *service, const void *input )
300 mlt_transition obj = NULL;
302 // Offer the application the chance to 'create'
303 mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
307 obj = mlt_repository_create( repository, profile, transition_type, service, input );
308 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
313 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
314 set_common_properties( properties, profile, "transition", service );
319 /** Fetch a consumer from the repository.
321 * \param profile the \p mlt_profile to use
322 * \param service the name of the consumer (optional, defaults to MLT_CONSUMER)
323 * \param input an optional argument to the consumer constructor, typically a string
324 * \return a new consumer
327 mlt_consumer mlt_factory_consumer( mlt_profile profile, const char *service, const void *input )
329 mlt_consumer obj = NULL;
331 if ( service == NULL )
332 service = mlt_environment( "MLT_CONSUMER" );
334 // Offer the application the chance to 'create'
335 mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
339 obj = mlt_repository_create( repository, profile, consumer_type, service, input );
340 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
345 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
346 set_common_properties( properties, profile, "consumer", service );
351 /** Register an object for clean up.
353 * \param ptr an opaque pointer to anything allocated on the heap
354 * \param destructor the function pointer of the deallocation subroutine (e.g., free or \p mlt_pool_release)
357 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
360 sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
361 mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
364 /** Close the factory.
366 * Cleanup all resources for the session.
369 void mlt_factory_close( )
371 if ( mlt_directory != NULL )
373 mlt_properties_close( event_object );
374 mlt_properties_close( global_properties );
376 mlt_repository_close( repository );
377 free( mlt_directory );
378 mlt_directory = NULL;
383 mlt_properties mlt_global_properties( )
385 return global_properties;