frei0r/factory.c: add more default locations for locating plugins including one for...
[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/stat.h>
27 #include <sys/types.h>
28 #include <dirent.h>
29 #include <dlfcn.h>
30 #include <stdlib.h>
31 #include <limits.h>
32
33 #define FREI0R_PLUGIN_PATH "/usr/lib/frei0r-1:/usr/local/lib/frei0r-1:/opt/local/lib/frei0r-1"
34
35 extern mlt_filter filter_frei0r_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
36 extern mlt_frame filter_process( mlt_filter this, mlt_frame frame );
37 extern void filter_close( mlt_filter this );
38 extern void transition_close( mlt_transition this );
39 extern mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame );
40
41 static mlt_properties fill_param_info ( mlt_service_type type, const char *service_name, char *name )
42 {
43 char file[ PATH_MAX ];
44 char servicetype[ 1024 ]="";
45 struct stat stat_buff;
46
47 switch ( type ) {
48 case filter_type:
49 strcpy ( servicetype , "filter" );
50 break;
51 case transition_type:
52 strcpy ( servicetype , "transition" ) ;
53 break;
54 default:
55 strcpy ( servicetype , "" );
56 };
57
58 snprintf( file, PATH_MAX, "%s/frei0r/%s_%s.yml", mlt_environment( "MLT_DATA" ), servicetype, service_name );
59 stat(file,&stat_buff);
60
61 if (S_ISREG(stat_buff.st_mode)){
62 return mlt_properties_parse_yaml( file );
63 }
64
65 void* handle=dlopen(name,RTLD_LAZY);
66 if (!handle) return NULL;
67 void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
68 void (*param_info)(f0r_param_info_t*,int param_index)=dlsym(handle,"f0r_get_param_info");
69 if (!plginfo || !param_info) {
70 dlclose(handle);
71 return NULL;
72 }
73 mlt_properties metadata = mlt_properties_new();
74 f0r_plugin_info_t info;
75 char string[48];
76 int j=0;
77
78 plginfo(&info);
79 snprintf ( string, sizeof(string) , "%d.%d" , info.major_version , info.minor_version );
80 mlt_properties_set ( metadata, "schema_version" , "0.1" );
81 mlt_properties_set ( metadata, "title" , info.name );
82 mlt_properties_set ( metadata, "version", string );
83 mlt_properties_set ( metadata, "identifier" , service_name );
84 mlt_properties_set ( metadata, "description" , info.explanation );
85 mlt_properties_set ( metadata, "creator" , info.author );
86 switch (type){
87 case filter_type:
88 mlt_properties_set ( metadata, "type" , "filter" );
89 break;
90 case transition_type:
91 mlt_properties_set ( metadata, "type" , "transition" );
92 break;
93 default:
94 break;
95 }
96
97 mlt_properties parameter = mlt_properties_new ( );
98 mlt_properties_set_data ( metadata , "parameters" , parameter , 0 , ( mlt_destructor )mlt_properties_close, NULL );
99 mlt_properties tags = mlt_properties_new ( );
100 mlt_properties_set_data ( metadata , "tags" , tags , 0 , ( mlt_destructor )mlt_properties_close, NULL );
101 mlt_properties_set ( tags , "0" , "Video" );
102
103 for (j=0;j<info.num_params;j++){
104 snprintf ( string , sizeof(string), "%d" , j );
105 mlt_properties pnum = mlt_properties_new ( );
106 mlt_properties_set_data ( parameter , string , pnum , 0 , ( mlt_destructor )mlt_properties_close, NULL );
107 f0r_param_info_t paraminfo;
108 param_info(&paraminfo,j);
109 mlt_properties_set ( pnum , "identifier" , paraminfo.name );
110 mlt_properties_set ( pnum , "title" , paraminfo.name );
111 mlt_properties_set ( pnum , "description" , paraminfo.explanation);
112 if ( paraminfo.type == F0R_PARAM_DOUBLE ){
113 mlt_properties_set ( pnum , "type" , "float" );
114 mlt_properties_set ( pnum , "minimum" , "0" );
115 mlt_properties_set ( pnum , "maximum" , "1" );
116 mlt_properties_set ( pnum , "readonly" , "no" );
117 mlt_properties_set ( pnum , "widget" , "spinner" );
118 }else
119 if ( paraminfo.type == F0R_PARAM_BOOL ){
120 mlt_properties_set ( pnum , "type" , "boolean" );
121 mlt_properties_set ( pnum , "minimum" , "0" );
122 mlt_properties_set ( pnum , "maximum" , "1" );
123 mlt_properties_set ( pnum , "readonly" , "no" );
124 }else
125 if ( paraminfo.type == F0R_PARAM_STRING ){
126 mlt_properties_set ( pnum , "type" , "string" );
127 mlt_properties_set ( pnum , "readonly" , "no" );
128 }
129 }
130 dlclose(handle);
131 free(name);
132
133 return metadata;
134 }
135
136 static void * load_lib( mlt_profile profile, mlt_service_type type , void* handle){
137
138 int i=0;
139 void (*f0r_get_plugin_info)(f0r_plugin_info_t*),
140 *f0r_construct , *f0r_update , *f0r_destruct,
141 (*f0r_get_param_info)(f0r_param_info_t* info, int param_index),
142 (*f0r_init)(void) , *f0r_deinit ,
143 (*f0r_set_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
144 (*f0r_get_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index),
145 (*f0r_update2) (f0r_instance_t instance, double time, const uint32_t* inframe1,
146 const uint32_t* inframe2,const uint32_t* inframe3, uint32_t* outframe);
147
148 if ( ( f0r_construct = dlsym(handle, "f0r_construct") ) &&
149 (f0r_update = dlsym(handle,"f0r_update") ) &&
150 (f0r_destruct = dlsym(handle,"f0r_destruct") ) &&
151 (f0r_get_plugin_info = dlsym(handle,"f0r_get_plugin_info") ) &&
152 (f0r_get_param_info = dlsym(handle,"f0r_get_param_info") ) &&
153 (f0r_set_param_value= dlsym(handle,"f0r_set_param_value" ) ) &&
154 (f0r_get_param_value= dlsym(handle,"f0r_get_param_value" ) ) &&
155 (f0r_init= dlsym(handle,"f0r_init" ) ) &&
156 (f0r_deinit= dlsym(handle,"f0r_deinit" ) )
157 ){
158
159 f0r_update2=dlsym(handle,"f0r_update2");
160
161 f0r_plugin_info_t info;
162 f0r_get_plugin_info(&info);
163
164 void* ret=NULL;
165 mlt_properties properties=NULL;
166
167 if (type == filter_type && info.plugin_type == F0R_PLUGIN_TYPE_FILTER ){
168 mlt_filter this = mlt_filter_new( );
169 if ( this != NULL )
170 {
171 this->process = filter_process;
172 this->close = filter_close;
173 f0r_init();
174 properties=MLT_FILTER_PROPERTIES ( this );
175
176 for (i=0;i<info.num_params;i++){
177 f0r_param_info_t pinfo;
178 f0r_get_param_info(&pinfo,i);
179
180 }
181
182 ret=this;
183 }
184 }else if (type == transition_type && info.plugin_type == F0R_PLUGIN_TYPE_MIXER2){
185 mlt_transition transition = mlt_transition_new( );
186 if ( transition != NULL )
187 {
188 transition->process = transition_process;
189 transition->close = transition_close;
190 properties=MLT_TRANSITION_PROPERTIES( transition );
191 mlt_properties_set_int(properties, "_transition_type", 1 );
192
193 ret=transition;
194 }
195 }
196 mlt_properties_set_data(properties, "_dlclose_handle", handle , sizeof (void*) , NULL , NULL );
197 mlt_properties_set_data(properties, "_dlclose", dlclose , sizeof (void*) , NULL , NULL );
198 mlt_properties_set_data(properties, "f0r_construct", f0r_construct , sizeof(void*),NULL,NULL);
199 mlt_properties_set_data(properties, "f0r_update", f0r_update , sizeof(void*),NULL,NULL);
200 if (f0r_update2)
201 mlt_properties_set_data(properties, "f0r_update2", f0r_update2 , sizeof(void*),NULL,NULL);
202 mlt_properties_set_data(properties, "f0r_destruct", f0r_destruct , sizeof(void*),NULL,NULL);
203 mlt_properties_set_data(properties, "f0r_get_plugin_info", f0r_get_plugin_info , sizeof(void*),NULL,NULL);
204 mlt_properties_set_data(properties, "f0r_get_param_info", f0r_get_param_info , sizeof(void*),NULL,NULL);
205 mlt_properties_set_data(properties, "f0r_set_param_value", f0r_set_param_value , sizeof(void*),NULL,NULL);
206 mlt_properties_set_data(properties, "f0r_get_param_value", f0r_get_param_value , sizeof(void*),NULL,NULL);
207
208
209 return ret;
210 }else{
211 printf("some was wrong\n");
212 dlerror();
213 }
214 return NULL;
215 }
216
217 static void * create_frei0r_item ( mlt_profile profile, mlt_service_type type, const char *id, void *arg){
218
219 mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
220 int dircount=mlt_tokeniser_parse_new (
221 tokeniser,
222 getenv("MLT_FREI0R_PLUGIN_PATH") ? getenv("MLT_FREI0R_PLUGIN_PATH") : FREI0R_PLUGIN_PATH,
223 ":"
224 );
225 void* ret=NULL;
226 while (dircount--){
227 char soname[1024]="";
228
229 char *save_firstptr = NULL;
230 char *firstname=strtok_r(strdup(id),".",&save_firstptr);
231
232 firstname=strtok_r(NULL,".",&save_firstptr);
233 sprintf(soname,"%s/%s.so", mlt_tokeniser_get_string( tokeniser , dircount ) , firstname );
234
235 if (firstname){
236
237 void* handle=dlopen(soname,RTLD_LAZY);
238
239 if (handle ){
240 ret=load_lib ( profile , type , handle );
241 }else{
242 dlerror();
243 }
244 }
245 }
246 mlt_tokeniser_close ( tokeniser );
247 return ret;
248 }
249
250
251 MLT_REPOSITORY
252 {
253 int i=0;
254 mlt_tokeniser tokeniser = mlt_tokeniser_init ( );
255 int dircount=mlt_tokeniser_parse_new (
256 tokeniser ,
257 getenv("MLT_FREI0R_PLUGIN_PATH") ? getenv("MLT_FREI0R_PLUGIN_PATH") : FREI0R_PLUGIN_PATH,
258 ":"
259 );
260
261 while (dircount--){
262
263 mlt_properties direntries = mlt_properties_new();
264 char* dirname = mlt_tokeniser_get_string ( tokeniser , dircount ) ;
265 mlt_properties_dir_list(direntries, dirname ,"*.so",1);
266
267 for (i=0;i<mlt_properties_count(direntries);i++){
268 char* name=mlt_properties_get_value(direntries,i);
269 char* shortname=name+strlen(dirname)+1;
270 char fname[1024]="";
271
272 strcat(fname,dirname);
273 strcat(fname,shortname);
274
275 char *save_firstptr = NULL;
276 char pluginname[1024]="frei0r.";
277 char* firstname = strtok_r ( shortname , "." , &save_firstptr );
278 strcat(pluginname,firstname);
279
280 void* handle=dlopen(strcat(name,".so"),RTLD_LAZY);
281 if (handle){
282 void (*plginfo)(f0r_plugin_info_t*)=dlsym(handle,"f0r_get_plugin_info");
283
284 if (plginfo){
285 f0r_plugin_info_t info;
286 plginfo(&info);
287
288 if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_FILTER){
289 MLT_REGISTER( filter_type, pluginname, create_frei0r_item );
290 MLT_REGISTER_METADATA( filter_type, pluginname, fill_param_info, strdup(name) );
291 }
292 else if (firstname && info.plugin_type==F0R_PLUGIN_TYPE_MIXER2 ){
293 MLT_REGISTER( transition_type, pluginname, create_frei0r_item );
294 MLT_REGISTER_METADATA( transition_type, pluginname, fill_param_info, strdup(name) );
295 }
296 }
297 dlclose(handle);
298 }
299 }
300 mlt_properties_close(direntries);
301 }
302 mlt_tokeniser_close ( tokeniser );
303 }