diff --git a/cmd/camera-streamer/opts.c b/cmd/camera-streamer/opts.c index 77ae39c..e969870 100644 --- a/cmd/camera-streamer/opts.c +++ b/cmd/camera-streamer/opts.c @@ -76,6 +76,7 @@ option_value_t camera_formats[] = { option_value_t camera_type[] = { { "v4l2", CAMERA_V4L2 }, { "libcamera", CAMERA_LIBCAMERA }, + { "dummy", CAMERA_DUMMY }, {} }; diff --git a/device/camera/camera.h b/device/camera/camera.h index 60ad814..6ecac82 100644 --- a/device/camera/camera.h +++ b/device/camera/camera.h @@ -12,7 +12,8 @@ typedef enum { CAMERA_V4L2 = 0, - CAMERA_LIBCAMERA + CAMERA_LIBCAMERA, + CAMERA_DUMMY } camera_type_t; typedef struct camera_output_options_s { diff --git a/device/camera/camera_input.c b/device/camera/camera_input.c index 176431f..419a4b0 100644 --- a/device/camera/camera_input.c +++ b/device/camera/camera_input.c @@ -73,6 +73,28 @@ static int camera_configure_input_libcamera(camera_t *camera) return camera_configure_pipeline(camera, camera_capture); } +static int camera_configure_input_dummy(camera_t *camera) +{ + camera->camera = device_dummy_open(camera->name, camera->options.path); + if (!camera->camera) { + return -1; + } + + buffer_format_t fmt = { + .width = camera->options.width, + .height = camera->options.height, + .format = camera->options.format, + .nbufs = camera->options.nbufs + }; + + buffer_list_t *camera_capture = device_open_buffer_list(camera->camera, true, fmt, true); + if (!camera_capture) { + return -1; + } + + return camera_configure_pipeline(camera, camera_capture); +} + int camera_configure_input(camera_t *camera) { switch (camera->options.type) { @@ -82,6 +104,9 @@ int camera_configure_input(camera_t *camera) case CAMERA_LIBCAMERA: return camera_configure_input_libcamera(camera); + case CAMERA_DUMMY: + return camera_configure_input_dummy(camera); + default: LOG_INFO(camera, "Unsupported camera type"); return -1; diff --git a/device/device.h b/device/device.h index 1263221..968db00 100644 --- a/device/device.h +++ b/device/device.h @@ -77,3 +77,4 @@ void device_set_option_list(device_t *dev, const char *option_list); device_t *device_v4l2_open(const char *name, const char *path); device_t *device_libcamera_open(const char *name, const char *path); +device_t *device_dummy_open(const char *name, const char *path); diff --git a/device/dummy/buffer.c b/device/dummy/buffer.c index a5f85be..eaba91e 100644 --- a/device/dummy/buffer.c +++ b/device/dummy/buffer.c @@ -1,11 +1,18 @@ #include "dummy.h" #include "device/buffer.h" +#include "device/buffer_list.h" +#include "util/opts/log.h" #include +#include +#include int dummy_buffer_open(buffer_t *buf) { buf->dummy = calloc(1, sizeof(buffer_dummy_t)); + buf->start = buf->buf_list->dummy->data; + buf->used = buf->buf_list->dummy->length; + buf->length = buf->buf_list->dummy->length; return 0; } @@ -16,15 +23,39 @@ void dummy_buffer_close(buffer_t *buf) int dummy_buffer_enqueue(buffer_t *buf, const char *who) { - return -1; + unsigned index = buf->index; + if (write(buf->buf_list->dummy->fds[1], &index, sizeof(index)) != sizeof(index)) { + return -1; + } + return 0; } int dummy_buffer_list_dequeue(buffer_list_t *buf_list, buffer_t **bufp) { - return -1; + unsigned index = 0; + int n = read(buf_list->dummy->fds[0], &index, sizeof(index)); + if (n != sizeof(index)) { + LOG_INFO(buf_list, "Received invalid result from `read`: %d", n); + return -1; + } + + if (index >= (unsigned)buf_list->nbufs) { + LOG_INFO(buf_list, "Received invalid index from `read`: %d >= %d", index, buf_list->nbufs); + return -1; + } + + *bufp = buf_list->bufs[index]; + return 0; } int dummy_buffer_list_pollfd(buffer_list_t *buf_list, struct pollfd *pollfd, bool can_dequeue) { - return -1; + int count_enqueued = buffer_list_count_enqueued(buf_list); + pollfd->fd = buf_list->dummy->fds[0]; // write end + pollfd->events = POLLHUP; + if (can_dequeue && count_enqueued > 0) { + pollfd->events |= POLLIN; + } + pollfd->revents = 0; + return 0; } diff --git a/device/dummy/buffer_list.c b/device/dummy/buffer_list.c index 1b68d8c..0f45e2d 100644 --- a/device/dummy/buffer_list.c +++ b/device/dummy/buffer_list.c @@ -1,25 +1,69 @@ #include "dummy.h" #include "device/buffer_list.h" +#include "device/device.h" +#include "util/opts/log.h" #include +#include +#include int dummy_buffer_list_open(buffer_list_t *buf_list) { + int fd = -1; + buf_list->dummy = calloc(1, sizeof(buffer_list_dummy_t)); - return 0; + buf_list->dummy->fds[0] = -1; + buf_list->dummy->fds[1] = -1; + + if (!buf_list->do_capture) { + LOG_ERROR(buf_list, "Only capture mode supported"); + } + + if (pipe2(buf_list->dummy->fds, O_DIRECT|O_CLOEXEC) < 0) { + LOG_INFO(buf_list, "Cannot open `pipe2`."); + return -1; + } + + fd = open(buf_list->dev->path, O_RDWR|O_NONBLOCK); + if (fd < 0) { + LOG_ERROR(buf_list, "Can't open device: %s", buf_list->dev->path); + } + + struct stat st; + if (fstat(fd, &st) < 0) { + LOG_ERROR(buf_list, "Can't get fstat: %s", buf_list->dev->path); + } + + buf_list->dummy->data = malloc(st.st_size); + if (!buf_list->dummy->data) { + LOG_ERROR(buf_list, "Can't allocate %ld bytes for %s", st.st_size, buf_list->dev->path); + } + + buf_list->dummy->length = read(fd, buf_list->dummy->data, st.st_size); + if (!buf_list->dummy->data) { + LOG_ERROR(buf_list, "Can't read %ld bytes for %s. Only read %zu.", st.st_size, buf_list->dev->path, buf_list->dummy->length); + } + + close(fd); + return buf_list->fmt.nbufs; + +error: + close(fd); + return -1; } void dummy_buffer_list_close(buffer_list_t *buf_list) { - free(buf_list->dummy); -} + if (buf_list->dummy) { + close(buf_list->dummy->fds[0]); + close(buf_list->dummy->fds[1]); + free(buf_list->dummy->data); + } -int dummy_buffer_list_set_buffers(buffer_list_t *buf_list, int nbufs) -{ - return -1; + free(buf_list->dummy); } int dummy_buffer_list_set_stream(buffer_list_t *buf_list, bool do_on) { - return -1; + return 0; } diff --git a/device/dummy/device.c b/device/dummy/device.c index 2ea7462..bfa0681 100644 --- a/device/dummy/device.c +++ b/device/dummy/device.c @@ -5,6 +5,7 @@ int dummy_device_open(device_t *dev) { + dev->opts.allow_dma = false; dev->dummy = calloc(1, sizeof(device_dummy_t)); return 0; } diff --git a/device/dummy/dummy.h b/device/dummy/dummy.h index 82297dd..bd51f9d 100644 --- a/device/dummy/dummy.h +++ b/device/dummy/dummy.h @@ -2,6 +2,7 @@ #include #include +#include typedef struct buffer_s buffer_t; typedef struct buffer_list_s buffer_list_t; @@ -12,6 +13,9 @@ typedef struct device_dummy_s { } device_dummy_t; typedef struct buffer_list_dummy_s { + int fds[2]; + void *data; + size_t length; } buffer_list_dummy_t; typedef struct buffer_dummy_s { @@ -32,5 +36,4 @@ int dummy_buffer_list_pollfd(buffer_list_t *buf_list, struct pollfd *pollfd, boo int dummy_buffer_list_open(buffer_list_t *buf_list); void dummy_buffer_list_close(buffer_list_t *buf_list); -int dummy_buffer_list_set_buffers(buffer_list_t *buf_list, int nbufs); int dummy_buffer_list_set_stream(buffer_list_t *buf_list, bool do_on); diff --git a/tests/capture.bg10p b/tests/capture.bg10p new file mode 100644 index 0000000..5a3e004 Binary files /dev/null and b/tests/capture.bg10p differ diff --git a/tests/capture.h264 b/tests/capture.h264 new file mode 100644 index 0000000..d12c6b9 Binary files /dev/null and b/tests/capture.h264 differ diff --git a/tests/capture.jpeg b/tests/capture.jpeg new file mode 100644 index 0000000..b0e541e Binary files /dev/null and b/tests/capture.jpeg differ diff --git a/tests/capture.sh b/tests/capture.sh new file mode 100755 index 0000000..7650ef4 --- /dev/null +++ b/tests/capture.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +cd "$SCRIPT_DIR" + +set -xeo pipefail + +[[ -e capture.jpeg ]] || libcamera-jpeg --timeout 1000 --output capture.jpeg --width 1920 --height 1080 --encoding jpg +[[ -e capture.yuv420 ]] || libcamera-jpeg --timeout 1000 --output capture.yuv420 --width 1920 --height 1080 --encoding yuv420 +[[ -e capture.h264 ]] || libcamera-vid --frames 1 --output capture.h264 --width 1920 --height 1080 --codec h264 --profile main --level 4.2 + +# This is not ideal as `libcamera-raw` does not respect `--frames` +[[ -e capture.bg10p ]] || libcamera-raw --frames 1 --timeout 300 --verbose 2 --flush --output capture.bg10p --width 2304 --height 1296 diff --git a/tests/capture.yuv420 b/tests/capture.yuv420 new file mode 100644 index 0000000..a218d16 Binary files /dev/null and b/tests/capture.yuv420 differ diff --git a/tests/dummy.sh b/tests/dummy.sh new file mode 100755 index 0000000..44e01e2 --- /dev/null +++ b/tests/dummy.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +if [[ $# -lt 1 ]]; then + echo "usage: $0 [camera-streamer options]" + echo + echo "examples:" + echo " $0 tests/capture.jpeg" + echo " $0 tests/capture.jpeg --video-height=720" + echo " $0 tests/capture.jpeg --snapshot-height=720 --video-height=480" + echo " $0 tests/capture.h264" + exit 1 +fi + +INPUT=$(realpath "$1") +shift + +case "$INPUT" in + *.jpeg) + set -- --camera-format=JPEG --camera-width=1920 --camera-height=1080 "$@" + ;; + + *.yuv420) + set -- --camera-format=YUV420 --camera-width=1920 --camera-height=1080 "$@" + ;; + + *.h264) + set -- --camera-format=H264 --camera-width=1920 --camera-height=1080 "$@" + ;; + + *.bg10p) + set -- --camera-format=BG10P --camera-width=2304 --camera-height=1296 "$@" + ;; + + *) + echo "$0: undefined format for $INPUT." + exit 1 +esac + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +cd "$SCRIPT_DIR/.." + +set -xeo pipefail +make -j$(nproc) + +exec ./camera-streamer \ + --camera-type=dummy \ + --camera-path="$INPUT" \ + --camera.snapshot-height=1080 \ + "$@" diff --git a/util/opts/log.h b/util/opts/log.h index 2e91472..21aac88 100644 --- a/util/opts/log.h +++ b/util/opts/log.h @@ -55,6 +55,6 @@ int ioctl_retried(const char *name, int fd, int request, void *arg); #define ERR_IOCTL(dev, _fd, _request, _value, _msg, ...) do { \ int ret; \ if ((ret = ioctl_retried(dev_name(dev), _fd, _request, _value)) < 0) { \ - LOG_ERROR(dev, "ioctl(ret=%d): " _msg, ret, ##__VA_ARGS__); \ + LOG_ERROR(dev, "ioctl(ret=%d, errno=%d): " _msg, ret, errno, ##__VA_ARGS__); \ } \ } while(0)