]> git.sesse.net Git - mlt/blob - src/framework/mlt_factory.c
A little debugging.
[mlt] / src / framework / mlt_factory.c
1 /**
2  * \file mlt_factory.c
3  * \brief the factory method interfaces
4  *
5  * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
6  * \author Charles Yates <charles.yates@pandora.be>
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "mlt.h"
24 #include "mlt_repository.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <locale.h>
30 #include <libgen.h>
31
32 #ifdef WIN32
33 #include <windows.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"
44 #endif
45
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;
56
57 /* Event transmitters. */
58
59 /** the -create-request event transmitter
60  *
61  * \param listener
62  * \param owner
63  * \param self
64  * \param args
65  */
66
67 static void mlt_factory_create_request( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
68 {
69         if ( listener != NULL )
70                 listener( owner, self, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service * )args[ 2 ] );
71 }
72
73 /** the -create-done event transmitter
74  *
75  * \param listener
76  * \param owner
77  * \param self
78  * \param args
79  */
80
81 static void mlt_factory_create_done( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
82 {
83         if ( listener != NULL )
84                 listener( owner, self, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
85 }
86
87 /** Construct the repository and factories.
88  *
89  * The environment variable MLT_PRODUCER is the name of a default producer often used by other services, defaults to "loader".
90  *
91  * The environment variable MLT_CONSUMER is the name of a default consumer, defaults to "sdl".
92  *
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).
94  *
95  * The environment variable MLT_DATA overrides the default full path to the MLT and module supplemental data files, defaults to \p PREFIX_DATA.
96  *
97  * The environment variable MLT_PROFILE defaults to "dv_pal."
98  *
99  * The environment variable MLT_REPOSITORY overrides the default location of the plugin modules, defaults to \p PREFIX_LIB.
100  *
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
104  */
105
106 mlt_repository mlt_factory_init( const char *directory )
107 {
108         // Load the system locales
109         setlocale( LC_ALL, "" );
110
111         if ( ! global_properties )
112                 global_properties = mlt_properties_new( );
113
114         // Allow property refresh on a subsequent initialisation
115         if ( global_properties )
116         {
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 );
123 #if defined(WIN32)
124                 char path[1024];
125                 DWORD size = sizeof( path );
126                 GetModuleFileName( NULL, path, size );
127 #elif defined(__DARWIN__)  && defined(RELOCATABLE)
128                 char path[1024];
129                 uint32_t size = sizeof( path );
130                 _NSGetExecutablePath( path, &size );
131 #endif
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 );
136                 free( path2 );
137 #endif
138         }
139
140         // Only initialise once
141         if ( mlt_directory == NULL )
142         {
143                 // Allow user over rides
144                 if ( directory == NULL || !strcmp( directory, "" ) )
145                         directory = getenv( "MLT_REPOSITORY" );
146
147                 // If no directory is specified, default to install directory
148                 if ( directory == NULL )
149                         directory = PREFIX_LIB;
150
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" ) )
156                 {
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 );
162                 }
163                 mlt_directory = calloc( 1, size + strlen( directory ) + 1 );
164                 strcpy( mlt_directory, exedir );
165                 strcat( mlt_directory, directory );
166 #else
167                 mlt_directory = strdup( directory );
168 #endif
169                 
170                 // Initialise the pool
171                 mlt_pool_init( );
172
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 );
184
185                 // Create the repository of services
186                 repository = mlt_repository_init( mlt_directory );
187
188                 // Force a clean up when app closes
189                 atexit( mlt_factory_close );
190         }
191
192         return repository;
193 }
194
195 /** Fetch the events object.
196  *
197  * \return the global factory event object
198  */
199
200 mlt_properties mlt_factory_event_object( )
201 {
202         return event_object;
203 }
204
205 /** Fetch the module directory used in this instance.
206  *
207  * \return the full path to the module directory that this session is using
208  */
209
210 const char *mlt_factory_directory( )
211 {
212         return mlt_directory;
213 }
214
215 /** Get a value from the environment.
216  *
217  * \param name the name of a MLT (runtime configuration) environment variable
218  * \return the value of the variable
219  */
220
221 char *mlt_environment( const char *name )
222 {
223         if ( global_properties )
224                 return mlt_properties_get( global_properties, name );
225         else
226                 return NULL;
227 }
228
229 /** Set a value in the environment.
230  *
231  * \param name the name of a MLT environment variable
232  * \param value the value of the variable
233  * \return true on error
234  */
235
236 int mlt_environment_set( const char *name, const char *value )
237 {
238         if ( global_properties )
239                 return mlt_properties_set( global_properties, name, value );
240         else
241                 return -1;
242 }
243
244 /** Set some properties common to all services.
245  *
246  * This sets _unique_id, \p mlt_type, \p mlt_service (unless _mlt_service_hidden), and _profile.
247  *
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
252  */
253
254 static void set_common_properties( mlt_properties properties, mlt_profile profile, const char *type, const char *service )
255 {
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 );
262 }
263
264 /** Fetch a producer from the repository.
265  *
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
270  */
271
272 mlt_producer mlt_factory_producer( mlt_profile profile, const char *service, const void *input )
273 {
274         mlt_producer obj = NULL;
275
276         // Pick up the default normalising producer if necessary
277         if ( service == NULL )
278                 service = mlt_environment( "MLT_PRODUCER" );
279
280         // Offer the application the chance to 'create'
281         mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
282
283         // Try to instantiate via the specified service
284         if ( obj == NULL )
285         {
286                 obj = mlt_repository_create( repository, profile, producer_type, service, input );
287                 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
288                 if ( obj != NULL )
289                 {
290                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
291                         set_common_properties( properties, profile, "producer", service );
292                 }
293         }
294         return obj;
295 }
296
297 /** Fetch a filter from the repository.
298  *
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
303  */
304
305 mlt_filter mlt_factory_filter( mlt_profile profile, const char *service, const void *input )
306 {
307         mlt_filter obj = NULL;
308
309         // Offer the application the chance to 'create'
310         mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
311
312         if ( obj == NULL )
313         {
314                 obj = mlt_repository_create( repository, profile, filter_type, service, input );
315                 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
316         }
317
318         if ( obj != NULL )
319         {
320                 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
321                 set_common_properties( properties, profile, "filter", service );
322         }
323         return obj;
324 }
325
326 /** Fetch a transition from the repository.
327  *
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
332  */
333
334 mlt_transition mlt_factory_transition( mlt_profile profile, const char *service, const void *input )
335 {
336         mlt_transition obj = NULL;
337
338         // Offer the application the chance to 'create'
339         mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
340
341         if ( obj == NULL )
342         {
343                 obj = mlt_repository_create( repository, profile, transition_type, service, input );
344                 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
345         }
346
347         if ( obj != NULL )
348         {
349                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
350                 set_common_properties( properties, profile, "transition", service );
351         }
352         return obj;
353 }
354
355 /** Fetch a consumer from the repository.
356  *
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
361  */
362
363 mlt_consumer mlt_factory_consumer( mlt_profile profile, const char *service, const void *input )
364 {
365         mlt_consumer obj = NULL;
366
367         if ( service == NULL )
368                 service = mlt_environment( "MLT_CONSUMER" );
369
370         // Offer the application the chance to 'create'
371         mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
372
373         if ( obj == NULL )
374         {
375                 obj = mlt_repository_create( repository, profile, consumer_type, service, input );
376                 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
377         }
378
379         if ( obj != NULL )
380         {
381                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
382                 set_common_properties( properties, profile, "consumer", service );
383         }
384         return obj;
385 }
386
387 /** Register an object for clean up.
388  *
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)
391  */
392
393 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
394 {
395         char unique[ 256 ];
396         sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
397         mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
398 }
399
400 /** Close the factory.
401  *
402  * Cleanup all resources for the session.
403  */
404
405 void mlt_factory_close( )
406 {
407         if ( mlt_directory != NULL )
408         {
409                 mlt_properties_close( event_object );
410                 event_object = NULL;
411 #if !defined(WIN32)
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;
416 #endif
417                 if ( repository )
418                 {
419                         mlt_repository_close( repository );
420                         repository = NULL;
421                 }
422                 free( mlt_directory );
423                 mlt_directory = NULL;
424                 mlt_pool_close( );
425         }
426 }
427
428 mlt_properties mlt_global_properties( )
429 {
430         return global_properties;
431 }