]> git.sesse.net Git - vlc/blob - modules/access/v4l2/lib.c
v4l2: mark --v4l2-fps as obsolete
[vlc] / modules / access / v4l2 / lib.c
1 /*****************************************************************************
2  * lib.c : libv4l2 run-time
3  *****************************************************************************
4  * Copyright (C) 2012 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <pthread.h>
26 #include <dlfcn.h>
27 #include <unistd.h>
28 #include <sys/ioctl.h>
29 #include <sys/mman.h>
30
31 #include <vlc_common.h>
32
33 #include "v4l2.h"
34
35 static void *v4l2_handle = NULL;
36 static int (*v4l2_fd_open_) (int, int);
37 int (*v4l2_close) (int);
38 int (*v4l2_ioctl) (int, unsigned long int, ...);
39 ssize_t (*v4l2_read) (int, void *, size_t);
40 void * (*v4l2_mmap) (void *, size_t, int, int, int, int64_t);
41 int (*v4l2_munmap) (void *, size_t);
42
43 static int fd_open (int fd, int flags)
44 {
45     (void) flags;
46     return fd;
47 }
48
49 static void v4l2_lib_load (void)
50 {
51     void *h = dlopen ("libv4l2.so", RTLD_LAZY | RTLD_LOCAL);
52     if (h == NULL)
53         goto fallback;
54
55     v4l2_fd_open_ = dlsym (h, "v4l2_fd_open");
56     v4l2_close = dlsym (h, "v4l2_close");
57     v4l2_ioctl = dlsym (h, "v4l2_ioctl");
58     v4l2_read = dlsym (h, "v4l2_read");
59     v4l2_mmap = dlsym (h, "v4l2_mmap");
60     v4l2_munmap = dlsym (h, "v4l2_munmap");
61
62     if (v4l2_fd_open_ != NULL && v4l2_close != NULL && v4l2_ioctl != NULL
63      && v4l2_read != NULL && v4l2_mmap != NULL && v4l2_munmap != NULL)
64     {
65         v4l2_handle = h;
66         return;
67     }
68
69     dlclose (h);
70 fallback:
71     v4l2_fd_open_ = fd_open;
72     v4l2_close = close;
73     v4l2_ioctl = ioctl;
74     v4l2_read = read;
75     v4l2_mmap = mmap;
76     v4l2_munmap = munmap;
77 }
78
79 __attribute__((destructor))
80 static void v4l2_lib_unload (void)
81 {
82     if (v4l2_handle != NULL)
83         dlclose (v4l2_handle);
84 }
85
86 int v4l2_fd_open (int fd, int flags)
87 {
88     static pthread_once_t once = PTHREAD_ONCE_INIT;
89
90     pthread_once (&once, v4l2_lib_load);
91     return v4l2_fd_open_ (fd, flags);
92 }