Skip to contentSkip to conversation

v4l2-compliance: call select before dequeuing event

PATCH Superseded Deborah Brouwer 2 participants 3 messages Jun 7, 2023 – Jun 9, 2023 Media (V4L/DVB)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 v4l2-compliance: call select before dequeuing event

When the streaming option is called with a stateful decoder, testUserPtr() and testDmaBuf() will hang indefinitely when attempting to dequeue a source change event. To prevent this call select() with a timeout.

---
It looks like this was the same issue that was previously fixed for testMmap() in commit f0a5b17d9 ("v4l2-compliance: add timeout when waiting for event").

 utils/v4l2-compliance/v4l2-test-buffers.cpp | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
utils/v4l2-compliance/v4l2-test-buffers.cpp +18 -0
index a097a0ff..0396e17e 100644
@@ -1746,8 +1746,17 @@ int testUserPtr(struct node *node, struct node *node_m2m_cap, unsigned frame_cou
 
 		if (node->is_m2m) {
 			if (node->codec_mask & STATEFUL_DECODER) {
+				int fd_flags = fcntl(node->g_fd(), F_GETFL);
+				struct timeval tv = { 1, 0 };
+				fd_set efds;
 				v4l2_event ev;
 
+				fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
+				FD_ZERO(&efds);
+				FD_SET(node->g_fd(), &efds);
+				ret = select(node->g_fd() + 1, nullptr, nullptr, &efds, &tv);
+				fail_on_test_val(ret < 0, ret);
+				fail_on_test(ret == 0);
 				fail_on_test(node->dqevent(ev));
 				fail_on_test(ev.type != V4L2_EVENT_SOURCE_CHANGE);
 				fail_on_test(!(ev.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION));
@@ -1949,8 +1958,17 @@ int testDmaBuf(struct node *expbuf_node, struct node *node, struct node *node_m2
 
 		if (node->is_m2m) {
 			if (node->codec_mask & STATEFUL_DECODER) {
+				int fd_flags = fcntl(node->g_fd(), F_GETFL);
+				struct timeval tv = { 1, 0 };
+				fd_set efds;
 				v4l2_event ev;
 
+				fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
+				FD_ZERO(&efds);
+				FD_SET(node->g_fd(), &efds);
+				ret = select(node->g_fd() + 1, nullptr, nullptr, &efds, &tv);
+				fail_on_test_val(ret < 0, ret);
+				fail_on_test(ret == 0);
 				fail_on_test(node->dqevent(ev));
 				fail_on_test(ev.type != V4L2_EVENT_SOURCE_CHANGE);
 				fail_on_test(!(ev.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION));
Signature · 1 line
2.40.1
#1 lore raw
Quoted 23 lines · Deborah Brouwer

When the streaming option is called with a stateful decoder, testUserPtr() and testDmaBuf() will hang indefinitely when attempting to dequeue a source change event. To prevent this call select() with a timeout.

---
It looks like this was the same issue that was previously fixed for testMmap() in commit f0a5b17d9 ("v4l2-compliance: add timeout when waiting for event").

 utils/v4l2-compliance/v4l2-test-buffers.cpp | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
utils/v4l2-compliance/v4l2-test-buffers.cpp +2 -0
index a097a0ff..0396e17e 100644
@@ -1746,8 +1746,17 @@ int testUserPtr(struct node *node, struct node *node_m2m_cap, unsigned frame_cou
 
 		if (node->is_m2m) {
 			if (node->codec_mask & STATEFUL_DECODER) {
+				int fd_flags = fcntl(node->g_fd(), F_GETFL);
+				struct timeval tv = { 1, 0 };

utils/v4l2-compliance/v4l2-test-buffers.cpp int testUserPtr(struct node *node, struct node *node_m2m_cap, unsigned frame_cou · line 1760

Quoted 10 lines
utils/v4l2-compliance/v4l2-test-buffers.cpp +7 -0
+				fd_set efds;
 				v4l2_event ev;
 
+				fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
+				FD_ZERO(&efds);
+				FD_SET(node->g_fd(), &efds);
+				ret = select(node->g_fd() + 1, nullptr, nullptr, &efds, &tv);
+				fail_on_test_val(ret < 0, ret);
+				fail_on_test(ret == 0);
 				fail_on_test(node->dqevent(ev));

This code keeps the fd in non-blocking mode. You need to add a fcntl(node->g_fd(), F_SETFL, fd_flags) line here. See also testMmap().

Quoted 8 lines
utils/v4l2-compliance/v4l2-test-buffers.cpp +2 -0
 				fail_on_test(ev.type != V4L2_EVENT_SOURCE_CHANGE);
 				fail_on_test(!(ev.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION));
@@ -1949,8 +1958,17 @@ int testDmaBuf(struct node *expbuf_node, struct node *node, struct node *node_m2
 
 		if (node->is_m2m) {
 			if (node->codec_mask & STATEFUL_DECODER) {
+				int fd_flags = fcntl(node->g_fd(), F_GETFL);
+				struct timeval tv = { 1, 0 };

utils/v4l2-compliance/v4l2-test-buffers.cpp int testDmaBuf(struct node *expbuf_node, struct node *node, struct node *node_m2 · line 1972

Quoted 10 lines
utils/v4l2-compliance/v4l2-test-buffers.cpp +7 -0
+				fd_set efds;
 				v4l2_event ev;
 
+				fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
+				FD_ZERO(&efds);
+				FD_SET(node->g_fd(), &efds);
+				ret = select(node->g_fd() + 1, nullptr, nullptr, &efds, &tv);
+				fail_on_test_val(ret < 0, ret);
+				fail_on_test(ret == 0);
 				fail_on_test(node->dqevent(ev));

Ditto.

utils/v4l2-compliance/v4l2-test-buffers.cpp int testDmaBuf(struct node *expbuf_node, struct node *node, struct node *node_m2 · line 1974

Quoted 2 lines
utils/v4l2-compliance/v4l2-test-buffers.cpp +0 -0
 				fail_on_test(ev.type != V4L2_EVENT_SOURCE_CHANGE);
 				fail_on_test(!(ev.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION));

Regards,

Hans

#2 lore raw
Quoted 27 lines · Hans Verkuil
Quoted 26 lines · Deborah Brouwer

When the streaming option is called with a stateful decoder, testUserPtr() and testDmaBuf() will hang indefinitely when attempting to dequeue a source change event. To prevent this call select() with a timeout.

---
It looks like this was the same issue that was previously fixed for testMmap() in commit f0a5b17d9 ("v4l2-compliance: add timeout when waiting for event").

 utils/v4l2-compliance/v4l2-test-buffers.cpp | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
utils/v4l2-compliance/v4l2-test-buffers.cpp +3 -0
index a097a0ff..0396e17e 100644
@@ -1746,8 +1746,17 @@ int testUserPtr(struct node *node, struct node *node_m2m_cap, unsigned frame_cou
 
 		if (node->is_m2m) {
 			if (node->codec_mask & STATEFUL_DECODER) {
+				int fd_flags = fcntl(node->g_fd(), F_GETFL);
+				struct timeval tv = { 1, 0 };
+				fd_set efds;
 				v4l2_event ev;
 
Quoted 10 lines

utils/v4l2-compliance/v4l2-test-buffers.cpp int testUserPtr(struct node *node, struct node *node_m2m_cap, unsigned frame_cou · line 1760

Quoted 7 lines
utils/v4l2-compliance/v4l2-test-buffers.cpp +6 -0
+				fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
+				FD_ZERO(&efds);
+				FD_SET(node->g_fd(), &efds);
+				ret = select(node->g_fd() + 1, nullptr, nullptr, &efds, &tv);
+				fail_on_test_val(ret < 0, ret);
+				fail_on_test(ret == 0);
 				fail_on_test(node->dqevent(ev));

This code keeps the fd in non-blocking mode. You need to add a fcntl(node->g_fd(), F_SETFL, fd_flags) line here. See also testMmap().

Oh sorry yes I missed that line. I'll send v2 with it fixed.

Related question: would you ever expect this test to do anything other than timeout? Probably I am missing something, but it seems like there are no output buffers being queued, so there could never be a source change event?
I was just testing it with vicodec and I always get the dmesg error message "No buffer was provided with the request" and then it times out.

Quoted 28 lines
Quoted 9 lines
utils/v4l2-compliance/v4l2-test-buffers.cpp +2 -0
 				fail_on_test(ev.type != V4L2_EVENT_SOURCE_CHANGE);
 				fail_on_test(!(ev.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION));
@@ -1949,8 +1958,17 @@ int testDmaBuf(struct node *expbuf_node, struct node *node, struct node *node_m2
 
 		if (node->is_m2m) {
 			if (node->codec_mask & STATEFUL_DECODER) {
+				int fd_flags = fcntl(node->g_fd(), F_GETFL);
+				struct timeval tv = { 1, 0 };

utils/v4l2-compliance/v4l2-test-buffers.cpp int testDmaBuf(struct node *expbuf_node, struct node *node, struct node *node_m2 · line 1972

Quoted 10 lines
utils/v4l2-compliance/v4l2-test-buffers.cpp +7 -0
+				fd_set efds;
 				v4l2_event ev;
 
+				fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
+				FD_ZERO(&efds);
+				FD_SET(node->g_fd(), &efds);
+				ret = select(node->g_fd() + 1, nullptr, nullptr, &efds, &tv);
+				fail_on_test_val(ret < 0, ret);
+				fail_on_test(ret == 0);
 				fail_on_test(node->dqevent(ev));

Ditto.

utils/v4l2-compliance/v4l2-test-buffers.cpp int testDmaBuf(struct node *expbuf_node, struct node *node, struct node *node_m2 · line 1974

Quoted 2 lines
utils/v4l2-compliance/v4l2-test-buffers.cpp +0 -0
 				fail_on_test(ev.type != V4L2_EVENT_SOURCE_CHANGE);
 				fail_on_test(!(ev.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION));

Regards,

Hans