File: v4l2-plugin-android.c

package info (click to toggle)
v4l-utils 1.32.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,276 kB
  • sloc: ansic: 85,528; cpp: 69,473; perl: 11,915; sh: 1,333; python: 883; php: 119; makefile: 39
file content (148 lines) | stat: -rw-r--r-- 4,601 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (C) 2010 Nokia Corporation <multimedia@maemo.org>

* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdarg.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <glob.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include "libv4l2.h"
#include "libv4l2-priv.h"
#include "libv4l-plugin.h"

/* libv4l plugin support:
   it is provided by functions v4l2_plugin_[open,close,etc].

   When open() is called libv4l dlopens files in /usr/lib[64]/libv4l/plugins
   1 at a time and call open callback passing through the applications
   parameters unmodified.

   If a plugin is relevant for the specified device node, it can indicate so
   by returning a value other than -1 (the actual file descriptor).
   As soon as a plugin returns another value than -1 plugin loading stops and
   information about it (fd and corresponding library handle) is stored. For
   each function v4l2_[ioctl,read,close,etc] is called corresponding
   v4l2_plugin_* function which looks if there is loaded plugin for that file
   and call it's callbacks.

   v4l2_plugin_* function indicates by it's first argument if plugin was used,
   and if it was not then v4l2_* functions proceed with their usual behavior.
*/

/* list of plugin search paths */
static const char *g_plugin_search_paths[] = {
	"/system/lib/libv4l/plugins",
	"/vendor/lib/libv4l/plugins",
	NULL /* list terminator */
};

void v4l2_plugin_init(int fd, void **plugin_lib_ret, void **plugin_priv_ret,
			  const struct libv4l_dev_ops **dev_ops_ret)
{
	char *error;
	void *plugin_library = NULL;
	const struct libv4l_dev_ops *libv4l2_plugin = NULL;
	DIR *plugin_dir = NULL;
	struct dirent *entry;
	char *suffix = NULL;
	int length, i;
	char filename[256];

	/* initialize output params */
	*dev_ops_ret = v4lconvert_get_default_dev_ops();
	*plugin_lib_ret = NULL;
	*plugin_priv_ret = NULL;

	/* read the plugin directory for "*.so" files */
	for (i = 0; g_plugin_search_paths[i] != NULL; i++) {
		plugin_dir = opendir(g_plugin_search_paths[i]);
		if (plugin_dir == NULL) {
			V4L2_LOG_ERR("PLUGIN: opening plugin directory (%s) failed\n",
				g_plugin_search_paths[i]);
			continue;
		}

		while (entry = readdir(plugin_dir)) {
			/* get last 3 letter suffix from the filename */
			length = strlen(entry->d_name);
			if (length > 3)
				suffix = entry->d_name + (length - 3);

			if (!suffix || strcmp(suffix, ".so")) {
				suffix = NULL; /* reset for next iteration */
				continue;
			}

			/* load library and get desired symbol */
			sprintf(filename, "%s/%s", g_plugin_search_paths[i], entry->d_name);
			V4L2_LOG("PLUGIN: dlopen(%s);\n", filename);
			plugin_library = dlopen(filename, RTLD_LAZY);
			if (!plugin_library)
				continue;

			dlerror(); /* Clear any existing error */
			libv4l2_plugin = (struct libv4l_dev_ops *)
				dlsym(plugin_library, "libv4l2_plugin");
			error = dlerror();
			if (error != NULL) {
				V4L2_LOG_ERR("PLUGIN: dlsym failed: %s\n", error);
				dlclose(plugin_library);
				continue;
			}

			if (!libv4l2_plugin->init ||
				!libv4l2_plugin->close ||
				!libv4l2_plugin->ioctl) {
				V4L2_LOG("PLUGIN: does not have all mandatory ops\n");
				dlclose(plugin_library);
				continue;
			}

			*plugin_priv_ret = libv4l2_plugin->init(fd);
			if (!*plugin_priv_ret) {
				V4L2_LOG("PLUGIN: plugin open() returned NULL\n");
				dlclose(plugin_library);
				continue;
			}

			/* exit loop when a suitable plugin is found */
			*plugin_lib_ret = plugin_library;
			*dev_ops_ret = libv4l2_plugin;
			break;
		}
		closedir(plugin_dir);

		/* exit loop when a suitable plugin is found */
		if (*plugin_lib_ret && *plugin_priv_ret && *dev_ops_ret)
			break;
	}
}

void v4l2_plugin_cleanup(void *plugin_lib, void *plugin_priv,
			 const struct libv4l_dev_ops *dev_ops)
{
	if (plugin_lib) {
		dev_ops->close(plugin_priv);
		dlclose(plugin_lib);
	}
}