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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/* fswebcam - FireStorm.cx's webcam generator */
/*============================================================*/
/* Copyright (C)2005-2011 Philip Heron <phil@sanslogic.co.uk> */
/* */
/* This program is distributed under the terms of the GNU */
/* General Public License, version 2. You may use, modify, */
/* and redistribute it under the terms of this license. A */
/* copy should be included with this source. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include "parse.h"
#include "src.h"
#include "log.h"
#ifdef HAVE_V4L2
extern src_mod_t src_v4l2;
#endif
#ifdef HAVE_V4L1
extern src_mod_t src_v4l1;
#endif
extern src_mod_t src_file;
extern src_mod_t src_raw;
extern src_mod_t src_test;
/* Modules should be listed here in order of preference. */
src_mod_t* src_mod[] = {
#ifdef HAVE_V4L2
&src_v4l2,
#endif
#ifdef HAVE_V4L1
&src_v4l1,
#endif
&src_file,
&src_raw,
&src_test,
0
};
/* Supported palette types. */
src_palette_t src_palette[] = {
{ "PNG" },
{ "JPEG" },
{ "MJPEG" },
{ "S561" },
{ "RGB32" },
{ "BGR32" },
{ "RGB24" },
{ "BGR24" },
{ "YUYV" },
{ "UYVY" },
{ "YUV420P" },
{ "NV12MB" },
{ "BAYER" },
{ "SGBRG8" },
{ "SGRBG8" },
{ "RGB565" },
{ "RGB555" },
{ "Y16" },
{ "GREY" },
{ NULL }
};
int src_open(src_t *src, char *source)
{
int i;
size_t sl;
char *s;
struct stat st;
if(!source)
{
ERROR("No source was specified.");
return(-1);
}
sl = strlen(source) + 1;
s = malloc(sl);
if(!s)
{
ERROR("Out of memory.");
return(-1);
}
/* Get the first part of the source. */
if(argncpy(s, sl, source, ":", 0, 0))
{
ERROR("No source was specified.");
free(s);
return(-1);
}
/* Check if it's a source name. */
i = 0;
while(src_mod[i])
{
if(!strcasecmp(s, src_mod[i]->name))
{
INFO(">>> Using '%s' source module.", src_mod[i]->name);
src->type = i;
src->source = NULL;
if(!argncpy(s, sl, source, ":", 1, 0)) src->source = s;
else free(s);
i = src_mod[src->type]->open(src);
if(i < 0)
{
if(src->source) free(src->source);
return(-1);
}
return(0);
}
i++;
}
/* No source type was specified. If the name is that of a file or
* device we can check each source until we find one that works. */
if(stat(s, &st))
{
ERROR("stat: %s", strerror(errno));
free(s);
return(-1);
}
i = 0;
src->source = s;
while(src_mod[i])
{
int r = src_mod[i]->flags;
if(S_ISCHR(st.st_mode) && r & SRC_TYPE_DEVICE) r = -1;
else if(!S_ISCHR(st.st_mode) && r & SRC_TYPE_FILE) r = -1;
else r = 0;
if(r)
{
MSG("Trying source module %s...", src_mod[i]->name);
src->type = i;
r = src_mod[src->type]->open(src);
if(r != -2) return(r);
}
i++;
}
ERROR("Unable to find a source module that can read %s.", source);
free(s);
return(-1);
}
int src_close(src_t *src)
{
int r;
if(src->captured_frames)
{
double seconds =
(src->tv_last.tv_sec + src->tv_last.tv_usec / 1000000.0) -
(src->tv_first.tv_sec + src->tv_first.tv_usec / 1000000.0);
/* Display FPS if enough frames where captured. */
if(src->captured_frames == 1)
{
MSG("Captured frame in %0.2f seconds.", seconds);
}
else if(src->captured_frames < 3)
{
MSG("Captured %i frames in %0.2f seconds.",
src->captured_frames, seconds);
}
else
{
MSG("Captured %i frames in %0.2f seconds. (%i fps)",
src->captured_frames, seconds,
(int) (src->captured_frames / seconds));
}
}
r = src_mod[src->type]->close(src);
if(src->source) free(src->source);
return(r);
}
int src_grab(src_t *src)
{
int r = src_mod[src->type]->grab(src);
if(!r)
{
if(!src->captured_frames) gettimeofday(&src->tv_first, NULL);
gettimeofday(&src->tv_last, NULL);
src->captured_frames++;
}
return(r);
}
/* Pointers are great things. Terrible things yes, but great. */
/* These work but are very ugly and will be re-written soon. */
int src_set_option(src_option_t ***options, char *name, char *value)
{
src_option_t **opts, *opt;
int count;
if(!options) return(-1);
if(!*options)
{
*options = malloc(sizeof(src_option_t *));
if(!*options)
{
ERROR("Out of memory.");
return(-1);
}
*options[0] = NULL;
}
count = 0;
opts = *options;
while(*opts)
{
if((*opts)->name) if(!strcasecmp(name, (*opts)->name)) break;
opts++;
count++;
}
if(!*opts)
{
void *new;
opt = (src_option_t *) malloc(sizeof(src_option_t));
if(!opt)
{
ERROR("Out of memory.");
return(-1);
}
new = realloc(*options, sizeof(src_option_t *) * (count + 2));
if(!new)
{
free(opt);
ERROR("Out of memory.");
return(-1);
}
*options = (src_option_t **) new;
(*options)[count++] = opt;
(*options)[count++] = NULL;
opt->name = strdup(name);
opt->value = NULL;
}
else opt = *opts;
if(opt->value)
{
free(opt->value);
opt->value = NULL;
}
if(value) opt->value = strdup(value);
return(0);
}
int src_get_option_by_number(src_option_t **opt, int number,
char **name, char **value)
{
int i;
if(!opt || !name || !value) return(-1);
i = 0;
while(*opt)
{
if(i == number)
{
*name = (*opt)->name;
*value = (*opt)->value;
return(0);
}
i++;
}
return(-1);
}
int src_get_option_by_name(src_option_t **opt, char *name, char **value)
{
if(!opt || !name || !value) return(-1);
while(*opt)
{
if((*opt)->name)
{
if(!strcasecmp(name, (*opt)->name))
{
*value = (*opt)->value;
return(0);
}
}
opt++;
}
return(-1);
}
int src_free_options(src_option_t ***options)
{
src_option_t **opts;
if(!options || !*options) return(-1);
opts = *options;
while(*opts)
{
if((*opts)->name) free((*opts)->name);
if((*opts)->value) free((*opts)->value);
free(*opts);
opts++;
}
free(*options);
*options = NULL;
return(0);
}
|