diff --git a/cmd/camera.c b/cmd/camera.c new file mode 100644 index 0000000..e22c705 --- /dev/null +++ b/cmd/camera.c @@ -0,0 +1,58 @@ +#include "camera.h" + +#include "hw/device.h" +#include "hw/links.h" +#include "hw/v4l2.h" + +void camera_init(camera_t *camera) +{ + memset(camera, 0, sizeof(*camera)); + camera->width = 2328; + camera->height = 1748; + camera->nbufs = 4; +} + +void camera_close(camera_t *camera) +{ + for (int i = MAX_DEVICES; i-- > 0; ) { + if (camera->devices[i]) { + device_close(camera->devices[i]); + camera->devices[i] = NULL; + } + } + + memset(camera->links, 0, sizeof(camera->links)); +} + +int camera_open(camera_t *camera, const char *path) +{ + camera->camera = device_open("CAMERA", path); + if (!camera->camera) { + return -1; + } + + return 0;; +} + +int camera_set_params(camera_t *camera) +{ + DEVICE_SET_OPTION(camera->camera, EXPOSURE, 1148); + DEVICE_SET_OPTION(camera->camera, ANALOGUE_GAIN, 938); + DEVICE_SET_OPTION(camera->camera, DIGITAL_GAIN, 256); + + DEVICE_SET_OPTION2(camera->codec_jpeg, JPEG, COMPRESSION_QUALITY, 80); + + DEVICE_SET_OPTION2(camera->codec_h264, MPEG_VIDEO, BITRATE, 5000 * 1000); + DEVICE_SET_OPTION2(camera->codec_h264, MPEG_VIDEO, H264_I_PERIOD, 30); + DEVICE_SET_OPTION2(camera->codec_h264, MPEG_VIDEO, H264_LEVEL, V4L2_MPEG_VIDEO_H264_LEVEL_4_0); + DEVICE_SET_OPTION2(camera->codec_h264, MPEG_VIDEO, REPEAT_SEQ_HEADER, 1); + DEVICE_SET_OPTION2(camera->codec_h264, MPEG_VIDEO, H264_MIN_QP, 16); + DEVICE_SET_OPTION2(camera->codec_h264, MPEG_VIDEO, H264_MIN_QP, 32); + return 0; +} + +int camera_run(camera_t *camera) +{ + bool running = false; + return links_loop(camera->links, &running); +} \ No newline at end of file diff --git a/cmd/camera.h b/cmd/camera.h new file mode 100644 index 0000000..418960f --- /dev/null +++ b/cmd/camera.h @@ -0,0 +1,49 @@ +#pragma once + +#include "hw/links.h" +#include "hw/device.h" + +#define MAX_DEVICES 20 +#define MAX_HTTP_METHODS 20 + +#define CAMERA_DEVICE_CAMERA 0 + +typedef struct camera_s { + union { + device_t *devices[MAX_DEVICES]; + struct { + device_t *camera; + + struct { + device_t *codec_jpeg; + device_t *codec_h264; + }; + + union { + struct { + device_t *isp_srgb; + device_t *isp_yuuv; + device_t *isp_yuuv_low; + } isp; + + struct { + device_t *isp; + } legacy_isp; + }; + }; + }; + link_t links[MAX_DEVICES]; + + unsigned width, height; + unsigned nbufs; +} camera_t; + +#define CAMERA(DEVICE) camera->devices[DEVICE] + +void camera_init(camera_t *camera); +void camera_close(camera_t *camera); +int camera_open(camera_t *camera, const char *path); +int camera_configure_srgb_isp(camera_t *camera, bool use_half); +int camera_configure_srgb_legacy_isp(camera_t *camera); +int camera_set_params(camera_t *camera); +int camera_run(camera_t *camera); diff --git a/cmd/camera_srgb_isp.c b/cmd/camera_srgb_isp.c new file mode 100644 index 0000000..c6dc58d --- /dev/null +++ b/cmd/camera_srgb_isp.c @@ -0,0 +1,71 @@ +#include "camera.h" + +#include "hw/buffer.h" +#include "hw/buffer_list.h" +#include "hw/device.h" +#include "hw/links.h" +#include "hw/v4l2.h" +#include "hw/buffer_list.h" +#include "http/http.h" + +extern bool check_streaming(); + +int camera_configure_srgb_isp(camera_t *camera, bool use_half) +{ + if (device_open_buffer_list(camera->camera, true, camera->width, camera->height, V4L2_PIX_FMT_SRGGB10P, camera->nbufs) < 0) { + return -1; + } + + buffer_list_t *src = camera->camera->capture_list; + + camera->isp.isp_srgb = device_open("ISP", "/dev/video13"); + camera->isp.isp_yuuv = device_open("ISP-YUUV", "/dev/video14"); + camera->codec_jpeg = device_open("JPEG", "/dev/video31"); + camera->codec_h264 = device_open("H264", "/dev/video11"); + + if (device_open_buffer_list(camera->isp.isp_srgb, false, src->fmt_width, src->fmt_height, src->fmt_format, camera->nbufs) < 0 || + device_open_buffer_list(camera->isp.isp_yuuv, true, src->fmt_width, src->fmt_height, V4L2_PIX_FMT_YUYV, camera->nbufs) < 0) { + return -1; + } + + if (use_half) { + camera->isp.isp_yuuv_low = device_open("ISP-YUUV-LOW", "/dev/video15"); + + if (device_open_buffer_list(camera->isp.isp_yuuv_low, true, src->fmt_width / 2, src->fmt_height / 2, V4L2_PIX_FMT_YUYV, camera->nbufs) < 0) { + return -1; + } + + src = camera->isp.isp_yuuv_low->capture_list; + } else { + src = camera->isp.isp_yuuv->capture_list; + } + + if (device_open_buffer_list(camera->codec_jpeg, false, src->fmt_width, src->fmt_height, src->fmt_format, camera->nbufs) < 0 || + device_open_buffer_list(camera->codec_jpeg, true, src->fmt_width, src->fmt_height, V4L2_PIX_FMT_JPEG, camera->nbufs) < 0) { + return -1; + } + + if (device_open_buffer_list(camera->codec_h264, false, src->fmt_width, src->fmt_height, src->fmt_format, camera->nbufs) < 0 || + device_open_buffer_list(camera->codec_h264, true, src->fmt_width, src->fmt_height, V4L2_PIX_FMT_H264, camera->nbufs) < 0) { + return -1; + } + + DEVICE_SET_OPTION(camera->isp.isp_srgb, RED_BALANCE, 2120); + DEVICE_SET_OPTION(camera->isp.isp_srgb, BLUE_BALANCE, 1472); + DEVICE_SET_OPTION(camera->isp.isp_srgb, DIGITAL_GAIN, 1007); + + link_t *links = camera->links; + + *links++ = (link_t){ camera->camera, { camera->isp.isp_srgb }, { NULL, check_streaming } }; + + if (use_half) { + *links++ = (link_t){ camera->isp.isp_yuuv, { } }; + *links++ = (link_t){ camera->isp.isp_yuuv_low, { camera->codec_jpeg, camera->codec_h264 } }; + } else { + *links++ = (link_t){ camera->isp.isp_yuuv, { camera->codec_jpeg, camera->codec_h264 } }; + } + + *links++ = (link_t){ camera->codec_jpeg, { }, { http_jpeg_capture, http_jpeg_needs_buffer } }; + *links++ = (link_t){ camera->codec_h264, { }, { http_h264_capture, http_h264_needs_buffer } }; + return 0; +} diff --git a/cmd/camera_srgb_legacy_isp.c b/cmd/camera_srgb_legacy_isp.c new file mode 100644 index 0000000..05b3b3f --- /dev/null +++ b/cmd/camera_srgb_legacy_isp.c @@ -0,0 +1,48 @@ +#include "camera.h" + +#include "hw/buffer.h" +#include "hw/buffer_list.h" +#include "hw/device.h" +#include "hw/links.h" +#include "hw/v4l2.h" +#include "hw/buffer_list.h" +#include "http/http.h" + +extern bool check_streaming(); + +int camera_configure_srgb_legacy_isp(camera_t *camera) +{ + if (device_open_buffer_list(camera->camera, true, camera->width, camera->height, V4L2_PIX_FMT_SRGGB10P, camera->nbufs) < 0) { + return -1; + } + + buffer_list_t *src = camera->camera->capture_list; + + camera->legacy_isp.isp = device_open("ISP", "/dev/video12"); + camera->codec_jpeg = device_open("JPEG", "/dev/video31"); + camera->codec_h264 = device_open("H264", "/dev/video11"); + + if (device_open_buffer_list(camera->legacy_isp.isp, false, src->fmt_width, src->fmt_height, src->fmt_format, camera->nbufs) < 0) { + return -1; + } + + src = camera->legacy_isp.isp->capture_list; + + if (device_open_buffer_list(camera->codec_jpeg, false, src->fmt_width, src->fmt_height, src->fmt_format, camera->nbufs) < 0 || + device_open_buffer_list(camera->codec_jpeg, true, src->fmt_width, src->fmt_height, V4L2_PIX_FMT_JPEG, camera->nbufs) < 0) { + return -1; + } + + if (device_open_buffer_list(camera->codec_h264, false, src->fmt_width, src->fmt_height, src->fmt_format, camera->nbufs) < 0 || + device_open_buffer_list(camera->codec_h264, true, src->fmt_width, src->fmt_height, V4L2_PIX_FMT_H264, camera->nbufs) < 0) { + return -1; + } + + link_t *links = camera->links; + + *links++ = (link_t){ camera->camera, { camera->legacy_isp.isp }, { NULL, check_streaming } }; + *links++ = (link_t){ camera->legacy_isp.isp, { camera->codec_jpeg, camera->codec_h264 } }; + *links++ = (link_t){ camera->codec_jpeg, { }, { http_jpeg_capture, http_jpeg_needs_buffer } }; + *links++ = (link_t){ camera->codec_h264, { }, { http_h264_capture, http_h264_needs_buffer } }; + return 0; +} diff --git a/cmd/main.c b/cmd/main.c new file mode 100644 index 0000000..8815863 --- /dev/null +++ b/cmd/main.c @@ -0,0 +1,59 @@ +#include "hw/buffer.h" +#include "hw/buffer_list.h" +#include "hw/device.h" +#include "hw/links.h" +#include "hw/v4l2.h" +#include "http/http.h" +#include "camera.h" + +#include + +http_method_t http_methods[] = { + { "GET / ", http_index }, + { "GET /snapshot ", http_snapshot }, + { "GET /stream ", http_stream }, + { "GET /?action=snapshot ", http_snapshot }, + { "GET /?action=stream ", http_stream }, + { "GET /video ", http_video_html }, + { "GET /video.h264 ", http_video }, + { "GET /jmuxer.min.js ", http_jmuxer_js }, + { NULL, NULL } +}; + +bool check_streaming() +{ + return http_jpeg_needs_buffer() || http_h264_needs_buffer(); +} + +int main(int argc, char *argv[]) +{ + camera_t camera; + int http_fd = -1; + int ret = -1; + + camera_init(&camera); + + if (camera_open(&camera, "/dev/video0") < 0) { + goto error; + } + + if (camera_configure_srgb_isp(&camera, true) < 0) { + goto error; + } + + if (camera_set_params(&camera) < 0) { + goto error; + } + + http_fd = http_server(9092, 5, http_methods); + if (http_fd < 0) { + goto error; + } + + ret = camera_run(&camera); + +error: + close(http_fd); + camera_close(&camera); + return ret; +} diff --git a/cmd/main_isp13.c b/cmd/main_isp13.c deleted file mode 100644 index 41b9764..0000000 --- a/cmd/main_isp13.c +++ /dev/null @@ -1,174 +0,0 @@ -#include "hw/buffer.h" -#include "hw/buffer_list.h" -#include "hw/device.h" -#include "hw/links.h" -#include "hw/v4l2.h" -#include "http/http.h" - -#include - -int camera_width = 2328; -int camera_height = 1748; -int camera_format = V4L2_PIX_FMT_SRGGB10P; -int camera_nbufs = 1; -bool camera_use_low = true; - -device_t *camera = NULL; -device_t *isp_srgb = NULL; -device_t *isp_yuuv = NULL; -device_t *isp_yuuv_low = NULL; -device_t *codec_jpeg = NULL; -device_t *codec_h264 = NULL; - -int open_isp(buffer_list_t *src, const char *srgb_path, const char *yuuv_path, const char *yuuv_low_path) -{ - DEVICE_SET_OPTION(isp_srgb, RED_BALANCE, 2120); - DEVICE_SET_OPTION(isp_srgb, BLUE_BALANCE, 1472); - DEVICE_SET_OPTION(isp_srgb, DIGITAL_GAIN, 1007); - - if (device_open_buffer_list(isp_srgb, false, src->fmt_width, src->fmt_height, src->fmt_format, camera_nbufs) < 0 || - device_open_buffer_list(isp_yuuv, true, src->fmt_width, src->fmt_height, V4L2_PIX_FMT_YUYV, camera_nbufs) < 0 || - device_open_buffer_list(isp_yuuv_low, true, src->fmt_width / 2, src->fmt_height / 2, V4L2_PIX_FMT_YUYV, camera_nbufs) < 0) { - return -1; - } - - return 0; -} - -int open_jpeg(buffer_list_t *src, const char *tmp) -{ - DEVICE_SET_OPTION2(codec_jpeg, JPEG, COMPRESSION_QUALITY, 80); - - if (device_open_buffer_list(codec_jpeg, false, src->fmt_width, src->fmt_height, src->fmt_format, camera_nbufs) < 0 || - device_open_buffer_list(codec_jpeg, true, codec_jpeg->output_list->fmt_width, codec_jpeg->output_list->fmt_height, V4L2_PIX_FMT_JPEG, camera_nbufs) < 0) { - return -1; - } - return 0; -} - -int open_h264(buffer_list_t *src, const char *tmp) -{ - DEVICE_SET_OPTION2(codec_h264, MPEG_VIDEO, BITRATE, 5000 * 1000); - DEVICE_SET_OPTION2(codec_h264, MPEG_VIDEO, H264_I_PERIOD, 30); - DEVICE_SET_OPTION2(codec_h264, MPEG_VIDEO, H264_LEVEL, V4L2_MPEG_VIDEO_H264_LEVEL_4_0); - DEVICE_SET_OPTION2(codec_h264, MPEG_VIDEO, REPEAT_SEQ_HEADER, 1); - DEVICE_SET_OPTION2(codec_h264, MPEG_VIDEO, H264_MIN_QP, 16); - DEVICE_SET_OPTION2(codec_h264, MPEG_VIDEO, H264_MIN_QP, 32); - - if (device_open_buffer_list(codec_h264, false, src->fmt_width, src->fmt_height, src->fmt_format, camera_nbufs) < 0 || - device_open_buffer_list(codec_h264, true, src->fmt_width, src->fmt_height, V4L2_PIX_FMT_H264, camera_nbufs) < 0) { - return -1; - } - return 0; -} - -int open_camera() -{ - camera = device_open("CAMERA", "/dev/video0"); - - if (device_open_buffer_list(camera, true, camera_width, camera_height, camera_format, camera_nbufs) < 0) { - return -1; - } - - DEVICE_SET_OPTION(camera, EXPOSURE, 1148); - DEVICE_SET_OPTION(camera, ANALOGUE_GAIN, 938); - DEVICE_SET_OPTION(camera, DIGITAL_GAIN, 256); - - isp_srgb = device_open("ISP-SRGB", "/dev/video13"); - //isp_srgb->allow_dma = false; - isp_yuuv = device_open("ISP-YUUV", "/dev/video14"); - isp_yuuv->output_device = isp_srgb; - isp_yuuv_low = device_open("ISP-YUUV-LOW", "/dev/video15"); - isp_yuuv_low->output_device = isp_srgb; - codec_jpeg = device_open("JPEG", "/dev/video31"); - //codec_jpeg->allow_dma = false; - codec_h264 = device_open("H264", "/dev/video11"); - //codec_h264->allow_dma = false; - - if (open_isp(camera->capture_list, "/dev/video13", "/dev/video14", "/dev/video15") < 0) { - return -1; - } - if (open_jpeg(camera_use_low ? isp_yuuv_low->capture_list : isp_yuuv->capture_list, "/dev/video31") < 0) { - return -1; - } - if (open_h264(camera_use_low ? isp_yuuv_low->capture_list : isp_yuuv->capture_list, "/dev/video11") < 0) { - return -1; - } - - return 0; -} - -bool check_streaming() -{ - return http_jpeg_needs_buffer() || http_h264_needs_buffer(); -} - -int main(int argc, char *argv[]) -{ - if (open_camera() < 0) { - return -1; - } - - link_t links[] = { - { - camera, - { isp_srgb }, - { NULL, check_streaming } - }, - { - isp_yuuv, - { - camera_use_low ? NULL : codec_jpeg, - //camera_use_low ? NULL : codec_h264, - }, - { NULL, NULL } - }, - { - isp_yuuv_low, - { - camera_use_low ? codec_jpeg : NULL, - //camera_use_low ? codec_h264 : NULL, - }, - { NULL, NULL } - }, - { - codec_jpeg, - { }, - { http_jpeg_capture, http_jpeg_needs_buffer } - }, - { - // codec_h264, - // { }, - // { http_h264_capture, http_h264_needs_buffer } - }, - { NULL } - }; - - http_method_t http_methods[] = { - { "GET / ", http_index }, - { "GET /snapshot ", http_snapshot }, - { "GET /stream ", http_stream }, - { "GET /?action=snapshot ", http_snapshot }, - { "GET /?action=stream ", http_stream }, - { "GET /video ", http_video_html }, - { "GET /video.h264 ", http_video }, - { "GET /jmuxer.min.js ", http_jmuxer_js }, - { NULL, NULL } - }; - - sigaction(SIGPIPE, &(struct sigaction){SIG_IGN}, NULL); - - int http_fd = http_server(9092, 5, http_methods); - - bool running = false; - links_loop(links, &running); - - close(http_fd); - -error: - device_close(isp_yuuv_low); - device_close(isp_yuuv); - device_close(isp_srgb); - device_close(camera); - return 0; -} diff --git a/http/http.c b/http/http.c index 8a9d505..be9a4fc 100644 --- a/http/http.c +++ b/http/http.c @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include "http/http.h" @@ -127,6 +129,8 @@ int http_server(int listen_port, int maxcons, http_method_t *methods) return -1; } + sigaction(SIGPIPE, &(struct sigaction){ SIG_IGN }, NULL); + while (maxcons-- > 0) { char name[20]; sprintf(name, "HTTP%d/%d", listen_port, maxcons); diff --git a/hw/device.c b/hw/device.c index 69ce5bf..3ee2842 100644 --- a/hw/device.c +++ b/hw/device.c @@ -56,6 +56,10 @@ int device_open_buffer_list(device_t *dev, bool do_capture, unsigned width, unsi struct buffer_list_s **buf_list = NULL; bool do_mmap = false; + if (!dev) { + return -1; + } + if (do_capture) { buf_list = &dev->capture_list; do_mmap = true; @@ -134,6 +138,10 @@ int device_consume_event(device_t *dev) { struct v4l2_event event; + if (!dev) { + return -1; + } + E_LOG_DEBUG(dev, "Consuming V4L2 event ..."); E_XIOCTL(dev, dev->fd, VIDIOC_DQEVENT, &event, "Got some V4L2 device event, but where is it?"); @@ -154,6 +162,10 @@ error: int device_video_force_key(device_t *dev) { + if (!dev) { + return -1; + } + struct v4l2_control ctl = {0}; ctl.id = V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME; ctl.value = 1; @@ -167,6 +179,11 @@ error: int device_set_fps(device_t *dev, int desired_fps) { struct v4l2_streamparm setfps = {0}; + + if (!dev) { + return -1; + } + setfps.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; setfps.parm.output.timeperframe.numerator = 1; setfps.parm.output.timeperframe.denominator = desired_fps; @@ -180,6 +197,11 @@ error: int device_set_option(device_t *dev, const char *name, uint32_t id, int32_t value) { struct v4l2_control ctl = {0}; + + if (!dev) { + return -1; + } + ctl.id = id; ctl.value = value; E_LOG_DEBUG(dev, "Configuring option %s (%08x) = %d", name, id, value); diff --git a/hw/links.h b/hw/links.h index fd152ba..b7a06db 100644 --- a/hw/links.h +++ b/hw/links.h @@ -2,7 +2,9 @@ #include "v4l2.h" -typedef void (*link_on_buffer)(struct buffer_s *buf); +typedef struct buffer_s buffer_t; + +typedef void (*link_on_buffer)(buffer_t *buf); typedef bool (*link_check_streaming)(); typedef struct link_s {