device: allow to use dummy as a way to test streamer
This adds loopback tests via `tests/`
This commit is contained in:
parent
2e9718fea7
commit
c9600d1253
@ -76,6 +76,7 @@ option_value_t camera_formats[] = {
|
|||||||
option_value_t camera_type[] = {
|
option_value_t camera_type[] = {
|
||||||
{ "v4l2", CAMERA_V4L2 },
|
{ "v4l2", CAMERA_V4L2 },
|
||||||
{ "libcamera", CAMERA_LIBCAMERA },
|
{ "libcamera", CAMERA_LIBCAMERA },
|
||||||
|
{ "dummy", CAMERA_DUMMY },
|
||||||
{}
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -12,7 +12,8 @@
|
|||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
CAMERA_V4L2 = 0,
|
CAMERA_V4L2 = 0,
|
||||||
CAMERA_LIBCAMERA
|
CAMERA_LIBCAMERA,
|
||||||
|
CAMERA_DUMMY
|
||||||
} camera_type_t;
|
} camera_type_t;
|
||||||
|
|
||||||
typedef struct camera_output_options_s {
|
typedef struct camera_output_options_s {
|
||||||
|
@ -73,6 +73,28 @@ static int camera_configure_input_libcamera(camera_t *camera)
|
|||||||
return camera_configure_pipeline(camera, camera_capture);
|
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)
|
int camera_configure_input(camera_t *camera)
|
||||||
{
|
{
|
||||||
switch (camera->options.type) {
|
switch (camera->options.type) {
|
||||||
@ -82,6 +104,9 @@ int camera_configure_input(camera_t *camera)
|
|||||||
case CAMERA_LIBCAMERA:
|
case CAMERA_LIBCAMERA:
|
||||||
return camera_configure_input_libcamera(camera);
|
return camera_configure_input_libcamera(camera);
|
||||||
|
|
||||||
|
case CAMERA_DUMMY:
|
||||||
|
return camera_configure_input_dummy(camera);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
LOG_INFO(camera, "Unsupported camera type");
|
LOG_INFO(camera, "Unsupported camera type");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -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_v4l2_open(const char *name, const char *path);
|
||||||
device_t *device_libcamera_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);
|
||||||
|
@ -1,11 +1,18 @@
|
|||||||
#include "dummy.h"
|
#include "dummy.h"
|
||||||
#include "device/buffer.h"
|
#include "device/buffer.h"
|
||||||
|
#include "device/buffer_list.h"
|
||||||
|
#include "util/opts/log.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
int dummy_buffer_open(buffer_t *buf)
|
int dummy_buffer_open(buffer_t *buf)
|
||||||
{
|
{
|
||||||
buf->dummy = calloc(1, sizeof(buffer_dummy_t));
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,15 +23,39 @@ void dummy_buffer_close(buffer_t *buf)
|
|||||||
|
|
||||||
int dummy_buffer_enqueue(buffer_t *buf, const char *who)
|
int dummy_buffer_enqueue(buffer_t *buf, const char *who)
|
||||||
{
|
{
|
||||||
|
unsigned index = buf->index;
|
||||||
|
if (write(buf->buf_list->dummy->fds[1], &index, sizeof(index)) != sizeof(index)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int dummy_buffer_list_dequeue(buffer_list_t *buf_list, buffer_t **bufp)
|
int dummy_buffer_list_dequeue(buffer_list_t *buf_list, buffer_t **bufp)
|
||||||
{
|
{
|
||||||
|
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;
|
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)
|
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;
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,69 @@
|
|||||||
#include "dummy.h"
|
#include "dummy.h"
|
||||||
#include "device/buffer_list.h"
|
#include "device/buffer_list.h"
|
||||||
|
#include "device/device.h"
|
||||||
|
#include "util/opts/log.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
int dummy_buffer_list_open(buffer_list_t *buf_list)
|
int dummy_buffer_list_open(buffer_list_t *buf_list)
|
||||||
{
|
{
|
||||||
|
int fd = -1;
|
||||||
|
|
||||||
buf_list->dummy = calloc(1, sizeof(buffer_list_dummy_t));
|
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)
|
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)
|
free(buf_list->dummy);
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int dummy_buffer_list_set_stream(buffer_list_t *buf_list, bool do_on)
|
int dummy_buffer_list_set_stream(buffer_list_t *buf_list, bool do_on)
|
||||||
{
|
{
|
||||||
return -1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
int dummy_device_open(device_t *dev)
|
int dummy_device_open(device_t *dev)
|
||||||
{
|
{
|
||||||
|
dev->opts.allow_dma = false;
|
||||||
dev->dummy = calloc(1, sizeof(device_dummy_t));
|
dev->dummy = calloc(1, sizeof(device_dummy_t));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
typedef struct buffer_s buffer_t;
|
typedef struct buffer_s buffer_t;
|
||||||
typedef struct buffer_list_s buffer_list_t;
|
typedef struct buffer_list_s buffer_list_t;
|
||||||
@ -12,6 +13,9 @@ typedef struct device_dummy_s {
|
|||||||
} device_dummy_t;
|
} device_dummy_t;
|
||||||
|
|
||||||
typedef struct buffer_list_dummy_s {
|
typedef struct buffer_list_dummy_s {
|
||||||
|
int fds[2];
|
||||||
|
void *data;
|
||||||
|
size_t length;
|
||||||
} buffer_list_dummy_t;
|
} buffer_list_dummy_t;
|
||||||
|
|
||||||
typedef struct buffer_dummy_s {
|
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);
|
int dummy_buffer_list_open(buffer_list_t *buf_list);
|
||||||
void dummy_buffer_list_close(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);
|
int dummy_buffer_list_set_stream(buffer_list_t *buf_list, bool do_on);
|
||||||
|
BIN
tests/capture.bg10p
Normal file
BIN
tests/capture.bg10p
Normal file
Binary file not shown.
BIN
tests/capture.h264
Normal file
BIN
tests/capture.h264
Normal file
Binary file not shown.
BIN
tests/capture.jpeg
Normal file
BIN
tests/capture.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 309 KiB |
13
tests/capture.sh
Executable file
13
tests/capture.sh
Executable file
@ -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
|
BIN
tests/capture.yuv420
Normal file
BIN
tests/capture.yuv420
Normal file
Binary file not shown.
49
tests/dummy.sh
Executable file
49
tests/dummy.sh
Executable file
@ -0,0 +1,49 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [[ $# -lt 1 ]]; then
|
||||||
|
echo "usage: $0 <input-file> [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 \
|
||||||
|
"$@"
|
@ -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 { \
|
#define ERR_IOCTL(dev, _fd, _request, _value, _msg, ...) do { \
|
||||||
int ret; \
|
int ret; \
|
||||||
if ((ret = ioctl_retried(dev_name(dev), _fd, _request, _value)) < 0) { \
|
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)
|
} while(0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user