src/modules/frei0r/factory.c: load metadata on request (thx for patch from Dan Dennedy)
[melted] / src / modules / frei0r / factory.c
1 /*
2 * factory.c -- the factory method interfaces
3 * Copyright (c) 2008 Marco Gittler <g.marco@freenet.de>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <string.h>
21 #include <framework/mlt.h>
22 #include <frei0r.h>
23
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <dirent.h>
28 #include <dlfcn.h>
29 #include <stdlib.h>
30
31 extern mlt_filter filter_frei0r_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
32 extern mlt_frame filter_process( mlt_filter this, mlt_frame frame );
33 extern void filter_close( mlt_filter this );
34 extern void transition_close( mlt_transition this );
35 extern mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame );
36
37 static mlt_properties fill_param_info ( mlt_service_type type, const char *service_name, char *name )
38 {
39 void* handle=dlopen(name,RTLD_LAZY);
40 if (!handle) return NULL;
41 void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
42 void (*param_info)(f0r_param_info_t*,int param_index)=dlsym(handle,"f0r_get_param_info");
43 if (!plginfo || !param_info) {
44 dlclose(handle);
45 return NULL;
46 }
47 mlt_properties metadata = mlt_properties_new();
48 f0r_plugin_info_t info;
49 char string[48];
50 int j=0;
51
52 plginfo(&info);
53 snprintf ( string, sizeof(string) , "%d.%d" , info.major_version , info.minor_version );
54 mlt_properties_set ( metadata, "title" , info.name );
55 mlt_properties_set ( metadata, "version", string );
56 mlt_properties_set ( metadata, "identifier" , service_name );
57 mlt_properties_set ( metadata, "description" , info.explanation );
58 mlt_properties_set ( metadata, "creator" , info.author );
59 switch (type){
60 case filter_type:
61 mlt_properties_set ( metadata, "type" , "filter" );
62 break;
63 case transition_type:
64 mlt_properties_set ( metadata, "type" , "transition" );
65 break;
66 default:
67 break;
68 }
69
70 mlt_properties parameter = mlt_properties_new ( );
71 mlt_properties_set_data ( metadata , "parameters" , parameter , 0 , ( mlt_destructor )mlt_properties_close, NULL );
72 mlt_properties tags = mlt_properties_new ( );
73 mlt_properties_set_data ( metadata , "tags" , tags , 0 , ( mlt_destructor )mlt_properties_close, NULL );
74 mlt_properties_set ( tags , "0" , "Video" );
75
76 for (j=0;j<info.num_params;j++){
77 snprintf ( string , sizeof(string), "%d" , j );
78 mlt_properties pnum = mlt_properties_new ( );
79 mlt_properties_set_data ( parameter , string , pnum , 0 , ( mlt_destructor )mlt_properties_close, NULL );
80 f0r_param_info_t paraminfo;
81 param_info(&paraminfo,j);
82 mlt_properties_set ( pnum , "identifier" , paraminfo.name );
83 mlt_properties_set ( pnum , "title" , paraminfo.name );
84 mlt_properties_set ( pnum , "description" , paraminfo.explanation);
85 if ( paraminfo.type == F0R_PARAM_DOUBLE ){
86 mlt_properties_set ( pnum , "type" , "integer" );
87 mlt_properties_set ( pnum , "minimum" , "0" );
88 mlt_properties_set ( pnum , "maximum" , "1000" );
89 mlt_properties_set ( pnum , "readonly" , "no" );
90 mlt_properties_set ( pnum , "widget" , "spinner" );
91 }else
92 if ( paraminfo.type == F0R_PARAM_BOOL ){
93 mlt_properties_set ( pnum , "type" , "boolean" );
94 mlt_properties_set ( pnum , "minimum" , "0" );
95 mlt_properties_set ( pnum , "maximum" , "1" );
96 mlt_properties_set ( pnum , "readonly" , "no" );
97 }else
98 if ( paraminfo.type == F0R_PARAM_STRING ){
99 mlt_properties_set ( pnum , "type" , "string" );
100 mlt_properties_set ( pnum , "readonly" , "no" );
101 }
102 }
103 dlclose(handle);
104 free(name);
105
106 return metadata;
107 }
108
109 static void * load_lib( mlt_profile profile, mlt_service_type type , void* handle){
110
111 int i=0;
112 void (*f0r_get_plugin_info)(f0r_plugin_info_t*),
113 *f0r_construct , *f0r_update , *f0r_destruct,
114 (*f0r_get_param_info)(f0r_param_info_t* info, int param_index),
115 (*f0r_init)(void) , *f0r_deinit ,
116 (*f0r_set_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
117 (*f0r_get_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
118 (*f0r_update2) (f0r_instance_t instance, double time, const uint32_t* inframe1,
119 const uint32_t* inframe2,const uint32_t* inframe3, uint32_t* outframe);
120
121 if ( ( f0r_construct = dlsym(handle, "f0r_construct") ) &&
122 (f0r_update = dlsym(handle,"f0r_update") ) &&
123 (f0r_destruct = dlsym(handle,"f0r_destruct") ) &&
124 (f0r_get_plugin_info = dlsym(handle,"f0r_get_plugin_info") ) &&
125 (f0r_get_param_info = dlsym(handle,"f0r_get_param_info") ) &&
126 (f0r_set_param_value= dlsym(handle,"f0r_set_param_value" ) ) &&
127 (f0r_get_param_value= dlsym(handle,"f0r_get_param_value" ) ) &&
128 (f0r_init= dlsym(handle,"f0r_init" ) ) &&
129 (f0r_deinit= dlsym(handle,"f0r_deinit" ) )
130 ){
131
132 f0r_update2=dlsym(handle,"f0r_update2");
133
134 f0r_plugin_info_t info;
135 f0r_get_plugin_info(&info);
136
137 void* ret=NULL;
138 mlt_properties properties=NULL;
139
140 if (type == filter_type && info.plugin_type == F0R_PLUGIN_TYPE_FILTER ){
141 mlt_filter this = mlt_filter_new( );
142 if ( this != NULL )
143 {
144 this->process = filter_process;
145 this->close = filter_close;
146 f0r_init();
147 properties=MLT_FILTER_PROPERTIES ( this );
148
149 for (i=0;i<info.num_params;i++){
150 f0r_param_info_t pinfo;
151 f0r_get_param_info(&pinfo,i);
152
153 }
154
155 ret=this;
156 }
157 }else if (type == transition_type && info.plugin_type == F0R_PLUGIN_TYPE_MIXER2){
158 mlt_transition transition = mlt_transition_new( );
159 if ( transition != NULL )
160 {
161 transition->process = transition_process;
162 transition->close = transition_close;
163 properties=MLT_TRANSITION_PROPERTIES( transition );
164 mlt_properties_set_int(properties, "_transition_type", 1 );
165
166 ret=transition;
167 }
168 }
169 mlt_properties_set_data(properties, "_dlclose_handle", handle , sizeof (void*) , NULL , NULL );
170 mlt_properties_set_data(properties, "_dlclose", dlclose , sizeof (void*) , NULL , NULL );
171 mlt_properties_set_data(properties, "f0r_construct", f0r_construct , sizeof(void*),NULL,NULL);
172 mlt_properties_set_data(properties, "f0r_update", f0r_update , sizeof(void*),NULL,NULL);
173 if (f0r_update2)
174 mlt_properties_set_data(properties, "f0r_update2", f0r_update2 , sizeof(void*),NULL,NULL);
175 mlt_properties_set_data(properties, "f0r_destruct", f0r_destruct , sizeof(void*),NULL,NULL);
176 mlt_properties_set_data(properties, "f0r_get_plugin_info", f0r_get_plugin_info , sizeof(void*),NULL,NULL);
177 mlt_properties_set_data(properties, "f0r_get_param_info", f0r_get_param_info , sizeof(void*),NULL,NULL);
178 mlt_properties_set_data(properties, "f0r_set_param_value", f0r_set_param_value , sizeof(void*),NULL,NULL);
179 mlt_properties_set_data(properties, "f0r_get_param_value", f0r_get_param_value , sizeof(void*),NULL,NULL);
180
181
182 return ret;
183 }else{
184 printf("some was wrong\n");
185 dlerror();
186 }
187 return NULL;
188 }
189
190 static void * create_frei0r_item ( mlt_profile profile, mlt_service_type type, const char *id, void *arg){
191
192 mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
193 int dircount=mlt_tokeniser_parse_new (
194 tokeniser ,
195 getenv("MLT_FREI0R_PLUGIN_PATH") ? getenv("MLT_FREI0R_PLUGIN_PATH") : "/usr/lib/frei0r-1" ,
196 ":"
197 );
198 void* ret=NULL;
199 while (dircount--){
200 char soname[1024]="";
201
202 char *save_firstptr = NULL;
203 char *firstname=strtok_r(strdup(id),".",&save_firstptr);
204
205 firstname=strtok_r(NULL,".",&save_firstptr);
206 sprintf(soname,"%s/%s.so", mlt_tokeniser_get_string( tokeniser , dircount ) , firstname );
207
208 if (firstname){
209
210 void* handle=dlopen(soname,RTLD_LAZY);
211
212 if (handle ){
213 ret=load_lib ( profile , type , handle );
214 }else{
215 dlerror();
216 }
217 }
218 }
219 mlt_tokeniser_close ( tokeniser );
220 return ret;
221 }
222
223
224 MLT_REPOSITORY
225 {
226 int i=0;
227 mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
228 int dircount=mlt_tokeniser_parse_new (
229 tokeniser ,
230 getenv("MLT_FREI0R_PLUGIN_PATH") ? getenv("MLT_FREI0R_PLUGIN_PATH") : "/usr/lib/frei0r-1" ,
231 ":"
232 );
233
234 while (dircount--){
235
236 mlt_properties direntries = mlt_properties_new();
237 char* dirname = mlt_tokeniser_get_string ( tokeniser , dircount ) ;
238 mlt_properties_dir_list(direntries, dirname ,"*.so",1);
239
240 for (i=0;i<mlt_properties_count(direntries);i++){
241 char* name=mlt_properties_get_value(direntries,i);
242 char* shortname=name+strlen(dirname)+1;
243 char fname[1024]="";
244
245 strcat(fname,dirname);
246 strcat(fname,shortname);
247
248 char *save_firstptr = NULL;
249 char pluginname[1024]="frei0r.";
250 char* firstname = strtok_r ( shortname , "." , &save_firstptr );
251 strcat(pluginname,firstname);
252
253 void* handle=dlopen(strcat(name,".so"),RTLD_LAZY);
254 if (handle){
255 void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
256
257 if (plginfo){
258 f0r_plugin_info_t info;
259 plginfo(&info);
260
261 if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_FILTER){
262 MLT_REGISTER( filter_type, pluginname, create_frei0r_item );
263 MLT_REGISTER_METADATA( filter_type, pluginname, fill_param_info, strdup(name) );
264 }
265 else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_MIXER2 ){
266 MLT_REGISTER( transition_type, pluginname, create_frei0r_item );
267 MLT_REGISTER_METADATA( transition_type, pluginname, fill_param_info, strdup(name) );
268 }
269 }
270 dlclose(handle);
271 }
272 }
273 mlt_properties_close(direntries);
274 }
275 mlt_tokeniser_close ( tokeniser );
276 }