Big modification - switch to macros for parent class access
[melted] / 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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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( 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 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
94 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "fezzik" );
95 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
96 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
97
98 // Create the object list.
99 object_list = mlt_properties_new( );
100
101 // Create a repository for each service type
102 producers = mlt_repository_init( object_list, prefix, "producers", "mlt_create_producer" );
103 filters = mlt_repository_init( object_list, prefix, "filters", "mlt_create_filter" );
104 transitions = mlt_repository_init( object_list, prefix, "transitions", "mlt_create_transition" );
105 consumers = mlt_repository_init( object_list, prefix, "consumers", "mlt_create_consumer" );
106
107 // Force a clean up when app closes
108 atexit( mlt_factory_close );
109 }
110
111 return 0;
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 prefix used in this instance.
123 */
124
125 const char *mlt_factory_prefix( )
126 {
127 return mlt_prefix;
128 }
129
130 /** Get a value from the environment.
131 */
132
133 char *mlt_environment( char *name )
134 {
135 return mlt_properties_get( global_properties, name );
136 }
137
138 /** Fetch a producer from the repository.
139 */
140
141 mlt_producer mlt_factory_producer( char *service, void *input )
142 {
143 mlt_producer obj = NULL;
144
145 // Pick up the default normalising producer if necessary
146 if ( service == NULL )
147 service = mlt_environment( "MLT_PRODUCER" );
148
149 // Offer the application the chance to 'create'
150 mlt_events_fire( event_object, "producer-create-request", service, input, &obj, NULL );
151
152 // Try to instantiate via the specified service
153 if ( obj == NULL )
154 {
155 obj = mlt_repository_fetch( producers, service, input );
156 mlt_events_fire( event_object, "producer-create-done", service, input, obj, NULL );
157 if ( obj != NULL )
158 {
159 mlt_properties properties = MLT_PRODUCER_PROPERTIES( obj );
160 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
161 mlt_properties_set( properties, "mlt_type", "producer" );
162 if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
163 mlt_properties_set( properties, "mlt_service", service );
164 }
165 }
166 return obj;
167 }
168
169 /** Fetch a filter from the repository.
170 */
171
172 mlt_filter mlt_factory_filter( char *service, void *input )
173 {
174 mlt_filter obj = NULL;
175
176 // Offer the application the chance to 'create'
177 mlt_events_fire( event_object, "filter-create-request", service, input, &obj, NULL );
178
179 if ( obj == NULL )
180 {
181 obj = mlt_repository_fetch( filters, service, input );
182 mlt_events_fire( event_object, "filter-create-done", service, input, obj, NULL );
183 }
184
185 if ( obj != NULL )
186 {
187 mlt_properties properties = MLT_FILTER_PROPERTIES( obj );
188 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
189 mlt_properties_set( properties, "mlt_type", "filter" );
190 mlt_properties_set( properties, "mlt_service", service );
191 }
192 return obj;
193 }
194
195 /** Fetch a transition from the repository.
196 */
197
198 mlt_transition mlt_factory_transition( char *service, void *input )
199 {
200 mlt_transition obj = NULL;
201
202 // Offer the application the chance to 'create'
203 mlt_events_fire( event_object, "transition-create-request", service, input, &obj, NULL );
204
205 if ( obj == NULL )
206 {
207 obj = mlt_repository_fetch( transitions, service, input );
208 mlt_events_fire( event_object, "transition-create-done", service, input, obj, NULL );
209 }
210
211 if ( obj != NULL )
212 {
213 mlt_properties properties = MLT_TRANSITION_PROPERTIES( obj );
214 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
215 mlt_properties_set( properties, "mlt_type", "transition" );
216 mlt_properties_set( properties, "mlt_service", service );
217 }
218 return obj;
219 }
220
221 /** Fetch a consumer from the repository
222 */
223
224 mlt_consumer mlt_factory_consumer( char *service, void *input )
225 {
226 mlt_consumer obj = NULL;
227
228 if ( service == NULL )
229 service = mlt_environment( "MLT_CONSUMER" );
230
231 // Offer the application the chance to 'create'
232 mlt_events_fire( event_object, "consumer-create-request", service, input, &obj, NULL );
233
234 if ( obj == NULL )
235 {
236 obj = mlt_repository_fetch( consumers, service, input );
237 mlt_events_fire( event_object, "consumer-create-done", service, input, obj, NULL );
238 }
239
240 if ( obj != NULL )
241 {
242 mlt_filter filter = mlt_factory_filter( "data_show", NULL );
243 mlt_properties properties = MLT_CONSUMER_PROPERTIES( obj );
244 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
245 mlt_properties_set( properties, "mlt_type", "consumer" );
246 mlt_properties_set( properties, "mlt_service", service );
247 mlt_service_attach( MLT_CONSUMER_SERVICE( obj ), filter );
248 mlt_filter_close( filter );
249 }
250 return obj;
251 }
252
253 /** Register an object for clean up.
254 */
255
256 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
257 {
258 char unique[ 256 ];
259 sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
260 mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
261 }
262
263 /** Close the factory.
264 */
265
266 void mlt_factory_close( )
267 {
268 if ( mlt_prefix != NULL )
269 {
270 mlt_properties_close( event_object );
271 mlt_repository_close( producers );
272 mlt_repository_close( filters );
273 mlt_repository_close( transitions );
274 mlt_repository_close( consumers );
275 mlt_properties_close( global_properties );
276 mlt_properties_close( object_list );
277 free( mlt_prefix );
278 mlt_prefix = NULL;
279 mlt_pool_close( );
280 }
281 }
282