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"
34 /** the default subdirectory of the libdir for holding modules (plugins) */
35 #define PREFIX_LIB "\\lib\\mlt"
36 /** the default subdirectory of the install prefix for holding module (plugin) data */
37 #define PREFIX_DATA "\\share\\mlt"
38 #elif defined(__DARWIN__) && defined(RELOCATABLE)
39 #include <mach-o/dyld.h>
40 /** the default subdirectory of the libdir for holding modules (plugins) */
41 #define PREFIX_LIB "/lib/mlt"
42 /** the default subdirectory of the install prefix for holding module (plugin) data */
43 #define PREFIX_DATA "/share/mlt"
46 /** holds the full path to the modules directory - initialized and retained for the entire session */
47 static char *mlt_directory = NULL;
48 /** a global properties list for holding environment config data and things needing session-oriented cleanup */
49 static mlt_properties global_properties = NULL;
50 /** the global repository singleton */
51 static mlt_repository repository = NULL;
52 /** the events object for the factory events */
53 static mlt_properties event_object = NULL;
54 /** for tracking the unique_id set on each constructed service */
55 static int unique_id = 0;
57 /* Event transmitters. */
59 /** the -create-request event transmitter
67 static void mlt_factory_create_request( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
69 if ( listener != NULL )
70 listener( owner, self, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service * )args[ 2 ] );
73 /** the -create-done event transmitter
81 static void mlt_factory_create_done( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
83 if ( listener != NULL )
84 listener( owner, self, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
87 /** Construct the repository and factories.
89 * The environment variable MLT_PRODUCER is the name of a default producer often used by other services, defaults to "loader".
91 * The environment variable MLT_CONSUMER is the name of a default consumer, defaults to "sdl".
93 * The environment variable MLT_TEST_CARD is the name of a producer or file to be played when nothing is available (all tracks blank).
95 * The environment variable MLT_DATA overrides the default full path to the MLT and module supplemental data files, defaults to \p PREFIX_DATA.
97 * The environment variable MLT_PROFILE defaults to "dv_pal."
99 * The environment variable MLT_REPOSITORY overrides the default location of the plugin modules, defaults to \p PREFIX_LIB.
101 * \param directory an optional full path to a directory containing the modules that overrides the default and
102 * the MLT_REPOSITORY environment variable
103 * \return the repository
106 mlt_repository mlt_factory_init( const char *directory )
108 // Load the system locales
109 setlocale( LC_ALL, "" );
111 if ( ! global_properties )
112 global_properties = mlt_properties_new( );
114 // Allow property refresh on a subsequent initialisation
115 if ( global_properties )
117 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
118 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "loader" );
119 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
120 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
121 mlt_properties_set_or_default( global_properties, "MLT_PROFILE", getenv( "MLT_PROFILE" ), "dv_pal" );
122 mlt_properties_set_or_default( global_properties, "MLT_DATA", getenv( "MLT_DATA" ), PREFIX_DATA );
125 DWORD size = sizeof( path );
126 GetModuleFileName( NULL, path, size );
127 #elif defined(__DARWIN__) && defined(RELOCATABLE)
129 uint32_t size = sizeof( path );
130 _NSGetExecutablePath( path, &size );
132 #if defined(WIN32) || (defined(__DARWIN__) && defined(RELOCATABLE))
133 char *path2 = strdup( path );
134 char *appdir = dirname( path2 );
135 mlt_properties_set( global_properties, "MLT_APPDIR", appdir );
140 // Only initialise once
141 if ( mlt_directory == NULL )
143 // Allow user over rides
144 if ( directory == NULL || !strcmp( directory, "" ) )
145 directory = getenv( "MLT_REPOSITORY" );
147 // If no directory is specified, default to install directory
148 if ( directory == NULL )
149 directory = PREFIX_LIB;
151 // Store the prefix for later retrieval
152 #if defined(WIN32) || (defined(__DARWIN__) && defined(RELOCATABLE))
153 char *exedir = mlt_environment( "MLT_APPDIR" );
154 size_t size = strlen( exedir );
155 if ( global_properties && !getenv( "MLT_DATA" ) )
157 mlt_directory = calloc( 1, size + strlen( PREFIX_DATA ) + 1 );
158 strcpy( mlt_directory, exedir );
159 strcat( mlt_directory, PREFIX_DATA );
160 mlt_properties_set( global_properties, "MLT_DATA", mlt_directory );
161 free( mlt_directory );
163 mlt_directory = calloc( 1, size + strlen( directory ) + 1 );
164 strcpy( mlt_directory, exedir );
165 strcat( mlt_directory, directory );
167 mlt_directory = strdup( directory );
170 // Initialise the pool
173 // Create and set up the events object
174 event_object = mlt_properties_new( );
175 mlt_events_init( event_object );
176 mlt_events_register( event_object, "producer-create-request", ( mlt_transmitter )mlt_factory_create_request );
177 mlt_events_register( event_object, "producer-create-done", ( mlt_transmitter )mlt_factory_create_done );
178 mlt_events_register( event_object, "filter-create-request", ( mlt_transmitter )mlt_factory_create_request );
179 mlt_events_register( event_object, "filter-create-done", ( mlt_transmitter )mlt_factory_create_done );
180 mlt_events_register( event_object, "transition-create-request", ( mlt_transmitter )mlt_factory_create_request );
181 mlt_events_register( event_object, "transition-create-done", ( mlt_transmitter )mlt_factory_create_done );
182 mlt_events_register( event_object, "consumer-create-request", ( mlt_transmitter )mlt_factory_create_request );
183 mlt_events_register( event_object, "consumer-create-done", ( mlt_transmitter )mlt_factory_create_done );
185 // Create the repository of services
186 repository = mlt_repository_init( mlt_directory );
188 // Force a clean up when app closes
189 atexit( mlt_factory_close );
195 /** Fetch the events object.
197 * \return the global factory event object
200 mlt_properties mlt_factory_event_object( )
205 /** Fetch the module directory used in this instance.
207 * \return the full path to the module directory that this session is using
210 const char *mlt_factory_directory( )
212 return mlt_directory;
215 /** Get a value from the environment.
217 * \param name the name of a MLT (runtime configuration) environment variable
218 * \return the value of the variable
221 char *mlt_environment( const char *name )
223 if ( global_properties )
224 return mlt_properties_get( global_properties, name );
229 /** Set a value in the environment.
231 * \param name the name of a MLT environment variable
232 * \param value the value of the variable
233 * \return true on error
236 int mlt_environment_set( const char *name, const char *value )
238 if ( global_properties )
239 return mlt_properties_set( global_properties, name, value );
244 /** Set some properties common to all services.
246 * This sets _unique_id, \p mlt_type, \p mlt_service (unless _mlt_service_hidden), and _profile.
248 * \param properties a service's properties list
249 * \param profile the \p mlt_profile supplied to the factory function
250 * \param type the MLT service class
251 * \param service the name of the service
254 static void set_common_properties( mlt_properties properties, mlt_profile profile, const char *type, const char *service )
256 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
257 mlt_properties_set( properties, "mlt_type", type );
258 if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
259 mlt_properties_set( properties, "mlt_service", service );
260 if ( profile != NULL )
261 mlt_properties_set_data( properties, "_profile", profile, 0, NULL, NULL );
264 /** Fetch a producer from the repository.
266 * \param profile the \p mlt_profile to use
267 * \param service the name of the producer (optional, defaults to MLT_PRODUCER)
268 * \param input an optional argument to the producer constructor, typically a string
269 * \return a new producer
272 mlt_producer mlt_factory_producer( mlt_profile profile, const char *service, const void *input )
274 mlt_producer obj = NULL;
276 // Pick up the default normalising producer if necessary
277 if ( service == NULL )
278 service = mlt_environment( "MLT_PRODUCER" );
280 // Offer the application the chance to 'create'
281 mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
283 // Try to instantiate via the specified service
286 obj = mlt_repository_create( repository, profile, producer_type, service, input );
287 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
290 mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
291 set_common_properties( properties, profile, "producer", service );
297 /** Fetch a filter from the repository.
299 * \param profile the \p mlt_profile to use
300 * \param service the name of the filter
301 * \param input an optional argument to the filter constructor, typically a string
302 * \return a new filter
305 mlt_filter mlt_factory_filter( mlt_profile profile, const char *service, const void *input )
307 mlt_filter obj = NULL;
309 // Offer the application the chance to 'create'
310 mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
314 obj = mlt_repository_create( repository, profile, filter_type, service, input );
315 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
320 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
321 set_common_properties( properties, profile, "filter", service );
326 /** Fetch a transition from the repository.
328 * \param profile the \p mlt_profile to use
329 * \param service the name of the transition
330 * \param input an optional argument to the transition constructor, typically a string
331 * \return a new transition
334 mlt_transition mlt_factory_transition( mlt_profile profile, const char *service, const void *input )
336 mlt_transition obj = NULL;
338 // Offer the application the chance to 'create'
339 mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
343 obj = mlt_repository_create( repository, profile, transition_type, service, input );
344 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
349 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
350 set_common_properties( properties, profile, "transition", service );
355 /** Fetch a consumer from the repository.
357 * \param profile the \p mlt_profile to use
358 * \param service the name of the consumer (optional, defaults to MLT_CONSUMER)
359 * \param input an optional argument to the consumer constructor, typically a string
360 * \return a new consumer
363 mlt_consumer mlt_factory_consumer( mlt_profile profile, const char *service, const void *input )
365 mlt_consumer obj = NULL;
367 if ( service == NULL )
368 service = mlt_environment( "MLT_CONSUMER" );
370 // Offer the application the chance to 'create'
371 mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
375 obj = mlt_repository_create( repository, profile, consumer_type, service, input );
376 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
381 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
382 set_common_properties( properties, profile, "consumer", service );
387 /** Register an object for clean up.
389 * \param ptr an opaque pointer to anything allocated on the heap
390 * \param destructor the function pointer of the deallocation subroutine (e.g., free or \p mlt_pool_release)
393 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
396 sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
397 mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
400 /** Close the factory.
402 * Cleanup all resources for the session.
405 void mlt_factory_close( )
407 if ( mlt_directory != NULL )
409 mlt_properties_close( event_object );
412 // XXX something in here is causing Shotcut/Win32 to not exit completely
413 // under certain conditions: e.g. play a playlist.
414 mlt_properties_close( global_properties );
415 global_properties = NULL;
419 mlt_repository_close( repository );
422 free( mlt_directory );
423 mlt_directory = NULL;
428 mlt_properties mlt_global_properties( )
430 return global_properties;