527 lines
17 KiB
C
527 lines
17 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|||
|
|
#include "gemui.h"
|
||
|
|
|
||
|
|
#include <dirent.h>
|
||
|
|
#include <errno.h>
|
||
|
|
#include <fcntl.h>
|
||
|
|
#include <linux/videodev2.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <jpeglib.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
#include <sys/ioctl.h>
|
||
|
|
#include <sys/mman.h>
|
||
|
|
#include <sys/stat.h>
|
||
|
|
#include <time.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
|
||
|
|
#define HIT_SHUTTER 1
|
||
|
|
#define HIT_RECORD 2
|
||
|
|
#define HIT_SWITCH 3
|
||
|
|
|
||
|
|
typedef struct {
|
||
|
|
void *start;
|
||
|
|
size_t length;
|
||
|
|
} v4l2_buf_t;
|
||
|
|
|
||
|
|
typedef struct {
|
||
|
|
uint32_t offset;
|
||
|
|
uint32_t size;
|
||
|
|
} avi_idx_t;
|
||
|
|
|
||
|
|
typedef struct {
|
||
|
|
gemui_app_t *app;
|
||
|
|
int vfd;
|
||
|
|
char dev_path[64];
|
||
|
|
char dev_list[8][64];
|
||
|
|
int num_devs;
|
||
|
|
int cur_dev;
|
||
|
|
uint32_t pixfmt;
|
||
|
|
int cam_w;
|
||
|
|
int cam_h;
|
||
|
|
v4l2_buf_t bufs[4];
|
||
|
|
int num_bufs;
|
||
|
|
uint8_t *rgb24;
|
||
|
|
uint32_t *argb32;
|
||
|
|
cairo_surface_t *frame_surf;
|
||
|
|
bool recording;
|
||
|
|
FILE *avi_fp;
|
||
|
|
uint32_t avi_frames;
|
||
|
|
long avi_movi_pos;
|
||
|
|
avi_idx_t *avi_idx;
|
||
|
|
int avi_idx_cap;
|
||
|
|
char last_file[128];
|
||
|
|
} cam_state_t;
|
||
|
|
|
||
|
|
static uint8_t clamp_u8(int v) {
|
||
|
|
return (uint8_t)(v < 0 ? 0 : (v > 255 ? 255 : v));
|
||
|
|
}
|
||
|
|
|
||
|
|
static void yuyv_to_buffers(const uint8_t *src, int w, int h, uint8_t *rgb, uint32_t *argb) {
|
||
|
|
int pairs = (w * h) / 2;
|
||
|
|
for (int i = 0; i < pairs; i++) {
|
||
|
|
int y0 = src[4 * i + 0] - 16;
|
||
|
|
int u = src[4 * i + 1] - 128;
|
||
|
|
int y1 = src[4 * i + 2] - 16;
|
||
|
|
int v = src[4 * i + 3] - 128;
|
||
|
|
|
||
|
|
int c0 = 298 * y0;
|
||
|
|
int c1 = 298 * y1;
|
||
|
|
int r0 = clamp_u8((c0 + 409 * v + 128) >> 8);
|
||
|
|
int g0 = clamp_u8((c0 - 100 * u - 208 * v + 128) >> 8);
|
||
|
|
int b0 = clamp_u8((c0 + 516 * u + 128) >> 8);
|
||
|
|
int r1 = clamp_u8((c1 + 409 * v + 128) >> 8);
|
||
|
|
int g1 = clamp_u8((c1 - 100 * u - 208 * v + 128) >> 8);
|
||
|
|
int b1 = clamp_u8((c1 + 516 * u + 128) >> 8);
|
||
|
|
|
||
|
|
rgb[6 * i + 0] = (uint8_t)r0;
|
||
|
|
rgb[6 * i + 1] = (uint8_t)g0;
|
||
|
|
rgb[6 * i + 2] = (uint8_t)b0;
|
||
|
|
rgb[6 * i + 3] = (uint8_t)r1;
|
||
|
|
rgb[6 * i + 4] = (uint8_t)g1;
|
||
|
|
rgb[6 * i + 5] = (uint8_t)b1;
|
||
|
|
|
||
|
|
argb[2 * i + 0] = 0xff000000U | ((uint32_t)r0 << 16) | ((uint32_t)g0 << 8) | (uint32_t)b0;
|
||
|
|
argb[2 * i + 1] = 0xff000000U | ((uint32_t)r1 << 16) | ((uint32_t)g1 << 8) | (uint32_t)b1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static bool encode_rgb24_jpeg_mem(const uint8_t *rgb, int w, int h, int quality,
|
||
|
|
unsigned char **out_buf, unsigned long *out_sz) {
|
||
|
|
struct jpeg_compress_struct cinfo;
|
||
|
|
struct jpeg_error_mgr jerr;
|
||
|
|
cinfo.err = jpeg_std_error(&jerr);
|
||
|
|
jpeg_create_compress(&cinfo);
|
||
|
|
*out_buf = NULL;
|
||
|
|
*out_sz = 0;
|
||
|
|
jpeg_mem_dest(&cinfo, out_buf, out_sz);
|
||
|
|
cinfo.image_width = (JDIMENSION)w;
|
||
|
|
cinfo.image_height = (JDIMENSION)h;
|
||
|
|
cinfo.input_components = 3;
|
||
|
|
cinfo.in_color_space = JCS_RGB;
|
||
|
|
jpeg_set_defaults(&cinfo);
|
||
|
|
jpeg_set_quality(&cinfo, quality, TRUE);
|
||
|
|
jpeg_start_compress(&cinfo, TRUE);
|
||
|
|
while (cinfo.next_scanline < cinfo.image_height) {
|
||
|
|
JSAMPROW row[1] = {(JSAMPROW)(rgb + (size_t)cinfo.next_scanline * (size_t)w * 3U)};
|
||
|
|
jpeg_write_scanlines(&cinfo, row, 1);
|
||
|
|
}
|
||
|
|
jpeg_finish_compress(&cinfo);
|
||
|
|
jpeg_destroy_compress(&cinfo);
|
||
|
|
return *out_buf != NULL && *out_sz > 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void put_le32(FILE *f, uint32_t v) {
|
||
|
|
uint8_t b[4] = {(uint8_t)v, (uint8_t)(v >> 8), (uint8_t)(v >> 16), (uint8_t)(v >> 24)};
|
||
|
|
fwrite(b, 1, 4, f);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void put_le16(FILE *f, uint16_t v) {
|
||
|
|
uint8_t b[2] = {(uint8_t)v, (uint8_t)(v >> 8)};
|
||
|
|
fwrite(b, 1, 2, f);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void avi_stop(cam_state_t *st) {
|
||
|
|
if (!st->recording || !st->avi_fp) return;
|
||
|
|
FILE *f = st->avi_fp;
|
||
|
|
long end_movi = ftell(f);
|
||
|
|
uint32_t movi_sz = (uint32_t)(end_movi - st->avi_movi_pos - 4);
|
||
|
|
|
||
|
|
fwrite("idx1", 1, 4, f);
|
||
|
|
put_le32(f, st->avi_frames * 16U);
|
||
|
|
for (uint32_t i = 0; i < st->avi_frames; i++) {
|
||
|
|
fwrite("00dc", 1, 4, f);
|
||
|
|
put_le32(f, 0x10U);
|
||
|
|
put_le32(f, st->avi_idx[i].offset);
|
||
|
|
put_le32(f, st->avi_idx[i].size);
|
||
|
|
}
|
||
|
|
long file_end = ftell(f);
|
||
|
|
|
||
|
|
fseek(f, 4, SEEK_SET);
|
||
|
|
put_le32(f, (uint32_t)(file_end - 8));
|
||
|
|
fseek(f, 48, SEEK_SET);
|
||
|
|
put_le32(f, st->avi_frames);
|
||
|
|
fseek(f, 140, SEEK_SET);
|
||
|
|
put_le32(f, st->avi_frames);
|
||
|
|
fseek(f, st->avi_movi_pos, SEEK_SET);
|
||
|
|
put_le32(f, movi_sz);
|
||
|
|
|
||
|
|
fclose(f);
|
||
|
|
st->avi_fp = NULL;
|
||
|
|
st->recording = false;
|
||
|
|
free(st->avi_idx);
|
||
|
|
st->avi_idx = NULL;
|
||
|
|
st->avi_idx_cap = 0;
|
||
|
|
gemui_show_toast(st->app, "Saved MJPEG AVI video", 2000);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void avi_start(cam_state_t *st) {
|
||
|
|
if (!st->rgb24 || st->cam_w <= 0 || st->cam_h <= 0) {
|
||
|
|
gemui_show_toast(st->app, "No active camera stream", 1800);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const char *home = getenv("HOME");
|
||
|
|
char dir[512], path[576], ts[32];
|
||
|
|
snprintf(dir, sizeof(dir), "%s/Videos", (home && home[0]) ? home : "/home/user");
|
||
|
|
const char *mk[] = {"mkdir", "-p", dir, NULL};
|
||
|
|
gemui_run_cmd_capture(mk, NULL, 0);
|
||
|
|
time_t now = time(NULL);
|
||
|
|
struct tm tm;
|
||
|
|
localtime_r(&now, &tm);
|
||
|
|
strftime(ts, sizeof(ts), "%Y%m%d_%H%M%S", &tm);
|
||
|
|
snprintf(path, sizeof(path), "%s/VID_%s.avi", dir, ts);
|
||
|
|
|
||
|
|
FILE *f = fopen(path, "wb+");
|
||
|
|
if (!f) return;
|
||
|
|
|
||
|
|
fwrite("RIFF", 1, 4, f); put_le32(f, 0); fwrite("AVI ", 1, 4, f);
|
||
|
|
fwrite("LIST", 1, 4, f); put_le32(f, 192); fwrite("hdrl", 1, 4, f);
|
||
|
|
fwrite("avih", 1, 4, f); put_le32(f, 56);
|
||
|
|
put_le32(f, 66667); put_le32(f, 0); put_le32(f, 0); put_le32(f, 0x10U);
|
||
|
|
put_le32(f, 0); put_le32(f, 0); put_le32(f, 1); put_le32(f, 0);
|
||
|
|
put_le32(f, (uint32_t)st->cam_w); put_le32(f, (uint32_t)st->cam_h);
|
||
|
|
for (int i = 0; i < 4; i++) put_le32(f, 0);
|
||
|
|
|
||
|
|
fwrite("LIST", 1, 4, f); put_le32(f, 116); fwrite("strl", 1, 4, f);
|
||
|
|
fwrite("strh", 1, 4, f); put_le32(f, 56);
|
||
|
|
fwrite("vids", 1, 4, f); fwrite("MJPG", 1, 4, f);
|
||
|
|
put_le32(f, 0); put_le32(f, 0); put_le32(f, 0);
|
||
|
|
put_le32(f, 1); put_le32(f, 15); put_le32(f, 0);
|
||
|
|
put_le32(f, 0); put_le32(f, 0); put_le32(f, 0);
|
||
|
|
put_le16(f, 0); put_le16(f, 0);
|
||
|
|
put_le16(f, (uint16_t)st->cam_w); put_le16(f, (uint16_t)st->cam_h);
|
||
|
|
|
||
|
|
fwrite("strf", 1, 4, f); put_le32(f, 40);
|
||
|
|
put_le32(f, 40); put_le32(f, (uint32_t)st->cam_w); put_le32(f, (uint32_t)st->cam_h);
|
||
|
|
put_le16(f, 1); put_le16(f, 24); fwrite("MJPG", 1, 4, f);
|
||
|
|
put_le32(f, (uint32_t)(st->cam_w * st->cam_h * 3));
|
||
|
|
for (int i = 0; i < 4; i++) put_le32(f, 0);
|
||
|
|
|
||
|
|
fwrite("LIST", 1, 4, f);
|
||
|
|
st->avi_movi_pos = ftell(f);
|
||
|
|
put_le32(f, 0);
|
||
|
|
fwrite("movi", 1, 4, f);
|
||
|
|
|
||
|
|
st->avi_fp = f;
|
||
|
|
st->avi_frames = 0;
|
||
|
|
st->recording = true;
|
||
|
|
snprintf(st->last_file, sizeof(st->last_file), "VID_%s.avi", ts);
|
||
|
|
gemui_show_toast(st->app, "Recording MJPEG AVI video...", 1500);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void avi_write_frame(cam_state_t *st, const uint8_t *jpg, size_t jpg_sz) {
|
||
|
|
if (!st->recording || !st->avi_fp || !jpg || !jpg_sz) return;
|
||
|
|
if ((int)st->avi_frames >= st->avi_idx_cap) {
|
||
|
|
int ncap = st->avi_idx_cap ? st->avi_idx_cap * 2 : 512;
|
||
|
|
avi_idx_t *nptr = realloc(st->avi_idx, (size_t)ncap * sizeof(avi_idx_t));
|
||
|
|
if (!nptr) return;
|
||
|
|
st->avi_idx = nptr;
|
||
|
|
st->avi_idx_cap = ncap;
|
||
|
|
}
|
||
|
|
long chunk_off = ftell(st->avi_fp) - (st->avi_movi_pos + 4);
|
||
|
|
fwrite("00dc", 1, 4, st->avi_fp);
|
||
|
|
put_le32(st->avi_fp, (uint32_t)jpg_sz);
|
||
|
|
fwrite(jpg, 1, jpg_sz, st->avi_fp);
|
||
|
|
if (jpg_sz & 1U) fputc(0, st->avi_fp);
|
||
|
|
|
||
|
|
st->avi_idx[st->avi_frames].offset = (uint32_t)chunk_off;
|
||
|
|
st->avi_idx[st->avi_frames].size = (uint32_t)jpg_sz;
|
||
|
|
st->avi_frames++;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void close_camera(cam_state_t *st) {
|
||
|
|
if (st->recording) avi_stop(st);
|
||
|
|
if (st->vfd >= 0) {
|
||
|
|
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||
|
|
ioctl(st->vfd, VIDIOC_STREAMOFF, &type);
|
||
|
|
gemui_del_fd(st->app, st->vfd);
|
||
|
|
for (int i = 0; i < st->num_bufs; i++) {
|
||
|
|
if (st->bufs[i].start && st->bufs[i].start != MAP_FAILED) {
|
||
|
|
munmap(st->bufs[i].start, st->bufs[i].length);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
close(st->vfd);
|
||
|
|
st->vfd = -1;
|
||
|
|
}
|
||
|
|
st->num_bufs = 0;
|
||
|
|
if (st->frame_surf) {
|
||
|
|
cairo_surface_destroy(st->frame_surf);
|
||
|
|
st->frame_surf = NULL;
|
||
|
|
}
|
||
|
|
free(st->rgb24); st->rgb24 = NULL;
|
||
|
|
free(st->argb32); st->argb32 = NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void on_v4l2_ready(gemui_app_t *app, int fd, void *ud) {
|
||
|
|
cam_state_t *st = ud;
|
||
|
|
struct v4l2_buffer buf;
|
||
|
|
memset(&buf, 0, sizeof(buf));
|
||
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
||
|
|
if (ioctl(fd, VIDIOC_DQBUF, &buf) < 0) return;
|
||
|
|
|
||
|
|
const uint8_t *raw = st->bufs[buf.index].start;
|
||
|
|
if (st->pixfmt == V4L2_PIX_FMT_YUYV && raw && buf.bytesused >= (uint32_t)(st->cam_w * st->cam_h * 2)) {
|
||
|
|
yuyv_to_buffers(raw, st->cam_w, st->cam_h, st->rgb24, st->argb32);
|
||
|
|
if (st->recording) {
|
||
|
|
unsigned char *jb = NULL;
|
||
|
|
unsigned long jsz = 0;
|
||
|
|
if (encode_rgb24_jpeg_mem(st->rgb24, st->cam_w, st->cam_h, 80, &jb, &jsz)) {
|
||
|
|
avi_write_frame(st, jb, jsz);
|
||
|
|
free(jb);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if (st->pixfmt == V4L2_PIX_FMT_MJPEG && raw && buf.bytesused > 4) {
|
||
|
|
if (st->recording) avi_write_frame(st, raw, buf.bytesused);
|
||
|
|
struct jpeg_decompress_struct cinfo;
|
||
|
|
struct jpeg_error_mgr jerr;
|
||
|
|
cinfo.err = jpeg_std_error(&jerr);
|
||
|
|
jpeg_create_decompress(&cinfo);
|
||
|
|
jpeg_mem_src(&cinfo, raw, buf.bytesused);
|
||
|
|
if (jpeg_read_header(&cinfo, TRUE) == JPEG_HEADER_OK) {
|
||
|
|
cinfo.out_color_space = JCS_RGB;
|
||
|
|
jpeg_start_decompress(&cinfo);
|
||
|
|
int row_stride = (int)cinfo.output_width * 3;
|
||
|
|
while ((int)cinfo.output_scanline < (int)cinfo.output_height &&
|
||
|
|
(int)cinfo.output_scanline < st->cam_h) {
|
||
|
|
int y = (int)cinfo.output_scanline;
|
||
|
|
uint8_t *dst = st->rgb24 + (size_t)y * (size_t)st->cam_w * 3U;
|
||
|
|
JSAMPROW row[1] = {dst};
|
||
|
|
jpeg_read_scanlines(&cinfo, row, 1);
|
||
|
|
int cw = (int)cinfo.output_width < st->cam_w ? (int)cinfo.output_width : st->cam_w;
|
||
|
|
for (int x = 0; x < cw; x++) {
|
||
|
|
uint8_t r = dst[3 * x], g = dst[3 * x + 1], b = dst[3 * x + 2];
|
||
|
|
st->argb32[y * st->cam_w + x] = 0xff000000U | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
|
||
|
|
}
|
||
|
|
(void)row_stride;
|
||
|
|
}
|
||
|
|
jpeg_finish_decompress(&cinfo);
|
||
|
|
}
|
||
|
|
jpeg_destroy_decompress(&cinfo);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (st->frame_surf) cairo_surface_mark_dirty(st->frame_surf);
|
||
|
|
ioctl(fd, VIDIOC_QBUF, &buf);
|
||
|
|
gemui_request_redraw(app);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void open_camera_idx(cam_state_t *st, int idx) {
|
||
|
|
close_camera(st);
|
||
|
|
if (idx < 0 || idx >= st->num_devs) return;
|
||
|
|
|
||
|
|
int fd = open(st->dev_list[idx], O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
||
|
|
if (fd < 0) return;
|
||
|
|
|
||
|
|
struct v4l2_format fmt;
|
||
|
|
memset(&fmt, 0, sizeof(fmt));
|
||
|
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||
|
|
fmt.fmt.pix.width = 1280;
|
||
|
|
fmt.fmt.pix.height = 720;
|
||
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
|
||
|
|
fmt.fmt.pix.field = V4L2_FIELD_NONE;
|
||
|
|
if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0) {
|
||
|
|
close(fd);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
st->cam_w = (int)fmt.fmt.pix.width;
|
||
|
|
st->cam_h = (int)fmt.fmt.pix.height;
|
||
|
|
st->pixfmt = fmt.fmt.pix.pixelformat;
|
||
|
|
st->rgb24 = calloc((size_t)st->cam_w * (size_t)st->cam_h, 3);
|
||
|
|
st->argb32 = calloc((size_t)st->cam_w * (size_t)st->cam_h, sizeof(uint32_t));
|
||
|
|
st->frame_surf = cairo_image_surface_create_for_data(
|
||
|
|
(unsigned char *)st->argb32, CAIRO_FORMAT_RGB24, st->cam_w, st->cam_h, st->cam_w * 4);
|
||
|
|
|
||
|
|
struct v4l2_requestbuffers req;
|
||
|
|
memset(&req, 0, sizeof(req));
|
||
|
|
req.count = 4;
|
||
|
|
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||
|
|
req.memory = V4L2_MEMORY_MMAP;
|
||
|
|
if (ioctl(fd, VIDIOC_REQBUFS, &req) < 0 || req.count == 0) {
|
||
|
|
close(fd);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
for (int i = 0; i < (int)req.count && i < 4; i++) {
|
||
|
|
struct v4l2_buffer b;
|
||
|
|
memset(&b, 0, sizeof(b));
|
||
|
|
b.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||
|
|
b.memory = V4L2_MEMORY_MMAP;
|
||
|
|
b.index = (uint32_t)i;
|
||
|
|
if (ioctl(fd, VIDIOC_QUERYBUF, &b) < 0) break;
|
||
|
|
st->bufs[i].length = b.length;
|
||
|
|
st->bufs[i].start = mmap(NULL, b.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, b.m.offset);
|
||
|
|
if (st->bufs[i].start == MAP_FAILED) break;
|
||
|
|
st->num_bufs = i + 1;
|
||
|
|
ioctl(fd, VIDIOC_QBUF, &b);
|
||
|
|
}
|
||
|
|
|
||
|
|
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||
|
|
if (ioctl(fd, VIDIOC_STREAMON, &type) < 0) {
|
||
|
|
st->vfd = fd;
|
||
|
|
close_camera(st);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
st->vfd = fd;
|
||
|
|
st->cur_dev = idx;
|
||
|
|
snprintf(st->dev_path, sizeof(st->dev_path), "%s", st->dev_list[idx]);
|
||
|
|
gemui_add_fd(st->app, fd, on_v4l2_ready, st);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void capture_still(cam_state_t *st) {
|
||
|
|
if (!st->rgb24 || st->cam_w <= 0 || st->cam_h <= 0) {
|
||
|
|
gemui_show_toast(st->app, "No active V4L2 frame to capture", 1800);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const char *home = getenv("HOME");
|
||
|
|
char dir[512], path[576], ts[32];
|
||
|
|
snprintf(dir, sizeof(dir), "%s/Pictures", (home && home[0]) ? home : "/home/user");
|
||
|
|
const char *mk[] = {"mkdir", "-p", dir, NULL};
|
||
|
|
gemui_run_cmd_capture(mk, NULL, 0);
|
||
|
|
|
||
|
|
time_t now = time(NULL);
|
||
|
|
struct tm tm;
|
||
|
|
localtime_r(&now, &tm);
|
||
|
|
strftime(ts, sizeof(ts), "%Y%m%d_%H%M%S", &tm);
|
||
|
|
snprintf(path, sizeof(path), "%s/IMG_%s.jpg", dir, ts);
|
||
|
|
|
||
|
|
unsigned char *jb = NULL;
|
||
|
|
unsigned long jsz = 0;
|
||
|
|
if (encode_rgb24_jpeg_mem(st->rgb24, st->cam_w, st->cam_h, 92, &jb, &jsz)) {
|
||
|
|
FILE *f = fopen(path, "wb");
|
||
|
|
if (f) {
|
||
|
|
fwrite(jb, 1, jsz, f);
|
||
|
|
fclose(f);
|
||
|
|
snprintf(st->last_file, sizeof(st->last_file), "IMG_%s.jpg", ts);
|
||
|
|
gemui_show_toast(st->app, "Captured still JPEG to ~/Pictures", 2000);
|
||
|
|
}
|
||
|
|
free(jb);
|
||
|
|
}
|
||
|
|
gemui_request_redraw(st->app);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void discover_cameras(cam_state_t *st) {
|
||
|
|
st->num_devs = 0;
|
||
|
|
for (int i = 0; i < 16 && st->num_devs < 8; i++) {
|
||
|
|
char p[64];
|
||
|
|
snprintf(p, sizeof(p), "/dev/video%d", i);
|
||
|
|
int fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
||
|
|
if (fd < 0) continue;
|
||
|
|
struct v4l2_capability cap;
|
||
|
|
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0 &&
|
||
|
|
((cap.device_caps & V4L2_CAP_VIDEO_CAPTURE) ||
|
||
|
|
(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))) {
|
||
|
|
snprintf(st->dev_list[st->num_devs++], 64, "%s", p);
|
||
|
|
}
|
||
|
|
close(fd);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
|
||
|
|
cam_state_t *st = ud;
|
||
|
|
const gemui_theme_t *t = gemui_theme(app);
|
||
|
|
|
||
|
|
char badge[96];
|
||
|
|
snprintf(badge, sizeof(badge), "%s%s",
|
||
|
|
st->vfd >= 0 ? st->dev_path : "No V4L2 Camera",
|
||
|
|
st->recording ? " [REC]" : "");
|
||
|
|
gemui_draw_header(app, cr, "\xef\x80\xb0", "Geminux Camera", badge);
|
||
|
|
|
||
|
|
int pad = 20;
|
||
|
|
gemui_rect_t vf = {pad, 80, w - 260 - pad * 3, h - 100};
|
||
|
|
gemui_fill_round(cr, vf, 14, t->bg_overlay);
|
||
|
|
gemui_stroke_round(cr, vf, 14, 2.0, st->recording ? t->danger : t->border);
|
||
|
|
|
||
|
|
if (st->vfd >= 0 && st->frame_surf && st->cam_w > 0 && st->cam_h > 0) {
|
||
|
|
double sx = (double)(vf.w - 8) / st->cam_w;
|
||
|
|
double sy = (double)(vf.h - 8) / st->cam_h;
|
||
|
|
double sc = sx < sy ? sx : sy;
|
||
|
|
int dw = (int)(st->cam_w * sc);
|
||
|
|
int dh = (int)(st->cam_h * sc);
|
||
|
|
int dx = vf.x + (vf.w - dw) / 2;
|
||
|
|
int dy = vf.y + (vf.h - dh) / 2;
|
||
|
|
cairo_save(cr);
|
||
|
|
cairo_translate(cr, dx, dy);
|
||
|
|
cairo_scale(cr, sc, sc);
|
||
|
|
cairo_set_source_surface(cr, st->frame_surf, 0, 0);
|
||
|
|
cairo_paint(cr);
|
||
|
|
cairo_restore(cr);
|
||
|
|
} else {
|
||
|
|
gemui_draw_text(cr, t, (gemui_rect_t){vf.x + 24, vf.y + vf.h / 2 - 40, vf.w - 48, 40},
|
||
|
|
"Camera Stream Unavailable", 4, true, GEMUI_ALIGN_CENTER, t->fg_bright);
|
||
|
|
gemui_draw_text(cr, t, (gemui_rect_t){vf.x + 24, vf.y + vf.h / 2 + 8, vf.w - 48, 36},
|
||
|
|
"Waiting for hi556 / mt6797-seninf V4L2 /dev/video* device",
|
||
|
|
-2, false, GEMUI_ALIGN_CENTER, t->fg_muted);
|
||
|
|
}
|
||
|
|
|
||
|
|
int rx = vf.x + vf.w + pad;
|
||
|
|
int rw = 260;
|
||
|
|
gemui_draw_button(app, cr, (gemui_rect_t){rx, 96, rw, 92},
|
||
|
|
"Photo (JPEG)", HIT_SHUTTER, 0, true, false, false);
|
||
|
|
gemui_draw_button(app, cr, (gemui_rect_t){rx, 208, rw, 92},
|
||
|
|
st->recording ? "Stop AVI" : "Record AVI", HIT_RECORD, 0, false, st->recording, false);
|
||
|
|
gemui_draw_button(app, cr, (gemui_rect_t){rx, 320, rw, 76},
|
||
|
|
"Switch Sensor", HIT_SWITCH, 0, false, false, false);
|
||
|
|
|
||
|
|
gemui_draw_text(cr, t, (gemui_rect_t){rx, 430, rw, 36},
|
||
|
|
st->last_file[0] ? "Last Saved:" : "", -3, true, GEMUI_ALIGN_LEFT, t->fg_muted);
|
||
|
|
gemui_draw_text(cr, t, (gemui_rect_t){rx, 466, rw, 36},
|
||
|
|
st->last_file[0] ? st->last_file : "Space: Shutter",
|
||
|
|
-3, false, GEMUI_ALIGN_LEFT, t->accent);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *utf8, void *ud) {
|
||
|
|
(void)state;
|
||
|
|
(void)utf8;
|
||
|
|
cam_state_t *st = ud;
|
||
|
|
if (ks == XK_Escape) {
|
||
|
|
gemui_quit(app, 0);
|
||
|
|
} else if (ks == XK_space || ks == XK_Return) {
|
||
|
|
capture_still(st);
|
||
|
|
} else if (ks == XK_r) {
|
||
|
|
if (st->recording) avi_stop(st); else avi_start(st);
|
||
|
|
gemui_request_redraw(app);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, void *ud) {
|
||
|
|
(void)btn;
|
||
|
|
cam_state_t *st = ud;
|
||
|
|
if (type != GEMUI_PTR_DOWN) return;
|
||
|
|
const gemui_hit_t *hit = gemui_hit_test(app, x, y);
|
||
|
|
if (!hit) return;
|
||
|
|
|
||
|
|
if (hit->id == HIT_SHUTTER) {
|
||
|
|
capture_still(st);
|
||
|
|
} else if (hit->id == HIT_RECORD) {
|
||
|
|
if (st->recording) avi_stop(st); else avi_start(st);
|
||
|
|
gemui_request_redraw(app);
|
||
|
|
} else if (hit->id == HIT_SWITCH) {
|
||
|
|
discover_cameras(st);
|
||
|
|
if (st->num_devs > 0) {
|
||
|
|
open_camera_idx(st, (st->cur_dev + 1) % st->num_devs);
|
||
|
|
} else {
|
||
|
|
gemui_show_toast(app, "No /dev/video* sensor found", 1800);
|
||
|
|
}
|
||
|
|
gemui_request_redraw(app);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(void) {
|
||
|
|
cam_state_t st;
|
||
|
|
memset(&st, 0, sizeof(st));
|
||
|
|
st.vfd = -1;
|
||
|
|
discover_cameras(&st);
|
||
|
|
|
||
|
|
st.app = gemui_create("geminux-camera", "Geminux Camera", 1640, 900, false);
|
||
|
|
if (!st.app) return 1;
|
||
|
|
if (st.num_devs > 0) open_camera_idx(&st, 0);
|
||
|
|
|
||
|
|
gemui_handlers_t h = { .on_draw = on_draw, .on_key = on_key, .on_ptr = on_ptr };
|
||
|
|
gemui_set_handlers(st.app, &h, &st);
|
||
|
|
int rc = gemui_run(st.app);
|
||
|
|
close_camera(&st);
|
||
|
|
gemui_destroy(st.app);
|
||
|
|
return rc;
|
||
|
|
}
|