]> git.sesse.net Git - mlt/blob - src/framework/mlt_factory.c
3d7759c5c22deccaeeeb576f54e1d332d50326d8
[mlt] / src / framework / mlt_factory.c
1 /*
2  * mlt_factory.c -- the factory method interfaces
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "mlt.h"
23 #include "mlt_repository.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /** Singleton repositories
30 */
31
32 static char *mlt_prefix = NULL;
33 static mlt_properties global_properties = NULL;
34 static mlt_properties object_list = NULL;
35 static mlt_repository producers = NULL;
36 static mlt_repository filters = NULL;
37 static mlt_repository transitions = NULL;
38 static mlt_repository consumers = NULL;
39 static mlt_properties event_object = NULL;
40 static int unique_id = 0;
41
42 /** Event transmitters.
43 */
44
45 static void mlt_factory_create_request( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
46 {
47         if ( listener != NULL )
48                 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service * )args[ 2 ] );
49 }
50
51 static void mlt_factory_create_done( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
52 {
53         if ( listener != NULL )
54                 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
55 }
56
57 /** Construct the factories.
58 */
59
60 int mlt_factory_init( const char *prefix )
61 {
62         // Only initialise once
63         if ( mlt_prefix == NULL )
64         {
65                 // Allow user over rides
66                 if ( prefix == NULL || !strcmp( prefix, "" ) )
67                         prefix = getenv( "MLT_REPOSITORY" );
68
69                 // If no directory is specified, default to install directory
70                 if ( prefix == NULL )
71                         prefix = PREFIX_DATA;
72
73                 // Store the prefix for later retrieval
74                 mlt_prefix = strdup( prefix );
75
76                 // Initialise the pool
77                 mlt_pool_init( );
78
79                 // Create and set up the events object
80                 event_object = mlt_properties_new( );
81                 mlt_events_init( event_object );
82                 mlt_events_register( event_object, "producer-create-request", ( mlt_transmitter )mlt_factory_create_request );
83                 mlt_events_register( event_object, "producer-create-done", ( mlt_transmitter )mlt_factory_create_done );
84                 mlt_events_register( event_object, "filter-create-request", ( mlt_transmitter )mlt_factory_create_request );
85                 mlt_events_register( event_object, "filter-create-done", ( mlt_transmitter )mlt_factory_create_done );
86                 mlt_events_register( event_object, "transition-create-request", ( mlt_transmitter )mlt_factory_create_request );
87                 mlt_events_register( event_object, "transition-create-done", ( mlt_transmitter )mlt_factory_create_done );
88                 mlt_events_register( event_object, "consumer-create-request", ( mlt_transmitter )mlt_factory_create_request );
89                 mlt_events_register( event_object, "consumer-create-done", ( mlt_transmitter )mlt_factory_create_done );
90
91                 // Create the global properties
92                 global_properties = mlt_properties_new( );
93
94                 // Create the object list.
95                 object_list = mlt_properties_new( );
96
97                 // Create a repository for each service type
98                 producers = mlt_repository_init( object_list, prefix, "producers", "mlt_create_producer" );
99                 filters = mlt_repository_init( object_list, prefix, "filters", "mlt_create_filter" );
100                 transitions = mlt_repository_init( object_list, prefix, "transitions", "mlt_create_transition" );
101                 consumers = mlt_repository_init( object_list, prefix, "consumers", "mlt_create_consumer" );
102
103                 // Force a clean up when app closes
104                 atexit( mlt_factory_close );
105         }
106
107         // Allow property refresh on a subsequent initialisation
108         if ( global_properties != NULL )
109         {
110                 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
111                 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "fezzik" );
112                 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
113                 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
114                 mlt_properties_set_or_default( global_properties, "MLT_PROFILE", getenv( "MLT_PROFILE" ), "dv_pal" );
115
116                 // Load the most appropriate profile
117                 // MLT_PROFILE preferred
118                 if ( getenv( "MLT_PROFILE" ) )
119                         mlt_profile_select( mlt_environment( "MLT_PROFILE" ) );
120                 // MLT_NORMALISATION backwards compatibility
121                 else if ( strcmp( mlt_environment( "MLT_NORMALISATION" ), "PAL" ) )
122                         mlt_profile_select( "dv_ntsc" );
123                 else
124                         mlt_profile_select( "dv_pal" );
125         }
126
127
128         return 0;
129 }
130
131 /** Fetch the events object.
132 */
133
134 mlt_properties mlt_factory_event_object( )
135 {
136         return event_object;
137 }
138
139 /** Fetch the prefix used in this instance.
140 */
141
142 const char *mlt_factory_prefix( )
143 {
144         return mlt_prefix;
145 }
146
147 /** Get a value from the environment.
148 */
149
150 char *mlt_environment( const char *name )
151 {
152         return mlt_properties_get( global_properties, name );
153 }
154
155 /** Set a value in the environment.
156 */
157
158 int mlt_environment_set( const char *name, const char *value )
159 {
160         return mlt_properties_set( global_properties, name, value );
161 }
162
163 /** Fetch a producer from the repository.
164 */
165
166 mlt_producer mlt_factory_producer( const char *service, void *input )
167 {
168         mlt_producer obj = NULL;
169
170         // Pick up the default normalising producer if necessary
171         if ( service == NULL )
172                 service = mlt_environment( "MLT_PRODUCER" );
173
174         // Offer the application the chance to 'create'
175         mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
176
177         // Try to instantiate via the specified service
178         if ( obj == NULL )
179         {
180                 obj = mlt_repository_fetch( producers, service, input );
181                 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
182                 if ( obj != NULL )
183                 {
184                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
185                         mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
186                         mlt_properties_set( properties, "mlt_type", "producer" );
187                         if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
188                                 mlt_properties_set( properties, "mlt_service", service );
189                 }
190         }
191         return obj;
192 }
193
194 /** Fetch a filter from the repository.
195 */
196
197 mlt_filter mlt_factory_filter( const char *service, void *input )
198 {
199         mlt_filter obj = NULL;
200
201         // Offer the application the chance to 'create'
202         mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
203
204         if ( obj == NULL )
205         {
206                 obj = mlt_repository_fetch( filters, service, input );
207                 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
208         }
209
210         if ( obj != NULL )
211         {
212                 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
213                 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
214                 mlt_properties_set( properties, "mlt_type", "filter" );
215                 mlt_properties_set( properties, "mlt_service", service );
216         }
217         return obj;
218 }
219
220 /** Fetch a transition from the repository.
221 */
222
223 mlt_transition mlt_factory_transition( const char *service, void *input )
224 {
225         mlt_transition obj = NULL;
226
227         // Offer the application the chance to 'create'
228         mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
229
230         if ( obj == NULL )
231         {
232                 obj = mlt_repository_fetch( transitions, service, input );
233                 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
234         }
235
236         if ( obj != NULL )
237         {
238                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
239                 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
240                 mlt_properties_set( properties, "mlt_type", "transition" );
241                 mlt_properties_set( properties, "mlt_service", service );
242         }
243         return obj;
244 }
245
246 /** Fetch a consumer from the repository
247 */
248
249 mlt_consumer mlt_factory_consumer( const char *service, void *input )
250 {
251         mlt_consumer obj = NULL;
252
253         if ( service == NULL )
254                 service = mlt_environment( "MLT_CONSUMER" );
255
256         // Offer the application the chance to 'create'
257         mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
258
259         if ( obj == NULL )
260         {
261                 obj = mlt_repository_fetch( consumers, service, input );
262                 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
263         }
264
265         if ( obj != NULL )
266         {
267                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
268                 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
269                 mlt_properties_set( properties, "mlt_type", "consumer" );
270                 mlt_properties_set( properties, "mlt_service", service );
271         }
272         return obj;
273 }
274
275 /** Register an object for clean up.
276 */
277
278 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
279 {
280         char unique[ 256 ];
281         sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
282         mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
283 }
284
285 /** Close the factory.
286 */
287
288 void mlt_factory_close( )
289 {
290         if ( mlt_prefix != NULL )
291         {
292                 mlt_properties_close( event_object );
293                 mlt_repository_close( producers );
294                 mlt_repository_close( filters );
295                 mlt_repository_close( transitions );
296                 mlt_repository_close( consumers );
297                 mlt_properties_close( global_properties );
298                 mlt_properties_close( object_list );
299                 free( mlt_prefix );
300                 mlt_prefix = NULL;
301                 mlt_pool_close( );
302                 mlt_profile_close();
303         }
304 }