]> git.sesse.net Git - mlt/blob - src/framework/mlt_factory.c
Change -1 handling for in/out points to be any negative value.
[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 /** the default subdirectory of the libdir for holding modules (plugins) */
31 #define PREFIX_LIB LIBDIR "/mlt"
32 /** the default subdirectory of the install prefix for holding module (plugin) data */
33 #define PREFIX_DATA PREFIX "/share/mlt"
34
35
36 /** holds the full path to the modules directory - initialized and retained for the entire session */
37 static char *mlt_directory = NULL;
38 /** a global properties list for holding environment config data and things needing session-oriented cleanup */
39 static mlt_properties global_properties = NULL;
40 /** the global repository singleton */
41 static mlt_repository repository = NULL;
42 /** the events object for the factory events */
43 static mlt_properties event_object = NULL;
44 /** for tracking the unique_id set on each constructed service */
45 static int unique_id = 0;
46
47 /* Event transmitters. */
48
49 /** the -create-request event transmitter
50  *
51  * \param listener
52  * \param owner
53  * \param this
54  * \param args
55  */
56
57 static void mlt_factory_create_request( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
58 {
59         if ( listener != NULL )
60                 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service * )args[ 2 ] );
61 }
62
63 /** the -create-done event transmitter
64  *
65  * \param listener
66  * \param owner
67  * \param this
68  * \param args
69  */
70
71 static void mlt_factory_create_done( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
72 {
73         if ( listener != NULL )
74                 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
75 }
76
77 /** Construct the repository and factories.
78  *
79  * The environment variable MLT_PRODUCER is the name of a default producer often used by other services, defaults to "loader".
80  *
81  * The environment variable MLT_CONSUMER is the name of a default consumer, defaults to "sdl".
82  *
83  * The environment variable MLT_TEST_CARD is the name of a producer or file to be played when nothing is available (all tracks blank).
84  *
85  * The environment variable MLT_DATA overrides the default full path to the MLT and module supplemental data files, defaults to \p PREFIX_DATA.
86  *
87  * The environment variable MLT_PROFILE defaults to "dv_pal."
88  *
89  * The environment variable MLT_REPOSITORY overrides the default location of the plugin modules, defaults to \p PREFIX_LIB.
90  *
91  * \param directory an optional full path to a directory containing the modules that overrides the default and
92  * the MLT_REPOSITORY environment variable
93  * \return the repository
94  */
95
96 mlt_repository mlt_factory_init( const char *directory )
97 {
98         if ( ! global_properties )
99                 global_properties = mlt_properties_new( );
100
101         // Allow property refresh on a subsequent initialisation
102         if ( global_properties )
103         {
104                 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
105                 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "loader" );
106                 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
107                 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
108                 mlt_properties_set_or_default( global_properties, "MLT_PROFILE", getenv( "MLT_PROFILE" ), "dv_pal" );
109                 mlt_properties_set_or_default( global_properties, "MLT_DATA", getenv( "MLT_DATA" ), PREFIX_DATA );
110         }
111
112         // Only initialise once
113         if ( mlt_directory == NULL )
114         {
115                 // Allow user over rides
116                 if ( directory == NULL || !strcmp( directory, "" ) )
117                         directory = getenv( "MLT_REPOSITORY" );
118
119                 // If no directory is specified, default to install directory
120                 if ( directory == NULL )
121                         directory = PREFIX_LIB;
122
123                 // Store the prefix for later retrieval
124                 mlt_directory = strdup( directory );
125
126                 // Initialise the pool
127                 mlt_pool_init( );
128
129                 // Create and set up the events object
130                 event_object = mlt_properties_new( );
131                 mlt_events_init( event_object );
132                 mlt_events_register( event_object, "producer-create-request", ( mlt_transmitter )mlt_factory_create_request );
133                 mlt_events_register( event_object, "producer-create-done", ( mlt_transmitter )mlt_factory_create_done );
134                 mlt_events_register( event_object, "filter-create-request", ( mlt_transmitter )mlt_factory_create_request );
135                 mlt_events_register( event_object, "filter-create-done", ( mlt_transmitter )mlt_factory_create_done );
136                 mlt_events_register( event_object, "transition-create-request", ( mlt_transmitter )mlt_factory_create_request );
137                 mlt_events_register( event_object, "transition-create-done", ( mlt_transmitter )mlt_factory_create_done );
138                 mlt_events_register( event_object, "consumer-create-request", ( mlt_transmitter )mlt_factory_create_request );
139                 mlt_events_register( event_object, "consumer-create-done", ( mlt_transmitter )mlt_factory_create_done );
140
141                 // Create the repository of services
142                 repository = mlt_repository_init( directory );
143
144                 // Force a clean up when app closes
145                 atexit( mlt_factory_close );
146         }
147
148         return repository;
149 }
150
151 /** Fetch the events object.
152  *
153  * \return the global factory event object
154  */
155
156 mlt_properties mlt_factory_event_object( )
157 {
158         return event_object;
159 }
160
161 /** Fetch the module directory used in this instance.
162  *
163  * \return the full path to the module directory that this session is using
164  */
165
166 const char *mlt_factory_directory( )
167 {
168         return mlt_directory;
169 }
170
171 /** Get a value from the environment.
172  *
173  * \param name the name of a MLT (runtime configuration) environment variable
174  * \return the value of the variable
175  */
176
177 char *mlt_environment( const char *name )
178 {
179         if ( global_properties )
180                 return mlt_properties_get( global_properties, name );
181         else
182                 return NULL;
183 }
184
185 /** Set a value in the environment.
186  *
187  * \param name the name of a MLT environment variable
188  * \param value the value of the variable
189  * \return true on error
190  */
191
192 int mlt_environment_set( const char *name, const char *value )
193 {
194         if ( global_properties )
195                 return mlt_properties_set( global_properties, name, value );
196         else
197                 return -1;
198 }
199
200 /** Set some properties common to all services.
201  *
202  * This sets _unique_id, \p mlt_type, \p mlt_service (unless _mlt_service_hidden), and _profile.
203  *
204  * \param properties a service's properties list
205  * \param profile the \p mlt_profile supplied to the factory function
206  * \param type the MLT service class
207  * \param service the name of the service
208  */
209
210 static void set_common_properties( mlt_properties properties, mlt_profile profile, const char *type, const char *service )
211 {
212         mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
213         mlt_properties_set( properties, "mlt_type", type );
214         if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
215                 mlt_properties_set( properties, "mlt_service", service );
216         if ( profile != NULL )
217                 mlt_properties_set_data( properties, "_profile", profile, 0, NULL, NULL );
218 }
219
220 /** Fetch a producer from the repository.
221  *
222  * \param profile the \p mlt_profile to use
223  * \param service the name of the producer (optional, defaults to MLT_PRODUCER)
224  * \param input an optional argument to the producer constructor, typically a string
225  * \return a new producer
226  */
227
228 mlt_producer mlt_factory_producer( mlt_profile profile, const char *service, const void *input )
229 {
230         mlt_producer obj = NULL;
231
232         // Pick up the default normalising producer if necessary
233         if ( service == NULL )
234                 service = mlt_environment( "MLT_PRODUCER" );
235
236         // Offer the application the chance to 'create'
237         mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
238
239         // Try to instantiate via the specified service
240         if ( obj == NULL )
241         {
242                 obj = mlt_repository_create( repository, profile, producer_type, service, input );
243                 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
244                 if ( obj != NULL )
245                 {
246                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
247                         set_common_properties( properties, profile, "producer", service );
248                 }
249         }
250         return obj;
251 }
252
253 /** Fetch a filter from the repository.
254  *
255  * \param profile the \p mlt_profile to use
256  * \param service the name of the filter
257  * \param input an optional argument to the filter constructor, typically a string
258  * \return a new filter
259  */
260
261 mlt_filter mlt_factory_filter( mlt_profile profile, const char *service, const void *input )
262 {
263         mlt_filter obj = NULL;
264
265         // Offer the application the chance to 'create'
266         mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
267
268         if ( obj == NULL )
269         {
270                 obj = mlt_repository_create( repository, profile, filter_type, service, input );
271                 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
272         }
273
274         if ( obj != NULL )
275         {
276                 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
277                 set_common_properties( properties, profile, "filter", service );
278         }
279         return obj;
280 }
281
282 /** Fetch a transition from the repository.
283  *
284  * \param profile the \p mlt_profile to use
285  * \param service the name of the transition
286  * \param input an optional argument to the transition constructor, typically a string
287  * \return a new transition
288  */
289
290 mlt_transition mlt_factory_transition( mlt_profile profile, const char *service, const void *input )
291 {
292         mlt_transition obj = NULL;
293
294         // Offer the application the chance to 'create'
295         mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
296
297         if ( obj == NULL )
298         {
299                 obj = mlt_repository_create( repository, profile, transition_type, service, input );
300                 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
301         }
302
303         if ( obj != NULL )
304         {
305                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
306                 set_common_properties( properties, profile, "transition", service );
307         }
308         return obj;
309 }
310
311 /** Fetch a consumer from the repository.
312  *
313  * \param profile the \p mlt_profile to use
314  * \param service the name of the consumer (optional, defaults to MLT_CONSUMER)
315  * \param input an optional argument to the consumer constructor, typically a string
316  * \return a new consumer
317  */
318
319 mlt_consumer mlt_factory_consumer( mlt_profile profile, const char *service, const void *input )
320 {
321         mlt_consumer obj = NULL;
322
323         if ( service == NULL )
324                 service = mlt_environment( "MLT_CONSUMER" );
325
326         // Offer the application the chance to 'create'
327         mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
328
329         if ( obj == NULL )
330         {
331                 obj = mlt_repository_create( repository, profile, consumer_type, service, input );
332                 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
333         }
334
335         if ( obj != NULL )
336         {
337                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
338                 set_common_properties( properties, profile, "consumer", service );
339         }
340         return obj;
341 }
342
343 /** Register an object for clean up.
344  *
345  * \param ptr an opaque pointer to anything allocated on the heap
346  * \param destructor the function pointer of the deallocation subroutine (e.g., free or \p mlt_pool_release)
347  */
348
349 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
350 {
351         char unique[ 256 ];
352         sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
353         mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
354 }
355
356 /** Close the factory.
357  *
358  * Cleanup all resources for the session.
359  */
360
361 void mlt_factory_close( )
362 {
363         if ( mlt_directory != NULL )
364         {
365                 mlt_properties_close( event_object );
366                 mlt_properties_close( global_properties );
367                 mlt_repository_close( repository );
368                 free( mlt_directory );
369                 mlt_directory = NULL;
370                 mlt_pool_close( );
371         }
372 }
373
374 mlt_properties mlt_global_properties( )
375 {
376         return global_properties;
377 }