- 论坛徽章:
- 0
|
目前根据TQ2440开发板的源码已经搭建好基本的嵌入式linux平台;试图读懂并且定制自己想要的网络摄像机功能,当然主要是linux平台上的C代码;但是遇到一些问题:
1、对于linux C变成中用到的一些库函数(不知道这样说是不是恰当)比较陌生,不知道其具体作用,源码读起来比较吃力,比如“getopt_long_only()”、“dlopen”、“dlsym”;
2、对于linux系统下应用程序执行的过程比较模糊;比如说:以前在裸机上去开发,程序一般都是顺序执行外加一些异常处理;在linux的平台下面,有时候弄不清楚一段代码怎么执行到,比如在一个main函数中,有一个while(1),在while(1)的后面还有一些代码,实际运行的时候,后面的这部分代码确实运行到了(打印结果看到),但是不能理解他是怎么执行到的。
int main(int argc, char *argv[])
{
char *input = "input_uvc.so --resolution 640x480 --fps 5 --device /dev/video0";
char *output[MAX_OUTPUT_PLUGINS];
int daemon=0, i;
size_t tmp=0;
output[0] = "output_http.so --port 8080";
global.outcnt = 0;
global.control = control;
/* parameter parsing */
while(1) {
int option_index = 0, c=0;
static struct option long_options[] = \
{
{"h", no_argument, 0, 0},
{"help", no_argument, 0, 0},
{"i", required_argument, 0, 0},
{"input", required_argument, 0, 0},
{"o", required_argument, 0, 0},
{"output", required_argument, 0, 0},
{"v", no_argument, 0, 0},
{"version", no_argument, 0, 0},
{"b", no_argument, 0, 0},
{"background", no_argument, 0, 0},
{0, 0, 0, 0}
};
c = getopt_long_only(argc, argv, "", long_options, &option_index);
/* no more options to parse */
if (c == -1) break;
/* unrecognized option */
if(c=='?'){ help(argv[0]); return 0; }
switch (option_index) {
/* h, help */
case 0:
case 1:
help(argv[0]);
return 0;
break;
/* i, input */
case 2:
case 3:
input = strdup(optarg);
break;
/* o, output */
case 4:
case 5:
output[global.outcnt++] = strdup(optarg);
break;
/* v, version */
case 6:
case 7:
printf("MJPG Streamer Version: %s\n" \
"Compilation Date.....: %s\n" \
"Compilation Time.....: %s\n", SOURCE_VERSION, __DATE__, __TIME__);
return 0;
break;
/* b, background */
case 8:
case 9:
daemon=1;
break;
default:
help(argv[0]);
return 0;
}
}
openlog("MJPG-streamer ", LOG_PID|LOG_CONS, LOG_USER);
//openlog("MJPG-streamer ", LOG_PID|LOG_CONS|LOG_PERROR, LOG_USER);
syslog(LOG_INFO, "starting application");
/* fork to the background */
if ( daemon ) {
LOG("enabling daemon mode");
daemon_mode();
}
/* initialise the global variables */
global.stop = 0;
global.buf = NULL;
global.size = 0;
global.in.plugin = NULL;
/* this mutex and the conditional variable are used to synchronize access to the global picture buffer */
if( pthread_mutex_init(&global.db, NULL) != 0 ) {
LOG("could not initialize mutex variable\n");
closelog();
exit(EXIT_FAILURE);
}
if( pthread_cond_init(&global.db_update, NULL) != 0 ) {
LOG("could not initialize condition variable\n");
closelog();
exit(EXIT_FAILURE);
}
/* ignore SIGPIPE (send by OS if transmitting to closed TCP sockets) */
signal(SIGPIPE, SIG_IGN);
/* register signal handler for <CTRL>+C in order to clean up */
if (signal(SIGINT, signal_handler) == SIG_ERR) {
LOG("could not register signal handler\n");
closelog();
exit(EXIT_FAILURE);
}
/*
* messages like the following will only be visible on your terminal
* if not running in daemon mode
*/
LOG("MJPG Streamer Version.: %s\n", SOURCE_VERSION);
/* check if at least one output plugin was selected */
if ( global.outcnt == 0 ) {
/* no? Then use the default plugin instead */
global.outcnt = 1;
}
/* open input plugin */
tmp = (size_t)(strchr(input, ' ')-input);
global.in.plugin = (tmp > 0)?strndup(input, tmp):strdup(input);
global.in.handle = dlopen(global.in.plugin, RTLD_LAZY);
if ( !global.in.handle ) {
LOG("ERROR: could not find input plugin\n");
LOG(" Perhaps you want to adjust the search path with:\n");
LOG(" # export LD_LIBRARY_PATH=/path/to/plugin/folder\n");
LOG(" dlopen: %s\n", dlerror() );
closelog();
exit(EXIT_FAILURE);
}
global.in.init = dlsym(global.in.handle, "input_init");
if ( global.in.init == NULL ) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
global.in.stop = dlsym(global.in.handle, "input_stop");
if ( global.in.stop == NULL ) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
global.in.run = dlsym(global.in.handle, "input_run");
if ( global.in.run == NULL ) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
/* try to find optional command */
global.in.cmd = dlsym(global.in.handle, "input_cmd");
global.in.param.parameter_string = strchr(input, ' ');
global.in.param.global = &global;
if ( global.in.init(&global.in.param) ) {
LOG("input_init() return value signals to exit");
closelog();
exit(0);
}
/* open output plugin */
for (i=0; i<global.outcnt; i++) {
tmp = (size_t)(strchr(output, ' ')-output);
global.out.plugin = (tmp > 0)?strndup(output, tmp):strdup(output);
global.out.handle = dlopen(global.out.plugin, RTLD_LAZY);
if ( !global.out.handle ) {
LOG("ERROR: could not find output plugin %s\n", global.out.plugin);
LOG(" Perhaps you want to adjust the search path with:\n");
LOG(" # export LD_LIBRARY_PATH=/path/to/plugin/folder\n");
LOG(" dlopen: %s\n", dlerror() );
closelog();
exit(EXIT_FAILURE);
}
global.out.init = dlsym(global.out.handle, "output_init");
if ( global.out.init == NULL ) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
global.out.stop = dlsym(global.out.handle, "output_stop");
if ( global.out.stop == NULL ) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
global.out.run = dlsym(global.out.handle, "output_run");
if ( global.out.run == NULL ) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
/* try to find optional command */
global.out.cmd = dlsym(global.out.handle, "output_cmd");
global.out.param.parameter_string = strchr(output, ' ');
global.out.param.global = &global;
global.out.param.id = i;
if ( global.out.init(&global.out.param) ) {
LOG("output_init() return value signals to exit");
closelog();
exit(0);
}
}
/* start to read the input, push pictures into global buffer */
DBG("starting input plugin\n");
syslog(LOG_INFO, "starting input plugin");
if ( global.in.run() ) {
LOG("can not run input plugin\n");
closelog();
return 1;
}
DBG("starting %d output plugin(s)\n", global.outcnt);
for(i=0; i<global.outcnt; i++) {
syslog(LOG_INFO, "starting output plugin: %s (ID: %02d)", global.out.plugin, global.out.param.id);
global.out.run(global.out.param.id);
}
/* wait for signals */
pause();
return 0;
}
目前的情况:
对于问题1,函数的功能在网上很容易了解到,只是多花点时间在相关函数的熟悉上,这个倒不是问题。
但是对于问题2,自己目前比较困惑。当然是自己刚刚接触这一块内容不熟悉无可厚非,但是必须想一个办法去解决;当前自己有两个方面的打算:
1、想买一些操作系统原理方面的书籍,仔细的读一读,希望各位前辈指点一下,有哪些书比较好(我知道有一个《操作系统原理Linux篇》,不过网上好像都缺货了);
2、继续去看一些实际的源码,不懂的地方来坛子里问,到网上查,不明白的地方坚决弄明白;
|
|