-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
484 lines (413 loc) · 12 KB
/
main.c
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* The main file.
*
* This file is part of the camshot utility.
*
* Copyright (c) 2010 Gabriel Zabusek <gabriel.zabusek@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 3 of the License, or (at your option) any later version.
*/
/*
TODO-Important:
- add mmap support
TODO
- add png support
- add jpg support
*/
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif
#define _BSD_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <linux/videodev2.h>
#include <pthread.h>
#include <sys/time.h>
#include <errno.h>
#include "arguments.h"
#include "camera.h"
#include "image.h"
#include "shmem.h"
pthread_mutex_t capture_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
uint8_t *p_shm;
int shm_sem;
int total_buffers;
int stream_finish = 0;
pthread_t capture_thread, stream_thread;
void *capture_func( void *ptr);
void *stream_func(void *ptr);
void exit_program(int sig);
int main(int argc, char **argv)
{
int shmid;
int i,j;
char in_str[16];
process_arguments(argc, argv);
/* open the camera device */
if( (camera_fd = open(psz_video_dev, O_RDWR)) < 0 )
{
char error_buf[256];
sprintf(error_buf, "open() %s", psz_video_dev);
perror(error_buf);
exit(-1);
}
get_caps();
get_format();
if( b_verbose ) printf("Device opened.\n");
if( b_verbose )
{
printf("Video device:\t\t%s\n", psz_video_dev);
print_caps();
print_format();
printf("Ouput directory:\t%s\n", psz_output_dir);
printf("Image format:\t\t%s\n",str_formats[e_outfmt]);
printf("\n");
printf("Opening device %s\n", psz_video_dev);
if( b_named_filename )
{
printf("Ouput filename:\t%s\n", psz_output_filename);
}
else if( b_named_pipe )
{
printf("Using named pipe %s\n", psz_named_pipe);
}
if( b_shared_mem )
printf("Using shared memory. key = %i\n", shared_mem_key);
}
if( b_printinfo )
{
printf("Device info:\n");
print_caps();
print_format();
close(camera_fd);
exit(EXIT_SUCCESS);
}
(void)signal(SIGINT, exit_program);
if( b_shared_mem && b_named_pipe )
{
printf("WARNING: shared memory and named pipe can't be used together. Use more instances of camshot. Defaulting to named pipe.\n");
b_shared_mem = 0;
}
if( b_named_pipe )
{
int ret_val = mkfifo(psz_named_pipe, 0666);
if ((ret_val == -1) && (errno != EEXIST)) {
perror("Error creating the named pipe");
exit(EXIT_FAILURE);
}
}
if( req_width && req_height )
{
if( b_verbose )
printf("Trying to set resolution to %ux%u.\n", req_width, req_height);
if( set_width_height(req_width,req_height) == -1 )
printf("Unable to set the desired resolution.\n");
else
if( b_verbose )
printf("Resolution set to %ux%u\n", req_width, req_height);
} else {
get_format();
req_width = camera_format.fmt.pix.width;
req_height = camera_format.fmt.pix.height;
}
if( b_shared_mem )
{
if((shmid = shmget(shared_mem_key, req_width*req_height*3, IPC_CREAT | 0666)) < 0) {
perror("Error getting shared memory id");
exit(EXIT_FAILURE);
}
if((p_shm = (uint8_t *)shmat(shmid, NULL, 0)) == (void *) -1) {
perror("Error getting shared memory ptr");
exit(EXIT_FAILURE);
}
shm_sem = semget((key_t)shared_mem_key, 1, IPC_CREAT | 0666);
sem_set(&shm_sem);
if( b_verbose )
printf("Shared memory ID: %i\nSemaphore ID: %i\n", shmid, shm_sem);
}
total_buffers = req_mmap_buffers(2);
/* start the capture */
streaming_on();
/* let the camera self adjust by 'ignoring' 200 complete buffer queues */
printf("Letting the camera automaticaly adjust the picture:");
for(i=0; i<AUTO_ADJUST_TURNS; i++)
{
for(j=0; j<total_buffers; j++)
{
int ready_buf = dequeue_buffer();
/* don't queue the last buffers */
if( i<AUTO_ADJUST_TURNS-1 )
queue_buffer(ready_buf);
}
printf(".");
fflush(stdout);
}
printf("Done.\n");
if( b_shared_mem || b_named_pipe )
{
pthread_create(&stream_thread, NULL, &stream_func, NULL);
while(1)
{
pthread_join(stream_thread, NULL);
}
}
else
{
pthread_create(&capture_thread, NULL, &capture_func, NULL);
}
if( b_named_filename )
{
usleep(200000);
pthread_mutex_lock(&cond_mutex);
pthread_cond_signal(&condition);
pthread_mutex_unlock(&cond_mutex);
usleep(200000);
exit_program(SIGINT);
fflush(stdout);
return 0;
}
while( in_str[0] != 'q' )
{
printf("Command (h for help): ");
fflush(stdout);
if( fgets(in_str, 16, stdin) == NULL )
{
printf("Got NULL! Try again.\n");
continue;
}
switch(in_str[0])
{
case 'x':
pthread_mutex_lock(&cond_mutex);
pthread_cond_signal(&condition);
pthread_mutex_unlock(&cond_mutex);
break;
case 'h':
printf("\nCommands:\n");
printf("\tx\tCapture a picture from camera.\n");
printf("\th\tPrints this help.\n");
printf("\tq\tQuits the program.\n");
printf("\n");
break;
case 'q':
case '\n':
break;
default:
fprintf(stderr, "Unknown command %c\n", in_str[0]);
break;
}
}
/* Clean up */
exit_program(SIGINT);
return 0;
}
void exit_program(int sig)
{
printf("\tcamshot ending\n");
/* Clean up */
if( b_shared_mem || b_named_pipe )
{
stream_finish = 1;
pthread_join(stream_thread, NULL);
}
streaming_off();
unmap_buffers();
close(camera_fd);
if( b_verbose ) printf("Device closed.\n");
if( b_shared_mem )
{
shmdt(p_shm);
sem_del(&shm_sem);
}
exit(EXIT_SUCCESS);
}
void *capture_func(void *ptr)
{
unsigned char *rgb_buffer;
int ready_buf;
char cur_name[64];
int i;
struct timeval timestamp;
if( b_shared_mem )
{
rgb_buffer = p_shm;
}
else
{
rgb_buffer = (unsigned char *)malloc(req_width*req_height*3);
}
for(;;)
{
/* Wait for the start condition */
pthread_mutex_lock(&cond_mutex);
pthread_cond_wait(&condition, &cond_mutex);
pthread_mutex_unlock(&cond_mutex);
/* queue one buffer and 'refresh it' */
/* @todo Change 2 to some #define */
for(i=0; i<2; i++)
{
queue_buffer(i);
}
for(i=0; i<2 - 1; i++)
{
dequeue_buffer();
}
/* get the idx of ready buffer */
ready_buf = dequeue_buffer();
if( b_verbose )
{
printf("Buffer %d ready. Length: %uB\n", ready_buf, image_buffers[ready_buf].length);
}
switch( check_pixelformat() )
{
case V4L2_PIX_FMT_YUYV:
/* convert data to rgb */
if( convert_yuv_to_rgb_buffer(
(unsigned char *)(image_buffers[ready_buf].start),
rgb_buffer, req_width, req_height) == 0 )
{
if( b_verbose )
{
printf("\tConverted to rgb.\n");
}
}
break;
default:
print_pixelformat(stderr);
fprintf(stderr,"\n");
return NULL;
}
timestamp = query_buffer(0);
/* make the image */
/* create the file name */
if( b_named_filename )
{
sprintf(cur_name, "%s%s", psz_output_dir, psz_output_filename);
}
else if( !b_named_pipe )
sprintf(cur_name, "%scamshot_%lu.bmp", psz_output_dir, timestamp.tv_sec);
else
sprintf(cur_name, "%s", psz_named_pipe);
switch( e_outfmt )
{
case FORMAT_BMP:
make_bmp(rgb_buffer,
cur_name,
req_width,
req_height);
break;
case FORMAT_RGB:
make_rgb(rgb_buffer,
cur_name,
req_width,
req_height);
break;
default:
fprintf(stderr, "Not supported format requested!\n");
break;
}
}
return NULL;
}
void *stream_func(void *ptr)
{
unsigned char *rgb_buffer;
int ready_buf;
char cur_name[64];
int i;
if( b_shared_mem )
{
rgb_buffer = p_shm;
}
else
{
rgb_buffer = (unsigned char *)malloc(req_width*req_height*3);
}
for(i=0; i<total_buffers; i++)
{
queue_buffer(i);
}
for(;;)
{
/* get the idx of ready buffer */
for(i=0; i<total_buffers; i++)
{
/* Check if the thread should stop. */
if( stream_finish ) return NULL;
ready_buf = dequeue_buffer();
if( b_verbose )
{
printf("Buffer %d ready. Length: %uB\n", ready_buf, image_buffers[ready_buf].length);
}
switch( check_pixelformat() )
{
case V4L2_PIX_FMT_YUYV:
/* convert data to rgb */
if( b_shared_mem )
sem_down(&shm_sem);
if( convert_yuv_to_rgb_buffer(
(unsigned char *)(image_buffers[ready_buf].start),
rgb_buffer, req_width, req_height) == 0 )
{
if( b_verbose )
{
printf("\tConverted to rgb.\n");
}
}
if( b_shared_mem )
sem_up(&shm_sem);
break;
default:
print_pixelformat(stderr);
fprintf(stderr,"\n");
return NULL;
}
/* make the image */
/* create the file name */
if( b_named_pipe )
sprintf(cur_name, "%s", psz_named_pipe);
switch( e_outfmt )
{
case FORMAT_BMP:
if( b_shared_mem )
{
printf("Unsupported!\n");
break;
}
make_bmp(rgb_buffer,
cur_name,
req_width,
req_height);
break;
case FORMAT_RGB:
if( b_shared_mem )
{
/* The buffer is already rgb :) */
break;
}
make_rgb(rgb_buffer,
cur_name,
req_width,
req_height);
break;
default:
fprintf(stderr, "Not supported format requested!\n");
break;
}
queue_buffer(ready_buf);
}
}
return NULL;
}