mlt_repository.[hc]:
[melted] / src / framework / mlt_repository.c
1 /*
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 *
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_repository.h"
22 #include "mlt_properties.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <dlfcn.h>
27 #include <string.h>
28
29 struct mlt_repository_s
30 {
31 struct mlt_properties_s parent; // a list of object files
32 mlt_properties consumers; // lists of entry points
33 mlt_properties filters;
34 mlt_properties producers;
35 mlt_properties transitions;
36 };
37
38 mlt_repository mlt_repository_init( const char *prefix )
39 {
40 // Construct the repository
41 mlt_repository this = calloc( sizeof( struct mlt_repository_s ), 1 );
42 mlt_properties_init( &this->parent, this );
43 this->consumers = mlt_properties_new();
44 this->filters = mlt_properties_new();
45 this->producers = mlt_properties_new();
46 this->transitions = mlt_properties_new();
47
48 // Get the directory list
49 mlt_properties dir = mlt_properties_new();
50 int count = mlt_properties_dir_list( dir, prefix, NULL, 0 );
51 int i;
52
53 // Iterate over files
54 for ( i = 0; i < count; i++ )
55 {
56 int flags = RTLD_NOW;
57 const char *object_name = mlt_properties_get_value( dir, i);
58
59 // Very temporary hack to allow the quicktime plugins to work
60 // TODO: extend repository to allow this to be used on a case by case basis
61 if ( strstr( object_name, "libmltkino" ) )
62 flags |= RTLD_GLOBAL;
63
64 // Open the shared object
65 void *object = dlopen( object_name, flags );
66 if ( object != NULL )
67 {
68 // Get the registration function
69 int ( *symbol_ptr )( mlt_repository ) = dlsym( object, "mlt_register" );
70
71 // Call the registration function
72 if ( symbol_ptr != NULL )
73 {
74 symbol_ptr( this );
75
76 // Register the object file for closure
77 mlt_properties_set_data( &this->parent, object_name, object, 0, ( mlt_destructor )dlclose, NULL );
78 }
79 else
80 {
81 dlclose( object );
82 }
83 }
84 }
85
86 return this;
87 }
88
89 void mlt_repository_register( mlt_repository this, mlt_service_type service_type, const char *service, void *symbol )
90 {
91 // Add the entry point to the corresponding service list
92 switch ( service_type )
93 {
94 case consumer_type:
95 mlt_properties_set_data( this->consumers, service, symbol, 0, NULL, NULL );
96 break;
97 case filter_type:
98 mlt_properties_set_data( this->filters, service, symbol, 0, NULL, NULL );
99 break;
100 case producer_type:
101 mlt_properties_set_data( this->producers, service, symbol, 0, NULL, NULL );
102 break;
103 case transition_type:
104 mlt_properties_set_data( this->transitions, service, symbol, 0, NULL, NULL );
105 break;
106 default:
107 break;
108 }
109 }
110
111 void *mlt_repository_fetch( mlt_repository this, mlt_profile profile, mlt_service_type type, const char *service, void *input )
112 {
113 void *( *symbol_ptr )( mlt_profile, mlt_service_type, const char *, void * ) = NULL;
114
115 // Get the entry point from the corresponding service list
116 switch ( type )
117 {
118 case consumer_type:
119 symbol_ptr = mlt_properties_get_data( this->consumers, service, NULL );
120 break;
121 case filter_type:
122 symbol_ptr = mlt_properties_get_data( this->filters, service, NULL );
123 break;
124 case producer_type:
125 symbol_ptr = mlt_properties_get_data( this->producers, service, NULL );
126 break;
127 case transition_type:
128 symbol_ptr = mlt_properties_get_data( this->transitions, service, NULL );
129 break;
130 default:
131 break;
132 }
133
134 // Construct the service
135 return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL;
136 }
137
138 void mlt_repository_close( mlt_repository this )
139 {
140 mlt_properties_close( this->consumers );
141 mlt_properties_close( this->filters );
142 mlt_properties_close( this->producers );
143 mlt_properties_close( this->transitions );
144 mlt_properties_close( &this->parent );
145 free( this );
146 }