Skip to content

Commit 04ac445

Browse files
committed
drivers: video: Add video buffer state
Add a video_buf_state field to track the current state of a buffer when it is flowing. This also helps to check whether a buffer can be safely enqueued or dequeued, data are valid or not, etc. Signed-off-by: Phi Bang Nguyen <[email protected]>
1 parent 891c1d0 commit 04ac445

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

drivers/video/video_common.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align, k_tim
6969
vbuf->buffer = block->data;
7070
vbuf->size = size;
7171
vbuf->bytesused = 0;
72+
vbuf->state = VIDEO_BUF_STATE_DONE;
7273

7374
return vbuf;
7475
}
@@ -112,6 +113,12 @@ int video_enqueue(const struct device *dev, struct video_buffer *buf)
112113
return -ENOSYS;
113114
}
114115

116+
if (video_buf[buf->index].state != VIDEO_BUF_STATE_DONE) {
117+
return -EINVAL;
118+
}
119+
120+
video_buf[buf->index].state = VIDEO_BUF_STATE_QUEUED;
121+
115122
return api->enqueue(dev, &video_buf[buf->index]);
116123
}
117124

include/zephyr/drivers/video.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ enum video_buf_type {
5656
VIDEO_BUF_TYPE_OUTPUT,
5757
};
5858

59+
/**
60+
* @brief video_buf_state enum
61+
*
62+
* Current state of a video buffer.
63+
* This helps to keep track of state of a buffer for debugging or to do some guard
64+
* checks, e.g. prevents re-enqueuing a buffer already queued, etc.
65+
*/
66+
enum video_buf_state {
67+
/** buffer queued in video subsystem and driver */
68+
VIDEO_BUF_STATE_QUEUED,
69+
/** buffer filled and can be dequeued by application */
70+
VIDEO_BUF_STATE_DONE,
71+
/** buffer returned from the driver with an error */
72+
VIDEO_BUF_STATE_ERROR,
73+
};
74+
5975
/**
6076
* @struct video_format
6177
* @brief Video format structure
@@ -146,6 +162,8 @@ struct video_caps {
146162
struct video_buffer {
147163
/** type of the buffer */
148164
enum video_buf_type type;
165+
/** current state of the buffer */
166+
enum video_buf_state state;
149167
/** pointer to driver specific data. */
150168
void *driver_data;
151169
/** pointer to the start of the buffer. */
@@ -591,6 +609,7 @@ static inline int video_dequeue(const struct device *dev, struct video_buffer **
591609
k_timeout_t timeout)
592610
{
593611
const struct video_driver_api *api;
612+
int ret;
594613

595614
__ASSERT_NO_MSG(dev != NULL);
596615
__ASSERT_NO_MSG(buf != NULL);
@@ -600,7 +619,13 @@ static inline int video_dequeue(const struct device *dev, struct video_buffer **
600619
return -ENOSYS;
601620
}
602621

603-
return api->dequeue(dev, buf, timeout);
622+
ret = api->dequeue(dev, buf, timeout);
623+
624+
if (ret == 0) {
625+
(*buf)->state = VIDEO_BUF_STATE_DONE;
626+
}
627+
628+
return ret;
604629
}
605630

606631
/**

0 commit comments

Comments
 (0)