summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlastair Poole <netstar@gmail.com>2020-07-17 17:19:57 +0100
committerAlastair Poole <netstar@gmail.com>2020-07-17 17:19:57 +0100
commitdcc0ec5aa7df89d2667cca3bedb2c94b30cbd9ee (patch)
tree9481204ef8003ab83b67ad4764062f8eacc2b1a3
parentb6dae6649a14b2ec690e02fdffb9ccbd01cb1157 (diff)
code: tidy
-rw-r--r--cam.c31
-rw-r--r--vid.c45
2 files changed, 39 insertions, 37 deletions
diff --git a/cam.c b/cam.c
index ce0eab1..340a6a0 100644
--- a/cam.c
+++ b/cam.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
+#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
@@ -10,12 +11,12 @@
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
+#include <termios.h>
#if defined(__linux__)
# include <linux/videodev2.h>
#else
# include <sys/videoio.h>
#endif
-#include <termios.h>
#include <Eina.h>
#include <Ecore.h>
@@ -90,7 +91,7 @@ YUV422toBGRA(unsigned char *buf, char *rgb, unsigned int w, unsigned int h)
}
}
-static void
+static bool
save_photo(const char *data, unsigned int w, unsigned int h)
{
Ecore_Evas *ee;
@@ -109,8 +110,10 @@ save_photo(const char *data, unsigned int w, unsigned int h)
snprintf(filename, sizeof(filename), "images/%s:%ld.jpg", buf, ts.tv_nsec);
ee = ecore_evas_new(NULL, 0, 0, 1, 1, NULL);
+ if (!ee) return false;
evas = ecore_evas_get(ee);
o = evas_object_image_filled_add(evas);
+ if (!o) return false;
evas_object_image_size_set(o, w, h);
evas_object_image_colorspace_set(o, EVAS_COLORSPACE_ARGB8888);
evas_object_image_data_set(o, (void *)data);
@@ -119,6 +122,8 @@ save_photo(const char *data, unsigned int w, unsigned int h)
evas_object_image_save(o, filename, NULL, NULL);
evas_object_del(o);
ecore_evas_free(ee);
+
+ return true;
}
typedef struct {
@@ -143,7 +148,7 @@ mmap_camera(int fd, int w, int h)
req.memory = V4L2_MEMORY_MMAP;
if (ioctl(fd, VIDIOC_REQBUFS, &req) == -1) {
- borked("VIDIO_REQBUFS");
+ borked("VIDIOC_REQBUFS");
}
buffer_t *buffers = calloc(req.count, sizeof(buffer_t));
@@ -205,25 +210,20 @@ mmap_camera(int fd, int w, int h)
else if (status == 0)
borked("timeout");
- if (FD_ISSET(STDIN_FILENO, &fds)) {
+ if (FD_ISSET(STDIN_FILENO, &fds))
break;
- }
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (ioctl(fd, VIDIOC_DQBUF, &buf) == -1) {
- switch (errno) {
- case EAGAIN:
- continue;
- default:
- borked("VIDIO_DQBUF");
- }
+ if (errno == EAGAIN) continue;
+ borked("VIDIOC_DQBUF");
}
YUV422toBGRA(buffers[buf.index].start, out, w, h);
- save_photo(out, w, h);
+ if (!save_photo(out, w, h)) break;
count++;
if (ioctl(fd, VIDIOC_QBUF, &buf) == -1) {
@@ -289,15 +289,15 @@ read_camera(int fd, int w, int h)
else if (status == 0)
borked("timeout");
- if (FD_ISSET(STDIN_FILENO, &fds)) {
+ if (FD_ISSET(STDIN_FILENO, &fds))
break;
- }
+
if ((read(fd, data, (w * h) * 2)) == -1) {
fprintf(stderr, "read: %s\n", strerror(errno));
break;
}
YUV422toBGRA(data, out, w, h);
- save_photo(out, w, h);
+ if (!save_photo(out, w, h)) break;
count++;
}
@@ -368,7 +368,6 @@ int main(int argc, char **argv)
switch (format.fmt.pix.pixelformat) {
case V4L2_PIX_FMT_YUYV:
- //read_camera(fd, w, h);
mmap_camera(fd, w, h);
break;
default:
diff --git a/vid.c b/vid.c
index e645764..ebedea5 100644
--- a/vid.c
+++ b/vid.c
@@ -106,7 +106,7 @@ _thread_feedback_cb(void *data, Ecore_Thread *thread, void *msg)
static void
_thread_end_cb(void *data, Ecore_Thread *thread)
{
- (void)data; (void)thread;
+ (void) data; (void) thread;
}
#define RGB_VALID(x) ((x) < 0) ? 0 :(((x) > 255) ? 255: (x))
@@ -143,7 +143,7 @@ YUV422toBGRA(unsigned char *buf, char *rgb, unsigned int w, unsigned int h)
}
typedef struct {
- char *start;
+ char *start;
uint32_t length;
} buffer_t;
@@ -155,8 +155,7 @@ mmap_camera(Cam *cam)
struct v4l2_buffer buf;
struct v4l2_requestbuffers req;
time_t ts, te;
- int i, type = V4L2_BUF_TYPE_VIDEO_CAPTURE, count = 0;
- int fd, w, h;
+ int i, count, fd, w, h, type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fd = cam->fd; w = cam->w; h = cam->h;
@@ -166,14 +165,14 @@ mmap_camera(Cam *cam)
req.memory = V4L2_MEMORY_MMAP;
if (ioctl(fd, VIDIOC_REQBUFS, &req) == -1) {
- fprintf(stderr, "VIDIO_REQBUFS\n");
- exit(1);
+ fprintf(stderr, "VIDIOC_REQBUFS\n");
+ exit(EXIT_FAILURE);
}
buffer_t *buffers = calloc(req.count, sizeof(buffer_t));
if (!buffers) {
fprintf(stderr, "calloc()\n");
- exit(1);
+ exit(EXIT_FAILURE);
}
for (i = 0; i < req.count; i++) {
@@ -184,14 +183,14 @@ mmap_camera(Cam *cam)
if (ioctl(fd, VIDIOC_QUERYBUF, &buf) == -1) {
fprintf(stderr, "VIDIOC_QUERYBUF\n");
- exit(1);
+ exit(EXIT_FAILURE);
}
buffers[i].length = buf.length;
buffers[i].start = mmap(NULL, buf.length, PROT_READ, MAP_SHARED, fd, buf.m.offset);
if (buffers[i].start == MAP_FAILED) {
fprintf(stderr, "mmap\n");
- exit(1);
+ exit(EXIT_FAILURE);
}
}
@@ -203,21 +202,22 @@ mmap_camera(Cam *cam)
if (ioctl(fd, VIDIOC_QBUF, &buf) == -1) {
fprintf(stderr, "VIDIOC_QBUF\n");
- exit(1);
+ exit(EXIT_FAILURE);
}
}
if (ioctl(fd, VIDIOC_STREAMON, &type) == -1) {
fprintf(stderr, "VIDIOC_STREAMON\n");
- exit(1);
+ exit(EXIT_FAILURE);
}
char *out = malloc((w * h) * sizeof(uint32_t));
if (!out) {
fprintf(stderr, "malloc\n");
- exit(1);
+ exit(EXIT_FAILURE);
}
+ count = 0;
ts = time(NULL);
while (1) {
@@ -228,7 +228,7 @@ mmap_camera(Cam *cam)
if (status == -1 && errno == EINTR) continue;
else if (status == 0) {
fprintf(stderr, "select timeout\n");
- exit(1);
+ exit(EXIT_FAILURE);
}
memset(&buf, 0, sizeof(buf));
@@ -239,8 +239,8 @@ mmap_camera(Cam *cam)
if (errno == EAGAIN)
continue;
else {
- fprintf(stderr, "VIDIO_DQBUF\n");
- exit(1);
+ fprintf(stderr, "VIDIOC_DQBUF\n");
+ exit(EXIT_FAILURE);
}
}
@@ -251,7 +251,7 @@ mmap_camera(Cam *cam)
if (ioctl(fd, VIDIOC_QBUF, &buf)) {
fprintf(stderr, "VIDIOC_QBUF\n");
- exit(1);
+ exit(EXIT_FAILURE);
}
if (ecore_thread_check(cam->thread)) break;
@@ -266,7 +266,7 @@ mmap_camera(Cam *cam)
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl(fd, VIDIOC_STREAMOFF, &type) == -1) {
fprintf(stderr, "VIDIOC_STREAMOFF\n");
- exit(1);
+ exit(EXIT_FAILURE);
}
for (i = 0; i < req.count; i++) {
@@ -288,8 +288,8 @@ int main(int argc, char **argv)
Cam *cam = NULL;
struct v4l2_capability cap;
struct v4l2_format format;
- int fd, ret = 1;
uint32_t w, h;
+ int fd, ret = 1;
eina_init();
ecore_init();
@@ -302,7 +302,7 @@ int main(int argc, char **argv)
fd = open(argv[1], O_RDWR | O_NONBLOCK);
if (fd == -1) {
fprintf(stderr, "Unable to open %s - %s\n", argv[1], strerror(errno));
- exit(1);
+ goto cleanup;
}
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
@@ -344,9 +344,12 @@ int main(int argc, char **argv)
cam->thread = ecore_thread_feedback_run(_thread_run_cb, _thread_feedback_cb, _thread_end_cb, _thread_end_cb, cam, EINA_FALSE);
ecore_main_loop_begin();
cleanup:
+
if (cam)
- free(cam);
- close(fd);
+ free(cam);
+
+ if (fd != -1)
+ close(fd);
elm_shutdown();
ecore_shutdown();