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