wip video device
This commit is contained in:
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
AUTOMAKE_OPTIONS = foreign subdir-objects -Wall
|
AUTOMAKE_OPTIONS = foreign subdir-objects -Wall
|
||||||
bin_PROGRAMS = forge
|
bin_PROGRAMS = forge
|
||||||
forge_SOURCES = src/main.c src/args.c src/forge.c src/file.c src/window.c src/shaders.c src/timer.c src/string.c src/config_file.c src/rand.c $(top_srcdir)/include/glad/gl.h $(top_srcdir)/hashmap.c/hashmap.c
|
forge_SOURCES = src/main.c src/args.c src/forge.c src/file.c src/window.c src/shaders.c src/timer.c src/string.c src/config_file.c src/rand.c src/video.c $(top_srcdir)/include/glad/gl.h $(top_srcdir)/include/glad/egl.h $(top_srcdir)/hashmap.c/hashmap.c
|
||||||
forge_CFLAGS = -Ofast -march=native -flto -funroll-loops -fprefetch-loop-arrays -fno-exceptions -fopenmp -I$(top_srcdir)/include -DGLFW_INCLUDE_NONE
|
forge_CFLAGS = -Ofast -march=native -flto -funroll-loops -fprefetch-loop-arrays -fno-exceptions -fopenmp -I$(top_srcdir)/include -DGLFW_INCLUDE_NONE
|
||||||
forge_LDADD = -lm -lGL -lglfw
|
forge_LDADD = -lm -lGL -lglfw
|
||||||
include_HEADERS = src/main.h src/args.h src/config.h src/types.h src/forge.h src/file.h src/constants.h src/window.h src/shaders.h src/logs.h src/timer.h src/string.h src/config_file.h src/rand.h $(top_srcdir)/include/glad/gl.h $(top_srcdir)/include/linmath.h $(top_srcdir)/include/hashmap.h
|
include_HEADERS = src/main.h src/args.h src/config.h src/types.h src/forge.h src/file.h src/constants.h src/window.h src/shaders.h src/logs.h src/timer.h src/string.h src/config_file.h src/rand.h src/video.h $(top_srcdir)/include/glad/gl.h $(top_srcdir)/include/glad/egl.h $(top_srcdir)/include/linmath.h $(top_srcdir)/include/hashmap.h
|
||||||
+5
-4
@@ -13,10 +13,13 @@ build:
|
|||||||
src/*.h src/*.c \
|
src/*.h src/*.c \
|
||||||
-Iinclude \
|
-Iinclude \
|
||||||
hashmap.c/hashmap.c \
|
hashmap.c/hashmap.c \
|
||||||
-lm -lGL -lglfw \
|
-lm -lGL -lglfw -ldrm \
|
||||||
-Wall -Wextra \
|
-Wall -Wextra \
|
||||||
-DGLFW_INCLUDE_NONE \
|
-DGLFW_INCLUDE_NONE \
|
||||||
-o build/$(TARGET)
|
-DGLFW_EXPOSE_NATIVE_EGL \
|
||||||
|
-DGLFW_NATIVE_INCLUDE_NONE \
|
||||||
|
-o build/$(TARGET) \
|
||||||
|
-g
|
||||||
|
|
||||||
.PHONY: run
|
.PHONY: run
|
||||||
run: build
|
run: build
|
||||||
@@ -33,8 +36,6 @@ install: build
|
|||||||
.PHONY: valgrind
|
.PHONY: valgrind
|
||||||
valgrind: build
|
valgrind: build
|
||||||
valgrind \
|
valgrind \
|
||||||
--leak-check=full \
|
|
||||||
--track-fds=all \
|
|
||||||
--show-realloc-size-zero=no \
|
--show-realloc-size-zero=no \
|
||||||
--undef-value-errors=no \
|
--undef-value-errors=no \
|
||||||
./build/$(TARGET) $(TEST_ARGS)
|
./build/$(TARGET) $(TEST_ARGS)
|
||||||
|
|||||||
@@ -134,6 +134,8 @@ make -f Makefile.dev release-arch
|
|||||||
- [ ] Clean code and fix things
|
- [ ] Clean code and fix things
|
||||||
- [ ] Video input
|
- [ ] Video input
|
||||||
- [ ] Fixed camera video
|
- [ ] Fixed camera video
|
||||||
|
- [ ] Sub process video reading
|
||||||
|
- [ ] Shader based format mapping
|
||||||
- [ ] Video mapping config file
|
- [ ] Video mapping config file
|
||||||
- [ ] Clean code and fix things
|
- [ ] Clean code and fix things
|
||||||
- [x] Monitor screen
|
- [x] Monitor screen
|
||||||
@@ -141,5 +143,6 @@ make -f Makefile.dev release-arch
|
|||||||
- [x] Use buffers as panels (INA A FXA / DEBUG A+B FXA+B / INB B FXB)
|
- [x] Use buffers as panels (INA A FXA / DEBUG A+B FXA+B / INB B FXB)
|
||||||
- [x] Clean code and fix things
|
- [x] Clean code and fix things
|
||||||
- [x] Share openGL state between monitor and screen
|
- [x] Share openGL state between monitor and screen
|
||||||
- [ ] Packaging & install
|
- [ ] Other
|
||||||
- [ ] Clone "shaders" and config in system path at setup
|
- [ ] Clone "shaders" and config in system path at setup
|
||||||
|
- [ ] Find and fix opengl errors 0500 ?
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
-Iinclude
|
-Iinclude
|
||||||
-DGLFW_INCLUDE_NONE
|
-DGLFW_INCLUDE_NONE
|
||||||
|
-DGLFW_EXPOSE_NATIVE_EGL
|
||||||
|
-DGLFW_NATIVE_INCLUDE_NONE
|
||||||
+14
-8
@@ -1,18 +1,24 @@
|
|||||||
AC_INIT([forge], [0.0.0], [klemek.dev@proton.me])
|
AC_INIT([forge], [0.0.0], [klemek.dev@proton.me])
|
||||||
AM_INIT_AUTOMAKE
|
AM_INIT_AUTOMAKE
|
||||||
AC_PROG_CC
|
AC_PROG_CC
|
||||||
AC_CHECK_HEADERS([stdio.h])
|
AC_CHECK_HEADERS([fcntl.h])
|
||||||
AC_CHECK_HEADERS([stdlib.h])
|
AC_CHECK_HEADERS([GLFW/glfw3.h])
|
||||||
|
AC_CHECK_HEADERS([GLFW/glfw3native.h])
|
||||||
|
AC_CHECK_HEADERS([linux/videodev2.h])
|
||||||
|
AC_CHECK_HEADERS([math.h])
|
||||||
AC_CHECK_HEADERS([stdbool.h])
|
AC_CHECK_HEADERS([stdbool.h])
|
||||||
AC_CHECK_HEADERS([stddef.h])
|
AC_CHECK_HEADERS([stddef.h])
|
||||||
AC_CHECK_HEADERS([string.h])
|
|
||||||
AC_CHECK_HEADERS([stdint.h])
|
AC_CHECK_HEADERS([stdint.h])
|
||||||
AC_CHECK_HEADERS([time.h])
|
AC_CHECK_HEADERS([stdio.h])
|
||||||
AC_CHECK_HEADERS([math.h])
|
AC_CHECK_HEADERS([stdlib.h])
|
||||||
|
AC_CHECK_HEADERS([string.h])
|
||||||
|
AC_CHECK_HEADERS([errno.h])
|
||||||
|
AC_CHECK_HEADERS([sys/ioctl.h])
|
||||||
|
AC_CHECK_HEADERS([sys/mman.h])
|
||||||
AC_CHECK_HEADERS([sys/stat.h])
|
AC_CHECK_HEADERS([sys/stat.h])
|
||||||
AC_CHECK_HEADERS([sys/types.h])
|
|
||||||
AC_CHECK_HEADERS([sys/time.h])
|
AC_CHECK_HEADERS([sys/time.h])
|
||||||
AC_CHECK_HEADERS([linux/videodev2.h])
|
AC_CHECK_HEADERS([sys/types.h])
|
||||||
AC_CHECK_HEADERS([GLFW/glfw3.h])
|
AC_CHECK_HEADERS([time.h])
|
||||||
|
AC_CHECK_HEADERS([unistd.h])
|
||||||
AC_CONFIG_FILES([Makefile])
|
AC_CONFIG_FILES([Makefile])
|
||||||
AC_OUTPUT
|
AC_OUTPUT
|
||||||
+1575
File diff suppressed because it is too large
Load Diff
+1413
-5
File diff suppressed because it is too large
Load Diff
+63
-6
@@ -1,5 +1,3 @@
|
|||||||
#include <GLFW/glfw3.h>
|
|
||||||
#include <glad/gl.h>
|
|
||||||
#include <linmath.h>
|
#include <linmath.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -14,7 +12,24 @@
|
|||||||
#define GLAD_GL_IMPLEMENTATION
|
#define GLAD_GL_IMPLEMENTATION
|
||||||
#include <glad/gl.h>
|
#include <glad/gl.h>
|
||||||
|
|
||||||
static void init_gl() { gladLoadGL(glfwGetProcAddress); }
|
#define GLAD_EGL_IMPLEMENTATION
|
||||||
|
#include <glad/egl.h>
|
||||||
|
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <GLFW/glfw3native.h>
|
||||||
|
|
||||||
|
static void init_gl(ShaderProgram *program) {
|
||||||
|
gladLoadGL(glfwGetProcAddress);
|
||||||
|
|
||||||
|
program->egl_display = glfwGetEGLDisplay();
|
||||||
|
if (program->egl_display == EGL_NO_DISPLAY) {
|
||||||
|
log_error("error: glfwGetEGLDisplay no EGLDisplay returned");
|
||||||
|
program->error = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
gladLoadEGL(program->egl_display, glfwGetProcAddress);
|
||||||
|
}
|
||||||
|
|
||||||
static void init_textures(ShaderProgram *program, Context context) {
|
static void init_textures(ShaderProgram *program, Context context) {
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
@@ -56,6 +71,41 @@ static void rebind_textures(ShaderProgram *program) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void link_video_to_texture(ShaderProgram *program, VideoDevice *device,
|
||||||
|
unsigned int texture_index) {
|
||||||
|
device->dma_image = EGL_NO_IMAGE_KHR;
|
||||||
|
|
||||||
|
const EGLint attrib_list[] = {EGL_WIDTH,
|
||||||
|
device->width,
|
||||||
|
EGL_HEIGHT,
|
||||||
|
device->height,
|
||||||
|
EGL_LINUX_DRM_FOURCC_EXT,
|
||||||
|
device->pixelformat,
|
||||||
|
EGL_DMA_BUF_PLANE0_FD_EXT,
|
||||||
|
device->exp_fd,
|
||||||
|
EGL_DMA_BUF_PLANE0_OFFSET_EXT,
|
||||||
|
0,
|
||||||
|
EGL_DMA_BUF_PLANE0_PITCH_EXT,
|
||||||
|
device->bytesperline,
|
||||||
|
EGL_NONE};
|
||||||
|
|
||||||
|
device->dma_image = eglCreateImageKHR(program->egl_display, EGL_NO_CONTEXT,
|
||||||
|
EGL_LINUX_DMA_BUF_EXT,
|
||||||
|
(EGLClientBuffer)NULL, attrib_list);
|
||||||
|
|
||||||
|
if (device->dma_image == EGL_NO_IMAGE_KHR) {
|
||||||
|
log_error("(%s) eglCreateImageKHR failed", device->name);
|
||||||
|
program->error = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://registry.khronos.org/OpenGL/extensions/EXT/EXT_EGL_image_storage.txt
|
||||||
|
glEGLImageTargetTextureStorageEXT(program->textures[texture_index],
|
||||||
|
(GLeglImageOES)device->dma_image, NULL);
|
||||||
|
|
||||||
|
log_success("Texture %d linked to %s", texture_index, device->name);
|
||||||
|
}
|
||||||
|
|
||||||
static void init_framebuffers(ShaderProgram *program,
|
static void init_framebuffers(ShaderProgram *program,
|
||||||
ConfigFile shader_config) {
|
ConfigFile shader_config) {
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
@@ -95,8 +145,6 @@ static void init_framebuffers(ShaderProgram *program,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void init_vertices(ShaderProgram *program) {
|
static void init_vertices(ShaderProgram *program) {
|
||||||
unsigned int i;
|
|
||||||
|
|
||||||
glGenBuffers(1, &program->vertex_buffer);
|
glGenBuffers(1, &program->vertex_buffer);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, program->vertex_buffer);
|
glBindBuffer(GL_ARRAY_BUFFER, program->vertex_buffer);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
@@ -290,7 +338,7 @@ ShaderProgram shaders_init(File *fragment_shaders, ConfigFile shader_config,
|
|||||||
program.sub_variant_count =
|
program.sub_variant_count =
|
||||||
config_file_get_int(shader_config, "SUB_VARIANT_COUNT", 1);
|
config_file_get_int(shader_config, "SUB_VARIANT_COUNT", 1);
|
||||||
|
|
||||||
init_gl();
|
init_gl(&program);
|
||||||
|
|
||||||
init_shaders(&program, fragment_shaders);
|
init_shaders(&program, fragment_shaders);
|
||||||
|
|
||||||
@@ -305,6 +353,9 @@ ShaderProgram shaders_init(File *fragment_shaders, ConfigFile shader_config,
|
|||||||
init_programs(&program, shader_config);
|
init_programs(&program, shader_config);
|
||||||
|
|
||||||
init_vertices(&program);
|
init_vertices(&program);
|
||||||
|
|
||||||
|
log_debug("Error after init: %04x",
|
||||||
|
glGetError()); // TODO check error at each step
|
||||||
} else {
|
} else {
|
||||||
program = *previous;
|
program = *previous;
|
||||||
}
|
}
|
||||||
@@ -456,3 +507,9 @@ void shaders_free(ShaderProgram program) {
|
|||||||
void shaders_free_window(ShaderProgram program, bool secondary) {
|
void shaders_free_window(ShaderProgram program, bool secondary) {
|
||||||
glDeleteVertexArrays(1, &program.vertex_array[secondary ? 1 : 0]);
|
glDeleteVertexArrays(1, &program.vertex_array[secondary ? 1 : 0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void shaders_free_video(ShaderProgram program, VideoDevice device) {
|
||||||
|
if (!device.error && device.dma_image != EGL_NO_IMAGE_KHR) {
|
||||||
|
eglDestroyImageKHR(program.egl_display, device.dma_image);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,4 +16,6 @@ void shaders_free(ShaderProgram program);
|
|||||||
|
|
||||||
void shaders_free_window(ShaderProgram program, bool secondary);
|
void shaders_free_window(ShaderProgram program, bool secondary);
|
||||||
|
|
||||||
|
void shaders_free_video(ShaderProgram program, VideoDevice device);
|
||||||
|
|
||||||
#endif /* SHADERS_H */
|
#endif /* SHADERS_H */
|
||||||
+17
@@ -1,7 +1,9 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <glad/egl.h>
|
||||||
#include <glad/gl.h>
|
#include <glad/gl.h>
|
||||||
#include <hashmap.h>
|
#include <hashmap.h>
|
||||||
#include <linmath.h>
|
#include <linmath.h>
|
||||||
|
#include <linux/videodev2.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
@@ -72,8 +74,23 @@ typedef struct ShaderProgram {
|
|||||||
unsigned int sub_type_count;
|
unsigned int sub_type_count;
|
||||||
unsigned int sub_variant_count;
|
unsigned int sub_variant_count;
|
||||||
GLuint *sub_locations;
|
GLuint *sub_locations;
|
||||||
|
|
||||||
|
EGLDisplay egl_display;
|
||||||
} ShaderProgram;
|
} ShaderProgram;
|
||||||
|
|
||||||
|
typedef struct VideoDevice {
|
||||||
|
char *name;
|
||||||
|
bool error;
|
||||||
|
int fd;
|
||||||
|
int exp_fd;
|
||||||
|
unsigned int width;
|
||||||
|
unsigned int height;
|
||||||
|
unsigned int pixelformat;
|
||||||
|
unsigned int bytesperline;
|
||||||
|
struct v4l2_buffer buf;
|
||||||
|
EGLImageKHR dma_image;
|
||||||
|
} VideoDevice;
|
||||||
|
|
||||||
typedef GLFWwindow Window;
|
typedef GLFWwindow Window;
|
||||||
|
|
||||||
typedef struct Context {
|
typedef struct Context {
|
||||||
|
|||||||
+207
@@ -0,0 +1,207 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <linux/videodev2.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "logs.h"
|
||||||
|
#include "types.h"
|
||||||
|
#include "video.h"
|
||||||
|
|
||||||
|
static VideoDevice open_device(char *name) {
|
||||||
|
VideoDevice device;
|
||||||
|
|
||||||
|
device.name = name;
|
||||||
|
device.error = false;
|
||||||
|
device.fd = -1;
|
||||||
|
|
||||||
|
device.fd = open(name, O_RDWR);
|
||||||
|
if (device.fd == -1) {
|
||||||
|
log_warn("(%s) Cannot open device", name);
|
||||||
|
device.error = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool check_device_caps(VideoDevice *device) {
|
||||||
|
struct v4l2_capability cap;
|
||||||
|
|
||||||
|
memset(&cap, 0, sizeof(cap));
|
||||||
|
|
||||||
|
if (ioctl(device->fd, VIDIOC_QUERYCAP, &cap) == -1) {
|
||||||
|
if (EINVAL == errno) {
|
||||||
|
log_warn("(%s) Not a V4L2 device", device->name);
|
||||||
|
} else {
|
||||||
|
log_error("(%s) VIDIOC_QUERYCAP unkown error", device->name);
|
||||||
|
}
|
||||||
|
device->error = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
|
||||||
|
log_warn("(%s) Not a video capture device", device->name);
|
||||||
|
device->error = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
|
||||||
|
log_warn("(%s) No streaming i/o support", device->name);
|
||||||
|
device->error = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool set_device_format(VideoDevice *device, unsigned int preferred_width,
|
||||||
|
unsigned int preferred_height) {
|
||||||
|
struct v4l2_format fmt;
|
||||||
|
|
||||||
|
memset(&fmt, 0, sizeof(fmt));
|
||||||
|
|
||||||
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
|
fmt.fmt.pix.width = preferred_width;
|
||||||
|
fmt.fmt.pix.height = preferred_height;
|
||||||
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGRX32;
|
||||||
|
fmt.fmt.pix.field = V4L2_FIELD_NONE;
|
||||||
|
|
||||||
|
if (ioctl(device->fd, VIDIOC_S_FMT, &fmt) == -1) {
|
||||||
|
log_warn("(%s) Format set failed", device->name);
|
||||||
|
device->error = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
device->width = fmt.fmt.pix.width;
|
||||||
|
device->height = fmt.fmt.pix.height;
|
||||||
|
device->pixelformat = fmt.fmt.pix.pixelformat;
|
||||||
|
device->bytesperline = fmt.fmt.pix.bytesperline;
|
||||||
|
|
||||||
|
log_info("(%s) Format fourcc: %c%c%c%c", device->name,
|
||||||
|
fmt.fmt.pix.pixelformat, fmt.fmt.pix.pixelformat >> 8,
|
||||||
|
fmt.fmt.pix.pixelformat >> 16, fmt.fmt.pix.pixelformat >> 24);
|
||||||
|
log_info("(%s) Resolution: %dx%d", device->name, fmt.fmt.pix.width,
|
||||||
|
fmt.fmt.pix.height);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool request_buffers(VideoDevice *device) {
|
||||||
|
struct v4l2_requestbuffers reqbuf;
|
||||||
|
|
||||||
|
memset(&reqbuf, 0, sizeof(reqbuf));
|
||||||
|
|
||||||
|
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
|
reqbuf.memory = V4L2_MEMORY_MMAP;
|
||||||
|
reqbuf.count = 1;
|
||||||
|
|
||||||
|
if (ioctl(device->fd, VIDIOC_REQBUFS, &reqbuf) == -1) {
|
||||||
|
if (errno == EINVAL) {
|
||||||
|
log_info("(%s) Video capturing or DMABUF streaming is not supported",
|
||||||
|
device->name);
|
||||||
|
} else {
|
||||||
|
log_error("(%s) VIDIOC_REQBUFS unkown error", device->name);
|
||||||
|
}
|
||||||
|
device->error = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
log_info("(%s) V4L2 Buffer Count: %d", device->name, reqbuf.count);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool export_buffer(VideoDevice *device) {
|
||||||
|
struct v4l2_exportbuffer expbuf;
|
||||||
|
|
||||||
|
device->exp_fd = -1;
|
||||||
|
|
||||||
|
memset(&expbuf, 0, sizeof(expbuf));
|
||||||
|
|
||||||
|
expbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
|
expbuf.index = 0; /// '0' for one buffer
|
||||||
|
expbuf.flags = O_RDONLY;
|
||||||
|
|
||||||
|
if (ioctl(device->fd, VIDIOC_EXPBUF, &expbuf) == -1) {
|
||||||
|
log_error("(%s) VIDIOC_EXPBUF error", device->name);
|
||||||
|
device->error = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
device->exp_fd = expbuf.fd;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const enum v4l2_buf_type buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
|
|
||||||
|
static bool open_stream(VideoDevice *device) {
|
||||||
|
if (ioctl(device->fd, VIDIOC_STREAMON, &buf_type) == -1) {
|
||||||
|
log_error("(%s) VIDIOC_STREAMON error", device->name);
|
||||||
|
device->error = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void create_image_buffer(VideoDevice *device) {
|
||||||
|
memset(&device->buf, 0, sizeof(device->buf));
|
||||||
|
|
||||||
|
device->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
|
device->buf.memory = V4L2_MEMORY_MMAP;
|
||||||
|
device->buf.index = 0;
|
||||||
|
|
||||||
|
ioctl(device->fd, VIDIOC_QBUF, &device->buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void close_stream(VideoDevice device) {
|
||||||
|
ioctl(device.fd, VIDIOC_STREAMOFF, &buf_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
VideoDevice video_init(char *name, unsigned int preferred_width,
|
||||||
|
unsigned int preferred_height) {
|
||||||
|
VideoDevice device;
|
||||||
|
|
||||||
|
device = open_device(name);
|
||||||
|
|
||||||
|
if (device.error) {
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!check_device_caps(&device)) {
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!set_device_format(&device, preferred_width, preferred_height)) {
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!request_buffers(&device)) {
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!export_buffer(&device)) {
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!open_stream(&device)) {
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
create_image_buffer(&device);
|
||||||
|
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
void video_read(VideoDevice device) {
|
||||||
|
ioctl(device.fd, VIDIOC_DQBUF, &device.buf);
|
||||||
|
ioctl(device.fd, VIDIOC_QBUF, &device.buf);
|
||||||
|
// TODO check for error and close
|
||||||
|
}
|
||||||
|
|
||||||
|
void video_free(VideoDevice device) {
|
||||||
|
close_stream(device);
|
||||||
|
close(device.exp_fd);
|
||||||
|
close(device.fd);
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
#ifndef VIDEO_H
|
||||||
|
#define VIDEO_H
|
||||||
|
|
||||||
|
VideoDevice video_init(char *name, unsigned int preferred_width,
|
||||||
|
unsigned int preferred_height);
|
||||||
|
|
||||||
|
void video_read(VideoDevice device);
|
||||||
|
|
||||||
|
void video_free(VideoDevice device);
|
||||||
|
|
||||||
|
#endif /* VIDEO_H */
|
||||||
Reference in New Issue
Block a user