Constness changes
[melted] / 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 "fezzil".
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 // Only initialise once
99 if ( mlt_directory == NULL )
100 {
101 // Allow user over rides
102 if ( directory == NULL || !strcmp( directory, "" ) )
103 directory = getenv( "MLT_REPOSITORY" );
104
105 // If no directory is specified, default to install directory
106 if ( directory == NULL )
107 directory = PREFIX_LIB;
108
109 // Store the prefix for later retrieval
110 mlt_directory = strdup( directory );
111
112 // Initialise the pool
113 mlt_pool_init( );
114
115 // Create and set up the events object
116 event_object = mlt_properties_new( );
117 mlt_events_init( event_object );
118 mlt_events_register( event_object, "producer-create-request", ( mlt_transmitter )mlt_factory_create_request );
119 mlt_events_register( event_object, "producer-create-done", ( mlt_transmitter )mlt_factory_create_done );
120 mlt_events_register( event_object, "filter-create-request", ( mlt_transmitter )mlt_factory_create_request );
121 mlt_events_register( event_object, "filter-create-done", ( mlt_transmitter )mlt_factory_create_done );
122 mlt_events_register( event_object, "transition-create-request", ( mlt_transmitter )mlt_factory_create_request );
123 mlt_events_register( event_object, "transition-create-done", ( mlt_transmitter )mlt_factory_create_done );
124 mlt_events_register( event_object, "consumer-create-request", ( mlt_transmitter )mlt_factory_create_request );
125 mlt_events_register( event_object, "consumer-create-done", ( mlt_transmitter )mlt_factory_create_done );
126
127 // Create the global properties
128 global_properties = mlt_properties_new( );
129
130 // Create the repository of services
131 repository = mlt_repository_init( directory );
132
133 // Force a clean up when app closes
134 atexit( mlt_factory_close );
135 }
136
137 // Allow property refresh on a subsequent initialisation
138 if ( global_properties != NULL )
139 {
140 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
141 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "fezzik" );
142 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
143 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
144 mlt_properties_set_or_default( global_properties, "MLT_PROFILE", getenv( "MLT_PROFILE" ), "dv_pal" );
145 mlt_properties_set_or_default( global_properties, "MLT_DATA", getenv( "MLT_DATA" ), PREFIX_DATA );
146 }
147
148
149 return repository;
150 }
151
152 /** Fetch the events object.
153 *
154 * \return the global factory event object
155 */
156
157 mlt_properties mlt_factory_event_object( )
158 {
159 return event_object;
160 }
161
162 /** Fetch the module directory used in this instance.
163 *
164 * \return the full path to the module directory that this session is using
165 */
166
167 const char *mlt_factory_directory( )
168 {
169 return mlt_directory;
170 }
171
172 /** Get a value from the environment.
173 *
174 * \param name the name of a MLT (runtime configuration) environment variable
175 * \return the value of the variable
176 */
177
178 char *mlt_environment( const char *name )
179 {
180 if ( global_properties )
181 return mlt_properties_get( global_properties, name );
182 else
183 return NULL;
184 }
185
186 /** Set a value in the environment.
187 *
188 * \param name the name of a MLT environment variable
189 * \param value the value of the variable
190 * \return true on error
191 */
192
193 int mlt_environment_set( const char *name, const char *value )
194 {
195 if ( global_properties )
196 return mlt_properties_set( global_properties, name, value );
197 else
198 return -1;
199 }
200
201 /** Set some properties common to all services.
202 *
203 * This sets _unique_id, \p mlt_type, \p mlt_service (unless _mlt_service_hidden), and _profile.
204 *
205 * \param properties a service's properties list
206 * \param profile the \p mlt_profile supplied to the factory function
207 * \param type the MLT service class
208 * \param service the name of the service
209 */
210
211 static void set_common_properties( mlt_properties properties, mlt_profile profile, const char *type, const char *service )
212 {
213 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
214 mlt_properties_set( properties, "mlt_type", type );
215 if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
216 mlt_properties_set( properties, "mlt_service", service );
217 if ( profile != NULL )
218 mlt_properties_set_data( properties, "_profile", profile, 0, NULL, NULL );
219 }
220
221 /** Fetch a producer from the repository.
222 *
223 * \param profile the \p mlt_profile to use
224 * \param service the name of the producer (optional, defaults to MLT_PRODUCER)
225 * \param input an optional argument to the producer constructor, typically a string
226 * \return a new producer
227 */
228
229 mlt_producer mlt_factory_producer( mlt_profile profile, const char *service, const void *input )
230 {
231 mlt_producer obj = NULL;
232
233 // Pick up the default normalising producer if necessary
234 if ( service == NULL )
235 service = mlt_environment( "MLT_PRODUCER" );
236
237 // Offer the application the chance to 'create'
238 mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
239
240 // Try to instantiate via the specified service
241 if ( obj == NULL )
242 {
243 obj = mlt_repository_create( repository, profile, producer_type, service, input );
244 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
245 if ( obj != NULL )
246 {
247 mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
248 set_common_properties( properties, profile, "producer", service );
249 }
250 }
251 return obj;
252 }
253
254 /** Fetch a filter from the repository.
255 *
256 * \param profile the \p mlt_profile to use
257 * \param service the name of the filter
258 * \param input an optional argument to the filter constructor, typically a string
259 * \return a new filter
260 */
261
262 mlt_filter mlt_factory_filter( mlt_profile profile, const char *service, const void *input )
263 {
264 mlt_filter obj = NULL;
265
266 // Offer the application the chance to 'create'
267 mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
268
269 if ( obj == NULL )
270 {
271 obj = mlt_repository_create( repository, profile, filter_type, service, input );
272 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
273 }
274
275 if ( obj != NULL )
276 {
277 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
278 set_common_properties( properties, profile, "filter", service );
279 }
280 return obj;
281 }
282
283 /** Fetch a transition from the repository.
284 *
285 * \param profile the \p mlt_profile to use
286 * \param service the name of the transition
287 * \param input an optional argument to the transition constructor, typically a string
288 * \return a new transition
289 */
290
291 mlt_transition mlt_factory_transition( mlt_profile profile, const char *service, const void *input )
292 {
293 mlt_transition obj = NULL;
294
295 // Offer the application the chance to 'create'
296 mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
297
298 if ( obj == NULL )
299 {
300 obj = mlt_repository_create( repository, profile, transition_type, service, input );
301 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
302 }
303
304 if ( obj != NULL )
305 {
306 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
307 set_common_properties( properties, profile, "transition", service );
308 }
309 return obj;
310 }
311
312 /** Fetch a consumer from the repository.
313 *
314 * \param profile the \p mlt_profile to use
315 * \param service the name of the consumer (optional, defaults to MLT_CONSUMER)
316 * \param input an optional argument to the consumer constructor, typically a string
317 * \return a new consumer
318 */
319
320 mlt_consumer mlt_factory_consumer( mlt_profile profile, const char *service, const void *input )
321 {
322 mlt_consumer obj = NULL;
323
324 if ( service == NULL )
325 service = mlt_environment( "MLT_CONSUMER" );
326
327 // Offer the application the chance to 'create'
328 mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
329
330 if ( obj == NULL )
331 {
332 obj = mlt_repository_create( repository, profile, consumer_type, service, input );
333 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
334 }
335
336 if ( obj != NULL )
337 {
338 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
339 set_common_properties( properties, profile, "consumer", service );
340 }
341 return obj;
342 }
343
344 /** Register an object for clean up.
345 *
346 * \param ptr an opaque pointer to anything allocated on the heap
347 * \param destructor the function pointer of the deallocation subroutine (e.g., free or \p mlt_pool_release)
348 */
349
350 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
351 {
352 char unique[ 256 ];
353 sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
354 mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
355 }
356
357 /** Close the factory.
358 *
359 * Cleanup all resources for the session.
360 */
361
362 void mlt_factory_close( )
363 {
364 if ( mlt_directory != NULL )
365 {
366 mlt_properties_close( event_object );
367 mlt_properties_close( global_properties );
368 mlt_repository_close( repository );
369 free( mlt_directory );
370 mlt_directory = NULL;
371 mlt_pool_close( );
372 }
373 }
374
375 mlt_properties mlt_global_properties( )
376 {
377 return global_properties;
378 }