]> git.sesse.net Git - mlt/blob - src/framework/mlt_factory.c
Always Load the system locales.
[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
31 #ifdef WIN32
32 #include <windows.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"
37 #else
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"
42 #endif
43
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;
54
55 /* Event transmitters. */
56
57 /** the -create-request event transmitter
58  *
59  * \param listener
60  * \param owner
61  * \param self
62  * \param args
63  */
64
65 static void mlt_factory_create_request( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
66 {
67         if ( listener != NULL )
68                 listener( owner, self, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service * )args[ 2 ] );
69 }
70
71 /** the -create-done event transmitter
72  *
73  * \param listener
74  * \param owner
75  * \param self
76  * \param args
77  */
78
79 static void mlt_factory_create_done( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
80 {
81         if ( listener != NULL )
82                 listener( owner, self, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
83 }
84
85 /** Construct the repository and factories.
86  *
87  * The environment variable MLT_PRODUCER is the name of a default producer often used by other services, defaults to "loader".
88  *
89  * The environment variable MLT_CONSUMER is the name of a default consumer, defaults to "sdl".
90  *
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).
92  *
93  * The environment variable MLT_DATA overrides the default full path to the MLT and module supplemental data files, defaults to \p PREFIX_DATA.
94  *
95  * The environment variable MLT_PROFILE defaults to "dv_pal."
96  *
97  * The environment variable MLT_REPOSITORY overrides the default location of the plugin modules, defaults to \p PREFIX_LIB.
98  *
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
102  */
103
104 mlt_repository mlt_factory_init( const char *directory )
105 {
106         // Load the system locales
107         setlocale( LC_ALL, "" );
108
109         if ( ! global_properties )
110                 global_properties = mlt_properties_new( );
111
112         // Allow property refresh on a subsequent initialisation
113         if ( global_properties )
114         {
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" );
120
121                 mlt_properties_set_or_default( global_properties, "MLT_DATA", getenv( "MLT_DATA" ), PREFIX_DATA );
122         }
123
124         // Only initialise once
125         if ( mlt_directory == NULL )
126         {
127                 // Allow user over rides
128                 if ( directory == NULL || !strcmp( directory, "" ) )
129                         directory = getenv( "MLT_REPOSITORY" );
130
131                 // If no directory is specified, default to install directory
132                 if ( directory == NULL )
133                         directory = PREFIX_LIB;
134
135                 // Store the prefix for later retrieval
136                 mlt_directory = strdup( directory );
137
138                 // Initialise the pool
139                 mlt_pool_init( );
140
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 );
152
153                 // Create the repository of services
154                 repository = mlt_repository_init( directory );
155
156                 // Force a clean up when app closes
157                 atexit( mlt_factory_close );
158         }
159
160         return repository;
161 }
162
163 /** Fetch the events object.
164  *
165  * \return the global factory event object
166  */
167
168 mlt_properties mlt_factory_event_object( )
169 {
170         return event_object;
171 }
172
173 /** Fetch the module directory used in this instance.
174  *
175  * \return the full path to the module directory that this session is using
176  */
177
178 const char *mlt_factory_directory( )
179 {
180         return mlt_directory;
181 }
182
183 /** Get a value from the environment.
184  *
185  * \param name the name of a MLT (runtime configuration) environment variable
186  * \return the value of the variable
187  */
188
189 char *mlt_environment( const char *name )
190 {
191         if ( global_properties )
192                 return mlt_properties_get( global_properties, name );
193         else
194                 return NULL;
195 }
196
197 /** Set a value in the environment.
198  *
199  * \param name the name of a MLT environment variable
200  * \param value the value of the variable
201  * \return true on error
202  */
203
204 int mlt_environment_set( const char *name, const char *value )
205 {
206         if ( global_properties )
207                 return mlt_properties_set( global_properties, name, value );
208         else
209                 return -1;
210 }
211
212 /** Set some properties common to all services.
213  *
214  * This sets _unique_id, \p mlt_type, \p mlt_service (unless _mlt_service_hidden), and _profile.
215  *
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
220  */
221
222 static void set_common_properties( mlt_properties properties, mlt_profile profile, const char *type, const char *service )
223 {
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 );
230 }
231
232 /** Fetch a producer from the repository.
233  *
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
238  */
239
240 mlt_producer mlt_factory_producer( mlt_profile profile, const char *service, const void *input )
241 {
242         mlt_producer obj = NULL;
243
244         // Pick up the default normalising producer if necessary
245         if ( service == NULL )
246                 service = mlt_environment( "MLT_PRODUCER" );
247
248         // Offer the application the chance to 'create'
249         mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
250
251         // Try to instantiate via the specified service
252         if ( obj == NULL )
253         {
254                 obj = mlt_repository_create( repository, profile, producer_type, service, input );
255                 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
256                 if ( obj != NULL )
257                 {
258                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
259                         set_common_properties( properties, profile, "producer", service );
260                 }
261         }
262         return obj;
263 }
264
265 /** Fetch a filter from the repository.
266  *
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
271  */
272
273 mlt_filter mlt_factory_filter( mlt_profile profile, const char *service, const void *input )
274 {
275         mlt_filter obj = NULL;
276
277         // Offer the application the chance to 'create'
278         mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
279
280         if ( obj == NULL )
281         {
282                 obj = mlt_repository_create( repository, profile, filter_type, service, input );
283                 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
284         }
285
286         if ( obj != NULL )
287         {
288                 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
289                 set_common_properties( properties, profile, "filter", service );
290         }
291         return obj;
292 }
293
294 /** Fetch a transition from the repository.
295  *
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
300  */
301
302 mlt_transition mlt_factory_transition( mlt_profile profile, const char *service, const void *input )
303 {
304         mlt_transition obj = NULL;
305
306         // Offer the application the chance to 'create'
307         mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
308
309         if ( obj == NULL )
310         {
311                 obj = mlt_repository_create( repository, profile, transition_type, service, input );
312                 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
313         }
314
315         if ( obj != NULL )
316         {
317                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
318                 set_common_properties( properties, profile, "transition", service );
319         }
320         return obj;
321 }
322
323 /** Fetch a consumer from the repository.
324  *
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
329  */
330
331 mlt_consumer mlt_factory_consumer( mlt_profile profile, const char *service, const void *input )
332 {
333         mlt_consumer obj = NULL;
334
335         if ( service == NULL )
336                 service = mlt_environment( "MLT_CONSUMER" );
337
338         // Offer the application the chance to 'create'
339         mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
340
341         if ( obj == NULL )
342         {
343                 obj = mlt_repository_create( repository, profile, consumer_type, service, input );
344                 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
345         }
346
347         if ( obj != NULL )
348         {
349                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
350                 set_common_properties( properties, profile, "consumer", service );
351         }
352         return obj;
353 }
354
355 /** Register an object for clean up.
356  *
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)
359  */
360
361 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
362 {
363         char unique[ 256 ];
364         sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
365         mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
366 }
367
368 /** Close the factory.
369  *
370  * Cleanup all resources for the session.
371  */
372
373 void mlt_factory_close( )
374 {
375         if ( mlt_directory != NULL )
376         {
377                 mlt_properties_close( event_object );
378                 event_object = NULL;
379                 mlt_properties_close( global_properties );
380                 global_properties = NULL;
381                 if ( repository )
382                 {
383                         mlt_repository_close( repository );
384                         repository = NULL;
385                 }
386                 free( mlt_directory );
387                 mlt_directory = NULL;
388                 mlt_pool_close( );
389         }
390 }
391
392 mlt_properties mlt_global_properties( )
393 {
394         return global_properties;
395 }