1 /*
2 * dl_loader.c
3 *
4 * Copyright (C) Thomas Oestreich - June 2001
5 *
6 * This file is part of transcode, a video stream processing tool
7 *
8 * transcode is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * transcode is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Make; see the file COPYING. If not, write to
20 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #ifdef HAVE_DLFCN_H
28 #include <dlfcn.h>
29 #else
30 # ifdef OS_DARWIN
31 # include "libdldarwin/dlfcn.h"
32 # endif
33 #endif
34
35 #include "transcode.h"
36 #include "dl_loader.h"
37
38 const char *mod_path = MOD_PATH;
39
40 char module[TC_BUF_MAX];
41
42 int (*TCV_export)(int opt, void *para1, void *para2);
43 int (*TCA_export)(int opt, void *para1, void *para2);
44 int (*TCV_import)(int opt, void *para1, void *para2);
45 int (*TCA_import)(int opt, void *para1, void *para2);
46
watch_export_module(const char * s,int opt,transfer_t * para)47 static void watch_export_module(const char *s, int opt, transfer_t *para)
48 {
49 tc_log_msg(__FILE__, "module=%s [option=%02d, flag=%d]", s, opt, ((para==NULL)? -1:para->flag));
50 }
51
watch_import_module(const char * s,int opt,transfer_t * para)52 static void watch_import_module(const char *s, int opt, transfer_t *para)
53 {
54 tc_log_msg(__FILE__, "module=%s [option=%02d, flag=%d]", s, opt, ((para==NULL)? -1:para->flag));
55 fflush(stdout);
56 }
57
tcv_export(int opt,void * para1,void * para2)58 int tcv_export(int opt, void *para1, void *para2)
59 {
60
61 int ret;
62
63 if(verbose & TC_WATCH)
64 watch_export_module("tcv_export", opt, (transfer_t*) para1);
65
66 ret = TCV_export(opt, para1, para2);
67
68 if(ret==TC_EXPORT_ERROR && (verbose & TC_DEBUG))
69 tc_log_msg(__FILE__, "video export module error");
70
71 if(ret==TC_EXPORT_UNKNOWN && (verbose & TC_DEBUG))
72 tc_log_msg(__FILE__, "option %d unsupported by video export module", opt);
73
74 return(ret);
75 }
76
tca_export(int opt,void * para1,void * para2)77 int tca_export(int opt, void *para1, void *para2)
78 {
79
80 int ret;
81
82 if(verbose & TC_WATCH)
83 watch_export_module("tca_export", opt, (transfer_t*) para1);
84
85 ret = TCA_export(opt, para1, para2);
86
87 if(ret==TC_EXPORT_ERROR && (verbose & TC_DEBUG))
88 tc_log_msg(__FILE__, "audio export module error");
89
90 if(ret==TC_EXPORT_UNKNOWN && (verbose & TC_DEBUG))
91 tc_log_msg(__FILE__, "option %d unsupported by audio export module", opt);
92
93 return(ret);
94 }
95
tcv_import(int opt,void * para1,void * para2)96 int tcv_import(int opt, void *para1, void *para2)
97 {
98
99 int ret;
100
101 if(verbose & TC_WATCH)
102 watch_import_module("tcv_import", opt, (transfer_t*) para1);
103
104 ret = TCV_import(opt, para1, para2);
105
106 if(ret==TC_IMPORT_ERROR && (verbose & TC_DEBUG))
107 tc_log_msg(__FILE__, "video import module error");
108
109 if(ret==TC_IMPORT_UNKNOWN && (verbose & TC_DEBUG))
110 tc_log_msg(__FILE__, "option %d unsupported by video import module", opt);
111
112 return(ret);
113 }
114
tca_import(int opt,void * para1,void * para2)115 int tca_import(int opt, void *para1, void *para2)
116 {
117 int ret;
118
119 if(verbose & TC_WATCH)
120 watch_import_module("tca_import", opt, (transfer_t*) para1);
121
122 ret = TCA_import(opt, para1, para2);
123
124 if(ret==TC_IMPORT_ERROR && (verbose & TC_DEBUG))
125 tc_log_msg(__FILE__, "audio import module error");
126
127 if(ret==TC_IMPORT_UNKNOWN && (verbose & TC_DEBUG))
128 tc_log_msg(__FILE__, "option %d unsupported by audio import module", opt);
129
130 return(ret);
131 }
132
133
load_module(const char * mod_name,int mode)134 void *load_module(const char *mod_name, int mode)
135 {
136 const char *error;
137 void *handle;
138
139 if(mode & TC_EXPORT) {
140
141 tc_snprintf(module, sizeof(module), "%s/export_%s.so", ((mod_path==NULL)? TC_DEFAULT_MOD_PATH:mod_path), mod_name);
142
143 if(verbose & TC_DEBUG)
144 tc_log_msg(__FILE__, "loading %s export module %s", ((mode & TC_VIDEO)? "video": "audio"), module);
145
146 handle = dlopen(module, RTLD_GLOBAL| RTLD_LAZY);
147
148 if (!handle) {
149 error=dlerror();
150 tc_warn("%s", error);
151 tc_warn("(%s) loading \"%s\" failed", __FILE__, module);
152 return(NULL);
153 }
154
155 if(mode & TC_VIDEO) {
156 TCV_export = dlsym(handle, "tc_export");
157 error = dlerror();
158 if (error != NULL) {
159 tc_warn("%s", error);
160 return(NULL);
161 }
162 }
163
164 if(mode & TC_AUDIO) {
165 TCA_export = dlsym(handle, "tc_export");
166 error = dlerror();
167 if (error != NULL) {
168 tc_warn("%s", error);
169 return(NULL);
170 }
171 }
172
173 return(handle);
174 }
175
176
177 if(mode & TC_IMPORT) {
178
179 tc_snprintf(module, sizeof(module), "%s/import_%s.so", ((mod_path==NULL)? TC_DEFAULT_MOD_PATH:mod_path), mod_name);
180
181 if(verbose & TC_DEBUG)
182 tc_log_msg(__FILE__, "loading %s import module %s", ((mode & TC_VIDEO)? "video": "audio"), module);
183
184 handle = dlopen(module, RTLD_GLOBAL| RTLD_LAZY);
185
186 if (!handle) {
187 error = dlerror();
188 tc_warn("%s", error);
189 return(NULL);
190 }
191
192 if(mode & TC_VIDEO) {
193 TCV_import = dlsym(handle, "tc_import");
194 if ((error = dlerror()) != NULL) {
195 tc_warn("%s", error);
196 return(NULL);
197 }
198 }
199
200
201 if(mode & TC_AUDIO) {
202 TCA_import = dlsym(handle, "tc_import");
203 if ((error = dlerror()) != NULL) {
204 tc_warn("%s", error);
205 return(NULL);
206 }
207 }
208
209 return(handle);
210 }
211
212 // wrong mode?
213 return(NULL);
214 }
215
unload_module(void * handle)216 void unload_module(void *handle)
217 {
218 if (dlclose(handle) != 0) {
219 perror("unloading module");
220 }
221 handle=NULL;
222 }
223
224