2 * repository.c -- provides a map between service and shared objects
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Contributor: Dan Dennedy <dan@dennedy.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "mlt_repository.h"
23 #include "mlt_properties.h"
30 struct mlt_repository_s
32 struct mlt_properties_s parent
; // a list of object files
33 mlt_properties consumers
; // lists of entry points
34 mlt_properties filters
;
35 mlt_properties producers
;
36 mlt_properties transitions
;
39 /** Construct a new repository
42 mlt_repository
mlt_repository_init( const char *directory
)
45 if ( directory
== NULL
|| strcmp( directory
, "" ) == 0 )
48 // Construct the repository
49 mlt_repository
this = calloc( sizeof( struct mlt_repository_s
), 1 );
50 mlt_properties_init( &this->parent
, this );
51 this->consumers
= mlt_properties_new();
52 this->filters
= mlt_properties_new();
53 this->producers
= mlt_properties_new();
54 this->transitions
= mlt_properties_new();
56 // Get the directory list
57 mlt_properties dir
= mlt_properties_new();
58 int count
= mlt_properties_dir_list( dir
, directory
, NULL
, 0 );
62 for ( i
= 0; i
< count
; i
++ )
65 const char *object_name
= mlt_properties_get_value( dir
, i
);
67 // Very temporary hack to allow the quicktime plugins to work
68 // TODO: extend repository to allow this to be used on a case by case basis
69 if ( strstr( object_name
, "libmltkino" ) )
72 // Open the shared object
73 void *object
= dlopen( object_name
, flags
);
76 // Get the registration function
77 mlt_repository_callback symbol_ptr
= dlsym( object
, "mlt_register" );
79 // Call the registration function
80 if ( symbol_ptr
!= NULL
)
84 // Register the object file for closure
85 mlt_properties_set_data( &this->parent
, object_name
, object
, 0, ( mlt_destructor
)dlclose
, NULL
);
92 else if ( strstr( object_name
, "libmlt" ) )
94 fprintf( stderr
, "%s, %s: failed to dlopen %s\n", __FILE__
, __FUNCTION__
, object_name
);
101 static mlt_properties
new_service( void *symbol
)
103 mlt_properties properties
= mlt_properties_new();
104 mlt_properties_set_data( properties
, "symbol", symbol
, 0, NULL
, NULL
);
108 /** Register a service with the repository
109 Typically, this is invoked by a module within its mlt_register().
112 void mlt_repository_register( mlt_repository
this, mlt_service_type service_type
, const char *service
, mlt_register_callback symbol
)
114 // Add the entry point to the corresponding service list
115 switch ( service_type
)
118 mlt_properties_set_data( this->consumers
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
121 mlt_properties_set_data( this->filters
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
124 mlt_properties_set_data( this->producers
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
126 case transition_type
:
127 mlt_properties_set_data( this->transitions
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
134 static mlt_properties
get_service_properties( mlt_repository
this, mlt_service_type type
, const char *service
)
136 mlt_properties service_properties
= NULL
;
138 // Get the entry point from the corresponding service list
142 service_properties
= mlt_properties_get_data( this->consumers
, service
, NULL
);
145 service_properties
= mlt_properties_get_data( this->filters
, service
, NULL
);
148 service_properties
= mlt_properties_get_data( this->producers
, service
, NULL
);
150 case transition_type
:
151 service_properties
= mlt_properties_get_data( this->transitions
, service
, NULL
);
156 return service_properties
;
159 /** Construct a new instance of a service
162 void *mlt_repository_create( mlt_repository
this, mlt_profile profile
, mlt_service_type type
, const char *service
, void *input
)
164 mlt_properties properties
= get_service_properties( this, type
, service
);
165 if ( properties
!= NULL
)
167 mlt_register_callback symbol_ptr
= mlt_properties_get_data( properties
, "symbol", NULL
);
169 // Construct the service
170 return ( symbol_ptr
!= NULL
) ?
symbol_ptr( profile
, type
, service
, input
) : NULL
;
175 /** Destroy a repository
178 void mlt_repository_close( mlt_repository
this )
180 mlt_properties_close( this->consumers
);
181 mlt_properties_close( this->filters
);
182 mlt_properties_close( this->producers
);
183 mlt_properties_close( this->transitions
);
184 mlt_properties_close( &this->parent
);
188 /** Get the list of registered consumers
191 mlt_properties
mlt_repository_consumers( mlt_repository self
)
193 return self
->consumers
;
196 /** Get the list of registered filters
199 mlt_properties
mlt_repository_filters( mlt_repository self
)
201 return self
->filters
;
204 /** Get the list of registered producers
207 mlt_properties
mlt_repository_producers( mlt_repository self
)
209 return self
->producers
;
212 /** Get the list of registered transitions
215 mlt_properties
mlt_repository_transitions( mlt_repository self
)
217 return self
->transitions
;
220 /** Register the metadata for a service
221 IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties that you supply!
223 void mlt_repository_register_metadata( mlt_repository self
, mlt_service_type type
, const char *service
, mlt_metadata_callback callback
, void *callback_data
)
225 mlt_properties service_properties
= get_service_properties( self
, type
, service
);
226 mlt_properties_set_data( service_properties
, "metadata_cb", callback
, 0, NULL
, NULL
);
227 mlt_properties_set_data( service_properties
, "metadata_cb_data", callback_data
, 0, NULL
, NULL
);
230 /** Get the metadata about a service
231 Returns NULL if service or its metadata are unavailable.
234 mlt_properties
mlt_repository_metadata( mlt_repository self
, mlt_service_type type
, const char *service
)
236 mlt_properties metadata
= NULL
;
237 mlt_properties properties
= get_service_properties( self
, type
, service
);
239 // If this is a valid service
242 // Lookup cached metadata
243 metadata
= mlt_properties_get_data( properties
, "metadata", NULL
);
246 // Not cached, so get the registered metadata callback function
247 mlt_metadata_callback callback
= mlt_properties_get_data( properties
, "metadata_cb", NULL
);
249 // If a metadata callback function is registered
252 // Fetch the callback data arg
253 void *data
= mlt_properties_get_data( properties
, "metadata_cb_data", NULL
);
255 // Fetch the metadata through the callback
256 metadata
= callback( type
, service
, data
);
258 // Cache the metadata
260 // Include dellocation and serialisation
261 mlt_properties_set_data( properties
, "metadata", metadata
, 0, ( mlt_destructor
)mlt_properties_close
, ( mlt_serialiser
)mlt_properties_serialise_yaml
);