1 /*******************************************************************************
2 # Linuc-UVC streaming input-plugin for MJPG-streamer                           #
3 #                                                                              #
4 # This package work with the Logitech UVC based webcams with the mjpeg feature #
5 #                                                                              #
6 # Copyright (C) 2005 2006 Laurent Pinchart &&  Michel Xhaard                   #
7 #                    2007 Lucas van Staden                                     #
8 #                    2007 Tom Stöveken                                         #
9 #                                                                              #
10 # This program is free software; you can redistribute it and/or modify         #
11 # it under the terms of the GNU General Public License as published by         #
12 # the Free Software Foundation; either version 2 of the License, or            #
13 # (at your option) any later version.                                          #
14 #                                                                              #
15 # This program is distributed in the hope that it will be useful,              #
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of               #
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
18 # GNU General Public License for more details.                                 #
19 #                                                                              #
20 # You should have received a copy of the GNU General Public License            #
21 # along with this program; if not, write to the Free Software                  #
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA    #
23 #                                                                              #
24 *******************************************************************************/
25 
26 #ifndef V4L2_UVC_H
27 #define V4L2_UVC_H
28 
29 
30 #include <stdio.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <sys/ioctl.h>
37 #include <sys/mman.h>
38 #include <sys/select.h>
39 
40 #include <linux/types.h>          /* for videodev2.h */
41 #include <linux/videodev2.h>
42 
43 #include "../../mjpg_streamer.h"
44 #define NB_BUFFER 4
45 
46 
47 #define IOCTL_RETRY 4
48 
49 /* ioctl with a number of retries in the case of I/O failure
50 * args:
51 * fd - device descriptor
52 * IOCTL_X - ioctl reference
53 * arg - pointer to ioctl data
54 * returns - ioctl result
55 */
56 int xioctl(int fd, int IOCTL_X, void *arg);
57 
58 #ifdef USE_LIBV4L2
59 #include <libv4l2.h>
60 #define IOCTL_VIDEO(fd, req, value) v4l2_ioctl(fd, req, value)
61 #define OPEN_VIDEO(fd, flags) v4l2_open(fd, flags)
62 #define CLOSE_VIDEO(fd) v4l2_close(fd)
63 #else
64 #define IOCTL_VIDEO(fd, req, value) ioctl(fd, req, value)
65 #define OPEN_VIDEO(fd, flags) open(fd, flags)
66 #define CLOSE_VIDEO(fd) close(fd)
67 #endif
68 
69 typedef enum _streaming_state streaming_state;
70 enum _streaming_state {
71     STREAMING_OFF = 0,
72     STREAMING_ON = 1,
73     STREAMING_PAUSED = 2,
74 };
75 
76 struct vdIn {
77     int fd;
78     char *videodevice;
79     char *status;
80     char *pictName;
81     struct v4l2_capability cap;
82     struct v4l2_format fmt;
83     struct v4l2_buffer buf;
84     struct v4l2_requestbuffers rb;
85     void *mem[NB_BUFFER];
86     unsigned char *tmpbuffer;
87     unsigned char *framebuffer;
88     streaming_state streamingState;
89     int grabmethod;
90     int width;
91     int height;
92     int fps;
93     int formatIn;
94     int formatOut;
95     int framesizeIn;
96     int signalquit;
97     int toggleAvi;
98     int getPict;
99     int rawFrameCapture;
100     /* raw frame capture */
101     unsigned int fileCounter;
102     /* raw frame stream capture */
103     unsigned int rfsFramesWritten;
104     unsigned int rfsBytesWritten;
105     /* raw stream capture */
106     FILE *captureFile;
107     unsigned int framesWritten;
108     unsigned int bytesWritten;
109     int framecount;
110     int recordstart;
111     int recordtime;
112     uint32_t tmpbytesused;
113     struct timeval tmptimestamp;
114     v4l2_std_id vstd;
115     unsigned long frame_period_time; // in ms
116     unsigned char soft_framedrop;
117     unsigned int dv_timings;
118 };
119 
120 /* optional initial settings */
121 typedef struct {
122     int quality_set, quality,
123         sh_set, sh,
124         co_set, co,
125         br_set, br_auto, br,
126         sa_set, sa,
127         wb_set, wb_auto, wb,
128         ex_set, ex_auto, ex,
129         bk_set, bk,
130         rot_set, rot,
131         hf_set, hf,
132         vf_set, vf,
133         pl_set, pl,
134         gain_set, gain_auto, gain,
135         cagc_set, cagc_auto, cagc,
136         cb_set, cb_auto, cb;
137 } context_settings;
138 
139 /* context of each camera thread */
140 typedef struct {
141     int id;
142     globals *pglobal;
143     pthread_t threadID;
144     pthread_mutex_t controls_mutex;
145     struct vdIn *videoIn;
146     context_settings *init_settings;
147 } context;
148 
149 int init_videoIn(struct vdIn *vd, char *device, int width, int height, int fps, int format, int grabmethod, globals *pglobal, int id, v4l2_std_id vstd);
150 void enumerateControls(struct vdIn *vd, globals *pglobal, int id);
151 void control_readed(struct vdIn *vd, struct v4l2_queryctrl *ctrl, globals *pglobal, int id);
152 int setResolution(struct vdIn *vd, int width, int height);
153 
154 int memcpy_picture(unsigned char *out, unsigned char *buf, int size);
155 int uvcGrab(struct vdIn *vd);
156 int close_v4l2(struct vdIn *vd);
157 
158 int video_enable(struct vdIn *vd);
159 int video_set_dv_timings(struct vdIn *vd);
160 int video_handle_event(struct vdIn *vd);
161 
162 int v4l2GetControl(struct vdIn *vd, int control);
163 int v4l2SetControl(struct vdIn *vd, int control, int value, int plugin_number, globals *pglobal);
164 int v4l2UpControl(struct vdIn *vd, int control);
165 int v4l2DownControl(struct vdIn *vd, int control);
166 int v4l2ToggleControl(struct vdIn *vd, int control);
167 int v4l2ResetControl(struct vdIn *vd, int control);
168 
169 #endif
170