From 4d9897c5aa5376f89e0d5ed1534536f680939e0d Mon Sep 17 00:00:00 2001 From: Daniel Scally Date: Tue, 10 Jan 2023 11:58:04 +0000 Subject: lib: Add video_source_type enumeration Add an enum to struct video_source that details the type of source we're dealing with. This will be used to make decisions about buffer allocation and handling in a more explicit way. Use the video_source_type associated with a video_source to decide on the appropriate allocation function and buffer handler at stream on time. This is a more explicit method than relying on the presence of the .alloc_buffers op and allows more flexibility. Reviewed-by: Kieran Bingham Signed-off-by: Daniel Scally --- include/uvcgadget/video-source.h | 13 +++++++++++++ lib/jpg-source.c | 1 + lib/libcamera-source.cpp | 1 + lib/slideshow-source.c | 1 + lib/stream.c | 18 ++++++++++++------ lib/test-source.c | 1 + lib/v4l2-source.c | 1 + 7 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/uvcgadget/video-source.h b/include/uvcgadget/video-source.h index b1a3cf4..f888ce4 100644 --- a/include/uvcgadget/video-source.h +++ b/include/uvcgadget/video-source.h @@ -32,11 +32,24 @@ struct video_source_ops { typedef void(*video_source_buffer_handler_t)(void *, struct video_source *, struct video_buffer *); +/* + * video_source_type - Enumeration of the different kinds of video source + * @VIDEO_SOURCE_DMABUF A source that can share data with the sink via a + * DMA file descriptor. + * @VIDEO_SOURCE_STATIC A source that draws data from an unchanging + * buffer such as a .jpg file + */ +enum video_source_type { + VIDEO_SOURCE_DMABUF, + VIDEO_SOURCE_STATIC, +}; + struct video_source { const struct video_source_ops *ops; struct events *events; video_source_buffer_handler_t handler; void *handler_data; + enum video_source_type type; }; void video_source_set_buffer_handler(struct video_source *src, diff --git a/lib/jpg-source.c b/lib/jpg-source.c index c95d76c..1cc0052 100644 --- a/lib/jpg-source.c +++ b/lib/jpg-source.c @@ -149,6 +149,7 @@ struct video_source *jpg_video_source_create(const char *img_path) memset(src, 0, sizeof *src); src->src.ops = &jpg_source_ops; + src->src.type = VIDEO_SOURCE_STATIC; fd = open(img_path, O_RDONLY); if (fd == -1) { diff --git a/lib/libcamera-source.cpp b/lib/libcamera-source.cpp index c53c915..7b4e212 100644 --- a/lib/libcamera-source.cpp +++ b/lib/libcamera-source.cpp @@ -390,6 +390,7 @@ struct video_source *libcamera_source_create(const char *devname) } src->src.ops = &libcamera_source_ops; + src->src.type = VIDEO_SOURCE_DMABUF; src->cm = std::make_unique(); src->cm->start(); diff --git a/lib/slideshow-source.c b/lib/slideshow-source.c index 42a42a9..6eeab72 100644 --- a/lib/slideshow-source.c +++ b/lib/slideshow-source.c @@ -367,6 +367,7 @@ struct video_source *slideshow_video_source_create(const char *img_dir) memset(src, 0, sizeof *src); src->src.ops = &slideshow_source_ops; + src->src.type = VIDEO_SOURCE_STATIC; strncpy(src->img_dir, img_dir, sizeof(src->img_dir)); diff --git a/lib/stream.c b/lib/stream.c index 63ccb1b..4a562a5 100644 --- a/lib/stream.c +++ b/lib/stream.c @@ -7,6 +7,7 @@ * Contact: Laurent Pinchart */ +#include #include #include #include @@ -183,10 +184,19 @@ static int uvc_stream_start(struct uvc_stream *stream) { printf("Starting video stream.\n"); - if (stream->src->ops->alloc_buffers) + switch (stream->src->type) { + case VIDEO_SOURCE_DMABUF: + video_source_set_buffer_handler(stream->src, uvc_stream_source_process, + stream); return uvc_stream_start_alloc(stream); - else + case VIDEO_SOURCE_STATIC: return uvc_stream_start_no_alloc(stream); + default: + fprintf(stderr, "invalid video source type\n"); + break; + } + + return -EINVAL; } static int uvc_stream_stop(struct uvc_stream *stream) @@ -288,8 +298,4 @@ void uvc_stream_set_video_source(struct uvc_stream *stream, struct video_source *src) { stream->src = src; - - if (stream->src->ops->alloc_buffers) - video_source_set_buffer_handler(src, uvc_stream_source_process, - stream); } diff --git a/lib/test-source.c b/lib/test-source.c index 743d8b3..40a1f69 100644 --- a/lib/test-source.c +++ b/lib/test-source.c @@ -134,6 +134,7 @@ struct video_source *test_video_source_create() memset(src, 0, sizeof *src); src->src.ops = &test_source_ops; + src->src.type = VIDEO_SOURCE_STATIC; return &src->src; } diff --git a/lib/v4l2-source.c b/lib/v4l2-source.c index 8cb9aef..22fe3da 100644 --- a/lib/v4l2-source.c +++ b/lib/v4l2-source.c @@ -170,6 +170,7 @@ struct video_source *v4l2_video_source_create(const char *devname) memset(src, 0, sizeof *src); src->src.ops = &v4l2_source_ops; + src->src.type = VIDEO_SOURCE_DMABUF; src->vdev = v4l2_open(devname); if (!src->vdev) { -- cgit v1.2.3