2 * mlt_factory.c -- the factory method interfaces
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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
22 #include "mlt_repository.h"
28 #define PREFIX_LIB LIBDIR "/mlt"
29 #define PREFIX_DATA PREFIX "/share/mlt"
31 /** Singleton repositories
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;
40 /** Event transmitters.
43 static void mlt_factory_create_request( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
45 if ( listener
!= NULL
)
46 listener( owner
, this, ( char * )args
[ 0 ], ( char * )args
[ 1 ], ( mlt_service
* )args
[ 2 ] );
49 static void mlt_factory_create_done( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
51 if ( listener
!= NULL
)
52 listener( owner
, this, ( char * )args
[ 0 ], ( char * )args
[ 1 ], ( mlt_service
)args
[ 2 ] );
55 /** Construct the factories.
58 mlt_repository
mlt_factory_init( const char *directory
)
60 // Only initialise once
61 if ( mlt_directory
== NULL
)
63 // Allow user over rides
64 if ( directory
== NULL
|| !strcmp( directory
, "" ) )
65 directory
= getenv( "MLT_REPOSITORY" );
67 // If no directory is specified, default to install directory
68 if ( directory
== NULL
)
69 directory
= PREFIX_LIB
;
71 // Store the prefix for later retrieval
72 mlt_directory
= strdup( directory
);
74 // Initialise the pool
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
);
89 // Create the global properties
90 global_properties
= mlt_properties_new( );
92 // Create the repository of services
93 repository
= mlt_repository_init( directory
);
95 // Force a clean up when app closes
96 atexit( mlt_factory_close
);
99 // Allow property refresh on a subsequent initialisation
100 if ( global_properties
!= NULL
)
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
);
114 /** Fetch the events object.
117 mlt_properties
mlt_factory_event_object( )
122 /** Fetch the module directory used in this instance.
125 const char *mlt_factory_directory( )
127 return mlt_directory
;
130 /** Get a value from the environment.
133 char *mlt_environment( const char *name
)
135 if ( global_properties
)
136 return mlt_properties_get( global_properties
, name
);
141 /** Set a value in the environment.
144 int mlt_environment_set( const char *name
, const char *value
)
146 if ( global_properties
)
147 return mlt_properties_set( global_properties
, name
, value
);
152 static void set_common_properties( mlt_properties properties
, mlt_profile profile
, const char *type
, const char *service
)
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
);
162 /** Fetch a producer from the repository.
165 mlt_producer
mlt_factory_producer( mlt_profile profile
, const char *service
, void *input
)
167 mlt_producer obj
= NULL
;
169 // Pick up the default normalising producer if necessary
170 if ( service
== NULL
)
171 service
= mlt_environment( "MLT_PRODUCER" );
173 // Offer the application the chance to 'create'
174 mlt_events_fire( event_object
, "producer-create-request", service
, input
, &obj
, NULL
);
176 // Try to instantiate via the specified service
179 obj
= mlt_repository_create( repository
, profile
, producer_type
, service
, input
);
180 mlt_events_fire( event_object
, "producer-create-done", service
, input
, obj
, NULL
);
183 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( obj
);
184 set_common_properties( properties
, profile
, "producer", service
);
190 /** Fetch a filter from the repository.
193 mlt_filter
mlt_factory_filter( mlt_profile profile
, const char *service
, void *input
)
195 mlt_filter obj
= NULL
;
197 // Offer the application the chance to 'create'
198 mlt_events_fire( event_object
, "filter-create-request", service
, input
, &obj
, NULL
);
202 obj
= mlt_repository_create( repository
, profile
, filter_type
, service
, input
);
203 mlt_events_fire( event_object
, "filter-create-done", service
, input
, obj
, NULL
);
208 mlt_properties properties
= MLT_FILTER_PROPERTIES( obj
);
209 set_common_properties( properties
, profile
, "filter", service
);
214 /** Fetch a transition from the repository.
217 mlt_transition
mlt_factory_transition( mlt_profile profile
, const char *service
, void *input
)
219 mlt_transition obj
= NULL
;
221 // Offer the application the chance to 'create'
222 mlt_events_fire( event_object
, "transition-create-request", service
, input
, &obj
, NULL
);
226 obj
= mlt_repository_create( repository
, profile
, transition_type
, service
, input
);
227 mlt_events_fire( event_object
, "transition-create-done", service
, input
, obj
, NULL
);
232 mlt_properties properties
= MLT_TRANSITION_PROPERTIES( obj
);
233 set_common_properties( properties
, profile
, "transition", service
);
238 /** Fetch a consumer from the repository
241 mlt_consumer
mlt_factory_consumer( mlt_profile profile
, const char *service
, void *input
)
243 mlt_consumer obj
= NULL
;
245 if ( service
== NULL
)
246 service
= mlt_environment( "MLT_CONSUMER" );
248 // Offer the application the chance to 'create'
249 mlt_events_fire( event_object
, "consumer-create-request", service
, input
, &obj
, NULL
);
253 obj
= mlt_repository_create( repository
, profile
, consumer_type
, service
, input
);
254 mlt_events_fire( event_object
, "consumer-create-done", service
, input
, obj
, NULL
);
259 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( obj
);
260 set_common_properties( properties
, profile
, "consumer", service
);
265 /** Register an object for clean up.
268 void mlt_factory_register_for_clean_up( void *ptr
, mlt_destructor destructor
)
271 sprintf( unique
, "%08d", mlt_properties_count( global_properties
) );
272 mlt_properties_set_data( global_properties
, unique
, ptr
, 0, destructor
, NULL
);
275 /** Close the factory.
278 void mlt_factory_close( )
280 if ( mlt_directory
!= NULL
)
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
;