]> git.sesse.net Git - mlt/blob - src/framework/mlt_factory.c
d7c6f000254dccf53b7324b84057aa619d3bf6b6
[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 "mlt.h"
22 #include "mlt_repository.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define PREFIX_LIB LIBDIR "/mlt"
29 #define PREFIX_DATA PREFIX "/share/mlt"
30
31 /** Singleton repositories
32 */
33
34 static char *mlt_directory = NULL;
35 static mlt_properties global_properties = NULL;
36 static mlt_repository repository = NULL;
37 static mlt_properties event_object = NULL;
38 static int unique_id = 0;
39
40 /** Event transmitters.
41 */
42
43 static void mlt_factory_create_request( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
44 {
45         if ( listener != NULL )
46                 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service * )args[ 2 ] );
47 }
48
49 static void mlt_factory_create_done( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
50 {
51         if ( listener != NULL )
52                 listener( owner, this, ( char * )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
53 }
54
55 /** Construct the factories.
56 */
57
58 mlt_repository mlt_factory_init( const char *directory )
59 {
60         // Only initialise once
61         if ( mlt_directory == NULL )
62         {
63                 // Allow user over rides
64                 if ( directory == NULL || !strcmp( directory, "" ) )
65                         directory = getenv( "MLT_REPOSITORY" );
66
67                 // If no directory is specified, default to install directory
68                 if ( directory == NULL )
69                         directory = PREFIX_LIB;
70
71                 // Store the prefix for later retrieval
72                 mlt_directory = strdup( directory );
73
74                 // Initialise the pool
75                 mlt_pool_init( );
76
77                 // Create and set up the events object
78                 event_object = mlt_properties_new( );
79                 mlt_events_init( event_object );
80                 mlt_events_register( event_object, "producer-create-request", ( mlt_transmitter )mlt_factory_create_request );
81                 mlt_events_register( event_object, "producer-create-done", ( mlt_transmitter )mlt_factory_create_done );
82                 mlt_events_register( event_object, "filter-create-request", ( mlt_transmitter )mlt_factory_create_request );
83                 mlt_events_register( event_object, "filter-create-done", ( mlt_transmitter )mlt_factory_create_done );
84                 mlt_events_register( event_object, "transition-create-request", ( mlt_transmitter )mlt_factory_create_request );
85                 mlt_events_register( event_object, "transition-create-done", ( mlt_transmitter )mlt_factory_create_done );
86                 mlt_events_register( event_object, "consumer-create-request", ( mlt_transmitter )mlt_factory_create_request );
87                 mlt_events_register( event_object, "consumer-create-done", ( mlt_transmitter )mlt_factory_create_done );
88
89                 // Create the global properties
90                 global_properties = mlt_properties_new( );
91
92                 // Create the repository of services
93                 repository = mlt_repository_init( directory );
94
95                 // Force a clean up when app closes
96                 atexit( mlt_factory_close );
97         }
98
99         // Allow property refresh on a subsequent initialisation
100         if ( global_properties != NULL )
101         {
102                 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
103                 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "fezzik" );
104                 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
105                 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
106                 mlt_properties_set_or_default( global_properties, "MLT_PROFILE", getenv( "MLT_PROFILE" ), "dv_pal" );
107                 mlt_properties_set_or_default( global_properties, "MLT_DATA", getenv( "MLT_DATA" ), PREFIX_DATA );
108         }
109
110
111         return repository;
112 }
113
114 /** Fetch the events object.
115 */
116
117 mlt_properties mlt_factory_event_object( )
118 {
119         return event_object;
120 }
121
122 /** Fetch the module directory used in this instance.
123 */
124
125 const char *mlt_factory_directory( )
126 {
127         return mlt_directory;
128 }
129
130 /** Get a value from the environment.
131 */
132
133 char *mlt_environment( const char *name )
134 {
135         if ( global_properties )
136                 return mlt_properties_get( global_properties, name );
137         else
138                 return NULL;
139 }
140
141 /** Set a value in the environment.
142 */
143
144 int mlt_environment_set( const char *name, const char *value )
145 {
146         if ( global_properties )
147                 return mlt_properties_set( global_properties, name, value );
148         else
149                 return -1;
150 }
151
152 static void set_common_properties( mlt_properties properties, mlt_profile profile, const char *type, const char *service )
153 {
154         mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
155         mlt_properties_set( properties, "mlt_type", type );
156         if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
157                 mlt_properties_set( properties, "mlt_service", service );
158         if ( profile != NULL )
159                 mlt_properties_set_data( properties, "_profile", profile, 0, NULL, NULL );
160 }
161
162 /** Fetch a producer from the repository.
163 */
164
165 mlt_producer mlt_factory_producer( mlt_profile profile, const char *service, void *input )
166 {
167         mlt_producer obj = NULL;
168
169         // Pick up the default normalising producer if necessary
170         if ( service == NULL )
171                 service = mlt_environment( "MLT_PRODUCER" );
172
173         // Offer the application the chance to 'create'
174         mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
175
176         // Try to instantiate via the specified service
177         if ( obj == NULL )
178         {
179                 obj = mlt_repository_create( repository, profile, producer_type, service, input );
180                 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
181                 if ( obj != NULL )
182                 {
183                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
184                         set_common_properties( properties, profile, "producer", service );
185                 }
186         }
187         return obj;
188 }
189
190 /** Fetch a filter from the repository.
191 */
192
193 mlt_filter mlt_factory_filter( mlt_profile profile, const char *service, void *input )
194 {
195         mlt_filter obj = NULL;
196
197         // Offer the application the chance to 'create'
198         mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
199
200         if ( obj == NULL )
201         {
202                 obj = mlt_repository_create( repository, profile, filter_type, service, input );
203                 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
204         }
205
206         if ( obj != NULL )
207         {
208                 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
209                 set_common_properties( properties, profile, "filter", service );
210         }
211         return obj;
212 }
213
214 /** Fetch a transition from the repository.
215 */
216
217 mlt_transition mlt_factory_transition( mlt_profile profile, const char *service, void *input )
218 {
219         mlt_transition obj = NULL;
220
221         // Offer the application the chance to 'create'
222         mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
223
224         if ( obj == NULL )
225         {
226                 obj = mlt_repository_create( repository, profile, transition_type, service, input );
227                 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
228         }
229
230         if ( obj != NULL )
231         {
232                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
233                 set_common_properties( properties, profile, "transition", service );
234         }
235         return obj;
236 }
237
238 /** Fetch a consumer from the repository
239 */
240
241 mlt_consumer mlt_factory_consumer( mlt_profile profile, const char *service, void *input )
242 {
243         mlt_consumer obj = NULL;
244
245         if ( service == NULL )
246                 service = mlt_environment( "MLT_CONSUMER" );
247
248         // Offer the application the chance to 'create'
249         mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
250
251         if ( obj == NULL )
252         {
253                 obj = mlt_repository_create( repository, profile, consumer_type, service, input );
254                 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
255         }
256
257         if ( obj != NULL )
258         {
259                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
260                 set_common_properties( properties, profile, "consumer", service );
261         }
262         return obj;
263 }
264
265 /** Register an object for clean up.
266 */
267
268 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
269 {
270         char unique[ 256 ];
271         sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
272         mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
273 }
274
275 /** Close the factory.
276 */
277
278 void mlt_factory_close( )
279 {
280         if ( mlt_directory != NULL )
281         {
282                 mlt_properties_close( event_object );
283                 mlt_properties_close( global_properties );
284                 mlt_repository_close( repository );
285                 free( mlt_directory );
286                 mlt_directory = NULL;
287                 mlt_pool_close( );
288         }
289 }