]> git.sesse.net Git - mlt/blob - src/framework/mlt_factory.c
Data feed and show filters
[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 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 int unique_id = 0;
40
41 /** Construct the factories.
42 */
43
44 int mlt_factory_init( char *prefix )
45 {
46         // Only initialise once
47         if ( mlt_prefix == NULL )
48         {
49                 // Allow user over rides
50                 if ( prefix == NULL || !strcmp( prefix, "" ) )
51                         prefix = getenv( "MLT_REPOSITORY" );
52
53                 // If no directory is specified, default to install directory
54                 if ( prefix == NULL )
55                         prefix = PREFIX_DATA;
56
57                 // Store the prefix for later retrieval
58                 mlt_prefix = strdup( prefix );
59
60                 // Initialise the pool
61                 mlt_pool_init( );
62
63                 // Create the global properties
64                 global_properties = mlt_properties_new( );
65                 mlt_properties_set_or_default( global_properties, "MLT_NORMALISATION", getenv( "MLT_NORMALISATION" ), "PAL" );
66                 mlt_properties_set_or_default( global_properties, "MLT_PRODUCER", getenv( "MLT_PRODUCER" ), "fezzik" );
67                 mlt_properties_set_or_default( global_properties, "MLT_CONSUMER", getenv( "MLT_CONSUMER" ), "sdl" );
68                 mlt_properties_set( global_properties, "MLT_TEST_CARD", getenv( "MLT_TEST_CARD" ) );
69
70                 // Create the object list.
71                 object_list = mlt_properties_new( );
72
73                 // Create a repository for each service type
74                 producers = mlt_repository_init( object_list, prefix, "producers", "mlt_create_producer" );
75                 filters = mlt_repository_init( object_list, prefix, "filters", "mlt_create_filter" );
76                 transitions = mlt_repository_init( object_list, prefix, "transitions", "mlt_create_transition" );
77                 consumers = mlt_repository_init( object_list, prefix, "consumers", "mlt_create_consumer" );
78
79                 // Force a clean up when app closes
80                 atexit( mlt_factory_close );
81         }
82
83         return 0;
84 }
85
86 /** Fetch the prefix used in this instance.
87 */
88
89 const char *mlt_factory_prefix( )
90 {
91         return mlt_prefix;
92 }
93
94 /** Get a value from the environment.
95 */
96
97 char *mlt_environment( char *name )
98 {
99         return mlt_properties_get( global_properties, name );
100 }
101
102 /** Fetch a producer from the repository.
103 */
104
105 mlt_producer mlt_factory_producer( char *service, void *input )
106 {
107         mlt_producer obj = NULL;
108
109         // Pick up the default normalising producer if necessary
110         if ( service == NULL )
111                 service = mlt_environment( "MLT_PRODUCER" );
112
113         // Try to instantiate via the specified service
114         obj = mlt_repository_fetch( producers, service, input );
115
116         if ( obj != NULL )
117         {
118                 mlt_properties properties = mlt_producer_properties( obj );
119                 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
120                 mlt_properties_set( properties, "mlt_type", "producer" );
121                 if ( mlt_properties_get_int( properties, "_mlt_service_hidden" ) == 0 )
122                         mlt_properties_set( properties, "mlt_service", service );
123         }
124         return obj;
125 }
126
127 /** Fetch a filter from the repository.
128 */
129
130 mlt_filter mlt_factory_filter( char *service, void *input )
131 {
132         mlt_filter obj = mlt_repository_fetch( filters, service, input );
133         if ( obj != NULL )
134         {
135                 mlt_properties properties = mlt_filter_properties( obj );
136                 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
137                 mlt_properties_set( properties, "mlt_type", "filter" );
138                 mlt_properties_set( properties, "mlt_service", service );
139         }
140         return obj;
141 }
142
143 /** Fetch a transition from the repository.
144 */
145
146 mlt_transition mlt_factory_transition( char *service, void *input )
147 {
148         mlt_transition obj = mlt_repository_fetch( transitions, service, input );
149         if ( obj != NULL )
150         {
151                 mlt_properties properties = mlt_transition_properties( obj );
152                 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
153                 mlt_properties_set( properties, "mlt_type", "transition" );
154                 mlt_properties_set( properties, "mlt_service", service );
155         }
156         return obj;
157 }
158
159 /** Fetch a consumer from the repository
160 */
161
162 mlt_consumer mlt_factory_consumer( char *service, void *input )
163 {
164         mlt_consumer obj = NULL;
165
166         if ( service == NULL )
167                 service = mlt_environment( "MLT_CONSUMER" );
168
169         obj = mlt_repository_fetch( consumers, service, input );
170
171         if ( obj != NULL )
172         {
173                 mlt_properties properties = mlt_consumer_properties( obj );
174                 mlt_properties_set_int( properties, "_unique_id", ++ unique_id );
175                 mlt_properties_set( properties, "mlt_type", "consumer" );
176                 mlt_properties_set( properties, "mlt_service", service );
177                 mlt_service_attach( mlt_consumer_service( obj ), mlt_factory_filter( "data_show", NULL ) );
178         }
179         return obj;
180 }
181
182 /** Register an object for clean up.
183 */
184
185 void mlt_factory_register_for_clean_up( void *ptr, mlt_destructor destructor )
186 {
187         char unique[ 256 ];
188         sprintf( unique, "%08d", mlt_properties_count( global_properties ) );
189         mlt_properties_set_data( global_properties, unique, ptr, 0, destructor, NULL );
190 }
191
192 /** Close the factory.
193 */
194
195 void mlt_factory_close( )
196 {
197         if ( mlt_prefix != NULL )
198         {
199                 mlt_repository_close( producers );
200                 mlt_repository_close( filters );
201                 mlt_repository_close( transitions );
202                 mlt_repository_close( consumers );
203                 mlt_properties_close( global_properties );
204                 mlt_properties_close( object_list );
205                 free( mlt_prefix );
206                 mlt_prefix = NULL;
207                 mlt_pool_close( );
208         }
209 }
210